PR c++/11509
[official-gcc.git] / gcc / genattrtab.c
blob15a8afabc227dca99faa47611e35d65da5f49f2e
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 = (struct attr_hash *) obstack_alloc (hash_obstack,
504 sizeof (struct attr_hash));
505 h->hashcode = hashcode;
506 h->u.rtl = rtl;
507 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
508 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
511 /* Add an entry to the hash table for STRING with hash code HASHCODE. */
513 static void
514 attr_hash_add_string (int hashcode, char *str)
516 struct attr_hash *h;
518 h = (struct attr_hash *) obstack_alloc (hash_obstack,
519 sizeof (struct attr_hash));
520 h->hashcode = -hashcode;
521 h->u.str = str;
522 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
523 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
526 /* Generate an RTL expression, but avoid duplicates.
527 Set the ATTR_PERMANENT_P flag for these permanent objects.
529 In some cases we cannot uniquify; then we return an ordinary
530 impermanent rtx with ATTR_PERMANENT_P clear.
532 Args are like gen_rtx, but without the mode:
534 rtx attr_rtx (code, [element1, ..., elementn]) */
536 static rtx
537 attr_rtx_1 (enum rtx_code code, va_list p)
539 rtx rt_val = NULL_RTX;/* RTX to return to caller... */
540 int hashcode;
541 struct attr_hash *h;
542 struct obstack *old_obstack = rtl_obstack;
544 /* For each of several cases, search the hash table for an existing entry.
545 Use that entry if one is found; otherwise create a new RTL and add it
546 to the table. */
548 if (GET_RTX_CLASS (code) == '1')
550 rtx arg0 = va_arg (p, rtx);
552 /* A permanent object cannot point to impermanent ones. */
553 if (! ATTR_PERMANENT_P (arg0))
555 rt_val = rtx_alloc (code);
556 XEXP (rt_val, 0) = arg0;
557 return rt_val;
560 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
561 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
562 if (h->hashcode == hashcode
563 && GET_CODE (h->u.rtl) == code
564 && XEXP (h->u.rtl, 0) == arg0)
565 return h->u.rtl;
567 if (h == 0)
569 rtl_obstack = hash_obstack;
570 rt_val = rtx_alloc (code);
571 XEXP (rt_val, 0) = arg0;
574 else if (GET_RTX_CLASS (code) == 'c'
575 || GET_RTX_CLASS (code) == '2'
576 || GET_RTX_CLASS (code) == '<')
578 rtx arg0 = va_arg (p, rtx);
579 rtx arg1 = va_arg (p, rtx);
581 /* A permanent object cannot point to impermanent ones. */
582 if (! ATTR_PERMANENT_P (arg0) || ! ATTR_PERMANENT_P (arg1))
584 rt_val = rtx_alloc (code);
585 XEXP (rt_val, 0) = arg0;
586 XEXP (rt_val, 1) = arg1;
587 return rt_val;
590 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
591 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
592 if (h->hashcode == hashcode
593 && GET_CODE (h->u.rtl) == code
594 && XEXP (h->u.rtl, 0) == arg0
595 && XEXP (h->u.rtl, 1) == arg1)
596 return h->u.rtl;
598 if (h == 0)
600 rtl_obstack = hash_obstack;
601 rt_val = rtx_alloc (code);
602 XEXP (rt_val, 0) = arg0;
603 XEXP (rt_val, 1) = arg1;
606 else if (GET_RTX_LENGTH (code) == 1
607 && GET_RTX_FORMAT (code)[0] == 's')
609 char *arg0 = va_arg (p, char *);
611 if (code == SYMBOL_REF)
612 arg0 = attr_string (arg0, strlen (arg0));
614 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
615 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
616 if (h->hashcode == hashcode
617 && GET_CODE (h->u.rtl) == code
618 && XSTR (h->u.rtl, 0) == arg0)
619 return h->u.rtl;
621 if (h == 0)
623 rtl_obstack = hash_obstack;
624 rt_val = rtx_alloc (code);
625 XSTR (rt_val, 0) = arg0;
628 else if (GET_RTX_LENGTH (code) == 2
629 && GET_RTX_FORMAT (code)[0] == 's'
630 && GET_RTX_FORMAT (code)[1] == 's')
632 char *arg0 = va_arg (p, char *);
633 char *arg1 = va_arg (p, char *);
635 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
636 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
637 if (h->hashcode == hashcode
638 && GET_CODE (h->u.rtl) == code
639 && XSTR (h->u.rtl, 0) == arg0
640 && XSTR (h->u.rtl, 1) == arg1)
641 return h->u.rtl;
643 if (h == 0)
645 rtl_obstack = hash_obstack;
646 rt_val = rtx_alloc (code);
647 XSTR (rt_val, 0) = arg0;
648 XSTR (rt_val, 1) = arg1;
651 else if (code == CONST_INT)
653 HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
654 if (arg0 == 0)
655 return false_rtx;
656 else if (arg0 == 1)
657 return true_rtx;
658 else
659 goto nohash;
661 else
663 int i; /* Array indices... */
664 const char *fmt; /* Current rtx's format... */
665 nohash:
666 rt_val = rtx_alloc (code); /* Allocate the storage space. */
668 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
669 for (i = 0; i < GET_RTX_LENGTH (code); i++)
671 switch (*fmt++)
673 case '0': /* Unused field. */
674 break;
676 case 'i': /* An integer? */
677 XINT (rt_val, i) = va_arg (p, int);
678 break;
680 case 'w': /* A wide integer? */
681 XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
682 break;
684 case 's': /* A string? */
685 XSTR (rt_val, i) = va_arg (p, char *);
686 break;
688 case 'e': /* An expression? */
689 case 'u': /* An insn? Same except when printing. */
690 XEXP (rt_val, i) = va_arg (p, rtx);
691 break;
693 case 'E': /* An RTX vector? */
694 XVEC (rt_val, i) = va_arg (p, rtvec);
695 break;
697 default:
698 abort ();
701 return rt_val;
704 rtl_obstack = old_obstack;
705 attr_hash_add_rtx (hashcode, rt_val);
706 ATTR_PERMANENT_P (rt_val) = 1;
707 return rt_val;
710 static rtx
711 attr_rtx (enum rtx_code code, ...)
713 rtx result;
714 va_list p;
716 va_start (p, code);
717 result = attr_rtx_1 (code, p);
718 va_end (p);
719 return result;
722 /* Create a new string printed with the printf line arguments into a space
723 of at most LEN bytes:
725 rtx attr_printf (len, format, [arg1, ..., argn]) */
727 char *
728 attr_printf (unsigned int len, const char *fmt, ...)
730 char str[256];
731 va_list p;
733 va_start (p, fmt);
735 if (len > sizeof str - 1) /* Leave room for \0. */
736 abort ();
738 vsprintf (str, fmt, p);
739 va_end (p);
741 return attr_string (str, strlen (str));
744 static rtx
745 attr_eq (const char *name, const char *value)
747 return attr_rtx (EQ_ATTR, attr_string (name, strlen (name)),
748 attr_string (value, strlen (value)));
751 static const char *
752 attr_numeral (int n)
754 return XSTR (make_numeric_value (n), 0);
757 /* Return a permanent (possibly shared) copy of a string STR (not assumed
758 to be null terminated) with LEN bytes. */
760 static char *
761 attr_string (const char *str, int len)
763 struct attr_hash *h;
764 int hashcode;
765 int i;
766 char *new_str;
768 /* Compute the hash code. */
769 hashcode = (len + 1) * 613 + (unsigned) str[0];
770 for (i = 1; i <= len; i += 2)
771 hashcode = ((hashcode * 613) + (unsigned) str[i]);
772 if (hashcode < 0)
773 hashcode = -hashcode;
775 /* Search the table for the string. */
776 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
777 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
778 && !strncmp (h->u.str, str, len))
779 return h->u.str; /* <-- return if found. */
781 /* Not found; create a permanent copy and add it to the hash table. */
782 new_str = (char *) obstack_alloc (hash_obstack, len + 1);
783 memcpy (new_str, str, len);
784 new_str[len] = '\0';
785 attr_hash_add_string (hashcode, new_str);
787 return new_str; /* Return the new string. */
790 /* Check two rtx's for equality of contents,
791 taking advantage of the fact that if both are hashed
792 then they can't be equal unless they are the same object. */
794 static int
795 attr_equal_p (rtx x, rtx y)
797 return (x == y || (! (ATTR_PERMANENT_P (x) && ATTR_PERMANENT_P (y))
798 && rtx_equal_p (x, y)));
801 /* Copy an attribute value expression,
802 descending to all depths, but not copying any
803 permanent hashed subexpressions. */
805 static rtx
806 attr_copy_rtx (rtx orig)
808 rtx copy;
809 int i, j;
810 RTX_CODE code;
811 const char *format_ptr;
813 /* No need to copy a permanent object. */
814 if (ATTR_PERMANENT_P (orig))
815 return orig;
817 code = GET_CODE (orig);
819 switch (code)
821 case REG:
822 case QUEUED:
823 case CONST_INT:
824 case CONST_DOUBLE:
825 case CONST_VECTOR:
826 case SYMBOL_REF:
827 case CODE_LABEL:
828 case PC:
829 case CC0:
830 return orig;
832 default:
833 break;
836 copy = rtx_alloc (code);
837 PUT_MODE (copy, GET_MODE (orig));
838 ATTR_IND_SIMPLIFIED_P (copy) = ATTR_IND_SIMPLIFIED_P (orig);
839 ATTR_CURR_SIMPLIFIED_P (copy) = ATTR_CURR_SIMPLIFIED_P (orig);
840 ATTR_PERMANENT_P (copy) = ATTR_PERMANENT_P (orig);
841 ATTR_EQ_ATTR_P (copy) = ATTR_EQ_ATTR_P (orig);
843 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
845 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
847 switch (*format_ptr++)
849 case 'e':
850 XEXP (copy, i) = XEXP (orig, i);
851 if (XEXP (orig, i) != NULL)
852 XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
853 break;
855 case 'E':
856 case 'V':
857 XVEC (copy, i) = XVEC (orig, i);
858 if (XVEC (orig, i) != NULL)
860 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
861 for (j = 0; j < XVECLEN (copy, i); j++)
862 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
864 break;
866 case 'n':
867 case 'i':
868 XINT (copy, i) = XINT (orig, i);
869 break;
871 case 'w':
872 XWINT (copy, i) = XWINT (orig, i);
873 break;
875 case 's':
876 case 'S':
877 XSTR (copy, i) = XSTR (orig, i);
878 break;
880 default:
881 abort ();
884 return copy;
887 /* Given a test expression for an attribute, ensure it is validly formed.
888 IS_CONST indicates whether the expression is constant for each compiler
889 run (a constant expression may not test any particular insn).
891 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
892 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
893 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
895 Update the string address in EQ_ATTR expression to be the same used
896 in the attribute (or `alternative_name') to speed up subsequent
897 `find_attr' calls and eliminate most `strcmp' calls.
899 Return the new expression, if any. */
902 check_attr_test (rtx exp, int is_const, int lineno)
904 struct attr_desc *attr;
905 struct attr_value *av;
906 const char *name_ptr, *p;
907 rtx orexp, newexp;
909 switch (GET_CODE (exp))
911 case EQ_ATTR:
912 /* Handle negation test. */
913 if (XSTR (exp, 1)[0] == '!')
914 return check_attr_test (attr_rtx (NOT,
915 attr_eq (XSTR (exp, 0),
916 &XSTR (exp, 1)[1])),
917 is_const, lineno);
919 else if (n_comma_elts (XSTR (exp, 1)) == 1)
921 attr = find_attr (XSTR (exp, 0), 0);
922 if (attr == NULL)
924 if (! strcmp (XSTR (exp, 0), "alternative"))
926 XSTR (exp, 0) = alternative_name;
927 /* This can't be simplified any further. */
928 ATTR_IND_SIMPLIFIED_P (exp) = 1;
929 return exp;
931 else
932 fatal ("unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
935 if (is_const && ! attr->is_const)
936 fatal ("constant expression uses insn attribute `%s' in EQ_ATTR",
937 XSTR (exp, 0));
939 /* Copy this just to make it permanent,
940 so expressions using it can be permanent too. */
941 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
943 /* It shouldn't be possible to simplify the value given to a
944 constant attribute, so don't expand this until it's time to
945 write the test expression. */
946 if (attr->is_const)
947 ATTR_IND_SIMPLIFIED_P (exp) = 1;
949 if (attr->is_numeric)
951 for (p = XSTR (exp, 1); *p; p++)
952 if (! ISDIGIT (*p))
953 fatal ("attribute `%s' takes only numeric values",
954 XSTR (exp, 0));
956 else
958 for (av = attr->first_value; av; av = av->next)
959 if (GET_CODE (av->value) == CONST_STRING
960 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
961 break;
963 if (av == NULL)
964 fatal ("unknown value `%s' for `%s' attribute",
965 XSTR (exp, 1), XSTR (exp, 0));
968 else
970 /* Make an IOR tree of the possible values. */
971 orexp = false_rtx;
972 name_ptr = XSTR (exp, 1);
973 while ((p = next_comma_elt (&name_ptr)) != NULL)
975 newexp = attr_eq (XSTR (exp, 0), p);
976 orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
979 return check_attr_test (orexp, is_const, lineno);
981 break;
983 case ATTR_FLAG:
984 break;
986 case CONST_INT:
987 /* Either TRUE or FALSE. */
988 if (XWINT (exp, 0))
989 return true_rtx;
990 else
991 return false_rtx;
993 case IOR:
994 case AND:
995 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
996 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const, lineno);
997 break;
999 case NOT:
1000 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
1001 break;
1003 case MATCH_INSN:
1004 case MATCH_OPERAND:
1005 if (is_const)
1006 fatal ("RTL operator \"%s\" not valid in constant attribute test",
1007 GET_RTX_NAME (GET_CODE (exp)));
1008 /* These cases can't be simplified. */
1009 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1010 break;
1012 case LE: case LT: case GT: case GE:
1013 case LEU: case LTU: case GTU: case GEU:
1014 case NE: case EQ:
1015 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
1016 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
1017 exp = attr_rtx (GET_CODE (exp),
1018 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
1019 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
1020 /* These cases can't be simplified. */
1021 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1022 break;
1024 case SYMBOL_REF:
1025 if (is_const)
1027 /* These cases are valid for constant attributes, but can't be
1028 simplified. */
1029 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1030 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1031 break;
1033 default:
1034 fatal ("RTL operator \"%s\" not valid in attribute test",
1035 GET_RTX_NAME (GET_CODE (exp)));
1038 return exp;
1041 /* Given an expression, ensure that it is validly formed and that all named
1042 attribute values are valid for the given attribute. Issue a fatal error
1043 if not. If no attribute is specified, assume a numeric attribute.
1045 Return a perhaps modified replacement expression for the value. */
1047 static rtx
1048 check_attr_value (rtx exp, struct attr_desc *attr)
1050 struct attr_value *av;
1051 const char *p;
1052 int i;
1054 switch (GET_CODE (exp))
1056 case CONST_INT:
1057 if (attr && ! attr->is_numeric)
1059 message_with_line (attr->lineno,
1060 "CONST_INT not valid for non-numeric attribute %s",
1061 attr->name);
1062 have_error = 1;
1063 break;
1066 if (INTVAL (exp) < 0 && ! attr->negative_ok)
1068 message_with_line (attr->lineno,
1069 "negative numeric value specified for attribute %s",
1070 attr->name);
1071 have_error = 1;
1072 break;
1074 break;
1076 case CONST_STRING:
1077 if (! strcmp (XSTR (exp, 0), "*"))
1078 break;
1080 if (attr == 0 || attr->is_numeric)
1082 p = XSTR (exp, 0);
1083 if (attr && attr->negative_ok && *p == '-')
1084 p++;
1085 for (; *p; p++)
1086 if (! ISDIGIT (*p))
1088 message_with_line (attr ? attr->lineno : 0,
1089 "non-numeric value for numeric attribute %s",
1090 attr ? attr->name : "internal");
1091 have_error = 1;
1092 break;
1094 break;
1097 for (av = attr->first_value; av; av = av->next)
1098 if (GET_CODE (av->value) == CONST_STRING
1099 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
1100 break;
1102 if (av == NULL)
1104 message_with_line (attr->lineno,
1105 "unknown value `%s' for `%s' attribute",
1106 XSTR (exp, 0), attr ? attr->name : "internal");
1107 have_error = 1;
1109 break;
1111 case IF_THEN_ELSE:
1112 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
1113 attr ? attr->is_const : 0,
1114 attr ? attr->lineno : 0);
1115 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1116 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
1117 break;
1119 case PLUS:
1120 case MINUS:
1121 case MULT:
1122 case DIV:
1123 case MOD:
1124 if (attr && !attr->is_numeric)
1126 message_with_line (attr->lineno,
1127 "invalid operation `%s' for non-numeric attribute value",
1128 GET_RTX_NAME (GET_CODE (exp)));
1129 have_error = 1;
1130 break;
1132 /* FALLTHRU */
1134 case IOR:
1135 case AND:
1136 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1137 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1138 break;
1140 case FFS:
1141 case CLZ:
1142 case CTZ:
1143 case POPCOUNT:
1144 case PARITY:
1145 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1146 break;
1148 case COND:
1149 if (XVECLEN (exp, 0) % 2 != 0)
1151 message_with_line (attr->lineno,
1152 "first operand of COND must have even length");
1153 have_error = 1;
1154 break;
1157 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1159 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
1160 attr ? attr->is_const : 0,
1161 attr ? attr->lineno : 0);
1162 XVECEXP (exp, 0, i + 1)
1163 = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
1166 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1167 break;
1169 case ATTR:
1171 struct attr_desc *attr2 = find_attr (XSTR (exp, 0), 0);
1172 if (attr2 == NULL)
1174 message_with_line (attr ? attr->lineno : 0,
1175 "unknown attribute `%s' in ATTR",
1176 XSTR (exp, 0));
1177 have_error = 1;
1179 else if (attr && attr->is_const && ! attr2->is_const)
1181 message_with_line (attr->lineno,
1182 "non-constant attribute `%s' referenced from `%s'",
1183 XSTR (exp, 0), attr->name);
1184 have_error = 1;
1186 else if (attr
1187 && (attr->is_numeric != attr2->is_numeric
1188 || (! attr->negative_ok && attr2->negative_ok)))
1190 message_with_line (attr->lineno,
1191 "numeric attribute mismatch calling `%s' from `%s'",
1192 XSTR (exp, 0), attr->name);
1193 have_error = 1;
1196 break;
1198 case SYMBOL_REF:
1199 /* A constant SYMBOL_REF is valid as a constant attribute test and
1200 is expanded later by make_canonical into a COND. In a non-constant
1201 attribute test, it is left be. */
1202 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1204 default:
1205 message_with_line (attr ? attr->lineno : 0,
1206 "invalid operation `%s' for attribute value",
1207 GET_RTX_NAME (GET_CODE (exp)));
1208 have_error = 1;
1209 break;
1212 return exp;
1215 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1216 It becomes a COND with each test being (eq_attr "alternative "n") */
1218 static rtx
1219 convert_set_attr_alternative (rtx exp, struct insn_def *id)
1221 int num_alt = id->num_alternatives;
1222 rtx condexp;
1223 int i;
1225 if (XVECLEN (exp, 1) != num_alt)
1227 message_with_line (id->lineno,
1228 "bad number of entries in SET_ATTR_ALTERNATIVE");
1229 have_error = 1;
1230 return NULL_RTX;
1233 /* Make a COND with all tests but the last. Select the last value via the
1234 default. */
1235 condexp = rtx_alloc (COND);
1236 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1238 for (i = 0; i < num_alt - 1; i++)
1240 const char *p;
1241 p = attr_numeral (i);
1243 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1244 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1247 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1249 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1252 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1253 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1255 static rtx
1256 convert_set_attr (rtx exp, struct insn_def *id)
1258 rtx newexp;
1259 const char *name_ptr;
1260 char *p;
1261 int n;
1263 /* See how many alternative specified. */
1264 n = n_comma_elts (XSTR (exp, 1));
1265 if (n == 1)
1266 return attr_rtx (SET,
1267 attr_rtx (ATTR, XSTR (exp, 0)),
1268 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1270 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1271 XSTR (newexp, 0) = XSTR (exp, 0);
1272 XVEC (newexp, 1) = rtvec_alloc (n);
1274 /* Process each comma-separated name. */
1275 name_ptr = XSTR (exp, 1);
1276 n = 0;
1277 while ((p = next_comma_elt (&name_ptr)) != NULL)
1278 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1280 return convert_set_attr_alternative (newexp, id);
1283 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1284 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1285 expressions. */
1287 static void
1288 check_defs (void)
1290 struct insn_def *id;
1291 struct attr_desc *attr;
1292 int i;
1293 rtx value;
1295 for (id = defs; id; id = id->next)
1297 if (XVEC (id->def, id->vec_idx) == NULL)
1298 continue;
1300 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1302 value = XVECEXP (id->def, id->vec_idx, i);
1303 switch (GET_CODE (value))
1305 case SET:
1306 if (GET_CODE (XEXP (value, 0)) != ATTR)
1308 message_with_line (id->lineno, "bad attribute set");
1309 have_error = 1;
1310 value = NULL_RTX;
1312 break;
1314 case SET_ATTR_ALTERNATIVE:
1315 value = convert_set_attr_alternative (value, id);
1316 break;
1318 case SET_ATTR:
1319 value = convert_set_attr (value, id);
1320 break;
1322 default:
1323 message_with_line (id->lineno, "invalid attribute code %s",
1324 GET_RTX_NAME (GET_CODE (value)));
1325 have_error = 1;
1326 value = NULL_RTX;
1328 if (value == NULL_RTX)
1329 continue;
1331 if ((attr = find_attr (XSTR (XEXP (value, 0), 0), 0)) == NULL)
1333 message_with_line (id->lineno, "unknown attribute %s",
1334 XSTR (XEXP (value, 0), 0));
1335 have_error = 1;
1336 continue;
1339 XVECEXP (id->def, id->vec_idx, i) = value;
1340 XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1345 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1346 expressions by converting them into a COND. This removes cases from this
1347 program. Also, replace an attribute value of "*" with the default attribute
1348 value. */
1350 static rtx
1351 make_canonical (struct attr_desc *attr, rtx exp)
1353 int i;
1354 rtx newexp;
1356 switch (GET_CODE (exp))
1358 case CONST_INT:
1359 exp = make_numeric_value (INTVAL (exp));
1360 break;
1362 case CONST_STRING:
1363 if (! strcmp (XSTR (exp, 0), "*"))
1365 if (attr == 0 || attr->default_val == 0)
1366 fatal ("(attr_value \"*\") used in invalid context");
1367 exp = attr->default_val->value;
1370 break;
1372 case SYMBOL_REF:
1373 if (!attr->is_const || ATTR_IND_SIMPLIFIED_P (exp))
1374 break;
1375 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1376 This makes the COND something that won't be considered an arbitrary
1377 expression by walk_attr_value. */
1378 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1379 exp = check_attr_value (exp, attr);
1380 break;
1382 case IF_THEN_ELSE:
1383 newexp = rtx_alloc (COND);
1384 XVEC (newexp, 0) = rtvec_alloc (2);
1385 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1386 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1388 XEXP (newexp, 1) = XEXP (exp, 2);
1390 exp = newexp;
1391 /* Fall through to COND case since this is now a COND. */
1393 case COND:
1395 int allsame = 1;
1396 rtx defval;
1398 /* First, check for degenerate COND. */
1399 if (XVECLEN (exp, 0) == 0)
1400 return make_canonical (attr, XEXP (exp, 1));
1401 defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1403 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1405 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1406 XVECEXP (exp, 0, i + 1)
1407 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1408 if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1409 allsame = 0;
1411 if (allsame)
1412 return defval;
1414 break;
1416 default:
1417 break;
1420 return exp;
1423 static rtx
1424 copy_boolean (rtx exp)
1426 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1427 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1428 copy_boolean (XEXP (exp, 1)));
1429 return exp;
1432 /* Given a value and an attribute description, return a `struct attr_value *'
1433 that represents that value. This is either an existing structure, if the
1434 value has been previously encountered, or a newly-created structure.
1436 `insn_code' is the code of an insn whose attribute has the specified
1437 value (-2 if not processing an insn). We ensure that all insns for
1438 a given value have the same number of alternatives if the value checks
1439 alternatives. */
1441 static struct attr_value *
1442 get_attr_value (rtx value, struct attr_desc *attr, int insn_code)
1444 struct attr_value *av;
1445 int num_alt = 0;
1447 value = make_canonical (attr, value);
1448 if (compares_alternatives_p (value))
1450 if (insn_code < 0 || insn_alternatives == NULL)
1451 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1452 else
1453 num_alt = insn_alternatives[insn_code];
1456 for (av = attr->first_value; av; av = av->next)
1457 if (rtx_equal_p (value, av->value)
1458 && (num_alt == 0 || av->first_insn == NULL
1459 || insn_alternatives[av->first_insn->insn_code]))
1460 return av;
1462 av = (struct attr_value *) oballoc (sizeof (struct attr_value));
1463 av->value = value;
1464 av->next = attr->first_value;
1465 attr->first_value = av;
1466 av->first_insn = NULL;
1467 av->num_insns = 0;
1468 av->has_asm_insn = 0;
1470 return av;
1473 /* After all DEFINE_DELAYs have been read in, create internal attributes
1474 to generate the required routines.
1476 First, we compute the number of delay slots for each insn (as a COND of
1477 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1478 delay type is specified, we compute a similar function giving the
1479 DEFINE_DELAY ordinal for each insn.
1481 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1482 tells whether a given insn can be in that delay slot.
1484 Normal attribute filling and optimization expands these to contain the
1485 information needed to handle delay slots. */
1487 static void
1488 expand_delays (void)
1490 struct delay_desc *delay;
1491 rtx condexp;
1492 rtx newexp;
1493 int i;
1494 char *p;
1496 /* First, generate data for `num_delay_slots' function. */
1498 condexp = rtx_alloc (COND);
1499 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1500 XEXP (condexp, 1) = make_numeric_value (0);
1502 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1504 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1505 XVECEXP (condexp, 0, i + 1)
1506 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1509 make_internal_attr ("*num_delay_slots", condexp, ATTR_NONE);
1511 /* If more than one delay type, do the same for computing the delay type. */
1512 if (num_delays > 1)
1514 condexp = rtx_alloc (COND);
1515 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1516 XEXP (condexp, 1) = make_numeric_value (0);
1518 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1520 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1521 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1524 make_internal_attr ("*delay_type", condexp, ATTR_SPECIAL);
1527 /* For each delay possibility and delay slot, compute an eligibility
1528 attribute for non-annulled insns and for each type of annulled (annul
1529 if true and annul if false). */
1530 for (delay = delays; delay; delay = delay->next)
1532 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1534 condexp = XVECEXP (delay->def, 1, i);
1535 if (condexp == 0)
1536 condexp = false_rtx;
1537 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1538 make_numeric_value (1), make_numeric_value (0));
1540 p = attr_printf (sizeof "*delay__" + MAX_DIGITS * 2,
1541 "*delay_%d_%d", delay->num, i / 3);
1542 make_internal_attr (p, newexp, ATTR_SPECIAL);
1544 if (have_annul_true)
1546 condexp = XVECEXP (delay->def, 1, i + 1);
1547 if (condexp == 0) condexp = false_rtx;
1548 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1549 make_numeric_value (1),
1550 make_numeric_value (0));
1551 p = attr_printf (sizeof "*annul_true__" + MAX_DIGITS * 2,
1552 "*annul_true_%d_%d", delay->num, i / 3);
1553 make_internal_attr (p, newexp, ATTR_SPECIAL);
1556 if (have_annul_false)
1558 condexp = XVECEXP (delay->def, 1, i + 2);
1559 if (condexp == 0) condexp = false_rtx;
1560 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1561 make_numeric_value (1),
1562 make_numeric_value (0));
1563 p = attr_printf (sizeof "*annul_false__" + MAX_DIGITS * 2,
1564 "*annul_false_%d_%d", delay->num, i / 3);
1565 make_internal_attr (p, newexp, ATTR_SPECIAL);
1571 /* This function is given a left and right side expression and an operator.
1572 Each side is a conditional expression, each alternative of which has a
1573 numerical value. The function returns another conditional expression
1574 which, for every possible set of condition values, returns a value that is
1575 the operator applied to the values of the two sides.
1577 Since this is called early, it must also support IF_THEN_ELSE. */
1579 static rtx
1580 operate_exp (enum operator op, rtx left, rtx right)
1582 int left_value, right_value;
1583 rtx newexp;
1584 int i;
1586 /* If left is a string, apply operator to it and the right side. */
1587 if (GET_CODE (left) == CONST_STRING)
1589 /* If right is also a string, just perform the operation. */
1590 if (GET_CODE (right) == CONST_STRING)
1592 left_value = atoi (XSTR (left, 0));
1593 right_value = atoi (XSTR (right, 0));
1594 switch (op)
1596 case PLUS_OP:
1597 i = left_value + right_value;
1598 break;
1600 case MINUS_OP:
1601 i = left_value - right_value;
1602 break;
1604 case POS_MINUS_OP: /* The positive part of LEFT - RIGHT. */
1605 if (left_value > right_value)
1606 i = left_value - right_value;
1607 else
1608 i = 0;
1609 break;
1611 case OR_OP:
1612 case ORX_OP:
1613 i = left_value | right_value;
1614 break;
1616 case EQ_OP:
1617 i = left_value == right_value;
1618 break;
1620 case RANGE_OP:
1621 i = (left_value << (HOST_BITS_PER_INT / 2)) | right_value;
1622 break;
1624 case MAX_OP:
1625 if (left_value > right_value)
1626 i = left_value;
1627 else
1628 i = right_value;
1629 break;
1631 case MIN_OP:
1632 if (left_value < right_value)
1633 i = left_value;
1634 else
1635 i = right_value;
1636 break;
1638 default:
1639 abort ();
1642 if (i == left_value)
1643 return left;
1644 if (i == right_value)
1645 return right;
1646 return make_numeric_value (i);
1648 else if (GET_CODE (right) == IF_THEN_ELSE)
1650 /* Apply recursively to all values within. */
1651 rtx newleft = operate_exp (op, left, XEXP (right, 1));
1652 rtx newright = operate_exp (op, left, XEXP (right, 2));
1653 if (rtx_equal_p (newleft, newright))
1654 return newleft;
1655 return attr_rtx (IF_THEN_ELSE, XEXP (right, 0), newleft, newright);
1657 else if (GET_CODE (right) == COND)
1659 int allsame = 1;
1660 rtx defval;
1662 newexp = rtx_alloc (COND);
1663 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (right, 0));
1664 defval = XEXP (newexp, 1) = operate_exp (op, left, XEXP (right, 1));
1666 for (i = 0; i < XVECLEN (right, 0); i += 2)
1668 XVECEXP (newexp, 0, i) = XVECEXP (right, 0, i);
1669 XVECEXP (newexp, 0, i + 1)
1670 = operate_exp (op, left, XVECEXP (right, 0, i + 1));
1671 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1672 defval))
1673 allsame = 0;
1676 /* If the resulting cond is trivial (all alternatives
1677 give the same value), optimize it away. */
1678 if (allsame)
1679 return operate_exp (op, left, XEXP (right, 1));
1681 return newexp;
1683 else
1684 fatal ("badly formed attribute value");
1687 /* A hack to prevent expand_units from completely blowing up: ORX_OP does
1688 not associate through IF_THEN_ELSE. */
1689 else if (op == ORX_OP && GET_CODE (right) == IF_THEN_ELSE)
1691 return attr_rtx (IOR, left, right);
1694 /* Otherwise, do recursion the other way. */
1695 else if (GET_CODE (left) == IF_THEN_ELSE)
1697 rtx newleft = operate_exp (op, XEXP (left, 1), right);
1698 rtx newright = operate_exp (op, XEXP (left, 2), right);
1699 if (rtx_equal_p (newleft, newright))
1700 return newleft;
1701 return attr_rtx (IF_THEN_ELSE, XEXP (left, 0), newleft, newright);
1703 else if (GET_CODE (left) == COND)
1705 int allsame = 1;
1706 rtx defval;
1708 newexp = rtx_alloc (COND);
1709 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (left, 0));
1710 defval = XEXP (newexp, 1) = operate_exp (op, XEXP (left, 1), right);
1712 for (i = 0; i < XVECLEN (left, 0); i += 2)
1714 XVECEXP (newexp, 0, i) = XVECEXP (left, 0, i);
1715 XVECEXP (newexp, 0, i + 1)
1716 = operate_exp (op, XVECEXP (left, 0, i + 1), right);
1717 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1718 defval))
1719 allsame = 0;
1722 /* If the cond is trivial (all alternatives give the same value),
1723 optimize it away. */
1724 if (allsame)
1725 return operate_exp (op, XEXP (left, 1), right);
1727 /* If the result is the same as the LEFT operand,
1728 just use that. */
1729 if (rtx_equal_p (newexp, left))
1730 return left;
1732 return newexp;
1735 else
1736 fatal ("badly formed attribute value");
1737 /* NOTREACHED */
1738 return NULL;
1741 /* Once all attributes and DEFINE_FUNCTION_UNITs have been read, we
1742 construct a number of attributes.
1744 The first produces a function `function_units_used' which is given an
1745 insn and produces an encoding showing which function units are required
1746 for the execution of that insn. If the value is non-negative, the insn
1747 uses that unit; otherwise, the value is a one's complement mask of units
1748 used.
1750 The second produces a function `result_ready_cost' which is used to
1751 determine the time that the result of an insn will be ready and hence
1752 a worst-case schedule.
1754 Both of these produce quite complex expressions which are then set as the
1755 default value of internal attributes. Normal attribute simplification
1756 should produce reasonable expressions.
1758 For each unit, a `<name>_unit_ready_cost' function will take an
1759 insn and give the delay until that unit will be ready with the result
1760 and a `<name>_unit_conflict_cost' function is given an insn already
1761 executing on the unit and a candidate to execute and will give the
1762 cost from the time the executing insn started until the candidate
1763 can start (ignore limitations on the number of simultaneous insns).
1765 For each unit, a `<name>_unit_blockage' function is given an insn
1766 already executing on the unit and a candidate to execute and will
1767 give the delay incurred due to function unit conflicts. The range of
1768 blockage cost values for a given executing insn is given by the
1769 `<name>_unit_blockage_range' function. These values are encoded in
1770 an int where the upper half gives the minimum value and the lower
1771 half gives the maximum value. */
1773 static void
1774 expand_units (void)
1776 struct function_unit *unit, **unit_num;
1777 struct function_unit_op *op, **op_array, ***unit_ops;
1778 rtx unitsmask;
1779 rtx readycost;
1780 rtx newexp;
1781 const char *str;
1782 int i, j, u, num, nvalues;
1784 /* Rebuild the condition for the unit to share the RTL expressions.
1785 Sharing is required by simplify_by_exploding. Build the issue delay
1786 expressions. Validate the expressions we were given for the conditions
1787 and conflict vector. Then make attributes for use in the conflict
1788 function. */
1790 for (unit = units; unit; unit = unit->next)
1792 unit->condexp = check_attr_test (unit->condexp, 0, unit->first_lineno);
1794 for (op = unit->ops; op; op = op->next)
1796 rtx issue_delay = make_numeric_value (op->issue_delay);
1797 rtx issue_exp = issue_delay;
1799 /* Build, validate, and simplify the issue delay expression. */
1800 if (op->conflict_exp != true_rtx)
1801 issue_exp = attr_rtx (IF_THEN_ELSE, op->conflict_exp,
1802 issue_exp, make_numeric_value (0));
1803 issue_exp = check_attr_value (make_canonical (NULL_ATTR,
1804 issue_exp),
1805 NULL_ATTR);
1806 issue_exp = simplify_knowing (issue_exp, unit->condexp);
1807 op->issue_exp = issue_exp;
1809 /* Make an attribute for use in the conflict function if needed. */
1810 unit->needs_conflict_function = (unit->issue_delay.min
1811 != unit->issue_delay.max);
1812 if (unit->needs_conflict_function)
1814 str = attr_printf ((strlen (unit->name) + sizeof "*_cost_"
1815 + MAX_DIGITS),
1816 "*%s_cost_%d", unit->name, op->num);
1817 make_internal_attr (str, issue_exp, ATTR_SPECIAL);
1820 /* Validate the condition. */
1821 op->condexp = check_attr_test (op->condexp, 0, op->lineno);
1825 /* Compute the mask of function units used. Initially, the unitsmask is
1826 zero. Set up a conditional to compute each unit's contribution. */
1827 unitsmask = make_numeric_value (0);
1828 newexp = rtx_alloc (IF_THEN_ELSE);
1829 XEXP (newexp, 2) = make_numeric_value (0);
1831 /* If we have just a few units, we may be all right expanding the whole
1832 thing. But the expansion is 2**N in space on the number of opclasses,
1833 so we can't do this for very long -- Alpha and MIPS in particular have
1834 problems with this. So in that situation, we fall back on an alternate
1835 implementation method. */
1836 #define NUM_UNITOP_CUTOFF 20
1838 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1840 /* Merge each function unit into the unit mask attributes. */
1841 for (unit = units; unit; unit = unit->next)
1843 XEXP (newexp, 0) = unit->condexp;
1844 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1845 unitsmask = operate_exp (OR_OP, unitsmask, newexp);
1848 else
1850 /* Merge each function unit into the unit mask attributes. */
1851 for (unit = units; unit; unit = unit->next)
1853 XEXP (newexp, 0) = unit->condexp;
1854 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1855 unitsmask = operate_exp (ORX_OP, unitsmask, attr_copy_rtx (newexp));
1859 /* Simplify the unit mask expression, encode it, and make an attribute
1860 for the function_units_used function. */
1861 unitsmask = simplify_by_exploding (unitsmask);
1863 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1864 unitsmask = encode_units_mask (unitsmask);
1865 else
1867 /* We can no longer encode unitsmask at compile time, so emit code to
1868 calculate it at runtime. Rather, put a marker for where we'd do
1869 the code, and actually output it in write_attr_get(). */
1870 unitsmask = attr_rtx (FFS, unitsmask);
1873 make_internal_attr ("*function_units_used", unitsmask,
1874 (ATTR_NEGATIVE_OK | ATTR_FUNC_UNITS));
1876 /* Create an array of ops for each unit. Add an extra unit for the
1877 result_ready_cost function that has the ops of all other units. */
1878 unit_ops = (struct function_unit_op ***)
1879 xmalloc ((num_units + 1) * sizeof (struct function_unit_op **));
1880 unit_num = (struct function_unit **)
1881 xmalloc ((num_units + 1) * sizeof (struct function_unit *));
1883 unit_num[num_units] = unit = (struct function_unit *)
1884 xmalloc (sizeof (struct function_unit));
1885 unit->num = num_units;
1886 unit->num_opclasses = 0;
1888 for (unit = units; unit; unit = unit->next)
1890 unit_num[num_units]->num_opclasses += unit->num_opclasses;
1891 unit_num[unit->num] = unit;
1892 unit_ops[unit->num] = op_array = (struct function_unit_op **)
1893 xmalloc (unit->num_opclasses * sizeof (struct function_unit_op *));
1895 for (op = unit->ops; op; op = op->next)
1896 op_array[op->num] = op;
1899 /* Compose the array of ops for the extra unit. */
1900 unit_ops[num_units] = op_array = (struct function_unit_op **)
1901 xmalloc (unit_num[num_units]->num_opclasses
1902 * sizeof (struct function_unit_op *));
1904 for (unit = units, i = 0; unit; i += unit->num_opclasses, unit = unit->next)
1905 memcpy (&op_array[i], unit_ops[unit->num],
1906 unit->num_opclasses * sizeof (struct function_unit_op *));
1908 /* Compute the ready cost function for each unit by computing the
1909 condition for each non-default value. */
1910 for (u = 0; u <= num_units; u++)
1912 rtx orexp;
1913 int value;
1915 unit = unit_num[u];
1916 op_array = unit_ops[unit->num];
1917 num = unit->num_opclasses;
1919 /* Sort the array of ops into increasing ready cost order. */
1920 for (i = 0; i < num; i++)
1921 for (j = num - 1; j > i; j--)
1922 if (op_array[j - 1]->ready < op_array[j]->ready)
1924 op = op_array[j];
1925 op_array[j] = op_array[j - 1];
1926 op_array[j - 1] = op;
1929 /* Determine how many distinct non-default ready cost values there
1930 are. We use a default ready cost value of 1. */
1931 nvalues = 0; value = 1;
1932 for (i = num - 1; i >= 0; i--)
1933 if (op_array[i]->ready > value)
1935 value = op_array[i]->ready;
1936 nvalues++;
1939 if (nvalues == 0)
1940 readycost = make_numeric_value (1);
1941 else
1943 /* Construct the ready cost expression as a COND of each value from
1944 the largest to the smallest. */
1945 readycost = rtx_alloc (COND);
1946 XVEC (readycost, 0) = rtvec_alloc (nvalues * 2);
1947 XEXP (readycost, 1) = make_numeric_value (1);
1949 nvalues = 0;
1950 orexp = false_rtx;
1951 value = op_array[0]->ready;
1952 for (i = 0; i < num; i++)
1954 op = op_array[i];
1955 if (op->ready <= 1)
1956 break;
1957 else if (op->ready == value)
1958 orexp = insert_right_side (IOR, orexp, op->condexp, -2, -2);
1959 else
1961 XVECEXP (readycost, 0, nvalues * 2) = orexp;
1962 XVECEXP (readycost, 0, nvalues * 2 + 1)
1963 = make_numeric_value (value);
1964 nvalues++;
1965 value = op->ready;
1966 orexp = op->condexp;
1969 XVECEXP (readycost, 0, nvalues * 2) = orexp;
1970 XVECEXP (readycost, 0, nvalues * 2 + 1) = make_numeric_value (value);
1973 if (u < num_units)
1975 rtx max_blockage = 0, min_blockage = 0;
1977 /* Simplify the readycost expression by only considering insns
1978 that use the unit. */
1979 readycost = simplify_knowing (readycost, unit->condexp);
1981 /* Determine the blockage cost the executing insn (E) given
1982 the candidate insn (C). This is the maximum of the issue
1983 delay, the pipeline delay, and the simultaneity constraint.
1984 Each function_unit_op represents the characteristics of the
1985 candidate insn, so in the expressions below, C is a known
1986 term and E is an unknown term.
1988 We compute the blockage cost for each E for every possible C.
1989 Thus OP represents E, and READYCOST is a list of values for
1990 every possible C.
1992 The issue delay function for C is op->issue_exp and is used to
1993 write the `<name>_unit_conflict_cost' function. Symbolically
1994 this is "ISSUE-DELAY (E,C)".
1996 The pipeline delay results form the FIFO constraint on the
1997 function unit and is "READY-COST (E) + 1 - READY-COST (C)".
1999 The simultaneity constraint is based on how long it takes to
2000 fill the unit given the minimum issue delay. FILL-TIME is the
2001 constant "MIN (ISSUE-DELAY (*,*)) * (SIMULTANEITY - 1)", and
2002 the simultaneity constraint is "READY-COST (E) - FILL-TIME"
2003 if SIMULTANEITY is nonzero and zero otherwise.
2005 Thus, BLOCKAGE (E,C) when SIMULTANEITY is zero is
2007 MAX (ISSUE-DELAY (E,C),
2008 READY-COST (E) - (READY-COST (C) - 1))
2010 and otherwise
2012 MAX (ISSUE-DELAY (E,C),
2013 READY-COST (E) - (READY-COST (C) - 1),
2014 READY-COST (E) - FILL-TIME)
2016 The `<name>_unit_blockage' function is computed by determining
2017 this value for each candidate insn. As these values are
2018 computed, we also compute the upper and lower bounds for
2019 BLOCKAGE (E,*). These are combined to form the function
2020 `<name>_unit_blockage_range'. Finally, the maximum blockage
2021 cost, MAX (BLOCKAGE (*,*)), is computed. */
2023 for (op = unit->ops; op; op = op->next)
2025 rtx blockage = op->issue_exp;
2026 blockage = simplify_knowing (blockage, unit->condexp);
2028 /* Add this op's contribution to MAX (BLOCKAGE (E,*)) and
2029 MIN (BLOCKAGE (E,*)). */
2030 if (max_blockage == 0)
2031 max_blockage = min_blockage = blockage;
2032 else
2034 max_blockage
2035 = simplify_knowing (operate_exp (MAX_OP, max_blockage,
2036 blockage),
2037 unit->condexp);
2038 min_blockage
2039 = simplify_knowing (operate_exp (MIN_OP, min_blockage,
2040 blockage),
2041 unit->condexp);
2044 /* Make an attribute for use in the blockage function. */
2045 str = attr_printf ((strlen (unit->name) + sizeof "*_block_"
2046 + MAX_DIGITS),
2047 "*%s_block_%d", unit->name, op->num);
2048 make_internal_attr (str, blockage, ATTR_SPECIAL);
2051 /* Record MAX (BLOCKAGE (*,*)). */
2053 int unknown;
2054 unit->max_blockage = max_attr_value (max_blockage, &unknown);
2057 /* See if the upper and lower bounds of BLOCKAGE (E,*) are the
2058 same. If so, the blockage function carries no additional
2059 information and is not written. */
2060 newexp = operate_exp (EQ_OP, max_blockage, min_blockage);
2061 newexp = simplify_knowing (newexp, unit->condexp);
2062 unit->needs_blockage_function
2063 = (GET_CODE (newexp) != CONST_STRING
2064 || atoi (XSTR (newexp, 0)) != 1);
2066 /* If the all values of BLOCKAGE (E,C) have the same value,
2067 neither blockage function is written. */
2068 unit->needs_range_function
2069 = (unit->needs_blockage_function
2070 || GET_CODE (max_blockage) != CONST_STRING);
2072 if (unit->needs_range_function)
2074 /* Compute the blockage range function and make an attribute
2075 for writing its value. */
2076 newexp = operate_exp (RANGE_OP, min_blockage, max_blockage);
2077 newexp = simplify_knowing (newexp, unit->condexp);
2079 str = attr_printf ((strlen (unit->name)
2080 + sizeof "*_unit_blockage_range"),
2081 "*%s_unit_blockage_range", unit->name);
2082 make_internal_attr (str, newexp, (ATTR_STATIC|ATTR_BLOCKAGE|ATTR_UNSIGNED));
2085 str = attr_printf (strlen (unit->name) + sizeof "*_unit_ready_cost",
2086 "*%s_unit_ready_cost", unit->name);
2087 make_internal_attr (str, readycost, ATTR_STATIC);
2089 else
2091 /* Make an attribute for the ready_cost function. Simplifying
2092 further with simplify_by_exploding doesn't win. */
2093 str = "*result_ready_cost";
2094 make_internal_attr (str, readycost, ATTR_NONE);
2098 /* For each unit that requires a conflict cost function, make an attribute
2099 that maps insns to the operation number. */
2100 for (unit = units; unit; unit = unit->next)
2102 rtx caseexp;
2104 if (! unit->needs_conflict_function
2105 && ! unit->needs_blockage_function)
2106 continue;
2108 caseexp = rtx_alloc (COND);
2109 XVEC (caseexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
2111 for (op = unit->ops; op; op = op->next)
2113 /* Make our adjustment to the COND being computed. If we are the
2114 last operation class, place our values into the default of the
2115 COND. */
2116 if (op->num == unit->num_opclasses - 1)
2118 XEXP (caseexp, 1) = make_numeric_value (op->num);
2120 else
2122 XVECEXP (caseexp, 0, op->num * 2) = op->condexp;
2123 XVECEXP (caseexp, 0, op->num * 2 + 1)
2124 = make_numeric_value (op->num);
2128 /* Simplifying caseexp with simplify_by_exploding doesn't win. */
2129 str = attr_printf (strlen (unit->name) + sizeof "*_cases",
2130 "*%s_cases", unit->name);
2131 make_internal_attr (str, caseexp, ATTR_SPECIAL);
2135 /* Simplify EXP given KNOWN_TRUE. */
2137 static rtx
2138 simplify_knowing (rtx exp, rtx known_true)
2140 if (GET_CODE (exp) != CONST_STRING)
2142 int unknown = 0, max;
2143 max = max_attr_value (exp, &unknown);
2144 if (! unknown)
2146 exp = attr_rtx (IF_THEN_ELSE, known_true, exp,
2147 make_numeric_value (max));
2148 exp = simplify_by_exploding (exp);
2151 return exp;
2154 /* Translate the CONST_STRING expressions in X to change the encoding of
2155 value. On input, the value is a bitmask with a one bit for each unit
2156 used; on output, the value is the unit number (zero based) if one
2157 and only one unit is used or the one's complement of the bitmask. */
2159 static rtx
2160 encode_units_mask (rtx x)
2162 int i;
2163 int j;
2164 enum rtx_code code;
2165 const char *fmt;
2167 code = GET_CODE (x);
2169 switch (code)
2171 case CONST_STRING:
2172 i = atoi (XSTR (x, 0));
2173 if (i < 0)
2174 /* The sign bit encodes a one's complement mask. */
2175 abort ();
2176 else if (i != 0 && i == (i & -i))
2177 /* Only one bit is set, so yield that unit number. */
2178 for (j = 0; (i >>= 1) != 0; j++)
2180 else
2181 j = ~i;
2182 return attr_rtx (CONST_STRING, attr_printf (MAX_DIGITS, "%d", j));
2184 case REG:
2185 case QUEUED:
2186 case CONST_INT:
2187 case CONST_DOUBLE:
2188 case CONST_VECTOR:
2189 case SYMBOL_REF:
2190 case CODE_LABEL:
2191 case PC:
2192 case CC0:
2193 case EQ_ATTR:
2194 return x;
2196 default:
2197 break;
2200 /* Compare the elements. If any pair of corresponding elements
2201 fail to match, return 0 for the whole things. */
2203 fmt = GET_RTX_FORMAT (code);
2204 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2206 switch (fmt[i])
2208 case 'V':
2209 case 'E':
2210 for (j = 0; j < XVECLEN (x, i); j++)
2211 XVECEXP (x, i, j) = encode_units_mask (XVECEXP (x, i, j));
2212 break;
2214 case 'e':
2215 XEXP (x, i) = encode_units_mask (XEXP (x, i));
2216 break;
2219 return x;
2222 /* Once all attributes and insns have been read and checked, we construct for
2223 each attribute value a list of all the insns that have that value for
2224 the attribute. */
2226 static void
2227 fill_attr (struct attr_desc *attr)
2229 struct attr_value *av;
2230 struct insn_ent *ie;
2231 struct insn_def *id;
2232 int i;
2233 rtx value;
2235 /* Don't fill constant attributes. The value is independent of
2236 any particular insn. */
2237 if (attr->is_const)
2238 return;
2240 for (id = defs; id; id = id->next)
2242 /* If no value is specified for this insn for this attribute, use the
2243 default. */
2244 value = NULL;
2245 if (XVEC (id->def, id->vec_idx))
2246 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
2247 if (! strcmp (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
2248 attr->name))
2249 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
2251 if (value == NULL)
2252 av = attr->default_val;
2253 else
2254 av = get_attr_value (value, attr, id->insn_code);
2256 ie = (struct insn_ent *) oballoc (sizeof (struct insn_ent));
2257 ie->insn_code = id->insn_code;
2258 ie->insn_index = id->insn_code;
2259 insert_insn_ent (av, ie);
2263 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
2264 test that checks relative positions of insns (uses MATCH_DUP or PC).
2265 If so, replace it with what is obtained by passing the expression to
2266 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
2267 recursively on each value (including the default value). Otherwise,
2268 return the value returned by NO_ADDRESS_FN applied to EXP. */
2270 static rtx
2271 substitute_address (rtx exp, rtx (*no_address_fn) (rtx),
2272 rtx (*address_fn) (rtx))
2274 int i;
2275 rtx newexp;
2277 if (GET_CODE (exp) == COND)
2279 /* See if any tests use addresses. */
2280 address_used = 0;
2281 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2282 walk_attr_value (XVECEXP (exp, 0, i));
2284 if (address_used)
2285 return (*address_fn) (exp);
2287 /* Make a new copy of this COND, replacing each element. */
2288 newexp = rtx_alloc (COND);
2289 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
2290 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2292 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
2293 XVECEXP (newexp, 0, i + 1)
2294 = substitute_address (XVECEXP (exp, 0, i + 1),
2295 no_address_fn, address_fn);
2298 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
2299 no_address_fn, address_fn);
2301 return newexp;
2304 else if (GET_CODE (exp) == IF_THEN_ELSE)
2306 address_used = 0;
2307 walk_attr_value (XEXP (exp, 0));
2308 if (address_used)
2309 return (*address_fn) (exp);
2311 return attr_rtx (IF_THEN_ELSE,
2312 substitute_address (XEXP (exp, 0),
2313 no_address_fn, address_fn),
2314 substitute_address (XEXP (exp, 1),
2315 no_address_fn, address_fn),
2316 substitute_address (XEXP (exp, 2),
2317 no_address_fn, address_fn));
2320 return (*no_address_fn) (exp);
2323 /* Make new attributes from the `length' attribute. The following are made,
2324 each corresponding to a function called from `shorten_branches' or
2325 `get_attr_length':
2327 *insn_default_length This is the length of the insn to be returned
2328 by `get_attr_length' before `shorten_branches'
2329 has been called. In each case where the length
2330 depends on relative addresses, the largest
2331 possible is used. This routine is also used
2332 to compute the initial size of the insn.
2334 *insn_variable_length_p This returns 1 if the insn's length depends
2335 on relative addresses, zero otherwise.
2337 *insn_current_length This is only called when it is known that the
2338 insn has a variable length and returns the
2339 current length, based on relative addresses.
2342 static void
2343 make_length_attrs (void)
2345 static const char *const new_names[] = {"*insn_default_length",
2346 "*insn_variable_length_p",
2347 "*insn_current_length"};
2348 static rtx (*const no_address_fn[]) (rtx) = {identity_fn, zero_fn, zero_fn};
2349 static rtx (*const address_fn[]) (rtx) = {max_fn, one_fn, identity_fn};
2350 size_t i;
2351 struct attr_desc *length_attr, *new_attr;
2352 struct attr_value *av, *new_av;
2353 struct insn_ent *ie, *new_ie;
2355 /* See if length attribute is defined. If so, it must be numeric. Make
2356 it special so we don't output anything for it. */
2357 length_attr = find_attr ("length", 0);
2358 if (length_attr == 0)
2359 return;
2361 if (! length_attr->is_numeric)
2362 fatal ("length attribute must be numeric");
2364 length_attr->is_const = 0;
2365 length_attr->is_special = 1;
2367 /* Make each new attribute, in turn. */
2368 for (i = 0; i < ARRAY_SIZE (new_names); i++)
2370 make_internal_attr (new_names[i],
2371 substitute_address (length_attr->default_val->value,
2372 no_address_fn[i], address_fn[i]),
2373 ATTR_NONE);
2374 new_attr = find_attr (new_names[i], 0);
2375 for (av = length_attr->first_value; av; av = av->next)
2376 for (ie = av->first_insn; ie; ie = ie->next)
2378 new_av = get_attr_value (substitute_address (av->value,
2379 no_address_fn[i],
2380 address_fn[i]),
2381 new_attr, ie->insn_code);
2382 new_ie = (struct insn_ent *) oballoc (sizeof (struct insn_ent));
2383 new_ie->insn_code = ie->insn_code;
2384 new_ie->insn_index = ie->insn_index;
2385 insert_insn_ent (new_av, new_ie);
2390 /* Utility functions called from above routine. */
2392 static rtx
2393 identity_fn (rtx exp)
2395 return exp;
2398 static rtx
2399 zero_fn (rtx exp ATTRIBUTE_UNUSED)
2401 return make_numeric_value (0);
2404 static rtx
2405 one_fn (rtx exp ATTRIBUTE_UNUSED)
2407 return make_numeric_value (1);
2410 static rtx
2411 max_fn (rtx exp)
2413 int unknown;
2414 return make_numeric_value (max_attr_value (exp, &unknown));
2417 static void
2418 write_length_unit_log (void)
2420 struct attr_desc *length_attr = find_attr ("length", 0);
2421 struct attr_value *av;
2422 struct insn_ent *ie;
2423 unsigned int length_unit_log, length_or;
2424 int unknown = 0;
2426 if (length_attr == 0)
2427 return;
2428 length_or = or_attr_value (length_attr->default_val->value, &unknown);
2429 for (av = length_attr->first_value; av; av = av->next)
2430 for (ie = av->first_insn; ie; ie = ie->next)
2431 length_or |= or_attr_value (av->value, &unknown);
2433 if (unknown)
2434 length_unit_log = 0;
2435 else
2437 length_or = ~length_or;
2438 for (length_unit_log = 0; length_or & 1; length_or >>= 1)
2439 length_unit_log++;
2441 printf ("int length_unit_log = %u;\n", length_unit_log);
2444 /* Take a COND expression and see if any of the conditions in it can be
2445 simplified. If any are known true or known false for the particular insn
2446 code, the COND can be further simplified.
2448 Also call ourselves on any COND operations that are values of this COND.
2450 We do not modify EXP; rather, we make and return a new rtx. */
2452 static rtx
2453 simplify_cond (rtx exp, int insn_code, int insn_index)
2455 int i, j;
2456 /* We store the desired contents here,
2457 then build a new expression if they don't match EXP. */
2458 rtx defval = XEXP (exp, 1);
2459 rtx new_defval = XEXP (exp, 1);
2460 int len = XVECLEN (exp, 0);
2461 rtx *tests = (rtx *) xmalloc (len * sizeof (rtx));
2462 int allsame = 1;
2463 rtx ret;
2465 /* This lets us free all storage allocated below, if appropriate. */
2466 obstack_finish (rtl_obstack);
2468 memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
2470 /* See if default value needs simplification. */
2471 if (GET_CODE (defval) == COND)
2472 new_defval = simplify_cond (defval, insn_code, insn_index);
2474 /* Simplify the subexpressions, and see what tests we can get rid of. */
2476 for (i = 0; i < len; i += 2)
2478 rtx newtest, newval;
2480 /* Simplify this test. */
2481 newtest = simplify_test_exp_in_temp (tests[i], insn_code, insn_index);
2482 tests[i] = newtest;
2484 newval = tests[i + 1];
2485 /* See if this value may need simplification. */
2486 if (GET_CODE (newval) == COND)
2487 newval = simplify_cond (newval, insn_code, insn_index);
2489 /* Look for ways to delete or combine this test. */
2490 if (newtest == true_rtx)
2492 /* If test is true, make this value the default
2493 and discard this + any following tests. */
2494 len = i;
2495 defval = tests[i + 1];
2496 new_defval = newval;
2499 else if (newtest == false_rtx)
2501 /* If test is false, discard it and its value. */
2502 for (j = i; j < len - 2; j++)
2503 tests[j] = tests[j + 2];
2504 len -= 2;
2507 else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
2509 /* If this value and the value for the prev test are the same,
2510 merge the tests. */
2512 tests[i - 2]
2513 = insert_right_side (IOR, tests[i - 2], newtest,
2514 insn_code, insn_index);
2516 /* Delete this test/value. */
2517 for (j = i; j < len - 2; j++)
2518 tests[j] = tests[j + 2];
2519 len -= 2;
2522 else
2523 tests[i + 1] = newval;
2526 /* If the last test in a COND has the same value
2527 as the default value, that test isn't needed. */
2529 while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
2530 len -= 2;
2532 /* See if we changed anything. */
2533 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
2534 allsame = 0;
2535 else
2536 for (i = 0; i < len; i++)
2537 if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
2539 allsame = 0;
2540 break;
2543 if (len == 0)
2545 if (GET_CODE (defval) == COND)
2546 ret = simplify_cond (defval, insn_code, insn_index);
2547 else
2548 ret = defval;
2550 else if (allsame)
2551 ret = exp;
2552 else
2554 rtx newexp = rtx_alloc (COND);
2556 XVEC (newexp, 0) = rtvec_alloc (len);
2557 memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
2558 XEXP (newexp, 1) = new_defval;
2559 ret = newexp;
2561 free (tests);
2562 return ret;
2565 /* Remove an insn entry from an attribute value. */
2567 static void
2568 remove_insn_ent (struct attr_value *av, struct insn_ent *ie)
2570 struct insn_ent *previe;
2572 if (av->first_insn == ie)
2573 av->first_insn = ie->next;
2574 else
2576 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
2578 previe->next = ie->next;
2581 av->num_insns--;
2582 if (ie->insn_code == -1)
2583 av->has_asm_insn = 0;
2585 num_insn_ents--;
2588 /* Insert an insn entry in an attribute value list. */
2590 static void
2591 insert_insn_ent (struct attr_value *av, struct insn_ent *ie)
2593 ie->next = av->first_insn;
2594 av->first_insn = ie;
2595 av->num_insns++;
2596 if (ie->insn_code == -1)
2597 av->has_asm_insn = 1;
2599 num_insn_ents++;
2602 /* This is a utility routine to take an expression that is a tree of either
2603 AND or IOR expressions and insert a new term. The new term will be
2604 inserted at the right side of the first node whose code does not match
2605 the root. A new node will be created with the root's code. Its left
2606 side will be the old right side and its right side will be the new
2607 term.
2609 If the `term' is itself a tree, all its leaves will be inserted. */
2611 static rtx
2612 insert_right_side (enum rtx_code code, rtx exp, rtx term, int insn_code, int insn_index)
2614 rtx newexp;
2616 /* Avoid consing in some special cases. */
2617 if (code == AND && term == true_rtx)
2618 return exp;
2619 if (code == AND && term == false_rtx)
2620 return false_rtx;
2621 if (code == AND && exp == true_rtx)
2622 return term;
2623 if (code == AND && exp == false_rtx)
2624 return false_rtx;
2625 if (code == IOR && term == true_rtx)
2626 return true_rtx;
2627 if (code == IOR && term == false_rtx)
2628 return exp;
2629 if (code == IOR && exp == true_rtx)
2630 return true_rtx;
2631 if (code == IOR && exp == false_rtx)
2632 return term;
2633 if (attr_equal_p (exp, term))
2634 return exp;
2636 if (GET_CODE (term) == code)
2638 exp = insert_right_side (code, exp, XEXP (term, 0),
2639 insn_code, insn_index);
2640 exp = insert_right_side (code, exp, XEXP (term, 1),
2641 insn_code, insn_index);
2643 return exp;
2646 if (GET_CODE (exp) == code)
2648 rtx new = insert_right_side (code, XEXP (exp, 1),
2649 term, insn_code, insn_index);
2650 if (new != XEXP (exp, 1))
2651 /* Make a copy of this expression and call recursively. */
2652 newexp = attr_rtx (code, XEXP (exp, 0), new);
2653 else
2654 newexp = exp;
2656 else
2658 /* Insert the new term. */
2659 newexp = attr_rtx (code, exp, term);
2662 return simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2665 /* If we have an expression which AND's a bunch of
2666 (not (eq_attrq "alternative" "n"))
2667 terms, we may have covered all or all but one of the possible alternatives.
2668 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
2670 This routine is passed an expression and either AND or IOR. It returns a
2671 bitmask indicating which alternatives are mentioned within EXP. */
2673 static int
2674 compute_alternative_mask (rtx exp, enum rtx_code code)
2676 const char *string;
2677 if (GET_CODE (exp) == code)
2678 return compute_alternative_mask (XEXP (exp, 0), code)
2679 | compute_alternative_mask (XEXP (exp, 1), code);
2681 else if (code == AND && GET_CODE (exp) == NOT
2682 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2683 && XSTR (XEXP (exp, 0), 0) == alternative_name)
2684 string = XSTR (XEXP (exp, 0), 1);
2686 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
2687 && XSTR (exp, 0) == alternative_name)
2688 string = XSTR (exp, 1);
2690 else
2691 return 0;
2693 if (string[1] == 0)
2694 return 1 << (string[0] - '0');
2695 return 1 << atoi (string);
2698 /* Given I, a single-bit mask, return RTX to compare the `alternative'
2699 attribute with the value represented by that bit. */
2701 static rtx
2702 make_alternative_compare (int mask)
2704 rtx newexp;
2705 int i;
2707 /* Find the bit. */
2708 for (i = 0; (mask & (1 << i)) == 0; i++)
2711 newexp = attr_rtx (EQ_ATTR, alternative_name, attr_numeral (i));
2712 ATTR_IND_SIMPLIFIED_P (newexp) = 1;
2714 return newexp;
2717 /* If we are processing an (eq_attr "attr" "value") test, we find the value
2718 of "attr" for this insn code. From that value, we can compute a test
2719 showing when the EQ_ATTR will be true. This routine performs that
2720 computation. If a test condition involves an address, we leave the EQ_ATTR
2721 intact because addresses are only valid for the `length' attribute.
2723 EXP is the EQ_ATTR expression and VALUE is the value of that attribute
2724 for the insn corresponding to INSN_CODE and INSN_INDEX. */
2726 static rtx
2727 evaluate_eq_attr (rtx exp, rtx value, int insn_code, int insn_index)
2729 rtx orexp, andexp;
2730 rtx right;
2731 rtx newexp;
2732 int i;
2734 if (GET_CODE (value) == CONST_STRING)
2736 if (! strcmp (XSTR (value, 0), XSTR (exp, 1)))
2737 newexp = true_rtx;
2738 else
2739 newexp = false_rtx;
2741 else if (GET_CODE (value) == SYMBOL_REF)
2743 char *p;
2744 char string[256];
2746 if (GET_CODE (exp) != EQ_ATTR)
2747 abort ();
2749 if (strlen (XSTR (exp, 0)) + strlen (XSTR (exp, 1)) + 2 > 256)
2750 abort ();
2752 strcpy (string, XSTR (exp, 0));
2753 strcat (string, "_");
2754 strcat (string, XSTR (exp, 1));
2755 for (p = string; *p; p++)
2756 *p = TOUPPER (*p);
2758 newexp = attr_rtx (EQ, value,
2759 attr_rtx (SYMBOL_REF,
2760 attr_string (string, strlen (string))));
2762 else if (GET_CODE (value) == COND)
2764 /* We construct an IOR of all the cases for which the requested attribute
2765 value is present. Since we start with FALSE, if it is not present,
2766 FALSE will be returned.
2768 Each case is the AND of the NOT's of the previous conditions with the
2769 current condition; in the default case the current condition is TRUE.
2771 For each possible COND value, call ourselves recursively.
2773 The extra TRUE and FALSE expressions will be eliminated by another
2774 call to the simplification routine. */
2776 orexp = false_rtx;
2777 andexp = true_rtx;
2779 if (current_alternative_string)
2780 clear_struct_flag (value);
2782 for (i = 0; i < XVECLEN (value, 0); i += 2)
2784 rtx this = simplify_test_exp_in_temp (XVECEXP (value, 0, i),
2785 insn_code, insn_index);
2787 SIMPLIFY_ALTERNATIVE (this);
2789 right = insert_right_side (AND, andexp, this,
2790 insn_code, insn_index);
2791 right = insert_right_side (AND, right,
2792 evaluate_eq_attr (exp,
2793 XVECEXP (value, 0,
2794 i + 1),
2795 insn_code, insn_index),
2796 insn_code, insn_index);
2797 orexp = insert_right_side (IOR, orexp, right,
2798 insn_code, insn_index);
2800 /* Add this condition into the AND expression. */
2801 newexp = attr_rtx (NOT, this);
2802 andexp = insert_right_side (AND, andexp, newexp,
2803 insn_code, insn_index);
2806 /* Handle the default case. */
2807 right = insert_right_side (AND, andexp,
2808 evaluate_eq_attr (exp, XEXP (value, 1),
2809 insn_code, insn_index),
2810 insn_code, insn_index);
2811 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2813 else
2814 abort ();
2816 /* If uses an address, must return original expression. But set the
2817 ATTR_IND_SIMPLIFIED_P bit so we don't try to simplify it again. */
2819 address_used = 0;
2820 walk_attr_value (newexp);
2822 if (address_used)
2824 /* This had `&& current_alternative_string', which seems to be wrong. */
2825 if (! ATTR_IND_SIMPLIFIED_P (exp))
2826 return copy_rtx_unchanging (exp);
2827 return exp;
2829 else
2830 return newexp;
2833 /* This routine is called when an AND of a term with a tree of AND's is
2834 encountered. If the term or its complement is present in the tree, it
2835 can be replaced with TRUE or FALSE, respectively.
2837 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2838 be true and hence are complementary.
2840 There is one special case: If we see
2841 (and (not (eq_attr "att" "v1"))
2842 (eq_attr "att" "v2"))
2843 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2844 replace the term, not anything in the AND tree. So we pass a pointer to
2845 the term. */
2847 static rtx
2848 simplify_and_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2850 rtx left, right;
2851 rtx newexp;
2852 rtx temp;
2853 int left_eliminates_term, right_eliminates_term;
2855 if (GET_CODE (exp) == AND)
2857 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2858 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2859 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2861 newexp = attr_rtx (GET_CODE (exp), left, right);
2863 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2867 else if (GET_CODE (exp) == IOR)
2869 /* For the IOR case, we do the same as above, except that we can
2870 only eliminate `term' if both sides of the IOR would do so. */
2871 temp = *pterm;
2872 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2873 left_eliminates_term = (temp == true_rtx);
2875 temp = *pterm;
2876 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2877 right_eliminates_term = (temp == true_rtx);
2879 if (left_eliminates_term && right_eliminates_term)
2880 *pterm = true_rtx;
2882 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2884 newexp = attr_rtx (GET_CODE (exp), left, right);
2886 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2890 /* Check for simplifications. Do some extra checking here since this
2891 routine is called so many times. */
2893 if (exp == *pterm)
2894 return true_rtx;
2896 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2897 return false_rtx;
2899 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2900 return false_rtx;
2902 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2904 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2905 return exp;
2907 if (! strcmp (XSTR (exp, 1), XSTR (*pterm, 1)))
2908 return true_rtx;
2909 else
2910 return false_rtx;
2913 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2914 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2916 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2917 return exp;
2919 if (! strcmp (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2920 return false_rtx;
2921 else
2922 return true_rtx;
2925 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2926 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2928 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2929 return exp;
2931 if (! strcmp (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2932 return false_rtx;
2933 else
2934 *pterm = true_rtx;
2937 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2939 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2940 return true_rtx;
2943 else if (GET_CODE (exp) == NOT)
2945 if (attr_equal_p (XEXP (exp, 0), *pterm))
2946 return false_rtx;
2949 else if (GET_CODE (*pterm) == NOT)
2951 if (attr_equal_p (XEXP (*pterm, 0), exp))
2952 return false_rtx;
2955 else if (attr_equal_p (exp, *pterm))
2956 return true_rtx;
2958 return exp;
2961 /* Similar to `simplify_and_tree', but for IOR trees. */
2963 static rtx
2964 simplify_or_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2966 rtx left, right;
2967 rtx newexp;
2968 rtx temp;
2969 int left_eliminates_term, right_eliminates_term;
2971 if (GET_CODE (exp) == IOR)
2973 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2974 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2975 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2977 newexp = attr_rtx (GET_CODE (exp), left, right);
2979 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2983 else if (GET_CODE (exp) == AND)
2985 /* For the AND case, we do the same as above, except that we can
2986 only eliminate `term' if both sides of the AND would do so. */
2987 temp = *pterm;
2988 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2989 left_eliminates_term = (temp == false_rtx);
2991 temp = *pterm;
2992 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2993 right_eliminates_term = (temp == false_rtx);
2995 if (left_eliminates_term && right_eliminates_term)
2996 *pterm = false_rtx;
2998 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3000 newexp = attr_rtx (GET_CODE (exp), left, right);
3002 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
3006 if (attr_equal_p (exp, *pterm))
3007 return false_rtx;
3009 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
3010 return true_rtx;
3012 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
3013 return true_rtx;
3015 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
3016 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3017 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
3018 *pterm = false_rtx;
3020 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
3021 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
3022 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
3023 return false_rtx;
3025 return exp;
3028 /* Compute approximate cost of the expression. Used to decide whether
3029 expression is cheap enough for inline. */
3030 static int
3031 attr_rtx_cost (rtx x)
3033 int cost = 0;
3034 enum rtx_code code;
3035 if (!x)
3036 return 0;
3037 code = GET_CODE (x);
3038 switch (code)
3040 case MATCH_OPERAND:
3041 if (XSTR (x, 1)[0])
3042 return 10;
3043 else
3044 return 0;
3045 case EQ_ATTR:
3046 /* Alternatives don't result into function call. */
3047 if (!strcmp (XSTR (x, 0), "alternative"))
3048 return 0;
3049 else
3050 return 5;
3051 default:
3053 int i, j;
3054 const char *fmt = GET_RTX_FORMAT (code);
3055 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3057 switch (fmt[i])
3059 case 'V':
3060 case 'E':
3061 for (j = 0; j < XVECLEN (x, i); j++)
3062 cost += attr_rtx_cost (XVECEXP (x, i, j));
3063 break;
3064 case 'e':
3065 cost += attr_rtx_cost (XEXP (x, i));
3066 break;
3070 break;
3072 return cost;
3075 /* Simplify test expression and use temporary obstack in order to avoid
3076 memory bloat. Use ATTR_IND_SIMPLIFIED to avoid unnecessary simplifications
3077 and avoid unnecessary copying if possible. */
3079 static rtx
3080 simplify_test_exp_in_temp (rtx exp, int insn_code, int insn_index)
3082 rtx x;
3083 struct obstack *old;
3084 if (ATTR_IND_SIMPLIFIED_P (exp))
3085 return exp;
3086 old = rtl_obstack;
3087 rtl_obstack = temp_obstack;
3088 x = simplify_test_exp (exp, insn_code, insn_index);
3089 rtl_obstack = old;
3090 if (x == exp || rtl_obstack == temp_obstack)
3091 return x;
3092 return attr_copy_rtx (x);
3095 /* Given an expression, see if it can be simplified for a particular insn
3096 code based on the values of other attributes being tested. This can
3097 eliminate nested get_attr_... calls.
3099 Note that if an endless recursion is specified in the patterns, the
3100 optimization will loop. However, it will do so in precisely the cases where
3101 an infinite recursion loop could occur during compilation. It's better that
3102 it occurs here! */
3104 static rtx
3105 simplify_test_exp (rtx exp, int insn_code, int insn_index)
3107 rtx left, right;
3108 struct attr_desc *attr;
3109 struct attr_value *av;
3110 struct insn_ent *ie;
3111 int i;
3112 rtx newexp = exp;
3114 /* Don't re-simplify something we already simplified. */
3115 if (ATTR_IND_SIMPLIFIED_P (exp) || ATTR_CURR_SIMPLIFIED_P (exp))
3116 return exp;
3118 switch (GET_CODE (exp))
3120 case AND:
3121 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3122 SIMPLIFY_ALTERNATIVE (left);
3123 if (left == false_rtx)
3124 return false_rtx;
3125 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3126 SIMPLIFY_ALTERNATIVE (right);
3127 if (left == false_rtx)
3128 return false_rtx;
3130 /* If either side is an IOR and we have (eq_attr "alternative" ..")
3131 present on both sides, apply the distributive law since this will
3132 yield simplifications. */
3133 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
3134 && compute_alternative_mask (left, IOR)
3135 && compute_alternative_mask (right, IOR))
3137 if (GET_CODE (left) == IOR)
3139 rtx tem = left;
3140 left = right;
3141 right = tem;
3144 newexp = attr_rtx (IOR,
3145 attr_rtx (AND, left, XEXP (right, 0)),
3146 attr_rtx (AND, left, XEXP (right, 1)));
3148 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3151 /* Try with the term on both sides. */
3152 right = simplify_and_tree (right, &left, insn_code, insn_index);
3153 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3154 left = simplify_and_tree (left, &right, insn_code, insn_index);
3156 if (left == false_rtx || right == false_rtx)
3157 return false_rtx;
3158 else if (left == true_rtx)
3160 return right;
3162 else if (right == true_rtx)
3164 return left;
3166 /* See if all or all but one of the insn's alternatives are specified
3167 in this tree. Optimize if so. */
3169 else if (insn_code >= 0
3170 && (GET_CODE (left) == AND
3171 || (GET_CODE (left) == NOT
3172 && GET_CODE (XEXP (left, 0)) == EQ_ATTR
3173 && XSTR (XEXP (left, 0), 0) == alternative_name)
3174 || GET_CODE (right) == AND
3175 || (GET_CODE (right) == NOT
3176 && GET_CODE (XEXP (right, 0)) == EQ_ATTR
3177 && XSTR (XEXP (right, 0), 0) == alternative_name)))
3179 i = compute_alternative_mask (exp, AND);
3180 if (i & ~insn_alternatives[insn_code])
3181 fatal ("invalid alternative specified for pattern number %d",
3182 insn_index);
3184 /* If all alternatives are excluded, this is false. */
3185 i ^= insn_alternatives[insn_code];
3186 if (i == 0)
3187 return false_rtx;
3188 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3190 /* If just one excluded, AND a comparison with that one to the
3191 front of the tree. The others will be eliminated by
3192 optimization. We do not want to do this if the insn has one
3193 alternative and we have tested none of them! */
3194 left = make_alternative_compare (i);
3195 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3196 newexp = attr_rtx (AND, left, right);
3198 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3202 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3204 newexp = attr_rtx (AND, left, right);
3205 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3207 break;
3209 case IOR:
3210 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3211 SIMPLIFY_ALTERNATIVE (left);
3212 if (left == true_rtx)
3213 return true_rtx;
3214 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3215 SIMPLIFY_ALTERNATIVE (right);
3216 if (right == true_rtx)
3217 return true_rtx;
3219 right = simplify_or_tree (right, &left, insn_code, insn_index);
3220 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3221 left = simplify_or_tree (left, &right, insn_code, insn_index);
3223 if (right == true_rtx || left == true_rtx)
3224 return true_rtx;
3225 else if (left == false_rtx)
3227 return right;
3229 else if (right == false_rtx)
3231 return left;
3234 /* Test for simple cases where the distributive law is useful. I.e.,
3235 convert (ior (and (x) (y))
3236 (and (x) (z)))
3237 to (and (x)
3238 (ior (y) (z)))
3241 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
3242 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
3244 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
3246 left = XEXP (left, 0);
3247 right = newexp;
3248 newexp = attr_rtx (AND, left, right);
3249 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3252 /* See if all or all but one of the insn's alternatives are specified
3253 in this tree. Optimize if so. */
3255 else if (insn_code >= 0
3256 && (GET_CODE (left) == IOR
3257 || (GET_CODE (left) == EQ_ATTR
3258 && XSTR (left, 0) == alternative_name)
3259 || GET_CODE (right) == IOR
3260 || (GET_CODE (right) == EQ_ATTR
3261 && XSTR (right, 0) == alternative_name)))
3263 i = compute_alternative_mask (exp, IOR);
3264 if (i & ~insn_alternatives[insn_code])
3265 fatal ("invalid alternative specified for pattern number %d",
3266 insn_index);
3268 /* If all alternatives are included, this is true. */
3269 i ^= insn_alternatives[insn_code];
3270 if (i == 0)
3271 return true_rtx;
3272 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3274 /* If just one excluded, IOR a comparison with that one to the
3275 front of the tree. The others will be eliminated by
3276 optimization. We do not want to do this if the insn has one
3277 alternative and we have tested none of them! */
3278 left = make_alternative_compare (i);
3279 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3280 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
3282 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3286 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3288 newexp = attr_rtx (IOR, left, right);
3289 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3291 break;
3293 case NOT:
3294 if (GET_CODE (XEXP (exp, 0)) == NOT)
3296 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
3297 insn_code, insn_index);
3298 SIMPLIFY_ALTERNATIVE (left);
3299 return left;
3302 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3303 SIMPLIFY_ALTERNATIVE (left);
3304 if (GET_CODE (left) == NOT)
3305 return XEXP (left, 0);
3307 if (left == false_rtx)
3308 return true_rtx;
3309 else if (left == true_rtx)
3310 return false_rtx;
3312 /* Try to apply De`Morgan's laws. */
3313 else if (GET_CODE (left) == IOR)
3315 newexp = attr_rtx (AND,
3316 attr_rtx (NOT, XEXP (left, 0)),
3317 attr_rtx (NOT, XEXP (left, 1)));
3319 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3321 else if (GET_CODE (left) == AND)
3323 newexp = attr_rtx (IOR,
3324 attr_rtx (NOT, XEXP (left, 0)),
3325 attr_rtx (NOT, XEXP (left, 1)));
3327 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3329 else if (left != XEXP (exp, 0))
3331 newexp = attr_rtx (NOT, left);
3333 break;
3335 case EQ_ATTR:
3336 if (current_alternative_string && XSTR (exp, 0) == alternative_name)
3337 return (XSTR (exp, 1) == current_alternative_string
3338 ? true_rtx : false_rtx);
3340 /* Look at the value for this insn code in the specified attribute.
3341 We normally can replace this comparison with the condition that
3342 would give this insn the values being tested for. */
3343 if (XSTR (exp, 0) != alternative_name
3344 && (attr = find_attr (XSTR (exp, 0), 0)) != NULL)
3345 for (av = attr->first_value; av; av = av->next)
3346 for (ie = av->first_insn; ie; ie = ie->next)
3347 if (ie->insn_code == insn_code)
3349 rtx x;
3350 x = evaluate_eq_attr (exp, av->value, insn_code, insn_index);
3351 x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
3352 if (attr_rtx_cost(x) < 20)
3353 return x;
3355 break;
3357 default:
3358 break;
3361 /* We have already simplified this expression. Simplifying it again
3362 won't buy anything unless we weren't given a valid insn code
3363 to process (i.e., we are canonicalizing something.). */
3364 if (insn_code != -2 /* Seems wrong: && current_alternative_string. */
3365 && ! ATTR_IND_SIMPLIFIED_P (newexp))
3366 return copy_rtx_unchanging (newexp);
3368 return newexp;
3371 /* Optimize the attribute lists by seeing if we can determine conditional
3372 values from the known values of other attributes. This will save subroutine
3373 calls during the compilation. */
3375 static void
3376 optimize_attrs (void)
3378 struct attr_desc *attr;
3379 struct attr_value *av;
3380 struct insn_ent *ie;
3381 rtx newexp;
3382 int i;
3383 struct attr_value_list
3385 struct attr_value *av;
3386 struct insn_ent *ie;
3387 struct attr_desc *attr;
3388 struct attr_value_list *next;
3390 struct attr_value_list **insn_code_values;
3391 struct attr_value_list *ivbuf;
3392 struct attr_value_list *iv;
3394 /* For each insn code, make a list of all the insn_ent's for it,
3395 for all values for all attributes. */
3397 if (num_insn_ents == 0)
3398 return;
3400 /* Make 2 extra elements, for "code" values -2 and -1. */
3401 insn_code_values
3402 = (struct attr_value_list **) xmalloc ((insn_code_number + 2)
3403 * sizeof (struct attr_value_list *));
3404 memset ((char *) insn_code_values, 0,
3405 (insn_code_number + 2) * sizeof (struct attr_value_list *));
3407 /* Offset the table address so we can index by -2 or -1. */
3408 insn_code_values += 2;
3410 iv = ivbuf = ((struct attr_value_list *)
3411 xmalloc (num_insn_ents * sizeof (struct attr_value_list)));
3413 for (i = 0; i < MAX_ATTRS_INDEX; i++)
3414 for (attr = attrs[i]; attr; attr = attr->next)
3415 for (av = attr->first_value; av; av = av->next)
3416 for (ie = av->first_insn; ie; ie = ie->next)
3418 iv->attr = attr;
3419 iv->av = av;
3420 iv->ie = ie;
3421 iv->next = insn_code_values[ie->insn_code];
3422 insn_code_values[ie->insn_code] = iv;
3423 iv++;
3426 /* Sanity check on num_insn_ents. */
3427 if (iv != ivbuf + num_insn_ents)
3428 abort ();
3430 /* Process one insn code at a time. */
3431 for (i = -2; i < insn_code_number; i++)
3433 /* Clear the ATTR_CURR_SIMPLIFIED_P flag everywhere relevant.
3434 We use it to mean "already simplified for this insn". */
3435 for (iv = insn_code_values[i]; iv; iv = iv->next)
3436 clear_struct_flag (iv->av->value);
3438 for (iv = insn_code_values[i]; iv; iv = iv->next)
3440 struct obstack *old = rtl_obstack;
3442 attr = iv->attr;
3443 av = iv->av;
3444 ie = iv->ie;
3445 if (GET_CODE (av->value) != COND)
3446 continue;
3448 rtl_obstack = temp_obstack;
3449 newexp = av->value;
3450 while (GET_CODE (newexp) == COND)
3452 rtx newexp2 = simplify_cond (newexp, ie->insn_code,
3453 ie->insn_index);
3454 if (newexp2 == newexp)
3455 break;
3456 newexp = newexp2;
3459 rtl_obstack = old;
3460 if (newexp != av->value)
3462 newexp = attr_copy_rtx (newexp);
3463 remove_insn_ent (av, ie);
3464 av = get_attr_value (newexp, attr, ie->insn_code);
3465 iv->av = av;
3466 insert_insn_ent (av, ie);
3471 free (ivbuf);
3472 free (insn_code_values - 2);
3475 /* If EXP is a suitable expression, reorganize it by constructing an
3476 equivalent expression that is a COND with the tests being all combinations
3477 of attribute values and the values being simple constants. */
3479 static rtx
3480 simplify_by_exploding (rtx exp)
3482 rtx list = 0, link, condexp, defval = NULL_RTX;
3483 struct dimension *space;
3484 rtx *condtest, *condval;
3485 int i, j, total, ndim = 0;
3486 int most_tests, num_marks, new_marks;
3487 rtx ret;
3489 /* Locate all the EQ_ATTR expressions. */
3490 if (! find_and_mark_used_attributes (exp, &list, &ndim) || ndim == 0)
3492 unmark_used_attributes (list, 0, 0);
3493 return exp;
3496 /* Create an attribute space from the list of used attributes. For each
3497 dimension in the attribute space, record the attribute, list of values
3498 used, and number of values used. Add members to the list of values to
3499 cover the domain of the attribute. This makes the expanded COND form
3500 order independent. */
3502 space = (struct dimension *) xmalloc (ndim * sizeof (struct dimension));
3504 total = 1;
3505 for (ndim = 0; list; ndim++)
3507 /* Pull the first attribute value from the list and record that
3508 attribute as another dimension in the attribute space. */
3509 const char *name = XSTR (XEXP (list, 0), 0);
3510 rtx *prev;
3512 if ((space[ndim].attr = find_attr (name, 0)) == 0
3513 || space[ndim].attr->is_numeric)
3515 unmark_used_attributes (list, space, ndim);
3516 return exp;
3519 /* Add all remaining attribute values that refer to this attribute. */
3520 space[ndim].num_values = 0;
3521 space[ndim].values = 0;
3522 prev = &list;
3523 for (link = list; link; link = *prev)
3524 if (! strcmp (XSTR (XEXP (link, 0), 0), name))
3526 space[ndim].num_values++;
3527 *prev = XEXP (link, 1);
3528 XEXP (link, 1) = space[ndim].values;
3529 space[ndim].values = link;
3531 else
3532 prev = &XEXP (link, 1);
3534 /* Add sufficient members to the list of values to make the list
3535 mutually exclusive and record the total size of the attribute
3536 space. */
3537 total *= add_values_to_cover (&space[ndim]);
3540 /* Sort the attribute space so that the attributes go from non-constant
3541 to constant and from most values to least values. */
3542 for (i = 0; i < ndim; i++)
3543 for (j = ndim - 1; j > i; j--)
3544 if ((space[j-1].attr->is_const && !space[j].attr->is_const)
3545 || space[j-1].num_values < space[j].num_values)
3547 struct dimension tmp;
3548 tmp = space[j];
3549 space[j] = space[j - 1];
3550 space[j - 1] = tmp;
3553 /* Establish the initial current value. */
3554 for (i = 0; i < ndim; i++)
3555 space[i].current_value = space[i].values;
3557 condtest = (rtx *) xmalloc (total * sizeof (rtx));
3558 condval = (rtx *) xmalloc (total * sizeof (rtx));
3560 /* Expand the tests and values by iterating over all values in the
3561 attribute space. */
3562 for (i = 0;; i++)
3564 condtest[i] = test_for_current_value (space, ndim);
3565 condval[i] = simplify_with_current_value (exp, space, ndim);
3566 if (! increment_current_value (space, ndim))
3567 break;
3569 if (i != total - 1)
3570 abort ();
3572 /* We are now finished with the original expression. */
3573 unmark_used_attributes (0, space, ndim);
3574 free (space);
3576 /* Find the most used constant value and make that the default. */
3577 most_tests = -1;
3578 for (i = num_marks = 0; i < total; i++)
3579 if (GET_CODE (condval[i]) == CONST_STRING
3580 && ! ATTR_EQ_ATTR_P (condval[i]))
3582 /* Mark the unmarked constant value and count how many are marked. */
3583 ATTR_EQ_ATTR_P (condval[i]) = 1;
3584 for (j = new_marks = 0; j < total; j++)
3585 if (GET_CODE (condval[j]) == CONST_STRING
3586 && ATTR_EQ_ATTR_P (condval[j]))
3587 new_marks++;
3588 if (new_marks - num_marks > most_tests)
3590 most_tests = new_marks - num_marks;
3591 defval = condval[i];
3593 num_marks = new_marks;
3595 /* Clear all the marks. */
3596 for (i = 0; i < total; i++)
3597 ATTR_EQ_ATTR_P (condval[i]) = 0;
3599 /* Give up if nothing is constant. */
3600 if (num_marks == 0)
3601 ret = exp;
3603 /* If all values are the default, use that. */
3604 else if (total == most_tests)
3605 ret = defval;
3607 /* Make a COND with the most common constant value the default. (A more
3608 complex method where tests with the same value were combined didn't
3609 seem to improve things.) */
3610 else
3612 condexp = rtx_alloc (COND);
3613 XVEC (condexp, 0) = rtvec_alloc ((total - most_tests) * 2);
3614 XEXP (condexp, 1) = defval;
3615 for (i = j = 0; i < total; i++)
3616 if (condval[i] != defval)
3618 XVECEXP (condexp, 0, 2 * j) = condtest[i];
3619 XVECEXP (condexp, 0, 2 * j + 1) = condval[i];
3620 j++;
3622 ret = condexp;
3624 free (condtest);
3625 free (condval);
3626 return ret;
3629 /* Set the ATTR_EQ_ATTR_P flag for all EQ_ATTR expressions in EXP and
3630 verify that EXP can be simplified to a constant term if all the EQ_ATTR
3631 tests have known value. */
3633 static int
3634 find_and_mark_used_attributes (rtx exp, rtx *terms, int *nterms)
3636 int i;
3638 switch (GET_CODE (exp))
3640 case EQ_ATTR:
3641 if (! ATTR_EQ_ATTR_P (exp))
3643 rtx link = rtx_alloc (EXPR_LIST);
3644 XEXP (link, 0) = exp;
3645 XEXP (link, 1) = *terms;
3646 *terms = link;
3647 *nterms += 1;
3648 ATTR_EQ_ATTR_P (exp) = 1;
3650 return 1;
3652 case CONST_STRING:
3653 case CONST_INT:
3654 return 1;
3656 case IF_THEN_ELSE:
3657 if (! find_and_mark_used_attributes (XEXP (exp, 2), terms, nterms))
3658 return 0;
3659 case IOR:
3660 case AND:
3661 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3662 return 0;
3663 case NOT:
3664 if (! find_and_mark_used_attributes (XEXP (exp, 0), terms, nterms))
3665 return 0;
3666 return 1;
3668 case COND:
3669 for (i = 0; i < XVECLEN (exp, 0); i++)
3670 if (! find_and_mark_used_attributes (XVECEXP (exp, 0, i), terms, nterms))
3671 return 0;
3672 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3673 return 0;
3674 return 1;
3676 default:
3677 return 0;
3681 /* Clear the ATTR_EQ_ATTR_P flag in all EQ_ATTR expressions on LIST and
3682 in the values of the NDIM-dimensional attribute space SPACE. */
3684 static void
3685 unmark_used_attributes (rtx list, struct dimension *space, int ndim)
3687 rtx link, exp;
3688 int i;
3690 for (i = 0; i < ndim; i++)
3691 unmark_used_attributes (space[i].values, 0, 0);
3693 for (link = list; link; link = XEXP (link, 1))
3695 exp = XEXP (link, 0);
3696 if (GET_CODE (exp) == EQ_ATTR)
3697 ATTR_EQ_ATTR_P (exp) = 0;
3701 /* Update the attribute dimension DIM so that all values of the attribute
3702 are tested. Return the updated number of values. */
3704 static int
3705 add_values_to_cover (struct dimension *dim)
3707 struct attr_value *av;
3708 rtx exp, link, *prev;
3709 int nalt = 0;
3711 for (av = dim->attr->first_value; av; av = av->next)
3712 if (GET_CODE (av->value) == CONST_STRING)
3713 nalt++;
3715 if (nalt < dim->num_values)
3716 abort ();
3717 else if (nalt == dim->num_values)
3718 /* OK. */
3720 else if (nalt * 2 < dim->num_values * 3)
3722 /* Most all the values of the attribute are used, so add all the unused
3723 values. */
3724 prev = &dim->values;
3725 for (link = dim->values; link; link = *prev)
3726 prev = &XEXP (link, 1);
3728 for (av = dim->attr->first_value; av; av = av->next)
3729 if (GET_CODE (av->value) == CONST_STRING)
3731 exp = attr_eq (dim->attr->name, XSTR (av->value, 0));
3732 if (ATTR_EQ_ATTR_P (exp))
3733 continue;
3735 link = rtx_alloc (EXPR_LIST);
3736 XEXP (link, 0) = exp;
3737 XEXP (link, 1) = 0;
3738 *prev = link;
3739 prev = &XEXP (link, 1);
3741 dim->num_values = nalt;
3743 else
3745 rtx orexp = false_rtx;
3747 /* Very few values are used, so compute a mutually exclusive
3748 expression. (We could do this for numeric values if that becomes
3749 important.) */
3750 prev = &dim->values;
3751 for (link = dim->values; link; link = *prev)
3753 orexp = insert_right_side (IOR, orexp, XEXP (link, 0), -2, -2);
3754 prev = &XEXP (link, 1);
3756 link = rtx_alloc (EXPR_LIST);
3757 XEXP (link, 0) = attr_rtx (NOT, orexp);
3758 XEXP (link, 1) = 0;
3759 *prev = link;
3760 dim->num_values++;
3762 return dim->num_values;
3765 /* Increment the current value for the NDIM-dimensional attribute space SPACE
3766 and return FALSE if the increment overflowed. */
3768 static int
3769 increment_current_value (struct dimension *space, int ndim)
3771 int i;
3773 for (i = ndim - 1; i >= 0; i--)
3775 if ((space[i].current_value = XEXP (space[i].current_value, 1)) == 0)
3776 space[i].current_value = space[i].values;
3777 else
3778 return 1;
3780 return 0;
3783 /* Construct an expression corresponding to the current value for the
3784 NDIM-dimensional attribute space SPACE. */
3786 static rtx
3787 test_for_current_value (struct dimension *space, int ndim)
3789 int i;
3790 rtx exp = true_rtx;
3792 for (i = 0; i < ndim; i++)
3793 exp = insert_right_side (AND, exp, XEXP (space[i].current_value, 0),
3794 -2, -2);
3796 return exp;
3799 /* Given the current value of the NDIM-dimensional attribute space SPACE,
3800 set the corresponding EQ_ATTR expressions to that value and reduce
3801 the expression EXP as much as possible. On input [and output], all
3802 known EQ_ATTR expressions are set to FALSE. */
3804 static rtx
3805 simplify_with_current_value (rtx exp, struct dimension *space, int ndim)
3807 int i;
3808 rtx x;
3810 /* Mark each current value as TRUE. */
3811 for (i = 0; i < ndim; i++)
3813 x = XEXP (space[i].current_value, 0);
3814 if (GET_CODE (x) == EQ_ATTR)
3815 ATTR_EQ_ATTR_P (x) = 0;
3818 exp = simplify_with_current_value_aux (exp);
3820 /* Change each current value back to FALSE. */
3821 for (i = 0; i < ndim; i++)
3823 x = XEXP (space[i].current_value, 0);
3824 if (GET_CODE (x) == EQ_ATTR)
3825 ATTR_EQ_ATTR_P (x) = 1;
3828 return exp;
3831 /* Reduce the expression EXP based on the ATTR_EQ_ATTR_P settings of
3832 all EQ_ATTR expressions. */
3834 static rtx
3835 simplify_with_current_value_aux (rtx exp)
3837 int i;
3838 rtx cond;
3840 switch (GET_CODE (exp))
3842 case EQ_ATTR:
3843 if (ATTR_EQ_ATTR_P (exp))
3844 return false_rtx;
3845 else
3846 return true_rtx;
3847 case CONST_STRING:
3848 case CONST_INT:
3849 return exp;
3851 case IF_THEN_ELSE:
3852 cond = simplify_with_current_value_aux (XEXP (exp, 0));
3853 if (cond == true_rtx)
3854 return simplify_with_current_value_aux (XEXP (exp, 1));
3855 else if (cond == false_rtx)
3856 return simplify_with_current_value_aux (XEXP (exp, 2));
3857 else
3858 return attr_rtx (IF_THEN_ELSE, cond,
3859 simplify_with_current_value_aux (XEXP (exp, 1)),
3860 simplify_with_current_value_aux (XEXP (exp, 2)));
3862 case IOR:
3863 cond = simplify_with_current_value_aux (XEXP (exp, 1));
3864 if (cond == true_rtx)
3865 return cond;
3866 else if (cond == false_rtx)
3867 return simplify_with_current_value_aux (XEXP (exp, 0));
3868 else
3869 return attr_rtx (IOR, cond,
3870 simplify_with_current_value_aux (XEXP (exp, 0)));
3872 case AND:
3873 cond = simplify_with_current_value_aux (XEXP (exp, 1));
3874 if (cond == true_rtx)
3875 return simplify_with_current_value_aux (XEXP (exp, 0));
3876 else if (cond == false_rtx)
3877 return cond;
3878 else
3879 return attr_rtx (AND, cond,
3880 simplify_with_current_value_aux (XEXP (exp, 0)));
3882 case NOT:
3883 cond = simplify_with_current_value_aux (XEXP (exp, 0));
3884 if (cond == true_rtx)
3885 return false_rtx;
3886 else if (cond == false_rtx)
3887 return true_rtx;
3888 else
3889 return attr_rtx (NOT, cond);
3891 case COND:
3892 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3894 cond = simplify_with_current_value_aux (XVECEXP (exp, 0, i));
3895 if (cond == true_rtx)
3896 return simplify_with_current_value_aux (XVECEXP (exp, 0, i + 1));
3897 else if (cond == false_rtx)
3898 continue;
3899 else
3900 abort (); /* With all EQ_ATTR's of known value, a case should
3901 have been selected. */
3903 return simplify_with_current_value_aux (XEXP (exp, 1));
3905 default:
3906 abort ();
3910 /* Clear the ATTR_CURR_SIMPLIFIED_P flag in EXP and its subexpressions. */
3912 static void
3913 clear_struct_flag (rtx x)
3915 int i;
3916 int j;
3917 enum rtx_code code;
3918 const char *fmt;
3920 ATTR_CURR_SIMPLIFIED_P (x) = 0;
3921 if (ATTR_IND_SIMPLIFIED_P (x))
3922 return;
3924 code = GET_CODE (x);
3926 switch (code)
3928 case REG:
3929 case QUEUED:
3930 case CONST_INT:
3931 case CONST_DOUBLE:
3932 case CONST_VECTOR:
3933 case SYMBOL_REF:
3934 case CODE_LABEL:
3935 case PC:
3936 case CC0:
3937 case EQ_ATTR:
3938 case ATTR_FLAG:
3939 return;
3941 default:
3942 break;
3945 /* Compare the elements. If any pair of corresponding elements
3946 fail to match, return 0 for the whole things. */
3948 fmt = GET_RTX_FORMAT (code);
3949 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3951 switch (fmt[i])
3953 case 'V':
3954 case 'E':
3955 for (j = 0; j < XVECLEN (x, i); j++)
3956 clear_struct_flag (XVECEXP (x, i, j));
3957 break;
3959 case 'e':
3960 clear_struct_flag (XEXP (x, i));
3961 break;
3966 /* Return the number of RTX objects making up the expression X.
3967 But if we count more than MAX objects, stop counting. */
3969 static int
3970 count_sub_rtxs (rtx x, int max)
3972 int i;
3973 int j;
3974 enum rtx_code code;
3975 const char *fmt;
3976 int total = 0;
3978 code = GET_CODE (x);
3980 switch (code)
3982 case REG:
3983 case QUEUED:
3984 case CONST_INT:
3985 case CONST_DOUBLE:
3986 case CONST_VECTOR:
3987 case SYMBOL_REF:
3988 case CODE_LABEL:
3989 case PC:
3990 case CC0:
3991 case EQ_ATTR:
3992 case ATTR_FLAG:
3993 return 1;
3995 default:
3996 break;
3999 /* Compare the elements. If any pair of corresponding elements
4000 fail to match, return 0 for the whole things. */
4002 fmt = GET_RTX_FORMAT (code);
4003 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4005 if (total >= max)
4006 return total;
4008 switch (fmt[i])
4010 case 'V':
4011 case 'E':
4012 for (j = 0; j < XVECLEN (x, i); j++)
4013 total += count_sub_rtxs (XVECEXP (x, i, j), max);
4014 break;
4016 case 'e':
4017 total += count_sub_rtxs (XEXP (x, i), max);
4018 break;
4021 return total;
4025 /* Create table entries for DEFINE_ATTR. */
4027 static void
4028 gen_attr (rtx exp, int lineno)
4030 struct attr_desc *attr;
4031 struct attr_value *av;
4032 const char *name_ptr;
4033 char *p;
4035 /* Make a new attribute structure. Check for duplicate by looking at
4036 attr->default_val, since it is initialized by this routine. */
4037 attr = find_attr (XSTR (exp, 0), 1);
4038 if (attr->default_val)
4040 message_with_line (lineno, "duplicate definition for attribute %s",
4041 attr->name);
4042 message_with_line (attr->lineno, "previous definition");
4043 have_error = 1;
4044 return;
4046 attr->lineno = lineno;
4048 if (*XSTR (exp, 1) == '\0')
4049 attr->is_numeric = 1;
4050 else
4052 name_ptr = XSTR (exp, 1);
4053 while ((p = next_comma_elt (&name_ptr)) != NULL)
4055 av = (struct attr_value *) oballoc (sizeof (struct attr_value));
4056 av->value = attr_rtx (CONST_STRING, p);
4057 av->next = attr->first_value;
4058 attr->first_value = av;
4059 av->first_insn = NULL;
4060 av->num_insns = 0;
4061 av->has_asm_insn = 0;
4065 if (GET_CODE (XEXP (exp, 2)) == CONST)
4067 attr->is_const = 1;
4068 if (attr->is_numeric)
4070 message_with_line (lineno,
4071 "constant attributes may not take numeric values");
4072 have_error = 1;
4075 /* Get rid of the CONST node. It is allowed only at top-level. */
4076 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
4079 if (! strcmp (attr->name, "length") && ! attr->is_numeric)
4081 message_with_line (lineno,
4082 "`length' attribute must take numeric values");
4083 have_error = 1;
4086 /* Set up the default value. */
4087 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
4088 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
4091 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
4092 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
4093 number of alternatives as this should be checked elsewhere. */
4095 static int
4096 count_alternatives (rtx exp)
4098 int i, j, n;
4099 const char *fmt;
4101 if (GET_CODE (exp) == MATCH_OPERAND)
4102 return n_comma_elts (XSTR (exp, 2));
4104 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4105 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4106 switch (*fmt++)
4108 case 'e':
4109 case 'u':
4110 n = count_alternatives (XEXP (exp, i));
4111 if (n)
4112 return n;
4113 break;
4115 case 'E':
4116 case 'V':
4117 if (XVEC (exp, i) != NULL)
4118 for (j = 0; j < XVECLEN (exp, i); j++)
4120 n = count_alternatives (XVECEXP (exp, i, j));
4121 if (n)
4122 return n;
4126 return 0;
4129 /* Returns nonzero if the given expression contains an EQ_ATTR with the
4130 `alternative' attribute. */
4132 static int
4133 compares_alternatives_p (rtx exp)
4135 int i, j;
4136 const char *fmt;
4138 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
4139 return 1;
4141 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4142 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4143 switch (*fmt++)
4145 case 'e':
4146 case 'u':
4147 if (compares_alternatives_p (XEXP (exp, i)))
4148 return 1;
4149 break;
4151 case 'E':
4152 for (j = 0; j < XVECLEN (exp, i); j++)
4153 if (compares_alternatives_p (XVECEXP (exp, i, j)))
4154 return 1;
4155 break;
4158 return 0;
4161 /* Returns nonzero is INNER is contained in EXP. */
4163 static int
4164 contained_in_p (rtx inner, rtx exp)
4166 int i, j;
4167 const char *fmt;
4169 if (rtx_equal_p (inner, exp))
4170 return 1;
4172 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4173 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4174 switch (*fmt++)
4176 case 'e':
4177 case 'u':
4178 if (contained_in_p (inner, XEXP (exp, i)))
4179 return 1;
4180 break;
4182 case 'E':
4183 for (j = 0; j < XVECLEN (exp, i); j++)
4184 if (contained_in_p (inner, XVECEXP (exp, i, j)))
4185 return 1;
4186 break;
4189 return 0;
4192 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
4194 static void
4195 gen_insn (rtx exp, int lineno)
4197 struct insn_def *id;
4199 id = (struct insn_def *) oballoc (sizeof (struct insn_def));
4200 id->next = defs;
4201 defs = id;
4202 id->def = exp;
4203 id->lineno = lineno;
4205 switch (GET_CODE (exp))
4207 case DEFINE_INSN:
4208 id->insn_code = insn_code_number;
4209 id->insn_index = insn_index_number;
4210 id->num_alternatives = count_alternatives (exp);
4211 if (id->num_alternatives == 0)
4212 id->num_alternatives = 1;
4213 id->vec_idx = 4;
4214 break;
4216 case DEFINE_PEEPHOLE:
4217 id->insn_code = insn_code_number;
4218 id->insn_index = insn_index_number;
4219 id->num_alternatives = count_alternatives (exp);
4220 if (id->num_alternatives == 0)
4221 id->num_alternatives = 1;
4222 id->vec_idx = 3;
4223 break;
4225 case DEFINE_ASM_ATTRIBUTES:
4226 id->insn_code = -1;
4227 id->insn_index = -1;
4228 id->num_alternatives = 1;
4229 id->vec_idx = 0;
4230 got_define_asm_attributes = 1;
4231 break;
4233 default:
4234 abort ();
4238 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
4239 true or annul false is specified, and make a `struct delay_desc'. */
4241 static void
4242 gen_delay (rtx def, int lineno)
4244 struct delay_desc *delay;
4245 int i;
4247 if (XVECLEN (def, 1) % 3 != 0)
4249 message_with_line (lineno,
4250 "number of elements in DEFINE_DELAY must be multiple of three");
4251 have_error = 1;
4252 return;
4255 for (i = 0; i < XVECLEN (def, 1); i += 3)
4257 if (XVECEXP (def, 1, i + 1))
4258 have_annul_true = 1;
4259 if (XVECEXP (def, 1, i + 2))
4260 have_annul_false = 1;
4263 delay = (struct delay_desc *) oballoc (sizeof (struct delay_desc));
4264 delay->def = def;
4265 delay->num = ++num_delays;
4266 delay->next = delays;
4267 delay->lineno = lineno;
4268 delays = delay;
4271 /* Process a DEFINE_FUNCTION_UNIT.
4273 This gives information about a function unit contained in the CPU.
4274 We fill in a `struct function_unit_op' and a `struct function_unit'
4275 with information used later by `expand_unit'. */
4277 static void
4278 gen_unit (rtx def, int lineno)
4280 struct function_unit *unit;
4281 struct function_unit_op *op;
4282 const char *name = XSTR (def, 0);
4283 int multiplicity = XINT (def, 1);
4284 int simultaneity = XINT (def, 2);
4285 rtx condexp = XEXP (def, 3);
4286 int ready_cost = MAX (XINT (def, 4), 1);
4287 int issue_delay = MAX (XINT (def, 5), 1);
4289 /* See if we have already seen this function unit. If so, check that
4290 the multiplicity and simultaneity values are the same. If not, make
4291 a structure for this function unit. */
4292 for (unit = units; unit; unit = unit->next)
4293 if (! strcmp (unit->name, name))
4295 if (unit->multiplicity != multiplicity
4296 || unit->simultaneity != simultaneity)
4298 message_with_line (lineno,
4299 "differing specifications given for function unit %s",
4300 unit->name);
4301 message_with_line (unit->first_lineno, "previous definition");
4302 have_error = 1;
4303 return;
4305 break;
4308 if (unit == 0)
4310 unit = (struct function_unit *) oballoc (sizeof (struct function_unit));
4311 unit->name = name;
4312 unit->multiplicity = multiplicity;
4313 unit->simultaneity = simultaneity;
4314 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
4315 unit->num = num_units++;
4316 unit->num_opclasses = 0;
4317 unit->condexp = false_rtx;
4318 unit->ops = 0;
4319 unit->next = units;
4320 unit->first_lineno = lineno;
4321 units = unit;
4324 /* Make a new operation class structure entry and initialize it. */
4325 op = (struct function_unit_op *) oballoc (sizeof (struct function_unit_op));
4326 op->condexp = condexp;
4327 op->num = unit->num_opclasses++;
4328 op->ready = ready_cost;
4329 op->issue_delay = issue_delay;
4330 op->next = unit->ops;
4331 op->lineno = lineno;
4332 unit->ops = op;
4333 num_unit_opclasses++;
4335 /* Set our issue expression based on whether or not an optional conflict
4336 vector was specified. */
4337 if (XVEC (def, 6))
4339 /* Compute the IOR of all the specified expressions. */
4340 rtx orexp = false_rtx;
4341 int i;
4343 for (i = 0; i < XVECLEN (def, 6); i++)
4344 orexp = insert_right_side (IOR, orexp, XVECEXP (def, 6, i), -2, -2);
4346 op->conflict_exp = orexp;
4347 extend_range (&unit->issue_delay, 1, issue_delay);
4349 else
4351 op->conflict_exp = true_rtx;
4352 extend_range (&unit->issue_delay, issue_delay, issue_delay);
4355 /* Merge our conditional into that of the function unit so we can determine
4356 which insns are used by the function unit. */
4357 unit->condexp = insert_right_side (IOR, unit->condexp, op->condexp, -2, -2);
4360 /* Given a piece of RTX, print a C expression to test its truth value.
4361 We use AND and IOR both for logical and bit-wise operations, so
4362 interpret them as logical unless they are inside a comparison expression.
4363 The first bit of FLAGS will be nonzero in that case.
4365 Set the second bit of FLAGS to make references to attribute values use
4366 a cached local variable instead of calling a function. */
4368 static void
4369 write_test_expr (rtx exp, int flags)
4371 int comparison_operator = 0;
4372 RTX_CODE code;
4373 struct attr_desc *attr;
4375 /* In order not to worry about operator precedence, surround our part of
4376 the expression with parentheses. */
4378 printf ("(");
4379 code = GET_CODE (exp);
4380 switch (code)
4382 /* Binary operators. */
4383 case EQ: case NE:
4384 case GE: case GT: case GEU: case GTU:
4385 case LE: case LT: case LEU: case LTU:
4386 comparison_operator = 1;
4388 case PLUS: case MINUS: case MULT: case DIV: case MOD:
4389 case AND: case IOR: case XOR:
4390 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4391 write_test_expr (XEXP (exp, 0), flags | comparison_operator);
4392 switch (code)
4394 case EQ:
4395 printf (" == ");
4396 break;
4397 case NE:
4398 printf (" != ");
4399 break;
4400 case GE:
4401 printf (" >= ");
4402 break;
4403 case GT:
4404 printf (" > ");
4405 break;
4406 case GEU:
4407 printf (" >= (unsigned) ");
4408 break;
4409 case GTU:
4410 printf (" > (unsigned) ");
4411 break;
4412 case LE:
4413 printf (" <= ");
4414 break;
4415 case LT:
4416 printf (" < ");
4417 break;
4418 case LEU:
4419 printf (" <= (unsigned) ");
4420 break;
4421 case LTU:
4422 printf (" < (unsigned) ");
4423 break;
4424 case PLUS:
4425 printf (" + ");
4426 break;
4427 case MINUS:
4428 printf (" - ");
4429 break;
4430 case MULT:
4431 printf (" * ");
4432 break;
4433 case DIV:
4434 printf (" / ");
4435 break;
4436 case MOD:
4437 printf (" %% ");
4438 break;
4439 case AND:
4440 if (flags & 1)
4441 printf (" & ");
4442 else
4443 printf (" && ");
4444 break;
4445 case IOR:
4446 if (flags & 1)
4447 printf (" | ");
4448 else
4449 printf (" || ");
4450 break;
4451 case XOR:
4452 printf (" ^ ");
4453 break;
4454 case ASHIFT:
4455 printf (" << ");
4456 break;
4457 case LSHIFTRT:
4458 case ASHIFTRT:
4459 printf (" >> ");
4460 break;
4461 default:
4462 abort ();
4465 write_test_expr (XEXP (exp, 1), flags | comparison_operator);
4466 break;
4468 case NOT:
4469 /* Special-case (not (eq_attrq "alternative" "x")) */
4470 if (! (flags & 1) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
4471 && XSTR (XEXP (exp, 0), 0) == alternative_name)
4473 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
4474 break;
4477 /* Otherwise, fall through to normal unary operator. */
4479 /* Unary operators. */
4480 case ABS: case NEG:
4481 switch (code)
4483 case NOT:
4484 if (flags & 1)
4485 printf ("~ ");
4486 else
4487 printf ("! ");
4488 break;
4489 case ABS:
4490 printf ("abs ");
4491 break;
4492 case NEG:
4493 printf ("-");
4494 break;
4495 default:
4496 abort ();
4499 write_test_expr (XEXP (exp, 0), flags);
4500 break;
4502 /* Comparison test of an attribute with a value. Most of these will
4503 have been removed by optimization. Handle "alternative"
4504 specially and give error if EQ_ATTR present inside a comparison. */
4505 case EQ_ATTR:
4506 if (flags & 1)
4507 fatal ("EQ_ATTR not valid inside comparison");
4509 if (XSTR (exp, 0) == alternative_name)
4511 printf ("which_alternative == %s", XSTR (exp, 1));
4512 break;
4515 attr = find_attr (XSTR (exp, 0), 0);
4516 if (! attr)
4517 abort ();
4519 /* Now is the time to expand the value of a constant attribute. */
4520 if (attr->is_const)
4522 write_test_expr (evaluate_eq_attr (exp, attr->default_val->value,
4523 -2, -2),
4524 flags);
4526 else
4528 if (flags & 2)
4529 printf ("attr_%s", attr->name);
4530 else
4531 printf ("get_attr_%s (insn)", attr->name);
4532 printf (" == ");
4533 write_attr_valueq (attr, XSTR (exp, 1));
4535 break;
4537 /* Comparison test of flags for define_delays. */
4538 case ATTR_FLAG:
4539 if (flags & 1)
4540 fatal ("ATTR_FLAG not valid inside comparison");
4541 printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
4542 break;
4544 /* See if an operand matches a predicate. */
4545 case MATCH_OPERAND:
4546 /* If only a mode is given, just ensure the mode matches the operand.
4547 If neither a mode nor predicate is given, error. */
4548 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
4550 if (GET_MODE (exp) == VOIDmode)
4551 fatal ("null MATCH_OPERAND specified as test");
4552 else
4553 printf ("GET_MODE (operands[%d]) == %smode",
4554 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4556 else
4557 printf ("%s (operands[%d], %smode)",
4558 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4559 break;
4561 case MATCH_INSN:
4562 printf ("%s (insn)", XSTR (exp, 0));
4563 break;
4565 /* Constant integer. */
4566 case CONST_INT:
4567 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
4568 break;
4570 /* A random C expression. */
4571 case SYMBOL_REF:
4572 printf ("%s", XSTR (exp, 0));
4573 break;
4575 /* The address of the branch target. */
4576 case MATCH_DUP:
4577 printf ("INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
4578 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
4579 break;
4581 case PC:
4582 /* The address of the current insn. We implement this actually as the
4583 address of the current insn for backward branches, but the last
4584 address of the next insn for forward branches, and both with
4585 adjustments that account for the worst-case possible stretching of
4586 intervening alignments between this insn and its destination. */
4587 printf ("insn_current_reference_address (insn)");
4588 break;
4590 case CONST_STRING:
4591 printf ("%s", XSTR (exp, 0));
4592 break;
4594 case IF_THEN_ELSE:
4595 write_test_expr (XEXP (exp, 0), flags & 2);
4596 printf (" ? ");
4597 write_test_expr (XEXP (exp, 1), flags | 1);
4598 printf (" : ");
4599 write_test_expr (XEXP (exp, 2), flags | 1);
4600 break;
4602 default:
4603 fatal ("bad RTX code `%s' in attribute calculation\n",
4604 GET_RTX_NAME (code));
4607 printf (")");
4610 /* Given an attribute value, return the maximum CONST_STRING argument
4611 encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
4613 static int
4614 max_attr_value (rtx exp, int *unknownp)
4616 int current_max;
4617 int i, n;
4619 switch (GET_CODE (exp))
4621 case CONST_STRING:
4622 current_max = atoi (XSTR (exp, 0));
4623 break;
4625 case COND:
4626 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4627 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4629 n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4630 if (n > current_max)
4631 current_max = n;
4633 break;
4635 case IF_THEN_ELSE:
4636 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4637 n = max_attr_value (XEXP (exp, 2), unknownp);
4638 if (n > current_max)
4639 current_max = n;
4640 break;
4642 default:
4643 *unknownp = 1;
4644 current_max = INT_MAX;
4645 break;
4648 return current_max;
4651 /* Given an attribute value, return the result of ORing together all
4652 CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
4653 if the numeric value is not known. */
4655 static int
4656 or_attr_value (rtx exp, int *unknownp)
4658 int current_or;
4659 int i;
4661 switch (GET_CODE (exp))
4663 case CONST_STRING:
4664 current_or = atoi (XSTR (exp, 0));
4665 break;
4667 case COND:
4668 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4669 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4670 current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4671 break;
4673 case IF_THEN_ELSE:
4674 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4675 current_or |= or_attr_value (XEXP (exp, 2), unknownp);
4676 break;
4678 default:
4679 *unknownp = 1;
4680 current_or = -1;
4681 break;
4684 return current_or;
4687 /* Scan an attribute value, possibly a conditional, and record what actions
4688 will be required to do any conditional tests in it.
4690 Specifically, set
4691 `must_extract' if we need to extract the insn operands
4692 `must_constrain' if we must compute `which_alternative'
4693 `address_used' if an address expression was used
4694 `length_used' if an (eq_attr "length" ...) was used
4697 static void
4698 walk_attr_value (rtx exp)
4700 int i, j;
4701 const char *fmt;
4702 RTX_CODE code;
4704 if (exp == NULL)
4705 return;
4707 code = GET_CODE (exp);
4708 switch (code)
4710 case SYMBOL_REF:
4711 if (! ATTR_IND_SIMPLIFIED_P (exp))
4712 /* Since this is an arbitrary expression, it can look at anything.
4713 However, constant expressions do not depend on any particular
4714 insn. */
4715 must_extract = must_constrain = 1;
4716 return;
4718 case MATCH_OPERAND:
4719 must_extract = 1;
4720 return;
4722 case EQ_ATTR:
4723 if (XSTR (exp, 0) == alternative_name)
4724 must_extract = must_constrain = 1;
4725 else if (strcmp (XSTR (exp, 0), "length") == 0)
4726 length_used = 1;
4727 return;
4729 case MATCH_DUP:
4730 must_extract = 1;
4731 address_used = 1;
4732 return;
4734 case PC:
4735 address_used = 1;
4736 return;
4738 case ATTR_FLAG:
4739 return;
4741 default:
4742 break;
4745 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
4746 switch (*fmt++)
4748 case 'e':
4749 case 'u':
4750 walk_attr_value (XEXP (exp, i));
4751 break;
4753 case 'E':
4754 if (XVEC (exp, i) != NULL)
4755 for (j = 0; j < XVECLEN (exp, i); j++)
4756 walk_attr_value (XVECEXP (exp, i, j));
4757 break;
4761 /* Write out a function to obtain the attribute for a given INSN. */
4763 static void
4764 write_attr_get (struct attr_desc *attr)
4766 struct attr_value *av, *common_av;
4768 /* Find the most used attribute value. Handle that as the `default' of the
4769 switch we will generate. */
4770 common_av = find_most_used (attr);
4772 /* Write out start of function, then all values with explicit `case' lines,
4773 then a `default', then the value with the most uses. */
4774 if (attr->static_p)
4775 printf ("static ");
4776 if (!attr->is_numeric)
4777 printf ("enum attr_%s\n", attr->name);
4778 else if (attr->unsigned_p)
4779 printf ("unsigned int\n");
4780 else
4781 printf ("int\n");
4783 /* If the attribute name starts with a star, the remainder is the name of
4784 the subroutine to use, instead of `get_attr_...'. */
4785 if (attr->name[0] == '*')
4786 printf ("%s (rtx insn ATTRIBUTE_UNUSED)\n", &attr->name[1]);
4787 else if (attr->is_const == 0)
4788 printf ("get_attr_%s (rtx insn ATTRIBUTE_UNUSED)\n", attr->name);
4789 else
4791 printf ("get_attr_%s (void)\n", attr->name);
4792 printf ("{\n");
4794 for (av = attr->first_value; av; av = av->next)
4795 if (av->num_insns != 0)
4796 write_attr_set (attr, 2, av->value, "return", ";",
4797 true_rtx, av->first_insn->insn_code,
4798 av->first_insn->insn_index);
4800 printf ("}\n\n");
4801 return;
4804 printf ("{\n");
4806 if (GET_CODE (common_av->value) == FFS)
4808 rtx p = XEXP (common_av->value, 0);
4810 /* No need to emit code to abort if the insn is unrecognized; the
4811 other get_attr_foo functions will do that when we call them. */
4813 write_toplevel_expr (p);
4815 printf ("\n if (accum && accum == (accum & -accum))\n");
4816 printf (" {\n");
4817 printf (" int i;\n");
4818 printf (" for (i = 0; accum >>= 1; ++i) continue;\n");
4819 printf (" accum = i;\n");
4820 printf (" }\n else\n");
4821 printf (" accum = ~accum;\n");
4822 printf (" return accum;\n}\n\n");
4824 else
4826 printf (" switch (recog_memoized (insn))\n");
4827 printf (" {\n");
4829 for (av = attr->first_value; av; av = av->next)
4830 if (av != common_av)
4831 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
4833 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
4834 printf (" }\n}\n\n");
4838 /* Given an AND tree of known true terms (because we are inside an `if' with
4839 that as the condition or are in an `else' clause) and an expression,
4840 replace any known true terms with TRUE. Use `simplify_and_tree' to do
4841 the bulk of the work. */
4843 static rtx
4844 eliminate_known_true (rtx known_true, rtx exp, int insn_code, int insn_index)
4846 rtx term;
4848 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
4850 if (GET_CODE (known_true) == AND)
4852 exp = eliminate_known_true (XEXP (known_true, 0), exp,
4853 insn_code, insn_index);
4854 exp = eliminate_known_true (XEXP (known_true, 1), exp,
4855 insn_code, insn_index);
4857 else
4859 term = known_true;
4860 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
4863 return exp;
4866 /* Write out a series of tests and assignment statements to perform tests and
4867 sets of an attribute value. We are passed an indentation amount and prefix
4868 and suffix strings to write around each attribute value (e.g., "return"
4869 and ";"). */
4871 static void
4872 write_attr_set (struct attr_desc *attr, int indent, rtx value,
4873 const char *prefix, const char *suffix, rtx known_true,
4874 int insn_code, int insn_index)
4876 if (GET_CODE (value) == COND)
4878 /* Assume the default value will be the default of the COND unless we
4879 find an always true expression. */
4880 rtx default_val = XEXP (value, 1);
4881 rtx our_known_true = known_true;
4882 rtx newexp;
4883 int first_if = 1;
4884 int i;
4886 for (i = 0; i < XVECLEN (value, 0); i += 2)
4888 rtx testexp;
4889 rtx inner_true;
4891 testexp = eliminate_known_true (our_known_true,
4892 XVECEXP (value, 0, i),
4893 insn_code, insn_index);
4894 newexp = attr_rtx (NOT, testexp);
4895 newexp = insert_right_side (AND, our_known_true, newexp,
4896 insn_code, insn_index);
4898 /* If the test expression is always true or if the next `known_true'
4899 expression is always false, this is the last case, so break
4900 out and let this value be the `else' case. */
4901 if (testexp == true_rtx || newexp == false_rtx)
4903 default_val = XVECEXP (value, 0, i + 1);
4904 break;
4907 /* Compute the expression to pass to our recursive call as being
4908 known true. */
4909 inner_true = insert_right_side (AND, our_known_true,
4910 testexp, insn_code, insn_index);
4912 /* If this is always false, skip it. */
4913 if (inner_true == false_rtx)
4914 continue;
4916 write_indent (indent);
4917 printf ("%sif ", first_if ? "" : "else ");
4918 first_if = 0;
4919 write_test_expr (testexp, 0);
4920 printf ("\n");
4921 write_indent (indent + 2);
4922 printf ("{\n");
4924 write_attr_set (attr, indent + 4,
4925 XVECEXP (value, 0, i + 1), prefix, suffix,
4926 inner_true, insn_code, insn_index);
4927 write_indent (indent + 2);
4928 printf ("}\n");
4929 our_known_true = newexp;
4932 if (! first_if)
4934 write_indent (indent);
4935 printf ("else\n");
4936 write_indent (indent + 2);
4937 printf ("{\n");
4940 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
4941 prefix, suffix, our_known_true, insn_code, insn_index);
4943 if (! first_if)
4945 write_indent (indent + 2);
4946 printf ("}\n");
4949 else
4951 write_indent (indent);
4952 printf ("%s ", prefix);
4953 write_attr_value (attr, value);
4954 printf ("%s\n", suffix);
4958 /* Write out the computation for one attribute value. */
4960 static void
4961 write_attr_case (struct attr_desc *attr, struct attr_value *av,
4962 int write_case_lines, const char *prefix, const char *suffix,
4963 int indent, rtx known_true)
4965 struct insn_ent *ie;
4967 if (av->num_insns == 0)
4968 return;
4970 if (av->has_asm_insn)
4972 write_indent (indent);
4973 printf ("case -1:\n");
4974 write_indent (indent + 2);
4975 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
4976 write_indent (indent + 2);
4977 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
4978 write_indent (indent + 2);
4979 printf (" fatal_insn_not_found (insn);\n");
4982 if (write_case_lines)
4984 for (ie = av->first_insn; ie; ie = ie->next)
4985 if (ie->insn_code != -1)
4987 write_indent (indent);
4988 printf ("case %d:\n", ie->insn_code);
4991 else
4993 write_indent (indent);
4994 printf ("default:\n");
4997 /* See what we have to do to output this value. */
4998 must_extract = must_constrain = address_used = 0;
4999 walk_attr_value (av->value);
5001 if (must_constrain)
5003 write_indent (indent + 2);
5004 printf ("extract_constrain_insn_cached (insn);\n");
5006 else if (must_extract)
5008 write_indent (indent + 2);
5009 printf ("extract_insn_cached (insn);\n");
5012 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
5013 known_true, av->first_insn->insn_code,
5014 av->first_insn->insn_index);
5016 if (strncmp (prefix, "return", 6))
5018 write_indent (indent + 2);
5019 printf ("break;\n");
5021 printf ("\n");
5024 /* Search for uses of non-const attributes and write code to cache them. */
5026 static int
5027 write_expr_attr_cache (rtx p, struct attr_desc *attr)
5029 const char *fmt;
5030 int i, ie, j, je;
5032 if (GET_CODE (p) == EQ_ATTR)
5034 if (XSTR (p, 0) != attr->name)
5035 return 0;
5037 if (!attr->is_numeric)
5038 printf (" enum attr_%s ", attr->name);
5039 else if (attr->unsigned_p)
5040 printf (" unsigned int ");
5041 else
5042 printf (" int ");
5044 printf ("attr_%s = get_attr_%s (insn);\n", attr->name, attr->name);
5045 return 1;
5048 fmt = GET_RTX_FORMAT (GET_CODE (p));
5049 ie = GET_RTX_LENGTH (GET_CODE (p));
5050 for (i = 0; i < ie; i++)
5052 switch (*fmt++)
5054 case 'e':
5055 if (write_expr_attr_cache (XEXP (p, i), attr))
5056 return 1;
5057 break;
5059 case 'E':
5060 je = XVECLEN (p, i);
5061 for (j = 0; j < je; ++j)
5062 if (write_expr_attr_cache (XVECEXP (p, i, j), attr))
5063 return 1;
5064 break;
5068 return 0;
5071 /* Evaluate an expression at top level. A front end to write_test_expr,
5072 in which we cache attribute values and break up excessively large
5073 expressions to cater to older compilers. */
5075 static void
5076 write_toplevel_expr (rtx p)
5078 struct attr_desc *attr;
5079 int i;
5081 for (i = 0; i < MAX_ATTRS_INDEX; ++i)
5082 for (attr = attrs[i]; attr; attr = attr->next)
5083 if (!attr->is_const)
5084 write_expr_attr_cache (p, attr);
5086 printf (" unsigned long accum = 0;\n\n");
5088 while (GET_CODE (p) == IOR)
5090 rtx e;
5091 if (GET_CODE (XEXP (p, 0)) == IOR)
5092 e = XEXP (p, 1), p = XEXP (p, 0);
5093 else
5094 e = XEXP (p, 0), p = XEXP (p, 1);
5096 printf (" accum |= ");
5097 write_test_expr (e, 3);
5098 printf (";\n");
5100 printf (" accum |= ");
5101 write_test_expr (p, 3);
5102 printf (";\n");
5105 /* Utilities to write names in various forms. */
5107 static void
5108 write_unit_name (const char *prefix, int num, const char *suffix)
5110 struct function_unit *unit;
5112 for (unit = units; unit; unit = unit->next)
5113 if (unit->num == num)
5115 printf ("%s%s%s", prefix, unit->name, suffix);
5116 return;
5119 printf ("%s<unknown>%s", prefix, suffix);
5122 static void
5123 write_attr_valueq (struct attr_desc *attr, const char *s)
5125 if (attr->is_numeric)
5127 int num = atoi (s);
5129 printf ("%d", num);
5131 /* Make the blockage range values and function units used values easier
5132 to read. */
5133 if (attr->func_units_p)
5135 if (num == -1)
5136 printf (" /* units: none */");
5137 else if (num >= 0)
5138 write_unit_name (" /* units: ", num, " */");
5139 else
5141 int i;
5142 const char *sep = " /* units: ";
5143 for (i = 0, num = ~num; num; i++, num >>= 1)
5144 if (num & 1)
5146 write_unit_name (sep, i, (num == 1) ? " */" : "");
5147 sep = ", ";
5152 else if (attr->blockage_p)
5153 printf (" /* min %d, max %d */", num >> (HOST_BITS_PER_INT / 2),
5154 num & ((1 << (HOST_BITS_PER_INT / 2)) - 1));
5156 else if (num > 9 || num < 0)
5157 printf (" /* 0x%x */", num);
5159 else
5161 write_upcase (attr->name);
5162 printf ("_");
5163 write_upcase (s);
5167 static void
5168 write_attr_value (struct attr_desc *attr, rtx value)
5170 int op;
5172 switch (GET_CODE (value))
5174 case CONST_STRING:
5175 write_attr_valueq (attr, XSTR (value, 0));
5176 break;
5178 case CONST_INT:
5179 printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
5180 break;
5182 case SYMBOL_REF:
5183 fputs (XSTR (value, 0), stdout);
5184 break;
5186 case ATTR:
5188 struct attr_desc *attr2 = find_attr (XSTR (value, 0), 0);
5189 printf ("get_attr_%s (%s)", attr2->name,
5190 (attr2->is_const ? "" : "insn"));
5192 break;
5194 case PLUS:
5195 op = '+';
5196 goto do_operator;
5197 case MINUS:
5198 op = '-';
5199 goto do_operator;
5200 case MULT:
5201 op = '*';
5202 goto do_operator;
5203 case DIV:
5204 op = '/';
5205 goto do_operator;
5206 case MOD:
5207 op = '%';
5208 goto do_operator;
5210 do_operator:
5211 write_attr_value (attr, XEXP (value, 0));
5212 putchar (' ');
5213 putchar (op);
5214 putchar (' ');
5215 write_attr_value (attr, XEXP (value, 1));
5216 break;
5218 default:
5219 abort ();
5223 static void
5224 write_upcase (const char *str)
5226 while (*str)
5228 /* The argument of TOUPPER should not have side effects. */
5229 putchar (TOUPPER(*str));
5230 str++;
5234 static void
5235 write_indent (int indent)
5237 for (; indent > 8; indent -= 8)
5238 printf ("\t");
5240 for (; indent; indent--)
5241 printf (" ");
5244 /* Write a subroutine that is given an insn that requires a delay slot, a
5245 delay slot ordinal, and a candidate insn. It returns nonzero if the
5246 candidate can be placed in the specified delay slot of the insn.
5248 We can write as many as three subroutines. `eligible_for_delay'
5249 handles normal delay slots, `eligible_for_annul_true' indicates that
5250 the specified insn can be annulled if the branch is true, and likewise
5251 for `eligible_for_annul_false'.
5253 KIND is a string distinguishing these three cases ("delay", "annul_true",
5254 or "annul_false"). */
5256 static void
5257 write_eligible_delay (const char *kind)
5259 struct delay_desc *delay;
5260 int max_slots;
5261 char str[50];
5262 struct attr_desc *attr;
5263 struct attr_value *av, *common_av;
5264 int i;
5266 /* Compute the maximum number of delay slots required. We use the delay
5267 ordinal times this number plus one, plus the slot number as an index into
5268 the appropriate predicate to test. */
5270 for (delay = delays, max_slots = 0; delay; delay = delay->next)
5271 if (XVECLEN (delay->def, 1) / 3 > max_slots)
5272 max_slots = XVECLEN (delay->def, 1) / 3;
5274 /* Write function prelude. */
5276 printf ("int\n");
5277 printf ("eligible_for_%s (rtx delay_insn ATTRIBUTE_UNUSED, int slot, rtx candidate_insn, int flags ATTRIBUTE_UNUSED)\n",
5278 kind);
5279 printf ("{\n");
5280 printf (" rtx insn;\n");
5281 printf ("\n");
5282 printf (" if (slot >= %d)\n", max_slots);
5283 printf (" abort ();\n");
5284 printf ("\n");
5286 /* If more than one delay type, find out which type the delay insn is. */
5288 if (num_delays > 1)
5290 attr = find_attr ("*delay_type", 0);
5291 if (! attr)
5292 abort ();
5293 common_av = find_most_used (attr);
5295 printf (" insn = delay_insn;\n");
5296 printf (" switch (recog_memoized (insn))\n");
5297 printf (" {\n");
5299 sprintf (str, " * %d;\n break;", max_slots);
5300 for (av = attr->first_value; av; av = av->next)
5301 if (av != common_av)
5302 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
5304 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
5305 printf (" }\n\n");
5307 /* Ensure matched. Otherwise, shouldn't have been called. */
5308 printf (" if (slot < %d)\n", max_slots);
5309 printf (" abort ();\n\n");
5312 /* If just one type of delay slot, write simple switch. */
5313 if (num_delays == 1 && max_slots == 1)
5315 printf (" insn = candidate_insn;\n");
5316 printf (" switch (recog_memoized (insn))\n");
5317 printf (" {\n");
5319 attr = find_attr ("*delay_1_0", 0);
5320 if (! attr)
5321 abort ();
5322 common_av = find_most_used (attr);
5324 for (av = attr->first_value; av; av = av->next)
5325 if (av != common_av)
5326 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
5328 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
5329 printf (" }\n");
5332 else
5334 /* Write a nested CASE. The first indicates which condition we need to
5335 test, and the inner CASE tests the condition. */
5336 printf (" insn = candidate_insn;\n");
5337 printf (" switch (slot)\n");
5338 printf (" {\n");
5340 for (delay = delays; delay; delay = delay->next)
5341 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
5343 printf (" case %d:\n",
5344 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
5345 printf (" switch (recog_memoized (insn))\n");
5346 printf ("\t{\n");
5348 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
5349 attr = find_attr (str, 0);
5350 if (! attr)
5351 abort ();
5352 common_av = find_most_used (attr);
5354 for (av = attr->first_value; av; av = av->next)
5355 if (av != common_av)
5356 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
5358 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
5359 printf (" }\n");
5362 printf (" default:\n");
5363 printf (" abort ();\n");
5364 printf (" }\n");
5367 printf ("}\n\n");
5370 /* Write routines to compute conflict cost for function units. Then write a
5371 table describing the available function units. */
5373 static void
5374 write_function_unit_info (void)
5376 struct function_unit *unit;
5377 int i;
5379 /* Write out conflict routines for function units. Don't bother writing
5380 one if there is only one issue delay value. */
5382 for (unit = units; unit; unit = unit->next)
5384 if (unit->needs_blockage_function)
5385 write_complex_function (unit, "blockage", "block");
5387 /* If the minimum and maximum conflict costs are the same, there
5388 is only one value, so we don't need a function. */
5389 if (! unit->needs_conflict_function)
5391 unit->default_cost = make_numeric_value (unit->issue_delay.max);
5392 continue;
5395 /* The function first computes the case from the candidate insn. */
5396 unit->default_cost = make_numeric_value (0);
5397 write_complex_function (unit, "conflict_cost", "cost");
5400 /* Now that all functions have been written, write the table describing
5401 the function units. The name is included for documentation purposes
5402 only. */
5404 printf ("const struct function_unit_desc function_units[] = {\n");
5406 /* Write out the descriptions in numeric order, but don't force that order
5407 on the list. Doing so increases the runtime of genattrtab.c. */
5408 for (i = 0; i < num_units; i++)
5410 for (unit = units; unit; unit = unit->next)
5411 if (unit->num == i)
5412 break;
5414 printf (" {\"%s\", %d, %d, %d, %s, %d, %s_unit_ready_cost, ",
5415 unit->name, 1 << unit->num, unit->multiplicity,
5416 unit->simultaneity, XSTR (unit->default_cost, 0),
5417 unit->issue_delay.max, unit->name);
5419 if (unit->needs_conflict_function)
5420 printf ("%s_unit_conflict_cost, ", unit->name);
5421 else
5422 printf ("0, ");
5424 printf ("%d, ", unit->max_blockage);
5426 if (unit->needs_range_function)
5427 printf ("%s_unit_blockage_range, ", unit->name);
5428 else
5429 printf ("0, ");
5431 if (unit->needs_blockage_function)
5432 printf ("%s_unit_blockage", unit->name);
5433 else
5434 printf ("0");
5436 printf ("}, \n");
5439 if (num_units == 0)
5440 printf ("{\"dummy\", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* a dummy element */");
5441 printf ("};\n\n");
5444 static void
5445 write_complex_function (struct function_unit *unit,
5446 const char *name,
5447 const char *connection)
5449 struct attr_desc *case_attr, *attr;
5450 struct attr_value *av, *common_av;
5451 rtx value;
5452 char str[256];
5453 int using_case;
5454 int i;
5456 printf ("static int\n");
5457 printf ("%s_unit_%s (rtx executing_insn, rtx candidate_insn)\n",
5458 unit->name, name);
5459 printf ("{\n");
5460 printf (" rtx insn;\n");
5461 printf (" int casenum;\n\n");
5462 printf (" insn = executing_insn;\n");
5463 printf (" switch (recog_memoized (insn))\n");
5464 printf (" {\n");
5466 /* Write the `switch' statement to get the case value. */
5467 if (strlen (unit->name) + sizeof "*_cases" > 256)
5468 abort ();
5469 sprintf (str, "*%s_cases", unit->name);
5470 case_attr = find_attr (str, 0);
5471 if (! case_attr)
5472 abort ();
5473 common_av = find_most_used (case_attr);
5475 for (av = case_attr->first_value; av; av = av->next)
5476 if (av != common_av)
5477 write_attr_case (case_attr, av, 1,
5478 "casenum =", ";", 4, unit->condexp);
5480 write_attr_case (case_attr, common_av, 0,
5481 "casenum =", ";", 4, unit->condexp);
5482 printf (" }\n\n");
5484 /* Now write an outer switch statement on each case. Then write
5485 the tests on the executing function within each. */
5486 printf (" insn = candidate_insn;\n");
5487 printf (" switch (casenum)\n");
5488 printf (" {\n");
5490 for (i = 0; i < unit->num_opclasses; i++)
5492 /* Ensure using this case. */
5493 using_case = 0;
5494 for (av = case_attr->first_value; av; av = av->next)
5495 if (av->num_insns
5496 && contained_in_p (make_numeric_value (i), av->value))
5497 using_case = 1;
5499 if (! using_case)
5500 continue;
5502 printf (" case %d:\n", i);
5503 sprintf (str, "*%s_%s_%d", unit->name, connection, i);
5504 attr = find_attr (str, 0);
5505 if (! attr)
5506 abort ();
5508 /* If single value, just write it. */
5509 value = find_single_value (attr);
5510 if (value)
5511 write_attr_set (attr, 6, value, "return", ";\n", true_rtx, -2, -2);
5512 else
5514 common_av = find_most_used (attr);
5515 printf (" switch (recog_memoized (insn))\n");
5516 printf ("\t{\n");
5518 for (av = attr->first_value; av; av = av->next)
5519 if (av != common_av)
5520 write_attr_case (attr, av, 1,
5521 "return", ";", 8, unit->condexp);
5523 write_attr_case (attr, common_av, 0,
5524 "return", ";", 8, unit->condexp);
5525 printf (" }\n\n");
5529 /* This default case should not be needed, but gcc's analysis is not
5530 good enough to realize that the default case is not needed for the
5531 second switch statement. */
5532 printf (" default:\n abort ();\n");
5533 printf (" }\n}\n\n");
5536 /* This page contains miscellaneous utility routines. */
5538 /* Given a pointer to a (char *), return a malloc'ed string containing the
5539 next comma-separated element. Advance the pointer to after the string
5540 scanned, or the end-of-string. Return NULL if at end of string. */
5542 static char *
5543 next_comma_elt (const char **pstr)
5545 const char *start;
5547 start = scan_comma_elt (pstr);
5549 if (start == NULL)
5550 return NULL;
5552 return attr_string (start, *pstr - start);
5555 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
5556 is nonzero, build a new attribute, if one does not exist. */
5558 static struct attr_desc *
5559 find_attr (const char *name, int create)
5561 struct attr_desc *attr;
5562 int index;
5564 /* Before we resort to using `strcmp', see if the string address matches
5565 anywhere. In most cases, it should have been canonicalized to do so. */
5566 if (name == alternative_name)
5567 return NULL;
5569 index = name[0] & (MAX_ATTRS_INDEX - 1);
5570 for (attr = attrs[index]; attr; attr = attr->next)
5571 if (name == attr->name)
5572 return attr;
5574 /* Otherwise, do it the slow way. */
5575 for (attr = attrs[index]; attr; attr = attr->next)
5576 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
5577 return attr;
5579 if (! create)
5580 return NULL;
5582 attr = (struct attr_desc *) oballoc (sizeof (struct attr_desc));
5583 attr->name = attr_string (name, strlen (name));
5584 attr->first_value = attr->default_val = NULL;
5585 attr->is_numeric = attr->negative_ok = attr->is_const = attr->is_special = 0;
5586 attr->unsigned_p = attr->func_units_p = attr->blockage_p = attr->static_p = 0;
5587 attr->next = attrs[index];
5588 attrs[index] = attr;
5590 return attr;
5593 /* Create internal attribute with the given default value. */
5595 void
5596 make_internal_attr (const char *name, rtx value, int special)
5598 struct attr_desc *attr;
5600 attr = find_attr (name, 1);
5601 if (attr->default_val)
5602 abort ();
5604 attr->is_numeric = 1;
5605 attr->is_const = 0;
5606 attr->is_special = (special & ATTR_SPECIAL) != 0;
5607 attr->negative_ok = (special & ATTR_NEGATIVE_OK) != 0;
5608 attr->unsigned_p = (special & ATTR_UNSIGNED) != 0;
5609 attr->func_units_p = (special & ATTR_FUNC_UNITS) != 0;
5610 attr->blockage_p = (special & ATTR_BLOCKAGE) != 0;
5611 attr->static_p = (special & ATTR_STATIC) != 0;
5612 attr->default_val = get_attr_value (value, attr, -2);
5615 /* Find the most used value of an attribute. */
5617 static struct attr_value *
5618 find_most_used (struct attr_desc *attr)
5620 struct attr_value *av;
5621 struct attr_value *most_used;
5622 int nuses;
5624 most_used = NULL;
5625 nuses = -1;
5627 for (av = attr->first_value; av; av = av->next)
5628 if (av->num_insns > nuses)
5629 nuses = av->num_insns, most_used = av;
5631 return most_used;
5634 /* If an attribute only has a single value used, return it. Otherwise
5635 return NULL. */
5637 static rtx
5638 find_single_value (struct attr_desc *attr)
5640 struct attr_value *av;
5641 rtx unique_value;
5643 unique_value = NULL;
5644 for (av = attr->first_value; av; av = av->next)
5645 if (av->num_insns)
5647 if (unique_value)
5648 return NULL;
5649 else
5650 unique_value = av->value;
5653 return unique_value;
5656 /* Return (attr_value "n") */
5659 make_numeric_value (int n)
5661 static rtx int_values[20];
5662 rtx exp;
5663 char *p;
5665 if (n < 0)
5666 abort ();
5668 if (n < 20 && int_values[n])
5669 return int_values[n];
5671 p = attr_printf (MAX_DIGITS, "%d", n);
5672 exp = attr_rtx (CONST_STRING, p);
5674 if (n < 20)
5675 int_values[n] = exp;
5677 return exp;
5680 static void
5681 extend_range (struct range *range, int min, int max)
5683 if (range->min > min)
5684 range->min = min;
5685 if (range->max < max)
5686 range->max = max;
5689 static rtx
5690 copy_rtx_unchanging (rtx orig)
5692 if (ATTR_IND_SIMPLIFIED_P (orig) || ATTR_CURR_SIMPLIFIED_P (orig))
5693 return orig;
5695 ATTR_CURR_SIMPLIFIED_P (orig) = 1;
5696 return orig;
5699 /* Determine if an insn has a constant number of delay slots, i.e., the
5700 number of delay slots is not a function of the length of the insn. */
5702 static void
5703 write_const_num_delay_slots (void)
5705 struct attr_desc *attr = find_attr ("*num_delay_slots", 0);
5706 struct attr_value *av;
5707 struct insn_ent *ie;
5709 if (attr)
5711 printf ("int\nconst_num_delay_slots (rtx insn)\n");
5712 printf ("{\n");
5713 printf (" switch (recog_memoized (insn))\n");
5714 printf (" {\n");
5716 for (av = attr->first_value; av; av = av->next)
5718 length_used = 0;
5719 walk_attr_value (av->value);
5720 if (length_used)
5722 for (ie = av->first_insn; ie; ie = ie->next)
5723 if (ie->insn_code != -1)
5724 printf (" case %d:\n", ie->insn_code);
5725 printf (" return 0;\n");
5729 printf (" default:\n");
5730 printf (" return 1;\n");
5731 printf (" }\n}\n\n");
5736 main (int argc, char **argv)
5738 rtx desc;
5739 struct attr_desc *attr;
5740 struct insn_def *id;
5741 rtx tem;
5742 int i;
5744 progname = "genattrtab";
5746 if (argc <= 1)
5747 fatal ("no input file name");
5749 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
5750 return (FATAL_EXIT_CODE);
5752 obstack_init (hash_obstack);
5753 obstack_init (temp_obstack);
5755 /* Set up true and false rtx's */
5756 true_rtx = rtx_alloc (CONST_INT);
5757 XWINT (true_rtx, 0) = 1;
5758 false_rtx = rtx_alloc (CONST_INT);
5759 XWINT (false_rtx, 0) = 0;
5760 ATTR_IND_SIMPLIFIED_P (true_rtx) = ATTR_IND_SIMPLIFIED_P (false_rtx) = 1;
5761 ATTR_PERMANENT_P (true_rtx) = ATTR_PERMANENT_P (false_rtx) = 1;
5763 alternative_name = attr_string ("alternative", strlen ("alternative"));
5765 printf ("/* Generated automatically by the program `genattrtab'\n\
5766 from the machine description file `md'. */\n\n");
5768 /* Read the machine description. */
5770 initiate_automaton_gen (argc, argv);
5771 while (1)
5773 int lineno;
5775 desc = read_md_rtx (&lineno, &insn_code_number);
5776 if (desc == NULL)
5777 break;
5779 switch (GET_CODE (desc))
5781 case DEFINE_INSN:
5782 case DEFINE_PEEPHOLE:
5783 case DEFINE_ASM_ATTRIBUTES:
5784 gen_insn (desc, lineno);
5785 break;
5787 case DEFINE_ATTR:
5788 gen_attr (desc, lineno);
5789 break;
5791 case DEFINE_DELAY:
5792 gen_delay (desc, lineno);
5793 break;
5795 case DEFINE_FUNCTION_UNIT:
5796 gen_unit (desc, lineno);
5797 break;
5799 case DEFINE_CPU_UNIT:
5800 gen_cpu_unit (desc);
5801 break;
5803 case DEFINE_QUERY_CPU_UNIT:
5804 gen_query_cpu_unit (desc);
5805 break;
5807 case DEFINE_BYPASS:
5808 gen_bypass (desc);
5809 break;
5811 case EXCLUSION_SET:
5812 gen_excl_set (desc);
5813 break;
5815 case PRESENCE_SET:
5816 gen_presence_set (desc);
5817 break;
5819 case FINAL_PRESENCE_SET:
5820 gen_final_presence_set (desc);
5821 break;
5823 case ABSENCE_SET:
5824 gen_absence_set (desc);
5825 break;
5827 case FINAL_ABSENCE_SET:
5828 gen_final_absence_set (desc);
5829 break;
5831 case DEFINE_AUTOMATON:
5832 gen_automaton (desc);
5833 break;
5835 case AUTOMATA_OPTION:
5836 gen_automata_option (desc);
5837 break;
5839 case DEFINE_RESERVATION:
5840 gen_reserv (desc);
5841 break;
5843 case DEFINE_INSN_RESERVATION:
5844 gen_insn_reserv (desc);
5845 break;
5847 default:
5848 break;
5850 if (GET_CODE (desc) != DEFINE_ASM_ATTRIBUTES)
5851 insn_index_number++;
5854 if (have_error)
5855 return FATAL_EXIT_CODE;
5857 insn_code_number++;
5859 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
5860 if (! got_define_asm_attributes)
5862 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
5863 XVEC (tem, 0) = rtvec_alloc (0);
5864 gen_insn (tem, 0);
5867 /* Expand DEFINE_DELAY information into new attribute. */
5868 if (num_delays)
5869 expand_delays ();
5871 if (num_units || num_dfa_decls)
5873 /* Expand DEFINE_FUNCTION_UNIT information into new attributes. */
5874 expand_units ();
5875 /* Build DFA, output some functions and expand DFA information
5876 into new attributes. */
5877 expand_automata ();
5880 printf ("#include \"config.h\"\n");
5881 printf ("#include \"system.h\"\n");
5882 printf ("#include \"coretypes.h\"\n");
5883 printf ("#include \"tm.h\"\n");
5884 printf ("#include \"rtl.h\"\n");
5885 printf ("#include \"tm_p.h\"\n");
5886 printf ("#include \"insn-config.h\"\n");
5887 printf ("#include \"recog.h\"\n");
5888 printf ("#include \"regs.h\"\n");
5889 printf ("#include \"real.h\"\n");
5890 printf ("#include \"output.h\"\n");
5891 printf ("#include \"insn-attr.h\"\n");
5892 printf ("#include \"toplev.h\"\n");
5893 printf ("#include \"flags.h\"\n");
5894 printf ("#include \"function.h\"\n");
5895 printf ("\n");
5896 printf ("#define operands recog_data.operand\n\n");
5898 /* Make `insn_alternatives'. */
5899 insn_alternatives = (int *) oballoc (insn_code_number * sizeof (int));
5900 for (id = defs; id; id = id->next)
5901 if (id->insn_code >= 0)
5902 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
5904 /* Make `insn_n_alternatives'. */
5905 insn_n_alternatives = (int *) oballoc (insn_code_number * sizeof (int));
5906 for (id = defs; id; id = id->next)
5907 if (id->insn_code >= 0)
5908 insn_n_alternatives[id->insn_code] = id->num_alternatives;
5910 /* Prepare to write out attribute subroutines by checking everything stored
5911 away and building the attribute cases. */
5913 check_defs ();
5915 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5916 for (attr = attrs[i]; attr; attr = attr->next)
5917 attr->default_val->value
5918 = check_attr_value (attr->default_val->value, attr);
5920 if (have_error)
5921 return FATAL_EXIT_CODE;
5923 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5924 for (attr = attrs[i]; attr; attr = attr->next)
5925 fill_attr (attr);
5927 /* Construct extra attributes for `length'. */
5928 make_length_attrs ();
5930 /* Perform any possible optimizations to speed up compilation. */
5931 optimize_attrs ();
5933 /* Now write out all the `gen_attr_...' routines. Do these before the
5934 special routines (specifically before write_function_unit_info), so
5935 that they get defined before they are used. */
5937 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5938 for (attr = attrs[i]; attr; attr = attr->next)
5940 if (! attr->is_special && ! attr->is_const)
5942 int insn_alts_p;
5944 insn_alts_p
5945 = (attr->name [0] == '*'
5946 && strcmp (&attr->name [1], INSN_ALTS_FUNC_NAME) == 0);
5947 if (insn_alts_p)
5948 printf ("\n#if AUTOMATON_ALTS\n");
5949 write_attr_get (attr);
5950 if (insn_alts_p)
5951 printf ("#endif\n\n");
5955 /* Write out delay eligibility information, if DEFINE_DELAY present.
5956 (The function to compute the number of delay slots will be written
5957 below.) */
5958 if (num_delays)
5960 write_eligible_delay ("delay");
5961 if (have_annul_true)
5962 write_eligible_delay ("annul_true");
5963 if (have_annul_false)
5964 write_eligible_delay ("annul_false");
5967 if (num_units || num_dfa_decls)
5969 /* Write out information about function units. */
5970 write_function_unit_info ();
5971 /* Output code for pipeline hazards recognition based on DFA
5972 (deterministic finite state automata. */
5973 write_automata ();
5976 /* Write out constant delay slot info. */
5977 write_const_num_delay_slots ();
5979 write_length_unit_log ();
5981 fflush (stdout);
5982 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
5985 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
5986 const char *
5987 get_insn_name (int code ATTRIBUTE_UNUSED)
5989 return NULL;