2010-11-11 Jakub Jelinek <jakub@redhat.com>
[official-gcc.git] / gcc / genattr.c
blobacb3a382588e37ecb348c918920e956f3e17ebf8
1 /* Generate attribute information (insn-attr.h) from machine description.
2 Copyright (C) 1991, 1994, 1996, 1998, 1999, 2000, 2003, 2004, 2007, 2008,
3 2010 Free Software Foundation, Inc.
4 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 #include "bconfig.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "errors.h"
29 #include "read-md.h"
30 #include "gensupport.h"
33 static void write_upcase (const char *);
34 static void gen_attr (rtx);
36 static void
37 write_upcase (const char *str)
39 for (; *str; str++)
40 putchar (TOUPPER(*str));
43 static VEC (rtx, heap) *const_attrs, *reservations;
46 static void
47 gen_attr (rtx attr)
49 const char *p, *tag;
50 int is_const = GET_CODE (XEXP (attr, 2)) == CONST;
52 if (is_const)
53 VEC_safe_push (rtx, heap, const_attrs, attr);
55 printf ("#define HAVE_ATTR_%s\n", XSTR (attr, 0));
57 /* If numeric attribute, don't need to write an enum. */
58 if (GET_CODE (attr) == DEFINE_ENUM_ATTR)
59 printf ("extern enum %s get_attr_%s (%s);\n\n",
60 XSTR (attr, 1), XSTR (attr, 0), (is_const ? "void" : "rtx"));
61 else
63 p = XSTR (attr, 1);
64 if (*p == '\0')
65 printf ("extern int get_attr_%s (%s);\n", XSTR (attr, 0),
66 (is_const ? "void" : "rtx"));
67 else
69 printf ("enum attr_%s {", XSTR (attr, 0));
71 while ((tag = scan_comma_elt (&p)) != 0)
73 write_upcase (XSTR (attr, 0));
74 putchar ('_');
75 while (tag != p)
76 putchar (TOUPPER (*tag++));
77 if (*p == ',')
78 fputs (", ", stdout);
80 fputs ("};\n", stdout);
82 printf ("extern enum attr_%s get_attr_%s (%s);\n\n",
83 XSTR (attr, 0), XSTR (attr, 0), (is_const ? "void" : "rtx"));
87 /* If `length' attribute, write additional function definitions and define
88 variables used by `insn_current_length'. */
89 if (! strcmp (XSTR (attr, 0), "length"))
91 puts ("\
92 extern void shorten_branches (rtx);\n\
93 extern int insn_default_length (rtx);\n\
94 extern int insn_min_length (rtx);\n\
95 extern int insn_variable_length_p (rtx);\n\
96 extern int insn_current_length (rtx);\n\n\
97 #include \"insn-addr.h\"\n");
101 /* Check that attribute NAME is used in define_insn_reservation condition
102 EXP. Return true if it is. */
103 static bool
104 check_tune_attr (const char *name, rtx exp)
106 switch (GET_CODE (exp))
108 case AND:
109 if (check_tune_attr (name, XEXP (exp, 0)))
110 return true;
111 return check_tune_attr (name, XEXP (exp, 1));
113 case IOR:
114 return (check_tune_attr (name, XEXP (exp, 0))
115 && check_tune_attr (name, XEXP (exp, 1)));
117 case EQ_ATTR:
118 return strcmp (XSTR (exp, 0), name) == 0;
120 default:
121 return false;
125 /* Try to find a const attribute (usually cpu or tune) that is used
126 in all define_insn_reservation conditions. */
127 static bool
128 find_tune_attr (rtx exp)
130 unsigned int i;
131 rtx attr;
133 switch (GET_CODE (exp))
135 case AND:
136 case IOR:
137 if (find_tune_attr (XEXP (exp, 0)))
138 return true;
139 return find_tune_attr (XEXP (exp, 1));
141 case EQ_ATTR:
142 if (strcmp (XSTR (exp, 0), "alternative") == 0)
143 return false;
145 FOR_EACH_VEC_ELT (rtx, const_attrs, i, attr)
146 if (strcmp (XSTR (attr, 0), XSTR (exp, 0)) == 0)
148 unsigned int j;
149 rtx resv;
151 FOR_EACH_VEC_ELT (rtx, reservations, j, resv)
152 if (! check_tune_attr (XSTR (attr, 0), XEXP (resv, 2)))
153 return false;
154 return true;
156 return false;
158 default:
159 return false;
164 main (int argc, char **argv)
166 rtx desc;
167 int have_delay = 0;
168 int have_annul_true = 0;
169 int have_annul_false = 0;
170 int num_insn_reservations = 0;
171 int i;
173 progname = "genattr";
175 if (!init_rtx_reader_args (argc, argv))
176 return (FATAL_EXIT_CODE);
178 puts ("/* Generated automatically by the program `genattr'");
179 puts (" from the machine description file `md'. */\n");
180 puts ("#ifndef GCC_INSN_ATTR_H");
181 puts ("#define GCC_INSN_ATTR_H\n");
183 /* For compatibility, define the attribute `alternative', which is just
184 a reference to the variable `which_alternative'. */
186 puts ("#define HAVE_ATTR_alternative");
187 puts ("#define get_attr_alternative(insn) which_alternative");
189 /* Read the machine description. */
191 while (1)
193 int line_no, insn_code_number;
195 desc = read_md_rtx (&line_no, &insn_code_number);
196 if (desc == NULL)
197 break;
199 if (GET_CODE (desc) == DEFINE_ATTR
200 || GET_CODE (desc) == DEFINE_ENUM_ATTR)
201 gen_attr (desc);
203 else if (GET_CODE (desc) == DEFINE_DELAY)
205 if (! have_delay)
207 printf ("#define DELAY_SLOTS\n");
208 printf ("extern int num_delay_slots (rtx);\n");
209 printf ("extern int eligible_for_delay (rtx, int, rtx, int);\n\n");
210 printf ("extern int const_num_delay_slots (rtx);\n\n");
211 have_delay = 1;
214 for (i = 0; i < XVECLEN (desc, 1); i += 3)
216 if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
218 printf ("#define ANNUL_IFTRUE_SLOTS\n");
219 printf ("extern int eligible_for_annul_true (rtx, int, rtx, int);\n");
220 have_annul_true = 1;
223 if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
225 printf ("#define ANNUL_IFFALSE_SLOTS\n");
226 printf ("extern int eligible_for_annul_false (rtx, int, rtx, int);\n");
227 have_annul_false = 1;
232 else if (GET_CODE (desc) == DEFINE_INSN_RESERVATION)
234 num_insn_reservations++;
235 VEC_safe_push (rtx, heap, reservations, desc);
239 if (num_insn_reservations > 0)
241 bool has_tune_attr
242 = find_tune_attr (XEXP (VEC_index (rtx, reservations, 0), 2));
243 /* Output interface for pipeline hazards recognition based on
244 DFA (deterministic finite state automata. */
245 printf ("\n#define INSN_SCHEDULING\n");
246 printf ("\n/* DFA based pipeline interface. */");
247 printf ("\n#ifndef AUTOMATON_ALTS\n");
248 printf ("#define AUTOMATON_ALTS 0\n");
249 printf ("#endif\n\n");
250 printf ("\n#ifndef AUTOMATON_STATE_ALTS\n");
251 printf ("#define AUTOMATON_STATE_ALTS 0\n");
252 printf ("#endif\n\n");
253 printf ("#ifndef CPU_UNITS_QUERY\n");
254 printf ("#define CPU_UNITS_QUERY 0\n");
255 printf ("#endif\n\n");
256 /* Interface itself: */
257 if (has_tune_attr)
259 printf ("/* Initialize fn pointers for internal_dfa_insn_code\n");
260 printf (" and insn_default_latency. */\n");
261 printf ("extern void init_sched_attrs (void);\n\n");
262 printf ("/* Internal insn code number used by automata. */\n");
263 printf ("extern int (*internal_dfa_insn_code) (rtx);\n\n");
264 printf ("/* Insn latency time defined in define_insn_reservation. */\n");
265 printf ("extern int (*insn_default_latency) (rtx);\n\n");
267 else
269 printf ("#define init_sched_attrs() do { } while (0)\n\n");
270 printf ("/* Internal insn code number used by automata. */\n");
271 printf ("extern int internal_dfa_insn_code (rtx);\n\n");
272 printf ("/* Insn latency time defined in define_insn_reservation. */\n");
273 printf ("extern int insn_default_latency (rtx);\n\n");
275 printf ("/* Return nonzero if there is a bypass for given insn\n");
276 printf (" which is a data producer. */\n");
277 printf ("extern int bypass_p (rtx);\n\n");
278 printf ("/* Insn latency time on data consumed by the 2nd insn.\n");
279 printf (" Use the function if bypass_p returns nonzero for\n");
280 printf (" the 1st insn. */\n");
281 printf ("extern int insn_latency (rtx, rtx);\n\n");
282 printf ("/* Maximal insn latency time possible of all bypasses for this insn.\n");
283 printf (" Use the function if bypass_p returns nonzero for\n");
284 printf (" the 1st insn. */\n");
285 printf ("extern int maximal_insn_latency (rtx);\n\n");
286 printf ("\n#if AUTOMATON_ALTS\n");
287 printf ("/* The following function returns number of alternative\n");
288 printf (" reservations of given insn. It may be used for better\n");
289 printf (" insns scheduling heuristics. */\n");
290 printf ("extern int insn_alts (rtx);\n\n");
291 printf ("#endif\n\n");
292 printf ("/* Maximal possible number of insns waiting results being\n");
293 printf (" produced by insns whose execution is not finished. */\n");
294 printf ("extern const int max_insn_queue_index;\n\n");
295 printf ("/* Pointer to data describing current state of DFA. */\n");
296 printf ("typedef void *state_t;\n\n");
297 printf ("/* Size of the data in bytes. */\n");
298 printf ("extern int state_size (void);\n\n");
299 printf ("/* Initiate given DFA state, i.e. Set up the state\n");
300 printf (" as all functional units were not reserved. */\n");
301 printf ("extern void state_reset (state_t);\n");
302 printf ("/* The following function returns negative value if given\n");
303 printf (" insn can be issued in processor state described by given\n");
304 printf (" DFA state. In this case, the DFA state is changed to\n");
305 printf (" reflect the current and future reservations by given\n");
306 printf (" insn. Otherwise the function returns minimal time\n");
307 printf (" delay to issue the insn. This delay may be zero\n");
308 printf (" for superscalar or VLIW processors. If the second\n");
309 printf (" parameter is NULL the function changes given DFA state\n");
310 printf (" as new processor cycle started. */\n");
311 printf ("extern int state_transition (state_t, rtx);\n");
312 printf ("\n#if AUTOMATON_STATE_ALTS\n");
313 printf ("/* The following function returns number of possible\n");
314 printf (" alternative reservations of given insn in given\n");
315 printf (" DFA state. It may be used for better insns scheduling\n");
316 printf (" heuristics. By default the function is defined if\n");
317 printf (" macro AUTOMATON_STATE_ALTS is defined because its\n");
318 printf (" implementation may require much memory. */\n");
319 printf ("extern int state_alts (state_t, rtx);\n");
320 printf ("#endif\n\n");
321 printf ("extern int min_issue_delay (state_t, rtx);\n");
322 printf ("/* The following function returns nonzero if no one insn\n");
323 printf (" can be issued in current DFA state. */\n");
324 printf ("extern int state_dead_lock_p (state_t);\n");
325 printf ("/* The function returns minimal delay of issue of the 2nd\n");
326 printf (" insn after issuing the 1st insn in given DFA state.\n");
327 printf (" The 1st insn should be issued in given state (i.e.\n");
328 printf (" state_transition should return negative value for\n");
329 printf (" the insn and the state). Data dependencies between\n");
330 printf (" the insns are ignored by the function. */\n");
331 printf
332 ("extern int min_insn_conflict_delay (state_t, rtx, rtx);\n");
333 printf ("/* The following function outputs reservations for given\n");
334 printf (" insn as they are described in the corresponding\n");
335 printf (" define_insn_reservation. */\n");
336 printf ("extern void print_reservation (FILE *, rtx);\n");
337 printf ("\n#if CPU_UNITS_QUERY\n");
338 printf ("/* The following function returns code of functional unit\n");
339 printf (" with given name (see define_cpu_unit). */\n");
340 printf ("extern int get_cpu_unit_code (const char *);\n");
341 printf ("/* The following function returns nonzero if functional\n");
342 printf (" unit with given code is currently reserved in given\n");
343 printf (" DFA state. */\n");
344 printf ("extern int cpu_unit_reservation_p (state_t, int);\n");
345 printf ("#endif\n\n");
346 printf ("/* The following function returns true if insn\n");
347 printf (" has a dfa reservation. */\n");
348 printf ("extern bool insn_has_dfa_reservation_p (rtx);\n\n");
349 printf ("/* Clean insn code cache. It should be called if there\n");
350 printf (" is a chance that condition value in a\n");
351 printf (" define_insn_reservation will be changed after\n");
352 printf (" last call of dfa_start. */\n");
353 printf ("extern void dfa_clean_insn_cache (void);\n\n");
354 printf ("extern void dfa_clear_single_insn_cache (rtx);\n\n");
355 printf ("/* Initiate and finish work with DFA. They should be\n");
356 printf (" called as the first and the last interface\n");
357 printf (" functions. */\n");
358 printf ("extern void dfa_start (void);\n");
359 printf ("extern void dfa_finish (void);\n");
361 else
363 /* Otherwise we do no scheduling, but we need these typedefs
364 in order to avoid uglifying other code with more ifdefs. */
365 printf ("typedef void *state_t;\n\n");
368 /* Output flag masks for use by reorg.
370 Flags are used to hold branch direction and prediction information
371 for use by eligible_for_... */
372 printf("\n#define ATTR_FLAG_forward\t0x1\n");
373 printf("#define ATTR_FLAG_backward\t0x2\n");
374 printf("#define ATTR_FLAG_likely\t0x4\n");
375 printf("#define ATTR_FLAG_very_likely\t0x8\n");
376 printf("#define ATTR_FLAG_unlikely\t0x10\n");
377 printf("#define ATTR_FLAG_very_unlikely\t0x20\n");
379 puts("\n#endif /* GCC_INSN_ATTR_H */");
381 if (ferror (stdout) || fflush (stdout) || fclose (stdout))
382 return FATAL_EXIT_CODE;
384 return SUCCESS_EXIT_CODE;