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, 2010
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"
119 /* Flags for make_internal_attr's `special' parameter. */
121 #define ATTR_SPECIAL (1 << 0)
123 static struct obstack obstack1, obstack2;
124 static struct obstack *hash_obstack = &obstack1;
125 static struct obstack *temp_obstack = &obstack2;
127 /* enough space to reserve for printing out ints */
128 #define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)
130 /* Define structures used to record attributes and values. */
132 /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
133 encountered, we store all the relevant information into a
134 `struct insn_def'. This is done to allow attribute definitions to occur
135 anywhere in the file. */
139 struct insn_def *next; /* Next insn in chain. */
140 rtx def; /* The DEFINE_... */
141 int insn_code; /* Instruction number. */
142 int insn_index; /* Expression number in file, for errors. */
143 int lineno; /* Line number. */
144 int num_alternatives; /* Number of alternatives. */
145 int vec_idx; /* Index of attribute vector in `def'. */
148 /* Once everything has been read in, we store in each attribute value a list
149 of insn codes that have that value. Here is the structure used for the
154 struct insn_ent *next; /* Next in chain. */
155 struct insn_def *def; /* Instruction definition. */
158 /* Each value of an attribute (either constant or computed) is assigned a
159 structure which is used as the listhead of the insns that have that
164 rtx value; /* Value of attribute. */
165 struct attr_value *next; /* Next attribute value in chain. */
166 struct insn_ent *first_insn; /* First insn with this value. */
167 int num_insns; /* Number of insns with this value. */
168 int has_asm_insn; /* True if this value used for `asm' insns */
171 /* Structure for each attribute. */
175 char *name; /* Name of attribute. */
176 const char *enum_name; /* Enum name for DEFINE_ENUM_NAME. */
177 struct attr_desc *next; /* Next attribute. */
178 struct attr_value *first_value; /* First value of this attribute. */
179 struct attr_value *default_val; /* Default value for this attribute. */
180 int lineno : 24; /* Line number. */
181 unsigned is_numeric : 1; /* Values of this attribute are numeric. */
182 unsigned is_const : 1; /* Attribute value constant for each run. */
183 unsigned is_special : 1; /* Don't call `write_attr_set'. */
186 /* Structure for each DEFINE_DELAY. */
190 rtx def; /* DEFINE_DELAY expression. */
191 struct delay_desc *next; /* Next DEFINE_DELAY. */
192 int num; /* Number of DEFINE_DELAY, starting at 1. */
193 int lineno; /* Line number. */
196 struct attr_value_list
198 struct attr_value *av;
200 struct attr_desc *attr;
201 struct attr_value_list *next;
204 /* Listheads of above structures. */
206 /* This one is indexed by the first character of the attribute name. */
207 #define MAX_ATTRS_INDEX 256
208 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
209 static struct insn_def *defs;
210 static struct delay_desc *delays;
211 struct attr_value_list **insn_code_values;
213 /* Other variables. */
215 static int insn_code_number;
216 static int insn_index_number;
217 static int got_define_asm_attributes;
218 static int must_extract;
219 static int must_constrain;
220 static int address_used;
221 static int length_used;
222 static int num_delays;
223 static int have_annul_true, have_annul_false;
224 static int num_insn_ents;
226 /* Stores, for each insn code, the number of constraint alternatives. */
228 static int *insn_n_alternatives;
230 /* Stores, for each insn code, a bitmap that has bits on for each possible
233 static int *insn_alternatives;
235 /* Used to simplify expressions. */
237 static rtx true_rtx, false_rtx;
239 /* Used to reduce calls to `strcmp' */
241 static const char *alternative_name;
242 static const char *length_str;
243 static const char *delay_type_str;
244 static const char *delay_1_0_str;
245 static const char *num_delay_slots_str;
247 /* Simplify an expression. Only call the routine if there is something to
249 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
250 (ATTR_IND_SIMPLIFIED_P (EXP) || ATTR_CURR_SIMPLIFIED_P (EXP) ? (EXP) \
251 : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
253 #define DEF_ATTR_STRING(S) (attr_string ((S), strlen (S)))
255 /* Forward declarations of functions used before their definitions, only. */
256 static char *attr_string (const char *, int);
257 static char *attr_printf (unsigned int, const char *, ...)
259 static rtx make_numeric_value (int);
260 static struct attr_desc *find_attr (const char **, int);
261 static rtx mk_attr_alt (int);
262 static char *next_comma_elt (const char **);
263 static rtx insert_right_side (enum rtx_code, rtx, rtx, int, int);
264 static rtx copy_boolean (rtx);
265 static int compares_alternatives_p (rtx);
266 static void make_internal_attr (const char *, rtx, int);
267 static void insert_insn_ent (struct attr_value *, struct insn_ent *);
268 static void walk_attr_value (rtx);
269 static int max_attr_value (rtx, int*);
270 static int min_attr_value (rtx, int*);
271 static int or_attr_value (rtx, int*);
272 static rtx simplify_test_exp (rtx, int, int);
273 static rtx simplify_test_exp_in_temp (rtx, int, int);
274 static rtx copy_rtx_unchanging (rtx);
275 static bool attr_alt_subset_p (rtx, rtx);
276 static bool attr_alt_subset_of_compl_p (rtx, rtx);
277 static void clear_struct_flag (rtx);
278 static void write_attr_valueq (struct attr_desc *, const char *);
279 static struct attr_value *find_most_used (struct attr_desc *);
280 static void write_attr_set (struct attr_desc *, int, rtx,
281 const char *, const char *, rtx,
282 int, int, unsigned int);
283 static void write_attr_case (struct attr_desc *, struct attr_value *,
284 int, const char *, const char *, int, rtx);
285 static void write_attr_value (struct attr_desc *, rtx);
286 static void write_upcase (const char *);
287 static void write_indent (int);
288 static rtx identity_fn (rtx);
289 static rtx zero_fn (rtx);
290 static rtx one_fn (rtx);
291 static rtx max_fn (rtx);
292 static rtx min_fn (rtx);
294 #define oballoc(T) XOBNEW (hash_obstack, T)
295 #define oballocvec(T, N) XOBNEWVEC (hash_obstack, T, (N))
297 /* Hash table for sharing RTL and strings. */
299 /* Each hash table slot is a bucket containing a chain of these structures.
300 Strings are given negative hash codes; RTL expressions are given positive
305 struct attr_hash *next; /* Next structure in the bucket. */
306 int hashcode; /* Hash code of this rtx or string. */
309 char *str; /* The string (negative hash codes) */
310 rtx rtl; /* or the RTL recorded here. */
314 /* Now here is the hash table. When recording an RTL, it is added to
315 the slot whose index is the hash code mod the table size. Note
316 that the hash table is used for several kinds of RTL (see attr_rtx)
317 and for strings. While all these live in the same table, they are
318 completely independent, and the hash code is computed differently
321 #define RTL_HASH_SIZE 4093
322 static struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
324 /* Here is how primitive or already-shared RTL's hash
326 #define RTL_HASH(RTL) ((intptr_t) (RTL) & 0777777)
328 /* Add an entry to the hash table for RTL with hash code HASHCODE. */
331 attr_hash_add_rtx (int hashcode, rtx rtl)
335 h = XOBNEW (hash_obstack, struct attr_hash);
336 h->hashcode = hashcode;
338 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
339 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
342 /* Add an entry to the hash table for STRING with hash code HASHCODE. */
345 attr_hash_add_string (int hashcode, char *str)
349 h = XOBNEW (hash_obstack, struct attr_hash);
350 h->hashcode = -hashcode;
352 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
353 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
356 /* Generate an RTL expression, but avoid duplicates.
357 Set the ATTR_PERMANENT_P flag for these permanent objects.
359 In some cases we cannot uniquify; then we return an ordinary
360 impermanent rtx with ATTR_PERMANENT_P clear.
364 rtx attr_rtx (code, [element1, ..., elementn]) */
367 attr_rtx_1 (enum rtx_code code, va_list p)
369 rtx rt_val = NULL_RTX;/* RTX to return to caller... */
372 struct obstack *old_obstack = rtl_obstack;
374 /* For each of several cases, search the hash table for an existing entry.
375 Use that entry if one is found; otherwise create a new RTL and add it
378 if (GET_RTX_CLASS (code) == RTX_UNARY)
380 rtx arg0 = va_arg (p, rtx);
382 /* A permanent object cannot point to impermanent ones. */
383 if (! ATTR_PERMANENT_P (arg0))
385 rt_val = rtx_alloc (code);
386 XEXP (rt_val, 0) = arg0;
390 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
391 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
392 if (h->hashcode == hashcode
393 && GET_CODE (h->u.rtl) == code
394 && XEXP (h->u.rtl, 0) == arg0)
399 rtl_obstack = hash_obstack;
400 rt_val = rtx_alloc (code);
401 XEXP (rt_val, 0) = arg0;
404 else if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
405 || GET_RTX_CLASS (code) == RTX_COMM_ARITH
406 || GET_RTX_CLASS (code) == RTX_COMPARE
407 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
409 rtx arg0 = va_arg (p, rtx);
410 rtx arg1 = va_arg (p, rtx);
412 /* A permanent object cannot point to impermanent ones. */
413 if (! ATTR_PERMANENT_P (arg0) || ! ATTR_PERMANENT_P (arg1))
415 rt_val = rtx_alloc (code);
416 XEXP (rt_val, 0) = arg0;
417 XEXP (rt_val, 1) = arg1;
421 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
422 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
423 if (h->hashcode == hashcode
424 && GET_CODE (h->u.rtl) == code
425 && XEXP (h->u.rtl, 0) == arg0
426 && XEXP (h->u.rtl, 1) == arg1)
431 rtl_obstack = hash_obstack;
432 rt_val = rtx_alloc (code);
433 XEXP (rt_val, 0) = arg0;
434 XEXP (rt_val, 1) = arg1;
437 else if (code == SYMBOL_REF
438 || (GET_RTX_LENGTH (code) == 1
439 && GET_RTX_FORMAT (code)[0] == 's'))
441 char *arg0 = va_arg (p, char *);
443 arg0 = DEF_ATTR_STRING (arg0);
445 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
446 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
447 if (h->hashcode == hashcode
448 && GET_CODE (h->u.rtl) == code
449 && XSTR (h->u.rtl, 0) == arg0)
454 rtl_obstack = hash_obstack;
455 rt_val = rtx_alloc (code);
456 XSTR (rt_val, 0) = arg0;
457 if (code == SYMBOL_REF)
459 X0EXP (rt_val, 1) = NULL_RTX;
460 X0EXP (rt_val, 2) = NULL_RTX;
464 else if (GET_RTX_LENGTH (code) == 2
465 && GET_RTX_FORMAT (code)[0] == 's'
466 && GET_RTX_FORMAT (code)[1] == 's')
468 char *arg0 = va_arg (p, char *);
469 char *arg1 = va_arg (p, char *);
471 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
472 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
473 if (h->hashcode == hashcode
474 && GET_CODE (h->u.rtl) == code
475 && XSTR (h->u.rtl, 0) == arg0
476 && XSTR (h->u.rtl, 1) == arg1)
481 rtl_obstack = hash_obstack;
482 rt_val = rtx_alloc (code);
483 XSTR (rt_val, 0) = arg0;
484 XSTR (rt_val, 1) = arg1;
487 else if (code == CONST_INT)
489 HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
499 int i; /* Array indices... */
500 const char *fmt; /* Current rtx's format... */
502 rt_val = rtx_alloc (code); /* Allocate the storage space. */
504 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
505 for (i = 0; i < GET_RTX_LENGTH (code); i++)
509 case '0': /* Unused field. */
512 case 'i': /* An integer? */
513 XINT (rt_val, i) = va_arg (p, int);
516 case 'w': /* A wide integer? */
517 XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
520 case 's': /* A string? */
521 XSTR (rt_val, i) = va_arg (p, char *);
524 case 'e': /* An expression? */
525 case 'u': /* An insn? Same except when printing. */
526 XEXP (rt_val, i) = va_arg (p, rtx);
529 case 'E': /* An RTX vector? */
530 XVEC (rt_val, i) = va_arg (p, rtvec);
540 rtl_obstack = old_obstack;
541 attr_hash_add_rtx (hashcode, rt_val);
542 ATTR_PERMANENT_P (rt_val) = 1;
547 attr_rtx (enum rtx_code code, ...)
553 result = attr_rtx_1 (code, p);
558 /* Create a new string printed with the printf line arguments into a space
559 of at most LEN bytes:
561 rtx attr_printf (len, format, [arg1, ..., argn]) */
564 attr_printf (unsigned int len, const char *fmt, ...)
571 gcc_assert (len < sizeof str); /* Leave room for \0. */
573 vsprintf (str, fmt, p);
576 return DEF_ATTR_STRING (str);
580 attr_eq (const char *name, const char *value)
582 return attr_rtx (EQ_ATTR, DEF_ATTR_STRING (name), DEF_ATTR_STRING (value));
588 return XSTR (make_numeric_value (n), 0);
591 /* Return a permanent (possibly shared) copy of a string STR (not assumed
592 to be null terminated) with LEN bytes. */
595 attr_string (const char *str, int len)
602 /* Compute the hash code. */
603 hashcode = (len + 1) * 613 + (unsigned) str[0];
604 for (i = 1; i < len; i += 2)
605 hashcode = ((hashcode * 613) + (unsigned) str[i]);
607 hashcode = -hashcode;
609 /* Search the table for the string. */
610 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
611 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
612 && !strncmp (h->u.str, str, len))
613 return h->u.str; /* <-- return if found. */
615 /* Not found; create a permanent copy and add it to the hash table. */
616 new_str = XOBNEWVAR (hash_obstack, char, len + 1);
617 memcpy (new_str, str, len);
619 attr_hash_add_string (hashcode, new_str);
620 copy_md_ptr_loc (new_str, str);
622 return new_str; /* Return the new string. */
625 /* Check two rtx's for equality of contents,
626 taking advantage of the fact that if both are hashed
627 then they can't be equal unless they are the same object. */
630 attr_equal_p (rtx x, rtx y)
632 return (x == y || (! (ATTR_PERMANENT_P (x) && ATTR_PERMANENT_P (y))
633 && rtx_equal_p (x, y)));
636 /* Copy an attribute value expression,
637 descending to all depths, but not copying any
638 permanent hashed subexpressions. */
641 attr_copy_rtx (rtx orig)
646 const char *format_ptr;
648 /* No need to copy a permanent object. */
649 if (ATTR_PERMANENT_P (orig))
652 code = GET_CODE (orig);
670 copy = rtx_alloc (code);
671 PUT_MODE (copy, GET_MODE (orig));
672 ATTR_IND_SIMPLIFIED_P (copy) = ATTR_IND_SIMPLIFIED_P (orig);
673 ATTR_CURR_SIMPLIFIED_P (copy) = ATTR_CURR_SIMPLIFIED_P (orig);
674 ATTR_PERMANENT_P (copy) = ATTR_PERMANENT_P (orig);
676 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
678 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
680 switch (*format_ptr++)
683 XEXP (copy, i) = XEXP (orig, i);
684 if (XEXP (orig, i) != NULL)
685 XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
690 XVEC (copy, i) = XVEC (orig, i);
691 if (XVEC (orig, i) != NULL)
693 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
694 for (j = 0; j < XVECLEN (copy, i); j++)
695 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
701 XINT (copy, i) = XINT (orig, i);
705 XWINT (copy, i) = XWINT (orig, i);
710 XSTR (copy, i) = XSTR (orig, i);
720 /* Given a test expression for an attribute, ensure it is validly formed.
721 IS_CONST indicates whether the expression is constant for each compiler
722 run (a constant expression may not test any particular insn).
724 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
725 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
726 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
728 Update the string address in EQ_ATTR expression to be the same used
729 in the attribute (or `alternative_name') to speed up subsequent
730 `find_attr' calls and eliminate most `strcmp' calls.
732 Return the new expression, if any. */
735 check_attr_test (rtx exp, int is_const, int lineno)
737 struct attr_desc *attr;
738 struct attr_value *av;
739 const char *name_ptr, *p;
742 switch (GET_CODE (exp))
745 /* Handle negation test. */
746 if (XSTR (exp, 1)[0] == '!')
747 return check_attr_test (attr_rtx (NOT,
748 attr_eq (XSTR (exp, 0),
752 else if (n_comma_elts (XSTR (exp, 1)) == 1)
754 attr = find_attr (&XSTR (exp, 0), 0);
757 if (! strcmp (XSTR (exp, 0), "alternative"))
758 return mk_attr_alt (1 << atoi (XSTR (exp, 1)));
760 fatal ("unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
763 if (is_const && ! attr->is_const)
764 fatal ("constant expression uses insn attribute `%s' in EQ_ATTR",
767 /* Copy this just to make it permanent,
768 so expressions using it can be permanent too. */
769 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
771 /* It shouldn't be possible to simplify the value given to a
772 constant attribute, so don't expand this until it's time to
773 write the test expression. */
775 ATTR_IND_SIMPLIFIED_P (exp) = 1;
777 if (attr->is_numeric)
779 for (p = XSTR (exp, 1); *p; p++)
781 fatal ("attribute `%s' takes only numeric values",
786 for (av = attr->first_value; av; av = av->next)
787 if (GET_CODE (av->value) == CONST_STRING
788 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
792 fatal ("unknown value `%s' for `%s' attribute",
793 XSTR (exp, 1), XSTR (exp, 0));
798 if (! strcmp (XSTR (exp, 0), "alternative"))
802 name_ptr = XSTR (exp, 1);
803 while ((p = next_comma_elt (&name_ptr)) != NULL)
804 set |= 1 << atoi (p);
806 return mk_attr_alt (set);
810 /* Make an IOR tree of the possible values. */
812 name_ptr = XSTR (exp, 1);
813 while ((p = next_comma_elt (&name_ptr)) != NULL)
815 newexp = attr_eq (XSTR (exp, 0), p);
816 orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
819 return check_attr_test (orexp, is_const, lineno);
828 /* Either TRUE or FALSE. */
836 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
837 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const, lineno);
841 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
846 fatal ("RTL operator \"%s\" not valid in constant attribute test",
847 GET_RTX_NAME (GET_CODE (exp)));
848 /* These cases can't be simplified. */
849 ATTR_IND_SIMPLIFIED_P (exp) = 1;
852 case LE: case LT: case GT: case GE:
853 case LEU: case LTU: case GTU: case GEU:
855 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
856 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
857 exp = attr_rtx (GET_CODE (exp),
858 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
859 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
860 /* These cases can't be simplified. */
861 ATTR_IND_SIMPLIFIED_P (exp) = 1;
867 /* These cases are valid for constant attributes, but can't be
869 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
870 ATTR_IND_SIMPLIFIED_P (exp) = 1;
874 fatal ("RTL operator \"%s\" not valid in attribute test",
875 GET_RTX_NAME (GET_CODE (exp)));
881 /* Given an expression, ensure that it is validly formed and that all named
882 attribute values are valid for the given attribute. Issue a fatal error
883 if not. If no attribute is specified, assume a numeric attribute.
885 Return a perhaps modified replacement expression for the value. */
888 check_attr_value (rtx exp, struct attr_desc *attr)
890 struct attr_value *av;
894 switch (GET_CODE (exp))
897 if (attr && ! attr->is_numeric)
899 error_with_line (attr->lineno,
900 "CONST_INT not valid for non-numeric attribute %s",
905 if (INTVAL (exp) < 0)
907 error_with_line (attr->lineno,
908 "negative numeric value specified for attribute %s",
915 if (! strcmp (XSTR (exp, 0), "*"))
918 if (attr == 0 || attr->is_numeric)
924 error_with_line (attr ? attr->lineno : 0,
925 "non-numeric value for numeric attribute %s",
926 attr ? attr->name : "internal");
932 for (av = attr->first_value; av; av = av->next)
933 if (GET_CODE (av->value) == CONST_STRING
934 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
938 error_with_line (attr->lineno,
939 "unknown value `%s' for `%s' attribute",
940 XSTR (exp, 0), attr ? attr->name : "internal");
944 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
945 attr ? attr->is_const : 0,
946 attr ? attr->lineno : 0);
947 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
948 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
956 if (attr && !attr->is_numeric)
958 error_with_line (attr->lineno,
959 "invalid operation `%s' for non-numeric"
960 " attribute value", GET_RTX_NAME (GET_CODE (exp)));
967 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
968 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
977 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
981 if (XVECLEN (exp, 0) % 2 != 0)
983 error_with_line (attr->lineno,
984 "first operand of COND must have even length");
988 for (i = 0; i < XVECLEN (exp, 0); i += 2)
990 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
991 attr ? attr->is_const : 0,
992 attr ? attr->lineno : 0);
993 XVECEXP (exp, 0, i + 1)
994 = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
997 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1002 struct attr_desc *attr2 = find_attr (&XSTR (exp, 0), 0);
1004 error_with_line (attr ? attr->lineno : 0,
1005 "unknown attribute `%s' in ATTR",
1007 else if (attr && attr->is_const && ! attr2->is_const)
1008 error_with_line (attr->lineno,
1009 "non-constant attribute `%s' referenced from `%s'",
1010 XSTR (exp, 0), attr->name);
1012 && attr->is_numeric != attr2->is_numeric)
1013 error_with_line (attr->lineno,
1014 "numeric attribute mismatch calling `%s' from `%s'",
1015 XSTR (exp, 0), attr->name);
1020 /* A constant SYMBOL_REF is valid as a constant attribute test and
1021 is expanded later by make_canonical into a COND. In a non-constant
1022 attribute test, it is left be. */
1023 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1026 error_with_line (attr ? attr->lineno : 0,
1027 "invalid operation `%s' for attribute value",
1028 GET_RTX_NAME (GET_CODE (exp)));
1035 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1036 It becomes a COND with each test being (eq_attr "alternative" "n") */
1039 convert_set_attr_alternative (rtx exp, struct insn_def *id)
1041 int num_alt = id->num_alternatives;
1045 if (XVECLEN (exp, 1) != num_alt)
1047 error_with_line (id->lineno,
1048 "bad number of entries in SET_ATTR_ALTERNATIVE");
1052 /* Make a COND with all tests but the last. Select the last value via the
1054 condexp = rtx_alloc (COND);
1055 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1057 for (i = 0; i < num_alt - 1; i++)
1060 p = attr_numeral (i);
1062 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1063 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1066 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1068 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1071 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1072 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1075 convert_set_attr (rtx exp, struct insn_def *id)
1078 const char *name_ptr;
1082 /* See how many alternative specified. */
1083 n = n_comma_elts (XSTR (exp, 1));
1085 return attr_rtx (SET,
1086 attr_rtx (ATTR, XSTR (exp, 0)),
1087 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1089 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1090 XSTR (newexp, 0) = XSTR (exp, 0);
1091 XVEC (newexp, 1) = rtvec_alloc (n);
1093 /* Process each comma-separated name. */
1094 name_ptr = XSTR (exp, 1);
1096 while ((p = next_comma_elt (&name_ptr)) != NULL)
1097 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1099 return convert_set_attr_alternative (newexp, id);
1102 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1103 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1109 struct insn_def *id;
1110 struct attr_desc *attr;
1114 for (id = defs; id; id = id->next)
1116 if (XVEC (id->def, id->vec_idx) == NULL)
1119 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1121 value = XVECEXP (id->def, id->vec_idx, i);
1122 switch (GET_CODE (value))
1125 if (GET_CODE (XEXP (value, 0)) != ATTR)
1127 error_with_line (id->lineno, "bad attribute set");
1132 case SET_ATTR_ALTERNATIVE:
1133 value = convert_set_attr_alternative (value, id);
1137 value = convert_set_attr (value, id);
1141 error_with_line (id->lineno, "invalid attribute code %s",
1142 GET_RTX_NAME (GET_CODE (value)));
1145 if (value == NULL_RTX)
1148 if ((attr = find_attr (&XSTR (XEXP (value, 0), 0), 0)) == NULL)
1150 error_with_line (id->lineno, "unknown attribute %s",
1151 XSTR (XEXP (value, 0), 0));
1155 XVECEXP (id->def, id->vec_idx, i) = value;
1156 XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1161 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1162 expressions by converting them into a COND. This removes cases from this
1163 program. Also, replace an attribute value of "*" with the default attribute
1167 make_canonical (struct attr_desc *attr, rtx exp)
1172 switch (GET_CODE (exp))
1175 exp = make_numeric_value (INTVAL (exp));
1179 if (! strcmp (XSTR (exp, 0), "*"))
1181 if (attr == 0 || attr->default_val == 0)
1182 fatal ("(attr_value \"*\") used in invalid context");
1183 exp = attr->default_val->value;
1186 XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1191 if (!attr->is_const || ATTR_IND_SIMPLIFIED_P (exp))
1193 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1194 This makes the COND something that won't be considered an arbitrary
1195 expression by walk_attr_value. */
1196 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1197 exp = check_attr_value (exp, attr);
1201 newexp = rtx_alloc (COND);
1202 XVEC (newexp, 0) = rtvec_alloc (2);
1203 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1204 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1206 XEXP (newexp, 1) = XEXP (exp, 2);
1209 /* Fall through to COND case since this is now a COND. */
1216 /* First, check for degenerate COND. */
1217 if (XVECLEN (exp, 0) == 0)
1218 return make_canonical (attr, XEXP (exp, 1));
1219 defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1221 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1223 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1224 XVECEXP (exp, 0, i + 1)
1225 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1226 if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1242 copy_boolean (rtx exp)
1244 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1245 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1246 copy_boolean (XEXP (exp, 1)));
1247 if (GET_CODE (exp) == MATCH_OPERAND)
1249 XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1250 XSTR (exp, 2) = DEF_ATTR_STRING (XSTR (exp, 2));
1252 else if (GET_CODE (exp) == EQ_ATTR)
1254 XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1255 XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1261 /* Given a value and an attribute description, return a `struct attr_value *'
1262 that represents that value. This is either an existing structure, if the
1263 value has been previously encountered, or a newly-created structure.
1265 `insn_code' is the code of an insn whose attribute has the specified
1266 value (-2 if not processing an insn). We ensure that all insns for
1267 a given value have the same number of alternatives if the value checks
1270 static struct attr_value *
1271 get_attr_value (rtx value, struct attr_desc *attr, int insn_code)
1273 struct attr_value *av;
1276 value = make_canonical (attr, value);
1277 if (compares_alternatives_p (value))
1279 if (insn_code < 0 || insn_alternatives == NULL)
1280 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1282 num_alt = insn_alternatives[insn_code];
1285 for (av = attr->first_value; av; av = av->next)
1286 if (rtx_equal_p (value, av->value)
1287 && (num_alt == 0 || av->first_insn == NULL
1288 || insn_alternatives[av->first_insn->def->insn_code]))
1291 av = oballoc (struct attr_value);
1293 av->next = attr->first_value;
1294 attr->first_value = av;
1295 av->first_insn = NULL;
1297 av->has_asm_insn = 0;
1302 /* After all DEFINE_DELAYs have been read in, create internal attributes
1303 to generate the required routines.
1305 First, we compute the number of delay slots for each insn (as a COND of
1306 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1307 delay type is specified, we compute a similar function giving the
1308 DEFINE_DELAY ordinal for each insn.
1310 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1311 tells whether a given insn can be in that delay slot.
1313 Normal attribute filling and optimization expands these to contain the
1314 information needed to handle delay slots. */
1317 expand_delays (void)
1319 struct delay_desc *delay;
1325 /* First, generate data for `num_delay_slots' function. */
1327 condexp = rtx_alloc (COND);
1328 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1329 XEXP (condexp, 1) = make_numeric_value (0);
1331 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1333 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1334 XVECEXP (condexp, 0, i + 1)
1335 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1338 make_internal_attr (num_delay_slots_str, condexp, ATTR_NONE);
1340 /* If more than one delay type, do the same for computing the delay type. */
1343 condexp = rtx_alloc (COND);
1344 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1345 XEXP (condexp, 1) = make_numeric_value (0);
1347 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1349 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1350 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1353 make_internal_attr (delay_type_str, condexp, ATTR_SPECIAL);
1356 /* For each delay possibility and delay slot, compute an eligibility
1357 attribute for non-annulled insns and for each type of annulled (annul
1358 if true and annul if false). */
1359 for (delay = delays; delay; delay = delay->next)
1361 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1363 condexp = XVECEXP (delay->def, 1, i);
1365 condexp = false_rtx;
1366 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1367 make_numeric_value (1), make_numeric_value (0));
1369 p = attr_printf (sizeof "*delay__" + MAX_DIGITS * 2,
1370 "*delay_%d_%d", delay->num, i / 3);
1371 make_internal_attr (p, newexp, ATTR_SPECIAL);
1373 if (have_annul_true)
1375 condexp = XVECEXP (delay->def, 1, i + 1);
1376 if (condexp == 0) condexp = false_rtx;
1377 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1378 make_numeric_value (1),
1379 make_numeric_value (0));
1380 p = attr_printf (sizeof "*annul_true__" + MAX_DIGITS * 2,
1381 "*annul_true_%d_%d", delay->num, i / 3);
1382 make_internal_attr (p, newexp, ATTR_SPECIAL);
1385 if (have_annul_false)
1387 condexp = XVECEXP (delay->def, 1, i + 2);
1388 if (condexp == 0) condexp = false_rtx;
1389 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1390 make_numeric_value (1),
1391 make_numeric_value (0));
1392 p = attr_printf (sizeof "*annul_false__" + MAX_DIGITS * 2,
1393 "*annul_false_%d_%d", delay->num, i / 3);
1394 make_internal_attr (p, newexp, ATTR_SPECIAL);
1400 /* Once all attributes and insns have been read and checked, we construct for
1401 each attribute value a list of all the insns that have that value for
1405 fill_attr (struct attr_desc *attr)
1407 struct attr_value *av;
1408 struct insn_ent *ie;
1409 struct insn_def *id;
1413 /* Don't fill constant attributes. The value is independent of
1414 any particular insn. */
1418 for (id = defs; id; id = id->next)
1420 /* If no value is specified for this insn for this attribute, use the
1423 if (XVEC (id->def, id->vec_idx))
1424 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1425 if (! strcmp_check (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
1427 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
1430 av = attr->default_val;
1432 av = get_attr_value (value, attr, id->insn_code);
1434 ie = oballoc (struct insn_ent);
1436 insert_insn_ent (av, ie);
1440 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
1441 test that checks relative positions of insns (uses MATCH_DUP or PC).
1442 If so, replace it with what is obtained by passing the expression to
1443 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
1444 recursively on each value (including the default value). Otherwise,
1445 return the value returned by NO_ADDRESS_FN applied to EXP. */
1448 substitute_address (rtx exp, rtx (*no_address_fn) (rtx),
1449 rtx (*address_fn) (rtx))
1454 if (GET_CODE (exp) == COND)
1456 /* See if any tests use addresses. */
1458 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1459 walk_attr_value (XVECEXP (exp, 0, i));
1462 return (*address_fn) (exp);
1464 /* Make a new copy of this COND, replacing each element. */
1465 newexp = rtx_alloc (COND);
1466 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
1467 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1469 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
1470 XVECEXP (newexp, 0, i + 1)
1471 = substitute_address (XVECEXP (exp, 0, i + 1),
1472 no_address_fn, address_fn);
1475 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
1476 no_address_fn, address_fn);
1481 else if (GET_CODE (exp) == IF_THEN_ELSE)
1484 walk_attr_value (XEXP (exp, 0));
1486 return (*address_fn) (exp);
1488 return attr_rtx (IF_THEN_ELSE,
1489 substitute_address (XEXP (exp, 0),
1490 no_address_fn, address_fn),
1491 substitute_address (XEXP (exp, 1),
1492 no_address_fn, address_fn),
1493 substitute_address (XEXP (exp, 2),
1494 no_address_fn, address_fn));
1497 return (*no_address_fn) (exp);
1500 /* Make new attributes from the `length' attribute. The following are made,
1501 each corresponding to a function called from `shorten_branches' or
1504 *insn_default_length This is the length of the insn to be returned
1505 by `get_attr_length' before `shorten_branches'
1506 has been called. In each case where the length
1507 depends on relative addresses, the largest
1508 possible is used. This routine is also used
1509 to compute the initial size of the insn.
1511 *insn_variable_length_p This returns 1 if the insn's length depends
1512 on relative addresses, zero otherwise.
1514 *insn_current_length This is only called when it is known that the
1515 insn has a variable length and returns the
1516 current length, based on relative addresses.
1520 make_length_attrs (void)
1522 static const char *new_names[] =
1524 "*insn_default_length",
1526 "*insn_variable_length_p",
1527 "*insn_current_length"
1529 static rtx (*const no_address_fn[]) (rtx)
1530 = {identity_fn,identity_fn, zero_fn, zero_fn};
1531 static rtx (*const address_fn[]) (rtx)
1532 = {max_fn, min_fn, one_fn, identity_fn};
1534 struct attr_desc *length_attr, *new_attr;
1535 struct attr_value *av, *new_av;
1536 struct insn_ent *ie, *new_ie;
1538 /* See if length attribute is defined. If so, it must be numeric. Make
1539 it special so we don't output anything for it. */
1540 length_attr = find_attr (&length_str, 0);
1541 if (length_attr == 0)
1544 if (! length_attr->is_numeric)
1545 fatal ("length attribute must be numeric");
1547 length_attr->is_const = 0;
1548 length_attr->is_special = 1;
1550 /* Make each new attribute, in turn. */
1551 for (i = 0; i < ARRAY_SIZE (new_names); i++)
1553 make_internal_attr (new_names[i],
1554 substitute_address (length_attr->default_val->value,
1555 no_address_fn[i], address_fn[i]),
1557 new_attr = find_attr (&new_names[i], 0);
1558 for (av = length_attr->first_value; av; av = av->next)
1559 for (ie = av->first_insn; ie; ie = ie->next)
1561 new_av = get_attr_value (substitute_address (av->value,
1564 new_attr, ie->def->insn_code);
1565 new_ie = oballoc (struct insn_ent);
1566 new_ie->def = ie->def;
1567 insert_insn_ent (new_av, new_ie);
1572 /* Utility functions called from above routine. */
1575 identity_fn (rtx exp)
1581 zero_fn (rtx exp ATTRIBUTE_UNUSED)
1583 return make_numeric_value (0);
1587 one_fn (rtx exp ATTRIBUTE_UNUSED)
1589 return make_numeric_value (1);
1596 return make_numeric_value (max_attr_value (exp, &unknown));
1603 return make_numeric_value (min_attr_value (exp, &unknown));
1607 write_length_unit_log (void)
1609 struct attr_desc *length_attr = find_attr (&length_str, 0);
1610 struct attr_value *av;
1611 struct insn_ent *ie;
1612 unsigned int length_unit_log, length_or;
1615 if (length_attr == 0)
1617 length_or = or_attr_value (length_attr->default_val->value, &unknown);
1618 for (av = length_attr->first_value; av; av = av->next)
1619 for (ie = av->first_insn; ie; ie = ie->next)
1620 length_or |= or_attr_value (av->value, &unknown);
1623 length_unit_log = 0;
1626 length_or = ~length_or;
1627 for (length_unit_log = 0; length_or & 1; length_or >>= 1)
1630 printf ("EXPORTED_CONST int length_unit_log = %u;\n", length_unit_log);
1633 /* Take a COND expression and see if any of the conditions in it can be
1634 simplified. If any are known true or known false for the particular insn
1635 code, the COND can be further simplified.
1637 Also call ourselves on any COND operations that are values of this COND.
1639 We do not modify EXP; rather, we make and return a new rtx. */
1642 simplify_cond (rtx exp, int insn_code, int insn_index)
1645 /* We store the desired contents here,
1646 then build a new expression if they don't match EXP. */
1647 rtx defval = XEXP (exp, 1);
1648 rtx new_defval = XEXP (exp, 1);
1649 int len = XVECLEN (exp, 0);
1650 rtx *tests = XNEWVEC (rtx, len);
1654 /* This lets us free all storage allocated below, if appropriate. */
1655 obstack_finish (rtl_obstack);
1657 memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
1659 /* See if default value needs simplification. */
1660 if (GET_CODE (defval) == COND)
1661 new_defval = simplify_cond (defval, insn_code, insn_index);
1663 /* Simplify the subexpressions, and see what tests we can get rid of. */
1665 for (i = 0; i < len; i += 2)
1667 rtx newtest, newval;
1669 /* Simplify this test. */
1670 newtest = simplify_test_exp_in_temp (tests[i], insn_code, insn_index);
1673 newval = tests[i + 1];
1674 /* See if this value may need simplification. */
1675 if (GET_CODE (newval) == COND)
1676 newval = simplify_cond (newval, insn_code, insn_index);
1678 /* Look for ways to delete or combine this test. */
1679 if (newtest == true_rtx)
1681 /* If test is true, make this value the default
1682 and discard this + any following tests. */
1684 defval = tests[i + 1];
1685 new_defval = newval;
1688 else if (newtest == false_rtx)
1690 /* If test is false, discard it and its value. */
1691 for (j = i; j < len - 2; j++)
1692 tests[j] = tests[j + 2];
1697 else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
1699 /* If this value and the value for the prev test are the same,
1703 = insert_right_side (IOR, tests[i - 2], newtest,
1704 insn_code, insn_index);
1706 /* Delete this test/value. */
1707 for (j = i; j < len - 2; j++)
1708 tests[j] = tests[j + 2];
1714 tests[i + 1] = newval;
1717 /* If the last test in a COND has the same value
1718 as the default value, that test isn't needed. */
1720 while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
1723 /* See if we changed anything. */
1724 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
1727 for (i = 0; i < len; i++)
1728 if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
1736 if (GET_CODE (defval) == COND)
1737 ret = simplify_cond (defval, insn_code, insn_index);
1745 rtx newexp = rtx_alloc (COND);
1747 XVEC (newexp, 0) = rtvec_alloc (len);
1748 memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
1749 XEXP (newexp, 1) = new_defval;
1756 /* Remove an insn entry from an attribute value. */
1759 remove_insn_ent (struct attr_value *av, struct insn_ent *ie)
1761 struct insn_ent *previe;
1763 if (av->first_insn == ie)
1764 av->first_insn = ie->next;
1767 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
1769 previe->next = ie->next;
1773 if (ie->def->insn_code == -1)
1774 av->has_asm_insn = 0;
1779 /* Insert an insn entry in an attribute value list. */
1782 insert_insn_ent (struct attr_value *av, struct insn_ent *ie)
1784 ie->next = av->first_insn;
1785 av->first_insn = ie;
1787 if (ie->def->insn_code == -1)
1788 av->has_asm_insn = 1;
1793 /* This is a utility routine to take an expression that is a tree of either
1794 AND or IOR expressions and insert a new term. The new term will be
1795 inserted at the right side of the first node whose code does not match
1796 the root. A new node will be created with the root's code. Its left
1797 side will be the old right side and its right side will be the new
1800 If the `term' is itself a tree, all its leaves will be inserted. */
1803 insert_right_side (enum rtx_code code, rtx exp, rtx term, int insn_code, int insn_index)
1807 /* Avoid consing in some special cases. */
1808 if (code == AND && term == true_rtx)
1810 if (code == AND && term == false_rtx)
1812 if (code == AND && exp == true_rtx)
1814 if (code == AND && exp == false_rtx)
1816 if (code == IOR && term == true_rtx)
1818 if (code == IOR && term == false_rtx)
1820 if (code == IOR && exp == true_rtx)
1822 if (code == IOR && exp == false_rtx)
1824 if (attr_equal_p (exp, term))
1827 if (GET_CODE (term) == code)
1829 exp = insert_right_side (code, exp, XEXP (term, 0),
1830 insn_code, insn_index);
1831 exp = insert_right_side (code, exp, XEXP (term, 1),
1832 insn_code, insn_index);
1837 if (GET_CODE (exp) == code)
1839 rtx new_rtx = insert_right_side (code, XEXP (exp, 1),
1840 term, insn_code, insn_index);
1841 if (new_rtx != XEXP (exp, 1))
1842 /* Make a copy of this expression and call recursively. */
1843 newexp = attr_rtx (code, XEXP (exp, 0), new_rtx);
1849 /* Insert the new term. */
1850 newexp = attr_rtx (code, exp, term);
1853 return simplify_test_exp_in_temp (newexp, insn_code, insn_index);
1856 /* If we have an expression which AND's a bunch of
1857 (not (eq_attrq "alternative" "n"))
1858 terms, we may have covered all or all but one of the possible alternatives.
1859 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
1861 This routine is passed an expression and either AND or IOR. It returns a
1862 bitmask indicating which alternatives are mentioned within EXP. */
1865 compute_alternative_mask (rtx exp, enum rtx_code code)
1868 if (GET_CODE (exp) == code)
1869 return compute_alternative_mask (XEXP (exp, 0), code)
1870 | compute_alternative_mask (XEXP (exp, 1), code);
1872 else if (code == AND && GET_CODE (exp) == NOT
1873 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
1874 && XSTR (XEXP (exp, 0), 0) == alternative_name)
1875 string = XSTR (XEXP (exp, 0), 1);
1877 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
1878 && XSTR (exp, 0) == alternative_name)
1879 string = XSTR (exp, 1);
1881 else if (GET_CODE (exp) == EQ_ATTR_ALT)
1883 if (code == AND && XINT (exp, 1))
1884 return XINT (exp, 0);
1886 if (code == IOR && !XINT (exp, 1))
1887 return XINT (exp, 0);
1895 return 1 << (string[0] - '0');
1896 return 1 << atoi (string);
1899 /* Given I, a single-bit mask, return RTX to compare the `alternative'
1900 attribute with the value represented by that bit. */
1903 make_alternative_compare (int mask)
1905 return mk_attr_alt (mask);
1908 /* If we are processing an (eq_attr "attr" "value") test, we find the value
1909 of "attr" for this insn code. From that value, we can compute a test
1910 showing when the EQ_ATTR will be true. This routine performs that
1911 computation. If a test condition involves an address, we leave the EQ_ATTR
1912 intact because addresses are only valid for the `length' attribute.
1914 EXP is the EQ_ATTR expression and ATTR is the attribute to which
1915 it refers. VALUE is the value of that attribute for the insn
1916 corresponding to INSN_CODE and INSN_INDEX. */
1919 evaluate_eq_attr (rtx exp, struct attr_desc *attr, rtx value,
1920 int insn_code, int insn_index)
1927 while (GET_CODE (value) == ATTR)
1929 struct attr_value *av = NULL;
1931 attr = find_attr (&XSTR (value, 0), 0);
1933 if (insn_code_values)
1935 struct attr_value_list *iv;
1936 for (iv = insn_code_values[insn_code]; iv; iv = iv->next)
1937 if (iv->attr == attr)
1945 struct insn_ent *ie;
1946 for (av = attr->first_value; av; av = av->next)
1947 for (ie = av->first_insn; ie; ie = ie->next)
1948 if (ie->def->insn_code == insn_code)
1958 switch (GET_CODE (value))
1961 if (! strcmp_check (XSTR (value, 0), XSTR (exp, 1)))
1972 gcc_assert (GET_CODE (exp) == EQ_ATTR);
1973 prefix = attr->enum_name ? attr->enum_name : attr->name;
1974 string = ACONCAT ((prefix, "_", XSTR (exp, 1), NULL));
1975 for (p = string; *p; p++)
1978 newexp = attr_rtx (EQ, value,
1979 attr_rtx (SYMBOL_REF,
1980 DEF_ATTR_STRING (string)));
1985 /* We construct an IOR of all the cases for which the
1986 requested attribute value is present. Since we start with
1987 FALSE, if it is not present, FALSE will be returned.
1989 Each case is the AND of the NOT's of the previous conditions with the
1990 current condition; in the default case the current condition is TRUE.
1992 For each possible COND value, call ourselves recursively.
1994 The extra TRUE and FALSE expressions will be eliminated by another
1995 call to the simplification routine. */
2000 for (i = 0; i < XVECLEN (value, 0); i += 2)
2002 rtx this_cond = simplify_test_exp_in_temp (XVECEXP (value, 0, i),
2003 insn_code, insn_index);
2005 right = insert_right_side (AND, andexp, this_cond,
2006 insn_code, insn_index);
2007 right = insert_right_side (AND, right,
2008 evaluate_eq_attr (exp, attr,
2011 insn_code, insn_index),
2012 insn_code, insn_index);
2013 orexp = insert_right_side (IOR, orexp, right,
2014 insn_code, insn_index);
2016 /* Add this condition into the AND expression. */
2017 newexp = attr_rtx (NOT, this_cond);
2018 andexp = insert_right_side (AND, andexp, newexp,
2019 insn_code, insn_index);
2022 /* Handle the default case. */
2023 right = insert_right_side (AND, andexp,
2024 evaluate_eq_attr (exp, attr, XEXP (value, 1),
2025 insn_code, insn_index),
2026 insn_code, insn_index);
2027 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2034 /* If uses an address, must return original expression. But set the
2035 ATTR_IND_SIMPLIFIED_P bit so we don't try to simplify it again. */
2038 walk_attr_value (newexp);
2042 if (! ATTR_IND_SIMPLIFIED_P (exp))
2043 return copy_rtx_unchanging (exp);
2050 /* This routine is called when an AND of a term with a tree of AND's is
2051 encountered. If the term or its complement is present in the tree, it
2052 can be replaced with TRUE or FALSE, respectively.
2054 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2055 be true and hence are complementary.
2057 There is one special case: If we see
2058 (and (not (eq_attr "att" "v1"))
2059 (eq_attr "att" "v2"))
2060 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2061 replace the term, not anything in the AND tree. So we pass a pointer to
2065 simplify_and_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2070 int left_eliminates_term, right_eliminates_term;
2072 if (GET_CODE (exp) == AND)
2074 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2075 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2076 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2078 newexp = attr_rtx (AND, left, right);
2080 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2084 else if (GET_CODE (exp) == IOR)
2086 /* For the IOR case, we do the same as above, except that we can
2087 only eliminate `term' if both sides of the IOR would do so. */
2089 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2090 left_eliminates_term = (temp == true_rtx);
2093 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2094 right_eliminates_term = (temp == true_rtx);
2096 if (left_eliminates_term && right_eliminates_term)
2099 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2101 newexp = attr_rtx (IOR, left, right);
2103 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2107 /* Check for simplifications. Do some extra checking here since this
2108 routine is called so many times. */
2113 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2116 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2119 else if (GET_CODE (exp) == EQ_ATTR_ALT && GET_CODE (*pterm) == EQ_ATTR_ALT)
2121 if (attr_alt_subset_p (*pterm, exp))
2124 if (attr_alt_subset_of_compl_p (*pterm, exp))
2127 if (attr_alt_subset_p (exp, *pterm))
2133 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2135 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2138 if (! strcmp_check (XSTR (exp, 1), XSTR (*pterm, 1)))
2144 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2145 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2147 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2150 if (! strcmp_check (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2156 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2157 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2159 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2162 if (! strcmp_check (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2168 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2170 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2174 else if (GET_CODE (exp) == NOT)
2176 if (attr_equal_p (XEXP (exp, 0), *pterm))
2180 else if (GET_CODE (*pterm) == NOT)
2182 if (attr_equal_p (XEXP (*pterm, 0), exp))
2186 else if (attr_equal_p (exp, *pterm))
2192 /* Similar to `simplify_and_tree', but for IOR trees. */
2195 simplify_or_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2200 int left_eliminates_term, right_eliminates_term;
2202 if (GET_CODE (exp) == IOR)
2204 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2205 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2206 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2208 newexp = attr_rtx (GET_CODE (exp), left, right);
2210 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2214 else if (GET_CODE (exp) == AND)
2216 /* For the AND case, we do the same as above, except that we can
2217 only eliminate `term' if both sides of the AND would do so. */
2219 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2220 left_eliminates_term = (temp == false_rtx);
2223 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2224 right_eliminates_term = (temp == false_rtx);
2226 if (left_eliminates_term && right_eliminates_term)
2229 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2231 newexp = attr_rtx (GET_CODE (exp), left, right);
2233 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2237 if (attr_equal_p (exp, *pterm))
2240 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
2243 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
2246 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2247 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2248 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
2251 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2252 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
2253 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
2259 /* Compute approximate cost of the expression. Used to decide whether
2260 expression is cheap enough for inline. */
2262 attr_rtx_cost (rtx x)
2268 code = GET_CODE (x);
2281 /* Alternatives don't result into function call. */
2282 if (!strcmp_check (XSTR (x, 0), alternative_name))
2289 const char *fmt = GET_RTX_FORMAT (code);
2290 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2296 for (j = 0; j < XVECLEN (x, i); j++)
2297 cost += attr_rtx_cost (XVECEXP (x, i, j));
2300 cost += attr_rtx_cost (XEXP (x, i));
2310 /* Simplify test expression and use temporary obstack in order to avoid
2311 memory bloat. Use ATTR_IND_SIMPLIFIED to avoid unnecessary simplifications
2312 and avoid unnecessary copying if possible. */
2315 simplify_test_exp_in_temp (rtx exp, int insn_code, int insn_index)
2318 struct obstack *old;
2319 if (ATTR_IND_SIMPLIFIED_P (exp))
2322 rtl_obstack = temp_obstack;
2323 x = simplify_test_exp (exp, insn_code, insn_index);
2325 if (x == exp || rtl_obstack == temp_obstack)
2327 return attr_copy_rtx (x);
2330 /* Returns true if S1 is a subset of S2. */
2333 attr_alt_subset_p (rtx s1, rtx s2)
2335 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2338 return !(XINT (s1, 0) &~ XINT (s2, 0));
2341 return !(XINT (s1, 0) & XINT (s2, 0));
2347 return !(XINT (s2, 0) &~ XINT (s1, 0));
2354 /* Returns true if S1 is a subset of complement of S2. */
2357 attr_alt_subset_of_compl_p (rtx s1, rtx s2)
2359 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2362 return !(XINT (s1, 0) & XINT (s2, 0));
2365 return !(XINT (s1, 0) & ~XINT (s2, 0));
2368 return !(XINT (s2, 0) &~ XINT (s1, 0));
2378 /* Return EQ_ATTR_ALT expression representing intersection of S1 and S2. */
2381 attr_alt_intersection (rtx s1, rtx s2)
2383 rtx result = rtx_alloc (EQ_ATTR_ALT);
2385 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2388 XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
2391 XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
2394 XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
2397 XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
2402 XINT (result, 1) = XINT (s1, 1) & XINT (s2, 1);
2407 /* Return EQ_ATTR_ALT expression representing union of S1 and S2. */
2410 attr_alt_union (rtx s1, rtx s2)
2412 rtx result = rtx_alloc (EQ_ATTR_ALT);
2414 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2417 XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
2420 XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
2423 XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
2426 XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
2432 XINT (result, 1) = XINT (s1, 1) | XINT (s2, 1);
2436 /* Return EQ_ATTR_ALT expression representing complement of S. */
2439 attr_alt_complement (rtx s)
2441 rtx result = rtx_alloc (EQ_ATTR_ALT);
2443 XINT (result, 0) = XINT (s, 0);
2444 XINT (result, 1) = 1 - XINT (s, 1);
2449 /* Return EQ_ATTR_ALT expression representing set containing elements set
2455 rtx result = rtx_alloc (EQ_ATTR_ALT);
2457 XINT (result, 0) = e;
2458 XINT (result, 1) = 0;
2463 /* Given an expression, see if it can be simplified for a particular insn
2464 code based on the values of other attributes being tested. This can
2465 eliminate nested get_attr_... calls.
2467 Note that if an endless recursion is specified in the patterns, the
2468 optimization will loop. However, it will do so in precisely the cases where
2469 an infinite recursion loop could occur during compilation. It's better that
2473 simplify_test_exp (rtx exp, int insn_code, int insn_index)
2476 struct attr_desc *attr;
2477 struct attr_value *av;
2478 struct insn_ent *ie;
2479 struct attr_value_list *iv;
2482 bool left_alt, right_alt;
2484 /* Don't re-simplify something we already simplified. */
2485 if (ATTR_IND_SIMPLIFIED_P (exp) || ATTR_CURR_SIMPLIFIED_P (exp))
2488 switch (GET_CODE (exp))
2491 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2492 if (left == false_rtx)
2494 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2495 if (right == false_rtx)
2498 if (GET_CODE (left) == EQ_ATTR_ALT
2499 && GET_CODE (right) == EQ_ATTR_ALT)
2501 exp = attr_alt_intersection (left, right);
2502 return simplify_test_exp (exp, insn_code, insn_index);
2505 /* If either side is an IOR and we have (eq_attr "alternative" ..")
2506 present on both sides, apply the distributive law since this will
2507 yield simplifications. */
2508 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
2509 && compute_alternative_mask (left, IOR)
2510 && compute_alternative_mask (right, IOR))
2512 if (GET_CODE (left) == IOR)
2519 newexp = attr_rtx (IOR,
2520 attr_rtx (AND, left, XEXP (right, 0)),
2521 attr_rtx (AND, left, XEXP (right, 1)));
2523 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2526 /* Try with the term on both sides. */
2527 right = simplify_and_tree (right, &left, insn_code, insn_index);
2528 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2529 left = simplify_and_tree (left, &right, insn_code, insn_index);
2531 if (left == false_rtx || right == false_rtx)
2533 else if (left == true_rtx)
2537 else if (right == true_rtx)
2541 /* See if all or all but one of the insn's alternatives are specified
2542 in this tree. Optimize if so. */
2544 if (GET_CODE (left) == NOT)
2545 left_alt = (GET_CODE (XEXP (left, 0)) == EQ_ATTR
2546 && XSTR (XEXP (left, 0), 0) == alternative_name);
2548 left_alt = (GET_CODE (left) == EQ_ATTR_ALT
2551 if (GET_CODE (right) == NOT)
2552 right_alt = (GET_CODE (XEXP (right, 0)) == EQ_ATTR
2553 && XSTR (XEXP (right, 0), 0) == alternative_name);
2555 right_alt = (GET_CODE (right) == EQ_ATTR_ALT
2556 && XINT (right, 1));
2559 && (GET_CODE (left) == AND
2561 || GET_CODE (right) == AND
2564 i = compute_alternative_mask (exp, AND);
2565 if (i & ~insn_alternatives[insn_code])
2566 fatal ("invalid alternative specified for pattern number %d",
2569 /* If all alternatives are excluded, this is false. */
2570 i ^= insn_alternatives[insn_code];
2573 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2575 /* If just one excluded, AND a comparison with that one to the
2576 front of the tree. The others will be eliminated by
2577 optimization. We do not want to do this if the insn has one
2578 alternative and we have tested none of them! */
2579 left = make_alternative_compare (i);
2580 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2581 newexp = attr_rtx (AND, left, right);
2583 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2587 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2589 newexp = attr_rtx (AND, left, right);
2590 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2595 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2596 if (left == true_rtx)
2598 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2599 if (right == true_rtx)
2602 if (GET_CODE (left) == EQ_ATTR_ALT
2603 && GET_CODE (right) == EQ_ATTR_ALT)
2605 exp = attr_alt_union (left, right);
2606 return simplify_test_exp (exp, insn_code, insn_index);
2609 right = simplify_or_tree (right, &left, insn_code, insn_index);
2610 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2611 left = simplify_or_tree (left, &right, insn_code, insn_index);
2613 if (right == true_rtx || left == true_rtx)
2615 else if (left == false_rtx)
2619 else if (right == false_rtx)
2624 /* Test for simple cases where the distributive law is useful. I.e.,
2625 convert (ior (and (x) (y))
2631 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
2632 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
2634 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
2636 left = XEXP (left, 0);
2638 newexp = attr_rtx (AND, left, right);
2639 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2642 /* See if all or all but one of the insn's alternatives are specified
2643 in this tree. Optimize if so. */
2645 else if (insn_code >= 0
2646 && (GET_CODE (left) == IOR
2647 || (GET_CODE (left) == EQ_ATTR_ALT
2649 || (GET_CODE (left) == EQ_ATTR
2650 && XSTR (left, 0) == alternative_name)
2651 || GET_CODE (right) == IOR
2652 || (GET_CODE (right) == EQ_ATTR_ALT
2653 && !XINT (right, 1))
2654 || (GET_CODE (right) == EQ_ATTR
2655 && XSTR (right, 0) == alternative_name)))
2657 i = compute_alternative_mask (exp, IOR);
2658 if (i & ~insn_alternatives[insn_code])
2659 fatal ("invalid alternative specified for pattern number %d",
2662 /* If all alternatives are included, this is true. */
2663 i ^= insn_alternatives[insn_code];
2666 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2668 /* If just one excluded, IOR a comparison with that one to the
2669 front of the tree. The others will be eliminated by
2670 optimization. We do not want to do this if the insn has one
2671 alternative and we have tested none of them! */
2672 left = make_alternative_compare (i);
2673 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2674 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
2676 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2680 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2682 newexp = attr_rtx (IOR, left, right);
2683 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2688 if (GET_CODE (XEXP (exp, 0)) == NOT)
2690 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
2691 insn_code, insn_index);
2695 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2696 if (GET_CODE (left) == NOT)
2697 return XEXP (left, 0);
2699 if (left == false_rtx)
2701 if (left == true_rtx)
2704 if (GET_CODE (left) == EQ_ATTR_ALT)
2706 exp = attr_alt_complement (left);
2707 return simplify_test_exp (exp, insn_code, insn_index);
2710 /* Try to apply De`Morgan's laws. */
2711 if (GET_CODE (left) == IOR)
2713 newexp = attr_rtx (AND,
2714 attr_rtx (NOT, XEXP (left, 0)),
2715 attr_rtx (NOT, XEXP (left, 1)));
2717 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2719 else if (GET_CODE (left) == AND)
2721 newexp = attr_rtx (IOR,
2722 attr_rtx (NOT, XEXP (left, 0)),
2723 attr_rtx (NOT, XEXP (left, 1)));
2725 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2727 else if (left != XEXP (exp, 0))
2729 newexp = attr_rtx (NOT, left);
2735 return XINT (exp, 1) ? true_rtx : false_rtx;
2739 if (XSTR (exp, 0) == alternative_name)
2741 newexp = mk_attr_alt (1 << atoi (XSTR (exp, 1)));
2745 /* Look at the value for this insn code in the specified attribute.
2746 We normally can replace this comparison with the condition that
2747 would give this insn the values being tested for. */
2749 && (attr = find_attr (&XSTR (exp, 0), 0)) != NULL)
2754 if (insn_code_values)
2756 for (iv = insn_code_values[insn_code]; iv; iv = iv->next)
2757 if (iv->attr == attr)
2765 for (av = attr->first_value; av; av = av->next)
2766 for (ie = av->first_insn; ie; ie = ie->next)
2767 if (ie->def->insn_code == insn_code)
2774 x = evaluate_eq_attr (exp, attr, av->value,
2775 insn_code, insn_index);
2776 x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
2777 if (attr_rtx_cost(x) < 20)
2787 /* We have already simplified this expression. Simplifying it again
2788 won't buy anything unless we weren't given a valid insn code
2789 to process (i.e., we are canonicalizing something.). */
2791 && ! ATTR_IND_SIMPLIFIED_P (newexp))
2792 return copy_rtx_unchanging (newexp);
2797 /* Optimize the attribute lists by seeing if we can determine conditional
2798 values from the known values of other attributes. This will save subroutine
2799 calls during the compilation. */
2802 optimize_attrs (void)
2804 struct attr_desc *attr;
2805 struct attr_value *av;
2806 struct insn_ent *ie;
2809 struct attr_value_list *ivbuf;
2810 struct attr_value_list *iv;
2812 /* For each insn code, make a list of all the insn_ent's for it,
2813 for all values for all attributes. */
2815 if (num_insn_ents == 0)
2818 /* Make 2 extra elements, for "code" values -2 and -1. */
2819 insn_code_values = XCNEWVEC (struct attr_value_list *, insn_code_number + 2);
2821 /* Offset the table address so we can index by -2 or -1. */
2822 insn_code_values += 2;
2824 iv = ivbuf = XNEWVEC (struct attr_value_list, num_insn_ents);
2826 for (i = 0; i < MAX_ATTRS_INDEX; i++)
2827 for (attr = attrs[i]; attr; attr = attr->next)
2828 for (av = attr->first_value; av; av = av->next)
2829 for (ie = av->first_insn; ie; ie = ie->next)
2834 iv->next = insn_code_values[ie->def->insn_code];
2835 insn_code_values[ie->def->insn_code] = iv;
2839 /* Sanity check on num_insn_ents. */
2840 gcc_assert (iv == ivbuf + num_insn_ents);
2842 /* Process one insn code at a time. */
2843 for (i = -2; i < insn_code_number; i++)
2845 /* Clear the ATTR_CURR_SIMPLIFIED_P flag everywhere relevant.
2846 We use it to mean "already simplified for this insn". */
2847 for (iv = insn_code_values[i]; iv; iv = iv->next)
2848 clear_struct_flag (iv->av->value);
2850 for (iv = insn_code_values[i]; iv; iv = iv->next)
2852 struct obstack *old = rtl_obstack;
2857 if (GET_CODE (av->value) != COND)
2860 rtl_obstack = temp_obstack;
2862 while (GET_CODE (newexp) == COND)
2864 rtx newexp2 = simplify_cond (newexp, ie->def->insn_code,
2865 ie->def->insn_index);
2866 if (newexp2 == newexp)
2872 if (newexp != av->value)
2874 newexp = attr_copy_rtx (newexp);
2875 remove_insn_ent (av, ie);
2876 av = get_attr_value (newexp, attr, ie->def->insn_code);
2878 insert_insn_ent (av, ie);
2884 free (insn_code_values - 2);
2885 insn_code_values = NULL;
2888 /* Clear the ATTR_CURR_SIMPLIFIED_P flag in EXP and its subexpressions. */
2891 clear_struct_flag (rtx x)
2898 ATTR_CURR_SIMPLIFIED_P (x) = 0;
2899 if (ATTR_IND_SIMPLIFIED_P (x))
2902 code = GET_CODE (x);
2922 /* Compare the elements. If any pair of corresponding elements
2923 fail to match, return 0 for the whole things. */
2925 fmt = GET_RTX_FORMAT (code);
2926 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2932 for (j = 0; j < XVECLEN (x, i); j++)
2933 clear_struct_flag (XVECEXP (x, i, j));
2937 clear_struct_flag (XEXP (x, i));
2943 /* Add attribute value NAME to the beginning of ATTR's list. */
2946 add_attr_value (struct attr_desc *attr, const char *name)
2948 struct attr_value *av;
2950 av = oballoc (struct attr_value);
2951 av->value = attr_rtx (CONST_STRING, name);
2952 av->next = attr->first_value;
2953 attr->first_value = av;
2954 av->first_insn = NULL;
2956 av->has_asm_insn = 0;
2959 /* Create table entries for DEFINE_ATTR or DEFINE_ENUM_ATTR. */
2962 gen_attr (rtx exp, int lineno)
2964 struct enum_type *et;
2965 struct enum_value *ev;
2966 struct attr_desc *attr;
2967 const char *name_ptr;
2970 /* Make a new attribute structure. Check for duplicate by looking at
2971 attr->default_val, since it is initialized by this routine. */
2972 attr = find_attr (&XSTR (exp, 0), 1);
2973 if (attr->default_val)
2975 error_with_line (lineno, "duplicate definition for attribute %s",
2977 message_with_line (attr->lineno, "previous definition");
2980 attr->lineno = lineno;
2982 if (GET_CODE (exp) == DEFINE_ENUM_ATTR)
2984 attr->enum_name = XSTR (exp, 1);
2985 et = lookup_enum_type (XSTR (exp, 1));
2986 if (!et || !et->md_p)
2987 error_with_line (lineno, "No define_enum called `%s' defined",
2989 for (ev = et->values; ev; ev = ev->next)
2990 add_attr_value (attr, ev->name);
2992 else if (*XSTR (exp, 1) == '\0')
2993 attr->is_numeric = 1;
2996 name_ptr = XSTR (exp, 1);
2997 while ((p = next_comma_elt (&name_ptr)) != NULL)
2998 add_attr_value (attr, p);
3001 if (GET_CODE (XEXP (exp, 2)) == CONST)
3004 if (attr->is_numeric)
3005 error_with_line (lineno,
3006 "constant attributes may not take numeric values");
3008 /* Get rid of the CONST node. It is allowed only at top-level. */
3009 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
3012 if (! strcmp_check (attr->name, length_str) && ! attr->is_numeric)
3013 error_with_line (lineno, "`length' attribute must take numeric values");
3015 /* Set up the default value. */
3016 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
3017 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
3020 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
3021 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
3022 number of alternatives as this should be checked elsewhere. */
3025 count_alternatives (rtx exp)
3030 if (GET_CODE (exp) == MATCH_OPERAND)
3031 return n_comma_elts (XSTR (exp, 2));
3033 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3034 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3039 n = count_alternatives (XEXP (exp, i));
3046 if (XVEC (exp, i) != NULL)
3047 for (j = 0; j < XVECLEN (exp, i); j++)
3049 n = count_alternatives (XVECEXP (exp, i, j));
3058 /* Returns nonzero if the given expression contains an EQ_ATTR with the
3059 `alternative' attribute. */
3062 compares_alternatives_p (rtx exp)
3067 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
3070 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3071 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3076 if (compares_alternatives_p (XEXP (exp, i)))
3081 for (j = 0; j < XVECLEN (exp, i); j++)
3082 if (compares_alternatives_p (XVECEXP (exp, i, j)))
3090 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
3093 gen_insn (rtx exp, int lineno)
3095 struct insn_def *id;
3097 id = oballoc (struct insn_def);
3101 id->lineno = lineno;
3103 switch (GET_CODE (exp))
3106 id->insn_code = insn_code_number;
3107 id->insn_index = insn_index_number;
3108 id->num_alternatives = count_alternatives (exp);
3109 if (id->num_alternatives == 0)
3110 id->num_alternatives = 1;
3114 case DEFINE_PEEPHOLE:
3115 id->insn_code = insn_code_number;
3116 id->insn_index = insn_index_number;
3117 id->num_alternatives = count_alternatives (exp);
3118 if (id->num_alternatives == 0)
3119 id->num_alternatives = 1;
3123 case DEFINE_ASM_ATTRIBUTES:
3125 id->insn_index = -1;
3126 id->num_alternatives = 1;
3128 got_define_asm_attributes = 1;
3136 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
3137 true or annul false is specified, and make a `struct delay_desc'. */
3140 gen_delay (rtx def, int lineno)
3142 struct delay_desc *delay;
3145 if (XVECLEN (def, 1) % 3 != 0)
3147 error_with_line (lineno,
3148 "number of elements in DEFINE_DELAY must"
3149 " be multiple of three");
3153 for (i = 0; i < XVECLEN (def, 1); i += 3)
3155 if (XVECEXP (def, 1, i + 1))
3156 have_annul_true = 1;
3157 if (XVECEXP (def, 1, i + 2))
3158 have_annul_false = 1;
3161 delay = oballoc (struct delay_desc);
3163 delay->num = ++num_delays;
3164 delay->next = delays;
3165 delay->lineno = lineno;
3169 /* Names of attributes that could be possibly cached. */
3170 static const char *cached_attrs[32];
3171 /* Number of such attributes. */
3172 static int cached_attr_count;
3173 /* Bitmasks of possibly cached attributes. */
3174 static unsigned int attrs_seen_once, attrs_seen_more_than_once;
3175 static unsigned int attrs_to_cache;
3176 static unsigned int attrs_cached_inside, attrs_cached_after;
3178 /* Finds non-const attributes that could be possibly cached.
3179 When create is TRUE, fills in cached_attrs array.
3180 Computes ATTRS_SEEN_ONCE and ATTRS_SEEN_MORE_THAN_ONCE
3184 find_attrs_to_cache (rtx exp, bool create)
3188 struct attr_desc *attr;
3193 switch (GET_CODE (exp))
3196 if (GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
3197 find_attrs_to_cache (XEXP (exp, 0), create);
3201 name = XSTR (exp, 0);
3202 if (name == alternative_name)
3204 for (i = 0; i < cached_attr_count; i++)
3205 if (name == cached_attrs[i])
3207 if ((attrs_seen_once & (1U << i)) != 0)
3208 attrs_seen_more_than_once |= (1U << i);
3210 attrs_seen_once |= (1U << i);
3215 attr = find_attr (&name, 0);
3219 if (cached_attr_count == 32)
3221 cached_attrs[cached_attr_count] = XSTR (exp, 0);
3222 attrs_seen_once |= (1U << cached_attr_count);
3223 cached_attr_count++;
3228 find_attrs_to_cache (XEXP (exp, 0), create);
3229 find_attrs_to_cache (XEXP (exp, 1), create);
3233 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3234 find_attrs_to_cache (XVECEXP (exp, 0, i), create);
3242 /* Given a piece of RTX, print a C expression to test its truth value.
3243 We use AND and IOR both for logical and bit-wise operations, so
3244 interpret them as logical unless they are inside a comparison expression. */
3246 /* Interpret AND/IOR as bit-wise operations instead of logical. */
3247 #define FLG_BITWISE 1
3248 /* Set if cached attribute will be known initialized in else block after
3249 this condition. This is true for LHS of toplevel && and || and
3250 even for RHS of ||, but not for RHS of &&. */
3252 /* Set if cached attribute will be known initialized in then block after
3253 this condition. This is true for LHS of toplevel && and || and
3254 even for RHS of &&, but not for RHS of ||. */
3255 #define FLG_INSIDE 4
3256 /* Cleared when an operand of &&. */
3257 #define FLG_OUTSIDE_AND 8
3260 write_test_expr (rtx exp, unsigned int attrs_cached, int flags)
3262 int comparison_operator = 0;
3264 struct attr_desc *attr;
3266 /* In order not to worry about operator precedence, surround our part of
3267 the expression with parentheses. */
3270 code = GET_CODE (exp);
3273 /* Binary operators. */
3276 printf ("(unsigned) ");
3282 comparison_operator = FLG_BITWISE;
3284 case PLUS: case MINUS: case MULT: case DIV: case MOD:
3285 case AND: case IOR: case XOR:
3286 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3287 if ((code != AND && code != IOR) || (flags & FLG_BITWISE))
3289 flags &= ~(FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND);
3290 write_test_expr (XEXP (exp, 0), attrs_cached,
3291 flags | comparison_operator);
3296 flags &= ~FLG_OUTSIDE_AND;
3297 if (GET_CODE (XEXP (exp, 0)) == code
3298 || GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3299 || (GET_CODE (XEXP (exp, 0)) == NOT
3300 && GET_CODE (XEXP (XEXP (exp, 0), 0)) == EQ_ATTR))
3302 = write_test_expr (XEXP (exp, 0), attrs_cached, flags);
3304 write_test_expr (XEXP (exp, 0), attrs_cached, flags);
3321 printf (" >= (unsigned) ");
3324 printf (" > (unsigned) ");
3333 printf (" <= (unsigned) ");
3336 printf (" < (unsigned) ");
3354 if (flags & FLG_BITWISE)
3360 if (flags & FLG_BITWISE)
3381 /* For if (something && (cached_x = get_attr_x (insn)) == X)
3382 cached_x is only known to be initialized in then block. */
3383 flags &= ~FLG_AFTER;
3385 else if (code == IOR)
3387 if (flags & FLG_OUTSIDE_AND)
3388 /* For if (something || (cached_x = get_attr_x (insn)) == X)
3389 cached_x is only known to be initialized in else block
3390 and else if conditions. */
3391 flags &= ~FLG_INSIDE;
3393 /* For if ((something || (cached_x = get_attr_x (insn)) == X)
3395 cached_x is not know to be initialized anywhere. */
3396 flags &= ~(FLG_AFTER | FLG_INSIDE);
3398 if ((code == AND || code == IOR)
3399 && (GET_CODE (XEXP (exp, 1)) == code
3400 || GET_CODE (XEXP (exp, 1)) == EQ_ATTR
3401 || (GET_CODE (XEXP (exp, 1)) == NOT
3402 && GET_CODE (XEXP (XEXP (exp, 1), 0)) == EQ_ATTR)))
3404 = write_test_expr (XEXP (exp, 1), attrs_cached, flags);
3406 write_test_expr (XEXP (exp, 1), attrs_cached,
3407 flags | comparison_operator);
3411 /* Special-case (not (eq_attrq "alternative" "x")) */
3412 if (! (flags & FLG_BITWISE) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
3414 if (XSTR (XEXP (exp, 0), 0) == alternative_name)
3416 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
3421 attrs_cached = write_test_expr (XEXP (exp, 0), attrs_cached, flags);
3425 /* Otherwise, fall through to normal unary operator. */
3427 /* Unary operators. */
3432 if (flags & FLG_BITWISE)
3447 flags &= ~(FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND);
3448 write_test_expr (XEXP (exp, 0), attrs_cached, flags);
3453 int set = XINT (exp, 0), bit = 0;
3455 if (flags & FLG_BITWISE)
3456 fatal ("EQ_ATTR_ALT not valid inside comparison");
3459 fatal ("Empty EQ_ATTR_ALT should be optimized out");
3461 if (!(set & (set - 1)))
3463 if (!(set & 0xffff))
3486 printf ("which_alternative %s= %d",
3487 XINT (exp, 1) ? "!" : "=", bit);
3491 printf ("%s((1 << which_alternative) & %#x)",
3492 XINT (exp, 1) ? "!" : "", set);
3497 /* Comparison test of an attribute with a value. Most of these will
3498 have been removed by optimization. Handle "alternative"
3499 specially and give error if EQ_ATTR present inside a comparison. */
3501 if (flags & FLG_BITWISE)
3502 fatal ("EQ_ATTR not valid inside comparison");
3504 if (XSTR (exp, 0) == alternative_name)
3506 printf ("which_alternative == %s", XSTR (exp, 1));
3510 attr = find_attr (&XSTR (exp, 0), 0);
3513 /* Now is the time to expand the value of a constant attribute. */
3516 write_test_expr (evaluate_eq_attr (exp, attr,
3517 attr->default_val->value, -2, -2),
3523 for (i = 0; i < cached_attr_count; i++)
3524 if (attr->name == cached_attrs[i])
3526 if (i < cached_attr_count && (attrs_cached & (1U << i)) != 0)
3527 printf ("cached_%s", attr->name);
3528 else if (i < cached_attr_count && (attrs_to_cache & (1U << i)) != 0)
3530 printf ("(cached_%s = get_attr_%s (insn))",
3531 attr->name, attr->name);
3532 if (flags & FLG_AFTER)
3533 attrs_cached_after |= (1U << i);
3534 if (flags & FLG_INSIDE)
3535 attrs_cached_inside |= (1U << i);
3536 attrs_cached |= (1U << i);
3539 printf ("get_attr_%s (insn)", attr->name);
3541 write_attr_valueq (attr, XSTR (exp, 1));
3545 /* Comparison test of flags for define_delays. */
3547 if (flags & FLG_BITWISE)
3548 fatal ("ATTR_FLAG not valid inside comparison");
3549 printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
3552 /* See if an operand matches a predicate. */
3554 /* If only a mode is given, just ensure the mode matches the operand.
3555 If neither a mode nor predicate is given, error. */
3556 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
3558 if (GET_MODE (exp) == VOIDmode)
3559 fatal ("null MATCH_OPERAND specified as test");
3561 printf ("GET_MODE (operands[%d]) == %smode",
3562 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3565 printf ("%s (operands[%d], %smode)",
3566 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3569 /* Constant integer. */
3571 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
3574 /* A random C expression. */
3576 print_c_condition (XSTR (exp, 0));
3579 /* The address of the branch target. */
3581 printf ("INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
3582 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
3586 /* The address of the current insn. We implement this actually as the
3587 address of the current insn for backward branches, but the last
3588 address of the next insn for forward branches, and both with
3589 adjustments that account for the worst-case possible stretching of
3590 intervening alignments between this insn and its destination. */
3591 printf ("insn_current_reference_address (insn)");
3595 printf ("%s", XSTR (exp, 0));
3599 write_test_expr (XEXP (exp, 0), attrs_cached, 0);
3601 write_test_expr (XEXP (exp, 1), attrs_cached, FLG_BITWISE);
3603 write_test_expr (XEXP (exp, 2), attrs_cached, FLG_BITWISE);
3607 fatal ("bad RTX code `%s' in attribute calculation\n",
3608 GET_RTX_NAME (code));
3612 return attrs_cached;
3615 /* Given an attribute value, return the maximum CONST_STRING argument
3616 encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
3619 max_attr_value (rtx exp, int *unknownp)
3624 switch (GET_CODE (exp))
3627 current_max = atoi (XSTR (exp, 0));
3631 current_max = max_attr_value (XEXP (exp, 1), unknownp);
3632 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3634 n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3635 if (n > current_max)
3641 current_max = max_attr_value (XEXP (exp, 1), unknownp);
3642 n = max_attr_value (XEXP (exp, 2), unknownp);
3643 if (n > current_max)
3649 current_max = INT_MAX;
3656 /* Given an attribute value, return the minimum CONST_STRING argument
3657 encountered. Set *UNKNOWNP and return 0 if the value is unknown. */
3660 min_attr_value (rtx exp, int *unknownp)
3665 switch (GET_CODE (exp))
3668 current_min = atoi (XSTR (exp, 0));
3672 current_min = min_attr_value (XEXP (exp, 1), unknownp);
3673 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3675 n = min_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3676 if (n < current_min)
3682 current_min = min_attr_value (XEXP (exp, 1), unknownp);
3683 n = min_attr_value (XEXP (exp, 2), unknownp);
3684 if (n < current_min)
3690 current_min = INT_MAX;
3697 /* Given an attribute value, return the result of ORing together all
3698 CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
3699 if the numeric value is not known. */
3702 or_attr_value (rtx exp, int *unknownp)
3707 switch (GET_CODE (exp))
3710 current_or = atoi (XSTR (exp, 0));
3714 current_or = or_attr_value (XEXP (exp, 1), unknownp);
3715 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3716 current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3720 current_or = or_attr_value (XEXP (exp, 1), unknownp);
3721 current_or |= or_attr_value (XEXP (exp, 2), unknownp);
3733 /* Scan an attribute value, possibly a conditional, and record what actions
3734 will be required to do any conditional tests in it.
3737 `must_extract' if we need to extract the insn operands
3738 `must_constrain' if we must compute `which_alternative'
3739 `address_used' if an address expression was used
3740 `length_used' if an (eq_attr "length" ...) was used
3744 walk_attr_value (rtx exp)
3753 code = GET_CODE (exp);
3757 if (! ATTR_IND_SIMPLIFIED_P (exp))
3758 /* Since this is an arbitrary expression, it can look at anything.
3759 However, constant expressions do not depend on any particular
3761 must_extract = must_constrain = 1;
3769 must_extract = must_constrain = 1;
3773 if (XSTR (exp, 0) == alternative_name)
3774 must_extract = must_constrain = 1;
3775 else if (strcmp_check (XSTR (exp, 0), length_str) == 0)
3795 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
3800 walk_attr_value (XEXP (exp, i));
3804 if (XVEC (exp, i) != NULL)
3805 for (j = 0; j < XVECLEN (exp, i); j++)
3806 walk_attr_value (XVECEXP (exp, i, j));
3811 /* Write out a function to obtain the attribute for a given INSN. */
3814 write_attr_get (struct attr_desc *attr)
3816 struct attr_value *av, *common_av;
3819 /* Find the most used attribute value. Handle that as the `default' of the
3820 switch we will generate. */
3821 common_av = find_most_used (attr);
3823 /* Write out start of function, then all values with explicit `case' lines,
3824 then a `default', then the value with the most uses. */
3825 if (attr->enum_name)
3826 printf ("enum %s\n", attr->enum_name);
3827 else if (!attr->is_numeric)
3828 printf ("enum attr_%s\n", attr->name);
3832 /* If the attribute name starts with a star, the remainder is the name of
3833 the subroutine to use, instead of `get_attr_...'. */
3834 if (attr->name[0] == '*')
3835 printf ("%s (rtx insn ATTRIBUTE_UNUSED)\n", &attr->name[1]);
3836 else if (attr->is_const == 0)
3837 printf ("get_attr_%s (rtx insn ATTRIBUTE_UNUSED)\n", attr->name);
3840 printf ("get_attr_%s (void)\n", attr->name);
3843 for (av = attr->first_value; av; av = av->next)
3844 if (av->num_insns == 1)
3845 write_attr_set (attr, 2, av->value, "return", ";",
3846 true_rtx, av->first_insn->def->insn_code,
3847 av->first_insn->def->insn_index, 0);
3848 else if (av->num_insns != 0)
3849 write_attr_set (attr, 2, av->value, "return", ";",
3850 true_rtx, -2, 0, 0);
3858 /* Find attributes that are worth caching in the conditions. */
3859 cached_attr_count = 0;
3860 attrs_seen_more_than_once = 0;
3861 for (av = attr->first_value; av; av = av->next)
3863 attrs_seen_once = 0;
3864 find_attrs_to_cache (av->value, true);
3866 /* Remove those that aren't worth caching from the array. */
3867 for (i = 0, j = 0; i < cached_attr_count; i++)
3868 if ((attrs_seen_more_than_once & (1U << i)) != 0)
3870 const char *name = cached_attrs[i];
3871 struct attr_desc *cached_attr;
3873 cached_attrs[j] = name;
3874 cached_attr = find_attr (&name, 0);
3875 gcc_assert (cached_attr && cached_attr->is_const == 0);
3876 if (cached_attr->enum_name)
3877 printf (" enum %s", cached_attr->enum_name);
3878 else if (!cached_attr->is_numeric)
3879 printf (" enum attr_%s", cached_attr->name);
3882 printf (" cached_%s ATTRIBUTE_UNUSED;\n", name);
3885 cached_attr_count = j;
3886 if (cached_attr_count)
3889 printf (" switch (recog_memoized (insn))\n");
3892 for (av = attr->first_value; av; av = av->next)
3893 if (av != common_av)
3894 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
3896 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
3897 printf (" }\n}\n\n");
3898 cached_attr_count = 0;
3901 /* Given an AND tree of known true terms (because we are inside an `if' with
3902 that as the condition or are in an `else' clause) and an expression,
3903 replace any known true terms with TRUE. Use `simplify_and_tree' to do
3904 the bulk of the work. */
3907 eliminate_known_true (rtx known_true, rtx exp, int insn_code, int insn_index)
3911 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
3913 if (GET_CODE (known_true) == AND)
3915 exp = eliminate_known_true (XEXP (known_true, 0), exp,
3916 insn_code, insn_index);
3917 exp = eliminate_known_true (XEXP (known_true, 1), exp,
3918 insn_code, insn_index);
3923 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
3929 /* Write out a series of tests and assignment statements to perform tests and
3930 sets of an attribute value. We are passed an indentation amount and prefix
3931 and suffix strings to write around each attribute value (e.g., "return"
3935 write_attr_set (struct attr_desc *attr, int indent, rtx value,
3936 const char *prefix, const char *suffix, rtx known_true,
3937 int insn_code, int insn_index, unsigned int attrs_cached)
3939 if (GET_CODE (value) == COND)
3941 /* Assume the default value will be the default of the COND unless we
3942 find an always true expression. */
3943 rtx default_val = XEXP (value, 1);
3944 rtx our_known_true = known_true;
3949 if (cached_attr_count)
3951 attrs_seen_once = 0;
3952 attrs_seen_more_than_once = 0;
3953 for (i = 0; i < XVECLEN (value, 0); i += 2)
3954 find_attrs_to_cache (XVECEXP (value, 0, i), false);
3955 attrs_to_cache |= attrs_seen_more_than_once;
3958 for (i = 0; i < XVECLEN (value, 0); i += 2)
3963 testexp = eliminate_known_true (our_known_true,
3964 XVECEXP (value, 0, i),
3965 insn_code, insn_index);
3966 newexp = attr_rtx (NOT, testexp);
3967 newexp = insert_right_side (AND, our_known_true, newexp,
3968 insn_code, insn_index);
3970 /* If the test expression is always true or if the next `known_true'
3971 expression is always false, this is the last case, so break
3972 out and let this value be the `else' case. */
3973 if (testexp == true_rtx || newexp == false_rtx)
3975 default_val = XVECEXP (value, 0, i + 1);
3979 /* Compute the expression to pass to our recursive call as being
3981 inner_true = insert_right_side (AND, our_known_true,
3982 testexp, insn_code, insn_index);
3984 /* If this is always false, skip it. */
3985 if (inner_true == false_rtx)
3988 attrs_cached_inside = attrs_cached;
3989 attrs_cached_after = attrs_cached;
3990 write_indent (indent);
3991 printf ("%sif ", first_if ? "" : "else ");
3993 write_test_expr (testexp, attrs_cached,
3994 (FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND));
3995 attrs_cached = attrs_cached_after;
3997 write_indent (indent + 2);
4000 write_attr_set (attr, indent + 4,
4001 XVECEXP (value, 0, i + 1), prefix, suffix,
4002 inner_true, insn_code, insn_index,
4003 attrs_cached_inside);
4004 write_indent (indent + 2);
4006 our_known_true = newexp;
4011 write_indent (indent);
4013 write_indent (indent + 2);
4017 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
4018 prefix, suffix, our_known_true, insn_code, insn_index,
4023 write_indent (indent + 2);
4029 write_indent (indent);
4030 printf ("%s ", prefix);
4031 write_attr_value (attr, value);
4032 printf ("%s\n", suffix);
4036 /* Write a series of case statements for every instruction in list IE.
4037 INDENT is the amount of indentation to write before each case. */
4040 write_insn_cases (struct insn_ent *ie, int indent)
4042 for (; ie != 0; ie = ie->next)
4043 if (ie->def->insn_code != -1)
4045 write_indent (indent);
4046 if (GET_CODE (ie->def->def) == DEFINE_PEEPHOLE)
4047 printf ("case %d: /* define_peephole, line %d */\n",
4048 ie->def->insn_code, ie->def->lineno);
4050 printf ("case %d: /* %s */\n",
4051 ie->def->insn_code, XSTR (ie->def->def, 0));
4055 /* Write out the computation for one attribute value. */
4058 write_attr_case (struct attr_desc *attr, struct attr_value *av,
4059 int write_case_lines, const char *prefix, const char *suffix,
4060 int indent, rtx known_true)
4062 if (av->num_insns == 0)
4065 if (av->has_asm_insn)
4067 write_indent (indent);
4068 printf ("case -1:\n");
4069 write_indent (indent + 2);
4070 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
4071 write_indent (indent + 2);
4072 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
4073 write_indent (indent + 2);
4074 printf (" fatal_insn_not_found (insn);\n");
4077 if (write_case_lines)
4078 write_insn_cases (av->first_insn, indent);
4081 write_indent (indent);
4082 printf ("default:\n");
4085 /* See what we have to do to output this value. */
4086 must_extract = must_constrain = address_used = 0;
4087 walk_attr_value (av->value);
4091 write_indent (indent + 2);
4092 printf ("extract_constrain_insn_cached (insn);\n");
4094 else if (must_extract)
4096 write_indent (indent + 2);
4097 printf ("extract_insn_cached (insn);\n");
4101 if (av->num_insns == 1)
4102 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
4103 known_true, av->first_insn->def->insn_code,
4104 av->first_insn->def->insn_index, 0);
4106 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
4107 known_true, -2, 0, 0);
4109 if (strncmp (prefix, "return", 6))
4111 write_indent (indent + 2);
4112 printf ("break;\n");
4117 /* Utilities to write in various forms. */
4120 write_attr_valueq (struct attr_desc *attr, const char *s)
4122 if (attr->is_numeric)
4128 if (num > 9 || num < 0)
4129 printf (" /* %#x */", num);
4133 write_upcase (attr->enum_name ? attr->enum_name : attr->name);
4140 write_attr_value (struct attr_desc *attr, rtx value)
4144 switch (GET_CODE (value))
4147 write_attr_valueq (attr, XSTR (value, 0));
4151 printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
4155 print_c_condition (XSTR (value, 0));
4160 struct attr_desc *attr2 = find_attr (&XSTR (value, 0), 0);
4161 if (attr->enum_name)
4162 printf ("(enum %s)", attr->enum_name);
4163 else if (!attr->is_numeric)
4164 printf ("(enum attr_%s)", attr->name);
4165 else if (!attr2->is_numeric)
4168 printf ("get_attr_%s (%s)", attr2->name,
4169 (attr2->is_const ? "" : "insn"));
4190 write_attr_value (attr, XEXP (value, 0));
4194 write_attr_value (attr, XEXP (value, 1));
4203 write_upcase (const char *str)
4207 /* The argument of TOUPPER should not have side effects. */
4208 putchar (TOUPPER(*str));
4214 write_indent (int indent)
4216 for (; indent > 8; indent -= 8)
4219 for (; indent; indent--)
4223 /* Write a subroutine that is given an insn that requires a delay slot, a
4224 delay slot ordinal, and a candidate insn. It returns nonzero if the
4225 candidate can be placed in the specified delay slot of the insn.
4227 We can write as many as three subroutines. `eligible_for_delay'
4228 handles normal delay slots, `eligible_for_annul_true' indicates that
4229 the specified insn can be annulled if the branch is true, and likewise
4230 for `eligible_for_annul_false'.
4232 KIND is a string distinguishing these three cases ("delay", "annul_true",
4233 or "annul_false"). */
4236 write_eligible_delay (const char *kind)
4238 struct delay_desc *delay;
4242 struct attr_desc *attr;
4243 struct attr_value *av, *common_av;
4246 /* Compute the maximum number of delay slots required. We use the delay
4247 ordinal times this number plus one, plus the slot number as an index into
4248 the appropriate predicate to test. */
4250 for (delay = delays, max_slots = 0; delay; delay = delay->next)
4251 if (XVECLEN (delay->def, 1) / 3 > max_slots)
4252 max_slots = XVECLEN (delay->def, 1) / 3;
4254 /* Write function prelude. */
4257 printf ("eligible_for_%s (rtx delay_insn ATTRIBUTE_UNUSED, int slot, rtx candidate_insn, int flags ATTRIBUTE_UNUSED)\n",
4260 printf (" rtx insn;\n");
4262 printf (" gcc_assert (slot < %d);\n", max_slots);
4264 /* Allow dbr_schedule to pass labels, etc. This can happen if try_split
4265 converts a compound instruction into a loop. */
4266 printf (" if (!INSN_P (candidate_insn))\n");
4267 printf (" return 0;\n");
4270 /* If more than one delay type, find out which type the delay insn is. */
4274 attr = find_attr (&delay_type_str, 0);
4276 common_av = find_most_used (attr);
4278 printf (" insn = delay_insn;\n");
4279 printf (" switch (recog_memoized (insn))\n");
4282 sprintf (str, " * %d;\n break;", max_slots);
4283 for (av = attr->first_value; av; av = av->next)
4284 if (av != common_av)
4285 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
4287 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
4290 /* Ensure matched. Otherwise, shouldn't have been called. */
4291 printf (" gcc_assert (slot >= %d);\n\n", max_slots);
4294 /* If just one type of delay slot, write simple switch. */
4295 if (num_delays == 1 && max_slots == 1)
4297 printf (" insn = candidate_insn;\n");
4298 printf (" switch (recog_memoized (insn))\n");
4301 attr = find_attr (&delay_1_0_str, 0);
4303 common_av = find_most_used (attr);
4305 for (av = attr->first_value; av; av = av->next)
4306 if (av != common_av)
4307 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
4309 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
4315 /* Write a nested CASE. The first indicates which condition we need to
4316 test, and the inner CASE tests the condition. */
4317 printf (" insn = candidate_insn;\n");
4318 printf (" switch (slot)\n");
4321 for (delay = delays; delay; delay = delay->next)
4322 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
4324 printf (" case %d:\n",
4325 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
4326 printf (" switch (recog_memoized (insn))\n");
4329 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
4331 attr = find_attr (&pstr, 0);
4333 common_av = find_most_used (attr);
4335 for (av = attr->first_value; av; av = av->next)
4336 if (av != common_av)
4337 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
4339 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
4343 printf (" default:\n");
4344 printf (" gcc_unreachable ();\n");
4351 /* This page contains miscellaneous utility routines. */
4353 /* Given a pointer to a (char *), return a malloc'ed string containing the
4354 next comma-separated element. Advance the pointer to after the string
4355 scanned, or the end-of-string. Return NULL if at end of string. */
4358 next_comma_elt (const char **pstr)
4362 start = scan_comma_elt (pstr);
4367 return attr_string (start, *pstr - start);
4370 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
4371 is nonzero, build a new attribute, if one does not exist. *NAME_P is
4372 replaced by a pointer to a canonical copy of the string. */
4374 static struct attr_desc *
4375 find_attr (const char **name_p, int create)
4377 struct attr_desc *attr;
4379 const char *name = *name_p;
4381 /* Before we resort to using `strcmp', see if the string address matches
4382 anywhere. In most cases, it should have been canonicalized to do so. */
4383 if (name == alternative_name)
4386 index = name[0] & (MAX_ATTRS_INDEX - 1);
4387 for (attr = attrs[index]; attr; attr = attr->next)
4388 if (name == attr->name)
4391 /* Otherwise, do it the slow way. */
4392 for (attr = attrs[index]; attr; attr = attr->next)
4393 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
4395 *name_p = attr->name;
4402 attr = oballoc (struct attr_desc);
4403 attr->name = DEF_ATTR_STRING (name);
4404 attr->enum_name = 0;
4405 attr->first_value = attr->default_val = NULL;
4406 attr->is_numeric = attr->is_const = attr->is_special = 0;
4407 attr->next = attrs[index];
4408 attrs[index] = attr;
4410 *name_p = attr->name;
4415 /* Create internal attribute with the given default value. */
4418 make_internal_attr (const char *name, rtx value, int special)
4420 struct attr_desc *attr;
4422 attr = find_attr (&name, 1);
4423 gcc_assert (!attr->default_val);
4425 attr->is_numeric = 1;
4427 attr->is_special = (special & ATTR_SPECIAL) != 0;
4428 attr->default_val = get_attr_value (value, attr, -2);
4431 /* Find the most used value of an attribute. */
4433 static struct attr_value *
4434 find_most_used (struct attr_desc *attr)
4436 struct attr_value *av;
4437 struct attr_value *most_used;
4443 for (av = attr->first_value; av; av = av->next)
4444 if (av->num_insns > nuses)
4445 nuses = av->num_insns, most_used = av;
4450 /* Return (attr_value "n") */
4453 make_numeric_value (int n)
4455 static rtx int_values[20];
4459 gcc_assert (n >= 0);
4461 if (n < 20 && int_values[n])
4462 return int_values[n];
4464 p = attr_printf (MAX_DIGITS, "%d", n);
4465 exp = attr_rtx (CONST_STRING, p);
4468 int_values[n] = exp;
4474 copy_rtx_unchanging (rtx orig)
4476 if (ATTR_IND_SIMPLIFIED_P (orig) || ATTR_CURR_SIMPLIFIED_P (orig))
4479 ATTR_CURR_SIMPLIFIED_P (orig) = 1;
4483 /* Determine if an insn has a constant number of delay slots, i.e., the
4484 number of delay slots is not a function of the length of the insn. */
4487 write_const_num_delay_slots (void)
4489 struct attr_desc *attr = find_attr (&num_delay_slots_str, 0);
4490 struct attr_value *av;
4494 printf ("int\nconst_num_delay_slots (rtx insn)\n");
4496 printf (" switch (recog_memoized (insn))\n");
4499 for (av = attr->first_value; av; av = av->next)
4502 walk_attr_value (av->value);
4504 write_insn_cases (av->first_insn, 4);
4507 printf (" default:\n");
4508 printf (" return 1;\n");
4509 printf (" }\n}\n\n");
4513 /* Synthetic attributes used by insn-automata.c and the scheduler.
4514 These are primarily concerned with (define_insn_reservation)
4519 struct insn_reserv *next;
4522 int default_latency;
4525 /* Sequence number of this insn. */
4528 /* Whether a (define_bypass) construct names this insn in its
4533 static struct insn_reserv *all_insn_reservs = 0;
4534 static struct insn_reserv **last_insn_reserv_p = &all_insn_reservs;
4535 static size_t n_insn_reservs;
4537 /* Store information from a DEFINE_INSN_RESERVATION for future
4538 attribute generation. */
4540 gen_insn_reserv (rtx def)
4542 struct insn_reserv *decl = oballoc (struct insn_reserv);
4544 decl->name = DEF_ATTR_STRING (XSTR (def, 0));
4545 decl->default_latency = XINT (def, 1);
4546 decl->condexp = check_attr_test (XEXP (def, 2), 0, 0);
4547 decl->insn_num = n_insn_reservs;
4548 decl->bypassed = false;
4551 *last_insn_reserv_p = decl;
4552 last_insn_reserv_p = &decl->next;
4556 /* Store information from a DEFINE_BYPASS for future attribute
4557 generation. The only thing we care about is the list of output
4558 insns, which will later be used to tag reservation structures with
4559 a 'bypassed' bit. */
4563 struct bypass_list *next;
4564 const char *pattern;
4567 static struct bypass_list *all_bypasses;
4568 static size_t n_bypasses;
4571 gen_bypass_1 (const char *s, size_t len)
4573 struct bypass_list *b;
4578 s = attr_string (s, len);
4579 for (b = all_bypasses; b; b = b->next)
4580 if (s == b->pattern)
4581 return; /* already got that one */
4583 b = oballoc (struct bypass_list);
4585 b->next = all_bypasses;
4591 gen_bypass (rtx def)
4593 const char *p, *base;
4595 for (p = base = XSTR (def, 1); *p; p++)
4598 gen_bypass_1 (base, p - base);
4601 while (ISSPACE (*p));
4604 gen_bypass_1 (base, p - base);
4607 /* Find and mark all of the bypassed insns. */
4609 process_bypasses (void)
4611 struct bypass_list *b;
4612 struct insn_reserv *r;
4614 /* The reservation list is likely to be much longer than the bypass
4616 for (r = all_insn_reservs; r; r = r->next)
4617 for (b = all_bypasses; b; b = b->next)
4618 if (fnmatch (b->pattern, r->name, 0) == 0)
4622 /* Check that attribute NAME is used in define_insn_reservation condition
4623 EXP. Return true if it is. */
4625 check_tune_attr (const char *name, rtx exp)
4627 switch (GET_CODE (exp))
4630 if (check_tune_attr (name, XEXP (exp, 0)))
4632 return check_tune_attr (name, XEXP (exp, 1));
4635 return (check_tune_attr (name, XEXP (exp, 0))
4636 && check_tune_attr (name, XEXP (exp, 1)));
4639 return XSTR (exp, 0) == name;
4646 /* Try to find a const attribute (usually cpu or tune) that is used
4647 in all define_insn_reservation conditions. */
4648 static struct attr_desc *
4649 find_tune_attr (rtx exp)
4651 struct attr_desc *attr;
4653 switch (GET_CODE (exp))
4657 attr = find_tune_attr (XEXP (exp, 0));
4660 return find_tune_attr (XEXP (exp, 1));
4663 if (XSTR (exp, 0) == alternative_name)
4666 attr = find_attr (&XSTR (exp, 0), 0);
4669 if (attr->is_const && !attr->is_special)
4671 struct insn_reserv *decl;
4673 for (decl = all_insn_reservs; decl; decl = decl->next)
4674 if (! check_tune_attr (attr->name, decl->condexp))
4685 /* Create all of the attributes that describe automaton properties. */
4687 make_automaton_attrs (void)
4690 struct insn_reserv *decl;
4691 rtx code_exp, lats_exp, byps_exp;
4692 struct attr_desc *tune_attr;
4694 if (n_insn_reservs == 0)
4697 tune_attr = find_tune_attr (all_insn_reservs->condexp);
4698 if (tune_attr != NULL)
4700 rtx *condexps = XNEWVEC (rtx, n_insn_reservs * 3);
4701 struct attr_value *val;
4704 gcc_assert (tune_attr->is_const
4705 && !tune_attr->is_special
4706 && !tune_attr->is_numeric);
4707 for (val = tune_attr->first_value; val; val = val->next)
4709 if (val == tune_attr->default_val)
4711 gcc_assert (GET_CODE (val->value) == CONST_STRING);
4712 printf ("static int internal_dfa_insn_code_%s (rtx);\n"
4713 "static int insn_default_latency_%s (rtx);\n",
4714 XSTR (val->value, 0), XSTR (val->value, 0));
4718 printf ("int (*internal_dfa_insn_code) (rtx);\n");
4719 printf ("int (*insn_default_latency) (rtx);\n");
4722 printf ("init_sched_attrs (void)\n");
4725 for (val = tune_attr->first_value; val; val = val->next)
4729 rtx test = attr_rtx (EQ_ATTR, tune_attr->name, XSTR (val->value, 0));
4731 if (val == tune_attr->default_val)
4733 for (decl = all_insn_reservs, i = 0;
4739 = simplify_and_tree (decl->condexp, &ctest, -2, 0);
4740 if (condexp == false_rtx)
4742 if (condexp == true_rtx)
4744 condexps[i] = condexp;
4745 condexps[i + 1] = make_numeric_value (decl->insn_num);
4746 condexps[i + 2] = make_numeric_value (decl->default_latency);
4750 code_exp = rtx_alloc (COND);
4751 lats_exp = rtx_alloc (COND);
4754 XVEC (code_exp, 0) = rtvec_alloc (j);
4755 XVEC (lats_exp, 0) = rtvec_alloc (j);
4759 XEXP (code_exp, 1) = make_numeric_value (decl->insn_num);
4760 XEXP (lats_exp, 1) = make_numeric_value (decl->default_latency);
4764 XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
4765 XEXP (lats_exp, 1) = make_numeric_value (0);
4772 XVECEXP (code_exp, 0, j) = condexps[i];
4773 XVECEXP (lats_exp, 0, j) = condexps[i];
4775 XVECEXP (code_exp, 0, j + 1) = condexps[i + 1];
4776 XVECEXP (lats_exp, 0, j + 1) = condexps[i + 2];
4779 name = XNEWVEC (char,
4780 sizeof ("*internal_dfa_insn_code_")
4781 + strlen (XSTR (val->value, 0)));
4782 strcpy (name, "*internal_dfa_insn_code_");
4783 strcat (name, XSTR (val->value, 0));
4784 make_internal_attr (name, code_exp, ATTR_NONE);
4785 strcpy (name, "*insn_default_latency_");
4786 strcat (name, XSTR (val->value, 0));
4787 make_internal_attr (name, lats_exp, ATTR_NONE);
4796 printf (" else if (");
4797 write_test_expr (test, 0, 0);
4800 printf (" internal_dfa_insn_code\n");
4801 printf (" = internal_dfa_insn_code_%s;\n",
4802 XSTR (val->value, 0));
4803 printf (" insn_default_latency\n");
4804 printf (" = insn_default_latency_%s;\n",
4805 XSTR (val->value, 0));
4810 printf (" gcc_unreachable ();\n");
4814 XDELETEVEC (condexps);
4818 code_exp = rtx_alloc (COND);
4819 lats_exp = rtx_alloc (COND);
4821 XVEC (code_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
4822 XVEC (lats_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
4824 XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
4825 XEXP (lats_exp, 1) = make_numeric_value (0);
4827 for (decl = all_insn_reservs, i = 0;
4829 decl = decl->next, i += 2)
4831 XVECEXP (code_exp, 0, i) = decl->condexp;
4832 XVECEXP (lats_exp, 0, i) = decl->condexp;
4834 XVECEXP (code_exp, 0, i+1) = make_numeric_value (decl->insn_num);
4835 XVECEXP (lats_exp, 0, i+1)
4836 = make_numeric_value (decl->default_latency);
4838 make_internal_attr ("*internal_dfa_insn_code", code_exp, ATTR_NONE);
4839 make_internal_attr ("*insn_default_latency", lats_exp, ATTR_NONE);
4842 if (n_bypasses == 0)
4843 byps_exp = make_numeric_value (0);
4846 process_bypasses ();
4848 byps_exp = rtx_alloc (COND);
4849 XVEC (byps_exp, 0) = rtvec_alloc (n_bypasses * 2);
4850 XEXP (byps_exp, 1) = make_numeric_value (0);
4851 for (decl = all_insn_reservs, i = 0;
4856 XVECEXP (byps_exp, 0, i) = decl->condexp;
4857 XVECEXP (byps_exp, 0, i+1) = make_numeric_value (1);
4862 make_internal_attr ("*bypass_p", byps_exp, ATTR_NONE);
4866 main (int argc, char **argv)
4869 struct attr_desc *attr;
4870 struct insn_def *id;
4874 progname = "genattrtab";
4876 if (!init_rtx_reader_args (argc, argv))
4877 return (FATAL_EXIT_CODE);
4879 obstack_init (hash_obstack);
4880 obstack_init (temp_obstack);
4882 /* Set up true and false rtx's */
4883 true_rtx = rtx_alloc (CONST_INT);
4884 XWINT (true_rtx, 0) = 1;
4885 false_rtx = rtx_alloc (CONST_INT);
4886 XWINT (false_rtx, 0) = 0;
4887 ATTR_IND_SIMPLIFIED_P (true_rtx) = ATTR_IND_SIMPLIFIED_P (false_rtx) = 1;
4888 ATTR_PERMANENT_P (true_rtx) = ATTR_PERMANENT_P (false_rtx) = 1;
4890 alternative_name = DEF_ATTR_STRING ("alternative");
4891 length_str = DEF_ATTR_STRING ("length");
4892 delay_type_str = DEF_ATTR_STRING ("*delay_type");
4893 delay_1_0_str = DEF_ATTR_STRING ("*delay_1_0");
4894 num_delay_slots_str = DEF_ATTR_STRING ("*num_delay_slots");
4896 printf ("/* Generated automatically by the program `genattrtab'\n\
4897 from the machine description file `md'. */\n\n");
4899 /* Read the machine description. */
4905 desc = read_md_rtx (&lineno, &insn_code_number);
4909 switch (GET_CODE (desc))
4912 case DEFINE_PEEPHOLE:
4913 case DEFINE_ASM_ATTRIBUTES:
4914 gen_insn (desc, lineno);
4918 case DEFINE_ENUM_ATTR:
4919 gen_attr (desc, lineno);
4923 gen_delay (desc, lineno);
4926 case DEFINE_INSN_RESERVATION:
4927 gen_insn_reserv (desc);
4937 if (GET_CODE (desc) != DEFINE_ASM_ATTRIBUTES)
4938 insn_index_number++;
4942 return FATAL_EXIT_CODE;
4946 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
4947 if (! got_define_asm_attributes)
4949 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
4950 XVEC (tem, 0) = rtvec_alloc (0);
4954 /* Expand DEFINE_DELAY information into new attribute. */
4958 printf ("#include \"config.h\"\n");
4959 printf ("#include \"system.h\"\n");
4960 printf ("#include \"coretypes.h\"\n");
4961 printf ("#include \"tm.h\"\n");
4962 printf ("#include \"rtl.h\"\n");
4963 printf ("#include \"insn-attr.h\"\n");
4964 printf ("#include \"tm_p.h\"\n");
4965 printf ("#include \"insn-config.h\"\n");
4966 printf ("#include \"recog.h\"\n");
4967 printf ("#include \"regs.h\"\n");
4968 printf ("#include \"output.h\"\n");
4969 printf ("#include \"diagnostic-core.h\"\n");
4970 printf ("#include \"flags.h\"\n");
4971 printf ("#include \"function.h\"\n");
4973 printf ("#define operands recog_data.operand\n\n");
4975 /* Make `insn_alternatives'. */
4976 insn_alternatives = oballocvec (int, insn_code_number);
4977 for (id = defs; id; id = id->next)
4978 if (id->insn_code >= 0)
4979 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
4981 /* Make `insn_n_alternatives'. */
4982 insn_n_alternatives = oballocvec (int, insn_code_number);
4983 for (id = defs; id; id = id->next)
4984 if (id->insn_code >= 0)
4985 insn_n_alternatives[id->insn_code] = id->num_alternatives;
4987 /* Construct extra attributes for automata. */
4988 make_automaton_attrs ();
4990 /* Prepare to write out attribute subroutines by checking everything stored
4991 away and building the attribute cases. */
4995 for (i = 0; i < MAX_ATTRS_INDEX; i++)
4996 for (attr = attrs[i]; attr; attr = attr->next)
4997 attr->default_val->value
4998 = check_attr_value (attr->default_val->value, attr);
5001 return FATAL_EXIT_CODE;
5003 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5004 for (attr = attrs[i]; attr; attr = attr->next)
5007 /* Construct extra attributes for `length'. */
5008 make_length_attrs ();
5010 /* Perform any possible optimizations to speed up compilation. */
5013 /* Now write out all the `gen_attr_...' routines. Do these before the
5014 special routines so that they get defined before they are used. */
5016 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5017 for (attr = attrs[i]; attr; attr = attr->next)
5019 if (! attr->is_special && ! attr->is_const)
5020 write_attr_get (attr);
5023 /* Write out delay eligibility information, if DEFINE_DELAY present.
5024 (The function to compute the number of delay slots will be written
5028 write_eligible_delay ("delay");
5029 if (have_annul_true)
5030 write_eligible_delay ("annul_true");
5031 if (have_annul_false)
5032 write_eligible_delay ("annul_false");
5035 /* Write out constant delay slot info. */
5036 write_const_num_delay_slots ();
5038 write_length_unit_log ();
5041 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);