Daily bump.
[official-gcc.git] / gcc / genattrtab.c
blob51dfe77401e8eac474b60935ad22063e281ff3b7
1 /* Generate code from machine description to compute values of attributes.
2 Copyright (C) 1991-2017 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 gcc_unreachable ();
569 return rt_val;
572 rtl_obstack = old_obstack;
573 attr_hash_add_rtx (hashcode, rt_val);
574 ATTR_PERMANENT_P (rt_val) = permanent_p;
575 return rt_val;
578 static rtx
579 attr_rtx (enum rtx_code code, ...)
581 rtx result;
582 va_list p;
584 va_start (p, code);
585 result = attr_rtx_1 (code, p);
586 va_end (p);
587 return result;
590 /* Create a new string printed with the printf line arguments into a space
591 of at most LEN bytes:
593 rtx attr_printf (len, format, [arg1, ..., argn]) */
595 static char *
596 attr_printf (unsigned int len, const char *fmt, ...)
598 char str[256];
599 va_list p;
601 va_start (p, fmt);
603 gcc_assert (len < sizeof str); /* Leave room for \0. */
605 vsprintf (str, fmt, p);
606 va_end (p);
608 return DEF_ATTR_STRING (str);
611 static rtx
612 attr_eq (const char *name, const char *value)
614 return attr_rtx (EQ_ATTR, name, value);
617 static const char *
618 attr_numeral (int n)
620 return XSTR (make_numeric_value (n), 0);
623 /* Return a permanent (possibly shared) copy of a string STR (not assumed
624 to be null terminated) with LEN bytes. */
626 static char *
627 attr_string (const char *str, int len)
629 struct attr_hash *h;
630 unsigned int hashcode;
631 int i;
632 char *new_str;
634 /* Compute the hash code. */
635 hashcode = (len + 1) * 613U + (unsigned) str[0];
636 for (i = 1; i < len; i += 2)
637 hashcode = ((hashcode * 613) + (unsigned) str[i]);
638 if ((int) hashcode < 0)
639 hashcode = -hashcode;
641 /* Search the table for the string. */
642 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
643 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
644 && !strncmp (h->u.str, str, len))
645 return h->u.str; /* <-- return if found. */
647 /* Not found; create a permanent copy and add it to the hash table. */
648 new_str = XOBNEWVAR (hash_obstack, char, len + 1);
649 memcpy (new_str, str, len);
650 new_str[len] = '\0';
651 attr_hash_add_string (hashcode, new_str);
652 rtx_reader_ptr->copy_md_ptr_loc (new_str, str);
654 return new_str; /* Return the new string. */
657 /* Check two rtx's for equality of contents,
658 taking advantage of the fact that if both are hashed
659 then they can't be equal unless they are the same object. */
661 static int
662 attr_equal_p (rtx x, rtx y)
664 return (x == y || (! (ATTR_PERMANENT_P (x) && ATTR_PERMANENT_P (y))
665 && rtx_equal_p (x, y)));
668 /* Given a test expression EXP for attribute ATTR, ensure it is validly
669 formed. LOC is the location of the .md construct that contains EXP.
671 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
672 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
673 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
675 Update the string address in EQ_ATTR expression to be the same used
676 in the attribute (or `alternative_name') to speed up subsequent
677 `find_attr' calls and eliminate most `strcmp' calls.
679 Return the new expression, if any. */
681 static rtx
682 check_attr_test (file_location loc, rtx exp, attr_desc *attr)
684 struct attr_value *av;
685 const char *name_ptr, *p;
686 rtx orexp, newexp;
688 switch (GET_CODE (exp))
690 case EQ_ATTR:
691 /* Handle negation test. */
692 if (XSTR (exp, 1)[0] == '!')
693 return check_attr_test (loc,
694 attr_rtx (NOT,
695 attr_eq (XSTR (exp, 0),
696 &XSTR (exp, 1)[1])),
697 attr);
699 else if (n_comma_elts (XSTR (exp, 1)) == 1)
701 attr_desc *attr2 = find_attr (&XSTR (exp, 0), 0);
702 if (attr2 == NULL)
704 if (! strcmp (XSTR (exp, 0), "alternative"))
705 return mk_attr_alt (((uint64_t) 1) << atoi (XSTR (exp, 1)));
706 else
707 fatal_at (loc, "unknown attribute `%s' in definition of"
708 " attribute `%s'", XSTR (exp, 0), attr->name);
711 if (attr->is_const && ! attr2->is_const)
712 fatal_at (loc, "constant attribute `%s' cannot test non-constant"
713 " attribute `%s'", attr->name, attr2->name);
715 /* Copy this just to make it permanent,
716 so expressions using it can be permanent too. */
717 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
719 /* It shouldn't be possible to simplify the value given to a
720 constant attribute, so don't expand this until it's time to
721 write the test expression. */
722 if (attr2->is_const)
723 ATTR_IND_SIMPLIFIED_P (exp) = 1;
725 if (attr2->is_numeric)
727 for (p = XSTR (exp, 1); *p; p++)
728 if (! ISDIGIT (*p))
729 fatal_at (loc, "attribute `%s' takes only numeric values",
730 attr2->name);
732 else
734 for (av = attr2->first_value; av; av = av->next)
735 if (GET_CODE (av->value) == CONST_STRING
736 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
737 break;
739 if (av == NULL)
740 fatal_at (loc, "unknown value `%s' for attribute `%s'",
741 XSTR (exp, 1), attr2->name);
744 else
746 if (! strcmp (XSTR (exp, 0), "alternative"))
748 int set = 0;
750 name_ptr = XSTR (exp, 1);
751 while ((p = next_comma_elt (&name_ptr)) != NULL)
752 set |= ((uint64_t) 1) << atoi (p);
754 return mk_attr_alt (set);
756 else
758 /* Make an IOR tree of the possible values. */
759 orexp = false_rtx;
760 name_ptr = XSTR (exp, 1);
761 while ((p = next_comma_elt (&name_ptr)) != NULL)
763 newexp = attr_eq (XSTR (exp, 0), p);
764 orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
767 return check_attr_test (loc, orexp, attr);
770 break;
772 case ATTR_FLAG:
773 break;
775 case CONST_INT:
776 /* Either TRUE or FALSE. */
777 if (XWINT (exp, 0))
778 return true_rtx;
779 else
780 return false_rtx;
782 case IOR:
783 case AND:
784 XEXP (exp, 0) = check_attr_test (loc, XEXP (exp, 0), attr);
785 XEXP (exp, 1) = check_attr_test (loc, XEXP (exp, 1), attr);
786 break;
788 case NOT:
789 XEXP (exp, 0) = check_attr_test (loc, XEXP (exp, 0), attr);
790 break;
792 case MATCH_TEST:
793 exp = attr_rtx (MATCH_TEST, XSTR (exp, 0));
794 ATTR_IND_SIMPLIFIED_P (exp) = 1;
795 break;
797 case MATCH_OPERAND:
798 if (attr->is_const)
799 fatal_at (loc, "invalid operator `%s' in definition of constant"
800 " attribute `%s'", GET_RTX_NAME (GET_CODE (exp)),
801 attr->name);
802 /* These cases can't be simplified. */
803 ATTR_IND_SIMPLIFIED_P (exp) = 1;
804 break;
806 case LE: case LT: case GT: case GE:
807 case LEU: case LTU: case GTU: case GEU:
808 case NE: case EQ:
809 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
810 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
811 exp = attr_rtx (GET_CODE (exp),
812 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
813 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
814 /* These cases can't be simplified. */
815 ATTR_IND_SIMPLIFIED_P (exp) = 1;
816 break;
818 case SYMBOL_REF:
819 if (attr->is_const)
821 /* These cases are valid for constant attributes, but can't be
822 simplified. */
823 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
824 ATTR_IND_SIMPLIFIED_P (exp) = 1;
825 break;
827 /* FALLTHRU */
828 default:
829 fatal_at (loc, "invalid operator `%s' in definition of attribute"
830 " `%s'", GET_RTX_NAME (GET_CODE (exp)), attr->name);
833 return exp;
836 /* Given an expression EXP, ensure that it is validly formed and that
837 all named attribute values are valid for ATTR. Issue an error if not.
838 LOC is the location of the .md construct that contains EXP.
840 Return a perhaps modified replacement expression for the value. */
842 static rtx
843 check_attr_value (file_location loc, rtx exp, struct attr_desc *attr)
845 struct attr_value *av;
846 const char *p;
847 int i;
849 switch (GET_CODE (exp))
851 case CONST_INT:
852 if (!attr->is_numeric)
854 error_at (loc,
855 "CONST_INT not valid for non-numeric attribute `%s'",
856 attr->name);
857 break;
860 if (INTVAL (exp) < 0)
862 error_at (loc,
863 "negative numeric value specified for attribute `%s'",
864 attr->name);
865 break;
867 break;
869 case CONST_STRING:
870 if (! strcmp (XSTR (exp, 0), "*"))
871 break;
873 if (attr->is_numeric)
875 p = XSTR (exp, 0);
876 for (; *p; p++)
877 if (! ISDIGIT (*p))
879 error_at (loc,
880 "non-numeric value specified for numeric"
881 " attribute `%s'", attr->name);
882 break;
884 break;
887 for (av = attr->first_value; av; av = av->next)
888 if (GET_CODE (av->value) == CONST_STRING
889 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
890 break;
892 if (av == NULL)
893 error_at (loc, "unknown value `%s' for attribute `%s'",
894 XSTR (exp, 0), attr->name);
895 break;
897 case IF_THEN_ELSE:
898 XEXP (exp, 0) = check_attr_test (loc, XEXP (exp, 0), attr);
899 XEXP (exp, 1) = check_attr_value (loc, XEXP (exp, 1), attr);
900 XEXP (exp, 2) = check_attr_value (loc, XEXP (exp, 2), attr);
901 break;
903 case PLUS:
904 case MINUS:
905 case MULT:
906 case DIV:
907 case MOD:
908 if (!attr->is_numeric)
910 error_at (loc, "invalid operation `%s' for non-numeric"
911 " attribute `%s'", GET_RTX_NAME (GET_CODE (exp)),
912 attr->name);
913 break;
915 /* Fall through. */
917 case IOR:
918 case AND:
919 XEXP (exp, 0) = check_attr_value (loc, XEXP (exp, 0), attr);
920 XEXP (exp, 1) = check_attr_value (loc, XEXP (exp, 1), attr);
921 break;
923 case FFS:
924 case CLZ:
925 case CTZ:
926 case POPCOUNT:
927 case PARITY:
928 case BSWAP:
929 XEXP (exp, 0) = check_attr_value (loc, XEXP (exp, 0), attr);
930 break;
932 case COND:
933 if (XVECLEN (exp, 0) % 2 != 0)
935 error_at (loc, "first operand of COND must have even length");
936 break;
939 for (i = 0; i < XVECLEN (exp, 0); i += 2)
941 XVECEXP (exp, 0, i) = check_attr_test (attr->loc,
942 XVECEXP (exp, 0, i),
943 attr);
944 XVECEXP (exp, 0, i + 1)
945 = check_attr_value (loc, XVECEXP (exp, 0, i + 1), attr);
948 XEXP (exp, 1) = check_attr_value (loc, XEXP (exp, 1), attr);
949 break;
951 case ATTR:
953 struct attr_desc *attr2 = find_attr (&XSTR (exp, 0), 0);
954 if (attr2 == NULL)
955 error_at (loc, "unknown attribute `%s' in ATTR",
956 XSTR (exp, 0));
957 else if (attr->is_const && ! attr2->is_const)
958 error_at (attr->loc,
959 "constant attribute `%s' cannot refer to non-constant"
960 " attribute `%s'", attr->name, attr2->name);
961 else if (attr->is_numeric != attr2->is_numeric)
962 error_at (loc,
963 "numeric attribute mismatch calling `%s' from `%s'",
964 attr2->name, attr->name);
966 break;
968 case SYMBOL_REF:
969 /* A constant SYMBOL_REF is valid as a constant attribute test and
970 is expanded later by make_canonical into a COND. In a non-constant
971 attribute test, it is left be. */
972 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
974 default:
975 error_at (loc, "invalid operator `%s' in definition of attribute `%s'",
976 GET_RTX_NAME (GET_CODE (exp)), attr->name);
977 break;
980 return exp;
983 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
984 It becomes a COND with each test being (eq_attr "alternative" "n") */
986 static rtx
987 convert_set_attr_alternative (rtx exp, struct insn_def *id)
989 int num_alt = id->num_alternatives;
990 rtx condexp;
991 int i;
993 if (XVECLEN (exp, 1) != num_alt)
995 error_at (id->loc, "bad number of entries in SET_ATTR_ALTERNATIVE,"
996 " was %d expected %d", XVECLEN (exp, 1), num_alt);
997 return NULL_RTX;
1000 /* Make a COND with all tests but the last. Select the last value via the
1001 default. */
1002 condexp = rtx_alloc (COND);
1003 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1005 for (i = 0; i < num_alt - 1; i++)
1007 const char *p;
1008 p = attr_numeral (i);
1010 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1011 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1014 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1016 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1019 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1020 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1022 static rtx
1023 convert_set_attr (rtx exp, struct insn_def *id)
1025 rtx newexp;
1026 const char *name_ptr;
1027 char *p;
1028 int n;
1030 /* See how many alternative specified. */
1031 n = n_comma_elts (XSTR (exp, 1));
1032 if (n == 1)
1033 return attr_rtx (SET,
1034 attr_rtx (ATTR, XSTR (exp, 0)),
1035 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1037 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1038 XSTR (newexp, 0) = XSTR (exp, 0);
1039 XVEC (newexp, 1) = rtvec_alloc (n);
1041 /* Process each comma-separated name. */
1042 name_ptr = XSTR (exp, 1);
1043 n = 0;
1044 while ((p = next_comma_elt (&name_ptr)) != NULL)
1045 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1047 return convert_set_attr_alternative (newexp, id);
1050 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1051 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1052 expressions. */
1054 static void
1055 check_defs (void)
1057 struct insn_def *id;
1058 struct attr_desc *attr;
1059 int i;
1060 rtx value;
1062 for (id = defs; id; id = id->next)
1064 if (XVEC (id->def, id->vec_idx) == NULL)
1065 continue;
1067 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1069 value = XVECEXP (id->def, id->vec_idx, i);
1070 switch (GET_CODE (value))
1072 case SET:
1073 if (GET_CODE (XEXP (value, 0)) != ATTR)
1075 error_at (id->loc, "bad attribute set");
1076 value = NULL_RTX;
1078 break;
1080 case SET_ATTR_ALTERNATIVE:
1081 value = convert_set_attr_alternative (value, id);
1082 break;
1084 case SET_ATTR:
1085 value = convert_set_attr (value, id);
1086 break;
1088 default:
1089 error_at (id->loc, "invalid attribute code %s",
1090 GET_RTX_NAME (GET_CODE (value)));
1091 value = NULL_RTX;
1093 if (value == NULL_RTX)
1094 continue;
1096 if ((attr = find_attr (&XSTR (XEXP (value, 0), 0), 0)) == NULL)
1098 error_at (id->loc, "unknown attribute %s",
1099 XSTR (XEXP (value, 0), 0));
1100 continue;
1103 XVECEXP (id->def, id->vec_idx, i) = value;
1104 XEXP (value, 1) = check_attr_value (id->loc, XEXP (value, 1), attr);
1109 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1110 expressions by converting them into a COND. This removes cases from this
1111 program. Also, replace an attribute value of "*" with the default attribute
1112 value. LOC is the location to use for error reporting. */
1114 static rtx
1115 make_canonical (file_location loc, struct attr_desc *attr, rtx exp)
1117 int i;
1118 rtx newexp;
1120 switch (GET_CODE (exp))
1122 case CONST_INT:
1123 exp = make_numeric_value (INTVAL (exp));
1124 break;
1126 case CONST_STRING:
1127 if (! strcmp (XSTR (exp, 0), "*"))
1129 if (attr->default_val == 0)
1130 fatal_at (loc, "(attr_value \"*\") used in invalid context");
1131 exp = attr->default_val->value;
1133 else
1134 XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1136 break;
1138 case SYMBOL_REF:
1139 if (!attr->is_const || ATTR_IND_SIMPLIFIED_P (exp))
1140 break;
1141 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1142 This makes the COND something that won't be considered an arbitrary
1143 expression by walk_attr_value. */
1144 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1145 exp = check_attr_value (loc, exp, attr);
1146 break;
1148 case IF_THEN_ELSE:
1149 newexp = rtx_alloc (COND);
1150 XVEC (newexp, 0) = rtvec_alloc (2);
1151 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1152 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1154 XEXP (newexp, 1) = XEXP (exp, 2);
1156 exp = newexp;
1157 /* Fall through to COND case since this is now a COND. */
1158 gcc_fallthrough ();
1160 case COND:
1162 int allsame = 1;
1163 rtx defval;
1165 /* First, check for degenerate COND. */
1166 if (XVECLEN (exp, 0) == 0)
1167 return make_canonical (loc, attr, XEXP (exp, 1));
1168 defval = XEXP (exp, 1) = make_canonical (loc, attr, XEXP (exp, 1));
1170 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1172 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1173 XVECEXP (exp, 0, i + 1)
1174 = make_canonical (loc, attr, XVECEXP (exp, 0, i + 1));
1175 if (! attr_equal_p (XVECEXP (exp, 0, i + 1), defval))
1176 allsame = 0;
1178 if (allsame)
1179 return defval;
1181 break;
1183 default:
1184 break;
1187 return exp;
1190 static rtx
1191 copy_boolean (rtx exp)
1193 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1194 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1195 copy_boolean (XEXP (exp, 1)));
1196 else if (GET_CODE (exp) == NOT)
1197 return attr_rtx (NOT, copy_boolean (XEXP (exp, 0)));
1198 if (GET_CODE (exp) == MATCH_OPERAND)
1200 XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1201 XSTR (exp, 2) = DEF_ATTR_STRING (XSTR (exp, 2));
1203 else if (GET_CODE (exp) == EQ_ATTR)
1205 XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1206 XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1209 return exp;
1212 /* Given a value and an attribute description, return a `struct attr_value *'
1213 that represents that value. This is either an existing structure, if the
1214 value has been previously encountered, or a newly-created structure.
1216 `insn_code' is the code of an insn whose attribute has the specified
1217 value (-2 if not processing an insn). We ensure that all insns for
1218 a given value have the same number of alternatives if the value checks
1219 alternatives. LOC is the location to use for error reporting. */
1221 static struct attr_value *
1222 get_attr_value (file_location loc, rtx value, struct attr_desc *attr,
1223 int insn_code)
1225 struct attr_value *av;
1226 uint64_t num_alt = 0;
1228 value = make_canonical (loc, attr, value);
1229 if (compares_alternatives_p (value))
1231 if (insn_code < 0 || insn_alternatives == NULL)
1232 fatal_at (loc, "(eq_attr \"alternatives\" ...) used in non-insn"
1233 " context");
1234 else
1235 num_alt = insn_alternatives[insn_code];
1238 for (av = attr->first_value; av; av = av->next)
1239 if (attr_equal_p (value, av->value)
1240 && (num_alt == 0 || av->first_insn == NULL
1241 || insn_alternatives[av->first_insn->def->insn_code]))
1242 return av;
1244 av = oballoc (struct attr_value);
1245 av->value = value;
1246 av->next = attr->first_value;
1247 attr->first_value = av;
1248 av->first_insn = NULL;
1249 av->num_insns = 0;
1250 av->has_asm_insn = 0;
1252 return av;
1255 /* After all DEFINE_DELAYs have been read in, create internal attributes
1256 to generate the required routines.
1258 First, we compute the number of delay slots for each insn (as a COND of
1259 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1260 delay type is specified, we compute a similar function giving the
1261 DEFINE_DELAY ordinal for each insn.
1263 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1264 tells whether a given insn can be in that delay slot.
1266 Normal attribute filling and optimization expands these to contain the
1267 information needed to handle delay slots. */
1269 static void
1270 expand_delays (void)
1272 struct delay_desc *delay;
1273 rtx condexp;
1274 rtx newexp;
1275 int i;
1276 char *p;
1278 /* First, generate data for `num_delay_slots' function. */
1280 condexp = rtx_alloc (COND);
1281 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1282 XEXP (condexp, 1) = make_numeric_value (0);
1284 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1286 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1287 XVECEXP (condexp, 0, i + 1)
1288 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1291 make_internal_attr (num_delay_slots_str, condexp, ATTR_NONE);
1293 /* If more than one delay type, do the same for computing the delay type. */
1294 if (num_delays > 1)
1296 condexp = rtx_alloc (COND);
1297 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1298 XEXP (condexp, 1) = make_numeric_value (0);
1300 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1302 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1303 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1306 make_internal_attr (delay_type_str, condexp, ATTR_SPECIAL);
1309 /* For each delay possibility and delay slot, compute an eligibility
1310 attribute for non-annulled insns and for each type of annulled (annul
1311 if true and annul if false). */
1312 for (delay = delays; delay; delay = delay->next)
1314 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1316 condexp = XVECEXP (delay->def, 1, i);
1317 if (condexp == 0)
1318 condexp = false_rtx;
1319 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1320 make_numeric_value (1), make_numeric_value (0));
1322 p = attr_printf (sizeof "*delay__" + MAX_DIGITS * 2,
1323 "*delay_%d_%d", delay->num, i / 3);
1324 make_internal_attr (p, newexp, ATTR_SPECIAL);
1326 if (have_annul_true)
1328 condexp = XVECEXP (delay->def, 1, i + 1);
1329 if (condexp == 0) condexp = false_rtx;
1330 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1331 make_numeric_value (1),
1332 make_numeric_value (0));
1333 p = attr_printf (sizeof "*annul_true__" + MAX_DIGITS * 2,
1334 "*annul_true_%d_%d", delay->num, i / 3);
1335 make_internal_attr (p, newexp, ATTR_SPECIAL);
1338 if (have_annul_false)
1340 condexp = XVECEXP (delay->def, 1, i + 2);
1341 if (condexp == 0) condexp = false_rtx;
1342 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1343 make_numeric_value (1),
1344 make_numeric_value (0));
1345 p = attr_printf (sizeof "*annul_false__" + MAX_DIGITS * 2,
1346 "*annul_false_%d_%d", delay->num, i / 3);
1347 make_internal_attr (p, newexp, ATTR_SPECIAL);
1353 /* Once all attributes and insns have been read and checked, we construct for
1354 each attribute value a list of all the insns that have that value for
1355 the attribute. */
1357 static void
1358 fill_attr (struct attr_desc *attr)
1360 struct attr_value *av;
1361 struct insn_ent *ie;
1362 struct insn_def *id;
1363 int i;
1364 rtx value;
1366 /* Don't fill constant attributes. The value is independent of
1367 any particular insn. */
1368 if (attr->is_const)
1369 return;
1371 for (id = defs; id; id = id->next)
1373 /* If no value is specified for this insn for this attribute, use the
1374 default. */
1375 value = NULL;
1376 if (XVEC (id->def, id->vec_idx))
1377 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1378 if (! strcmp_check (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
1379 attr->name))
1380 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
1382 if (value == NULL)
1383 av = attr->default_val;
1384 else
1385 av = get_attr_value (id->loc, value, attr, id->insn_code);
1387 ie = oballoc (struct insn_ent);
1388 ie->def = id;
1389 insert_insn_ent (av, ie);
1393 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
1394 test that checks relative positions of insns (uses MATCH_DUP or PC).
1395 If so, replace it with what is obtained by passing the expression to
1396 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
1397 recursively on each value (including the default value). Otherwise,
1398 return the value returned by NO_ADDRESS_FN applied to EXP. */
1400 static rtx
1401 substitute_address (rtx exp, rtx (*no_address_fn) (rtx),
1402 rtx (*address_fn) (rtx))
1404 int i;
1405 rtx newexp;
1407 if (GET_CODE (exp) == COND)
1409 /* See if any tests use addresses. */
1410 address_used = 0;
1411 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1412 walk_attr_value (XVECEXP (exp, 0, i));
1414 if (address_used)
1415 return (*address_fn) (exp);
1417 /* Make a new copy of this COND, replacing each element. */
1418 newexp = rtx_alloc (COND);
1419 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
1420 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1422 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
1423 XVECEXP (newexp, 0, i + 1)
1424 = substitute_address (XVECEXP (exp, 0, i + 1),
1425 no_address_fn, address_fn);
1428 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
1429 no_address_fn, address_fn);
1431 return newexp;
1434 else if (GET_CODE (exp) == IF_THEN_ELSE)
1436 address_used = 0;
1437 walk_attr_value (XEXP (exp, 0));
1438 if (address_used)
1439 return (*address_fn) (exp);
1441 return attr_rtx (IF_THEN_ELSE,
1442 substitute_address (XEXP (exp, 0),
1443 no_address_fn, address_fn),
1444 substitute_address (XEXP (exp, 1),
1445 no_address_fn, address_fn),
1446 substitute_address (XEXP (exp, 2),
1447 no_address_fn, address_fn));
1450 return (*no_address_fn) (exp);
1453 /* Make new attributes from the `length' attribute. The following are made,
1454 each corresponding to a function called from `shorten_branches' or
1455 `get_attr_length':
1457 *insn_default_length This is the length of the insn to be returned
1458 by `get_attr_length' before `shorten_branches'
1459 has been called. In each case where the length
1460 depends on relative addresses, the largest
1461 possible is used. This routine is also used
1462 to compute the initial size of the insn.
1464 *insn_variable_length_p This returns 1 if the insn's length depends
1465 on relative addresses, zero otherwise.
1467 *insn_current_length This is only called when it is known that the
1468 insn has a variable length and returns the
1469 current length, based on relative addresses.
1472 static void
1473 make_length_attrs (void)
1475 static const char *new_names[] =
1477 "*insn_default_length",
1478 "*insn_min_length",
1479 "*insn_variable_length_p",
1480 "*insn_current_length"
1482 static rtx (*const no_address_fn[]) (rtx)
1483 = {identity_fn,identity_fn, zero_fn, zero_fn};
1484 static rtx (*const address_fn[]) (rtx)
1485 = {max_fn, min_fn, one_fn, identity_fn};
1486 size_t i;
1487 struct attr_desc *length_attr, *new_attr;
1488 struct attr_value *av, *new_av;
1489 struct insn_ent *ie, *new_ie;
1491 /* See if length attribute is defined. If so, it must be numeric. Make
1492 it special so we don't output anything for it. */
1493 length_attr = find_attr (&length_str, 0);
1494 if (length_attr == 0)
1495 return;
1497 if (! length_attr->is_numeric)
1498 fatal_at (length_attr->loc, "length attribute must be numeric");
1500 length_attr->is_const = 0;
1501 length_attr->is_special = 1;
1503 /* Make each new attribute, in turn. */
1504 for (i = 0; i < ARRAY_SIZE (new_names); i++)
1506 make_internal_attr (new_names[i],
1507 substitute_address (length_attr->default_val->value,
1508 no_address_fn[i], address_fn[i]),
1509 ATTR_NONE);
1510 new_attr = find_attr (&new_names[i], 0);
1511 for (av = length_attr->first_value; av; av = av->next)
1512 for (ie = av->first_insn; ie; ie = ie->next)
1514 new_av = get_attr_value (ie->def->loc,
1515 substitute_address (av->value,
1516 no_address_fn[i],
1517 address_fn[i]),
1518 new_attr, ie->def->insn_code);
1519 new_ie = oballoc (struct insn_ent);
1520 new_ie->def = ie->def;
1521 insert_insn_ent (new_av, new_ie);
1526 /* Utility functions called from above routine. */
1528 static rtx
1529 identity_fn (rtx exp)
1531 return exp;
1534 static rtx
1535 zero_fn (rtx exp ATTRIBUTE_UNUSED)
1537 return make_numeric_value (0);
1540 static rtx
1541 one_fn (rtx exp ATTRIBUTE_UNUSED)
1543 return make_numeric_value (1);
1546 static rtx
1547 max_fn (rtx exp)
1549 int unknown;
1550 return make_numeric_value (max_attr_value (exp, &unknown));
1553 static rtx
1554 min_fn (rtx exp)
1556 int unknown;
1557 return make_numeric_value (min_attr_value (exp, &unknown));
1560 static void
1561 write_length_unit_log (FILE *outf)
1563 struct attr_desc *length_attr = find_attr (&length_str, 0);
1564 struct attr_value *av;
1565 struct insn_ent *ie;
1566 unsigned int length_unit_log, length_or;
1567 int unknown = 0;
1569 if (length_attr)
1571 length_or = or_attr_value (length_attr->default_val->value, &unknown);
1572 for (av = length_attr->first_value; av; av = av->next)
1573 for (ie = av->first_insn; ie; ie = ie->next)
1574 length_or |= or_attr_value (av->value, &unknown);
1577 if (length_attr == NULL || unknown)
1578 length_unit_log = 0;
1579 else
1581 length_or = ~length_or;
1582 for (length_unit_log = 0; length_or & 1; length_or >>= 1)
1583 length_unit_log++;
1585 fprintf (outf, "EXPORTED_CONST int length_unit_log = %u;\n", length_unit_log);
1588 /* Compute approximate cost of the expression. Used to decide whether
1589 expression is cheap enough for inline. */
1590 static int
1591 attr_rtx_cost (rtx x)
1593 int cost = 1;
1594 enum rtx_code code;
1595 if (!x)
1596 return 0;
1597 code = GET_CODE (x);
1598 switch (code)
1600 case MATCH_OPERAND:
1601 if (XSTR (x, 1)[0])
1602 return 10;
1603 else
1604 return 1;
1606 case EQ_ATTR_ALT:
1607 return 1;
1609 case EQ_ATTR:
1610 /* Alternatives don't result into function call. */
1611 if (!strcmp_check (XSTR (x, 0), alternative_name))
1612 return 1;
1613 else
1614 return 5;
1615 default:
1617 int i, j;
1618 const char *fmt = GET_RTX_FORMAT (code);
1619 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1621 switch (fmt[i])
1623 case 'V':
1624 case 'E':
1625 for (j = 0; j < XVECLEN (x, i); j++)
1626 cost += attr_rtx_cost (XVECEXP (x, i, j));
1627 break;
1628 case 'e':
1629 cost += attr_rtx_cost (XEXP (x, i));
1630 break;
1634 break;
1636 return cost;
1639 /* Take a COND expression and see if any of the conditions in it can be
1640 simplified. If any are known true or known false for the particular insn
1641 code, the COND can be further simplified.
1643 Also call ourselves on any COND operations that are values of this COND.
1645 We do not modify EXP; rather, we make and return a new rtx. */
1647 static rtx
1648 simplify_cond (rtx exp, int insn_code, int insn_index)
1650 int i, j;
1651 /* We store the desired contents here,
1652 then build a new expression if they don't match EXP. */
1653 rtx defval = XEXP (exp, 1);
1654 rtx new_defval = XEXP (exp, 1);
1655 int len = XVECLEN (exp, 0);
1656 rtx *tests = XNEWVEC (rtx, len);
1657 int allsame = 1;
1658 rtx ret;
1660 /* This lets us free all storage allocated below, if appropriate. */
1661 obstack_finish (rtl_obstack);
1663 memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
1665 /* See if default value needs simplification. */
1666 if (GET_CODE (defval) == COND)
1667 new_defval = simplify_cond (defval, insn_code, insn_index);
1669 /* Simplify the subexpressions, and see what tests we can get rid of. */
1671 for (i = 0; i < len; i += 2)
1673 rtx newtest, newval;
1675 /* Simplify this test. */
1676 newtest = simplify_test_exp_in_temp (tests[i], insn_code, insn_index);
1677 tests[i] = newtest;
1679 newval = tests[i + 1];
1680 /* See if this value may need simplification. */
1681 if (GET_CODE (newval) == COND)
1682 newval = simplify_cond (newval, insn_code, insn_index);
1684 /* Look for ways to delete or combine this test. */
1685 if (newtest == true_rtx)
1687 /* If test is true, make this value the default
1688 and discard this + any following tests. */
1689 len = i;
1690 defval = tests[i + 1];
1691 new_defval = newval;
1694 else if (newtest == false_rtx)
1696 /* If test is false, discard it and its value. */
1697 for (j = i; j < len - 2; j++)
1698 tests[j] = tests[j + 2];
1699 i -= 2;
1700 len -= 2;
1703 else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
1705 /* If this value and the value for the prev test are the same,
1706 merge the tests. */
1708 tests[i - 2]
1709 = insert_right_side (IOR, tests[i - 2], newtest,
1710 insn_code, insn_index);
1712 /* Delete this test/value. */
1713 for (j = i; j < len - 2; j++)
1714 tests[j] = tests[j + 2];
1715 len -= 2;
1716 i -= 2;
1719 else
1720 tests[i + 1] = newval;
1723 /* If the last test in a COND has the same value
1724 as the default value, that test isn't needed. */
1726 while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
1727 len -= 2;
1729 /* See if we changed anything. */
1730 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
1731 allsame = 0;
1732 else
1733 for (i = 0; i < len; i++)
1734 if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
1736 allsame = 0;
1737 break;
1740 if (len == 0)
1742 if (GET_CODE (defval) == COND)
1743 ret = simplify_cond (defval, insn_code, insn_index);
1744 else
1745 ret = defval;
1747 else if (allsame)
1748 ret = exp;
1749 else
1751 rtx newexp = rtx_alloc (COND);
1753 XVEC (newexp, 0) = rtvec_alloc (len);
1754 memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
1755 XEXP (newexp, 1) = new_defval;
1756 ret = newexp;
1758 free (tests);
1759 return ret;
1762 /* Remove an insn entry from an attribute value. */
1764 static void
1765 remove_insn_ent (struct attr_value *av, struct insn_ent *ie)
1767 struct insn_ent *previe;
1769 if (av->first_insn == ie)
1770 av->first_insn = ie->next;
1771 else
1773 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
1775 previe->next = ie->next;
1778 av->num_insns--;
1779 if (ie->def->insn_code == -1)
1780 av->has_asm_insn = 0;
1782 num_insn_ents--;
1785 /* Insert an insn entry in an attribute value list. */
1787 static void
1788 insert_insn_ent (struct attr_value *av, struct insn_ent *ie)
1790 ie->next = av->first_insn;
1791 av->first_insn = ie;
1792 av->num_insns++;
1793 if (ie->def->insn_code == -1)
1794 av->has_asm_insn = 1;
1796 num_insn_ents++;
1799 /* This is a utility routine to take an expression that is a tree of either
1800 AND or IOR expressions and insert a new term. The new term will be
1801 inserted at the right side of the first node whose code does not match
1802 the root. A new node will be created with the root's code. Its left
1803 side will be the old right side and its right side will be the new
1804 term.
1806 If the `term' is itself a tree, all its leaves will be inserted. */
1808 static rtx
1809 insert_right_side (enum rtx_code code, rtx exp, rtx term, int insn_code, int insn_index)
1811 rtx newexp;
1813 /* Avoid consing in some special cases. */
1814 if (code == AND && term == true_rtx)
1815 return exp;
1816 if (code == AND && term == false_rtx)
1817 return false_rtx;
1818 if (code == AND && exp == true_rtx)
1819 return term;
1820 if (code == AND && exp == false_rtx)
1821 return false_rtx;
1822 if (code == IOR && term == true_rtx)
1823 return true_rtx;
1824 if (code == IOR && term == false_rtx)
1825 return exp;
1826 if (code == IOR && exp == true_rtx)
1827 return true_rtx;
1828 if (code == IOR && exp == false_rtx)
1829 return term;
1830 if (attr_equal_p (exp, term))
1831 return exp;
1833 if (GET_CODE (term) == code)
1835 exp = insert_right_side (code, exp, XEXP (term, 0),
1836 insn_code, insn_index);
1837 exp = insert_right_side (code, exp, XEXP (term, 1),
1838 insn_code, insn_index);
1840 return exp;
1843 if (GET_CODE (exp) == code)
1845 rtx new_rtx = insert_right_side (code, XEXP (exp, 1),
1846 term, insn_code, insn_index);
1847 if (new_rtx != XEXP (exp, 1))
1848 /* Make a copy of this expression and call recursively. */
1849 newexp = attr_rtx (code, XEXP (exp, 0), new_rtx);
1850 else
1851 newexp = exp;
1853 else
1855 /* Insert the new term. */
1856 newexp = attr_rtx (code, exp, term);
1859 return simplify_test_exp_in_temp (newexp, insn_code, insn_index);
1862 /* If we have an expression which AND's a bunch of
1863 (not (eq_attrq "alternative" "n"))
1864 terms, we may have covered all or all but one of the possible alternatives.
1865 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
1867 This routine is passed an expression and either AND or IOR. It returns a
1868 bitmask indicating which alternatives are mentioned within EXP. */
1870 static uint64_t
1871 compute_alternative_mask (rtx exp, enum rtx_code code)
1873 const char *string;
1874 if (GET_CODE (exp) == code)
1875 return compute_alternative_mask (XEXP (exp, 0), code)
1876 | compute_alternative_mask (XEXP (exp, 1), code);
1878 else if (code == AND && GET_CODE (exp) == NOT
1879 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
1880 && XSTR (XEXP (exp, 0), 0) == alternative_name)
1881 string = XSTR (XEXP (exp, 0), 1);
1883 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
1884 && XSTR (exp, 0) == alternative_name)
1885 string = XSTR (exp, 1);
1887 else if (GET_CODE (exp) == EQ_ATTR_ALT)
1889 if (code == AND && XINT (exp, 1))
1890 return XINT (exp, 0);
1892 if (code == IOR && !XINT (exp, 1))
1893 return XINT (exp, 0);
1895 return 0;
1897 else
1898 return 0;
1900 if (string[1] == 0)
1901 return ((uint64_t) 1) << (string[0] - '0');
1902 return ((uint64_t) 1) << atoi (string);
1905 /* Given I, a single-bit mask, return RTX to compare the `alternative'
1906 attribute with the value represented by that bit. */
1908 static rtx
1909 make_alternative_compare (uint64_t mask)
1911 return mk_attr_alt (mask);
1914 /* If we are processing an (eq_attr "attr" "value") test, we find the value
1915 of "attr" for this insn code. From that value, we can compute a test
1916 showing when the EQ_ATTR will be true. This routine performs that
1917 computation. If a test condition involves an address, we leave the EQ_ATTR
1918 intact because addresses are only valid for the `length' attribute.
1920 EXP is the EQ_ATTR expression and ATTR is the attribute to which
1921 it refers. VALUE is the value of that attribute for the insn
1922 corresponding to INSN_CODE and INSN_INDEX. */
1924 static rtx
1925 evaluate_eq_attr (rtx exp, struct attr_desc *attr, rtx value,
1926 int insn_code, int insn_index)
1928 rtx orexp, andexp;
1929 rtx right;
1930 rtx newexp;
1931 int i;
1933 while (GET_CODE (value) == ATTR)
1935 struct attr_value *av = NULL;
1937 attr = find_attr (&XSTR (value, 0), 0);
1939 if (insn_code_values)
1941 struct attr_value_list *iv;
1942 for (iv = insn_code_values[insn_code]; iv; iv = iv->next)
1943 if (iv->attr == attr)
1945 av = iv->av;
1946 break;
1949 else
1951 struct insn_ent *ie;
1952 for (av = attr->first_value; av; av = av->next)
1953 for (ie = av->first_insn; ie; ie = ie->next)
1954 if (ie->def->insn_code == insn_code)
1955 goto got_av;
1957 if (av)
1959 got_av:
1960 value = av->value;
1964 switch (GET_CODE (value))
1966 case CONST_STRING:
1967 if (! strcmp_check (XSTR (value, 0), XSTR (exp, 1)))
1968 newexp = true_rtx;
1969 else
1970 newexp = false_rtx;
1971 break;
1973 case SYMBOL_REF:
1975 const char *prefix;
1976 char *string, *p;
1978 gcc_assert (GET_CODE (exp) == EQ_ATTR);
1979 prefix = attr->enum_name ? attr->enum_name : attr->name;
1980 string = ACONCAT ((prefix, "_", XSTR (exp, 1), NULL));
1981 for (p = string; *p; p++)
1982 *p = TOUPPER (*p);
1984 newexp = attr_rtx (EQ, value,
1985 attr_rtx (SYMBOL_REF,
1986 DEF_ATTR_STRING (string)));
1987 break;
1990 case COND:
1991 /* We construct an IOR of all the cases for which the
1992 requested attribute value is present. Since we start with
1993 FALSE, if it is not present, FALSE will be returned.
1995 Each case is the AND of the NOT's of the previous conditions with the
1996 current condition; in the default case the current condition is TRUE.
1998 For each possible COND value, call ourselves recursively.
2000 The extra TRUE and FALSE expressions will be eliminated by another
2001 call to the simplification routine. */
2003 orexp = false_rtx;
2004 andexp = true_rtx;
2006 for (i = 0; i < XVECLEN (value, 0); i += 2)
2008 rtx this_cond = simplify_test_exp_in_temp (XVECEXP (value, 0, i),
2009 insn_code, insn_index);
2011 right = insert_right_side (AND, andexp, this_cond,
2012 insn_code, insn_index);
2013 right = insert_right_side (AND, right,
2014 evaluate_eq_attr (exp, attr,
2015 XVECEXP (value, 0,
2016 i + 1),
2017 insn_code, insn_index),
2018 insn_code, insn_index);
2019 orexp = insert_right_side (IOR, orexp, right,
2020 insn_code, insn_index);
2022 /* Add this condition into the AND expression. */
2023 newexp = attr_rtx (NOT, this_cond);
2024 andexp = insert_right_side (AND, andexp, newexp,
2025 insn_code, insn_index);
2028 /* Handle the default case. */
2029 right = insert_right_side (AND, andexp,
2030 evaluate_eq_attr (exp, attr, XEXP (value, 1),
2031 insn_code, insn_index),
2032 insn_code, insn_index);
2033 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2034 break;
2036 default:
2037 gcc_unreachable ();
2040 /* If uses an address, must return original expression. But set the
2041 ATTR_IND_SIMPLIFIED_P bit so we don't try to simplify it again. */
2043 address_used = 0;
2044 walk_attr_value (newexp);
2046 if (address_used)
2048 if (! ATTR_IND_SIMPLIFIED_P (exp))
2049 return copy_rtx_unchanging (exp);
2050 return exp;
2052 else
2053 return newexp;
2056 /* This routine is called when an AND of a term with a tree of AND's is
2057 encountered. If the term or its complement is present in the tree, it
2058 can be replaced with TRUE or FALSE, respectively.
2060 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2061 be true and hence are complementary.
2063 There is one special case: If we see
2064 (and (not (eq_attr "att" "v1"))
2065 (eq_attr "att" "v2"))
2066 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2067 replace the term, not anything in the AND tree. So we pass a pointer to
2068 the term. */
2070 static rtx
2071 simplify_and_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2073 rtx left, right;
2074 rtx newexp;
2075 rtx temp;
2076 int left_eliminates_term, right_eliminates_term;
2078 if (GET_CODE (exp) == AND)
2080 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2081 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2082 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2084 newexp = attr_rtx (AND, left, right);
2086 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2090 else if (GET_CODE (exp) == IOR)
2092 /* For the IOR case, we do the same as above, except that we can
2093 only eliminate `term' if both sides of the IOR would do so. */
2094 temp = *pterm;
2095 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2096 left_eliminates_term = (temp == true_rtx);
2098 temp = *pterm;
2099 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2100 right_eliminates_term = (temp == true_rtx);
2102 if (left_eliminates_term && right_eliminates_term)
2103 *pterm = true_rtx;
2105 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2107 newexp = attr_rtx (IOR, left, right);
2109 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2113 /* Check for simplifications. Do some extra checking here since this
2114 routine is called so many times. */
2116 if (exp == *pterm)
2117 return true_rtx;
2119 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2120 return false_rtx;
2122 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2123 return false_rtx;
2125 else if (GET_CODE (exp) == EQ_ATTR_ALT && GET_CODE (*pterm) == EQ_ATTR_ALT)
2127 if (attr_alt_subset_p (*pterm, exp))
2128 return true_rtx;
2130 if (attr_alt_subset_of_compl_p (*pterm, exp))
2131 return false_rtx;
2133 if (attr_alt_subset_p (exp, *pterm))
2134 *pterm = true_rtx;
2136 return exp;
2139 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2141 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2142 return exp;
2144 if (! strcmp_check (XSTR (exp, 1), XSTR (*pterm, 1)))
2145 return true_rtx;
2146 else
2147 return false_rtx;
2150 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2151 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2153 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2154 return exp;
2156 if (! strcmp_check (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2157 return false_rtx;
2158 else
2159 return true_rtx;
2162 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2163 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2165 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2166 return exp;
2168 if (! strcmp_check (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2169 return false_rtx;
2170 else
2171 *pterm = true_rtx;
2174 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2176 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2177 return true_rtx;
2180 else if (GET_CODE (exp) == NOT)
2182 if (attr_equal_p (XEXP (exp, 0), *pterm))
2183 return false_rtx;
2186 else if (GET_CODE (*pterm) == NOT)
2188 if (attr_equal_p (XEXP (*pterm, 0), exp))
2189 return false_rtx;
2192 else if (attr_equal_p (exp, *pterm))
2193 return true_rtx;
2195 return exp;
2198 /* Similar to `simplify_and_tree', but for IOR trees. */
2200 static rtx
2201 simplify_or_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2203 rtx left, right;
2204 rtx newexp;
2205 rtx temp;
2206 int left_eliminates_term, right_eliminates_term;
2208 if (GET_CODE (exp) == IOR)
2210 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2211 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2212 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2214 newexp = attr_rtx (GET_CODE (exp), left, right);
2216 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2220 else if (GET_CODE (exp) == AND)
2222 /* For the AND case, we do the same as above, except that we can
2223 only eliminate `term' if both sides of the AND would do so. */
2224 temp = *pterm;
2225 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2226 left_eliminates_term = (temp == false_rtx);
2228 temp = *pterm;
2229 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2230 right_eliminates_term = (temp == false_rtx);
2232 if (left_eliminates_term && right_eliminates_term)
2233 *pterm = false_rtx;
2235 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2237 newexp = attr_rtx (GET_CODE (exp), left, right);
2239 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2243 if (attr_equal_p (exp, *pterm))
2244 return false_rtx;
2246 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
2247 return true_rtx;
2249 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
2250 return true_rtx;
2252 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2253 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2254 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
2255 *pterm = false_rtx;
2257 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2258 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
2259 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
2260 return false_rtx;
2262 return exp;
2265 /* Simplify test expression and use temporary obstack in order to avoid
2266 memory bloat. Use ATTR_IND_SIMPLIFIED to avoid unnecessary simplifications
2267 and avoid unnecessary copying if possible. */
2269 static rtx
2270 simplify_test_exp_in_temp (rtx exp, int insn_code, int insn_index)
2272 rtx x;
2273 struct obstack *old;
2274 if (ATTR_IND_SIMPLIFIED_P (exp))
2275 return exp;
2276 old = rtl_obstack;
2277 rtl_obstack = temp_obstack;
2278 x = simplify_test_exp (exp, insn_code, insn_index);
2279 rtl_obstack = old;
2280 return x;
2283 /* Returns true if S1 is a subset of S2. */
2285 static bool
2286 attr_alt_subset_p (rtx s1, rtx s2)
2288 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2290 case (0 << 1) | 0:
2291 return !(XINT (s1, 0) &~ XINT (s2, 0));
2293 case (0 << 1) | 1:
2294 return !(XINT (s1, 0) & XINT (s2, 0));
2296 case (1 << 1) | 0:
2297 return false;
2299 case (1 << 1) | 1:
2300 return !(XINT (s2, 0) &~ XINT (s1, 0));
2302 default:
2303 gcc_unreachable ();
2307 /* Returns true if S1 is a subset of complement of S2. */
2309 static bool
2310 attr_alt_subset_of_compl_p (rtx s1, rtx s2)
2312 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2314 case (0 << 1) | 0:
2315 return !(XINT (s1, 0) & XINT (s2, 0));
2317 case (0 << 1) | 1:
2318 return !(XINT (s1, 0) & ~XINT (s2, 0));
2320 case (1 << 1) | 0:
2321 return !(XINT (s2, 0) &~ XINT (s1, 0));
2323 case (1 << 1) | 1:
2324 return false;
2326 default:
2327 gcc_unreachable ();
2331 /* Return EQ_ATTR_ALT expression representing intersection of S1 and S2. */
2333 static rtx
2334 attr_alt_intersection (rtx s1, rtx s2)
2336 int result;
2338 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2340 case (0 << 1) | 0:
2341 result = XINT (s1, 0) & XINT (s2, 0);
2342 break;
2343 case (0 << 1) | 1:
2344 result = XINT (s1, 0) & ~XINT (s2, 0);
2345 break;
2346 case (1 << 1) | 0:
2347 result = XINT (s2, 0) & ~XINT (s1, 0);
2348 break;
2349 case (1 << 1) | 1:
2350 result = XINT (s1, 0) | XINT (s2, 0);
2351 break;
2352 default:
2353 gcc_unreachable ();
2356 return attr_rtx (EQ_ATTR_ALT, result, XINT (s1, 1) & XINT (s2, 1));
2359 /* Return EQ_ATTR_ALT expression representing union of S1 and S2. */
2361 static rtx
2362 attr_alt_union (rtx s1, rtx s2)
2364 int result;
2366 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2368 case (0 << 1) | 0:
2369 result = XINT (s1, 0) | XINT (s2, 0);
2370 break;
2371 case (0 << 1) | 1:
2372 result = XINT (s2, 0) & ~XINT (s1, 0);
2373 break;
2374 case (1 << 1) | 0:
2375 result = XINT (s1, 0) & ~XINT (s2, 0);
2376 break;
2377 case (1 << 1) | 1:
2378 result = XINT (s1, 0) & XINT (s2, 0);
2379 break;
2380 default:
2381 gcc_unreachable ();
2384 return attr_rtx (EQ_ATTR_ALT, result, XINT (s1, 1) | XINT (s2, 1));
2387 /* Return EQ_ATTR_ALT expression representing complement of S. */
2389 static rtx
2390 attr_alt_complement (rtx s)
2392 return attr_rtx (EQ_ATTR_ALT, XINT (s, 0), 1 - XINT (s, 1));
2395 /* Return EQ_ATTR_ALT expression representing set containing elements set
2396 in E. */
2398 static rtx
2399 mk_attr_alt (uint64_t e)
2401 return attr_rtx (EQ_ATTR_ALT, (int)e, 0);
2404 /* Given an expression, see if it can be simplified for a particular insn
2405 code based on the values of other attributes being tested. This can
2406 eliminate nested get_attr_... calls.
2408 Note that if an endless recursion is specified in the patterns, the
2409 optimization will loop. However, it will do so in precisely the cases where
2410 an infinite recursion loop could occur during compilation. It's better that
2411 it occurs here! */
2413 static rtx
2414 simplify_test_exp (rtx exp, int insn_code, int insn_index)
2416 rtx left, right;
2417 struct attr_desc *attr;
2418 struct attr_value *av;
2419 struct insn_ent *ie;
2420 struct attr_value_list *iv;
2421 uint64_t i;
2422 rtx newexp = exp;
2423 bool left_alt, right_alt;
2425 /* Don't re-simplify something we already simplified. */
2426 if (ATTR_IND_SIMPLIFIED_P (exp) || ATTR_CURR_SIMPLIFIED_P (exp))
2427 return exp;
2429 switch (GET_CODE (exp))
2431 case AND:
2432 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2433 if (left == false_rtx)
2434 return false_rtx;
2435 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2436 if (right == false_rtx)
2437 return false_rtx;
2439 if (GET_CODE (left) == EQ_ATTR_ALT
2440 && GET_CODE (right) == EQ_ATTR_ALT)
2442 exp = attr_alt_intersection (left, right);
2443 return simplify_test_exp (exp, insn_code, insn_index);
2446 /* If either side is an IOR and we have (eq_attr "alternative" ..")
2447 present on both sides, apply the distributive law since this will
2448 yield simplifications. */
2449 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
2450 && compute_alternative_mask (left, IOR)
2451 && compute_alternative_mask (right, IOR))
2453 if (GET_CODE (left) == IOR)
2454 std::swap (left, right);
2456 newexp = attr_rtx (IOR,
2457 attr_rtx (AND, left, XEXP (right, 0)),
2458 attr_rtx (AND, left, XEXP (right, 1)));
2460 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2463 /* Try with the term on both sides. */
2464 right = simplify_and_tree (right, &left, insn_code, insn_index);
2465 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2466 left = simplify_and_tree (left, &right, insn_code, insn_index);
2468 if (left == false_rtx || right == false_rtx)
2469 return false_rtx;
2470 else if (left == true_rtx)
2472 return right;
2474 else if (right == true_rtx)
2476 return left;
2478 /* See if all or all but one of the insn's alternatives are specified
2479 in this tree. Optimize if so. */
2481 if (GET_CODE (left) == NOT)
2482 left_alt = (GET_CODE (XEXP (left, 0)) == EQ_ATTR
2483 && XSTR (XEXP (left, 0), 0) == alternative_name);
2484 else
2485 left_alt = (GET_CODE (left) == EQ_ATTR_ALT
2486 && XINT (left, 1));
2488 if (GET_CODE (right) == NOT)
2489 right_alt = (GET_CODE (XEXP (right, 0)) == EQ_ATTR
2490 && XSTR (XEXP (right, 0), 0) == alternative_name);
2491 else
2492 right_alt = (GET_CODE (right) == EQ_ATTR_ALT
2493 && XINT (right, 1));
2495 if (insn_code >= 0
2496 && (GET_CODE (left) == AND
2497 || left_alt
2498 || GET_CODE (right) == AND
2499 || right_alt))
2501 i = compute_alternative_mask (exp, AND);
2502 if (i & ~insn_alternatives[insn_code])
2503 fatal ("invalid alternative specified for pattern number %d",
2504 insn_index);
2506 /* If all alternatives are excluded, this is false. */
2507 i ^= insn_alternatives[insn_code];
2508 if (i == 0)
2509 return false_rtx;
2510 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2512 /* If just one excluded, AND a comparison with that one to the
2513 front of the tree. The others will be eliminated by
2514 optimization. We do not want to do this if the insn has one
2515 alternative and we have tested none of them! */
2516 left = make_alternative_compare (i);
2517 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2518 newexp = attr_rtx (AND, left, right);
2520 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2524 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2526 newexp = attr_rtx (AND, left, right);
2527 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2529 break;
2531 case IOR:
2532 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2533 if (left == true_rtx)
2534 return true_rtx;
2535 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2536 if (right == true_rtx)
2537 return true_rtx;
2539 if (GET_CODE (left) == EQ_ATTR_ALT
2540 && GET_CODE (right) == EQ_ATTR_ALT)
2542 exp = attr_alt_union (left, right);
2543 return simplify_test_exp (exp, insn_code, insn_index);
2546 right = simplify_or_tree (right, &left, insn_code, insn_index);
2547 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2548 left = simplify_or_tree (left, &right, insn_code, insn_index);
2550 if (right == true_rtx || left == true_rtx)
2551 return true_rtx;
2552 else if (left == false_rtx)
2554 return right;
2556 else if (right == false_rtx)
2558 return left;
2561 /* Test for simple cases where the distributive law is useful. I.e.,
2562 convert (ior (and (x) (y))
2563 (and (x) (z)))
2564 to (and (x)
2565 (ior (y) (z)))
2568 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
2569 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
2571 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
2573 left = XEXP (left, 0);
2574 right = newexp;
2575 newexp = attr_rtx (AND, left, right);
2576 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2579 /* Similarly,
2580 convert (ior (and (y) (x))
2581 (and (z) (x)))
2582 to (and (ior (y) (z))
2583 (x))
2584 Note that we want the common term to stay at the end.
2587 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
2588 && attr_equal_p (XEXP (left, 1), XEXP (right, 1)))
2590 newexp = attr_rtx (IOR, XEXP (left, 0), XEXP (right, 0));
2592 left = newexp;
2593 right = XEXP (right, 1);
2594 newexp = attr_rtx (AND, left, right);
2595 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2598 /* See if all or all but one of the insn's alternatives are specified
2599 in this tree. Optimize if so. */
2601 else if (insn_code >= 0
2602 && (GET_CODE (left) == IOR
2603 || (GET_CODE (left) == EQ_ATTR_ALT
2604 && !XINT (left, 1))
2605 || (GET_CODE (left) == EQ_ATTR
2606 && XSTR (left, 0) == alternative_name)
2607 || GET_CODE (right) == IOR
2608 || (GET_CODE (right) == EQ_ATTR_ALT
2609 && !XINT (right, 1))
2610 || (GET_CODE (right) == EQ_ATTR
2611 && XSTR (right, 0) == alternative_name)))
2613 i = compute_alternative_mask (exp, IOR);
2614 if (i & ~insn_alternatives[insn_code])
2615 fatal ("invalid alternative specified for pattern number %d",
2616 insn_index);
2618 /* If all alternatives are included, this is true. */
2619 i ^= insn_alternatives[insn_code];
2620 if (i == 0)
2621 return true_rtx;
2622 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2624 /* If just one excluded, IOR a comparison with that one to the
2625 front of the tree. The others will be eliminated by
2626 optimization. We do not want to do this if the insn has one
2627 alternative and we have tested none of them! */
2628 left = make_alternative_compare (i);
2629 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2630 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
2632 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2636 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2638 newexp = attr_rtx (IOR, left, right);
2639 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2641 break;
2643 case NOT:
2644 if (GET_CODE (XEXP (exp, 0)) == NOT)
2646 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
2647 insn_code, insn_index);
2648 return left;
2651 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2652 if (GET_CODE (left) == NOT)
2653 return XEXP (left, 0);
2655 if (left == false_rtx)
2656 return true_rtx;
2657 if (left == true_rtx)
2658 return false_rtx;
2660 if (GET_CODE (left) == EQ_ATTR_ALT)
2662 exp = attr_alt_complement (left);
2663 return simplify_test_exp (exp, insn_code, insn_index);
2666 /* Try to apply De`Morgan's laws. */
2667 if (GET_CODE (left) == IOR)
2669 newexp = attr_rtx (AND,
2670 attr_rtx (NOT, XEXP (left, 0)),
2671 attr_rtx (NOT, XEXP (left, 1)));
2673 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2675 else if (GET_CODE (left) == AND)
2677 newexp = attr_rtx (IOR,
2678 attr_rtx (NOT, XEXP (left, 0)),
2679 attr_rtx (NOT, XEXP (left, 1)));
2681 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2683 else if (left != XEXP (exp, 0))
2685 newexp = attr_rtx (NOT, left);
2687 break;
2689 case EQ_ATTR_ALT:
2690 if (!XINT (exp, 0))
2691 return XINT (exp, 1) ? true_rtx : false_rtx;
2692 break;
2694 case EQ_ATTR:
2695 if (XSTR (exp, 0) == alternative_name)
2697 newexp = mk_attr_alt (((uint64_t) 1) << atoi (XSTR (exp, 1)));
2698 break;
2701 /* Look at the value for this insn code in the specified attribute.
2702 We normally can replace this comparison with the condition that
2703 would give this insn the values being tested for. */
2704 if (insn_code >= 0
2705 && (attr = find_attr (&XSTR (exp, 0), 0)) != NULL)
2707 rtx x;
2709 av = NULL;
2710 if (insn_code_values)
2712 for (iv = insn_code_values[insn_code]; iv; iv = iv->next)
2713 if (iv->attr == attr)
2715 av = iv->av;
2716 break;
2719 else
2721 for (av = attr->first_value; av; av = av->next)
2722 for (ie = av->first_insn; ie; ie = ie->next)
2723 if (ie->def->insn_code == insn_code)
2724 goto got_av;
2727 if (av)
2729 got_av:
2730 x = evaluate_eq_attr (exp, attr, av->value,
2731 insn_code, insn_index);
2732 x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
2733 if (attr_rtx_cost (x) < 7)
2734 return x;
2737 break;
2739 default:
2740 break;
2743 /* We have already simplified this expression. Simplifying it again
2744 won't buy anything unless we weren't given a valid insn code
2745 to process (i.e., we are canonicalizing something.). */
2746 if (insn_code != -2
2747 && ! ATTR_IND_SIMPLIFIED_P (newexp))
2748 return copy_rtx_unchanging (newexp);
2750 return newexp;
2753 /* Return 1 if any EQ_ATTR subexpression of P refers to ATTR,
2754 otherwise return 0. */
2756 static int
2757 tests_attr_p (rtx p, struct attr_desc *attr)
2759 const char *fmt;
2760 int i, ie, j, je;
2762 if (GET_CODE (p) == EQ_ATTR)
2764 if (XSTR (p, 0) != attr->name)
2765 return 0;
2766 return 1;
2769 fmt = GET_RTX_FORMAT (GET_CODE (p));
2770 ie = GET_RTX_LENGTH (GET_CODE (p));
2771 for (i = 0; i < ie; i++)
2773 switch (*fmt++)
2775 case 'e':
2776 if (tests_attr_p (XEXP (p, i), attr))
2777 return 1;
2778 break;
2780 case 'E':
2781 je = XVECLEN (p, i);
2782 for (j = 0; j < je; ++j)
2783 if (tests_attr_p (XVECEXP (p, i, j), attr))
2784 return 1;
2785 break;
2789 return 0;
2792 /* Calculate a topological sorting of all attributes so that
2793 all attributes only depend on attributes in front of it.
2794 Place the result in *RET (which is a pointer to an array of
2795 attr_desc pointers), and return the size of that array. */
2797 static int
2798 get_attr_order (struct attr_desc ***ret)
2800 int i, j;
2801 int num = 0;
2802 struct attr_desc *attr;
2803 struct attr_desc **all, **sorted;
2804 char *handled;
2805 for (i = 0; i < MAX_ATTRS_INDEX; i++)
2806 for (attr = attrs[i]; attr; attr = attr->next)
2807 num++;
2808 all = XNEWVEC (struct attr_desc *, num);
2809 sorted = XNEWVEC (struct attr_desc *, num);
2810 handled = XCNEWVEC (char, num);
2811 num = 0;
2812 for (i = 0; i < MAX_ATTRS_INDEX; i++)
2813 for (attr = attrs[i]; attr; attr = attr->next)
2814 all[num++] = attr;
2816 j = 0;
2817 for (i = 0; i < num; i++)
2818 if (all[i]->is_const)
2819 handled[i] = 1, sorted[j++] = all[i];
2821 /* We have only few attributes hence we can live with the inner
2822 loop being O(n^2), unlike the normal fast variants of topological
2823 sorting. */
2824 while (j < num)
2826 for (i = 0; i < num; i++)
2827 if (!handled[i])
2829 /* Let's see if I depends on anything interesting. */
2830 int k;
2831 for (k = 0; k < num; k++)
2832 if (!handled[k])
2834 struct attr_value *av;
2835 for (av = all[i]->first_value; av; av = av->next)
2836 if (av->num_insns != 0)
2837 if (tests_attr_p (av->value, all[k]))
2838 break;
2840 if (av)
2841 /* Something in I depends on K. */
2842 break;
2844 if (k == num)
2846 /* Nothing in I depended on anything intersting, so
2847 it's done. */
2848 handled[i] = 1;
2849 sorted[j++] = all[i];
2854 if (DEBUG)
2855 for (j = 0; j < num; j++)
2857 struct attr_desc *attr2;
2858 struct attr_value *av;
2860 attr = sorted[j];
2861 fprintf (stderr, "%s depends on: ", attr->name);
2862 for (i = 0; i < MAX_ATTRS_INDEX; ++i)
2863 for (attr2 = attrs[i]; attr2; attr2 = attr2->next)
2864 if (!attr2->is_const)
2865 for (av = attr->first_value; av; av = av->next)
2866 if (av->num_insns != 0)
2867 if (tests_attr_p (av->value, attr2))
2869 fprintf (stderr, "%s, ", attr2->name);
2870 break;
2872 fprintf (stderr, "\n");
2875 free (all);
2876 *ret = sorted;
2877 return num;
2880 /* Optimize the attribute lists by seeing if we can determine conditional
2881 values from the known values of other attributes. This will save subroutine
2882 calls during the compilation. NUM_INSN_CODES is the number of unique
2883 instruction codes. */
2885 static void
2886 optimize_attrs (int num_insn_codes)
2888 struct attr_desc *attr;
2889 struct attr_value *av;
2890 struct insn_ent *ie;
2891 rtx newexp;
2892 int i;
2893 struct attr_value_list *ivbuf;
2894 struct attr_value_list *iv;
2895 struct attr_desc **topsort;
2896 int topnum;
2898 /* For each insn code, make a list of all the insn_ent's for it,
2899 for all values for all attributes. */
2901 if (num_insn_ents == 0)
2902 return;
2904 /* Make 2 extra elements, for "code" values -2 and -1. */
2905 insn_code_values = XCNEWVEC (struct attr_value_list *, num_insn_codes + 2);
2907 /* Offset the table address so we can index by -2 or -1. */
2908 insn_code_values += 2;
2910 iv = ivbuf = XNEWVEC (struct attr_value_list, num_insn_ents);
2912 /* Create the chain of insn*attr values such that we see dependend
2913 attributes after their dependencies. As we use a stack via the
2914 next pointers start from the end of the topological order. */
2915 topnum = get_attr_order (&topsort);
2916 for (i = topnum - 1; i >= 0; i--)
2917 for (av = topsort[i]->first_value; av; av = av->next)
2918 for (ie = av->first_insn; ie; ie = ie->next)
2920 iv->attr = topsort[i];
2921 iv->av = av;
2922 iv->ie = ie;
2923 iv->next = insn_code_values[ie->def->insn_code];
2924 insn_code_values[ie->def->insn_code] = iv;
2925 iv++;
2927 free (topsort);
2929 /* Sanity check on num_insn_ents. */
2930 gcc_assert (iv == ivbuf + num_insn_ents);
2932 /* Process one insn code at a time. */
2933 for (i = -2; i < num_insn_codes; i++)
2935 /* Clear the ATTR_CURR_SIMPLIFIED_P flag everywhere relevant.
2936 We use it to mean "already simplified for this insn". */
2937 for (iv = insn_code_values[i]; iv; iv = iv->next)
2938 clear_struct_flag (iv->av->value);
2940 for (iv = insn_code_values[i]; iv; iv = iv->next)
2942 struct obstack *old = rtl_obstack;
2944 attr = iv->attr;
2945 av = iv->av;
2946 ie = iv->ie;
2947 if (GET_CODE (av->value) != COND)
2948 continue;
2950 rtl_obstack = temp_obstack;
2951 newexp = av->value;
2952 while (GET_CODE (newexp) == COND)
2954 rtx newexp2 = simplify_cond (newexp, ie->def->insn_code,
2955 ie->def->insn_index);
2956 if (newexp2 == newexp)
2957 break;
2958 newexp = newexp2;
2961 rtl_obstack = old;
2962 /* If we created a new value for this instruction, and it's
2963 cheaper than the old value, and overall cheap, use that
2964 one as specific value for the current instruction.
2965 The last test is to avoid exploding the get_attr_ function
2966 sizes for no much gain. */
2967 if (newexp != av->value
2968 && attr_rtx_cost (newexp) < attr_rtx_cost (av->value)
2969 && attr_rtx_cost (newexp) < 26
2972 remove_insn_ent (av, ie);
2973 av = get_attr_value (ie->def->loc, newexp, attr,
2974 ie->def->insn_code);
2975 iv->av = av;
2976 insert_insn_ent (av, ie);
2981 free (ivbuf);
2982 free (insn_code_values - 2);
2983 insn_code_values = NULL;
2986 /* Clear the ATTR_CURR_SIMPLIFIED_P flag in EXP and its subexpressions. */
2988 static void
2989 clear_struct_flag (rtx x)
2991 int i;
2992 int j;
2993 enum rtx_code code;
2994 const char *fmt;
2996 ATTR_CURR_SIMPLIFIED_P (x) = 0;
2997 if (ATTR_IND_SIMPLIFIED_P (x))
2998 return;
3000 code = GET_CODE (x);
3002 switch (code)
3004 case REG:
3005 CASE_CONST_ANY:
3006 case MATCH_TEST:
3007 case SYMBOL_REF:
3008 case CODE_LABEL:
3009 case PC:
3010 case CC0:
3011 case EQ_ATTR:
3012 case ATTR_FLAG:
3013 return;
3015 default:
3016 break;
3019 /* Compare the elements. If any pair of corresponding elements
3020 fail to match, return 0 for the whole things. */
3022 fmt = GET_RTX_FORMAT (code);
3023 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3025 switch (fmt[i])
3027 case 'V':
3028 case 'E':
3029 for (j = 0; j < XVECLEN (x, i); j++)
3030 clear_struct_flag (XVECEXP (x, i, j));
3031 break;
3033 case 'e':
3034 clear_struct_flag (XEXP (x, i));
3035 break;
3040 /* Add attribute value NAME to the beginning of ATTR's list. */
3042 static void
3043 add_attr_value (struct attr_desc *attr, const char *name)
3045 struct attr_value *av;
3047 av = oballoc (struct attr_value);
3048 av->value = attr_rtx (CONST_STRING, name);
3049 av->next = attr->first_value;
3050 attr->first_value = av;
3051 av->first_insn = NULL;
3052 av->num_insns = 0;
3053 av->has_asm_insn = 0;
3056 /* Create table entries for DEFINE_ATTR or DEFINE_ENUM_ATTR. */
3058 static void
3059 gen_attr (md_rtx_info *info)
3061 struct enum_type *et;
3062 struct enum_value *ev;
3063 struct attr_desc *attr;
3064 const char *name_ptr;
3065 char *p;
3066 rtx def = info->def;
3068 /* Make a new attribute structure. Check for duplicate by looking at
3069 attr->default_val, since it is initialized by this routine. */
3070 attr = find_attr (&XSTR (def, 0), 1);
3071 if (attr->default_val)
3073 error_at (info->loc, "duplicate definition for attribute %s",
3074 attr->name);
3075 message_at (attr->loc, "previous definition");
3076 return;
3078 attr->loc = info->loc;
3080 if (GET_CODE (def) == DEFINE_ENUM_ATTR)
3082 attr->enum_name = XSTR (def, 1);
3083 et = rtx_reader_ptr->lookup_enum_type (XSTR (def, 1));
3084 if (!et || !et->md_p)
3085 error_at (info->loc, "No define_enum called `%s' defined",
3086 attr->name);
3087 if (et)
3088 for (ev = et->values; ev; ev = ev->next)
3089 add_attr_value (attr, ev->name);
3091 else if (*XSTR (def, 1) == '\0')
3092 attr->is_numeric = 1;
3093 else
3095 name_ptr = XSTR (def, 1);
3096 while ((p = next_comma_elt (&name_ptr)) != NULL)
3097 add_attr_value (attr, p);
3100 if (GET_CODE (XEXP (def, 2)) == CONST)
3102 attr->is_const = 1;
3103 if (attr->is_numeric)
3104 error_at (info->loc,
3105 "constant attributes may not take numeric values");
3107 /* Get rid of the CONST node. It is allowed only at top-level. */
3108 XEXP (def, 2) = XEXP (XEXP (def, 2), 0);
3111 if (! strcmp_check (attr->name, length_str) && ! attr->is_numeric)
3112 error_at (info->loc, "`length' attribute must take numeric values");
3114 /* Set up the default value. */
3115 XEXP (def, 2) = check_attr_value (info->loc, XEXP (def, 2), attr);
3116 attr->default_val = get_attr_value (info->loc, XEXP (def, 2), attr, -2);
3119 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
3120 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
3121 number of alternatives as this should be checked elsewhere. */
3123 static int
3124 count_alternatives (rtx exp)
3126 int i, j, n;
3127 const char *fmt;
3129 if (GET_CODE (exp) == MATCH_OPERAND)
3130 return n_comma_elts (XSTR (exp, 2));
3132 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3133 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3134 switch (*fmt++)
3136 case 'e':
3137 case 'u':
3138 n = count_alternatives (XEXP (exp, i));
3139 if (n)
3140 return n;
3141 break;
3143 case 'E':
3144 case 'V':
3145 if (XVEC (exp, i) != NULL)
3146 for (j = 0; j < XVECLEN (exp, i); j++)
3148 n = count_alternatives (XVECEXP (exp, i, j));
3149 if (n)
3150 return n;
3154 return 0;
3157 /* Returns nonzero if the given expression contains an EQ_ATTR with the
3158 `alternative' attribute. */
3160 static int
3161 compares_alternatives_p (rtx exp)
3163 int i, j;
3164 const char *fmt;
3166 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
3167 return 1;
3169 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3170 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3171 switch (*fmt++)
3173 case 'e':
3174 case 'u':
3175 if (compares_alternatives_p (XEXP (exp, i)))
3176 return 1;
3177 break;
3179 case 'E':
3180 for (j = 0; j < XVECLEN (exp, i); j++)
3181 if (compares_alternatives_p (XVECEXP (exp, i, j)))
3182 return 1;
3183 break;
3186 return 0;
3189 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
3191 static void
3192 gen_insn (md_rtx_info *info)
3194 struct insn_def *id;
3195 rtx def = info->def;
3197 id = oballoc (struct insn_def);
3198 id->next = defs;
3199 defs = id;
3200 id->def = def;
3201 id->loc = info->loc;
3203 switch (GET_CODE (def))
3205 case DEFINE_INSN:
3206 id->insn_code = info->index;
3207 id->insn_index = insn_index_number;
3208 id->num_alternatives = count_alternatives (def);
3209 if (id->num_alternatives == 0)
3210 id->num_alternatives = 1;
3211 id->vec_idx = 4;
3212 break;
3214 case DEFINE_PEEPHOLE:
3215 id->insn_code = info->index;
3216 id->insn_index = insn_index_number;
3217 id->num_alternatives = count_alternatives (def);
3218 if (id->num_alternatives == 0)
3219 id->num_alternatives = 1;
3220 id->vec_idx = 3;
3221 break;
3223 case DEFINE_ASM_ATTRIBUTES:
3224 id->insn_code = -1;
3225 id->insn_index = -1;
3226 id->num_alternatives = 1;
3227 id->vec_idx = 0;
3228 got_define_asm_attributes = 1;
3229 break;
3231 default:
3232 gcc_unreachable ();
3236 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
3237 true or annul false is specified, and make a `struct delay_desc'. */
3239 static void
3240 gen_delay (md_rtx_info *info)
3242 struct delay_desc *delay;
3243 int i;
3245 rtx def = info->def;
3246 if (XVECLEN (def, 1) % 3 != 0)
3248 error_at (info->loc, "number of elements in DEFINE_DELAY must"
3249 " be multiple of three");
3250 return;
3253 for (i = 0; i < XVECLEN (def, 1); i += 3)
3255 if (XVECEXP (def, 1, i + 1))
3256 have_annul_true = 1;
3257 if (XVECEXP (def, 1, i + 2))
3258 have_annul_false = 1;
3261 delay = oballoc (struct delay_desc);
3262 delay->def = def;
3263 delay->num = ++num_delays;
3264 delay->next = delays;
3265 delay->loc = info->loc;
3266 delays = delay;
3269 /* Names of attributes that could be possibly cached. */
3270 static const char *cached_attrs[32];
3271 /* Number of such attributes. */
3272 static int cached_attr_count;
3273 /* Bitmasks of possibly cached attributes. */
3274 static unsigned int attrs_seen_once, attrs_seen_more_than_once;
3275 static unsigned int attrs_to_cache;
3276 static unsigned int attrs_cached_inside, attrs_cached_after;
3278 /* Finds non-const attributes that could be possibly cached.
3279 When create is TRUE, fills in cached_attrs array.
3280 Computes ATTRS_SEEN_ONCE and ATTRS_SEEN_MORE_THAN_ONCE
3281 bitmasks. */
3283 static void
3284 find_attrs_to_cache (rtx exp, bool create)
3286 int i;
3287 const char *name;
3288 struct attr_desc *attr;
3290 if (exp == NULL)
3291 return;
3293 switch (GET_CODE (exp))
3295 case NOT:
3296 if (GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
3297 find_attrs_to_cache (XEXP (exp, 0), create);
3298 return;
3300 case EQ_ATTR:
3301 name = XSTR (exp, 0);
3302 if (name == alternative_name)
3303 return;
3304 for (i = 0; i < cached_attr_count; i++)
3305 if (name == cached_attrs[i])
3307 if ((attrs_seen_once & (1U << i)) != 0)
3308 attrs_seen_more_than_once |= (1U << i);
3309 else
3310 attrs_seen_once |= (1U << i);
3311 return;
3313 if (!create)
3314 return;
3315 attr = find_attr (&name, 0);
3316 gcc_assert (attr);
3317 if (attr->is_const)
3318 return;
3319 if (cached_attr_count == 32)
3320 return;
3321 cached_attrs[cached_attr_count] = XSTR (exp, 0);
3322 attrs_seen_once |= (1U << cached_attr_count);
3323 cached_attr_count++;
3324 return;
3326 case AND:
3327 case IOR:
3328 find_attrs_to_cache (XEXP (exp, 0), create);
3329 find_attrs_to_cache (XEXP (exp, 1), create);
3330 return;
3332 case COND:
3333 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3334 find_attrs_to_cache (XVECEXP (exp, 0, i), create);
3335 return;
3337 default:
3338 return;
3342 /* Given a piece of RTX, print a C expression to test its truth value to OUTF.
3343 We use AND and IOR both for logical and bit-wise operations, so
3344 interpret them as logical unless they are inside a comparison expression.
3346 An outermost pair of parentheses is emitted around this C expression unless
3347 EMIT_PARENS is false. */
3349 /* Interpret AND/IOR as bit-wise operations instead of logical. */
3350 #define FLG_BITWISE 1
3351 /* Set if cached attribute will be known initialized in else block after
3352 this condition. This is true for LHS of toplevel && and || and
3353 even for RHS of ||, but not for RHS of &&. */
3354 #define FLG_AFTER 2
3355 /* Set if cached attribute will be known initialized in then block after
3356 this condition. This is true for LHS of toplevel && and || and
3357 even for RHS of &&, but not for RHS of ||. */
3358 #define FLG_INSIDE 4
3359 /* Cleared when an operand of &&. */
3360 #define FLG_OUTSIDE_AND 8
3362 static unsigned int
3363 write_test_expr (FILE *outf, rtx exp, unsigned int attrs_cached, int flags,
3364 bool emit_parens = true)
3366 int comparison_operator = 0;
3367 RTX_CODE code;
3368 struct attr_desc *attr;
3370 if (emit_parens)
3371 fprintf (outf, "(");
3373 code = GET_CODE (exp);
3374 switch (code)
3376 /* Binary operators. */
3377 case GEU: case GTU:
3378 case LEU: case LTU:
3379 fprintf (outf, "(unsigned) ");
3380 /* Fall through. */
3382 case EQ: case NE:
3383 case GE: case GT:
3384 case LE: case LT:
3385 comparison_operator = FLG_BITWISE;
3386 /* FALLTHRU */
3388 case PLUS: case MINUS: case MULT: case DIV: case MOD:
3389 case AND: case IOR: case XOR:
3390 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3391 if ((code != AND && code != IOR) || (flags & FLG_BITWISE))
3393 flags &= ~(FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND);
3394 write_test_expr (outf, XEXP (exp, 0), attrs_cached,
3395 flags | comparison_operator);
3397 else
3399 if (code == AND)
3400 flags &= ~FLG_OUTSIDE_AND;
3401 if (GET_CODE (XEXP (exp, 0)) == code
3402 || GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3403 || (GET_CODE (XEXP (exp, 0)) == NOT
3404 && GET_CODE (XEXP (XEXP (exp, 0), 0)) == EQ_ATTR))
3405 attrs_cached
3406 = write_test_expr (outf, XEXP (exp, 0), attrs_cached, flags);
3407 else
3408 write_test_expr (outf, XEXP (exp, 0), attrs_cached, flags);
3410 switch (code)
3412 case EQ:
3413 fprintf (outf, " == ");
3414 break;
3415 case NE:
3416 fprintf (outf, " != ");
3417 break;
3418 case GE:
3419 fprintf (outf, " >= ");
3420 break;
3421 case GT:
3422 fprintf (outf, " > ");
3423 break;
3424 case GEU:
3425 fprintf (outf, " >= (unsigned) ");
3426 break;
3427 case GTU:
3428 fprintf (outf, " > (unsigned) ");
3429 break;
3430 case LE:
3431 fprintf (outf, " <= ");
3432 break;
3433 case LT:
3434 fprintf (outf, " < ");
3435 break;
3436 case LEU:
3437 fprintf (outf, " <= (unsigned) ");
3438 break;
3439 case LTU:
3440 fprintf (outf, " < (unsigned) ");
3441 break;
3442 case PLUS:
3443 fprintf (outf, " + ");
3444 break;
3445 case MINUS:
3446 fprintf (outf, " - ");
3447 break;
3448 case MULT:
3449 fprintf (outf, " * ");
3450 break;
3451 case DIV:
3452 fprintf (outf, " / ");
3453 break;
3454 case MOD:
3455 fprintf (outf, " %% ");
3456 break;
3457 case AND:
3458 if (flags & FLG_BITWISE)
3459 fprintf (outf, " & ");
3460 else
3461 fprintf (outf, " && ");
3462 break;
3463 case IOR:
3464 if (flags & FLG_BITWISE)
3465 fprintf (outf, " | ");
3466 else
3467 fprintf (outf, " || ");
3468 break;
3469 case XOR:
3470 fprintf (outf, " ^ ");
3471 break;
3472 case ASHIFT:
3473 fprintf (outf, " << ");
3474 break;
3475 case LSHIFTRT:
3476 case ASHIFTRT:
3477 fprintf (outf, " >> ");
3478 break;
3479 default:
3480 gcc_unreachable ();
3483 if (code == AND)
3485 /* For if (something && (cached_x = get_attr_x (insn)) == X)
3486 cached_x is only known to be initialized in then block. */
3487 flags &= ~FLG_AFTER;
3489 else if (code == IOR)
3491 if (flags & FLG_OUTSIDE_AND)
3492 /* For if (something || (cached_x = get_attr_x (insn)) == X)
3493 cached_x is only known to be initialized in else block
3494 and else if conditions. */
3495 flags &= ~FLG_INSIDE;
3496 else
3497 /* For if ((something || (cached_x = get_attr_x (insn)) == X)
3498 && something_else)
3499 cached_x is not know to be initialized anywhere. */
3500 flags &= ~(FLG_AFTER | FLG_INSIDE);
3502 if ((code == AND || code == IOR)
3503 && (GET_CODE (XEXP (exp, 1)) == code
3504 || GET_CODE (XEXP (exp, 1)) == EQ_ATTR
3505 || (GET_CODE (XEXP (exp, 1)) == NOT
3506 && GET_CODE (XEXP (XEXP (exp, 1), 0)) == EQ_ATTR)))
3508 bool need_parens = true;
3510 /* No need to emit parentheses around the right-hand operand if we are
3511 continuing a chain of && or || (or & or |). */
3512 if (GET_CODE (XEXP (exp, 1)) == code)
3513 need_parens = false;
3515 attrs_cached
3516 = write_test_expr (outf, XEXP (exp, 1), attrs_cached, flags,
3517 need_parens);
3519 else
3520 write_test_expr (outf, XEXP (exp, 1), attrs_cached,
3521 flags | comparison_operator);
3522 break;
3524 case NOT:
3525 /* Special-case (not (eq_attrq "alternative" "x")) */
3526 if (! (flags & FLG_BITWISE) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
3528 if (XSTR (XEXP (exp, 0), 0) == alternative_name)
3530 fprintf (outf, "which_alternative != %s",
3531 XSTR (XEXP (exp, 0), 1));
3532 break;
3535 fprintf (outf, "! ");
3536 attrs_cached =
3537 write_test_expr (outf, XEXP (exp, 0), attrs_cached, flags);
3538 break;
3541 /* Otherwise, fall through to normal unary operator. */
3542 gcc_fallthrough ();
3544 /* Unary operators. */
3545 case ABS: case NEG:
3546 switch (code)
3548 case NOT:
3549 if (flags & FLG_BITWISE)
3550 fprintf (outf, "~ ");
3551 else
3552 fprintf (outf, "! ");
3553 break;
3554 case ABS:
3555 fprintf (outf, "abs ");
3556 break;
3557 case NEG:
3558 fprintf (outf, "-");
3559 break;
3560 default:
3561 gcc_unreachable ();
3564 flags &= ~(FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND);
3565 write_test_expr (outf, XEXP (exp, 0), attrs_cached, flags);
3566 break;
3568 case EQ_ATTR_ALT:
3570 int set = XINT (exp, 0), bit = 0;
3572 if (flags & FLG_BITWISE)
3573 fatal ("EQ_ATTR_ALT not valid inside comparison");
3575 if (!set)
3576 fatal ("Empty EQ_ATTR_ALT should be optimized out");
3578 if (!(set & (set - 1)))
3580 if (!(set & 0xffff))
3582 bit += 16;
3583 set >>= 16;
3585 if (!(set & 0xff))
3587 bit += 8;
3588 set >>= 8;
3590 if (!(set & 0xf))
3592 bit += 4;
3593 set >>= 4;
3595 if (!(set & 0x3))
3597 bit += 2;
3598 set >>= 2;
3600 if (!(set & 1))
3601 bit++;
3603 fprintf (outf, "which_alternative %s= %d",
3604 XINT (exp, 1) ? "!" : "=", bit);
3606 else
3608 fprintf (outf, "%s((1 << which_alternative) & %#x)",
3609 XINT (exp, 1) ? "!" : "", set);
3612 break;
3614 /* Comparison test of an attribute with a value. Most of these will
3615 have been removed by optimization. Handle "alternative"
3616 specially and give error if EQ_ATTR present inside a comparison. */
3617 case EQ_ATTR:
3618 if (flags & FLG_BITWISE)
3619 fatal ("EQ_ATTR not valid inside comparison");
3621 if (XSTR (exp, 0) == alternative_name)
3623 fprintf (outf, "which_alternative == %s", XSTR (exp, 1));
3624 break;
3627 attr = find_attr (&XSTR (exp, 0), 0);
3628 gcc_assert (attr);
3630 /* Now is the time to expand the value of a constant attribute. */
3631 if (attr->is_const)
3633 write_test_expr (outf,
3634 evaluate_eq_attr (exp, attr,
3635 attr->default_val->value,
3636 -2, -2),
3637 attrs_cached, 0);
3639 else
3641 int i;
3642 for (i = 0; i < cached_attr_count; i++)
3643 if (attr->name == cached_attrs[i])
3644 break;
3645 if (i < cached_attr_count && (attrs_cached & (1U << i)) != 0)
3646 fprintf (outf, "cached_%s", attr->name);
3647 else if (i < cached_attr_count && (attrs_to_cache & (1U << i)) != 0)
3649 fprintf (outf, "(cached_%s = get_attr_%s (insn))",
3650 attr->name, attr->name);
3651 if (flags & FLG_AFTER)
3652 attrs_cached_after |= (1U << i);
3653 if (flags & FLG_INSIDE)
3654 attrs_cached_inside |= (1U << i);
3655 attrs_cached |= (1U << i);
3657 else
3658 fprintf (outf, "get_attr_%s (insn)", attr->name);
3659 fprintf (outf, " == ");
3660 write_attr_valueq (outf, attr, XSTR (exp, 1));
3662 break;
3664 /* Comparison test of flags for define_delays. */
3665 case ATTR_FLAG:
3666 if (flags & FLG_BITWISE)
3667 fatal ("ATTR_FLAG not valid inside comparison");
3668 fprintf (outf, "(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
3669 break;
3671 /* See if an operand matches a predicate. */
3672 case MATCH_OPERAND:
3673 /* If only a mode is given, just ensure the mode matches the operand.
3674 If neither a mode nor predicate is given, error. */
3675 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
3677 if (GET_MODE (exp) == VOIDmode)
3678 fatal ("null MATCH_OPERAND specified as test");
3679 else
3680 fprintf (outf, "GET_MODE (operands[%d]) == %smode",
3681 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3683 else
3684 fprintf (outf, "%s (operands[%d], %smode)",
3685 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3686 break;
3688 /* Constant integer. */
3689 case CONST_INT:
3690 fprintf (outf, HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
3691 break;
3693 case MATCH_TEST:
3694 rtx_reader_ptr->fprint_c_condition (outf, XSTR (exp, 0));
3695 if (flags & FLG_BITWISE)
3696 fprintf (outf, " != 0");
3697 break;
3699 /* A random C expression. */
3700 case SYMBOL_REF:
3701 rtx_reader_ptr->fprint_c_condition (outf, XSTR (exp, 0));
3702 break;
3704 /* The address of the branch target. */
3705 case MATCH_DUP:
3706 fprintf (outf,
3707 "INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
3708 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
3709 break;
3711 case PC:
3712 /* The address of the current insn. We implement this actually as the
3713 address of the current insn for backward branches, but the last
3714 address of the next insn for forward branches, and both with
3715 adjustments that account for the worst-case possible stretching of
3716 intervening alignments between this insn and its destination. */
3717 fprintf (outf, "insn_current_reference_address (insn)");
3718 break;
3720 case CONST_STRING:
3721 fprintf (outf, "%s", XSTR (exp, 0));
3722 break;
3724 case IF_THEN_ELSE:
3725 write_test_expr (outf, XEXP (exp, 0), attrs_cached, 0);
3726 fprintf (outf, " ? ");
3727 write_test_expr (outf, XEXP (exp, 1), attrs_cached, FLG_BITWISE);
3728 fprintf (outf, " : ");
3729 write_test_expr (outf, XEXP (exp, 2), attrs_cached, FLG_BITWISE);
3730 break;
3732 default:
3733 fatal ("bad RTX code `%s' in attribute calculation\n",
3734 GET_RTX_NAME (code));
3737 if (emit_parens)
3738 fprintf (outf, ")");
3740 return attrs_cached;
3743 /* Given an attribute value, return the maximum CONST_STRING argument
3744 encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
3746 static int
3747 max_attr_value (rtx exp, int *unknownp)
3749 int current_max;
3750 int i, n;
3752 switch (GET_CODE (exp))
3754 case CONST_STRING:
3755 current_max = atoi (XSTR (exp, 0));
3756 break;
3758 case COND:
3759 current_max = max_attr_value (XEXP (exp, 1), unknownp);
3760 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3762 n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3763 if (n > current_max)
3764 current_max = n;
3766 break;
3768 case IF_THEN_ELSE:
3769 current_max = max_attr_value (XEXP (exp, 1), unknownp);
3770 n = max_attr_value (XEXP (exp, 2), unknownp);
3771 if (n > current_max)
3772 current_max = n;
3773 break;
3775 default:
3776 *unknownp = 1;
3777 current_max = INT_MAX;
3778 break;
3781 return current_max;
3784 /* Given an attribute value, return the minimum CONST_STRING argument
3785 encountered. Set *UNKNOWNP and return 0 if the value is unknown. */
3787 static int
3788 min_attr_value (rtx exp, int *unknownp)
3790 int current_min;
3791 int i, n;
3793 switch (GET_CODE (exp))
3795 case CONST_STRING:
3796 current_min = atoi (XSTR (exp, 0));
3797 break;
3799 case COND:
3800 current_min = min_attr_value (XEXP (exp, 1), unknownp);
3801 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3803 n = min_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3804 if (n < current_min)
3805 current_min = n;
3807 break;
3809 case IF_THEN_ELSE:
3810 current_min = min_attr_value (XEXP (exp, 1), unknownp);
3811 n = min_attr_value (XEXP (exp, 2), unknownp);
3812 if (n < current_min)
3813 current_min = n;
3814 break;
3816 default:
3817 *unknownp = 1;
3818 current_min = INT_MAX;
3819 break;
3822 return current_min;
3825 /* Given an attribute value, return the result of ORing together all
3826 CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
3827 if the numeric value is not known. */
3829 static int
3830 or_attr_value (rtx exp, int *unknownp)
3832 int current_or;
3833 int i;
3835 switch (GET_CODE (exp))
3837 case CONST_STRING:
3838 current_or = atoi (XSTR (exp, 0));
3839 break;
3841 case COND:
3842 current_or = or_attr_value (XEXP (exp, 1), unknownp);
3843 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3844 current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3845 break;
3847 case IF_THEN_ELSE:
3848 current_or = or_attr_value (XEXP (exp, 1), unknownp);
3849 current_or |= or_attr_value (XEXP (exp, 2), unknownp);
3850 break;
3852 default:
3853 *unknownp = 1;
3854 current_or = -1;
3855 break;
3858 return current_or;
3861 /* Scan an attribute value, possibly a conditional, and record what actions
3862 will be required to do any conditional tests in it.
3864 Specifically, set
3865 `must_extract' if we need to extract the insn operands
3866 `must_constrain' if we must compute `which_alternative'
3867 `address_used' if an address expression was used
3868 `length_used' if an (eq_attr "length" ...) was used
3871 static void
3872 walk_attr_value (rtx exp)
3874 int i, j;
3875 const char *fmt;
3876 RTX_CODE code;
3878 if (exp == NULL)
3879 return;
3881 code = GET_CODE (exp);
3882 switch (code)
3884 case SYMBOL_REF:
3885 if (! ATTR_IND_SIMPLIFIED_P (exp))
3886 /* Since this is an arbitrary expression, it can look at anything.
3887 However, constant expressions do not depend on any particular
3888 insn. */
3889 must_extract = must_constrain = 1;
3890 return;
3892 case MATCH_OPERAND:
3893 must_extract = 1;
3894 return;
3896 case MATCH_TEST:
3897 case EQ_ATTR_ALT:
3898 must_extract = must_constrain = 1;
3899 break;
3901 case EQ_ATTR:
3902 if (XSTR (exp, 0) == alternative_name)
3903 must_extract = must_constrain = 1;
3904 else if (strcmp_check (XSTR (exp, 0), length_str) == 0)
3905 length_used = 1;
3906 return;
3908 case MATCH_DUP:
3909 must_extract = 1;
3910 address_used = 1;
3911 return;
3913 case PC:
3914 address_used = 1;
3915 return;
3917 case ATTR_FLAG:
3918 return;
3920 default:
3921 break;
3924 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
3925 switch (*fmt++)
3927 case 'e':
3928 case 'u':
3929 walk_attr_value (XEXP (exp, i));
3930 break;
3932 case 'E':
3933 if (XVEC (exp, i) != NULL)
3934 for (j = 0; j < XVECLEN (exp, i); j++)
3935 walk_attr_value (XVECEXP (exp, i, j));
3936 break;
3940 /* Write out a function to obtain the attribute for a given INSN. */
3942 static void
3943 write_attr_get (FILE *outf, struct attr_desc *attr)
3945 struct attr_value *av, *common_av;
3946 int i, j;
3948 /* Find the most used attribute value. Handle that as the `default' of the
3949 switch we will generate. */
3950 common_av = find_most_used (attr);
3952 /* Write out start of function, then all values with explicit `case' lines,
3953 then a `default', then the value with the most uses. */
3954 if (attr->enum_name)
3955 fprintf (outf, "enum %s\n", attr->enum_name);
3956 else if (!attr->is_numeric)
3957 fprintf (outf, "enum attr_%s\n", attr->name);
3958 else
3959 fprintf (outf, "int\n");
3961 /* If the attribute name starts with a star, the remainder is the name of
3962 the subroutine to use, instead of `get_attr_...'. */
3963 if (attr->name[0] == '*')
3964 fprintf (outf, "%s (rtx_insn *insn ATTRIBUTE_UNUSED)\n", &attr->name[1]);
3965 else if (attr->is_const == 0)
3966 fprintf (outf, "get_attr_%s (rtx_insn *insn ATTRIBUTE_UNUSED)\n", attr->name);
3967 else
3969 fprintf (outf, "get_attr_%s (void)\n", attr->name);
3970 fprintf (outf, "{\n");
3972 for (av = attr->first_value; av; av = av->next)
3973 if (av->num_insns == 1)
3974 write_attr_set (outf, attr, 2, av->value, "return", ";",
3975 true_rtx, av->first_insn->def->insn_code,
3976 av->first_insn->def->insn_index, 0);
3977 else if (av->num_insns != 0)
3978 write_attr_set (outf, attr, 2, av->value, "return", ";",
3979 true_rtx, -2, 0, 0);
3981 fprintf (outf, "}\n\n");
3982 return;
3985 fprintf (outf, "{\n");
3987 /* Find attributes that are worth caching in the conditions. */
3988 cached_attr_count = 0;
3989 attrs_seen_more_than_once = 0;
3990 for (av = attr->first_value; av; av = av->next)
3992 attrs_seen_once = 0;
3993 find_attrs_to_cache (av->value, true);
3995 /* Remove those that aren't worth caching from the array. */
3996 for (i = 0, j = 0; i < cached_attr_count; i++)
3997 if ((attrs_seen_more_than_once & (1U << i)) != 0)
3999 const char *name = cached_attrs[i];
4000 struct attr_desc *cached_attr;
4001 if (i != j)
4002 cached_attrs[j] = name;
4003 cached_attr = find_attr (&name, 0);
4004 gcc_assert (cached_attr && cached_attr->is_const == 0);
4005 if (cached_attr->enum_name)
4006 fprintf (outf, " enum %s", cached_attr->enum_name);
4007 else if (!cached_attr->is_numeric)
4008 fprintf (outf, " enum attr_%s", cached_attr->name);
4009 else
4010 fprintf (outf, " int");
4011 fprintf (outf, " cached_%s ATTRIBUTE_UNUSED;\n", name);
4012 j++;
4014 cached_attr_count = j;
4015 if (cached_attr_count)
4016 fprintf (outf, "\n");
4018 fprintf (outf, " switch (recog_memoized (insn))\n");
4019 fprintf (outf, " {\n");
4021 for (av = attr->first_value; av; av = av->next)
4022 if (av != common_av)
4023 write_attr_case (outf, attr, av, 1, "return", ";", 4, true_rtx);
4025 write_attr_case (outf, attr, common_av, 0, "return", ";", 4, true_rtx);
4026 fprintf (outf, " }\n}\n\n");
4027 cached_attr_count = 0;
4030 /* Given an AND tree of known true terms (because we are inside an `if' with
4031 that as the condition or are in an `else' clause) and an expression,
4032 replace any known true terms with TRUE. Use `simplify_and_tree' to do
4033 the bulk of the work. */
4035 static rtx
4036 eliminate_known_true (rtx known_true, rtx exp, int insn_code, int insn_index)
4038 rtx term;
4040 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
4042 if (GET_CODE (known_true) == AND)
4044 exp = eliminate_known_true (XEXP (known_true, 0), exp,
4045 insn_code, insn_index);
4046 exp = eliminate_known_true (XEXP (known_true, 1), exp,
4047 insn_code, insn_index);
4049 else
4051 term = known_true;
4052 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
4055 return exp;
4058 /* Write out a series of tests and assignment statements to perform tests and
4059 sets of an attribute value. We are passed an indentation amount and prefix
4060 and suffix strings to write around each attribute value (e.g., "return"
4061 and ";"). */
4063 static void
4064 write_attr_set (FILE *outf, struct attr_desc *attr, int indent, rtx value,
4065 const char *prefix, const char *suffix, rtx known_true,
4066 int insn_code, int insn_index, unsigned int attrs_cached)
4068 if (GET_CODE (value) == COND)
4070 /* Assume the default value will be the default of the COND unless we
4071 find an always true expression. */
4072 rtx default_val = XEXP (value, 1);
4073 rtx our_known_true = known_true;
4074 rtx newexp;
4075 int first_if = 1;
4076 int i;
4078 if (cached_attr_count)
4080 attrs_seen_once = 0;
4081 attrs_seen_more_than_once = 0;
4082 for (i = 0; i < XVECLEN (value, 0); i += 2)
4083 find_attrs_to_cache (XVECEXP (value, 0, i), false);
4084 attrs_to_cache |= attrs_seen_more_than_once;
4087 for (i = 0; i < XVECLEN (value, 0); i += 2)
4089 rtx testexp;
4090 rtx inner_true;
4092 /* Reset our_known_true after some time to not accumulate
4093 too much cruft (slowing down genattrtab). */
4094 if ((i & 31) == 0)
4095 our_known_true = known_true;
4096 testexp = eliminate_known_true (our_known_true,
4097 XVECEXP (value, 0, i),
4098 insn_code, insn_index);
4099 newexp = attr_rtx (NOT, testexp);
4100 newexp = insert_right_side (AND, our_known_true, newexp,
4101 insn_code, insn_index);
4103 /* If the test expression is always true or if the next `known_true'
4104 expression is always false, this is the last case, so break
4105 out and let this value be the `else' case. */
4106 if (testexp == true_rtx || newexp == false_rtx)
4108 default_val = XVECEXP (value, 0, i + 1);
4109 break;
4112 /* Compute the expression to pass to our recursive call as being
4113 known true. */
4114 inner_true = insert_right_side (AND, our_known_true,
4115 testexp, insn_code, insn_index);
4117 /* If this is always false, skip it. */
4118 if (inner_true == false_rtx)
4119 continue;
4121 attrs_cached_inside = attrs_cached;
4122 attrs_cached_after = attrs_cached;
4123 write_indent (outf, indent);
4124 fprintf (outf, "%sif ", first_if ? "" : "else ");
4125 first_if = 0;
4126 write_test_expr (outf, testexp, attrs_cached,
4127 (FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND));
4128 attrs_cached = attrs_cached_after;
4129 fprintf (outf, "\n");
4130 write_indent (outf, indent + 2);
4131 fprintf (outf, "{\n");
4133 write_attr_set (outf, attr, indent + 4,
4134 XVECEXP (value, 0, i + 1), prefix, suffix,
4135 inner_true, insn_code, insn_index,
4136 attrs_cached_inside);
4137 write_indent (outf, indent + 2);
4138 fprintf (outf, "}\n");
4139 our_known_true = newexp;
4142 if (! first_if)
4144 write_indent (outf, indent);
4145 fprintf (outf, "else\n");
4146 write_indent (outf, indent + 2);
4147 fprintf (outf, "{\n");
4150 write_attr_set (outf, attr, first_if ? indent : indent + 4, default_val,
4151 prefix, suffix, our_known_true, insn_code, insn_index,
4152 attrs_cached);
4154 if (! first_if)
4156 write_indent (outf, indent + 2);
4157 fprintf (outf, "}\n");
4160 else
4162 write_indent (outf, indent);
4163 fprintf (outf, "%s ", prefix);
4164 write_attr_value (outf, attr, value);
4165 fprintf (outf, "%s\n", suffix);
4169 /* Write a series of case statements for every instruction in list IE.
4170 INDENT is the amount of indentation to write before each case. */
4172 static void
4173 write_insn_cases (FILE *outf, struct insn_ent *ie, int indent)
4175 for (; ie != 0; ie = ie->next)
4176 if (ie->def->insn_code != -1)
4178 write_indent (outf, indent);
4179 if (GET_CODE (ie->def->def) == DEFINE_PEEPHOLE)
4180 fprintf (outf, "case %d: /* define_peephole, %s:%d */\n",
4181 ie->def->insn_code, ie->def->loc.filename,
4182 ie->def->loc.lineno);
4183 else
4184 fprintf (outf, "case %d: /* %s */\n",
4185 ie->def->insn_code, XSTR (ie->def->def, 0));
4189 /* Write out the computation for one attribute value. */
4191 static void
4192 write_attr_case (FILE *outf, struct attr_desc *attr, struct attr_value *av,
4193 int write_case_lines, const char *prefix, const char *suffix,
4194 int indent, rtx known_true)
4196 if (av->num_insns == 0)
4197 return;
4199 if (av->has_asm_insn)
4201 write_indent (outf, indent);
4202 fprintf (outf, "case -1:\n");
4203 write_indent (outf, indent + 2);
4204 fprintf (outf, "if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
4205 write_indent (outf, indent + 2);
4206 fprintf (outf, " && asm_noperands (PATTERN (insn)) < 0)\n");
4207 write_indent (outf, indent + 2);
4208 fprintf (outf, " fatal_insn_not_found (insn);\n");
4209 write_indent (outf, indent + 2);
4210 fprintf (outf, "/* FALLTHRU */\n");
4213 if (write_case_lines)
4214 write_insn_cases (outf, av->first_insn, indent);
4215 else
4217 write_indent (outf, indent);
4218 fprintf (outf, "default:\n");
4221 /* See what we have to do to output this value. */
4222 must_extract = must_constrain = address_used = 0;
4223 walk_attr_value (av->value);
4225 if (must_constrain)
4227 write_indent (outf, indent + 2);
4228 fprintf (outf, "extract_constrain_insn_cached (insn);\n");
4230 else if (must_extract)
4232 write_indent (outf, indent + 2);
4233 fprintf (outf, "extract_insn_cached (insn);\n");
4236 attrs_to_cache = 0;
4237 if (av->num_insns == 1)
4238 write_attr_set (outf, attr, indent + 2, av->value, prefix, suffix,
4239 known_true, av->first_insn->def->insn_code,
4240 av->first_insn->def->insn_index, 0);
4241 else
4242 write_attr_set (outf, attr, indent + 2, av->value, prefix, suffix,
4243 known_true, -2, 0, 0);
4245 if (strncmp (prefix, "return", 6))
4247 write_indent (outf, indent + 2);
4248 fprintf (outf, "break;\n");
4250 fprintf (outf, "\n");
4253 /* Utilities to write in various forms. */
4255 static void
4256 write_attr_valueq (FILE *outf, struct attr_desc *attr, const char *s)
4258 if (attr->is_numeric)
4260 int num = atoi (s);
4262 fprintf (outf, "%d", num);
4264 if (num > 9 || num < 0)
4265 fprintf (outf, " /* %#x */", num);
4267 else
4269 write_upcase (outf, attr->enum_name ? attr->enum_name : attr->name);
4270 fprintf (outf, "_");
4271 write_upcase (outf, s);
4275 static void
4276 write_attr_value (FILE *outf, struct attr_desc *attr, rtx value)
4278 int op;
4280 switch (GET_CODE (value))
4282 case CONST_STRING:
4283 write_attr_valueq (outf, attr, XSTR (value, 0));
4284 break;
4286 case CONST_INT:
4287 fprintf (outf, HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
4288 break;
4290 case SYMBOL_REF:
4291 rtx_reader_ptr->fprint_c_condition (outf, XSTR (value, 0));
4292 break;
4294 case ATTR:
4296 struct attr_desc *attr2 = find_attr (&XSTR (value, 0), 0);
4297 if (attr->enum_name)
4298 fprintf (outf, "(enum %s)", attr->enum_name);
4299 else if (!attr->is_numeric)
4300 fprintf (outf, "(enum attr_%s)", attr->name);
4301 else if (!attr2->is_numeric)
4302 fprintf (outf, "(int)");
4304 fprintf (outf, "get_attr_%s (%s)", attr2->name,
4305 (attr2->is_const ? "" : "insn"));
4307 break;
4309 case PLUS:
4310 op = '+';
4311 goto do_operator;
4312 case MINUS:
4313 op = '-';
4314 goto do_operator;
4315 case MULT:
4316 op = '*';
4317 goto do_operator;
4318 case DIV:
4319 op = '/';
4320 goto do_operator;
4321 case MOD:
4322 op = '%';
4323 goto do_operator;
4325 do_operator:
4326 write_attr_value (outf, attr, XEXP (value, 0));
4327 fputc (' ', outf);
4328 fputc (op, outf);
4329 fputc (' ', outf);
4330 write_attr_value (outf, attr, XEXP (value, 1));
4331 break;
4333 default:
4334 gcc_unreachable ();
4338 static void
4339 write_upcase (FILE *outf, const char *str)
4341 while (*str)
4343 /* The argument of TOUPPER should not have side effects. */
4344 fputc (TOUPPER (*str), outf);
4345 str++;
4349 static void
4350 write_indent (FILE *outf, int indent)
4352 for (; indent > 8; indent -= 8)
4353 fprintf (outf, "\t");
4355 for (; indent; indent--)
4356 fprintf (outf, " ");
4359 /* If the target does not have annul-true or annul-false delay slots, this
4360 function will create a dummy eligible_for function on OUTF which always
4361 returns false. KIND will be annul_true or annul_false. */
4363 static void
4364 write_dummy_eligible_delay (FILE *outf, const char *kind)
4366 /* Write function prelude. */
4368 fprintf (outf, "int\n");
4369 fprintf (outf, "eligible_for_%s (rtx_insn *delay_insn ATTRIBUTE_UNUSED,\n"
4370 " int slot ATTRIBUTE_UNUSED,\n"
4371 " rtx_insn *candidate_insn ATTRIBUTE_UNUSED,\n"
4372 " int flags ATTRIBUTE_UNUSED)\n",
4373 kind);
4374 fprintf (outf, "{\n");
4375 fprintf (outf, " return 0;\n");
4376 fprintf (outf, "}\n\n");
4379 /* Write a subroutine that is given an insn that requires a delay slot, a
4380 delay slot ordinal, and a candidate insn. It returns nonzero if the
4381 candidate can be placed in the specified delay slot of the insn.
4383 We can write as many as three subroutines. `eligible_for_delay'
4384 handles normal delay slots, `eligible_for_annul_true' indicates that
4385 the specified insn can be annulled if the branch is true, and likewise
4386 for `eligible_for_annul_false'.
4388 KIND is a string distinguishing these three cases ("delay", "annul_true",
4389 or "annul_false"). */
4391 static void
4392 write_eligible_delay (FILE *outf, const char *kind)
4394 struct delay_desc *delay;
4395 int max_slots;
4396 char str[50];
4397 const char *pstr;
4398 struct attr_desc *attr;
4399 struct attr_value *av, *common_av;
4400 int i;
4402 /* Compute the maximum number of delay slots required. We use the delay
4403 ordinal times this number plus one, plus the slot number as an index into
4404 the appropriate predicate to test. */
4406 for (delay = delays, max_slots = 0; delay; delay = delay->next)
4407 if (XVECLEN (delay->def, 1) / 3 > max_slots)
4408 max_slots = XVECLEN (delay->def, 1) / 3;
4410 /* Write function prelude. */
4412 fprintf (outf, "int\n");
4413 fprintf (outf, "eligible_for_%s (rtx_insn *delay_insn ATTRIBUTE_UNUSED, int slot, \n"
4414 " rtx_insn *candidate_insn, int flags ATTRIBUTE_UNUSED)\n",
4415 kind);
4416 fprintf (outf, "{\n");
4417 fprintf (outf, " rtx_insn *insn ATTRIBUTE_UNUSED;\n");
4418 fprintf (outf, "\n");
4419 fprintf (outf, " if (num_delay_slots (delay_insn) == 0)\n");
4420 fprintf (outf, " return 0;");
4421 fprintf (outf, "\n");
4422 fprintf (outf, " gcc_assert (slot < %d);\n", max_slots);
4423 fprintf (outf, "\n");
4424 /* Allow dbr_schedule to pass labels, etc. This can happen if try_split
4425 converts a compound instruction into a loop. */
4426 fprintf (outf, " if (!INSN_P (candidate_insn))\n");
4427 fprintf (outf, " return 0;\n");
4428 fprintf (outf, "\n");
4430 /* If more than one delay type, find out which type the delay insn is. */
4432 if (num_delays > 1)
4434 attr = find_attr (&delay_type_str, 0);
4435 gcc_assert (attr);
4436 common_av = find_most_used (attr);
4438 fprintf (outf, " insn = delay_insn;\n");
4439 fprintf (outf, " switch (recog_memoized (insn))\n");
4440 fprintf (outf, " {\n");
4442 sprintf (str, " * %d;\n break;", max_slots);
4443 for (av = attr->first_value; av; av = av->next)
4444 if (av != common_av)
4445 write_attr_case (outf, attr, av, 1, "slot +=", str, 4, true_rtx);
4447 write_attr_case (outf, attr, common_av, 0, "slot +=", str, 4, true_rtx);
4448 fprintf (outf, " }\n\n");
4450 /* Ensure matched. Otherwise, shouldn't have been called. */
4451 fprintf (outf, " gcc_assert (slot >= %d);\n\n", max_slots);
4454 /* If just one type of delay slot, write simple switch. */
4455 if (num_delays == 1 && max_slots == 1)
4457 fprintf (outf, " insn = candidate_insn;\n");
4458 fprintf (outf, " switch (recog_memoized (insn))\n");
4459 fprintf (outf, " {\n");
4461 attr = find_attr (&delay_1_0_str, 0);
4462 gcc_assert (attr);
4463 common_av = find_most_used (attr);
4465 for (av = attr->first_value; av; av = av->next)
4466 if (av != common_av)
4467 write_attr_case (outf, attr, av, 1, "return", ";", 4, true_rtx);
4469 write_attr_case (outf, attr, common_av, 0, "return", ";", 4, true_rtx);
4470 fprintf (outf, " }\n");
4473 else
4475 /* Write a nested CASE. The first indicates which condition we need to
4476 test, and the inner CASE tests the condition. */
4477 fprintf (outf, " insn = candidate_insn;\n");
4478 fprintf (outf, " switch (slot)\n");
4479 fprintf (outf, " {\n");
4481 for (delay = delays; delay; delay = delay->next)
4482 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
4484 fprintf (outf, " case %d:\n",
4485 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
4486 fprintf (outf, " switch (recog_memoized (insn))\n");
4487 fprintf (outf, "\t{\n");
4489 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
4490 pstr = str;
4491 attr = find_attr (&pstr, 0);
4492 gcc_assert (attr);
4493 common_av = find_most_used (attr);
4495 for (av = attr->first_value; av; av = av->next)
4496 if (av != common_av)
4497 write_attr_case (outf, attr, av, 1, "return", ";", 8, true_rtx);
4499 write_attr_case (outf, attr, common_av, 0, "return", ";", 8, true_rtx);
4500 fprintf (outf, " }\n");
4503 fprintf (outf, " default:\n");
4504 fprintf (outf, " gcc_unreachable ();\n");
4505 fprintf (outf, " }\n");
4508 fprintf (outf, "}\n\n");
4511 /* This page contains miscellaneous utility routines. */
4513 /* Given a pointer to a (char *), return a malloc'ed string containing the
4514 next comma-separated element. Advance the pointer to after the string
4515 scanned, or the end-of-string. Return NULL if at end of string. */
4517 static char *
4518 next_comma_elt (const char **pstr)
4520 const char *start;
4522 start = scan_comma_elt (pstr);
4524 if (start == NULL)
4525 return NULL;
4527 return attr_string (start, *pstr - start);
4530 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
4531 is nonzero, build a new attribute, if one does not exist. *NAME_P is
4532 replaced by a pointer to a canonical copy of the string. */
4534 static struct attr_desc *
4535 find_attr (const char **name_p, int create)
4537 struct attr_desc *attr;
4538 int index;
4539 const char *name = *name_p;
4541 /* Before we resort to using `strcmp', see if the string address matches
4542 anywhere. In most cases, it should have been canonicalized to do so. */
4543 if (name == alternative_name)
4544 return NULL;
4546 index = name[0] & (MAX_ATTRS_INDEX - 1);
4547 for (attr = attrs[index]; attr; attr = attr->next)
4548 if (name == attr->name)
4549 return attr;
4551 /* Otherwise, do it the slow way. */
4552 for (attr = attrs[index]; attr; attr = attr->next)
4553 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
4555 *name_p = attr->name;
4556 return attr;
4559 if (! create)
4560 return NULL;
4562 attr = oballoc (struct attr_desc);
4563 attr->name = DEF_ATTR_STRING (name);
4564 attr->enum_name = 0;
4565 attr->first_value = attr->default_val = NULL;
4566 attr->is_numeric = attr->is_const = attr->is_special = 0;
4567 attr->next = attrs[index];
4568 attrs[index] = attr;
4570 *name_p = attr->name;
4572 return attr;
4575 /* Create internal attribute with the given default value. */
4577 static void
4578 make_internal_attr (const char *name, rtx value, int special)
4580 struct attr_desc *attr;
4582 attr = find_attr (&name, 1);
4583 gcc_assert (!attr->default_val);
4585 attr->is_numeric = 1;
4586 attr->is_const = 0;
4587 attr->is_special = (special & ATTR_SPECIAL) != 0;
4588 attr->default_val = get_attr_value (file_location ("<internal>", 0, 0),
4589 value, attr, -2);
4592 /* Find the most used value of an attribute. */
4594 static struct attr_value *
4595 find_most_used (struct attr_desc *attr)
4597 struct attr_value *av;
4598 struct attr_value *most_used;
4599 int nuses;
4601 most_used = NULL;
4602 nuses = -1;
4604 for (av = attr->first_value; av; av = av->next)
4605 if (av->num_insns > nuses)
4606 nuses = av->num_insns, most_used = av;
4608 return most_used;
4611 /* Return (attr_value "n") */
4613 static rtx
4614 make_numeric_value (int n)
4616 static rtx int_values[20];
4617 rtx exp;
4618 char *p;
4620 gcc_assert (n >= 0);
4622 if (n < 20 && int_values[n])
4623 return int_values[n];
4625 p = attr_printf (MAX_DIGITS, "%d", n);
4626 exp = attr_rtx (CONST_STRING, p);
4628 if (n < 20)
4629 int_values[n] = exp;
4631 return exp;
4634 static rtx
4635 copy_rtx_unchanging (rtx orig)
4637 if (ATTR_IND_SIMPLIFIED_P (orig) || ATTR_CURR_SIMPLIFIED_P (orig))
4638 return orig;
4640 ATTR_CURR_SIMPLIFIED_P (orig) = 1;
4641 return orig;
4644 /* Determine if an insn has a constant number of delay slots, i.e., the
4645 number of delay slots is not a function of the length of the insn. */
4647 static void
4648 write_const_num_delay_slots (FILE *outf)
4650 struct attr_desc *attr = find_attr (&num_delay_slots_str, 0);
4651 struct attr_value *av;
4653 if (attr)
4655 fprintf (outf, "int\nconst_num_delay_slots (rtx_insn *insn)\n");
4656 fprintf (outf, "{\n");
4657 fprintf (outf, " switch (recog_memoized (insn))\n");
4658 fprintf (outf, " {\n");
4660 for (av = attr->first_value; av; av = av->next)
4662 length_used = 0;
4663 walk_attr_value (av->value);
4664 if (length_used)
4665 write_insn_cases (outf, av->first_insn, 4);
4668 fprintf (outf, " default:\n");
4669 fprintf (outf, " return 1;\n");
4670 fprintf (outf, " }\n}\n\n");
4674 /* Synthetic attributes used by insn-automata.c and the scheduler.
4675 These are primarily concerned with (define_insn_reservation)
4676 patterns. */
4678 struct insn_reserv
4680 struct insn_reserv *next;
4682 const char *name;
4683 int default_latency;
4684 rtx condexp;
4686 /* Sequence number of this insn. */
4687 int insn_num;
4689 /* Whether a (define_bypass) construct names this insn in its
4690 output list. */
4691 bool bypassed;
4694 static struct insn_reserv *all_insn_reservs = 0;
4695 static struct insn_reserv **last_insn_reserv_p = &all_insn_reservs;
4696 static size_t n_insn_reservs;
4698 /* Store information from a DEFINE_INSN_RESERVATION for future
4699 attribute generation. */
4700 static void
4701 gen_insn_reserv (md_rtx_info *info)
4703 struct insn_reserv *decl = oballoc (struct insn_reserv);
4704 rtx def = info->def;
4706 struct attr_desc attr = { };
4708 attr.name = DEF_ATTR_STRING (XSTR (def, 0));
4709 attr.loc = info->loc;
4711 decl->name = DEF_ATTR_STRING (XSTR (def, 0));
4712 decl->default_latency = XINT (def, 1);
4713 decl->condexp = check_attr_test (info->loc, XEXP (def, 2), &attr);
4714 decl->insn_num = n_insn_reservs;
4715 decl->bypassed = false;
4716 decl->next = 0;
4718 *last_insn_reserv_p = decl;
4719 last_insn_reserv_p = &decl->next;
4720 n_insn_reservs++;
4723 /* Store information from a DEFINE_BYPASS for future attribute
4724 generation. The only thing we care about is the list of output
4725 insns, which will later be used to tag reservation structures with
4726 a 'bypassed' bit. */
4728 struct bypass_list
4730 struct bypass_list *next;
4731 const char *pattern;
4734 static struct bypass_list *all_bypasses;
4735 static size_t n_bypasses;
4736 static size_t n_bypassed;
4738 static void
4739 gen_bypass_1 (const char *s, size_t len)
4741 struct bypass_list *b;
4743 if (len == 0)
4744 return;
4746 s = attr_string (s, len);
4747 for (b = all_bypasses; b; b = b->next)
4748 if (s == b->pattern)
4749 return; /* already got that one */
4751 b = oballoc (struct bypass_list);
4752 b->pattern = s;
4753 b->next = all_bypasses;
4754 all_bypasses = b;
4755 n_bypasses++;
4758 static void
4759 gen_bypass (md_rtx_info *info)
4761 const char *p, *base;
4763 rtx def = info->def;
4764 for (p = base = XSTR (def, 1); *p; p++)
4765 if (*p == ',')
4767 gen_bypass_1 (base, p - base);
4769 p++;
4770 while (ISSPACE (*p));
4771 base = p;
4773 gen_bypass_1 (base, p - base);
4776 /* Find and mark all of the bypassed insns. */
4777 static void
4778 process_bypasses (void)
4780 struct bypass_list *b;
4781 struct insn_reserv *r;
4783 n_bypassed = 0;
4785 /* The reservation list is likely to be much longer than the bypass
4786 list. */
4787 for (r = all_insn_reservs; r; r = r->next)
4788 for (b = all_bypasses; b; b = b->next)
4789 if (fnmatch (b->pattern, r->name, 0) == 0)
4791 n_bypassed++;
4792 r->bypassed = true;
4793 break;
4797 /* Check that attribute NAME is used in define_insn_reservation condition
4798 EXP. Return true if it is. */
4799 static bool
4800 check_tune_attr (const char *name, rtx exp)
4802 switch (GET_CODE (exp))
4804 case AND:
4805 if (check_tune_attr (name, XEXP (exp, 0)))
4806 return true;
4807 return check_tune_attr (name, XEXP (exp, 1));
4809 case IOR:
4810 return (check_tune_attr (name, XEXP (exp, 0))
4811 && check_tune_attr (name, XEXP (exp, 1)));
4813 case EQ_ATTR:
4814 return XSTR (exp, 0) == name;
4816 default:
4817 return false;
4821 /* Try to find a const attribute (usually cpu or tune) that is used
4822 in all define_insn_reservation conditions. */
4823 static struct attr_desc *
4824 find_tune_attr (rtx exp)
4826 struct attr_desc *attr;
4828 switch (GET_CODE (exp))
4830 case AND:
4831 case IOR:
4832 attr = find_tune_attr (XEXP (exp, 0));
4833 if (attr)
4834 return attr;
4835 return find_tune_attr (XEXP (exp, 1));
4837 case EQ_ATTR:
4838 if (XSTR (exp, 0) == alternative_name)
4839 return NULL;
4841 attr = find_attr (&XSTR (exp, 0), 0);
4842 gcc_assert (attr);
4844 if (attr->is_const && !attr->is_special)
4846 struct insn_reserv *decl;
4848 for (decl = all_insn_reservs; decl; decl = decl->next)
4849 if (! check_tune_attr (attr->name, decl->condexp))
4850 return NULL;
4851 return attr;
4853 return NULL;
4855 default:
4856 return NULL;
4860 /* Create all of the attributes that describe automaton properties.
4861 Write the DFA and latency function prototypes to the files that
4862 need to have them, and write the init_sched_attrs(). */
4864 static void
4865 make_automaton_attrs (void)
4867 int i;
4868 struct insn_reserv *decl;
4869 rtx code_exp, lats_exp, byps_exp;
4870 struct attr_desc *tune_attr;
4872 if (n_insn_reservs == 0)
4873 return;
4875 tune_attr = find_tune_attr (all_insn_reservs->condexp);
4876 if (tune_attr != NULL)
4878 rtx *condexps = XNEWVEC (rtx, n_insn_reservs * 3);
4879 struct attr_value *val;
4880 bool first = true;
4882 gcc_assert (tune_attr->is_const
4883 && !tune_attr->is_special
4884 && !tune_attr->is_numeric);
4886 /* Write the prototypes for all DFA functions. */
4887 for (val = tune_attr->first_value; val; val = val->next)
4889 if (val == tune_attr->default_val)
4890 continue;
4891 gcc_assert (GET_CODE (val->value) == CONST_STRING);
4892 fprintf (dfa_file,
4893 "extern int internal_dfa_insn_code_%s (rtx_insn *);\n",
4894 XSTR (val->value, 0));
4896 fprintf (dfa_file, "\n");
4898 /* Write the prototypes for all latency functions. */
4899 for (val = tune_attr->first_value; val; val = val->next)
4901 if (val == tune_attr->default_val)
4902 continue;
4903 gcc_assert (GET_CODE (val->value) == CONST_STRING);
4904 fprintf (latency_file,
4905 "extern int insn_default_latency_%s (rtx_insn *);\n",
4906 XSTR (val->value, 0));
4908 fprintf (latency_file, "\n");
4910 /* Write the prototypes for all automaton functions. */
4911 for (val = tune_attr->first_value; val; val = val->next)
4913 if (val == tune_attr->default_val)
4914 continue;
4915 gcc_assert (GET_CODE (val->value) == CONST_STRING);
4916 fprintf (attr_file,
4917 "extern int internal_dfa_insn_code_%s (rtx_insn *);\n"
4918 "extern int insn_default_latency_%s (rtx_insn *);\n",
4919 XSTR (val->value, 0), XSTR (val->value, 0));
4921 fprintf (attr_file, "\n");
4922 fprintf (attr_file, "int (*internal_dfa_insn_code) (rtx_insn *);\n");
4923 fprintf (attr_file, "int (*insn_default_latency) (rtx_insn *);\n");
4924 fprintf (attr_file, "\n");
4925 fprintf (attr_file, "void\n");
4926 fprintf (attr_file, "init_sched_attrs (void)\n");
4927 fprintf (attr_file, "{\n");
4929 for (val = tune_attr->first_value; val; val = val->next)
4931 int j;
4932 char *name;
4933 rtx test = attr_eq (tune_attr->name, XSTR (val->value, 0));
4935 if (val == tune_attr->default_val)
4936 continue;
4937 for (decl = all_insn_reservs, i = 0;
4938 decl;
4939 decl = decl->next)
4941 rtx ctest = test;
4942 rtx condexp
4943 = simplify_and_tree (decl->condexp, &ctest, -2, 0);
4944 if (condexp == false_rtx)
4945 continue;
4946 if (condexp == true_rtx)
4947 break;
4948 condexps[i] = condexp;
4949 condexps[i + 1] = make_numeric_value (decl->insn_num);
4950 condexps[i + 2] = make_numeric_value (decl->default_latency);
4951 i += 3;
4954 code_exp = rtx_alloc (COND);
4955 lats_exp = rtx_alloc (COND);
4957 j = i / 3 * 2;
4958 XVEC (code_exp, 0) = rtvec_alloc (j);
4959 XVEC (lats_exp, 0) = rtvec_alloc (j);
4961 if (decl)
4963 XEXP (code_exp, 1) = make_numeric_value (decl->insn_num);
4964 XEXP (lats_exp, 1) = make_numeric_value (decl->default_latency);
4966 else
4968 XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
4969 XEXP (lats_exp, 1) = make_numeric_value (0);
4972 while (i > 0)
4974 i -= 3;
4975 j -= 2;
4976 XVECEXP (code_exp, 0, j) = condexps[i];
4977 XVECEXP (lats_exp, 0, j) = condexps[i];
4979 XVECEXP (code_exp, 0, j + 1) = condexps[i + 1];
4980 XVECEXP (lats_exp, 0, j + 1) = condexps[i + 2];
4983 name = XNEWVEC (char,
4984 sizeof ("*internal_dfa_insn_code_")
4985 + strlen (XSTR (val->value, 0)));
4986 strcpy (name, "*internal_dfa_insn_code_");
4987 strcat (name, XSTR (val->value, 0));
4988 make_internal_attr (name, code_exp, ATTR_NONE);
4989 strcpy (name, "*insn_default_latency_");
4990 strcat (name, XSTR (val->value, 0));
4991 make_internal_attr (name, lats_exp, ATTR_NONE);
4992 XDELETEVEC (name);
4994 if (first)
4996 fprintf (attr_file, " if (");
4997 first = false;
4999 else
5000 fprintf (attr_file, " else if (");
5001 write_test_expr (attr_file, test, 0, 0);
5002 fprintf (attr_file, ")\n");
5003 fprintf (attr_file, " {\n");
5004 fprintf (attr_file, " internal_dfa_insn_code\n");
5005 fprintf (attr_file, " = internal_dfa_insn_code_%s;\n",
5006 XSTR (val->value, 0));
5007 fprintf (attr_file, " insn_default_latency\n");
5008 fprintf (attr_file, " = insn_default_latency_%s;\n",
5009 XSTR (val->value, 0));
5010 fprintf (attr_file, " }\n");
5013 fprintf (attr_file, " else\n");
5014 fprintf (attr_file, " gcc_unreachable ();\n");
5015 fprintf (attr_file, "}\n");
5016 fprintf (attr_file, "\n");
5018 XDELETEVEC (condexps);
5020 else
5022 code_exp = rtx_alloc (COND);
5023 lats_exp = rtx_alloc (COND);
5025 XVEC (code_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
5026 XVEC (lats_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
5028 XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
5029 XEXP (lats_exp, 1) = make_numeric_value (0);
5031 for (decl = all_insn_reservs, i = 0;
5032 decl;
5033 decl = decl->next, i += 2)
5035 XVECEXP (code_exp, 0, i) = decl->condexp;
5036 XVECEXP (lats_exp, 0, i) = decl->condexp;
5038 XVECEXP (code_exp, 0, i+1) = make_numeric_value (decl->insn_num);
5039 XVECEXP (lats_exp, 0, i+1)
5040 = make_numeric_value (decl->default_latency);
5042 make_internal_attr ("*internal_dfa_insn_code", code_exp, ATTR_NONE);
5043 make_internal_attr ("*insn_default_latency", lats_exp, ATTR_NONE);
5046 if (n_bypasses == 0)
5047 byps_exp = make_numeric_value (0);
5048 else
5050 process_bypasses ();
5052 byps_exp = rtx_alloc (COND);
5053 XVEC (byps_exp, 0) = rtvec_alloc (n_bypassed * 2);
5054 XEXP (byps_exp, 1) = make_numeric_value (0);
5055 for (decl = all_insn_reservs, i = 0;
5056 decl;
5057 decl = decl->next)
5058 if (decl->bypassed)
5060 XVECEXP (byps_exp, 0, i) = decl->condexp;
5061 XVECEXP (byps_exp, 0, i+1) = make_numeric_value (1);
5062 i += 2;
5066 make_internal_attr ("*bypass_p", byps_exp, ATTR_NONE);
5069 static void
5070 write_header (FILE *outf)
5072 fprintf (outf, "/* Generated automatically by the program `genattrtab'\n"
5073 " from the machine description file `md'. */\n\n");
5075 fprintf (outf, "#include \"config.h\"\n");
5076 fprintf (outf, "#include \"system.h\"\n");
5077 fprintf (outf, "#include \"coretypes.h\"\n");
5078 fprintf (outf, "#include \"backend.h\"\n");
5079 fprintf (outf, "#include \"predict.h\"\n");
5080 fprintf (outf, "#include \"tree.h\"\n");
5081 fprintf (outf, "#include \"rtl.h\"\n");
5082 fprintf (outf, "#include \"alias.h\"\n");
5083 fprintf (outf, "#include \"options.h\"\n");
5084 fprintf (outf, "#include \"varasm.h\"\n");
5085 fprintf (outf, "#include \"stor-layout.h\"\n");
5086 fprintf (outf, "#include \"calls.h\"\n");
5087 fprintf (outf, "#include \"insn-attr.h\"\n");
5088 fprintf (outf, "#include \"memmodel.h\"\n");
5089 fprintf (outf, "#include \"tm_p.h\"\n");
5090 fprintf (outf, "#include \"insn-config.h\"\n");
5091 fprintf (outf, "#include \"recog.h\"\n");
5092 fprintf (outf, "#include \"regs.h\"\n");
5093 fprintf (outf, "#include \"real.h\"\n");
5094 fprintf (outf, "#include \"output.h\"\n");
5095 fprintf (outf, "#include \"toplev.h\"\n");
5096 fprintf (outf, "#include \"flags.h\"\n");
5097 fprintf (outf, "#include \"emit-rtl.h\"\n");
5098 fprintf (outf, "\n");
5099 fprintf (outf, "#define operands recog_data.operand\n\n");
5102 static FILE *
5103 open_outfile (const char *file_name)
5105 FILE *outf;
5106 outf = fopen (file_name, "w");
5107 if (! outf)
5108 fatal ("cannot open file %s: %s", file_name, xstrerror (errno));
5109 write_header (outf);
5110 return outf;
5113 static bool
5114 handle_arg (const char *arg)
5116 switch (arg[1])
5118 case 'A':
5119 attr_file_name = &arg[2];
5120 return true;
5121 case 'D':
5122 dfa_file_name = &arg[2];
5123 return true;
5124 case 'L':
5125 latency_file_name = &arg[2];
5126 return true;
5127 default:
5128 return false;
5133 main (int argc, const char **argv)
5135 struct attr_desc *attr;
5136 struct insn_def *id;
5137 int i;
5139 progname = "genattrtab";
5141 if (!init_rtx_reader_args_cb (argc, argv, handle_arg))
5142 return FATAL_EXIT_CODE;
5144 attr_file = open_outfile (attr_file_name);
5145 dfa_file = open_outfile (dfa_file_name);
5146 latency_file = open_outfile (latency_file_name);
5148 obstack_init (hash_obstack);
5149 obstack_init (temp_obstack);
5151 /* Set up true and false rtx's */
5152 true_rtx = rtx_alloc (CONST_INT);
5153 XWINT (true_rtx, 0) = 1;
5154 false_rtx = rtx_alloc (CONST_INT);
5155 XWINT (false_rtx, 0) = 0;
5156 ATTR_IND_SIMPLIFIED_P (true_rtx) = ATTR_IND_SIMPLIFIED_P (false_rtx) = 1;
5157 ATTR_PERMANENT_P (true_rtx) = ATTR_PERMANENT_P (false_rtx) = 1;
5159 alternative_name = DEF_ATTR_STRING ("alternative");
5160 length_str = DEF_ATTR_STRING ("length");
5161 delay_type_str = DEF_ATTR_STRING ("*delay_type");
5162 delay_1_0_str = DEF_ATTR_STRING ("*delay_1_0");
5163 num_delay_slots_str = DEF_ATTR_STRING ("*num_delay_slots");
5165 /* Read the machine description. */
5167 md_rtx_info info;
5168 while (read_md_rtx (&info))
5170 switch (GET_CODE (info.def))
5172 case DEFINE_INSN:
5173 case DEFINE_PEEPHOLE:
5174 case DEFINE_ASM_ATTRIBUTES:
5175 gen_insn (&info);
5176 break;
5178 case DEFINE_ATTR:
5179 case DEFINE_ENUM_ATTR:
5180 gen_attr (&info);
5181 break;
5183 case DEFINE_DELAY:
5184 gen_delay (&info);
5185 break;
5187 case DEFINE_INSN_RESERVATION:
5188 gen_insn_reserv (&info);
5189 break;
5191 case DEFINE_BYPASS:
5192 gen_bypass (&info);
5193 break;
5195 default:
5196 break;
5198 if (GET_CODE (info.def) != DEFINE_ASM_ATTRIBUTES)
5199 insn_index_number++;
5202 if (have_error)
5203 return FATAL_EXIT_CODE;
5205 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
5206 if (! got_define_asm_attributes)
5208 md_rtx_info info;
5209 info.def = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
5210 XVEC (info.def, 0) = rtvec_alloc (0);
5211 info.loc = file_location ("<internal>", 0, 0);
5212 info.index = -1;
5213 gen_insn (&info);
5216 /* Expand DEFINE_DELAY information into new attribute. */
5217 expand_delays ();
5219 /* Make `insn_alternatives'. */
5220 int num_insn_codes = get_num_insn_codes ();
5221 insn_alternatives = oballocvec (uint64_t, num_insn_codes);
5222 for (id = defs; id; id = id->next)
5223 if (id->insn_code >= 0)
5224 insn_alternatives[id->insn_code]
5225 = (((uint64_t) 1) << id->num_alternatives) - 1;
5227 /* Make `insn_n_alternatives'. */
5228 insn_n_alternatives = oballocvec (int, num_insn_codes);
5229 for (id = defs; id; id = id->next)
5230 if (id->insn_code >= 0)
5231 insn_n_alternatives[id->insn_code] = id->num_alternatives;
5233 /* Construct extra attributes for automata. */
5234 make_automaton_attrs ();
5236 /* Prepare to write out attribute subroutines by checking everything stored
5237 away and building the attribute cases. */
5239 check_defs ();
5241 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5242 for (attr = attrs[i]; attr; attr = attr->next)
5243 attr->default_val->value
5244 = check_attr_value (attr->loc, attr->default_val->value, attr);
5246 if (have_error)
5247 return FATAL_EXIT_CODE;
5249 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5250 for (attr = attrs[i]; attr; attr = attr->next)
5251 fill_attr (attr);
5253 /* Construct extra attributes for `length'. */
5254 make_length_attrs ();
5256 /* Perform any possible optimizations to speed up compilation. */
5257 optimize_attrs (num_insn_codes);
5259 /* Now write out all the `gen_attr_...' routines. Do these before the
5260 special routines so that they get defined before they are used. */
5262 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5263 for (attr = attrs[i]; attr; attr = attr->next)
5265 FILE *outf;
5267 #define IS_ATTR_GROUP(X) (!strncmp (attr->name, X, strlen (X)))
5268 if (IS_ATTR_GROUP ("*internal_dfa_insn_code"))
5269 outf = dfa_file;
5270 else if (IS_ATTR_GROUP ("*insn_default_latency"))
5271 outf = latency_file;
5272 else
5273 outf = attr_file;
5274 #undef IS_ATTR_GROUP
5276 if (! attr->is_special && ! attr->is_const)
5277 write_attr_get (outf, attr);
5280 /* Write out delay eligibility information, if DEFINE_DELAY present.
5281 (The function to compute the number of delay slots will be written
5282 below.) */
5283 write_eligible_delay (attr_file, "delay");
5284 if (have_annul_true)
5285 write_eligible_delay (attr_file, "annul_true");
5286 else
5287 write_dummy_eligible_delay (attr_file, "annul_true");
5288 if (have_annul_false)
5289 write_eligible_delay (attr_file, "annul_false");
5290 else
5291 write_dummy_eligible_delay (attr_file, "annul_false");
5293 /* Write out constant delay slot info. */
5294 write_const_num_delay_slots (attr_file);
5296 write_length_unit_log (attr_file);
5298 if (fclose (attr_file) != 0)
5299 fatal ("cannot close file %s: %s", attr_file_name, xstrerror (errno));
5300 if (fclose (dfa_file) != 0)
5301 fatal ("cannot close file %s: %s", dfa_file_name, xstrerror (errno));
5302 if (fclose (latency_file) != 0)
5303 fatal ("cannot close file %s: %s", latency_file_name, xstrerror (errno));
5305 return SUCCESS_EXIT_CODE;