re PR c++/36897 (ICE with function pointer template parameter)
[official-gcc.git] / gcc / recog.c
blob540617d5bb44f2abd1ca63931058b898c9a3f445
1 /* Subroutines used by or related to instruction recognition.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
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/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "insn-config.h"
30 #include "insn-attr.h"
31 #include "hard-reg-set.h"
32 #include "recog.h"
33 #include "regs.h"
34 #include "addresses.h"
35 #include "expr.h"
36 #include "function.h"
37 #include "flags.h"
38 #include "real.h"
39 #include "toplev.h"
40 #include "basic-block.h"
41 #include "output.h"
42 #include "reload.h"
43 #include "target.h"
44 #include "timevar.h"
45 #include "tree-pass.h"
46 #include "df.h"
48 #ifndef STACK_PUSH_CODE
49 #ifdef STACK_GROWS_DOWNWARD
50 #define STACK_PUSH_CODE PRE_DEC
51 #else
52 #define STACK_PUSH_CODE PRE_INC
53 #endif
54 #endif
56 #ifndef STACK_POP_CODE
57 #ifdef STACK_GROWS_DOWNWARD
58 #define STACK_POP_CODE POST_INC
59 #else
60 #define STACK_POP_CODE POST_DEC
61 #endif
62 #endif
64 #ifndef HAVE_ATTR_enabled
65 static inline bool
66 get_attr_enabled (rtx insn ATTRIBUTE_UNUSED)
68 return true;
70 #endif
72 static void validate_replace_rtx_1 (rtx *, rtx, rtx, rtx, bool);
73 static void validate_replace_src_1 (rtx *, void *);
74 static rtx split_insn (rtx);
76 /* Nonzero means allow operands to be volatile.
77 This should be 0 if you are generating rtl, such as if you are calling
78 the functions in optabs.c and expmed.c (most of the time).
79 This should be 1 if all valid insns need to be recognized,
80 such as in regclass.c and final.c and reload.c.
82 init_recog and init_recog_no_volatile are responsible for setting this. */
84 int volatile_ok;
86 struct recog_data recog_data;
88 /* Contains a vector of operand_alternative structures for every operand.
89 Set up by preprocess_constraints. */
90 struct operand_alternative recog_op_alt[MAX_RECOG_OPERANDS][MAX_RECOG_ALTERNATIVES];
92 /* On return from `constrain_operands', indicate which alternative
93 was satisfied. */
95 int which_alternative;
97 /* Nonzero after end of reload pass.
98 Set to 1 or 0 by toplev.c.
99 Controls the significance of (SUBREG (MEM)). */
101 int reload_completed;
103 /* Nonzero after thread_prologue_and_epilogue_insns has run. */
104 int epilogue_completed;
106 /* Initialize data used by the function `recog'.
107 This must be called once in the compilation of a function
108 before any insn recognition may be done in the function. */
110 void
111 init_recog_no_volatile (void)
113 volatile_ok = 0;
116 void
117 init_recog (void)
119 volatile_ok = 1;
123 /* Check that X is an insn-body for an `asm' with operands
124 and that the operands mentioned in it are legitimate. */
127 check_asm_operands (rtx x)
129 int noperands;
130 rtx *operands;
131 const char **constraints;
132 int i;
134 /* Post-reload, be more strict with things. */
135 if (reload_completed)
137 /* ??? Doh! We've not got the wrapping insn. Cook one up. */
138 extract_insn (make_insn_raw (x));
139 constrain_operands (1);
140 return which_alternative >= 0;
143 noperands = asm_noperands (x);
144 if (noperands < 0)
145 return 0;
146 if (noperands == 0)
147 return 1;
149 operands = XALLOCAVEC (rtx, noperands);
150 constraints = XALLOCAVEC (const char *, noperands);
152 decode_asm_operands (x, operands, NULL, constraints, NULL, NULL);
154 for (i = 0; i < noperands; i++)
156 const char *c = constraints[i];
157 if (c[0] == '%')
158 c++;
159 if (! asm_operand_ok (operands[i], c, constraints))
160 return 0;
163 return 1;
166 /* Static data for the next two routines. */
168 typedef struct change_t
170 rtx object;
171 int old_code;
172 rtx *loc;
173 rtx old;
174 bool unshare;
175 } change_t;
177 static change_t *changes;
178 static int changes_allocated;
180 static int num_changes = 0;
182 /* Validate a proposed change to OBJECT. LOC is the location in the rtl
183 at which NEW_RTX will be placed. If OBJECT is zero, no validation is done,
184 the change is simply made.
186 Two types of objects are supported: If OBJECT is a MEM, memory_address_p
187 will be called with the address and mode as parameters. If OBJECT is
188 an INSN, CALL_INSN, or JUMP_INSN, the insn will be re-recognized with
189 the change in place.
191 IN_GROUP is nonzero if this is part of a group of changes that must be
192 performed as a group. In that case, the changes will be stored. The
193 function `apply_change_group' will validate and apply the changes.
195 If IN_GROUP is zero, this is a single change. Try to recognize the insn
196 or validate the memory reference with the change applied. If the result
197 is not valid for the machine, suppress the change and return zero.
198 Otherwise, perform the change and return 1. */
200 static bool
201 validate_change_1 (rtx object, rtx *loc, rtx new_rtx, bool in_group, bool unshare)
203 rtx old = *loc;
205 if (old == new_rtx || rtx_equal_p (old, new_rtx))
206 return 1;
208 gcc_assert (in_group != 0 || num_changes == 0);
210 *loc = new_rtx;
212 /* Save the information describing this change. */
213 if (num_changes >= changes_allocated)
215 if (changes_allocated == 0)
216 /* This value allows for repeated substitutions inside complex
217 indexed addresses, or changes in up to 5 insns. */
218 changes_allocated = MAX_RECOG_OPERANDS * 5;
219 else
220 changes_allocated *= 2;
222 changes = XRESIZEVEC (change_t, changes, changes_allocated);
225 changes[num_changes].object = object;
226 changes[num_changes].loc = loc;
227 changes[num_changes].old = old;
228 changes[num_changes].unshare = unshare;
230 if (object && !MEM_P (object))
232 /* Set INSN_CODE to force rerecognition of insn. Save old code in
233 case invalid. */
234 changes[num_changes].old_code = INSN_CODE (object);
235 INSN_CODE (object) = -1;
238 num_changes++;
240 /* If we are making a group of changes, return 1. Otherwise, validate the
241 change group we made. */
243 if (in_group)
244 return 1;
245 else
246 return apply_change_group ();
249 /* Wrapper for validate_change_1 without the UNSHARE argument defaulting
250 UNSHARE to false. */
252 bool
253 validate_change (rtx object, rtx *loc, rtx new_rtx, bool in_group)
255 return validate_change_1 (object, loc, new_rtx, in_group, false);
258 /* Wrapper for validate_change_1 without the UNSHARE argument defaulting
259 UNSHARE to true. */
261 bool
262 validate_unshare_change (rtx object, rtx *loc, rtx new_rtx, bool in_group)
264 return validate_change_1 (object, loc, new_rtx, in_group, true);
268 /* Keep X canonicalized if some changes have made it non-canonical; only
269 modifies the operands of X, not (for example) its code. Simplifications
270 are not the job of this routine.
272 Return true if anything was changed. */
273 bool
274 canonicalize_change_group (rtx insn, rtx x)
276 if (COMMUTATIVE_P (x)
277 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
279 /* Oops, the caller has made X no longer canonical.
280 Let's redo the changes in the correct order. */
281 rtx tem = XEXP (x, 0);
282 validate_change (insn, &XEXP (x, 0), XEXP (x, 1), 1);
283 validate_change (insn, &XEXP (x, 1), tem, 1);
284 return true;
286 else
287 return false;
291 /* This subroutine of apply_change_group verifies whether the changes to INSN
292 were valid; i.e. whether INSN can still be recognized. */
295 insn_invalid_p (rtx insn)
297 rtx pat = PATTERN (insn);
298 int num_clobbers = 0;
299 /* If we are before reload and the pattern is a SET, see if we can add
300 clobbers. */
301 int icode = recog (pat, insn,
302 (GET_CODE (pat) == SET
303 && ! reload_completed && ! reload_in_progress)
304 ? &num_clobbers : 0);
305 int is_asm = icode < 0 && asm_noperands (PATTERN (insn)) >= 0;
308 /* If this is an asm and the operand aren't legal, then fail. Likewise if
309 this is not an asm and the insn wasn't recognized. */
310 if ((is_asm && ! check_asm_operands (PATTERN (insn)))
311 || (!is_asm && icode < 0))
312 return 1;
314 /* If we have to add CLOBBERs, fail if we have to add ones that reference
315 hard registers since our callers can't know if they are live or not.
316 Otherwise, add them. */
317 if (num_clobbers > 0)
319 rtx newpat;
321 if (added_clobbers_hard_reg_p (icode))
322 return 1;
324 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_clobbers + 1));
325 XVECEXP (newpat, 0, 0) = pat;
326 add_clobbers (newpat, icode);
327 PATTERN (insn) = pat = newpat;
330 /* After reload, verify that all constraints are satisfied. */
331 if (reload_completed)
333 extract_insn (insn);
335 if (! constrain_operands (1))
336 return 1;
339 INSN_CODE (insn) = icode;
340 return 0;
343 /* Return number of changes made and not validated yet. */
345 num_changes_pending (void)
347 return num_changes;
350 /* Tentatively apply the changes numbered NUM and up.
351 Return 1 if all changes are valid, zero otherwise. */
354 verify_changes (int num)
356 int i;
357 rtx last_validated = NULL_RTX;
359 /* The changes have been applied and all INSN_CODEs have been reset to force
360 rerecognition.
362 The changes are valid if we aren't given an object, or if we are
363 given a MEM and it still is a valid address, or if this is in insn
364 and it is recognized. In the latter case, if reload has completed,
365 we also require that the operands meet the constraints for
366 the insn. */
368 for (i = num; i < num_changes; i++)
370 rtx object = changes[i].object;
372 /* If there is no object to test or if it is the same as the one we
373 already tested, ignore it. */
374 if (object == 0 || object == last_validated)
375 continue;
377 if (MEM_P (object))
379 if (! memory_address_p (GET_MODE (object), XEXP (object, 0)))
380 break;
382 else if (insn_invalid_p (object))
384 rtx pat = PATTERN (object);
386 /* Perhaps we couldn't recognize the insn because there were
387 extra CLOBBERs at the end. If so, try to re-recognize
388 without the last CLOBBER (later iterations will cause each of
389 them to be eliminated, in turn). But don't do this if we
390 have an ASM_OPERAND. */
391 if (GET_CODE (pat) == PARALLEL
392 && GET_CODE (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1)) == CLOBBER
393 && asm_noperands (PATTERN (object)) < 0)
395 rtx newpat;
397 if (XVECLEN (pat, 0) == 2)
398 newpat = XVECEXP (pat, 0, 0);
399 else
401 int j;
403 newpat
404 = gen_rtx_PARALLEL (VOIDmode,
405 rtvec_alloc (XVECLEN (pat, 0) - 1));
406 for (j = 0; j < XVECLEN (newpat, 0); j++)
407 XVECEXP (newpat, 0, j) = XVECEXP (pat, 0, j);
410 /* Add a new change to this group to replace the pattern
411 with this new pattern. Then consider this change
412 as having succeeded. The change we added will
413 cause the entire call to fail if things remain invalid.
415 Note that this can lose if a later change than the one
416 we are processing specified &XVECEXP (PATTERN (object), 0, X)
417 but this shouldn't occur. */
419 validate_change (object, &PATTERN (object), newpat, 1);
420 continue;
422 else if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
423 /* If this insn is a CLOBBER or USE, it is always valid, but is
424 never recognized. */
425 continue;
426 else
427 break;
429 last_validated = object;
432 return (i == num_changes);
435 /* A group of changes has previously been issued with validate_change
436 and verified with verify_changes. Call df_insn_rescan for each of
437 the insn changed and clear num_changes. */
439 void
440 confirm_change_group (void)
442 int i;
443 rtx last_object = NULL;
445 for (i = 0; i < num_changes; i++)
447 rtx object = changes[i].object;
449 if (changes[i].unshare)
450 *changes[i].loc = copy_rtx (*changes[i].loc);
452 /* Avoid unnecessary rescanning when multiple changes to same instruction
453 are made. */
454 if (object)
456 if (object != last_object && last_object && INSN_P (last_object))
457 df_insn_rescan (last_object);
458 last_object = object;
462 if (last_object && INSN_P (last_object))
463 df_insn_rescan (last_object);
464 num_changes = 0;
467 /* Apply a group of changes previously issued with `validate_change'.
468 If all changes are valid, call confirm_change_group and return 1,
469 otherwise, call cancel_changes and return 0. */
472 apply_change_group (void)
474 if (verify_changes (0))
476 confirm_change_group ();
477 return 1;
479 else
481 cancel_changes (0);
482 return 0;
487 /* Return the number of changes so far in the current group. */
490 num_validated_changes (void)
492 return num_changes;
495 /* Retract the changes numbered NUM and up. */
497 void
498 cancel_changes (int num)
500 int i;
502 /* Back out all the changes. Do this in the opposite order in which
503 they were made. */
504 for (i = num_changes - 1; i >= num; i--)
506 *changes[i].loc = changes[i].old;
507 if (changes[i].object && !MEM_P (changes[i].object))
508 INSN_CODE (changes[i].object) = changes[i].old_code;
510 num_changes = num;
513 /* A subroutine of validate_replace_rtx_1 that tries to simplify the resulting
514 rtx. */
516 static void
517 simplify_while_replacing (rtx *loc, rtx to, rtx object,
518 enum machine_mode op0_mode)
520 rtx x = *loc;
521 enum rtx_code code = GET_CODE (x);
522 rtx new_rtx;
524 if (SWAPPABLE_OPERANDS_P (x)
525 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
527 validate_unshare_change (object, loc,
528 gen_rtx_fmt_ee (COMMUTATIVE_ARITH_P (x) ? code
529 : swap_condition (code),
530 GET_MODE (x), XEXP (x, 1),
531 XEXP (x, 0)), 1);
532 x = *loc;
533 code = GET_CODE (x);
536 switch (code)
538 case PLUS:
539 /* If we have a PLUS whose second operand is now a CONST_INT, use
540 simplify_gen_binary to try to simplify it.
541 ??? We may want later to remove this, once simplification is
542 separated from this function. */
543 if (GET_CODE (XEXP (x, 1)) == CONST_INT && XEXP (x, 1) == to)
544 validate_change (object, loc,
545 simplify_gen_binary
546 (PLUS, GET_MODE (x), XEXP (x, 0), XEXP (x, 1)), 1);
547 break;
548 case MINUS:
549 if (GET_CODE (XEXP (x, 1)) == CONST_INT
550 || GET_CODE (XEXP (x, 1)) == CONST_DOUBLE)
551 validate_change (object, loc,
552 simplify_gen_binary
553 (PLUS, GET_MODE (x), XEXP (x, 0),
554 simplify_gen_unary (NEG,
555 GET_MODE (x), XEXP (x, 1),
556 GET_MODE (x))), 1);
557 break;
558 case ZERO_EXTEND:
559 case SIGN_EXTEND:
560 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
562 new_rtx = simplify_gen_unary (code, GET_MODE (x), XEXP (x, 0),
563 op0_mode);
564 /* If any of the above failed, substitute in something that
565 we know won't be recognized. */
566 if (!new_rtx)
567 new_rtx = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
568 validate_change (object, loc, new_rtx, 1);
570 break;
571 case SUBREG:
572 /* All subregs possible to simplify should be simplified. */
573 new_rtx = simplify_subreg (GET_MODE (x), SUBREG_REG (x), op0_mode,
574 SUBREG_BYTE (x));
576 /* Subregs of VOIDmode operands are incorrect. */
577 if (!new_rtx && GET_MODE (SUBREG_REG (x)) == VOIDmode)
578 new_rtx = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
579 if (new_rtx)
580 validate_change (object, loc, new_rtx, 1);
581 break;
582 case ZERO_EXTRACT:
583 case SIGN_EXTRACT:
584 /* If we are replacing a register with memory, try to change the memory
585 to be the mode required for memory in extract operations (this isn't
586 likely to be an insertion operation; if it was, nothing bad will
587 happen, we might just fail in some cases). */
589 if (MEM_P (XEXP (x, 0))
590 && GET_CODE (XEXP (x, 1)) == CONST_INT
591 && GET_CODE (XEXP (x, 2)) == CONST_INT
592 && !mode_dependent_address_p (XEXP (XEXP (x, 0), 0))
593 && !MEM_VOLATILE_P (XEXP (x, 0)))
595 enum machine_mode wanted_mode = VOIDmode;
596 enum machine_mode is_mode = GET_MODE (XEXP (x, 0));
597 int pos = INTVAL (XEXP (x, 2));
599 if (GET_CODE (x) == ZERO_EXTRACT)
601 enum machine_mode new_mode
602 = mode_for_extraction (EP_extzv, 1);
603 if (new_mode != MAX_MACHINE_MODE)
604 wanted_mode = new_mode;
606 else if (GET_CODE (x) == SIGN_EXTRACT)
608 enum machine_mode new_mode
609 = mode_for_extraction (EP_extv, 1);
610 if (new_mode != MAX_MACHINE_MODE)
611 wanted_mode = new_mode;
614 /* If we have a narrower mode, we can do something. */
615 if (wanted_mode != VOIDmode
616 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
618 int offset = pos / BITS_PER_UNIT;
619 rtx newmem;
621 /* If the bytes and bits are counted differently, we
622 must adjust the offset. */
623 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
624 offset =
625 (GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (wanted_mode) -
626 offset);
628 pos %= GET_MODE_BITSIZE (wanted_mode);
630 newmem = adjust_address_nv (XEXP (x, 0), wanted_mode, offset);
632 validate_change (object, &XEXP (x, 2), GEN_INT (pos), 1);
633 validate_change (object, &XEXP (x, 0), newmem, 1);
637 break;
639 default:
640 break;
644 /* Replace every occurrence of FROM in X with TO. Mark each change with
645 validate_change passing OBJECT. */
647 static void
648 validate_replace_rtx_1 (rtx *loc, rtx from, rtx to, rtx object,
649 bool simplify)
651 int i, j;
652 const char *fmt;
653 rtx x = *loc;
654 enum rtx_code code;
655 enum machine_mode op0_mode = VOIDmode;
656 int prev_changes = num_changes;
658 if (!x)
659 return;
661 code = GET_CODE (x);
662 fmt = GET_RTX_FORMAT (code);
663 if (fmt[0] == 'e')
664 op0_mode = GET_MODE (XEXP (x, 0));
666 /* X matches FROM if it is the same rtx or they are both referring to the
667 same register in the same mode. Avoid calling rtx_equal_p unless the
668 operands look similar. */
670 if (x == from
671 || (REG_P (x) && REG_P (from)
672 && GET_MODE (x) == GET_MODE (from)
673 && REGNO (x) == REGNO (from))
674 || (GET_CODE (x) == GET_CODE (from) && GET_MODE (x) == GET_MODE (from)
675 && rtx_equal_p (x, from)))
677 validate_unshare_change (object, loc, to, 1);
678 return;
681 /* Call ourself recursively to perform the replacements.
682 We must not replace inside already replaced expression, otherwise we
683 get infinite recursion for replacements like (reg X)->(subreg (reg X))
684 done by regmove, so we must special case shared ASM_OPERANDS. */
686 if (GET_CODE (x) == PARALLEL)
688 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
690 if (j && GET_CODE (XVECEXP (x, 0, j)) == SET
691 && GET_CODE (SET_SRC (XVECEXP (x, 0, j))) == ASM_OPERANDS)
693 /* Verify that operands are really shared. */
694 gcc_assert (ASM_OPERANDS_INPUT_VEC (SET_SRC (XVECEXP (x, 0, 0)))
695 == ASM_OPERANDS_INPUT_VEC (SET_SRC (XVECEXP
696 (x, 0, j))));
697 validate_replace_rtx_1 (&SET_DEST (XVECEXP (x, 0, j)),
698 from, to, object, simplify);
700 else
701 validate_replace_rtx_1 (&XVECEXP (x, 0, j), from, to, object,
702 simplify);
705 else
706 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
708 if (fmt[i] == 'e')
709 validate_replace_rtx_1 (&XEXP (x, i), from, to, object, simplify);
710 else if (fmt[i] == 'E')
711 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
712 validate_replace_rtx_1 (&XVECEXP (x, i, j), from, to, object,
713 simplify);
716 /* If we didn't substitute, there is nothing more to do. */
717 if (num_changes == prev_changes)
718 return;
720 /* Allow substituted expression to have different mode. This is used by
721 regmove to change mode of pseudo register. */
722 if (fmt[0] == 'e' && GET_MODE (XEXP (x, 0)) != VOIDmode)
723 op0_mode = GET_MODE (XEXP (x, 0));
725 /* Do changes needed to keep rtx consistent. Don't do any other
726 simplifications, as it is not our job. */
727 if (simplify)
728 simplify_while_replacing (loc, to, object, op0_mode);
731 /* Try replacing every occurrence of FROM in INSN with TO. After all
732 changes have been made, validate by seeing if INSN is still valid. */
735 validate_replace_rtx (rtx from, rtx to, rtx insn)
737 validate_replace_rtx_1 (&PATTERN (insn), from, to, insn, true);
738 return apply_change_group ();
741 /* Try replacing every occurrence of FROM in WHERE with TO. Assume that WHERE
742 is a part of INSN. After all changes have been made, validate by seeing if
743 INSN is still valid.
744 validate_replace_rtx (from, to, insn) is equivalent to
745 validate_replace_rtx_part (from, to, &PATTERN (insn), insn). */
748 validate_replace_rtx_part (rtx from, rtx to, rtx *where, rtx insn)
750 validate_replace_rtx_1 (where, from, to, insn, true);
751 return apply_change_group ();
754 /* Same as above, but do not simplify rtx afterwards. */
755 int
756 validate_replace_rtx_part_nosimplify (rtx from, rtx to, rtx *where,
757 rtx insn)
759 validate_replace_rtx_1 (where, from, to, insn, false);
760 return apply_change_group ();
764 /* Try replacing every occurrence of FROM in INSN with TO. */
766 void
767 validate_replace_rtx_group (rtx from, rtx to, rtx insn)
769 validate_replace_rtx_1 (&PATTERN (insn), from, to, insn, true);
772 /* Function called by note_uses to replace used subexpressions. */
773 struct validate_replace_src_data
775 rtx from; /* Old RTX */
776 rtx to; /* New RTX */
777 rtx insn; /* Insn in which substitution is occurring. */
780 static void
781 validate_replace_src_1 (rtx *x, void *data)
783 struct validate_replace_src_data *d
784 = (struct validate_replace_src_data *) data;
786 validate_replace_rtx_1 (x, d->from, d->to, d->insn, true);
789 /* Try replacing every occurrence of FROM in INSN with TO, avoiding
790 SET_DESTs. */
792 void
793 validate_replace_src_group (rtx from, rtx to, rtx insn)
795 struct validate_replace_src_data d;
797 d.from = from;
798 d.to = to;
799 d.insn = insn;
800 note_uses (&PATTERN (insn), validate_replace_src_1, &d);
803 /* Try simplify INSN.
804 Invoke simplify_rtx () on every SET_SRC and SET_DEST inside the INSN's
805 pattern and return true if something was simplified. */
807 bool
808 validate_simplify_insn (rtx insn)
810 int i;
811 rtx pat = NULL;
812 rtx newpat = NULL;
814 pat = PATTERN (insn);
816 if (GET_CODE (pat) == SET)
818 newpat = simplify_rtx (SET_SRC (pat));
819 if (newpat && !rtx_equal_p (SET_SRC (pat), newpat))
820 validate_change (insn, &SET_SRC (pat), newpat, 1);
821 newpat = simplify_rtx (SET_DEST (pat));
822 if (newpat && !rtx_equal_p (SET_DEST (pat), newpat))
823 validate_change (insn, &SET_DEST (pat), newpat, 1);
825 else if (GET_CODE (pat) == PARALLEL)
826 for (i = 0; i < XVECLEN (pat, 0); i++)
828 rtx s = XVECEXP (pat, 0, i);
830 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
832 newpat = simplify_rtx (SET_SRC (s));
833 if (newpat && !rtx_equal_p (SET_SRC (s), newpat))
834 validate_change (insn, &SET_SRC (s), newpat, 1);
835 newpat = simplify_rtx (SET_DEST (s));
836 if (newpat && !rtx_equal_p (SET_DEST (s), newpat))
837 validate_change (insn, &SET_DEST (s), newpat, 1);
840 return ((num_changes_pending () > 0) && (apply_change_group () > 0));
843 #ifdef HAVE_cc0
844 /* Return 1 if the insn using CC0 set by INSN does not contain
845 any ordered tests applied to the condition codes.
846 EQ and NE tests do not count. */
849 next_insn_tests_no_inequality (rtx insn)
851 rtx next = next_cc0_user (insn);
853 /* If there is no next insn, we have to take the conservative choice. */
854 if (next == 0)
855 return 0;
857 return (INSN_P (next)
858 && ! inequality_comparisons_p (PATTERN (next)));
860 #endif
862 /* Return 1 if OP is a valid general operand for machine mode MODE.
863 This is either a register reference, a memory reference,
864 or a constant. In the case of a memory reference, the address
865 is checked for general validity for the target machine.
867 Register and memory references must have mode MODE in order to be valid,
868 but some constants have no machine mode and are valid for any mode.
870 If MODE is VOIDmode, OP is checked for validity for whatever mode
871 it has.
873 The main use of this function is as a predicate in match_operand
874 expressions in the machine description.
876 For an explanation of this function's behavior for registers of
877 class NO_REGS, see the comment for `register_operand'. */
880 general_operand (rtx op, enum machine_mode mode)
882 enum rtx_code code = GET_CODE (op);
884 if (mode == VOIDmode)
885 mode = GET_MODE (op);
887 /* Don't accept CONST_INT or anything similar
888 if the caller wants something floating. */
889 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
890 && GET_MODE_CLASS (mode) != MODE_INT
891 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
892 return 0;
894 if (GET_CODE (op) == CONST_INT
895 && mode != VOIDmode
896 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
897 return 0;
899 if (CONSTANT_P (op))
900 return ((GET_MODE (op) == VOIDmode || GET_MODE (op) == mode
901 || mode == VOIDmode)
902 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
903 && LEGITIMATE_CONSTANT_P (op));
905 /* Except for certain constants with VOIDmode, already checked for,
906 OP's mode must match MODE if MODE specifies a mode. */
908 if (GET_MODE (op) != mode)
909 return 0;
911 if (code == SUBREG)
913 rtx sub = SUBREG_REG (op);
915 #ifdef INSN_SCHEDULING
916 /* On machines that have insn scheduling, we want all memory
917 reference to be explicit, so outlaw paradoxical SUBREGs.
918 However, we must allow them after reload so that they can
919 get cleaned up by cleanup_subreg_operands. */
920 if (!reload_completed && MEM_P (sub)
921 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (sub)))
922 return 0;
923 #endif
924 /* Avoid memories with nonzero SUBREG_BYTE, as offsetting the memory
925 may result in incorrect reference. We should simplify all valid
926 subregs of MEM anyway. But allow this after reload because we
927 might be called from cleanup_subreg_operands.
929 ??? This is a kludge. */
930 if (!reload_completed && SUBREG_BYTE (op) != 0
931 && MEM_P (sub))
932 return 0;
934 /* FLOAT_MODE subregs can't be paradoxical. Combine will occasionally
935 create such rtl, and we must reject it. */
936 if (SCALAR_FLOAT_MODE_P (GET_MODE (op))
937 && GET_MODE_SIZE (GET_MODE (op)) > GET_MODE_SIZE (GET_MODE (sub)))
938 return 0;
940 op = sub;
941 code = GET_CODE (op);
944 if (code == REG)
945 /* A register whose class is NO_REGS is not a general operand. */
946 return (REGNO (op) >= FIRST_PSEUDO_REGISTER
947 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS);
949 if (code == MEM)
951 rtx y = XEXP (op, 0);
953 if (! volatile_ok && MEM_VOLATILE_P (op))
954 return 0;
956 /* Use the mem's mode, since it will be reloaded thus. */
957 if (memory_address_p (GET_MODE (op), y))
958 return 1;
961 return 0;
964 /* Return 1 if OP is a valid memory address for a memory reference
965 of mode MODE.
967 The main use of this function is as a predicate in match_operand
968 expressions in the machine description. */
971 address_operand (rtx op, enum machine_mode mode)
973 return memory_address_p (mode, op);
976 /* Return 1 if OP is a register reference of mode MODE.
977 If MODE is VOIDmode, accept a register in any mode.
979 The main use of this function is as a predicate in match_operand
980 expressions in the machine description.
982 As a special exception, registers whose class is NO_REGS are
983 not accepted by `register_operand'. The reason for this change
984 is to allow the representation of special architecture artifacts
985 (such as a condition code register) without extending the rtl
986 definitions. Since registers of class NO_REGS cannot be used
987 as registers in any case where register classes are examined,
988 it is most consistent to keep this function from accepting them. */
991 register_operand (rtx op, enum machine_mode mode)
993 if (GET_MODE (op) != mode && mode != VOIDmode)
994 return 0;
996 if (GET_CODE (op) == SUBREG)
998 rtx sub = SUBREG_REG (op);
1000 /* Before reload, we can allow (SUBREG (MEM...)) as a register operand
1001 because it is guaranteed to be reloaded into one.
1002 Just make sure the MEM is valid in itself.
1003 (Ideally, (SUBREG (MEM)...) should not exist after reload,
1004 but currently it does result from (SUBREG (REG)...) where the
1005 reg went on the stack.) */
1006 if (! reload_completed && MEM_P (sub))
1007 return general_operand (op, mode);
1009 #ifdef CANNOT_CHANGE_MODE_CLASS
1010 if (REG_P (sub)
1011 && REGNO (sub) < FIRST_PSEUDO_REGISTER
1012 && REG_CANNOT_CHANGE_MODE_P (REGNO (sub), GET_MODE (sub), mode)
1013 && GET_MODE_CLASS (GET_MODE (sub)) != MODE_COMPLEX_INT
1014 && GET_MODE_CLASS (GET_MODE (sub)) != MODE_COMPLEX_FLOAT)
1015 return 0;
1016 #endif
1018 /* FLOAT_MODE subregs can't be paradoxical. Combine will occasionally
1019 create such rtl, and we must reject it. */
1020 if (SCALAR_FLOAT_MODE_P (GET_MODE (op))
1021 && GET_MODE_SIZE (GET_MODE (op)) > GET_MODE_SIZE (GET_MODE (sub)))
1022 return 0;
1024 op = sub;
1027 /* We don't consider registers whose class is NO_REGS
1028 to be a register operand. */
1029 return (REG_P (op)
1030 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
1031 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
1034 /* Return 1 for a register in Pmode; ignore the tested mode. */
1037 pmode_register_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1039 return register_operand (op, Pmode);
1042 /* Return 1 if OP should match a MATCH_SCRATCH, i.e., if it is a SCRATCH
1043 or a hard register. */
1046 scratch_operand (rtx op, enum machine_mode mode)
1048 if (GET_MODE (op) != mode && mode != VOIDmode)
1049 return 0;
1051 return (GET_CODE (op) == SCRATCH
1052 || (REG_P (op)
1053 && REGNO (op) < FIRST_PSEUDO_REGISTER));
1056 /* Return 1 if OP is a valid immediate operand for mode MODE.
1058 The main use of this function is as a predicate in match_operand
1059 expressions in the machine description. */
1062 immediate_operand (rtx op, enum machine_mode mode)
1064 /* Don't accept CONST_INT or anything similar
1065 if the caller wants something floating. */
1066 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1067 && GET_MODE_CLASS (mode) != MODE_INT
1068 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1069 return 0;
1071 if (GET_CODE (op) == CONST_INT
1072 && mode != VOIDmode
1073 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
1074 return 0;
1076 return (CONSTANT_P (op)
1077 && (GET_MODE (op) == mode || mode == VOIDmode
1078 || GET_MODE (op) == VOIDmode)
1079 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1080 && LEGITIMATE_CONSTANT_P (op));
1083 /* Returns 1 if OP is an operand that is a CONST_INT. */
1086 const_int_operand (rtx op, enum machine_mode mode)
1088 if (GET_CODE (op) != CONST_INT)
1089 return 0;
1091 if (mode != VOIDmode
1092 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
1093 return 0;
1095 return 1;
1098 /* Returns 1 if OP is an operand that is a constant integer or constant
1099 floating-point number. */
1102 const_double_operand (rtx op, enum machine_mode mode)
1104 /* Don't accept CONST_INT or anything similar
1105 if the caller wants something floating. */
1106 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1107 && GET_MODE_CLASS (mode) != MODE_INT
1108 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1109 return 0;
1111 return ((GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT)
1112 && (mode == VOIDmode || GET_MODE (op) == mode
1113 || GET_MODE (op) == VOIDmode));
1116 /* Return 1 if OP is a general operand that is not an immediate operand. */
1119 nonimmediate_operand (rtx op, enum machine_mode mode)
1121 return (general_operand (op, mode) && ! CONSTANT_P (op));
1124 /* Return 1 if OP is a register reference or immediate value of mode MODE. */
1127 nonmemory_operand (rtx op, enum machine_mode mode)
1129 if (CONSTANT_P (op))
1131 /* Don't accept CONST_INT or anything similar
1132 if the caller wants something floating. */
1133 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1134 && GET_MODE_CLASS (mode) != MODE_INT
1135 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1136 return 0;
1138 if (GET_CODE (op) == CONST_INT
1139 && mode != VOIDmode
1140 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
1141 return 0;
1143 return ((GET_MODE (op) == VOIDmode || GET_MODE (op) == mode
1144 || mode == VOIDmode)
1145 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1146 && LEGITIMATE_CONSTANT_P (op));
1149 if (GET_MODE (op) != mode && mode != VOIDmode)
1150 return 0;
1152 if (GET_CODE (op) == SUBREG)
1154 /* Before reload, we can allow (SUBREG (MEM...)) as a register operand
1155 because it is guaranteed to be reloaded into one.
1156 Just make sure the MEM is valid in itself.
1157 (Ideally, (SUBREG (MEM)...) should not exist after reload,
1158 but currently it does result from (SUBREG (REG)...) where the
1159 reg went on the stack.) */
1160 if (! reload_completed && MEM_P (SUBREG_REG (op)))
1161 return general_operand (op, mode);
1162 op = SUBREG_REG (op);
1165 /* We don't consider registers whose class is NO_REGS
1166 to be a register operand. */
1167 return (REG_P (op)
1168 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
1169 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
1172 /* Return 1 if OP is a valid operand that stands for pushing a
1173 value of mode MODE onto the stack.
1175 The main use of this function is as a predicate in match_operand
1176 expressions in the machine description. */
1179 push_operand (rtx op, enum machine_mode mode)
1181 unsigned int rounded_size = GET_MODE_SIZE (mode);
1183 #ifdef PUSH_ROUNDING
1184 rounded_size = PUSH_ROUNDING (rounded_size);
1185 #endif
1187 if (!MEM_P (op))
1188 return 0;
1190 if (mode != VOIDmode && GET_MODE (op) != mode)
1191 return 0;
1193 op = XEXP (op, 0);
1195 if (rounded_size == GET_MODE_SIZE (mode))
1197 if (GET_CODE (op) != STACK_PUSH_CODE)
1198 return 0;
1200 else
1202 if (GET_CODE (op) != PRE_MODIFY
1203 || GET_CODE (XEXP (op, 1)) != PLUS
1204 || XEXP (XEXP (op, 1), 0) != XEXP (op, 0)
1205 || GET_CODE (XEXP (XEXP (op, 1), 1)) != CONST_INT
1206 #ifdef STACK_GROWS_DOWNWARD
1207 || INTVAL (XEXP (XEXP (op, 1), 1)) != - (int) rounded_size
1208 #else
1209 || INTVAL (XEXP (XEXP (op, 1), 1)) != (int) rounded_size
1210 #endif
1212 return 0;
1215 return XEXP (op, 0) == stack_pointer_rtx;
1218 /* Return 1 if OP is a valid operand that stands for popping a
1219 value of mode MODE off the stack.
1221 The main use of this function is as a predicate in match_operand
1222 expressions in the machine description. */
1225 pop_operand (rtx op, enum machine_mode mode)
1227 if (!MEM_P (op))
1228 return 0;
1230 if (mode != VOIDmode && GET_MODE (op) != mode)
1231 return 0;
1233 op = XEXP (op, 0);
1235 if (GET_CODE (op) != STACK_POP_CODE)
1236 return 0;
1238 return XEXP (op, 0) == stack_pointer_rtx;
1241 /* Return 1 if ADDR is a valid memory address for mode MODE. */
1244 memory_address_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx addr)
1246 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
1247 return 0;
1249 win:
1250 return 1;
1253 /* Return 1 if OP is a valid memory reference with mode MODE,
1254 including a valid address.
1256 The main use of this function is as a predicate in match_operand
1257 expressions in the machine description. */
1260 memory_operand (rtx op, enum machine_mode mode)
1262 rtx inner;
1264 if (! reload_completed)
1265 /* Note that no SUBREG is a memory operand before end of reload pass,
1266 because (SUBREG (MEM...)) forces reloading into a register. */
1267 return MEM_P (op) && general_operand (op, mode);
1269 if (mode != VOIDmode && GET_MODE (op) != mode)
1270 return 0;
1272 inner = op;
1273 if (GET_CODE (inner) == SUBREG)
1274 inner = SUBREG_REG (inner);
1276 return (MEM_P (inner) && general_operand (op, mode));
1279 /* Return 1 if OP is a valid indirect memory reference with mode MODE;
1280 that is, a memory reference whose address is a general_operand. */
1283 indirect_operand (rtx op, enum machine_mode mode)
1285 /* Before reload, a SUBREG isn't in memory (see memory_operand, above). */
1286 if (! reload_completed
1287 && GET_CODE (op) == SUBREG && MEM_P (SUBREG_REG (op)))
1289 int offset = SUBREG_BYTE (op);
1290 rtx inner = SUBREG_REG (op);
1292 if (mode != VOIDmode && GET_MODE (op) != mode)
1293 return 0;
1295 /* The only way that we can have a general_operand as the resulting
1296 address is if OFFSET is zero and the address already is an operand
1297 or if the address is (plus Y (const_int -OFFSET)) and Y is an
1298 operand. */
1300 return ((offset == 0 && general_operand (XEXP (inner, 0), Pmode))
1301 || (GET_CODE (XEXP (inner, 0)) == PLUS
1302 && GET_CODE (XEXP (XEXP (inner, 0), 1)) == CONST_INT
1303 && INTVAL (XEXP (XEXP (inner, 0), 1)) == -offset
1304 && general_operand (XEXP (XEXP (inner, 0), 0), Pmode)));
1307 return (MEM_P (op)
1308 && memory_operand (op, mode)
1309 && general_operand (XEXP (op, 0), Pmode));
1312 /* Return 1 if this is a comparison operator. This allows the use of
1313 MATCH_OPERATOR to recognize all the branch insns. */
1316 comparison_operator (rtx op, enum machine_mode mode)
1318 return ((mode == VOIDmode || GET_MODE (op) == mode)
1319 && COMPARISON_P (op));
1322 /* If BODY is an insn body that uses ASM_OPERANDS,
1323 return the number of operands (both input and output) in the insn.
1324 Otherwise return -1. */
1327 asm_noperands (const_rtx body)
1329 switch (GET_CODE (body))
1331 case ASM_OPERANDS:
1332 /* No output operands: return number of input operands. */
1333 return ASM_OPERANDS_INPUT_LENGTH (body);
1334 case SET:
1335 if (GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
1336 /* Single output operand: BODY is (set OUTPUT (asm_operands ...)). */
1337 return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (body)) + 1;
1338 else
1339 return -1;
1340 case PARALLEL:
1341 if (GET_CODE (XVECEXP (body, 0, 0)) == SET
1342 && GET_CODE (SET_SRC (XVECEXP (body, 0, 0))) == ASM_OPERANDS)
1344 /* Multiple output operands, or 1 output plus some clobbers:
1345 body is [(set OUTPUT (asm_operands ...))... (clobber (reg ...))...]. */
1346 int i;
1347 int n_sets;
1349 /* Count backwards through CLOBBERs to determine number of SETs. */
1350 for (i = XVECLEN (body, 0); i > 0; i--)
1352 if (GET_CODE (XVECEXP (body, 0, i - 1)) == SET)
1353 break;
1354 if (GET_CODE (XVECEXP (body, 0, i - 1)) != CLOBBER)
1355 return -1;
1358 /* N_SETS is now number of output operands. */
1359 n_sets = i;
1361 /* Verify that all the SETs we have
1362 came from a single original asm_operands insn
1363 (so that invalid combinations are blocked). */
1364 for (i = 0; i < n_sets; i++)
1366 rtx elt = XVECEXP (body, 0, i);
1367 if (GET_CODE (elt) != SET)
1368 return -1;
1369 if (GET_CODE (SET_SRC (elt)) != ASM_OPERANDS)
1370 return -1;
1371 /* If these ASM_OPERANDS rtx's came from different original insns
1372 then they aren't allowed together. */
1373 if (ASM_OPERANDS_INPUT_VEC (SET_SRC (elt))
1374 != ASM_OPERANDS_INPUT_VEC (SET_SRC (XVECEXP (body, 0, 0))))
1375 return -1;
1377 return (ASM_OPERANDS_INPUT_LENGTH (SET_SRC (XVECEXP (body, 0, 0)))
1378 + n_sets);
1380 else if (GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
1382 /* 0 outputs, but some clobbers:
1383 body is [(asm_operands ...) (clobber (reg ...))...]. */
1384 int i;
1386 /* Make sure all the other parallel things really are clobbers. */
1387 for (i = XVECLEN (body, 0) - 1; i > 0; i--)
1388 if (GET_CODE (XVECEXP (body, 0, i)) != CLOBBER)
1389 return -1;
1391 return ASM_OPERANDS_INPUT_LENGTH (XVECEXP (body, 0, 0));
1393 else
1394 return -1;
1395 default:
1396 return -1;
1400 /* Assuming BODY is an insn body that uses ASM_OPERANDS,
1401 copy its operands (both input and output) into the vector OPERANDS,
1402 the locations of the operands within the insn into the vector OPERAND_LOCS,
1403 and the constraints for the operands into CONSTRAINTS.
1404 Write the modes of the operands into MODES.
1405 Return the assembler-template.
1407 If MODES, OPERAND_LOCS, CONSTRAINTS or OPERANDS is 0,
1408 we don't store that info. */
1410 const char *
1411 decode_asm_operands (rtx body, rtx *operands, rtx **operand_locs,
1412 const char **constraints, enum machine_mode *modes,
1413 location_t *loc)
1415 int i;
1416 int noperands;
1417 rtx asmop = 0;
1419 if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
1421 asmop = SET_SRC (body);
1422 /* Single output operand: BODY is (set OUTPUT (asm_operands ....)). */
1424 noperands = ASM_OPERANDS_INPUT_LENGTH (asmop) + 1;
1426 for (i = 1; i < noperands; i++)
1428 if (operand_locs)
1429 operand_locs[i] = &ASM_OPERANDS_INPUT (asmop, i - 1);
1430 if (operands)
1431 operands[i] = ASM_OPERANDS_INPUT (asmop, i - 1);
1432 if (constraints)
1433 constraints[i] = ASM_OPERANDS_INPUT_CONSTRAINT (asmop, i - 1);
1434 if (modes)
1435 modes[i] = ASM_OPERANDS_INPUT_MODE (asmop, i - 1);
1438 /* The output is in the SET.
1439 Its constraint is in the ASM_OPERANDS itself. */
1440 if (operands)
1441 operands[0] = SET_DEST (body);
1442 if (operand_locs)
1443 operand_locs[0] = &SET_DEST (body);
1444 if (constraints)
1445 constraints[0] = ASM_OPERANDS_OUTPUT_CONSTRAINT (asmop);
1446 if (modes)
1447 modes[0] = GET_MODE (SET_DEST (body));
1449 else if (GET_CODE (body) == ASM_OPERANDS)
1451 asmop = body;
1452 /* No output operands: BODY is (asm_operands ....). */
1454 noperands = ASM_OPERANDS_INPUT_LENGTH (asmop);
1456 /* The input operands are found in the 1st element vector. */
1457 /* Constraints for inputs are in the 2nd element vector. */
1458 for (i = 0; i < noperands; i++)
1460 if (operand_locs)
1461 operand_locs[i] = &ASM_OPERANDS_INPUT (asmop, i);
1462 if (operands)
1463 operands[i] = ASM_OPERANDS_INPUT (asmop, i);
1464 if (constraints)
1465 constraints[i] = ASM_OPERANDS_INPUT_CONSTRAINT (asmop, i);
1466 if (modes)
1467 modes[i] = ASM_OPERANDS_INPUT_MODE (asmop, i);
1470 else if (GET_CODE (body) == PARALLEL
1471 && GET_CODE (XVECEXP (body, 0, 0)) == SET
1472 && GET_CODE (SET_SRC (XVECEXP (body, 0, 0))) == ASM_OPERANDS)
1474 int nparallel = XVECLEN (body, 0); /* Includes CLOBBERs. */
1475 int nin;
1476 int nout = 0; /* Does not include CLOBBERs. */
1478 asmop = SET_SRC (XVECEXP (body, 0, 0));
1479 nin = ASM_OPERANDS_INPUT_LENGTH (asmop);
1481 /* At least one output, plus some CLOBBERs. */
1483 /* The outputs are in the SETs.
1484 Their constraints are in the ASM_OPERANDS itself. */
1485 for (i = 0; i < nparallel; i++)
1487 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
1488 break; /* Past last SET */
1490 if (operands)
1491 operands[i] = SET_DEST (XVECEXP (body, 0, i));
1492 if (operand_locs)
1493 operand_locs[i] = &SET_DEST (XVECEXP (body, 0, i));
1494 if (constraints)
1495 constraints[i] = XSTR (SET_SRC (XVECEXP (body, 0, i)), 1);
1496 if (modes)
1497 modes[i] = GET_MODE (SET_DEST (XVECEXP (body, 0, i)));
1498 nout++;
1501 for (i = 0; i < nin; i++)
1503 if (operand_locs)
1504 operand_locs[i + nout] = &ASM_OPERANDS_INPUT (asmop, i);
1505 if (operands)
1506 operands[i + nout] = ASM_OPERANDS_INPUT (asmop, i);
1507 if (constraints)
1508 constraints[i + nout] = ASM_OPERANDS_INPUT_CONSTRAINT (asmop, i);
1509 if (modes)
1510 modes[i + nout] = ASM_OPERANDS_INPUT_MODE (asmop, i);
1513 else if (GET_CODE (body) == PARALLEL
1514 && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
1516 /* No outputs, but some CLOBBERs. */
1518 int nin;
1520 asmop = XVECEXP (body, 0, 0);
1521 nin = ASM_OPERANDS_INPUT_LENGTH (asmop);
1523 for (i = 0; i < nin; i++)
1525 if (operand_locs)
1526 operand_locs[i] = &ASM_OPERANDS_INPUT (asmop, i);
1527 if (operands)
1528 operands[i] = ASM_OPERANDS_INPUT (asmop, i);
1529 if (constraints)
1530 constraints[i] = ASM_OPERANDS_INPUT_CONSTRAINT (asmop, i);
1531 if (modes)
1532 modes[i] = ASM_OPERANDS_INPUT_MODE (asmop, i);
1537 if (loc)
1538 *loc = ASM_OPERANDS_SOURCE_LOCATION (asmop);
1540 return ASM_OPERANDS_TEMPLATE (asmop);
1543 /* Check if an asm_operand matches its constraints.
1544 Return > 0 if ok, = 0 if bad, < 0 if inconclusive. */
1547 asm_operand_ok (rtx op, const char *constraint, const char **constraints)
1549 int result = 0;
1551 /* Use constrain_operands after reload. */
1552 gcc_assert (!reload_completed);
1554 while (*constraint)
1556 char c = *constraint;
1557 int len;
1558 switch (c)
1560 case ',':
1561 constraint++;
1562 continue;
1563 case '=':
1564 case '+':
1565 case '*':
1566 case '%':
1567 case '!':
1568 case '#':
1569 case '&':
1570 case '?':
1571 break;
1573 case '0': case '1': case '2': case '3': case '4':
1574 case '5': case '6': case '7': case '8': case '9':
1575 /* If caller provided constraints pointer, look up
1576 the maching constraint. Otherwise, our caller should have
1577 given us the proper matching constraint, but we can't
1578 actually fail the check if they didn't. Indicate that
1579 results are inconclusive. */
1580 if (constraints)
1582 char *end;
1583 unsigned long match;
1585 match = strtoul (constraint, &end, 10);
1586 if (!result)
1587 result = asm_operand_ok (op, constraints[match], NULL);
1588 constraint = (const char *) end;
1590 else
1593 constraint++;
1594 while (ISDIGIT (*constraint));
1595 if (! result)
1596 result = -1;
1598 continue;
1600 case 'p':
1601 if (address_operand (op, VOIDmode))
1602 result = 1;
1603 break;
1605 case TARGET_MEM_CONSTRAINT:
1606 case 'V': /* non-offsettable */
1607 if (memory_operand (op, VOIDmode))
1608 result = 1;
1609 break;
1611 case 'o': /* offsettable */
1612 if (offsettable_nonstrict_memref_p (op))
1613 result = 1;
1614 break;
1616 case '<':
1617 /* ??? Before auto-inc-dec, auto inc/dec insns are not supposed to exist,
1618 excepting those that expand_call created. Further, on some
1619 machines which do not have generalized auto inc/dec, an inc/dec
1620 is not a memory_operand.
1622 Match any memory and hope things are resolved after reload. */
1624 if (MEM_P (op)
1625 && (1
1626 || GET_CODE (XEXP (op, 0)) == PRE_DEC
1627 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1628 result = 1;
1629 break;
1631 case '>':
1632 if (MEM_P (op)
1633 && (1
1634 || GET_CODE (XEXP (op, 0)) == PRE_INC
1635 || GET_CODE (XEXP (op, 0)) == POST_INC))
1636 result = 1;
1637 break;
1639 case 'E':
1640 case 'F':
1641 if (GET_CODE (op) == CONST_DOUBLE
1642 || (GET_CODE (op) == CONST_VECTOR
1643 && GET_MODE_CLASS (GET_MODE (op)) == MODE_VECTOR_FLOAT))
1644 result = 1;
1645 break;
1647 case 'G':
1648 if (GET_CODE (op) == CONST_DOUBLE
1649 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, 'G', constraint))
1650 result = 1;
1651 break;
1652 case 'H':
1653 if (GET_CODE (op) == CONST_DOUBLE
1654 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, 'H', constraint))
1655 result = 1;
1656 break;
1658 case 's':
1659 if (GET_CODE (op) == CONST_INT
1660 || (GET_CODE (op) == CONST_DOUBLE
1661 && GET_MODE (op) == VOIDmode))
1662 break;
1663 /* Fall through. */
1665 case 'i':
1666 if (CONSTANT_P (op) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op)))
1667 result = 1;
1668 break;
1670 case 'n':
1671 if (GET_CODE (op) == CONST_INT
1672 || (GET_CODE (op) == CONST_DOUBLE
1673 && GET_MODE (op) == VOIDmode))
1674 result = 1;
1675 break;
1677 case 'I':
1678 if (GET_CODE (op) == CONST_INT
1679 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), 'I', constraint))
1680 result = 1;
1681 break;
1682 case 'J':
1683 if (GET_CODE (op) == CONST_INT
1684 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), 'J', constraint))
1685 result = 1;
1686 break;
1687 case 'K':
1688 if (GET_CODE (op) == CONST_INT
1689 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), 'K', constraint))
1690 result = 1;
1691 break;
1692 case 'L':
1693 if (GET_CODE (op) == CONST_INT
1694 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), 'L', constraint))
1695 result = 1;
1696 break;
1697 case 'M':
1698 if (GET_CODE (op) == CONST_INT
1699 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), 'M', constraint))
1700 result = 1;
1701 break;
1702 case 'N':
1703 if (GET_CODE (op) == CONST_INT
1704 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), 'N', constraint))
1705 result = 1;
1706 break;
1707 case 'O':
1708 if (GET_CODE (op) == CONST_INT
1709 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), 'O', constraint))
1710 result = 1;
1711 break;
1712 case 'P':
1713 if (GET_CODE (op) == CONST_INT
1714 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), 'P', constraint))
1715 result = 1;
1716 break;
1718 case 'X':
1719 result = 1;
1720 break;
1722 case 'g':
1723 if (general_operand (op, VOIDmode))
1724 result = 1;
1725 break;
1727 default:
1728 /* For all other letters, we first check for a register class,
1729 otherwise it is an EXTRA_CONSTRAINT. */
1730 if (REG_CLASS_FROM_CONSTRAINT (c, constraint) != NO_REGS)
1732 case 'r':
1733 if (GET_MODE (op) == BLKmode)
1734 break;
1735 if (register_operand (op, VOIDmode))
1736 result = 1;
1738 #ifdef EXTRA_CONSTRAINT_STR
1739 else if (EXTRA_MEMORY_CONSTRAINT (c, constraint))
1740 /* Every memory operand can be reloaded to fit. */
1741 result = result || memory_operand (op, VOIDmode);
1742 else if (EXTRA_ADDRESS_CONSTRAINT (c, constraint))
1743 /* Every address operand can be reloaded to fit. */
1744 result = result || address_operand (op, VOIDmode);
1745 else if (EXTRA_CONSTRAINT_STR (op, c, constraint))
1746 result = 1;
1747 #endif
1748 break;
1750 len = CONSTRAINT_LEN (c, constraint);
1752 constraint++;
1753 while (--len && *constraint);
1754 if (len)
1755 return 0;
1758 return result;
1761 /* Given an rtx *P, if it is a sum containing an integer constant term,
1762 return the location (type rtx *) of the pointer to that constant term.
1763 Otherwise, return a null pointer. */
1765 rtx *
1766 find_constant_term_loc (rtx *p)
1768 rtx *tem;
1769 enum rtx_code code = GET_CODE (*p);
1771 /* If *P IS such a constant term, P is its location. */
1773 if (code == CONST_INT || code == SYMBOL_REF || code == LABEL_REF
1774 || code == CONST)
1775 return p;
1777 /* Otherwise, if not a sum, it has no constant term. */
1779 if (GET_CODE (*p) != PLUS)
1780 return 0;
1782 /* If one of the summands is constant, return its location. */
1784 if (XEXP (*p, 0) && CONSTANT_P (XEXP (*p, 0))
1785 && XEXP (*p, 1) && CONSTANT_P (XEXP (*p, 1)))
1786 return p;
1788 /* Otherwise, check each summand for containing a constant term. */
1790 if (XEXP (*p, 0) != 0)
1792 tem = find_constant_term_loc (&XEXP (*p, 0));
1793 if (tem != 0)
1794 return tem;
1797 if (XEXP (*p, 1) != 0)
1799 tem = find_constant_term_loc (&XEXP (*p, 1));
1800 if (tem != 0)
1801 return tem;
1804 return 0;
1807 /* Return 1 if OP is a memory reference
1808 whose address contains no side effects
1809 and remains valid after the addition
1810 of a positive integer less than the
1811 size of the object being referenced.
1813 We assume that the original address is valid and do not check it.
1815 This uses strict_memory_address_p as a subroutine, so
1816 don't use it before reload. */
1819 offsettable_memref_p (rtx op)
1821 return ((MEM_P (op))
1822 && offsettable_address_p (1, GET_MODE (op), XEXP (op, 0)));
1825 /* Similar, but don't require a strictly valid mem ref:
1826 consider pseudo-regs valid as index or base regs. */
1829 offsettable_nonstrict_memref_p (rtx op)
1831 return ((MEM_P (op))
1832 && offsettable_address_p (0, GET_MODE (op), XEXP (op, 0)));
1835 /* Return 1 if Y is a memory address which contains no side effects
1836 and would remain valid after the addition of a positive integer
1837 less than the size of that mode.
1839 We assume that the original address is valid and do not check it.
1840 We do check that it is valid for narrower modes.
1842 If STRICTP is nonzero, we require a strictly valid address,
1843 for the sake of use in reload.c. */
1846 offsettable_address_p (int strictp, enum machine_mode mode, rtx y)
1848 enum rtx_code ycode = GET_CODE (y);
1849 rtx z;
1850 rtx y1 = y;
1851 rtx *y2;
1852 int (*addressp) (enum machine_mode, rtx) =
1853 (strictp ? strict_memory_address_p : memory_address_p);
1854 unsigned int mode_sz = GET_MODE_SIZE (mode);
1856 if (CONSTANT_ADDRESS_P (y))
1857 return 1;
1859 /* Adjusting an offsettable address involves changing to a narrower mode.
1860 Make sure that's OK. */
1862 if (mode_dependent_address_p (y))
1863 return 0;
1865 /* ??? How much offset does an offsettable BLKmode reference need?
1866 Clearly that depends on the situation in which it's being used.
1867 However, the current situation in which we test 0xffffffff is
1868 less than ideal. Caveat user. */
1869 if (mode_sz == 0)
1870 mode_sz = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
1872 /* If the expression contains a constant term,
1873 see if it remains valid when max possible offset is added. */
1875 if ((ycode == PLUS) && (y2 = find_constant_term_loc (&y1)))
1877 int good;
1879 y1 = *y2;
1880 *y2 = plus_constant (*y2, mode_sz - 1);
1881 /* Use QImode because an odd displacement may be automatically invalid
1882 for any wider mode. But it should be valid for a single byte. */
1883 good = (*addressp) (QImode, y);
1885 /* In any case, restore old contents of memory. */
1886 *y2 = y1;
1887 return good;
1890 if (GET_RTX_CLASS (ycode) == RTX_AUTOINC)
1891 return 0;
1893 /* The offset added here is chosen as the maximum offset that
1894 any instruction could need to add when operating on something
1895 of the specified mode. We assume that if Y and Y+c are
1896 valid addresses then so is Y+d for all 0<d<c. adjust_address will
1897 go inside a LO_SUM here, so we do so as well. */
1898 if (GET_CODE (y) == LO_SUM
1899 && mode != BLKmode
1900 && mode_sz <= GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT)
1901 z = gen_rtx_LO_SUM (GET_MODE (y), XEXP (y, 0),
1902 plus_constant (XEXP (y, 1), mode_sz - 1));
1903 else
1904 z = plus_constant (y, mode_sz - 1);
1906 /* Use QImode because an odd displacement may be automatically invalid
1907 for any wider mode. But it should be valid for a single byte. */
1908 return (*addressp) (QImode, z);
1911 /* Return 1 if ADDR is an address-expression whose effect depends
1912 on the mode of the memory reference it is used in.
1914 Autoincrement addressing is a typical example of mode-dependence
1915 because the amount of the increment depends on the mode. */
1918 mode_dependent_address_p (rtx addr)
1920 /* Auto-increment addressing with anything other than post_modify
1921 or pre_modify always introduces a mode dependency. Catch such
1922 cases now instead of deferring to the target. */
1923 if (GET_CODE (addr) == PRE_INC
1924 || GET_CODE (addr) == POST_INC
1925 || GET_CODE (addr) == PRE_DEC
1926 || GET_CODE (addr) == POST_DEC)
1927 return 1;
1929 GO_IF_MODE_DEPENDENT_ADDRESS (addr, win);
1930 return 0;
1931 /* Label `win' might (not) be used via GO_IF_MODE_DEPENDENT_ADDRESS. */
1932 win: ATTRIBUTE_UNUSED_LABEL
1933 return 1;
1936 /* Like extract_insn, but save insn extracted and don't extract again, when
1937 called again for the same insn expecting that recog_data still contain the
1938 valid information. This is used primary by gen_attr infrastructure that
1939 often does extract insn again and again. */
1940 void
1941 extract_insn_cached (rtx insn)
1943 if (recog_data.insn == insn && INSN_CODE (insn) >= 0)
1944 return;
1945 extract_insn (insn);
1946 recog_data.insn = insn;
1949 /* Do cached extract_insn, constrain_operands and complain about failures.
1950 Used by insn_attrtab. */
1951 void
1952 extract_constrain_insn_cached (rtx insn)
1954 extract_insn_cached (insn);
1955 if (which_alternative == -1
1956 && !constrain_operands (reload_completed))
1957 fatal_insn_not_found (insn);
1960 /* Do cached constrain_operands and complain about failures. */
1962 constrain_operands_cached (int strict)
1964 if (which_alternative == -1)
1965 return constrain_operands (strict);
1966 else
1967 return 1;
1970 /* Analyze INSN and fill in recog_data. */
1972 void
1973 extract_insn (rtx insn)
1975 int i;
1976 int icode;
1977 int noperands;
1978 rtx body = PATTERN (insn);
1980 recog_data.n_operands = 0;
1981 recog_data.n_alternatives = 0;
1982 recog_data.n_dups = 0;
1984 switch (GET_CODE (body))
1986 case USE:
1987 case CLOBBER:
1988 case ASM_INPUT:
1989 case ADDR_VEC:
1990 case ADDR_DIFF_VEC:
1991 return;
1993 case SET:
1994 if (GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
1995 goto asm_insn;
1996 else
1997 goto normal_insn;
1998 case PARALLEL:
1999 if ((GET_CODE (XVECEXP (body, 0, 0)) == SET
2000 && GET_CODE (SET_SRC (XVECEXP (body, 0, 0))) == ASM_OPERANDS)
2001 || GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
2002 goto asm_insn;
2003 else
2004 goto normal_insn;
2005 case ASM_OPERANDS:
2006 asm_insn:
2007 recog_data.n_operands = noperands = asm_noperands (body);
2008 if (noperands >= 0)
2010 /* This insn is an `asm' with operands. */
2012 /* expand_asm_operands makes sure there aren't too many operands. */
2013 gcc_assert (noperands <= MAX_RECOG_OPERANDS);
2015 /* Now get the operand values and constraints out of the insn. */
2016 decode_asm_operands (body, recog_data.operand,
2017 recog_data.operand_loc,
2018 recog_data.constraints,
2019 recog_data.operand_mode, NULL);
2020 if (noperands > 0)
2022 const char *p = recog_data.constraints[0];
2023 recog_data.n_alternatives = 1;
2024 while (*p)
2025 recog_data.n_alternatives += (*p++ == ',');
2027 break;
2029 fatal_insn_not_found (insn);
2031 default:
2032 normal_insn:
2033 /* Ordinary insn: recognize it, get the operands via insn_extract
2034 and get the constraints. */
2036 icode = recog_memoized (insn);
2037 if (icode < 0)
2038 fatal_insn_not_found (insn);
2040 recog_data.n_operands = noperands = insn_data[icode].n_operands;
2041 recog_data.n_alternatives = insn_data[icode].n_alternatives;
2042 recog_data.n_dups = insn_data[icode].n_dups;
2044 insn_extract (insn);
2046 for (i = 0; i < noperands; i++)
2048 recog_data.constraints[i] = insn_data[icode].operand[i].constraint;
2049 recog_data.operand_mode[i] = insn_data[icode].operand[i].mode;
2050 /* VOIDmode match_operands gets mode from their real operand. */
2051 if (recog_data.operand_mode[i] == VOIDmode)
2052 recog_data.operand_mode[i] = GET_MODE (recog_data.operand[i]);
2055 for (i = 0; i < noperands; i++)
2056 recog_data.operand_type[i]
2057 = (recog_data.constraints[i][0] == '=' ? OP_OUT
2058 : recog_data.constraints[i][0] == '+' ? OP_INOUT
2059 : OP_IN);
2061 gcc_assert (recog_data.n_alternatives <= MAX_RECOG_ALTERNATIVES);
2063 if (INSN_CODE (insn) < 0)
2064 for (i = 0; i < recog_data.n_alternatives; i++)
2065 recog_data.alternative_enabled_p[i] = true;
2066 else
2068 recog_data.insn = insn;
2069 for (i = 0; i < recog_data.n_alternatives; i++)
2071 which_alternative = i;
2072 recog_data.alternative_enabled_p[i] = get_attr_enabled (insn);
2076 recog_data.insn = NULL;
2077 which_alternative = -1;
2080 /* After calling extract_insn, you can use this function to extract some
2081 information from the constraint strings into a more usable form.
2082 The collected data is stored in recog_op_alt. */
2083 void
2084 preprocess_constraints (void)
2086 int i;
2088 for (i = 0; i < recog_data.n_operands; i++)
2089 memset (recog_op_alt[i], 0, (recog_data.n_alternatives
2090 * sizeof (struct operand_alternative)));
2092 for (i = 0; i < recog_data.n_operands; i++)
2094 int j;
2095 struct operand_alternative *op_alt;
2096 const char *p = recog_data.constraints[i];
2098 op_alt = recog_op_alt[i];
2100 for (j = 0; j < recog_data.n_alternatives; j++)
2102 op_alt[j].cl = NO_REGS;
2103 op_alt[j].constraint = p;
2104 op_alt[j].matches = -1;
2105 op_alt[j].matched = -1;
2107 if (!recog_data.alternative_enabled_p[j])
2109 p = skip_alternative (p);
2110 continue;
2113 if (*p == '\0' || *p == ',')
2115 op_alt[j].anything_ok = 1;
2116 continue;
2119 for (;;)
2121 char c = *p;
2122 if (c == '#')
2124 c = *++p;
2125 while (c != ',' && c != '\0');
2126 if (c == ',' || c == '\0')
2128 p++;
2129 break;
2132 switch (c)
2134 case '=': case '+': case '*': case '%':
2135 case 'E': case 'F': case 'G': case 'H':
2136 case 's': case 'i': case 'n':
2137 case 'I': case 'J': case 'K': case 'L':
2138 case 'M': case 'N': case 'O': case 'P':
2139 /* These don't say anything we care about. */
2140 break;
2142 case '?':
2143 op_alt[j].reject += 6;
2144 break;
2145 case '!':
2146 op_alt[j].reject += 600;
2147 break;
2148 case '&':
2149 op_alt[j].earlyclobber = 1;
2150 break;
2152 case '0': case '1': case '2': case '3': case '4':
2153 case '5': case '6': case '7': case '8': case '9':
2155 char *end;
2156 op_alt[j].matches = strtoul (p, &end, 10);
2157 recog_op_alt[op_alt[j].matches][j].matched = i;
2158 p = end;
2160 continue;
2162 case TARGET_MEM_CONSTRAINT:
2163 op_alt[j].memory_ok = 1;
2164 break;
2165 case '<':
2166 op_alt[j].decmem_ok = 1;
2167 break;
2168 case '>':
2169 op_alt[j].incmem_ok = 1;
2170 break;
2171 case 'V':
2172 op_alt[j].nonoffmem_ok = 1;
2173 break;
2174 case 'o':
2175 op_alt[j].offmem_ok = 1;
2176 break;
2177 case 'X':
2178 op_alt[j].anything_ok = 1;
2179 break;
2181 case 'p':
2182 op_alt[j].is_address = 1;
2183 op_alt[j].cl = reg_class_subunion[(int) op_alt[j].cl]
2184 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
2185 break;
2187 case 'g':
2188 case 'r':
2189 op_alt[j].cl =
2190 reg_class_subunion[(int) op_alt[j].cl][(int) GENERAL_REGS];
2191 break;
2193 default:
2194 if (EXTRA_MEMORY_CONSTRAINT (c, p))
2196 op_alt[j].memory_ok = 1;
2197 break;
2199 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
2201 op_alt[j].is_address = 1;
2202 op_alt[j].cl
2203 = (reg_class_subunion
2204 [(int) op_alt[j].cl]
2205 [(int) base_reg_class (VOIDmode, ADDRESS,
2206 SCRATCH)]);
2207 break;
2210 op_alt[j].cl
2211 = (reg_class_subunion
2212 [(int) op_alt[j].cl]
2213 [(int) REG_CLASS_FROM_CONSTRAINT ((unsigned char) c, p)]);
2214 break;
2216 p += CONSTRAINT_LEN (c, p);
2222 /* Check the operands of an insn against the insn's operand constraints
2223 and return 1 if they are valid.
2224 The information about the insn's operands, constraints, operand modes
2225 etc. is obtained from the global variables set up by extract_insn.
2227 WHICH_ALTERNATIVE is set to a number which indicates which
2228 alternative of constraints was matched: 0 for the first alternative,
2229 1 for the next, etc.
2231 In addition, when two operands are required to match
2232 and it happens that the output operand is (reg) while the
2233 input operand is --(reg) or ++(reg) (a pre-inc or pre-dec),
2234 make the output operand look like the input.
2235 This is because the output operand is the one the template will print.
2237 This is used in final, just before printing the assembler code and by
2238 the routines that determine an insn's attribute.
2240 If STRICT is a positive nonzero value, it means that we have been
2241 called after reload has been completed. In that case, we must
2242 do all checks strictly. If it is zero, it means that we have been called
2243 before reload has completed. In that case, we first try to see if we can
2244 find an alternative that matches strictly. If not, we try again, this
2245 time assuming that reload will fix up the insn. This provides a "best
2246 guess" for the alternative and is used to compute attributes of insns prior
2247 to reload. A negative value of STRICT is used for this internal call. */
2249 struct funny_match
2251 int this_op, other;
2255 constrain_operands (int strict)
2257 const char *constraints[MAX_RECOG_OPERANDS];
2258 int matching_operands[MAX_RECOG_OPERANDS];
2259 int earlyclobber[MAX_RECOG_OPERANDS];
2260 int c;
2262 struct funny_match funny_match[MAX_RECOG_OPERANDS];
2263 int funny_match_index;
2265 which_alternative = 0;
2266 if (recog_data.n_operands == 0 || recog_data.n_alternatives == 0)
2267 return 1;
2269 for (c = 0; c < recog_data.n_operands; c++)
2271 constraints[c] = recog_data.constraints[c];
2272 matching_operands[c] = -1;
2277 int seen_earlyclobber_at = -1;
2278 int opno;
2279 int lose = 0;
2280 funny_match_index = 0;
2282 if (!recog_data.alternative_enabled_p[which_alternative])
2284 int i;
2286 for (i = 0; i < recog_data.n_operands; i++)
2287 constraints[i] = skip_alternative (constraints[i]);
2289 which_alternative++;
2290 continue;
2293 for (opno = 0; opno < recog_data.n_operands; opno++)
2295 rtx op = recog_data.operand[opno];
2296 enum machine_mode mode = GET_MODE (op);
2297 const char *p = constraints[opno];
2298 int offset = 0;
2299 int win = 0;
2300 int val;
2301 int len;
2303 earlyclobber[opno] = 0;
2305 /* A unary operator may be accepted by the predicate, but it
2306 is irrelevant for matching constraints. */
2307 if (UNARY_P (op))
2308 op = XEXP (op, 0);
2310 if (GET_CODE (op) == SUBREG)
2312 if (REG_P (SUBREG_REG (op))
2313 && REGNO (SUBREG_REG (op)) < FIRST_PSEUDO_REGISTER)
2314 offset = subreg_regno_offset (REGNO (SUBREG_REG (op)),
2315 GET_MODE (SUBREG_REG (op)),
2316 SUBREG_BYTE (op),
2317 GET_MODE (op));
2318 op = SUBREG_REG (op);
2321 /* An empty constraint or empty alternative
2322 allows anything which matched the pattern. */
2323 if (*p == 0 || *p == ',')
2324 win = 1;
2327 switch (c = *p, len = CONSTRAINT_LEN (c, p), c)
2329 case '\0':
2330 len = 0;
2331 break;
2332 case ',':
2333 c = '\0';
2334 break;
2336 case '?': case '!': case '*': case '%':
2337 case '=': case '+':
2338 break;
2340 case '#':
2341 /* Ignore rest of this alternative as far as
2342 constraint checking is concerned. */
2344 p++;
2345 while (*p && *p != ',');
2346 len = 0;
2347 break;
2349 case '&':
2350 earlyclobber[opno] = 1;
2351 if (seen_earlyclobber_at < 0)
2352 seen_earlyclobber_at = opno;
2353 break;
2355 case '0': case '1': case '2': case '3': case '4':
2356 case '5': case '6': case '7': case '8': case '9':
2358 /* This operand must be the same as a previous one.
2359 This kind of constraint is used for instructions such
2360 as add when they take only two operands.
2362 Note that the lower-numbered operand is passed first.
2364 If we are not testing strictly, assume that this
2365 constraint will be satisfied. */
2367 char *end;
2368 int match;
2370 match = strtoul (p, &end, 10);
2371 p = end;
2373 if (strict < 0)
2374 val = 1;
2375 else
2377 rtx op1 = recog_data.operand[match];
2378 rtx op2 = recog_data.operand[opno];
2380 /* A unary operator may be accepted by the predicate,
2381 but it is irrelevant for matching constraints. */
2382 if (UNARY_P (op1))
2383 op1 = XEXP (op1, 0);
2384 if (UNARY_P (op2))
2385 op2 = XEXP (op2, 0);
2387 val = operands_match_p (op1, op2);
2390 matching_operands[opno] = match;
2391 matching_operands[match] = opno;
2393 if (val != 0)
2394 win = 1;
2396 /* If output is *x and input is *--x, arrange later
2397 to change the output to *--x as well, since the
2398 output op is the one that will be printed. */
2399 if (val == 2 && strict > 0)
2401 funny_match[funny_match_index].this_op = opno;
2402 funny_match[funny_match_index++].other = match;
2405 len = 0;
2406 break;
2408 case 'p':
2409 /* p is used for address_operands. When we are called by
2410 gen_reload, no one will have checked that the address is
2411 strictly valid, i.e., that all pseudos requiring hard regs
2412 have gotten them. */
2413 if (strict <= 0
2414 || (strict_memory_address_p (recog_data.operand_mode[opno],
2415 op)))
2416 win = 1;
2417 break;
2419 /* No need to check general_operand again;
2420 it was done in insn-recog.c. Well, except that reload
2421 doesn't check the validity of its replacements, but
2422 that should only matter when there's a bug. */
2423 case 'g':
2424 /* Anything goes unless it is a REG and really has a hard reg
2425 but the hard reg is not in the class GENERAL_REGS. */
2426 if (REG_P (op))
2428 if (strict < 0
2429 || GENERAL_REGS == ALL_REGS
2430 || (reload_in_progress
2431 && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2432 || reg_fits_class_p (op, GENERAL_REGS, offset, mode))
2433 win = 1;
2435 else if (strict < 0 || general_operand (op, mode))
2436 win = 1;
2437 break;
2439 case 'X':
2440 /* This is used for a MATCH_SCRATCH in the cases when
2441 we don't actually need anything. So anything goes
2442 any time. */
2443 win = 1;
2444 break;
2446 case TARGET_MEM_CONSTRAINT:
2447 /* Memory operands must be valid, to the extent
2448 required by STRICT. */
2449 if (MEM_P (op))
2451 if (strict > 0
2452 && !strict_memory_address_p (GET_MODE (op),
2453 XEXP (op, 0)))
2454 break;
2455 if (strict == 0
2456 && !memory_address_p (GET_MODE (op), XEXP (op, 0)))
2457 break;
2458 win = 1;
2460 /* Before reload, accept what reload can turn into mem. */
2461 else if (strict < 0 && CONSTANT_P (op))
2462 win = 1;
2463 /* During reload, accept a pseudo */
2464 else if (reload_in_progress && REG_P (op)
2465 && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2466 win = 1;
2467 break;
2469 case '<':
2470 if (MEM_P (op)
2471 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
2472 || GET_CODE (XEXP (op, 0)) == POST_DEC))
2473 win = 1;
2474 break;
2476 case '>':
2477 if (MEM_P (op)
2478 && (GET_CODE (XEXP (op, 0)) == PRE_INC
2479 || GET_CODE (XEXP (op, 0)) == POST_INC))
2480 win = 1;
2481 break;
2483 case 'E':
2484 case 'F':
2485 if (GET_CODE (op) == CONST_DOUBLE
2486 || (GET_CODE (op) == CONST_VECTOR
2487 && GET_MODE_CLASS (GET_MODE (op)) == MODE_VECTOR_FLOAT))
2488 win = 1;
2489 break;
2491 case 'G':
2492 case 'H':
2493 if (GET_CODE (op) == CONST_DOUBLE
2494 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
2495 win = 1;
2496 break;
2498 case 's':
2499 if (GET_CODE (op) == CONST_INT
2500 || (GET_CODE (op) == CONST_DOUBLE
2501 && GET_MODE (op) == VOIDmode))
2502 break;
2503 case 'i':
2504 if (CONSTANT_P (op))
2505 win = 1;
2506 break;
2508 case 'n':
2509 if (GET_CODE (op) == CONST_INT
2510 || (GET_CODE (op) == CONST_DOUBLE
2511 && GET_MODE (op) == VOIDmode))
2512 win = 1;
2513 break;
2515 case 'I':
2516 case 'J':
2517 case 'K':
2518 case 'L':
2519 case 'M':
2520 case 'N':
2521 case 'O':
2522 case 'P':
2523 if (GET_CODE (op) == CONST_INT
2524 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
2525 win = 1;
2526 break;
2528 case 'V':
2529 if (MEM_P (op)
2530 && ((strict > 0 && ! offsettable_memref_p (op))
2531 || (strict < 0
2532 && !(CONSTANT_P (op) || MEM_P (op)))
2533 || (reload_in_progress
2534 && !(REG_P (op)
2535 && REGNO (op) >= FIRST_PSEUDO_REGISTER))))
2536 win = 1;
2537 break;
2539 case 'o':
2540 if ((strict > 0 && offsettable_memref_p (op))
2541 || (strict == 0 && offsettable_nonstrict_memref_p (op))
2542 /* Before reload, accept what reload can handle. */
2543 || (strict < 0
2544 && (CONSTANT_P (op) || MEM_P (op)))
2545 /* During reload, accept a pseudo */
2546 || (reload_in_progress && REG_P (op)
2547 && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2548 win = 1;
2549 break;
2551 default:
2553 enum reg_class cl;
2555 cl = (c == 'r'
2556 ? GENERAL_REGS : REG_CLASS_FROM_CONSTRAINT (c, p));
2557 if (cl != NO_REGS)
2559 if (strict < 0
2560 || (strict == 0
2561 && REG_P (op)
2562 && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2563 || (strict == 0 && GET_CODE (op) == SCRATCH)
2564 || (REG_P (op)
2565 && reg_fits_class_p (op, cl, offset, mode)))
2566 win = 1;
2568 #ifdef EXTRA_CONSTRAINT_STR
2569 else if (EXTRA_CONSTRAINT_STR (op, c, p))
2570 win = 1;
2572 else if (EXTRA_MEMORY_CONSTRAINT (c, p)
2573 /* Every memory operand can be reloaded to fit. */
2574 && ((strict < 0 && MEM_P (op))
2575 /* Before reload, accept what reload can turn
2576 into mem. */
2577 || (strict < 0 && CONSTANT_P (op))
2578 /* During reload, accept a pseudo */
2579 || (reload_in_progress && REG_P (op)
2580 && REGNO (op) >= FIRST_PSEUDO_REGISTER)))
2581 win = 1;
2582 else if (EXTRA_ADDRESS_CONSTRAINT (c, p)
2583 /* Every address operand can be reloaded to fit. */
2584 && strict < 0)
2585 win = 1;
2586 #endif
2587 break;
2590 while (p += len, c);
2592 constraints[opno] = p;
2593 /* If this operand did not win somehow,
2594 this alternative loses. */
2595 if (! win)
2596 lose = 1;
2598 /* This alternative won; the operands are ok.
2599 Change whichever operands this alternative says to change. */
2600 if (! lose)
2602 int opno, eopno;
2604 /* See if any earlyclobber operand conflicts with some other
2605 operand. */
2607 if (strict > 0 && seen_earlyclobber_at >= 0)
2608 for (eopno = seen_earlyclobber_at;
2609 eopno < recog_data.n_operands;
2610 eopno++)
2611 /* Ignore earlyclobber operands now in memory,
2612 because we would often report failure when we have
2613 two memory operands, one of which was formerly a REG. */
2614 if (earlyclobber[eopno]
2615 && REG_P (recog_data.operand[eopno]))
2616 for (opno = 0; opno < recog_data.n_operands; opno++)
2617 if ((MEM_P (recog_data.operand[opno])
2618 || recog_data.operand_type[opno] != OP_OUT)
2619 && opno != eopno
2620 /* Ignore things like match_operator operands. */
2621 && *recog_data.constraints[opno] != 0
2622 && ! (matching_operands[opno] == eopno
2623 && operands_match_p (recog_data.operand[opno],
2624 recog_data.operand[eopno]))
2625 && ! safe_from_earlyclobber (recog_data.operand[opno],
2626 recog_data.operand[eopno]))
2627 lose = 1;
2629 if (! lose)
2631 while (--funny_match_index >= 0)
2633 recog_data.operand[funny_match[funny_match_index].other]
2634 = recog_data.operand[funny_match[funny_match_index].this_op];
2637 return 1;
2641 which_alternative++;
2643 while (which_alternative < recog_data.n_alternatives);
2645 which_alternative = -1;
2646 /* If we are about to reject this, but we are not to test strictly,
2647 try a very loose test. Only return failure if it fails also. */
2648 if (strict == 0)
2649 return constrain_operands (-1);
2650 else
2651 return 0;
2654 /* Return 1 iff OPERAND (assumed to be a REG rtx)
2655 is a hard reg in class CLASS when its regno is offset by OFFSET
2656 and changed to mode MODE.
2657 If REG occupies multiple hard regs, all of them must be in CLASS. */
2660 reg_fits_class_p (rtx operand, enum reg_class cl, int offset,
2661 enum machine_mode mode)
2663 int regno = REGNO (operand);
2665 if (cl == NO_REGS)
2666 return 0;
2668 return (regno < FIRST_PSEUDO_REGISTER
2669 && in_hard_reg_set_p (reg_class_contents[(int) cl],
2670 mode, regno + offset));
2673 /* Split single instruction. Helper function for split_all_insns and
2674 split_all_insns_noflow. Return last insn in the sequence if successful,
2675 or NULL if unsuccessful. */
2677 static rtx
2678 split_insn (rtx insn)
2680 /* Split insns here to get max fine-grain parallelism. */
2681 rtx first = PREV_INSN (insn);
2682 rtx last = try_split (PATTERN (insn), insn, 1);
2683 rtx insn_set, last_set, note;
2685 if (last == insn)
2686 return NULL_RTX;
2688 /* If the original instruction was a single set that was known to be
2689 equivalent to a constant, see if we can say the same about the last
2690 instruction in the split sequence. The two instructions must set
2691 the same destination. */
2692 insn_set = single_set (insn);
2693 if (insn_set)
2695 last_set = single_set (last);
2696 if (last_set && rtx_equal_p (SET_DEST (last_set), SET_DEST (insn_set)))
2698 note = find_reg_equal_equiv_note (insn);
2699 if (note && CONSTANT_P (XEXP (note, 0)))
2700 set_unique_reg_note (last, REG_EQUAL, XEXP (note, 0));
2701 else if (CONSTANT_P (SET_SRC (insn_set)))
2702 set_unique_reg_note (last, REG_EQUAL, SET_SRC (insn_set));
2706 /* try_split returns the NOTE that INSN became. */
2707 SET_INSN_DELETED (insn);
2709 /* ??? Coddle to md files that generate subregs in post-reload
2710 splitters instead of computing the proper hard register. */
2711 if (reload_completed && first != last)
2713 first = NEXT_INSN (first);
2714 for (;;)
2716 if (INSN_P (first))
2717 cleanup_subreg_operands (first);
2718 if (first == last)
2719 break;
2720 first = NEXT_INSN (first);
2724 return last;
2727 /* Split all insns in the function. If UPD_LIFE, update life info after. */
2729 void
2730 split_all_insns (void)
2732 sbitmap blocks;
2733 bool changed;
2734 basic_block bb;
2736 blocks = sbitmap_alloc (last_basic_block);
2737 sbitmap_zero (blocks);
2738 changed = false;
2740 FOR_EACH_BB_REVERSE (bb)
2742 rtx insn, next;
2743 bool finish = false;
2745 rtl_profile_for_bb (bb);
2746 for (insn = BB_HEAD (bb); !finish ; insn = next)
2748 /* Can't use `next_real_insn' because that might go across
2749 CODE_LABELS and short-out basic blocks. */
2750 next = NEXT_INSN (insn);
2751 finish = (insn == BB_END (bb));
2752 if (INSN_P (insn))
2754 rtx set = single_set (insn);
2756 /* Don't split no-op move insns. These should silently
2757 disappear later in final. Splitting such insns would
2758 break the code that handles LIBCALL blocks. */
2759 if (set && set_noop_p (set))
2761 /* Nops get in the way while scheduling, so delete them
2762 now if register allocation has already been done. It
2763 is too risky to try to do this before register
2764 allocation, and there are unlikely to be very many
2765 nops then anyways. */
2766 if (reload_completed)
2767 delete_insn_and_edges (insn);
2769 else
2771 rtx last = split_insn (insn);
2772 if (last)
2774 /* The split sequence may include barrier, but the
2775 BB boundary we are interested in will be set to
2776 previous one. */
2778 while (BARRIER_P (last))
2779 last = PREV_INSN (last);
2780 SET_BIT (blocks, bb->index);
2781 changed = true;
2788 default_rtl_profile ();
2789 if (changed)
2790 find_many_sub_basic_blocks (blocks);
2792 #ifdef ENABLE_CHECKING
2793 verify_flow_info ();
2794 #endif
2796 sbitmap_free (blocks);
2799 /* Same as split_all_insns, but do not expect CFG to be available.
2800 Used by machine dependent reorg passes. */
2802 unsigned int
2803 split_all_insns_noflow (void)
2805 rtx next, insn;
2807 for (insn = get_insns (); insn; insn = next)
2809 next = NEXT_INSN (insn);
2810 if (INSN_P (insn))
2812 /* Don't split no-op move insns. These should silently
2813 disappear later in final. Splitting such insns would
2814 break the code that handles LIBCALL blocks. */
2815 rtx set = single_set (insn);
2816 if (set && set_noop_p (set))
2818 /* Nops get in the way while scheduling, so delete them
2819 now if register allocation has already been done. It
2820 is too risky to try to do this before register
2821 allocation, and there are unlikely to be very many
2822 nops then anyways.
2824 ??? Should we use delete_insn when the CFG isn't valid? */
2825 if (reload_completed)
2826 delete_insn_and_edges (insn);
2828 else
2829 split_insn (insn);
2832 return 0;
2835 #ifdef HAVE_peephole2
2836 struct peep2_insn_data
2838 rtx insn;
2839 regset live_before;
2842 static struct peep2_insn_data peep2_insn_data[MAX_INSNS_PER_PEEP2 + 1];
2843 static int peep2_current;
2844 /* The number of instructions available to match a peep2. */
2845 int peep2_current_count;
2847 /* A non-insn marker indicating the last insn of the block.
2848 The live_before regset for this element is correct, indicating
2849 DF_LIVE_OUT for the block. */
2850 #define PEEP2_EOB pc_rtx
2852 /* Return the Nth non-note insn after `current', or return NULL_RTX if it
2853 does not exist. Used by the recognizer to find the next insn to match
2854 in a multi-insn pattern. */
2857 peep2_next_insn (int n)
2859 gcc_assert (n <= peep2_current_count);
2861 n += peep2_current;
2862 if (n >= MAX_INSNS_PER_PEEP2 + 1)
2863 n -= MAX_INSNS_PER_PEEP2 + 1;
2865 return peep2_insn_data[n].insn;
2868 /* Return true if REGNO is dead before the Nth non-note insn
2869 after `current'. */
2872 peep2_regno_dead_p (int ofs, int regno)
2874 gcc_assert (ofs < MAX_INSNS_PER_PEEP2 + 1);
2876 ofs += peep2_current;
2877 if (ofs >= MAX_INSNS_PER_PEEP2 + 1)
2878 ofs -= MAX_INSNS_PER_PEEP2 + 1;
2880 gcc_assert (peep2_insn_data[ofs].insn != NULL_RTX);
2882 return ! REGNO_REG_SET_P (peep2_insn_data[ofs].live_before, regno);
2885 /* Similarly for a REG. */
2888 peep2_reg_dead_p (int ofs, rtx reg)
2890 int regno, n;
2892 gcc_assert (ofs < MAX_INSNS_PER_PEEP2 + 1);
2894 ofs += peep2_current;
2895 if (ofs >= MAX_INSNS_PER_PEEP2 + 1)
2896 ofs -= MAX_INSNS_PER_PEEP2 + 1;
2898 gcc_assert (peep2_insn_data[ofs].insn != NULL_RTX);
2900 regno = REGNO (reg);
2901 n = hard_regno_nregs[regno][GET_MODE (reg)];
2902 while (--n >= 0)
2903 if (REGNO_REG_SET_P (peep2_insn_data[ofs].live_before, regno + n))
2904 return 0;
2905 return 1;
2908 /* Try to find a hard register of mode MODE, matching the register class in
2909 CLASS_STR, which is available at the beginning of insn CURRENT_INSN and
2910 remains available until the end of LAST_INSN. LAST_INSN may be NULL_RTX,
2911 in which case the only condition is that the register must be available
2912 before CURRENT_INSN.
2913 Registers that already have bits set in REG_SET will not be considered.
2915 If an appropriate register is available, it will be returned and the
2916 corresponding bit(s) in REG_SET will be set; otherwise, NULL_RTX is
2917 returned. */
2920 peep2_find_free_register (int from, int to, const char *class_str,
2921 enum machine_mode mode, HARD_REG_SET *reg_set)
2923 static int search_ofs;
2924 enum reg_class cl;
2925 HARD_REG_SET live;
2926 int i;
2928 gcc_assert (from < MAX_INSNS_PER_PEEP2 + 1);
2929 gcc_assert (to < MAX_INSNS_PER_PEEP2 + 1);
2931 from += peep2_current;
2932 if (from >= MAX_INSNS_PER_PEEP2 + 1)
2933 from -= MAX_INSNS_PER_PEEP2 + 1;
2934 to += peep2_current;
2935 if (to >= MAX_INSNS_PER_PEEP2 + 1)
2936 to -= MAX_INSNS_PER_PEEP2 + 1;
2938 gcc_assert (peep2_insn_data[from].insn != NULL_RTX);
2939 REG_SET_TO_HARD_REG_SET (live, peep2_insn_data[from].live_before);
2941 while (from != to)
2943 HARD_REG_SET this_live;
2945 if (++from >= MAX_INSNS_PER_PEEP2 + 1)
2946 from = 0;
2947 gcc_assert (peep2_insn_data[from].insn != NULL_RTX);
2948 REG_SET_TO_HARD_REG_SET (this_live, peep2_insn_data[from].live_before);
2949 IOR_HARD_REG_SET (live, this_live);
2952 cl = (class_str[0] == 'r' ? GENERAL_REGS
2953 : REG_CLASS_FROM_CONSTRAINT (class_str[0], class_str));
2955 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2957 int raw_regno, regno, success, j;
2959 /* Distribute the free registers as much as possible. */
2960 raw_regno = search_ofs + i;
2961 if (raw_regno >= FIRST_PSEUDO_REGISTER)
2962 raw_regno -= FIRST_PSEUDO_REGISTER;
2963 #ifdef REG_ALLOC_ORDER
2964 regno = reg_alloc_order[raw_regno];
2965 #else
2966 regno = raw_regno;
2967 #endif
2969 /* Don't allocate fixed registers. */
2970 if (fixed_regs[regno])
2971 continue;
2972 /* Don't allocate global registers. */
2973 if (global_regs[regno])
2974 continue;
2975 /* Make sure the register is of the right class. */
2976 if (! TEST_HARD_REG_BIT (reg_class_contents[cl], regno))
2977 continue;
2978 /* And can support the mode we need. */
2979 if (! HARD_REGNO_MODE_OK (regno, mode))
2980 continue;
2981 /* And that we don't create an extra save/restore. */
2982 if (! call_used_regs[regno] && ! df_regs_ever_live_p (regno))
2983 continue;
2984 if (! targetm.hard_regno_scratch_ok (regno))
2985 continue;
2987 /* And we don't clobber traceback for noreturn functions. */
2988 if ((regno == FRAME_POINTER_REGNUM || regno == HARD_FRAME_POINTER_REGNUM)
2989 && (! reload_completed || frame_pointer_needed))
2990 continue;
2992 success = 1;
2993 for (j = hard_regno_nregs[regno][mode] - 1; j >= 0; j--)
2995 if (TEST_HARD_REG_BIT (*reg_set, regno + j)
2996 || TEST_HARD_REG_BIT (live, regno + j))
2998 success = 0;
2999 break;
3002 if (success)
3004 add_to_hard_reg_set (reg_set, mode, regno);
3006 /* Start the next search with the next register. */
3007 if (++raw_regno >= FIRST_PSEUDO_REGISTER)
3008 raw_regno = 0;
3009 search_ofs = raw_regno;
3011 return gen_rtx_REG (mode, regno);
3015 search_ofs = 0;
3016 return NULL_RTX;
3019 /* Perform the peephole2 optimization pass. */
3021 static void
3022 peephole2_optimize (void)
3024 rtx insn, prev;
3025 bitmap live;
3026 int i;
3027 basic_block bb;
3028 bool do_cleanup_cfg = false;
3029 bool do_rebuild_jump_labels = false;
3031 df_set_flags (DF_LR_RUN_DCE);
3032 df_analyze ();
3034 /* Initialize the regsets we're going to use. */
3035 for (i = 0; i < MAX_INSNS_PER_PEEP2 + 1; ++i)
3036 peep2_insn_data[i].live_before = BITMAP_ALLOC (&reg_obstack);
3037 live = BITMAP_ALLOC (&reg_obstack);
3039 FOR_EACH_BB_REVERSE (bb)
3041 rtl_profile_for_bb (bb);
3042 /* Indicate that all slots except the last holds invalid data. */
3043 for (i = 0; i < MAX_INSNS_PER_PEEP2; ++i)
3044 peep2_insn_data[i].insn = NULL_RTX;
3045 peep2_current_count = 0;
3047 /* Indicate that the last slot contains live_after data. */
3048 peep2_insn_data[MAX_INSNS_PER_PEEP2].insn = PEEP2_EOB;
3049 peep2_current = MAX_INSNS_PER_PEEP2;
3051 /* Start up propagation. */
3052 bitmap_copy (live, DF_LR_OUT (bb));
3053 df_simulate_initialize_backwards (bb, live);
3054 bitmap_copy (peep2_insn_data[MAX_INSNS_PER_PEEP2].live_before, live);
3056 for (insn = BB_END (bb); ; insn = prev)
3058 prev = PREV_INSN (insn);
3059 if (INSN_P (insn))
3061 rtx attempt, before_try, x;
3062 int match_len;
3063 rtx note;
3064 bool was_call = false;
3066 /* Record this insn. */
3067 if (--peep2_current < 0)
3068 peep2_current = MAX_INSNS_PER_PEEP2;
3069 if (peep2_current_count < MAX_INSNS_PER_PEEP2
3070 && peep2_insn_data[peep2_current].insn == NULL_RTX)
3071 peep2_current_count++;
3072 peep2_insn_data[peep2_current].insn = insn;
3073 df_simulate_one_insn_backwards (bb, insn, live);
3074 COPY_REG_SET (peep2_insn_data[peep2_current].live_before, live);
3076 if (RTX_FRAME_RELATED_P (insn))
3078 /* If an insn has RTX_FRAME_RELATED_P set, peephole
3079 substitution would lose the
3080 REG_FRAME_RELATED_EXPR that is attached. */
3081 peep2_current_count = 0;
3082 attempt = NULL;
3084 else
3085 /* Match the peephole. */
3086 attempt = peephole2_insns (PATTERN (insn), insn, &match_len);
3088 if (attempt != NULL)
3090 /* If we are splitting a CALL_INSN, look for the CALL_INSN
3091 in SEQ and copy our CALL_INSN_FUNCTION_USAGE and other
3092 cfg-related call notes. */
3093 for (i = 0; i <= match_len; ++i)
3095 int j;
3096 rtx old_insn, new_insn, note;
3098 j = i + peep2_current;
3099 if (j >= MAX_INSNS_PER_PEEP2 + 1)
3100 j -= MAX_INSNS_PER_PEEP2 + 1;
3101 old_insn = peep2_insn_data[j].insn;
3102 if (!CALL_P (old_insn))
3103 continue;
3104 was_call = true;
3106 new_insn = attempt;
3107 while (new_insn != NULL_RTX)
3109 if (CALL_P (new_insn))
3110 break;
3111 new_insn = NEXT_INSN (new_insn);
3114 gcc_assert (new_insn != NULL_RTX);
3116 CALL_INSN_FUNCTION_USAGE (new_insn)
3117 = CALL_INSN_FUNCTION_USAGE (old_insn);
3119 for (note = REG_NOTES (old_insn);
3120 note;
3121 note = XEXP (note, 1))
3122 switch (REG_NOTE_KIND (note))
3124 case REG_NORETURN:
3125 case REG_SETJMP:
3126 add_reg_note (new_insn, REG_NOTE_KIND (note),
3127 XEXP (note, 0));
3128 break;
3129 default:
3130 /* Discard all other reg notes. */
3131 break;
3134 /* Croak if there is another call in the sequence. */
3135 while (++i <= match_len)
3137 j = i + peep2_current;
3138 if (j >= MAX_INSNS_PER_PEEP2 + 1)
3139 j -= MAX_INSNS_PER_PEEP2 + 1;
3140 old_insn = peep2_insn_data[j].insn;
3141 gcc_assert (!CALL_P (old_insn));
3143 break;
3146 i = match_len + peep2_current;
3147 if (i >= MAX_INSNS_PER_PEEP2 + 1)
3148 i -= MAX_INSNS_PER_PEEP2 + 1;
3150 note = find_reg_note (peep2_insn_data[i].insn,
3151 REG_EH_REGION, NULL_RTX);
3153 /* Replace the old sequence with the new. */
3154 attempt = emit_insn_after_setloc (attempt,
3155 peep2_insn_data[i].insn,
3156 INSN_LOCATOR (peep2_insn_data[i].insn));
3157 before_try = PREV_INSN (insn);
3158 delete_insn_chain (insn, peep2_insn_data[i].insn, false);
3160 /* Re-insert the EH_REGION notes. */
3161 if (note || (was_call && nonlocal_goto_handler_labels))
3163 edge eh_edge;
3164 edge_iterator ei;
3166 FOR_EACH_EDGE (eh_edge, ei, bb->succs)
3167 if (eh_edge->flags & (EDGE_EH | EDGE_ABNORMAL_CALL))
3168 break;
3170 for (x = attempt ; x != before_try ; x = PREV_INSN (x))
3171 if (CALL_P (x)
3172 || (flag_non_call_exceptions
3173 && may_trap_p (PATTERN (x))
3174 && !find_reg_note (x, REG_EH_REGION, NULL)))
3176 if (note)
3177 add_reg_note (x, REG_EH_REGION, XEXP (note, 0));
3179 if (x != BB_END (bb) && eh_edge)
3181 edge nfte, nehe;
3182 int flags;
3184 nfte = split_block (bb, x);
3185 flags = (eh_edge->flags
3186 & (EDGE_EH | EDGE_ABNORMAL));
3187 if (CALL_P (x))
3188 flags |= EDGE_ABNORMAL_CALL;
3189 nehe = make_edge (nfte->src, eh_edge->dest,
3190 flags);
3192 nehe->probability = eh_edge->probability;
3193 nfte->probability
3194 = REG_BR_PROB_BASE - nehe->probability;
3196 do_cleanup_cfg |= purge_dead_edges (nfte->dest);
3197 bb = nfte->src;
3198 eh_edge = nehe;
3202 /* Converting possibly trapping insn to non-trapping is
3203 possible. Zap dummy outgoing edges. */
3204 do_cleanup_cfg |= purge_dead_edges (bb);
3207 #ifdef HAVE_conditional_execution
3208 for (i = 0; i < MAX_INSNS_PER_PEEP2 + 1; ++i)
3209 peep2_insn_data[i].insn = NULL_RTX;
3210 peep2_insn_data[peep2_current].insn = PEEP2_EOB;
3211 peep2_current_count = 0;
3212 #else
3213 /* Back up lifetime information past the end of the
3214 newly created sequence. */
3215 if (++i >= MAX_INSNS_PER_PEEP2 + 1)
3216 i = 0;
3217 bitmap_copy (live, peep2_insn_data[i].live_before);
3219 /* Update life information for the new sequence. */
3220 x = attempt;
3223 if (INSN_P (x))
3225 if (--i < 0)
3226 i = MAX_INSNS_PER_PEEP2;
3227 if (peep2_current_count < MAX_INSNS_PER_PEEP2
3228 && peep2_insn_data[i].insn == NULL_RTX)
3229 peep2_current_count++;
3230 peep2_insn_data[i].insn = x;
3231 df_insn_rescan (x);
3232 df_simulate_one_insn_backwards (bb, x, live);
3233 bitmap_copy (peep2_insn_data[i].live_before, live);
3235 x = PREV_INSN (x);
3237 while (x != prev);
3239 peep2_current = i;
3240 #endif
3242 /* If we generated a jump instruction, it won't have
3243 JUMP_LABEL set. Recompute after we're done. */
3244 for (x = attempt; x != before_try; x = PREV_INSN (x))
3245 if (JUMP_P (x))
3247 do_rebuild_jump_labels = true;
3248 break;
3253 if (insn == BB_HEAD (bb))
3254 break;
3258 default_rtl_profile ();
3259 for (i = 0; i < MAX_INSNS_PER_PEEP2 + 1; ++i)
3260 BITMAP_FREE (peep2_insn_data[i].live_before);
3261 BITMAP_FREE (live);
3262 if (do_rebuild_jump_labels)
3263 rebuild_jump_labels (get_insns ());
3265 #endif /* HAVE_peephole2 */
3267 /* Common predicates for use with define_bypass. */
3269 /* True if the dependency between OUT_INSN and IN_INSN is on the store
3270 data not the address operand(s) of the store. IN_INSN and OUT_INSN
3271 must be either a single_set or a PARALLEL with SETs inside. */
3274 store_data_bypass_p (rtx out_insn, rtx in_insn)
3276 rtx out_set, in_set;
3277 rtx out_pat, in_pat;
3278 rtx out_exp, in_exp;
3279 int i, j;
3281 in_set = single_set (in_insn);
3282 if (in_set)
3284 if (!MEM_P (SET_DEST (in_set)))
3285 return false;
3287 out_set = single_set (out_insn);
3288 if (out_set)
3290 if (reg_mentioned_p (SET_DEST (out_set), SET_DEST (in_set)))
3291 return false;
3293 else
3295 out_pat = PATTERN (out_insn);
3297 if (GET_CODE (out_pat) != PARALLEL)
3298 return false;
3300 for (i = 0; i < XVECLEN (out_pat, 0); i++)
3302 out_exp = XVECEXP (out_pat, 0, i);
3304 if (GET_CODE (out_exp) == CLOBBER)
3305 continue;
3307 gcc_assert (GET_CODE (out_exp) == SET);
3309 if (reg_mentioned_p (SET_DEST (out_exp), SET_DEST (in_set)))
3310 return false;
3314 else
3316 in_pat = PATTERN (in_insn);
3317 gcc_assert (GET_CODE (in_pat) == PARALLEL);
3319 for (i = 0; i < XVECLEN (in_pat, 0); i++)
3321 in_exp = XVECEXP (in_pat, 0, i);
3323 if (GET_CODE (in_exp) == CLOBBER)
3324 continue;
3326 gcc_assert (GET_CODE (in_exp) == SET);
3328 if (!MEM_P (SET_DEST (in_exp)))
3329 return false;
3331 out_set = single_set (out_insn);
3332 if (out_set)
3334 if (reg_mentioned_p (SET_DEST (out_set), SET_DEST (in_exp)))
3335 return false;
3337 else
3339 out_pat = PATTERN (out_insn);
3340 gcc_assert (GET_CODE (out_pat) == PARALLEL);
3342 for (j = 0; j < XVECLEN (out_pat, 0); j++)
3344 out_exp = XVECEXP (out_pat, 0, j);
3346 if (GET_CODE (out_exp) == CLOBBER)
3347 continue;
3349 gcc_assert (GET_CODE (out_exp) == SET);
3351 if (reg_mentioned_p (SET_DEST (out_exp), SET_DEST (in_exp)))
3352 return false;
3358 return true;
3361 /* True if the dependency between OUT_INSN and IN_INSN is in the IF_THEN_ELSE
3362 condition, and not the THEN or ELSE branch. OUT_INSN may be either a single
3363 or multiple set; IN_INSN should be single_set for truth, but for convenience
3364 of insn categorization may be any JUMP or CALL insn. */
3367 if_test_bypass_p (rtx out_insn, rtx in_insn)
3369 rtx out_set, in_set;
3371 in_set = single_set (in_insn);
3372 if (! in_set)
3374 gcc_assert (JUMP_P (in_insn) || CALL_P (in_insn));
3375 return false;
3378 if (GET_CODE (SET_SRC (in_set)) != IF_THEN_ELSE)
3379 return false;
3380 in_set = SET_SRC (in_set);
3382 out_set = single_set (out_insn);
3383 if (out_set)
3385 if (reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 1))
3386 || reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 2)))
3387 return false;
3389 else
3391 rtx out_pat;
3392 int i;
3394 out_pat = PATTERN (out_insn);
3395 gcc_assert (GET_CODE (out_pat) == PARALLEL);
3397 for (i = 0; i < XVECLEN (out_pat, 0); i++)
3399 rtx exp = XVECEXP (out_pat, 0, i);
3401 if (GET_CODE (exp) == CLOBBER)
3402 continue;
3404 gcc_assert (GET_CODE (exp) == SET);
3406 if (reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 1))
3407 || reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 2)))
3408 return false;
3412 return true;
3415 static bool
3416 gate_handle_peephole2 (void)
3418 return (optimize > 0 && flag_peephole2);
3421 static unsigned int
3422 rest_of_handle_peephole2 (void)
3424 #ifdef HAVE_peephole2
3425 peephole2_optimize ();
3426 #endif
3427 return 0;
3430 struct rtl_opt_pass pass_peephole2 =
3433 RTL_PASS,
3434 "peephole2", /* name */
3435 gate_handle_peephole2, /* gate */
3436 rest_of_handle_peephole2, /* execute */
3437 NULL, /* sub */
3438 NULL, /* next */
3439 0, /* static_pass_number */
3440 TV_PEEPHOLE2, /* tv_id */
3441 0, /* properties_required */
3442 0, /* properties_provided */
3443 0, /* properties_destroyed */
3444 0, /* todo_flags_start */
3445 TODO_df_finish | TODO_verify_rtl_sharing |
3446 TODO_dump_func /* todo_flags_finish */
3450 static unsigned int
3451 rest_of_handle_split_all_insns (void)
3453 split_all_insns ();
3454 return 0;
3457 struct rtl_opt_pass pass_split_all_insns =
3460 RTL_PASS,
3461 "split1", /* name */
3462 NULL, /* gate */
3463 rest_of_handle_split_all_insns, /* execute */
3464 NULL, /* sub */
3465 NULL, /* next */
3466 0, /* static_pass_number */
3467 0, /* tv_id */
3468 0, /* properties_required */
3469 0, /* properties_provided */
3470 0, /* properties_destroyed */
3471 0, /* todo_flags_start */
3472 TODO_dump_func /* todo_flags_finish */
3476 static unsigned int
3477 rest_of_handle_split_after_reload (void)
3479 /* If optimizing, then go ahead and split insns now. */
3480 #ifndef STACK_REGS
3481 if (optimize > 0)
3482 #endif
3483 split_all_insns ();
3484 return 0;
3487 struct rtl_opt_pass pass_split_after_reload =
3490 RTL_PASS,
3491 "split2", /* name */
3492 NULL, /* gate */
3493 rest_of_handle_split_after_reload, /* execute */
3494 NULL, /* sub */
3495 NULL, /* next */
3496 0, /* static_pass_number */
3497 0, /* tv_id */
3498 0, /* properties_required */
3499 0, /* properties_provided */
3500 0, /* properties_destroyed */
3501 0, /* todo_flags_start */
3502 TODO_dump_func /* todo_flags_finish */
3506 static bool
3507 gate_handle_split_before_regstack (void)
3509 #if defined (HAVE_ATTR_length) && defined (STACK_REGS)
3510 /* If flow2 creates new instructions which need splitting
3511 and scheduling after reload is not done, they might not be
3512 split until final which doesn't allow splitting
3513 if HAVE_ATTR_length. */
3514 # ifdef INSN_SCHEDULING
3515 return (optimize && !flag_schedule_insns_after_reload);
3516 # else
3517 return (optimize);
3518 # endif
3519 #else
3520 return 0;
3521 #endif
3524 static unsigned int
3525 rest_of_handle_split_before_regstack (void)
3527 split_all_insns ();
3528 return 0;
3531 struct rtl_opt_pass pass_split_before_regstack =
3534 RTL_PASS,
3535 "split3", /* name */
3536 gate_handle_split_before_regstack, /* gate */
3537 rest_of_handle_split_before_regstack, /* execute */
3538 NULL, /* sub */
3539 NULL, /* next */
3540 0, /* static_pass_number */
3541 0, /* tv_id */
3542 0, /* properties_required */
3543 0, /* properties_provided */
3544 0, /* properties_destroyed */
3545 0, /* todo_flags_start */
3546 TODO_dump_func /* todo_flags_finish */
3550 static bool
3551 gate_handle_split_before_sched2 (void)
3553 #ifdef INSN_SCHEDULING
3554 return optimize > 0 && flag_schedule_insns_after_reload;
3555 #else
3556 return 0;
3557 #endif
3560 static unsigned int
3561 rest_of_handle_split_before_sched2 (void)
3563 #ifdef INSN_SCHEDULING
3564 split_all_insns ();
3565 #endif
3566 return 0;
3569 struct rtl_opt_pass pass_split_before_sched2 =
3572 RTL_PASS,
3573 "split4", /* name */
3574 gate_handle_split_before_sched2, /* gate */
3575 rest_of_handle_split_before_sched2, /* execute */
3576 NULL, /* sub */
3577 NULL, /* next */
3578 0, /* static_pass_number */
3579 0, /* tv_id */
3580 0, /* properties_required */
3581 0, /* properties_provided */
3582 0, /* properties_destroyed */
3583 0, /* todo_flags_start */
3584 TODO_verify_flow |
3585 TODO_dump_func /* todo_flags_finish */
3589 /* The placement of the splitting that we do for shorten_branches
3590 depends on whether regstack is used by the target or not. */
3591 static bool
3592 gate_do_final_split (void)
3594 #if defined (HAVE_ATTR_length) && !defined (STACK_REGS)
3595 return 1;
3596 #else
3597 return 0;
3598 #endif
3601 struct rtl_opt_pass pass_split_for_shorten_branches =
3604 RTL_PASS,
3605 "split5", /* name */
3606 gate_do_final_split, /* gate */
3607 split_all_insns_noflow, /* execute */
3608 NULL, /* sub */
3609 NULL, /* next */
3610 0, /* static_pass_number */
3611 0, /* tv_id */
3612 0, /* properties_required */
3613 0, /* properties_provided */
3614 0, /* properties_destroyed */
3615 0, /* todo_flags_start */
3616 TODO_dump_func | TODO_verify_rtl_sharing /* todo_flags_finish */