gcc/testsuite/
[official-gcc.git] / gcc / genattr.c
blob44550c0b63756e7077c558fa12ab9019b7bcb5aa
1 /* Generate attribute information (insn-attr.h) from machine description.
2 Copyright (C) 1991-2014 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), (is_const ? "void" : "rtx"));
52 else
54 p = XSTR (attr, 1);
55 if (*p == '\0')
56 printf ("extern int get_attr_%s (%s);\n", XSTR (attr, 0),
57 (is_const ? "void" : "rtx"));
58 else
59 printf ("extern enum attr_%s get_attr_%s (%s);\n\n",
60 XSTR (attr, 0), XSTR (attr, 0), (is_const ? "void" : "rtx"));
63 /* If `length' attribute, write additional function definitions and define
64 variables used by `insn_current_length'. */
65 if (! strcmp (XSTR (attr, 0), "length"))
67 puts ("\
68 extern void shorten_branches (rtx);\n\
69 extern int insn_default_length (rtx);\n\
70 extern int insn_min_length (rtx);\n\
71 extern int insn_variable_length_p (rtx);\n\
72 extern int insn_current_length (rtx);\n\n\
73 #include \"insn-addr.h\"\n");
77 /* Check that attribute NAME is used in define_insn_reservation condition
78 EXP. Return true if it is. */
79 static bool
80 check_tune_attr (const char *name, rtx exp)
82 switch (GET_CODE (exp))
84 case AND:
85 if (check_tune_attr (name, XEXP (exp, 0)))
86 return true;
87 return check_tune_attr (name, XEXP (exp, 1));
89 case IOR:
90 return (check_tune_attr (name, XEXP (exp, 0))
91 && check_tune_attr (name, XEXP (exp, 1)));
93 case EQ_ATTR:
94 return strcmp (XSTR (exp, 0), name) == 0;
96 default:
97 return false;
101 /* Try to find a const attribute (usually cpu or tune) that is used
102 in all define_insn_reservation conditions. */
103 static bool
104 find_tune_attr (rtx exp)
106 unsigned int i;
107 rtx attr;
109 switch (GET_CODE (exp))
111 case AND:
112 case IOR:
113 if (find_tune_attr (XEXP (exp, 0)))
114 return true;
115 return find_tune_attr (XEXP (exp, 1));
117 case EQ_ATTR:
118 if (strcmp (XSTR (exp, 0), "alternative") == 0)
119 return false;
121 FOR_EACH_VEC_ELT (const_attrs, i, attr)
122 if (strcmp (XSTR (attr, 0), XSTR (exp, 0)) == 0)
124 unsigned int j;
125 rtx resv;
127 FOR_EACH_VEC_ELT (reservations, j, resv)
128 if (! check_tune_attr (XSTR (attr, 0), XEXP (resv, 2)))
129 return false;
130 return true;
132 return false;
134 default:
135 return false;
140 main (int argc, char **argv)
142 rtx desc;
143 int have_delay = 0;
144 int have_annul_true = 0;
145 int have_annul_false = 0;
146 int num_insn_reservations = 0;
147 int i;
149 progname = "genattr";
151 if (!init_rtx_reader_args (argc, argv))
152 return (FATAL_EXIT_CODE);
154 puts ("/* Generated automatically by the program `genattr'");
155 puts (" from the machine description file `md'. */\n");
156 puts ("#ifndef GCC_INSN_ATTR_H");
157 puts ("#define GCC_INSN_ATTR_H\n");
159 puts ("#include \"insn-attr-common.h\"\n");
161 /* Read the machine description. */
163 while (1)
165 int line_no, insn_code_number;
167 desc = read_md_rtx (&line_no, &insn_code_number);
168 if (desc == NULL)
169 break;
171 if (GET_CODE (desc) == DEFINE_ATTR
172 || GET_CODE (desc) == DEFINE_ENUM_ATTR)
173 gen_attr (desc);
175 else if (GET_CODE (desc) == DEFINE_DELAY)
177 if (! have_delay)
179 printf ("extern int num_delay_slots (rtx);\n");
180 printf ("extern int eligible_for_delay (rtx, int, rtx, int);\n\n");
181 printf ("extern int const_num_delay_slots (rtx);\n\n");
182 have_delay = 1;
185 for (i = 0; i < XVECLEN (desc, 1); i += 3)
187 if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
189 printf ("#define ANNUL_IFTRUE_SLOTS\n");
190 printf ("extern int eligible_for_annul_true (rtx, int, rtx, int);\n");
191 have_annul_true = 1;
194 if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
196 printf ("#define ANNUL_IFFALSE_SLOTS\n");
197 printf ("extern int eligible_for_annul_false (rtx, int, rtx, int);\n");
198 have_annul_false = 1;
203 else if (GET_CODE (desc) == DEFINE_INSN_RESERVATION)
205 num_insn_reservations++;
206 reservations.safe_push (desc);
210 if (num_insn_reservations > 0)
212 bool has_tune_attr
213 = find_tune_attr (XEXP (reservations[0], 2));
214 /* Output interface for pipeline hazards recognition based on
215 DFA (deterministic finite state automata. */
216 printf ("\n/* DFA based pipeline interface. */");
217 printf ("\n#ifndef AUTOMATON_ALTS\n");
218 printf ("#define AUTOMATON_ALTS 0\n");
219 printf ("#endif\n\n");
220 printf ("\n#ifndef AUTOMATON_STATE_ALTS\n");
221 printf ("#define AUTOMATON_STATE_ALTS 0\n");
222 printf ("#endif\n\n");
223 printf ("#ifndef CPU_UNITS_QUERY\n");
224 printf ("#define CPU_UNITS_QUERY 0\n");
225 printf ("#endif\n\n");
226 /* Interface itself: */
227 if (has_tune_attr)
229 printf ("/* Initialize fn pointers for internal_dfa_insn_code\n");
230 printf (" and insn_default_latency. */\n");
231 printf ("extern void init_sched_attrs (void);\n\n");
232 printf ("/* Internal insn code number used by automata. */\n");
233 printf ("extern int (*internal_dfa_insn_code) (rtx);\n\n");
234 printf ("/* Insn latency time defined in define_insn_reservation. */\n");
235 printf ("extern int (*insn_default_latency) (rtx);\n\n");
237 else
239 printf ("#define init_sched_attrs() do { } while (0)\n\n");
240 printf ("/* Internal insn code number used by automata. */\n");
241 printf ("extern int internal_dfa_insn_code (rtx);\n\n");
242 printf ("/* Insn latency time defined in define_insn_reservation. */\n");
243 printf ("extern int insn_default_latency (rtx);\n\n");
245 printf ("/* Return nonzero if there is a bypass for given insn\n");
246 printf (" which is a data producer. */\n");
247 printf ("extern int bypass_p (rtx);\n\n");
248 printf ("/* Insn latency time on data consumed by the 2nd insn.\n");
249 printf (" Use the function if bypass_p returns nonzero for\n");
250 printf (" the 1st insn. */\n");
251 printf ("extern int insn_latency (rtx, rtx);\n\n");
252 printf ("/* Maximal insn latency time possible of all bypasses for this insn.\n");
253 printf (" Use the function if bypass_p returns nonzero for\n");
254 printf (" the 1st insn. */\n");
255 printf ("extern int maximal_insn_latency (rtx);\n\n");
256 printf ("\n#if AUTOMATON_ALTS\n");
257 printf ("/* The following function returns number of alternative\n");
258 printf (" reservations of given insn. It may be used for better\n");
259 printf (" insns scheduling heuristics. */\n");
260 printf ("extern int insn_alts (rtx);\n\n");
261 printf ("#endif\n\n");
262 printf ("/* Maximal possible number of insns waiting results being\n");
263 printf (" produced by insns whose execution is not finished. */\n");
264 printf ("extern const int max_insn_queue_index;\n\n");
265 printf ("/* Pointer to data describing current state of DFA. */\n");
266 printf ("typedef void *state_t;\n\n");
267 printf ("/* Size of the data in bytes. */\n");
268 printf ("extern int state_size (void);\n\n");
269 printf ("/* Initiate given DFA state, i.e. Set up the state\n");
270 printf (" as all functional units were not reserved. */\n");
271 printf ("extern void state_reset (state_t);\n");
272 printf ("/* The following function returns negative value if given\n");
273 printf (" insn can be issued in processor state described by given\n");
274 printf (" DFA state. In this case, the DFA state is changed to\n");
275 printf (" reflect the current and future reservations by given\n");
276 printf (" insn. Otherwise the function returns minimal time\n");
277 printf (" delay to issue the insn. This delay may be zero\n");
278 printf (" for superscalar or VLIW processors. If the second\n");
279 printf (" parameter is NULL the function changes given DFA state\n");
280 printf (" as new processor cycle started. */\n");
281 printf ("extern int state_transition (state_t, rtx);\n");
282 printf ("\n#if AUTOMATON_STATE_ALTS\n");
283 printf ("/* The following function returns number of possible\n");
284 printf (" alternative reservations of given insn in given\n");
285 printf (" DFA state. It may be used for better insns scheduling\n");
286 printf (" heuristics. By default the function is defined if\n");
287 printf (" macro AUTOMATON_STATE_ALTS is defined because its\n");
288 printf (" implementation may require much memory. */\n");
289 printf ("extern int state_alts (state_t, rtx);\n");
290 printf ("#endif\n\n");
291 printf ("extern int min_issue_delay (state_t, rtx);\n");
292 printf ("/* The following function returns nonzero if no one insn\n");
293 printf (" can be issued in current DFA state. */\n");
294 printf ("extern int state_dead_lock_p (state_t);\n");
295 printf ("/* The function returns minimal delay of issue of the 2nd\n");
296 printf (" insn after issuing the 1st insn in given DFA state.\n");
297 printf (" The 1st insn should be issued in given state (i.e.\n");
298 printf (" state_transition should return negative value for\n");
299 printf (" the insn and the state). Data dependencies between\n");
300 printf (" the insns are ignored by the function. */\n");
301 printf
302 ("extern int min_insn_conflict_delay (state_t, rtx, rtx);\n");
303 printf ("/* The following function outputs reservations for given\n");
304 printf (" insn as they are described in the corresponding\n");
305 printf (" define_insn_reservation. */\n");
306 printf ("extern void print_reservation (FILE *, rtx);\n");
307 printf ("\n#if CPU_UNITS_QUERY\n");
308 printf ("/* The following function returns code of functional unit\n");
309 printf (" with given name (see define_cpu_unit). */\n");
310 printf ("extern int get_cpu_unit_code (const char *);\n");
311 printf ("/* The following function returns nonzero if functional\n");
312 printf (" unit with given code is currently reserved in given\n");
313 printf (" DFA state. */\n");
314 printf ("extern int cpu_unit_reservation_p (state_t, int);\n");
315 printf ("#endif\n\n");
316 printf ("/* The following function returns true if insn\n");
317 printf (" has a dfa reservation. */\n");
318 printf ("extern bool insn_has_dfa_reservation_p (rtx);\n\n");
319 printf ("/* Clean insn code cache. It should be called if there\n");
320 printf (" is a chance that condition value in a\n");
321 printf (" define_insn_reservation will be changed after\n");
322 printf (" last call of dfa_start. */\n");
323 printf ("extern void dfa_clean_insn_cache (void);\n\n");
324 printf ("extern void dfa_clear_single_insn_cache (rtx);\n\n");
325 printf ("/* Initiate and finish work with DFA. They should be\n");
326 printf (" called as the first and the last interface\n");
327 printf (" functions. */\n");
328 printf ("extern void dfa_start (void);\n");
329 printf ("extern void dfa_finish (void);\n");
331 else
333 /* Otherwise we do no scheduling, but we need these typedefs
334 in order to avoid uglifying other code with more ifdefs. */
335 printf ("typedef void *state_t;\n\n");
338 /* Special-purpose attributes should be tested with if, not #ifdef. */
339 const char * const special_attrs[] = { "length", "enabled", 0 };
340 for (const char * const *p = special_attrs; *p; p++)
342 printf ("#ifndef HAVE_ATTR_%s\n"
343 "#define HAVE_ATTR_%s 0\n"
344 "#endif\n", *p, *p);
346 /* We make an exception here to provide stub definitions for
347 insn_*_length* / get_attr_enabled functions. */
348 puts ("#if !HAVE_ATTR_length\n"
349 "extern int hook_int_rtx_unreachable (rtx);\n"
350 "#define insn_default_length hook_int_rtx_unreachable\n"
351 "#define insn_min_length hook_int_rtx_unreachable\n"
352 "#define insn_variable_length_p hook_int_rtx_unreachable\n"
353 "#define insn_current_length hook_int_rtx_unreachable\n"
354 "#include \"insn-addr.h\"\n"
355 "#endif\n"
356 "#if !HAVE_ATTR_enabled\n"
357 "extern int hook_int_rtx_1 (rtx);\n"
358 "#define get_attr_enabled hook_int_rtx_1\n"
359 "#endif\n");
361 /* Output flag masks for use by reorg.
363 Flags are used to hold branch direction for use by eligible_for_... */
364 printf ("\n#define ATTR_FLAG_forward\t0x1\n");
365 printf ("#define ATTR_FLAG_backward\t0x2\n");
367 puts ("\n#endif /* GCC_INSN_ATTR_H */");
369 if (ferror (stdout) || fflush (stdout) || fclose (stdout))
370 return FATAL_EXIT_CODE;
372 return SUCCESS_EXIT_CODE;