2015-05-22 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / recog.c
blob39a5d1f89ffdc549cad1111315bf4a025d9f6067
1 /* Subroutines used by or related to instruction recognition.
2 Copyright (C) 1987-2015 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 "tm.h"
25 #include "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "rtl-error.h"
36 #include "tm_p.h"
37 #include "insn-config.h"
38 #include "insn-attr.h"
39 #include "hard-reg-set.h"
40 #include "recog.h"
41 #include "regs.h"
42 #include "addresses.h"
43 #include "hashtab.h"
44 #include "function.h"
45 #include "rtl.h"
46 #include "flags.h"
47 #include "statistics.h"
48 #include "real.h"
49 #include "fixed-value.h"
50 #include "expmed.h"
51 #include "dojump.h"
52 #include "explow.h"
53 #include "calls.h"
54 #include "emit-rtl.h"
55 #include "varasm.h"
56 #include "stmt.h"
57 #include "expr.h"
58 #include "predict.h"
59 #include "dominance.h"
60 #include "cfg.h"
61 #include "cfgrtl.h"
62 #include "cfgbuild.h"
63 #include "cfgcleanup.h"
64 #include "basic-block.h"
65 #include "reload.h"
66 #include "target.h"
67 #include "tree-pass.h"
68 #include "df.h"
69 #include "insn-codes.h"
71 #ifndef STACK_POP_CODE
72 #if STACK_GROWS_DOWNWARD
73 #define STACK_POP_CODE POST_INC
74 #else
75 #define STACK_POP_CODE POST_DEC
76 #endif
77 #endif
79 static void validate_replace_rtx_1 (rtx *, rtx, rtx, rtx_insn *, bool);
80 static void validate_replace_src_1 (rtx *, void *);
81 static rtx_insn *split_insn (rtx_insn *);
83 struct target_recog default_target_recog;
84 #if SWITCHABLE_TARGET
85 struct target_recog *this_target_recog = &default_target_recog;
86 #endif
88 /* Nonzero means allow operands to be volatile.
89 This should be 0 if you are generating rtl, such as if you are calling
90 the functions in optabs.c and expmed.c (most of the time).
91 This should be 1 if all valid insns need to be recognized,
92 such as in reginfo.c and final.c and reload.c.
94 init_recog and init_recog_no_volatile are responsible for setting this. */
96 int volatile_ok;
98 struct recog_data_d recog_data;
100 /* Contains a vector of operand_alternative structures, such that
101 operand OP of alternative A is at index A * n_operands + OP.
102 Set up by preprocess_constraints. */
103 const operand_alternative *recog_op_alt;
105 /* Used to provide recog_op_alt for asms. */
106 static operand_alternative asm_op_alt[MAX_RECOG_OPERANDS
107 * MAX_RECOG_ALTERNATIVES];
109 /* On return from `constrain_operands', indicate which alternative
110 was satisfied. */
112 int which_alternative;
114 /* Nonzero after end of reload pass.
115 Set to 1 or 0 by toplev.c.
116 Controls the significance of (SUBREG (MEM)). */
118 int reload_completed;
120 /* Nonzero after thread_prologue_and_epilogue_insns has run. */
121 int epilogue_completed;
123 /* Initialize data used by the function `recog'.
124 This must be called once in the compilation of a function
125 before any insn recognition may be done in the function. */
127 void
128 init_recog_no_volatile (void)
130 volatile_ok = 0;
133 void
134 init_recog (void)
136 volatile_ok = 1;
140 /* Return true if labels in asm operands BODY are LABEL_REFs. */
142 static bool
143 asm_labels_ok (rtx body)
145 rtx asmop;
146 int i;
148 asmop = extract_asm_operands (body);
149 if (asmop == NULL_RTX)
150 return true;
152 for (i = 0; i < ASM_OPERANDS_LABEL_LENGTH (asmop); i++)
153 if (GET_CODE (ASM_OPERANDS_LABEL (asmop, i)) != LABEL_REF)
154 return false;
156 return true;
159 /* Check that X is an insn-body for an `asm' with operands
160 and that the operands mentioned in it are legitimate. */
163 check_asm_operands (rtx x)
165 int noperands;
166 rtx *operands;
167 const char **constraints;
168 int i;
170 if (!asm_labels_ok (x))
171 return 0;
173 /* Post-reload, be more strict with things. */
174 if (reload_completed)
176 /* ??? Doh! We've not got the wrapping insn. Cook one up. */
177 rtx_insn *insn = make_insn_raw (x);
178 extract_insn (insn);
179 constrain_operands (1, get_enabled_alternatives (insn));
180 return which_alternative >= 0;
183 noperands = asm_noperands (x);
184 if (noperands < 0)
185 return 0;
186 if (noperands == 0)
187 return 1;
189 operands = XALLOCAVEC (rtx, noperands);
190 constraints = XALLOCAVEC (const char *, noperands);
192 decode_asm_operands (x, operands, NULL, constraints, NULL, NULL);
194 for (i = 0; i < noperands; i++)
196 const char *c = constraints[i];
197 if (c[0] == '%')
198 c++;
199 if (! asm_operand_ok (operands[i], c, constraints))
200 return 0;
203 return 1;
206 /* Static data for the next two routines. */
208 typedef struct change_t
210 rtx object;
211 int old_code;
212 rtx *loc;
213 rtx old;
214 bool unshare;
215 } change_t;
217 static change_t *changes;
218 static int changes_allocated;
220 static int num_changes = 0;
222 /* Validate a proposed change to OBJECT. LOC is the location in the rtl
223 at which NEW_RTX will be placed. If OBJECT is zero, no validation is done,
224 the change is simply made.
226 Two types of objects are supported: If OBJECT is a MEM, memory_address_p
227 will be called with the address and mode as parameters. If OBJECT is
228 an INSN, CALL_INSN, or JUMP_INSN, the insn will be re-recognized with
229 the change in place.
231 IN_GROUP is nonzero if this is part of a group of changes that must be
232 performed as a group. In that case, the changes will be stored. The
233 function `apply_change_group' will validate and apply the changes.
235 If IN_GROUP is zero, this is a single change. Try to recognize the insn
236 or validate the memory reference with the change applied. If the result
237 is not valid for the machine, suppress the change and return zero.
238 Otherwise, perform the change and return 1. */
240 static bool
241 validate_change_1 (rtx object, rtx *loc, rtx new_rtx, bool in_group, bool unshare)
243 rtx old = *loc;
245 if (old == new_rtx || rtx_equal_p (old, new_rtx))
246 return 1;
248 gcc_assert (in_group != 0 || num_changes == 0);
250 *loc = new_rtx;
252 /* Save the information describing this change. */
253 if (num_changes >= changes_allocated)
255 if (changes_allocated == 0)
256 /* This value allows for repeated substitutions inside complex
257 indexed addresses, or changes in up to 5 insns. */
258 changes_allocated = MAX_RECOG_OPERANDS * 5;
259 else
260 changes_allocated *= 2;
262 changes = XRESIZEVEC (change_t, changes, changes_allocated);
265 changes[num_changes].object = object;
266 changes[num_changes].loc = loc;
267 changes[num_changes].old = old;
268 changes[num_changes].unshare = unshare;
270 if (object && !MEM_P (object))
272 /* Set INSN_CODE to force rerecognition of insn. Save old code in
273 case invalid. */
274 changes[num_changes].old_code = INSN_CODE (object);
275 INSN_CODE (object) = -1;
278 num_changes++;
280 /* If we are making a group of changes, return 1. Otherwise, validate the
281 change group we made. */
283 if (in_group)
284 return 1;
285 else
286 return apply_change_group ();
289 /* Wrapper for validate_change_1 without the UNSHARE argument defaulting
290 UNSHARE to false. */
292 bool
293 validate_change (rtx object, rtx *loc, rtx new_rtx, bool in_group)
295 return validate_change_1 (object, loc, new_rtx, in_group, false);
298 /* Wrapper for validate_change_1 without the UNSHARE argument defaulting
299 UNSHARE to true. */
301 bool
302 validate_unshare_change (rtx object, rtx *loc, rtx new_rtx, bool in_group)
304 return validate_change_1 (object, loc, new_rtx, in_group, true);
308 /* Keep X canonicalized if some changes have made it non-canonical; only
309 modifies the operands of X, not (for example) its code. Simplifications
310 are not the job of this routine.
312 Return true if anything was changed. */
313 bool
314 canonicalize_change_group (rtx_insn *insn, rtx x)
316 if (COMMUTATIVE_P (x)
317 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
319 /* Oops, the caller has made X no longer canonical.
320 Let's redo the changes in the correct order. */
321 rtx tem = XEXP (x, 0);
322 validate_unshare_change (insn, &XEXP (x, 0), XEXP (x, 1), 1);
323 validate_unshare_change (insn, &XEXP (x, 1), tem, 1);
324 return true;
326 else
327 return false;
331 /* This subroutine of apply_change_group verifies whether the changes to INSN
332 were valid; i.e. whether INSN can still be recognized.
334 If IN_GROUP is true clobbers which have to be added in order to
335 match the instructions will be added to the current change group.
336 Otherwise the changes will take effect immediately. */
339 insn_invalid_p (rtx_insn *insn, bool in_group)
341 rtx pat = PATTERN (insn);
342 int num_clobbers = 0;
343 /* If we are before reload and the pattern is a SET, see if we can add
344 clobbers. */
345 int icode = recog (pat, insn,
346 (GET_CODE (pat) == SET
347 && ! reload_completed
348 && ! reload_in_progress)
349 ? &num_clobbers : 0);
350 int is_asm = icode < 0 && asm_noperands (PATTERN (insn)) >= 0;
353 /* If this is an asm and the operand aren't legal, then fail. Likewise if
354 this is not an asm and the insn wasn't recognized. */
355 if ((is_asm && ! check_asm_operands (PATTERN (insn)))
356 || (!is_asm && icode < 0))
357 return 1;
359 /* If we have to add CLOBBERs, fail if we have to add ones that reference
360 hard registers since our callers can't know if they are live or not.
361 Otherwise, add them. */
362 if (num_clobbers > 0)
364 rtx newpat;
366 if (added_clobbers_hard_reg_p (icode))
367 return 1;
369 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_clobbers + 1));
370 XVECEXP (newpat, 0, 0) = pat;
371 add_clobbers (newpat, icode);
372 if (in_group)
373 validate_change (insn, &PATTERN (insn), newpat, 1);
374 else
375 PATTERN (insn) = pat = newpat;
378 /* After reload, verify that all constraints are satisfied. */
379 if (reload_completed)
381 extract_insn (insn);
383 if (! constrain_operands (1, get_preferred_alternatives (insn)))
384 return 1;
387 INSN_CODE (insn) = icode;
388 return 0;
391 /* Return number of changes made and not validated yet. */
393 num_changes_pending (void)
395 return num_changes;
398 /* Tentatively apply the changes numbered NUM and up.
399 Return 1 if all changes are valid, zero otherwise. */
402 verify_changes (int num)
404 int i;
405 rtx last_validated = NULL_RTX;
407 /* The changes have been applied and all INSN_CODEs have been reset to force
408 rerecognition.
410 The changes are valid if we aren't given an object, or if we are
411 given a MEM and it still is a valid address, or if this is in insn
412 and it is recognized. In the latter case, if reload has completed,
413 we also require that the operands meet the constraints for
414 the insn. */
416 for (i = num; i < num_changes; i++)
418 rtx object = changes[i].object;
420 /* If there is no object to test or if it is the same as the one we
421 already tested, ignore it. */
422 if (object == 0 || object == last_validated)
423 continue;
425 if (MEM_P (object))
427 if (! memory_address_addr_space_p (GET_MODE (object),
428 XEXP (object, 0),
429 MEM_ADDR_SPACE (object)))
430 break;
432 else if (/* changes[i].old might be zero, e.g. when putting a
433 REG_FRAME_RELATED_EXPR into a previously empty list. */
434 changes[i].old
435 && REG_P (changes[i].old)
436 && asm_noperands (PATTERN (object)) > 0
437 && REG_EXPR (changes[i].old) != NULL_TREE
438 && DECL_ASSEMBLER_NAME_SET_P (REG_EXPR (changes[i].old))
439 && DECL_REGISTER (REG_EXPR (changes[i].old)))
441 /* Don't allow changes of hard register operands to inline
442 assemblies if they have been defined as register asm ("x"). */
443 break;
445 else if (DEBUG_INSN_P (object))
446 continue;
447 else if (insn_invalid_p (as_a <rtx_insn *> (object), true))
449 rtx pat = PATTERN (object);
451 /* Perhaps we couldn't recognize the insn because there were
452 extra CLOBBERs at the end. If so, try to re-recognize
453 without the last CLOBBER (later iterations will cause each of
454 them to be eliminated, in turn). But don't do this if we
455 have an ASM_OPERAND. */
456 if (GET_CODE (pat) == PARALLEL
457 && GET_CODE (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1)) == CLOBBER
458 && asm_noperands (PATTERN (object)) < 0)
460 rtx newpat;
462 if (XVECLEN (pat, 0) == 2)
463 newpat = XVECEXP (pat, 0, 0);
464 else
466 int j;
468 newpat
469 = gen_rtx_PARALLEL (VOIDmode,
470 rtvec_alloc (XVECLEN (pat, 0) - 1));
471 for (j = 0; j < XVECLEN (newpat, 0); j++)
472 XVECEXP (newpat, 0, j) = XVECEXP (pat, 0, j);
475 /* Add a new change to this group to replace the pattern
476 with this new pattern. Then consider this change
477 as having succeeded. The change we added will
478 cause the entire call to fail if things remain invalid.
480 Note that this can lose if a later change than the one
481 we are processing specified &XVECEXP (PATTERN (object), 0, X)
482 but this shouldn't occur. */
484 validate_change (object, &PATTERN (object), newpat, 1);
485 continue;
487 else if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER
488 || GET_CODE (pat) == VAR_LOCATION)
489 /* If this insn is a CLOBBER or USE, it is always valid, but is
490 never recognized. */
491 continue;
492 else
493 break;
495 last_validated = object;
498 return (i == num_changes);
501 /* A group of changes has previously been issued with validate_change
502 and verified with verify_changes. Call df_insn_rescan for each of
503 the insn changed and clear num_changes. */
505 void
506 confirm_change_group (void)
508 int i;
509 rtx last_object = NULL;
511 for (i = 0; i < num_changes; i++)
513 rtx object = changes[i].object;
515 if (changes[i].unshare)
516 *changes[i].loc = copy_rtx (*changes[i].loc);
518 /* Avoid unnecessary rescanning when multiple changes to same instruction
519 are made. */
520 if (object)
522 if (object != last_object && last_object && INSN_P (last_object))
523 df_insn_rescan (as_a <rtx_insn *> (last_object));
524 last_object = object;
528 if (last_object && INSN_P (last_object))
529 df_insn_rescan (as_a <rtx_insn *> (last_object));
530 num_changes = 0;
533 /* Apply a group of changes previously issued with `validate_change'.
534 If all changes are valid, call confirm_change_group and return 1,
535 otherwise, call cancel_changes and return 0. */
538 apply_change_group (void)
540 if (verify_changes (0))
542 confirm_change_group ();
543 return 1;
545 else
547 cancel_changes (0);
548 return 0;
553 /* Return the number of changes so far in the current group. */
556 num_validated_changes (void)
558 return num_changes;
561 /* Retract the changes numbered NUM and up. */
563 void
564 cancel_changes (int num)
566 int i;
568 /* Back out all the changes. Do this in the opposite order in which
569 they were made. */
570 for (i = num_changes - 1; i >= num; i--)
572 *changes[i].loc = changes[i].old;
573 if (changes[i].object && !MEM_P (changes[i].object))
574 INSN_CODE (changes[i].object) = changes[i].old_code;
576 num_changes = num;
579 /* Reduce conditional compilation elsewhere. */
580 #ifndef HAVE_extv
581 #define HAVE_extv 0
582 #define CODE_FOR_extv CODE_FOR_nothing
583 #endif
584 #ifndef HAVE_extzv
585 #define HAVE_extzv 0
586 #define CODE_FOR_extzv CODE_FOR_nothing
587 #endif
589 /* A subroutine of validate_replace_rtx_1 that tries to simplify the resulting
590 rtx. */
592 static void
593 simplify_while_replacing (rtx *loc, rtx to, rtx_insn *object,
594 machine_mode op0_mode)
596 rtx x = *loc;
597 enum rtx_code code = GET_CODE (x);
598 rtx new_rtx = NULL_RTX;
600 if (SWAPPABLE_OPERANDS_P (x)
601 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
603 validate_unshare_change (object, loc,
604 gen_rtx_fmt_ee (COMMUTATIVE_ARITH_P (x) ? code
605 : swap_condition (code),
606 GET_MODE (x), XEXP (x, 1),
607 XEXP (x, 0)), 1);
608 x = *loc;
609 code = GET_CODE (x);
612 /* Canonicalize arithmetics with all constant operands. */
613 switch (GET_RTX_CLASS (code))
615 case RTX_UNARY:
616 if (CONSTANT_P (XEXP (x, 0)))
617 new_rtx = simplify_unary_operation (code, GET_MODE (x), XEXP (x, 0),
618 op0_mode);
619 break;
620 case RTX_COMM_ARITH:
621 case RTX_BIN_ARITH:
622 if (CONSTANT_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
623 new_rtx = simplify_binary_operation (code, GET_MODE (x), XEXP (x, 0),
624 XEXP (x, 1));
625 break;
626 case RTX_COMPARE:
627 case RTX_COMM_COMPARE:
628 if (CONSTANT_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
629 new_rtx = simplify_relational_operation (code, GET_MODE (x), op0_mode,
630 XEXP (x, 0), XEXP (x, 1));
631 break;
632 default:
633 break;
635 if (new_rtx)
637 validate_change (object, loc, new_rtx, 1);
638 return;
641 switch (code)
643 case PLUS:
644 /* If we have a PLUS whose second operand is now a CONST_INT, use
645 simplify_gen_binary to try to simplify it.
646 ??? We may want later to remove this, once simplification is
647 separated from this function. */
648 if (CONST_INT_P (XEXP (x, 1)) && XEXP (x, 1) == to)
649 validate_change (object, loc,
650 simplify_gen_binary
651 (PLUS, GET_MODE (x), XEXP (x, 0), XEXP (x, 1)), 1);
652 break;
653 case MINUS:
654 if (CONST_SCALAR_INT_P (XEXP (x, 1)))
655 validate_change (object, loc,
656 simplify_gen_binary
657 (PLUS, GET_MODE (x), XEXP (x, 0),
658 simplify_gen_unary (NEG,
659 GET_MODE (x), XEXP (x, 1),
660 GET_MODE (x))), 1);
661 break;
662 case ZERO_EXTEND:
663 case SIGN_EXTEND:
664 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
666 new_rtx = simplify_gen_unary (code, GET_MODE (x), XEXP (x, 0),
667 op0_mode);
668 /* If any of the above failed, substitute in something that
669 we know won't be recognized. */
670 if (!new_rtx)
671 new_rtx = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
672 validate_change (object, loc, new_rtx, 1);
674 break;
675 case SUBREG:
676 /* All subregs possible to simplify should be simplified. */
677 new_rtx = simplify_subreg (GET_MODE (x), SUBREG_REG (x), op0_mode,
678 SUBREG_BYTE (x));
680 /* Subregs of VOIDmode operands are incorrect. */
681 if (!new_rtx && GET_MODE (SUBREG_REG (x)) == VOIDmode)
682 new_rtx = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
683 if (new_rtx)
684 validate_change (object, loc, new_rtx, 1);
685 break;
686 case ZERO_EXTRACT:
687 case SIGN_EXTRACT:
688 /* If we are replacing a register with memory, try to change the memory
689 to be the mode required for memory in extract operations (this isn't
690 likely to be an insertion operation; if it was, nothing bad will
691 happen, we might just fail in some cases). */
693 if (MEM_P (XEXP (x, 0))
694 && CONST_INT_P (XEXP (x, 1))
695 && CONST_INT_P (XEXP (x, 2))
696 && !mode_dependent_address_p (XEXP (XEXP (x, 0), 0),
697 MEM_ADDR_SPACE (XEXP (x, 0)))
698 && !MEM_VOLATILE_P (XEXP (x, 0)))
700 machine_mode wanted_mode = VOIDmode;
701 machine_mode is_mode = GET_MODE (XEXP (x, 0));
702 int pos = INTVAL (XEXP (x, 2));
704 if (GET_CODE (x) == ZERO_EXTRACT && HAVE_extzv)
706 wanted_mode = insn_data[CODE_FOR_extzv].operand[1].mode;
707 if (wanted_mode == VOIDmode)
708 wanted_mode = word_mode;
710 else if (GET_CODE (x) == SIGN_EXTRACT && HAVE_extv)
712 wanted_mode = insn_data[CODE_FOR_extv].operand[1].mode;
713 if (wanted_mode == VOIDmode)
714 wanted_mode = word_mode;
717 /* If we have a narrower mode, we can do something. */
718 if (wanted_mode != VOIDmode
719 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
721 int offset = pos / BITS_PER_UNIT;
722 rtx newmem;
724 /* If the bytes and bits are counted differently, we
725 must adjust the offset. */
726 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
727 offset =
728 (GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (wanted_mode) -
729 offset);
731 gcc_assert (GET_MODE_PRECISION (wanted_mode)
732 == GET_MODE_BITSIZE (wanted_mode));
733 pos %= GET_MODE_BITSIZE (wanted_mode);
735 newmem = adjust_address_nv (XEXP (x, 0), wanted_mode, offset);
737 validate_change (object, &XEXP (x, 2), GEN_INT (pos), 1);
738 validate_change (object, &XEXP (x, 0), newmem, 1);
742 break;
744 default:
745 break;
749 /* Replace every occurrence of FROM in X with TO. Mark each change with
750 validate_change passing OBJECT. */
752 static void
753 validate_replace_rtx_1 (rtx *loc, rtx from, rtx to, rtx_insn *object,
754 bool simplify)
756 int i, j;
757 const char *fmt;
758 rtx x = *loc;
759 enum rtx_code code;
760 machine_mode op0_mode = VOIDmode;
761 int prev_changes = num_changes;
763 if (!x)
764 return;
766 code = GET_CODE (x);
767 fmt = GET_RTX_FORMAT (code);
768 if (fmt[0] == 'e')
769 op0_mode = GET_MODE (XEXP (x, 0));
771 /* X matches FROM if it is the same rtx or they are both referring to the
772 same register in the same mode. Avoid calling rtx_equal_p unless the
773 operands look similar. */
775 if (x == from
776 || (REG_P (x) && REG_P (from)
777 && GET_MODE (x) == GET_MODE (from)
778 && REGNO (x) == REGNO (from))
779 || (GET_CODE (x) == GET_CODE (from) && GET_MODE (x) == GET_MODE (from)
780 && rtx_equal_p (x, from)))
782 validate_unshare_change (object, loc, to, 1);
783 return;
786 /* Call ourself recursively to perform the replacements.
787 We must not replace inside already replaced expression, otherwise we
788 get infinite recursion for replacements like (reg X)->(subreg (reg X))
789 so we must special case shared ASM_OPERANDS. */
791 if (GET_CODE (x) == PARALLEL)
793 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
795 if (j && GET_CODE (XVECEXP (x, 0, j)) == SET
796 && GET_CODE (SET_SRC (XVECEXP (x, 0, j))) == ASM_OPERANDS)
798 /* Verify that operands are really shared. */
799 gcc_assert (ASM_OPERANDS_INPUT_VEC (SET_SRC (XVECEXP (x, 0, 0)))
800 == ASM_OPERANDS_INPUT_VEC (SET_SRC (XVECEXP
801 (x, 0, j))));
802 validate_replace_rtx_1 (&SET_DEST (XVECEXP (x, 0, j)),
803 from, to, object, simplify);
805 else
806 validate_replace_rtx_1 (&XVECEXP (x, 0, j), from, to, object,
807 simplify);
810 else
811 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
813 if (fmt[i] == 'e')
814 validate_replace_rtx_1 (&XEXP (x, i), from, to, object, simplify);
815 else if (fmt[i] == 'E')
816 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
817 validate_replace_rtx_1 (&XVECEXP (x, i, j), from, to, object,
818 simplify);
821 /* If we didn't substitute, there is nothing more to do. */
822 if (num_changes == prev_changes)
823 return;
825 /* ??? The regmove is no more, so is this aberration still necessary? */
826 /* Allow substituted expression to have different mode. This is used by
827 regmove to change mode of pseudo register. */
828 if (fmt[0] == 'e' && GET_MODE (XEXP (x, 0)) != VOIDmode)
829 op0_mode = GET_MODE (XEXP (x, 0));
831 /* Do changes needed to keep rtx consistent. Don't do any other
832 simplifications, as it is not our job. */
833 if (simplify)
834 simplify_while_replacing (loc, to, object, op0_mode);
837 /* Try replacing every occurrence of FROM in subexpression LOC of INSN
838 with TO. After all changes have been made, validate by seeing
839 if INSN is still valid. */
842 validate_replace_rtx_subexp (rtx from, rtx to, rtx_insn *insn, rtx *loc)
844 validate_replace_rtx_1 (loc, from, to, insn, true);
845 return apply_change_group ();
848 /* Try replacing every occurrence of FROM in INSN with TO. After all
849 changes have been made, validate by seeing if INSN is still valid. */
852 validate_replace_rtx (rtx from, rtx to, rtx_insn *insn)
854 validate_replace_rtx_1 (&PATTERN (insn), from, to, insn, true);
855 return apply_change_group ();
858 /* Try replacing every occurrence of FROM in WHERE with TO. Assume that WHERE
859 is a part of INSN. After all changes have been made, validate by seeing if
860 INSN is still valid.
861 validate_replace_rtx (from, to, insn) is equivalent to
862 validate_replace_rtx_part (from, to, &PATTERN (insn), insn). */
865 validate_replace_rtx_part (rtx from, rtx to, rtx *where, rtx_insn *insn)
867 validate_replace_rtx_1 (where, from, to, insn, true);
868 return apply_change_group ();
871 /* Same as above, but do not simplify rtx afterwards. */
873 validate_replace_rtx_part_nosimplify (rtx from, rtx to, rtx *where,
874 rtx_insn *insn)
876 validate_replace_rtx_1 (where, from, to, insn, false);
877 return apply_change_group ();
881 /* Try replacing every occurrence of FROM in INSN with TO. This also
882 will replace in REG_EQUAL and REG_EQUIV notes. */
884 void
885 validate_replace_rtx_group (rtx from, rtx to, rtx_insn *insn)
887 rtx note;
888 validate_replace_rtx_1 (&PATTERN (insn), from, to, insn, true);
889 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
890 if (REG_NOTE_KIND (note) == REG_EQUAL
891 || REG_NOTE_KIND (note) == REG_EQUIV)
892 validate_replace_rtx_1 (&XEXP (note, 0), from, to, insn, true);
895 /* Function called by note_uses to replace used subexpressions. */
896 struct validate_replace_src_data
898 rtx from; /* Old RTX */
899 rtx to; /* New RTX */
900 rtx_insn *insn; /* Insn in which substitution is occurring. */
903 static void
904 validate_replace_src_1 (rtx *x, void *data)
906 struct validate_replace_src_data *d
907 = (struct validate_replace_src_data *) data;
909 validate_replace_rtx_1 (x, d->from, d->to, d->insn, true);
912 /* Try replacing every occurrence of FROM in INSN with TO, avoiding
913 SET_DESTs. */
915 void
916 validate_replace_src_group (rtx from, rtx to, rtx_insn *insn)
918 struct validate_replace_src_data d;
920 d.from = from;
921 d.to = to;
922 d.insn = insn;
923 note_uses (&PATTERN (insn), validate_replace_src_1, &d);
926 /* Try simplify INSN.
927 Invoke simplify_rtx () on every SET_SRC and SET_DEST inside the INSN's
928 pattern and return true if something was simplified. */
930 bool
931 validate_simplify_insn (rtx_insn *insn)
933 int i;
934 rtx pat = NULL;
935 rtx newpat = NULL;
937 pat = PATTERN (insn);
939 if (GET_CODE (pat) == SET)
941 newpat = simplify_rtx (SET_SRC (pat));
942 if (newpat && !rtx_equal_p (SET_SRC (pat), newpat))
943 validate_change (insn, &SET_SRC (pat), newpat, 1);
944 newpat = simplify_rtx (SET_DEST (pat));
945 if (newpat && !rtx_equal_p (SET_DEST (pat), newpat))
946 validate_change (insn, &SET_DEST (pat), newpat, 1);
948 else if (GET_CODE (pat) == PARALLEL)
949 for (i = 0; i < XVECLEN (pat, 0); i++)
951 rtx s = XVECEXP (pat, 0, i);
953 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
955 newpat = simplify_rtx (SET_SRC (s));
956 if (newpat && !rtx_equal_p (SET_SRC (s), newpat))
957 validate_change (insn, &SET_SRC (s), newpat, 1);
958 newpat = simplify_rtx (SET_DEST (s));
959 if (newpat && !rtx_equal_p (SET_DEST (s), newpat))
960 validate_change (insn, &SET_DEST (s), newpat, 1);
963 return ((num_changes_pending () > 0) && (apply_change_group () > 0));
966 /* Return 1 if the insn using CC0 set by INSN does not contain
967 any ordered tests applied to the condition codes.
968 EQ and NE tests do not count. */
971 next_insn_tests_no_inequality (rtx_insn *insn)
973 rtx_insn *next = next_cc0_user (insn);
975 /* If there is no next insn, we have to take the conservative choice. */
976 if (next == 0)
977 return 0;
979 return (INSN_P (next)
980 && ! inequality_comparisons_p (PATTERN (next)));
983 /* Return 1 if OP is a valid general operand for machine mode MODE.
984 This is either a register reference, a memory reference,
985 or a constant. In the case of a memory reference, the address
986 is checked for general validity for the target machine.
988 Register and memory references must have mode MODE in order to be valid,
989 but some constants have no machine mode and are valid for any mode.
991 If MODE is VOIDmode, OP is checked for validity for whatever mode
992 it has.
994 The main use of this function is as a predicate in match_operand
995 expressions in the machine description. */
998 general_operand (rtx op, machine_mode mode)
1000 enum rtx_code code = GET_CODE (op);
1002 if (mode == VOIDmode)
1003 mode = GET_MODE (op);
1005 /* Don't accept CONST_INT or anything similar
1006 if the caller wants something floating. */
1007 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1008 && GET_MODE_CLASS (mode) != MODE_INT
1009 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1010 return 0;
1012 if (CONST_INT_P (op)
1013 && mode != VOIDmode
1014 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
1015 return 0;
1017 if (CONSTANT_P (op))
1018 return ((GET_MODE (op) == VOIDmode || GET_MODE (op) == mode
1019 || mode == VOIDmode)
1020 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1021 && targetm.legitimate_constant_p (mode == VOIDmode
1022 ? GET_MODE (op)
1023 : mode, op));
1025 /* Except for certain constants with VOIDmode, already checked for,
1026 OP's mode must match MODE if MODE specifies a mode. */
1028 if (GET_MODE (op) != mode)
1029 return 0;
1031 if (code == SUBREG)
1033 rtx sub = SUBREG_REG (op);
1035 #ifdef INSN_SCHEDULING
1036 /* On machines that have insn scheduling, we want all memory
1037 reference to be explicit, so outlaw paradoxical SUBREGs.
1038 However, we must allow them after reload so that they can
1039 get cleaned up by cleanup_subreg_operands. */
1040 if (!reload_completed && MEM_P (sub)
1041 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (sub)))
1042 return 0;
1043 #endif
1044 /* Avoid memories with nonzero SUBREG_BYTE, as offsetting the memory
1045 may result in incorrect reference. We should simplify all valid
1046 subregs of MEM anyway. But allow this after reload because we
1047 might be called from cleanup_subreg_operands.
1049 ??? This is a kludge. */
1050 if (!reload_completed && SUBREG_BYTE (op) != 0
1051 && MEM_P (sub))
1052 return 0;
1054 #ifdef CANNOT_CHANGE_MODE_CLASS
1055 if (REG_P (sub)
1056 && REGNO (sub) < FIRST_PSEUDO_REGISTER
1057 && REG_CANNOT_CHANGE_MODE_P (REGNO (sub), GET_MODE (sub), mode)
1058 && GET_MODE_CLASS (GET_MODE (sub)) != MODE_COMPLEX_INT
1059 && GET_MODE_CLASS (GET_MODE (sub)) != MODE_COMPLEX_FLOAT
1060 /* LRA can generate some invalid SUBREGS just for matched
1061 operand reload presentation. LRA needs to treat them as
1062 valid. */
1063 && ! LRA_SUBREG_P (op))
1064 return 0;
1065 #endif
1067 /* FLOAT_MODE subregs can't be paradoxical. Combine will occasionally
1068 create such rtl, and we must reject it. */
1069 if (SCALAR_FLOAT_MODE_P (GET_MODE (op))
1070 /* LRA can use subreg to store a floating point value in an
1071 integer mode. Although the floating point and the
1072 integer modes need the same number of hard registers, the
1073 size of floating point mode can be less than the integer
1074 mode. */
1075 && ! lra_in_progress
1076 && GET_MODE_SIZE (GET_MODE (op)) > GET_MODE_SIZE (GET_MODE (sub)))
1077 return 0;
1079 op = sub;
1080 code = GET_CODE (op);
1083 if (code == REG)
1084 return (REGNO (op) >= FIRST_PSEUDO_REGISTER
1085 || in_hard_reg_set_p (operand_reg_set, GET_MODE (op), REGNO (op)));
1087 if (code == MEM)
1089 rtx y = XEXP (op, 0);
1091 if (! volatile_ok && MEM_VOLATILE_P (op))
1092 return 0;
1094 /* Use the mem's mode, since it will be reloaded thus. LRA can
1095 generate move insn with invalid addresses which is made valid
1096 and efficiently calculated by LRA through further numerous
1097 transformations. */
1098 if (lra_in_progress
1099 || memory_address_addr_space_p (GET_MODE (op), y, MEM_ADDR_SPACE (op)))
1100 return 1;
1103 return 0;
1106 /* Return 1 if OP is a valid memory address for a memory reference
1107 of mode MODE.
1109 The main use of this function is as a predicate in match_operand
1110 expressions in the machine description. */
1113 address_operand (rtx op, machine_mode mode)
1115 return memory_address_p (mode, op);
1118 /* Return 1 if OP is a register reference of mode MODE.
1119 If MODE is VOIDmode, accept a register in any mode.
1121 The main use of this function is as a predicate in match_operand
1122 expressions in the machine description. */
1125 register_operand (rtx op, machine_mode mode)
1127 if (GET_CODE (op) == SUBREG)
1129 rtx sub = SUBREG_REG (op);
1131 /* Before reload, we can allow (SUBREG (MEM...)) as a register operand
1132 because it is guaranteed to be reloaded into one.
1133 Just make sure the MEM is valid in itself.
1134 (Ideally, (SUBREG (MEM)...) should not exist after reload,
1135 but currently it does result from (SUBREG (REG)...) where the
1136 reg went on the stack.) */
1137 if (!REG_P (sub) && (reload_completed || !MEM_P (sub)))
1138 return 0;
1140 else if (!REG_P (op))
1141 return 0;
1142 return general_operand (op, mode);
1145 /* Return 1 for a register in Pmode; ignore the tested mode. */
1148 pmode_register_operand (rtx op, machine_mode mode ATTRIBUTE_UNUSED)
1150 return register_operand (op, Pmode);
1153 /* Return 1 if OP should match a MATCH_SCRATCH, i.e., if it is a SCRATCH
1154 or a hard register. */
1157 scratch_operand (rtx op, machine_mode mode)
1159 if (GET_MODE (op) != mode && mode != VOIDmode)
1160 return 0;
1162 return (GET_CODE (op) == SCRATCH
1163 || (REG_P (op)
1164 && (lra_in_progress
1165 || (REGNO (op) < FIRST_PSEUDO_REGISTER
1166 && REGNO_REG_CLASS (REGNO (op)) != NO_REGS))));
1169 /* Return 1 if OP is a valid immediate operand for mode MODE.
1171 The main use of this function is as a predicate in match_operand
1172 expressions in the machine description. */
1175 immediate_operand (rtx op, machine_mode mode)
1177 /* Don't accept CONST_INT or anything similar
1178 if the caller wants something floating. */
1179 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1180 && GET_MODE_CLASS (mode) != MODE_INT
1181 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1182 return 0;
1184 if (CONST_INT_P (op)
1185 && mode != VOIDmode
1186 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
1187 return 0;
1189 return (CONSTANT_P (op)
1190 && (GET_MODE (op) == mode || mode == VOIDmode
1191 || GET_MODE (op) == VOIDmode)
1192 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1193 && targetm.legitimate_constant_p (mode == VOIDmode
1194 ? GET_MODE (op)
1195 : mode, op));
1198 /* Returns 1 if OP is an operand that is a CONST_INT of mode MODE. */
1201 const_int_operand (rtx op, machine_mode mode)
1203 if (!CONST_INT_P (op))
1204 return 0;
1206 if (mode != VOIDmode
1207 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
1208 return 0;
1210 return 1;
1213 #if TARGET_SUPPORTS_WIDE_INT
1214 /* Returns 1 if OP is an operand that is a CONST_INT or CONST_WIDE_INT
1215 of mode MODE. */
1217 const_scalar_int_operand (rtx op, machine_mode mode)
1219 if (!CONST_SCALAR_INT_P (op))
1220 return 0;
1222 if (CONST_INT_P (op))
1223 return const_int_operand (op, mode);
1225 if (mode != VOIDmode)
1227 int prec = GET_MODE_PRECISION (mode);
1228 int bitsize = GET_MODE_BITSIZE (mode);
1230 if (CONST_WIDE_INT_NUNITS (op) * HOST_BITS_PER_WIDE_INT > bitsize)
1231 return 0;
1233 if (prec == bitsize)
1234 return 1;
1235 else
1237 /* Multiword partial int. */
1238 HOST_WIDE_INT x
1239 = CONST_WIDE_INT_ELT (op, CONST_WIDE_INT_NUNITS (op) - 1);
1240 return (sext_hwi (x, prec & (HOST_BITS_PER_WIDE_INT - 1)) == x);
1243 return 1;
1246 /* Returns 1 if OP is an operand that is a constant integer or constant
1247 floating-point number of MODE. */
1250 const_double_operand (rtx op, machine_mode mode)
1252 return (GET_CODE (op) == CONST_DOUBLE)
1253 && (GET_MODE (op) == mode || mode == VOIDmode);
1255 #else
1256 /* Returns 1 if OP is an operand that is a constant integer or constant
1257 floating-point number of MODE. */
1260 const_double_operand (rtx op, machine_mode mode)
1262 /* Don't accept CONST_INT or anything similar
1263 if the caller wants something floating. */
1264 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1265 && GET_MODE_CLASS (mode) != MODE_INT
1266 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1267 return 0;
1269 return ((CONST_DOUBLE_P (op) || CONST_INT_P (op))
1270 && (mode == VOIDmode || GET_MODE (op) == mode
1271 || GET_MODE (op) == VOIDmode));
1273 #endif
1274 /* Return 1 if OP is a general operand that is not an immediate
1275 operand of mode MODE. */
1278 nonimmediate_operand (rtx op, machine_mode mode)
1280 return (general_operand (op, mode) && ! CONSTANT_P (op));
1283 /* Return 1 if OP is a register reference or immediate value of mode MODE. */
1286 nonmemory_operand (rtx op, machine_mode mode)
1288 if (CONSTANT_P (op))
1289 return immediate_operand (op, mode);
1290 return register_operand (op, mode);
1293 /* Return 1 if OP is a valid operand that stands for pushing a
1294 value of mode MODE onto the stack.
1296 The main use of this function is as a predicate in match_operand
1297 expressions in the machine description. */
1300 push_operand (rtx op, machine_mode mode)
1302 unsigned int rounded_size = GET_MODE_SIZE (mode);
1304 #ifdef PUSH_ROUNDING
1305 rounded_size = PUSH_ROUNDING (rounded_size);
1306 #endif
1308 if (!MEM_P (op))
1309 return 0;
1311 if (mode != VOIDmode && GET_MODE (op) != mode)
1312 return 0;
1314 op = XEXP (op, 0);
1316 if (rounded_size == GET_MODE_SIZE (mode))
1318 if (GET_CODE (op) != STACK_PUSH_CODE)
1319 return 0;
1321 else
1323 if (GET_CODE (op) != PRE_MODIFY
1324 || GET_CODE (XEXP (op, 1)) != PLUS
1325 || XEXP (XEXP (op, 1), 0) != XEXP (op, 0)
1326 || !CONST_INT_P (XEXP (XEXP (op, 1), 1))
1327 || INTVAL (XEXP (XEXP (op, 1), 1))
1328 != ((STACK_GROWS_DOWNWARD ? -1 : 1) * (int) rounded_size))
1329 return 0;
1332 return XEXP (op, 0) == stack_pointer_rtx;
1335 /* Return 1 if OP is a valid operand that stands for popping a
1336 value of mode MODE off the stack.
1338 The main use of this function is as a predicate in match_operand
1339 expressions in the machine description. */
1342 pop_operand (rtx op, machine_mode mode)
1344 if (!MEM_P (op))
1345 return 0;
1347 if (mode != VOIDmode && GET_MODE (op) != mode)
1348 return 0;
1350 op = XEXP (op, 0);
1352 if (GET_CODE (op) != STACK_POP_CODE)
1353 return 0;
1355 return XEXP (op, 0) == stack_pointer_rtx;
1358 /* Return 1 if ADDR is a valid memory address
1359 for mode MODE in address space AS. */
1362 memory_address_addr_space_p (machine_mode mode ATTRIBUTE_UNUSED,
1363 rtx addr, addr_space_t as)
1365 #ifdef GO_IF_LEGITIMATE_ADDRESS
1366 gcc_assert (ADDR_SPACE_GENERIC_P (as));
1367 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
1368 return 0;
1370 win:
1371 return 1;
1372 #else
1373 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
1374 #endif
1377 /* Return 1 if OP is a valid memory reference with mode MODE,
1378 including a valid address.
1380 The main use of this function is as a predicate in match_operand
1381 expressions in the machine description. */
1384 memory_operand (rtx op, machine_mode mode)
1386 rtx inner;
1388 if (! reload_completed)
1389 /* Note that no SUBREG is a memory operand before end of reload pass,
1390 because (SUBREG (MEM...)) forces reloading into a register. */
1391 return MEM_P (op) && general_operand (op, mode);
1393 if (mode != VOIDmode && GET_MODE (op) != mode)
1394 return 0;
1396 inner = op;
1397 if (GET_CODE (inner) == SUBREG)
1398 inner = SUBREG_REG (inner);
1400 return (MEM_P (inner) && general_operand (op, mode));
1403 /* Return 1 if OP is a valid indirect memory reference with mode MODE;
1404 that is, a memory reference whose address is a general_operand. */
1407 indirect_operand (rtx op, machine_mode mode)
1409 /* Before reload, a SUBREG isn't in memory (see memory_operand, above). */
1410 if (! reload_completed
1411 && GET_CODE (op) == SUBREG && MEM_P (SUBREG_REG (op)))
1413 int offset = SUBREG_BYTE (op);
1414 rtx inner = SUBREG_REG (op);
1416 if (mode != VOIDmode && GET_MODE (op) != mode)
1417 return 0;
1419 /* The only way that we can have a general_operand as the resulting
1420 address is if OFFSET is zero and the address already is an operand
1421 or if the address is (plus Y (const_int -OFFSET)) and Y is an
1422 operand. */
1424 return ((offset == 0 && general_operand (XEXP (inner, 0), Pmode))
1425 || (GET_CODE (XEXP (inner, 0)) == PLUS
1426 && CONST_INT_P (XEXP (XEXP (inner, 0), 1))
1427 && INTVAL (XEXP (XEXP (inner, 0), 1)) == -offset
1428 && general_operand (XEXP (XEXP (inner, 0), 0), Pmode)));
1431 return (MEM_P (op)
1432 && memory_operand (op, mode)
1433 && general_operand (XEXP (op, 0), Pmode));
1436 /* Return 1 if this is an ordered comparison operator (not including
1437 ORDERED and UNORDERED). */
1440 ordered_comparison_operator (rtx op, machine_mode mode)
1442 if (mode != VOIDmode && GET_MODE (op) != mode)
1443 return false;
1444 switch (GET_CODE (op))
1446 case EQ:
1447 case NE:
1448 case LT:
1449 case LTU:
1450 case LE:
1451 case LEU:
1452 case GT:
1453 case GTU:
1454 case GE:
1455 case GEU:
1456 return true;
1457 default:
1458 return false;
1462 /* Return 1 if this is a comparison operator. This allows the use of
1463 MATCH_OPERATOR to recognize all the branch insns. */
1466 comparison_operator (rtx op, machine_mode mode)
1468 return ((mode == VOIDmode || GET_MODE (op) == mode)
1469 && COMPARISON_P (op));
1472 /* If BODY is an insn body that uses ASM_OPERANDS, return it. */
1475 extract_asm_operands (rtx body)
1477 rtx tmp;
1478 switch (GET_CODE (body))
1480 case ASM_OPERANDS:
1481 return body;
1483 case SET:
1484 /* Single output operand: BODY is (set OUTPUT (asm_operands ...)). */
1485 tmp = SET_SRC (body);
1486 if (GET_CODE (tmp) == ASM_OPERANDS)
1487 return tmp;
1488 break;
1490 case PARALLEL:
1491 tmp = XVECEXP (body, 0, 0);
1492 if (GET_CODE (tmp) == ASM_OPERANDS)
1493 return tmp;
1494 if (GET_CODE (tmp) == SET)
1496 tmp = SET_SRC (tmp);
1497 if (GET_CODE (tmp) == ASM_OPERANDS)
1498 return tmp;
1500 break;
1502 default:
1503 break;
1505 return NULL;
1508 /* If BODY is an insn body that uses ASM_OPERANDS,
1509 return the number of operands (both input and output) in the insn.
1510 Otherwise return -1. */
1513 asm_noperands (const_rtx body)
1515 rtx asm_op = extract_asm_operands (CONST_CAST_RTX (body));
1516 int n_sets = 0;
1518 if (asm_op == NULL)
1519 return -1;
1521 if (GET_CODE (body) == SET)
1522 n_sets = 1;
1523 else if (GET_CODE (body) == PARALLEL)
1525 int i;
1526 if (GET_CODE (XVECEXP (body, 0, 0)) == SET)
1528 /* Multiple output operands, or 1 output plus some clobbers:
1529 body is
1530 [(set OUTPUT (asm_operands ...))... (clobber (reg ...))...]. */
1531 /* Count backwards through CLOBBERs to determine number of SETs. */
1532 for (i = XVECLEN (body, 0); i > 0; i--)
1534 if (GET_CODE (XVECEXP (body, 0, i - 1)) == SET)
1535 break;
1536 if (GET_CODE (XVECEXP (body, 0, i - 1)) != CLOBBER)
1537 return -1;
1540 /* N_SETS is now number of output operands. */
1541 n_sets = i;
1543 /* Verify that all the SETs we have
1544 came from a single original asm_operands insn
1545 (so that invalid combinations are blocked). */
1546 for (i = 0; i < n_sets; i++)
1548 rtx elt = XVECEXP (body, 0, i);
1549 if (GET_CODE (elt) != SET)
1550 return -1;
1551 if (GET_CODE (SET_SRC (elt)) != ASM_OPERANDS)
1552 return -1;
1553 /* If these ASM_OPERANDS rtx's came from different original insns
1554 then they aren't allowed together. */
1555 if (ASM_OPERANDS_INPUT_VEC (SET_SRC (elt))
1556 != ASM_OPERANDS_INPUT_VEC (asm_op))
1557 return -1;
1560 else
1562 /* 0 outputs, but some clobbers:
1563 body is [(asm_operands ...) (clobber (reg ...))...]. */
1564 /* Make sure all the other parallel things really are clobbers. */
1565 for (i = XVECLEN (body, 0) - 1; i > 0; i--)
1566 if (GET_CODE (XVECEXP (body, 0, i)) != CLOBBER)
1567 return -1;
1571 return (ASM_OPERANDS_INPUT_LENGTH (asm_op)
1572 + ASM_OPERANDS_LABEL_LENGTH (asm_op) + n_sets);
1575 /* Assuming BODY is an insn body that uses ASM_OPERANDS,
1576 copy its operands (both input and output) into the vector OPERANDS,
1577 the locations of the operands within the insn into the vector OPERAND_LOCS,
1578 and the constraints for the operands into CONSTRAINTS.
1579 Write the modes of the operands into MODES.
1580 Return the assembler-template.
1582 If MODES, OPERAND_LOCS, CONSTRAINTS or OPERANDS is 0,
1583 we don't store that info. */
1585 const char *
1586 decode_asm_operands (rtx body, rtx *operands, rtx **operand_locs,
1587 const char **constraints, machine_mode *modes,
1588 location_t *loc)
1590 int nbase = 0, n, i;
1591 rtx asmop;
1593 switch (GET_CODE (body))
1595 case ASM_OPERANDS:
1596 /* Zero output asm: BODY is (asm_operands ...). */
1597 asmop = body;
1598 break;
1600 case SET:
1601 /* Single output asm: BODY is (set OUTPUT (asm_operands ...)). */
1602 asmop = SET_SRC (body);
1604 /* The output is in the SET.
1605 Its constraint is in the ASM_OPERANDS itself. */
1606 if (operands)
1607 operands[0] = SET_DEST (body);
1608 if (operand_locs)
1609 operand_locs[0] = &SET_DEST (body);
1610 if (constraints)
1611 constraints[0] = ASM_OPERANDS_OUTPUT_CONSTRAINT (asmop);
1612 if (modes)
1613 modes[0] = GET_MODE (SET_DEST (body));
1614 nbase = 1;
1615 break;
1617 case PARALLEL:
1619 int nparallel = XVECLEN (body, 0); /* Includes CLOBBERs. */
1621 asmop = XVECEXP (body, 0, 0);
1622 if (GET_CODE (asmop) == SET)
1624 asmop = SET_SRC (asmop);
1626 /* At least one output, plus some CLOBBERs. The outputs are in
1627 the SETs. Their constraints are in the ASM_OPERANDS itself. */
1628 for (i = 0; i < nparallel; i++)
1630 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
1631 break; /* Past last SET */
1632 if (operands)
1633 operands[i] = SET_DEST (XVECEXP (body, 0, i));
1634 if (operand_locs)
1635 operand_locs[i] = &SET_DEST (XVECEXP (body, 0, i));
1636 if (constraints)
1637 constraints[i] = XSTR (SET_SRC (XVECEXP (body, 0, i)), 1);
1638 if (modes)
1639 modes[i] = GET_MODE (SET_DEST (XVECEXP (body, 0, i)));
1641 nbase = i;
1643 break;
1646 default:
1647 gcc_unreachable ();
1650 n = ASM_OPERANDS_INPUT_LENGTH (asmop);
1651 for (i = 0; i < n; i++)
1653 if (operand_locs)
1654 operand_locs[nbase + i] = &ASM_OPERANDS_INPUT (asmop, i);
1655 if (operands)
1656 operands[nbase + i] = ASM_OPERANDS_INPUT (asmop, i);
1657 if (constraints)
1658 constraints[nbase + i] = ASM_OPERANDS_INPUT_CONSTRAINT (asmop, i);
1659 if (modes)
1660 modes[nbase + i] = ASM_OPERANDS_INPUT_MODE (asmop, i);
1662 nbase += n;
1664 n = ASM_OPERANDS_LABEL_LENGTH (asmop);
1665 for (i = 0; i < n; i++)
1667 if (operand_locs)
1668 operand_locs[nbase + i] = &ASM_OPERANDS_LABEL (asmop, i);
1669 if (operands)
1670 operands[nbase + i] = ASM_OPERANDS_LABEL (asmop, i);
1671 if (constraints)
1672 constraints[nbase + i] = "";
1673 if (modes)
1674 modes[nbase + i] = Pmode;
1677 if (loc)
1678 *loc = ASM_OPERANDS_SOURCE_LOCATION (asmop);
1680 return ASM_OPERANDS_TEMPLATE (asmop);
1683 /* Parse inline assembly string STRING and determine which operands are
1684 referenced by % markers. For the first NOPERANDS operands, set USED[I]
1685 to true if operand I is referenced.
1687 This is intended to distinguish barrier-like asms such as:
1689 asm ("" : "=m" (...));
1691 from real references such as:
1693 asm ("sw\t$0, %0" : "=m" (...)); */
1695 void
1696 get_referenced_operands (const char *string, bool *used,
1697 unsigned int noperands)
1699 memset (used, 0, sizeof (bool) * noperands);
1700 const char *p = string;
1701 while (*p)
1702 switch (*p)
1704 case '%':
1705 p += 1;
1706 /* A letter followed by a digit indicates an operand number. */
1707 if (ISALPHA (p[0]) && ISDIGIT (p[1]))
1708 p += 1;
1709 if (ISDIGIT (*p))
1711 char *endptr;
1712 unsigned long opnum = strtoul (p, &endptr, 10);
1713 if (endptr != p && opnum < noperands)
1714 used[opnum] = true;
1715 p = endptr;
1717 else
1718 p += 1;
1719 break;
1721 default:
1722 p++;
1723 break;
1727 /* Check if an asm_operand matches its constraints.
1728 Return > 0 if ok, = 0 if bad, < 0 if inconclusive. */
1731 asm_operand_ok (rtx op, const char *constraint, const char **constraints)
1733 int result = 0;
1734 #ifdef AUTO_INC_DEC
1735 bool incdec_ok = false;
1736 #endif
1738 /* Use constrain_operands after reload. */
1739 gcc_assert (!reload_completed);
1741 /* Empty constraint string is the same as "X,...,X", i.e. X for as
1742 many alternatives as required to match the other operands. */
1743 if (*constraint == '\0')
1744 result = 1;
1746 while (*constraint)
1748 enum constraint_num cn;
1749 char c = *constraint;
1750 int len;
1751 switch (c)
1753 case ',':
1754 constraint++;
1755 continue;
1757 case '0': case '1': case '2': case '3': case '4':
1758 case '5': case '6': case '7': case '8': case '9':
1759 /* If caller provided constraints pointer, look up
1760 the matching constraint. Otherwise, our caller should have
1761 given us the proper matching constraint, but we can't
1762 actually fail the check if they didn't. Indicate that
1763 results are inconclusive. */
1764 if (constraints)
1766 char *end;
1767 unsigned long match;
1769 match = strtoul (constraint, &end, 10);
1770 if (!result)
1771 result = asm_operand_ok (op, constraints[match], NULL);
1772 constraint = (const char *) end;
1774 else
1777 constraint++;
1778 while (ISDIGIT (*constraint));
1779 if (! result)
1780 result = -1;
1782 continue;
1784 /* The rest of the compiler assumes that reloading the address
1785 of a MEM into a register will make it fit an 'o' constraint.
1786 That is, if it sees a MEM operand for an 'o' constraint,
1787 it assumes that (mem (base-reg)) will fit.
1789 That assumption fails on targets that don't have offsettable
1790 addresses at all. We therefore need to treat 'o' asm
1791 constraints as a special case and only accept operands that
1792 are already offsettable, thus proving that at least one
1793 offsettable address exists. */
1794 case 'o': /* offsettable */
1795 if (offsettable_nonstrict_memref_p (op))
1796 result = 1;
1797 break;
1799 case 'g':
1800 if (general_operand (op, VOIDmode))
1801 result = 1;
1802 break;
1804 #ifdef AUTO_INC_DEC
1805 case '<':
1806 case '>':
1807 /* ??? Before auto-inc-dec, auto inc/dec insns are not supposed
1808 to exist, excepting those that expand_call created. Further,
1809 on some machines which do not have generalized auto inc/dec,
1810 an inc/dec is not a memory_operand.
1812 Match any memory and hope things are resolved after reload. */
1813 incdec_ok = true;
1814 #endif
1815 default:
1816 cn = lookup_constraint (constraint);
1817 switch (get_constraint_type (cn))
1819 case CT_REGISTER:
1820 if (!result
1821 && reg_class_for_constraint (cn) != NO_REGS
1822 && GET_MODE (op) != BLKmode
1823 && register_operand (op, VOIDmode))
1824 result = 1;
1825 break;
1827 case CT_CONST_INT:
1828 if (!result
1829 && CONST_INT_P (op)
1830 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
1831 result = 1;
1832 break;
1834 case CT_MEMORY:
1835 /* Every memory operand can be reloaded to fit. */
1836 result = result || memory_operand (op, VOIDmode);
1837 break;
1839 case CT_ADDRESS:
1840 /* Every address operand can be reloaded to fit. */
1841 result = result || address_operand (op, VOIDmode);
1842 break;
1844 case CT_FIXED_FORM:
1845 result = result || constraint_satisfied_p (op, cn);
1846 break;
1848 break;
1850 len = CONSTRAINT_LEN (c, constraint);
1852 constraint++;
1853 while (--len && *constraint);
1854 if (len)
1855 return 0;
1858 #ifdef AUTO_INC_DEC
1859 /* For operands without < or > constraints reject side-effects. */
1860 if (!incdec_ok && result && MEM_P (op))
1861 switch (GET_CODE (XEXP (op, 0)))
1863 case PRE_INC:
1864 case POST_INC:
1865 case PRE_DEC:
1866 case POST_DEC:
1867 case PRE_MODIFY:
1868 case POST_MODIFY:
1869 return 0;
1870 default:
1871 break;
1873 #endif
1875 return result;
1878 /* Given an rtx *P, if it is a sum containing an integer constant term,
1879 return the location (type rtx *) of the pointer to that constant term.
1880 Otherwise, return a null pointer. */
1882 rtx *
1883 find_constant_term_loc (rtx *p)
1885 rtx *tem;
1886 enum rtx_code code = GET_CODE (*p);
1888 /* If *P IS such a constant term, P is its location. */
1890 if (code == CONST_INT || code == SYMBOL_REF || code == LABEL_REF
1891 || code == CONST)
1892 return p;
1894 /* Otherwise, if not a sum, it has no constant term. */
1896 if (GET_CODE (*p) != PLUS)
1897 return 0;
1899 /* If one of the summands is constant, return its location. */
1901 if (XEXP (*p, 0) && CONSTANT_P (XEXP (*p, 0))
1902 && XEXP (*p, 1) && CONSTANT_P (XEXP (*p, 1)))
1903 return p;
1905 /* Otherwise, check each summand for containing a constant term. */
1907 if (XEXP (*p, 0) != 0)
1909 tem = find_constant_term_loc (&XEXP (*p, 0));
1910 if (tem != 0)
1911 return tem;
1914 if (XEXP (*p, 1) != 0)
1916 tem = find_constant_term_loc (&XEXP (*p, 1));
1917 if (tem != 0)
1918 return tem;
1921 return 0;
1924 /* Return 1 if OP is a memory reference
1925 whose address contains no side effects
1926 and remains valid after the addition
1927 of a positive integer less than the
1928 size of the object being referenced.
1930 We assume that the original address is valid and do not check it.
1932 This uses strict_memory_address_p as a subroutine, so
1933 don't use it before reload. */
1936 offsettable_memref_p (rtx op)
1938 return ((MEM_P (op))
1939 && offsettable_address_addr_space_p (1, GET_MODE (op), XEXP (op, 0),
1940 MEM_ADDR_SPACE (op)));
1943 /* Similar, but don't require a strictly valid mem ref:
1944 consider pseudo-regs valid as index or base regs. */
1947 offsettable_nonstrict_memref_p (rtx op)
1949 return ((MEM_P (op))
1950 && offsettable_address_addr_space_p (0, GET_MODE (op), XEXP (op, 0),
1951 MEM_ADDR_SPACE (op)));
1954 /* Return 1 if Y is a memory address which contains no side effects
1955 and would remain valid for address space AS after the addition of
1956 a positive integer less than the size of that mode.
1958 We assume that the original address is valid and do not check it.
1959 We do check that it is valid for narrower modes.
1961 If STRICTP is nonzero, we require a strictly valid address,
1962 for the sake of use in reload.c. */
1965 offsettable_address_addr_space_p (int strictp, machine_mode mode, rtx y,
1966 addr_space_t as)
1968 enum rtx_code ycode = GET_CODE (y);
1969 rtx z;
1970 rtx y1 = y;
1971 rtx *y2;
1972 int (*addressp) (machine_mode, rtx, addr_space_t) =
1973 (strictp ? strict_memory_address_addr_space_p
1974 : memory_address_addr_space_p);
1975 unsigned int mode_sz = GET_MODE_SIZE (mode);
1977 if (CONSTANT_ADDRESS_P (y))
1978 return 1;
1980 /* Adjusting an offsettable address involves changing to a narrower mode.
1981 Make sure that's OK. */
1983 if (mode_dependent_address_p (y, as))
1984 return 0;
1986 machine_mode address_mode = GET_MODE (y);
1987 if (address_mode == VOIDmode)
1988 address_mode = targetm.addr_space.address_mode (as);
1989 #ifdef POINTERS_EXTEND_UNSIGNED
1990 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
1991 #endif
1993 /* ??? How much offset does an offsettable BLKmode reference need?
1994 Clearly that depends on the situation in which it's being used.
1995 However, the current situation in which we test 0xffffffff is
1996 less than ideal. Caveat user. */
1997 if (mode_sz == 0)
1998 mode_sz = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
2000 /* If the expression contains a constant term,
2001 see if it remains valid when max possible offset is added. */
2003 if ((ycode == PLUS) && (y2 = find_constant_term_loc (&y1)))
2005 int good;
2007 y1 = *y2;
2008 *y2 = plus_constant (address_mode, *y2, mode_sz - 1);
2009 /* Use QImode because an odd displacement may be automatically invalid
2010 for any wider mode. But it should be valid for a single byte. */
2011 good = (*addressp) (QImode, y, as);
2013 /* In any case, restore old contents of memory. */
2014 *y2 = y1;
2015 return good;
2018 if (GET_RTX_CLASS (ycode) == RTX_AUTOINC)
2019 return 0;
2021 /* The offset added here is chosen as the maximum offset that
2022 any instruction could need to add when operating on something
2023 of the specified mode. We assume that if Y and Y+c are
2024 valid addresses then so is Y+d for all 0<d<c. adjust_address will
2025 go inside a LO_SUM here, so we do so as well. */
2026 if (GET_CODE (y) == LO_SUM
2027 && mode != BLKmode
2028 && mode_sz <= GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT)
2029 z = gen_rtx_LO_SUM (address_mode, XEXP (y, 0),
2030 plus_constant (address_mode, XEXP (y, 1),
2031 mode_sz - 1));
2032 #ifdef POINTERS_EXTEND_UNSIGNED
2033 /* Likewise for a ZERO_EXTEND from pointer_mode. */
2034 else if (POINTERS_EXTEND_UNSIGNED > 0
2035 && GET_CODE (y) == ZERO_EXTEND
2036 && GET_MODE (XEXP (y, 0)) == pointer_mode)
2037 z = gen_rtx_ZERO_EXTEND (address_mode,
2038 plus_constant (pointer_mode, XEXP (y, 0),
2039 mode_sz - 1));
2040 #endif
2041 else
2042 z = plus_constant (address_mode, y, mode_sz - 1);
2044 /* Use QImode because an odd displacement may be automatically invalid
2045 for any wider mode. But it should be valid for a single byte. */
2046 return (*addressp) (QImode, z, as);
2049 /* Return 1 if ADDR is an address-expression whose effect depends
2050 on the mode of the memory reference it is used in.
2052 ADDRSPACE is the address space associated with the address.
2054 Autoincrement addressing is a typical example of mode-dependence
2055 because the amount of the increment depends on the mode. */
2057 bool
2058 mode_dependent_address_p (rtx addr, addr_space_t addrspace)
2060 /* Auto-increment addressing with anything other than post_modify
2061 or pre_modify always introduces a mode dependency. Catch such
2062 cases now instead of deferring to the target. */
2063 if (GET_CODE (addr) == PRE_INC
2064 || GET_CODE (addr) == POST_INC
2065 || GET_CODE (addr) == PRE_DEC
2066 || GET_CODE (addr) == POST_DEC)
2067 return true;
2069 return targetm.mode_dependent_address_p (addr, addrspace);
2072 /* Return true if boolean attribute ATTR is supported. */
2074 static bool
2075 have_bool_attr (bool_attr attr)
2077 switch (attr)
2079 case BA_ENABLED:
2080 return HAVE_ATTR_enabled;
2081 case BA_PREFERRED_FOR_SIZE:
2082 return HAVE_ATTR_enabled || HAVE_ATTR_preferred_for_size;
2083 case BA_PREFERRED_FOR_SPEED:
2084 return HAVE_ATTR_enabled || HAVE_ATTR_preferred_for_speed;
2086 gcc_unreachable ();
2089 /* Return the value of ATTR for instruction INSN. */
2091 static bool
2092 get_bool_attr (rtx_insn *insn, bool_attr attr)
2094 switch (attr)
2096 case BA_ENABLED:
2097 return get_attr_enabled (insn);
2098 case BA_PREFERRED_FOR_SIZE:
2099 return get_attr_enabled (insn) && get_attr_preferred_for_size (insn);
2100 case BA_PREFERRED_FOR_SPEED:
2101 return get_attr_enabled (insn) && get_attr_preferred_for_speed (insn);
2103 gcc_unreachable ();
2106 /* Like get_bool_attr_mask, but don't use the cache. */
2108 static alternative_mask
2109 get_bool_attr_mask_uncached (rtx_insn *insn, bool_attr attr)
2111 /* Temporarily install enough information for get_attr_<foo> to assume
2112 that the insn operands are already cached. As above, the attribute
2113 mustn't depend on the values of operands, so we don't provide their
2114 real values here. */
2115 rtx_insn *old_insn = recog_data.insn;
2116 int old_alternative = which_alternative;
2118 recog_data.insn = insn;
2119 alternative_mask mask = ALL_ALTERNATIVES;
2120 int n_alternatives = insn_data[INSN_CODE (insn)].n_alternatives;
2121 for (int i = 0; i < n_alternatives; i++)
2123 which_alternative = i;
2124 if (!get_bool_attr (insn, attr))
2125 mask &= ~ALTERNATIVE_BIT (i);
2128 recog_data.insn = old_insn;
2129 which_alternative = old_alternative;
2130 return mask;
2133 /* Return the mask of operand alternatives that are allowed for INSN
2134 by boolean attribute ATTR. This mask depends only on INSN and on
2135 the current target; it does not depend on things like the values of
2136 operands. */
2138 static alternative_mask
2139 get_bool_attr_mask (rtx_insn *insn, bool_attr attr)
2141 /* Quick exit for asms and for targets that don't use these attributes. */
2142 int code = INSN_CODE (insn);
2143 if (code < 0 || !have_bool_attr (attr))
2144 return ALL_ALTERNATIVES;
2146 /* Calling get_attr_<foo> can be expensive, so cache the mask
2147 for speed. */
2148 if (!this_target_recog->x_bool_attr_masks[code][attr])
2149 this_target_recog->x_bool_attr_masks[code][attr]
2150 = get_bool_attr_mask_uncached (insn, attr);
2151 return this_target_recog->x_bool_attr_masks[code][attr];
2154 /* Return the set of alternatives of INSN that are allowed by the current
2155 target. */
2157 alternative_mask
2158 get_enabled_alternatives (rtx_insn *insn)
2160 return get_bool_attr_mask (insn, BA_ENABLED);
2163 /* Return the set of alternatives of INSN that are allowed by the current
2164 target and are preferred for the current size/speed optimization
2165 choice. */
2167 alternative_mask
2168 get_preferred_alternatives (rtx_insn *insn)
2170 if (optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn)))
2171 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SPEED);
2172 else
2173 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SIZE);
2176 /* Return the set of alternatives of INSN that are allowed by the current
2177 target and are preferred for the size/speed optimization choice
2178 associated with BB. Passing a separate BB is useful if INSN has not
2179 been emitted yet or if we are considering moving it to a different
2180 block. */
2182 alternative_mask
2183 get_preferred_alternatives (rtx_insn *insn, basic_block bb)
2185 if (optimize_bb_for_speed_p (bb))
2186 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SPEED);
2187 else
2188 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SIZE);
2191 /* Assert that the cached boolean attributes for INSN are still accurate.
2192 The backend is required to define these attributes in a way that only
2193 depends on the current target (rather than operands, compiler phase,
2194 etc.). */
2196 bool
2197 check_bool_attrs (rtx_insn *insn)
2199 int code = INSN_CODE (insn);
2200 if (code >= 0)
2201 for (int i = 0; i <= BA_LAST; ++i)
2203 enum bool_attr attr = (enum bool_attr) i;
2204 if (this_target_recog->x_bool_attr_masks[code][attr])
2205 gcc_assert (this_target_recog->x_bool_attr_masks[code][attr]
2206 == get_bool_attr_mask_uncached (insn, attr));
2208 return true;
2211 /* Like extract_insn, but save insn extracted and don't extract again, when
2212 called again for the same insn expecting that recog_data still contain the
2213 valid information. This is used primary by gen_attr infrastructure that
2214 often does extract insn again and again. */
2215 void
2216 extract_insn_cached (rtx_insn *insn)
2218 if (recog_data.insn == insn && INSN_CODE (insn) >= 0)
2219 return;
2220 extract_insn (insn);
2221 recog_data.insn = insn;
2224 /* Do uncached extract_insn, constrain_operands and complain about failures.
2225 This should be used when extracting a pre-existing constrained instruction
2226 if the caller wants to know which alternative was chosen. */
2227 void
2228 extract_constrain_insn (rtx_insn *insn)
2230 extract_insn (insn);
2231 if (!constrain_operands (reload_completed, get_enabled_alternatives (insn)))
2232 fatal_insn_not_found (insn);
2235 /* Do cached extract_insn, constrain_operands and complain about failures.
2236 Used by insn_attrtab. */
2237 void
2238 extract_constrain_insn_cached (rtx_insn *insn)
2240 extract_insn_cached (insn);
2241 if (which_alternative == -1
2242 && !constrain_operands (reload_completed,
2243 get_enabled_alternatives (insn)))
2244 fatal_insn_not_found (insn);
2247 /* Do cached constrain_operands on INSN and complain about failures. */
2249 constrain_operands_cached (rtx_insn *insn, int strict)
2251 if (which_alternative == -1)
2252 return constrain_operands (strict, get_enabled_alternatives (insn));
2253 else
2254 return 1;
2257 /* Analyze INSN and fill in recog_data. */
2259 void
2260 extract_insn (rtx_insn *insn)
2262 int i;
2263 int icode;
2264 int noperands;
2265 rtx body = PATTERN (insn);
2267 recog_data.n_operands = 0;
2268 recog_data.n_alternatives = 0;
2269 recog_data.n_dups = 0;
2270 recog_data.is_asm = false;
2272 switch (GET_CODE (body))
2274 case USE:
2275 case CLOBBER:
2276 case ASM_INPUT:
2277 case ADDR_VEC:
2278 case ADDR_DIFF_VEC:
2279 case VAR_LOCATION:
2280 return;
2282 case SET:
2283 if (GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
2284 goto asm_insn;
2285 else
2286 goto normal_insn;
2287 case PARALLEL:
2288 if ((GET_CODE (XVECEXP (body, 0, 0)) == SET
2289 && GET_CODE (SET_SRC (XVECEXP (body, 0, 0))) == ASM_OPERANDS)
2290 || GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
2291 goto asm_insn;
2292 else
2293 goto normal_insn;
2294 case ASM_OPERANDS:
2295 asm_insn:
2296 recog_data.n_operands = noperands = asm_noperands (body);
2297 if (noperands >= 0)
2299 /* This insn is an `asm' with operands. */
2301 /* expand_asm_operands makes sure there aren't too many operands. */
2302 gcc_assert (noperands <= MAX_RECOG_OPERANDS);
2304 /* Now get the operand values and constraints out of the insn. */
2305 decode_asm_operands (body, recog_data.operand,
2306 recog_data.operand_loc,
2307 recog_data.constraints,
2308 recog_data.operand_mode, NULL);
2309 memset (recog_data.is_operator, 0, sizeof recog_data.is_operator);
2310 if (noperands > 0)
2312 const char *p = recog_data.constraints[0];
2313 recog_data.n_alternatives = 1;
2314 while (*p)
2315 recog_data.n_alternatives += (*p++ == ',');
2317 recog_data.is_asm = true;
2318 break;
2320 fatal_insn_not_found (insn);
2322 default:
2323 normal_insn:
2324 /* Ordinary insn: recognize it, get the operands via insn_extract
2325 and get the constraints. */
2327 icode = recog_memoized (insn);
2328 if (icode < 0)
2329 fatal_insn_not_found (insn);
2331 recog_data.n_operands = noperands = insn_data[icode].n_operands;
2332 recog_data.n_alternatives = insn_data[icode].n_alternatives;
2333 recog_data.n_dups = insn_data[icode].n_dups;
2335 insn_extract (insn);
2337 for (i = 0; i < noperands; i++)
2339 recog_data.constraints[i] = insn_data[icode].operand[i].constraint;
2340 recog_data.is_operator[i] = insn_data[icode].operand[i].is_operator;
2341 recog_data.operand_mode[i] = insn_data[icode].operand[i].mode;
2342 /* VOIDmode match_operands gets mode from their real operand. */
2343 if (recog_data.operand_mode[i] == VOIDmode)
2344 recog_data.operand_mode[i] = GET_MODE (recog_data.operand[i]);
2347 for (i = 0; i < noperands; i++)
2348 recog_data.operand_type[i]
2349 = (recog_data.constraints[i][0] == '=' ? OP_OUT
2350 : recog_data.constraints[i][0] == '+' ? OP_INOUT
2351 : OP_IN);
2353 gcc_assert (recog_data.n_alternatives <= MAX_RECOG_ALTERNATIVES);
2355 recog_data.insn = NULL;
2356 which_alternative = -1;
2359 /* Fill in OP_ALT_BASE for an instruction that has N_OPERANDS operands,
2360 N_ALTERNATIVES alternatives and constraint strings CONSTRAINTS.
2361 OP_ALT_BASE has N_ALTERNATIVES * N_OPERANDS entries and CONSTRAINTS
2362 has N_OPERANDS entries. */
2364 void
2365 preprocess_constraints (int n_operands, int n_alternatives,
2366 const char **constraints,
2367 operand_alternative *op_alt_base)
2369 for (int i = 0; i < n_operands; i++)
2371 int j;
2372 struct operand_alternative *op_alt;
2373 const char *p = constraints[i];
2375 op_alt = op_alt_base;
2377 for (j = 0; j < n_alternatives; j++, op_alt += n_operands)
2379 op_alt[i].cl = NO_REGS;
2380 op_alt[i].constraint = p;
2381 op_alt[i].matches = -1;
2382 op_alt[i].matched = -1;
2384 if (*p == '\0' || *p == ',')
2386 op_alt[i].anything_ok = 1;
2387 continue;
2390 for (;;)
2392 char c = *p;
2393 if (c == '#')
2395 c = *++p;
2396 while (c != ',' && c != '\0');
2397 if (c == ',' || c == '\0')
2399 p++;
2400 break;
2403 switch (c)
2405 case '?':
2406 op_alt[i].reject += 6;
2407 break;
2408 case '!':
2409 op_alt[i].reject += 600;
2410 break;
2411 case '&':
2412 op_alt[i].earlyclobber = 1;
2413 break;
2415 case '0': case '1': case '2': case '3': case '4':
2416 case '5': case '6': case '7': case '8': case '9':
2418 char *end;
2419 op_alt[i].matches = strtoul (p, &end, 10);
2420 op_alt[op_alt[i].matches].matched = i;
2421 p = end;
2423 continue;
2425 case 'X':
2426 op_alt[i].anything_ok = 1;
2427 break;
2429 case 'g':
2430 op_alt[i].cl =
2431 reg_class_subunion[(int) op_alt[i].cl][(int) GENERAL_REGS];
2432 break;
2434 default:
2435 enum constraint_num cn = lookup_constraint (p);
2436 enum reg_class cl;
2437 switch (get_constraint_type (cn))
2439 case CT_REGISTER:
2440 cl = reg_class_for_constraint (cn);
2441 if (cl != NO_REGS)
2442 op_alt[i].cl = reg_class_subunion[op_alt[i].cl][cl];
2443 break;
2445 case CT_CONST_INT:
2446 break;
2448 case CT_MEMORY:
2449 op_alt[i].memory_ok = 1;
2450 break;
2452 case CT_ADDRESS:
2453 op_alt[i].is_address = 1;
2454 op_alt[i].cl
2455 = (reg_class_subunion
2456 [(int) op_alt[i].cl]
2457 [(int) base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2458 ADDRESS, SCRATCH)]);
2459 break;
2461 case CT_FIXED_FORM:
2462 break;
2464 break;
2466 p += CONSTRAINT_LEN (c, p);
2472 /* Return an array of operand_alternative instructions for
2473 instruction ICODE. */
2475 const operand_alternative *
2476 preprocess_insn_constraints (int icode)
2478 gcc_checking_assert (IN_RANGE (icode, 0, LAST_INSN_CODE));
2479 if (this_target_recog->x_op_alt[icode])
2480 return this_target_recog->x_op_alt[icode];
2482 int n_operands = insn_data[icode].n_operands;
2483 if (n_operands == 0)
2484 return 0;
2485 /* Always provide at least one alternative so that which_op_alt ()
2486 works correctly. If the instruction has 0 alternatives (i.e. all
2487 constraint strings are empty) then each operand in this alternative
2488 will have anything_ok set. */
2489 int n_alternatives = MAX (insn_data[icode].n_alternatives, 1);
2490 int n_entries = n_operands * n_alternatives;
2492 operand_alternative *op_alt = XCNEWVEC (operand_alternative, n_entries);
2493 const char **constraints = XALLOCAVEC (const char *, n_operands);
2495 for (int i = 0; i < n_operands; ++i)
2496 constraints[i] = insn_data[icode].operand[i].constraint;
2497 preprocess_constraints (n_operands, n_alternatives, constraints, op_alt);
2499 this_target_recog->x_op_alt[icode] = op_alt;
2500 return op_alt;
2503 /* After calling extract_insn, you can use this function to extract some
2504 information from the constraint strings into a more usable form.
2505 The collected data is stored in recog_op_alt. */
2507 void
2508 preprocess_constraints (rtx_insn *insn)
2510 int icode = INSN_CODE (insn);
2511 if (icode >= 0)
2512 recog_op_alt = preprocess_insn_constraints (icode);
2513 else
2515 int n_operands = recog_data.n_operands;
2516 int n_alternatives = recog_data.n_alternatives;
2517 int n_entries = n_operands * n_alternatives;
2518 memset (asm_op_alt, 0, n_entries * sizeof (operand_alternative));
2519 preprocess_constraints (n_operands, n_alternatives,
2520 recog_data.constraints, asm_op_alt);
2521 recog_op_alt = asm_op_alt;
2525 /* Check the operands of an insn against the insn's operand constraints
2526 and return 1 if they match any of the alternatives in ALTERNATIVES.
2528 The information about the insn's operands, constraints, operand modes
2529 etc. is obtained from the global variables set up by extract_insn.
2531 WHICH_ALTERNATIVE is set to a number which indicates which
2532 alternative of constraints was matched: 0 for the first alternative,
2533 1 for the next, etc.
2535 In addition, when two operands are required to match
2536 and it happens that the output operand is (reg) while the
2537 input operand is --(reg) or ++(reg) (a pre-inc or pre-dec),
2538 make the output operand look like the input.
2539 This is because the output operand is the one the template will print.
2541 This is used in final, just before printing the assembler code and by
2542 the routines that determine an insn's attribute.
2544 If STRICT is a positive nonzero value, it means that we have been
2545 called after reload has been completed. In that case, we must
2546 do all checks strictly. If it is zero, it means that we have been called
2547 before reload has completed. In that case, we first try to see if we can
2548 find an alternative that matches strictly. If not, we try again, this
2549 time assuming that reload will fix up the insn. This provides a "best
2550 guess" for the alternative and is used to compute attributes of insns prior
2551 to reload. A negative value of STRICT is used for this internal call. */
2553 struct funny_match
2555 int this_op, other;
2559 constrain_operands (int strict, alternative_mask alternatives)
2561 const char *constraints[MAX_RECOG_OPERANDS];
2562 int matching_operands[MAX_RECOG_OPERANDS];
2563 int earlyclobber[MAX_RECOG_OPERANDS];
2564 int c;
2566 struct funny_match funny_match[MAX_RECOG_OPERANDS];
2567 int funny_match_index;
2569 which_alternative = 0;
2570 if (recog_data.n_operands == 0 || recog_data.n_alternatives == 0)
2571 return 1;
2573 for (c = 0; c < recog_data.n_operands; c++)
2575 constraints[c] = recog_data.constraints[c];
2576 matching_operands[c] = -1;
2581 int seen_earlyclobber_at = -1;
2582 int opno;
2583 int lose = 0;
2584 funny_match_index = 0;
2586 if (!TEST_BIT (alternatives, which_alternative))
2588 int i;
2590 for (i = 0; i < recog_data.n_operands; i++)
2591 constraints[i] = skip_alternative (constraints[i]);
2593 which_alternative++;
2594 continue;
2597 for (opno = 0; opno < recog_data.n_operands; opno++)
2599 rtx op = recog_data.operand[opno];
2600 machine_mode mode = GET_MODE (op);
2601 const char *p = constraints[opno];
2602 int offset = 0;
2603 int win = 0;
2604 int val;
2605 int len;
2607 earlyclobber[opno] = 0;
2609 /* A unary operator may be accepted by the predicate, but it
2610 is irrelevant for matching constraints. */
2611 if (UNARY_P (op))
2612 op = XEXP (op, 0);
2614 if (GET_CODE (op) == SUBREG)
2616 if (REG_P (SUBREG_REG (op))
2617 && REGNO (SUBREG_REG (op)) < FIRST_PSEUDO_REGISTER)
2618 offset = subreg_regno_offset (REGNO (SUBREG_REG (op)),
2619 GET_MODE (SUBREG_REG (op)),
2620 SUBREG_BYTE (op),
2621 GET_MODE (op));
2622 op = SUBREG_REG (op);
2625 /* An empty constraint or empty alternative
2626 allows anything which matched the pattern. */
2627 if (*p == 0 || *p == ',')
2628 win = 1;
2631 switch (c = *p, len = CONSTRAINT_LEN (c, p), c)
2633 case '\0':
2634 len = 0;
2635 break;
2636 case ',':
2637 c = '\0';
2638 break;
2640 case '#':
2641 /* Ignore rest of this alternative as far as
2642 constraint checking is concerned. */
2644 p++;
2645 while (*p && *p != ',');
2646 len = 0;
2647 break;
2649 case '&':
2650 earlyclobber[opno] = 1;
2651 if (seen_earlyclobber_at < 0)
2652 seen_earlyclobber_at = opno;
2653 break;
2655 case '0': case '1': case '2': case '3': case '4':
2656 case '5': case '6': case '7': case '8': case '9':
2658 /* This operand must be the same as a previous one.
2659 This kind of constraint is used for instructions such
2660 as add when they take only two operands.
2662 Note that the lower-numbered operand is passed first.
2664 If we are not testing strictly, assume that this
2665 constraint will be satisfied. */
2667 char *end;
2668 int match;
2670 match = strtoul (p, &end, 10);
2671 p = end;
2673 if (strict < 0)
2674 val = 1;
2675 else
2677 rtx op1 = recog_data.operand[match];
2678 rtx op2 = recog_data.operand[opno];
2680 /* A unary operator may be accepted by the predicate,
2681 but it is irrelevant for matching constraints. */
2682 if (UNARY_P (op1))
2683 op1 = XEXP (op1, 0);
2684 if (UNARY_P (op2))
2685 op2 = XEXP (op2, 0);
2687 val = operands_match_p (op1, op2);
2690 matching_operands[opno] = match;
2691 matching_operands[match] = opno;
2693 if (val != 0)
2694 win = 1;
2696 /* If output is *x and input is *--x, arrange later
2697 to change the output to *--x as well, since the
2698 output op is the one that will be printed. */
2699 if (val == 2 && strict > 0)
2701 funny_match[funny_match_index].this_op = opno;
2702 funny_match[funny_match_index++].other = match;
2705 len = 0;
2706 break;
2708 case 'p':
2709 /* p is used for address_operands. When we are called by
2710 gen_reload, no one will have checked that the address is
2711 strictly valid, i.e., that all pseudos requiring hard regs
2712 have gotten them. */
2713 if (strict <= 0
2714 || (strict_memory_address_p (recog_data.operand_mode[opno],
2715 op)))
2716 win = 1;
2717 break;
2719 /* No need to check general_operand again;
2720 it was done in insn-recog.c. Well, except that reload
2721 doesn't check the validity of its replacements, but
2722 that should only matter when there's a bug. */
2723 case 'g':
2724 /* Anything goes unless it is a REG and really has a hard reg
2725 but the hard reg is not in the class GENERAL_REGS. */
2726 if (REG_P (op))
2728 if (strict < 0
2729 || GENERAL_REGS == ALL_REGS
2730 || (reload_in_progress
2731 && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2732 || reg_fits_class_p (op, GENERAL_REGS, offset, mode))
2733 win = 1;
2735 else if (strict < 0 || general_operand (op, mode))
2736 win = 1;
2737 break;
2739 default:
2741 enum constraint_num cn = lookup_constraint (p);
2742 enum reg_class cl = reg_class_for_constraint (cn);
2743 if (cl != NO_REGS)
2745 if (strict < 0
2746 || (strict == 0
2747 && REG_P (op)
2748 && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2749 || (strict == 0 && GET_CODE (op) == SCRATCH)
2750 || (REG_P (op)
2751 && reg_fits_class_p (op, cl, offset, mode)))
2752 win = 1;
2755 else if (constraint_satisfied_p (op, cn))
2756 win = 1;
2758 else if (insn_extra_memory_constraint (cn)
2759 /* Every memory operand can be reloaded to fit. */
2760 && ((strict < 0 && MEM_P (op))
2761 /* Before reload, accept what reload can turn
2762 into a mem. */
2763 || (strict < 0 && CONSTANT_P (op))
2764 /* Before reload, accept a pseudo,
2765 since LRA can turn it into a mem. */
2766 || (strict < 0 && targetm.lra_p () && REG_P (op)
2767 && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2768 /* During reload, accept a pseudo */
2769 || (reload_in_progress && REG_P (op)
2770 && REGNO (op) >= FIRST_PSEUDO_REGISTER)))
2771 win = 1;
2772 else if (insn_extra_address_constraint (cn)
2773 /* Every address operand can be reloaded to fit. */
2774 && strict < 0)
2775 win = 1;
2776 /* Cater to architectures like IA-64 that define extra memory
2777 constraints without using define_memory_constraint. */
2778 else if (reload_in_progress
2779 && REG_P (op)
2780 && REGNO (op) >= FIRST_PSEUDO_REGISTER
2781 && reg_renumber[REGNO (op)] < 0
2782 && reg_equiv_mem (REGNO (op)) != 0
2783 && constraint_satisfied_p
2784 (reg_equiv_mem (REGNO (op)), cn))
2785 win = 1;
2786 break;
2789 while (p += len, c);
2791 constraints[opno] = p;
2792 /* If this operand did not win somehow,
2793 this alternative loses. */
2794 if (! win)
2795 lose = 1;
2797 /* This alternative won; the operands are ok.
2798 Change whichever operands this alternative says to change. */
2799 if (! lose)
2801 int opno, eopno;
2803 /* See if any earlyclobber operand conflicts with some other
2804 operand. */
2806 if (strict > 0 && seen_earlyclobber_at >= 0)
2807 for (eopno = seen_earlyclobber_at;
2808 eopno < recog_data.n_operands;
2809 eopno++)
2810 /* Ignore earlyclobber operands now in memory,
2811 because we would often report failure when we have
2812 two memory operands, one of which was formerly a REG. */
2813 if (earlyclobber[eopno]
2814 && REG_P (recog_data.operand[eopno]))
2815 for (opno = 0; opno < recog_data.n_operands; opno++)
2816 if ((MEM_P (recog_data.operand[opno])
2817 || recog_data.operand_type[opno] != OP_OUT)
2818 && opno != eopno
2819 /* Ignore things like match_operator operands. */
2820 && *recog_data.constraints[opno] != 0
2821 && ! (matching_operands[opno] == eopno
2822 && operands_match_p (recog_data.operand[opno],
2823 recog_data.operand[eopno]))
2824 && ! safe_from_earlyclobber (recog_data.operand[opno],
2825 recog_data.operand[eopno]))
2826 lose = 1;
2828 if (! lose)
2830 while (--funny_match_index >= 0)
2832 recog_data.operand[funny_match[funny_match_index].other]
2833 = recog_data.operand[funny_match[funny_match_index].this_op];
2836 #ifdef AUTO_INC_DEC
2837 /* For operands without < or > constraints reject side-effects. */
2838 if (recog_data.is_asm)
2840 for (opno = 0; opno < recog_data.n_operands; opno++)
2841 if (MEM_P (recog_data.operand[opno]))
2842 switch (GET_CODE (XEXP (recog_data.operand[opno], 0)))
2844 case PRE_INC:
2845 case POST_INC:
2846 case PRE_DEC:
2847 case POST_DEC:
2848 case PRE_MODIFY:
2849 case POST_MODIFY:
2850 if (strchr (recog_data.constraints[opno], '<') == NULL
2851 && strchr (recog_data.constraints[opno], '>')
2852 == NULL)
2853 return 0;
2854 break;
2855 default:
2856 break;
2859 #endif
2860 return 1;
2864 which_alternative++;
2866 while (which_alternative < recog_data.n_alternatives);
2868 which_alternative = -1;
2869 /* If we are about to reject this, but we are not to test strictly,
2870 try a very loose test. Only return failure if it fails also. */
2871 if (strict == 0)
2872 return constrain_operands (-1, alternatives);
2873 else
2874 return 0;
2877 /* Return true iff OPERAND (assumed to be a REG rtx)
2878 is a hard reg in class CLASS when its regno is offset by OFFSET
2879 and changed to mode MODE.
2880 If REG occupies multiple hard regs, all of them must be in CLASS. */
2882 bool
2883 reg_fits_class_p (const_rtx operand, reg_class_t cl, int offset,
2884 machine_mode mode)
2886 unsigned int regno = REGNO (operand);
2888 if (cl == NO_REGS)
2889 return false;
2891 /* Regno must not be a pseudo register. Offset may be negative. */
2892 return (HARD_REGISTER_NUM_P (regno)
2893 && HARD_REGISTER_NUM_P (regno + offset)
2894 && in_hard_reg_set_p (reg_class_contents[(int) cl], mode,
2895 regno + offset));
2898 /* Split single instruction. Helper function for split_all_insns and
2899 split_all_insns_noflow. Return last insn in the sequence if successful,
2900 or NULL if unsuccessful. */
2902 static rtx_insn *
2903 split_insn (rtx_insn *insn)
2905 /* Split insns here to get max fine-grain parallelism. */
2906 rtx_insn *first = PREV_INSN (insn);
2907 rtx_insn *last = try_split (PATTERN (insn), insn, 1);
2908 rtx insn_set, last_set, note;
2910 if (last == insn)
2911 return NULL;
2913 /* If the original instruction was a single set that was known to be
2914 equivalent to a constant, see if we can say the same about the last
2915 instruction in the split sequence. The two instructions must set
2916 the same destination. */
2917 insn_set = single_set (insn);
2918 if (insn_set)
2920 last_set = single_set (last);
2921 if (last_set && rtx_equal_p (SET_DEST (last_set), SET_DEST (insn_set)))
2923 note = find_reg_equal_equiv_note (insn);
2924 if (note && CONSTANT_P (XEXP (note, 0)))
2925 set_unique_reg_note (last, REG_EQUAL, XEXP (note, 0));
2926 else if (CONSTANT_P (SET_SRC (insn_set)))
2927 set_unique_reg_note (last, REG_EQUAL,
2928 copy_rtx (SET_SRC (insn_set)));
2932 /* try_split returns the NOTE that INSN became. */
2933 SET_INSN_DELETED (insn);
2935 /* ??? Coddle to md files that generate subregs in post-reload
2936 splitters instead of computing the proper hard register. */
2937 if (reload_completed && first != last)
2939 first = NEXT_INSN (first);
2940 for (;;)
2942 if (INSN_P (first))
2943 cleanup_subreg_operands (first);
2944 if (first == last)
2945 break;
2946 first = NEXT_INSN (first);
2950 return last;
2953 /* Split all insns in the function. If UPD_LIFE, update life info after. */
2955 void
2956 split_all_insns (void)
2958 sbitmap blocks;
2959 bool changed;
2960 basic_block bb;
2962 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
2963 bitmap_clear (blocks);
2964 changed = false;
2966 FOR_EACH_BB_REVERSE_FN (bb, cfun)
2968 rtx_insn *insn, *next;
2969 bool finish = false;
2971 rtl_profile_for_bb (bb);
2972 for (insn = BB_HEAD (bb); !finish ; insn = next)
2974 /* Can't use `next_real_insn' because that might go across
2975 CODE_LABELS and short-out basic blocks. */
2976 next = NEXT_INSN (insn);
2977 finish = (insn == BB_END (bb));
2978 if (INSN_P (insn))
2980 rtx set = single_set (insn);
2982 /* Don't split no-op move insns. These should silently
2983 disappear later in final. Splitting such insns would
2984 break the code that handles LIBCALL blocks. */
2985 if (set && set_noop_p (set))
2987 /* Nops get in the way while scheduling, so delete them
2988 now if register allocation has already been done. It
2989 is too risky to try to do this before register
2990 allocation, and there are unlikely to be very many
2991 nops then anyways. */
2992 if (reload_completed)
2993 delete_insn_and_edges (insn);
2995 else
2997 if (split_insn (insn))
2999 bitmap_set_bit (blocks, bb->index);
3000 changed = true;
3007 default_rtl_profile ();
3008 if (changed)
3009 find_many_sub_basic_blocks (blocks);
3011 #ifdef ENABLE_CHECKING
3012 verify_flow_info ();
3013 #endif
3015 sbitmap_free (blocks);
3018 /* Same as split_all_insns, but do not expect CFG to be available.
3019 Used by machine dependent reorg passes. */
3021 unsigned int
3022 split_all_insns_noflow (void)
3024 rtx_insn *next, *insn;
3026 for (insn = get_insns (); insn; insn = next)
3028 next = NEXT_INSN (insn);
3029 if (INSN_P (insn))
3031 /* Don't split no-op move insns. These should silently
3032 disappear later in final. Splitting such insns would
3033 break the code that handles LIBCALL blocks. */
3034 rtx set = single_set (insn);
3035 if (set && set_noop_p (set))
3037 /* Nops get in the way while scheduling, so delete them
3038 now if register allocation has already been done. It
3039 is too risky to try to do this before register
3040 allocation, and there are unlikely to be very many
3041 nops then anyways.
3043 ??? Should we use delete_insn when the CFG isn't valid? */
3044 if (reload_completed)
3045 delete_insn_and_edges (insn);
3047 else
3048 split_insn (insn);
3051 return 0;
3054 #ifdef HAVE_peephole2
3055 struct peep2_insn_data
3057 rtx_insn *insn;
3058 regset live_before;
3061 static struct peep2_insn_data peep2_insn_data[MAX_INSNS_PER_PEEP2 + 1];
3062 static int peep2_current;
3064 static bool peep2_do_rebuild_jump_labels;
3065 static bool peep2_do_cleanup_cfg;
3067 /* The number of instructions available to match a peep2. */
3068 int peep2_current_count;
3070 /* A marker indicating the last insn of the block. The live_before regset
3071 for this element is correct, indicating DF_LIVE_OUT for the block. */
3072 #define PEEP2_EOB invalid_insn_rtx
3074 /* Wrap N to fit into the peep2_insn_data buffer. */
3076 static int
3077 peep2_buf_position (int n)
3079 if (n >= MAX_INSNS_PER_PEEP2 + 1)
3080 n -= MAX_INSNS_PER_PEEP2 + 1;
3081 return n;
3084 /* Return the Nth non-note insn after `current', or return NULL_RTX if it
3085 does not exist. Used by the recognizer to find the next insn to match
3086 in a multi-insn pattern. */
3089 peep2_next_insn (int n)
3091 gcc_assert (n <= peep2_current_count);
3093 n = peep2_buf_position (peep2_current + n);
3095 return peep2_insn_data[n].insn;
3098 /* Return true if REGNO is dead before the Nth non-note insn
3099 after `current'. */
3102 peep2_regno_dead_p (int ofs, int regno)
3104 gcc_assert (ofs < MAX_INSNS_PER_PEEP2 + 1);
3106 ofs = peep2_buf_position (peep2_current + ofs);
3108 gcc_assert (peep2_insn_data[ofs].insn != NULL_RTX);
3110 return ! REGNO_REG_SET_P (peep2_insn_data[ofs].live_before, regno);
3113 /* Similarly for a REG. */
3116 peep2_reg_dead_p (int ofs, rtx reg)
3118 gcc_assert (ofs < MAX_INSNS_PER_PEEP2 + 1);
3120 ofs = peep2_buf_position (peep2_current + ofs);
3122 gcc_assert (peep2_insn_data[ofs].insn != NULL_RTX);
3124 unsigned int end_regno = END_REGNO (reg);
3125 for (unsigned int regno = REGNO (reg); regno < end_regno; ++regno)
3126 if (REGNO_REG_SET_P (peep2_insn_data[ofs].live_before, regno))
3127 return 0;
3128 return 1;
3131 /* Regno offset to be used in the register search. */
3132 static int search_ofs;
3134 /* Try to find a hard register of mode MODE, matching the register class in
3135 CLASS_STR, which is available at the beginning of insn CURRENT_INSN and
3136 remains available until the end of LAST_INSN. LAST_INSN may be NULL_RTX,
3137 in which case the only condition is that the register must be available
3138 before CURRENT_INSN.
3139 Registers that already have bits set in REG_SET will not be considered.
3141 If an appropriate register is available, it will be returned and the
3142 corresponding bit(s) in REG_SET will be set; otherwise, NULL_RTX is
3143 returned. */
3146 peep2_find_free_register (int from, int to, const char *class_str,
3147 machine_mode mode, HARD_REG_SET *reg_set)
3149 enum reg_class cl;
3150 HARD_REG_SET live;
3151 df_ref def;
3152 int i;
3154 gcc_assert (from < MAX_INSNS_PER_PEEP2 + 1);
3155 gcc_assert (to < MAX_INSNS_PER_PEEP2 + 1);
3157 from = peep2_buf_position (peep2_current + from);
3158 to = peep2_buf_position (peep2_current + to);
3160 gcc_assert (peep2_insn_data[from].insn != NULL_RTX);
3161 REG_SET_TO_HARD_REG_SET (live, peep2_insn_data[from].live_before);
3163 while (from != to)
3165 gcc_assert (peep2_insn_data[from].insn != NULL_RTX);
3167 /* Don't use registers set or clobbered by the insn. */
3168 FOR_EACH_INSN_DEF (def, peep2_insn_data[from].insn)
3169 SET_HARD_REG_BIT (live, DF_REF_REGNO (def));
3171 from = peep2_buf_position (from + 1);
3174 cl = reg_class_for_constraint (lookup_constraint (class_str));
3176 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3178 int raw_regno, regno, success, j;
3180 /* Distribute the free registers as much as possible. */
3181 raw_regno = search_ofs + i;
3182 if (raw_regno >= FIRST_PSEUDO_REGISTER)
3183 raw_regno -= FIRST_PSEUDO_REGISTER;
3184 #ifdef REG_ALLOC_ORDER
3185 regno = reg_alloc_order[raw_regno];
3186 #else
3187 regno = raw_regno;
3188 #endif
3190 /* Can it support the mode we need? */
3191 if (! HARD_REGNO_MODE_OK (regno, mode))
3192 continue;
3194 success = 1;
3195 for (j = 0; success && j < hard_regno_nregs[regno][mode]; j++)
3197 /* Don't allocate fixed registers. */
3198 if (fixed_regs[regno + j])
3200 success = 0;
3201 break;
3203 /* Don't allocate global registers. */
3204 if (global_regs[regno + j])
3206 success = 0;
3207 break;
3209 /* Make sure the register is of the right class. */
3210 if (! TEST_HARD_REG_BIT (reg_class_contents[cl], regno + j))
3212 success = 0;
3213 break;
3215 /* And that we don't create an extra save/restore. */
3216 if (! call_used_regs[regno + j] && ! df_regs_ever_live_p (regno + j))
3218 success = 0;
3219 break;
3222 if (! targetm.hard_regno_scratch_ok (regno + j))
3224 success = 0;
3225 break;
3228 /* And we don't clobber traceback for noreturn functions. */
3229 if ((regno + j == FRAME_POINTER_REGNUM
3230 || regno + j == HARD_FRAME_POINTER_REGNUM)
3231 && (! reload_completed || frame_pointer_needed))
3233 success = 0;
3234 break;
3237 if (TEST_HARD_REG_BIT (*reg_set, regno + j)
3238 || TEST_HARD_REG_BIT (live, regno + j))
3240 success = 0;
3241 break;
3245 if (success)
3247 add_to_hard_reg_set (reg_set, mode, regno);
3249 /* Start the next search with the next register. */
3250 if (++raw_regno >= FIRST_PSEUDO_REGISTER)
3251 raw_regno = 0;
3252 search_ofs = raw_regno;
3254 return gen_rtx_REG (mode, regno);
3258 search_ofs = 0;
3259 return NULL_RTX;
3262 /* Forget all currently tracked instructions, only remember current
3263 LIVE regset. */
3265 static void
3266 peep2_reinit_state (regset live)
3268 int i;
3270 /* Indicate that all slots except the last holds invalid data. */
3271 for (i = 0; i < MAX_INSNS_PER_PEEP2; ++i)
3272 peep2_insn_data[i].insn = NULL;
3273 peep2_current_count = 0;
3275 /* Indicate that the last slot contains live_after data. */
3276 peep2_insn_data[MAX_INSNS_PER_PEEP2].insn = PEEP2_EOB;
3277 peep2_current = MAX_INSNS_PER_PEEP2;
3279 COPY_REG_SET (peep2_insn_data[MAX_INSNS_PER_PEEP2].live_before, live);
3282 /* While scanning basic block BB, we found a match of length MATCH_LEN,
3283 starting at INSN. Perform the replacement, removing the old insns and
3284 replacing them with ATTEMPT. Returns the last insn emitted, or NULL
3285 if the replacement is rejected. */
3287 static rtx_insn *
3288 peep2_attempt (basic_block bb, rtx_insn *insn, int match_len, rtx_insn *attempt)
3290 int i;
3291 rtx_insn *last, *before_try, *x;
3292 rtx eh_note, as_note;
3293 rtx_insn *old_insn;
3294 rtx_insn *new_insn;
3295 bool was_call = false;
3297 /* If we are splitting an RTX_FRAME_RELATED_P insn, do not allow it to
3298 match more than one insn, or to be split into more than one insn. */
3299 old_insn = peep2_insn_data[peep2_current].insn;
3300 if (RTX_FRAME_RELATED_P (old_insn))
3302 bool any_note = false;
3303 rtx note;
3305 if (match_len != 0)
3306 return NULL;
3308 /* Look for one "active" insn. I.e. ignore any "clobber" insns that
3309 may be in the stream for the purpose of register allocation. */
3310 if (active_insn_p (attempt))
3311 new_insn = attempt;
3312 else
3313 new_insn = next_active_insn (attempt);
3314 if (next_active_insn (new_insn))
3315 return NULL;
3317 /* We have a 1-1 replacement. Copy over any frame-related info. */
3318 RTX_FRAME_RELATED_P (new_insn) = 1;
3320 /* Allow the backend to fill in a note during the split. */
3321 for (note = REG_NOTES (new_insn); note ; note = XEXP (note, 1))
3322 switch (REG_NOTE_KIND (note))
3324 case REG_FRAME_RELATED_EXPR:
3325 case REG_CFA_DEF_CFA:
3326 case REG_CFA_ADJUST_CFA:
3327 case REG_CFA_OFFSET:
3328 case REG_CFA_REGISTER:
3329 case REG_CFA_EXPRESSION:
3330 case REG_CFA_RESTORE:
3331 case REG_CFA_SET_VDRAP:
3332 any_note = true;
3333 break;
3334 default:
3335 break;
3338 /* If the backend didn't supply a note, copy one over. */
3339 if (!any_note)
3340 for (note = REG_NOTES (old_insn); note ; note = XEXP (note, 1))
3341 switch (REG_NOTE_KIND (note))
3343 case REG_FRAME_RELATED_EXPR:
3344 case REG_CFA_DEF_CFA:
3345 case REG_CFA_ADJUST_CFA:
3346 case REG_CFA_OFFSET:
3347 case REG_CFA_REGISTER:
3348 case REG_CFA_EXPRESSION:
3349 case REG_CFA_RESTORE:
3350 case REG_CFA_SET_VDRAP:
3351 add_reg_note (new_insn, REG_NOTE_KIND (note), XEXP (note, 0));
3352 any_note = true;
3353 break;
3354 default:
3355 break;
3358 /* If there still isn't a note, make sure the unwind info sees the
3359 same expression as before the split. */
3360 if (!any_note)
3362 rtx old_set, new_set;
3364 /* The old insn had better have been simple, or annotated. */
3365 old_set = single_set (old_insn);
3366 gcc_assert (old_set != NULL);
3368 new_set = single_set (new_insn);
3369 if (!new_set || !rtx_equal_p (new_set, old_set))
3370 add_reg_note (new_insn, REG_FRAME_RELATED_EXPR, old_set);
3373 /* Copy prologue/epilogue status. This is required in order to keep
3374 proper placement of EPILOGUE_BEG and the DW_CFA_remember_state. */
3375 maybe_copy_prologue_epilogue_insn (old_insn, new_insn);
3378 /* If we are splitting a CALL_INSN, look for the CALL_INSN
3379 in SEQ and copy our CALL_INSN_FUNCTION_USAGE and other
3380 cfg-related call notes. */
3381 for (i = 0; i <= match_len; ++i)
3383 int j;
3384 rtx note;
3386 j = peep2_buf_position (peep2_current + i);
3387 old_insn = peep2_insn_data[j].insn;
3388 if (!CALL_P (old_insn))
3389 continue;
3390 was_call = true;
3392 new_insn = attempt;
3393 while (new_insn != NULL_RTX)
3395 if (CALL_P (new_insn))
3396 break;
3397 new_insn = NEXT_INSN (new_insn);
3400 gcc_assert (new_insn != NULL_RTX);
3402 CALL_INSN_FUNCTION_USAGE (new_insn)
3403 = CALL_INSN_FUNCTION_USAGE (old_insn);
3404 SIBLING_CALL_P (new_insn) = SIBLING_CALL_P (old_insn);
3406 for (note = REG_NOTES (old_insn);
3407 note;
3408 note = XEXP (note, 1))
3409 switch (REG_NOTE_KIND (note))
3411 case REG_NORETURN:
3412 case REG_SETJMP:
3413 case REG_TM:
3414 add_reg_note (new_insn, REG_NOTE_KIND (note),
3415 XEXP (note, 0));
3416 break;
3417 default:
3418 /* Discard all other reg notes. */
3419 break;
3422 /* Croak if there is another call in the sequence. */
3423 while (++i <= match_len)
3425 j = peep2_buf_position (peep2_current + i);
3426 old_insn = peep2_insn_data[j].insn;
3427 gcc_assert (!CALL_P (old_insn));
3429 break;
3432 /* If we matched any instruction that had a REG_ARGS_SIZE, then
3433 move those notes over to the new sequence. */
3434 as_note = NULL;
3435 for (i = match_len; i >= 0; --i)
3437 int j = peep2_buf_position (peep2_current + i);
3438 old_insn = peep2_insn_data[j].insn;
3440 as_note = find_reg_note (old_insn, REG_ARGS_SIZE, NULL);
3441 if (as_note)
3442 break;
3445 i = peep2_buf_position (peep2_current + match_len);
3446 eh_note = find_reg_note (peep2_insn_data[i].insn, REG_EH_REGION, NULL_RTX);
3448 /* Replace the old sequence with the new. */
3449 rtx_insn *peepinsn = peep2_insn_data[i].insn;
3450 last = emit_insn_after_setloc (attempt,
3451 peep2_insn_data[i].insn,
3452 INSN_LOCATION (peepinsn));
3453 before_try = PREV_INSN (insn);
3454 delete_insn_chain (insn, peep2_insn_data[i].insn, false);
3456 /* Re-insert the EH_REGION notes. */
3457 if (eh_note || (was_call && nonlocal_goto_handler_labels))
3459 edge eh_edge;
3460 edge_iterator ei;
3462 FOR_EACH_EDGE (eh_edge, ei, bb->succs)
3463 if (eh_edge->flags & (EDGE_EH | EDGE_ABNORMAL_CALL))
3464 break;
3466 if (eh_note)
3467 copy_reg_eh_region_note_backward (eh_note, last, before_try);
3469 if (eh_edge)
3470 for (x = last; x != before_try; x = PREV_INSN (x))
3471 if (x != BB_END (bb)
3472 && (can_throw_internal (x)
3473 || can_nonlocal_goto (x)))
3475 edge nfte, nehe;
3476 int flags;
3478 nfte = split_block (bb, x);
3479 flags = (eh_edge->flags
3480 & (EDGE_EH | EDGE_ABNORMAL));
3481 if (CALL_P (x))
3482 flags |= EDGE_ABNORMAL_CALL;
3483 nehe = make_edge (nfte->src, eh_edge->dest,
3484 flags);
3486 nehe->probability = eh_edge->probability;
3487 nfte->probability
3488 = REG_BR_PROB_BASE - nehe->probability;
3490 peep2_do_cleanup_cfg |= purge_dead_edges (nfte->dest);
3491 bb = nfte->src;
3492 eh_edge = nehe;
3495 /* Converting possibly trapping insn to non-trapping is
3496 possible. Zap dummy outgoing edges. */
3497 peep2_do_cleanup_cfg |= purge_dead_edges (bb);
3500 /* Re-insert the ARGS_SIZE notes. */
3501 if (as_note)
3502 fixup_args_size_notes (before_try, last, INTVAL (XEXP (as_note, 0)));
3504 /* If we generated a jump instruction, it won't have
3505 JUMP_LABEL set. Recompute after we're done. */
3506 for (x = last; x != before_try; x = PREV_INSN (x))
3507 if (JUMP_P (x))
3509 peep2_do_rebuild_jump_labels = true;
3510 break;
3513 return last;
3516 /* After performing a replacement in basic block BB, fix up the life
3517 information in our buffer. LAST is the last of the insns that we
3518 emitted as a replacement. PREV is the insn before the start of
3519 the replacement. MATCH_LEN is the number of instructions that were
3520 matched, and which now need to be replaced in the buffer. */
3522 static void
3523 peep2_update_life (basic_block bb, int match_len, rtx_insn *last,
3524 rtx_insn *prev)
3526 int i = peep2_buf_position (peep2_current + match_len + 1);
3527 rtx_insn *x;
3528 regset_head live;
3530 INIT_REG_SET (&live);
3531 COPY_REG_SET (&live, peep2_insn_data[i].live_before);
3533 gcc_assert (peep2_current_count >= match_len + 1);
3534 peep2_current_count -= match_len + 1;
3536 x = last;
3539 if (INSN_P (x))
3541 df_insn_rescan (x);
3542 if (peep2_current_count < MAX_INSNS_PER_PEEP2)
3544 peep2_current_count++;
3545 if (--i < 0)
3546 i = MAX_INSNS_PER_PEEP2;
3547 peep2_insn_data[i].insn = x;
3548 df_simulate_one_insn_backwards (bb, x, &live);
3549 COPY_REG_SET (peep2_insn_data[i].live_before, &live);
3552 x = PREV_INSN (x);
3554 while (x != prev);
3555 CLEAR_REG_SET (&live);
3557 peep2_current = i;
3560 /* Add INSN, which is in BB, at the end of the peep2 insn buffer if possible.
3561 Return true if we added it, false otherwise. The caller will try to match
3562 peepholes against the buffer if we return false; otherwise it will try to
3563 add more instructions to the buffer. */
3565 static bool
3566 peep2_fill_buffer (basic_block bb, rtx_insn *insn, regset live)
3568 int pos;
3570 /* Once we have filled the maximum number of insns the buffer can hold,
3571 allow the caller to match the insns against peepholes. We wait until
3572 the buffer is full in case the target has similar peepholes of different
3573 length; we always want to match the longest if possible. */
3574 if (peep2_current_count == MAX_INSNS_PER_PEEP2)
3575 return false;
3577 /* If an insn has RTX_FRAME_RELATED_P set, do not allow it to be matched with
3578 any other pattern, lest it change the semantics of the frame info. */
3579 if (RTX_FRAME_RELATED_P (insn))
3581 /* Let the buffer drain first. */
3582 if (peep2_current_count > 0)
3583 return false;
3584 /* Now the insn will be the only thing in the buffer. */
3587 pos = peep2_buf_position (peep2_current + peep2_current_count);
3588 peep2_insn_data[pos].insn = insn;
3589 COPY_REG_SET (peep2_insn_data[pos].live_before, live);
3590 peep2_current_count++;
3592 df_simulate_one_insn_forwards (bb, insn, live);
3593 return true;
3596 /* Perform the peephole2 optimization pass. */
3598 static void
3599 peephole2_optimize (void)
3601 rtx_insn *insn;
3602 bitmap live;
3603 int i;
3604 basic_block bb;
3606 peep2_do_cleanup_cfg = false;
3607 peep2_do_rebuild_jump_labels = false;
3609 df_set_flags (DF_LR_RUN_DCE);
3610 df_note_add_problem ();
3611 df_analyze ();
3613 /* Initialize the regsets we're going to use. */
3614 for (i = 0; i < MAX_INSNS_PER_PEEP2 + 1; ++i)
3615 peep2_insn_data[i].live_before = BITMAP_ALLOC (&reg_obstack);
3616 search_ofs = 0;
3617 live = BITMAP_ALLOC (&reg_obstack);
3619 FOR_EACH_BB_REVERSE_FN (bb, cfun)
3621 bool past_end = false;
3622 int pos;
3624 rtl_profile_for_bb (bb);
3626 /* Start up propagation. */
3627 bitmap_copy (live, DF_LR_IN (bb));
3628 df_simulate_initialize_forwards (bb, live);
3629 peep2_reinit_state (live);
3631 insn = BB_HEAD (bb);
3632 for (;;)
3634 rtx_insn *attempt, *head;
3635 int match_len;
3637 if (!past_end && !NONDEBUG_INSN_P (insn))
3639 next_insn:
3640 insn = NEXT_INSN (insn);
3641 if (insn == NEXT_INSN (BB_END (bb)))
3642 past_end = true;
3643 continue;
3645 if (!past_end && peep2_fill_buffer (bb, insn, live))
3646 goto next_insn;
3648 /* If we did not fill an empty buffer, it signals the end of the
3649 block. */
3650 if (peep2_current_count == 0)
3651 break;
3653 /* The buffer filled to the current maximum, so try to match. */
3655 pos = peep2_buf_position (peep2_current + peep2_current_count);
3656 peep2_insn_data[pos].insn = PEEP2_EOB;
3657 COPY_REG_SET (peep2_insn_data[pos].live_before, live);
3659 /* Match the peephole. */
3660 head = peep2_insn_data[peep2_current].insn;
3661 attempt = safe_as_a <rtx_insn *> (
3662 peephole2_insns (PATTERN (head), head, &match_len));
3663 if (attempt != NULL)
3665 rtx_insn *last = peep2_attempt (bb, head, match_len, attempt);
3666 if (last)
3668 peep2_update_life (bb, match_len, last, PREV_INSN (attempt));
3669 continue;
3673 /* No match: advance the buffer by one insn. */
3674 peep2_current = peep2_buf_position (peep2_current + 1);
3675 peep2_current_count--;
3679 default_rtl_profile ();
3680 for (i = 0; i < MAX_INSNS_PER_PEEP2 + 1; ++i)
3681 BITMAP_FREE (peep2_insn_data[i].live_before);
3682 BITMAP_FREE (live);
3683 if (peep2_do_rebuild_jump_labels)
3684 rebuild_jump_labels (get_insns ());
3685 if (peep2_do_cleanup_cfg)
3686 cleanup_cfg (CLEANUP_CFG_CHANGED);
3688 #endif /* HAVE_peephole2 */
3690 /* Common predicates for use with define_bypass. */
3692 /* True if the dependency between OUT_INSN and IN_INSN is on the store
3693 data not the address operand(s) of the store. IN_INSN and OUT_INSN
3694 must be either a single_set or a PARALLEL with SETs inside. */
3697 store_data_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
3699 rtx out_set, in_set;
3700 rtx out_pat, in_pat;
3701 rtx out_exp, in_exp;
3702 int i, j;
3704 in_set = single_set (in_insn);
3705 if (in_set)
3707 if (!MEM_P (SET_DEST (in_set)))
3708 return false;
3710 out_set = single_set (out_insn);
3711 if (out_set)
3713 if (reg_mentioned_p (SET_DEST (out_set), SET_DEST (in_set)))
3714 return false;
3716 else
3718 out_pat = PATTERN (out_insn);
3720 if (GET_CODE (out_pat) != PARALLEL)
3721 return false;
3723 for (i = 0; i < XVECLEN (out_pat, 0); i++)
3725 out_exp = XVECEXP (out_pat, 0, i);
3727 if (GET_CODE (out_exp) == CLOBBER)
3728 continue;
3730 gcc_assert (GET_CODE (out_exp) == SET);
3732 if (reg_mentioned_p (SET_DEST (out_exp), SET_DEST (in_set)))
3733 return false;
3737 else
3739 in_pat = PATTERN (in_insn);
3740 gcc_assert (GET_CODE (in_pat) == PARALLEL);
3742 for (i = 0; i < XVECLEN (in_pat, 0); i++)
3744 in_exp = XVECEXP (in_pat, 0, i);
3746 if (GET_CODE (in_exp) == CLOBBER)
3747 continue;
3749 gcc_assert (GET_CODE (in_exp) == SET);
3751 if (!MEM_P (SET_DEST (in_exp)))
3752 return false;
3754 out_set = single_set (out_insn);
3755 if (out_set)
3757 if (reg_mentioned_p (SET_DEST (out_set), SET_DEST (in_exp)))
3758 return false;
3760 else
3762 out_pat = PATTERN (out_insn);
3763 gcc_assert (GET_CODE (out_pat) == PARALLEL);
3765 for (j = 0; j < XVECLEN (out_pat, 0); j++)
3767 out_exp = XVECEXP (out_pat, 0, j);
3769 if (GET_CODE (out_exp) == CLOBBER)
3770 continue;
3772 gcc_assert (GET_CODE (out_exp) == SET);
3774 if (reg_mentioned_p (SET_DEST (out_exp), SET_DEST (in_exp)))
3775 return false;
3781 return true;
3784 /* True if the dependency between OUT_INSN and IN_INSN is in the IF_THEN_ELSE
3785 condition, and not the THEN or ELSE branch. OUT_INSN may be either a single
3786 or multiple set; IN_INSN should be single_set for truth, but for convenience
3787 of insn categorization may be any JUMP or CALL insn. */
3790 if_test_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
3792 rtx out_set, in_set;
3794 in_set = single_set (in_insn);
3795 if (! in_set)
3797 gcc_assert (JUMP_P (in_insn) || CALL_P (in_insn));
3798 return false;
3801 if (GET_CODE (SET_SRC (in_set)) != IF_THEN_ELSE)
3802 return false;
3803 in_set = SET_SRC (in_set);
3805 out_set = single_set (out_insn);
3806 if (out_set)
3808 if (reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 1))
3809 || reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 2)))
3810 return false;
3812 else
3814 rtx out_pat;
3815 int i;
3817 out_pat = PATTERN (out_insn);
3818 gcc_assert (GET_CODE (out_pat) == PARALLEL);
3820 for (i = 0; i < XVECLEN (out_pat, 0); i++)
3822 rtx exp = XVECEXP (out_pat, 0, i);
3824 if (GET_CODE (exp) == CLOBBER)
3825 continue;
3827 gcc_assert (GET_CODE (exp) == SET);
3829 if (reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 1))
3830 || reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 2)))
3831 return false;
3835 return true;
3838 static unsigned int
3839 rest_of_handle_peephole2 (void)
3841 #ifdef HAVE_peephole2
3842 peephole2_optimize ();
3843 #endif
3844 return 0;
3847 namespace {
3849 const pass_data pass_data_peephole2 =
3851 RTL_PASS, /* type */
3852 "peephole2", /* name */
3853 OPTGROUP_NONE, /* optinfo_flags */
3854 TV_PEEPHOLE2, /* tv_id */
3855 0, /* properties_required */
3856 0, /* properties_provided */
3857 0, /* properties_destroyed */
3858 0, /* todo_flags_start */
3859 TODO_df_finish, /* todo_flags_finish */
3862 class pass_peephole2 : public rtl_opt_pass
3864 public:
3865 pass_peephole2 (gcc::context *ctxt)
3866 : rtl_opt_pass (pass_data_peephole2, ctxt)
3869 /* opt_pass methods: */
3870 /* The epiphany backend creates a second instance of this pass, so we need
3871 a clone method. */
3872 opt_pass * clone () { return new pass_peephole2 (m_ctxt); }
3873 virtual bool gate (function *) { return (optimize > 0 && flag_peephole2); }
3874 virtual unsigned int execute (function *)
3876 return rest_of_handle_peephole2 ();
3879 }; // class pass_peephole2
3881 } // anon namespace
3883 rtl_opt_pass *
3884 make_pass_peephole2 (gcc::context *ctxt)
3886 return new pass_peephole2 (ctxt);
3889 namespace {
3891 const pass_data pass_data_split_all_insns =
3893 RTL_PASS, /* type */
3894 "split1", /* name */
3895 OPTGROUP_NONE, /* optinfo_flags */
3896 TV_NONE, /* tv_id */
3897 0, /* properties_required */
3898 0, /* properties_provided */
3899 0, /* properties_destroyed */
3900 0, /* todo_flags_start */
3901 0, /* todo_flags_finish */
3904 class pass_split_all_insns : public rtl_opt_pass
3906 public:
3907 pass_split_all_insns (gcc::context *ctxt)
3908 : rtl_opt_pass (pass_data_split_all_insns, ctxt)
3911 /* opt_pass methods: */
3912 /* The epiphany backend creates a second instance of this pass, so
3913 we need a clone method. */
3914 opt_pass * clone () { return new pass_split_all_insns (m_ctxt); }
3915 virtual unsigned int execute (function *)
3917 split_all_insns ();
3918 return 0;
3921 }; // class pass_split_all_insns
3923 } // anon namespace
3925 rtl_opt_pass *
3926 make_pass_split_all_insns (gcc::context *ctxt)
3928 return new pass_split_all_insns (ctxt);
3931 static unsigned int
3932 rest_of_handle_split_after_reload (void)
3934 /* If optimizing, then go ahead and split insns now. */
3935 #ifndef STACK_REGS
3936 if (optimize > 0)
3937 #endif
3938 split_all_insns ();
3939 return 0;
3942 namespace {
3944 const pass_data pass_data_split_after_reload =
3946 RTL_PASS, /* type */
3947 "split2", /* name */
3948 OPTGROUP_NONE, /* optinfo_flags */
3949 TV_NONE, /* tv_id */
3950 0, /* properties_required */
3951 0, /* properties_provided */
3952 0, /* properties_destroyed */
3953 0, /* todo_flags_start */
3954 0, /* todo_flags_finish */
3957 class pass_split_after_reload : public rtl_opt_pass
3959 public:
3960 pass_split_after_reload (gcc::context *ctxt)
3961 : rtl_opt_pass (pass_data_split_after_reload, ctxt)
3964 /* opt_pass methods: */
3965 virtual unsigned int execute (function *)
3967 return rest_of_handle_split_after_reload ();
3970 }; // class pass_split_after_reload
3972 } // anon namespace
3974 rtl_opt_pass *
3975 make_pass_split_after_reload (gcc::context *ctxt)
3977 return new pass_split_after_reload (ctxt);
3980 namespace {
3982 const pass_data pass_data_split_before_regstack =
3984 RTL_PASS, /* type */
3985 "split3", /* name */
3986 OPTGROUP_NONE, /* optinfo_flags */
3987 TV_NONE, /* tv_id */
3988 0, /* properties_required */
3989 0, /* properties_provided */
3990 0, /* properties_destroyed */
3991 0, /* todo_flags_start */
3992 0, /* todo_flags_finish */
3995 class pass_split_before_regstack : public rtl_opt_pass
3997 public:
3998 pass_split_before_regstack (gcc::context *ctxt)
3999 : rtl_opt_pass (pass_data_split_before_regstack, ctxt)
4002 /* opt_pass methods: */
4003 virtual bool gate (function *);
4004 virtual unsigned int execute (function *)
4006 split_all_insns ();
4007 return 0;
4010 }; // class pass_split_before_regstack
4012 bool
4013 pass_split_before_regstack::gate (function *)
4015 #if HAVE_ATTR_length && defined (STACK_REGS)
4016 /* If flow2 creates new instructions which need splitting
4017 and scheduling after reload is not done, they might not be
4018 split until final which doesn't allow splitting
4019 if HAVE_ATTR_length. */
4020 # ifdef INSN_SCHEDULING
4021 return (optimize && !flag_schedule_insns_after_reload);
4022 # else
4023 return (optimize);
4024 # endif
4025 #else
4026 return 0;
4027 #endif
4030 } // anon namespace
4032 rtl_opt_pass *
4033 make_pass_split_before_regstack (gcc::context *ctxt)
4035 return new pass_split_before_regstack (ctxt);
4038 static unsigned int
4039 rest_of_handle_split_before_sched2 (void)
4041 #ifdef INSN_SCHEDULING
4042 split_all_insns ();
4043 #endif
4044 return 0;
4047 namespace {
4049 const pass_data pass_data_split_before_sched2 =
4051 RTL_PASS, /* type */
4052 "split4", /* name */
4053 OPTGROUP_NONE, /* optinfo_flags */
4054 TV_NONE, /* tv_id */
4055 0, /* properties_required */
4056 0, /* properties_provided */
4057 0, /* properties_destroyed */
4058 0, /* todo_flags_start */
4059 0, /* todo_flags_finish */
4062 class pass_split_before_sched2 : public rtl_opt_pass
4064 public:
4065 pass_split_before_sched2 (gcc::context *ctxt)
4066 : rtl_opt_pass (pass_data_split_before_sched2, ctxt)
4069 /* opt_pass methods: */
4070 virtual bool gate (function *)
4072 #ifdef INSN_SCHEDULING
4073 return optimize > 0 && flag_schedule_insns_after_reload;
4074 #else
4075 return false;
4076 #endif
4079 virtual unsigned int execute (function *)
4081 return rest_of_handle_split_before_sched2 ();
4084 }; // class pass_split_before_sched2
4086 } // anon namespace
4088 rtl_opt_pass *
4089 make_pass_split_before_sched2 (gcc::context *ctxt)
4091 return new pass_split_before_sched2 (ctxt);
4094 namespace {
4096 const pass_data pass_data_split_for_shorten_branches =
4098 RTL_PASS, /* type */
4099 "split5", /* name */
4100 OPTGROUP_NONE, /* optinfo_flags */
4101 TV_NONE, /* tv_id */
4102 0, /* properties_required */
4103 0, /* properties_provided */
4104 0, /* properties_destroyed */
4105 0, /* todo_flags_start */
4106 0, /* todo_flags_finish */
4109 class pass_split_for_shorten_branches : public rtl_opt_pass
4111 public:
4112 pass_split_for_shorten_branches (gcc::context *ctxt)
4113 : rtl_opt_pass (pass_data_split_for_shorten_branches, ctxt)
4116 /* opt_pass methods: */
4117 virtual bool gate (function *)
4119 /* The placement of the splitting that we do for shorten_branches
4120 depends on whether regstack is used by the target or not. */
4121 #if HAVE_ATTR_length && !defined (STACK_REGS)
4122 return true;
4123 #else
4124 return false;
4125 #endif
4128 virtual unsigned int execute (function *)
4130 return split_all_insns_noflow ();
4133 }; // class pass_split_for_shorten_branches
4135 } // anon namespace
4137 rtl_opt_pass *
4138 make_pass_split_for_shorten_branches (gcc::context *ctxt)
4140 return new pass_split_for_shorten_branches (ctxt);
4143 /* (Re)initialize the target information after a change in target. */
4145 void
4146 recog_init ()
4148 /* The information is zero-initialized, so we don't need to do anything
4149 first time round. */
4150 if (!this_target_recog->x_initialized)
4152 this_target_recog->x_initialized = true;
4153 return;
4155 memset (this_target_recog->x_bool_attr_masks, 0,
4156 sizeof (this_target_recog->x_bool_attr_masks));
4157 for (int i = 0; i < LAST_INSN_CODE; ++i)
4158 if (this_target_recog->x_op_alt[i])
4160 free (this_target_recog->x_op_alt[i]);
4161 this_target_recog->x_op_alt[i] = 0;