2003-03-13 Aldy Hernandez <aldyh@redhat.com>
[official-gcc.git] / gcc / genattr.c
blob9f512a7ce9f9ccbb24a82986e7b1c08099a61f85
1 /* Generate attribute information (insn-attr.h) from machine description.
2 Copyright (C) 1991, 1994, 1996, 1998, 1999, 2000 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
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 "gensupport.h"
32 /* A range of values. */
34 struct range
36 int min;
37 int max;
40 /* Record information about each function unit mentioned in a
41 DEFINE_FUNCTION_UNIT. */
43 struct function_unit
45 char *name; /* Function unit name. */
46 struct function_unit *next; /* Next function unit. */
47 int multiplicity; /* Number of units of this type. */
48 int simultaneity; /* Maximum number of simultaneous insns
49 on this function unit or 0 if unlimited. */
50 struct range ready_cost; /* Range of ready cost values. */
51 struct range issue_delay; /* Range of issue delay values. */
54 static void extend_range PARAMS ((struct range *, int, int));
55 static void init_range PARAMS ((struct range *));
56 static void write_upcase PARAMS ((const char *));
57 static void gen_attr PARAMS ((rtx));
58 static void write_units PARAMS ((int, struct range *, struct range *,
59 struct range *, struct range *,
60 struct range *));
61 static void
62 extend_range (range, min, max)
63 struct range *range;
64 int min;
65 int max;
67 if (range->min > min) range->min = min;
68 if (range->max < max) range->max = max;
71 static void
72 init_range (range)
73 struct range *range;
75 range->min = 100000;
76 range->max = -1;
79 static void
80 write_upcase (str)
81 const char *str;
83 for (; *str; str++)
84 putchar (TOUPPER(*str));
87 static void
88 gen_attr (attr)
89 rtx attr;
91 const char *p, *tag;
92 int is_const = GET_CODE (XEXP (attr, 2)) == CONST;
94 printf ("#define HAVE_ATTR_%s\n", XSTR (attr, 0));
96 /* If numeric attribute, don't need to write an enum. */
97 p = XSTR (attr, 1);
98 if (*p == '\0')
99 printf ("extern int get_attr_%s PARAMS ((%s));\n", XSTR (attr, 0),
100 (is_const ? "void" : "rtx"));
101 else
103 printf ("enum attr_%s {", XSTR (attr, 0));
105 while ((tag = scan_comma_elt (&p)) != 0)
107 write_upcase (XSTR (attr, 0));
108 putchar ('_');
109 while (tag != p)
110 putchar (TOUPPER (*tag++));
111 if (*p == ',')
112 fputs (", ", stdout);
115 fputs ("};\n", stdout);
116 printf ("extern enum attr_%s get_attr_%s PARAMS ((%s));\n\n",
117 XSTR (attr, 0), XSTR (attr, 0), (is_const ? "void" : "rtx"));
120 /* If `length' attribute, write additional function definitions and define
121 variables used by `insn_current_length'. */
122 if (! strcmp (XSTR (attr, 0), "length"))
124 puts ("\
125 extern void shorten_branches PARAMS ((rtx));\n\
126 extern int insn_default_length PARAMS ((rtx));\n\
127 extern int insn_variable_length_p PARAMS ((rtx));\n\
128 extern int insn_current_length PARAMS ((rtx));\n\n\
129 #include \"insn-addr.h\"\n");
133 static void
134 write_units (num_units, multiplicity, simultaneity,
135 ready_cost, issue_delay, blockage)
136 int num_units;
137 struct range *multiplicity;
138 struct range *simultaneity;
139 struct range *ready_cost;
140 struct range *issue_delay;
141 struct range *blockage;
143 int i, q_size;
145 printf ("#define INSN_SCHEDULING\n\n");
146 printf ("extern int result_ready_cost PARAMS ((rtx));\n");
147 printf ("extern int function_units_used PARAMS ((rtx));\n\n");
148 printf ("extern const struct function_unit_desc\n");
149 printf ("{\n");
150 printf (" const char *const name;\n");
151 printf (" const int bitmask;\n");
152 printf (" const int multiplicity;\n");
153 printf (" const int simultaneity;\n");
154 printf (" const int default_cost;\n");
155 printf (" const int max_issue_delay;\n");
156 printf (" int (*const ready_cost_function) PARAMS ((rtx));\n");
157 printf (" int (*const conflict_cost_function) PARAMS ((rtx, rtx));\n");
158 printf (" const int max_blockage;\n");
159 printf (" unsigned int (*const blockage_range_function) PARAMS ((rtx));\n");
160 printf (" int (*const blockage_function) PARAMS ((rtx, rtx));\n");
161 printf ("} function_units[];\n\n");
162 printf ("#define FUNCTION_UNITS_SIZE %d\n", num_units);
163 printf ("#define MIN_MULTIPLICITY %d\n", multiplicity->min);
164 printf ("#define MAX_MULTIPLICITY %d\n", multiplicity->max);
165 printf ("#define MIN_SIMULTANEITY %d\n", simultaneity->min);
166 printf ("#define MAX_SIMULTANEITY %d\n", simultaneity->max);
167 printf ("#define MIN_READY_COST %d\n", ready_cost->min);
168 printf ("#define MAX_READY_COST %d\n", ready_cost->max);
169 printf ("#define MIN_ISSUE_DELAY %d\n", issue_delay->min);
170 printf ("#define MAX_ISSUE_DELAY %d\n", issue_delay->max);
171 printf ("#define MIN_BLOCKAGE %d\n", blockage->min);
172 printf ("#define MAX_BLOCKAGE %d\n", blockage->max);
173 for (i = 0; (1 << i) < blockage->max; i++)
175 printf ("#define BLOCKAGE_BITS %d\n", i + 1);
177 /* INSN_QUEUE_SIZE is a power of two larger than MAX_BLOCKAGE and
178 MAX_READY_COST. This is the longest time an isnsn may be queued. */
179 i = MAX (blockage->max, ready_cost->max);
180 for (q_size = 1; q_size <= i; q_size <<= 1)
182 printf ("#define INSN_QUEUE_SIZE %d\n", q_size);
185 extern int main PARAMS ((int, char **));
188 main (argc, argv)
189 int argc;
190 char **argv;
192 rtx desc;
193 int have_delay = 0;
194 int have_annul_true = 0;
195 int have_annul_false = 0;
196 int num_insn_reservations = 0;
197 int num_units = 0;
198 struct range all_simultaneity, all_multiplicity;
199 struct range all_ready_cost, all_issue_delay, all_blockage;
200 struct function_unit *units = 0, *unit;
201 int i;
203 init_range (&all_multiplicity);
204 init_range (&all_simultaneity);
205 init_range (&all_ready_cost);
206 init_range (&all_issue_delay);
207 init_range (&all_blockage);
209 progname = "genattr";
211 if (argc <= 1)
212 fatal ("no input file name");
214 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
215 return (FATAL_EXIT_CODE);
217 puts ("/* Generated automatically by the program `genattr'");
218 puts (" from the machine description file `md'. */\n");
219 puts ("#ifndef GCC_INSN_ATTR_H");
220 puts ("#define GCC_INSN_ATTR_H\n");
222 /* For compatibility, define the attribute `alternative', which is just
223 a reference to the variable `which_alternative'. */
225 puts ("#define HAVE_ATTR_alternative");
226 puts ("#define get_attr_alternative(insn) which_alternative");
228 /* Read the machine description. */
230 while (1)
232 int line_no, insn_code_number;
234 desc = read_md_rtx (&line_no, &insn_code_number);
235 if (desc == NULL)
236 break;
238 if (GET_CODE (desc) == DEFINE_ATTR)
239 gen_attr (desc);
241 else if (GET_CODE (desc) == DEFINE_DELAY)
243 if (! have_delay)
245 printf ("#define DELAY_SLOTS\n");
246 printf ("extern int num_delay_slots PARAMS ((rtx));\n");
247 printf ("extern int eligible_for_delay PARAMS ((rtx, int, rtx, int));\n\n");
248 printf ("extern int const_num_delay_slots PARAMS ((rtx));\n\n");
249 have_delay = 1;
252 for (i = 0; i < XVECLEN (desc, 1); i += 3)
254 if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
256 printf ("#define ANNUL_IFTRUE_SLOTS\n");
257 printf ("extern int eligible_for_annul_true PARAMS ((rtx, int, rtx, int));\n");
258 have_annul_true = 1;
261 if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
263 printf ("#define ANNUL_IFFALSE_SLOTS\n");
264 printf ("extern int eligible_for_annul_false PARAMS ((rtx, int, rtx, int));\n");
265 have_annul_false = 1;
270 else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
272 const char *name = XSTR (desc, 0);
273 int multiplicity = XINT (desc, 1);
274 int simultaneity = XINT (desc, 2);
275 int ready_cost = MAX (XINT (desc, 4), 1);
276 int issue_delay = MAX (XINT (desc, 5), 1);
277 int issueexp_p = (XVEC (desc, 6) != 0);
279 for (unit = units; unit; unit = unit->next)
280 if (strcmp (unit->name, name) == 0)
281 break;
283 if (unit == 0)
285 unit = (struct function_unit *)
286 xmalloc (sizeof (struct function_unit));
287 unit->name = xstrdup (name);
288 unit->multiplicity = multiplicity;
289 unit->simultaneity = simultaneity;
290 unit->ready_cost.min = unit->ready_cost.max = ready_cost;
291 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
292 unit->next = units;
293 units = unit;
294 num_units++;
296 extend_range (&all_multiplicity, multiplicity, multiplicity);
297 extend_range (&all_simultaneity, simultaneity, simultaneity);
299 else if (unit->multiplicity != multiplicity
300 || unit->simultaneity != simultaneity)
301 fatal ("Differing specifications given for `%s' function unit",
302 unit->name);
304 extend_range (&unit->ready_cost, ready_cost, ready_cost);
305 extend_range (&unit->issue_delay,
306 issueexp_p ? 1 : issue_delay, issue_delay);
307 extend_range (&all_ready_cost,
308 unit->ready_cost.min, unit->ready_cost.max);
309 extend_range (&all_issue_delay,
310 unit->issue_delay.min, unit->issue_delay.max);
312 else if (GET_CODE (desc) == DEFINE_INSN_RESERVATION)
313 num_insn_reservations++;
316 if (num_units > 0 || num_insn_reservations > 0)
318 if (num_units > 0)
319 printf ("#define TRADITIONAL_PIPELINE_INTERFACE 1\n");
321 if (num_insn_reservations > 0)
322 printf ("#define DFA_PIPELINE_INTERFACE 1\n");
324 /* Compute the range of blockage cost values. See genattrtab.c
325 for the derivation. BLOCKAGE (E,C) when SIMULTANEITY is zero is
327 MAX (ISSUE-DELAY (E,C),
328 READY-COST (E) - (READY-COST (C) - 1))
330 and otherwise
332 MAX (ISSUE-DELAY (E,C),
333 READY-COST (E) - (READY-COST (C) - 1),
334 READY-COST (E) - FILL-TIME) */
336 for (unit = units; unit; unit = unit->next)
338 struct range blockage;
340 blockage = unit->issue_delay;
341 blockage.max = MAX (unit->ready_cost.max
342 - (unit->ready_cost.min - 1),
343 blockage.max);
344 blockage.min = MAX (1, blockage.min);
346 if (unit->simultaneity != 0)
348 int fill_time = ((unit->simultaneity - 1)
349 * unit->issue_delay.min);
350 blockage.min = MAX (unit->ready_cost.min - fill_time,
351 blockage.min);
352 blockage.max = MAX (unit->ready_cost.max - fill_time,
353 blockage.max);
355 extend_range (&all_blockage, blockage.min, blockage.max);
358 write_units (num_units, &all_multiplicity, &all_simultaneity,
359 &all_ready_cost, &all_issue_delay, &all_blockage);
361 /* Output interface for pipeline hazards recognition based on
362 DFA (deterministic finite state automata. */
363 printf ("\n/* DFA based pipeline interface. */");
364 printf ("\n#ifndef AUTOMATON_ALTS\n");
365 printf ("#define AUTOMATON_ALTS 0\n");
366 printf ("#endif\n\n");
367 printf ("\n#ifndef AUTOMATON_STATE_ALTS\n");
368 printf ("#define AUTOMATON_STATE_ALTS 0\n");
369 printf ("#endif\n\n");
370 printf ("#ifndef CPU_UNITS_QUERY\n");
371 printf ("#define CPU_UNITS_QUERY 0\n");
372 printf ("#endif\n\n");
373 /* Interface itself: */
374 printf ("extern int max_dfa_issue_rate;\n\n");
375 printf ("/* The following macro value is calculated from the\n");
376 printf (" automaton based pipeline description and is equal to\n");
377 printf (" maximal number of all insns described in constructions\n");
378 printf (" `define_insn_reservation' which can be issued on the\n");
379 printf (" same processor cycle. */\n");
380 printf ("#define MAX_DFA_ISSUE_RATE max_dfa_issue_rate\n\n");
381 printf ("/* Insn latency time defined in define_insn_reservation. */\n");
382 printf ("extern int insn_default_latency PARAMS ((rtx));\n\n");
383 printf ("/* Return nonzero if there is a bypass for given insn\n");
384 printf (" which is a data producer. */\n");
385 printf ("extern int bypass_p PARAMS ((rtx));\n\n");
386 printf ("/* Insn latency time on data consumed by the 2nd insn.\n");
387 printf (" Use the function if bypass_p returns nonzero for\n");
388 printf (" the 1st insn. */\n");
389 printf ("extern int insn_latency PARAMS ((rtx, rtx));\n\n");
390 printf ("\n#if AUTOMATON_ALTS\n");
391 printf ("/* The following function returns number of alternative\n");
392 printf (" reservations of given insn. It may be used for better\n");
393 printf (" insns scheduling heuristics. */\n");
394 printf ("extern int insn_alts PARAMS ((rtx));\n\n");
395 printf ("#endif\n\n");
396 printf ("/* Maximal possible number of insns waiting results being\n");
397 printf (" produced by insns whose execution is not finished. */\n");
398 printf ("extern int max_insn_queue_index;\n\n");
399 printf ("/* Pointer to data describing current state of DFA. */\n");
400 printf ("typedef void *state_t;\n\n");
401 printf ("/* Size of the data in bytes. */\n");
402 printf ("extern int state_size PARAMS ((void));\n\n");
403 printf ("/* Initiate given DFA state, i.e. Set up the state\n");
404 printf (" as all functional units were not reserved. */\n");
405 printf ("extern void state_reset PARAMS ((state_t));\n");
406 printf ("/* The following function returns negative value if given\n");
407 printf (" insn can be issued in processor state described by given\n");
408 printf (" DFA state. In this case, the DFA state is changed to\n");
409 printf (" reflect the current and future reservations by given\n");
410 printf (" insn. Otherwise the function returns minimal time\n");
411 printf (" delay to issue the insn. This delay may be zero\n");
412 printf (" for superscalar or VLIW processors. If the second\n");
413 printf (" parameter is NULL the function changes given DFA state\n");
414 printf (" as new processor cycle started. */\n");
415 printf ("extern int state_transition PARAMS ((state_t, rtx));\n");
416 printf ("\n#if AUTOMATON_STATE_ALTS\n");
417 printf ("/* The following function returns number of possible\n");
418 printf (" alternative reservations of given insn in given\n");
419 printf (" DFA state. It may be used for better insns scheduling\n");
420 printf (" heuristics. By default the function is defined if\n");
421 printf (" macro AUTOMATON_STATE_ALTS is defined because its\n");
422 printf (" implementation may require much memory. */\n");
423 printf ("extern int state_alts PARAMS ((state_t, rtx));\n");
424 printf ("#endif\n\n");
425 printf ("extern int min_issue_delay PARAMS ((state_t, rtx));\n");
426 printf ("/* The following function returns nonzero if no one insn\n");
427 printf (" can be issued in current DFA state. */\n");
428 printf ("extern int state_dead_lock_p PARAMS ((state_t));\n");
429 printf ("/* The function returns minimal delay of issue of the 2nd\n");
430 printf (" insn after issuing the 1st insn in given DFA state.\n");
431 printf (" The 1st insn should be issued in given state (i.e.\n");
432 printf (" state_transition should return negative value for\n");
433 printf (" the insn and the state). Data dependencies between\n");
434 printf (" the insns are ignored by the function. */\n");
435 printf
436 ("extern int min_insn_conflict_delay PARAMS ((state_t, rtx, rtx));\n");
437 printf ("/* The following function outputs reservations for given\n");
438 printf (" insn as they are described in the corresponding\n");
439 printf (" define_insn_reservation. */\n");
440 printf ("extern void print_reservation PARAMS ((FILE *, rtx));\n");
441 printf ("\n#if CPU_UNITS_QUERY\n");
442 printf ("/* The following function returns code of functional unit\n");
443 printf (" with given name (see define_cpu_unit). */\n");
444 printf ("extern int get_cpu_unit_code PARAMS ((const char *));\n");
445 printf ("/* The following function returns nonzero if functional\n");
446 printf (" unit with given code is currently reserved in given\n");
447 printf (" DFA state. */\n");
448 printf ("extern int cpu_unit_reservation_p PARAMS ((state_t, int));\n");
449 printf ("#endif\n\n");
450 printf ("/* Clean insn code cache. It should be called if there\n");
451 printf (" is a chance that condition value in a\n");
452 printf (" define_insn_reservation will be changed after\n");
453 printf (" last call of dfa_start. */\n");
454 printf ("extern void dfa_clean_insn_cache PARAMS ((void));\n\n");
455 printf ("/* Initiate and finish work with DFA. They should be\n");
456 printf (" called as the first and the last interface\n");
457 printf (" functions. */\n");
458 printf ("extern void dfa_start PARAMS ((void));\n");
459 printf ("extern void dfa_finish PARAMS ((void));\n");
461 else
463 /* Otherwise we do no scheduling, but we need these typedefs
464 in order to avoid uglifying other code with more ifdefs. */
465 printf ("typedef void *state_t;\n\n");
468 /* Output flag masks for use by reorg.
470 Flags are used to hold branch direction and prediction information
471 for use by eligible_for_... */
472 printf("\n#define ATTR_FLAG_forward\t0x1\n");
473 printf("#define ATTR_FLAG_backward\t0x2\n");
474 printf("#define ATTR_FLAG_likely\t0x4\n");
475 printf("#define ATTR_FLAG_very_likely\t0x8\n");
476 printf("#define ATTR_FLAG_unlikely\t0x10\n");
477 printf("#define ATTR_FLAG_very_unlikely\t0x20\n");
479 puts("\n#endif /* GCC_INSN_ATTR_H */");
481 if (ferror (stdout) || fflush (stdout) || fclose (stdout))
482 return FATAL_EXIT_CODE;
484 return SUCCESS_EXIT_CODE;
487 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
488 const char *
489 get_insn_name (code)
490 int code ATTRIBUTE_UNUSED;
492 return NULL;