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