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