1 /* Generate code from machine description to compute values of attributes.
2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* This program handles insn attributes and the DEFINE_DELAY and
24 DEFINE_INSN_RESERVATION definitions.
26 It produces a series of functions named `get_attr_...', one for each insn
27 attribute. Each of these is given the rtx for an insn and returns a member
28 of the enum for the attribute.
30 These subroutines have the form of a `switch' on the INSN_CODE (via
31 `recog_memoized'). Each case either returns a constant attribute value
32 or a value that depends on tests on other attributes, the form of
33 operands, or some random C expression (encoded with a SYMBOL_REF
36 If the attribute `alternative', or a random C expression is present,
37 `constrain_operands' is called. If either of these cases of a reference to
38 an operand is found, `extract_insn' is called.
40 The special attribute `length' is also recognized. For this operand,
41 expressions involving the address of an operand or the current insn,
42 (address (pc)), are valid. In this case, an initial pass is made to
43 set all lengths that do not depend on address. Those that do are set to
44 the maximum length. Then each insn that depends on an address is checked
45 and possibly has its length changed. The process repeats until no further
46 changed are made. The resulting lengths are saved for use by
49 A special form of DEFINE_ATTR, where the expression for default value is a
50 CONST expression, indicates an attribute that is constant for a given run
51 of the compiler. The subroutine generated for these attributes has no
52 parameters as it does not depend on any particular insn. Constant
53 attributes are typically used to specify which variety of processor is
56 Internal attributes are defined to handle DEFINE_DELAY and
57 DEFINE_INSN_RESERVATION. Special routines are output for these cases.
59 This program works by keeping a list of possible values for each attribute.
60 These include the basic attribute choices, default values for attribute, and
61 all derived quantities.
63 As the description file is read, the definition for each insn is saved in a
64 `struct insn_def'. When the file reading is complete, a `struct insn_ent'
65 is created for each insn and chained to the corresponding attribute value,
66 either that specified, or the default.
68 An optimization phase is then run. This simplifies expressions for each
69 insn. EQ_ATTR tests are resolved, whenever possible, to a test that
70 indicates when the attribute has the specified value for the insn. This
71 avoids recursive calls during compilation.
73 The strategy used when processing DEFINE_DELAY definitions is to create
74 arbitrarily complex expressions and have the optimization simplify them.
76 Once optimization is complete, any required routines and definitions
79 An optimization that is not yet implemented is to hoist the constant
80 expressions entirely out of the routines and definitions that are written.
81 A way to do this is to iterate over all possible combinations of values
82 for constant attributes and generate a set of functions for that given
83 combination. An initialization function would be written that evaluates
84 the attributes and installs the corresponding set of routines and
85 definitions (each would be accessed through a pointer).
87 We use the flags in an RTX as follows:
88 `unchanging' (ATTR_IND_SIMPLIFIED_P): This rtx is fully simplified
89 independent of the insn code.
90 `in_struct' (ATTR_CURR_SIMPLIFIED_P): This rtx is fully simplified
91 for the insn code currently being processed (see optimize_attrs).
92 `return_val' (ATTR_PERMANENT_P): This rtx is permanent and unique
95 #define ATTR_IND_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), unchanging))
96 #define ATTR_CURR_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), in_struct))
97 #define ATTR_PERMANENT_P(RTX) (RTX_FLAG((RTX), return_val))
100 #define strcmp_check(S1, S2) ((S1) == (S2) \
102 : (gcc_assert (strcmp ((S1), (S2))), 1))
104 #define strcmp_check(S1, S2) ((S1) != (S2))
109 #include "coretypes.h"
115 #include "gensupport.h"
117 /* Flags for make_internal_attr's `special' parameter. */
119 #define ATTR_SPECIAL (1 << 0)
121 static struct obstack obstack1, obstack2;
122 static struct obstack *hash_obstack = &obstack1;
123 static struct obstack *temp_obstack = &obstack2;
125 /* enough space to reserve for printing out ints */
126 #define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)
128 /* Define structures used to record attributes and values. */
130 /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
131 encountered, we store all the relevant information into a
132 `struct insn_def'. This is done to allow attribute definitions to occur
133 anywhere in the file. */
137 struct insn_def *next; /* Next insn in chain. */
138 rtx def; /* The DEFINE_... */
139 int insn_code; /* Instruction number. */
140 int insn_index; /* Expression number in file, for errors. */
141 int lineno; /* Line number. */
142 int num_alternatives; /* Number of alternatives. */
143 int vec_idx; /* Index of attribute vector in `def'. */
146 /* Once everything has been read in, we store in each attribute value a list
147 of insn codes that have that value. Here is the structure used for the
152 struct insn_ent *next; /* Next in chain. */
153 struct insn_def *def; /* Instruction definition. */
156 /* Each value of an attribute (either constant or computed) is assigned a
157 structure which is used as the listhead of the insns that have that
162 rtx value; /* Value of attribute. */
163 struct attr_value *next; /* Next attribute value in chain. */
164 struct insn_ent *first_insn; /* First insn with this value. */
165 int num_insns; /* Number of insns with this value. */
166 int has_asm_insn; /* True if this value used for `asm' insns */
169 /* Structure for each attribute. */
173 char *name; /* Name of attribute. */
174 const char *enum_name; /* Enum name for DEFINE_ENUM_NAME. */
175 struct attr_desc *next; /* Next attribute. */
176 struct attr_value *first_value; /* First value of this attribute. */
177 struct attr_value *default_val; /* Default value for this attribute. */
178 int lineno : 24; /* Line number. */
179 unsigned is_numeric : 1; /* Values of this attribute are numeric. */
180 unsigned is_const : 1; /* Attribute value constant for each run. */
181 unsigned is_special : 1; /* Don't call `write_attr_set'. */
184 /* Structure for each DEFINE_DELAY. */
188 rtx def; /* DEFINE_DELAY expression. */
189 struct delay_desc *next; /* Next DEFINE_DELAY. */
190 int num; /* Number of DEFINE_DELAY, starting at 1. */
191 int lineno; /* Line number. */
194 struct attr_value_list
196 struct attr_value *av;
198 struct attr_desc *attr;
199 struct attr_value_list *next;
202 /* Listheads of above structures. */
204 /* This one is indexed by the first character of the attribute name. */
205 #define MAX_ATTRS_INDEX 256
206 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
207 static struct insn_def *defs;
208 static struct delay_desc *delays;
209 struct attr_value_list **insn_code_values;
211 /* Other variables. */
213 static int insn_code_number;
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
231 static int *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
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 *, ...)
257 static rtx make_numeric_value (int);
258 static struct attr_desc *find_attr (const char **, int);
259 static rtx mk_attr_alt (int);
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 (struct attr_desc *, const char *);
277 static struct attr_value *find_most_used (struct attr_desc *);
278 static void write_attr_set (struct attr_desc *, int, rtx,
279 const char *, const char *, rtx,
281 static void write_attr_case (struct attr_desc *, struct attr_value *,
282 int, const char *, const char *, int, rtx);
283 static void write_attr_value (struct attr_desc *, rtx);
284 static void write_upcase (const char *);
285 static void write_indent (int);
286 static rtx identity_fn (rtx);
287 static rtx zero_fn (rtx);
288 static rtx one_fn (rtx);
289 static rtx max_fn (rtx);
290 static rtx min_fn (rtx);
292 #define oballoc(T) XOBNEW (hash_obstack, T)
293 #define oballocvec(T, N) XOBNEWVEC (hash_obstack, T, (N))
295 /* Hash table for sharing RTL and strings. */
297 /* Each hash table slot is a bucket containing a chain of these structures.
298 Strings are given negative hash codes; RTL expressions are given positive
303 struct attr_hash *next; /* Next structure in the bucket. */
304 int hashcode; /* Hash code of this rtx or string. */
307 char *str; /* The string (negative hash codes) */
308 rtx rtl; /* or the RTL recorded here. */
312 /* Now here is the hash table. When recording an RTL, it is added to
313 the slot whose index is the hash code mod the table size. Note
314 that the hash table is used for several kinds of RTL (see attr_rtx)
315 and for strings. While all these live in the same table, they are
316 completely independent, and the hash code is computed differently
319 #define RTL_HASH_SIZE 4093
320 static struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
322 /* Here is how primitive or already-shared RTL's hash
324 #define RTL_HASH(RTL) ((intptr_t) (RTL) & 0777777)
326 /* Add an entry to the hash table for RTL with hash code HASHCODE. */
329 attr_hash_add_rtx (int hashcode, rtx rtl)
333 h = XOBNEW (hash_obstack, struct attr_hash);
334 h->hashcode = hashcode;
336 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
337 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
340 /* Add an entry to the hash table for STRING with hash code HASHCODE. */
343 attr_hash_add_string (int hashcode, char *str)
347 h = XOBNEW (hash_obstack, struct attr_hash);
348 h->hashcode = -hashcode;
350 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
351 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
354 /* Generate an RTL expression, but avoid duplicates.
355 Set the ATTR_PERMANENT_P flag for these permanent objects.
357 In some cases we cannot uniquify; then we return an ordinary
358 impermanent rtx with ATTR_PERMANENT_P clear.
362 rtx attr_rtx (code, [element1, ..., elementn]) */
365 attr_rtx_1 (enum rtx_code code, va_list p)
367 rtx rt_val = NULL_RTX;/* RTX to return to caller... */
370 struct obstack *old_obstack = rtl_obstack;
372 /* For each of several cases, search the hash table for an existing entry.
373 Use that entry if one is found; otherwise create a new RTL and add it
376 if (GET_RTX_CLASS (code) == RTX_UNARY)
378 rtx arg0 = va_arg (p, rtx);
380 /* A permanent object cannot point to impermanent ones. */
381 if (! ATTR_PERMANENT_P (arg0))
383 rt_val = rtx_alloc (code);
384 XEXP (rt_val, 0) = arg0;
388 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
389 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
390 if (h->hashcode == hashcode
391 && GET_CODE (h->u.rtl) == code
392 && XEXP (h->u.rtl, 0) == arg0)
397 rtl_obstack = hash_obstack;
398 rt_val = rtx_alloc (code);
399 XEXP (rt_val, 0) = arg0;
402 else if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
403 || GET_RTX_CLASS (code) == RTX_COMM_ARITH
404 || GET_RTX_CLASS (code) == RTX_COMPARE
405 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
407 rtx arg0 = va_arg (p, rtx);
408 rtx arg1 = va_arg (p, rtx);
410 /* A permanent object cannot point to impermanent ones. */
411 if (! ATTR_PERMANENT_P (arg0) || ! ATTR_PERMANENT_P (arg1))
413 rt_val = rtx_alloc (code);
414 XEXP (rt_val, 0) = arg0;
415 XEXP (rt_val, 1) = arg1;
419 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
420 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
421 if (h->hashcode == hashcode
422 && GET_CODE (h->u.rtl) == code
423 && XEXP (h->u.rtl, 0) == arg0
424 && XEXP (h->u.rtl, 1) == arg1)
429 rtl_obstack = hash_obstack;
430 rt_val = rtx_alloc (code);
431 XEXP (rt_val, 0) = arg0;
432 XEXP (rt_val, 1) = arg1;
435 else if (GET_RTX_LENGTH (code) == 1
436 && GET_RTX_FORMAT (code)[0] == 's')
438 char *arg0 = va_arg (p, char *);
440 arg0 = DEF_ATTR_STRING (arg0);
442 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
443 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
444 if (h->hashcode == hashcode
445 && GET_CODE (h->u.rtl) == code
446 && XSTR (h->u.rtl, 0) == arg0)
451 rtl_obstack = hash_obstack;
452 rt_val = rtx_alloc (code);
453 XSTR (rt_val, 0) = arg0;
456 else if (GET_RTX_LENGTH (code) == 2
457 && GET_RTX_FORMAT (code)[0] == 's'
458 && GET_RTX_FORMAT (code)[1] == 's')
460 char *arg0 = va_arg (p, char *);
461 char *arg1 = va_arg (p, char *);
463 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
464 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
465 if (h->hashcode == hashcode
466 && GET_CODE (h->u.rtl) == code
467 && XSTR (h->u.rtl, 0) == arg0
468 && XSTR (h->u.rtl, 1) == arg1)
473 rtl_obstack = hash_obstack;
474 rt_val = rtx_alloc (code);
475 XSTR (rt_val, 0) = arg0;
476 XSTR (rt_val, 1) = arg1;
479 else if (code == CONST_INT)
481 HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
491 int i; /* Array indices... */
492 const char *fmt; /* Current rtx's format... */
494 rt_val = rtx_alloc (code); /* Allocate the storage space. */
496 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
497 for (i = 0; i < GET_RTX_LENGTH (code); i++)
501 case '0': /* Unused field. */
504 case 'i': /* An integer? */
505 XINT (rt_val, i) = va_arg (p, int);
508 case 'w': /* A wide integer? */
509 XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
512 case 's': /* A string? */
513 XSTR (rt_val, i) = va_arg (p, char *);
516 case 'e': /* An expression? */
517 case 'u': /* An insn? Same except when printing. */
518 XEXP (rt_val, i) = va_arg (p, rtx);
521 case 'E': /* An RTX vector? */
522 XVEC (rt_val, i) = va_arg (p, rtvec);
532 rtl_obstack = old_obstack;
533 attr_hash_add_rtx (hashcode, rt_val);
534 ATTR_PERMANENT_P (rt_val) = 1;
539 attr_rtx (enum rtx_code code, ...)
545 result = attr_rtx_1 (code, p);
550 /* Create a new string printed with the printf line arguments into a space
551 of at most LEN bytes:
553 rtx attr_printf (len, format, [arg1, ..., argn]) */
556 attr_printf (unsigned int len, const char *fmt, ...)
563 gcc_assert (len < sizeof str); /* Leave room for \0. */
565 vsprintf (str, fmt, p);
568 return DEF_ATTR_STRING (str);
572 attr_eq (const char *name, const char *value)
574 return attr_rtx (EQ_ATTR, DEF_ATTR_STRING (name), DEF_ATTR_STRING (value));
580 return XSTR (make_numeric_value (n), 0);
583 /* Return a permanent (possibly shared) copy of a string STR (not assumed
584 to be null terminated) with LEN bytes. */
587 attr_string (const char *str, int len)
594 /* Compute the hash code. */
595 hashcode = (len + 1) * 613 + (unsigned) str[0];
596 for (i = 1; i < len; i += 2)
597 hashcode = ((hashcode * 613) + (unsigned) str[i]);
599 hashcode = -hashcode;
601 /* Search the table for the string. */
602 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
603 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
604 && !strncmp (h->u.str, str, len))
605 return h->u.str; /* <-- return if found. */
607 /* Not found; create a permanent copy and add it to the hash table. */
608 new_str = XOBNEWVAR (hash_obstack, char, len + 1);
609 memcpy (new_str, str, len);
611 attr_hash_add_string (hashcode, new_str);
613 return new_str; /* Return the new string. */
616 /* Check two rtx's for equality of contents,
617 taking advantage of the fact that if both are hashed
618 then they can't be equal unless they are the same object. */
621 attr_equal_p (rtx x, rtx y)
623 return (x == y || (! (ATTR_PERMANENT_P (x) && ATTR_PERMANENT_P (y))
624 && rtx_equal_p (x, y)));
627 /* Copy an attribute value expression,
628 descending to all depths, but not copying any
629 permanent hashed subexpressions. */
632 attr_copy_rtx (rtx orig)
637 const char *format_ptr;
639 /* No need to copy a permanent object. */
640 if (ATTR_PERMANENT_P (orig))
643 code = GET_CODE (orig);
661 copy = rtx_alloc (code);
662 PUT_MODE (copy, GET_MODE (orig));
663 ATTR_IND_SIMPLIFIED_P (copy) = ATTR_IND_SIMPLIFIED_P (orig);
664 ATTR_CURR_SIMPLIFIED_P (copy) = ATTR_CURR_SIMPLIFIED_P (orig);
665 ATTR_PERMANENT_P (copy) = ATTR_PERMANENT_P (orig);
667 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
669 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
671 switch (*format_ptr++)
674 XEXP (copy, i) = XEXP (orig, i);
675 if (XEXP (orig, i) != NULL)
676 XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
681 XVEC (copy, i) = XVEC (orig, i);
682 if (XVEC (orig, i) != NULL)
684 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
685 for (j = 0; j < XVECLEN (copy, i); j++)
686 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
692 XINT (copy, i) = XINT (orig, i);
696 XWINT (copy, i) = XWINT (orig, i);
701 XSTR (copy, i) = XSTR (orig, i);
711 /* Given a test expression for an attribute, ensure it is validly formed.
712 IS_CONST indicates whether the expression is constant for each compiler
713 run (a constant expression may not test any particular insn).
715 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
716 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
717 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
719 Update the string address in EQ_ATTR expression to be the same used
720 in the attribute (or `alternative_name') to speed up subsequent
721 `find_attr' calls and eliminate most `strcmp' calls.
723 Return the new expression, if any. */
726 check_attr_test (rtx exp, int is_const, int lineno)
728 struct attr_desc *attr;
729 struct attr_value *av;
730 const char *name_ptr, *p;
733 switch (GET_CODE (exp))
736 /* Handle negation test. */
737 if (XSTR (exp, 1)[0] == '!')
738 return check_attr_test (attr_rtx (NOT,
739 attr_eq (XSTR (exp, 0),
743 else if (n_comma_elts (XSTR (exp, 1)) == 1)
745 attr = find_attr (&XSTR (exp, 0), 0);
748 if (! strcmp (XSTR (exp, 0), "alternative"))
749 return mk_attr_alt (1 << atoi (XSTR (exp, 1)));
751 fatal ("unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
754 if (is_const && ! attr->is_const)
755 fatal ("constant expression uses insn attribute `%s' in EQ_ATTR",
758 /* Copy this just to make it permanent,
759 so expressions using it can be permanent too. */
760 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
762 /* It shouldn't be possible to simplify the value given to a
763 constant attribute, so don't expand this until it's time to
764 write the test expression. */
766 ATTR_IND_SIMPLIFIED_P (exp) = 1;
768 if (attr->is_numeric)
770 for (p = XSTR (exp, 1); *p; p++)
772 fatal ("attribute `%s' takes only numeric values",
777 for (av = attr->first_value; av; av = av->next)
778 if (GET_CODE (av->value) == CONST_STRING
779 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
783 fatal ("unknown value `%s' for `%s' attribute",
784 XSTR (exp, 1), XSTR (exp, 0));
789 if (! strcmp (XSTR (exp, 0), "alternative"))
793 name_ptr = XSTR (exp, 1);
794 while ((p = next_comma_elt (&name_ptr)) != NULL)
795 set |= 1 << atoi (p);
797 return mk_attr_alt (set);
801 /* Make an IOR tree of the possible values. */
803 name_ptr = XSTR (exp, 1);
804 while ((p = next_comma_elt (&name_ptr)) != NULL)
806 newexp = attr_eq (XSTR (exp, 0), p);
807 orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
810 return check_attr_test (orexp, is_const, lineno);
819 /* Either TRUE or FALSE. */
827 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
828 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const, lineno);
832 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
837 fatal ("RTL operator \"%s\" not valid in constant attribute test",
838 GET_RTX_NAME (GET_CODE (exp)));
839 /* These cases can't be simplified. */
840 ATTR_IND_SIMPLIFIED_P (exp) = 1;
843 case LE: case LT: case GT: case GE:
844 case LEU: case LTU: case GTU: case GEU:
846 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
847 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
848 exp = attr_rtx (GET_CODE (exp),
849 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
850 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
851 /* These cases can't be simplified. */
852 ATTR_IND_SIMPLIFIED_P (exp) = 1;
858 /* These cases are valid for constant attributes, but can't be
860 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
861 ATTR_IND_SIMPLIFIED_P (exp) = 1;
865 fatal ("RTL operator \"%s\" not valid in attribute test",
866 GET_RTX_NAME (GET_CODE (exp)));
872 /* Given an expression, ensure that it is validly formed and that all named
873 attribute values are valid for the given attribute. Issue a fatal error
874 if not. If no attribute is specified, assume a numeric attribute.
876 Return a perhaps modified replacement expression for the value. */
879 check_attr_value (rtx exp, struct attr_desc *attr)
881 struct attr_value *av;
885 switch (GET_CODE (exp))
888 if (attr && ! attr->is_numeric)
890 error_with_line (attr->lineno,
891 "CONST_INT not valid for non-numeric attribute %s",
896 if (INTVAL (exp) < 0)
898 error_with_line (attr->lineno,
899 "negative numeric value specified for attribute %s",
906 if (! strcmp (XSTR (exp, 0), "*"))
909 if (attr == 0 || attr->is_numeric)
915 error_with_line (attr ? attr->lineno : 0,
916 "non-numeric value for numeric attribute %s",
917 attr ? attr->name : "internal");
923 for (av = attr->first_value; av; av = av->next)
924 if (GET_CODE (av->value) == CONST_STRING
925 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
929 error_with_line (attr->lineno,
930 "unknown value `%s' for `%s' attribute",
931 XSTR (exp, 0), attr ? attr->name : "internal");
935 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
936 attr ? attr->is_const : 0,
937 attr ? attr->lineno : 0);
938 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
939 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
947 if (attr && !attr->is_numeric)
949 error_with_line (attr->lineno,
950 "invalid operation `%s' for non-numeric"
951 " attribute value", GET_RTX_NAME (GET_CODE (exp)));
958 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
959 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
968 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
972 if (XVECLEN (exp, 0) % 2 != 0)
974 error_with_line (attr->lineno,
975 "first operand of COND must have even length");
979 for (i = 0; i < XVECLEN (exp, 0); i += 2)
981 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
982 attr ? attr->is_const : 0,
983 attr ? attr->lineno : 0);
984 XVECEXP (exp, 0, i + 1)
985 = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
988 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
993 struct attr_desc *attr2 = find_attr (&XSTR (exp, 0), 0);
995 error_with_line (attr ? attr->lineno : 0,
996 "unknown attribute `%s' in ATTR",
998 else if (attr && attr->is_const && ! attr2->is_const)
999 error_with_line (attr->lineno,
1000 "non-constant attribute `%s' referenced from `%s'",
1001 XSTR (exp, 0), attr->name);
1003 && attr->is_numeric != attr2->is_numeric)
1004 error_with_line (attr->lineno,
1005 "numeric attribute mismatch calling `%s' from `%s'",
1006 XSTR (exp, 0), attr->name);
1011 /* A constant SYMBOL_REF is valid as a constant attribute test and
1012 is expanded later by make_canonical into a COND. In a non-constant
1013 attribute test, it is left be. */
1014 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1017 error_with_line (attr ? attr->lineno : 0,
1018 "invalid operation `%s' for attribute value",
1019 GET_RTX_NAME (GET_CODE (exp)));
1026 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1027 It becomes a COND with each test being (eq_attr "alternative" "n") */
1030 convert_set_attr_alternative (rtx exp, struct insn_def *id)
1032 int num_alt = id->num_alternatives;
1036 if (XVECLEN (exp, 1) != num_alt)
1038 error_with_line (id->lineno,
1039 "bad number of entries in SET_ATTR_ALTERNATIVE");
1043 /* Make a COND with all tests but the last. Select the last value via the
1045 condexp = rtx_alloc (COND);
1046 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1048 for (i = 0; i < num_alt - 1; i++)
1051 p = attr_numeral (i);
1053 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1054 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1057 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1059 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1062 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1063 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1066 convert_set_attr (rtx exp, struct insn_def *id)
1069 const char *name_ptr;
1073 /* See how many alternative specified. */
1074 n = n_comma_elts (XSTR (exp, 1));
1076 return attr_rtx (SET,
1077 attr_rtx (ATTR, XSTR (exp, 0)),
1078 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1080 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1081 XSTR (newexp, 0) = XSTR (exp, 0);
1082 XVEC (newexp, 1) = rtvec_alloc (n);
1084 /* Process each comma-separated name. */
1085 name_ptr = XSTR (exp, 1);
1087 while ((p = next_comma_elt (&name_ptr)) != NULL)
1088 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1090 return convert_set_attr_alternative (newexp, id);
1093 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1094 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1100 struct insn_def *id;
1101 struct attr_desc *attr;
1105 for (id = defs; id; id = id->next)
1107 if (XVEC (id->def, id->vec_idx) == NULL)
1110 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1112 value = XVECEXP (id->def, id->vec_idx, i);
1113 switch (GET_CODE (value))
1116 if (GET_CODE (XEXP (value, 0)) != ATTR)
1118 error_with_line (id->lineno, "bad attribute set");
1123 case SET_ATTR_ALTERNATIVE:
1124 value = convert_set_attr_alternative (value, id);
1128 value = convert_set_attr (value, id);
1132 error_with_line (id->lineno, "invalid attribute code %s",
1133 GET_RTX_NAME (GET_CODE (value)));
1136 if (value == NULL_RTX)
1139 if ((attr = find_attr (&XSTR (XEXP (value, 0), 0), 0)) == NULL)
1141 error_with_line (id->lineno, "unknown attribute %s",
1142 XSTR (XEXP (value, 0), 0));
1146 XVECEXP (id->def, id->vec_idx, i) = value;
1147 XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1152 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1153 expressions by converting them into a COND. This removes cases from this
1154 program. Also, replace an attribute value of "*" with the default attribute
1158 make_canonical (struct attr_desc *attr, rtx exp)
1163 switch (GET_CODE (exp))
1166 exp = make_numeric_value (INTVAL (exp));
1170 if (! strcmp (XSTR (exp, 0), "*"))
1172 if (attr == 0 || attr->default_val == 0)
1173 fatal ("(attr_value \"*\") used in invalid context");
1174 exp = attr->default_val->value;
1177 XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1182 if (!attr->is_const || ATTR_IND_SIMPLIFIED_P (exp))
1184 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1185 This makes the COND something that won't be considered an arbitrary
1186 expression by walk_attr_value. */
1187 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1188 exp = check_attr_value (exp, attr);
1192 newexp = rtx_alloc (COND);
1193 XVEC (newexp, 0) = rtvec_alloc (2);
1194 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1195 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1197 XEXP (newexp, 1) = XEXP (exp, 2);
1200 /* Fall through to COND case since this is now a COND. */
1207 /* First, check for degenerate COND. */
1208 if (XVECLEN (exp, 0) == 0)
1209 return make_canonical (attr, XEXP (exp, 1));
1210 defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1212 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1214 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1215 XVECEXP (exp, 0, i + 1)
1216 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1217 if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1233 copy_boolean (rtx exp)
1235 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1236 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1237 copy_boolean (XEXP (exp, 1)));
1238 if (GET_CODE (exp) == MATCH_OPERAND)
1240 XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1241 XSTR (exp, 2) = DEF_ATTR_STRING (XSTR (exp, 2));
1243 else if (GET_CODE (exp) == EQ_ATTR)
1245 XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1246 XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1252 /* Given a value and an attribute description, return a `struct attr_value *'
1253 that represents that value. This is either an existing structure, if the
1254 value has been previously encountered, or a newly-created structure.
1256 `insn_code' is the code of an insn whose attribute has the specified
1257 value (-2 if not processing an insn). We ensure that all insns for
1258 a given value have the same number of alternatives if the value checks
1261 static struct attr_value *
1262 get_attr_value (rtx value, struct attr_desc *attr, int insn_code)
1264 struct attr_value *av;
1267 value = make_canonical (attr, value);
1268 if (compares_alternatives_p (value))
1270 if (insn_code < 0 || insn_alternatives == NULL)
1271 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1273 num_alt = insn_alternatives[insn_code];
1276 for (av = attr->first_value; av; av = av->next)
1277 if (rtx_equal_p (value, av->value)
1278 && (num_alt == 0 || av->first_insn == NULL
1279 || insn_alternatives[av->first_insn->def->insn_code]))
1282 av = oballoc (struct attr_value);
1284 av->next = attr->first_value;
1285 attr->first_value = av;
1286 av->first_insn = NULL;
1288 av->has_asm_insn = 0;
1293 /* After all DEFINE_DELAYs have been read in, create internal attributes
1294 to generate the required routines.
1296 First, we compute the number of delay slots for each insn (as a COND of
1297 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1298 delay type is specified, we compute a similar function giving the
1299 DEFINE_DELAY ordinal for each insn.
1301 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1302 tells whether a given insn can be in that delay slot.
1304 Normal attribute filling and optimization expands these to contain the
1305 information needed to handle delay slots. */
1308 expand_delays (void)
1310 struct delay_desc *delay;
1316 /* First, generate data for `num_delay_slots' function. */
1318 condexp = rtx_alloc (COND);
1319 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1320 XEXP (condexp, 1) = make_numeric_value (0);
1322 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1324 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1325 XVECEXP (condexp, 0, i + 1)
1326 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1329 make_internal_attr (num_delay_slots_str, condexp, ATTR_NONE);
1331 /* If more than one delay type, do the same for computing the delay type. */
1334 condexp = rtx_alloc (COND);
1335 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1336 XEXP (condexp, 1) = make_numeric_value (0);
1338 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1340 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1341 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1344 make_internal_attr (delay_type_str, condexp, ATTR_SPECIAL);
1347 /* For each delay possibility and delay slot, compute an eligibility
1348 attribute for non-annulled insns and for each type of annulled (annul
1349 if true and annul if false). */
1350 for (delay = delays; delay; delay = delay->next)
1352 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1354 condexp = XVECEXP (delay->def, 1, i);
1356 condexp = false_rtx;
1357 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1358 make_numeric_value (1), make_numeric_value (0));
1360 p = attr_printf (sizeof "*delay__" + MAX_DIGITS * 2,
1361 "*delay_%d_%d", delay->num, i / 3);
1362 make_internal_attr (p, newexp, ATTR_SPECIAL);
1364 if (have_annul_true)
1366 condexp = XVECEXP (delay->def, 1, i + 1);
1367 if (condexp == 0) condexp = false_rtx;
1368 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1369 make_numeric_value (1),
1370 make_numeric_value (0));
1371 p = attr_printf (sizeof "*annul_true__" + MAX_DIGITS * 2,
1372 "*annul_true_%d_%d", delay->num, i / 3);
1373 make_internal_attr (p, newexp, ATTR_SPECIAL);
1376 if (have_annul_false)
1378 condexp = XVECEXP (delay->def, 1, i + 2);
1379 if (condexp == 0) condexp = false_rtx;
1380 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1381 make_numeric_value (1),
1382 make_numeric_value (0));
1383 p = attr_printf (sizeof "*annul_false__" + MAX_DIGITS * 2,
1384 "*annul_false_%d_%d", delay->num, i / 3);
1385 make_internal_attr (p, newexp, ATTR_SPECIAL);
1391 /* Once all attributes and insns have been read and checked, we construct for
1392 each attribute value a list of all the insns that have that value for
1396 fill_attr (struct attr_desc *attr)
1398 struct attr_value *av;
1399 struct insn_ent *ie;
1400 struct insn_def *id;
1404 /* Don't fill constant attributes. The value is independent of
1405 any particular insn. */
1409 for (id = defs; id; id = id->next)
1411 /* If no value is specified for this insn for this attribute, use the
1414 if (XVEC (id->def, id->vec_idx))
1415 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1416 if (! strcmp_check (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
1418 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
1421 av = attr->default_val;
1423 av = get_attr_value (value, attr, id->insn_code);
1425 ie = oballoc (struct insn_ent);
1427 insert_insn_ent (av, ie);
1431 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
1432 test that checks relative positions of insns (uses MATCH_DUP or PC).
1433 If so, replace it with what is obtained by passing the expression to
1434 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
1435 recursively on each value (including the default value). Otherwise,
1436 return the value returned by NO_ADDRESS_FN applied to EXP. */
1439 substitute_address (rtx exp, rtx (*no_address_fn) (rtx),
1440 rtx (*address_fn) (rtx))
1445 if (GET_CODE (exp) == COND)
1447 /* See if any tests use addresses. */
1449 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1450 walk_attr_value (XVECEXP (exp, 0, i));
1453 return (*address_fn) (exp);
1455 /* Make a new copy of this COND, replacing each element. */
1456 newexp = rtx_alloc (COND);
1457 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
1458 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1460 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
1461 XVECEXP (newexp, 0, i + 1)
1462 = substitute_address (XVECEXP (exp, 0, i + 1),
1463 no_address_fn, address_fn);
1466 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
1467 no_address_fn, address_fn);
1472 else if (GET_CODE (exp) == IF_THEN_ELSE)
1475 walk_attr_value (XEXP (exp, 0));
1477 return (*address_fn) (exp);
1479 return attr_rtx (IF_THEN_ELSE,
1480 substitute_address (XEXP (exp, 0),
1481 no_address_fn, address_fn),
1482 substitute_address (XEXP (exp, 1),
1483 no_address_fn, address_fn),
1484 substitute_address (XEXP (exp, 2),
1485 no_address_fn, address_fn));
1488 return (*no_address_fn) (exp);
1491 /* Make new attributes from the `length' attribute. The following are made,
1492 each corresponding to a function called from `shorten_branches' or
1495 *insn_default_length This is the length of the insn to be returned
1496 by `get_attr_length' before `shorten_branches'
1497 has been called. In each case where the length
1498 depends on relative addresses, the largest
1499 possible is used. This routine is also used
1500 to compute the initial size of the insn.
1502 *insn_variable_length_p This returns 1 if the insn's length depends
1503 on relative addresses, zero otherwise.
1505 *insn_current_length This is only called when it is known that the
1506 insn has a variable length and returns the
1507 current length, based on relative addresses.
1511 make_length_attrs (void)
1513 static const char *new_names[] =
1515 "*insn_default_length",
1517 "*insn_variable_length_p",
1518 "*insn_current_length"
1520 static rtx (*const no_address_fn[]) (rtx)
1521 = {identity_fn,identity_fn, zero_fn, zero_fn};
1522 static rtx (*const address_fn[]) (rtx)
1523 = {max_fn, min_fn, one_fn, identity_fn};
1525 struct attr_desc *length_attr, *new_attr;
1526 struct attr_value *av, *new_av;
1527 struct insn_ent *ie, *new_ie;
1529 /* See if length attribute is defined. If so, it must be numeric. Make
1530 it special so we don't output anything for it. */
1531 length_attr = find_attr (&length_str, 0);
1532 if (length_attr == 0)
1535 if (! length_attr->is_numeric)
1536 fatal ("length attribute must be numeric");
1538 length_attr->is_const = 0;
1539 length_attr->is_special = 1;
1541 /* Make each new attribute, in turn. */
1542 for (i = 0; i < ARRAY_SIZE (new_names); i++)
1544 make_internal_attr (new_names[i],
1545 substitute_address (length_attr->default_val->value,
1546 no_address_fn[i], address_fn[i]),
1548 new_attr = find_attr (&new_names[i], 0);
1549 for (av = length_attr->first_value; av; av = av->next)
1550 for (ie = av->first_insn; ie; ie = ie->next)
1552 new_av = get_attr_value (substitute_address (av->value,
1555 new_attr, ie->def->insn_code);
1556 new_ie = oballoc (struct insn_ent);
1557 new_ie->def = ie->def;
1558 insert_insn_ent (new_av, new_ie);
1563 /* Utility functions called from above routine. */
1566 identity_fn (rtx exp)
1572 zero_fn (rtx exp ATTRIBUTE_UNUSED)
1574 return make_numeric_value (0);
1578 one_fn (rtx exp ATTRIBUTE_UNUSED)
1580 return make_numeric_value (1);
1587 return make_numeric_value (max_attr_value (exp, &unknown));
1594 return make_numeric_value (min_attr_value (exp, &unknown));
1598 write_length_unit_log (void)
1600 struct attr_desc *length_attr = find_attr (&length_str, 0);
1601 struct attr_value *av;
1602 struct insn_ent *ie;
1603 unsigned int length_unit_log, length_or;
1606 if (length_attr == 0)
1608 length_or = or_attr_value (length_attr->default_val->value, &unknown);
1609 for (av = length_attr->first_value; av; av = av->next)
1610 for (ie = av->first_insn; ie; ie = ie->next)
1611 length_or |= or_attr_value (av->value, &unknown);
1614 length_unit_log = 0;
1617 length_or = ~length_or;
1618 for (length_unit_log = 0; length_or & 1; length_or >>= 1)
1621 printf ("EXPORTED_CONST int length_unit_log = %u;\n", length_unit_log);
1624 /* Take a COND expression and see if any of the conditions in it can be
1625 simplified. If any are known true or known false for the particular insn
1626 code, the COND can be further simplified.
1628 Also call ourselves on any COND operations that are values of this COND.
1630 We do not modify EXP; rather, we make and return a new rtx. */
1633 simplify_cond (rtx exp, int insn_code, int insn_index)
1636 /* We store the desired contents here,
1637 then build a new expression if they don't match EXP. */
1638 rtx defval = XEXP (exp, 1);
1639 rtx new_defval = XEXP (exp, 1);
1640 int len = XVECLEN (exp, 0);
1641 rtx *tests = XNEWVEC (rtx, len);
1645 /* This lets us free all storage allocated below, if appropriate. */
1646 obstack_finish (rtl_obstack);
1648 memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
1650 /* See if default value needs simplification. */
1651 if (GET_CODE (defval) == COND)
1652 new_defval = simplify_cond (defval, insn_code, insn_index);
1654 /* Simplify the subexpressions, and see what tests we can get rid of. */
1656 for (i = 0; i < len; i += 2)
1658 rtx newtest, newval;
1660 /* Simplify this test. */
1661 newtest = simplify_test_exp_in_temp (tests[i], insn_code, insn_index);
1664 newval = tests[i + 1];
1665 /* See if this value may need simplification. */
1666 if (GET_CODE (newval) == COND)
1667 newval = simplify_cond (newval, insn_code, insn_index);
1669 /* Look for ways to delete or combine this test. */
1670 if (newtest == true_rtx)
1672 /* If test is true, make this value the default
1673 and discard this + any following tests. */
1675 defval = tests[i + 1];
1676 new_defval = newval;
1679 else if (newtest == false_rtx)
1681 /* If test is false, discard it and its value. */
1682 for (j = i; j < len - 2; j++)
1683 tests[j] = tests[j + 2];
1688 else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
1690 /* If this value and the value for the prev test are the same,
1694 = insert_right_side (IOR, tests[i - 2], newtest,
1695 insn_code, insn_index);
1697 /* Delete this test/value. */
1698 for (j = i; j < len - 2; j++)
1699 tests[j] = tests[j + 2];
1705 tests[i + 1] = newval;
1708 /* If the last test in a COND has the same value
1709 as the default value, that test isn't needed. */
1711 while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
1714 /* See if we changed anything. */
1715 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
1718 for (i = 0; i < len; i++)
1719 if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
1727 if (GET_CODE (defval) == COND)
1728 ret = simplify_cond (defval, insn_code, insn_index);
1736 rtx newexp = rtx_alloc (COND);
1738 XVEC (newexp, 0) = rtvec_alloc (len);
1739 memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
1740 XEXP (newexp, 1) = new_defval;
1747 /* Remove an insn entry from an attribute value. */
1750 remove_insn_ent (struct attr_value *av, struct insn_ent *ie)
1752 struct insn_ent *previe;
1754 if (av->first_insn == ie)
1755 av->first_insn = ie->next;
1758 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
1760 previe->next = ie->next;
1764 if (ie->def->insn_code == -1)
1765 av->has_asm_insn = 0;
1770 /* Insert an insn entry in an attribute value list. */
1773 insert_insn_ent (struct attr_value *av, struct insn_ent *ie)
1775 ie->next = av->first_insn;
1776 av->first_insn = ie;
1778 if (ie->def->insn_code == -1)
1779 av->has_asm_insn = 1;
1784 /* This is a utility routine to take an expression that is a tree of either
1785 AND or IOR expressions and insert a new term. The new term will be
1786 inserted at the right side of the first node whose code does not match
1787 the root. A new node will be created with the root's code. Its left
1788 side will be the old right side and its right side will be the new
1791 If the `term' is itself a tree, all its leaves will be inserted. */
1794 insert_right_side (enum rtx_code code, rtx exp, rtx term, int insn_code, int insn_index)
1798 /* Avoid consing in some special cases. */
1799 if (code == AND && term == true_rtx)
1801 if (code == AND && term == false_rtx)
1803 if (code == AND && exp == true_rtx)
1805 if (code == AND && exp == false_rtx)
1807 if (code == IOR && term == true_rtx)
1809 if (code == IOR && term == false_rtx)
1811 if (code == IOR && exp == true_rtx)
1813 if (code == IOR && exp == false_rtx)
1815 if (attr_equal_p (exp, term))
1818 if (GET_CODE (term) == code)
1820 exp = insert_right_side (code, exp, XEXP (term, 0),
1821 insn_code, insn_index);
1822 exp = insert_right_side (code, exp, XEXP (term, 1),
1823 insn_code, insn_index);
1828 if (GET_CODE (exp) == code)
1830 rtx new_rtx = insert_right_side (code, XEXP (exp, 1),
1831 term, insn_code, insn_index);
1832 if (new_rtx != XEXP (exp, 1))
1833 /* Make a copy of this expression and call recursively. */
1834 newexp = attr_rtx (code, XEXP (exp, 0), new_rtx);
1840 /* Insert the new term. */
1841 newexp = attr_rtx (code, exp, term);
1844 return simplify_test_exp_in_temp (newexp, insn_code, insn_index);
1847 /* If we have an expression which AND's a bunch of
1848 (not (eq_attrq "alternative" "n"))
1849 terms, we may have covered all or all but one of the possible alternatives.
1850 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
1852 This routine is passed an expression and either AND or IOR. It returns a
1853 bitmask indicating which alternatives are mentioned within EXP. */
1856 compute_alternative_mask (rtx exp, enum rtx_code code)
1859 if (GET_CODE (exp) == code)
1860 return compute_alternative_mask (XEXP (exp, 0), code)
1861 | compute_alternative_mask (XEXP (exp, 1), code);
1863 else if (code == AND && GET_CODE (exp) == NOT
1864 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
1865 && XSTR (XEXP (exp, 0), 0) == alternative_name)
1866 string = XSTR (XEXP (exp, 0), 1);
1868 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
1869 && XSTR (exp, 0) == alternative_name)
1870 string = XSTR (exp, 1);
1872 else if (GET_CODE (exp) == EQ_ATTR_ALT)
1874 if (code == AND && XINT (exp, 1))
1875 return XINT (exp, 0);
1877 if (code == IOR && !XINT (exp, 1))
1878 return XINT (exp, 0);
1886 return 1 << (string[0] - '0');
1887 return 1 << atoi (string);
1890 /* Given I, a single-bit mask, return RTX to compare the `alternative'
1891 attribute with the value represented by that bit. */
1894 make_alternative_compare (int mask)
1896 return mk_attr_alt (mask);
1899 /* If we are processing an (eq_attr "attr" "value") test, we find the value
1900 of "attr" for this insn code. From that value, we can compute a test
1901 showing when the EQ_ATTR will be true. This routine performs that
1902 computation. If a test condition involves an address, we leave the EQ_ATTR
1903 intact because addresses are only valid for the `length' attribute.
1905 EXP is the EQ_ATTR expression and ATTR is the attribute to which
1906 it refers. VALUE is the value of that attribute for the insn
1907 corresponding to INSN_CODE and INSN_INDEX. */
1910 evaluate_eq_attr (rtx exp, struct attr_desc *attr, rtx value,
1911 int insn_code, int insn_index)
1918 switch (GET_CODE (value))
1921 if (! strcmp_check (XSTR (value, 0), XSTR (exp, 1)))
1932 gcc_assert (GET_CODE (exp) == EQ_ATTR);
1933 prefix = attr->enum_name ? attr->enum_name : attr->name;
1934 string = ACONCAT ((prefix, "_", XSTR (exp, 1), NULL));
1935 for (p = string; *p; p++)
1938 newexp = attr_rtx (EQ, value,
1939 attr_rtx (SYMBOL_REF,
1940 DEF_ATTR_STRING (string)));
1945 /* We construct an IOR of all the cases for which the
1946 requested attribute value is present. Since we start with
1947 FALSE, if it is not present, FALSE will be returned.
1949 Each case is the AND of the NOT's of the previous conditions with the
1950 current condition; in the default case the current condition is TRUE.
1952 For each possible COND value, call ourselves recursively.
1954 The extra TRUE and FALSE expressions will be eliminated by another
1955 call to the simplification routine. */
1960 for (i = 0; i < XVECLEN (value, 0); i += 2)
1962 rtx this_cond = simplify_test_exp_in_temp (XVECEXP (value, 0, i),
1963 insn_code, insn_index);
1965 right = insert_right_side (AND, andexp, this_cond,
1966 insn_code, insn_index);
1967 right = insert_right_side (AND, right,
1968 evaluate_eq_attr (exp, attr,
1971 insn_code, insn_index),
1972 insn_code, insn_index);
1973 orexp = insert_right_side (IOR, orexp, right,
1974 insn_code, insn_index);
1976 /* Add this condition into the AND expression. */
1977 newexp = attr_rtx (NOT, this_cond);
1978 andexp = insert_right_side (AND, andexp, newexp,
1979 insn_code, insn_index);
1982 /* Handle the default case. */
1983 right = insert_right_side (AND, andexp,
1984 evaluate_eq_attr (exp, attr, XEXP (value, 1),
1985 insn_code, insn_index),
1986 insn_code, insn_index);
1987 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
1994 /* If uses an address, must return original expression. But set the
1995 ATTR_IND_SIMPLIFIED_P bit so we don't try to simplify it again. */
1998 walk_attr_value (newexp);
2002 if (! ATTR_IND_SIMPLIFIED_P (exp))
2003 return copy_rtx_unchanging (exp);
2010 /* This routine is called when an AND of a term with a tree of AND's is
2011 encountered. If the term or its complement is present in the tree, it
2012 can be replaced with TRUE or FALSE, respectively.
2014 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2015 be true and hence are complementary.
2017 There is one special case: If we see
2018 (and (not (eq_attr "att" "v1"))
2019 (eq_attr "att" "v2"))
2020 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2021 replace the term, not anything in the AND tree. So we pass a pointer to
2025 simplify_and_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2030 int left_eliminates_term, right_eliminates_term;
2032 if (GET_CODE (exp) == AND)
2034 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2035 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2036 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2038 newexp = attr_rtx (AND, left, right);
2040 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2044 else if (GET_CODE (exp) == IOR)
2046 /* For the IOR case, we do the same as above, except that we can
2047 only eliminate `term' if both sides of the IOR would do so. */
2049 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2050 left_eliminates_term = (temp == true_rtx);
2053 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2054 right_eliminates_term = (temp == true_rtx);
2056 if (left_eliminates_term && right_eliminates_term)
2059 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2061 newexp = attr_rtx (IOR, left, right);
2063 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2067 /* Check for simplifications. Do some extra checking here since this
2068 routine is called so many times. */
2073 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2076 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2079 else if (GET_CODE (exp) == EQ_ATTR_ALT && GET_CODE (*pterm) == EQ_ATTR_ALT)
2081 if (attr_alt_subset_p (*pterm, exp))
2084 if (attr_alt_subset_of_compl_p (*pterm, exp))
2087 if (attr_alt_subset_p (exp, *pterm))
2093 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2095 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2098 if (! strcmp_check (XSTR (exp, 1), XSTR (*pterm, 1)))
2104 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2105 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2107 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2110 if (! strcmp_check (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2116 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2117 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2119 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2122 if (! strcmp_check (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2128 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2130 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2134 else if (GET_CODE (exp) == NOT)
2136 if (attr_equal_p (XEXP (exp, 0), *pterm))
2140 else if (GET_CODE (*pterm) == NOT)
2142 if (attr_equal_p (XEXP (*pterm, 0), exp))
2146 else if (attr_equal_p (exp, *pterm))
2152 /* Similar to `simplify_and_tree', but for IOR trees. */
2155 simplify_or_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2160 int left_eliminates_term, right_eliminates_term;
2162 if (GET_CODE (exp) == IOR)
2164 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2165 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2166 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2168 newexp = attr_rtx (GET_CODE (exp), left, right);
2170 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2174 else if (GET_CODE (exp) == AND)
2176 /* For the AND case, we do the same as above, except that we can
2177 only eliminate `term' if both sides of the AND would do so. */
2179 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2180 left_eliminates_term = (temp == false_rtx);
2183 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2184 right_eliminates_term = (temp == false_rtx);
2186 if (left_eliminates_term && right_eliminates_term)
2189 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2191 newexp = attr_rtx (GET_CODE (exp), left, right);
2193 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2197 if (attr_equal_p (exp, *pterm))
2200 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
2203 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
2206 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2207 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2208 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
2211 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2212 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
2213 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
2219 /* Compute approximate cost of the expression. Used to decide whether
2220 expression is cheap enough for inline. */
2222 attr_rtx_cost (rtx x)
2228 code = GET_CODE (x);
2241 /* Alternatives don't result into function call. */
2242 if (!strcmp_check (XSTR (x, 0), alternative_name))
2249 const char *fmt = GET_RTX_FORMAT (code);
2250 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2256 for (j = 0; j < XVECLEN (x, i); j++)
2257 cost += attr_rtx_cost (XVECEXP (x, i, j));
2260 cost += attr_rtx_cost (XEXP (x, i));
2270 /* Simplify test expression and use temporary obstack in order to avoid
2271 memory bloat. Use ATTR_IND_SIMPLIFIED to avoid unnecessary simplifications
2272 and avoid unnecessary copying if possible. */
2275 simplify_test_exp_in_temp (rtx exp, int insn_code, int insn_index)
2278 struct obstack *old;
2279 if (ATTR_IND_SIMPLIFIED_P (exp))
2282 rtl_obstack = temp_obstack;
2283 x = simplify_test_exp (exp, insn_code, insn_index);
2285 if (x == exp || rtl_obstack == temp_obstack)
2287 return attr_copy_rtx (x);
2290 /* Returns true if S1 is a subset of S2. */
2293 attr_alt_subset_p (rtx s1, rtx s2)
2295 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2298 return !(XINT (s1, 0) &~ XINT (s2, 0));
2301 return !(XINT (s1, 0) & XINT (s2, 0));
2307 return !(XINT (s2, 0) &~ XINT (s1, 0));
2314 /* Returns true if S1 is a subset of complement of S2. */
2317 attr_alt_subset_of_compl_p (rtx s1, rtx s2)
2319 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2322 return !(XINT (s1, 0) & XINT (s2, 0));
2325 return !(XINT (s1, 0) & ~XINT (s2, 0));
2328 return !(XINT (s2, 0) &~ XINT (s1, 0));
2338 /* Return EQ_ATTR_ALT expression representing intersection of S1 and S2. */
2341 attr_alt_intersection (rtx s1, rtx s2)
2343 rtx result = rtx_alloc (EQ_ATTR_ALT);
2345 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2348 XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
2351 XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
2354 XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
2357 XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
2362 XINT (result, 1) = XINT (s1, 1) & XINT (s2, 1);
2367 /* Return EQ_ATTR_ALT expression representing union of S1 and S2. */
2370 attr_alt_union (rtx s1, rtx s2)
2372 rtx result = rtx_alloc (EQ_ATTR_ALT);
2374 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2377 XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
2380 XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
2383 XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
2386 XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
2392 XINT (result, 1) = XINT (s1, 1) | XINT (s2, 1);
2396 /* Return EQ_ATTR_ALT expression representing complement of S. */
2399 attr_alt_complement (rtx s)
2401 rtx result = rtx_alloc (EQ_ATTR_ALT);
2403 XINT (result, 0) = XINT (s, 0);
2404 XINT (result, 1) = 1 - XINT (s, 1);
2409 /* Return EQ_ATTR_ALT expression representing set containing elements set
2415 rtx result = rtx_alloc (EQ_ATTR_ALT);
2417 XINT (result, 0) = e;
2418 XINT (result, 1) = 0;
2423 /* Given an expression, see if it can be simplified for a particular insn
2424 code based on the values of other attributes being tested. This can
2425 eliminate nested get_attr_... calls.
2427 Note that if an endless recursion is specified in the patterns, the
2428 optimization will loop. However, it will do so in precisely the cases where
2429 an infinite recursion loop could occur during compilation. It's better that
2433 simplify_test_exp (rtx exp, int insn_code, int insn_index)
2436 struct attr_desc *attr;
2437 struct attr_value *av;
2438 struct insn_ent *ie;
2439 struct attr_value_list *iv;
2442 bool left_alt, right_alt;
2444 /* Don't re-simplify something we already simplified. */
2445 if (ATTR_IND_SIMPLIFIED_P (exp) || ATTR_CURR_SIMPLIFIED_P (exp))
2448 switch (GET_CODE (exp))
2451 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2452 if (left == false_rtx)
2454 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2455 if (right == false_rtx)
2458 if (GET_CODE (left) == EQ_ATTR_ALT
2459 && GET_CODE (right) == EQ_ATTR_ALT)
2461 exp = attr_alt_intersection (left, right);
2462 return simplify_test_exp (exp, insn_code, insn_index);
2465 /* If either side is an IOR and we have (eq_attr "alternative" ..")
2466 present on both sides, apply the distributive law since this will
2467 yield simplifications. */
2468 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
2469 && compute_alternative_mask (left, IOR)
2470 && compute_alternative_mask (right, IOR))
2472 if (GET_CODE (left) == IOR)
2479 newexp = attr_rtx (IOR,
2480 attr_rtx (AND, left, XEXP (right, 0)),
2481 attr_rtx (AND, left, XEXP (right, 1)));
2483 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2486 /* Try with the term on both sides. */
2487 right = simplify_and_tree (right, &left, insn_code, insn_index);
2488 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2489 left = simplify_and_tree (left, &right, insn_code, insn_index);
2491 if (left == false_rtx || right == false_rtx)
2493 else if (left == true_rtx)
2497 else if (right == true_rtx)
2501 /* See if all or all but one of the insn's alternatives are specified
2502 in this tree. Optimize if so. */
2504 if (GET_CODE (left) == NOT)
2505 left_alt = (GET_CODE (XEXP (left, 0)) == EQ_ATTR
2506 && XSTR (XEXP (left, 0), 0) == alternative_name);
2508 left_alt = (GET_CODE (left) == EQ_ATTR_ALT
2511 if (GET_CODE (right) == NOT)
2512 right_alt = (GET_CODE (XEXP (right, 0)) == EQ_ATTR
2513 && XSTR (XEXP (right, 0), 0) == alternative_name);
2515 right_alt = (GET_CODE (right) == EQ_ATTR_ALT
2516 && XINT (right, 1));
2519 && (GET_CODE (left) == AND
2521 || GET_CODE (right) == AND
2524 i = compute_alternative_mask (exp, AND);
2525 if (i & ~insn_alternatives[insn_code])
2526 fatal ("invalid alternative specified for pattern number %d",
2529 /* If all alternatives are excluded, this is false. */
2530 i ^= insn_alternatives[insn_code];
2533 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2535 /* If just one excluded, AND a comparison with that one to the
2536 front of the tree. The others will be eliminated by
2537 optimization. We do not want to do this if the insn has one
2538 alternative and we have tested none of them! */
2539 left = make_alternative_compare (i);
2540 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2541 newexp = attr_rtx (AND, left, right);
2543 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2547 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2549 newexp = attr_rtx (AND, left, right);
2550 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2555 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2556 if (left == true_rtx)
2558 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2559 if (right == true_rtx)
2562 if (GET_CODE (left) == EQ_ATTR_ALT
2563 && GET_CODE (right) == EQ_ATTR_ALT)
2565 exp = attr_alt_union (left, right);
2566 return simplify_test_exp (exp, insn_code, insn_index);
2569 right = simplify_or_tree (right, &left, insn_code, insn_index);
2570 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2571 left = simplify_or_tree (left, &right, insn_code, insn_index);
2573 if (right == true_rtx || left == true_rtx)
2575 else if (left == false_rtx)
2579 else if (right == false_rtx)
2584 /* Test for simple cases where the distributive law is useful. I.e.,
2585 convert (ior (and (x) (y))
2591 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
2592 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
2594 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
2596 left = XEXP (left, 0);
2598 newexp = attr_rtx (AND, left, right);
2599 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2602 /* See if all or all but one of the insn's alternatives are specified
2603 in this tree. Optimize if so. */
2605 else if (insn_code >= 0
2606 && (GET_CODE (left) == IOR
2607 || (GET_CODE (left) == EQ_ATTR_ALT
2609 || (GET_CODE (left) == EQ_ATTR
2610 && XSTR (left, 0) == alternative_name)
2611 || GET_CODE (right) == IOR
2612 || (GET_CODE (right) == EQ_ATTR_ALT
2613 && !XINT (right, 1))
2614 || (GET_CODE (right) == EQ_ATTR
2615 && XSTR (right, 0) == alternative_name)))
2617 i = compute_alternative_mask (exp, IOR);
2618 if (i & ~insn_alternatives[insn_code])
2619 fatal ("invalid alternative specified for pattern number %d",
2622 /* If all alternatives are included, this is true. */
2623 i ^= insn_alternatives[insn_code];
2626 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2628 /* If just one excluded, IOR a comparison with that one to the
2629 front of the tree. The others will be eliminated by
2630 optimization. We do not want to do this if the insn has one
2631 alternative and we have tested none of them! */
2632 left = make_alternative_compare (i);
2633 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2634 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
2636 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2640 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2642 newexp = attr_rtx (IOR, left, right);
2643 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2648 if (GET_CODE (XEXP (exp, 0)) == NOT)
2650 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
2651 insn_code, insn_index);
2655 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2656 if (GET_CODE (left) == NOT)
2657 return XEXP (left, 0);
2659 if (left == false_rtx)
2661 if (left == true_rtx)
2664 if (GET_CODE (left) == EQ_ATTR_ALT)
2666 exp = attr_alt_complement (left);
2667 return simplify_test_exp (exp, insn_code, insn_index);
2670 /* Try to apply De`Morgan's laws. */
2671 if (GET_CODE (left) == IOR)
2673 newexp = attr_rtx (AND,
2674 attr_rtx (NOT, XEXP (left, 0)),
2675 attr_rtx (NOT, XEXP (left, 1)));
2677 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2679 else if (GET_CODE (left) == AND)
2681 newexp = attr_rtx (IOR,
2682 attr_rtx (NOT, XEXP (left, 0)),
2683 attr_rtx (NOT, XEXP (left, 1)));
2685 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2687 else if (left != XEXP (exp, 0))
2689 newexp = attr_rtx (NOT, left);
2695 return XINT (exp, 1) ? true_rtx : false_rtx;
2699 if (XSTR (exp, 0) == alternative_name)
2701 newexp = mk_attr_alt (1 << atoi (XSTR (exp, 1)));
2705 /* Look at the value for this insn code in the specified attribute.
2706 We normally can replace this comparison with the condition that
2707 would give this insn the values being tested for. */
2709 && (attr = find_attr (&XSTR (exp, 0), 0)) != NULL)
2714 if (insn_code_values)
2716 for (iv = insn_code_values[insn_code]; iv; iv = iv->next)
2717 if (iv->attr == attr)
2725 for (av = attr->first_value; av; av = av->next)
2726 for (ie = av->first_insn; ie; ie = ie->next)
2727 if (ie->def->insn_code == insn_code)
2734 x = evaluate_eq_attr (exp, attr, av->value,
2735 insn_code, insn_index);
2736 x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
2737 if (attr_rtx_cost(x) < 20)
2747 /* We have already simplified this expression. Simplifying it again
2748 won't buy anything unless we weren't given a valid insn code
2749 to process (i.e., we are canonicalizing something.). */
2751 && ! ATTR_IND_SIMPLIFIED_P (newexp))
2752 return copy_rtx_unchanging (newexp);
2757 /* Optimize the attribute lists by seeing if we can determine conditional
2758 values from the known values of other attributes. This will save subroutine
2759 calls during the compilation. */
2762 optimize_attrs (void)
2764 struct attr_desc *attr;
2765 struct attr_value *av;
2766 struct insn_ent *ie;
2769 struct attr_value_list *ivbuf;
2770 struct attr_value_list *iv;
2772 /* For each insn code, make a list of all the insn_ent's for it,
2773 for all values for all attributes. */
2775 if (num_insn_ents == 0)
2778 /* Make 2 extra elements, for "code" values -2 and -1. */
2779 insn_code_values = XCNEWVEC (struct attr_value_list *, insn_code_number + 2);
2781 /* Offset the table address so we can index by -2 or -1. */
2782 insn_code_values += 2;
2784 iv = ivbuf = XNEWVEC (struct attr_value_list, num_insn_ents);
2786 for (i = 0; i < MAX_ATTRS_INDEX; i++)
2787 for (attr = attrs[i]; attr; attr = attr->next)
2788 for (av = attr->first_value; av; av = av->next)
2789 for (ie = av->first_insn; ie; ie = ie->next)
2794 iv->next = insn_code_values[ie->def->insn_code];
2795 insn_code_values[ie->def->insn_code] = iv;
2799 /* Sanity check on num_insn_ents. */
2800 gcc_assert (iv == ivbuf + num_insn_ents);
2802 /* Process one insn code at a time. */
2803 for (i = -2; i < insn_code_number; i++)
2805 /* Clear the ATTR_CURR_SIMPLIFIED_P flag everywhere relevant.
2806 We use it to mean "already simplified for this insn". */
2807 for (iv = insn_code_values[i]; iv; iv = iv->next)
2808 clear_struct_flag (iv->av->value);
2810 for (iv = insn_code_values[i]; iv; iv = iv->next)
2812 struct obstack *old = rtl_obstack;
2817 if (GET_CODE (av->value) != COND)
2820 rtl_obstack = temp_obstack;
2822 while (GET_CODE (newexp) == COND)
2824 rtx newexp2 = simplify_cond (newexp, ie->def->insn_code,
2825 ie->def->insn_index);
2826 if (newexp2 == newexp)
2832 if (newexp != av->value)
2834 newexp = attr_copy_rtx (newexp);
2835 remove_insn_ent (av, ie);
2836 av = get_attr_value (newexp, attr, ie->def->insn_code);
2838 insert_insn_ent (av, ie);
2844 free (insn_code_values - 2);
2845 insn_code_values = NULL;
2848 /* Clear the ATTR_CURR_SIMPLIFIED_P flag in EXP and its subexpressions. */
2851 clear_struct_flag (rtx x)
2858 ATTR_CURR_SIMPLIFIED_P (x) = 0;
2859 if (ATTR_IND_SIMPLIFIED_P (x))
2862 code = GET_CODE (x);
2882 /* Compare the elements. If any pair of corresponding elements
2883 fail to match, return 0 for the whole things. */
2885 fmt = GET_RTX_FORMAT (code);
2886 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2892 for (j = 0; j < XVECLEN (x, i); j++)
2893 clear_struct_flag (XVECEXP (x, i, j));
2897 clear_struct_flag (XEXP (x, i));
2903 /* Add attribute value NAME to the beginning of ATTR's list. */
2906 add_attr_value (struct attr_desc *attr, const char *name)
2908 struct attr_value *av;
2910 av = oballoc (struct attr_value);
2911 av->value = attr_rtx (CONST_STRING, name);
2912 av->next = attr->first_value;
2913 attr->first_value = av;
2914 av->first_insn = NULL;
2916 av->has_asm_insn = 0;
2919 /* Create table entries for DEFINE_ATTR or DEFINE_ENUM_ATTR. */
2922 gen_attr (rtx exp, int lineno)
2924 struct enum_type *et;
2925 struct enum_value *ev;
2926 struct attr_desc *attr;
2927 const char *name_ptr;
2930 /* Make a new attribute structure. Check for duplicate by looking at
2931 attr->default_val, since it is initialized by this routine. */
2932 attr = find_attr (&XSTR (exp, 0), 1);
2933 if (attr->default_val)
2935 error_with_line (lineno, "duplicate definition for attribute %s",
2937 message_with_line (attr->lineno, "previous definition");
2940 attr->lineno = lineno;
2942 if (GET_CODE (exp) == DEFINE_ENUM_ATTR)
2944 attr->enum_name = XSTR (exp, 1);
2945 et = lookup_enum_type (XSTR (exp, 1));
2946 if (!et || !et->md_p)
2947 error_with_line (lineno, "No define_enum called `%s' defined",
2949 for (ev = et->values; ev; ev = ev->next)
2950 add_attr_value (attr, ev->name);
2952 else if (*XSTR (exp, 1) == '\0')
2953 attr->is_numeric = 1;
2956 name_ptr = XSTR (exp, 1);
2957 while ((p = next_comma_elt (&name_ptr)) != NULL)
2958 add_attr_value (attr, p);
2961 if (GET_CODE (XEXP (exp, 2)) == CONST)
2964 if (attr->is_numeric)
2965 error_with_line (lineno,
2966 "constant attributes may not take numeric values");
2968 /* Get rid of the CONST node. It is allowed only at top-level. */
2969 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
2972 if (! strcmp_check (attr->name, length_str) && ! attr->is_numeric)
2973 error_with_line (lineno, "`length' attribute must take numeric values");
2975 /* Set up the default value. */
2976 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
2977 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
2980 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
2981 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
2982 number of alternatives as this should be checked elsewhere. */
2985 count_alternatives (rtx exp)
2990 if (GET_CODE (exp) == MATCH_OPERAND)
2991 return n_comma_elts (XSTR (exp, 2));
2993 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
2994 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
2999 n = count_alternatives (XEXP (exp, i));
3006 if (XVEC (exp, i) != NULL)
3007 for (j = 0; j < XVECLEN (exp, i); j++)
3009 n = count_alternatives (XVECEXP (exp, i, j));
3018 /* Returns nonzero if the given expression contains an EQ_ATTR with the
3019 `alternative' attribute. */
3022 compares_alternatives_p (rtx exp)
3027 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
3030 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3031 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3036 if (compares_alternatives_p (XEXP (exp, i)))
3041 for (j = 0; j < XVECLEN (exp, i); j++)
3042 if (compares_alternatives_p (XVECEXP (exp, i, j)))
3050 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
3053 gen_insn (rtx exp, int lineno)
3055 struct insn_def *id;
3057 id = oballoc (struct insn_def);
3061 id->lineno = lineno;
3063 switch (GET_CODE (exp))
3066 id->insn_code = insn_code_number;
3067 id->insn_index = insn_index_number;
3068 id->num_alternatives = count_alternatives (exp);
3069 if (id->num_alternatives == 0)
3070 id->num_alternatives = 1;
3074 case DEFINE_PEEPHOLE:
3075 id->insn_code = insn_code_number;
3076 id->insn_index = insn_index_number;
3077 id->num_alternatives = count_alternatives (exp);
3078 if (id->num_alternatives == 0)
3079 id->num_alternatives = 1;
3083 case DEFINE_ASM_ATTRIBUTES:
3085 id->insn_index = -1;
3086 id->num_alternatives = 1;
3088 got_define_asm_attributes = 1;
3096 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
3097 true or annul false is specified, and make a `struct delay_desc'. */
3100 gen_delay (rtx def, int lineno)
3102 struct delay_desc *delay;
3105 if (XVECLEN (def, 1) % 3 != 0)
3107 error_with_line (lineno,
3108 "number of elements in DEFINE_DELAY must"
3109 " be multiple of three");
3113 for (i = 0; i < XVECLEN (def, 1); i += 3)
3115 if (XVECEXP (def, 1, i + 1))
3116 have_annul_true = 1;
3117 if (XVECEXP (def, 1, i + 2))
3118 have_annul_false = 1;
3121 delay = oballoc (struct delay_desc);
3123 delay->num = ++num_delays;
3124 delay->next = delays;
3125 delay->lineno = lineno;
3129 /* Given a piece of RTX, print a C expression to test its truth value.
3130 We use AND and IOR both for logical and bit-wise operations, so
3131 interpret them as logical unless they are inside a comparison expression.
3132 The first bit of FLAGS will be nonzero in that case.
3134 Set the second bit of FLAGS to make references to attribute values use
3135 a cached local variable instead of calling a function. */
3138 write_test_expr (rtx exp, int flags)
3140 int comparison_operator = 0;
3142 struct attr_desc *attr;
3144 /* In order not to worry about operator precedence, surround our part of
3145 the expression with parentheses. */
3148 code = GET_CODE (exp);
3151 /* Binary operators. */
3154 printf ("(unsigned) ");
3160 comparison_operator = 1;
3162 case PLUS: case MINUS: case MULT: case DIV: case MOD:
3163 case AND: case IOR: case XOR:
3164 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3165 write_test_expr (XEXP (exp, 0), flags | comparison_operator);
3181 printf (" >= (unsigned) ");
3184 printf (" > (unsigned) ");
3193 printf (" <= (unsigned) ");
3196 printf (" < (unsigned) ");
3239 write_test_expr (XEXP (exp, 1), flags | comparison_operator);
3243 /* Special-case (not (eq_attrq "alternative" "x")) */
3244 if (! (flags & 1) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3245 && XSTR (XEXP (exp, 0), 0) == alternative_name)
3247 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
3251 /* Otherwise, fall through to normal unary operator. */
3253 /* Unary operators. */
3273 write_test_expr (XEXP (exp, 0), flags);
3278 int set = XINT (exp, 0), bit = 0;
3281 fatal ("EQ_ATTR_ALT not valid inside comparison");
3284 fatal ("Empty EQ_ATTR_ALT should be optimized out");
3286 if (!(set & (set - 1)))
3288 if (!(set & 0xffff))
3311 printf ("which_alternative %s= %d",
3312 XINT (exp, 1) ? "!" : "=", bit);
3316 printf ("%s((1 << which_alternative) & %#x)",
3317 XINT (exp, 1) ? "!" : "", set);
3322 /* Comparison test of an attribute with a value. Most of these will
3323 have been removed by optimization. Handle "alternative"
3324 specially and give error if EQ_ATTR present inside a comparison. */
3327 fatal ("EQ_ATTR not valid inside comparison");
3329 if (XSTR (exp, 0) == alternative_name)
3331 printf ("which_alternative == %s", XSTR (exp, 1));
3335 attr = find_attr (&XSTR (exp, 0), 0);
3338 /* Now is the time to expand the value of a constant attribute. */
3341 write_test_expr (evaluate_eq_attr (exp, attr,
3342 attr->default_val->value, -2, -2),
3348 printf ("attr_%s", attr->name);
3350 printf ("get_attr_%s (insn)", attr->name);
3352 write_attr_valueq (attr, XSTR (exp, 1));
3356 /* Comparison test of flags for define_delays. */
3359 fatal ("ATTR_FLAG not valid inside comparison");
3360 printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
3363 /* See if an operand matches a predicate. */
3365 /* If only a mode is given, just ensure the mode matches the operand.
3366 If neither a mode nor predicate is given, error. */
3367 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
3369 if (GET_MODE (exp) == VOIDmode)
3370 fatal ("null MATCH_OPERAND specified as test");
3372 printf ("GET_MODE (operands[%d]) == %smode",
3373 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3376 printf ("%s (operands[%d], %smode)",
3377 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3380 /* Constant integer. */
3382 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
3385 /* A random C expression. */
3387 print_c_condition (XSTR (exp, 0));
3390 /* The address of the branch target. */
3392 printf ("INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
3393 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
3397 /* The address of the current insn. We implement this actually as the
3398 address of the current insn for backward branches, but the last
3399 address of the next insn for forward branches, and both with
3400 adjustments that account for the worst-case possible stretching of
3401 intervening alignments between this insn and its destination. */
3402 printf ("insn_current_reference_address (insn)");
3406 printf ("%s", XSTR (exp, 0));
3410 write_test_expr (XEXP (exp, 0), flags & 2);
3412 write_test_expr (XEXP (exp, 1), flags | 1);
3414 write_test_expr (XEXP (exp, 2), flags | 1);
3418 fatal ("bad RTX code `%s' in attribute calculation\n",
3419 GET_RTX_NAME (code));
3425 /* Given an attribute value, return the maximum CONST_STRING argument
3426 encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
3429 max_attr_value (rtx exp, int *unknownp)
3434 switch (GET_CODE (exp))
3437 current_max = atoi (XSTR (exp, 0));
3441 current_max = max_attr_value (XEXP (exp, 1), unknownp);
3442 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3444 n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3445 if (n > current_max)
3451 current_max = max_attr_value (XEXP (exp, 1), unknownp);
3452 n = max_attr_value (XEXP (exp, 2), unknownp);
3453 if (n > current_max)
3459 current_max = INT_MAX;
3466 /* Given an attribute value, return the minimum CONST_STRING argument
3467 encountered. Set *UNKNOWNP and return 0 if the value is unknown. */
3470 min_attr_value (rtx exp, int *unknownp)
3475 switch (GET_CODE (exp))
3478 current_min = atoi (XSTR (exp, 0));
3482 current_min = min_attr_value (XEXP (exp, 1), unknownp);
3483 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3485 n = min_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3486 if (n < current_min)
3492 current_min = min_attr_value (XEXP (exp, 1), unknownp);
3493 n = min_attr_value (XEXP (exp, 2), unknownp);
3494 if (n < current_min)
3500 current_min = INT_MAX;
3507 /* Given an attribute value, return the result of ORing together all
3508 CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
3509 if the numeric value is not known. */
3512 or_attr_value (rtx exp, int *unknownp)
3517 switch (GET_CODE (exp))
3520 current_or = atoi (XSTR (exp, 0));
3524 current_or = or_attr_value (XEXP (exp, 1), unknownp);
3525 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3526 current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3530 current_or = or_attr_value (XEXP (exp, 1), unknownp);
3531 current_or |= or_attr_value (XEXP (exp, 2), unknownp);
3543 /* Scan an attribute value, possibly a conditional, and record what actions
3544 will be required to do any conditional tests in it.
3547 `must_extract' if we need to extract the insn operands
3548 `must_constrain' if we must compute `which_alternative'
3549 `address_used' if an address expression was used
3550 `length_used' if an (eq_attr "length" ...) was used
3554 walk_attr_value (rtx exp)
3563 code = GET_CODE (exp);
3567 if (! ATTR_IND_SIMPLIFIED_P (exp))
3568 /* Since this is an arbitrary expression, it can look at anything.
3569 However, constant expressions do not depend on any particular
3571 must_extract = must_constrain = 1;
3579 must_extract = must_constrain = 1;
3583 if (XSTR (exp, 0) == alternative_name)
3584 must_extract = must_constrain = 1;
3585 else if (strcmp_check (XSTR (exp, 0), length_str) == 0)
3605 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
3610 walk_attr_value (XEXP (exp, i));
3614 if (XVEC (exp, i) != NULL)
3615 for (j = 0; j < XVECLEN (exp, i); j++)
3616 walk_attr_value (XVECEXP (exp, i, j));
3621 /* Write out a function to obtain the attribute for a given INSN. */
3624 write_attr_get (struct attr_desc *attr)
3626 struct attr_value *av, *common_av;
3628 /* Find the most used attribute value. Handle that as the `default' of the
3629 switch we will generate. */
3630 common_av = find_most_used (attr);
3632 /* Write out start of function, then all values with explicit `case' lines,
3633 then a `default', then the value with the most uses. */
3634 if (attr->enum_name)
3635 printf ("enum %s\n", attr->enum_name);
3636 else if (!attr->is_numeric)
3637 printf ("enum attr_%s\n", attr->name);
3641 /* If the attribute name starts with a star, the remainder is the name of
3642 the subroutine to use, instead of `get_attr_...'. */
3643 if (attr->name[0] == '*')
3644 printf ("%s (rtx insn ATTRIBUTE_UNUSED)\n", &attr->name[1]);
3645 else if (attr->is_const == 0)
3646 printf ("get_attr_%s (rtx insn ATTRIBUTE_UNUSED)\n", attr->name);
3649 printf ("get_attr_%s (void)\n", attr->name);
3652 for (av = attr->first_value; av; av = av->next)
3653 if (av->num_insns == 1)
3654 write_attr_set (attr, 2, av->value, "return", ";",
3655 true_rtx, av->first_insn->def->insn_code,
3656 av->first_insn->def->insn_index);
3657 else if (av->num_insns != 0)
3658 write_attr_set (attr, 2, av->value, "return", ";",
3666 printf (" switch (recog_memoized (insn))\n");
3669 for (av = attr->first_value; av; av = av->next)
3670 if (av != common_av)
3671 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
3673 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
3674 printf (" }\n}\n\n");
3677 /* Given an AND tree of known true terms (because we are inside an `if' with
3678 that as the condition or are in an `else' clause) and an expression,
3679 replace any known true terms with TRUE. Use `simplify_and_tree' to do
3680 the bulk of the work. */
3683 eliminate_known_true (rtx known_true, rtx exp, int insn_code, int insn_index)
3687 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
3689 if (GET_CODE (known_true) == AND)
3691 exp = eliminate_known_true (XEXP (known_true, 0), exp,
3692 insn_code, insn_index);
3693 exp = eliminate_known_true (XEXP (known_true, 1), exp,
3694 insn_code, insn_index);
3699 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
3705 /* Write out a series of tests and assignment statements to perform tests and
3706 sets of an attribute value. We are passed an indentation amount and prefix
3707 and suffix strings to write around each attribute value (e.g., "return"
3711 write_attr_set (struct attr_desc *attr, int indent, rtx value,
3712 const char *prefix, const char *suffix, rtx known_true,
3713 int insn_code, int insn_index)
3715 if (GET_CODE (value) == COND)
3717 /* Assume the default value will be the default of the COND unless we
3718 find an always true expression. */
3719 rtx default_val = XEXP (value, 1);
3720 rtx our_known_true = known_true;
3725 for (i = 0; i < XVECLEN (value, 0); i += 2)
3730 testexp = eliminate_known_true (our_known_true,
3731 XVECEXP (value, 0, i),
3732 insn_code, insn_index);
3733 newexp = attr_rtx (NOT, testexp);
3734 newexp = insert_right_side (AND, our_known_true, newexp,
3735 insn_code, insn_index);
3737 /* If the test expression is always true or if the next `known_true'
3738 expression is always false, this is the last case, so break
3739 out and let this value be the `else' case. */
3740 if (testexp == true_rtx || newexp == false_rtx)
3742 default_val = XVECEXP (value, 0, i + 1);
3746 /* Compute the expression to pass to our recursive call as being
3748 inner_true = insert_right_side (AND, our_known_true,
3749 testexp, insn_code, insn_index);
3751 /* If this is always false, skip it. */
3752 if (inner_true == false_rtx)
3755 write_indent (indent);
3756 printf ("%sif ", first_if ? "" : "else ");
3758 write_test_expr (testexp, 0);
3760 write_indent (indent + 2);
3763 write_attr_set (attr, indent + 4,
3764 XVECEXP (value, 0, i + 1), prefix, suffix,
3765 inner_true, insn_code, insn_index);
3766 write_indent (indent + 2);
3768 our_known_true = newexp;
3773 write_indent (indent);
3775 write_indent (indent + 2);
3779 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
3780 prefix, suffix, our_known_true, insn_code, insn_index);
3784 write_indent (indent + 2);
3790 write_indent (indent);
3791 printf ("%s ", prefix);
3792 write_attr_value (attr, value);
3793 printf ("%s\n", suffix);
3797 /* Write a series of case statements for every instruction in list IE.
3798 INDENT is the amount of indentation to write before each case. */
3801 write_insn_cases (struct insn_ent *ie, int indent)
3803 for (; ie != 0; ie = ie->next)
3804 if (ie->def->insn_code != -1)
3806 write_indent (indent);
3807 if (GET_CODE (ie->def->def) == DEFINE_PEEPHOLE)
3808 printf ("case %d: /* define_peephole, line %d */\n",
3809 ie->def->insn_code, ie->def->lineno);
3811 printf ("case %d: /* %s */\n",
3812 ie->def->insn_code, XSTR (ie->def->def, 0));
3816 /* Write out the computation for one attribute value. */
3819 write_attr_case (struct attr_desc *attr, struct attr_value *av,
3820 int write_case_lines, const char *prefix, const char *suffix,
3821 int indent, rtx known_true)
3823 if (av->num_insns == 0)
3826 if (av->has_asm_insn)
3828 write_indent (indent);
3829 printf ("case -1:\n");
3830 write_indent (indent + 2);
3831 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
3832 write_indent (indent + 2);
3833 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
3834 write_indent (indent + 2);
3835 printf (" fatal_insn_not_found (insn);\n");
3838 if (write_case_lines)
3839 write_insn_cases (av->first_insn, indent);
3842 write_indent (indent);
3843 printf ("default:\n");
3846 /* See what we have to do to output this value. */
3847 must_extract = must_constrain = address_used = 0;
3848 walk_attr_value (av->value);
3852 write_indent (indent + 2);
3853 printf ("extract_constrain_insn_cached (insn);\n");
3855 else if (must_extract)
3857 write_indent (indent + 2);
3858 printf ("extract_insn_cached (insn);\n");
3861 if (av->num_insns == 1)
3862 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
3863 known_true, av->first_insn->def->insn_code,
3864 av->first_insn->def->insn_index);
3866 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
3869 if (strncmp (prefix, "return", 6))
3871 write_indent (indent + 2);
3872 printf ("break;\n");
3877 /* Utilities to write in various forms. */
3880 write_attr_valueq (struct attr_desc *attr, const char *s)
3882 if (attr->is_numeric)
3888 if (num > 9 || num < 0)
3889 printf (" /* %#x */", num);
3893 write_upcase (attr->enum_name ? attr->enum_name : attr->name);
3900 write_attr_value (struct attr_desc *attr, rtx value)
3904 switch (GET_CODE (value))
3907 write_attr_valueq (attr, XSTR (value, 0));
3911 printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
3915 print_c_condition (XSTR (value, 0));
3920 struct attr_desc *attr2 = find_attr (&XSTR (value, 0), 0);
3921 printf ("get_attr_%s (%s)", attr2->name,
3922 (attr2->is_const ? "" : "insn"));
3943 write_attr_value (attr, XEXP (value, 0));
3947 write_attr_value (attr, XEXP (value, 1));
3956 write_upcase (const char *str)
3960 /* The argument of TOUPPER should not have side effects. */
3961 putchar (TOUPPER(*str));
3967 write_indent (int indent)
3969 for (; indent > 8; indent -= 8)
3972 for (; indent; indent--)
3976 /* Write a subroutine that is given an insn that requires a delay slot, a
3977 delay slot ordinal, and a candidate insn. It returns nonzero if the
3978 candidate can be placed in the specified delay slot of the insn.
3980 We can write as many as three subroutines. `eligible_for_delay'
3981 handles normal delay slots, `eligible_for_annul_true' indicates that
3982 the specified insn can be annulled if the branch is true, and likewise
3983 for `eligible_for_annul_false'.
3985 KIND is a string distinguishing these three cases ("delay", "annul_true",
3986 or "annul_false"). */
3989 write_eligible_delay (const char *kind)
3991 struct delay_desc *delay;
3995 struct attr_desc *attr;
3996 struct attr_value *av, *common_av;
3999 /* Compute the maximum number of delay slots required. We use the delay
4000 ordinal times this number plus one, plus the slot number as an index into
4001 the appropriate predicate to test. */
4003 for (delay = delays, max_slots = 0; delay; delay = delay->next)
4004 if (XVECLEN (delay->def, 1) / 3 > max_slots)
4005 max_slots = XVECLEN (delay->def, 1) / 3;
4007 /* Write function prelude. */
4010 printf ("eligible_for_%s (rtx delay_insn ATTRIBUTE_UNUSED, int slot, rtx candidate_insn, int flags ATTRIBUTE_UNUSED)\n",
4013 printf (" rtx insn;\n");
4015 printf (" gcc_assert (slot < %d);\n", max_slots);
4017 /* Allow dbr_schedule to pass labels, etc. This can happen if try_split
4018 converts a compound instruction into a loop. */
4019 printf (" if (!INSN_P (candidate_insn))\n");
4020 printf (" return 0;\n");
4023 /* If more than one delay type, find out which type the delay insn is. */
4027 attr = find_attr (&delay_type_str, 0);
4029 common_av = find_most_used (attr);
4031 printf (" insn = delay_insn;\n");
4032 printf (" switch (recog_memoized (insn))\n");
4035 sprintf (str, " * %d;\n break;", max_slots);
4036 for (av = attr->first_value; av; av = av->next)
4037 if (av != common_av)
4038 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
4040 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
4043 /* Ensure matched. Otherwise, shouldn't have been called. */
4044 printf (" gcc_assert (slot >= %d);\n\n", max_slots);
4047 /* If just one type of delay slot, write simple switch. */
4048 if (num_delays == 1 && max_slots == 1)
4050 printf (" insn = candidate_insn;\n");
4051 printf (" switch (recog_memoized (insn))\n");
4054 attr = find_attr (&delay_1_0_str, 0);
4056 common_av = find_most_used (attr);
4058 for (av = attr->first_value; av; av = av->next)
4059 if (av != common_av)
4060 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
4062 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
4068 /* Write a nested CASE. The first indicates which condition we need to
4069 test, and the inner CASE tests the condition. */
4070 printf (" insn = candidate_insn;\n");
4071 printf (" switch (slot)\n");
4074 for (delay = delays; delay; delay = delay->next)
4075 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
4077 printf (" case %d:\n",
4078 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
4079 printf (" switch (recog_memoized (insn))\n");
4082 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
4084 attr = find_attr (&pstr, 0);
4086 common_av = find_most_used (attr);
4088 for (av = attr->first_value; av; av = av->next)
4089 if (av != common_av)
4090 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
4092 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
4096 printf (" default:\n");
4097 printf (" gcc_unreachable ();\n");
4104 /* This page contains miscellaneous utility routines. */
4106 /* Given a pointer to a (char *), return a malloc'ed string containing the
4107 next comma-separated element. Advance the pointer to after the string
4108 scanned, or the end-of-string. Return NULL if at end of string. */
4111 next_comma_elt (const char **pstr)
4115 start = scan_comma_elt (pstr);
4120 return attr_string (start, *pstr - start);
4123 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
4124 is nonzero, build a new attribute, if one does not exist. *NAME_P is
4125 replaced by a pointer to a canonical copy of the string. */
4127 static struct attr_desc *
4128 find_attr (const char **name_p, int create)
4130 struct attr_desc *attr;
4132 const char *name = *name_p;
4134 /* Before we resort to using `strcmp', see if the string address matches
4135 anywhere. In most cases, it should have been canonicalized to do so. */
4136 if (name == alternative_name)
4139 index = name[0] & (MAX_ATTRS_INDEX - 1);
4140 for (attr = attrs[index]; attr; attr = attr->next)
4141 if (name == attr->name)
4144 /* Otherwise, do it the slow way. */
4145 for (attr = attrs[index]; attr; attr = attr->next)
4146 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
4148 *name_p = attr->name;
4155 attr = oballoc (struct attr_desc);
4156 attr->name = DEF_ATTR_STRING (name);
4157 attr->enum_name = 0;
4158 attr->first_value = attr->default_val = NULL;
4159 attr->is_numeric = attr->is_const = attr->is_special = 0;
4160 attr->next = attrs[index];
4161 attrs[index] = attr;
4163 *name_p = attr->name;
4168 /* Create internal attribute with the given default value. */
4171 make_internal_attr (const char *name, rtx value, int special)
4173 struct attr_desc *attr;
4175 attr = find_attr (&name, 1);
4176 gcc_assert (!attr->default_val);
4178 attr->is_numeric = 1;
4180 attr->is_special = (special & ATTR_SPECIAL) != 0;
4181 attr->default_val = get_attr_value (value, attr, -2);
4184 /* Find the most used value of an attribute. */
4186 static struct attr_value *
4187 find_most_used (struct attr_desc *attr)
4189 struct attr_value *av;
4190 struct attr_value *most_used;
4196 for (av = attr->first_value; av; av = av->next)
4197 if (av->num_insns > nuses)
4198 nuses = av->num_insns, most_used = av;
4203 /* Return (attr_value "n") */
4206 make_numeric_value (int n)
4208 static rtx int_values[20];
4212 gcc_assert (n >= 0);
4214 if (n < 20 && int_values[n])
4215 return int_values[n];
4217 p = attr_printf (MAX_DIGITS, "%d", n);
4218 exp = attr_rtx (CONST_STRING, p);
4221 int_values[n] = exp;
4227 copy_rtx_unchanging (rtx orig)
4229 if (ATTR_IND_SIMPLIFIED_P (orig) || ATTR_CURR_SIMPLIFIED_P (orig))
4232 ATTR_CURR_SIMPLIFIED_P (orig) = 1;
4236 /* Determine if an insn has a constant number of delay slots, i.e., the
4237 number of delay slots is not a function of the length of the insn. */
4240 write_const_num_delay_slots (void)
4242 struct attr_desc *attr = find_attr (&num_delay_slots_str, 0);
4243 struct attr_value *av;
4247 printf ("int\nconst_num_delay_slots (rtx insn)\n");
4249 printf (" switch (recog_memoized (insn))\n");
4252 for (av = attr->first_value; av; av = av->next)
4255 walk_attr_value (av->value);
4257 write_insn_cases (av->first_insn, 4);
4260 printf (" default:\n");
4261 printf (" return 1;\n");
4262 printf (" }\n}\n\n");
4266 /* Synthetic attributes used by insn-automata.c and the scheduler.
4267 These are primarily concerned with (define_insn_reservation)
4272 struct insn_reserv *next;
4275 int default_latency;
4278 /* Sequence number of this insn. */
4281 /* Whether a (define_bypass) construct names this insn in its
4286 static struct insn_reserv *all_insn_reservs = 0;
4287 static struct insn_reserv **last_insn_reserv_p = &all_insn_reservs;
4288 static size_t n_insn_reservs;
4290 /* Store information from a DEFINE_INSN_RESERVATION for future
4291 attribute generation. */
4293 gen_insn_reserv (rtx def)
4295 struct insn_reserv *decl = oballoc (struct insn_reserv);
4297 decl->name = DEF_ATTR_STRING (XSTR (def, 0));
4298 decl->default_latency = XINT (def, 1);
4299 decl->condexp = check_attr_test (XEXP (def, 2), 0, 0);
4300 decl->insn_num = n_insn_reservs;
4301 decl->bypassed = false;
4304 *last_insn_reserv_p = decl;
4305 last_insn_reserv_p = &decl->next;
4309 /* Store information from a DEFINE_BYPASS for future attribute
4310 generation. The only thing we care about is the list of output
4311 insns, which will later be used to tag reservation structures with
4312 a 'bypassed' bit. */
4316 struct bypass_list *next;
4320 static struct bypass_list *all_bypasses;
4321 static size_t n_bypasses;
4324 gen_bypass_1 (const char *s, size_t len)
4326 struct bypass_list *b;
4331 s = attr_string (s, len);
4332 for (b = all_bypasses; b; b = b->next)
4334 return; /* already got that one */
4336 b = oballoc (struct bypass_list);
4338 b->next = all_bypasses;
4344 gen_bypass (rtx def)
4346 const char *p, *base;
4348 for (p = base = XSTR (def, 1); *p; p++)
4351 gen_bypass_1 (base, p - base);
4354 while (ISSPACE (*p));
4357 gen_bypass_1 (base, p - base);
4360 /* Find and mark all of the bypassed insns. */
4362 process_bypasses (void)
4364 struct bypass_list *b;
4365 struct insn_reserv *r;
4367 /* The reservation list is likely to be much longer than the bypass
4369 for (r = all_insn_reservs; r; r = r->next)
4370 for (b = all_bypasses; b; b = b->next)
4371 if (r->name == b->insn)
4375 /* Create all of the attributes that describe automaton properties. */
4377 make_automaton_attrs (void)
4380 struct insn_reserv *decl;
4381 rtx code_exp, lats_exp, byps_exp;
4383 if (n_insn_reservs == 0)
4386 code_exp = rtx_alloc (COND);
4387 lats_exp = rtx_alloc (COND);
4389 XVEC (code_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
4390 XVEC (lats_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
4392 XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
4393 XEXP (lats_exp, 1) = make_numeric_value (0);
4395 for (decl = all_insn_reservs, i = 0;
4397 decl = decl->next, i += 2)
4399 XVECEXP (code_exp, 0, i) = decl->condexp;
4400 XVECEXP (lats_exp, 0, i) = decl->condexp;
4402 XVECEXP (code_exp, 0, i+1) = make_numeric_value (decl->insn_num);
4403 XVECEXP (lats_exp, 0, i+1) = make_numeric_value (decl->default_latency);
4406 if (n_bypasses == 0)
4407 byps_exp = make_numeric_value (0);
4410 process_bypasses ();
4412 byps_exp = rtx_alloc (COND);
4413 XVEC (byps_exp, 0) = rtvec_alloc (n_bypasses * 2);
4414 XEXP (byps_exp, 1) = make_numeric_value (0);
4415 for (decl = all_insn_reservs, i = 0;
4420 XVECEXP (byps_exp, 0, i) = decl->condexp;
4421 XVECEXP (byps_exp, 0, i+1) = make_numeric_value (1);
4426 make_internal_attr ("*internal_dfa_insn_code", code_exp, ATTR_NONE);
4427 make_internal_attr ("*insn_default_latency", lats_exp, ATTR_NONE);
4428 make_internal_attr ("*bypass_p", byps_exp, ATTR_NONE);
4432 main (int argc, char **argv)
4435 struct attr_desc *attr;
4436 struct insn_def *id;
4440 progname = "genattrtab";
4442 if (!init_rtx_reader_args (argc, argv))
4443 return (FATAL_EXIT_CODE);
4445 obstack_init (hash_obstack);
4446 obstack_init (temp_obstack);
4448 /* Set up true and false rtx's */
4449 true_rtx = rtx_alloc (CONST_INT);
4450 XWINT (true_rtx, 0) = 1;
4451 false_rtx = rtx_alloc (CONST_INT);
4452 XWINT (false_rtx, 0) = 0;
4453 ATTR_IND_SIMPLIFIED_P (true_rtx) = ATTR_IND_SIMPLIFIED_P (false_rtx) = 1;
4454 ATTR_PERMANENT_P (true_rtx) = ATTR_PERMANENT_P (false_rtx) = 1;
4456 alternative_name = DEF_ATTR_STRING ("alternative");
4457 length_str = DEF_ATTR_STRING ("length");
4458 delay_type_str = DEF_ATTR_STRING ("*delay_type");
4459 delay_1_0_str = DEF_ATTR_STRING ("*delay_1_0");
4460 num_delay_slots_str = DEF_ATTR_STRING ("*num_delay_slots");
4462 printf ("/* Generated automatically by the program `genattrtab'\n\
4463 from the machine description file `md'. */\n\n");
4465 /* Read the machine description. */
4471 desc = read_md_rtx (&lineno, &insn_code_number);
4475 switch (GET_CODE (desc))
4478 case DEFINE_PEEPHOLE:
4479 case DEFINE_ASM_ATTRIBUTES:
4480 gen_insn (desc, lineno);
4484 case DEFINE_ENUM_ATTR:
4485 gen_attr (desc, lineno);
4489 gen_delay (desc, lineno);
4492 case DEFINE_INSN_RESERVATION:
4493 gen_insn_reserv (desc);
4503 if (GET_CODE (desc) != DEFINE_ASM_ATTRIBUTES)
4504 insn_index_number++;
4508 return FATAL_EXIT_CODE;
4512 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
4513 if (! got_define_asm_attributes)
4515 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
4516 XVEC (tem, 0) = rtvec_alloc (0);
4520 /* Expand DEFINE_DELAY information into new attribute. */
4524 printf ("#include \"config.h\"\n");
4525 printf ("#include \"system.h\"\n");
4526 printf ("#include \"coretypes.h\"\n");
4527 printf ("#include \"tm.h\"\n");
4528 printf ("#include \"rtl.h\"\n");
4529 printf ("#include \"insn-attr.h\"\n");
4530 printf ("#include \"tm_p.h\"\n");
4531 printf ("#include \"insn-config.h\"\n");
4532 printf ("#include \"recog.h\"\n");
4533 printf ("#include \"regs.h\"\n");
4534 printf ("#include \"output.h\"\n");
4535 printf ("#include \"toplev.h\"\n");
4536 printf ("#include \"flags.h\"\n");
4537 printf ("#include \"function.h\"\n");
4539 printf ("#define operands recog_data.operand\n\n");
4541 /* Make `insn_alternatives'. */
4542 insn_alternatives = oballocvec (int, insn_code_number);
4543 for (id = defs; id; id = id->next)
4544 if (id->insn_code >= 0)
4545 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
4547 /* Make `insn_n_alternatives'. */
4548 insn_n_alternatives = oballocvec (int, insn_code_number);
4549 for (id = defs; id; id = id->next)
4550 if (id->insn_code >= 0)
4551 insn_n_alternatives[id->insn_code] = id->num_alternatives;
4553 /* Construct extra attributes for automata. */
4554 make_automaton_attrs ();
4556 /* Prepare to write out attribute subroutines by checking everything stored
4557 away and building the attribute cases. */
4561 for (i = 0; i < MAX_ATTRS_INDEX; i++)
4562 for (attr = attrs[i]; attr; attr = attr->next)
4563 attr->default_val->value
4564 = check_attr_value (attr->default_val->value, attr);
4567 return FATAL_EXIT_CODE;
4569 for (i = 0; i < MAX_ATTRS_INDEX; i++)
4570 for (attr = attrs[i]; attr; attr = attr->next)
4573 /* Construct extra attributes for `length'. */
4574 make_length_attrs ();
4576 /* Perform any possible optimizations to speed up compilation. */
4579 /* Now write out all the `gen_attr_...' routines. Do these before the
4580 special routines so that they get defined before they are used. */
4582 for (i = 0; i < MAX_ATTRS_INDEX; i++)
4583 for (attr = attrs[i]; attr; attr = attr->next)
4585 if (! attr->is_special && ! attr->is_const)
4586 write_attr_get (attr);
4589 /* Write out delay eligibility information, if DEFINE_DELAY present.
4590 (The function to compute the number of delay slots will be written
4594 write_eligible_delay ("delay");
4595 if (have_annul_true)
4596 write_eligible_delay ("annul_true");
4597 if (have_annul_false)
4598 write_eligible_delay ("annul_false");
4601 /* Write out constant delay slot info. */
4602 write_const_num_delay_slots ();
4604 write_length_unit_log ();
4607 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);