1 /* Generate code from machine description to compute values of attributes.
2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
4 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This program handles insn attributes and the DEFINE_DELAY and
24 DEFINE_FUNCTION_UNIT definitions.
26 It produces a series of functions named `get_attr_...', one for each insn
27 attribute. Each of these is given the rtx for an insn and returns a member
28 of the enum for the attribute.
30 These subroutines have the form of a `switch' on the INSN_CODE (via
31 `recog_memoized'). Each case either returns a constant attribute value
32 or a value that depends on tests on other attributes, the form of
33 operands, or some random C expression (encoded with a SYMBOL_REF
36 If the attribute `alternative', or a random C expression is present,
37 `constrain_operands' is called. If either of these cases of a reference to
38 an operand is found, `extract_insn' is called.
40 The special attribute `length' is also recognized. For this operand,
41 expressions involving the address of an operand or the current insn,
42 (address (pc)), are valid. In this case, an initial pass is made to
43 set all lengths that do not depend on address. Those that do are set to
44 the maximum length. Then each insn that depends on an address is checked
45 and possibly has its length changed. The process repeats until no further
46 changed are made. The resulting lengths are saved for use by
49 A special form of DEFINE_ATTR, where the expression for default value is a
50 CONST expression, indicates an attribute that is constant for a given run
51 of the compiler. The subroutine generated for these attributes has no
52 parameters as it does not depend on any particular insn. Constant
53 attributes are typically used to specify which variety of processor is
56 Internal attributes are defined to handle DEFINE_DELAY and
57 DEFINE_FUNCTION_UNIT. Special routines are output for these cases.
59 This program works by keeping a list of possible values for each attribute.
60 These include the basic attribute choices, default values for attribute, and
61 all derived quantities.
63 As the description file is read, the definition for each insn is saved in a
64 `struct insn_def'. When the file reading is complete, a `struct insn_ent'
65 is created for each insn and chained to the corresponding attribute value,
66 either that specified, or the default.
68 An optimization phase is then run. This simplifies expressions for each
69 insn. EQ_ATTR tests are resolved, whenever possible, to a test that
70 indicates when the attribute has the specified value for the insn. This
71 avoids recursive calls during compilation.
73 The strategy used when processing DEFINE_DELAY and DEFINE_FUNCTION_UNIT
74 definitions is to create arbitrarily complex expressions and have the
75 optimization simplify them.
77 Once optimization is complete, any required routines and definitions
80 An optimization that is not yet implemented is to hoist the constant
81 expressions entirely out of the routines and definitions that are written.
82 A way to do this is to iterate over all possible combinations of values
83 for constant attributes and generate a set of functions for that given
84 combination. An initialization function would be written that evaluates
85 the attributes and installs the corresponding set of routines and
86 definitions (each would be accessed through a pointer).
88 We use the flags in an RTX as follows:
89 `unchanging' (ATTR_IND_SIMPLIFIED_P): This rtx is fully simplified
90 independent of the insn code.
91 `in_struct' (ATTR_CURR_SIMPLIFIED_P): This rtx is fully simplified
92 for the insn code currently being processed (see optimize_attrs).
93 `integrated' (ATTR_PERMANENT_P): This rtx is permanent and unique
95 `volatil' (ATTR_EQ_ATTR_P): During simplify_by_exploding the value of an
96 EQ_ATTR rtx is true if !volatil and false if volatil. */
98 #define ATTR_IND_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), unchanging))
99 #define ATTR_CURR_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), in_struct))
100 #define ATTR_PERMANENT_P(RTX) (RTX_FLAG((RTX), integrated))
101 #define ATTR_EQ_ATTR_P(RTX) (RTX_FLAG((RTX), volatil))
105 #include "coretypes.h"
109 #include "gensupport.h"
111 #ifdef HAVE_SYS_RESOURCE_H
112 # include <sys/resource.h>
115 /* We must include obstack.h after <sys/time.h>, to avoid lossage with
116 /usr/include/sys/stdtypes.h on Sun OS 4.x. */
120 #include "genattrtab.h"
122 static struct obstack obstack1, obstack2;
123 struct obstack *hash_obstack = &obstack1;
124 struct obstack *temp_obstack = &obstack2;
126 /* enough space to reserve for printing out ints */
127 #define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)
129 /* Define structures used to record attributes and values. */
131 /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
132 encountered, we store all the relevant information into a
133 `struct insn_def'. This is done to allow attribute definitions to occur
134 anywhere in the file. */
138 struct insn_def *next; /* Next insn in chain. */
139 rtx def; /* The DEFINE_... */
140 int insn_code; /* Instruction number. */
141 int insn_index; /* Expression numer in file, for errors. */
142 int lineno; /* Line number. */
143 int num_alternatives; /* Number of alternatives. */
144 int vec_idx; /* Index of attribute vector in `def'. */
147 /* Once everything has been read in, we store in each attribute value a list
148 of insn codes that have that value. Here is the structure used for the
153 struct insn_ent *next; /* Next in chain. */
154 int insn_code; /* Instruction number. */
155 int insn_index; /* Index of definition in file */
156 int lineno; /* Line number. */
159 /* Each value of an attribute (either constant or computed) is assigned a
160 structure which is used as the listhead of the insns that have that
165 rtx value; /* Value of attribute. */
166 struct attr_value *next; /* Next attribute value in chain. */
167 struct insn_ent *first_insn; /* First insn with this value. */
168 int num_insns; /* Number of insns with this value. */
169 int has_asm_insn; /* True if this value used for `asm' insns */
172 /* Structure for each attribute. */
176 char *name; /* Name of attribute. */
177 struct attr_desc *next; /* Next attribute. */
178 struct attr_value *first_value; /* First value of this attribute. */
179 struct attr_value *default_val; /* Default value for this attribute. */
180 int lineno : 24; /* Line number. */
181 unsigned is_numeric : 1; /* Values of this attribute are numeric. */
182 unsigned negative_ok : 1; /* Allow negative numeric values. */
183 unsigned unsigned_p : 1; /* Make the output function unsigned int. */
184 unsigned is_const : 1; /* Attribute value constant for each run. */
185 unsigned is_special : 1; /* Don't call `write_attr_set'. */
186 unsigned func_units_p : 1; /* this is the function_units attribute */
187 unsigned blockage_p : 1; /* this is the blockage range function */
190 #define NULL_ATTR (struct attr_desc *) NULL
192 /* A range of values. */
200 /* Structure for each DEFINE_DELAY. */
204 rtx def; /* DEFINE_DELAY expression. */
205 struct delay_desc *next; /* Next DEFINE_DELAY. */
206 int num; /* Number of DEFINE_DELAY, starting at 1. */
207 int lineno; /* Line number. */
210 /* Record information about each DEFINE_FUNCTION_UNIT. */
212 struct function_unit_op
214 rtx condexp; /* Expression TRUE for applicable insn. */
215 struct function_unit_op *next; /* Next operation for this function unit. */
216 int num; /* Ordinal for this operation type in unit. */
217 int ready; /* Cost until data is ready. */
218 int issue_delay; /* Cost until unit can accept another insn. */
219 rtx conflict_exp; /* Expression TRUE for insns incurring issue delay. */
220 rtx issue_exp; /* Expression computing issue delay. */
221 int lineno; /* Line number. */
224 /* Record information about each function unit mentioned in a
225 DEFINE_FUNCTION_UNIT. */
229 const char *name; /* Function unit name. */
230 struct function_unit *next; /* Next function unit. */
231 int num; /* Ordinal of this unit type. */
232 int multiplicity; /* Number of units of this type. */
233 int simultaneity; /* Maximum number of simultaneous insns
234 on this function unit or 0 if unlimited. */
235 rtx condexp; /* Expression TRUE for insn needing unit. */
236 int num_opclasses; /* Number of different operation types. */
237 struct function_unit_op *ops; /* Pointer to first operation type. */
238 int needs_conflict_function; /* Nonzero if a conflict function required. */
239 int needs_blockage_function; /* Nonzero if a blockage function required. */
240 int needs_range_function; /* Nonzero if blockage range function needed. */
241 rtx default_cost; /* Conflict cost, if constant. */
242 struct range issue_delay; /* Range of issue delay values. */
243 int max_blockage; /* Maximum time an insn blocks the unit. */
244 int first_lineno; /* First seen line number. */
247 /* Listheads of above structures. */
249 /* This one is indexed by the first character of the attribute name. */
250 #define MAX_ATTRS_INDEX 256
251 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
252 static struct insn_def *defs;
253 static struct delay_desc *delays;
254 static struct function_unit *units;
256 /* An expression where all the unknown terms are EQ_ATTR tests can be
257 rearranged into a COND provided we can enumerate all possible
258 combinations of the unknown values. The set of combinations become the
259 tests of the COND; the value of the expression given that combination is
260 computed and becomes the corresponding value. To do this, we must be
261 able to enumerate all values for each attribute used in the expression
262 (currently, we give up if we find a numeric attribute).
264 If the set of EQ_ATTR tests used in an expression tests the value of N
265 different attributes, the list of all possible combinations can be made
266 by walking the N-dimensional attribute space defined by those
267 attributes. We record each of these as a struct dimension.
269 The algorithm relies on sharing EQ_ATTR nodes: if two nodes in an
270 expression are the same, the will also have the same address. We find
271 all the EQ_ATTR nodes by marking them ATTR_EQ_ATTR_P. This bit later
272 represents the value of an EQ_ATTR node, so once all nodes are marked,
273 they are also given an initial value of FALSE.
275 We then separate the set of EQ_ATTR nodes into dimensions for each
276 attribute and put them on the VALUES list. Terms are added as needed by
277 `add_values_to_cover' so that all possible values of the attribute are
280 Each dimension also has a current value. This is the node that is
281 currently considered to be TRUE. If this is one of the nodes added by
282 `add_values_to_cover', all the EQ_ATTR tests in the original expression
283 will be FALSE. Otherwise, only the CURRENT_VALUE will be true.
285 NUM_VALUES is simply the length of the VALUES list and is there for
288 Once the dimensions are created, the algorithm enumerates all possible
289 values and computes the current value of the given expression. */
293 struct attr_desc *attr; /* Attribute for this dimension. */
294 rtx values; /* List of attribute values used. */
295 rtx current_value; /* Position in the list for the TRUE value. */
296 int num_values; /* Length of the values list. */
299 /* Other variables. */
301 static int insn_code_number;
302 static int insn_index_number;
303 static int got_define_asm_attributes;
304 static int must_extract;
305 static int must_constrain;
306 static int address_used;
307 static int length_used;
308 static int num_delays;
309 static int have_annul_true, have_annul_false;
310 static int num_units, num_unit_opclasses;
311 static int num_insn_ents;
315 /* Used as operand to `operate_exp': */
317 enum operator {PLUS_OP, MINUS_OP, POS_MINUS_OP, EQ_OP, OR_OP, ORX_OP, MAX_OP, MIN_OP, RANGE_OP};
319 /* Stores, for each insn code, the number of constraint alternatives. */
321 static int *insn_n_alternatives;
323 /* Stores, for each insn code, a bitmap that has bits on for each possible
326 static int *insn_alternatives;
328 /* If nonzero, assume that the `alternative' attr has this value.
329 This is the hashed, unique string for the numeral
330 whose value is chosen alternative. */
332 static const char *current_alternative_string;
334 /* Used to simplify expressions. */
336 static rtx true_rtx, false_rtx;
338 /* Used to reduce calls to `strcmp' */
340 static char *alternative_name;
342 /* Indicate that REG_DEAD notes are valid if dead_or_set_p is ever
345 int reload_completed = 0;
347 /* Some machines test `optimize' in macros called from rtlanal.c, so we need
348 to define it here. */
352 /* Simplify an expression. Only call the routine if there is something to
354 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
355 (ATTR_IND_SIMPLIFIED_P (EXP) || ATTR_CURR_SIMPLIFIED_P (EXP) ? (EXP) \
356 : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
358 /* Simplify (eq_attr ("alternative") ...)
359 when we are working with a particular alternative. */
360 #define SIMPLIFY_ALTERNATIVE(EXP) \
361 if (current_alternative_string \
362 && GET_CODE ((EXP)) == EQ_ATTR \
363 && XSTR ((EXP), 0) == alternative_name) \
364 (EXP) = (XSTR ((EXP), 1) == current_alternative_string \
365 ? true_rtx : false_rtx);
367 /* These are referenced by rtlanal.c and hence need to be defined somewhere.
368 They won't actually be used. */
370 rtx global_rtl[GR_MAX];
371 rtx pic_offset_table_rtx;
373 static void attr_hash_add_rtx (int, rtx);
374 static void attr_hash_add_string (int, char *);
375 static rtx attr_rtx (enum rtx_code, ...);
376 static rtx attr_rtx_1 (enum rtx_code, va_list);
377 static char *attr_string (const char *, int);
378 static rtx check_attr_value (rtx, struct attr_desc *);
379 static rtx convert_set_attr_alternative (rtx, struct insn_def *);
380 static rtx convert_set_attr (rtx, struct insn_def *);
381 static void check_defs (void);
382 static rtx make_canonical (struct attr_desc *, rtx);
383 static struct attr_value *get_attr_value (rtx, struct attr_desc *, int);
384 static rtx copy_rtx_unchanging (rtx);
385 static rtx copy_boolean (rtx);
386 static void expand_delays (void);
387 static rtx operate_exp (enum operator, rtx, rtx);
388 static void expand_units (void);
389 static rtx simplify_knowing (rtx, rtx);
390 static rtx encode_units_mask (rtx);
391 static void fill_attr (struct attr_desc *);
392 static rtx substitute_address (rtx, rtx (*) (rtx), rtx (*) (rtx));
393 static void make_length_attrs (void);
394 static rtx identity_fn (rtx);
395 static rtx zero_fn (rtx);
396 static rtx one_fn (rtx);
397 static rtx max_fn (rtx);
398 static void write_length_unit_log (void);
399 static rtx simplify_cond (rtx, int, int);
400 static rtx simplify_by_exploding (rtx);
401 static int find_and_mark_used_attributes (rtx, rtx *, int *);
402 static void unmark_used_attributes (rtx, struct dimension *, int);
403 static int add_values_to_cover (struct dimension *);
404 static int increment_current_value (struct dimension *, int);
405 static rtx test_for_current_value (struct dimension *, int);
406 static rtx simplify_with_current_value (rtx, struct dimension *, int);
407 static rtx simplify_with_current_value_aux (rtx);
408 static void clear_struct_flag (rtx);
409 static int count_sub_rtxs (rtx, int);
410 static void remove_insn_ent (struct attr_value *, struct insn_ent *);
411 static void insert_insn_ent (struct attr_value *, struct insn_ent *);
412 static rtx insert_right_side (enum rtx_code, rtx, rtx, int, int);
413 static rtx make_alternative_compare (int);
414 static int compute_alternative_mask (rtx, enum rtx_code);
415 static rtx evaluate_eq_attr (rtx, rtx, int, int);
416 static rtx simplify_and_tree (rtx, rtx *, int, int);
417 static rtx simplify_or_tree (rtx, rtx *, int, int);
418 static rtx simplify_test_exp (rtx, int, int);
419 static rtx simplify_test_exp_in_temp (rtx, int, int);
420 static void optimize_attrs (void);
421 static void gen_attr (rtx, int);
422 static int count_alternatives (rtx);
423 static int compares_alternatives_p (rtx);
424 static int contained_in_p (rtx, rtx);
425 static void gen_insn (rtx, int);
426 static void gen_delay (rtx, int);
427 static void gen_unit (rtx, int);
428 static void write_test_expr (rtx, int);
429 static int max_attr_value (rtx, int*);
430 static int or_attr_value (rtx, int*);
431 static void walk_attr_value (rtx);
432 static void write_attr_get (struct attr_desc *);
433 static rtx eliminate_known_true (rtx, rtx, int, int);
434 static void write_attr_set (struct attr_desc *, int, rtx,
435 const char *, const char *, rtx,
437 static void write_attr_case (struct attr_desc *, struct attr_value *,
438 int, const char *, const char *, int, rtx);
439 static void write_unit_name (const char *, int, const char *);
440 static void write_attr_valueq (struct attr_desc *, const char *);
441 static void write_attr_value (struct attr_desc *, rtx);
442 static void write_upcase (const char *);
443 static void write_indent (int);
444 static void write_eligible_delay (const char *);
445 static void write_function_unit_info (void);
446 static void write_complex_function (struct function_unit *, const char *,
448 static int write_expr_attr_cache (rtx, struct attr_desc *);
449 static void write_toplevel_expr (rtx);
450 static void write_const_num_delay_slots (void);
451 static char *next_comma_elt (const char **);
452 static struct attr_desc *find_attr (const char *, int);
453 static struct attr_value *find_most_used (struct attr_desc *);
454 static rtx find_single_value (struct attr_desc *);
455 static void extend_range (struct range *, int, int);
456 static rtx attr_eq (const char *, const char *);
457 static const char *attr_numeral (int);
458 static int attr_equal_p (rtx, rtx);
459 static rtx attr_copy_rtx (rtx);
460 static int attr_rtx_cost (rtx);
462 #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 (int hashcode, rtx rtl)
502 h = (struct attr_hash *) obstack_alloc (hash_obstack,
503 sizeof (struct attr_hash));
504 h->hashcode = hashcode;
506 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
507 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
510 /* Add an entry to the hash table for STRING with hash code HASHCODE. */
513 attr_hash_add_string (int hashcode, char *str)
517 h = (struct attr_hash *) obstack_alloc (hash_obstack,
518 sizeof (struct attr_hash));
519 h->hashcode = -hashcode;
521 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
522 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
525 /* Generate an RTL expression, but avoid duplicates.
526 Set the ATTR_PERMANENT_P flag for these permanent objects.
528 In some cases we cannot uniquify; then we return an ordinary
529 impermanent rtx with ATTR_PERMANENT_P clear.
531 Args are like gen_rtx, but without the mode:
533 rtx attr_rtx (code, [element1, ..., elementn]) */
536 attr_rtx_1 (enum rtx_code code, va_list p)
538 rtx rt_val = NULL_RTX;/* RTX to return to caller... */
541 struct obstack *old_obstack = rtl_obstack;
543 /* For each of several cases, search the hash table for an existing entry.
544 Use that entry if one is found; otherwise create a new RTL and add it
547 if (GET_RTX_CLASS (code) == '1')
549 rtx arg0 = va_arg (p, rtx);
551 /* A permanent object cannot point to impermanent ones. */
552 if (! ATTR_PERMANENT_P (arg0))
554 rt_val = rtx_alloc (code);
555 XEXP (rt_val, 0) = arg0;
559 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
560 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
561 if (h->hashcode == hashcode
562 && GET_CODE (h->u.rtl) == code
563 && XEXP (h->u.rtl, 0) == arg0)
568 rtl_obstack = hash_obstack;
569 rt_val = rtx_alloc (code);
570 XEXP (rt_val, 0) = arg0;
573 else if (GET_RTX_CLASS (code) == 'c'
574 || GET_RTX_CLASS (code) == '2'
575 || GET_RTX_CLASS (code) == '<')
577 rtx arg0 = va_arg (p, rtx);
578 rtx arg1 = va_arg (p, rtx);
580 /* A permanent object cannot point to impermanent ones. */
581 if (! ATTR_PERMANENT_P (arg0) || ! ATTR_PERMANENT_P (arg1))
583 rt_val = rtx_alloc (code);
584 XEXP (rt_val, 0) = arg0;
585 XEXP (rt_val, 1) = arg1;
589 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
590 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
591 if (h->hashcode == hashcode
592 && GET_CODE (h->u.rtl) == code
593 && XEXP (h->u.rtl, 0) == arg0
594 && XEXP (h->u.rtl, 1) == arg1)
599 rtl_obstack = hash_obstack;
600 rt_val = rtx_alloc (code);
601 XEXP (rt_val, 0) = arg0;
602 XEXP (rt_val, 1) = arg1;
605 else if (GET_RTX_LENGTH (code) == 1
606 && GET_RTX_FORMAT (code)[0] == 's')
608 char *arg0 = va_arg (p, char *);
610 if (code == SYMBOL_REF)
611 arg0 = attr_string (arg0, strlen (arg0));
613 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
614 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
615 if (h->hashcode == hashcode
616 && GET_CODE (h->u.rtl) == code
617 && XSTR (h->u.rtl, 0) == arg0)
622 rtl_obstack = hash_obstack;
623 rt_val = rtx_alloc (code);
624 XSTR (rt_val, 0) = arg0;
627 else if (GET_RTX_LENGTH (code) == 2
628 && GET_RTX_FORMAT (code)[0] == 's'
629 && GET_RTX_FORMAT (code)[1] == 's')
631 char *arg0 = va_arg (p, char *);
632 char *arg1 = va_arg (p, char *);
634 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
635 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
636 if (h->hashcode == hashcode
637 && GET_CODE (h->u.rtl) == code
638 && XSTR (h->u.rtl, 0) == arg0
639 && XSTR (h->u.rtl, 1) == arg1)
644 rtl_obstack = hash_obstack;
645 rt_val = rtx_alloc (code);
646 XSTR (rt_val, 0) = arg0;
647 XSTR (rt_val, 1) = arg1;
650 else if (code == CONST_INT)
652 HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
662 int i; /* Array indices... */
663 const char *fmt; /* Current rtx's format... */
665 rt_val = rtx_alloc (code); /* Allocate the storage space. */
667 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
668 for (i = 0; i < GET_RTX_LENGTH (code); i++)
672 case '0': /* Unused field. */
675 case 'i': /* An integer? */
676 XINT (rt_val, i) = va_arg (p, int);
679 case 'w': /* A wide integer? */
680 XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
683 case 's': /* A string? */
684 XSTR (rt_val, i) = va_arg (p, char *);
687 case 'e': /* An expression? */
688 case 'u': /* An insn? Same except when printing. */
689 XEXP (rt_val, i) = va_arg (p, rtx);
692 case 'E': /* An RTX vector? */
693 XVEC (rt_val, i) = va_arg (p, rtvec);
703 rtl_obstack = old_obstack;
704 attr_hash_add_rtx (hashcode, rt_val);
705 ATTR_PERMANENT_P (rt_val) = 1;
710 attr_rtx (enum rtx_code code, ...)
716 result = attr_rtx_1 (code, p);
721 /* Create a new string printed with the printf line arguments into a space
722 of at most LEN bytes:
724 rtx attr_printf (len, format, [arg1, ..., argn]) */
727 attr_printf (unsigned int len, const char *fmt, ...)
734 if (len > sizeof str - 1) /* Leave room for \0. */
737 vsprintf (str, fmt, p);
740 return attr_string (str, strlen (str));
744 attr_eq (const char *name, const char *value)
746 return attr_rtx (EQ_ATTR, attr_string (name, strlen (name)),
747 attr_string (value, strlen (value)));
753 return XSTR (make_numeric_value (n), 0);
756 /* Return a permanent (possibly shared) copy of a string STR (not assumed
757 to be null terminated) with LEN bytes. */
760 attr_string (const char *str, int len)
767 /* Compute the hash code. */
768 hashcode = (len + 1) * 613 + (unsigned) str[0];
769 for (i = 1; i <= len; i += 2)
770 hashcode = ((hashcode * 613) + (unsigned) str[i]);
772 hashcode = -hashcode;
774 /* Search the table for the string. */
775 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
776 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
777 && !strncmp (h->u.str, str, len))
778 return h->u.str; /* <-- return if found. */
780 /* Not found; create a permanent copy and add it to the hash table. */
781 new_str = (char *) obstack_alloc (hash_obstack, len + 1);
782 memcpy (new_str, str, len);
784 attr_hash_add_string (hashcode, new_str);
786 return new_str; /* Return the new string. */
789 /* Check two rtx's for equality of contents,
790 taking advantage of the fact that if both are hashed
791 then they can't be equal unless they are the same object. */
794 attr_equal_p (rtx x, rtx y)
796 return (x == y || (! (ATTR_PERMANENT_P (x) && ATTR_PERMANENT_P (y))
797 && rtx_equal_p (x, y)));
800 /* Copy an attribute value expression,
801 descending to all depths, but not copying any
802 permanent hashed subexpressions. */
805 attr_copy_rtx (rtx orig)
810 const char *format_ptr;
812 /* No need to copy a permanent object. */
813 if (ATTR_PERMANENT_P (orig))
816 code = GET_CODE (orig);
835 copy = rtx_alloc (code);
836 PUT_MODE (copy, GET_MODE (orig));
837 ATTR_IND_SIMPLIFIED_P (copy) = ATTR_IND_SIMPLIFIED_P (orig);
838 ATTR_CURR_SIMPLIFIED_P (copy) = ATTR_CURR_SIMPLIFIED_P (orig);
839 ATTR_PERMANENT_P (copy) = ATTR_PERMANENT_P (orig);
840 ATTR_EQ_ATTR_P (copy) = ATTR_EQ_ATTR_P (orig);
842 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
844 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
846 switch (*format_ptr++)
849 XEXP (copy, i) = XEXP (orig, i);
850 if (XEXP (orig, i) != NULL)
851 XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
856 XVEC (copy, i) = XVEC (orig, i);
857 if (XVEC (orig, i) != NULL)
859 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
860 for (j = 0; j < XVECLEN (copy, i); j++)
861 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
867 XINT (copy, i) = XINT (orig, i);
871 XWINT (copy, i) = XWINT (orig, i);
876 XSTR (copy, i) = XSTR (orig, i);
886 /* Given a test expression for an attribute, ensure it is validly formed.
887 IS_CONST indicates whether the expression is constant for each compiler
888 run (a constant expression may not test any particular insn).
890 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
891 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
892 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
894 Update the string address in EQ_ATTR expression to be the same used
895 in the attribute (or `alternative_name') to speed up subsequent
896 `find_attr' calls and eliminate most `strcmp' calls.
898 Return the new expression, if any. */
901 check_attr_test (rtx exp, int is_const, int lineno)
903 struct attr_desc *attr;
904 struct attr_value *av;
905 const char *name_ptr, *p;
908 switch (GET_CODE (exp))
911 /* Handle negation test. */
912 if (XSTR (exp, 1)[0] == '!')
913 return check_attr_test (attr_rtx (NOT,
914 attr_eq (XSTR (exp, 0),
918 else if (n_comma_elts (XSTR (exp, 1)) == 1)
920 attr = find_attr (XSTR (exp, 0), 0);
923 if (! strcmp (XSTR (exp, 0), "alternative"))
925 XSTR (exp, 0) = alternative_name;
926 /* This can't be simplified any further. */
927 ATTR_IND_SIMPLIFIED_P (exp) = 1;
931 fatal ("unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
934 if (is_const && ! attr->is_const)
935 fatal ("constant expression uses insn attribute `%s' in EQ_ATTR",
938 /* Copy this just to make it permanent,
939 so expressions using it can be permanent too. */
940 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
942 /* It shouldn't be possible to simplify the value given to a
943 constant attribute, so don't expand this until it's time to
944 write the test expression. */
946 ATTR_IND_SIMPLIFIED_P (exp) = 1;
948 if (attr->is_numeric)
950 for (p = XSTR (exp, 1); *p; p++)
952 fatal ("attribute `%s' takes only numeric values",
957 for (av = attr->first_value; av; av = av->next)
958 if (GET_CODE (av->value) == CONST_STRING
959 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
963 fatal ("unknown value `%s' for `%s' attribute",
964 XSTR (exp, 1), XSTR (exp, 0));
969 /* Make an IOR tree of the possible values. */
971 name_ptr = XSTR (exp, 1);
972 while ((p = next_comma_elt (&name_ptr)) != NULL)
974 newexp = attr_eq (XSTR (exp, 0), p);
975 orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
978 return check_attr_test (orexp, is_const, lineno);
986 /* Either TRUE or FALSE. */
994 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
995 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const, lineno);
999 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
1005 fatal ("RTL operator \"%s\" not valid in constant attribute test",
1006 GET_RTX_NAME (GET_CODE (exp)));
1007 /* These cases can't be simplified. */
1008 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1011 case LE: case LT: case GT: case GE:
1012 case LEU: case LTU: case GTU: case GEU:
1014 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
1015 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
1016 exp = attr_rtx (GET_CODE (exp),
1017 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
1018 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
1019 /* These cases can't be simplified. */
1020 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1026 /* These cases are valid for constant attributes, but can't be
1028 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1029 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1033 fatal ("RTL operator \"%s\" not valid in attribute test",
1034 GET_RTX_NAME (GET_CODE (exp)));
1040 /* Given an expression, ensure that it is validly formed and that all named
1041 attribute values are valid for the given attribute. Issue a fatal error
1042 if not. If no attribute is specified, assume a numeric attribute.
1044 Return a perhaps modified replacement expression for the value. */
1047 check_attr_value (rtx exp, struct attr_desc *attr)
1049 struct attr_value *av;
1053 switch (GET_CODE (exp))
1056 if (attr && ! attr->is_numeric)
1058 message_with_line (attr->lineno,
1059 "CONST_INT not valid for non-numeric attribute %s",
1065 if (INTVAL (exp) < 0 && ! attr->negative_ok)
1067 message_with_line (attr->lineno,
1068 "negative numeric value specified for attribute %s",
1076 if (! strcmp (XSTR (exp, 0), "*"))
1079 if (attr == 0 || attr->is_numeric)
1082 if (attr && attr->negative_ok && *p == '-')
1087 message_with_line (attr ? attr->lineno : 0,
1088 "non-numeric value for numeric attribute %s",
1089 attr ? attr->name : "internal");
1096 for (av = attr->first_value; av; av = av->next)
1097 if (GET_CODE (av->value) == CONST_STRING
1098 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
1103 message_with_line (attr->lineno,
1104 "unknown value `%s' for `%s' attribute",
1105 XSTR (exp, 0), attr ? attr->name : "internal");
1111 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
1112 attr ? attr->is_const : 0,
1113 attr ? attr->lineno : 0);
1114 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1115 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
1123 if (attr && !attr->is_numeric)
1125 message_with_line (attr->lineno,
1126 "invalid operation `%s' for non-numeric attribute value",
1127 GET_RTX_NAME (GET_CODE (exp)));
1135 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1136 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1144 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1148 if (XVECLEN (exp, 0) % 2 != 0)
1150 message_with_line (attr->lineno,
1151 "first operand of COND must have even length");
1156 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1158 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
1159 attr ? attr->is_const : 0,
1160 attr ? attr->lineno : 0);
1161 XVECEXP (exp, 0, i + 1)
1162 = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
1165 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1170 struct attr_desc *attr2 = find_attr (XSTR (exp, 0), 0);
1173 message_with_line (attr ? attr->lineno : 0,
1174 "unknown attribute `%s' in ATTR",
1178 else if (attr && attr->is_const && ! attr2->is_const)
1180 message_with_line (attr->lineno,
1181 "non-constant attribute `%s' referenced from `%s'",
1182 XSTR (exp, 0), attr->name);
1186 && (attr->is_numeric != attr2->is_numeric
1187 || (! attr->negative_ok && attr2->negative_ok)))
1189 message_with_line (attr->lineno,
1190 "numeric attribute mismatch calling `%s' from `%s'",
1191 XSTR (exp, 0), attr->name);
1198 /* A constant SYMBOL_REF is valid as a constant attribute test and
1199 is expanded later by make_canonical into a COND. In a non-constant
1200 attribute test, it is left be. */
1201 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1204 message_with_line (attr ? attr->lineno : 0,
1205 "invalid operation `%s' for attribute value",
1206 GET_RTX_NAME (GET_CODE (exp)));
1214 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1215 It becomes a COND with each test being (eq_attr "alternative "n") */
1218 convert_set_attr_alternative (rtx exp, struct insn_def *id)
1220 int num_alt = id->num_alternatives;
1224 if (XVECLEN (exp, 1) != num_alt)
1226 message_with_line (id->lineno,
1227 "bad number of entries in SET_ATTR_ALTERNATIVE");
1232 /* Make a COND with all tests but the last. Select the last value via the
1234 condexp = rtx_alloc (COND);
1235 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1237 for (i = 0; i < num_alt - 1; i++)
1240 p = attr_numeral (i);
1242 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1243 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1246 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1248 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1251 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1252 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1255 convert_set_attr (rtx exp, struct insn_def *id)
1258 const char *name_ptr;
1262 /* See how many alternative specified. */
1263 n = n_comma_elts (XSTR (exp, 1));
1265 return attr_rtx (SET,
1266 attr_rtx (ATTR, XSTR (exp, 0)),
1267 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1269 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1270 XSTR (newexp, 0) = XSTR (exp, 0);
1271 XVEC (newexp, 1) = rtvec_alloc (n);
1273 /* Process each comma-separated name. */
1274 name_ptr = XSTR (exp, 1);
1276 while ((p = next_comma_elt (&name_ptr)) != NULL)
1277 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1279 return convert_set_attr_alternative (newexp, id);
1282 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1283 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1289 struct insn_def *id;
1290 struct attr_desc *attr;
1294 for (id = defs; id; id = id->next)
1296 if (XVEC (id->def, id->vec_idx) == NULL)
1299 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1301 value = XVECEXP (id->def, id->vec_idx, i);
1302 switch (GET_CODE (value))
1305 if (GET_CODE (XEXP (value, 0)) != ATTR)
1307 message_with_line (id->lineno, "bad attribute set");
1313 case SET_ATTR_ALTERNATIVE:
1314 value = convert_set_attr_alternative (value, id);
1318 value = convert_set_attr (value, id);
1322 message_with_line (id->lineno, "invalid attribute code %s",
1323 GET_RTX_NAME (GET_CODE (value)));
1327 if (value == NULL_RTX)
1330 if ((attr = find_attr (XSTR (XEXP (value, 0), 0), 0)) == NULL)
1332 message_with_line (id->lineno, "unknown attribute %s",
1333 XSTR (XEXP (value, 0), 0));
1338 XVECEXP (id->def, id->vec_idx, i) = value;
1339 XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1344 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1345 expressions by converting them into a COND. This removes cases from this
1346 program. Also, replace an attribute value of "*" with the default attribute
1350 make_canonical (struct attr_desc *attr, rtx exp)
1355 switch (GET_CODE (exp))
1358 exp = make_numeric_value (INTVAL (exp));
1362 if (! strcmp (XSTR (exp, 0), "*"))
1364 if (attr == 0 || attr->default_val == 0)
1365 fatal ("(attr_value \"*\") used in invalid context");
1366 exp = attr->default_val->value;
1372 if (!attr->is_const || ATTR_IND_SIMPLIFIED_P (exp))
1374 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1375 This makes the COND something that won't be considered an arbitrary
1376 expression by walk_attr_value. */
1377 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1378 exp = check_attr_value (exp, attr);
1382 newexp = rtx_alloc (COND);
1383 XVEC (newexp, 0) = rtvec_alloc (2);
1384 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1385 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1387 XEXP (newexp, 1) = XEXP (exp, 2);
1390 /* Fall through to COND case since this is now a COND. */
1397 /* First, check for degenerate COND. */
1398 if (XVECLEN (exp, 0) == 0)
1399 return make_canonical (attr, XEXP (exp, 1));
1400 defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1402 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1404 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1405 XVECEXP (exp, 0, i + 1)
1406 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1407 if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1423 copy_boolean (rtx exp)
1425 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1426 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1427 copy_boolean (XEXP (exp, 1)));
1431 /* Given a value and an attribute description, return a `struct attr_value *'
1432 that represents that value. This is either an existing structure, if the
1433 value has been previously encountered, or a newly-created structure.
1435 `insn_code' is the code of an insn whose attribute has the specified
1436 value (-2 if not processing an insn). We ensure that all insns for
1437 a given value have the same number of alternatives if the value checks
1440 static struct attr_value *
1441 get_attr_value (rtx value, struct attr_desc *attr, int insn_code)
1443 struct attr_value *av;
1446 value = make_canonical (attr, value);
1447 if (compares_alternatives_p (value))
1449 if (insn_code < 0 || insn_alternatives == NULL)
1450 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1452 num_alt = insn_alternatives[insn_code];
1455 for (av = attr->first_value; av; av = av->next)
1456 if (rtx_equal_p (value, av->value)
1457 && (num_alt == 0 || av->first_insn == NULL
1458 || insn_alternatives[av->first_insn->insn_code]))
1461 av = (struct attr_value *) oballoc (sizeof (struct attr_value));
1463 av->next = attr->first_value;
1464 attr->first_value = av;
1465 av->first_insn = NULL;
1467 av->has_asm_insn = 0;
1472 /* After all DEFINE_DELAYs have been read in, create internal attributes
1473 to generate the required routines.
1475 First, we compute the number of delay slots for each insn (as a COND of
1476 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1477 delay type is specified, we compute a similar function giving the
1478 DEFINE_DELAY ordinal for each insn.
1480 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1481 tells whether a given insn can be in that delay slot.
1483 Normal attribute filling and optimization expands these to contain the
1484 information needed to handle delay slots. */
1487 expand_delays (void)
1489 struct delay_desc *delay;
1495 /* First, generate data for `num_delay_slots' function. */
1497 condexp = rtx_alloc (COND);
1498 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1499 XEXP (condexp, 1) = make_numeric_value (0);
1501 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1503 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1504 XVECEXP (condexp, 0, i + 1)
1505 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1508 make_internal_attr ("*num_delay_slots", condexp, ATTR_NONE);
1510 /* If more than one delay type, do the same for computing the delay type. */
1513 condexp = rtx_alloc (COND);
1514 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1515 XEXP (condexp, 1) = make_numeric_value (0);
1517 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1519 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1520 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1523 make_internal_attr ("*delay_type", condexp, ATTR_SPECIAL);
1526 /* For each delay possibility and delay slot, compute an eligibility
1527 attribute for non-annulled insns and for each type of annulled (annul
1528 if true and annul if false). */
1529 for (delay = delays; delay; delay = delay->next)
1531 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1533 condexp = XVECEXP (delay->def, 1, i);
1535 condexp = false_rtx;
1536 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1537 make_numeric_value (1), make_numeric_value (0));
1539 p = attr_printf (sizeof "*delay__" + MAX_DIGITS * 2,
1540 "*delay_%d_%d", delay->num, i / 3);
1541 make_internal_attr (p, newexp, ATTR_SPECIAL);
1543 if (have_annul_true)
1545 condexp = XVECEXP (delay->def, 1, i + 1);
1546 if (condexp == 0) condexp = false_rtx;
1547 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1548 make_numeric_value (1),
1549 make_numeric_value (0));
1550 p = attr_printf (sizeof "*annul_true__" + MAX_DIGITS * 2,
1551 "*annul_true_%d_%d", delay->num, i / 3);
1552 make_internal_attr (p, newexp, ATTR_SPECIAL);
1555 if (have_annul_false)
1557 condexp = XVECEXP (delay->def, 1, i + 2);
1558 if (condexp == 0) condexp = false_rtx;
1559 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1560 make_numeric_value (1),
1561 make_numeric_value (0));
1562 p = attr_printf (sizeof "*annul_false__" + MAX_DIGITS * 2,
1563 "*annul_false_%d_%d", delay->num, i / 3);
1564 make_internal_attr (p, newexp, ATTR_SPECIAL);
1570 /* This function is given a left and right side expression and an operator.
1571 Each side is a conditional expression, each alternative of which has a
1572 numerical value. The function returns another conditional expression
1573 which, for every possible set of condition values, returns a value that is
1574 the operator applied to the values of the two sides.
1576 Since this is called early, it must also support IF_THEN_ELSE. */
1579 operate_exp (enum operator op, rtx left, rtx right)
1581 int left_value, right_value;
1585 /* If left is a string, apply operator to it and the right side. */
1586 if (GET_CODE (left) == CONST_STRING)
1588 /* If right is also a string, just perform the operation. */
1589 if (GET_CODE (right) == CONST_STRING)
1591 left_value = atoi (XSTR (left, 0));
1592 right_value = atoi (XSTR (right, 0));
1596 i = left_value + right_value;
1600 i = left_value - right_value;
1603 case POS_MINUS_OP: /* The positive part of LEFT - RIGHT. */
1604 if (left_value > right_value)
1605 i = left_value - right_value;
1612 i = left_value | right_value;
1616 i = left_value == right_value;
1620 i = (left_value << (HOST_BITS_PER_INT / 2)) | right_value;
1624 if (left_value > right_value)
1631 if (left_value < right_value)
1641 if (i == left_value)
1643 if (i == right_value)
1645 return make_numeric_value (i);
1647 else if (GET_CODE (right) == IF_THEN_ELSE)
1649 /* Apply recursively to all values within. */
1650 rtx newleft = operate_exp (op, left, XEXP (right, 1));
1651 rtx newright = operate_exp (op, left, XEXP (right, 2));
1652 if (rtx_equal_p (newleft, newright))
1654 return attr_rtx (IF_THEN_ELSE, XEXP (right, 0), newleft, newright);
1656 else if (GET_CODE (right) == COND)
1661 newexp = rtx_alloc (COND);
1662 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (right, 0));
1663 defval = XEXP (newexp, 1) = operate_exp (op, left, XEXP (right, 1));
1665 for (i = 0; i < XVECLEN (right, 0); i += 2)
1667 XVECEXP (newexp, 0, i) = XVECEXP (right, 0, i);
1668 XVECEXP (newexp, 0, i + 1)
1669 = operate_exp (op, left, XVECEXP (right, 0, i + 1));
1670 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1675 /* If the resulting cond is trivial (all alternatives
1676 give the same value), optimize it away. */
1678 return operate_exp (op, left, XEXP (right, 1));
1683 fatal ("badly formed attribute value");
1686 /* A hack to prevent expand_units from completely blowing up: ORX_OP does
1687 not associate through IF_THEN_ELSE. */
1688 else if (op == ORX_OP && GET_CODE (right) == IF_THEN_ELSE)
1690 return attr_rtx (IOR, left, right);
1693 /* Otherwise, do recursion the other way. */
1694 else if (GET_CODE (left) == IF_THEN_ELSE)
1696 rtx newleft = operate_exp (op, XEXP (left, 1), right);
1697 rtx newright = operate_exp (op, XEXP (left, 2), right);
1698 if (rtx_equal_p (newleft, newright))
1700 return attr_rtx (IF_THEN_ELSE, XEXP (left, 0), newleft, newright);
1702 else if (GET_CODE (left) == COND)
1707 newexp = rtx_alloc (COND);
1708 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (left, 0));
1709 defval = XEXP (newexp, 1) = operate_exp (op, XEXP (left, 1), right);
1711 for (i = 0; i < XVECLEN (left, 0); i += 2)
1713 XVECEXP (newexp, 0, i) = XVECEXP (left, 0, i);
1714 XVECEXP (newexp, 0, i + 1)
1715 = operate_exp (op, XVECEXP (left, 0, i + 1), right);
1716 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1721 /* If the cond is trivial (all alternatives give the same value),
1722 optimize it away. */
1724 return operate_exp (op, XEXP (left, 1), right);
1726 /* If the result is the same as the LEFT operand,
1728 if (rtx_equal_p (newexp, left))
1735 fatal ("badly formed attribute value");
1740 /* Once all attributes and DEFINE_FUNCTION_UNITs have been read, we
1741 construct a number of attributes.
1743 The first produces a function `function_units_used' which is given an
1744 insn and produces an encoding showing which function units are required
1745 for the execution of that insn. If the value is non-negative, the insn
1746 uses that unit; otherwise, the value is a one's complement mask of units
1749 The second produces a function `result_ready_cost' which is used to
1750 determine the time that the result of an insn will be ready and hence
1751 a worst-case schedule.
1753 Both of these produce quite complex expressions which are then set as the
1754 default value of internal attributes. Normal attribute simplification
1755 should produce reasonable expressions.
1757 For each unit, a `<name>_unit_ready_cost' function will take an
1758 insn and give the delay until that unit will be ready with the result
1759 and a `<name>_unit_conflict_cost' function is given an insn already
1760 executing on the unit and a candidate to execute and will give the
1761 cost from the time the executing insn started until the candidate
1762 can start (ignore limitations on the number of simultaneous insns).
1764 For each unit, a `<name>_unit_blockage' function is given an insn
1765 already executing on the unit and a candidate to execute and will
1766 give the delay incurred due to function unit conflicts. The range of
1767 blockage cost values for a given executing insn is given by the
1768 `<name>_unit_blockage_range' function. These values are encoded in
1769 an int where the upper half gives the minimum value and the lower
1770 half gives the maximum value. */
1775 struct function_unit *unit, **unit_num;
1776 struct function_unit_op *op, **op_array, ***unit_ops;
1781 int i, j, u, num, nvalues;
1783 /* Rebuild the condition for the unit to share the RTL expressions.
1784 Sharing is required by simplify_by_exploding. Build the issue delay
1785 expressions. Validate the expressions we were given for the conditions
1786 and conflict vector. Then make attributes for use in the conflict
1789 for (unit = units; unit; unit = unit->next)
1791 unit->condexp = check_attr_test (unit->condexp, 0, unit->first_lineno);
1793 for (op = unit->ops; op; op = op->next)
1795 rtx issue_delay = make_numeric_value (op->issue_delay);
1796 rtx issue_exp = issue_delay;
1798 /* Build, validate, and simplify the issue delay expression. */
1799 if (op->conflict_exp != true_rtx)
1800 issue_exp = attr_rtx (IF_THEN_ELSE, op->conflict_exp,
1801 issue_exp, make_numeric_value (0));
1802 issue_exp = check_attr_value (make_canonical (NULL_ATTR,
1805 issue_exp = simplify_knowing (issue_exp, unit->condexp);
1806 op->issue_exp = issue_exp;
1808 /* Make an attribute for use in the conflict function if needed. */
1809 unit->needs_conflict_function = (unit->issue_delay.min
1810 != unit->issue_delay.max);
1811 if (unit->needs_conflict_function)
1813 str = attr_printf ((strlen (unit->name) + sizeof "*_cost_"
1815 "*%s_cost_%d", unit->name, op->num);
1816 make_internal_attr (str, issue_exp, ATTR_SPECIAL);
1819 /* Validate the condition. */
1820 op->condexp = check_attr_test (op->condexp, 0, op->lineno);
1824 /* Compute the mask of function units used. Initially, the unitsmask is
1825 zero. Set up a conditional to compute each unit's contribution. */
1826 unitsmask = make_numeric_value (0);
1827 newexp = rtx_alloc (IF_THEN_ELSE);
1828 XEXP (newexp, 2) = make_numeric_value (0);
1830 /* If we have just a few units, we may be all right expanding the whole
1831 thing. But the expansion is 2**N in space on the number of opclasses,
1832 so we can't do this for very long -- Alpha and MIPS in particular have
1833 problems with this. So in that situation, we fall back on an alternate
1834 implementation method. */
1835 #define NUM_UNITOP_CUTOFF 20
1837 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1839 /* Merge each function unit into the unit mask attributes. */
1840 for (unit = units; unit; unit = unit->next)
1842 XEXP (newexp, 0) = unit->condexp;
1843 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1844 unitsmask = operate_exp (OR_OP, unitsmask, newexp);
1849 /* Merge each function unit into the unit mask attributes. */
1850 for (unit = units; unit; unit = unit->next)
1852 XEXP (newexp, 0) = unit->condexp;
1853 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1854 unitsmask = operate_exp (ORX_OP, unitsmask, attr_copy_rtx (newexp));
1858 /* Simplify the unit mask expression, encode it, and make an attribute
1859 for the function_units_used function. */
1860 unitsmask = simplify_by_exploding (unitsmask);
1862 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1863 unitsmask = encode_units_mask (unitsmask);
1866 /* We can no longer encode unitsmask at compile time, so emit code to
1867 calculate it at runtime. Rather, put a marker for where we'd do
1868 the code, and actually output it in write_attr_get(). */
1869 unitsmask = attr_rtx (FFS, unitsmask);
1872 make_internal_attr ("*function_units_used", unitsmask,
1873 (ATTR_NEGATIVE_OK | ATTR_FUNC_UNITS));
1875 /* Create an array of ops for each unit. Add an extra unit for the
1876 result_ready_cost function that has the ops of all other units. */
1877 unit_ops = (struct function_unit_op ***)
1878 xmalloc ((num_units + 1) * sizeof (struct function_unit_op **));
1879 unit_num = (struct function_unit **)
1880 xmalloc ((num_units + 1) * sizeof (struct function_unit *));
1882 unit_num[num_units] = unit = (struct function_unit *)
1883 xmalloc (sizeof (struct function_unit));
1884 unit->num = num_units;
1885 unit->num_opclasses = 0;
1887 for (unit = units; unit; unit = unit->next)
1889 unit_num[num_units]->num_opclasses += unit->num_opclasses;
1890 unit_num[unit->num] = unit;
1891 unit_ops[unit->num] = op_array = (struct function_unit_op **)
1892 xmalloc (unit->num_opclasses * sizeof (struct function_unit_op *));
1894 for (op = unit->ops; op; op = op->next)
1895 op_array[op->num] = op;
1898 /* Compose the array of ops for the extra unit. */
1899 unit_ops[num_units] = op_array = (struct function_unit_op **)
1900 xmalloc (unit_num[num_units]->num_opclasses
1901 * sizeof (struct function_unit_op *));
1903 for (unit = units, i = 0; unit; i += unit->num_opclasses, unit = unit->next)
1904 memcpy (&op_array[i], unit_ops[unit->num],
1905 unit->num_opclasses * sizeof (struct function_unit_op *));
1907 /* Compute the ready cost function for each unit by computing the
1908 condition for each non-default value. */
1909 for (u = 0; u <= num_units; u++)
1915 op_array = unit_ops[unit->num];
1916 num = unit->num_opclasses;
1918 /* Sort the array of ops into increasing ready cost order. */
1919 for (i = 0; i < num; i++)
1920 for (j = num - 1; j > i; j--)
1921 if (op_array[j - 1]->ready < op_array[j]->ready)
1924 op_array[j] = op_array[j - 1];
1925 op_array[j - 1] = op;
1928 /* Determine how many distinct non-default ready cost values there
1929 are. We use a default ready cost value of 1. */
1930 nvalues = 0; value = 1;
1931 for (i = num - 1; i >= 0; i--)
1932 if (op_array[i]->ready > value)
1934 value = op_array[i]->ready;
1939 readycost = make_numeric_value (1);
1942 /* Construct the ready cost expression as a COND of each value from
1943 the largest to the smallest. */
1944 readycost = rtx_alloc (COND);
1945 XVEC (readycost, 0) = rtvec_alloc (nvalues * 2);
1946 XEXP (readycost, 1) = make_numeric_value (1);
1950 value = op_array[0]->ready;
1951 for (i = 0; i < num; i++)
1956 else if (op->ready == value)
1957 orexp = insert_right_side (IOR, orexp, op->condexp, -2, -2);
1960 XVECEXP (readycost, 0, nvalues * 2) = orexp;
1961 XVECEXP (readycost, 0, nvalues * 2 + 1)
1962 = make_numeric_value (value);
1965 orexp = op->condexp;
1968 XVECEXP (readycost, 0, nvalues * 2) = orexp;
1969 XVECEXP (readycost, 0, nvalues * 2 + 1) = make_numeric_value (value);
1974 rtx max_blockage = 0, min_blockage = 0;
1976 /* Simplify the readycost expression by only considering insns
1977 that use the unit. */
1978 readycost = simplify_knowing (readycost, unit->condexp);
1980 /* Determine the blockage cost the executing insn (E) given
1981 the candidate insn (C). This is the maximum of the issue
1982 delay, the pipeline delay, and the simultaneity constraint.
1983 Each function_unit_op represents the characteristics of the
1984 candidate insn, so in the expressions below, C is a known
1985 term and E is an unknown term.
1987 We compute the blockage cost for each E for every possible C.
1988 Thus OP represents E, and READYCOST is a list of values for
1991 The issue delay function for C is op->issue_exp and is used to
1992 write the `<name>_unit_conflict_cost' function. Symbolically
1993 this is "ISSUE-DELAY (E,C)".
1995 The pipeline delay results form the FIFO constraint on the
1996 function unit and is "READY-COST (E) + 1 - READY-COST (C)".
1998 The simultaneity constraint is based on how long it takes to
1999 fill the unit given the minimum issue delay. FILL-TIME is the
2000 constant "MIN (ISSUE-DELAY (*,*)) * (SIMULTANEITY - 1)", and
2001 the simultaneity constraint is "READY-COST (E) - FILL-TIME"
2002 if SIMULTANEITY is nonzero and zero otherwise.
2004 Thus, BLOCKAGE (E,C) when SIMULTANEITY is zero is
2006 MAX (ISSUE-DELAY (E,C),
2007 READY-COST (E) - (READY-COST (C) - 1))
2011 MAX (ISSUE-DELAY (E,C),
2012 READY-COST (E) - (READY-COST (C) - 1),
2013 READY-COST (E) - FILL-TIME)
2015 The `<name>_unit_blockage' function is computed by determining
2016 this value for each candidate insn. As these values are
2017 computed, we also compute the upper and lower bounds for
2018 BLOCKAGE (E,*). These are combined to form the function
2019 `<name>_unit_blockage_range'. Finally, the maximum blockage
2020 cost, MAX (BLOCKAGE (*,*)), is computed. */
2022 for (op = unit->ops; op; op = op->next)
2024 rtx blockage = op->issue_exp;
2025 blockage = simplify_knowing (blockage, unit->condexp);
2027 /* Add this op's contribution to MAX (BLOCKAGE (E,*)) and
2028 MIN (BLOCKAGE (E,*)). */
2029 if (max_blockage == 0)
2030 max_blockage = min_blockage = blockage;
2034 = simplify_knowing (operate_exp (MAX_OP, max_blockage,
2038 = simplify_knowing (operate_exp (MIN_OP, min_blockage,
2043 /* Make an attribute for use in the blockage function. */
2044 str = attr_printf ((strlen (unit->name) + sizeof "*_block_"
2046 "*%s_block_%d", unit->name, op->num);
2047 make_internal_attr (str, blockage, ATTR_SPECIAL);
2050 /* Record MAX (BLOCKAGE (*,*)). */
2053 unit->max_blockage = max_attr_value (max_blockage, &unknown);
2056 /* See if the upper and lower bounds of BLOCKAGE (E,*) are the
2057 same. If so, the blockage function carries no additional
2058 information and is not written. */
2059 newexp = operate_exp (EQ_OP, max_blockage, min_blockage);
2060 newexp = simplify_knowing (newexp, unit->condexp);
2061 unit->needs_blockage_function
2062 = (GET_CODE (newexp) != CONST_STRING
2063 || atoi (XSTR (newexp, 0)) != 1);
2065 /* If the all values of BLOCKAGE (E,C) have the same value,
2066 neither blockage function is written. */
2067 unit->needs_range_function
2068 = (unit->needs_blockage_function
2069 || GET_CODE (max_blockage) != CONST_STRING);
2071 if (unit->needs_range_function)
2073 /* Compute the blockage range function and make an attribute
2074 for writing its value. */
2075 newexp = operate_exp (RANGE_OP, min_blockage, max_blockage);
2076 newexp = simplify_knowing (newexp, unit->condexp);
2078 str = attr_printf ((strlen (unit->name)
2079 + sizeof "*_unit_blockage_range"),
2080 "*%s_unit_blockage_range", unit->name);
2081 make_internal_attr (str, newexp, (ATTR_BLOCKAGE|ATTR_UNSIGNED));
2084 str = attr_printf (strlen (unit->name) + sizeof "*_unit_ready_cost",
2085 "*%s_unit_ready_cost", unit->name);
2088 str = "*result_ready_cost";
2090 /* Make an attribute for the ready_cost function. Simplifying
2091 further with simplify_by_exploding doesn't win. */
2092 make_internal_attr (str, readycost, ATTR_NONE);
2095 /* For each unit that requires a conflict cost function, make an attribute
2096 that maps insns to the operation number. */
2097 for (unit = units; unit; unit = unit->next)
2101 if (! unit->needs_conflict_function
2102 && ! unit->needs_blockage_function)
2105 caseexp = rtx_alloc (COND);
2106 XVEC (caseexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
2108 for (op = unit->ops; op; op = op->next)
2110 /* Make our adjustment to the COND being computed. If we are the
2111 last operation class, place our values into the default of the
2113 if (op->num == unit->num_opclasses - 1)
2115 XEXP (caseexp, 1) = make_numeric_value (op->num);
2119 XVECEXP (caseexp, 0, op->num * 2) = op->condexp;
2120 XVECEXP (caseexp, 0, op->num * 2 + 1)
2121 = make_numeric_value (op->num);
2125 /* Simplifying caseexp with simplify_by_exploding doesn't win. */
2126 str = attr_printf (strlen (unit->name) + sizeof "*_cases",
2127 "*%s_cases", unit->name);
2128 make_internal_attr (str, caseexp, ATTR_SPECIAL);
2132 /* Simplify EXP given KNOWN_TRUE. */
2135 simplify_knowing (rtx exp, rtx known_true)
2137 if (GET_CODE (exp) != CONST_STRING)
2139 int unknown = 0, max;
2140 max = max_attr_value (exp, &unknown);
2143 exp = attr_rtx (IF_THEN_ELSE, known_true, exp,
2144 make_numeric_value (max));
2145 exp = simplify_by_exploding (exp);
2151 /* Translate the CONST_STRING expressions in X to change the encoding of
2152 value. On input, the value is a bitmask with a one bit for each unit
2153 used; on output, the value is the unit number (zero based) if one
2154 and only one unit is used or the one's complement of the bitmask. */
2157 encode_units_mask (rtx x)
2164 code = GET_CODE (x);
2169 i = atoi (XSTR (x, 0));
2171 /* The sign bit encodes a one's complement mask. */
2173 else if (i != 0 && i == (i & -i))
2174 /* Only one bit is set, so yield that unit number. */
2175 for (j = 0; (i >>= 1) != 0; j++)
2179 return attr_rtx (CONST_STRING, attr_printf (MAX_DIGITS, "%d", j));
2197 /* Compare the elements. If any pair of corresponding elements
2198 fail to match, return 0 for the whole things. */
2200 fmt = GET_RTX_FORMAT (code);
2201 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2207 for (j = 0; j < XVECLEN (x, i); j++)
2208 XVECEXP (x, i, j) = encode_units_mask (XVECEXP (x, i, j));
2212 XEXP (x, i) = encode_units_mask (XEXP (x, i));
2219 /* Once all attributes and insns have been read and checked, we construct for
2220 each attribute value a list of all the insns that have that value for
2224 fill_attr (struct attr_desc *attr)
2226 struct attr_value *av;
2227 struct insn_ent *ie;
2228 struct insn_def *id;
2232 /* Don't fill constant attributes. The value is independent of
2233 any particular insn. */
2237 for (id = defs; id; id = id->next)
2239 /* If no value is specified for this insn for this attribute, use the
2242 if (XVEC (id->def, id->vec_idx))
2243 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
2244 if (! strcmp (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
2246 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
2249 av = attr->default_val;
2251 av = get_attr_value (value, attr, id->insn_code);
2253 ie = (struct insn_ent *) oballoc (sizeof (struct insn_ent));
2254 ie->insn_code = id->insn_code;
2255 ie->insn_index = id->insn_code;
2256 insert_insn_ent (av, ie);
2260 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
2261 test that checks relative positions of insns (uses MATCH_DUP or PC).
2262 If so, replace it with what is obtained by passing the expression to
2263 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
2264 recursively on each value (including the default value). Otherwise,
2265 return the value returned by NO_ADDRESS_FN applied to EXP. */
2268 substitute_address (rtx exp, rtx (*no_address_fn) (rtx),
2269 rtx (*address_fn) (rtx))
2274 if (GET_CODE (exp) == COND)
2276 /* See if any tests use addresses. */
2278 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2279 walk_attr_value (XVECEXP (exp, 0, i));
2282 return (*address_fn) (exp);
2284 /* Make a new copy of this COND, replacing each element. */
2285 newexp = rtx_alloc (COND);
2286 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
2287 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2289 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
2290 XVECEXP (newexp, 0, i + 1)
2291 = substitute_address (XVECEXP (exp, 0, i + 1),
2292 no_address_fn, address_fn);
2295 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
2296 no_address_fn, address_fn);
2301 else if (GET_CODE (exp) == IF_THEN_ELSE)
2304 walk_attr_value (XEXP (exp, 0));
2306 return (*address_fn) (exp);
2308 return attr_rtx (IF_THEN_ELSE,
2309 substitute_address (XEXP (exp, 0),
2310 no_address_fn, address_fn),
2311 substitute_address (XEXP (exp, 1),
2312 no_address_fn, address_fn),
2313 substitute_address (XEXP (exp, 2),
2314 no_address_fn, address_fn));
2317 return (*no_address_fn) (exp);
2320 /* Make new attributes from the `length' attribute. The following are made,
2321 each corresponding to a function called from `shorten_branches' or
2324 *insn_default_length This is the length of the insn to be returned
2325 by `get_attr_length' before `shorten_branches'
2326 has been called. In each case where the length
2327 depends on relative addresses, the largest
2328 possible is used. This routine is also used
2329 to compute the initial size of the insn.
2331 *insn_variable_length_p This returns 1 if the insn's length depends
2332 on relative addresses, zero otherwise.
2334 *insn_current_length This is only called when it is known that the
2335 insn has a variable length and returns the
2336 current length, based on relative addresses.
2340 make_length_attrs (void)
2342 static const char *const new_names[] = {"*insn_default_length",
2343 "*insn_variable_length_p",
2344 "*insn_current_length"};
2345 static rtx (*const no_address_fn[]) (rtx) = {identity_fn, zero_fn, zero_fn};
2346 static rtx (*const address_fn[]) (rtx) = {max_fn, one_fn, identity_fn};
2348 struct attr_desc *length_attr, *new_attr;
2349 struct attr_value *av, *new_av;
2350 struct insn_ent *ie, *new_ie;
2352 /* See if length attribute is defined. If so, it must be numeric. Make
2353 it special so we don't output anything for it. */
2354 length_attr = find_attr ("length", 0);
2355 if (length_attr == 0)
2358 if (! length_attr->is_numeric)
2359 fatal ("length attribute must be numeric");
2361 length_attr->is_const = 0;
2362 length_attr->is_special = 1;
2364 /* Make each new attribute, in turn. */
2365 for (i = 0; i < ARRAY_SIZE (new_names); i++)
2367 make_internal_attr (new_names[i],
2368 substitute_address (length_attr->default_val->value,
2369 no_address_fn[i], address_fn[i]),
2371 new_attr = find_attr (new_names[i], 0);
2372 for (av = length_attr->first_value; av; av = av->next)
2373 for (ie = av->first_insn; ie; ie = ie->next)
2375 new_av = get_attr_value (substitute_address (av->value,
2378 new_attr, ie->insn_code);
2379 new_ie = (struct insn_ent *) oballoc (sizeof (struct insn_ent));
2380 new_ie->insn_code = ie->insn_code;
2381 new_ie->insn_index = ie->insn_index;
2382 insert_insn_ent (new_av, new_ie);
2387 /* Utility functions called from above routine. */
2390 identity_fn (rtx exp)
2396 zero_fn (rtx exp ATTRIBUTE_UNUSED)
2398 return make_numeric_value (0);
2402 one_fn (rtx exp ATTRIBUTE_UNUSED)
2404 return make_numeric_value (1);
2411 return make_numeric_value (max_attr_value (exp, &unknown));
2415 write_length_unit_log (void)
2417 struct attr_desc *length_attr = find_attr ("length", 0);
2418 struct attr_value *av;
2419 struct insn_ent *ie;
2420 unsigned int length_unit_log, length_or;
2423 if (length_attr == 0)
2425 length_or = or_attr_value (length_attr->default_val->value, &unknown);
2426 for (av = length_attr->first_value; av; av = av->next)
2427 for (ie = av->first_insn; ie; ie = ie->next)
2428 length_or |= or_attr_value (av->value, &unknown);
2431 length_unit_log = 0;
2434 length_or = ~length_or;
2435 for (length_unit_log = 0; length_or & 1; length_or >>= 1)
2438 printf ("int length_unit_log = %u;\n", length_unit_log);
2441 /* Take a COND expression and see if any of the conditions in it can be
2442 simplified. If any are known true or known false for the particular insn
2443 code, the COND can be further simplified.
2445 Also call ourselves on any COND operations that are values of this COND.
2447 We do not modify EXP; rather, we make and return a new rtx. */
2450 simplify_cond (rtx exp, int insn_code, int insn_index)
2453 /* We store the desired contents here,
2454 then build a new expression if they don't match EXP. */
2455 rtx defval = XEXP (exp, 1);
2456 rtx new_defval = XEXP (exp, 1);
2457 int len = XVECLEN (exp, 0);
2458 rtx *tests = (rtx *) xmalloc (len * sizeof (rtx));
2462 /* This lets us free all storage allocated below, if appropriate. */
2463 obstack_finish (rtl_obstack);
2465 memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
2467 /* See if default value needs simplification. */
2468 if (GET_CODE (defval) == COND)
2469 new_defval = simplify_cond (defval, insn_code, insn_index);
2471 /* Simplify the subexpressions, and see what tests we can get rid of. */
2473 for (i = 0; i < len; i += 2)
2475 rtx newtest, newval;
2477 /* Simplify this test. */
2478 newtest = simplify_test_exp_in_temp (tests[i], insn_code, insn_index);
2481 newval = tests[i + 1];
2482 /* See if this value may need simplification. */
2483 if (GET_CODE (newval) == COND)
2484 newval = simplify_cond (newval, insn_code, insn_index);
2486 /* Look for ways to delete or combine this test. */
2487 if (newtest == true_rtx)
2489 /* If test is true, make this value the default
2490 and discard this + any following tests. */
2492 defval = tests[i + 1];
2493 new_defval = newval;
2496 else if (newtest == false_rtx)
2498 /* If test is false, discard it and its value. */
2499 for (j = i; j < len - 2; j++)
2500 tests[j] = tests[j + 2];
2504 else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
2506 /* If this value and the value for the prev test are the same,
2510 = insert_right_side (IOR, tests[i - 2], newtest,
2511 insn_code, insn_index);
2513 /* Delete this test/value. */
2514 for (j = i; j < len - 2; j++)
2515 tests[j] = tests[j + 2];
2520 tests[i + 1] = newval;
2523 /* If the last test in a COND has the same value
2524 as the default value, that test isn't needed. */
2526 while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
2529 /* See if we changed anything. */
2530 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
2533 for (i = 0; i < len; i++)
2534 if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
2542 if (GET_CODE (defval) == COND)
2543 ret = simplify_cond (defval, insn_code, insn_index);
2551 rtx newexp = rtx_alloc (COND);
2553 XVEC (newexp, 0) = rtvec_alloc (len);
2554 memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
2555 XEXP (newexp, 1) = new_defval;
2562 /* Remove an insn entry from an attribute value. */
2565 remove_insn_ent (struct attr_value *av, struct insn_ent *ie)
2567 struct insn_ent *previe;
2569 if (av->first_insn == ie)
2570 av->first_insn = ie->next;
2573 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
2575 previe->next = ie->next;
2579 if (ie->insn_code == -1)
2580 av->has_asm_insn = 0;
2585 /* Insert an insn entry in an attribute value list. */
2588 insert_insn_ent (struct attr_value *av, struct insn_ent *ie)
2590 ie->next = av->first_insn;
2591 av->first_insn = ie;
2593 if (ie->insn_code == -1)
2594 av->has_asm_insn = 1;
2599 /* This is a utility routine to take an expression that is a tree of either
2600 AND or IOR expressions and insert a new term. The new term will be
2601 inserted at the right side of the first node whose code does not match
2602 the root. A new node will be created with the root's code. Its left
2603 side will be the old right side and its right side will be the new
2606 If the `term' is itself a tree, all its leaves will be inserted. */
2609 insert_right_side (enum rtx_code code, rtx exp, rtx term, int insn_code, int insn_index)
2613 /* Avoid consing in some special cases. */
2614 if (code == AND && term == true_rtx)
2616 if (code == AND && term == false_rtx)
2618 if (code == AND && exp == true_rtx)
2620 if (code == AND && exp == false_rtx)
2622 if (code == IOR && term == true_rtx)
2624 if (code == IOR && term == false_rtx)
2626 if (code == IOR && exp == true_rtx)
2628 if (code == IOR && exp == false_rtx)
2630 if (attr_equal_p (exp, term))
2633 if (GET_CODE (term) == code)
2635 exp = insert_right_side (code, exp, XEXP (term, 0),
2636 insn_code, insn_index);
2637 exp = insert_right_side (code, exp, XEXP (term, 1),
2638 insn_code, insn_index);
2643 if (GET_CODE (exp) == code)
2645 rtx new = insert_right_side (code, XEXP (exp, 1),
2646 term, insn_code, insn_index);
2647 if (new != XEXP (exp, 1))
2648 /* Make a copy of this expression and call recursively. */
2649 newexp = attr_rtx (code, XEXP (exp, 0), new);
2655 /* Insert the new term. */
2656 newexp = attr_rtx (code, exp, term);
2659 return simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2662 /* If we have an expression which AND's a bunch of
2663 (not (eq_attrq "alternative" "n"))
2664 terms, we may have covered all or all but one of the possible alternatives.
2665 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
2667 This routine is passed an expression and either AND or IOR. It returns a
2668 bitmask indicating which alternatives are mentioned within EXP. */
2671 compute_alternative_mask (rtx exp, enum rtx_code code)
2674 if (GET_CODE (exp) == code)
2675 return compute_alternative_mask (XEXP (exp, 0), code)
2676 | compute_alternative_mask (XEXP (exp, 1), code);
2678 else if (code == AND && GET_CODE (exp) == NOT
2679 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2680 && XSTR (XEXP (exp, 0), 0) == alternative_name)
2681 string = XSTR (XEXP (exp, 0), 1);
2683 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
2684 && XSTR (exp, 0) == alternative_name)
2685 string = XSTR (exp, 1);
2691 return 1 << (string[0] - '0');
2692 return 1 << atoi (string);
2695 /* Given I, a single-bit mask, return RTX to compare the `alternative'
2696 attribute with the value represented by that bit. */
2699 make_alternative_compare (int mask)
2705 for (i = 0; (mask & (1 << i)) == 0; i++)
2708 newexp = attr_rtx (EQ_ATTR, alternative_name, attr_numeral (i));
2709 ATTR_IND_SIMPLIFIED_P (newexp) = 1;
2714 /* If we are processing an (eq_attr "attr" "value") test, we find the value
2715 of "attr" for this insn code. From that value, we can compute a test
2716 showing when the EQ_ATTR will be true. This routine performs that
2717 computation. If a test condition involves an address, we leave the EQ_ATTR
2718 intact because addresses are only valid for the `length' attribute.
2720 EXP is the EQ_ATTR expression and VALUE is the value of that attribute
2721 for the insn corresponding to INSN_CODE and INSN_INDEX. */
2724 evaluate_eq_attr (rtx exp, rtx value, int insn_code, int insn_index)
2731 if (GET_CODE (value) == CONST_STRING)
2733 if (! strcmp (XSTR (value, 0), XSTR (exp, 1)))
2738 else if (GET_CODE (value) == SYMBOL_REF)
2743 if (GET_CODE (exp) != EQ_ATTR)
2746 if (strlen (XSTR (exp, 0)) + strlen (XSTR (exp, 1)) + 2 > 256)
2749 strcpy (string, XSTR (exp, 0));
2750 strcat (string, "_");
2751 strcat (string, XSTR (exp, 1));
2752 for (p = string; *p; p++)
2755 newexp = attr_rtx (EQ, value,
2756 attr_rtx (SYMBOL_REF,
2757 attr_string (string, strlen (string))));
2759 else if (GET_CODE (value) == COND)
2761 /* We construct an IOR of all the cases for which the requested attribute
2762 value is present. Since we start with FALSE, if it is not present,
2763 FALSE will be returned.
2765 Each case is the AND of the NOT's of the previous conditions with the
2766 current condition; in the default case the current condition is TRUE.
2768 For each possible COND value, call ourselves recursively.
2770 The extra TRUE and FALSE expressions will be eliminated by another
2771 call to the simplification routine. */
2776 if (current_alternative_string)
2777 clear_struct_flag (value);
2779 for (i = 0; i < XVECLEN (value, 0); i += 2)
2781 rtx this = simplify_test_exp_in_temp (XVECEXP (value, 0, i),
2782 insn_code, insn_index);
2784 SIMPLIFY_ALTERNATIVE (this);
2786 right = insert_right_side (AND, andexp, this,
2787 insn_code, insn_index);
2788 right = insert_right_side (AND, right,
2789 evaluate_eq_attr (exp,
2792 insn_code, insn_index),
2793 insn_code, insn_index);
2794 orexp = insert_right_side (IOR, orexp, right,
2795 insn_code, insn_index);
2797 /* Add this condition into the AND expression. */
2798 newexp = attr_rtx (NOT, this);
2799 andexp = insert_right_side (AND, andexp, newexp,
2800 insn_code, insn_index);
2803 /* Handle the default case. */
2804 right = insert_right_side (AND, andexp,
2805 evaluate_eq_attr (exp, XEXP (value, 1),
2806 insn_code, insn_index),
2807 insn_code, insn_index);
2808 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2813 /* If uses an address, must return original expression. But set the
2814 ATTR_IND_SIMPLIFIED_P bit so we don't try to simplify it again. */
2817 walk_attr_value (newexp);
2821 /* This had `&& current_alternative_string', which seems to be wrong. */
2822 if (! ATTR_IND_SIMPLIFIED_P (exp))
2823 return copy_rtx_unchanging (exp);
2830 /* This routine is called when an AND of a term with a tree of AND's is
2831 encountered. If the term or its complement is present in the tree, it
2832 can be replaced with TRUE or FALSE, respectively.
2834 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2835 be true and hence are complementary.
2837 There is one special case: If we see
2838 (and (not (eq_attr "att" "v1"))
2839 (eq_attr "att" "v2"))
2840 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2841 replace the term, not anything in the AND tree. So we pass a pointer to
2845 simplify_and_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2850 int left_eliminates_term, right_eliminates_term;
2852 if (GET_CODE (exp) == AND)
2854 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2855 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2856 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2858 newexp = attr_rtx (GET_CODE (exp), left, right);
2860 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2864 else if (GET_CODE (exp) == IOR)
2866 /* For the IOR case, we do the same as above, except that we can
2867 only eliminate `term' if both sides of the IOR would do so. */
2869 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2870 left_eliminates_term = (temp == true_rtx);
2873 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2874 right_eliminates_term = (temp == true_rtx);
2876 if (left_eliminates_term && right_eliminates_term)
2879 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2881 newexp = attr_rtx (GET_CODE (exp), left, right);
2883 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2887 /* Check for simplifications. Do some extra checking here since this
2888 routine is called so many times. */
2893 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2896 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2899 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2901 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2904 if (! strcmp (XSTR (exp, 1), XSTR (*pterm, 1)))
2910 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2911 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2913 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2916 if (! strcmp (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2922 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2923 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2925 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2928 if (! strcmp (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2934 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2936 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2940 else if (GET_CODE (exp) == NOT)
2942 if (attr_equal_p (XEXP (exp, 0), *pterm))
2946 else if (GET_CODE (*pterm) == NOT)
2948 if (attr_equal_p (XEXP (*pterm, 0), exp))
2952 else if (attr_equal_p (exp, *pterm))
2958 /* Similar to `simplify_and_tree', but for IOR trees. */
2961 simplify_or_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2966 int left_eliminates_term, right_eliminates_term;
2968 if (GET_CODE (exp) == IOR)
2970 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2971 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2972 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2974 newexp = attr_rtx (GET_CODE (exp), left, right);
2976 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2980 else if (GET_CODE (exp) == AND)
2982 /* For the AND case, we do the same as above, except that we can
2983 only eliminate `term' if both sides of the AND would do so. */
2985 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2986 left_eliminates_term = (temp == false_rtx);
2989 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2990 right_eliminates_term = (temp == false_rtx);
2992 if (left_eliminates_term && right_eliminates_term)
2995 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2997 newexp = attr_rtx (GET_CODE (exp), left, right);
2999 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
3003 if (attr_equal_p (exp, *pterm))
3006 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
3009 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
3012 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
3013 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3014 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
3017 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
3018 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
3019 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
3025 /* Compute approximate cost of the expression. Used to decide whether
3026 expression is cheap enough for inline. */
3028 attr_rtx_cost (rtx x)
3034 code = GET_CODE (x);
3043 /* Alternatives don't result into function call. */
3044 if (!strcmp (XSTR (x, 0), "alternative"))
3051 const char *fmt = GET_RTX_FORMAT (code);
3052 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3058 for (j = 0; j < XVECLEN (x, i); j++)
3059 cost += attr_rtx_cost (XVECEXP (x, i, j));
3062 cost += attr_rtx_cost (XEXP (x, i));
3072 /* Simplify test expression and use temporary obstack in order to avoid
3073 memory bloat. Use ATTR_IND_SIMPLIFIED to avoid unnecessary simplifications
3074 and avoid unnecessary copying if possible. */
3077 simplify_test_exp_in_temp (rtx exp, int insn_code, int insn_index)
3080 struct obstack *old;
3081 if (ATTR_IND_SIMPLIFIED_P (exp))
3084 rtl_obstack = temp_obstack;
3085 x = simplify_test_exp (exp, insn_code, insn_index);
3087 if (x == exp || rtl_obstack == temp_obstack)
3089 return attr_copy_rtx (x);
3092 /* Given an expression, see if it can be simplified for a particular insn
3093 code based on the values of other attributes being tested. This can
3094 eliminate nested get_attr_... calls.
3096 Note that if an endless recursion is specified in the patterns, the
3097 optimization will loop. However, it will do so in precisely the cases where
3098 an infinite recursion loop could occur during compilation. It's better that
3102 simplify_test_exp (rtx exp, int insn_code, int insn_index)
3105 struct attr_desc *attr;
3106 struct attr_value *av;
3107 struct insn_ent *ie;
3111 /* Don't re-simplify something we already simplified. */
3112 if (ATTR_IND_SIMPLIFIED_P (exp) || ATTR_CURR_SIMPLIFIED_P (exp))
3115 switch (GET_CODE (exp))
3118 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3119 SIMPLIFY_ALTERNATIVE (left);
3120 if (left == false_rtx)
3122 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3123 SIMPLIFY_ALTERNATIVE (right);
3124 if (left == false_rtx)
3127 /* If either side is an IOR and we have (eq_attr "alternative" ..")
3128 present on both sides, apply the distributive law since this will
3129 yield simplifications. */
3130 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
3131 && compute_alternative_mask (left, IOR)
3132 && compute_alternative_mask (right, IOR))
3134 if (GET_CODE (left) == IOR)
3141 newexp = attr_rtx (IOR,
3142 attr_rtx (AND, left, XEXP (right, 0)),
3143 attr_rtx (AND, left, XEXP (right, 1)));
3145 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3148 /* Try with the term on both sides. */
3149 right = simplify_and_tree (right, &left, insn_code, insn_index);
3150 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3151 left = simplify_and_tree (left, &right, insn_code, insn_index);
3153 if (left == false_rtx || right == false_rtx)
3155 else if (left == true_rtx)
3159 else if (right == true_rtx)
3163 /* See if all or all but one of the insn's alternatives are specified
3164 in this tree. Optimize if so. */
3166 else if (insn_code >= 0
3167 && (GET_CODE (left) == AND
3168 || (GET_CODE (left) == NOT
3169 && GET_CODE (XEXP (left, 0)) == EQ_ATTR
3170 && XSTR (XEXP (left, 0), 0) == alternative_name)
3171 || GET_CODE (right) == AND
3172 || (GET_CODE (right) == NOT
3173 && GET_CODE (XEXP (right, 0)) == EQ_ATTR
3174 && XSTR (XEXP (right, 0), 0) == alternative_name)))
3176 i = compute_alternative_mask (exp, AND);
3177 if (i & ~insn_alternatives[insn_code])
3178 fatal ("invalid alternative specified for pattern number %d",
3181 /* If all alternatives are excluded, this is false. */
3182 i ^= insn_alternatives[insn_code];
3185 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3187 /* If just one excluded, AND a comparison with that one to the
3188 front of the tree. The others will be eliminated by
3189 optimization. We do not want to do this if the insn has one
3190 alternative and we have tested none of them! */
3191 left = make_alternative_compare (i);
3192 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3193 newexp = attr_rtx (AND, left, right);
3195 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3199 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3201 newexp = attr_rtx (AND, left, right);
3202 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3207 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3208 SIMPLIFY_ALTERNATIVE (left);
3209 if (left == true_rtx)
3211 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3212 SIMPLIFY_ALTERNATIVE (right);
3213 if (right == true_rtx)
3216 right = simplify_or_tree (right, &left, insn_code, insn_index);
3217 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3218 left = simplify_or_tree (left, &right, insn_code, insn_index);
3220 if (right == true_rtx || left == true_rtx)
3222 else if (left == false_rtx)
3226 else if (right == false_rtx)
3231 /* Test for simple cases where the distributive law is useful. I.e.,
3232 convert (ior (and (x) (y))
3238 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
3239 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
3241 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
3243 left = XEXP (left, 0);
3245 newexp = attr_rtx (AND, left, right);
3246 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3249 /* See if all or all but one of the insn's alternatives are specified
3250 in this tree. Optimize if so. */
3252 else if (insn_code >= 0
3253 && (GET_CODE (left) == IOR
3254 || (GET_CODE (left) == EQ_ATTR
3255 && XSTR (left, 0) == alternative_name)
3256 || GET_CODE (right) == IOR
3257 || (GET_CODE (right) == EQ_ATTR
3258 && XSTR (right, 0) == alternative_name)))
3260 i = compute_alternative_mask (exp, IOR);
3261 if (i & ~insn_alternatives[insn_code])
3262 fatal ("invalid alternative specified for pattern number %d",
3265 /* If all alternatives are included, this is true. */
3266 i ^= insn_alternatives[insn_code];
3269 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3271 /* If just one excluded, IOR a comparison with that one to the
3272 front of the tree. The others will be eliminated by
3273 optimization. We do not want to do this if the insn has one
3274 alternative and we have tested none of them! */
3275 left = make_alternative_compare (i);
3276 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3277 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
3279 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3283 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3285 newexp = attr_rtx (IOR, left, right);
3286 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3291 if (GET_CODE (XEXP (exp, 0)) == NOT)
3293 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
3294 insn_code, insn_index);
3295 SIMPLIFY_ALTERNATIVE (left);
3299 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3300 SIMPLIFY_ALTERNATIVE (left);
3301 if (GET_CODE (left) == NOT)
3302 return XEXP (left, 0);
3304 if (left == false_rtx)
3306 else if (left == true_rtx)
3309 /* Try to apply De`Morgan's laws. */
3310 else if (GET_CODE (left) == IOR)
3312 newexp = attr_rtx (AND,
3313 attr_rtx (NOT, XEXP (left, 0)),
3314 attr_rtx (NOT, XEXP (left, 1)));
3316 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3318 else if (GET_CODE (left) == AND)
3320 newexp = attr_rtx (IOR,
3321 attr_rtx (NOT, XEXP (left, 0)),
3322 attr_rtx (NOT, XEXP (left, 1)));
3324 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3326 else if (left != XEXP (exp, 0))
3328 newexp = attr_rtx (NOT, left);
3333 if (current_alternative_string && XSTR (exp, 0) == alternative_name)
3334 return (XSTR (exp, 1) == current_alternative_string
3335 ? true_rtx : false_rtx);
3337 /* Look at the value for this insn code in the specified attribute.
3338 We normally can replace this comparison with the condition that
3339 would give this insn the values being tested for. */
3340 if (XSTR (exp, 0) != alternative_name
3341 && (attr = find_attr (XSTR (exp, 0), 0)) != NULL)
3342 for (av = attr->first_value; av; av = av->next)
3343 for (ie = av->first_insn; ie; ie = ie->next)
3344 if (ie->insn_code == insn_code)
3347 x = evaluate_eq_attr (exp, av->value, insn_code, insn_index);
3348 x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
3349 if (attr_rtx_cost(x) < 20)
3358 /* We have already simplified this expression. Simplifying it again
3359 won't buy anything unless we weren't given a valid insn code
3360 to process (i.e., we are canonicalizing something.). */
3361 if (insn_code != -2 /* Seems wrong: && current_alternative_string. */
3362 && ! ATTR_IND_SIMPLIFIED_P (newexp))
3363 return copy_rtx_unchanging (newexp);
3368 /* Optimize the attribute lists by seeing if we can determine conditional
3369 values from the known values of other attributes. This will save subroutine
3370 calls during the compilation. */
3373 optimize_attrs (void)
3375 struct attr_desc *attr;
3376 struct attr_value *av;
3377 struct insn_ent *ie;
3380 struct attr_value_list
3382 struct attr_value *av;
3383 struct insn_ent *ie;
3384 struct attr_desc *attr;
3385 struct attr_value_list *next;
3387 struct attr_value_list **insn_code_values;
3388 struct attr_value_list *ivbuf;
3389 struct attr_value_list *iv;
3391 /* For each insn code, make a list of all the insn_ent's for it,
3392 for all values for all attributes. */
3394 if (num_insn_ents == 0)
3397 /* Make 2 extra elements, for "code" values -2 and -1. */
3399 = (struct attr_value_list **) xmalloc ((insn_code_number + 2)
3400 * sizeof (struct attr_value_list *));
3401 memset ((char *) insn_code_values, 0,
3402 (insn_code_number + 2) * sizeof (struct attr_value_list *));
3404 /* Offset the table address so we can index by -2 or -1. */
3405 insn_code_values += 2;
3407 iv = ivbuf = ((struct attr_value_list *)
3408 xmalloc (num_insn_ents * sizeof (struct attr_value_list)));
3410 for (i = 0; i < MAX_ATTRS_INDEX; i++)
3411 for (attr = attrs[i]; attr; attr = attr->next)
3412 for (av = attr->first_value; av; av = av->next)
3413 for (ie = av->first_insn; ie; ie = ie->next)
3418 iv->next = insn_code_values[ie->insn_code];
3419 insn_code_values[ie->insn_code] = iv;
3423 /* Sanity check on num_insn_ents. */
3424 if (iv != ivbuf + num_insn_ents)
3427 /* Process one insn code at a time. */
3428 for (i = -2; i < insn_code_number; i++)
3430 /* Clear the ATTR_CURR_SIMPLIFIED_P flag everywhere relevant.
3431 We use it to mean "already simplified for this insn". */
3432 for (iv = insn_code_values[i]; iv; iv = iv->next)
3433 clear_struct_flag (iv->av->value);
3435 for (iv = insn_code_values[i]; iv; iv = iv->next)
3437 struct obstack *old = rtl_obstack;
3442 if (GET_CODE (av->value) != COND)
3445 rtl_obstack = temp_obstack;
3447 while (GET_CODE (newexp) == COND)
3449 rtx newexp2 = simplify_cond (newexp, ie->insn_code,
3451 if (newexp2 == newexp)
3457 if (newexp != av->value)
3459 newexp = attr_copy_rtx (newexp);
3460 remove_insn_ent (av, ie);
3461 av = get_attr_value (newexp, attr, ie->insn_code);
3463 insert_insn_ent (av, ie);
3469 free (insn_code_values - 2);
3472 /* If EXP is a suitable expression, reorganize it by constructing an
3473 equivalent expression that is a COND with the tests being all combinations
3474 of attribute values and the values being simple constants. */
3477 simplify_by_exploding (rtx exp)
3479 rtx list = 0, link, condexp, defval = NULL_RTX;
3480 struct dimension *space;
3481 rtx *condtest, *condval;
3482 int i, j, total, ndim = 0;
3483 int most_tests, num_marks, new_marks;
3486 /* Locate all the EQ_ATTR expressions. */
3487 if (! find_and_mark_used_attributes (exp, &list, &ndim) || ndim == 0)
3489 unmark_used_attributes (list, 0, 0);
3493 /* Create an attribute space from the list of used attributes. For each
3494 dimension in the attribute space, record the attribute, list of values
3495 used, and number of values used. Add members to the list of values to
3496 cover the domain of the attribute. This makes the expanded COND form
3497 order independent. */
3499 space = (struct dimension *) xmalloc (ndim * sizeof (struct dimension));
3502 for (ndim = 0; list; ndim++)
3504 /* Pull the first attribute value from the list and record that
3505 attribute as another dimension in the attribute space. */
3506 const char *name = XSTR (XEXP (list, 0), 0);
3509 if ((space[ndim].attr = find_attr (name, 0)) == 0
3510 || space[ndim].attr->is_numeric)
3512 unmark_used_attributes (list, space, ndim);
3516 /* Add all remaining attribute values that refer to this attribute. */
3517 space[ndim].num_values = 0;
3518 space[ndim].values = 0;
3520 for (link = list; link; link = *prev)
3521 if (! strcmp (XSTR (XEXP (link, 0), 0), name))
3523 space[ndim].num_values++;
3524 *prev = XEXP (link, 1);
3525 XEXP (link, 1) = space[ndim].values;
3526 space[ndim].values = link;
3529 prev = &XEXP (link, 1);
3531 /* Add sufficient members to the list of values to make the list
3532 mutually exclusive and record the total size of the attribute
3534 total *= add_values_to_cover (&space[ndim]);
3537 /* Sort the attribute space so that the attributes go from non-constant
3538 to constant and from most values to least values. */
3539 for (i = 0; i < ndim; i++)
3540 for (j = ndim - 1; j > i; j--)
3541 if ((space[j-1].attr->is_const && !space[j].attr->is_const)
3542 || space[j-1].num_values < space[j].num_values)
3544 struct dimension tmp;
3546 space[j] = space[j - 1];
3550 /* Establish the initial current value. */
3551 for (i = 0; i < ndim; i++)
3552 space[i].current_value = space[i].values;
3554 condtest = (rtx *) xmalloc (total * sizeof (rtx));
3555 condval = (rtx *) xmalloc (total * sizeof (rtx));
3557 /* Expand the tests and values by iterating over all values in the
3561 condtest[i] = test_for_current_value (space, ndim);
3562 condval[i] = simplify_with_current_value (exp, space, ndim);
3563 if (! increment_current_value (space, ndim))
3569 /* We are now finished with the original expression. */
3570 unmark_used_attributes (0, space, ndim);
3573 /* Find the most used constant value and make that the default. */
3575 for (i = num_marks = 0; i < total; i++)
3576 if (GET_CODE (condval[i]) == CONST_STRING
3577 && ! ATTR_EQ_ATTR_P (condval[i]))
3579 /* Mark the unmarked constant value and count how many are marked. */
3580 ATTR_EQ_ATTR_P (condval[i]) = 1;
3581 for (j = new_marks = 0; j < total; j++)
3582 if (GET_CODE (condval[j]) == CONST_STRING
3583 && ATTR_EQ_ATTR_P (condval[j]))
3585 if (new_marks - num_marks > most_tests)
3587 most_tests = new_marks - num_marks;
3588 defval = condval[i];
3590 num_marks = new_marks;
3592 /* Clear all the marks. */
3593 for (i = 0; i < total; i++)
3594 ATTR_EQ_ATTR_P (condval[i]) = 0;
3596 /* Give up if nothing is constant. */
3600 /* If all values are the default, use that. */
3601 else if (total == most_tests)
3604 /* Make a COND with the most common constant value the default. (A more
3605 complex method where tests with the same value were combined didn't
3606 seem to improve things.) */
3609 condexp = rtx_alloc (COND);
3610 XVEC (condexp, 0) = rtvec_alloc ((total - most_tests) * 2);
3611 XEXP (condexp, 1) = defval;
3612 for (i = j = 0; i < total; i++)
3613 if (condval[i] != defval)
3615 XVECEXP (condexp, 0, 2 * j) = condtest[i];
3616 XVECEXP (condexp, 0, 2 * j + 1) = condval[i];
3626 /* Set the ATTR_EQ_ATTR_P flag for all EQ_ATTR expressions in EXP and
3627 verify that EXP can be simplified to a constant term if all the EQ_ATTR
3628 tests have known value. */
3631 find_and_mark_used_attributes (rtx exp, rtx *terms, int *nterms)
3635 switch (GET_CODE (exp))
3638 if (! ATTR_EQ_ATTR_P (exp))
3640 rtx link = rtx_alloc (EXPR_LIST);
3641 XEXP (link, 0) = exp;
3642 XEXP (link, 1) = *terms;
3645 ATTR_EQ_ATTR_P (exp) = 1;
3654 if (! find_and_mark_used_attributes (XEXP (exp, 2), terms, nterms))
3658 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3661 if (! find_and_mark_used_attributes (XEXP (exp, 0), terms, nterms))
3666 for (i = 0; i < XVECLEN (exp, 0); i++)
3667 if (! find_and_mark_used_attributes (XVECEXP (exp, 0, i), terms, nterms))
3669 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3678 /* Clear the ATTR_EQ_ATTR_P flag in all EQ_ATTR expressions on LIST and
3679 in the values of the NDIM-dimensional attribute space SPACE. */
3682 unmark_used_attributes (rtx list, struct dimension *space, int ndim)
3687 for (i = 0; i < ndim; i++)
3688 unmark_used_attributes (space[i].values, 0, 0);
3690 for (link = list; link; link = XEXP (link, 1))
3692 exp = XEXP (link, 0);
3693 if (GET_CODE (exp) == EQ_ATTR)
3694 ATTR_EQ_ATTR_P (exp) = 0;
3698 /* Update the attribute dimension DIM so that all values of the attribute
3699 are tested. Return the updated number of values. */
3702 add_values_to_cover (struct dimension *dim)
3704 struct attr_value *av;
3705 rtx exp, link, *prev;
3708 for (av = dim->attr->first_value; av; av = av->next)
3709 if (GET_CODE (av->value) == CONST_STRING)
3712 if (nalt < dim->num_values)
3714 else if (nalt == dim->num_values)
3717 else if (nalt * 2 < dim->num_values * 3)
3719 /* Most all the values of the attribute are used, so add all the unused
3721 prev = &dim->values;
3722 for (link = dim->values; link; link = *prev)
3723 prev = &XEXP (link, 1);
3725 for (av = dim->attr->first_value; av; av = av->next)
3726 if (GET_CODE (av->value) == CONST_STRING)
3728 exp = attr_eq (dim->attr->name, XSTR (av->value, 0));
3729 if (ATTR_EQ_ATTR_P (exp))
3732 link = rtx_alloc (EXPR_LIST);
3733 XEXP (link, 0) = exp;
3736 prev = &XEXP (link, 1);
3738 dim->num_values = nalt;
3742 rtx orexp = false_rtx;
3744 /* Very few values are used, so compute a mutually exclusive
3745 expression. (We could do this for numeric values if that becomes
3747 prev = &dim->values;
3748 for (link = dim->values; link; link = *prev)
3750 orexp = insert_right_side (IOR, orexp, XEXP (link, 0), -2, -2);
3751 prev = &XEXP (link, 1);
3753 link = rtx_alloc (EXPR_LIST);
3754 XEXP (link, 0) = attr_rtx (NOT, orexp);
3759 return dim->num_values;
3762 /* Increment the current value for the NDIM-dimensional attribute space SPACE
3763 and return FALSE if the increment overflowed. */
3766 increment_current_value (struct dimension *space, int ndim)
3770 for (i = ndim - 1; i >= 0; i--)
3772 if ((space[i].current_value = XEXP (space[i].current_value, 1)) == 0)
3773 space[i].current_value = space[i].values;
3780 /* Construct an expression corresponding to the current value for the
3781 NDIM-dimensional attribute space SPACE. */
3784 test_for_current_value (struct dimension *space, int ndim)
3789 for (i = 0; i < ndim; i++)
3790 exp = insert_right_side (AND, exp, XEXP (space[i].current_value, 0),
3796 /* Given the current value of the NDIM-dimensional attribute space SPACE,
3797 set the corresponding EQ_ATTR expressions to that value and reduce
3798 the expression EXP as much as possible. On input [and output], all
3799 known EQ_ATTR expressions are set to FALSE. */
3802 simplify_with_current_value (rtx exp, struct dimension *space, int ndim)
3807 /* Mark each current value as TRUE. */
3808 for (i = 0; i < ndim; i++)
3810 x = XEXP (space[i].current_value, 0);
3811 if (GET_CODE (x) == EQ_ATTR)
3812 ATTR_EQ_ATTR_P (x) = 0;
3815 exp = simplify_with_current_value_aux (exp);
3817 /* Change each current value back to FALSE. */
3818 for (i = 0; i < ndim; i++)
3820 x = XEXP (space[i].current_value, 0);
3821 if (GET_CODE (x) == EQ_ATTR)
3822 ATTR_EQ_ATTR_P (x) = 1;
3828 /* Reduce the expression EXP based on the ATTR_EQ_ATTR_P settings of
3829 all EQ_ATTR expressions. */
3832 simplify_with_current_value_aux (rtx exp)
3837 switch (GET_CODE (exp))
3840 if (ATTR_EQ_ATTR_P (exp))
3849 cond = simplify_with_current_value_aux (XEXP (exp, 0));
3850 if (cond == true_rtx)
3851 return simplify_with_current_value_aux (XEXP (exp, 1));
3852 else if (cond == false_rtx)
3853 return simplify_with_current_value_aux (XEXP (exp, 2));
3855 return attr_rtx (IF_THEN_ELSE, cond,
3856 simplify_with_current_value_aux (XEXP (exp, 1)),
3857 simplify_with_current_value_aux (XEXP (exp, 2)));
3860 cond = simplify_with_current_value_aux (XEXP (exp, 1));
3861 if (cond == true_rtx)
3863 else if (cond == false_rtx)
3864 return simplify_with_current_value_aux (XEXP (exp, 0));
3866 return attr_rtx (IOR, cond,
3867 simplify_with_current_value_aux (XEXP (exp, 0)));
3870 cond = simplify_with_current_value_aux (XEXP (exp, 1));
3871 if (cond == true_rtx)
3872 return simplify_with_current_value_aux (XEXP (exp, 0));
3873 else if (cond == false_rtx)
3876 return attr_rtx (AND, cond,
3877 simplify_with_current_value_aux (XEXP (exp, 0)));
3880 cond = simplify_with_current_value_aux (XEXP (exp, 0));
3881 if (cond == true_rtx)
3883 else if (cond == false_rtx)
3886 return attr_rtx (NOT, cond);
3889 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3891 cond = simplify_with_current_value_aux (XVECEXP (exp, 0, i));
3892 if (cond == true_rtx)
3893 return simplify_with_current_value_aux (XVECEXP (exp, 0, i + 1));
3894 else if (cond == false_rtx)
3897 abort (); /* With all EQ_ATTR's of known value, a case should
3898 have been selected. */
3900 return simplify_with_current_value_aux (XEXP (exp, 1));
3907 /* Clear the ATTR_CURR_SIMPLIFIED_P flag in EXP and its subexpressions. */
3910 clear_struct_flag (rtx x)
3917 ATTR_CURR_SIMPLIFIED_P (x) = 0;
3918 if (ATTR_IND_SIMPLIFIED_P (x))
3921 code = GET_CODE (x);
3942 /* Compare the elements. If any pair of corresponding elements
3943 fail to match, return 0 for the whole things. */
3945 fmt = GET_RTX_FORMAT (code);
3946 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3952 for (j = 0; j < XVECLEN (x, i); j++)
3953 clear_struct_flag (XVECEXP (x, i, j));
3957 clear_struct_flag (XEXP (x, i));
3963 /* Return the number of RTX objects making up the expression X.
3964 But if we count more than MAX objects, stop counting. */
3967 count_sub_rtxs (rtx x, int max)
3975 code = GET_CODE (x);
3996 /* Compare the elements. If any pair of corresponding elements
3997 fail to match, return 0 for the whole things. */
3999 fmt = GET_RTX_FORMAT (code);
4000 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4009 for (j = 0; j < XVECLEN (x, i); j++)
4010 total += count_sub_rtxs (XVECEXP (x, i, j), max);
4014 total += count_sub_rtxs (XEXP (x, i), max);
4022 /* Create table entries for DEFINE_ATTR. */
4025 gen_attr (rtx exp, int lineno)
4027 struct attr_desc *attr;
4028 struct attr_value *av;
4029 const char *name_ptr;
4032 /* Make a new attribute structure. Check for duplicate by looking at
4033 attr->default_val, since it is initialized by this routine. */
4034 attr = find_attr (XSTR (exp, 0), 1);
4035 if (attr->default_val)
4037 message_with_line (lineno, "duplicate definition for attribute %s",
4039 message_with_line (attr->lineno, "previous definition");
4043 attr->lineno = lineno;
4045 if (*XSTR (exp, 1) == '\0')
4046 attr->is_numeric = 1;
4049 name_ptr = XSTR (exp, 1);
4050 while ((p = next_comma_elt (&name_ptr)) != NULL)
4052 av = (struct attr_value *) oballoc (sizeof (struct attr_value));
4053 av->value = attr_rtx (CONST_STRING, p);
4054 av->next = attr->first_value;
4055 attr->first_value = av;
4056 av->first_insn = NULL;
4058 av->has_asm_insn = 0;
4062 if (GET_CODE (XEXP (exp, 2)) == CONST)
4065 if (attr->is_numeric)
4067 message_with_line (lineno,
4068 "constant attributes may not take numeric values");
4072 /* Get rid of the CONST node. It is allowed only at top-level. */
4073 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
4076 if (! strcmp (attr->name, "length") && ! attr->is_numeric)
4078 message_with_line (lineno,
4079 "`length' attribute must take numeric values");
4083 /* Set up the default value. */
4084 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
4085 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
4088 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
4089 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
4090 number of alternatives as this should be checked elsewhere. */
4093 count_alternatives (rtx exp)
4098 if (GET_CODE (exp) == MATCH_OPERAND)
4099 return n_comma_elts (XSTR (exp, 2));
4101 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4102 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4107 n = count_alternatives (XEXP (exp, i));
4114 if (XVEC (exp, i) != NULL)
4115 for (j = 0; j < XVECLEN (exp, i); j++)
4117 n = count_alternatives (XVECEXP (exp, i, j));
4126 /* Returns nonzero if the given expression contains an EQ_ATTR with the
4127 `alternative' attribute. */
4130 compares_alternatives_p (rtx exp)
4135 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
4138 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4139 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4144 if (compares_alternatives_p (XEXP (exp, i)))
4149 for (j = 0; j < XVECLEN (exp, i); j++)
4150 if (compares_alternatives_p (XVECEXP (exp, i, j)))
4158 /* Returns nonzero is INNER is contained in EXP. */
4161 contained_in_p (rtx inner, rtx exp)
4166 if (rtx_equal_p (inner, exp))
4169 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4170 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4175 if (contained_in_p (inner, XEXP (exp, i)))
4180 for (j = 0; j < XVECLEN (exp, i); j++)
4181 if (contained_in_p (inner, XVECEXP (exp, i, j)))
4189 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
4192 gen_insn (rtx exp, int lineno)
4194 struct insn_def *id;
4196 id = (struct insn_def *) oballoc (sizeof (struct insn_def));
4200 id->lineno = lineno;
4202 switch (GET_CODE (exp))
4205 id->insn_code = insn_code_number;
4206 id->insn_index = insn_index_number;
4207 id->num_alternatives = count_alternatives (exp);
4208 if (id->num_alternatives == 0)
4209 id->num_alternatives = 1;
4213 case DEFINE_PEEPHOLE:
4214 id->insn_code = insn_code_number;
4215 id->insn_index = insn_index_number;
4216 id->num_alternatives = count_alternatives (exp);
4217 if (id->num_alternatives == 0)
4218 id->num_alternatives = 1;
4222 case DEFINE_ASM_ATTRIBUTES:
4224 id->insn_index = -1;
4225 id->num_alternatives = 1;
4227 got_define_asm_attributes = 1;
4235 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
4236 true or annul false is specified, and make a `struct delay_desc'. */
4239 gen_delay (rtx def, int lineno)
4241 struct delay_desc *delay;
4244 if (XVECLEN (def, 1) % 3 != 0)
4246 message_with_line (lineno,
4247 "number of elements in DEFINE_DELAY must be multiple of three");
4252 for (i = 0; i < XVECLEN (def, 1); i += 3)
4254 if (XVECEXP (def, 1, i + 1))
4255 have_annul_true = 1;
4256 if (XVECEXP (def, 1, i + 2))
4257 have_annul_false = 1;
4260 delay = (struct delay_desc *) oballoc (sizeof (struct delay_desc));
4262 delay->num = ++num_delays;
4263 delay->next = delays;
4264 delay->lineno = lineno;
4268 /* Process a DEFINE_FUNCTION_UNIT.
4270 This gives information about a function unit contained in the CPU.
4271 We fill in a `struct function_unit_op' and a `struct function_unit'
4272 with information used later by `expand_unit'. */
4275 gen_unit (rtx def, int lineno)
4277 struct function_unit *unit;
4278 struct function_unit_op *op;
4279 const char *name = XSTR (def, 0);
4280 int multiplicity = XINT (def, 1);
4281 int simultaneity = XINT (def, 2);
4282 rtx condexp = XEXP (def, 3);
4283 int ready_cost = MAX (XINT (def, 4), 1);
4284 int issue_delay = MAX (XINT (def, 5), 1);
4286 /* See if we have already seen this function unit. If so, check that
4287 the multiplicity and simultaneity values are the same. If not, make
4288 a structure for this function unit. */
4289 for (unit = units; unit; unit = unit->next)
4290 if (! strcmp (unit->name, name))
4292 if (unit->multiplicity != multiplicity
4293 || unit->simultaneity != simultaneity)
4295 message_with_line (lineno,
4296 "differing specifications given for function unit %s",
4298 message_with_line (unit->first_lineno, "previous definition");
4307 unit = (struct function_unit *) oballoc (sizeof (struct function_unit));
4309 unit->multiplicity = multiplicity;
4310 unit->simultaneity = simultaneity;
4311 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
4312 unit->num = num_units++;
4313 unit->num_opclasses = 0;
4314 unit->condexp = false_rtx;
4317 unit->first_lineno = lineno;
4321 /* Make a new operation class structure entry and initialize it. */
4322 op = (struct function_unit_op *) oballoc (sizeof (struct function_unit_op));
4323 op->condexp = condexp;
4324 op->num = unit->num_opclasses++;
4325 op->ready = ready_cost;
4326 op->issue_delay = issue_delay;
4327 op->next = unit->ops;
4328 op->lineno = lineno;
4330 num_unit_opclasses++;
4332 /* Set our issue expression based on whether or not an optional conflict
4333 vector was specified. */
4336 /* Compute the IOR of all the specified expressions. */
4337 rtx orexp = false_rtx;
4340 for (i = 0; i < XVECLEN (def, 6); i++)
4341 orexp = insert_right_side (IOR, orexp, XVECEXP (def, 6, i), -2, -2);
4343 op->conflict_exp = orexp;
4344 extend_range (&unit->issue_delay, 1, issue_delay);
4348 op->conflict_exp = true_rtx;
4349 extend_range (&unit->issue_delay, issue_delay, issue_delay);
4352 /* Merge our conditional into that of the function unit so we can determine
4353 which insns are used by the function unit. */
4354 unit->condexp = insert_right_side (IOR, unit->condexp, op->condexp, -2, -2);
4357 /* Given a piece of RTX, print a C expression to test its truth value.
4358 We use AND and IOR both for logical and bit-wise operations, so
4359 interpret them as logical unless they are inside a comparison expression.
4360 The first bit of FLAGS will be nonzero in that case.
4362 Set the second bit of FLAGS to make references to attribute values use
4363 a cached local variable instead of calling a function. */
4366 write_test_expr (rtx exp, int flags)
4368 int comparison_operator = 0;
4370 struct attr_desc *attr;
4372 /* In order not to worry about operator precedence, surround our part of
4373 the expression with parentheses. */
4376 code = GET_CODE (exp);
4379 /* Binary operators. */
4381 case GE: case GT: case GEU: case GTU:
4382 case LE: case LT: case LEU: case LTU:
4383 comparison_operator = 1;
4385 case PLUS: case MINUS: case MULT: case DIV: case MOD:
4386 case AND: case IOR: case XOR:
4387 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4388 write_test_expr (XEXP (exp, 0), flags | comparison_operator);
4404 printf (" >= (unsigned) ");
4407 printf (" > (unsigned) ");
4416 printf (" <= (unsigned) ");
4419 printf (" < (unsigned) ");
4462 write_test_expr (XEXP (exp, 1), flags | comparison_operator);
4466 /* Special-case (not (eq_attrq "alternative" "x")) */
4467 if (! (flags & 1) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
4468 && XSTR (XEXP (exp, 0), 0) == alternative_name)
4470 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
4474 /* Otherwise, fall through to normal unary operator. */
4476 /* Unary operators. */
4496 write_test_expr (XEXP (exp, 0), flags);
4499 /* Comparison test of an attribute with a value. Most of these will
4500 have been removed by optimization. Handle "alternative"
4501 specially and give error if EQ_ATTR present inside a comparison. */
4504 fatal ("EQ_ATTR not valid inside comparison");
4506 if (XSTR (exp, 0) == alternative_name)
4508 printf ("which_alternative == %s", XSTR (exp, 1));
4512 attr = find_attr (XSTR (exp, 0), 0);
4516 /* Now is the time to expand the value of a constant attribute. */
4519 write_test_expr (evaluate_eq_attr (exp, attr->default_val->value,
4526 printf ("attr_%s", attr->name);
4528 printf ("get_attr_%s (insn)", attr->name);
4530 write_attr_valueq (attr, XSTR (exp, 1));
4534 /* Comparison test of flags for define_delays. */
4537 fatal ("ATTR_FLAG not valid inside comparison");
4538 printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
4541 /* See if an operand matches a predicate. */
4543 /* If only a mode is given, just ensure the mode matches the operand.
4544 If neither a mode nor predicate is given, error. */
4545 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
4547 if (GET_MODE (exp) == VOIDmode)
4548 fatal ("null MATCH_OPERAND specified as test");
4550 printf ("GET_MODE (operands[%d]) == %smode",
4551 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4554 printf ("%s (operands[%d], %smode)",
4555 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4559 printf ("%s (insn)", XSTR (exp, 0));
4562 /* Constant integer. */
4564 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
4567 /* A random C expression. */
4569 printf ("%s", XSTR (exp, 0));
4572 /* The address of the branch target. */
4574 printf ("INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
4575 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
4579 /* The address of the current insn. We implement this actually as the
4580 address of the current insn for backward branches, but the last
4581 address of the next insn for forward branches, and both with
4582 adjustments that account for the worst-case possible stretching of
4583 intervening alignments between this insn and its destination. */
4584 printf ("insn_current_reference_address (insn)");
4588 printf ("%s", XSTR (exp, 0));
4592 write_test_expr (XEXP (exp, 0), flags & 2);
4594 write_test_expr (XEXP (exp, 1), flags | 1);
4596 write_test_expr (XEXP (exp, 2), flags | 1);
4600 fatal ("bad RTX code `%s' in attribute calculation\n",
4601 GET_RTX_NAME (code));
4607 /* Given an attribute value, return the maximum CONST_STRING argument
4608 encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
4611 max_attr_value (rtx exp, int *unknownp)
4616 switch (GET_CODE (exp))
4619 current_max = atoi (XSTR (exp, 0));
4623 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4624 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4626 n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4627 if (n > current_max)
4633 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4634 n = max_attr_value (XEXP (exp, 2), unknownp);
4635 if (n > current_max)
4641 current_max = INT_MAX;
4648 /* Given an attribute value, return the result of ORing together all
4649 CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
4650 if the numeric value is not known. */
4653 or_attr_value (rtx exp, int *unknownp)
4658 switch (GET_CODE (exp))
4661 current_or = atoi (XSTR (exp, 0));
4665 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4666 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4667 current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4671 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4672 current_or |= or_attr_value (XEXP (exp, 2), unknownp);
4684 /* Scan an attribute value, possibly a conditional, and record what actions
4685 will be required to do any conditional tests in it.
4688 `must_extract' if we need to extract the insn operands
4689 `must_constrain' if we must compute `which_alternative'
4690 `address_used' if an address expression was used
4691 `length_used' if an (eq_attr "length" ...) was used
4695 walk_attr_value (rtx exp)
4704 code = GET_CODE (exp);
4708 if (! ATTR_IND_SIMPLIFIED_P (exp))
4709 /* Since this is an arbitrary expression, it can look at anything.
4710 However, constant expressions do not depend on any particular
4712 must_extract = must_constrain = 1;
4720 if (XSTR (exp, 0) == alternative_name)
4721 must_extract = must_constrain = 1;
4722 else if (strcmp (XSTR (exp, 0), "length") == 0)
4742 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
4747 walk_attr_value (XEXP (exp, i));
4751 if (XVEC (exp, i) != NULL)
4752 for (j = 0; j < XVECLEN (exp, i); j++)
4753 walk_attr_value (XVECEXP (exp, i, j));
4758 /* Write out a function to obtain the attribute for a given INSN. */
4761 write_attr_get (struct attr_desc *attr)
4763 struct attr_value *av, *common_av;
4765 /* Find the most used attribute value. Handle that as the `default' of the
4766 switch we will generate. */
4767 common_av = find_most_used (attr);
4769 /* Write out prototype of function. */
4770 if (!attr->is_numeric)
4771 printf ("extern enum attr_%s ", attr->name);
4772 else if (attr->unsigned_p)
4773 printf ("extern unsigned int ");
4775 printf ("extern int ");
4776 /* If the attribute name starts with a star, the remainder is the name of
4777 the subroutine to use, instead of `get_attr_...'. */
4778 if (attr->name[0] == '*')
4779 printf ("%s (rtx);\n", &attr->name[1]);
4781 printf ("get_attr_%s (%s);\n", attr->name,
4782 (attr->is_const ? "void" : "rtx"));
4784 /* Write out start of function, then all values with explicit `case' lines,
4785 then a `default', then the value with the most uses. */
4786 if (!attr->is_numeric)
4787 printf ("enum attr_%s\n", attr->name);
4788 else if (attr->unsigned_p)
4789 printf ("unsigned int\n");
4793 /* If the attribute name starts with a star, the remainder is the name of
4794 the subroutine to use, instead of `get_attr_...'. */
4795 if (attr->name[0] == '*')
4796 printf ("%s (rtx insn ATTRIBUTE_UNUSED)\n", &attr->name[1]);
4797 else if (attr->is_const == 0)
4798 printf ("get_attr_%s (rtx insn ATTRIBUTE_UNUSED)\n", attr->name);
4801 printf ("get_attr_%s (void)\n", attr->name);
4804 for (av = attr->first_value; av; av = av->next)
4805 if (av->num_insns != 0)
4806 write_attr_set (attr, 2, av->value, "return", ";",
4807 true_rtx, av->first_insn->insn_code,
4808 av->first_insn->insn_index);
4816 if (GET_CODE (common_av->value) == FFS)
4818 rtx p = XEXP (common_av->value, 0);
4820 /* No need to emit code to abort if the insn is unrecognized; the
4821 other get_attr_foo functions will do that when we call them. */
4823 write_toplevel_expr (p);
4825 printf ("\n if (accum && accum == (accum & -accum))\n");
4827 printf (" int i;\n");
4828 printf (" for (i = 0; accum >>= 1; ++i) continue;\n");
4829 printf (" accum = i;\n");
4830 printf (" }\n else\n");
4831 printf (" accum = ~accum;\n");
4832 printf (" return accum;\n}\n\n");
4836 printf (" switch (recog_memoized (insn))\n");
4839 for (av = attr->first_value; av; av = av->next)
4840 if (av != common_av)
4841 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
4843 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
4844 printf (" }\n}\n\n");
4848 /* Given an AND tree of known true terms (because we are inside an `if' with
4849 that as the condition or are in an `else' clause) and an expression,
4850 replace any known true terms with TRUE. Use `simplify_and_tree' to do
4851 the bulk of the work. */
4854 eliminate_known_true (rtx known_true, rtx exp, int insn_code, int insn_index)
4858 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
4860 if (GET_CODE (known_true) == AND)
4862 exp = eliminate_known_true (XEXP (known_true, 0), exp,
4863 insn_code, insn_index);
4864 exp = eliminate_known_true (XEXP (known_true, 1), exp,
4865 insn_code, insn_index);
4870 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
4876 /* Write out a series of tests and assignment statements to perform tests and
4877 sets of an attribute value. We are passed an indentation amount and prefix
4878 and suffix strings to write around each attribute value (e.g., "return"
4882 write_attr_set (struct attr_desc *attr, int indent, rtx value,
4883 const char *prefix, const char *suffix, rtx known_true,
4884 int insn_code, int insn_index)
4886 if (GET_CODE (value) == COND)
4888 /* Assume the default value will be the default of the COND unless we
4889 find an always true expression. */
4890 rtx default_val = XEXP (value, 1);
4891 rtx our_known_true = known_true;
4896 for (i = 0; i < XVECLEN (value, 0); i += 2)
4901 testexp = eliminate_known_true (our_known_true,
4902 XVECEXP (value, 0, i),
4903 insn_code, insn_index);
4904 newexp = attr_rtx (NOT, testexp);
4905 newexp = insert_right_side (AND, our_known_true, newexp,
4906 insn_code, insn_index);
4908 /* If the test expression is always true or if the next `known_true'
4909 expression is always false, this is the last case, so break
4910 out and let this value be the `else' case. */
4911 if (testexp == true_rtx || newexp == false_rtx)
4913 default_val = XVECEXP (value, 0, i + 1);
4917 /* Compute the expression to pass to our recursive call as being
4919 inner_true = insert_right_side (AND, our_known_true,
4920 testexp, insn_code, insn_index);
4922 /* If this is always false, skip it. */
4923 if (inner_true == false_rtx)
4926 write_indent (indent);
4927 printf ("%sif ", first_if ? "" : "else ");
4929 write_test_expr (testexp, 0);
4931 write_indent (indent + 2);
4934 write_attr_set (attr, indent + 4,
4935 XVECEXP (value, 0, i + 1), prefix, suffix,
4936 inner_true, insn_code, insn_index);
4937 write_indent (indent + 2);
4939 our_known_true = newexp;
4944 write_indent (indent);
4946 write_indent (indent + 2);
4950 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
4951 prefix, suffix, our_known_true, insn_code, insn_index);
4955 write_indent (indent + 2);
4961 write_indent (indent);
4962 printf ("%s ", prefix);
4963 write_attr_value (attr, value);
4964 printf ("%s\n", suffix);
4968 /* Write out the computation for one attribute value. */
4971 write_attr_case (struct attr_desc *attr, struct attr_value *av,
4972 int write_case_lines, const char *prefix, const char *suffix,
4973 int indent, rtx known_true)
4975 struct insn_ent *ie;
4977 if (av->num_insns == 0)
4980 if (av->has_asm_insn)
4982 write_indent (indent);
4983 printf ("case -1:\n");
4984 write_indent (indent + 2);
4985 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
4986 write_indent (indent + 2);
4987 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
4988 write_indent (indent + 2);
4989 printf (" fatal_insn_not_found (insn);\n");
4992 if (write_case_lines)
4994 for (ie = av->first_insn; ie; ie = ie->next)
4995 if (ie->insn_code != -1)
4997 write_indent (indent);
4998 printf ("case %d:\n", ie->insn_code);
5003 write_indent (indent);
5004 printf ("default:\n");
5007 /* See what we have to do to output this value. */
5008 must_extract = must_constrain = address_used = 0;
5009 walk_attr_value (av->value);
5013 write_indent (indent + 2);
5014 printf ("extract_constrain_insn_cached (insn);\n");
5016 else if (must_extract)
5018 write_indent (indent + 2);
5019 printf ("extract_insn_cached (insn);\n");
5022 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
5023 known_true, av->first_insn->insn_code,
5024 av->first_insn->insn_index);
5026 if (strncmp (prefix, "return", 6))
5028 write_indent (indent + 2);
5029 printf ("break;\n");
5034 /* Search for uses of non-const attributes and write code to cache them. */
5037 write_expr_attr_cache (rtx p, struct attr_desc *attr)
5042 if (GET_CODE (p) == EQ_ATTR)
5044 if (XSTR (p, 0) != attr->name)
5047 if (!attr->is_numeric)
5048 printf (" enum attr_%s ", attr->name);
5049 else if (attr->unsigned_p)
5050 printf (" unsigned int ");
5054 printf ("attr_%s = get_attr_%s (insn);\n", attr->name, attr->name);
5058 fmt = GET_RTX_FORMAT (GET_CODE (p));
5059 ie = GET_RTX_LENGTH (GET_CODE (p));
5060 for (i = 0; i < ie; i++)
5065 if (write_expr_attr_cache (XEXP (p, i), attr))
5070 je = XVECLEN (p, i);
5071 for (j = 0; j < je; ++j)
5072 if (write_expr_attr_cache (XVECEXP (p, i, j), attr))
5081 /* Evaluate an expression at top level. A front end to write_test_expr,
5082 in which we cache attribute values and break up excessively large
5083 expressions to cater to older compilers. */
5086 write_toplevel_expr (rtx p)
5088 struct attr_desc *attr;
5091 for (i = 0; i < MAX_ATTRS_INDEX; ++i)
5092 for (attr = attrs[i]; attr; attr = attr->next)
5093 if (!attr->is_const)
5094 write_expr_attr_cache (p, attr);
5096 printf (" unsigned long accum = 0;\n\n");
5098 while (GET_CODE (p) == IOR)
5101 if (GET_CODE (XEXP (p, 0)) == IOR)
5102 e = XEXP (p, 1), p = XEXP (p, 0);
5104 e = XEXP (p, 0), p = XEXP (p, 1);
5106 printf (" accum |= ");
5107 write_test_expr (e, 3);
5110 printf (" accum |= ");
5111 write_test_expr (p, 3);
5115 /* Utilities to write names in various forms. */
5118 write_unit_name (const char *prefix, int num, const char *suffix)
5120 struct function_unit *unit;
5122 for (unit = units; unit; unit = unit->next)
5123 if (unit->num == num)
5125 printf ("%s%s%s", prefix, unit->name, suffix);
5129 printf ("%s<unknown>%s", prefix, suffix);
5133 write_attr_valueq (struct attr_desc *attr, const char *s)
5135 if (attr->is_numeric)
5141 /* Make the blockage range values and function units used values easier
5143 if (attr->func_units_p)
5146 printf (" /* units: none */");
5148 write_unit_name (" /* units: ", num, " */");
5152 const char *sep = " /* units: ";
5153 for (i = 0, num = ~num; num; i++, num >>= 1)
5156 write_unit_name (sep, i, (num == 1) ? " */" : "");
5162 else if (attr->blockage_p)
5163 printf (" /* min %d, max %d */", num >> (HOST_BITS_PER_INT / 2),
5164 num & ((1 << (HOST_BITS_PER_INT / 2)) - 1));
5166 else if (num > 9 || num < 0)
5167 printf (" /* 0x%x */", num);
5171 write_upcase (attr->name);
5178 write_attr_value (struct attr_desc *attr, rtx value)
5182 switch (GET_CODE (value))
5185 write_attr_valueq (attr, XSTR (value, 0));
5189 printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
5193 fputs (XSTR (value, 0), stdout);
5198 struct attr_desc *attr2 = find_attr (XSTR (value, 0), 0);
5199 printf ("get_attr_%s (%s)", attr2->name,
5200 (attr2->is_const ? "" : "insn"));
5221 write_attr_value (attr, XEXP (value, 0));
5225 write_attr_value (attr, XEXP (value, 1));
5234 write_upcase (const char *str)
5238 /* The argument of TOUPPER should not have side effects. */
5239 putchar (TOUPPER(*str));
5245 write_indent (int indent)
5247 for (; indent > 8; indent -= 8)
5250 for (; indent; indent--)
5254 /* Write a subroutine that is given an insn that requires a delay slot, a
5255 delay slot ordinal, and a candidate insn. It returns nonzero if the
5256 candidate can be placed in the specified delay slot of the insn.
5258 We can write as many as three subroutines. `eligible_for_delay'
5259 handles normal delay slots, `eligible_for_annul_true' indicates that
5260 the specified insn can be annulled if the branch is true, and likewise
5261 for `eligible_for_annul_false'.
5263 KIND is a string distinguishing these three cases ("delay", "annul_true",
5264 or "annul_false"). */
5267 write_eligible_delay (const char *kind)
5269 struct delay_desc *delay;
5272 struct attr_desc *attr;
5273 struct attr_value *av, *common_av;
5276 /* Compute the maximum number of delay slots required. We use the delay
5277 ordinal times this number plus one, plus the slot number as an index into
5278 the appropriate predicate to test. */
5280 for (delay = delays, max_slots = 0; delay; delay = delay->next)
5281 if (XVECLEN (delay->def, 1) / 3 > max_slots)
5282 max_slots = XVECLEN (delay->def, 1) / 3;
5284 /* Write function prelude. */
5287 printf ("eligible_for_%s (rtx delay_insn ATTRIBUTE_UNUSED, int slot, rtx candidate_insn, int flags ATTRIBUTE_UNUSED)\n",
5290 printf (" rtx insn;\n");
5292 printf (" if (slot >= %d)\n", max_slots);
5293 printf (" abort ();\n");
5296 /* If more than one delay type, find out which type the delay insn is. */
5300 attr = find_attr ("*delay_type", 0);
5303 common_av = find_most_used (attr);
5305 printf (" insn = delay_insn;\n");
5306 printf (" switch (recog_memoized (insn))\n");
5309 sprintf (str, " * %d;\n break;", max_slots);
5310 for (av = attr->first_value; av; av = av->next)
5311 if (av != common_av)
5312 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
5314 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
5317 /* Ensure matched. Otherwise, shouldn't have been called. */
5318 printf (" if (slot < %d)\n", max_slots);
5319 printf (" abort ();\n\n");
5322 /* If just one type of delay slot, write simple switch. */
5323 if (num_delays == 1 && max_slots == 1)
5325 printf (" insn = candidate_insn;\n");
5326 printf (" switch (recog_memoized (insn))\n");
5329 attr = find_attr ("*delay_1_0", 0);
5332 common_av = find_most_used (attr);
5334 for (av = attr->first_value; av; av = av->next)
5335 if (av != common_av)
5336 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
5338 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
5344 /* Write a nested CASE. The first indicates which condition we need to
5345 test, and the inner CASE tests the condition. */
5346 printf (" insn = candidate_insn;\n");
5347 printf (" switch (slot)\n");
5350 for (delay = delays; delay; delay = delay->next)
5351 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
5353 printf (" case %d:\n",
5354 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
5355 printf (" switch (recog_memoized (insn))\n");
5358 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
5359 attr = find_attr (str, 0);
5362 common_av = find_most_used (attr);
5364 for (av = attr->first_value; av; av = av->next)
5365 if (av != common_av)
5366 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
5368 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
5372 printf (" default:\n");
5373 printf (" abort ();\n");
5380 /* Write routines to compute conflict cost for function units. Then write a
5381 table describing the available function units. */
5384 write_function_unit_info (void)
5386 struct function_unit *unit;
5389 /* Write out conflict routines for function units. Don't bother writing
5390 one if there is only one issue delay value. */
5392 for (unit = units; unit; unit = unit->next)
5394 if (unit->needs_blockage_function)
5395 write_complex_function (unit, "blockage", "block");
5397 /* If the minimum and maximum conflict costs are the same, there
5398 is only one value, so we don't need a function. */
5399 if (! unit->needs_conflict_function)
5401 unit->default_cost = make_numeric_value (unit->issue_delay.max);
5405 /* The function first computes the case from the candidate insn. */
5406 unit->default_cost = make_numeric_value (0);
5407 write_complex_function (unit, "conflict_cost", "cost");
5410 /* Now that all functions have been written, write the table describing
5411 the function units. The name is included for documentation purposes
5414 printf ("const struct function_unit_desc function_units[] = {\n");
5416 /* Write out the descriptions in numeric order, but don't force that order
5417 on the list. Doing so increases the runtime of genattrtab.c. */
5418 for (i = 0; i < num_units; i++)
5420 for (unit = units; unit; unit = unit->next)
5424 printf (" {\"%s\", %d, %d, %d, %s, %d, %s_unit_ready_cost, ",
5425 unit->name, 1 << unit->num, unit->multiplicity,
5426 unit->simultaneity, XSTR (unit->default_cost, 0),
5427 unit->issue_delay.max, unit->name);
5429 if (unit->needs_conflict_function)
5430 printf ("%s_unit_conflict_cost, ", unit->name);
5434 printf ("%d, ", unit->max_blockage);
5436 if (unit->needs_range_function)
5437 printf ("%s_unit_blockage_range, ", unit->name);
5441 if (unit->needs_blockage_function)
5442 printf ("%s_unit_blockage", unit->name);
5450 printf ("{\"dummy\", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* a dummy element */");
5455 write_complex_function (struct function_unit *unit,
5457 const char *connection)
5459 struct attr_desc *case_attr, *attr;
5460 struct attr_value *av, *common_av;
5466 printf ("static int\n");
5467 printf ("%s_unit_%s (rtx executing_insn, rtx candidate_insn)\n",
5470 printf (" rtx insn;\n");
5471 printf (" int casenum;\n\n");
5472 printf (" insn = executing_insn;\n");
5473 printf (" switch (recog_memoized (insn))\n");
5476 /* Write the `switch' statement to get the case value. */
5477 if (strlen (unit->name) + sizeof "*_cases" > 256)
5479 sprintf (str, "*%s_cases", unit->name);
5480 case_attr = find_attr (str, 0);
5483 common_av = find_most_used (case_attr);
5485 for (av = case_attr->first_value; av; av = av->next)
5486 if (av != common_av)
5487 write_attr_case (case_attr, av, 1,
5488 "casenum =", ";", 4, unit->condexp);
5490 write_attr_case (case_attr, common_av, 0,
5491 "casenum =", ";", 4, unit->condexp);
5494 /* Now write an outer switch statement on each case. Then write
5495 the tests on the executing function within each. */
5496 printf (" insn = candidate_insn;\n");
5497 printf (" switch (casenum)\n");
5500 for (i = 0; i < unit->num_opclasses; i++)
5502 /* Ensure using this case. */
5504 for (av = case_attr->first_value; av; av = av->next)
5506 && contained_in_p (make_numeric_value (i), av->value))
5512 printf (" case %d:\n", i);
5513 sprintf (str, "*%s_%s_%d", unit->name, connection, i);
5514 attr = find_attr (str, 0);
5518 /* If single value, just write it. */
5519 value = find_single_value (attr);
5521 write_attr_set (attr, 6, value, "return", ";\n", true_rtx, -2, -2);
5524 common_av = find_most_used (attr);
5525 printf (" switch (recog_memoized (insn))\n");
5528 for (av = attr->first_value; av; av = av->next)
5529 if (av != common_av)
5530 write_attr_case (attr, av, 1,
5531 "return", ";", 8, unit->condexp);
5533 write_attr_case (attr, common_av, 0,
5534 "return", ";", 8, unit->condexp);
5539 /* This default case should not be needed, but gcc's analysis is not
5540 good enough to realize that the default case is not needed for the
5541 second switch statement. */
5542 printf (" default:\n abort ();\n");
5543 printf (" }\n}\n\n");
5546 /* This page contains miscellaneous utility routines. */
5548 /* Given a pointer to a (char *), return a malloc'ed string containing the
5549 next comma-separated element. Advance the pointer to after the string
5550 scanned, or the end-of-string. Return NULL if at end of string. */
5553 next_comma_elt (const char **pstr)
5557 start = scan_comma_elt (pstr);
5562 return attr_string (start, *pstr - start);
5565 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
5566 is nonzero, build a new attribute, if one does not exist. */
5568 static struct attr_desc *
5569 find_attr (const char *name, int create)
5571 struct attr_desc *attr;
5574 /* Before we resort to using `strcmp', see if the string address matches
5575 anywhere. In most cases, it should have been canonicalized to do so. */
5576 if (name == alternative_name)
5579 index = name[0] & (MAX_ATTRS_INDEX - 1);
5580 for (attr = attrs[index]; attr; attr = attr->next)
5581 if (name == attr->name)
5584 /* Otherwise, do it the slow way. */
5585 for (attr = attrs[index]; attr; attr = attr->next)
5586 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
5592 attr = (struct attr_desc *) oballoc (sizeof (struct attr_desc));
5593 attr->name = attr_string (name, strlen (name));
5594 attr->first_value = attr->default_val = NULL;
5595 attr->is_numeric = attr->negative_ok = attr->is_const = attr->is_special = 0;
5596 attr->unsigned_p = attr->func_units_p = attr->blockage_p = 0;
5597 attr->next = attrs[index];
5598 attrs[index] = attr;
5603 /* Create internal attribute with the given default value. */
5606 make_internal_attr (const char *name, rtx value, int special)
5608 struct attr_desc *attr;
5610 attr = find_attr (name, 1);
5611 if (attr->default_val)
5614 attr->is_numeric = 1;
5616 attr->is_special = (special & ATTR_SPECIAL) != 0;
5617 attr->negative_ok = (special & ATTR_NEGATIVE_OK) != 0;
5618 attr->unsigned_p = (special & ATTR_UNSIGNED) != 0;
5619 attr->func_units_p = (special & ATTR_FUNC_UNITS) != 0;
5620 attr->blockage_p = (special & ATTR_BLOCKAGE) != 0;
5621 attr->default_val = get_attr_value (value, attr, -2);
5624 /* Find the most used value of an attribute. */
5626 static struct attr_value *
5627 find_most_used (struct attr_desc *attr)
5629 struct attr_value *av;
5630 struct attr_value *most_used;
5636 for (av = attr->first_value; av; av = av->next)
5637 if (av->num_insns > nuses)
5638 nuses = av->num_insns, most_used = av;
5643 /* If an attribute only has a single value used, return it. Otherwise
5647 find_single_value (struct attr_desc *attr)
5649 struct attr_value *av;
5652 unique_value = NULL;
5653 for (av = attr->first_value; av; av = av->next)
5659 unique_value = av->value;
5662 return unique_value;
5665 /* Return (attr_value "n") */
5668 make_numeric_value (int n)
5670 static rtx int_values[20];
5677 if (n < 20 && int_values[n])
5678 return int_values[n];
5680 p = attr_printf (MAX_DIGITS, "%d", n);
5681 exp = attr_rtx (CONST_STRING, p);
5684 int_values[n] = exp;
5690 extend_range (struct range *range, int min, int max)
5692 if (range->min > min)
5694 if (range->max < max)
5699 copy_rtx_unchanging (rtx orig)
5701 if (ATTR_IND_SIMPLIFIED_P (orig) || ATTR_CURR_SIMPLIFIED_P (orig))
5704 ATTR_CURR_SIMPLIFIED_P (orig) = 1;
5708 /* Determine if an insn has a constant number of delay slots, i.e., the
5709 number of delay slots is not a function of the length of the insn. */
5712 write_const_num_delay_slots (void)
5714 struct attr_desc *attr = find_attr ("*num_delay_slots", 0);
5715 struct attr_value *av;
5716 struct insn_ent *ie;
5720 printf ("int\nconst_num_delay_slots (rtx insn)\n");
5722 printf (" switch (recog_memoized (insn))\n");
5725 for (av = attr->first_value; av; av = av->next)
5728 walk_attr_value (av->value);
5731 for (ie = av->first_insn; ie; ie = ie->next)
5732 if (ie->insn_code != -1)
5733 printf (" case %d:\n", ie->insn_code);
5734 printf (" return 0;\n");
5738 printf (" default:\n");
5739 printf (" return 1;\n");
5740 printf (" }\n}\n\n");
5745 main (int argc, char **argv)
5748 struct attr_desc *attr;
5749 struct insn_def *id;
5753 progname = "genattrtab";
5756 fatal ("no input file name");
5758 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
5759 return (FATAL_EXIT_CODE);
5761 obstack_init (hash_obstack);
5762 obstack_init (temp_obstack);
5764 /* Set up true and false rtx's */
5765 true_rtx = rtx_alloc (CONST_INT);
5766 XWINT (true_rtx, 0) = 1;
5767 false_rtx = rtx_alloc (CONST_INT);
5768 XWINT (false_rtx, 0) = 0;
5769 ATTR_IND_SIMPLIFIED_P (true_rtx) = ATTR_IND_SIMPLIFIED_P (false_rtx) = 1;
5770 ATTR_PERMANENT_P (true_rtx) = ATTR_PERMANENT_P (false_rtx) = 1;
5772 alternative_name = attr_string ("alternative", strlen ("alternative"));
5774 printf ("/* Generated automatically by the program `genattrtab'\n\
5775 from the machine description file `md'. */\n\n");
5777 /* Read the machine description. */
5779 initiate_automaton_gen (argc, argv);
5784 desc = read_md_rtx (&lineno, &insn_code_number);
5788 switch (GET_CODE (desc))
5791 case DEFINE_PEEPHOLE:
5792 case DEFINE_ASM_ATTRIBUTES:
5793 gen_insn (desc, lineno);
5797 gen_attr (desc, lineno);
5801 gen_delay (desc, lineno);
5804 case DEFINE_FUNCTION_UNIT:
5805 gen_unit (desc, lineno);
5808 case DEFINE_CPU_UNIT:
5809 gen_cpu_unit (desc);
5812 case DEFINE_QUERY_CPU_UNIT:
5813 gen_query_cpu_unit (desc);
5821 gen_excl_set (desc);
5825 gen_presence_set (desc);
5828 case FINAL_PRESENCE_SET:
5829 gen_final_presence_set (desc);
5833 gen_absence_set (desc);
5836 case FINAL_ABSENCE_SET:
5837 gen_final_absence_set (desc);
5840 case DEFINE_AUTOMATON:
5841 gen_automaton (desc);
5844 case AUTOMATA_OPTION:
5845 gen_automata_option (desc);
5848 case DEFINE_RESERVATION:
5852 case DEFINE_INSN_RESERVATION:
5853 gen_insn_reserv (desc);
5859 if (GET_CODE (desc) != DEFINE_ASM_ATTRIBUTES)
5860 insn_index_number++;
5864 return FATAL_EXIT_CODE;
5868 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
5869 if (! got_define_asm_attributes)
5871 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
5872 XVEC (tem, 0) = rtvec_alloc (0);
5876 /* Expand DEFINE_DELAY information into new attribute. */
5880 if (num_units || num_dfa_decls)
5882 /* Expand DEFINE_FUNCTION_UNIT information into new attributes. */
5884 /* Build DFA, output some functions and expand DFA information
5885 into new attributes. */
5889 printf ("#include \"config.h\"\n");
5890 printf ("#include \"system.h\"\n");
5891 printf ("#include \"coretypes.h\"\n");
5892 printf ("#include \"tm.h\"\n");
5893 printf ("#include \"rtl.h\"\n");
5894 printf ("#include \"tm_p.h\"\n");
5895 printf ("#include \"insn-config.h\"\n");
5896 printf ("#include \"recog.h\"\n");
5897 printf ("#include \"regs.h\"\n");
5898 printf ("#include \"real.h\"\n");
5899 printf ("#include \"output.h\"\n");
5900 printf ("#include \"insn-attr.h\"\n");
5901 printf ("#include \"toplev.h\"\n");
5902 printf ("#include \"flags.h\"\n");
5903 printf ("#include \"function.h\"\n");
5905 printf ("#define operands recog_data.operand\n\n");
5907 /* Make `insn_alternatives'. */
5908 insn_alternatives = (int *) oballoc (insn_code_number * sizeof (int));
5909 for (id = defs; id; id = id->next)
5910 if (id->insn_code >= 0)
5911 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
5913 /* Make `insn_n_alternatives'. */
5914 insn_n_alternatives = (int *) oballoc (insn_code_number * sizeof (int));
5915 for (id = defs; id; id = id->next)
5916 if (id->insn_code >= 0)
5917 insn_n_alternatives[id->insn_code] = id->num_alternatives;
5919 /* Prepare to write out attribute subroutines by checking everything stored
5920 away and building the attribute cases. */
5924 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5925 for (attr = attrs[i]; attr; attr = attr->next)
5926 attr->default_val->value
5927 = check_attr_value (attr->default_val->value, attr);
5930 return FATAL_EXIT_CODE;
5932 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5933 for (attr = attrs[i]; attr; attr = attr->next)
5936 /* Construct extra attributes for `length'. */
5937 make_length_attrs ();
5939 /* Perform any possible optimizations to speed up compilation. */
5942 /* Now write out all the `gen_attr_...' routines. Do these before the
5943 special routines (specifically before write_function_unit_info), so
5944 that they get defined before they are used. */
5946 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5947 for (attr = attrs[i]; attr; attr = attr->next)
5949 if (! attr->is_special && ! attr->is_const)
5954 = (attr->name [0] == '*'
5955 && strcmp (&attr->name [1], INSN_ALTS_FUNC_NAME) == 0);
5957 printf ("\n#if AUTOMATON_ALTS\n");
5958 write_attr_get (attr);
5960 printf ("#endif\n\n");
5964 /* Write out delay eligibility information, if DEFINE_DELAY present.
5965 (The function to compute the number of delay slots will be written
5969 write_eligible_delay ("delay");
5970 if (have_annul_true)
5971 write_eligible_delay ("annul_true");
5972 if (have_annul_false)
5973 write_eligible_delay ("annul_false");
5976 if (num_units || num_dfa_decls)
5978 /* Write out information about function units. */
5979 write_function_unit_info ();
5980 /* Output code for pipeline hazards recognition based on DFA
5981 (deterministic finite state automata. */
5985 /* Write out constant delay slot info. */
5986 write_const_num_delay_slots ();
5988 write_length_unit_log ();
5991 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
5994 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
5996 get_insn_name (int code ATTRIBUTE_UNUSED)