1 /* Generate code from machine description to compute values of attributes.
2 Copyright (C) 1991, 93-98, 1999 Free Software Foundation, Inc.
3 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* This program handles insn attributes and the DEFINE_DELAY and
23 DEFINE_FUNCTION_UNIT definitions.
25 It produces a series of functions named `get_attr_...', one for each insn
26 attribute. Each of these is given the rtx for an insn and returns a member
27 of the enum for the attribute.
29 These subroutines have the form of a `switch' on the INSN_CODE (via
30 `recog_memoized'). Each case either returns a constant attribute value
31 or a value that depends on tests on other attributes, the form of
32 operands, or some random C expression (encoded with a SYMBOL_REF
35 If the attribute `alternative', or a random C expression is present,
36 `constrain_operands' is called. If either of these cases of a reference to
37 an operand is found, `extract_insn' is called.
39 The special attribute `length' is also recognized. For this operand,
40 expressions involving the address of an operand or the current insn,
41 (address (pc)), are valid. In this case, an initial pass is made to
42 set all lengths that do not depend on address. Those that do are set to
43 the maximum length. Then each insn that depends on an address is checked
44 and possibly has its length changed. The process repeats until no further
45 changed are made. The resulting lengths are saved for use by
48 A special form of DEFINE_ATTR, where the expression for default value is a
49 CONST expression, indicates an attribute that is constant for a given run
50 of the compiler. The subroutine generated for these attributes has no
51 parameters as it does not depend on any particular insn. Constant
52 attributes are typically used to specify which variety of processor is
55 Internal attributes are defined to handle DEFINE_DELAY and
56 DEFINE_FUNCTION_UNIT. Special routines are output for these cases.
58 This program works by keeping a list of possible values for each attribute.
59 These include the basic attribute choices, default values for attribute, and
60 all derived quantities.
62 As the description file is read, the definition for each insn is saved in a
63 `struct insn_def'. When the file reading is complete, a `struct insn_ent'
64 is created for each insn and chained to the corresponding attribute value,
65 either that specified, or the default.
67 An optimization phase is then run. This simplifies expressions for each
68 insn. EQ_ATTR tests are resolved, whenever possible, to a test that
69 indicates when the attribute has the specified value for the insn. This
70 avoids recursive calls during compilation.
72 The strategy used when processing DEFINE_DELAY and DEFINE_FUNCTION_UNIT
73 definitions is to create arbitrarily complex expressions and have the
74 optimization simplify them.
76 Once optimization is complete, any required routines and definitions
79 An optimization that is not yet implemented is to hoist the constant
80 expressions entirely out of the routines and definitions that are written.
81 A way to do this is to iterate over all possible combinations of values
82 for constant attributes and generate a set of functions for that given
83 combination. An initialization function would be written that evaluates
84 the attributes and installs the corresponding set of routines and
85 definitions (each would be accessed through a pointer).
87 We use the flags in an RTX as follows:
88 `unchanging' (RTX_UNCHANGING_P): This rtx is fully simplified
89 independent of the insn code.
90 `in_struct' (MEM_IN_STRUCT_P): This rtx is fully simplified
91 for the insn code currently being processed (see optimize_attrs).
92 `integrated' (RTX_INTEGRATED_P): This rtx is permanent and unique
94 `volatil' (MEM_VOLATILE_P): During simplify_by_exploding the value of an
95 EQ_ATTR rtx is true if !volatil and false if volatil. */
101 #include "insn-config.h" /* For REGISTER_CONSTRAINTS */
103 #ifdef HAVE_SYS_RESOURCE_H
104 # include <sys/resource.h>
107 /* We must include obstack.h after <sys/time.h>, to avoid lossage with
108 /usr/include/sys/stdtypes.h on Sun OS 4.x. */
112 static struct obstack obstack, obstack1, obstack2;
113 struct obstack *rtl_obstack = &obstack;
114 struct obstack *hash_obstack = &obstack1;
115 struct obstack *temp_obstack = &obstack2;
117 #define obstack_chunk_alloc xmalloc
118 #define obstack_chunk_free free
120 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
121 char **insn_name_ptr = 0;
123 /* enough space to reserve for printing out ints */
124 #define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)
126 /* Define structures used to record attributes and values. */
128 /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
129 encountered, we store all the relevant information into a
130 `struct insn_def'. This is done to allow attribute definitions to occur
131 anywhere in the file. */
135 int insn_code; /* Instruction number. */
136 int insn_index; /* Expression numer in file, for errors. */
137 struct insn_def *next; /* Next insn in chain. */
138 rtx def; /* The DEFINE_... */
139 int num_alternatives; /* Number of alternatives. */
140 int vec_idx; /* Index of attribute vector in `def'. */
143 /* Once everything has been read in, we store in each attribute value a list
144 of insn codes that have that value. Here is the structure used for the
149 int insn_code; /* Instruction number. */
150 int insn_index; /* Index of definition in file */
151 struct insn_ent *next; /* Next in chain. */
154 /* Each value of an attribute (either constant or computed) is assigned a
155 structure which is used as the listhead of the insns that have that
160 rtx value; /* Value of attribute. */
161 struct attr_value *next; /* Next attribute value in chain. */
162 struct insn_ent *first_insn; /* First insn with this value. */
163 int num_insns; /* Number of insns with this value. */
164 int has_asm_insn; /* True if this value used for `asm' insns */
167 /* Structure for each attribute. */
171 char *name; /* Name of attribute. */
172 struct attr_desc *next; /* Next attribute. */
173 unsigned is_numeric : 1; /* Values of this attribute are numeric. */
174 unsigned negative_ok : 1; /* Allow negative numeric values. */
175 unsigned unsigned_p : 1; /* Make the output function unsigned int. */
176 unsigned is_const : 1; /* Attribute value constant for each run. */
177 unsigned is_special : 1; /* Don't call `write_attr_set'. */
178 unsigned func_units_p : 1; /* this is the function_units attribute */
179 unsigned blockage_p : 1; /* this is the blockage range function */
180 struct attr_value *first_value; /* First value of this attribute. */
181 struct attr_value *default_val; /* Default value for this attribute. */
184 #define NULL_ATTR (struct attr_desc *) NULL
186 /* A range of values. */
194 /* Structure for each DEFINE_DELAY. */
198 rtx def; /* DEFINE_DELAY expression. */
199 struct delay_desc *next; /* Next DEFINE_DELAY. */
200 int num; /* Number of DEFINE_DELAY, starting at 1. */
203 /* Record information about each DEFINE_FUNCTION_UNIT. */
205 struct function_unit_op
207 rtx condexp; /* Expression TRUE for applicable insn. */
208 struct function_unit_op *next; /* Next operation for this function unit. */
209 int num; /* Ordinal for this operation type in unit. */
210 int ready; /* Cost until data is ready. */
211 int issue_delay; /* Cost until unit can accept another insn. */
212 rtx conflict_exp; /* Expression TRUE for insns incurring issue delay. */
213 rtx issue_exp; /* Expression computing issue delay. */
216 /* Record information about each function unit mentioned in a
217 DEFINE_FUNCTION_UNIT. */
221 char *name; /* Function unit name. */
222 struct function_unit *next; /* Next function unit. */
223 int num; /* Ordinal of this unit type. */
224 int multiplicity; /* Number of units of this type. */
225 int simultaneity; /* Maximum number of simultaneous insns
226 on this function unit or 0 if unlimited. */
227 rtx condexp; /* Expression TRUE for insn needing unit. */
228 int num_opclasses; /* Number of different operation types. */
229 struct function_unit_op *ops; /* Pointer to first operation type. */
230 int needs_conflict_function; /* Nonzero if a conflict function required. */
231 int needs_blockage_function; /* Nonzero if a blockage function required. */
232 int needs_range_function; /* Nonzero if blockage range function needed.*/
233 rtx default_cost; /* Conflict cost, if constant. */
234 struct range issue_delay; /* Range of issue delay values. */
235 int max_blockage; /* Maximum time an insn blocks the unit. */
238 /* Listheads of above structures. */
240 /* This one is indexed by the first character of the attribute name. */
241 #define MAX_ATTRS_INDEX 256
242 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
243 static struct insn_def *defs;
244 static struct delay_desc *delays;
245 static struct function_unit *units;
247 /* An expression where all the unknown terms are EQ_ATTR tests can be
248 rearranged into a COND provided we can enumerate all possible
249 combinations of the unknown values. The set of combinations become the
250 tests of the COND; the value of the expression given that combination is
251 computed and becomes the corresponding value. To do this, we must be
252 able to enumerate all values for each attribute used in the expression
253 (currently, we give up if we find a numeric attribute).
255 If the set of EQ_ATTR tests used in an expression tests the value of N
256 different attributes, the list of all possible combinations can be made
257 by walking the N-dimensional attribute space defined by those
258 attributes. We record each of these as a struct dimension.
260 The algorithm relies on sharing EQ_ATTR nodes: if two nodes in an
261 expression are the same, the will also have the same address. We find
262 all the EQ_ATTR nodes by marking them MEM_VOLATILE_P. This bit later
263 represents the value of an EQ_ATTR node, so once all nodes are marked,
264 they are also given an initial value of FALSE.
266 We then separate the set of EQ_ATTR nodes into dimensions for each
267 attribute and put them on the VALUES list. Terms are added as needed by
268 `add_values_to_cover' so that all possible values of the attribute are
271 Each dimension also has a current value. This is the node that is
272 currently considered to be TRUE. If this is one of the nodes added by
273 `add_values_to_cover', all the EQ_ATTR tests in the original expression
274 will be FALSE. Otherwise, only the CURRENT_VALUE will be true.
276 NUM_VALUES is simply the length of the VALUES list and is there for
279 Once the dimensions are created, the algorithm enumerates all possible
280 values and computes the current value of the given expression. */
284 struct attr_desc *attr; /* Attribute for this dimension. */
285 rtx values; /* List of attribute values used. */
286 rtx current_value; /* Position in the list for the TRUE value. */
287 int num_values; /* Length of the values list. */
290 /* Other variables. */
292 static int insn_code_number;
293 static int insn_index_number;
294 static int got_define_asm_attributes;
295 static int must_extract;
296 static int must_constrain;
297 static int address_used;
298 static int length_used;
299 static int num_delays;
300 static int have_annul_true, have_annul_false;
301 static int num_units, num_unit_opclasses;
302 static int num_insn_ents;
304 /* Used as operand to `operate_exp': */
306 enum operator {PLUS_OP, MINUS_OP, POS_MINUS_OP, EQ_OP, OR_OP, ORX_OP, MAX_OP, MIN_OP, RANGE_OP};
308 /* Stores, for each insn code, the number of constraint alternatives. */
310 static int *insn_n_alternatives;
312 /* Stores, for each insn code, a bitmap that has bits on for each possible
315 static int *insn_alternatives;
317 /* If nonzero, assume that the `alternative' attr has this value.
318 This is the hashed, unique string for the numeral
319 whose value is chosen alternative. */
321 static char *current_alternative_string;
323 /* Used to simplify expressions. */
325 static rtx true_rtx, false_rtx;
327 /* Used to reduce calls to `strcmp' */
329 static char *alternative_name;
331 /* Indicate that REG_DEAD notes are valid if dead_or_set_p is ever
334 int reload_completed = 0;
336 /* Some machines test `optimize' in macros called from rtlanal.c, so we need
337 to define it here. */
341 /* Simplify an expression. Only call the routine if there is something to
343 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
344 (RTX_UNCHANGING_P (EXP) || MEM_IN_STRUCT_P (EXP) ? (EXP) \
345 : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
347 /* Simplify (eq_attr ("alternative") ...)
348 when we are working with a particular alternative. */
349 #define SIMPLIFY_ALTERNATIVE(EXP) \
350 if (current_alternative_string \
351 && GET_CODE ((EXP)) == EQ_ATTR \
352 && XSTR ((EXP), 0) == alternative_name) \
353 (EXP) = (XSTR ((EXP), 1) == current_alternative_string \
354 ? true_rtx : false_rtx);
356 /* These are referenced by rtlanal.c and hence need to be defined somewhere.
357 They won't actually be used. */
359 struct _global_rtl global_rtl;
360 rtx pic_offset_table_rtx;
362 static void attr_hash_add_rtx PROTO((int, rtx));
363 static void attr_hash_add_string PROTO((int, char *));
364 static rtx attr_rtx PVPROTO((enum rtx_code, ...));
365 static char *attr_printf PVPROTO((int, const char *, ...))
367 static char *attr_string PROTO((const char *, int));
368 static rtx check_attr_test PROTO((rtx, int));
369 static rtx check_attr_value PROTO((rtx, struct attr_desc *));
370 static rtx convert_set_attr_alternative PROTO((rtx, int, int));
371 static rtx convert_set_attr PROTO((rtx, int, int));
372 static void check_defs PROTO((void));
374 static rtx convert_const_symbol_ref PROTO((rtx, struct attr_desc *));
376 static rtx make_canonical PROTO((struct attr_desc *, rtx));
377 static struct attr_value *get_attr_value PROTO((rtx, struct attr_desc *, int));
378 static rtx copy_rtx_unchanging PROTO((rtx));
379 static rtx copy_boolean PROTO((rtx));
380 static void expand_delays PROTO((void));
381 static rtx operate_exp PROTO((enum operator, rtx, rtx));
382 static void expand_units PROTO((void));
383 static rtx simplify_knowing PROTO((rtx, rtx));
384 static rtx encode_units_mask PROTO((rtx));
385 static void fill_attr PROTO((struct attr_desc *));
386 /* dpx2 compiler chokes if we specify the arg types of the args. */
387 static rtx substitute_address PROTO((rtx, rtx (*) (rtx), rtx (*) (rtx)));
388 static void make_length_attrs PROTO((void));
389 static rtx identity_fn PROTO((rtx));
390 static rtx zero_fn PROTO((rtx));
391 static rtx one_fn PROTO((rtx));
392 static rtx max_fn PROTO((rtx));
393 static void write_length_unit_log PROTO ((void));
394 static rtx simplify_cond PROTO((rtx, int, int));
396 static rtx simplify_by_alternatives PROTO((rtx, int, int));
398 static rtx simplify_by_exploding PROTO((rtx));
399 static int find_and_mark_used_attributes PROTO((rtx, rtx *, int *));
400 static void unmark_used_attributes PROTO((rtx, struct dimension *, int));
401 static int add_values_to_cover PROTO((struct dimension *));
402 static int increment_current_value PROTO((struct dimension *, int));
403 static rtx test_for_current_value PROTO((struct dimension *, int));
404 static rtx simplify_with_current_value PROTO((rtx, struct dimension *, int));
405 static rtx simplify_with_current_value_aux PROTO((rtx));
406 static void clear_struct_flag PROTO((rtx));
407 static int count_sub_rtxs PROTO((rtx, int));
408 static void remove_insn_ent PROTO((struct attr_value *, struct insn_ent *));
409 static void insert_insn_ent PROTO((struct attr_value *, struct insn_ent *));
410 static rtx insert_right_side PROTO((enum rtx_code, rtx, rtx, int, int));
411 static rtx make_alternative_compare PROTO((int));
412 static int compute_alternative_mask PROTO((rtx, enum rtx_code));
413 static rtx evaluate_eq_attr PROTO((rtx, rtx, int, int));
414 static rtx simplify_and_tree PROTO((rtx, rtx *, int, int));
415 static rtx simplify_or_tree PROTO((rtx, rtx *, int, int));
416 static rtx simplify_test_exp PROTO((rtx, int, int));
417 static void optimize_attrs PROTO((void));
418 static void gen_attr PROTO((rtx));
419 static int count_alternatives PROTO((rtx));
420 static int compares_alternatives_p PROTO((rtx));
421 static int contained_in_p PROTO((rtx, rtx));
422 static void gen_insn PROTO((rtx));
423 static void gen_delay PROTO((rtx));
424 static void gen_unit PROTO((rtx));
425 static void write_test_expr PROTO((rtx, int));
426 static int max_attr_value PROTO((rtx, int*));
427 static int or_attr_value PROTO((rtx, int*));
428 static void walk_attr_value PROTO((rtx));
429 static void write_attr_get PROTO((struct attr_desc *));
430 static rtx eliminate_known_true PROTO((rtx, rtx, int, int));
431 static void write_attr_set PROTO((struct attr_desc *, int, rtx,
432 const char *, const char *, rtx,
434 static void write_attr_case PROTO((struct attr_desc *, struct attr_value *,
435 int, const char *, const char *, int, rtx));
436 static void write_unit_name PROTO((const char *, int, const char *));
437 static void write_attr_valueq PROTO((struct attr_desc *, char *));
438 static void write_attr_value PROTO((struct attr_desc *, rtx));
439 static void write_upcase PROTO((char *));
440 static void write_indent PROTO((int));
441 static void write_eligible_delay PROTO((const char *));
442 static void write_function_unit_info PROTO((void));
443 static void write_complex_function PROTO((struct function_unit *, const char *,
445 static int write_expr_attr_cache PROTO((rtx, struct attr_desc *));
446 static void write_toplevel_expr PROTO((rtx));
447 static void write_const_num_delay_slots PROTO ((void));
448 static int n_comma_elts PROTO((char *));
449 static char *next_comma_elt PROTO((char **));
450 static struct attr_desc *find_attr PROTO((const char *, int));
451 static void make_internal_attr PROTO((const char *, rtx, int));
452 static struct attr_value *find_most_used PROTO((struct attr_desc *));
453 static rtx find_single_value PROTO((struct attr_desc *));
454 static rtx make_numeric_value PROTO((int));
455 static void extend_range PROTO((struct range *, int, int));
456 static rtx attr_eq PROTO((char *, char *));
457 static char *attr_numeral PROTO((int));
458 static int attr_equal_p PROTO((rtx, rtx));
459 static rtx attr_copy_rtx PROTO((rtx));
461 #define oballoc(size) obstack_alloc (hash_obstack, size)
464 /* Hash table for sharing RTL and strings. */
466 /* Each hash table slot is a bucket containing a chain of these structures.
467 Strings are given negative hash codes; RTL expressions are given positive
472 struct attr_hash *next; /* Next structure in the bucket. */
473 int hashcode; /* Hash code of this rtx or string. */
476 char *str; /* The string (negative hash codes) */
477 rtx rtl; /* or the RTL recorded here. */
481 /* Now here is the hash table. When recording an RTL, it is added to
482 the slot whose index is the hash code mod the table size. Note
483 that the hash table is used for several kinds of RTL (see attr_rtx)
484 and for strings. While all these live in the same table, they are
485 completely independent, and the hash code is computed differently
488 #define RTL_HASH_SIZE 4093
489 struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
491 /* Here is how primitive or already-shared RTL's hash
493 #define RTL_HASH(RTL) ((long) (RTL) & 0777777)
495 /* Add an entry to the hash table for RTL with hash code HASHCODE. */
498 attr_hash_add_rtx (hashcode, rtl)
502 register struct attr_hash *h;
504 h = (struct attr_hash *) obstack_alloc (hash_obstack,
505 sizeof (struct attr_hash));
506 h->hashcode = hashcode;
508 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
509 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
512 /* Add an entry to the hash table for STRING with hash code HASHCODE. */
515 attr_hash_add_string (hashcode, str)
519 register struct attr_hash *h;
521 h = (struct attr_hash *) obstack_alloc (hash_obstack,
522 sizeof (struct attr_hash));
523 h->hashcode = -hashcode;
525 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
526 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
529 /* Generate an RTL expression, but avoid duplicates.
530 Set the RTX_INTEGRATED_P flag for these permanent objects.
532 In some cases we cannot uniquify; then we return an ordinary
533 impermanent rtx with RTX_INTEGRATED_P clear.
535 Args are like gen_rtx, but without the mode:
537 rtx attr_rtx (code, [element1, ..., elementn]) */
541 attr_rtx VPROTO((enum rtx_code code, ...))
543 #ifndef ANSI_PROTOTYPES
547 register int i; /* Array indices... */
548 register const char *fmt; /* Current rtx's format... */
549 register rtx rt_val; /* RTX to return to caller... */
551 register struct attr_hash *h;
552 struct obstack *old_obstack = rtl_obstack;
556 #ifndef ANSI_PROTOTYPES
557 code = va_arg (p, enum rtx_code);
560 /* For each of several cases, search the hash table for an existing entry.
561 Use that entry if one is found; otherwise create a new RTL and add it
564 if (GET_RTX_CLASS (code) == '1')
566 rtx arg0 = va_arg (p, rtx);
568 /* A permanent object cannot point to impermanent ones. */
569 if (! RTX_INTEGRATED_P (arg0))
571 rt_val = rtx_alloc (code);
572 XEXP (rt_val, 0) = arg0;
577 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
578 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
579 if (h->hashcode == hashcode
580 && GET_CODE (h->u.rtl) == code
581 && XEXP (h->u.rtl, 0) == arg0)
586 rtl_obstack = hash_obstack;
587 rt_val = rtx_alloc (code);
588 XEXP (rt_val, 0) = arg0;
591 else if (GET_RTX_CLASS (code) == 'c'
592 || GET_RTX_CLASS (code) == '2'
593 || GET_RTX_CLASS (code) == '<')
595 rtx arg0 = va_arg (p, rtx);
596 rtx arg1 = va_arg (p, rtx);
598 /* A permanent object cannot point to impermanent ones. */
599 if (! RTX_INTEGRATED_P (arg0) || ! RTX_INTEGRATED_P (arg1))
601 rt_val = rtx_alloc (code);
602 XEXP (rt_val, 0) = arg0;
603 XEXP (rt_val, 1) = arg1;
608 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
609 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
610 if (h->hashcode == hashcode
611 && GET_CODE (h->u.rtl) == code
612 && XEXP (h->u.rtl, 0) == arg0
613 && XEXP (h->u.rtl, 1) == arg1)
618 rtl_obstack = hash_obstack;
619 rt_val = rtx_alloc (code);
620 XEXP (rt_val, 0) = arg0;
621 XEXP (rt_val, 1) = arg1;
624 else if (GET_RTX_LENGTH (code) == 1
625 && GET_RTX_FORMAT (code)[0] == 's')
627 char * arg0 = va_arg (p, char *);
629 if (code == SYMBOL_REF)
630 arg0 = attr_string (arg0, strlen (arg0));
632 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
633 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
634 if (h->hashcode == hashcode
635 && GET_CODE (h->u.rtl) == code
636 && XSTR (h->u.rtl, 0) == arg0)
641 rtl_obstack = hash_obstack;
642 rt_val = rtx_alloc (code);
643 XSTR (rt_val, 0) = arg0;
646 else if (GET_RTX_LENGTH (code) == 2
647 && GET_RTX_FORMAT (code)[0] == 's'
648 && GET_RTX_FORMAT (code)[1] == 's')
650 char *arg0 = va_arg (p, char *);
651 char *arg1 = va_arg (p, char *);
653 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
654 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
655 if (h->hashcode == hashcode
656 && GET_CODE (h->u.rtl) == code
657 && XSTR (h->u.rtl, 0) == arg0
658 && XSTR (h->u.rtl, 1) == arg1)
663 rtl_obstack = hash_obstack;
664 rt_val = rtx_alloc (code);
665 XSTR (rt_val, 0) = arg0;
666 XSTR (rt_val, 1) = arg1;
669 else if (code == CONST_INT)
671 HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
681 rt_val = rtx_alloc (code); /* Allocate the storage space. */
683 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
684 for (i = 0; i < GET_RTX_LENGTH (code); i++)
688 case '0': /* Unused field. */
691 case 'i': /* An integer? */
692 XINT (rt_val, i) = va_arg (p, int);
695 case 'w': /* A wide integer? */
696 XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
699 case 's': /* A string? */
700 XSTR (rt_val, i) = va_arg (p, char *);
703 case 'e': /* An expression? */
704 case 'u': /* An insn? Same except when printing. */
705 XEXP (rt_val, i) = va_arg (p, rtx);
708 case 'E': /* An RTX vector? */
709 XVEC (rt_val, i) = va_arg (p, rtvec);
720 rtl_obstack = old_obstack;
722 attr_hash_add_rtx (hashcode, rt_val);
723 RTX_INTEGRATED_P (rt_val) = 1;
731 /* Create a new string printed with the printf line arguments into a space
732 of at most LEN bytes:
734 rtx attr_printf (len, format, [arg1, ..., argn]) */
738 attr_printf VPROTO((register int len, const char *fmt, ...))
740 #ifndef ANSI_PROTOTYPES
749 #ifndef ANSI_PROTOTYPES
750 len = va_arg (p, int);
751 fmt = va_arg (p, const char *);
754 /* Print the string into a temporary location. */
755 str = (char *) alloca (len);
756 vsprintf (str, fmt, p);
759 return attr_string (str, strlen (str));
763 attr_eq (name, value)
766 return attr_rtx (EQ_ATTR, attr_string (name, strlen (name)),
767 attr_string (value, strlen (value)));
774 return XSTR (make_numeric_value (n), 0);
777 /* Return a permanent (possibly shared) copy of a string STR (not assumed
778 to be null terminated) with LEN bytes. */
781 attr_string (str, len)
785 register struct attr_hash *h;
788 register char *new_str;
790 /* Compute the hash code. */
791 hashcode = (len + 1) * 613 + (unsigned)str[0];
792 for (i = 1; i <= len; i += 2)
793 hashcode = ((hashcode * 613) + (unsigned)str[i]);
795 hashcode = -hashcode;
797 /* Search the table for the string. */
798 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
799 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
800 && !strncmp (h->u.str, str, len))
801 return h->u.str; /* <-- return if found. */
803 /* Not found; create a permanent copy and add it to the hash table. */
804 new_str = (char *) obstack_alloc (hash_obstack, len + 1);
805 bcopy (str, new_str, len);
807 attr_hash_add_string (hashcode, new_str);
809 return new_str; /* Return the new string. */
812 /* Check two rtx's for equality of contents,
813 taking advantage of the fact that if both are hashed
814 then they can't be equal unless they are the same object. */
820 return (x == y || (! (RTX_INTEGRATED_P (x) && RTX_INTEGRATED_P (y))
821 && rtx_equal_p (x, y)));
824 /* Copy an attribute value expression,
825 descending to all depths, but not copying any
826 permanent hashed subexpressions. */
834 register RTX_CODE code;
835 register const char *format_ptr;
837 /* No need to copy a permanent object. */
838 if (RTX_INTEGRATED_P (orig))
841 code = GET_CODE (orig);
859 copy = rtx_alloc (code);
860 PUT_MODE (copy, GET_MODE (orig));
861 copy->in_struct = orig->in_struct;
862 copy->volatil = orig->volatil;
863 copy->unchanging = orig->unchanging;
864 copy->integrated = orig->integrated;
866 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
868 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
870 switch (*format_ptr++)
873 XEXP (copy, i) = XEXP (orig, i);
874 if (XEXP (orig, i) != NULL)
875 XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
880 XVEC (copy, i) = XVEC (orig, i);
881 if (XVEC (orig, i) != NULL)
883 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
884 for (j = 0; j < XVECLEN (copy, i); j++)
885 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
891 XINT (copy, i) = XINT (orig, i);
895 XWINT (copy, i) = XWINT (orig, i);
900 XSTR (copy, i) = XSTR (orig, i);
910 /* Given a test expression for an attribute, ensure it is validly formed.
911 IS_CONST indicates whether the expression is constant for each compiler
912 run (a constant expression may not test any particular insn).
914 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
915 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
916 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
918 Update the string address in EQ_ATTR expression to be the same used
919 in the attribute (or `alternative_name') to speed up subsequent
920 `find_attr' calls and eliminate most `strcmp' calls.
922 Return the new expression, if any. */
925 check_attr_test (exp, is_const)
929 struct attr_desc *attr;
930 struct attr_value *av;
934 switch (GET_CODE (exp))
937 /* Handle negation test. */
938 if (XSTR (exp, 1)[0] == '!')
939 return check_attr_test (attr_rtx (NOT,
940 attr_eq (XSTR (exp, 0),
944 else if (n_comma_elts (XSTR (exp, 1)) == 1)
946 attr = find_attr (XSTR (exp, 0), 0);
949 if (! strcmp (XSTR (exp, 0), "alternative"))
951 XSTR (exp, 0) = alternative_name;
952 /* This can't be simplified any further. */
953 RTX_UNCHANGING_P (exp) = 1;
957 fatal ("Unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
960 if (is_const && ! attr->is_const)
961 fatal ("Constant expression uses insn attribute `%s' in EQ_ATTR",
964 /* Copy this just to make it permanent,
965 so expressions using it can be permanent too. */
966 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
968 /* It shouldn't be possible to simplify the value given to a
969 constant attribute, so don't expand this until it's time to
970 write the test expression. */
972 RTX_UNCHANGING_P (exp) = 1;
974 if (attr->is_numeric)
976 for (p = XSTR (exp, 1); *p; p++)
977 if (*p < '0' || *p > '9')
978 fatal ("Attribute `%s' takes only numeric values",
983 for (av = attr->first_value; av; av = av->next)
984 if (GET_CODE (av->value) == CONST_STRING
985 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
989 fatal ("Unknown value `%s' for `%s' attribute",
990 XSTR (exp, 1), XSTR (exp, 0));
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);
1012 /* Either TRUE or FALSE. */
1020 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const);
1021 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const);
1025 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const);
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 RTX_UNCHANGING_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 RTX_UNCHANGING_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 RTX_UNCHANGING_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 (exp, attr)
1075 struct attr_desc *attr;
1077 struct attr_value *av;
1081 switch (GET_CODE (exp))
1084 if (attr && ! attr->is_numeric)
1085 fatal ("CONST_INT not valid for non-numeric `%s' attribute",
1088 if (INTVAL (exp) < 0 && ! attr->negative_ok)
1089 fatal ("Negative numeric value specified for `%s' attribute",
1095 if (! strcmp (XSTR (exp, 0), "*"))
1098 if (attr == 0 || attr->is_numeric)
1101 if (attr && attr->negative_ok && *p == '-')
1104 if (*p > '9' || *p < '0')
1105 fatal ("Non-numeric value for numeric `%s' attribute",
1106 attr ? attr->name : "internal");
1110 for (av = attr->first_value; av; av = av->next)
1111 if (GET_CODE (av->value) == CONST_STRING
1112 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
1116 fatal ("Unknown value `%s' for `%s' attribute",
1117 XSTR (exp, 0), attr ? attr->name : "internal");
1122 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
1123 attr ? attr->is_const : 0);
1124 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1125 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
1133 if (attr && !attr->is_numeric)
1134 fatal ("Invalid operation `%s' for non-numeric attribute value",
1135 GET_RTX_NAME (GET_CODE (exp)));
1140 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1141 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1145 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1149 if (XVECLEN (exp, 0) % 2 != 0)
1150 fatal ("First operand of COND must have even length");
1152 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1154 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
1155 attr ? attr->is_const : 0);
1156 XVECEXP (exp, 0, i + 1)
1157 = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
1160 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1165 struct attr_desc *attr2 = find_attr (XSTR (exp, 0), 0);
1167 fatal ("Unknown attribute `%s' in ATTR", XSTR (exp, 0));
1168 else if ((attr && attr->is_const) && ! attr2->is_const)
1169 fatal ("Non-constant attribute `%s' referenced from `%s'",
1170 XSTR (exp, 0), attr->name);
1172 && (attr->is_numeric != attr2->is_numeric
1173 || (! attr->negative_ok && attr2->negative_ok)))
1174 fatal ("Numeric attribute mismatch calling `%s' from `%s'",
1175 XSTR (exp, 0), attr->name);
1180 /* A constant SYMBOL_REF is valid as a constant attribute test and
1181 is expanded later by make_canonical into a COND. In a non-constant
1182 attribute test, it is left be. */
1183 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1186 fatal ("Invalid operation `%s' for attribute value",
1187 GET_RTX_NAME (GET_CODE (exp)));
1193 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1194 It becomes a COND with each test being (eq_attr "alternative "n") */
1197 convert_set_attr_alternative (exp, num_alt, insn_index)
1205 if (XVECLEN (exp, 1) != num_alt)
1206 fatal ("Bad number of entries in SET_ATTR_ALTERNATIVE for insn %d",
1209 /* Make a COND with all tests but the last. Select the last value via the
1211 condexp = rtx_alloc (COND);
1212 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1214 for (i = 0; i < num_alt - 1; i++)
1217 p = attr_numeral (i);
1219 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1221 /* Sharing this EQ_ATTR rtl causes trouble. */
1222 XVECEXP (condexp, 0, 2 * i) = rtx_alloc (EQ_ATTR);
1223 XSTR (XVECEXP (condexp, 0, 2 * i), 0) = alternative_name;
1224 XSTR (XVECEXP (condexp, 0, 2 * i), 1) = p;
1226 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1229 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1231 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1234 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1235 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1238 convert_set_attr (exp, num_alt, insn_index)
1248 /* See how many alternative specified. */
1249 n = n_comma_elts (XSTR (exp, 1));
1251 return attr_rtx (SET,
1252 attr_rtx (ATTR, XSTR (exp, 0)),
1253 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1255 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1256 XSTR (newexp, 0) = XSTR (exp, 0);
1257 XVEC (newexp, 1) = rtvec_alloc (n);
1259 /* Process each comma-separated name. */
1260 name_ptr = XSTR (exp, 1);
1262 while ((p = next_comma_elt (&name_ptr)) != NULL)
1263 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1265 return convert_set_attr_alternative (newexp, num_alt, insn_index);
1268 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1269 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1275 struct insn_def *id;
1276 struct attr_desc *attr;
1280 for (id = defs; id; id = id->next)
1282 if (XVEC (id->def, id->vec_idx) == NULL)
1285 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1287 value = XVECEXP (id->def, id->vec_idx, i);
1288 switch (GET_CODE (value))
1291 if (GET_CODE (XEXP (value, 0)) != ATTR)
1292 fatal ("Bad attribute set in pattern %d", id->insn_index);
1295 case SET_ATTR_ALTERNATIVE:
1296 value = convert_set_attr_alternative (value,
1297 id->num_alternatives,
1302 value = convert_set_attr (value, id->num_alternatives,
1307 fatal ("Invalid attribute code `%s' for pattern %d",
1308 GET_RTX_NAME (GET_CODE (value)), id->insn_index);
1311 if ((attr = find_attr (XSTR (XEXP (value, 0), 0), 0)) == NULL)
1312 fatal ("Unknown attribute `%s' for pattern number %d",
1313 XSTR (XEXP (value, 0), 0), id->insn_index);
1315 XVECEXP (id->def, id->vec_idx, i) = value;
1316 XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1322 /* Given a constant SYMBOL_REF expression, convert to a COND that
1323 explicitly tests each enumerated value. */
1326 convert_const_symbol_ref (exp, attr)
1328 struct attr_desc *attr;
1331 struct attr_value *av;
1335 for (av = attr->first_value; av; av = av->next)
1338 /* Make a COND with all tests but the last, and in the original order.
1339 Select the last value via the default. Note that the attr values
1340 are constructed in reverse order. */
1342 condexp = rtx_alloc (COND);
1343 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1344 av = attr->first_value;
1345 XEXP (condexp, 1) = av->value;
1347 for (i = num_alt - 2; av = av->next, i >= 0; i--)
1352 string = p = (char *) oballoc (2
1353 + strlen (attr->name)
1354 + strlen (XSTR (av->value, 0)));
1355 strcpy (p, attr->name);
1357 strcat (p, XSTR (av->value, 0));
1358 for (; *p != '\0'; p++)
1359 if (*p >= 'a' && *p <= 'z')
1362 value = attr_rtx (SYMBOL_REF, string);
1363 RTX_UNCHANGING_P (value) = 1;
1365 XVECEXP (condexp, 0, 2 * i) = attr_rtx (EQ, exp, value);
1367 XVECEXP (condexp, 0, 2 * i + 1) = av->value;
1374 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1375 expressions by converting them into a COND. This removes cases from this
1376 program. Also, replace an attribute value of "*" with the default attribute
1380 make_canonical (attr, exp)
1381 struct attr_desc *attr;
1387 switch (GET_CODE (exp))
1390 exp = make_numeric_value (INTVAL (exp));
1394 if (! strcmp (XSTR (exp, 0), "*"))
1396 if (attr == 0 || attr->default_val == 0)
1397 fatal ("(attr_value \"*\") used in invalid context.");
1398 exp = attr->default_val->value;
1404 if (!attr->is_const || RTX_UNCHANGING_P (exp))
1406 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1407 This makes the COND something that won't be considered an arbitrary
1408 expression by walk_attr_value. */
1409 RTX_UNCHANGING_P (exp) = 1;
1411 /* ??? Why do we do this? With attribute values { A B C D E }, this
1412 tends to generate (!(x==A) && !(x==B) && !(x==C) && !(x==D)) rather
1414 exp = convert_const_symbol_ref (exp, attr);
1415 RTX_UNCHANGING_P (exp) = 1;
1416 exp = check_attr_value (exp, attr);
1417 /* Goto COND case since this is now a COND. Note that while the
1418 new expression is rescanned, all symbol_ref notes are marked as
1422 exp = check_attr_value (exp, attr);
1427 newexp = rtx_alloc (COND);
1428 XVEC (newexp, 0) = rtvec_alloc (2);
1429 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1430 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1432 XEXP (newexp, 1) = XEXP (exp, 2);
1435 /* Fall through to COND case since this is now a COND. */
1442 /* First, check for degenerate COND. */
1443 if (XVECLEN (exp, 0) == 0)
1444 return make_canonical (attr, XEXP (exp, 1));
1445 defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1447 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1449 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1450 XVECEXP (exp, 0, i + 1)
1451 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1452 if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1471 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1472 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1473 copy_boolean (XEXP (exp, 1)));
1477 /* Given a value and an attribute description, return a `struct attr_value *'
1478 that represents that value. This is either an existing structure, if the
1479 value has been previously encountered, or a newly-created structure.
1481 `insn_code' is the code of an insn whose attribute has the specified
1482 value (-2 if not processing an insn). We ensure that all insns for
1483 a given value have the same number of alternatives if the value checks
1486 static struct attr_value *
1487 get_attr_value (value, attr, insn_code)
1489 struct attr_desc *attr;
1492 struct attr_value *av;
1495 value = make_canonical (attr, value);
1496 if (compares_alternatives_p (value))
1498 if (insn_code < 0 || insn_alternatives == NULL)
1499 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1501 num_alt = insn_alternatives[insn_code];
1504 for (av = attr->first_value; av; av = av->next)
1505 if (rtx_equal_p (value, av->value)
1506 && (num_alt == 0 || av->first_insn == NULL
1507 || insn_alternatives[av->first_insn->insn_code]))
1510 av = (struct attr_value *) oballoc (sizeof (struct attr_value));
1512 av->next = attr->first_value;
1513 attr->first_value = av;
1514 av->first_insn = NULL;
1516 av->has_asm_insn = 0;
1521 /* After all DEFINE_DELAYs have been read in, create internal attributes
1522 to generate the required routines.
1524 First, we compute the number of delay slots for each insn (as a COND of
1525 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1526 delay type is specified, we compute a similar function giving the
1527 DEFINE_DELAY ordinal for each insn.
1529 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1530 tells whether a given insn can be in that delay slot.
1532 Normal attribute filling and optimization expands these to contain the
1533 information needed to handle delay slots. */
1538 struct delay_desc *delay;
1544 /* First, generate data for `num_delay_slots' function. */
1546 condexp = rtx_alloc (COND);
1547 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1548 XEXP (condexp, 1) = make_numeric_value (0);
1550 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1552 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1553 XVECEXP (condexp, 0, i + 1)
1554 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1557 make_internal_attr ("*num_delay_slots", condexp, 0);
1559 /* If more than one delay type, do the same for computing the delay type. */
1562 condexp = rtx_alloc (COND);
1563 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1564 XEXP (condexp, 1) = make_numeric_value (0);
1566 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1568 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1569 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1572 make_internal_attr ("*delay_type", condexp, 1);
1575 /* For each delay possibility and delay slot, compute an eligibility
1576 attribute for non-annulled insns and for each type of annulled (annul
1577 if true and annul if false). */
1578 for (delay = delays; delay; delay = delay->next)
1580 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1582 condexp = XVECEXP (delay->def, 1, i);
1583 if (condexp == 0) condexp = false_rtx;
1584 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1585 make_numeric_value (1), make_numeric_value (0));
1587 p = attr_printf (sizeof ("*delay__") + MAX_DIGITS*2, "*delay_%d_%d",
1589 make_internal_attr (p, newexp, 1);
1591 if (have_annul_true)
1593 condexp = XVECEXP (delay->def, 1, i + 1);
1594 if (condexp == 0) condexp = false_rtx;
1595 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1596 make_numeric_value (1),
1597 make_numeric_value (0));
1598 p = attr_printf (sizeof ("*annul_true__") + MAX_DIGITS*2,
1599 "*annul_true_%d_%d", delay->num, i / 3);
1600 make_internal_attr (p, newexp, 1);
1603 if (have_annul_false)
1605 condexp = XVECEXP (delay->def, 1, i + 2);
1606 if (condexp == 0) condexp = false_rtx;
1607 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1608 make_numeric_value (1),
1609 make_numeric_value (0));
1610 p = attr_printf (sizeof ("*annul_false__") + MAX_DIGITS*2,
1611 "*annul_false_%d_%d", delay->num, i / 3);
1612 make_internal_attr (p, newexp, 1);
1618 /* This function is given a left and right side expression and an operator.
1619 Each side is a conditional expression, each alternative of which has a
1620 numerical value. The function returns another conditional expression
1621 which, for every possible set of condition values, returns a value that is
1622 the operator applied to the values of the two sides.
1624 Since this is called early, it must also support IF_THEN_ELSE. */
1627 operate_exp (op, left, right)
1631 int left_value, right_value;
1635 /* If left is a string, apply operator to it and the right side. */
1636 if (GET_CODE (left) == CONST_STRING)
1638 /* If right is also a string, just perform the operation. */
1639 if (GET_CODE (right) == CONST_STRING)
1641 left_value = atoi (XSTR (left, 0));
1642 right_value = atoi (XSTR (right, 0));
1646 i = left_value + right_value;
1650 i = left_value - right_value;
1653 case POS_MINUS_OP: /* The positive part of LEFT - RIGHT. */
1654 if (left_value > right_value)
1655 i = left_value - right_value;
1662 i = left_value | right_value;
1666 i = left_value == right_value;
1670 i = (left_value << (HOST_BITS_PER_INT / 2)) | right_value;
1674 if (left_value > right_value)
1681 if (left_value < right_value)
1691 if (i == left_value)
1693 if (i == right_value)
1695 return make_numeric_value (i);
1697 else if (GET_CODE (right) == IF_THEN_ELSE)
1699 /* Apply recursively to all values within. */
1700 rtx newleft = operate_exp (op, left, XEXP (right, 1));
1701 rtx newright = operate_exp (op, left, XEXP (right, 2));
1702 if (rtx_equal_p (newleft, newright))
1704 return attr_rtx (IF_THEN_ELSE, XEXP (right, 0), newleft, newright);
1706 else if (GET_CODE (right) == COND)
1711 newexp = rtx_alloc (COND);
1712 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (right, 0));
1713 defval = XEXP (newexp, 1) = operate_exp (op, left, XEXP (right, 1));
1715 for (i = 0; i < XVECLEN (right, 0); i += 2)
1717 XVECEXP (newexp, 0, i) = XVECEXP (right, 0, i);
1718 XVECEXP (newexp, 0, i + 1)
1719 = operate_exp (op, left, XVECEXP (right, 0, i + 1));
1720 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1725 /* If the resulting cond is trivial (all alternatives
1726 give the same value), optimize it away. */
1729 obstack_free (rtl_obstack, newexp);
1730 return operate_exp (op, left, XEXP (right, 1));
1733 /* If the result is the same as the RIGHT operand,
1735 if (rtx_equal_p (newexp, right))
1737 obstack_free (rtl_obstack, newexp);
1744 fatal ("Badly formed attribute value");
1747 /* A hack to prevent expand_units from completely blowing up: ORX_OP does
1748 not associate through IF_THEN_ELSE. */
1749 else if (op == ORX_OP && GET_CODE (right) == IF_THEN_ELSE)
1751 return attr_rtx (IOR, left, right);
1754 /* Otherwise, do recursion the other way. */
1755 else if (GET_CODE (left) == IF_THEN_ELSE)
1757 rtx newleft = operate_exp (op, XEXP (left, 1), right);
1758 rtx newright = operate_exp (op, XEXP (left, 2), right);
1759 if (rtx_equal_p (newleft, newright))
1761 return attr_rtx (IF_THEN_ELSE, XEXP (left, 0), newleft, newright);
1763 else if (GET_CODE (left) == COND)
1768 newexp = rtx_alloc (COND);
1769 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (left, 0));
1770 defval = XEXP (newexp, 1) = operate_exp (op, XEXP (left, 1), right);
1772 for (i = 0; i < XVECLEN (left, 0); i += 2)
1774 XVECEXP (newexp, 0, i) = XVECEXP (left, 0, i);
1775 XVECEXP (newexp, 0, i + 1)
1776 = operate_exp (op, XVECEXP (left, 0, i + 1), right);
1777 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1782 /* If the cond is trivial (all alternatives give the same value),
1783 optimize it away. */
1786 obstack_free (rtl_obstack, newexp);
1787 return operate_exp (op, XEXP (left, 1), right);
1790 /* If the result is the same as the LEFT operand,
1792 if (rtx_equal_p (newexp, left))
1794 obstack_free (rtl_obstack, newexp);
1802 fatal ("Badly formed attribute value.");
1807 /* Once all attributes and DEFINE_FUNCTION_UNITs have been read, we
1808 construct a number of attributes.
1810 The first produces a function `function_units_used' which is given an
1811 insn and produces an encoding showing which function units are required
1812 for the execution of that insn. If the value is non-negative, the insn
1813 uses that unit; otherwise, the value is a one's compliment mask of units
1816 The second produces a function `result_ready_cost' which is used to
1817 determine the time that the result of an insn will be ready and hence
1818 a worst-case schedule.
1820 Both of these produce quite complex expressions which are then set as the
1821 default value of internal attributes. Normal attribute simplification
1822 should produce reasonable expressions.
1824 For each unit, a `<name>_unit_ready_cost' function will take an
1825 insn and give the delay until that unit will be ready with the result
1826 and a `<name>_unit_conflict_cost' function is given an insn already
1827 executing on the unit and a candidate to execute and will give the
1828 cost from the time the executing insn started until the candidate
1829 can start (ignore limitations on the number of simultaneous insns).
1831 For each unit, a `<name>_unit_blockage' function is given an insn
1832 already executing on the unit and a candidate to execute and will
1833 give the delay incurred due to function unit conflicts. The range of
1834 blockage cost values for a given executing insn is given by the
1835 `<name>_unit_blockage_range' function. These values are encoded in
1836 an int where the upper half gives the minimum value and the lower
1837 half gives the maximum value. */
1842 struct function_unit *unit, **unit_num;
1843 struct function_unit_op *op, **op_array, ***unit_ops;
1848 int i, j, u, num, nvalues;
1850 /* Rebuild the condition for the unit to share the RTL expressions.
1851 Sharing is required by simplify_by_exploding. Build the issue delay
1852 expressions. Validate the expressions we were given for the conditions
1853 and conflict vector. Then make attributes for use in the conflict
1856 for (unit = units; unit; unit = unit->next)
1858 unit->condexp = check_attr_test (unit->condexp, 0);
1860 for (op = unit->ops; op; op = op->next)
1862 rtx issue_delay = make_numeric_value (op->issue_delay);
1863 rtx issue_exp = issue_delay;
1865 /* Build, validate, and simplify the issue delay expression. */
1866 if (op->conflict_exp != true_rtx)
1867 issue_exp = attr_rtx (IF_THEN_ELSE, op->conflict_exp,
1868 issue_exp, make_numeric_value (0));
1869 issue_exp = check_attr_value (make_canonical (NULL_ATTR,
1872 issue_exp = simplify_knowing (issue_exp, unit->condexp);
1873 op->issue_exp = issue_exp;
1875 /* Make an attribute for use in the conflict function if needed. */
1876 unit->needs_conflict_function = (unit->issue_delay.min
1877 != unit->issue_delay.max);
1878 if (unit->needs_conflict_function)
1880 str = attr_printf (strlen (unit->name) + sizeof ("*_cost_") + MAX_DIGITS,
1881 "*%s_cost_%d", unit->name, op->num);
1882 make_internal_attr (str, issue_exp, 1);
1885 /* Validate the condition. */
1886 op->condexp = check_attr_test (op->condexp, 0);
1890 /* Compute the mask of function units used. Initially, the unitsmask is
1891 zero. Set up a conditional to compute each unit's contribution. */
1892 unitsmask = make_numeric_value (0);
1893 newexp = rtx_alloc (IF_THEN_ELSE);
1894 XEXP (newexp, 2) = make_numeric_value (0);
1896 /* If we have just a few units, we may be all right expanding the whole
1897 thing. But the expansion is 2**N in space on the number of opclasses,
1898 so we can't do this for very long -- Alpha and MIPS in particular have
1899 problems with this. So in that situation, we fall back on an alternate
1900 implementation method. */
1901 #define NUM_UNITOP_CUTOFF 20
1903 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1905 /* Merge each function unit into the unit mask attributes. */
1906 for (unit = units; unit; unit = unit->next)
1908 XEXP (newexp, 0) = unit->condexp;
1909 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1910 unitsmask = operate_exp (OR_OP, unitsmask, newexp);
1915 /* Merge each function unit into the unit mask attributes. */
1916 for (unit = units; unit; unit = unit->next)
1918 XEXP (newexp, 0) = unit->condexp;
1919 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1920 unitsmask = operate_exp (ORX_OP, unitsmask, attr_copy_rtx (newexp));
1924 /* Simplify the unit mask expression, encode it, and make an attribute
1925 for the function_units_used function. */
1926 unitsmask = simplify_by_exploding (unitsmask);
1928 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1929 unitsmask = encode_units_mask (unitsmask);
1932 /* We can no longer encode unitsmask at compile time, so emit code to
1933 calculate it at runtime. Rather, put a marker for where we'd do
1934 the code, and actually output it in write_attr_get(). */
1935 unitsmask = attr_rtx (FFS, unitsmask);
1938 make_internal_attr ("*function_units_used", unitsmask, 10);
1940 /* Create an array of ops for each unit. Add an extra unit for the
1941 result_ready_cost function that has the ops of all other units. */
1942 unit_ops = (struct function_unit_op ***)
1943 alloca ((num_units + 1) * sizeof (struct function_unit_op **));
1944 unit_num = (struct function_unit **)
1945 alloca ((num_units + 1) * sizeof (struct function_unit *));
1947 unit_num[num_units] = unit = (struct function_unit *)
1948 alloca (sizeof (struct function_unit));
1949 unit->num = num_units;
1950 unit->num_opclasses = 0;
1952 for (unit = units; unit; unit = unit->next)
1954 unit_num[num_units]->num_opclasses += unit->num_opclasses;
1955 unit_num[unit->num] = unit;
1956 unit_ops[unit->num] = op_array = (struct function_unit_op **)
1957 alloca (unit->num_opclasses * sizeof (struct function_unit_op *));
1959 for (op = unit->ops; op; op = op->next)
1960 op_array[op->num] = op;
1963 /* Compose the array of ops for the extra unit. */
1964 unit_ops[num_units] = op_array = (struct function_unit_op **)
1965 alloca (unit_num[num_units]->num_opclasses
1966 * sizeof (struct function_unit_op *));
1968 for (unit = units, i = 0; unit; i += unit->num_opclasses, unit = unit->next)
1969 bcopy ((char *) unit_ops[unit->num], (char *) &op_array[i],
1970 unit->num_opclasses * sizeof (struct function_unit_op *));
1972 /* Compute the ready cost function for each unit by computing the
1973 condition for each non-default value. */
1974 for (u = 0; u <= num_units; u++)
1980 op_array = unit_ops[unit->num];
1981 num = unit->num_opclasses;
1983 /* Sort the array of ops into increasing ready cost order. */
1984 for (i = 0; i < num; i++)
1985 for (j = num - 1; j > i; j--)
1986 if (op_array[j-1]->ready < op_array[j]->ready)
1989 op_array[j] = op_array[j-1];
1993 /* Determine how many distinct non-default ready cost values there
1994 are. We use a default ready cost value of 1. */
1995 nvalues = 0; value = 1;
1996 for (i = num - 1; i >= 0; i--)
1997 if (op_array[i]->ready > value)
1999 value = op_array[i]->ready;
2004 readycost = make_numeric_value (1);
2007 /* Construct the ready cost expression as a COND of each value from
2008 the largest to the smallest. */
2009 readycost = rtx_alloc (COND);
2010 XVEC (readycost, 0) = rtvec_alloc (nvalues * 2);
2011 XEXP (readycost, 1) = make_numeric_value (1);
2013 nvalues = 0; orexp = false_rtx; value = op_array[0]->ready;
2014 for (i = 0; i < num; i++)
2019 else if (op->ready == value)
2020 orexp = insert_right_side (IOR, orexp, op->condexp, -2, -2);
2023 XVECEXP (readycost, 0, nvalues * 2) = orexp;
2024 XVECEXP (readycost, 0, nvalues * 2 + 1)
2025 = make_numeric_value (value);
2028 orexp = op->condexp;
2031 XVECEXP (readycost, 0, nvalues * 2) = orexp;
2032 XVECEXP (readycost, 0, nvalues * 2 + 1) = make_numeric_value (value);
2037 rtx max_blockage = 0, min_blockage = 0;
2039 /* Simplify the readycost expression by only considering insns
2040 that use the unit. */
2041 readycost = simplify_knowing (readycost, unit->condexp);
2043 /* Determine the blockage cost the executing insn (E) given
2044 the candidate insn (C). This is the maximum of the issue
2045 delay, the pipeline delay, and the simultaneity constraint.
2046 Each function_unit_op represents the characteristics of the
2047 candidate insn, so in the expressions below, C is a known
2048 term and E is an unknown term.
2050 We compute the blockage cost for each E for every possible C.
2051 Thus OP represents E, and READYCOST is a list of values for
2054 The issue delay function for C is op->issue_exp and is used to
2055 write the `<name>_unit_conflict_cost' function. Symbolicly
2056 this is "ISSUE-DELAY (E,C)".
2058 The pipeline delay results form the FIFO constraint on the
2059 function unit and is "READY-COST (E) + 1 - READY-COST (C)".
2061 The simultaneity constraint is based on how long it takes to
2062 fill the unit given the minimum issue delay. FILL-TIME is the
2063 constant "MIN (ISSUE-DELAY (*,*)) * (SIMULTANEITY - 1)", and
2064 the simultaneity constraint is "READY-COST (E) - FILL-TIME"
2065 if SIMULTANEITY is non-zero and zero otherwise.
2067 Thus, BLOCKAGE (E,C) when SIMULTANEITY is zero is
2069 MAX (ISSUE-DELAY (E,C),
2070 READY-COST (E) - (READY-COST (C) - 1))
2074 MAX (ISSUE-DELAY (E,C),
2075 READY-COST (E) - (READY-COST (C) - 1),
2076 READY-COST (E) - FILL-TIME)
2078 The `<name>_unit_blockage' function is computed by determining
2079 this value for each candidate insn. As these values are
2080 computed, we also compute the upper and lower bounds for
2081 BLOCKAGE (E,*). These are combined to form the function
2082 `<name>_unit_blockage_range'. Finally, the maximum blockage
2083 cost, MAX (BLOCKAGE (*,*)), is computed. */
2085 for (op = unit->ops; op; op = op->next)
2087 rtx blockage = op->issue_exp;
2088 blockage = simplify_knowing (blockage, unit->condexp);
2090 /* Add this op's contribution to MAX (BLOCKAGE (E,*)) and
2091 MIN (BLOCKAGE (E,*)). */
2092 if (max_blockage == 0)
2093 max_blockage = min_blockage = blockage;
2097 = simplify_knowing (operate_exp (MAX_OP, max_blockage,
2101 = simplify_knowing (operate_exp (MIN_OP, min_blockage,
2106 /* Make an attribute for use in the blockage function. */
2107 str = attr_printf (strlen (unit->name) + sizeof ("*_block_") + MAX_DIGITS,
2108 "*%s_block_%d", unit->name, op->num);
2109 make_internal_attr (str, blockage, 1);
2112 /* Record MAX (BLOCKAGE (*,*)). */
2115 unit->max_blockage = max_attr_value (max_blockage, &unknown);
2118 /* See if the upper and lower bounds of BLOCKAGE (E,*) are the
2119 same. If so, the blockage function carries no additional
2120 information and is not written. */
2121 newexp = operate_exp (EQ_OP, max_blockage, min_blockage);
2122 newexp = simplify_knowing (newexp, unit->condexp);
2123 unit->needs_blockage_function
2124 = (GET_CODE (newexp) != CONST_STRING
2125 || atoi (XSTR (newexp, 0)) != 1);
2127 /* If the all values of BLOCKAGE (E,C) have the same value,
2128 neither blockage function is written. */
2129 unit->needs_range_function
2130 = (unit->needs_blockage_function
2131 || GET_CODE (max_blockage) != CONST_STRING);
2133 if (unit->needs_range_function)
2135 /* Compute the blockage range function and make an attribute
2136 for writing its value. */
2137 newexp = operate_exp (RANGE_OP, min_blockage, max_blockage);
2138 newexp = simplify_knowing (newexp, unit->condexp);
2140 str = attr_printf (strlen (unit->name) + sizeof ("*_unit_blockage_range"),
2141 "*%s_unit_blockage_range", unit->name);
2142 make_internal_attr (str, newexp, 20);
2145 str = attr_printf (strlen (unit->name) + sizeof ("*_unit_ready_cost"),
2146 "*%s_unit_ready_cost", unit->name);
2149 str = "*result_ready_cost";
2151 /* Make an attribute for the ready_cost function. Simplifying
2152 further with simplify_by_exploding doesn't win. */
2153 make_internal_attr (str, readycost, 0);
2156 /* For each unit that requires a conflict cost function, make an attribute
2157 that maps insns to the operation number. */
2158 for (unit = units; unit; unit = unit->next)
2162 if (! unit->needs_conflict_function
2163 && ! unit->needs_blockage_function)
2166 caseexp = rtx_alloc (COND);
2167 XVEC (caseexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
2169 for (op = unit->ops; op; op = op->next)
2171 /* Make our adjustment to the COND being computed. If we are the
2172 last operation class, place our values into the default of the
2174 if (op->num == unit->num_opclasses - 1)
2176 XEXP (caseexp, 1) = make_numeric_value (op->num);
2180 XVECEXP (caseexp, 0, op->num * 2) = op->condexp;
2181 XVECEXP (caseexp, 0, op->num * 2 + 1)
2182 = make_numeric_value (op->num);
2186 /* Simplifying caseexp with simplify_by_exploding doesn't win. */
2187 str = attr_printf (strlen (unit->name) + sizeof ("*_cases"),
2188 "*%s_cases", unit->name);
2189 make_internal_attr (str, caseexp, 1);
2193 /* Simplify EXP given KNOWN_TRUE. */
2196 simplify_knowing (exp, known_true)
2197 rtx exp, known_true;
2199 if (GET_CODE (exp) != CONST_STRING)
2201 int unknown = 0, max;
2202 max = max_attr_value (exp, &unknown);
2205 exp = attr_rtx (IF_THEN_ELSE, known_true, exp,
2206 make_numeric_value (max));
2207 exp = simplify_by_exploding (exp);
2213 /* Translate the CONST_STRING expressions in X to change the encoding of
2214 value. On input, the value is a bitmask with a one bit for each unit
2215 used; on output, the value is the unit number (zero based) if one
2216 and only one unit is used or the one's compliment of the bitmask. */
2219 encode_units_mask (x)
2224 register enum rtx_code code;
2225 register const char *fmt;
2227 code = GET_CODE (x);
2232 i = atoi (XSTR (x, 0));
2234 abort (); /* The sign bit encodes a one's compliment mask. */
2235 else if (i != 0 && i == (i & -i))
2236 /* Only one bit is set, so yield that unit number. */
2237 for (j = 0; (i >>= 1) != 0; j++)
2241 return attr_rtx (CONST_STRING, attr_printf (MAX_DIGITS, "%d", j));
2258 /* Compare the elements. If any pair of corresponding elements
2259 fail to match, return 0 for the whole things. */
2261 fmt = GET_RTX_FORMAT (code);
2262 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2268 for (j = 0; j < XVECLEN (x, i); j++)
2269 XVECEXP (x, i, j) = encode_units_mask (XVECEXP (x, i, j));
2273 XEXP (x, i) = encode_units_mask (XEXP (x, i));
2280 /* Once all attributes and insns have been read and checked, we construct for
2281 each attribute value a list of all the insns that have that value for
2286 struct attr_desc *attr;
2288 struct attr_value *av;
2289 struct insn_ent *ie;
2290 struct insn_def *id;
2294 /* Don't fill constant attributes. The value is independent of
2295 any particular insn. */
2299 for (id = defs; id; id = id->next)
2301 /* If no value is specified for this insn for this attribute, use the
2304 if (XVEC (id->def, id->vec_idx))
2305 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
2306 if (! strcmp (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
2308 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
2311 av = attr->default_val;
2313 av = get_attr_value (value, attr, id->insn_code);
2315 ie = (struct insn_ent *) oballoc (sizeof (struct insn_ent));
2316 ie->insn_code = id->insn_code;
2317 ie->insn_index = id->insn_code;
2318 insert_insn_ent (av, ie);
2322 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
2323 test that checks relative positions of insns (uses MATCH_DUP or PC).
2324 If so, replace it with what is obtained by passing the expression to
2325 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
2326 recursively on each value (including the default value). Otherwise,
2327 return the value returned by NO_ADDRESS_FN applied to EXP. */
2330 substitute_address (exp, no_address_fn, address_fn)
2332 rtx (*no_address_fn) PROTO ((rtx));
2333 rtx (*address_fn) PROTO ((rtx));
2338 if (GET_CODE (exp) == COND)
2340 /* See if any tests use addresses. */
2342 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2343 walk_attr_value (XVECEXP (exp, 0, i));
2346 return (*address_fn) (exp);
2348 /* Make a new copy of this COND, replacing each element. */
2349 newexp = rtx_alloc (COND);
2350 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
2351 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2353 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
2354 XVECEXP (newexp, 0, i + 1)
2355 = substitute_address (XVECEXP (exp, 0, i + 1),
2356 no_address_fn, address_fn);
2359 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
2360 no_address_fn, address_fn);
2365 else if (GET_CODE (exp) == IF_THEN_ELSE)
2368 walk_attr_value (XEXP (exp, 0));
2370 return (*address_fn) (exp);
2372 return attr_rtx (IF_THEN_ELSE,
2373 substitute_address (XEXP (exp, 0),
2374 no_address_fn, address_fn),
2375 substitute_address (XEXP (exp, 1),
2376 no_address_fn, address_fn),
2377 substitute_address (XEXP (exp, 2),
2378 no_address_fn, address_fn));
2381 return (*no_address_fn) (exp);
2384 /* Make new attributes from the `length' attribute. The following are made,
2385 each corresponding to a function called from `shorten_branches' or
2388 *insn_default_length This is the length of the insn to be returned
2389 by `get_attr_length' before `shorten_branches'
2390 has been called. In each case where the length
2391 depends on relative addresses, the largest
2392 possible is used. This routine is also used
2393 to compute the initial size of the insn.
2395 *insn_variable_length_p This returns 1 if the insn's length depends
2396 on relative addresses, zero otherwise.
2398 *insn_current_length This is only called when it is known that the
2399 insn has a variable length and returns the
2400 current length, based on relative addresses.
2404 make_length_attrs ()
2406 static const char *new_names[] = {"*insn_default_length",
2407 "*insn_variable_length_p",
2408 "*insn_current_length"};
2409 static rtx (*no_address_fn[]) PROTO((rtx)) = {identity_fn, zero_fn, zero_fn};
2410 static rtx (*address_fn[]) PROTO((rtx)) = {max_fn, one_fn, identity_fn};
2412 struct attr_desc *length_attr, *new_attr;
2413 struct attr_value *av, *new_av;
2414 struct insn_ent *ie, *new_ie;
2416 /* See if length attribute is defined. If so, it must be numeric. Make
2417 it special so we don't output anything for it. */
2418 length_attr = find_attr ("length", 0);
2419 if (length_attr == 0)
2422 if (! length_attr->is_numeric)
2423 fatal ("length attribute must be numeric.");
2425 length_attr->is_const = 0;
2426 length_attr->is_special = 1;
2428 /* Make each new attribute, in turn. */
2429 for (i = 0; i < sizeof new_names / sizeof new_names[0]; i++)
2431 make_internal_attr (new_names[i],
2432 substitute_address (length_attr->default_val->value,
2433 no_address_fn[i], address_fn[i]),
2435 new_attr = find_attr (new_names[i], 0);
2436 for (av = length_attr->first_value; av; av = av->next)
2437 for (ie = av->first_insn; ie; ie = ie->next)
2439 new_av = get_attr_value (substitute_address (av->value,
2442 new_attr, ie->insn_code);
2443 new_ie = (struct insn_ent *) oballoc (sizeof (struct insn_ent));
2444 new_ie->insn_code = ie->insn_code;
2445 new_ie->insn_index = ie->insn_index;
2446 insert_insn_ent (new_av, new_ie);
2451 /* Utility functions called from above routine. */
2462 rtx exp ATTRIBUTE_UNUSED;
2464 return make_numeric_value (0);
2469 rtx exp ATTRIBUTE_UNUSED;
2471 return make_numeric_value (1);
2479 return make_numeric_value (max_attr_value (exp, &unknown));
2483 write_length_unit_log ()
2485 struct attr_desc *length_attr = find_attr ("length", 0);
2486 struct attr_value *av;
2487 struct insn_ent *ie;
2488 unsigned int length_unit_log, length_or;
2491 if (length_attr == 0)
2493 length_or = or_attr_value (length_attr->default_val->value, &unknown);
2494 for (av = length_attr->first_value; av; av = av->next)
2495 for (ie = av->first_insn; ie; ie = ie->next)
2496 length_or |= or_attr_value (av->value, &unknown);
2499 length_unit_log = 0;
2502 length_or = ~length_or;
2503 for (length_unit_log = 0; length_or & 1; length_or >>= 1)
2506 printf ("int length_unit_log = %u;\n", length_unit_log);
2509 /* Take a COND expression and see if any of the conditions in it can be
2510 simplified. If any are known true or known false for the particular insn
2511 code, the COND can be further simplified.
2513 Also call ourselves on any COND operations that are values of this COND.
2515 We do not modify EXP; rather, we make and return a new rtx. */
2518 simplify_cond (exp, insn_code, insn_index)
2520 int insn_code, insn_index;
2523 /* We store the desired contents here,
2524 then build a new expression if they don't match EXP. */
2525 rtx defval = XEXP (exp, 1);
2526 rtx new_defval = XEXP (exp, 1);
2527 int len = XVECLEN (exp, 0);
2528 rtunion *tests = (rtunion *) alloca (len * sizeof (rtunion));
2532 /* This lets us free all storage allocated below, if appropriate. */
2533 first_spacer = (char *) obstack_finish (rtl_obstack);
2535 bcopy ((char *) XVEC (exp, 0)->elem, (char *) tests, len * sizeof (rtunion));
2537 /* See if default value needs simplification. */
2538 if (GET_CODE (defval) == COND)
2539 new_defval = simplify_cond (defval, insn_code, insn_index);
2541 /* Simplify the subexpressions, and see what tests we can get rid of. */
2543 for (i = 0; i < len; i += 2)
2545 rtx newtest, newval;
2547 /* Simplify this test. */
2548 newtest = SIMPLIFY_TEST_EXP (tests[i].rtx, insn_code, insn_index);
2549 tests[i].rtx = newtest;
2551 newval = tests[i + 1].rtx;
2552 /* See if this value may need simplification. */
2553 if (GET_CODE (newval) == COND)
2554 newval = simplify_cond (newval, insn_code, insn_index);
2556 /* Look for ways to delete or combine this test. */
2557 if (newtest == true_rtx)
2559 /* If test is true, make this value the default
2560 and discard this + any following tests. */
2562 defval = tests[i + 1].rtx;
2563 new_defval = newval;
2566 else if (newtest == false_rtx)
2568 /* If test is false, discard it and its value. */
2569 for (j = i; j < len - 2; j++)
2570 tests[j].rtx = tests[j + 2].rtx;
2574 else if (i > 0 && attr_equal_p (newval, tests[i - 1].rtx))
2576 /* If this value and the value for the prev test are the same,
2580 = insert_right_side (IOR, tests[i - 2].rtx, newtest,
2581 insn_code, insn_index);
2583 /* Delete this test/value. */
2584 for (j = i; j < len - 2; j++)
2585 tests[j].rtx = tests[j + 2].rtx;
2590 tests[i + 1].rtx = newval;
2593 /* If the last test in a COND has the same value
2594 as the default value, that test isn't needed. */
2596 while (len > 0 && attr_equal_p (tests[len - 1].rtx, new_defval))
2599 /* See if we changed anything. */
2600 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
2603 for (i = 0; i < len; i++)
2604 if (! attr_equal_p (tests[i].rtx, XVECEXP (exp, 0, i)))
2612 obstack_free (rtl_obstack, first_spacer);
2613 if (GET_CODE (defval) == COND)
2614 return simplify_cond (defval, insn_code, insn_index);
2619 obstack_free (rtl_obstack, first_spacer);
2624 rtx newexp = rtx_alloc (COND);
2626 XVEC (newexp, 0) = rtvec_alloc (len);
2627 bcopy ((char *) tests, (char *) XVEC (newexp, 0)->elem,
2628 len * sizeof (rtunion));
2629 XEXP (newexp, 1) = new_defval;
2634 /* Remove an insn entry from an attribute value. */
2637 remove_insn_ent (av, ie)
2638 struct attr_value *av;
2639 struct insn_ent *ie;
2641 struct insn_ent *previe;
2643 if (av->first_insn == ie)
2644 av->first_insn = ie->next;
2647 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
2649 previe->next = ie->next;
2653 if (ie->insn_code == -1)
2654 av->has_asm_insn = 0;
2659 /* Insert an insn entry in an attribute value list. */
2662 insert_insn_ent (av, ie)
2663 struct attr_value *av;
2664 struct insn_ent *ie;
2666 ie->next = av->first_insn;
2667 av->first_insn = ie;
2669 if (ie->insn_code == -1)
2670 av->has_asm_insn = 1;
2675 /* This is a utility routine to take an expression that is a tree of either
2676 AND or IOR expressions and insert a new term. The new term will be
2677 inserted at the right side of the first node whose code does not match
2678 the root. A new node will be created with the root's code. Its left
2679 side will be the old right side and its right side will be the new
2682 If the `term' is itself a tree, all its leaves will be inserted. */
2685 insert_right_side (code, exp, term, insn_code, insn_index)
2689 int insn_code, insn_index;
2693 /* Avoid consing in some special cases. */
2694 if (code == AND && term == true_rtx)
2696 if (code == AND && term == false_rtx)
2698 if (code == AND && exp == true_rtx)
2700 if (code == AND && exp == false_rtx)
2702 if (code == IOR && term == true_rtx)
2704 if (code == IOR && term == false_rtx)
2706 if (code == IOR && exp == true_rtx)
2708 if (code == IOR && exp == false_rtx)
2710 if (attr_equal_p (exp, term))
2713 if (GET_CODE (term) == code)
2715 exp = insert_right_side (code, exp, XEXP (term, 0),
2716 insn_code, insn_index);
2717 exp = insert_right_side (code, exp, XEXP (term, 1),
2718 insn_code, insn_index);
2723 if (GET_CODE (exp) == code)
2725 rtx new = insert_right_side (code, XEXP (exp, 1),
2726 term, insn_code, insn_index);
2727 if (new != XEXP (exp, 1))
2728 /* Make a copy of this expression and call recursively. */
2729 newexp = attr_rtx (code, XEXP (exp, 0), new);
2735 /* Insert the new term. */
2736 newexp = attr_rtx (code, exp, term);
2739 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2742 /* If we have an expression which AND's a bunch of
2743 (not (eq_attrq "alternative" "n"))
2744 terms, we may have covered all or all but one of the possible alternatives.
2745 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
2747 This routine is passed an expression and either AND or IOR. It returns a
2748 bitmask indicating which alternatives are mentioned within EXP. */
2751 compute_alternative_mask (exp, code)
2756 if (GET_CODE (exp) == code)
2757 return compute_alternative_mask (XEXP (exp, 0), code)
2758 | compute_alternative_mask (XEXP (exp, 1), code);
2760 else if (code == AND && GET_CODE (exp) == NOT
2761 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2762 && XSTR (XEXP (exp, 0), 0) == alternative_name)
2763 string = XSTR (XEXP (exp, 0), 1);
2765 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
2766 && XSTR (exp, 0) == alternative_name)
2767 string = XSTR (exp, 1);
2773 return 1 << (string[0] - '0');
2774 return 1 << atoi (string);
2777 /* Given I, a single-bit mask, return RTX to compare the `alternative'
2778 attribute with the value represented by that bit. */
2781 make_alternative_compare (mask)
2788 for (i = 0; (mask & (1 << i)) == 0; i++)
2791 newexp = attr_rtx (EQ_ATTR, alternative_name, attr_numeral (i));
2792 RTX_UNCHANGING_P (newexp) = 1;
2797 /* If we are processing an (eq_attr "attr" "value") test, we find the value
2798 of "attr" for this insn code. From that value, we can compute a test
2799 showing when the EQ_ATTR will be true. This routine performs that
2800 computation. If a test condition involves an address, we leave the EQ_ATTR
2801 intact because addresses are only valid for the `length' attribute.
2803 EXP is the EQ_ATTR expression and VALUE is the value of that attribute
2804 for the insn corresponding to INSN_CODE and INSN_INDEX. */
2807 evaluate_eq_attr (exp, value, insn_code, insn_index)
2810 int insn_code, insn_index;
2817 if (GET_CODE (value) == CONST_STRING)
2819 if (! strcmp (XSTR (value, 0), XSTR (exp, 1)))
2824 else if (GET_CODE (value) == SYMBOL_REF)
2828 if (GET_CODE (exp) != EQ_ATTR)
2831 string = (char *) alloca (2 + strlen (XSTR (exp, 0))
2832 + strlen (XSTR (exp, 1)));
2833 strcpy (string, XSTR (exp, 0));
2834 strcat (string, "_");
2835 strcat (string, XSTR (exp, 1));
2836 for (p = string; *p ; p++)
2837 if (*p >= 'a' && *p <= 'z')
2840 newexp = attr_rtx (EQ, value,
2841 attr_rtx (SYMBOL_REF,
2842 attr_string(string, strlen(string))));
2844 else if (GET_CODE (value) == COND)
2846 /* We construct an IOR of all the cases for which the requested attribute
2847 value is present. Since we start with FALSE, if it is not present,
2848 FALSE will be returned.
2850 Each case is the AND of the NOT's of the previous conditions with the
2851 current condition; in the default case the current condition is TRUE.
2853 For each possible COND value, call ourselves recursively.
2855 The extra TRUE and FALSE expressions will be eliminated by another
2856 call to the simplification routine. */
2861 if (current_alternative_string)
2862 clear_struct_flag (value);
2864 for (i = 0; i < XVECLEN (value, 0); i += 2)
2866 rtx this = SIMPLIFY_TEST_EXP (XVECEXP (value, 0, i),
2867 insn_code, insn_index);
2869 SIMPLIFY_ALTERNATIVE (this);
2871 right = insert_right_side (AND, andexp, this,
2872 insn_code, insn_index);
2873 right = insert_right_side (AND, right,
2874 evaluate_eq_attr (exp,
2877 insn_code, insn_index),
2878 insn_code, insn_index);
2879 orexp = insert_right_side (IOR, orexp, right,
2880 insn_code, insn_index);
2882 /* Add this condition into the AND expression. */
2883 newexp = attr_rtx (NOT, this);
2884 andexp = insert_right_side (AND, andexp, newexp,
2885 insn_code, insn_index);
2888 /* Handle the default case. */
2889 right = insert_right_side (AND, andexp,
2890 evaluate_eq_attr (exp, XEXP (value, 1),
2891 insn_code, insn_index),
2892 insn_code, insn_index);
2893 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2898 /* If uses an address, must return original expression. But set the
2899 RTX_UNCHANGING_P bit so we don't try to simplify it again. */
2902 walk_attr_value (newexp);
2906 /* This had `&& current_alternative_string', which seems to be wrong. */
2907 if (! RTX_UNCHANGING_P (exp))
2908 return copy_rtx_unchanging (exp);
2915 /* This routine is called when an AND of a term with a tree of AND's is
2916 encountered. If the term or its complement is present in the tree, it
2917 can be replaced with TRUE or FALSE, respectively.
2919 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2920 be true and hence are complementary.
2922 There is one special case: If we see
2923 (and (not (eq_attr "att" "v1"))
2924 (eq_attr "att" "v2"))
2925 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2926 replace the term, not anything in the AND tree. So we pass a pointer to
2930 simplify_and_tree (exp, pterm, insn_code, insn_index)
2933 int insn_code, insn_index;
2938 int left_eliminates_term, right_eliminates_term;
2940 if (GET_CODE (exp) == AND)
2942 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2943 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2944 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2946 newexp = attr_rtx (GET_CODE (exp), left, right);
2948 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2952 else if (GET_CODE (exp) == IOR)
2954 /* For the IOR case, we do the same as above, except that we can
2955 only eliminate `term' if both sides of the IOR would do so. */
2957 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2958 left_eliminates_term = (temp == true_rtx);
2961 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2962 right_eliminates_term = (temp == true_rtx);
2964 if (left_eliminates_term && right_eliminates_term)
2967 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2969 newexp = attr_rtx (GET_CODE (exp), left, right);
2971 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2975 /* Check for simplifications. Do some extra checking here since this
2976 routine is called so many times. */
2981 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2984 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2987 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2989 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2992 if (! strcmp (XSTR (exp, 1), XSTR (*pterm, 1)))
2998 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2999 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
3001 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
3004 if (! strcmp (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
3010 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
3011 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
3013 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
3016 if (! strcmp (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
3022 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
3024 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
3028 else if (GET_CODE (exp) == NOT)
3030 if (attr_equal_p (XEXP (exp, 0), *pterm))
3034 else if (GET_CODE (*pterm) == NOT)
3036 if (attr_equal_p (XEXP (*pterm, 0), exp))
3040 else if (attr_equal_p (exp, *pterm))
3046 /* Similar to `simplify_and_tree', but for IOR trees. */
3049 simplify_or_tree (exp, pterm, insn_code, insn_index)
3052 int insn_code, insn_index;
3057 int left_eliminates_term, right_eliminates_term;
3059 if (GET_CODE (exp) == IOR)
3061 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
3062 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
3063 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3065 newexp = attr_rtx (GET_CODE (exp), left, right);
3067 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3071 else if (GET_CODE (exp) == AND)
3073 /* For the AND case, we do the same as above, except that we can
3074 only eliminate `term' if both sides of the AND would do so. */
3076 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
3077 left_eliminates_term = (temp == false_rtx);
3080 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
3081 right_eliminates_term = (temp == false_rtx);
3083 if (left_eliminates_term && right_eliminates_term)
3086 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3088 newexp = attr_rtx (GET_CODE (exp), left, right);
3090 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3094 if (attr_equal_p (exp, *pterm))
3097 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
3100 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
3103 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
3104 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3105 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
3108 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
3109 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
3110 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
3116 /* Given an expression, see if it can be simplified for a particular insn
3117 code based on the values of other attributes being tested. This can
3118 eliminate nested get_attr_... calls.
3120 Note that if an endless recursion is specified in the patterns, the
3121 optimization will loop. However, it will do so in precisely the cases where
3122 an infinite recursion loop could occur during compilation. It's better that
3126 simplify_test_exp (exp, insn_code, insn_index)
3128 int insn_code, insn_index;
3131 struct attr_desc *attr;
3132 struct attr_value *av;
3133 struct insn_ent *ie;
3136 char *spacer = (char *) obstack_finish (rtl_obstack);
3138 /* Don't re-simplify something we already simplified. */
3139 if (RTX_UNCHANGING_P (exp) || MEM_IN_STRUCT_P (exp))
3142 switch (GET_CODE (exp))
3145 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3146 SIMPLIFY_ALTERNATIVE (left);
3147 if (left == false_rtx)
3149 obstack_free (rtl_obstack, spacer);
3152 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3153 SIMPLIFY_ALTERNATIVE (right);
3154 if (left == false_rtx)
3156 obstack_free (rtl_obstack, spacer);
3160 /* If either side is an IOR and we have (eq_attr "alternative" ..")
3161 present on both sides, apply the distributive law since this will
3162 yield simplifications. */
3163 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
3164 && compute_alternative_mask (left, IOR)
3165 && compute_alternative_mask (right, IOR))
3167 if (GET_CODE (left) == IOR)
3174 newexp = attr_rtx (IOR,
3175 attr_rtx (AND, left, XEXP (right, 0)),
3176 attr_rtx (AND, left, XEXP (right, 1)));
3178 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3181 /* Try with the term on both sides. */
3182 right = simplify_and_tree (right, &left, insn_code, insn_index);
3183 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3184 left = simplify_and_tree (left, &right, insn_code, insn_index);
3186 if (left == false_rtx || right == false_rtx)
3188 obstack_free (rtl_obstack, spacer);
3191 else if (left == true_rtx)
3195 else if (right == true_rtx)
3199 /* See if all or all but one of the insn's alternatives are specified
3200 in this tree. Optimize if so. */
3202 else if (insn_code >= 0
3203 && (GET_CODE (left) == AND
3204 || (GET_CODE (left) == NOT
3205 && GET_CODE (XEXP (left, 0)) == EQ_ATTR
3206 && XSTR (XEXP (left, 0), 0) == alternative_name)
3207 || GET_CODE (right) == AND
3208 || (GET_CODE (right) == NOT
3209 && GET_CODE (XEXP (right, 0)) == EQ_ATTR
3210 && XSTR (XEXP (right, 0), 0) == alternative_name)))
3212 i = compute_alternative_mask (exp, AND);
3213 if (i & ~insn_alternatives[insn_code])
3214 fatal ("Invalid alternative specified for pattern number %d",
3217 /* If all alternatives are excluded, this is false. */
3218 i ^= insn_alternatives[insn_code];
3221 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3223 /* If just one excluded, AND a comparison with that one to the
3224 front of the tree. The others will be eliminated by
3225 optimization. We do not want to do this if the insn has one
3226 alternative and we have tested none of them! */
3227 left = make_alternative_compare (i);
3228 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3229 newexp = attr_rtx (AND, left, right);
3231 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3235 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3237 newexp = attr_rtx (AND, left, right);
3238 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3243 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3244 SIMPLIFY_ALTERNATIVE (left);
3245 if (left == true_rtx)
3247 obstack_free (rtl_obstack, spacer);
3250 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3251 SIMPLIFY_ALTERNATIVE (right);
3252 if (right == true_rtx)
3254 obstack_free (rtl_obstack, spacer);
3258 right = simplify_or_tree (right, &left, insn_code, insn_index);
3259 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3260 left = simplify_or_tree (left, &right, insn_code, insn_index);
3262 if (right == true_rtx || left == true_rtx)
3264 obstack_free (rtl_obstack, spacer);
3267 else if (left == false_rtx)
3271 else if (right == false_rtx)
3276 /* Test for simple cases where the distributive law is useful. I.e.,
3277 convert (ior (and (x) (y))
3283 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
3284 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
3286 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
3288 left = XEXP (left, 0);
3290 newexp = attr_rtx (AND, left, right);
3291 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3294 /* See if all or all but one of the insn's alternatives are specified
3295 in this tree. Optimize if so. */
3297 else if (insn_code >= 0
3298 && (GET_CODE (left) == IOR
3299 || (GET_CODE (left) == EQ_ATTR
3300 && XSTR (left, 0) == alternative_name)
3301 || GET_CODE (right) == IOR
3302 || (GET_CODE (right) == EQ_ATTR
3303 && XSTR (right, 0) == alternative_name)))
3305 i = compute_alternative_mask (exp, IOR);
3306 if (i & ~insn_alternatives[insn_code])
3307 fatal ("Invalid alternative specified for pattern number %d",
3310 /* If all alternatives are included, this is true. */
3311 i ^= insn_alternatives[insn_code];
3314 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3316 /* If just one excluded, IOR a comparison with that one to the
3317 front of the tree. The others will be eliminated by
3318 optimization. We do not want to do this if the insn has one
3319 alternative and we have tested none of them! */
3320 left = make_alternative_compare (i);
3321 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3322 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
3324 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3328 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3330 newexp = attr_rtx (IOR, left, right);
3331 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3336 if (GET_CODE (XEXP (exp, 0)) == NOT)
3338 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
3339 insn_code, insn_index);
3340 SIMPLIFY_ALTERNATIVE (left);
3344 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3345 SIMPLIFY_ALTERNATIVE (left);
3346 if (GET_CODE (left) == NOT)
3347 return XEXP (left, 0);
3349 if (left == false_rtx)
3351 obstack_free (rtl_obstack, spacer);
3354 else if (left == true_rtx)
3356 obstack_free (rtl_obstack, spacer);
3360 /* Try to apply De`Morgan's laws. */
3361 else if (GET_CODE (left) == IOR)
3363 newexp = attr_rtx (AND,
3364 attr_rtx (NOT, XEXP (left, 0)),
3365 attr_rtx (NOT, XEXP (left, 1)));
3367 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3369 else if (GET_CODE (left) == AND)
3371 newexp = attr_rtx (IOR,
3372 attr_rtx (NOT, XEXP (left, 0)),
3373 attr_rtx (NOT, XEXP (left, 1)));
3375 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3377 else if (left != XEXP (exp, 0))
3379 newexp = attr_rtx (NOT, left);
3384 if (current_alternative_string && XSTR (exp, 0) == alternative_name)
3385 return (XSTR (exp, 1) == current_alternative_string
3386 ? true_rtx : false_rtx);
3388 /* Look at the value for this insn code in the specified attribute.
3389 We normally can replace this comparison with the condition that
3390 would give this insn the values being tested for. */
3391 if (XSTR (exp, 0) != alternative_name
3392 && (attr = find_attr (XSTR (exp, 0), 0)) != NULL)
3393 for (av = attr->first_value; av; av = av->next)
3394 for (ie = av->first_insn; ie; ie = ie->next)
3395 if (ie->insn_code == insn_code)
3396 return evaluate_eq_attr (exp, av->value, insn_code, insn_index);
3403 /* We have already simplified this expression. Simplifying it again
3404 won't buy anything unless we weren't given a valid insn code
3405 to process (i.e., we are canonicalizing something.). */
3406 if (insn_code != -2 /* Seems wrong: && current_alternative_string. */
3407 && ! RTX_UNCHANGING_P (newexp))
3408 return copy_rtx_unchanging (newexp);
3413 /* Optimize the attribute lists by seeing if we can determine conditional
3414 values from the known values of other attributes. This will save subroutine
3415 calls during the compilation. */
3420 struct attr_desc *attr;
3421 struct attr_value *av;
3422 struct insn_ent *ie;
3424 int something_changed = 1;
3426 struct attr_value_list { struct attr_value *av;
3427 struct insn_ent *ie;
3428 struct attr_desc * attr;
3429 struct attr_value_list *next; };
3430 struct attr_value_list **insn_code_values;
3431 struct attr_value_list *ivbuf;
3432 struct attr_value_list *iv;
3434 /* For each insn code, make a list of all the insn_ent's for it,
3435 for all values for all attributes. */
3437 if (num_insn_ents == 0)
3440 /* Make 2 extra elements, for "code" values -2 and -1. */
3442 = (struct attr_value_list **) alloca ((insn_code_number + 2)
3443 * sizeof (struct attr_value_list *));
3444 bzero ((char *) insn_code_values,
3445 (insn_code_number + 2) * sizeof (struct attr_value_list *));
3447 /* Offset the table address so we can index by -2 or -1. */
3448 insn_code_values += 2;
3450 /* Allocate the attr_value_list structures using xmalloc rather than
3451 alloca, because using alloca can overflow the maximum permitted
3452 stack limit on SPARC Lynx. */
3453 iv = ivbuf = ((struct attr_value_list *)
3454 xmalloc (num_insn_ents * sizeof (struct attr_value_list)));
3456 for (i = 0; i < MAX_ATTRS_INDEX; i++)
3457 for (attr = attrs[i]; attr; attr = attr->next)
3458 for (av = attr->first_value; av; av = av->next)
3459 for (ie = av->first_insn; ie; ie = ie->next)
3464 iv->next = insn_code_values[ie->insn_code];
3465 insn_code_values[ie->insn_code] = iv;
3469 /* Sanity check on num_insn_ents. */
3470 if (iv != ivbuf + num_insn_ents)
3473 /* Process one insn code at a time. */
3474 for (i = -2; i < insn_code_number; i++)
3476 /* Clear the MEM_IN_STRUCT_P flag everywhere relevant.
3477 We use it to mean "already simplified for this insn". */
3478 for (iv = insn_code_values[i]; iv; iv = iv->next)
3479 clear_struct_flag (iv->av->value);
3481 /* Loop until nothing changes for one iteration. */
3482 something_changed = 1;
3483 while (something_changed)
3485 something_changed = 0;
3486 for (iv = insn_code_values[i]; iv; iv = iv->next)
3488 struct obstack *old = rtl_obstack;
3489 char *spacer = (char *) obstack_finish (temp_obstack);
3494 if (GET_CODE (av->value) != COND)
3497 rtl_obstack = temp_obstack;
3498 #if 0 /* This was intended as a speed up, but it was slower. */
3499 if (insn_n_alternatives[ie->insn_code] > 6
3500 && count_sub_rtxs (av->value, 200) >= 200)
3501 newexp = simplify_by_alternatives (av->value, ie->insn_code,
3505 newexp = simplify_cond (av->value, ie->insn_code,
3509 if (newexp != av->value)
3511 newexp = attr_copy_rtx (newexp);
3512 remove_insn_ent (av, ie);
3513 av = get_attr_value (newexp, attr, ie->insn_code);
3515 insert_insn_ent (av, ie);
3516 something_changed = 1;
3518 obstack_free (temp_obstack, spacer);
3528 simplify_by_alternatives (exp, insn_code, insn_index)
3530 int insn_code, insn_index;
3533 int len = insn_n_alternatives[insn_code];
3534 rtx newexp = rtx_alloc (COND);
3538 XVEC (newexp, 0) = rtvec_alloc (len * 2);
3540 /* It will not matter what value we use as the default value
3541 of the new COND, since that default will never be used.
3542 Choose something of the right type. */
3543 for (ultimate = exp; GET_CODE (ultimate) == COND;)
3544 ultimate = XEXP (ultimate, 1);
3545 XEXP (newexp, 1) = ultimate;
3547 for (i = 0; i < insn_n_alternatives[insn_code]; i++)
3549 current_alternative_string = attr_numeral (i);
3550 XVECEXP (newexp, 0, i * 2) = make_alternative_compare (1 << i);
3551 XVECEXP (newexp, 0, i * 2 + 1)
3552 = simplify_cond (exp, insn_code, insn_index);
3555 current_alternative_string = 0;
3556 return simplify_cond (newexp, insn_code, insn_index);
3560 /* If EXP is a suitable expression, reorganize it by constructing an
3561 equivalent expression that is a COND with the tests being all combinations
3562 of attribute values and the values being simple constants. */
3565 simplify_by_exploding (exp)
3568 rtx list = 0, link, condexp, defval = NULL_RTX;
3569 struct dimension *space;
3570 rtx *condtest, *condval;
3571 int i, j, total, ndim = 0;
3572 int most_tests, num_marks, new_marks;
3574 /* Locate all the EQ_ATTR expressions. */
3575 if (! find_and_mark_used_attributes (exp, &list, &ndim) || ndim == 0)
3577 unmark_used_attributes (list, 0, 0);
3581 /* Create an attribute space from the list of used attributes. For each
3582 dimension in the attribute space, record the attribute, list of values
3583 used, and number of values used. Add members to the list of values to
3584 cover the domain of the attribute. This makes the expanded COND form
3585 order independent. */
3587 space = (struct dimension *) alloca (ndim * sizeof (struct dimension));
3590 for (ndim = 0; list; ndim++)
3592 /* Pull the first attribute value from the list and record that
3593 attribute as another dimension in the attribute space. */
3594 char *name = XSTR (XEXP (list, 0), 0);
3597 if ((space[ndim].attr = find_attr (name, 0)) == 0
3598 || space[ndim].attr->is_numeric)
3600 unmark_used_attributes (list, space, ndim);
3604 /* Add all remaining attribute values that refer to this attribute. */
3605 space[ndim].num_values = 0;
3606 space[ndim].values = 0;
3608 for (link = list; link; link = *prev)
3609 if (! strcmp (XSTR (XEXP (link, 0), 0), name))
3611 space[ndim].num_values++;
3612 *prev = XEXP (link, 1);
3613 XEXP (link, 1) = space[ndim].values;
3614 space[ndim].values = link;
3617 prev = &XEXP (link, 1);
3619 /* Add sufficient members to the list of values to make the list
3620 mutually exclusive and record the total size of the attribute
3622 total *= add_values_to_cover (&space[ndim]);
3625 /* Sort the attribute space so that the attributes go from non-constant
3626 to constant and from most values to least values. */
3627 for (i = 0; i < ndim; i++)
3628 for (j = ndim - 1; j > i; j--)
3629 if ((space[j-1].attr->is_const && !space[j].attr->is_const)
3630 || space[j-1].num_values < space[j].num_values)
3632 struct dimension tmp;
3634 space[j] = space[j-1];
3638 /* Establish the initial current value. */
3639 for (i = 0; i < ndim; i++)
3640 space[i].current_value = space[i].values;
3642 condtest = (rtx *) alloca (total * sizeof (rtx));
3643 condval = (rtx *) alloca (total * sizeof (rtx));
3645 /* Expand the tests and values by iterating over all values in the
3649 condtest[i] = test_for_current_value (space, ndim);
3650 condval[i] = simplify_with_current_value (exp, space, ndim);
3651 if (! increment_current_value (space, ndim))
3657 /* We are now finished with the original expression. */
3658 unmark_used_attributes (0, space, ndim);
3660 /* Find the most used constant value and make that the default. */
3662 for (i = num_marks = 0; i < total; i++)
3663 if (GET_CODE (condval[i]) == CONST_STRING
3664 && ! MEM_VOLATILE_P (condval[i]))
3666 /* Mark the unmarked constant value and count how many are marked. */
3667 MEM_VOLATILE_P (condval[i]) = 1;
3668 for (j = new_marks = 0; j < total; j++)
3669 if (GET_CODE (condval[j]) == CONST_STRING
3670 && MEM_VOLATILE_P (condval[j]))
3672 if (new_marks - num_marks > most_tests)
3674 most_tests = new_marks - num_marks;
3675 defval = condval[i];
3677 num_marks = new_marks;
3679 /* Clear all the marks. */
3680 for (i = 0; i < total; i++)
3681 MEM_VOLATILE_P (condval[i]) = 0;
3683 /* Give up if nothing is constant. */
3687 /* If all values are the default, use that. */
3688 if (total == most_tests)
3691 /* Make a COND with the most common constant value the default. (A more
3692 complex method where tests with the same value were combined didn't
3693 seem to improve things.) */
3694 condexp = rtx_alloc (COND);
3695 XVEC (condexp, 0) = rtvec_alloc ((total - most_tests) * 2);
3696 XEXP (condexp, 1) = defval;
3697 for (i = j = 0; i < total; i++)
3698 if (condval[i] != defval)
3700 XVECEXP (condexp, 0, 2 * j) = condtest[i];
3701 XVECEXP (condexp, 0, 2 * j + 1) = condval[i];
3708 /* Set the MEM_VOLATILE_P flag for all EQ_ATTR expressions in EXP and
3709 verify that EXP can be simplified to a constant term if all the EQ_ATTR
3710 tests have known value. */
3713 find_and_mark_used_attributes (exp, terms, nterms)
3719 switch (GET_CODE (exp))
3722 if (! MEM_VOLATILE_P (exp))
3724 rtx link = rtx_alloc (EXPR_LIST);
3725 XEXP (link, 0) = exp;
3726 XEXP (link, 1) = *terms;
3729 MEM_VOLATILE_P (exp) = 1;
3738 if (! find_and_mark_used_attributes (XEXP (exp, 2), terms, nterms))
3742 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3745 if (! find_and_mark_used_attributes (XEXP (exp, 0), terms, nterms))
3750 for (i = 0; i < XVECLEN (exp, 0); i++)
3751 if (! find_and_mark_used_attributes (XVECEXP (exp, 0, i), terms, nterms))
3753 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3762 /* Clear the MEM_VOLATILE_P flag in all EQ_ATTR expressions on LIST and
3763 in the values of the NDIM-dimensional attribute space SPACE. */
3766 unmark_used_attributes (list, space, ndim)
3768 struct dimension *space;
3774 for (i = 0; i < ndim; i++)
3775 unmark_used_attributes (space[i].values, 0, 0);
3777 for (link = list; link; link = XEXP (link, 1))
3779 exp = XEXP (link, 0);
3780 if (GET_CODE (exp) == EQ_ATTR)
3781 MEM_VOLATILE_P (exp) = 0;
3785 /* Update the attribute dimension DIM so that all values of the attribute
3786 are tested. Return the updated number of values. */
3789 add_values_to_cover (dim)
3790 struct dimension *dim;
3792 struct attr_value *av;
3793 rtx exp, link, *prev;
3796 for (av = dim->attr->first_value; av; av = av->next)
3797 if (GET_CODE (av->value) == CONST_STRING)
3800 if (nalt < dim->num_values)
3802 else if (nalt == dim->num_values)
3804 else if (nalt * 2 < dim->num_values * 3)
3806 /* Most all the values of the attribute are used, so add all the unused
3808 prev = &dim->values;
3809 for (link = dim->values; link; link = *prev)
3810 prev = &XEXP (link, 1);
3812 for (av = dim->attr->first_value; av; av = av->next)
3813 if (GET_CODE (av->value) == CONST_STRING)
3815 exp = attr_eq (dim->attr->name, XSTR (av->value, 0));
3816 if (MEM_VOLATILE_P (exp))
3819 link = rtx_alloc (EXPR_LIST);
3820 XEXP (link, 0) = exp;
3823 prev = &XEXP (link, 1);
3825 dim->num_values = nalt;
3829 rtx orexp = false_rtx;
3831 /* Very few values are used, so compute a mutually exclusive
3832 expression. (We could do this for numeric values if that becomes
3834 prev = &dim->values;
3835 for (link = dim->values; link; link = *prev)
3837 orexp = insert_right_side (IOR, orexp, XEXP (link, 0), -2, -2);
3838 prev = &XEXP (link, 1);
3840 link = rtx_alloc (EXPR_LIST);
3841 XEXP (link, 0) = attr_rtx (NOT, orexp);
3846 return dim->num_values;
3849 /* Increment the current value for the NDIM-dimensional attribute space SPACE
3850 and return FALSE if the increment overflowed. */
3853 increment_current_value (space, ndim)
3854 struct dimension *space;
3859 for (i = ndim - 1; i >= 0; i--)
3861 if ((space[i].current_value = XEXP (space[i].current_value, 1)) == 0)
3862 space[i].current_value = space[i].values;
3869 /* Construct an expression corresponding to the current value for the
3870 NDIM-dimensional attribute space SPACE. */
3873 test_for_current_value (space, ndim)
3874 struct dimension *space;
3880 for (i = 0; i < ndim; i++)
3881 exp = insert_right_side (AND, exp, XEXP (space[i].current_value, 0),
3887 /* Given the current value of the NDIM-dimensional attribute space SPACE,
3888 set the corresponding EQ_ATTR expressions to that value and reduce
3889 the expression EXP as much as possible. On input [and output], all
3890 known EQ_ATTR expressions are set to FALSE. */
3893 simplify_with_current_value (exp, space, ndim)
3895 struct dimension *space;
3901 /* Mark each current value as TRUE. */
3902 for (i = 0; i < ndim; i++)
3904 x = XEXP (space[i].current_value, 0);
3905 if (GET_CODE (x) == EQ_ATTR)
3906 MEM_VOLATILE_P (x) = 0;
3909 exp = simplify_with_current_value_aux (exp);
3911 /* Change each current value back to FALSE. */
3912 for (i = 0; i < ndim; i++)
3914 x = XEXP (space[i].current_value, 0);
3915 if (GET_CODE (x) == EQ_ATTR)
3916 MEM_VOLATILE_P (x) = 1;
3922 /* Reduce the expression EXP based on the MEM_VOLATILE_P settings of
3923 all EQ_ATTR expressions. */
3926 simplify_with_current_value_aux (exp)
3932 switch (GET_CODE (exp))
3935 if (MEM_VOLATILE_P (exp))
3944 cond = simplify_with_current_value_aux (XEXP (exp, 0));
3945 if (cond == true_rtx)
3946 return simplify_with_current_value_aux (XEXP (exp, 1));
3947 else if (cond == false_rtx)
3948 return simplify_with_current_value_aux (XEXP (exp, 2));
3950 return attr_rtx (IF_THEN_ELSE, cond,
3951 simplify_with_current_value_aux (XEXP (exp, 1)),
3952 simplify_with_current_value_aux (XEXP (exp, 2)));
3955 cond = simplify_with_current_value_aux (XEXP (exp, 1));
3956 if (cond == true_rtx)
3958 else if (cond == false_rtx)
3959 return simplify_with_current_value_aux (XEXP (exp, 0));
3961 return attr_rtx (IOR, cond,
3962 simplify_with_current_value_aux (XEXP (exp, 0)));
3965 cond = simplify_with_current_value_aux (XEXP (exp, 1));
3966 if (cond == true_rtx)
3967 return simplify_with_current_value_aux (XEXP (exp, 0));
3968 else if (cond == false_rtx)
3971 return attr_rtx (AND, cond,
3972 simplify_with_current_value_aux (XEXP (exp, 0)));
3975 cond = simplify_with_current_value_aux (XEXP (exp, 0));
3976 if (cond == true_rtx)
3978 else if (cond == false_rtx)
3981 return attr_rtx (NOT, cond);
3984 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3986 cond = simplify_with_current_value_aux (XVECEXP (exp, 0, i));
3987 if (cond == true_rtx)
3988 return simplify_with_current_value_aux (XVECEXP (exp, 0, i + 1));
3989 else if (cond == false_rtx)
3992 abort (); /* With all EQ_ATTR's of known value, a case should
3993 have been selected. */
3995 return simplify_with_current_value_aux (XEXP (exp, 1));
4002 /* Clear the MEM_IN_STRUCT_P flag in EXP and its subexpressions. */
4005 clear_struct_flag (x)
4010 register enum rtx_code code;
4011 register const char *fmt;
4013 MEM_IN_STRUCT_P (x) = 0;
4014 if (RTX_UNCHANGING_P (x))
4017 code = GET_CODE (x);
4037 /* Compare the elements. If any pair of corresponding elements
4038 fail to match, return 0 for the whole things. */
4040 fmt = GET_RTX_FORMAT (code);
4041 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4047 for (j = 0; j < XVECLEN (x, i); j++)
4048 clear_struct_flag (XVECEXP (x, i, j));
4052 clear_struct_flag (XEXP (x, i));
4058 /* Return the number of RTX objects making up the expression X.
4059 But if we count more than MAX objects, stop counting. */
4062 count_sub_rtxs (x, max)
4068 register enum rtx_code code;
4069 register const char *fmt;
4072 code = GET_CODE (x);
4092 /* Compare the elements. If any pair of corresponding elements
4093 fail to match, return 0 for the whole things. */
4095 fmt = GET_RTX_FORMAT (code);
4096 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4105 for (j = 0; j < XVECLEN (x, i); j++)
4106 total += count_sub_rtxs (XVECEXP (x, i, j), max);
4110 total += count_sub_rtxs (XEXP (x, i), max);
4118 /* Create table entries for DEFINE_ATTR. */
4124 struct attr_desc *attr;
4125 struct attr_value *av;
4129 /* Make a new attribute structure. Check for duplicate by looking at
4130 attr->default_val, since it is initialized by this routine. */
4131 attr = find_attr (XSTR (exp, 0), 1);
4132 if (attr->default_val)
4133 fatal ("Duplicate definition for `%s' attribute", attr->name);
4135 if (*XSTR (exp, 1) == '\0')
4136 attr->is_numeric = 1;
4139 name_ptr = XSTR (exp, 1);
4140 while ((p = next_comma_elt (&name_ptr)) != NULL)
4142 av = (struct attr_value *) oballoc (sizeof (struct attr_value));
4143 av->value = attr_rtx (CONST_STRING, p);
4144 av->next = attr->first_value;
4145 attr->first_value = av;
4146 av->first_insn = NULL;
4148 av->has_asm_insn = 0;
4152 if (GET_CODE (XEXP (exp, 2)) == CONST)
4155 if (attr->is_numeric)
4156 fatal ("Constant attributes may not take numeric values");
4157 /* Get rid of the CONST node. It is allowed only at top-level. */
4158 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
4161 if (! strcmp (attr->name, "length") && ! attr->is_numeric)
4162 fatal ("`length' attribute must take numeric values");
4164 /* Set up the default value. */
4165 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
4166 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
4169 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
4170 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
4171 number of alternatives as this should be checked elsewhere. */
4174 count_alternatives (exp)
4180 if (GET_CODE (exp) == MATCH_OPERAND)
4181 return n_comma_elts (XSTR (exp, 2));
4183 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4184 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4189 n = count_alternatives (XEXP (exp, i));
4196 if (XVEC (exp, i) != NULL)
4197 for (j = 0; j < XVECLEN (exp, i); j++)
4199 n = count_alternatives (XVECEXP (exp, i, j));
4208 /* Returns non-zero if the given expression contains an EQ_ATTR with the
4209 `alternative' attribute. */
4212 compares_alternatives_p (exp)
4218 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
4221 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4222 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4227 if (compares_alternatives_p (XEXP (exp, i)))
4232 for (j = 0; j < XVECLEN (exp, i); j++)
4233 if (compares_alternatives_p (XVECEXP (exp, i, j)))
4241 /* Returns non-zero is INNER is contained in EXP. */
4244 contained_in_p (inner, exp)
4251 if (rtx_equal_p (inner, exp))
4254 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4255 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4260 if (contained_in_p (inner, XEXP (exp, i)))
4265 for (j = 0; j < XVECLEN (exp, i); j++)
4266 if (contained_in_p (inner, XVECEXP (exp, i, j)))
4274 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
4280 struct insn_def *id;
4282 id = (struct insn_def *) oballoc (sizeof (struct insn_def));
4287 switch (GET_CODE (exp))
4290 id->insn_code = insn_code_number++;
4291 id->insn_index = insn_index_number++;
4292 id->num_alternatives = count_alternatives (exp);
4293 if (id->num_alternatives == 0)
4294 id->num_alternatives = 1;
4298 case DEFINE_PEEPHOLE:
4299 id->insn_code = insn_code_number++;
4300 id->insn_index = insn_index_number++;
4301 id->num_alternatives = count_alternatives (exp);
4302 if (id->num_alternatives == 0)
4303 id->num_alternatives = 1;
4307 case DEFINE_ASM_ATTRIBUTES:
4309 id->insn_index = -1;
4310 id->num_alternatives = 1;
4312 got_define_asm_attributes = 1;
4320 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
4321 true or annul false is specified, and make a `struct delay_desc'. */
4327 struct delay_desc *delay;
4330 if (XVECLEN (def, 1) % 3 != 0)
4331 fatal ("Number of elements in DEFINE_DELAY must be multiple of three.");
4333 for (i = 0; i < XVECLEN (def, 1); i += 3)
4335 if (XVECEXP (def, 1, i + 1))
4336 have_annul_true = 1;
4337 if (XVECEXP (def, 1, i + 2))
4338 have_annul_false = 1;
4341 delay = (struct delay_desc *) oballoc (sizeof (struct delay_desc));
4343 delay->num = ++num_delays;
4344 delay->next = delays;
4348 /* Process a DEFINE_FUNCTION_UNIT.
4350 This gives information about a function unit contained in the CPU.
4351 We fill in a `struct function_unit_op' and a `struct function_unit'
4352 with information used later by `expand_unit'. */
4358 struct function_unit *unit;
4359 struct function_unit_op *op;
4360 char *name = XSTR (def, 0);
4361 int multiplicity = XINT (def, 1);
4362 int simultaneity = XINT (def, 2);
4363 rtx condexp = XEXP (def, 3);
4364 int ready_cost = MAX (XINT (def, 4), 1);
4365 int issue_delay = MAX (XINT (def, 5), 1);
4367 /* See if we have already seen this function unit. If so, check that
4368 the multiplicity and simultaneity values are the same. If not, make
4369 a structure for this function unit. */
4370 for (unit = units; unit; unit = unit->next)
4371 if (! strcmp (unit->name, name))
4373 if (unit->multiplicity != multiplicity
4374 || unit->simultaneity != simultaneity)
4375 fatal ("Differing specifications given for `%s' function unit.",
4382 unit = (struct function_unit *) oballoc (sizeof (struct function_unit));
4384 unit->multiplicity = multiplicity;
4385 unit->simultaneity = simultaneity;
4386 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
4387 unit->num = num_units++;
4388 unit->num_opclasses = 0;
4389 unit->condexp = false_rtx;
4395 /* Make a new operation class structure entry and initialize it. */
4396 op = (struct function_unit_op *) oballoc (sizeof (struct function_unit_op));
4397 op->condexp = condexp;
4398 op->num = unit->num_opclasses++;
4399 op->ready = ready_cost;
4400 op->issue_delay = issue_delay;
4401 op->next = unit->ops;
4403 num_unit_opclasses++;
4405 /* Set our issue expression based on whether or not an optional conflict
4406 vector was specified. */
4409 /* Compute the IOR of all the specified expressions. */
4410 rtx orexp = false_rtx;
4413 for (i = 0; i < XVECLEN (def, 6); i++)
4414 orexp = insert_right_side (IOR, orexp, XVECEXP (def, 6, i), -2, -2);
4416 op->conflict_exp = orexp;
4417 extend_range (&unit->issue_delay, 1, issue_delay);
4421 op->conflict_exp = true_rtx;
4422 extend_range (&unit->issue_delay, issue_delay, issue_delay);
4425 /* Merge our conditional into that of the function unit so we can determine
4426 which insns are used by the function unit. */
4427 unit->condexp = insert_right_side (IOR, unit->condexp, op->condexp, -2, -2);
4430 /* Given a piece of RTX, print a C expression to test its truth value.
4431 We use AND and IOR both for logical and bit-wise operations, so
4432 interpret them as logical unless they are inside a comparison expression.
4433 The first bit of FLAGS will be non-zero in that case.
4435 Set the second bit of FLAGS to make references to attribute values use
4436 a cached local variable instead of calling a function. */
4439 write_test_expr (exp, flags)
4443 int comparison_operator = 0;
4445 struct attr_desc *attr;
4447 /* In order not to worry about operator precedence, surround our part of
4448 the expression with parentheses. */
4451 code = GET_CODE (exp);
4454 /* Binary operators. */
4456 case GE: case GT: case GEU: case GTU:
4457 case LE: case LT: case LEU: case LTU:
4458 comparison_operator = 1;
4460 case PLUS: case MINUS: case MULT: case DIV: case MOD:
4461 case AND: case IOR: case XOR:
4462 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4463 write_test_expr (XEXP (exp, 0), flags | comparison_operator);
4479 printf (" >= (unsigned) ");
4482 printf (" > (unsigned) ");
4491 printf (" <= (unsigned) ");
4494 printf (" < (unsigned) ");
4537 write_test_expr (XEXP (exp, 1), flags | comparison_operator);
4541 /* Special-case (not (eq_attrq "alternative" "x")) */
4542 if (! (flags & 1) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
4543 && XSTR (XEXP (exp, 0), 0) == alternative_name)
4545 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
4549 /* Otherwise, fall through to normal unary operator. */
4551 /* Unary operators. */
4571 write_test_expr (XEXP (exp, 0), flags);
4574 /* Comparison test of an attribute with a value. Most of these will
4575 have been removed by optimization. Handle "alternative"
4576 specially and give error if EQ_ATTR present inside a comparison. */
4579 fatal ("EQ_ATTR not valid inside comparison");
4581 if (XSTR (exp, 0) == alternative_name)
4583 printf ("which_alternative == %s", XSTR (exp, 1));
4587 attr = find_attr (XSTR (exp, 0), 0);
4588 if (! attr) abort ();
4590 /* Now is the time to expand the value of a constant attribute. */
4593 write_test_expr (evaluate_eq_attr (exp, attr->default_val->value,
4600 printf ("attr_%s", attr->name);
4602 printf ("get_attr_%s (insn)", attr->name);
4604 write_attr_valueq (attr, XSTR (exp, 1));
4608 /* Comparison test of flags for define_delays. */
4611 fatal ("ATTR_FLAG not valid inside comparison");
4612 printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
4615 /* See if an operand matches a predicate. */
4617 /* If only a mode is given, just ensure the mode matches the operand.
4618 If neither a mode nor predicate is given, error. */
4619 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
4621 if (GET_MODE (exp) == VOIDmode)
4622 fatal ("Null MATCH_OPERAND specified as test");
4624 printf ("GET_MODE (operands[%d]) == %smode",
4625 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4628 printf ("%s (operands[%d], %smode)",
4629 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4633 printf ("%s (insn)", XSTR (exp, 0));
4636 /* Constant integer. */
4638 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
4641 /* A random C expression. */
4643 printf ("%s", XSTR (exp, 0));
4646 /* The address of the branch target. */
4648 printf ("insn_addresses[INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])]",
4649 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
4653 /* The address of the current insn. We implement this actually as the
4654 address of the current insn for backward branches, but the last
4655 address of the next insn for forward branches, and both with
4656 adjustments that account for the worst-case possible stretching of
4657 intervening alignments between this insn and its destination. */
4658 printf("insn_current_reference_address (insn)");
4662 printf ("%s", XSTR (exp, 0));
4666 write_test_expr (XEXP (exp, 0), flags & 2);
4668 write_test_expr (XEXP (exp, 1), flags | 1);
4670 write_test_expr (XEXP (exp, 2), flags | 1);
4674 fatal ("bad RTX code `%s' in attribute calculation\n",
4675 GET_RTX_NAME (code));
4681 /* Given an attribute value, return the maximum CONST_STRING argument
4682 encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
4685 max_attr_value (exp, unknownp)
4692 switch (GET_CODE (exp))
4695 current_max = atoi (XSTR (exp, 0));
4699 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4700 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4702 n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4703 if (n > current_max)
4709 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4710 n = max_attr_value (XEXP (exp, 2), unknownp);
4711 if (n > current_max)
4717 current_max = INT_MAX;
4724 /* Given an attribute value, return the result of ORing together all
4725 CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
4726 if the numeric value is not known. */
4729 or_attr_value (exp, unknownp)
4736 switch (GET_CODE (exp))
4739 current_or = atoi (XSTR (exp, 0));
4743 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4744 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4745 current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4749 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4750 current_or |= or_attr_value (XEXP (exp, 2), unknownp);
4762 /* Scan an attribute value, possibly a conditional, and record what actions
4763 will be required to do any conditional tests in it.
4766 `must_extract' if we need to extract the insn operands
4767 `must_constrain' if we must compute `which_alternative'
4768 `address_used' if an address expression was used
4769 `length_used' if an (eq_attr "length" ...) was used
4773 walk_attr_value (exp)
4777 register const char *fmt;
4783 code = GET_CODE (exp);
4787 if (! RTX_UNCHANGING_P (exp))
4788 /* Since this is an arbitrary expression, it can look at anything.
4789 However, constant expressions do not depend on any particular
4791 must_extract = must_constrain = 1;
4799 if (XSTR (exp, 0) == alternative_name)
4800 must_extract = must_constrain = 1;
4801 else if (strcmp (XSTR (exp, 0), "length") == 0)
4821 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
4826 walk_attr_value (XEXP (exp, i));
4830 if (XVEC (exp, i) != NULL)
4831 for (j = 0; j < XVECLEN (exp, i); j++)
4832 walk_attr_value (XVECEXP (exp, i, j));
4837 /* Write out a function to obtain the attribute for a given INSN. */
4840 write_attr_get (attr)
4841 struct attr_desc *attr;
4843 struct attr_value *av, *common_av;
4845 /* Find the most used attribute value. Handle that as the `default' of the
4846 switch we will generate. */
4847 common_av = find_most_used (attr);
4849 /* Write out prototype of function. */
4850 if (!attr->is_numeric)
4851 printf ("extern enum attr_%s ", attr->name);
4852 else if (attr->unsigned_p)
4853 printf ("extern unsigned int ");
4855 printf ("extern int ");
4856 /* If the attribute name starts with a star, the remainder is the name of
4857 the subroutine to use, instead of `get_attr_...'. */
4858 if (attr->name[0] == '*')
4859 printf ("%s PROTO ((rtx));\n", &attr->name[1]);
4861 printf ("get_attr_%s PROTO ((%s));\n", attr->name,
4862 (attr->is_const ? "void" : "rtx"));
4864 /* Write out start of function, then all values with explicit `case' lines,
4865 then a `default', then the value with the most uses. */
4866 if (!attr->is_numeric)
4867 printf ("enum attr_%s\n", attr->name);
4868 else if (attr->unsigned_p)
4869 printf ("unsigned int\n");
4873 /* If the attribute name starts with a star, the remainder is the name of
4874 the subroutine to use, instead of `get_attr_...'. */
4875 if (attr->name[0] == '*')
4876 printf ("%s (insn)\n", &attr->name[1]);
4877 else if (attr->is_const == 0)
4878 printf ("get_attr_%s (insn)\n", attr->name);
4881 printf ("get_attr_%s ()\n", attr->name);
4884 for (av = attr->first_value; av; av = av->next)
4885 if (av->num_insns != 0)
4886 write_attr_set (attr, 2, av->value, "return", ";",
4887 true_rtx, av->first_insn->insn_code,
4888 av->first_insn->insn_index);
4894 printf (" rtx insn;\n");
4897 if (GET_CODE (common_av->value) == FFS)
4899 rtx p = XEXP (common_av->value, 0);
4901 /* No need to emit code to abort if the insn is unrecognized; the
4902 other get_attr_foo functions will do that when we call them. */
4904 write_toplevel_expr (p);
4906 printf ("\n if (accum && accum == (accum & -accum))\n");
4908 printf (" int i;\n");
4909 printf (" for (i = 0; accum >>= 1; ++i) continue;\n");
4910 printf (" accum = i;\n");
4911 printf (" }\n else\n");
4912 printf (" accum = ~accum;\n");
4913 printf (" return accum;\n}\n\n");
4917 printf (" switch (recog_memoized (insn))\n");
4920 for (av = attr->first_value; av; av = av->next)
4921 if (av != common_av)
4922 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
4924 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
4925 printf (" }\n}\n\n");
4929 /* Given an AND tree of known true terms (because we are inside an `if' with
4930 that as the condition or are in an `else' clause) and an expression,
4931 replace any known true terms with TRUE. Use `simplify_and_tree' to do
4932 the bulk of the work. */
4935 eliminate_known_true (known_true, exp, insn_code, insn_index)
4938 int insn_code, insn_index;
4942 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
4944 if (GET_CODE (known_true) == AND)
4946 exp = eliminate_known_true (XEXP (known_true, 0), exp,
4947 insn_code, insn_index);
4948 exp = eliminate_known_true (XEXP (known_true, 1), exp,
4949 insn_code, insn_index);
4954 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
4960 /* Write out a series of tests and assignment statements to perform tests and
4961 sets of an attribute value. We are passed an indentation amount and prefix
4962 and suffix strings to write around each attribute value (e.g., "return"
4966 write_attr_set (attr, indent, value, prefix, suffix, known_true,
4967 insn_code, insn_index)
4968 struct attr_desc *attr;
4974 int insn_code, insn_index;
4976 if (GET_CODE (value) == COND)
4978 /* Assume the default value will be the default of the COND unless we
4979 find an always true expression. */
4980 rtx default_val = XEXP (value, 1);
4981 rtx our_known_true = known_true;
4986 for (i = 0; i < XVECLEN (value, 0); i += 2)
4991 testexp = eliminate_known_true (our_known_true,
4992 XVECEXP (value, 0, i),
4993 insn_code, insn_index);
4994 newexp = attr_rtx (NOT, testexp);
4995 newexp = insert_right_side (AND, our_known_true, newexp,
4996 insn_code, insn_index);
4998 /* If the test expression is always true or if the next `known_true'
4999 expression is always false, this is the last case, so break
5000 out and let this value be the `else' case. */
5001 if (testexp == true_rtx || newexp == false_rtx)
5003 default_val = XVECEXP (value, 0, i + 1);
5007 /* Compute the expression to pass to our recursive call as being
5009 inner_true = insert_right_side (AND, our_known_true,
5010 testexp, insn_code, insn_index);
5012 /* If this is always false, skip it. */
5013 if (inner_true == false_rtx)
5016 write_indent (indent);
5017 printf ("%sif ", first_if ? "" : "else ");
5019 write_test_expr (testexp, 0);
5021 write_indent (indent + 2);
5024 write_attr_set (attr, indent + 4,
5025 XVECEXP (value, 0, i + 1), prefix, suffix,
5026 inner_true, insn_code, insn_index);
5027 write_indent (indent + 2);
5029 our_known_true = newexp;
5034 write_indent (indent);
5036 write_indent (indent + 2);
5040 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
5041 prefix, suffix, our_known_true, insn_code, insn_index);
5045 write_indent (indent + 2);
5051 write_indent (indent);
5052 printf ("%s ", prefix);
5053 write_attr_value (attr, value);
5054 printf ("%s\n", suffix);
5058 /* Write out the computation for one attribute value. */
5061 write_attr_case (attr, av, write_case_lines, prefix, suffix, indent,
5063 struct attr_desc *attr;
5064 struct attr_value *av;
5065 int write_case_lines;
5066 const char *prefix, *suffix;
5070 struct insn_ent *ie;
5072 if (av->num_insns == 0)
5075 if (av->has_asm_insn)
5077 write_indent (indent);
5078 printf ("case -1:\n");
5079 write_indent (indent + 2);
5080 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
5081 write_indent (indent + 2);
5082 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
5083 write_indent (indent + 2);
5084 printf (" fatal_insn_not_found (insn);\n");
5087 if (write_case_lines)
5089 for (ie = av->first_insn; ie; ie = ie->next)
5090 if (ie->insn_code != -1)
5092 write_indent (indent);
5093 printf ("case %d:\n", ie->insn_code);
5098 write_indent (indent);
5099 printf ("default:\n");
5102 /* See what we have to do to output this value. */
5103 must_extract = must_constrain = address_used = 0;
5104 walk_attr_value (av->value);
5108 write_indent (indent + 2);
5109 printf ("extract_insn (insn);\n");
5114 #ifdef REGISTER_CONSTRAINTS
5115 write_indent (indent + 2);
5116 printf ("if (! constrain_operands (reload_completed))\n");
5117 write_indent (indent + 2);
5118 printf (" fatal_insn_not_found (insn);\n");
5122 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
5123 known_true, av->first_insn->insn_code,
5124 av->first_insn->insn_index);
5126 if (strncmp (prefix, "return", 6))
5128 write_indent (indent + 2);
5129 printf ("break;\n");
5134 /* Search for uses of non-const attributes and write code to cache them. */
5137 write_expr_attr_cache (p, attr)
5139 struct attr_desc *attr;
5144 if (GET_CODE (p) == EQ_ATTR)
5146 if (XSTR (p, 0) != attr->name)
5149 if (!attr->is_numeric)
5150 printf (" register enum attr_%s ", attr->name);
5151 else if (attr->unsigned_p)
5152 printf (" register unsigned int ");
5154 printf (" register int ");
5156 printf ("attr_%s = get_attr_%s (insn);\n", attr->name, attr->name);
5160 fmt = GET_RTX_FORMAT (GET_CODE (p));
5161 ie = GET_RTX_LENGTH (GET_CODE (p));
5162 for (i = 0; i < ie; i++)
5167 if (write_expr_attr_cache (XEXP (p, i), attr))
5172 je = XVECLEN (p, i);
5173 for (j = 0; j < je; ++j)
5174 if (write_expr_attr_cache (XVECEXP (p, i, j), attr))
5183 /* Evaluate an expression at top level. A front end to write_test_expr,
5184 in which we cache attribute values and break up excessively large
5185 expressions to cater to older compilers. */
5188 write_toplevel_expr (p)
5191 struct attr_desc *attr;
5194 for (i = 0; i < MAX_ATTRS_INDEX; ++i)
5195 for (attr = attrs[i]; attr ; attr = attr->next)
5196 if (!attr->is_const)
5197 write_expr_attr_cache (p, attr);
5199 printf(" register unsigned long accum = 0;\n\n");
5201 while (GET_CODE (p) == IOR)
5204 if (GET_CODE (XEXP (p, 0)) == IOR)
5205 e = XEXP (p, 1), p = XEXP (p, 0);
5207 e = XEXP (p, 0), p = XEXP (p, 1);
5209 printf (" accum |= ");
5210 write_test_expr (e, 3);
5213 printf (" accum |= ");
5214 write_test_expr (p, 3);
5218 /* Utilities to write names in various forms. */
5221 write_unit_name (prefix, num, suffix)
5226 struct function_unit *unit;
5228 for (unit = units; unit; unit = unit->next)
5229 if (unit->num == num)
5231 printf ("%s%s%s", prefix, unit->name, suffix);
5235 printf ("%s<unknown>%s", prefix, suffix);
5239 write_attr_valueq (attr, s)
5240 struct attr_desc *attr;
5243 if (attr->is_numeric)
5249 /* Make the blockage range values and function units used values easier
5251 if (attr->func_units_p)
5254 printf (" /* units: none */");
5256 write_unit_name (" /* units: ", num, " */");
5260 const char *sep = " /* units: ";
5261 for (i = 0, num = ~num; num; i++, num >>= 1)
5264 write_unit_name (sep, i, (num == 1) ? " */" : "");
5270 else if (attr->blockage_p)
5271 printf (" /* min %d, max %d */", num >> (HOST_BITS_PER_INT / 2),
5272 num & ((1 << (HOST_BITS_PER_INT / 2)) - 1));
5274 else if (num > 9 || num < 0)
5275 printf (" /* 0x%x */", num);
5279 write_upcase (attr->name);
5286 write_attr_value (attr, value)
5287 struct attr_desc *attr;
5292 switch (GET_CODE (value))
5295 write_attr_valueq (attr, XSTR (value, 0));
5299 fputs (XSTR (value, 0), stdout);
5304 struct attr_desc *attr2 = find_attr (XSTR (value, 0), 0);
5305 printf ("get_attr_%s (%s)", attr2->name,
5306 (attr2->is_const ? "" : "insn"));
5327 write_attr_value (attr, XEXP (value, 0));
5331 write_attr_value (attr, XEXP (value, 1));
5344 if (*str < 'a' || *str > 'z')
5345 printf ("%c", *str++);
5347 printf ("%c", *str++ - 'a' + 'A');
5351 write_indent (indent)
5354 for (; indent > 8; indent -= 8)
5357 for (; indent; indent--)
5361 /* Write a subroutine that is given an insn that requires a delay slot, a
5362 delay slot ordinal, and a candidate insn. It returns non-zero if the
5363 candidate can be placed in the specified delay slot of the insn.
5365 We can write as many as three subroutines. `eligible_for_delay'
5366 handles normal delay slots, `eligible_for_annul_true' indicates that
5367 the specified insn can be annulled if the branch is true, and likewise
5368 for `eligible_for_annul_false'.
5370 KIND is a string distinguishing these three cases ("delay", "annul_true",
5371 or "annul_false"). */
5374 write_eligible_delay (kind)
5377 struct delay_desc *delay;
5380 struct attr_desc *attr;
5381 struct attr_value *av, *common_av;
5384 /* Compute the maximum number of delay slots required. We use the delay
5385 ordinal times this number plus one, plus the slot number as an index into
5386 the appropriate predicate to test. */
5388 for (delay = delays, max_slots = 0; delay; delay = delay->next)
5389 if (XVECLEN (delay->def, 1) / 3 > max_slots)
5390 max_slots = XVECLEN (delay->def, 1) / 3;
5392 /* Write function prelude. */
5395 printf ("eligible_for_%s (delay_insn, slot, candidate_insn, flags)\n",
5397 printf (" rtx delay_insn;\n");
5398 printf (" int slot;\n");
5399 printf (" rtx candidate_insn;\n");
5400 printf (" int flags ATTRIBUTE_UNUSED;\n");
5402 printf (" rtx insn;\n");
5404 printf (" if (slot >= %d)\n", max_slots);
5405 printf (" abort ();\n");
5408 /* If more than one delay type, find out which type the delay insn is. */
5412 attr = find_attr ("*delay_type", 0);
5413 if (! attr) abort ();
5414 common_av = find_most_used (attr);
5416 printf (" insn = delay_insn;\n");
5417 printf (" switch (recog_memoized (insn))\n");
5420 sprintf (str, " * %d;\n break;", max_slots);
5421 for (av = attr->first_value; av; av = av->next)
5422 if (av != common_av)
5423 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
5425 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
5428 /* Ensure matched. Otherwise, shouldn't have been called. */
5429 printf (" if (slot < %d)\n", max_slots);
5430 printf (" abort ();\n\n");
5433 /* If just one type of delay slot, write simple switch. */
5434 if (num_delays == 1 && max_slots == 1)
5436 printf (" insn = candidate_insn;\n");
5437 printf (" switch (recog_memoized (insn))\n");
5440 attr = find_attr ("*delay_1_0", 0);
5441 if (! attr) abort ();
5442 common_av = find_most_used (attr);
5444 for (av = attr->first_value; av; av = av->next)
5445 if (av != common_av)
5446 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
5448 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
5454 /* Write a nested CASE. The first indicates which condition we need to
5455 test, and the inner CASE tests the condition. */
5456 printf (" insn = candidate_insn;\n");
5457 printf (" switch (slot)\n");
5460 for (delay = delays; delay; delay = delay->next)
5461 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
5463 printf (" case %d:\n",
5464 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
5465 printf (" switch (recog_memoized (insn))\n");
5468 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
5469 attr = find_attr (str, 0);
5470 if (! attr) abort ();
5471 common_av = find_most_used (attr);
5473 for (av = attr->first_value; av; av = av->next)
5474 if (av != common_av)
5475 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
5477 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
5481 printf (" default:\n");
5482 printf (" abort ();\n");
5489 /* Write routines to compute conflict cost for function units. Then write a
5490 table describing the available function units. */
5493 write_function_unit_info ()
5495 struct function_unit *unit;
5498 /* Write out conflict routines for function units. Don't bother writing
5499 one if there is only one issue delay value. */
5501 for (unit = units; unit; unit = unit->next)
5503 if (unit->needs_blockage_function)
5504 write_complex_function (unit, "blockage", "block");
5506 /* If the minimum and maximum conflict costs are the same, there
5507 is only one value, so we don't need a function. */
5508 if (! unit->needs_conflict_function)
5510 unit->default_cost = make_numeric_value (unit->issue_delay.max);
5514 /* The function first computes the case from the candidate insn. */
5515 unit->default_cost = make_numeric_value (0);
5516 write_complex_function (unit, "conflict_cost", "cost");
5519 /* Now that all functions have been written, write the table describing
5520 the function units. The name is included for documentation purposes
5523 printf ("struct function_unit_desc function_units[] = {\n");
5525 /* Write out the descriptions in numeric order, but don't force that order
5526 on the list. Doing so increases the runtime of genattrtab.c. */
5527 for (i = 0; i < num_units; i++)
5529 for (unit = units; unit; unit = unit->next)
5533 printf (" {\"%s\", %d, %d, %d, %s, %d, %s_unit_ready_cost, ",
5534 unit->name, 1 << unit->num, unit->multiplicity,
5535 unit->simultaneity, XSTR (unit->default_cost, 0),
5536 unit->issue_delay.max, unit->name);
5538 if (unit->needs_conflict_function)
5539 printf ("%s_unit_conflict_cost, ", unit->name);
5543 printf ("%d, ", unit->max_blockage);
5545 if (unit->needs_range_function)
5546 printf ("%s_unit_blockage_range, ", unit->name);
5550 if (unit->needs_blockage_function)
5551 printf ("%s_unit_blockage", unit->name);
5562 write_complex_function (unit, name, connection)
5563 struct function_unit *unit;
5564 const char *name, *connection;
5566 struct attr_desc *case_attr, *attr;
5567 struct attr_value *av, *common_av;
5573 printf ("static int %s_unit_%s PROTO ((rtx, rtx));\n", unit->name, name);
5574 printf ("static int\n");
5575 printf ("%s_unit_%s (executing_insn, candidate_insn)\n",
5577 printf (" rtx executing_insn;\n");
5578 printf (" rtx candidate_insn;\n");
5580 printf (" rtx insn;\n");
5581 printf (" int casenum;\n\n");
5582 printf (" insn = executing_insn;\n");
5583 printf (" switch (recog_memoized (insn))\n");
5586 /* Write the `switch' statement to get the case value. */
5587 str = (char *) alloca (strlen (unit->name) + strlen (name) + strlen (connection) + 10);
5588 sprintf (str, "*%s_cases", unit->name);
5589 case_attr = find_attr (str, 0);
5590 if (! case_attr) abort ();
5591 common_av = find_most_used (case_attr);
5593 for (av = case_attr->first_value; av; av = av->next)
5594 if (av != common_av)
5595 write_attr_case (case_attr, av, 1,
5596 "casenum =", ";", 4, unit->condexp);
5598 write_attr_case (case_attr, common_av, 0,
5599 "casenum =", ";", 4, unit->condexp);
5602 /* Now write an outer switch statement on each case. Then write
5603 the tests on the executing function within each. */
5604 printf (" insn = candidate_insn;\n");
5605 printf (" switch (casenum)\n");
5608 for (i = 0; i < unit->num_opclasses; i++)
5610 /* Ensure using this case. */
5612 for (av = case_attr->first_value; av; av = av->next)
5614 && contained_in_p (make_numeric_value (i), av->value))
5620 printf (" case %d:\n", i);
5621 sprintf (str, "*%s_%s_%d", unit->name, connection, i);
5622 attr = find_attr (str, 0);
5623 if (! attr) abort ();
5625 /* If single value, just write it. */
5626 value = find_single_value (attr);
5628 write_attr_set (attr, 6, value, "return", ";\n", true_rtx, -2, -2);
5631 common_av = find_most_used (attr);
5632 printf (" switch (recog_memoized (insn))\n");
5635 for (av = attr->first_value; av; av = av->next)
5636 if (av != common_av)
5637 write_attr_case (attr, av, 1,
5638 "return", ";", 8, unit->condexp);
5640 write_attr_case (attr, common_av, 0,
5641 "return", ";", 8, unit->condexp);
5646 /* This default case should not be needed, but gcc's analysis is not
5647 good enough to realize that the default case is not needed for the
5648 second switch statement. */
5649 printf (" default:\n abort ();\n");
5650 printf (" }\n}\n\n");
5653 /* This page contains miscellaneous utility routines. */
5655 /* Given a string, return the number of comma-separated elements in it.
5656 Return 0 for the null string. */
5667 for (n = 1; *s; s++)
5674 /* Given a pointer to a (char *), return a malloc'ed string containing the
5675 next comma-separated element. Advance the pointer to after the string
5676 scanned, or the end-of-string. Return NULL if at end of string. */
5679 next_comma_elt (pstr)
5688 /* Find end of string to compute length. */
5689 for (p = *pstr; *p != ',' && *p != '\0'; p++)
5692 out_str = attr_string (*pstr, p - *pstr);
5701 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
5702 is non-zero, build a new attribute, if one does not exist. */
5704 static struct attr_desc *
5705 find_attr (name, create)
5709 struct attr_desc *attr;
5712 /* Before we resort to using `strcmp', see if the string address matches
5713 anywhere. In most cases, it should have been canonicalized to do so. */
5714 if (name == alternative_name)
5717 index = name[0] & (MAX_ATTRS_INDEX - 1);
5718 for (attr = attrs[index]; attr; attr = attr->next)
5719 if (name == attr->name)
5722 /* Otherwise, do it the slow way. */
5723 for (attr = attrs[index]; attr; attr = attr->next)
5724 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
5730 attr = (struct attr_desc *) oballoc (sizeof (struct attr_desc));
5731 attr->name = attr_string (name, strlen (name));
5732 attr->first_value = attr->default_val = NULL;
5733 attr->is_numeric = attr->negative_ok = attr->is_const = attr->is_special = 0;
5734 attr->next = attrs[index];
5735 attrs[index] = attr;
5740 /* Create internal attribute with the given default value. */
5743 make_internal_attr (name, value, special)
5748 struct attr_desc *attr;
5750 attr = find_attr (name, 1);
5751 if (attr->default_val)
5754 attr->is_numeric = 1;
5756 attr->is_special = (special & 1) != 0;
5757 attr->negative_ok = (special & 2) != 0;
5758 attr->unsigned_p = (special & 4) != 0;
5759 attr->func_units_p = (special & 8) != 0;
5760 attr->blockage_p = (special & 16) != 0;
5761 attr->default_val = get_attr_value (value, attr, -2);
5764 /* Find the most used value of an attribute. */
5766 static struct attr_value *
5767 find_most_used (attr)
5768 struct attr_desc *attr;
5770 struct attr_value *av;
5771 struct attr_value *most_used;
5777 for (av = attr->first_value; av; av = av->next)
5778 if (av->num_insns > nuses)
5779 nuses = av->num_insns, most_used = av;
5784 /* If an attribute only has a single value used, return it. Otherwise
5788 find_single_value (attr)
5789 struct attr_desc *attr;
5791 struct attr_value *av;
5794 unique_value = NULL;
5795 for (av = attr->first_value; av; av = av->next)
5801 unique_value = av->value;
5804 return unique_value;
5807 /* Return (attr_value "n") */
5810 make_numeric_value (n)
5813 static rtx int_values[20];
5820 if (n < 20 && int_values[n])
5821 return int_values[n];
5823 p = attr_printf (MAX_DIGITS, "%d", n);
5824 exp = attr_rtx (CONST_STRING, p);
5827 int_values[n] = exp;
5833 extend_range (range, min, max)
5834 struct range *range;
5838 if (range->min > min) range->min = min;
5839 if (range->max < max) range->max = max;
5843 xrealloc (old, size)
5849 ptr = (PTR) realloc (old, size);
5851 ptr = (PTR) malloc (size);
5853 fatal ("virtual memory exhausted");
5861 register PTR val = (PTR) malloc (size);
5864 fatal ("virtual memory exhausted");
5869 copy_rtx_unchanging (orig)
5874 register RTX_CODE code;
5877 if (RTX_UNCHANGING_P (orig) || MEM_IN_STRUCT_P (orig))
5880 MEM_IN_STRUCT_P (orig) = 1;
5884 code = GET_CODE (orig);
5897 copy = rtx_alloc (code);
5898 PUT_MODE (copy, GET_MODE (orig));
5899 RTX_UNCHANGING_P (copy) = 1;
5901 bcopy ((char *) &XEXP (orig, 0), (char *) &XEXP (copy, 0),
5902 GET_RTX_LENGTH (GET_CODE (copy)) * sizeof (rtx));
5907 /* Determine if an insn has a constant number of delay slots, i.e., the
5908 number of delay slots is not a function of the length of the insn. */
5911 write_const_num_delay_slots ()
5913 struct attr_desc *attr = find_attr ("*num_delay_slots", 0);
5914 struct attr_value *av;
5915 struct insn_ent *ie;
5919 printf ("int\nconst_num_delay_slots (insn)\n");
5920 printf (" rtx insn;\n");
5922 printf (" switch (recog_memoized (insn))\n");
5925 for (av = attr->first_value; av; av = av->next)
5928 walk_attr_value (av->value);
5931 for (ie = av->first_insn; ie; ie = ie->next)
5932 if (ie->insn_code != -1)
5933 printf (" case %d:\n", ie->insn_code);
5934 printf (" return 0;\n");
5938 printf (" default:\n");
5939 printf (" return 1;\n");
5940 printf (" }\n}\n\n");
5953 struct attr_desc *attr;
5954 struct insn_def *id;
5958 #if defined (RLIMIT_STACK) && defined (HAVE_GETRLIMIT) && defined (HAVE_SETRLIMIT)
5959 /* Get rid of any avoidable limit on stack size. */
5963 /* Set the stack limit huge so that alloca does not fail. */
5964 getrlimit (RLIMIT_STACK, &rlim);
5965 rlim.rlim_cur = rlim.rlim_max;
5966 setrlimit (RLIMIT_STACK, &rlim);
5970 progname = "genattrtab";
5971 obstack_init (rtl_obstack);
5972 obstack_init (hash_obstack);
5973 obstack_init (temp_obstack);
5976 fatal ("No input file name.");
5978 infile = fopen (argv[1], "r");
5982 exit (FATAL_EXIT_CODE);
5987 /* Set up true and false rtx's */
5988 true_rtx = rtx_alloc (CONST_INT);
5989 XWINT (true_rtx, 0) = 1;
5990 false_rtx = rtx_alloc (CONST_INT);
5991 XWINT (false_rtx, 0) = 0;
5992 RTX_UNCHANGING_P (true_rtx) = RTX_UNCHANGING_P (false_rtx) = 1;
5993 RTX_INTEGRATED_P (true_rtx) = RTX_INTEGRATED_P (false_rtx) = 1;
5995 alternative_name = attr_string ("alternative", strlen ("alternative"));
5997 printf ("/* Generated automatically by the program `genattrtab'\n\
5998 from the machine description file `md'. */\n\n");
6000 /* Read the machine description. */
6004 c = read_skip_spaces (infile);
6009 desc = read_rtx (infile);
6010 if (GET_CODE (desc) == DEFINE_INSN
6011 || GET_CODE (desc) == DEFINE_PEEPHOLE
6012 || GET_CODE (desc) == DEFINE_ASM_ATTRIBUTES)
6015 else if (GET_CODE (desc) == DEFINE_EXPAND)
6016 insn_code_number++, insn_index_number++;
6018 else if (GET_CODE (desc) == DEFINE_SPLIT)
6019 insn_code_number++, insn_index_number++;
6021 else if (GET_CODE (desc) == DEFINE_ATTR)
6024 insn_index_number++;
6027 else if (GET_CODE (desc) == DEFINE_DELAY)
6030 insn_index_number++;
6033 else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
6036 insn_index_number++;
6040 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
6041 if (! got_define_asm_attributes)
6043 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
6044 XVEC (tem, 0) = rtvec_alloc (0);
6048 /* Expand DEFINE_DELAY information into new attribute. */
6052 /* Expand DEFINE_FUNCTION_UNIT information into new attributes. */
6056 printf ("#include \"config.h\"\n");
6057 printf ("#include \"system.h\"\n");
6058 printf ("#include \"rtl.h\"\n");
6059 printf ("#include \"insn-config.h\"\n");
6060 printf ("#include \"recog.h\"\n");
6061 printf ("#include \"regs.h\"\n");
6062 printf ("#include \"real.h\"\n");
6063 printf ("#include \"output.h\"\n");
6064 printf ("#include \"insn-attr.h\"\n");
6065 printf ("#include \"toplev.h\"\n");
6067 printf ("#define operands recog_operand\n\n");
6069 /* Make `insn_alternatives'. */
6070 insn_alternatives = (int *) oballoc (insn_code_number * sizeof (int));
6071 for (id = defs; id; id = id->next)
6072 if (id->insn_code >= 0)
6073 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
6075 /* Make `insn_n_alternatives'. */
6076 insn_n_alternatives = (int *) oballoc (insn_code_number * sizeof (int));
6077 for (id = defs; id; id = id->next)
6078 if (id->insn_code >= 0)
6079 insn_n_alternatives[id->insn_code] = id->num_alternatives;
6081 /* Prepare to write out attribute subroutines by checking everything stored
6082 away and building the attribute cases. */
6085 for (i = 0; i < MAX_ATTRS_INDEX; i++)
6086 for (attr = attrs[i]; attr; attr = attr->next)
6088 attr->default_val->value
6089 = check_attr_value (attr->default_val->value, attr);
6093 /* Construct extra attributes for `length'. */
6094 make_length_attrs ();
6096 /* Perform any possible optimizations to speed up compilation. */
6099 /* Now write out all the `gen_attr_...' routines. Do these before the
6100 special routines (specifically before write_function_unit_info), so
6101 that they get defined before they are used. */
6103 for (i = 0; i < MAX_ATTRS_INDEX; i++)
6104 for (attr = attrs[i]; attr; attr = attr->next)
6106 if (! attr->is_special && ! attr->is_const)
6107 write_attr_get (attr);
6110 /* Write out delay eligibility information, if DEFINE_DELAY present.
6111 (The function to compute the number of delay slots will be written
6115 write_eligible_delay ("delay");
6116 if (have_annul_true)
6117 write_eligible_delay ("annul_true");
6118 if (have_annul_false)
6119 write_eligible_delay ("annul_false");
6122 /* Write out information about function units. */
6124 write_function_unit_info ();
6126 /* Write out constant delay slot info */
6127 write_const_num_delay_slots ();
6129 write_length_unit_log ();
6132 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);