genrecog.c (validate_pattern): Check matching constraint refers to a lower numbered...
[gcc.git] / gcc / genrecog.c
1 /* Generate code from machine description to recognize rtl as insns.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20
21 /* This program is used to produce insn-recog.c, which contains a
22 function called `recog' plus its subroutines. These functions
23 contain a decision tree that recognizes whether an rtx, the
24 argument given to recog, is a valid instruction.
25
26 recog returns -1 if the rtx is not valid. If the rtx is valid,
27 recog returns a nonnegative number which is the insn code number
28 for the pattern that matched. This is the same as the order in the
29 machine description of the entry that matched. This number can be
30 used as an index into various insn_* tables, such as insn_template,
31 insn_outfun, and insn_n_operands (found in insn-output.c).
32
33 The third argument to recog is an optional pointer to an int. If
34 present, recog will accept a pattern if it matches except for
35 missing CLOBBER expressions at the end. In that case, the value
36 pointed to by the optional pointer will be set to the number of
37 CLOBBERs that need to be added (it should be initialized to zero by
38 the caller). If it is set nonzero, the caller should allocate a
39 PARALLEL of the appropriate size, copy the initial entries, and
40 call add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.
41
42 This program also generates the function `split_insns', which
43 returns 0 if the rtl could not be split, or it returns the split
44 rtl as an INSN list.
45
46 This program also generates the function `peephole2_insns', which
47 returns 0 if the rtl could not be matched. If there was a match,
48 the new rtl is returned in an INSN list, and LAST_INSN will point
49 to the last recognized insn in the old sequence. */
50
51 #include "bconfig.h"
52 #include "system.h"
53 #include "coretypes.h"
54 #include "tm.h"
55 #include "rtl.h"
56 #include "errors.h"
57 #include "read-md.h"
58 #include "gensupport.h"
59
60 #define OUTPUT_LABEL(INDENT_STRING, LABEL_NUMBER) \
61 printf ("%sL%d: ATTRIBUTE_UNUSED_LABEL\n", (INDENT_STRING), (LABEL_NUMBER))
62
63 /* Ways of obtaining an rtx to be tested. */
64 enum position_type {
65 /* PATTERN (peep2_next_insn (ARG)). */
66 POS_PEEP2_INSN,
67
68 /* XEXP (BASE, ARG). */
69 POS_XEXP,
70
71 /* XVECEXP (BASE, 0, ARG). */
72 POS_XVECEXP0
73 };
74
75 /* The position of an rtx relative to X0. Each useful position is
76 represented by exactly one instance of this structure. */
77 struct position
78 {
79 /* The parent rtx. This is the root position for POS_PEEP2_INSNs. */
80 struct position *base;
81
82 /* A position with the same BASE and TYPE, but with the next value
83 of ARG. */
84 struct position *next;
85
86 /* A list of all POS_XEXP positions that use this one as their base,
87 chained by NEXT fields. The first entry represents XEXP (this, 0),
88 the second represents XEXP (this, 1), and so on. */
89 struct position *xexps;
90
91 /* A list of POS_XVECEXP0 positions that use this one as their base,
92 chained by NEXT fields. The first entry represents XVECEXP (this, 0, 0),
93 the second represents XVECEXP (this, 0, 1), and so on. */
94 struct position *xvecexp0s;
95
96 /* The type of position. */
97 enum position_type type;
98
99 /* The argument to TYPE (shown as ARG in the position_type comments). */
100 int arg;
101
102 /* The depth of this position, with 0 as the root. */
103 int depth;
104 };
105
106 /* A listhead of decision trees. The alternatives to a node are kept
107 in a doubly-linked list so we can easily add nodes to the proper
108 place when merging. */
109
110 struct decision_head
111 {
112 struct decision *first;
113 struct decision *last;
114 };
115
116 /* These types are roughly in the order in which we'd like to test them. */
117 enum decision_type
118 {
119 DT_num_insns,
120 DT_mode, DT_code, DT_veclen,
121 DT_elt_zero_int, DT_elt_one_int, DT_elt_zero_wide, DT_elt_zero_wide_safe,
122 DT_const_int,
123 DT_veclen_ge, DT_dup, DT_pred, DT_c_test,
124 DT_accept_op, DT_accept_insn
125 };
126
127 /* A single test. The two accept types aren't tests per-se, but
128 their equality (or lack thereof) does affect tree merging so
129 it is convenient to keep them here. */
130
131 struct decision_test
132 {
133 /* A linked list through the tests attached to a node. */
134 struct decision_test *next;
135
136 enum decision_type type;
137
138 union
139 {
140 int num_insns; /* Number if insn in a define_peephole2. */
141 machine_mode mode; /* Machine mode of node. */
142 RTX_CODE code; /* Code to test. */
143
144 struct
145 {
146 const char *name; /* Predicate to call. */
147 const struct pred_data *data;
148 /* Optimization hints for this predicate. */
149 machine_mode mode; /* Machine mode for node. */
150 } pred;
151
152 const char *c_test; /* Additional test to perform. */
153 int veclen; /* Length of vector. */
154 int dup; /* Number of operand to compare against. */
155 HOST_WIDE_INT intval; /* Value for XINT for XWINT. */
156 int opno; /* Operand number matched. */
157
158 struct {
159 int code_number; /* Insn number matched. */
160 int lineno; /* Line number of the insn. */
161 int num_clobbers_to_add; /* Number of CLOBBERs to be added. */
162 } insn;
163 } u;
164 };
165
166 /* Data structure for decision tree for recognizing legitimate insns. */
167
168 struct decision
169 {
170 struct decision_head success; /* Nodes to test on success. */
171 struct decision *next; /* Node to test on failure. */
172 struct decision *prev; /* Node whose failure tests us. */
173 struct decision *afterward; /* Node to test on success,
174 but failure of successor nodes. */
175
176 struct position *position; /* Position in pattern. */
177
178 struct decision_test *tests; /* The tests for this node. */
179
180 int number; /* Node number, used for labels */
181 int subroutine_number; /* Number of subroutine this node starts */
182 int need_label; /* Label needs to be output. */
183 };
184
185 #define SUBROUTINE_THRESHOLD 100
186
187 static int next_subroutine_number;
188
189 /* We can write three types of subroutines: One for insn recognition,
190 one to split insns, and one for peephole-type optimizations. This
191 defines which type is being written. */
192
193 enum routine_type {
194 RECOG, SPLIT, PEEPHOLE2
195 };
196
197 #define IS_SPLIT(X) ((X) != RECOG)
198
199 /* Next available node number for tree nodes. */
200
201 static int next_number;
202
203 /* Next number to use as an insn_code. */
204
205 static int next_insn_code;
206
207 /* Record the highest depth we ever have so we know how many variables to
208 allocate in each subroutine we make. */
209
210 static int max_depth;
211
212 /* The line number of the start of the pattern currently being processed. */
213 static int pattern_lineno;
214
215 /* The root position (x0). */
216 static struct position root_pos;
217
218 /* A list of all POS_PEEP2_INSNs. The entry for insn 0 is the root position,
219 since we are given that instruction's pattern as x0. */
220 static struct position *peep2_insn_pos_list = &root_pos;
221 \f
222 extern void debug_decision
223 (struct decision *);
224 extern void debug_decision_list
225 (struct decision *);
226 \f
227 /* Return a position with the given BASE, TYPE and ARG. NEXT_PTR
228 points to where the unique object that represents the position
229 should be stored. Create the object if it doesn't already exist,
230 otherwise reuse the object that is already there. */
231
232 static struct position *
233 next_position (struct position **next_ptr, struct position *base,
234 enum position_type type, int arg)
235 {
236 struct position *pos;
237
238 pos = *next_ptr;
239 if (!pos)
240 {
241 pos = XCNEW (struct position);
242 pos->base = base;
243 pos->type = type;
244 pos->arg = arg;
245 pos->depth = base->depth + 1;
246 *next_ptr = pos;
247 }
248 return pos;
249 }
250
251 /* Compare positions POS1 and POS2 lexicographically. */
252
253 static int
254 compare_positions (struct position *pos1, struct position *pos2)
255 {
256 int diff;
257
258 diff = pos1->depth - pos2->depth;
259 if (diff < 0)
260 do
261 pos2 = pos2->base;
262 while (pos1->depth != pos2->depth);
263 else if (diff > 0)
264 do
265 pos1 = pos1->base;
266 while (pos1->depth != pos2->depth);
267 while (pos1 != pos2)
268 {
269 diff = (int) pos1->type - (int) pos2->type;
270 if (diff == 0)
271 diff = pos1->arg - pos2->arg;
272 pos1 = pos1->base;
273 pos2 = pos2->base;
274 }
275 return diff;
276 }
277
278 /* Create a new node in sequence after LAST. */
279
280 static struct decision *
281 new_decision (struct position *pos, struct decision_head *last)
282 {
283 struct decision *new_decision = XCNEW (struct decision);
284
285 new_decision->success = *last;
286 new_decision->position = pos;
287 new_decision->number = next_number++;
288
289 last->first = last->last = new_decision;
290 return new_decision;
291 }
292
293 /* Create a new test and link it in at PLACE. */
294
295 static struct decision_test *
296 new_decision_test (enum decision_type type, struct decision_test ***pplace)
297 {
298 struct decision_test **place = *pplace;
299 struct decision_test *test;
300
301 test = XNEW (struct decision_test);
302 test->next = *place;
303 test->type = type;
304 *place = test;
305
306 place = &test->next;
307 *pplace = place;
308
309 return test;
310 }
311
312 /* Search for and return operand N, stop when reaching node STOP. */
313
314 static rtx
315 find_operand (rtx pattern, int n, rtx stop)
316 {
317 const char *fmt;
318 RTX_CODE code;
319 int i, j, len;
320 rtx r;
321
322 if (pattern == stop)
323 return stop;
324
325 code = GET_CODE (pattern);
326 if ((code == MATCH_SCRATCH
327 || code == MATCH_OPERAND
328 || code == MATCH_OPERATOR
329 || code == MATCH_PARALLEL)
330 && XINT (pattern, 0) == n)
331 return pattern;
332
333 fmt = GET_RTX_FORMAT (code);
334 len = GET_RTX_LENGTH (code);
335 for (i = 0; i < len; i++)
336 {
337 switch (fmt[i])
338 {
339 case 'e': case 'u':
340 if ((r = find_operand (XEXP (pattern, i), n, stop)) != NULL_RTX)
341 return r;
342 break;
343
344 case 'V':
345 if (! XVEC (pattern, i))
346 break;
347 /* Fall through. */
348
349 case 'E':
350 for (j = 0; j < XVECLEN (pattern, i); j++)
351 if ((r = find_operand (XVECEXP (pattern, i, j), n, stop))
352 != NULL_RTX)
353 return r;
354 break;
355
356 case 'i': case 'w': case '0': case 's':
357 break;
358
359 default:
360 gcc_unreachable ();
361 }
362 }
363
364 return NULL;
365 }
366
367 /* Search for and return operand M, such that it has a matching
368 constraint for operand N. */
369
370 static rtx
371 find_matching_operand (rtx pattern, int n)
372 {
373 const char *fmt;
374 RTX_CODE code;
375 int i, j, len;
376 rtx r;
377
378 code = GET_CODE (pattern);
379 if (code == MATCH_OPERAND
380 && (XSTR (pattern, 2)[0] == '0' + n
381 || (XSTR (pattern, 2)[0] == '%'
382 && XSTR (pattern, 2)[1] == '0' + n)))
383 return pattern;
384
385 fmt = GET_RTX_FORMAT (code);
386 len = GET_RTX_LENGTH (code);
387 for (i = 0; i < len; i++)
388 {
389 switch (fmt[i])
390 {
391 case 'e': case 'u':
392 if ((r = find_matching_operand (XEXP (pattern, i), n)))
393 return r;
394 break;
395
396 case 'V':
397 if (! XVEC (pattern, i))
398 break;
399 /* Fall through. */
400
401 case 'E':
402 for (j = 0; j < XVECLEN (pattern, i); j++)
403 if ((r = find_matching_operand (XVECEXP (pattern, i, j), n)))
404 return r;
405 break;
406
407 case 'i': case 'w': case '0': case 's':
408 break;
409
410 default:
411 gcc_unreachable ();
412 }
413 }
414
415 return NULL;
416 }
417
418 /* In DEFINE_EXPAND, DEFINE_SPLIT, and DEFINE_PEEPHOLE2, we
419 don't use the MATCH_OPERAND constraint, only the predicate.
420 This is confusing to folks doing new ports, so help them
421 not make the mistake. */
422
423 static bool
424 constraints_supported_in_insn_p (rtx insn)
425 {
426 return !(GET_CODE (insn) == DEFINE_EXPAND
427 || GET_CODE (insn) == DEFINE_SPLIT
428 || GET_CODE (insn) == DEFINE_PEEPHOLE2);
429 }
430
431 /* Check for various errors in patterns. SET is nonnull for a destination,
432 and is the complete set pattern. SET_CODE is '=' for normal sets, and
433 '+' within a context that requires in-out constraints. */
434
435 static void
436 validate_pattern (rtx pattern, rtx insn, rtx set, int set_code)
437 {
438 const char *fmt;
439 RTX_CODE code;
440 size_t i, len;
441 int j;
442
443 code = GET_CODE (pattern);
444 switch (code)
445 {
446 case MATCH_SCRATCH:
447 {
448 const char constraints0 = XSTR (pattern, 1)[0];
449
450 if (!constraints_supported_in_insn_p (insn))
451 {
452 if (constraints0)
453 {
454 error_with_line (pattern_lineno,
455 "constraints not supported in %s",
456 rtx_name[GET_CODE (insn)]);
457 }
458 return;
459 }
460
461 /* If a MATCH_SCRATCH is used in a context requiring an write-only
462 or read/write register, validate that. */
463 if (set_code == '='
464 && constraints0
465 && constraints0 != '='
466 && constraints0 != '+')
467 {
468 error_with_line (pattern_lineno,
469 "operand %d missing output reload",
470 XINT (pattern, 0));
471 }
472 return;
473 }
474 case MATCH_DUP:
475 case MATCH_OP_DUP:
476 case MATCH_PAR_DUP:
477 if (find_operand (insn, XINT (pattern, 0), pattern) == pattern)
478 error_with_line (pattern_lineno,
479 "operand %i duplicated before defined",
480 XINT (pattern, 0));
481 break;
482 case MATCH_OPERAND:
483 case MATCH_OPERATOR:
484 {
485 const char *pred_name = XSTR (pattern, 1);
486 const struct pred_data *pred;
487 const char *c_test;
488
489 if (GET_CODE (insn) == DEFINE_INSN)
490 c_test = XSTR (insn, 2);
491 else
492 c_test = XSTR (insn, 1);
493
494 if (pred_name[0] != 0)
495 {
496 pred = lookup_predicate (pred_name);
497 if (!pred)
498 error_with_line (pattern_lineno, "unknown predicate '%s'",
499 pred_name);
500 }
501 else
502 pred = 0;
503
504 if (code == MATCH_OPERAND)
505 {
506 const char *constraints = XSTR (pattern, 2);
507 const char constraints0 = constraints[0];
508
509 if (!constraints_supported_in_insn_p (insn))
510 {
511 if (constraints0)
512 {
513 error_with_line (pattern_lineno,
514 "constraints not supported in %s",
515 rtx_name[GET_CODE (insn)]);
516 }
517 }
518
519 /* A MATCH_OPERAND that is a SET should have an output reload. */
520 else if (set && constraints0)
521 {
522 if (set_code == '+')
523 {
524 if (constraints0 == '+')
525 ;
526 /* If we've only got an output reload for this operand,
527 we'd better have a matching input operand. */
528 else if (constraints0 == '='
529 && find_matching_operand (insn, XINT (pattern, 0)))
530 ;
531 else
532 error_with_line (pattern_lineno,
533 "operand %d missing in-out reload",
534 XINT (pattern, 0));
535 }
536 else if (constraints0 != '=' && constraints0 != '+')
537 error_with_line (pattern_lineno,
538 "operand %d missing output reload",
539 XINT (pattern, 0));
540 }
541
542 /* For matching constraint in MATCH_OPERAND, the digit must be a
543 smaller number than the number of the operand that uses it in the
544 constraint. */
545 while (1)
546 {
547 while (constraints[0]
548 && (constraints[0] == ' ' || constraints[0] == ','))
549 constraints++;
550 if (!constraints[0])
551 break;
552
553 if (constraints[0] >= '0' && constraints[0] <= '9')
554 {
555 int val;
556
557 sscanf (constraints, "%d", &val);
558 if (val >= XINT (pattern, 0))
559 error_with_line (pattern_lineno,
560 "constraint digit %d is not smaller than"
561 " operand %d",
562 val, XINT (pattern, 0));
563 }
564
565 while (constraints[0] && constraints[0] != ',')
566 constraints++;
567 }
568 }
569
570 /* Allowing non-lvalues in destinations -- particularly CONST_INT --
571 while not likely to occur at runtime, results in less efficient
572 code from insn-recog.c. */
573 if (set && pred && pred->allows_non_lvalue)
574 error_with_line (pattern_lineno,
575 "destination operand %d allows non-lvalue",
576 XINT (pattern, 0));
577
578 /* A modeless MATCH_OPERAND can be handy when we can check for
579 multiple modes in the c_test. In most other cases, it is a
580 mistake. Only DEFINE_INSN is eligible, since SPLIT and
581 PEEP2 can FAIL within the output pattern. Exclude special
582 predicates, which check the mode themselves. Also exclude
583 predicates that allow only constants. Exclude the SET_DEST
584 of a call instruction, as that is a common idiom. */
585
586 if (GET_MODE (pattern) == VOIDmode
587 && code == MATCH_OPERAND
588 && GET_CODE (insn) == DEFINE_INSN
589 && pred
590 && !pred->special
591 && pred->allows_non_const
592 && strstr (c_test, "operands") == NULL
593 && ! (set
594 && GET_CODE (set) == SET
595 && GET_CODE (SET_SRC (set)) == CALL))
596 message_with_line (pattern_lineno,
597 "warning: operand %d missing mode?",
598 XINT (pattern, 0));
599 return;
600 }
601
602 case SET:
603 {
604 machine_mode dmode, smode;
605 rtx dest, src;
606
607 dest = SET_DEST (pattern);
608 src = SET_SRC (pattern);
609
610 /* STRICT_LOW_PART is a wrapper. Its argument is the real
611 destination, and it's mode should match the source. */
612 if (GET_CODE (dest) == STRICT_LOW_PART)
613 dest = XEXP (dest, 0);
614
615 /* Find the referent for a DUP. */
616
617 if (GET_CODE (dest) == MATCH_DUP
618 || GET_CODE (dest) == MATCH_OP_DUP
619 || GET_CODE (dest) == MATCH_PAR_DUP)
620 dest = find_operand (insn, XINT (dest, 0), NULL);
621
622 if (GET_CODE (src) == MATCH_DUP
623 || GET_CODE (src) == MATCH_OP_DUP
624 || GET_CODE (src) == MATCH_PAR_DUP)
625 src = find_operand (insn, XINT (src, 0), NULL);
626
627 dmode = GET_MODE (dest);
628 smode = GET_MODE (src);
629
630 /* The mode of an ADDRESS_OPERAND is the mode of the memory
631 reference, not the mode of the address. */
632 if (GET_CODE (src) == MATCH_OPERAND
633 && ! strcmp (XSTR (src, 1), "address_operand"))
634 ;
635
636 /* The operands of a SET must have the same mode unless one
637 is VOIDmode. */
638 else if (dmode != VOIDmode && smode != VOIDmode && dmode != smode)
639 error_with_line (pattern_lineno,
640 "mode mismatch in set: %smode vs %smode",
641 GET_MODE_NAME (dmode), GET_MODE_NAME (smode));
642
643 /* If only one of the operands is VOIDmode, and PC or CC0 is
644 not involved, it's probably a mistake. */
645 else if (dmode != smode
646 && GET_CODE (dest) != PC
647 && GET_CODE (dest) != CC0
648 && GET_CODE (src) != PC
649 && GET_CODE (src) != CC0
650 && !CONST_INT_P (src)
651 && !CONST_WIDE_INT_P (src)
652 && GET_CODE (src) != CALL)
653 {
654 const char *which;
655 which = (dmode == VOIDmode ? "destination" : "source");
656 message_with_line (pattern_lineno,
657 "warning: %s missing a mode?", which);
658 }
659
660 if (dest != SET_DEST (pattern))
661 validate_pattern (dest, insn, pattern, '=');
662 validate_pattern (SET_DEST (pattern), insn, pattern, '=');
663 validate_pattern (SET_SRC (pattern), insn, NULL_RTX, 0);
664 return;
665 }
666
667 case CLOBBER:
668 validate_pattern (SET_DEST (pattern), insn, pattern, '=');
669 return;
670
671 case ZERO_EXTRACT:
672 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
673 validate_pattern (XEXP (pattern, 1), insn, NULL_RTX, 0);
674 validate_pattern (XEXP (pattern, 2), insn, NULL_RTX, 0);
675 return;
676
677 case STRICT_LOW_PART:
678 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
679 return;
680
681 case LABEL_REF:
682 if (GET_MODE (LABEL_REF_LABEL (pattern)) != VOIDmode)
683 error_with_line (pattern_lineno,
684 "operand to label_ref %smode not VOIDmode",
685 GET_MODE_NAME (GET_MODE (LABEL_REF_LABEL (pattern))));
686 break;
687
688 default:
689 break;
690 }
691
692 fmt = GET_RTX_FORMAT (code);
693 len = GET_RTX_LENGTH (code);
694 for (i = 0; i < len; i++)
695 {
696 switch (fmt[i])
697 {
698 case 'e': case 'u':
699 validate_pattern (XEXP (pattern, i), insn, NULL_RTX, 0);
700 break;
701
702 case 'E':
703 for (j = 0; j < XVECLEN (pattern, i); j++)
704 validate_pattern (XVECEXP (pattern, i, j), insn, NULL_RTX, 0);
705 break;
706
707 case 'i': case 'w': case '0': case 's':
708 break;
709
710 default:
711 gcc_unreachable ();
712 }
713 }
714 }
715
716 /* Create a chain of nodes to verify that an rtl expression matches
717 PATTERN.
718
719 LAST is a pointer to the listhead in the previous node in the chain (or
720 in the calling function, for the first node).
721
722 POSITION is the current position in the insn.
723
724 INSN_TYPE is the type of insn for which we are emitting code.
725
726 A pointer to the final node in the chain is returned. */
727
728 static struct decision *
729 add_to_sequence (rtx pattern, struct decision_head *last,
730 struct position *pos, enum routine_type insn_type, int top)
731 {
732 RTX_CODE code;
733 struct decision *this_decision, *sub;
734 struct decision_test *test;
735 struct decision_test **place;
736 struct position *subpos, **subpos_ptr;
737 size_t i;
738 const char *fmt;
739 int len;
740 machine_mode mode;
741 enum position_type pos_type;
742
743 if (pos->depth > max_depth)
744 max_depth = pos->depth;
745
746 sub = this_decision = new_decision (pos, last);
747 place = &this_decision->tests;
748
749 mode = GET_MODE (pattern);
750 code = GET_CODE (pattern);
751
752 switch (code)
753 {
754 case PARALLEL:
755 /* Toplevel peephole pattern. */
756 if (insn_type == PEEPHOLE2 && top)
757 {
758 int num_insns;
759
760 /* Check we have sufficient insns. This avoids complications
761 because we then know peep2_next_insn never fails. */
762 num_insns = XVECLEN (pattern, 0);
763 if (num_insns > 1)
764 {
765 test = new_decision_test (DT_num_insns, &place);
766 test->u.num_insns = num_insns;
767 last = &sub->success;
768 }
769 else
770 {
771 /* We don't need the node we just created -- unlink it. */
772 last->first = last->last = NULL;
773 }
774
775 subpos_ptr = &peep2_insn_pos_list;
776 for (i = 0; i < (size_t) XVECLEN (pattern, 0); i++)
777 {
778 subpos = next_position (subpos_ptr, &root_pos,
779 POS_PEEP2_INSN, i);
780 sub = add_to_sequence (XVECEXP (pattern, 0, i),
781 last, subpos, insn_type, 0);
782 last = &sub->success;
783 subpos_ptr = &subpos->next;
784 }
785 goto ret;
786 }
787
788 /* Else nothing special. */
789 break;
790
791 case MATCH_PARALLEL:
792 /* The explicit patterns within a match_parallel enforce a minimum
793 length on the vector. The match_parallel predicate may allow
794 for more elements. We do need to check for this minimum here
795 or the code generated to match the internals may reference data
796 beyond the end of the vector. */
797 test = new_decision_test (DT_veclen_ge, &place);
798 test->u.veclen = XVECLEN (pattern, 2);
799 /* Fall through. */
800
801 case MATCH_OPERAND:
802 case MATCH_SCRATCH:
803 case MATCH_OPERATOR:
804 {
805 RTX_CODE was_code = code;
806 const char *pred_name;
807 bool allows_const_int = true;
808
809 if (code == MATCH_SCRATCH)
810 {
811 pred_name = "scratch_operand";
812 code = UNKNOWN;
813 }
814 else
815 {
816 pred_name = XSTR (pattern, 1);
817 if (code == MATCH_PARALLEL)
818 code = PARALLEL;
819 else
820 code = UNKNOWN;
821 }
822
823 if (pred_name[0] != 0)
824 {
825 const struct pred_data *pred;
826
827 test = new_decision_test (DT_pred, &place);
828 test->u.pred.name = pred_name;
829 test->u.pred.mode = mode;
830
831 /* See if we know about this predicate.
832 If we do, remember it for use below.
833
834 We can optimize the generated code a little if either
835 (a) the predicate only accepts one code, or (b) the
836 predicate does not allow CONST_INT or CONST_WIDE_INT,
837 in which case it can match only if the modes match. */
838 pred = lookup_predicate (pred_name);
839 if (pred)
840 {
841 test->u.pred.data = pred;
842 allows_const_int = (pred->codes[CONST_INT]
843 || pred->codes[CONST_WIDE_INT]);
844 if (was_code == MATCH_PARALLEL
845 && pred->singleton != PARALLEL)
846 error_with_line (pattern_lineno,
847 "predicate '%s' used in match_parallel "
848 "does not allow only PARALLEL", pred->name);
849 else
850 code = pred->singleton;
851 }
852 else
853 error_with_line (pattern_lineno,
854 "unknown predicate '%s' in '%s' expression",
855 pred_name, GET_RTX_NAME (was_code));
856 }
857
858 /* Can't enforce a mode if we allow const_int. */
859 if (allows_const_int)
860 mode = VOIDmode;
861
862 /* Accept the operand, i.e. record it in `operands'. */
863 test = new_decision_test (DT_accept_op, &place);
864 test->u.opno = XINT (pattern, 0);
865
866 if (was_code == MATCH_OPERATOR || was_code == MATCH_PARALLEL)
867 {
868 if (was_code == MATCH_OPERATOR)
869 {
870 pos_type = POS_XEXP;
871 subpos_ptr = &pos->xexps;
872 }
873 else
874 {
875 pos_type = POS_XVECEXP0;
876 subpos_ptr = &pos->xvecexp0s;
877 }
878 for (i = 0; i < (size_t) XVECLEN (pattern, 2); i++)
879 {
880 subpos = next_position (subpos_ptr, pos, pos_type, i);
881 sub = add_to_sequence (XVECEXP (pattern, 2, i),
882 &sub->success, subpos, insn_type, 0);
883 subpos_ptr = &subpos->next;
884 }
885 }
886 goto fini;
887 }
888
889 case MATCH_OP_DUP:
890 code = UNKNOWN;
891
892 test = new_decision_test (DT_dup, &place);
893 test->u.dup = XINT (pattern, 0);
894
895 test = new_decision_test (DT_accept_op, &place);
896 test->u.opno = XINT (pattern, 0);
897
898 subpos_ptr = &pos->xexps;
899 for (i = 0; i < (size_t) XVECLEN (pattern, 1); i++)
900 {
901 subpos = next_position (subpos_ptr, pos, POS_XEXP, i);
902 sub = add_to_sequence (XVECEXP (pattern, 1, i),
903 &sub->success, subpos, insn_type, 0);
904 subpos_ptr = &subpos->next;
905 }
906 goto fini;
907
908 case MATCH_DUP:
909 case MATCH_PAR_DUP:
910 code = UNKNOWN;
911
912 test = new_decision_test (DT_dup, &place);
913 test->u.dup = XINT (pattern, 0);
914 goto fini;
915
916 default:
917 break;
918 }
919
920 fmt = GET_RTX_FORMAT (code);
921 len = GET_RTX_LENGTH (code);
922
923 /* Do tests against the current node first. */
924 for (i = 0; i < (size_t) len; i++)
925 {
926 if (fmt[i] == 'i')
927 {
928 gcc_assert (i < 2);
929
930 if (!i)
931 {
932 test = new_decision_test (DT_elt_zero_int, &place);
933 test->u.intval = XINT (pattern, i);
934 }
935 else
936 {
937 test = new_decision_test (DT_elt_one_int, &place);
938 test->u.intval = XINT (pattern, i);
939 }
940 }
941 else if (fmt[i] == 'w')
942 {
943 /* If this value actually fits in an int, we can use a switch
944 statement here, so indicate that. */
945 enum decision_type type
946 = ((int) XWINT (pattern, i) == XWINT (pattern, i))
947 ? DT_elt_zero_wide_safe : DT_elt_zero_wide;
948
949 gcc_assert (!i);
950
951 test = new_decision_test (type, &place);
952 test->u.intval = XWINT (pattern, i);
953 }
954 else if (fmt[i] == 'E')
955 {
956 gcc_assert (!i);
957
958 test = new_decision_test (DT_veclen, &place);
959 test->u.veclen = XVECLEN (pattern, i);
960 }
961 }
962
963 /* Now test our sub-patterns. */
964 subpos_ptr = &pos->xexps;
965 for (i = 0; i < (size_t) len; i++)
966 {
967 subpos = next_position (subpos_ptr, pos, POS_XEXP, i);
968 switch (fmt[i])
969 {
970 case 'e': case 'u':
971 sub = add_to_sequence (XEXP (pattern, i), &sub->success,
972 subpos, insn_type, 0);
973 break;
974
975 case 'E':
976 {
977 struct position *subpos2, **subpos2_ptr;
978 int j;
979
980 subpos2_ptr = &pos->xvecexp0s;
981 for (j = 0; j < XVECLEN (pattern, i); j++)
982 {
983 subpos2 = next_position (subpos2_ptr, pos, POS_XVECEXP0, j);
984 sub = add_to_sequence (XVECEXP (pattern, i, j),
985 &sub->success, subpos2, insn_type, 0);
986 subpos2_ptr = &subpos2->next;
987 }
988 break;
989 }
990
991 case 'i': case 'w':
992 /* Handled above. */
993 break;
994 case '0':
995 break;
996
997 default:
998 gcc_unreachable ();
999 }
1000 subpos_ptr = &subpos->next;
1001 }
1002
1003 fini:
1004 /* Insert nodes testing mode and code, if they're still relevant,
1005 before any of the nodes we may have added above. */
1006 if (code != UNKNOWN)
1007 {
1008 place = &this_decision->tests;
1009 test = new_decision_test (DT_code, &place);
1010 test->u.code = code;
1011 }
1012
1013 if (mode != VOIDmode)
1014 {
1015 place = &this_decision->tests;
1016 test = new_decision_test (DT_mode, &place);
1017 test->u.mode = mode;
1018 }
1019
1020 /* If we didn't insert any tests or accept nodes, hork. */
1021 gcc_assert (this_decision->tests);
1022
1023 ret:
1024 return sub;
1025 }
1026 \f
1027 /* A subroutine of maybe_both_true; examines only one test.
1028 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
1029
1030 static int
1031 maybe_both_true_2 (struct decision_test *d1, struct decision_test *d2)
1032 {
1033 if (d1->type == d2->type)
1034 {
1035 switch (d1->type)
1036 {
1037 case DT_num_insns:
1038 if (d1->u.num_insns == d2->u.num_insns)
1039 return 1;
1040 else
1041 return -1;
1042
1043 case DT_mode:
1044 return d1->u.mode == d2->u.mode;
1045
1046 case DT_code:
1047 return d1->u.code == d2->u.code;
1048
1049 case DT_veclen:
1050 return d1->u.veclen == d2->u.veclen;
1051
1052 case DT_elt_zero_int:
1053 case DT_elt_one_int:
1054 case DT_elt_zero_wide:
1055 case DT_elt_zero_wide_safe:
1056 return d1->u.intval == d2->u.intval;
1057
1058 default:
1059 break;
1060 }
1061 }
1062
1063 /* If either has a predicate that we know something about, set
1064 things up so that D1 is the one that always has a known
1065 predicate. Then see if they have any codes in common. */
1066
1067 if (d1->type == DT_pred || d2->type == DT_pred)
1068 {
1069 if (d2->type == DT_pred)
1070 {
1071 struct decision_test *tmp;
1072 tmp = d1, d1 = d2, d2 = tmp;
1073 }
1074
1075 /* If D2 tests a mode, see if it matches D1. */
1076 if (d1->u.pred.mode != VOIDmode)
1077 {
1078 if (d2->type == DT_mode)
1079 {
1080 if (d1->u.pred.mode != d2->u.mode
1081 /* The mode of an address_operand predicate is the
1082 mode of the memory, not the operand. It can only
1083 be used for testing the predicate, so we must
1084 ignore it here. */
1085 && strcmp (d1->u.pred.name, "address_operand") != 0)
1086 return 0;
1087 }
1088 /* Don't check two predicate modes here, because if both predicates
1089 accept CONST_INT, then both can still be true even if the modes
1090 are different. If they don't accept CONST_INT, there will be a
1091 separate DT_mode that will make maybe_both_true_1 return 0. */
1092 }
1093
1094 if (d1->u.pred.data)
1095 {
1096 /* If D2 tests a code, see if it is in the list of valid
1097 codes for D1's predicate. */
1098 if (d2->type == DT_code)
1099 {
1100 if (!d1->u.pred.data->codes[d2->u.code])
1101 return 0;
1102 }
1103
1104 /* Otherwise see if the predicates have any codes in common. */
1105 else if (d2->type == DT_pred && d2->u.pred.data)
1106 {
1107 bool common = false;
1108 int c;
1109
1110 for (c = 0; c < NUM_RTX_CODE; c++)
1111 if (d1->u.pred.data->codes[c] && d2->u.pred.data->codes[c])
1112 {
1113 common = true;
1114 break;
1115 }
1116
1117 if (!common)
1118 return 0;
1119 }
1120 }
1121 }
1122
1123 /* Tests vs veclen may be known when strict equality is involved. */
1124 if (d1->type == DT_veclen && d2->type == DT_veclen_ge)
1125 return d1->u.veclen >= d2->u.veclen;
1126 if (d1->type == DT_veclen_ge && d2->type == DT_veclen)
1127 return d2->u.veclen >= d1->u.veclen;
1128
1129 return -1;
1130 }
1131
1132 /* A subroutine of maybe_both_true; examines all the tests for a given node.
1133 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
1134
1135 static int
1136 maybe_both_true_1 (struct decision_test *d1, struct decision_test *d2)
1137 {
1138 struct decision_test *t1, *t2;
1139
1140 /* A match_operand with no predicate can match anything. Recognize
1141 this by the existence of a lone DT_accept_op test. */
1142 if (d1->type == DT_accept_op || d2->type == DT_accept_op)
1143 return 1;
1144
1145 /* Eliminate pairs of tests while they can exactly match. */
1146 while (d1 && d2 && d1->type == d2->type)
1147 {
1148 if (maybe_both_true_2 (d1, d2) == 0)
1149 return 0;
1150 d1 = d1->next, d2 = d2->next;
1151 }
1152
1153 /* After that, consider all pairs. */
1154 for (t1 = d1; t1 ; t1 = t1->next)
1155 for (t2 = d2; t2 ; t2 = t2->next)
1156 if (maybe_both_true_2 (t1, t2) == 0)
1157 return 0;
1158
1159 return -1;
1160 }
1161
1162 /* Return 0 if we can prove that there is no RTL that can match both
1163 D1 and D2. Otherwise, return 1 (it may be that there is an RTL that
1164 can match both or just that we couldn't prove there wasn't such an RTL).
1165
1166 TOPLEVEL is nonzero if we are to only look at the top level and not
1167 recursively descend. */
1168
1169 static int
1170 maybe_both_true (struct decision *d1, struct decision *d2,
1171 int toplevel)
1172 {
1173 struct decision *p1, *p2;
1174 int cmp;
1175
1176 /* Don't compare strings on the different positions in insn. Doing so
1177 is incorrect and results in false matches from constructs like
1178
1179 [(set (subreg:HI (match_operand:SI "register_operand" "r") 0)
1180 (subreg:HI (match_operand:SI "register_operand" "r") 0))]
1181 vs
1182 [(set (match_operand:HI "register_operand" "r")
1183 (match_operand:HI "register_operand" "r"))]
1184
1185 If we are presented with such, we are recursing through the remainder
1186 of a node's success nodes (from the loop at the end of this function).
1187 Skip forward until we come to a position that matches.
1188
1189 Due to the way positions are constructed, we know that iterating
1190 forward from the lexically lower position will run into the lexically
1191 higher position and not the other way around. This saves a bit
1192 of effort. */
1193
1194 cmp = compare_positions (d1->position, d2->position);
1195 if (cmp != 0)
1196 {
1197 gcc_assert (!toplevel);
1198
1199 /* If the d2->position was lexically lower, swap. */
1200 if (cmp > 0)
1201 p1 = d1, d1 = d2, d2 = p1;
1202
1203 if (d1->success.first == 0)
1204 return 1;
1205 for (p1 = d1->success.first; p1; p1 = p1->next)
1206 if (maybe_both_true (p1, d2, 0))
1207 return 1;
1208
1209 return 0;
1210 }
1211
1212 /* Test the current level. */
1213 cmp = maybe_both_true_1 (d1->tests, d2->tests);
1214 if (cmp >= 0)
1215 return cmp;
1216
1217 /* We can't prove that D1 and D2 cannot both be true. If we are only
1218 to check the top level, return 1. Otherwise, see if we can prove
1219 that all choices in both successors are mutually exclusive. If
1220 either does not have any successors, we can't prove they can't both
1221 be true. */
1222
1223 if (toplevel || d1->success.first == 0 || d2->success.first == 0)
1224 return 1;
1225
1226 for (p1 = d1->success.first; p1; p1 = p1->next)
1227 for (p2 = d2->success.first; p2; p2 = p2->next)
1228 if (maybe_both_true (p1, p2, 0))
1229 return 1;
1230
1231 return 0;
1232 }
1233
1234 /* A subroutine of nodes_identical. Examine two tests for equivalence. */
1235
1236 static int
1237 nodes_identical_1 (struct decision_test *d1, struct decision_test *d2)
1238 {
1239 switch (d1->type)
1240 {
1241 case DT_num_insns:
1242 return d1->u.num_insns == d2->u.num_insns;
1243
1244 case DT_mode:
1245 return d1->u.mode == d2->u.mode;
1246
1247 case DT_code:
1248 return d1->u.code == d2->u.code;
1249
1250 case DT_pred:
1251 return (d1->u.pred.mode == d2->u.pred.mode
1252 && strcmp (d1->u.pred.name, d2->u.pred.name) == 0);
1253
1254 case DT_c_test:
1255 return strcmp (d1->u.c_test, d2->u.c_test) == 0;
1256
1257 case DT_veclen:
1258 case DT_veclen_ge:
1259 return d1->u.veclen == d2->u.veclen;
1260
1261 case DT_dup:
1262 return d1->u.dup == d2->u.dup;
1263
1264 case DT_elt_zero_int:
1265 case DT_elt_one_int:
1266 case DT_elt_zero_wide:
1267 case DT_elt_zero_wide_safe:
1268 return d1->u.intval == d2->u.intval;
1269
1270 case DT_accept_op:
1271 return d1->u.opno == d2->u.opno;
1272
1273 case DT_accept_insn:
1274 /* Differences will be handled in merge_accept_insn. */
1275 return 1;
1276
1277 default:
1278 gcc_unreachable ();
1279 }
1280 }
1281
1282 /* True iff the two nodes are identical (on one level only). Due
1283 to the way these lists are constructed, we shouldn't have to
1284 consider different orderings on the tests. */
1285
1286 static int
1287 nodes_identical (struct decision *d1, struct decision *d2)
1288 {
1289 struct decision_test *t1, *t2;
1290
1291 for (t1 = d1->tests, t2 = d2->tests; t1 && t2; t1 = t1->next, t2 = t2->next)
1292 {
1293 if (t1->type != t2->type)
1294 return 0;
1295 if (! nodes_identical_1 (t1, t2))
1296 return 0;
1297 }
1298
1299 /* For success, they should now both be null. */
1300 if (t1 != t2)
1301 return 0;
1302
1303 /* Check that their subnodes are at the same position, as any one set
1304 of sibling decisions must be at the same position. Allowing this
1305 requires complications to find_afterward and when change_state is
1306 invoked. */
1307 if (d1->success.first
1308 && d2->success.first
1309 && d1->success.first->position != d2->success.first->position)
1310 return 0;
1311
1312 return 1;
1313 }
1314
1315 /* A subroutine of merge_trees; given two nodes that have been declared
1316 identical, cope with two insn accept states. If they differ in the
1317 number of clobbers, then the conflict was created by make_insn_sequence
1318 and we can drop the with-clobbers version on the floor. If both
1319 nodes have no additional clobbers, we have found an ambiguity in the
1320 source machine description. */
1321
1322 static void
1323 merge_accept_insn (struct decision *oldd, struct decision *addd)
1324 {
1325 struct decision_test *old, *add;
1326
1327 for (old = oldd->tests; old; old = old->next)
1328 if (old->type == DT_accept_insn)
1329 break;
1330 if (old == NULL)
1331 return;
1332
1333 for (add = addd->tests; add; add = add->next)
1334 if (add->type == DT_accept_insn)
1335 break;
1336 if (add == NULL)
1337 return;
1338
1339 /* If one node is for a normal insn and the second is for the base
1340 insn with clobbers stripped off, the second node should be ignored. */
1341
1342 if (old->u.insn.num_clobbers_to_add == 0
1343 && add->u.insn.num_clobbers_to_add > 0)
1344 {
1345 /* Nothing to do here. */
1346 }
1347 else if (old->u.insn.num_clobbers_to_add > 0
1348 && add->u.insn.num_clobbers_to_add == 0)
1349 {
1350 /* In this case, replace OLD with ADD. */
1351 old->u.insn = add->u.insn;
1352 }
1353 else
1354 {
1355 error_with_line (add->u.insn.lineno, "`%s' matches `%s'",
1356 get_insn_name (add->u.insn.code_number),
1357 get_insn_name (old->u.insn.code_number));
1358 message_with_line (old->u.insn.lineno, "previous definition of `%s'",
1359 get_insn_name (old->u.insn.code_number));
1360 }
1361 }
1362
1363 /* Merge two decision trees OLDH and ADDH, modifying OLDH destructively. */
1364
1365 static void
1366 merge_trees (struct decision_head *oldh, struct decision_head *addh)
1367 {
1368 struct decision *next, *add;
1369
1370 if (addh->first == 0)
1371 return;
1372 if (oldh->first == 0)
1373 {
1374 *oldh = *addh;
1375 return;
1376 }
1377
1378 /* Trying to merge bits at different positions isn't possible. */
1379 gcc_assert (oldh->first->position == addh->first->position);
1380
1381 for (add = addh->first; add ; add = next)
1382 {
1383 struct decision *old, *insert_before = NULL;
1384
1385 next = add->next;
1386
1387 /* The semantics of pattern matching state that the tests are
1388 done in the order given in the MD file so that if an insn
1389 matches two patterns, the first one will be used. However,
1390 in practice, most, if not all, patterns are unambiguous so
1391 that their order is independent. In that case, we can merge
1392 identical tests and group all similar modes and codes together.
1393
1394 Scan starting from the end of OLDH until we reach a point
1395 where we reach the head of the list or where we pass a
1396 pattern that could also be true if NEW is true. If we find
1397 an identical pattern, we can merge them. Also, record the
1398 last node that tests the same code and mode and the last one
1399 that tests just the same mode.
1400
1401 If we have no match, place NEW after the closest match we found. */
1402
1403 for (old = oldh->last; old; old = old->prev)
1404 {
1405 if (nodes_identical (old, add))
1406 {
1407 merge_accept_insn (old, add);
1408 merge_trees (&old->success, &add->success);
1409 goto merged_nodes;
1410 }
1411
1412 if (maybe_both_true (old, add, 0))
1413 break;
1414
1415 /* Insert the nodes in DT test type order, which is roughly
1416 how expensive/important the test is. Given that the tests
1417 are also ordered within the list, examining the first is
1418 sufficient. */
1419 if ((int) add->tests->type < (int) old->tests->type)
1420 insert_before = old;
1421 }
1422
1423 if (insert_before == NULL)
1424 {
1425 add->next = NULL;
1426 add->prev = oldh->last;
1427 oldh->last->next = add;
1428 oldh->last = add;
1429 }
1430 else
1431 {
1432 if ((add->prev = insert_before->prev) != NULL)
1433 add->prev->next = add;
1434 else
1435 oldh->first = add;
1436 add->next = insert_before;
1437 insert_before->prev = add;
1438 }
1439
1440 merged_nodes:;
1441 }
1442 }
1443 \f
1444 /* Walk the tree looking for sub-nodes that perform common tests.
1445 Factor out the common test into a new node. This enables us
1446 (depending on the test type) to emit switch statements later. */
1447
1448 static void
1449 factor_tests (struct decision_head *head)
1450 {
1451 struct decision *first, *next;
1452
1453 for (first = head->first; first && first->next; first = next)
1454 {
1455 enum decision_type type;
1456 struct decision *new_dec, *old_last;
1457
1458 type = first->tests->type;
1459 next = first->next;
1460
1461 /* Want at least two compatible sequential nodes. */
1462 if (next->tests->type != type)
1463 continue;
1464
1465 /* Don't want all node types, just those we can turn into
1466 switch statements. */
1467 if (type != DT_mode
1468 && type != DT_code
1469 && type != DT_veclen
1470 && type != DT_elt_zero_int
1471 && type != DT_elt_one_int
1472 && type != DT_elt_zero_wide_safe)
1473 continue;
1474
1475 /* If we'd been performing more than one test, create a new node
1476 below our first test. */
1477 if (first->tests->next != NULL)
1478 {
1479 new_dec = new_decision (first->position, &first->success);
1480 new_dec->tests = first->tests->next;
1481 first->tests->next = NULL;
1482 }
1483
1484 /* Crop the node tree off after our first test. */
1485 first->next = NULL;
1486 old_last = head->last;
1487 head->last = first;
1488
1489 /* For each compatible test, adjust to perform only one test in
1490 the top level node, then merge the node back into the tree. */
1491 do
1492 {
1493 struct decision_head h;
1494
1495 if (next->tests->next != NULL)
1496 {
1497 new_dec = new_decision (next->position, &next->success);
1498 new_dec->tests = next->tests->next;
1499 next->tests->next = NULL;
1500 }
1501 new_dec = next;
1502 next = next->next;
1503 new_dec->next = NULL;
1504 h.first = h.last = new_dec;
1505
1506 merge_trees (head, &h);
1507 }
1508 while (next && next->tests->type == type);
1509
1510 /* After we run out of compatible tests, graft the remaining nodes
1511 back onto the tree. */
1512 if (next)
1513 {
1514 next->prev = head->last;
1515 head->last->next = next;
1516 head->last = old_last;
1517 }
1518 }
1519
1520 /* Recurse. */
1521 for (first = head->first; first; first = first->next)
1522 factor_tests (&first->success);
1523 }
1524
1525 /* After factoring, try to simplify the tests on any one node.
1526 Tests that are useful for switch statements are recognizable
1527 by having only a single test on a node -- we'll be manipulating
1528 nodes with multiple tests:
1529
1530 If we have mode tests or code tests that are redundant with
1531 predicates, remove them. */
1532
1533 static void
1534 simplify_tests (struct decision_head *head)
1535 {
1536 struct decision *tree;
1537
1538 for (tree = head->first; tree; tree = tree->next)
1539 {
1540 struct decision_test *a, *b;
1541
1542 a = tree->tests;
1543 b = a->next;
1544 if (b == NULL)
1545 continue;
1546
1547 /* Find a predicate node. */
1548 while (b && b->type != DT_pred)
1549 b = b->next;
1550 if (b)
1551 {
1552 /* Due to how these tests are constructed, we don't even need
1553 to check that the mode and code are compatible -- they were
1554 generated from the predicate in the first place. */
1555 while (a->type == DT_mode || a->type == DT_code)
1556 a = a->next;
1557 tree->tests = a;
1558 }
1559 }
1560
1561 /* Recurse. */
1562 for (tree = head->first; tree; tree = tree->next)
1563 simplify_tests (&tree->success);
1564 }
1565
1566 /* Count the number of subnodes of HEAD. If the number is high enough,
1567 make the first node in HEAD start a separate subroutine in the C code
1568 that is generated. */
1569
1570 static int
1571 break_out_subroutines (struct decision_head *head, int initial)
1572 {
1573 int size = 0;
1574 struct decision *sub;
1575
1576 for (sub = head->first; sub; sub = sub->next)
1577 size += 1 + break_out_subroutines (&sub->success, 0);
1578
1579 if (size > SUBROUTINE_THRESHOLD && ! initial)
1580 {
1581 head->first->subroutine_number = ++next_subroutine_number;
1582 size = 1;
1583 }
1584 return size;
1585 }
1586
1587 /* For each node p, find the next alternative that might be true
1588 when p is true. */
1589
1590 static void
1591 find_afterward (struct decision_head *head, struct decision *real_afterward)
1592 {
1593 struct decision *p, *q, *afterward;
1594
1595 /* We can't propagate alternatives across subroutine boundaries.
1596 This is not incorrect, merely a minor optimization loss. */
1597
1598 p = head->first;
1599 afterward = (p->subroutine_number > 0 ? NULL : real_afterward);
1600
1601 for ( ; p ; p = p->next)
1602 {
1603 /* Find the next node that might be true if this one fails. */
1604 for (q = p->next; q ; q = q->next)
1605 if (maybe_both_true (p, q, 1))
1606 break;
1607
1608 /* If we reached the end of the list without finding one,
1609 use the incoming afterward position. */
1610 if (!q)
1611 q = afterward;
1612 p->afterward = q;
1613 if (q)
1614 q->need_label = 1;
1615 }
1616
1617 /* Recurse. */
1618 for (p = head->first; p ; p = p->next)
1619 if (p->success.first)
1620 find_afterward (&p->success, p->afterward);
1621
1622 /* When we are generating a subroutine, record the real afterward
1623 position in the first node where write_tree can find it, and we
1624 can do the right thing at the subroutine call site. */
1625 p = head->first;
1626 if (p->subroutine_number > 0)
1627 p->afterward = real_afterward;
1628 }
1629 \f
1630 /* Assuming that the state of argument is denoted by OLDPOS, take whatever
1631 actions are necessary to move to NEWPOS. If we fail to move to the
1632 new state, branch to node AFTERWARD if nonzero, otherwise return.
1633
1634 Failure to move to the new state can only occur if we are trying to
1635 match multiple insns and we try to step past the end of the stream. */
1636
1637 static void
1638 change_state (struct position *oldpos, struct position *newpos,
1639 const char *indent)
1640 {
1641 while (oldpos->depth > newpos->depth)
1642 oldpos = oldpos->base;
1643
1644 if (oldpos != newpos)
1645 switch (newpos->type)
1646 {
1647 case POS_PEEP2_INSN:
1648 printf ("%stem = peep2_next_insn (%d);\n", indent, newpos->arg);
1649 printf ("%sx%d = PATTERN (tem);\n", indent, newpos->depth);
1650 break;
1651
1652 case POS_XEXP:
1653 change_state (oldpos, newpos->base, indent);
1654 printf ("%sx%d = XEXP (x%d, %d);\n",
1655 indent, newpos->depth, newpos->depth - 1, newpos->arg);
1656 break;
1657
1658 case POS_XVECEXP0:
1659 change_state (oldpos, newpos->base, indent);
1660 printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1661 indent, newpos->depth, newpos->depth - 1, newpos->arg);
1662 break;
1663 }
1664 }
1665 \f
1666 /* Print the enumerator constant for CODE -- the upcase version of
1667 the name. */
1668
1669 static void
1670 print_code (enum rtx_code code)
1671 {
1672 const char *p;
1673 for (p = GET_RTX_NAME (code); *p; p++)
1674 putchar (TOUPPER (*p));
1675 }
1676
1677 /* Emit code to cross an afterward link -- change state and branch. */
1678
1679 static void
1680 write_afterward (struct decision *start, struct decision *afterward,
1681 const char *indent)
1682 {
1683 if (!afterward || start->subroutine_number > 0)
1684 printf ("%sgoto ret0;\n", indent);
1685 else
1686 {
1687 change_state (start->position, afterward->position, indent);
1688 printf ("%sgoto L%d;\n", indent, afterward->number);
1689 }
1690 }
1691
1692 /* Emit a HOST_WIDE_INT as an integer constant expression. We need to take
1693 special care to avoid "decimal constant is so large that it is unsigned"
1694 warnings in the resulting code. */
1695
1696 static void
1697 print_host_wide_int (HOST_WIDE_INT val)
1698 {
1699 HOST_WIDE_INT min = (unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT-1);
1700 if (val == min)
1701 printf ("(" HOST_WIDE_INT_PRINT_DEC_C "-1)", val + 1);
1702 else
1703 printf (HOST_WIDE_INT_PRINT_DEC_C, val);
1704 }
1705
1706 /* Emit a switch statement, if possible, for an initial sequence of
1707 nodes at START. Return the first node yet untested. */
1708
1709 static struct decision *
1710 write_switch (struct decision *start, int depth)
1711 {
1712 struct decision *p = start;
1713 enum decision_type type = p->tests->type;
1714 struct decision *needs_label = NULL;
1715
1716 /* If we have two or more nodes in sequence that test the same one
1717 thing, we may be able to use a switch statement. */
1718
1719 if (!p->next
1720 || p->tests->next
1721 || p->next->tests->type != type
1722 || p->next->tests->next
1723 || nodes_identical_1 (p->tests, p->next->tests))
1724 return p;
1725
1726 /* DT_code is special in that we can do interesting things with
1727 known predicates at the same time. */
1728 if (type == DT_code)
1729 {
1730 char codemap[NUM_RTX_CODE];
1731 struct decision *ret;
1732 RTX_CODE code;
1733
1734 memset (codemap, 0, sizeof (codemap));
1735
1736 printf (" switch (GET_CODE (x%d))\n {\n", depth);
1737 code = p->tests->u.code;
1738 do
1739 {
1740 if (p != start && p->need_label && needs_label == NULL)
1741 needs_label = p;
1742
1743 printf (" case ");
1744 print_code (code);
1745 printf (":\n goto L%d;\n", p->success.first->number);
1746 p->success.first->need_label = 1;
1747
1748 codemap[code] = 1;
1749 p = p->next;
1750 }
1751 while (p
1752 && ! p->tests->next
1753 && p->tests->type == DT_code
1754 && ! codemap[code = p->tests->u.code]);
1755
1756 /* If P is testing a predicate that we know about and we haven't
1757 seen any of the codes that are valid for the predicate, we can
1758 write a series of "case" statement, one for each possible code.
1759 Since we are already in a switch, these redundant tests are very
1760 cheap and will reduce the number of predicates called. */
1761
1762 /* Note that while we write out cases for these predicates here,
1763 we don't actually write the test here, as it gets kinda messy.
1764 It is trivial to leave this to later by telling our caller that
1765 we only processed the CODE tests. */
1766 if (needs_label != NULL)
1767 ret = needs_label;
1768 else
1769 ret = p;
1770
1771 while (p && p->tests->type == DT_pred && p->tests->u.pred.data)
1772 {
1773 const struct pred_data *data = p->tests->u.pred.data;
1774 int c;
1775
1776 for (c = 0; c < NUM_RTX_CODE; c++)
1777 if (codemap[c] && data->codes[c])
1778 goto pred_done;
1779
1780 for (c = 0; c < NUM_RTX_CODE; c++)
1781 if (data->codes[c])
1782 {
1783 fputs (" case ", stdout);
1784 print_code ((enum rtx_code) c);
1785 fputs (":\n", stdout);
1786 codemap[c] = 1;
1787 }
1788
1789 printf (" goto L%d;\n", p->number);
1790 p->need_label = 1;
1791 p = p->next;
1792 }
1793
1794 pred_done:
1795 /* Make the default case skip the predicates we managed to match. */
1796
1797 printf (" default:\n");
1798 if (p != ret)
1799 {
1800 if (p)
1801 {
1802 printf (" goto L%d;\n", p->number);
1803 p->need_label = 1;
1804 }
1805 else
1806 write_afterward (start, start->afterward, " ");
1807 }
1808 else
1809 printf (" break;\n");
1810 printf (" }\n");
1811
1812 return ret;
1813 }
1814 else if (type == DT_mode
1815 || type == DT_veclen
1816 || type == DT_elt_zero_int
1817 || type == DT_elt_one_int
1818 || type == DT_elt_zero_wide_safe)
1819 {
1820 const char *indent = "";
1821
1822 /* We cast switch parameter to integer, so we must ensure that the value
1823 fits. */
1824 if (type == DT_elt_zero_wide_safe)
1825 {
1826 indent = " ";
1827 printf (" if ((int) XWINT (x%d, 0) == XWINT (x%d, 0))\n",
1828 depth, depth);
1829 }
1830 printf ("%s switch (", indent);
1831 switch (type)
1832 {
1833 case DT_mode:
1834 printf ("GET_MODE (x%d)", depth);
1835 break;
1836 case DT_veclen:
1837 printf ("XVECLEN (x%d, 0)", depth);
1838 break;
1839 case DT_elt_zero_int:
1840 printf ("XINT (x%d, 0)", depth);
1841 break;
1842 case DT_elt_one_int:
1843 printf ("XINT (x%d, 1)", depth);
1844 break;
1845 case DT_elt_zero_wide_safe:
1846 /* Convert result of XWINT to int for portability since some C
1847 compilers won't do it and some will. */
1848 printf ("(int) XWINT (x%d, 0)", depth);
1849 break;
1850 default:
1851 gcc_unreachable ();
1852 }
1853 printf (")\n%s {\n", indent);
1854
1855 do
1856 {
1857 /* Merge trees will not unify identical nodes if their
1858 sub-nodes are at different levels. Thus we must check
1859 for duplicate cases. */
1860 struct decision *q;
1861 for (q = start; q != p; q = q->next)
1862 if (nodes_identical_1 (p->tests, q->tests))
1863 goto case_done;
1864
1865 if (p != start && p->need_label && needs_label == NULL)
1866 needs_label = p;
1867
1868 printf ("%s case ", indent);
1869 switch (type)
1870 {
1871 case DT_mode:
1872 printf ("%smode", GET_MODE_NAME (p->tests->u.mode));
1873 break;
1874 case DT_veclen:
1875 printf ("%d", p->tests->u.veclen);
1876 break;
1877 case DT_elt_zero_int:
1878 case DT_elt_one_int:
1879 case DT_elt_zero_wide:
1880 case DT_elt_zero_wide_safe:
1881 print_host_wide_int (p->tests->u.intval);
1882 break;
1883 default:
1884 gcc_unreachable ();
1885 }
1886 printf (":\n%s goto L%d;\n", indent, p->success.first->number);
1887 p->success.first->need_label = 1;
1888
1889 p = p->next;
1890 }
1891 while (p && p->tests->type == type && !p->tests->next);
1892
1893 case_done:
1894 printf ("%s default:\n%s break;\n%s }\n",
1895 indent, indent, indent);
1896
1897 return needs_label != NULL ? needs_label : p;
1898 }
1899 else
1900 {
1901 /* None of the other tests are amenable. */
1902 return p;
1903 }
1904 }
1905
1906 /* Emit code for one test. */
1907
1908 static void
1909 write_cond (struct decision_test *p, int depth,
1910 enum routine_type subroutine_type)
1911 {
1912 switch (p->type)
1913 {
1914 case DT_num_insns:
1915 printf ("peep2_current_count >= %d", p->u.num_insns);
1916 break;
1917
1918 case DT_mode:
1919 printf ("GET_MODE (x%d) == %smode", depth, GET_MODE_NAME (p->u.mode));
1920 break;
1921
1922 case DT_code:
1923 printf ("GET_CODE (x%d) == ", depth);
1924 print_code (p->u.code);
1925 break;
1926
1927 case DT_veclen:
1928 printf ("XVECLEN (x%d, 0) == %d", depth, p->u.veclen);
1929 break;
1930
1931 case DT_elt_zero_int:
1932 printf ("XINT (x%d, 0) == %d", depth, (int) p->u.intval);
1933 break;
1934
1935 case DT_elt_one_int:
1936 printf ("XINT (x%d, 1) == %d", depth, (int) p->u.intval);
1937 break;
1938
1939 case DT_elt_zero_wide:
1940 case DT_elt_zero_wide_safe:
1941 printf ("XWINT (x%d, 0) == ", depth);
1942 print_host_wide_int (p->u.intval);
1943 break;
1944
1945 case DT_const_int:
1946 printf ("x%d == const_int_rtx[MAX_SAVED_CONST_INT + (%d)]",
1947 depth, (int) p->u.intval);
1948 break;
1949
1950 case DT_veclen_ge:
1951 printf ("XVECLEN (x%d, 0) >= %d", depth, p->u.veclen);
1952 break;
1953
1954 case DT_dup:
1955 printf ("rtx_equal_p (x%d, operands[%d])", depth, p->u.dup);
1956 break;
1957
1958 case DT_pred:
1959 printf ("%s (x%d, %smode)", p->u.pred.name, depth,
1960 GET_MODE_NAME (p->u.pred.mode));
1961 break;
1962
1963 case DT_c_test:
1964 print_c_condition (p->u.c_test);
1965 break;
1966
1967 case DT_accept_insn:
1968 gcc_assert (subroutine_type == RECOG);
1969 gcc_assert (p->u.insn.num_clobbers_to_add);
1970 printf ("pnum_clobbers != NULL");
1971 break;
1972
1973 default:
1974 gcc_unreachable ();
1975 }
1976 }
1977
1978 /* Emit code for one action. The previous tests have succeeded;
1979 TEST is the last of the chain. In the normal case we simply
1980 perform a state change. For the `accept' tests we must do more work. */
1981
1982 static void
1983 write_action (struct decision *p, struct decision_test *test,
1984 int depth, int uncond, struct decision *success,
1985 enum routine_type subroutine_type)
1986 {
1987 const char *indent;
1988 int want_close = 0;
1989
1990 if (uncond)
1991 indent = " ";
1992 else if (test->type == DT_accept_op || test->type == DT_accept_insn)
1993 {
1994 fputs (" {\n", stdout);
1995 indent = " ";
1996 want_close = 1;
1997 }
1998 else
1999 indent = " ";
2000
2001 if (test->type == DT_accept_op)
2002 {
2003 printf ("%soperands[%d] = x%d;\n", indent, test->u.opno, depth);
2004
2005 /* Only allow DT_accept_insn to follow. */
2006 if (test->next)
2007 {
2008 test = test->next;
2009 gcc_assert (test->type == DT_accept_insn);
2010 }
2011 }
2012
2013 /* Sanity check that we're now at the end of the list of tests. */
2014 gcc_assert (!test->next);
2015
2016 if (test->type == DT_accept_insn)
2017 {
2018 switch (subroutine_type)
2019 {
2020 case RECOG:
2021 if (test->u.insn.num_clobbers_to_add != 0)
2022 printf ("%s*pnum_clobbers = %d;\n",
2023 indent, test->u.insn.num_clobbers_to_add);
2024 printf ("%sreturn %d; /* %s */\n", indent,
2025 test->u.insn.code_number,
2026 get_insn_name (test->u.insn.code_number));
2027 break;
2028
2029 case SPLIT:
2030 printf ("%sreturn gen_split_%d (insn, operands);\n",
2031 indent, test->u.insn.code_number);
2032 break;
2033
2034 case PEEPHOLE2:
2035 {
2036 int match_len = 0;
2037 struct position *pos;
2038
2039 for (pos = p->position; pos; pos = pos->base)
2040 if (pos->type == POS_PEEP2_INSN)
2041 {
2042 match_len = pos->arg;
2043 break;
2044 }
2045 printf ("%s*_pmatch_len = %d;\n", indent, match_len);
2046 printf ("%stem = gen_peephole2_%d (insn, operands);\n",
2047 indent, test->u.insn.code_number);
2048 printf ("%sif (tem != 0)\n%s return tem;\n", indent, indent);
2049 }
2050 break;
2051
2052 default:
2053 gcc_unreachable ();
2054 }
2055 }
2056 else
2057 {
2058 printf ("%sgoto L%d;\n", indent, success->number);
2059 success->need_label = 1;
2060 }
2061
2062 if (want_close)
2063 fputs (" }\n", stdout);
2064 }
2065
2066 /* Return 1 if the test is always true and has no fallthru path. Return -1
2067 if the test does have a fallthru path, but requires that the condition be
2068 terminated. Otherwise return 0 for a normal test. */
2069 /* ??? is_unconditional is a stupid name for a tri-state function. */
2070
2071 static int
2072 is_unconditional (struct decision_test *t, enum routine_type subroutine_type)
2073 {
2074 if (t->type == DT_accept_op)
2075 return 1;
2076
2077 if (t->type == DT_accept_insn)
2078 {
2079 switch (subroutine_type)
2080 {
2081 case RECOG:
2082 return (t->u.insn.num_clobbers_to_add == 0);
2083 case SPLIT:
2084 return 1;
2085 case PEEPHOLE2:
2086 return -1;
2087 default:
2088 gcc_unreachable ();
2089 }
2090 }
2091
2092 return 0;
2093 }
2094
2095 /* Emit code for one node -- the conditional and the accompanying action.
2096 Return true if there is no fallthru path. */
2097
2098 static int
2099 write_node (struct decision *p, int depth,
2100 enum routine_type subroutine_type)
2101 {
2102 struct decision_test *test, *last_test;
2103 int uncond;
2104
2105 /* Scan the tests and simplify comparisons against small
2106 constants. */
2107 for (test = p->tests; test; test = test->next)
2108 {
2109 if (test->type == DT_code
2110 && test->u.code == CONST_INT
2111 && test->next
2112 && test->next->type == DT_elt_zero_wide_safe
2113 && -MAX_SAVED_CONST_INT <= test->next->u.intval
2114 && test->next->u.intval <= MAX_SAVED_CONST_INT)
2115 {
2116 test->type = DT_const_int;
2117 test->u.intval = test->next->u.intval;
2118 test->next = test->next->next;
2119 }
2120 }
2121
2122 last_test = test = p->tests;
2123 uncond = is_unconditional (test, subroutine_type);
2124 if (uncond == 0)
2125 {
2126 printf (" if (");
2127 write_cond (test, depth, subroutine_type);
2128
2129 while ((test = test->next) != NULL)
2130 {
2131 last_test = test;
2132 if (is_unconditional (test, subroutine_type))
2133 break;
2134
2135 printf ("\n && ");
2136 write_cond (test, depth, subroutine_type);
2137 }
2138
2139 printf (")\n");
2140 }
2141
2142 write_action (p, last_test, depth, uncond, p->success.first, subroutine_type);
2143
2144 return uncond > 0;
2145 }
2146
2147 /* Emit code for all of the sibling nodes of HEAD. */
2148
2149 static void
2150 write_tree_1 (struct decision_head *head, int depth,
2151 enum routine_type subroutine_type)
2152 {
2153 struct decision *p, *next;
2154 int uncond = 0;
2155
2156 for (p = head->first; p ; p = next)
2157 {
2158 /* The label for the first element was printed in write_tree. */
2159 if (p != head->first && p->need_label)
2160 OUTPUT_LABEL (" ", p->number);
2161
2162 /* Attempt to write a switch statement for a whole sequence. */
2163 next = write_switch (p, depth);
2164 if (p != next)
2165 uncond = 0;
2166 else
2167 {
2168 /* Failed -- fall back and write one node. */
2169 uncond = write_node (p, depth, subroutine_type);
2170 next = p->next;
2171 }
2172 }
2173
2174 /* Finished with this chain. Close a fallthru path by branching
2175 to the afterward node. */
2176 if (! uncond)
2177 write_afterward (head->last, head->last->afterward, " ");
2178 }
2179
2180 /* Write out the decision tree starting at HEAD. PREVPOS is the
2181 position at the node that branched to this node. */
2182
2183 static void
2184 write_tree (struct decision_head *head, struct position *prevpos,
2185 enum routine_type type, int initial)
2186 {
2187 struct decision *p = head->first;
2188
2189 putchar ('\n');
2190 if (p->need_label)
2191 OUTPUT_LABEL (" ", p->number);
2192
2193 if (! initial && p->subroutine_number > 0)
2194 {
2195 static const char * const name_prefix[] = {
2196 "recog", "split", "peephole2"
2197 };
2198
2199 static const char * const call_suffix[] = {
2200 ", pnum_clobbers", "", ", _pmatch_len"
2201 };
2202
2203 /* This node has been broken out into a separate subroutine.
2204 Call it, test the result, and branch accordingly. */
2205
2206 if (p->afterward)
2207 {
2208 printf (" tem = %s_%d (x0, insn%s);\n",
2209 name_prefix[type], p->subroutine_number, call_suffix[type]);
2210 if (IS_SPLIT (type))
2211 printf (" if (tem != 0)\n return tem;\n");
2212 else
2213 printf (" if (tem >= 0)\n return tem;\n");
2214
2215 change_state (p->position, p->afterward->position, " ");
2216 printf (" goto L%d;\n", p->afterward->number);
2217 }
2218 else
2219 {
2220 printf (" return %s_%d (x0, insn%s);\n",
2221 name_prefix[type], p->subroutine_number, call_suffix[type]);
2222 }
2223 }
2224 else
2225 {
2226 change_state (prevpos, p->position, " ");
2227 write_tree_1 (head, p->position->depth, type);
2228
2229 for (p = head->first; p; p = p->next)
2230 if (p->success.first)
2231 write_tree (&p->success, p->position, type, 0);
2232 }
2233 }
2234
2235 /* Write out a subroutine of type TYPE to do comparisons starting at
2236 node TREE. */
2237
2238 static void
2239 write_subroutine (struct decision_head *head, enum routine_type type)
2240 {
2241 int subfunction = head->first ? head->first->subroutine_number : 0;
2242 const char *s_or_e;
2243 char extension[32];
2244 int i;
2245 const char *insn_param;
2246
2247 s_or_e = subfunction ? "static " : "";
2248
2249 if (subfunction)
2250 sprintf (extension, "_%d", subfunction);
2251 else if (type == RECOG)
2252 extension[0] = '\0';
2253 else
2254 strcpy (extension, "_insns");
2255
2256 /* For now, the top-level functions take a plain "rtx", and perform a
2257 checked cast to "rtx_insn *" for use throughout the rest of the
2258 function and the code it calls. */
2259 insn_param = subfunction ? "rtx_insn *insn" : "rtx uncast_insn";
2260
2261 switch (type)
2262 {
2263 case RECOG:
2264 printf ("%sint\n\
2265 recog%s (rtx x0 ATTRIBUTE_UNUSED,\n\t%s ATTRIBUTE_UNUSED,\n\tint *pnum_clobbers ATTRIBUTE_UNUSED)\n",
2266 s_or_e, extension, insn_param);
2267 break;
2268 case SPLIT:
2269 printf ("%srtx\n\
2270 split%s (rtx x0 ATTRIBUTE_UNUSED, %s ATTRIBUTE_UNUSED)\n",
2271 s_or_e, extension, insn_param);
2272 break;
2273 case PEEPHOLE2:
2274 printf ("%srtx\n\
2275 peephole2%s (rtx x0 ATTRIBUTE_UNUSED,\n\t%s ATTRIBUTE_UNUSED,\n\tint *_pmatch_len ATTRIBUTE_UNUSED)\n",
2276 s_or_e, extension, insn_param);
2277 break;
2278 }
2279
2280 printf ("{\n rtx * const operands ATTRIBUTE_UNUSED = &recog_data.operand[0];\n");
2281 for (i = 1; i <= max_depth; i++)
2282 printf (" rtx x%d ATTRIBUTE_UNUSED;\n", i);
2283
2284 printf (" %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type) ? "rtx" : "int");
2285
2286 if (!subfunction)
2287 printf (" recog_data.insn = NULL_RTX;\n");
2288
2289 /* For now add the downcast to rtx_insn *, at the top of each top-level
2290 function. */
2291 if (!subfunction)
2292 {
2293 printf (" rtx_insn *insn ATTRIBUTE_UNUSED;\n");
2294 printf (" insn = safe_as_a <rtx_insn *> (uncast_insn);\n");
2295 }
2296
2297 if (head->first)
2298 write_tree (head, &root_pos, type, 1);
2299 else
2300 printf (" goto ret0;\n");
2301
2302 printf (" ret0:\n return %d;\n}\n\n", IS_SPLIT (type) ? 0 : -1);
2303 }
2304
2305 /* In break_out_subroutines, we discovered the boundaries for the
2306 subroutines, but did not write them out. Do so now. */
2307
2308 static void
2309 write_subroutines (struct decision_head *head, enum routine_type type)
2310 {
2311 struct decision *p;
2312
2313 for (p = head->first; p ; p = p->next)
2314 if (p->success.first)
2315 write_subroutines (&p->success, type);
2316
2317 if (head->first->subroutine_number > 0)
2318 write_subroutine (head, type);
2319 }
2320
2321 /* Begin the output file. */
2322
2323 static void
2324 write_header (void)
2325 {
2326 puts ("\
2327 /* Generated automatically by the program `genrecog' from the target\n\
2328 machine description file. */\n\
2329 \n\
2330 #include \"config.h\"\n\
2331 #include \"system.h\"\n\
2332 #include \"coretypes.h\"\n\
2333 #include \"tm.h\"\n\
2334 #include \"rtl.h\"\n\
2335 #include \"tm_p.h\"\n\
2336 #include \"hashtab.h\"\n\
2337 #include \"hash-set.h\"\n\
2338 #include \"vec.h\"\n\
2339 #include \"machmode.h\"\n\
2340 #include \"hard-reg-set.h\"\n\
2341 #include \"input.h\"\n\
2342 #include \"function.h\"\n\
2343 #include \"insn-config.h\"\n\
2344 #include \"recog.h\"\n\
2345 #include \"output.h\"\n\
2346 #include \"flags.h\"\n\
2347 #include \"hard-reg-set.h\"\n\
2348 #include \"predict.h\"\n\
2349 #include \"basic-block.h\"\n\
2350 #include \"resource.h\"\n\
2351 #include \"diagnostic-core.h\"\n\
2352 #include \"reload.h\"\n\
2353 #include \"regs.h\"\n\
2354 #include \"tm-constrs.h\"\n\
2355 #include \"predict.h\"\n\
2356 \n");
2357
2358 puts ("\n\
2359 /* `recog' contains a decision tree that recognizes whether the rtx\n\
2360 X0 is a valid instruction.\n\
2361 \n\
2362 recog returns -1 if the rtx is not valid. If the rtx is valid, recog\n\
2363 returns a nonnegative number which is the insn code number for the\n\
2364 pattern that matched. This is the same as the order in the machine\n\
2365 description of the entry that matched. This number can be used as an\n\
2366 index into `insn_data' and other tables.\n");
2367 puts ("\
2368 The third argument to recog is an optional pointer to an int. If\n\
2369 present, recog will accept a pattern if it matches except for missing\n\
2370 CLOBBER expressions at the end. In that case, the value pointed to by\n\
2371 the optional pointer will be set to the number of CLOBBERs that need\n\
2372 to be added (it should be initialized to zero by the caller). If it");
2373 puts ("\
2374 is set nonzero, the caller should allocate a PARALLEL of the\n\
2375 appropriate size, copy the initial entries, and call add_clobbers\n\
2376 (found in insn-emit.c) to fill in the CLOBBERs.\n\
2377 ");
2378
2379 puts ("\n\
2380 The function split_insns returns 0 if the rtl could not\n\
2381 be split or the split rtl as an INSN list if it can be.\n\
2382 \n\
2383 The function peephole2_insns returns 0 if the rtl could not\n\
2384 be matched. If there was a match, the new rtl is returned in an INSN list,\n\
2385 and LAST_INSN will point to the last recognized insn in the old sequence.\n\
2386 */\n\n");
2387 }
2388
2389 \f
2390 /* Construct and return a sequence of decisions
2391 that will recognize INSN.
2392
2393 TYPE says what type of routine we are recognizing (RECOG or SPLIT). */
2394
2395 static struct decision_head
2396 make_insn_sequence (rtx insn, enum routine_type type)
2397 {
2398 rtx x;
2399 const char *c_test = XSTR (insn, type == RECOG ? 2 : 1);
2400 int truth = maybe_eval_c_test (c_test);
2401 struct decision *last;
2402 struct decision_test *test, **place;
2403 struct decision_head head;
2404 struct position *c_test_pos, **pos_ptr;
2405
2406 /* We should never see an insn whose C test is false at compile time. */
2407 gcc_assert (truth);
2408
2409 c_test_pos = &root_pos;
2410 if (type == PEEPHOLE2)
2411 {
2412 int i, j;
2413
2414 /* peephole2 gets special treatment:
2415 - X always gets an outer parallel even if it's only one entry
2416 - we remove all traces of outer-level match_scratch and match_dup
2417 expressions here. */
2418 x = rtx_alloc (PARALLEL);
2419 PUT_MODE (x, VOIDmode);
2420 XVEC (x, 0) = rtvec_alloc (XVECLEN (insn, 0));
2421 pos_ptr = &peep2_insn_pos_list;
2422 for (i = j = 0; i < XVECLEN (insn, 0); i++)
2423 {
2424 rtx tmp = XVECEXP (insn, 0, i);
2425 if (GET_CODE (tmp) != MATCH_SCRATCH && GET_CODE (tmp) != MATCH_DUP)
2426 {
2427 c_test_pos = next_position (pos_ptr, &root_pos,
2428 POS_PEEP2_INSN, j);
2429 XVECEXP (x, 0, j) = tmp;
2430 j++;
2431 pos_ptr = &c_test_pos->next;
2432 }
2433 }
2434 XVECLEN (x, 0) = j;
2435 }
2436 else if (XVECLEN (insn, type == RECOG) == 1)
2437 x = XVECEXP (insn, type == RECOG, 0);
2438 else
2439 {
2440 x = rtx_alloc (PARALLEL);
2441 XVEC (x, 0) = XVEC (insn, type == RECOG);
2442 PUT_MODE (x, VOIDmode);
2443 }
2444
2445 validate_pattern (x, insn, NULL_RTX, 0);
2446
2447 memset (&head, 0, sizeof (head));
2448 last = add_to_sequence (x, &head, &root_pos, type, 1);
2449
2450 /* Find the end of the test chain on the last node. */
2451 for (test = last->tests; test->next; test = test->next)
2452 continue;
2453 place = &test->next;
2454
2455 /* Skip the C test if it's known to be true at compile time. */
2456 if (truth == -1)
2457 {
2458 /* Need a new node if we have another test to add. */
2459 if (test->type == DT_accept_op)
2460 {
2461 last = new_decision (c_test_pos, &last->success);
2462 place = &last->tests;
2463 }
2464 test = new_decision_test (DT_c_test, &place);
2465 test->u.c_test = c_test;
2466 }
2467
2468 test = new_decision_test (DT_accept_insn, &place);
2469 test->u.insn.code_number = next_insn_code;
2470 test->u.insn.lineno = pattern_lineno;
2471 test->u.insn.num_clobbers_to_add = 0;
2472
2473 switch (type)
2474 {
2475 case RECOG:
2476 /* If this is a DEFINE_INSN and X is a PARALLEL, see if it ends
2477 with a group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.
2478 If so, set up to recognize the pattern without these CLOBBERs. */
2479
2480 if (GET_CODE (x) == PARALLEL)
2481 {
2482 int i;
2483
2484 /* Find the last non-clobber in the parallel. */
2485 for (i = XVECLEN (x, 0); i > 0; i--)
2486 {
2487 rtx y = XVECEXP (x, 0, i - 1);
2488 if (GET_CODE (y) != CLOBBER
2489 || (!REG_P (XEXP (y, 0))
2490 && GET_CODE (XEXP (y, 0)) != MATCH_SCRATCH))
2491 break;
2492 }
2493
2494 if (i != XVECLEN (x, 0))
2495 {
2496 rtx new_rtx;
2497 struct decision_head clobber_head;
2498
2499 /* Build a similar insn without the clobbers. */
2500 if (i == 1)
2501 new_rtx = XVECEXP (x, 0, 0);
2502 else
2503 {
2504 int j;
2505
2506 new_rtx = rtx_alloc (PARALLEL);
2507 XVEC (new_rtx, 0) = rtvec_alloc (i);
2508 for (j = i - 1; j >= 0; j--)
2509 XVECEXP (new_rtx, 0, j) = XVECEXP (x, 0, j);
2510 }
2511
2512 /* Recognize it. */
2513 memset (&clobber_head, 0, sizeof (clobber_head));
2514 last = add_to_sequence (new_rtx, &clobber_head, &root_pos,
2515 type, 1);
2516
2517 /* Find the end of the test chain on the last node. */
2518 for (test = last->tests; test->next; test = test->next)
2519 continue;
2520
2521 /* We definitely have a new test to add -- create a new
2522 node if needed. */
2523 place = &test->next;
2524 if (test->type == DT_accept_op)
2525 {
2526 last = new_decision (&root_pos, &last->success);
2527 place = &last->tests;
2528 }
2529
2530 /* Skip the C test if it's known to be true at compile
2531 time. */
2532 if (truth == -1)
2533 {
2534 test = new_decision_test (DT_c_test, &place);
2535 test->u.c_test = c_test;
2536 }
2537
2538 test = new_decision_test (DT_accept_insn, &place);
2539 test->u.insn.code_number = next_insn_code;
2540 test->u.insn.lineno = pattern_lineno;
2541 test->u.insn.num_clobbers_to_add = XVECLEN (x, 0) - i;
2542
2543 merge_trees (&head, &clobber_head);
2544 }
2545 }
2546 break;
2547
2548 case SPLIT:
2549 /* Define the subroutine we will call below and emit in genemit. */
2550 printf ("extern rtx gen_split_%d (rtx_insn *, rtx *);\n", next_insn_code);
2551 break;
2552
2553 case PEEPHOLE2:
2554 /* Define the subroutine we will call below and emit in genemit. */
2555 printf ("extern rtx gen_peephole2_%d (rtx_insn *, rtx *);\n",
2556 next_insn_code);
2557 break;
2558 }
2559
2560 return head;
2561 }
2562
2563 static void
2564 process_tree (struct decision_head *head, enum routine_type subroutine_type)
2565 {
2566 if (head->first == NULL)
2567 {
2568 /* We can elide peephole2_insns, but not recog or split_insns. */
2569 if (subroutine_type == PEEPHOLE2)
2570 return;
2571 }
2572 else
2573 {
2574 factor_tests (head);
2575
2576 next_subroutine_number = 0;
2577 break_out_subroutines (head, 1);
2578 find_afterward (head, NULL);
2579
2580 /* We run this after find_afterward, because find_afterward needs
2581 the redundant DT_mode tests on predicates to determine whether
2582 two tests can both be true or not. */
2583 simplify_tests (head);
2584
2585 write_subroutines (head, subroutine_type);
2586 }
2587
2588 write_subroutine (head, subroutine_type);
2589 }
2590 \f
2591 extern int main (int, char **);
2592
2593 int
2594 main (int argc, char **argv)
2595 {
2596 rtx desc;
2597 struct decision_head recog_tree, split_tree, peephole2_tree, h;
2598
2599 progname = "genrecog";
2600
2601 memset (&recog_tree, 0, sizeof recog_tree);
2602 memset (&split_tree, 0, sizeof split_tree);
2603 memset (&peephole2_tree, 0, sizeof peephole2_tree);
2604
2605 if (!init_rtx_reader_args (argc, argv))
2606 return (FATAL_EXIT_CODE);
2607
2608 next_insn_code = 0;
2609
2610 write_header ();
2611
2612 /* Read the machine description. */
2613
2614 while (1)
2615 {
2616 desc = read_md_rtx (&pattern_lineno, &next_insn_code);
2617 if (desc == NULL)
2618 break;
2619
2620 switch (GET_CODE (desc))
2621 {
2622 case DEFINE_INSN:
2623 h = make_insn_sequence (desc, RECOG);
2624 merge_trees (&recog_tree, &h);
2625 break;
2626
2627 case DEFINE_SPLIT:
2628 h = make_insn_sequence (desc, SPLIT);
2629 merge_trees (&split_tree, &h);
2630 break;
2631
2632 case DEFINE_PEEPHOLE2:
2633 h = make_insn_sequence (desc, PEEPHOLE2);
2634 merge_trees (&peephole2_tree, &h);
2635
2636 default:
2637 /* do nothing */;
2638 }
2639 }
2640
2641 if (have_error)
2642 return FATAL_EXIT_CODE;
2643
2644 puts ("\n\n");
2645
2646 process_tree (&recog_tree, RECOG);
2647 process_tree (&split_tree, SPLIT);
2648 process_tree (&peephole2_tree, PEEPHOLE2);
2649
2650 fflush (stdout);
2651 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
2652 }
2653 \f
2654 static void
2655 debug_decision_2 (struct decision_test *test)
2656 {
2657 switch (test->type)
2658 {
2659 case DT_num_insns:
2660 fprintf (stderr, "num_insns=%d", test->u.num_insns);
2661 break;
2662 case DT_mode:
2663 fprintf (stderr, "mode=%s", GET_MODE_NAME (test->u.mode));
2664 break;
2665 case DT_code:
2666 fprintf (stderr, "code=%s", GET_RTX_NAME (test->u.code));
2667 break;
2668 case DT_veclen:
2669 fprintf (stderr, "veclen=%d", test->u.veclen);
2670 break;
2671 case DT_elt_zero_int:
2672 fprintf (stderr, "elt0_i=%d", (int) test->u.intval);
2673 break;
2674 case DT_elt_one_int:
2675 fprintf (stderr, "elt1_i=%d", (int) test->u.intval);
2676 break;
2677 case DT_elt_zero_wide:
2678 fprintf (stderr, "elt0_w=" HOST_WIDE_INT_PRINT_DEC, test->u.intval);
2679 break;
2680 case DT_elt_zero_wide_safe:
2681 fprintf (stderr, "elt0_ws=" HOST_WIDE_INT_PRINT_DEC, test->u.intval);
2682 break;
2683 case DT_veclen_ge:
2684 fprintf (stderr, "veclen>=%d", test->u.veclen);
2685 break;
2686 case DT_dup:
2687 fprintf (stderr, "dup=%d", test->u.dup);
2688 break;
2689 case DT_pred:
2690 fprintf (stderr, "pred=(%s,%s)",
2691 test->u.pred.name, GET_MODE_NAME (test->u.pred.mode));
2692 break;
2693 case DT_c_test:
2694 {
2695 char sub[16+4];
2696 strncpy (sub, test->u.c_test, sizeof (sub));
2697 memcpy (sub+16, "...", 4);
2698 fprintf (stderr, "c_test=\"%s\"", sub);
2699 }
2700 break;
2701 case DT_accept_op:
2702 fprintf (stderr, "A_op=%d", test->u.opno);
2703 break;
2704 case DT_accept_insn:
2705 fprintf (stderr, "A_insn=(%d,%d)",
2706 test->u.insn.code_number, test->u.insn.num_clobbers_to_add);
2707 break;
2708
2709 default:
2710 gcc_unreachable ();
2711 }
2712 }
2713
2714 static void
2715 debug_decision_1 (struct decision *d, int indent)
2716 {
2717 int i;
2718 struct decision_test *test;
2719
2720 if (d == NULL)
2721 {
2722 for (i = 0; i < indent; ++i)
2723 putc (' ', stderr);
2724 fputs ("(nil)\n", stderr);
2725 return;
2726 }
2727
2728 for (i = 0; i < indent; ++i)
2729 putc (' ', stderr);
2730
2731 putc ('{', stderr);
2732 test = d->tests;
2733 if (test)
2734 {
2735 debug_decision_2 (test);
2736 while ((test = test->next) != NULL)
2737 {
2738 fputs (" + ", stderr);
2739 debug_decision_2 (test);
2740 }
2741 }
2742 fprintf (stderr, "} %d n %d a %d\n", d->number,
2743 (d->next ? d->next->number : -1),
2744 (d->afterward ? d->afterward->number : -1));
2745 }
2746
2747 static void
2748 debug_decision_0 (struct decision *d, int indent, int maxdepth)
2749 {
2750 struct decision *n;
2751 int i;
2752
2753 if (maxdepth < 0)
2754 return;
2755 if (d == NULL)
2756 {
2757 for (i = 0; i < indent; ++i)
2758 putc (' ', stderr);
2759 fputs ("(nil)\n", stderr);
2760 return;
2761 }
2762
2763 debug_decision_1 (d, indent);
2764 for (n = d->success.first; n ; n = n->next)
2765 debug_decision_0 (n, indent + 2, maxdepth - 1);
2766 }
2767
2768 DEBUG_FUNCTION void
2769 debug_decision (struct decision *d)
2770 {
2771 debug_decision_0 (d, 0, 1000000);
2772 }
2773
2774 DEBUG_FUNCTION void
2775 debug_decision_list (struct decision *d)
2776 {
2777 while (d)
2778 {
2779 debug_decision_0 (d, 0, 0);
2780 d = d->next;
2781 }
2782 }