Makefile.in (stmp-docobjdir): New target; ensure $docobjdir exists.
[official-gcc.git] / gcc / genattrtab.c
blob30a889ca70d5c359cae89540e7fbb4a6ee4a9970
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
11 version.
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
16 for more details.
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
21 02111-1307, USA. */
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
34 expression).
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
47 `get_attr_length'.
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
54 used.
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
78 will be written.
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
94 (see attr_rtx).
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))
103 #include "bconfig.h"
104 #include "system.h"
105 #include "coretypes.h"
106 #include "tm.h"
107 #include "rtl.h"
108 #include "ggc.h"
109 #include "gensupport.h"
111 #ifdef HAVE_SYS_RESOURCE_H
112 # include <sys/resource.h>
113 #endif
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. */
117 #include "obstack.h"
118 #include "errors.h"
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. */
136 struct insn_def
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
149 list. */
151 struct insn_ent
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
161 value. */
163 struct attr_value
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. */
174 struct attr_desc
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 */
188 unsigned static_p : 1; /* Make the output function static. */
191 #define NULL_ATTR (struct attr_desc *) NULL
193 /* A range of values. */
195 struct range
197 int min;
198 int max;
201 /* Structure for each DEFINE_DELAY. */
203 struct delay_desc
205 rtx def; /* DEFINE_DELAY expression. */
206 struct delay_desc *next; /* Next DEFINE_DELAY. */
207 int num; /* Number of DEFINE_DELAY, starting at 1. */
208 int lineno; /* Line number. */
211 /* Record information about each DEFINE_FUNCTION_UNIT. */
213 struct function_unit_op
215 rtx condexp; /* Expression TRUE for applicable insn. */
216 struct function_unit_op *next; /* Next operation for this function unit. */
217 int num; /* Ordinal for this operation type in unit. */
218 int ready; /* Cost until data is ready. */
219 int issue_delay; /* Cost until unit can accept another insn. */
220 rtx conflict_exp; /* Expression TRUE for insns incurring issue delay. */
221 rtx issue_exp; /* Expression computing issue delay. */
222 int lineno; /* Line number. */
225 /* Record information about each function unit mentioned in a
226 DEFINE_FUNCTION_UNIT. */
228 struct function_unit
230 const char *name; /* Function unit name. */
231 struct function_unit *next; /* Next function unit. */
232 int num; /* Ordinal of this unit type. */
233 int multiplicity; /* Number of units of this type. */
234 int simultaneity; /* Maximum number of simultaneous insns
235 on this function unit or 0 if unlimited. */
236 rtx condexp; /* Expression TRUE for insn needing unit. */
237 int num_opclasses; /* Number of different operation types. */
238 struct function_unit_op *ops; /* Pointer to first operation type. */
239 int needs_conflict_function; /* Nonzero if a conflict function required. */
240 int needs_blockage_function; /* Nonzero if a blockage function required. */
241 int needs_range_function; /* Nonzero if blockage range function needed. */
242 rtx default_cost; /* Conflict cost, if constant. */
243 struct range issue_delay; /* Range of issue delay values. */
244 int max_blockage; /* Maximum time an insn blocks the unit. */
245 int first_lineno; /* First seen line number. */
248 /* Listheads of above structures. */
250 /* This one is indexed by the first character of the attribute name. */
251 #define MAX_ATTRS_INDEX 256
252 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
253 static struct insn_def *defs;
254 static struct delay_desc *delays;
255 static struct function_unit *units;
257 /* An expression where all the unknown terms are EQ_ATTR tests can be
258 rearranged into a COND provided we can enumerate all possible
259 combinations of the unknown values. The set of combinations become the
260 tests of the COND; the value of the expression given that combination is
261 computed and becomes the corresponding value. To do this, we must be
262 able to enumerate all values for each attribute used in the expression
263 (currently, we give up if we find a numeric attribute).
265 If the set of EQ_ATTR tests used in an expression tests the value of N
266 different attributes, the list of all possible combinations can be made
267 by walking the N-dimensional attribute space defined by those
268 attributes. We record each of these as a struct dimension.
270 The algorithm relies on sharing EQ_ATTR nodes: if two nodes in an
271 expression are the same, the will also have the same address. We find
272 all the EQ_ATTR nodes by marking them ATTR_EQ_ATTR_P. This bit later
273 represents the value of an EQ_ATTR node, so once all nodes are marked,
274 they are also given an initial value of FALSE.
276 We then separate the set of EQ_ATTR nodes into dimensions for each
277 attribute and put them on the VALUES list. Terms are added as needed by
278 `add_values_to_cover' so that all possible values of the attribute are
279 tested.
281 Each dimension also has a current value. This is the node that is
282 currently considered to be TRUE. If this is one of the nodes added by
283 `add_values_to_cover', all the EQ_ATTR tests in the original expression
284 will be FALSE. Otherwise, only the CURRENT_VALUE will be true.
286 NUM_VALUES is simply the length of the VALUES list and is there for
287 convenience.
289 Once the dimensions are created, the algorithm enumerates all possible
290 values and computes the current value of the given expression. */
292 struct dimension
294 struct attr_desc *attr; /* Attribute for this dimension. */
295 rtx values; /* List of attribute values used. */
296 rtx current_value; /* Position in the list for the TRUE value. */
297 int num_values; /* Length of the values list. */
300 /* Other variables. */
302 static int insn_code_number;
303 static int insn_index_number;
304 static int got_define_asm_attributes;
305 static int must_extract;
306 static int must_constrain;
307 static int address_used;
308 static int length_used;
309 static int num_delays;
310 static int have_annul_true, have_annul_false;
311 static int num_units, num_unit_opclasses;
312 static int num_insn_ents;
314 int num_dfa_decls;
316 /* Used as operand to `operate_exp': */
318 enum operator {PLUS_OP, MINUS_OP, POS_MINUS_OP, EQ_OP, OR_OP, ORX_OP, MAX_OP, MIN_OP, RANGE_OP};
320 /* Stores, for each insn code, the number of constraint alternatives. */
322 static int *insn_n_alternatives;
324 /* Stores, for each insn code, a bitmap that has bits on for each possible
325 alternative. */
327 static int *insn_alternatives;
329 /* If nonzero, assume that the `alternative' attr has this value.
330 This is the hashed, unique string for the numeral
331 whose value is chosen alternative. */
333 static const char *current_alternative_string;
335 /* Used to simplify expressions. */
337 static rtx true_rtx, false_rtx;
339 /* Used to reduce calls to `strcmp' */
341 static char *alternative_name;
343 /* Indicate that REG_DEAD notes are valid if dead_or_set_p is ever
344 called. */
346 int reload_completed = 0;
348 /* Some machines test `optimize' in macros called from rtlanal.c, so we need
349 to define it here. */
351 int optimize = 0;
353 /* Simplify an expression. Only call the routine if there is something to
354 simplify. */
355 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
356 (ATTR_IND_SIMPLIFIED_P (EXP) || ATTR_CURR_SIMPLIFIED_P (EXP) ? (EXP) \
357 : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
359 /* Simplify (eq_attr ("alternative") ...)
360 when we are working with a particular alternative. */
361 #define SIMPLIFY_ALTERNATIVE(EXP) \
362 if (current_alternative_string \
363 && GET_CODE ((EXP)) == EQ_ATTR \
364 && XSTR ((EXP), 0) == alternative_name) \
365 (EXP) = (XSTR ((EXP), 1) == current_alternative_string \
366 ? true_rtx : false_rtx);
368 /* These are referenced by rtlanal.c and hence need to be defined somewhere.
369 They won't actually be used. */
371 rtx global_rtl[GR_MAX];
372 rtx pic_offset_table_rtx;
374 static void attr_hash_add_rtx (int, rtx);
375 static void attr_hash_add_string (int, char *);
376 static rtx attr_rtx (enum rtx_code, ...);
377 static rtx attr_rtx_1 (enum rtx_code, va_list);
378 static char *attr_string (const char *, int);
379 static rtx check_attr_value (rtx, struct attr_desc *);
380 static rtx convert_set_attr_alternative (rtx, struct insn_def *);
381 static rtx convert_set_attr (rtx, struct insn_def *);
382 static void check_defs (void);
383 static rtx make_canonical (struct attr_desc *, rtx);
384 static struct attr_value *get_attr_value (rtx, struct attr_desc *, int);
385 static rtx copy_rtx_unchanging (rtx);
386 static rtx copy_boolean (rtx);
387 static void expand_delays (void);
388 static rtx operate_exp (enum operator, rtx, rtx);
389 static void expand_units (void);
390 static rtx simplify_knowing (rtx, rtx);
391 static rtx encode_units_mask (rtx);
392 static void fill_attr (struct attr_desc *);
393 static rtx substitute_address (rtx, rtx (*) (rtx), rtx (*) (rtx));
394 static void make_length_attrs (void);
395 static rtx identity_fn (rtx);
396 static rtx zero_fn (rtx);
397 static rtx one_fn (rtx);
398 static rtx max_fn (rtx);
399 static void write_length_unit_log (void);
400 static rtx simplify_cond (rtx, int, int);
401 static rtx simplify_by_exploding (rtx);
402 static int find_and_mark_used_attributes (rtx, rtx *, int *);
403 static void unmark_used_attributes (rtx, struct dimension *, int);
404 static int add_values_to_cover (struct dimension *);
405 static int increment_current_value (struct dimension *, int);
406 static rtx test_for_current_value (struct dimension *, int);
407 static rtx simplify_with_current_value (rtx, struct dimension *, int);
408 static rtx simplify_with_current_value_aux (rtx);
409 static void clear_struct_flag (rtx);
410 static int count_sub_rtxs (rtx, int);
411 static void remove_insn_ent (struct attr_value *, struct insn_ent *);
412 static void insert_insn_ent (struct attr_value *, struct insn_ent *);
413 static rtx insert_right_side (enum rtx_code, rtx, rtx, int, int);
414 static rtx make_alternative_compare (int);
415 static int compute_alternative_mask (rtx, enum rtx_code);
416 static rtx evaluate_eq_attr (rtx, rtx, int, int);
417 static rtx simplify_and_tree (rtx, rtx *, int, int);
418 static rtx simplify_or_tree (rtx, rtx *, int, int);
419 static rtx simplify_test_exp (rtx, int, int);
420 static rtx simplify_test_exp_in_temp (rtx, int, int);
421 static void optimize_attrs (void);
422 static void gen_attr (rtx, int);
423 static int count_alternatives (rtx);
424 static int compares_alternatives_p (rtx);
425 static int contained_in_p (rtx, rtx);
426 static void gen_insn (rtx, int);
427 static void gen_delay (rtx, int);
428 static void gen_unit (rtx, int);
429 static void write_test_expr (rtx, int);
430 static int max_attr_value (rtx, int*);
431 static int or_attr_value (rtx, int*);
432 static void walk_attr_value (rtx);
433 static void write_attr_get (struct attr_desc *);
434 static rtx eliminate_known_true (rtx, rtx, int, int);
435 static void write_attr_set (struct attr_desc *, int, rtx,
436 const char *, const char *, rtx,
437 int, int);
438 static void write_attr_case (struct attr_desc *, struct attr_value *,
439 int, const char *, const char *, int, rtx);
440 static void write_unit_name (const char *, int, const char *);
441 static void write_attr_valueq (struct attr_desc *, const char *);
442 static void write_attr_value (struct attr_desc *, rtx);
443 static void write_upcase (const char *);
444 static void write_indent (int);
445 static void write_eligible_delay (const char *);
446 static void write_function_unit_info (void);
447 static void write_complex_function (struct function_unit *, const char *,
448 const char *);
449 static int write_expr_attr_cache (rtx, struct attr_desc *);
450 static void write_toplevel_expr (rtx);
451 static void write_const_num_delay_slots (void);
452 static char *next_comma_elt (const char **);
453 static struct attr_desc *find_attr (const char *, int);
454 static struct attr_value *find_most_used (struct attr_desc *);
455 static rtx find_single_value (struct attr_desc *);
456 static void extend_range (struct range *, int, int);
457 static rtx attr_eq (const char *, const char *);
458 static const char *attr_numeral (int);
459 static int attr_equal_p (rtx, rtx);
460 static rtx attr_copy_rtx (rtx);
461 static int attr_rtx_cost (rtx);
463 #define oballoc(size) obstack_alloc (hash_obstack, size)
465 /* Hash table for sharing RTL and strings. */
467 /* Each hash table slot is a bucket containing a chain of these structures.
468 Strings are given negative hash codes; RTL expressions are given positive
469 hash codes. */
471 struct attr_hash
473 struct attr_hash *next; /* Next structure in the bucket. */
474 int hashcode; /* Hash code of this rtx or string. */
475 union
477 char *str; /* The string (negative hash codes) */
478 rtx rtl; /* or the RTL recorded here. */
479 } u;
482 /* Now here is the hash table. When recording an RTL, it is added to
483 the slot whose index is the hash code mod the table size. Note
484 that the hash table is used for several kinds of RTL (see attr_rtx)
485 and for strings. While all these live in the same table, they are
486 completely independent, and the hash code is computed differently
487 for each. */
489 #define RTL_HASH_SIZE 4093
490 struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
492 /* Here is how primitive or already-shared RTL's hash
493 codes are made. */
494 #define RTL_HASH(RTL) ((long) (RTL) & 0777777)
496 /* Add an entry to the hash table for RTL with hash code HASHCODE. */
498 static void
499 attr_hash_add_rtx (int hashcode, rtx rtl)
501 struct attr_hash *h;
503 h = obstack_alloc (hash_obstack, sizeof (struct attr_hash));
504 h->hashcode = hashcode;
505 h->u.rtl = rtl;
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. */
512 static void
513 attr_hash_add_string (int hashcode, char *str)
515 struct attr_hash *h;
517 h = obstack_alloc (hash_obstack, sizeof (struct attr_hash));
518 h->hashcode = -hashcode;
519 h->u.str = str;
520 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
521 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
524 /* Generate an RTL expression, but avoid duplicates.
525 Set the ATTR_PERMANENT_P flag for these permanent objects.
527 In some cases we cannot uniquify; then we return an ordinary
528 impermanent rtx with ATTR_PERMANENT_P clear.
530 Args are like gen_rtx, but without the mode:
532 rtx attr_rtx (code, [element1, ..., elementn]) */
534 static rtx
535 attr_rtx_1 (enum rtx_code code, va_list p)
537 rtx rt_val = NULL_RTX;/* RTX to return to caller... */
538 int hashcode;
539 struct attr_hash *h;
540 struct obstack *old_obstack = rtl_obstack;
542 /* For each of several cases, search the hash table for an existing entry.
543 Use that entry if one is found; otherwise create a new RTL and add it
544 to the table. */
546 if (GET_RTX_CLASS (code) == '1')
548 rtx arg0 = va_arg (p, rtx);
550 /* A permanent object cannot point to impermanent ones. */
551 if (! ATTR_PERMANENT_P (arg0))
553 rt_val = rtx_alloc (code);
554 XEXP (rt_val, 0) = arg0;
555 return rt_val;
558 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
559 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
560 if (h->hashcode == hashcode
561 && GET_CODE (h->u.rtl) == code
562 && XEXP (h->u.rtl, 0) == arg0)
563 return h->u.rtl;
565 if (h == 0)
567 rtl_obstack = hash_obstack;
568 rt_val = rtx_alloc (code);
569 XEXP (rt_val, 0) = arg0;
572 else if (GET_RTX_CLASS (code) == 'c'
573 || GET_RTX_CLASS (code) == '2'
574 || GET_RTX_CLASS (code) == '<')
576 rtx arg0 = va_arg (p, rtx);
577 rtx arg1 = va_arg (p, rtx);
579 /* A permanent object cannot point to impermanent ones. */
580 if (! ATTR_PERMANENT_P (arg0) || ! ATTR_PERMANENT_P (arg1))
582 rt_val = rtx_alloc (code);
583 XEXP (rt_val, 0) = arg0;
584 XEXP (rt_val, 1) = arg1;
585 return rt_val;
588 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
589 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
590 if (h->hashcode == hashcode
591 && GET_CODE (h->u.rtl) == code
592 && XEXP (h->u.rtl, 0) == arg0
593 && XEXP (h->u.rtl, 1) == arg1)
594 return h->u.rtl;
596 if (h == 0)
598 rtl_obstack = hash_obstack;
599 rt_val = rtx_alloc (code);
600 XEXP (rt_val, 0) = arg0;
601 XEXP (rt_val, 1) = arg1;
604 else if (GET_RTX_LENGTH (code) == 1
605 && GET_RTX_FORMAT (code)[0] == 's')
607 char *arg0 = va_arg (p, char *);
609 if (code == SYMBOL_REF)
610 arg0 = attr_string (arg0, strlen (arg0));
612 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
613 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
614 if (h->hashcode == hashcode
615 && GET_CODE (h->u.rtl) == code
616 && XSTR (h->u.rtl, 0) == arg0)
617 return h->u.rtl;
619 if (h == 0)
621 rtl_obstack = hash_obstack;
622 rt_val = rtx_alloc (code);
623 XSTR (rt_val, 0) = arg0;
626 else if (GET_RTX_LENGTH (code) == 2
627 && GET_RTX_FORMAT (code)[0] == 's'
628 && GET_RTX_FORMAT (code)[1] == 's')
630 char *arg0 = va_arg (p, char *);
631 char *arg1 = va_arg (p, char *);
633 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
634 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
635 if (h->hashcode == hashcode
636 && GET_CODE (h->u.rtl) == code
637 && XSTR (h->u.rtl, 0) == arg0
638 && XSTR (h->u.rtl, 1) == arg1)
639 return h->u.rtl;
641 if (h == 0)
643 rtl_obstack = hash_obstack;
644 rt_val = rtx_alloc (code);
645 XSTR (rt_val, 0) = arg0;
646 XSTR (rt_val, 1) = arg1;
649 else if (code == CONST_INT)
651 HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
652 if (arg0 == 0)
653 return false_rtx;
654 else if (arg0 == 1)
655 return true_rtx;
656 else
657 goto nohash;
659 else
661 int i; /* Array indices... */
662 const char *fmt; /* Current rtx's format... */
663 nohash:
664 rt_val = rtx_alloc (code); /* Allocate the storage space. */
666 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
667 for (i = 0; i < GET_RTX_LENGTH (code); i++)
669 switch (*fmt++)
671 case '0': /* Unused field. */
672 break;
674 case 'i': /* An integer? */
675 XINT (rt_val, i) = va_arg (p, int);
676 break;
678 case 'w': /* A wide integer? */
679 XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
680 break;
682 case 's': /* A string? */
683 XSTR (rt_val, i) = va_arg (p, char *);
684 break;
686 case 'e': /* An expression? */
687 case 'u': /* An insn? Same except when printing. */
688 XEXP (rt_val, i) = va_arg (p, rtx);
689 break;
691 case 'E': /* An RTX vector? */
692 XVEC (rt_val, i) = va_arg (p, rtvec);
693 break;
695 default:
696 abort ();
699 return rt_val;
702 rtl_obstack = old_obstack;
703 attr_hash_add_rtx (hashcode, rt_val);
704 ATTR_PERMANENT_P (rt_val) = 1;
705 return rt_val;
708 static rtx
709 attr_rtx (enum rtx_code code, ...)
711 rtx result;
712 va_list p;
714 va_start (p, code);
715 result = attr_rtx_1 (code, p);
716 va_end (p);
717 return result;
720 /* Create a new string printed with the printf line arguments into a space
721 of at most LEN bytes:
723 rtx attr_printf (len, format, [arg1, ..., argn]) */
725 char *
726 attr_printf (unsigned int len, const char *fmt, ...)
728 char str[256];
729 va_list p;
731 va_start (p, fmt);
733 if (len > sizeof str - 1) /* Leave room for \0. */
734 abort ();
736 vsprintf (str, fmt, p);
737 va_end (p);
739 return attr_string (str, strlen (str));
742 static rtx
743 attr_eq (const char *name, const char *value)
745 return attr_rtx (EQ_ATTR, attr_string (name, strlen (name)),
746 attr_string (value, strlen (value)));
749 static const char *
750 attr_numeral (int n)
752 return XSTR (make_numeric_value (n), 0);
755 /* Return a permanent (possibly shared) copy of a string STR (not assumed
756 to be null terminated) with LEN bytes. */
758 static char *
759 attr_string (const char *str, int len)
761 struct attr_hash *h;
762 int hashcode;
763 int i;
764 char *new_str;
766 /* Compute the hash code. */
767 hashcode = (len + 1) * 613 + (unsigned) str[0];
768 for (i = 1; i <= len; i += 2)
769 hashcode = ((hashcode * 613) + (unsigned) str[i]);
770 if (hashcode < 0)
771 hashcode = -hashcode;
773 /* Search the table for the string. */
774 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
775 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
776 && !strncmp (h->u.str, str, len))
777 return h->u.str; /* <-- return if found. */
779 /* Not found; create a permanent copy and add it to the hash table. */
780 new_str = obstack_alloc (hash_obstack, len + 1);
781 memcpy (new_str, str, len);
782 new_str[len] = '\0';
783 attr_hash_add_string (hashcode, new_str);
785 return new_str; /* Return the new string. */
788 /* Check two rtx's for equality of contents,
789 taking advantage of the fact that if both are hashed
790 then they can't be equal unless they are the same object. */
792 static int
793 attr_equal_p (rtx x, rtx y)
795 return (x == y || (! (ATTR_PERMANENT_P (x) && ATTR_PERMANENT_P (y))
796 && rtx_equal_p (x, y)));
799 /* Copy an attribute value expression,
800 descending to all depths, but not copying any
801 permanent hashed subexpressions. */
803 static rtx
804 attr_copy_rtx (rtx orig)
806 rtx copy;
807 int i, j;
808 RTX_CODE code;
809 const char *format_ptr;
811 /* No need to copy a permanent object. */
812 if (ATTR_PERMANENT_P (orig))
813 return orig;
815 code = GET_CODE (orig);
817 switch (code)
819 case REG:
820 case QUEUED:
821 case CONST_INT:
822 case CONST_DOUBLE:
823 case CONST_VECTOR:
824 case SYMBOL_REF:
825 case CODE_LABEL:
826 case PC:
827 case CC0:
828 return orig;
830 default:
831 break;
834 copy = rtx_alloc (code);
835 PUT_MODE (copy, GET_MODE (orig));
836 ATTR_IND_SIMPLIFIED_P (copy) = ATTR_IND_SIMPLIFIED_P (orig);
837 ATTR_CURR_SIMPLIFIED_P (copy) = ATTR_CURR_SIMPLIFIED_P (orig);
838 ATTR_PERMANENT_P (copy) = ATTR_PERMANENT_P (orig);
839 ATTR_EQ_ATTR_P (copy) = ATTR_EQ_ATTR_P (orig);
841 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
843 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
845 switch (*format_ptr++)
847 case 'e':
848 XEXP (copy, i) = XEXP (orig, i);
849 if (XEXP (orig, i) != NULL)
850 XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
851 break;
853 case 'E':
854 case 'V':
855 XVEC (copy, i) = XVEC (orig, i);
856 if (XVEC (orig, i) != NULL)
858 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
859 for (j = 0; j < XVECLEN (copy, i); j++)
860 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
862 break;
864 case 'n':
865 case 'i':
866 XINT (copy, i) = XINT (orig, i);
867 break;
869 case 'w':
870 XWINT (copy, i) = XWINT (orig, i);
871 break;
873 case 's':
874 case 'S':
875 XSTR (copy, i) = XSTR (orig, i);
876 break;
878 default:
879 abort ();
882 return copy;
885 /* Given a test expression for an attribute, ensure it is validly formed.
886 IS_CONST indicates whether the expression is constant for each compiler
887 run (a constant expression may not test any particular insn).
889 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
890 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
891 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
893 Update the string address in EQ_ATTR expression to be the same used
894 in the attribute (or `alternative_name') to speed up subsequent
895 `find_attr' calls and eliminate most `strcmp' calls.
897 Return the new expression, if any. */
900 check_attr_test (rtx exp, int is_const, int lineno)
902 struct attr_desc *attr;
903 struct attr_value *av;
904 const char *name_ptr, *p;
905 rtx orexp, newexp;
907 switch (GET_CODE (exp))
909 case EQ_ATTR:
910 /* Handle negation test. */
911 if (XSTR (exp, 1)[0] == '!')
912 return check_attr_test (attr_rtx (NOT,
913 attr_eq (XSTR (exp, 0),
914 &XSTR (exp, 1)[1])),
915 is_const, lineno);
917 else if (n_comma_elts (XSTR (exp, 1)) == 1)
919 attr = find_attr (XSTR (exp, 0), 0);
920 if (attr == NULL)
922 if (! strcmp (XSTR (exp, 0), "alternative"))
924 XSTR (exp, 0) = alternative_name;
925 /* This can't be simplified any further. */
926 ATTR_IND_SIMPLIFIED_P (exp) = 1;
927 return exp;
929 else
930 fatal ("unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
933 if (is_const && ! attr->is_const)
934 fatal ("constant expression uses insn attribute `%s' in EQ_ATTR",
935 XSTR (exp, 0));
937 /* Copy this just to make it permanent,
938 so expressions using it can be permanent too. */
939 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
941 /* It shouldn't be possible to simplify the value given to a
942 constant attribute, so don't expand this until it's time to
943 write the test expression. */
944 if (attr->is_const)
945 ATTR_IND_SIMPLIFIED_P (exp) = 1;
947 if (attr->is_numeric)
949 for (p = XSTR (exp, 1); *p; p++)
950 if (! ISDIGIT (*p))
951 fatal ("attribute `%s' takes only numeric values",
952 XSTR (exp, 0));
954 else
956 for (av = attr->first_value; av; av = av->next)
957 if (GET_CODE (av->value) == CONST_STRING
958 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
959 break;
961 if (av == NULL)
962 fatal ("unknown value `%s' for `%s' attribute",
963 XSTR (exp, 1), XSTR (exp, 0));
966 else
968 /* Make an IOR tree of the possible values. */
969 orexp = false_rtx;
970 name_ptr = XSTR (exp, 1);
971 while ((p = next_comma_elt (&name_ptr)) != NULL)
973 newexp = attr_eq (XSTR (exp, 0), p);
974 orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
977 return check_attr_test (orexp, is_const, lineno);
979 break;
981 case ATTR_FLAG:
982 break;
984 case CONST_INT:
985 /* Either TRUE or FALSE. */
986 if (XWINT (exp, 0))
987 return true_rtx;
988 else
989 return false_rtx;
991 case IOR:
992 case AND:
993 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
994 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const, lineno);
995 break;
997 case NOT:
998 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
999 break;
1001 case MATCH_INSN:
1002 case MATCH_OPERAND:
1003 if (is_const)
1004 fatal ("RTL operator \"%s\" not valid in constant attribute test",
1005 GET_RTX_NAME (GET_CODE (exp)));
1006 /* These cases can't be simplified. */
1007 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1008 break;
1010 case LE: case LT: case GT: case GE:
1011 case LEU: case LTU: case GTU: case GEU:
1012 case NE: case EQ:
1013 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
1014 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
1015 exp = attr_rtx (GET_CODE (exp),
1016 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
1017 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
1018 /* These cases can't be simplified. */
1019 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1020 break;
1022 case SYMBOL_REF:
1023 if (is_const)
1025 /* These cases are valid for constant attributes, but can't be
1026 simplified. */
1027 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1028 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1029 break;
1031 default:
1032 fatal ("RTL operator \"%s\" not valid in attribute test",
1033 GET_RTX_NAME (GET_CODE (exp)));
1036 return exp;
1039 /* Given an expression, ensure that it is validly formed and that all named
1040 attribute values are valid for the given attribute. Issue a fatal error
1041 if not. If no attribute is specified, assume a numeric attribute.
1043 Return a perhaps modified replacement expression for the value. */
1045 static rtx
1046 check_attr_value (rtx exp, struct attr_desc *attr)
1048 struct attr_value *av;
1049 const char *p;
1050 int i;
1052 switch (GET_CODE (exp))
1054 case CONST_INT:
1055 if (attr && ! attr->is_numeric)
1057 message_with_line (attr->lineno,
1058 "CONST_INT not valid for non-numeric attribute %s",
1059 attr->name);
1060 have_error = 1;
1061 break;
1064 if (INTVAL (exp) < 0 && ! attr->negative_ok)
1066 message_with_line (attr->lineno,
1067 "negative numeric value specified for attribute %s",
1068 attr->name);
1069 have_error = 1;
1070 break;
1072 break;
1074 case CONST_STRING:
1075 if (! strcmp (XSTR (exp, 0), "*"))
1076 break;
1078 if (attr == 0 || attr->is_numeric)
1080 p = XSTR (exp, 0);
1081 if (attr && attr->negative_ok && *p == '-')
1082 p++;
1083 for (; *p; p++)
1084 if (! ISDIGIT (*p))
1086 message_with_line (attr ? attr->lineno : 0,
1087 "non-numeric value for numeric attribute %s",
1088 attr ? attr->name : "internal");
1089 have_error = 1;
1090 break;
1092 break;
1095 for (av = attr->first_value; av; av = av->next)
1096 if (GET_CODE (av->value) == CONST_STRING
1097 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
1098 break;
1100 if (av == NULL)
1102 message_with_line (attr->lineno,
1103 "unknown value `%s' for `%s' attribute",
1104 XSTR (exp, 0), attr ? attr->name : "internal");
1105 have_error = 1;
1107 break;
1109 case IF_THEN_ELSE:
1110 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
1111 attr ? attr->is_const : 0,
1112 attr ? attr->lineno : 0);
1113 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1114 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
1115 break;
1117 case PLUS:
1118 case MINUS:
1119 case MULT:
1120 case DIV:
1121 case MOD:
1122 if (attr && !attr->is_numeric)
1124 message_with_line (attr->lineno,
1125 "invalid operation `%s' for non-numeric attribute value",
1126 GET_RTX_NAME (GET_CODE (exp)));
1127 have_error = 1;
1128 break;
1130 /* FALLTHRU */
1132 case IOR:
1133 case AND:
1134 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1135 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1136 break;
1138 case FFS:
1139 case CLZ:
1140 case CTZ:
1141 case POPCOUNT:
1142 case PARITY:
1143 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1144 break;
1146 case COND:
1147 if (XVECLEN (exp, 0) % 2 != 0)
1149 message_with_line (attr->lineno,
1150 "first operand of COND must have even length");
1151 have_error = 1;
1152 break;
1155 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1157 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
1158 attr ? attr->is_const : 0,
1159 attr ? attr->lineno : 0);
1160 XVECEXP (exp, 0, i + 1)
1161 = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
1164 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1165 break;
1167 case ATTR:
1169 struct attr_desc *attr2 = find_attr (XSTR (exp, 0), 0);
1170 if (attr2 == NULL)
1172 message_with_line (attr ? attr->lineno : 0,
1173 "unknown attribute `%s' in ATTR",
1174 XSTR (exp, 0));
1175 have_error = 1;
1177 else if (attr && attr->is_const && ! attr2->is_const)
1179 message_with_line (attr->lineno,
1180 "non-constant attribute `%s' referenced from `%s'",
1181 XSTR (exp, 0), attr->name);
1182 have_error = 1;
1184 else if (attr
1185 && (attr->is_numeric != attr2->is_numeric
1186 || (! attr->negative_ok && attr2->negative_ok)))
1188 message_with_line (attr->lineno,
1189 "numeric attribute mismatch calling `%s' from `%s'",
1190 XSTR (exp, 0), attr->name);
1191 have_error = 1;
1194 break;
1196 case SYMBOL_REF:
1197 /* A constant SYMBOL_REF is valid as a constant attribute test and
1198 is expanded later by make_canonical into a COND. In a non-constant
1199 attribute test, it is left be. */
1200 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1202 default:
1203 message_with_line (attr ? attr->lineno : 0,
1204 "invalid operation `%s' for attribute value",
1205 GET_RTX_NAME (GET_CODE (exp)));
1206 have_error = 1;
1207 break;
1210 return exp;
1213 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1214 It becomes a COND with each test being (eq_attr "alternative "n") */
1216 static rtx
1217 convert_set_attr_alternative (rtx exp, struct insn_def *id)
1219 int num_alt = id->num_alternatives;
1220 rtx condexp;
1221 int i;
1223 if (XVECLEN (exp, 1) != num_alt)
1225 message_with_line (id->lineno,
1226 "bad number of entries in SET_ATTR_ALTERNATIVE");
1227 have_error = 1;
1228 return NULL_RTX;
1231 /* Make a COND with all tests but the last. Select the last value via the
1232 default. */
1233 condexp = rtx_alloc (COND);
1234 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1236 for (i = 0; i < num_alt - 1; i++)
1238 const char *p;
1239 p = attr_numeral (i);
1241 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1242 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1245 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1247 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1250 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1251 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1253 static rtx
1254 convert_set_attr (rtx exp, struct insn_def *id)
1256 rtx newexp;
1257 const char *name_ptr;
1258 char *p;
1259 int n;
1261 /* See how many alternative specified. */
1262 n = n_comma_elts (XSTR (exp, 1));
1263 if (n == 1)
1264 return attr_rtx (SET,
1265 attr_rtx (ATTR, XSTR (exp, 0)),
1266 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1268 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1269 XSTR (newexp, 0) = XSTR (exp, 0);
1270 XVEC (newexp, 1) = rtvec_alloc (n);
1272 /* Process each comma-separated name. */
1273 name_ptr = XSTR (exp, 1);
1274 n = 0;
1275 while ((p = next_comma_elt (&name_ptr)) != NULL)
1276 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1278 return convert_set_attr_alternative (newexp, id);
1281 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1282 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1283 expressions. */
1285 static void
1286 check_defs (void)
1288 struct insn_def *id;
1289 struct attr_desc *attr;
1290 int i;
1291 rtx value;
1293 for (id = defs; id; id = id->next)
1295 if (XVEC (id->def, id->vec_idx) == NULL)
1296 continue;
1298 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1300 value = XVECEXP (id->def, id->vec_idx, i);
1301 switch (GET_CODE (value))
1303 case SET:
1304 if (GET_CODE (XEXP (value, 0)) != ATTR)
1306 message_with_line (id->lineno, "bad attribute set");
1307 have_error = 1;
1308 value = NULL_RTX;
1310 break;
1312 case SET_ATTR_ALTERNATIVE:
1313 value = convert_set_attr_alternative (value, id);
1314 break;
1316 case SET_ATTR:
1317 value = convert_set_attr (value, id);
1318 break;
1320 default:
1321 message_with_line (id->lineno, "invalid attribute code %s",
1322 GET_RTX_NAME (GET_CODE (value)));
1323 have_error = 1;
1324 value = NULL_RTX;
1326 if (value == NULL_RTX)
1327 continue;
1329 if ((attr = find_attr (XSTR (XEXP (value, 0), 0), 0)) == NULL)
1331 message_with_line (id->lineno, "unknown attribute %s",
1332 XSTR (XEXP (value, 0), 0));
1333 have_error = 1;
1334 continue;
1337 XVECEXP (id->def, id->vec_idx, i) = value;
1338 XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1343 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1344 expressions by converting them into a COND. This removes cases from this
1345 program. Also, replace an attribute value of "*" with the default attribute
1346 value. */
1348 static rtx
1349 make_canonical (struct attr_desc *attr, rtx exp)
1351 int i;
1352 rtx newexp;
1354 switch (GET_CODE (exp))
1356 case CONST_INT:
1357 exp = make_numeric_value (INTVAL (exp));
1358 break;
1360 case CONST_STRING:
1361 if (! strcmp (XSTR (exp, 0), "*"))
1363 if (attr == 0 || attr->default_val == 0)
1364 fatal ("(attr_value \"*\") used in invalid context");
1365 exp = attr->default_val->value;
1368 break;
1370 case SYMBOL_REF:
1371 if (!attr->is_const || ATTR_IND_SIMPLIFIED_P (exp))
1372 break;
1373 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1374 This makes the COND something that won't be considered an arbitrary
1375 expression by walk_attr_value. */
1376 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1377 exp = check_attr_value (exp, attr);
1378 break;
1380 case IF_THEN_ELSE:
1381 newexp = rtx_alloc (COND);
1382 XVEC (newexp, 0) = rtvec_alloc (2);
1383 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1384 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1386 XEXP (newexp, 1) = XEXP (exp, 2);
1388 exp = newexp;
1389 /* Fall through to COND case since this is now a COND. */
1391 case COND:
1393 int allsame = 1;
1394 rtx defval;
1396 /* First, check for degenerate COND. */
1397 if (XVECLEN (exp, 0) == 0)
1398 return make_canonical (attr, XEXP (exp, 1));
1399 defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1401 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1403 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1404 XVECEXP (exp, 0, i + 1)
1405 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1406 if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1407 allsame = 0;
1409 if (allsame)
1410 return defval;
1412 break;
1414 default:
1415 break;
1418 return exp;
1421 static rtx
1422 copy_boolean (rtx exp)
1424 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1425 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1426 copy_boolean (XEXP (exp, 1)));
1427 return exp;
1430 /* Given a value and an attribute description, return a `struct attr_value *'
1431 that represents that value. This is either an existing structure, if the
1432 value has been previously encountered, or a newly-created structure.
1434 `insn_code' is the code of an insn whose attribute has the specified
1435 value (-2 if not processing an insn). We ensure that all insns for
1436 a given value have the same number of alternatives if the value checks
1437 alternatives. */
1439 static struct attr_value *
1440 get_attr_value (rtx value, struct attr_desc *attr, int insn_code)
1442 struct attr_value *av;
1443 int num_alt = 0;
1445 value = make_canonical (attr, value);
1446 if (compares_alternatives_p (value))
1448 if (insn_code < 0 || insn_alternatives == NULL)
1449 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1450 else
1451 num_alt = insn_alternatives[insn_code];
1454 for (av = attr->first_value; av; av = av->next)
1455 if (rtx_equal_p (value, av->value)
1456 && (num_alt == 0 || av->first_insn == NULL
1457 || insn_alternatives[av->first_insn->insn_code]))
1458 return av;
1460 av = oballoc (sizeof (struct attr_value));
1461 av->value = value;
1462 av->next = attr->first_value;
1463 attr->first_value = av;
1464 av->first_insn = NULL;
1465 av->num_insns = 0;
1466 av->has_asm_insn = 0;
1468 return av;
1471 /* After all DEFINE_DELAYs have been read in, create internal attributes
1472 to generate the required routines.
1474 First, we compute the number of delay slots for each insn (as a COND of
1475 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1476 delay type is specified, we compute a similar function giving the
1477 DEFINE_DELAY ordinal for each insn.
1479 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1480 tells whether a given insn can be in that delay slot.
1482 Normal attribute filling and optimization expands these to contain the
1483 information needed to handle delay slots. */
1485 static void
1486 expand_delays (void)
1488 struct delay_desc *delay;
1489 rtx condexp;
1490 rtx newexp;
1491 int i;
1492 char *p;
1494 /* First, generate data for `num_delay_slots' function. */
1496 condexp = rtx_alloc (COND);
1497 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1498 XEXP (condexp, 1) = make_numeric_value (0);
1500 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1502 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1503 XVECEXP (condexp, 0, i + 1)
1504 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1507 make_internal_attr ("*num_delay_slots", condexp, ATTR_NONE);
1509 /* If more than one delay type, do the same for computing the delay type. */
1510 if (num_delays > 1)
1512 condexp = rtx_alloc (COND);
1513 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1514 XEXP (condexp, 1) = make_numeric_value (0);
1516 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1518 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1519 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1522 make_internal_attr ("*delay_type", condexp, ATTR_SPECIAL);
1525 /* For each delay possibility and delay slot, compute an eligibility
1526 attribute for non-annulled insns and for each type of annulled (annul
1527 if true and annul if false). */
1528 for (delay = delays; delay; delay = delay->next)
1530 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1532 condexp = XVECEXP (delay->def, 1, i);
1533 if (condexp == 0)
1534 condexp = false_rtx;
1535 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1536 make_numeric_value (1), make_numeric_value (0));
1538 p = attr_printf (sizeof "*delay__" + MAX_DIGITS * 2,
1539 "*delay_%d_%d", delay->num, i / 3);
1540 make_internal_attr (p, newexp, ATTR_SPECIAL);
1542 if (have_annul_true)
1544 condexp = XVECEXP (delay->def, 1, i + 1);
1545 if (condexp == 0) condexp = false_rtx;
1546 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1547 make_numeric_value (1),
1548 make_numeric_value (0));
1549 p = attr_printf (sizeof "*annul_true__" + MAX_DIGITS * 2,
1550 "*annul_true_%d_%d", delay->num, i / 3);
1551 make_internal_attr (p, newexp, ATTR_SPECIAL);
1554 if (have_annul_false)
1556 condexp = XVECEXP (delay->def, 1, i + 2);
1557 if (condexp == 0) condexp = false_rtx;
1558 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1559 make_numeric_value (1),
1560 make_numeric_value (0));
1561 p = attr_printf (sizeof "*annul_false__" + MAX_DIGITS * 2,
1562 "*annul_false_%d_%d", delay->num, i / 3);
1563 make_internal_attr (p, newexp, ATTR_SPECIAL);
1569 /* This function is given a left and right side expression and an operator.
1570 Each side is a conditional expression, each alternative of which has a
1571 numerical value. The function returns another conditional expression
1572 which, for every possible set of condition values, returns a value that is
1573 the operator applied to the values of the two sides.
1575 Since this is called early, it must also support IF_THEN_ELSE. */
1577 static rtx
1578 operate_exp (enum operator op, rtx left, rtx right)
1580 int left_value, right_value;
1581 rtx newexp;
1582 int i;
1584 /* If left is a string, apply operator to it and the right side. */
1585 if (GET_CODE (left) == CONST_STRING)
1587 /* If right is also a string, just perform the operation. */
1588 if (GET_CODE (right) == CONST_STRING)
1590 left_value = atoi (XSTR (left, 0));
1591 right_value = atoi (XSTR (right, 0));
1592 switch (op)
1594 case PLUS_OP:
1595 i = left_value + right_value;
1596 break;
1598 case MINUS_OP:
1599 i = left_value - right_value;
1600 break;
1602 case POS_MINUS_OP: /* The positive part of LEFT - RIGHT. */
1603 if (left_value > right_value)
1604 i = left_value - right_value;
1605 else
1606 i = 0;
1607 break;
1609 case OR_OP:
1610 case ORX_OP:
1611 i = left_value | right_value;
1612 break;
1614 case EQ_OP:
1615 i = left_value == right_value;
1616 break;
1618 case RANGE_OP:
1619 i = (left_value << (HOST_BITS_PER_INT / 2)) | right_value;
1620 break;
1622 case MAX_OP:
1623 if (left_value > right_value)
1624 i = left_value;
1625 else
1626 i = right_value;
1627 break;
1629 case MIN_OP:
1630 if (left_value < right_value)
1631 i = left_value;
1632 else
1633 i = right_value;
1634 break;
1636 default:
1637 abort ();
1640 if (i == left_value)
1641 return left;
1642 if (i == right_value)
1643 return right;
1644 return make_numeric_value (i);
1646 else if (GET_CODE (right) == IF_THEN_ELSE)
1648 /* Apply recursively to all values within. */
1649 rtx newleft = operate_exp (op, left, XEXP (right, 1));
1650 rtx newright = operate_exp (op, left, XEXP (right, 2));
1651 if (rtx_equal_p (newleft, newright))
1652 return newleft;
1653 return attr_rtx (IF_THEN_ELSE, XEXP (right, 0), newleft, newright);
1655 else if (GET_CODE (right) == COND)
1657 int allsame = 1;
1658 rtx defval;
1660 newexp = rtx_alloc (COND);
1661 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (right, 0));
1662 defval = XEXP (newexp, 1) = operate_exp (op, left, XEXP (right, 1));
1664 for (i = 0; i < XVECLEN (right, 0); i += 2)
1666 XVECEXP (newexp, 0, i) = XVECEXP (right, 0, i);
1667 XVECEXP (newexp, 0, i + 1)
1668 = operate_exp (op, left, XVECEXP (right, 0, i + 1));
1669 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1670 defval))
1671 allsame = 0;
1674 /* If the resulting cond is trivial (all alternatives
1675 give the same value), optimize it away. */
1676 if (allsame)
1677 return operate_exp (op, left, XEXP (right, 1));
1679 return newexp;
1681 else
1682 fatal ("badly formed attribute value");
1685 /* A hack to prevent expand_units from completely blowing up: ORX_OP does
1686 not associate through IF_THEN_ELSE. */
1687 else if (op == ORX_OP && GET_CODE (right) == IF_THEN_ELSE)
1689 return attr_rtx (IOR, left, right);
1692 /* Otherwise, do recursion the other way. */
1693 else if (GET_CODE (left) == IF_THEN_ELSE)
1695 rtx newleft = operate_exp (op, XEXP (left, 1), right);
1696 rtx newright = operate_exp (op, XEXP (left, 2), right);
1697 if (rtx_equal_p (newleft, newright))
1698 return newleft;
1699 return attr_rtx (IF_THEN_ELSE, XEXP (left, 0), newleft, newright);
1701 else if (GET_CODE (left) == COND)
1703 int allsame = 1;
1704 rtx defval;
1706 newexp = rtx_alloc (COND);
1707 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (left, 0));
1708 defval = XEXP (newexp, 1) = operate_exp (op, XEXP (left, 1), right);
1710 for (i = 0; i < XVECLEN (left, 0); i += 2)
1712 XVECEXP (newexp, 0, i) = XVECEXP (left, 0, i);
1713 XVECEXP (newexp, 0, i + 1)
1714 = operate_exp (op, XVECEXP (left, 0, i + 1), right);
1715 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1716 defval))
1717 allsame = 0;
1720 /* If the cond is trivial (all alternatives give the same value),
1721 optimize it away. */
1722 if (allsame)
1723 return operate_exp (op, XEXP (left, 1), right);
1725 /* If the result is the same as the LEFT operand,
1726 just use that. */
1727 if (rtx_equal_p (newexp, left))
1728 return left;
1730 return newexp;
1733 else
1734 fatal ("badly formed attribute value");
1735 /* NOTREACHED */
1736 return NULL;
1739 /* Once all attributes and DEFINE_FUNCTION_UNITs have been read, we
1740 construct a number of attributes.
1742 The first produces a function `function_units_used' which is given an
1743 insn and produces an encoding showing which function units are required
1744 for the execution of that insn. If the value is non-negative, the insn
1745 uses that unit; otherwise, the value is a one's complement mask of units
1746 used.
1748 The second produces a function `result_ready_cost' which is used to
1749 determine the time that the result of an insn will be ready and hence
1750 a worst-case schedule.
1752 Both of these produce quite complex expressions which are then set as the
1753 default value of internal attributes. Normal attribute simplification
1754 should produce reasonable expressions.
1756 For each unit, a `<name>_unit_ready_cost' function will take an
1757 insn and give the delay until that unit will be ready with the result
1758 and a `<name>_unit_conflict_cost' function is given an insn already
1759 executing on the unit and a candidate to execute and will give the
1760 cost from the time the executing insn started until the candidate
1761 can start (ignore limitations on the number of simultaneous insns).
1763 For each unit, a `<name>_unit_blockage' function is given an insn
1764 already executing on the unit and a candidate to execute and will
1765 give the delay incurred due to function unit conflicts. The range of
1766 blockage cost values for a given executing insn is given by the
1767 `<name>_unit_blockage_range' function. These values are encoded in
1768 an int where the upper half gives the minimum value and the lower
1769 half gives the maximum value. */
1771 static void
1772 expand_units (void)
1774 struct function_unit *unit, **unit_num;
1775 struct function_unit_op *op, **op_array, ***unit_ops;
1776 rtx unitsmask;
1777 rtx readycost;
1778 rtx newexp;
1779 const char *str;
1780 int i, j, u, num, nvalues;
1782 /* Rebuild the condition for the unit to share the RTL expressions.
1783 Sharing is required by simplify_by_exploding. Build the issue delay
1784 expressions. Validate the expressions we were given for the conditions
1785 and conflict vector. Then make attributes for use in the conflict
1786 function. */
1788 for (unit = units; unit; unit = unit->next)
1790 unit->condexp = check_attr_test (unit->condexp, 0, unit->first_lineno);
1792 for (op = unit->ops; op; op = op->next)
1794 rtx issue_delay = make_numeric_value (op->issue_delay);
1795 rtx issue_exp = issue_delay;
1797 /* Build, validate, and simplify the issue delay expression. */
1798 if (op->conflict_exp != true_rtx)
1799 issue_exp = attr_rtx (IF_THEN_ELSE, op->conflict_exp,
1800 issue_exp, make_numeric_value (0));
1801 issue_exp = check_attr_value (make_canonical (NULL_ATTR,
1802 issue_exp),
1803 NULL_ATTR);
1804 issue_exp = simplify_knowing (issue_exp, unit->condexp);
1805 op->issue_exp = issue_exp;
1807 /* Make an attribute for use in the conflict function if needed. */
1808 unit->needs_conflict_function = (unit->issue_delay.min
1809 != unit->issue_delay.max);
1810 if (unit->needs_conflict_function)
1812 str = attr_printf ((strlen (unit->name) + sizeof "*_cost_"
1813 + MAX_DIGITS),
1814 "*%s_cost_%d", unit->name, op->num);
1815 make_internal_attr (str, issue_exp, ATTR_SPECIAL);
1818 /* Validate the condition. */
1819 op->condexp = check_attr_test (op->condexp, 0, op->lineno);
1823 /* Compute the mask of function units used. Initially, the unitsmask is
1824 zero. Set up a conditional to compute each unit's contribution. */
1825 unitsmask = make_numeric_value (0);
1826 newexp = rtx_alloc (IF_THEN_ELSE);
1827 XEXP (newexp, 2) = make_numeric_value (0);
1829 /* If we have just a few units, we may be all right expanding the whole
1830 thing. But the expansion is 2**N in space on the number of opclasses,
1831 so we can't do this for very long -- Alpha and MIPS in particular have
1832 problems with this. So in that situation, we fall back on an alternate
1833 implementation method. */
1834 #define NUM_UNITOP_CUTOFF 20
1836 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1838 /* Merge each function unit into the unit mask attributes. */
1839 for (unit = units; unit; unit = unit->next)
1841 XEXP (newexp, 0) = unit->condexp;
1842 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1843 unitsmask = operate_exp (OR_OP, unitsmask, newexp);
1846 else
1848 /* Merge each function unit into the unit mask attributes. */
1849 for (unit = units; unit; unit = unit->next)
1851 XEXP (newexp, 0) = unit->condexp;
1852 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1853 unitsmask = operate_exp (ORX_OP, unitsmask, attr_copy_rtx (newexp));
1857 /* Simplify the unit mask expression, encode it, and make an attribute
1858 for the function_units_used function. */
1859 unitsmask = simplify_by_exploding (unitsmask);
1861 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1862 unitsmask = encode_units_mask (unitsmask);
1863 else
1865 /* We can no longer encode unitsmask at compile time, so emit code to
1866 calculate it at runtime. Rather, put a marker for where we'd do
1867 the code, and actually output it in write_attr_get(). */
1868 unitsmask = attr_rtx (FFS, unitsmask);
1871 make_internal_attr ("*function_units_used", unitsmask,
1872 (ATTR_NEGATIVE_OK | ATTR_FUNC_UNITS));
1874 /* Create an array of ops for each unit. Add an extra unit for the
1875 result_ready_cost function that has the ops of all other units. */
1876 unit_ops = xmalloc ((num_units + 1) * sizeof (struct function_unit_op **));
1877 unit_num = xmalloc ((num_units + 1) * sizeof (struct function_unit *));
1879 unit_num[num_units] = unit = xmalloc (sizeof (struct function_unit));
1880 unit->num = num_units;
1881 unit->num_opclasses = 0;
1883 for (unit = units; unit; unit = unit->next)
1885 unit_num[num_units]->num_opclasses += unit->num_opclasses;
1886 unit_num[unit->num] = unit;
1887 unit_ops[unit->num] = op_array =
1888 xmalloc (unit->num_opclasses * sizeof (struct function_unit_op *));
1890 for (op = unit->ops; op; op = op->next)
1891 op_array[op->num] = op;
1894 /* Compose the array of ops for the extra unit. */
1895 unit_ops[num_units] = op_array =
1896 xmalloc (unit_num[num_units]->num_opclasses
1897 * sizeof (struct function_unit_op *));
1899 for (unit = units, i = 0; unit; i += unit->num_opclasses, unit = unit->next)
1900 memcpy (&op_array[i], unit_ops[unit->num],
1901 unit->num_opclasses * sizeof (struct function_unit_op *));
1903 /* Compute the ready cost function for each unit by computing the
1904 condition for each non-default value. */
1905 for (u = 0; u <= num_units; u++)
1907 rtx orexp;
1908 int value;
1910 unit = unit_num[u];
1911 op_array = unit_ops[unit->num];
1912 num = unit->num_opclasses;
1914 /* Sort the array of ops into increasing ready cost order. */
1915 for (i = 0; i < num; i++)
1916 for (j = num - 1; j > i; j--)
1917 if (op_array[j - 1]->ready < op_array[j]->ready)
1919 op = op_array[j];
1920 op_array[j] = op_array[j - 1];
1921 op_array[j - 1] = op;
1924 /* Determine how many distinct non-default ready cost values there
1925 are. We use a default ready cost value of 1. */
1926 nvalues = 0; value = 1;
1927 for (i = num - 1; i >= 0; i--)
1928 if (op_array[i]->ready > value)
1930 value = op_array[i]->ready;
1931 nvalues++;
1934 if (nvalues == 0)
1935 readycost = make_numeric_value (1);
1936 else
1938 /* Construct the ready cost expression as a COND of each value from
1939 the largest to the smallest. */
1940 readycost = rtx_alloc (COND);
1941 XVEC (readycost, 0) = rtvec_alloc (nvalues * 2);
1942 XEXP (readycost, 1) = make_numeric_value (1);
1944 nvalues = 0;
1945 orexp = false_rtx;
1946 value = op_array[0]->ready;
1947 for (i = 0; i < num; i++)
1949 op = op_array[i];
1950 if (op->ready <= 1)
1951 break;
1952 else if (op->ready == value)
1953 orexp = insert_right_side (IOR, orexp, op->condexp, -2, -2);
1954 else
1956 XVECEXP (readycost, 0, nvalues * 2) = orexp;
1957 XVECEXP (readycost, 0, nvalues * 2 + 1)
1958 = make_numeric_value (value);
1959 nvalues++;
1960 value = op->ready;
1961 orexp = op->condexp;
1964 XVECEXP (readycost, 0, nvalues * 2) = orexp;
1965 XVECEXP (readycost, 0, nvalues * 2 + 1) = make_numeric_value (value);
1968 if (u < num_units)
1970 rtx max_blockage = 0, min_blockage = 0;
1972 /* Simplify the readycost expression by only considering insns
1973 that use the unit. */
1974 readycost = simplify_knowing (readycost, unit->condexp);
1976 /* Determine the blockage cost the executing insn (E) given
1977 the candidate insn (C). This is the maximum of the issue
1978 delay, the pipeline delay, and the simultaneity constraint.
1979 Each function_unit_op represents the characteristics of the
1980 candidate insn, so in the expressions below, C is a known
1981 term and E is an unknown term.
1983 We compute the blockage cost for each E for every possible C.
1984 Thus OP represents E, and READYCOST is a list of values for
1985 every possible C.
1987 The issue delay function for C is op->issue_exp and is used to
1988 write the `<name>_unit_conflict_cost' function. Symbolically
1989 this is "ISSUE-DELAY (E,C)".
1991 The pipeline delay results form the FIFO constraint on the
1992 function unit and is "READY-COST (E) + 1 - READY-COST (C)".
1994 The simultaneity constraint is based on how long it takes to
1995 fill the unit given the minimum issue delay. FILL-TIME is the
1996 constant "MIN (ISSUE-DELAY (*,*)) * (SIMULTANEITY - 1)", and
1997 the simultaneity constraint is "READY-COST (E) - FILL-TIME"
1998 if SIMULTANEITY is nonzero and zero otherwise.
2000 Thus, BLOCKAGE (E,C) when SIMULTANEITY is zero is
2002 MAX (ISSUE-DELAY (E,C),
2003 READY-COST (E) - (READY-COST (C) - 1))
2005 and otherwise
2007 MAX (ISSUE-DELAY (E,C),
2008 READY-COST (E) - (READY-COST (C) - 1),
2009 READY-COST (E) - FILL-TIME)
2011 The `<name>_unit_blockage' function is computed by determining
2012 this value for each candidate insn. As these values are
2013 computed, we also compute the upper and lower bounds for
2014 BLOCKAGE (E,*). These are combined to form the function
2015 `<name>_unit_blockage_range'. Finally, the maximum blockage
2016 cost, MAX (BLOCKAGE (*,*)), is computed. */
2018 for (op = unit->ops; op; op = op->next)
2020 rtx blockage = op->issue_exp;
2021 blockage = simplify_knowing (blockage, unit->condexp);
2023 /* Add this op's contribution to MAX (BLOCKAGE (E,*)) and
2024 MIN (BLOCKAGE (E,*)). */
2025 if (max_blockage == 0)
2026 max_blockage = min_blockage = blockage;
2027 else
2029 max_blockage
2030 = simplify_knowing (operate_exp (MAX_OP, max_blockage,
2031 blockage),
2032 unit->condexp);
2033 min_blockage
2034 = simplify_knowing (operate_exp (MIN_OP, min_blockage,
2035 blockage),
2036 unit->condexp);
2039 /* Make an attribute for use in the blockage function. */
2040 str = attr_printf ((strlen (unit->name) + sizeof "*_block_"
2041 + MAX_DIGITS),
2042 "*%s_block_%d", unit->name, op->num);
2043 make_internal_attr (str, blockage, ATTR_SPECIAL);
2046 /* Record MAX (BLOCKAGE (*,*)). */
2048 int unknown;
2049 unit->max_blockage = max_attr_value (max_blockage, &unknown);
2052 /* See if the upper and lower bounds of BLOCKAGE (E,*) are the
2053 same. If so, the blockage function carries no additional
2054 information and is not written. */
2055 newexp = operate_exp (EQ_OP, max_blockage, min_blockage);
2056 newexp = simplify_knowing (newexp, unit->condexp);
2057 unit->needs_blockage_function
2058 = (GET_CODE (newexp) != CONST_STRING
2059 || atoi (XSTR (newexp, 0)) != 1);
2061 /* If the all values of BLOCKAGE (E,C) have the same value,
2062 neither blockage function is written. */
2063 unit->needs_range_function
2064 = (unit->needs_blockage_function
2065 || GET_CODE (max_blockage) != CONST_STRING);
2067 if (unit->needs_range_function)
2069 /* Compute the blockage range function and make an attribute
2070 for writing its value. */
2071 newexp = operate_exp (RANGE_OP, min_blockage, max_blockage);
2072 newexp = simplify_knowing (newexp, unit->condexp);
2074 str = attr_printf ((strlen (unit->name)
2075 + sizeof "*_unit_blockage_range"),
2076 "*%s_unit_blockage_range", unit->name);
2077 make_internal_attr (str, newexp, (ATTR_STATIC|ATTR_BLOCKAGE|ATTR_UNSIGNED));
2080 str = attr_printf (strlen (unit->name) + sizeof "*_unit_ready_cost",
2081 "*%s_unit_ready_cost", unit->name);
2082 make_internal_attr (str, readycost, ATTR_STATIC);
2084 else
2086 /* Make an attribute for the ready_cost function. Simplifying
2087 further with simplify_by_exploding doesn't win. */
2088 str = "*result_ready_cost";
2089 make_internal_attr (str, readycost, ATTR_NONE);
2093 /* For each unit that requires a conflict cost function, make an attribute
2094 that maps insns to the operation number. */
2095 for (unit = units; unit; unit = unit->next)
2097 rtx caseexp;
2099 if (! unit->needs_conflict_function
2100 && ! unit->needs_blockage_function)
2101 continue;
2103 caseexp = rtx_alloc (COND);
2104 XVEC (caseexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
2106 for (op = unit->ops; op; op = op->next)
2108 /* Make our adjustment to the COND being computed. If we are the
2109 last operation class, place our values into the default of the
2110 COND. */
2111 if (op->num == unit->num_opclasses - 1)
2113 XEXP (caseexp, 1) = make_numeric_value (op->num);
2115 else
2117 XVECEXP (caseexp, 0, op->num * 2) = op->condexp;
2118 XVECEXP (caseexp, 0, op->num * 2 + 1)
2119 = make_numeric_value (op->num);
2123 /* Simplifying caseexp with simplify_by_exploding doesn't win. */
2124 str = attr_printf (strlen (unit->name) + sizeof "*_cases",
2125 "*%s_cases", unit->name);
2126 make_internal_attr (str, caseexp, ATTR_SPECIAL);
2130 /* Simplify EXP given KNOWN_TRUE. */
2132 static rtx
2133 simplify_knowing (rtx exp, rtx known_true)
2135 if (GET_CODE (exp) != CONST_STRING)
2137 int unknown = 0, max;
2138 max = max_attr_value (exp, &unknown);
2139 if (! unknown)
2141 exp = attr_rtx (IF_THEN_ELSE, known_true, exp,
2142 make_numeric_value (max));
2143 exp = simplify_by_exploding (exp);
2146 return exp;
2149 /* Translate the CONST_STRING expressions in X to change the encoding of
2150 value. On input, the value is a bitmask with a one bit for each unit
2151 used; on output, the value is the unit number (zero based) if one
2152 and only one unit is used or the one's complement of the bitmask. */
2154 static rtx
2155 encode_units_mask (rtx x)
2157 int i;
2158 int j;
2159 enum rtx_code code;
2160 const char *fmt;
2162 code = GET_CODE (x);
2164 switch (code)
2166 case CONST_STRING:
2167 i = atoi (XSTR (x, 0));
2168 if (i < 0)
2169 /* The sign bit encodes a one's complement mask. */
2170 abort ();
2171 else if (i != 0 && i == (i & -i))
2172 /* Only one bit is set, so yield that unit number. */
2173 for (j = 0; (i >>= 1) != 0; j++)
2175 else
2176 j = ~i;
2177 return attr_rtx (CONST_STRING, attr_printf (MAX_DIGITS, "%d", j));
2179 case REG:
2180 case QUEUED:
2181 case CONST_INT:
2182 case CONST_DOUBLE:
2183 case CONST_VECTOR:
2184 case SYMBOL_REF:
2185 case CODE_LABEL:
2186 case PC:
2187 case CC0:
2188 case EQ_ATTR:
2189 return x;
2191 default:
2192 break;
2195 /* Compare the elements. If any pair of corresponding elements
2196 fail to match, return 0 for the whole things. */
2198 fmt = GET_RTX_FORMAT (code);
2199 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2201 switch (fmt[i])
2203 case 'V':
2204 case 'E':
2205 for (j = 0; j < XVECLEN (x, i); j++)
2206 XVECEXP (x, i, j) = encode_units_mask (XVECEXP (x, i, j));
2207 break;
2209 case 'e':
2210 XEXP (x, i) = encode_units_mask (XEXP (x, i));
2211 break;
2214 return x;
2217 /* Once all attributes and insns have been read and checked, we construct for
2218 each attribute value a list of all the insns that have that value for
2219 the attribute. */
2221 static void
2222 fill_attr (struct attr_desc *attr)
2224 struct attr_value *av;
2225 struct insn_ent *ie;
2226 struct insn_def *id;
2227 int i;
2228 rtx value;
2230 /* Don't fill constant attributes. The value is independent of
2231 any particular insn. */
2232 if (attr->is_const)
2233 return;
2235 for (id = defs; id; id = id->next)
2237 /* If no value is specified for this insn for this attribute, use the
2238 default. */
2239 value = NULL;
2240 if (XVEC (id->def, id->vec_idx))
2241 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
2242 if (! strcmp (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
2243 attr->name))
2244 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
2246 if (value == NULL)
2247 av = attr->default_val;
2248 else
2249 av = get_attr_value (value, attr, id->insn_code);
2251 ie = oballoc (sizeof (struct insn_ent));
2252 ie->insn_code = id->insn_code;
2253 ie->insn_index = id->insn_code;
2254 insert_insn_ent (av, ie);
2258 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
2259 test that checks relative positions of insns (uses MATCH_DUP or PC).
2260 If so, replace it with what is obtained by passing the expression to
2261 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
2262 recursively on each value (including the default value). Otherwise,
2263 return the value returned by NO_ADDRESS_FN applied to EXP. */
2265 static rtx
2266 substitute_address (rtx exp, rtx (*no_address_fn) (rtx),
2267 rtx (*address_fn) (rtx))
2269 int i;
2270 rtx newexp;
2272 if (GET_CODE (exp) == COND)
2274 /* See if any tests use addresses. */
2275 address_used = 0;
2276 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2277 walk_attr_value (XVECEXP (exp, 0, i));
2279 if (address_used)
2280 return (*address_fn) (exp);
2282 /* Make a new copy of this COND, replacing each element. */
2283 newexp = rtx_alloc (COND);
2284 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
2285 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2287 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
2288 XVECEXP (newexp, 0, i + 1)
2289 = substitute_address (XVECEXP (exp, 0, i + 1),
2290 no_address_fn, address_fn);
2293 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
2294 no_address_fn, address_fn);
2296 return newexp;
2299 else if (GET_CODE (exp) == IF_THEN_ELSE)
2301 address_used = 0;
2302 walk_attr_value (XEXP (exp, 0));
2303 if (address_used)
2304 return (*address_fn) (exp);
2306 return attr_rtx (IF_THEN_ELSE,
2307 substitute_address (XEXP (exp, 0),
2308 no_address_fn, address_fn),
2309 substitute_address (XEXP (exp, 1),
2310 no_address_fn, address_fn),
2311 substitute_address (XEXP (exp, 2),
2312 no_address_fn, address_fn));
2315 return (*no_address_fn) (exp);
2318 /* Make new attributes from the `length' attribute. The following are made,
2319 each corresponding to a function called from `shorten_branches' or
2320 `get_attr_length':
2322 *insn_default_length This is the length of the insn to be returned
2323 by `get_attr_length' before `shorten_branches'
2324 has been called. In each case where the length
2325 depends on relative addresses, the largest
2326 possible is used. This routine is also used
2327 to compute the initial size of the insn.
2329 *insn_variable_length_p This returns 1 if the insn's length depends
2330 on relative addresses, zero otherwise.
2332 *insn_current_length This is only called when it is known that the
2333 insn has a variable length and returns the
2334 current length, based on relative addresses.
2337 static void
2338 make_length_attrs (void)
2340 static const char *const new_names[] = {"*insn_default_length",
2341 "*insn_variable_length_p",
2342 "*insn_current_length"};
2343 static rtx (*const no_address_fn[]) (rtx) = {identity_fn, zero_fn, zero_fn};
2344 static rtx (*const address_fn[]) (rtx) = {max_fn, one_fn, identity_fn};
2345 size_t i;
2346 struct attr_desc *length_attr, *new_attr;
2347 struct attr_value *av, *new_av;
2348 struct insn_ent *ie, *new_ie;
2350 /* See if length attribute is defined. If so, it must be numeric. Make
2351 it special so we don't output anything for it. */
2352 length_attr = find_attr ("length", 0);
2353 if (length_attr == 0)
2354 return;
2356 if (! length_attr->is_numeric)
2357 fatal ("length attribute must be numeric");
2359 length_attr->is_const = 0;
2360 length_attr->is_special = 1;
2362 /* Make each new attribute, in turn. */
2363 for (i = 0; i < ARRAY_SIZE (new_names); i++)
2365 make_internal_attr (new_names[i],
2366 substitute_address (length_attr->default_val->value,
2367 no_address_fn[i], address_fn[i]),
2368 ATTR_NONE);
2369 new_attr = find_attr (new_names[i], 0);
2370 for (av = length_attr->first_value; av; av = av->next)
2371 for (ie = av->first_insn; ie; ie = ie->next)
2373 new_av = get_attr_value (substitute_address (av->value,
2374 no_address_fn[i],
2375 address_fn[i]),
2376 new_attr, ie->insn_code);
2377 new_ie = oballoc (sizeof (struct insn_ent));
2378 new_ie->insn_code = ie->insn_code;
2379 new_ie->insn_index = ie->insn_index;
2380 insert_insn_ent (new_av, new_ie);
2385 /* Utility functions called from above routine. */
2387 static rtx
2388 identity_fn (rtx exp)
2390 return exp;
2393 static rtx
2394 zero_fn (rtx exp ATTRIBUTE_UNUSED)
2396 return make_numeric_value (0);
2399 static rtx
2400 one_fn (rtx exp ATTRIBUTE_UNUSED)
2402 return make_numeric_value (1);
2405 static rtx
2406 max_fn (rtx exp)
2408 int unknown;
2409 return make_numeric_value (max_attr_value (exp, &unknown));
2412 static void
2413 write_length_unit_log (void)
2415 struct attr_desc *length_attr = find_attr ("length", 0);
2416 struct attr_value *av;
2417 struct insn_ent *ie;
2418 unsigned int length_unit_log, length_or;
2419 int unknown = 0;
2421 if (length_attr == 0)
2422 return;
2423 length_or = or_attr_value (length_attr->default_val->value, &unknown);
2424 for (av = length_attr->first_value; av; av = av->next)
2425 for (ie = av->first_insn; ie; ie = ie->next)
2426 length_or |= or_attr_value (av->value, &unknown);
2428 if (unknown)
2429 length_unit_log = 0;
2430 else
2432 length_or = ~length_or;
2433 for (length_unit_log = 0; length_or & 1; length_or >>= 1)
2434 length_unit_log++;
2436 printf ("int length_unit_log = %u;\n", length_unit_log);
2439 /* Take a COND expression and see if any of the conditions in it can be
2440 simplified. If any are known true or known false for the particular insn
2441 code, the COND can be further simplified.
2443 Also call ourselves on any COND operations that are values of this COND.
2445 We do not modify EXP; rather, we make and return a new rtx. */
2447 static rtx
2448 simplify_cond (rtx exp, int insn_code, int insn_index)
2450 int i, j;
2451 /* We store the desired contents here,
2452 then build a new expression if they don't match EXP. */
2453 rtx defval = XEXP (exp, 1);
2454 rtx new_defval = XEXP (exp, 1);
2455 int len = XVECLEN (exp, 0);
2456 rtx *tests = xmalloc (len * sizeof (rtx));
2457 int allsame = 1;
2458 rtx ret;
2460 /* This lets us free all storage allocated below, if appropriate. */
2461 obstack_finish (rtl_obstack);
2463 memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
2465 /* See if default value needs simplification. */
2466 if (GET_CODE (defval) == COND)
2467 new_defval = simplify_cond (defval, insn_code, insn_index);
2469 /* Simplify the subexpressions, and see what tests we can get rid of. */
2471 for (i = 0; i < len; i += 2)
2473 rtx newtest, newval;
2475 /* Simplify this test. */
2476 newtest = simplify_test_exp_in_temp (tests[i], insn_code, insn_index);
2477 tests[i] = newtest;
2479 newval = tests[i + 1];
2480 /* See if this value may need simplification. */
2481 if (GET_CODE (newval) == COND)
2482 newval = simplify_cond (newval, insn_code, insn_index);
2484 /* Look for ways to delete or combine this test. */
2485 if (newtest == true_rtx)
2487 /* If test is true, make this value the default
2488 and discard this + any following tests. */
2489 len = i;
2490 defval = tests[i + 1];
2491 new_defval = newval;
2494 else if (newtest == false_rtx)
2496 /* If test is false, discard it and its value. */
2497 for (j = i; j < len - 2; j++)
2498 tests[j] = tests[j + 2];
2499 len -= 2;
2502 else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
2504 /* If this value and the value for the prev test are the same,
2505 merge the tests. */
2507 tests[i - 2]
2508 = insert_right_side (IOR, tests[i - 2], newtest,
2509 insn_code, insn_index);
2511 /* Delete this test/value. */
2512 for (j = i; j < len - 2; j++)
2513 tests[j] = tests[j + 2];
2514 len -= 2;
2517 else
2518 tests[i + 1] = newval;
2521 /* If the last test in a COND has the same value
2522 as the default value, that test isn't needed. */
2524 while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
2525 len -= 2;
2527 /* See if we changed anything. */
2528 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
2529 allsame = 0;
2530 else
2531 for (i = 0; i < len; i++)
2532 if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
2534 allsame = 0;
2535 break;
2538 if (len == 0)
2540 if (GET_CODE (defval) == COND)
2541 ret = simplify_cond (defval, insn_code, insn_index);
2542 else
2543 ret = defval;
2545 else if (allsame)
2546 ret = exp;
2547 else
2549 rtx newexp = rtx_alloc (COND);
2551 XVEC (newexp, 0) = rtvec_alloc (len);
2552 memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
2553 XEXP (newexp, 1) = new_defval;
2554 ret = newexp;
2556 free (tests);
2557 return ret;
2560 /* Remove an insn entry from an attribute value. */
2562 static void
2563 remove_insn_ent (struct attr_value *av, struct insn_ent *ie)
2565 struct insn_ent *previe;
2567 if (av->first_insn == ie)
2568 av->first_insn = ie->next;
2569 else
2571 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
2573 previe->next = ie->next;
2576 av->num_insns--;
2577 if (ie->insn_code == -1)
2578 av->has_asm_insn = 0;
2580 num_insn_ents--;
2583 /* Insert an insn entry in an attribute value list. */
2585 static void
2586 insert_insn_ent (struct attr_value *av, struct insn_ent *ie)
2588 ie->next = av->first_insn;
2589 av->first_insn = ie;
2590 av->num_insns++;
2591 if (ie->insn_code == -1)
2592 av->has_asm_insn = 1;
2594 num_insn_ents++;
2597 /* This is a utility routine to take an expression that is a tree of either
2598 AND or IOR expressions and insert a new term. The new term will be
2599 inserted at the right side of the first node whose code does not match
2600 the root. A new node will be created with the root's code. Its left
2601 side will be the old right side and its right side will be the new
2602 term.
2604 If the `term' is itself a tree, all its leaves will be inserted. */
2606 static rtx
2607 insert_right_side (enum rtx_code code, rtx exp, rtx term, int insn_code, int insn_index)
2609 rtx newexp;
2611 /* Avoid consing in some special cases. */
2612 if (code == AND && term == true_rtx)
2613 return exp;
2614 if (code == AND && term == false_rtx)
2615 return false_rtx;
2616 if (code == AND && exp == true_rtx)
2617 return term;
2618 if (code == AND && exp == false_rtx)
2619 return false_rtx;
2620 if (code == IOR && term == true_rtx)
2621 return true_rtx;
2622 if (code == IOR && term == false_rtx)
2623 return exp;
2624 if (code == IOR && exp == true_rtx)
2625 return true_rtx;
2626 if (code == IOR && exp == false_rtx)
2627 return term;
2628 if (attr_equal_p (exp, term))
2629 return exp;
2631 if (GET_CODE (term) == code)
2633 exp = insert_right_side (code, exp, XEXP (term, 0),
2634 insn_code, insn_index);
2635 exp = insert_right_side (code, exp, XEXP (term, 1),
2636 insn_code, insn_index);
2638 return exp;
2641 if (GET_CODE (exp) == code)
2643 rtx new = insert_right_side (code, XEXP (exp, 1),
2644 term, insn_code, insn_index);
2645 if (new != XEXP (exp, 1))
2646 /* Make a copy of this expression and call recursively. */
2647 newexp = attr_rtx (code, XEXP (exp, 0), new);
2648 else
2649 newexp = exp;
2651 else
2653 /* Insert the new term. */
2654 newexp = attr_rtx (code, exp, term);
2657 return simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2660 /* If we have an expression which AND's a bunch of
2661 (not (eq_attrq "alternative" "n"))
2662 terms, we may have covered all or all but one of the possible alternatives.
2663 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
2665 This routine is passed an expression and either AND or IOR. It returns a
2666 bitmask indicating which alternatives are mentioned within EXP. */
2668 static int
2669 compute_alternative_mask (rtx exp, enum rtx_code code)
2671 const char *string;
2672 if (GET_CODE (exp) == code)
2673 return compute_alternative_mask (XEXP (exp, 0), code)
2674 | compute_alternative_mask (XEXP (exp, 1), code);
2676 else if (code == AND && GET_CODE (exp) == NOT
2677 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2678 && XSTR (XEXP (exp, 0), 0) == alternative_name)
2679 string = XSTR (XEXP (exp, 0), 1);
2681 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
2682 && XSTR (exp, 0) == alternative_name)
2683 string = XSTR (exp, 1);
2685 else
2686 return 0;
2688 if (string[1] == 0)
2689 return 1 << (string[0] - '0');
2690 return 1 << atoi (string);
2693 /* Given I, a single-bit mask, return RTX to compare the `alternative'
2694 attribute with the value represented by that bit. */
2696 static rtx
2697 make_alternative_compare (int mask)
2699 rtx newexp;
2700 int i;
2702 /* Find the bit. */
2703 for (i = 0; (mask & (1 << i)) == 0; i++)
2706 newexp = attr_rtx (EQ_ATTR, alternative_name, attr_numeral (i));
2707 ATTR_IND_SIMPLIFIED_P (newexp) = 1;
2709 return newexp;
2712 /* If we are processing an (eq_attr "attr" "value") test, we find the value
2713 of "attr" for this insn code. From that value, we can compute a test
2714 showing when the EQ_ATTR will be true. This routine performs that
2715 computation. If a test condition involves an address, we leave the EQ_ATTR
2716 intact because addresses are only valid for the `length' attribute.
2718 EXP is the EQ_ATTR expression and VALUE is the value of that attribute
2719 for the insn corresponding to INSN_CODE and INSN_INDEX. */
2721 static rtx
2722 evaluate_eq_attr (rtx exp, rtx value, int insn_code, int insn_index)
2724 rtx orexp, andexp;
2725 rtx right;
2726 rtx newexp;
2727 int i;
2729 if (GET_CODE (value) == CONST_STRING)
2731 if (! strcmp (XSTR (value, 0), XSTR (exp, 1)))
2732 newexp = true_rtx;
2733 else
2734 newexp = false_rtx;
2736 else if (GET_CODE (value) == SYMBOL_REF)
2738 char *p;
2739 char string[256];
2741 if (GET_CODE (exp) != EQ_ATTR)
2742 abort ();
2744 if (strlen (XSTR (exp, 0)) + strlen (XSTR (exp, 1)) + 2 > 256)
2745 abort ();
2747 strcpy (string, XSTR (exp, 0));
2748 strcat (string, "_");
2749 strcat (string, XSTR (exp, 1));
2750 for (p = string; *p; p++)
2751 *p = TOUPPER (*p);
2753 newexp = attr_rtx (EQ, value,
2754 attr_rtx (SYMBOL_REF,
2755 attr_string (string, strlen (string))));
2757 else if (GET_CODE (value) == COND)
2759 /* We construct an IOR of all the cases for which the requested attribute
2760 value is present. Since we start with FALSE, if it is not present,
2761 FALSE will be returned.
2763 Each case is the AND of the NOT's of the previous conditions with the
2764 current condition; in the default case the current condition is TRUE.
2766 For each possible COND value, call ourselves recursively.
2768 The extra TRUE and FALSE expressions will be eliminated by another
2769 call to the simplification routine. */
2771 orexp = false_rtx;
2772 andexp = true_rtx;
2774 if (current_alternative_string)
2775 clear_struct_flag (value);
2777 for (i = 0; i < XVECLEN (value, 0); i += 2)
2779 rtx this = simplify_test_exp_in_temp (XVECEXP (value, 0, i),
2780 insn_code, insn_index);
2782 SIMPLIFY_ALTERNATIVE (this);
2784 right = insert_right_side (AND, andexp, this,
2785 insn_code, insn_index);
2786 right = insert_right_side (AND, right,
2787 evaluate_eq_attr (exp,
2788 XVECEXP (value, 0,
2789 i + 1),
2790 insn_code, insn_index),
2791 insn_code, insn_index);
2792 orexp = insert_right_side (IOR, orexp, right,
2793 insn_code, insn_index);
2795 /* Add this condition into the AND expression. */
2796 newexp = attr_rtx (NOT, this);
2797 andexp = insert_right_side (AND, andexp, newexp,
2798 insn_code, insn_index);
2801 /* Handle the default case. */
2802 right = insert_right_side (AND, andexp,
2803 evaluate_eq_attr (exp, XEXP (value, 1),
2804 insn_code, insn_index),
2805 insn_code, insn_index);
2806 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2808 else
2809 abort ();
2811 /* If uses an address, must return original expression. But set the
2812 ATTR_IND_SIMPLIFIED_P bit so we don't try to simplify it again. */
2814 address_used = 0;
2815 walk_attr_value (newexp);
2817 if (address_used)
2819 /* This had `&& current_alternative_string', which seems to be wrong. */
2820 if (! ATTR_IND_SIMPLIFIED_P (exp))
2821 return copy_rtx_unchanging (exp);
2822 return exp;
2824 else
2825 return newexp;
2828 /* This routine is called when an AND of a term with a tree of AND's is
2829 encountered. If the term or its complement is present in the tree, it
2830 can be replaced with TRUE or FALSE, respectively.
2832 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2833 be true and hence are complementary.
2835 There is one special case: If we see
2836 (and (not (eq_attr "att" "v1"))
2837 (eq_attr "att" "v2"))
2838 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2839 replace the term, not anything in the AND tree. So we pass a pointer to
2840 the term. */
2842 static rtx
2843 simplify_and_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2845 rtx left, right;
2846 rtx newexp;
2847 rtx temp;
2848 int left_eliminates_term, right_eliminates_term;
2850 if (GET_CODE (exp) == AND)
2852 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2853 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2854 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2856 newexp = attr_rtx (GET_CODE (exp), left, right);
2858 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2862 else if (GET_CODE (exp) == IOR)
2864 /* For the IOR case, we do the same as above, except that we can
2865 only eliminate `term' if both sides of the IOR would do so. */
2866 temp = *pterm;
2867 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2868 left_eliminates_term = (temp == true_rtx);
2870 temp = *pterm;
2871 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2872 right_eliminates_term = (temp == true_rtx);
2874 if (left_eliminates_term && right_eliminates_term)
2875 *pterm = true_rtx;
2877 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2879 newexp = attr_rtx (GET_CODE (exp), left, right);
2881 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2885 /* Check for simplifications. Do some extra checking here since this
2886 routine is called so many times. */
2888 if (exp == *pterm)
2889 return true_rtx;
2891 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2892 return false_rtx;
2894 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2895 return false_rtx;
2897 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2899 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2900 return exp;
2902 if (! strcmp (XSTR (exp, 1), XSTR (*pterm, 1)))
2903 return true_rtx;
2904 else
2905 return false_rtx;
2908 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2909 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2911 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2912 return exp;
2914 if (! strcmp (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2915 return false_rtx;
2916 else
2917 return true_rtx;
2920 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2921 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2923 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2924 return exp;
2926 if (! strcmp (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2927 return false_rtx;
2928 else
2929 *pterm = true_rtx;
2932 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2934 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2935 return true_rtx;
2938 else if (GET_CODE (exp) == NOT)
2940 if (attr_equal_p (XEXP (exp, 0), *pterm))
2941 return false_rtx;
2944 else if (GET_CODE (*pterm) == NOT)
2946 if (attr_equal_p (XEXP (*pterm, 0), exp))
2947 return false_rtx;
2950 else if (attr_equal_p (exp, *pterm))
2951 return true_rtx;
2953 return exp;
2956 /* Similar to `simplify_and_tree', but for IOR trees. */
2958 static rtx
2959 simplify_or_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2961 rtx left, right;
2962 rtx newexp;
2963 rtx temp;
2964 int left_eliminates_term, right_eliminates_term;
2966 if (GET_CODE (exp) == IOR)
2968 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2969 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2970 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2972 newexp = attr_rtx (GET_CODE (exp), left, right);
2974 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2978 else if (GET_CODE (exp) == AND)
2980 /* For the AND case, we do the same as above, except that we can
2981 only eliminate `term' if both sides of the AND would do so. */
2982 temp = *pterm;
2983 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2984 left_eliminates_term = (temp == false_rtx);
2986 temp = *pterm;
2987 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2988 right_eliminates_term = (temp == false_rtx);
2990 if (left_eliminates_term && right_eliminates_term)
2991 *pterm = false_rtx;
2993 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2995 newexp = attr_rtx (GET_CODE (exp), left, right);
2997 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
3001 if (attr_equal_p (exp, *pterm))
3002 return false_rtx;
3004 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
3005 return true_rtx;
3007 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
3008 return true_rtx;
3010 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
3011 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3012 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
3013 *pterm = false_rtx;
3015 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
3016 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
3017 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
3018 return false_rtx;
3020 return exp;
3023 /* Compute approximate cost of the expression. Used to decide whether
3024 expression is cheap enough for inline. */
3025 static int
3026 attr_rtx_cost (rtx x)
3028 int cost = 0;
3029 enum rtx_code code;
3030 if (!x)
3031 return 0;
3032 code = GET_CODE (x);
3033 switch (code)
3035 case MATCH_OPERAND:
3036 if (XSTR (x, 1)[0])
3037 return 10;
3038 else
3039 return 0;
3040 case EQ_ATTR:
3041 /* Alternatives don't result into function call. */
3042 if (!strcmp (XSTR (x, 0), "alternative"))
3043 return 0;
3044 else
3045 return 5;
3046 default:
3048 int i, j;
3049 const char *fmt = GET_RTX_FORMAT (code);
3050 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3052 switch (fmt[i])
3054 case 'V':
3055 case 'E':
3056 for (j = 0; j < XVECLEN (x, i); j++)
3057 cost += attr_rtx_cost (XVECEXP (x, i, j));
3058 break;
3059 case 'e':
3060 cost += attr_rtx_cost (XEXP (x, i));
3061 break;
3065 break;
3067 return cost;
3070 /* Simplify test expression and use temporary obstack in order to avoid
3071 memory bloat. Use ATTR_IND_SIMPLIFIED to avoid unnecessary simplifications
3072 and avoid unnecessary copying if possible. */
3074 static rtx
3075 simplify_test_exp_in_temp (rtx exp, int insn_code, int insn_index)
3077 rtx x;
3078 struct obstack *old;
3079 if (ATTR_IND_SIMPLIFIED_P (exp))
3080 return exp;
3081 old = rtl_obstack;
3082 rtl_obstack = temp_obstack;
3083 x = simplify_test_exp (exp, insn_code, insn_index);
3084 rtl_obstack = old;
3085 if (x == exp || rtl_obstack == temp_obstack)
3086 return x;
3087 return attr_copy_rtx (x);
3090 /* Given an expression, see if it can be simplified for a particular insn
3091 code based on the values of other attributes being tested. This can
3092 eliminate nested get_attr_... calls.
3094 Note that if an endless recursion is specified in the patterns, the
3095 optimization will loop. However, it will do so in precisely the cases where
3096 an infinite recursion loop could occur during compilation. It's better that
3097 it occurs here! */
3099 static rtx
3100 simplify_test_exp (rtx exp, int insn_code, int insn_index)
3102 rtx left, right;
3103 struct attr_desc *attr;
3104 struct attr_value *av;
3105 struct insn_ent *ie;
3106 int i;
3107 rtx newexp = exp;
3109 /* Don't re-simplify something we already simplified. */
3110 if (ATTR_IND_SIMPLIFIED_P (exp) || ATTR_CURR_SIMPLIFIED_P (exp))
3111 return exp;
3113 switch (GET_CODE (exp))
3115 case AND:
3116 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3117 SIMPLIFY_ALTERNATIVE (left);
3118 if (left == false_rtx)
3119 return false_rtx;
3120 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3121 SIMPLIFY_ALTERNATIVE (right);
3122 if (left == false_rtx)
3123 return false_rtx;
3125 /* If either side is an IOR and we have (eq_attr "alternative" ..")
3126 present on both sides, apply the distributive law since this will
3127 yield simplifications. */
3128 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
3129 && compute_alternative_mask (left, IOR)
3130 && compute_alternative_mask (right, IOR))
3132 if (GET_CODE (left) == IOR)
3134 rtx tem = left;
3135 left = right;
3136 right = tem;
3139 newexp = attr_rtx (IOR,
3140 attr_rtx (AND, left, XEXP (right, 0)),
3141 attr_rtx (AND, left, XEXP (right, 1)));
3143 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3146 /* Try with the term on both sides. */
3147 right = simplify_and_tree (right, &left, insn_code, insn_index);
3148 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3149 left = simplify_and_tree (left, &right, insn_code, insn_index);
3151 if (left == false_rtx || right == false_rtx)
3152 return false_rtx;
3153 else if (left == true_rtx)
3155 return right;
3157 else if (right == true_rtx)
3159 return left;
3161 /* See if all or all but one of the insn's alternatives are specified
3162 in this tree. Optimize if so. */
3164 else if (insn_code >= 0
3165 && (GET_CODE (left) == AND
3166 || (GET_CODE (left) == NOT
3167 && GET_CODE (XEXP (left, 0)) == EQ_ATTR
3168 && XSTR (XEXP (left, 0), 0) == alternative_name)
3169 || GET_CODE (right) == AND
3170 || (GET_CODE (right) == NOT
3171 && GET_CODE (XEXP (right, 0)) == EQ_ATTR
3172 && XSTR (XEXP (right, 0), 0) == alternative_name)))
3174 i = compute_alternative_mask (exp, AND);
3175 if (i & ~insn_alternatives[insn_code])
3176 fatal ("invalid alternative specified for pattern number %d",
3177 insn_index);
3179 /* If all alternatives are excluded, this is false. */
3180 i ^= insn_alternatives[insn_code];
3181 if (i == 0)
3182 return false_rtx;
3183 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3185 /* If just one excluded, AND a comparison with that one to the
3186 front of the tree. The others will be eliminated by
3187 optimization. We do not want to do this if the insn has one
3188 alternative and we have tested none of them! */
3189 left = make_alternative_compare (i);
3190 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3191 newexp = attr_rtx (AND, left, right);
3193 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3197 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3199 newexp = attr_rtx (AND, left, right);
3200 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3202 break;
3204 case IOR:
3205 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3206 SIMPLIFY_ALTERNATIVE (left);
3207 if (left == true_rtx)
3208 return true_rtx;
3209 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3210 SIMPLIFY_ALTERNATIVE (right);
3211 if (right == true_rtx)
3212 return true_rtx;
3214 right = simplify_or_tree (right, &left, insn_code, insn_index);
3215 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3216 left = simplify_or_tree (left, &right, insn_code, insn_index);
3218 if (right == true_rtx || left == true_rtx)
3219 return true_rtx;
3220 else if (left == false_rtx)
3222 return right;
3224 else if (right == false_rtx)
3226 return left;
3229 /* Test for simple cases where the distributive law is useful. I.e.,
3230 convert (ior (and (x) (y))
3231 (and (x) (z)))
3232 to (and (x)
3233 (ior (y) (z)))
3236 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
3237 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
3239 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
3241 left = XEXP (left, 0);
3242 right = newexp;
3243 newexp = attr_rtx (AND, left, right);
3244 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3247 /* See if all or all but one of the insn's alternatives are specified
3248 in this tree. Optimize if so. */
3250 else if (insn_code >= 0
3251 && (GET_CODE (left) == IOR
3252 || (GET_CODE (left) == EQ_ATTR
3253 && XSTR (left, 0) == alternative_name)
3254 || GET_CODE (right) == IOR
3255 || (GET_CODE (right) == EQ_ATTR
3256 && XSTR (right, 0) == alternative_name)))
3258 i = compute_alternative_mask (exp, IOR);
3259 if (i & ~insn_alternatives[insn_code])
3260 fatal ("invalid alternative specified for pattern number %d",
3261 insn_index);
3263 /* If all alternatives are included, this is true. */
3264 i ^= insn_alternatives[insn_code];
3265 if (i == 0)
3266 return true_rtx;
3267 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3269 /* If just one excluded, IOR a comparison with that one to the
3270 front of the tree. The others will be eliminated by
3271 optimization. We do not want to do this if the insn has one
3272 alternative and we have tested none of them! */
3273 left = make_alternative_compare (i);
3274 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3275 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
3277 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3281 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3283 newexp = attr_rtx (IOR, left, right);
3284 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3286 break;
3288 case NOT:
3289 if (GET_CODE (XEXP (exp, 0)) == NOT)
3291 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
3292 insn_code, insn_index);
3293 SIMPLIFY_ALTERNATIVE (left);
3294 return left;
3297 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3298 SIMPLIFY_ALTERNATIVE (left);
3299 if (GET_CODE (left) == NOT)
3300 return XEXP (left, 0);
3302 if (left == false_rtx)
3303 return true_rtx;
3304 else if (left == true_rtx)
3305 return false_rtx;
3307 /* Try to apply De`Morgan's laws. */
3308 else if (GET_CODE (left) == IOR)
3310 newexp = attr_rtx (AND,
3311 attr_rtx (NOT, XEXP (left, 0)),
3312 attr_rtx (NOT, XEXP (left, 1)));
3314 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3316 else if (GET_CODE (left) == AND)
3318 newexp = attr_rtx (IOR,
3319 attr_rtx (NOT, XEXP (left, 0)),
3320 attr_rtx (NOT, XEXP (left, 1)));
3322 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3324 else if (left != XEXP (exp, 0))
3326 newexp = attr_rtx (NOT, left);
3328 break;
3330 case EQ_ATTR:
3331 if (current_alternative_string && XSTR (exp, 0) == alternative_name)
3332 return (XSTR (exp, 1) == current_alternative_string
3333 ? true_rtx : false_rtx);
3335 /* Look at the value for this insn code in the specified attribute.
3336 We normally can replace this comparison with the condition that
3337 would give this insn the values being tested for. */
3338 if (XSTR (exp, 0) != alternative_name
3339 && (attr = find_attr (XSTR (exp, 0), 0)) != NULL)
3340 for (av = attr->first_value; av; av = av->next)
3341 for (ie = av->first_insn; ie; ie = ie->next)
3342 if (ie->insn_code == insn_code)
3344 rtx x;
3345 x = evaluate_eq_attr (exp, av->value, insn_code, insn_index);
3346 x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
3347 if (attr_rtx_cost(x) < 20)
3348 return x;
3350 break;
3352 default:
3353 break;
3356 /* We have already simplified this expression. Simplifying it again
3357 won't buy anything unless we weren't given a valid insn code
3358 to process (i.e., we are canonicalizing something.). */
3359 if (insn_code != -2 /* Seems wrong: && current_alternative_string. */
3360 && ! ATTR_IND_SIMPLIFIED_P (newexp))
3361 return copy_rtx_unchanging (newexp);
3363 return newexp;
3366 /* Optimize the attribute lists by seeing if we can determine conditional
3367 values from the known values of other attributes. This will save subroutine
3368 calls during the compilation. */
3370 static void
3371 optimize_attrs (void)
3373 struct attr_desc *attr;
3374 struct attr_value *av;
3375 struct insn_ent *ie;
3376 rtx newexp;
3377 int i;
3378 struct attr_value_list
3380 struct attr_value *av;
3381 struct insn_ent *ie;
3382 struct attr_desc *attr;
3383 struct attr_value_list *next;
3385 struct attr_value_list **insn_code_values;
3386 struct attr_value_list *ivbuf;
3387 struct attr_value_list *iv;
3389 /* For each insn code, make a list of all the insn_ent's for it,
3390 for all values for all attributes. */
3392 if (num_insn_ents == 0)
3393 return;
3395 /* Make 2 extra elements, for "code" values -2 and -1. */
3396 insn_code_values = xmalloc ((insn_code_number + 2)
3397 * sizeof (struct attr_value_list *));
3398 memset (insn_code_values, 0,
3399 (insn_code_number + 2) * sizeof (struct attr_value_list *));
3401 /* Offset the table address so we can index by -2 or -1. */
3402 insn_code_values += 2;
3404 iv = ivbuf = xmalloc (num_insn_ents * sizeof (struct attr_value_list));
3406 for (i = 0; i < MAX_ATTRS_INDEX; i++)
3407 for (attr = attrs[i]; attr; attr = attr->next)
3408 for (av = attr->first_value; av; av = av->next)
3409 for (ie = av->first_insn; ie; ie = ie->next)
3411 iv->attr = attr;
3412 iv->av = av;
3413 iv->ie = ie;
3414 iv->next = insn_code_values[ie->insn_code];
3415 insn_code_values[ie->insn_code] = iv;
3416 iv++;
3419 /* Sanity check on num_insn_ents. */
3420 if (iv != ivbuf + num_insn_ents)
3421 abort ();
3423 /* Process one insn code at a time. */
3424 for (i = -2; i < insn_code_number; i++)
3426 /* Clear the ATTR_CURR_SIMPLIFIED_P flag everywhere relevant.
3427 We use it to mean "already simplified for this insn". */
3428 for (iv = insn_code_values[i]; iv; iv = iv->next)
3429 clear_struct_flag (iv->av->value);
3431 for (iv = insn_code_values[i]; iv; iv = iv->next)
3433 struct obstack *old = rtl_obstack;
3435 attr = iv->attr;
3436 av = iv->av;
3437 ie = iv->ie;
3438 if (GET_CODE (av->value) != COND)
3439 continue;
3441 rtl_obstack = temp_obstack;
3442 newexp = av->value;
3443 while (GET_CODE (newexp) == COND)
3445 rtx newexp2 = simplify_cond (newexp, ie->insn_code,
3446 ie->insn_index);
3447 if (newexp2 == newexp)
3448 break;
3449 newexp = newexp2;
3452 rtl_obstack = old;
3453 if (newexp != av->value)
3455 newexp = attr_copy_rtx (newexp);
3456 remove_insn_ent (av, ie);
3457 av = get_attr_value (newexp, attr, ie->insn_code);
3458 iv->av = av;
3459 insert_insn_ent (av, ie);
3464 free (ivbuf);
3465 free (insn_code_values - 2);
3468 /* If EXP is a suitable expression, reorganize it by constructing an
3469 equivalent expression that is a COND with the tests being all combinations
3470 of attribute values and the values being simple constants. */
3472 static rtx
3473 simplify_by_exploding (rtx exp)
3475 rtx list = 0, link, condexp, defval = NULL_RTX;
3476 struct dimension *space;
3477 rtx *condtest, *condval;
3478 int i, j, total, ndim = 0;
3479 int most_tests, num_marks, new_marks;
3480 rtx ret;
3482 /* Locate all the EQ_ATTR expressions. */
3483 if (! find_and_mark_used_attributes (exp, &list, &ndim) || ndim == 0)
3485 unmark_used_attributes (list, 0, 0);
3486 return exp;
3489 /* Create an attribute space from the list of used attributes. For each
3490 dimension in the attribute space, record the attribute, list of values
3491 used, and number of values used. Add members to the list of values to
3492 cover the domain of the attribute. This makes the expanded COND form
3493 order independent. */
3495 space = xmalloc (ndim * sizeof (struct dimension));
3497 total = 1;
3498 for (ndim = 0; list; ndim++)
3500 /* Pull the first attribute value from the list and record that
3501 attribute as another dimension in the attribute space. */
3502 const char *name = XSTR (XEXP (list, 0), 0);
3503 rtx *prev;
3505 if ((space[ndim].attr = find_attr (name, 0)) == 0
3506 || space[ndim].attr->is_numeric)
3508 unmark_used_attributes (list, space, ndim);
3509 return exp;
3512 /* Add all remaining attribute values that refer to this attribute. */
3513 space[ndim].num_values = 0;
3514 space[ndim].values = 0;
3515 prev = &list;
3516 for (link = list; link; link = *prev)
3517 if (! strcmp (XSTR (XEXP (link, 0), 0), name))
3519 space[ndim].num_values++;
3520 *prev = XEXP (link, 1);
3521 XEXP (link, 1) = space[ndim].values;
3522 space[ndim].values = link;
3524 else
3525 prev = &XEXP (link, 1);
3527 /* Add sufficient members to the list of values to make the list
3528 mutually exclusive and record the total size of the attribute
3529 space. */
3530 total *= add_values_to_cover (&space[ndim]);
3533 /* Sort the attribute space so that the attributes go from non-constant
3534 to constant and from most values to least values. */
3535 for (i = 0; i < ndim; i++)
3536 for (j = ndim - 1; j > i; j--)
3537 if ((space[j-1].attr->is_const && !space[j].attr->is_const)
3538 || space[j-1].num_values < space[j].num_values)
3540 struct dimension tmp;
3541 tmp = space[j];
3542 space[j] = space[j - 1];
3543 space[j - 1] = tmp;
3546 /* Establish the initial current value. */
3547 for (i = 0; i < ndim; i++)
3548 space[i].current_value = space[i].values;
3550 condtest = xmalloc (total * sizeof (rtx));
3551 condval = xmalloc (total * sizeof (rtx));
3553 /* Expand the tests and values by iterating over all values in the
3554 attribute space. */
3555 for (i = 0;; i++)
3557 condtest[i] = test_for_current_value (space, ndim);
3558 condval[i] = simplify_with_current_value (exp, space, ndim);
3559 if (! increment_current_value (space, ndim))
3560 break;
3562 if (i != total - 1)
3563 abort ();
3565 /* We are now finished with the original expression. */
3566 unmark_used_attributes (0, space, ndim);
3567 free (space);
3569 /* Find the most used constant value and make that the default. */
3570 most_tests = -1;
3571 for (i = num_marks = 0; i < total; i++)
3572 if (GET_CODE (condval[i]) == CONST_STRING
3573 && ! ATTR_EQ_ATTR_P (condval[i]))
3575 /* Mark the unmarked constant value and count how many are marked. */
3576 ATTR_EQ_ATTR_P (condval[i]) = 1;
3577 for (j = new_marks = 0; j < total; j++)
3578 if (GET_CODE (condval[j]) == CONST_STRING
3579 && ATTR_EQ_ATTR_P (condval[j]))
3580 new_marks++;
3581 if (new_marks - num_marks > most_tests)
3583 most_tests = new_marks - num_marks;
3584 defval = condval[i];
3586 num_marks = new_marks;
3588 /* Clear all the marks. */
3589 for (i = 0; i < total; i++)
3590 ATTR_EQ_ATTR_P (condval[i]) = 0;
3592 /* Give up if nothing is constant. */
3593 if (num_marks == 0)
3594 ret = exp;
3596 /* If all values are the default, use that. */
3597 else if (total == most_tests)
3598 ret = defval;
3600 /* Make a COND with the most common constant value the default. (A more
3601 complex method where tests with the same value were combined didn't
3602 seem to improve things.) */
3603 else
3605 condexp = rtx_alloc (COND);
3606 XVEC (condexp, 0) = rtvec_alloc ((total - most_tests) * 2);
3607 XEXP (condexp, 1) = defval;
3608 for (i = j = 0; i < total; i++)
3609 if (condval[i] != defval)
3611 XVECEXP (condexp, 0, 2 * j) = condtest[i];
3612 XVECEXP (condexp, 0, 2 * j + 1) = condval[i];
3613 j++;
3615 ret = condexp;
3617 free (condtest);
3618 free (condval);
3619 return ret;
3622 /* Set the ATTR_EQ_ATTR_P flag for all EQ_ATTR expressions in EXP and
3623 verify that EXP can be simplified to a constant term if all the EQ_ATTR
3624 tests have known value. */
3626 static int
3627 find_and_mark_used_attributes (rtx exp, rtx *terms, int *nterms)
3629 int i;
3631 switch (GET_CODE (exp))
3633 case EQ_ATTR:
3634 if (! ATTR_EQ_ATTR_P (exp))
3636 rtx link = rtx_alloc (EXPR_LIST);
3637 XEXP (link, 0) = exp;
3638 XEXP (link, 1) = *terms;
3639 *terms = link;
3640 *nterms += 1;
3641 ATTR_EQ_ATTR_P (exp) = 1;
3643 return 1;
3645 case CONST_STRING:
3646 case CONST_INT:
3647 return 1;
3649 case IF_THEN_ELSE:
3650 if (! find_and_mark_used_attributes (XEXP (exp, 2), terms, nterms))
3651 return 0;
3652 case IOR:
3653 case AND:
3654 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3655 return 0;
3656 case NOT:
3657 if (! find_and_mark_used_attributes (XEXP (exp, 0), terms, nterms))
3658 return 0;
3659 return 1;
3661 case COND:
3662 for (i = 0; i < XVECLEN (exp, 0); i++)
3663 if (! find_and_mark_used_attributes (XVECEXP (exp, 0, i), terms, nterms))
3664 return 0;
3665 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3666 return 0;
3667 return 1;
3669 default:
3670 return 0;
3674 /* Clear the ATTR_EQ_ATTR_P flag in all EQ_ATTR expressions on LIST and
3675 in the values of the NDIM-dimensional attribute space SPACE. */
3677 static void
3678 unmark_used_attributes (rtx list, struct dimension *space, int ndim)
3680 rtx link, exp;
3681 int i;
3683 for (i = 0; i < ndim; i++)
3684 unmark_used_attributes (space[i].values, 0, 0);
3686 for (link = list; link; link = XEXP (link, 1))
3688 exp = XEXP (link, 0);
3689 if (GET_CODE (exp) == EQ_ATTR)
3690 ATTR_EQ_ATTR_P (exp) = 0;
3694 /* Update the attribute dimension DIM so that all values of the attribute
3695 are tested. Return the updated number of values. */
3697 static int
3698 add_values_to_cover (struct dimension *dim)
3700 struct attr_value *av;
3701 rtx exp, link, *prev;
3702 int nalt = 0;
3704 for (av = dim->attr->first_value; av; av = av->next)
3705 if (GET_CODE (av->value) == CONST_STRING)
3706 nalt++;
3708 if (nalt < dim->num_values)
3709 abort ();
3710 else if (nalt == dim->num_values)
3711 /* OK. */
3713 else if (nalt * 2 < dim->num_values * 3)
3715 /* Most all the values of the attribute are used, so add all the unused
3716 values. */
3717 prev = &dim->values;
3718 for (link = dim->values; link; link = *prev)
3719 prev = &XEXP (link, 1);
3721 for (av = dim->attr->first_value; av; av = av->next)
3722 if (GET_CODE (av->value) == CONST_STRING)
3724 exp = attr_eq (dim->attr->name, XSTR (av->value, 0));
3725 if (ATTR_EQ_ATTR_P (exp))
3726 continue;
3728 link = rtx_alloc (EXPR_LIST);
3729 XEXP (link, 0) = exp;
3730 XEXP (link, 1) = 0;
3731 *prev = link;
3732 prev = &XEXP (link, 1);
3734 dim->num_values = nalt;
3736 else
3738 rtx orexp = false_rtx;
3740 /* Very few values are used, so compute a mutually exclusive
3741 expression. (We could do this for numeric values if that becomes
3742 important.) */
3743 prev = &dim->values;
3744 for (link = dim->values; link; link = *prev)
3746 orexp = insert_right_side (IOR, orexp, XEXP (link, 0), -2, -2);
3747 prev = &XEXP (link, 1);
3749 link = rtx_alloc (EXPR_LIST);
3750 XEXP (link, 0) = attr_rtx (NOT, orexp);
3751 XEXP (link, 1) = 0;
3752 *prev = link;
3753 dim->num_values++;
3755 return dim->num_values;
3758 /* Increment the current value for the NDIM-dimensional attribute space SPACE
3759 and return FALSE if the increment overflowed. */
3761 static int
3762 increment_current_value (struct dimension *space, int ndim)
3764 int i;
3766 for (i = ndim - 1; i >= 0; i--)
3768 if ((space[i].current_value = XEXP (space[i].current_value, 1)) == 0)
3769 space[i].current_value = space[i].values;
3770 else
3771 return 1;
3773 return 0;
3776 /* Construct an expression corresponding to the current value for the
3777 NDIM-dimensional attribute space SPACE. */
3779 static rtx
3780 test_for_current_value (struct dimension *space, int ndim)
3782 int i;
3783 rtx exp = true_rtx;
3785 for (i = 0; i < ndim; i++)
3786 exp = insert_right_side (AND, exp, XEXP (space[i].current_value, 0),
3787 -2, -2);
3789 return exp;
3792 /* Given the current value of the NDIM-dimensional attribute space SPACE,
3793 set the corresponding EQ_ATTR expressions to that value and reduce
3794 the expression EXP as much as possible. On input [and output], all
3795 known EQ_ATTR expressions are set to FALSE. */
3797 static rtx
3798 simplify_with_current_value (rtx exp, struct dimension *space, int ndim)
3800 int i;
3801 rtx x;
3803 /* Mark each current value as TRUE. */
3804 for (i = 0; i < ndim; i++)
3806 x = XEXP (space[i].current_value, 0);
3807 if (GET_CODE (x) == EQ_ATTR)
3808 ATTR_EQ_ATTR_P (x) = 0;
3811 exp = simplify_with_current_value_aux (exp);
3813 /* Change each current value back to FALSE. */
3814 for (i = 0; i < ndim; i++)
3816 x = XEXP (space[i].current_value, 0);
3817 if (GET_CODE (x) == EQ_ATTR)
3818 ATTR_EQ_ATTR_P (x) = 1;
3821 return exp;
3824 /* Reduce the expression EXP based on the ATTR_EQ_ATTR_P settings of
3825 all EQ_ATTR expressions. */
3827 static rtx
3828 simplify_with_current_value_aux (rtx exp)
3830 int i;
3831 rtx cond;
3833 switch (GET_CODE (exp))
3835 case EQ_ATTR:
3836 if (ATTR_EQ_ATTR_P (exp))
3837 return false_rtx;
3838 else
3839 return true_rtx;
3840 case CONST_STRING:
3841 case CONST_INT:
3842 return exp;
3844 case IF_THEN_ELSE:
3845 cond = simplify_with_current_value_aux (XEXP (exp, 0));
3846 if (cond == true_rtx)
3847 return simplify_with_current_value_aux (XEXP (exp, 1));
3848 else if (cond == false_rtx)
3849 return simplify_with_current_value_aux (XEXP (exp, 2));
3850 else
3851 return attr_rtx (IF_THEN_ELSE, cond,
3852 simplify_with_current_value_aux (XEXP (exp, 1)),
3853 simplify_with_current_value_aux (XEXP (exp, 2)));
3855 case IOR:
3856 cond = simplify_with_current_value_aux (XEXP (exp, 1));
3857 if (cond == true_rtx)
3858 return cond;
3859 else if (cond == false_rtx)
3860 return simplify_with_current_value_aux (XEXP (exp, 0));
3861 else
3862 return attr_rtx (IOR, cond,
3863 simplify_with_current_value_aux (XEXP (exp, 0)));
3865 case AND:
3866 cond = simplify_with_current_value_aux (XEXP (exp, 1));
3867 if (cond == true_rtx)
3868 return simplify_with_current_value_aux (XEXP (exp, 0));
3869 else if (cond == false_rtx)
3870 return cond;
3871 else
3872 return attr_rtx (AND, cond,
3873 simplify_with_current_value_aux (XEXP (exp, 0)));
3875 case NOT:
3876 cond = simplify_with_current_value_aux (XEXP (exp, 0));
3877 if (cond == true_rtx)
3878 return false_rtx;
3879 else if (cond == false_rtx)
3880 return true_rtx;
3881 else
3882 return attr_rtx (NOT, cond);
3884 case COND:
3885 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3887 cond = simplify_with_current_value_aux (XVECEXP (exp, 0, i));
3888 if (cond == true_rtx)
3889 return simplify_with_current_value_aux (XVECEXP (exp, 0, i + 1));
3890 else if (cond == false_rtx)
3891 continue;
3892 else
3893 abort (); /* With all EQ_ATTR's of known value, a case should
3894 have been selected. */
3896 return simplify_with_current_value_aux (XEXP (exp, 1));
3898 default:
3899 abort ();
3903 /* Clear the ATTR_CURR_SIMPLIFIED_P flag in EXP and its subexpressions. */
3905 static void
3906 clear_struct_flag (rtx x)
3908 int i;
3909 int j;
3910 enum rtx_code code;
3911 const char *fmt;
3913 ATTR_CURR_SIMPLIFIED_P (x) = 0;
3914 if (ATTR_IND_SIMPLIFIED_P (x))
3915 return;
3917 code = GET_CODE (x);
3919 switch (code)
3921 case REG:
3922 case QUEUED:
3923 case CONST_INT:
3924 case CONST_DOUBLE:
3925 case CONST_VECTOR:
3926 case SYMBOL_REF:
3927 case CODE_LABEL:
3928 case PC:
3929 case CC0:
3930 case EQ_ATTR:
3931 case ATTR_FLAG:
3932 return;
3934 default:
3935 break;
3938 /* Compare the elements. If any pair of corresponding elements
3939 fail to match, return 0 for the whole things. */
3941 fmt = GET_RTX_FORMAT (code);
3942 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3944 switch (fmt[i])
3946 case 'V':
3947 case 'E':
3948 for (j = 0; j < XVECLEN (x, i); j++)
3949 clear_struct_flag (XVECEXP (x, i, j));
3950 break;
3952 case 'e':
3953 clear_struct_flag (XEXP (x, i));
3954 break;
3959 /* Return the number of RTX objects making up the expression X.
3960 But if we count more than MAX objects, stop counting. */
3962 static int
3963 count_sub_rtxs (rtx x, int max)
3965 int i;
3966 int j;
3967 enum rtx_code code;
3968 const char *fmt;
3969 int total = 0;
3971 code = GET_CODE (x);
3973 switch (code)
3975 case REG:
3976 case QUEUED:
3977 case CONST_INT:
3978 case CONST_DOUBLE:
3979 case CONST_VECTOR:
3980 case SYMBOL_REF:
3981 case CODE_LABEL:
3982 case PC:
3983 case CC0:
3984 case EQ_ATTR:
3985 case ATTR_FLAG:
3986 return 1;
3988 default:
3989 break;
3992 /* Compare the elements. If any pair of corresponding elements
3993 fail to match, return 0 for the whole things. */
3995 fmt = GET_RTX_FORMAT (code);
3996 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3998 if (total >= max)
3999 return total;
4001 switch (fmt[i])
4003 case 'V':
4004 case 'E':
4005 for (j = 0; j < XVECLEN (x, i); j++)
4006 total += count_sub_rtxs (XVECEXP (x, i, j), max);
4007 break;
4009 case 'e':
4010 total += count_sub_rtxs (XEXP (x, i), max);
4011 break;
4014 return total;
4018 /* Create table entries for DEFINE_ATTR. */
4020 static void
4021 gen_attr (rtx exp, int lineno)
4023 struct attr_desc *attr;
4024 struct attr_value *av;
4025 const char *name_ptr;
4026 char *p;
4028 /* Make a new attribute structure. Check for duplicate by looking at
4029 attr->default_val, since it is initialized by this routine. */
4030 attr = find_attr (XSTR (exp, 0), 1);
4031 if (attr->default_val)
4033 message_with_line (lineno, "duplicate definition for attribute %s",
4034 attr->name);
4035 message_with_line (attr->lineno, "previous definition");
4036 have_error = 1;
4037 return;
4039 attr->lineno = lineno;
4041 if (*XSTR (exp, 1) == '\0')
4042 attr->is_numeric = 1;
4043 else
4045 name_ptr = XSTR (exp, 1);
4046 while ((p = next_comma_elt (&name_ptr)) != NULL)
4048 av = oballoc (sizeof (struct attr_value));
4049 av->value = attr_rtx (CONST_STRING, p);
4050 av->next = attr->first_value;
4051 attr->first_value = av;
4052 av->first_insn = NULL;
4053 av->num_insns = 0;
4054 av->has_asm_insn = 0;
4058 if (GET_CODE (XEXP (exp, 2)) == CONST)
4060 attr->is_const = 1;
4061 if (attr->is_numeric)
4063 message_with_line (lineno,
4064 "constant attributes may not take numeric values");
4065 have_error = 1;
4068 /* Get rid of the CONST node. It is allowed only at top-level. */
4069 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
4072 if (! strcmp (attr->name, "length") && ! attr->is_numeric)
4074 message_with_line (lineno,
4075 "`length' attribute must take numeric values");
4076 have_error = 1;
4079 /* Set up the default value. */
4080 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
4081 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
4084 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
4085 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
4086 number of alternatives as this should be checked elsewhere. */
4088 static int
4089 count_alternatives (rtx exp)
4091 int i, j, n;
4092 const char *fmt;
4094 if (GET_CODE (exp) == MATCH_OPERAND)
4095 return n_comma_elts (XSTR (exp, 2));
4097 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4098 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4099 switch (*fmt++)
4101 case 'e':
4102 case 'u':
4103 n = count_alternatives (XEXP (exp, i));
4104 if (n)
4105 return n;
4106 break;
4108 case 'E':
4109 case 'V':
4110 if (XVEC (exp, i) != NULL)
4111 for (j = 0; j < XVECLEN (exp, i); j++)
4113 n = count_alternatives (XVECEXP (exp, i, j));
4114 if (n)
4115 return n;
4119 return 0;
4122 /* Returns nonzero if the given expression contains an EQ_ATTR with the
4123 `alternative' attribute. */
4125 static int
4126 compares_alternatives_p (rtx exp)
4128 int i, j;
4129 const char *fmt;
4131 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
4132 return 1;
4134 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4135 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4136 switch (*fmt++)
4138 case 'e':
4139 case 'u':
4140 if (compares_alternatives_p (XEXP (exp, i)))
4141 return 1;
4142 break;
4144 case 'E':
4145 for (j = 0; j < XVECLEN (exp, i); j++)
4146 if (compares_alternatives_p (XVECEXP (exp, i, j)))
4147 return 1;
4148 break;
4151 return 0;
4154 /* Returns nonzero is INNER is contained in EXP. */
4156 static int
4157 contained_in_p (rtx inner, rtx exp)
4159 int i, j;
4160 const char *fmt;
4162 if (rtx_equal_p (inner, exp))
4163 return 1;
4165 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4166 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4167 switch (*fmt++)
4169 case 'e':
4170 case 'u':
4171 if (contained_in_p (inner, XEXP (exp, i)))
4172 return 1;
4173 break;
4175 case 'E':
4176 for (j = 0; j < XVECLEN (exp, i); j++)
4177 if (contained_in_p (inner, XVECEXP (exp, i, j)))
4178 return 1;
4179 break;
4182 return 0;
4185 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
4187 static void
4188 gen_insn (rtx exp, int lineno)
4190 struct insn_def *id;
4192 id = oballoc (sizeof (struct insn_def));
4193 id->next = defs;
4194 defs = id;
4195 id->def = exp;
4196 id->lineno = lineno;
4198 switch (GET_CODE (exp))
4200 case DEFINE_INSN:
4201 id->insn_code = insn_code_number;
4202 id->insn_index = insn_index_number;
4203 id->num_alternatives = count_alternatives (exp);
4204 if (id->num_alternatives == 0)
4205 id->num_alternatives = 1;
4206 id->vec_idx = 4;
4207 break;
4209 case DEFINE_PEEPHOLE:
4210 id->insn_code = insn_code_number;
4211 id->insn_index = insn_index_number;
4212 id->num_alternatives = count_alternatives (exp);
4213 if (id->num_alternatives == 0)
4214 id->num_alternatives = 1;
4215 id->vec_idx = 3;
4216 break;
4218 case DEFINE_ASM_ATTRIBUTES:
4219 id->insn_code = -1;
4220 id->insn_index = -1;
4221 id->num_alternatives = 1;
4222 id->vec_idx = 0;
4223 got_define_asm_attributes = 1;
4224 break;
4226 default:
4227 abort ();
4231 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
4232 true or annul false is specified, and make a `struct delay_desc'. */
4234 static void
4235 gen_delay (rtx def, int lineno)
4237 struct delay_desc *delay;
4238 int i;
4240 if (XVECLEN (def, 1) % 3 != 0)
4242 message_with_line (lineno,
4243 "number of elements in DEFINE_DELAY must be multiple of three");
4244 have_error = 1;
4245 return;
4248 for (i = 0; i < XVECLEN (def, 1); i += 3)
4250 if (XVECEXP (def, 1, i + 1))
4251 have_annul_true = 1;
4252 if (XVECEXP (def, 1, i + 2))
4253 have_annul_false = 1;
4256 delay = oballoc (sizeof (struct delay_desc));
4257 delay->def = def;
4258 delay->num = ++num_delays;
4259 delay->next = delays;
4260 delay->lineno = lineno;
4261 delays = delay;
4264 /* Process a DEFINE_FUNCTION_UNIT.
4266 This gives information about a function unit contained in the CPU.
4267 We fill in a `struct function_unit_op' and a `struct function_unit'
4268 with information used later by `expand_unit'. */
4270 static void
4271 gen_unit (rtx def, int lineno)
4273 struct function_unit *unit;
4274 struct function_unit_op *op;
4275 const char *name = XSTR (def, 0);
4276 int multiplicity = XINT (def, 1);
4277 int simultaneity = XINT (def, 2);
4278 rtx condexp = XEXP (def, 3);
4279 int ready_cost = MAX (XINT (def, 4), 1);
4280 int issue_delay = MAX (XINT (def, 5), 1);
4282 /* See if we have already seen this function unit. If so, check that
4283 the multiplicity and simultaneity values are the same. If not, make
4284 a structure for this function unit. */
4285 for (unit = units; unit; unit = unit->next)
4286 if (! strcmp (unit->name, name))
4288 if (unit->multiplicity != multiplicity
4289 || unit->simultaneity != simultaneity)
4291 message_with_line (lineno,
4292 "differing specifications given for function unit %s",
4293 unit->name);
4294 message_with_line (unit->first_lineno, "previous definition");
4295 have_error = 1;
4296 return;
4298 break;
4301 if (unit == 0)
4303 unit = oballoc (sizeof (struct function_unit));
4304 unit->name = name;
4305 unit->multiplicity = multiplicity;
4306 unit->simultaneity = simultaneity;
4307 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
4308 unit->num = num_units++;
4309 unit->num_opclasses = 0;
4310 unit->condexp = false_rtx;
4311 unit->ops = 0;
4312 unit->next = units;
4313 unit->first_lineno = lineno;
4314 units = unit;
4317 /* Make a new operation class structure entry and initialize it. */
4318 op = oballoc (sizeof (struct function_unit_op));
4319 op->condexp = condexp;
4320 op->num = unit->num_opclasses++;
4321 op->ready = ready_cost;
4322 op->issue_delay = issue_delay;
4323 op->next = unit->ops;
4324 op->lineno = lineno;
4325 unit->ops = op;
4326 num_unit_opclasses++;
4328 /* Set our issue expression based on whether or not an optional conflict
4329 vector was specified. */
4330 if (XVEC (def, 6))
4332 /* Compute the IOR of all the specified expressions. */
4333 rtx orexp = false_rtx;
4334 int i;
4336 for (i = 0; i < XVECLEN (def, 6); i++)
4337 orexp = insert_right_side (IOR, orexp, XVECEXP (def, 6, i), -2, -2);
4339 op->conflict_exp = orexp;
4340 extend_range (&unit->issue_delay, 1, issue_delay);
4342 else
4344 op->conflict_exp = true_rtx;
4345 extend_range (&unit->issue_delay, issue_delay, issue_delay);
4348 /* Merge our conditional into that of the function unit so we can determine
4349 which insns are used by the function unit. */
4350 unit->condexp = insert_right_side (IOR, unit->condexp, op->condexp, -2, -2);
4353 /* Given a piece of RTX, print a C expression to test its truth value.
4354 We use AND and IOR both for logical and bit-wise operations, so
4355 interpret them as logical unless they are inside a comparison expression.
4356 The first bit of FLAGS will be nonzero in that case.
4358 Set the second bit of FLAGS to make references to attribute values use
4359 a cached local variable instead of calling a function. */
4361 static void
4362 write_test_expr (rtx exp, int flags)
4364 int comparison_operator = 0;
4365 RTX_CODE code;
4366 struct attr_desc *attr;
4368 /* In order not to worry about operator precedence, surround our part of
4369 the expression with parentheses. */
4371 printf ("(");
4372 code = GET_CODE (exp);
4373 switch (code)
4375 /* Binary operators. */
4376 case EQ: case NE:
4377 case GE: case GT: case GEU: case GTU:
4378 case LE: case LT: case LEU: case LTU:
4379 comparison_operator = 1;
4381 case PLUS: case MINUS: case MULT: case DIV: case MOD:
4382 case AND: case IOR: case XOR:
4383 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4384 write_test_expr (XEXP (exp, 0), flags | comparison_operator);
4385 switch (code)
4387 case EQ:
4388 printf (" == ");
4389 break;
4390 case NE:
4391 printf (" != ");
4392 break;
4393 case GE:
4394 printf (" >= ");
4395 break;
4396 case GT:
4397 printf (" > ");
4398 break;
4399 case GEU:
4400 printf (" >= (unsigned) ");
4401 break;
4402 case GTU:
4403 printf (" > (unsigned) ");
4404 break;
4405 case LE:
4406 printf (" <= ");
4407 break;
4408 case LT:
4409 printf (" < ");
4410 break;
4411 case LEU:
4412 printf (" <= (unsigned) ");
4413 break;
4414 case LTU:
4415 printf (" < (unsigned) ");
4416 break;
4417 case PLUS:
4418 printf (" + ");
4419 break;
4420 case MINUS:
4421 printf (" - ");
4422 break;
4423 case MULT:
4424 printf (" * ");
4425 break;
4426 case DIV:
4427 printf (" / ");
4428 break;
4429 case MOD:
4430 printf (" %% ");
4431 break;
4432 case AND:
4433 if (flags & 1)
4434 printf (" & ");
4435 else
4436 printf (" && ");
4437 break;
4438 case IOR:
4439 if (flags & 1)
4440 printf (" | ");
4441 else
4442 printf (" || ");
4443 break;
4444 case XOR:
4445 printf (" ^ ");
4446 break;
4447 case ASHIFT:
4448 printf (" << ");
4449 break;
4450 case LSHIFTRT:
4451 case ASHIFTRT:
4452 printf (" >> ");
4453 break;
4454 default:
4455 abort ();
4458 write_test_expr (XEXP (exp, 1), flags | comparison_operator);
4459 break;
4461 case NOT:
4462 /* Special-case (not (eq_attrq "alternative" "x")) */
4463 if (! (flags & 1) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
4464 && XSTR (XEXP (exp, 0), 0) == alternative_name)
4466 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
4467 break;
4470 /* Otherwise, fall through to normal unary operator. */
4472 /* Unary operators. */
4473 case ABS: case NEG:
4474 switch (code)
4476 case NOT:
4477 if (flags & 1)
4478 printf ("~ ");
4479 else
4480 printf ("! ");
4481 break;
4482 case ABS:
4483 printf ("abs ");
4484 break;
4485 case NEG:
4486 printf ("-");
4487 break;
4488 default:
4489 abort ();
4492 write_test_expr (XEXP (exp, 0), flags);
4493 break;
4495 /* Comparison test of an attribute with a value. Most of these will
4496 have been removed by optimization. Handle "alternative"
4497 specially and give error if EQ_ATTR present inside a comparison. */
4498 case EQ_ATTR:
4499 if (flags & 1)
4500 fatal ("EQ_ATTR not valid inside comparison");
4502 if (XSTR (exp, 0) == alternative_name)
4504 printf ("which_alternative == %s", XSTR (exp, 1));
4505 break;
4508 attr = find_attr (XSTR (exp, 0), 0);
4509 if (! attr)
4510 abort ();
4512 /* Now is the time to expand the value of a constant attribute. */
4513 if (attr->is_const)
4515 write_test_expr (evaluate_eq_attr (exp, attr->default_val->value,
4516 -2, -2),
4517 flags);
4519 else
4521 if (flags & 2)
4522 printf ("attr_%s", attr->name);
4523 else
4524 printf ("get_attr_%s (insn)", attr->name);
4525 printf (" == ");
4526 write_attr_valueq (attr, XSTR (exp, 1));
4528 break;
4530 /* Comparison test of flags for define_delays. */
4531 case ATTR_FLAG:
4532 if (flags & 1)
4533 fatal ("ATTR_FLAG not valid inside comparison");
4534 printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
4535 break;
4537 /* See if an operand matches a predicate. */
4538 case MATCH_OPERAND:
4539 /* If only a mode is given, just ensure the mode matches the operand.
4540 If neither a mode nor predicate is given, error. */
4541 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
4543 if (GET_MODE (exp) == VOIDmode)
4544 fatal ("null MATCH_OPERAND specified as test");
4545 else
4546 printf ("GET_MODE (operands[%d]) == %smode",
4547 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4549 else
4550 printf ("%s (operands[%d], %smode)",
4551 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4552 break;
4554 case MATCH_INSN:
4555 printf ("%s (insn)", XSTR (exp, 0));
4556 break;
4558 /* Constant integer. */
4559 case CONST_INT:
4560 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
4561 break;
4563 /* A random C expression. */
4564 case SYMBOL_REF:
4565 printf ("%s", XSTR (exp, 0));
4566 break;
4568 /* The address of the branch target. */
4569 case MATCH_DUP:
4570 printf ("INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
4571 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
4572 break;
4574 case PC:
4575 /* The address of the current insn. We implement this actually as the
4576 address of the current insn for backward branches, but the last
4577 address of the next insn for forward branches, and both with
4578 adjustments that account for the worst-case possible stretching of
4579 intervening alignments between this insn and its destination. */
4580 printf ("insn_current_reference_address (insn)");
4581 break;
4583 case CONST_STRING:
4584 printf ("%s", XSTR (exp, 0));
4585 break;
4587 case IF_THEN_ELSE:
4588 write_test_expr (XEXP (exp, 0), flags & 2);
4589 printf (" ? ");
4590 write_test_expr (XEXP (exp, 1), flags | 1);
4591 printf (" : ");
4592 write_test_expr (XEXP (exp, 2), flags | 1);
4593 break;
4595 default:
4596 fatal ("bad RTX code `%s' in attribute calculation\n",
4597 GET_RTX_NAME (code));
4600 printf (")");
4603 /* Given an attribute value, return the maximum CONST_STRING argument
4604 encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
4606 static int
4607 max_attr_value (rtx exp, int *unknownp)
4609 int current_max;
4610 int i, n;
4612 switch (GET_CODE (exp))
4614 case CONST_STRING:
4615 current_max = atoi (XSTR (exp, 0));
4616 break;
4618 case COND:
4619 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4620 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4622 n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4623 if (n > current_max)
4624 current_max = n;
4626 break;
4628 case IF_THEN_ELSE:
4629 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4630 n = max_attr_value (XEXP (exp, 2), unknownp);
4631 if (n > current_max)
4632 current_max = n;
4633 break;
4635 default:
4636 *unknownp = 1;
4637 current_max = INT_MAX;
4638 break;
4641 return current_max;
4644 /* Given an attribute value, return the result of ORing together all
4645 CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
4646 if the numeric value is not known. */
4648 static int
4649 or_attr_value (rtx exp, int *unknownp)
4651 int current_or;
4652 int i;
4654 switch (GET_CODE (exp))
4656 case CONST_STRING:
4657 current_or = atoi (XSTR (exp, 0));
4658 break;
4660 case COND:
4661 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4662 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4663 current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4664 break;
4666 case IF_THEN_ELSE:
4667 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4668 current_or |= or_attr_value (XEXP (exp, 2), unknownp);
4669 break;
4671 default:
4672 *unknownp = 1;
4673 current_or = -1;
4674 break;
4677 return current_or;
4680 /* Scan an attribute value, possibly a conditional, and record what actions
4681 will be required to do any conditional tests in it.
4683 Specifically, set
4684 `must_extract' if we need to extract the insn operands
4685 `must_constrain' if we must compute `which_alternative'
4686 `address_used' if an address expression was used
4687 `length_used' if an (eq_attr "length" ...) was used
4690 static void
4691 walk_attr_value (rtx exp)
4693 int i, j;
4694 const char *fmt;
4695 RTX_CODE code;
4697 if (exp == NULL)
4698 return;
4700 code = GET_CODE (exp);
4701 switch (code)
4703 case SYMBOL_REF:
4704 if (! ATTR_IND_SIMPLIFIED_P (exp))
4705 /* Since this is an arbitrary expression, it can look at anything.
4706 However, constant expressions do not depend on any particular
4707 insn. */
4708 must_extract = must_constrain = 1;
4709 return;
4711 case MATCH_OPERAND:
4712 must_extract = 1;
4713 return;
4715 case EQ_ATTR:
4716 if (XSTR (exp, 0) == alternative_name)
4717 must_extract = must_constrain = 1;
4718 else if (strcmp (XSTR (exp, 0), "length") == 0)
4719 length_used = 1;
4720 return;
4722 case MATCH_DUP:
4723 must_extract = 1;
4724 address_used = 1;
4725 return;
4727 case PC:
4728 address_used = 1;
4729 return;
4731 case ATTR_FLAG:
4732 return;
4734 default:
4735 break;
4738 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
4739 switch (*fmt++)
4741 case 'e':
4742 case 'u':
4743 walk_attr_value (XEXP (exp, i));
4744 break;
4746 case 'E':
4747 if (XVEC (exp, i) != NULL)
4748 for (j = 0; j < XVECLEN (exp, i); j++)
4749 walk_attr_value (XVECEXP (exp, i, j));
4750 break;
4754 /* Write out a function to obtain the attribute for a given INSN. */
4756 static void
4757 write_attr_get (struct attr_desc *attr)
4759 struct attr_value *av, *common_av;
4761 /* Find the most used attribute value. Handle that as the `default' of the
4762 switch we will generate. */
4763 common_av = find_most_used (attr);
4765 /* Write out start of function, then all values with explicit `case' lines,
4766 then a `default', then the value with the most uses. */
4767 if (attr->static_p)
4768 printf ("static ");
4769 if (!attr->is_numeric)
4770 printf ("enum attr_%s\n", attr->name);
4771 else if (attr->unsigned_p)
4772 printf ("unsigned int\n");
4773 else
4774 printf ("int\n");
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 insn ATTRIBUTE_UNUSED)\n", &attr->name[1]);
4780 else if (attr->is_const == 0)
4781 printf ("get_attr_%s (rtx insn ATTRIBUTE_UNUSED)\n", attr->name);
4782 else
4784 printf ("get_attr_%s (void)\n", attr->name);
4785 printf ("{\n");
4787 for (av = attr->first_value; av; av = av->next)
4788 if (av->num_insns != 0)
4789 write_attr_set (attr, 2, av->value, "return", ";",
4790 true_rtx, av->first_insn->insn_code,
4791 av->first_insn->insn_index);
4793 printf ("}\n\n");
4794 return;
4797 printf ("{\n");
4799 if (GET_CODE (common_av->value) == FFS)
4801 rtx p = XEXP (common_av->value, 0);
4803 /* No need to emit code to abort if the insn is unrecognized; the
4804 other get_attr_foo functions will do that when we call them. */
4806 write_toplevel_expr (p);
4808 printf ("\n if (accum && accum == (accum & -accum))\n");
4809 printf (" {\n");
4810 printf (" int i;\n");
4811 printf (" for (i = 0; accum >>= 1; ++i) continue;\n");
4812 printf (" accum = i;\n");
4813 printf (" }\n else\n");
4814 printf (" accum = ~accum;\n");
4815 printf (" return accum;\n}\n\n");
4817 else
4819 printf (" switch (recog_memoized (insn))\n");
4820 printf (" {\n");
4822 for (av = attr->first_value; av; av = av->next)
4823 if (av != common_av)
4824 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
4826 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
4827 printf (" }\n}\n\n");
4831 /* Given an AND tree of known true terms (because we are inside an `if' with
4832 that as the condition or are in an `else' clause) and an expression,
4833 replace any known true terms with TRUE. Use `simplify_and_tree' to do
4834 the bulk of the work. */
4836 static rtx
4837 eliminate_known_true (rtx known_true, rtx exp, int insn_code, int insn_index)
4839 rtx term;
4841 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
4843 if (GET_CODE (known_true) == AND)
4845 exp = eliminate_known_true (XEXP (known_true, 0), exp,
4846 insn_code, insn_index);
4847 exp = eliminate_known_true (XEXP (known_true, 1), exp,
4848 insn_code, insn_index);
4850 else
4852 term = known_true;
4853 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
4856 return exp;
4859 /* Write out a series of tests and assignment statements to perform tests and
4860 sets of an attribute value. We are passed an indentation amount and prefix
4861 and suffix strings to write around each attribute value (e.g., "return"
4862 and ";"). */
4864 static void
4865 write_attr_set (struct attr_desc *attr, int indent, rtx value,
4866 const char *prefix, const char *suffix, rtx known_true,
4867 int insn_code, int insn_index)
4869 if (GET_CODE (value) == COND)
4871 /* Assume the default value will be the default of the COND unless we
4872 find an always true expression. */
4873 rtx default_val = XEXP (value, 1);
4874 rtx our_known_true = known_true;
4875 rtx newexp;
4876 int first_if = 1;
4877 int i;
4879 for (i = 0; i < XVECLEN (value, 0); i += 2)
4881 rtx testexp;
4882 rtx inner_true;
4884 testexp = eliminate_known_true (our_known_true,
4885 XVECEXP (value, 0, i),
4886 insn_code, insn_index);
4887 newexp = attr_rtx (NOT, testexp);
4888 newexp = insert_right_side (AND, our_known_true, newexp,
4889 insn_code, insn_index);
4891 /* If the test expression is always true or if the next `known_true'
4892 expression is always false, this is the last case, so break
4893 out and let this value be the `else' case. */
4894 if (testexp == true_rtx || newexp == false_rtx)
4896 default_val = XVECEXP (value, 0, i + 1);
4897 break;
4900 /* Compute the expression to pass to our recursive call as being
4901 known true. */
4902 inner_true = insert_right_side (AND, our_known_true,
4903 testexp, insn_code, insn_index);
4905 /* If this is always false, skip it. */
4906 if (inner_true == false_rtx)
4907 continue;
4909 write_indent (indent);
4910 printf ("%sif ", first_if ? "" : "else ");
4911 first_if = 0;
4912 write_test_expr (testexp, 0);
4913 printf ("\n");
4914 write_indent (indent + 2);
4915 printf ("{\n");
4917 write_attr_set (attr, indent + 4,
4918 XVECEXP (value, 0, i + 1), prefix, suffix,
4919 inner_true, insn_code, insn_index);
4920 write_indent (indent + 2);
4921 printf ("}\n");
4922 our_known_true = newexp;
4925 if (! first_if)
4927 write_indent (indent);
4928 printf ("else\n");
4929 write_indent (indent + 2);
4930 printf ("{\n");
4933 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
4934 prefix, suffix, our_known_true, insn_code, insn_index);
4936 if (! first_if)
4938 write_indent (indent + 2);
4939 printf ("}\n");
4942 else
4944 write_indent (indent);
4945 printf ("%s ", prefix);
4946 write_attr_value (attr, value);
4947 printf ("%s\n", suffix);
4951 /* Write out the computation for one attribute value. */
4953 static void
4954 write_attr_case (struct attr_desc *attr, struct attr_value *av,
4955 int write_case_lines, const char *prefix, const char *suffix,
4956 int indent, rtx known_true)
4958 struct insn_ent *ie;
4960 if (av->num_insns == 0)
4961 return;
4963 if (av->has_asm_insn)
4965 write_indent (indent);
4966 printf ("case -1:\n");
4967 write_indent (indent + 2);
4968 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
4969 write_indent (indent + 2);
4970 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
4971 write_indent (indent + 2);
4972 printf (" fatal_insn_not_found (insn);\n");
4975 if (write_case_lines)
4977 for (ie = av->first_insn; ie; ie = ie->next)
4978 if (ie->insn_code != -1)
4980 write_indent (indent);
4981 printf ("case %d:\n", ie->insn_code);
4984 else
4986 write_indent (indent);
4987 printf ("default:\n");
4990 /* See what we have to do to output this value. */
4991 must_extract = must_constrain = address_used = 0;
4992 walk_attr_value (av->value);
4994 if (must_constrain)
4996 write_indent (indent + 2);
4997 printf ("extract_constrain_insn_cached (insn);\n");
4999 else if (must_extract)
5001 write_indent (indent + 2);
5002 printf ("extract_insn_cached (insn);\n");
5005 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
5006 known_true, av->first_insn->insn_code,
5007 av->first_insn->insn_index);
5009 if (strncmp (prefix, "return", 6))
5011 write_indent (indent + 2);
5012 printf ("break;\n");
5014 printf ("\n");
5017 /* Search for uses of non-const attributes and write code to cache them. */
5019 static int
5020 write_expr_attr_cache (rtx p, struct attr_desc *attr)
5022 const char *fmt;
5023 int i, ie, j, je;
5025 if (GET_CODE (p) == EQ_ATTR)
5027 if (XSTR (p, 0) != attr->name)
5028 return 0;
5030 if (!attr->is_numeric)
5031 printf (" enum attr_%s ", attr->name);
5032 else if (attr->unsigned_p)
5033 printf (" unsigned int ");
5034 else
5035 printf (" int ");
5037 printf ("attr_%s = get_attr_%s (insn);\n", attr->name, attr->name);
5038 return 1;
5041 fmt = GET_RTX_FORMAT (GET_CODE (p));
5042 ie = GET_RTX_LENGTH (GET_CODE (p));
5043 for (i = 0; i < ie; i++)
5045 switch (*fmt++)
5047 case 'e':
5048 if (write_expr_attr_cache (XEXP (p, i), attr))
5049 return 1;
5050 break;
5052 case 'E':
5053 je = XVECLEN (p, i);
5054 for (j = 0; j < je; ++j)
5055 if (write_expr_attr_cache (XVECEXP (p, i, j), attr))
5056 return 1;
5057 break;
5061 return 0;
5064 /* Evaluate an expression at top level. A front end to write_test_expr,
5065 in which we cache attribute values and break up excessively large
5066 expressions to cater to older compilers. */
5068 static void
5069 write_toplevel_expr (rtx p)
5071 struct attr_desc *attr;
5072 int i;
5074 for (i = 0; i < MAX_ATTRS_INDEX; ++i)
5075 for (attr = attrs[i]; attr; attr = attr->next)
5076 if (!attr->is_const)
5077 write_expr_attr_cache (p, attr);
5079 printf (" unsigned long accum = 0;\n\n");
5081 while (GET_CODE (p) == IOR)
5083 rtx e;
5084 if (GET_CODE (XEXP (p, 0)) == IOR)
5085 e = XEXP (p, 1), p = XEXP (p, 0);
5086 else
5087 e = XEXP (p, 0), p = XEXP (p, 1);
5089 printf (" accum |= ");
5090 write_test_expr (e, 3);
5091 printf (";\n");
5093 printf (" accum |= ");
5094 write_test_expr (p, 3);
5095 printf (";\n");
5098 /* Utilities to write names in various forms. */
5100 static void
5101 write_unit_name (const char *prefix, int num, const char *suffix)
5103 struct function_unit *unit;
5105 for (unit = units; unit; unit = unit->next)
5106 if (unit->num == num)
5108 printf ("%s%s%s", prefix, unit->name, suffix);
5109 return;
5112 printf ("%s<unknown>%s", prefix, suffix);
5115 static void
5116 write_attr_valueq (struct attr_desc *attr, const char *s)
5118 if (attr->is_numeric)
5120 int num = atoi (s);
5122 printf ("%d", num);
5124 /* Make the blockage range values and function units used values easier
5125 to read. */
5126 if (attr->func_units_p)
5128 if (num == -1)
5129 printf (" /* units: none */");
5130 else if (num >= 0)
5131 write_unit_name (" /* units: ", num, " */");
5132 else
5134 int i;
5135 const char *sep = " /* units: ";
5136 for (i = 0, num = ~num; num; i++, num >>= 1)
5137 if (num & 1)
5139 write_unit_name (sep, i, (num == 1) ? " */" : "");
5140 sep = ", ";
5145 else if (attr->blockage_p)
5146 printf (" /* min %d, max %d */", num >> (HOST_BITS_PER_INT / 2),
5147 num & ((1 << (HOST_BITS_PER_INT / 2)) - 1));
5149 else if (num > 9 || num < 0)
5150 printf (" /* 0x%x */", num);
5152 else
5154 write_upcase (attr->name);
5155 printf ("_");
5156 write_upcase (s);
5160 static void
5161 write_attr_value (struct attr_desc *attr, rtx value)
5163 int op;
5165 switch (GET_CODE (value))
5167 case CONST_STRING:
5168 write_attr_valueq (attr, XSTR (value, 0));
5169 break;
5171 case CONST_INT:
5172 printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
5173 break;
5175 case SYMBOL_REF:
5176 fputs (XSTR (value, 0), stdout);
5177 break;
5179 case ATTR:
5181 struct attr_desc *attr2 = find_attr (XSTR (value, 0), 0);
5182 printf ("get_attr_%s (%s)", attr2->name,
5183 (attr2->is_const ? "" : "insn"));
5185 break;
5187 case PLUS:
5188 op = '+';
5189 goto do_operator;
5190 case MINUS:
5191 op = '-';
5192 goto do_operator;
5193 case MULT:
5194 op = '*';
5195 goto do_operator;
5196 case DIV:
5197 op = '/';
5198 goto do_operator;
5199 case MOD:
5200 op = '%';
5201 goto do_operator;
5203 do_operator:
5204 write_attr_value (attr, XEXP (value, 0));
5205 putchar (' ');
5206 putchar (op);
5207 putchar (' ');
5208 write_attr_value (attr, XEXP (value, 1));
5209 break;
5211 default:
5212 abort ();
5216 static void
5217 write_upcase (const char *str)
5219 while (*str)
5221 /* The argument of TOUPPER should not have side effects. */
5222 putchar (TOUPPER(*str));
5223 str++;
5227 static void
5228 write_indent (int indent)
5230 for (; indent > 8; indent -= 8)
5231 printf ("\t");
5233 for (; indent; indent--)
5234 printf (" ");
5237 /* Write a subroutine that is given an insn that requires a delay slot, a
5238 delay slot ordinal, and a candidate insn. It returns nonzero if the
5239 candidate can be placed in the specified delay slot of the insn.
5241 We can write as many as three subroutines. `eligible_for_delay'
5242 handles normal delay slots, `eligible_for_annul_true' indicates that
5243 the specified insn can be annulled if the branch is true, and likewise
5244 for `eligible_for_annul_false'.
5246 KIND is a string distinguishing these three cases ("delay", "annul_true",
5247 or "annul_false"). */
5249 static void
5250 write_eligible_delay (const char *kind)
5252 struct delay_desc *delay;
5253 int max_slots;
5254 char str[50];
5255 struct attr_desc *attr;
5256 struct attr_value *av, *common_av;
5257 int i;
5259 /* Compute the maximum number of delay slots required. We use the delay
5260 ordinal times this number plus one, plus the slot number as an index into
5261 the appropriate predicate to test. */
5263 for (delay = delays, max_slots = 0; delay; delay = delay->next)
5264 if (XVECLEN (delay->def, 1) / 3 > max_slots)
5265 max_slots = XVECLEN (delay->def, 1) / 3;
5267 /* Write function prelude. */
5269 printf ("int\n");
5270 printf ("eligible_for_%s (rtx delay_insn ATTRIBUTE_UNUSED, int slot, rtx candidate_insn, int flags ATTRIBUTE_UNUSED)\n",
5271 kind);
5272 printf ("{\n");
5273 printf (" rtx insn;\n");
5274 printf ("\n");
5275 printf (" if (slot >= %d)\n", max_slots);
5276 printf (" abort ();\n");
5277 printf ("\n");
5279 /* If more than one delay type, find out which type the delay insn is. */
5281 if (num_delays > 1)
5283 attr = find_attr ("*delay_type", 0);
5284 if (! attr)
5285 abort ();
5286 common_av = find_most_used (attr);
5288 printf (" insn = delay_insn;\n");
5289 printf (" switch (recog_memoized (insn))\n");
5290 printf (" {\n");
5292 sprintf (str, " * %d;\n break;", max_slots);
5293 for (av = attr->first_value; av; av = av->next)
5294 if (av != common_av)
5295 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
5297 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
5298 printf (" }\n\n");
5300 /* Ensure matched. Otherwise, shouldn't have been called. */
5301 printf (" if (slot < %d)\n", max_slots);
5302 printf (" abort ();\n\n");
5305 /* If just one type of delay slot, write simple switch. */
5306 if (num_delays == 1 && max_slots == 1)
5308 printf (" insn = candidate_insn;\n");
5309 printf (" switch (recog_memoized (insn))\n");
5310 printf (" {\n");
5312 attr = find_attr ("*delay_1_0", 0);
5313 if (! attr)
5314 abort ();
5315 common_av = find_most_used (attr);
5317 for (av = attr->first_value; av; av = av->next)
5318 if (av != common_av)
5319 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
5321 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
5322 printf (" }\n");
5325 else
5327 /* Write a nested CASE. The first indicates which condition we need to
5328 test, and the inner CASE tests the condition. */
5329 printf (" insn = candidate_insn;\n");
5330 printf (" switch (slot)\n");
5331 printf (" {\n");
5333 for (delay = delays; delay; delay = delay->next)
5334 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
5336 printf (" case %d:\n",
5337 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
5338 printf (" switch (recog_memoized (insn))\n");
5339 printf ("\t{\n");
5341 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
5342 attr = find_attr (str, 0);
5343 if (! attr)
5344 abort ();
5345 common_av = find_most_used (attr);
5347 for (av = attr->first_value; av; av = av->next)
5348 if (av != common_av)
5349 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
5351 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
5352 printf (" }\n");
5355 printf (" default:\n");
5356 printf (" abort ();\n");
5357 printf (" }\n");
5360 printf ("}\n\n");
5363 /* Write routines to compute conflict cost for function units. Then write a
5364 table describing the available function units. */
5366 static void
5367 write_function_unit_info (void)
5369 struct function_unit *unit;
5370 int i;
5372 /* Write out conflict routines for function units. Don't bother writing
5373 one if there is only one issue delay value. */
5375 for (unit = units; unit; unit = unit->next)
5377 if (unit->needs_blockage_function)
5378 write_complex_function (unit, "blockage", "block");
5380 /* If the minimum and maximum conflict costs are the same, there
5381 is only one value, so we don't need a function. */
5382 if (! unit->needs_conflict_function)
5384 unit->default_cost = make_numeric_value (unit->issue_delay.max);
5385 continue;
5388 /* The function first computes the case from the candidate insn. */
5389 unit->default_cost = make_numeric_value (0);
5390 write_complex_function (unit, "conflict_cost", "cost");
5393 /* Now that all functions have been written, write the table describing
5394 the function units. The name is included for documentation purposes
5395 only. */
5397 printf ("const struct function_unit_desc function_units[] = {\n");
5399 /* Write out the descriptions in numeric order, but don't force that order
5400 on the list. Doing so increases the runtime of genattrtab.c. */
5401 for (i = 0; i < num_units; i++)
5403 for (unit = units; unit; unit = unit->next)
5404 if (unit->num == i)
5405 break;
5407 printf (" {\"%s\", %d, %d, %d, %s, %d, %s_unit_ready_cost, ",
5408 unit->name, 1 << unit->num, unit->multiplicity,
5409 unit->simultaneity, XSTR (unit->default_cost, 0),
5410 unit->issue_delay.max, unit->name);
5412 if (unit->needs_conflict_function)
5413 printf ("%s_unit_conflict_cost, ", unit->name);
5414 else
5415 printf ("0, ");
5417 printf ("%d, ", unit->max_blockage);
5419 if (unit->needs_range_function)
5420 printf ("%s_unit_blockage_range, ", unit->name);
5421 else
5422 printf ("0, ");
5424 if (unit->needs_blockage_function)
5425 printf ("%s_unit_blockage", unit->name);
5426 else
5427 printf ("0");
5429 printf ("}, \n");
5432 if (num_units == 0)
5433 printf ("{\"dummy\", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* a dummy element */");
5434 printf ("};\n\n");
5437 static void
5438 write_complex_function (struct function_unit *unit,
5439 const char *name,
5440 const char *connection)
5442 struct attr_desc *case_attr, *attr;
5443 struct attr_value *av, *common_av;
5444 rtx value;
5445 char str[256];
5446 int using_case;
5447 int i;
5449 printf ("static int\n");
5450 printf ("%s_unit_%s (rtx executing_insn, rtx candidate_insn)\n",
5451 unit->name, name);
5452 printf ("{\n");
5453 printf (" rtx insn;\n");
5454 printf (" int casenum;\n\n");
5455 printf (" insn = executing_insn;\n");
5456 printf (" switch (recog_memoized (insn))\n");
5457 printf (" {\n");
5459 /* Write the `switch' statement to get the case value. */
5460 if (strlen (unit->name) + sizeof "*_cases" > 256)
5461 abort ();
5462 sprintf (str, "*%s_cases", unit->name);
5463 case_attr = find_attr (str, 0);
5464 if (! case_attr)
5465 abort ();
5466 common_av = find_most_used (case_attr);
5468 for (av = case_attr->first_value; av; av = av->next)
5469 if (av != common_av)
5470 write_attr_case (case_attr, av, 1,
5471 "casenum =", ";", 4, unit->condexp);
5473 write_attr_case (case_attr, common_av, 0,
5474 "casenum =", ";", 4, unit->condexp);
5475 printf (" }\n\n");
5477 /* Now write an outer switch statement on each case. Then write
5478 the tests on the executing function within each. */
5479 printf (" insn = candidate_insn;\n");
5480 printf (" switch (casenum)\n");
5481 printf (" {\n");
5483 for (i = 0; i < unit->num_opclasses; i++)
5485 /* Ensure using this case. */
5486 using_case = 0;
5487 for (av = case_attr->first_value; av; av = av->next)
5488 if (av->num_insns
5489 && contained_in_p (make_numeric_value (i), av->value))
5490 using_case = 1;
5492 if (! using_case)
5493 continue;
5495 printf (" case %d:\n", i);
5496 sprintf (str, "*%s_%s_%d", unit->name, connection, i);
5497 attr = find_attr (str, 0);
5498 if (! attr)
5499 abort ();
5501 /* If single value, just write it. */
5502 value = find_single_value (attr);
5503 if (value)
5504 write_attr_set (attr, 6, value, "return", ";\n", true_rtx, -2, -2);
5505 else
5507 common_av = find_most_used (attr);
5508 printf (" switch (recog_memoized (insn))\n");
5509 printf ("\t{\n");
5511 for (av = attr->first_value; av; av = av->next)
5512 if (av != common_av)
5513 write_attr_case (attr, av, 1,
5514 "return", ";", 8, unit->condexp);
5516 write_attr_case (attr, common_av, 0,
5517 "return", ";", 8, unit->condexp);
5518 printf (" }\n\n");
5522 /* This default case should not be needed, but gcc's analysis is not
5523 good enough to realize that the default case is not needed for the
5524 second switch statement. */
5525 printf (" default:\n abort ();\n");
5526 printf (" }\n}\n\n");
5529 /* This page contains miscellaneous utility routines. */
5531 /* Given a pointer to a (char *), return a malloc'ed string containing the
5532 next comma-separated element. Advance the pointer to after the string
5533 scanned, or the end-of-string. Return NULL if at end of string. */
5535 static char *
5536 next_comma_elt (const char **pstr)
5538 const char *start;
5540 start = scan_comma_elt (pstr);
5542 if (start == NULL)
5543 return NULL;
5545 return attr_string (start, *pstr - start);
5548 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
5549 is nonzero, build a new attribute, if one does not exist. */
5551 static struct attr_desc *
5552 find_attr (const char *name, int create)
5554 struct attr_desc *attr;
5555 int index;
5557 /* Before we resort to using `strcmp', see if the string address matches
5558 anywhere. In most cases, it should have been canonicalized to do so. */
5559 if (name == alternative_name)
5560 return NULL;
5562 index = name[0] & (MAX_ATTRS_INDEX - 1);
5563 for (attr = attrs[index]; attr; attr = attr->next)
5564 if (name == attr->name)
5565 return attr;
5567 /* Otherwise, do it the slow way. */
5568 for (attr = attrs[index]; attr; attr = attr->next)
5569 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
5570 return attr;
5572 if (! create)
5573 return NULL;
5575 attr = oballoc (sizeof (struct attr_desc));
5576 attr->name = attr_string (name, strlen (name));
5577 attr->first_value = attr->default_val = NULL;
5578 attr->is_numeric = attr->negative_ok = attr->is_const = attr->is_special = 0;
5579 attr->unsigned_p = attr->func_units_p = attr->blockage_p = attr->static_p = 0;
5580 attr->next = attrs[index];
5581 attrs[index] = attr;
5583 return attr;
5586 /* Create internal attribute with the given default value. */
5588 void
5589 make_internal_attr (const char *name, rtx value, int special)
5591 struct attr_desc *attr;
5593 attr = find_attr (name, 1);
5594 if (attr->default_val)
5595 abort ();
5597 attr->is_numeric = 1;
5598 attr->is_const = 0;
5599 attr->is_special = (special & ATTR_SPECIAL) != 0;
5600 attr->negative_ok = (special & ATTR_NEGATIVE_OK) != 0;
5601 attr->unsigned_p = (special & ATTR_UNSIGNED) != 0;
5602 attr->func_units_p = (special & ATTR_FUNC_UNITS) != 0;
5603 attr->blockage_p = (special & ATTR_BLOCKAGE) != 0;
5604 attr->static_p = (special & ATTR_STATIC) != 0;
5605 attr->default_val = get_attr_value (value, attr, -2);
5608 /* Find the most used value of an attribute. */
5610 static struct attr_value *
5611 find_most_used (struct attr_desc *attr)
5613 struct attr_value *av;
5614 struct attr_value *most_used;
5615 int nuses;
5617 most_used = NULL;
5618 nuses = -1;
5620 for (av = attr->first_value; av; av = av->next)
5621 if (av->num_insns > nuses)
5622 nuses = av->num_insns, most_used = av;
5624 return most_used;
5627 /* If an attribute only has a single value used, return it. Otherwise
5628 return NULL. */
5630 static rtx
5631 find_single_value (struct attr_desc *attr)
5633 struct attr_value *av;
5634 rtx unique_value;
5636 unique_value = NULL;
5637 for (av = attr->first_value; av; av = av->next)
5638 if (av->num_insns)
5640 if (unique_value)
5641 return NULL;
5642 else
5643 unique_value = av->value;
5646 return unique_value;
5649 /* Return (attr_value "n") */
5652 make_numeric_value (int n)
5654 static rtx int_values[20];
5655 rtx exp;
5656 char *p;
5658 if (n < 0)
5659 abort ();
5661 if (n < 20 && int_values[n])
5662 return int_values[n];
5664 p = attr_printf (MAX_DIGITS, "%d", n);
5665 exp = attr_rtx (CONST_STRING, p);
5667 if (n < 20)
5668 int_values[n] = exp;
5670 return exp;
5673 static void
5674 extend_range (struct range *range, int min, int max)
5676 if (range->min > min)
5677 range->min = min;
5678 if (range->max < max)
5679 range->max = max;
5682 static rtx
5683 copy_rtx_unchanging (rtx orig)
5685 if (ATTR_IND_SIMPLIFIED_P (orig) || ATTR_CURR_SIMPLIFIED_P (orig))
5686 return orig;
5688 ATTR_CURR_SIMPLIFIED_P (orig) = 1;
5689 return orig;
5692 /* Determine if an insn has a constant number of delay slots, i.e., the
5693 number of delay slots is not a function of the length of the insn. */
5695 static void
5696 write_const_num_delay_slots (void)
5698 struct attr_desc *attr = find_attr ("*num_delay_slots", 0);
5699 struct attr_value *av;
5700 struct insn_ent *ie;
5702 if (attr)
5704 printf ("int\nconst_num_delay_slots (rtx insn)\n");
5705 printf ("{\n");
5706 printf (" switch (recog_memoized (insn))\n");
5707 printf (" {\n");
5709 for (av = attr->first_value; av; av = av->next)
5711 length_used = 0;
5712 walk_attr_value (av->value);
5713 if (length_used)
5715 for (ie = av->first_insn; ie; ie = ie->next)
5716 if (ie->insn_code != -1)
5717 printf (" case %d:\n", ie->insn_code);
5718 printf (" return 0;\n");
5722 printf (" default:\n");
5723 printf (" return 1;\n");
5724 printf (" }\n}\n\n");
5729 main (int argc, char **argv)
5731 rtx desc;
5732 struct attr_desc *attr;
5733 struct insn_def *id;
5734 rtx tem;
5735 int i;
5737 progname = "genattrtab";
5739 if (argc <= 1)
5740 fatal ("no input file name");
5742 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
5743 return (FATAL_EXIT_CODE);
5745 obstack_init (hash_obstack);
5746 obstack_init (temp_obstack);
5748 /* Set up true and false rtx's */
5749 true_rtx = rtx_alloc (CONST_INT);
5750 XWINT (true_rtx, 0) = 1;
5751 false_rtx = rtx_alloc (CONST_INT);
5752 XWINT (false_rtx, 0) = 0;
5753 ATTR_IND_SIMPLIFIED_P (true_rtx) = ATTR_IND_SIMPLIFIED_P (false_rtx) = 1;
5754 ATTR_PERMANENT_P (true_rtx) = ATTR_PERMANENT_P (false_rtx) = 1;
5756 alternative_name = attr_string ("alternative", strlen ("alternative"));
5758 printf ("/* Generated automatically by the program `genattrtab'\n\
5759 from the machine description file `md'. */\n\n");
5761 /* Read the machine description. */
5763 initiate_automaton_gen (argc, argv);
5764 while (1)
5766 int lineno;
5768 desc = read_md_rtx (&lineno, &insn_code_number);
5769 if (desc == NULL)
5770 break;
5772 switch (GET_CODE (desc))
5774 case DEFINE_INSN:
5775 case DEFINE_PEEPHOLE:
5776 case DEFINE_ASM_ATTRIBUTES:
5777 gen_insn (desc, lineno);
5778 break;
5780 case DEFINE_ATTR:
5781 gen_attr (desc, lineno);
5782 break;
5784 case DEFINE_DELAY:
5785 gen_delay (desc, lineno);
5786 break;
5788 case DEFINE_FUNCTION_UNIT:
5789 gen_unit (desc, lineno);
5790 break;
5792 case DEFINE_CPU_UNIT:
5793 gen_cpu_unit (desc);
5794 break;
5796 case DEFINE_QUERY_CPU_UNIT:
5797 gen_query_cpu_unit (desc);
5798 break;
5800 case DEFINE_BYPASS:
5801 gen_bypass (desc);
5802 break;
5804 case EXCLUSION_SET:
5805 gen_excl_set (desc);
5806 break;
5808 case PRESENCE_SET:
5809 gen_presence_set (desc);
5810 break;
5812 case FINAL_PRESENCE_SET:
5813 gen_final_presence_set (desc);
5814 break;
5816 case ABSENCE_SET:
5817 gen_absence_set (desc);
5818 break;
5820 case FINAL_ABSENCE_SET:
5821 gen_final_absence_set (desc);
5822 break;
5824 case DEFINE_AUTOMATON:
5825 gen_automaton (desc);
5826 break;
5828 case AUTOMATA_OPTION:
5829 gen_automata_option (desc);
5830 break;
5832 case DEFINE_RESERVATION:
5833 gen_reserv (desc);
5834 break;
5836 case DEFINE_INSN_RESERVATION:
5837 gen_insn_reserv (desc);
5838 break;
5840 default:
5841 break;
5843 if (GET_CODE (desc) != DEFINE_ASM_ATTRIBUTES)
5844 insn_index_number++;
5847 if (have_error)
5848 return FATAL_EXIT_CODE;
5850 insn_code_number++;
5852 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
5853 if (! got_define_asm_attributes)
5855 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
5856 XVEC (tem, 0) = rtvec_alloc (0);
5857 gen_insn (tem, 0);
5860 /* Expand DEFINE_DELAY information into new attribute. */
5861 if (num_delays)
5862 expand_delays ();
5864 if (num_units || num_dfa_decls)
5866 /* Expand DEFINE_FUNCTION_UNIT information into new attributes. */
5867 expand_units ();
5868 /* Build DFA, output some functions and expand DFA information
5869 into new attributes. */
5870 expand_automata ();
5873 printf ("#include \"config.h\"\n");
5874 printf ("#include \"system.h\"\n");
5875 printf ("#include \"coretypes.h\"\n");
5876 printf ("#include \"tm.h\"\n");
5877 printf ("#include \"rtl.h\"\n");
5878 printf ("#include \"tm_p.h\"\n");
5879 printf ("#include \"insn-config.h\"\n");
5880 printf ("#include \"recog.h\"\n");
5881 printf ("#include \"regs.h\"\n");
5882 printf ("#include \"real.h\"\n");
5883 printf ("#include \"output.h\"\n");
5884 printf ("#include \"insn-attr.h\"\n");
5885 printf ("#include \"toplev.h\"\n");
5886 printf ("#include \"flags.h\"\n");
5887 printf ("#include \"function.h\"\n");
5888 printf ("\n");
5889 printf ("#define operands recog_data.operand\n\n");
5891 /* Make `insn_alternatives'. */
5892 insn_alternatives = oballoc (insn_code_number * sizeof (int));
5893 for (id = defs; id; id = id->next)
5894 if (id->insn_code >= 0)
5895 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
5897 /* Make `insn_n_alternatives'. */
5898 insn_n_alternatives = oballoc (insn_code_number * sizeof (int));
5899 for (id = defs; id; id = id->next)
5900 if (id->insn_code >= 0)
5901 insn_n_alternatives[id->insn_code] = id->num_alternatives;
5903 /* Prepare to write out attribute subroutines by checking everything stored
5904 away and building the attribute cases. */
5906 check_defs ();
5908 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5909 for (attr = attrs[i]; attr; attr = attr->next)
5910 attr->default_val->value
5911 = check_attr_value (attr->default_val->value, attr);
5913 if (have_error)
5914 return FATAL_EXIT_CODE;
5916 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5917 for (attr = attrs[i]; attr; attr = attr->next)
5918 fill_attr (attr);
5920 /* Construct extra attributes for `length'. */
5921 make_length_attrs ();
5923 /* Perform any possible optimizations to speed up compilation. */
5924 optimize_attrs ();
5926 /* Now write out all the `gen_attr_...' routines. Do these before the
5927 special routines (specifically before write_function_unit_info), so
5928 that they get defined before they are used. */
5930 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5931 for (attr = attrs[i]; attr; attr = attr->next)
5933 if (! attr->is_special && ! attr->is_const)
5935 int insn_alts_p;
5937 insn_alts_p
5938 = (attr->name [0] == '*'
5939 && strcmp (&attr->name [1], INSN_ALTS_FUNC_NAME) == 0);
5940 if (insn_alts_p)
5941 printf ("\n#if AUTOMATON_ALTS\n");
5942 write_attr_get (attr);
5943 if (insn_alts_p)
5944 printf ("#endif\n\n");
5948 /* Write out delay eligibility information, if DEFINE_DELAY present.
5949 (The function to compute the number of delay slots will be written
5950 below.) */
5951 if (num_delays)
5953 write_eligible_delay ("delay");
5954 if (have_annul_true)
5955 write_eligible_delay ("annul_true");
5956 if (have_annul_false)
5957 write_eligible_delay ("annul_false");
5960 if (num_units || num_dfa_decls)
5962 /* Write out information about function units. */
5963 write_function_unit_info ();
5964 /* Output code for pipeline hazards recognition based on DFA
5965 (deterministic finite state automata. */
5966 write_automata ();
5969 /* Write out constant delay slot info. */
5970 write_const_num_delay_slots ();
5972 write_length_unit_log ();
5974 fflush (stdout);
5975 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
5978 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
5979 const char *
5980 get_insn_name (int code ATTRIBUTE_UNUSED)
5982 return NULL;