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