Add -march=interaptiv.
[platform/upstream/gcc.git] / gcc / genattr.c
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)
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>.  */
20
21
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"
30
31
32 static vec<rtx> const_attrs, reservations;
33
34
35 static void
36 gen_attr (md_rtx_info *info)
37 {
38   const char *p;
39   rtx attr = info->def;
40   int is_const = GET_CODE (XEXP (attr, 2)) == CONST;
41
42   if (is_const)
43     const_attrs.safe_push (attr);
44
45   printf ("#define HAVE_ATTR_%s 1\n", XSTR (attr, 0));
46
47   /* If numeric attribute, don't need to write an enum.  */
48   if (GET_CODE (attr) == DEFINE_ENUM_ATTR)
49     printf ("extern enum %s get_attr_%s (%s);\n\n",
50             XSTR (attr, 1), XSTR (attr, 0),
51             (is_const ? "void" : "rtx_insn *"));
52   else
53     {
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_insn *"));
58       else
59         printf ("extern enum attr_%s get_attr_%s (%s);\n\n",
60                 XSTR (attr, 0), XSTR (attr, 0),
61                 (is_const ? "void" : "rtx_insn *"));
62     }
63
64   /* If `length' attribute, write additional function definitions and define
65      variables used by `insn_current_length'.  */
66   if (! strcmp (XSTR (attr, 0), "length"))
67     {
68       puts ("\
69 extern void shorten_branches (rtx_insn *);\n\
70 extern int insn_default_length (rtx_insn *);\n\
71 extern int insn_min_length (rtx_insn *);\n\
72 extern int insn_variable_length_p (rtx_insn *);\n\
73 extern int insn_current_length (rtx_insn *);\n\n\
74 #include \"insn-addr.h\"\n");
75     }
76 }
77
78 /* Check that attribute NAME is used in define_insn_reservation condition
79    EXP.  Return true if it is.  */
80 static bool
81 check_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.  */
104 static bool
105 find_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
122       FOR_EACH_VEC_ELT (const_attrs, i, attr)
123         if (strcmp (XSTR (attr, 0), XSTR (exp, 0)) == 0)
124           {
125             unsigned int j;
126             rtx resv;
127
128             FOR_EACH_VEC_ELT (reservations, j, resv)
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
140 int
141 main (int argc, char **argv)
142 {
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;
148
149   progname = "genattr";
150
151   if (!init_rtx_reader_args (argc, argv))
152     return (FATAL_EXIT_CODE);
153
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");
158
159   puts ("#include \"insn-attr-common.h\"\n");
160
161   /* Read the machine description.  */
162
163   md_rtx_info info;
164   while (read_md_rtx (&info))
165     {
166       rtx def = info.def;
167       switch (GET_CODE (def))
168         {
169         case DEFINE_ATTR:
170         case DEFINE_ENUM_ATTR:
171           gen_attr (&info);
172           break;
173
174         case DEFINE_DELAY:
175           if (! have_delay)
176             {
177               printf ("extern int num_delay_slots (rtx_insn *);\n");
178               printf ("extern int eligible_for_delay (rtx_insn *, int, rtx_insn *, int);\n\n");
179               printf ("extern int const_num_delay_slots (rtx_insn *);\n\n");
180               have_delay = 1;
181             }
182
183           for (i = 0; i < XVECLEN (def, 1); i += 3)
184             {
185               if (XVECEXP (def, 1, i + 1) && ! have_annul_true)
186                 {
187                   printf ("#define ANNUL_IFTRUE_SLOTS\n");
188                   printf ("extern int eligible_for_annul_true (rtx_insn *, int, rtx_insn *, int);\n");
189                   have_annul_true = 1;
190                 }
191
192               if (XVECEXP (def, 1, i + 2) && ! have_annul_false)
193                 {
194                   printf ("#define ANNUL_IFFALSE_SLOTS\n");
195                   printf ("extern int eligible_for_annul_false (rtx_insn *, int, rtx_insn *, int);\n");
196                   have_annul_false = 1;
197                 }
198             }
199           break;
200
201         case DEFINE_INSN_RESERVATION:
202           num_insn_reservations++;
203           reservations.safe_push (def);
204           break;
205
206         default:
207           break;
208         }
209     }
210
211   if (num_insn_reservations > 0)
212     {
213       bool has_tune_attr
214         = find_tune_attr (XEXP (reservations[0], 2));
215       /* Output interface for pipeline hazards recognition based on
216          DFA (deterministic finite state automata.  */
217       printf ("\n/* DFA based pipeline interface.  */");
218       printf ("\n#ifndef AUTOMATON_ALTS\n");
219       printf ("#define AUTOMATON_ALTS 0\n");
220       printf ("#endif\n\n");
221       printf ("\n#ifndef AUTOMATON_STATE_ALTS\n");
222       printf ("#define AUTOMATON_STATE_ALTS 0\n");
223       printf ("#endif\n\n");
224       printf ("#ifndef CPU_UNITS_QUERY\n");
225       printf ("#define CPU_UNITS_QUERY 0\n");
226       printf ("#endif\n\n");
227       /* Interface itself: */
228       if (has_tune_attr)
229         {
230           printf ("/* Initialize fn pointers for internal_dfa_insn_code\n");
231           printf ("   and insn_default_latency.  */\n");
232           printf ("extern void init_sched_attrs (void);\n\n");
233           printf ("/* Internal insn code number used by automata.  */\n");
234           printf ("extern int (*internal_dfa_insn_code) (rtx_insn *);\n\n");
235           printf ("/* Insn latency time defined in define_insn_reservation. */\n");
236           printf ("extern int (*insn_default_latency) (rtx_insn *);\n\n");
237         }
238       else
239         {
240           printf ("#define init_sched_attrs() do { } while (0)\n\n");
241           printf ("/* Internal insn code number used by automata.  */\n");
242           printf ("extern int internal_dfa_insn_code (rtx_insn *);\n\n");
243           printf ("/* Insn latency time defined in define_insn_reservation. */\n");
244           printf ("extern int insn_default_latency (rtx_insn *);\n\n");
245         }
246       printf ("/* Return nonzero if there is a bypass for given insn\n");
247       printf ("   which is a data producer.  */\n");
248       printf ("extern int bypass_p (rtx_insn *);\n\n");
249       printf ("/* Insn latency time on data consumed by the 2nd insn.\n");
250       printf ("   Use the function if bypass_p returns nonzero for\n");
251       printf ("   the 1st insn. */\n");
252       printf ("extern int insn_latency (rtx, rtx);\n\n");
253       printf ("/* Maximal insn latency time possible of all bypasses for this insn.\n");
254       printf ("   Use the function if bypass_p returns nonzero for\n");
255       printf ("   the 1st insn. */\n");
256       printf ("extern int maximal_insn_latency (rtx);\n\n");
257       printf ("\n#if AUTOMATON_ALTS\n");
258       printf ("/* The following function returns number of alternative\n");
259       printf ("   reservations of given insn.  It may be used for better\n");
260       printf ("   insns scheduling heuristics. */\n");
261       printf ("extern int insn_alts (rtx);\n\n");
262       printf ("#endif\n\n");
263       printf ("/* Maximal possible number of insns waiting results being\n");
264       printf ("   produced by insns whose execution is not finished. */\n");
265       printf ("extern const int max_insn_queue_index;\n\n");
266       printf ("/* Pointer to data describing current state of DFA.  */\n");
267       printf ("typedef void *state_t;\n\n");
268       printf ("/* Size of the data in bytes.  */\n");
269       printf ("extern int state_size (void);\n\n");
270       printf ("/* Initiate given DFA state, i.e. Set up the state\n");
271       printf ("   as all functional units were not reserved.  */\n");
272       printf ("extern void state_reset (state_t);\n");
273       printf ("/* The following function returns negative value if given\n");
274       printf ("   insn can be issued in processor state described by given\n");
275       printf ("   DFA state.  In this case, the DFA state is changed to\n");
276       printf ("   reflect the current and future reservations by given\n");
277       printf ("   insn.  Otherwise the function returns minimal time\n");
278       printf ("   delay to issue the insn.  This delay may be zero\n");
279       printf ("   for superscalar or VLIW processors.  If the second\n");
280       printf ("   parameter is NULL the function changes given DFA state\n");
281       printf ("   as new processor cycle started.  */\n");
282       printf ("extern int state_transition (state_t, rtx);\n");
283       printf ("\n#if AUTOMATON_STATE_ALTS\n");
284       printf ("/* The following function returns number of possible\n");
285       printf ("   alternative reservations of given insn in given\n");
286       printf ("   DFA state.  It may be used for better insns scheduling\n");
287       printf ("   heuristics.  By default the function is defined if\n");
288       printf ("   macro AUTOMATON_STATE_ALTS is defined because its\n");
289       printf ("   implementation may require much memory.  */\n");
290       printf ("extern int state_alts (state_t, rtx);\n");
291       printf ("#endif\n\n");
292       printf ("extern int min_issue_delay (state_t, rtx_insn *);\n");
293       printf ("/* The following function returns nonzero if no one insn\n");
294       printf ("   can be issued in current DFA state. */\n");
295       printf ("extern int state_dead_lock_p (state_t);\n");
296       printf ("/* The function returns minimal delay of issue of the 2nd\n");
297       printf ("   insn after issuing the 1st insn in given DFA state.\n");
298       printf ("   The 1st insn should be issued in given state (i.e.\n");
299       printf ("    state_transition should return negative value for\n");
300       printf ("    the insn and the state).  Data dependencies between\n");
301       printf ("    the insns are ignored by the function.  */\n");
302       printf
303         ("extern int min_insn_conflict_delay (state_t, rtx, rtx);\n");
304       printf ("/* The following function outputs reservations for given\n");
305       printf ("   insn as they are described in the corresponding\n");
306       printf ("   define_insn_reservation.  */\n");
307       printf ("extern void print_reservation (FILE *, rtx_insn *);\n");
308       printf ("\n#if CPU_UNITS_QUERY\n");
309       printf ("/* The following function returns code of functional unit\n");
310       printf ("   with given name (see define_cpu_unit). */\n");
311       printf ("extern int get_cpu_unit_code (const char *);\n");
312       printf ("/* The following function returns nonzero if functional\n");
313       printf ("   unit with given code is currently reserved in given\n");
314       printf ("   DFA state.  */\n");
315       printf ("extern int cpu_unit_reservation_p (state_t, int);\n");
316       printf ("#endif\n\n");
317       printf ("/* The following function returns true if insn\n");
318       printf ("   has a dfa reservation.  */\n");
319       printf ("extern bool insn_has_dfa_reservation_p (rtx_insn *);\n\n");
320       printf ("/* Clean insn code cache.  It should be called if there\n");
321       printf ("   is a chance that condition value in a\n");
322       printf ("   define_insn_reservation will be changed after\n");
323       printf ("   last call of dfa_start.  */\n");
324       printf ("extern void dfa_clean_insn_cache (void);\n\n");
325       printf ("extern void dfa_clear_single_insn_cache (rtx_insn *);\n\n");
326       printf ("/* Initiate and finish work with DFA.  They should be\n");
327       printf ("   called as the first and the last interface\n");
328       printf ("   functions.  */\n");
329       printf ("extern void dfa_start (void);\n");
330       printf ("extern void dfa_finish (void);\n");
331     }
332   else
333     {
334       /* Otherwise we do no scheduling, but we need these typedefs
335          in order to avoid uglifying other code with more ifdefs.  */
336       printf ("typedef void *state_t;\n\n");
337     }
338
339   /* Special-purpose attributes should be tested with if, not #ifdef.  */
340   const char * const special_attrs[] = { "length", "enabled",
341                                          "preferred_for_size",
342                                          "preferred_for_speed", 0 };
343   for (const char * const *p = special_attrs; *p; p++)
344     {
345       printf ("#ifndef HAVE_ATTR_%s\n"
346               "#define HAVE_ATTR_%s 0\n"
347               "#endif\n", *p, *p);
348     }
349   /* We make an exception here to provide stub definitions for
350      insn_*_length* / get_attr_enabled functions.  */
351   puts ("#if !HAVE_ATTR_length\n"
352         "extern int hook_int_rtx_insn_unreachable (rtx_insn *);\n"
353         "#define insn_default_length hook_int_rtx_insn_unreachable\n"
354         "#define insn_min_length hook_int_rtx_insn_unreachable\n"
355         "#define insn_variable_length_p hook_int_rtx_insn_unreachable\n"
356         "#define insn_current_length hook_int_rtx_insn_unreachable\n"
357         "#include \"insn-addr.h\"\n"
358         "#endif\n"
359         "extern int hook_int_rtx_1 (rtx);\n"
360         "#if !HAVE_ATTR_enabled\n"
361         "#define get_attr_enabled hook_int_rtx_1\n"
362         "#endif\n"
363         "#if !HAVE_ATTR_preferred_for_size\n"
364         "#define get_attr_preferred_for_size hook_int_rtx_1\n"
365         "#endif\n"
366         "#if !HAVE_ATTR_preferred_for_speed\n"
367         "#define get_attr_preferred_for_speed hook_int_rtx_1\n"
368         "#endif\n");
369
370   /* Output flag masks for use by reorg.
371
372      Flags are used to hold branch direction for use by eligible_for_...  */
373   printf ("\n#define ATTR_FLAG_forward\t0x1\n");
374   printf ("#define ATTR_FLAG_backward\t0x2\n");
375
376   puts ("\n#endif /* GCC_INSN_ATTR_H */");
377
378   if (ferror (stdout) || fflush (stdout) || fclose (stdout))
379     return FATAL_EXIT_CODE;
380
381   return SUCCESS_EXIT_CODE;
382 }