1 /* Generate code from machine description to compute values of attributes.
2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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
94 `volatil' (ATTR_EQ_ATTR_P): During simplify_by_exploding the value of an
95 EQ_ATTR rtx is true if !volatil and false if volatil. */
97 #define ATTR_IND_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), unchanging))
98 #define ATTR_CURR_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), in_struct))
99 #define ATTR_PERMANENT_P(RTX) (RTX_FLAG((RTX), return_val))
100 #define ATTR_EQ_ATTR_P(RTX) (RTX_FLAG((RTX), volatil))
103 #define strcmp_check(S1, S2) ((S1) == (S2) \
105 : (gcc_assert (strcmp ((S1), (S2))), 1))
107 #define strcmp_check(S1, S2) ((S1) != (S2))
112 #include "coretypes.h"
116 #include "gensupport.h"
118 #ifdef HAVE_SYS_RESOURCE_H
119 # include <sys/resource.h>
122 /* We must include obstack.h after <sys/time.h>, to avoid lossage with
123 /usr/include/sys/stdtypes.h on Sun OS 4.x. */
127 #include "genattrtab.h"
129 static struct obstack obstack1, obstack2;
130 struct obstack *hash_obstack = &obstack1;
131 struct obstack *temp_obstack = &obstack2;
133 /* enough space to reserve for printing out ints */
134 #define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)
136 /* Define structures used to record attributes and values. */
138 /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
139 encountered, we store all the relevant information into a
140 `struct insn_def'. This is done to allow attribute definitions to occur
141 anywhere in the file. */
145 struct insn_def *next; /* Next insn in chain. */
146 rtx def; /* The DEFINE_... */
147 int insn_code; /* Instruction number. */
148 int insn_index; /* Expression numer in file, for errors. */
149 int lineno; /* Line number. */
150 int num_alternatives; /* Number of alternatives. */
151 int vec_idx; /* Index of attribute vector in `def'. */
154 /* Once everything has been read in, we store in each attribute value a list
155 of insn codes that have that value. Here is the structure used for the
160 struct insn_ent *next; /* Next in chain. */
161 struct insn_def *def; /* Instruction definition. */
164 /* Each value of an attribute (either constant or computed) is assigned a
165 structure which is used as the listhead of the insns that have that
170 rtx value; /* Value of attribute. */
171 struct attr_value *next; /* Next attribute value in chain. */
172 struct insn_ent *first_insn; /* First insn with this value. */
173 int num_insns; /* Number of insns with this value. */
174 int has_asm_insn; /* True if this value used for `asm' insns */
177 /* Structure for each attribute. */
181 char *name; /* Name of attribute. */
182 struct attr_desc *next; /* Next attribute. */
183 struct attr_value *first_value; /* First value of this attribute. */
184 struct attr_value *default_val; /* Default value for this attribute. */
185 int lineno : 24; /* Line number. */
186 unsigned is_numeric : 1; /* Values of this attribute are numeric. */
187 unsigned is_const : 1; /* Attribute value constant for each run. */
188 unsigned is_special : 1; /* Don't call `write_attr_set'. */
189 unsigned static_p : 1; /* Make the output function static. */
192 /* Structure for each DEFINE_DELAY. */
196 rtx def; /* DEFINE_DELAY expression. */
197 struct delay_desc *next; /* Next DEFINE_DELAY. */
198 int num; /* Number of DEFINE_DELAY, starting at 1. */
199 int lineno; /* Line number. */
202 /* Listheads of above structures. */
204 /* This one is indexed by the first character of the attribute name. */
205 #define MAX_ATTRS_INDEX 256
206 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
207 static struct insn_def *defs;
208 static struct delay_desc *delays;
210 /* Other variables. */
212 static int insn_code_number;
213 static int insn_index_number;
214 static int got_define_asm_attributes;
215 static int must_extract;
216 static int must_constrain;
217 static int address_used;
218 static int length_used;
219 static int num_delays;
220 static int have_annul_true, have_annul_false;
221 static int num_insn_ents;
225 /* Stores, for each insn code, the number of constraint alternatives. */
227 static int *insn_n_alternatives;
229 /* Stores, for each insn code, a bitmap that has bits on for each possible
232 static int *insn_alternatives;
234 /* If nonzero, assume that the `alternative' attr has this value.
235 This is the hashed, unique string for the numeral
236 whose value is chosen alternative. */
238 static const char *current_alternative_string;
240 /* Used to simplify expressions. */
242 static rtx true_rtx, false_rtx;
244 /* Used to reduce calls to `strcmp' */
246 static char *alternative_name;
247 static const char *length_str;
248 static const char *delay_type_str;
249 static const char *delay_1_0_str;
250 static const char *num_delay_slots_str;
252 /* Indicate that REG_DEAD notes are valid if dead_or_set_p is ever
255 int reload_completed = 0;
257 /* Some machines test `optimize' in macros called from rtlanal.c, so we need
258 to define it here. */
262 /* Simplify an expression. Only call the routine if there is something to
264 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
265 (ATTR_IND_SIMPLIFIED_P (EXP) || ATTR_CURR_SIMPLIFIED_P (EXP) ? (EXP) \
266 : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
268 /* Simplify (eq_attr ("alternative") ...)
269 when we are working with a particular alternative. */
270 #define SIMPLIFY_ALTERNATIVE(EXP) \
271 if (current_alternative_string \
272 && GET_CODE ((EXP)) == EQ_ATTR \
273 && XSTR ((EXP), 0) == alternative_name) \
274 (EXP) = (XSTR ((EXP), 1) == current_alternative_string \
275 ? true_rtx : false_rtx);
277 #define DEF_ATTR_STRING(S) (attr_string ((S), strlen (S)))
279 /* These are referenced by rtlanal.c and hence need to be defined somewhere.
280 They won't actually be used. */
282 rtx global_rtl[GR_MAX];
283 rtx pic_offset_table_rtx;
285 static void attr_hash_add_rtx (int, rtx);
286 static void attr_hash_add_string (int, char *);
287 static rtx attr_rtx (enum rtx_code, ...);
288 static rtx attr_rtx_1 (enum rtx_code, va_list);
289 static char *attr_string (const char *, int);
290 static rtx check_attr_value (rtx, struct attr_desc *);
291 static rtx convert_set_attr_alternative (rtx, struct insn_def *);
292 static rtx convert_set_attr (rtx, struct insn_def *);
293 static void check_defs (void);
294 static rtx make_canonical (struct attr_desc *, rtx);
295 static struct attr_value *get_attr_value (rtx, struct attr_desc *, int);
296 static rtx copy_rtx_unchanging (rtx);
297 static rtx copy_boolean (rtx);
298 static void expand_delays (void);
299 static void fill_attr (struct attr_desc *);
300 static rtx substitute_address (rtx, rtx (*) (rtx), rtx (*) (rtx));
301 static void make_length_attrs (void);
302 static rtx identity_fn (rtx);
303 static rtx zero_fn (rtx);
304 static rtx one_fn (rtx);
305 static rtx max_fn (rtx);
306 static void write_length_unit_log (void);
307 static rtx simplify_cond (rtx, int, int);
308 static void clear_struct_flag (rtx);
309 static void remove_insn_ent (struct attr_value *, struct insn_ent *);
310 static void insert_insn_ent (struct attr_value *, struct insn_ent *);
311 static rtx insert_right_side (enum rtx_code, rtx, rtx, int, int);
312 static rtx make_alternative_compare (int);
313 static int compute_alternative_mask (rtx, enum rtx_code);
314 static rtx evaluate_eq_attr (rtx, rtx, int, int);
315 static rtx simplify_and_tree (rtx, rtx *, int, int);
316 static rtx simplify_or_tree (rtx, rtx *, int, int);
317 static rtx simplify_test_exp (rtx, int, int);
318 static rtx simplify_test_exp_in_temp (rtx, int, int);
319 static void optimize_attrs (void);
320 static void gen_attr (rtx, int);
321 static int count_alternatives (rtx);
322 static int compares_alternatives_p (rtx);
323 static int contained_in_p (rtx, rtx);
324 static void gen_insn (rtx, int);
325 static void gen_delay (rtx, int);
326 static void write_test_expr (rtx, int);
327 static int max_attr_value (rtx, int*);
328 static int or_attr_value (rtx, int*);
329 static void walk_attr_value (rtx);
330 static void write_attr_get (struct attr_desc *);
331 static rtx eliminate_known_true (rtx, rtx, int, int);
332 static void write_attr_set (struct attr_desc *, int, rtx,
333 const char *, const char *, rtx,
335 static void write_insn_cases (struct insn_ent *, int);
336 static void write_attr_case (struct attr_desc *, struct attr_value *,
337 int, const char *, const char *, int, rtx);
338 static void write_attr_valueq (struct attr_desc *, const char *);
339 static void write_attr_value (struct attr_desc *, rtx);
340 static void write_upcase (const char *);
341 static void write_indent (int);
342 static void write_eligible_delay (const char *);
343 static int write_expr_attr_cache (rtx, struct attr_desc *);
344 static void write_const_num_delay_slots (void);
345 static char *next_comma_elt (const char **);
346 static struct attr_desc *find_attr (const char **, int);
347 static struct attr_value *find_most_used (struct attr_desc *);
348 static rtx attr_eq (const char *, const char *);
349 static const char *attr_numeral (int);
350 static int attr_equal_p (rtx, rtx);
351 static rtx attr_copy_rtx (rtx);
352 static int attr_rtx_cost (rtx);
353 static bool attr_alt_subset_p (rtx, rtx);
354 static bool attr_alt_subset_of_compl_p (rtx, rtx);
355 static rtx attr_alt_intersection (rtx, rtx);
356 static rtx attr_alt_union (rtx, rtx);
357 static rtx attr_alt_complement (rtx);
358 static bool attr_alt_bit_p (rtx, int);
359 static rtx mk_attr_alt (int);
361 #define oballoc(size) obstack_alloc (hash_obstack, size)
363 /* Hash table for sharing RTL and strings. */
365 /* Each hash table slot is a bucket containing a chain of these structures.
366 Strings are given negative hash codes; RTL expressions are given positive
371 struct attr_hash *next; /* Next structure in the bucket. */
372 int hashcode; /* Hash code of this rtx or string. */
375 char *str; /* The string (negative hash codes) */
376 rtx rtl; /* or the RTL recorded here. */
380 /* Now here is the hash table. When recording an RTL, it is added to
381 the slot whose index is the hash code mod the table size. Note
382 that the hash table is used for several kinds of RTL (see attr_rtx)
383 and for strings. While all these live in the same table, they are
384 completely independent, and the hash code is computed differently
387 #define RTL_HASH_SIZE 4093
388 struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
390 /* Here is how primitive or already-shared RTL's hash
392 #define RTL_HASH(RTL) ((long) (RTL) & 0777777)
394 /* Add an entry to the hash table for RTL with hash code HASHCODE. */
397 attr_hash_add_rtx (int hashcode, rtx rtl)
401 h = obstack_alloc (hash_obstack, sizeof (struct attr_hash));
402 h->hashcode = hashcode;
404 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
405 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
408 /* Add an entry to the hash table for STRING with hash code HASHCODE. */
411 attr_hash_add_string (int hashcode, char *str)
415 h = obstack_alloc (hash_obstack, sizeof (struct attr_hash));
416 h->hashcode = -hashcode;
418 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
419 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
422 /* Generate an RTL expression, but avoid duplicates.
423 Set the ATTR_PERMANENT_P flag for these permanent objects.
425 In some cases we cannot uniquify; then we return an ordinary
426 impermanent rtx with ATTR_PERMANENT_P clear.
430 rtx attr_rtx (code, [element1, ..., elementn]) */
433 attr_rtx_1 (enum rtx_code code, va_list p)
435 rtx rt_val = NULL_RTX;/* RTX to return to caller... */
438 struct obstack *old_obstack = rtl_obstack;
440 /* For each of several cases, search the hash table for an existing entry.
441 Use that entry if one is found; otherwise create a new RTL and add it
444 if (GET_RTX_CLASS (code) == RTX_UNARY)
446 rtx arg0 = va_arg (p, rtx);
448 /* A permanent object cannot point to impermanent ones. */
449 if (! ATTR_PERMANENT_P (arg0))
451 rt_val = rtx_alloc (code);
452 XEXP (rt_val, 0) = arg0;
456 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
457 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
458 if (h->hashcode == hashcode
459 && GET_CODE (h->u.rtl) == code
460 && XEXP (h->u.rtl, 0) == arg0)
465 rtl_obstack = hash_obstack;
466 rt_val = rtx_alloc (code);
467 XEXP (rt_val, 0) = arg0;
470 else if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
471 || GET_RTX_CLASS (code) == RTX_COMM_ARITH
472 || GET_RTX_CLASS (code) == RTX_COMPARE
473 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
475 rtx arg0 = va_arg (p, rtx);
476 rtx arg1 = va_arg (p, rtx);
478 /* A permanent object cannot point to impermanent ones. */
479 if (! ATTR_PERMANENT_P (arg0) || ! ATTR_PERMANENT_P (arg1))
481 rt_val = rtx_alloc (code);
482 XEXP (rt_val, 0) = arg0;
483 XEXP (rt_val, 1) = arg1;
487 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
488 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
489 if (h->hashcode == hashcode
490 && GET_CODE (h->u.rtl) == code
491 && XEXP (h->u.rtl, 0) == arg0
492 && XEXP (h->u.rtl, 1) == arg1)
497 rtl_obstack = hash_obstack;
498 rt_val = rtx_alloc (code);
499 XEXP (rt_val, 0) = arg0;
500 XEXP (rt_val, 1) = arg1;
503 else if (GET_RTX_LENGTH (code) == 1
504 && GET_RTX_FORMAT (code)[0] == 's')
506 char *arg0 = va_arg (p, char *);
508 arg0 = DEF_ATTR_STRING (arg0);
510 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
511 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
512 if (h->hashcode == hashcode
513 && GET_CODE (h->u.rtl) == code
514 && XSTR (h->u.rtl, 0) == arg0)
519 rtl_obstack = hash_obstack;
520 rt_val = rtx_alloc (code);
521 XSTR (rt_val, 0) = arg0;
524 else if (GET_RTX_LENGTH (code) == 2
525 && GET_RTX_FORMAT (code)[0] == 's'
526 && GET_RTX_FORMAT (code)[1] == 's')
528 char *arg0 = va_arg (p, char *);
529 char *arg1 = va_arg (p, char *);
531 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
532 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
533 if (h->hashcode == hashcode
534 && GET_CODE (h->u.rtl) == code
535 && XSTR (h->u.rtl, 0) == arg0
536 && XSTR (h->u.rtl, 1) == arg1)
541 rtl_obstack = hash_obstack;
542 rt_val = rtx_alloc (code);
543 XSTR (rt_val, 0) = arg0;
544 XSTR (rt_val, 1) = arg1;
547 else if (code == CONST_INT)
549 HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
559 int i; /* Array indices... */
560 const char *fmt; /* Current rtx's format... */
562 rt_val = rtx_alloc (code); /* Allocate the storage space. */
564 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
565 for (i = 0; i < GET_RTX_LENGTH (code); i++)
569 case '0': /* Unused field. */
572 case 'i': /* An integer? */
573 XINT (rt_val, i) = va_arg (p, int);
576 case 'w': /* A wide integer? */
577 XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
580 case 's': /* A string? */
581 XSTR (rt_val, i) = va_arg (p, char *);
584 case 'e': /* An expression? */
585 case 'u': /* An insn? Same except when printing. */
586 XEXP (rt_val, i) = va_arg (p, rtx);
589 case 'E': /* An RTX vector? */
590 XVEC (rt_val, i) = va_arg (p, rtvec);
600 rtl_obstack = old_obstack;
601 attr_hash_add_rtx (hashcode, rt_val);
602 ATTR_PERMANENT_P (rt_val) = 1;
607 attr_rtx (enum rtx_code code, ...)
613 result = attr_rtx_1 (code, p);
618 /* Create a new string printed with the printf line arguments into a space
619 of at most LEN bytes:
621 rtx attr_printf (len, format, [arg1, ..., argn]) */
624 attr_printf (unsigned int len, const char *fmt, ...)
631 gcc_assert (len < sizeof str); /* Leave room for \0. */
633 vsprintf (str, fmt, p);
636 return DEF_ATTR_STRING (str);
640 attr_eq (const char *name, const char *value)
642 return attr_rtx (EQ_ATTR, DEF_ATTR_STRING (name), DEF_ATTR_STRING (value));
648 return XSTR (make_numeric_value (n), 0);
651 /* Return a permanent (possibly shared) copy of a string STR (not assumed
652 to be null terminated) with LEN bytes. */
655 attr_string (const char *str, int len)
662 /* Compute the hash code. */
663 hashcode = (len + 1) * 613 + (unsigned) str[0];
664 for (i = 1; i <= len; i += 2)
665 hashcode = ((hashcode * 613) + (unsigned) str[i]);
667 hashcode = -hashcode;
669 /* Search the table for the string. */
670 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
671 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
672 && !strncmp (h->u.str, str, len))
673 return h->u.str; /* <-- return if found. */
675 /* Not found; create a permanent copy and add it to the hash table. */
676 new_str = obstack_alloc (hash_obstack, len + 1);
677 memcpy (new_str, str, len);
679 attr_hash_add_string (hashcode, new_str);
681 return new_str; /* Return the new string. */
684 /* Check two rtx's for equality of contents,
685 taking advantage of the fact that if both are hashed
686 then they can't be equal unless they are the same object. */
689 attr_equal_p (rtx x, rtx y)
691 return (x == y || (! (ATTR_PERMANENT_P (x) && ATTR_PERMANENT_P (y))
692 && rtx_equal_p (x, y)));
695 /* Copy an attribute value expression,
696 descending to all depths, but not copying any
697 permanent hashed subexpressions. */
700 attr_copy_rtx (rtx orig)
705 const char *format_ptr;
707 /* No need to copy a permanent object. */
708 if (ATTR_PERMANENT_P (orig))
711 code = GET_CODE (orig);
729 copy = rtx_alloc (code);
730 PUT_MODE (copy, GET_MODE (orig));
731 ATTR_IND_SIMPLIFIED_P (copy) = ATTR_IND_SIMPLIFIED_P (orig);
732 ATTR_CURR_SIMPLIFIED_P (copy) = ATTR_CURR_SIMPLIFIED_P (orig);
733 ATTR_PERMANENT_P (copy) = ATTR_PERMANENT_P (orig);
734 ATTR_EQ_ATTR_P (copy) = ATTR_EQ_ATTR_P (orig);
736 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
738 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
740 switch (*format_ptr++)
743 XEXP (copy, i) = XEXP (orig, i);
744 if (XEXP (orig, i) != NULL)
745 XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
750 XVEC (copy, i) = XVEC (orig, i);
751 if (XVEC (orig, i) != NULL)
753 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
754 for (j = 0; j < XVECLEN (copy, i); j++)
755 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
761 XINT (copy, i) = XINT (orig, i);
765 XWINT (copy, i) = XWINT (orig, i);
770 XSTR (copy, i) = XSTR (orig, i);
780 /* Given a test expression for an attribute, ensure it is validly formed.
781 IS_CONST indicates whether the expression is constant for each compiler
782 run (a constant expression may not test any particular insn).
784 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
785 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
786 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
788 Update the string address in EQ_ATTR expression to be the same used
789 in the attribute (or `alternative_name') to speed up subsequent
790 `find_attr' calls and eliminate most `strcmp' calls.
792 Return the new expression, if any. */
795 check_attr_test (rtx exp, int is_const, int lineno)
797 struct attr_desc *attr;
798 struct attr_value *av;
799 const char *name_ptr, *p;
802 switch (GET_CODE (exp))
805 /* Handle negation test. */
806 if (XSTR (exp, 1)[0] == '!')
807 return check_attr_test (attr_rtx (NOT,
808 attr_eq (XSTR (exp, 0),
812 else if (n_comma_elts (XSTR (exp, 1)) == 1)
814 attr = find_attr (&XSTR (exp, 0), 0);
817 if (! strcmp (XSTR (exp, 0), "alternative"))
818 return mk_attr_alt (1 << atoi (XSTR (exp, 1)));
820 fatal ("unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
823 if (is_const && ! attr->is_const)
824 fatal ("constant expression uses insn attribute `%s' in EQ_ATTR",
827 /* Copy this just to make it permanent,
828 so expressions using it can be permanent too. */
829 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
831 /* It shouldn't be possible to simplify the value given to a
832 constant attribute, so don't expand this until it's time to
833 write the test expression. */
835 ATTR_IND_SIMPLIFIED_P (exp) = 1;
837 if (attr->is_numeric)
839 for (p = XSTR (exp, 1); *p; p++)
841 fatal ("attribute `%s' takes only numeric values",
846 for (av = attr->first_value; av; av = av->next)
847 if (GET_CODE (av->value) == CONST_STRING
848 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
852 fatal ("unknown value `%s' for `%s' attribute",
853 XSTR (exp, 1), XSTR (exp, 0));
858 if (! strcmp (XSTR (exp, 0), "alternative"))
862 name_ptr = XSTR (exp, 1);
863 while ((p = next_comma_elt (&name_ptr)) != NULL)
864 set |= 1 << atoi (p);
866 return mk_attr_alt (set);
870 /* Make an IOR tree of the possible values. */
872 name_ptr = XSTR (exp, 1);
873 while ((p = next_comma_elt (&name_ptr)) != NULL)
875 newexp = attr_eq (XSTR (exp, 0), p);
876 orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
879 return check_attr_test (orexp, is_const, lineno);
888 /* Either TRUE or FALSE. */
896 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
897 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const, lineno);
901 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
906 fatal ("RTL operator \"%s\" not valid in constant attribute test",
907 GET_RTX_NAME (GET_CODE (exp)));
908 /* These cases can't be simplified. */
909 ATTR_IND_SIMPLIFIED_P (exp) = 1;
912 case LE: case LT: case GT: case GE:
913 case LEU: case LTU: case GTU: case GEU:
915 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
916 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
917 exp = attr_rtx (GET_CODE (exp),
918 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
919 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
920 /* These cases can't be simplified. */
921 ATTR_IND_SIMPLIFIED_P (exp) = 1;
927 /* These cases are valid for constant attributes, but can't be
929 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
930 ATTR_IND_SIMPLIFIED_P (exp) = 1;
934 fatal ("RTL operator \"%s\" not valid in attribute test",
935 GET_RTX_NAME (GET_CODE (exp)));
941 /* Given an expression, ensure that it is validly formed and that all named
942 attribute values are valid for the given attribute. Issue a fatal error
943 if not. If no attribute is specified, assume a numeric attribute.
945 Return a perhaps modified replacement expression for the value. */
948 check_attr_value (rtx exp, struct attr_desc *attr)
950 struct attr_value *av;
954 switch (GET_CODE (exp))
957 if (attr && ! attr->is_numeric)
959 message_with_line (attr->lineno,
960 "CONST_INT not valid for non-numeric attribute %s",
966 if (INTVAL (exp) < 0)
968 message_with_line (attr->lineno,
969 "negative numeric value specified for attribute %s",
977 if (! strcmp (XSTR (exp, 0), "*"))
980 if (attr == 0 || attr->is_numeric)
986 message_with_line (attr ? attr->lineno : 0,
987 "non-numeric value for numeric attribute %s",
988 attr ? attr->name : "internal");
995 for (av = attr->first_value; av; av = av->next)
996 if (GET_CODE (av->value) == CONST_STRING
997 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
1002 message_with_line (attr->lineno,
1003 "unknown value `%s' for `%s' attribute",
1004 XSTR (exp, 0), attr ? attr->name : "internal");
1010 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
1011 attr ? attr->is_const : 0,
1012 attr ? attr->lineno : 0);
1013 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1014 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
1022 if (attr && !attr->is_numeric)
1024 message_with_line (attr->lineno,
1025 "invalid operation `%s' for non-numeric attribute value",
1026 GET_RTX_NAME (GET_CODE (exp)));
1034 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1035 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1043 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1047 if (XVECLEN (exp, 0) % 2 != 0)
1049 message_with_line (attr->lineno,
1050 "first operand of COND must have even length");
1055 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1057 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
1058 attr ? attr->is_const : 0,
1059 attr ? attr->lineno : 0);
1060 XVECEXP (exp, 0, i + 1)
1061 = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
1064 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1069 struct attr_desc *attr2 = find_attr (&XSTR (exp, 0), 0);
1072 message_with_line (attr ? attr->lineno : 0,
1073 "unknown attribute `%s' in ATTR",
1077 else if (attr && attr->is_const && ! attr2->is_const)
1079 message_with_line (attr->lineno,
1080 "non-constant attribute `%s' referenced from `%s'",
1081 XSTR (exp, 0), attr->name);
1085 && attr->is_numeric != attr2->is_numeric)
1087 message_with_line (attr->lineno,
1088 "numeric attribute mismatch calling `%s' from `%s'",
1089 XSTR (exp, 0), attr->name);
1096 /* A constant SYMBOL_REF is valid as a constant attribute test and
1097 is expanded later by make_canonical into a COND. In a non-constant
1098 attribute test, it is left be. */
1099 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1102 message_with_line (attr ? attr->lineno : 0,
1103 "invalid operation `%s' for attribute value",
1104 GET_RTX_NAME (GET_CODE (exp)));
1112 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1113 It becomes a COND with each test being (eq_attr "alternative "n") */
1116 convert_set_attr_alternative (rtx exp, struct insn_def *id)
1118 int num_alt = id->num_alternatives;
1122 if (XVECLEN (exp, 1) != num_alt)
1124 message_with_line (id->lineno,
1125 "bad number of entries in SET_ATTR_ALTERNATIVE");
1130 /* Make a COND with all tests but the last. Select the last value via the
1132 condexp = rtx_alloc (COND);
1133 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1135 for (i = 0; i < num_alt - 1; i++)
1138 p = attr_numeral (i);
1140 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1141 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1144 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1146 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1149 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1150 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1153 convert_set_attr (rtx exp, struct insn_def *id)
1156 const char *name_ptr;
1160 /* See how many alternative specified. */
1161 n = n_comma_elts (XSTR (exp, 1));
1163 return attr_rtx (SET,
1164 attr_rtx (ATTR, XSTR (exp, 0)),
1165 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1167 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1168 XSTR (newexp, 0) = XSTR (exp, 0);
1169 XVEC (newexp, 1) = rtvec_alloc (n);
1171 /* Process each comma-separated name. */
1172 name_ptr = XSTR (exp, 1);
1174 while ((p = next_comma_elt (&name_ptr)) != NULL)
1175 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1177 return convert_set_attr_alternative (newexp, id);
1180 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1181 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1187 struct insn_def *id;
1188 struct attr_desc *attr;
1192 for (id = defs; id; id = id->next)
1194 if (XVEC (id->def, id->vec_idx) == NULL)
1197 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1199 value = XVECEXP (id->def, id->vec_idx, i);
1200 switch (GET_CODE (value))
1203 if (GET_CODE (XEXP (value, 0)) != ATTR)
1205 message_with_line (id->lineno, "bad attribute set");
1211 case SET_ATTR_ALTERNATIVE:
1212 value = convert_set_attr_alternative (value, id);
1216 value = convert_set_attr (value, id);
1220 message_with_line (id->lineno, "invalid attribute code %s",
1221 GET_RTX_NAME (GET_CODE (value)));
1225 if (value == NULL_RTX)
1228 if ((attr = find_attr (&XSTR (XEXP (value, 0), 0), 0)) == NULL)
1230 message_with_line (id->lineno, "unknown attribute %s",
1231 XSTR (XEXP (value, 0), 0));
1236 XVECEXP (id->def, id->vec_idx, i) = value;
1237 XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1242 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1243 expressions by converting them into a COND. This removes cases from this
1244 program. Also, replace an attribute value of "*" with the default attribute
1248 make_canonical (struct attr_desc *attr, rtx exp)
1253 switch (GET_CODE (exp))
1256 exp = make_numeric_value (INTVAL (exp));
1260 if (! strcmp (XSTR (exp, 0), "*"))
1262 if (attr == 0 || attr->default_val == 0)
1263 fatal ("(attr_value \"*\") used in invalid context");
1264 exp = attr->default_val->value;
1267 XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1272 if (!attr->is_const || ATTR_IND_SIMPLIFIED_P (exp))
1274 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1275 This makes the COND something that won't be considered an arbitrary
1276 expression by walk_attr_value. */
1277 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1278 exp = check_attr_value (exp, attr);
1282 newexp = rtx_alloc (COND);
1283 XVEC (newexp, 0) = rtvec_alloc (2);
1284 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1285 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1287 XEXP (newexp, 1) = XEXP (exp, 2);
1290 /* Fall through to COND case since this is now a COND. */
1297 /* First, check for degenerate COND. */
1298 if (XVECLEN (exp, 0) == 0)
1299 return make_canonical (attr, XEXP (exp, 1));
1300 defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1302 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1304 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1305 XVECEXP (exp, 0, i + 1)
1306 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1307 if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1323 copy_boolean (rtx exp)
1325 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1326 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1327 copy_boolean (XEXP (exp, 1)));
1328 if (GET_CODE (exp) == MATCH_OPERAND)
1330 XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1331 XSTR (exp, 2) = DEF_ATTR_STRING (XSTR (exp, 2));
1333 else if (GET_CODE (exp) == EQ_ATTR)
1335 XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1336 XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1342 /* Given a value and an attribute description, return a `struct attr_value *'
1343 that represents that value. This is either an existing structure, if the
1344 value has been previously encountered, or a newly-created structure.
1346 `insn_code' is the code of an insn whose attribute has the specified
1347 value (-2 if not processing an insn). We ensure that all insns for
1348 a given value have the same number of alternatives if the value checks
1351 static struct attr_value *
1352 get_attr_value (rtx value, struct attr_desc *attr, int insn_code)
1354 struct attr_value *av;
1357 value = make_canonical (attr, value);
1358 if (compares_alternatives_p (value))
1360 if (insn_code < 0 || insn_alternatives == NULL)
1361 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1363 num_alt = insn_alternatives[insn_code];
1366 for (av = attr->first_value; av; av = av->next)
1367 if (rtx_equal_p (value, av->value)
1368 && (num_alt == 0 || av->first_insn == NULL
1369 || insn_alternatives[av->first_insn->def->insn_code]))
1372 av = oballoc (sizeof (struct attr_value));
1374 av->next = attr->first_value;
1375 attr->first_value = av;
1376 av->first_insn = NULL;
1378 av->has_asm_insn = 0;
1383 /* After all DEFINE_DELAYs have been read in, create internal attributes
1384 to generate the required routines.
1386 First, we compute the number of delay slots for each insn (as a COND of
1387 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1388 delay type is specified, we compute a similar function giving the
1389 DEFINE_DELAY ordinal for each insn.
1391 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1392 tells whether a given insn can be in that delay slot.
1394 Normal attribute filling and optimization expands these to contain the
1395 information needed to handle delay slots. */
1398 expand_delays (void)
1400 struct delay_desc *delay;
1406 /* First, generate data for `num_delay_slots' function. */
1408 condexp = rtx_alloc (COND);
1409 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1410 XEXP (condexp, 1) = make_numeric_value (0);
1412 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1414 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1415 XVECEXP (condexp, 0, i + 1)
1416 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1419 make_internal_attr (num_delay_slots_str, condexp, ATTR_NONE);
1421 /* If more than one delay type, do the same for computing the delay type. */
1424 condexp = rtx_alloc (COND);
1425 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1426 XEXP (condexp, 1) = make_numeric_value (0);
1428 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1430 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1431 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1434 make_internal_attr (delay_type_str, condexp, ATTR_SPECIAL);
1437 /* For each delay possibility and delay slot, compute an eligibility
1438 attribute for non-annulled insns and for each type of annulled (annul
1439 if true and annul if false). */
1440 for (delay = delays; delay; delay = delay->next)
1442 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1444 condexp = XVECEXP (delay->def, 1, i);
1446 condexp = false_rtx;
1447 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1448 make_numeric_value (1), make_numeric_value (0));
1450 p = attr_printf (sizeof "*delay__" + MAX_DIGITS * 2,
1451 "*delay_%d_%d", delay->num, i / 3);
1452 make_internal_attr (p, newexp, ATTR_SPECIAL);
1454 if (have_annul_true)
1456 condexp = XVECEXP (delay->def, 1, i + 1);
1457 if (condexp == 0) condexp = false_rtx;
1458 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1459 make_numeric_value (1),
1460 make_numeric_value (0));
1461 p = attr_printf (sizeof "*annul_true__" + MAX_DIGITS * 2,
1462 "*annul_true_%d_%d", delay->num, i / 3);
1463 make_internal_attr (p, newexp, ATTR_SPECIAL);
1466 if (have_annul_false)
1468 condexp = XVECEXP (delay->def, 1, i + 2);
1469 if (condexp == 0) condexp = false_rtx;
1470 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1471 make_numeric_value (1),
1472 make_numeric_value (0));
1473 p = attr_printf (sizeof "*annul_false__" + MAX_DIGITS * 2,
1474 "*annul_false_%d_%d", delay->num, i / 3);
1475 make_internal_attr (p, newexp, ATTR_SPECIAL);
1481 /* Once all attributes and insns have been read and checked, we construct for
1482 each attribute value a list of all the insns that have that value for
1486 fill_attr (struct attr_desc *attr)
1488 struct attr_value *av;
1489 struct insn_ent *ie;
1490 struct insn_def *id;
1494 /* Don't fill constant attributes. The value is independent of
1495 any particular insn. */
1499 for (id = defs; id; id = id->next)
1501 /* If no value is specified for this insn for this attribute, use the
1504 if (XVEC (id->def, id->vec_idx))
1505 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1506 if (! strcmp_check (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
1508 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
1511 av = attr->default_val;
1513 av = get_attr_value (value, attr, id->insn_code);
1515 ie = oballoc (sizeof (struct insn_ent));
1517 insert_insn_ent (av, ie);
1521 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
1522 test that checks relative positions of insns (uses MATCH_DUP or PC).
1523 If so, replace it with what is obtained by passing the expression to
1524 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
1525 recursively on each value (including the default value). Otherwise,
1526 return the value returned by NO_ADDRESS_FN applied to EXP. */
1529 substitute_address (rtx exp, rtx (*no_address_fn) (rtx),
1530 rtx (*address_fn) (rtx))
1535 if (GET_CODE (exp) == COND)
1537 /* See if any tests use addresses. */
1539 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1540 walk_attr_value (XVECEXP (exp, 0, i));
1543 return (*address_fn) (exp);
1545 /* Make a new copy of this COND, replacing each element. */
1546 newexp = rtx_alloc (COND);
1547 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
1548 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1550 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
1551 XVECEXP (newexp, 0, i + 1)
1552 = substitute_address (XVECEXP (exp, 0, i + 1),
1553 no_address_fn, address_fn);
1556 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
1557 no_address_fn, address_fn);
1562 else if (GET_CODE (exp) == IF_THEN_ELSE)
1565 walk_attr_value (XEXP (exp, 0));
1567 return (*address_fn) (exp);
1569 return attr_rtx (IF_THEN_ELSE,
1570 substitute_address (XEXP (exp, 0),
1571 no_address_fn, address_fn),
1572 substitute_address (XEXP (exp, 1),
1573 no_address_fn, address_fn),
1574 substitute_address (XEXP (exp, 2),
1575 no_address_fn, address_fn));
1578 return (*no_address_fn) (exp);
1581 /* Make new attributes from the `length' attribute. The following are made,
1582 each corresponding to a function called from `shorten_branches' or
1585 *insn_default_length This is the length of the insn to be returned
1586 by `get_attr_length' before `shorten_branches'
1587 has been called. In each case where the length
1588 depends on relative addresses, the largest
1589 possible is used. This routine is also used
1590 to compute the initial size of the insn.
1592 *insn_variable_length_p This returns 1 if the insn's length depends
1593 on relative addresses, zero otherwise.
1595 *insn_current_length This is only called when it is known that the
1596 insn has a variable length and returns the
1597 current length, based on relative addresses.
1601 make_length_attrs (void)
1603 static const char *new_names[] =
1605 "*insn_default_length",
1606 "*insn_variable_length_p",
1607 "*insn_current_length"
1609 static rtx (*const no_address_fn[]) (rtx) = {identity_fn, zero_fn, zero_fn};
1610 static rtx (*const address_fn[]) (rtx) = {max_fn, one_fn, identity_fn};
1612 struct attr_desc *length_attr, *new_attr;
1613 struct attr_value *av, *new_av;
1614 struct insn_ent *ie, *new_ie;
1616 /* See if length attribute is defined. If so, it must be numeric. Make
1617 it special so we don't output anything for it. */
1618 length_attr = find_attr (&length_str, 0);
1619 if (length_attr == 0)
1622 if (! length_attr->is_numeric)
1623 fatal ("length attribute must be numeric");
1625 length_attr->is_const = 0;
1626 length_attr->is_special = 1;
1628 /* Make each new attribute, in turn. */
1629 for (i = 0; i < ARRAY_SIZE (new_names); i++)
1631 make_internal_attr (new_names[i],
1632 substitute_address (length_attr->default_val->value,
1633 no_address_fn[i], address_fn[i]),
1635 new_attr = find_attr (&new_names[i], 0);
1636 for (av = length_attr->first_value; av; av = av->next)
1637 for (ie = av->first_insn; ie; ie = ie->next)
1639 new_av = get_attr_value (substitute_address (av->value,
1642 new_attr, ie->def->insn_code);
1643 new_ie = oballoc (sizeof (struct insn_ent));
1644 new_ie->def = ie->def;
1645 insert_insn_ent (new_av, new_ie);
1650 /* Utility functions called from above routine. */
1653 identity_fn (rtx exp)
1659 zero_fn (rtx exp ATTRIBUTE_UNUSED)
1661 return make_numeric_value (0);
1665 one_fn (rtx exp ATTRIBUTE_UNUSED)
1667 return make_numeric_value (1);
1674 return make_numeric_value (max_attr_value (exp, &unknown));
1678 write_length_unit_log (void)
1680 struct attr_desc *length_attr = find_attr (&length_str, 0);
1681 struct attr_value *av;
1682 struct insn_ent *ie;
1683 unsigned int length_unit_log, length_or;
1686 if (length_attr == 0)
1688 length_or = or_attr_value (length_attr->default_val->value, &unknown);
1689 for (av = length_attr->first_value; av; av = av->next)
1690 for (ie = av->first_insn; ie; ie = ie->next)
1691 length_or |= or_attr_value (av->value, &unknown);
1694 length_unit_log = 0;
1697 length_or = ~length_or;
1698 for (length_unit_log = 0; length_or & 1; length_or >>= 1)
1701 printf ("int length_unit_log = %u;\n", length_unit_log);
1704 /* Take a COND expression and see if any of the conditions in it can be
1705 simplified. If any are known true or known false for the particular insn
1706 code, the COND can be further simplified.
1708 Also call ourselves on any COND operations that are values of this COND.
1710 We do not modify EXP; rather, we make and return a new rtx. */
1713 simplify_cond (rtx exp, int insn_code, int insn_index)
1716 /* We store the desired contents here,
1717 then build a new expression if they don't match EXP. */
1718 rtx defval = XEXP (exp, 1);
1719 rtx new_defval = XEXP (exp, 1);
1720 int len = XVECLEN (exp, 0);
1721 rtx *tests = xmalloc (len * sizeof (rtx));
1725 /* This lets us free all storage allocated below, if appropriate. */
1726 obstack_finish (rtl_obstack);
1728 memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
1730 /* See if default value needs simplification. */
1731 if (GET_CODE (defval) == COND)
1732 new_defval = simplify_cond (defval, insn_code, insn_index);
1734 /* Simplify the subexpressions, and see what tests we can get rid of. */
1736 for (i = 0; i < len; i += 2)
1738 rtx newtest, newval;
1740 /* Simplify this test. */
1741 newtest = simplify_test_exp_in_temp (tests[i], insn_code, insn_index);
1744 newval = tests[i + 1];
1745 /* See if this value may need simplification. */
1746 if (GET_CODE (newval) == COND)
1747 newval = simplify_cond (newval, insn_code, insn_index);
1749 /* Look for ways to delete or combine this test. */
1750 if (newtest == true_rtx)
1752 /* If test is true, make this value the default
1753 and discard this + any following tests. */
1755 defval = tests[i + 1];
1756 new_defval = newval;
1759 else if (newtest == false_rtx)
1761 /* If test is false, discard it and its value. */
1762 for (j = i; j < len - 2; j++)
1763 tests[j] = tests[j + 2];
1768 else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
1770 /* If this value and the value for the prev test are the same,
1774 = insert_right_side (IOR, tests[i - 2], newtest,
1775 insn_code, insn_index);
1777 /* Delete this test/value. */
1778 for (j = i; j < len - 2; j++)
1779 tests[j] = tests[j + 2];
1785 tests[i + 1] = newval;
1788 /* If the last test in a COND has the same value
1789 as the default value, that test isn't needed. */
1791 while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
1794 /* See if we changed anything. */
1795 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
1798 for (i = 0; i < len; i++)
1799 if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
1807 if (GET_CODE (defval) == COND)
1808 ret = simplify_cond (defval, insn_code, insn_index);
1816 rtx newexp = rtx_alloc (COND);
1818 XVEC (newexp, 0) = rtvec_alloc (len);
1819 memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
1820 XEXP (newexp, 1) = new_defval;
1827 /* Remove an insn entry from an attribute value. */
1830 remove_insn_ent (struct attr_value *av, struct insn_ent *ie)
1832 struct insn_ent *previe;
1834 if (av->first_insn == ie)
1835 av->first_insn = ie->next;
1838 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
1840 previe->next = ie->next;
1844 if (ie->def->insn_code == -1)
1845 av->has_asm_insn = 0;
1850 /* Insert an insn entry in an attribute value list. */
1853 insert_insn_ent (struct attr_value *av, struct insn_ent *ie)
1855 ie->next = av->first_insn;
1856 av->first_insn = ie;
1858 if (ie->def->insn_code == -1)
1859 av->has_asm_insn = 1;
1864 /* This is a utility routine to take an expression that is a tree of either
1865 AND or IOR expressions and insert a new term. The new term will be
1866 inserted at the right side of the first node whose code does not match
1867 the root. A new node will be created with the root's code. Its left
1868 side will be the old right side and its right side will be the new
1871 If the `term' is itself a tree, all its leaves will be inserted. */
1874 insert_right_side (enum rtx_code code, rtx exp, rtx term, int insn_code, int insn_index)
1878 /* Avoid consing in some special cases. */
1879 if (code == AND && term == true_rtx)
1881 if (code == AND && term == false_rtx)
1883 if (code == AND && exp == true_rtx)
1885 if (code == AND && exp == false_rtx)
1887 if (code == IOR && term == true_rtx)
1889 if (code == IOR && term == false_rtx)
1891 if (code == IOR && exp == true_rtx)
1893 if (code == IOR && exp == false_rtx)
1895 if (attr_equal_p (exp, term))
1898 if (GET_CODE (term) == code)
1900 exp = insert_right_side (code, exp, XEXP (term, 0),
1901 insn_code, insn_index);
1902 exp = insert_right_side (code, exp, XEXP (term, 1),
1903 insn_code, insn_index);
1908 if (GET_CODE (exp) == code)
1910 rtx new = insert_right_side (code, XEXP (exp, 1),
1911 term, insn_code, insn_index);
1912 if (new != XEXP (exp, 1))
1913 /* Make a copy of this expression and call recursively. */
1914 newexp = attr_rtx (code, XEXP (exp, 0), new);
1920 /* Insert the new term. */
1921 newexp = attr_rtx (code, exp, term);
1924 return simplify_test_exp_in_temp (newexp, insn_code, insn_index);
1927 /* If we have an expression which AND's a bunch of
1928 (not (eq_attrq "alternative" "n"))
1929 terms, we may have covered all or all but one of the possible alternatives.
1930 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
1932 This routine is passed an expression and either AND or IOR. It returns a
1933 bitmask indicating which alternatives are mentioned within EXP. */
1936 compute_alternative_mask (rtx exp, enum rtx_code code)
1939 if (GET_CODE (exp) == code)
1940 return compute_alternative_mask (XEXP (exp, 0), code)
1941 | compute_alternative_mask (XEXP (exp, 1), code);
1943 else if (code == AND && GET_CODE (exp) == NOT
1944 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
1945 && XSTR (XEXP (exp, 0), 0) == alternative_name)
1946 string = XSTR (XEXP (exp, 0), 1);
1948 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
1949 && XSTR (exp, 0) == alternative_name)
1950 string = XSTR (exp, 1);
1952 else if (GET_CODE (exp) == EQ_ATTR_ALT)
1954 if (code == AND && XINT (exp, 1))
1955 return XINT (exp, 0);
1957 if (code == IOR && !XINT (exp, 1))
1958 return XINT (exp, 0);
1966 return 1 << (string[0] - '0');
1967 return 1 << atoi (string);
1970 /* Given I, a single-bit mask, return RTX to compare the `alternative'
1971 attribute with the value represented by that bit. */
1974 make_alternative_compare (int mask)
1976 return mk_attr_alt (mask);
1979 /* If we are processing an (eq_attr "attr" "value") test, we find the value
1980 of "attr" for this insn code. From that value, we can compute a test
1981 showing when the EQ_ATTR will be true. This routine performs that
1982 computation. If a test condition involves an address, we leave the EQ_ATTR
1983 intact because addresses are only valid for the `length' attribute.
1985 EXP is the EQ_ATTR expression and VALUE is the value of that attribute
1986 for the insn corresponding to INSN_CODE and INSN_INDEX. */
1989 evaluate_eq_attr (rtx exp, rtx value, int insn_code, int insn_index)
1996 switch (GET_CODE (value))
1999 if (! strcmp_check (XSTR (value, 0), XSTR (exp, 1)))
2010 gcc_assert (GET_CODE (exp) == EQ_ATTR);
2011 gcc_assert (strlen (XSTR (exp, 0)) + strlen (XSTR (exp, 1)) + 2
2014 strcpy (string, XSTR (exp, 0));
2015 strcat (string, "_");
2016 strcat (string, XSTR (exp, 1));
2017 for (p = string; *p; p++)
2020 newexp = attr_rtx (EQ, value,
2021 attr_rtx (SYMBOL_REF,
2022 DEF_ATTR_STRING (string)));
2027 /* We construct an IOR of all the cases for which the
2028 requested attribute value is present. Since we start with
2029 FALSE, if it is not present, FALSE will be returned.
2031 Each case is the AND of the NOT's of the previous conditions with the
2032 current condition; in the default case the current condition is TRUE.
2034 For each possible COND value, call ourselves recursively.
2036 The extra TRUE and FALSE expressions will be eliminated by another
2037 call to the simplification routine. */
2042 if (current_alternative_string)
2043 clear_struct_flag (value);
2045 for (i = 0; i < XVECLEN (value, 0); i += 2)
2047 rtx this = simplify_test_exp_in_temp (XVECEXP (value, 0, i),
2048 insn_code, insn_index);
2050 SIMPLIFY_ALTERNATIVE (this);
2052 right = insert_right_side (AND, andexp, this,
2053 insn_code, insn_index);
2054 right = insert_right_side (AND, right,
2055 evaluate_eq_attr (exp,
2058 insn_code, insn_index),
2059 insn_code, insn_index);
2060 orexp = insert_right_side (IOR, orexp, right,
2061 insn_code, insn_index);
2063 /* Add this condition into the AND expression. */
2064 newexp = attr_rtx (NOT, this);
2065 andexp = insert_right_side (AND, andexp, newexp,
2066 insn_code, insn_index);
2069 /* Handle the default case. */
2070 right = insert_right_side (AND, andexp,
2071 evaluate_eq_attr (exp, XEXP (value, 1),
2072 insn_code, insn_index),
2073 insn_code, insn_index);
2074 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2081 /* If uses an address, must return original expression. But set the
2082 ATTR_IND_SIMPLIFIED_P bit so we don't try to simplify it again. */
2085 walk_attr_value (newexp);
2089 /* This had `&& current_alternative_string', which seems to be wrong. */
2090 if (! ATTR_IND_SIMPLIFIED_P (exp))
2091 return copy_rtx_unchanging (exp);
2098 /* This routine is called when an AND of a term with a tree of AND's is
2099 encountered. If the term or its complement is present in the tree, it
2100 can be replaced with TRUE or FALSE, respectively.
2102 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2103 be true and hence are complementary.
2105 There is one special case: If we see
2106 (and (not (eq_attr "att" "v1"))
2107 (eq_attr "att" "v2"))
2108 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2109 replace the term, not anything in the AND tree. So we pass a pointer to
2113 simplify_and_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2118 int left_eliminates_term, right_eliminates_term;
2120 if (GET_CODE (exp) == AND)
2122 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2123 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2124 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2126 newexp = attr_rtx (AND, left, right);
2128 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2132 else if (GET_CODE (exp) == IOR)
2134 /* For the IOR case, we do the same as above, except that we can
2135 only eliminate `term' if both sides of the IOR would do so. */
2137 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2138 left_eliminates_term = (temp == true_rtx);
2141 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2142 right_eliminates_term = (temp == true_rtx);
2144 if (left_eliminates_term && right_eliminates_term)
2147 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2149 newexp = attr_rtx (IOR, left, right);
2151 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2155 /* Check for simplifications. Do some extra checking here since this
2156 routine is called so many times. */
2161 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2164 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2167 else if (GET_CODE (exp) == EQ_ATTR_ALT && GET_CODE (*pterm) == EQ_ATTR_ALT)
2169 if (attr_alt_subset_p (*pterm, exp))
2172 if (attr_alt_subset_of_compl_p (*pterm, exp))
2175 if (attr_alt_subset_p (exp, *pterm))
2181 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2183 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2186 if (! strcmp_check (XSTR (exp, 1), XSTR (*pterm, 1)))
2192 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2193 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2195 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2198 if (! strcmp_check (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2204 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2205 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2207 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2210 if (! strcmp_check (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2216 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2218 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2222 else if (GET_CODE (exp) == NOT)
2224 if (attr_equal_p (XEXP (exp, 0), *pterm))
2228 else if (GET_CODE (*pterm) == NOT)
2230 if (attr_equal_p (XEXP (*pterm, 0), exp))
2234 else if (attr_equal_p (exp, *pterm))
2240 /* Similar to `simplify_and_tree', but for IOR trees. */
2243 simplify_or_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2248 int left_eliminates_term, right_eliminates_term;
2250 if (GET_CODE (exp) == IOR)
2252 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2253 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2254 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2256 newexp = attr_rtx (GET_CODE (exp), left, right);
2258 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2262 else if (GET_CODE (exp) == AND)
2264 /* For the AND case, we do the same as above, except that we can
2265 only eliminate `term' if both sides of the AND would do so. */
2267 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2268 left_eliminates_term = (temp == false_rtx);
2271 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2272 right_eliminates_term = (temp == false_rtx);
2274 if (left_eliminates_term && right_eliminates_term)
2277 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2279 newexp = attr_rtx (GET_CODE (exp), left, right);
2281 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2285 if (attr_equal_p (exp, *pterm))
2288 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
2291 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
2294 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2295 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2296 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
2299 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2300 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
2301 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
2307 /* Compute approximate cost of the expression. Used to decide whether
2308 expression is cheap enough for inline. */
2310 attr_rtx_cost (rtx x)
2316 code = GET_CODE (x);
2329 /* Alternatives don't result into function call. */
2330 if (!strcmp_check (XSTR (x, 0), alternative_name))
2337 const char *fmt = GET_RTX_FORMAT (code);
2338 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2344 for (j = 0; j < XVECLEN (x, i); j++)
2345 cost += attr_rtx_cost (XVECEXP (x, i, j));
2348 cost += attr_rtx_cost (XEXP (x, i));
2358 /* Simplify test expression and use temporary obstack in order to avoid
2359 memory bloat. Use ATTR_IND_SIMPLIFIED to avoid unnecessary simplifications
2360 and avoid unnecessary copying if possible. */
2363 simplify_test_exp_in_temp (rtx exp, int insn_code, int insn_index)
2366 struct obstack *old;
2367 if (ATTR_IND_SIMPLIFIED_P (exp))
2370 rtl_obstack = temp_obstack;
2371 x = simplify_test_exp (exp, insn_code, insn_index);
2373 if (x == exp || rtl_obstack == temp_obstack)
2375 return attr_copy_rtx (x);
2378 /* Returns true if S1 is a subset of S2. */
2381 attr_alt_subset_p (rtx s1, rtx s2)
2383 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2386 return !(XINT (s1, 0) &~ XINT (s2, 0));
2389 return !(XINT (s1, 0) & XINT (s2, 0));
2395 return !(XINT (s2, 0) &~ XINT (s1, 0));
2402 /* Returns true if S1 is a subset of complement of S2. */
2405 attr_alt_subset_of_compl_p (rtx s1, rtx s2)
2407 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2410 return !(XINT (s1, 0) & XINT (s2, 0));
2413 return !(XINT (s1, 0) & ~XINT (s2, 0));
2416 return !(XINT (s2, 0) &~ XINT (s1, 0));
2426 /* Return EQ_ATTR_ALT expression representing intersection of S1 and S2. */
2429 attr_alt_intersection (rtx s1, rtx s2)
2431 rtx result = rtx_alloc (EQ_ATTR_ALT);
2433 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2436 XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
2439 XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
2442 XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
2445 XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
2450 XINT (result, 1) = XINT (s1, 1) & XINT (s2, 1);
2455 /* Return EQ_ATTR_ALT expression representing union of S1 and S2. */
2458 attr_alt_union (rtx s1, rtx s2)
2460 rtx result = rtx_alloc (EQ_ATTR_ALT);
2462 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2465 XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
2468 XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
2471 XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
2474 XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
2480 XINT (result, 1) = XINT (s1, 1) | XINT (s2, 1);
2484 /* Return EQ_ATTR_ALT expression representing complement of S. */
2487 attr_alt_complement (rtx s)
2489 rtx result = rtx_alloc (EQ_ATTR_ALT);
2491 XINT (result, 0) = XINT (s, 0);
2492 XINT (result, 1) = 1 - XINT (s, 1);
2497 /* Tests whether a bit B belongs to the set represented by S. */
2500 attr_alt_bit_p (rtx s, int b)
2502 return XINT (s, 1) ^ ((XINT (s, 0) >> b) & 1);
2505 /* Return EQ_ATTR_ALT expression representing set containing elements set
2511 rtx result = rtx_alloc (EQ_ATTR_ALT);
2513 XINT (result, 0) = e;
2514 XINT (result, 1) = 0;
2519 /* Given an expression, see if it can be simplified for a particular insn
2520 code based on the values of other attributes being tested. This can
2521 eliminate nested get_attr_... calls.
2523 Note that if an endless recursion is specified in the patterns, the
2524 optimization will loop. However, it will do so in precisely the cases where
2525 an infinite recursion loop could occur during compilation. It's better that
2529 simplify_test_exp (rtx exp, int insn_code, int insn_index)
2532 struct attr_desc *attr;
2533 struct attr_value *av;
2534 struct insn_ent *ie;
2537 bool left_alt, right_alt;
2539 /* Don't re-simplify something we already simplified. */
2540 if (ATTR_IND_SIMPLIFIED_P (exp) || ATTR_CURR_SIMPLIFIED_P (exp))
2543 switch (GET_CODE (exp))
2546 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2547 SIMPLIFY_ALTERNATIVE (left);
2548 if (left == false_rtx)
2550 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2551 SIMPLIFY_ALTERNATIVE (right);
2552 if (left == false_rtx)
2555 if (GET_CODE (left) == EQ_ATTR_ALT
2556 && GET_CODE (right) == EQ_ATTR_ALT)
2558 exp = attr_alt_intersection (left, right);
2559 return simplify_test_exp (exp, insn_code, insn_index);
2562 /* If either side is an IOR and we have (eq_attr "alternative" ..")
2563 present on both sides, apply the distributive law since this will
2564 yield simplifications. */
2565 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
2566 && compute_alternative_mask (left, IOR)
2567 && compute_alternative_mask (right, IOR))
2569 if (GET_CODE (left) == IOR)
2576 newexp = attr_rtx (IOR,
2577 attr_rtx (AND, left, XEXP (right, 0)),
2578 attr_rtx (AND, left, XEXP (right, 1)));
2580 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2583 /* Try with the term on both sides. */
2584 right = simplify_and_tree (right, &left, insn_code, insn_index);
2585 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2586 left = simplify_and_tree (left, &right, insn_code, insn_index);
2588 if (left == false_rtx || right == false_rtx)
2590 else if (left == true_rtx)
2594 else if (right == true_rtx)
2598 /* See if all or all but one of the insn's alternatives are specified
2599 in this tree. Optimize if so. */
2601 if (GET_CODE (left) == NOT)
2602 left_alt = (GET_CODE (XEXP (left, 0)) == EQ_ATTR
2603 && XSTR (XEXP (left, 0), 0) == alternative_name);
2605 left_alt = (GET_CODE (left) == EQ_ATTR_ALT
2608 if (GET_CODE (right) == NOT)
2609 right_alt = (GET_CODE (XEXP (right, 0)) == EQ_ATTR
2610 && XSTR (XEXP (right, 0), 0) == alternative_name);
2612 right_alt = (GET_CODE (right) == EQ_ATTR_ALT
2613 && XINT (right, 1));
2616 && (GET_CODE (left) == AND
2618 || GET_CODE (right) == AND
2621 i = compute_alternative_mask (exp, AND);
2622 if (i & ~insn_alternatives[insn_code])
2623 fatal ("invalid alternative specified for pattern number %d",
2626 /* If all alternatives are excluded, this is false. */
2627 i ^= insn_alternatives[insn_code];
2630 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2632 /* If just one excluded, AND a comparison with that one to the
2633 front of the tree. The others will be eliminated by
2634 optimization. We do not want to do this if the insn has one
2635 alternative and we have tested none of them! */
2636 left = make_alternative_compare (i);
2637 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2638 newexp = attr_rtx (AND, left, right);
2640 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2644 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2646 newexp = attr_rtx (AND, left, right);
2647 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2652 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2653 SIMPLIFY_ALTERNATIVE (left);
2654 if (left == true_rtx)
2656 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2657 SIMPLIFY_ALTERNATIVE (right);
2658 if (right == true_rtx)
2661 if (GET_CODE (left) == EQ_ATTR_ALT
2662 && GET_CODE (right) == EQ_ATTR_ALT)
2664 exp = attr_alt_union (left, right);
2665 return simplify_test_exp (exp, insn_code, insn_index);
2668 right = simplify_or_tree (right, &left, insn_code, insn_index);
2669 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2670 left = simplify_or_tree (left, &right, insn_code, insn_index);
2672 if (right == true_rtx || left == true_rtx)
2674 else if (left == false_rtx)
2678 else if (right == false_rtx)
2683 /* Test for simple cases where the distributive law is useful. I.e.,
2684 convert (ior (and (x) (y))
2690 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
2691 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
2693 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
2695 left = XEXP (left, 0);
2697 newexp = attr_rtx (AND, left, right);
2698 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2701 /* See if all or all but one of the insn's alternatives are specified
2702 in this tree. Optimize if so. */
2704 else if (insn_code >= 0
2705 && (GET_CODE (left) == IOR
2706 || (GET_CODE (left) == EQ_ATTR_ALT
2708 || (GET_CODE (left) == EQ_ATTR
2709 && XSTR (left, 0) == alternative_name)
2710 || GET_CODE (right) == IOR
2711 || (GET_CODE (right) == EQ_ATTR_ALT
2712 && !XINT (right, 1))
2713 || (GET_CODE (right) == EQ_ATTR
2714 && XSTR (right, 0) == alternative_name)))
2716 i = compute_alternative_mask (exp, IOR);
2717 if (i & ~insn_alternatives[insn_code])
2718 fatal ("invalid alternative specified for pattern number %d",
2721 /* If all alternatives are included, this is true. */
2722 i ^= insn_alternatives[insn_code];
2725 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2727 /* If just one excluded, IOR a comparison with that one to the
2728 front of the tree. The others will be eliminated by
2729 optimization. We do not want to do this if the insn has one
2730 alternative and we have tested none of them! */
2731 left = make_alternative_compare (i);
2732 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2733 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
2735 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2739 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2741 newexp = attr_rtx (IOR, left, right);
2742 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2747 if (GET_CODE (XEXP (exp, 0)) == NOT)
2749 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
2750 insn_code, insn_index);
2751 SIMPLIFY_ALTERNATIVE (left);
2755 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2756 SIMPLIFY_ALTERNATIVE (left);
2757 if (GET_CODE (left) == NOT)
2758 return XEXP (left, 0);
2760 if (left == false_rtx)
2762 if (left == true_rtx)
2765 if (GET_CODE (left) == EQ_ATTR_ALT)
2767 exp = attr_alt_complement (left);
2768 return simplify_test_exp (exp, insn_code, insn_index);
2771 /* Try to apply De`Morgan's laws. */
2772 if (GET_CODE (left) == IOR)
2774 newexp = attr_rtx (AND,
2775 attr_rtx (NOT, XEXP (left, 0)),
2776 attr_rtx (NOT, XEXP (left, 1)));
2778 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2780 else if (GET_CODE (left) == AND)
2782 newexp = attr_rtx (IOR,
2783 attr_rtx (NOT, XEXP (left, 0)),
2784 attr_rtx (NOT, XEXP (left, 1)));
2786 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2788 else if (left != XEXP (exp, 0))
2790 newexp = attr_rtx (NOT, left);
2795 if (current_alternative_string)
2796 return attr_alt_bit_p (exp, atoi (current_alternative_string)) ? true_rtx : false_rtx;
2799 return XINT (exp, 1) ? true_rtx : false_rtx;
2803 if (current_alternative_string && XSTR (exp, 0) == alternative_name)
2804 return (XSTR (exp, 1) == current_alternative_string
2805 ? true_rtx : false_rtx);
2807 if (XSTR (exp, 0) == alternative_name)
2809 newexp = mk_attr_alt (1 << atoi (XSTR (exp, 1)));
2813 /* Look at the value for this insn code in the specified attribute.
2814 We normally can replace this comparison with the condition that
2815 would give this insn the values being tested for. */
2816 if (XSTR (exp, 0) != alternative_name
2817 && (attr = find_attr (&XSTR (exp, 0), 0)) != NULL)
2818 for (av = attr->first_value; av; av = av->next)
2819 for (ie = av->first_insn; ie; ie = ie->next)
2820 if (ie->def->insn_code == insn_code)
2823 x = evaluate_eq_attr (exp, av->value, insn_code, insn_index);
2824 x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
2825 if (attr_rtx_cost(x) < 20)
2834 /* We have already simplified this expression. Simplifying it again
2835 won't buy anything unless we weren't given a valid insn code
2836 to process (i.e., we are canonicalizing something.). */
2837 if (insn_code != -2 /* Seems wrong: && current_alternative_string. */
2838 && ! ATTR_IND_SIMPLIFIED_P (newexp))
2839 return copy_rtx_unchanging (newexp);
2844 /* Optimize the attribute lists by seeing if we can determine conditional
2845 values from the known values of other attributes. This will save subroutine
2846 calls during the compilation. */
2849 optimize_attrs (void)
2851 struct attr_desc *attr;
2852 struct attr_value *av;
2853 struct insn_ent *ie;
2856 struct attr_value_list
2858 struct attr_value *av;
2859 struct insn_ent *ie;
2860 struct attr_desc *attr;
2861 struct attr_value_list *next;
2863 struct attr_value_list **insn_code_values;
2864 struct attr_value_list *ivbuf;
2865 struct attr_value_list *iv;
2867 /* For each insn code, make a list of all the insn_ent's for it,
2868 for all values for all attributes. */
2870 if (num_insn_ents == 0)
2873 /* Make 2 extra elements, for "code" values -2 and -1. */
2874 insn_code_values = xcalloc ((insn_code_number + 2),
2875 sizeof (struct attr_value_list *));
2877 /* Offset the table address so we can index by -2 or -1. */
2878 insn_code_values += 2;
2880 iv = ivbuf = xmalloc (num_insn_ents * sizeof (struct attr_value_list));
2882 for (i = 0; i < MAX_ATTRS_INDEX; i++)
2883 for (attr = attrs[i]; attr; attr = attr->next)
2884 for (av = attr->first_value; av; av = av->next)
2885 for (ie = av->first_insn; ie; ie = ie->next)
2890 iv->next = insn_code_values[ie->def->insn_code];
2891 insn_code_values[ie->def->insn_code] = iv;
2895 /* Sanity check on num_insn_ents. */
2896 gcc_assert (iv == ivbuf + num_insn_ents);
2898 /* Process one insn code at a time. */
2899 for (i = -2; i < insn_code_number; i++)
2901 /* Clear the ATTR_CURR_SIMPLIFIED_P flag everywhere relevant.
2902 We use it to mean "already simplified for this insn". */
2903 for (iv = insn_code_values[i]; iv; iv = iv->next)
2904 clear_struct_flag (iv->av->value);
2906 for (iv = insn_code_values[i]; iv; iv = iv->next)
2908 struct obstack *old = rtl_obstack;
2913 if (GET_CODE (av->value) != COND)
2916 rtl_obstack = temp_obstack;
2918 while (GET_CODE (newexp) == COND)
2920 rtx newexp2 = simplify_cond (newexp, ie->def->insn_code,
2921 ie->def->insn_index);
2922 if (newexp2 == newexp)
2928 if (newexp != av->value)
2930 newexp = attr_copy_rtx (newexp);
2931 remove_insn_ent (av, ie);
2932 av = get_attr_value (newexp, attr, ie->def->insn_code);
2934 insert_insn_ent (av, ie);
2940 free (insn_code_values - 2);
2943 /* Clear the ATTR_CURR_SIMPLIFIED_P flag in EXP and its subexpressions. */
2946 clear_struct_flag (rtx x)
2953 ATTR_CURR_SIMPLIFIED_P (x) = 0;
2954 if (ATTR_IND_SIMPLIFIED_P (x))
2957 code = GET_CODE (x);
2977 /* Compare the elements. If any pair of corresponding elements
2978 fail to match, return 0 for the whole things. */
2980 fmt = GET_RTX_FORMAT (code);
2981 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2987 for (j = 0; j < XVECLEN (x, i); j++)
2988 clear_struct_flag (XVECEXP (x, i, j));
2992 clear_struct_flag (XEXP (x, i));
2998 /* Create table entries for DEFINE_ATTR. */
3001 gen_attr (rtx exp, int lineno)
3003 struct attr_desc *attr;
3004 struct attr_value *av;
3005 const char *name_ptr;
3008 /* Make a new attribute structure. Check for duplicate by looking at
3009 attr->default_val, since it is initialized by this routine. */
3010 attr = find_attr (&XSTR (exp, 0), 1);
3011 if (attr->default_val)
3013 message_with_line (lineno, "duplicate definition for attribute %s",
3015 message_with_line (attr->lineno, "previous definition");
3019 attr->lineno = lineno;
3021 if (*XSTR (exp, 1) == '\0')
3022 attr->is_numeric = 1;
3025 name_ptr = XSTR (exp, 1);
3026 while ((p = next_comma_elt (&name_ptr)) != NULL)
3028 av = oballoc (sizeof (struct attr_value));
3029 av->value = attr_rtx (CONST_STRING, p);
3030 av->next = attr->first_value;
3031 attr->first_value = av;
3032 av->first_insn = NULL;
3034 av->has_asm_insn = 0;
3038 if (GET_CODE (XEXP (exp, 2)) == CONST)
3041 if (attr->is_numeric)
3043 message_with_line (lineno,
3044 "constant attributes may not take numeric values");
3048 /* Get rid of the CONST node. It is allowed only at top-level. */
3049 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
3052 if (! strcmp_check (attr->name, length_str) && ! attr->is_numeric)
3054 message_with_line (lineno,
3055 "`length' attribute must take numeric values");
3059 /* Set up the default value. */
3060 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
3061 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
3064 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
3065 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
3066 number of alternatives as this should be checked elsewhere. */
3069 count_alternatives (rtx exp)
3074 if (GET_CODE (exp) == MATCH_OPERAND)
3075 return n_comma_elts (XSTR (exp, 2));
3077 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3078 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3083 n = count_alternatives (XEXP (exp, i));
3090 if (XVEC (exp, i) != NULL)
3091 for (j = 0; j < XVECLEN (exp, i); j++)
3093 n = count_alternatives (XVECEXP (exp, i, j));
3102 /* Returns nonzero if the given expression contains an EQ_ATTR with the
3103 `alternative' attribute. */
3106 compares_alternatives_p (rtx exp)
3111 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
3114 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3115 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3120 if (compares_alternatives_p (XEXP (exp, i)))
3125 for (j = 0; j < XVECLEN (exp, i); j++)
3126 if (compares_alternatives_p (XVECEXP (exp, i, j)))
3134 /* Returns nonzero is INNER is contained in EXP. */
3137 contained_in_p (rtx inner, rtx exp)
3142 if (rtx_equal_p (inner, exp))
3145 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3146 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3151 if (contained_in_p (inner, XEXP (exp, i)))
3156 for (j = 0; j < XVECLEN (exp, i); j++)
3157 if (contained_in_p (inner, XVECEXP (exp, i, j)))
3165 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
3168 gen_insn (rtx exp, int lineno)
3170 struct insn_def *id;
3172 id = oballoc (sizeof (struct insn_def));
3176 id->lineno = lineno;
3178 switch (GET_CODE (exp))
3181 id->insn_code = insn_code_number;
3182 id->insn_index = insn_index_number;
3183 id->num_alternatives = count_alternatives (exp);
3184 if (id->num_alternatives == 0)
3185 id->num_alternatives = 1;
3189 case DEFINE_PEEPHOLE:
3190 id->insn_code = insn_code_number;
3191 id->insn_index = insn_index_number;
3192 id->num_alternatives = count_alternatives (exp);
3193 if (id->num_alternatives == 0)
3194 id->num_alternatives = 1;
3198 case DEFINE_ASM_ATTRIBUTES:
3200 id->insn_index = -1;
3201 id->num_alternatives = 1;
3203 got_define_asm_attributes = 1;
3211 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
3212 true or annul false is specified, and make a `struct delay_desc'. */
3215 gen_delay (rtx def, int lineno)
3217 struct delay_desc *delay;
3220 if (XVECLEN (def, 1) % 3 != 0)
3222 message_with_line (lineno,
3223 "number of elements in DEFINE_DELAY must be multiple of three");
3228 for (i = 0; i < XVECLEN (def, 1); i += 3)
3230 if (XVECEXP (def, 1, i + 1))
3231 have_annul_true = 1;
3232 if (XVECEXP (def, 1, i + 2))
3233 have_annul_false = 1;
3236 delay = oballoc (sizeof (struct delay_desc));
3238 delay->num = ++num_delays;
3239 delay->next = delays;
3240 delay->lineno = lineno;
3244 /* Given a piece of RTX, print a C expression to test its truth value.
3245 We use AND and IOR both for logical and bit-wise operations, so
3246 interpret them as logical unless they are inside a comparison expression.
3247 The first bit of FLAGS will be nonzero in that case.
3249 Set the second bit of FLAGS to make references to attribute values use
3250 a cached local variable instead of calling a function. */
3253 write_test_expr (rtx exp, int flags)
3255 int comparison_operator = 0;
3257 struct attr_desc *attr;
3259 /* In order not to worry about operator precedence, surround our part of
3260 the expression with parentheses. */
3263 code = GET_CODE (exp);
3266 /* Binary operators. */
3269 printf ("(unsigned) ");
3275 comparison_operator = 1;
3277 case PLUS: case MINUS: case MULT: case DIV: case MOD:
3278 case AND: case IOR: case XOR:
3279 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3280 write_test_expr (XEXP (exp, 0), flags | comparison_operator);
3296 printf (" >= (unsigned) ");
3299 printf (" > (unsigned) ");
3308 printf (" <= (unsigned) ");
3311 printf (" < (unsigned) ");
3354 write_test_expr (XEXP (exp, 1), flags | comparison_operator);
3358 /* Special-case (not (eq_attrq "alternative" "x")) */
3359 if (! (flags & 1) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3360 && XSTR (XEXP (exp, 0), 0) == alternative_name)
3362 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
3366 /* Otherwise, fall through to normal unary operator. */
3368 /* Unary operators. */
3388 write_test_expr (XEXP (exp, 0), flags);
3393 int set = XINT (exp, 0), bit = 0;
3396 fatal ("EQ_ATTR_ALT not valid inside comparison");
3399 fatal ("Empty EQ_ATTR_ALT should be optimized out");
3401 if (!(set & (set - 1)))
3403 if (!(set & 0xffff))
3426 printf ("which_alternative %s= %d",
3427 XINT (exp, 1) ? "!" : "=", bit);
3431 printf ("%s((1 << which_alternative) & 0x%x)",
3432 XINT (exp, 1) ? "!" : "", set);
3437 /* Comparison test of an attribute with a value. Most of these will
3438 have been removed by optimization. Handle "alternative"
3439 specially and give error if EQ_ATTR present inside a comparison. */
3442 fatal ("EQ_ATTR not valid inside comparison");
3444 if (XSTR (exp, 0) == alternative_name)
3446 printf ("which_alternative == %s", XSTR (exp, 1));
3450 attr = find_attr (&XSTR (exp, 0), 0);
3453 /* Now is the time to expand the value of a constant attribute. */
3456 write_test_expr (evaluate_eq_attr (exp, attr->default_val->value,
3463 printf ("attr_%s", attr->name);
3465 printf ("get_attr_%s (insn)", attr->name);
3467 write_attr_valueq (attr, XSTR (exp, 1));
3471 /* Comparison test of flags for define_delays. */
3474 fatal ("ATTR_FLAG not valid inside comparison");
3475 printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
3478 /* See if an operand matches a predicate. */
3480 /* If only a mode is given, just ensure the mode matches the operand.
3481 If neither a mode nor predicate is given, error. */
3482 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
3484 if (GET_MODE (exp) == VOIDmode)
3485 fatal ("null MATCH_OPERAND specified as test");
3487 printf ("GET_MODE (operands[%d]) == %smode",
3488 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3491 printf ("%s (operands[%d], %smode)",
3492 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3495 /* Constant integer. */
3497 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
3500 /* A random C expression. */
3502 print_c_condition (XSTR (exp, 0));
3505 /* The address of the branch target. */
3507 printf ("INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
3508 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
3512 /* The address of the current insn. We implement this actually as the
3513 address of the current insn for backward branches, but the last
3514 address of the next insn for forward branches, and both with
3515 adjustments that account for the worst-case possible stretching of
3516 intervening alignments between this insn and its destination. */
3517 printf ("insn_current_reference_address (insn)");
3521 printf ("%s", XSTR (exp, 0));
3525 write_test_expr (XEXP (exp, 0), flags & 2);
3527 write_test_expr (XEXP (exp, 1), flags | 1);
3529 write_test_expr (XEXP (exp, 2), flags | 1);
3533 fatal ("bad RTX code `%s' in attribute calculation\n",
3534 GET_RTX_NAME (code));
3540 /* Given an attribute value, return the maximum CONST_STRING argument
3541 encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
3544 max_attr_value (rtx exp, int *unknownp)
3549 switch (GET_CODE (exp))
3552 current_max = atoi (XSTR (exp, 0));
3556 current_max = max_attr_value (XEXP (exp, 1), unknownp);
3557 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3559 n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3560 if (n > current_max)
3566 current_max = max_attr_value (XEXP (exp, 1), unknownp);
3567 n = max_attr_value (XEXP (exp, 2), unknownp);
3568 if (n > current_max)
3574 current_max = INT_MAX;
3581 /* Given an attribute value, return the result of ORing together all
3582 CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
3583 if the numeric value is not known. */
3586 or_attr_value (rtx exp, int *unknownp)
3591 switch (GET_CODE (exp))
3594 current_or = atoi (XSTR (exp, 0));
3598 current_or = or_attr_value (XEXP (exp, 1), unknownp);
3599 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3600 current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3604 current_or = or_attr_value (XEXP (exp, 1), unknownp);
3605 current_or |= or_attr_value (XEXP (exp, 2), unknownp);
3617 /* Scan an attribute value, possibly a conditional, and record what actions
3618 will be required to do any conditional tests in it.
3621 `must_extract' if we need to extract the insn operands
3622 `must_constrain' if we must compute `which_alternative'
3623 `address_used' if an address expression was used
3624 `length_used' if an (eq_attr "length" ...) was used
3628 walk_attr_value (rtx exp)
3637 code = GET_CODE (exp);
3641 if (! ATTR_IND_SIMPLIFIED_P (exp))
3642 /* Since this is an arbitrary expression, it can look at anything.
3643 However, constant expressions do not depend on any particular
3645 must_extract = must_constrain = 1;
3653 must_extract = must_constrain = 1;
3657 if (XSTR (exp, 0) == alternative_name)
3658 must_extract = must_constrain = 1;
3659 else if (strcmp_check (XSTR (exp, 0), length_str) == 0)
3679 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
3684 walk_attr_value (XEXP (exp, i));
3688 if (XVEC (exp, i) != NULL)
3689 for (j = 0; j < XVECLEN (exp, i); j++)
3690 walk_attr_value (XVECEXP (exp, i, j));
3695 /* Write out a function to obtain the attribute for a given INSN. */
3698 write_attr_get (struct attr_desc *attr)
3700 struct attr_value *av, *common_av;
3702 /* Find the most used attribute value. Handle that as the `default' of the
3703 switch we will generate. */
3704 common_av = find_most_used (attr);
3706 /* Write out start of function, then all values with explicit `case' lines,
3707 then a `default', then the value with the most uses. */
3710 if (!attr->is_numeric)
3711 printf ("enum attr_%s\n", attr->name);
3715 /* If the attribute name starts with a star, the remainder is the name of
3716 the subroutine to use, instead of `get_attr_...'. */
3717 if (attr->name[0] == '*')
3718 printf ("%s (rtx insn ATTRIBUTE_UNUSED)\n", &attr->name[1]);
3719 else if (attr->is_const == 0)
3720 printf ("get_attr_%s (rtx insn ATTRIBUTE_UNUSED)\n", attr->name);
3723 printf ("get_attr_%s (void)\n", attr->name);
3726 for (av = attr->first_value; av; av = av->next)
3727 if (av->num_insns != 0)
3728 write_attr_set (attr, 2, av->value, "return", ";",
3729 true_rtx, av->first_insn->def->insn_code,
3730 av->first_insn->def->insn_index);
3737 printf (" switch (recog_memoized (insn))\n");
3740 for (av = attr->first_value; av; av = av->next)
3741 if (av != common_av)
3742 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
3744 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
3745 printf (" }\n}\n\n");
3748 /* Given an AND tree of known true terms (because we are inside an `if' with
3749 that as the condition or are in an `else' clause) and an expression,
3750 replace any known true terms with TRUE. Use `simplify_and_tree' to do
3751 the bulk of the work. */
3754 eliminate_known_true (rtx known_true, rtx exp, int insn_code, int insn_index)
3758 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
3760 if (GET_CODE (known_true) == AND)
3762 exp = eliminate_known_true (XEXP (known_true, 0), exp,
3763 insn_code, insn_index);
3764 exp = eliminate_known_true (XEXP (known_true, 1), exp,
3765 insn_code, insn_index);
3770 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
3776 /* Write out a series of tests and assignment statements to perform tests and
3777 sets of an attribute value. We are passed an indentation amount and prefix
3778 and suffix strings to write around each attribute value (e.g., "return"
3782 write_attr_set (struct attr_desc *attr, int indent, rtx value,
3783 const char *prefix, const char *suffix, rtx known_true,
3784 int insn_code, int insn_index)
3786 if (GET_CODE (value) == COND)
3788 /* Assume the default value will be the default of the COND unless we
3789 find an always true expression. */
3790 rtx default_val = XEXP (value, 1);
3791 rtx our_known_true = known_true;
3796 for (i = 0; i < XVECLEN (value, 0); i += 2)
3801 testexp = eliminate_known_true (our_known_true,
3802 XVECEXP (value, 0, i),
3803 insn_code, insn_index);
3804 newexp = attr_rtx (NOT, testexp);
3805 newexp = insert_right_side (AND, our_known_true, newexp,
3806 insn_code, insn_index);
3808 /* If the test expression is always true or if the next `known_true'
3809 expression is always false, this is the last case, so break
3810 out and let this value be the `else' case. */
3811 if (testexp == true_rtx || newexp == false_rtx)
3813 default_val = XVECEXP (value, 0, i + 1);
3817 /* Compute the expression to pass to our recursive call as being
3819 inner_true = insert_right_side (AND, our_known_true,
3820 testexp, insn_code, insn_index);
3822 /* If this is always false, skip it. */
3823 if (inner_true == false_rtx)
3826 write_indent (indent);
3827 printf ("%sif ", first_if ? "" : "else ");
3829 write_test_expr (testexp, 0);
3831 write_indent (indent + 2);
3834 write_attr_set (attr, indent + 4,
3835 XVECEXP (value, 0, i + 1), prefix, suffix,
3836 inner_true, insn_code, insn_index);
3837 write_indent (indent + 2);
3839 our_known_true = newexp;
3844 write_indent (indent);
3846 write_indent (indent + 2);
3850 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
3851 prefix, suffix, our_known_true, insn_code, insn_index);
3855 write_indent (indent + 2);
3861 write_indent (indent);
3862 printf ("%s ", prefix);
3863 write_attr_value (attr, value);
3864 printf ("%s\n", suffix);
3868 /* Write a series of case statements for every instruction in list IE.
3869 INDENT is the amount of indentation to write before each case. */
3872 write_insn_cases (struct insn_ent *ie, int indent)
3874 for (; ie != 0; ie = ie->next)
3875 if (ie->def->insn_code != -1)
3877 write_indent (indent);
3878 if (GET_CODE (ie->def->def) == DEFINE_PEEPHOLE)
3879 printf ("case %d: /* define_peephole, line %d */\n",
3880 ie->def->insn_code, ie->def->lineno);
3882 printf ("case %d: /* %s */\n",
3883 ie->def->insn_code, XSTR (ie->def->def, 0));
3887 /* Write out the computation for one attribute value. */
3890 write_attr_case (struct attr_desc *attr, struct attr_value *av,
3891 int write_case_lines, const char *prefix, const char *suffix,
3892 int indent, rtx known_true)
3894 if (av->num_insns == 0)
3897 if (av->has_asm_insn)
3899 write_indent (indent);
3900 printf ("case -1:\n");
3901 write_indent (indent + 2);
3902 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
3903 write_indent (indent + 2);
3904 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
3905 write_indent (indent + 2);
3906 printf (" fatal_insn_not_found (insn);\n");
3909 if (write_case_lines)
3910 write_insn_cases (av->first_insn, indent);
3913 write_indent (indent);
3914 printf ("default:\n");
3917 /* See what we have to do to output this value. */
3918 must_extract = must_constrain = address_used = 0;
3919 walk_attr_value (av->value);
3923 write_indent (indent + 2);
3924 printf ("extract_constrain_insn_cached (insn);\n");
3926 else if (must_extract)
3928 write_indent (indent + 2);
3929 printf ("extract_insn_cached (insn);\n");
3932 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
3933 known_true, av->first_insn->def->insn_code,
3934 av->first_insn->def->insn_index);
3936 if (strncmp (prefix, "return", 6))
3938 write_indent (indent + 2);
3939 printf ("break;\n");
3944 /* Search for uses of non-const attributes and write code to cache them. */
3947 write_expr_attr_cache (rtx p, struct attr_desc *attr)
3952 if (GET_CODE (p) == EQ_ATTR)
3954 if (XSTR (p, 0) != attr->name)
3957 if (!attr->is_numeric)
3958 printf (" enum attr_%s ", attr->name);
3962 printf ("attr_%s = get_attr_%s (insn);\n", attr->name, attr->name);
3966 fmt = GET_RTX_FORMAT (GET_CODE (p));
3967 ie = GET_RTX_LENGTH (GET_CODE (p));
3968 for (i = 0; i < ie; i++)
3973 if (write_expr_attr_cache (XEXP (p, i), attr))
3978 je = XVECLEN (p, i);
3979 for (j = 0; j < je; ++j)
3980 if (write_expr_attr_cache (XVECEXP (p, i, j), attr))
3989 /* Utilities to write in various forms. */
3992 write_attr_valueq (struct attr_desc *attr, const char *s)
3994 if (attr->is_numeric)
4000 if (num > 9 || num < 0)
4001 printf (" /* 0x%x */", num);
4005 write_upcase (attr->name);
4012 write_attr_value (struct attr_desc *attr, rtx value)
4016 switch (GET_CODE (value))
4019 write_attr_valueq (attr, XSTR (value, 0));
4023 printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
4027 print_c_condition (XSTR (value, 0));
4032 struct attr_desc *attr2 = find_attr (&XSTR (value, 0), 0);
4033 printf ("get_attr_%s (%s)", attr2->name,
4034 (attr2->is_const ? "" : "insn"));
4055 write_attr_value (attr, XEXP (value, 0));
4059 write_attr_value (attr, XEXP (value, 1));
4068 write_upcase (const char *str)
4072 /* The argument of TOUPPER should not have side effects. */
4073 putchar (TOUPPER(*str));
4079 write_indent (int indent)
4081 for (; indent > 8; indent -= 8)
4084 for (; indent; indent--)
4088 /* Write a subroutine that is given an insn that requires a delay slot, a
4089 delay slot ordinal, and a candidate insn. It returns nonzero if the
4090 candidate can be placed in the specified delay slot of the insn.
4092 We can write as many as three subroutines. `eligible_for_delay'
4093 handles normal delay slots, `eligible_for_annul_true' indicates that
4094 the specified insn can be annulled if the branch is true, and likewise
4095 for `eligible_for_annul_false'.
4097 KIND is a string distinguishing these three cases ("delay", "annul_true",
4098 or "annul_false"). */
4101 write_eligible_delay (const char *kind)
4103 struct delay_desc *delay;
4107 struct attr_desc *attr;
4108 struct attr_value *av, *common_av;
4111 /* Compute the maximum number of delay slots required. We use the delay
4112 ordinal times this number plus one, plus the slot number as an index into
4113 the appropriate predicate to test. */
4115 for (delay = delays, max_slots = 0; delay; delay = delay->next)
4116 if (XVECLEN (delay->def, 1) / 3 > max_slots)
4117 max_slots = XVECLEN (delay->def, 1) / 3;
4119 /* Write function prelude. */
4122 printf ("eligible_for_%s (rtx delay_insn ATTRIBUTE_UNUSED, int slot, rtx candidate_insn, int flags ATTRIBUTE_UNUSED)\n",
4125 printf (" rtx insn;\n");
4127 printf (" gcc_assert (slot < %d);\n", max_slots);
4129 /* Allow dbr_schedule to pass labels, etc. This can happen if try_split
4130 converts a compound instruction into a loop. */
4131 printf (" if (!INSN_P (candidate_insn))\n");
4132 printf (" return 0;\n");
4135 /* If more than one delay type, find out which type the delay insn is. */
4139 attr = find_attr (&delay_type_str, 0);
4141 common_av = find_most_used (attr);
4143 printf (" insn = delay_insn;\n");
4144 printf (" switch (recog_memoized (insn))\n");
4147 sprintf (str, " * %d;\n break;", max_slots);
4148 for (av = attr->first_value; av; av = av->next)
4149 if (av != common_av)
4150 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
4152 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
4155 /* Ensure matched. Otherwise, shouldn't have been called. */
4156 printf (" gcc_assert (slot >= %d);\n\n", max_slots);
4159 /* If just one type of delay slot, write simple switch. */
4160 if (num_delays == 1 && max_slots == 1)
4162 printf (" insn = candidate_insn;\n");
4163 printf (" switch (recog_memoized (insn))\n");
4166 attr = find_attr (&delay_1_0_str, 0);
4168 common_av = find_most_used (attr);
4170 for (av = attr->first_value; av; av = av->next)
4171 if (av != common_av)
4172 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
4174 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
4180 /* Write a nested CASE. The first indicates which condition we need to
4181 test, and the inner CASE tests the condition. */
4182 printf (" insn = candidate_insn;\n");
4183 printf (" switch (slot)\n");
4186 for (delay = delays; delay; delay = delay->next)
4187 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
4189 printf (" case %d:\n",
4190 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
4191 printf (" switch (recog_memoized (insn))\n");
4194 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
4196 attr = find_attr (&pstr, 0);
4198 common_av = find_most_used (attr);
4200 for (av = attr->first_value; av; av = av->next)
4201 if (av != common_av)
4202 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
4204 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
4208 printf (" default:\n");
4209 printf (" gcc_unreachable ();\n");
4216 /* This page contains miscellaneous utility routines. */
4218 /* Given a pointer to a (char *), return a malloc'ed string containing the
4219 next comma-separated element. Advance the pointer to after the string
4220 scanned, or the end-of-string. Return NULL if at end of string. */
4223 next_comma_elt (const char **pstr)
4227 start = scan_comma_elt (pstr);
4232 return attr_string (start, *pstr - start);
4235 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
4236 is nonzero, build a new attribute, if one does not exist. *NAME_P is
4237 replaced by a pointer to a canonical copy of the string. */
4239 static struct attr_desc *
4240 find_attr (const char **name_p, int create)
4242 struct attr_desc *attr;
4244 const char *name = *name_p;
4246 /* Before we resort to using `strcmp', see if the string address matches
4247 anywhere. In most cases, it should have been canonicalized to do so. */
4248 if (name == alternative_name)
4251 index = name[0] & (MAX_ATTRS_INDEX - 1);
4252 for (attr = attrs[index]; attr; attr = attr->next)
4253 if (name == attr->name)
4256 /* Otherwise, do it the slow way. */
4257 for (attr = attrs[index]; attr; attr = attr->next)
4258 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
4260 *name_p = attr->name;
4267 attr = oballoc (sizeof (struct attr_desc));
4268 attr->name = DEF_ATTR_STRING (name);
4269 attr->first_value = attr->default_val = NULL;
4270 attr->is_numeric = attr->is_const = attr->is_special = 0;
4272 attr->next = attrs[index];
4273 attrs[index] = attr;
4275 *name_p = attr->name;
4280 /* Create internal attribute with the given default value. */
4283 make_internal_attr (const char *name, rtx value, int special)
4285 struct attr_desc *attr;
4287 attr = find_attr (&name, 1);
4288 gcc_assert (!attr->default_val);
4290 attr->is_numeric = 1;
4292 attr->is_special = (special & ATTR_SPECIAL) != 0;
4293 attr->static_p = (special & ATTR_STATIC) != 0;
4294 attr->default_val = get_attr_value (value, attr, -2);
4297 /* Find the most used value of an attribute. */
4299 static struct attr_value *
4300 find_most_used (struct attr_desc *attr)
4302 struct attr_value *av;
4303 struct attr_value *most_used;
4309 for (av = attr->first_value; av; av = av->next)
4310 if (av->num_insns > nuses)
4311 nuses = av->num_insns, most_used = av;
4316 /* Return (attr_value "n") */
4319 make_numeric_value (int n)
4321 static rtx int_values[20];
4325 gcc_assert (n >= 0);
4327 if (n < 20 && int_values[n])
4328 return int_values[n];
4330 p = attr_printf (MAX_DIGITS, "%d", n);
4331 exp = attr_rtx (CONST_STRING, p);
4334 int_values[n] = exp;
4340 copy_rtx_unchanging (rtx orig)
4342 if (ATTR_IND_SIMPLIFIED_P (orig) || ATTR_CURR_SIMPLIFIED_P (orig))
4345 ATTR_CURR_SIMPLIFIED_P (orig) = 1;
4349 /* Determine if an insn has a constant number of delay slots, i.e., the
4350 number of delay slots is not a function of the length of the insn. */
4353 write_const_num_delay_slots (void)
4355 struct attr_desc *attr = find_attr (&num_delay_slots_str, 0);
4356 struct attr_value *av;
4360 printf ("int\nconst_num_delay_slots (rtx insn)\n");
4362 printf (" switch (recog_memoized (insn))\n");
4365 for (av = attr->first_value; av; av = av->next)
4368 walk_attr_value (av->value);
4370 write_insn_cases (av->first_insn, 4);
4373 printf (" default:\n");
4374 printf (" return 1;\n");
4375 printf (" }\n}\n\n");
4380 main (int argc, char **argv)
4383 struct attr_desc *attr;
4384 struct insn_def *id;
4388 progname = "genattrtab";
4390 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
4391 return (FATAL_EXIT_CODE);
4393 obstack_init (hash_obstack);
4394 obstack_init (temp_obstack);
4396 /* Set up true and false rtx's */
4397 true_rtx = rtx_alloc (CONST_INT);
4398 XWINT (true_rtx, 0) = 1;
4399 false_rtx = rtx_alloc (CONST_INT);
4400 XWINT (false_rtx, 0) = 0;
4401 ATTR_IND_SIMPLIFIED_P (true_rtx) = ATTR_IND_SIMPLIFIED_P (false_rtx) = 1;
4402 ATTR_PERMANENT_P (true_rtx) = ATTR_PERMANENT_P (false_rtx) = 1;
4404 alternative_name = DEF_ATTR_STRING ("alternative");
4405 length_str = DEF_ATTR_STRING ("length");
4406 delay_type_str = DEF_ATTR_STRING ("*delay_type");
4407 delay_1_0_str = DEF_ATTR_STRING ("*delay_1_0");
4408 num_delay_slots_str = DEF_ATTR_STRING ("*num_delay_slots");
4410 printf ("/* Generated automatically by the program `genattrtab'\n\
4411 from the machine description file `md'. */\n\n");
4413 /* Read the machine description. */
4415 initiate_automaton_gen (argc, argv);
4420 desc = read_md_rtx (&lineno, &insn_code_number);
4424 switch (GET_CODE (desc))
4427 case DEFINE_PEEPHOLE:
4428 case DEFINE_ASM_ATTRIBUTES:
4429 gen_insn (desc, lineno);
4433 gen_attr (desc, lineno);
4437 gen_delay (desc, lineno);
4440 case DEFINE_CPU_UNIT:
4441 gen_cpu_unit (desc);
4444 case DEFINE_QUERY_CPU_UNIT:
4445 gen_query_cpu_unit (desc);
4453 gen_excl_set (desc);
4457 gen_presence_set (desc);
4460 case FINAL_PRESENCE_SET:
4461 gen_final_presence_set (desc);
4465 gen_absence_set (desc);
4468 case FINAL_ABSENCE_SET:
4469 gen_final_absence_set (desc);
4472 case DEFINE_AUTOMATON:
4473 gen_automaton (desc);
4476 case AUTOMATA_OPTION:
4477 gen_automata_option (desc);
4480 case DEFINE_RESERVATION:
4484 case DEFINE_INSN_RESERVATION:
4485 gen_insn_reserv (desc);
4491 if (GET_CODE (desc) != DEFINE_ASM_ATTRIBUTES)
4492 insn_index_number++;
4496 return FATAL_EXIT_CODE;
4500 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
4501 if (! got_define_asm_attributes)
4503 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
4504 XVEC (tem, 0) = rtvec_alloc (0);
4508 /* Expand DEFINE_DELAY information into new attribute. */
4512 /* Build DFA, output some functions and expand DFA information
4513 to new attributes. */
4517 printf ("#include \"config.h\"\n");
4518 printf ("#include \"system.h\"\n");
4519 printf ("#include \"coretypes.h\"\n");
4520 printf ("#include \"tm.h\"\n");
4521 printf ("#include \"rtl.h\"\n");
4522 printf ("#include \"tm_p.h\"\n");
4523 printf ("#include \"insn-config.h\"\n");
4524 printf ("#include \"recog.h\"\n");
4525 printf ("#include \"regs.h\"\n");
4526 printf ("#include \"real.h\"\n");
4527 printf ("#include \"output.h\"\n");
4528 printf ("#include \"insn-attr.h\"\n");
4529 printf ("#include \"toplev.h\"\n");
4530 printf ("#include \"flags.h\"\n");
4531 printf ("#include \"function.h\"\n");
4533 printf ("#define operands recog_data.operand\n\n");
4535 /* Make `insn_alternatives'. */
4536 insn_alternatives = oballoc (insn_code_number * sizeof (int));
4537 for (id = defs; id; id = id->next)
4538 if (id->insn_code >= 0)
4539 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
4541 /* Make `insn_n_alternatives'. */
4542 insn_n_alternatives = oballoc (insn_code_number * sizeof (int));
4543 for (id = defs; id; id = id->next)
4544 if (id->insn_code >= 0)
4545 insn_n_alternatives[id->insn_code] = id->num_alternatives;
4547 /* Prepare to write out attribute subroutines by checking everything stored
4548 away and building the attribute cases. */
4552 for (i = 0; i < MAX_ATTRS_INDEX; i++)
4553 for (attr = attrs[i]; attr; attr = attr->next)
4554 attr->default_val->value
4555 = check_attr_value (attr->default_val->value, attr);
4558 return FATAL_EXIT_CODE;
4560 for (i = 0; i < MAX_ATTRS_INDEX; i++)
4561 for (attr = attrs[i]; attr; attr = attr->next)
4564 /* Construct extra attributes for `length'. */
4565 make_length_attrs ();
4567 /* Perform any possible optimizations to speed up compilation. */
4570 /* Now write out all the `gen_attr_...' routines. Do these before the
4571 special routines so that they get defined before they are used. */
4573 for (i = 0; i < MAX_ATTRS_INDEX; i++)
4574 for (attr = attrs[i]; attr; attr = attr->next)
4576 if (! attr->is_special && ! attr->is_const)
4581 = (attr->name [0] == '*'
4582 && strcmp (&attr->name[1], INSN_ALTS_FUNC_NAME) == 0);
4584 printf ("\n#if AUTOMATON_ALTS\n");
4585 write_attr_get (attr);
4587 printf ("#endif\n\n");
4591 /* Write out delay eligibility information, if DEFINE_DELAY present.
4592 (The function to compute the number of delay slots will be written
4596 write_eligible_delay ("delay");
4597 if (have_annul_true)
4598 write_eligible_delay ("annul_true");
4599 if (have_annul_false)
4600 write_eligible_delay ("annul_false");
4603 /* Output code for pipeline hazards recognition based on DFA
4604 (deterministic finite-state automata). */
4608 /* Write out constant delay slot info. */
4609 write_const_num_delay_slots ();
4611 write_length_unit_log ();
4614 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
4617 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
4619 get_insn_name (int code ATTRIBUTE_UNUSED)