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 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_FUNCTION_UNIT 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_FUNCTION_UNIT. 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 and DEFINE_FUNCTION_UNIT
74 definitions is to create arbitrarily complex expressions and have the
75 optimization simplify them.
77 Once optimization is complete, any required routines and definitions
80 An optimization that is not yet implemented is to hoist the constant
81 expressions entirely out of the routines and definitions that are written.
82 A way to do this is to iterate over all possible combinations of values
83 for constant attributes and generate a set of functions for that given
84 combination. An initialization function would be written that evaluates
85 the attributes and installs the corresponding set of routines and
86 definitions (each would be accessed through a pointer).
88 We use the flags in an RTX as follows:
89 `unchanging' (ATTR_IND_SIMPLIFIED_P): This rtx is fully simplified
90 independent of the insn code.
91 `in_struct' (ATTR_CURR_SIMPLIFIED_P): This rtx is fully simplified
92 for the insn code currently being processed (see optimize_attrs).
93 `integrated' (ATTR_PERMANENT_P): This rtx is permanent and unique
95 `volatil' (ATTR_EQ_ATTR_P): During simplify_by_exploding the value of an
96 EQ_ATTR rtx is true if !volatil and false if volatil. */
98 #define ATTR_IND_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), unchanging))
99 #define ATTR_CURR_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), in_struct))
100 #define ATTR_PERMANENT_P(RTX) (RTX_FLAG((RTX), integrated))
101 #define ATTR_EQ_ATTR_P(RTX) (RTX_FLAG((RTX), volatil))
104 #define strcmp_check(S1, S2) ((S1) == (S2) \
106 : (strcmp ((S1), (S2)) \
110 #define strcmp_check(S1, S2) ((S1) != (S2))
115 #include "coretypes.h"
119 #include "gensupport.h"
121 #ifdef HAVE_SYS_RESOURCE_H
122 # include <sys/resource.h>
125 /* We must include obstack.h after <sys/time.h>, to avoid lossage with
126 /usr/include/sys/stdtypes.h on Sun OS 4.x. */
130 #include "genattrtab.h"
132 static struct obstack obstack1, obstack2;
133 struct obstack *hash_obstack = &obstack1;
134 struct obstack *temp_obstack = &obstack2;
136 /* enough space to reserve for printing out ints */
137 #define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)
139 /* Define structures used to record attributes and values. */
141 /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
142 encountered, we store all the relevant information into a
143 `struct insn_def'. This is done to allow attribute definitions to occur
144 anywhere in the file. */
148 struct insn_def *next; /* Next insn in chain. */
149 rtx def; /* The DEFINE_... */
150 int insn_code; /* Instruction number. */
151 int insn_index; /* Expression numer in file, for errors. */
152 int lineno; /* Line number. */
153 int num_alternatives; /* Number of alternatives. */
154 int vec_idx; /* Index of attribute vector in `def'. */
157 /* Once everything has been read in, we store in each attribute value a list
158 of insn codes that have that value. Here is the structure used for the
163 struct insn_ent *next; /* Next in chain. */
164 int insn_code; /* Instruction number. */
165 int insn_index; /* Index of definition in file */
166 int lineno; /* Line number. */
169 /* Each value of an attribute (either constant or computed) is assigned a
170 structure which is used as the listhead of the insns that have that
175 rtx value; /* Value of attribute. */
176 struct attr_value *next; /* Next attribute value in chain. */
177 struct insn_ent *first_insn; /* First insn with this value. */
178 int num_insns; /* Number of insns with this value. */
179 int has_asm_insn; /* True if this value used for `asm' insns */
182 /* Structure for each attribute. */
186 char *name; /* Name of attribute. */
187 struct attr_desc *next; /* Next attribute. */
188 struct attr_value *first_value; /* First value of this attribute. */
189 struct attr_value *default_val; /* Default value for this attribute. */
190 int lineno : 24; /* Line number. */
191 unsigned is_numeric : 1; /* Values of this attribute are numeric. */
192 unsigned negative_ok : 1; /* Allow negative numeric values. */
193 unsigned unsigned_p : 1; /* Make the output function unsigned int. */
194 unsigned is_const : 1; /* Attribute value constant for each run. */
195 unsigned is_special : 1; /* Don't call `write_attr_set'. */
196 unsigned func_units_p : 1; /* This is the function_units attribute. */
197 unsigned blockage_p : 1; /* This is the blockage range function. */
198 unsigned static_p : 1; /* Make the output function static. */
201 #define NULL_ATTR (struct attr_desc *) NULL
203 /* A range of values. */
211 /* Structure for each DEFINE_DELAY. */
215 rtx def; /* DEFINE_DELAY expression. */
216 struct delay_desc *next; /* Next DEFINE_DELAY. */
217 int num; /* Number of DEFINE_DELAY, starting at 1. */
218 int lineno; /* Line number. */
221 /* Record information about each DEFINE_FUNCTION_UNIT. */
223 struct function_unit_op
225 rtx condexp; /* Expression TRUE for applicable insn. */
226 struct function_unit_op *next; /* Next operation for this function unit. */
227 int num; /* Ordinal for this operation type in unit. */
228 int ready; /* Cost until data is ready. */
229 int issue_delay; /* Cost until unit can accept another insn. */
230 rtx conflict_exp; /* Expression TRUE for insns incurring issue delay. */
231 rtx issue_exp; /* Expression computing issue delay. */
232 int lineno; /* Line number. */
235 /* Record information about each function unit mentioned in a
236 DEFINE_FUNCTION_UNIT. */
240 const char *name; /* Function unit name. */
241 struct function_unit *next; /* Next function unit. */
242 int num; /* Ordinal of this unit type. */
243 int multiplicity; /* Number of units of this type. */
244 int simultaneity; /* Maximum number of simultaneous insns
245 on this function unit or 0 if unlimited. */
246 rtx condexp; /* Expression TRUE for insn needing unit. */
247 int num_opclasses; /* Number of different operation types. */
248 struct function_unit_op *ops; /* Pointer to first operation type. */
249 int needs_conflict_function; /* Nonzero if a conflict function required. */
250 int needs_blockage_function; /* Nonzero if a blockage function required. */
251 int needs_range_function; /* Nonzero if blockage range function needed. */
252 rtx default_cost; /* Conflict cost, if constant. */
253 struct range issue_delay; /* Range of issue delay values. */
254 int max_blockage; /* Maximum time an insn blocks the unit. */
255 int first_lineno; /* First seen line number. */
258 /* Listheads of above structures. */
260 /* This one is indexed by the first character of the attribute name. */
261 #define MAX_ATTRS_INDEX 256
262 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
263 static struct insn_def *defs;
264 static struct delay_desc *delays;
265 static struct function_unit *units;
267 /* An expression where all the unknown terms are EQ_ATTR tests can be
268 rearranged into a COND provided we can enumerate all possible
269 combinations of the unknown values. The set of combinations become the
270 tests of the COND; the value of the expression given that combination is
271 computed and becomes the corresponding value. To do this, we must be
272 able to enumerate all values for each attribute used in the expression
273 (currently, we give up if we find a numeric attribute).
275 If the set of EQ_ATTR tests used in an expression tests the value of N
276 different attributes, the list of all possible combinations can be made
277 by walking the N-dimensional attribute space defined by those
278 attributes. We record each of these as a struct dimension.
280 The algorithm relies on sharing EQ_ATTR nodes: if two nodes in an
281 expression are the same, the will also have the same address. We find
282 all the EQ_ATTR nodes by marking them ATTR_EQ_ATTR_P. This bit later
283 represents the value of an EQ_ATTR node, so once all nodes are marked,
284 they are also given an initial value of FALSE.
286 We then separate the set of EQ_ATTR nodes into dimensions for each
287 attribute and put them on the VALUES list. Terms are added as needed by
288 `add_values_to_cover' so that all possible values of the attribute are
291 Each dimension also has a current value. This is the node that is
292 currently considered to be TRUE. If this is one of the nodes added by
293 `add_values_to_cover', all the EQ_ATTR tests in the original expression
294 will be FALSE. Otherwise, only the CURRENT_VALUE will be true.
296 NUM_VALUES is simply the length of the VALUES list and is there for
299 Once the dimensions are created, the algorithm enumerates all possible
300 values and computes the current value of the given expression. */
304 struct attr_desc *attr; /* Attribute for this dimension. */
305 rtx values; /* List of attribute values used. */
306 rtx current_value; /* Position in the list for the TRUE value. */
307 int num_values; /* Length of the values list. */
310 /* Other variables. */
312 static int insn_code_number;
313 static int insn_index_number;
314 static int got_define_asm_attributes;
315 static int must_extract;
316 static int must_constrain;
317 static int address_used;
318 static int length_used;
319 static int num_delays;
320 static int have_annul_true, have_annul_false;
321 static int num_units, num_unit_opclasses;
322 static int num_insn_ents;
326 /* Used as operand to `operate_exp': */
328 enum operator {PLUS_OP, MINUS_OP, POS_MINUS_OP, EQ_OP, OR_OP, ORX_OP, MAX_OP, MIN_OP, RANGE_OP};
330 /* Stores, for each insn code, the number of constraint alternatives. */
332 static int *insn_n_alternatives;
334 /* Stores, for each insn code, a bitmap that has bits on for each possible
337 static int *insn_alternatives;
339 /* If nonzero, assume that the `alternative' attr has this value.
340 This is the hashed, unique string for the numeral
341 whose value is chosen alternative. */
343 static const char *current_alternative_string;
345 /* Used to simplify expressions. */
347 static rtx true_rtx, false_rtx;
349 /* Used to reduce calls to `strcmp' */
351 static char *alternative_name;
352 static const char *length_str;
353 static const char *delay_type_str;
354 static const char *delay_1_0_str;
355 static const char *num_delay_slots_str;
357 /* Indicate that REG_DEAD notes are valid if dead_or_set_p is ever
360 int reload_completed = 0;
362 /* Some machines test `optimize' in macros called from rtlanal.c, so we need
363 to define it here. */
367 /* Simplify an expression. Only call the routine if there is something to
369 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
370 (ATTR_IND_SIMPLIFIED_P (EXP) || ATTR_CURR_SIMPLIFIED_P (EXP) ? (EXP) \
371 : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
373 /* Simplify (eq_attr ("alternative") ...)
374 when we are working with a particular alternative. */
375 #define SIMPLIFY_ALTERNATIVE(EXP) \
376 if (current_alternative_string \
377 && GET_CODE ((EXP)) == EQ_ATTR \
378 && XSTR ((EXP), 0) == alternative_name) \
379 (EXP) = (XSTR ((EXP), 1) == current_alternative_string \
380 ? true_rtx : false_rtx);
382 #define DEF_ATTR_STRING(S) (attr_string ((S), strlen (S)))
384 /* These are referenced by rtlanal.c and hence need to be defined somewhere.
385 They won't actually be used. */
387 rtx global_rtl[GR_MAX];
388 rtx pic_offset_table_rtx;
390 static void attr_hash_add_rtx (int, rtx);
391 static void attr_hash_add_string (int, char *);
392 static rtx attr_rtx (enum rtx_code, ...);
393 static rtx attr_rtx_1 (enum rtx_code, va_list);
394 static char *attr_string (const char *, int);
395 static rtx check_attr_value (rtx, struct attr_desc *);
396 static rtx convert_set_attr_alternative (rtx, struct insn_def *);
397 static rtx convert_set_attr (rtx, struct insn_def *);
398 static void check_defs (void);
399 static rtx make_canonical (struct attr_desc *, rtx);
400 static struct attr_value *get_attr_value (rtx, struct attr_desc *, int);
401 static rtx copy_rtx_unchanging (rtx);
402 static rtx copy_boolean (rtx);
403 static void expand_delays (void);
404 static rtx operate_exp (enum operator, rtx, rtx);
405 static void expand_units (void);
406 static rtx simplify_knowing (rtx, rtx);
407 static rtx encode_units_mask (rtx);
408 static void fill_attr (struct attr_desc *);
409 static rtx substitute_address (rtx, rtx (*) (rtx), rtx (*) (rtx));
410 static void make_length_attrs (void);
411 static rtx identity_fn (rtx);
412 static rtx zero_fn (rtx);
413 static rtx one_fn (rtx);
414 static rtx max_fn (rtx);
415 static void write_length_unit_log (void);
416 static rtx simplify_cond (rtx, int, int);
417 static rtx simplify_by_exploding (rtx);
418 static int find_and_mark_used_attributes (rtx, rtx *, int *);
419 static void unmark_used_attributes (rtx, struct dimension *, int);
420 static int add_values_to_cover (struct dimension *);
421 static int increment_current_value (struct dimension *, int);
422 static rtx test_for_current_value (struct dimension *, int);
423 static rtx simplify_with_current_value (rtx, struct dimension *, int);
424 static rtx simplify_with_current_value_aux (rtx);
425 static void clear_struct_flag (rtx);
426 static void remove_insn_ent (struct attr_value *, struct insn_ent *);
427 static void insert_insn_ent (struct attr_value *, struct insn_ent *);
428 static rtx insert_right_side (enum rtx_code, rtx, rtx, int, int);
429 static rtx make_alternative_compare (int);
430 static int compute_alternative_mask (rtx, enum rtx_code);
431 static rtx evaluate_eq_attr (rtx, rtx, int, int);
432 static rtx simplify_and_tree (rtx, rtx *, int, int);
433 static rtx simplify_or_tree (rtx, rtx *, int, int);
434 static rtx simplify_test_exp (rtx, int, int);
435 static rtx simplify_test_exp_in_temp (rtx, int, int);
436 static void optimize_attrs (void);
437 static void gen_attr (rtx, int);
438 static int count_alternatives (rtx);
439 static int compares_alternatives_p (rtx);
440 static int contained_in_p (rtx, rtx);
441 static void gen_insn (rtx, int);
442 static void gen_delay (rtx, int);
443 static void gen_unit (rtx, int);
444 static void write_test_expr (rtx, int);
445 static int max_attr_value (rtx, int*);
446 static int or_attr_value (rtx, int*);
447 static void walk_attr_value (rtx);
448 static void write_attr_get (struct attr_desc *);
449 static rtx eliminate_known_true (rtx, rtx, int, int);
450 static void write_attr_set (struct attr_desc *, int, rtx,
451 const char *, const char *, rtx,
453 static void write_attr_case (struct attr_desc *, struct attr_value *,
454 int, const char *, const char *, int, rtx);
455 static void write_unit_name (const char *, int, const char *);
456 static void write_attr_valueq (struct attr_desc *, const char *);
457 static void write_attr_value (struct attr_desc *, rtx);
458 static void write_upcase (const char *);
459 static void write_indent (int);
460 static void write_eligible_delay (const char *);
461 static void write_function_unit_info (void);
462 static void write_complex_function (struct function_unit *, const char *,
464 static int write_expr_attr_cache (rtx, struct attr_desc *);
465 static void write_toplevel_expr (rtx);
466 static void write_const_num_delay_slots (void);
467 static char *next_comma_elt (const char **);
468 static struct attr_desc *find_attr (const char **, int);
469 static struct attr_value *find_most_used (struct attr_desc *);
470 static rtx find_single_value (struct attr_desc *);
471 static void extend_range (struct range *, int, int);
472 static rtx attr_eq (const char *, const char *);
473 static const char *attr_numeral (int);
474 static int attr_equal_p (rtx, rtx);
475 static rtx attr_copy_rtx (rtx);
476 static int attr_rtx_cost (rtx);
477 static bool attr_alt_subset_p (rtx, rtx);
478 static bool attr_alt_subset_of_compl_p (rtx, rtx);
479 static rtx attr_alt_intersection (rtx, rtx);
480 static rtx attr_alt_union (rtx, rtx);
481 static rtx attr_alt_complement (rtx);
482 static bool attr_alt_bit_p (rtx, int);
483 static rtx mk_attr_alt (int);
485 #define oballoc(size) obstack_alloc (hash_obstack, size)
487 /* Hash table for sharing RTL and strings. */
489 /* Each hash table slot is a bucket containing a chain of these structures.
490 Strings are given negative hash codes; RTL expressions are given positive
495 struct attr_hash *next; /* Next structure in the bucket. */
496 int hashcode; /* Hash code of this rtx or string. */
499 char *str; /* The string (negative hash codes) */
500 rtx rtl; /* or the RTL recorded here. */
504 /* Now here is the hash table. When recording an RTL, it is added to
505 the slot whose index is the hash code mod the table size. Note
506 that the hash table is used for several kinds of RTL (see attr_rtx)
507 and for strings. While all these live in the same table, they are
508 completely independent, and the hash code is computed differently
511 #define RTL_HASH_SIZE 4093
512 struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
514 /* Here is how primitive or already-shared RTL's hash
516 #define RTL_HASH(RTL) ((long) (RTL) & 0777777)
518 /* Add an entry to the hash table for RTL with hash code HASHCODE. */
521 attr_hash_add_rtx (int hashcode, rtx rtl)
525 h = obstack_alloc (hash_obstack, sizeof (struct attr_hash));
526 h->hashcode = hashcode;
528 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
529 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
532 /* Add an entry to the hash table for STRING with hash code HASHCODE. */
535 attr_hash_add_string (int hashcode, char *str)
539 h = obstack_alloc (hash_obstack, sizeof (struct attr_hash));
540 h->hashcode = -hashcode;
542 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
543 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
546 /* Generate an RTL expression, but avoid duplicates.
547 Set the ATTR_PERMANENT_P flag for these permanent objects.
549 In some cases we cannot uniquify; then we return an ordinary
550 impermanent rtx with ATTR_PERMANENT_P clear.
554 rtx attr_rtx (code, [element1, ..., elementn]) */
557 attr_rtx_1 (enum rtx_code code, va_list p)
559 rtx rt_val = NULL_RTX;/* RTX to return to caller... */
562 struct obstack *old_obstack = rtl_obstack;
564 /* For each of several cases, search the hash table for an existing entry.
565 Use that entry if one is found; otherwise create a new RTL and add it
568 if (GET_RTX_CLASS (code) == '1')
570 rtx arg0 = va_arg (p, rtx);
572 /* A permanent object cannot point to impermanent ones. */
573 if (! ATTR_PERMANENT_P (arg0))
575 rt_val = rtx_alloc (code);
576 XEXP (rt_val, 0) = arg0;
580 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
581 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
582 if (h->hashcode == hashcode
583 && GET_CODE (h->u.rtl) == code
584 && XEXP (h->u.rtl, 0) == arg0)
589 rtl_obstack = hash_obstack;
590 rt_val = rtx_alloc (code);
591 XEXP (rt_val, 0) = arg0;
594 else if (GET_RTX_CLASS (code) == 'c'
595 || GET_RTX_CLASS (code) == '2'
596 || GET_RTX_CLASS (code) == '<')
598 rtx arg0 = va_arg (p, rtx);
599 rtx arg1 = va_arg (p, rtx);
601 /* A permanent object cannot point to impermanent ones. */
602 if (! ATTR_PERMANENT_P (arg0) || ! ATTR_PERMANENT_P (arg1))
604 rt_val = rtx_alloc (code);
605 XEXP (rt_val, 0) = arg0;
606 XEXP (rt_val, 1) = arg1;
610 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
611 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
612 if (h->hashcode == hashcode
613 && GET_CODE (h->u.rtl) == code
614 && XEXP (h->u.rtl, 0) == arg0
615 && XEXP (h->u.rtl, 1) == arg1)
620 rtl_obstack = hash_obstack;
621 rt_val = rtx_alloc (code);
622 XEXP (rt_val, 0) = arg0;
623 XEXP (rt_val, 1) = arg1;
626 else if (GET_RTX_LENGTH (code) == 1
627 && GET_RTX_FORMAT (code)[0] == 's')
629 char *arg0 = va_arg (p, char *);
631 arg0 = DEF_ATTR_STRING (arg0);
633 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
634 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
635 if (h->hashcode == hashcode
636 && GET_CODE (h->u.rtl) == code
637 && XSTR (h->u.rtl, 0) == arg0)
642 rtl_obstack = hash_obstack;
643 rt_val = rtx_alloc (code);
644 XSTR (rt_val, 0) = arg0;
647 else if (GET_RTX_LENGTH (code) == 2
648 && GET_RTX_FORMAT (code)[0] == 's'
649 && GET_RTX_FORMAT (code)[1] == 's')
651 char *arg0 = va_arg (p, char *);
652 char *arg1 = va_arg (p, char *);
654 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
655 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
656 if (h->hashcode == hashcode
657 && GET_CODE (h->u.rtl) == code
658 && XSTR (h->u.rtl, 0) == arg0
659 && XSTR (h->u.rtl, 1) == arg1)
664 rtl_obstack = hash_obstack;
665 rt_val = rtx_alloc (code);
666 XSTR (rt_val, 0) = arg0;
667 XSTR (rt_val, 1) = arg1;
670 else if (code == CONST_INT)
672 HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
682 int i; /* Array indices... */
683 const char *fmt; /* Current rtx's format... */
685 rt_val = rtx_alloc (code); /* Allocate the storage space. */
687 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
688 for (i = 0; i < GET_RTX_LENGTH (code); i++)
692 case '0': /* Unused field. */
695 case 'i': /* An integer? */
696 XINT (rt_val, i) = va_arg (p, int);
699 case 'w': /* A wide integer? */
700 XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
703 case 's': /* A string? */
704 XSTR (rt_val, i) = va_arg (p, char *);
707 case 'e': /* An expression? */
708 case 'u': /* An insn? Same except when printing. */
709 XEXP (rt_val, i) = va_arg (p, rtx);
712 case 'E': /* An RTX vector? */
713 XVEC (rt_val, i) = va_arg (p, rtvec);
723 rtl_obstack = old_obstack;
724 attr_hash_add_rtx (hashcode, rt_val);
725 ATTR_PERMANENT_P (rt_val) = 1;
730 attr_rtx (enum rtx_code code, ...)
736 result = attr_rtx_1 (code, p);
741 /* Create a new string printed with the printf line arguments into a space
742 of at most LEN bytes:
744 rtx attr_printf (len, format, [arg1, ..., argn]) */
747 attr_printf (unsigned int len, const char *fmt, ...)
754 if (len > sizeof str - 1) /* Leave room for \0. */
757 vsprintf (str, fmt, p);
760 return DEF_ATTR_STRING (str);
764 attr_eq (const char *name, const char *value)
766 return attr_rtx (EQ_ATTR, DEF_ATTR_STRING (name), DEF_ATTR_STRING (value));
772 return XSTR (make_numeric_value (n), 0);
775 /* Return a permanent (possibly shared) copy of a string STR (not assumed
776 to be null terminated) with LEN bytes. */
779 attr_string (const char *str, int len)
786 /* Compute the hash code. */
787 hashcode = (len + 1) * 613 + (unsigned) str[0];
788 for (i = 1; i <= len; i += 2)
789 hashcode = ((hashcode * 613) + (unsigned) str[i]);
791 hashcode = -hashcode;
793 /* Search the table for the string. */
794 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
795 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
796 && !strncmp (h->u.str, str, len))
797 return h->u.str; /* <-- return if found. */
799 /* Not found; create a permanent copy and add it to the hash table. */
800 new_str = obstack_alloc (hash_obstack, len + 1);
801 memcpy (new_str, str, len);
803 attr_hash_add_string (hashcode, new_str);
805 return new_str; /* Return the new string. */
808 /* Check two rtx's for equality of contents,
809 taking advantage of the fact that if both are hashed
810 then they can't be equal unless they are the same object. */
813 attr_equal_p (rtx x, rtx y)
815 return (x == y || (! (ATTR_PERMANENT_P (x) && ATTR_PERMANENT_P (y))
816 && rtx_equal_p (x, y)));
819 /* Copy an attribute value expression,
820 descending to all depths, but not copying any
821 permanent hashed subexpressions. */
824 attr_copy_rtx (rtx orig)
829 const char *format_ptr;
831 /* No need to copy a permanent object. */
832 if (ATTR_PERMANENT_P (orig))
835 code = GET_CODE (orig);
854 copy = rtx_alloc (code);
855 PUT_MODE (copy, GET_MODE (orig));
856 ATTR_IND_SIMPLIFIED_P (copy) = ATTR_IND_SIMPLIFIED_P (orig);
857 ATTR_CURR_SIMPLIFIED_P (copy) = ATTR_CURR_SIMPLIFIED_P (orig);
858 ATTR_PERMANENT_P (copy) = ATTR_PERMANENT_P (orig);
859 ATTR_EQ_ATTR_P (copy) = ATTR_EQ_ATTR_P (orig);
861 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
863 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
865 switch (*format_ptr++)
868 XEXP (copy, i) = XEXP (orig, i);
869 if (XEXP (orig, i) != NULL)
870 XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
875 XVEC (copy, i) = XVEC (orig, i);
876 if (XVEC (orig, i) != NULL)
878 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
879 for (j = 0; j < XVECLEN (copy, i); j++)
880 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
886 XINT (copy, i) = XINT (orig, i);
890 XWINT (copy, i) = XWINT (orig, i);
895 XSTR (copy, i) = XSTR (orig, i);
905 /* Given a test expression for an attribute, ensure it is validly formed.
906 IS_CONST indicates whether the expression is constant for each compiler
907 run (a constant expression may not test any particular insn).
909 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
910 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
911 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
913 Update the string address in EQ_ATTR expression to be the same used
914 in the attribute (or `alternative_name') to speed up subsequent
915 `find_attr' calls and eliminate most `strcmp' calls.
917 Return the new expression, if any. */
920 check_attr_test (rtx exp, int is_const, int lineno)
922 struct attr_desc *attr;
923 struct attr_value *av;
924 const char *name_ptr, *p;
927 switch (GET_CODE (exp))
930 /* Handle negation test. */
931 if (XSTR (exp, 1)[0] == '!')
932 return check_attr_test (attr_rtx (NOT,
933 attr_eq (XSTR (exp, 0),
937 else if (n_comma_elts (XSTR (exp, 1)) == 1)
939 attr = find_attr (&XSTR (exp, 0), 0);
942 if (! strcmp (XSTR (exp, 0), "alternative"))
943 return mk_attr_alt (1 << atoi (XSTR (exp, 1)));
945 fatal ("unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
948 if (is_const && ! attr->is_const)
949 fatal ("constant expression uses insn attribute `%s' in EQ_ATTR",
952 /* Copy this just to make it permanent,
953 so expressions using it can be permanent too. */
954 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
956 /* It shouldn't be possible to simplify the value given to a
957 constant attribute, so don't expand this until it's time to
958 write the test expression. */
960 ATTR_IND_SIMPLIFIED_P (exp) = 1;
962 if (attr->is_numeric)
964 for (p = XSTR (exp, 1); *p; p++)
966 fatal ("attribute `%s' takes only numeric values",
971 for (av = attr->first_value; av; av = av->next)
972 if (GET_CODE (av->value) == CONST_STRING
973 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
977 fatal ("unknown value `%s' for `%s' attribute",
978 XSTR (exp, 1), XSTR (exp, 0));
983 if (! strcmp (XSTR (exp, 0), "alternative"))
987 name_ptr = XSTR (exp, 1);
988 while ((p = next_comma_elt (&name_ptr)) != NULL)
989 set |= 1 << atoi (p);
991 return mk_attr_alt (set);
995 /* Make an IOR tree of the possible values. */
997 name_ptr = XSTR (exp, 1);
998 while ((p = next_comma_elt (&name_ptr)) != NULL)
1000 newexp = attr_eq (XSTR (exp, 0), p);
1001 orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
1004 return check_attr_test (orexp, is_const, lineno);
1013 /* Either TRUE or FALSE. */
1021 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
1022 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const, lineno);
1026 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
1031 fatal ("RTL operator \"%s\" not valid in constant attribute test",
1032 GET_RTX_NAME (GET_CODE (exp)));
1033 /* These cases can't be simplified. */
1034 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1037 case LE: case LT: case GT: case GE:
1038 case LEU: case LTU: case GTU: case GEU:
1040 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
1041 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
1042 exp = attr_rtx (GET_CODE (exp),
1043 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
1044 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
1045 /* These cases can't be simplified. */
1046 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1052 /* These cases are valid for constant attributes, but can't be
1054 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1055 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1059 fatal ("RTL operator \"%s\" not valid in attribute test",
1060 GET_RTX_NAME (GET_CODE (exp)));
1066 /* Given an expression, ensure that it is validly formed and that all named
1067 attribute values are valid for the given attribute. Issue a fatal error
1068 if not. If no attribute is specified, assume a numeric attribute.
1070 Return a perhaps modified replacement expression for the value. */
1073 check_attr_value (rtx exp, struct attr_desc *attr)
1075 struct attr_value *av;
1079 switch (GET_CODE (exp))
1082 if (attr && ! attr->is_numeric)
1084 message_with_line (attr->lineno,
1085 "CONST_INT not valid for non-numeric attribute %s",
1091 if (INTVAL (exp) < 0 && ! attr->negative_ok)
1093 message_with_line (attr->lineno,
1094 "negative numeric value specified for attribute %s",
1102 if (! strcmp (XSTR (exp, 0), "*"))
1105 if (attr == 0 || attr->is_numeric)
1108 if (attr && attr->negative_ok && *p == '-')
1113 message_with_line (attr ? attr->lineno : 0,
1114 "non-numeric value for numeric attribute %s",
1115 attr ? attr->name : "internal");
1122 for (av = attr->first_value; av; av = av->next)
1123 if (GET_CODE (av->value) == CONST_STRING
1124 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
1129 message_with_line (attr->lineno,
1130 "unknown value `%s' for `%s' attribute",
1131 XSTR (exp, 0), attr ? attr->name : "internal");
1137 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
1138 attr ? attr->is_const : 0,
1139 attr ? attr->lineno : 0);
1140 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1141 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
1149 if (attr && !attr->is_numeric)
1151 message_with_line (attr->lineno,
1152 "invalid operation `%s' for non-numeric attribute value",
1153 GET_RTX_NAME (GET_CODE (exp)));
1161 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1162 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1170 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1174 if (XVECLEN (exp, 0) % 2 != 0)
1176 message_with_line (attr->lineno,
1177 "first operand of COND must have even length");
1182 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1184 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
1185 attr ? attr->is_const : 0,
1186 attr ? attr->lineno : 0);
1187 XVECEXP (exp, 0, i + 1)
1188 = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
1191 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1196 struct attr_desc *attr2 = find_attr (&XSTR (exp, 0), 0);
1199 message_with_line (attr ? attr->lineno : 0,
1200 "unknown attribute `%s' in ATTR",
1204 else if (attr && attr->is_const && ! attr2->is_const)
1206 message_with_line (attr->lineno,
1207 "non-constant attribute `%s' referenced from `%s'",
1208 XSTR (exp, 0), attr->name);
1212 && (attr->is_numeric != attr2->is_numeric
1213 || (! attr->negative_ok && attr2->negative_ok)))
1215 message_with_line (attr->lineno,
1216 "numeric attribute mismatch calling `%s' from `%s'",
1217 XSTR (exp, 0), attr->name);
1224 /* A constant SYMBOL_REF is valid as a constant attribute test and
1225 is expanded later by make_canonical into a COND. In a non-constant
1226 attribute test, it is left be. */
1227 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1230 message_with_line (attr ? attr->lineno : 0,
1231 "invalid operation `%s' for attribute value",
1232 GET_RTX_NAME (GET_CODE (exp)));
1240 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1241 It becomes a COND with each test being (eq_attr "alternative "n") */
1244 convert_set_attr_alternative (rtx exp, struct insn_def *id)
1246 int num_alt = id->num_alternatives;
1250 if (XVECLEN (exp, 1) != num_alt)
1252 message_with_line (id->lineno,
1253 "bad number of entries in SET_ATTR_ALTERNATIVE");
1258 /* Make a COND with all tests but the last. Select the last value via the
1260 condexp = rtx_alloc (COND);
1261 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1263 for (i = 0; i < num_alt - 1; i++)
1266 p = attr_numeral (i);
1268 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1269 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1272 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1274 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1277 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1278 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1281 convert_set_attr (rtx exp, struct insn_def *id)
1284 const char *name_ptr;
1288 /* See how many alternative specified. */
1289 n = n_comma_elts (XSTR (exp, 1));
1291 return attr_rtx (SET,
1292 attr_rtx (ATTR, XSTR (exp, 0)),
1293 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1295 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1296 XSTR (newexp, 0) = XSTR (exp, 0);
1297 XVEC (newexp, 1) = rtvec_alloc (n);
1299 /* Process each comma-separated name. */
1300 name_ptr = XSTR (exp, 1);
1302 while ((p = next_comma_elt (&name_ptr)) != NULL)
1303 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1305 return convert_set_attr_alternative (newexp, id);
1308 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1309 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1315 struct insn_def *id;
1316 struct attr_desc *attr;
1320 for (id = defs; id; id = id->next)
1322 if (XVEC (id->def, id->vec_idx) == NULL)
1325 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1327 value = XVECEXP (id->def, id->vec_idx, i);
1328 switch (GET_CODE (value))
1331 if (GET_CODE (XEXP (value, 0)) != ATTR)
1333 message_with_line (id->lineno, "bad attribute set");
1339 case SET_ATTR_ALTERNATIVE:
1340 value = convert_set_attr_alternative (value, id);
1344 value = convert_set_attr (value, id);
1348 message_with_line (id->lineno, "invalid attribute code %s",
1349 GET_RTX_NAME (GET_CODE (value)));
1353 if (value == NULL_RTX)
1356 if ((attr = find_attr (&XSTR (XEXP (value, 0), 0), 0)) == NULL)
1358 message_with_line (id->lineno, "unknown attribute %s",
1359 XSTR (XEXP (value, 0), 0));
1364 XVECEXP (id->def, id->vec_idx, i) = value;
1365 XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1370 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1371 expressions by converting them into a COND. This removes cases from this
1372 program. Also, replace an attribute value of "*" with the default attribute
1376 make_canonical (struct attr_desc *attr, rtx exp)
1381 switch (GET_CODE (exp))
1384 exp = make_numeric_value (INTVAL (exp));
1388 if (! strcmp (XSTR (exp, 0), "*"))
1390 if (attr == 0 || attr->default_val == 0)
1391 fatal ("(attr_value \"*\") used in invalid context");
1392 exp = attr->default_val->value;
1395 XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1400 if (!attr->is_const || ATTR_IND_SIMPLIFIED_P (exp))
1402 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1403 This makes the COND something that won't be considered an arbitrary
1404 expression by walk_attr_value. */
1405 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1406 exp = check_attr_value (exp, attr);
1410 newexp = rtx_alloc (COND);
1411 XVEC (newexp, 0) = rtvec_alloc (2);
1412 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1413 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1415 XEXP (newexp, 1) = XEXP (exp, 2);
1418 /* Fall through to COND case since this is now a COND. */
1425 /* First, check for degenerate COND. */
1426 if (XVECLEN (exp, 0) == 0)
1427 return make_canonical (attr, XEXP (exp, 1));
1428 defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1430 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1432 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1433 XVECEXP (exp, 0, i + 1)
1434 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1435 if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1451 copy_boolean (rtx exp)
1453 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1454 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1455 copy_boolean (XEXP (exp, 1)));
1456 if (GET_CODE (exp) == MATCH_OPERAND)
1458 XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1459 XSTR (exp, 2) = DEF_ATTR_STRING (XSTR (exp, 2));
1461 else if (GET_CODE (exp) == EQ_ATTR)
1463 XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1464 XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1470 /* Given a value and an attribute description, return a `struct attr_value *'
1471 that represents that value. This is either an existing structure, if the
1472 value has been previously encountered, or a newly-created structure.
1474 `insn_code' is the code of an insn whose attribute has the specified
1475 value (-2 if not processing an insn). We ensure that all insns for
1476 a given value have the same number of alternatives if the value checks
1479 static struct attr_value *
1480 get_attr_value (rtx value, struct attr_desc *attr, int insn_code)
1482 struct attr_value *av;
1485 value = make_canonical (attr, value);
1486 if (compares_alternatives_p (value))
1488 if (insn_code < 0 || insn_alternatives == NULL)
1489 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1491 num_alt = insn_alternatives[insn_code];
1494 for (av = attr->first_value; av; av = av->next)
1495 if (rtx_equal_p (value, av->value)
1496 && (num_alt == 0 || av->first_insn == NULL
1497 || insn_alternatives[av->first_insn->insn_code]))
1500 av = oballoc (sizeof (struct attr_value));
1502 av->next = attr->first_value;
1503 attr->first_value = av;
1504 av->first_insn = NULL;
1506 av->has_asm_insn = 0;
1511 /* After all DEFINE_DELAYs have been read in, create internal attributes
1512 to generate the required routines.
1514 First, we compute the number of delay slots for each insn (as a COND of
1515 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1516 delay type is specified, we compute a similar function giving the
1517 DEFINE_DELAY ordinal for each insn.
1519 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1520 tells whether a given insn can be in that delay slot.
1522 Normal attribute filling and optimization expands these to contain the
1523 information needed to handle delay slots. */
1526 expand_delays (void)
1528 struct delay_desc *delay;
1534 /* First, generate data for `num_delay_slots' function. */
1536 condexp = rtx_alloc (COND);
1537 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1538 XEXP (condexp, 1) = make_numeric_value (0);
1540 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1542 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1543 XVECEXP (condexp, 0, i + 1)
1544 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1547 make_internal_attr (num_delay_slots_str, condexp, ATTR_NONE);
1549 /* If more than one delay type, do the same for computing the delay type. */
1552 condexp = rtx_alloc (COND);
1553 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1554 XEXP (condexp, 1) = make_numeric_value (0);
1556 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1558 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1559 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1562 make_internal_attr (delay_type_str, condexp, ATTR_SPECIAL);
1565 /* For each delay possibility and delay slot, compute an eligibility
1566 attribute for non-annulled insns and for each type of annulled (annul
1567 if true and annul if false). */
1568 for (delay = delays; delay; delay = delay->next)
1570 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1572 condexp = XVECEXP (delay->def, 1, i);
1574 condexp = false_rtx;
1575 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1576 make_numeric_value (1), make_numeric_value (0));
1578 p = attr_printf (sizeof "*delay__" + MAX_DIGITS * 2,
1579 "*delay_%d_%d", delay->num, i / 3);
1580 make_internal_attr (p, newexp, ATTR_SPECIAL);
1582 if (have_annul_true)
1584 condexp = XVECEXP (delay->def, 1, i + 1);
1585 if (condexp == 0) condexp = false_rtx;
1586 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1587 make_numeric_value (1),
1588 make_numeric_value (0));
1589 p = attr_printf (sizeof "*annul_true__" + MAX_DIGITS * 2,
1590 "*annul_true_%d_%d", delay->num, i / 3);
1591 make_internal_attr (p, newexp, ATTR_SPECIAL);
1594 if (have_annul_false)
1596 condexp = XVECEXP (delay->def, 1, i + 2);
1597 if (condexp == 0) condexp = false_rtx;
1598 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1599 make_numeric_value (1),
1600 make_numeric_value (0));
1601 p = attr_printf (sizeof "*annul_false__" + MAX_DIGITS * 2,
1602 "*annul_false_%d_%d", delay->num, i / 3);
1603 make_internal_attr (p, newexp, ATTR_SPECIAL);
1609 /* This function is given a left and right side expression and an operator.
1610 Each side is a conditional expression, each alternative of which has a
1611 numerical value. The function returns another conditional expression
1612 which, for every possible set of condition values, returns a value that is
1613 the operator applied to the values of the two sides.
1615 Since this is called early, it must also support IF_THEN_ELSE. */
1618 operate_exp (enum operator op, rtx left, rtx right)
1620 int left_value, right_value;
1624 /* If left is a string, apply operator to it and the right side. */
1625 if (GET_CODE (left) == CONST_STRING)
1627 /* If right is also a string, just perform the operation. */
1628 if (GET_CODE (right) == CONST_STRING)
1630 left_value = atoi (XSTR (left, 0));
1631 right_value = atoi (XSTR (right, 0));
1635 i = left_value + right_value;
1639 i = left_value - right_value;
1642 case POS_MINUS_OP: /* The positive part of LEFT - RIGHT. */
1643 if (left_value > right_value)
1644 i = left_value - right_value;
1651 i = left_value | right_value;
1655 i = left_value == right_value;
1659 i = (left_value << (HOST_BITS_PER_INT / 2)) | right_value;
1663 if (left_value > right_value)
1670 if (left_value < right_value)
1680 if (i == left_value)
1682 if (i == right_value)
1684 return make_numeric_value (i);
1686 else if (GET_CODE (right) == IF_THEN_ELSE)
1688 /* Apply recursively to all values within. */
1689 rtx newleft = operate_exp (op, left, XEXP (right, 1));
1690 rtx newright = operate_exp (op, left, XEXP (right, 2));
1691 if (rtx_equal_p (newleft, newright))
1693 return attr_rtx (IF_THEN_ELSE, XEXP (right, 0), newleft, newright);
1695 else if (GET_CODE (right) == COND)
1700 newexp = rtx_alloc (COND);
1701 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (right, 0));
1702 defval = XEXP (newexp, 1) = operate_exp (op, left, XEXP (right, 1));
1704 for (i = 0; i < XVECLEN (right, 0); i += 2)
1706 XVECEXP (newexp, 0, i) = XVECEXP (right, 0, i);
1707 XVECEXP (newexp, 0, i + 1)
1708 = operate_exp (op, left, XVECEXP (right, 0, i + 1));
1709 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1714 /* If the resulting cond is trivial (all alternatives
1715 give the same value), optimize it away. */
1717 return operate_exp (op, left, XEXP (right, 1));
1722 fatal ("badly formed attribute value");
1725 /* A hack to prevent expand_units from completely blowing up: ORX_OP does
1726 not associate through IF_THEN_ELSE. */
1727 else if (op == ORX_OP && GET_CODE (right) == IF_THEN_ELSE)
1729 return attr_rtx (IOR, left, right);
1732 /* Otherwise, do recursion the other way. */
1733 else if (GET_CODE (left) == IF_THEN_ELSE)
1735 rtx newleft = operate_exp (op, XEXP (left, 1), right);
1736 rtx newright = operate_exp (op, XEXP (left, 2), right);
1737 if (rtx_equal_p (newleft, newright))
1739 return attr_rtx (IF_THEN_ELSE, XEXP (left, 0), newleft, newright);
1741 else if (GET_CODE (left) == COND)
1746 newexp = rtx_alloc (COND);
1747 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (left, 0));
1748 defval = XEXP (newexp, 1) = operate_exp (op, XEXP (left, 1), right);
1750 for (i = 0; i < XVECLEN (left, 0); i += 2)
1752 XVECEXP (newexp, 0, i) = XVECEXP (left, 0, i);
1753 XVECEXP (newexp, 0, i + 1)
1754 = operate_exp (op, XVECEXP (left, 0, i + 1), right);
1755 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1760 /* If the cond is trivial (all alternatives give the same value),
1761 optimize it away. */
1763 return operate_exp (op, XEXP (left, 1), right);
1765 /* If the result is the same as the LEFT operand,
1767 if (rtx_equal_p (newexp, left))
1774 fatal ("badly formed attribute value");
1779 /* Once all attributes and DEFINE_FUNCTION_UNITs have been read, we
1780 construct a number of attributes.
1782 The first produces a function `function_units_used' which is given an
1783 insn and produces an encoding showing which function units are required
1784 for the execution of that insn. If the value is non-negative, the insn
1785 uses that unit; otherwise, the value is a one's complement mask of units
1788 The second produces a function `result_ready_cost' which is used to
1789 determine the time that the result of an insn will be ready and hence
1790 a worst-case schedule.
1792 Both of these produce quite complex expressions which are then set as the
1793 default value of internal attributes. Normal attribute simplification
1794 should produce reasonable expressions.
1796 For each unit, a `<name>_unit_ready_cost' function will take an
1797 insn and give the delay until that unit will be ready with the result
1798 and a `<name>_unit_conflict_cost' function is given an insn already
1799 executing on the unit and a candidate to execute and will give the
1800 cost from the time the executing insn started until the candidate
1801 can start (ignore limitations on the number of simultaneous insns).
1803 For each unit, a `<name>_unit_blockage' function is given an insn
1804 already executing on the unit and a candidate to execute and will
1805 give the delay incurred due to function unit conflicts. The range of
1806 blockage cost values for a given executing insn is given by the
1807 `<name>_unit_blockage_range' function. These values are encoded in
1808 an int where the upper half gives the minimum value and the lower
1809 half gives the maximum value. */
1814 struct function_unit *unit, **unit_num;
1815 struct function_unit_op *op, **op_array, ***unit_ops;
1820 int i, j, u, num, nvalues;
1822 /* Rebuild the condition for the unit to share the RTL expressions.
1823 Sharing is required by simplify_by_exploding. Build the issue delay
1824 expressions. Validate the expressions we were given for the conditions
1825 and conflict vector. Then make attributes for use in the conflict
1828 for (unit = units; unit; unit = unit->next)
1830 unit->condexp = check_attr_test (unit->condexp, 0, unit->first_lineno);
1832 for (op = unit->ops; op; op = op->next)
1834 rtx issue_delay = make_numeric_value (op->issue_delay);
1835 rtx issue_exp = issue_delay;
1837 /* Build, validate, and simplify the issue delay expression. */
1838 if (op->conflict_exp != true_rtx)
1839 issue_exp = attr_rtx (IF_THEN_ELSE, op->conflict_exp,
1840 issue_exp, make_numeric_value (0));
1841 issue_exp = check_attr_value (make_canonical (NULL_ATTR,
1844 issue_exp = simplify_knowing (issue_exp, unit->condexp);
1845 op->issue_exp = issue_exp;
1847 /* Make an attribute for use in the conflict function if needed. */
1848 unit->needs_conflict_function = (unit->issue_delay.min
1849 != unit->issue_delay.max);
1850 if (unit->needs_conflict_function)
1852 str = attr_printf ((strlen (unit->name) + sizeof "*_cost_"
1854 "*%s_cost_%d", unit->name, op->num);
1855 make_internal_attr (str, issue_exp, ATTR_SPECIAL);
1858 /* Validate the condition. */
1859 op->condexp = check_attr_test (op->condexp, 0, op->lineno);
1863 /* Compute the mask of function units used. Initially, the unitsmask is
1864 zero. Set up a conditional to compute each unit's contribution. */
1865 unitsmask = make_numeric_value (0);
1866 newexp = rtx_alloc (IF_THEN_ELSE);
1867 XEXP (newexp, 2) = make_numeric_value (0);
1869 /* If we have just a few units, we may be all right expanding the whole
1870 thing. But the expansion is 2**N in space on the number of opclasses,
1871 so we can't do this for very long -- Alpha and MIPS in particular have
1872 problems with this. So in that situation, we fall back on an alternate
1873 implementation method. */
1874 #define NUM_UNITOP_CUTOFF 20
1876 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1878 /* Merge each function unit into the unit mask attributes. */
1879 for (unit = units; unit; unit = unit->next)
1881 XEXP (newexp, 0) = unit->condexp;
1882 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1883 unitsmask = operate_exp (OR_OP, unitsmask, newexp);
1888 /* Merge each function unit into the unit mask attributes. */
1889 for (unit = units; unit; unit = unit->next)
1891 XEXP (newexp, 0) = unit->condexp;
1892 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1893 unitsmask = operate_exp (ORX_OP, unitsmask, attr_copy_rtx (newexp));
1897 /* Simplify the unit mask expression, encode it, and make an attribute
1898 for the function_units_used function. */
1899 unitsmask = simplify_by_exploding (unitsmask);
1901 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1902 unitsmask = encode_units_mask (unitsmask);
1905 /* We can no longer encode unitsmask at compile time, so emit code to
1906 calculate it at runtime. Rather, put a marker for where we'd do
1907 the code, and actually output it in write_attr_get(). */
1908 unitsmask = attr_rtx (FFS, unitsmask);
1911 make_internal_attr ("*function_units_used", unitsmask,
1912 (ATTR_NEGATIVE_OK | ATTR_FUNC_UNITS));
1914 /* Create an array of ops for each unit. Add an extra unit for the
1915 result_ready_cost function that has the ops of all other units. */
1916 unit_ops = xmalloc ((num_units + 1) * sizeof (struct function_unit_op **));
1917 unit_num = xmalloc ((num_units + 1) * sizeof (struct function_unit *));
1919 unit_num[num_units] = unit = xmalloc (sizeof (struct function_unit));
1920 unit->num = num_units;
1921 unit->num_opclasses = 0;
1923 for (unit = units; unit; unit = unit->next)
1925 unit_num[num_units]->num_opclasses += unit->num_opclasses;
1926 unit_num[unit->num] = unit;
1927 unit_ops[unit->num] = op_array =
1928 xmalloc (unit->num_opclasses * sizeof (struct function_unit_op *));
1930 for (op = unit->ops; op; op = op->next)
1931 op_array[op->num] = op;
1934 /* Compose the array of ops for the extra unit. */
1935 unit_ops[num_units] = op_array =
1936 xmalloc (unit_num[num_units]->num_opclasses
1937 * sizeof (struct function_unit_op *));
1939 for (unit = units, i = 0; unit; i += unit->num_opclasses, unit = unit->next)
1940 memcpy (&op_array[i], unit_ops[unit->num],
1941 unit->num_opclasses * sizeof (struct function_unit_op *));
1943 /* Compute the ready cost function for each unit by computing the
1944 condition for each non-default value. */
1945 for (u = 0; u <= num_units; u++)
1951 op_array = unit_ops[unit->num];
1952 num = unit->num_opclasses;
1954 /* Sort the array of ops into increasing ready cost order. */
1955 for (i = 0; i < num; i++)
1956 for (j = num - 1; j > i; j--)
1957 if (op_array[j - 1]->ready < op_array[j]->ready)
1960 op_array[j] = op_array[j - 1];
1961 op_array[j - 1] = op;
1964 /* Determine how many distinct non-default ready cost values there
1965 are. We use a default ready cost value of 1. */
1966 nvalues = 0; value = 1;
1967 for (i = num - 1; i >= 0; i--)
1968 if (op_array[i]->ready > value)
1970 value = op_array[i]->ready;
1975 readycost = make_numeric_value (1);
1978 /* Construct the ready cost expression as a COND of each value from
1979 the largest to the smallest. */
1980 readycost = rtx_alloc (COND);
1981 XVEC (readycost, 0) = rtvec_alloc (nvalues * 2);
1982 XEXP (readycost, 1) = make_numeric_value (1);
1986 value = op_array[0]->ready;
1987 for (i = 0; i < num; i++)
1992 else if (op->ready == value)
1993 orexp = insert_right_side (IOR, orexp, op->condexp, -2, -2);
1996 XVECEXP (readycost, 0, nvalues * 2) = orexp;
1997 XVECEXP (readycost, 0, nvalues * 2 + 1)
1998 = make_numeric_value (value);
2001 orexp = op->condexp;
2004 XVECEXP (readycost, 0, nvalues * 2) = orexp;
2005 XVECEXP (readycost, 0, nvalues * 2 + 1) = make_numeric_value (value);
2010 rtx max_blockage = 0, min_blockage = 0;
2012 /* Simplify the readycost expression by only considering insns
2013 that use the unit. */
2014 readycost = simplify_knowing (readycost, unit->condexp);
2016 /* Determine the blockage cost the executing insn (E) given
2017 the candidate insn (C). This is the maximum of the issue
2018 delay, the pipeline delay, and the simultaneity constraint.
2019 Each function_unit_op represents the characteristics of the
2020 candidate insn, so in the expressions below, C is a known
2021 term and E is an unknown term.
2023 We compute the blockage cost for each E for every possible C.
2024 Thus OP represents E, and READYCOST is a list of values for
2027 The issue delay function for C is op->issue_exp and is used to
2028 write the `<name>_unit_conflict_cost' function. Symbolically
2029 this is "ISSUE-DELAY (E,C)".
2031 The pipeline delay results form the FIFO constraint on the
2032 function unit and is "READY-COST (E) + 1 - READY-COST (C)".
2034 The simultaneity constraint is based on how long it takes to
2035 fill the unit given the minimum issue delay. FILL-TIME is the
2036 constant "MIN (ISSUE-DELAY (*,*)) * (SIMULTANEITY - 1)", and
2037 the simultaneity constraint is "READY-COST (E) - FILL-TIME"
2038 if SIMULTANEITY is nonzero and zero otherwise.
2040 Thus, BLOCKAGE (E,C) when SIMULTANEITY is zero is
2042 MAX (ISSUE-DELAY (E,C),
2043 READY-COST (E) - (READY-COST (C) - 1))
2047 MAX (ISSUE-DELAY (E,C),
2048 READY-COST (E) - (READY-COST (C) - 1),
2049 READY-COST (E) - FILL-TIME)
2051 The `<name>_unit_blockage' function is computed by determining
2052 this value for each candidate insn. As these values are
2053 computed, we also compute the upper and lower bounds for
2054 BLOCKAGE (E,*). These are combined to form the function
2055 `<name>_unit_blockage_range'. Finally, the maximum blockage
2056 cost, MAX (BLOCKAGE (*,*)), is computed. */
2058 for (op = unit->ops; op; op = op->next)
2060 rtx blockage = op->issue_exp;
2061 blockage = simplify_knowing (blockage, unit->condexp);
2063 /* Add this op's contribution to MAX (BLOCKAGE (E,*)) and
2064 MIN (BLOCKAGE (E,*)). */
2065 if (max_blockage == 0)
2066 max_blockage = min_blockage = blockage;
2070 = simplify_knowing (operate_exp (MAX_OP, max_blockage,
2074 = simplify_knowing (operate_exp (MIN_OP, min_blockage,
2079 /* Make an attribute for use in the blockage function. */
2080 str = attr_printf ((strlen (unit->name) + sizeof "*_block_"
2082 "*%s_block_%d", unit->name, op->num);
2083 make_internal_attr (str, blockage, ATTR_SPECIAL);
2086 /* Record MAX (BLOCKAGE (*,*)). */
2089 unit->max_blockage = max_attr_value (max_blockage, &unknown);
2092 /* See if the upper and lower bounds of BLOCKAGE (E,*) are the
2093 same. If so, the blockage function carries no additional
2094 information and is not written. */
2095 newexp = operate_exp (EQ_OP, max_blockage, min_blockage);
2096 newexp = simplify_knowing (newexp, unit->condexp);
2097 unit->needs_blockage_function
2098 = (GET_CODE (newexp) != CONST_STRING
2099 || atoi (XSTR (newexp, 0)) != 1);
2101 /* If the all values of BLOCKAGE (E,C) have the same value,
2102 neither blockage function is written. */
2103 unit->needs_range_function
2104 = (unit->needs_blockage_function
2105 || GET_CODE (max_blockage) != CONST_STRING);
2107 if (unit->needs_range_function)
2109 /* Compute the blockage range function and make an attribute
2110 for writing its value. */
2111 newexp = operate_exp (RANGE_OP, min_blockage, max_blockage);
2112 newexp = simplify_knowing (newexp, unit->condexp);
2114 str = attr_printf ((strlen (unit->name)
2115 + sizeof "*_unit_blockage_range"),
2116 "*%s_unit_blockage_range", unit->name);
2117 make_internal_attr (str, newexp, (ATTR_STATIC|ATTR_BLOCKAGE|ATTR_UNSIGNED));
2120 str = attr_printf (strlen (unit->name) + sizeof "*_unit_ready_cost",
2121 "*%s_unit_ready_cost", unit->name);
2122 make_internal_attr (str, readycost, ATTR_STATIC);
2126 /* Make an attribute for the ready_cost function. Simplifying
2127 further with simplify_by_exploding doesn't win. */
2128 str = "*result_ready_cost";
2129 make_internal_attr (str, readycost, ATTR_NONE);
2133 /* For each unit that requires a conflict cost function, make an attribute
2134 that maps insns to the operation number. */
2135 for (unit = units; unit; unit = unit->next)
2139 if (! unit->needs_conflict_function
2140 && ! unit->needs_blockage_function)
2143 caseexp = rtx_alloc (COND);
2144 XVEC (caseexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
2146 for (op = unit->ops; op; op = op->next)
2148 /* Make our adjustment to the COND being computed. If we are the
2149 last operation class, place our values into the default of the
2151 if (op->num == unit->num_opclasses - 1)
2153 XEXP (caseexp, 1) = make_numeric_value (op->num);
2157 XVECEXP (caseexp, 0, op->num * 2) = op->condexp;
2158 XVECEXP (caseexp, 0, op->num * 2 + 1)
2159 = make_numeric_value (op->num);
2163 /* Simplifying caseexp with simplify_by_exploding doesn't win. */
2164 str = attr_printf (strlen (unit->name) + sizeof "*_cases",
2165 "*%s_cases", unit->name);
2166 make_internal_attr (str, caseexp, ATTR_SPECIAL);
2170 /* Simplify EXP given KNOWN_TRUE. */
2173 simplify_knowing (rtx exp, rtx known_true)
2175 if (GET_CODE (exp) != CONST_STRING)
2177 int unknown = 0, max;
2178 max = max_attr_value (exp, &unknown);
2181 exp = attr_rtx (IF_THEN_ELSE, known_true, exp,
2182 make_numeric_value (max));
2183 exp = simplify_by_exploding (exp);
2189 /* Translate the CONST_STRING expressions in X to change the encoding of
2190 value. On input, the value is a bitmask with a one bit for each unit
2191 used; on output, the value is the unit number (zero based) if one
2192 and only one unit is used or the one's complement of the bitmask. */
2195 encode_units_mask (rtx x)
2202 code = GET_CODE (x);
2207 i = atoi (XSTR (x, 0));
2209 /* The sign bit encodes a one's complement mask. */
2211 else if (i != 0 && i == (i & -i))
2212 /* Only one bit is set, so yield that unit number. */
2213 for (j = 0; (i >>= 1) != 0; j++)
2217 return attr_rtx (CONST_STRING, attr_printf (MAX_DIGITS, "%d", j));
2236 /* Compare the elements. If any pair of corresponding elements
2237 fail to match, return 0 for the whole things. */
2239 fmt = GET_RTX_FORMAT (code);
2240 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2246 for (j = 0; j < XVECLEN (x, i); j++)
2247 XVECEXP (x, i, j) = encode_units_mask (XVECEXP (x, i, j));
2251 XEXP (x, i) = encode_units_mask (XEXP (x, i));
2258 /* Once all attributes and insns have been read and checked, we construct for
2259 each attribute value a list of all the insns that have that value for
2263 fill_attr (struct attr_desc *attr)
2265 struct attr_value *av;
2266 struct insn_ent *ie;
2267 struct insn_def *id;
2271 /* Don't fill constant attributes. The value is independent of
2272 any particular insn. */
2276 for (id = defs; id; id = id->next)
2278 /* If no value is specified for this insn for this attribute, use the
2281 if (XVEC (id->def, id->vec_idx))
2282 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
2283 if (! strcmp_check (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
2285 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
2288 av = attr->default_val;
2290 av = get_attr_value (value, attr, id->insn_code);
2292 ie = oballoc (sizeof (struct insn_ent));
2293 ie->insn_code = id->insn_code;
2294 ie->insn_index = id->insn_code;
2295 insert_insn_ent (av, ie);
2299 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
2300 test that checks relative positions of insns (uses MATCH_DUP or PC).
2301 If so, replace it with what is obtained by passing the expression to
2302 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
2303 recursively on each value (including the default value). Otherwise,
2304 return the value returned by NO_ADDRESS_FN applied to EXP. */
2307 substitute_address (rtx exp, rtx (*no_address_fn) (rtx),
2308 rtx (*address_fn) (rtx))
2313 if (GET_CODE (exp) == COND)
2315 /* See if any tests use addresses. */
2317 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2318 walk_attr_value (XVECEXP (exp, 0, i));
2321 return (*address_fn) (exp);
2323 /* Make a new copy of this COND, replacing each element. */
2324 newexp = rtx_alloc (COND);
2325 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
2326 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2328 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
2329 XVECEXP (newexp, 0, i + 1)
2330 = substitute_address (XVECEXP (exp, 0, i + 1),
2331 no_address_fn, address_fn);
2334 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
2335 no_address_fn, address_fn);
2340 else if (GET_CODE (exp) == IF_THEN_ELSE)
2343 walk_attr_value (XEXP (exp, 0));
2345 return (*address_fn) (exp);
2347 return attr_rtx (IF_THEN_ELSE,
2348 substitute_address (XEXP (exp, 0),
2349 no_address_fn, address_fn),
2350 substitute_address (XEXP (exp, 1),
2351 no_address_fn, address_fn),
2352 substitute_address (XEXP (exp, 2),
2353 no_address_fn, address_fn));
2356 return (*no_address_fn) (exp);
2359 /* Make new attributes from the `length' attribute. The following are made,
2360 each corresponding to a function called from `shorten_branches' or
2363 *insn_default_length This is the length of the insn to be returned
2364 by `get_attr_length' before `shorten_branches'
2365 has been called. In each case where the length
2366 depends on relative addresses, the largest
2367 possible is used. This routine is also used
2368 to compute the initial size of the insn.
2370 *insn_variable_length_p This returns 1 if the insn's length depends
2371 on relative addresses, zero otherwise.
2373 *insn_current_length This is only called when it is known that the
2374 insn has a variable length and returns the
2375 current length, based on relative addresses.
2379 make_length_attrs (void)
2381 static const char *new_names[] =
2383 "*insn_default_length",
2384 "*insn_variable_length_p",
2385 "*insn_current_length"
2387 static rtx (*const no_address_fn[]) (rtx) = {identity_fn, zero_fn, zero_fn};
2388 static rtx (*const address_fn[]) (rtx) = {max_fn, one_fn, identity_fn};
2390 struct attr_desc *length_attr, *new_attr;
2391 struct attr_value *av, *new_av;
2392 struct insn_ent *ie, *new_ie;
2394 /* See if length attribute is defined. If so, it must be numeric. Make
2395 it special so we don't output anything for it. */
2396 length_attr = find_attr (&length_str, 0);
2397 if (length_attr == 0)
2400 if (! length_attr->is_numeric)
2401 fatal ("length attribute must be numeric");
2403 length_attr->is_const = 0;
2404 length_attr->is_special = 1;
2406 /* Make each new attribute, in turn. */
2407 for (i = 0; i < ARRAY_SIZE (new_names); i++)
2409 make_internal_attr (new_names[i],
2410 substitute_address (length_attr->default_val->value,
2411 no_address_fn[i], address_fn[i]),
2413 new_attr = find_attr (&new_names[i], 0);
2414 for (av = length_attr->first_value; av; av = av->next)
2415 for (ie = av->first_insn; ie; ie = ie->next)
2417 new_av = get_attr_value (substitute_address (av->value,
2420 new_attr, ie->insn_code);
2421 new_ie = oballoc (sizeof (struct insn_ent));
2422 new_ie->insn_code = ie->insn_code;
2423 new_ie->insn_index = ie->insn_index;
2424 insert_insn_ent (new_av, new_ie);
2429 /* Utility functions called from above routine. */
2432 identity_fn (rtx exp)
2438 zero_fn (rtx exp ATTRIBUTE_UNUSED)
2440 return make_numeric_value (0);
2444 one_fn (rtx exp ATTRIBUTE_UNUSED)
2446 return make_numeric_value (1);
2453 return make_numeric_value (max_attr_value (exp, &unknown));
2457 write_length_unit_log (void)
2459 struct attr_desc *length_attr = find_attr (&length_str, 0);
2460 struct attr_value *av;
2461 struct insn_ent *ie;
2462 unsigned int length_unit_log, length_or;
2465 if (length_attr == 0)
2467 length_or = or_attr_value (length_attr->default_val->value, &unknown);
2468 for (av = length_attr->first_value; av; av = av->next)
2469 for (ie = av->first_insn; ie; ie = ie->next)
2470 length_or |= or_attr_value (av->value, &unknown);
2473 length_unit_log = 0;
2476 length_or = ~length_or;
2477 for (length_unit_log = 0; length_or & 1; length_or >>= 1)
2480 printf ("int length_unit_log = %u;\n", length_unit_log);
2483 /* Take a COND expression and see if any of the conditions in it can be
2484 simplified. If any are known true or known false for the particular insn
2485 code, the COND can be further simplified.
2487 Also call ourselves on any COND operations that are values of this COND.
2489 We do not modify EXP; rather, we make and return a new rtx. */
2492 simplify_cond (rtx exp, int insn_code, int insn_index)
2495 /* We store the desired contents here,
2496 then build a new expression if they don't match EXP. */
2497 rtx defval = XEXP (exp, 1);
2498 rtx new_defval = XEXP (exp, 1);
2499 int len = XVECLEN (exp, 0);
2500 rtx *tests = xmalloc (len * sizeof (rtx));
2504 /* This lets us free all storage allocated below, if appropriate. */
2505 obstack_finish (rtl_obstack);
2507 memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
2509 /* See if default value needs simplification. */
2510 if (GET_CODE (defval) == COND)
2511 new_defval = simplify_cond (defval, insn_code, insn_index);
2513 /* Simplify the subexpressions, and see what tests we can get rid of. */
2515 for (i = 0; i < len; i += 2)
2517 rtx newtest, newval;
2519 /* Simplify this test. */
2520 newtest = simplify_test_exp_in_temp (tests[i], insn_code, insn_index);
2523 newval = tests[i + 1];
2524 /* See if this value may need simplification. */
2525 if (GET_CODE (newval) == COND)
2526 newval = simplify_cond (newval, insn_code, insn_index);
2528 /* Look for ways to delete or combine this test. */
2529 if (newtest == true_rtx)
2531 /* If test is true, make this value the default
2532 and discard this + any following tests. */
2534 defval = tests[i + 1];
2535 new_defval = newval;
2538 else if (newtest == false_rtx)
2540 /* If test is false, discard it and its value. */
2541 for (j = i; j < len - 2; j++)
2542 tests[j] = tests[j + 2];
2547 else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
2549 /* If this value and the value for the prev test are the same,
2553 = insert_right_side (IOR, tests[i - 2], newtest,
2554 insn_code, insn_index);
2556 /* Delete this test/value. */
2557 for (j = i; j < len - 2; j++)
2558 tests[j] = tests[j + 2];
2564 tests[i + 1] = newval;
2567 /* If the last test in a COND has the same value
2568 as the default value, that test isn't needed. */
2570 while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
2573 /* See if we changed anything. */
2574 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
2577 for (i = 0; i < len; i++)
2578 if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
2586 if (GET_CODE (defval) == COND)
2587 ret = simplify_cond (defval, insn_code, insn_index);
2595 rtx newexp = rtx_alloc (COND);
2597 XVEC (newexp, 0) = rtvec_alloc (len);
2598 memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
2599 XEXP (newexp, 1) = new_defval;
2606 /* Remove an insn entry from an attribute value. */
2609 remove_insn_ent (struct attr_value *av, struct insn_ent *ie)
2611 struct insn_ent *previe;
2613 if (av->first_insn == ie)
2614 av->first_insn = ie->next;
2617 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
2619 previe->next = ie->next;
2623 if (ie->insn_code == -1)
2624 av->has_asm_insn = 0;
2629 /* Insert an insn entry in an attribute value list. */
2632 insert_insn_ent (struct attr_value *av, struct insn_ent *ie)
2634 ie->next = av->first_insn;
2635 av->first_insn = ie;
2637 if (ie->insn_code == -1)
2638 av->has_asm_insn = 1;
2643 /* This is a utility routine to take an expression that is a tree of either
2644 AND or IOR expressions and insert a new term. The new term will be
2645 inserted at the right side of the first node whose code does not match
2646 the root. A new node will be created with the root's code. Its left
2647 side will be the old right side and its right side will be the new
2650 If the `term' is itself a tree, all its leaves will be inserted. */
2653 insert_right_side (enum rtx_code code, rtx exp, rtx term, int insn_code, int insn_index)
2657 /* Avoid consing in some special cases. */
2658 if (code == AND && term == true_rtx)
2660 if (code == AND && term == false_rtx)
2662 if (code == AND && exp == true_rtx)
2664 if (code == AND && exp == false_rtx)
2666 if (code == IOR && term == true_rtx)
2668 if (code == IOR && term == false_rtx)
2670 if (code == IOR && exp == true_rtx)
2672 if (code == IOR && exp == false_rtx)
2674 if (attr_equal_p (exp, term))
2677 if (GET_CODE (term) == code)
2679 exp = insert_right_side (code, exp, XEXP (term, 0),
2680 insn_code, insn_index);
2681 exp = insert_right_side (code, exp, XEXP (term, 1),
2682 insn_code, insn_index);
2687 if (GET_CODE (exp) == code)
2689 rtx new = insert_right_side (code, XEXP (exp, 1),
2690 term, insn_code, insn_index);
2691 if (new != XEXP (exp, 1))
2692 /* Make a copy of this expression and call recursively. */
2693 newexp = attr_rtx (code, XEXP (exp, 0), new);
2699 /* Insert the new term. */
2700 newexp = attr_rtx (code, exp, term);
2703 return simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2706 /* If we have an expression which AND's a bunch of
2707 (not (eq_attrq "alternative" "n"))
2708 terms, we may have covered all or all but one of the possible alternatives.
2709 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
2711 This routine is passed an expression and either AND or IOR. It returns a
2712 bitmask indicating which alternatives are mentioned within EXP. */
2715 compute_alternative_mask (rtx exp, enum rtx_code code)
2718 if (GET_CODE (exp) == code)
2719 return compute_alternative_mask (XEXP (exp, 0), code)
2720 | compute_alternative_mask (XEXP (exp, 1), code);
2722 else if (code == AND && GET_CODE (exp) == NOT
2723 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2724 && XSTR (XEXP (exp, 0), 0) == alternative_name)
2725 string = XSTR (XEXP (exp, 0), 1);
2727 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
2728 && XSTR (exp, 0) == alternative_name)
2729 string = XSTR (exp, 1);
2731 else if (GET_CODE (exp) == EQ_ATTR_ALT)
2733 if (code == AND && XINT (exp, 1))
2734 return XINT (exp, 0);
2736 if (code == IOR && !XINT (exp, 1))
2737 return XINT (exp, 0);
2745 return 1 << (string[0] - '0');
2746 return 1 << atoi (string);
2749 /* Given I, a single-bit mask, return RTX to compare the `alternative'
2750 attribute with the value represented by that bit. */
2753 make_alternative_compare (int mask)
2755 return mk_attr_alt (mask);
2758 /* If we are processing an (eq_attr "attr" "value") test, we find the value
2759 of "attr" for this insn code. From that value, we can compute a test
2760 showing when the EQ_ATTR will be true. This routine performs that
2761 computation. If a test condition involves an address, we leave the EQ_ATTR
2762 intact because addresses are only valid for the `length' attribute.
2764 EXP is the EQ_ATTR expression and VALUE is the value of that attribute
2765 for the insn corresponding to INSN_CODE and INSN_INDEX. */
2768 evaluate_eq_attr (rtx exp, rtx value, int insn_code, int insn_index)
2775 if (GET_CODE (value) == CONST_STRING)
2777 if (! strcmp_check (XSTR (value, 0), XSTR (exp, 1)))
2782 else if (GET_CODE (value) == SYMBOL_REF)
2787 if (GET_CODE (exp) != EQ_ATTR)
2790 if (strlen (XSTR (exp, 0)) + strlen (XSTR (exp, 1)) + 2 > 256)
2793 strcpy (string, XSTR (exp, 0));
2794 strcat (string, "_");
2795 strcat (string, XSTR (exp, 1));
2796 for (p = string; *p; p++)
2799 newexp = attr_rtx (EQ, value,
2800 attr_rtx (SYMBOL_REF,
2801 DEF_ATTR_STRING (string)));
2803 else if (GET_CODE (value) == COND)
2805 /* We construct an IOR of all the cases for which the requested attribute
2806 value is present. Since we start with FALSE, if it is not present,
2807 FALSE will be returned.
2809 Each case is the AND of the NOT's of the previous conditions with the
2810 current condition; in the default case the current condition is TRUE.
2812 For each possible COND value, call ourselves recursively.
2814 The extra TRUE and FALSE expressions will be eliminated by another
2815 call to the simplification routine. */
2820 if (current_alternative_string)
2821 clear_struct_flag (value);
2823 for (i = 0; i < XVECLEN (value, 0); i += 2)
2825 rtx this = simplify_test_exp_in_temp (XVECEXP (value, 0, i),
2826 insn_code, insn_index);
2828 SIMPLIFY_ALTERNATIVE (this);
2830 right = insert_right_side (AND, andexp, this,
2831 insn_code, insn_index);
2832 right = insert_right_side (AND, right,
2833 evaluate_eq_attr (exp,
2836 insn_code, insn_index),
2837 insn_code, insn_index);
2838 orexp = insert_right_side (IOR, orexp, right,
2839 insn_code, insn_index);
2841 /* Add this condition into the AND expression. */
2842 newexp = attr_rtx (NOT, this);
2843 andexp = insert_right_side (AND, andexp, newexp,
2844 insn_code, insn_index);
2847 /* Handle the default case. */
2848 right = insert_right_side (AND, andexp,
2849 evaluate_eq_attr (exp, XEXP (value, 1),
2850 insn_code, insn_index),
2851 insn_code, insn_index);
2852 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2857 /* If uses an address, must return original expression. But set the
2858 ATTR_IND_SIMPLIFIED_P bit so we don't try to simplify it again. */
2861 walk_attr_value (newexp);
2865 /* This had `&& current_alternative_string', which seems to be wrong. */
2866 if (! ATTR_IND_SIMPLIFIED_P (exp))
2867 return copy_rtx_unchanging (exp);
2874 /* This routine is called when an AND of a term with a tree of AND's is
2875 encountered. If the term or its complement is present in the tree, it
2876 can be replaced with TRUE or FALSE, respectively.
2878 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2879 be true and hence are complementary.
2881 There is one special case: If we see
2882 (and (not (eq_attr "att" "v1"))
2883 (eq_attr "att" "v2"))
2884 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2885 replace the term, not anything in the AND tree. So we pass a pointer to
2889 simplify_and_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2894 int left_eliminates_term, right_eliminates_term;
2896 if (GET_CODE (exp) == AND)
2898 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2899 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2900 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2902 newexp = attr_rtx (AND, left, right);
2904 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2908 else if (GET_CODE (exp) == IOR)
2910 /* For the IOR case, we do the same as above, except that we can
2911 only eliminate `term' if both sides of the IOR would do so. */
2913 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2914 left_eliminates_term = (temp == true_rtx);
2917 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2918 right_eliminates_term = (temp == true_rtx);
2920 if (left_eliminates_term && right_eliminates_term)
2923 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2925 newexp = attr_rtx (IOR, left, right);
2927 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2931 /* Check for simplifications. Do some extra checking here since this
2932 routine is called so many times. */
2937 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2940 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2943 else if (GET_CODE (exp) == EQ_ATTR_ALT && GET_CODE (*pterm) == EQ_ATTR_ALT)
2945 if (attr_alt_subset_p (*pterm, exp))
2948 if (attr_alt_subset_of_compl_p (*pterm, exp))
2951 if (attr_alt_subset_p (exp, *pterm))
2957 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2959 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2962 if (! strcmp_check (XSTR (exp, 1), XSTR (*pterm, 1)))
2968 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2969 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2971 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2974 if (! strcmp_check (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2980 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2981 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2983 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2986 if (! strcmp_check (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2992 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2994 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2998 else if (GET_CODE (exp) == NOT)
3000 if (attr_equal_p (XEXP (exp, 0), *pterm))
3004 else if (GET_CODE (*pterm) == NOT)
3006 if (attr_equal_p (XEXP (*pterm, 0), exp))
3010 else if (attr_equal_p (exp, *pterm))
3016 /* Similar to `simplify_and_tree', but for IOR trees. */
3019 simplify_or_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
3024 int left_eliminates_term, right_eliminates_term;
3026 if (GET_CODE (exp) == IOR)
3028 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
3029 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
3030 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3032 newexp = attr_rtx (GET_CODE (exp), left, right);
3034 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
3038 else if (GET_CODE (exp) == AND)
3040 /* For the AND case, we do the same as above, except that we can
3041 only eliminate `term' if both sides of the AND would do so. */
3043 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
3044 left_eliminates_term = (temp == false_rtx);
3047 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
3048 right_eliminates_term = (temp == false_rtx);
3050 if (left_eliminates_term && right_eliminates_term)
3053 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3055 newexp = attr_rtx (GET_CODE (exp), left, right);
3057 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
3061 if (attr_equal_p (exp, *pterm))
3064 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
3067 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
3070 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
3071 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3072 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
3075 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
3076 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
3077 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
3083 /* Compute approximate cost of the expression. Used to decide whether
3084 expression is cheap enough for inline. */
3086 attr_rtx_cost (rtx x)
3092 code = GET_CODE (x);
3105 /* Alternatives don't result into function call. */
3106 if (!strcmp_check (XSTR (x, 0), alternative_name))
3113 const char *fmt = GET_RTX_FORMAT (code);
3114 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3120 for (j = 0; j < XVECLEN (x, i); j++)
3121 cost += attr_rtx_cost (XVECEXP (x, i, j));
3124 cost += attr_rtx_cost (XEXP (x, i));
3134 /* Simplify test expression and use temporary obstack in order to avoid
3135 memory bloat. Use ATTR_IND_SIMPLIFIED to avoid unnecessary simplifications
3136 and avoid unnecessary copying if possible. */
3139 simplify_test_exp_in_temp (rtx exp, int insn_code, int insn_index)
3142 struct obstack *old;
3143 if (ATTR_IND_SIMPLIFIED_P (exp))
3146 rtl_obstack = temp_obstack;
3147 x = simplify_test_exp (exp, insn_code, insn_index);
3149 if (x == exp || rtl_obstack == temp_obstack)
3151 return attr_copy_rtx (x);
3154 /* Returns true if S1 is a subset of S2. */
3157 attr_alt_subset_p (rtx s1, rtx s2)
3159 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
3162 return !(XINT (s1, 0) &~ XINT (s2, 0));
3165 return !(XINT (s1, 0) & XINT (s2, 0));
3171 return !(XINT (s2, 0) &~ XINT (s1, 0));
3178 /* Returns true if S1 is a subset of complement of S2. */
3180 static bool attr_alt_subset_of_compl_p (rtx s1, rtx s2)
3182 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
3185 return !(XINT (s1, 0) & XINT (s2, 0));
3188 return !(XINT (s1, 0) & ~XINT (s2, 0));
3191 return !(XINT (s2, 0) &~ XINT (s1, 0));
3201 /* Return EQ_ATTR_ALT expression representing intersection of S1 and S2. */
3204 attr_alt_intersection (rtx s1, rtx s2)
3206 rtx result = rtx_alloc (EQ_ATTR_ALT);
3208 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
3211 XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
3214 XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
3217 XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
3220 XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
3225 XINT (result, 1) = XINT (s1, 1) & XINT (s2, 1);
3230 /* Return EQ_ATTR_ALT expression representing union of S1 and S2. */
3233 attr_alt_union (rtx s1, rtx s2)
3235 rtx result = rtx_alloc (EQ_ATTR_ALT);
3237 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
3240 XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
3243 XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
3246 XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
3249 XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
3255 XINT (result, 1) = XINT (s1, 1) | XINT (s2, 1);
3259 /* Return EQ_ATTR_ALT expression representing complement of S. */
3262 attr_alt_complement (rtx s)
3264 rtx result = rtx_alloc (EQ_ATTR_ALT);
3266 XINT (result, 0) = XINT (s, 0);
3267 XINT (result, 1) = 1 - XINT (s, 1);
3272 /* Tests whether a bit B belongs to the set represented by S. */
3275 attr_alt_bit_p (rtx s, int b)
3277 return XINT (s, 1) ^ ((XINT (s, 0) >> b) & 1);
3280 /* Return EQ_ATTR_ALT expression representing set containing elements set
3286 rtx result = rtx_alloc (EQ_ATTR_ALT);
3288 XINT (result, 0) = e;
3289 XINT (result, 1) = 0;
3294 /* Given an expression, see if it can be simplified for a particular insn
3295 code based on the values of other attributes being tested. This can
3296 eliminate nested get_attr_... calls.
3298 Note that if an endless recursion is specified in the patterns, the
3299 optimization will loop. However, it will do so in precisely the cases where
3300 an infinite recursion loop could occur during compilation. It's better that
3304 simplify_test_exp (rtx exp, int insn_code, int insn_index)
3307 struct attr_desc *attr;
3308 struct attr_value *av;
3309 struct insn_ent *ie;
3312 bool left_alt, right_alt;
3314 /* Don't re-simplify something we already simplified. */
3315 if (ATTR_IND_SIMPLIFIED_P (exp) || ATTR_CURR_SIMPLIFIED_P (exp))
3318 switch (GET_CODE (exp))
3321 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3322 SIMPLIFY_ALTERNATIVE (left);
3323 if (left == false_rtx)
3325 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3326 SIMPLIFY_ALTERNATIVE (right);
3327 if (left == false_rtx)
3330 if (GET_CODE (left) == EQ_ATTR_ALT
3331 && GET_CODE (right) == EQ_ATTR_ALT)
3333 exp = attr_alt_intersection (left, right);
3334 return simplify_test_exp (exp, insn_code, insn_index);
3337 /* If either side is an IOR and we have (eq_attr "alternative" ..")
3338 present on both sides, apply the distributive law since this will
3339 yield simplifications. */
3340 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
3341 && compute_alternative_mask (left, IOR)
3342 && compute_alternative_mask (right, IOR))
3344 if (GET_CODE (left) == IOR)
3351 newexp = attr_rtx (IOR,
3352 attr_rtx (AND, left, XEXP (right, 0)),
3353 attr_rtx (AND, left, XEXP (right, 1)));
3355 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3358 /* Try with the term on both sides. */
3359 right = simplify_and_tree (right, &left, insn_code, insn_index);
3360 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3361 left = simplify_and_tree (left, &right, insn_code, insn_index);
3363 if (left == false_rtx || right == false_rtx)
3365 else if (left == true_rtx)
3369 else if (right == true_rtx)
3373 /* See if all or all but one of the insn's alternatives are specified
3374 in this tree. Optimize if so. */
3376 if (GET_CODE (left) == NOT)
3377 left_alt = (GET_CODE (XEXP (left, 0)) == EQ_ATTR
3378 && XSTR (XEXP (left, 0), 0) == alternative_name);
3380 left_alt = (GET_CODE (left) == EQ_ATTR_ALT
3383 if (GET_CODE (right) == NOT)
3384 right_alt = (GET_CODE (XEXP (right, 0)) == EQ_ATTR
3385 && XSTR (XEXP (right, 0), 0) == alternative_name);
3387 right_alt = (GET_CODE (right) == EQ_ATTR_ALT
3388 && XINT (right, 1));
3391 && (GET_CODE (left) == AND
3393 || GET_CODE (right) == AND
3396 i = compute_alternative_mask (exp, AND);
3397 if (i & ~insn_alternatives[insn_code])
3398 fatal ("invalid alternative specified for pattern number %d",
3401 /* If all alternatives are excluded, this is false. */
3402 i ^= insn_alternatives[insn_code];
3405 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3407 /* If just one excluded, AND a comparison with that one to the
3408 front of the tree. The others will be eliminated by
3409 optimization. We do not want to do this if the insn has one
3410 alternative and we have tested none of them! */
3411 left = make_alternative_compare (i);
3412 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3413 newexp = attr_rtx (AND, left, right);
3415 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3419 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3421 newexp = attr_rtx (AND, left, right);
3422 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3427 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3428 SIMPLIFY_ALTERNATIVE (left);
3429 if (left == true_rtx)
3431 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3432 SIMPLIFY_ALTERNATIVE (right);
3433 if (right == true_rtx)
3436 if (GET_CODE (left) == EQ_ATTR_ALT
3437 && GET_CODE (right) == EQ_ATTR_ALT)
3439 exp = attr_alt_union (left, right);
3440 return simplify_test_exp (exp, insn_code, insn_index);
3443 right = simplify_or_tree (right, &left, insn_code, insn_index);
3444 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3445 left = simplify_or_tree (left, &right, insn_code, insn_index);
3447 if (right == true_rtx || left == true_rtx)
3449 else if (left == false_rtx)
3453 else if (right == false_rtx)
3458 /* Test for simple cases where the distributive law is useful. I.e.,
3459 convert (ior (and (x) (y))
3465 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
3466 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
3468 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
3470 left = XEXP (left, 0);
3472 newexp = attr_rtx (AND, left, right);
3473 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3476 /* See if all or all but one of the insn's alternatives are specified
3477 in this tree. Optimize if so. */
3479 else if (insn_code >= 0
3480 && (GET_CODE (left) == IOR
3481 || (GET_CODE (left) == EQ_ATTR_ALT
3483 || (GET_CODE (left) == EQ_ATTR
3484 && XSTR (left, 0) == alternative_name)
3485 || GET_CODE (right) == IOR
3486 || (GET_CODE (right) == EQ_ATTR_ALT
3487 && !XINT (right, 1))
3488 || (GET_CODE (right) == EQ_ATTR
3489 && XSTR (right, 0) == alternative_name)))
3491 i = compute_alternative_mask (exp, IOR);
3492 if (i & ~insn_alternatives[insn_code])
3493 fatal ("invalid alternative specified for pattern number %d",
3496 /* If all alternatives are included, this is true. */
3497 i ^= insn_alternatives[insn_code];
3500 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3502 /* If just one excluded, IOR a comparison with that one to the
3503 front of the tree. The others will be eliminated by
3504 optimization. We do not want to do this if the insn has one
3505 alternative and we have tested none of them! */
3506 left = make_alternative_compare (i);
3507 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3508 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
3510 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3514 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3516 newexp = attr_rtx (IOR, left, right);
3517 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3522 if (GET_CODE (XEXP (exp, 0)) == NOT)
3524 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
3525 insn_code, insn_index);
3526 SIMPLIFY_ALTERNATIVE (left);
3530 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3531 SIMPLIFY_ALTERNATIVE (left);
3532 if (GET_CODE (left) == NOT)
3533 return XEXP (left, 0);
3535 if (left == false_rtx)
3537 if (left == true_rtx)
3540 if (GET_CODE (left) == EQ_ATTR_ALT)
3542 exp = attr_alt_complement (left);
3543 return simplify_test_exp (exp, insn_code, insn_index);
3546 /* Try to apply De`Morgan's laws. */
3547 if (GET_CODE (left) == IOR)
3549 newexp = attr_rtx (AND,
3550 attr_rtx (NOT, XEXP (left, 0)),
3551 attr_rtx (NOT, XEXP (left, 1)));
3553 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3555 else if (GET_CODE (left) == AND)
3557 newexp = attr_rtx (IOR,
3558 attr_rtx (NOT, XEXP (left, 0)),
3559 attr_rtx (NOT, XEXP (left, 1)));
3561 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3563 else if (left != XEXP (exp, 0))
3565 newexp = attr_rtx (NOT, left);
3570 if (current_alternative_string)
3571 return attr_alt_bit_p (exp, atoi (current_alternative_string)) ? true_rtx : false_rtx;
3574 return XINT (exp, 1) ? true_rtx : false_rtx;
3578 if (current_alternative_string && XSTR (exp, 0) == alternative_name)
3579 return (XSTR (exp, 1) == current_alternative_string
3580 ? true_rtx : false_rtx);
3582 if (XSTR (exp, 0) == alternative_name)
3584 newexp = mk_attr_alt (1 << atoi (XSTR (exp, 1)));
3588 /* Look at the value for this insn code in the specified attribute.
3589 We normally can replace this comparison with the condition that
3590 would give this insn the values being tested for. */
3591 if (XSTR (exp, 0) != alternative_name
3592 && (attr = find_attr (&XSTR (exp, 0), 0)) != NULL)
3593 for (av = attr->first_value; av; av = av->next)
3594 for (ie = av->first_insn; ie; ie = ie->next)
3595 if (ie->insn_code == insn_code)
3598 x = evaluate_eq_attr (exp, av->value, insn_code, insn_index);
3599 x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
3600 if (attr_rtx_cost(x) < 20)
3609 /* We have already simplified this expression. Simplifying it again
3610 won't buy anything unless we weren't given a valid insn code
3611 to process (i.e., we are canonicalizing something.). */
3612 if (insn_code != -2 /* Seems wrong: && current_alternative_string. */
3613 && ! ATTR_IND_SIMPLIFIED_P (newexp))
3614 return copy_rtx_unchanging (newexp);
3619 /* Optimize the attribute lists by seeing if we can determine conditional
3620 values from the known values of other attributes. This will save subroutine
3621 calls during the compilation. */
3624 optimize_attrs (void)
3626 struct attr_desc *attr;
3627 struct attr_value *av;
3628 struct insn_ent *ie;
3631 struct attr_value_list
3633 struct attr_value *av;
3634 struct insn_ent *ie;
3635 struct attr_desc *attr;
3636 struct attr_value_list *next;
3638 struct attr_value_list **insn_code_values;
3639 struct attr_value_list *ivbuf;
3640 struct attr_value_list *iv;
3642 /* For each insn code, make a list of all the insn_ent's for it,
3643 for all values for all attributes. */
3645 if (num_insn_ents == 0)
3648 /* Make 2 extra elements, for "code" values -2 and -1. */
3649 insn_code_values = xcalloc ((insn_code_number + 2),
3650 sizeof (struct attr_value_list *));
3652 /* Offset the table address so we can index by -2 or -1. */
3653 insn_code_values += 2;
3655 iv = ivbuf = xmalloc (num_insn_ents * sizeof (struct attr_value_list));
3657 for (i = 0; i < MAX_ATTRS_INDEX; i++)
3658 for (attr = attrs[i]; attr; attr = attr->next)
3659 for (av = attr->first_value; av; av = av->next)
3660 for (ie = av->first_insn; ie; ie = ie->next)
3665 iv->next = insn_code_values[ie->insn_code];
3666 insn_code_values[ie->insn_code] = iv;
3670 /* Sanity check on num_insn_ents. */
3671 if (iv != ivbuf + num_insn_ents)
3674 /* Process one insn code at a time. */
3675 for (i = -2; i < insn_code_number; i++)
3677 /* Clear the ATTR_CURR_SIMPLIFIED_P flag everywhere relevant.
3678 We use it to mean "already simplified for this insn". */
3679 for (iv = insn_code_values[i]; iv; iv = iv->next)
3680 clear_struct_flag (iv->av->value);
3682 for (iv = insn_code_values[i]; iv; iv = iv->next)
3684 struct obstack *old = rtl_obstack;
3689 if (GET_CODE (av->value) != COND)
3692 rtl_obstack = temp_obstack;
3694 while (GET_CODE (newexp) == COND)
3696 rtx newexp2 = simplify_cond (newexp, ie->insn_code,
3698 if (newexp2 == newexp)
3704 if (newexp != av->value)
3706 newexp = attr_copy_rtx (newexp);
3707 remove_insn_ent (av, ie);
3708 av = get_attr_value (newexp, attr, ie->insn_code);
3710 insert_insn_ent (av, ie);
3716 free (insn_code_values - 2);
3719 /* If EXP is a suitable expression, reorganize it by constructing an
3720 equivalent expression that is a COND with the tests being all combinations
3721 of attribute values and the values being simple constants. */
3724 simplify_by_exploding (rtx exp)
3726 rtx list = 0, link, condexp, defval = NULL_RTX;
3727 struct dimension *space;
3728 rtx *condtest, *condval;
3729 int i, j, total, ndim = 0;
3730 int most_tests, num_marks, new_marks;
3733 /* Locate all the EQ_ATTR expressions. */
3734 if (! find_and_mark_used_attributes (exp, &list, &ndim) || ndim == 0)
3736 unmark_used_attributes (list, 0, 0);
3740 /* Create an attribute space from the list of used attributes. For each
3741 dimension in the attribute space, record the attribute, list of values
3742 used, and number of values used. Add members to the list of values to
3743 cover the domain of the attribute. This makes the expanded COND form
3744 order independent. */
3746 space = xmalloc (ndim * sizeof (struct dimension));
3749 for (ndim = 0; list; ndim++)
3751 /* Pull the first attribute value from the list and record that
3752 attribute as another dimension in the attribute space. */
3753 const char *name = XSTR (XEXP (list, 0), 0);
3756 space[ndim].attr = find_attr (&name, 0);
3757 XSTR (XEXP (list, 0), 0) = name;
3759 if (space[ndim].attr == 0
3760 || space[ndim].attr->is_numeric)
3762 unmark_used_attributes (list, space, ndim);
3766 /* Add all remaining attribute values that refer to this attribute. */
3767 space[ndim].num_values = 0;
3768 space[ndim].values = 0;
3770 for (link = list; link; link = *prev)
3771 if (! strcmp_check (XSTR (XEXP (link, 0), 0), name))
3773 space[ndim].num_values++;
3774 *prev = XEXP (link, 1);
3775 XEXP (link, 1) = space[ndim].values;
3776 space[ndim].values = link;
3779 prev = &XEXP (link, 1);
3781 /* Add sufficient members to the list of values to make the list
3782 mutually exclusive and record the total size of the attribute
3784 total *= add_values_to_cover (&space[ndim]);
3787 /* Sort the attribute space so that the attributes go from non-constant
3788 to constant and from most values to least values. */
3789 for (i = 0; i < ndim; i++)
3790 for (j = ndim - 1; j > i; j--)
3791 if ((space[j-1].attr->is_const && !space[j].attr->is_const)
3792 || space[j-1].num_values < space[j].num_values)
3794 struct dimension tmp;
3796 space[j] = space[j - 1];
3800 /* Establish the initial current value. */
3801 for (i = 0; i < ndim; i++)
3802 space[i].current_value = space[i].values;
3804 condtest = xmalloc (total * sizeof (rtx));
3805 condval = xmalloc (total * sizeof (rtx));
3807 /* Expand the tests and values by iterating over all values in the
3811 condtest[i] = test_for_current_value (space, ndim);
3812 condval[i] = simplify_with_current_value (exp, space, ndim);
3813 if (! increment_current_value (space, ndim))
3819 /* We are now finished with the original expression. */
3820 unmark_used_attributes (0, space, ndim);
3823 /* Find the most used constant value and make that the default. */
3825 for (i = num_marks = 0; i < total; i++)
3826 if (GET_CODE (condval[i]) == CONST_STRING
3827 && ! ATTR_EQ_ATTR_P (condval[i]))
3829 /* Mark the unmarked constant value and count how many are marked. */
3830 ATTR_EQ_ATTR_P (condval[i]) = 1;
3831 for (j = new_marks = 0; j < total; j++)
3832 if (GET_CODE (condval[j]) == CONST_STRING
3833 && ATTR_EQ_ATTR_P (condval[j]))
3835 if (new_marks - num_marks > most_tests)
3837 most_tests = new_marks - num_marks;
3838 defval = condval[i];
3840 num_marks = new_marks;
3842 /* Clear all the marks. */
3843 for (i = 0; i < total; i++)
3844 ATTR_EQ_ATTR_P (condval[i]) = 0;
3846 /* Give up if nothing is constant. */
3850 /* If all values are the default, use that. */
3851 else if (total == most_tests)
3854 /* Make a COND with the most common constant value the default. (A more
3855 complex method where tests with the same value were combined didn't
3856 seem to improve things.) */
3859 condexp = rtx_alloc (COND);
3860 XVEC (condexp, 0) = rtvec_alloc ((total - most_tests) * 2);
3861 XEXP (condexp, 1) = defval;
3862 for (i = j = 0; i < total; i++)
3863 if (condval[i] != defval)
3865 XVECEXP (condexp, 0, 2 * j) = condtest[i];
3866 XVECEXP (condexp, 0, 2 * j + 1) = condval[i];
3876 /* Set the ATTR_EQ_ATTR_P flag for all EQ_ATTR expressions in EXP and
3877 verify that EXP can be simplified to a constant term if all the EQ_ATTR
3878 tests have known value. */
3881 find_and_mark_used_attributes (rtx exp, rtx *terms, int *nterms)
3885 switch (GET_CODE (exp))
3888 if (! ATTR_EQ_ATTR_P (exp))
3890 rtx link = rtx_alloc (EXPR_LIST);
3891 XEXP (link, 0) = exp;
3892 XEXP (link, 1) = *terms;
3895 ATTR_EQ_ATTR_P (exp) = 1;
3904 if (! find_and_mark_used_attributes (XEXP (exp, 2), terms, nterms))
3908 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3911 if (! find_and_mark_used_attributes (XEXP (exp, 0), terms, nterms))
3916 for (i = 0; i < XVECLEN (exp, 0); i++)
3917 if (! find_and_mark_used_attributes (XVECEXP (exp, 0, i), terms, nterms))
3919 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3928 /* Clear the ATTR_EQ_ATTR_P flag in all EQ_ATTR expressions on LIST and
3929 in the values of the NDIM-dimensional attribute space SPACE. */
3932 unmark_used_attributes (rtx list, struct dimension *space, int ndim)
3937 for (i = 0; i < ndim; i++)
3938 unmark_used_attributes (space[i].values, 0, 0);
3940 for (link = list; link; link = XEXP (link, 1))
3942 exp = XEXP (link, 0);
3943 if (GET_CODE (exp) == EQ_ATTR)
3944 ATTR_EQ_ATTR_P (exp) = 0;
3948 /* Update the attribute dimension DIM so that all values of the attribute
3949 are tested. Return the updated number of values. */
3952 add_values_to_cover (struct dimension *dim)
3954 struct attr_value *av;
3955 rtx exp, link, *prev;
3958 for (av = dim->attr->first_value; av; av = av->next)
3959 if (GET_CODE (av->value) == CONST_STRING)
3962 if (nalt < dim->num_values)
3964 else if (nalt == dim->num_values)
3967 else if (nalt * 2 < dim->num_values * 3)
3969 /* Most all the values of the attribute are used, so add all the unused
3971 prev = &dim->values;
3972 for (link = dim->values; link; link = *prev)
3973 prev = &XEXP (link, 1);
3975 for (av = dim->attr->first_value; av; av = av->next)
3976 if (GET_CODE (av->value) == CONST_STRING)
3978 exp = attr_eq (dim->attr->name, XSTR (av->value, 0));
3979 if (ATTR_EQ_ATTR_P (exp))
3982 link = rtx_alloc (EXPR_LIST);
3983 XEXP (link, 0) = exp;
3986 prev = &XEXP (link, 1);
3988 dim->num_values = nalt;
3992 rtx orexp = false_rtx;
3994 /* Very few values are used, so compute a mutually exclusive
3995 expression. (We could do this for numeric values if that becomes
3997 prev = &dim->values;
3998 for (link = dim->values; link; link = *prev)
4000 orexp = insert_right_side (IOR, orexp, XEXP (link, 0), -2, -2);
4001 prev = &XEXP (link, 1);
4003 link = rtx_alloc (EXPR_LIST);
4004 XEXP (link, 0) = attr_rtx (NOT, orexp);
4009 return dim->num_values;
4012 /* Increment the current value for the NDIM-dimensional attribute space SPACE
4013 and return FALSE if the increment overflowed. */
4016 increment_current_value (struct dimension *space, int ndim)
4020 for (i = ndim - 1; i >= 0; i--)
4022 if ((space[i].current_value = XEXP (space[i].current_value, 1)) == 0)
4023 space[i].current_value = space[i].values;
4030 /* Construct an expression corresponding to the current value for the
4031 NDIM-dimensional attribute space SPACE. */
4034 test_for_current_value (struct dimension *space, int ndim)
4039 for (i = 0; i < ndim; i++)
4040 exp = insert_right_side (AND, exp, XEXP (space[i].current_value, 0),
4046 /* Given the current value of the NDIM-dimensional attribute space SPACE,
4047 set the corresponding EQ_ATTR expressions to that value and reduce
4048 the expression EXP as much as possible. On input [and output], all
4049 known EQ_ATTR expressions are set to FALSE. */
4052 simplify_with_current_value (rtx exp, struct dimension *space, int ndim)
4057 /* Mark each current value as TRUE. */
4058 for (i = 0; i < ndim; i++)
4060 x = XEXP (space[i].current_value, 0);
4061 if (GET_CODE (x) == EQ_ATTR)
4062 ATTR_EQ_ATTR_P (x) = 0;
4065 exp = simplify_with_current_value_aux (exp);
4067 /* Change each current value back to FALSE. */
4068 for (i = 0; i < ndim; i++)
4070 x = XEXP (space[i].current_value, 0);
4071 if (GET_CODE (x) == EQ_ATTR)
4072 ATTR_EQ_ATTR_P (x) = 1;
4078 /* Reduce the expression EXP based on the ATTR_EQ_ATTR_P settings of
4079 all EQ_ATTR expressions. */
4082 simplify_with_current_value_aux (rtx exp)
4087 switch (GET_CODE (exp))
4090 if (ATTR_EQ_ATTR_P (exp))
4099 cond = simplify_with_current_value_aux (XEXP (exp, 0));
4100 if (cond == true_rtx)
4101 return simplify_with_current_value_aux (XEXP (exp, 1));
4102 else if (cond == false_rtx)
4103 return simplify_with_current_value_aux (XEXP (exp, 2));
4105 return attr_rtx (IF_THEN_ELSE, cond,
4106 simplify_with_current_value_aux (XEXP (exp, 1)),
4107 simplify_with_current_value_aux (XEXP (exp, 2)));
4110 cond = simplify_with_current_value_aux (XEXP (exp, 1));
4111 if (cond == true_rtx)
4113 else if (cond == false_rtx)
4114 return simplify_with_current_value_aux (XEXP (exp, 0));
4116 return attr_rtx (IOR, cond,
4117 simplify_with_current_value_aux (XEXP (exp, 0)));
4120 cond = simplify_with_current_value_aux (XEXP (exp, 1));
4121 if (cond == true_rtx)
4122 return simplify_with_current_value_aux (XEXP (exp, 0));
4123 else if (cond == false_rtx)
4126 return attr_rtx (AND, cond,
4127 simplify_with_current_value_aux (XEXP (exp, 0)));
4130 cond = simplify_with_current_value_aux (XEXP (exp, 0));
4131 if (cond == true_rtx)
4133 else if (cond == false_rtx)
4136 return attr_rtx (NOT, cond);
4139 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4141 cond = simplify_with_current_value_aux (XVECEXP (exp, 0, i));
4142 if (cond == true_rtx)
4143 return simplify_with_current_value_aux (XVECEXP (exp, 0, i + 1));
4144 else if (cond == false_rtx)
4147 abort (); /* With all EQ_ATTR's of known value, a case should
4148 have been selected. */
4150 return simplify_with_current_value_aux (XEXP (exp, 1));
4157 /* Clear the ATTR_CURR_SIMPLIFIED_P flag in EXP and its subexpressions. */
4160 clear_struct_flag (rtx x)
4167 ATTR_CURR_SIMPLIFIED_P (x) = 0;
4168 if (ATTR_IND_SIMPLIFIED_P (x))
4171 code = GET_CODE (x);
4192 /* Compare the elements. If any pair of corresponding elements
4193 fail to match, return 0 for the whole things. */
4195 fmt = GET_RTX_FORMAT (code);
4196 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4202 for (j = 0; j < XVECLEN (x, i); j++)
4203 clear_struct_flag (XVECEXP (x, i, j));
4207 clear_struct_flag (XEXP (x, i));
4213 /* Create table entries for DEFINE_ATTR. */
4216 gen_attr (rtx exp, int lineno)
4218 struct attr_desc *attr;
4219 struct attr_value *av;
4220 const char *name_ptr;
4223 /* Make a new attribute structure. Check for duplicate by looking at
4224 attr->default_val, since it is initialized by this routine. */
4225 attr = find_attr (&XSTR (exp, 0), 1);
4226 if (attr->default_val)
4228 message_with_line (lineno, "duplicate definition for attribute %s",
4230 message_with_line (attr->lineno, "previous definition");
4234 attr->lineno = lineno;
4236 if (*XSTR (exp, 1) == '\0')
4237 attr->is_numeric = 1;
4240 name_ptr = XSTR (exp, 1);
4241 while ((p = next_comma_elt (&name_ptr)) != NULL)
4243 av = oballoc (sizeof (struct attr_value));
4244 av->value = attr_rtx (CONST_STRING, p);
4245 av->next = attr->first_value;
4246 attr->first_value = av;
4247 av->first_insn = NULL;
4249 av->has_asm_insn = 0;
4253 if (GET_CODE (XEXP (exp, 2)) == CONST)
4256 if (attr->is_numeric)
4258 message_with_line (lineno,
4259 "constant attributes may not take numeric values");
4263 /* Get rid of the CONST node. It is allowed only at top-level. */
4264 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
4267 if (! strcmp_check (attr->name, length_str) && ! attr->is_numeric)
4269 message_with_line (lineno,
4270 "`length' attribute must take numeric values");
4274 /* Set up the default value. */
4275 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
4276 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
4279 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
4280 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
4281 number of alternatives as this should be checked elsewhere. */
4284 count_alternatives (rtx exp)
4289 if (GET_CODE (exp) == MATCH_OPERAND)
4290 return n_comma_elts (XSTR (exp, 2));
4292 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4293 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4298 n = count_alternatives (XEXP (exp, i));
4305 if (XVEC (exp, i) != NULL)
4306 for (j = 0; j < XVECLEN (exp, i); j++)
4308 n = count_alternatives (XVECEXP (exp, i, j));
4317 /* Returns nonzero if the given expression contains an EQ_ATTR with the
4318 `alternative' attribute. */
4321 compares_alternatives_p (rtx exp)
4326 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
4329 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4330 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4335 if (compares_alternatives_p (XEXP (exp, i)))
4340 for (j = 0; j < XVECLEN (exp, i); j++)
4341 if (compares_alternatives_p (XVECEXP (exp, i, j)))
4349 /* Returns nonzero is INNER is contained in EXP. */
4352 contained_in_p (rtx inner, rtx exp)
4357 if (rtx_equal_p (inner, exp))
4360 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4361 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4366 if (contained_in_p (inner, XEXP (exp, i)))
4371 for (j = 0; j < XVECLEN (exp, i); j++)
4372 if (contained_in_p (inner, XVECEXP (exp, i, j)))
4380 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
4383 gen_insn (rtx exp, int lineno)
4385 struct insn_def *id;
4387 id = oballoc (sizeof (struct insn_def));
4391 id->lineno = lineno;
4393 switch (GET_CODE (exp))
4396 id->insn_code = insn_code_number;
4397 id->insn_index = insn_index_number;
4398 id->num_alternatives = count_alternatives (exp);
4399 if (id->num_alternatives == 0)
4400 id->num_alternatives = 1;
4404 case DEFINE_PEEPHOLE:
4405 id->insn_code = insn_code_number;
4406 id->insn_index = insn_index_number;
4407 id->num_alternatives = count_alternatives (exp);
4408 if (id->num_alternatives == 0)
4409 id->num_alternatives = 1;
4413 case DEFINE_ASM_ATTRIBUTES:
4415 id->insn_index = -1;
4416 id->num_alternatives = 1;
4418 got_define_asm_attributes = 1;
4426 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
4427 true or annul false is specified, and make a `struct delay_desc'. */
4430 gen_delay (rtx def, int lineno)
4432 struct delay_desc *delay;
4435 if (XVECLEN (def, 1) % 3 != 0)
4437 message_with_line (lineno,
4438 "number of elements in DEFINE_DELAY must be multiple of three");
4443 for (i = 0; i < XVECLEN (def, 1); i += 3)
4445 if (XVECEXP (def, 1, i + 1))
4446 have_annul_true = 1;
4447 if (XVECEXP (def, 1, i + 2))
4448 have_annul_false = 1;
4451 delay = oballoc (sizeof (struct delay_desc));
4453 delay->num = ++num_delays;
4454 delay->next = delays;
4455 delay->lineno = lineno;
4459 /* Process a DEFINE_FUNCTION_UNIT.
4461 This gives information about a function unit contained in the CPU.
4462 We fill in a `struct function_unit_op' and a `struct function_unit'
4463 with information used later by `expand_unit'. */
4466 gen_unit (rtx def, int lineno)
4468 struct function_unit *unit;
4469 struct function_unit_op *op;
4470 const char *name = XSTR (def, 0);
4471 int multiplicity = XINT (def, 1);
4472 int simultaneity = XINT (def, 2);
4473 rtx condexp = XEXP (def, 3);
4474 int ready_cost = MAX (XINT (def, 4), 1);
4475 int issue_delay = MAX (XINT (def, 5), 1);
4477 /* See if we have already seen this function unit. If so, check that
4478 the multiplicity and simultaneity values are the same. If not, make
4479 a structure for this function unit. */
4480 for (unit = units; unit; unit = unit->next)
4481 if (! strcmp (unit->name, name))
4483 if (unit->multiplicity != multiplicity
4484 || unit->simultaneity != simultaneity)
4486 message_with_line (lineno,
4487 "differing specifications given for function unit %s",
4489 message_with_line (unit->first_lineno, "previous definition");
4498 unit = oballoc (sizeof (struct function_unit));
4500 unit->multiplicity = multiplicity;
4501 unit->simultaneity = simultaneity;
4502 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
4503 unit->num = num_units++;
4504 unit->num_opclasses = 0;
4505 unit->condexp = false_rtx;
4508 unit->first_lineno = lineno;
4512 XSTR (def, 0) = unit->name;
4514 /* Make a new operation class structure entry and initialize it. */
4515 op = oballoc (sizeof (struct function_unit_op));
4516 op->condexp = condexp;
4517 op->num = unit->num_opclasses++;
4518 op->ready = ready_cost;
4519 op->issue_delay = issue_delay;
4520 op->next = unit->ops;
4521 op->lineno = lineno;
4523 num_unit_opclasses++;
4525 /* Set our issue expression based on whether or not an optional conflict
4526 vector was specified. */
4529 /* Compute the IOR of all the specified expressions. */
4530 rtx orexp = false_rtx;
4533 for (i = 0; i < XVECLEN (def, 6); i++)
4534 orexp = insert_right_side (IOR, orexp, XVECEXP (def, 6, i), -2, -2);
4536 op->conflict_exp = orexp;
4537 extend_range (&unit->issue_delay, 1, issue_delay);
4541 op->conflict_exp = true_rtx;
4542 extend_range (&unit->issue_delay, issue_delay, issue_delay);
4545 /* Merge our conditional into that of the function unit so we can determine
4546 which insns are used by the function unit. */
4547 unit->condexp = insert_right_side (IOR, unit->condexp, op->condexp, -2, -2);
4550 /* Given a piece of RTX, print a C expression to test its truth value.
4551 We use AND and IOR both for logical and bit-wise operations, so
4552 interpret them as logical unless they are inside a comparison expression.
4553 The first bit of FLAGS will be nonzero in that case.
4555 Set the second bit of FLAGS to make references to attribute values use
4556 a cached local variable instead of calling a function. */
4559 write_test_expr (rtx exp, int flags)
4561 int comparison_operator = 0;
4563 struct attr_desc *attr;
4565 /* In order not to worry about operator precedence, surround our part of
4566 the expression with parentheses. */
4569 code = GET_CODE (exp);
4572 /* Binary operators. */
4574 case GE: case GT: case GEU: case GTU:
4575 case LE: case LT: case LEU: case LTU:
4576 comparison_operator = 1;
4578 case PLUS: case MINUS: case MULT: case DIV: case MOD:
4579 case AND: case IOR: case XOR:
4580 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4581 write_test_expr (XEXP (exp, 0), flags | comparison_operator);
4597 printf (" >= (unsigned) ");
4600 printf (" > (unsigned) ");
4609 printf (" <= (unsigned) ");
4612 printf (" < (unsigned) ");
4655 write_test_expr (XEXP (exp, 1), flags | comparison_operator);
4659 /* Special-case (not (eq_attrq "alternative" "x")) */
4660 if (! (flags & 1) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
4661 && XSTR (XEXP (exp, 0), 0) == alternative_name)
4663 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
4667 /* Otherwise, fall through to normal unary operator. */
4669 /* Unary operators. */
4689 write_test_expr (XEXP (exp, 0), flags);
4694 int set = XINT (exp, 0), bit = 0;
4697 fatal ("EQ_ATTR_ALT not valid inside comparison");
4700 fatal ("Empty EQ_ATTR_ALT should be optimized out");
4702 if (!(set & (set - 1)))
4704 if (!(set & 0xffff))
4727 printf ("which_alternative %s= %d",
4728 XINT (exp, 1) ? "!" : "=", bit);
4732 printf ("%s((1 << which_alternative) & 0x%x)",
4733 XINT (exp, 1) ? "!" : "", set);
4738 /* Comparison test of an attribute with a value. Most of these will
4739 have been removed by optimization. Handle "alternative"
4740 specially and give error if EQ_ATTR present inside a comparison. */
4743 fatal ("EQ_ATTR not valid inside comparison");
4745 if (XSTR (exp, 0) == alternative_name)
4747 printf ("which_alternative == %s", XSTR (exp, 1));
4751 attr = find_attr (&XSTR (exp, 0), 0);
4755 /* Now is the time to expand the value of a constant attribute. */
4758 write_test_expr (evaluate_eq_attr (exp, attr->default_val->value,
4765 printf ("attr_%s", attr->name);
4767 printf ("get_attr_%s (insn)", attr->name);
4769 write_attr_valueq (attr, XSTR (exp, 1));
4773 /* Comparison test of flags for define_delays. */
4776 fatal ("ATTR_FLAG not valid inside comparison");
4777 printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
4780 /* See if an operand matches a predicate. */
4782 /* If only a mode is given, just ensure the mode matches the operand.
4783 If neither a mode nor predicate is given, error. */
4784 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
4786 if (GET_MODE (exp) == VOIDmode)
4787 fatal ("null MATCH_OPERAND specified as test");
4789 printf ("GET_MODE (operands[%d]) == %smode",
4790 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4793 printf ("%s (operands[%d], %smode)",
4794 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4797 /* Constant integer. */
4799 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
4802 /* A random C expression. */
4804 printf ("%s", XSTR (exp, 0));
4807 /* The address of the branch target. */
4809 printf ("INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
4810 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
4814 /* The address of the current insn. We implement this actually as the
4815 address of the current insn for backward branches, but the last
4816 address of the next insn for forward branches, and both with
4817 adjustments that account for the worst-case possible stretching of
4818 intervening alignments between this insn and its destination. */
4819 printf ("insn_current_reference_address (insn)");
4823 printf ("%s", XSTR (exp, 0));
4827 write_test_expr (XEXP (exp, 0), flags & 2);
4829 write_test_expr (XEXP (exp, 1), flags | 1);
4831 write_test_expr (XEXP (exp, 2), flags | 1);
4835 fatal ("bad RTX code `%s' in attribute calculation\n",
4836 GET_RTX_NAME (code));
4842 /* Given an attribute value, return the maximum CONST_STRING argument
4843 encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
4846 max_attr_value (rtx exp, int *unknownp)
4851 switch (GET_CODE (exp))
4854 current_max = atoi (XSTR (exp, 0));
4858 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4859 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4861 n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4862 if (n > current_max)
4868 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4869 n = max_attr_value (XEXP (exp, 2), unknownp);
4870 if (n > current_max)
4876 current_max = INT_MAX;
4883 /* Given an attribute value, return the result of ORing together all
4884 CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
4885 if the numeric value is not known. */
4888 or_attr_value (rtx exp, int *unknownp)
4893 switch (GET_CODE (exp))
4896 current_or = atoi (XSTR (exp, 0));
4900 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4901 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4902 current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4906 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4907 current_or |= or_attr_value (XEXP (exp, 2), unknownp);
4919 /* Scan an attribute value, possibly a conditional, and record what actions
4920 will be required to do any conditional tests in it.
4923 `must_extract' if we need to extract the insn operands
4924 `must_constrain' if we must compute `which_alternative'
4925 `address_used' if an address expression was used
4926 `length_used' if an (eq_attr "length" ...) was used
4930 walk_attr_value (rtx exp)
4939 code = GET_CODE (exp);
4943 if (! ATTR_IND_SIMPLIFIED_P (exp))
4944 /* Since this is an arbitrary expression, it can look at anything.
4945 However, constant expressions do not depend on any particular
4947 must_extract = must_constrain = 1;
4955 must_extract = must_constrain = 1;
4959 if (XSTR (exp, 0) == alternative_name)
4960 must_extract = must_constrain = 1;
4961 else if (strcmp_check (XSTR (exp, 0), length_str) == 0)
4981 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
4986 walk_attr_value (XEXP (exp, i));
4990 if (XVEC (exp, i) != NULL)
4991 for (j = 0; j < XVECLEN (exp, i); j++)
4992 walk_attr_value (XVECEXP (exp, i, j));
4997 /* Write out a function to obtain the attribute for a given INSN. */
5000 write_attr_get (struct attr_desc *attr)
5002 struct attr_value *av, *common_av;
5004 /* Find the most used attribute value. Handle that as the `default' of the
5005 switch we will generate. */
5006 common_av = find_most_used (attr);
5008 /* Write out start of function, then all values with explicit `case' lines,
5009 then a `default', then the value with the most uses. */
5012 if (!attr->is_numeric)
5013 printf ("enum attr_%s\n", attr->name);
5014 else if (attr->unsigned_p)
5015 printf ("unsigned int\n");
5019 /* If the attribute name starts with a star, the remainder is the name of
5020 the subroutine to use, instead of `get_attr_...'. */
5021 if (attr->name[0] == '*')
5022 printf ("%s (rtx insn ATTRIBUTE_UNUSED)\n", &attr->name[1]);
5023 else if (attr->is_const == 0)
5024 printf ("get_attr_%s (rtx insn ATTRIBUTE_UNUSED)\n", attr->name);
5027 printf ("get_attr_%s (void)\n", attr->name);
5030 for (av = attr->first_value; av; av = av->next)
5031 if (av->num_insns != 0)
5032 write_attr_set (attr, 2, av->value, "return", ";",
5033 true_rtx, av->first_insn->insn_code,
5034 av->first_insn->insn_index);
5042 if (GET_CODE (common_av->value) == FFS)
5044 rtx p = XEXP (common_av->value, 0);
5046 /* No need to emit code to abort if the insn is unrecognized; the
5047 other get_attr_foo functions will do that when we call them. */
5049 write_toplevel_expr (p);
5051 printf ("\n if (accum && accum == (accum & -accum))\n");
5053 printf (" int i;\n");
5054 printf (" for (i = 0; accum >>= 1; ++i) continue;\n");
5055 printf (" accum = i;\n");
5056 printf (" }\n else\n");
5057 printf (" accum = ~accum;\n");
5058 printf (" return accum;\n}\n\n");
5062 printf (" switch (recog_memoized (insn))\n");
5065 for (av = attr->first_value; av; av = av->next)
5066 if (av != common_av)
5067 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
5069 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
5070 printf (" }\n}\n\n");
5074 /* Given an AND tree of known true terms (because we are inside an `if' with
5075 that as the condition or are in an `else' clause) and an expression,
5076 replace any known true terms with TRUE. Use `simplify_and_tree' to do
5077 the bulk of the work. */
5080 eliminate_known_true (rtx known_true, rtx exp, int insn_code, int insn_index)
5084 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
5086 if (GET_CODE (known_true) == AND)
5088 exp = eliminate_known_true (XEXP (known_true, 0), exp,
5089 insn_code, insn_index);
5090 exp = eliminate_known_true (XEXP (known_true, 1), exp,
5091 insn_code, insn_index);
5096 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
5102 /* Write out a series of tests and assignment statements to perform tests and
5103 sets of an attribute value. We are passed an indentation amount and prefix
5104 and suffix strings to write around each attribute value (e.g., "return"
5108 write_attr_set (struct attr_desc *attr, int indent, rtx value,
5109 const char *prefix, const char *suffix, rtx known_true,
5110 int insn_code, int insn_index)
5112 if (GET_CODE (value) == COND)
5114 /* Assume the default value will be the default of the COND unless we
5115 find an always true expression. */
5116 rtx default_val = XEXP (value, 1);
5117 rtx our_known_true = known_true;
5122 for (i = 0; i < XVECLEN (value, 0); i += 2)
5127 testexp = eliminate_known_true (our_known_true,
5128 XVECEXP (value, 0, i),
5129 insn_code, insn_index);
5130 newexp = attr_rtx (NOT, testexp);
5131 newexp = insert_right_side (AND, our_known_true, newexp,
5132 insn_code, insn_index);
5134 /* If the test expression is always true or if the next `known_true'
5135 expression is always false, this is the last case, so break
5136 out and let this value be the `else' case. */
5137 if (testexp == true_rtx || newexp == false_rtx)
5139 default_val = XVECEXP (value, 0, i + 1);
5143 /* Compute the expression to pass to our recursive call as being
5145 inner_true = insert_right_side (AND, our_known_true,
5146 testexp, insn_code, insn_index);
5148 /* If this is always false, skip it. */
5149 if (inner_true == false_rtx)
5152 write_indent (indent);
5153 printf ("%sif ", first_if ? "" : "else ");
5155 write_test_expr (testexp, 0);
5157 write_indent (indent + 2);
5160 write_attr_set (attr, indent + 4,
5161 XVECEXP (value, 0, i + 1), prefix, suffix,
5162 inner_true, insn_code, insn_index);
5163 write_indent (indent + 2);
5165 our_known_true = newexp;
5170 write_indent (indent);
5172 write_indent (indent + 2);
5176 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
5177 prefix, suffix, our_known_true, insn_code, insn_index);
5181 write_indent (indent + 2);
5187 write_indent (indent);
5188 printf ("%s ", prefix);
5189 write_attr_value (attr, value);
5190 printf ("%s\n", suffix);
5194 /* Write out the computation for one attribute value. */
5197 write_attr_case (struct attr_desc *attr, struct attr_value *av,
5198 int write_case_lines, const char *prefix, const char *suffix,
5199 int indent, rtx known_true)
5201 struct insn_ent *ie;
5203 if (av->num_insns == 0)
5206 if (av->has_asm_insn)
5208 write_indent (indent);
5209 printf ("case -1:\n");
5210 write_indent (indent + 2);
5211 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
5212 write_indent (indent + 2);
5213 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
5214 write_indent (indent + 2);
5215 printf (" fatal_insn_not_found (insn);\n");
5218 if (write_case_lines)
5220 for (ie = av->first_insn; ie; ie = ie->next)
5221 if (ie->insn_code != -1)
5223 write_indent (indent);
5224 printf ("case %d:\n", ie->insn_code);
5229 write_indent (indent);
5230 printf ("default:\n");
5233 /* See what we have to do to output this value. */
5234 must_extract = must_constrain = address_used = 0;
5235 walk_attr_value (av->value);
5239 write_indent (indent + 2);
5240 printf ("extract_constrain_insn_cached (insn);\n");
5242 else if (must_extract)
5244 write_indent (indent + 2);
5245 printf ("extract_insn_cached (insn);\n");
5248 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
5249 known_true, av->first_insn->insn_code,
5250 av->first_insn->insn_index);
5252 if (strncmp (prefix, "return", 6))
5254 write_indent (indent + 2);
5255 printf ("break;\n");
5260 /* Search for uses of non-const attributes and write code to cache them. */
5263 write_expr_attr_cache (rtx p, struct attr_desc *attr)
5268 if (GET_CODE (p) == EQ_ATTR)
5270 if (XSTR (p, 0) != attr->name)
5273 if (!attr->is_numeric)
5274 printf (" enum attr_%s ", attr->name);
5275 else if (attr->unsigned_p)
5276 printf (" unsigned int ");
5280 printf ("attr_%s = get_attr_%s (insn);\n", attr->name, attr->name);
5284 fmt = GET_RTX_FORMAT (GET_CODE (p));
5285 ie = GET_RTX_LENGTH (GET_CODE (p));
5286 for (i = 0; i < ie; i++)
5291 if (write_expr_attr_cache (XEXP (p, i), attr))
5296 je = XVECLEN (p, i);
5297 for (j = 0; j < je; ++j)
5298 if (write_expr_attr_cache (XVECEXP (p, i, j), attr))
5307 /* Evaluate an expression at top level. A front end to write_test_expr,
5308 in which we cache attribute values and break up excessively large
5309 expressions to cater to older compilers. */
5312 write_toplevel_expr (rtx p)
5314 struct attr_desc *attr;
5317 for (i = 0; i < MAX_ATTRS_INDEX; ++i)
5318 for (attr = attrs[i]; attr; attr = attr->next)
5319 if (!attr->is_const)
5320 write_expr_attr_cache (p, attr);
5322 printf (" unsigned long accum = 0;\n\n");
5324 while (GET_CODE (p) == IOR)
5327 if (GET_CODE (XEXP (p, 0)) == IOR)
5328 e = XEXP (p, 1), p = XEXP (p, 0);
5330 e = XEXP (p, 0), p = XEXP (p, 1);
5332 printf (" accum |= ");
5333 write_test_expr (e, 3);
5336 printf (" accum |= ");
5337 write_test_expr (p, 3);
5341 /* Utilities to write names in various forms. */
5344 write_unit_name (const char *prefix, int num, const char *suffix)
5346 struct function_unit *unit;
5348 for (unit = units; unit; unit = unit->next)
5349 if (unit->num == num)
5351 printf ("%s%s%s", prefix, unit->name, suffix);
5355 printf ("%s<unknown>%s", prefix, suffix);
5359 write_attr_valueq (struct attr_desc *attr, const char *s)
5361 if (attr->is_numeric)
5367 /* Make the blockage range values and function units used values easier
5369 if (attr->func_units_p)
5372 printf (" /* units: none */");
5374 write_unit_name (" /* units: ", num, " */");
5378 const char *sep = " /* units: ";
5379 for (i = 0, num = ~num; num; i++, num >>= 1)
5382 write_unit_name (sep, i, (num == 1) ? " */" : "");
5388 else if (attr->blockage_p)
5389 printf (" /* min %d, max %d */", num >> (HOST_BITS_PER_INT / 2),
5390 num & ((1 << (HOST_BITS_PER_INT / 2)) - 1));
5392 else if (num > 9 || num < 0)
5393 printf (" /* 0x%x */", num);
5397 write_upcase (attr->name);
5404 write_attr_value (struct attr_desc *attr, rtx value)
5408 switch (GET_CODE (value))
5411 write_attr_valueq (attr, XSTR (value, 0));
5415 printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
5419 fputs (XSTR (value, 0), stdout);
5424 struct attr_desc *attr2 = find_attr (&XSTR (value, 0), 0);
5425 printf ("get_attr_%s (%s)", attr2->name,
5426 (attr2->is_const ? "" : "insn"));
5447 write_attr_value (attr, XEXP (value, 0));
5451 write_attr_value (attr, XEXP (value, 1));
5460 write_upcase (const char *str)
5464 /* The argument of TOUPPER should not have side effects. */
5465 putchar (TOUPPER(*str));
5471 write_indent (int indent)
5473 for (; indent > 8; indent -= 8)
5476 for (; indent; indent--)
5480 /* Write a subroutine that is given an insn that requires a delay slot, a
5481 delay slot ordinal, and a candidate insn. It returns nonzero if the
5482 candidate can be placed in the specified delay slot of the insn.
5484 We can write as many as three subroutines. `eligible_for_delay'
5485 handles normal delay slots, `eligible_for_annul_true' indicates that
5486 the specified insn can be annulled if the branch is true, and likewise
5487 for `eligible_for_annul_false'.
5489 KIND is a string distinguishing these three cases ("delay", "annul_true",
5490 or "annul_false"). */
5493 write_eligible_delay (const char *kind)
5495 struct delay_desc *delay;
5499 struct attr_desc *attr;
5500 struct attr_value *av, *common_av;
5503 /* Compute the maximum number of delay slots required. We use the delay
5504 ordinal times this number plus one, plus the slot number as an index into
5505 the appropriate predicate to test. */
5507 for (delay = delays, max_slots = 0; delay; delay = delay->next)
5508 if (XVECLEN (delay->def, 1) / 3 > max_slots)
5509 max_slots = XVECLEN (delay->def, 1) / 3;
5511 /* Write function prelude. */
5514 printf ("eligible_for_%s (rtx delay_insn ATTRIBUTE_UNUSED, int slot, rtx candidate_insn, int flags ATTRIBUTE_UNUSED)\n",
5517 printf (" rtx insn;\n");
5519 printf (" if (slot >= %d)\n", max_slots);
5520 printf (" abort ();\n");
5523 /* If more than one delay type, find out which type the delay insn is. */
5527 attr = find_attr (&delay_type_str, 0);
5530 common_av = find_most_used (attr);
5532 printf (" insn = delay_insn;\n");
5533 printf (" switch (recog_memoized (insn))\n");
5536 sprintf (str, " * %d;\n break;", max_slots);
5537 for (av = attr->first_value; av; av = av->next)
5538 if (av != common_av)
5539 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
5541 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
5544 /* Ensure matched. Otherwise, shouldn't have been called. */
5545 printf (" if (slot < %d)\n", max_slots);
5546 printf (" abort ();\n\n");
5549 /* If just one type of delay slot, write simple switch. */
5550 if (num_delays == 1 && max_slots == 1)
5552 printf (" insn = candidate_insn;\n");
5553 printf (" switch (recog_memoized (insn))\n");
5556 attr = find_attr (&delay_1_0_str, 0);
5559 common_av = find_most_used (attr);
5561 for (av = attr->first_value; av; av = av->next)
5562 if (av != common_av)
5563 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
5565 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
5571 /* Write a nested CASE. The first indicates which condition we need to
5572 test, and the inner CASE tests the condition. */
5573 printf (" insn = candidate_insn;\n");
5574 printf (" switch (slot)\n");
5577 for (delay = delays; delay; delay = delay->next)
5578 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
5580 printf (" case %d:\n",
5581 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
5582 printf (" switch (recog_memoized (insn))\n");
5585 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
5587 attr = find_attr (&pstr, 0);
5590 common_av = find_most_used (attr);
5592 for (av = attr->first_value; av; av = av->next)
5593 if (av != common_av)
5594 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
5596 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
5600 printf (" default:\n");
5601 printf (" abort ();\n");
5608 /* Write routines to compute conflict cost for function units. Then write a
5609 table describing the available function units. */
5612 write_function_unit_info (void)
5614 struct function_unit *unit;
5617 /* Write out conflict routines for function units. Don't bother writing
5618 one if there is only one issue delay value. */
5620 for (unit = units; unit; unit = unit->next)
5622 if (unit->needs_blockage_function)
5623 write_complex_function (unit, "blockage", "block");
5625 /* If the minimum and maximum conflict costs are the same, there
5626 is only one value, so we don't need a function. */
5627 if (! unit->needs_conflict_function)
5629 unit->default_cost = make_numeric_value (unit->issue_delay.max);
5633 /* The function first computes the case from the candidate insn. */
5634 unit->default_cost = make_numeric_value (0);
5635 write_complex_function (unit, "conflict_cost", "cost");
5638 /* Now that all functions have been written, write the table describing
5639 the function units. The name is included for documentation purposes
5642 printf ("const struct function_unit_desc function_units[] = {\n");
5644 /* Write out the descriptions in numeric order, but don't force that order
5645 on the list. Doing so increases the runtime of genattrtab.c. */
5646 for (i = 0; i < num_units; i++)
5648 for (unit = units; unit; unit = unit->next)
5652 printf (" {\"%s\", %d, %d, %d, %s, %d, %s_unit_ready_cost, ",
5653 unit->name, 1 << unit->num, unit->multiplicity,
5654 unit->simultaneity, XSTR (unit->default_cost, 0),
5655 unit->issue_delay.max, unit->name);
5657 if (unit->needs_conflict_function)
5658 printf ("%s_unit_conflict_cost, ", unit->name);
5662 printf ("%d, ", unit->max_blockage);
5664 if (unit->needs_range_function)
5665 printf ("%s_unit_blockage_range, ", unit->name);
5669 if (unit->needs_blockage_function)
5670 printf ("%s_unit_blockage", unit->name);
5678 printf ("{\"dummy\", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* a dummy element */");
5683 write_complex_function (struct function_unit *unit,
5685 const char *connection)
5687 struct attr_desc *case_attr, *attr;
5688 struct attr_value *av, *common_av;
5695 printf ("static int\n");
5696 printf ("%s_unit_%s (rtx executing_insn, rtx candidate_insn)\n",
5699 printf (" rtx insn;\n");
5700 printf (" int casenum;\n\n");
5701 printf (" insn = executing_insn;\n");
5702 printf (" switch (recog_memoized (insn))\n");
5705 /* Write the `switch' statement to get the case value. */
5706 if (strlen (unit->name) + sizeof "*_cases" > 256)
5708 sprintf (str, "*%s_cases", unit->name);
5710 case_attr = find_attr (&pstr, 0);
5713 common_av = find_most_used (case_attr);
5715 for (av = case_attr->first_value; av; av = av->next)
5716 if (av != common_av)
5717 write_attr_case (case_attr, av, 1,
5718 "casenum =", ";", 4, unit->condexp);
5720 write_attr_case (case_attr, common_av, 0,
5721 "casenum =", ";", 4, unit->condexp);
5724 /* Now write an outer switch statement on each case. Then write
5725 the tests on the executing function within each. */
5726 printf (" insn = candidate_insn;\n");
5727 printf (" switch (casenum)\n");
5730 for (i = 0; i < unit->num_opclasses; i++)
5732 /* Ensure using this case. */
5734 for (av = case_attr->first_value; av; av = av->next)
5736 && contained_in_p (make_numeric_value (i), av->value))
5742 printf (" case %d:\n", i);
5743 sprintf (str, "*%s_%s_%d", unit->name, connection, i);
5745 attr = find_attr (&pstr, 0);
5749 /* If single value, just write it. */
5750 value = find_single_value (attr);
5752 write_attr_set (attr, 6, value, "return", ";\n", true_rtx, -2, -2);
5755 common_av = find_most_used (attr);
5756 printf (" switch (recog_memoized (insn))\n");
5759 for (av = attr->first_value; av; av = av->next)
5760 if (av != common_av)
5761 write_attr_case (attr, av, 1,
5762 "return", ";", 8, unit->condexp);
5764 write_attr_case (attr, common_av, 0,
5765 "return", ";", 8, unit->condexp);
5770 /* This default case should not be needed, but gcc's analysis is not
5771 good enough to realize that the default case is not needed for the
5772 second switch statement. */
5773 printf (" default:\n abort ();\n");
5774 printf (" }\n}\n\n");
5777 /* This page contains miscellaneous utility routines. */
5779 /* Given a pointer to a (char *), return a malloc'ed string containing the
5780 next comma-separated element. Advance the pointer to after the string
5781 scanned, or the end-of-string. Return NULL if at end of string. */
5784 next_comma_elt (const char **pstr)
5788 start = scan_comma_elt (pstr);
5793 return attr_string (start, *pstr - start);
5796 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
5797 is nonzero, build a new attribute, if one does not exist. *NAME_P is
5798 replaced by a pointer to a canonical copy of the string. */
5800 static struct attr_desc *
5801 find_attr (const char **name_p, int create)
5803 struct attr_desc *attr;
5805 const char *name = *name_p;
5807 /* Before we resort to using `strcmp', see if the string address matches
5808 anywhere. In most cases, it should have been canonicalized to do so. */
5809 if (name == alternative_name)
5812 index = name[0] & (MAX_ATTRS_INDEX - 1);
5813 for (attr = attrs[index]; attr; attr = attr->next)
5814 if (name == attr->name)
5817 /* Otherwise, do it the slow way. */
5818 for (attr = attrs[index]; attr; attr = attr->next)
5819 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
5821 *name_p = attr->name;
5828 attr = oballoc (sizeof (struct attr_desc));
5829 attr->name = DEF_ATTR_STRING (name);
5830 attr->first_value = attr->default_val = NULL;
5831 attr->is_numeric = attr->negative_ok = attr->is_const = attr->is_special = 0;
5832 attr->unsigned_p = attr->func_units_p = attr->blockage_p = attr->static_p = 0;
5833 attr->next = attrs[index];
5834 attrs[index] = attr;
5836 *name_p = attr->name;
5841 /* Create internal attribute with the given default value. */
5844 make_internal_attr (const char *name, rtx value, int special)
5846 struct attr_desc *attr;
5848 attr = find_attr (&name, 1);
5849 if (attr->default_val)
5852 attr->is_numeric = 1;
5854 attr->is_special = (special & ATTR_SPECIAL) != 0;
5855 attr->negative_ok = (special & ATTR_NEGATIVE_OK) != 0;
5856 attr->unsigned_p = (special & ATTR_UNSIGNED) != 0;
5857 attr->func_units_p = (special & ATTR_FUNC_UNITS) != 0;
5858 attr->blockage_p = (special & ATTR_BLOCKAGE) != 0;
5859 attr->static_p = (special & ATTR_STATIC) != 0;
5860 attr->default_val = get_attr_value (value, attr, -2);
5863 /* Find the most used value of an attribute. */
5865 static struct attr_value *
5866 find_most_used (struct attr_desc *attr)
5868 struct attr_value *av;
5869 struct attr_value *most_used;
5875 for (av = attr->first_value; av; av = av->next)
5876 if (av->num_insns > nuses)
5877 nuses = av->num_insns, most_used = av;
5882 /* If an attribute only has a single value used, return it. Otherwise
5886 find_single_value (struct attr_desc *attr)
5888 struct attr_value *av;
5891 unique_value = NULL;
5892 for (av = attr->first_value; av; av = av->next)
5898 unique_value = av->value;
5901 return unique_value;
5904 /* Return (attr_value "n") */
5907 make_numeric_value (int n)
5909 static rtx int_values[20];
5916 if (n < 20 && int_values[n])
5917 return int_values[n];
5919 p = attr_printf (MAX_DIGITS, "%d", n);
5920 exp = attr_rtx (CONST_STRING, p);
5923 int_values[n] = exp;
5929 extend_range (struct range *range, int min, int max)
5931 if (range->min > min)
5933 if (range->max < max)
5938 copy_rtx_unchanging (rtx orig)
5940 if (ATTR_IND_SIMPLIFIED_P (orig) || ATTR_CURR_SIMPLIFIED_P (orig))
5943 ATTR_CURR_SIMPLIFIED_P (orig) = 1;
5947 /* Determine if an insn has a constant number of delay slots, i.e., the
5948 number of delay slots is not a function of the length of the insn. */
5951 write_const_num_delay_slots (void)
5953 struct attr_desc *attr = find_attr (&num_delay_slots_str, 0);
5954 struct attr_value *av;
5955 struct insn_ent *ie;
5959 printf ("int\nconst_num_delay_slots (rtx insn)\n");
5961 printf (" switch (recog_memoized (insn))\n");
5964 for (av = attr->first_value; av; av = av->next)
5967 walk_attr_value (av->value);
5970 for (ie = av->first_insn; ie; ie = ie->next)
5971 if (ie->insn_code != -1)
5972 printf (" case %d:\n", ie->insn_code);
5973 printf (" return 0;\n");
5977 printf (" default:\n");
5978 printf (" return 1;\n");
5979 printf (" }\n}\n\n");
5984 main (int argc, char **argv)
5987 struct attr_desc *attr;
5988 struct insn_def *id;
5992 progname = "genattrtab";
5995 fatal ("no input file name");
5997 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
5998 return (FATAL_EXIT_CODE);
6000 obstack_init (hash_obstack);
6001 obstack_init (temp_obstack);
6003 /* Set up true and false rtx's */
6004 true_rtx = rtx_alloc (CONST_INT);
6005 XWINT (true_rtx, 0) = 1;
6006 false_rtx = rtx_alloc (CONST_INT);
6007 XWINT (false_rtx, 0) = 0;
6008 ATTR_IND_SIMPLIFIED_P (true_rtx) = ATTR_IND_SIMPLIFIED_P (false_rtx) = 1;
6009 ATTR_PERMANENT_P (true_rtx) = ATTR_PERMANENT_P (false_rtx) = 1;
6011 alternative_name = DEF_ATTR_STRING ("alternative");
6012 length_str = DEF_ATTR_STRING ("length");
6013 delay_type_str = DEF_ATTR_STRING ("*delay_type");
6014 delay_1_0_str = DEF_ATTR_STRING ("*delay_1_0");
6015 num_delay_slots_str = DEF_ATTR_STRING ("*num_delay_slots");
6017 printf ("/* Generated automatically by the program `genattrtab'\n\
6018 from the machine description file `md'. */\n\n");
6020 /* Read the machine description. */
6022 initiate_automaton_gen (argc, argv);
6027 desc = read_md_rtx (&lineno, &insn_code_number);
6031 switch (GET_CODE (desc))
6034 case DEFINE_PEEPHOLE:
6035 case DEFINE_ASM_ATTRIBUTES:
6036 gen_insn (desc, lineno);
6040 gen_attr (desc, lineno);
6044 gen_delay (desc, lineno);
6047 case DEFINE_FUNCTION_UNIT:
6048 gen_unit (desc, lineno);
6051 case DEFINE_CPU_UNIT:
6052 gen_cpu_unit (desc);
6055 case DEFINE_QUERY_CPU_UNIT:
6056 gen_query_cpu_unit (desc);
6064 gen_excl_set (desc);
6068 gen_presence_set (desc);
6071 case FINAL_PRESENCE_SET:
6072 gen_final_presence_set (desc);
6076 gen_absence_set (desc);
6079 case FINAL_ABSENCE_SET:
6080 gen_final_absence_set (desc);
6083 case DEFINE_AUTOMATON:
6084 gen_automaton (desc);
6087 case AUTOMATA_OPTION:
6088 gen_automata_option (desc);
6091 case DEFINE_RESERVATION:
6095 case DEFINE_INSN_RESERVATION:
6096 gen_insn_reserv (desc);
6102 if (GET_CODE (desc) != DEFINE_ASM_ATTRIBUTES)
6103 insn_index_number++;
6107 return FATAL_EXIT_CODE;
6111 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
6112 if (! got_define_asm_attributes)
6114 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
6115 XVEC (tem, 0) = rtvec_alloc (0);
6119 /* Expand DEFINE_DELAY information into new attribute. */
6123 if (num_units || num_dfa_decls)
6125 /* Expand DEFINE_FUNCTION_UNIT information into new attributes. */
6127 /* Build DFA, output some functions and expand DFA information
6128 into new attributes. */
6132 printf ("#include \"config.h\"\n");
6133 printf ("#include \"system.h\"\n");
6134 printf ("#include \"coretypes.h\"\n");
6135 printf ("#include \"tm.h\"\n");
6136 printf ("#include \"rtl.h\"\n");
6137 printf ("#include \"tm_p.h\"\n");
6138 printf ("#include \"insn-config.h\"\n");
6139 printf ("#include \"recog.h\"\n");
6140 printf ("#include \"regs.h\"\n");
6141 printf ("#include \"real.h\"\n");
6142 printf ("#include \"output.h\"\n");
6143 printf ("#include \"insn-attr.h\"\n");
6144 printf ("#include \"toplev.h\"\n");
6145 printf ("#include \"flags.h\"\n");
6146 printf ("#include \"function.h\"\n");
6148 printf ("#define operands recog_data.operand\n\n");
6150 /* Make `insn_alternatives'. */
6151 insn_alternatives = oballoc (insn_code_number * sizeof (int));
6152 for (id = defs; id; id = id->next)
6153 if (id->insn_code >= 0)
6154 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
6156 /* Make `insn_n_alternatives'. */
6157 insn_n_alternatives = oballoc (insn_code_number * sizeof (int));
6158 for (id = defs; id; id = id->next)
6159 if (id->insn_code >= 0)
6160 insn_n_alternatives[id->insn_code] = id->num_alternatives;
6162 /* Prepare to write out attribute subroutines by checking everything stored
6163 away and building the attribute cases. */
6167 for (i = 0; i < MAX_ATTRS_INDEX; i++)
6168 for (attr = attrs[i]; attr; attr = attr->next)
6169 attr->default_val->value
6170 = check_attr_value (attr->default_val->value, attr);
6173 return FATAL_EXIT_CODE;
6175 for (i = 0; i < MAX_ATTRS_INDEX; i++)
6176 for (attr = attrs[i]; attr; attr = attr->next)
6179 /* Construct extra attributes for `length'. */
6180 make_length_attrs ();
6182 /* Perform any possible optimizations to speed up compilation. */
6185 /* Now write out all the `gen_attr_...' routines. Do these before the
6186 special routines (specifically before write_function_unit_info), so
6187 that they get defined before they are used. */
6189 for (i = 0; i < MAX_ATTRS_INDEX; i++)
6190 for (attr = attrs[i]; attr; attr = attr->next)
6192 if (! attr->is_special && ! attr->is_const)
6197 = (attr->name [0] == '*'
6198 && strcmp (&attr->name[1], INSN_ALTS_FUNC_NAME) == 0);
6200 printf ("\n#if AUTOMATON_ALTS\n");
6201 write_attr_get (attr);
6203 printf ("#endif\n\n");
6207 /* Write out delay eligibility information, if DEFINE_DELAY present.
6208 (The function to compute the number of delay slots will be written
6212 write_eligible_delay ("delay");
6213 if (have_annul_true)
6214 write_eligible_delay ("annul_true");
6215 if (have_annul_false)
6216 write_eligible_delay ("annul_false");
6219 if (num_units || num_dfa_decls)
6221 /* Write out information about function units. */
6222 write_function_unit_info ();
6223 /* Output code for pipeline hazards recognition based on DFA
6224 (deterministic finite state automata. */
6228 /* Write out constant delay slot info. */
6229 write_const_num_delay_slots ();
6231 write_length_unit_log ();
6234 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
6237 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
6239 get_insn_name (int code ATTRIBUTE_UNUSED)