poly_int: GET_MODE_NUNITS
[official-gcc.git] / gcc / genattrtab.c
blobf9b0bc94b0ff43df87da73e7e6ceca99af63c629
1 /* Generate code from machine description to compute values of attributes.
2 Copyright (C) 1991-2018 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/>. */
21 /* This program handles insn attributes and the DEFINE_DELAY and
22 DEFINE_INSN_RESERVATION definitions.
24 It produces a series of functions named `get_attr_...', one for each insn
25 attribute. Each of these is given the rtx for an insn and returns a member
26 of the enum for the attribute.
28 These subroutines have the form of a `switch' on the INSN_CODE (via
29 `recog_memoized'). Each case either returns a constant attribute value
30 or a value that depends on tests on other attributes, the form of
31 operands, or some random C expression (encoded with a SYMBOL_REF
32 expression).
34 If the attribute `alternative', or a random C expression is present,
35 `constrain_operands' is called. If either of these cases of a reference to
36 an operand is found, `extract_insn' is called.
38 The special attribute `length' is also recognized. For this operand,
39 expressions involving the address of an operand or the current insn,
40 (address (pc)), are valid. In this case, an initial pass is made to
41 set all lengths that do not depend on address. Those that do are set to
42 the maximum length. Then each insn that depends on an address is checked
43 and possibly has its length changed. The process repeats until no further
44 changed are made. The resulting lengths are saved for use by
45 `get_attr_length'.
47 A special form of DEFINE_ATTR, where the expression for default value is a
48 CONST expression, indicates an attribute that is constant for a given run
49 of the compiler. The subroutine generated for these attributes has no
50 parameters as it does not depend on any particular insn. Constant
51 attributes are typically used to specify which variety of processor is
52 used.
54 Internal attributes are defined to handle DEFINE_DELAY and
55 DEFINE_INSN_RESERVATION. Special routines are output for these cases.
57 This program works by keeping a list of possible values for each attribute.
58 These include the basic attribute choices, default values for attribute, and
59 all derived quantities.
61 As the description file is read, the definition for each insn is saved in a
62 `struct insn_def'. When the file reading is complete, a `struct insn_ent'
63 is created for each insn and chained to the corresponding attribute value,
64 either that specified, or the default.
66 An optimization phase is then run. This simplifies expressions for each
67 insn. EQ_ATTR tests are resolved, whenever possible, to a test that
68 indicates when the attribute has the specified value for the insn. This
69 avoids recursive calls during compilation.
71 The strategy used when processing DEFINE_DELAY definitions is to create
72 arbitrarily complex expressions and have the optimization simplify them.
74 Once optimization is complete, any required routines and definitions
75 will be written.
77 An optimization that is not yet implemented is to hoist the constant
78 expressions entirely out of the routines and definitions that are written.
79 A way to do this is to iterate over all possible combinations of values
80 for constant attributes and generate a set of functions for that given
81 combination. An initialization function would be written that evaluates
82 the attributes and installs the corresponding set of routines and
83 definitions (each would be accessed through a pointer).
85 We use the flags in an RTX as follows:
86 `unchanging' (ATTR_IND_SIMPLIFIED_P): This rtx is fully simplified
87 independent of the insn code.
88 `in_struct' (ATTR_CURR_SIMPLIFIED_P): This rtx is fully simplified
89 for the insn code currently being processed (see optimize_attrs).
90 `return_val' (ATTR_PERMANENT_P): This rtx is permanent and unique
91 (see attr_rtx). */
93 #define ATTR_IND_SIMPLIFIED_P(RTX) (RTX_FLAG ((RTX), unchanging))
94 #define ATTR_CURR_SIMPLIFIED_P(RTX) (RTX_FLAG ((RTX), in_struct))
95 #define ATTR_PERMANENT_P(RTX) (RTX_FLAG ((RTX), return_val))
97 #if 0
98 #define strcmp_check(S1, S2) ((S1) == (S2) \
99 ? 0 \
100 : (gcc_assert (strcmp ((S1), (S2))), 1))
101 #else
102 #define strcmp_check(S1, S2) ((S1) != (S2))
103 #endif
105 #include "bconfig.h"
106 #include "system.h"
107 #include "coretypes.h"
108 #include "tm.h"
109 #include "rtl.h"
110 #include "obstack.h"
111 #include "errors.h"
112 #include "read-md.h"
113 #include "gensupport.h"
114 #include "fnmatch.h"
116 #define DEBUG 0
118 /* Flags for make_internal_attr's `special' parameter. */
119 #define ATTR_NONE 0
120 #define ATTR_SPECIAL (1 << 0)
122 static struct obstack obstack1, obstack2;
123 static struct obstack *hash_obstack = &obstack1;
124 static struct obstack *temp_obstack = &obstack2;
126 /* enough space to reserve for printing out ints */
127 #define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)
129 /* Define structures used to record attributes and values. */
131 /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
132 encountered, we store all the relevant information into a
133 `struct insn_def'. This is done to allow attribute definitions to occur
134 anywhere in the file. */
136 struct insn_def
138 struct insn_def *next; /* Next insn in chain. */
139 rtx def; /* The DEFINE_... */
140 int insn_code; /* Instruction number. */
141 int insn_index; /* Expression number in file, for errors. */
142 file_location loc; /* Where in the .md files it occurs. */
143 int num_alternatives; /* Number of alternatives. */
144 int vec_idx; /* Index of attribute vector in `def'. */
147 /* Once everything has been read in, we store in each attribute value a list
148 of insn codes that have that value. Here is the structure used for the
149 list. */
151 struct insn_ent
153 struct insn_ent *next; /* Next in chain. */
154 struct insn_def *def; /* Instruction definition. */
157 /* Each value of an attribute (either constant or computed) is assigned a
158 structure which is used as the listhead of the insns that have that
159 value. */
161 struct attr_value
163 rtx value; /* Value of attribute. */
164 struct attr_value *next; /* Next attribute value in chain. */
165 struct insn_ent *first_insn; /* First insn with this value. */
166 int num_insns; /* Number of insns with this value. */
167 int has_asm_insn; /* True if this value used for `asm' insns */
170 /* Structure for each attribute. */
172 struct attr_desc
174 char *name; /* Name of attribute. */
175 const char *enum_name; /* Enum name for DEFINE_ENUM_NAME. */
176 struct attr_desc *next; /* Next attribute. */
177 struct attr_value *first_value; /* First value of this attribute. */
178 struct attr_value *default_val; /* Default value for this attribute. */
179 file_location loc; /* Where in the .md files it occurs. */
180 unsigned is_numeric : 1; /* Values of this attribute are numeric. */
181 unsigned is_const : 1; /* Attribute value constant for each run. */
182 unsigned is_special : 1; /* Don't call `write_attr_set'. */
185 /* Structure for each DEFINE_DELAY. */
187 struct delay_desc
189 rtx def; /* DEFINE_DELAY expression. */
190 struct delay_desc *next; /* Next DEFINE_DELAY. */
191 file_location loc; /* Where in the .md files it occurs. */
192 int num; /* Number of DEFINE_DELAY, starting at 1. */
195 struct attr_value_list
197 struct attr_value *av;
198 struct insn_ent *ie;
199 struct attr_desc *attr;
200 struct attr_value_list *next;
203 /* Listheads of above structures. */
205 /* This one is indexed by the first character of the attribute name. */
206 #define MAX_ATTRS_INDEX 256
207 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
208 static struct insn_def *defs;
209 static struct delay_desc *delays;
210 struct attr_value_list **insn_code_values;
212 /* Other variables. */
214 static int insn_index_number;
215 static int got_define_asm_attributes;
216 static int must_extract;
217 static int must_constrain;
218 static int address_used;
219 static int length_used;
220 static int num_delays;
221 static int have_annul_true, have_annul_false;
222 static int num_insn_ents;
224 /* Stores, for each insn code, the number of constraint alternatives. */
226 static int *insn_n_alternatives;
228 /* Stores, for each insn code, a bitmap that has bits on for each possible
229 alternative. */
231 static uint64_t *insn_alternatives;
233 /* Used to simplify expressions. */
235 static rtx true_rtx, false_rtx;
237 /* Used to reduce calls to `strcmp' */
239 static const char *alternative_name;
240 static const char *length_str;
241 static const char *delay_type_str;
242 static const char *delay_1_0_str;
243 static const char *num_delay_slots_str;
245 /* Simplify an expression. Only call the routine if there is something to
246 simplify. */
247 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
248 (ATTR_IND_SIMPLIFIED_P (EXP) || ATTR_CURR_SIMPLIFIED_P (EXP) ? (EXP) \
249 : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
251 #define DEF_ATTR_STRING(S) (attr_string ((S), strlen (S)))
253 /* Forward declarations of functions used before their definitions, only. */
254 static char *attr_string (const char *, int);
255 static char *attr_printf (unsigned int, const char *, ...)
256 ATTRIBUTE_PRINTF_2;
257 static rtx make_numeric_value (int);
258 static struct attr_desc *find_attr (const char **, int);
259 static rtx mk_attr_alt (uint64_t);
260 static char *next_comma_elt (const char **);
261 static rtx insert_right_side (enum rtx_code, rtx, rtx, int, int);
262 static rtx copy_boolean (rtx);
263 static int compares_alternatives_p (rtx);
264 static void make_internal_attr (const char *, rtx, int);
265 static void insert_insn_ent (struct attr_value *, struct insn_ent *);
266 static void walk_attr_value (rtx);
267 static int max_attr_value (rtx, int*);
268 static int min_attr_value (rtx, int*);
269 static int or_attr_value (rtx, int*);
270 static rtx simplify_test_exp (rtx, int, int);
271 static rtx simplify_test_exp_in_temp (rtx, int, int);
272 static rtx copy_rtx_unchanging (rtx);
273 static bool attr_alt_subset_p (rtx, rtx);
274 static bool attr_alt_subset_of_compl_p (rtx, rtx);
275 static void clear_struct_flag (rtx);
276 static void write_attr_valueq (FILE *, struct attr_desc *, const char *);
277 static struct attr_value *find_most_used (struct attr_desc *);
278 static void write_attr_set (FILE *, struct attr_desc *, int, rtx,
279 const char *, const char *, rtx,
280 int, int, unsigned int);
281 static void write_attr_case (FILE *, struct attr_desc *,
282 struct attr_value *,
283 int, const char *, const char *, int, rtx);
284 static void write_attr_value (FILE *, struct attr_desc *, rtx);
285 static void write_upcase (FILE *, const char *);
286 static void write_indent (FILE *, int);
287 static rtx identity_fn (rtx);
288 static rtx zero_fn (rtx);
289 static rtx one_fn (rtx);
290 static rtx max_fn (rtx);
291 static rtx min_fn (rtx);
293 #define oballoc(T) XOBNEW (hash_obstack, T)
294 #define oballocvec(T, N) XOBNEWVEC (hash_obstack, T, (N))
296 /* This gen* file is unique, in that it writes out multiple files.
298 Before GCC 4.8, insn-attrtab.c was written out containing many large
299 functions and tables. This made insn-attrtab.c _the_ bottle-neck in
300 a parallel build, and even made it impossible to build GCC on machines
301 with relatively small RAM space (PR other/29442). Therefore, the
302 atrribute functions/tables are now written out to three separate
303 files: all "*insn_default_latency" functions go to LATENCY_FILE_NAME,
304 all "*internal_dfa_insn_code" functions go to DFA_FILE_NAME, and the
305 rest goes to ATTR_FILE_NAME. */
307 static const char *attr_file_name = NULL;
308 static const char *dfa_file_name = NULL;
309 static const char *latency_file_name = NULL;
311 static FILE *attr_file, *dfa_file, *latency_file;
313 /* Hash table for sharing RTL and strings. */
315 /* Each hash table slot is a bucket containing a chain of these structures.
316 Strings are given negative hash codes; RTL expressions are given positive
317 hash codes. */
319 struct attr_hash
321 struct attr_hash *next; /* Next structure in the bucket. */
322 unsigned int hashcode; /* Hash code of this rtx or string. */
323 union
325 char *str; /* The string (negative hash codes) */
326 rtx rtl; /* or the RTL recorded here. */
327 } u;
330 /* Now here is the hash table. When recording an RTL, it is added to
331 the slot whose index is the hash code mod the table size. Note
332 that the hash table is used for several kinds of RTL (see attr_rtx)
333 and for strings. While all these live in the same table, they are
334 completely independent, and the hash code is computed differently
335 for each. */
337 #define RTL_HASH_SIZE 4093
338 static struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
340 /* Here is how primitive or already-shared RTL's hash
341 codes are made. */
342 #define RTL_HASH(RTL) ((intptr_t) (RTL) & 0777777)
344 /* Add an entry to the hash table for RTL with hash code HASHCODE. */
346 static void
347 attr_hash_add_rtx (unsigned int hashcode, rtx rtl)
349 struct attr_hash *h;
351 h = XOBNEW (hash_obstack, struct attr_hash);
352 h->hashcode = hashcode;
353 h->u.rtl = rtl;
354 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
355 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
358 /* Add an entry to the hash table for STRING with hash code HASHCODE. */
360 static void
361 attr_hash_add_string (unsigned int hashcode, char *str)
363 struct attr_hash *h;
365 h = XOBNEW (hash_obstack, struct attr_hash);
366 h->hashcode = -hashcode;
367 h->u.str = str;
368 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
369 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
372 /* Generate an RTL expression, but avoid duplicates.
373 Set the ATTR_PERMANENT_P flag for these permanent objects.
375 In some cases we cannot uniquify; then we return an ordinary
376 impermanent rtx with ATTR_PERMANENT_P clear.
378 Args are as follows:
380 rtx attr_rtx (code, [element1, ..., elementn]) */
382 static rtx
383 attr_rtx_1 (enum rtx_code code, va_list p)
385 rtx rt_val = NULL_RTX;/* RTX to return to caller... */
386 unsigned int hashcode;
387 struct attr_hash *h;
388 struct obstack *old_obstack = rtl_obstack;
389 int permanent_p = 1;
391 /* For each of several cases, search the hash table for an existing entry.
392 Use that entry if one is found; otherwise create a new RTL and add it
393 to the table. */
395 if (GET_RTX_CLASS (code) == RTX_UNARY)
397 rtx arg0 = va_arg (p, rtx);
399 if (! ATTR_PERMANENT_P (arg0))
400 permanent_p = 0;
402 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
403 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
404 if (h->hashcode == hashcode
405 && GET_CODE (h->u.rtl) == code
406 && XEXP (h->u.rtl, 0) == arg0)
407 return h->u.rtl;
409 if (h == 0)
411 rtl_obstack = hash_obstack;
412 rt_val = rtx_alloc (code);
413 XEXP (rt_val, 0) = arg0;
416 else if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
417 || GET_RTX_CLASS (code) == RTX_COMM_ARITH
418 || GET_RTX_CLASS (code) == RTX_COMPARE
419 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
421 rtx arg0 = va_arg (p, rtx);
422 rtx arg1 = va_arg (p, rtx);
424 if (! ATTR_PERMANENT_P (arg0) || ! ATTR_PERMANENT_P (arg1))
425 permanent_p = 0;
427 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
428 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
429 if (h->hashcode == hashcode
430 && GET_CODE (h->u.rtl) == code
431 && XEXP (h->u.rtl, 0) == arg0
432 && XEXP (h->u.rtl, 1) == arg1)
434 ATTR_CURR_SIMPLIFIED_P (h->u.rtl) = 0;
435 return h->u.rtl;
438 if (h == 0)
440 rtl_obstack = hash_obstack;
441 rt_val = rtx_alloc (code);
442 XEXP (rt_val, 0) = arg0;
443 XEXP (rt_val, 1) = arg1;
446 else if (code == SYMBOL_REF
447 || (GET_RTX_LENGTH (code) == 1
448 && GET_RTX_FORMAT (code)[0] == 's'))
450 char *arg0 = va_arg (p, char *);
452 arg0 = DEF_ATTR_STRING (arg0);
454 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
455 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
456 if (h->hashcode == hashcode
457 && GET_CODE (h->u.rtl) == code
458 && XSTR (h->u.rtl, 0) == arg0)
459 return h->u.rtl;
461 if (h == 0)
463 rtl_obstack = hash_obstack;
464 rt_val = rtx_alloc (code);
465 XSTR (rt_val, 0) = arg0;
466 if (code == SYMBOL_REF)
467 X0EXP (rt_val, 1) = NULL_RTX;
470 else if (GET_RTX_LENGTH (code) == 2
471 && GET_RTX_FORMAT (code)[0] == 's'
472 && GET_RTX_FORMAT (code)[1] == 's')
474 char *arg0 = va_arg (p, char *);
475 char *arg1 = va_arg (p, char *);
477 arg0 = DEF_ATTR_STRING (arg0);
478 arg1 = DEF_ATTR_STRING (arg1);
480 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
481 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
482 if (h->hashcode == hashcode
483 && GET_CODE (h->u.rtl) == code
484 && XSTR (h->u.rtl, 0) == arg0
485 && XSTR (h->u.rtl, 1) == arg1)
486 return h->u.rtl;
488 if (h == 0)
490 rtl_obstack = hash_obstack;
491 rt_val = rtx_alloc (code);
492 XSTR (rt_val, 0) = arg0;
493 XSTR (rt_val, 1) = arg1;
496 else if (GET_RTX_LENGTH (code) == 2
497 && GET_RTX_FORMAT (code)[0] == 'i'
498 && GET_RTX_FORMAT (code)[1] == 'i')
500 int arg0 = va_arg (p, int);
501 int arg1 = va_arg (p, int);
503 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
504 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
505 if (h->hashcode == hashcode
506 && GET_CODE (h->u.rtl) == code
507 && XINT (h->u.rtl, 0) == arg0
508 && XINT (h->u.rtl, 1) == arg1)
509 return h->u.rtl;
511 if (h == 0)
513 rtl_obstack = hash_obstack;
514 rt_val = rtx_alloc (code);
515 XINT (rt_val, 0) = arg0;
516 XINT (rt_val, 1) = arg1;
519 else if (code == CONST_INT)
521 HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
522 if (arg0 == 0)
523 return false_rtx;
524 else if (arg0 == 1)
525 return true_rtx;
526 else
527 goto nohash;
529 else
531 int i; /* Array indices... */
532 const char *fmt; /* Current rtx's format... */
533 nohash:
534 rt_val = rtx_alloc (code); /* Allocate the storage space. */
536 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
537 for (i = 0; i < GET_RTX_LENGTH (code); i++)
539 switch (*fmt++)
541 case '0': /* Unused field. */
542 break;
544 case 'i': /* An integer? */
545 XINT (rt_val, i) = va_arg (p, int);
546 break;
548 case 'w': /* A wide integer? */
549 XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
550 break;
552 case 's': /* A string? */
553 XSTR (rt_val, i) = va_arg (p, char *);
554 break;
556 case 'e': /* An expression? */
557 case 'u': /* An insn? Same except when printing. */
558 XEXP (rt_val, i) = va_arg (p, rtx);
559 break;
561 case 'E': /* An RTX vector? */
562 XVEC (rt_val, i) = va_arg (p, rtvec);
563 break;
565 default:
566 /* Don't need to handle 'p' for attributes. */
567 gcc_unreachable ();
570 return rt_val;
573 rtl_obstack = old_obstack;
574 attr_hash_add_rtx (hashcode, rt_val);
575 ATTR_PERMANENT_P (rt_val) = permanent_p;
576 return rt_val;
579 static rtx
580 attr_rtx (enum rtx_code code, ...)
582 rtx result;
583 va_list p;
585 va_start (p, code);
586 result = attr_rtx_1 (code, p);
587 va_end (p);
588 return result;
591 /* Create a new string printed with the printf line arguments into a space
592 of at most LEN bytes:
594 rtx attr_printf (len, format, [arg1, ..., argn]) */
596 static char *
597 attr_printf (unsigned int len, const char *fmt, ...)
599 char str[256];
600 va_list p;
602 va_start (p, fmt);
604 gcc_assert (len < sizeof str); /* Leave room for \0. */
606 vsprintf (str, fmt, p);
607 va_end (p);
609 return DEF_ATTR_STRING (str);
612 static rtx
613 attr_eq (const char *name, const char *value)
615 return attr_rtx (EQ_ATTR, name, value);
618 static const char *
619 attr_numeral (int n)
621 return XSTR (make_numeric_value (n), 0);
624 /* Return a permanent (possibly shared) copy of a string STR (not assumed
625 to be null terminated) with LEN bytes. */
627 static char *
628 attr_string (const char *str, int len)
630 struct attr_hash *h;
631 unsigned int hashcode;
632 int i;
633 char *new_str;
635 /* Compute the hash code. */
636 hashcode = (len + 1) * 613U + (unsigned) str[0];
637 for (i = 1; i < len; i += 2)
638 hashcode = ((hashcode * 613) + (unsigned) str[i]);
639 if ((int) hashcode < 0)
640 hashcode = -hashcode;
642 /* Search the table for the string. */
643 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
644 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
645 && !strncmp (h->u.str, str, len))
646 return h->u.str; /* <-- return if found. */
648 /* Not found; create a permanent copy and add it to the hash table. */
649 new_str = XOBNEWVAR (hash_obstack, char, len + 1);
650 memcpy (new_str, str, len);
651 new_str[len] = '\0';
652 attr_hash_add_string (hashcode, new_str);
653 rtx_reader_ptr->copy_md_ptr_loc (new_str, str);
655 return new_str; /* Return the new string. */
658 /* Check two rtx's for equality of contents,
659 taking advantage of the fact that if both are hashed
660 then they can't be equal unless they are the same object. */
662 static int
663 attr_equal_p (rtx x, rtx y)
665 return (x == y || (! (ATTR_PERMANENT_P (x) && ATTR_PERMANENT_P (y))
666 && rtx_equal_p (x, y)));
669 /* Given a test expression EXP for attribute ATTR, ensure it is validly
670 formed. LOC is the location of the .md construct that contains EXP.
672 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
673 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
674 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
676 Update the string address in EQ_ATTR expression to be the same used
677 in the attribute (or `alternative_name') to speed up subsequent
678 `find_attr' calls and eliminate most `strcmp' calls.
680 Return the new expression, if any. */
682 static rtx
683 check_attr_test (file_location loc, rtx exp, attr_desc *attr)
685 struct attr_value *av;
686 const char *name_ptr, *p;
687 rtx orexp, newexp;
689 switch (GET_CODE (exp))
691 case EQ_ATTR:
692 /* Handle negation test. */
693 if (XSTR (exp, 1)[0] == '!')
694 return check_attr_test (loc,
695 attr_rtx (NOT,
696 attr_eq (XSTR (exp, 0),
697 &XSTR (exp, 1)[1])),
698 attr);
700 else if (n_comma_elts (XSTR (exp, 1)) == 1)
702 attr_desc *attr2 = find_attr (&XSTR (exp, 0), 0);
703 if (attr2 == NULL)
705 if (! strcmp (XSTR (exp, 0), "alternative"))
706 return mk_attr_alt (((uint64_t) 1) << atoi (XSTR (exp, 1)));
707 else
708 fatal_at (loc, "unknown attribute `%s' in definition of"
709 " attribute `%s'", XSTR (exp, 0), attr->name);
712 if (attr->is_const && ! attr2->is_const)
713 fatal_at (loc, "constant attribute `%s' cannot test non-constant"
714 " attribute `%s'", attr->name, attr2->name);
716 /* Copy this just to make it permanent,
717 so expressions using it can be permanent too. */
718 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
720 /* It shouldn't be possible to simplify the value given to a
721 constant attribute, so don't expand this until it's time to
722 write the test expression. */
723 if (attr2->is_const)
724 ATTR_IND_SIMPLIFIED_P (exp) = 1;
726 if (attr2->is_numeric)
728 for (p = XSTR (exp, 1); *p; p++)
729 if (! ISDIGIT (*p))
730 fatal_at (loc, "attribute `%s' takes only numeric values",
731 attr2->name);
733 else
735 for (av = attr2->first_value; av; av = av->next)
736 if (GET_CODE (av->value) == CONST_STRING
737 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
738 break;
740 if (av == NULL)
741 fatal_at (loc, "unknown value `%s' for attribute `%s'",
742 XSTR (exp, 1), attr2->name);
745 else
747 if (! strcmp (XSTR (exp, 0), "alternative"))
749 int set = 0;
751 name_ptr = XSTR (exp, 1);
752 while ((p = next_comma_elt (&name_ptr)) != NULL)
753 set |= ((uint64_t) 1) << atoi (p);
755 return mk_attr_alt (set);
757 else
759 /* Make an IOR tree of the possible values. */
760 orexp = false_rtx;
761 name_ptr = XSTR (exp, 1);
762 while ((p = next_comma_elt (&name_ptr)) != NULL)
764 newexp = attr_eq (XSTR (exp, 0), p);
765 orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
768 return check_attr_test (loc, orexp, attr);
771 break;
773 case ATTR_FLAG:
774 break;
776 case CONST_INT:
777 /* Either TRUE or FALSE. */
778 if (XWINT (exp, 0))
779 return true_rtx;
780 else
781 return false_rtx;
783 case IOR:
784 case AND:
785 XEXP (exp, 0) = check_attr_test (loc, XEXP (exp, 0), attr);
786 XEXP (exp, 1) = check_attr_test (loc, XEXP (exp, 1), attr);
787 break;
789 case NOT:
790 XEXP (exp, 0) = check_attr_test (loc, XEXP (exp, 0), attr);
791 break;
793 case MATCH_TEST:
794 exp = attr_rtx (MATCH_TEST, XSTR (exp, 0));
795 ATTR_IND_SIMPLIFIED_P (exp) = 1;
796 break;
798 case MATCH_OPERAND:
799 if (attr->is_const)
800 fatal_at (loc, "invalid operator `%s' in definition of constant"
801 " attribute `%s'", GET_RTX_NAME (GET_CODE (exp)),
802 attr->name);
803 /* These cases can't be simplified. */
804 ATTR_IND_SIMPLIFIED_P (exp) = 1;
805 break;
807 case LE: case LT: case GT: case GE:
808 case LEU: case LTU: case GTU: case GEU:
809 case NE: case EQ:
810 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
811 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
812 exp = attr_rtx (GET_CODE (exp),
813 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
814 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
815 /* These cases can't be simplified. */
816 ATTR_IND_SIMPLIFIED_P (exp) = 1;
817 break;
819 case SYMBOL_REF:
820 if (attr->is_const)
822 /* These cases are valid for constant attributes, but can't be
823 simplified. */
824 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
825 ATTR_IND_SIMPLIFIED_P (exp) = 1;
826 break;
828 /* FALLTHRU */
829 default:
830 fatal_at (loc, "invalid operator `%s' in definition of attribute"
831 " `%s'", GET_RTX_NAME (GET_CODE (exp)), attr->name);
834 return exp;
837 /* Given an expression EXP, ensure that it is validly formed and that
838 all named attribute values are valid for ATTR. Issue an error if not.
839 LOC is the location of the .md construct that contains EXP.
841 Return a perhaps modified replacement expression for the value. */
843 static rtx
844 check_attr_value (file_location loc, rtx exp, struct attr_desc *attr)
846 struct attr_value *av;
847 const char *p;
848 int i;
850 switch (GET_CODE (exp))
852 case CONST_INT:
853 if (!attr->is_numeric)
855 error_at (loc,
856 "CONST_INT not valid for non-numeric attribute `%s'",
857 attr->name);
858 break;
861 if (INTVAL (exp) < 0)
863 error_at (loc,
864 "negative numeric value specified for attribute `%s'",
865 attr->name);
866 break;
868 break;
870 case CONST_STRING:
871 if (! strcmp (XSTR (exp, 0), "*"))
872 break;
874 if (attr->is_numeric)
876 p = XSTR (exp, 0);
877 for (; *p; p++)
878 if (! ISDIGIT (*p))
880 error_at (loc,
881 "non-numeric value specified for numeric"
882 " attribute `%s'", attr->name);
883 break;
885 break;
888 for (av = attr->first_value; av; av = av->next)
889 if (GET_CODE (av->value) == CONST_STRING
890 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
891 break;
893 if (av == NULL)
894 error_at (loc, "unknown value `%s' for attribute `%s'",
895 XSTR (exp, 0), attr->name);
896 break;
898 case IF_THEN_ELSE:
899 XEXP (exp, 0) = check_attr_test (loc, XEXP (exp, 0), attr);
900 XEXP (exp, 1) = check_attr_value (loc, XEXP (exp, 1), attr);
901 XEXP (exp, 2) = check_attr_value (loc, XEXP (exp, 2), attr);
902 break;
904 case PLUS:
905 case MINUS:
906 case MULT:
907 case DIV:
908 case MOD:
909 if (!attr->is_numeric)
911 error_at (loc, "invalid operation `%s' for non-numeric"
912 " attribute `%s'", GET_RTX_NAME (GET_CODE (exp)),
913 attr->name);
914 break;
916 /* Fall through. */
918 case IOR:
919 case AND:
920 XEXP (exp, 0) = check_attr_value (loc, XEXP (exp, 0), attr);
921 XEXP (exp, 1) = check_attr_value (loc, XEXP (exp, 1), attr);
922 break;
924 case FFS:
925 case CLZ:
926 case CTZ:
927 case POPCOUNT:
928 case PARITY:
929 case BSWAP:
930 XEXP (exp, 0) = check_attr_value (loc, XEXP (exp, 0), attr);
931 break;
933 case COND:
934 if (XVECLEN (exp, 0) % 2 != 0)
936 error_at (loc, "first operand of COND must have even length");
937 break;
940 for (i = 0; i < XVECLEN (exp, 0); i += 2)
942 XVECEXP (exp, 0, i) = check_attr_test (attr->loc,
943 XVECEXP (exp, 0, i),
944 attr);
945 XVECEXP (exp, 0, i + 1)
946 = check_attr_value (loc, XVECEXP (exp, 0, i + 1), attr);
949 XEXP (exp, 1) = check_attr_value (loc, XEXP (exp, 1), attr);
950 break;
952 case ATTR:
954 struct attr_desc *attr2 = find_attr (&XSTR (exp, 0), 0);
955 if (attr2 == NULL)
956 error_at (loc, "unknown attribute `%s' in ATTR",
957 XSTR (exp, 0));
958 else if (attr->is_const && ! attr2->is_const)
959 error_at (attr->loc,
960 "constant attribute `%s' cannot refer to non-constant"
961 " attribute `%s'", attr->name, attr2->name);
962 else if (attr->is_numeric != attr2->is_numeric)
963 error_at (loc,
964 "numeric attribute mismatch calling `%s' from `%s'",
965 attr2->name, attr->name);
967 break;
969 case SYMBOL_REF:
970 /* A constant SYMBOL_REF is valid as a constant attribute test and
971 is expanded later by make_canonical into a COND. In a non-constant
972 attribute test, it is left be. */
973 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
975 default:
976 error_at (loc, "invalid operator `%s' in definition of attribute `%s'",
977 GET_RTX_NAME (GET_CODE (exp)), attr->name);
978 break;
981 return exp;
984 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
985 It becomes a COND with each test being (eq_attr "alternative" "n") */
987 static rtx
988 convert_set_attr_alternative (rtx exp, struct insn_def *id)
990 int num_alt = id->num_alternatives;
991 rtx condexp;
992 int i;
994 if (XVECLEN (exp, 1) != num_alt)
996 error_at (id->loc, "bad number of entries in SET_ATTR_ALTERNATIVE,"
997 " was %d expected %d", XVECLEN (exp, 1), num_alt);
998 return NULL_RTX;
1001 /* Make a COND with all tests but the last. Select the last value via the
1002 default. */
1003 condexp = rtx_alloc (COND);
1004 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1006 for (i = 0; i < num_alt - 1; i++)
1008 const char *p;
1009 p = attr_numeral (i);
1011 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1012 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1015 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1017 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1020 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1021 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1023 static rtx
1024 convert_set_attr (rtx exp, struct insn_def *id)
1026 rtx newexp;
1027 const char *name_ptr;
1028 char *p;
1029 int n;
1031 /* See how many alternative specified. */
1032 n = n_comma_elts (XSTR (exp, 1));
1033 if (n == 1)
1034 return attr_rtx (SET,
1035 attr_rtx (ATTR, XSTR (exp, 0)),
1036 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1038 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1039 XSTR (newexp, 0) = XSTR (exp, 0);
1040 XVEC (newexp, 1) = rtvec_alloc (n);
1042 /* Process each comma-separated name. */
1043 name_ptr = XSTR (exp, 1);
1044 n = 0;
1045 while ((p = next_comma_elt (&name_ptr)) != NULL)
1046 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1048 return convert_set_attr_alternative (newexp, id);
1051 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1052 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1053 expressions. */
1055 static void
1056 check_defs (void)
1058 struct insn_def *id;
1059 struct attr_desc *attr;
1060 int i;
1061 rtx value;
1063 for (id = defs; id; id = id->next)
1065 if (XVEC (id->def, id->vec_idx) == NULL)
1066 continue;
1068 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1070 value = XVECEXP (id->def, id->vec_idx, i);
1071 switch (GET_CODE (value))
1073 case SET:
1074 if (GET_CODE (XEXP (value, 0)) != ATTR)
1076 error_at (id->loc, "bad attribute set");
1077 value = NULL_RTX;
1079 break;
1081 case SET_ATTR_ALTERNATIVE:
1082 value = convert_set_attr_alternative (value, id);
1083 break;
1085 case SET_ATTR:
1086 value = convert_set_attr (value, id);
1087 break;
1089 default:
1090 error_at (id->loc, "invalid attribute code %s",
1091 GET_RTX_NAME (GET_CODE (value)));
1092 value = NULL_RTX;
1094 if (value == NULL_RTX)
1095 continue;
1097 if ((attr = find_attr (&XSTR (XEXP (value, 0), 0), 0)) == NULL)
1099 error_at (id->loc, "unknown attribute %s",
1100 XSTR (XEXP (value, 0), 0));
1101 continue;
1104 XVECEXP (id->def, id->vec_idx, i) = value;
1105 XEXP (value, 1) = check_attr_value (id->loc, XEXP (value, 1), attr);
1110 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1111 expressions by converting them into a COND. This removes cases from this
1112 program. Also, replace an attribute value of "*" with the default attribute
1113 value. LOC is the location to use for error reporting. */
1115 static rtx
1116 make_canonical (file_location loc, struct attr_desc *attr, rtx exp)
1118 int i;
1119 rtx newexp;
1121 switch (GET_CODE (exp))
1123 case CONST_INT:
1124 exp = make_numeric_value (INTVAL (exp));
1125 break;
1127 case CONST_STRING:
1128 if (! strcmp (XSTR (exp, 0), "*"))
1130 if (attr->default_val == 0)
1131 fatal_at (loc, "(attr_value \"*\") used in invalid context");
1132 exp = attr->default_val->value;
1134 else
1135 XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1137 break;
1139 case SYMBOL_REF:
1140 if (!attr->is_const || ATTR_IND_SIMPLIFIED_P (exp))
1141 break;
1142 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1143 This makes the COND something that won't be considered an arbitrary
1144 expression by walk_attr_value. */
1145 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1146 exp = check_attr_value (loc, exp, attr);
1147 break;
1149 case IF_THEN_ELSE:
1150 newexp = rtx_alloc (COND);
1151 XVEC (newexp, 0) = rtvec_alloc (2);
1152 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1153 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1155 XEXP (newexp, 1) = XEXP (exp, 2);
1157 exp = newexp;
1158 /* Fall through to COND case since this is now a COND. */
1159 gcc_fallthrough ();
1161 case COND:
1163 int allsame = 1;
1164 rtx defval;
1166 /* First, check for degenerate COND. */
1167 if (XVECLEN (exp, 0) == 0)
1168 return make_canonical (loc, attr, XEXP (exp, 1));
1169 defval = XEXP (exp, 1) = make_canonical (loc, attr, XEXP (exp, 1));
1171 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1173 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1174 XVECEXP (exp, 0, i + 1)
1175 = make_canonical (loc, attr, XVECEXP (exp, 0, i + 1));
1176 if (! attr_equal_p (XVECEXP (exp, 0, i + 1), defval))
1177 allsame = 0;
1179 if (allsame)
1180 return defval;
1182 break;
1184 default:
1185 break;
1188 return exp;
1191 static rtx
1192 copy_boolean (rtx exp)
1194 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1195 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1196 copy_boolean (XEXP (exp, 1)));
1197 else if (GET_CODE (exp) == NOT)
1198 return attr_rtx (NOT, copy_boolean (XEXP (exp, 0)));
1199 if (GET_CODE (exp) == MATCH_OPERAND)
1201 XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1202 XSTR (exp, 2) = DEF_ATTR_STRING (XSTR (exp, 2));
1204 else if (GET_CODE (exp) == EQ_ATTR)
1206 XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1207 XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1210 return exp;
1213 /* Given a value and an attribute description, return a `struct attr_value *'
1214 that represents that value. This is either an existing structure, if the
1215 value has been previously encountered, or a newly-created structure.
1217 `insn_code' is the code of an insn whose attribute has the specified
1218 value (-2 if not processing an insn). We ensure that all insns for
1219 a given value have the same number of alternatives if the value checks
1220 alternatives. LOC is the location to use for error reporting. */
1222 static struct attr_value *
1223 get_attr_value (file_location loc, rtx value, struct attr_desc *attr,
1224 int insn_code)
1226 struct attr_value *av;
1227 uint64_t num_alt = 0;
1229 value = make_canonical (loc, attr, value);
1230 if (compares_alternatives_p (value))
1232 if (insn_code < 0 || insn_alternatives == NULL)
1233 fatal_at (loc, "(eq_attr \"alternatives\" ...) used in non-insn"
1234 " context");
1235 else
1236 num_alt = insn_alternatives[insn_code];
1239 for (av = attr->first_value; av; av = av->next)
1240 if (attr_equal_p (value, av->value)
1241 && (num_alt == 0 || av->first_insn == NULL
1242 || insn_alternatives[av->first_insn->def->insn_code]))
1243 return av;
1245 av = oballoc (struct attr_value);
1246 av->value = value;
1247 av->next = attr->first_value;
1248 attr->first_value = av;
1249 av->first_insn = NULL;
1250 av->num_insns = 0;
1251 av->has_asm_insn = 0;
1253 return av;
1256 /* After all DEFINE_DELAYs have been read in, create internal attributes
1257 to generate the required routines.
1259 First, we compute the number of delay slots for each insn (as a COND of
1260 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1261 delay type is specified, we compute a similar function giving the
1262 DEFINE_DELAY ordinal for each insn.
1264 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1265 tells whether a given insn can be in that delay slot.
1267 Normal attribute filling and optimization expands these to contain the
1268 information needed to handle delay slots. */
1270 static void
1271 expand_delays (void)
1273 struct delay_desc *delay;
1274 rtx condexp;
1275 rtx newexp;
1276 int i;
1277 char *p;
1279 /* First, generate data for `num_delay_slots' function. */
1281 condexp = rtx_alloc (COND);
1282 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1283 XEXP (condexp, 1) = make_numeric_value (0);
1285 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1287 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1288 XVECEXP (condexp, 0, i + 1)
1289 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1292 make_internal_attr (num_delay_slots_str, condexp, ATTR_NONE);
1294 /* If more than one delay type, do the same for computing the delay type. */
1295 if (num_delays > 1)
1297 condexp = rtx_alloc (COND);
1298 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1299 XEXP (condexp, 1) = make_numeric_value (0);
1301 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1303 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1304 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1307 make_internal_attr (delay_type_str, condexp, ATTR_SPECIAL);
1310 /* For each delay possibility and delay slot, compute an eligibility
1311 attribute for non-annulled insns and for each type of annulled (annul
1312 if true and annul if false). */
1313 for (delay = delays; delay; delay = delay->next)
1315 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1317 condexp = XVECEXP (delay->def, 1, i);
1318 if (condexp == 0)
1319 condexp = false_rtx;
1320 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1321 make_numeric_value (1), make_numeric_value (0));
1323 p = attr_printf (sizeof "*delay__" + MAX_DIGITS * 2,
1324 "*delay_%d_%d", delay->num, i / 3);
1325 make_internal_attr (p, newexp, ATTR_SPECIAL);
1327 if (have_annul_true)
1329 condexp = XVECEXP (delay->def, 1, i + 1);
1330 if (condexp == 0) condexp = false_rtx;
1331 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1332 make_numeric_value (1),
1333 make_numeric_value (0));
1334 p = attr_printf (sizeof "*annul_true__" + MAX_DIGITS * 2,
1335 "*annul_true_%d_%d", delay->num, i / 3);
1336 make_internal_attr (p, newexp, ATTR_SPECIAL);
1339 if (have_annul_false)
1341 condexp = XVECEXP (delay->def, 1, i + 2);
1342 if (condexp == 0) condexp = false_rtx;
1343 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1344 make_numeric_value (1),
1345 make_numeric_value (0));
1346 p = attr_printf (sizeof "*annul_false__" + MAX_DIGITS * 2,
1347 "*annul_false_%d_%d", delay->num, i / 3);
1348 make_internal_attr (p, newexp, ATTR_SPECIAL);
1354 /* Once all attributes and insns have been read and checked, we construct for
1355 each attribute value a list of all the insns that have that value for
1356 the attribute. */
1358 static void
1359 fill_attr (struct attr_desc *attr)
1361 struct attr_value *av;
1362 struct insn_ent *ie;
1363 struct insn_def *id;
1364 int i;
1365 rtx value;
1367 /* Don't fill constant attributes. The value is independent of
1368 any particular insn. */
1369 if (attr->is_const)
1370 return;
1372 for (id = defs; id; id = id->next)
1374 /* If no value is specified for this insn for this attribute, use the
1375 default. */
1376 value = NULL;
1377 if (XVEC (id->def, id->vec_idx))
1378 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1379 if (! strcmp_check (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
1380 attr->name))
1381 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
1383 if (value == NULL)
1384 av = attr->default_val;
1385 else
1386 av = get_attr_value (id->loc, value, attr, id->insn_code);
1388 ie = oballoc (struct insn_ent);
1389 ie->def = id;
1390 insert_insn_ent (av, ie);
1394 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
1395 test that checks relative positions of insns (uses MATCH_DUP or PC).
1396 If so, replace it with what is obtained by passing the expression to
1397 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
1398 recursively on each value (including the default value). Otherwise,
1399 return the value returned by NO_ADDRESS_FN applied to EXP. */
1401 static rtx
1402 substitute_address (rtx exp, rtx (*no_address_fn) (rtx),
1403 rtx (*address_fn) (rtx))
1405 int i;
1406 rtx newexp;
1408 if (GET_CODE (exp) == COND)
1410 /* See if any tests use addresses. */
1411 address_used = 0;
1412 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1413 walk_attr_value (XVECEXP (exp, 0, i));
1415 if (address_used)
1416 return (*address_fn) (exp);
1418 /* Make a new copy of this COND, replacing each element. */
1419 newexp = rtx_alloc (COND);
1420 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
1421 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1423 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
1424 XVECEXP (newexp, 0, i + 1)
1425 = substitute_address (XVECEXP (exp, 0, i + 1),
1426 no_address_fn, address_fn);
1429 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
1430 no_address_fn, address_fn);
1432 return newexp;
1435 else if (GET_CODE (exp) == IF_THEN_ELSE)
1437 address_used = 0;
1438 walk_attr_value (XEXP (exp, 0));
1439 if (address_used)
1440 return (*address_fn) (exp);
1442 return attr_rtx (IF_THEN_ELSE,
1443 substitute_address (XEXP (exp, 0),
1444 no_address_fn, address_fn),
1445 substitute_address (XEXP (exp, 1),
1446 no_address_fn, address_fn),
1447 substitute_address (XEXP (exp, 2),
1448 no_address_fn, address_fn));
1451 return (*no_address_fn) (exp);
1454 /* Make new attributes from the `length' attribute. The following are made,
1455 each corresponding to a function called from `shorten_branches' or
1456 `get_attr_length':
1458 *insn_default_length This is the length of the insn to be returned
1459 by `get_attr_length' before `shorten_branches'
1460 has been called. In each case where the length
1461 depends on relative addresses, the largest
1462 possible is used. This routine is also used
1463 to compute the initial size of the insn.
1465 *insn_variable_length_p This returns 1 if the insn's length depends
1466 on relative addresses, zero otherwise.
1468 *insn_current_length This is only called when it is known that the
1469 insn has a variable length and returns the
1470 current length, based on relative addresses.
1473 static void
1474 make_length_attrs (void)
1476 static const char *new_names[] =
1478 "*insn_default_length",
1479 "*insn_min_length",
1480 "*insn_variable_length_p",
1481 "*insn_current_length"
1483 static rtx (*const no_address_fn[]) (rtx)
1484 = {identity_fn,identity_fn, zero_fn, zero_fn};
1485 static rtx (*const address_fn[]) (rtx)
1486 = {max_fn, min_fn, one_fn, identity_fn};
1487 size_t i;
1488 struct attr_desc *length_attr, *new_attr;
1489 struct attr_value *av, *new_av;
1490 struct insn_ent *ie, *new_ie;
1492 /* See if length attribute is defined. If so, it must be numeric. Make
1493 it special so we don't output anything for it. */
1494 length_attr = find_attr (&length_str, 0);
1495 if (length_attr == 0)
1496 return;
1498 if (! length_attr->is_numeric)
1499 fatal_at (length_attr->loc, "length attribute must be numeric");
1501 length_attr->is_const = 0;
1502 length_attr->is_special = 1;
1504 /* Make each new attribute, in turn. */
1505 for (i = 0; i < ARRAY_SIZE (new_names); i++)
1507 make_internal_attr (new_names[i],
1508 substitute_address (length_attr->default_val->value,
1509 no_address_fn[i], address_fn[i]),
1510 ATTR_NONE);
1511 new_attr = find_attr (&new_names[i], 0);
1512 for (av = length_attr->first_value; av; av = av->next)
1513 for (ie = av->first_insn; ie; ie = ie->next)
1515 new_av = get_attr_value (ie->def->loc,
1516 substitute_address (av->value,
1517 no_address_fn[i],
1518 address_fn[i]),
1519 new_attr, ie->def->insn_code);
1520 new_ie = oballoc (struct insn_ent);
1521 new_ie->def = ie->def;
1522 insert_insn_ent (new_av, new_ie);
1527 /* Utility functions called from above routine. */
1529 static rtx
1530 identity_fn (rtx exp)
1532 return exp;
1535 static rtx
1536 zero_fn (rtx exp ATTRIBUTE_UNUSED)
1538 return make_numeric_value (0);
1541 static rtx
1542 one_fn (rtx exp ATTRIBUTE_UNUSED)
1544 return make_numeric_value (1);
1547 static rtx
1548 max_fn (rtx exp)
1550 int unknown;
1551 return make_numeric_value (max_attr_value (exp, &unknown));
1554 static rtx
1555 min_fn (rtx exp)
1557 int unknown;
1558 return make_numeric_value (min_attr_value (exp, &unknown));
1561 static void
1562 write_length_unit_log (FILE *outf)
1564 struct attr_desc *length_attr = find_attr (&length_str, 0);
1565 struct attr_value *av;
1566 struct insn_ent *ie;
1567 unsigned int length_unit_log, length_or;
1568 int unknown = 0;
1570 if (length_attr)
1572 length_or = or_attr_value (length_attr->default_val->value, &unknown);
1573 for (av = length_attr->first_value; av; av = av->next)
1574 for (ie = av->first_insn; ie; ie = ie->next)
1575 length_or |= or_attr_value (av->value, &unknown);
1578 if (length_attr == NULL || unknown)
1579 length_unit_log = 0;
1580 else
1582 length_or = ~length_or;
1583 for (length_unit_log = 0; length_or & 1; length_or >>= 1)
1584 length_unit_log++;
1586 fprintf (outf, "EXPORTED_CONST int length_unit_log = %u;\n", length_unit_log);
1589 /* Compute approximate cost of the expression. Used to decide whether
1590 expression is cheap enough for inline. */
1591 static int
1592 attr_rtx_cost (rtx x)
1594 int cost = 1;
1595 enum rtx_code code;
1596 if (!x)
1597 return 0;
1598 code = GET_CODE (x);
1599 switch (code)
1601 case MATCH_OPERAND:
1602 if (XSTR (x, 1)[0])
1603 return 10;
1604 else
1605 return 1;
1607 case EQ_ATTR_ALT:
1608 return 1;
1610 case EQ_ATTR:
1611 /* Alternatives don't result into function call. */
1612 if (!strcmp_check (XSTR (x, 0), alternative_name))
1613 return 1;
1614 else
1615 return 5;
1616 default:
1618 int i, j;
1619 const char *fmt = GET_RTX_FORMAT (code);
1620 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1622 switch (fmt[i])
1624 case 'V':
1625 case 'E':
1626 for (j = 0; j < XVECLEN (x, i); j++)
1627 cost += attr_rtx_cost (XVECEXP (x, i, j));
1628 break;
1629 case 'e':
1630 cost += attr_rtx_cost (XEXP (x, i));
1631 break;
1635 break;
1637 return cost;
1640 /* Take a COND expression and see if any of the conditions in it can be
1641 simplified. If any are known true or known false for the particular insn
1642 code, the COND can be further simplified.
1644 Also call ourselves on any COND operations that are values of this COND.
1646 We do not modify EXP; rather, we make and return a new rtx. */
1648 static rtx
1649 simplify_cond (rtx exp, int insn_code, int insn_index)
1651 int i, j;
1652 /* We store the desired contents here,
1653 then build a new expression if they don't match EXP. */
1654 rtx defval = XEXP (exp, 1);
1655 rtx new_defval = XEXP (exp, 1);
1656 int len = XVECLEN (exp, 0);
1657 rtx *tests = XNEWVEC (rtx, len);
1658 int allsame = 1;
1659 rtx ret;
1661 /* This lets us free all storage allocated below, if appropriate. */
1662 obstack_finish (rtl_obstack);
1664 memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
1666 /* See if default value needs simplification. */
1667 if (GET_CODE (defval) == COND)
1668 new_defval = simplify_cond (defval, insn_code, insn_index);
1670 /* Simplify the subexpressions, and see what tests we can get rid of. */
1672 for (i = 0; i < len; i += 2)
1674 rtx newtest, newval;
1676 /* Simplify this test. */
1677 newtest = simplify_test_exp_in_temp (tests[i], insn_code, insn_index);
1678 tests[i] = newtest;
1680 newval = tests[i + 1];
1681 /* See if this value may need simplification. */
1682 if (GET_CODE (newval) == COND)
1683 newval = simplify_cond (newval, insn_code, insn_index);
1685 /* Look for ways to delete or combine this test. */
1686 if (newtest == true_rtx)
1688 /* If test is true, make this value the default
1689 and discard this + any following tests. */
1690 len = i;
1691 defval = tests[i + 1];
1692 new_defval = newval;
1695 else if (newtest == false_rtx)
1697 /* If test is false, discard it and its value. */
1698 for (j = i; j < len - 2; j++)
1699 tests[j] = tests[j + 2];
1700 i -= 2;
1701 len -= 2;
1704 else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
1706 /* If this value and the value for the prev test are the same,
1707 merge the tests. */
1709 tests[i - 2]
1710 = insert_right_side (IOR, tests[i - 2], newtest,
1711 insn_code, insn_index);
1713 /* Delete this test/value. */
1714 for (j = i; j < len - 2; j++)
1715 tests[j] = tests[j + 2];
1716 len -= 2;
1717 i -= 2;
1720 else
1721 tests[i + 1] = newval;
1724 /* If the last test in a COND has the same value
1725 as the default value, that test isn't needed. */
1727 while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
1728 len -= 2;
1730 /* See if we changed anything. */
1731 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
1732 allsame = 0;
1733 else
1734 for (i = 0; i < len; i++)
1735 if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
1737 allsame = 0;
1738 break;
1741 if (len == 0)
1743 if (GET_CODE (defval) == COND)
1744 ret = simplify_cond (defval, insn_code, insn_index);
1745 else
1746 ret = defval;
1748 else if (allsame)
1749 ret = exp;
1750 else
1752 rtx newexp = rtx_alloc (COND);
1754 XVEC (newexp, 0) = rtvec_alloc (len);
1755 memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
1756 XEXP (newexp, 1) = new_defval;
1757 ret = newexp;
1759 free (tests);
1760 return ret;
1763 /* Remove an insn entry from an attribute value. */
1765 static void
1766 remove_insn_ent (struct attr_value *av, struct insn_ent *ie)
1768 struct insn_ent *previe;
1770 if (av->first_insn == ie)
1771 av->first_insn = ie->next;
1772 else
1774 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
1776 previe->next = ie->next;
1779 av->num_insns--;
1780 if (ie->def->insn_code == -1)
1781 av->has_asm_insn = 0;
1783 num_insn_ents--;
1786 /* Insert an insn entry in an attribute value list. */
1788 static void
1789 insert_insn_ent (struct attr_value *av, struct insn_ent *ie)
1791 ie->next = av->first_insn;
1792 av->first_insn = ie;
1793 av->num_insns++;
1794 if (ie->def->insn_code == -1)
1795 av->has_asm_insn = 1;
1797 num_insn_ents++;
1800 /* This is a utility routine to take an expression that is a tree of either
1801 AND or IOR expressions and insert a new term. The new term will be
1802 inserted at the right side of the first node whose code does not match
1803 the root. A new node will be created with the root's code. Its left
1804 side will be the old right side and its right side will be the new
1805 term.
1807 If the `term' is itself a tree, all its leaves will be inserted. */
1809 static rtx
1810 insert_right_side (enum rtx_code code, rtx exp, rtx term, int insn_code, int insn_index)
1812 rtx newexp;
1814 /* Avoid consing in some special cases. */
1815 if (code == AND && term == true_rtx)
1816 return exp;
1817 if (code == AND && term == false_rtx)
1818 return false_rtx;
1819 if (code == AND && exp == true_rtx)
1820 return term;
1821 if (code == AND && exp == false_rtx)
1822 return false_rtx;
1823 if (code == IOR && term == true_rtx)
1824 return true_rtx;
1825 if (code == IOR && term == false_rtx)
1826 return exp;
1827 if (code == IOR && exp == true_rtx)
1828 return true_rtx;
1829 if (code == IOR && exp == false_rtx)
1830 return term;
1831 if (attr_equal_p (exp, term))
1832 return exp;
1834 if (GET_CODE (term) == code)
1836 exp = insert_right_side (code, exp, XEXP (term, 0),
1837 insn_code, insn_index);
1838 exp = insert_right_side (code, exp, XEXP (term, 1),
1839 insn_code, insn_index);
1841 return exp;
1844 if (GET_CODE (exp) == code)
1846 rtx new_rtx = insert_right_side (code, XEXP (exp, 1),
1847 term, insn_code, insn_index);
1848 if (new_rtx != XEXP (exp, 1))
1849 /* Make a copy of this expression and call recursively. */
1850 newexp = attr_rtx (code, XEXP (exp, 0), new_rtx);
1851 else
1852 newexp = exp;
1854 else
1856 /* Insert the new term. */
1857 newexp = attr_rtx (code, exp, term);
1860 return simplify_test_exp_in_temp (newexp, insn_code, insn_index);
1863 /* If we have an expression which AND's a bunch of
1864 (not (eq_attrq "alternative" "n"))
1865 terms, we may have covered all or all but one of the possible alternatives.
1866 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
1868 This routine is passed an expression and either AND or IOR. It returns a
1869 bitmask indicating which alternatives are mentioned within EXP. */
1871 static uint64_t
1872 compute_alternative_mask (rtx exp, enum rtx_code code)
1874 const char *string;
1875 if (GET_CODE (exp) == code)
1876 return compute_alternative_mask (XEXP (exp, 0), code)
1877 | compute_alternative_mask (XEXP (exp, 1), code);
1879 else if (code == AND && GET_CODE (exp) == NOT
1880 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
1881 && XSTR (XEXP (exp, 0), 0) == alternative_name)
1882 string = XSTR (XEXP (exp, 0), 1);
1884 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
1885 && XSTR (exp, 0) == alternative_name)
1886 string = XSTR (exp, 1);
1888 else if (GET_CODE (exp) == EQ_ATTR_ALT)
1890 if (code == AND && XINT (exp, 1))
1891 return XINT (exp, 0);
1893 if (code == IOR && !XINT (exp, 1))
1894 return XINT (exp, 0);
1896 return 0;
1898 else
1899 return 0;
1901 if (string[1] == 0)
1902 return ((uint64_t) 1) << (string[0] - '0');
1903 return ((uint64_t) 1) << atoi (string);
1906 /* Given I, a single-bit mask, return RTX to compare the `alternative'
1907 attribute with the value represented by that bit. */
1909 static rtx
1910 make_alternative_compare (uint64_t mask)
1912 return mk_attr_alt (mask);
1915 /* If we are processing an (eq_attr "attr" "value") test, we find the value
1916 of "attr" for this insn code. From that value, we can compute a test
1917 showing when the EQ_ATTR will be true. This routine performs that
1918 computation. If a test condition involves an address, we leave the EQ_ATTR
1919 intact because addresses are only valid for the `length' attribute.
1921 EXP is the EQ_ATTR expression and ATTR is the attribute to which
1922 it refers. VALUE is the value of that attribute for the insn
1923 corresponding to INSN_CODE and INSN_INDEX. */
1925 static rtx
1926 evaluate_eq_attr (rtx exp, struct attr_desc *attr, rtx value,
1927 int insn_code, int insn_index)
1929 rtx orexp, andexp;
1930 rtx right;
1931 rtx newexp;
1932 int i;
1934 while (GET_CODE (value) == ATTR)
1936 struct attr_value *av = NULL;
1938 attr = find_attr (&XSTR (value, 0), 0);
1940 if (insn_code_values)
1942 struct attr_value_list *iv;
1943 for (iv = insn_code_values[insn_code]; iv; iv = iv->next)
1944 if (iv->attr == attr)
1946 av = iv->av;
1947 break;
1950 else
1952 struct insn_ent *ie;
1953 for (av = attr->first_value; av; av = av->next)
1954 for (ie = av->first_insn; ie; ie = ie->next)
1955 if (ie->def->insn_code == insn_code)
1956 goto got_av;
1958 if (av)
1960 got_av:
1961 value = av->value;
1965 switch (GET_CODE (value))
1967 case CONST_STRING:
1968 if (! strcmp_check (XSTR (value, 0), XSTR (exp, 1)))
1969 newexp = true_rtx;
1970 else
1971 newexp = false_rtx;
1972 break;
1974 case SYMBOL_REF:
1976 const char *prefix;
1977 char *string, *p;
1979 gcc_assert (GET_CODE (exp) == EQ_ATTR);
1980 prefix = attr->enum_name ? attr->enum_name : attr->name;
1981 string = ACONCAT ((prefix, "_", XSTR (exp, 1), NULL));
1982 for (p = string; *p; p++)
1983 *p = TOUPPER (*p);
1985 newexp = attr_rtx (EQ, value,
1986 attr_rtx (SYMBOL_REF,
1987 DEF_ATTR_STRING (string)));
1988 break;
1991 case COND:
1992 /* We construct an IOR of all the cases for which the
1993 requested attribute value is present. Since we start with
1994 FALSE, if it is not present, FALSE will be returned.
1996 Each case is the AND of the NOT's of the previous conditions with the
1997 current condition; in the default case the current condition is TRUE.
1999 For each possible COND value, call ourselves recursively.
2001 The extra TRUE and FALSE expressions will be eliminated by another
2002 call to the simplification routine. */
2004 orexp = false_rtx;
2005 andexp = true_rtx;
2007 for (i = 0; i < XVECLEN (value, 0); i += 2)
2009 rtx this_cond = simplify_test_exp_in_temp (XVECEXP (value, 0, i),
2010 insn_code, insn_index);
2012 right = insert_right_side (AND, andexp, this_cond,
2013 insn_code, insn_index);
2014 right = insert_right_side (AND, right,
2015 evaluate_eq_attr (exp, attr,
2016 XVECEXP (value, 0,
2017 i + 1),
2018 insn_code, insn_index),
2019 insn_code, insn_index);
2020 orexp = insert_right_side (IOR, orexp, right,
2021 insn_code, insn_index);
2023 /* Add this condition into the AND expression. */
2024 newexp = attr_rtx (NOT, this_cond);
2025 andexp = insert_right_side (AND, andexp, newexp,
2026 insn_code, insn_index);
2029 /* Handle the default case. */
2030 right = insert_right_side (AND, andexp,
2031 evaluate_eq_attr (exp, attr, XEXP (value, 1),
2032 insn_code, insn_index),
2033 insn_code, insn_index);
2034 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2035 break;
2037 default:
2038 gcc_unreachable ();
2041 /* If uses an address, must return original expression. But set the
2042 ATTR_IND_SIMPLIFIED_P bit so we don't try to simplify it again. */
2044 address_used = 0;
2045 walk_attr_value (newexp);
2047 if (address_used)
2049 if (! ATTR_IND_SIMPLIFIED_P (exp))
2050 return copy_rtx_unchanging (exp);
2051 return exp;
2053 else
2054 return newexp;
2057 /* This routine is called when an AND of a term with a tree of AND's is
2058 encountered. If the term or its complement is present in the tree, it
2059 can be replaced with TRUE or FALSE, respectively.
2061 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2062 be true and hence are complementary.
2064 There is one special case: If we see
2065 (and (not (eq_attr "att" "v1"))
2066 (eq_attr "att" "v2"))
2067 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2068 replace the term, not anything in the AND tree. So we pass a pointer to
2069 the term. */
2071 static rtx
2072 simplify_and_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2074 rtx left, right;
2075 rtx newexp;
2076 rtx temp;
2077 int left_eliminates_term, right_eliminates_term;
2079 if (GET_CODE (exp) == AND)
2081 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2082 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2083 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2085 newexp = attr_rtx (AND, left, right);
2087 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2091 else if (GET_CODE (exp) == IOR)
2093 /* For the IOR case, we do the same as above, except that we can
2094 only eliminate `term' if both sides of the IOR would do so. */
2095 temp = *pterm;
2096 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2097 left_eliminates_term = (temp == true_rtx);
2099 temp = *pterm;
2100 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2101 right_eliminates_term = (temp == true_rtx);
2103 if (left_eliminates_term && right_eliminates_term)
2104 *pterm = true_rtx;
2106 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2108 newexp = attr_rtx (IOR, left, right);
2110 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2114 /* Check for simplifications. Do some extra checking here since this
2115 routine is called so many times. */
2117 if (exp == *pterm)
2118 return true_rtx;
2120 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2121 return false_rtx;
2123 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2124 return false_rtx;
2126 else if (GET_CODE (exp) == EQ_ATTR_ALT && GET_CODE (*pterm) == EQ_ATTR_ALT)
2128 if (attr_alt_subset_p (*pterm, exp))
2129 return true_rtx;
2131 if (attr_alt_subset_of_compl_p (*pterm, exp))
2132 return false_rtx;
2134 if (attr_alt_subset_p (exp, *pterm))
2135 *pterm = true_rtx;
2137 return exp;
2140 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2142 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2143 return exp;
2145 if (! strcmp_check (XSTR (exp, 1), XSTR (*pterm, 1)))
2146 return true_rtx;
2147 else
2148 return false_rtx;
2151 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2152 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2154 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2155 return exp;
2157 if (! strcmp_check (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2158 return false_rtx;
2159 else
2160 return true_rtx;
2163 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2164 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2166 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2167 return exp;
2169 if (! strcmp_check (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2170 return false_rtx;
2171 else
2172 *pterm = true_rtx;
2175 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2177 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2178 return true_rtx;
2181 else if (GET_CODE (exp) == NOT)
2183 if (attr_equal_p (XEXP (exp, 0), *pterm))
2184 return false_rtx;
2187 else if (GET_CODE (*pterm) == NOT)
2189 if (attr_equal_p (XEXP (*pterm, 0), exp))
2190 return false_rtx;
2193 else if (attr_equal_p (exp, *pterm))
2194 return true_rtx;
2196 return exp;
2199 /* Similar to `simplify_and_tree', but for IOR trees. */
2201 static rtx
2202 simplify_or_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2204 rtx left, right;
2205 rtx newexp;
2206 rtx temp;
2207 int left_eliminates_term, right_eliminates_term;
2209 if (GET_CODE (exp) == IOR)
2211 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2212 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2213 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2215 newexp = attr_rtx (GET_CODE (exp), left, right);
2217 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2221 else if (GET_CODE (exp) == AND)
2223 /* For the AND case, we do the same as above, except that we can
2224 only eliminate `term' if both sides of the AND would do so. */
2225 temp = *pterm;
2226 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2227 left_eliminates_term = (temp == false_rtx);
2229 temp = *pterm;
2230 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2231 right_eliminates_term = (temp == false_rtx);
2233 if (left_eliminates_term && right_eliminates_term)
2234 *pterm = false_rtx;
2236 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2238 newexp = attr_rtx (GET_CODE (exp), left, right);
2240 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2244 if (attr_equal_p (exp, *pterm))
2245 return false_rtx;
2247 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
2248 return true_rtx;
2250 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
2251 return true_rtx;
2253 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2254 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2255 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
2256 *pterm = false_rtx;
2258 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2259 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
2260 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
2261 return false_rtx;
2263 return exp;
2266 /* Simplify test expression and use temporary obstack in order to avoid
2267 memory bloat. Use ATTR_IND_SIMPLIFIED to avoid unnecessary simplifications
2268 and avoid unnecessary copying if possible. */
2270 static rtx
2271 simplify_test_exp_in_temp (rtx exp, int insn_code, int insn_index)
2273 rtx x;
2274 struct obstack *old;
2275 if (ATTR_IND_SIMPLIFIED_P (exp))
2276 return exp;
2277 old = rtl_obstack;
2278 rtl_obstack = temp_obstack;
2279 x = simplify_test_exp (exp, insn_code, insn_index);
2280 rtl_obstack = old;
2281 return x;
2284 /* Returns true if S1 is a subset of S2. */
2286 static bool
2287 attr_alt_subset_p (rtx s1, rtx s2)
2289 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2291 case (0 << 1) | 0:
2292 return !(XINT (s1, 0) &~ XINT (s2, 0));
2294 case (0 << 1) | 1:
2295 return !(XINT (s1, 0) & XINT (s2, 0));
2297 case (1 << 1) | 0:
2298 return false;
2300 case (1 << 1) | 1:
2301 return !(XINT (s2, 0) &~ XINT (s1, 0));
2303 default:
2304 gcc_unreachable ();
2308 /* Returns true if S1 is a subset of complement of S2. */
2310 static bool
2311 attr_alt_subset_of_compl_p (rtx s1, rtx s2)
2313 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2315 case (0 << 1) | 0:
2316 return !(XINT (s1, 0) & XINT (s2, 0));
2318 case (0 << 1) | 1:
2319 return !(XINT (s1, 0) & ~XINT (s2, 0));
2321 case (1 << 1) | 0:
2322 return !(XINT (s2, 0) &~ XINT (s1, 0));
2324 case (1 << 1) | 1:
2325 return false;
2327 default:
2328 gcc_unreachable ();
2332 /* Return EQ_ATTR_ALT expression representing intersection of S1 and S2. */
2334 static rtx
2335 attr_alt_intersection (rtx s1, rtx s2)
2337 int result;
2339 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2341 case (0 << 1) | 0:
2342 result = XINT (s1, 0) & XINT (s2, 0);
2343 break;
2344 case (0 << 1) | 1:
2345 result = XINT (s1, 0) & ~XINT (s2, 0);
2346 break;
2347 case (1 << 1) | 0:
2348 result = XINT (s2, 0) & ~XINT (s1, 0);
2349 break;
2350 case (1 << 1) | 1:
2351 result = XINT (s1, 0) | XINT (s2, 0);
2352 break;
2353 default:
2354 gcc_unreachable ();
2357 return attr_rtx (EQ_ATTR_ALT, result, XINT (s1, 1) & XINT (s2, 1));
2360 /* Return EQ_ATTR_ALT expression representing union of S1 and S2. */
2362 static rtx
2363 attr_alt_union (rtx s1, rtx s2)
2365 int result;
2367 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2369 case (0 << 1) | 0:
2370 result = XINT (s1, 0) | XINT (s2, 0);
2371 break;
2372 case (0 << 1) | 1:
2373 result = XINT (s2, 0) & ~XINT (s1, 0);
2374 break;
2375 case (1 << 1) | 0:
2376 result = XINT (s1, 0) & ~XINT (s2, 0);
2377 break;
2378 case (1 << 1) | 1:
2379 result = XINT (s1, 0) & XINT (s2, 0);
2380 break;
2381 default:
2382 gcc_unreachable ();
2385 return attr_rtx (EQ_ATTR_ALT, result, XINT (s1, 1) | XINT (s2, 1));
2388 /* Return EQ_ATTR_ALT expression representing complement of S. */
2390 static rtx
2391 attr_alt_complement (rtx s)
2393 return attr_rtx (EQ_ATTR_ALT, XINT (s, 0), 1 - XINT (s, 1));
2396 /* Return EQ_ATTR_ALT expression representing set containing elements set
2397 in E. */
2399 static rtx
2400 mk_attr_alt (uint64_t e)
2402 return attr_rtx (EQ_ATTR_ALT, (int)e, 0);
2405 /* Given an expression, see if it can be simplified for a particular insn
2406 code based on the values of other attributes being tested. This can
2407 eliminate nested get_attr_... calls.
2409 Note that if an endless recursion is specified in the patterns, the
2410 optimization will loop. However, it will do so in precisely the cases where
2411 an infinite recursion loop could occur during compilation. It's better that
2412 it occurs here! */
2414 static rtx
2415 simplify_test_exp (rtx exp, int insn_code, int insn_index)
2417 rtx left, right;
2418 struct attr_desc *attr;
2419 struct attr_value *av;
2420 struct insn_ent *ie;
2421 struct attr_value_list *iv;
2422 uint64_t i;
2423 rtx newexp = exp;
2424 bool left_alt, right_alt;
2426 /* Don't re-simplify something we already simplified. */
2427 if (ATTR_IND_SIMPLIFIED_P (exp) || ATTR_CURR_SIMPLIFIED_P (exp))
2428 return exp;
2430 switch (GET_CODE (exp))
2432 case AND:
2433 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2434 if (left == false_rtx)
2435 return false_rtx;
2436 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2437 if (right == false_rtx)
2438 return false_rtx;
2440 if (GET_CODE (left) == EQ_ATTR_ALT
2441 && GET_CODE (right) == EQ_ATTR_ALT)
2443 exp = attr_alt_intersection (left, right);
2444 return simplify_test_exp (exp, insn_code, insn_index);
2447 /* If either side is an IOR and we have (eq_attr "alternative" ..")
2448 present on both sides, apply the distributive law since this will
2449 yield simplifications. */
2450 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
2451 && compute_alternative_mask (left, IOR)
2452 && compute_alternative_mask (right, IOR))
2454 if (GET_CODE (left) == IOR)
2455 std::swap (left, right);
2457 newexp = attr_rtx (IOR,
2458 attr_rtx (AND, left, XEXP (right, 0)),
2459 attr_rtx (AND, left, XEXP (right, 1)));
2461 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2464 /* Try with the term on both sides. */
2465 right = simplify_and_tree (right, &left, insn_code, insn_index);
2466 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2467 left = simplify_and_tree (left, &right, insn_code, insn_index);
2469 if (left == false_rtx || right == false_rtx)
2470 return false_rtx;
2471 else if (left == true_rtx)
2473 return right;
2475 else if (right == true_rtx)
2477 return left;
2479 /* See if all or all but one of the insn's alternatives are specified
2480 in this tree. Optimize if so. */
2482 if (GET_CODE (left) == NOT)
2483 left_alt = (GET_CODE (XEXP (left, 0)) == EQ_ATTR
2484 && XSTR (XEXP (left, 0), 0) == alternative_name);
2485 else
2486 left_alt = (GET_CODE (left) == EQ_ATTR_ALT
2487 && XINT (left, 1));
2489 if (GET_CODE (right) == NOT)
2490 right_alt = (GET_CODE (XEXP (right, 0)) == EQ_ATTR
2491 && XSTR (XEXP (right, 0), 0) == alternative_name);
2492 else
2493 right_alt = (GET_CODE (right) == EQ_ATTR_ALT
2494 && XINT (right, 1));
2496 if (insn_code >= 0
2497 && (GET_CODE (left) == AND
2498 || left_alt
2499 || GET_CODE (right) == AND
2500 || right_alt))
2502 i = compute_alternative_mask (exp, AND);
2503 if (i & ~insn_alternatives[insn_code])
2504 fatal ("invalid alternative specified for pattern number %d",
2505 insn_index);
2507 /* If all alternatives are excluded, this is false. */
2508 i ^= insn_alternatives[insn_code];
2509 if (i == 0)
2510 return false_rtx;
2511 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2513 /* If just one excluded, AND a comparison with that one to the
2514 front of the tree. The others will be eliminated by
2515 optimization. We do not want to do this if the insn has one
2516 alternative and we have tested none of them! */
2517 left = make_alternative_compare (i);
2518 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2519 newexp = attr_rtx (AND, left, right);
2521 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2525 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2527 newexp = attr_rtx (AND, left, right);
2528 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2530 break;
2532 case IOR:
2533 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2534 if (left == true_rtx)
2535 return true_rtx;
2536 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2537 if (right == true_rtx)
2538 return true_rtx;
2540 if (GET_CODE (left) == EQ_ATTR_ALT
2541 && GET_CODE (right) == EQ_ATTR_ALT)
2543 exp = attr_alt_union (left, right);
2544 return simplify_test_exp (exp, insn_code, insn_index);
2547 right = simplify_or_tree (right, &left, insn_code, insn_index);
2548 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2549 left = simplify_or_tree (left, &right, insn_code, insn_index);
2551 if (right == true_rtx || left == true_rtx)
2552 return true_rtx;
2553 else if (left == false_rtx)
2555 return right;
2557 else if (right == false_rtx)
2559 return left;
2562 /* Test for simple cases where the distributive law is useful. I.e.,
2563 convert (ior (and (x) (y))
2564 (and (x) (z)))
2565 to (and (x)
2566 (ior (y) (z)))
2569 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
2570 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
2572 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
2574 left = XEXP (left, 0);
2575 right = newexp;
2576 newexp = attr_rtx (AND, left, right);
2577 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2580 /* Similarly,
2581 convert (ior (and (y) (x))
2582 (and (z) (x)))
2583 to (and (ior (y) (z))
2584 (x))
2585 Note that we want the common term to stay at the end.
2588 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
2589 && attr_equal_p (XEXP (left, 1), XEXP (right, 1)))
2591 newexp = attr_rtx (IOR, XEXP (left, 0), XEXP (right, 0));
2593 left = newexp;
2594 right = XEXP (right, 1);
2595 newexp = attr_rtx (AND, left, right);
2596 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2599 /* See if all or all but one of the insn's alternatives are specified
2600 in this tree. Optimize if so. */
2602 else if (insn_code >= 0
2603 && (GET_CODE (left) == IOR
2604 || (GET_CODE (left) == EQ_ATTR_ALT
2605 && !XINT (left, 1))
2606 || (GET_CODE (left) == EQ_ATTR
2607 && XSTR (left, 0) == alternative_name)
2608 || GET_CODE (right) == IOR
2609 || (GET_CODE (right) == EQ_ATTR_ALT
2610 && !XINT (right, 1))
2611 || (GET_CODE (right) == EQ_ATTR
2612 && XSTR (right, 0) == alternative_name)))
2614 i = compute_alternative_mask (exp, IOR);
2615 if (i & ~insn_alternatives[insn_code])
2616 fatal ("invalid alternative specified for pattern number %d",
2617 insn_index);
2619 /* If all alternatives are included, this is true. */
2620 i ^= insn_alternatives[insn_code];
2621 if (i == 0)
2622 return true_rtx;
2623 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2625 /* If just one excluded, IOR a comparison with that one to the
2626 front of the tree. The others will be eliminated by
2627 optimization. We do not want to do this if the insn has one
2628 alternative and we have tested none of them! */
2629 left = make_alternative_compare (i);
2630 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2631 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
2633 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2637 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2639 newexp = attr_rtx (IOR, left, right);
2640 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2642 break;
2644 case NOT:
2645 if (GET_CODE (XEXP (exp, 0)) == NOT)
2647 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
2648 insn_code, insn_index);
2649 return left;
2652 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2653 if (GET_CODE (left) == NOT)
2654 return XEXP (left, 0);
2656 if (left == false_rtx)
2657 return true_rtx;
2658 if (left == true_rtx)
2659 return false_rtx;
2661 if (GET_CODE (left) == EQ_ATTR_ALT)
2663 exp = attr_alt_complement (left);
2664 return simplify_test_exp (exp, insn_code, insn_index);
2667 /* Try to apply De`Morgan's laws. */
2668 if (GET_CODE (left) == IOR)
2670 newexp = attr_rtx (AND,
2671 attr_rtx (NOT, XEXP (left, 0)),
2672 attr_rtx (NOT, XEXP (left, 1)));
2674 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2676 else if (GET_CODE (left) == AND)
2678 newexp = attr_rtx (IOR,
2679 attr_rtx (NOT, XEXP (left, 0)),
2680 attr_rtx (NOT, XEXP (left, 1)));
2682 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2684 else if (left != XEXP (exp, 0))
2686 newexp = attr_rtx (NOT, left);
2688 break;
2690 case EQ_ATTR_ALT:
2691 if (!XINT (exp, 0))
2692 return XINT (exp, 1) ? true_rtx : false_rtx;
2693 break;
2695 case EQ_ATTR:
2696 if (XSTR (exp, 0) == alternative_name)
2698 newexp = mk_attr_alt (((uint64_t) 1) << atoi (XSTR (exp, 1)));
2699 break;
2702 /* Look at the value for this insn code in the specified attribute.
2703 We normally can replace this comparison with the condition that
2704 would give this insn the values being tested for. */
2705 if (insn_code >= 0
2706 && (attr = find_attr (&XSTR (exp, 0), 0)) != NULL)
2708 rtx x;
2710 av = NULL;
2711 if (insn_code_values)
2713 for (iv = insn_code_values[insn_code]; iv; iv = iv->next)
2714 if (iv->attr == attr)
2716 av = iv->av;
2717 break;
2720 else
2722 for (av = attr->first_value; av; av = av->next)
2723 for (ie = av->first_insn; ie; ie = ie->next)
2724 if (ie->def->insn_code == insn_code)
2725 goto got_av;
2728 if (av)
2730 got_av:
2731 x = evaluate_eq_attr (exp, attr, av->value,
2732 insn_code, insn_index);
2733 x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
2734 if (attr_rtx_cost (x) < 7)
2735 return x;
2738 break;
2740 default:
2741 break;
2744 /* We have already simplified this expression. Simplifying it again
2745 won't buy anything unless we weren't given a valid insn code
2746 to process (i.e., we are canonicalizing something.). */
2747 if (insn_code != -2
2748 && ! ATTR_IND_SIMPLIFIED_P (newexp))
2749 return copy_rtx_unchanging (newexp);
2751 return newexp;
2754 /* Return 1 if any EQ_ATTR subexpression of P refers to ATTR,
2755 otherwise return 0. */
2757 static int
2758 tests_attr_p (rtx p, struct attr_desc *attr)
2760 const char *fmt;
2761 int i, ie, j, je;
2763 if (GET_CODE (p) == EQ_ATTR)
2765 if (XSTR (p, 0) != attr->name)
2766 return 0;
2767 return 1;
2770 fmt = GET_RTX_FORMAT (GET_CODE (p));
2771 ie = GET_RTX_LENGTH (GET_CODE (p));
2772 for (i = 0; i < ie; i++)
2774 switch (*fmt++)
2776 case 'e':
2777 if (tests_attr_p (XEXP (p, i), attr))
2778 return 1;
2779 break;
2781 case 'E':
2782 je = XVECLEN (p, i);
2783 for (j = 0; j < je; ++j)
2784 if (tests_attr_p (XVECEXP (p, i, j), attr))
2785 return 1;
2786 break;
2790 return 0;
2793 /* Calculate a topological sorting of all attributes so that
2794 all attributes only depend on attributes in front of it.
2795 Place the result in *RET (which is a pointer to an array of
2796 attr_desc pointers), and return the size of that array. */
2798 static int
2799 get_attr_order (struct attr_desc ***ret)
2801 int i, j;
2802 int num = 0;
2803 struct attr_desc *attr;
2804 struct attr_desc **all, **sorted;
2805 char *handled;
2806 for (i = 0; i < MAX_ATTRS_INDEX; i++)
2807 for (attr = attrs[i]; attr; attr = attr->next)
2808 num++;
2809 all = XNEWVEC (struct attr_desc *, num);
2810 sorted = XNEWVEC (struct attr_desc *, num);
2811 handled = XCNEWVEC (char, num);
2812 num = 0;
2813 for (i = 0; i < MAX_ATTRS_INDEX; i++)
2814 for (attr = attrs[i]; attr; attr = attr->next)
2815 all[num++] = attr;
2817 j = 0;
2818 for (i = 0; i < num; i++)
2819 if (all[i]->is_const)
2820 handled[i] = 1, sorted[j++] = all[i];
2822 /* We have only few attributes hence we can live with the inner
2823 loop being O(n^2), unlike the normal fast variants of topological
2824 sorting. */
2825 while (j < num)
2827 for (i = 0; i < num; i++)
2828 if (!handled[i])
2830 /* Let's see if I depends on anything interesting. */
2831 int k;
2832 for (k = 0; k < num; k++)
2833 if (!handled[k])
2835 struct attr_value *av;
2836 for (av = all[i]->first_value; av; av = av->next)
2837 if (av->num_insns != 0)
2838 if (tests_attr_p (av->value, all[k]))
2839 break;
2841 if (av)
2842 /* Something in I depends on K. */
2843 break;
2845 if (k == num)
2847 /* Nothing in I depended on anything intersting, so
2848 it's done. */
2849 handled[i] = 1;
2850 sorted[j++] = all[i];
2855 if (DEBUG)
2856 for (j = 0; j < num; j++)
2858 struct attr_desc *attr2;
2859 struct attr_value *av;
2861 attr = sorted[j];
2862 fprintf (stderr, "%s depends on: ", attr->name);
2863 for (i = 0; i < MAX_ATTRS_INDEX; ++i)
2864 for (attr2 = attrs[i]; attr2; attr2 = attr2->next)
2865 if (!attr2->is_const)
2866 for (av = attr->first_value; av; av = av->next)
2867 if (av->num_insns != 0)
2868 if (tests_attr_p (av->value, attr2))
2870 fprintf (stderr, "%s, ", attr2->name);
2871 break;
2873 fprintf (stderr, "\n");
2876 free (all);
2877 *ret = sorted;
2878 return num;
2881 /* Optimize the attribute lists by seeing if we can determine conditional
2882 values from the known values of other attributes. This will save subroutine
2883 calls during the compilation. NUM_INSN_CODES is the number of unique
2884 instruction codes. */
2886 static void
2887 optimize_attrs (int num_insn_codes)
2889 struct attr_desc *attr;
2890 struct attr_value *av;
2891 struct insn_ent *ie;
2892 rtx newexp;
2893 int i;
2894 struct attr_value_list *ivbuf;
2895 struct attr_value_list *iv;
2896 struct attr_desc **topsort;
2897 int topnum;
2899 /* For each insn code, make a list of all the insn_ent's for it,
2900 for all values for all attributes. */
2902 if (num_insn_ents == 0)
2903 return;
2905 /* Make 2 extra elements, for "code" values -2 and -1. */
2906 insn_code_values = XCNEWVEC (struct attr_value_list *, num_insn_codes + 2);
2908 /* Offset the table address so we can index by -2 or -1. */
2909 insn_code_values += 2;
2911 iv = ivbuf = XNEWVEC (struct attr_value_list, num_insn_ents);
2913 /* Create the chain of insn*attr values such that we see dependend
2914 attributes after their dependencies. As we use a stack via the
2915 next pointers start from the end of the topological order. */
2916 topnum = get_attr_order (&topsort);
2917 for (i = topnum - 1; i >= 0; i--)
2918 for (av = topsort[i]->first_value; av; av = av->next)
2919 for (ie = av->first_insn; ie; ie = ie->next)
2921 iv->attr = topsort[i];
2922 iv->av = av;
2923 iv->ie = ie;
2924 iv->next = insn_code_values[ie->def->insn_code];
2925 insn_code_values[ie->def->insn_code] = iv;
2926 iv++;
2928 free (topsort);
2930 /* Sanity check on num_insn_ents. */
2931 gcc_assert (iv == ivbuf + num_insn_ents);
2933 /* Process one insn code at a time. */
2934 for (i = -2; i < num_insn_codes; i++)
2936 /* Clear the ATTR_CURR_SIMPLIFIED_P flag everywhere relevant.
2937 We use it to mean "already simplified for this insn". */
2938 for (iv = insn_code_values[i]; iv; iv = iv->next)
2939 clear_struct_flag (iv->av->value);
2941 for (iv = insn_code_values[i]; iv; iv = iv->next)
2943 struct obstack *old = rtl_obstack;
2945 attr = iv->attr;
2946 av = iv->av;
2947 ie = iv->ie;
2948 if (GET_CODE (av->value) != COND)
2949 continue;
2951 rtl_obstack = temp_obstack;
2952 newexp = av->value;
2953 while (GET_CODE (newexp) == COND)
2955 rtx newexp2 = simplify_cond (newexp, ie->def->insn_code,
2956 ie->def->insn_index);
2957 if (newexp2 == newexp)
2958 break;
2959 newexp = newexp2;
2962 rtl_obstack = old;
2963 /* If we created a new value for this instruction, and it's
2964 cheaper than the old value, and overall cheap, use that
2965 one as specific value for the current instruction.
2966 The last test is to avoid exploding the get_attr_ function
2967 sizes for no much gain. */
2968 if (newexp != av->value
2969 && attr_rtx_cost (newexp) < attr_rtx_cost (av->value)
2970 && attr_rtx_cost (newexp) < 26
2973 remove_insn_ent (av, ie);
2974 av = get_attr_value (ie->def->loc, newexp, attr,
2975 ie->def->insn_code);
2976 iv->av = av;
2977 insert_insn_ent (av, ie);
2982 free (ivbuf);
2983 free (insn_code_values - 2);
2984 insn_code_values = NULL;
2987 /* Clear the ATTR_CURR_SIMPLIFIED_P flag in EXP and its subexpressions. */
2989 static void
2990 clear_struct_flag (rtx x)
2992 int i;
2993 int j;
2994 enum rtx_code code;
2995 const char *fmt;
2997 ATTR_CURR_SIMPLIFIED_P (x) = 0;
2998 if (ATTR_IND_SIMPLIFIED_P (x))
2999 return;
3001 code = GET_CODE (x);
3003 switch (code)
3005 case REG:
3006 CASE_CONST_ANY:
3007 case MATCH_TEST:
3008 case SYMBOL_REF:
3009 case CODE_LABEL:
3010 case PC:
3011 case CC0:
3012 case EQ_ATTR:
3013 case ATTR_FLAG:
3014 return;
3016 default:
3017 break;
3020 /* Compare the elements. If any pair of corresponding elements
3021 fail to match, return 0 for the whole things. */
3023 fmt = GET_RTX_FORMAT (code);
3024 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3026 switch (fmt[i])
3028 case 'V':
3029 case 'E':
3030 for (j = 0; j < XVECLEN (x, i); j++)
3031 clear_struct_flag (XVECEXP (x, i, j));
3032 break;
3034 case 'e':
3035 clear_struct_flag (XEXP (x, i));
3036 break;
3041 /* Add attribute value NAME to the beginning of ATTR's list. */
3043 static void
3044 add_attr_value (struct attr_desc *attr, const char *name)
3046 struct attr_value *av;
3048 av = oballoc (struct attr_value);
3049 av->value = attr_rtx (CONST_STRING, name);
3050 av->next = attr->first_value;
3051 attr->first_value = av;
3052 av->first_insn = NULL;
3053 av->num_insns = 0;
3054 av->has_asm_insn = 0;
3057 /* Create table entries for DEFINE_ATTR or DEFINE_ENUM_ATTR. */
3059 static void
3060 gen_attr (md_rtx_info *info)
3062 struct enum_type *et;
3063 struct enum_value *ev;
3064 struct attr_desc *attr;
3065 const char *name_ptr;
3066 char *p;
3067 rtx def = info->def;
3069 /* Make a new attribute structure. Check for duplicate by looking at
3070 attr->default_val, since it is initialized by this routine. */
3071 attr = find_attr (&XSTR (def, 0), 1);
3072 if (attr->default_val)
3074 error_at (info->loc, "duplicate definition for attribute %s",
3075 attr->name);
3076 message_at (attr->loc, "previous definition");
3077 return;
3079 attr->loc = info->loc;
3081 if (GET_CODE (def) == DEFINE_ENUM_ATTR)
3083 attr->enum_name = XSTR (def, 1);
3084 et = rtx_reader_ptr->lookup_enum_type (XSTR (def, 1));
3085 if (!et || !et->md_p)
3086 error_at (info->loc, "No define_enum called `%s' defined",
3087 attr->name);
3088 if (et)
3089 for (ev = et->values; ev; ev = ev->next)
3090 add_attr_value (attr, ev->name);
3092 else if (*XSTR (def, 1) == '\0')
3093 attr->is_numeric = 1;
3094 else
3096 name_ptr = XSTR (def, 1);
3097 while ((p = next_comma_elt (&name_ptr)) != NULL)
3098 add_attr_value (attr, p);
3101 if (GET_CODE (XEXP (def, 2)) == CONST)
3103 attr->is_const = 1;
3104 if (attr->is_numeric)
3105 error_at (info->loc,
3106 "constant attributes may not take numeric values");
3108 /* Get rid of the CONST node. It is allowed only at top-level. */
3109 XEXP (def, 2) = XEXP (XEXP (def, 2), 0);
3112 if (! strcmp_check (attr->name, length_str) && ! attr->is_numeric)
3113 error_at (info->loc, "`length' attribute must take numeric values");
3115 /* Set up the default value. */
3116 XEXP (def, 2) = check_attr_value (info->loc, XEXP (def, 2), attr);
3117 attr->default_val = get_attr_value (info->loc, XEXP (def, 2), attr, -2);
3120 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
3121 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
3122 number of alternatives as this should be checked elsewhere. */
3124 static int
3125 count_alternatives (rtx exp)
3127 int i, j, n;
3128 const char *fmt;
3130 if (GET_CODE (exp) == MATCH_OPERAND)
3131 return n_comma_elts (XSTR (exp, 2));
3133 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3134 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3135 switch (*fmt++)
3137 case 'e':
3138 case 'u':
3139 n = count_alternatives (XEXP (exp, i));
3140 if (n)
3141 return n;
3142 break;
3144 case 'E':
3145 case 'V':
3146 if (XVEC (exp, i) != NULL)
3147 for (j = 0; j < XVECLEN (exp, i); j++)
3149 n = count_alternatives (XVECEXP (exp, i, j));
3150 if (n)
3151 return n;
3155 return 0;
3158 /* Returns nonzero if the given expression contains an EQ_ATTR with the
3159 `alternative' attribute. */
3161 static int
3162 compares_alternatives_p (rtx exp)
3164 int i, j;
3165 const char *fmt;
3167 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
3168 return 1;
3170 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3171 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3172 switch (*fmt++)
3174 case 'e':
3175 case 'u':
3176 if (compares_alternatives_p (XEXP (exp, i)))
3177 return 1;
3178 break;
3180 case 'E':
3181 for (j = 0; j < XVECLEN (exp, i); j++)
3182 if (compares_alternatives_p (XVECEXP (exp, i, j)))
3183 return 1;
3184 break;
3187 return 0;
3190 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
3192 static void
3193 gen_insn (md_rtx_info *info)
3195 struct insn_def *id;
3196 rtx def = info->def;
3198 id = oballoc (struct insn_def);
3199 id->next = defs;
3200 defs = id;
3201 id->def = def;
3202 id->loc = info->loc;
3204 switch (GET_CODE (def))
3206 case DEFINE_INSN:
3207 id->insn_code = info->index;
3208 id->insn_index = insn_index_number;
3209 id->num_alternatives = count_alternatives (def);
3210 if (id->num_alternatives == 0)
3211 id->num_alternatives = 1;
3212 id->vec_idx = 4;
3213 break;
3215 case DEFINE_PEEPHOLE:
3216 id->insn_code = info->index;
3217 id->insn_index = insn_index_number;
3218 id->num_alternatives = count_alternatives (def);
3219 if (id->num_alternatives == 0)
3220 id->num_alternatives = 1;
3221 id->vec_idx = 3;
3222 break;
3224 case DEFINE_ASM_ATTRIBUTES:
3225 id->insn_code = -1;
3226 id->insn_index = -1;
3227 id->num_alternatives = 1;
3228 id->vec_idx = 0;
3229 got_define_asm_attributes = 1;
3230 break;
3232 default:
3233 gcc_unreachable ();
3237 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
3238 true or annul false is specified, and make a `struct delay_desc'. */
3240 static void
3241 gen_delay (md_rtx_info *info)
3243 struct delay_desc *delay;
3244 int i;
3246 rtx def = info->def;
3247 if (XVECLEN (def, 1) % 3 != 0)
3249 error_at (info->loc, "number of elements in DEFINE_DELAY must"
3250 " be multiple of three");
3251 return;
3254 for (i = 0; i < XVECLEN (def, 1); i += 3)
3256 if (XVECEXP (def, 1, i + 1))
3257 have_annul_true = 1;
3258 if (XVECEXP (def, 1, i + 2))
3259 have_annul_false = 1;
3262 delay = oballoc (struct delay_desc);
3263 delay->def = def;
3264 delay->num = ++num_delays;
3265 delay->next = delays;
3266 delay->loc = info->loc;
3267 delays = delay;
3270 /* Names of attributes that could be possibly cached. */
3271 static const char *cached_attrs[32];
3272 /* Number of such attributes. */
3273 static int cached_attr_count;
3274 /* Bitmasks of possibly cached attributes. */
3275 static unsigned int attrs_seen_once, attrs_seen_more_than_once;
3276 static unsigned int attrs_to_cache;
3277 static unsigned int attrs_cached_inside, attrs_cached_after;
3279 /* Finds non-const attributes that could be possibly cached.
3280 When create is TRUE, fills in cached_attrs array.
3281 Computes ATTRS_SEEN_ONCE and ATTRS_SEEN_MORE_THAN_ONCE
3282 bitmasks. */
3284 static void
3285 find_attrs_to_cache (rtx exp, bool create)
3287 int i;
3288 const char *name;
3289 struct attr_desc *attr;
3291 if (exp == NULL)
3292 return;
3294 switch (GET_CODE (exp))
3296 case NOT:
3297 if (GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
3298 find_attrs_to_cache (XEXP (exp, 0), create);
3299 return;
3301 case EQ_ATTR:
3302 name = XSTR (exp, 0);
3303 if (name == alternative_name)
3304 return;
3305 for (i = 0; i < cached_attr_count; i++)
3306 if (name == cached_attrs[i])
3308 if ((attrs_seen_once & (1U << i)) != 0)
3309 attrs_seen_more_than_once |= (1U << i);
3310 else
3311 attrs_seen_once |= (1U << i);
3312 return;
3314 if (!create)
3315 return;
3316 attr = find_attr (&name, 0);
3317 gcc_assert (attr);
3318 if (attr->is_const)
3319 return;
3320 if (cached_attr_count == 32)
3321 return;
3322 cached_attrs[cached_attr_count] = XSTR (exp, 0);
3323 attrs_seen_once |= (1U << cached_attr_count);
3324 cached_attr_count++;
3325 return;
3327 case AND:
3328 case IOR:
3329 find_attrs_to_cache (XEXP (exp, 0), create);
3330 find_attrs_to_cache (XEXP (exp, 1), create);
3331 return;
3333 case COND:
3334 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3335 find_attrs_to_cache (XVECEXP (exp, 0, i), create);
3336 return;
3338 default:
3339 return;
3343 /* Given a piece of RTX, print a C expression to test its truth value to OUTF.
3344 We use AND and IOR both for logical and bit-wise operations, so
3345 interpret them as logical unless they are inside a comparison expression.
3347 An outermost pair of parentheses is emitted around this C expression unless
3348 EMIT_PARENS is false. */
3350 /* Interpret AND/IOR as bit-wise operations instead of logical. */
3351 #define FLG_BITWISE 1
3352 /* Set if cached attribute will be known initialized in else block after
3353 this condition. This is true for LHS of toplevel && and || and
3354 even for RHS of ||, but not for RHS of &&. */
3355 #define FLG_AFTER 2
3356 /* Set if cached attribute will be known initialized in then block after
3357 this condition. This is true for LHS of toplevel && and || and
3358 even for RHS of &&, but not for RHS of ||. */
3359 #define FLG_INSIDE 4
3360 /* Cleared when an operand of &&. */
3361 #define FLG_OUTSIDE_AND 8
3363 static unsigned int
3364 write_test_expr (FILE *outf, rtx exp, unsigned int attrs_cached, int flags,
3365 bool emit_parens = true)
3367 int comparison_operator = 0;
3368 RTX_CODE code;
3369 struct attr_desc *attr;
3371 if (emit_parens)
3372 fprintf (outf, "(");
3374 code = GET_CODE (exp);
3375 switch (code)
3377 /* Binary operators. */
3378 case GEU: case GTU:
3379 case LEU: case LTU:
3380 fprintf (outf, "(unsigned) ");
3381 /* Fall through. */
3383 case EQ: case NE:
3384 case GE: case GT:
3385 case LE: case LT:
3386 comparison_operator = FLG_BITWISE;
3387 /* FALLTHRU */
3389 case PLUS: case MINUS: case MULT: case DIV: case MOD:
3390 case AND: case IOR: case XOR:
3391 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3392 if ((code != AND && code != IOR) || (flags & FLG_BITWISE))
3394 flags &= ~(FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND);
3395 write_test_expr (outf, XEXP (exp, 0), attrs_cached,
3396 flags | comparison_operator);
3398 else
3400 if (code == AND)
3401 flags &= ~FLG_OUTSIDE_AND;
3402 if (GET_CODE (XEXP (exp, 0)) == code
3403 || GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3404 || (GET_CODE (XEXP (exp, 0)) == NOT
3405 && GET_CODE (XEXP (XEXP (exp, 0), 0)) == EQ_ATTR))
3406 attrs_cached
3407 = write_test_expr (outf, XEXP (exp, 0), attrs_cached, flags);
3408 else
3409 write_test_expr (outf, XEXP (exp, 0), attrs_cached, flags);
3411 switch (code)
3413 case EQ:
3414 fprintf (outf, " == ");
3415 break;
3416 case NE:
3417 fprintf (outf, " != ");
3418 break;
3419 case GE:
3420 fprintf (outf, " >= ");
3421 break;
3422 case GT:
3423 fprintf (outf, " > ");
3424 break;
3425 case GEU:
3426 fprintf (outf, " >= (unsigned) ");
3427 break;
3428 case GTU:
3429 fprintf (outf, " > (unsigned) ");
3430 break;
3431 case LE:
3432 fprintf (outf, " <= ");
3433 break;
3434 case LT:
3435 fprintf (outf, " < ");
3436 break;
3437 case LEU:
3438 fprintf (outf, " <= (unsigned) ");
3439 break;
3440 case LTU:
3441 fprintf (outf, " < (unsigned) ");
3442 break;
3443 case PLUS:
3444 fprintf (outf, " + ");
3445 break;
3446 case MINUS:
3447 fprintf (outf, " - ");
3448 break;
3449 case MULT:
3450 fprintf (outf, " * ");
3451 break;
3452 case DIV:
3453 fprintf (outf, " / ");
3454 break;
3455 case MOD:
3456 fprintf (outf, " %% ");
3457 break;
3458 case AND:
3459 if (flags & FLG_BITWISE)
3460 fprintf (outf, " & ");
3461 else
3462 fprintf (outf, " && ");
3463 break;
3464 case IOR:
3465 if (flags & FLG_BITWISE)
3466 fprintf (outf, " | ");
3467 else
3468 fprintf (outf, " || ");
3469 break;
3470 case XOR:
3471 fprintf (outf, " ^ ");
3472 break;
3473 case ASHIFT:
3474 fprintf (outf, " << ");
3475 break;
3476 case LSHIFTRT:
3477 case ASHIFTRT:
3478 fprintf (outf, " >> ");
3479 break;
3480 default:
3481 gcc_unreachable ();
3484 if (code == AND)
3486 /* For if (something && (cached_x = get_attr_x (insn)) == X)
3487 cached_x is only known to be initialized in then block. */
3488 flags &= ~FLG_AFTER;
3490 else if (code == IOR)
3492 if (flags & FLG_OUTSIDE_AND)
3493 /* For if (something || (cached_x = get_attr_x (insn)) == X)
3494 cached_x is only known to be initialized in else block
3495 and else if conditions. */
3496 flags &= ~FLG_INSIDE;
3497 else
3498 /* For if ((something || (cached_x = get_attr_x (insn)) == X)
3499 && something_else)
3500 cached_x is not know to be initialized anywhere. */
3501 flags &= ~(FLG_AFTER | FLG_INSIDE);
3503 if ((code == AND || code == IOR)
3504 && (GET_CODE (XEXP (exp, 1)) == code
3505 || GET_CODE (XEXP (exp, 1)) == EQ_ATTR
3506 || (GET_CODE (XEXP (exp, 1)) == NOT
3507 && GET_CODE (XEXP (XEXP (exp, 1), 0)) == EQ_ATTR)))
3509 bool need_parens = true;
3511 /* No need to emit parentheses around the right-hand operand if we are
3512 continuing a chain of && or || (or & or |). */
3513 if (GET_CODE (XEXP (exp, 1)) == code)
3514 need_parens = false;
3516 attrs_cached
3517 = write_test_expr (outf, XEXP (exp, 1), attrs_cached, flags,
3518 need_parens);
3520 else
3521 write_test_expr (outf, XEXP (exp, 1), attrs_cached,
3522 flags | comparison_operator);
3523 break;
3525 case NOT:
3526 /* Special-case (not (eq_attrq "alternative" "x")) */
3527 if (! (flags & FLG_BITWISE) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
3529 if (XSTR (XEXP (exp, 0), 0) == alternative_name)
3531 fprintf (outf, "which_alternative != %s",
3532 XSTR (XEXP (exp, 0), 1));
3533 break;
3536 fprintf (outf, "! ");
3537 attrs_cached =
3538 write_test_expr (outf, XEXP (exp, 0), attrs_cached, flags);
3539 break;
3542 /* Otherwise, fall through to normal unary operator. */
3543 gcc_fallthrough ();
3545 /* Unary operators. */
3546 case ABS: case NEG:
3547 switch (code)
3549 case NOT:
3550 if (flags & FLG_BITWISE)
3551 fprintf (outf, "~ ");
3552 else
3553 fprintf (outf, "! ");
3554 break;
3555 case ABS:
3556 fprintf (outf, "abs ");
3557 break;
3558 case NEG:
3559 fprintf (outf, "-");
3560 break;
3561 default:
3562 gcc_unreachable ();
3565 flags &= ~(FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND);
3566 write_test_expr (outf, XEXP (exp, 0), attrs_cached, flags);
3567 break;
3569 case EQ_ATTR_ALT:
3571 int set = XINT (exp, 0), bit = 0;
3573 if (flags & FLG_BITWISE)
3574 fatal ("EQ_ATTR_ALT not valid inside comparison");
3576 if (!set)
3577 fatal ("Empty EQ_ATTR_ALT should be optimized out");
3579 if (!(set & (set - 1)))
3581 if (!(set & 0xffff))
3583 bit += 16;
3584 set >>= 16;
3586 if (!(set & 0xff))
3588 bit += 8;
3589 set >>= 8;
3591 if (!(set & 0xf))
3593 bit += 4;
3594 set >>= 4;
3596 if (!(set & 0x3))
3598 bit += 2;
3599 set >>= 2;
3601 if (!(set & 1))
3602 bit++;
3604 fprintf (outf, "which_alternative %s= %d",
3605 XINT (exp, 1) ? "!" : "=", bit);
3607 else
3609 fprintf (outf, "%s((1 << which_alternative) & %#x)",
3610 XINT (exp, 1) ? "!" : "", set);
3613 break;
3615 /* Comparison test of an attribute with a value. Most of these will
3616 have been removed by optimization. Handle "alternative"
3617 specially and give error if EQ_ATTR present inside a comparison. */
3618 case EQ_ATTR:
3619 if (flags & FLG_BITWISE)
3620 fatal ("EQ_ATTR not valid inside comparison");
3622 if (XSTR (exp, 0) == alternative_name)
3624 fprintf (outf, "which_alternative == %s", XSTR (exp, 1));
3625 break;
3628 attr = find_attr (&XSTR (exp, 0), 0);
3629 gcc_assert (attr);
3631 /* Now is the time to expand the value of a constant attribute. */
3632 if (attr->is_const)
3634 write_test_expr (outf,
3635 evaluate_eq_attr (exp, attr,
3636 attr->default_val->value,
3637 -2, -2),
3638 attrs_cached, 0);
3640 else
3642 int i;
3643 for (i = 0; i < cached_attr_count; i++)
3644 if (attr->name == cached_attrs[i])
3645 break;
3646 if (i < cached_attr_count && (attrs_cached & (1U << i)) != 0)
3647 fprintf (outf, "cached_%s", attr->name);
3648 else if (i < cached_attr_count && (attrs_to_cache & (1U << i)) != 0)
3650 fprintf (outf, "(cached_%s = get_attr_%s (insn))",
3651 attr->name, attr->name);
3652 if (flags & FLG_AFTER)
3653 attrs_cached_after |= (1U << i);
3654 if (flags & FLG_INSIDE)
3655 attrs_cached_inside |= (1U << i);
3656 attrs_cached |= (1U << i);
3658 else
3659 fprintf (outf, "get_attr_%s (insn)", attr->name);
3660 fprintf (outf, " == ");
3661 write_attr_valueq (outf, attr, XSTR (exp, 1));
3663 break;
3665 /* Comparison test of flags for define_delays. */
3666 case ATTR_FLAG:
3667 if (flags & FLG_BITWISE)
3668 fatal ("ATTR_FLAG not valid inside comparison");
3669 fprintf (outf, "(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
3670 break;
3672 /* See if an operand matches a predicate. */
3673 case MATCH_OPERAND:
3674 /* If only a mode is given, just ensure the mode matches the operand.
3675 If neither a mode nor predicate is given, error. */
3676 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
3678 if (GET_MODE (exp) == VOIDmode)
3679 fatal ("null MATCH_OPERAND specified as test");
3680 else
3681 fprintf (outf, "GET_MODE (operands[%d]) == %smode",
3682 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3684 else
3685 fprintf (outf, "%s (operands[%d], %smode)",
3686 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3687 break;
3689 /* Constant integer. */
3690 case CONST_INT:
3691 fprintf (outf, HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
3692 break;
3694 case MATCH_TEST:
3695 rtx_reader_ptr->fprint_c_condition (outf, XSTR (exp, 0));
3696 if (flags & FLG_BITWISE)
3697 fprintf (outf, " != 0");
3698 break;
3700 /* A random C expression. */
3701 case SYMBOL_REF:
3702 rtx_reader_ptr->fprint_c_condition (outf, XSTR (exp, 0));
3703 break;
3705 /* The address of the branch target. */
3706 case MATCH_DUP:
3707 fprintf (outf,
3708 "INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
3709 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
3710 break;
3712 case PC:
3713 /* The address of the current insn. We implement this actually as the
3714 address of the current insn for backward branches, but the last
3715 address of the next insn for forward branches, and both with
3716 adjustments that account for the worst-case possible stretching of
3717 intervening alignments between this insn and its destination. */
3718 fprintf (outf, "insn_current_reference_address (insn)");
3719 break;
3721 case CONST_STRING:
3722 fprintf (outf, "%s", XSTR (exp, 0));
3723 break;
3725 case IF_THEN_ELSE:
3726 write_test_expr (outf, XEXP (exp, 0), attrs_cached, 0);
3727 fprintf (outf, " ? ");
3728 write_test_expr (outf, XEXP (exp, 1), attrs_cached, FLG_BITWISE);
3729 fprintf (outf, " : ");
3730 write_test_expr (outf, XEXP (exp, 2), attrs_cached, FLG_BITWISE);
3731 break;
3733 default:
3734 fatal ("bad RTX code `%s' in attribute calculation\n",
3735 GET_RTX_NAME (code));
3738 if (emit_parens)
3739 fprintf (outf, ")");
3741 return attrs_cached;
3744 /* Given an attribute value, return the maximum CONST_STRING argument
3745 encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
3747 static int
3748 max_attr_value (rtx exp, int *unknownp)
3750 int current_max;
3751 int i, n;
3753 switch (GET_CODE (exp))
3755 case CONST_STRING:
3756 current_max = atoi (XSTR (exp, 0));
3757 break;
3759 case COND:
3760 current_max = max_attr_value (XEXP (exp, 1), unknownp);
3761 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3763 n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3764 if (n > current_max)
3765 current_max = n;
3767 break;
3769 case IF_THEN_ELSE:
3770 current_max = max_attr_value (XEXP (exp, 1), unknownp);
3771 n = max_attr_value (XEXP (exp, 2), unknownp);
3772 if (n > current_max)
3773 current_max = n;
3774 break;
3776 default:
3777 *unknownp = 1;
3778 current_max = INT_MAX;
3779 break;
3782 return current_max;
3785 /* Given an attribute value, return the minimum CONST_STRING argument
3786 encountered. Set *UNKNOWNP and return 0 if the value is unknown. */
3788 static int
3789 min_attr_value (rtx exp, int *unknownp)
3791 int current_min;
3792 int i, n;
3794 switch (GET_CODE (exp))
3796 case CONST_STRING:
3797 current_min = atoi (XSTR (exp, 0));
3798 break;
3800 case COND:
3801 current_min = min_attr_value (XEXP (exp, 1), unknownp);
3802 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3804 n = min_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3805 if (n < current_min)
3806 current_min = n;
3808 break;
3810 case IF_THEN_ELSE:
3811 current_min = min_attr_value (XEXP (exp, 1), unknownp);
3812 n = min_attr_value (XEXP (exp, 2), unknownp);
3813 if (n < current_min)
3814 current_min = n;
3815 break;
3817 default:
3818 *unknownp = 1;
3819 current_min = INT_MAX;
3820 break;
3823 return current_min;
3826 /* Given an attribute value, return the result of ORing together all
3827 CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
3828 if the numeric value is not known. */
3830 static int
3831 or_attr_value (rtx exp, int *unknownp)
3833 int current_or;
3834 int i;
3836 switch (GET_CODE (exp))
3838 case CONST_STRING:
3839 current_or = atoi (XSTR (exp, 0));
3840 break;
3842 case COND:
3843 current_or = or_attr_value (XEXP (exp, 1), unknownp);
3844 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3845 current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3846 break;
3848 case IF_THEN_ELSE:
3849 current_or = or_attr_value (XEXP (exp, 1), unknownp);
3850 current_or |= or_attr_value (XEXP (exp, 2), unknownp);
3851 break;
3853 default:
3854 *unknownp = 1;
3855 current_or = -1;
3856 break;
3859 return current_or;
3862 /* Scan an attribute value, possibly a conditional, and record what actions
3863 will be required to do any conditional tests in it.
3865 Specifically, set
3866 `must_extract' if we need to extract the insn operands
3867 `must_constrain' if we must compute `which_alternative'
3868 `address_used' if an address expression was used
3869 `length_used' if an (eq_attr "length" ...) was used
3872 static void
3873 walk_attr_value (rtx exp)
3875 int i, j;
3876 const char *fmt;
3877 RTX_CODE code;
3879 if (exp == NULL)
3880 return;
3882 code = GET_CODE (exp);
3883 switch (code)
3885 case SYMBOL_REF:
3886 if (! ATTR_IND_SIMPLIFIED_P (exp))
3887 /* Since this is an arbitrary expression, it can look at anything.
3888 However, constant expressions do not depend on any particular
3889 insn. */
3890 must_extract = must_constrain = 1;
3891 return;
3893 case MATCH_OPERAND:
3894 must_extract = 1;
3895 return;
3897 case MATCH_TEST:
3898 case EQ_ATTR_ALT:
3899 must_extract = must_constrain = 1;
3900 break;
3902 case EQ_ATTR:
3903 if (XSTR (exp, 0) == alternative_name)
3904 must_extract = must_constrain = 1;
3905 else if (strcmp_check (XSTR (exp, 0), length_str) == 0)
3906 length_used = 1;
3907 return;
3909 case MATCH_DUP:
3910 must_extract = 1;
3911 address_used = 1;
3912 return;
3914 case PC:
3915 address_used = 1;
3916 return;
3918 case ATTR_FLAG:
3919 return;
3921 default:
3922 break;
3925 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
3926 switch (*fmt++)
3928 case 'e':
3929 case 'u':
3930 walk_attr_value (XEXP (exp, i));
3931 break;
3933 case 'E':
3934 if (XVEC (exp, i) != NULL)
3935 for (j = 0; j < XVECLEN (exp, i); j++)
3936 walk_attr_value (XVECEXP (exp, i, j));
3937 break;
3941 /* Write out a function to obtain the attribute for a given INSN. */
3943 static void
3944 write_attr_get (FILE *outf, struct attr_desc *attr)
3946 struct attr_value *av, *common_av;
3947 int i, j;
3949 /* Find the most used attribute value. Handle that as the `default' of the
3950 switch we will generate. */
3951 common_av = find_most_used (attr);
3953 /* Write out start of function, then all values with explicit `case' lines,
3954 then a `default', then the value with the most uses. */
3955 if (attr->enum_name)
3956 fprintf (outf, "enum %s\n", attr->enum_name);
3957 else if (!attr->is_numeric)
3958 fprintf (outf, "enum attr_%s\n", attr->name);
3959 else
3960 fprintf (outf, "int\n");
3962 /* If the attribute name starts with a star, the remainder is the name of
3963 the subroutine to use, instead of `get_attr_...'. */
3964 if (attr->name[0] == '*')
3965 fprintf (outf, "%s (rtx_insn *insn ATTRIBUTE_UNUSED)\n", &attr->name[1]);
3966 else if (attr->is_const == 0)
3967 fprintf (outf, "get_attr_%s (rtx_insn *insn ATTRIBUTE_UNUSED)\n", attr->name);
3968 else
3970 fprintf (outf, "get_attr_%s (void)\n", attr->name);
3971 fprintf (outf, "{\n");
3973 for (av = attr->first_value; av; av = av->next)
3974 if (av->num_insns == 1)
3975 write_attr_set (outf, attr, 2, av->value, "return", ";",
3976 true_rtx, av->first_insn->def->insn_code,
3977 av->first_insn->def->insn_index, 0);
3978 else if (av->num_insns != 0)
3979 write_attr_set (outf, attr, 2, av->value, "return", ";",
3980 true_rtx, -2, 0, 0);
3982 fprintf (outf, "}\n\n");
3983 return;
3986 fprintf (outf, "{\n");
3988 /* Find attributes that are worth caching in the conditions. */
3989 cached_attr_count = 0;
3990 attrs_seen_more_than_once = 0;
3991 for (av = attr->first_value; av; av = av->next)
3993 attrs_seen_once = 0;
3994 find_attrs_to_cache (av->value, true);
3996 /* Remove those that aren't worth caching from the array. */
3997 for (i = 0, j = 0; i < cached_attr_count; i++)
3998 if ((attrs_seen_more_than_once & (1U << i)) != 0)
4000 const char *name = cached_attrs[i];
4001 struct attr_desc *cached_attr;
4002 if (i != j)
4003 cached_attrs[j] = name;
4004 cached_attr = find_attr (&name, 0);
4005 gcc_assert (cached_attr && cached_attr->is_const == 0);
4006 if (cached_attr->enum_name)
4007 fprintf (outf, " enum %s", cached_attr->enum_name);
4008 else if (!cached_attr->is_numeric)
4009 fprintf (outf, " enum attr_%s", cached_attr->name);
4010 else
4011 fprintf (outf, " int");
4012 fprintf (outf, " cached_%s ATTRIBUTE_UNUSED;\n", name);
4013 j++;
4015 cached_attr_count = j;
4016 if (cached_attr_count)
4017 fprintf (outf, "\n");
4019 fprintf (outf, " switch (recog_memoized (insn))\n");
4020 fprintf (outf, " {\n");
4022 for (av = attr->first_value; av; av = av->next)
4023 if (av != common_av)
4024 write_attr_case (outf, attr, av, 1, "return", ";", 4, true_rtx);
4026 write_attr_case (outf, attr, common_av, 0, "return", ";", 4, true_rtx);
4027 fprintf (outf, " }\n}\n\n");
4028 cached_attr_count = 0;
4031 /* Given an AND tree of known true terms (because we are inside an `if' with
4032 that as the condition or are in an `else' clause) and an expression,
4033 replace any known true terms with TRUE. Use `simplify_and_tree' to do
4034 the bulk of the work. */
4036 static rtx
4037 eliminate_known_true (rtx known_true, rtx exp, int insn_code, int insn_index)
4039 rtx term;
4041 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
4043 if (GET_CODE (known_true) == AND)
4045 exp = eliminate_known_true (XEXP (known_true, 0), exp,
4046 insn_code, insn_index);
4047 exp = eliminate_known_true (XEXP (known_true, 1), exp,
4048 insn_code, insn_index);
4050 else
4052 term = known_true;
4053 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
4056 return exp;
4059 /* Write out a series of tests and assignment statements to perform tests and
4060 sets of an attribute value. We are passed an indentation amount and prefix
4061 and suffix strings to write around each attribute value (e.g., "return"
4062 and ";"). */
4064 static void
4065 write_attr_set (FILE *outf, struct attr_desc *attr, int indent, rtx value,
4066 const char *prefix, const char *suffix, rtx known_true,
4067 int insn_code, int insn_index, unsigned int attrs_cached)
4069 if (GET_CODE (value) == COND)
4071 /* Assume the default value will be the default of the COND unless we
4072 find an always true expression. */
4073 rtx default_val = XEXP (value, 1);
4074 rtx our_known_true = known_true;
4075 rtx newexp;
4076 int first_if = 1;
4077 int i;
4079 if (cached_attr_count)
4081 attrs_seen_once = 0;
4082 attrs_seen_more_than_once = 0;
4083 for (i = 0; i < XVECLEN (value, 0); i += 2)
4084 find_attrs_to_cache (XVECEXP (value, 0, i), false);
4085 attrs_to_cache |= attrs_seen_more_than_once;
4088 for (i = 0; i < XVECLEN (value, 0); i += 2)
4090 rtx testexp;
4091 rtx inner_true;
4093 /* Reset our_known_true after some time to not accumulate
4094 too much cruft (slowing down genattrtab). */
4095 if ((i & 31) == 0)
4096 our_known_true = known_true;
4097 testexp = eliminate_known_true (our_known_true,
4098 XVECEXP (value, 0, i),
4099 insn_code, insn_index);
4100 newexp = attr_rtx (NOT, testexp);
4101 newexp = insert_right_side (AND, our_known_true, newexp,
4102 insn_code, insn_index);
4104 /* If the test expression is always true or if the next `known_true'
4105 expression is always false, this is the last case, so break
4106 out and let this value be the `else' case. */
4107 if (testexp == true_rtx || newexp == false_rtx)
4109 default_val = XVECEXP (value, 0, i + 1);
4110 break;
4113 /* Compute the expression to pass to our recursive call as being
4114 known true. */
4115 inner_true = insert_right_side (AND, our_known_true,
4116 testexp, insn_code, insn_index);
4118 /* If this is always false, skip it. */
4119 if (inner_true == false_rtx)
4120 continue;
4122 attrs_cached_inside = attrs_cached;
4123 attrs_cached_after = attrs_cached;
4124 write_indent (outf, indent);
4125 fprintf (outf, "%sif ", first_if ? "" : "else ");
4126 first_if = 0;
4127 write_test_expr (outf, testexp, attrs_cached,
4128 (FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND));
4129 attrs_cached = attrs_cached_after;
4130 fprintf (outf, "\n");
4131 write_indent (outf, indent + 2);
4132 fprintf (outf, "{\n");
4134 write_attr_set (outf, attr, indent + 4,
4135 XVECEXP (value, 0, i + 1), prefix, suffix,
4136 inner_true, insn_code, insn_index,
4137 attrs_cached_inside);
4138 write_indent (outf, indent + 2);
4139 fprintf (outf, "}\n");
4140 our_known_true = newexp;
4143 if (! first_if)
4145 write_indent (outf, indent);
4146 fprintf (outf, "else\n");
4147 write_indent (outf, indent + 2);
4148 fprintf (outf, "{\n");
4151 write_attr_set (outf, attr, first_if ? indent : indent + 4, default_val,
4152 prefix, suffix, our_known_true, insn_code, insn_index,
4153 attrs_cached);
4155 if (! first_if)
4157 write_indent (outf, indent + 2);
4158 fprintf (outf, "}\n");
4161 else
4163 write_indent (outf, indent);
4164 fprintf (outf, "%s ", prefix);
4165 write_attr_value (outf, attr, value);
4166 fprintf (outf, "%s\n", suffix);
4170 /* Write a series of case statements for every instruction in list IE.
4171 INDENT is the amount of indentation to write before each case. */
4173 static void
4174 write_insn_cases (FILE *outf, struct insn_ent *ie, int indent)
4176 for (; ie != 0; ie = ie->next)
4177 if (ie->def->insn_code != -1)
4179 write_indent (outf, indent);
4180 if (GET_CODE (ie->def->def) == DEFINE_PEEPHOLE)
4181 fprintf (outf, "case %d: /* define_peephole, %s:%d */\n",
4182 ie->def->insn_code, ie->def->loc.filename,
4183 ie->def->loc.lineno);
4184 else
4185 fprintf (outf, "case %d: /* %s */\n",
4186 ie->def->insn_code, XSTR (ie->def->def, 0));
4190 /* Write out the computation for one attribute value. */
4192 static void
4193 write_attr_case (FILE *outf, struct attr_desc *attr, struct attr_value *av,
4194 int write_case_lines, const char *prefix, const char *suffix,
4195 int indent, rtx known_true)
4197 if (av->num_insns == 0)
4198 return;
4200 if (av->has_asm_insn)
4202 write_indent (outf, indent);
4203 fprintf (outf, "case -1:\n");
4204 write_indent (outf, indent + 2);
4205 fprintf (outf, "if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
4206 write_indent (outf, indent + 2);
4207 fprintf (outf, " && asm_noperands (PATTERN (insn)) < 0)\n");
4208 write_indent (outf, indent + 2);
4209 fprintf (outf, " fatal_insn_not_found (insn);\n");
4210 write_indent (outf, indent + 2);
4211 fprintf (outf, "/* FALLTHRU */\n");
4214 if (write_case_lines)
4215 write_insn_cases (outf, av->first_insn, indent);
4216 else
4218 write_indent (outf, indent);
4219 fprintf (outf, "default:\n");
4222 /* See what we have to do to output this value. */
4223 must_extract = must_constrain = address_used = 0;
4224 walk_attr_value (av->value);
4226 if (must_constrain)
4228 write_indent (outf, indent + 2);
4229 fprintf (outf, "extract_constrain_insn_cached (insn);\n");
4231 else if (must_extract)
4233 write_indent (outf, indent + 2);
4234 fprintf (outf, "extract_insn_cached (insn);\n");
4237 attrs_to_cache = 0;
4238 if (av->num_insns == 1)
4239 write_attr_set (outf, attr, indent + 2, av->value, prefix, suffix,
4240 known_true, av->first_insn->def->insn_code,
4241 av->first_insn->def->insn_index, 0);
4242 else
4243 write_attr_set (outf, attr, indent + 2, av->value, prefix, suffix,
4244 known_true, -2, 0, 0);
4246 if (strncmp (prefix, "return", 6))
4248 write_indent (outf, indent + 2);
4249 fprintf (outf, "break;\n");
4251 fprintf (outf, "\n");
4254 /* Utilities to write in various forms. */
4256 static void
4257 write_attr_valueq (FILE *outf, struct attr_desc *attr, const char *s)
4259 if (attr->is_numeric)
4261 int num = atoi (s);
4263 fprintf (outf, "%d", num);
4265 if (num > 9 || num < 0)
4266 fprintf (outf, " /* %#x */", num);
4268 else
4270 write_upcase (outf, attr->enum_name ? attr->enum_name : attr->name);
4271 fprintf (outf, "_");
4272 write_upcase (outf, s);
4276 static void
4277 write_attr_value (FILE *outf, struct attr_desc *attr, rtx value)
4279 int op;
4281 switch (GET_CODE (value))
4283 case CONST_STRING:
4284 write_attr_valueq (outf, attr, XSTR (value, 0));
4285 break;
4287 case CONST_INT:
4288 fprintf (outf, HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
4289 break;
4291 case SYMBOL_REF:
4292 rtx_reader_ptr->fprint_c_condition (outf, XSTR (value, 0));
4293 break;
4295 case ATTR:
4297 struct attr_desc *attr2 = find_attr (&XSTR (value, 0), 0);
4298 if (attr->enum_name)
4299 fprintf (outf, "(enum %s)", attr->enum_name);
4300 else if (!attr->is_numeric)
4301 fprintf (outf, "(enum attr_%s)", attr->name);
4302 else if (!attr2->is_numeric)
4303 fprintf (outf, "(int)");
4305 fprintf (outf, "get_attr_%s (%s)", attr2->name,
4306 (attr2->is_const ? "" : "insn"));
4308 break;
4310 case PLUS:
4311 op = '+';
4312 goto do_operator;
4313 case MINUS:
4314 op = '-';
4315 goto do_operator;
4316 case MULT:
4317 op = '*';
4318 goto do_operator;
4319 case DIV:
4320 op = '/';
4321 goto do_operator;
4322 case MOD:
4323 op = '%';
4324 goto do_operator;
4326 do_operator:
4327 write_attr_value (outf, attr, XEXP (value, 0));
4328 fputc (' ', outf);
4329 fputc (op, outf);
4330 fputc (' ', outf);
4331 write_attr_value (outf, attr, XEXP (value, 1));
4332 break;
4334 default:
4335 gcc_unreachable ();
4339 static void
4340 write_upcase (FILE *outf, const char *str)
4342 while (*str)
4344 /* The argument of TOUPPER should not have side effects. */
4345 fputc (TOUPPER (*str), outf);
4346 str++;
4350 static void
4351 write_indent (FILE *outf, int indent)
4353 for (; indent > 8; indent -= 8)
4354 fprintf (outf, "\t");
4356 for (; indent; indent--)
4357 fprintf (outf, " ");
4360 /* If the target does not have annul-true or annul-false delay slots, this
4361 function will create a dummy eligible_for function on OUTF which always
4362 returns false. KIND will be annul_true or annul_false. */
4364 static void
4365 write_dummy_eligible_delay (FILE *outf, const char *kind)
4367 /* Write function prelude. */
4369 fprintf (outf, "int\n");
4370 fprintf (outf, "eligible_for_%s (rtx_insn *delay_insn ATTRIBUTE_UNUSED,\n"
4371 " int slot ATTRIBUTE_UNUSED,\n"
4372 " rtx_insn *candidate_insn ATTRIBUTE_UNUSED,\n"
4373 " int flags ATTRIBUTE_UNUSED)\n",
4374 kind);
4375 fprintf (outf, "{\n");
4376 fprintf (outf, " return 0;\n");
4377 fprintf (outf, "}\n\n");
4380 /* Write a subroutine that is given an insn that requires a delay slot, a
4381 delay slot ordinal, and a candidate insn. It returns nonzero if the
4382 candidate can be placed in the specified delay slot of the insn.
4384 We can write as many as three subroutines. `eligible_for_delay'
4385 handles normal delay slots, `eligible_for_annul_true' indicates that
4386 the specified insn can be annulled if the branch is true, and likewise
4387 for `eligible_for_annul_false'.
4389 KIND is a string distinguishing these three cases ("delay", "annul_true",
4390 or "annul_false"). */
4392 static void
4393 write_eligible_delay (FILE *outf, const char *kind)
4395 struct delay_desc *delay;
4396 int max_slots;
4397 char str[50];
4398 const char *pstr;
4399 struct attr_desc *attr;
4400 struct attr_value *av, *common_av;
4401 int i;
4403 /* Compute the maximum number of delay slots required. We use the delay
4404 ordinal times this number plus one, plus the slot number as an index into
4405 the appropriate predicate to test. */
4407 for (delay = delays, max_slots = 0; delay; delay = delay->next)
4408 if (XVECLEN (delay->def, 1) / 3 > max_slots)
4409 max_slots = XVECLEN (delay->def, 1) / 3;
4411 /* Write function prelude. */
4413 fprintf (outf, "int\n");
4414 fprintf (outf, "eligible_for_%s (rtx_insn *delay_insn ATTRIBUTE_UNUSED, int slot, \n"
4415 " rtx_insn *candidate_insn, int flags ATTRIBUTE_UNUSED)\n",
4416 kind);
4417 fprintf (outf, "{\n");
4418 fprintf (outf, " rtx_insn *insn ATTRIBUTE_UNUSED;\n");
4419 fprintf (outf, "\n");
4420 fprintf (outf, " if (num_delay_slots (delay_insn) == 0)\n");
4421 fprintf (outf, " return 0;");
4422 fprintf (outf, "\n");
4423 fprintf (outf, " gcc_assert (slot < %d);\n", max_slots);
4424 fprintf (outf, "\n");
4425 /* Allow dbr_schedule to pass labels, etc. This can happen if try_split
4426 converts a compound instruction into a loop. */
4427 fprintf (outf, " if (!INSN_P (candidate_insn))\n");
4428 fprintf (outf, " return 0;\n");
4429 fprintf (outf, "\n");
4431 /* If more than one delay type, find out which type the delay insn is. */
4433 if (num_delays > 1)
4435 attr = find_attr (&delay_type_str, 0);
4436 gcc_assert (attr);
4437 common_av = find_most_used (attr);
4439 fprintf (outf, " insn = delay_insn;\n");
4440 fprintf (outf, " switch (recog_memoized (insn))\n");
4441 fprintf (outf, " {\n");
4443 sprintf (str, " * %d;\n break;", max_slots);
4444 for (av = attr->first_value; av; av = av->next)
4445 if (av != common_av)
4446 write_attr_case (outf, attr, av, 1, "slot +=", str, 4, true_rtx);
4448 write_attr_case (outf, attr, common_av, 0, "slot +=", str, 4, true_rtx);
4449 fprintf (outf, " }\n\n");
4451 /* Ensure matched. Otherwise, shouldn't have been called. */
4452 fprintf (outf, " gcc_assert (slot >= %d);\n\n", max_slots);
4455 /* If just one type of delay slot, write simple switch. */
4456 if (num_delays == 1 && max_slots == 1)
4458 fprintf (outf, " insn = candidate_insn;\n");
4459 fprintf (outf, " switch (recog_memoized (insn))\n");
4460 fprintf (outf, " {\n");
4462 attr = find_attr (&delay_1_0_str, 0);
4463 gcc_assert (attr);
4464 common_av = find_most_used (attr);
4466 for (av = attr->first_value; av; av = av->next)
4467 if (av != common_av)
4468 write_attr_case (outf, attr, av, 1, "return", ";", 4, true_rtx);
4470 write_attr_case (outf, attr, common_av, 0, "return", ";", 4, true_rtx);
4471 fprintf (outf, " }\n");
4474 else
4476 /* Write a nested CASE. The first indicates which condition we need to
4477 test, and the inner CASE tests the condition. */
4478 fprintf (outf, " insn = candidate_insn;\n");
4479 fprintf (outf, " switch (slot)\n");
4480 fprintf (outf, " {\n");
4482 for (delay = delays; delay; delay = delay->next)
4483 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
4485 fprintf (outf, " case %d:\n",
4486 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
4487 fprintf (outf, " switch (recog_memoized (insn))\n");
4488 fprintf (outf, "\t{\n");
4490 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
4491 pstr = str;
4492 attr = find_attr (&pstr, 0);
4493 gcc_assert (attr);
4494 common_av = find_most_used (attr);
4496 for (av = attr->first_value; av; av = av->next)
4497 if (av != common_av)
4498 write_attr_case (outf, attr, av, 1, "return", ";", 8, true_rtx);
4500 write_attr_case (outf, attr, common_av, 0, "return", ";", 8, true_rtx);
4501 fprintf (outf, " }\n");
4504 fprintf (outf, " default:\n");
4505 fprintf (outf, " gcc_unreachable ();\n");
4506 fprintf (outf, " }\n");
4509 fprintf (outf, "}\n\n");
4512 /* This page contains miscellaneous utility routines. */
4514 /* Given a pointer to a (char *), return a malloc'ed string containing the
4515 next comma-separated element. Advance the pointer to after the string
4516 scanned, or the end-of-string. Return NULL if at end of string. */
4518 static char *
4519 next_comma_elt (const char **pstr)
4521 const char *start;
4523 start = scan_comma_elt (pstr);
4525 if (start == NULL)
4526 return NULL;
4528 return attr_string (start, *pstr - start);
4531 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
4532 is nonzero, build a new attribute, if one does not exist. *NAME_P is
4533 replaced by a pointer to a canonical copy of the string. */
4535 static struct attr_desc *
4536 find_attr (const char **name_p, int create)
4538 struct attr_desc *attr;
4539 int index;
4540 const char *name = *name_p;
4542 /* Before we resort to using `strcmp', see if the string address matches
4543 anywhere. In most cases, it should have been canonicalized to do so. */
4544 if (name == alternative_name)
4545 return NULL;
4547 index = name[0] & (MAX_ATTRS_INDEX - 1);
4548 for (attr = attrs[index]; attr; attr = attr->next)
4549 if (name == attr->name)
4550 return attr;
4552 /* Otherwise, do it the slow way. */
4553 for (attr = attrs[index]; attr; attr = attr->next)
4554 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
4556 *name_p = attr->name;
4557 return attr;
4560 if (! create)
4561 return NULL;
4563 attr = oballoc (struct attr_desc);
4564 attr->name = DEF_ATTR_STRING (name);
4565 attr->enum_name = 0;
4566 attr->first_value = attr->default_val = NULL;
4567 attr->is_numeric = attr->is_const = attr->is_special = 0;
4568 attr->next = attrs[index];
4569 attrs[index] = attr;
4571 *name_p = attr->name;
4573 return attr;
4576 /* Create internal attribute with the given default value. */
4578 static void
4579 make_internal_attr (const char *name, rtx value, int special)
4581 struct attr_desc *attr;
4583 attr = find_attr (&name, 1);
4584 gcc_assert (!attr->default_val);
4586 attr->is_numeric = 1;
4587 attr->is_const = 0;
4588 attr->is_special = (special & ATTR_SPECIAL) != 0;
4589 attr->default_val = get_attr_value (file_location ("<internal>", 0, 0),
4590 value, attr, -2);
4593 /* Find the most used value of an attribute. */
4595 static struct attr_value *
4596 find_most_used (struct attr_desc *attr)
4598 struct attr_value *av;
4599 struct attr_value *most_used;
4600 int nuses;
4602 most_used = NULL;
4603 nuses = -1;
4605 for (av = attr->first_value; av; av = av->next)
4606 if (av->num_insns > nuses)
4607 nuses = av->num_insns, most_used = av;
4609 return most_used;
4612 /* Return (attr_value "n") */
4614 static rtx
4615 make_numeric_value (int n)
4617 static rtx int_values[20];
4618 rtx exp;
4619 char *p;
4621 gcc_assert (n >= 0);
4623 if (n < 20 && int_values[n])
4624 return int_values[n];
4626 p = attr_printf (MAX_DIGITS, "%d", n);
4627 exp = attr_rtx (CONST_STRING, p);
4629 if (n < 20)
4630 int_values[n] = exp;
4632 return exp;
4635 static rtx
4636 copy_rtx_unchanging (rtx orig)
4638 if (ATTR_IND_SIMPLIFIED_P (orig) || ATTR_CURR_SIMPLIFIED_P (orig))
4639 return orig;
4641 ATTR_CURR_SIMPLIFIED_P (orig) = 1;
4642 return orig;
4645 /* Determine if an insn has a constant number of delay slots, i.e., the
4646 number of delay slots is not a function of the length of the insn. */
4648 static void
4649 write_const_num_delay_slots (FILE *outf)
4651 struct attr_desc *attr = find_attr (&num_delay_slots_str, 0);
4652 struct attr_value *av;
4654 if (attr)
4656 fprintf (outf, "int\nconst_num_delay_slots (rtx_insn *insn)\n");
4657 fprintf (outf, "{\n");
4658 fprintf (outf, " switch (recog_memoized (insn))\n");
4659 fprintf (outf, " {\n");
4661 for (av = attr->first_value; av; av = av->next)
4663 length_used = 0;
4664 walk_attr_value (av->value);
4665 if (length_used)
4666 write_insn_cases (outf, av->first_insn, 4);
4669 fprintf (outf, " default:\n");
4670 fprintf (outf, " return 1;\n");
4671 fprintf (outf, " }\n}\n\n");
4675 /* Synthetic attributes used by insn-automata.c and the scheduler.
4676 These are primarily concerned with (define_insn_reservation)
4677 patterns. */
4679 struct insn_reserv
4681 struct insn_reserv *next;
4683 const char *name;
4684 int default_latency;
4685 rtx condexp;
4687 /* Sequence number of this insn. */
4688 int insn_num;
4690 /* Whether a (define_bypass) construct names this insn in its
4691 output list. */
4692 bool bypassed;
4695 static struct insn_reserv *all_insn_reservs = 0;
4696 static struct insn_reserv **last_insn_reserv_p = &all_insn_reservs;
4697 static size_t n_insn_reservs;
4699 /* Store information from a DEFINE_INSN_RESERVATION for future
4700 attribute generation. */
4701 static void
4702 gen_insn_reserv (md_rtx_info *info)
4704 struct insn_reserv *decl = oballoc (struct insn_reserv);
4705 rtx def = info->def;
4707 struct attr_desc attr = { };
4709 attr.name = DEF_ATTR_STRING (XSTR (def, 0));
4710 attr.loc = info->loc;
4712 decl->name = DEF_ATTR_STRING (XSTR (def, 0));
4713 decl->default_latency = XINT (def, 1);
4714 decl->condexp = check_attr_test (info->loc, XEXP (def, 2), &attr);
4715 decl->insn_num = n_insn_reservs;
4716 decl->bypassed = false;
4717 decl->next = 0;
4719 *last_insn_reserv_p = decl;
4720 last_insn_reserv_p = &decl->next;
4721 n_insn_reservs++;
4724 /* Store information from a DEFINE_BYPASS for future attribute
4725 generation. The only thing we care about is the list of output
4726 insns, which will later be used to tag reservation structures with
4727 a 'bypassed' bit. */
4729 struct bypass_list
4731 struct bypass_list *next;
4732 const char *pattern;
4735 static struct bypass_list *all_bypasses;
4736 static size_t n_bypasses;
4737 static size_t n_bypassed;
4739 static void
4740 gen_bypass_1 (const char *s, size_t len)
4742 struct bypass_list *b;
4744 if (len == 0)
4745 return;
4747 s = attr_string (s, len);
4748 for (b = all_bypasses; b; b = b->next)
4749 if (s == b->pattern)
4750 return; /* already got that one */
4752 b = oballoc (struct bypass_list);
4753 b->pattern = s;
4754 b->next = all_bypasses;
4755 all_bypasses = b;
4756 n_bypasses++;
4759 static void
4760 gen_bypass (md_rtx_info *info)
4762 const char *p, *base;
4764 rtx def = info->def;
4765 for (p = base = XSTR (def, 1); *p; p++)
4766 if (*p == ',')
4768 gen_bypass_1 (base, p - base);
4770 p++;
4771 while (ISSPACE (*p));
4772 base = p;
4774 gen_bypass_1 (base, p - base);
4777 /* Find and mark all of the bypassed insns. */
4778 static void
4779 process_bypasses (void)
4781 struct bypass_list *b;
4782 struct insn_reserv *r;
4784 n_bypassed = 0;
4786 /* The reservation list is likely to be much longer than the bypass
4787 list. */
4788 for (r = all_insn_reservs; r; r = r->next)
4789 for (b = all_bypasses; b; b = b->next)
4790 if (fnmatch (b->pattern, r->name, 0) == 0)
4792 n_bypassed++;
4793 r->bypassed = true;
4794 break;
4798 /* Check that attribute NAME is used in define_insn_reservation condition
4799 EXP. Return true if it is. */
4800 static bool
4801 check_tune_attr (const char *name, rtx exp)
4803 switch (GET_CODE (exp))
4805 case AND:
4806 if (check_tune_attr (name, XEXP (exp, 0)))
4807 return true;
4808 return check_tune_attr (name, XEXP (exp, 1));
4810 case IOR:
4811 return (check_tune_attr (name, XEXP (exp, 0))
4812 && check_tune_attr (name, XEXP (exp, 1)));
4814 case EQ_ATTR:
4815 return XSTR (exp, 0) == name;
4817 default:
4818 return false;
4822 /* Try to find a const attribute (usually cpu or tune) that is used
4823 in all define_insn_reservation conditions. */
4824 static struct attr_desc *
4825 find_tune_attr (rtx exp)
4827 struct attr_desc *attr;
4829 switch (GET_CODE (exp))
4831 case AND:
4832 case IOR:
4833 attr = find_tune_attr (XEXP (exp, 0));
4834 if (attr)
4835 return attr;
4836 return find_tune_attr (XEXP (exp, 1));
4838 case EQ_ATTR:
4839 if (XSTR (exp, 0) == alternative_name)
4840 return NULL;
4842 attr = find_attr (&XSTR (exp, 0), 0);
4843 gcc_assert (attr);
4845 if (attr->is_const && !attr->is_special)
4847 struct insn_reserv *decl;
4849 for (decl = all_insn_reservs; decl; decl = decl->next)
4850 if (! check_tune_attr (attr->name, decl->condexp))
4851 return NULL;
4852 return attr;
4854 return NULL;
4856 default:
4857 return NULL;
4861 /* Create all of the attributes that describe automaton properties.
4862 Write the DFA and latency function prototypes to the files that
4863 need to have them, and write the init_sched_attrs(). */
4865 static void
4866 make_automaton_attrs (void)
4868 int i;
4869 struct insn_reserv *decl;
4870 rtx code_exp, lats_exp, byps_exp;
4871 struct attr_desc *tune_attr;
4873 if (n_insn_reservs == 0)
4874 return;
4876 tune_attr = find_tune_attr (all_insn_reservs->condexp);
4877 if (tune_attr != NULL)
4879 rtx *condexps = XNEWVEC (rtx, n_insn_reservs * 3);
4880 struct attr_value *val;
4881 bool first = true;
4883 gcc_assert (tune_attr->is_const
4884 && !tune_attr->is_special
4885 && !tune_attr->is_numeric);
4887 /* Write the prototypes for all DFA functions. */
4888 for (val = tune_attr->first_value; val; val = val->next)
4890 if (val == tune_attr->default_val)
4891 continue;
4892 gcc_assert (GET_CODE (val->value) == CONST_STRING);
4893 fprintf (dfa_file,
4894 "extern int internal_dfa_insn_code_%s (rtx_insn *);\n",
4895 XSTR (val->value, 0));
4897 fprintf (dfa_file, "\n");
4899 /* Write the prototypes for all latency functions. */
4900 for (val = tune_attr->first_value; val; val = val->next)
4902 if (val == tune_attr->default_val)
4903 continue;
4904 gcc_assert (GET_CODE (val->value) == CONST_STRING);
4905 fprintf (latency_file,
4906 "extern int insn_default_latency_%s (rtx_insn *);\n",
4907 XSTR (val->value, 0));
4909 fprintf (latency_file, "\n");
4911 /* Write the prototypes for all automaton functions. */
4912 for (val = tune_attr->first_value; val; val = val->next)
4914 if (val == tune_attr->default_val)
4915 continue;
4916 gcc_assert (GET_CODE (val->value) == CONST_STRING);
4917 fprintf (attr_file,
4918 "extern int internal_dfa_insn_code_%s (rtx_insn *);\n"
4919 "extern int insn_default_latency_%s (rtx_insn *);\n",
4920 XSTR (val->value, 0), XSTR (val->value, 0));
4922 fprintf (attr_file, "\n");
4923 fprintf (attr_file, "int (*internal_dfa_insn_code) (rtx_insn *);\n");
4924 fprintf (attr_file, "int (*insn_default_latency) (rtx_insn *);\n");
4925 fprintf (attr_file, "\n");
4926 fprintf (attr_file, "void\n");
4927 fprintf (attr_file, "init_sched_attrs (void)\n");
4928 fprintf (attr_file, "{\n");
4930 for (val = tune_attr->first_value; val; val = val->next)
4932 int j;
4933 char *name;
4934 rtx test = attr_eq (tune_attr->name, XSTR (val->value, 0));
4936 if (val == tune_attr->default_val)
4937 continue;
4938 for (decl = all_insn_reservs, i = 0;
4939 decl;
4940 decl = decl->next)
4942 rtx ctest = test;
4943 rtx condexp
4944 = simplify_and_tree (decl->condexp, &ctest, -2, 0);
4945 if (condexp == false_rtx)
4946 continue;
4947 if (condexp == true_rtx)
4948 break;
4949 condexps[i] = condexp;
4950 condexps[i + 1] = make_numeric_value (decl->insn_num);
4951 condexps[i + 2] = make_numeric_value (decl->default_latency);
4952 i += 3;
4955 code_exp = rtx_alloc (COND);
4956 lats_exp = rtx_alloc (COND);
4958 j = i / 3 * 2;
4959 XVEC (code_exp, 0) = rtvec_alloc (j);
4960 XVEC (lats_exp, 0) = rtvec_alloc (j);
4962 if (decl)
4964 XEXP (code_exp, 1) = make_numeric_value (decl->insn_num);
4965 XEXP (lats_exp, 1) = make_numeric_value (decl->default_latency);
4967 else
4969 XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
4970 XEXP (lats_exp, 1) = make_numeric_value (0);
4973 while (i > 0)
4975 i -= 3;
4976 j -= 2;
4977 XVECEXP (code_exp, 0, j) = condexps[i];
4978 XVECEXP (lats_exp, 0, j) = condexps[i];
4980 XVECEXP (code_exp, 0, j + 1) = condexps[i + 1];
4981 XVECEXP (lats_exp, 0, j + 1) = condexps[i + 2];
4984 name = XNEWVEC (char,
4985 sizeof ("*internal_dfa_insn_code_")
4986 + strlen (XSTR (val->value, 0)));
4987 strcpy (name, "*internal_dfa_insn_code_");
4988 strcat (name, XSTR (val->value, 0));
4989 make_internal_attr (name, code_exp, ATTR_NONE);
4990 strcpy (name, "*insn_default_latency_");
4991 strcat (name, XSTR (val->value, 0));
4992 make_internal_attr (name, lats_exp, ATTR_NONE);
4993 XDELETEVEC (name);
4995 if (first)
4997 fprintf (attr_file, " if (");
4998 first = false;
5000 else
5001 fprintf (attr_file, " else if (");
5002 write_test_expr (attr_file, test, 0, 0);
5003 fprintf (attr_file, ")\n");
5004 fprintf (attr_file, " {\n");
5005 fprintf (attr_file, " internal_dfa_insn_code\n");
5006 fprintf (attr_file, " = internal_dfa_insn_code_%s;\n",
5007 XSTR (val->value, 0));
5008 fprintf (attr_file, " insn_default_latency\n");
5009 fprintf (attr_file, " = insn_default_latency_%s;\n",
5010 XSTR (val->value, 0));
5011 fprintf (attr_file, " }\n");
5014 fprintf (attr_file, " else\n");
5015 fprintf (attr_file, " gcc_unreachable ();\n");
5016 fprintf (attr_file, "}\n");
5017 fprintf (attr_file, "\n");
5019 XDELETEVEC (condexps);
5021 else
5023 code_exp = rtx_alloc (COND);
5024 lats_exp = rtx_alloc (COND);
5026 XVEC (code_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
5027 XVEC (lats_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
5029 XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
5030 XEXP (lats_exp, 1) = make_numeric_value (0);
5032 for (decl = all_insn_reservs, i = 0;
5033 decl;
5034 decl = decl->next, i += 2)
5036 XVECEXP (code_exp, 0, i) = decl->condexp;
5037 XVECEXP (lats_exp, 0, i) = decl->condexp;
5039 XVECEXP (code_exp, 0, i+1) = make_numeric_value (decl->insn_num);
5040 XVECEXP (lats_exp, 0, i+1)
5041 = make_numeric_value (decl->default_latency);
5043 make_internal_attr ("*internal_dfa_insn_code", code_exp, ATTR_NONE);
5044 make_internal_attr ("*insn_default_latency", lats_exp, ATTR_NONE);
5047 if (n_bypasses == 0)
5048 byps_exp = make_numeric_value (0);
5049 else
5051 process_bypasses ();
5053 byps_exp = rtx_alloc (COND);
5054 XVEC (byps_exp, 0) = rtvec_alloc (n_bypassed * 2);
5055 XEXP (byps_exp, 1) = make_numeric_value (0);
5056 for (decl = all_insn_reservs, i = 0;
5057 decl;
5058 decl = decl->next)
5059 if (decl->bypassed)
5061 XVECEXP (byps_exp, 0, i) = decl->condexp;
5062 XVECEXP (byps_exp, 0, i+1) = make_numeric_value (1);
5063 i += 2;
5067 make_internal_attr ("*bypass_p", byps_exp, ATTR_NONE);
5070 static void
5071 write_header (FILE *outf)
5073 fprintf (outf, "/* Generated automatically by the program `genattrtab'\n"
5074 " from the machine description file `md'. */\n\n");
5076 fprintf (outf, "#define IN_TARGET_CODE 1\n");
5077 fprintf (outf, "#include \"config.h\"\n");
5078 fprintf (outf, "#include \"system.h\"\n");
5079 fprintf (outf, "#include \"coretypes.h\"\n");
5080 fprintf (outf, "#include \"backend.h\"\n");
5081 fprintf (outf, "#include \"predict.h\"\n");
5082 fprintf (outf, "#include \"tree.h\"\n");
5083 fprintf (outf, "#include \"rtl.h\"\n");
5084 fprintf (outf, "#include \"alias.h\"\n");
5085 fprintf (outf, "#include \"options.h\"\n");
5086 fprintf (outf, "#include \"varasm.h\"\n");
5087 fprintf (outf, "#include \"stor-layout.h\"\n");
5088 fprintf (outf, "#include \"calls.h\"\n");
5089 fprintf (outf, "#include \"insn-attr.h\"\n");
5090 fprintf (outf, "#include \"memmodel.h\"\n");
5091 fprintf (outf, "#include \"tm_p.h\"\n");
5092 fprintf (outf, "#include \"insn-config.h\"\n");
5093 fprintf (outf, "#include \"recog.h\"\n");
5094 fprintf (outf, "#include \"regs.h\"\n");
5095 fprintf (outf, "#include \"real.h\"\n");
5096 fprintf (outf, "#include \"output.h\"\n");
5097 fprintf (outf, "#include \"toplev.h\"\n");
5098 fprintf (outf, "#include \"flags.h\"\n");
5099 fprintf (outf, "#include \"emit-rtl.h\"\n");
5100 fprintf (outf, "\n");
5101 fprintf (outf, "#define operands recog_data.operand\n\n");
5104 static FILE *
5105 open_outfile (const char *file_name)
5107 FILE *outf;
5108 outf = fopen (file_name, "w");
5109 if (! outf)
5110 fatal ("cannot open file %s: %s", file_name, xstrerror (errno));
5111 write_header (outf);
5112 return outf;
5115 static bool
5116 handle_arg (const char *arg)
5118 switch (arg[1])
5120 case 'A':
5121 attr_file_name = &arg[2];
5122 return true;
5123 case 'D':
5124 dfa_file_name = &arg[2];
5125 return true;
5126 case 'L':
5127 latency_file_name = &arg[2];
5128 return true;
5129 default:
5130 return false;
5135 main (int argc, const char **argv)
5137 struct attr_desc *attr;
5138 struct insn_def *id;
5139 int i;
5141 progname = "genattrtab";
5143 if (!init_rtx_reader_args_cb (argc, argv, handle_arg))
5144 return FATAL_EXIT_CODE;
5146 attr_file = open_outfile (attr_file_name);
5147 dfa_file = open_outfile (dfa_file_name);
5148 latency_file = open_outfile (latency_file_name);
5150 obstack_init (hash_obstack);
5151 obstack_init (temp_obstack);
5153 /* Set up true and false rtx's */
5154 true_rtx = rtx_alloc (CONST_INT);
5155 XWINT (true_rtx, 0) = 1;
5156 false_rtx = rtx_alloc (CONST_INT);
5157 XWINT (false_rtx, 0) = 0;
5158 ATTR_IND_SIMPLIFIED_P (true_rtx) = ATTR_IND_SIMPLIFIED_P (false_rtx) = 1;
5159 ATTR_PERMANENT_P (true_rtx) = ATTR_PERMANENT_P (false_rtx) = 1;
5161 alternative_name = DEF_ATTR_STRING ("alternative");
5162 length_str = DEF_ATTR_STRING ("length");
5163 delay_type_str = DEF_ATTR_STRING ("*delay_type");
5164 delay_1_0_str = DEF_ATTR_STRING ("*delay_1_0");
5165 num_delay_slots_str = DEF_ATTR_STRING ("*num_delay_slots");
5167 /* Read the machine description. */
5169 md_rtx_info info;
5170 while (read_md_rtx (&info))
5172 switch (GET_CODE (info.def))
5174 case DEFINE_INSN:
5175 case DEFINE_PEEPHOLE:
5176 case DEFINE_ASM_ATTRIBUTES:
5177 gen_insn (&info);
5178 break;
5180 case DEFINE_ATTR:
5181 case DEFINE_ENUM_ATTR:
5182 gen_attr (&info);
5183 break;
5185 case DEFINE_DELAY:
5186 gen_delay (&info);
5187 break;
5189 case DEFINE_INSN_RESERVATION:
5190 gen_insn_reserv (&info);
5191 break;
5193 case DEFINE_BYPASS:
5194 gen_bypass (&info);
5195 break;
5197 default:
5198 break;
5200 if (GET_CODE (info.def) != DEFINE_ASM_ATTRIBUTES)
5201 insn_index_number++;
5204 if (have_error)
5205 return FATAL_EXIT_CODE;
5207 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
5208 if (! got_define_asm_attributes)
5210 md_rtx_info info;
5211 info.def = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
5212 XVEC (info.def, 0) = rtvec_alloc (0);
5213 info.loc = file_location ("<internal>", 0, 0);
5214 info.index = -1;
5215 gen_insn (&info);
5218 /* Expand DEFINE_DELAY information into new attribute. */
5219 expand_delays ();
5221 /* Make `insn_alternatives'. */
5222 int num_insn_codes = get_num_insn_codes ();
5223 insn_alternatives = oballocvec (uint64_t, num_insn_codes);
5224 for (id = defs; id; id = id->next)
5225 if (id->insn_code >= 0)
5226 insn_alternatives[id->insn_code]
5227 = (((uint64_t) 1) << id->num_alternatives) - 1;
5229 /* Make `insn_n_alternatives'. */
5230 insn_n_alternatives = oballocvec (int, num_insn_codes);
5231 for (id = defs; id; id = id->next)
5232 if (id->insn_code >= 0)
5233 insn_n_alternatives[id->insn_code] = id->num_alternatives;
5235 /* Construct extra attributes for automata. */
5236 make_automaton_attrs ();
5238 /* Prepare to write out attribute subroutines by checking everything stored
5239 away and building the attribute cases. */
5241 check_defs ();
5243 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5244 for (attr = attrs[i]; attr; attr = attr->next)
5245 attr->default_val->value
5246 = check_attr_value (attr->loc, attr->default_val->value, attr);
5248 if (have_error)
5249 return FATAL_EXIT_CODE;
5251 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5252 for (attr = attrs[i]; attr; attr = attr->next)
5253 fill_attr (attr);
5255 /* Construct extra attributes for `length'. */
5256 make_length_attrs ();
5258 /* Perform any possible optimizations to speed up compilation. */
5259 optimize_attrs (num_insn_codes);
5261 /* Now write out all the `gen_attr_...' routines. Do these before the
5262 special routines so that they get defined before they are used. */
5264 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5265 for (attr = attrs[i]; attr; attr = attr->next)
5267 FILE *outf;
5269 #define IS_ATTR_GROUP(X) (!strncmp (attr->name, X, strlen (X)))
5270 if (IS_ATTR_GROUP ("*internal_dfa_insn_code"))
5271 outf = dfa_file;
5272 else if (IS_ATTR_GROUP ("*insn_default_latency"))
5273 outf = latency_file;
5274 else
5275 outf = attr_file;
5276 #undef IS_ATTR_GROUP
5278 if (! attr->is_special && ! attr->is_const)
5279 write_attr_get (outf, attr);
5282 /* Write out delay eligibility information, if DEFINE_DELAY present.
5283 (The function to compute the number of delay slots will be written
5284 below.) */
5285 write_eligible_delay (attr_file, "delay");
5286 if (have_annul_true)
5287 write_eligible_delay (attr_file, "annul_true");
5288 else
5289 write_dummy_eligible_delay (attr_file, "annul_true");
5290 if (have_annul_false)
5291 write_eligible_delay (attr_file, "annul_false");
5292 else
5293 write_dummy_eligible_delay (attr_file, "annul_false");
5295 /* Write out constant delay slot info. */
5296 write_const_num_delay_slots (attr_file);
5298 write_length_unit_log (attr_file);
5300 if (fclose (attr_file) != 0)
5301 fatal ("cannot close file %s: %s", attr_file_name, xstrerror (errno));
5302 if (fclose (dfa_file) != 0)
5303 fatal ("cannot close file %s: %s", dfa_file_name, xstrerror (errno));
5304 if (fclose (latency_file) != 0)
5305 fatal ("cannot close file %s: %s", latency_file_name, xstrerror (errno));
5307 return SUCCESS_EXIT_CODE;