* gcc.h (lang_specific_driver): Constify second argument.
[official-gcc.git] / gcc / genattrtab.c
blobaedd1d04877f7fb741a3b54bcb9edd08a42d97d9
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 struct insn_def *next; /* Next insn in chain. */
134 rtx def; /* The DEFINE_... */
135 int insn_code; /* Instruction number. */
136 int insn_index; /* Expression numer in file, for errors. */
137 int lineno; /* Line number. */
138 int num_alternatives; /* Number of alternatives. */
139 int vec_idx; /* Index of attribute vector in `def'. */
142 /* Once everything has been read in, we store in each attribute value a list
143 of insn codes that have that value. Here is the structure used for the
144 list. */
146 struct insn_ent
148 struct insn_ent *next; /* Next in chain. */
149 int insn_code; /* Instruction number. */
150 int insn_index; /* Index of definition in file */
151 int lineno; /* Line number. */
154 /* Each value of an attribute (either constant or computed) is assigned a
155 structure which is used as the listhead of the insns that have that
156 value. */
158 struct attr_value
160 rtx value; /* Value of attribute. */
161 struct attr_value *next; /* Next attribute value in chain. */
162 struct insn_ent *first_insn; /* First insn with this value. */
163 int num_insns; /* Number of insns with this value. */
164 int has_asm_insn; /* True if this value used for `asm' insns */
167 /* Structure for each attribute. */
169 struct attr_desc
171 char *name; /* Name of attribute. */
172 struct attr_desc *next; /* Next attribute. */
173 unsigned is_numeric : 1; /* Values of this attribute are numeric. */
174 unsigned negative_ok : 1; /* Allow negative numeric values. */
175 unsigned unsigned_p : 1; /* Make the output function unsigned int. */
176 unsigned is_const : 1; /* Attribute value constant for each run. */
177 unsigned is_special : 1; /* Don't call `write_attr_set'. */
178 unsigned func_units_p : 1; /* this is the function_units attribute */
179 unsigned blockage_p : 1; /* this is the blockage range function */
180 struct attr_value *first_value; /* First value of this attribute. */
181 struct attr_value *default_val; /* Default value for this attribute. */
182 int lineno; /* Line number. */
185 #define NULL_ATTR (struct attr_desc *) NULL
187 /* A range of values. */
189 struct range
191 int min;
192 int max;
195 /* Structure for each DEFINE_DELAY. */
197 struct delay_desc
199 rtx def; /* DEFINE_DELAY expression. */
200 struct delay_desc *next; /* Next DEFINE_DELAY. */
201 int num; /* Number of DEFINE_DELAY, starting at 1. */
202 int lineno; /* Line number. */
205 /* Record information about each DEFINE_FUNCTION_UNIT. */
207 struct function_unit_op
209 rtx condexp; /* Expression TRUE for applicable insn. */
210 struct function_unit_op *next; /* Next operation for this function unit. */
211 int num; /* Ordinal for this operation type in unit. */
212 int ready; /* Cost until data is ready. */
213 int issue_delay; /* Cost until unit can accept another insn. */
214 rtx conflict_exp; /* Expression TRUE for insns incurring issue delay. */
215 rtx issue_exp; /* Expression computing issue delay. */
216 int lineno; /* Line number. */
219 /* Record information about each function unit mentioned in a
220 DEFINE_FUNCTION_UNIT. */
222 struct function_unit
224 const char *name; /* Function unit name. */
225 struct function_unit *next; /* Next function unit. */
226 int num; /* Ordinal of this unit type. */
227 int multiplicity; /* Number of units of this type. */
228 int simultaneity; /* Maximum number of simultaneous insns
229 on this function unit or 0 if unlimited. */
230 rtx condexp; /* Expression TRUE for insn needing unit. */
231 int num_opclasses; /* Number of different operation types. */
232 struct function_unit_op *ops; /* Pointer to first operation type. */
233 int needs_conflict_function; /* Nonzero if a conflict function required. */
234 int needs_blockage_function; /* Nonzero if a blockage function required. */
235 int needs_range_function; /* Nonzero if blockage range function needed.*/
236 rtx default_cost; /* Conflict cost, if constant. */
237 struct range issue_delay; /* Range of issue delay values. */
238 int max_blockage; /* Maximum time an insn blocks the unit. */
239 int first_lineno; /* First seen line number. */
242 /* Listheads of above structures. */
244 /* This one is indexed by the first character of the attribute name. */
245 #define MAX_ATTRS_INDEX 256
246 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
247 static struct insn_def *defs;
248 static struct delay_desc *delays;
249 static struct function_unit *units;
251 /* An expression where all the unknown terms are EQ_ATTR tests can be
252 rearranged into a COND provided we can enumerate all possible
253 combinations of the unknown values. The set of combinations become the
254 tests of the COND; the value of the expression given that combination is
255 computed and becomes the corresponding value. To do this, we must be
256 able to enumerate all values for each attribute used in the expression
257 (currently, we give up if we find a numeric attribute).
259 If the set of EQ_ATTR tests used in an expression tests the value of N
260 different attributes, the list of all possible combinations can be made
261 by walking the N-dimensional attribute space defined by those
262 attributes. We record each of these as a struct dimension.
264 The algorithm relies on sharing EQ_ATTR nodes: if two nodes in an
265 expression are the same, the will also have the same address. We find
266 all the EQ_ATTR nodes by marking them MEM_VOLATILE_P. This bit later
267 represents the value of an EQ_ATTR node, so once all nodes are marked,
268 they are also given an initial value of FALSE.
270 We then separate the set of EQ_ATTR nodes into dimensions for each
271 attribute and put them on the VALUES list. Terms are added as needed by
272 `add_values_to_cover' so that all possible values of the attribute are
273 tested.
275 Each dimension also has a current value. This is the node that is
276 currently considered to be TRUE. If this is one of the nodes added by
277 `add_values_to_cover', all the EQ_ATTR tests in the original expression
278 will be FALSE. Otherwise, only the CURRENT_VALUE will be true.
280 NUM_VALUES is simply the length of the VALUES list and is there for
281 convenience.
283 Once the dimensions are created, the algorithm enumerates all possible
284 values and computes the current value of the given expression. */
286 struct dimension
288 struct attr_desc *attr; /* Attribute for this dimension. */
289 rtx values; /* List of attribute values used. */
290 rtx current_value; /* Position in the list for the TRUE value. */
291 int num_values; /* Length of the values list. */
294 /* Other variables. */
296 static int insn_code_number;
297 static int insn_index_number;
298 static int got_define_asm_attributes;
299 static int must_extract;
300 static int must_constrain;
301 static int address_used;
302 static int length_used;
303 static int num_delays;
304 static int have_annul_true, have_annul_false;
305 static int num_units, num_unit_opclasses;
306 static int num_insn_ents;
308 /* Used as operand to `operate_exp': */
310 enum operator {PLUS_OP, MINUS_OP, POS_MINUS_OP, EQ_OP, OR_OP, ORX_OP, MAX_OP, MIN_OP, RANGE_OP};
312 /* Stores, for each insn code, the number of constraint alternatives. */
314 static int *insn_n_alternatives;
316 /* Stores, for each insn code, a bitmap that has bits on for each possible
317 alternative. */
319 static int *insn_alternatives;
321 /* If nonzero, assume that the `alternative' attr has this value.
322 This is the hashed, unique string for the numeral
323 whose value is chosen alternative. */
325 static const char *current_alternative_string;
327 /* Used to simplify expressions. */
329 static rtx true_rtx, false_rtx;
331 /* Used to reduce calls to `strcmp' */
333 static char *alternative_name;
335 /* Indicate that REG_DEAD notes are valid if dead_or_set_p is ever
336 called. */
338 int reload_completed = 0;
340 /* Some machines test `optimize' in macros called from rtlanal.c, so we need
341 to define it here. */
343 int optimize = 0;
345 /* Simplify an expression. Only call the routine if there is something to
346 simplify. */
347 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
348 (RTX_UNCHANGING_P (EXP) || MEM_IN_STRUCT_P (EXP) ? (EXP) \
349 : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
351 /* Simplify (eq_attr ("alternative") ...)
352 when we are working with a particular alternative. */
353 #define SIMPLIFY_ALTERNATIVE(EXP) \
354 if (current_alternative_string \
355 && GET_CODE ((EXP)) == EQ_ATTR \
356 && XSTR ((EXP), 0) == alternative_name) \
357 (EXP) = (XSTR ((EXP), 1) == current_alternative_string \
358 ? true_rtx : false_rtx);
360 /* These are referenced by rtlanal.c and hence need to be defined somewhere.
361 They won't actually be used. */
363 rtx global_rtl[GR_MAX];
364 rtx pic_offset_table_rtx;
366 static void attr_hash_add_rtx PARAMS ((int, rtx));
367 static void attr_hash_add_string PARAMS ((int, char *));
368 static rtx attr_rtx PARAMS ((enum rtx_code, ...));
369 static char *attr_printf PARAMS ((int, const char *, ...))
370 ATTRIBUTE_PRINTF_2;
371 static char *attr_string PARAMS ((const char *, int));
372 static rtx check_attr_test PARAMS ((rtx, int, int));
373 static rtx check_attr_value PARAMS ((rtx, struct attr_desc *));
374 static rtx convert_set_attr_alternative PARAMS ((rtx, struct insn_def *));
375 static rtx convert_set_attr PARAMS ((rtx, struct insn_def *));
376 static void check_defs PARAMS ((void));
377 #if 0
378 static rtx convert_const_symbol_ref PARAMS ((rtx, struct attr_desc *));
379 #endif
380 static rtx make_canonical PARAMS ((struct attr_desc *, rtx));
381 static struct attr_value *get_attr_value PARAMS ((rtx, struct attr_desc *, int));
382 static rtx copy_rtx_unchanging PARAMS ((rtx));
383 static rtx copy_boolean PARAMS ((rtx));
384 static void expand_delays PARAMS ((void));
385 static rtx operate_exp PARAMS ((enum operator, rtx, rtx));
386 static void expand_units PARAMS ((void));
387 static rtx simplify_knowing PARAMS ((rtx, rtx));
388 static rtx encode_units_mask PARAMS ((rtx));
389 static void fill_attr PARAMS ((struct attr_desc *));
390 /* dpx2 compiler chokes if we specify the arg types of the args. */
391 static rtx substitute_address PARAMS ((rtx, rtx (*) (rtx), rtx (*) (rtx)));
392 static void make_length_attrs PARAMS ((void));
393 static rtx identity_fn PARAMS ((rtx));
394 static rtx zero_fn PARAMS ((rtx));
395 static rtx one_fn PARAMS ((rtx));
396 static rtx max_fn PARAMS ((rtx));
397 static void write_length_unit_log PARAMS ((void));
398 static rtx simplify_cond PARAMS ((rtx, int, int));
399 #if 0
400 static rtx simplify_by_alternatives PARAMS ((rtx, int, int));
401 #endif
402 static rtx simplify_by_exploding PARAMS ((rtx));
403 static int find_and_mark_used_attributes PARAMS ((rtx, rtx *, int *));
404 static void unmark_used_attributes PARAMS ((rtx, struct dimension *, int));
405 static int add_values_to_cover PARAMS ((struct dimension *));
406 static int increment_current_value PARAMS ((struct dimension *, int));
407 static rtx test_for_current_value PARAMS ((struct dimension *, int));
408 static rtx simplify_with_current_value PARAMS ((rtx, struct dimension *, int));
409 static rtx simplify_with_current_value_aux PARAMS ((rtx));
410 static void clear_struct_flag PARAMS ((rtx));
411 static int count_sub_rtxs PARAMS ((rtx, int));
412 static void remove_insn_ent PARAMS ((struct attr_value *, struct insn_ent *));
413 static void insert_insn_ent PARAMS ((struct attr_value *, struct insn_ent *));
414 static rtx insert_right_side PARAMS ((enum rtx_code, rtx, rtx, int, int));
415 static rtx make_alternative_compare PARAMS ((int));
416 static int compute_alternative_mask PARAMS ((rtx, enum rtx_code));
417 static rtx evaluate_eq_attr PARAMS ((rtx, rtx, int, int));
418 static rtx simplify_and_tree PARAMS ((rtx, rtx *, int, int));
419 static rtx simplify_or_tree PARAMS ((rtx, rtx *, int, int));
420 static rtx simplify_test_exp PARAMS ((rtx, int, int));
421 static void optimize_attrs PARAMS ((void));
422 static void gen_attr PARAMS ((rtx, int));
423 static int count_alternatives PARAMS ((rtx));
424 static int compares_alternatives_p PARAMS ((rtx));
425 static int contained_in_p PARAMS ((rtx, rtx));
426 static void gen_insn PARAMS ((rtx, int));
427 static void gen_delay PARAMS ((rtx, int));
428 static void gen_unit PARAMS ((rtx, int));
429 static void write_test_expr PARAMS ((rtx, int));
430 static int max_attr_value PARAMS ((rtx, int*));
431 static int or_attr_value PARAMS ((rtx, int*));
432 static void walk_attr_value PARAMS ((rtx));
433 static void write_attr_get PARAMS ((struct attr_desc *));
434 static rtx eliminate_known_true PARAMS ((rtx, rtx, int, int));
435 static void write_attr_set PARAMS ((struct attr_desc *, int, rtx,
436 const char *, const char *, rtx,
437 int, int));
438 static void write_attr_case PARAMS ((struct attr_desc *, struct attr_value *,
439 int, const char *, const char *, int, rtx));
440 static void write_unit_name PARAMS ((const char *, int, const char *));
441 static void write_attr_valueq PARAMS ((struct attr_desc *, const char *));
442 static void write_attr_value PARAMS ((struct attr_desc *, rtx));
443 static void write_upcase PARAMS ((const char *));
444 static void write_indent PARAMS ((int));
445 static void write_eligible_delay PARAMS ((const char *));
446 static void write_function_unit_info PARAMS ((void));
447 static void write_complex_function PARAMS ((struct function_unit *, const char *,
448 const char *));
449 static int write_expr_attr_cache PARAMS ((rtx, struct attr_desc *));
450 static void write_toplevel_expr PARAMS ((rtx));
451 static void write_const_num_delay_slots PARAMS ((void));
452 static int n_comma_elts PARAMS ((const char *));
453 static char *next_comma_elt PARAMS ((const char **));
454 static struct attr_desc *find_attr PARAMS ((const char *, int));
455 static void make_internal_attr PARAMS ((const char *, rtx, int));
456 static struct attr_value *find_most_used PARAMS ((struct attr_desc *));
457 static rtx find_single_value PARAMS ((struct attr_desc *));
458 static rtx make_numeric_value PARAMS ((int));
459 static void extend_range PARAMS ((struct range *, int, int));
460 static rtx attr_eq PARAMS ((const char *, const char *));
461 static const char *attr_numeral PARAMS ((int));
462 static int attr_equal_p PARAMS ((rtx, rtx));
463 static rtx attr_copy_rtx PARAMS ((rtx));
465 #define oballoc(size) obstack_alloc (hash_obstack, size)
468 /* Hash table for sharing RTL and strings. */
470 /* Each hash table slot is a bucket containing a chain of these structures.
471 Strings are given negative hash codes; RTL expressions are given positive
472 hash codes. */
474 struct attr_hash
476 struct attr_hash *next; /* Next structure in the bucket. */
477 int hashcode; /* Hash code of this rtx or string. */
478 union
480 char *str; /* The string (negative hash codes) */
481 rtx rtl; /* or the RTL recorded here. */
482 } u;
485 /* Now here is the hash table. When recording an RTL, it is added to
486 the slot whose index is the hash code mod the table size. Note
487 that the hash table is used for several kinds of RTL (see attr_rtx)
488 and for strings. While all these live in the same table, they are
489 completely independent, and the hash code is computed differently
490 for each. */
492 #define RTL_HASH_SIZE 4093
493 struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
495 /* Here is how primitive or already-shared RTL's hash
496 codes are made. */
497 #define RTL_HASH(RTL) ((long) (RTL) & 0777777)
499 /* Add an entry to the hash table for RTL with hash code HASHCODE. */
501 static void
502 attr_hash_add_rtx (hashcode, rtl)
503 int hashcode;
504 rtx rtl;
506 register struct attr_hash *h;
508 h = (struct attr_hash *) obstack_alloc (hash_obstack,
509 sizeof (struct attr_hash));
510 h->hashcode = hashcode;
511 h->u.rtl = rtl;
512 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
513 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
516 /* Add an entry to the hash table for STRING with hash code HASHCODE. */
518 static void
519 attr_hash_add_string (hashcode, str)
520 int hashcode;
521 char *str;
523 register struct attr_hash *h;
525 h = (struct attr_hash *) obstack_alloc (hash_obstack,
526 sizeof (struct attr_hash));
527 h->hashcode = -hashcode;
528 h->u.str = str;
529 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
530 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
533 /* Generate an RTL expression, but avoid duplicates.
534 Set the RTX_INTEGRATED_P flag for these permanent objects.
536 In some cases we cannot uniquify; then we return an ordinary
537 impermanent rtx with RTX_INTEGRATED_P clear.
539 Args are like gen_rtx, but without the mode:
541 rtx attr_rtx (code, [element1, ..., elementn]) */
543 /*VARARGS1*/
544 static rtx
545 attr_rtx VPARAMS ((enum rtx_code code, ...))
547 #ifndef ANSI_PROTOTYPES
548 enum rtx_code code;
549 #endif
550 va_list p;
551 register int i; /* Array indices... */
552 register const char *fmt; /* Current rtx's format... */
553 register rtx rt_val = NULL_RTX;/* RTX to return to caller... */
554 int hashcode;
555 register struct attr_hash *h;
556 struct obstack *old_obstack = rtl_obstack;
558 VA_START (p, code);
560 #ifndef ANSI_PROTOTYPES
561 code = va_arg (p, enum rtx_code);
562 #endif
564 /* For each of several cases, search the hash table for an existing entry.
565 Use that entry if one is found; otherwise create a new RTL and add it
566 to the table. */
568 if (GET_RTX_CLASS (code) == '1')
570 rtx arg0 = va_arg (p, rtx);
572 /* A permanent object cannot point to impermanent ones. */
573 if (! RTX_INTEGRATED_P (arg0))
575 rt_val = rtx_alloc (code);
576 XEXP (rt_val, 0) = arg0;
577 va_end (p);
578 return rt_val;
581 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
582 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
583 if (h->hashcode == hashcode
584 && GET_CODE (h->u.rtl) == code
585 && XEXP (h->u.rtl, 0) == arg0)
586 goto found;
588 if (h == 0)
590 rtl_obstack = hash_obstack;
591 rt_val = rtx_alloc (code);
592 XEXP (rt_val, 0) = arg0;
595 else if (GET_RTX_CLASS (code) == 'c'
596 || GET_RTX_CLASS (code) == '2'
597 || GET_RTX_CLASS (code) == '<')
599 rtx arg0 = va_arg (p, rtx);
600 rtx arg1 = va_arg (p, rtx);
602 /* A permanent object cannot point to impermanent ones. */
603 if (! RTX_INTEGRATED_P (arg0) || ! RTX_INTEGRATED_P (arg1))
605 rt_val = rtx_alloc (code);
606 XEXP (rt_val, 0) = arg0;
607 XEXP (rt_val, 1) = arg1;
608 va_end (p);
609 return rt_val;
612 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
613 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
614 if (h->hashcode == hashcode
615 && GET_CODE (h->u.rtl) == code
616 && XEXP (h->u.rtl, 0) == arg0
617 && XEXP (h->u.rtl, 1) == arg1)
618 goto found;
620 if (h == 0)
622 rtl_obstack = hash_obstack;
623 rt_val = rtx_alloc (code);
624 XEXP (rt_val, 0) = arg0;
625 XEXP (rt_val, 1) = arg1;
628 else if (GET_RTX_LENGTH (code) == 1
629 && GET_RTX_FORMAT (code)[0] == 's')
631 char *arg0 = va_arg (p, char *);
633 if (code == SYMBOL_REF)
634 arg0 = attr_string (arg0, strlen (arg0));
636 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
637 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
638 if (h->hashcode == hashcode
639 && GET_CODE (h->u.rtl) == code
640 && XSTR (h->u.rtl, 0) == arg0)
641 goto found;
643 if (h == 0)
645 rtl_obstack = hash_obstack;
646 rt_val = rtx_alloc (code);
647 XSTR (rt_val, 0) = arg0;
650 else if (GET_RTX_LENGTH (code) == 2
651 && GET_RTX_FORMAT (code)[0] == 's'
652 && GET_RTX_FORMAT (code)[1] == 's')
654 char *arg0 = va_arg (p, char *);
655 char *arg1 = va_arg (p, char *);
657 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
658 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
659 if (h->hashcode == hashcode
660 && GET_CODE (h->u.rtl) == code
661 && XSTR (h->u.rtl, 0) == arg0
662 && XSTR (h->u.rtl, 1) == arg1)
663 goto found;
665 if (h == 0)
667 rtl_obstack = hash_obstack;
668 rt_val = rtx_alloc (code);
669 XSTR (rt_val, 0) = arg0;
670 XSTR (rt_val, 1) = arg1;
673 else if (code == CONST_INT)
675 HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
676 if (arg0 == 0)
678 va_end (p);
679 return false_rtx;
681 if (arg0 == 1)
683 va_end (p);
684 return true_rtx;
686 goto nohash;
688 else
690 nohash:
691 rt_val = rtx_alloc (code); /* Allocate the storage space. */
693 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
694 for (i = 0; i < GET_RTX_LENGTH (code); i++)
696 switch (*fmt++)
698 case '0': /* Unused field. */
699 break;
701 case 'i': /* An integer? */
702 XINT (rt_val, i) = va_arg (p, int);
703 break;
705 case 'w': /* A wide integer? */
706 XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
707 break;
709 case 's': /* A string? */
710 XSTR (rt_val, i) = va_arg (p, char *);
711 break;
713 case 'e': /* An expression? */
714 case 'u': /* An insn? Same except when printing. */
715 XEXP (rt_val, i) = va_arg (p, rtx);
716 break;
718 case 'E': /* An RTX vector? */
719 XVEC (rt_val, i) = va_arg (p, rtvec);
720 break;
722 default:
723 abort();
726 va_end (p);
727 return rt_val;
730 rtl_obstack = old_obstack;
731 va_end (p);
732 attr_hash_add_rtx (hashcode, rt_val);
733 RTX_INTEGRATED_P (rt_val) = 1;
734 return rt_val;
736 found:
737 va_end (p);
738 return h->u.rtl;
741 /* Create a new string printed with the printf line arguments into a space
742 of at most LEN bytes:
744 rtx attr_printf (len, format, [arg1, ..., argn]) */
746 /*VARARGS2*/
747 static char *
748 attr_printf VPARAMS ((register int len, const char *fmt, ...))
750 #ifndef ANSI_PROTOTYPES
751 register int len;
752 const char *fmt;
753 #endif
754 va_list p;
755 register char *str;
757 VA_START (p, fmt);
759 #ifndef ANSI_PROTOTYPES
760 len = va_arg (p, int);
761 fmt = va_arg (p, const char *);
762 #endif
764 /* Print the string into a temporary location. */
765 str = (char *) alloca (len);
766 vsprintf (str, fmt, p);
767 va_end (p);
769 return attr_string (str, strlen (str));
772 static rtx
773 attr_eq (name, value)
774 const char *name, *value;
776 return attr_rtx (EQ_ATTR, attr_string (name, strlen (name)),
777 attr_string (value, strlen (value)));
780 static const char *
781 attr_numeral (n)
782 int n;
784 return XSTR (make_numeric_value (n), 0);
787 /* Return a permanent (possibly shared) copy of a string STR (not assumed
788 to be null terminated) with LEN bytes. */
790 static char *
791 attr_string (str, len)
792 const char *str;
793 int len;
795 register struct attr_hash *h;
796 int hashcode;
797 int i;
798 register char *new_str;
800 /* Compute the hash code. */
801 hashcode = (len + 1) * 613 + (unsigned)str[0];
802 for (i = 1; i <= len; i += 2)
803 hashcode = ((hashcode * 613) + (unsigned)str[i]);
804 if (hashcode < 0)
805 hashcode = -hashcode;
807 /* Search the table for the string. */
808 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
809 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
810 && !strncmp (h->u.str, str, len))
811 return h->u.str; /* <-- return if found. */
813 /* Not found; create a permanent copy and add it to the hash table. */
814 new_str = (char *) obstack_alloc (hash_obstack, len + 1);
815 bcopy (str, new_str, len);
816 new_str[len] = '\0';
817 attr_hash_add_string (hashcode, new_str);
819 return new_str; /* Return the new string. */
822 /* Check two rtx's for equality of contents,
823 taking advantage of the fact that if both are hashed
824 then they can't be equal unless they are the same object. */
826 static int
827 attr_equal_p (x, y)
828 rtx x, y;
830 return (x == y || (! (RTX_INTEGRATED_P (x) && RTX_INTEGRATED_P (y))
831 && rtx_equal_p (x, y)));
834 /* Copy an attribute value expression,
835 descending to all depths, but not copying any
836 permanent hashed subexpressions. */
838 static rtx
839 attr_copy_rtx (orig)
840 register rtx orig;
842 register rtx copy;
843 register int i, j;
844 register RTX_CODE code;
845 register const char *format_ptr;
847 /* No need to copy a permanent object. */
848 if (RTX_INTEGRATED_P (orig))
849 return orig;
851 code = GET_CODE (orig);
853 switch (code)
855 case REG:
856 case QUEUED:
857 case CONST_INT:
858 case CONST_DOUBLE:
859 case SYMBOL_REF:
860 case CODE_LABEL:
861 case PC:
862 case CC0:
863 return orig;
865 default:
866 break;
869 copy = rtx_alloc (code);
870 PUT_MODE (copy, GET_MODE (orig));
871 copy->in_struct = orig->in_struct;
872 copy->volatil = orig->volatil;
873 copy->unchanging = orig->unchanging;
874 copy->integrated = orig->integrated;
876 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
878 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
880 switch (*format_ptr++)
882 case 'e':
883 XEXP (copy, i) = XEXP (orig, i);
884 if (XEXP (orig, i) != NULL)
885 XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
886 break;
888 case 'E':
889 case 'V':
890 XVEC (copy, i) = XVEC (orig, i);
891 if (XVEC (orig, i) != NULL)
893 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
894 for (j = 0; j < XVECLEN (copy, i); j++)
895 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
897 break;
899 case 'n':
900 case 'i':
901 XINT (copy, i) = XINT (orig, i);
902 break;
904 case 'w':
905 XWINT (copy, i) = XWINT (orig, i);
906 break;
908 case 's':
909 case 'S':
910 XSTR (copy, i) = XSTR (orig, i);
911 break;
913 default:
914 abort ();
917 return copy;
920 /* Given a test expression for an attribute, ensure it is validly formed.
921 IS_CONST indicates whether the expression is constant for each compiler
922 run (a constant expression may not test any particular insn).
924 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
925 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
926 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
928 Update the string address in EQ_ATTR expression to be the same used
929 in the attribute (or `alternative_name') to speed up subsequent
930 `find_attr' calls and eliminate most `strcmp' calls.
932 Return the new expression, if any. */
934 static rtx
935 check_attr_test (exp, is_const, lineno)
936 rtx exp;
937 int is_const;
938 int lineno;
940 struct attr_desc *attr;
941 struct attr_value *av;
942 const char *name_ptr, *p;
943 rtx orexp, newexp;
945 switch (GET_CODE (exp))
947 case EQ_ATTR:
948 /* Handle negation test. */
949 if (XSTR (exp, 1)[0] == '!')
950 return check_attr_test (attr_rtx (NOT,
951 attr_eq (XSTR (exp, 0),
952 &XSTR (exp, 1)[1])),
953 is_const, lineno);
955 else if (n_comma_elts (XSTR (exp, 1)) == 1)
957 attr = find_attr (XSTR (exp, 0), 0);
958 if (attr == NULL)
960 if (! strcmp (XSTR (exp, 0), "alternative"))
962 XSTR (exp, 0) = alternative_name;
963 /* This can't be simplified any further. */
964 RTX_UNCHANGING_P (exp) = 1;
965 return exp;
967 else
968 fatal ("Unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
971 if (is_const && ! attr->is_const)
972 fatal ("Constant expression uses insn attribute `%s' in EQ_ATTR",
973 XSTR (exp, 0));
975 /* Copy this just to make it permanent,
976 so expressions using it can be permanent too. */
977 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
979 /* It shouldn't be possible to simplify the value given to a
980 constant attribute, so don't expand this until it's time to
981 write the test expression. */
982 if (attr->is_const)
983 RTX_UNCHANGING_P (exp) = 1;
985 if (attr->is_numeric)
987 for (p = XSTR (exp, 1); *p; p++)
988 if (*p < '0' || *p > '9')
989 fatal ("Attribute `%s' takes only numeric values",
990 XSTR (exp, 0));
992 else
994 for (av = attr->first_value; av; av = av->next)
995 if (GET_CODE (av->value) == CONST_STRING
996 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
997 break;
999 if (av == NULL)
1000 fatal ("Unknown value `%s' for `%s' attribute",
1001 XSTR (exp, 1), XSTR (exp, 0));
1004 else
1006 /* Make an IOR tree of the possible values. */
1007 orexp = false_rtx;
1008 name_ptr = XSTR (exp, 1);
1009 while ((p = next_comma_elt (&name_ptr)) != NULL)
1011 newexp = attr_eq (XSTR (exp, 0), p);
1012 orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
1015 return check_attr_test (orexp, is_const, lineno);
1017 break;
1019 case ATTR_FLAG:
1020 break;
1022 case CONST_INT:
1023 /* Either TRUE or FALSE. */
1024 if (XWINT (exp, 0))
1025 return true_rtx;
1026 else
1027 return false_rtx;
1029 case IOR:
1030 case AND:
1031 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
1032 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const, lineno);
1033 break;
1035 case NOT:
1036 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
1037 break;
1039 case MATCH_INSN:
1040 case MATCH_OPERAND:
1041 if (is_const)
1042 fatal ("RTL operator \"%s\" not valid in constant attribute test",
1043 GET_RTX_NAME (GET_CODE (exp)));
1044 /* These cases can't be simplified. */
1045 RTX_UNCHANGING_P (exp) = 1;
1046 break;
1048 case LE: case LT: case GT: case GE:
1049 case LEU: case LTU: case GTU: case GEU:
1050 case NE: case EQ:
1051 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
1052 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
1053 exp = attr_rtx (GET_CODE (exp),
1054 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
1055 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
1056 /* These cases can't be simplified. */
1057 RTX_UNCHANGING_P (exp) = 1;
1058 break;
1060 case SYMBOL_REF:
1061 if (is_const)
1063 /* These cases are valid for constant attributes, but can't be
1064 simplified. */
1065 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1066 RTX_UNCHANGING_P (exp) = 1;
1067 break;
1069 default:
1070 fatal ("RTL operator \"%s\" not valid in attribute test",
1071 GET_RTX_NAME (GET_CODE (exp)));
1074 return exp;
1077 /* Given an expression, ensure that it is validly formed and that all named
1078 attribute values are valid for the given attribute. Issue a fatal error
1079 if not. If no attribute is specified, assume a numeric attribute.
1081 Return a perhaps modified replacement expression for the value. */
1083 static rtx
1084 check_attr_value (exp, attr)
1085 rtx exp;
1086 struct attr_desc *attr;
1088 struct attr_value *av;
1089 const char *p;
1090 int i;
1092 switch (GET_CODE (exp))
1094 case CONST_INT:
1095 if (attr && ! attr->is_numeric)
1097 message_with_line (attr->lineno,
1098 "CONST_INT not valid for non-numeric attribute %s",
1099 attr->name);
1100 have_error = 1;
1101 break;
1104 if (INTVAL (exp) < 0 && ! attr->negative_ok)
1106 message_with_line (attr->lineno,
1107 "negative numeric value specified for attribute %s",
1108 attr->name);
1109 have_error = 1;
1110 break;
1112 break;
1114 case CONST_STRING:
1115 if (! strcmp (XSTR (exp, 0), "*"))
1116 break;
1118 if (attr == 0 || attr->is_numeric)
1120 p = XSTR (exp, 0);
1121 if (attr && attr->negative_ok && *p == '-')
1122 p++;
1123 for (; *p; p++)
1124 if (*p > '9' || *p < '0')
1126 message_with_line (attr ? attr->lineno : 0,
1127 "non-numeric value for numeric attribute %s",
1128 attr ? attr->name : "internal");
1129 have_error = 1;
1130 break;
1132 break;
1135 for (av = attr->first_value; av; av = av->next)
1136 if (GET_CODE (av->value) == CONST_STRING
1137 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
1138 break;
1140 if (av == NULL)
1142 message_with_line (attr->lineno,
1143 "unknown value `%s' for `%s' attribute",
1144 XSTR (exp, 0), attr ? attr->name : "internal");
1145 have_error = 1;
1147 break;
1149 case IF_THEN_ELSE:
1150 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
1151 attr ? attr->is_const : 0,
1152 attr ? attr->lineno : 0);
1153 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1154 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
1155 break;
1157 case PLUS:
1158 case MINUS:
1159 case MULT:
1160 case DIV:
1161 case MOD:
1162 if (attr && !attr->is_numeric)
1164 message_with_line (attr->lineno,
1165 "invalid operation `%s' for non-numeric attribute value",
1166 GET_RTX_NAME (GET_CODE (exp)));
1167 have_error = 1;
1168 break;
1170 /* FALLTHRU */
1172 case IOR:
1173 case AND:
1174 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1175 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1176 break;
1178 case FFS:
1179 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1180 break;
1182 case COND:
1183 if (XVECLEN (exp, 0) % 2 != 0)
1185 message_with_line (attr->lineno,
1186 "first operand of COND must have even length");
1187 have_error = 1;
1188 break;
1191 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1193 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
1194 attr ? attr->is_const : 0,
1195 attr ? attr->lineno : 0);
1196 XVECEXP (exp, 0, i + 1)
1197 = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
1200 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1201 break;
1203 case ATTR:
1205 struct attr_desc *attr2 = find_attr (XSTR (exp, 0), 0);
1206 if (attr2 == NULL)
1208 message_with_line (attr ? attr->lineno : 0,
1209 "unknown attribute `%s' in ATTR",
1210 XSTR (exp, 0));
1211 have_error = 1;
1213 else if (attr && attr->is_const && ! attr2->is_const)
1215 message_with_line (attr->lineno,
1216 "non-constant attribute `%s' referenced from `%s'",
1217 XSTR (exp, 0), attr->name);
1218 have_error = 1;
1220 else if (attr
1221 && (attr->is_numeric != attr2->is_numeric
1222 || (! attr->negative_ok && attr2->negative_ok)))
1224 message_with_line (attr->lineno,
1225 "numeric attribute mismatch calling `%s' from `%s'",
1226 XSTR (exp, 0), attr->name);
1227 have_error = 1;
1230 break;
1232 case SYMBOL_REF:
1233 /* A constant SYMBOL_REF is valid as a constant attribute test and
1234 is expanded later by make_canonical into a COND. In a non-constant
1235 attribute test, it is left be. */
1236 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1238 default:
1239 message_with_line (attr ? attr->lineno : 0,
1240 "invalid operation `%s' for attribute value",
1241 GET_RTX_NAME (GET_CODE (exp)));
1242 have_error = 1;
1243 break;
1246 return exp;
1249 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1250 It becomes a COND with each test being (eq_attr "alternative "n") */
1252 static rtx
1253 convert_set_attr_alternative (exp, id)
1254 rtx exp;
1255 struct insn_def *id;
1257 int num_alt = id->num_alternatives;
1258 rtx condexp;
1259 int i;
1261 if (XVECLEN (exp, 1) != num_alt)
1263 message_with_line (id->lineno,
1264 "bad number of entries in SET_ATTR_ALTERNATIVE");
1265 have_error = 1;
1266 return NULL_RTX;
1269 /* Make a COND with all tests but the last. Select the last value via the
1270 default. */
1271 condexp = rtx_alloc (COND);
1272 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1274 for (i = 0; i < num_alt - 1; i++)
1276 const char *p;
1277 p = attr_numeral (i);
1279 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1280 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1283 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1285 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1288 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1289 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1291 static rtx
1292 convert_set_attr (exp, id)
1293 rtx exp;
1294 struct insn_def *id;
1296 rtx newexp;
1297 const char *name_ptr;
1298 char *p;
1299 int n;
1301 /* See how many alternative specified. */
1302 n = n_comma_elts (XSTR (exp, 1));
1303 if (n == 1)
1304 return attr_rtx (SET,
1305 attr_rtx (ATTR, XSTR (exp, 0)),
1306 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1308 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1309 XSTR (newexp, 0) = XSTR (exp, 0);
1310 XVEC (newexp, 1) = rtvec_alloc (n);
1312 /* Process each comma-separated name. */
1313 name_ptr = XSTR (exp, 1);
1314 n = 0;
1315 while ((p = next_comma_elt (&name_ptr)) != NULL)
1316 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1318 return convert_set_attr_alternative (newexp, id);
1321 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1322 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1323 expressions. */
1325 static void
1326 check_defs ()
1328 struct insn_def *id;
1329 struct attr_desc *attr;
1330 int i;
1331 rtx value;
1333 for (id = defs; id; id = id->next)
1335 if (XVEC (id->def, id->vec_idx) == NULL)
1336 continue;
1338 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1340 value = XVECEXP (id->def, id->vec_idx, i);
1341 switch (GET_CODE (value))
1343 case SET:
1344 if (GET_CODE (XEXP (value, 0)) != ATTR)
1346 message_with_line (id->lineno, "bad attribute set");
1347 have_error = 1;
1348 value = NULL_RTX;
1350 break;
1352 case SET_ATTR_ALTERNATIVE:
1353 value = convert_set_attr_alternative (value, id);
1354 break;
1356 case SET_ATTR:
1357 value = convert_set_attr (value, id);
1358 break;
1360 default:
1361 message_with_line (id->lineno, "invalid attribute code %s",
1362 GET_RTX_NAME (GET_CODE (value)));
1363 have_error = 1;
1364 value = NULL_RTX;
1366 if (value == NULL_RTX)
1367 continue;
1369 if ((attr = find_attr (XSTR (XEXP (value, 0), 0), 0)) == NULL)
1371 message_with_line (id->lineno, "unknown attribute %s",
1372 XSTR (XEXP (value, 0), 0));
1373 have_error = 1;
1374 continue;
1377 XVECEXP (id->def, id->vec_idx, i) = value;
1378 XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1383 #if 0
1384 /* Given a constant SYMBOL_REF expression, convert to a COND that
1385 explicitly tests each enumerated value. */
1387 static rtx
1388 convert_const_symbol_ref (exp, attr)
1389 rtx exp;
1390 struct attr_desc *attr;
1392 rtx condexp;
1393 struct attr_value *av;
1394 int i;
1395 int num_alt = 0;
1397 for (av = attr->first_value; av; av = av->next)
1398 num_alt++;
1400 /* Make a COND with all tests but the last, and in the original order.
1401 Select the last value via the default. Note that the attr values
1402 are constructed in reverse order. */
1404 condexp = rtx_alloc (COND);
1405 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1406 av = attr->first_value;
1407 XEXP (condexp, 1) = av->value;
1409 for (i = num_alt - 2; av = av->next, i >= 0; i--)
1411 char *p, *string;
1412 rtx value;
1414 string = p = (char *) oballoc (2
1415 + strlen (attr->name)
1416 + strlen (XSTR (av->value, 0)));
1417 strcpy (p, attr->name);
1418 strcat (p, "_");
1419 strcat (p, XSTR (av->value, 0));
1420 for (; *p != '\0'; p++)
1421 *p = TOUPPER (*p);
1423 value = attr_rtx (SYMBOL_REF, string);
1424 RTX_UNCHANGING_P (value) = 1;
1426 XVECEXP (condexp, 0, 2 * i) = attr_rtx (EQ, exp, value);
1428 XVECEXP (condexp, 0, 2 * i + 1) = av->value;
1431 return condexp;
1433 #endif
1435 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1436 expressions by converting them into a COND. This removes cases from this
1437 program. Also, replace an attribute value of "*" with the default attribute
1438 value. */
1440 static rtx
1441 make_canonical (attr, exp)
1442 struct attr_desc *attr;
1443 rtx exp;
1445 int i;
1446 rtx newexp;
1448 switch (GET_CODE (exp))
1450 case CONST_INT:
1451 exp = make_numeric_value (INTVAL (exp));
1452 break;
1454 case CONST_STRING:
1455 if (! strcmp (XSTR (exp, 0), "*"))
1457 if (attr == 0 || attr->default_val == 0)
1458 fatal ("(attr_value \"*\") used in invalid context.");
1459 exp = attr->default_val->value;
1462 break;
1464 case SYMBOL_REF:
1465 if (!attr->is_const || RTX_UNCHANGING_P (exp))
1466 break;
1467 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1468 This makes the COND something that won't be considered an arbitrary
1469 expression by walk_attr_value. */
1470 RTX_UNCHANGING_P (exp) = 1;
1471 #if 0
1472 /* ??? Why do we do this? With attribute values { A B C D E }, this
1473 tends to generate (!(x==A) && !(x==B) && !(x==C) && !(x==D)) rather
1474 than (x==E). */
1475 exp = convert_const_symbol_ref (exp, attr);
1476 RTX_UNCHANGING_P (exp) = 1;
1477 exp = check_attr_value (exp, attr);
1478 /* Goto COND case since this is now a COND. Note that while the
1479 new expression is rescanned, all symbol_ref notes are marked as
1480 unchanging. */
1481 goto cond;
1482 #else
1483 exp = check_attr_value (exp, attr);
1484 break;
1485 #endif
1487 case IF_THEN_ELSE:
1488 newexp = rtx_alloc (COND);
1489 XVEC (newexp, 0) = rtvec_alloc (2);
1490 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1491 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1493 XEXP (newexp, 1) = XEXP (exp, 2);
1495 exp = newexp;
1496 /* Fall through to COND case since this is now a COND. */
1498 case COND:
1500 int allsame = 1;
1501 rtx defval;
1503 /* First, check for degenerate COND. */
1504 if (XVECLEN (exp, 0) == 0)
1505 return make_canonical (attr, XEXP (exp, 1));
1506 defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1508 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1510 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1511 XVECEXP (exp, 0, i + 1)
1512 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1513 if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1514 allsame = 0;
1516 if (allsame)
1517 return defval;
1519 break;
1521 default:
1522 break;
1525 return exp;
1528 static rtx
1529 copy_boolean (exp)
1530 rtx exp;
1532 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1533 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1534 copy_boolean (XEXP (exp, 1)));
1535 return exp;
1538 /* Given a value and an attribute description, return a `struct attr_value *'
1539 that represents that value. This is either an existing structure, if the
1540 value has been previously encountered, or a newly-created structure.
1542 `insn_code' is the code of an insn whose attribute has the specified
1543 value (-2 if not processing an insn). We ensure that all insns for
1544 a given value have the same number of alternatives if the value checks
1545 alternatives. */
1547 static struct attr_value *
1548 get_attr_value (value, attr, insn_code)
1549 rtx value;
1550 struct attr_desc *attr;
1551 int insn_code;
1553 struct attr_value *av;
1554 int num_alt = 0;
1556 value = make_canonical (attr, value);
1557 if (compares_alternatives_p (value))
1559 if (insn_code < 0 || insn_alternatives == NULL)
1560 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1561 else
1562 num_alt = insn_alternatives[insn_code];
1565 for (av = attr->first_value; av; av = av->next)
1566 if (rtx_equal_p (value, av->value)
1567 && (num_alt == 0 || av->first_insn == NULL
1568 || insn_alternatives[av->first_insn->insn_code]))
1569 return av;
1571 av = (struct attr_value *) oballoc (sizeof (struct attr_value));
1572 av->value = value;
1573 av->next = attr->first_value;
1574 attr->first_value = av;
1575 av->first_insn = NULL;
1576 av->num_insns = 0;
1577 av->has_asm_insn = 0;
1579 return av;
1582 /* After all DEFINE_DELAYs have been read in, create internal attributes
1583 to generate the required routines.
1585 First, we compute the number of delay slots for each insn (as a COND of
1586 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1587 delay type is specified, we compute a similar function giving the
1588 DEFINE_DELAY ordinal for each insn.
1590 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1591 tells whether a given insn can be in that delay slot.
1593 Normal attribute filling and optimization expands these to contain the
1594 information needed to handle delay slots. */
1596 static void
1597 expand_delays ()
1599 struct delay_desc *delay;
1600 rtx condexp;
1601 rtx newexp;
1602 int i;
1603 char *p;
1605 /* First, generate data for `num_delay_slots' function. */
1607 condexp = rtx_alloc (COND);
1608 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1609 XEXP (condexp, 1) = make_numeric_value (0);
1611 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1613 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1614 XVECEXP (condexp, 0, i + 1)
1615 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1618 make_internal_attr ("*num_delay_slots", condexp, 0);
1620 /* If more than one delay type, do the same for computing the delay type. */
1621 if (num_delays > 1)
1623 condexp = rtx_alloc (COND);
1624 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1625 XEXP (condexp, 1) = make_numeric_value (0);
1627 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1629 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1630 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1633 make_internal_attr ("*delay_type", condexp, 1);
1636 /* For each delay possibility and delay slot, compute an eligibility
1637 attribute for non-annulled insns and for each type of annulled (annul
1638 if true and annul if false). */
1639 for (delay = delays; delay; delay = delay->next)
1641 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1643 condexp = XVECEXP (delay->def, 1, i);
1644 if (condexp == 0) condexp = false_rtx;
1645 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1646 make_numeric_value (1), make_numeric_value (0));
1648 p = attr_printf (sizeof ("*delay__") + MAX_DIGITS*2, "*delay_%d_%d",
1649 delay->num, i / 3);
1650 make_internal_attr (p, newexp, 1);
1652 if (have_annul_true)
1654 condexp = XVECEXP (delay->def, 1, i + 1);
1655 if (condexp == 0) condexp = false_rtx;
1656 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1657 make_numeric_value (1),
1658 make_numeric_value (0));
1659 p = attr_printf (sizeof ("*annul_true__") + MAX_DIGITS*2,
1660 "*annul_true_%d_%d", delay->num, i / 3);
1661 make_internal_attr (p, newexp, 1);
1664 if (have_annul_false)
1666 condexp = XVECEXP (delay->def, 1, i + 2);
1667 if (condexp == 0) condexp = false_rtx;
1668 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1669 make_numeric_value (1),
1670 make_numeric_value (0));
1671 p = attr_printf (sizeof ("*annul_false__") + MAX_DIGITS*2,
1672 "*annul_false_%d_%d", delay->num, i / 3);
1673 make_internal_attr (p, newexp, 1);
1679 /* This function is given a left and right side expression and an operator.
1680 Each side is a conditional expression, each alternative of which has a
1681 numerical value. The function returns another conditional expression
1682 which, for every possible set of condition values, returns a value that is
1683 the operator applied to the values of the two sides.
1685 Since this is called early, it must also support IF_THEN_ELSE. */
1687 static rtx
1688 operate_exp (op, left, right)
1689 enum operator op;
1690 rtx left, right;
1692 int left_value, right_value;
1693 rtx newexp;
1694 int i;
1696 /* If left is a string, apply operator to it and the right side. */
1697 if (GET_CODE (left) == CONST_STRING)
1699 /* If right is also a string, just perform the operation. */
1700 if (GET_CODE (right) == CONST_STRING)
1702 left_value = atoi (XSTR (left, 0));
1703 right_value = atoi (XSTR (right, 0));
1704 switch (op)
1706 case PLUS_OP:
1707 i = left_value + right_value;
1708 break;
1710 case MINUS_OP:
1711 i = left_value - right_value;
1712 break;
1714 case POS_MINUS_OP: /* The positive part of LEFT - RIGHT. */
1715 if (left_value > right_value)
1716 i = left_value - right_value;
1717 else
1718 i = 0;
1719 break;
1721 case OR_OP:
1722 case ORX_OP:
1723 i = left_value | right_value;
1724 break;
1726 case EQ_OP:
1727 i = left_value == right_value;
1728 break;
1730 case RANGE_OP:
1731 i = (left_value << (HOST_BITS_PER_INT / 2)) | right_value;
1732 break;
1734 case MAX_OP:
1735 if (left_value > right_value)
1736 i = left_value;
1737 else
1738 i = right_value;
1739 break;
1741 case MIN_OP:
1742 if (left_value < right_value)
1743 i = left_value;
1744 else
1745 i = right_value;
1746 break;
1748 default:
1749 abort ();
1752 if (i == left_value)
1753 return left;
1754 if (i == right_value)
1755 return right;
1756 return make_numeric_value (i);
1758 else if (GET_CODE (right) == IF_THEN_ELSE)
1760 /* Apply recursively to all values within. */
1761 rtx newleft = operate_exp (op, left, XEXP (right, 1));
1762 rtx newright = operate_exp (op, left, XEXP (right, 2));
1763 if (rtx_equal_p (newleft, newright))
1764 return newleft;
1765 return attr_rtx (IF_THEN_ELSE, XEXP (right, 0), newleft, newright);
1767 else if (GET_CODE (right) == COND)
1769 int allsame = 1;
1770 rtx defval;
1772 newexp = rtx_alloc (COND);
1773 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (right, 0));
1774 defval = XEXP (newexp, 1) = operate_exp (op, left, XEXP (right, 1));
1776 for (i = 0; i < XVECLEN (right, 0); i += 2)
1778 XVECEXP (newexp, 0, i) = XVECEXP (right, 0, i);
1779 XVECEXP (newexp, 0, i + 1)
1780 = operate_exp (op, left, XVECEXP (right, 0, i + 1));
1781 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1782 defval))
1783 allsame = 0;
1786 /* If the resulting cond is trivial (all alternatives
1787 give the same value), optimize it away. */
1788 if (allsame)
1790 if (!ggc_p)
1791 obstack_free (rtl_obstack, newexp);
1792 return operate_exp (op, left, XEXP (right, 1));
1795 /* If the result is the same as the RIGHT operand,
1796 just use that. */
1797 if (rtx_equal_p (newexp, right))
1799 if (!ggc_p)
1800 obstack_free (rtl_obstack, newexp);
1801 return right;
1804 return newexp;
1806 else
1807 fatal ("Badly formed attribute value");
1810 /* A hack to prevent expand_units from completely blowing up: ORX_OP does
1811 not associate through IF_THEN_ELSE. */
1812 else if (op == ORX_OP && GET_CODE (right) == IF_THEN_ELSE)
1814 return attr_rtx (IOR, left, right);
1817 /* Otherwise, do recursion the other way. */
1818 else if (GET_CODE (left) == IF_THEN_ELSE)
1820 rtx newleft = operate_exp (op, XEXP (left, 1), right);
1821 rtx newright = operate_exp (op, XEXP (left, 2), right);
1822 if (rtx_equal_p (newleft, newright))
1823 return newleft;
1824 return attr_rtx (IF_THEN_ELSE, XEXP (left, 0), newleft, newright);
1826 else if (GET_CODE (left) == COND)
1828 int allsame = 1;
1829 rtx defval;
1831 newexp = rtx_alloc (COND);
1832 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (left, 0));
1833 defval = XEXP (newexp, 1) = operate_exp (op, XEXP (left, 1), right);
1835 for (i = 0; i < XVECLEN (left, 0); i += 2)
1837 XVECEXP (newexp, 0, i) = XVECEXP (left, 0, i);
1838 XVECEXP (newexp, 0, i + 1)
1839 = operate_exp (op, XVECEXP (left, 0, i + 1), right);
1840 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1841 defval))
1842 allsame = 0;
1845 /* If the cond is trivial (all alternatives give the same value),
1846 optimize it away. */
1847 if (allsame)
1849 if (!ggc_p)
1850 obstack_free (rtl_obstack, newexp);
1851 return operate_exp (op, XEXP (left, 1), right);
1854 /* If the result is the same as the LEFT operand,
1855 just use that. */
1856 if (rtx_equal_p (newexp, left))
1858 if (!ggc_p)
1859 obstack_free (rtl_obstack, newexp);
1860 return left;
1863 return newexp;
1866 else
1867 fatal ("Badly formed attribute value.");
1868 /* NOTREACHED */
1869 return NULL;
1872 /* Once all attributes and DEFINE_FUNCTION_UNITs have been read, we
1873 construct a number of attributes.
1875 The first produces a function `function_units_used' which is given an
1876 insn and produces an encoding showing which function units are required
1877 for the execution of that insn. If the value is non-negative, the insn
1878 uses that unit; otherwise, the value is a one's compliment mask of units
1879 used.
1881 The second produces a function `result_ready_cost' which is used to
1882 determine the time that the result of an insn will be ready and hence
1883 a worst-case schedule.
1885 Both of these produce quite complex expressions which are then set as the
1886 default value of internal attributes. Normal attribute simplification
1887 should produce reasonable expressions.
1889 For each unit, a `<name>_unit_ready_cost' function will take an
1890 insn and give the delay until that unit will be ready with the result
1891 and a `<name>_unit_conflict_cost' function is given an insn already
1892 executing on the unit and a candidate to execute and will give the
1893 cost from the time the executing insn started until the candidate
1894 can start (ignore limitations on the number of simultaneous insns).
1896 For each unit, a `<name>_unit_blockage' function is given an insn
1897 already executing on the unit and a candidate to execute and will
1898 give the delay incurred due to function unit conflicts. The range of
1899 blockage cost values for a given executing insn is given by the
1900 `<name>_unit_blockage_range' function. These values are encoded in
1901 an int where the upper half gives the minimum value and the lower
1902 half gives the maximum value. */
1904 static void
1905 expand_units ()
1907 struct function_unit *unit, **unit_num;
1908 struct function_unit_op *op, **op_array, ***unit_ops;
1909 rtx unitsmask;
1910 rtx readycost;
1911 rtx newexp;
1912 const char *str;
1913 int i, j, u, num, nvalues;
1915 /* Rebuild the condition for the unit to share the RTL expressions.
1916 Sharing is required by simplify_by_exploding. Build the issue delay
1917 expressions. Validate the expressions we were given for the conditions
1918 and conflict vector. Then make attributes for use in the conflict
1919 function. */
1921 for (unit = units; unit; unit = unit->next)
1923 unit->condexp = check_attr_test (unit->condexp, 0, unit->first_lineno);
1925 for (op = unit->ops; op; op = op->next)
1927 rtx issue_delay = make_numeric_value (op->issue_delay);
1928 rtx issue_exp = issue_delay;
1930 /* Build, validate, and simplify the issue delay expression. */
1931 if (op->conflict_exp != true_rtx)
1932 issue_exp = attr_rtx (IF_THEN_ELSE, op->conflict_exp,
1933 issue_exp, make_numeric_value (0));
1934 issue_exp = check_attr_value (make_canonical (NULL_ATTR,
1935 issue_exp),
1936 NULL_ATTR);
1937 issue_exp = simplify_knowing (issue_exp, unit->condexp);
1938 op->issue_exp = issue_exp;
1940 /* Make an attribute for use in the conflict function if needed. */
1941 unit->needs_conflict_function = (unit->issue_delay.min
1942 != unit->issue_delay.max);
1943 if (unit->needs_conflict_function)
1945 str = attr_printf (strlen (unit->name) + sizeof ("*_cost_") + MAX_DIGITS,
1946 "*%s_cost_%d", unit->name, op->num);
1947 make_internal_attr (str, issue_exp, 1);
1950 /* Validate the condition. */
1951 op->condexp = check_attr_test (op->condexp, 0, op->lineno);
1955 /* Compute the mask of function units used. Initially, the unitsmask is
1956 zero. Set up a conditional to compute each unit's contribution. */
1957 unitsmask = make_numeric_value (0);
1958 newexp = rtx_alloc (IF_THEN_ELSE);
1959 XEXP (newexp, 2) = make_numeric_value (0);
1961 /* If we have just a few units, we may be all right expanding the whole
1962 thing. But the expansion is 2**N in space on the number of opclasses,
1963 so we can't do this for very long -- Alpha and MIPS in particular have
1964 problems with this. So in that situation, we fall back on an alternate
1965 implementation method. */
1966 #define NUM_UNITOP_CUTOFF 20
1968 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1970 /* Merge each function unit into the unit mask attributes. */
1971 for (unit = units; unit; unit = unit->next)
1973 XEXP (newexp, 0) = unit->condexp;
1974 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1975 unitsmask = operate_exp (OR_OP, unitsmask, newexp);
1978 else
1980 /* Merge each function unit into the unit mask attributes. */
1981 for (unit = units; unit; unit = unit->next)
1983 XEXP (newexp, 0) = unit->condexp;
1984 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1985 unitsmask = operate_exp (ORX_OP, unitsmask, attr_copy_rtx (newexp));
1989 /* Simplify the unit mask expression, encode it, and make an attribute
1990 for the function_units_used function. */
1991 unitsmask = simplify_by_exploding (unitsmask);
1993 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1994 unitsmask = encode_units_mask (unitsmask);
1995 else
1997 /* We can no longer encode unitsmask at compile time, so emit code to
1998 calculate it at runtime. Rather, put a marker for where we'd do
1999 the code, and actually output it in write_attr_get(). */
2000 unitsmask = attr_rtx (FFS, unitsmask);
2003 make_internal_attr ("*function_units_used", unitsmask, 10);
2005 /* Create an array of ops for each unit. Add an extra unit for the
2006 result_ready_cost function that has the ops of all other units. */
2007 unit_ops = (struct function_unit_op ***)
2008 alloca ((num_units + 1) * sizeof (struct function_unit_op **));
2009 unit_num = (struct function_unit **)
2010 alloca ((num_units + 1) * sizeof (struct function_unit *));
2012 unit_num[num_units] = unit = (struct function_unit *)
2013 alloca (sizeof (struct function_unit));
2014 unit->num = num_units;
2015 unit->num_opclasses = 0;
2017 for (unit = units; unit; unit = unit->next)
2019 unit_num[num_units]->num_opclasses += unit->num_opclasses;
2020 unit_num[unit->num] = unit;
2021 unit_ops[unit->num] = op_array = (struct function_unit_op **)
2022 alloca (unit->num_opclasses * sizeof (struct function_unit_op *));
2024 for (op = unit->ops; op; op = op->next)
2025 op_array[op->num] = op;
2028 /* Compose the array of ops for the extra unit. */
2029 unit_ops[num_units] = op_array = (struct function_unit_op **)
2030 alloca (unit_num[num_units]->num_opclasses
2031 * sizeof (struct function_unit_op *));
2033 for (unit = units, i = 0; unit; i += unit->num_opclasses, unit = unit->next)
2034 bcopy ((char *) unit_ops[unit->num], (char *) &op_array[i],
2035 unit->num_opclasses * sizeof (struct function_unit_op *));
2037 /* Compute the ready cost function for each unit by computing the
2038 condition for each non-default value. */
2039 for (u = 0; u <= num_units; u++)
2041 rtx orexp;
2042 int value;
2044 unit = unit_num[u];
2045 op_array = unit_ops[unit->num];
2046 num = unit->num_opclasses;
2048 /* Sort the array of ops into increasing ready cost order. */
2049 for (i = 0; i < num; i++)
2050 for (j = num - 1; j > i; j--)
2051 if (op_array[j-1]->ready < op_array[j]->ready)
2053 op = op_array[j];
2054 op_array[j] = op_array[j-1];
2055 op_array[j-1] = op;
2058 /* Determine how many distinct non-default ready cost values there
2059 are. We use a default ready cost value of 1. */
2060 nvalues = 0; value = 1;
2061 for (i = num - 1; i >= 0; i--)
2062 if (op_array[i]->ready > value)
2064 value = op_array[i]->ready;
2065 nvalues++;
2068 if (nvalues == 0)
2069 readycost = make_numeric_value (1);
2070 else
2072 /* Construct the ready cost expression as a COND of each value from
2073 the largest to the smallest. */
2074 readycost = rtx_alloc (COND);
2075 XVEC (readycost, 0) = rtvec_alloc (nvalues * 2);
2076 XEXP (readycost, 1) = make_numeric_value (1);
2078 nvalues = 0; orexp = false_rtx; value = op_array[0]->ready;
2079 for (i = 0; i < num; i++)
2081 op = op_array[i];
2082 if (op->ready <= 1)
2083 break;
2084 else if (op->ready == value)
2085 orexp = insert_right_side (IOR, orexp, op->condexp, -2, -2);
2086 else
2088 XVECEXP (readycost, 0, nvalues * 2) = orexp;
2089 XVECEXP (readycost, 0, nvalues * 2 + 1)
2090 = make_numeric_value (value);
2091 nvalues++;
2092 value = op->ready;
2093 orexp = op->condexp;
2096 XVECEXP (readycost, 0, nvalues * 2) = orexp;
2097 XVECEXP (readycost, 0, nvalues * 2 + 1) = make_numeric_value (value);
2100 if (u < num_units)
2102 rtx max_blockage = 0, min_blockage = 0;
2104 /* Simplify the readycost expression by only considering insns
2105 that use the unit. */
2106 readycost = simplify_knowing (readycost, unit->condexp);
2108 /* Determine the blockage cost the executing insn (E) given
2109 the candidate insn (C). This is the maximum of the issue
2110 delay, the pipeline delay, and the simultaneity constraint.
2111 Each function_unit_op represents the characteristics of the
2112 candidate insn, so in the expressions below, C is a known
2113 term and E is an unknown term.
2115 We compute the blockage cost for each E for every possible C.
2116 Thus OP represents E, and READYCOST is a list of values for
2117 every possible C.
2119 The issue delay function for C is op->issue_exp and is used to
2120 write the `<name>_unit_conflict_cost' function. Symbolicly
2121 this is "ISSUE-DELAY (E,C)".
2123 The pipeline delay results form the FIFO constraint on the
2124 function unit and is "READY-COST (E) + 1 - READY-COST (C)".
2126 The simultaneity constraint is based on how long it takes to
2127 fill the unit given the minimum issue delay. FILL-TIME is the
2128 constant "MIN (ISSUE-DELAY (*,*)) * (SIMULTANEITY - 1)", and
2129 the simultaneity constraint is "READY-COST (E) - FILL-TIME"
2130 if SIMULTANEITY is non-zero and zero otherwise.
2132 Thus, BLOCKAGE (E,C) when SIMULTANEITY is zero is
2134 MAX (ISSUE-DELAY (E,C),
2135 READY-COST (E) - (READY-COST (C) - 1))
2137 and otherwise
2139 MAX (ISSUE-DELAY (E,C),
2140 READY-COST (E) - (READY-COST (C) - 1),
2141 READY-COST (E) - FILL-TIME)
2143 The `<name>_unit_blockage' function is computed by determining
2144 this value for each candidate insn. As these values are
2145 computed, we also compute the upper and lower bounds for
2146 BLOCKAGE (E,*). These are combined to form the function
2147 `<name>_unit_blockage_range'. Finally, the maximum blockage
2148 cost, MAX (BLOCKAGE (*,*)), is computed. */
2150 for (op = unit->ops; op; op = op->next)
2152 rtx blockage = op->issue_exp;
2153 blockage = simplify_knowing (blockage, unit->condexp);
2155 /* Add this op's contribution to MAX (BLOCKAGE (E,*)) and
2156 MIN (BLOCKAGE (E,*)). */
2157 if (max_blockage == 0)
2158 max_blockage = min_blockage = blockage;
2159 else
2161 max_blockage
2162 = simplify_knowing (operate_exp (MAX_OP, max_blockage,
2163 blockage),
2164 unit->condexp);
2165 min_blockage
2166 = simplify_knowing (operate_exp (MIN_OP, min_blockage,
2167 blockage),
2168 unit->condexp);
2171 /* Make an attribute for use in the blockage function. */
2172 str = attr_printf (strlen (unit->name) + sizeof ("*_block_") + MAX_DIGITS,
2173 "*%s_block_%d", unit->name, op->num);
2174 make_internal_attr (str, blockage, 1);
2177 /* Record MAX (BLOCKAGE (*,*)). */
2179 int unknown;
2180 unit->max_blockage = max_attr_value (max_blockage, &unknown);
2183 /* See if the upper and lower bounds of BLOCKAGE (E,*) are the
2184 same. If so, the blockage function carries no additional
2185 information and is not written. */
2186 newexp = operate_exp (EQ_OP, max_blockage, min_blockage);
2187 newexp = simplify_knowing (newexp, unit->condexp);
2188 unit->needs_blockage_function
2189 = (GET_CODE (newexp) != CONST_STRING
2190 || atoi (XSTR (newexp, 0)) != 1);
2192 /* If the all values of BLOCKAGE (E,C) have the same value,
2193 neither blockage function is written. */
2194 unit->needs_range_function
2195 = (unit->needs_blockage_function
2196 || GET_CODE (max_blockage) != CONST_STRING);
2198 if (unit->needs_range_function)
2200 /* Compute the blockage range function and make an attribute
2201 for writing its value. */
2202 newexp = operate_exp (RANGE_OP, min_blockage, max_blockage);
2203 newexp = simplify_knowing (newexp, unit->condexp);
2205 str = attr_printf (strlen (unit->name) + sizeof ("*_unit_blockage_range"),
2206 "*%s_unit_blockage_range", unit->name);
2207 make_internal_attr (str, newexp, 20);
2210 str = attr_printf (strlen (unit->name) + sizeof ("*_unit_ready_cost"),
2211 "*%s_unit_ready_cost", unit->name);
2213 else
2214 str = "*result_ready_cost";
2216 /* Make an attribute for the ready_cost function. Simplifying
2217 further with simplify_by_exploding doesn't win. */
2218 make_internal_attr (str, readycost, 0);
2221 /* For each unit that requires a conflict cost function, make an attribute
2222 that maps insns to the operation number. */
2223 for (unit = units; unit; unit = unit->next)
2225 rtx caseexp;
2227 if (! unit->needs_conflict_function
2228 && ! unit->needs_blockage_function)
2229 continue;
2231 caseexp = rtx_alloc (COND);
2232 XVEC (caseexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
2234 for (op = unit->ops; op; op = op->next)
2236 /* Make our adjustment to the COND being computed. If we are the
2237 last operation class, place our values into the default of the
2238 COND. */
2239 if (op->num == unit->num_opclasses - 1)
2241 XEXP (caseexp, 1) = make_numeric_value (op->num);
2243 else
2245 XVECEXP (caseexp, 0, op->num * 2) = op->condexp;
2246 XVECEXP (caseexp, 0, op->num * 2 + 1)
2247 = make_numeric_value (op->num);
2251 /* Simplifying caseexp with simplify_by_exploding doesn't win. */
2252 str = attr_printf (strlen (unit->name) + sizeof ("*_cases"),
2253 "*%s_cases", unit->name);
2254 make_internal_attr (str, caseexp, 1);
2258 /* Simplify EXP given KNOWN_TRUE. */
2260 static rtx
2261 simplify_knowing (exp, known_true)
2262 rtx exp, known_true;
2264 if (GET_CODE (exp) != CONST_STRING)
2266 int unknown = 0, max;
2267 max = max_attr_value (exp, &unknown);
2268 if (! unknown)
2270 exp = attr_rtx (IF_THEN_ELSE, known_true, exp,
2271 make_numeric_value (max));
2272 exp = simplify_by_exploding (exp);
2275 return exp;
2278 /* Translate the CONST_STRING expressions in X to change the encoding of
2279 value. On input, the value is a bitmask with a one bit for each unit
2280 used; on output, the value is the unit number (zero based) if one
2281 and only one unit is used or the one's compliment of the bitmask. */
2283 static rtx
2284 encode_units_mask (x)
2285 rtx x;
2287 register int i;
2288 register int j;
2289 register enum rtx_code code;
2290 register const char *fmt;
2292 code = GET_CODE (x);
2294 switch (code)
2296 case CONST_STRING:
2297 i = atoi (XSTR (x, 0));
2298 if (i < 0)
2299 abort (); /* The sign bit encodes a one's compliment mask. */
2300 else if (i != 0 && i == (i & -i))
2301 /* Only one bit is set, so yield that unit number. */
2302 for (j = 0; (i >>= 1) != 0; j++)
2304 else
2305 j = ~i;
2306 return attr_rtx (CONST_STRING, attr_printf (MAX_DIGITS, "%d", j));
2308 case REG:
2309 case QUEUED:
2310 case CONST_INT:
2311 case CONST_DOUBLE:
2312 case SYMBOL_REF:
2313 case CODE_LABEL:
2314 case PC:
2315 case CC0:
2316 case EQ_ATTR:
2317 return x;
2319 default:
2320 break;
2323 /* Compare the elements. If any pair of corresponding elements
2324 fail to match, return 0 for the whole things. */
2326 fmt = GET_RTX_FORMAT (code);
2327 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2329 switch (fmt[i])
2331 case 'V':
2332 case 'E':
2333 for (j = 0; j < XVECLEN (x, i); j++)
2334 XVECEXP (x, i, j) = encode_units_mask (XVECEXP (x, i, j));
2335 break;
2337 case 'e':
2338 XEXP (x, i) = encode_units_mask (XEXP (x, i));
2339 break;
2342 return x;
2345 /* Once all attributes and insns have been read and checked, we construct for
2346 each attribute value a list of all the insns that have that value for
2347 the attribute. */
2349 static void
2350 fill_attr (attr)
2351 struct attr_desc *attr;
2353 struct attr_value *av;
2354 struct insn_ent *ie;
2355 struct insn_def *id;
2356 int i;
2357 rtx value;
2359 /* Don't fill constant attributes. The value is independent of
2360 any particular insn. */
2361 if (attr->is_const)
2362 return;
2364 for (id = defs; id; id = id->next)
2366 /* If no value is specified for this insn for this attribute, use the
2367 default. */
2368 value = NULL;
2369 if (XVEC (id->def, id->vec_idx))
2370 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
2371 if (! strcmp (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
2372 attr->name))
2373 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
2375 if (value == NULL)
2376 av = attr->default_val;
2377 else
2378 av = get_attr_value (value, attr, id->insn_code);
2380 ie = (struct insn_ent *) oballoc (sizeof (struct insn_ent));
2381 ie->insn_code = id->insn_code;
2382 ie->insn_index = id->insn_code;
2383 insert_insn_ent (av, ie);
2387 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
2388 test that checks relative positions of insns (uses MATCH_DUP or PC).
2389 If so, replace it with what is obtained by passing the expression to
2390 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
2391 recursively on each value (including the default value). Otherwise,
2392 return the value returned by NO_ADDRESS_FN applied to EXP. */
2394 static rtx
2395 substitute_address (exp, no_address_fn, address_fn)
2396 rtx exp;
2397 rtx (*no_address_fn) PARAMS ((rtx));
2398 rtx (*address_fn) PARAMS ((rtx));
2400 int i;
2401 rtx newexp;
2403 if (GET_CODE (exp) == COND)
2405 /* See if any tests use addresses. */
2406 address_used = 0;
2407 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2408 walk_attr_value (XVECEXP (exp, 0, i));
2410 if (address_used)
2411 return (*address_fn) (exp);
2413 /* Make a new copy of this COND, replacing each element. */
2414 newexp = rtx_alloc (COND);
2415 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
2416 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2418 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
2419 XVECEXP (newexp, 0, i + 1)
2420 = substitute_address (XVECEXP (exp, 0, i + 1),
2421 no_address_fn, address_fn);
2424 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
2425 no_address_fn, address_fn);
2427 return newexp;
2430 else if (GET_CODE (exp) == IF_THEN_ELSE)
2432 address_used = 0;
2433 walk_attr_value (XEXP (exp, 0));
2434 if (address_used)
2435 return (*address_fn) (exp);
2437 return attr_rtx (IF_THEN_ELSE,
2438 substitute_address (XEXP (exp, 0),
2439 no_address_fn, address_fn),
2440 substitute_address (XEXP (exp, 1),
2441 no_address_fn, address_fn),
2442 substitute_address (XEXP (exp, 2),
2443 no_address_fn, address_fn));
2446 return (*no_address_fn) (exp);
2449 /* Make new attributes from the `length' attribute. The following are made,
2450 each corresponding to a function called from `shorten_branches' or
2451 `get_attr_length':
2453 *insn_default_length This is the length of the insn to be returned
2454 by `get_attr_length' before `shorten_branches'
2455 has been called. In each case where the length
2456 depends on relative addresses, the largest
2457 possible is used. This routine is also used
2458 to compute the initial size of the insn.
2460 *insn_variable_length_p This returns 1 if the insn's length depends
2461 on relative addresses, zero otherwise.
2463 *insn_current_length This is only called when it is known that the
2464 insn has a variable length and returns the
2465 current length, based on relative addresses.
2468 static void
2469 make_length_attrs ()
2471 static const char *new_names[] = {"*insn_default_length",
2472 "*insn_variable_length_p",
2473 "*insn_current_length"};
2474 static rtx (*no_address_fn[]) PARAMS ((rtx)) = {identity_fn, zero_fn, zero_fn};
2475 static rtx (*address_fn[]) PARAMS ((rtx)) = {max_fn, one_fn, identity_fn};
2476 size_t i;
2477 struct attr_desc *length_attr, *new_attr;
2478 struct attr_value *av, *new_av;
2479 struct insn_ent *ie, *new_ie;
2481 /* See if length attribute is defined. If so, it must be numeric. Make
2482 it special so we don't output anything for it. */
2483 length_attr = find_attr ("length", 0);
2484 if (length_attr == 0)
2485 return;
2487 if (! length_attr->is_numeric)
2488 fatal ("length attribute must be numeric.");
2490 length_attr->is_const = 0;
2491 length_attr->is_special = 1;
2493 /* Make each new attribute, in turn. */
2494 for (i = 0; i < sizeof new_names / sizeof new_names[0]; i++)
2496 make_internal_attr (new_names[i],
2497 substitute_address (length_attr->default_val->value,
2498 no_address_fn[i], address_fn[i]),
2500 new_attr = find_attr (new_names[i], 0);
2501 for (av = length_attr->first_value; av; av = av->next)
2502 for (ie = av->first_insn; ie; ie = ie->next)
2504 new_av = get_attr_value (substitute_address (av->value,
2505 no_address_fn[i],
2506 address_fn[i]),
2507 new_attr, ie->insn_code);
2508 new_ie = (struct insn_ent *) oballoc (sizeof (struct insn_ent));
2509 new_ie->insn_code = ie->insn_code;
2510 new_ie->insn_index = ie->insn_index;
2511 insert_insn_ent (new_av, new_ie);
2516 /* Utility functions called from above routine. */
2518 static rtx
2519 identity_fn (exp)
2520 rtx exp;
2522 return exp;
2525 static rtx
2526 zero_fn (exp)
2527 rtx exp ATTRIBUTE_UNUSED;
2529 return make_numeric_value (0);
2532 static rtx
2533 one_fn (exp)
2534 rtx exp ATTRIBUTE_UNUSED;
2536 return make_numeric_value (1);
2539 static rtx
2540 max_fn (exp)
2541 rtx exp;
2543 int unknown;
2544 return make_numeric_value (max_attr_value (exp, &unknown));
2547 static void
2548 write_length_unit_log ()
2550 struct attr_desc *length_attr = find_attr ("length", 0);
2551 struct attr_value *av;
2552 struct insn_ent *ie;
2553 unsigned int length_unit_log, length_or;
2554 int unknown = 0;
2556 if (length_attr == 0)
2557 return;
2558 length_or = or_attr_value (length_attr->default_val->value, &unknown);
2559 for (av = length_attr->first_value; av; av = av->next)
2560 for (ie = av->first_insn; ie; ie = ie->next)
2561 length_or |= or_attr_value (av->value, &unknown);
2563 if (unknown)
2564 length_unit_log = 0;
2565 else
2567 length_or = ~length_or;
2568 for (length_unit_log = 0; length_or & 1; length_or >>= 1)
2569 length_unit_log++;
2571 printf ("int length_unit_log = %u;\n", length_unit_log);
2574 /* Take a COND expression and see if any of the conditions in it can be
2575 simplified. If any are known true or known false for the particular insn
2576 code, the COND can be further simplified.
2578 Also call ourselves on any COND operations that are values of this COND.
2580 We do not modify EXP; rather, we make and return a new rtx. */
2582 static rtx
2583 simplify_cond (exp, insn_code, insn_index)
2584 rtx exp;
2585 int insn_code, insn_index;
2587 int i, j;
2588 /* We store the desired contents here,
2589 then build a new expression if they don't match EXP. */
2590 rtx defval = XEXP (exp, 1);
2591 rtx new_defval = XEXP (exp, 1);
2592 int len = XVECLEN (exp, 0);
2593 rtx *tests = (rtx *) alloca (len * sizeof (rtx));
2594 int allsame = 1;
2595 char *first_spacer;
2597 /* This lets us free all storage allocated below, if appropriate. */
2598 first_spacer = (char *) obstack_finish (rtl_obstack);
2600 bcopy ((char *) XVEC (exp, 0)->elem, (char *) tests, len * sizeof (rtx));
2602 /* See if default value needs simplification. */
2603 if (GET_CODE (defval) == COND)
2604 new_defval = simplify_cond (defval, insn_code, insn_index);
2606 /* Simplify the subexpressions, and see what tests we can get rid of. */
2608 for (i = 0; i < len; i += 2)
2610 rtx newtest, newval;
2612 /* Simplify this test. */
2613 newtest = SIMPLIFY_TEST_EXP (tests[i], insn_code, insn_index);
2614 tests[i] = newtest;
2616 newval = tests[i + 1];
2617 /* See if this value may need simplification. */
2618 if (GET_CODE (newval) == COND)
2619 newval = simplify_cond (newval, insn_code, insn_index);
2621 /* Look for ways to delete or combine this test. */
2622 if (newtest == true_rtx)
2624 /* If test is true, make this value the default
2625 and discard this + any following tests. */
2626 len = i;
2627 defval = tests[i + 1];
2628 new_defval = newval;
2631 else if (newtest == false_rtx)
2633 /* If test is false, discard it and its value. */
2634 for (j = i; j < len - 2; j++)
2635 tests[j] = tests[j + 2];
2636 len -= 2;
2639 else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
2641 /* If this value and the value for the prev test are the same,
2642 merge the tests. */
2644 tests[i - 2]
2645 = insert_right_side (IOR, tests[i - 2], newtest,
2646 insn_code, insn_index);
2648 /* Delete this test/value. */
2649 for (j = i; j < len - 2; j++)
2650 tests[j] = tests[j + 2];
2651 len -= 2;
2654 else
2655 tests[i + 1] = newval;
2658 /* If the last test in a COND has the same value
2659 as the default value, that test isn't needed. */
2661 while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
2662 len -= 2;
2664 /* See if we changed anything. */
2665 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
2666 allsame = 0;
2667 else
2668 for (i = 0; i < len; i++)
2669 if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
2671 allsame = 0;
2672 break;
2675 if (len == 0)
2677 if (!ggc_p)
2678 obstack_free (rtl_obstack, first_spacer);
2679 if (GET_CODE (defval) == COND)
2680 return simplify_cond (defval, insn_code, insn_index);
2681 return defval;
2683 else if (allsame)
2685 if (!ggc_p)
2686 obstack_free (rtl_obstack, first_spacer);
2687 return exp;
2689 else
2691 rtx newexp = rtx_alloc (COND);
2693 XVEC (newexp, 0) = rtvec_alloc (len);
2694 bcopy ((char *) tests, (char *) XVEC (newexp, 0)->elem,
2695 len * sizeof (rtx));
2696 XEXP (newexp, 1) = new_defval;
2697 return newexp;
2701 /* Remove an insn entry from an attribute value. */
2703 static void
2704 remove_insn_ent (av, ie)
2705 struct attr_value *av;
2706 struct insn_ent *ie;
2708 struct insn_ent *previe;
2710 if (av->first_insn == ie)
2711 av->first_insn = ie->next;
2712 else
2714 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
2716 previe->next = ie->next;
2719 av->num_insns--;
2720 if (ie->insn_code == -1)
2721 av->has_asm_insn = 0;
2723 num_insn_ents--;
2726 /* Insert an insn entry in an attribute value list. */
2728 static void
2729 insert_insn_ent (av, ie)
2730 struct attr_value *av;
2731 struct insn_ent *ie;
2733 ie->next = av->first_insn;
2734 av->first_insn = ie;
2735 av->num_insns++;
2736 if (ie->insn_code == -1)
2737 av->has_asm_insn = 1;
2739 num_insn_ents++;
2742 /* This is a utility routine to take an expression that is a tree of either
2743 AND or IOR expressions and insert a new term. The new term will be
2744 inserted at the right side of the first node whose code does not match
2745 the root. A new node will be created with the root's code. Its left
2746 side will be the old right side and its right side will be the new
2747 term.
2749 If the `term' is itself a tree, all its leaves will be inserted. */
2751 static rtx
2752 insert_right_side (code, exp, term, insn_code, insn_index)
2753 enum rtx_code code;
2754 rtx exp;
2755 rtx term;
2756 int insn_code, insn_index;
2758 rtx newexp;
2760 /* Avoid consing in some special cases. */
2761 if (code == AND && term == true_rtx)
2762 return exp;
2763 if (code == AND && term == false_rtx)
2764 return false_rtx;
2765 if (code == AND && exp == true_rtx)
2766 return term;
2767 if (code == AND && exp == false_rtx)
2768 return false_rtx;
2769 if (code == IOR && term == true_rtx)
2770 return true_rtx;
2771 if (code == IOR && term == false_rtx)
2772 return exp;
2773 if (code == IOR && exp == true_rtx)
2774 return true_rtx;
2775 if (code == IOR && exp == false_rtx)
2776 return term;
2777 if (attr_equal_p (exp, term))
2778 return exp;
2780 if (GET_CODE (term) == code)
2782 exp = insert_right_side (code, exp, XEXP (term, 0),
2783 insn_code, insn_index);
2784 exp = insert_right_side (code, exp, XEXP (term, 1),
2785 insn_code, insn_index);
2787 return exp;
2790 if (GET_CODE (exp) == code)
2792 rtx new = insert_right_side (code, XEXP (exp, 1),
2793 term, insn_code, insn_index);
2794 if (new != XEXP (exp, 1))
2795 /* Make a copy of this expression and call recursively. */
2796 newexp = attr_rtx (code, XEXP (exp, 0), new);
2797 else
2798 newexp = exp;
2800 else
2802 /* Insert the new term. */
2803 newexp = attr_rtx (code, exp, term);
2806 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2809 /* If we have an expression which AND's a bunch of
2810 (not (eq_attrq "alternative" "n"))
2811 terms, we may have covered all or all but one of the possible alternatives.
2812 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
2814 This routine is passed an expression and either AND or IOR. It returns a
2815 bitmask indicating which alternatives are mentioned within EXP. */
2817 static int
2818 compute_alternative_mask (exp, code)
2819 rtx exp;
2820 enum rtx_code code;
2822 const char *string;
2823 if (GET_CODE (exp) == code)
2824 return compute_alternative_mask (XEXP (exp, 0), code)
2825 | compute_alternative_mask (XEXP (exp, 1), code);
2827 else if (code == AND && GET_CODE (exp) == NOT
2828 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2829 && XSTR (XEXP (exp, 0), 0) == alternative_name)
2830 string = XSTR (XEXP (exp, 0), 1);
2832 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
2833 && XSTR (exp, 0) == alternative_name)
2834 string = XSTR (exp, 1);
2836 else
2837 return 0;
2839 if (string[1] == 0)
2840 return 1 << (string[0] - '0');
2841 return 1 << atoi (string);
2844 /* Given I, a single-bit mask, return RTX to compare the `alternative'
2845 attribute with the value represented by that bit. */
2847 static rtx
2848 make_alternative_compare (mask)
2849 int mask;
2851 rtx newexp;
2852 int i;
2854 /* Find the bit. */
2855 for (i = 0; (mask & (1 << i)) == 0; i++)
2858 newexp = attr_rtx (EQ_ATTR, alternative_name, attr_numeral (i));
2859 RTX_UNCHANGING_P (newexp) = 1;
2861 return newexp;
2864 /* If we are processing an (eq_attr "attr" "value") test, we find the value
2865 of "attr" for this insn code. From that value, we can compute a test
2866 showing when the EQ_ATTR will be true. This routine performs that
2867 computation. If a test condition involves an address, we leave the EQ_ATTR
2868 intact because addresses are only valid for the `length' attribute.
2870 EXP is the EQ_ATTR expression and VALUE is the value of that attribute
2871 for the insn corresponding to INSN_CODE and INSN_INDEX. */
2873 static rtx
2874 evaluate_eq_attr (exp, value, insn_code, insn_index)
2875 rtx exp;
2876 rtx value;
2877 int insn_code, insn_index;
2879 rtx orexp, andexp;
2880 rtx right;
2881 rtx newexp;
2882 int i;
2884 if (GET_CODE (value) == CONST_STRING)
2886 if (! strcmp (XSTR (value, 0), XSTR (exp, 1)))
2887 newexp = true_rtx;
2888 else
2889 newexp = false_rtx;
2891 else if (GET_CODE (value) == SYMBOL_REF)
2893 char *p, *string;
2895 if (GET_CODE (exp) != EQ_ATTR)
2896 abort();
2898 string = (char *) alloca (2 + strlen (XSTR (exp, 0))
2899 + strlen (XSTR (exp, 1)));
2900 strcpy (string, XSTR (exp, 0));
2901 strcat (string, "_");
2902 strcat (string, XSTR (exp, 1));
2903 for (p = string; *p ; p++)
2904 *p = TOUPPER (*p);
2906 newexp = attr_rtx (EQ, value,
2907 attr_rtx (SYMBOL_REF,
2908 attr_string(string, strlen(string))));
2910 else if (GET_CODE (value) == COND)
2912 /* We construct an IOR of all the cases for which the requested attribute
2913 value is present. Since we start with FALSE, if it is not present,
2914 FALSE will be returned.
2916 Each case is the AND of the NOT's of the previous conditions with the
2917 current condition; in the default case the current condition is TRUE.
2919 For each possible COND value, call ourselves recursively.
2921 The extra TRUE and FALSE expressions will be eliminated by another
2922 call to the simplification routine. */
2924 orexp = false_rtx;
2925 andexp = true_rtx;
2927 if (current_alternative_string)
2928 clear_struct_flag (value);
2930 for (i = 0; i < XVECLEN (value, 0); i += 2)
2932 rtx this = SIMPLIFY_TEST_EXP (XVECEXP (value, 0, i),
2933 insn_code, insn_index);
2935 SIMPLIFY_ALTERNATIVE (this);
2937 right = insert_right_side (AND, andexp, this,
2938 insn_code, insn_index);
2939 right = insert_right_side (AND, right,
2940 evaluate_eq_attr (exp,
2941 XVECEXP (value, 0,
2942 i + 1),
2943 insn_code, insn_index),
2944 insn_code, insn_index);
2945 orexp = insert_right_side (IOR, orexp, right,
2946 insn_code, insn_index);
2948 /* Add this condition into the AND expression. */
2949 newexp = attr_rtx (NOT, this);
2950 andexp = insert_right_side (AND, andexp, newexp,
2951 insn_code, insn_index);
2954 /* Handle the default case. */
2955 right = insert_right_side (AND, andexp,
2956 evaluate_eq_attr (exp, XEXP (value, 1),
2957 insn_code, insn_index),
2958 insn_code, insn_index);
2959 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2961 else
2962 abort ();
2964 /* If uses an address, must return original expression. But set the
2965 RTX_UNCHANGING_P bit so we don't try to simplify it again. */
2967 address_used = 0;
2968 walk_attr_value (newexp);
2970 if (address_used)
2972 /* This had `&& current_alternative_string', which seems to be wrong. */
2973 if (! RTX_UNCHANGING_P (exp))
2974 return copy_rtx_unchanging (exp);
2975 return exp;
2977 else
2978 return newexp;
2981 /* This routine is called when an AND of a term with a tree of AND's is
2982 encountered. If the term or its complement is present in the tree, it
2983 can be replaced with TRUE or FALSE, respectively.
2985 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2986 be true and hence are complementary.
2988 There is one special case: If we see
2989 (and (not (eq_attr "att" "v1"))
2990 (eq_attr "att" "v2"))
2991 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2992 replace the term, not anything in the AND tree. So we pass a pointer to
2993 the term. */
2995 static rtx
2996 simplify_and_tree (exp, pterm, insn_code, insn_index)
2997 rtx exp;
2998 rtx *pterm;
2999 int insn_code, insn_index;
3001 rtx left, right;
3002 rtx newexp;
3003 rtx temp;
3004 int left_eliminates_term, right_eliminates_term;
3006 if (GET_CODE (exp) == AND)
3008 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
3009 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
3010 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3012 newexp = attr_rtx (GET_CODE (exp), left, right);
3014 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3018 else if (GET_CODE (exp) == IOR)
3020 /* For the IOR case, we do the same as above, except that we can
3021 only eliminate `term' if both sides of the IOR would do so. */
3022 temp = *pterm;
3023 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
3024 left_eliminates_term = (temp == true_rtx);
3026 temp = *pterm;
3027 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
3028 right_eliminates_term = (temp == true_rtx);
3030 if (left_eliminates_term && right_eliminates_term)
3031 *pterm = true_rtx;
3033 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3035 newexp = attr_rtx (GET_CODE (exp), left, right);
3037 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3041 /* Check for simplifications. Do some extra checking here since this
3042 routine is called so many times. */
3044 if (exp == *pterm)
3045 return true_rtx;
3047 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
3048 return false_rtx;
3050 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
3051 return false_rtx;
3053 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
3055 if (XSTR (exp, 0) != XSTR (*pterm, 0))
3056 return exp;
3058 if (! strcmp (XSTR (exp, 1), XSTR (*pterm, 1)))
3059 return true_rtx;
3060 else
3061 return false_rtx;
3064 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
3065 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
3067 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
3068 return exp;
3070 if (! strcmp (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
3071 return false_rtx;
3072 else
3073 return true_rtx;
3076 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
3077 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
3079 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
3080 return exp;
3082 if (! strcmp (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
3083 return false_rtx;
3084 else
3085 *pterm = true_rtx;
3088 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
3090 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
3091 return true_rtx;
3094 else if (GET_CODE (exp) == NOT)
3096 if (attr_equal_p (XEXP (exp, 0), *pterm))
3097 return false_rtx;
3100 else if (GET_CODE (*pterm) == NOT)
3102 if (attr_equal_p (XEXP (*pterm, 0), exp))
3103 return false_rtx;
3106 else if (attr_equal_p (exp, *pterm))
3107 return true_rtx;
3109 return exp;
3112 /* Similar to `simplify_and_tree', but for IOR trees. */
3114 static rtx
3115 simplify_or_tree (exp, pterm, insn_code, insn_index)
3116 rtx exp;
3117 rtx *pterm;
3118 int insn_code, insn_index;
3120 rtx left, right;
3121 rtx newexp;
3122 rtx temp;
3123 int left_eliminates_term, right_eliminates_term;
3125 if (GET_CODE (exp) == IOR)
3127 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
3128 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
3129 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3131 newexp = attr_rtx (GET_CODE (exp), left, right);
3133 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3137 else if (GET_CODE (exp) == AND)
3139 /* For the AND case, we do the same as above, except that we can
3140 only eliminate `term' if both sides of the AND would do so. */
3141 temp = *pterm;
3142 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
3143 left_eliminates_term = (temp == false_rtx);
3145 temp = *pterm;
3146 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
3147 right_eliminates_term = (temp == false_rtx);
3149 if (left_eliminates_term && right_eliminates_term)
3150 *pterm = false_rtx;
3152 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3154 newexp = attr_rtx (GET_CODE (exp), left, right);
3156 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3160 if (attr_equal_p (exp, *pterm))
3161 return false_rtx;
3163 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
3164 return true_rtx;
3166 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
3167 return true_rtx;
3169 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
3170 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3171 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
3172 *pterm = false_rtx;
3174 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
3175 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
3176 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
3177 return false_rtx;
3179 return exp;
3182 /* Given an expression, see if it can be simplified for a particular insn
3183 code based on the values of other attributes being tested. This can
3184 eliminate nested get_attr_... calls.
3186 Note that if an endless recursion is specified in the patterns, the
3187 optimization will loop. However, it will do so in precisely the cases where
3188 an infinite recursion loop could occur during compilation. It's better that
3189 it occurs here! */
3191 static rtx
3192 simplify_test_exp (exp, insn_code, insn_index)
3193 rtx exp;
3194 int insn_code, insn_index;
3196 rtx left, right;
3197 struct attr_desc *attr;
3198 struct attr_value *av;
3199 struct insn_ent *ie;
3200 int i;
3201 rtx newexp = exp;
3202 char *spacer = (char *) obstack_finish (rtl_obstack);
3204 /* Don't re-simplify something we already simplified. */
3205 if (RTX_UNCHANGING_P (exp) || MEM_IN_STRUCT_P (exp))
3206 return exp;
3208 switch (GET_CODE (exp))
3210 case AND:
3211 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3212 SIMPLIFY_ALTERNATIVE (left);
3213 if (left == false_rtx)
3215 if (!ggc_p)
3216 obstack_free (rtl_obstack, spacer);
3217 return false_rtx;
3219 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3220 SIMPLIFY_ALTERNATIVE (right);
3221 if (left == false_rtx)
3223 if (!ggc_p)
3224 obstack_free (rtl_obstack, spacer);
3225 return false_rtx;
3228 /* If either side is an IOR and we have (eq_attr "alternative" ..")
3229 present on both sides, apply the distributive law since this will
3230 yield simplifications. */
3231 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
3232 && compute_alternative_mask (left, IOR)
3233 && compute_alternative_mask (right, IOR))
3235 if (GET_CODE (left) == IOR)
3237 rtx tem = left;
3238 left = right;
3239 right = tem;
3242 newexp = attr_rtx (IOR,
3243 attr_rtx (AND, left, XEXP (right, 0)),
3244 attr_rtx (AND, left, XEXP (right, 1)));
3246 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3249 /* Try with the term on both sides. */
3250 right = simplify_and_tree (right, &left, insn_code, insn_index);
3251 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3252 left = simplify_and_tree (left, &right, insn_code, insn_index);
3254 if (left == false_rtx || right == false_rtx)
3256 if (!ggc_p)
3257 obstack_free (rtl_obstack, spacer);
3258 return false_rtx;
3260 else if (left == true_rtx)
3262 return right;
3264 else if (right == true_rtx)
3266 return left;
3268 /* See if all or all but one of the insn's alternatives are specified
3269 in this tree. Optimize if so. */
3271 else if (insn_code >= 0
3272 && (GET_CODE (left) == AND
3273 || (GET_CODE (left) == NOT
3274 && GET_CODE (XEXP (left, 0)) == EQ_ATTR
3275 && XSTR (XEXP (left, 0), 0) == alternative_name)
3276 || GET_CODE (right) == AND
3277 || (GET_CODE (right) == NOT
3278 && GET_CODE (XEXP (right, 0)) == EQ_ATTR
3279 && XSTR (XEXP (right, 0), 0) == alternative_name)))
3281 i = compute_alternative_mask (exp, AND);
3282 if (i & ~insn_alternatives[insn_code])
3283 fatal ("Invalid alternative specified for pattern number %d",
3284 insn_index);
3286 /* If all alternatives are excluded, this is false. */
3287 i ^= insn_alternatives[insn_code];
3288 if (i == 0)
3289 return false_rtx;
3290 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3292 /* If just one excluded, AND a comparison with that one to the
3293 front of the tree. The others will be eliminated by
3294 optimization. We do not want to do this if the insn has one
3295 alternative and we have tested none of them! */
3296 left = make_alternative_compare (i);
3297 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3298 newexp = attr_rtx (AND, left, right);
3300 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3304 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3306 newexp = attr_rtx (AND, left, right);
3307 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3309 break;
3311 case IOR:
3312 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3313 SIMPLIFY_ALTERNATIVE (left);
3314 if (left == true_rtx)
3316 if (!ggc_p)
3317 obstack_free (rtl_obstack, spacer);
3318 return true_rtx;
3320 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3321 SIMPLIFY_ALTERNATIVE (right);
3322 if (right == true_rtx)
3324 if (!ggc_p)
3325 obstack_free (rtl_obstack, spacer);
3326 return true_rtx;
3329 right = simplify_or_tree (right, &left, insn_code, insn_index);
3330 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3331 left = simplify_or_tree (left, &right, insn_code, insn_index);
3333 if (right == true_rtx || left == true_rtx)
3335 if (!ggc_p)
3336 obstack_free (rtl_obstack, spacer);
3337 return true_rtx;
3339 else if (left == false_rtx)
3341 return right;
3343 else if (right == false_rtx)
3345 return left;
3348 /* Test for simple cases where the distributive law is useful. I.e.,
3349 convert (ior (and (x) (y))
3350 (and (x) (z)))
3351 to (and (x)
3352 (ior (y) (z)))
3355 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
3356 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
3358 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
3360 left = XEXP (left, 0);
3361 right = newexp;
3362 newexp = attr_rtx (AND, left, right);
3363 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3366 /* See if all or all but one of the insn's alternatives are specified
3367 in this tree. Optimize if so. */
3369 else if (insn_code >= 0
3370 && (GET_CODE (left) == IOR
3371 || (GET_CODE (left) == EQ_ATTR
3372 && XSTR (left, 0) == alternative_name)
3373 || GET_CODE (right) == IOR
3374 || (GET_CODE (right) == EQ_ATTR
3375 && XSTR (right, 0) == alternative_name)))
3377 i = compute_alternative_mask (exp, IOR);
3378 if (i & ~insn_alternatives[insn_code])
3379 fatal ("Invalid alternative specified for pattern number %d",
3380 insn_index);
3382 /* If all alternatives are included, this is true. */
3383 i ^= insn_alternatives[insn_code];
3384 if (i == 0)
3385 return true_rtx;
3386 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3388 /* If just one excluded, IOR a comparison with that one to the
3389 front of the tree. The others will be eliminated by
3390 optimization. We do not want to do this if the insn has one
3391 alternative and we have tested none of them! */
3392 left = make_alternative_compare (i);
3393 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3394 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
3396 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3400 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3402 newexp = attr_rtx (IOR, left, right);
3403 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3405 break;
3407 case NOT:
3408 if (GET_CODE (XEXP (exp, 0)) == NOT)
3410 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
3411 insn_code, insn_index);
3412 SIMPLIFY_ALTERNATIVE (left);
3413 return left;
3416 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3417 SIMPLIFY_ALTERNATIVE (left);
3418 if (GET_CODE (left) == NOT)
3419 return XEXP (left, 0);
3421 if (left == false_rtx)
3423 if (!ggc_p)
3424 obstack_free (rtl_obstack, spacer);
3425 return true_rtx;
3427 else if (left == true_rtx)
3429 if (!ggc_p)
3430 obstack_free (rtl_obstack, spacer);
3431 return false_rtx;
3434 /* Try to apply De`Morgan's laws. */
3435 else if (GET_CODE (left) == IOR)
3437 newexp = attr_rtx (AND,
3438 attr_rtx (NOT, XEXP (left, 0)),
3439 attr_rtx (NOT, XEXP (left, 1)));
3441 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3443 else if (GET_CODE (left) == AND)
3445 newexp = attr_rtx (IOR,
3446 attr_rtx (NOT, XEXP (left, 0)),
3447 attr_rtx (NOT, XEXP (left, 1)));
3449 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3451 else if (left != XEXP (exp, 0))
3453 newexp = attr_rtx (NOT, left);
3455 break;
3457 case EQ_ATTR:
3458 if (current_alternative_string && XSTR (exp, 0) == alternative_name)
3459 return (XSTR (exp, 1) == current_alternative_string
3460 ? true_rtx : false_rtx);
3462 /* Look at the value for this insn code in the specified attribute.
3463 We normally can replace this comparison with the condition that
3464 would give this insn the values being tested for. */
3465 if (XSTR (exp, 0) != alternative_name
3466 && (attr = find_attr (XSTR (exp, 0), 0)) != NULL)
3467 for (av = attr->first_value; av; av = av->next)
3468 for (ie = av->first_insn; ie; ie = ie->next)
3469 if (ie->insn_code == insn_code)
3470 return evaluate_eq_attr (exp, av->value, insn_code, insn_index);
3471 break;
3473 default:
3474 break;
3477 /* We have already simplified this expression. Simplifying it again
3478 won't buy anything unless we weren't given a valid insn code
3479 to process (i.e., we are canonicalizing something.). */
3480 if (insn_code != -2 /* Seems wrong: && current_alternative_string. */
3481 && ! RTX_UNCHANGING_P (newexp))
3482 return copy_rtx_unchanging (newexp);
3484 return newexp;
3487 /* Optimize the attribute lists by seeing if we can determine conditional
3488 values from the known values of other attributes. This will save subroutine
3489 calls during the compilation. */
3491 static void
3492 optimize_attrs ()
3494 struct attr_desc *attr;
3495 struct attr_value *av;
3496 struct insn_ent *ie;
3497 rtx newexp;
3498 int something_changed = 1;
3499 int i;
3500 struct attr_value_list { struct attr_value *av;
3501 struct insn_ent *ie;
3502 struct attr_desc * attr;
3503 struct attr_value_list *next; };
3504 struct attr_value_list **insn_code_values;
3505 struct attr_value_list *ivbuf;
3506 struct attr_value_list *iv;
3508 /* For each insn code, make a list of all the insn_ent's for it,
3509 for all values for all attributes. */
3511 if (num_insn_ents == 0)
3512 return;
3514 /* Make 2 extra elements, for "code" values -2 and -1. */
3515 insn_code_values
3516 = (struct attr_value_list **) alloca ((insn_code_number + 2)
3517 * sizeof (struct attr_value_list *));
3518 bzero ((char *) insn_code_values,
3519 (insn_code_number + 2) * sizeof (struct attr_value_list *));
3521 /* Offset the table address so we can index by -2 or -1. */
3522 insn_code_values += 2;
3524 /* Allocate the attr_value_list structures using xmalloc rather than
3525 alloca, because using alloca can overflow the maximum permitted
3526 stack limit on SPARC Lynx. */
3527 iv = ivbuf = ((struct attr_value_list *)
3528 xmalloc (num_insn_ents * sizeof (struct attr_value_list)));
3530 for (i = 0; i < MAX_ATTRS_INDEX; i++)
3531 for (attr = attrs[i]; attr; attr = attr->next)
3532 for (av = attr->first_value; av; av = av->next)
3533 for (ie = av->first_insn; ie; ie = ie->next)
3535 iv->attr = attr;
3536 iv->av = av;
3537 iv->ie = ie;
3538 iv->next = insn_code_values[ie->insn_code];
3539 insn_code_values[ie->insn_code] = iv;
3540 iv++;
3543 /* Sanity check on num_insn_ents. */
3544 if (iv != ivbuf + num_insn_ents)
3545 abort ();
3547 /* Process one insn code at a time. */
3548 for (i = -2; i < insn_code_number; i++)
3550 /* Clear the MEM_IN_STRUCT_P flag everywhere relevant.
3551 We use it to mean "already simplified for this insn". */
3552 for (iv = insn_code_values[i]; iv; iv = iv->next)
3553 clear_struct_flag (iv->av->value);
3555 /* Loop until nothing changes for one iteration. */
3556 something_changed = 1;
3557 while (something_changed)
3559 something_changed = 0;
3560 for (iv = insn_code_values[i]; iv; iv = iv->next)
3562 struct obstack *old = rtl_obstack;
3563 char *spacer = (char *) obstack_finish (temp_obstack);
3565 attr = iv->attr;
3566 av = iv->av;
3567 ie = iv->ie;
3568 if (GET_CODE (av->value) != COND)
3569 continue;
3571 rtl_obstack = temp_obstack;
3572 #if 0 /* This was intended as a speed up, but it was slower. */
3573 if (insn_n_alternatives[ie->insn_code] > 6
3574 && count_sub_rtxs (av->value, 200) >= 200)
3575 newexp = simplify_by_alternatives (av->value, ie->insn_code,
3576 ie->insn_index);
3577 else
3578 #endif
3579 newexp = simplify_cond (av->value, ie->insn_code,
3580 ie->insn_index);
3582 rtl_obstack = old;
3583 if (newexp != av->value)
3585 newexp = attr_copy_rtx (newexp);
3586 remove_insn_ent (av, ie);
3587 av = get_attr_value (newexp, attr, ie->insn_code);
3588 iv->av = av;
3589 insert_insn_ent (av, ie);
3590 something_changed = 1;
3592 if (!ggc_p)
3593 obstack_free (temp_obstack, spacer);
3598 free (ivbuf);
3601 #if 0
3602 static rtx
3603 simplify_by_alternatives (exp, insn_code, insn_index)
3604 rtx exp;
3605 int insn_code, insn_index;
3607 int i;
3608 int len = insn_n_alternatives[insn_code];
3609 rtx newexp = rtx_alloc (COND);
3610 rtx ultimate;
3613 XVEC (newexp, 0) = rtvec_alloc (len * 2);
3615 /* It will not matter what value we use as the default value
3616 of the new COND, since that default will never be used.
3617 Choose something of the right type. */
3618 for (ultimate = exp; GET_CODE (ultimate) == COND;)
3619 ultimate = XEXP (ultimate, 1);
3620 XEXP (newexp, 1) = ultimate;
3622 for (i = 0; i < insn_n_alternatives[insn_code]; i++)
3624 current_alternative_string = attr_numeral (i);
3625 XVECEXP (newexp, 0, i * 2) = make_alternative_compare (1 << i);
3626 XVECEXP (newexp, 0, i * 2 + 1)
3627 = simplify_cond (exp, insn_code, insn_index);
3630 current_alternative_string = 0;
3631 return simplify_cond (newexp, insn_code, insn_index);
3633 #endif
3635 /* If EXP is a suitable expression, reorganize it by constructing an
3636 equivalent expression that is a COND with the tests being all combinations
3637 of attribute values and the values being simple constants. */
3639 static rtx
3640 simplify_by_exploding (exp)
3641 rtx exp;
3643 rtx list = 0, link, condexp, defval = NULL_RTX;
3644 struct dimension *space;
3645 rtx *condtest, *condval;
3646 int i, j, total, ndim = 0;
3647 int most_tests, num_marks, new_marks;
3649 /* Locate all the EQ_ATTR expressions. */
3650 if (! find_and_mark_used_attributes (exp, &list, &ndim) || ndim == 0)
3652 unmark_used_attributes (list, 0, 0);
3653 return exp;
3656 /* Create an attribute space from the list of used attributes. For each
3657 dimension in the attribute space, record the attribute, list of values
3658 used, and number of values used. Add members to the list of values to
3659 cover the domain of the attribute. This makes the expanded COND form
3660 order independent. */
3662 space = (struct dimension *) alloca (ndim * sizeof (struct dimension));
3664 total = 1;
3665 for (ndim = 0; list; ndim++)
3667 /* Pull the first attribute value from the list and record that
3668 attribute as another dimension in the attribute space. */
3669 const char *name = XSTR (XEXP (list, 0), 0);
3670 rtx *prev;
3672 if ((space[ndim].attr = find_attr (name, 0)) == 0
3673 || space[ndim].attr->is_numeric)
3675 unmark_used_attributes (list, space, ndim);
3676 return exp;
3679 /* Add all remaining attribute values that refer to this attribute. */
3680 space[ndim].num_values = 0;
3681 space[ndim].values = 0;
3682 prev = &list;
3683 for (link = list; link; link = *prev)
3684 if (! strcmp (XSTR (XEXP (link, 0), 0), name))
3686 space[ndim].num_values++;
3687 *prev = XEXP (link, 1);
3688 XEXP (link, 1) = space[ndim].values;
3689 space[ndim].values = link;
3691 else
3692 prev = &XEXP (link, 1);
3694 /* Add sufficient members to the list of values to make the list
3695 mutually exclusive and record the total size of the attribute
3696 space. */
3697 total *= add_values_to_cover (&space[ndim]);
3700 /* Sort the attribute space so that the attributes go from non-constant
3701 to constant and from most values to least values. */
3702 for (i = 0; i < ndim; i++)
3703 for (j = ndim - 1; j > i; j--)
3704 if ((space[j-1].attr->is_const && !space[j].attr->is_const)
3705 || space[j-1].num_values < space[j].num_values)
3707 struct dimension tmp;
3708 tmp = space[j];
3709 space[j] = space[j-1];
3710 space[j-1] = tmp;
3713 /* Establish the initial current value. */
3714 for (i = 0; i < ndim; i++)
3715 space[i].current_value = space[i].values;
3717 condtest = (rtx *) alloca (total * sizeof (rtx));
3718 condval = (rtx *) alloca (total * sizeof (rtx));
3720 /* Expand the tests and values by iterating over all values in the
3721 attribute space. */
3722 for (i = 0;; i++)
3724 condtest[i] = test_for_current_value (space, ndim);
3725 condval[i] = simplify_with_current_value (exp, space, ndim);
3726 if (! increment_current_value (space, ndim))
3727 break;
3729 if (i != total - 1)
3730 abort ();
3732 /* We are now finished with the original expression. */
3733 unmark_used_attributes (0, space, ndim);
3735 /* Find the most used constant value and make that the default. */
3736 most_tests = -1;
3737 for (i = num_marks = 0; i < total; i++)
3738 if (GET_CODE (condval[i]) == CONST_STRING
3739 && ! MEM_VOLATILE_P (condval[i]))
3741 /* Mark the unmarked constant value and count how many are marked. */
3742 MEM_VOLATILE_P (condval[i]) = 1;
3743 for (j = new_marks = 0; j < total; j++)
3744 if (GET_CODE (condval[j]) == CONST_STRING
3745 && MEM_VOLATILE_P (condval[j]))
3746 new_marks++;
3747 if (new_marks - num_marks > most_tests)
3749 most_tests = new_marks - num_marks;
3750 defval = condval[i];
3752 num_marks = new_marks;
3754 /* Clear all the marks. */
3755 for (i = 0; i < total; i++)
3756 MEM_VOLATILE_P (condval[i]) = 0;
3758 /* Give up if nothing is constant. */
3759 if (num_marks == 0)
3760 return exp;
3762 /* If all values are the default, use that. */
3763 if (total == most_tests)
3764 return defval;
3766 /* Make a COND with the most common constant value the default. (A more
3767 complex method where tests with the same value were combined didn't
3768 seem to improve things.) */
3769 condexp = rtx_alloc (COND);
3770 XVEC (condexp, 0) = rtvec_alloc ((total - most_tests) * 2);
3771 XEXP (condexp, 1) = defval;
3772 for (i = j = 0; i < total; i++)
3773 if (condval[i] != defval)
3775 XVECEXP (condexp, 0, 2 * j) = condtest[i];
3776 XVECEXP (condexp, 0, 2 * j + 1) = condval[i];
3777 j++;
3780 return condexp;
3783 /* Set the MEM_VOLATILE_P flag for all EQ_ATTR expressions in EXP and
3784 verify that EXP can be simplified to a constant term if all the EQ_ATTR
3785 tests have known value. */
3787 static int
3788 find_and_mark_used_attributes (exp, terms, nterms)
3789 rtx exp, *terms;
3790 int *nterms;
3792 int i;
3794 switch (GET_CODE (exp))
3796 case EQ_ATTR:
3797 if (! MEM_VOLATILE_P (exp))
3799 rtx link = rtx_alloc (EXPR_LIST);
3800 XEXP (link, 0) = exp;
3801 XEXP (link, 1) = *terms;
3802 *terms = link;
3803 *nterms += 1;
3804 MEM_VOLATILE_P (exp) = 1;
3806 return 1;
3808 case CONST_STRING:
3809 case CONST_INT:
3810 return 1;
3812 case IF_THEN_ELSE:
3813 if (! find_and_mark_used_attributes (XEXP (exp, 2), terms, nterms))
3814 return 0;
3815 case IOR:
3816 case AND:
3817 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3818 return 0;
3819 case NOT:
3820 if (! find_and_mark_used_attributes (XEXP (exp, 0), terms, nterms))
3821 return 0;
3822 return 1;
3824 case COND:
3825 for (i = 0; i < XVECLEN (exp, 0); i++)
3826 if (! find_and_mark_used_attributes (XVECEXP (exp, 0, i), terms, nterms))
3827 return 0;
3828 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3829 return 0;
3830 return 1;
3832 default:
3833 return 0;
3837 /* Clear the MEM_VOLATILE_P flag in all EQ_ATTR expressions on LIST and
3838 in the values of the NDIM-dimensional attribute space SPACE. */
3840 static void
3841 unmark_used_attributes (list, space, ndim)
3842 rtx list;
3843 struct dimension *space;
3844 int ndim;
3846 rtx link, exp;
3847 int i;
3849 for (i = 0; i < ndim; i++)
3850 unmark_used_attributes (space[i].values, 0, 0);
3852 for (link = list; link; link = XEXP (link, 1))
3854 exp = XEXP (link, 0);
3855 if (GET_CODE (exp) == EQ_ATTR)
3856 MEM_VOLATILE_P (exp) = 0;
3860 /* Update the attribute dimension DIM so that all values of the attribute
3861 are tested. Return the updated number of values. */
3863 static int
3864 add_values_to_cover (dim)
3865 struct dimension *dim;
3867 struct attr_value *av;
3868 rtx exp, link, *prev;
3869 int nalt = 0;
3871 for (av = dim->attr->first_value; av; av = av->next)
3872 if (GET_CODE (av->value) == CONST_STRING)
3873 nalt++;
3875 if (nalt < dim->num_values)
3876 abort ();
3877 else if (nalt == dim->num_values)
3878 ; /* Ok. */
3879 else if (nalt * 2 < dim->num_values * 3)
3881 /* Most all the values of the attribute are used, so add all the unused
3882 values. */
3883 prev = &dim->values;
3884 for (link = dim->values; link; link = *prev)
3885 prev = &XEXP (link, 1);
3887 for (av = dim->attr->first_value; av; av = av->next)
3888 if (GET_CODE (av->value) == CONST_STRING)
3890 exp = attr_eq (dim->attr->name, XSTR (av->value, 0));
3891 if (MEM_VOLATILE_P (exp))
3892 continue;
3894 link = rtx_alloc (EXPR_LIST);
3895 XEXP (link, 0) = exp;
3896 XEXP (link, 1) = 0;
3897 *prev = link;
3898 prev = &XEXP (link, 1);
3900 dim->num_values = nalt;
3902 else
3904 rtx orexp = false_rtx;
3906 /* Very few values are used, so compute a mutually exclusive
3907 expression. (We could do this for numeric values if that becomes
3908 important.) */
3909 prev = &dim->values;
3910 for (link = dim->values; link; link = *prev)
3912 orexp = insert_right_side (IOR, orexp, XEXP (link, 0), -2, -2);
3913 prev = &XEXP (link, 1);
3915 link = rtx_alloc (EXPR_LIST);
3916 XEXP (link, 0) = attr_rtx (NOT, orexp);
3917 XEXP (link, 1) = 0;
3918 *prev = link;
3919 dim->num_values++;
3921 return dim->num_values;
3924 /* Increment the current value for the NDIM-dimensional attribute space SPACE
3925 and return FALSE if the increment overflowed. */
3927 static int
3928 increment_current_value (space, ndim)
3929 struct dimension *space;
3930 int ndim;
3932 int i;
3934 for (i = ndim - 1; i >= 0; i--)
3936 if ((space[i].current_value = XEXP (space[i].current_value, 1)) == 0)
3937 space[i].current_value = space[i].values;
3938 else
3939 return 1;
3941 return 0;
3944 /* Construct an expression corresponding to the current value for the
3945 NDIM-dimensional attribute space SPACE. */
3947 static rtx
3948 test_for_current_value (space, ndim)
3949 struct dimension *space;
3950 int ndim;
3952 int i;
3953 rtx exp = true_rtx;
3955 for (i = 0; i < ndim; i++)
3956 exp = insert_right_side (AND, exp, XEXP (space[i].current_value, 0),
3957 -2, -2);
3959 return exp;
3962 /* Given the current value of the NDIM-dimensional attribute space SPACE,
3963 set the corresponding EQ_ATTR expressions to that value and reduce
3964 the expression EXP as much as possible. On input [and output], all
3965 known EQ_ATTR expressions are set to FALSE. */
3967 static rtx
3968 simplify_with_current_value (exp, space, ndim)
3969 rtx exp;
3970 struct dimension *space;
3971 int ndim;
3973 int i;
3974 rtx x;
3976 /* Mark each current value as TRUE. */
3977 for (i = 0; i < ndim; i++)
3979 x = XEXP (space[i].current_value, 0);
3980 if (GET_CODE (x) == EQ_ATTR)
3981 MEM_VOLATILE_P (x) = 0;
3984 exp = simplify_with_current_value_aux (exp);
3986 /* Change each current value back to FALSE. */
3987 for (i = 0; i < ndim; i++)
3989 x = XEXP (space[i].current_value, 0);
3990 if (GET_CODE (x) == EQ_ATTR)
3991 MEM_VOLATILE_P (x) = 1;
3994 return exp;
3997 /* Reduce the expression EXP based on the MEM_VOLATILE_P settings of
3998 all EQ_ATTR expressions. */
4000 static rtx
4001 simplify_with_current_value_aux (exp)
4002 rtx exp;
4004 register int i;
4005 rtx cond;
4007 switch (GET_CODE (exp))
4009 case EQ_ATTR:
4010 if (MEM_VOLATILE_P (exp))
4011 return false_rtx;
4012 else
4013 return true_rtx;
4014 case CONST_STRING:
4015 case CONST_INT:
4016 return exp;
4018 case IF_THEN_ELSE:
4019 cond = simplify_with_current_value_aux (XEXP (exp, 0));
4020 if (cond == true_rtx)
4021 return simplify_with_current_value_aux (XEXP (exp, 1));
4022 else if (cond == false_rtx)
4023 return simplify_with_current_value_aux (XEXP (exp, 2));
4024 else
4025 return attr_rtx (IF_THEN_ELSE, cond,
4026 simplify_with_current_value_aux (XEXP (exp, 1)),
4027 simplify_with_current_value_aux (XEXP (exp, 2)));
4029 case IOR:
4030 cond = simplify_with_current_value_aux (XEXP (exp, 1));
4031 if (cond == true_rtx)
4032 return cond;
4033 else if (cond == false_rtx)
4034 return simplify_with_current_value_aux (XEXP (exp, 0));
4035 else
4036 return attr_rtx (IOR, cond,
4037 simplify_with_current_value_aux (XEXP (exp, 0)));
4039 case AND:
4040 cond = simplify_with_current_value_aux (XEXP (exp, 1));
4041 if (cond == true_rtx)
4042 return simplify_with_current_value_aux (XEXP (exp, 0));
4043 else if (cond == false_rtx)
4044 return cond;
4045 else
4046 return attr_rtx (AND, cond,
4047 simplify_with_current_value_aux (XEXP (exp, 0)));
4049 case NOT:
4050 cond = simplify_with_current_value_aux (XEXP (exp, 0));
4051 if (cond == true_rtx)
4052 return false_rtx;
4053 else if (cond == false_rtx)
4054 return true_rtx;
4055 else
4056 return attr_rtx (NOT, cond);
4058 case COND:
4059 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4061 cond = simplify_with_current_value_aux (XVECEXP (exp, 0, i));
4062 if (cond == true_rtx)
4063 return simplify_with_current_value_aux (XVECEXP (exp, 0, i + 1));
4064 else if (cond == false_rtx)
4065 continue;
4066 else
4067 abort (); /* With all EQ_ATTR's of known value, a case should
4068 have been selected. */
4070 return simplify_with_current_value_aux (XEXP (exp, 1));
4072 default:
4073 abort ();
4077 /* Clear the MEM_IN_STRUCT_P flag in EXP and its subexpressions. */
4079 static void
4080 clear_struct_flag (x)
4081 rtx x;
4083 register int i;
4084 register int j;
4085 register enum rtx_code code;
4086 register const char *fmt;
4088 MEM_IN_STRUCT_P (x) = 0;
4089 if (RTX_UNCHANGING_P (x))
4090 return;
4092 code = GET_CODE (x);
4094 switch (code)
4096 case REG:
4097 case QUEUED:
4098 case CONST_INT:
4099 case CONST_DOUBLE:
4100 case SYMBOL_REF:
4101 case CODE_LABEL:
4102 case PC:
4103 case CC0:
4104 case EQ_ATTR:
4105 case ATTR_FLAG:
4106 return;
4108 default:
4109 break;
4112 /* Compare the elements. If any pair of corresponding elements
4113 fail to match, return 0 for the whole things. */
4115 fmt = GET_RTX_FORMAT (code);
4116 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4118 switch (fmt[i])
4120 case 'V':
4121 case 'E':
4122 for (j = 0; j < XVECLEN (x, i); j++)
4123 clear_struct_flag (XVECEXP (x, i, j));
4124 break;
4126 case 'e':
4127 clear_struct_flag (XEXP (x, i));
4128 break;
4133 /* Return the number of RTX objects making up the expression X.
4134 But if we count more than MAX objects, stop counting. */
4136 static int
4137 count_sub_rtxs (x, max)
4138 rtx x;
4139 int max;
4141 register int i;
4142 register int j;
4143 register enum rtx_code code;
4144 register const char *fmt;
4145 int total = 0;
4147 code = GET_CODE (x);
4149 switch (code)
4151 case REG:
4152 case QUEUED:
4153 case CONST_INT:
4154 case CONST_DOUBLE:
4155 case SYMBOL_REF:
4156 case CODE_LABEL:
4157 case PC:
4158 case CC0:
4159 case EQ_ATTR:
4160 case ATTR_FLAG:
4161 return 1;
4163 default:
4164 break;
4167 /* Compare the elements. If any pair of corresponding elements
4168 fail to match, return 0 for the whole things. */
4170 fmt = GET_RTX_FORMAT (code);
4171 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4173 if (total >= max)
4174 return total;
4176 switch (fmt[i])
4178 case 'V':
4179 case 'E':
4180 for (j = 0; j < XVECLEN (x, i); j++)
4181 total += count_sub_rtxs (XVECEXP (x, i, j), max);
4182 break;
4184 case 'e':
4185 total += count_sub_rtxs (XEXP (x, i), max);
4186 break;
4189 return total;
4193 /* Create table entries for DEFINE_ATTR. */
4195 static void
4196 gen_attr (exp, lineno)
4197 rtx exp;
4198 int lineno;
4200 struct attr_desc *attr;
4201 struct attr_value *av;
4202 const char *name_ptr;
4203 char *p;
4205 /* Make a new attribute structure. Check for duplicate by looking at
4206 attr->default_val, since it is initialized by this routine. */
4207 attr = find_attr (XSTR (exp, 0), 1);
4208 if (attr->default_val)
4210 message_with_line (lineno, "duplicate definition for attribute %s",
4211 attr->name);
4212 message_with_line (attr->lineno, "previous definition");
4213 have_error = 1;
4214 return;
4216 attr->lineno = lineno;
4218 if (*XSTR (exp, 1) == '\0')
4219 attr->is_numeric = 1;
4220 else
4222 name_ptr = XSTR (exp, 1);
4223 while ((p = next_comma_elt (&name_ptr)) != NULL)
4225 av = (struct attr_value *) oballoc (sizeof (struct attr_value));
4226 av->value = attr_rtx (CONST_STRING, p);
4227 av->next = attr->first_value;
4228 attr->first_value = av;
4229 av->first_insn = NULL;
4230 av->num_insns = 0;
4231 av->has_asm_insn = 0;
4235 if (GET_CODE (XEXP (exp, 2)) == CONST)
4237 attr->is_const = 1;
4238 if (attr->is_numeric)
4240 message_with_line (lineno,
4241 "constant attributes may not take numeric values");
4242 have_error = 1;
4245 /* Get rid of the CONST node. It is allowed only at top-level. */
4246 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
4249 if (! strcmp (attr->name, "length") && ! attr->is_numeric)
4251 message_with_line (lineno, "`length' attribute must take numeric values");
4252 have_error = 1;
4255 /* Set up the default value. */
4256 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
4257 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
4260 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
4261 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
4262 number of alternatives as this should be checked elsewhere. */
4264 static int
4265 count_alternatives (exp)
4266 rtx exp;
4268 int i, j, n;
4269 const char *fmt;
4271 if (GET_CODE (exp) == MATCH_OPERAND)
4272 return n_comma_elts (XSTR (exp, 2));
4274 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4275 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4276 switch (*fmt++)
4278 case 'e':
4279 case 'u':
4280 n = count_alternatives (XEXP (exp, i));
4281 if (n)
4282 return n;
4283 break;
4285 case 'E':
4286 case 'V':
4287 if (XVEC (exp, i) != NULL)
4288 for (j = 0; j < XVECLEN (exp, i); j++)
4290 n = count_alternatives (XVECEXP (exp, i, j));
4291 if (n)
4292 return n;
4296 return 0;
4299 /* Returns non-zero if the given expression contains an EQ_ATTR with the
4300 `alternative' attribute. */
4302 static int
4303 compares_alternatives_p (exp)
4304 rtx exp;
4306 int i, j;
4307 const char *fmt;
4309 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
4310 return 1;
4312 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4313 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4314 switch (*fmt++)
4316 case 'e':
4317 case 'u':
4318 if (compares_alternatives_p (XEXP (exp, i)))
4319 return 1;
4320 break;
4322 case 'E':
4323 for (j = 0; j < XVECLEN (exp, i); j++)
4324 if (compares_alternatives_p (XVECEXP (exp, i, j)))
4325 return 1;
4326 break;
4329 return 0;
4332 /* Returns non-zero is INNER is contained in EXP. */
4334 static int
4335 contained_in_p (inner, exp)
4336 rtx inner;
4337 rtx exp;
4339 int i, j;
4340 const char *fmt;
4342 if (rtx_equal_p (inner, exp))
4343 return 1;
4345 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4346 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4347 switch (*fmt++)
4349 case 'e':
4350 case 'u':
4351 if (contained_in_p (inner, XEXP (exp, i)))
4352 return 1;
4353 break;
4355 case 'E':
4356 for (j = 0; j < XVECLEN (exp, i); j++)
4357 if (contained_in_p (inner, XVECEXP (exp, i, j)))
4358 return 1;
4359 break;
4362 return 0;
4365 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
4367 static void
4368 gen_insn (exp, lineno)
4369 rtx exp;
4370 int lineno;
4372 struct insn_def *id;
4374 id = (struct insn_def *) oballoc (sizeof (struct insn_def));
4375 id->next = defs;
4376 defs = id;
4377 id->def = exp;
4378 id->lineno = lineno;
4380 switch (GET_CODE (exp))
4382 case DEFINE_INSN:
4383 id->insn_code = insn_code_number;
4384 id->insn_index = insn_index_number;
4385 id->num_alternatives = count_alternatives (exp);
4386 if (id->num_alternatives == 0)
4387 id->num_alternatives = 1;
4388 id->vec_idx = 4;
4389 break;
4391 case DEFINE_PEEPHOLE:
4392 id->insn_code = insn_code_number;
4393 id->insn_index = insn_index_number;
4394 id->num_alternatives = count_alternatives (exp);
4395 if (id->num_alternatives == 0)
4396 id->num_alternatives = 1;
4397 id->vec_idx = 3;
4398 break;
4400 case DEFINE_ASM_ATTRIBUTES:
4401 id->insn_code = -1;
4402 id->insn_index = -1;
4403 id->num_alternatives = 1;
4404 id->vec_idx = 0;
4405 got_define_asm_attributes = 1;
4406 break;
4408 default:
4409 abort ();
4413 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
4414 true or annul false is specified, and make a `struct delay_desc'. */
4416 static void
4417 gen_delay (def, lineno)
4418 rtx def;
4419 int lineno;
4421 struct delay_desc *delay;
4422 int i;
4424 if (XVECLEN (def, 1) % 3 != 0)
4426 message_with_line (lineno,
4427 "number of elements in DEFINE_DELAY must be multiple of three");
4428 have_error = 1;
4429 return;
4432 for (i = 0; i < XVECLEN (def, 1); i += 3)
4434 if (XVECEXP (def, 1, i + 1))
4435 have_annul_true = 1;
4436 if (XVECEXP (def, 1, i + 2))
4437 have_annul_false = 1;
4440 delay = (struct delay_desc *) oballoc (sizeof (struct delay_desc));
4441 delay->def = def;
4442 delay->num = ++num_delays;
4443 delay->next = delays;
4444 delay->lineno = lineno;
4445 delays = delay;
4448 /* Process a DEFINE_FUNCTION_UNIT.
4450 This gives information about a function unit contained in the CPU.
4451 We fill in a `struct function_unit_op' and a `struct function_unit'
4452 with information used later by `expand_unit'. */
4454 static void
4455 gen_unit (def, lineno)
4456 rtx def;
4457 int lineno;
4459 struct function_unit *unit;
4460 struct function_unit_op *op;
4461 const char *name = XSTR (def, 0);
4462 int multiplicity = XINT (def, 1);
4463 int simultaneity = XINT (def, 2);
4464 rtx condexp = XEXP (def, 3);
4465 int ready_cost = MAX (XINT (def, 4), 1);
4466 int issue_delay = MAX (XINT (def, 5), 1);
4468 /* See if we have already seen this function unit. If so, check that
4469 the multiplicity and simultaneity values are the same. If not, make
4470 a structure for this function unit. */
4471 for (unit = units; unit; unit = unit->next)
4472 if (! strcmp (unit->name, name))
4474 if (unit->multiplicity != multiplicity
4475 || unit->simultaneity != simultaneity)
4477 message_with_line (lineno,
4478 "differing specifications given for function unit %s",
4479 unit->name);
4480 message_with_line (unit->first_lineno, "previous definition");
4481 have_error = 1;
4482 return;
4484 break;
4487 if (unit == 0)
4489 unit = (struct function_unit *) oballoc (sizeof (struct function_unit));
4490 unit->name = name;
4491 unit->multiplicity = multiplicity;
4492 unit->simultaneity = simultaneity;
4493 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
4494 unit->num = num_units++;
4495 unit->num_opclasses = 0;
4496 unit->condexp = false_rtx;
4497 unit->ops = 0;
4498 unit->next = units;
4499 unit->first_lineno = lineno;
4500 units = unit;
4503 /* Make a new operation class structure entry and initialize it. */
4504 op = (struct function_unit_op *) oballoc (sizeof (struct function_unit_op));
4505 op->condexp = condexp;
4506 op->num = unit->num_opclasses++;
4507 op->ready = ready_cost;
4508 op->issue_delay = issue_delay;
4509 op->next = unit->ops;
4510 op->lineno = lineno;
4511 unit->ops = op;
4512 num_unit_opclasses++;
4514 /* Set our issue expression based on whether or not an optional conflict
4515 vector was specified. */
4516 if (XVEC (def, 6))
4518 /* Compute the IOR of all the specified expressions. */
4519 rtx orexp = false_rtx;
4520 int i;
4522 for (i = 0; i < XVECLEN (def, 6); i++)
4523 orexp = insert_right_side (IOR, orexp, XVECEXP (def, 6, i), -2, -2);
4525 op->conflict_exp = orexp;
4526 extend_range (&unit->issue_delay, 1, issue_delay);
4528 else
4530 op->conflict_exp = true_rtx;
4531 extend_range (&unit->issue_delay, issue_delay, issue_delay);
4534 /* Merge our conditional into that of the function unit so we can determine
4535 which insns are used by the function unit. */
4536 unit->condexp = insert_right_side (IOR, unit->condexp, op->condexp, -2, -2);
4539 /* Given a piece of RTX, print a C expression to test its truth value.
4540 We use AND and IOR both for logical and bit-wise operations, so
4541 interpret them as logical unless they are inside a comparison expression.
4542 The first bit of FLAGS will be non-zero in that case.
4544 Set the second bit of FLAGS to make references to attribute values use
4545 a cached local variable instead of calling a function. */
4547 static void
4548 write_test_expr (exp, flags)
4549 rtx exp;
4550 int flags;
4552 int comparison_operator = 0;
4553 RTX_CODE code;
4554 struct attr_desc *attr;
4556 /* In order not to worry about operator precedence, surround our part of
4557 the expression with parentheses. */
4559 printf ("(");
4560 code = GET_CODE (exp);
4561 switch (code)
4563 /* Binary operators. */
4564 case EQ: case NE:
4565 case GE: case GT: case GEU: case GTU:
4566 case LE: case LT: case LEU: case LTU:
4567 comparison_operator = 1;
4569 case PLUS: case MINUS: case MULT: case DIV: case MOD:
4570 case AND: case IOR: case XOR:
4571 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4572 write_test_expr (XEXP (exp, 0), flags | comparison_operator);
4573 switch (code)
4575 case EQ:
4576 printf (" == ");
4577 break;
4578 case NE:
4579 printf (" != ");
4580 break;
4581 case GE:
4582 printf (" >= ");
4583 break;
4584 case GT:
4585 printf (" > ");
4586 break;
4587 case GEU:
4588 printf (" >= (unsigned) ");
4589 break;
4590 case GTU:
4591 printf (" > (unsigned) ");
4592 break;
4593 case LE:
4594 printf (" <= ");
4595 break;
4596 case LT:
4597 printf (" < ");
4598 break;
4599 case LEU:
4600 printf (" <= (unsigned) ");
4601 break;
4602 case LTU:
4603 printf (" < (unsigned) ");
4604 break;
4605 case PLUS:
4606 printf (" + ");
4607 break;
4608 case MINUS:
4609 printf (" - ");
4610 break;
4611 case MULT:
4612 printf (" * ");
4613 break;
4614 case DIV:
4615 printf (" / ");
4616 break;
4617 case MOD:
4618 printf (" %% ");
4619 break;
4620 case AND:
4621 if (flags & 1)
4622 printf (" & ");
4623 else
4624 printf (" && ");
4625 break;
4626 case IOR:
4627 if (flags & 1)
4628 printf (" | ");
4629 else
4630 printf (" || ");
4631 break;
4632 case XOR:
4633 printf (" ^ ");
4634 break;
4635 case ASHIFT:
4636 printf (" << ");
4637 break;
4638 case LSHIFTRT:
4639 case ASHIFTRT:
4640 printf (" >> ");
4641 break;
4642 default:
4643 abort ();
4646 write_test_expr (XEXP (exp, 1), flags | comparison_operator);
4647 break;
4649 case NOT:
4650 /* Special-case (not (eq_attrq "alternative" "x")) */
4651 if (! (flags & 1) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
4652 && XSTR (XEXP (exp, 0), 0) == alternative_name)
4654 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
4655 break;
4658 /* Otherwise, fall through to normal unary operator. */
4660 /* Unary operators. */
4661 case ABS: case NEG:
4662 switch (code)
4664 case NOT:
4665 if (flags & 1)
4666 printf ("~ ");
4667 else
4668 printf ("! ");
4669 break;
4670 case ABS:
4671 printf ("abs ");
4672 break;
4673 case NEG:
4674 printf ("-");
4675 break;
4676 default:
4677 abort ();
4680 write_test_expr (XEXP (exp, 0), flags);
4681 break;
4683 /* Comparison test of an attribute with a value. Most of these will
4684 have been removed by optimization. Handle "alternative"
4685 specially and give error if EQ_ATTR present inside a comparison. */
4686 case EQ_ATTR:
4687 if (flags & 1)
4688 fatal ("EQ_ATTR not valid inside comparison");
4690 if (XSTR (exp, 0) == alternative_name)
4692 printf ("which_alternative == %s", XSTR (exp, 1));
4693 break;
4696 attr = find_attr (XSTR (exp, 0), 0);
4697 if (! attr) abort ();
4699 /* Now is the time to expand the value of a constant attribute. */
4700 if (attr->is_const)
4702 write_test_expr (evaluate_eq_attr (exp, attr->default_val->value,
4703 -2, -2),
4704 flags);
4706 else
4708 if (flags & 2)
4709 printf ("attr_%s", attr->name);
4710 else
4711 printf ("get_attr_%s (insn)", attr->name);
4712 printf (" == ");
4713 write_attr_valueq (attr, XSTR (exp, 1));
4715 break;
4717 /* Comparison test of flags for define_delays. */
4718 case ATTR_FLAG:
4719 if (flags & 1)
4720 fatal ("ATTR_FLAG not valid inside comparison");
4721 printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
4722 break;
4724 /* See if an operand matches a predicate. */
4725 case MATCH_OPERAND:
4726 /* If only a mode is given, just ensure the mode matches the operand.
4727 If neither a mode nor predicate is given, error. */
4728 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
4730 if (GET_MODE (exp) == VOIDmode)
4731 fatal ("Null MATCH_OPERAND specified as test");
4732 else
4733 printf ("GET_MODE (operands[%d]) == %smode",
4734 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4736 else
4737 printf ("%s (operands[%d], %smode)",
4738 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4739 break;
4741 case MATCH_INSN:
4742 printf ("%s (insn)", XSTR (exp, 0));
4743 break;
4745 /* Constant integer. */
4746 case CONST_INT:
4747 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
4748 break;
4750 /* A random C expression. */
4751 case SYMBOL_REF:
4752 printf ("%s", XSTR (exp, 0));
4753 break;
4755 /* The address of the branch target. */
4756 case MATCH_DUP:
4757 printf ("INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
4758 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
4759 break;
4761 case PC:
4762 /* The address of the current insn. We implement this actually as the
4763 address of the current insn for backward branches, but the last
4764 address of the next insn for forward branches, and both with
4765 adjustments that account for the worst-case possible stretching of
4766 intervening alignments between this insn and its destination. */
4767 printf("insn_current_reference_address (insn)");
4768 break;
4770 case CONST_STRING:
4771 printf ("%s", XSTR (exp, 0));
4772 break;
4774 case IF_THEN_ELSE:
4775 write_test_expr (XEXP (exp, 0), flags & 2);
4776 printf (" ? ");
4777 write_test_expr (XEXP (exp, 1), flags | 1);
4778 printf (" : ");
4779 write_test_expr (XEXP (exp, 2), flags | 1);
4780 break;
4782 default:
4783 fatal ("bad RTX code `%s' in attribute calculation\n",
4784 GET_RTX_NAME (code));
4787 printf (")");
4790 /* Given an attribute value, return the maximum CONST_STRING argument
4791 encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
4793 static int
4794 max_attr_value (exp, unknownp)
4795 rtx exp;
4796 int *unknownp;
4798 int current_max;
4799 int i, n;
4801 switch (GET_CODE (exp))
4803 case CONST_STRING:
4804 current_max = atoi (XSTR (exp, 0));
4805 break;
4807 case COND:
4808 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4809 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4811 n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4812 if (n > current_max)
4813 current_max = n;
4815 break;
4817 case IF_THEN_ELSE:
4818 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4819 n = max_attr_value (XEXP (exp, 2), unknownp);
4820 if (n > current_max)
4821 current_max = n;
4822 break;
4824 default:
4825 *unknownp = 1;
4826 current_max = INT_MAX;
4827 break;
4830 return current_max;
4833 /* Given an attribute value, return the result of ORing together all
4834 CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
4835 if the numeric value is not known. */
4837 static int
4838 or_attr_value (exp, unknownp)
4839 rtx exp;
4840 int *unknownp;
4842 int current_or;
4843 int i;
4845 switch (GET_CODE (exp))
4847 case CONST_STRING:
4848 current_or = atoi (XSTR (exp, 0));
4849 break;
4851 case COND:
4852 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4853 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4854 current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4855 break;
4857 case IF_THEN_ELSE:
4858 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4859 current_or |= or_attr_value (XEXP (exp, 2), unknownp);
4860 break;
4862 default:
4863 *unknownp = 1;
4864 current_or = -1;
4865 break;
4868 return current_or;
4871 /* Scan an attribute value, possibly a conditional, and record what actions
4872 will be required to do any conditional tests in it.
4874 Specifically, set
4875 `must_extract' if we need to extract the insn operands
4876 `must_constrain' if we must compute `which_alternative'
4877 `address_used' if an address expression was used
4878 `length_used' if an (eq_attr "length" ...) was used
4881 static void
4882 walk_attr_value (exp)
4883 rtx exp;
4885 register int i, j;
4886 register const char *fmt;
4887 RTX_CODE code;
4889 if (exp == NULL)
4890 return;
4892 code = GET_CODE (exp);
4893 switch (code)
4895 case SYMBOL_REF:
4896 if (! RTX_UNCHANGING_P (exp))
4897 /* Since this is an arbitrary expression, it can look at anything.
4898 However, constant expressions do not depend on any particular
4899 insn. */
4900 must_extract = must_constrain = 1;
4901 return;
4903 case MATCH_OPERAND:
4904 must_extract = 1;
4905 return;
4907 case EQ_ATTR:
4908 if (XSTR (exp, 0) == alternative_name)
4909 must_extract = must_constrain = 1;
4910 else if (strcmp (XSTR (exp, 0), "length") == 0)
4911 length_used = 1;
4912 return;
4914 case MATCH_DUP:
4915 must_extract = 1;
4916 address_used = 1;
4917 return;
4919 case PC:
4920 address_used = 1;
4921 return;
4923 case ATTR_FLAG:
4924 return;
4926 default:
4927 break;
4930 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
4931 switch (*fmt++)
4933 case 'e':
4934 case 'u':
4935 walk_attr_value (XEXP (exp, i));
4936 break;
4938 case 'E':
4939 if (XVEC (exp, i) != NULL)
4940 for (j = 0; j < XVECLEN (exp, i); j++)
4941 walk_attr_value (XVECEXP (exp, i, j));
4942 break;
4946 /* Write out a function to obtain the attribute for a given INSN. */
4948 static void
4949 write_attr_get (attr)
4950 struct attr_desc *attr;
4952 struct attr_value *av, *common_av;
4954 /* Find the most used attribute value. Handle that as the `default' of the
4955 switch we will generate. */
4956 common_av = find_most_used (attr);
4958 /* Write out prototype of function. */
4959 if (!attr->is_numeric)
4960 printf ("extern enum attr_%s ", attr->name);
4961 else if (attr->unsigned_p)
4962 printf ("extern unsigned int ");
4963 else
4964 printf ("extern int ");
4965 /* If the attribute name starts with a star, the remainder is the name of
4966 the subroutine to use, instead of `get_attr_...'. */
4967 if (attr->name[0] == '*')
4968 printf ("%s PARAMS ((rtx));\n", &attr->name[1]);
4969 else
4970 printf ("get_attr_%s PARAMS ((%s));\n", attr->name,
4971 (attr->is_const ? "void" : "rtx"));
4973 /* Write out start of function, then all values with explicit `case' lines,
4974 then a `default', then the value with the most uses. */
4975 if (!attr->is_numeric)
4976 printf ("enum attr_%s\n", attr->name);
4977 else if (attr->unsigned_p)
4978 printf ("unsigned int\n");
4979 else
4980 printf ("int\n");
4982 /* If the attribute name starts with a star, the remainder is the name of
4983 the subroutine to use, instead of `get_attr_...'. */
4984 if (attr->name[0] == '*')
4985 printf ("%s (insn)\n", &attr->name[1]);
4986 else if (attr->is_const == 0)
4987 printf ("get_attr_%s (insn)\n", attr->name);
4988 else
4990 printf ("get_attr_%s ()\n", attr->name);
4991 printf ("{\n");
4993 for (av = attr->first_value; av; av = av->next)
4994 if (av->num_insns != 0)
4995 write_attr_set (attr, 2, av->value, "return", ";",
4996 true_rtx, av->first_insn->insn_code,
4997 av->first_insn->insn_index);
4999 printf ("}\n\n");
5000 return;
5003 printf (" rtx insn;\n");
5004 printf ("{\n");
5006 if (GET_CODE (common_av->value) == FFS)
5008 rtx p = XEXP (common_av->value, 0);
5010 /* No need to emit code to abort if the insn is unrecognized; the
5011 other get_attr_foo functions will do that when we call them. */
5013 write_toplevel_expr (p);
5015 printf ("\n if (accum && accum == (accum & -accum))\n");
5016 printf (" {\n");
5017 printf (" int i;\n");
5018 printf (" for (i = 0; accum >>= 1; ++i) continue;\n");
5019 printf (" accum = i;\n");
5020 printf (" }\n else\n");
5021 printf (" accum = ~accum;\n");
5022 printf (" return accum;\n}\n\n");
5024 else
5026 printf (" switch (recog_memoized (insn))\n");
5027 printf (" {\n");
5029 for (av = attr->first_value; av; av = av->next)
5030 if (av != common_av)
5031 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
5033 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
5034 printf (" }\n}\n\n");
5038 /* Given an AND tree of known true terms (because we are inside an `if' with
5039 that as the condition or are in an `else' clause) and an expression,
5040 replace any known true terms with TRUE. Use `simplify_and_tree' to do
5041 the bulk of the work. */
5043 static rtx
5044 eliminate_known_true (known_true, exp, insn_code, insn_index)
5045 rtx known_true;
5046 rtx exp;
5047 int insn_code, insn_index;
5049 rtx term;
5051 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
5053 if (GET_CODE (known_true) == AND)
5055 exp = eliminate_known_true (XEXP (known_true, 0), exp,
5056 insn_code, insn_index);
5057 exp = eliminate_known_true (XEXP (known_true, 1), exp,
5058 insn_code, insn_index);
5060 else
5062 term = known_true;
5063 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
5066 return exp;
5069 /* Write out a series of tests and assignment statements to perform tests and
5070 sets of an attribute value. We are passed an indentation amount and prefix
5071 and suffix strings to write around each attribute value (e.g., "return"
5072 and ";"). */
5074 static void
5075 write_attr_set (attr, indent, value, prefix, suffix, known_true,
5076 insn_code, insn_index)
5077 struct attr_desc *attr;
5078 int indent;
5079 rtx value;
5080 const char *prefix;
5081 const char *suffix;
5082 rtx known_true;
5083 int insn_code, insn_index;
5085 if (GET_CODE (value) == COND)
5087 /* Assume the default value will be the default of the COND unless we
5088 find an always true expression. */
5089 rtx default_val = XEXP (value, 1);
5090 rtx our_known_true = known_true;
5091 rtx newexp;
5092 int first_if = 1;
5093 int i;
5095 for (i = 0; i < XVECLEN (value, 0); i += 2)
5097 rtx testexp;
5098 rtx inner_true;
5100 testexp = eliminate_known_true (our_known_true,
5101 XVECEXP (value, 0, i),
5102 insn_code, insn_index);
5103 newexp = attr_rtx (NOT, testexp);
5104 newexp = insert_right_side (AND, our_known_true, newexp,
5105 insn_code, insn_index);
5107 /* If the test expression is always true or if the next `known_true'
5108 expression is always false, this is the last case, so break
5109 out and let this value be the `else' case. */
5110 if (testexp == true_rtx || newexp == false_rtx)
5112 default_val = XVECEXP (value, 0, i + 1);
5113 break;
5116 /* Compute the expression to pass to our recursive call as being
5117 known true. */
5118 inner_true = insert_right_side (AND, our_known_true,
5119 testexp, insn_code, insn_index);
5121 /* If this is always false, skip it. */
5122 if (inner_true == false_rtx)
5123 continue;
5125 write_indent (indent);
5126 printf ("%sif ", first_if ? "" : "else ");
5127 first_if = 0;
5128 write_test_expr (testexp, 0);
5129 printf ("\n");
5130 write_indent (indent + 2);
5131 printf ("{\n");
5133 write_attr_set (attr, indent + 4,
5134 XVECEXP (value, 0, i + 1), prefix, suffix,
5135 inner_true, insn_code, insn_index);
5136 write_indent (indent + 2);
5137 printf ("}\n");
5138 our_known_true = newexp;
5141 if (! first_if)
5143 write_indent (indent);
5144 printf ("else\n");
5145 write_indent (indent + 2);
5146 printf ("{\n");
5149 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
5150 prefix, suffix, our_known_true, insn_code, insn_index);
5152 if (! first_if)
5154 write_indent (indent + 2);
5155 printf ("}\n");
5158 else
5160 write_indent (indent);
5161 printf ("%s ", prefix);
5162 write_attr_value (attr, value);
5163 printf ("%s\n", suffix);
5167 /* Write out the computation for one attribute value. */
5169 static void
5170 write_attr_case (attr, av, write_case_lines, prefix, suffix, indent,
5171 known_true)
5172 struct attr_desc *attr;
5173 struct attr_value *av;
5174 int write_case_lines;
5175 const char *prefix, *suffix;
5176 int indent;
5177 rtx known_true;
5179 struct insn_ent *ie;
5181 if (av->num_insns == 0)
5182 return;
5184 if (av->has_asm_insn)
5186 write_indent (indent);
5187 printf ("case -1:\n");
5188 write_indent (indent + 2);
5189 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
5190 write_indent (indent + 2);
5191 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
5192 write_indent (indent + 2);
5193 printf (" fatal_insn_not_found (insn);\n");
5196 if (write_case_lines)
5198 for (ie = av->first_insn; ie; ie = ie->next)
5199 if (ie->insn_code != -1)
5201 write_indent (indent);
5202 printf ("case %d:\n", ie->insn_code);
5205 else
5207 write_indent (indent);
5208 printf ("default:\n");
5211 /* See what we have to do to output this value. */
5212 must_extract = must_constrain = address_used = 0;
5213 walk_attr_value (av->value);
5215 if (must_extract)
5217 write_indent (indent + 2);
5218 printf ("extract_insn (insn);\n");
5221 if (must_constrain)
5223 write_indent (indent + 2);
5224 printf ("if (! constrain_operands (reload_completed))\n");
5225 write_indent (indent + 2);
5226 printf (" fatal_insn_not_found (insn);\n");
5229 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
5230 known_true, av->first_insn->insn_code,
5231 av->first_insn->insn_index);
5233 if (strncmp (prefix, "return", 6))
5235 write_indent (indent + 2);
5236 printf ("break;\n");
5238 printf ("\n");
5241 /* Search for uses of non-const attributes and write code to cache them. */
5243 static int
5244 write_expr_attr_cache (p, attr)
5245 rtx p;
5246 struct attr_desc *attr;
5248 const char *fmt;
5249 int i, ie, j, je;
5251 if (GET_CODE (p) == EQ_ATTR)
5253 if (XSTR (p, 0) != attr->name)
5254 return 0;
5256 if (!attr->is_numeric)
5257 printf (" register enum attr_%s ", attr->name);
5258 else if (attr->unsigned_p)
5259 printf (" register unsigned int ");
5260 else
5261 printf (" register int ");
5263 printf ("attr_%s = get_attr_%s (insn);\n", attr->name, attr->name);
5264 return 1;
5267 fmt = GET_RTX_FORMAT (GET_CODE (p));
5268 ie = GET_RTX_LENGTH (GET_CODE (p));
5269 for (i = 0; i < ie; i++)
5271 switch (*fmt++)
5273 case 'e':
5274 if (write_expr_attr_cache (XEXP (p, i), attr))
5275 return 1;
5276 break;
5278 case 'E':
5279 je = XVECLEN (p, i);
5280 for (j = 0; j < je; ++j)
5281 if (write_expr_attr_cache (XVECEXP (p, i, j), attr))
5282 return 1;
5283 break;
5287 return 0;
5290 /* Evaluate an expression at top level. A front end to write_test_expr,
5291 in which we cache attribute values and break up excessively large
5292 expressions to cater to older compilers. */
5294 static void
5295 write_toplevel_expr (p)
5296 rtx p;
5298 struct attr_desc *attr;
5299 int i;
5301 for (i = 0; i < MAX_ATTRS_INDEX; ++i)
5302 for (attr = attrs[i]; attr ; attr = attr->next)
5303 if (!attr->is_const)
5304 write_expr_attr_cache (p, attr);
5306 printf(" register unsigned long accum = 0;\n\n");
5308 while (GET_CODE (p) == IOR)
5310 rtx e;
5311 if (GET_CODE (XEXP (p, 0)) == IOR)
5312 e = XEXP (p, 1), p = XEXP (p, 0);
5313 else
5314 e = XEXP (p, 0), p = XEXP (p, 1);
5316 printf (" accum |= ");
5317 write_test_expr (e, 3);
5318 printf (";\n");
5320 printf (" accum |= ");
5321 write_test_expr (p, 3);
5322 printf (";\n");
5325 /* Utilities to write names in various forms. */
5327 static void
5328 write_unit_name (prefix, num, suffix)
5329 const char *prefix;
5330 int num;
5331 const char *suffix;
5333 struct function_unit *unit;
5335 for (unit = units; unit; unit = unit->next)
5336 if (unit->num == num)
5338 printf ("%s%s%s", prefix, unit->name, suffix);
5339 return;
5342 printf ("%s<unknown>%s", prefix, suffix);
5345 static void
5346 write_attr_valueq (attr, s)
5347 struct attr_desc *attr;
5348 const char *s;
5350 if (attr->is_numeric)
5352 int num = atoi (s);
5354 printf ("%d", num);
5356 /* Make the blockage range values and function units used values easier
5357 to read. */
5358 if (attr->func_units_p)
5360 if (num == -1)
5361 printf (" /* units: none */");
5362 else if (num >= 0)
5363 write_unit_name (" /* units: ", num, " */");
5364 else
5366 int i;
5367 const char *sep = " /* units: ";
5368 for (i = 0, num = ~num; num; i++, num >>= 1)
5369 if (num & 1)
5371 write_unit_name (sep, i, (num == 1) ? " */" : "");
5372 sep = ", ";
5377 else if (attr->blockage_p)
5378 printf (" /* min %d, max %d */", num >> (HOST_BITS_PER_INT / 2),
5379 num & ((1 << (HOST_BITS_PER_INT / 2)) - 1));
5381 else if (num > 9 || num < 0)
5382 printf (" /* 0x%x */", num);
5384 else
5386 write_upcase (attr->name);
5387 printf ("_");
5388 write_upcase (s);
5392 static void
5393 write_attr_value (attr, value)
5394 struct attr_desc *attr;
5395 rtx value;
5397 int op;
5399 switch (GET_CODE (value))
5401 case CONST_STRING:
5402 write_attr_valueq (attr, XSTR (value, 0));
5403 break;
5405 case CONST_INT:
5406 printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
5407 break;
5409 case SYMBOL_REF:
5410 fputs (XSTR (value, 0), stdout);
5411 break;
5413 case ATTR:
5415 struct attr_desc *attr2 = find_attr (XSTR (value, 0), 0);
5416 printf ("get_attr_%s (%s)", attr2->name,
5417 (attr2->is_const ? "" : "insn"));
5419 break;
5421 case PLUS:
5422 op = '+';
5423 goto do_operator;
5424 case MINUS:
5425 op = '-';
5426 goto do_operator;
5427 case MULT:
5428 op = '*';
5429 goto do_operator;
5430 case DIV:
5431 op = '/';
5432 goto do_operator;
5433 case MOD:
5434 op = '%';
5435 goto do_operator;
5437 do_operator:
5438 write_attr_value (attr, XEXP (value, 0));
5439 putchar (' ');
5440 putchar (op);
5441 putchar (' ');
5442 write_attr_value (attr, XEXP (value, 1));
5443 break;
5445 default:
5446 abort ();
5450 static void
5451 write_upcase (str)
5452 const char *str;
5454 while (*str)
5456 /* The argument of TOUPPER should not have side effects. */
5457 putchar (TOUPPER(*str));
5458 str++;
5462 static void
5463 write_indent (indent)
5464 int indent;
5466 for (; indent > 8; indent -= 8)
5467 printf ("\t");
5469 for (; indent; indent--)
5470 printf (" ");
5473 /* Write a subroutine that is given an insn that requires a delay slot, a
5474 delay slot ordinal, and a candidate insn. It returns non-zero if the
5475 candidate can be placed in the specified delay slot of the insn.
5477 We can write as many as three subroutines. `eligible_for_delay'
5478 handles normal delay slots, `eligible_for_annul_true' indicates that
5479 the specified insn can be annulled if the branch is true, and likewise
5480 for `eligible_for_annul_false'.
5482 KIND is a string distinguishing these three cases ("delay", "annul_true",
5483 or "annul_false"). */
5485 static void
5486 write_eligible_delay (kind)
5487 const char *kind;
5489 struct delay_desc *delay;
5490 int max_slots;
5491 char str[50];
5492 struct attr_desc *attr;
5493 struct attr_value *av, *common_av;
5494 int i;
5496 /* Compute the maximum number of delay slots required. We use the delay
5497 ordinal times this number plus one, plus the slot number as an index into
5498 the appropriate predicate to test. */
5500 for (delay = delays, max_slots = 0; delay; delay = delay->next)
5501 if (XVECLEN (delay->def, 1) / 3 > max_slots)
5502 max_slots = XVECLEN (delay->def, 1) / 3;
5504 /* Write function prelude. */
5506 printf ("int\n");
5507 printf ("eligible_for_%s (delay_insn, slot, candidate_insn, flags)\n",
5508 kind);
5509 printf (" rtx delay_insn;\n");
5510 printf (" int slot;\n");
5511 printf (" rtx candidate_insn;\n");
5512 printf (" int flags ATTRIBUTE_UNUSED;\n");
5513 printf ("{\n");
5514 printf (" rtx insn;\n");
5515 printf ("\n");
5516 printf (" if (slot >= %d)\n", max_slots);
5517 printf (" abort ();\n");
5518 printf ("\n");
5520 /* If more than one delay type, find out which type the delay insn is. */
5522 if (num_delays > 1)
5524 attr = find_attr ("*delay_type", 0);
5525 if (! attr) abort ();
5526 common_av = find_most_used (attr);
5528 printf (" insn = delay_insn;\n");
5529 printf (" switch (recog_memoized (insn))\n");
5530 printf (" {\n");
5532 sprintf (str, " * %d;\n break;", max_slots);
5533 for (av = attr->first_value; av; av = av->next)
5534 if (av != common_av)
5535 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
5537 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
5538 printf (" }\n\n");
5540 /* Ensure matched. Otherwise, shouldn't have been called. */
5541 printf (" if (slot < %d)\n", max_slots);
5542 printf (" abort ();\n\n");
5545 /* If just one type of delay slot, write simple switch. */
5546 if (num_delays == 1 && max_slots == 1)
5548 printf (" insn = candidate_insn;\n");
5549 printf (" switch (recog_memoized (insn))\n");
5550 printf (" {\n");
5552 attr = find_attr ("*delay_1_0", 0);
5553 if (! attr) abort ();
5554 common_av = find_most_used (attr);
5556 for (av = attr->first_value; av; av = av->next)
5557 if (av != common_av)
5558 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
5560 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
5561 printf (" }\n");
5564 else
5566 /* Write a nested CASE. The first indicates which condition we need to
5567 test, and the inner CASE tests the condition. */
5568 printf (" insn = candidate_insn;\n");
5569 printf (" switch (slot)\n");
5570 printf (" {\n");
5572 for (delay = delays; delay; delay = delay->next)
5573 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
5575 printf (" case %d:\n",
5576 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
5577 printf (" switch (recog_memoized (insn))\n");
5578 printf ("\t{\n");
5580 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
5581 attr = find_attr (str, 0);
5582 if (! attr) abort ();
5583 common_av = find_most_used (attr);
5585 for (av = attr->first_value; av; av = av->next)
5586 if (av != common_av)
5587 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
5589 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
5590 printf (" }\n");
5593 printf (" default:\n");
5594 printf (" abort ();\n");
5595 printf (" }\n");
5598 printf ("}\n\n");
5601 /* Write routines to compute conflict cost for function units. Then write a
5602 table describing the available function units. */
5604 static void
5605 write_function_unit_info ()
5607 struct function_unit *unit;
5608 int i;
5610 /* Write out conflict routines for function units. Don't bother writing
5611 one if there is only one issue delay value. */
5613 for (unit = units; unit; unit = unit->next)
5615 if (unit->needs_blockage_function)
5616 write_complex_function (unit, "blockage", "block");
5618 /* If the minimum and maximum conflict costs are the same, there
5619 is only one value, so we don't need a function. */
5620 if (! unit->needs_conflict_function)
5622 unit->default_cost = make_numeric_value (unit->issue_delay.max);
5623 continue;
5626 /* The function first computes the case from the candidate insn. */
5627 unit->default_cost = make_numeric_value (0);
5628 write_complex_function (unit, "conflict_cost", "cost");
5631 /* Now that all functions have been written, write the table describing
5632 the function units. The name is included for documentation purposes
5633 only. */
5635 printf ("struct function_unit_desc function_units[] = {\n");
5637 /* Write out the descriptions in numeric order, but don't force that order
5638 on the list. Doing so increases the runtime of genattrtab.c. */
5639 for (i = 0; i < num_units; i++)
5641 for (unit = units; unit; unit = unit->next)
5642 if (unit->num == i)
5643 break;
5645 printf (" {\"%s\", %d, %d, %d, %s, %d, %s_unit_ready_cost, ",
5646 unit->name, 1 << unit->num, unit->multiplicity,
5647 unit->simultaneity, XSTR (unit->default_cost, 0),
5648 unit->issue_delay.max, unit->name);
5650 if (unit->needs_conflict_function)
5651 printf ("%s_unit_conflict_cost, ", unit->name);
5652 else
5653 printf ("0, ");
5655 printf ("%d, ", unit->max_blockage);
5657 if (unit->needs_range_function)
5658 printf ("%s_unit_blockage_range, ", unit->name);
5659 else
5660 printf ("0, ");
5662 if (unit->needs_blockage_function)
5663 printf ("%s_unit_blockage", unit->name);
5664 else
5665 printf ("0");
5667 printf ("}, \n");
5670 printf ("};\n\n");
5673 static void
5674 write_complex_function (unit, name, connection)
5675 struct function_unit *unit;
5676 const char *name, *connection;
5678 struct attr_desc *case_attr, *attr;
5679 struct attr_value *av, *common_av;
5680 rtx value;
5681 char *str;
5682 int using_case;
5683 int i;
5685 printf ("static int %s_unit_%s PARAMS ((rtx, rtx));\n", unit->name, name);
5686 printf ("static int\n");
5687 printf ("%s_unit_%s (executing_insn, candidate_insn)\n",
5688 unit->name, name);
5689 printf (" rtx executing_insn;\n");
5690 printf (" rtx candidate_insn;\n");
5691 printf ("{\n");
5692 printf (" rtx insn;\n");
5693 printf (" int casenum;\n\n");
5694 printf (" insn = executing_insn;\n");
5695 printf (" switch (recog_memoized (insn))\n");
5696 printf (" {\n");
5698 /* Write the `switch' statement to get the case value. */
5699 str = (char *) alloca (strlen (unit->name) + strlen (name) + strlen (connection) + 10);
5700 sprintf (str, "*%s_cases", unit->name);
5701 case_attr = find_attr (str, 0);
5702 if (! case_attr) abort ();
5703 common_av = find_most_used (case_attr);
5705 for (av = case_attr->first_value; av; av = av->next)
5706 if (av != common_av)
5707 write_attr_case (case_attr, av, 1,
5708 "casenum =", ";", 4, unit->condexp);
5710 write_attr_case (case_attr, common_av, 0,
5711 "casenum =", ";", 4, unit->condexp);
5712 printf (" }\n\n");
5714 /* Now write an outer switch statement on each case. Then write
5715 the tests on the executing function within each. */
5716 printf (" insn = candidate_insn;\n");
5717 printf (" switch (casenum)\n");
5718 printf (" {\n");
5720 for (i = 0; i < unit->num_opclasses; i++)
5722 /* Ensure using this case. */
5723 using_case = 0;
5724 for (av = case_attr->first_value; av; av = av->next)
5725 if (av->num_insns
5726 && contained_in_p (make_numeric_value (i), av->value))
5727 using_case = 1;
5729 if (! using_case)
5730 continue;
5732 printf (" case %d:\n", i);
5733 sprintf (str, "*%s_%s_%d", unit->name, connection, i);
5734 attr = find_attr (str, 0);
5735 if (! attr) abort ();
5737 /* If single value, just write it. */
5738 value = find_single_value (attr);
5739 if (value)
5740 write_attr_set (attr, 6, value, "return", ";\n", true_rtx, -2, -2);
5741 else
5743 common_av = find_most_used (attr);
5744 printf (" switch (recog_memoized (insn))\n");
5745 printf ("\t{\n");
5747 for (av = attr->first_value; av; av = av->next)
5748 if (av != common_av)
5749 write_attr_case (attr, av, 1,
5750 "return", ";", 8, unit->condexp);
5752 write_attr_case (attr, common_av, 0,
5753 "return", ";", 8, unit->condexp);
5754 printf (" }\n\n");
5758 /* This default case should not be needed, but gcc's analysis is not
5759 good enough to realize that the default case is not needed for the
5760 second switch statement. */
5761 printf (" default:\n abort ();\n");
5762 printf (" }\n}\n\n");
5765 /* This page contains miscellaneous utility routines. */
5767 /* Given a string, return the number of comma-separated elements in it.
5768 Return 0 for the null string. */
5770 static int
5771 n_comma_elts (s)
5772 const char *s;
5774 int n;
5776 if (*s == '\0')
5777 return 0;
5779 for (n = 1; *s; s++)
5780 if (*s == ',')
5781 n++;
5783 return n;
5786 /* Given a pointer to a (char *), return a malloc'ed string containing the
5787 next comma-separated element. Advance the pointer to after the string
5788 scanned, or the end-of-string. Return NULL if at end of string. */
5790 static char *
5791 next_comma_elt (pstr)
5792 const char **pstr;
5794 char *out_str;
5795 const char *p;
5797 if (**pstr == '\0')
5798 return NULL;
5800 /* Find end of string to compute length. */
5801 for (p = *pstr; *p != ',' && *p != '\0'; p++)
5804 out_str = attr_string (*pstr, p - *pstr);
5805 *pstr = p;
5807 if (**pstr == ',')
5808 (*pstr)++;
5810 return out_str;
5813 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
5814 is non-zero, build a new attribute, if one does not exist. */
5816 static struct attr_desc *
5817 find_attr (name, create)
5818 const char *name;
5819 int create;
5821 struct attr_desc *attr;
5822 int index;
5824 /* Before we resort to using `strcmp', see if the string address matches
5825 anywhere. In most cases, it should have been canonicalized to do so. */
5826 if (name == alternative_name)
5827 return NULL;
5829 index = name[0] & (MAX_ATTRS_INDEX - 1);
5830 for (attr = attrs[index]; attr; attr = attr->next)
5831 if (name == attr->name)
5832 return attr;
5834 /* Otherwise, do it the slow way. */
5835 for (attr = attrs[index]; attr; attr = attr->next)
5836 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
5837 return attr;
5839 if (! create)
5840 return NULL;
5842 attr = (struct attr_desc *) oballoc (sizeof (struct attr_desc));
5843 attr->name = attr_string (name, strlen (name));
5844 attr->first_value = attr->default_val = NULL;
5845 attr->is_numeric = attr->negative_ok = attr->is_const = attr->is_special = 0;
5846 attr->unsigned_p = attr->func_units_p = attr->blockage_p = 0;
5847 attr->next = attrs[index];
5848 attrs[index] = attr;
5850 return attr;
5853 /* Create internal attribute with the given default value. */
5855 static void
5856 make_internal_attr (name, value, special)
5857 const char *name;
5858 rtx value;
5859 int special;
5861 struct attr_desc *attr;
5863 attr = find_attr (name, 1);
5864 if (attr->default_val)
5865 abort ();
5867 attr->is_numeric = 1;
5868 attr->is_const = 0;
5869 attr->is_special = (special & 1) != 0;
5870 attr->negative_ok = (special & 2) != 0;
5871 attr->unsigned_p = (special & 4) != 0;
5872 attr->func_units_p = (special & 8) != 0;
5873 attr->blockage_p = (special & 16) != 0;
5874 attr->default_val = get_attr_value (value, attr, -2);
5877 /* Find the most used value of an attribute. */
5879 static struct attr_value *
5880 find_most_used (attr)
5881 struct attr_desc *attr;
5883 struct attr_value *av;
5884 struct attr_value *most_used;
5885 int nuses;
5887 most_used = NULL;
5888 nuses = -1;
5890 for (av = attr->first_value; av; av = av->next)
5891 if (av->num_insns > nuses)
5892 nuses = av->num_insns, most_used = av;
5894 return most_used;
5897 /* If an attribute only has a single value used, return it. Otherwise
5898 return NULL. */
5900 static rtx
5901 find_single_value (attr)
5902 struct attr_desc *attr;
5904 struct attr_value *av;
5905 rtx unique_value;
5907 unique_value = NULL;
5908 for (av = attr->first_value; av; av = av->next)
5909 if (av->num_insns)
5911 if (unique_value)
5912 return NULL;
5913 else
5914 unique_value = av->value;
5917 return unique_value;
5920 /* Return (attr_value "n") */
5922 static rtx
5923 make_numeric_value (n)
5924 int n;
5926 static rtx int_values[20];
5927 rtx exp;
5928 char *p;
5930 if (n < 0)
5931 abort ();
5933 if (n < 20 && int_values[n])
5934 return int_values[n];
5936 p = attr_printf (MAX_DIGITS, "%d", n);
5937 exp = attr_rtx (CONST_STRING, p);
5939 if (n < 20)
5940 int_values[n] = exp;
5942 return exp;
5945 static void
5946 extend_range (range, min, max)
5947 struct range *range;
5948 int min;
5949 int max;
5951 if (range->min > min) range->min = min;
5952 if (range->max < max) range->max = max;
5955 static rtx
5956 copy_rtx_unchanging (orig)
5957 register rtx orig;
5959 #if 0
5960 register rtx copy;
5961 register RTX_CODE code;
5962 #endif
5964 if (RTX_UNCHANGING_P (orig) || MEM_IN_STRUCT_P (orig))
5965 return orig;
5967 MEM_IN_STRUCT_P (orig) = 1;
5968 return orig;
5970 #if 0
5971 code = GET_CODE (orig);
5972 switch (code)
5974 case CONST_INT:
5975 case CONST_DOUBLE:
5976 case SYMBOL_REF:
5977 case CODE_LABEL:
5978 return orig;
5980 default:
5981 break;
5984 copy = rtx_alloc (code);
5985 PUT_MODE (copy, GET_MODE (orig));
5986 RTX_UNCHANGING_P (copy) = 1;
5988 bcopy ((char *) &XEXP (orig, 0), (char *) &XEXP (copy, 0),
5989 GET_RTX_LENGTH (GET_CODE (copy)) * sizeof (rtx));
5990 return copy;
5991 #endif
5994 /* Determine if an insn has a constant number of delay slots, i.e., the
5995 number of delay slots is not a function of the length of the insn. */
5997 static void
5998 write_const_num_delay_slots ()
6000 struct attr_desc *attr = find_attr ("*num_delay_slots", 0);
6001 struct attr_value *av;
6002 struct insn_ent *ie;
6004 if (attr)
6006 printf ("int\nconst_num_delay_slots (insn)\n");
6007 printf (" rtx insn;\n");
6008 printf ("{\n");
6009 printf (" switch (recog_memoized (insn))\n");
6010 printf (" {\n");
6012 for (av = attr->first_value; av; av = av->next)
6014 length_used = 0;
6015 walk_attr_value (av->value);
6016 if (length_used)
6018 for (ie = av->first_insn; ie; ie = ie->next)
6019 if (ie->insn_code != -1)
6020 printf (" case %d:\n", ie->insn_code);
6021 printf (" return 0;\n");
6025 printf (" default:\n");
6026 printf (" return 1;\n");
6027 printf (" }\n}\n\n");
6032 extern int main PARAMS ((int, char **));
6035 main (argc, argv)
6036 int argc;
6037 char **argv;
6039 rtx desc;
6040 struct attr_desc *attr;
6041 struct insn_def *id;
6042 rtx tem;
6043 int i;
6045 progname = "genattrtab";
6047 if (argc <= 1)
6048 fatal ("No input file name.");
6050 #if defined (RLIMIT_STACK) && defined (HAVE_GETRLIMIT) && defined (HAVE_SETRLIMIT)
6051 /* Get rid of any avoidable limit on stack size. */
6053 struct rlimit rlim;
6055 /* Set the stack limit huge so that alloca does not fail. */
6056 getrlimit (RLIMIT_STACK, &rlim);
6057 rlim.rlim_cur = rlim.rlim_max;
6058 setrlimit (RLIMIT_STACK, &rlim);
6060 #endif
6062 if (init_md_reader (argv[1]) != SUCCESS_EXIT_CODE)
6063 return (FATAL_EXIT_CODE);
6065 obstack_init (hash_obstack);
6066 obstack_init (temp_obstack);
6068 /* Set up true and false rtx's */
6069 true_rtx = rtx_alloc (CONST_INT);
6070 XWINT (true_rtx, 0) = 1;
6071 false_rtx = rtx_alloc (CONST_INT);
6072 XWINT (false_rtx, 0) = 0;
6073 RTX_UNCHANGING_P (true_rtx) = RTX_UNCHANGING_P (false_rtx) = 1;
6074 RTX_INTEGRATED_P (true_rtx) = RTX_INTEGRATED_P (false_rtx) = 1;
6076 alternative_name = attr_string ("alternative", strlen ("alternative"));
6078 printf ("/* Generated automatically by the program `genattrtab'\n\
6079 from the machine description file `md'. */\n\n");
6081 /* Read the machine description. */
6083 while (1)
6085 int lineno;
6087 desc = read_md_rtx (&lineno, &insn_code_number);
6088 if (desc == NULL)
6089 break;
6091 switch (GET_CODE (desc))
6093 case DEFINE_INSN:
6094 case DEFINE_PEEPHOLE:
6095 case DEFINE_ASM_ATTRIBUTES:
6096 gen_insn(desc, lineno);
6097 break;
6099 case DEFINE_ATTR:
6100 gen_attr (desc, lineno);
6101 break;
6103 case DEFINE_DELAY:
6104 gen_delay (desc, lineno);
6105 break;
6107 case DEFINE_FUNCTION_UNIT:
6108 gen_unit (desc, lineno);
6109 break;
6111 default:
6112 break;
6114 if (GET_CODE (desc) != DEFINE_ASM_ATTRIBUTES)
6115 insn_index_number++;
6118 if (have_error)
6119 return FATAL_EXIT_CODE;
6121 insn_code_number++;
6123 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
6124 if (! got_define_asm_attributes)
6126 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
6127 XVEC (tem, 0) = rtvec_alloc (0);
6128 gen_insn (tem, 0);
6131 /* Expand DEFINE_DELAY information into new attribute. */
6132 if (num_delays)
6133 expand_delays ();
6135 /* Expand DEFINE_FUNCTION_UNIT information into new attributes. */
6136 if (num_units)
6137 expand_units ();
6139 printf ("#include \"config.h\"\n");
6140 printf ("#include \"system.h\"\n");
6141 printf ("#include \"rtl.h\"\n");
6142 printf ("#include \"tm_p.h\"\n");
6143 printf ("#include \"insn-config.h\"\n");
6144 printf ("#include \"recog.h\"\n");
6145 printf ("#include \"regs.h\"\n");
6146 printf ("#include \"real.h\"\n");
6147 printf ("#include \"output.h\"\n");
6148 printf ("#include \"insn-attr.h\"\n");
6149 printf ("#include \"toplev.h\"\n");
6150 printf ("\n");
6151 printf ("#define operands recog_data.operand\n\n");
6153 /* Make `insn_alternatives'. */
6154 insn_alternatives = (int *) oballoc (insn_code_number * sizeof (int));
6155 for (id = defs; id; id = id->next)
6156 if (id->insn_code >= 0)
6157 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
6159 /* Make `insn_n_alternatives'. */
6160 insn_n_alternatives = (int *) oballoc (insn_code_number * sizeof (int));
6161 for (id = defs; id; id = id->next)
6162 if (id->insn_code >= 0)
6163 insn_n_alternatives[id->insn_code] = id->num_alternatives;
6165 /* Prepare to write out attribute subroutines by checking everything stored
6166 away and building the attribute cases. */
6168 check_defs ();
6170 for (i = 0; i < MAX_ATTRS_INDEX; i++)
6171 for (attr = attrs[i]; attr; attr = attr->next)
6172 attr->default_val->value
6173 = check_attr_value (attr->default_val->value, attr);
6175 if (have_error)
6176 return FATAL_EXIT_CODE;
6178 for (i = 0; i < MAX_ATTRS_INDEX; i++)
6179 for (attr = attrs[i]; attr; attr = attr->next)
6180 fill_attr (attr);
6182 /* Construct extra attributes for `length'. */
6183 make_length_attrs ();
6185 /* Perform any possible optimizations to speed up compilation. */
6186 optimize_attrs ();
6188 /* Now write out all the `gen_attr_...' routines. Do these before the
6189 special routines (specifically before write_function_unit_info), so
6190 that they get defined before they are used. */
6192 for (i = 0; i < MAX_ATTRS_INDEX; i++)
6193 for (attr = attrs[i]; attr; attr = attr->next)
6195 if (! attr->is_special && ! attr->is_const)
6196 write_attr_get (attr);
6199 /* Write out delay eligibility information, if DEFINE_DELAY present.
6200 (The function to compute the number of delay slots will be written
6201 below.) */
6202 if (num_delays)
6204 write_eligible_delay ("delay");
6205 if (have_annul_true)
6206 write_eligible_delay ("annul_true");
6207 if (have_annul_false)
6208 write_eligible_delay ("annul_false");
6211 /* Write out information about function units. */
6212 if (num_units)
6213 write_function_unit_info ();
6215 /* Write out constant delay slot info */
6216 write_const_num_delay_slots ();
6218 write_length_unit_log ();
6220 fflush (stdout);
6221 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
6224 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
6225 const char *
6226 get_insn_name (code)
6227 int code ATTRIBUTE_UNUSED;
6229 return NULL;