]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/genattr.c
backport: As described in http://gcc.gnu.org/ml/gcc/2012-08/msg00015.html...
[thirdparty/gcc.git] / gcc / genattr.c
CommitLineData
f12b8918 1/* Generate attribute information (insn-attr.h) from machine description.
2b21299c 2 Copyright (C) 1991, 1994, 1996, 1998, 1999, 2000, 2003, 2004, 2007, 2008,
88a00ef7 3 2010, 2011 Free Software Foundation, Inc.
c9309570 4 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
41299f41 5
1322177d 6This file is part of GCC.
41299f41 7
1322177d
LB
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
1322177d 11version.
41299f41 12
1322177d
LB
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
41299f41
TW
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
41299f41
TW
21
22
4977bab6 23#include "bconfig.h"
0b93b64e 24#include "system.h"
4977bab6
ZW
25#include "coretypes.h"
26#include "tm.h"
41299f41 27#include "rtl.h"
f8b6598e 28#include "errors.h"
10692477 29#include "read-md.h"
c88c0d42 30#include "gensupport.h"
41299f41 31
41299f41 32
3d7aafde 33static void gen_attr (rtx);
ef3fad04 34
2b21299c
JJ
35static VEC (rtx, heap) *const_attrs, *reservations;
36
37
4beba5da 38static void
3d7aafde 39gen_attr (rtx attr)
4beba5da 40{
c344a866 41 const char *p;
3d7aafde 42 int is_const = GET_CODE (XEXP (attr, 2)) == CONST;
41299f41 43
2b21299c
JJ
44 if (is_const)
45 VEC_safe_push (rtx, heap, const_attrs, attr);
46
f12b8918 47 printf ("#define HAVE_ATTR_%s\n", XSTR (attr, 0));
41299f41 48
f12b8918 49 /* If numeric attribute, don't need to write an enum. */
8f4fe86c
RS
50 if (GET_CODE (attr) == DEFINE_ENUM_ATTR)
51 printf ("extern enum %s get_attr_%s (%s);\n\n",
52 XSTR (attr, 1), XSTR (attr, 0), (is_const ? "void" : "rtx"));
41299f41
TW
53 else
54 {
8f4fe86c
RS
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"));
59 else
c344a866
JR
60 printf ("extern enum attr_%s get_attr_%s (%s);\n\n",
61 XSTR (attr, 0), XSTR (attr, 0), (is_const ? "void" : "rtx"));
41299f41
TW
62 }
63
f12b8918
TM
64 /* If `length' attribute, write additional function definitions and define
65 variables used by `insn_current_length'. */
66 if (! strcmp (XSTR (attr, 0), "length"))
41299f41 67 {
9a5834ae 68 puts ("\
3d7aafde
AJ
69extern void shorten_branches (rtx);\n\
70extern int insn_default_length (rtx);\n\
070a7956 71extern int insn_min_length (rtx);\n\
3d7aafde
AJ
72extern int insn_variable_length_p (rtx);\n\
73extern int insn_current_length (rtx);\n\n\
9a5834ae 74#include \"insn-addr.h\"\n");
41299f41
TW
75 }
76}
77
2b21299c
JJ
78/* Check that attribute NAME is used in define_insn_reservation condition
79 EXP. Return true if it is. */
80static bool
81check_tune_attr (const char *name, rtx exp)
82{
83 switch (GET_CODE (exp))
84 {
85 case AND:
86 if (check_tune_attr (name, XEXP (exp, 0)))
87 return true;
88 return check_tune_attr (name, XEXP (exp, 1));
89
90 case IOR:
91 return (check_tune_attr (name, XEXP (exp, 0))
92 && check_tune_attr (name, XEXP (exp, 1)));
93
94 case EQ_ATTR:
95 return strcmp (XSTR (exp, 0), name) == 0;
96
97 default:
98 return false;
99 }
100}
101
102/* Try to find a const attribute (usually cpu or tune) that is used
103 in all define_insn_reservation conditions. */
104static bool
105find_tune_attr (rtx exp)
106{
107 unsigned int i;
108 rtx attr;
109
110 switch (GET_CODE (exp))
111 {
112 case AND:
113 case IOR:
114 if (find_tune_attr (XEXP (exp, 0)))
115 return true;
116 return find_tune_attr (XEXP (exp, 1));
117
118 case EQ_ATTR:
119 if (strcmp (XSTR (exp, 0), "alternative") == 0)
120 return false;
121
ac47786e 122 FOR_EACH_VEC_ELT (rtx, const_attrs, i, attr)
2b21299c
JJ
123 if (strcmp (XSTR (attr, 0), XSTR (exp, 0)) == 0)
124 {
125 unsigned int j;
126 rtx resv;
127
ac47786e 128 FOR_EACH_VEC_ELT (rtx, reservations, j, resv)
2b21299c
JJ
129 if (! check_tune_attr (XSTR (attr, 0), XEXP (resv, 2)))
130 return false;
131 return true;
132 }
133 return false;
134
135 default:
136 return false;
137 }
138}
139
41299f41 140int
3d7aafde 141main (int argc, char **argv)
41299f41
TW
142{
143 rtx desc;
f12b8918
TM
144 int have_delay = 0;
145 int have_annul_true = 0;
146 int have_annul_false = 0;
fae15c93 147 int num_insn_reservations = 0;
41299f41
TW
148 int i;
149
f8b6598e 150 progname = "genattr";
41299f41 151
600ab3fc 152 if (!init_rtx_reader_args (argc, argv))
c88c0d42 153 return (FATAL_EXIT_CODE);
41299f41 154
0313e85b
ZW
155 puts ("/* Generated automatically by the program `genattr'");
156 puts (" from the machine description file `md'. */\n");
157 puts ("#ifndef GCC_INSN_ATTR_H");
158 puts ("#define GCC_INSN_ATTR_H\n");
41299f41 159
88a00ef7
JM
160 puts ("#include \"insn-attr-common.h\"\n");
161
f12b8918
TM
162 /* For compatibility, define the attribute `alternative', which is just
163 a reference to the variable `which_alternative'. */
164
0313e85b
ZW
165 puts ("#define HAVE_ATTR_alternative");
166 puts ("#define get_attr_alternative(insn) which_alternative");
3d7aafde 167
41299f41
TW
168 /* Read the machine description. */
169
170 while (1)
171 {
c88c0d42
CP
172 int line_no, insn_code_number;
173
174 desc = read_md_rtx (&line_no, &insn_code_number);
175 if (desc == NULL)
41299f41 176 break;
41299f41 177
8f4fe86c
RS
178 if (GET_CODE (desc) == DEFINE_ATTR
179 || GET_CODE (desc) == DEFINE_ENUM_ATTR)
f12b8918 180 gen_attr (desc);
41299f41 181
f12b8918
TM
182 else if (GET_CODE (desc) == DEFINE_DELAY)
183 {
184 if (! have_delay)
185 {
3d7aafde
AJ
186 printf ("extern int num_delay_slots (rtx);\n");
187 printf ("extern int eligible_for_delay (rtx, int, rtx, int);\n\n");
188 printf ("extern int const_num_delay_slots (rtx);\n\n");
f12b8918
TM
189 have_delay = 1;
190 }
41299f41 191
f12b8918
TM
192 for (i = 0; i < XVECLEN (desc, 1); i += 3)
193 {
194 if (XVECEXP (desc, 1, i + 1) && ! have_annul_true)
195 {
196 printf ("#define ANNUL_IFTRUE_SLOTS\n");
3d7aafde 197 printf ("extern int eligible_for_annul_true (rtx, int, rtx, int);\n");
f12b8918
TM
198 have_annul_true = 1;
199 }
ef3fad04 200
f12b8918
TM
201 if (XVECEXP (desc, 1, i + 2) && ! have_annul_false)
202 {
203 printf ("#define ANNUL_IFFALSE_SLOTS\n");
3d7aafde 204 printf ("extern int eligible_for_annul_false (rtx, int, rtx, int);\n");
f12b8918
TM
205 have_annul_false = 1;
206 }
207 }
208 }
ef3fad04 209
fae15c93 210 else if (GET_CODE (desc) == DEFINE_INSN_RESERVATION)
2b21299c
JJ
211 {
212 num_insn_reservations++;
213 VEC_safe_push (rtx, heap, reservations, desc);
214 }
f12b8918 215 }
ef3fad04 216
fa0aee89 217 if (num_insn_reservations > 0)
f12b8918 218 {
2b21299c
JJ
219 bool has_tune_attr
220 = find_tune_attr (XEXP (VEC_index (rtx, reservations, 0), 2));
fae15c93
VM
221 /* Output interface for pipeline hazards recognition based on
222 DFA (deterministic finite state automata. */
223 printf ("\n/* DFA based pipeline interface. */");
d530b07f
VM
224 printf ("\n#ifndef AUTOMATON_ALTS\n");
225 printf ("#define AUTOMATON_ALTS 0\n");
226 printf ("#endif\n\n");
fae15c93
VM
227 printf ("\n#ifndef AUTOMATON_STATE_ALTS\n");
228 printf ("#define AUTOMATON_STATE_ALTS 0\n");
229 printf ("#endif\n\n");
230 printf ("#ifndef CPU_UNITS_QUERY\n");
231 printf ("#define CPU_UNITS_QUERY 0\n");
232 printf ("#endif\n\n");
233 /* Interface itself: */
2b21299c
JJ
234 if (has_tune_attr)
235 {
236 printf ("/* Initialize fn pointers for internal_dfa_insn_code\n");
237 printf (" and insn_default_latency. */\n");
238 printf ("extern void init_sched_attrs (void);\n\n");
239 printf ("/* Internal insn code number used by automata. */\n");
240 printf ("extern int (*internal_dfa_insn_code) (rtx);\n\n");
241 printf ("/* Insn latency time defined in define_insn_reservation. */\n");
242 printf ("extern int (*insn_default_latency) (rtx);\n\n");
243 }
244 else
245 {
246 printf ("#define init_sched_attrs() do { } while (0)\n\n");
247 printf ("/* Internal insn code number used by automata. */\n");
248 printf ("extern int internal_dfa_insn_code (rtx);\n\n");
249 printf ("/* Insn latency time defined in define_insn_reservation. */\n");
250 printf ("extern int insn_default_latency (rtx);\n\n");
251 }
fae15c93
VM
252 printf ("/* Return nonzero if there is a bypass for given insn\n");
253 printf (" which is a data producer. */\n");
3d7aafde 254 printf ("extern int bypass_p (rtx);\n\n");
fae15c93
VM
255 printf ("/* Insn latency time on data consumed by the 2nd insn.\n");
256 printf (" Use the function if bypass_p returns nonzero for\n");
257 printf (" the 1st insn. */\n");
3d7aafde 258 printf ("extern int insn_latency (rtx, rtx);\n\n");
e855c69d
AB
259 printf ("/* Maximal insn latency time possible of all bypasses for this insn.\n");
260 printf (" Use the function if bypass_p returns nonzero for\n");
261 printf (" the 1st insn. */\n");
262 printf ("extern int maximal_insn_latency (rtx);\n\n");
d530b07f 263 printf ("\n#if AUTOMATON_ALTS\n");
fae15c93
VM
264 printf ("/* The following function returns number of alternative\n");
265 printf (" reservations of given insn. It may be used for better\n");
266 printf (" insns scheduling heuristics. */\n");
3d7aafde 267 printf ("extern int insn_alts (rtx);\n\n");
d530b07f 268 printf ("#endif\n\n");
fae15c93
VM
269 printf ("/* Maximal possible number of insns waiting results being\n");
270 printf (" produced by insns whose execution is not finished. */\n");
5f2f0edd 271 printf ("extern const int max_insn_queue_index;\n\n");
fae15c93
VM
272 printf ("/* Pointer to data describing current state of DFA. */\n");
273 printf ("typedef void *state_t;\n\n");
274 printf ("/* Size of the data in bytes. */\n");
3d7aafde 275 printf ("extern int state_size (void);\n\n");
fae15c93
VM
276 printf ("/* Initiate given DFA state, i.e. Set up the state\n");
277 printf (" as all functional units were not reserved. */\n");
3d7aafde 278 printf ("extern void state_reset (state_t);\n");
fae15c93
VM
279 printf ("/* The following function returns negative value if given\n");
280 printf (" insn can be issued in processor state described by given\n");
281 printf (" DFA state. In this case, the DFA state is changed to\n");
282 printf (" reflect the current and future reservations by given\n");
283 printf (" insn. Otherwise the function returns minimal time\n");
284 printf (" delay to issue the insn. This delay may be zero\n");
285 printf (" for superscalar or VLIW processors. If the second\n");
286 printf (" parameter is NULL the function changes given DFA state\n");
287 printf (" as new processor cycle started. */\n");
3d7aafde 288 printf ("extern int state_transition (state_t, rtx);\n");
fae15c93
VM
289 printf ("\n#if AUTOMATON_STATE_ALTS\n");
290 printf ("/* The following function returns number of possible\n");
291 printf (" alternative reservations of given insn in given\n");
292 printf (" DFA state. It may be used for better insns scheduling\n");
293 printf (" heuristics. By default the function is defined if\n");
294 printf (" macro AUTOMATON_STATE_ALTS is defined because its\n");
295 printf (" implementation may require much memory. */\n");
3d7aafde 296 printf ("extern int state_alts (state_t, rtx);\n");
fae15c93 297 printf ("#endif\n\n");
3d7aafde 298 printf ("extern int min_issue_delay (state_t, rtx);\n");
fae15c93
VM
299 printf ("/* The following function returns nonzero if no one insn\n");
300 printf (" can be issued in current DFA state. */\n");
3d7aafde 301 printf ("extern int state_dead_lock_p (state_t);\n");
fae15c93
VM
302 printf ("/* The function returns minimal delay of issue of the 2nd\n");
303 printf (" insn after issuing the 1st insn in given DFA state.\n");
304 printf (" The 1st insn should be issued in given state (i.e.\n");
305 printf (" state_transition should return negative value for\n");
306 printf (" the insn and the state). Data dependencies between\n");
307 printf (" the insns are ignored by the function. */\n");
308 printf
3d7aafde 309 ("extern int min_insn_conflict_delay (state_t, rtx, rtx);\n");
fae15c93
VM
310 printf ("/* The following function outputs reservations for given\n");
311 printf (" insn as they are described in the corresponding\n");
312 printf (" define_insn_reservation. */\n");
3d7aafde 313 printf ("extern void print_reservation (FILE *, rtx);\n");
fae15c93
VM
314 printf ("\n#if CPU_UNITS_QUERY\n");
315 printf ("/* The following function returns code of functional unit\n");
316 printf (" with given name (see define_cpu_unit). */\n");
3d7aafde 317 printf ("extern int get_cpu_unit_code (const char *);\n");
fae15c93
VM
318 printf ("/* The following function returns nonzero if functional\n");
319 printf (" unit with given code is currently reserved in given\n");
320 printf (" DFA state. */\n");
3d7aafde 321 printf ("extern int cpu_unit_reservation_p (state_t, int);\n");
dd1b7476 322 printf ("#endif\n\n");
1c3d0d93
MK
323 printf ("/* The following function returns true if insn\n");
324 printf (" has a dfa reservation. */\n");
325 printf ("extern bool insn_has_dfa_reservation_p (rtx);\n\n");
30028c85
VM
326 printf ("/* Clean insn code cache. It should be called if there\n");
327 printf (" is a chance that condition value in a\n");
328 printf (" define_insn_reservation will be changed after\n");
329 printf (" last call of dfa_start. */\n");
3d7aafde 330 printf ("extern void dfa_clean_insn_cache (void);\n\n");
b8698a0f 331 printf ("extern void dfa_clear_single_insn_cache (rtx);\n\n");
fae15c93
VM
332 printf ("/* Initiate and finish work with DFA. They should be\n");
333 printf (" called as the first and the last interface\n");
334 printf (" functions. */\n");
3d7aafde
AJ
335 printf ("extern void dfa_start (void);\n");
336 printf ("extern void dfa_finish (void);\n");
fae15c93
VM
337 }
338 else
339 {
340 /* Otherwise we do no scheduling, but we need these typedefs
341 in order to avoid uglifying other code with more ifdefs. */
342 printf ("typedef void *state_t;\n\n");
a224278b 343 }
b29d5f75 344
3d7aafde 345 /* Output flag masks for use by reorg.
eaa48dab 346
593dbe11 347 Flags are used to hold branch direction for use by eligible_for_... */
eaa48dab
JL
348 printf("\n#define ATTR_FLAG_forward\t0x1\n");
349 printf("#define ATTR_FLAG_backward\t0x2\n");
eaa48dab 350
0313e85b
ZW
351 puts("\n#endif /* GCC_INSN_ATTR_H */");
352
353 if (ferror (stdout) || fflush (stdout) || fclose (stdout))
354 return FATAL_EXIT_CODE;
355
356 return SUCCESS_EXIT_CODE;
41299f41 357}