poly_int: expand_vector_ubsan_overflow
[official-gcc.git] / gcc / recog.c
blob99031df553d18fbeb2b21dc4fe8cea13c65d96fc
1 /* Subroutines used by or related to instruction recognition.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "cfghooks.h"
29 #include "df.h"
30 #include "memmodel.h"
31 #include "tm_p.h"
32 #include "insn-config.h"
33 #include "regs.h"
34 #include "emit-rtl.h"
35 #include "recog.h"
36 #include "insn-attr.h"
37 #include "addresses.h"
38 #include "cfgrtl.h"
39 #include "cfgbuild.h"
40 #include "cfgcleanup.h"
41 #include "reload.h"
42 #include "tree-pass.h"
44 #ifndef STACK_POP_CODE
45 #if STACK_GROWS_DOWNWARD
46 #define STACK_POP_CODE POST_INC
47 #else
48 #define STACK_POP_CODE POST_DEC
49 #endif
50 #endif
52 static void validate_replace_rtx_1 (rtx *, rtx, rtx, rtx_insn *, bool);
53 static void validate_replace_src_1 (rtx *, void *);
54 static rtx_insn *split_insn (rtx_insn *);
56 struct target_recog default_target_recog;
57 #if SWITCHABLE_TARGET
58 struct target_recog *this_target_recog = &default_target_recog;
59 #endif
61 /* Nonzero means allow operands to be volatile.
62 This should be 0 if you are generating rtl, such as if you are calling
63 the functions in optabs.c and expmed.c (most of the time).
64 This should be 1 if all valid insns need to be recognized,
65 such as in reginfo.c and final.c and reload.c.
67 init_recog and init_recog_no_volatile are responsible for setting this. */
69 int volatile_ok;
71 struct recog_data_d recog_data;
73 /* Contains a vector of operand_alternative structures, such that
74 operand OP of alternative A is at index A * n_operands + OP.
75 Set up by preprocess_constraints. */
76 const operand_alternative *recog_op_alt;
78 /* Used to provide recog_op_alt for asms. */
79 static operand_alternative asm_op_alt[MAX_RECOG_OPERANDS
80 * MAX_RECOG_ALTERNATIVES];
82 /* On return from `constrain_operands', indicate which alternative
83 was satisfied. */
85 int which_alternative;
87 /* Nonzero after end of reload pass.
88 Set to 1 or 0 by toplev.c.
89 Controls the significance of (SUBREG (MEM)). */
91 int reload_completed;
93 /* Nonzero after thread_prologue_and_epilogue_insns has run. */
94 int epilogue_completed;
96 /* Initialize data used by the function `recog'.
97 This must be called once in the compilation of a function
98 before any insn recognition may be done in the function. */
100 void
101 init_recog_no_volatile (void)
103 volatile_ok = 0;
106 void
107 init_recog (void)
109 volatile_ok = 1;
113 /* Return true if labels in asm operands BODY are LABEL_REFs. */
115 static bool
116 asm_labels_ok (rtx body)
118 rtx asmop;
119 int i;
121 asmop = extract_asm_operands (body);
122 if (asmop == NULL_RTX)
123 return true;
125 for (i = 0; i < ASM_OPERANDS_LABEL_LENGTH (asmop); i++)
126 if (GET_CODE (ASM_OPERANDS_LABEL (asmop, i)) != LABEL_REF)
127 return false;
129 return true;
132 /* Check that X is an insn-body for an `asm' with operands
133 and that the operands mentioned in it are legitimate. */
136 check_asm_operands (rtx x)
138 int noperands;
139 rtx *operands;
140 const char **constraints;
141 int i;
143 if (!asm_labels_ok (x))
144 return 0;
146 /* Post-reload, be more strict with things. */
147 if (reload_completed)
149 /* ??? Doh! We've not got the wrapping insn. Cook one up. */
150 rtx_insn *insn = make_insn_raw (x);
151 extract_insn (insn);
152 constrain_operands (1, get_enabled_alternatives (insn));
153 return which_alternative >= 0;
156 noperands = asm_noperands (x);
157 if (noperands < 0)
158 return 0;
159 if (noperands == 0)
160 return 1;
162 operands = XALLOCAVEC (rtx, noperands);
163 constraints = XALLOCAVEC (const char *, noperands);
165 decode_asm_operands (x, operands, NULL, constraints, NULL, NULL);
167 for (i = 0; i < noperands; i++)
169 const char *c = constraints[i];
170 if (c[0] == '%')
171 c++;
172 if (! asm_operand_ok (operands[i], c, constraints))
173 return 0;
176 return 1;
179 /* Static data for the next two routines. */
181 struct change_t
183 rtx object;
184 int old_code;
185 bool unshare;
186 rtx *loc;
187 rtx old;
190 static change_t *changes;
191 static int changes_allocated;
193 static int num_changes = 0;
195 /* Validate a proposed change to OBJECT. LOC is the location in the rtl
196 at which NEW_RTX will be placed. If OBJECT is zero, no validation is done,
197 the change is simply made.
199 Two types of objects are supported: If OBJECT is a MEM, memory_address_p
200 will be called with the address and mode as parameters. If OBJECT is
201 an INSN, CALL_INSN, or JUMP_INSN, the insn will be re-recognized with
202 the change in place.
204 IN_GROUP is nonzero if this is part of a group of changes that must be
205 performed as a group. In that case, the changes will be stored. The
206 function `apply_change_group' will validate and apply the changes.
208 If IN_GROUP is zero, this is a single change. Try to recognize the insn
209 or validate the memory reference with the change applied. If the result
210 is not valid for the machine, suppress the change and return zero.
211 Otherwise, perform the change and return 1. */
213 static bool
214 validate_change_1 (rtx object, rtx *loc, rtx new_rtx, bool in_group, bool unshare)
216 rtx old = *loc;
218 if (old == new_rtx || rtx_equal_p (old, new_rtx))
219 return 1;
221 gcc_assert (in_group != 0 || num_changes == 0);
223 *loc = new_rtx;
225 /* Save the information describing this change. */
226 if (num_changes >= changes_allocated)
228 if (changes_allocated == 0)
229 /* This value allows for repeated substitutions inside complex
230 indexed addresses, or changes in up to 5 insns. */
231 changes_allocated = MAX_RECOG_OPERANDS * 5;
232 else
233 changes_allocated *= 2;
235 changes = XRESIZEVEC (change_t, changes, changes_allocated);
238 changes[num_changes].object = object;
239 changes[num_changes].loc = loc;
240 changes[num_changes].old = old;
241 changes[num_changes].unshare = unshare;
243 if (object && !MEM_P (object))
245 /* Set INSN_CODE to force rerecognition of insn. Save old code in
246 case invalid. */
247 changes[num_changes].old_code = INSN_CODE (object);
248 INSN_CODE (object) = -1;
251 num_changes++;
253 /* If we are making a group of changes, return 1. Otherwise, validate the
254 change group we made. */
256 if (in_group)
257 return 1;
258 else
259 return apply_change_group ();
262 /* Wrapper for validate_change_1 without the UNSHARE argument defaulting
263 UNSHARE to false. */
265 bool
266 validate_change (rtx object, rtx *loc, rtx new_rtx, bool in_group)
268 return validate_change_1 (object, loc, new_rtx, in_group, false);
271 /* Wrapper for validate_change_1 without the UNSHARE argument defaulting
272 UNSHARE to true. */
274 bool
275 validate_unshare_change (rtx object, rtx *loc, rtx new_rtx, bool in_group)
277 return validate_change_1 (object, loc, new_rtx, in_group, true);
281 /* Keep X canonicalized if some changes have made it non-canonical; only
282 modifies the operands of X, not (for example) its code. Simplifications
283 are not the job of this routine.
285 Return true if anything was changed. */
286 bool
287 canonicalize_change_group (rtx_insn *insn, rtx x)
289 if (COMMUTATIVE_P (x)
290 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
292 /* Oops, the caller has made X no longer canonical.
293 Let's redo the changes in the correct order. */
294 rtx tem = XEXP (x, 0);
295 validate_unshare_change (insn, &XEXP (x, 0), XEXP (x, 1), 1);
296 validate_unshare_change (insn, &XEXP (x, 1), tem, 1);
297 return true;
299 else
300 return false;
304 /* This subroutine of apply_change_group verifies whether the changes to INSN
305 were valid; i.e. whether INSN can still be recognized.
307 If IN_GROUP is true clobbers which have to be added in order to
308 match the instructions will be added to the current change group.
309 Otherwise the changes will take effect immediately. */
312 insn_invalid_p (rtx_insn *insn, bool in_group)
314 rtx pat = PATTERN (insn);
315 int num_clobbers = 0;
316 /* If we are before reload and the pattern is a SET, see if we can add
317 clobbers. */
318 int icode = recog (pat, insn,
319 (GET_CODE (pat) == SET
320 && ! reload_completed
321 && ! reload_in_progress)
322 ? &num_clobbers : 0);
323 int is_asm = icode < 0 && asm_noperands (PATTERN (insn)) >= 0;
326 /* If this is an asm and the operand aren't legal, then fail. Likewise if
327 this is not an asm and the insn wasn't recognized. */
328 if ((is_asm && ! check_asm_operands (PATTERN (insn)))
329 || (!is_asm && icode < 0))
330 return 1;
332 /* If we have to add CLOBBERs, fail if we have to add ones that reference
333 hard registers since our callers can't know if they are live or not.
334 Otherwise, add them. */
335 if (num_clobbers > 0)
337 rtx newpat;
339 if (added_clobbers_hard_reg_p (icode))
340 return 1;
342 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_clobbers + 1));
343 XVECEXP (newpat, 0, 0) = pat;
344 add_clobbers (newpat, icode);
345 if (in_group)
346 validate_change (insn, &PATTERN (insn), newpat, 1);
347 else
348 PATTERN (insn) = pat = newpat;
351 /* After reload, verify that all constraints are satisfied. */
352 if (reload_completed)
354 extract_insn (insn);
356 if (! constrain_operands (1, get_preferred_alternatives (insn)))
357 return 1;
360 INSN_CODE (insn) = icode;
361 return 0;
364 /* Return number of changes made and not validated yet. */
366 num_changes_pending (void)
368 return num_changes;
371 /* Tentatively apply the changes numbered NUM and up.
372 Return 1 if all changes are valid, zero otherwise. */
375 verify_changes (int num)
377 int i;
378 rtx last_validated = NULL_RTX;
380 /* The changes have been applied and all INSN_CODEs have been reset to force
381 rerecognition.
383 The changes are valid if we aren't given an object, or if we are
384 given a MEM and it still is a valid address, or if this is in insn
385 and it is recognized. In the latter case, if reload has completed,
386 we also require that the operands meet the constraints for
387 the insn. */
389 for (i = num; i < num_changes; i++)
391 rtx object = changes[i].object;
393 /* If there is no object to test or if it is the same as the one we
394 already tested, ignore it. */
395 if (object == 0 || object == last_validated)
396 continue;
398 if (MEM_P (object))
400 if (! memory_address_addr_space_p (GET_MODE (object),
401 XEXP (object, 0),
402 MEM_ADDR_SPACE (object)))
403 break;
405 else if (/* changes[i].old might be zero, e.g. when putting a
406 REG_FRAME_RELATED_EXPR into a previously empty list. */
407 changes[i].old
408 && REG_P (changes[i].old)
409 && asm_noperands (PATTERN (object)) > 0
410 && REG_EXPR (changes[i].old) != NULL_TREE
411 && HAS_DECL_ASSEMBLER_NAME_P (REG_EXPR (changes[i].old))
412 && DECL_ASSEMBLER_NAME_SET_P (REG_EXPR (changes[i].old))
413 && DECL_REGISTER (REG_EXPR (changes[i].old)))
415 /* Don't allow changes of hard register operands to inline
416 assemblies if they have been defined as register asm ("x"). */
417 break;
419 else if (DEBUG_INSN_P (object))
420 continue;
421 else if (insn_invalid_p (as_a <rtx_insn *> (object), true))
423 rtx pat = PATTERN (object);
425 /* Perhaps we couldn't recognize the insn because there were
426 extra CLOBBERs at the end. If so, try to re-recognize
427 without the last CLOBBER (later iterations will cause each of
428 them to be eliminated, in turn). But don't do this if we
429 have an ASM_OPERAND. */
430 if (GET_CODE (pat) == PARALLEL
431 && GET_CODE (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1)) == CLOBBER
432 && asm_noperands (PATTERN (object)) < 0)
434 rtx newpat;
436 if (XVECLEN (pat, 0) == 2)
437 newpat = XVECEXP (pat, 0, 0);
438 else
440 int j;
442 newpat
443 = gen_rtx_PARALLEL (VOIDmode,
444 rtvec_alloc (XVECLEN (pat, 0) - 1));
445 for (j = 0; j < XVECLEN (newpat, 0); j++)
446 XVECEXP (newpat, 0, j) = XVECEXP (pat, 0, j);
449 /* Add a new change to this group to replace the pattern
450 with this new pattern. Then consider this change
451 as having succeeded. The change we added will
452 cause the entire call to fail if things remain invalid.
454 Note that this can lose if a later change than the one
455 we are processing specified &XVECEXP (PATTERN (object), 0, X)
456 but this shouldn't occur. */
458 validate_change (object, &PATTERN (object), newpat, 1);
459 continue;
461 else if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER
462 || GET_CODE (pat) == VAR_LOCATION)
463 /* If this insn is a CLOBBER or USE, it is always valid, but is
464 never recognized. */
465 continue;
466 else
467 break;
469 last_validated = object;
472 return (i == num_changes);
475 /* A group of changes has previously been issued with validate_change
476 and verified with verify_changes. Call df_insn_rescan for each of
477 the insn changed and clear num_changes. */
479 void
480 confirm_change_group (void)
482 int i;
483 rtx last_object = NULL;
485 for (i = 0; i < num_changes; i++)
487 rtx object = changes[i].object;
489 if (changes[i].unshare)
490 *changes[i].loc = copy_rtx (*changes[i].loc);
492 /* Avoid unnecessary rescanning when multiple changes to same instruction
493 are made. */
494 if (object)
496 if (object != last_object && last_object && INSN_P (last_object))
497 df_insn_rescan (as_a <rtx_insn *> (last_object));
498 last_object = object;
502 if (last_object && INSN_P (last_object))
503 df_insn_rescan (as_a <rtx_insn *> (last_object));
504 num_changes = 0;
507 /* Apply a group of changes previously issued with `validate_change'.
508 If all changes are valid, call confirm_change_group and return 1,
509 otherwise, call cancel_changes and return 0. */
512 apply_change_group (void)
514 if (verify_changes (0))
516 confirm_change_group ();
517 return 1;
519 else
521 cancel_changes (0);
522 return 0;
527 /* Return the number of changes so far in the current group. */
530 num_validated_changes (void)
532 return num_changes;
535 /* Retract the changes numbered NUM and up. */
537 void
538 cancel_changes (int num)
540 int i;
542 /* Back out all the changes. Do this in the opposite order in which
543 they were made. */
544 for (i = num_changes - 1; i >= num; i--)
546 *changes[i].loc = changes[i].old;
547 if (changes[i].object && !MEM_P (changes[i].object))
548 INSN_CODE (changes[i].object) = changes[i].old_code;
550 num_changes = num;
553 /* Reduce conditional compilation elsewhere. */
554 /* A subroutine of validate_replace_rtx_1 that tries to simplify the resulting
555 rtx. */
557 static void
558 simplify_while_replacing (rtx *loc, rtx to, rtx_insn *object,
559 machine_mode op0_mode)
561 rtx x = *loc;
562 enum rtx_code code = GET_CODE (x);
563 rtx new_rtx = NULL_RTX;
564 scalar_int_mode is_mode;
566 if (SWAPPABLE_OPERANDS_P (x)
567 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
569 validate_unshare_change (object, loc,
570 gen_rtx_fmt_ee (COMMUTATIVE_ARITH_P (x) ? code
571 : swap_condition (code),
572 GET_MODE (x), XEXP (x, 1),
573 XEXP (x, 0)), 1);
574 x = *loc;
575 code = GET_CODE (x);
578 /* Canonicalize arithmetics with all constant operands. */
579 switch (GET_RTX_CLASS (code))
581 case RTX_UNARY:
582 if (CONSTANT_P (XEXP (x, 0)))
583 new_rtx = simplify_unary_operation (code, GET_MODE (x), XEXP (x, 0),
584 op0_mode);
585 break;
586 case RTX_COMM_ARITH:
587 case RTX_BIN_ARITH:
588 if (CONSTANT_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
589 new_rtx = simplify_binary_operation (code, GET_MODE (x), XEXP (x, 0),
590 XEXP (x, 1));
591 break;
592 case RTX_COMPARE:
593 case RTX_COMM_COMPARE:
594 if (CONSTANT_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
595 new_rtx = simplify_relational_operation (code, GET_MODE (x), op0_mode,
596 XEXP (x, 0), XEXP (x, 1));
597 break;
598 default:
599 break;
601 if (new_rtx)
603 validate_change (object, loc, new_rtx, 1);
604 return;
607 switch (code)
609 case PLUS:
610 /* If we have a PLUS whose second operand is now a CONST_INT, use
611 simplify_gen_binary to try to simplify it.
612 ??? We may want later to remove this, once simplification is
613 separated from this function. */
614 if (CONST_INT_P (XEXP (x, 1)) && XEXP (x, 1) == to)
615 validate_change (object, loc,
616 simplify_gen_binary
617 (PLUS, GET_MODE (x), XEXP (x, 0), XEXP (x, 1)), 1);
618 break;
619 case MINUS:
620 if (CONST_SCALAR_INT_P (XEXP (x, 1)))
621 validate_change (object, loc,
622 simplify_gen_binary
623 (PLUS, GET_MODE (x), XEXP (x, 0),
624 simplify_gen_unary (NEG,
625 GET_MODE (x), XEXP (x, 1),
626 GET_MODE (x))), 1);
627 break;
628 case ZERO_EXTEND:
629 case SIGN_EXTEND:
630 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
632 new_rtx = simplify_gen_unary (code, GET_MODE (x), XEXP (x, 0),
633 op0_mode);
634 /* If any of the above failed, substitute in something that
635 we know won't be recognized. */
636 if (!new_rtx)
637 new_rtx = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
638 validate_change (object, loc, new_rtx, 1);
640 break;
641 case SUBREG:
642 /* All subregs possible to simplify should be simplified. */
643 new_rtx = simplify_subreg (GET_MODE (x), SUBREG_REG (x), op0_mode,
644 SUBREG_BYTE (x));
646 /* Subregs of VOIDmode operands are incorrect. */
647 if (!new_rtx && GET_MODE (SUBREG_REG (x)) == VOIDmode)
648 new_rtx = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
649 if (new_rtx)
650 validate_change (object, loc, new_rtx, 1);
651 break;
652 case ZERO_EXTRACT:
653 case SIGN_EXTRACT:
654 /* If we are replacing a register with memory, try to change the memory
655 to be the mode required for memory in extract operations (this isn't
656 likely to be an insertion operation; if it was, nothing bad will
657 happen, we might just fail in some cases). */
659 if (MEM_P (XEXP (x, 0))
660 && is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &is_mode)
661 && CONST_INT_P (XEXP (x, 1))
662 && CONST_INT_P (XEXP (x, 2))
663 && !mode_dependent_address_p (XEXP (XEXP (x, 0), 0),
664 MEM_ADDR_SPACE (XEXP (x, 0)))
665 && !MEM_VOLATILE_P (XEXP (x, 0)))
667 int pos = INTVAL (XEXP (x, 2));
668 machine_mode new_mode = is_mode;
669 if (GET_CODE (x) == ZERO_EXTRACT && targetm.have_extzv ())
670 new_mode = insn_data[targetm.code_for_extzv].operand[1].mode;
671 else if (GET_CODE (x) == SIGN_EXTRACT && targetm.have_extv ())
672 new_mode = insn_data[targetm.code_for_extv].operand[1].mode;
673 scalar_int_mode wanted_mode = (new_mode == VOIDmode
674 ? word_mode
675 : as_a <scalar_int_mode> (new_mode));
677 /* If we have a narrower mode, we can do something. */
678 if (GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
680 int offset = pos / BITS_PER_UNIT;
681 rtx newmem;
683 /* If the bytes and bits are counted differently, we
684 must adjust the offset. */
685 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
686 offset =
687 (GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (wanted_mode) -
688 offset);
690 gcc_assert (GET_MODE_PRECISION (wanted_mode)
691 == GET_MODE_BITSIZE (wanted_mode));
692 pos %= GET_MODE_BITSIZE (wanted_mode);
694 newmem = adjust_address_nv (XEXP (x, 0), wanted_mode, offset);
696 validate_change (object, &XEXP (x, 2), GEN_INT (pos), 1);
697 validate_change (object, &XEXP (x, 0), newmem, 1);
701 break;
703 default:
704 break;
708 /* Replace every occurrence of FROM in X with TO. Mark each change with
709 validate_change passing OBJECT. */
711 static void
712 validate_replace_rtx_1 (rtx *loc, rtx from, rtx to, rtx_insn *object,
713 bool simplify)
715 int i, j;
716 const char *fmt;
717 rtx x = *loc;
718 enum rtx_code code;
719 machine_mode op0_mode = VOIDmode;
720 int prev_changes = num_changes;
722 if (!x)
723 return;
725 code = GET_CODE (x);
726 fmt = GET_RTX_FORMAT (code);
727 if (fmt[0] == 'e')
728 op0_mode = GET_MODE (XEXP (x, 0));
730 /* X matches FROM if it is the same rtx or they are both referring to the
731 same register in the same mode. Avoid calling rtx_equal_p unless the
732 operands look similar. */
734 if (x == from
735 || (REG_P (x) && REG_P (from)
736 && GET_MODE (x) == GET_MODE (from)
737 && REGNO (x) == REGNO (from))
738 || (GET_CODE (x) == GET_CODE (from) && GET_MODE (x) == GET_MODE (from)
739 && rtx_equal_p (x, from)))
741 validate_unshare_change (object, loc, to, 1);
742 return;
745 /* Call ourself recursively to perform the replacements.
746 We must not replace inside already replaced expression, otherwise we
747 get infinite recursion for replacements like (reg X)->(subreg (reg X))
748 so we must special case shared ASM_OPERANDS. */
750 if (GET_CODE (x) == PARALLEL)
752 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
754 if (j && GET_CODE (XVECEXP (x, 0, j)) == SET
755 && GET_CODE (SET_SRC (XVECEXP (x, 0, j))) == ASM_OPERANDS)
757 /* Verify that operands are really shared. */
758 gcc_assert (ASM_OPERANDS_INPUT_VEC (SET_SRC (XVECEXP (x, 0, 0)))
759 == ASM_OPERANDS_INPUT_VEC (SET_SRC (XVECEXP
760 (x, 0, j))));
761 validate_replace_rtx_1 (&SET_DEST (XVECEXP (x, 0, j)),
762 from, to, object, simplify);
764 else
765 validate_replace_rtx_1 (&XVECEXP (x, 0, j), from, to, object,
766 simplify);
769 else
770 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
772 if (fmt[i] == 'e')
773 validate_replace_rtx_1 (&XEXP (x, i), from, to, object, simplify);
774 else if (fmt[i] == 'E')
775 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
776 validate_replace_rtx_1 (&XVECEXP (x, i, j), from, to, object,
777 simplify);
780 /* If we didn't substitute, there is nothing more to do. */
781 if (num_changes == prev_changes)
782 return;
784 /* ??? The regmove is no more, so is this aberration still necessary? */
785 /* Allow substituted expression to have different mode. This is used by
786 regmove to change mode of pseudo register. */
787 if (fmt[0] == 'e' && GET_MODE (XEXP (x, 0)) != VOIDmode)
788 op0_mode = GET_MODE (XEXP (x, 0));
790 /* Do changes needed to keep rtx consistent. Don't do any other
791 simplifications, as it is not our job. */
792 if (simplify)
793 simplify_while_replacing (loc, to, object, op0_mode);
796 /* Try replacing every occurrence of FROM in subexpression LOC of INSN
797 with TO. After all changes have been made, validate by seeing
798 if INSN is still valid. */
801 validate_replace_rtx_subexp (rtx from, rtx to, rtx_insn *insn, rtx *loc)
803 validate_replace_rtx_1 (loc, from, to, insn, true);
804 return apply_change_group ();
807 /* Try replacing every occurrence of FROM in INSN with TO. After all
808 changes have been made, validate by seeing if INSN is still valid. */
811 validate_replace_rtx (rtx from, rtx to, rtx_insn *insn)
813 validate_replace_rtx_1 (&PATTERN (insn), from, to, insn, true);
814 return apply_change_group ();
817 /* Try replacing every occurrence of FROM in WHERE with TO. Assume that WHERE
818 is a part of INSN. After all changes have been made, validate by seeing if
819 INSN is still valid.
820 validate_replace_rtx (from, to, insn) is equivalent to
821 validate_replace_rtx_part (from, to, &PATTERN (insn), insn). */
824 validate_replace_rtx_part (rtx from, rtx to, rtx *where, rtx_insn *insn)
826 validate_replace_rtx_1 (where, from, to, insn, true);
827 return apply_change_group ();
830 /* Same as above, but do not simplify rtx afterwards. */
832 validate_replace_rtx_part_nosimplify (rtx from, rtx to, rtx *where,
833 rtx_insn *insn)
835 validate_replace_rtx_1 (where, from, to, insn, false);
836 return apply_change_group ();
840 /* Try replacing every occurrence of FROM in INSN with TO. This also
841 will replace in REG_EQUAL and REG_EQUIV notes. */
843 void
844 validate_replace_rtx_group (rtx from, rtx to, rtx_insn *insn)
846 rtx note;
847 validate_replace_rtx_1 (&PATTERN (insn), from, to, insn, true);
848 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
849 if (REG_NOTE_KIND (note) == REG_EQUAL
850 || REG_NOTE_KIND (note) == REG_EQUIV)
851 validate_replace_rtx_1 (&XEXP (note, 0), from, to, insn, true);
854 /* Function called by note_uses to replace used subexpressions. */
855 struct validate_replace_src_data
857 rtx from; /* Old RTX */
858 rtx to; /* New RTX */
859 rtx_insn *insn; /* Insn in which substitution is occurring. */
862 static void
863 validate_replace_src_1 (rtx *x, void *data)
865 struct validate_replace_src_data *d
866 = (struct validate_replace_src_data *) data;
868 validate_replace_rtx_1 (x, d->from, d->to, d->insn, true);
871 /* Try replacing every occurrence of FROM in INSN with TO, avoiding
872 SET_DESTs. */
874 void
875 validate_replace_src_group (rtx from, rtx to, rtx_insn *insn)
877 struct validate_replace_src_data d;
879 d.from = from;
880 d.to = to;
881 d.insn = insn;
882 note_uses (&PATTERN (insn), validate_replace_src_1, &d);
885 /* Try simplify INSN.
886 Invoke simplify_rtx () on every SET_SRC and SET_DEST inside the INSN's
887 pattern and return true if something was simplified. */
889 bool
890 validate_simplify_insn (rtx_insn *insn)
892 int i;
893 rtx pat = NULL;
894 rtx newpat = NULL;
896 pat = PATTERN (insn);
898 if (GET_CODE (pat) == SET)
900 newpat = simplify_rtx (SET_SRC (pat));
901 if (newpat && !rtx_equal_p (SET_SRC (pat), newpat))
902 validate_change (insn, &SET_SRC (pat), newpat, 1);
903 newpat = simplify_rtx (SET_DEST (pat));
904 if (newpat && !rtx_equal_p (SET_DEST (pat), newpat))
905 validate_change (insn, &SET_DEST (pat), newpat, 1);
907 else if (GET_CODE (pat) == PARALLEL)
908 for (i = 0; i < XVECLEN (pat, 0); i++)
910 rtx s = XVECEXP (pat, 0, i);
912 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
914 newpat = simplify_rtx (SET_SRC (s));
915 if (newpat && !rtx_equal_p (SET_SRC (s), newpat))
916 validate_change (insn, &SET_SRC (s), newpat, 1);
917 newpat = simplify_rtx (SET_DEST (s));
918 if (newpat && !rtx_equal_p (SET_DEST (s), newpat))
919 validate_change (insn, &SET_DEST (s), newpat, 1);
922 return ((num_changes_pending () > 0) && (apply_change_group () > 0));
925 /* Return 1 if the insn using CC0 set by INSN does not contain
926 any ordered tests applied to the condition codes.
927 EQ and NE tests do not count. */
930 next_insn_tests_no_inequality (rtx_insn *insn)
932 rtx_insn *next = next_cc0_user (insn);
934 /* If there is no next insn, we have to take the conservative choice. */
935 if (next == 0)
936 return 0;
938 return (INSN_P (next)
939 && ! inequality_comparisons_p (PATTERN (next)));
942 /* Return 1 if OP is a valid general operand for machine mode MODE.
943 This is either a register reference, a memory reference,
944 or a constant. In the case of a memory reference, the address
945 is checked for general validity for the target machine.
947 Register and memory references must have mode MODE in order to be valid,
948 but some constants have no machine mode and are valid for any mode.
950 If MODE is VOIDmode, OP is checked for validity for whatever mode
951 it has.
953 The main use of this function is as a predicate in match_operand
954 expressions in the machine description. */
957 general_operand (rtx op, machine_mode mode)
959 enum rtx_code code = GET_CODE (op);
961 if (mode == VOIDmode)
962 mode = GET_MODE (op);
964 /* Don't accept CONST_INT or anything similar
965 if the caller wants something floating. */
966 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
967 && GET_MODE_CLASS (mode) != MODE_INT
968 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
969 return 0;
971 if (CONST_INT_P (op)
972 && mode != VOIDmode
973 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
974 return 0;
976 if (CONSTANT_P (op))
977 return ((GET_MODE (op) == VOIDmode || GET_MODE (op) == mode
978 || mode == VOIDmode)
979 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
980 && targetm.legitimate_constant_p (mode == VOIDmode
981 ? GET_MODE (op)
982 : mode, op));
984 /* Except for certain constants with VOIDmode, already checked for,
985 OP's mode must match MODE if MODE specifies a mode. */
987 if (GET_MODE (op) != mode)
988 return 0;
990 if (code == SUBREG)
992 rtx sub = SUBREG_REG (op);
994 #ifdef INSN_SCHEDULING
995 /* On machines that have insn scheduling, we want all memory
996 reference to be explicit, so outlaw paradoxical SUBREGs.
997 However, we must allow them after reload so that they can
998 get cleaned up by cleanup_subreg_operands. */
999 if (!reload_completed && MEM_P (sub)
1000 && paradoxical_subreg_p (op))
1001 return 0;
1002 #endif
1003 /* Avoid memories with nonzero SUBREG_BYTE, as offsetting the memory
1004 may result in incorrect reference. We should simplify all valid
1005 subregs of MEM anyway. But allow this after reload because we
1006 might be called from cleanup_subreg_operands.
1008 ??? This is a kludge. */
1009 if (!reload_completed
1010 && maybe_ne (SUBREG_BYTE (op), 0)
1011 && MEM_P (sub))
1012 return 0;
1014 if (REG_P (sub)
1015 && REGNO (sub) < FIRST_PSEUDO_REGISTER
1016 && !REG_CAN_CHANGE_MODE_P (REGNO (sub), GET_MODE (sub), mode)
1017 && GET_MODE_CLASS (GET_MODE (sub)) != MODE_COMPLEX_INT
1018 && GET_MODE_CLASS (GET_MODE (sub)) != MODE_COMPLEX_FLOAT
1019 /* LRA can generate some invalid SUBREGS just for matched
1020 operand reload presentation. LRA needs to treat them as
1021 valid. */
1022 && ! LRA_SUBREG_P (op))
1023 return 0;
1025 /* FLOAT_MODE subregs can't be paradoxical. Combine will occasionally
1026 create such rtl, and we must reject it. */
1027 if (SCALAR_FLOAT_MODE_P (GET_MODE (op))
1028 /* LRA can use subreg to store a floating point value in an
1029 integer mode. Although the floating point and the
1030 integer modes need the same number of hard registers, the
1031 size of floating point mode can be less than the integer
1032 mode. */
1033 && ! lra_in_progress
1034 && paradoxical_subreg_p (op))
1035 return 0;
1037 op = sub;
1038 code = GET_CODE (op);
1041 if (code == REG)
1042 return (REGNO (op) >= FIRST_PSEUDO_REGISTER
1043 || in_hard_reg_set_p (operand_reg_set, GET_MODE (op), REGNO (op)));
1045 if (code == MEM)
1047 rtx y = XEXP (op, 0);
1049 if (! volatile_ok && MEM_VOLATILE_P (op))
1050 return 0;
1052 /* Use the mem's mode, since it will be reloaded thus. LRA can
1053 generate move insn with invalid addresses which is made valid
1054 and efficiently calculated by LRA through further numerous
1055 transformations. */
1056 if (lra_in_progress
1057 || memory_address_addr_space_p (GET_MODE (op), y, MEM_ADDR_SPACE (op)))
1058 return 1;
1061 return 0;
1064 /* Return 1 if OP is a valid memory address for a memory reference
1065 of mode MODE.
1067 The main use of this function is as a predicate in match_operand
1068 expressions in the machine description. */
1071 address_operand (rtx op, machine_mode mode)
1073 return memory_address_p (mode, op);
1076 /* Return 1 if OP is a register reference of mode MODE.
1077 If MODE is VOIDmode, accept a register in any mode.
1079 The main use of this function is as a predicate in match_operand
1080 expressions in the machine description. */
1083 register_operand (rtx op, machine_mode mode)
1085 if (GET_CODE (op) == SUBREG)
1087 rtx sub = SUBREG_REG (op);
1089 /* Before reload, we can allow (SUBREG (MEM...)) as a register operand
1090 because it is guaranteed to be reloaded into one.
1091 Just make sure the MEM is valid in itself.
1092 (Ideally, (SUBREG (MEM)...) should not exist after reload,
1093 but currently it does result from (SUBREG (REG)...) where the
1094 reg went on the stack.) */
1095 if (!REG_P (sub) && (reload_completed || !MEM_P (sub)))
1096 return 0;
1098 else if (!REG_P (op))
1099 return 0;
1100 return general_operand (op, mode);
1103 /* Return 1 for a register in Pmode; ignore the tested mode. */
1106 pmode_register_operand (rtx op, machine_mode mode ATTRIBUTE_UNUSED)
1108 return register_operand (op, Pmode);
1111 /* Return 1 if OP should match a MATCH_SCRATCH, i.e., if it is a SCRATCH
1112 or a hard register. */
1115 scratch_operand (rtx op, machine_mode mode)
1117 if (GET_MODE (op) != mode && mode != VOIDmode)
1118 return 0;
1120 return (GET_CODE (op) == SCRATCH
1121 || (REG_P (op)
1122 && (lra_in_progress
1123 || (REGNO (op) < FIRST_PSEUDO_REGISTER
1124 && REGNO_REG_CLASS (REGNO (op)) != NO_REGS))));
1127 /* Return 1 if OP is a valid immediate operand for mode MODE.
1129 The main use of this function is as a predicate in match_operand
1130 expressions in the machine description. */
1133 immediate_operand (rtx op, machine_mode mode)
1135 /* Don't accept CONST_INT or anything similar
1136 if the caller wants something floating. */
1137 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1138 && GET_MODE_CLASS (mode) != MODE_INT
1139 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1140 return 0;
1142 if (CONST_INT_P (op)
1143 && mode != VOIDmode
1144 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
1145 return 0;
1147 return (CONSTANT_P (op)
1148 && (GET_MODE (op) == mode || mode == VOIDmode
1149 || GET_MODE (op) == VOIDmode)
1150 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1151 && targetm.legitimate_constant_p (mode == VOIDmode
1152 ? GET_MODE (op)
1153 : mode, op));
1156 /* Returns 1 if OP is an operand that is a CONST_INT of mode MODE. */
1159 const_int_operand (rtx op, machine_mode mode)
1161 if (!CONST_INT_P (op))
1162 return 0;
1164 if (mode != VOIDmode
1165 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
1166 return 0;
1168 return 1;
1171 #if TARGET_SUPPORTS_WIDE_INT
1172 /* Returns 1 if OP is an operand that is a CONST_INT or CONST_WIDE_INT
1173 of mode MODE. */
1175 const_scalar_int_operand (rtx op, machine_mode mode)
1177 if (!CONST_SCALAR_INT_P (op))
1178 return 0;
1180 if (CONST_INT_P (op))
1181 return const_int_operand (op, mode);
1183 if (mode != VOIDmode)
1185 scalar_int_mode int_mode = as_a <scalar_int_mode> (mode);
1186 int prec = GET_MODE_PRECISION (int_mode);
1187 int bitsize = GET_MODE_BITSIZE (int_mode);
1189 if (CONST_WIDE_INT_NUNITS (op) * HOST_BITS_PER_WIDE_INT > bitsize)
1190 return 0;
1192 if (prec == bitsize)
1193 return 1;
1194 else
1196 /* Multiword partial int. */
1197 HOST_WIDE_INT x
1198 = CONST_WIDE_INT_ELT (op, CONST_WIDE_INT_NUNITS (op) - 1);
1199 return (sext_hwi (x, prec & (HOST_BITS_PER_WIDE_INT - 1)) == x);
1202 return 1;
1205 /* Returns 1 if OP is an operand that is a constant integer or constant
1206 floating-point number of MODE. */
1209 const_double_operand (rtx op, machine_mode mode)
1211 return (GET_CODE (op) == CONST_DOUBLE)
1212 && (GET_MODE (op) == mode || mode == VOIDmode);
1214 #else
1215 /* Returns 1 if OP is an operand that is a constant integer or constant
1216 floating-point number of MODE. */
1219 const_double_operand (rtx op, machine_mode mode)
1221 /* Don't accept CONST_INT or anything similar
1222 if the caller wants something floating. */
1223 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1224 && GET_MODE_CLASS (mode) != MODE_INT
1225 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1226 return 0;
1228 return ((CONST_DOUBLE_P (op) || CONST_INT_P (op))
1229 && (mode == VOIDmode || GET_MODE (op) == mode
1230 || GET_MODE (op) == VOIDmode));
1232 #endif
1233 /* Return 1 if OP is a general operand that is not an immediate
1234 operand of mode MODE. */
1237 nonimmediate_operand (rtx op, machine_mode mode)
1239 return (general_operand (op, mode) && ! CONSTANT_P (op));
1242 /* Return 1 if OP is a register reference or immediate value of mode MODE. */
1245 nonmemory_operand (rtx op, machine_mode mode)
1247 if (CONSTANT_P (op))
1248 return immediate_operand (op, mode);
1249 return register_operand (op, mode);
1252 /* Return 1 if OP is a valid operand that stands for pushing a
1253 value of mode MODE onto the stack.
1255 The main use of this function is as a predicate in match_operand
1256 expressions in the machine description. */
1259 push_operand (rtx op, machine_mode mode)
1261 unsigned int rounded_size = GET_MODE_SIZE (mode);
1263 #ifdef PUSH_ROUNDING
1264 rounded_size = PUSH_ROUNDING (rounded_size);
1265 #endif
1267 if (!MEM_P (op))
1268 return 0;
1270 if (mode != VOIDmode && GET_MODE (op) != mode)
1271 return 0;
1273 op = XEXP (op, 0);
1275 if (rounded_size == GET_MODE_SIZE (mode))
1277 if (GET_CODE (op) != STACK_PUSH_CODE)
1278 return 0;
1280 else
1282 if (GET_CODE (op) != PRE_MODIFY
1283 || GET_CODE (XEXP (op, 1)) != PLUS
1284 || XEXP (XEXP (op, 1), 0) != XEXP (op, 0)
1285 || !CONST_INT_P (XEXP (XEXP (op, 1), 1))
1286 || INTVAL (XEXP (XEXP (op, 1), 1))
1287 != ((STACK_GROWS_DOWNWARD ? -1 : 1) * (int) rounded_size))
1288 return 0;
1291 return XEXP (op, 0) == stack_pointer_rtx;
1294 /* Return 1 if OP is a valid operand that stands for popping a
1295 value of mode MODE off the stack.
1297 The main use of this function is as a predicate in match_operand
1298 expressions in the machine description. */
1301 pop_operand (rtx op, machine_mode mode)
1303 if (!MEM_P (op))
1304 return 0;
1306 if (mode != VOIDmode && GET_MODE (op) != mode)
1307 return 0;
1309 op = XEXP (op, 0);
1311 if (GET_CODE (op) != STACK_POP_CODE)
1312 return 0;
1314 return XEXP (op, 0) == stack_pointer_rtx;
1317 /* Return 1 if ADDR is a valid memory address
1318 for mode MODE in address space AS. */
1321 memory_address_addr_space_p (machine_mode mode ATTRIBUTE_UNUSED,
1322 rtx addr, addr_space_t as)
1324 #ifdef GO_IF_LEGITIMATE_ADDRESS
1325 gcc_assert (ADDR_SPACE_GENERIC_P (as));
1326 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
1327 return 0;
1329 win:
1330 return 1;
1331 #else
1332 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
1333 #endif
1336 /* Return 1 if OP is a valid memory reference with mode MODE,
1337 including a valid address.
1339 The main use of this function is as a predicate in match_operand
1340 expressions in the machine description. */
1343 memory_operand (rtx op, machine_mode mode)
1345 rtx inner;
1347 if (! reload_completed)
1348 /* Note that no SUBREG is a memory operand before end of reload pass,
1349 because (SUBREG (MEM...)) forces reloading into a register. */
1350 return MEM_P (op) && general_operand (op, mode);
1352 if (mode != VOIDmode && GET_MODE (op) != mode)
1353 return 0;
1355 inner = op;
1356 if (GET_CODE (inner) == SUBREG)
1357 inner = SUBREG_REG (inner);
1359 return (MEM_P (inner) && general_operand (op, mode));
1362 /* Return 1 if OP is a valid indirect memory reference with mode MODE;
1363 that is, a memory reference whose address is a general_operand. */
1366 indirect_operand (rtx op, machine_mode mode)
1368 /* Before reload, a SUBREG isn't in memory (see memory_operand, above). */
1369 if (! reload_completed
1370 && GET_CODE (op) == SUBREG && MEM_P (SUBREG_REG (op)))
1372 if (mode != VOIDmode && GET_MODE (op) != mode)
1373 return 0;
1375 /* The only way that we can have a general_operand as the resulting
1376 address is if OFFSET is zero and the address already is an operand
1377 or if the address is (plus Y (const_int -OFFSET)) and Y is an
1378 operand. */
1379 poly_int64 offset;
1380 rtx addr = strip_offset (XEXP (SUBREG_REG (op), 0), &offset);
1381 return (known_eq (offset + SUBREG_BYTE (op), 0)
1382 && general_operand (addr, Pmode));
1385 return (MEM_P (op)
1386 && memory_operand (op, mode)
1387 && general_operand (XEXP (op, 0), Pmode));
1390 /* Return 1 if this is an ordered comparison operator (not including
1391 ORDERED and UNORDERED). */
1394 ordered_comparison_operator (rtx op, machine_mode mode)
1396 if (mode != VOIDmode && GET_MODE (op) != mode)
1397 return false;
1398 switch (GET_CODE (op))
1400 case EQ:
1401 case NE:
1402 case LT:
1403 case LTU:
1404 case LE:
1405 case LEU:
1406 case GT:
1407 case GTU:
1408 case GE:
1409 case GEU:
1410 return true;
1411 default:
1412 return false;
1416 /* Return 1 if this is a comparison operator. This allows the use of
1417 MATCH_OPERATOR to recognize all the branch insns. */
1420 comparison_operator (rtx op, machine_mode mode)
1422 return ((mode == VOIDmode || GET_MODE (op) == mode)
1423 && COMPARISON_P (op));
1426 /* If BODY is an insn body that uses ASM_OPERANDS, return it. */
1429 extract_asm_operands (rtx body)
1431 rtx tmp;
1432 switch (GET_CODE (body))
1434 case ASM_OPERANDS:
1435 return body;
1437 case SET:
1438 /* Single output operand: BODY is (set OUTPUT (asm_operands ...)). */
1439 tmp = SET_SRC (body);
1440 if (GET_CODE (tmp) == ASM_OPERANDS)
1441 return tmp;
1442 break;
1444 case PARALLEL:
1445 tmp = XVECEXP (body, 0, 0);
1446 if (GET_CODE (tmp) == ASM_OPERANDS)
1447 return tmp;
1448 if (GET_CODE (tmp) == SET)
1450 tmp = SET_SRC (tmp);
1451 if (GET_CODE (tmp) == ASM_OPERANDS)
1452 return tmp;
1454 break;
1456 default:
1457 break;
1459 return NULL;
1462 /* If BODY is an insn body that uses ASM_OPERANDS,
1463 return the number of operands (both input and output) in the insn.
1464 If BODY is an insn body that uses ASM_INPUT with CLOBBERS in PARALLEL,
1465 return 0.
1466 Otherwise return -1. */
1469 asm_noperands (const_rtx body)
1471 rtx asm_op = extract_asm_operands (CONST_CAST_RTX (body));
1472 int i, n_sets = 0;
1474 if (asm_op == NULL)
1476 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) >= 2
1477 && GET_CODE (XVECEXP (body, 0, 0)) == ASM_INPUT)
1479 /* body is [(asm_input ...) (clobber (reg ...))...]. */
1480 for (i = XVECLEN (body, 0) - 1; i > 0; i--)
1481 if (GET_CODE (XVECEXP (body, 0, i)) != CLOBBER)
1482 return -1;
1483 return 0;
1485 return -1;
1488 if (GET_CODE (body) == SET)
1489 n_sets = 1;
1490 else if (GET_CODE (body) == PARALLEL)
1492 if (GET_CODE (XVECEXP (body, 0, 0)) == SET)
1494 /* Multiple output operands, or 1 output plus some clobbers:
1495 body is
1496 [(set OUTPUT (asm_operands ...))... (clobber (reg ...))...]. */
1497 /* Count backwards through CLOBBERs to determine number of SETs. */
1498 for (i = XVECLEN (body, 0); i > 0; i--)
1500 if (GET_CODE (XVECEXP (body, 0, i - 1)) == SET)
1501 break;
1502 if (GET_CODE (XVECEXP (body, 0, i - 1)) != CLOBBER)
1503 return -1;
1506 /* N_SETS is now number of output operands. */
1507 n_sets = i;
1509 /* Verify that all the SETs we have
1510 came from a single original asm_operands insn
1511 (so that invalid combinations are blocked). */
1512 for (i = 0; i < n_sets; i++)
1514 rtx elt = XVECEXP (body, 0, i);
1515 if (GET_CODE (elt) != SET)
1516 return -1;
1517 if (GET_CODE (SET_SRC (elt)) != ASM_OPERANDS)
1518 return -1;
1519 /* If these ASM_OPERANDS rtx's came from different original insns
1520 then they aren't allowed together. */
1521 if (ASM_OPERANDS_INPUT_VEC (SET_SRC (elt))
1522 != ASM_OPERANDS_INPUT_VEC (asm_op))
1523 return -1;
1526 else
1528 /* 0 outputs, but some clobbers:
1529 body is [(asm_operands ...) (clobber (reg ...))...]. */
1530 /* Make sure all the other parallel things really are clobbers. */
1531 for (i = XVECLEN (body, 0) - 1; i > 0; i--)
1532 if (GET_CODE (XVECEXP (body, 0, i)) != CLOBBER)
1533 return -1;
1537 return (ASM_OPERANDS_INPUT_LENGTH (asm_op)
1538 + ASM_OPERANDS_LABEL_LENGTH (asm_op) + n_sets);
1541 /* Assuming BODY is an insn body that uses ASM_OPERANDS,
1542 copy its operands (both input and output) into the vector OPERANDS,
1543 the locations of the operands within the insn into the vector OPERAND_LOCS,
1544 and the constraints for the operands into CONSTRAINTS.
1545 Write the modes of the operands into MODES.
1546 Write the location info into LOC.
1547 Return the assembler-template.
1548 If BODY is an insn body that uses ASM_INPUT with CLOBBERS in PARALLEL,
1549 return the basic assembly string.
1551 If LOC, MODES, OPERAND_LOCS, CONSTRAINTS or OPERANDS is 0,
1552 we don't store that info. */
1554 const char *
1555 decode_asm_operands (rtx body, rtx *operands, rtx **operand_locs,
1556 const char **constraints, machine_mode *modes,
1557 location_t *loc)
1559 int nbase = 0, n, i;
1560 rtx asmop;
1562 switch (GET_CODE (body))
1564 case ASM_OPERANDS:
1565 /* Zero output asm: BODY is (asm_operands ...). */
1566 asmop = body;
1567 break;
1569 case SET:
1570 /* Single output asm: BODY is (set OUTPUT (asm_operands ...)). */
1571 asmop = SET_SRC (body);
1573 /* The output is in the SET.
1574 Its constraint is in the ASM_OPERANDS itself. */
1575 if (operands)
1576 operands[0] = SET_DEST (body);
1577 if (operand_locs)
1578 operand_locs[0] = &SET_DEST (body);
1579 if (constraints)
1580 constraints[0] = ASM_OPERANDS_OUTPUT_CONSTRAINT (asmop);
1581 if (modes)
1582 modes[0] = GET_MODE (SET_DEST (body));
1583 nbase = 1;
1584 break;
1586 case PARALLEL:
1588 int nparallel = XVECLEN (body, 0); /* Includes CLOBBERs. */
1590 asmop = XVECEXP (body, 0, 0);
1591 if (GET_CODE (asmop) == SET)
1593 asmop = SET_SRC (asmop);
1595 /* At least one output, plus some CLOBBERs. The outputs are in
1596 the SETs. Their constraints are in the ASM_OPERANDS itself. */
1597 for (i = 0; i < nparallel; i++)
1599 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
1600 break; /* Past last SET */
1601 if (operands)
1602 operands[i] = SET_DEST (XVECEXP (body, 0, i));
1603 if (operand_locs)
1604 operand_locs[i] = &SET_DEST (XVECEXP (body, 0, i));
1605 if (constraints)
1606 constraints[i] = XSTR (SET_SRC (XVECEXP (body, 0, i)), 1);
1607 if (modes)
1608 modes[i] = GET_MODE (SET_DEST (XVECEXP (body, 0, i)));
1610 nbase = i;
1612 else if (GET_CODE (asmop) == ASM_INPUT)
1614 if (loc)
1615 *loc = ASM_INPUT_SOURCE_LOCATION (asmop);
1616 return XSTR (asmop, 0);
1618 break;
1621 default:
1622 gcc_unreachable ();
1625 n = ASM_OPERANDS_INPUT_LENGTH (asmop);
1626 for (i = 0; i < n; i++)
1628 if (operand_locs)
1629 operand_locs[nbase + i] = &ASM_OPERANDS_INPUT (asmop, i);
1630 if (operands)
1631 operands[nbase + i] = ASM_OPERANDS_INPUT (asmop, i);
1632 if (constraints)
1633 constraints[nbase + i] = ASM_OPERANDS_INPUT_CONSTRAINT (asmop, i);
1634 if (modes)
1635 modes[nbase + i] = ASM_OPERANDS_INPUT_MODE (asmop, i);
1637 nbase += n;
1639 n = ASM_OPERANDS_LABEL_LENGTH (asmop);
1640 for (i = 0; i < n; i++)
1642 if (operand_locs)
1643 operand_locs[nbase + i] = &ASM_OPERANDS_LABEL (asmop, i);
1644 if (operands)
1645 operands[nbase + i] = ASM_OPERANDS_LABEL (asmop, i);
1646 if (constraints)
1647 constraints[nbase + i] = "";
1648 if (modes)
1649 modes[nbase + i] = Pmode;
1652 if (loc)
1653 *loc = ASM_OPERANDS_SOURCE_LOCATION (asmop);
1655 return ASM_OPERANDS_TEMPLATE (asmop);
1658 /* Parse inline assembly string STRING and determine which operands are
1659 referenced by % markers. For the first NOPERANDS operands, set USED[I]
1660 to true if operand I is referenced.
1662 This is intended to distinguish barrier-like asms such as:
1664 asm ("" : "=m" (...));
1666 from real references such as:
1668 asm ("sw\t$0, %0" : "=m" (...)); */
1670 void
1671 get_referenced_operands (const char *string, bool *used,
1672 unsigned int noperands)
1674 memset (used, 0, sizeof (bool) * noperands);
1675 const char *p = string;
1676 while (*p)
1677 switch (*p)
1679 case '%':
1680 p += 1;
1681 /* A letter followed by a digit indicates an operand number. */
1682 if (ISALPHA (p[0]) && ISDIGIT (p[1]))
1683 p += 1;
1684 if (ISDIGIT (*p))
1686 char *endptr;
1687 unsigned long opnum = strtoul (p, &endptr, 10);
1688 if (endptr != p && opnum < noperands)
1689 used[opnum] = true;
1690 p = endptr;
1692 else
1693 p += 1;
1694 break;
1696 default:
1697 p++;
1698 break;
1702 /* Check if an asm_operand matches its constraints.
1703 Return > 0 if ok, = 0 if bad, < 0 if inconclusive. */
1706 asm_operand_ok (rtx op, const char *constraint, const char **constraints)
1708 int result = 0;
1709 bool incdec_ok = false;
1711 /* Use constrain_operands after reload. */
1712 gcc_assert (!reload_completed);
1714 /* Empty constraint string is the same as "X,...,X", i.e. X for as
1715 many alternatives as required to match the other operands. */
1716 if (*constraint == '\0')
1717 result = 1;
1719 while (*constraint)
1721 enum constraint_num cn;
1722 char c = *constraint;
1723 int len;
1724 switch (c)
1726 case ',':
1727 constraint++;
1728 continue;
1730 case '0': case '1': case '2': case '3': case '4':
1731 case '5': case '6': case '7': case '8': case '9':
1732 /* If caller provided constraints pointer, look up
1733 the matching constraint. Otherwise, our caller should have
1734 given us the proper matching constraint, but we can't
1735 actually fail the check if they didn't. Indicate that
1736 results are inconclusive. */
1737 if (constraints)
1739 char *end;
1740 unsigned long match;
1742 match = strtoul (constraint, &end, 10);
1743 if (!result)
1744 result = asm_operand_ok (op, constraints[match], NULL);
1745 constraint = (const char *) end;
1747 else
1750 constraint++;
1751 while (ISDIGIT (*constraint));
1752 if (! result)
1753 result = -1;
1755 continue;
1757 /* The rest of the compiler assumes that reloading the address
1758 of a MEM into a register will make it fit an 'o' constraint.
1759 That is, if it sees a MEM operand for an 'o' constraint,
1760 it assumes that (mem (base-reg)) will fit.
1762 That assumption fails on targets that don't have offsettable
1763 addresses at all. We therefore need to treat 'o' asm
1764 constraints as a special case and only accept operands that
1765 are already offsettable, thus proving that at least one
1766 offsettable address exists. */
1767 case 'o': /* offsettable */
1768 if (offsettable_nonstrict_memref_p (op))
1769 result = 1;
1770 break;
1772 case 'g':
1773 if (general_operand (op, VOIDmode))
1774 result = 1;
1775 break;
1777 case '<':
1778 case '>':
1779 /* ??? Before auto-inc-dec, auto inc/dec insns are not supposed
1780 to exist, excepting those that expand_call created. Further,
1781 on some machines which do not have generalized auto inc/dec,
1782 an inc/dec is not a memory_operand.
1784 Match any memory and hope things are resolved after reload. */
1785 incdec_ok = true;
1786 /* FALLTHRU */
1787 default:
1788 cn = lookup_constraint (constraint);
1789 switch (get_constraint_type (cn))
1791 case CT_REGISTER:
1792 if (!result
1793 && reg_class_for_constraint (cn) != NO_REGS
1794 && GET_MODE (op) != BLKmode
1795 && register_operand (op, VOIDmode))
1796 result = 1;
1797 break;
1799 case CT_CONST_INT:
1800 if (!result
1801 && CONST_INT_P (op)
1802 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
1803 result = 1;
1804 break;
1806 case CT_MEMORY:
1807 case CT_SPECIAL_MEMORY:
1808 /* Every memory operand can be reloaded to fit. */
1809 result = result || memory_operand (op, VOIDmode);
1810 break;
1812 case CT_ADDRESS:
1813 /* Every address operand can be reloaded to fit. */
1814 result = result || address_operand (op, VOIDmode);
1815 break;
1817 case CT_FIXED_FORM:
1818 result = result || constraint_satisfied_p (op, cn);
1819 break;
1821 break;
1823 len = CONSTRAINT_LEN (c, constraint);
1825 constraint++;
1826 while (--len && *constraint);
1827 if (len)
1828 return 0;
1831 /* For operands without < or > constraints reject side-effects. */
1832 if (AUTO_INC_DEC && !incdec_ok && result && MEM_P (op))
1833 switch (GET_CODE (XEXP (op, 0)))
1835 case PRE_INC:
1836 case POST_INC:
1837 case PRE_DEC:
1838 case POST_DEC:
1839 case PRE_MODIFY:
1840 case POST_MODIFY:
1841 return 0;
1842 default:
1843 break;
1846 return result;
1849 /* Given an rtx *P, if it is a sum containing an integer constant term,
1850 return the location (type rtx *) of the pointer to that constant term.
1851 Otherwise, return a null pointer. */
1853 rtx *
1854 find_constant_term_loc (rtx *p)
1856 rtx *tem;
1857 enum rtx_code code = GET_CODE (*p);
1859 /* If *P IS such a constant term, P is its location. */
1861 if (code == CONST_INT || code == SYMBOL_REF || code == LABEL_REF
1862 || code == CONST)
1863 return p;
1865 /* Otherwise, if not a sum, it has no constant term. */
1867 if (GET_CODE (*p) != PLUS)
1868 return 0;
1870 /* If one of the summands is constant, return its location. */
1872 if (XEXP (*p, 0) && CONSTANT_P (XEXP (*p, 0))
1873 && XEXP (*p, 1) && CONSTANT_P (XEXP (*p, 1)))
1874 return p;
1876 /* Otherwise, check each summand for containing a constant term. */
1878 if (XEXP (*p, 0) != 0)
1880 tem = find_constant_term_loc (&XEXP (*p, 0));
1881 if (tem != 0)
1882 return tem;
1885 if (XEXP (*p, 1) != 0)
1887 tem = find_constant_term_loc (&XEXP (*p, 1));
1888 if (tem != 0)
1889 return tem;
1892 return 0;
1895 /* Return 1 if OP is a memory reference
1896 whose address contains no side effects
1897 and remains valid after the addition
1898 of a positive integer less than the
1899 size of the object being referenced.
1901 We assume that the original address is valid and do not check it.
1903 This uses strict_memory_address_p as a subroutine, so
1904 don't use it before reload. */
1907 offsettable_memref_p (rtx op)
1909 return ((MEM_P (op))
1910 && offsettable_address_addr_space_p (1, GET_MODE (op), XEXP (op, 0),
1911 MEM_ADDR_SPACE (op)));
1914 /* Similar, but don't require a strictly valid mem ref:
1915 consider pseudo-regs valid as index or base regs. */
1918 offsettable_nonstrict_memref_p (rtx op)
1920 return ((MEM_P (op))
1921 && offsettable_address_addr_space_p (0, GET_MODE (op), XEXP (op, 0),
1922 MEM_ADDR_SPACE (op)));
1925 /* Return 1 if Y is a memory address which contains no side effects
1926 and would remain valid for address space AS after the addition of
1927 a positive integer less than the size of that mode.
1929 We assume that the original address is valid and do not check it.
1930 We do check that it is valid for narrower modes.
1932 If STRICTP is nonzero, we require a strictly valid address,
1933 for the sake of use in reload.c. */
1936 offsettable_address_addr_space_p (int strictp, machine_mode mode, rtx y,
1937 addr_space_t as)
1939 enum rtx_code ycode = GET_CODE (y);
1940 rtx z;
1941 rtx y1 = y;
1942 rtx *y2;
1943 int (*addressp) (machine_mode, rtx, addr_space_t) =
1944 (strictp ? strict_memory_address_addr_space_p
1945 : memory_address_addr_space_p);
1946 unsigned int mode_sz = GET_MODE_SIZE (mode);
1948 if (CONSTANT_ADDRESS_P (y))
1949 return 1;
1951 /* Adjusting an offsettable address involves changing to a narrower mode.
1952 Make sure that's OK. */
1954 if (mode_dependent_address_p (y, as))
1955 return 0;
1957 machine_mode address_mode = GET_MODE (y);
1958 if (address_mode == VOIDmode)
1959 address_mode = targetm.addr_space.address_mode (as);
1960 #ifdef POINTERS_EXTEND_UNSIGNED
1961 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
1962 #endif
1964 /* ??? How much offset does an offsettable BLKmode reference need?
1965 Clearly that depends on the situation in which it's being used.
1966 However, the current situation in which we test 0xffffffff is
1967 less than ideal. Caveat user. */
1968 if (mode_sz == 0)
1969 mode_sz = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
1971 /* If the expression contains a constant term,
1972 see if it remains valid when max possible offset is added. */
1974 if ((ycode == PLUS) && (y2 = find_constant_term_loc (&y1)))
1976 int good;
1978 y1 = *y2;
1979 *y2 = plus_constant (address_mode, *y2, mode_sz - 1);
1980 /* Use QImode because an odd displacement may be automatically invalid
1981 for any wider mode. But it should be valid for a single byte. */
1982 good = (*addressp) (QImode, y, as);
1984 /* In any case, restore old contents of memory. */
1985 *y2 = y1;
1986 return good;
1989 if (GET_RTX_CLASS (ycode) == RTX_AUTOINC)
1990 return 0;
1992 /* The offset added here is chosen as the maximum offset that
1993 any instruction could need to add when operating on something
1994 of the specified mode. We assume that if Y and Y+c are
1995 valid addresses then so is Y+d for all 0<d<c. adjust_address will
1996 go inside a LO_SUM here, so we do so as well. */
1997 if (GET_CODE (y) == LO_SUM
1998 && mode != BLKmode
1999 && mode_sz <= GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT)
2000 z = gen_rtx_LO_SUM (address_mode, XEXP (y, 0),
2001 plus_constant (address_mode, XEXP (y, 1),
2002 mode_sz - 1));
2003 #ifdef POINTERS_EXTEND_UNSIGNED
2004 /* Likewise for a ZERO_EXTEND from pointer_mode. */
2005 else if (POINTERS_EXTEND_UNSIGNED > 0
2006 && GET_CODE (y) == ZERO_EXTEND
2007 && GET_MODE (XEXP (y, 0)) == pointer_mode)
2008 z = gen_rtx_ZERO_EXTEND (address_mode,
2009 plus_constant (pointer_mode, XEXP (y, 0),
2010 mode_sz - 1));
2011 #endif
2012 else
2013 z = plus_constant (address_mode, y, mode_sz - 1);
2015 /* Use QImode because an odd displacement may be automatically invalid
2016 for any wider mode. But it should be valid for a single byte. */
2017 return (*addressp) (QImode, z, as);
2020 /* Return 1 if ADDR is an address-expression whose effect depends
2021 on the mode of the memory reference it is used in.
2023 ADDRSPACE is the address space associated with the address.
2025 Autoincrement addressing is a typical example of mode-dependence
2026 because the amount of the increment depends on the mode. */
2028 bool
2029 mode_dependent_address_p (rtx addr, addr_space_t addrspace)
2031 /* Auto-increment addressing with anything other than post_modify
2032 or pre_modify always introduces a mode dependency. Catch such
2033 cases now instead of deferring to the target. */
2034 if (GET_CODE (addr) == PRE_INC
2035 || GET_CODE (addr) == POST_INC
2036 || GET_CODE (addr) == PRE_DEC
2037 || GET_CODE (addr) == POST_DEC)
2038 return true;
2040 return targetm.mode_dependent_address_p (addr, addrspace);
2043 /* Return true if boolean attribute ATTR is supported. */
2045 static bool
2046 have_bool_attr (bool_attr attr)
2048 switch (attr)
2050 case BA_ENABLED:
2051 return HAVE_ATTR_enabled;
2052 case BA_PREFERRED_FOR_SIZE:
2053 return HAVE_ATTR_enabled || HAVE_ATTR_preferred_for_size;
2054 case BA_PREFERRED_FOR_SPEED:
2055 return HAVE_ATTR_enabled || HAVE_ATTR_preferred_for_speed;
2057 gcc_unreachable ();
2060 /* Return the value of ATTR for instruction INSN. */
2062 static bool
2063 get_bool_attr (rtx_insn *insn, bool_attr attr)
2065 switch (attr)
2067 case BA_ENABLED:
2068 return get_attr_enabled (insn);
2069 case BA_PREFERRED_FOR_SIZE:
2070 return get_attr_enabled (insn) && get_attr_preferred_for_size (insn);
2071 case BA_PREFERRED_FOR_SPEED:
2072 return get_attr_enabled (insn) && get_attr_preferred_for_speed (insn);
2074 gcc_unreachable ();
2077 /* Like get_bool_attr_mask, but don't use the cache. */
2079 static alternative_mask
2080 get_bool_attr_mask_uncached (rtx_insn *insn, bool_attr attr)
2082 /* Temporarily install enough information for get_attr_<foo> to assume
2083 that the insn operands are already cached. As above, the attribute
2084 mustn't depend on the values of operands, so we don't provide their
2085 real values here. */
2086 rtx_insn *old_insn = recog_data.insn;
2087 int old_alternative = which_alternative;
2089 recog_data.insn = insn;
2090 alternative_mask mask = ALL_ALTERNATIVES;
2091 int n_alternatives = insn_data[INSN_CODE (insn)].n_alternatives;
2092 for (int i = 0; i < n_alternatives; i++)
2094 which_alternative = i;
2095 if (!get_bool_attr (insn, attr))
2096 mask &= ~ALTERNATIVE_BIT (i);
2099 recog_data.insn = old_insn;
2100 which_alternative = old_alternative;
2101 return mask;
2104 /* Return the mask of operand alternatives that are allowed for INSN
2105 by boolean attribute ATTR. This mask depends only on INSN and on
2106 the current target; it does not depend on things like the values of
2107 operands. */
2109 static alternative_mask
2110 get_bool_attr_mask (rtx_insn *insn, bool_attr attr)
2112 /* Quick exit for asms and for targets that don't use these attributes. */
2113 int code = INSN_CODE (insn);
2114 if (code < 0 || !have_bool_attr (attr))
2115 return ALL_ALTERNATIVES;
2117 /* Calling get_attr_<foo> can be expensive, so cache the mask
2118 for speed. */
2119 if (!this_target_recog->x_bool_attr_masks[code][attr])
2120 this_target_recog->x_bool_attr_masks[code][attr]
2121 = get_bool_attr_mask_uncached (insn, attr);
2122 return this_target_recog->x_bool_attr_masks[code][attr];
2125 /* Return the set of alternatives of INSN that are allowed by the current
2126 target. */
2128 alternative_mask
2129 get_enabled_alternatives (rtx_insn *insn)
2131 return get_bool_attr_mask (insn, BA_ENABLED);
2134 /* Return the set of alternatives of INSN that are allowed by the current
2135 target and are preferred for the current size/speed optimization
2136 choice. */
2138 alternative_mask
2139 get_preferred_alternatives (rtx_insn *insn)
2141 if (optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn)))
2142 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SPEED);
2143 else
2144 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SIZE);
2147 /* Return the set of alternatives of INSN that are allowed by the current
2148 target and are preferred for the size/speed optimization choice
2149 associated with BB. Passing a separate BB is useful if INSN has not
2150 been emitted yet or if we are considering moving it to a different
2151 block. */
2153 alternative_mask
2154 get_preferred_alternatives (rtx_insn *insn, basic_block bb)
2156 if (optimize_bb_for_speed_p (bb))
2157 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SPEED);
2158 else
2159 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SIZE);
2162 /* Assert that the cached boolean attributes for INSN are still accurate.
2163 The backend is required to define these attributes in a way that only
2164 depends on the current target (rather than operands, compiler phase,
2165 etc.). */
2167 bool
2168 check_bool_attrs (rtx_insn *insn)
2170 int code = INSN_CODE (insn);
2171 if (code >= 0)
2172 for (int i = 0; i <= BA_LAST; ++i)
2174 enum bool_attr attr = (enum bool_attr) i;
2175 if (this_target_recog->x_bool_attr_masks[code][attr])
2176 gcc_assert (this_target_recog->x_bool_attr_masks[code][attr]
2177 == get_bool_attr_mask_uncached (insn, attr));
2179 return true;
2182 /* Like extract_insn, but save insn extracted and don't extract again, when
2183 called again for the same insn expecting that recog_data still contain the
2184 valid information. This is used primary by gen_attr infrastructure that
2185 often does extract insn again and again. */
2186 void
2187 extract_insn_cached (rtx_insn *insn)
2189 if (recog_data.insn == insn && INSN_CODE (insn) >= 0)
2190 return;
2191 extract_insn (insn);
2192 recog_data.insn = insn;
2195 /* Do uncached extract_insn, constrain_operands and complain about failures.
2196 This should be used when extracting a pre-existing constrained instruction
2197 if the caller wants to know which alternative was chosen. */
2198 void
2199 extract_constrain_insn (rtx_insn *insn)
2201 extract_insn (insn);
2202 if (!constrain_operands (reload_completed, get_enabled_alternatives (insn)))
2203 fatal_insn_not_found (insn);
2206 /* Do cached extract_insn, constrain_operands and complain about failures.
2207 Used by insn_attrtab. */
2208 void
2209 extract_constrain_insn_cached (rtx_insn *insn)
2211 extract_insn_cached (insn);
2212 if (which_alternative == -1
2213 && !constrain_operands (reload_completed,
2214 get_enabled_alternatives (insn)))
2215 fatal_insn_not_found (insn);
2218 /* Do cached constrain_operands on INSN and complain about failures. */
2220 constrain_operands_cached (rtx_insn *insn, int strict)
2222 if (which_alternative == -1)
2223 return constrain_operands (strict, get_enabled_alternatives (insn));
2224 else
2225 return 1;
2228 /* Analyze INSN and fill in recog_data. */
2230 void
2231 extract_insn (rtx_insn *insn)
2233 int i;
2234 int icode;
2235 int noperands;
2236 rtx body = PATTERN (insn);
2238 recog_data.n_operands = 0;
2239 recog_data.n_alternatives = 0;
2240 recog_data.n_dups = 0;
2241 recog_data.is_asm = false;
2243 switch (GET_CODE (body))
2245 case USE:
2246 case CLOBBER:
2247 case ASM_INPUT:
2248 case ADDR_VEC:
2249 case ADDR_DIFF_VEC:
2250 case VAR_LOCATION:
2251 case DEBUG_MARKER:
2252 return;
2254 case SET:
2255 if (GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
2256 goto asm_insn;
2257 else
2258 goto normal_insn;
2259 case PARALLEL:
2260 if ((GET_CODE (XVECEXP (body, 0, 0)) == SET
2261 && GET_CODE (SET_SRC (XVECEXP (body, 0, 0))) == ASM_OPERANDS)
2262 || GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS
2263 || GET_CODE (XVECEXP (body, 0, 0)) == ASM_INPUT)
2264 goto asm_insn;
2265 else
2266 goto normal_insn;
2267 case ASM_OPERANDS:
2268 asm_insn:
2269 recog_data.n_operands = noperands = asm_noperands (body);
2270 if (noperands >= 0)
2272 /* This insn is an `asm' with operands. */
2274 /* expand_asm_operands makes sure there aren't too many operands. */
2275 gcc_assert (noperands <= MAX_RECOG_OPERANDS);
2277 /* Now get the operand values and constraints out of the insn. */
2278 decode_asm_operands (body, recog_data.operand,
2279 recog_data.operand_loc,
2280 recog_data.constraints,
2281 recog_data.operand_mode, NULL);
2282 memset (recog_data.is_operator, 0, sizeof recog_data.is_operator);
2283 if (noperands > 0)
2285 const char *p = recog_data.constraints[0];
2286 recog_data.n_alternatives = 1;
2287 while (*p)
2288 recog_data.n_alternatives += (*p++ == ',');
2290 recog_data.is_asm = true;
2291 break;
2293 fatal_insn_not_found (insn);
2295 default:
2296 normal_insn:
2297 /* Ordinary insn: recognize it, get the operands via insn_extract
2298 and get the constraints. */
2300 icode = recog_memoized (insn);
2301 if (icode < 0)
2302 fatal_insn_not_found (insn);
2304 recog_data.n_operands = noperands = insn_data[icode].n_operands;
2305 recog_data.n_alternatives = insn_data[icode].n_alternatives;
2306 recog_data.n_dups = insn_data[icode].n_dups;
2308 insn_extract (insn);
2310 for (i = 0; i < noperands; i++)
2312 recog_data.constraints[i] = insn_data[icode].operand[i].constraint;
2313 recog_data.is_operator[i] = insn_data[icode].operand[i].is_operator;
2314 recog_data.operand_mode[i] = insn_data[icode].operand[i].mode;
2315 /* VOIDmode match_operands gets mode from their real operand. */
2316 if (recog_data.operand_mode[i] == VOIDmode)
2317 recog_data.operand_mode[i] = GET_MODE (recog_data.operand[i]);
2320 for (i = 0; i < noperands; i++)
2321 recog_data.operand_type[i]
2322 = (recog_data.constraints[i][0] == '=' ? OP_OUT
2323 : recog_data.constraints[i][0] == '+' ? OP_INOUT
2324 : OP_IN);
2326 gcc_assert (recog_data.n_alternatives <= MAX_RECOG_ALTERNATIVES);
2328 recog_data.insn = NULL;
2329 which_alternative = -1;
2332 /* Fill in OP_ALT_BASE for an instruction that has N_OPERANDS operands,
2333 N_ALTERNATIVES alternatives and constraint strings CONSTRAINTS.
2334 OP_ALT_BASE has N_ALTERNATIVES * N_OPERANDS entries and CONSTRAINTS
2335 has N_OPERANDS entries. */
2337 void
2338 preprocess_constraints (int n_operands, int n_alternatives,
2339 const char **constraints,
2340 operand_alternative *op_alt_base)
2342 for (int i = 0; i < n_operands; i++)
2344 int j;
2345 struct operand_alternative *op_alt;
2346 const char *p = constraints[i];
2348 op_alt = op_alt_base;
2350 for (j = 0; j < n_alternatives; j++, op_alt += n_operands)
2352 op_alt[i].cl = NO_REGS;
2353 op_alt[i].constraint = p;
2354 op_alt[i].matches = -1;
2355 op_alt[i].matched = -1;
2357 if (*p == '\0' || *p == ',')
2359 op_alt[i].anything_ok = 1;
2360 continue;
2363 for (;;)
2365 char c = *p;
2366 if (c == '#')
2368 c = *++p;
2369 while (c != ',' && c != '\0');
2370 if (c == ',' || c == '\0')
2372 p++;
2373 break;
2376 switch (c)
2378 case '?':
2379 op_alt[i].reject += 6;
2380 break;
2381 case '!':
2382 op_alt[i].reject += 600;
2383 break;
2384 case '&':
2385 op_alt[i].earlyclobber = 1;
2386 break;
2388 case '0': case '1': case '2': case '3': case '4':
2389 case '5': case '6': case '7': case '8': case '9':
2391 char *end;
2392 op_alt[i].matches = strtoul (p, &end, 10);
2393 op_alt[op_alt[i].matches].matched = i;
2394 p = end;
2396 continue;
2398 case 'X':
2399 op_alt[i].anything_ok = 1;
2400 break;
2402 case 'g':
2403 op_alt[i].cl =
2404 reg_class_subunion[(int) op_alt[i].cl][(int) GENERAL_REGS];
2405 break;
2407 default:
2408 enum constraint_num cn = lookup_constraint (p);
2409 enum reg_class cl;
2410 switch (get_constraint_type (cn))
2412 case CT_REGISTER:
2413 cl = reg_class_for_constraint (cn);
2414 if (cl != NO_REGS)
2415 op_alt[i].cl = reg_class_subunion[op_alt[i].cl][cl];
2416 break;
2418 case CT_CONST_INT:
2419 break;
2421 case CT_MEMORY:
2422 case CT_SPECIAL_MEMORY:
2423 op_alt[i].memory_ok = 1;
2424 break;
2426 case CT_ADDRESS:
2427 op_alt[i].is_address = 1;
2428 op_alt[i].cl
2429 = (reg_class_subunion
2430 [(int) op_alt[i].cl]
2431 [(int) base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2432 ADDRESS, SCRATCH)]);
2433 break;
2435 case CT_FIXED_FORM:
2436 break;
2438 break;
2440 p += CONSTRAINT_LEN (c, p);
2446 /* Return an array of operand_alternative instructions for
2447 instruction ICODE. */
2449 const operand_alternative *
2450 preprocess_insn_constraints (unsigned int icode)
2452 gcc_checking_assert (IN_RANGE (icode, 0, NUM_INSN_CODES - 1));
2453 if (this_target_recog->x_op_alt[icode])
2454 return this_target_recog->x_op_alt[icode];
2456 int n_operands = insn_data[icode].n_operands;
2457 if (n_operands == 0)
2458 return 0;
2459 /* Always provide at least one alternative so that which_op_alt ()
2460 works correctly. If the instruction has 0 alternatives (i.e. all
2461 constraint strings are empty) then each operand in this alternative
2462 will have anything_ok set. */
2463 int n_alternatives = MAX (insn_data[icode].n_alternatives, 1);
2464 int n_entries = n_operands * n_alternatives;
2466 operand_alternative *op_alt = XCNEWVEC (operand_alternative, n_entries);
2467 const char **constraints = XALLOCAVEC (const char *, n_operands);
2469 for (int i = 0; i < n_operands; ++i)
2470 constraints[i] = insn_data[icode].operand[i].constraint;
2471 preprocess_constraints (n_operands, n_alternatives, constraints, op_alt);
2473 this_target_recog->x_op_alt[icode] = op_alt;
2474 return op_alt;
2477 /* After calling extract_insn, you can use this function to extract some
2478 information from the constraint strings into a more usable form.
2479 The collected data is stored in recog_op_alt. */
2481 void
2482 preprocess_constraints (rtx_insn *insn)
2484 int icode = INSN_CODE (insn);
2485 if (icode >= 0)
2486 recog_op_alt = preprocess_insn_constraints (icode);
2487 else
2489 int n_operands = recog_data.n_operands;
2490 int n_alternatives = recog_data.n_alternatives;
2491 int n_entries = n_operands * n_alternatives;
2492 memset (asm_op_alt, 0, n_entries * sizeof (operand_alternative));
2493 preprocess_constraints (n_operands, n_alternatives,
2494 recog_data.constraints, asm_op_alt);
2495 recog_op_alt = asm_op_alt;
2499 /* Check the operands of an insn against the insn's operand constraints
2500 and return 1 if they match any of the alternatives in ALTERNATIVES.
2502 The information about the insn's operands, constraints, operand modes
2503 etc. is obtained from the global variables set up by extract_insn.
2505 WHICH_ALTERNATIVE is set to a number which indicates which
2506 alternative of constraints was matched: 0 for the first alternative,
2507 1 for the next, etc.
2509 In addition, when two operands are required to match
2510 and it happens that the output operand is (reg) while the
2511 input operand is --(reg) or ++(reg) (a pre-inc or pre-dec),
2512 make the output operand look like the input.
2513 This is because the output operand is the one the template will print.
2515 This is used in final, just before printing the assembler code and by
2516 the routines that determine an insn's attribute.
2518 If STRICT is a positive nonzero value, it means that we have been
2519 called after reload has been completed. In that case, we must
2520 do all checks strictly. If it is zero, it means that we have been called
2521 before reload has completed. In that case, we first try to see if we can
2522 find an alternative that matches strictly. If not, we try again, this
2523 time assuming that reload will fix up the insn. This provides a "best
2524 guess" for the alternative and is used to compute attributes of insns prior
2525 to reload. A negative value of STRICT is used for this internal call. */
2527 struct funny_match
2529 int this_op, other;
2533 constrain_operands (int strict, alternative_mask alternatives)
2535 const char *constraints[MAX_RECOG_OPERANDS];
2536 int matching_operands[MAX_RECOG_OPERANDS];
2537 int earlyclobber[MAX_RECOG_OPERANDS];
2538 int c;
2540 struct funny_match funny_match[MAX_RECOG_OPERANDS];
2541 int funny_match_index;
2543 which_alternative = 0;
2544 if (recog_data.n_operands == 0 || recog_data.n_alternatives == 0)
2545 return 1;
2547 for (c = 0; c < recog_data.n_operands; c++)
2549 constraints[c] = recog_data.constraints[c];
2550 matching_operands[c] = -1;
2555 int seen_earlyclobber_at = -1;
2556 int opno;
2557 int lose = 0;
2558 funny_match_index = 0;
2560 if (!TEST_BIT (alternatives, which_alternative))
2562 int i;
2564 for (i = 0; i < recog_data.n_operands; i++)
2565 constraints[i] = skip_alternative (constraints[i]);
2567 which_alternative++;
2568 continue;
2571 for (opno = 0; opno < recog_data.n_operands; opno++)
2573 rtx op = recog_data.operand[opno];
2574 machine_mode mode = GET_MODE (op);
2575 const char *p = constraints[opno];
2576 int offset = 0;
2577 int win = 0;
2578 int val;
2579 int len;
2581 earlyclobber[opno] = 0;
2583 /* A unary operator may be accepted by the predicate, but it
2584 is irrelevant for matching constraints. */
2585 if (UNARY_P (op))
2586 op = XEXP (op, 0);
2588 if (GET_CODE (op) == SUBREG)
2590 if (REG_P (SUBREG_REG (op))
2591 && REGNO (SUBREG_REG (op)) < FIRST_PSEUDO_REGISTER)
2592 offset = subreg_regno_offset (REGNO (SUBREG_REG (op)),
2593 GET_MODE (SUBREG_REG (op)),
2594 SUBREG_BYTE (op),
2595 GET_MODE (op));
2596 op = SUBREG_REG (op);
2599 /* An empty constraint or empty alternative
2600 allows anything which matched the pattern. */
2601 if (*p == 0 || *p == ',')
2602 win = 1;
2605 switch (c = *p, len = CONSTRAINT_LEN (c, p), c)
2607 case '\0':
2608 len = 0;
2609 break;
2610 case ',':
2611 c = '\0';
2612 break;
2614 case '#':
2615 /* Ignore rest of this alternative as far as
2616 constraint checking is concerned. */
2618 p++;
2619 while (*p && *p != ',');
2620 len = 0;
2621 break;
2623 case '&':
2624 earlyclobber[opno] = 1;
2625 if (seen_earlyclobber_at < 0)
2626 seen_earlyclobber_at = opno;
2627 break;
2629 case '0': case '1': case '2': case '3': case '4':
2630 case '5': case '6': case '7': case '8': case '9':
2632 /* This operand must be the same as a previous one.
2633 This kind of constraint is used for instructions such
2634 as add when they take only two operands.
2636 Note that the lower-numbered operand is passed first.
2638 If we are not testing strictly, assume that this
2639 constraint will be satisfied. */
2641 char *end;
2642 int match;
2644 match = strtoul (p, &end, 10);
2645 p = end;
2647 if (strict < 0)
2648 val = 1;
2649 else
2651 rtx op1 = recog_data.operand[match];
2652 rtx op2 = recog_data.operand[opno];
2654 /* A unary operator may be accepted by the predicate,
2655 but it is irrelevant for matching constraints. */
2656 if (UNARY_P (op1))
2657 op1 = XEXP (op1, 0);
2658 if (UNARY_P (op2))
2659 op2 = XEXP (op2, 0);
2661 val = operands_match_p (op1, op2);
2664 matching_operands[opno] = match;
2665 matching_operands[match] = opno;
2667 if (val != 0)
2668 win = 1;
2670 /* If output is *x and input is *--x, arrange later
2671 to change the output to *--x as well, since the
2672 output op is the one that will be printed. */
2673 if (val == 2 && strict > 0)
2675 funny_match[funny_match_index].this_op = opno;
2676 funny_match[funny_match_index++].other = match;
2679 len = 0;
2680 break;
2682 case 'p':
2683 /* p is used for address_operands. When we are called by
2684 gen_reload, no one will have checked that the address is
2685 strictly valid, i.e., that all pseudos requiring hard regs
2686 have gotten them. */
2687 if (strict <= 0
2688 || (strict_memory_address_p (recog_data.operand_mode[opno],
2689 op)))
2690 win = 1;
2691 break;
2693 /* No need to check general_operand again;
2694 it was done in insn-recog.c. Well, except that reload
2695 doesn't check the validity of its replacements, but
2696 that should only matter when there's a bug. */
2697 case 'g':
2698 /* Anything goes unless it is a REG and really has a hard reg
2699 but the hard reg is not in the class GENERAL_REGS. */
2700 if (REG_P (op))
2702 if (strict < 0
2703 || GENERAL_REGS == ALL_REGS
2704 || (reload_in_progress
2705 && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2706 || reg_fits_class_p (op, GENERAL_REGS, offset, mode))
2707 win = 1;
2709 else if (strict < 0 || general_operand (op, mode))
2710 win = 1;
2711 break;
2713 default:
2715 enum constraint_num cn = lookup_constraint (p);
2716 enum reg_class cl = reg_class_for_constraint (cn);
2717 if (cl != NO_REGS)
2719 if (strict < 0
2720 || (strict == 0
2721 && REG_P (op)
2722 && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2723 || (strict == 0 && GET_CODE (op) == SCRATCH)
2724 || (REG_P (op)
2725 && reg_fits_class_p (op, cl, offset, mode)))
2726 win = 1;
2729 else if (constraint_satisfied_p (op, cn))
2730 win = 1;
2732 else if (insn_extra_memory_constraint (cn)
2733 /* Every memory operand can be reloaded to fit. */
2734 && ((strict < 0 && MEM_P (op))
2735 /* Before reload, accept what reload can turn
2736 into a mem. */
2737 || (strict < 0 && CONSTANT_P (op))
2738 /* Before reload, accept a pseudo,
2739 since LRA can turn it into a mem. */
2740 || (strict < 0 && targetm.lra_p () && REG_P (op)
2741 && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2742 /* During reload, accept a pseudo */
2743 || (reload_in_progress && REG_P (op)
2744 && REGNO (op) >= FIRST_PSEUDO_REGISTER)))
2745 win = 1;
2746 else if (insn_extra_address_constraint (cn)
2747 /* Every address operand can be reloaded to fit. */
2748 && strict < 0)
2749 win = 1;
2750 /* Cater to architectures like IA-64 that define extra memory
2751 constraints without using define_memory_constraint. */
2752 else if (reload_in_progress
2753 && REG_P (op)
2754 && REGNO (op) >= FIRST_PSEUDO_REGISTER
2755 && reg_renumber[REGNO (op)] < 0
2756 && reg_equiv_mem (REGNO (op)) != 0
2757 && constraint_satisfied_p
2758 (reg_equiv_mem (REGNO (op)), cn))
2759 win = 1;
2760 break;
2763 while (p += len, c);
2765 constraints[opno] = p;
2766 /* If this operand did not win somehow,
2767 this alternative loses. */
2768 if (! win)
2769 lose = 1;
2771 /* This alternative won; the operands are ok.
2772 Change whichever operands this alternative says to change. */
2773 if (! lose)
2775 int opno, eopno;
2777 /* See if any earlyclobber operand conflicts with some other
2778 operand. */
2780 if (strict > 0 && seen_earlyclobber_at >= 0)
2781 for (eopno = seen_earlyclobber_at;
2782 eopno < recog_data.n_operands;
2783 eopno++)
2784 /* Ignore earlyclobber operands now in memory,
2785 because we would often report failure when we have
2786 two memory operands, one of which was formerly a REG. */
2787 if (earlyclobber[eopno]
2788 && REG_P (recog_data.operand[eopno]))
2789 for (opno = 0; opno < recog_data.n_operands; opno++)
2790 if ((MEM_P (recog_data.operand[opno])
2791 || recog_data.operand_type[opno] != OP_OUT)
2792 && opno != eopno
2793 /* Ignore things like match_operator operands. */
2794 && *recog_data.constraints[opno] != 0
2795 && ! (matching_operands[opno] == eopno
2796 && operands_match_p (recog_data.operand[opno],
2797 recog_data.operand[eopno]))
2798 && ! safe_from_earlyclobber (recog_data.operand[opno],
2799 recog_data.operand[eopno]))
2800 lose = 1;
2802 if (! lose)
2804 while (--funny_match_index >= 0)
2806 recog_data.operand[funny_match[funny_match_index].other]
2807 = recog_data.operand[funny_match[funny_match_index].this_op];
2810 /* For operands without < or > constraints reject side-effects. */
2811 if (AUTO_INC_DEC && recog_data.is_asm)
2813 for (opno = 0; opno < recog_data.n_operands; opno++)
2814 if (MEM_P (recog_data.operand[opno]))
2815 switch (GET_CODE (XEXP (recog_data.operand[opno], 0)))
2817 case PRE_INC:
2818 case POST_INC:
2819 case PRE_DEC:
2820 case POST_DEC:
2821 case PRE_MODIFY:
2822 case POST_MODIFY:
2823 if (strchr (recog_data.constraints[opno], '<') == NULL
2824 && strchr (recog_data.constraints[opno], '>')
2825 == NULL)
2826 return 0;
2827 break;
2828 default:
2829 break;
2833 return 1;
2837 which_alternative++;
2839 while (which_alternative < recog_data.n_alternatives);
2841 which_alternative = -1;
2842 /* If we are about to reject this, but we are not to test strictly,
2843 try a very loose test. Only return failure if it fails also. */
2844 if (strict == 0)
2845 return constrain_operands (-1, alternatives);
2846 else
2847 return 0;
2850 /* Return true iff OPERAND (assumed to be a REG rtx)
2851 is a hard reg in class CLASS when its regno is offset by OFFSET
2852 and changed to mode MODE.
2853 If REG occupies multiple hard regs, all of them must be in CLASS. */
2855 bool
2856 reg_fits_class_p (const_rtx operand, reg_class_t cl, int offset,
2857 machine_mode mode)
2859 unsigned int regno = REGNO (operand);
2861 if (cl == NO_REGS)
2862 return false;
2864 /* Regno must not be a pseudo register. Offset may be negative. */
2865 return (HARD_REGISTER_NUM_P (regno)
2866 && HARD_REGISTER_NUM_P (regno + offset)
2867 && in_hard_reg_set_p (reg_class_contents[(int) cl], mode,
2868 regno + offset));
2871 /* Split single instruction. Helper function for split_all_insns and
2872 split_all_insns_noflow. Return last insn in the sequence if successful,
2873 or NULL if unsuccessful. */
2875 static rtx_insn *
2876 split_insn (rtx_insn *insn)
2878 /* Split insns here to get max fine-grain parallelism. */
2879 rtx_insn *first = PREV_INSN (insn);
2880 rtx_insn *last = try_split (PATTERN (insn), insn, 1);
2881 rtx insn_set, last_set, note;
2883 if (last == insn)
2884 return NULL;
2886 /* If the original instruction was a single set that was known to be
2887 equivalent to a constant, see if we can say the same about the last
2888 instruction in the split sequence. The two instructions must set
2889 the same destination. */
2890 insn_set = single_set (insn);
2891 if (insn_set)
2893 last_set = single_set (last);
2894 if (last_set && rtx_equal_p (SET_DEST (last_set), SET_DEST (insn_set)))
2896 note = find_reg_equal_equiv_note (insn);
2897 if (note && CONSTANT_P (XEXP (note, 0)))
2898 set_unique_reg_note (last, REG_EQUAL, XEXP (note, 0));
2899 else if (CONSTANT_P (SET_SRC (insn_set)))
2900 set_unique_reg_note (last, REG_EQUAL,
2901 copy_rtx (SET_SRC (insn_set)));
2905 /* try_split returns the NOTE that INSN became. */
2906 SET_INSN_DELETED (insn);
2908 /* ??? Coddle to md files that generate subregs in post-reload
2909 splitters instead of computing the proper hard register. */
2910 if (reload_completed && first != last)
2912 first = NEXT_INSN (first);
2913 for (;;)
2915 if (INSN_P (first))
2916 cleanup_subreg_operands (first);
2917 if (first == last)
2918 break;
2919 first = NEXT_INSN (first);
2923 return last;
2926 /* Split all insns in the function. If UPD_LIFE, update life info after. */
2928 void
2929 split_all_insns (void)
2931 bool changed;
2932 basic_block bb;
2934 auto_sbitmap blocks (last_basic_block_for_fn (cfun));
2935 bitmap_clear (blocks);
2936 changed = false;
2938 FOR_EACH_BB_REVERSE_FN (bb, cfun)
2940 rtx_insn *insn, *next;
2941 bool finish = false;
2943 rtl_profile_for_bb (bb);
2944 for (insn = BB_HEAD (bb); !finish ; insn = next)
2946 /* Can't use `next_real_insn' because that might go across
2947 CODE_LABELS and short-out basic blocks. */
2948 next = NEXT_INSN (insn);
2949 finish = (insn == BB_END (bb));
2950 if (INSN_P (insn))
2952 rtx set = single_set (insn);
2954 /* Don't split no-op move insns. These should silently
2955 disappear later in final. Splitting such insns would
2956 break the code that handles LIBCALL blocks. */
2957 if (set && set_noop_p (set))
2959 /* Nops get in the way while scheduling, so delete them
2960 now if register allocation has already been done. It
2961 is too risky to try to do this before register
2962 allocation, and there are unlikely to be very many
2963 nops then anyways. */
2964 if (reload_completed)
2965 delete_insn_and_edges (insn);
2967 else
2969 if (split_insn (insn))
2971 bitmap_set_bit (blocks, bb->index);
2972 changed = true;
2979 default_rtl_profile ();
2980 if (changed)
2981 find_many_sub_basic_blocks (blocks);
2983 checking_verify_flow_info ();
2986 /* Same as split_all_insns, but do not expect CFG to be available.
2987 Used by machine dependent reorg passes. */
2989 unsigned int
2990 split_all_insns_noflow (void)
2992 rtx_insn *next, *insn;
2994 for (insn = get_insns (); insn; insn = next)
2996 next = NEXT_INSN (insn);
2997 if (INSN_P (insn))
2999 /* Don't split no-op move insns. These should silently
3000 disappear later in final. Splitting such insns would
3001 break the code that handles LIBCALL blocks. */
3002 rtx set = single_set (insn);
3003 if (set && set_noop_p (set))
3005 /* Nops get in the way while scheduling, so delete them
3006 now if register allocation has already been done. It
3007 is too risky to try to do this before register
3008 allocation, and there are unlikely to be very many
3009 nops then anyways.
3011 ??? Should we use delete_insn when the CFG isn't valid? */
3012 if (reload_completed)
3013 delete_insn_and_edges (insn);
3015 else
3016 split_insn (insn);
3019 return 0;
3022 struct peep2_insn_data
3024 rtx_insn *insn;
3025 regset live_before;
3028 static struct peep2_insn_data peep2_insn_data[MAX_INSNS_PER_PEEP2 + 1];
3029 static int peep2_current;
3031 static bool peep2_do_rebuild_jump_labels;
3032 static bool peep2_do_cleanup_cfg;
3034 /* The number of instructions available to match a peep2. */
3035 int peep2_current_count;
3037 /* A marker indicating the last insn of the block. The live_before regset
3038 for this element is correct, indicating DF_LIVE_OUT for the block. */
3039 #define PEEP2_EOB invalid_insn_rtx
3041 /* Wrap N to fit into the peep2_insn_data buffer. */
3043 static int
3044 peep2_buf_position (int n)
3046 if (n >= MAX_INSNS_PER_PEEP2 + 1)
3047 n -= MAX_INSNS_PER_PEEP2 + 1;
3048 return n;
3051 /* Return the Nth non-note insn after `current', or return NULL_RTX if it
3052 does not exist. Used by the recognizer to find the next insn to match
3053 in a multi-insn pattern. */
3055 rtx_insn *
3056 peep2_next_insn (int n)
3058 gcc_assert (n <= peep2_current_count);
3060 n = peep2_buf_position (peep2_current + n);
3062 return peep2_insn_data[n].insn;
3065 /* Return true if REGNO is dead before the Nth non-note insn
3066 after `current'. */
3069 peep2_regno_dead_p (int ofs, int regno)
3071 gcc_assert (ofs < MAX_INSNS_PER_PEEP2 + 1);
3073 ofs = peep2_buf_position (peep2_current + ofs);
3075 gcc_assert (peep2_insn_data[ofs].insn != NULL_RTX);
3077 return ! REGNO_REG_SET_P (peep2_insn_data[ofs].live_before, regno);
3080 /* Similarly for a REG. */
3083 peep2_reg_dead_p (int ofs, rtx reg)
3085 gcc_assert (ofs < MAX_INSNS_PER_PEEP2 + 1);
3087 ofs = peep2_buf_position (peep2_current + ofs);
3089 gcc_assert (peep2_insn_data[ofs].insn != NULL_RTX);
3091 unsigned int end_regno = END_REGNO (reg);
3092 for (unsigned int regno = REGNO (reg); regno < end_regno; ++regno)
3093 if (REGNO_REG_SET_P (peep2_insn_data[ofs].live_before, regno))
3094 return 0;
3095 return 1;
3098 /* Regno offset to be used in the register search. */
3099 static int search_ofs;
3101 /* Try to find a hard register of mode MODE, matching the register class in
3102 CLASS_STR, which is available at the beginning of insn CURRENT_INSN and
3103 remains available until the end of LAST_INSN. LAST_INSN may be NULL_RTX,
3104 in which case the only condition is that the register must be available
3105 before CURRENT_INSN.
3106 Registers that already have bits set in REG_SET will not be considered.
3108 If an appropriate register is available, it will be returned and the
3109 corresponding bit(s) in REG_SET will be set; otherwise, NULL_RTX is
3110 returned. */
3113 peep2_find_free_register (int from, int to, const char *class_str,
3114 machine_mode mode, HARD_REG_SET *reg_set)
3116 enum reg_class cl;
3117 HARD_REG_SET live;
3118 df_ref def;
3119 int i;
3121 gcc_assert (from < MAX_INSNS_PER_PEEP2 + 1);
3122 gcc_assert (to < MAX_INSNS_PER_PEEP2 + 1);
3124 from = peep2_buf_position (peep2_current + from);
3125 to = peep2_buf_position (peep2_current + to);
3127 gcc_assert (peep2_insn_data[from].insn != NULL_RTX);
3128 REG_SET_TO_HARD_REG_SET (live, peep2_insn_data[from].live_before);
3130 while (from != to)
3132 gcc_assert (peep2_insn_data[from].insn != NULL_RTX);
3134 /* Don't use registers set or clobbered by the insn. */
3135 FOR_EACH_INSN_DEF (def, peep2_insn_data[from].insn)
3136 SET_HARD_REG_BIT (live, DF_REF_REGNO (def));
3138 from = peep2_buf_position (from + 1);
3141 cl = reg_class_for_constraint (lookup_constraint (class_str));
3143 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3145 int raw_regno, regno, success, j;
3147 /* Distribute the free registers as much as possible. */
3148 raw_regno = search_ofs + i;
3149 if (raw_regno >= FIRST_PSEUDO_REGISTER)
3150 raw_regno -= FIRST_PSEUDO_REGISTER;
3151 #ifdef REG_ALLOC_ORDER
3152 regno = reg_alloc_order[raw_regno];
3153 #else
3154 regno = raw_regno;
3155 #endif
3157 /* Can it support the mode we need? */
3158 if (!targetm.hard_regno_mode_ok (regno, mode))
3159 continue;
3161 success = 1;
3162 for (j = 0; success && j < hard_regno_nregs (regno, mode); j++)
3164 /* Don't allocate fixed registers. */
3165 if (fixed_regs[regno + j])
3167 success = 0;
3168 break;
3170 /* Don't allocate global registers. */
3171 if (global_regs[regno + j])
3173 success = 0;
3174 break;
3176 /* Make sure the register is of the right class. */
3177 if (! TEST_HARD_REG_BIT (reg_class_contents[cl], regno + j))
3179 success = 0;
3180 break;
3182 /* And that we don't create an extra save/restore. */
3183 if (! call_used_regs[regno + j] && ! df_regs_ever_live_p (regno + j))
3185 success = 0;
3186 break;
3189 if (! targetm.hard_regno_scratch_ok (regno + j))
3191 success = 0;
3192 break;
3195 /* And we don't clobber traceback for noreturn functions. */
3196 if ((regno + j == FRAME_POINTER_REGNUM
3197 || regno + j == HARD_FRAME_POINTER_REGNUM)
3198 && (! reload_completed || frame_pointer_needed))
3200 success = 0;
3201 break;
3204 if (TEST_HARD_REG_BIT (*reg_set, regno + j)
3205 || TEST_HARD_REG_BIT (live, regno + j))
3207 success = 0;
3208 break;
3212 if (success)
3214 add_to_hard_reg_set (reg_set, mode, regno);
3216 /* Start the next search with the next register. */
3217 if (++raw_regno >= FIRST_PSEUDO_REGISTER)
3218 raw_regno = 0;
3219 search_ofs = raw_regno;
3221 return gen_rtx_REG (mode, regno);
3225 search_ofs = 0;
3226 return NULL_RTX;
3229 /* Forget all currently tracked instructions, only remember current
3230 LIVE regset. */
3232 static void
3233 peep2_reinit_state (regset live)
3235 int i;
3237 /* Indicate that all slots except the last holds invalid data. */
3238 for (i = 0; i < MAX_INSNS_PER_PEEP2; ++i)
3239 peep2_insn_data[i].insn = NULL;
3240 peep2_current_count = 0;
3242 /* Indicate that the last slot contains live_after data. */
3243 peep2_insn_data[MAX_INSNS_PER_PEEP2].insn = PEEP2_EOB;
3244 peep2_current = MAX_INSNS_PER_PEEP2;
3246 COPY_REG_SET (peep2_insn_data[MAX_INSNS_PER_PEEP2].live_before, live);
3249 /* While scanning basic block BB, we found a match of length MATCH_LEN,
3250 starting at INSN. Perform the replacement, removing the old insns and
3251 replacing them with ATTEMPT. Returns the last insn emitted, or NULL
3252 if the replacement is rejected. */
3254 static rtx_insn *
3255 peep2_attempt (basic_block bb, rtx_insn *insn, int match_len, rtx_insn *attempt)
3257 int i;
3258 rtx_insn *last, *before_try, *x;
3259 rtx eh_note, as_note;
3260 rtx_insn *old_insn;
3261 rtx_insn *new_insn;
3262 bool was_call = false;
3264 /* If we are splitting an RTX_FRAME_RELATED_P insn, do not allow it to
3265 match more than one insn, or to be split into more than one insn. */
3266 old_insn = peep2_insn_data[peep2_current].insn;
3267 if (RTX_FRAME_RELATED_P (old_insn))
3269 bool any_note = false;
3270 rtx note;
3272 if (match_len != 0)
3273 return NULL;
3275 /* Look for one "active" insn. I.e. ignore any "clobber" insns that
3276 may be in the stream for the purpose of register allocation. */
3277 if (active_insn_p (attempt))
3278 new_insn = attempt;
3279 else
3280 new_insn = next_active_insn (attempt);
3281 if (next_active_insn (new_insn))
3282 return NULL;
3284 /* We have a 1-1 replacement. Copy over any frame-related info. */
3285 RTX_FRAME_RELATED_P (new_insn) = 1;
3287 /* Allow the backend to fill in a note during the split. */
3288 for (note = REG_NOTES (new_insn); note ; note = XEXP (note, 1))
3289 switch (REG_NOTE_KIND (note))
3291 case REG_FRAME_RELATED_EXPR:
3292 case REG_CFA_DEF_CFA:
3293 case REG_CFA_ADJUST_CFA:
3294 case REG_CFA_OFFSET:
3295 case REG_CFA_REGISTER:
3296 case REG_CFA_EXPRESSION:
3297 case REG_CFA_RESTORE:
3298 case REG_CFA_SET_VDRAP:
3299 any_note = true;
3300 break;
3301 default:
3302 break;
3305 /* If the backend didn't supply a note, copy one over. */
3306 if (!any_note)
3307 for (note = REG_NOTES (old_insn); note ; note = XEXP (note, 1))
3308 switch (REG_NOTE_KIND (note))
3310 case REG_FRAME_RELATED_EXPR:
3311 case REG_CFA_DEF_CFA:
3312 case REG_CFA_ADJUST_CFA:
3313 case REG_CFA_OFFSET:
3314 case REG_CFA_REGISTER:
3315 case REG_CFA_EXPRESSION:
3316 case REG_CFA_RESTORE:
3317 case REG_CFA_SET_VDRAP:
3318 add_reg_note (new_insn, REG_NOTE_KIND (note), XEXP (note, 0));
3319 any_note = true;
3320 break;
3321 default:
3322 break;
3325 /* If there still isn't a note, make sure the unwind info sees the
3326 same expression as before the split. */
3327 if (!any_note)
3329 rtx old_set, new_set;
3331 /* The old insn had better have been simple, or annotated. */
3332 old_set = single_set (old_insn);
3333 gcc_assert (old_set != NULL);
3335 new_set = single_set (new_insn);
3336 if (!new_set || !rtx_equal_p (new_set, old_set))
3337 add_reg_note (new_insn, REG_FRAME_RELATED_EXPR, old_set);
3340 /* Copy prologue/epilogue status. This is required in order to keep
3341 proper placement of EPILOGUE_BEG and the DW_CFA_remember_state. */
3342 maybe_copy_prologue_epilogue_insn (old_insn, new_insn);
3345 /* If we are splitting a CALL_INSN, look for the CALL_INSN
3346 in SEQ and copy our CALL_INSN_FUNCTION_USAGE and other
3347 cfg-related call notes. */
3348 for (i = 0; i <= match_len; ++i)
3350 int j;
3351 rtx note;
3353 j = peep2_buf_position (peep2_current + i);
3354 old_insn = peep2_insn_data[j].insn;
3355 if (!CALL_P (old_insn))
3356 continue;
3357 was_call = true;
3359 new_insn = attempt;
3360 while (new_insn != NULL_RTX)
3362 if (CALL_P (new_insn))
3363 break;
3364 new_insn = NEXT_INSN (new_insn);
3367 gcc_assert (new_insn != NULL_RTX);
3369 CALL_INSN_FUNCTION_USAGE (new_insn)
3370 = CALL_INSN_FUNCTION_USAGE (old_insn);
3371 SIBLING_CALL_P (new_insn) = SIBLING_CALL_P (old_insn);
3373 for (note = REG_NOTES (old_insn);
3374 note;
3375 note = XEXP (note, 1))
3376 switch (REG_NOTE_KIND (note))
3378 case REG_NORETURN:
3379 case REG_SETJMP:
3380 case REG_TM:
3381 case REG_CALL_NOCF_CHECK:
3382 add_reg_note (new_insn, REG_NOTE_KIND (note),
3383 XEXP (note, 0));
3384 break;
3385 default:
3386 /* Discard all other reg notes. */
3387 break;
3390 /* Croak if there is another call in the sequence. */
3391 while (++i <= match_len)
3393 j = peep2_buf_position (peep2_current + i);
3394 old_insn = peep2_insn_data[j].insn;
3395 gcc_assert (!CALL_P (old_insn));
3397 break;
3400 /* If we matched any instruction that had a REG_ARGS_SIZE, then
3401 move those notes over to the new sequence. */
3402 as_note = NULL;
3403 for (i = match_len; i >= 0; --i)
3405 int j = peep2_buf_position (peep2_current + i);
3406 old_insn = peep2_insn_data[j].insn;
3408 as_note = find_reg_note (old_insn, REG_ARGS_SIZE, NULL);
3409 if (as_note)
3410 break;
3413 i = peep2_buf_position (peep2_current + match_len);
3414 eh_note = find_reg_note (peep2_insn_data[i].insn, REG_EH_REGION, NULL_RTX);
3416 /* Replace the old sequence with the new. */
3417 rtx_insn *peepinsn = peep2_insn_data[i].insn;
3418 last = emit_insn_after_setloc (attempt,
3419 peep2_insn_data[i].insn,
3420 INSN_LOCATION (peepinsn));
3421 before_try = PREV_INSN (insn);
3422 delete_insn_chain (insn, peep2_insn_data[i].insn, false);
3424 /* Re-insert the EH_REGION notes. */
3425 if (eh_note || (was_call && nonlocal_goto_handler_labels))
3427 edge eh_edge;
3428 edge_iterator ei;
3430 FOR_EACH_EDGE (eh_edge, ei, bb->succs)
3431 if (eh_edge->flags & (EDGE_EH | EDGE_ABNORMAL_CALL))
3432 break;
3434 if (eh_note)
3435 copy_reg_eh_region_note_backward (eh_note, last, before_try);
3437 if (eh_edge)
3438 for (x = last; x != before_try; x = PREV_INSN (x))
3439 if (x != BB_END (bb)
3440 && (can_throw_internal (x)
3441 || can_nonlocal_goto (x)))
3443 edge nfte, nehe;
3444 int flags;
3446 nfte = split_block (bb, x);
3447 flags = (eh_edge->flags
3448 & (EDGE_EH | EDGE_ABNORMAL));
3449 if (CALL_P (x))
3450 flags |= EDGE_ABNORMAL_CALL;
3451 nehe = make_edge (nfte->src, eh_edge->dest,
3452 flags);
3454 nehe->probability = eh_edge->probability;
3455 nfte->probability = nehe->probability.invert ();
3457 peep2_do_cleanup_cfg |= purge_dead_edges (nfte->dest);
3458 bb = nfte->src;
3459 eh_edge = nehe;
3462 /* Converting possibly trapping insn to non-trapping is
3463 possible. Zap dummy outgoing edges. */
3464 peep2_do_cleanup_cfg |= purge_dead_edges (bb);
3467 /* Re-insert the ARGS_SIZE notes. */
3468 if (as_note)
3469 fixup_args_size_notes (before_try, last, get_args_size (as_note));
3471 /* If we generated a jump instruction, it won't have
3472 JUMP_LABEL set. Recompute after we're done. */
3473 for (x = last; x != before_try; x = PREV_INSN (x))
3474 if (JUMP_P (x))
3476 peep2_do_rebuild_jump_labels = true;
3477 break;
3480 return last;
3483 /* After performing a replacement in basic block BB, fix up the life
3484 information in our buffer. LAST is the last of the insns that we
3485 emitted as a replacement. PREV is the insn before the start of
3486 the replacement. MATCH_LEN is the number of instructions that were
3487 matched, and which now need to be replaced in the buffer. */
3489 static void
3490 peep2_update_life (basic_block bb, int match_len, rtx_insn *last,
3491 rtx_insn *prev)
3493 int i = peep2_buf_position (peep2_current + match_len + 1);
3494 rtx_insn *x;
3495 regset_head live;
3497 INIT_REG_SET (&live);
3498 COPY_REG_SET (&live, peep2_insn_data[i].live_before);
3500 gcc_assert (peep2_current_count >= match_len + 1);
3501 peep2_current_count -= match_len + 1;
3503 x = last;
3506 if (INSN_P (x))
3508 df_insn_rescan (x);
3509 if (peep2_current_count < MAX_INSNS_PER_PEEP2)
3511 peep2_current_count++;
3512 if (--i < 0)
3513 i = MAX_INSNS_PER_PEEP2;
3514 peep2_insn_data[i].insn = x;
3515 df_simulate_one_insn_backwards (bb, x, &live);
3516 COPY_REG_SET (peep2_insn_data[i].live_before, &live);
3519 x = PREV_INSN (x);
3521 while (x != prev);
3522 CLEAR_REG_SET (&live);
3524 peep2_current = i;
3527 /* Add INSN, which is in BB, at the end of the peep2 insn buffer if possible.
3528 Return true if we added it, false otherwise. The caller will try to match
3529 peepholes against the buffer if we return false; otherwise it will try to
3530 add more instructions to the buffer. */
3532 static bool
3533 peep2_fill_buffer (basic_block bb, rtx_insn *insn, regset live)
3535 int pos;
3537 /* Once we have filled the maximum number of insns the buffer can hold,
3538 allow the caller to match the insns against peepholes. We wait until
3539 the buffer is full in case the target has similar peepholes of different
3540 length; we always want to match the longest if possible. */
3541 if (peep2_current_count == MAX_INSNS_PER_PEEP2)
3542 return false;
3544 /* If an insn has RTX_FRAME_RELATED_P set, do not allow it to be matched with
3545 any other pattern, lest it change the semantics of the frame info. */
3546 if (RTX_FRAME_RELATED_P (insn))
3548 /* Let the buffer drain first. */
3549 if (peep2_current_count > 0)
3550 return false;
3551 /* Now the insn will be the only thing in the buffer. */
3554 pos = peep2_buf_position (peep2_current + peep2_current_count);
3555 peep2_insn_data[pos].insn = insn;
3556 COPY_REG_SET (peep2_insn_data[pos].live_before, live);
3557 peep2_current_count++;
3559 df_simulate_one_insn_forwards (bb, insn, live);
3560 return true;
3563 /* Perform the peephole2 optimization pass. */
3565 static void
3566 peephole2_optimize (void)
3568 rtx_insn *insn;
3569 bitmap live;
3570 int i;
3571 basic_block bb;
3573 peep2_do_cleanup_cfg = false;
3574 peep2_do_rebuild_jump_labels = false;
3576 df_set_flags (DF_LR_RUN_DCE);
3577 df_note_add_problem ();
3578 df_analyze ();
3580 /* Initialize the regsets we're going to use. */
3581 for (i = 0; i < MAX_INSNS_PER_PEEP2 + 1; ++i)
3582 peep2_insn_data[i].live_before = BITMAP_ALLOC (&reg_obstack);
3583 search_ofs = 0;
3584 live = BITMAP_ALLOC (&reg_obstack);
3586 FOR_EACH_BB_REVERSE_FN (bb, cfun)
3588 bool past_end = false;
3589 int pos;
3591 rtl_profile_for_bb (bb);
3593 /* Start up propagation. */
3594 bitmap_copy (live, DF_LR_IN (bb));
3595 df_simulate_initialize_forwards (bb, live);
3596 peep2_reinit_state (live);
3598 insn = BB_HEAD (bb);
3599 for (;;)
3601 rtx_insn *attempt, *head;
3602 int match_len;
3604 if (!past_end && !NONDEBUG_INSN_P (insn))
3606 next_insn:
3607 insn = NEXT_INSN (insn);
3608 if (insn == NEXT_INSN (BB_END (bb)))
3609 past_end = true;
3610 continue;
3612 if (!past_end && peep2_fill_buffer (bb, insn, live))
3613 goto next_insn;
3615 /* If we did not fill an empty buffer, it signals the end of the
3616 block. */
3617 if (peep2_current_count == 0)
3618 break;
3620 /* The buffer filled to the current maximum, so try to match. */
3622 pos = peep2_buf_position (peep2_current + peep2_current_count);
3623 peep2_insn_data[pos].insn = PEEP2_EOB;
3624 COPY_REG_SET (peep2_insn_data[pos].live_before, live);
3626 /* Match the peephole. */
3627 head = peep2_insn_data[peep2_current].insn;
3628 attempt = peephole2_insns (PATTERN (head), head, &match_len);
3629 if (attempt != NULL)
3631 rtx_insn *last = peep2_attempt (bb, head, match_len, attempt);
3632 if (last)
3634 peep2_update_life (bb, match_len, last, PREV_INSN (attempt));
3635 continue;
3639 /* No match: advance the buffer by one insn. */
3640 peep2_current = peep2_buf_position (peep2_current + 1);
3641 peep2_current_count--;
3645 default_rtl_profile ();
3646 for (i = 0; i < MAX_INSNS_PER_PEEP2 + 1; ++i)
3647 BITMAP_FREE (peep2_insn_data[i].live_before);
3648 BITMAP_FREE (live);
3649 if (peep2_do_rebuild_jump_labels)
3650 rebuild_jump_labels (get_insns ());
3651 if (peep2_do_cleanup_cfg)
3652 cleanup_cfg (CLEANUP_CFG_CHANGED);
3655 /* Common predicates for use with define_bypass. */
3657 /* Helper function for store_data_bypass_p, handle just a single SET
3658 IN_SET. */
3660 static bool
3661 store_data_bypass_p_1 (rtx_insn *out_insn, rtx in_set)
3663 if (!MEM_P (SET_DEST (in_set)))
3664 return false;
3666 rtx out_set = single_set (out_insn);
3667 if (out_set)
3668 return !reg_mentioned_p (SET_DEST (out_set), SET_DEST (in_set));
3670 rtx out_pat = PATTERN (out_insn);
3671 if (GET_CODE (out_pat) != PARALLEL)
3672 return false;
3674 for (int i = 0; i < XVECLEN (out_pat, 0); i++)
3676 rtx out_exp = XVECEXP (out_pat, 0, i);
3678 if (GET_CODE (out_exp) == CLOBBER || GET_CODE (out_exp) == USE)
3679 continue;
3681 gcc_assert (GET_CODE (out_exp) == SET);
3683 if (reg_mentioned_p (SET_DEST (out_exp), SET_DEST (in_set)))
3684 return false;
3687 return true;
3690 /* True if the dependency between OUT_INSN and IN_INSN is on the store
3691 data not the address operand(s) of the store. IN_INSN and OUT_INSN
3692 must be either a single_set or a PARALLEL with SETs inside. */
3695 store_data_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
3697 rtx in_set = single_set (in_insn);
3698 if (in_set)
3699 return store_data_bypass_p_1 (out_insn, in_set);
3701 rtx in_pat = PATTERN (in_insn);
3702 if (GET_CODE (in_pat) != PARALLEL)
3703 return false;
3705 for (int i = 0; i < XVECLEN (in_pat, 0); i++)
3707 rtx in_exp = XVECEXP (in_pat, 0, i);
3709 if (GET_CODE (in_exp) == CLOBBER || GET_CODE (in_exp) == USE)
3710 continue;
3712 gcc_assert (GET_CODE (in_exp) == SET);
3714 if (!store_data_bypass_p_1 (out_insn, in_exp))
3715 return false;
3718 return true;
3721 /* True if the dependency between OUT_INSN and IN_INSN is in the IF_THEN_ELSE
3722 condition, and not the THEN or ELSE branch. OUT_INSN may be either a single
3723 or multiple set; IN_INSN should be single_set for truth, but for convenience
3724 of insn categorization may be any JUMP or CALL insn. */
3727 if_test_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
3729 rtx out_set, in_set;
3731 in_set = single_set (in_insn);
3732 if (! in_set)
3734 gcc_assert (JUMP_P (in_insn) || CALL_P (in_insn));
3735 return false;
3738 if (GET_CODE (SET_SRC (in_set)) != IF_THEN_ELSE)
3739 return false;
3740 in_set = SET_SRC (in_set);
3742 out_set = single_set (out_insn);
3743 if (out_set)
3745 if (reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 1))
3746 || reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 2)))
3747 return false;
3749 else
3751 rtx out_pat;
3752 int i;
3754 out_pat = PATTERN (out_insn);
3755 gcc_assert (GET_CODE (out_pat) == PARALLEL);
3757 for (i = 0; i < XVECLEN (out_pat, 0); i++)
3759 rtx exp = XVECEXP (out_pat, 0, i);
3761 if (GET_CODE (exp) == CLOBBER)
3762 continue;
3764 gcc_assert (GET_CODE (exp) == SET);
3766 if (reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 1))
3767 || reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 2)))
3768 return false;
3772 return true;
3775 static unsigned int
3776 rest_of_handle_peephole2 (void)
3778 if (HAVE_peephole2)
3779 peephole2_optimize ();
3781 return 0;
3784 namespace {
3786 const pass_data pass_data_peephole2 =
3788 RTL_PASS, /* type */
3789 "peephole2", /* name */
3790 OPTGROUP_NONE, /* optinfo_flags */
3791 TV_PEEPHOLE2, /* tv_id */
3792 0, /* properties_required */
3793 0, /* properties_provided */
3794 0, /* properties_destroyed */
3795 0, /* todo_flags_start */
3796 TODO_df_finish, /* todo_flags_finish */
3799 class pass_peephole2 : public rtl_opt_pass
3801 public:
3802 pass_peephole2 (gcc::context *ctxt)
3803 : rtl_opt_pass (pass_data_peephole2, ctxt)
3806 /* opt_pass methods: */
3807 /* The epiphany backend creates a second instance of this pass, so we need
3808 a clone method. */
3809 opt_pass * clone () { return new pass_peephole2 (m_ctxt); }
3810 virtual bool gate (function *) { return (optimize > 0 && flag_peephole2); }
3811 virtual unsigned int execute (function *)
3813 return rest_of_handle_peephole2 ();
3816 }; // class pass_peephole2
3818 } // anon namespace
3820 rtl_opt_pass *
3821 make_pass_peephole2 (gcc::context *ctxt)
3823 return new pass_peephole2 (ctxt);
3826 namespace {
3828 const pass_data pass_data_split_all_insns =
3830 RTL_PASS, /* type */
3831 "split1", /* name */
3832 OPTGROUP_NONE, /* optinfo_flags */
3833 TV_NONE, /* tv_id */
3834 0, /* properties_required */
3835 PROP_rtl_split_insns, /* properties_provided */
3836 0, /* properties_destroyed */
3837 0, /* todo_flags_start */
3838 0, /* todo_flags_finish */
3841 class pass_split_all_insns : public rtl_opt_pass
3843 public:
3844 pass_split_all_insns (gcc::context *ctxt)
3845 : rtl_opt_pass (pass_data_split_all_insns, ctxt)
3848 /* opt_pass methods: */
3849 /* The epiphany backend creates a second instance of this pass, so
3850 we need a clone method. */
3851 opt_pass * clone () { return new pass_split_all_insns (m_ctxt); }
3852 virtual unsigned int execute (function *)
3854 split_all_insns ();
3855 return 0;
3858 }; // class pass_split_all_insns
3860 } // anon namespace
3862 rtl_opt_pass *
3863 make_pass_split_all_insns (gcc::context *ctxt)
3865 return new pass_split_all_insns (ctxt);
3868 namespace {
3870 const pass_data pass_data_split_after_reload =
3872 RTL_PASS, /* type */
3873 "split2", /* name */
3874 OPTGROUP_NONE, /* optinfo_flags */
3875 TV_NONE, /* tv_id */
3876 0, /* properties_required */
3877 0, /* properties_provided */
3878 0, /* properties_destroyed */
3879 0, /* todo_flags_start */
3880 0, /* todo_flags_finish */
3883 class pass_split_after_reload : public rtl_opt_pass
3885 public:
3886 pass_split_after_reload (gcc::context *ctxt)
3887 : rtl_opt_pass (pass_data_split_after_reload, ctxt)
3890 /* opt_pass methods: */
3891 virtual bool gate (function *)
3893 /* If optimizing, then go ahead and split insns now. */
3894 if (optimize > 0)
3895 return true;
3897 #ifdef STACK_REGS
3898 return true;
3899 #else
3900 return false;
3901 #endif
3904 virtual unsigned int execute (function *)
3906 split_all_insns ();
3907 return 0;
3910 }; // class pass_split_after_reload
3912 } // anon namespace
3914 rtl_opt_pass *
3915 make_pass_split_after_reload (gcc::context *ctxt)
3917 return new pass_split_after_reload (ctxt);
3920 namespace {
3922 const pass_data pass_data_split_before_regstack =
3924 RTL_PASS, /* type */
3925 "split3", /* name */
3926 OPTGROUP_NONE, /* optinfo_flags */
3927 TV_NONE, /* tv_id */
3928 0, /* properties_required */
3929 0, /* properties_provided */
3930 0, /* properties_destroyed */
3931 0, /* todo_flags_start */
3932 0, /* todo_flags_finish */
3935 class pass_split_before_regstack : public rtl_opt_pass
3937 public:
3938 pass_split_before_regstack (gcc::context *ctxt)
3939 : rtl_opt_pass (pass_data_split_before_regstack, ctxt)
3942 /* opt_pass methods: */
3943 virtual bool gate (function *);
3944 virtual unsigned int execute (function *)
3946 split_all_insns ();
3947 return 0;
3950 }; // class pass_split_before_regstack
3952 bool
3953 pass_split_before_regstack::gate (function *)
3955 #if HAVE_ATTR_length && defined (STACK_REGS)
3956 /* If flow2 creates new instructions which need splitting
3957 and scheduling after reload is not done, they might not be
3958 split until final which doesn't allow splitting
3959 if HAVE_ATTR_length. */
3960 # ifdef INSN_SCHEDULING
3961 return (optimize && !flag_schedule_insns_after_reload);
3962 # else
3963 return (optimize);
3964 # endif
3965 #else
3966 return 0;
3967 #endif
3970 } // anon namespace
3972 rtl_opt_pass *
3973 make_pass_split_before_regstack (gcc::context *ctxt)
3975 return new pass_split_before_regstack (ctxt);
3978 static unsigned int
3979 rest_of_handle_split_before_sched2 (void)
3981 #ifdef INSN_SCHEDULING
3982 split_all_insns ();
3983 #endif
3984 return 0;
3987 namespace {
3989 const pass_data pass_data_split_before_sched2 =
3991 RTL_PASS, /* type */
3992 "split4", /* name */
3993 OPTGROUP_NONE, /* optinfo_flags */
3994 TV_NONE, /* tv_id */
3995 0, /* properties_required */
3996 0, /* properties_provided */
3997 0, /* properties_destroyed */
3998 0, /* todo_flags_start */
3999 0, /* todo_flags_finish */
4002 class pass_split_before_sched2 : public rtl_opt_pass
4004 public:
4005 pass_split_before_sched2 (gcc::context *ctxt)
4006 : rtl_opt_pass (pass_data_split_before_sched2, ctxt)
4009 /* opt_pass methods: */
4010 virtual bool gate (function *)
4012 #ifdef INSN_SCHEDULING
4013 return optimize > 0 && flag_schedule_insns_after_reload;
4014 #else
4015 return false;
4016 #endif
4019 virtual unsigned int execute (function *)
4021 return rest_of_handle_split_before_sched2 ();
4024 }; // class pass_split_before_sched2
4026 } // anon namespace
4028 rtl_opt_pass *
4029 make_pass_split_before_sched2 (gcc::context *ctxt)
4031 return new pass_split_before_sched2 (ctxt);
4034 namespace {
4036 const pass_data pass_data_split_for_shorten_branches =
4038 RTL_PASS, /* type */
4039 "split5", /* name */
4040 OPTGROUP_NONE, /* optinfo_flags */
4041 TV_NONE, /* tv_id */
4042 0, /* properties_required */
4043 0, /* properties_provided */
4044 0, /* properties_destroyed */
4045 0, /* todo_flags_start */
4046 0, /* todo_flags_finish */
4049 class pass_split_for_shorten_branches : public rtl_opt_pass
4051 public:
4052 pass_split_for_shorten_branches (gcc::context *ctxt)
4053 : rtl_opt_pass (pass_data_split_for_shorten_branches, ctxt)
4056 /* opt_pass methods: */
4057 virtual bool gate (function *)
4059 /* The placement of the splitting that we do for shorten_branches
4060 depends on whether regstack is used by the target or not. */
4061 #if HAVE_ATTR_length && !defined (STACK_REGS)
4062 return true;
4063 #else
4064 return false;
4065 #endif
4068 virtual unsigned int execute (function *)
4070 return split_all_insns_noflow ();
4073 }; // class pass_split_for_shorten_branches
4075 } // anon namespace
4077 rtl_opt_pass *
4078 make_pass_split_for_shorten_branches (gcc::context *ctxt)
4080 return new pass_split_for_shorten_branches (ctxt);
4083 /* (Re)initialize the target information after a change in target. */
4085 void
4086 recog_init ()
4088 /* The information is zero-initialized, so we don't need to do anything
4089 first time round. */
4090 if (!this_target_recog->x_initialized)
4092 this_target_recog->x_initialized = true;
4093 return;
4095 memset (this_target_recog->x_bool_attr_masks, 0,
4096 sizeof (this_target_recog->x_bool_attr_masks));
4097 for (unsigned int i = 0; i < NUM_INSN_CODES; ++i)
4098 if (this_target_recog->x_op_alt[i])
4100 free (this_target_recog->x_op_alt[i]);
4101 this_target_recog->x_op_alt[i] = 0;