re PR target/32086 (10% to 20% Performance Regression Between 4.1.3 and 4.3)
[official-gcc.git] / gcc / dbgcnt.c
blob8ab5fd0ea521ea50af03c2e3b14024e9aafb817b
1 /* Debug counter for debugging support
2 Copyright (C) 2006, 2007 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 See dbgcnt.def for usage information. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "errors.h"
27 #include "dbgcnt.h"
29 struct string2counter_map {
30 const char *name;
31 enum debug_counter counter;
34 #define DEBUG_COUNTER(a) { #a , a },
36 static struct string2counter_map map[debug_counter_number_of_counters] =
38 #include "dbgcnt.def"
40 #undef DEBUG_COUNTER
42 #define DEBUG_COUNTER(a) UINT_MAX,
43 static unsigned int limit[debug_counter_number_of_counters] =
45 #include "dbgcnt.def"
47 #undef DEBUG_COUNTER
49 static unsigned int count[debug_counter_number_of_counters];
51 bool
52 dbg_cnt_is_enabled (enum debug_counter index)
54 return count[index] <= limit[index];
57 bool
58 dbg_cnt (enum debug_counter index)
60 count[index]++;
61 return dbg_cnt_is_enabled (index);
65 static void
66 dbg_cnt_set_limit_by_index (enum debug_counter index, int value)
68 limit[index] = value;
70 fprintf (stderr, "dbg_cnt '%s' set to %d\n", map[index].name, value);
73 static bool
74 dbg_cnt_set_limit_by_name (const char *name, int len, int value)
76 int i;
77 for (i = debug_counter_number_of_counters - 1; i >= 0; i--)
78 if (!strncmp (map[i].name, name, len))
79 break;
81 if (i < 0)
82 return false;
84 dbg_cnt_set_limit_by_index (i, value);
85 return true;
89 /* Process a single "name:value" pair.
90 Returns NULL if there's no valid pair is found.
91 Otherwise returns a pointer to the end of the pair. */
93 static const char *
94 dbg_cnt_process_single_pair (const char *arg)
96 char *colon = strchr (arg, ':');
97 char *endptr = NULL;
98 int value;
100 if (colon == NULL)
101 return NULL;
103 value = strtol (colon + 1, &endptr, 10);
105 if (endptr != NULL && endptr != colon + 1
106 && dbg_cnt_set_limit_by_name (arg, colon - arg, value))
107 return endptr;
109 return NULL;
112 void
113 dbg_cnt_process_opt (const char *arg)
115 const char *start = arg;
116 const char *next;
117 do {
118 next = dbg_cnt_process_single_pair (arg);
119 if (next == NULL)
120 break;
121 } while (*next == ',' && (arg = next + 1));
123 if (next == NULL || *next != 0)
125 char *buffer = alloca (arg - start + 2);
126 sprintf (buffer, "%*c", (int)(1 + (arg - start)), '^');
127 error ("Can not find a valid counter:value pair:");
128 error ("-fdbg-cnt=%s", start);
129 error (" %s", buffer);
133 /* Print name, limit and count of all counters. */
135 void dbg_cnt_list_all_counters (void)
137 int i;
138 printf (" %-30s %-5s %-5s\n", "counter name", "limit", "value");
139 printf ("----------------------------------------------\n");
140 for (i = 0; i < debug_counter_number_of_counters; i++)
141 printf (" %-30s %5d %5u\n",
142 map[i].name, limit[map[i].counter], count[map[i].counter]);
143 printf ("\n");