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