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