Fix cut and paste error in last change
[official-gcc.git] / gcc / genattrtab.c
blob5b4d656d0b10db8359d92f59957cde2adc14aeef
1 /* Generate code from machine description to compute values of attributes.
2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 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' (RTX_UNCHANGING_P): This rtx is fully simplified
90 independent of the insn code.
91 `in_struct' (MEM_IN_STRUCT_P): This rtx is fully simplified
92 for the insn code currently being processed (see optimize_attrs).
93 `integrated' (RTX_INTEGRATED_P): This rtx is permanent and unique
94 (see attr_rtx).
95 `volatil' (MEM_VOLATILE_P): During simplify_by_exploding the value of an
96 EQ_ATTR rtx is true if !volatil and false if volatil. */
99 #include "hconfig.h"
100 #include "system.h"
101 #include "rtl.h"
102 #include "ggc.h"
103 #include "gensupport.h"
105 #ifdef HAVE_SYS_RESOURCE_H
106 # include <sys/resource.h>
107 #endif
109 /* We must include obstack.h after <sys/time.h>, to avoid lossage with
110 /usr/include/sys/stdtypes.h on Sun OS 4.x. */
111 #include "obstack.h"
112 #include "errors.h"
114 static struct obstack obstack1, obstack2;
115 struct obstack *hash_obstack = &obstack1;
116 struct obstack *temp_obstack = &obstack2;
118 #define obstack_chunk_alloc xmalloc
119 #define obstack_chunk_free free
121 /* enough space to reserve for printing out ints */
122 #define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)
124 /* Define structures used to record attributes and values. */
126 /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
127 encountered, we store all the relevant information into a
128 `struct insn_def'. This is done to allow attribute definitions to occur
129 anywhere in the file. */
131 struct insn_def
133 int insn_code; /* Instruction number. */
134 int insn_index; /* Expression numer in file, for errors. */
135 struct insn_def *next; /* Next insn in chain. */
136 rtx def; /* The DEFINE_... */
137 int num_alternatives; /* Number of alternatives. */
138 int vec_idx; /* Index of attribute vector in `def'. */
141 /* Once everything has been read in, we store in each attribute value a list
142 of insn codes that have that value. Here is the structure used for the
143 list. */
145 struct insn_ent
147 int insn_code; /* Instruction number. */
148 int insn_index; /* Index of definition in file */
149 struct insn_ent *next; /* Next in chain. */
152 /* Each value of an attribute (either constant or computed) is assigned a
153 structure which is used as the listhead of the insns that have that
154 value. */
156 struct attr_value
158 rtx value; /* Value of attribute. */
159 struct attr_value *next; /* Next attribute value in chain. */
160 struct insn_ent *first_insn; /* First insn with this value. */
161 int num_insns; /* Number of insns with this value. */
162 int has_asm_insn; /* True if this value used for `asm' insns */
165 /* Structure for each attribute. */
167 struct attr_desc
169 char *name; /* Name of attribute. */
170 struct attr_desc *next; /* Next attribute. */
171 unsigned is_numeric : 1; /* Values of this attribute are numeric. */
172 unsigned negative_ok : 1; /* Allow negative numeric values. */
173 unsigned unsigned_p : 1; /* Make the output function unsigned int. */
174 unsigned is_const : 1; /* Attribute value constant for each run. */
175 unsigned is_special : 1; /* Don't call `write_attr_set'. */
176 unsigned func_units_p : 1; /* this is the function_units attribute */
177 unsigned blockage_p : 1; /* this is the blockage range function */
178 struct attr_value *first_value; /* First value of this attribute. */
179 struct attr_value *default_val; /* Default value for this attribute. */
182 #define NULL_ATTR (struct attr_desc *) NULL
184 /* A range of values. */
186 struct range
188 int min;
189 int max;
192 /* Structure for each DEFINE_DELAY. */
194 struct delay_desc
196 rtx def; /* DEFINE_DELAY expression. */
197 struct delay_desc *next; /* Next DEFINE_DELAY. */
198 int num; /* Number of DEFINE_DELAY, starting at 1. */
201 /* Record information about each DEFINE_FUNCTION_UNIT. */
203 struct function_unit_op
205 rtx condexp; /* Expression TRUE for applicable insn. */
206 struct function_unit_op *next; /* Next operation for this function unit. */
207 int num; /* Ordinal for this operation type in unit. */
208 int ready; /* Cost until data is ready. */
209 int issue_delay; /* Cost until unit can accept another insn. */
210 rtx conflict_exp; /* Expression TRUE for insns incurring issue delay. */
211 rtx issue_exp; /* Expression computing issue delay. */
214 /* Record information about each function unit mentioned in a
215 DEFINE_FUNCTION_UNIT. */
217 struct function_unit
219 const char *name; /* Function unit name. */
220 struct function_unit *next; /* Next function unit. */
221 int num; /* Ordinal of this unit type. */
222 int multiplicity; /* Number of units of this type. */
223 int simultaneity; /* Maximum number of simultaneous insns
224 on this function unit or 0 if unlimited. */
225 rtx condexp; /* Expression TRUE for insn needing unit. */
226 int num_opclasses; /* Number of different operation types. */
227 struct function_unit_op *ops; /* Pointer to first operation type. */
228 int needs_conflict_function; /* Nonzero if a conflict function required. */
229 int needs_blockage_function; /* Nonzero if a blockage function required. */
230 int needs_range_function; /* Nonzero if blockage range function needed.*/
231 rtx default_cost; /* Conflict cost, if constant. */
232 struct range issue_delay; /* Range of issue delay values. */
233 int max_blockage; /* Maximum time an insn blocks the unit. */
236 /* Listheads of above structures. */
238 /* This one is indexed by the first character of the attribute name. */
239 #define MAX_ATTRS_INDEX 256
240 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
241 static struct insn_def *defs;
242 static struct delay_desc *delays;
243 static struct function_unit *units;
245 /* An expression where all the unknown terms are EQ_ATTR tests can be
246 rearranged into a COND provided we can enumerate all possible
247 combinations of the unknown values. The set of combinations become the
248 tests of the COND; the value of the expression given that combination is
249 computed and becomes the corresponding value. To do this, we must be
250 able to enumerate all values for each attribute used in the expression
251 (currently, we give up if we find a numeric attribute).
253 If the set of EQ_ATTR tests used in an expression tests the value of N
254 different attributes, the list of all possible combinations can be made
255 by walking the N-dimensional attribute space defined by those
256 attributes. We record each of these as a struct dimension.
258 The algorithm relies on sharing EQ_ATTR nodes: if two nodes in an
259 expression are the same, the will also have the same address. We find
260 all the EQ_ATTR nodes by marking them MEM_VOLATILE_P. This bit later
261 represents the value of an EQ_ATTR node, so once all nodes are marked,
262 they are also given an initial value of FALSE.
264 We then separate the set of EQ_ATTR nodes into dimensions for each
265 attribute and put them on the VALUES list. Terms are added as needed by
266 `add_values_to_cover' so that all possible values of the attribute are
267 tested.
269 Each dimension also has a current value. This is the node that is
270 currently considered to be TRUE. If this is one of the nodes added by
271 `add_values_to_cover', all the EQ_ATTR tests in the original expression
272 will be FALSE. Otherwise, only the CURRENT_VALUE will be true.
274 NUM_VALUES is simply the length of the VALUES list and is there for
275 convenience.
277 Once the dimensions are created, the algorithm enumerates all possible
278 values and computes the current value of the given expression. */
280 struct dimension
282 struct attr_desc *attr; /* Attribute for this dimension. */
283 rtx values; /* List of attribute values used. */
284 rtx current_value; /* Position in the list for the TRUE value. */
285 int num_values; /* Length of the values list. */
288 /* Other variables. */
290 static int insn_code_number;
291 static int insn_index_number;
292 static int got_define_asm_attributes;
293 static int must_extract;
294 static int must_constrain;
295 static int address_used;
296 static int length_used;
297 static int num_delays;
298 static int have_annul_true, have_annul_false;
299 static int num_units, num_unit_opclasses;
300 static int num_insn_ents;
302 /* Used as operand to `operate_exp': */
304 enum operator {PLUS_OP, MINUS_OP, POS_MINUS_OP, EQ_OP, OR_OP, ORX_OP, MAX_OP, MIN_OP, RANGE_OP};
306 /* Stores, for each insn code, the number of constraint alternatives. */
308 static int *insn_n_alternatives;
310 /* Stores, for each insn code, a bitmap that has bits on for each possible
311 alternative. */
313 static int *insn_alternatives;
315 /* If nonzero, assume that the `alternative' attr has this value.
316 This is the hashed, unique string for the numeral
317 whose value is chosen alternative. */
319 static const char *current_alternative_string;
321 /* Used to simplify expressions. */
323 static rtx true_rtx, false_rtx;
325 /* Used to reduce calls to `strcmp' */
327 static char *alternative_name;
329 /* Indicate that REG_DEAD notes are valid if dead_or_set_p is ever
330 called. */
332 int reload_completed = 0;
334 /* Some machines test `optimize' in macros called from rtlanal.c, so we need
335 to define it here. */
337 int optimize = 0;
339 /* Simplify an expression. Only call the routine if there is something to
340 simplify. */
341 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
342 (RTX_UNCHANGING_P (EXP) || MEM_IN_STRUCT_P (EXP) ? (EXP) \
343 : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
345 /* Simplify (eq_attr ("alternative") ...)
346 when we are working with a particular alternative. */
347 #define SIMPLIFY_ALTERNATIVE(EXP) \
348 if (current_alternative_string \
349 && GET_CODE ((EXP)) == EQ_ATTR \
350 && XSTR ((EXP), 0) == alternative_name) \
351 (EXP) = (XSTR ((EXP), 1) == current_alternative_string \
352 ? true_rtx : false_rtx);
354 /* These are referenced by rtlanal.c and hence need to be defined somewhere.
355 They won't actually be used. */
357 rtx global_rtl[GR_MAX];
358 rtx pic_offset_table_rtx;
360 static void attr_hash_add_rtx PARAMS ((int, rtx));
361 static void attr_hash_add_string PARAMS ((int, char *));
362 static rtx attr_rtx PARAMS ((enum rtx_code, ...));
363 static char *attr_printf PARAMS ((int, const char *, ...))
364 ATTRIBUTE_PRINTF_2;
365 static char *attr_string PARAMS ((const char *, int));
366 static rtx check_attr_test PARAMS ((rtx, int));
367 static rtx check_attr_value PARAMS ((rtx, struct attr_desc *));
368 static rtx convert_set_attr_alternative PARAMS ((rtx, int, int));
369 static rtx convert_set_attr PARAMS ((rtx, int, int));
370 static void check_defs PARAMS ((void));
371 #if 0
372 static rtx convert_const_symbol_ref PARAMS ((rtx, struct attr_desc *));
373 #endif
374 static rtx make_canonical PARAMS ((struct attr_desc *, rtx));
375 static struct attr_value *get_attr_value PARAMS ((rtx, struct attr_desc *, int));
376 static rtx copy_rtx_unchanging PARAMS ((rtx));
377 static rtx copy_boolean PARAMS ((rtx));
378 static void expand_delays PARAMS ((void));
379 static rtx operate_exp PARAMS ((enum operator, rtx, rtx));
380 static void expand_units PARAMS ((void));
381 static rtx simplify_knowing PARAMS ((rtx, rtx));
382 static rtx encode_units_mask PARAMS ((rtx));
383 static void fill_attr PARAMS ((struct attr_desc *));
384 /* dpx2 compiler chokes if we specify the arg types of the args. */
385 static rtx substitute_address PARAMS ((rtx, rtx (*) (rtx), rtx (*) (rtx)));
386 static void make_length_attrs PARAMS ((void));
387 static rtx identity_fn PARAMS ((rtx));
388 static rtx zero_fn PARAMS ((rtx));
389 static rtx one_fn PARAMS ((rtx));
390 static rtx max_fn PARAMS ((rtx));
391 static void write_length_unit_log PARAMS ((void));
392 static rtx simplify_cond PARAMS ((rtx, int, int));
393 #if 0
394 static rtx simplify_by_alternatives PARAMS ((rtx, int, int));
395 #endif
396 static rtx simplify_by_exploding PARAMS ((rtx));
397 static int find_and_mark_used_attributes PARAMS ((rtx, rtx *, int *));
398 static void unmark_used_attributes PARAMS ((rtx, struct dimension *, int));
399 static int add_values_to_cover PARAMS ((struct dimension *));
400 static int increment_current_value PARAMS ((struct dimension *, int));
401 static rtx test_for_current_value PARAMS ((struct dimension *, int));
402 static rtx simplify_with_current_value PARAMS ((rtx, struct dimension *, int));
403 static rtx simplify_with_current_value_aux PARAMS ((rtx));
404 static void clear_struct_flag PARAMS ((rtx));
405 static int count_sub_rtxs PARAMS ((rtx, int));
406 static void remove_insn_ent PARAMS ((struct attr_value *, struct insn_ent *));
407 static void insert_insn_ent PARAMS ((struct attr_value *, struct insn_ent *));
408 static rtx insert_right_side PARAMS ((enum rtx_code, rtx, rtx, int, int));
409 static rtx make_alternative_compare PARAMS ((int));
410 static int compute_alternative_mask PARAMS ((rtx, enum rtx_code));
411 static rtx evaluate_eq_attr PARAMS ((rtx, rtx, int, int));
412 static rtx simplify_and_tree PARAMS ((rtx, rtx *, int, int));
413 static rtx simplify_or_tree PARAMS ((rtx, rtx *, int, int));
414 static rtx simplify_test_exp PARAMS ((rtx, int, int));
415 static void optimize_attrs PARAMS ((void));
416 static void gen_attr PARAMS ((rtx));
417 static int count_alternatives PARAMS ((rtx));
418 static int compares_alternatives_p PARAMS ((rtx));
419 static int contained_in_p PARAMS ((rtx, rtx));
420 static void gen_insn PARAMS ((rtx));
421 static void gen_delay PARAMS ((rtx));
422 static void gen_unit PARAMS ((rtx));
423 static void write_test_expr PARAMS ((rtx, int));
424 static int max_attr_value PARAMS ((rtx, int*));
425 static int or_attr_value PARAMS ((rtx, int*));
426 static void walk_attr_value PARAMS ((rtx));
427 static void write_attr_get PARAMS ((struct attr_desc *));
428 static rtx eliminate_known_true PARAMS ((rtx, rtx, int, int));
429 static void write_attr_set PARAMS ((struct attr_desc *, int, rtx,
430 const char *, const char *, rtx,
431 int, int));
432 static void write_attr_case PARAMS ((struct attr_desc *, struct attr_value *,
433 int, const char *, const char *, int, rtx));
434 static void write_unit_name PARAMS ((const char *, int, const char *));
435 static void write_attr_valueq PARAMS ((struct attr_desc *, const char *));
436 static void write_attr_value PARAMS ((struct attr_desc *, rtx));
437 static void write_upcase PARAMS ((const char *));
438 static void write_indent PARAMS ((int));
439 static void write_eligible_delay PARAMS ((const char *));
440 static void write_function_unit_info PARAMS ((void));
441 static void write_complex_function PARAMS ((struct function_unit *, const char *,
442 const char *));
443 static int write_expr_attr_cache PARAMS ((rtx, struct attr_desc *));
444 static void write_toplevel_expr PARAMS ((rtx));
445 static void write_const_num_delay_slots PARAMS ((void));
446 static int n_comma_elts PARAMS ((const char *));
447 static char *next_comma_elt PARAMS ((const char **));
448 static struct attr_desc *find_attr PARAMS ((const char *, int));
449 static void make_internal_attr PARAMS ((const char *, rtx, int));
450 static struct attr_value *find_most_used PARAMS ((struct attr_desc *));
451 static rtx find_single_value PARAMS ((struct attr_desc *));
452 static rtx make_numeric_value PARAMS ((int));
453 static void extend_range PARAMS ((struct range *, int, int));
454 static rtx attr_eq PARAMS ((const char *, const char *));
455 static const char *attr_numeral PARAMS ((int));
456 static int attr_equal_p PARAMS ((rtx, rtx));
457 static rtx attr_copy_rtx PARAMS ((rtx));
459 #define oballoc(size) obstack_alloc (hash_obstack, size)
462 /* Hash table for sharing RTL and strings. */
464 /* Each hash table slot is a bucket containing a chain of these structures.
465 Strings are given negative hash codes; RTL expressions are given positive
466 hash codes. */
468 struct attr_hash
470 struct attr_hash *next; /* Next structure in the bucket. */
471 int hashcode; /* Hash code of this rtx or string. */
472 union
474 char *str; /* The string (negative hash codes) */
475 rtx rtl; /* or the RTL recorded here. */
476 } u;
479 /* Now here is the hash table. When recording an RTL, it is added to
480 the slot whose index is the hash code mod the table size. Note
481 that the hash table is used for several kinds of RTL (see attr_rtx)
482 and for strings. While all these live in the same table, they are
483 completely independent, and the hash code is computed differently
484 for each. */
486 #define RTL_HASH_SIZE 4093
487 struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
489 /* Here is how primitive or already-shared RTL's hash
490 codes are made. */
491 #define RTL_HASH(RTL) ((long) (RTL) & 0777777)
493 /* Add an entry to the hash table for RTL with hash code HASHCODE. */
495 static void
496 attr_hash_add_rtx (hashcode, rtl)
497 int hashcode;
498 rtx rtl;
500 register struct attr_hash *h;
502 h = (struct attr_hash *) obstack_alloc (hash_obstack,
503 sizeof (struct attr_hash));
504 h->hashcode = hashcode;
505 h->u.rtl = rtl;
506 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
507 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
510 /* Add an entry to the hash table for STRING with hash code HASHCODE. */
512 static void
513 attr_hash_add_string (hashcode, str)
514 int hashcode;
515 char *str;
517 register struct attr_hash *h;
519 h = (struct attr_hash *) obstack_alloc (hash_obstack,
520 sizeof (struct attr_hash));
521 h->hashcode = -hashcode;
522 h->u.str = str;
523 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
524 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
527 /* Generate an RTL expression, but avoid duplicates.
528 Set the RTX_INTEGRATED_P flag for these permanent objects.
530 In some cases we cannot uniquify; then we return an ordinary
531 impermanent rtx with RTX_INTEGRATED_P clear.
533 Args are like gen_rtx, but without the mode:
535 rtx attr_rtx (code, [element1, ..., elementn]) */
537 /*VARARGS1*/
538 static rtx
539 attr_rtx VPARAMS ((enum rtx_code code, ...))
541 #ifndef ANSI_PROTOTYPES
542 enum rtx_code code;
543 #endif
544 va_list p;
545 register int i; /* Array indices... */
546 register const char *fmt; /* Current rtx's format... */
547 register rtx rt_val = NULL_RTX;/* RTX to return to caller... */
548 int hashcode;
549 register struct attr_hash *h;
550 struct obstack *old_obstack = rtl_obstack;
552 VA_START (p, code);
554 #ifndef ANSI_PROTOTYPES
555 code = va_arg (p, enum rtx_code);
556 #endif
558 /* For each of several cases, search the hash table for an existing entry.
559 Use that entry if one is found; otherwise create a new RTL and add it
560 to the table. */
562 if (GET_RTX_CLASS (code) == '1')
564 rtx arg0 = va_arg (p, rtx);
566 /* A permanent object cannot point to impermanent ones. */
567 if (! RTX_INTEGRATED_P (arg0))
569 rt_val = rtx_alloc (code);
570 XEXP (rt_val, 0) = arg0;
571 va_end (p);
572 return rt_val;
575 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
576 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
577 if (h->hashcode == hashcode
578 && GET_CODE (h->u.rtl) == code
579 && XEXP (h->u.rtl, 0) == arg0)
580 goto found;
582 if (h == 0)
584 rtl_obstack = hash_obstack;
585 rt_val = rtx_alloc (code);
586 XEXP (rt_val, 0) = arg0;
589 else if (GET_RTX_CLASS (code) == 'c'
590 || GET_RTX_CLASS (code) == '2'
591 || GET_RTX_CLASS (code) == '<')
593 rtx arg0 = va_arg (p, rtx);
594 rtx arg1 = va_arg (p, rtx);
596 /* A permanent object cannot point to impermanent ones. */
597 if (! RTX_INTEGRATED_P (arg0) || ! RTX_INTEGRATED_P (arg1))
599 rt_val = rtx_alloc (code);
600 XEXP (rt_val, 0) = arg0;
601 XEXP (rt_val, 1) = arg1;
602 va_end (p);
603 return rt_val;
606 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
607 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
608 if (h->hashcode == hashcode
609 && GET_CODE (h->u.rtl) == code
610 && XEXP (h->u.rtl, 0) == arg0
611 && XEXP (h->u.rtl, 1) == arg1)
612 goto found;
614 if (h == 0)
616 rtl_obstack = hash_obstack;
617 rt_val = rtx_alloc (code);
618 XEXP (rt_val, 0) = arg0;
619 XEXP (rt_val, 1) = arg1;
622 else if (GET_RTX_LENGTH (code) == 1
623 && GET_RTX_FORMAT (code)[0] == 's')
625 char *arg0 = va_arg (p, char *);
627 if (code == SYMBOL_REF)
628 arg0 = attr_string (arg0, strlen (arg0));
630 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
631 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
632 if (h->hashcode == hashcode
633 && GET_CODE (h->u.rtl) == code
634 && XSTR (h->u.rtl, 0) == arg0)
635 goto found;
637 if (h == 0)
639 rtl_obstack = hash_obstack;
640 rt_val = rtx_alloc (code);
641 XSTR (rt_val, 0) = arg0;
644 else if (GET_RTX_LENGTH (code) == 2
645 && GET_RTX_FORMAT (code)[0] == 's'
646 && GET_RTX_FORMAT (code)[1] == 's')
648 char *arg0 = va_arg (p, char *);
649 char *arg1 = va_arg (p, char *);
651 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
652 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
653 if (h->hashcode == hashcode
654 && GET_CODE (h->u.rtl) == code
655 && XSTR (h->u.rtl, 0) == arg0
656 && XSTR (h->u.rtl, 1) == arg1)
657 goto found;
659 if (h == 0)
661 rtl_obstack = hash_obstack;
662 rt_val = rtx_alloc (code);
663 XSTR (rt_val, 0) = arg0;
664 XSTR (rt_val, 1) = arg1;
667 else if (code == CONST_INT)
669 HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
670 if (arg0 == 0)
672 va_end (p);
673 return false_rtx;
675 if (arg0 == 1)
677 va_end (p);
678 return true_rtx;
680 goto nohash;
682 else
684 nohash:
685 rt_val = rtx_alloc (code); /* Allocate the storage space. */
687 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
688 for (i = 0; i < GET_RTX_LENGTH (code); i++)
690 switch (*fmt++)
692 case '0': /* Unused field. */
693 break;
695 case 'i': /* An integer? */
696 XINT (rt_val, i) = va_arg (p, int);
697 break;
699 case 'w': /* A wide integer? */
700 XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
701 break;
703 case 's': /* A string? */
704 XSTR (rt_val, i) = va_arg (p, char *);
705 break;
707 case 'e': /* An expression? */
708 case 'u': /* An insn? Same except when printing. */
709 XEXP (rt_val, i) = va_arg (p, rtx);
710 break;
712 case 'E': /* An RTX vector? */
713 XVEC (rt_val, i) = va_arg (p, rtvec);
714 break;
716 default:
717 abort();
720 va_end (p);
721 return rt_val;
724 rtl_obstack = old_obstack;
725 va_end (p);
726 attr_hash_add_rtx (hashcode, rt_val);
727 RTX_INTEGRATED_P (rt_val) = 1;
728 return rt_val;
730 found:
731 va_end (p);
732 return h->u.rtl;
735 /* Create a new string printed with the printf line arguments into a space
736 of at most LEN bytes:
738 rtx attr_printf (len, format, [arg1, ..., argn]) */
740 /*VARARGS2*/
741 static char *
742 attr_printf VPARAMS ((register int len, const char *fmt, ...))
744 #ifndef ANSI_PROTOTYPES
745 register int len;
746 const char *fmt;
747 #endif
748 va_list p;
749 register char *str;
751 VA_START (p, fmt);
753 #ifndef ANSI_PROTOTYPES
754 len = va_arg (p, int);
755 fmt = va_arg (p, const char *);
756 #endif
758 /* Print the string into a temporary location. */
759 str = (char *) alloca (len);
760 vsprintf (str, fmt, p);
761 va_end (p);
763 return attr_string (str, strlen (str));
766 static rtx
767 attr_eq (name, value)
768 const char *name, *value;
770 return attr_rtx (EQ_ATTR, attr_string (name, strlen (name)),
771 attr_string (value, strlen (value)));
774 static const char *
775 attr_numeral (n)
776 int n;
778 return XSTR (make_numeric_value (n), 0);
781 /* Return a permanent (possibly shared) copy of a string STR (not assumed
782 to be null terminated) with LEN bytes. */
784 static char *
785 attr_string (str, len)
786 const char *str;
787 int len;
789 register struct attr_hash *h;
790 int hashcode;
791 int i;
792 register char *new_str;
794 /* Compute the hash code. */
795 hashcode = (len + 1) * 613 + (unsigned)str[0];
796 for (i = 1; i <= len; i += 2)
797 hashcode = ((hashcode * 613) + (unsigned)str[i]);
798 if (hashcode < 0)
799 hashcode = -hashcode;
801 /* Search the table for the string. */
802 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
803 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
804 && !strncmp (h->u.str, str, len))
805 return h->u.str; /* <-- return if found. */
807 /* Not found; create a permanent copy and add it to the hash table. */
808 new_str = (char *) obstack_alloc (hash_obstack, len + 1);
809 bcopy (str, new_str, len);
810 new_str[len] = '\0';
811 attr_hash_add_string (hashcode, new_str);
813 return new_str; /* Return the new string. */
816 /* Check two rtx's for equality of contents,
817 taking advantage of the fact that if both are hashed
818 then they can't be equal unless they are the same object. */
820 static int
821 attr_equal_p (x, y)
822 rtx x, y;
824 return (x == y || (! (RTX_INTEGRATED_P (x) && RTX_INTEGRATED_P (y))
825 && rtx_equal_p (x, y)));
828 /* Copy an attribute value expression,
829 descending to all depths, but not copying any
830 permanent hashed subexpressions. */
832 static rtx
833 attr_copy_rtx (orig)
834 register rtx orig;
836 register rtx copy;
837 register int i, j;
838 register RTX_CODE code;
839 register const char *format_ptr;
841 /* No need to copy a permanent object. */
842 if (RTX_INTEGRATED_P (orig))
843 return orig;
845 code = GET_CODE (orig);
847 switch (code)
849 case REG:
850 case QUEUED:
851 case CONST_INT:
852 case CONST_DOUBLE:
853 case SYMBOL_REF:
854 case CODE_LABEL:
855 case PC:
856 case CC0:
857 return orig;
859 default:
860 break;
863 copy = rtx_alloc (code);
864 PUT_MODE (copy, GET_MODE (orig));
865 copy->in_struct = orig->in_struct;
866 copy->volatil = orig->volatil;
867 copy->unchanging = orig->unchanging;
868 copy->integrated = orig->integrated;
870 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
872 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
874 switch (*format_ptr++)
876 case 'e':
877 XEXP (copy, i) = XEXP (orig, i);
878 if (XEXP (orig, i) != NULL)
879 XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
880 break;
882 case 'E':
883 case 'V':
884 XVEC (copy, i) = XVEC (orig, i);
885 if (XVEC (orig, i) != NULL)
887 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
888 for (j = 0; j < XVECLEN (copy, i); j++)
889 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
891 break;
893 case 'n':
894 case 'i':
895 XINT (copy, i) = XINT (orig, i);
896 break;
898 case 'w':
899 XWINT (copy, i) = XWINT (orig, i);
900 break;
902 case 's':
903 case 'S':
904 XSTR (copy, i) = XSTR (orig, i);
905 break;
907 default:
908 abort ();
911 return copy;
914 /* Given a test expression for an attribute, ensure it is validly formed.
915 IS_CONST indicates whether the expression is constant for each compiler
916 run (a constant expression may not test any particular insn).
918 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
919 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
920 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
922 Update the string address in EQ_ATTR expression to be the same used
923 in the attribute (or `alternative_name') to speed up subsequent
924 `find_attr' calls and eliminate most `strcmp' calls.
926 Return the new expression, if any. */
928 static rtx
929 check_attr_test (exp, is_const)
930 rtx exp;
931 int is_const;
933 struct attr_desc *attr;
934 struct attr_value *av;
935 const char *name_ptr, *p;
936 rtx orexp, newexp;
938 switch (GET_CODE (exp))
940 case EQ_ATTR:
941 /* Handle negation test. */
942 if (XSTR (exp, 1)[0] == '!')
943 return check_attr_test (attr_rtx (NOT,
944 attr_eq (XSTR (exp, 0),
945 &XSTR (exp, 1)[1])),
946 is_const);
948 else if (n_comma_elts (XSTR (exp, 1)) == 1)
950 attr = find_attr (XSTR (exp, 0), 0);
951 if (attr == NULL)
953 if (! strcmp (XSTR (exp, 0), "alternative"))
955 XSTR (exp, 0) = alternative_name;
956 /* This can't be simplified any further. */
957 RTX_UNCHANGING_P (exp) = 1;
958 return exp;
960 else
961 fatal ("Unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
964 if (is_const && ! attr->is_const)
965 fatal ("Constant expression uses insn attribute `%s' in EQ_ATTR",
966 XSTR (exp, 0));
968 /* Copy this just to make it permanent,
969 so expressions using it can be permanent too. */
970 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
972 /* It shouldn't be possible to simplify the value given to a
973 constant attribute, so don't expand this until it's time to
974 write the test expression. */
975 if (attr->is_const)
976 RTX_UNCHANGING_P (exp) = 1;
978 if (attr->is_numeric)
980 for (p = XSTR (exp, 1); *p; p++)
981 if (*p < '0' || *p > '9')
982 fatal ("Attribute `%s' takes only numeric values",
983 XSTR (exp, 0));
985 else
987 for (av = attr->first_value; av; av = av->next)
988 if (GET_CODE (av->value) == CONST_STRING
989 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
990 break;
992 if (av == NULL)
993 fatal ("Unknown value `%s' for `%s' attribute",
994 XSTR (exp, 1), XSTR (exp, 0));
997 else
999 /* Make an IOR tree of the possible values. */
1000 orexp = false_rtx;
1001 name_ptr = XSTR (exp, 1);
1002 while ((p = next_comma_elt (&name_ptr)) != NULL)
1004 newexp = attr_eq (XSTR (exp, 0), p);
1005 orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
1008 return check_attr_test (orexp, is_const);
1010 break;
1012 case ATTR_FLAG:
1013 break;
1015 case CONST_INT:
1016 /* Either TRUE or FALSE. */
1017 if (XWINT (exp, 0))
1018 return true_rtx;
1019 else
1020 return false_rtx;
1022 case IOR:
1023 case AND:
1024 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const);
1025 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const);
1026 break;
1028 case NOT:
1029 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const);
1030 break;
1032 case MATCH_INSN:
1033 case MATCH_OPERAND:
1034 if (is_const)
1035 fatal ("RTL operator \"%s\" not valid in constant attribute test",
1036 GET_RTX_NAME (GET_CODE (exp)));
1037 /* These cases can't be simplified. */
1038 RTX_UNCHANGING_P (exp) = 1;
1039 break;
1041 case LE: case LT: case GT: case GE:
1042 case LEU: case LTU: case GTU: case GEU:
1043 case NE: case EQ:
1044 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
1045 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
1046 exp = attr_rtx (GET_CODE (exp),
1047 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
1048 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
1049 /* These cases can't be simplified. */
1050 RTX_UNCHANGING_P (exp) = 1;
1051 break;
1053 case SYMBOL_REF:
1054 if (is_const)
1056 /* These cases are valid for constant attributes, but can't be
1057 simplified. */
1058 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1059 RTX_UNCHANGING_P (exp) = 1;
1060 break;
1062 default:
1063 fatal ("RTL operator \"%s\" not valid in attribute test",
1064 GET_RTX_NAME (GET_CODE (exp)));
1067 return exp;
1070 /* Given an expression, ensure that it is validly formed and that all named
1071 attribute values are valid for the given attribute. Issue a fatal error
1072 if not. If no attribute is specified, assume a numeric attribute.
1074 Return a perhaps modified replacement expression for the value. */
1076 static rtx
1077 check_attr_value (exp, attr)
1078 rtx exp;
1079 struct attr_desc *attr;
1081 struct attr_value *av;
1082 const char *p;
1083 int i;
1085 switch (GET_CODE (exp))
1087 case CONST_INT:
1088 if (attr && ! attr->is_numeric)
1089 fatal ("CONST_INT not valid for non-numeric `%s' attribute",
1090 attr->name);
1092 if (INTVAL (exp) < 0 && ! attr->negative_ok)
1093 fatal ("Negative numeric value specified for `%s' attribute",
1094 attr->name);
1096 break;
1098 case CONST_STRING:
1099 if (! strcmp (XSTR (exp, 0), "*"))
1100 break;
1102 if (attr == 0 || attr->is_numeric)
1104 p = XSTR (exp, 0);
1105 if (attr && attr->negative_ok && *p == '-')
1106 p++;
1107 for (; *p; p++)
1108 if (*p > '9' || *p < '0')
1109 fatal ("Non-numeric value for numeric `%s' attribute",
1110 attr ? attr->name : "internal");
1111 break;
1114 for (av = attr->first_value; av; av = av->next)
1115 if (GET_CODE (av->value) == CONST_STRING
1116 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
1117 break;
1119 if (av == NULL)
1120 fatal ("Unknown value `%s' for `%s' attribute",
1121 XSTR (exp, 0), attr ? attr->name : "internal");
1123 break;
1125 case IF_THEN_ELSE:
1126 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
1127 attr ? attr->is_const : 0);
1128 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1129 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
1130 break;
1132 case PLUS:
1133 case MINUS:
1134 case MULT:
1135 case DIV:
1136 case MOD:
1137 if (attr && !attr->is_numeric)
1138 fatal ("Invalid operation `%s' for non-numeric attribute value",
1139 GET_RTX_NAME (GET_CODE (exp)));
1140 /* FALLTHRU */
1142 case IOR:
1143 case AND:
1144 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1145 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1146 break;
1148 case FFS:
1149 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1150 break;
1152 case COND:
1153 if (XVECLEN (exp, 0) % 2 != 0)
1154 fatal ("First operand of COND must have even length");
1156 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1158 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
1159 attr ? attr->is_const : 0);
1160 XVECEXP (exp, 0, i + 1)
1161 = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
1164 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1165 break;
1167 case ATTR:
1169 struct attr_desc *attr2 = find_attr (XSTR (exp, 0), 0);
1170 if (attr2 == NULL)
1171 fatal ("Unknown attribute `%s' in ATTR", XSTR (exp, 0));
1172 else if ((attr && attr->is_const) && ! attr2->is_const)
1173 fatal ("Non-constant attribute `%s' referenced from `%s'",
1174 XSTR (exp, 0), attr->name);
1175 else if (attr
1176 && (attr->is_numeric != attr2->is_numeric
1177 || (! attr->negative_ok && attr2->negative_ok)))
1178 fatal ("Numeric attribute mismatch calling `%s' from `%s'",
1179 XSTR (exp, 0), attr->name);
1181 break;
1183 case SYMBOL_REF:
1184 /* A constant SYMBOL_REF is valid as a constant attribute test and
1185 is expanded later by make_canonical into a COND. In a non-constant
1186 attribute test, it is left be. */
1187 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1189 default:
1190 fatal ("Invalid operation `%s' for attribute value",
1191 GET_RTX_NAME (GET_CODE (exp)));
1194 return exp;
1197 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1198 It becomes a COND with each test being (eq_attr "alternative "n") */
1200 static rtx
1201 convert_set_attr_alternative (exp, num_alt, insn_index)
1202 rtx exp;
1203 int num_alt;
1204 int insn_index;
1206 rtx condexp;
1207 int i;
1209 if (XVECLEN (exp, 1) != num_alt)
1210 fatal ("Bad number of entries in SET_ATTR_ALTERNATIVE for insn %d",
1211 insn_index);
1213 /* Make a COND with all tests but the last. Select the last value via the
1214 default. */
1215 condexp = rtx_alloc (COND);
1216 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1218 for (i = 0; i < num_alt - 1; i++)
1220 const char *p;
1221 p = attr_numeral (i);
1223 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1224 #if 0
1225 /* Sharing this EQ_ATTR rtl causes trouble. */
1226 XVECEXP (condexp, 0, 2 * i) = rtx_alloc (EQ_ATTR);
1227 XSTR (XVECEXP (condexp, 0, 2 * i), 0) = alternative_name;
1228 XSTR (XVECEXP (condexp, 0, 2 * i), 1) = p;
1229 #endif
1230 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1233 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1235 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1238 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1239 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1241 static rtx
1242 convert_set_attr (exp, num_alt, insn_index)
1243 rtx exp;
1244 int num_alt;
1245 int insn_index;
1247 rtx newexp;
1248 const char *name_ptr;
1249 char *p;
1250 int n;
1252 /* See how many alternative specified. */
1253 n = n_comma_elts (XSTR (exp, 1));
1254 if (n == 1)
1255 return attr_rtx (SET,
1256 attr_rtx (ATTR, XSTR (exp, 0)),
1257 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1259 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1260 XSTR (newexp, 0) = XSTR (exp, 0);
1261 XVEC (newexp, 1) = rtvec_alloc (n);
1263 /* Process each comma-separated name. */
1264 name_ptr = XSTR (exp, 1);
1265 n = 0;
1266 while ((p = next_comma_elt (&name_ptr)) != NULL)
1267 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1269 return convert_set_attr_alternative (newexp, num_alt, insn_index);
1272 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1273 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1274 expressions. */
1276 static void
1277 check_defs ()
1279 struct insn_def *id;
1280 struct attr_desc *attr;
1281 int i;
1282 rtx value;
1284 for (id = defs; id; id = id->next)
1286 if (XVEC (id->def, id->vec_idx) == NULL)
1287 continue;
1289 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1291 value = XVECEXP (id->def, id->vec_idx, i);
1292 switch (GET_CODE (value))
1294 case SET:
1295 if (GET_CODE (XEXP (value, 0)) != ATTR)
1296 fatal ("Bad attribute set in pattern %d", id->insn_index);
1297 break;
1299 case SET_ATTR_ALTERNATIVE:
1300 value = convert_set_attr_alternative (value,
1301 id->num_alternatives,
1302 id->insn_index);
1303 break;
1305 case SET_ATTR:
1306 value = convert_set_attr (value, id->num_alternatives,
1307 id->insn_index);
1308 break;
1310 default:
1311 fatal ("Invalid attribute code `%s' for pattern %d",
1312 GET_RTX_NAME (GET_CODE (value)), id->insn_index);
1315 if ((attr = find_attr (XSTR (XEXP (value, 0), 0), 0)) == NULL)
1316 fatal ("Unknown attribute `%s' for pattern number %d",
1317 XSTR (XEXP (value, 0), 0), id->insn_index);
1319 XVECEXP (id->def, id->vec_idx, i) = value;
1320 XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1325 #if 0
1326 /* Given a constant SYMBOL_REF expression, convert to a COND that
1327 explicitly tests each enumerated value. */
1329 static rtx
1330 convert_const_symbol_ref (exp, attr)
1331 rtx exp;
1332 struct attr_desc *attr;
1334 rtx condexp;
1335 struct attr_value *av;
1336 int i;
1337 int num_alt = 0;
1339 for (av = attr->first_value; av; av = av->next)
1340 num_alt++;
1342 /* Make a COND with all tests but the last, and in the original order.
1343 Select the last value via the default. Note that the attr values
1344 are constructed in reverse order. */
1346 condexp = rtx_alloc (COND);
1347 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1348 av = attr->first_value;
1349 XEXP (condexp, 1) = av->value;
1351 for (i = num_alt - 2; av = av->next, i >= 0; i--)
1353 char *p, *string;
1354 rtx value;
1356 string = p = (char *) oballoc (2
1357 + strlen (attr->name)
1358 + strlen (XSTR (av->value, 0)));
1359 strcpy (p, attr->name);
1360 strcat (p, "_");
1361 strcat (p, XSTR (av->value, 0));
1362 for (; *p != '\0'; p++)
1363 *p = TOUPPER (*p);
1365 value = attr_rtx (SYMBOL_REF, string);
1366 RTX_UNCHANGING_P (value) = 1;
1368 XVECEXP (condexp, 0, 2 * i) = attr_rtx (EQ, exp, value);
1370 XVECEXP (condexp, 0, 2 * i + 1) = av->value;
1373 return condexp;
1375 #endif
1377 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1378 expressions by converting them into a COND. This removes cases from this
1379 program. Also, replace an attribute value of "*" with the default attribute
1380 value. */
1382 static rtx
1383 make_canonical (attr, exp)
1384 struct attr_desc *attr;
1385 rtx exp;
1387 int i;
1388 rtx newexp;
1390 switch (GET_CODE (exp))
1392 case CONST_INT:
1393 exp = make_numeric_value (INTVAL (exp));
1394 break;
1396 case CONST_STRING:
1397 if (! strcmp (XSTR (exp, 0), "*"))
1399 if (attr == 0 || attr->default_val == 0)
1400 fatal ("(attr_value \"*\") used in invalid context.");
1401 exp = attr->default_val->value;
1404 break;
1406 case SYMBOL_REF:
1407 if (!attr->is_const || RTX_UNCHANGING_P (exp))
1408 break;
1409 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1410 This makes the COND something that won't be considered an arbitrary
1411 expression by walk_attr_value. */
1412 RTX_UNCHANGING_P (exp) = 1;
1413 #if 0
1414 /* ??? Why do we do this? With attribute values { A B C D E }, this
1415 tends to generate (!(x==A) && !(x==B) && !(x==C) && !(x==D)) rather
1416 than (x==E). */
1417 exp = convert_const_symbol_ref (exp, attr);
1418 RTX_UNCHANGING_P (exp) = 1;
1419 exp = check_attr_value (exp, attr);
1420 /* Goto COND case since this is now a COND. Note that while the
1421 new expression is rescanned, all symbol_ref notes are marked as
1422 unchanging. */
1423 goto cond;
1424 #else
1425 exp = check_attr_value (exp, attr);
1426 break;
1427 #endif
1429 case IF_THEN_ELSE:
1430 newexp = rtx_alloc (COND);
1431 XVEC (newexp, 0) = rtvec_alloc (2);
1432 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1433 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1435 XEXP (newexp, 1) = XEXP (exp, 2);
1437 exp = newexp;
1438 /* Fall through to COND case since this is now a COND. */
1440 case COND:
1442 int allsame = 1;
1443 rtx defval;
1445 /* First, check for degenerate COND. */
1446 if (XVECLEN (exp, 0) == 0)
1447 return make_canonical (attr, XEXP (exp, 1));
1448 defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1450 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1452 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1453 XVECEXP (exp, 0, i + 1)
1454 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1455 if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1456 allsame = 0;
1458 if (allsame)
1459 return defval;
1461 break;
1463 default:
1464 break;
1467 return exp;
1470 static rtx
1471 copy_boolean (exp)
1472 rtx exp;
1474 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1475 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1476 copy_boolean (XEXP (exp, 1)));
1477 return exp;
1480 /* Given a value and an attribute description, return a `struct attr_value *'
1481 that represents that value. This is either an existing structure, if the
1482 value has been previously encountered, or a newly-created structure.
1484 `insn_code' is the code of an insn whose attribute has the specified
1485 value (-2 if not processing an insn). We ensure that all insns for
1486 a given value have the same number of alternatives if the value checks
1487 alternatives. */
1489 static struct attr_value *
1490 get_attr_value (value, attr, insn_code)
1491 rtx value;
1492 struct attr_desc *attr;
1493 int insn_code;
1495 struct attr_value *av;
1496 int num_alt = 0;
1498 value = make_canonical (attr, value);
1499 if (compares_alternatives_p (value))
1501 if (insn_code < 0 || insn_alternatives == NULL)
1502 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1503 else
1504 num_alt = insn_alternatives[insn_code];
1507 for (av = attr->first_value; av; av = av->next)
1508 if (rtx_equal_p (value, av->value)
1509 && (num_alt == 0 || av->first_insn == NULL
1510 || insn_alternatives[av->first_insn->insn_code]))
1511 return av;
1513 av = (struct attr_value *) oballoc (sizeof (struct attr_value));
1514 av->value = value;
1515 av->next = attr->first_value;
1516 attr->first_value = av;
1517 av->first_insn = NULL;
1518 av->num_insns = 0;
1519 av->has_asm_insn = 0;
1521 return av;
1524 /* After all DEFINE_DELAYs have been read in, create internal attributes
1525 to generate the required routines.
1527 First, we compute the number of delay slots for each insn (as a COND of
1528 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1529 delay type is specified, we compute a similar function giving the
1530 DEFINE_DELAY ordinal for each insn.
1532 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1533 tells whether a given insn can be in that delay slot.
1535 Normal attribute filling and optimization expands these to contain the
1536 information needed to handle delay slots. */
1538 static void
1539 expand_delays ()
1541 struct delay_desc *delay;
1542 rtx condexp;
1543 rtx newexp;
1544 int i;
1545 char *p;
1547 /* First, generate data for `num_delay_slots' function. */
1549 condexp = rtx_alloc (COND);
1550 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1551 XEXP (condexp, 1) = make_numeric_value (0);
1553 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1555 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1556 XVECEXP (condexp, 0, i + 1)
1557 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1560 make_internal_attr ("*num_delay_slots", condexp, 0);
1562 /* If more than one delay type, do the same for computing the delay type. */
1563 if (num_delays > 1)
1565 condexp = rtx_alloc (COND);
1566 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1567 XEXP (condexp, 1) = make_numeric_value (0);
1569 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1571 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1572 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1575 make_internal_attr ("*delay_type", condexp, 1);
1578 /* For each delay possibility and delay slot, compute an eligibility
1579 attribute for non-annulled insns and for each type of annulled (annul
1580 if true and annul if false). */
1581 for (delay = delays; delay; delay = delay->next)
1583 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1585 condexp = XVECEXP (delay->def, 1, i);
1586 if (condexp == 0) condexp = false_rtx;
1587 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1588 make_numeric_value (1), make_numeric_value (0));
1590 p = attr_printf (sizeof ("*delay__") + MAX_DIGITS*2, "*delay_%d_%d",
1591 delay->num, i / 3);
1592 make_internal_attr (p, newexp, 1);
1594 if (have_annul_true)
1596 condexp = XVECEXP (delay->def, 1, i + 1);
1597 if (condexp == 0) condexp = false_rtx;
1598 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1599 make_numeric_value (1),
1600 make_numeric_value (0));
1601 p = attr_printf (sizeof ("*annul_true__") + MAX_DIGITS*2,
1602 "*annul_true_%d_%d", delay->num, i / 3);
1603 make_internal_attr (p, newexp, 1);
1606 if (have_annul_false)
1608 condexp = XVECEXP (delay->def, 1, i + 2);
1609 if (condexp == 0) condexp = false_rtx;
1610 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1611 make_numeric_value (1),
1612 make_numeric_value (0));
1613 p = attr_printf (sizeof ("*annul_false__") + MAX_DIGITS*2,
1614 "*annul_false_%d_%d", delay->num, i / 3);
1615 make_internal_attr (p, newexp, 1);
1621 /* This function is given a left and right side expression and an operator.
1622 Each side is a conditional expression, each alternative of which has a
1623 numerical value. The function returns another conditional expression
1624 which, for every possible set of condition values, returns a value that is
1625 the operator applied to the values of the two sides.
1627 Since this is called early, it must also support IF_THEN_ELSE. */
1629 static rtx
1630 operate_exp (op, left, right)
1631 enum operator op;
1632 rtx left, right;
1634 int left_value, right_value;
1635 rtx newexp;
1636 int i;
1638 /* If left is a string, apply operator to it and the right side. */
1639 if (GET_CODE (left) == CONST_STRING)
1641 /* If right is also a string, just perform the operation. */
1642 if (GET_CODE (right) == CONST_STRING)
1644 left_value = atoi (XSTR (left, 0));
1645 right_value = atoi (XSTR (right, 0));
1646 switch (op)
1648 case PLUS_OP:
1649 i = left_value + right_value;
1650 break;
1652 case MINUS_OP:
1653 i = left_value - right_value;
1654 break;
1656 case POS_MINUS_OP: /* The positive part of LEFT - RIGHT. */
1657 if (left_value > right_value)
1658 i = left_value - right_value;
1659 else
1660 i = 0;
1661 break;
1663 case OR_OP:
1664 case ORX_OP:
1665 i = left_value | right_value;
1666 break;
1668 case EQ_OP:
1669 i = left_value == right_value;
1670 break;
1672 case RANGE_OP:
1673 i = (left_value << (HOST_BITS_PER_INT / 2)) | right_value;
1674 break;
1676 case MAX_OP:
1677 if (left_value > right_value)
1678 i = left_value;
1679 else
1680 i = right_value;
1681 break;
1683 case MIN_OP:
1684 if (left_value < right_value)
1685 i = left_value;
1686 else
1687 i = right_value;
1688 break;
1690 default:
1691 abort ();
1694 if (i == left_value)
1695 return left;
1696 if (i == right_value)
1697 return right;
1698 return make_numeric_value (i);
1700 else if (GET_CODE (right) == IF_THEN_ELSE)
1702 /* Apply recursively to all values within. */
1703 rtx newleft = operate_exp (op, left, XEXP (right, 1));
1704 rtx newright = operate_exp (op, left, XEXP (right, 2));
1705 if (rtx_equal_p (newleft, newright))
1706 return newleft;
1707 return attr_rtx (IF_THEN_ELSE, XEXP (right, 0), newleft, newright);
1709 else if (GET_CODE (right) == COND)
1711 int allsame = 1;
1712 rtx defval;
1714 newexp = rtx_alloc (COND);
1715 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (right, 0));
1716 defval = XEXP (newexp, 1) = operate_exp (op, left, XEXP (right, 1));
1718 for (i = 0; i < XVECLEN (right, 0); i += 2)
1720 XVECEXP (newexp, 0, i) = XVECEXP (right, 0, i);
1721 XVECEXP (newexp, 0, i + 1)
1722 = operate_exp (op, left, XVECEXP (right, 0, i + 1));
1723 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1724 defval))
1725 allsame = 0;
1728 /* If the resulting cond is trivial (all alternatives
1729 give the same value), optimize it away. */
1730 if (allsame)
1732 if (!ggc_p)
1733 obstack_free (rtl_obstack, newexp);
1734 return operate_exp (op, left, XEXP (right, 1));
1737 /* If the result is the same as the RIGHT operand,
1738 just use that. */
1739 if (rtx_equal_p (newexp, right))
1741 if (!ggc_p)
1742 obstack_free (rtl_obstack, newexp);
1743 return right;
1746 return newexp;
1748 else
1749 fatal ("Badly formed attribute value");
1752 /* A hack to prevent expand_units from completely blowing up: ORX_OP does
1753 not associate through IF_THEN_ELSE. */
1754 else if (op == ORX_OP && GET_CODE (right) == IF_THEN_ELSE)
1756 return attr_rtx (IOR, left, right);
1759 /* Otherwise, do recursion the other way. */
1760 else if (GET_CODE (left) == IF_THEN_ELSE)
1762 rtx newleft = operate_exp (op, XEXP (left, 1), right);
1763 rtx newright = operate_exp (op, XEXP (left, 2), right);
1764 if (rtx_equal_p (newleft, newright))
1765 return newleft;
1766 return attr_rtx (IF_THEN_ELSE, XEXP (left, 0), newleft, newright);
1768 else if (GET_CODE (left) == COND)
1770 int allsame = 1;
1771 rtx defval;
1773 newexp = rtx_alloc (COND);
1774 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (left, 0));
1775 defval = XEXP (newexp, 1) = operate_exp (op, XEXP (left, 1), right);
1777 for (i = 0; i < XVECLEN (left, 0); i += 2)
1779 XVECEXP (newexp, 0, i) = XVECEXP (left, 0, i);
1780 XVECEXP (newexp, 0, i + 1)
1781 = operate_exp (op, XVECEXP (left, 0, i + 1), right);
1782 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1783 defval))
1784 allsame = 0;
1787 /* If the cond is trivial (all alternatives give the same value),
1788 optimize it away. */
1789 if (allsame)
1791 if (!ggc_p)
1792 obstack_free (rtl_obstack, newexp);
1793 return operate_exp (op, XEXP (left, 1), right);
1796 /* If the result is the same as the LEFT operand,
1797 just use that. */
1798 if (rtx_equal_p (newexp, left))
1800 if (!ggc_p)
1801 obstack_free (rtl_obstack, newexp);
1802 return left;
1805 return newexp;
1808 else
1809 fatal ("Badly formed attribute value.");
1810 /* NOTREACHED */
1811 return NULL;
1814 /* Once all attributes and DEFINE_FUNCTION_UNITs have been read, we
1815 construct a number of attributes.
1817 The first produces a function `function_units_used' which is given an
1818 insn and produces an encoding showing which function units are required
1819 for the execution of that insn. If the value is non-negative, the insn
1820 uses that unit; otherwise, the value is a one's compliment mask of units
1821 used.
1823 The second produces a function `result_ready_cost' which is used to
1824 determine the time that the result of an insn will be ready and hence
1825 a worst-case schedule.
1827 Both of these produce quite complex expressions which are then set as the
1828 default value of internal attributes. Normal attribute simplification
1829 should produce reasonable expressions.
1831 For each unit, a `<name>_unit_ready_cost' function will take an
1832 insn and give the delay until that unit will be ready with the result
1833 and a `<name>_unit_conflict_cost' function is given an insn already
1834 executing on the unit and a candidate to execute and will give the
1835 cost from the time the executing insn started until the candidate
1836 can start (ignore limitations on the number of simultaneous insns).
1838 For each unit, a `<name>_unit_blockage' function is given an insn
1839 already executing on the unit and a candidate to execute and will
1840 give the delay incurred due to function unit conflicts. The range of
1841 blockage cost values for a given executing insn is given by the
1842 `<name>_unit_blockage_range' function. These values are encoded in
1843 an int where the upper half gives the minimum value and the lower
1844 half gives the maximum value. */
1846 static void
1847 expand_units ()
1849 struct function_unit *unit, **unit_num;
1850 struct function_unit_op *op, **op_array, ***unit_ops;
1851 rtx unitsmask;
1852 rtx readycost;
1853 rtx newexp;
1854 const char *str;
1855 int i, j, u, num, nvalues;
1857 /* Rebuild the condition for the unit to share the RTL expressions.
1858 Sharing is required by simplify_by_exploding. Build the issue delay
1859 expressions. Validate the expressions we were given for the conditions
1860 and conflict vector. Then make attributes for use in the conflict
1861 function. */
1863 for (unit = units; unit; unit = unit->next)
1865 unit->condexp = check_attr_test (unit->condexp, 0);
1867 for (op = unit->ops; op; op = op->next)
1869 rtx issue_delay = make_numeric_value (op->issue_delay);
1870 rtx issue_exp = issue_delay;
1872 /* Build, validate, and simplify the issue delay expression. */
1873 if (op->conflict_exp != true_rtx)
1874 issue_exp = attr_rtx (IF_THEN_ELSE, op->conflict_exp,
1875 issue_exp, make_numeric_value (0));
1876 issue_exp = check_attr_value (make_canonical (NULL_ATTR,
1877 issue_exp),
1878 NULL_ATTR);
1879 issue_exp = simplify_knowing (issue_exp, unit->condexp);
1880 op->issue_exp = issue_exp;
1882 /* Make an attribute for use in the conflict function if needed. */
1883 unit->needs_conflict_function = (unit->issue_delay.min
1884 != unit->issue_delay.max);
1885 if (unit->needs_conflict_function)
1887 str = attr_printf (strlen (unit->name) + sizeof ("*_cost_") + MAX_DIGITS,
1888 "*%s_cost_%d", unit->name, op->num);
1889 make_internal_attr (str, issue_exp, 1);
1892 /* Validate the condition. */
1893 op->condexp = check_attr_test (op->condexp, 0);
1897 /* Compute the mask of function units used. Initially, the unitsmask is
1898 zero. Set up a conditional to compute each unit's contribution. */
1899 unitsmask = make_numeric_value (0);
1900 newexp = rtx_alloc (IF_THEN_ELSE);
1901 XEXP (newexp, 2) = make_numeric_value (0);
1903 /* If we have just a few units, we may be all right expanding the whole
1904 thing. But the expansion is 2**N in space on the number of opclasses,
1905 so we can't do this for very long -- Alpha and MIPS in particular have
1906 problems with this. So in that situation, we fall back on an alternate
1907 implementation method. */
1908 #define NUM_UNITOP_CUTOFF 20
1910 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1912 /* Merge each function unit into the unit mask attributes. */
1913 for (unit = units; unit; unit = unit->next)
1915 XEXP (newexp, 0) = unit->condexp;
1916 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1917 unitsmask = operate_exp (OR_OP, unitsmask, newexp);
1920 else
1922 /* Merge each function unit into the unit mask attributes. */
1923 for (unit = units; unit; unit = unit->next)
1925 XEXP (newexp, 0) = unit->condexp;
1926 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1927 unitsmask = operate_exp (ORX_OP, unitsmask, attr_copy_rtx (newexp));
1931 /* Simplify the unit mask expression, encode it, and make an attribute
1932 for the function_units_used function. */
1933 unitsmask = simplify_by_exploding (unitsmask);
1935 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1936 unitsmask = encode_units_mask (unitsmask);
1937 else
1939 /* We can no longer encode unitsmask at compile time, so emit code to
1940 calculate it at runtime. Rather, put a marker for where we'd do
1941 the code, and actually output it in write_attr_get(). */
1942 unitsmask = attr_rtx (FFS, unitsmask);
1945 make_internal_attr ("*function_units_used", unitsmask, 10);
1947 /* Create an array of ops for each unit. Add an extra unit for the
1948 result_ready_cost function that has the ops of all other units. */
1949 unit_ops = (struct function_unit_op ***)
1950 alloca ((num_units + 1) * sizeof (struct function_unit_op **));
1951 unit_num = (struct function_unit **)
1952 alloca ((num_units + 1) * sizeof (struct function_unit *));
1954 unit_num[num_units] = unit = (struct function_unit *)
1955 alloca (sizeof (struct function_unit));
1956 unit->num = num_units;
1957 unit->num_opclasses = 0;
1959 for (unit = units; unit; unit = unit->next)
1961 unit_num[num_units]->num_opclasses += unit->num_opclasses;
1962 unit_num[unit->num] = unit;
1963 unit_ops[unit->num] = op_array = (struct function_unit_op **)
1964 alloca (unit->num_opclasses * sizeof (struct function_unit_op *));
1966 for (op = unit->ops; op; op = op->next)
1967 op_array[op->num] = op;
1970 /* Compose the array of ops for the extra unit. */
1971 unit_ops[num_units] = op_array = (struct function_unit_op **)
1972 alloca (unit_num[num_units]->num_opclasses
1973 * sizeof (struct function_unit_op *));
1975 for (unit = units, i = 0; unit; i += unit->num_opclasses, unit = unit->next)
1976 bcopy ((char *) unit_ops[unit->num], (char *) &op_array[i],
1977 unit->num_opclasses * sizeof (struct function_unit_op *));
1979 /* Compute the ready cost function for each unit by computing the
1980 condition for each non-default value. */
1981 for (u = 0; u <= num_units; u++)
1983 rtx orexp;
1984 int value;
1986 unit = unit_num[u];
1987 op_array = unit_ops[unit->num];
1988 num = unit->num_opclasses;
1990 /* Sort the array of ops into increasing ready cost order. */
1991 for (i = 0; i < num; i++)
1992 for (j = num - 1; j > i; j--)
1993 if (op_array[j-1]->ready < op_array[j]->ready)
1995 op = op_array[j];
1996 op_array[j] = op_array[j-1];
1997 op_array[j-1] = op;
2000 /* Determine how many distinct non-default ready cost values there
2001 are. We use a default ready cost value of 1. */
2002 nvalues = 0; value = 1;
2003 for (i = num - 1; i >= 0; i--)
2004 if (op_array[i]->ready > value)
2006 value = op_array[i]->ready;
2007 nvalues++;
2010 if (nvalues == 0)
2011 readycost = make_numeric_value (1);
2012 else
2014 /* Construct the ready cost expression as a COND of each value from
2015 the largest to the smallest. */
2016 readycost = rtx_alloc (COND);
2017 XVEC (readycost, 0) = rtvec_alloc (nvalues * 2);
2018 XEXP (readycost, 1) = make_numeric_value (1);
2020 nvalues = 0; orexp = false_rtx; value = op_array[0]->ready;
2021 for (i = 0; i < num; i++)
2023 op = op_array[i];
2024 if (op->ready <= 1)
2025 break;
2026 else if (op->ready == value)
2027 orexp = insert_right_side (IOR, orexp, op->condexp, -2, -2);
2028 else
2030 XVECEXP (readycost, 0, nvalues * 2) = orexp;
2031 XVECEXP (readycost, 0, nvalues * 2 + 1)
2032 = make_numeric_value (value);
2033 nvalues++;
2034 value = op->ready;
2035 orexp = op->condexp;
2038 XVECEXP (readycost, 0, nvalues * 2) = orexp;
2039 XVECEXP (readycost, 0, nvalues * 2 + 1) = make_numeric_value (value);
2042 if (u < num_units)
2044 rtx max_blockage = 0, min_blockage = 0;
2046 /* Simplify the readycost expression by only considering insns
2047 that use the unit. */
2048 readycost = simplify_knowing (readycost, unit->condexp);
2050 /* Determine the blockage cost the executing insn (E) given
2051 the candidate insn (C). This is the maximum of the issue
2052 delay, the pipeline delay, and the simultaneity constraint.
2053 Each function_unit_op represents the characteristics of the
2054 candidate insn, so in the expressions below, C is a known
2055 term and E is an unknown term.
2057 We compute the blockage cost for each E for every possible C.
2058 Thus OP represents E, and READYCOST is a list of values for
2059 every possible C.
2061 The issue delay function for C is op->issue_exp and is used to
2062 write the `<name>_unit_conflict_cost' function. Symbolicly
2063 this is "ISSUE-DELAY (E,C)".
2065 The pipeline delay results form the FIFO constraint on the
2066 function unit and is "READY-COST (E) + 1 - READY-COST (C)".
2068 The simultaneity constraint is based on how long it takes to
2069 fill the unit given the minimum issue delay. FILL-TIME is the
2070 constant "MIN (ISSUE-DELAY (*,*)) * (SIMULTANEITY - 1)", and
2071 the simultaneity constraint is "READY-COST (E) - FILL-TIME"
2072 if SIMULTANEITY is non-zero and zero otherwise.
2074 Thus, BLOCKAGE (E,C) when SIMULTANEITY is zero is
2076 MAX (ISSUE-DELAY (E,C),
2077 READY-COST (E) - (READY-COST (C) - 1))
2079 and otherwise
2081 MAX (ISSUE-DELAY (E,C),
2082 READY-COST (E) - (READY-COST (C) - 1),
2083 READY-COST (E) - FILL-TIME)
2085 The `<name>_unit_blockage' function is computed by determining
2086 this value for each candidate insn. As these values are
2087 computed, we also compute the upper and lower bounds for
2088 BLOCKAGE (E,*). These are combined to form the function
2089 `<name>_unit_blockage_range'. Finally, the maximum blockage
2090 cost, MAX (BLOCKAGE (*,*)), is computed. */
2092 for (op = unit->ops; op; op = op->next)
2094 rtx blockage = op->issue_exp;
2095 blockage = simplify_knowing (blockage, unit->condexp);
2097 /* Add this op's contribution to MAX (BLOCKAGE (E,*)) and
2098 MIN (BLOCKAGE (E,*)). */
2099 if (max_blockage == 0)
2100 max_blockage = min_blockage = blockage;
2101 else
2103 max_blockage
2104 = simplify_knowing (operate_exp (MAX_OP, max_blockage,
2105 blockage),
2106 unit->condexp);
2107 min_blockage
2108 = simplify_knowing (operate_exp (MIN_OP, min_blockage,
2109 blockage),
2110 unit->condexp);
2113 /* Make an attribute for use in the blockage function. */
2114 str = attr_printf (strlen (unit->name) + sizeof ("*_block_") + MAX_DIGITS,
2115 "*%s_block_%d", unit->name, op->num);
2116 make_internal_attr (str, blockage, 1);
2119 /* Record MAX (BLOCKAGE (*,*)). */
2121 int unknown;
2122 unit->max_blockage = max_attr_value (max_blockage, &unknown);
2125 /* See if the upper and lower bounds of BLOCKAGE (E,*) are the
2126 same. If so, the blockage function carries no additional
2127 information and is not written. */
2128 newexp = operate_exp (EQ_OP, max_blockage, min_blockage);
2129 newexp = simplify_knowing (newexp, unit->condexp);
2130 unit->needs_blockage_function
2131 = (GET_CODE (newexp) != CONST_STRING
2132 || atoi (XSTR (newexp, 0)) != 1);
2134 /* If the all values of BLOCKAGE (E,C) have the same value,
2135 neither blockage function is written. */
2136 unit->needs_range_function
2137 = (unit->needs_blockage_function
2138 || GET_CODE (max_blockage) != CONST_STRING);
2140 if (unit->needs_range_function)
2142 /* Compute the blockage range function and make an attribute
2143 for writing its value. */
2144 newexp = operate_exp (RANGE_OP, min_blockage, max_blockage);
2145 newexp = simplify_knowing (newexp, unit->condexp);
2147 str = attr_printf (strlen (unit->name) + sizeof ("*_unit_blockage_range"),
2148 "*%s_unit_blockage_range", unit->name);
2149 make_internal_attr (str, newexp, 20);
2152 str = attr_printf (strlen (unit->name) + sizeof ("*_unit_ready_cost"),
2153 "*%s_unit_ready_cost", unit->name);
2155 else
2156 str = "*result_ready_cost";
2158 /* Make an attribute for the ready_cost function. Simplifying
2159 further with simplify_by_exploding doesn't win. */
2160 make_internal_attr (str, readycost, 0);
2163 /* For each unit that requires a conflict cost function, make an attribute
2164 that maps insns to the operation number. */
2165 for (unit = units; unit; unit = unit->next)
2167 rtx caseexp;
2169 if (! unit->needs_conflict_function
2170 && ! unit->needs_blockage_function)
2171 continue;
2173 caseexp = rtx_alloc (COND);
2174 XVEC (caseexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
2176 for (op = unit->ops; op; op = op->next)
2178 /* Make our adjustment to the COND being computed. If we are the
2179 last operation class, place our values into the default of the
2180 COND. */
2181 if (op->num == unit->num_opclasses - 1)
2183 XEXP (caseexp, 1) = make_numeric_value (op->num);
2185 else
2187 XVECEXP (caseexp, 0, op->num * 2) = op->condexp;
2188 XVECEXP (caseexp, 0, op->num * 2 + 1)
2189 = make_numeric_value (op->num);
2193 /* Simplifying caseexp with simplify_by_exploding doesn't win. */
2194 str = attr_printf (strlen (unit->name) + sizeof ("*_cases"),
2195 "*%s_cases", unit->name);
2196 make_internal_attr (str, caseexp, 1);
2200 /* Simplify EXP given KNOWN_TRUE. */
2202 static rtx
2203 simplify_knowing (exp, known_true)
2204 rtx exp, known_true;
2206 if (GET_CODE (exp) != CONST_STRING)
2208 int unknown = 0, max;
2209 max = max_attr_value (exp, &unknown);
2210 if (! unknown)
2212 exp = attr_rtx (IF_THEN_ELSE, known_true, exp,
2213 make_numeric_value (max));
2214 exp = simplify_by_exploding (exp);
2217 return exp;
2220 /* Translate the CONST_STRING expressions in X to change the encoding of
2221 value. On input, the value is a bitmask with a one bit for each unit
2222 used; on output, the value is the unit number (zero based) if one
2223 and only one unit is used or the one's compliment of the bitmask. */
2225 static rtx
2226 encode_units_mask (x)
2227 rtx x;
2229 register int i;
2230 register int j;
2231 register enum rtx_code code;
2232 register const char *fmt;
2234 code = GET_CODE (x);
2236 switch (code)
2238 case CONST_STRING:
2239 i = atoi (XSTR (x, 0));
2240 if (i < 0)
2241 abort (); /* The sign bit encodes a one's compliment mask. */
2242 else if (i != 0 && i == (i & -i))
2243 /* Only one bit is set, so yield that unit number. */
2244 for (j = 0; (i >>= 1) != 0; j++)
2246 else
2247 j = ~i;
2248 return attr_rtx (CONST_STRING, attr_printf (MAX_DIGITS, "%d", j));
2250 case REG:
2251 case QUEUED:
2252 case CONST_INT:
2253 case CONST_DOUBLE:
2254 case SYMBOL_REF:
2255 case CODE_LABEL:
2256 case PC:
2257 case CC0:
2258 case EQ_ATTR:
2259 return x;
2261 default:
2262 break;
2265 /* Compare the elements. If any pair of corresponding elements
2266 fail to match, return 0 for the whole things. */
2268 fmt = GET_RTX_FORMAT (code);
2269 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2271 switch (fmt[i])
2273 case 'V':
2274 case 'E':
2275 for (j = 0; j < XVECLEN (x, i); j++)
2276 XVECEXP (x, i, j) = encode_units_mask (XVECEXP (x, i, j));
2277 break;
2279 case 'e':
2280 XEXP (x, i) = encode_units_mask (XEXP (x, i));
2281 break;
2284 return x;
2287 /* Once all attributes and insns have been read and checked, we construct for
2288 each attribute value a list of all the insns that have that value for
2289 the attribute. */
2291 static void
2292 fill_attr (attr)
2293 struct attr_desc *attr;
2295 struct attr_value *av;
2296 struct insn_ent *ie;
2297 struct insn_def *id;
2298 int i;
2299 rtx value;
2301 /* Don't fill constant attributes. The value is independent of
2302 any particular insn. */
2303 if (attr->is_const)
2304 return;
2306 for (id = defs; id; id = id->next)
2308 /* If no value is specified for this insn for this attribute, use the
2309 default. */
2310 value = NULL;
2311 if (XVEC (id->def, id->vec_idx))
2312 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
2313 if (! strcmp (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
2314 attr->name))
2315 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
2317 if (value == NULL)
2318 av = attr->default_val;
2319 else
2320 av = get_attr_value (value, attr, id->insn_code);
2322 ie = (struct insn_ent *) oballoc (sizeof (struct insn_ent));
2323 ie->insn_code = id->insn_code;
2324 ie->insn_index = id->insn_code;
2325 insert_insn_ent (av, ie);
2329 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
2330 test that checks relative positions of insns (uses MATCH_DUP or PC).
2331 If so, replace it with what is obtained by passing the expression to
2332 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
2333 recursively on each value (including the default value). Otherwise,
2334 return the value returned by NO_ADDRESS_FN applied to EXP. */
2336 static rtx
2337 substitute_address (exp, no_address_fn, address_fn)
2338 rtx exp;
2339 rtx (*no_address_fn) PARAMS ((rtx));
2340 rtx (*address_fn) PARAMS ((rtx));
2342 int i;
2343 rtx newexp;
2345 if (GET_CODE (exp) == COND)
2347 /* See if any tests use addresses. */
2348 address_used = 0;
2349 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2350 walk_attr_value (XVECEXP (exp, 0, i));
2352 if (address_used)
2353 return (*address_fn) (exp);
2355 /* Make a new copy of this COND, replacing each element. */
2356 newexp = rtx_alloc (COND);
2357 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
2358 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2360 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
2361 XVECEXP (newexp, 0, i + 1)
2362 = substitute_address (XVECEXP (exp, 0, i + 1),
2363 no_address_fn, address_fn);
2366 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
2367 no_address_fn, address_fn);
2369 return newexp;
2372 else if (GET_CODE (exp) == IF_THEN_ELSE)
2374 address_used = 0;
2375 walk_attr_value (XEXP (exp, 0));
2376 if (address_used)
2377 return (*address_fn) (exp);
2379 return attr_rtx (IF_THEN_ELSE,
2380 substitute_address (XEXP (exp, 0),
2381 no_address_fn, address_fn),
2382 substitute_address (XEXP (exp, 1),
2383 no_address_fn, address_fn),
2384 substitute_address (XEXP (exp, 2),
2385 no_address_fn, address_fn));
2388 return (*no_address_fn) (exp);
2391 /* Make new attributes from the `length' attribute. The following are made,
2392 each corresponding to a function called from `shorten_branches' or
2393 `get_attr_length':
2395 *insn_default_length This is the length of the insn to be returned
2396 by `get_attr_length' before `shorten_branches'
2397 has been called. In each case where the length
2398 depends on relative addresses, the largest
2399 possible is used. This routine is also used
2400 to compute the initial size of the insn.
2402 *insn_variable_length_p This returns 1 if the insn's length depends
2403 on relative addresses, zero otherwise.
2405 *insn_current_length This is only called when it is known that the
2406 insn has a variable length and returns the
2407 current length, based on relative addresses.
2410 static void
2411 make_length_attrs ()
2413 static const char *new_names[] = {"*insn_default_length",
2414 "*insn_variable_length_p",
2415 "*insn_current_length"};
2416 static rtx (*no_address_fn[]) PARAMS ((rtx)) = {identity_fn, zero_fn, zero_fn};
2417 static rtx (*address_fn[]) PARAMS ((rtx)) = {max_fn, one_fn, identity_fn};
2418 size_t i;
2419 struct attr_desc *length_attr, *new_attr;
2420 struct attr_value *av, *new_av;
2421 struct insn_ent *ie, *new_ie;
2423 /* See if length attribute is defined. If so, it must be numeric. Make
2424 it special so we don't output anything for it. */
2425 length_attr = find_attr ("length", 0);
2426 if (length_attr == 0)
2427 return;
2429 if (! length_attr->is_numeric)
2430 fatal ("length attribute must be numeric.");
2432 length_attr->is_const = 0;
2433 length_attr->is_special = 1;
2435 /* Make each new attribute, in turn. */
2436 for (i = 0; i < sizeof new_names / sizeof new_names[0]; i++)
2438 make_internal_attr (new_names[i],
2439 substitute_address (length_attr->default_val->value,
2440 no_address_fn[i], address_fn[i]),
2442 new_attr = find_attr (new_names[i], 0);
2443 for (av = length_attr->first_value; av; av = av->next)
2444 for (ie = av->first_insn; ie; ie = ie->next)
2446 new_av = get_attr_value (substitute_address (av->value,
2447 no_address_fn[i],
2448 address_fn[i]),
2449 new_attr, ie->insn_code);
2450 new_ie = (struct insn_ent *) oballoc (sizeof (struct insn_ent));
2451 new_ie->insn_code = ie->insn_code;
2452 new_ie->insn_index = ie->insn_index;
2453 insert_insn_ent (new_av, new_ie);
2458 /* Utility functions called from above routine. */
2460 static rtx
2461 identity_fn (exp)
2462 rtx exp;
2464 return exp;
2467 static rtx
2468 zero_fn (exp)
2469 rtx exp ATTRIBUTE_UNUSED;
2471 return make_numeric_value (0);
2474 static rtx
2475 one_fn (exp)
2476 rtx exp ATTRIBUTE_UNUSED;
2478 return make_numeric_value (1);
2481 static rtx
2482 max_fn (exp)
2483 rtx exp;
2485 int unknown;
2486 return make_numeric_value (max_attr_value (exp, &unknown));
2489 static void
2490 write_length_unit_log ()
2492 struct attr_desc *length_attr = find_attr ("length", 0);
2493 struct attr_value *av;
2494 struct insn_ent *ie;
2495 unsigned int length_unit_log, length_or;
2496 int unknown = 0;
2498 if (length_attr == 0)
2499 return;
2500 length_or = or_attr_value (length_attr->default_val->value, &unknown);
2501 for (av = length_attr->first_value; av; av = av->next)
2502 for (ie = av->first_insn; ie; ie = ie->next)
2503 length_or |= or_attr_value (av->value, &unknown);
2505 if (unknown)
2506 length_unit_log = 0;
2507 else
2509 length_or = ~length_or;
2510 for (length_unit_log = 0; length_or & 1; length_or >>= 1)
2511 length_unit_log++;
2513 printf ("int length_unit_log = %u;\n", length_unit_log);
2516 /* Take a COND expression and see if any of the conditions in it can be
2517 simplified. If any are known true or known false for the particular insn
2518 code, the COND can be further simplified.
2520 Also call ourselves on any COND operations that are values of this COND.
2522 We do not modify EXP; rather, we make and return a new rtx. */
2524 static rtx
2525 simplify_cond (exp, insn_code, insn_index)
2526 rtx exp;
2527 int insn_code, insn_index;
2529 int i, j;
2530 /* We store the desired contents here,
2531 then build a new expression if they don't match EXP. */
2532 rtx defval = XEXP (exp, 1);
2533 rtx new_defval = XEXP (exp, 1);
2534 int len = XVECLEN (exp, 0);
2535 rtx *tests = (rtx *) alloca (len * sizeof (rtx));
2536 int allsame = 1;
2537 char *first_spacer;
2539 /* This lets us free all storage allocated below, if appropriate. */
2540 first_spacer = (char *) obstack_finish (rtl_obstack);
2542 bcopy ((char *) XVEC (exp, 0)->elem, (char *) tests, len * sizeof (rtx));
2544 /* See if default value needs simplification. */
2545 if (GET_CODE (defval) == COND)
2546 new_defval = simplify_cond (defval, insn_code, insn_index);
2548 /* Simplify the subexpressions, and see what tests we can get rid of. */
2550 for (i = 0; i < len; i += 2)
2552 rtx newtest, newval;
2554 /* Simplify this test. */
2555 newtest = SIMPLIFY_TEST_EXP (tests[i], insn_code, insn_index);
2556 tests[i] = newtest;
2558 newval = tests[i + 1];
2559 /* See if this value may need simplification. */
2560 if (GET_CODE (newval) == COND)
2561 newval = simplify_cond (newval, insn_code, insn_index);
2563 /* Look for ways to delete or combine this test. */
2564 if (newtest == true_rtx)
2566 /* If test is true, make this value the default
2567 and discard this + any following tests. */
2568 len = i;
2569 defval = tests[i + 1];
2570 new_defval = newval;
2573 else if (newtest == false_rtx)
2575 /* If test is false, discard it and its value. */
2576 for (j = i; j < len - 2; j++)
2577 tests[j] = tests[j + 2];
2578 len -= 2;
2581 else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
2583 /* If this value and the value for the prev test are the same,
2584 merge the tests. */
2586 tests[i - 2]
2587 = insert_right_side (IOR, tests[i - 2], newtest,
2588 insn_code, insn_index);
2590 /* Delete this test/value. */
2591 for (j = i; j < len - 2; j++)
2592 tests[j] = tests[j + 2];
2593 len -= 2;
2596 else
2597 tests[i + 1] = newval;
2600 /* If the last test in a COND has the same value
2601 as the default value, that test isn't needed. */
2603 while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
2604 len -= 2;
2606 /* See if we changed anything. */
2607 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
2608 allsame = 0;
2609 else
2610 for (i = 0; i < len; i++)
2611 if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
2613 allsame = 0;
2614 break;
2617 if (len == 0)
2619 if (!ggc_p)
2620 obstack_free (rtl_obstack, first_spacer);
2621 if (GET_CODE (defval) == COND)
2622 return simplify_cond (defval, insn_code, insn_index);
2623 return defval;
2625 else if (allsame)
2627 if (!ggc_p)
2628 obstack_free (rtl_obstack, first_spacer);
2629 return exp;
2631 else
2633 rtx newexp = rtx_alloc (COND);
2635 XVEC (newexp, 0) = rtvec_alloc (len);
2636 bcopy ((char *) tests, (char *) XVEC (newexp, 0)->elem,
2637 len * sizeof (rtx));
2638 XEXP (newexp, 1) = new_defval;
2639 return newexp;
2643 /* Remove an insn entry from an attribute value. */
2645 static void
2646 remove_insn_ent (av, ie)
2647 struct attr_value *av;
2648 struct insn_ent *ie;
2650 struct insn_ent *previe;
2652 if (av->first_insn == ie)
2653 av->first_insn = ie->next;
2654 else
2656 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
2658 previe->next = ie->next;
2661 av->num_insns--;
2662 if (ie->insn_code == -1)
2663 av->has_asm_insn = 0;
2665 num_insn_ents--;
2668 /* Insert an insn entry in an attribute value list. */
2670 static void
2671 insert_insn_ent (av, ie)
2672 struct attr_value *av;
2673 struct insn_ent *ie;
2675 ie->next = av->first_insn;
2676 av->first_insn = ie;
2677 av->num_insns++;
2678 if (ie->insn_code == -1)
2679 av->has_asm_insn = 1;
2681 num_insn_ents++;
2684 /* This is a utility routine to take an expression that is a tree of either
2685 AND or IOR expressions and insert a new term. The new term will be
2686 inserted at the right side of the first node whose code does not match
2687 the root. A new node will be created with the root's code. Its left
2688 side will be the old right side and its right side will be the new
2689 term.
2691 If the `term' is itself a tree, all its leaves will be inserted. */
2693 static rtx
2694 insert_right_side (code, exp, term, insn_code, insn_index)
2695 enum rtx_code code;
2696 rtx exp;
2697 rtx term;
2698 int insn_code, insn_index;
2700 rtx newexp;
2702 /* Avoid consing in some special cases. */
2703 if (code == AND && term == true_rtx)
2704 return exp;
2705 if (code == AND && term == false_rtx)
2706 return false_rtx;
2707 if (code == AND && exp == true_rtx)
2708 return term;
2709 if (code == AND && exp == false_rtx)
2710 return false_rtx;
2711 if (code == IOR && term == true_rtx)
2712 return true_rtx;
2713 if (code == IOR && term == false_rtx)
2714 return exp;
2715 if (code == IOR && exp == true_rtx)
2716 return true_rtx;
2717 if (code == IOR && exp == false_rtx)
2718 return term;
2719 if (attr_equal_p (exp, term))
2720 return exp;
2722 if (GET_CODE (term) == code)
2724 exp = insert_right_side (code, exp, XEXP (term, 0),
2725 insn_code, insn_index);
2726 exp = insert_right_side (code, exp, XEXP (term, 1),
2727 insn_code, insn_index);
2729 return exp;
2732 if (GET_CODE (exp) == code)
2734 rtx new = insert_right_side (code, XEXP (exp, 1),
2735 term, insn_code, insn_index);
2736 if (new != XEXP (exp, 1))
2737 /* Make a copy of this expression and call recursively. */
2738 newexp = attr_rtx (code, XEXP (exp, 0), new);
2739 else
2740 newexp = exp;
2742 else
2744 /* Insert the new term. */
2745 newexp = attr_rtx (code, exp, term);
2748 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2751 /* If we have an expression which AND's a bunch of
2752 (not (eq_attrq "alternative" "n"))
2753 terms, we may have covered all or all but one of the possible alternatives.
2754 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
2756 This routine is passed an expression and either AND or IOR. It returns a
2757 bitmask indicating which alternatives are mentioned within EXP. */
2759 static int
2760 compute_alternative_mask (exp, code)
2761 rtx exp;
2762 enum rtx_code code;
2764 const char *string;
2765 if (GET_CODE (exp) == code)
2766 return compute_alternative_mask (XEXP (exp, 0), code)
2767 | compute_alternative_mask (XEXP (exp, 1), code);
2769 else if (code == AND && GET_CODE (exp) == NOT
2770 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2771 && XSTR (XEXP (exp, 0), 0) == alternative_name)
2772 string = XSTR (XEXP (exp, 0), 1);
2774 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
2775 && XSTR (exp, 0) == alternative_name)
2776 string = XSTR (exp, 1);
2778 else
2779 return 0;
2781 if (string[1] == 0)
2782 return 1 << (string[0] - '0');
2783 return 1 << atoi (string);
2786 /* Given I, a single-bit mask, return RTX to compare the `alternative'
2787 attribute with the value represented by that bit. */
2789 static rtx
2790 make_alternative_compare (mask)
2791 int mask;
2793 rtx newexp;
2794 int i;
2796 /* Find the bit. */
2797 for (i = 0; (mask & (1 << i)) == 0; i++)
2800 newexp = attr_rtx (EQ_ATTR, alternative_name, attr_numeral (i));
2801 RTX_UNCHANGING_P (newexp) = 1;
2803 return newexp;
2806 /* If we are processing an (eq_attr "attr" "value") test, we find the value
2807 of "attr" for this insn code. From that value, we can compute a test
2808 showing when the EQ_ATTR will be true. This routine performs that
2809 computation. If a test condition involves an address, we leave the EQ_ATTR
2810 intact because addresses are only valid for the `length' attribute.
2812 EXP is the EQ_ATTR expression and VALUE is the value of that attribute
2813 for the insn corresponding to INSN_CODE and INSN_INDEX. */
2815 static rtx
2816 evaluate_eq_attr (exp, value, insn_code, insn_index)
2817 rtx exp;
2818 rtx value;
2819 int insn_code, insn_index;
2821 rtx orexp, andexp;
2822 rtx right;
2823 rtx newexp;
2824 int i;
2826 if (GET_CODE (value) == CONST_STRING)
2828 if (! strcmp (XSTR (value, 0), XSTR (exp, 1)))
2829 newexp = true_rtx;
2830 else
2831 newexp = false_rtx;
2833 else if (GET_CODE (value) == SYMBOL_REF)
2835 char *p, *string;
2837 if (GET_CODE (exp) != EQ_ATTR)
2838 abort();
2840 string = (char *) alloca (2 + strlen (XSTR (exp, 0))
2841 + strlen (XSTR (exp, 1)));
2842 strcpy (string, XSTR (exp, 0));
2843 strcat (string, "_");
2844 strcat (string, XSTR (exp, 1));
2845 for (p = string; *p ; p++)
2846 *p = TOUPPER (*p);
2848 newexp = attr_rtx (EQ, value,
2849 attr_rtx (SYMBOL_REF,
2850 attr_string(string, strlen(string))));
2852 else if (GET_CODE (value) == COND)
2854 /* We construct an IOR of all the cases for which the requested attribute
2855 value is present. Since we start with FALSE, if it is not present,
2856 FALSE will be returned.
2858 Each case is the AND of the NOT's of the previous conditions with the
2859 current condition; in the default case the current condition is TRUE.
2861 For each possible COND value, call ourselves recursively.
2863 The extra TRUE and FALSE expressions will be eliminated by another
2864 call to the simplification routine. */
2866 orexp = false_rtx;
2867 andexp = true_rtx;
2869 if (current_alternative_string)
2870 clear_struct_flag (value);
2872 for (i = 0; i < XVECLEN (value, 0); i += 2)
2874 rtx this = SIMPLIFY_TEST_EXP (XVECEXP (value, 0, i),
2875 insn_code, insn_index);
2877 SIMPLIFY_ALTERNATIVE (this);
2879 right = insert_right_side (AND, andexp, this,
2880 insn_code, insn_index);
2881 right = insert_right_side (AND, right,
2882 evaluate_eq_attr (exp,
2883 XVECEXP (value, 0,
2884 i + 1),
2885 insn_code, insn_index),
2886 insn_code, insn_index);
2887 orexp = insert_right_side (IOR, orexp, right,
2888 insn_code, insn_index);
2890 /* Add this condition into the AND expression. */
2891 newexp = attr_rtx (NOT, this);
2892 andexp = insert_right_side (AND, andexp, newexp,
2893 insn_code, insn_index);
2896 /* Handle the default case. */
2897 right = insert_right_side (AND, andexp,
2898 evaluate_eq_attr (exp, XEXP (value, 1),
2899 insn_code, insn_index),
2900 insn_code, insn_index);
2901 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2903 else
2904 abort ();
2906 /* If uses an address, must return original expression. But set the
2907 RTX_UNCHANGING_P bit so we don't try to simplify it again. */
2909 address_used = 0;
2910 walk_attr_value (newexp);
2912 if (address_used)
2914 /* This had `&& current_alternative_string', which seems to be wrong. */
2915 if (! RTX_UNCHANGING_P (exp))
2916 return copy_rtx_unchanging (exp);
2917 return exp;
2919 else
2920 return newexp;
2923 /* This routine is called when an AND of a term with a tree of AND's is
2924 encountered. If the term or its complement is present in the tree, it
2925 can be replaced with TRUE or FALSE, respectively.
2927 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2928 be true and hence are complementary.
2930 There is one special case: If we see
2931 (and (not (eq_attr "att" "v1"))
2932 (eq_attr "att" "v2"))
2933 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2934 replace the term, not anything in the AND tree. So we pass a pointer to
2935 the term. */
2937 static rtx
2938 simplify_and_tree (exp, pterm, insn_code, insn_index)
2939 rtx exp;
2940 rtx *pterm;
2941 int insn_code, insn_index;
2943 rtx left, right;
2944 rtx newexp;
2945 rtx temp;
2946 int left_eliminates_term, right_eliminates_term;
2948 if (GET_CODE (exp) == AND)
2950 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2951 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2952 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2954 newexp = attr_rtx (GET_CODE (exp), left, right);
2956 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2960 else if (GET_CODE (exp) == IOR)
2962 /* For the IOR case, we do the same as above, except that we can
2963 only eliminate `term' if both sides of the IOR would do so. */
2964 temp = *pterm;
2965 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2966 left_eliminates_term = (temp == true_rtx);
2968 temp = *pterm;
2969 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2970 right_eliminates_term = (temp == true_rtx);
2972 if (left_eliminates_term && right_eliminates_term)
2973 *pterm = true_rtx;
2975 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2977 newexp = attr_rtx (GET_CODE (exp), left, right);
2979 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2983 /* Check for simplifications. Do some extra checking here since this
2984 routine is called so many times. */
2986 if (exp == *pterm)
2987 return true_rtx;
2989 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2990 return false_rtx;
2992 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2993 return false_rtx;
2995 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2997 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2998 return exp;
3000 if (! strcmp (XSTR (exp, 1), XSTR (*pterm, 1)))
3001 return true_rtx;
3002 else
3003 return false_rtx;
3006 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
3007 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
3009 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
3010 return exp;
3012 if (! strcmp (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
3013 return false_rtx;
3014 else
3015 return true_rtx;
3018 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
3019 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
3021 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
3022 return exp;
3024 if (! strcmp (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
3025 return false_rtx;
3026 else
3027 *pterm = true_rtx;
3030 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
3032 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
3033 return true_rtx;
3036 else if (GET_CODE (exp) == NOT)
3038 if (attr_equal_p (XEXP (exp, 0), *pterm))
3039 return false_rtx;
3042 else if (GET_CODE (*pterm) == NOT)
3044 if (attr_equal_p (XEXP (*pterm, 0), exp))
3045 return false_rtx;
3048 else if (attr_equal_p (exp, *pterm))
3049 return true_rtx;
3051 return exp;
3054 /* Similar to `simplify_and_tree', but for IOR trees. */
3056 static rtx
3057 simplify_or_tree (exp, pterm, insn_code, insn_index)
3058 rtx exp;
3059 rtx *pterm;
3060 int insn_code, insn_index;
3062 rtx left, right;
3063 rtx newexp;
3064 rtx temp;
3065 int left_eliminates_term, right_eliminates_term;
3067 if (GET_CODE (exp) == IOR)
3069 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
3070 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
3071 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3073 newexp = attr_rtx (GET_CODE (exp), left, right);
3075 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3079 else if (GET_CODE (exp) == AND)
3081 /* For the AND case, we do the same as above, except that we can
3082 only eliminate `term' if both sides of the AND would do so. */
3083 temp = *pterm;
3084 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
3085 left_eliminates_term = (temp == false_rtx);
3087 temp = *pterm;
3088 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
3089 right_eliminates_term = (temp == false_rtx);
3091 if (left_eliminates_term && right_eliminates_term)
3092 *pterm = false_rtx;
3094 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3096 newexp = attr_rtx (GET_CODE (exp), left, right);
3098 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3102 if (attr_equal_p (exp, *pterm))
3103 return false_rtx;
3105 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
3106 return true_rtx;
3108 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
3109 return true_rtx;
3111 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
3112 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3113 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
3114 *pterm = false_rtx;
3116 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
3117 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
3118 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
3119 return false_rtx;
3121 return exp;
3124 /* Given an expression, see if it can be simplified for a particular insn
3125 code based on the values of other attributes being tested. This can
3126 eliminate nested get_attr_... calls.
3128 Note that if an endless recursion is specified in the patterns, the
3129 optimization will loop. However, it will do so in precisely the cases where
3130 an infinite recursion loop could occur during compilation. It's better that
3131 it occurs here! */
3133 static rtx
3134 simplify_test_exp (exp, insn_code, insn_index)
3135 rtx exp;
3136 int insn_code, insn_index;
3138 rtx left, right;
3139 struct attr_desc *attr;
3140 struct attr_value *av;
3141 struct insn_ent *ie;
3142 int i;
3143 rtx newexp = exp;
3144 char *spacer = (char *) obstack_finish (rtl_obstack);
3146 /* Don't re-simplify something we already simplified. */
3147 if (RTX_UNCHANGING_P (exp) || MEM_IN_STRUCT_P (exp))
3148 return exp;
3150 switch (GET_CODE (exp))
3152 case AND:
3153 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3154 SIMPLIFY_ALTERNATIVE (left);
3155 if (left == false_rtx)
3157 if (!ggc_p)
3158 obstack_free (rtl_obstack, spacer);
3159 return false_rtx;
3161 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3162 SIMPLIFY_ALTERNATIVE (right);
3163 if (left == false_rtx)
3165 if (!ggc_p)
3166 obstack_free (rtl_obstack, spacer);
3167 return false_rtx;
3170 /* If either side is an IOR and we have (eq_attr "alternative" ..")
3171 present on both sides, apply the distributive law since this will
3172 yield simplifications. */
3173 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
3174 && compute_alternative_mask (left, IOR)
3175 && compute_alternative_mask (right, IOR))
3177 if (GET_CODE (left) == IOR)
3179 rtx tem = left;
3180 left = right;
3181 right = tem;
3184 newexp = attr_rtx (IOR,
3185 attr_rtx (AND, left, XEXP (right, 0)),
3186 attr_rtx (AND, left, XEXP (right, 1)));
3188 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3191 /* Try with the term on both sides. */
3192 right = simplify_and_tree (right, &left, insn_code, insn_index);
3193 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3194 left = simplify_and_tree (left, &right, insn_code, insn_index);
3196 if (left == false_rtx || right == false_rtx)
3198 if (!ggc_p)
3199 obstack_free (rtl_obstack, spacer);
3200 return false_rtx;
3202 else if (left == true_rtx)
3204 return right;
3206 else if (right == true_rtx)
3208 return left;
3210 /* See if all or all but one of the insn's alternatives are specified
3211 in this tree. Optimize if so. */
3213 else if (insn_code >= 0
3214 && (GET_CODE (left) == AND
3215 || (GET_CODE (left) == NOT
3216 && GET_CODE (XEXP (left, 0)) == EQ_ATTR
3217 && XSTR (XEXP (left, 0), 0) == alternative_name)
3218 || GET_CODE (right) == AND
3219 || (GET_CODE (right) == NOT
3220 && GET_CODE (XEXP (right, 0)) == EQ_ATTR
3221 && XSTR (XEXP (right, 0), 0) == alternative_name)))
3223 i = compute_alternative_mask (exp, AND);
3224 if (i & ~insn_alternatives[insn_code])
3225 fatal ("Invalid alternative specified for pattern number %d",
3226 insn_index);
3228 /* If all alternatives are excluded, this is false. */
3229 i ^= insn_alternatives[insn_code];
3230 if (i == 0)
3231 return false_rtx;
3232 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3234 /* If just one excluded, AND a comparison with that one to the
3235 front of the tree. The others will be eliminated by
3236 optimization. We do not want to do this if the insn has one
3237 alternative and we have tested none of them! */
3238 left = make_alternative_compare (i);
3239 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3240 newexp = attr_rtx (AND, left, right);
3242 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3246 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3248 newexp = attr_rtx (AND, left, right);
3249 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3251 break;
3253 case IOR:
3254 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3255 SIMPLIFY_ALTERNATIVE (left);
3256 if (left == true_rtx)
3258 if (!ggc_p)
3259 obstack_free (rtl_obstack, spacer);
3260 return true_rtx;
3262 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3263 SIMPLIFY_ALTERNATIVE (right);
3264 if (right == true_rtx)
3266 if (!ggc_p)
3267 obstack_free (rtl_obstack, spacer);
3268 return true_rtx;
3271 right = simplify_or_tree (right, &left, insn_code, insn_index);
3272 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3273 left = simplify_or_tree (left, &right, insn_code, insn_index);
3275 if (right == true_rtx || left == true_rtx)
3277 if (!ggc_p)
3278 obstack_free (rtl_obstack, spacer);
3279 return true_rtx;
3281 else if (left == false_rtx)
3283 return right;
3285 else if (right == false_rtx)
3287 return left;
3290 /* Test for simple cases where the distributive law is useful. I.e.,
3291 convert (ior (and (x) (y))
3292 (and (x) (z)))
3293 to (and (x)
3294 (ior (y) (z)))
3297 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
3298 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
3300 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
3302 left = XEXP (left, 0);
3303 right = newexp;
3304 newexp = attr_rtx (AND, left, right);
3305 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3308 /* See if all or all but one of the insn's alternatives are specified
3309 in this tree. Optimize if so. */
3311 else if (insn_code >= 0
3312 && (GET_CODE (left) == IOR
3313 || (GET_CODE (left) == EQ_ATTR
3314 && XSTR (left, 0) == alternative_name)
3315 || GET_CODE (right) == IOR
3316 || (GET_CODE (right) == EQ_ATTR
3317 && XSTR (right, 0) == alternative_name)))
3319 i = compute_alternative_mask (exp, IOR);
3320 if (i & ~insn_alternatives[insn_code])
3321 fatal ("Invalid alternative specified for pattern number %d",
3322 insn_index);
3324 /* If all alternatives are included, this is true. */
3325 i ^= insn_alternatives[insn_code];
3326 if (i == 0)
3327 return true_rtx;
3328 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3330 /* If just one excluded, IOR a comparison with that one to the
3331 front of the tree. The others will be eliminated by
3332 optimization. We do not want to do this if the insn has one
3333 alternative and we have tested none of them! */
3334 left = make_alternative_compare (i);
3335 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3336 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
3338 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3342 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3344 newexp = attr_rtx (IOR, left, right);
3345 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3347 break;
3349 case NOT:
3350 if (GET_CODE (XEXP (exp, 0)) == NOT)
3352 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
3353 insn_code, insn_index);
3354 SIMPLIFY_ALTERNATIVE (left);
3355 return left;
3358 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3359 SIMPLIFY_ALTERNATIVE (left);
3360 if (GET_CODE (left) == NOT)
3361 return XEXP (left, 0);
3363 if (left == false_rtx)
3365 if (!ggc_p)
3366 obstack_free (rtl_obstack, spacer);
3367 return true_rtx;
3369 else if (left == true_rtx)
3371 if (!ggc_p)
3372 obstack_free (rtl_obstack, spacer);
3373 return false_rtx;
3376 /* Try to apply De`Morgan's laws. */
3377 else if (GET_CODE (left) == IOR)
3379 newexp = attr_rtx (AND,
3380 attr_rtx (NOT, XEXP (left, 0)),
3381 attr_rtx (NOT, XEXP (left, 1)));
3383 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3385 else if (GET_CODE (left) == AND)
3387 newexp = attr_rtx (IOR,
3388 attr_rtx (NOT, XEXP (left, 0)),
3389 attr_rtx (NOT, XEXP (left, 1)));
3391 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3393 else if (left != XEXP (exp, 0))
3395 newexp = attr_rtx (NOT, left);
3397 break;
3399 case EQ_ATTR:
3400 if (current_alternative_string && XSTR (exp, 0) == alternative_name)
3401 return (XSTR (exp, 1) == current_alternative_string
3402 ? true_rtx : false_rtx);
3404 /* Look at the value for this insn code in the specified attribute.
3405 We normally can replace this comparison with the condition that
3406 would give this insn the values being tested for. */
3407 if (XSTR (exp, 0) != alternative_name
3408 && (attr = find_attr (XSTR (exp, 0), 0)) != NULL)
3409 for (av = attr->first_value; av; av = av->next)
3410 for (ie = av->first_insn; ie; ie = ie->next)
3411 if (ie->insn_code == insn_code)
3412 return evaluate_eq_attr (exp, av->value, insn_code, insn_index);
3413 break;
3415 default:
3416 break;
3419 /* We have already simplified this expression. Simplifying it again
3420 won't buy anything unless we weren't given a valid insn code
3421 to process (i.e., we are canonicalizing something.). */
3422 if (insn_code != -2 /* Seems wrong: && current_alternative_string. */
3423 && ! RTX_UNCHANGING_P (newexp))
3424 return copy_rtx_unchanging (newexp);
3426 return newexp;
3429 /* Optimize the attribute lists by seeing if we can determine conditional
3430 values from the known values of other attributes. This will save subroutine
3431 calls during the compilation. */
3433 static void
3434 optimize_attrs ()
3436 struct attr_desc *attr;
3437 struct attr_value *av;
3438 struct insn_ent *ie;
3439 rtx newexp;
3440 int something_changed = 1;
3441 int i;
3442 struct attr_value_list { struct attr_value *av;
3443 struct insn_ent *ie;
3444 struct attr_desc * attr;
3445 struct attr_value_list *next; };
3446 struct attr_value_list **insn_code_values;
3447 struct attr_value_list *ivbuf;
3448 struct attr_value_list *iv;
3450 /* For each insn code, make a list of all the insn_ent's for it,
3451 for all values for all attributes. */
3453 if (num_insn_ents == 0)
3454 return;
3456 /* Make 2 extra elements, for "code" values -2 and -1. */
3457 insn_code_values
3458 = (struct attr_value_list **) alloca ((insn_code_number + 2)
3459 * sizeof (struct attr_value_list *));
3460 bzero ((char *) insn_code_values,
3461 (insn_code_number + 2) * sizeof (struct attr_value_list *));
3463 /* Offset the table address so we can index by -2 or -1. */
3464 insn_code_values += 2;
3466 /* Allocate the attr_value_list structures using xmalloc rather than
3467 alloca, because using alloca can overflow the maximum permitted
3468 stack limit on SPARC Lynx. */
3469 iv = ivbuf = ((struct attr_value_list *)
3470 xmalloc (num_insn_ents * sizeof (struct attr_value_list)));
3472 for (i = 0; i < MAX_ATTRS_INDEX; i++)
3473 for (attr = attrs[i]; attr; attr = attr->next)
3474 for (av = attr->first_value; av; av = av->next)
3475 for (ie = av->first_insn; ie; ie = ie->next)
3477 iv->attr = attr;
3478 iv->av = av;
3479 iv->ie = ie;
3480 iv->next = insn_code_values[ie->insn_code];
3481 insn_code_values[ie->insn_code] = iv;
3482 iv++;
3485 /* Sanity check on num_insn_ents. */
3486 if (iv != ivbuf + num_insn_ents)
3487 abort ();
3489 /* Process one insn code at a time. */
3490 for (i = -2; i < insn_code_number; i++)
3492 /* Clear the MEM_IN_STRUCT_P flag everywhere relevant.
3493 We use it to mean "already simplified for this insn". */
3494 for (iv = insn_code_values[i]; iv; iv = iv->next)
3495 clear_struct_flag (iv->av->value);
3497 /* Loop until nothing changes for one iteration. */
3498 something_changed = 1;
3499 while (something_changed)
3501 something_changed = 0;
3502 for (iv = insn_code_values[i]; iv; iv = iv->next)
3504 struct obstack *old = rtl_obstack;
3505 char *spacer = (char *) obstack_finish (temp_obstack);
3507 attr = iv->attr;
3508 av = iv->av;
3509 ie = iv->ie;
3510 if (GET_CODE (av->value) != COND)
3511 continue;
3513 rtl_obstack = temp_obstack;
3514 #if 0 /* This was intended as a speed up, but it was slower. */
3515 if (insn_n_alternatives[ie->insn_code] > 6
3516 && count_sub_rtxs (av->value, 200) >= 200)
3517 newexp = simplify_by_alternatives (av->value, ie->insn_code,
3518 ie->insn_index);
3519 else
3520 #endif
3521 newexp = simplify_cond (av->value, ie->insn_code,
3522 ie->insn_index);
3524 rtl_obstack = old;
3525 if (newexp != av->value)
3527 newexp = attr_copy_rtx (newexp);
3528 remove_insn_ent (av, ie);
3529 av = get_attr_value (newexp, attr, ie->insn_code);
3530 iv->av = av;
3531 insert_insn_ent (av, ie);
3532 something_changed = 1;
3534 if (!ggc_p)
3535 obstack_free (temp_obstack, spacer);
3540 free (ivbuf);
3543 #if 0
3544 static rtx
3545 simplify_by_alternatives (exp, insn_code, insn_index)
3546 rtx exp;
3547 int insn_code, insn_index;
3549 int i;
3550 int len = insn_n_alternatives[insn_code];
3551 rtx newexp = rtx_alloc (COND);
3552 rtx ultimate;
3555 XVEC (newexp, 0) = rtvec_alloc (len * 2);
3557 /* It will not matter what value we use as the default value
3558 of the new COND, since that default will never be used.
3559 Choose something of the right type. */
3560 for (ultimate = exp; GET_CODE (ultimate) == COND;)
3561 ultimate = XEXP (ultimate, 1);
3562 XEXP (newexp, 1) = ultimate;
3564 for (i = 0; i < insn_n_alternatives[insn_code]; i++)
3566 current_alternative_string = attr_numeral (i);
3567 XVECEXP (newexp, 0, i * 2) = make_alternative_compare (1 << i);
3568 XVECEXP (newexp, 0, i * 2 + 1)
3569 = simplify_cond (exp, insn_code, insn_index);
3572 current_alternative_string = 0;
3573 return simplify_cond (newexp, insn_code, insn_index);
3575 #endif
3577 /* If EXP is a suitable expression, reorganize it by constructing an
3578 equivalent expression that is a COND with the tests being all combinations
3579 of attribute values and the values being simple constants. */
3581 static rtx
3582 simplify_by_exploding (exp)
3583 rtx exp;
3585 rtx list = 0, link, condexp, defval = NULL_RTX;
3586 struct dimension *space;
3587 rtx *condtest, *condval;
3588 int i, j, total, ndim = 0;
3589 int most_tests, num_marks, new_marks;
3591 /* Locate all the EQ_ATTR expressions. */
3592 if (! find_and_mark_used_attributes (exp, &list, &ndim) || ndim == 0)
3594 unmark_used_attributes (list, 0, 0);
3595 return exp;
3598 /* Create an attribute space from the list of used attributes. For each
3599 dimension in the attribute space, record the attribute, list of values
3600 used, and number of values used. Add members to the list of values to
3601 cover the domain of the attribute. This makes the expanded COND form
3602 order independent. */
3604 space = (struct dimension *) alloca (ndim * sizeof (struct dimension));
3606 total = 1;
3607 for (ndim = 0; list; ndim++)
3609 /* Pull the first attribute value from the list and record that
3610 attribute as another dimension in the attribute space. */
3611 const char *name = XSTR (XEXP (list, 0), 0);
3612 rtx *prev;
3614 if ((space[ndim].attr = find_attr (name, 0)) == 0
3615 || space[ndim].attr->is_numeric)
3617 unmark_used_attributes (list, space, ndim);
3618 return exp;
3621 /* Add all remaining attribute values that refer to this attribute. */
3622 space[ndim].num_values = 0;
3623 space[ndim].values = 0;
3624 prev = &list;
3625 for (link = list; link; link = *prev)
3626 if (! strcmp (XSTR (XEXP (link, 0), 0), name))
3628 space[ndim].num_values++;
3629 *prev = XEXP (link, 1);
3630 XEXP (link, 1) = space[ndim].values;
3631 space[ndim].values = link;
3633 else
3634 prev = &XEXP (link, 1);
3636 /* Add sufficient members to the list of values to make the list
3637 mutually exclusive and record the total size of the attribute
3638 space. */
3639 total *= add_values_to_cover (&space[ndim]);
3642 /* Sort the attribute space so that the attributes go from non-constant
3643 to constant and from most values to least values. */
3644 for (i = 0; i < ndim; i++)
3645 for (j = ndim - 1; j > i; j--)
3646 if ((space[j-1].attr->is_const && !space[j].attr->is_const)
3647 || space[j-1].num_values < space[j].num_values)
3649 struct dimension tmp;
3650 tmp = space[j];
3651 space[j] = space[j-1];
3652 space[j-1] = tmp;
3655 /* Establish the initial current value. */
3656 for (i = 0; i < ndim; i++)
3657 space[i].current_value = space[i].values;
3659 condtest = (rtx *) alloca (total * sizeof (rtx));
3660 condval = (rtx *) alloca (total * sizeof (rtx));
3662 /* Expand the tests and values by iterating over all values in the
3663 attribute space. */
3664 for (i = 0;; i++)
3666 condtest[i] = test_for_current_value (space, ndim);
3667 condval[i] = simplify_with_current_value (exp, space, ndim);
3668 if (! increment_current_value (space, ndim))
3669 break;
3671 if (i != total - 1)
3672 abort ();
3674 /* We are now finished with the original expression. */
3675 unmark_used_attributes (0, space, ndim);
3677 /* Find the most used constant value and make that the default. */
3678 most_tests = -1;
3679 for (i = num_marks = 0; i < total; i++)
3680 if (GET_CODE (condval[i]) == CONST_STRING
3681 && ! MEM_VOLATILE_P (condval[i]))
3683 /* Mark the unmarked constant value and count how many are marked. */
3684 MEM_VOLATILE_P (condval[i]) = 1;
3685 for (j = new_marks = 0; j < total; j++)
3686 if (GET_CODE (condval[j]) == CONST_STRING
3687 && MEM_VOLATILE_P (condval[j]))
3688 new_marks++;
3689 if (new_marks - num_marks > most_tests)
3691 most_tests = new_marks - num_marks;
3692 defval = condval[i];
3694 num_marks = new_marks;
3696 /* Clear all the marks. */
3697 for (i = 0; i < total; i++)
3698 MEM_VOLATILE_P (condval[i]) = 0;
3700 /* Give up if nothing is constant. */
3701 if (num_marks == 0)
3702 return exp;
3704 /* If all values are the default, use that. */
3705 if (total == most_tests)
3706 return defval;
3708 /* Make a COND with the most common constant value the default. (A more
3709 complex method where tests with the same value were combined didn't
3710 seem to improve things.) */
3711 condexp = rtx_alloc (COND);
3712 XVEC (condexp, 0) = rtvec_alloc ((total - most_tests) * 2);
3713 XEXP (condexp, 1) = defval;
3714 for (i = j = 0; i < total; i++)
3715 if (condval[i] != defval)
3717 XVECEXP (condexp, 0, 2 * j) = condtest[i];
3718 XVECEXP (condexp, 0, 2 * j + 1) = condval[i];
3719 j++;
3722 return condexp;
3725 /* Set the MEM_VOLATILE_P flag for all EQ_ATTR expressions in EXP and
3726 verify that EXP can be simplified to a constant term if all the EQ_ATTR
3727 tests have known value. */
3729 static int
3730 find_and_mark_used_attributes (exp, terms, nterms)
3731 rtx exp, *terms;
3732 int *nterms;
3734 int i;
3736 switch (GET_CODE (exp))
3738 case EQ_ATTR:
3739 if (! MEM_VOLATILE_P (exp))
3741 rtx link = rtx_alloc (EXPR_LIST);
3742 XEXP (link, 0) = exp;
3743 XEXP (link, 1) = *terms;
3744 *terms = link;
3745 *nterms += 1;
3746 MEM_VOLATILE_P (exp) = 1;
3748 return 1;
3750 case CONST_STRING:
3751 case CONST_INT:
3752 return 1;
3754 case IF_THEN_ELSE:
3755 if (! find_and_mark_used_attributes (XEXP (exp, 2), terms, nterms))
3756 return 0;
3757 case IOR:
3758 case AND:
3759 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3760 return 0;
3761 case NOT:
3762 if (! find_and_mark_used_attributes (XEXP (exp, 0), terms, nterms))
3763 return 0;
3764 return 1;
3766 case COND:
3767 for (i = 0; i < XVECLEN (exp, 0); i++)
3768 if (! find_and_mark_used_attributes (XVECEXP (exp, 0, i), terms, nterms))
3769 return 0;
3770 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3771 return 0;
3772 return 1;
3774 default:
3775 return 0;
3779 /* Clear the MEM_VOLATILE_P flag in all EQ_ATTR expressions on LIST and
3780 in the values of the NDIM-dimensional attribute space SPACE. */
3782 static void
3783 unmark_used_attributes (list, space, ndim)
3784 rtx list;
3785 struct dimension *space;
3786 int ndim;
3788 rtx link, exp;
3789 int i;
3791 for (i = 0; i < ndim; i++)
3792 unmark_used_attributes (space[i].values, 0, 0);
3794 for (link = list; link; link = XEXP (link, 1))
3796 exp = XEXP (link, 0);
3797 if (GET_CODE (exp) == EQ_ATTR)
3798 MEM_VOLATILE_P (exp) = 0;
3802 /* Update the attribute dimension DIM so that all values of the attribute
3803 are tested. Return the updated number of values. */
3805 static int
3806 add_values_to_cover (dim)
3807 struct dimension *dim;
3809 struct attr_value *av;
3810 rtx exp, link, *prev;
3811 int nalt = 0;
3813 for (av = dim->attr->first_value; av; av = av->next)
3814 if (GET_CODE (av->value) == CONST_STRING)
3815 nalt++;
3817 if (nalt < dim->num_values)
3818 abort ();
3819 else if (nalt == dim->num_values)
3820 ; /* Ok. */
3821 else if (nalt * 2 < dim->num_values * 3)
3823 /* Most all the values of the attribute are used, so add all the unused
3824 values. */
3825 prev = &dim->values;
3826 for (link = dim->values; link; link = *prev)
3827 prev = &XEXP (link, 1);
3829 for (av = dim->attr->first_value; av; av = av->next)
3830 if (GET_CODE (av->value) == CONST_STRING)
3832 exp = attr_eq (dim->attr->name, XSTR (av->value, 0));
3833 if (MEM_VOLATILE_P (exp))
3834 continue;
3836 link = rtx_alloc (EXPR_LIST);
3837 XEXP (link, 0) = exp;
3838 XEXP (link, 1) = 0;
3839 *prev = link;
3840 prev = &XEXP (link, 1);
3842 dim->num_values = nalt;
3844 else
3846 rtx orexp = false_rtx;
3848 /* Very few values are used, so compute a mutually exclusive
3849 expression. (We could do this for numeric values if that becomes
3850 important.) */
3851 prev = &dim->values;
3852 for (link = dim->values; link; link = *prev)
3854 orexp = insert_right_side (IOR, orexp, XEXP (link, 0), -2, -2);
3855 prev = &XEXP (link, 1);
3857 link = rtx_alloc (EXPR_LIST);
3858 XEXP (link, 0) = attr_rtx (NOT, orexp);
3859 XEXP (link, 1) = 0;
3860 *prev = link;
3861 dim->num_values++;
3863 return dim->num_values;
3866 /* Increment the current value for the NDIM-dimensional attribute space SPACE
3867 and return FALSE if the increment overflowed. */
3869 static int
3870 increment_current_value (space, ndim)
3871 struct dimension *space;
3872 int ndim;
3874 int i;
3876 for (i = ndim - 1; i >= 0; i--)
3878 if ((space[i].current_value = XEXP (space[i].current_value, 1)) == 0)
3879 space[i].current_value = space[i].values;
3880 else
3881 return 1;
3883 return 0;
3886 /* Construct an expression corresponding to the current value for the
3887 NDIM-dimensional attribute space SPACE. */
3889 static rtx
3890 test_for_current_value (space, ndim)
3891 struct dimension *space;
3892 int ndim;
3894 int i;
3895 rtx exp = true_rtx;
3897 for (i = 0; i < ndim; i++)
3898 exp = insert_right_side (AND, exp, XEXP (space[i].current_value, 0),
3899 -2, -2);
3901 return exp;
3904 /* Given the current value of the NDIM-dimensional attribute space SPACE,
3905 set the corresponding EQ_ATTR expressions to that value and reduce
3906 the expression EXP as much as possible. On input [and output], all
3907 known EQ_ATTR expressions are set to FALSE. */
3909 static rtx
3910 simplify_with_current_value (exp, space, ndim)
3911 rtx exp;
3912 struct dimension *space;
3913 int ndim;
3915 int i;
3916 rtx x;
3918 /* Mark each current value as TRUE. */
3919 for (i = 0; i < ndim; i++)
3921 x = XEXP (space[i].current_value, 0);
3922 if (GET_CODE (x) == EQ_ATTR)
3923 MEM_VOLATILE_P (x) = 0;
3926 exp = simplify_with_current_value_aux (exp);
3928 /* Change each current value back to FALSE. */
3929 for (i = 0; i < ndim; i++)
3931 x = XEXP (space[i].current_value, 0);
3932 if (GET_CODE (x) == EQ_ATTR)
3933 MEM_VOLATILE_P (x) = 1;
3936 return exp;
3939 /* Reduce the expression EXP based on the MEM_VOLATILE_P settings of
3940 all EQ_ATTR expressions. */
3942 static rtx
3943 simplify_with_current_value_aux (exp)
3944 rtx exp;
3946 register int i;
3947 rtx cond;
3949 switch (GET_CODE (exp))
3951 case EQ_ATTR:
3952 if (MEM_VOLATILE_P (exp))
3953 return false_rtx;
3954 else
3955 return true_rtx;
3956 case CONST_STRING:
3957 case CONST_INT:
3958 return exp;
3960 case IF_THEN_ELSE:
3961 cond = simplify_with_current_value_aux (XEXP (exp, 0));
3962 if (cond == true_rtx)
3963 return simplify_with_current_value_aux (XEXP (exp, 1));
3964 else if (cond == false_rtx)
3965 return simplify_with_current_value_aux (XEXP (exp, 2));
3966 else
3967 return attr_rtx (IF_THEN_ELSE, cond,
3968 simplify_with_current_value_aux (XEXP (exp, 1)),
3969 simplify_with_current_value_aux (XEXP (exp, 2)));
3971 case IOR:
3972 cond = simplify_with_current_value_aux (XEXP (exp, 1));
3973 if (cond == true_rtx)
3974 return cond;
3975 else if (cond == false_rtx)
3976 return simplify_with_current_value_aux (XEXP (exp, 0));
3977 else
3978 return attr_rtx (IOR, cond,
3979 simplify_with_current_value_aux (XEXP (exp, 0)));
3981 case AND:
3982 cond = simplify_with_current_value_aux (XEXP (exp, 1));
3983 if (cond == true_rtx)
3984 return simplify_with_current_value_aux (XEXP (exp, 0));
3985 else if (cond == false_rtx)
3986 return cond;
3987 else
3988 return attr_rtx (AND, cond,
3989 simplify_with_current_value_aux (XEXP (exp, 0)));
3991 case NOT:
3992 cond = simplify_with_current_value_aux (XEXP (exp, 0));
3993 if (cond == true_rtx)
3994 return false_rtx;
3995 else if (cond == false_rtx)
3996 return true_rtx;
3997 else
3998 return attr_rtx (NOT, cond);
4000 case COND:
4001 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4003 cond = simplify_with_current_value_aux (XVECEXP (exp, 0, i));
4004 if (cond == true_rtx)
4005 return simplify_with_current_value_aux (XVECEXP (exp, 0, i + 1));
4006 else if (cond == false_rtx)
4007 continue;
4008 else
4009 abort (); /* With all EQ_ATTR's of known value, a case should
4010 have been selected. */
4012 return simplify_with_current_value_aux (XEXP (exp, 1));
4014 default:
4015 abort ();
4019 /* Clear the MEM_IN_STRUCT_P flag in EXP and its subexpressions. */
4021 static void
4022 clear_struct_flag (x)
4023 rtx x;
4025 register int i;
4026 register int j;
4027 register enum rtx_code code;
4028 register const char *fmt;
4030 MEM_IN_STRUCT_P (x) = 0;
4031 if (RTX_UNCHANGING_P (x))
4032 return;
4034 code = GET_CODE (x);
4036 switch (code)
4038 case REG:
4039 case QUEUED:
4040 case CONST_INT:
4041 case CONST_DOUBLE:
4042 case SYMBOL_REF:
4043 case CODE_LABEL:
4044 case PC:
4045 case CC0:
4046 case EQ_ATTR:
4047 case ATTR_FLAG:
4048 return;
4050 default:
4051 break;
4054 /* Compare the elements. If any pair of corresponding elements
4055 fail to match, return 0 for the whole things. */
4057 fmt = GET_RTX_FORMAT (code);
4058 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4060 switch (fmt[i])
4062 case 'V':
4063 case 'E':
4064 for (j = 0; j < XVECLEN (x, i); j++)
4065 clear_struct_flag (XVECEXP (x, i, j));
4066 break;
4068 case 'e':
4069 clear_struct_flag (XEXP (x, i));
4070 break;
4075 /* Return the number of RTX objects making up the expression X.
4076 But if we count more than MAX objects, stop counting. */
4078 static int
4079 count_sub_rtxs (x, max)
4080 rtx x;
4081 int max;
4083 register int i;
4084 register int j;
4085 register enum rtx_code code;
4086 register const char *fmt;
4087 int total = 0;
4089 code = GET_CODE (x);
4091 switch (code)
4093 case REG:
4094 case QUEUED:
4095 case CONST_INT:
4096 case CONST_DOUBLE:
4097 case SYMBOL_REF:
4098 case CODE_LABEL:
4099 case PC:
4100 case CC0:
4101 case EQ_ATTR:
4102 case ATTR_FLAG:
4103 return 1;
4105 default:
4106 break;
4109 /* Compare the elements. If any pair of corresponding elements
4110 fail to match, return 0 for the whole things. */
4112 fmt = GET_RTX_FORMAT (code);
4113 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4115 if (total >= max)
4116 return total;
4118 switch (fmt[i])
4120 case 'V':
4121 case 'E':
4122 for (j = 0; j < XVECLEN (x, i); j++)
4123 total += count_sub_rtxs (XVECEXP (x, i, j), max);
4124 break;
4126 case 'e':
4127 total += count_sub_rtxs (XEXP (x, i), max);
4128 break;
4131 return total;
4135 /* Create table entries for DEFINE_ATTR. */
4137 static void
4138 gen_attr (exp)
4139 rtx exp;
4141 struct attr_desc *attr;
4142 struct attr_value *av;
4143 const char *name_ptr;
4144 char *p;
4146 /* Make a new attribute structure. Check for duplicate by looking at
4147 attr->default_val, since it is initialized by this routine. */
4148 attr = find_attr (XSTR (exp, 0), 1);
4149 if (attr->default_val)
4150 fatal ("Duplicate definition for `%s' attribute", attr->name);
4152 if (*XSTR (exp, 1) == '\0')
4153 attr->is_numeric = 1;
4154 else
4156 name_ptr = XSTR (exp, 1);
4157 while ((p = next_comma_elt (&name_ptr)) != NULL)
4159 av = (struct attr_value *) oballoc (sizeof (struct attr_value));
4160 av->value = attr_rtx (CONST_STRING, p);
4161 av->next = attr->first_value;
4162 attr->first_value = av;
4163 av->first_insn = NULL;
4164 av->num_insns = 0;
4165 av->has_asm_insn = 0;
4169 if (GET_CODE (XEXP (exp, 2)) == CONST)
4171 attr->is_const = 1;
4172 if (attr->is_numeric)
4173 fatal ("Constant attributes may not take numeric values");
4174 /* Get rid of the CONST node. It is allowed only at top-level. */
4175 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
4178 if (! strcmp (attr->name, "length") && ! attr->is_numeric)
4179 fatal ("`length' attribute must take numeric values");
4181 /* Set up the default value. */
4182 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
4183 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
4186 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
4187 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
4188 number of alternatives as this should be checked elsewhere. */
4190 static int
4191 count_alternatives (exp)
4192 rtx exp;
4194 int i, j, n;
4195 const char *fmt;
4197 if (GET_CODE (exp) == MATCH_OPERAND)
4198 return n_comma_elts (XSTR (exp, 2));
4200 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4201 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4202 switch (*fmt++)
4204 case 'e':
4205 case 'u':
4206 n = count_alternatives (XEXP (exp, i));
4207 if (n)
4208 return n;
4209 break;
4211 case 'E':
4212 case 'V':
4213 if (XVEC (exp, i) != NULL)
4214 for (j = 0; j < XVECLEN (exp, i); j++)
4216 n = count_alternatives (XVECEXP (exp, i, j));
4217 if (n)
4218 return n;
4222 return 0;
4225 /* Returns non-zero if the given expression contains an EQ_ATTR with the
4226 `alternative' attribute. */
4228 static int
4229 compares_alternatives_p (exp)
4230 rtx exp;
4232 int i, j;
4233 const char *fmt;
4235 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
4236 return 1;
4238 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4239 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4240 switch (*fmt++)
4242 case 'e':
4243 case 'u':
4244 if (compares_alternatives_p (XEXP (exp, i)))
4245 return 1;
4246 break;
4248 case 'E':
4249 for (j = 0; j < XVECLEN (exp, i); j++)
4250 if (compares_alternatives_p (XVECEXP (exp, i, j)))
4251 return 1;
4252 break;
4255 return 0;
4258 /* Returns non-zero is INNER is contained in EXP. */
4260 static int
4261 contained_in_p (inner, exp)
4262 rtx inner;
4263 rtx exp;
4265 int i, j;
4266 const char *fmt;
4268 if (rtx_equal_p (inner, exp))
4269 return 1;
4271 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4272 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4273 switch (*fmt++)
4275 case 'e':
4276 case 'u':
4277 if (contained_in_p (inner, XEXP (exp, i)))
4278 return 1;
4279 break;
4281 case 'E':
4282 for (j = 0; j < XVECLEN (exp, i); j++)
4283 if (contained_in_p (inner, XVECEXP (exp, i, j)))
4284 return 1;
4285 break;
4288 return 0;
4291 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
4293 static void
4294 gen_insn (exp)
4295 rtx exp;
4297 struct insn_def *id;
4299 id = (struct insn_def *) oballoc (sizeof (struct insn_def));
4300 id->next = defs;
4301 defs = id;
4302 id->def = exp;
4304 switch (GET_CODE (exp))
4306 case DEFINE_INSN:
4307 id->insn_code = insn_code_number;
4308 id->insn_index = insn_index_number;
4309 id->num_alternatives = count_alternatives (exp);
4310 if (id->num_alternatives == 0)
4311 id->num_alternatives = 1;
4312 id->vec_idx = 4;
4313 break;
4315 case DEFINE_PEEPHOLE:
4316 id->insn_code = insn_code_number;
4317 id->insn_index = insn_index_number;
4318 id->num_alternatives = count_alternatives (exp);
4319 if (id->num_alternatives == 0)
4320 id->num_alternatives = 1;
4321 id->vec_idx = 3;
4322 break;
4324 case DEFINE_ASM_ATTRIBUTES:
4325 id->insn_code = -1;
4326 id->insn_index = -1;
4327 id->num_alternatives = 1;
4328 id->vec_idx = 0;
4329 got_define_asm_attributes = 1;
4330 break;
4332 default:
4333 abort ();
4337 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
4338 true or annul false is specified, and make a `struct delay_desc'. */
4340 static void
4341 gen_delay (def)
4342 rtx def;
4344 struct delay_desc *delay;
4345 int i;
4347 if (XVECLEN (def, 1) % 3 != 0)
4348 fatal ("Number of elements in DEFINE_DELAY must be multiple of three.");
4350 for (i = 0; i < XVECLEN (def, 1); i += 3)
4352 if (XVECEXP (def, 1, i + 1))
4353 have_annul_true = 1;
4354 if (XVECEXP (def, 1, i + 2))
4355 have_annul_false = 1;
4358 delay = (struct delay_desc *) oballoc (sizeof (struct delay_desc));
4359 delay->def = def;
4360 delay->num = ++num_delays;
4361 delay->next = delays;
4362 delays = delay;
4365 /* Process a DEFINE_FUNCTION_UNIT.
4367 This gives information about a function unit contained in the CPU.
4368 We fill in a `struct function_unit_op' and a `struct function_unit'
4369 with information used later by `expand_unit'. */
4371 static void
4372 gen_unit (def)
4373 rtx def;
4375 struct function_unit *unit;
4376 struct function_unit_op *op;
4377 const char *name = XSTR (def, 0);
4378 int multiplicity = XINT (def, 1);
4379 int simultaneity = XINT (def, 2);
4380 rtx condexp = XEXP (def, 3);
4381 int ready_cost = MAX (XINT (def, 4), 1);
4382 int issue_delay = MAX (XINT (def, 5), 1);
4384 /* See if we have already seen this function unit. If so, check that
4385 the multiplicity and simultaneity values are the same. If not, make
4386 a structure for this function unit. */
4387 for (unit = units; unit; unit = unit->next)
4388 if (! strcmp (unit->name, name))
4390 if (unit->multiplicity != multiplicity
4391 || unit->simultaneity != simultaneity)
4392 fatal ("Differing specifications given for `%s' function unit.",
4393 unit->name);
4394 break;
4397 if (unit == 0)
4399 unit = (struct function_unit *) oballoc (sizeof (struct function_unit));
4400 unit->name = name;
4401 unit->multiplicity = multiplicity;
4402 unit->simultaneity = simultaneity;
4403 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
4404 unit->num = num_units++;
4405 unit->num_opclasses = 0;
4406 unit->condexp = false_rtx;
4407 unit->ops = 0;
4408 unit->next = units;
4409 units = unit;
4412 /* Make a new operation class structure entry and initialize it. */
4413 op = (struct function_unit_op *) oballoc (sizeof (struct function_unit_op));
4414 op->condexp = condexp;
4415 op->num = unit->num_opclasses++;
4416 op->ready = ready_cost;
4417 op->issue_delay = issue_delay;
4418 op->next = unit->ops;
4419 unit->ops = op;
4420 num_unit_opclasses++;
4422 /* Set our issue expression based on whether or not an optional conflict
4423 vector was specified. */
4424 if (XVEC (def, 6))
4426 /* Compute the IOR of all the specified expressions. */
4427 rtx orexp = false_rtx;
4428 int i;
4430 for (i = 0; i < XVECLEN (def, 6); i++)
4431 orexp = insert_right_side (IOR, orexp, XVECEXP (def, 6, i), -2, -2);
4433 op->conflict_exp = orexp;
4434 extend_range (&unit->issue_delay, 1, issue_delay);
4436 else
4438 op->conflict_exp = true_rtx;
4439 extend_range (&unit->issue_delay, issue_delay, issue_delay);
4442 /* Merge our conditional into that of the function unit so we can determine
4443 which insns are used by the function unit. */
4444 unit->condexp = insert_right_side (IOR, unit->condexp, op->condexp, -2, -2);
4447 /* Given a piece of RTX, print a C expression to test its truth value.
4448 We use AND and IOR both for logical and bit-wise operations, so
4449 interpret them as logical unless they are inside a comparison expression.
4450 The first bit of FLAGS will be non-zero in that case.
4452 Set the second bit of FLAGS to make references to attribute values use
4453 a cached local variable instead of calling a function. */
4455 static void
4456 write_test_expr (exp, flags)
4457 rtx exp;
4458 int flags;
4460 int comparison_operator = 0;
4461 RTX_CODE code;
4462 struct attr_desc *attr;
4464 /* In order not to worry about operator precedence, surround our part of
4465 the expression with parentheses. */
4467 printf ("(");
4468 code = GET_CODE (exp);
4469 switch (code)
4471 /* Binary operators. */
4472 case EQ: case NE:
4473 case GE: case GT: case GEU: case GTU:
4474 case LE: case LT: case LEU: case LTU:
4475 comparison_operator = 1;
4477 case PLUS: case MINUS: case MULT: case DIV: case MOD:
4478 case AND: case IOR: case XOR:
4479 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4480 write_test_expr (XEXP (exp, 0), flags | comparison_operator);
4481 switch (code)
4483 case EQ:
4484 printf (" == ");
4485 break;
4486 case NE:
4487 printf (" != ");
4488 break;
4489 case GE:
4490 printf (" >= ");
4491 break;
4492 case GT:
4493 printf (" > ");
4494 break;
4495 case GEU:
4496 printf (" >= (unsigned) ");
4497 break;
4498 case GTU:
4499 printf (" > (unsigned) ");
4500 break;
4501 case LE:
4502 printf (" <= ");
4503 break;
4504 case LT:
4505 printf (" < ");
4506 break;
4507 case LEU:
4508 printf (" <= (unsigned) ");
4509 break;
4510 case LTU:
4511 printf (" < (unsigned) ");
4512 break;
4513 case PLUS:
4514 printf (" + ");
4515 break;
4516 case MINUS:
4517 printf (" - ");
4518 break;
4519 case MULT:
4520 printf (" * ");
4521 break;
4522 case DIV:
4523 printf (" / ");
4524 break;
4525 case MOD:
4526 printf (" %% ");
4527 break;
4528 case AND:
4529 if (flags & 1)
4530 printf (" & ");
4531 else
4532 printf (" && ");
4533 break;
4534 case IOR:
4535 if (flags & 1)
4536 printf (" | ");
4537 else
4538 printf (" || ");
4539 break;
4540 case XOR:
4541 printf (" ^ ");
4542 break;
4543 case ASHIFT:
4544 printf (" << ");
4545 break;
4546 case LSHIFTRT:
4547 case ASHIFTRT:
4548 printf (" >> ");
4549 break;
4550 default:
4551 abort ();
4554 write_test_expr (XEXP (exp, 1), flags | comparison_operator);
4555 break;
4557 case NOT:
4558 /* Special-case (not (eq_attrq "alternative" "x")) */
4559 if (! (flags & 1) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
4560 && XSTR (XEXP (exp, 0), 0) == alternative_name)
4562 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
4563 break;
4566 /* Otherwise, fall through to normal unary operator. */
4568 /* Unary operators. */
4569 case ABS: case NEG:
4570 switch (code)
4572 case NOT:
4573 if (flags & 1)
4574 printf ("~ ");
4575 else
4576 printf ("! ");
4577 break;
4578 case ABS:
4579 printf ("abs ");
4580 break;
4581 case NEG:
4582 printf ("-");
4583 break;
4584 default:
4585 abort ();
4588 write_test_expr (XEXP (exp, 0), flags);
4589 break;
4591 /* Comparison test of an attribute with a value. Most of these will
4592 have been removed by optimization. Handle "alternative"
4593 specially and give error if EQ_ATTR present inside a comparison. */
4594 case EQ_ATTR:
4595 if (flags & 1)
4596 fatal ("EQ_ATTR not valid inside comparison");
4598 if (XSTR (exp, 0) == alternative_name)
4600 printf ("which_alternative == %s", XSTR (exp, 1));
4601 break;
4604 attr = find_attr (XSTR (exp, 0), 0);
4605 if (! attr) abort ();
4607 /* Now is the time to expand the value of a constant attribute. */
4608 if (attr->is_const)
4610 write_test_expr (evaluate_eq_attr (exp, attr->default_val->value,
4611 -2, -2),
4612 flags);
4614 else
4616 if (flags & 2)
4617 printf ("attr_%s", attr->name);
4618 else
4619 printf ("get_attr_%s (insn)", attr->name);
4620 printf (" == ");
4621 write_attr_valueq (attr, XSTR (exp, 1));
4623 break;
4625 /* Comparison test of flags for define_delays. */
4626 case ATTR_FLAG:
4627 if (flags & 1)
4628 fatal ("ATTR_FLAG not valid inside comparison");
4629 printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
4630 break;
4632 /* See if an operand matches a predicate. */
4633 case MATCH_OPERAND:
4634 /* If only a mode is given, just ensure the mode matches the operand.
4635 If neither a mode nor predicate is given, error. */
4636 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
4638 if (GET_MODE (exp) == VOIDmode)
4639 fatal ("Null MATCH_OPERAND specified as test");
4640 else
4641 printf ("GET_MODE (operands[%d]) == %smode",
4642 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4644 else
4645 printf ("%s (operands[%d], %smode)",
4646 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4647 break;
4649 case MATCH_INSN:
4650 printf ("%s (insn)", XSTR (exp, 0));
4651 break;
4653 /* Constant integer. */
4654 case CONST_INT:
4655 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
4656 break;
4658 /* A random C expression. */
4659 case SYMBOL_REF:
4660 printf ("%s", XSTR (exp, 0));
4661 break;
4663 /* The address of the branch target. */
4664 case MATCH_DUP:
4665 printf ("insn_addresses[INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])]",
4666 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
4667 break;
4669 case PC:
4670 /* The address of the current insn. We implement this actually as the
4671 address of the current insn for backward branches, but the last
4672 address of the next insn for forward branches, and both with
4673 adjustments that account for the worst-case possible stretching of
4674 intervening alignments between this insn and its destination. */
4675 printf("insn_current_reference_address (insn)");
4676 break;
4678 case CONST_STRING:
4679 printf ("%s", XSTR (exp, 0));
4680 break;
4682 case IF_THEN_ELSE:
4683 write_test_expr (XEXP (exp, 0), flags & 2);
4684 printf (" ? ");
4685 write_test_expr (XEXP (exp, 1), flags | 1);
4686 printf (" : ");
4687 write_test_expr (XEXP (exp, 2), flags | 1);
4688 break;
4690 default:
4691 fatal ("bad RTX code `%s' in attribute calculation\n",
4692 GET_RTX_NAME (code));
4695 printf (")");
4698 /* Given an attribute value, return the maximum CONST_STRING argument
4699 encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
4701 static int
4702 max_attr_value (exp, unknownp)
4703 rtx exp;
4704 int *unknownp;
4706 int current_max;
4707 int i, n;
4709 switch (GET_CODE (exp))
4711 case CONST_STRING:
4712 current_max = atoi (XSTR (exp, 0));
4713 break;
4715 case COND:
4716 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4717 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4719 n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4720 if (n > current_max)
4721 current_max = n;
4723 break;
4725 case IF_THEN_ELSE:
4726 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4727 n = max_attr_value (XEXP (exp, 2), unknownp);
4728 if (n > current_max)
4729 current_max = n;
4730 break;
4732 default:
4733 *unknownp = 1;
4734 current_max = INT_MAX;
4735 break;
4738 return current_max;
4741 /* Given an attribute value, return the result of ORing together all
4742 CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
4743 if the numeric value is not known. */
4745 static int
4746 or_attr_value (exp, unknownp)
4747 rtx exp;
4748 int *unknownp;
4750 int current_or;
4751 int i;
4753 switch (GET_CODE (exp))
4755 case CONST_STRING:
4756 current_or = atoi (XSTR (exp, 0));
4757 break;
4759 case COND:
4760 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4761 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4762 current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4763 break;
4765 case IF_THEN_ELSE:
4766 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4767 current_or |= or_attr_value (XEXP (exp, 2), unknownp);
4768 break;
4770 default:
4771 *unknownp = 1;
4772 current_or = -1;
4773 break;
4776 return current_or;
4779 /* Scan an attribute value, possibly a conditional, and record what actions
4780 will be required to do any conditional tests in it.
4782 Specifically, set
4783 `must_extract' if we need to extract the insn operands
4784 `must_constrain' if we must compute `which_alternative'
4785 `address_used' if an address expression was used
4786 `length_used' if an (eq_attr "length" ...) was used
4789 static void
4790 walk_attr_value (exp)
4791 rtx exp;
4793 register int i, j;
4794 register const char *fmt;
4795 RTX_CODE code;
4797 if (exp == NULL)
4798 return;
4800 code = GET_CODE (exp);
4801 switch (code)
4803 case SYMBOL_REF:
4804 if (! RTX_UNCHANGING_P (exp))
4805 /* Since this is an arbitrary expression, it can look at anything.
4806 However, constant expressions do not depend on any particular
4807 insn. */
4808 must_extract = must_constrain = 1;
4809 return;
4811 case MATCH_OPERAND:
4812 must_extract = 1;
4813 return;
4815 case EQ_ATTR:
4816 if (XSTR (exp, 0) == alternative_name)
4817 must_extract = must_constrain = 1;
4818 else if (strcmp (XSTR (exp, 0), "length") == 0)
4819 length_used = 1;
4820 return;
4822 case MATCH_DUP:
4823 must_extract = 1;
4824 address_used = 1;
4825 return;
4827 case PC:
4828 address_used = 1;
4829 return;
4831 case ATTR_FLAG:
4832 return;
4834 default:
4835 break;
4838 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
4839 switch (*fmt++)
4841 case 'e':
4842 case 'u':
4843 walk_attr_value (XEXP (exp, i));
4844 break;
4846 case 'E':
4847 if (XVEC (exp, i) != NULL)
4848 for (j = 0; j < XVECLEN (exp, i); j++)
4849 walk_attr_value (XVECEXP (exp, i, j));
4850 break;
4854 /* Write out a function to obtain the attribute for a given INSN. */
4856 static void
4857 write_attr_get (attr)
4858 struct attr_desc *attr;
4860 struct attr_value *av, *common_av;
4862 /* Find the most used attribute value. Handle that as the `default' of the
4863 switch we will generate. */
4864 common_av = find_most_used (attr);
4866 /* Write out prototype of function. */
4867 if (!attr->is_numeric)
4868 printf ("extern enum attr_%s ", attr->name);
4869 else if (attr->unsigned_p)
4870 printf ("extern unsigned int ");
4871 else
4872 printf ("extern int ");
4873 /* If the attribute name starts with a star, the remainder is the name of
4874 the subroutine to use, instead of `get_attr_...'. */
4875 if (attr->name[0] == '*')
4876 printf ("%s PARAMS ((rtx));\n", &attr->name[1]);
4877 else
4878 printf ("get_attr_%s PARAMS ((%s));\n", attr->name,
4879 (attr->is_const ? "void" : "rtx"));
4881 /* Write out start of function, then all values with explicit `case' lines,
4882 then a `default', then the value with the most uses. */
4883 if (!attr->is_numeric)
4884 printf ("enum attr_%s\n", attr->name);
4885 else if (attr->unsigned_p)
4886 printf ("unsigned int\n");
4887 else
4888 printf ("int\n");
4890 /* If the attribute name starts with a star, the remainder is the name of
4891 the subroutine to use, instead of `get_attr_...'. */
4892 if (attr->name[0] == '*')
4893 printf ("%s (insn)\n", &attr->name[1]);
4894 else if (attr->is_const == 0)
4895 printf ("get_attr_%s (insn)\n", attr->name);
4896 else
4898 printf ("get_attr_%s ()\n", attr->name);
4899 printf ("{\n");
4901 for (av = attr->first_value; av; av = av->next)
4902 if (av->num_insns != 0)
4903 write_attr_set (attr, 2, av->value, "return", ";",
4904 true_rtx, av->first_insn->insn_code,
4905 av->first_insn->insn_index);
4907 printf ("}\n\n");
4908 return;
4911 printf (" rtx insn;\n");
4912 printf ("{\n");
4914 if (GET_CODE (common_av->value) == FFS)
4916 rtx p = XEXP (common_av->value, 0);
4918 /* No need to emit code to abort if the insn is unrecognized; the
4919 other get_attr_foo functions will do that when we call them. */
4921 write_toplevel_expr (p);
4923 printf ("\n if (accum && accum == (accum & -accum))\n");
4924 printf (" {\n");
4925 printf (" int i;\n");
4926 printf (" for (i = 0; accum >>= 1; ++i) continue;\n");
4927 printf (" accum = i;\n");
4928 printf (" }\n else\n");
4929 printf (" accum = ~accum;\n");
4930 printf (" return accum;\n}\n\n");
4932 else
4934 printf (" switch (recog_memoized (insn))\n");
4935 printf (" {\n");
4937 for (av = attr->first_value; av; av = av->next)
4938 if (av != common_av)
4939 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
4941 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
4942 printf (" }\n}\n\n");
4946 /* Given an AND tree of known true terms (because we are inside an `if' with
4947 that as the condition or are in an `else' clause) and an expression,
4948 replace any known true terms with TRUE. Use `simplify_and_tree' to do
4949 the bulk of the work. */
4951 static rtx
4952 eliminate_known_true (known_true, exp, insn_code, insn_index)
4953 rtx known_true;
4954 rtx exp;
4955 int insn_code, insn_index;
4957 rtx term;
4959 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
4961 if (GET_CODE (known_true) == AND)
4963 exp = eliminate_known_true (XEXP (known_true, 0), exp,
4964 insn_code, insn_index);
4965 exp = eliminate_known_true (XEXP (known_true, 1), exp,
4966 insn_code, insn_index);
4968 else
4970 term = known_true;
4971 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
4974 return exp;
4977 /* Write out a series of tests and assignment statements to perform tests and
4978 sets of an attribute value. We are passed an indentation amount and prefix
4979 and suffix strings to write around each attribute value (e.g., "return"
4980 and ";"). */
4982 static void
4983 write_attr_set (attr, indent, value, prefix, suffix, known_true,
4984 insn_code, insn_index)
4985 struct attr_desc *attr;
4986 int indent;
4987 rtx value;
4988 const char *prefix;
4989 const char *suffix;
4990 rtx known_true;
4991 int insn_code, insn_index;
4993 if (GET_CODE (value) == COND)
4995 /* Assume the default value will be the default of the COND unless we
4996 find an always true expression. */
4997 rtx default_val = XEXP (value, 1);
4998 rtx our_known_true = known_true;
4999 rtx newexp;
5000 int first_if = 1;
5001 int i;
5003 for (i = 0; i < XVECLEN (value, 0); i += 2)
5005 rtx testexp;
5006 rtx inner_true;
5008 testexp = eliminate_known_true (our_known_true,
5009 XVECEXP (value, 0, i),
5010 insn_code, insn_index);
5011 newexp = attr_rtx (NOT, testexp);
5012 newexp = insert_right_side (AND, our_known_true, newexp,
5013 insn_code, insn_index);
5015 /* If the test expression is always true or if the next `known_true'
5016 expression is always false, this is the last case, so break
5017 out and let this value be the `else' case. */
5018 if (testexp == true_rtx || newexp == false_rtx)
5020 default_val = XVECEXP (value, 0, i + 1);
5021 break;
5024 /* Compute the expression to pass to our recursive call as being
5025 known true. */
5026 inner_true = insert_right_side (AND, our_known_true,
5027 testexp, insn_code, insn_index);
5029 /* If this is always false, skip it. */
5030 if (inner_true == false_rtx)
5031 continue;
5033 write_indent (indent);
5034 printf ("%sif ", first_if ? "" : "else ");
5035 first_if = 0;
5036 write_test_expr (testexp, 0);
5037 printf ("\n");
5038 write_indent (indent + 2);
5039 printf ("{\n");
5041 write_attr_set (attr, indent + 4,
5042 XVECEXP (value, 0, i + 1), prefix, suffix,
5043 inner_true, insn_code, insn_index);
5044 write_indent (indent + 2);
5045 printf ("}\n");
5046 our_known_true = newexp;
5049 if (! first_if)
5051 write_indent (indent);
5052 printf ("else\n");
5053 write_indent (indent + 2);
5054 printf ("{\n");
5057 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
5058 prefix, suffix, our_known_true, insn_code, insn_index);
5060 if (! first_if)
5062 write_indent (indent + 2);
5063 printf ("}\n");
5066 else
5068 write_indent (indent);
5069 printf ("%s ", prefix);
5070 write_attr_value (attr, value);
5071 printf ("%s\n", suffix);
5075 /* Write out the computation for one attribute value. */
5077 static void
5078 write_attr_case (attr, av, write_case_lines, prefix, suffix, indent,
5079 known_true)
5080 struct attr_desc *attr;
5081 struct attr_value *av;
5082 int write_case_lines;
5083 const char *prefix, *suffix;
5084 int indent;
5085 rtx known_true;
5087 struct insn_ent *ie;
5089 if (av->num_insns == 0)
5090 return;
5092 if (av->has_asm_insn)
5094 write_indent (indent);
5095 printf ("case -1:\n");
5096 write_indent (indent + 2);
5097 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
5098 write_indent (indent + 2);
5099 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
5100 write_indent (indent + 2);
5101 printf (" fatal_insn_not_found (insn);\n");
5104 if (write_case_lines)
5106 for (ie = av->first_insn; ie; ie = ie->next)
5107 if (ie->insn_code != -1)
5109 write_indent (indent);
5110 printf ("case %d:\n", ie->insn_code);
5113 else
5115 write_indent (indent);
5116 printf ("default:\n");
5119 /* See what we have to do to output this value. */
5120 must_extract = must_constrain = address_used = 0;
5121 walk_attr_value (av->value);
5123 if (must_extract)
5125 write_indent (indent + 2);
5126 printf ("extract_insn (insn);\n");
5129 if (must_constrain)
5131 write_indent (indent + 2);
5132 printf ("if (! constrain_operands (reload_completed))\n");
5133 write_indent (indent + 2);
5134 printf (" fatal_insn_not_found (insn);\n");
5137 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
5138 known_true, av->first_insn->insn_code,
5139 av->first_insn->insn_index);
5141 if (strncmp (prefix, "return", 6))
5143 write_indent (indent + 2);
5144 printf ("break;\n");
5146 printf ("\n");
5149 /* Search for uses of non-const attributes and write code to cache them. */
5151 static int
5152 write_expr_attr_cache (p, attr)
5153 rtx p;
5154 struct attr_desc *attr;
5156 const char *fmt;
5157 int i, ie, j, je;
5159 if (GET_CODE (p) == EQ_ATTR)
5161 if (XSTR (p, 0) != attr->name)
5162 return 0;
5164 if (!attr->is_numeric)
5165 printf (" register enum attr_%s ", attr->name);
5166 else if (attr->unsigned_p)
5167 printf (" register unsigned int ");
5168 else
5169 printf (" register int ");
5171 printf ("attr_%s = get_attr_%s (insn);\n", attr->name, attr->name);
5172 return 1;
5175 fmt = GET_RTX_FORMAT (GET_CODE (p));
5176 ie = GET_RTX_LENGTH (GET_CODE (p));
5177 for (i = 0; i < ie; i++)
5179 switch (*fmt++)
5181 case 'e':
5182 if (write_expr_attr_cache (XEXP (p, i), attr))
5183 return 1;
5184 break;
5186 case 'E':
5187 je = XVECLEN (p, i);
5188 for (j = 0; j < je; ++j)
5189 if (write_expr_attr_cache (XVECEXP (p, i, j), attr))
5190 return 1;
5191 break;
5195 return 0;
5198 /* Evaluate an expression at top level. A front end to write_test_expr,
5199 in which we cache attribute values and break up excessively large
5200 expressions to cater to older compilers. */
5202 static void
5203 write_toplevel_expr (p)
5204 rtx p;
5206 struct attr_desc *attr;
5207 int i;
5209 for (i = 0; i < MAX_ATTRS_INDEX; ++i)
5210 for (attr = attrs[i]; attr ; attr = attr->next)
5211 if (!attr->is_const)
5212 write_expr_attr_cache (p, attr);
5214 printf(" register unsigned long accum = 0;\n\n");
5216 while (GET_CODE (p) == IOR)
5218 rtx e;
5219 if (GET_CODE (XEXP (p, 0)) == IOR)
5220 e = XEXP (p, 1), p = XEXP (p, 0);
5221 else
5222 e = XEXP (p, 0), p = XEXP (p, 1);
5224 printf (" accum |= ");
5225 write_test_expr (e, 3);
5226 printf (";\n");
5228 printf (" accum |= ");
5229 write_test_expr (p, 3);
5230 printf (";\n");
5233 /* Utilities to write names in various forms. */
5235 static void
5236 write_unit_name (prefix, num, suffix)
5237 const char *prefix;
5238 int num;
5239 const char *suffix;
5241 struct function_unit *unit;
5243 for (unit = units; unit; unit = unit->next)
5244 if (unit->num == num)
5246 printf ("%s%s%s", prefix, unit->name, suffix);
5247 return;
5250 printf ("%s<unknown>%s", prefix, suffix);
5253 static void
5254 write_attr_valueq (attr, s)
5255 struct attr_desc *attr;
5256 const char *s;
5258 if (attr->is_numeric)
5260 int num = atoi (s);
5262 printf ("%d", num);
5264 /* Make the blockage range values and function units used values easier
5265 to read. */
5266 if (attr->func_units_p)
5268 if (num == -1)
5269 printf (" /* units: none */");
5270 else if (num >= 0)
5271 write_unit_name (" /* units: ", num, " */");
5272 else
5274 int i;
5275 const char *sep = " /* units: ";
5276 for (i = 0, num = ~num; num; i++, num >>= 1)
5277 if (num & 1)
5279 write_unit_name (sep, i, (num == 1) ? " */" : "");
5280 sep = ", ";
5285 else if (attr->blockage_p)
5286 printf (" /* min %d, max %d */", num >> (HOST_BITS_PER_INT / 2),
5287 num & ((1 << (HOST_BITS_PER_INT / 2)) - 1));
5289 else if (num > 9 || num < 0)
5290 printf (" /* 0x%x */", num);
5292 else
5294 write_upcase (attr->name);
5295 printf ("_");
5296 write_upcase (s);
5300 static void
5301 write_attr_value (attr, value)
5302 struct attr_desc *attr;
5303 rtx value;
5305 int op;
5307 switch (GET_CODE (value))
5309 case CONST_STRING:
5310 write_attr_valueq (attr, XSTR (value, 0));
5311 break;
5313 case CONST_INT:
5314 printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
5315 break;
5317 case SYMBOL_REF:
5318 fputs (XSTR (value, 0), stdout);
5319 break;
5321 case ATTR:
5323 struct attr_desc *attr2 = find_attr (XSTR (value, 0), 0);
5324 printf ("get_attr_%s (%s)", attr2->name,
5325 (attr2->is_const ? "" : "insn"));
5327 break;
5329 case PLUS:
5330 op = '+';
5331 goto do_operator;
5332 case MINUS:
5333 op = '-';
5334 goto do_operator;
5335 case MULT:
5336 op = '*';
5337 goto do_operator;
5338 case DIV:
5339 op = '/';
5340 goto do_operator;
5341 case MOD:
5342 op = '%';
5343 goto do_operator;
5345 do_operator:
5346 write_attr_value (attr, XEXP (value, 0));
5347 putchar (' ');
5348 putchar (op);
5349 putchar (' ');
5350 write_attr_value (attr, XEXP (value, 1));
5351 break;
5353 default:
5354 abort ();
5358 static void
5359 write_upcase (str)
5360 const char *str;
5362 while (*str)
5364 /* The argument of TOUPPER should not have side effects. */
5365 putchar (TOUPPER(*str));
5366 str++;
5370 static void
5371 write_indent (indent)
5372 int indent;
5374 for (; indent > 8; indent -= 8)
5375 printf ("\t");
5377 for (; indent; indent--)
5378 printf (" ");
5381 /* Write a subroutine that is given an insn that requires a delay slot, a
5382 delay slot ordinal, and a candidate insn. It returns non-zero if the
5383 candidate can be placed in the specified delay slot of the insn.
5385 We can write as many as three subroutines. `eligible_for_delay'
5386 handles normal delay slots, `eligible_for_annul_true' indicates that
5387 the specified insn can be annulled if the branch is true, and likewise
5388 for `eligible_for_annul_false'.
5390 KIND is a string distinguishing these three cases ("delay", "annul_true",
5391 or "annul_false"). */
5393 static void
5394 write_eligible_delay (kind)
5395 const char *kind;
5397 struct delay_desc *delay;
5398 int max_slots;
5399 char str[50];
5400 struct attr_desc *attr;
5401 struct attr_value *av, *common_av;
5402 int i;
5404 /* Compute the maximum number of delay slots required. We use the delay
5405 ordinal times this number plus one, plus the slot number as an index into
5406 the appropriate predicate to test. */
5408 for (delay = delays, max_slots = 0; delay; delay = delay->next)
5409 if (XVECLEN (delay->def, 1) / 3 > max_slots)
5410 max_slots = XVECLEN (delay->def, 1) / 3;
5412 /* Write function prelude. */
5414 printf ("int\n");
5415 printf ("eligible_for_%s (delay_insn, slot, candidate_insn, flags)\n",
5416 kind);
5417 printf (" rtx delay_insn;\n");
5418 printf (" int slot;\n");
5419 printf (" rtx candidate_insn;\n");
5420 printf (" int flags ATTRIBUTE_UNUSED;\n");
5421 printf ("{\n");
5422 printf (" rtx insn;\n");
5423 printf ("\n");
5424 printf (" if (slot >= %d)\n", max_slots);
5425 printf (" abort ();\n");
5426 printf ("\n");
5428 /* If more than one delay type, find out which type the delay insn is. */
5430 if (num_delays > 1)
5432 attr = find_attr ("*delay_type", 0);
5433 if (! attr) abort ();
5434 common_av = find_most_used (attr);
5436 printf (" insn = delay_insn;\n");
5437 printf (" switch (recog_memoized (insn))\n");
5438 printf (" {\n");
5440 sprintf (str, " * %d;\n break;", max_slots);
5441 for (av = attr->first_value; av; av = av->next)
5442 if (av != common_av)
5443 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
5445 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
5446 printf (" }\n\n");
5448 /* Ensure matched. Otherwise, shouldn't have been called. */
5449 printf (" if (slot < %d)\n", max_slots);
5450 printf (" abort ();\n\n");
5453 /* If just one type of delay slot, write simple switch. */
5454 if (num_delays == 1 && max_slots == 1)
5456 printf (" insn = candidate_insn;\n");
5457 printf (" switch (recog_memoized (insn))\n");
5458 printf (" {\n");
5460 attr = find_attr ("*delay_1_0", 0);
5461 if (! attr) abort ();
5462 common_av = find_most_used (attr);
5464 for (av = attr->first_value; av; av = av->next)
5465 if (av != common_av)
5466 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
5468 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
5469 printf (" }\n");
5472 else
5474 /* Write a nested CASE. The first indicates which condition we need to
5475 test, and the inner CASE tests the condition. */
5476 printf (" insn = candidate_insn;\n");
5477 printf (" switch (slot)\n");
5478 printf (" {\n");
5480 for (delay = delays; delay; delay = delay->next)
5481 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
5483 printf (" case %d:\n",
5484 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
5485 printf (" switch (recog_memoized (insn))\n");
5486 printf ("\t{\n");
5488 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
5489 attr = find_attr (str, 0);
5490 if (! attr) abort ();
5491 common_av = find_most_used (attr);
5493 for (av = attr->first_value; av; av = av->next)
5494 if (av != common_av)
5495 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
5497 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
5498 printf (" }\n");
5501 printf (" default:\n");
5502 printf (" abort ();\n");
5503 printf (" }\n");
5506 printf ("}\n\n");
5509 /* Write routines to compute conflict cost for function units. Then write a
5510 table describing the available function units. */
5512 static void
5513 write_function_unit_info ()
5515 struct function_unit *unit;
5516 int i;
5518 /* Write out conflict routines for function units. Don't bother writing
5519 one if there is only one issue delay value. */
5521 for (unit = units; unit; unit = unit->next)
5523 if (unit->needs_blockage_function)
5524 write_complex_function (unit, "blockage", "block");
5526 /* If the minimum and maximum conflict costs are the same, there
5527 is only one value, so we don't need a function. */
5528 if (! unit->needs_conflict_function)
5530 unit->default_cost = make_numeric_value (unit->issue_delay.max);
5531 continue;
5534 /* The function first computes the case from the candidate insn. */
5535 unit->default_cost = make_numeric_value (0);
5536 write_complex_function (unit, "conflict_cost", "cost");
5539 /* Now that all functions have been written, write the table describing
5540 the function units. The name is included for documentation purposes
5541 only. */
5543 printf ("struct function_unit_desc function_units[] = {\n");
5545 /* Write out the descriptions in numeric order, but don't force that order
5546 on the list. Doing so increases the runtime of genattrtab.c. */
5547 for (i = 0; i < num_units; i++)
5549 for (unit = units; unit; unit = unit->next)
5550 if (unit->num == i)
5551 break;
5553 printf (" {\"%s\", %d, %d, %d, %s, %d, %s_unit_ready_cost, ",
5554 unit->name, 1 << unit->num, unit->multiplicity,
5555 unit->simultaneity, XSTR (unit->default_cost, 0),
5556 unit->issue_delay.max, unit->name);
5558 if (unit->needs_conflict_function)
5559 printf ("%s_unit_conflict_cost, ", unit->name);
5560 else
5561 printf ("0, ");
5563 printf ("%d, ", unit->max_blockage);
5565 if (unit->needs_range_function)
5566 printf ("%s_unit_blockage_range, ", unit->name);
5567 else
5568 printf ("0, ");
5570 if (unit->needs_blockage_function)
5571 printf ("%s_unit_blockage", unit->name);
5572 else
5573 printf ("0");
5575 printf ("}, \n");
5578 printf ("};\n\n");
5581 static void
5582 write_complex_function (unit, name, connection)
5583 struct function_unit *unit;
5584 const char *name, *connection;
5586 struct attr_desc *case_attr, *attr;
5587 struct attr_value *av, *common_av;
5588 rtx value;
5589 char *str;
5590 int using_case;
5591 int i;
5593 printf ("static int %s_unit_%s PARAMS ((rtx, rtx));\n", unit->name, name);
5594 printf ("static int\n");
5595 printf ("%s_unit_%s (executing_insn, candidate_insn)\n",
5596 unit->name, name);
5597 printf (" rtx executing_insn;\n");
5598 printf (" rtx candidate_insn;\n");
5599 printf ("{\n");
5600 printf (" rtx insn;\n");
5601 printf (" int casenum;\n\n");
5602 printf (" insn = executing_insn;\n");
5603 printf (" switch (recog_memoized (insn))\n");
5604 printf (" {\n");
5606 /* Write the `switch' statement to get the case value. */
5607 str = (char *) alloca (strlen (unit->name) + strlen (name) + strlen (connection) + 10);
5608 sprintf (str, "*%s_cases", unit->name);
5609 case_attr = find_attr (str, 0);
5610 if (! case_attr) abort ();
5611 common_av = find_most_used (case_attr);
5613 for (av = case_attr->first_value; av; av = av->next)
5614 if (av != common_av)
5615 write_attr_case (case_attr, av, 1,
5616 "casenum =", ";", 4, unit->condexp);
5618 write_attr_case (case_attr, common_av, 0,
5619 "casenum =", ";", 4, unit->condexp);
5620 printf (" }\n\n");
5622 /* Now write an outer switch statement on each case. Then write
5623 the tests on the executing function within each. */
5624 printf (" insn = candidate_insn;\n");
5625 printf (" switch (casenum)\n");
5626 printf (" {\n");
5628 for (i = 0; i < unit->num_opclasses; i++)
5630 /* Ensure using this case. */
5631 using_case = 0;
5632 for (av = case_attr->first_value; av; av = av->next)
5633 if (av->num_insns
5634 && contained_in_p (make_numeric_value (i), av->value))
5635 using_case = 1;
5637 if (! using_case)
5638 continue;
5640 printf (" case %d:\n", i);
5641 sprintf (str, "*%s_%s_%d", unit->name, connection, i);
5642 attr = find_attr (str, 0);
5643 if (! attr) abort ();
5645 /* If single value, just write it. */
5646 value = find_single_value (attr);
5647 if (value)
5648 write_attr_set (attr, 6, value, "return", ";\n", true_rtx, -2, -2);
5649 else
5651 common_av = find_most_used (attr);
5652 printf (" switch (recog_memoized (insn))\n");
5653 printf ("\t{\n");
5655 for (av = attr->first_value; av; av = av->next)
5656 if (av != common_av)
5657 write_attr_case (attr, av, 1,
5658 "return", ";", 8, unit->condexp);
5660 write_attr_case (attr, common_av, 0,
5661 "return", ";", 8, unit->condexp);
5662 printf (" }\n\n");
5666 /* This default case should not be needed, but gcc's analysis is not
5667 good enough to realize that the default case is not needed for the
5668 second switch statement. */
5669 printf (" default:\n abort ();\n");
5670 printf (" }\n}\n\n");
5673 /* This page contains miscellaneous utility routines. */
5675 /* Given a string, return the number of comma-separated elements in it.
5676 Return 0 for the null string. */
5678 static int
5679 n_comma_elts (s)
5680 const char *s;
5682 int n;
5684 if (*s == '\0')
5685 return 0;
5687 for (n = 1; *s; s++)
5688 if (*s == ',')
5689 n++;
5691 return n;
5694 /* Given a pointer to a (char *), return a malloc'ed string containing the
5695 next comma-separated element. Advance the pointer to after the string
5696 scanned, or the end-of-string. Return NULL if at end of string. */
5698 static char *
5699 next_comma_elt (pstr)
5700 const char **pstr;
5702 char *out_str;
5703 const char *p;
5705 if (**pstr == '\0')
5706 return NULL;
5708 /* Find end of string to compute length. */
5709 for (p = *pstr; *p != ',' && *p != '\0'; p++)
5712 out_str = attr_string (*pstr, p - *pstr);
5713 *pstr = p;
5715 if (**pstr == ',')
5716 (*pstr)++;
5718 return out_str;
5721 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
5722 is non-zero, build a new attribute, if one does not exist. */
5724 static struct attr_desc *
5725 find_attr (name, create)
5726 const char *name;
5727 int create;
5729 struct attr_desc *attr;
5730 int index;
5732 /* Before we resort to using `strcmp', see if the string address matches
5733 anywhere. In most cases, it should have been canonicalized to do so. */
5734 if (name == alternative_name)
5735 return NULL;
5737 index = name[0] & (MAX_ATTRS_INDEX - 1);
5738 for (attr = attrs[index]; attr; attr = attr->next)
5739 if (name == attr->name)
5740 return attr;
5742 /* Otherwise, do it the slow way. */
5743 for (attr = attrs[index]; attr; attr = attr->next)
5744 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
5745 return attr;
5747 if (! create)
5748 return NULL;
5750 attr = (struct attr_desc *) oballoc (sizeof (struct attr_desc));
5751 attr->name = attr_string (name, strlen (name));
5752 attr->first_value = attr->default_val = NULL;
5753 attr->is_numeric = attr->negative_ok = attr->is_const = attr->is_special = 0;
5754 attr->unsigned_p = attr->func_units_p = attr->blockage_p = 0;
5755 attr->next = attrs[index];
5756 attrs[index] = attr;
5758 return attr;
5761 /* Create internal attribute with the given default value. */
5763 static void
5764 make_internal_attr (name, value, special)
5765 const char *name;
5766 rtx value;
5767 int special;
5769 struct attr_desc *attr;
5771 attr = find_attr (name, 1);
5772 if (attr->default_val)
5773 abort ();
5775 attr->is_numeric = 1;
5776 attr->is_const = 0;
5777 attr->is_special = (special & 1) != 0;
5778 attr->negative_ok = (special & 2) != 0;
5779 attr->unsigned_p = (special & 4) != 0;
5780 attr->func_units_p = (special & 8) != 0;
5781 attr->blockage_p = (special & 16) != 0;
5782 attr->default_val = get_attr_value (value, attr, -2);
5785 /* Find the most used value of an attribute. */
5787 static struct attr_value *
5788 find_most_used (attr)
5789 struct attr_desc *attr;
5791 struct attr_value *av;
5792 struct attr_value *most_used;
5793 int nuses;
5795 most_used = NULL;
5796 nuses = -1;
5798 for (av = attr->first_value; av; av = av->next)
5799 if (av->num_insns > nuses)
5800 nuses = av->num_insns, most_used = av;
5802 return most_used;
5805 /* If an attribute only has a single value used, return it. Otherwise
5806 return NULL. */
5808 static rtx
5809 find_single_value (attr)
5810 struct attr_desc *attr;
5812 struct attr_value *av;
5813 rtx unique_value;
5815 unique_value = NULL;
5816 for (av = attr->first_value; av; av = av->next)
5817 if (av->num_insns)
5819 if (unique_value)
5820 return NULL;
5821 else
5822 unique_value = av->value;
5825 return unique_value;
5828 /* Return (attr_value "n") */
5830 static rtx
5831 make_numeric_value (n)
5832 int n;
5834 static rtx int_values[20];
5835 rtx exp;
5836 char *p;
5838 if (n < 0)
5839 abort ();
5841 if (n < 20 && int_values[n])
5842 return int_values[n];
5844 p = attr_printf (MAX_DIGITS, "%d", n);
5845 exp = attr_rtx (CONST_STRING, p);
5847 if (n < 20)
5848 int_values[n] = exp;
5850 return exp;
5853 static void
5854 extend_range (range, min, max)
5855 struct range *range;
5856 int min;
5857 int max;
5859 if (range->min > min) range->min = min;
5860 if (range->max < max) range->max = max;
5863 static rtx
5864 copy_rtx_unchanging (orig)
5865 register rtx orig;
5867 #if 0
5868 register rtx copy;
5869 register RTX_CODE code;
5870 #endif
5872 if (RTX_UNCHANGING_P (orig) || MEM_IN_STRUCT_P (orig))
5873 return orig;
5875 MEM_IN_STRUCT_P (orig) = 1;
5876 return orig;
5878 #if 0
5879 code = GET_CODE (orig);
5880 switch (code)
5882 case CONST_INT:
5883 case CONST_DOUBLE:
5884 case SYMBOL_REF:
5885 case CODE_LABEL:
5886 return orig;
5888 default:
5889 break;
5892 copy = rtx_alloc (code);
5893 PUT_MODE (copy, GET_MODE (orig));
5894 RTX_UNCHANGING_P (copy) = 1;
5896 bcopy ((char *) &XEXP (orig, 0), (char *) &XEXP (copy, 0),
5897 GET_RTX_LENGTH (GET_CODE (copy)) * sizeof (rtx));
5898 return copy;
5899 #endif
5902 /* Determine if an insn has a constant number of delay slots, i.e., the
5903 number of delay slots is not a function of the length of the insn. */
5905 static void
5906 write_const_num_delay_slots ()
5908 struct attr_desc *attr = find_attr ("*num_delay_slots", 0);
5909 struct attr_value *av;
5910 struct insn_ent *ie;
5912 if (attr)
5914 printf ("int\nconst_num_delay_slots (insn)\n");
5915 printf (" rtx insn;\n");
5916 printf ("{\n");
5917 printf (" switch (recog_memoized (insn))\n");
5918 printf (" {\n");
5920 for (av = attr->first_value; av; av = av->next)
5922 length_used = 0;
5923 walk_attr_value (av->value);
5924 if (length_used)
5926 for (ie = av->first_insn; ie; ie = ie->next)
5927 if (ie->insn_code != -1)
5928 printf (" case %d:\n", ie->insn_code);
5929 printf (" return 0;\n");
5933 printf (" default:\n");
5934 printf (" return 1;\n");
5935 printf (" }\n}\n\n");
5940 extern int main PARAMS ((int, char **));
5943 main (argc, argv)
5944 int argc;
5945 char **argv;
5947 rtx desc;
5948 struct attr_desc *attr;
5949 struct insn_def *id;
5950 rtx tem;
5951 int i;
5953 progname = "genattrtab";
5955 if (argc <= 1)
5956 fatal ("No input file name.");
5958 #if defined (RLIMIT_STACK) && defined (HAVE_GETRLIMIT) && defined (HAVE_SETRLIMIT)
5959 /* Get rid of any avoidable limit on stack size. */
5961 struct rlimit rlim;
5963 /* Set the stack limit huge so that alloca does not fail. */
5964 getrlimit (RLIMIT_STACK, &rlim);
5965 rlim.rlim_cur = rlim.rlim_max;
5966 setrlimit (RLIMIT_STACK, &rlim);
5968 #endif
5970 if (init_md_reader (argv[1]) != SUCCESS_EXIT_CODE)
5971 return (FATAL_EXIT_CODE);
5973 obstack_init (hash_obstack);
5974 obstack_init (temp_obstack);
5976 /* Set up true and false rtx's */
5977 true_rtx = rtx_alloc (CONST_INT);
5978 XWINT (true_rtx, 0) = 1;
5979 false_rtx = rtx_alloc (CONST_INT);
5980 XWINT (false_rtx, 0) = 0;
5981 RTX_UNCHANGING_P (true_rtx) = RTX_UNCHANGING_P (false_rtx) = 1;
5982 RTX_INTEGRATED_P (true_rtx) = RTX_INTEGRATED_P (false_rtx) = 1;
5984 alternative_name = attr_string ("alternative", strlen ("alternative"));
5986 printf ("/* Generated automatically by the program `genattrtab'\n\
5987 from the machine description file `md'. */\n\n");
5989 /* Read the machine description. */
5991 while (1)
5993 int line_no;
5995 desc = read_md_rtx (&line_no, &insn_code_number);
5996 if (desc == NULL)
5997 break;
5999 switch (GET_CODE (desc))
6001 case DEFINE_INSN:
6002 case DEFINE_PEEPHOLE:
6003 case DEFINE_ASM_ATTRIBUTES:
6004 gen_insn(desc);
6005 break;
6007 case DEFINE_ATTR:
6008 gen_attr (desc);
6009 break;
6011 case DEFINE_DELAY:
6012 gen_delay (desc);
6013 break;
6015 case DEFINE_FUNCTION_UNIT:
6016 gen_unit (desc);
6017 break;
6019 default:
6020 break;
6022 if (GET_CODE (desc) != DEFINE_ASM_ATTRIBUTES)
6023 insn_index_number++;
6026 insn_code_number++;
6028 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
6029 if (! got_define_asm_attributes)
6031 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
6032 XVEC (tem, 0) = rtvec_alloc (0);
6033 gen_insn (tem);
6036 /* Expand DEFINE_DELAY information into new attribute. */
6037 if (num_delays)
6038 expand_delays ();
6040 /* Expand DEFINE_FUNCTION_UNIT information into new attributes. */
6041 if (num_units)
6042 expand_units ();
6044 printf ("#include \"config.h\"\n");
6045 printf ("#include \"system.h\"\n");
6046 printf ("#include \"rtl.h\"\n");
6047 printf ("#include \"tm_p.h\"\n");
6048 printf ("#include \"insn-config.h\"\n");
6049 printf ("#include \"recog.h\"\n");
6050 printf ("#include \"regs.h\"\n");
6051 printf ("#include \"real.h\"\n");
6052 printf ("#include \"output.h\"\n");
6053 printf ("#include \"insn-attr.h\"\n");
6054 printf ("#include \"toplev.h\"\n");
6055 printf ("\n");
6056 printf ("#define operands recog_data.operand\n\n");
6058 /* Make `insn_alternatives'. */
6059 insn_alternatives = (int *) oballoc (insn_code_number * sizeof (int));
6060 for (id = defs; id; id = id->next)
6061 if (id->insn_code >= 0)
6062 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
6064 /* Make `insn_n_alternatives'. */
6065 insn_n_alternatives = (int *) oballoc (insn_code_number * sizeof (int));
6066 for (id = defs; id; id = id->next)
6067 if (id->insn_code >= 0)
6068 insn_n_alternatives[id->insn_code] = id->num_alternatives;
6070 /* Prepare to write out attribute subroutines by checking everything stored
6071 away and building the attribute cases. */
6073 check_defs ();
6074 for (i = 0; i < MAX_ATTRS_INDEX; i++)
6075 for (attr = attrs[i]; attr; attr = attr->next)
6077 attr->default_val->value
6078 = check_attr_value (attr->default_val->value, attr);
6079 fill_attr (attr);
6082 /* Construct extra attributes for `length'. */
6083 make_length_attrs ();
6085 /* Perform any possible optimizations to speed up compilation. */
6086 optimize_attrs ();
6088 /* Now write out all the `gen_attr_...' routines. Do these before the
6089 special routines (specifically before write_function_unit_info), so
6090 that they get defined before they are used. */
6092 for (i = 0; i < MAX_ATTRS_INDEX; i++)
6093 for (attr = attrs[i]; attr; attr = attr->next)
6095 if (! attr->is_special && ! attr->is_const)
6096 write_attr_get (attr);
6099 /* Write out delay eligibility information, if DEFINE_DELAY present.
6100 (The function to compute the number of delay slots will be written
6101 below.) */
6102 if (num_delays)
6104 write_eligible_delay ("delay");
6105 if (have_annul_true)
6106 write_eligible_delay ("annul_true");
6107 if (have_annul_false)
6108 write_eligible_delay ("annul_false");
6111 /* Write out information about function units. */
6112 if (num_units)
6113 write_function_unit_info ();
6115 /* Write out constant delay slot info */
6116 write_const_num_delay_slots ();
6118 write_length_unit_log ();
6120 fflush (stdout);
6121 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
6124 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
6125 const char *
6126 get_insn_name (code)
6127 int code ATTRIBUTE_UNUSED;
6129 return NULL;