2015-05-05 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / gcc / genattr.c
blob97ed613e421a1297d2df9418522dea587a6c2642
1 /* Generate attribute information (insn-attr.h) from machine description.
2 Copyright (C) 1991-2015 Free Software Foundation, Inc.
3 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #include "bconfig.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "errors.h"
28 #include "read-md.h"
29 #include "gensupport.h"
32 static void gen_attr (rtx);
34 static vec<rtx> const_attrs, reservations;
37 static void
38 gen_attr (rtx attr)
40 const char *p;
41 int is_const = GET_CODE (XEXP (attr, 2)) == CONST;
43 if (is_const)
44 const_attrs.safe_push (attr);
46 printf ("#define HAVE_ATTR_%s 1\n", XSTR (attr, 0));
48 /* If numeric attribute, don't need to write an enum. */
49 if (GET_CODE (attr) == DEFINE_ENUM_ATTR)
50 printf ("extern enum %s get_attr_%s (%s);\n\n",
51 XSTR (attr, 1), XSTR (attr, 0),
52 (is_const ? "void" : "rtx_insn *"));
53 else
55 p = XSTR (attr, 1);
56 if (*p == '\0')
57 printf ("extern int get_attr_%s (%s);\n", XSTR (attr, 0),
58 (is_const ? "void" : "rtx_insn *"));
59 else
60 printf ("extern enum attr_%s get_attr_%s (%s);\n\n",
61 XSTR (attr, 0), XSTR (attr, 0),
62 (is_const ? "void" : "rtx_insn *"));
65 /* If `length' attribute, write additional function definitions and define
66 variables used by `insn_current_length'. */
67 if (! strcmp (XSTR (attr, 0), "length"))
69 puts ("\
70 extern void shorten_branches (rtx_insn *);\n\
71 extern int insn_default_length (rtx_insn *);\n\
72 extern int insn_min_length (rtx_insn *);\n\
73 extern int insn_variable_length_p (rtx_insn *);\n\
74 extern int insn_current_length (rtx_insn *);\n\n\
75 #include \"insn-addr.h\"\n");
79 /* Check that attribute NAME is used in define_insn_reservation condition
80 EXP. Return true if it is. */
81 static bool
82 check_tune_attr (const char *name, rtx exp)
84 switch (GET_CODE (exp))
86 case AND:
87 if (check_tune_attr (name, XEXP (exp, 0)))
88 return true;
89 return check_tune_attr (name, XEXP (exp, 1));
91 case IOR:
92 return (check_tune_attr (name, XEXP (exp, 0))
93 && check_tune_attr (name, XEXP (exp, 1)));
95 case EQ_ATTR:
96 return strcmp (XSTR (exp, 0), name) == 0;
98 default:
99 return false;
103 /* Try to find a const attribute (usually cpu or tune) that is used
104 in all define_insn_reservation conditions. */
105 static bool
106 find_tune_attr (rtx exp)
108 unsigned int i;
109 rtx attr;
111 switch (GET_CODE (exp))
113 case AND:
114 case IOR:
115 if (find_tune_attr (XEXP (exp, 0)))
116 return true;
117 return find_tune_attr (XEXP (exp, 1));
119 case EQ_ATTR:
120 if (strcmp (XSTR (exp, 0), "alternative") == 0)
121 return false;
123 FOR_EACH_VEC_ELT (const_attrs, i, attr)
124 if (strcmp (XSTR (attr, 0), XSTR (exp, 0)) == 0)
126 unsigned int j;
127 rtx resv;
129 FOR_EACH_VEC_ELT (reservations, j, resv)
130 if (! check_tune_attr (XSTR (attr, 0), XEXP (resv, 2)))
131 return false;
132 return true;
134 return false;
136 default:
137 return false;
142 main (int argc, char **argv)
144 rtx desc;
145 int have_delay = 0;
146 int have_annul_true = 0;
147 int have_annul_false = 0;
148 int num_insn_reservations = 0;
149 int i;
151 progname = "genattr";
153 if (!init_rtx_reader_args (argc, argv))
154 return (FATAL_EXIT_CODE);
156 puts ("/* Generated automatically by the program `genattr'");
157 puts (" from the machine description file `md'. */\n");
158 puts ("#ifndef GCC_INSN_ATTR_H");
159 puts ("#define GCC_INSN_ATTR_H\n");
161 puts ("#include \"insn-attr-common.h\"\n");
163 /* Read the machine description. */
165 while (1)
167 int line_no, insn_code_number;
169 desc = read_md_rtx (&line_no, &insn_code_number);
170 if (desc == NULL)
171 break;
173 if (GET_CODE (desc) == DEFINE_ATTR
174 || GET_CODE (desc) == DEFINE_ENUM_ATTR)
175 gen_attr (desc);
177 else if (GET_CODE (desc) == DEFINE_DELAY)
179 if (! have_delay)
181 printf ("extern int num_delay_slots (rtx_insn *);\n");
182 printf ("extern int eligible_for_delay (rtx_insn *, int, rtx_insn *, int);\n\n");
183 printf ("extern int const_num_delay_slots (rtx_insn *);\n\n");
184 have_delay = 1;
187 for (i = 0; i < XVECLEN (desc, 1); i += 3)
189 if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
191 printf ("#define ANNUL_IFTRUE_SLOTS\n");
192 printf ("extern int eligible_for_annul_true (rtx_insn *, int, rtx_insn *, int);\n");
193 have_annul_true = 1;
196 if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
198 printf ("#define ANNUL_IFFALSE_SLOTS\n");
199 printf ("extern int eligible_for_annul_false (rtx_insn *, int, rtx_insn *, int);\n");
200 have_annul_false = 1;
205 else if (GET_CODE (desc) == DEFINE_INSN_RESERVATION)
207 num_insn_reservations++;
208 reservations.safe_push (desc);
212 if (num_insn_reservations > 0)
214 bool has_tune_attr
215 = find_tune_attr (XEXP (reservations[0], 2));
216 /* Output interface for pipeline hazards recognition based on
217 DFA (deterministic finite state automata. */
218 printf ("\n/* DFA based pipeline interface. */");
219 printf ("\n#ifndef AUTOMATON_ALTS\n");
220 printf ("#define AUTOMATON_ALTS 0\n");
221 printf ("#endif\n\n");
222 printf ("\n#ifndef AUTOMATON_STATE_ALTS\n");
223 printf ("#define AUTOMATON_STATE_ALTS 0\n");
224 printf ("#endif\n\n");
225 printf ("#ifndef CPU_UNITS_QUERY\n");
226 printf ("#define CPU_UNITS_QUERY 0\n");
227 printf ("#endif\n\n");
228 /* Interface itself: */
229 if (has_tune_attr)
231 printf ("/* Initialize fn pointers for internal_dfa_insn_code\n");
232 printf (" and insn_default_latency. */\n");
233 printf ("extern void init_sched_attrs (void);\n\n");
234 printf ("/* Internal insn code number used by automata. */\n");
235 printf ("extern int (*internal_dfa_insn_code) (rtx_insn *);\n\n");
236 printf ("/* Insn latency time defined in define_insn_reservation. */\n");
237 printf ("extern int (*insn_default_latency) (rtx_insn *);\n\n");
239 else
241 printf ("#define init_sched_attrs() do { } while (0)\n\n");
242 printf ("/* Internal insn code number used by automata. */\n");
243 printf ("extern int internal_dfa_insn_code (rtx_insn *);\n\n");
244 printf ("/* Insn latency time defined in define_insn_reservation. */\n");
245 printf ("extern int insn_default_latency (rtx_insn *);\n\n");
247 printf ("/* Return nonzero if there is a bypass for given insn\n");
248 printf (" which is a data producer. */\n");
249 printf ("extern int bypass_p (rtx_insn *);\n\n");
250 printf ("/* Insn latency time on data consumed by the 2nd insn.\n");
251 printf (" Use the function if bypass_p returns nonzero for\n");
252 printf (" the 1st insn. */\n");
253 printf ("extern int insn_latency (rtx, rtx);\n\n");
254 printf ("/* Maximal insn latency time possible of all bypasses for this insn.\n");
255 printf (" Use the function if bypass_p returns nonzero for\n");
256 printf (" the 1st insn. */\n");
257 printf ("extern int maximal_insn_latency (rtx);\n\n");
258 printf ("\n#if AUTOMATON_ALTS\n");
259 printf ("/* The following function returns number of alternative\n");
260 printf (" reservations of given insn. It may be used for better\n");
261 printf (" insns scheduling heuristics. */\n");
262 printf ("extern int insn_alts (rtx);\n\n");
263 printf ("#endif\n\n");
264 printf ("/* Maximal possible number of insns waiting results being\n");
265 printf (" produced by insns whose execution is not finished. */\n");
266 printf ("extern const int max_insn_queue_index;\n\n");
267 printf ("/* Pointer to data describing current state of DFA. */\n");
268 printf ("typedef void *state_t;\n\n");
269 printf ("/* Size of the data in bytes. */\n");
270 printf ("extern int state_size (void);\n\n");
271 printf ("/* Initiate given DFA state, i.e. Set up the state\n");
272 printf (" as all functional units were not reserved. */\n");
273 printf ("extern void state_reset (state_t);\n");
274 printf ("/* The following function returns negative value if given\n");
275 printf (" insn can be issued in processor state described by given\n");
276 printf (" DFA state. In this case, the DFA state is changed to\n");
277 printf (" reflect the current and future reservations by given\n");
278 printf (" insn. Otherwise the function returns minimal time\n");
279 printf (" delay to issue the insn. This delay may be zero\n");
280 printf (" for superscalar or VLIW processors. If the second\n");
281 printf (" parameter is NULL the function changes given DFA state\n");
282 printf (" as new processor cycle started. */\n");
283 printf ("extern int state_transition (state_t, rtx);\n");
284 printf ("\n#if AUTOMATON_STATE_ALTS\n");
285 printf ("/* The following function returns number of possible\n");
286 printf (" alternative reservations of given insn in given\n");
287 printf (" DFA state. It may be used for better insns scheduling\n");
288 printf (" heuristics. By default the function is defined if\n");
289 printf (" macro AUTOMATON_STATE_ALTS is defined because its\n");
290 printf (" implementation may require much memory. */\n");
291 printf ("extern int state_alts (state_t, rtx);\n");
292 printf ("#endif\n\n");
293 printf ("extern int min_issue_delay (state_t, rtx_insn *);\n");
294 printf ("/* The following function returns nonzero if no one insn\n");
295 printf (" can be issued in current DFA state. */\n");
296 printf ("extern int state_dead_lock_p (state_t);\n");
297 printf ("/* The function returns minimal delay of issue of the 2nd\n");
298 printf (" insn after issuing the 1st insn in given DFA state.\n");
299 printf (" The 1st insn should be issued in given state (i.e.\n");
300 printf (" state_transition should return negative value for\n");
301 printf (" the insn and the state). Data dependencies between\n");
302 printf (" the insns are ignored by the function. */\n");
303 printf
304 ("extern int min_insn_conflict_delay (state_t, rtx, rtx);\n");
305 printf ("/* The following function outputs reservations for given\n");
306 printf (" insn as they are described in the corresponding\n");
307 printf (" define_insn_reservation. */\n");
308 printf ("extern void print_reservation (FILE *, rtx_insn *);\n");
309 printf ("\n#if CPU_UNITS_QUERY\n");
310 printf ("/* The following function returns code of functional unit\n");
311 printf (" with given name (see define_cpu_unit). */\n");
312 printf ("extern int get_cpu_unit_code (const char *);\n");
313 printf ("/* The following function returns nonzero if functional\n");
314 printf (" unit with given code is currently reserved in given\n");
315 printf (" DFA state. */\n");
316 printf ("extern int cpu_unit_reservation_p (state_t, int);\n");
317 printf ("#endif\n\n");
318 printf ("/* The following function returns true if insn\n");
319 printf (" has a dfa reservation. */\n");
320 printf ("extern bool insn_has_dfa_reservation_p (rtx_insn *);\n\n");
321 printf ("/* Clean insn code cache. It should be called if there\n");
322 printf (" is a chance that condition value in a\n");
323 printf (" define_insn_reservation will be changed after\n");
324 printf (" last call of dfa_start. */\n");
325 printf ("extern void dfa_clean_insn_cache (void);\n\n");
326 printf ("extern void dfa_clear_single_insn_cache (rtx_insn *);\n\n");
327 printf ("/* Initiate and finish work with DFA. They should be\n");
328 printf (" called as the first and the last interface\n");
329 printf (" functions. */\n");
330 printf ("extern void dfa_start (void);\n");
331 printf ("extern void dfa_finish (void);\n");
333 else
335 /* Otherwise we do no scheduling, but we need these typedefs
336 in order to avoid uglifying other code with more ifdefs. */
337 printf ("typedef void *state_t;\n\n");
340 /* Special-purpose attributes should be tested with if, not #ifdef. */
341 const char * const special_attrs[] = { "length", "enabled",
342 "preferred_for_size",
343 "preferred_for_speed", 0 };
344 for (const char * const *p = special_attrs; *p; p++)
346 printf ("#ifndef HAVE_ATTR_%s\n"
347 "#define HAVE_ATTR_%s 0\n"
348 "#endif\n", *p, *p);
350 /* We make an exception here to provide stub definitions for
351 insn_*_length* / get_attr_enabled functions. */
352 puts ("#if !HAVE_ATTR_length\n"
353 "extern int hook_int_rtx_insn_unreachable (rtx_insn *);\n"
354 "#define insn_default_length hook_int_rtx_insn_unreachable\n"
355 "#define insn_min_length hook_int_rtx_insn_unreachable\n"
356 "#define insn_variable_length_p hook_int_rtx_insn_unreachable\n"
357 "#define insn_current_length hook_int_rtx_insn_unreachable\n"
358 "#include \"insn-addr.h\"\n"
359 "#endif\n"
360 "extern int hook_int_rtx_1 (rtx);\n"
361 "#if !HAVE_ATTR_enabled\n"
362 "#define get_attr_enabled hook_int_rtx_1\n"
363 "#endif\n"
364 "#if !HAVE_ATTR_preferred_for_size\n"
365 "#define get_attr_preferred_for_size hook_int_rtx_1\n"
366 "#endif\n"
367 "#if !HAVE_ATTR_preferred_for_speed\n"
368 "#define get_attr_preferred_for_speed hook_int_rtx_1\n"
369 "#endif\n");
371 /* Output flag masks for use by reorg.
373 Flags are used to hold branch direction for use by eligible_for_... */
374 printf ("\n#define ATTR_FLAG_forward\t0x1\n");
375 printf ("#define ATTR_FLAG_backward\t0x2\n");
377 puts ("\n#endif /* GCC_INSN_ATTR_H */");
379 if (ferror (stdout) || fflush (stdout) || fclose (stdout))
380 return FATAL_EXIT_CODE;
382 return SUCCESS_EXIT_CODE;