]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/genattrtab.c
Oops, missed ChangeLog in last checkin...
[thirdparty/gcc.git] / gcc / genattrtab.c
CommitLineData
41299f41 1/* Generate code from machine description to compute values of attributes.
d050d723
JL
2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
9e1b6503 4 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
41299f41
TW
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
a35311b0
RK
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
41299f41 22
6dc42e49 23/* This program handles insn attributes and the DEFINE_DELAY and
41299f41
TW
24 DEFINE_FUNCTION_UNIT definitions.
25
3e7b5313 26 It produces a series of functions named `get_attr_...', one for each insn
41299f41
TW
27 attribute. Each of these is given the rtx for an insn and returns a member
28 of the enum for the attribute.
29
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).
35
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
0eadeb15 38 an operand is found, `extract_insn' is called.
41299f41
TW
39
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'.
48
3e7b5313
TW
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.
55
41299f41
TW
56 Internal attributes are defined to handle DEFINE_DELAY and
57 DEFINE_FUNCTION_UNIT. Special routines are output for these cases.
58
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.
62
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.
67
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.
72
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.
76
77 Once optimization is complete, any required routines and definitions
3e7b5313
TW
78 will be written.
79
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
3715a518
RS
86 definitions (each would be accessed through a pointer).
87
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
72f1215c
TW
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. */
3715a518 97
41299f41 98
0d64891c 99#include "hconfig.h"
0b93b64e 100#include "system.h"
41299f41 101#include "rtl.h"
a3770a81 102#include "ggc.h"
41299f41 103
956d6950
JL
104#ifdef HAVE_SYS_RESOURCE_H
105# include <sys/resource.h>
f0cdf2b2
RK
106#endif
107
31f0534c
RS
108/* We must include obstack.h after <sys/time.h>, to avoid lossage with
109 /usr/include/sys/stdtypes.h on Sun OS 4.x. */
110#include "obstack.h"
f8b6598e 111#include "errors.h"
31f0534c 112
81fd4c6e 113static struct obstack obstack, obstack1, obstack2;
41299f41 114struct obstack *rtl_obstack = &obstack;
7339c88d 115struct obstack *hash_obstack = &obstack1;
81fd4c6e 116struct obstack *temp_obstack = &obstack2;
41299f41
TW
117
118#define obstack_chunk_alloc xmalloc
119#define obstack_chunk_free free
120
0e9414fd
MM
121/* enough space to reserve for printing out ints */
122#define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)
123
41299f41
TW
124/* Define structures used to record attributes and values. */
125
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. */
130
131struct insn_def
132{
0f41302f
MS
133 int insn_code; /* Instruction number. */
134 int insn_index; /* Expression numer in file, for errors. */
135 struct insn_def *next; /* Next insn in chain. */
136 rtx def; /* The DEFINE_... */
41299f41 137 int num_alternatives; /* Number of alternatives. */
0f41302f 138 int vec_idx; /* Index of attribute vector in `def'. */
41299f41
TW
139};
140
141/* Once everything has been read in, we store in each attribute value a list
142 of insn codes that have that value. Here is the structure used for the
143 list. */
144
145struct insn_ent
146{
147 int insn_code; /* Instruction number. */
148 int insn_index; /* Index of definition in file */
149 struct insn_ent *next; /* Next in chain. */
150};
151
152/* Each value of an attribute (either constant or computed) is assigned a
153 structure which is used as the listhead of the insns that have that
154 value. */
155
156struct attr_value
157{
158 rtx value; /* Value of attribute. */
159 struct attr_value *next; /* Next attribute value in chain. */
160 struct insn_ent *first_insn; /* First insn with this value. */
161 int num_insns; /* Number of insns with this value. */
162 int has_asm_insn; /* True if this value used for `asm' insns */
163};
164
165/* Structure for each attribute. */
166
167struct attr_desc
168{
0f41302f
MS
169 char *name; /* Name of attribute. */
170 struct attr_desc *next; /* Next attribute. */
6f6074ea
MM
171 unsigned is_numeric : 1; /* Values of this attribute are numeric. */
172 unsigned negative_ok : 1; /* Allow negative numeric values. */
173 unsigned unsigned_p : 1; /* Make the output function unsigned int. */
174 unsigned is_const : 1; /* Attribute value constant for each run. */
175 unsigned is_special : 1; /* Don't call `write_attr_set'. */
176 unsigned func_units_p : 1; /* this is the function_units attribute */
177 unsigned blockage_p : 1; /* this is the blockage range function */
0f41302f
MS
178 struct attr_value *first_value; /* First value of this attribute. */
179 struct attr_value *default_val; /* Default value for this attribute. */
41299f41
TW
180};
181
3d678dca
RS
182#define NULL_ATTR (struct attr_desc *) NULL
183
bee757e1
TW
184/* A range of values. */
185
186struct range
187{
188 int min;
189 int max;
190};
191
41299f41
TW
192/* Structure for each DEFINE_DELAY. */
193
194struct delay_desc
195{
196 rtx def; /* DEFINE_DELAY expression. */
0f41302f 197 struct delay_desc *next; /* Next DEFINE_DELAY. */
41299f41
TW
198 int num; /* Number of DEFINE_DELAY, starting at 1. */
199};
200
201/* Record information about each DEFINE_FUNCTION_UNIT. */
202
203struct function_unit_op
204{
205 rtx condexp; /* Expression TRUE for applicable insn. */
206 struct function_unit_op *next; /* Next operation for this function unit. */
207 int num; /* Ordinal for this operation type in unit. */
208 int ready; /* Cost until data is ready. */
bee757e1
TW
209 int issue_delay; /* Cost until unit can accept another insn. */
210 rtx conflict_exp; /* Expression TRUE for insns incurring issue delay. */
211 rtx issue_exp; /* Expression computing issue delay. */
41299f41
TW
212};
213
214/* Record information about each function unit mentioned in a
215 DEFINE_FUNCTION_UNIT. */
216
217struct function_unit
218{
3cce094d 219 const char *name; /* Function unit name. */
41299f41
TW
220 struct function_unit *next; /* Next function unit. */
221 int num; /* Ordinal of this unit type. */
222 int multiplicity; /* Number of units of this type. */
223 int simultaneity; /* Maximum number of simultaneous insns
224 on this function unit or 0 if unlimited. */
0f41302f 225 rtx condexp; /* Expression TRUE for insn needing unit. */
41299f41
TW
226 int num_opclasses; /* Number of different operation types. */
227 struct function_unit_op *ops; /* Pointer to first operation type. */
228 int needs_conflict_function; /* Nonzero if a conflict function required. */
bee757e1 229 int needs_blockage_function; /* Nonzero if a blockage function required. */
f75d38a7 230 int needs_range_function; /* Nonzero if blockage range function needed.*/
41299f41 231 rtx default_cost; /* Conflict cost, if constant. */
bee757e1
TW
232 struct range issue_delay; /* Range of issue delay values. */
233 int max_blockage; /* Maximum time an insn blocks the unit. */
41299f41
TW
234};
235
236/* Listheads of above structures. */
237
3715a518
RS
238/* This one is indexed by the first character of the attribute name. */
239#define MAX_ATTRS_INDEX 256
240static struct attr_desc *attrs[MAX_ATTRS_INDEX];
41299f41
TW
241static struct insn_def *defs;
242static struct delay_desc *delays;
243static struct function_unit *units;
244
f75d38a7
RK
245/* An expression where all the unknown terms are EQ_ATTR tests can be
246 rearranged into a COND provided we can enumerate all possible
247 combinations of the unknown values. The set of combinations become the
248 tests of the COND; the value of the expression given that combination is
249 computed and becomes the corresponding value. To do this, we must be
250 able to enumerate all values for each attribute used in the expression
251 (currently, we give up if we find a numeric attribute).
252
253 If the set of EQ_ATTR tests used in an expression tests the value of N
254 different attributes, the list of all possible combinations can be made
255 by walking the N-dimensional attribute space defined by those
256 attributes. We record each of these as a struct dimension.
257
258 The algorithm relies on sharing EQ_ATTR nodes: if two nodes in an
259 expression are the same, the will also have the same address. We find
260 all the EQ_ATTR nodes by marking them MEM_VOLATILE_P. This bit later
261 represents the value of an EQ_ATTR node, so once all nodes are marked,
262 they are also given an initial value of FALSE.
263
264 We then separate the set of EQ_ATTR nodes into dimensions for each
265 attribute and put them on the VALUES list. Terms are added as needed by
266 `add_values_to_cover' so that all possible values of the attribute are
267 tested.
268
269 Each dimension also has a current value. This is the node that is
270 currently considered to be TRUE. If this is one of the nodes added by
271 `add_values_to_cover', all the EQ_ATTR tests in the original expression
272 will be FALSE. Otherwise, only the CURRENT_VALUE will be true.
273
274 NUM_VALUES is simply the length of the VALUES list and is there for
275 convenience.
276
277 Once the dimensions are created, the algorithm enumerates all possible
278 values and computes the current value of the given expression. */
279
280struct dimension
281{
282 struct attr_desc *attr; /* Attribute for this dimension. */
283 rtx values; /* List of attribute values used. */
284 rtx current_value; /* Position in the list for the TRUE value. */
285 int num_values; /* Length of the values list. */
286};
287
0f41302f 288/* Other variables. */
41299f41
TW
289
290static int insn_code_number;
291static int insn_index_number;
292static int got_define_asm_attributes;
293static int must_extract;
294static int must_constrain;
295static int address_used;
d7c665bf 296static int length_used;
41299f41
TW
297static int num_delays;
298static int have_annul_true, have_annul_false;
71d9b493 299static int num_units, num_unit_opclasses;
1c69865d 300static int num_insn_ents;
41299f41
TW
301
302/* Used as operand to `operate_exp': */
303
71d9b493 304enum operator {PLUS_OP, MINUS_OP, POS_MINUS_OP, EQ_OP, OR_OP, ORX_OP, MAX_OP, MIN_OP, RANGE_OP};
41299f41 305
3715a518
RS
306/* Stores, for each insn code, the number of constraint alternatives. */
307
308static int *insn_n_alternatives;
309
41299f41
TW
310/* Stores, for each insn code, a bitmap that has bits on for each possible
311 alternative. */
312
313static int *insn_alternatives;
314
3715a518
RS
315/* If nonzero, assume that the `alternative' attr has this value.
316 This is the hashed, unique string for the numeral
317 whose value is chosen alternative. */
318
3cce094d 319static const char *current_alternative_string;
3715a518 320
41299f41
TW
321/* Used to simplify expressions. */
322
323static rtx true_rtx, false_rtx;
324
325/* Used to reduce calls to `strcmp' */
326
81fd4c6e 327static char *alternative_name;
41299f41 328
21ca87b8
MS
329/* Indicate that REG_DEAD notes are valid if dead_or_set_p is ever
330 called. */
331
332int reload_completed = 0;
333
9ec36da5
JL
334/* Some machines test `optimize' in macros called from rtlanal.c, so we need
335 to define it here. */
336
c5afbb49
JL
337int optimize = 0;
338
41299f41
TW
339/* Simplify an expression. Only call the routine if there is something to
340 simplify. */
341#define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
3715a518 342 (RTX_UNCHANGING_P (EXP) || MEM_IN_STRUCT_P (EXP) ? (EXP) \
41299f41
TW
343 : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
344
3715a518
RS
345/* Simplify (eq_attr ("alternative") ...)
346 when we are working with a particular alternative. */
347#define SIMPLIFY_ALTERNATIVE(EXP) \
348 if (current_alternative_string \
349 && GET_CODE ((EXP)) == EQ_ATTR \
350 && XSTR ((EXP), 0) == alternative_name) \
351 (EXP) = (XSTR ((EXP), 1) == current_alternative_string \
352 ? true_rtx : false_rtx);
353
41299f41
TW
354/* These are referenced by rtlanal.c and hence need to be defined somewhere.
355 They won't actually be used. */
356
5da077de 357rtx global_rtl[GR_MAX];
e5e809f4 358rtx pic_offset_table_rtx;
41299f41 359
a94ae8f5
KG
360static void attr_hash_add_rtx PARAMS ((int, rtx));
361static void attr_hash_add_string PARAMS ((int, char *));
362static rtx attr_rtx PARAMS ((enum rtx_code, ...));
363static char *attr_printf PARAMS ((int, const char *, ...))
85fda1eb 364 ATTRIBUTE_PRINTF_2;
a94ae8f5
KG
365static char *attr_string PARAMS ((const char *, int));
366static rtx check_attr_test PARAMS ((rtx, int));
367static rtx check_attr_value PARAMS ((rtx, struct attr_desc *));
368static rtx convert_set_attr_alternative PARAMS ((rtx, int, int));
369static rtx convert_set_attr PARAMS ((rtx, int, int));
370static void check_defs PARAMS ((void));
71d9b493 371#if 0
a94ae8f5 372static rtx convert_const_symbol_ref PARAMS ((rtx, struct attr_desc *));
71d9b493 373#endif
a94ae8f5
KG
374static rtx make_canonical PARAMS ((struct attr_desc *, rtx));
375static struct attr_value *get_attr_value PARAMS ((rtx, struct attr_desc *, int));
376static rtx copy_rtx_unchanging PARAMS ((rtx));
377static rtx copy_boolean PARAMS ((rtx));
378static void expand_delays PARAMS ((void));
379static rtx operate_exp PARAMS ((enum operator, rtx, rtx));
380static void expand_units PARAMS ((void));
381static rtx simplify_knowing PARAMS ((rtx, rtx));
382static rtx encode_units_mask PARAMS ((rtx));
383static void fill_attr PARAMS ((struct attr_desc *));
5836dc64 384/* dpx2 compiler chokes if we specify the arg types of the args. */
a94ae8f5
KG
385static rtx substitute_address PARAMS ((rtx, rtx (*) (rtx), rtx (*) (rtx)));
386static void make_length_attrs PARAMS ((void));
387static rtx identity_fn PARAMS ((rtx));
388static rtx zero_fn PARAMS ((rtx));
389static rtx one_fn PARAMS ((rtx));
390static rtx max_fn PARAMS ((rtx));
391static void write_length_unit_log PARAMS ((void));
392static rtx simplify_cond PARAMS ((rtx, int, int));
e9a25f70 393#if 0
a94ae8f5 394static rtx simplify_by_alternatives PARAMS ((rtx, int, int));
e9a25f70 395#endif
a94ae8f5
KG
396static rtx simplify_by_exploding PARAMS ((rtx));
397static int find_and_mark_used_attributes PARAMS ((rtx, rtx *, int *));
398static void unmark_used_attributes PARAMS ((rtx, struct dimension *, int));
399static int add_values_to_cover PARAMS ((struct dimension *));
400static int increment_current_value PARAMS ((struct dimension *, int));
401static rtx test_for_current_value PARAMS ((struct dimension *, int));
402static rtx simplify_with_current_value PARAMS ((rtx, struct dimension *, int));
403static rtx simplify_with_current_value_aux PARAMS ((rtx));
404static void clear_struct_flag PARAMS ((rtx));
405static int count_sub_rtxs PARAMS ((rtx, int));
406static void remove_insn_ent PARAMS ((struct attr_value *, struct insn_ent *));
407static void insert_insn_ent PARAMS ((struct attr_value *, struct insn_ent *));
408static rtx insert_right_side PARAMS ((enum rtx_code, rtx, rtx, int, int));
409static rtx make_alternative_compare PARAMS ((int));
410static int compute_alternative_mask PARAMS ((rtx, enum rtx_code));
411static rtx evaluate_eq_attr PARAMS ((rtx, rtx, int, int));
412static rtx simplify_and_tree PARAMS ((rtx, rtx *, int, int));
413static rtx simplify_or_tree PARAMS ((rtx, rtx *, int, int));
414static rtx simplify_test_exp PARAMS ((rtx, int, int));
415static void optimize_attrs PARAMS ((void));
416static void gen_attr PARAMS ((rtx));
417static int count_alternatives PARAMS ((rtx));
418static int compares_alternatives_p PARAMS ((rtx));
419static int contained_in_p PARAMS ((rtx, rtx));
420static void gen_insn PARAMS ((rtx));
421static void gen_delay PARAMS ((rtx));
422static void gen_unit PARAMS ((rtx));
423static void write_test_expr PARAMS ((rtx, int));
424static int max_attr_value PARAMS ((rtx, int*));
425static int or_attr_value PARAMS ((rtx, int*));
426static void walk_attr_value PARAMS ((rtx));
427static void write_attr_get PARAMS ((struct attr_desc *));
428static rtx eliminate_known_true PARAMS ((rtx, rtx, int, int));
429static void write_attr_set PARAMS ((struct attr_desc *, int, rtx,
85fda1eb
KG
430 const char *, const char *, rtx,
431 int, int));
a94ae8f5 432static void write_attr_case PARAMS ((struct attr_desc *, struct attr_value *,
85fda1eb 433 int, const char *, const char *, int, rtx));
a94ae8f5 434static void write_unit_name PARAMS ((const char *, int, const char *));
3cce094d 435static void write_attr_valueq PARAMS ((struct attr_desc *, const char *));
a94ae8f5
KG
436static void write_attr_value PARAMS ((struct attr_desc *, rtx));
437static void write_upcase PARAMS ((const char *));
438static void write_indent PARAMS ((int));
439static void write_eligible_delay PARAMS ((const char *));
440static void write_function_unit_info PARAMS ((void));
441static void write_complex_function PARAMS ((struct function_unit *, const char *,
85fda1eb 442 const char *));
a94ae8f5
KG
443static int write_expr_attr_cache PARAMS ((rtx, struct attr_desc *));
444static void write_toplevel_expr PARAMS ((rtx));
445static void write_const_num_delay_slots PARAMS ((void));
3cce094d
KG
446static int n_comma_elts PARAMS ((const char *));
447static char *next_comma_elt PARAMS ((const char **));
a94ae8f5
KG
448static struct attr_desc *find_attr PARAMS ((const char *, int));
449static void make_internal_attr PARAMS ((const char *, rtx, int));
450static struct attr_value *find_most_used PARAMS ((struct attr_desc *));
451static rtx find_single_value PARAMS ((struct attr_desc *));
452static rtx make_numeric_value PARAMS ((int));
453static void extend_range PARAMS ((struct range *, int, int));
3cce094d
KG
454static rtx attr_eq PARAMS ((const char *, const char *));
455static const char *attr_numeral PARAMS ((int));
a94ae8f5
KG
456static int attr_equal_p PARAMS ((rtx, rtx));
457static rtx attr_copy_rtx PARAMS ((rtx));
0e9414fd
MM
458
459#define oballoc(size) obstack_alloc (hash_obstack, size)
460
41299f41 461\f
3e7b5313
TW
462/* Hash table for sharing RTL and strings. */
463
464/* Each hash table slot is a bucket containing a chain of these structures.
465 Strings are given negative hash codes; RTL expressions are given positive
466 hash codes. */
467
468struct attr_hash
469{
470 struct attr_hash *next; /* Next structure in the bucket. */
471 int hashcode; /* Hash code of this rtx or string. */
472 union
473 {
474 char *str; /* The string (negative hash codes) */
475 rtx rtl; /* or the RTL recorded here. */
476 } u;
477};
478
479/* Now here is the hash table. When recording an RTL, it is added to
480 the slot whose index is the hash code mod the table size. Note
481 that the hash table is used for several kinds of RTL (see attr_rtx)
482 and for strings. While all these live in the same table, they are
483 completely independent, and the hash code is computed differently
484 for each. */
485
486#define RTL_HASH_SIZE 4093
487struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
488
489/* Here is how primitive or already-shared RTL's hash
490 codes are made. */
2e0dd623 491#define RTL_HASH(RTL) ((long) (RTL) & 0777777)
3e7b5313
TW
492
493/* Add an entry to the hash table for RTL with hash code HASHCODE. */
494
495static void
496attr_hash_add_rtx (hashcode, rtl)
497 int hashcode;
498 rtx rtl;
499{
500 register struct attr_hash *h;
501
7339c88d
RS
502 h = (struct attr_hash *) obstack_alloc (hash_obstack,
503 sizeof (struct attr_hash));
3e7b5313
TW
504 h->hashcode = hashcode;
505 h->u.rtl = rtl;
506 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
507 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
508}
509
510/* Add an entry to the hash table for STRING with hash code HASHCODE. */
511
512static void
513attr_hash_add_string (hashcode, str)
514 int hashcode;
515 char *str;
516{
517 register struct attr_hash *h;
518
7339c88d
RS
519 h = (struct attr_hash *) obstack_alloc (hash_obstack,
520 sizeof (struct attr_hash));
3e7b5313
TW
521 h->hashcode = -hashcode;
522 h->u.str = str;
523 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
524 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
525}
526
81fd4c6e
RS
527/* Generate an RTL expression, but avoid duplicates.
528 Set the RTX_INTEGRATED_P flag for these permanent objects.
529
530 In some cases we cannot uniquify; then we return an ordinary
531 impermanent rtx with RTX_INTEGRATED_P clear.
532
533 Args are like gen_rtx, but without the mode:
3e7b5313
TW
534
535 rtx attr_rtx (code, [element1, ..., elementn]) */
536
537/*VARARGS1*/
538static rtx
a94ae8f5 539attr_rtx VPARAMS ((enum rtx_code code, ...))
3e7b5313 540{
5148a72b 541#ifndef ANSI_PROTOTYPES
3e7b5313 542 enum rtx_code code;
9e1b6503
RK
543#endif
544 va_list p;
3e7b5313 545 register int i; /* Array indices... */
a544cfd2
KG
546 register const char *fmt; /* Current rtx's format... */
547 register rtx rt_val = NULL_RTX;/* RTX to return to caller... */
3e7b5313
TW
548 int hashcode;
549 register struct attr_hash *h;
7339c88d 550 struct obstack *old_obstack = rtl_obstack;
3e7b5313 551
9e1b6503
RK
552 VA_START (p, code);
553
5148a72b 554#ifndef ANSI_PROTOTYPES
3e7b5313 555 code = va_arg (p, enum rtx_code);
9e1b6503 556#endif
3e7b5313
TW
557
558 /* For each of several cases, search the hash table for an existing entry.
559 Use that entry if one is found; otherwise create a new RTL and add it
560 to the table. */
561
562 if (GET_RTX_CLASS (code) == '1')
563 {
564 rtx arg0 = va_arg (p, rtx);
565
81fd4c6e
RS
566 /* A permanent object cannot point to impermanent ones. */
567 if (! RTX_INTEGRATED_P (arg0))
568 {
569 rt_val = rtx_alloc (code);
570 XEXP (rt_val, 0) = arg0;
571 va_end (p);
572 return rt_val;
573 }
574
d98c1e33 575 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
3e7b5313
TW
576 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
577 if (h->hashcode == hashcode
578 && GET_CODE (h->u.rtl) == code
579 && XEXP (h->u.rtl, 0) == arg0)
580 goto found;
581
582 if (h == 0)
583 {
7339c88d 584 rtl_obstack = hash_obstack;
3e7b5313
TW
585 rt_val = rtx_alloc (code);
586 XEXP (rt_val, 0) = arg0;
587 }
588 }
589 else if (GET_RTX_CLASS (code) == 'c'
590 || GET_RTX_CLASS (code) == '2'
591 || GET_RTX_CLASS (code) == '<')
592 {
593 rtx arg0 = va_arg (p, rtx);
594 rtx arg1 = va_arg (p, rtx);
595
81fd4c6e
RS
596 /* A permanent object cannot point to impermanent ones. */
597 if (! RTX_INTEGRATED_P (arg0) || ! RTX_INTEGRATED_P (arg1))
598 {
599 rt_val = rtx_alloc (code);
600 XEXP (rt_val, 0) = arg0;
601 XEXP (rt_val, 1) = arg1;
602 va_end (p);
603 return rt_val;
604 }
605
d98c1e33 606 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
3e7b5313
TW
607 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
608 if (h->hashcode == hashcode
609 && GET_CODE (h->u.rtl) == code
610 && XEXP (h->u.rtl, 0) == arg0
611 && XEXP (h->u.rtl, 1) == arg1)
612 goto found;
613
614 if (h == 0)
615 {
7339c88d 616 rtl_obstack = hash_obstack;
3e7b5313
TW
617 rt_val = rtx_alloc (code);
618 XEXP (rt_val, 0) = arg0;
619 XEXP (rt_val, 1) = arg1;
620 }
621 }
622 else if (GET_RTX_LENGTH (code) == 1
623 && GET_RTX_FORMAT (code)[0] == 's')
624 {
770ae6cc 625 char *arg0 = va_arg (p, char *);
3e7b5313 626
81fd4c6e
RS
627 if (code == SYMBOL_REF)
628 arg0 = attr_string (arg0, strlen (arg0));
629
d98c1e33 630 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
3e7b5313
TW
631 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
632 if (h->hashcode == hashcode
633 && GET_CODE (h->u.rtl) == code
634 && XSTR (h->u.rtl, 0) == arg0)
635 goto found;
636
637 if (h == 0)
638 {
7339c88d 639 rtl_obstack = hash_obstack;
3e7b5313
TW
640 rt_val = rtx_alloc (code);
641 XSTR (rt_val, 0) = arg0;
642 }
643 }
644 else if (GET_RTX_LENGTH (code) == 2
645 && GET_RTX_FORMAT (code)[0] == 's'
646 && GET_RTX_FORMAT (code)[1] == 's')
647 {
81fd4c6e
RS
648 char *arg0 = va_arg (p, char *);
649 char *arg1 = va_arg (p, char *);
3e7b5313 650
d98c1e33 651 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
3e7b5313
TW
652 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
653 if (h->hashcode == hashcode
654 && GET_CODE (h->u.rtl) == code
655 && XSTR (h->u.rtl, 0) == arg0
656 && XSTR (h->u.rtl, 1) == arg1)
657 goto found;
658
659 if (h == 0)
660 {
7339c88d 661 rtl_obstack = hash_obstack;
3e7b5313
TW
662 rt_val = rtx_alloc (code);
663 XSTR (rt_val, 0) = arg0;
664 XSTR (rt_val, 1) = arg1;
665 }
666 }
81fd4c6e
RS
667 else if (code == CONST_INT)
668 {
3d678dca 669 HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
81fd4c6e 670 if (arg0 == 0)
f0305a2b
KG
671 {
672 va_end (p);
673 return false_rtx;
674 }
81fd4c6e 675 if (arg0 == 1)
f0305a2b
KG
676 {
677 va_end (p);
678 return true_rtx;
679 }
81fd4c6e
RS
680 goto nohash;
681 }
3e7b5313
TW
682 else
683 {
81fd4c6e 684 nohash:
3e7b5313
TW
685 rt_val = rtx_alloc (code); /* Allocate the storage space. */
686
687 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
688 for (i = 0; i < GET_RTX_LENGTH (code); i++)
689 {
690 switch (*fmt++)
691 {
692 case '0': /* Unused field. */
693 break;
694
695 case 'i': /* An integer? */
696 XINT (rt_val, i) = va_arg (p, int);
697 break;
698
3d678dca
RS
699 case 'w': /* A wide integer? */
700 XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
701 break;
702
3e7b5313
TW
703 case 's': /* A string? */
704 XSTR (rt_val, i) = va_arg (p, char *);
705 break;
706
707 case 'e': /* An expression? */
708 case 'u': /* An insn? Same except when printing. */
709 XEXP (rt_val, i) = va_arg (p, rtx);
710 break;
711
712 case 'E': /* An RTX vector? */
713 XVEC (rt_val, i) = va_arg (p, rtvec);
714 break;
715
716 default:
717 abort();
718 }
719 }
720 va_end (p);
721 return rt_val;
722 }
723
7339c88d 724 rtl_obstack = old_obstack;
3e7b5313
TW
725 va_end (p);
726 attr_hash_add_rtx (hashcode, rt_val);
81fd4c6e 727 RTX_INTEGRATED_P (rt_val) = 1;
3e7b5313
TW
728 return rt_val;
729
730 found:
731 va_end (p);
732 return h->u.rtl;
733}
734
735/* Create a new string printed with the printf line arguments into a space
736 of at most LEN bytes:
737
738 rtx attr_printf (len, format, [arg1, ..., argn]) */
739
740/*VARARGS2*/
741static char *
a94ae8f5 742attr_printf VPARAMS ((register int len, const char *fmt, ...))
3e7b5313 743{
5148a72b 744#ifndef ANSI_PROTOTYPES
3e7b5313 745 register int len;
85fda1eb 746 const char *fmt;
9e1b6503
RK
747#endif
748 va_list p;
3e7b5313
TW
749 register char *str;
750
9e1b6503
RK
751 VA_START (p, fmt);
752
5148a72b 753#ifndef ANSI_PROTOTYPES
3e7b5313 754 len = va_arg (p, int);
85fda1eb 755 fmt = va_arg (p, const char *);
9e1b6503
RK
756#endif
757
758 /* Print the string into a temporary location. */
3e7b5313 759 str = (char *) alloca (len);
3e7b5313
TW
760 vsprintf (str, fmt, p);
761 va_end (p);
762
763 return attr_string (str, strlen (str));
764}
765
69277eec 766static rtx
81fd4c6e 767attr_eq (name, value)
3cce094d 768 const char *name, *value;
81fd4c6e
RS
769{
770 return attr_rtx (EQ_ATTR, attr_string (name, strlen (name)),
771 attr_string (value, strlen (value)));
772}
773
3cce094d 774static const char *
81fd4c6e
RS
775attr_numeral (n)
776 int n;
777{
778 return XSTR (make_numeric_value (n), 0);
779}
780
3e7b5313
TW
781/* Return a permanent (possibly shared) copy of a string STR (not assumed
782 to be null terminated) with LEN bytes. */
783
784static char *
785attr_string (str, len)
85fda1eb 786 const char *str;
3e7b5313
TW
787 int len;
788{
789 register struct attr_hash *h;
790 int hashcode;
791 int i;
792 register char *new_str;
793
794 /* Compute the hash code. */
795 hashcode = (len + 1) * 613 + (unsigned)str[0];
796 for (i = 1; i <= len; i += 2)
797 hashcode = ((hashcode * 613) + (unsigned)str[i]);
798 if (hashcode < 0)
799 hashcode = -hashcode;
800
801 /* Search the table for the string. */
802 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
81fd4c6e 803 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
d45cf215 804 && !strncmp (h->u.str, str, len))
3e7b5313
TW
805 return h->u.str; /* <-- return if found. */
806
807 /* Not found; create a permanent copy and add it to the hash table. */
b31a5831 808 new_str = (char *) obstack_alloc (hash_obstack, len + 1);
3e7b5313
TW
809 bcopy (str, new_str, len);
810 new_str[len] = '\0';
811 attr_hash_add_string (hashcode, new_str);
812
813 return new_str; /* Return the new string. */
814}
81fd4c6e
RS
815
816/* Check two rtx's for equality of contents,
817 taking advantage of the fact that if both are hashed
818 then they can't be equal unless they are the same object. */
819
69277eec 820static int
81fd4c6e
RS
821attr_equal_p (x, y)
822 rtx x, y;
823{
824 return (x == y || (! (RTX_INTEGRATED_P (x) && RTX_INTEGRATED_P (y))
825 && rtx_equal_p (x, y)));
826}
827\f
828/* Copy an attribute value expression,
829 descending to all depths, but not copying any
830 permanent hashed subexpressions. */
831
69277eec 832static rtx
81fd4c6e
RS
833attr_copy_rtx (orig)
834 register rtx orig;
835{
836 register rtx copy;
837 register int i, j;
838 register RTX_CODE code;
6f7d635c 839 register const char *format_ptr;
81fd4c6e
RS
840
841 /* No need to copy a permanent object. */
842 if (RTX_INTEGRATED_P (orig))
843 return orig;
844
845 code = GET_CODE (orig);
846
847 switch (code)
848 {
849 case REG:
850 case QUEUED:
851 case CONST_INT:
852 case CONST_DOUBLE:
853 case SYMBOL_REF:
854 case CODE_LABEL:
855 case PC:
856 case CC0:
857 return orig;
e9a25f70
JL
858
859 default:
860 break;
81fd4c6e
RS
861 }
862
863 copy = rtx_alloc (code);
864 PUT_MODE (copy, GET_MODE (orig));
865 copy->in_struct = orig->in_struct;
866 copy->volatil = orig->volatil;
867 copy->unchanging = orig->unchanging;
868 copy->integrated = orig->integrated;
869
870 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
871
872 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
873 {
874 switch (*format_ptr++)
875 {
876 case 'e':
877 XEXP (copy, i) = XEXP (orig, i);
878 if (XEXP (orig, i) != NULL)
879 XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
880 break;
881
882 case 'E':
883 case 'V':
884 XVEC (copy, i) = XVEC (orig, i);
885 if (XVEC (orig, i) != NULL)
886 {
887 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
888 for (j = 0; j < XVECLEN (copy, i); j++)
889 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
890 }
891 break;
892
3d678dca
RS
893 case 'n':
894 case 'i':
81fd4c6e
RS
895 XINT (copy, i) = XINT (orig, i);
896 break;
3d678dca
RS
897
898 case 'w':
899 XWINT (copy, i) = XWINT (orig, i);
900 break;
901
902 case 's':
903 case 'S':
904 XSTR (copy, i) = XSTR (orig, i);
905 break;
906
907 default:
908 abort ();
81fd4c6e
RS
909 }
910 }
911 return copy;
912}
3e7b5313 913\f
41299f41 914/* Given a test expression for an attribute, ensure it is validly formed.
3e7b5313
TW
915 IS_CONST indicates whether the expression is constant for each compiler
916 run (a constant expression may not test any particular insn).
917
41299f41
TW
918 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
919 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
920 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
921
922 Update the string address in EQ_ATTR expression to be the same used
923 in the attribute (or `alternative_name') to speed up subsequent
924 `find_attr' calls and eliminate most `strcmp' calls.
925
926 Return the new expression, if any. */
927
928static rtx
3e7b5313 929check_attr_test (exp, is_const)
41299f41 930 rtx exp;
3e7b5313 931 int is_const;
41299f41
TW
932{
933 struct attr_desc *attr;
934 struct attr_value *av;
3cce094d 935 const char *name_ptr, *p;
41299f41
TW
936 rtx orexp, newexp;
937
938 switch (GET_CODE (exp))
939 {
940 case EQ_ATTR:
941 /* Handle negation test. */
942 if (XSTR (exp, 1)[0] == '!')
3e7b5313 943 return check_attr_test (attr_rtx (NOT,
81fd4c6e
RS
944 attr_eq (XSTR (exp, 0),
945 &XSTR (exp, 1)[1])),
3e7b5313 946 is_const);
41299f41
TW
947
948 else if (n_comma_elts (XSTR (exp, 1)) == 1)
949 {
c114787a 950 attr = find_attr (XSTR (exp, 0), 0);
41299f41
TW
951 if (attr == NULL)
952 {
953 if (! strcmp (XSTR (exp, 0), "alternative"))
954 {
955 XSTR (exp, 0) = alternative_name;
956 /* This can't be simplified any further. */
957 RTX_UNCHANGING_P (exp) = 1;
958 return exp;
959 }
7339c88d 960 else
487a6e06 961 fatal ("Unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
41299f41
TW
962 }
963
3e7b5313
TW
964 if (is_const && ! attr->is_const)
965 fatal ("Constant expression uses insn attribute `%s' in EQ_ATTR",
487a6e06 966 XSTR (exp, 0));
3e7b5313 967
81fd4c6e
RS
968 /* Copy this just to make it permanent,
969 so expressions using it can be permanent too. */
970 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
41299f41 971
f72aed24 972 /* It shouldn't be possible to simplify the value given to a
b31a5831
RS
973 constant attribute, so don't expand this until it's time to
974 write the test expression. */
975 if (attr->is_const)
976 RTX_UNCHANGING_P (exp) = 1;
977
41299f41
TW
978 if (attr->is_numeric)
979 {
980 for (p = XSTR (exp, 1); *p; p++)
981 if (*p < '0' || *p > '9')
982 fatal ("Attribute `%s' takes only numeric values",
487a6e06 983 XSTR (exp, 0));
41299f41
TW
984 }
985 else
986 {
987 for (av = attr->first_value; av; av = av->next)
988 if (GET_CODE (av->value) == CONST_STRING
989 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
990 break;
991
992 if (av == NULL)
993 fatal ("Unknown value `%s' for `%s' attribute",
487a6e06 994 XSTR (exp, 1), XSTR (exp, 0));
41299f41
TW
995 }
996 }
997 else
998 {
999 /* Make an IOR tree of the possible values. */
1000 orexp = false_rtx;
1001 name_ptr = XSTR (exp, 1);
1002 while ((p = next_comma_elt (&name_ptr)) != NULL)
1003 {
81fd4c6e 1004 newexp = attr_eq (XSTR (exp, 0), p);
f75d38a7 1005 orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
41299f41
TW
1006 }
1007
3e7b5313 1008 return check_attr_test (orexp, is_const);
41299f41
TW
1009 }
1010 break;
1011
0b0316dc
JL
1012 case ATTR_FLAG:
1013 break;
1014
41299f41
TW
1015 case CONST_INT:
1016 /* Either TRUE or FALSE. */
3d678dca 1017 if (XWINT (exp, 0))
41299f41
TW
1018 return true_rtx;
1019 else
1020 return false_rtx;
1021
1022 case IOR:
1023 case AND:
3e7b5313
TW
1024 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const);
1025 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const);
41299f41
TW
1026 break;
1027
1028 case NOT:
3e7b5313 1029 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const);
41299f41
TW
1030 break;
1031
8450a694 1032 case MATCH_INSN:
41299f41 1033 case MATCH_OPERAND:
3e7b5313
TW
1034 if (is_const)
1035 fatal ("RTL operator \"%s\" not valid in constant attribute test",
8450a694 1036 GET_RTX_NAME (GET_CODE (exp)));
81fd4c6e
RS
1037 /* These cases can't be simplified. */
1038 RTX_UNCHANGING_P (exp) = 1;
1039 break;
71d9b493 1040
41299f41
TW
1041 case LE: case LT: case GT: case GE:
1042 case LEU: case LTU: case GTU: case GEU:
1043 case NE: case EQ:
81fd4c6e
RS
1044 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
1045 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
1046 exp = attr_rtx (GET_CODE (exp),
1047 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
1048 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
41299f41
TW
1049 /* These cases can't be simplified. */
1050 RTX_UNCHANGING_P (exp) = 1;
1051 break;
1052
3e7b5313
TW
1053 case SYMBOL_REF:
1054 if (is_const)
1055 {
1056 /* These cases are valid for constant attributes, but can't be
1057 simplified. */
81fd4c6e 1058 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
3e7b5313
TW
1059 RTX_UNCHANGING_P (exp) = 1;
1060 break;
1061 }
41299f41
TW
1062 default:
1063 fatal ("RTL operator \"%s\" not valid in attribute test",
1064 GET_RTX_NAME (GET_CODE (exp)));
1065 }
1066
1067 return exp;
1068}
1069\f
1070/* Given an expression, ensure that it is validly formed and that all named
1071 attribute values are valid for the given attribute. Issue a fatal error
81fd4c6e 1072 if not. If no attribute is specified, assume a numeric attribute.
41299f41 1073
81fd4c6e
RS
1074 Return a perhaps modified replacement expression for the value. */
1075
1076static rtx
41299f41
TW
1077check_attr_value (exp, attr)
1078 rtx exp;
1079 struct attr_desc *attr;
1080{
1081 struct attr_value *av;
3cce094d 1082 const char *p;
41299f41
TW
1083 int i;
1084
1085 switch (GET_CODE (exp))
1086 {
1087 case CONST_INT:
1088 if (attr && ! attr->is_numeric)
1089 fatal ("CONST_INT not valid for non-numeric `%s' attribute",
1090 attr->name);
1091
7ee37ba4 1092 if (INTVAL (exp) < 0 && ! attr->negative_ok)
41299f41
TW
1093 fatal ("Negative numeric value specified for `%s' attribute",
1094 attr->name);
1095
1096 break;
1097
1098 case CONST_STRING:
1099 if (! strcmp (XSTR (exp, 0), "*"))
1100 break;
1101
1102 if (attr == 0 || attr->is_numeric)
1103 {
72f1215c
TW
1104 p = XSTR (exp, 0);
1105 if (attr && attr->negative_ok && *p == '-')
1106 p++;
1107 for (; *p; p++)
41299f41
TW
1108 if (*p > '9' || *p < '0')
1109 fatal ("Non-numeric value for numeric `%s' attribute",
e7c33f54 1110 attr ? attr->name : "internal");
41299f41
TW
1111 break;
1112 }
1113
1114 for (av = attr->first_value; av; av = av->next)
1115 if (GET_CODE (av->value) == CONST_STRING
1116 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
1117 break;
1118
1119 if (av == NULL)
1120 fatal ("Unknown value `%s' for `%s' attribute",
e7c33f54 1121 XSTR (exp, 0), attr ? attr->name : "internal");
41299f41 1122
81fd4c6e 1123 break;
41299f41
TW
1124
1125 case IF_THEN_ELSE:
3e7b5313
TW
1126 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
1127 attr ? attr->is_const : 0);
81fd4c6e
RS
1128 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1129 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
1130 break;
41299f41 1131
7ee37ba4
RH
1132 case PLUS:
1133 case MINUS:
1134 case MULT:
1135 case DIV:
1136 case MOD:
1137 if (attr && !attr->is_numeric)
1138 fatal ("Invalid operation `%s' for non-numeric attribute value",
1139 GET_RTX_NAME (GET_CODE (exp)));
1140 /* FALLTHRU */
1141
71d9b493
RH
1142 case IOR:
1143 case AND:
1144 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1145 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1146 break;
1147
1148 case FFS:
1149 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
1150 break;
1151
41299f41
TW
1152 case COND:
1153 if (XVECLEN (exp, 0) % 2 != 0)
1154 fatal ("First operand of COND must have even length");
1155
1156 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1157 {
3e7b5313
TW
1158 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
1159 attr ? attr->is_const : 0);
81fd4c6e
RS
1160 XVECEXP (exp, 0, i + 1)
1161 = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
41299f41
TW
1162 }
1163
81fd4c6e
RS
1164 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1165 break;
41299f41 1166
7ee37ba4
RH
1167 case ATTR:
1168 {
1169 struct attr_desc *attr2 = find_attr (XSTR (exp, 0), 0);
1170 if (attr2 == NULL)
1171 fatal ("Unknown attribute `%s' in ATTR", XSTR (exp, 0));
1172 else if ((attr && attr->is_const) && ! attr2->is_const)
1173 fatal ("Non-constant attribute `%s' referenced from `%s'",
1174 XSTR (exp, 0), attr->name);
1175 else if (attr
1176 && (attr->is_numeric != attr2->is_numeric
1177 || (! attr->negative_ok && attr2->negative_ok)))
1178 fatal ("Numeric attribute mismatch calling `%s' from `%s'",
1179 XSTR (exp, 0), attr->name);
1180 }
1181 break;
1182
3e7b5313 1183 case SYMBOL_REF:
7ee37ba4
RH
1184 /* A constant SYMBOL_REF is valid as a constant attribute test and
1185 is expanded later by make_canonical into a COND. In a non-constant
1186 attribute test, it is left be. */
1187 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
3e7b5313 1188
41299f41 1189 default:
9916c524 1190 fatal ("Invalid operation `%s' for attribute value",
41299f41
TW
1191 GET_RTX_NAME (GET_CODE (exp)));
1192 }
81fd4c6e
RS
1193
1194 return exp;
41299f41
TW
1195}
1196\f
1197/* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1198 It becomes a COND with each test being (eq_attr "alternative "n") */
1199
1200static rtx
e51712db 1201convert_set_attr_alternative (exp, num_alt, insn_index)
41299f41
TW
1202 rtx exp;
1203 int num_alt;
e51712db 1204 int insn_index;
41299f41 1205{
41299f41
TW
1206 rtx condexp;
1207 int i;
1208
1209 if (XVECLEN (exp, 1) != num_alt)
1210 fatal ("Bad number of entries in SET_ATTR_ALTERNATIVE for insn %d",
1211 insn_index);
1212
1213 /* Make a COND with all tests but the last. Select the last value via the
1214 default. */
1215 condexp = rtx_alloc (COND);
1216 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1217
1218 for (i = 0; i < num_alt - 1; i++)
1219 {
3cce094d 1220 const char *p;
81fd4c6e 1221 p = attr_numeral (i);
3e7b5313 1222
81fd4c6e
RS
1223 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1224#if 0
3e7b5313 1225 /* Sharing this EQ_ATTR rtl causes trouble. */
41299f41
TW
1226 XVECEXP (condexp, 0, 2 * i) = rtx_alloc (EQ_ATTR);
1227 XSTR (XVECEXP (condexp, 0, 2 * i), 0) = alternative_name;
3e7b5313 1228 XSTR (XVECEXP (condexp, 0, 2 * i), 1) = p;
81fd4c6e 1229#endif
41299f41
TW
1230 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1231 }
1232
1233 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1234
3e7b5313 1235 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
41299f41
TW
1236}
1237\f
1238/* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1239 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1240
1241static rtx
e51712db 1242convert_set_attr (exp, num_alt, insn_index)
41299f41
TW
1243 rtx exp;
1244 int num_alt;
e51712db 1245 int insn_index;
41299f41
TW
1246{
1247 rtx newexp;
3cce094d 1248 const char *name_ptr;
41299f41
TW
1249 char *p;
1250 int n;
1251
1252 /* See how many alternative specified. */
1253 n = n_comma_elts (XSTR (exp, 1));
1254 if (n == 1)
3e7b5313
TW
1255 return attr_rtx (SET,
1256 attr_rtx (ATTR, XSTR (exp, 0)),
1257 attr_rtx (CONST_STRING, XSTR (exp, 1)));
41299f41
TW
1258
1259 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1260 XSTR (newexp, 0) = XSTR (exp, 0);
1261 XVEC (newexp, 1) = rtvec_alloc (n);
1262
1263 /* Process each comma-separated name. */
1264 name_ptr = XSTR (exp, 1);
1265 n = 0;
1266 while ((p = next_comma_elt (&name_ptr)) != NULL)
3e7b5313 1267 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
41299f41 1268
e51712db 1269 return convert_set_attr_alternative (newexp, num_alt, insn_index);
41299f41
TW
1270}
1271\f
1272/* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1273 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
0f41302f 1274 expressions. */
41299f41
TW
1275
1276static void
1277check_defs ()
1278{
1279 struct insn_def *id;
1280 struct attr_desc *attr;
1281 int i;
1282 rtx value;
1283
1284 for (id = defs; id; id = id->next)
1285 {
1286 if (XVEC (id->def, id->vec_idx) == NULL)
1287 continue;
1288
1289 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1290 {
1291 value = XVECEXP (id->def, id->vec_idx, i);
1292 switch (GET_CODE (value))
1293 {
1294 case SET:
1295 if (GET_CODE (XEXP (value, 0)) != ATTR)
1296 fatal ("Bad attribute set in pattern %d", id->insn_index);
1297 break;
1298
1299 case SET_ATTR_ALTERNATIVE:
1300 value = convert_set_attr_alternative (value,
1301 id->num_alternatives,
41299f41
TW
1302 id->insn_index);
1303 break;
1304
1305 case SET_ATTR:
1306 value = convert_set_attr (value, id->num_alternatives,
e51712db 1307 id->insn_index);
41299f41
TW
1308 break;
1309
1310 default:
1311 fatal ("Invalid attribute code `%s' for pattern %d",
1312 GET_RTX_NAME (GET_CODE (value)), id->insn_index);
1313 }
1314
1315 if ((attr = find_attr (XSTR (XEXP (value, 0), 0), 0)) == NULL)
1316 fatal ("Unknown attribute `%s' for pattern number %d",
1317 XSTR (XEXP (value, 0), 0), id->insn_index);
1318
1319 XVECEXP (id->def, id->vec_idx, i) = value;
81fd4c6e 1320 XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
41299f41
TW
1321 }
1322 }
1323}
1324\f
71d9b493 1325#if 0
3e7b5313
TW
1326/* Given a constant SYMBOL_REF expression, convert to a COND that
1327 explicitly tests each enumerated value. */
1328
1329static rtx
1330convert_const_symbol_ref (exp, attr)
1331 rtx exp;
1332 struct attr_desc *attr;
1333{
1334 rtx condexp;
1335 struct attr_value *av;
1336 int i;
1337 int num_alt = 0;
1338
1339 for (av = attr->first_value; av; av = av->next)
1340 num_alt++;
1341
1342 /* Make a COND with all tests but the last, and in the original order.
1343 Select the last value via the default. Note that the attr values
1344 are constructed in reverse order. */
1345
1346 condexp = rtx_alloc (COND);
1347 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1348 av = attr->first_value;
1349 XEXP (condexp, 1) = av->value;
1350
1351 for (i = num_alt - 2; av = av->next, i >= 0; i--)
1352 {
81fd4c6e 1353 char *p, *string;
3e7b5313
TW
1354 rtx value;
1355
0e9414fd 1356 string = p = (char *) oballoc (2
81fd4c6e
RS
1357 + strlen (attr->name)
1358 + strlen (XSTR (av->value, 0)));
3e7b5313
TW
1359 strcpy (p, attr->name);
1360 strcat (p, "_");
1361 strcat (p, XSTR (av->value, 0));
1362 for (; *p != '\0'; p++)
92a438d1 1363 *p = TOUPPER (*p);
3e7b5313 1364
81fd4c6e
RS
1365 value = attr_rtx (SYMBOL_REF, string);
1366 RTX_UNCHANGING_P (value) = 1;
1367
3715a518 1368 XVECEXP (condexp, 0, 2 * i) = attr_rtx (EQ, exp, value);
81fd4c6e 1369
3e7b5313
TW
1370 XVECEXP (condexp, 0, 2 * i + 1) = av->value;
1371 }
1372
1373 return condexp;
1374}
71d9b493 1375#endif
3e7b5313 1376\f
41299f41
TW
1377/* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1378 expressions by converting them into a COND. This removes cases from this
1379 program. Also, replace an attribute value of "*" with the default attribute
1380 value. */
1381
1382static rtx
1383make_canonical (attr, exp)
1384 struct attr_desc *attr;
1385 rtx exp;
1386{
1387 int i;
1388 rtx newexp;
1389
1390 switch (GET_CODE (exp))
1391 {
1392 case CONST_INT:
1393 exp = make_numeric_value (INTVAL (exp));
1394 break;
1395
1396 case CONST_STRING:
1397 if (! strcmp (XSTR (exp, 0), "*"))
1398 {
1399 if (attr == 0 || attr->default_val == 0)
1400 fatal ("(attr_value \"*\") used in invalid context.");
1401 exp = attr->default_val->value;
1402 }
1403
1404 break;
1405
3e7b5313
TW
1406 case SYMBOL_REF:
1407 if (!attr->is_const || RTX_UNCHANGING_P (exp))
1408 break;
3715a518
RS
1409 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1410 This makes the COND something that won't be considered an arbitrary
1411 expression by walk_attr_value. */
1412 RTX_UNCHANGING_P (exp) = 1;
71d9b493
RH
1413#if 0
1414 /* ??? Why do we do this? With attribute values { A B C D E }, this
1415 tends to generate (!(x==A) && !(x==B) && !(x==C) && !(x==D)) rather
1416 than (x==E). */
3e7b5313 1417 exp = convert_const_symbol_ref (exp, attr);
052aaaef 1418 RTX_UNCHANGING_P (exp) = 1;
81fd4c6e 1419 exp = check_attr_value (exp, attr);
3e7b5313 1420 /* Goto COND case since this is now a COND. Note that while the
956d6950 1421 new expression is rescanned, all symbol_ref notes are marked as
3e7b5313
TW
1422 unchanging. */
1423 goto cond;
71d9b493
RH
1424#else
1425 exp = check_attr_value (exp, attr);
1426 break;
1427#endif
3e7b5313 1428
41299f41
TW
1429 case IF_THEN_ELSE:
1430 newexp = rtx_alloc (COND);
1431 XVEC (newexp, 0) = rtvec_alloc (2);
1432 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1433 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1434
1435 XEXP (newexp, 1) = XEXP (exp, 2);
1436
1437 exp = newexp;
1438 /* Fall through to COND case since this is now a COND. */
1439
1440 case COND:
7339c88d
RS
1441 {
1442 int allsame = 1;
1443 rtx defval;
41299f41 1444
0f41302f 1445 /* First, check for degenerate COND. */
7339c88d
RS
1446 if (XVECLEN (exp, 0) == 0)
1447 return make_canonical (attr, XEXP (exp, 1));
1448 defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
41299f41 1449
7339c88d
RS
1450 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1451 {
81fd4c6e 1452 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
7339c88d
RS
1453 XVECEXP (exp, 0, i + 1)
1454 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1455 if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1456 allsame = 0;
1457 }
1458 if (allsame)
1459 return defval;
7339c88d 1460 }
e9a25f70
JL
1461 break;
1462
1463 default:
1464 break;
41299f41
TW
1465 }
1466
1467 return exp;
1468}
81fd4c6e
RS
1469
1470static rtx
1471copy_boolean (exp)
1472 rtx exp;
1473{
1474 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1475 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1476 copy_boolean (XEXP (exp, 1)));
1477 return exp;
1478}
41299f41
TW
1479\f
1480/* Given a value and an attribute description, return a `struct attr_value *'
1481 that represents that value. This is either an existing structure, if the
1482 value has been previously encountered, or a newly-created structure.
1483
1484 `insn_code' is the code of an insn whose attribute has the specified
1485 value (-2 if not processing an insn). We ensure that all insns for
1486 a given value have the same number of alternatives if the value checks
1487 alternatives. */
1488
1489static struct attr_value *
1490get_attr_value (value, attr, insn_code)
1491 rtx value;
1492 struct attr_desc *attr;
1493 int insn_code;
1494{
1495 struct attr_value *av;
1496 int num_alt = 0;
1497
1498 value = make_canonical (attr, value);
1499 if (compares_alternatives_p (value))
1500 {
1501 if (insn_code < 0 || insn_alternatives == NULL)
1502 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1503 else
1504 num_alt = insn_alternatives[insn_code];
1505 }
1506
1507 for (av = attr->first_value; av; av = av->next)
1508 if (rtx_equal_p (value, av->value)
1509 && (num_alt == 0 || av->first_insn == NULL
1510 || insn_alternatives[av->first_insn->insn_code]))
1511 return av;
1512
0e9414fd 1513 av = (struct attr_value *) oballoc (sizeof (struct attr_value));
41299f41
TW
1514 av->value = value;
1515 av->next = attr->first_value;
1516 attr->first_value = av;
1517 av->first_insn = NULL;
1518 av->num_insns = 0;
1519 av->has_asm_insn = 0;
1520
1521 return av;
1522}
1523\f
1524/* After all DEFINE_DELAYs have been read in, create internal attributes
1525 to generate the required routines.
1526
1527 First, we compute the number of delay slots for each insn (as a COND of
1528 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1529 delay type is specified, we compute a similar function giving the
1530 DEFINE_DELAY ordinal for each insn.
1531
1532 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1533 tells whether a given insn can be in that delay slot.
1534
6dc42e49 1535 Normal attribute filling and optimization expands these to contain the
41299f41
TW
1536 information needed to handle delay slots. */
1537
1538static void
1539expand_delays ()
1540{
1541 struct delay_desc *delay;
1542 rtx condexp;
1543 rtx newexp;
1544 int i;
1545 char *p;
1546
1547 /* First, generate data for `num_delay_slots' function. */
1548
1549 condexp = rtx_alloc (COND);
1550 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1551 XEXP (condexp, 1) = make_numeric_value (0);
1552
1553 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1554 {
1555 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1556 XVECEXP (condexp, 0, i + 1)
1557 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1558 }
1559
1560 make_internal_attr ("*num_delay_slots", condexp, 0);
1561
1562 /* If more than one delay type, do the same for computing the delay type. */
1563 if (num_delays > 1)
1564 {
1565 condexp = rtx_alloc (COND);
1566 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1567 XEXP (condexp, 1) = make_numeric_value (0);
1568
1569 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1570 {
1571 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1572 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1573 }
1574
1575 make_internal_attr ("*delay_type", condexp, 1);
1576 }
1577
6dc42e49
RS
1578 /* For each delay possibility and delay slot, compute an eligibility
1579 attribute for non-annulled insns and for each type of annulled (annul
41299f41
TW
1580 if true and annul if false). */
1581 for (delay = delays; delay; delay = delay->next)
1582 {
1583 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1584 {
41299f41
TW
1585 condexp = XVECEXP (delay->def, 1, i);
1586 if (condexp == 0) condexp = false_rtx;
3e7b5313
TW
1587 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1588 make_numeric_value (1), make_numeric_value (0));
41299f41 1589
0e9414fd
MM
1590 p = attr_printf (sizeof ("*delay__") + MAX_DIGITS*2, "*delay_%d_%d",
1591 delay->num, i / 3);
41299f41
TW
1592 make_internal_attr (p, newexp, 1);
1593
1594 if (have_annul_true)
1595 {
41299f41
TW
1596 condexp = XVECEXP (delay->def, 1, i + 1);
1597 if (condexp == 0) condexp = false_rtx;
3e7b5313
TW
1598 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1599 make_numeric_value (1),
1600 make_numeric_value (0));
0e9414fd
MM
1601 p = attr_printf (sizeof ("*annul_true__") + MAX_DIGITS*2,
1602 "*annul_true_%d_%d", delay->num, i / 3);
41299f41
TW
1603 make_internal_attr (p, newexp, 1);
1604 }
1605
1606 if (have_annul_false)
1607 {
41299f41
TW
1608 condexp = XVECEXP (delay->def, 1, i + 2);
1609 if (condexp == 0) condexp = false_rtx;
3e7b5313
TW
1610 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1611 make_numeric_value (1),
1612 make_numeric_value (0));
0e9414fd
MM
1613 p = attr_printf (sizeof ("*annul_false__") + MAX_DIGITS*2,
1614 "*annul_false_%d_%d", delay->num, i / 3);
41299f41
TW
1615 make_internal_attr (p, newexp, 1);
1616 }
1617 }
1618 }
1619}
1620\f
1621/* This function is given a left and right side expression and an operator.
1622 Each side is a conditional expression, each alternative of which has a
1623 numerical value. The function returns another conditional expression
1624 which, for every possible set of condition values, returns a value that is
1625 the operator applied to the values of the two sides.
1626
1627 Since this is called early, it must also support IF_THEN_ELSE. */
1628
1629static rtx
1630operate_exp (op, left, right)
1631 enum operator op;
1632 rtx left, right;
1633{
1634 int left_value, right_value;
1635 rtx newexp;
1636 int i;
1637
1638 /* If left is a string, apply operator to it and the right side. */
1639 if (GET_CODE (left) == CONST_STRING)
1640 {
1641 /* If right is also a string, just perform the operation. */
1642 if (GET_CODE (right) == CONST_STRING)
1643 {
1644 left_value = atoi (XSTR (left, 0));
1645 right_value = atoi (XSTR (right, 0));
1646 switch (op)
1647 {
1648 case PLUS_OP:
1649 i = left_value + right_value;
1650 break;
1651
1652 case MINUS_OP:
1653 i = left_value - right_value;
1654 break;
1655
bee757e1
TW
1656 case POS_MINUS_OP: /* The positive part of LEFT - RIGHT. */
1657 if (left_value > right_value)
1658 i = left_value - right_value;
1659 else
1660 i = 0;
1661 break;
1662
41299f41 1663 case OR_OP:
71d9b493 1664 case ORX_OP:
41299f41
TW
1665 i = left_value | right_value;
1666 break;
1667
bee757e1
TW
1668 case EQ_OP:
1669 i = left_value == right_value;
1670 break;
1671
1672 case RANGE_OP:
1673 i = (left_value << (HOST_BITS_PER_INT / 2)) | right_value;
1674 break;
1675
41299f41
TW
1676 case MAX_OP:
1677 if (left_value > right_value)
1678 i = left_value;
1679 else
1680 i = right_value;
1681 break;
1682
bee757e1
TW
1683 case MIN_OP:
1684 if (left_value < right_value)
1685 i = left_value;
1686 else
1687 i = right_value;
1688 break;
1689
41299f41
TW
1690 default:
1691 abort ();
1692 }
1693
71d9b493
RH
1694 if (i == left_value)
1695 return left;
1696 if (i == right_value)
1697 return right;
41299f41
TW
1698 return make_numeric_value (i);
1699 }
1700 else if (GET_CODE (right) == IF_THEN_ELSE)
1701 {
1702 /* Apply recursively to all values within. */
7339c88d
RS
1703 rtx newleft = operate_exp (op, left, XEXP (right, 1));
1704 rtx newright = operate_exp (op, left, XEXP (right, 2));
1705 if (rtx_equal_p (newleft, newright))
1706 return newleft;
1707 return attr_rtx (IF_THEN_ELSE, XEXP (right, 0), newleft, newright);
41299f41
TW
1708 }
1709 else if (GET_CODE (right) == COND)
1710 {
7339c88d
RS
1711 int allsame = 1;
1712 rtx defval;
1713
41299f41
TW
1714 newexp = rtx_alloc (COND);
1715 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (right, 0));
7339c88d
RS
1716 defval = XEXP (newexp, 1) = operate_exp (op, left, XEXP (right, 1));
1717
41299f41
TW
1718 for (i = 0; i < XVECLEN (right, 0); i += 2)
1719 {
1720 XVECEXP (newexp, 0, i) = XVECEXP (right, 0, i);
1721 XVECEXP (newexp, 0, i + 1)
1722 = operate_exp (op, left, XVECEXP (right, 0, i + 1));
7339c88d
RS
1723 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1724 defval))
1725 allsame = 0;
41299f41
TW
1726 }
1727
7339c88d
RS
1728 /* If the resulting cond is trivial (all alternatives
1729 give the same value), optimize it away. */
1730 if (allsame)
1731 {
a3770a81
RH
1732 if (!ggc_p)
1733 obstack_free (rtl_obstack, newexp);
7339c88d
RS
1734 return operate_exp (op, left, XEXP (right, 1));
1735 }
1736
1737 /* If the result is the same as the RIGHT operand,
1738 just use that. */
1739 if (rtx_equal_p (newexp, right))
1740 {
a3770a81
RH
1741 if (!ggc_p)
1742 obstack_free (rtl_obstack, newexp);
7339c88d
RS
1743 return right;
1744 }
41299f41
TW
1745
1746 return newexp;
1747 }
1748 else
1749 fatal ("Badly formed attribute value");
1750 }
1751
71d9b493
RH
1752 /* A hack to prevent expand_units from completely blowing up: ORX_OP does
1753 not associate through IF_THEN_ELSE. */
1754 else if (op == ORX_OP && GET_CODE (right) == IF_THEN_ELSE)
1755 {
1756 return attr_rtx (IOR, left, right);
1757 }
1758
41299f41
TW
1759 /* Otherwise, do recursion the other way. */
1760 else if (GET_CODE (left) == IF_THEN_ELSE)
1761 {
7339c88d
RS
1762 rtx newleft = operate_exp (op, XEXP (left, 1), right);
1763 rtx newright = operate_exp (op, XEXP (left, 2), right);
1764 if (rtx_equal_p (newleft, newright))
1765 return newleft;
1766 return attr_rtx (IF_THEN_ELSE, XEXP (left, 0), newleft, newright);
41299f41 1767 }
41299f41
TW
1768 else if (GET_CODE (left) == COND)
1769 {
7339c88d
RS
1770 int allsame = 1;
1771 rtx defval;
1772
41299f41
TW
1773 newexp = rtx_alloc (COND);
1774 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (left, 0));
7339c88d
RS
1775 defval = XEXP (newexp, 1) = operate_exp (op, XEXP (left, 1), right);
1776
41299f41
TW
1777 for (i = 0; i < XVECLEN (left, 0); i += 2)
1778 {
1779 XVECEXP (newexp, 0, i) = XVECEXP (left, 0, i);
1780 XVECEXP (newexp, 0, i + 1)
1781 = operate_exp (op, XVECEXP (left, 0, i + 1), right);
7339c88d
RS
1782 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1783 defval))
1784 allsame = 0;
41299f41
TW
1785 }
1786
7339c88d
RS
1787 /* If the cond is trivial (all alternatives give the same value),
1788 optimize it away. */
1789 if (allsame)
1790 {
a3770a81
RH
1791 if (!ggc_p)
1792 obstack_free (rtl_obstack, newexp);
7339c88d
RS
1793 return operate_exp (op, XEXP (left, 1), right);
1794 }
1795
1796 /* If the result is the same as the LEFT operand,
1797 just use that. */
1798 if (rtx_equal_p (newexp, left))
1799 {
a3770a81
RH
1800 if (!ggc_p)
1801 obstack_free (rtl_obstack, newexp);
7339c88d
RS
1802 return left;
1803 }
41299f41
TW
1804
1805 return newexp;
1806 }
1807
1808 else
1809 fatal ("Badly formed attribute value.");
1810 /* NOTREACHED */
1811 return NULL;
1812}
1813\f
1814/* Once all attributes and DEFINE_FUNCTION_UNITs have been read, we
1815 construct a number of attributes.
1816
1817 The first produces a function `function_units_used' which is given an
72f1215c
TW
1818 insn and produces an encoding showing which function units are required
1819 for the execution of that insn. If the value is non-negative, the insn
1820 uses that unit; otherwise, the value is a one's compliment mask of units
1821 used.
41299f41
TW
1822
1823 The second produces a function `result_ready_cost' which is used to
1824 determine the time that the result of an insn will be ready and hence
1825 a worst-case schedule.
1826
1827 Both of these produce quite complex expressions which are then set as the
1828 default value of internal attributes. Normal attribute simplification
1829 should produce reasonable expressions.
1830
1831 For each unit, a `<name>_unit_ready_cost' function will take an
1832 insn and give the delay until that unit will be ready with the result
72f1215c 1833 and a `<name>_unit_conflict_cost' function is given an insn already
41299f41
TW
1834 executing on the unit and a candidate to execute and will give the
1835 cost from the time the executing insn started until the candidate
bee757e1
TW
1836 can start (ignore limitations on the number of simultaneous insns).
1837
1838 For each unit, a `<name>_unit_blockage' function is given an insn
1839 already executing on the unit and a candidate to execute and will
1840 give the delay incurred due to function unit conflicts. The range of
1841 blockage cost values for a given executing insn is given by the
1842 `<name>_unit_blockage_range' function. These values are encoded in
1843 an int where the upper half gives the minimum value and the lower
1844 half gives the maximum value. */
41299f41
TW
1845
1846static void
1847expand_units ()
1848{
72f1215c
TW
1849 struct function_unit *unit, **unit_num;
1850 struct function_unit_op *op, **op_array, ***unit_ops;
41299f41
TW
1851 rtx unitsmask;
1852 rtx readycost;
1853 rtx newexp;
85fda1eb 1854 const char *str;
72f1215c 1855 int i, j, u, num, nvalues;
41299f41 1856
bee757e1
TW
1857 /* Rebuild the condition for the unit to share the RTL expressions.
1858 Sharing is required by simplify_by_exploding. Build the issue delay
1859 expressions. Validate the expressions we were given for the conditions
1860 and conflict vector. Then make attributes for use in the conflict
1861 function. */
1862
72f1215c 1863 for (unit = units; unit; unit = unit->next)
bee757e1 1864 {
bee757e1
TW
1865 unit->condexp = check_attr_test (unit->condexp, 0);
1866
1867 for (op = unit->ops; op; op = op->next)
1868 {
1869 rtx issue_delay = make_numeric_value (op->issue_delay);
1870 rtx issue_exp = issue_delay;
1871
1872 /* Build, validate, and simplify the issue delay expression. */
1873 if (op->conflict_exp != true_rtx)
1874 issue_exp = attr_rtx (IF_THEN_ELSE, op->conflict_exp,
1875 issue_exp, make_numeric_value (0));
1876 issue_exp = check_attr_value (make_canonical (NULL_ATTR,
1877 issue_exp),
72f1215c 1878 NULL_ATTR);
bee757e1
TW
1879 issue_exp = simplify_knowing (issue_exp, unit->condexp);
1880 op->issue_exp = issue_exp;
1881
1882 /* Make an attribute for use in the conflict function if needed. */
1883 unit->needs_conflict_function = (unit->issue_delay.min
1884 != unit->issue_delay.max);
1885 if (unit->needs_conflict_function)
1886 {
0e9414fd
MM
1887 str = attr_printf (strlen (unit->name) + sizeof ("*_cost_") + MAX_DIGITS,
1888 "*%s_cost_%d", unit->name, op->num);
bee757e1
TW
1889 make_internal_attr (str, issue_exp, 1);
1890 }
1891
1892 /* Validate the condition. */
1893 op->condexp = check_attr_test (op->condexp, 0);
1894 }
1895 }
41299f41 1896
72f1215c
TW
1897 /* Compute the mask of function units used. Initially, the unitsmask is
1898 zero. Set up a conditional to compute each unit's contribution. */
1899 unitsmask = make_numeric_value (0);
41299f41
TW
1900 newexp = rtx_alloc (IF_THEN_ELSE);
1901 XEXP (newexp, 2) = make_numeric_value (0);
1902
71d9b493
RH
1903 /* If we have just a few units, we may be all right expanding the whole
1904 thing. But the expansion is 2**N in space on the number of opclasses,
1905 so we can't do this for very long -- Alpha and MIPS in particular have
1906 problems with this. So in that situation, we fall back on an alternate
1907 implementation method. */
1908#define NUM_UNITOP_CUTOFF 20
1909
1910 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
72f1215c 1911 {
71d9b493
RH
1912 /* Merge each function unit into the unit mask attributes. */
1913 for (unit = units; unit; unit = unit->next)
1914 {
1915 XEXP (newexp, 0) = unit->condexp;
1916 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1917 unitsmask = operate_exp (OR_OP, unitsmask, newexp);
1918 }
1919 }
1920 else
1921 {
1922 /* Merge each function unit into the unit mask attributes. */
1923 for (unit = units; unit; unit = unit->next)
1924 {
1925 XEXP (newexp, 0) = unit->condexp;
1926 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1927 unitsmask = operate_exp (ORX_OP, unitsmask, attr_copy_rtx (newexp));
1928 }
72f1215c
TW
1929 }
1930
1931 /* Simplify the unit mask expression, encode it, and make an attribute
1932 for the function_units_used function. */
1933 unitsmask = simplify_by_exploding (unitsmask);
71d9b493
RH
1934
1935 if (num_unit_opclasses < NUM_UNITOP_CUTOFF)
1936 unitsmask = encode_units_mask (unitsmask);
1937 else
1938 {
1939 /* We can no longer encode unitsmask at compile time, so emit code to
1940 calculate it at runtime. Rather, put a marker for where we'd do
1941 the code, and actually output it in write_attr_get(). */
1942 unitsmask = attr_rtx (FFS, unitsmask);
1943 }
1944
6f6074ea 1945 make_internal_attr ("*function_units_used", unitsmask, 10);
72f1215c
TW
1946
1947 /* Create an array of ops for each unit. Add an extra unit for the
1948 result_ready_cost function that has the ops of all other units. */
1949 unit_ops = (struct function_unit_op ***)
1950 alloca ((num_units + 1) * sizeof (struct function_unit_op **));
1951 unit_num = (struct function_unit **)
1952 alloca ((num_units + 1) * sizeof (struct function_unit *));
1953
1954 unit_num[num_units] = unit = (struct function_unit *)
1955 alloca (sizeof (struct function_unit));
1956 unit->num = num_units;
1957 unit->num_opclasses = 0;
1958
1959 for (unit = units; unit; unit = unit->next)
1960 {
1961 unit_num[num_units]->num_opclasses += unit->num_opclasses;
1962 unit_num[unit->num] = unit;
1963 unit_ops[unit->num] = op_array = (struct function_unit_op **)
1964 alloca (unit->num_opclasses * sizeof (struct function_unit_op *));
1965
1966 for (op = unit->ops; op; op = op->next)
1967 op_array[op->num] = op;
1968 }
1969
1970 /* Compose the array of ops for the extra unit. */
1971 unit_ops[num_units] = op_array = (struct function_unit_op **)
1972 alloca (unit_num[num_units]->num_opclasses
1973 * sizeof (struct function_unit_op *));
1974
1975 for (unit = units, i = 0; unit; i += unit->num_opclasses, unit = unit->next)
4c9a05bc 1976 bcopy ((char *) unit_ops[unit->num], (char *) &op_array[i],
72f1215c
TW
1977 unit->num_opclasses * sizeof (struct function_unit_op *));
1978
1979 /* Compute the ready cost function for each unit by computing the
1980 condition for each non-default value. */
1981 for (u = 0; u <= num_units; u++)
1982 {
1983 rtx orexp;
1984 int value;
1985
1986 unit = unit_num[u];
1987 op_array = unit_ops[unit->num];
1988 num = unit->num_opclasses;
1989
1990 /* Sort the array of ops into increasing ready cost order. */
1991 for (i = 0; i < num; i++)
1992 for (j = num - 1; j > i; j--)
1993 if (op_array[j-1]->ready < op_array[j]->ready)
1994 {
1995 op = op_array[j];
1996 op_array[j] = op_array[j-1];
1997 op_array[j-1] = op;
1998 }
1999
2000 /* Determine how many distinct non-default ready cost values there
2001 are. We use a default ready cost value of 1. */
2002 nvalues = 0; value = 1;
2003 for (i = num - 1; i >= 0; i--)
2004 if (op_array[i]->ready > value)
2005 {
2006 value = op_array[i]->ready;
2007 nvalues++;
2008 }
2009
2010 if (nvalues == 0)
2011 readycost = make_numeric_value (1);
2012 else
2013 {
2014 /* Construct the ready cost expression as a COND of each value from
2015 the largest to the smallest. */
2016 readycost = rtx_alloc (COND);
2017 XVEC (readycost, 0) = rtvec_alloc (nvalues * 2);
2018 XEXP (readycost, 1) = make_numeric_value (1);
2019
2020 nvalues = 0; orexp = false_rtx; value = op_array[0]->ready;
2021 for (i = 0; i < num; i++)
2022 {
2023 op = op_array[i];
2024 if (op->ready <= 1)
2025 break;
2026 else if (op->ready == value)
f75d38a7 2027 orexp = insert_right_side (IOR, orexp, op->condexp, -2, -2);
72f1215c
TW
2028 else
2029 {
2030 XVECEXP (readycost, 0, nvalues * 2) = orexp;
2031 XVECEXP (readycost, 0, nvalues * 2 + 1)
2032 = make_numeric_value (value);
2033 nvalues++;
2034 value = op->ready;
2035 orexp = op->condexp;
2036 }
2037 }
2038 XVECEXP (readycost, 0, nvalues * 2) = orexp;
2039 XVECEXP (readycost, 0, nvalues * 2 + 1) = make_numeric_value (value);
2040 }
2041
72f1215c 2042 if (u < num_units)
bee757e1
TW
2043 {
2044 rtx max_blockage = 0, min_blockage = 0;
2045
2046 /* Simplify the readycost expression by only considering insns
2047 that use the unit. */
2048 readycost = simplify_knowing (readycost, unit->condexp);
2049
2050 /* Determine the blockage cost the executing insn (E) given
2051 the candidate insn (C). This is the maximum of the issue
2052 delay, the pipeline delay, and the simultaneity constraint.
2053 Each function_unit_op represents the characteristics of the
2054 candidate insn, so in the expressions below, C is a known
2055 term and E is an unknown term.
2056
cb1520bc
JW
2057 We compute the blockage cost for each E for every possible C.
2058 Thus OP represents E, and READYCOST is a list of values for
2059 every possible C.
2060
bee757e1
TW
2061 The issue delay function for C is op->issue_exp and is used to
2062 write the `<name>_unit_conflict_cost' function. Symbolicly
2063 this is "ISSUE-DELAY (E,C)".
2064
2065 The pipeline delay results form the FIFO constraint on the
2066 function unit and is "READY-COST (E) + 1 - READY-COST (C)".
2067
2068 The simultaneity constraint is based on how long it takes to
2069 fill the unit given the minimum issue delay. FILL-TIME is the
2070 constant "MIN (ISSUE-DELAY (*,*)) * (SIMULTANEITY - 1)", and
2071 the simultaneity constraint is "READY-COST (E) - FILL-TIME"
2072 if SIMULTANEITY is non-zero and zero otherwise.
2073
2074 Thus, BLOCKAGE (E,C) when SIMULTANEITY is zero is
2075
2076 MAX (ISSUE-DELAY (E,C),
2077 READY-COST (E) - (READY-COST (C) - 1))
2078
2079 and otherwise
2080
2081 MAX (ISSUE-DELAY (E,C),
2082 READY-COST (E) - (READY-COST (C) - 1),
2083 READY-COST (E) - FILL-TIME)
2084
2085 The `<name>_unit_blockage' function is computed by determining
2086 this value for each candidate insn. As these values are
2087 computed, we also compute the upper and lower bounds for
2088 BLOCKAGE (E,*). These are combined to form the function
2089 `<name>_unit_blockage_range'. Finally, the maximum blockage
2090 cost, MAX (BLOCKAGE (*,*)), is computed. */
2091
2092 for (op = unit->ops; op; op = op->next)
2093 {
8c660648 2094 rtx blockage = op->issue_exp;
bee757e1
TW
2095 blockage = simplify_knowing (blockage, unit->condexp);
2096
2097 /* Add this op's contribution to MAX (BLOCKAGE (E,*)) and
2098 MIN (BLOCKAGE (E,*)). */
2099 if (max_blockage == 0)
2100 max_blockage = min_blockage = blockage;
2101 else
2102 {
2103 max_blockage
2104 = simplify_knowing (operate_exp (MAX_OP, max_blockage,
2105 blockage),
2106 unit->condexp);
2107 min_blockage
2108 = simplify_knowing (operate_exp (MIN_OP, min_blockage,
2109 blockage),
2110 unit->condexp);
2111 }
2112
2113 /* Make an attribute for use in the blockage function. */
0e9414fd
MM
2114 str = attr_printf (strlen (unit->name) + sizeof ("*_block_") + MAX_DIGITS,
2115 "*%s_block_%d", unit->name, op->num);
bee757e1
TW
2116 make_internal_attr (str, blockage, 1);
2117 }
2118
2119 /* Record MAX (BLOCKAGE (*,*)). */
7ee37ba4
RH
2120 {
2121 int unknown;
2122 unit->max_blockage = max_attr_value (max_blockage, &unknown);
2123 }
bee757e1
TW
2124
2125 /* See if the upper and lower bounds of BLOCKAGE (E,*) are the
2126 same. If so, the blockage function carries no additional
2127 information and is not written. */
2128 newexp = operate_exp (EQ_OP, max_blockage, min_blockage);
2129 newexp = simplify_knowing (newexp, unit->condexp);
2130 unit->needs_blockage_function
2131 = (GET_CODE (newexp) != CONST_STRING
2132 || atoi (XSTR (newexp, 0)) != 1);
2133
2134 /* If the all values of BLOCKAGE (E,C) have the same value,
2135 neither blockage function is written. */
2136 unit->needs_range_function
2137 = (unit->needs_blockage_function
2138 || GET_CODE (max_blockage) != CONST_STRING);
2139
2140 if (unit->needs_range_function)
2141 {
2142 /* Compute the blockage range function and make an attribute
9ec36da5 2143 for writing its value. */
bee757e1
TW
2144 newexp = operate_exp (RANGE_OP, min_blockage, max_blockage);
2145 newexp = simplify_knowing (newexp, unit->condexp);
2146
0e9414fd 2147 str = attr_printf (strlen (unit->name) + sizeof ("*_unit_blockage_range"),
bee757e1 2148 "*%s_unit_blockage_range", unit->name);
6f6074ea 2149 make_internal_attr (str, newexp, 20);
bee757e1
TW
2150 }
2151
0e9414fd
MM
2152 str = attr_printf (strlen (unit->name) + sizeof ("*_unit_ready_cost"),
2153 "*%s_unit_ready_cost", unit->name);
bee757e1 2154 }
72f1215c
TW
2155 else
2156 str = "*result_ready_cost";
bee757e1
TW
2157
2158 /* Make an attribute for the ready_cost function. Simplifying
2159 further with simplify_by_exploding doesn't win. */
72f1215c
TW
2160 make_internal_attr (str, readycost, 0);
2161 }
2162
2163 /* For each unit that requires a conflict cost function, make an attribute
2164 that maps insns to the operation number. */
41299f41
TW
2165 for (unit = units; unit; unit = unit->next)
2166 {
72f1215c 2167 rtx caseexp;
41299f41 2168
bee757e1
TW
2169 if (! unit->needs_conflict_function
2170 && ! unit->needs_blockage_function)
72f1215c
TW
2171 continue;
2172
2173 caseexp = rtx_alloc (COND);
41299f41
TW
2174 XVEC (caseexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
2175
2176 for (op = unit->ops; op; op = op->next)
2177 {
72f1215c
TW
2178 /* Make our adjustment to the COND being computed. If we are the
2179 last operation class, place our values into the default of the
2180 COND. */
41299f41
TW
2181 if (op->num == unit->num_opclasses - 1)
2182 {
41299f41
TW
2183 XEXP (caseexp, 1) = make_numeric_value (op->num);
2184 }
2185 else
2186 {
41299f41
TW
2187 XVECEXP (caseexp, 0, op->num * 2) = op->condexp;
2188 XVECEXP (caseexp, 0, op->num * 2 + 1)
2189 = make_numeric_value (op->num);
2190 }
2191 }
2192
72f1215c 2193 /* Simplifying caseexp with simplify_by_exploding doesn't win. */
0e9414fd
MM
2194 str = attr_printf (strlen (unit->name) + sizeof ("*_cases"),
2195 "*%s_cases", unit->name);
41299f41 2196 make_internal_attr (str, caseexp, 1);
72f1215c
TW
2197 }
2198}
41299f41 2199
bee757e1
TW
2200/* Simplify EXP given KNOWN_TRUE. */
2201
2202static rtx
2203simplify_knowing (exp, known_true)
2204 rtx exp, known_true;
2205{
2206 if (GET_CODE (exp) != CONST_STRING)
2207 {
6166aa01 2208 int unknown = 0, max;
7ee37ba4
RH
2209 max = max_attr_value (exp, &unknown);
2210 if (! unknown)
2211 {
2212 exp = attr_rtx (IF_THEN_ELSE, known_true, exp,
2213 make_numeric_value (max));
2214 exp = simplify_by_exploding (exp);
2215 }
bee757e1
TW
2216 }
2217 return exp;
2218}
2219
72f1215c
TW
2220/* Translate the CONST_STRING expressions in X to change the encoding of
2221 value. On input, the value is a bitmask with a one bit for each unit
2222 used; on output, the value is the unit number (zero based) if one
2223 and only one unit is used or the one's compliment of the bitmask. */
41299f41 2224
72f1215c
TW
2225static rtx
2226encode_units_mask (x)
2227 rtx x;
2228{
2229 register int i;
2230 register int j;
2231 register enum rtx_code code;
6f7d635c 2232 register const char *fmt;
72f1215c
TW
2233
2234 code = GET_CODE (x);
2235
2236 switch (code)
2237 {
2238 case CONST_STRING:
2239 i = atoi (XSTR (x, 0));
2240 if (i < 0)
2241 abort (); /* The sign bit encodes a one's compliment mask. */
2242 else if (i != 0 && i == (i & -i))
2243 /* Only one bit is set, so yield that unit number. */
2244 for (j = 0; (i >>= 1) != 0; j++)
2245 ;
2246 else
2247 j = ~i;
0e9414fd 2248 return attr_rtx (CONST_STRING, attr_printf (MAX_DIGITS, "%d", j));
41299f41 2249
72f1215c
TW
2250 case REG:
2251 case QUEUED:
2252 case CONST_INT:
2253 case CONST_DOUBLE:
2254 case SYMBOL_REF:
2255 case CODE_LABEL:
2256 case PC:
2257 case CC0:
2258 case EQ_ATTR:
2259 return x;
e9a25f70
JL
2260
2261 default:
2262 break;
41299f41
TW
2263 }
2264
72f1215c
TW
2265 /* Compare the elements. If any pair of corresponding elements
2266 fail to match, return 0 for the whole things. */
2267
2268 fmt = GET_RTX_FORMAT (code);
2269 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2270 {
2271 switch (fmt[i])
2272 {
2273 case 'V':
2274 case 'E':
2275 for (j = 0; j < XVECLEN (x, i); j++)
2276 XVECEXP (x, i, j) = encode_units_mask (XVECEXP (x, i, j));
2277 break;
2278
2279 case 'e':
2280 XEXP (x, i) = encode_units_mask (XEXP (x, i));
2281 break;
2282 }
2283 }
2284 return x;
41299f41
TW
2285}
2286\f
2287/* Once all attributes and insns have been read and checked, we construct for
2288 each attribute value a list of all the insns that have that value for
2289 the attribute. */
2290
2291static void
2292fill_attr (attr)
2293 struct attr_desc *attr;
2294{
2295 struct attr_value *av;
2296 struct insn_ent *ie;
2297 struct insn_def *id;
2298 int i;
2299 rtx value;
2300
b31a5831
RS
2301 /* Don't fill constant attributes. The value is independent of
2302 any particular insn. */
2303 if (attr->is_const)
2304 return;
2305
41299f41
TW
2306 for (id = defs; id; id = id->next)
2307 {
2308 /* If no value is specified for this insn for this attribute, use the
2309 default. */
2310 value = NULL;
2311 if (XVEC (id->def, id->vec_idx))
2312 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
2313 if (! strcmp (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
2314 attr->name))
2315 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
2316
2317 if (value == NULL)
2318 av = attr->default_val;
2319 else
2320 av = get_attr_value (value, attr, id->insn_code);
2321
0e9414fd 2322 ie = (struct insn_ent *) oballoc (sizeof (struct insn_ent));
41299f41
TW
2323 ie->insn_code = id->insn_code;
2324 ie->insn_index = id->insn_code;
2325 insert_insn_ent (av, ie);
2326 }
2327}
2328\f
ae90e6a3
RS
2329/* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
2330 test that checks relative positions of insns (uses MATCH_DUP or PC).
2331 If so, replace it with what is obtained by passing the expression to
2332 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
2333 recursively on each value (including the default value). Otherwise,
2334 return the value returned by NO_ADDRESS_FN applied to EXP. */
41299f41
TW
2335
2336static rtx
2337substitute_address (exp, no_address_fn, address_fn)
2338 rtx exp;
a94ae8f5
KG
2339 rtx (*no_address_fn) PARAMS ((rtx));
2340 rtx (*address_fn) PARAMS ((rtx));
41299f41
TW
2341{
2342 int i;
2343 rtx newexp;
2344
ae90e6a3
RS
2345 if (GET_CODE (exp) == COND)
2346 {
2347 /* See if any tests use addresses. */
2348 address_used = 0;
2349 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2350 walk_attr_value (XVECEXP (exp, 0, i));
41299f41 2351
ae90e6a3
RS
2352 if (address_used)
2353 return (*address_fn) (exp);
41299f41 2354
ae90e6a3
RS
2355 /* Make a new copy of this COND, replacing each element. */
2356 newexp = rtx_alloc (COND);
2357 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
2358 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2359 {
2360 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
2361 XVECEXP (newexp, 0, i + 1)
2362 = substitute_address (XVECEXP (exp, 0, i + 1),
2363 no_address_fn, address_fn);
2364 }
41299f41 2365
ae90e6a3
RS
2366 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
2367 no_address_fn, address_fn);
2368
2369 return newexp;
41299f41
TW
2370 }
2371
ae90e6a3
RS
2372 else if (GET_CODE (exp) == IF_THEN_ELSE)
2373 {
2374 address_used = 0;
2375 walk_attr_value (XEXP (exp, 0));
2376 if (address_used)
2377 return (*address_fn) (exp);
41299f41 2378
3e7b5313
TW
2379 return attr_rtx (IF_THEN_ELSE,
2380 substitute_address (XEXP (exp, 0),
2381 no_address_fn, address_fn),
2382 substitute_address (XEXP (exp, 1),
2383 no_address_fn, address_fn),
2384 substitute_address (XEXP (exp, 2),
2385 no_address_fn, address_fn));
ae90e6a3
RS
2386 }
2387
2388 return (*no_address_fn) (exp);
41299f41
TW
2389}
2390\f
2391/* Make new attributes from the `length' attribute. The following are made,
2392 each corresponding to a function called from `shorten_branches' or
2393 `get_attr_length':
2394
2395 *insn_default_length This is the length of the insn to be returned
2396 by `get_attr_length' before `shorten_branches'
2397 has been called. In each case where the length
2398 depends on relative addresses, the largest
2399 possible is used. This routine is also used
2400 to compute the initial size of the insn.
2401
2402 *insn_variable_length_p This returns 1 if the insn's length depends
2403 on relative addresses, zero otherwise.
2404
2405 *insn_current_length This is only called when it is known that the
2406 insn has a variable length and returns the
2407 current length, based on relative addresses.
2408 */
2409
2410static void
2411make_length_attrs ()
2412{
85fda1eb
KG
2413 static const char *new_names[] = {"*insn_default_length",
2414 "*insn_variable_length_p",
2415 "*insn_current_length"};
a94ae8f5
KG
2416 static rtx (*no_address_fn[]) PARAMS ((rtx)) = {identity_fn, zero_fn, zero_fn};
2417 static rtx (*address_fn[]) PARAMS ((rtx)) = {max_fn, one_fn, identity_fn};
d6f4ec51 2418 size_t i;
41299f41
TW
2419 struct attr_desc *length_attr, *new_attr;
2420 struct attr_value *av, *new_av;
2421 struct insn_ent *ie, *new_ie;
2422
2423 /* See if length attribute is defined. If so, it must be numeric. Make
2424 it special so we don't output anything for it. */
2425 length_attr = find_attr ("length", 0);
2426 if (length_attr == 0)
2427 return;
2428
2429 if (! length_attr->is_numeric)
2430 fatal ("length attribute must be numeric.");
2431
3e7b5313 2432 length_attr->is_const = 0;
41299f41
TW
2433 length_attr->is_special = 1;
2434
2435 /* Make each new attribute, in turn. */
2436 for (i = 0; i < sizeof new_names / sizeof new_names[0]; i++)
2437 {
2438 make_internal_attr (new_names[i],
2439 substitute_address (length_attr->default_val->value,
2440 no_address_fn[i], address_fn[i]),
2441 0);
2442 new_attr = find_attr (new_names[i], 0);
2443 for (av = length_attr->first_value; av; av = av->next)
2444 for (ie = av->first_insn; ie; ie = ie->next)
2445 {
2446 new_av = get_attr_value (substitute_address (av->value,
2447 no_address_fn[i],
2448 address_fn[i]),
2449 new_attr, ie->insn_code);
0e9414fd 2450 new_ie = (struct insn_ent *) oballoc (sizeof (struct insn_ent));
41299f41
TW
2451 new_ie->insn_code = ie->insn_code;
2452 new_ie->insn_index = ie->insn_index;
2453 insert_insn_ent (new_av, new_ie);
2454 }
2455 }
2456}
2457
2458/* Utility functions called from above routine. */
2459
2460static rtx
2461identity_fn (exp)
2462 rtx exp;
2463{
2464 return exp;
2465}
2466
2467static rtx
2468zero_fn (exp)
d6f4ec51 2469 rtx exp ATTRIBUTE_UNUSED;
41299f41
TW
2470{
2471 return make_numeric_value (0);
2472}
2473
2474static rtx
2475one_fn (exp)
d6f4ec51 2476 rtx exp ATTRIBUTE_UNUSED;
41299f41
TW
2477{
2478 return make_numeric_value (1);
2479}
2480
2481static rtx
2482max_fn (exp)
2483 rtx exp;
2484{
7ee37ba4
RH
2485 int unknown;
2486 return make_numeric_value (max_attr_value (exp, &unknown));
41299f41 2487}
fc470718
R
2488
2489static void
2490write_length_unit_log ()
2491{
2492 struct attr_desc *length_attr = find_attr ("length", 0);
2493 struct attr_value *av;
2494 struct insn_ent *ie;
2495 unsigned int length_unit_log, length_or;
7ee37ba4 2496 int unknown = 0;
fc470718
R
2497
2498 if (length_attr == 0)
2499 return;
7ee37ba4
RH
2500 length_or = or_attr_value (length_attr->default_val->value, &unknown);
2501 for (av = length_attr->first_value; av; av = av->next)
2502 for (ie = av->first_insn; ie; ie = ie->next)
2503 length_or |= or_attr_value (av->value, &unknown);
2504
2505 if (unknown)
2506 length_unit_log = 0;
2507 else
2508 {
2509 length_or = ~length_or;
2510 for (length_unit_log = 0; length_or & 1; length_or >>= 1)
2511 length_unit_log++;
2512 }
fc470718
R
2513 printf ("int length_unit_log = %u;\n", length_unit_log);
2514}
41299f41
TW
2515\f
2516/* Take a COND expression and see if any of the conditions in it can be
2517 simplified. If any are known true or known false for the particular insn
2518 code, the COND can be further simplified.
2519
2520 Also call ourselves on any COND operations that are values of this COND.
2521
7339c88d 2522 We do not modify EXP; rather, we make and return a new rtx. */
41299f41
TW
2523
2524static rtx
2525simplify_cond (exp, insn_code, insn_index)
2526 rtx exp;
2527 int insn_code, insn_index;
2528{
2529 int i, j;
7339c88d
RS
2530 /* We store the desired contents here,
2531 then build a new expression if they don't match EXP. */
2532 rtx defval = XEXP (exp, 1);
eaed7119 2533 rtx new_defval = XEXP (exp, 1);
7339c88d 2534 int len = XVECLEN (exp, 0);
910eabe5 2535 rtx *tests = (rtx *) alloca (len * sizeof (rtx));
7339c88d 2536 int allsame = 1;
b5b6ad46 2537 char *first_spacer;
41299f41 2538
7339c88d 2539 /* This lets us free all storage allocated below, if appropriate. */
81fd4c6e 2540 first_spacer = (char *) obstack_finish (rtl_obstack);
41299f41 2541
910eabe5 2542 bcopy ((char *) XVEC (exp, 0)->elem, (char *) tests, len * sizeof (rtx));
41299f41 2543
7339c88d
RS
2544 /* See if default value needs simplification. */
2545 if (GET_CODE (defval) == COND)
eaed7119 2546 new_defval = simplify_cond (defval, insn_code, insn_index);
41299f41 2547
81fd4c6e 2548 /* Simplify the subexpressions, and see what tests we can get rid of. */
41299f41 2549
81fd4c6e 2550 for (i = 0; i < len; i += 2)
7339c88d
RS
2551 {
2552 rtx newtest, newval;
41299f41 2553
7339c88d 2554 /* Simplify this test. */
910eabe5
JL
2555 newtest = SIMPLIFY_TEST_EXP (tests[i], insn_code, insn_index);
2556 tests[i] = newtest;
41299f41 2557
910eabe5 2558 newval = tests[i + 1];
7339c88d
RS
2559 /* See if this value may need simplification. */
2560 if (GET_CODE (newval) == COND)
2561 newval = simplify_cond (newval, insn_code, insn_index);
2562
2563 /* Look for ways to delete or combine this test. */
2564 if (newtest == true_rtx)
2565 {
2566 /* If test is true, make this value the default
2567 and discard this + any following tests. */
2568 len = i;
910eabe5 2569 defval = tests[i + 1];
eaed7119 2570 new_defval = newval;
41299f41
TW
2571 }
2572
7339c88d 2573 else if (newtest == false_rtx)
41299f41 2574 {
7339c88d
RS
2575 /* If test is false, discard it and its value. */
2576 for (j = i; j < len - 2; j++)
910eabe5 2577 tests[j] = tests[j + 2];
81fd4c6e
RS
2578 len -= 2;
2579 }
41299f41 2580
910eabe5 2581 else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
81fd4c6e
RS
2582 {
2583 /* If this value and the value for the prev test are the same,
2584 merge the tests. */
2585
910eabe5
JL
2586 tests[i - 2]
2587 = insert_right_side (IOR, tests[i - 2], newtest,
81fd4c6e
RS
2588 insn_code, insn_index);
2589
2590 /* Delete this test/value. */
2591 for (j = i; j < len - 2; j++)
910eabe5 2592 tests[j] = tests[j + 2];
7339c88d 2593 len -= 2;
41299f41
TW
2594 }
2595
81fd4c6e 2596 else
910eabe5 2597 tests[i + 1] = newval;
7339c88d 2598 }
41299f41 2599
81fd4c6e
RS
2600 /* If the last test in a COND has the same value
2601 as the default value, that test isn't needed. */
2602
910eabe5 2603 while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
81fd4c6e
RS
2604 len -= 2;
2605
2606 /* See if we changed anything. */
2607 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
2608 allsame = 0;
2609 else
2610 for (i = 0; i < len; i++)
910eabe5 2611 if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
81fd4c6e
RS
2612 {
2613 allsame = 0;
2614 break;
2615 }
41299f41 2616
7339c88d
RS
2617 if (len == 0)
2618 {
a3770a81
RH
2619 if (!ggc_p)
2620 obstack_free (rtl_obstack, first_spacer);
7339c88d
RS
2621 if (GET_CODE (defval) == COND)
2622 return simplify_cond (defval, insn_code, insn_index);
2623 return defval;
2624 }
81fd4c6e
RS
2625 else if (allsame)
2626 {
a3770a81
RH
2627 if (!ggc_p)
2628 obstack_free (rtl_obstack, first_spacer);
81fd4c6e
RS
2629 return exp;
2630 }
7339c88d
RS
2631 else
2632 {
81fd4c6e 2633 rtx newexp = rtx_alloc (COND);
7339c88d
RS
2634
2635 XVEC (newexp, 0) = rtvec_alloc (len);
ec743e55 2636 bcopy ((char *) tests, (char *) XVEC (newexp, 0)->elem,
910eabe5 2637 len * sizeof (rtx));
81fd4c6e 2638 XEXP (newexp, 1) = new_defval;
7339c88d 2639 return newexp;
41299f41 2640 }
41299f41
TW
2641}
2642\f
2643/* Remove an insn entry from an attribute value. */
2644
2645static void
2646remove_insn_ent (av, ie)
2647 struct attr_value *av;
2648 struct insn_ent *ie;
2649{
2650 struct insn_ent *previe;
2651
2652 if (av->first_insn == ie)
2653 av->first_insn = ie->next;
2654 else
2655 {
2656 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
2657 ;
2658 previe->next = ie->next;
2659 }
2660
2661 av->num_insns--;
2662 if (ie->insn_code == -1)
2663 av->has_asm_insn = 0;
1c69865d
ILT
2664
2665 num_insn_ents--;
41299f41
TW
2666}
2667
2668/* Insert an insn entry in an attribute value list. */
2669
2670static void
2671insert_insn_ent (av, ie)
2672 struct attr_value *av;
2673 struct insn_ent *ie;
2674{
2675 ie->next = av->first_insn;
2676 av->first_insn = ie;
2677 av->num_insns++;
2678 if (ie->insn_code == -1)
2679 av->has_asm_insn = 1;
1c69865d
ILT
2680
2681 num_insn_ents++;
41299f41
TW
2682}
2683\f
2684/* This is a utility routine to take an expression that is a tree of either
2685 AND or IOR expressions and insert a new term. The new term will be
2686 inserted at the right side of the first node whose code does not match
2687 the root. A new node will be created with the root's code. Its left
2688 side will be the old right side and its right side will be the new
2689 term.
2690
2691 If the `term' is itself a tree, all its leaves will be inserted. */
2692
2693static rtx
2694insert_right_side (code, exp, term, insn_code, insn_index)
f75d38a7 2695 enum rtx_code code;
41299f41
TW
2696 rtx exp;
2697 rtx term;
2698 int insn_code, insn_index;
2699{
2700 rtx newexp;
2701
7339c88d
RS
2702 /* Avoid consing in some special cases. */
2703 if (code == AND && term == true_rtx)
2704 return exp;
2705 if (code == AND && term == false_rtx)
2706 return false_rtx;
2707 if (code == AND && exp == true_rtx)
2708 return term;
2709 if (code == AND && exp == false_rtx)
2710 return false_rtx;
2711 if (code == IOR && term == true_rtx)
2712 return true_rtx;
2713 if (code == IOR && term == false_rtx)
2714 return exp;
2715 if (code == IOR && exp == true_rtx)
2716 return true_rtx;
2717 if (code == IOR && exp == false_rtx)
2718 return term;
81fd4c6e 2719 if (attr_equal_p (exp, term))
7339c88d
RS
2720 return exp;
2721
41299f41
TW
2722 if (GET_CODE (term) == code)
2723 {
2724 exp = insert_right_side (code, exp, XEXP (term, 0),
2725 insn_code, insn_index);
2726 exp = insert_right_side (code, exp, XEXP (term, 1),
2727 insn_code, insn_index);
2728
2729 return exp;
2730 }
2731
2732 if (GET_CODE (exp) == code)
2733 {
7339c88d
RS
2734 rtx new = insert_right_side (code, XEXP (exp, 1),
2735 term, insn_code, insn_index);
2736 if (new != XEXP (exp, 1))
2737 /* Make a copy of this expression and call recursively. */
2738 newexp = attr_rtx (code, XEXP (exp, 0), new);
2739 else
2740 newexp = exp;
41299f41
TW
2741 }
2742 else
2743 {
2744 /* Insert the new term. */
3e7b5313 2745 newexp = attr_rtx (code, exp, term);
7339c88d 2746 }
41299f41
TW
2747
2748 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2749}
2750\f
2751/* If we have an expression which AND's a bunch of
2752 (not (eq_attrq "alternative" "n"))
2753 terms, we may have covered all or all but one of the possible alternatives.
2754 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
2755
2756 This routine is passed an expression and either AND or IOR. It returns a
f75d38a7 2757 bitmask indicating which alternatives are mentioned within EXP. */
41299f41
TW
2758
2759static int
2760compute_alternative_mask (exp, code)
2761 rtx exp;
f75d38a7 2762 enum rtx_code code;
41299f41 2763{
3cce094d 2764 const char *string;
41299f41
TW
2765 if (GET_CODE (exp) == code)
2766 return compute_alternative_mask (XEXP (exp, 0), code)
2767 | compute_alternative_mask (XEXP (exp, 1), code);
2768
2769 else if (code == AND && GET_CODE (exp) == NOT
2770 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2771 && XSTR (XEXP (exp, 0), 0) == alternative_name)
b31a5831 2772 string = XSTR (XEXP (exp, 0), 1);
41299f41
TW
2773
2774 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
2775 && XSTR (exp, 0) == alternative_name)
b31a5831 2776 string = XSTR (exp, 1);
41299f41
TW
2777
2778 else
2779 return 0;
b31a5831
RS
2780
2781 if (string[1] == 0)
2782 return 1 << (string[0] - '0');
2783 return 1 << atoi (string);
41299f41
TW
2784}
2785
2786/* Given I, a single-bit mask, return RTX to compare the `alternative'
2787 attribute with the value represented by that bit. */
2788
2789static rtx
2790make_alternative_compare (mask)
2791 int mask;
2792{
2793 rtx newexp;
2794 int i;
41299f41
TW
2795
2796 /* Find the bit. */
2797 for (i = 0; (mask & (1 << i)) == 0; i++)
2798 ;
2799
81fd4c6e 2800 newexp = attr_rtx (EQ_ATTR, alternative_name, attr_numeral (i));
41299f41
TW
2801 RTX_UNCHANGING_P (newexp) = 1;
2802
2803 return newexp;
2804}
2805\f
2806/* If we are processing an (eq_attr "attr" "value") test, we find the value
2807 of "attr" for this insn code. From that value, we can compute a test
2808 showing when the EQ_ATTR will be true. This routine performs that
2809 computation. If a test condition involves an address, we leave the EQ_ATTR
f75d38a7 2810 intact because addresses are only valid for the `length' attribute.
41299f41 2811
f75d38a7
RK
2812 EXP is the EQ_ATTR expression and VALUE is the value of that attribute
2813 for the insn corresponding to INSN_CODE and INSN_INDEX. */
7339c88d 2814
41299f41
TW
2815static rtx
2816evaluate_eq_attr (exp, value, insn_code, insn_index)
2817 rtx exp;
2818 rtx value;
2819 int insn_code, insn_index;
2820{
2821 rtx orexp, andexp;
2822 rtx right;
2823 rtx newexp;
2824 int i;
2825
2826 if (GET_CODE (value) == CONST_STRING)
2827 {
2828 if (! strcmp (XSTR (value, 0), XSTR (exp, 1)))
2829 newexp = true_rtx;
2830 else
2831 newexp = false_rtx;
2832 }
71d9b493
RH
2833 else if (GET_CODE (value) == SYMBOL_REF)
2834 {
2835 char *p, *string;
2836
2837 if (GET_CODE (exp) != EQ_ATTR)
2838 abort();
2839
2840 string = (char *) alloca (2 + strlen (XSTR (exp, 0))
2841 + strlen (XSTR (exp, 1)));
2842 strcpy (string, XSTR (exp, 0));
2843 strcat (string, "_");
2844 strcat (string, XSTR (exp, 1));
2845 for (p = string; *p ; p++)
92a438d1 2846 *p = TOUPPER (*p);
71d9b493
RH
2847
2848 newexp = attr_rtx (EQ, value,
2849 attr_rtx (SYMBOL_REF,
2850 attr_string(string, strlen(string))));
2851 }
41299f41
TW
2852 else if (GET_CODE (value) == COND)
2853 {
2854 /* We construct an IOR of all the cases for which the requested attribute
2855 value is present. Since we start with FALSE, if it is not present,
2856 FALSE will be returned.
2857
2858 Each case is the AND of the NOT's of the previous conditions with the
2859 current condition; in the default case the current condition is TRUE.
2860
2861 For each possible COND value, call ourselves recursively.
2862
2863 The extra TRUE and FALSE expressions will be eliminated by another
0f41302f 2864 call to the simplification routine. */
41299f41
TW
2865
2866 orexp = false_rtx;
2867 andexp = true_rtx;
2868
3715a518
RS
2869 if (current_alternative_string)
2870 clear_struct_flag (value);
2871
41299f41
TW
2872 for (i = 0; i < XVECLEN (value, 0); i += 2)
2873 {
7339c88d
RS
2874 rtx this = SIMPLIFY_TEST_EXP (XVECEXP (value, 0, i),
2875 insn_code, insn_index);
2876
3715a518
RS
2877 SIMPLIFY_ALTERNATIVE (this);
2878
7339c88d 2879 right = insert_right_side (AND, andexp, this,
41299f41
TW
2880 insn_code, insn_index);
2881 right = insert_right_side (AND, right,
f75d38a7
RK
2882 evaluate_eq_attr (exp,
2883 XVECEXP (value, 0,
2884 i + 1),
2885 insn_code, insn_index),
41299f41
TW
2886 insn_code, insn_index);
2887 orexp = insert_right_side (IOR, orexp, right,
2888 insn_code, insn_index);
2889
2890 /* Add this condition into the AND expression. */
7339c88d 2891 newexp = attr_rtx (NOT, this);
41299f41
TW
2892 andexp = insert_right_side (AND, andexp, newexp,
2893 insn_code, insn_index);
2894 }
2895
2896 /* Handle the default case. */
2897 right = insert_right_side (AND, andexp,
2898 evaluate_eq_attr (exp, XEXP (value, 1),
f75d38a7 2899 insn_code, insn_index),
41299f41
TW
2900 insn_code, insn_index);
2901 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2902 }
2903 else
2904 abort ();
2905
052aaaef
RK
2906 /* If uses an address, must return original expression. But set the
2907 RTX_UNCHANGING_P bit so we don't try to simplify it again. */
41299f41
TW
2908
2909 address_used = 0;
2910 walk_attr_value (newexp);
2911
2912 if (address_used)
052aaaef 2913 {
45044655
RS
2914 /* This had `&& current_alternative_string', which seems to be wrong. */
2915 if (! RTX_UNCHANGING_P (exp))
7339c88d 2916 return copy_rtx_unchanging (exp);
052aaaef
RK
2917 return exp;
2918 }
41299f41
TW
2919 else
2920 return newexp;
2921}
2922\f
2923/* This routine is called when an AND of a term with a tree of AND's is
2924 encountered. If the term or its complement is present in the tree, it
2925 can be replaced with TRUE or FALSE, respectively.
2926
2927 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2928 be true and hence are complementary.
2929
2930 There is one special case: If we see
2931 (and (not (eq_attr "att" "v1"))
2932 (eq_attr "att" "v2"))
2933 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2934 replace the term, not anything in the AND tree. So we pass a pointer to
2935 the term. */
2936
2937static rtx
2938simplify_and_tree (exp, pterm, insn_code, insn_index)
2939 rtx exp;
2940 rtx *pterm;
2941 int insn_code, insn_index;
2942{
2943 rtx left, right;
2944 rtx newexp;
2945 rtx temp;
2946 int left_eliminates_term, right_eliminates_term;
2947
2948 if (GET_CODE (exp) == AND)
2949 {
2950 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2951 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2952 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2953 {
3e7b5313 2954 newexp = attr_rtx (GET_CODE (exp), left, right);
41299f41
TW
2955
2956 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2957 }
2958 }
2959
2960 else if (GET_CODE (exp) == IOR)
2961 {
2962 /* For the IOR case, we do the same as above, except that we can
2963 only eliminate `term' if both sides of the IOR would do so. */
2964 temp = *pterm;
2965 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2966 left_eliminates_term = (temp == true_rtx);
2967
2968 temp = *pterm;
2969 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2970 right_eliminates_term = (temp == true_rtx);
2971
2972 if (left_eliminates_term && right_eliminates_term)
2973 *pterm = true_rtx;
2974
2975 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2976 {
3e7b5313 2977 newexp = attr_rtx (GET_CODE (exp), left, right);
41299f41
TW
2978
2979 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2980 }
2981 }
2982
2983 /* Check for simplifications. Do some extra checking here since this
2984 routine is called so many times. */
2985
2986 if (exp == *pterm)
2987 return true_rtx;
2988
2989 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2990 return false_rtx;
2991
2992 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2993 return false_rtx;
2994
2995 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2996 {
2997 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2998 return exp;
2999
3000 if (! strcmp (XSTR (exp, 1), XSTR (*pterm, 1)))
3001 return true_rtx;
3002 else
3003 return false_rtx;
3004 }
3005
3006 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
3007 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
3008 {
3009 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
3010 return exp;
3011
3012 if (! strcmp (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
3013 return false_rtx;
3014 else
3015 return true_rtx;
3016 }
3017
3018 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
3019 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
3020 {
3021 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
3022 return exp;
3023
3024 if (! strcmp (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
3025 return false_rtx;
3026 else
3027 *pterm = true_rtx;
3028 }
3029
3030 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
3031 {
81fd4c6e 3032 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
41299f41
TW
3033 return true_rtx;
3034 }
3035
3036 else if (GET_CODE (exp) == NOT)
3037 {
81fd4c6e 3038 if (attr_equal_p (XEXP (exp, 0), *pterm))
41299f41
TW
3039 return false_rtx;
3040 }
3041
3042 else if (GET_CODE (*pterm) == NOT)
3043 {
81fd4c6e 3044 if (attr_equal_p (XEXP (*pterm, 0), exp))
41299f41
TW
3045 return false_rtx;
3046 }
3047
81fd4c6e 3048 else if (attr_equal_p (exp, *pterm))
41299f41
TW
3049 return true_rtx;
3050
3051 return exp;
3052}
3053\f
6dc42e49 3054/* Similar to `simplify_and_tree', but for IOR trees. */
41299f41
TW
3055
3056static rtx
3057simplify_or_tree (exp, pterm, insn_code, insn_index)
3058 rtx exp;
3059 rtx *pterm;
3060 int insn_code, insn_index;
3061{
3062 rtx left, right;
3063 rtx newexp;
3064 rtx temp;
3065 int left_eliminates_term, right_eliminates_term;
3066
3067 if (GET_CODE (exp) == IOR)
3068 {
3069 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
3070 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
3071 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3072 {
3e7b5313 3073 newexp = attr_rtx (GET_CODE (exp), left, right);
41299f41
TW
3074
3075 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3076 }
3077 }
3078
3079 else if (GET_CODE (exp) == AND)
3080 {
3081 /* For the AND case, we do the same as above, except that we can
3082 only eliminate `term' if both sides of the AND would do so. */
3083 temp = *pterm;
3084 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
3085 left_eliminates_term = (temp == false_rtx);
3086
3087 temp = *pterm;
3088 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
3089 right_eliminates_term = (temp == false_rtx);
3090
3091 if (left_eliminates_term && right_eliminates_term)
3092 *pterm = false_rtx;
3093
3094 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3095 {
3e7b5313 3096 newexp = attr_rtx (GET_CODE (exp), left, right);
41299f41
TW
3097
3098 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3099 }
3100 }
3101
81fd4c6e 3102 if (attr_equal_p (exp, *pterm))
41299f41
TW
3103 return false_rtx;
3104
81fd4c6e 3105 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
41299f41
TW
3106 return true_rtx;
3107
81fd4c6e 3108 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
41299f41
TW
3109 return true_rtx;
3110
3111 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
3112 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3113 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
3114 *pterm = false_rtx;
3115
3116 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
3117 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
3118 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
3119 return false_rtx;
3120
3121 return exp;
3122}
3123\f
3124/* Given an expression, see if it can be simplified for a particular insn
3125 code based on the values of other attributes being tested. This can
3126 eliminate nested get_attr_... calls.
3127
3128 Note that if an endless recursion is specified in the patterns, the
3129 optimization will loop. However, it will do so in precisely the cases where
3130 an infinite recursion loop could occur during compilation. It's better that
3131 it occurs here! */
3132
3133static rtx
3134simplify_test_exp (exp, insn_code, insn_index)
3135 rtx exp;
3136 int insn_code, insn_index;
3137{
3138 rtx left, right;
3139 struct attr_desc *attr;
3140 struct attr_value *av;
3141 struct insn_ent *ie;
3142 int i;
3143 rtx newexp = exp;
81fd4c6e 3144 char *spacer = (char *) obstack_finish (rtl_obstack);
7339c88d 3145
7339c88d 3146 /* Don't re-simplify something we already simplified. */
85093b9c 3147 if (RTX_UNCHANGING_P (exp) || MEM_IN_STRUCT_P (exp))
7339c88d 3148 return exp;
41299f41
TW
3149
3150 switch (GET_CODE (exp))
3151 {
3152 case AND:
61abc2ca 3153 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3715a518
RS
3154 SIMPLIFY_ALTERNATIVE (left);
3155 if (left == false_rtx)
3156 {
a3770a81
RH
3157 if (!ggc_p)
3158 obstack_free (rtl_obstack, spacer);
3715a518
RS
3159 return false_rtx;
3160 }
61abc2ca 3161 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3715a518
RS
3162 SIMPLIFY_ALTERNATIVE (right);
3163 if (left == false_rtx)
3164 {
a3770a81
RH
3165 if (!ggc_p)
3166 obstack_free (rtl_obstack, spacer);
3715a518
RS
3167 return false_rtx;
3168 }
61abc2ca
RS
3169
3170 /* If either side is an IOR and we have (eq_attr "alternative" ..")
3171 present on both sides, apply the distributive law since this will
3172 yield simplifications. */
3173 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
3174 && compute_alternative_mask (left, IOR)
3175 && compute_alternative_mask (right, IOR))
41299f41 3176 {
61abc2ca 3177 if (GET_CODE (left) == IOR)
41299f41 3178 {
61abc2ca
RS
3179 rtx tem = left;
3180 left = right;
3181 right = tem;
3182 }
3183
3184 newexp = attr_rtx (IOR,
3185 attr_rtx (AND, left, XEXP (right, 0)),
3186 attr_rtx (AND, left, XEXP (right, 1)));
3187
3188 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3189 }
3190
3191 /* Try with the term on both sides. */
3192 right = simplify_and_tree (right, &left, insn_code, insn_index);
3193 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3194 left = simplify_and_tree (left, &right, insn_code, insn_index);
3195
3196 if (left == false_rtx || right == false_rtx)
3197 {
a3770a81
RH
3198 if (!ggc_p)
3199 obstack_free (rtl_obstack, spacer);
61abc2ca
RS
3200 return false_rtx;
3201 }
3202 else if (left == true_rtx)
3203 {
85093b9c 3204 return right;
61abc2ca
RS
3205 }
3206 else if (right == true_rtx)
3207 {
85093b9c 3208 return left;
61abc2ca 3209 }
61abc2ca
RS
3210 /* See if all or all but one of the insn's alternatives are specified
3211 in this tree. Optimize if so. */
3212
3213 else if (insn_code >= 0
3214 && (GET_CODE (left) == AND
3215 || (GET_CODE (left) == NOT
3216 && GET_CODE (XEXP (left, 0)) == EQ_ATTR
3217 && XSTR (XEXP (left, 0), 0) == alternative_name)
3218 || GET_CODE (right) == AND
3219 || (GET_CODE (right) == NOT
3220 && GET_CODE (XEXP (right, 0)) == EQ_ATTR
3221 && XSTR (XEXP (right, 0), 0) == alternative_name)))
3222 {
3223 i = compute_alternative_mask (exp, AND);
3224 if (i & ~insn_alternatives[insn_code])
9916c524 3225 fatal ("Invalid alternative specified for pattern number %d",
61abc2ca
RS
3226 insn_index);
3227
0f41302f 3228 /* If all alternatives are excluded, this is false. */
61abc2ca
RS
3229 i ^= insn_alternatives[insn_code];
3230 if (i == 0)
3231 return false_rtx;
3232 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3233 {
3234 /* If just one excluded, AND a comparison with that one to the
3235 front of the tree. The others will be eliminated by
3236 optimization. We do not want to do this if the insn has one
3237 alternative and we have tested none of them! */
3238 left = make_alternative_compare (i);
3239 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3240 newexp = attr_rtx (AND, left, right);
41299f41
TW
3241
3242 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3243 }
3244 }
61abc2ca
RS
3245
3246 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3247 {
3248 newexp = attr_rtx (AND, left, right);
3249 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3250 }
3251 break;
41299f41
TW
3252
3253 case IOR:
61abc2ca 3254 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3715a518
RS
3255 SIMPLIFY_ALTERNATIVE (left);
3256 if (left == true_rtx)
3257 {
a3770a81
RH
3258 if (!ggc_p)
3259 obstack_free (rtl_obstack, spacer);
3715a518
RS
3260 return true_rtx;
3261 }
61abc2ca 3262 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3715a518
RS
3263 SIMPLIFY_ALTERNATIVE (right);
3264 if (right == true_rtx)
3265 {
a3770a81
RH
3266 if (!ggc_p)
3267 obstack_free (rtl_obstack, spacer);
3715a518
RS
3268 return true_rtx;
3269 }
61abc2ca
RS
3270
3271 right = simplify_or_tree (right, &left, insn_code, insn_index);
3272 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3273 left = simplify_or_tree (left, &right, insn_code, insn_index);
3274
3275 if (right == true_rtx || left == true_rtx)
3276 {
a3770a81
RH
3277 if (!ggc_p)
3278 obstack_free (rtl_obstack, spacer);
61abc2ca
RS
3279 return true_rtx;
3280 }
3281 else if (left == false_rtx)
3282 {
85093b9c 3283 return right;
61abc2ca
RS
3284 }
3285 else if (right == false_rtx)
3286 {
85093b9c 3287 return left;
61abc2ca
RS
3288 }
3289
3290 /* Test for simple cases where the distributive law is useful. I.e.,
3291 convert (ior (and (x) (y))
3292 (and (x) (z)))
3293 to (and (x)
3294 (ior (y) (z)))
3295 */
3296
3297 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
81fd4c6e 3298 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
61abc2ca
RS
3299 {
3300 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
3301
3302 left = XEXP (left, 0);
3303 right = newexp;
3304 newexp = attr_rtx (AND, left, right);
3305 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3306 }
3307
3308 /* See if all or all but one of the insn's alternatives are specified
3309 in this tree. Optimize if so. */
3310
3311 else if (insn_code >= 0
3312 && (GET_CODE (left) == IOR
3313 || (GET_CODE (left) == EQ_ATTR
3314 && XSTR (left, 0) == alternative_name)
3315 || GET_CODE (right) == IOR
3316 || (GET_CODE (right) == EQ_ATTR
3317 && XSTR (right, 0) == alternative_name)))
3318 {
3319 i = compute_alternative_mask (exp, IOR);
3320 if (i & ~insn_alternatives[insn_code])
9916c524 3321 fatal ("Invalid alternative specified for pattern number %d",
61abc2ca
RS
3322 insn_index);
3323
0f41302f 3324 /* If all alternatives are included, this is true. */
61abc2ca
RS
3325 i ^= insn_alternatives[insn_code];
3326 if (i == 0)
3327 return true_rtx;
3328 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3329 {
3330 /* If just one excluded, IOR a comparison with that one to the
3331 front of the tree. The others will be eliminated by
3332 optimization. We do not want to do this if the insn has one
3333 alternative and we have tested none of them! */
3334 left = make_alternative_compare (i);
3335 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3336 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
3337
3338 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3339 }
3340 }
3341
3342 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3343 {
3344 newexp = attr_rtx (IOR, left, right);
3345 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3346 }
3347 break;
41299f41
TW
3348
3349 case NOT:
7339c88d 3350 if (GET_CODE (XEXP (exp, 0)) == NOT)
3715a518
RS
3351 {
3352 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
3353 insn_code, insn_index);
3354 SIMPLIFY_ALTERNATIVE (left);
3355 return left;
3356 }
3357
41299f41 3358 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3715a518 3359 SIMPLIFY_ALTERNATIVE (left);
41299f41
TW
3360 if (GET_CODE (left) == NOT)
3361 return XEXP (left, 0);
3362
3363 if (left == false_rtx)
7339c88d 3364 {
a3770a81
RH
3365 if (!ggc_p)
3366 obstack_free (rtl_obstack, spacer);
7339c88d
RS
3367 return true_rtx;
3368 }
41299f41 3369 else if (left == true_rtx)
7339c88d 3370 {
a3770a81
RH
3371 if (!ggc_p)
3372 obstack_free (rtl_obstack, spacer);
7339c88d
RS
3373 return false_rtx;
3374 }
41299f41
TW
3375
3376 /* Try to apply De`Morgan's laws. */
3377 else if (GET_CODE (left) == IOR)
3378 {
3e7b5313
TW
3379 newexp = attr_rtx (AND,
3380 attr_rtx (NOT, XEXP (left, 0)),
3381 attr_rtx (NOT, XEXP (left, 1)));
41299f41
TW
3382
3383 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3384 }
3385 else if (GET_CODE (left) == AND)
3386 {
3e7b5313
TW
3387 newexp = attr_rtx (IOR,
3388 attr_rtx (NOT, XEXP (left, 0)),
3389 attr_rtx (NOT, XEXP (left, 1)));
41299f41
TW
3390
3391 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3392 }
3393 else if (left != XEXP (exp, 0))
3394 {
3e7b5313 3395 newexp = attr_rtx (NOT, left);
41299f41
TW
3396 }
3397 break;
3398
3399 case EQ_ATTR:
3715a518
RS
3400 if (current_alternative_string && XSTR (exp, 0) == alternative_name)
3401 return (XSTR (exp, 1) == current_alternative_string
3402 ? true_rtx : false_rtx);
3403
41299f41
TW
3404 /* Look at the value for this insn code in the specified attribute.
3405 We normally can replace this comparison with the condition that
3406 would give this insn the values being tested for. */
3407 if (XSTR (exp, 0) != alternative_name
3408 && (attr = find_attr (XSTR (exp, 0), 0)) != NULL)
3409 for (av = attr->first_value; av; av = av->next)
3410 for (ie = av->first_insn; ie; ie = ie->next)
3411 if (ie->insn_code == insn_code)
3412 return evaluate_eq_attr (exp, av->value, insn_code, insn_index);
e9a25f70
JL
3413 break;
3414
3415 default:
3416 break;
41299f41
TW
3417 }
3418
3419 /* We have already simplified this expression. Simplifying it again
3420 won't buy anything unless we weren't given a valid insn code
3421 to process (i.e., we are canonicalizing something.). */
45044655 3422 if (insn_code != -2 /* Seems wrong: && current_alternative_string. */
3715a518
RS
3423 && ! RTX_UNCHANGING_P (newexp))
3424 return copy_rtx_unchanging (newexp);
41299f41
TW
3425
3426 return newexp;
3427}
3428\f
3429/* Optimize the attribute lists by seeing if we can determine conditional
3430 values from the known values of other attributes. This will save subroutine
3431 calls during the compilation. */
3432
3433static void
3434optimize_attrs ()
3435{
3436 struct attr_desc *attr;
3437 struct attr_value *av;
b5b6ad46 3438 struct insn_ent *ie;
41299f41
TW
3439 rtx newexp;
3440 int something_changed = 1;
85093b9c
RS
3441 int i;
3442 struct attr_value_list { struct attr_value *av;
3443 struct insn_ent *ie;
3444 struct attr_desc * attr;
3445 struct attr_value_list *next; };
3446 struct attr_value_list **insn_code_values;
1c69865d 3447 struct attr_value_list *ivbuf;
85093b9c
RS
3448 struct attr_value_list *iv;
3449
3450 /* For each insn code, make a list of all the insn_ent's for it,
3451 for all values for all attributes. */
3452
bd1b0893
ILT
3453 if (num_insn_ents == 0)
3454 return;
3455
85093b9c
RS
3456 /* Make 2 extra elements, for "code" values -2 and -1. */
3457 insn_code_values
3458 = (struct attr_value_list **) alloca ((insn_code_number + 2)
3459 * sizeof (struct attr_value_list *));
4c9a05bc 3460 bzero ((char *) insn_code_values,
85093b9c 3461 (insn_code_number + 2) * sizeof (struct attr_value_list *));
4c9a05bc 3462
85093b9c
RS
3463 /* Offset the table address so we can index by -2 or -1. */
3464 insn_code_values += 2;
3465
1c69865d
ILT
3466 /* Allocate the attr_value_list structures using xmalloc rather than
3467 alloca, because using alloca can overflow the maximum permitted
3468 stack limit on SPARC Lynx. */
3469 iv = ivbuf = ((struct attr_value_list *)
3470 xmalloc (num_insn_ents * sizeof (struct attr_value_list)));
3471
3715a518
RS
3472 for (i = 0; i < MAX_ATTRS_INDEX; i++)
3473 for (attr = attrs[i]; attr; attr = attr->next)
3474 for (av = attr->first_value; av; av = av->next)
3475 for (ie = av->first_insn; ie; ie = ie->next)
3476 {
3715a518
RS
3477 iv->attr = attr;
3478 iv->av = av;
3479 iv->ie = ie;
3480 iv->next = insn_code_values[ie->insn_code];
3481 insn_code_values[ie->insn_code] = iv;
1c69865d 3482 iv++;
3715a518 3483 }
41299f41 3484
1c69865d
ILT
3485 /* Sanity check on num_insn_ents. */
3486 if (iv != ivbuf + num_insn_ents)
3487 abort ();
3488
3715a518
RS
3489 /* Process one insn code at a time. */
3490 for (i = -2; i < insn_code_number; i++)
41299f41 3491 {
3715a518
RS
3492 /* Clear the MEM_IN_STRUCT_P flag everywhere relevant.
3493 We use it to mean "already simplified for this insn". */
3494 for (iv = insn_code_values[i]; iv; iv = iv->next)
3495 clear_struct_flag (iv->av->value);
3496
3497 /* Loop until nothing changes for one iteration. */
3498 something_changed = 1;
3499 while (something_changed)
85093b9c 3500 {
3715a518 3501 something_changed = 0;
85093b9c
RS
3502 for (iv = insn_code_values[i]; iv; iv = iv->next)
3503 {
3504 struct obstack *old = rtl_obstack;
3505 char *spacer = (char *) obstack_finish (temp_obstack);
3506
3507 attr = iv->attr;
3508 av = iv->av;
3509 ie = iv->ie;
3510 if (GET_CODE (av->value) != COND)
3511 continue;
3512
3513 rtl_obstack = temp_obstack;
3715a518
RS
3514#if 0 /* This was intended as a speed up, but it was slower. */
3515 if (insn_n_alternatives[ie->insn_code] > 6
3516 && count_sub_rtxs (av->value, 200) >= 200)
3517 newexp = simplify_by_alternatives (av->value, ie->insn_code,
3518 ie->insn_index);
3519 else
3520#endif
3521 newexp = simplify_cond (av->value, ie->insn_code,
3522 ie->insn_index);
3523
85093b9c
RS
3524 rtl_obstack = old;
3525 if (newexp != av->value)
3526 {
3527 newexp = attr_copy_rtx (newexp);
3528 remove_insn_ent (av, ie);
3529 av = get_attr_value (newexp, attr, ie->insn_code);
3530 iv->av = av;
3531 insert_insn_ent (av, ie);
3532 something_changed = 1;
3533 }
a3770a81
RH
3534 if (!ggc_p)
3535 obstack_free (temp_obstack, spacer);
85093b9c
RS
3536 }
3537 }
3538 }
1c69865d
ILT
3539
3540 free (ivbuf);
85093b9c
RS
3541}
3542
b5b6ad46 3543#if 0
3715a518
RS
3544static rtx
3545simplify_by_alternatives (exp, insn_code, insn_index)
3546 rtx exp;
3547 int insn_code, insn_index;
3548{
3549 int i;
3550 int len = insn_n_alternatives[insn_code];
3551 rtx newexp = rtx_alloc (COND);
3552 rtx ultimate;
3553
3554
3555 XVEC (newexp, 0) = rtvec_alloc (len * 2);
3556
3557 /* It will not matter what value we use as the default value
3558 of the new COND, since that default will never be used.
3559 Choose something of the right type. */
3560 for (ultimate = exp; GET_CODE (ultimate) == COND;)
3561 ultimate = XEXP (ultimate, 1);
3562 XEXP (newexp, 1) = ultimate;
3563
3564 for (i = 0; i < insn_n_alternatives[insn_code]; i++)
3565 {
3566 current_alternative_string = attr_numeral (i);
3567 XVECEXP (newexp, 0, i * 2) = make_alternative_compare (1 << i);
3568 XVECEXP (newexp, 0, i * 2 + 1)
3569 = simplify_cond (exp, insn_code, insn_index);
3570 }
3571
3572 current_alternative_string = 0;
3573 return simplify_cond (newexp, insn_code, insn_index);
3574}
b5b6ad46 3575#endif
3715a518 3576\f
72f1215c
TW
3577/* If EXP is a suitable expression, reorganize it by constructing an
3578 equivalent expression that is a COND with the tests being all combinations
3579 of attribute values and the values being simple constants. */
3580
3581static rtx
3582simplify_by_exploding (exp)
3583 rtx exp;
3584{
aefdd5ab 3585 rtx list = 0, link, condexp, defval = NULL_RTX;
72f1215c
TW
3586 struct dimension *space;
3587 rtx *condtest, *condval;
bee757e1 3588 int i, j, total, ndim = 0;
72f1215c
TW
3589 int most_tests, num_marks, new_marks;
3590
3591 /* Locate all the EQ_ATTR expressions. */
bee757e1 3592 if (! find_and_mark_used_attributes (exp, &list, &ndim) || ndim == 0)
72f1215c
TW
3593 {
3594 unmark_used_attributes (list, 0, 0);
3595 return exp;
3596 }
3597
3598 /* Create an attribute space from the list of used attributes. For each
3599 dimension in the attribute space, record the attribute, list of values
3600 used, and number of values used. Add members to the list of values to
3601 cover the domain of the attribute. This makes the expanded COND form
3602 order independent. */
3603
72f1215c
TW
3604 space = (struct dimension *) alloca (ndim * sizeof (struct dimension));
3605
3606 total = 1;
3607 for (ndim = 0; list; ndim++)
3608 {
3609 /* Pull the first attribute value from the list and record that
3610 attribute as another dimension in the attribute space. */
3cce094d 3611 const char *name = XSTR (XEXP (list, 0), 0);
72f1215c
TW
3612 rtx *prev;
3613
3614 if ((space[ndim].attr = find_attr (name, 0)) == 0
3615 || space[ndim].attr->is_numeric)
3616 {
3617 unmark_used_attributes (list, space, ndim);
3618 return exp;
3619 }
3620
3621 /* Add all remaining attribute values that refer to this attribute. */
3622 space[ndim].num_values = 0;
3623 space[ndim].values = 0;
3624 prev = &list;
3625 for (link = list; link; link = *prev)
3626 if (! strcmp (XSTR (XEXP (link, 0), 0), name))
3627 {
3628 space[ndim].num_values++;
3629 *prev = XEXP (link, 1);
3630 XEXP (link, 1) = space[ndim].values;
3631 space[ndim].values = link;
3632 }
3633 else
3634 prev = &XEXP (link, 1);
3635
3636 /* Add sufficient members to the list of values to make the list
3637 mutually exclusive and record the total size of the attribute
3638 space. */
3639 total *= add_values_to_cover (&space[ndim]);
3640 }
3641
3642 /* Sort the attribute space so that the attributes go from non-constant
3643 to constant and from most values to least values. */
3644 for (i = 0; i < ndim; i++)
3645 for (j = ndim - 1; j > i; j--)
3646 if ((space[j-1].attr->is_const && !space[j].attr->is_const)
3647 || space[j-1].num_values < space[j].num_values)
3648 {
3649 struct dimension tmp;
3650 tmp = space[j];
3651 space[j] = space[j-1];
3652 space[j-1] = tmp;
3653 }
3654
3655 /* Establish the initial current value. */
3656 for (i = 0; i < ndim; i++)
3657 space[i].current_value = space[i].values;
3658
3659 condtest = (rtx *) alloca (total * sizeof (rtx));
3660 condval = (rtx *) alloca (total * sizeof (rtx));
3661
3662 /* Expand the tests and values by iterating over all values in the
3663 attribute space. */
3664 for (i = 0;; i++)
3665 {
3666 condtest[i] = test_for_current_value (space, ndim);
3667 condval[i] = simplify_with_current_value (exp, space, ndim);
3668 if (! increment_current_value (space, ndim))
3669 break;
3670 }
3671 if (i != total - 1)
3672 abort ();
3673
3674 /* We are now finished with the original expression. */
3675 unmark_used_attributes (0, space, ndim);
3676
3677 /* Find the most used constant value and make that the default. */
3678 most_tests = -1;
3679 for (i = num_marks = 0; i < total; i++)
3680 if (GET_CODE (condval[i]) == CONST_STRING
3681 && ! MEM_VOLATILE_P (condval[i]))
3682 {
3683 /* Mark the unmarked constant value and count how many are marked. */
3684 MEM_VOLATILE_P (condval[i]) = 1;
3685 for (j = new_marks = 0; j < total; j++)
3686 if (GET_CODE (condval[j]) == CONST_STRING
3687 && MEM_VOLATILE_P (condval[j]))
3688 new_marks++;
3689 if (new_marks - num_marks > most_tests)
3690 {
3691 most_tests = new_marks - num_marks;
3692 defval = condval[i];
3693 }
3694 num_marks = new_marks;
3695 }
3696 /* Clear all the marks. */
3697 for (i = 0; i < total; i++)
3698 MEM_VOLATILE_P (condval[i]) = 0;
3699
3700 /* Give up if nothing is constant. */
3701 if (num_marks == 0)
3702 return exp;
3703
bee757e1
TW
3704 /* If all values are the default, use that. */
3705 if (total == most_tests)
3706 return defval;
3707
72f1215c
TW
3708 /* Make a COND with the most common constant value the default. (A more
3709 complex method where tests with the same value were combined didn't
3710 seem to improve things.) */
3711 condexp = rtx_alloc (COND);
3712 XVEC (condexp, 0) = rtvec_alloc ((total - most_tests) * 2);
3713 XEXP (condexp, 1) = defval;
3714 for (i = j = 0; i < total; i++)
3715 if (condval[i] != defval)
3716 {
3717 XVECEXP (condexp, 0, 2 * j) = condtest[i];
3718 XVECEXP (condexp, 0, 2 * j + 1) = condval[i];
3719 j++;
3720 }
3721
3722 return condexp;
3723}
3724
3725/* Set the MEM_VOLATILE_P flag for all EQ_ATTR expressions in EXP and
3726 verify that EXP can be simplified to a constant term if all the EQ_ATTR
3727 tests have known value. */
3728
3729static int
bee757e1 3730find_and_mark_used_attributes (exp, terms, nterms)
72f1215c 3731 rtx exp, *terms;
bee757e1 3732 int *nterms;
72f1215c
TW
3733{
3734 int i;
3735
3736 switch (GET_CODE (exp))
3737 {
3738 case EQ_ATTR:
3739 if (! MEM_VOLATILE_P (exp))
3740 {
3741 rtx link = rtx_alloc (EXPR_LIST);
3742 XEXP (link, 0) = exp;
3743 XEXP (link, 1) = *terms;
3744 *terms = link;
bee757e1 3745 *nterms += 1;
72f1215c
TW
3746 MEM_VOLATILE_P (exp) = 1;
3747 }
7ee37ba4
RH
3748 return 1;
3749
72f1215c 3750 case CONST_STRING:
5830e3a0 3751 case CONST_INT:
72f1215c
TW
3752 return 1;
3753
3754 case IF_THEN_ELSE:
bee757e1 3755 if (! find_and_mark_used_attributes (XEXP (exp, 2), terms, nterms))
72f1215c
TW
3756 return 0;
3757 case IOR:
3758 case AND:
bee757e1 3759 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
72f1215c
TW
3760 return 0;
3761 case NOT:
bee757e1 3762 if (! find_and_mark_used_attributes (XEXP (exp, 0), terms, nterms))
72f1215c
TW
3763 return 0;
3764 return 1;
3765
3766 case COND:
3767 for (i = 0; i < XVECLEN (exp, 0); i++)
bee757e1 3768 if (! find_and_mark_used_attributes (XVECEXP (exp, 0, i), terms, nterms))
72f1215c 3769 return 0;
bee757e1 3770 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
72f1215c
TW
3771 return 0;
3772 return 1;
72f1215c 3773
e9a25f70
JL
3774 default:
3775 return 0;
3776 }
72f1215c
TW
3777}
3778
3779/* Clear the MEM_VOLATILE_P flag in all EQ_ATTR expressions on LIST and
3780 in the values of the NDIM-dimensional attribute space SPACE. */
3781
3782static void
3783unmark_used_attributes (list, space, ndim)
3784 rtx list;
3785 struct dimension *space;
3786 int ndim;
3787{
3788 rtx link, exp;
3789 int i;
3790
3791 for (i = 0; i < ndim; i++)
3792 unmark_used_attributes (space[i].values, 0, 0);
3793
3794 for (link = list; link; link = XEXP (link, 1))
3795 {
3796 exp = XEXP (link, 0);
3797 if (GET_CODE (exp) == EQ_ATTR)
3798 MEM_VOLATILE_P (exp) = 0;
3799 }
3800}
3801
3802/* Update the attribute dimension DIM so that all values of the attribute
3803 are tested. Return the updated number of values. */
3804
3805static int
3806add_values_to_cover (dim)
3807 struct dimension *dim;
3808{
3809 struct attr_value *av;
3810 rtx exp, link, *prev;
3811 int nalt = 0;
3812
3813 for (av = dim->attr->first_value; av; av = av->next)
3814 if (GET_CODE (av->value) == CONST_STRING)
3815 nalt++;
3816
3817 if (nalt < dim->num_values)
3818 abort ();
3819 else if (nalt == dim->num_values)
3820 ; /* Ok. */
5830e3a0 3821 else if (nalt * 2 < dim->num_values * 3)
72f1215c
TW
3822 {
3823 /* Most all the values of the attribute are used, so add all the unused
3824 values. */
3825 prev = &dim->values;
3826 for (link = dim->values; link; link = *prev)
3827 prev = &XEXP (link, 1);
3828
3829 for (av = dim->attr->first_value; av; av = av->next)
3830 if (GET_CODE (av->value) == CONST_STRING)
3831 {
3832 exp = attr_eq (dim->attr->name, XSTR (av->value, 0));
3833 if (MEM_VOLATILE_P (exp))
3834 continue;
3835
3836 link = rtx_alloc (EXPR_LIST);
3837 XEXP (link, 0) = exp;
3838 XEXP (link, 1) = 0;
3839 *prev = link;
3840 prev = &XEXP (link, 1);
3841 }
3842 dim->num_values = nalt;
3843 }
3844 else
3845 {
3846 rtx orexp = false_rtx;
3847
3848 /* Very few values are used, so compute a mutually exclusive
3849 expression. (We could do this for numeric values if that becomes
3850 important.) */
3851 prev = &dim->values;
3852 for (link = dim->values; link; link = *prev)
3853 {
f75d38a7 3854 orexp = insert_right_side (IOR, orexp, XEXP (link, 0), -2, -2);
72f1215c
TW
3855 prev = &XEXP (link, 1);
3856 }
3857 link = rtx_alloc (EXPR_LIST);
3858 XEXP (link, 0) = attr_rtx (NOT, orexp);
3859 XEXP (link, 1) = 0;
3860 *prev = link;
3861 dim->num_values++;
3862 }
3863 return dim->num_values;
3864}
3865
3866/* Increment the current value for the NDIM-dimensional attribute space SPACE
3867 and return FALSE if the increment overflowed. */
3868
3869static int
3870increment_current_value (space, ndim)
3871 struct dimension *space;
3872 int ndim;
3873{
3874 int i;
3875
3876 for (i = ndim - 1; i >= 0; i--)
3877 {
3878 if ((space[i].current_value = XEXP (space[i].current_value, 1)) == 0)
3879 space[i].current_value = space[i].values;
3880 else
3881 return 1;
3882 }
3883 return 0;
3884}
3885
3886/* Construct an expression corresponding to the current value for the
3887 NDIM-dimensional attribute space SPACE. */
3888
3889static rtx
3890test_for_current_value (space, ndim)
3891 struct dimension *space;
3892 int ndim;
3893{
3894 int i;
3895 rtx exp = true_rtx;
3896
3897 for (i = 0; i < ndim; i++)
f75d38a7
RK
3898 exp = insert_right_side (AND, exp, XEXP (space[i].current_value, 0),
3899 -2, -2);
72f1215c
TW
3900
3901 return exp;
3902}
3903
3904/* Given the current value of the NDIM-dimensional attribute space SPACE,
3905 set the corresponding EQ_ATTR expressions to that value and reduce
3906 the expression EXP as much as possible. On input [and output], all
3907 known EQ_ATTR expressions are set to FALSE. */
3908
3909static rtx
3910simplify_with_current_value (exp, space, ndim)
3911 rtx exp;
3912 struct dimension *space;
3913 int ndim;
3914{
3915 int i;
3916 rtx x;
3917
3918 /* Mark each current value as TRUE. */
3919 for (i = 0; i < ndim; i++)
3920 {
3921 x = XEXP (space[i].current_value, 0);
3922 if (GET_CODE (x) == EQ_ATTR)
3923 MEM_VOLATILE_P (x) = 0;
3924 }
3925
3926 exp = simplify_with_current_value_aux (exp);
3927
3928 /* Change each current value back to FALSE. */
3929 for (i = 0; i < ndim; i++)
3930 {
3931 x = XEXP (space[i].current_value, 0);
3932 if (GET_CODE (x) == EQ_ATTR)
3933 MEM_VOLATILE_P (x) = 1;
3934 }
b5b6ad46
MM
3935
3936 return exp;
72f1215c
TW
3937}
3938
3939/* Reduce the expression EXP based on the MEM_VOLATILE_P settings of
3940 all EQ_ATTR expressions. */
3941
3942static rtx
3943simplify_with_current_value_aux (exp)
3944 rtx exp;
3945{
3946 register int i;
3947 rtx cond;
3948
3949 switch (GET_CODE (exp))
3950 {
3951 case EQ_ATTR:
3952 if (MEM_VOLATILE_P (exp))
3953 return false_rtx;
3954 else
3955 return true_rtx;
3956 case CONST_STRING:
5830e3a0 3957 case CONST_INT:
72f1215c
TW
3958 return exp;
3959
3960 case IF_THEN_ELSE:
3961 cond = simplify_with_current_value_aux (XEXP (exp, 0));
3962 if (cond == true_rtx)
3963 return simplify_with_current_value_aux (XEXP (exp, 1));
3964 else if (cond == false_rtx)
3965 return simplify_with_current_value_aux (XEXP (exp, 2));
3966 else
3967 return attr_rtx (IF_THEN_ELSE, cond,
3968 simplify_with_current_value_aux (XEXP (exp, 1)),
3969 simplify_with_current_value_aux (XEXP (exp, 2)));
3970
3971 case IOR:
3972 cond = simplify_with_current_value_aux (XEXP (exp, 1));
3973 if (cond == true_rtx)
3974 return cond;
3975 else if (cond == false_rtx)
3976 return simplify_with_current_value_aux (XEXP (exp, 0));
3977 else
3978 return attr_rtx (IOR, cond,
3979 simplify_with_current_value_aux (XEXP (exp, 0)));
3980
3981 case AND:
3982 cond = simplify_with_current_value_aux (XEXP (exp, 1));
3983 if (cond == true_rtx)
3984 return simplify_with_current_value_aux (XEXP (exp, 0));
3985 else if (cond == false_rtx)
3986 return cond;
3987 else
3988 return attr_rtx (AND, cond,
3989 simplify_with_current_value_aux (XEXP (exp, 0)));
3990
3991 case NOT:
3992 cond = simplify_with_current_value_aux (XEXP (exp, 0));
3993 if (cond == true_rtx)
3994 return false_rtx;
3995 else if (cond == false_rtx)
3996 return true_rtx;
3997 else
3998 return attr_rtx (NOT, cond);
3999
4000 case COND:
4001 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4002 {
4003 cond = simplify_with_current_value_aux (XVECEXP (exp, 0, i));
4004 if (cond == true_rtx)
4005 return simplify_with_current_value_aux (XVECEXP (exp, 0, i + 1));
4006 else if (cond == false_rtx)
4007 continue;
4008 else
4009 abort (); /* With all EQ_ATTR's of known value, a case should
4010 have been selected. */
4011 }
4012 return simplify_with_current_value_aux (XEXP (exp, 1));
e9a25f70
JL
4013
4014 default:
4015 abort ();
72f1215c 4016 }
72f1215c
TW
4017}
4018\f
85093b9c
RS
4019/* Clear the MEM_IN_STRUCT_P flag in EXP and its subexpressions. */
4020
9a63e81d 4021static void
85093b9c
RS
4022clear_struct_flag (x)
4023 rtx x;
4024{
4025 register int i;
4026 register int j;
4027 register enum rtx_code code;
6f7d635c 4028 register const char *fmt;
85093b9c
RS
4029
4030 MEM_IN_STRUCT_P (x) = 0;
4031 if (RTX_UNCHANGING_P (x))
4032 return;
4033
4034 code = GET_CODE (x);
4035
4036 switch (code)
4037 {
4038 case REG:
4039 case QUEUED:
4040 case CONST_INT:
4041 case CONST_DOUBLE:
4042 case SYMBOL_REF:
4043 case CODE_LABEL:
4044 case PC:
4045 case CC0:
4046 case EQ_ATTR:
0b0316dc 4047 case ATTR_FLAG:
85093b9c 4048 return;
e9a25f70
JL
4049
4050 default:
4051 break;
85093b9c
RS
4052 }
4053
4054 /* Compare the elements. If any pair of corresponding elements
4055 fail to match, return 0 for the whole things. */
4056
4057 fmt = GET_RTX_FORMAT (code);
4058 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4059 {
4060 switch (fmt[i])
4061 {
4062 case 'V':
4063 case 'E':
85093b9c
RS
4064 for (j = 0; j < XVECLEN (x, i); j++)
4065 clear_struct_flag (XVECEXP (x, i, j));
4066 break;
4067
4068 case 'e':
4069 clear_struct_flag (XEXP (x, i));
4070 break;
4071 }
41299f41
TW
4072 }
4073}
3715a518
RS
4074
4075/* Return the number of RTX objects making up the expression X.
38e01259 4076 But if we count more than MAX objects, stop counting. */
3715a518 4077
9a63e81d 4078static int
3715a518
RS
4079count_sub_rtxs (x, max)
4080 rtx x;
4081 int max;
4082{
4083 register int i;
4084 register int j;
4085 register enum rtx_code code;
6f7d635c 4086 register const char *fmt;
3715a518
RS
4087 int total = 0;
4088
4089 code = GET_CODE (x);
4090
4091 switch (code)
4092 {
4093 case REG:
4094 case QUEUED:
4095 case CONST_INT:
4096 case CONST_DOUBLE:
4097 case SYMBOL_REF:
4098 case CODE_LABEL:
4099 case PC:
4100 case CC0:
4101 case EQ_ATTR:
0b0316dc 4102 case ATTR_FLAG:
3715a518 4103 return 1;
e9a25f70
JL
4104
4105 default:
4106 break;
3715a518
RS
4107 }
4108
4109 /* Compare the elements. If any pair of corresponding elements
4110 fail to match, return 0 for the whole things. */
4111
4112 fmt = GET_RTX_FORMAT (code);
4113 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4114 {
4115 if (total >= max)
4116 return total;
4117
4118 switch (fmt[i])
4119 {
4120 case 'V':
4121 case 'E':
4122 for (j = 0; j < XVECLEN (x, i); j++)
4123 total += count_sub_rtxs (XVECEXP (x, i, j), max);
4124 break;
4125
4126 case 'e':
4127 total += count_sub_rtxs (XEXP (x, i), max);
4128 break;
4129 }
4130 }
4131 return total;
4132
4133}
41299f41
TW
4134\f
4135/* Create table entries for DEFINE_ATTR. */
4136
4137static void
4138gen_attr (exp)
4139 rtx exp;
4140{
4141 struct attr_desc *attr;
4142 struct attr_value *av;
3cce094d 4143 const char *name_ptr;
41299f41
TW
4144 char *p;
4145
4146 /* Make a new attribute structure. Check for duplicate by looking at
4147 attr->default_val, since it is initialized by this routine. */
4148 attr = find_attr (XSTR (exp, 0), 1);
4149 if (attr->default_val)
4150 fatal ("Duplicate definition for `%s' attribute", attr->name);
4151
4152 if (*XSTR (exp, 1) == '\0')
7ee37ba4 4153 attr->is_numeric = 1;
41299f41
TW
4154 else
4155 {
4156 name_ptr = XSTR (exp, 1);
4157 while ((p = next_comma_elt (&name_ptr)) != NULL)
4158 {
0e9414fd 4159 av = (struct attr_value *) oballoc (sizeof (struct attr_value));
3e7b5313 4160 av->value = attr_rtx (CONST_STRING, p);
41299f41
TW
4161 av->next = attr->first_value;
4162 attr->first_value = av;
4163 av->first_insn = NULL;
4164 av->num_insns = 0;
4165 av->has_asm_insn = 0;
4166 }
4167 }
4168
3e7b5313
TW
4169 if (GET_CODE (XEXP (exp, 2)) == CONST)
4170 {
4171 attr->is_const = 1;
4172 if (attr->is_numeric)
4173 fatal ("Constant attributes may not take numeric values");
4174 /* Get rid of the CONST node. It is allowed only at top-level. */
4175 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
4176 }
4177
41299f41
TW
4178 if (! strcmp (attr->name, "length") && ! attr->is_numeric)
4179 fatal ("`length' attribute must take numeric values");
4180
0f41302f 4181 /* Set up the default value. */
81fd4c6e 4182 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
41299f41
TW
4183 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
4184}
4185\f
4186/* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
4187 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
4188 number of alternatives as this should be checked elsewhere. */
4189
4190static int
4191count_alternatives (exp)
4192 rtx exp;
4193{
4194 int i, j, n;
6f7d635c 4195 const char *fmt;
41299f41
TW
4196
4197 if (GET_CODE (exp) == MATCH_OPERAND)
4198 return n_comma_elts (XSTR (exp, 2));
4199
4200 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4201 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4202 switch (*fmt++)
4203 {
4204 case 'e':
4205 case 'u':
4206 n = count_alternatives (XEXP (exp, i));
4207 if (n)
4208 return n;
4209 break;
4210
4211 case 'E':
4212 case 'V':
4213 if (XVEC (exp, i) != NULL)
4214 for (j = 0; j < XVECLEN (exp, i); j++)
4215 {
4216 n = count_alternatives (XVECEXP (exp, i, j));
4217 if (n)
4218 return n;
4219 }
4220 }
4221
4222 return 0;
4223}
4224\f
4225/* Returns non-zero if the given expression contains an EQ_ATTR with the
4226 `alternative' attribute. */
4227
4228static int
4229compares_alternatives_p (exp)
4230 rtx exp;
4231{
4232 int i, j;
6f7d635c 4233 const char *fmt;
41299f41
TW
4234
4235 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
4236 return 1;
4237
4238 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4239 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4240 switch (*fmt++)
4241 {
4242 case 'e':
4243 case 'u':
4244 if (compares_alternatives_p (XEXP (exp, i)))
4245 return 1;
4246 break;
4247
4248 case 'E':
4249 for (j = 0; j < XVECLEN (exp, i); j++)
4250 if (compares_alternatives_p (XVECEXP (exp, i, j)))
4251 return 1;
4252 break;
4253 }
4254
4255 return 0;
4256}
4257\f
4258/* Returns non-zero is INNER is contained in EXP. */
4259
4260static int
4261contained_in_p (inner, exp)
4262 rtx inner;
4263 rtx exp;
4264{
4265 int i, j;
6f7d635c 4266 const char *fmt;
41299f41
TW
4267
4268 if (rtx_equal_p (inner, exp))
4269 return 1;
4270
4271 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4272 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4273 switch (*fmt++)
4274 {
4275 case 'e':
4276 case 'u':
4277 if (contained_in_p (inner, XEXP (exp, i)))
4278 return 1;
4279 break;
4280
4281 case 'E':
4282 for (j = 0; j < XVECLEN (exp, i); j++)
4283 if (contained_in_p (inner, XVECEXP (exp, i, j)))
4284 return 1;
4285 break;
4286 }
4287
4288 return 0;
4289}
4290\f
4291/* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
4292
4293static void
4294gen_insn (exp)
4295 rtx exp;
4296{
4297 struct insn_def *id;
4298
0e9414fd 4299 id = (struct insn_def *) oballoc (sizeof (struct insn_def));
41299f41
TW
4300 id->next = defs;
4301 defs = id;
4302 id->def = exp;
4303
4304 switch (GET_CODE (exp))
4305 {
4306 case DEFINE_INSN:
4307 id->insn_code = insn_code_number++;
4308 id->insn_index = insn_index_number++;
4309 id->num_alternatives = count_alternatives (exp);
4310 if (id->num_alternatives == 0)
4311 id->num_alternatives = 1;
4312 id->vec_idx = 4;
4313 break;
4314
4315 case DEFINE_PEEPHOLE:
4316 id->insn_code = insn_code_number++;
4317 id->insn_index = insn_index_number++;
4318 id->num_alternatives = count_alternatives (exp);
4319 if (id->num_alternatives == 0)
4320 id->num_alternatives = 1;
4321 id->vec_idx = 3;
4322 break;
4323
4324 case DEFINE_ASM_ATTRIBUTES:
4325 id->insn_code = -1;
4326 id->insn_index = -1;
4327 id->num_alternatives = 1;
4328 id->vec_idx = 0;
4329 got_define_asm_attributes = 1;
4330 break;
e9a25f70
JL
4331
4332 default:
4333 abort ();
41299f41
TW
4334 }
4335}
4336\f
4337/* Process a DEFINE_DELAY. Validate the vector length, check if annul
4338 true or annul false is specified, and make a `struct delay_desc'. */
4339
4340static void
4341gen_delay (def)
4342 rtx def;
4343{
4344 struct delay_desc *delay;
4345 int i;
4346
4347 if (XVECLEN (def, 1) % 3 != 0)
4348 fatal ("Number of elements in DEFINE_DELAY must be multiple of three.");
4349
4350 for (i = 0; i < XVECLEN (def, 1); i += 3)
4351 {
4352 if (XVECEXP (def, 1, i + 1))
4353 have_annul_true = 1;
4354 if (XVECEXP (def, 1, i + 2))
4355 have_annul_false = 1;
4356 }
4357
0e9414fd 4358 delay = (struct delay_desc *) oballoc (sizeof (struct delay_desc));
41299f41
TW
4359 delay->def = def;
4360 delay->num = ++num_delays;
4361 delay->next = delays;
4362 delays = delay;
4363}
4364\f
4365/* Process a DEFINE_FUNCTION_UNIT.
4366
4367 This gives information about a function unit contained in the CPU.
4368 We fill in a `struct function_unit_op' and a `struct function_unit'
4369 with information used later by `expand_unit'. */
4370
4371static void
4372gen_unit (def)
4373 rtx def;
4374{
4375 struct function_unit *unit;
4376 struct function_unit_op *op;
3cce094d 4377 const char *name = XSTR (def, 0);
bee757e1
TW
4378 int multiplicity = XINT (def, 1);
4379 int simultaneity = XINT (def, 2);
4380 rtx condexp = XEXP (def, 3);
4381 int ready_cost = MAX (XINT (def, 4), 1);
4382 int issue_delay = MAX (XINT (def, 5), 1);
41299f41
TW
4383
4384 /* See if we have already seen this function unit. If so, check that
6dc42e49 4385 the multiplicity and simultaneity values are the same. If not, make
41299f41
TW
4386 a structure for this function unit. */
4387 for (unit = units; unit; unit = unit->next)
bee757e1 4388 if (! strcmp (unit->name, name))
41299f41 4389 {
bee757e1
TW
4390 if (unit->multiplicity != multiplicity
4391 || unit->simultaneity != simultaneity)
41299f41
TW
4392 fatal ("Differing specifications given for `%s' function unit.",
4393 unit->name);
4394 break;
4395 }
4396
4397 if (unit == 0)
4398 {
0e9414fd 4399 unit = (struct function_unit *) oballoc (sizeof (struct function_unit));
bee757e1
TW
4400 unit->name = name;
4401 unit->multiplicity = multiplicity;
4402 unit->simultaneity = simultaneity;
4403 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
41299f41
TW
4404 unit->num = num_units++;
4405 unit->num_opclasses = 0;
4406 unit->condexp = false_rtx;
4407 unit->ops = 0;
4408 unit->next = units;
4409 units = unit;
4410 }
4411
4412 /* Make a new operation class structure entry and initialize it. */
0e9414fd 4413 op = (struct function_unit_op *) oballoc (sizeof (struct function_unit_op));
bee757e1 4414 op->condexp = condexp;
41299f41 4415 op->num = unit->num_opclasses++;
bee757e1
TW
4416 op->ready = ready_cost;
4417 op->issue_delay = issue_delay;
41299f41
TW
4418 op->next = unit->ops;
4419 unit->ops = op;
71d9b493 4420 num_unit_opclasses++;
41299f41 4421
bee757e1 4422 /* Set our issue expression based on whether or not an optional conflict
41299f41
TW
4423 vector was specified. */
4424 if (XVEC (def, 6))
4425 {
4426 /* Compute the IOR of all the specified expressions. */
4427 rtx orexp = false_rtx;
4428 int i;
4429
4430 for (i = 0; i < XVECLEN (def, 6); i++)
f75d38a7 4431 orexp = insert_right_side (IOR, orexp, XVECEXP (def, 6, i), -2, -2);
41299f41 4432
bee757e1
TW
4433 op->conflict_exp = orexp;
4434 extend_range (&unit->issue_delay, 1, issue_delay);
41299f41
TW
4435 }
4436 else
72f1215c 4437 {
bee757e1
TW
4438 op->conflict_exp = true_rtx;
4439 extend_range (&unit->issue_delay, issue_delay, issue_delay);
72f1215c 4440 }
41299f41
TW
4441
4442 /* Merge our conditional into that of the function unit so we can determine
4443 which insns are used by the function unit. */
f75d38a7 4444 unit->condexp = insert_right_side (IOR, unit->condexp, op->condexp, -2, -2);
41299f41
TW
4445}
4446\f
9ec36da5 4447/* Given a piece of RTX, print a C expression to test its truth value.
41299f41
TW
4448 We use AND and IOR both for logical and bit-wise operations, so
4449 interpret them as logical unless they are inside a comparison expression.
71d9b493
RH
4450 The first bit of FLAGS will be non-zero in that case.
4451
4452 Set the second bit of FLAGS to make references to attribute values use
4453 a cached local variable instead of calling a function. */
41299f41
TW
4454
4455static void
71d9b493 4456write_test_expr (exp, flags)
41299f41 4457 rtx exp;
71d9b493 4458 int flags;
41299f41
TW
4459{
4460 int comparison_operator = 0;
4461 RTX_CODE code;
4462 struct attr_desc *attr;
4463
4464 /* In order not to worry about operator precedence, surround our part of
4465 the expression with parentheses. */
4466
4467 printf ("(");
4468 code = GET_CODE (exp);
4469 switch (code)
4470 {
4471 /* Binary operators. */
4472 case EQ: case NE:
4473 case GE: case GT: case GEU: case GTU:
4474 case LE: case LT: case LEU: case LTU:
4475 comparison_operator = 1;
4476
4477 case PLUS: case MINUS: case MULT: case DIV: case MOD:
4478 case AND: case IOR: case XOR:
45620ed4 4479 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
71d9b493 4480 write_test_expr (XEXP (exp, 0), flags | comparison_operator);
41299f41
TW
4481 switch (code)
4482 {
4483 case EQ:
4484 printf (" == ");
4485 break;
4486 case NE:
4487 printf (" != ");
4488 break;
4489 case GE:
4490 printf (" >= ");
4491 break;
4492 case GT:
4493 printf (" > ");
4494 break;
4495 case GEU:
4496 printf (" >= (unsigned) ");
4497 break;
4498 case GTU:
4499 printf (" > (unsigned) ");
4500 break;
4501 case LE:
4502 printf (" <= ");
4503 break;
4504 case LT:
4505 printf (" < ");
4506 break;
4507 case LEU:
4508 printf (" <= (unsigned) ");
4509 break;
4510 case LTU:
4511 printf (" < (unsigned) ");
4512 break;
4513 case PLUS:
4514 printf (" + ");
4515 break;
4516 case MINUS:
4517 printf (" - ");
4518 break;
4519 case MULT:
4520 printf (" * ");
4521 break;
4522 case DIV:
4523 printf (" / ");
4524 break;
4525 case MOD:
412dc348 4526 printf (" %% ");
41299f41
TW
4527 break;
4528 case AND:
71d9b493 4529 if (flags & 1)
41299f41
TW
4530 printf (" & ");
4531 else
4532 printf (" && ");
4533 break;
4534 case IOR:
71d9b493 4535 if (flags & 1)
41299f41
TW
4536 printf (" | ");
4537 else
4538 printf (" || ");
4539 break;
4540 case XOR:
4541 printf (" ^ ");
4542 break;
41299f41
TW
4543 case ASHIFT:
4544 printf (" << ");
4545 break;
4546 case LSHIFTRT:
4547 case ASHIFTRT:
4548 printf (" >> ");
4549 break;
e9a25f70
JL
4550 default:
4551 abort ();
41299f41
TW
4552 }
4553
71d9b493 4554 write_test_expr (XEXP (exp, 1), flags | comparison_operator);
41299f41
TW
4555 break;
4556
4557 case NOT:
4558 /* Special-case (not (eq_attrq "alternative" "x")) */
71d9b493 4559 if (! (flags & 1) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
41299f41
TW
4560 && XSTR (XEXP (exp, 0), 0) == alternative_name)
4561 {
4562 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
4563 break;
4564 }
4565
4566 /* Otherwise, fall through to normal unary operator. */
4567
4568 /* Unary operators. */
4569 case ABS: case NEG:
4570 switch (code)
4571 {
4572 case NOT:
71d9b493 4573 if (flags & 1)
41299f41
TW
4574 printf ("~ ");
4575 else
4576 printf ("! ");
4577 break;
4578 case ABS:
4579 printf ("abs ");
4580 break;
4581 case NEG:
4582 printf ("-");
4583 break;
e9a25f70
JL
4584 default:
4585 abort ();
41299f41
TW
4586 }
4587
71d9b493 4588 write_test_expr (XEXP (exp, 0), flags);
41299f41
TW
4589 break;
4590
4591 /* Comparison test of an attribute with a value. Most of these will
4592 have been removed by optimization. Handle "alternative"
4593 specially and give error if EQ_ATTR present inside a comparison. */
4594 case EQ_ATTR:
71d9b493 4595 if (flags & 1)
41299f41
TW
4596 fatal ("EQ_ATTR not valid inside comparison");
4597
4598 if (XSTR (exp, 0) == alternative_name)
4599 {
4600 printf ("which_alternative == %s", XSTR (exp, 1));
4601 break;
4602 }
4603
4604 attr = find_attr (XSTR (exp, 0), 0);
4605 if (! attr) abort ();
b31a5831
RS
4606
4607 /* Now is the time to expand the value of a constant attribute. */
4608 if (attr->is_const)
4609 {
4610 write_test_expr (evaluate_eq_attr (exp, attr->default_val->value,
dedb78d4 4611 -2, -2),
71d9b493 4612 flags);
b31a5831
RS
4613 }
4614 else
4615 {
71d9b493
RH
4616 if (flags & 2)
4617 printf ("attr_%s", attr->name);
4618 else
4619 printf ("get_attr_%s (insn)", attr->name);
4620 printf (" == ");
4621 write_attr_valueq (attr, XSTR (exp, 1));
b31a5831 4622 }
41299f41
TW
4623 break;
4624
0b0316dc
JL
4625 /* Comparison test of flags for define_delays. */
4626 case ATTR_FLAG:
71d9b493 4627 if (flags & 1)
0b0316dc
JL
4628 fatal ("ATTR_FLAG not valid inside comparison");
4629 printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
4630 break;
4631
41299f41
TW
4632 /* See if an operand matches a predicate. */
4633 case MATCH_OPERAND:
4634 /* If only a mode is given, just ensure the mode matches the operand.
4635 If neither a mode nor predicate is given, error. */
4636 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
4637 {
4638 if (GET_MODE (exp) == VOIDmode)
4639 fatal ("Null MATCH_OPERAND specified as test");
4640 else
4641 printf ("GET_MODE (operands[%d]) == %smode",
4642 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4643 }
4644 else
4645 printf ("%s (operands[%d], %smode)",
4646 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4647 break;
4648
8450a694
JC
4649 case MATCH_INSN:
4650 printf ("%s (insn)", XSTR (exp, 0));
4651 break;
4652
0f41302f 4653 /* Constant integer. */
41299f41 4654 case CONST_INT:
76d31c63 4655 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
41299f41
TW
4656 break;
4657
0f41302f 4658 /* A random C expression. */
41299f41
TW
4659 case SYMBOL_REF:
4660 printf ("%s", XSTR (exp, 0));
4661 break;
4662
4663 /* The address of the branch target. */
4664 case MATCH_DUP:
5ab7138b
DE
4665 printf ("insn_addresses[INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])]",
4666 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
41299f41
TW
4667 break;
4668
41299f41 4669 case PC:
fc470718
R
4670 /* The address of the current insn. We implement this actually as the
4671 address of the current insn for backward branches, but the last
4672 address of the next insn for forward branches, and both with
4673 adjustments that account for the worst-case possible stretching of
4674 intervening alignments between this insn and its destination. */
4675 printf("insn_current_reference_address (insn)");
41299f41
TW
4676 break;
4677
71d9b493
RH
4678 case CONST_STRING:
4679 printf ("%s", XSTR (exp, 0));
4680 break;
4681
4682 case IF_THEN_ELSE:
4683 write_test_expr (XEXP (exp, 0), flags & 2);
4684 printf (" ? ");
4685 write_test_expr (XEXP (exp, 1), flags | 1);
4686 printf (" : ");
4687 write_test_expr (XEXP (exp, 2), flags | 1);
4688 break;
4689
41299f41
TW
4690 default:
4691 fatal ("bad RTX code `%s' in attribute calculation\n",
4692 GET_RTX_NAME (code));
4693 }
4694
4695 printf (")");
4696}
4697\f
4698/* Given an attribute value, return the maximum CONST_STRING argument
7ee37ba4 4699 encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
41299f41
TW
4700
4701static int
7ee37ba4 4702max_attr_value (exp, unknownp)
41299f41 4703 rtx exp;
7ee37ba4 4704 int *unknownp;
41299f41 4705{
7ee37ba4
RH
4706 int current_max;
4707 int i, n;
41299f41 4708
7ee37ba4 4709 switch (GET_CODE (exp))
41299f41 4710 {
7ee37ba4
RH
4711 case CONST_STRING:
4712 current_max = atoi (XSTR (exp, 0));
4713 break;
4714
4715 case COND:
4716 current_max = max_attr_value (XEXP (exp, 1), unknownp);
41299f41
TW
4717 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4718 {
7ee37ba4 4719 n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
41299f41
TW
4720 if (n > current_max)
4721 current_max = n;
4722 }
7ee37ba4 4723 break;
41299f41 4724
7ee37ba4
RH
4725 case IF_THEN_ELSE:
4726 current_max = max_attr_value (XEXP (exp, 1), unknownp);
4727 n = max_attr_value (XEXP (exp, 2), unknownp);
41299f41
TW
4728 if (n > current_max)
4729 current_max = n;
7ee37ba4 4730 break;
41299f41 4731
7ee37ba4
RH
4732 default:
4733 *unknownp = 1;
4734 current_max = INT_MAX;
4735 break;
bee757e1
TW
4736 }
4737
41299f41
TW
4738 return current_max;
4739}
fc470718
R
4740
4741/* Given an attribute value, return the result of ORing together all
7ee37ba4
RH
4742 CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
4743 if the numeric value is not known. */
fc470718
R
4744
4745static int
7ee37ba4 4746or_attr_value (exp, unknownp)
fc470718 4747 rtx exp;
7ee37ba4 4748 int *unknownp;
fc470718 4749{
7ee37ba4 4750 int current_or;
fc470718
R
4751 int i;
4752
7ee37ba4 4753 switch (GET_CODE (exp))
fc470718 4754 {
7ee37ba4
RH
4755 case CONST_STRING:
4756 current_or = atoi (XSTR (exp, 0));
4757 break;
4758
4759 case COND:
4760 current_or = or_attr_value (XEXP (exp, 1), unknownp);
fc470718 4761 for (i = 0; i < XVECLEN (exp, 0); i += 2)
7ee37ba4
RH
4762 current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
4763 break;
fc470718 4764
7ee37ba4
RH
4765 case IF_THEN_ELSE:
4766 current_or = or_attr_value (XEXP (exp, 1), unknownp);
4767 current_or |= or_attr_value (XEXP (exp, 2), unknownp);
4768 break;
fc470718 4769
7ee37ba4
RH
4770 default:
4771 *unknownp = 1;
4772 current_or = -1;
4773 break;
fc470718
R
4774 }
4775
fc470718
R
4776 return current_or;
4777}
41299f41
TW
4778\f
4779/* Scan an attribute value, possibly a conditional, and record what actions
4780 will be required to do any conditional tests in it.
4781
4782 Specifically, set
4783 `must_extract' if we need to extract the insn operands
4784 `must_constrain' if we must compute `which_alternative'
4785 `address_used' if an address expression was used
d7c665bf 4786 `length_used' if an (eq_attr "length" ...) was used
41299f41
TW
4787 */
4788
4789static void
4790walk_attr_value (exp)
4791 rtx exp;
4792{
4793 register int i, j;
6f7d635c 4794 register const char *fmt;
41299f41
TW
4795 RTX_CODE code;
4796
4797 if (exp == NULL)
4798 return;
4799
4800 code = GET_CODE (exp);
4801 switch (code)
4802 {
4803 case SYMBOL_REF:
3e7b5313
TW
4804 if (! RTX_UNCHANGING_P (exp))
4805 /* Since this is an arbitrary expression, it can look at anything.
4806 However, constant expressions do not depend on any particular
4807 insn. */
4808 must_extract = must_constrain = 1;
41299f41
TW
4809 return;
4810
4811 case MATCH_OPERAND:
4812 must_extract = 1;
4813 return;
4814
4815 case EQ_ATTR:
4816 if (XSTR (exp, 0) == alternative_name)
4817 must_extract = must_constrain = 1;
d7c665bf
RK
4818 else if (strcmp (XSTR (exp, 0), "length") == 0)
4819 length_used = 1;
41299f41
TW
4820 return;
4821
4822 case MATCH_DUP:
426cd2f4
RK
4823 must_extract = 1;
4824 address_used = 1;
4825 return;
4826
41299f41
TW
4827 case PC:
4828 address_used = 1;
4829 return;
0b0316dc
JL
4830
4831 case ATTR_FLAG:
4832 return;
e9a25f70
JL
4833
4834 default:
4835 break;
41299f41
TW
4836 }
4837
4838 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
4839 switch (*fmt++)
4840 {
4841 case 'e':
4842 case 'u':
4843 walk_attr_value (XEXP (exp, i));
4844 break;
4845
4846 case 'E':
4847 if (XVEC (exp, i) != NULL)
4848 for (j = 0; j < XVECLEN (exp, i); j++)
4849 walk_attr_value (XVECEXP (exp, i, j));
4850 break;
4851 }
4852}
4853\f
4854/* Write out a function to obtain the attribute for a given INSN. */
4855
4856static void
4857write_attr_get (attr)
4858 struct attr_desc *attr;
4859{
4860 struct attr_value *av, *common_av;
4861
4862 /* Find the most used attribute value. Handle that as the `default' of the
0f41302f 4863 switch we will generate. */
41299f41
TW
4864 common_av = find_most_used (attr);
4865
69277eec
KG
4866 /* Write out prototype of function. */
4867 if (!attr->is_numeric)
4868 printf ("extern enum attr_%s ", attr->name);
4869 else if (attr->unsigned_p)
4870 printf ("extern unsigned int ");
4871 else
4872 printf ("extern int ");
4873 /* If the attribute name starts with a star, the remainder is the name of
4874 the subroutine to use, instead of `get_attr_...'. */
4875 if (attr->name[0] == '*')
a94ae8f5 4876 printf ("%s PARAMS ((rtx));\n", &attr->name[1]);
69277eec 4877 else
a94ae8f5 4878 printf ("get_attr_%s PARAMS ((%s));\n", attr->name,
69277eec
KG
4879 (attr->is_const ? "void" : "rtx"));
4880
41299f41
TW
4881 /* Write out start of function, then all values with explicit `case' lines,
4882 then a `default', then the value with the most uses. */
bee757e1 4883 if (!attr->is_numeric)
41299f41 4884 printf ("enum attr_%s\n", attr->name);
bee757e1
TW
4885 else if (attr->unsigned_p)
4886 printf ("unsigned int\n");
4887 else
4888 printf ("int\n");
41299f41
TW
4889
4890 /* If the attribute name starts with a star, the remainder is the name of
4891 the subroutine to use, instead of `get_attr_...'. */
4892 if (attr->name[0] == '*')
4893 printf ("%s (insn)\n", &attr->name[1]);
3e7b5313 4894 else if (attr->is_const == 0)
41299f41 4895 printf ("get_attr_%s (insn)\n", attr->name);
3e7b5313
TW
4896 else
4897 {
4898 printf ("get_attr_%s ()\n", attr->name);
4899 printf ("{\n");
4900
4901 for (av = attr->first_value; av; av = av->next)
4902 if (av->num_insns != 0)
4903 write_attr_set (attr, 2, av->value, "return", ";",
4904 true_rtx, av->first_insn->insn_code,
4905 av->first_insn->insn_index);
4906
4907 printf ("}\n\n");
4908 return;
4909 }
71d9b493 4910
41299f41
TW
4911 printf (" rtx insn;\n");
4912 printf ("{\n");
41299f41 4913
71d9b493
RH
4914 if (GET_CODE (common_av->value) == FFS)
4915 {
4916 rtx p = XEXP (common_av->value, 0);
41299f41 4917
71d9b493
RH
4918 /* No need to emit code to abort if the insn is unrecognized; the
4919 other get_attr_foo functions will do that when we call them. */
4920
4921 write_toplevel_expr (p);
4922
4923 printf ("\n if (accum && accum == (accum & -accum))\n");
4924 printf (" {\n");
4925 printf (" int i;\n");
4926 printf (" for (i = 0; accum >>= 1; ++i) continue;\n");
4927 printf (" accum = i;\n");
4928 printf (" }\n else\n");
4929 printf (" accum = ~accum;\n");
4930 printf (" return accum;\n}\n\n");
4931 }
4932 else
4933 {
4934 printf (" switch (recog_memoized (insn))\n");
4935 printf (" {\n");
4936
4937 for (av = attr->first_value; av; av = av->next)
4938 if (av != common_av)
4939 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
4940
4941 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
4942 printf (" }\n}\n\n");
4943 }
41299f41
TW
4944}
4945\f
4946/* Given an AND tree of known true terms (because we are inside an `if' with
4947 that as the condition or are in an `else' clause) and an expression,
4948 replace any known true terms with TRUE. Use `simplify_and_tree' to do
4949 the bulk of the work. */
4950
4951static rtx
4952eliminate_known_true (known_true, exp, insn_code, insn_index)
4953 rtx known_true;
4954 rtx exp;
4955 int insn_code, insn_index;
4956{
4957 rtx term;
4958
61abc2ca
RS
4959 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
4960
4961 if (GET_CODE (known_true) == AND)
4962 {
4963 exp = eliminate_known_true (XEXP (known_true, 0), exp,
4964 insn_code, insn_index);
4965 exp = eliminate_known_true (XEXP (known_true, 1), exp,
4966 insn_code, insn_index);
4967 }
4968 else
4969 {
4970 term = known_true;
4971 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
4972 }
4973
4974 return exp;
41299f41
TW
4975}
4976\f
4977/* Write out a series of tests and assignment statements to perform tests and
4978 sets of an attribute value. We are passed an indentation amount and prefix
4979 and suffix strings to write around each attribute value (e.g., "return"
4980 and ";"). */
4981
4982static void
4983write_attr_set (attr, indent, value, prefix, suffix, known_true,
4984 insn_code, insn_index)
4985 struct attr_desc *attr;
4986 int indent;
4987 rtx value;
85fda1eb
KG
4988 const char *prefix;
4989 const char *suffix;
41299f41
TW
4990 rtx known_true;
4991 int insn_code, insn_index;
4992{
7ee37ba4 4993 if (GET_CODE (value) == COND)
41299f41
TW
4994 {
4995 /* Assume the default value will be the default of the COND unless we
4996 find an always true expression. */
4997 rtx default_val = XEXP (value, 1);
4998 rtx our_known_true = known_true;
4999 rtx newexp;
5000 int first_if = 1;
5001 int i;
5002
5003 for (i = 0; i < XVECLEN (value, 0); i += 2)
5004 {
5005 rtx testexp;
5006 rtx inner_true;
5007
5008 testexp = eliminate_known_true (our_known_true,
5009 XVECEXP (value, 0, i),
5010 insn_code, insn_index);
3e7b5313 5011 newexp = attr_rtx (NOT, testexp);
41299f41
TW
5012 newexp = insert_right_side (AND, our_known_true, newexp,
5013 insn_code, insn_index);
5014
5015 /* If the test expression is always true or if the next `known_true'
5016 expression is always false, this is the last case, so break
5017 out and let this value be the `else' case. */
5018 if (testexp == true_rtx || newexp == false_rtx)
5019 {
5020 default_val = XVECEXP (value, 0, i + 1);
5021 break;
5022 }
5023
5024 /* Compute the expression to pass to our recursive call as being
5025 known true. */
5026 inner_true = insert_right_side (AND, our_known_true,
5027 testexp, insn_code, insn_index);
5028
5029 /* If this is always false, skip it. */
5030 if (inner_true == false_rtx)
5031 continue;
5032
5033 write_indent (indent);
5034 printf ("%sif ", first_if ? "" : "else ");
5035 first_if = 0;
5036 write_test_expr (testexp, 0);
5037 printf ("\n");
5038 write_indent (indent + 2);
5039 printf ("{\n");
5040
5041 write_attr_set (attr, indent + 4,
5042 XVECEXP (value, 0, i + 1), prefix, suffix,
5043 inner_true, insn_code, insn_index);
5044 write_indent (indent + 2);
5045 printf ("}\n");
5046 our_known_true = newexp;
5047 }
5048
5049 if (! first_if)
5050 {
5051 write_indent (indent);
5052 printf ("else\n");
5053 write_indent (indent + 2);
5054 printf ("{\n");
5055 }
5056
5057 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
5058 prefix, suffix, our_known_true, insn_code, insn_index);
5059
5060 if (! first_if)
5061 {
5062 write_indent (indent + 2);
5063 printf ("}\n");
5064 }
5065 }
5066 else
7ee37ba4
RH
5067 {
5068 write_indent (indent);
5069 printf ("%s ", prefix);
5070 write_attr_value (attr, value);
5071 printf ("%s\n", suffix);
5072 }
41299f41
TW
5073}
5074\f
5075/* Write out the computation for one attribute value. */
5076
5077static void
f75d38a7
RK
5078write_attr_case (attr, av, write_case_lines, prefix, suffix, indent,
5079 known_true)
41299f41
TW
5080 struct attr_desc *attr;
5081 struct attr_value *av;
5082 int write_case_lines;
85fda1eb 5083 const char *prefix, *suffix;
41299f41
TW
5084 int indent;
5085 rtx known_true;
5086{
5087 struct insn_ent *ie;
5088
5089 if (av->num_insns == 0)
5090 return;
5091
5092 if (av->has_asm_insn)
5093 {
5094 write_indent (indent);
5095 printf ("case -1:\n");
5096 write_indent (indent + 2);
5097 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
5098 write_indent (indent + 2);
5099 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
5100 write_indent (indent + 2);
5101 printf (" fatal_insn_not_found (insn);\n");
5102 }
5103
5104 if (write_case_lines)
5105 {
5106 for (ie = av->first_insn; ie; ie = ie->next)
5107 if (ie->insn_code != -1)
5108 {
5109 write_indent (indent);
5110 printf ("case %d:\n", ie->insn_code);
5111 }
5112 }
5113 else
5114 {
5115 write_indent (indent);
5116 printf ("default:\n");
5117 }
5118
d7c665bf 5119 /* See what we have to do to output this value. */
41299f41
TW
5120 must_extract = must_constrain = address_used = 0;
5121 walk_attr_value (av->value);
5122
5123 if (must_extract)
5124 {
5125 write_indent (indent + 2);
0eadeb15 5126 printf ("extract_insn (insn);\n");
41299f41
TW
5127 }
5128
5129 if (must_constrain)
5130 {
41299f41 5131 write_indent (indent + 2);
0eadeb15 5132 printf ("if (! constrain_operands (reload_completed))\n");
41299f41
TW
5133 write_indent (indent + 2);
5134 printf (" fatal_insn_not_found (insn);\n");
41299f41
TW
5135 }
5136
5137 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
5138 known_true, av->first_insn->insn_code,
5139 av->first_insn->insn_index);
5140
5141 if (strncmp (prefix, "return", 6))
5142 {
5143 write_indent (indent + 2);
5144 printf ("break;\n");
5145 }
5146 printf ("\n");
5147}
5148\f
71d9b493
RH
5149/* Search for uses of non-const attributes and write code to cache them. */
5150
5151static int
5152write_expr_attr_cache (p, attr)
5153 rtx p;
5154 struct attr_desc *attr;
5155{
6f7d635c 5156 const char *fmt;
71d9b493
RH
5157 int i, ie, j, je;
5158
5159 if (GET_CODE (p) == EQ_ATTR)
5160 {
5161 if (XSTR (p, 0) != attr->name)
5162 return 0;
5163
5164 if (!attr->is_numeric)
5165 printf (" register enum attr_%s ", attr->name);
5166 else if (attr->unsigned_p)
5167 printf (" register unsigned int ");
5168 else
5169 printf (" register int ");
5170
5171 printf ("attr_%s = get_attr_%s (insn);\n", attr->name, attr->name);
5172 return 1;
5173 }
5174
5175 fmt = GET_RTX_FORMAT (GET_CODE (p));
5176 ie = GET_RTX_LENGTH (GET_CODE (p));
5177 for (i = 0; i < ie; i++)
5178 {
5179 switch (*fmt++)
5180 {
5181 case 'e':
5182 if (write_expr_attr_cache (XEXP (p, i), attr))
5183 return 1;
5184 break;
5185
5186 case 'E':
5187 je = XVECLEN (p, i);
5188 for (j = 0; j < je; ++j)
5189 if (write_expr_attr_cache (XVECEXP (p, i, j), attr))
5190 return 1;
5191 break;
5192 }
5193 }
5194
5195 return 0;
5196}
5197
5198/* Evaluate an expression at top level. A front end to write_test_expr,
5199 in which we cache attribute values and break up excessively large
5200 expressions to cater to older compilers. */
5201
5202static void
5203write_toplevel_expr (p)
5204 rtx p;
5205{
5206 struct attr_desc *attr;
5207 int i;
5208
5209 for (i = 0; i < MAX_ATTRS_INDEX; ++i)
5210 for (attr = attrs[i]; attr ; attr = attr->next)
5211 if (!attr->is_const)
5212 write_expr_attr_cache (p, attr);
5213
5214 printf(" register unsigned long accum = 0;\n\n");
5215
5216 while (GET_CODE (p) == IOR)
5217 {
5218 rtx e;
5219 if (GET_CODE (XEXP (p, 0)) == IOR)
5220 e = XEXP (p, 1), p = XEXP (p, 0);
5221 else
5222 e = XEXP (p, 0), p = XEXP (p, 1);
5223
5224 printf (" accum |= ");
5225 write_test_expr (e, 3);
5226 printf (";\n");
5227 }
5228 printf (" accum |= ");
5229 write_test_expr (p, 3);
5230 printf (";\n");
5231}
5232\f
41299f41
TW
5233/* Utilities to write names in various forms. */
5234
6f6074ea
MM
5235static void
5236write_unit_name (prefix, num, suffix)
85fda1eb 5237 const char *prefix;
6f6074ea 5238 int num;
85fda1eb 5239 const char *suffix;
6f6074ea
MM
5240{
5241 struct function_unit *unit;
5242
5243 for (unit = units; unit; unit = unit->next)
5244 if (unit->num == num)
5245 {
5246 printf ("%s%s%s", prefix, unit->name, suffix);
5247 return;
5248 }
5249
5250 printf ("%s<unknown>%s", prefix, suffix);
5251}
5252
41299f41
TW
5253static void
5254write_attr_valueq (attr, s)
5255 struct attr_desc *attr;
3cce094d 5256 const char *s;
41299f41
TW
5257{
5258 if (attr->is_numeric)
bee757e1 5259 {
6f6074ea
MM
5260 int num = atoi (s);
5261
5262 printf ("%d", num);
5263
5264 /* Make the blockage range values and function units used values easier
5265 to read. */
5266 if (attr->func_units_p)
5267 {
5268 if (num == -1)
5269 printf (" /* units: none */");
5270 else if (num >= 0)
5271 write_unit_name (" /* units: ", num, " */");
5272 else
5273 {
5274 int i;
85fda1eb 5275 const char *sep = " /* units: ";
6f6074ea
MM
5276 for (i = 0, num = ~num; num; i++, num >>= 1)
5277 if (num & 1)
5278 {
5279 write_unit_name (sep, i, (num == 1) ? " */" : "");
5280 sep = ", ";
5281 }
5282 }
5283 }
5284
5285 else if (attr->blockage_p)
5286 printf (" /* min %d, max %d */", num >> (HOST_BITS_PER_INT / 2),
5287 num & ((1 << (HOST_BITS_PER_INT / 2)) - 1));
5288
5289 else if (num > 9 || num < 0)
5290 printf (" /* 0x%x */", num);
bee757e1 5291 }
41299f41
TW
5292 else
5293 {
5294 write_upcase (attr->name);
5295 printf ("_");
5296 write_upcase (s);
5297 }
5298}
5299
5300static void
5301write_attr_value (attr, value)
5302 struct attr_desc *attr;
5303 rtx value;
5304{
7ee37ba4
RH
5305 int op;
5306
5307 switch (GET_CODE (value))
5308 {
5309 case CONST_STRING:
5310 write_attr_valueq (attr, XSTR (value, 0));
5311 break;
5312
5313 case SYMBOL_REF:
5314 fputs (XSTR (value, 0), stdout);
5315 break;
5316
5317 case ATTR:
5318 {
5319 struct attr_desc *attr2 = find_attr (XSTR (value, 0), 0);
5320 printf ("get_attr_%s (%s)", attr2->name,
5321 (attr2->is_const ? "" : "insn"));
5322 }
5323 break;
41299f41 5324
7ee37ba4
RH
5325 case PLUS:
5326 op = '+';
5327 goto do_operator;
5328 case MINUS:
5329 op = '-';
5330 goto do_operator;
5331 case MULT:
5332 op = '*';
5333 goto do_operator;
5334 case DIV:
5335 op = '/';
5336 goto do_operator;
5337 case MOD:
5338 op = '%';
5339 goto do_operator;
5340
5341 do_operator:
5342 write_attr_value (attr, XEXP (value, 0));
5343 putchar (' ');
5344 putchar (op);
5345 putchar (' ');
5346 write_attr_value (attr, XEXP (value, 1));
5347 break;
5348
5349 default:
5350 abort ();
5351 }
41299f41
TW
5352}
5353
5354static void
5355write_upcase (str)
92a438d1 5356 const char *str;
41299f41
TW
5357{
5358 while (*str)
92a438d1
KG
5359 {
5360 /* The argument of TOUPPER should not have side effects. */
5361 putchar (TOUPPER(*str));
5362 str++;
5363 }
41299f41
TW
5364}
5365
5366static void
5367write_indent (indent)
5368 int indent;
5369{
5370 for (; indent > 8; indent -= 8)
5371 printf ("\t");
5372
5373 for (; indent; indent--)
5374 printf (" ");
5375}
5376\f
5377/* Write a subroutine that is given an insn that requires a delay slot, a
5378 delay slot ordinal, and a candidate insn. It returns non-zero if the
5379 candidate can be placed in the specified delay slot of the insn.
5380
5381 We can write as many as three subroutines. `eligible_for_delay'
5382 handles normal delay slots, `eligible_for_annul_true' indicates that
5383 the specified insn can be annulled if the branch is true, and likewise
5384 for `eligible_for_annul_false'.
5385
6dc42e49 5386 KIND is a string distinguishing these three cases ("delay", "annul_true",
41299f41
TW
5387 or "annul_false"). */
5388
5389static void
5390write_eligible_delay (kind)
85fda1eb 5391 const char *kind;
41299f41
TW
5392{
5393 struct delay_desc *delay;
5394 int max_slots;
5395 char str[50];
5396 struct attr_desc *attr;
5397 struct attr_value *av, *common_av;
5398 int i;
5399
5400 /* Compute the maximum number of delay slots required. We use the delay
5401 ordinal times this number plus one, plus the slot number as an index into
5402 the appropriate predicate to test. */
5403
5404 for (delay = delays, max_slots = 0; delay; delay = delay->next)
5405 if (XVECLEN (delay->def, 1) / 3 > max_slots)
5406 max_slots = XVECLEN (delay->def, 1) / 3;
5407
5408 /* Write function prelude. */
5409
5410 printf ("int\n");
0b0316dc
JL
5411 printf ("eligible_for_%s (delay_insn, slot, candidate_insn, flags)\n",
5412 kind);
41299f41
TW
5413 printf (" rtx delay_insn;\n");
5414 printf (" int slot;\n");
5415 printf (" rtx candidate_insn;\n");
69277eec 5416 printf (" int flags ATTRIBUTE_UNUSED;\n");
41299f41
TW
5417 printf ("{\n");
5418 printf (" rtx insn;\n");
5419 printf ("\n");
5420 printf (" if (slot >= %d)\n", max_slots);
5421 printf (" abort ();\n");
5422 printf ("\n");
5423
5424 /* If more than one delay type, find out which type the delay insn is. */
5425
5426 if (num_delays > 1)
5427 {
412dc348 5428 attr = find_attr ("*delay_type", 0);
41299f41
TW
5429 if (! attr) abort ();
5430 common_av = find_most_used (attr);
5431
5432 printf (" insn = delay_insn;\n");
5433 printf (" switch (recog_memoized (insn))\n");
5434 printf (" {\n");
5435
5436 sprintf (str, " * %d;\n break;", max_slots);
5437 for (av = attr->first_value; av; av = av->next)
5438 if (av != common_av)
5439 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
5440
5441 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
5442 printf (" }\n\n");
5443
5444 /* Ensure matched. Otherwise, shouldn't have been called. */
5445 printf (" if (slot < %d)\n", max_slots);
5446 printf (" abort ();\n\n");
5447 }
5448
5449 /* If just one type of delay slot, write simple switch. */
5450 if (num_delays == 1 && max_slots == 1)
5451 {
5452 printf (" insn = candidate_insn;\n");
5453 printf (" switch (recog_memoized (insn))\n");
5454 printf (" {\n");
5455
5456 attr = find_attr ("*delay_1_0", 0);
5457 if (! attr) abort ();
5458 common_av = find_most_used (attr);
5459
5460 for (av = attr->first_value; av; av = av->next)
5461 if (av != common_av)
5462 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
5463
5464 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
5465 printf (" }\n");
5466 }
5467
5468 else
5469 {
5470 /* Write a nested CASE. The first indicates which condition we need to
5471 test, and the inner CASE tests the condition. */
5472 printf (" insn = candidate_insn;\n");
5473 printf (" switch (slot)\n");
5474 printf (" {\n");
5475
5476 for (delay = delays; delay; delay = delay->next)
5477 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
5478 {
5479 printf (" case %d:\n",
5480 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
5481 printf (" switch (recog_memoized (insn))\n");
5482 printf ("\t{\n");
5483
5484 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
5485 attr = find_attr (str, 0);
5486 if (! attr) abort ();
5487 common_av = find_most_used (attr);
5488
5489 for (av = attr->first_value; av; av = av->next)
5490 if (av != common_av)
5491 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
5492
5493 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
5494 printf (" }\n");
5495 }
5496
5497 printf (" default:\n");
5498 printf (" abort ();\n");
5499 printf (" }\n");
5500 }
5501
5502 printf ("}\n\n");
5503}
5504\f
5505/* Write routines to compute conflict cost for function units. Then write a
5506 table describing the available function units. */
5507
5508static void
5509write_function_unit_info ()
5510{
5511 struct function_unit *unit;
41299f41
TW
5512 int i;
5513
5514 /* Write out conflict routines for function units. Don't bother writing
bee757e1 5515 one if there is only one issue delay value. */
41299f41
TW
5516
5517 for (unit = units; unit; unit = unit->next)
5518 {
bee757e1
TW
5519 if (unit->needs_blockage_function)
5520 write_complex_function (unit, "blockage", "block");
72f1215c
TW
5521
5522 /* If the minimum and maximum conflict costs are the same, there
5523 is only one value, so we don't need a function. */
bee757e1 5524 if (! unit->needs_conflict_function)
41299f41 5525 {
bee757e1 5526 unit->default_cost = make_numeric_value (unit->issue_delay.max);
72f1215c 5527 continue;
41299f41
TW
5528 }
5529
5530 /* The function first computes the case from the candidate insn. */
41299f41 5531 unit->default_cost = make_numeric_value (0);
bee757e1 5532 write_complex_function (unit, "conflict_cost", "cost");
41299f41
TW
5533 }
5534
5535 /* Now that all functions have been written, write the table describing
6dc42e49 5536 the function units. The name is included for documentation purposes
41299f41
TW
5537 only. */
5538
5539 printf ("struct function_unit_desc function_units[] = {\n");
5540
72f1215c
TW
5541 /* Write out the descriptions in numeric order, but don't force that order
5542 on the list. Doing so increases the runtime of genattrtab.c. */
5543 for (i = 0; i < num_units; i++)
41299f41 5544 {
72f1215c
TW
5545 for (unit = units; unit; unit = unit->next)
5546 if (unit->num == i)
5547 break;
5548
bee757e1 5549 printf (" {\"%s\", %d, %d, %d, %s, %d, %s_unit_ready_cost, ",
41299f41 5550 unit->name, 1 << unit->num, unit->multiplicity,
72f1215c 5551 unit->simultaneity, XSTR (unit->default_cost, 0),
bee757e1 5552 unit->issue_delay.max, unit->name);
41299f41
TW
5553
5554 if (unit->needs_conflict_function)
bee757e1
TW
5555 printf ("%s_unit_conflict_cost, ", unit->name);
5556 else
5557 printf ("0, ");
5558
5559 printf ("%d, ", unit->max_blockage);
5560
5561 if (unit->needs_range_function)
5562 printf ("%s_unit_blockage_range, ", unit->name);
5563 else
5564 printf ("0, ");
5565
5566 if (unit->needs_blockage_function)
5567 printf ("%s_unit_blockage", unit->name);
41299f41
TW
5568 else
5569 printf ("0");
5570
5571 printf ("}, \n");
5572 }
5573
5574 printf ("};\n\n");
5575}
bee757e1
TW
5576
5577static void
5578write_complex_function (unit, name, connection)
5579 struct function_unit *unit;
85fda1eb 5580 const char *name, *connection;
bee757e1
TW
5581{
5582 struct attr_desc *case_attr, *attr;
5583 struct attr_value *av, *common_av;
5584 rtx value;
5585 char *str;
5586 int using_case;
5587 int i;
5588
a94ae8f5 5589 printf ("static int %s_unit_%s PARAMS ((rtx, rtx));\n", unit->name, name);
bee757e1
TW
5590 printf ("static int\n");
5591 printf ("%s_unit_%s (executing_insn, candidate_insn)\n",
5592 unit->name, name);
5593 printf (" rtx executing_insn;\n");
5594 printf (" rtx candidate_insn;\n");
5595 printf ("{\n");
5596 printf (" rtx insn;\n");
5597 printf (" int casenum;\n\n");
cb1520bc 5598 printf (" insn = executing_insn;\n");
bee757e1
TW
5599 printf (" switch (recog_memoized (insn))\n");
5600 printf (" {\n");
5601
5602 /* Write the `switch' statement to get the case value. */
5603 str = (char *) alloca (strlen (unit->name) + strlen (name) + strlen (connection) + 10);
5604 sprintf (str, "*%s_cases", unit->name);
5605 case_attr = find_attr (str, 0);
5606 if (! case_attr) abort ();
5607 common_av = find_most_used (case_attr);
5608
5609 for (av = case_attr->first_value; av; av = av->next)
5610 if (av != common_av)
5611 write_attr_case (case_attr, av, 1,
5612 "casenum =", ";", 4, unit->condexp);
5613
5614 write_attr_case (case_attr, common_av, 0,
5615 "casenum =", ";", 4, unit->condexp);
5616 printf (" }\n\n");
5617
5618 /* Now write an outer switch statement on each case. Then write
5619 the tests on the executing function within each. */
cb1520bc 5620 printf (" insn = candidate_insn;\n");
bee757e1
TW
5621 printf (" switch (casenum)\n");
5622 printf (" {\n");
5623
5624 for (i = 0; i < unit->num_opclasses; i++)
5625 {
5626 /* Ensure using this case. */
5627 using_case = 0;
5628 for (av = case_attr->first_value; av; av = av->next)
5629 if (av->num_insns
5630 && contained_in_p (make_numeric_value (i), av->value))
5631 using_case = 1;
5632
5633 if (! using_case)
5634 continue;
5635
5636 printf (" case %d:\n", i);
5637 sprintf (str, "*%s_%s_%d", unit->name, connection, i);
5638 attr = find_attr (str, 0);
5639 if (! attr) abort ();
5640
5641 /* If single value, just write it. */
5642 value = find_single_value (attr);
5643 if (value)
f75d38a7 5644 write_attr_set (attr, 6, value, "return", ";\n", true_rtx, -2, -2);
bee757e1
TW
5645 else
5646 {
5647 common_av = find_most_used (attr);
5648 printf (" switch (recog_memoized (insn))\n");
5649 printf ("\t{\n");
5650
5651 for (av = attr->first_value; av; av = av->next)
5652 if (av != common_av)
5653 write_attr_case (attr, av, 1,
5654 "return", ";", 8, unit->condexp);
5655
5656 write_attr_case (attr, common_av, 0,
5657 "return", ";", 8, unit->condexp);
5658 printf (" }\n\n");
5659 }
5660 }
5661
2e61a5a5
JL
5662 /* This default case should not be needed, but gcc's analysis is not
5663 good enough to realize that the default case is not needed for the
5664 second switch statement. */
5665 printf (" default:\n abort ();\n");
bee757e1
TW
5666 printf (" }\n}\n\n");
5667}
41299f41
TW
5668\f
5669/* This page contains miscellaneous utility routines. */
5670
5671/* Given a string, return the number of comma-separated elements in it.
5672 Return 0 for the null string. */
5673
5674static int
5675n_comma_elts (s)
3cce094d 5676 const char *s;
41299f41
TW
5677{
5678 int n;
5679
5680 if (*s == '\0')
5681 return 0;
5682
5683 for (n = 1; *s; s++)
5684 if (*s == ',')
5685 n++;
5686
5687 return n;
5688}
5689
5690/* Given a pointer to a (char *), return a malloc'ed string containing the
5691 next comma-separated element. Advance the pointer to after the string
5692 scanned, or the end-of-string. Return NULL if at end of string. */
5693
5694static char *
5695next_comma_elt (pstr)
3cce094d 5696 const char **pstr;
41299f41
TW
5697{
5698 char *out_str;
3cce094d 5699 const char *p;
41299f41
TW
5700
5701 if (**pstr == '\0')
5702 return NULL;
5703
5704 /* Find end of string to compute length. */
5705 for (p = *pstr; *p != ',' && *p != '\0'; p++)
5706 ;
5707
3e7b5313
TW
5708 out_str = attr_string (*pstr, p - *pstr);
5709 *pstr = p;
41299f41 5710
41299f41
TW
5711 if (**pstr == ',')
5712 (*pstr)++;
5713
5714 return out_str;
5715}
5716
5717/* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
5718 is non-zero, build a new attribute, if one does not exist. */
5719
5720static struct attr_desc *
5721find_attr (name, create)
85fda1eb 5722 const char *name;
41299f41
TW
5723 int create;
5724{
5725 struct attr_desc *attr;
3715a518 5726 int index;
41299f41
TW
5727
5728 /* Before we resort to using `strcmp', see if the string address matches
5729 anywhere. In most cases, it should have been canonicalized to do so. */
5730 if (name == alternative_name)
5731 return NULL;
5732
3715a518
RS
5733 index = name[0] & (MAX_ATTRS_INDEX - 1);
5734 for (attr = attrs[index]; attr; attr = attr->next)
41299f41
TW
5735 if (name == attr->name)
5736 return attr;
5737
5738 /* Otherwise, do it the slow way. */
3715a518 5739 for (attr = attrs[index]; attr; attr = attr->next)
81fd4c6e 5740 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
41299f41
TW
5741 return attr;
5742
5743 if (! create)
5744 return NULL;
5745
0e9414fd 5746 attr = (struct attr_desc *) oballoc (sizeof (struct attr_desc));
3715a518 5747 attr->name = attr_string (name, strlen (name));
41299f41 5748 attr->first_value = attr->default_val = NULL;
72f1215c 5749 attr->is_numeric = attr->negative_ok = attr->is_const = attr->is_special = 0;
605fba2b 5750 attr->unsigned_p = attr->func_units_p = attr->blockage_p = 0;
3715a518
RS
5751 attr->next = attrs[index];
5752 attrs[index] = attr;
41299f41
TW
5753
5754 return attr;
5755}
5756
5757/* Create internal attribute with the given default value. */
5758
5759static void
5760make_internal_attr (name, value, special)
85fda1eb 5761 const char *name;
41299f41
TW
5762 rtx value;
5763 int special;
5764{
5765 struct attr_desc *attr;
5766
5767 attr = find_attr (name, 1);
5768 if (attr->default_val)
5769 abort ();
5770
5771 attr->is_numeric = 1;
3e7b5313 5772 attr->is_const = 0;
72f1215c
TW
5773 attr->is_special = (special & 1) != 0;
5774 attr->negative_ok = (special & 2) != 0;
bee757e1 5775 attr->unsigned_p = (special & 4) != 0;
6f6074ea
MM
5776 attr->func_units_p = (special & 8) != 0;
5777 attr->blockage_p = (special & 16) != 0;
41299f41
TW
5778 attr->default_val = get_attr_value (value, attr, -2);
5779}
5780
5781/* Find the most used value of an attribute. */
5782
5783static struct attr_value *
5784find_most_used (attr)
5785 struct attr_desc *attr;
5786{
5787 struct attr_value *av;
5788 struct attr_value *most_used;
5789 int nuses;
5790
5791 most_used = NULL;
5792 nuses = -1;
5793
5794 for (av = attr->first_value; av; av = av->next)
5795 if (av->num_insns > nuses)
5796 nuses = av->num_insns, most_used = av;
5797
5798 return most_used;
5799}
5800
5801/* If an attribute only has a single value used, return it. Otherwise
5802 return NULL. */
5803
5804static rtx
5805find_single_value (attr)
5806 struct attr_desc *attr;
5807{
5808 struct attr_value *av;
5809 rtx unique_value;
5810
5811 unique_value = NULL;
5812 for (av = attr->first_value; av; av = av->next)
5813 if (av->num_insns)
5814 {
5815 if (unique_value)
5816 return NULL;
5817 else
5818 unique_value = av->value;
5819 }
5820
5821 return unique_value;
5822}
5823
5824/* Return (attr_value "n") */
5825
5826static rtx
5827make_numeric_value (n)
5828 int n;
5829{
5830 static rtx int_values[20];
5831 rtx exp;
3e7b5313 5832 char *p;
41299f41
TW
5833
5834 if (n < 0)
5835 abort ();
5836
5837 if (n < 20 && int_values[n])
5838 return int_values[n];
5839
0e9414fd 5840 p = attr_printf (MAX_DIGITS, "%d", n);
3e7b5313 5841 exp = attr_rtx (CONST_STRING, p);
41299f41
TW
5842
5843 if (n < 20)
5844 int_values[n] = exp;
5845
5846 return exp;
5847}
5848\f
bee757e1
TW
5849static void
5850extend_range (range, min, max)
5851 struct range *range;
5852 int min;
5853 int max;
5854{
5855 if (range->min > min) range->min = min;
5856 if (range->max < max) range->max = max;
5857}
5858
2778b98d 5859PTR
470b68c0
RH
5860xrealloc (old, size)
5861 PTR old;
2778b98d 5862 size_t size;
41299f41 5863{
470b68c0 5864 register PTR ptr;
09d83d25 5865 if (old)
470b68c0
RH
5866 ptr = (PTR) realloc (old, size);
5867 else
5868 ptr = (PTR) malloc (size);
5869 if (!ptr)
41299f41 5870 fatal ("virtual memory exhausted");
470b68c0 5871 return ptr;
41299f41
TW
5872}
5873
2778b98d 5874PTR
41299f41 5875xmalloc (size)
2778b98d 5876 size_t size;
41299f41 5877{
2778b98d 5878 register PTR val = (PTR) malloc (size);
41299f41
TW
5879
5880 if (val == 0)
5881 fatal ("virtual memory exhausted");
5882 return val;
5883}
5884
7339c88d
RS
5885static rtx
5886copy_rtx_unchanging (orig)
5887 register rtx orig;
5888{
b5b6ad46 5889#if 0
7339c88d
RS
5890 register rtx copy;
5891 register RTX_CODE code;
b5b6ad46 5892#endif
7339c88d 5893
85093b9c 5894 if (RTX_UNCHANGING_P (orig) || MEM_IN_STRUCT_P (orig))
81fd4c6e
RS
5895 return orig;
5896
85093b9c
RS
5897 MEM_IN_STRUCT_P (orig) = 1;
5898 return orig;
7339c88d 5899
85093b9c
RS
5900#if 0
5901 code = GET_CODE (orig);
7339c88d
RS
5902 switch (code)
5903 {
5904 case CONST_INT:
5905 case CONST_DOUBLE:
5906 case SYMBOL_REF:
5907 case CODE_LABEL:
5908 return orig;
e9a25f70
JL
5909
5910 default:
5911 break;
7339c88d
RS
5912 }
5913
5914 copy = rtx_alloc (code);
5915 PUT_MODE (copy, GET_MODE (orig));
5916 RTX_UNCHANGING_P (copy) = 1;
5917
4c9a05bc 5918 bcopy ((char *) &XEXP (orig, 0), (char *) &XEXP (copy, 0),
7339c88d
RS
5919 GET_RTX_LENGTH (GET_CODE (copy)) * sizeof (rtx));
5920 return copy;
85093b9c 5921#endif
7339c88d
RS
5922}
5923
d7c665bf
RK
5924/* Determine if an insn has a constant number of delay slots, i.e., the
5925 number of delay slots is not a function of the length of the insn. */
5926
69277eec 5927static void
a9ab5e00
TM
5928write_const_num_delay_slots ()
5929{
5930 struct attr_desc *attr = find_attr ("*num_delay_slots", 0);
5931 struct attr_value *av;
5932 struct insn_ent *ie;
a9ab5e00
TM
5933
5934 if (attr)
5935 {
5936 printf ("int\nconst_num_delay_slots (insn)\n");
dea2643c 5937 printf (" rtx insn;\n");
a9ab5e00
TM
5938 printf ("{\n");
5939 printf (" switch (recog_memoized (insn))\n");
5940 printf (" {\n");
5941
5942 for (av = attr->first_value; av; av = av->next)
d7c665bf
RK
5943 {
5944 length_used = 0;
5945 walk_attr_value (av->value);
5946 if (length_used)
5947 {
5948 for (ie = av->first_insn; ie; ie = ie->next)
a9ab5e00
TM
5949 if (ie->insn_code != -1)
5950 printf (" case %d:\n", ie->insn_code);
d7c665bf
RK
5951 printf (" return 0;\n");
5952 }
5953 }
5954
a9ab5e00
TM
5955 printf (" default:\n");
5956 printf (" return 1;\n");
fc470718 5957 printf (" }\n}\n\n");
a9ab5e00
TM
5958 }
5959}
5960
41299f41 5961\f
a94ae8f5 5962extern int main PARAMS ((int, char **));
c1b59dce 5963
41299f41
TW
5964int
5965main (argc, argv)
5966 int argc;
5967 char **argv;
5968{
5969 rtx desc;
5970 FILE *infile;
41299f41
TW
5971 register int c;
5972 struct attr_desc *attr;
41299f41
TW
5973 struct insn_def *id;
5974 rtx tem;
3715a518 5975 int i;
41299f41 5976
ef178af3
ZW
5977 progname = "genattrtab";
5978
95b8c0c8 5979#if defined (RLIMIT_STACK) && defined (HAVE_GETRLIMIT) && defined (HAVE_SETRLIMIT)
f0cdf2b2
RK
5980 /* Get rid of any avoidable limit on stack size. */
5981 {
5982 struct rlimit rlim;
5983
0f41302f 5984 /* Set the stack limit huge so that alloca does not fail. */
f0cdf2b2
RK
5985 getrlimit (RLIMIT_STACK, &rlim);
5986 rlim.rlim_cur = rlim.rlim_max;
5987 setrlimit (RLIMIT_STACK, &rlim);
5988 }
c85f7c16 5989#endif
f0cdf2b2 5990
f8b6598e 5991 progname = "genattrtab";
41299f41 5992 obstack_init (rtl_obstack);
7339c88d 5993 obstack_init (hash_obstack);
81fd4c6e 5994 obstack_init (temp_obstack);
41299f41
TW
5995
5996 if (argc <= 1)
5997 fatal ("No input file name.");
5998
5999 infile = fopen (argv[1], "r");
6000 if (infile == 0)
6001 {
6002 perror (argv[1]);
c1b59dce 6003 return (FATAL_EXIT_CODE);
41299f41 6004 }
bcdaba58 6005 read_rtx_filename = argv[1];
41299f41 6006
41299f41 6007 /* Set up true and false rtx's */
81fd4c6e 6008 true_rtx = rtx_alloc (CONST_INT);
3d678dca 6009 XWINT (true_rtx, 0) = 1;
81fd4c6e 6010 false_rtx = rtx_alloc (CONST_INT);
3d678dca 6011 XWINT (false_rtx, 0) = 0;
41299f41 6012 RTX_UNCHANGING_P (true_rtx) = RTX_UNCHANGING_P (false_rtx) = 1;
81fd4c6e
RS
6013 RTX_INTEGRATED_P (true_rtx) = RTX_INTEGRATED_P (false_rtx) = 1;
6014
6015 alternative_name = attr_string ("alternative", strlen ("alternative"));
41299f41
TW
6016
6017 printf ("/* Generated automatically by the program `genattrtab'\n\
6018from the machine description file `md'. */\n\n");
6019
6020 /* Read the machine description. */
6021
6022 while (1)
6023 {
6024 c = read_skip_spaces (infile);
6025 if (c == EOF)
6026 break;
6027 ungetc (c, infile);
6028
6029 desc = read_rtx (infile);
6030 if (GET_CODE (desc) == DEFINE_INSN
6031 || GET_CODE (desc) == DEFINE_PEEPHOLE
6032 || GET_CODE (desc) == DEFINE_ASM_ATTRIBUTES)
6033 gen_insn (desc);
6034
6035 else if (GET_CODE (desc) == DEFINE_EXPAND)
6036 insn_code_number++, insn_index_number++;
6037
6038 else if (GET_CODE (desc) == DEFINE_SPLIT)
6039 insn_code_number++, insn_index_number++;
6040
ede7cd44
RH
6041 else if (GET_CODE (desc) == DEFINE_PEEPHOLE2)
6042 insn_code_number++, insn_index_number++;
6043
41299f41
TW
6044 else if (GET_CODE (desc) == DEFINE_ATTR)
6045 {
6046 gen_attr (desc);
6047 insn_index_number++;
6048 }
6049
6050 else if (GET_CODE (desc) == DEFINE_DELAY)
6051 {
6052 gen_delay (desc);
6053 insn_index_number++;
6054 }
6055
6056 else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
6057 {
6058 gen_unit (desc);
6059 insn_index_number++;
6060 }
6061 }
6062
6063 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
6064 if (! got_define_asm_attributes)
6065 {
6066 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
6067 XVEC (tem, 0) = rtvec_alloc (0);
6068 gen_insn (tem);
6069 }
6070
6071 /* Expand DEFINE_DELAY information into new attribute. */
6072 if (num_delays)
6073 expand_delays ();
6074
6075 /* Expand DEFINE_FUNCTION_UNIT information into new attributes. */
6076 if (num_units)
6077 expand_units ();
6078
6079 printf ("#include \"config.h\"\n");
729da3f5 6080 printf ("#include \"system.h\"\n");
41299f41 6081 printf ("#include \"rtl.h\"\n");
6baf1cc8 6082 printf ("#include \"tm_p.h\"\n");
41299f41
TW
6083 printf ("#include \"insn-config.h\"\n");
6084 printf ("#include \"recog.h\"\n");
6085 printf ("#include \"regs.h\"\n");
6086 printf ("#include \"real.h\"\n");
6087 printf ("#include \"output.h\"\n");
6088 printf ("#include \"insn-attr.h\"\n");
114791ea 6089 printf ("#include \"toplev.h\"\n");
41299f41 6090 printf ("\n");
1ccbefce 6091 printf ("#define operands recog_data.operand\n\n");
41299f41
TW
6092
6093 /* Make `insn_alternatives'. */
0e9414fd 6094 insn_alternatives = (int *) oballoc (insn_code_number * sizeof (int));
41299f41
TW
6095 for (id = defs; id; id = id->next)
6096 if (id->insn_code >= 0)
6097 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
6098
3715a518 6099 /* Make `insn_n_alternatives'. */
0e9414fd 6100 insn_n_alternatives = (int *) oballoc (insn_code_number * sizeof (int));
3715a518
RS
6101 for (id = defs; id; id = id->next)
6102 if (id->insn_code >= 0)
6103 insn_n_alternatives[id->insn_code] = id->num_alternatives;
6104
41299f41
TW
6105 /* Prepare to write out attribute subroutines by checking everything stored
6106 away and building the attribute cases. */
6107
6108 check_defs ();
3715a518
RS
6109 for (i = 0; i < MAX_ATTRS_INDEX; i++)
6110 for (attr = attrs[i]; attr; attr = attr->next)
6111 {
6112 attr->default_val->value
6113 = check_attr_value (attr->default_val->value, attr);
6114 fill_attr (attr);
6115 }
41299f41
TW
6116
6117 /* Construct extra attributes for `length'. */
6118 make_length_attrs ();
6119
0f41302f 6120 /* Perform any possible optimizations to speed up compilation. */
41299f41
TW
6121 optimize_attrs ();
6122
6123 /* Now write out all the `gen_attr_...' routines. Do these before the
6124 special routines (specifically before write_function_unit_info), so
6125 that they get defined before they are used. */
6126
3715a518
RS
6127 for (i = 0; i < MAX_ATTRS_INDEX; i++)
6128 for (attr = attrs[i]; attr; attr = attr->next)
6129 {
71d9b493 6130 if (! attr->is_special && ! attr->is_const)
3715a518
RS
6131 write_attr_get (attr);
6132 }
41299f41
TW
6133
6134 /* Write out delay eligibility information, if DEFINE_DELAY present.
6135 (The function to compute the number of delay slots will be written
6136 below.) */
6137 if (num_delays)
6138 {
6139 write_eligible_delay ("delay");
6140 if (have_annul_true)
6141 write_eligible_delay ("annul_true");
6142 if (have_annul_false)
6143 write_eligible_delay ("annul_false");
6144 }
6145
6146 /* Write out information about function units. */
6147 if (num_units)
6148 write_function_unit_info ();
6149
a9ab5e00
TM
6150 /* Write out constant delay slot info */
6151 write_const_num_delay_slots ();
6152
fc470718
R
6153 write_length_unit_log ();
6154
41299f41 6155 fflush (stdout);
c1b59dce 6156 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
41299f41 6157}
a995e389
RH
6158
6159/* Define this so we can link with print-rtl.o to get debug_rtx function. */
6160const char *
6161get_insn_name (code)
c1b59dce 6162 int code ATTRIBUTE_UNUSED;
a995e389
RH
6163{
6164 return NULL;
6165}