Fix bootstrap failure.
[official-gcc.git] / gcc / recog.c
blob410edb17d19e713de01833549e02359036d40a16
1 /* Subroutines used by or related to instruction recognition.
2 Copyright (C) 1987-2014 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 "tree.h"
26 #include "rtl-error.h"
27 #include "tm_p.h"
28 #include "insn-config.h"
29 #include "insn-attr.h"
30 #include "hard-reg-set.h"
31 #include "recog.h"
32 #include "regs.h"
33 #include "addresses.h"
34 #include "expr.h"
35 #include "hashtab.h"
36 #include "hash-set.h"
37 #include "vec.h"
38 #include "machmode.h"
39 #include "input.h"
40 #include "function.h"
41 #include "flags.h"
42 #include "predict.h"
43 #include "dominance.h"
44 #include "cfg.h"
45 #include "cfgrtl.h"
46 #include "cfgbuild.h"
47 #include "cfgcleanup.h"
48 #include "basic-block.h"
49 #include "reload.h"
50 #include "target.h"
51 #include "tree-pass.h"
52 #include "df.h"
53 #include "insn-codes.h"
55 #ifndef STACK_PUSH_CODE
56 #ifdef STACK_GROWS_DOWNWARD
57 #define STACK_PUSH_CODE PRE_DEC
58 #else
59 #define STACK_PUSH_CODE PRE_INC
60 #endif
61 #endif
63 #ifndef STACK_POP_CODE
64 #ifdef STACK_GROWS_DOWNWARD
65 #define STACK_POP_CODE POST_INC
66 #else
67 #define STACK_POP_CODE POST_DEC
68 #endif
69 #endif
71 static void validate_replace_rtx_1 (rtx *, rtx, rtx, rtx, bool);
72 static void validate_replace_src_1 (rtx *, void *);
73 static rtx split_insn (rtx_insn *);
75 struct target_recog default_target_recog;
76 #if SWITCHABLE_TARGET
77 struct target_recog *this_target_recog = &default_target_recog;
78 #endif
80 /* Nonzero means allow operands to be volatile.
81 This should be 0 if you are generating rtl, such as if you are calling
82 the functions in optabs.c and expmed.c (most of the time).
83 This should be 1 if all valid insns need to be recognized,
84 such as in reginfo.c and final.c and reload.c.
86 init_recog and init_recog_no_volatile are responsible for setting this. */
88 int volatile_ok;
90 struct recog_data_d recog_data;
92 /* Contains a vector of operand_alternative structures, such that
93 operand OP of alternative A is at index A * n_operands + OP.
94 Set up by preprocess_constraints. */
95 const operand_alternative *recog_op_alt;
97 /* Used to provide recog_op_alt for asms. */
98 static operand_alternative asm_op_alt[MAX_RECOG_OPERANDS
99 * MAX_RECOG_ALTERNATIVES];
101 /* On return from `constrain_operands', indicate which alternative
102 was satisfied. */
104 int which_alternative;
106 /* Nonzero after end of reload pass.
107 Set to 1 or 0 by toplev.c.
108 Controls the significance of (SUBREG (MEM)). */
110 int reload_completed;
112 /* Nonzero after thread_prologue_and_epilogue_insns has run. */
113 int epilogue_completed;
115 /* Initialize data used by the function `recog'.
116 This must be called once in the compilation of a function
117 before any insn recognition may be done in the function. */
119 void
120 init_recog_no_volatile (void)
122 volatile_ok = 0;
125 void
126 init_recog (void)
128 volatile_ok = 1;
132 /* Return true if labels in asm operands BODY are LABEL_REFs. */
134 static bool
135 asm_labels_ok (rtx body)
137 rtx asmop;
138 int i;
140 asmop = extract_asm_operands (body);
141 if (asmop == NULL_RTX)
142 return true;
144 for (i = 0; i < ASM_OPERANDS_LABEL_LENGTH (asmop); i++)
145 if (GET_CODE (ASM_OPERANDS_LABEL (asmop, i)) != LABEL_REF)
146 return false;
148 return true;
151 /* Check that X is an insn-body for an `asm' with operands
152 and that the operands mentioned in it are legitimate. */
155 check_asm_operands (rtx x)
157 int noperands;
158 rtx *operands;
159 const char **constraints;
160 int i;
162 if (!asm_labels_ok (x))
163 return 0;
165 /* Post-reload, be more strict with things. */
166 if (reload_completed)
168 /* ??? Doh! We've not got the wrapping insn. Cook one up. */
169 rtx_insn *insn = make_insn_raw (x);
170 extract_insn (insn);
171 constrain_operands (1, get_enabled_alternatives (insn));
172 return which_alternative >= 0;
175 noperands = asm_noperands (x);
176 if (noperands < 0)
177 return 0;
178 if (noperands == 0)
179 return 1;
181 operands = XALLOCAVEC (rtx, noperands);
182 constraints = XALLOCAVEC (const char *, noperands);
184 decode_asm_operands (x, operands, NULL, constraints, NULL, NULL);
186 for (i = 0; i < noperands; i++)
188 const char *c = constraints[i];
189 if (c[0] == '%')
190 c++;
191 if (! asm_operand_ok (operands[i], c, constraints))
192 return 0;
195 return 1;
198 /* Static data for the next two routines. */
200 typedef struct change_t
202 rtx object;
203 int old_code;
204 rtx *loc;
205 rtx old;
206 bool unshare;
207 } change_t;
209 static change_t *changes;
210 static int changes_allocated;
212 static int num_changes = 0;
214 /* Validate a proposed change to OBJECT. LOC is the location in the rtl
215 at which NEW_RTX will be placed. If OBJECT is zero, no validation is done,
216 the change is simply made.
218 Two types of objects are supported: If OBJECT is a MEM, memory_address_p
219 will be called with the address and mode as parameters. If OBJECT is
220 an INSN, CALL_INSN, or JUMP_INSN, the insn will be re-recognized with
221 the change in place.
223 IN_GROUP is nonzero if this is part of a group of changes that must be
224 performed as a group. In that case, the changes will be stored. The
225 function `apply_change_group' will validate and apply the changes.
227 If IN_GROUP is zero, this is a single change. Try to recognize the insn
228 or validate the memory reference with the change applied. If the result
229 is not valid for the machine, suppress the change and return zero.
230 Otherwise, perform the change and return 1. */
232 static bool
233 validate_change_1 (rtx object, rtx *loc, rtx new_rtx, bool in_group, bool unshare)
235 rtx old = *loc;
237 if (old == new_rtx || rtx_equal_p (old, new_rtx))
238 return 1;
240 gcc_assert (in_group != 0 || num_changes == 0);
242 *loc = new_rtx;
244 /* Save the information describing this change. */
245 if (num_changes >= changes_allocated)
247 if (changes_allocated == 0)
248 /* This value allows for repeated substitutions inside complex
249 indexed addresses, or changes in up to 5 insns. */
250 changes_allocated = MAX_RECOG_OPERANDS * 5;
251 else
252 changes_allocated *= 2;
254 changes = XRESIZEVEC (change_t, changes, changes_allocated);
257 changes[num_changes].object = object;
258 changes[num_changes].loc = loc;
259 changes[num_changes].old = old;
260 changes[num_changes].unshare = unshare;
262 if (object && !MEM_P (object))
264 /* Set INSN_CODE to force rerecognition of insn. Save old code in
265 case invalid. */
266 changes[num_changes].old_code = INSN_CODE (object);
267 INSN_CODE (object) = -1;
270 num_changes++;
272 /* If we are making a group of changes, return 1. Otherwise, validate the
273 change group we made. */
275 if (in_group)
276 return 1;
277 else
278 return apply_change_group ();
281 /* Wrapper for validate_change_1 without the UNSHARE argument defaulting
282 UNSHARE to false. */
284 bool
285 validate_change (rtx object, rtx *loc, rtx new_rtx, bool in_group)
287 return validate_change_1 (object, loc, new_rtx, in_group, false);
290 /* Wrapper for validate_change_1 without the UNSHARE argument defaulting
291 UNSHARE to true. */
293 bool
294 validate_unshare_change (rtx object, rtx *loc, rtx new_rtx, bool in_group)
296 return validate_change_1 (object, loc, new_rtx, in_group, true);
300 /* Keep X canonicalized if some changes have made it non-canonical; only
301 modifies the operands of X, not (for example) its code. Simplifications
302 are not the job of this routine.
304 Return true if anything was changed. */
305 bool
306 canonicalize_change_group (rtx insn, rtx x)
308 if (COMMUTATIVE_P (x)
309 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
311 /* Oops, the caller has made X no longer canonical.
312 Let's redo the changes in the correct order. */
313 rtx tem = XEXP (x, 0);
314 validate_unshare_change (insn, &XEXP (x, 0), XEXP (x, 1), 1);
315 validate_unshare_change (insn, &XEXP (x, 1), tem, 1);
316 return true;
318 else
319 return false;
323 /* This subroutine of apply_change_group verifies whether the changes to INSN
324 were valid; i.e. whether INSN can still be recognized.
326 If IN_GROUP is true clobbers which have to be added in order to
327 match the instructions will be added to the current change group.
328 Otherwise the changes will take effect immediately. */
331 insn_invalid_p (rtx_insn *insn, bool in_group)
333 rtx pat = PATTERN (insn);
334 int num_clobbers = 0;
335 /* If we are before reload and the pattern is a SET, see if we can add
336 clobbers. */
337 int icode = recog (pat, insn,
338 (GET_CODE (pat) == SET
339 && ! reload_completed
340 && ! reload_in_progress)
341 ? &num_clobbers : 0);
342 int is_asm = icode < 0 && asm_noperands (PATTERN (insn)) >= 0;
345 /* If this is an asm and the operand aren't legal, then fail. Likewise if
346 this is not an asm and the insn wasn't recognized. */
347 if ((is_asm && ! check_asm_operands (PATTERN (insn)))
348 || (!is_asm && icode < 0))
349 return 1;
351 /* If we have to add CLOBBERs, fail if we have to add ones that reference
352 hard registers since our callers can't know if they are live or not.
353 Otherwise, add them. */
354 if (num_clobbers > 0)
356 rtx newpat;
358 if (added_clobbers_hard_reg_p (icode))
359 return 1;
361 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_clobbers + 1));
362 XVECEXP (newpat, 0, 0) = pat;
363 add_clobbers (newpat, icode);
364 if (in_group)
365 validate_change (insn, &PATTERN (insn), newpat, 1);
366 else
367 PATTERN (insn) = pat = newpat;
370 /* After reload, verify that all constraints are satisfied. */
371 if (reload_completed)
373 extract_insn (insn);
375 if (! constrain_operands (1, get_preferred_alternatives (insn)))
376 return 1;
379 INSN_CODE (insn) = icode;
380 return 0;
383 /* Return number of changes made and not validated yet. */
385 num_changes_pending (void)
387 return num_changes;
390 /* Tentatively apply the changes numbered NUM and up.
391 Return 1 if all changes are valid, zero otherwise. */
394 verify_changes (int num)
396 int i;
397 rtx last_validated = NULL_RTX;
399 /* The changes have been applied and all INSN_CODEs have been reset to force
400 rerecognition.
402 The changes are valid if we aren't given an object, or if we are
403 given a MEM and it still is a valid address, or if this is in insn
404 and it is recognized. In the latter case, if reload has completed,
405 we also require that the operands meet the constraints for
406 the insn. */
408 for (i = num; i < num_changes; i++)
410 rtx object = changes[i].object;
412 /* If there is no object to test or if it is the same as the one we
413 already tested, ignore it. */
414 if (object == 0 || object == last_validated)
415 continue;
417 if (MEM_P (object))
419 if (! memory_address_addr_space_p (GET_MODE (object),
420 XEXP (object, 0),
421 MEM_ADDR_SPACE (object)))
422 break;
424 else if (/* changes[i].old might be zero, e.g. when putting a
425 REG_FRAME_RELATED_EXPR into a previously empty list. */
426 changes[i].old
427 && REG_P (changes[i].old)
428 && asm_noperands (PATTERN (object)) > 0
429 && REG_EXPR (changes[i].old) != NULL_TREE
430 && DECL_ASSEMBLER_NAME_SET_P (REG_EXPR (changes[i].old))
431 && DECL_REGISTER (REG_EXPR (changes[i].old)))
433 /* Don't allow changes of hard register operands to inline
434 assemblies if they have been defined as register asm ("x"). */
435 break;
437 else if (DEBUG_INSN_P (object))
438 continue;
439 else if (insn_invalid_p (as_a <rtx_insn *> (object), true))
441 rtx pat = PATTERN (object);
443 /* Perhaps we couldn't recognize the insn because there were
444 extra CLOBBERs at the end. If so, try to re-recognize
445 without the last CLOBBER (later iterations will cause each of
446 them to be eliminated, in turn). But don't do this if we
447 have an ASM_OPERAND. */
448 if (GET_CODE (pat) == PARALLEL
449 && GET_CODE (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1)) == CLOBBER
450 && asm_noperands (PATTERN (object)) < 0)
452 rtx newpat;
454 if (XVECLEN (pat, 0) == 2)
455 newpat = XVECEXP (pat, 0, 0);
456 else
458 int j;
460 newpat
461 = gen_rtx_PARALLEL (VOIDmode,
462 rtvec_alloc (XVECLEN (pat, 0) - 1));
463 for (j = 0; j < XVECLEN (newpat, 0); j++)
464 XVECEXP (newpat, 0, j) = XVECEXP (pat, 0, j);
467 /* Add a new change to this group to replace the pattern
468 with this new pattern. Then consider this change
469 as having succeeded. The change we added will
470 cause the entire call to fail if things remain invalid.
472 Note that this can lose if a later change than the one
473 we are processing specified &XVECEXP (PATTERN (object), 0, X)
474 but this shouldn't occur. */
476 validate_change (object, &PATTERN (object), newpat, 1);
477 continue;
479 else if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER
480 || GET_CODE (pat) == VAR_LOCATION)
481 /* If this insn is a CLOBBER or USE, it is always valid, but is
482 never recognized. */
483 continue;
484 else
485 break;
487 last_validated = object;
490 return (i == num_changes);
493 /* A group of changes has previously been issued with validate_change
494 and verified with verify_changes. Call df_insn_rescan for each of
495 the insn changed and clear num_changes. */
497 void
498 confirm_change_group (void)
500 int i;
501 rtx last_object = NULL;
503 for (i = 0; i < num_changes; i++)
505 rtx object = changes[i].object;
507 if (changes[i].unshare)
508 *changes[i].loc = copy_rtx (*changes[i].loc);
510 /* Avoid unnecessary rescanning when multiple changes to same instruction
511 are made. */
512 if (object)
514 if (object != last_object && last_object && INSN_P (last_object))
515 df_insn_rescan (as_a <rtx_insn *> (last_object));
516 last_object = object;
520 if (last_object && INSN_P (last_object))
521 df_insn_rescan (as_a <rtx_insn *> (last_object));
522 num_changes = 0;
525 /* Apply a group of changes previously issued with `validate_change'.
526 If all changes are valid, call confirm_change_group and return 1,
527 otherwise, call cancel_changes and return 0. */
530 apply_change_group (void)
532 if (verify_changes (0))
534 confirm_change_group ();
535 return 1;
537 else
539 cancel_changes (0);
540 return 0;
545 /* Return the number of changes so far in the current group. */
548 num_validated_changes (void)
550 return num_changes;
553 /* Retract the changes numbered NUM and up. */
555 void
556 cancel_changes (int num)
558 int i;
560 /* Back out all the changes. Do this in the opposite order in which
561 they were made. */
562 for (i = num_changes - 1; i >= num; i--)
564 *changes[i].loc = changes[i].old;
565 if (changes[i].object && !MEM_P (changes[i].object))
566 INSN_CODE (changes[i].object) = changes[i].old_code;
568 num_changes = num;
571 /* Reduce conditional compilation elsewhere. */
572 #ifndef HAVE_extv
573 #define HAVE_extv 0
574 #define CODE_FOR_extv CODE_FOR_nothing
575 #endif
576 #ifndef HAVE_extzv
577 #define HAVE_extzv 0
578 #define CODE_FOR_extzv CODE_FOR_nothing
579 #endif
581 /* A subroutine of validate_replace_rtx_1 that tries to simplify the resulting
582 rtx. */
584 static void
585 simplify_while_replacing (rtx *loc, rtx to, rtx object,
586 machine_mode op0_mode)
588 rtx x = *loc;
589 enum rtx_code code = GET_CODE (x);
590 rtx new_rtx = NULL_RTX;
592 if (SWAPPABLE_OPERANDS_P (x)
593 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
595 validate_unshare_change (object, loc,
596 gen_rtx_fmt_ee (COMMUTATIVE_ARITH_P (x) ? code
597 : swap_condition (code),
598 GET_MODE (x), XEXP (x, 1),
599 XEXP (x, 0)), 1);
600 x = *loc;
601 code = GET_CODE (x);
604 /* Canonicalize arithmetics with all constant operands. */
605 switch (GET_RTX_CLASS (code))
607 case RTX_UNARY:
608 if (CONSTANT_P (XEXP (x, 0)))
609 new_rtx = simplify_unary_operation (code, GET_MODE (x), XEXP (x, 0),
610 op0_mode);
611 break;
612 case RTX_COMM_ARITH:
613 case RTX_BIN_ARITH:
614 if (CONSTANT_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
615 new_rtx = simplify_binary_operation (code, GET_MODE (x), XEXP (x, 0),
616 XEXP (x, 1));
617 break;
618 case RTX_COMPARE:
619 case RTX_COMM_COMPARE:
620 if (CONSTANT_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
621 new_rtx = simplify_relational_operation (code, GET_MODE (x), op0_mode,
622 XEXP (x, 0), XEXP (x, 1));
623 break;
624 default:
625 break;
627 if (new_rtx)
629 validate_change (object, loc, new_rtx, 1);
630 return;
633 switch (code)
635 case PLUS:
636 /* If we have a PLUS whose second operand is now a CONST_INT, use
637 simplify_gen_binary to try to simplify it.
638 ??? We may want later to remove this, once simplification is
639 separated from this function. */
640 if (CONST_INT_P (XEXP (x, 1)) && XEXP (x, 1) == to)
641 validate_change (object, loc,
642 simplify_gen_binary
643 (PLUS, GET_MODE (x), XEXP (x, 0), XEXP (x, 1)), 1);
644 break;
645 case MINUS:
646 if (CONST_SCALAR_INT_P (XEXP (x, 1)))
647 validate_change (object, loc,
648 simplify_gen_binary
649 (PLUS, GET_MODE (x), XEXP (x, 0),
650 simplify_gen_unary (NEG,
651 GET_MODE (x), XEXP (x, 1),
652 GET_MODE (x))), 1);
653 break;
654 case ZERO_EXTEND:
655 case SIGN_EXTEND:
656 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
658 new_rtx = simplify_gen_unary (code, GET_MODE (x), XEXP (x, 0),
659 op0_mode);
660 /* If any of the above failed, substitute in something that
661 we know won't be recognized. */
662 if (!new_rtx)
663 new_rtx = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
664 validate_change (object, loc, new_rtx, 1);
666 break;
667 case SUBREG:
668 /* All subregs possible to simplify should be simplified. */
669 new_rtx = simplify_subreg (GET_MODE (x), SUBREG_REG (x), op0_mode,
670 SUBREG_BYTE (x));
672 /* Subregs of VOIDmode operands are incorrect. */
673 if (!new_rtx && GET_MODE (SUBREG_REG (x)) == VOIDmode)
674 new_rtx = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
675 if (new_rtx)
676 validate_change (object, loc, new_rtx, 1);
677 break;
678 case ZERO_EXTRACT:
679 case SIGN_EXTRACT:
680 /* If we are replacing a register with memory, try to change the memory
681 to be the mode required for memory in extract operations (this isn't
682 likely to be an insertion operation; if it was, nothing bad will
683 happen, we might just fail in some cases). */
685 if (MEM_P (XEXP (x, 0))
686 && CONST_INT_P (XEXP (x, 1))
687 && CONST_INT_P (XEXP (x, 2))
688 && !mode_dependent_address_p (XEXP (XEXP (x, 0), 0),
689 MEM_ADDR_SPACE (XEXP (x, 0)))
690 && !MEM_VOLATILE_P (XEXP (x, 0)))
692 machine_mode wanted_mode = VOIDmode;
693 machine_mode is_mode = GET_MODE (XEXP (x, 0));
694 int pos = INTVAL (XEXP (x, 2));
696 if (GET_CODE (x) == ZERO_EXTRACT && HAVE_extzv)
698 wanted_mode = insn_data[CODE_FOR_extzv].operand[1].mode;
699 if (wanted_mode == VOIDmode)
700 wanted_mode = word_mode;
702 else if (GET_CODE (x) == SIGN_EXTRACT && HAVE_extv)
704 wanted_mode = insn_data[CODE_FOR_extv].operand[1].mode;
705 if (wanted_mode == VOIDmode)
706 wanted_mode = word_mode;
709 /* If we have a narrower mode, we can do something. */
710 if (wanted_mode != VOIDmode
711 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
713 int offset = pos / BITS_PER_UNIT;
714 rtx newmem;
716 /* If the bytes and bits are counted differently, we
717 must adjust the offset. */
718 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
719 offset =
720 (GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (wanted_mode) -
721 offset);
723 gcc_assert (GET_MODE_PRECISION (wanted_mode)
724 == GET_MODE_BITSIZE (wanted_mode));
725 pos %= GET_MODE_BITSIZE (wanted_mode);
727 newmem = adjust_address_nv (XEXP (x, 0), wanted_mode, offset);
729 validate_change (object, &XEXP (x, 2), GEN_INT (pos), 1);
730 validate_change (object, &XEXP (x, 0), newmem, 1);
734 break;
736 default:
737 break;
741 /* Replace every occurrence of FROM in X with TO. Mark each change with
742 validate_change passing OBJECT. */
744 static void
745 validate_replace_rtx_1 (rtx *loc, rtx from, rtx to, rtx object,
746 bool simplify)
748 int i, j;
749 const char *fmt;
750 rtx x = *loc;
751 enum rtx_code code;
752 machine_mode op0_mode = VOIDmode;
753 int prev_changes = num_changes;
755 if (!x)
756 return;
758 code = GET_CODE (x);
759 fmt = GET_RTX_FORMAT (code);
760 if (fmt[0] == 'e')
761 op0_mode = GET_MODE (XEXP (x, 0));
763 /* X matches FROM if it is the same rtx or they are both referring to the
764 same register in the same mode. Avoid calling rtx_equal_p unless the
765 operands look similar. */
767 if (x == from
768 || (REG_P (x) && REG_P (from)
769 && GET_MODE (x) == GET_MODE (from)
770 && REGNO (x) == REGNO (from))
771 || (GET_CODE (x) == GET_CODE (from) && GET_MODE (x) == GET_MODE (from)
772 && rtx_equal_p (x, from)))
774 validate_unshare_change (object, loc, to, 1);
775 return;
778 /* Call ourself recursively to perform the replacements.
779 We must not replace inside already replaced expression, otherwise we
780 get infinite recursion for replacements like (reg X)->(subreg (reg X))
781 so we must special case shared ASM_OPERANDS. */
783 if (GET_CODE (x) == PARALLEL)
785 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
787 if (j && GET_CODE (XVECEXP (x, 0, j)) == SET
788 && GET_CODE (SET_SRC (XVECEXP (x, 0, j))) == ASM_OPERANDS)
790 /* Verify that operands are really shared. */
791 gcc_assert (ASM_OPERANDS_INPUT_VEC (SET_SRC (XVECEXP (x, 0, 0)))
792 == ASM_OPERANDS_INPUT_VEC (SET_SRC (XVECEXP
793 (x, 0, j))));
794 validate_replace_rtx_1 (&SET_DEST (XVECEXP (x, 0, j)),
795 from, to, object, simplify);
797 else
798 validate_replace_rtx_1 (&XVECEXP (x, 0, j), from, to, object,
799 simplify);
802 else
803 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
805 if (fmt[i] == 'e')
806 validate_replace_rtx_1 (&XEXP (x, i), from, to, object, simplify);
807 else if (fmt[i] == 'E')
808 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
809 validate_replace_rtx_1 (&XVECEXP (x, i, j), from, to, object,
810 simplify);
813 /* If we didn't substitute, there is nothing more to do. */
814 if (num_changes == prev_changes)
815 return;
817 /* ??? The regmove is no more, so is this aberration still necessary? */
818 /* Allow substituted expression to have different mode. This is used by
819 regmove to change mode of pseudo register. */
820 if (fmt[0] == 'e' && GET_MODE (XEXP (x, 0)) != VOIDmode)
821 op0_mode = GET_MODE (XEXP (x, 0));
823 /* Do changes needed to keep rtx consistent. Don't do any other
824 simplifications, as it is not our job. */
825 if (simplify)
826 simplify_while_replacing (loc, to, object, op0_mode);
829 /* Try replacing every occurrence of FROM in subexpression LOC of INSN
830 with TO. After all changes have been made, validate by seeing
831 if INSN is still valid. */
834 validate_replace_rtx_subexp (rtx from, rtx to, rtx insn, rtx *loc)
836 validate_replace_rtx_1 (loc, from, to, insn, true);
837 return apply_change_group ();
840 /* Try replacing every occurrence of FROM in INSN with TO. After all
841 changes have been made, validate by seeing if INSN is still valid. */
844 validate_replace_rtx (rtx from, rtx to, rtx insn)
846 validate_replace_rtx_1 (&PATTERN (insn), from, to, insn, true);
847 return apply_change_group ();
850 /* Try replacing every occurrence of FROM in WHERE with TO. Assume that WHERE
851 is a part of INSN. After all changes have been made, validate by seeing if
852 INSN is still valid.
853 validate_replace_rtx (from, to, insn) is equivalent to
854 validate_replace_rtx_part (from, to, &PATTERN (insn), insn). */
857 validate_replace_rtx_part (rtx from, rtx to, rtx *where, rtx insn)
859 validate_replace_rtx_1 (where, from, to, insn, true);
860 return apply_change_group ();
863 /* Same as above, but do not simplify rtx afterwards. */
865 validate_replace_rtx_part_nosimplify (rtx from, rtx to, rtx *where,
866 rtx insn)
868 validate_replace_rtx_1 (where, from, to, insn, false);
869 return apply_change_group ();
873 /* Try replacing every occurrence of FROM in INSN with TO. This also
874 will replace in REG_EQUAL and REG_EQUIV notes. */
876 void
877 validate_replace_rtx_group (rtx from, rtx to, rtx insn)
879 rtx note;
880 validate_replace_rtx_1 (&PATTERN (insn), from, to, insn, true);
881 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
882 if (REG_NOTE_KIND (note) == REG_EQUAL
883 || REG_NOTE_KIND (note) == REG_EQUIV)
884 validate_replace_rtx_1 (&XEXP (note, 0), from, to, insn, true);
887 /* Function called by note_uses to replace used subexpressions. */
888 struct validate_replace_src_data
890 rtx from; /* Old RTX */
891 rtx to; /* New RTX */
892 rtx insn; /* Insn in which substitution is occurring. */
895 static void
896 validate_replace_src_1 (rtx *x, void *data)
898 struct validate_replace_src_data *d
899 = (struct validate_replace_src_data *) data;
901 validate_replace_rtx_1 (x, d->from, d->to, d->insn, true);
904 /* Try replacing every occurrence of FROM in INSN with TO, avoiding
905 SET_DESTs. */
907 void
908 validate_replace_src_group (rtx from, rtx to, rtx insn)
910 struct validate_replace_src_data d;
912 d.from = from;
913 d.to = to;
914 d.insn = insn;
915 note_uses (&PATTERN (insn), validate_replace_src_1, &d);
918 /* Try simplify INSN.
919 Invoke simplify_rtx () on every SET_SRC and SET_DEST inside the INSN's
920 pattern and return true if something was simplified. */
922 bool
923 validate_simplify_insn (rtx insn)
925 int i;
926 rtx pat = NULL;
927 rtx newpat = NULL;
929 pat = PATTERN (insn);
931 if (GET_CODE (pat) == SET)
933 newpat = simplify_rtx (SET_SRC (pat));
934 if (newpat && !rtx_equal_p (SET_SRC (pat), newpat))
935 validate_change (insn, &SET_SRC (pat), newpat, 1);
936 newpat = simplify_rtx (SET_DEST (pat));
937 if (newpat && !rtx_equal_p (SET_DEST (pat), newpat))
938 validate_change (insn, &SET_DEST (pat), newpat, 1);
940 else if (GET_CODE (pat) == PARALLEL)
941 for (i = 0; i < XVECLEN (pat, 0); i++)
943 rtx s = XVECEXP (pat, 0, i);
945 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
947 newpat = simplify_rtx (SET_SRC (s));
948 if (newpat && !rtx_equal_p (SET_SRC (s), newpat))
949 validate_change (insn, &SET_SRC (s), newpat, 1);
950 newpat = simplify_rtx (SET_DEST (s));
951 if (newpat && !rtx_equal_p (SET_DEST (s), newpat))
952 validate_change (insn, &SET_DEST (s), newpat, 1);
955 return ((num_changes_pending () > 0) && (apply_change_group () > 0));
958 #ifdef HAVE_cc0
959 /* Return 1 if the insn using CC0 set by INSN does not contain
960 any ordered tests applied to the condition codes.
961 EQ and NE tests do not count. */
964 next_insn_tests_no_inequality (rtx insn)
966 rtx next = next_cc0_user (insn);
968 /* If there is no next insn, we have to take the conservative choice. */
969 if (next == 0)
970 return 0;
972 return (INSN_P (next)
973 && ! inequality_comparisons_p (PATTERN (next)));
975 #endif
977 /* Return 1 if OP is a valid general operand for machine mode MODE.
978 This is either a register reference, a memory reference,
979 or a constant. In the case of a memory reference, the address
980 is checked for general validity for the target machine.
982 Register and memory references must have mode MODE in order to be valid,
983 but some constants have no machine mode and are valid for any mode.
985 If MODE is VOIDmode, OP is checked for validity for whatever mode
986 it has.
988 The main use of this function is as a predicate in match_operand
989 expressions in the machine description. */
992 general_operand (rtx op, machine_mode mode)
994 enum rtx_code code = GET_CODE (op);
996 if (mode == VOIDmode)
997 mode = GET_MODE (op);
999 /* Don't accept CONST_INT or anything similar
1000 if the caller wants something floating. */
1001 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1002 && GET_MODE_CLASS (mode) != MODE_INT
1003 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1004 return 0;
1006 if (CONST_INT_P (op)
1007 && mode != VOIDmode
1008 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
1009 return 0;
1011 if (CONSTANT_P (op))
1012 return ((GET_MODE (op) == VOIDmode || GET_MODE (op) == mode
1013 || mode == VOIDmode)
1014 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1015 && targetm.legitimate_constant_p (mode == VOIDmode
1016 ? GET_MODE (op)
1017 : mode, op));
1019 /* Except for certain constants with VOIDmode, already checked for,
1020 OP's mode must match MODE if MODE specifies a mode. */
1022 if (GET_MODE (op) != mode)
1023 return 0;
1025 if (code == SUBREG)
1027 rtx sub = SUBREG_REG (op);
1029 #ifdef INSN_SCHEDULING
1030 /* On machines that have insn scheduling, we want all memory
1031 reference to be explicit, so outlaw paradoxical SUBREGs.
1032 However, we must allow them after reload so that they can
1033 get cleaned up by cleanup_subreg_operands. */
1034 if (!reload_completed && MEM_P (sub)
1035 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (sub)))
1036 return 0;
1037 #endif
1038 /* Avoid memories with nonzero SUBREG_BYTE, as offsetting the memory
1039 may result in incorrect reference. We should simplify all valid
1040 subregs of MEM anyway. But allow this after reload because we
1041 might be called from cleanup_subreg_operands.
1043 ??? This is a kludge. */
1044 if (!reload_completed && SUBREG_BYTE (op) != 0
1045 && MEM_P (sub))
1046 return 0;
1048 #ifdef CANNOT_CHANGE_MODE_CLASS
1049 if (REG_P (sub)
1050 && REGNO (sub) < FIRST_PSEUDO_REGISTER
1051 && REG_CANNOT_CHANGE_MODE_P (REGNO (sub), GET_MODE (sub), mode)
1052 && GET_MODE_CLASS (GET_MODE (sub)) != MODE_COMPLEX_INT
1053 && GET_MODE_CLASS (GET_MODE (sub)) != MODE_COMPLEX_FLOAT
1054 /* LRA can generate some invalid SUBREGS just for matched
1055 operand reload presentation. LRA needs to treat them as
1056 valid. */
1057 && ! LRA_SUBREG_P (op))
1058 return 0;
1059 #endif
1061 /* FLOAT_MODE subregs can't be paradoxical. Combine will occasionally
1062 create such rtl, and we must reject it. */
1063 if (SCALAR_FLOAT_MODE_P (GET_MODE (op))
1064 /* LRA can use subreg to store a floating point value in an
1065 integer mode. Although the floating point and the
1066 integer modes need the same number of hard registers, the
1067 size of floating point mode can be less than the integer
1068 mode. */
1069 && ! lra_in_progress
1070 && GET_MODE_SIZE (GET_MODE (op)) > GET_MODE_SIZE (GET_MODE (sub)))
1071 return 0;
1073 op = sub;
1074 code = GET_CODE (op);
1077 if (code == REG)
1078 return (REGNO (op) >= FIRST_PSEUDO_REGISTER
1079 || in_hard_reg_set_p (operand_reg_set, GET_MODE (op), REGNO (op)));
1081 if (code == MEM)
1083 rtx y = XEXP (op, 0);
1085 if (! volatile_ok && MEM_VOLATILE_P (op))
1086 return 0;
1088 /* Use the mem's mode, since it will be reloaded thus. LRA can
1089 generate move insn with invalid addresses which is made valid
1090 and efficiently calculated by LRA through further numerous
1091 transformations. */
1092 if (lra_in_progress
1093 || memory_address_addr_space_p (GET_MODE (op), y, MEM_ADDR_SPACE (op)))
1094 return 1;
1097 return 0;
1100 /* Return 1 if OP is a valid memory address for a memory reference
1101 of mode MODE.
1103 The main use of this function is as a predicate in match_operand
1104 expressions in the machine description. */
1107 address_operand (rtx op, machine_mode mode)
1109 return memory_address_p (mode, op);
1112 /* Return 1 if OP is a register reference of mode MODE.
1113 If MODE is VOIDmode, accept a register in any mode.
1115 The main use of this function is as a predicate in match_operand
1116 expressions in the machine description. */
1119 register_operand (rtx op, machine_mode mode)
1121 if (GET_CODE (op) == SUBREG)
1123 rtx sub = SUBREG_REG (op);
1125 /* Before reload, we can allow (SUBREG (MEM...)) as a register operand
1126 because it is guaranteed to be reloaded into one.
1127 Just make sure the MEM is valid in itself.
1128 (Ideally, (SUBREG (MEM)...) should not exist after reload,
1129 but currently it does result from (SUBREG (REG)...) where the
1130 reg went on the stack.) */
1131 if (!REG_P (sub) && (reload_completed || !MEM_P (sub)))
1132 return 0;
1134 else if (!REG_P (op))
1135 return 0;
1136 return general_operand (op, mode);
1139 /* Return 1 for a register in Pmode; ignore the tested mode. */
1142 pmode_register_operand (rtx op, machine_mode mode ATTRIBUTE_UNUSED)
1144 return register_operand (op, Pmode);
1147 /* Return 1 if OP should match a MATCH_SCRATCH, i.e., if it is a SCRATCH
1148 or a hard register. */
1151 scratch_operand (rtx op, machine_mode mode)
1153 if (GET_MODE (op) != mode && mode != VOIDmode)
1154 return 0;
1156 return (GET_CODE (op) == SCRATCH
1157 || (REG_P (op)
1158 && (lra_in_progress
1159 || (REGNO (op) < FIRST_PSEUDO_REGISTER
1160 && REGNO_REG_CLASS (REGNO (op)) != NO_REGS))));
1163 /* Return 1 if OP is a valid immediate operand for mode MODE.
1165 The main use of this function is as a predicate in match_operand
1166 expressions in the machine description. */
1169 immediate_operand (rtx op, machine_mode mode)
1171 /* Don't accept CONST_INT or anything similar
1172 if the caller wants something floating. */
1173 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1174 && GET_MODE_CLASS (mode) != MODE_INT
1175 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1176 return 0;
1178 if (CONST_INT_P (op)
1179 && mode != VOIDmode
1180 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
1181 return 0;
1183 return (CONSTANT_P (op)
1184 && (GET_MODE (op) == mode || mode == VOIDmode
1185 || GET_MODE (op) == VOIDmode)
1186 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1187 && targetm.legitimate_constant_p (mode == VOIDmode
1188 ? GET_MODE (op)
1189 : mode, op));
1192 /* Returns 1 if OP is an operand that is a CONST_INT of mode MODE. */
1195 const_int_operand (rtx op, machine_mode mode)
1197 if (!CONST_INT_P (op))
1198 return 0;
1200 if (mode != VOIDmode
1201 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
1202 return 0;
1204 return 1;
1207 #if TARGET_SUPPORTS_WIDE_INT
1208 /* Returns 1 if OP is an operand that is a CONST_INT or CONST_WIDE_INT
1209 of mode MODE. */
1211 const_scalar_int_operand (rtx op, machine_mode mode)
1213 if (!CONST_SCALAR_INT_P (op))
1214 return 0;
1216 if (CONST_INT_P (op))
1217 return const_int_operand (op, mode);
1219 if (mode != VOIDmode)
1221 int prec = GET_MODE_PRECISION (mode);
1222 int bitsize = GET_MODE_BITSIZE (mode);
1224 if (CONST_WIDE_INT_NUNITS (op) * HOST_BITS_PER_WIDE_INT > bitsize)
1225 return 0;
1227 if (prec == bitsize)
1228 return 1;
1229 else
1231 /* Multiword partial int. */
1232 HOST_WIDE_INT x
1233 = CONST_WIDE_INT_ELT (op, CONST_WIDE_INT_NUNITS (op) - 1);
1234 return (sext_hwi (x, prec & (HOST_BITS_PER_WIDE_INT - 1)) == x);
1237 return 1;
1240 /* Returns 1 if OP is an operand that is a constant integer or constant
1241 floating-point number of MODE. */
1244 const_double_operand (rtx op, machine_mode mode)
1246 return (GET_CODE (op) == CONST_DOUBLE)
1247 && (GET_MODE (op) == mode || mode == VOIDmode);
1249 #else
1250 /* Returns 1 if OP is an operand that is a constant integer or constant
1251 floating-point number of MODE. */
1254 const_double_operand (rtx op, machine_mode mode)
1256 /* Don't accept CONST_INT or anything similar
1257 if the caller wants something floating. */
1258 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1259 && GET_MODE_CLASS (mode) != MODE_INT
1260 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1261 return 0;
1263 return ((CONST_DOUBLE_P (op) || CONST_INT_P (op))
1264 && (mode == VOIDmode || GET_MODE (op) == mode
1265 || GET_MODE (op) == VOIDmode));
1267 #endif
1268 /* Return 1 if OP is a general operand that is not an immediate
1269 operand of mode MODE. */
1272 nonimmediate_operand (rtx op, machine_mode mode)
1274 return (general_operand (op, mode) && ! CONSTANT_P (op));
1277 /* Return 1 if OP is a register reference or immediate value of mode MODE. */
1280 nonmemory_operand (rtx op, machine_mode mode)
1282 if (CONSTANT_P (op))
1283 return immediate_operand (op, mode);
1284 return register_operand (op, mode);
1287 /* Return 1 if OP is a valid operand that stands for pushing a
1288 value of mode MODE onto the stack.
1290 The main use of this function is as a predicate in match_operand
1291 expressions in the machine description. */
1294 push_operand (rtx op, machine_mode mode)
1296 unsigned int rounded_size = GET_MODE_SIZE (mode);
1298 #ifdef PUSH_ROUNDING
1299 rounded_size = PUSH_ROUNDING (rounded_size);
1300 #endif
1302 if (!MEM_P (op))
1303 return 0;
1305 if (mode != VOIDmode && GET_MODE (op) != mode)
1306 return 0;
1308 op = XEXP (op, 0);
1310 if (rounded_size == GET_MODE_SIZE (mode))
1312 if (GET_CODE (op) != STACK_PUSH_CODE)
1313 return 0;
1315 else
1317 if (GET_CODE (op) != PRE_MODIFY
1318 || GET_CODE (XEXP (op, 1)) != PLUS
1319 || XEXP (XEXP (op, 1), 0) != XEXP (op, 0)
1320 || !CONST_INT_P (XEXP (XEXP (op, 1), 1))
1321 #ifdef STACK_GROWS_DOWNWARD
1322 || INTVAL (XEXP (XEXP (op, 1), 1)) != - (int) rounded_size
1323 #else
1324 || INTVAL (XEXP (XEXP (op, 1), 1)) != (int) rounded_size
1325 #endif
1327 return 0;
1330 return XEXP (op, 0) == stack_pointer_rtx;
1333 /* Return 1 if OP is a valid operand that stands for popping a
1334 value of mode MODE off the stack.
1336 The main use of this function is as a predicate in match_operand
1337 expressions in the machine description. */
1340 pop_operand (rtx op, machine_mode mode)
1342 if (!MEM_P (op))
1343 return 0;
1345 if (mode != VOIDmode && GET_MODE (op) != mode)
1346 return 0;
1348 op = XEXP (op, 0);
1350 if (GET_CODE (op) != STACK_POP_CODE)
1351 return 0;
1353 return XEXP (op, 0) == stack_pointer_rtx;
1356 /* Return 1 if ADDR is a valid memory address
1357 for mode MODE in address space AS. */
1360 memory_address_addr_space_p (machine_mode mode ATTRIBUTE_UNUSED,
1361 rtx addr, addr_space_t as)
1363 #ifdef GO_IF_LEGITIMATE_ADDRESS
1364 gcc_assert (ADDR_SPACE_GENERIC_P (as));
1365 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
1366 return 0;
1368 win:
1369 return 1;
1370 #else
1371 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
1372 #endif
1375 /* Return 1 if OP is a valid memory reference with mode MODE,
1376 including a valid address.
1378 The main use of this function is as a predicate in match_operand
1379 expressions in the machine description. */
1382 memory_operand (rtx op, machine_mode mode)
1384 rtx inner;
1386 if (! reload_completed)
1387 /* Note that no SUBREG is a memory operand before end of reload pass,
1388 because (SUBREG (MEM...)) forces reloading into a register. */
1389 return MEM_P (op) && general_operand (op, mode);
1391 if (mode != VOIDmode && GET_MODE (op) != mode)
1392 return 0;
1394 inner = op;
1395 if (GET_CODE (inner) == SUBREG)
1396 inner = SUBREG_REG (inner);
1398 return (MEM_P (inner) && general_operand (op, mode));
1401 /* Return 1 if OP is a valid indirect memory reference with mode MODE;
1402 that is, a memory reference whose address is a general_operand. */
1405 indirect_operand (rtx op, machine_mode mode)
1407 /* Before reload, a SUBREG isn't in memory (see memory_operand, above). */
1408 if (! reload_completed
1409 && GET_CODE (op) == SUBREG && MEM_P (SUBREG_REG (op)))
1411 int offset = SUBREG_BYTE (op);
1412 rtx inner = SUBREG_REG (op);
1414 if (mode != VOIDmode && GET_MODE (op) != mode)
1415 return 0;
1417 /* The only way that we can have a general_operand as the resulting
1418 address is if OFFSET is zero and the address already is an operand
1419 or if the address is (plus Y (const_int -OFFSET)) and Y is an
1420 operand. */
1422 return ((offset == 0 && general_operand (XEXP (inner, 0), Pmode))
1423 || (GET_CODE (XEXP (inner, 0)) == PLUS
1424 && CONST_INT_P (XEXP (XEXP (inner, 0), 1))
1425 && INTVAL (XEXP (XEXP (inner, 0), 1)) == -offset
1426 && general_operand (XEXP (XEXP (inner, 0), 0), Pmode)));
1429 return (MEM_P (op)
1430 && memory_operand (op, mode)
1431 && general_operand (XEXP (op, 0), Pmode));
1434 /* Return 1 if this is an ordered comparison operator (not including
1435 ORDERED and UNORDERED). */
1438 ordered_comparison_operator (rtx op, machine_mode mode)
1440 if (mode != VOIDmode && GET_MODE (op) != mode)
1441 return false;
1442 switch (GET_CODE (op))
1444 case EQ:
1445 case NE:
1446 case LT:
1447 case LTU:
1448 case LE:
1449 case LEU:
1450 case GT:
1451 case GTU:
1452 case GE:
1453 case GEU:
1454 return true;
1455 default:
1456 return false;
1460 /* Return 1 if this is a comparison operator. This allows the use of
1461 MATCH_OPERATOR to recognize all the branch insns. */
1464 comparison_operator (rtx op, machine_mode mode)
1466 return ((mode == VOIDmode || GET_MODE (op) == mode)
1467 && COMPARISON_P (op));
1470 /* If BODY is an insn body that uses ASM_OPERANDS, return it. */
1473 extract_asm_operands (rtx body)
1475 rtx tmp;
1476 switch (GET_CODE (body))
1478 case ASM_OPERANDS:
1479 return body;
1481 case SET:
1482 /* Single output operand: BODY is (set OUTPUT (asm_operands ...)). */
1483 tmp = SET_SRC (body);
1484 if (GET_CODE (tmp) == ASM_OPERANDS)
1485 return tmp;
1486 break;
1488 case PARALLEL:
1489 tmp = XVECEXP (body, 0, 0);
1490 if (GET_CODE (tmp) == ASM_OPERANDS)
1491 return tmp;
1492 if (GET_CODE (tmp) == SET)
1494 tmp = SET_SRC (tmp);
1495 if (GET_CODE (tmp) == ASM_OPERANDS)
1496 return tmp;
1498 break;
1500 default:
1501 break;
1503 return NULL;
1506 /* If BODY is an insn body that uses ASM_OPERANDS,
1507 return the number of operands (both input and output) in the insn.
1508 Otherwise return -1. */
1511 asm_noperands (const_rtx body)
1513 rtx asm_op = extract_asm_operands (CONST_CAST_RTX (body));
1514 int n_sets = 0;
1516 if (asm_op == NULL)
1517 return -1;
1519 if (GET_CODE (body) == SET)
1520 n_sets = 1;
1521 else if (GET_CODE (body) == PARALLEL)
1523 int i;
1524 if (GET_CODE (XVECEXP (body, 0, 0)) == SET)
1526 /* Multiple output operands, or 1 output plus some clobbers:
1527 body is
1528 [(set OUTPUT (asm_operands ...))... (clobber (reg ...))...]. */
1529 /* Count backwards through CLOBBERs to determine number of SETs. */
1530 for (i = XVECLEN (body, 0); i > 0; i--)
1532 if (GET_CODE (XVECEXP (body, 0, i - 1)) == SET)
1533 break;
1534 if (GET_CODE (XVECEXP (body, 0, i - 1)) != CLOBBER)
1535 return -1;
1538 /* N_SETS is now number of output operands. */
1539 n_sets = i;
1541 /* Verify that all the SETs we have
1542 came from a single original asm_operands insn
1543 (so that invalid combinations are blocked). */
1544 for (i = 0; i < n_sets; i++)
1546 rtx elt = XVECEXP (body, 0, i);
1547 if (GET_CODE (elt) != SET)
1548 return -1;
1549 if (GET_CODE (SET_SRC (elt)) != ASM_OPERANDS)
1550 return -1;
1551 /* If these ASM_OPERANDS rtx's came from different original insns
1552 then they aren't allowed together. */
1553 if (ASM_OPERANDS_INPUT_VEC (SET_SRC (elt))
1554 != ASM_OPERANDS_INPUT_VEC (asm_op))
1555 return -1;
1558 else
1560 /* 0 outputs, but some clobbers:
1561 body is [(asm_operands ...) (clobber (reg ...))...]. */
1562 /* Make sure all the other parallel things really are clobbers. */
1563 for (i = XVECLEN (body, 0) - 1; i > 0; i--)
1564 if (GET_CODE (XVECEXP (body, 0, i)) != CLOBBER)
1565 return -1;
1569 return (ASM_OPERANDS_INPUT_LENGTH (asm_op)
1570 + ASM_OPERANDS_LABEL_LENGTH (asm_op) + n_sets);
1573 /* Assuming BODY is an insn body that uses ASM_OPERANDS,
1574 copy its operands (both input and output) into the vector OPERANDS,
1575 the locations of the operands within the insn into the vector OPERAND_LOCS,
1576 and the constraints for the operands into CONSTRAINTS.
1577 Write the modes of the operands into MODES.
1578 Return the assembler-template.
1580 If MODES, OPERAND_LOCS, CONSTRAINTS or OPERANDS is 0,
1581 we don't store that info. */
1583 const char *
1584 decode_asm_operands (rtx body, rtx *operands, rtx **operand_locs,
1585 const char **constraints, machine_mode *modes,
1586 location_t *loc)
1588 int nbase = 0, n, i;
1589 rtx asmop;
1591 switch (GET_CODE (body))
1593 case ASM_OPERANDS:
1594 /* Zero output asm: BODY is (asm_operands ...). */
1595 asmop = body;
1596 break;
1598 case SET:
1599 /* Single output asm: BODY is (set OUTPUT (asm_operands ...)). */
1600 asmop = SET_SRC (body);
1602 /* The output is in the SET.
1603 Its constraint is in the ASM_OPERANDS itself. */
1604 if (operands)
1605 operands[0] = SET_DEST (body);
1606 if (operand_locs)
1607 operand_locs[0] = &SET_DEST (body);
1608 if (constraints)
1609 constraints[0] = ASM_OPERANDS_OUTPUT_CONSTRAINT (asmop);
1610 if (modes)
1611 modes[0] = GET_MODE (SET_DEST (body));
1612 nbase = 1;
1613 break;
1615 case PARALLEL:
1617 int nparallel = XVECLEN (body, 0); /* Includes CLOBBERs. */
1619 asmop = XVECEXP (body, 0, 0);
1620 if (GET_CODE (asmop) == SET)
1622 asmop = SET_SRC (asmop);
1624 /* At least one output, plus some CLOBBERs. The outputs are in
1625 the SETs. Their constraints are in the ASM_OPERANDS itself. */
1626 for (i = 0; i < nparallel; i++)
1628 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
1629 break; /* Past last SET */
1630 if (operands)
1631 operands[i] = SET_DEST (XVECEXP (body, 0, i));
1632 if (operand_locs)
1633 operand_locs[i] = &SET_DEST (XVECEXP (body, 0, i));
1634 if (constraints)
1635 constraints[i] = XSTR (SET_SRC (XVECEXP (body, 0, i)), 1);
1636 if (modes)
1637 modes[i] = GET_MODE (SET_DEST (XVECEXP (body, 0, i)));
1639 nbase = i;
1641 break;
1644 default:
1645 gcc_unreachable ();
1648 n = ASM_OPERANDS_INPUT_LENGTH (asmop);
1649 for (i = 0; i < n; i++)
1651 if (operand_locs)
1652 operand_locs[nbase + i] = &ASM_OPERANDS_INPUT (asmop, i);
1653 if (operands)
1654 operands[nbase + i] = ASM_OPERANDS_INPUT (asmop, i);
1655 if (constraints)
1656 constraints[nbase + i] = ASM_OPERANDS_INPUT_CONSTRAINT (asmop, i);
1657 if (modes)
1658 modes[nbase + i] = ASM_OPERANDS_INPUT_MODE (asmop, i);
1660 nbase += n;
1662 n = ASM_OPERANDS_LABEL_LENGTH (asmop);
1663 for (i = 0; i < n; i++)
1665 if (operand_locs)
1666 operand_locs[nbase + i] = &ASM_OPERANDS_LABEL (asmop, i);
1667 if (operands)
1668 operands[nbase + i] = ASM_OPERANDS_LABEL (asmop, i);
1669 if (constraints)
1670 constraints[nbase + i] = "";
1671 if (modes)
1672 modes[nbase + i] = Pmode;
1675 if (loc)
1676 *loc = ASM_OPERANDS_SOURCE_LOCATION (asmop);
1678 return ASM_OPERANDS_TEMPLATE (asmop);
1681 /* Parse inline assembly string STRING and determine which operands are
1682 referenced by % markers. For the first NOPERANDS operands, set USED[I]
1683 to true if operand I is referenced.
1685 This is intended to distinguish barrier-like asms such as:
1687 asm ("" : "=m" (...));
1689 from real references such as:
1691 asm ("sw\t$0, %0" : "=m" (...)); */
1693 void
1694 get_referenced_operands (const char *string, bool *used,
1695 unsigned int noperands)
1697 memset (used, 0, sizeof (bool) * noperands);
1698 const char *p = string;
1699 while (*p)
1700 switch (*p)
1702 case '%':
1703 p += 1;
1704 /* A letter followed by a digit indicates an operand number. */
1705 if (ISALPHA (p[0]) && ISDIGIT (p[1]))
1706 p += 1;
1707 if (ISDIGIT (*p))
1709 char *endptr;
1710 unsigned long opnum = strtoul (p, &endptr, 10);
1711 if (endptr != p && opnum < noperands)
1712 used[opnum] = true;
1713 p = endptr;
1715 else
1716 p += 1;
1717 break;
1719 default:
1720 p++;
1721 break;
1725 /* Check if an asm_operand matches its constraints.
1726 Return > 0 if ok, = 0 if bad, < 0 if inconclusive. */
1729 asm_operand_ok (rtx op, const char *constraint, const char **constraints)
1731 int result = 0;
1732 #ifdef AUTO_INC_DEC
1733 bool incdec_ok = false;
1734 #endif
1736 /* Use constrain_operands after reload. */
1737 gcc_assert (!reload_completed);
1739 /* Empty constraint string is the same as "X,...,X", i.e. X for as
1740 many alternatives as required to match the other operands. */
1741 if (*constraint == '\0')
1742 result = 1;
1744 while (*constraint)
1746 enum constraint_num cn;
1747 char c = *constraint;
1748 int len;
1749 switch (c)
1751 case ',':
1752 constraint++;
1753 continue;
1755 case '0': case '1': case '2': case '3': case '4':
1756 case '5': case '6': case '7': case '8': case '9':
1757 /* If caller provided constraints pointer, look up
1758 the matching constraint. Otherwise, our caller should have
1759 given us the proper matching constraint, but we can't
1760 actually fail the check if they didn't. Indicate that
1761 results are inconclusive. */
1762 if (constraints)
1764 char *end;
1765 unsigned long match;
1767 match = strtoul (constraint, &end, 10);
1768 if (!result)
1769 result = asm_operand_ok (op, constraints[match], NULL);
1770 constraint = (const char *) end;
1772 else
1775 constraint++;
1776 while (ISDIGIT (*constraint));
1777 if (! result)
1778 result = -1;
1780 continue;
1782 /* The rest of the compiler assumes that reloading the address
1783 of a MEM into a register will make it fit an 'o' constraint.
1784 That is, if it sees a MEM operand for an 'o' constraint,
1785 it assumes that (mem (base-reg)) will fit.
1787 That assumption fails on targets that don't have offsettable
1788 addresses at all. We therefore need to treat 'o' asm
1789 constraints as a special case and only accept operands that
1790 are already offsettable, thus proving that at least one
1791 offsettable address exists. */
1792 case 'o': /* offsettable */
1793 if (offsettable_nonstrict_memref_p (op))
1794 result = 1;
1795 break;
1797 case 'g':
1798 if (general_operand (op, VOIDmode))
1799 result = 1;
1800 break;
1802 #ifdef AUTO_INC_DEC
1803 case '<':
1804 case '>':
1805 /* ??? Before auto-inc-dec, auto inc/dec insns are not supposed
1806 to exist, excepting those that expand_call created. Further,
1807 on some machines which do not have generalized auto inc/dec,
1808 an inc/dec is not a memory_operand.
1810 Match any memory and hope things are resolved after reload. */
1811 incdec_ok = true;
1812 #endif
1813 default:
1814 cn = lookup_constraint (constraint);
1815 switch (get_constraint_type (cn))
1817 case CT_REGISTER:
1818 if (!result
1819 && reg_class_for_constraint (cn) != NO_REGS
1820 && GET_MODE (op) != BLKmode
1821 && register_operand (op, VOIDmode))
1822 result = 1;
1823 break;
1825 case CT_CONST_INT:
1826 if (!result
1827 && CONST_INT_P (op)
1828 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
1829 result = 1;
1830 break;
1832 case CT_MEMORY:
1833 /* Every memory operand can be reloaded to fit. */
1834 result = result || memory_operand (op, VOIDmode);
1835 break;
1837 case CT_ADDRESS:
1838 /* Every address operand can be reloaded to fit. */
1839 result = result || address_operand (op, VOIDmode);
1840 break;
1842 case CT_FIXED_FORM:
1843 result = result || constraint_satisfied_p (op, cn);
1844 break;
1846 break;
1848 len = CONSTRAINT_LEN (c, constraint);
1850 constraint++;
1851 while (--len && *constraint);
1852 if (len)
1853 return 0;
1856 #ifdef AUTO_INC_DEC
1857 /* For operands without < or > constraints reject side-effects. */
1858 if (!incdec_ok && result && MEM_P (op))
1859 switch (GET_CODE (XEXP (op, 0)))
1861 case PRE_INC:
1862 case POST_INC:
1863 case PRE_DEC:
1864 case POST_DEC:
1865 case PRE_MODIFY:
1866 case POST_MODIFY:
1867 return 0;
1868 default:
1869 break;
1871 #endif
1873 return result;
1876 /* Given an rtx *P, if it is a sum containing an integer constant term,
1877 return the location (type rtx *) of the pointer to that constant term.
1878 Otherwise, return a null pointer. */
1880 rtx *
1881 find_constant_term_loc (rtx *p)
1883 rtx *tem;
1884 enum rtx_code code = GET_CODE (*p);
1886 /* If *P IS such a constant term, P is its location. */
1888 if (code == CONST_INT || code == SYMBOL_REF || code == LABEL_REF
1889 || code == CONST)
1890 return p;
1892 /* Otherwise, if not a sum, it has no constant term. */
1894 if (GET_CODE (*p) != PLUS)
1895 return 0;
1897 /* If one of the summands is constant, return its location. */
1899 if (XEXP (*p, 0) && CONSTANT_P (XEXP (*p, 0))
1900 && XEXP (*p, 1) && CONSTANT_P (XEXP (*p, 1)))
1901 return p;
1903 /* Otherwise, check each summand for containing a constant term. */
1905 if (XEXP (*p, 0) != 0)
1907 tem = find_constant_term_loc (&XEXP (*p, 0));
1908 if (tem != 0)
1909 return tem;
1912 if (XEXP (*p, 1) != 0)
1914 tem = find_constant_term_loc (&XEXP (*p, 1));
1915 if (tem != 0)
1916 return tem;
1919 return 0;
1922 /* Return 1 if OP is a memory reference
1923 whose address contains no side effects
1924 and remains valid after the addition
1925 of a positive integer less than the
1926 size of the object being referenced.
1928 We assume that the original address is valid and do not check it.
1930 This uses strict_memory_address_p as a subroutine, so
1931 don't use it before reload. */
1934 offsettable_memref_p (rtx op)
1936 return ((MEM_P (op))
1937 && offsettable_address_addr_space_p (1, GET_MODE (op), XEXP (op, 0),
1938 MEM_ADDR_SPACE (op)));
1941 /* Similar, but don't require a strictly valid mem ref:
1942 consider pseudo-regs valid as index or base regs. */
1945 offsettable_nonstrict_memref_p (rtx op)
1947 return ((MEM_P (op))
1948 && offsettable_address_addr_space_p (0, GET_MODE (op), XEXP (op, 0),
1949 MEM_ADDR_SPACE (op)));
1952 /* Return 1 if Y is a memory address which contains no side effects
1953 and would remain valid for address space AS after the addition of
1954 a positive integer less than the size of that mode.
1956 We assume that the original address is valid and do not check it.
1957 We do check that it is valid for narrower modes.
1959 If STRICTP is nonzero, we require a strictly valid address,
1960 for the sake of use in reload.c. */
1963 offsettable_address_addr_space_p (int strictp, machine_mode mode, rtx y,
1964 addr_space_t as)
1966 enum rtx_code ycode = GET_CODE (y);
1967 rtx z;
1968 rtx y1 = y;
1969 rtx *y2;
1970 int (*addressp) (machine_mode, rtx, addr_space_t) =
1971 (strictp ? strict_memory_address_addr_space_p
1972 : memory_address_addr_space_p);
1973 unsigned int mode_sz = GET_MODE_SIZE (mode);
1975 if (CONSTANT_ADDRESS_P (y))
1976 return 1;
1978 /* Adjusting an offsettable address involves changing to a narrower mode.
1979 Make sure that's OK. */
1981 if (mode_dependent_address_p (y, as))
1982 return 0;
1984 machine_mode address_mode = GET_MODE (y);
1985 if (address_mode == VOIDmode)
1986 address_mode = targetm.addr_space.address_mode (as);
1987 #ifdef POINTERS_EXTEND_UNSIGNED
1988 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
1989 #endif
1991 /* ??? How much offset does an offsettable BLKmode reference need?
1992 Clearly that depends on the situation in which it's being used.
1993 However, the current situation in which we test 0xffffffff is
1994 less than ideal. Caveat user. */
1995 if (mode_sz == 0)
1996 mode_sz = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
1998 /* If the expression contains a constant term,
1999 see if it remains valid when max possible offset is added. */
2001 if ((ycode == PLUS) && (y2 = find_constant_term_loc (&y1)))
2003 int good;
2005 y1 = *y2;
2006 *y2 = plus_constant (address_mode, *y2, mode_sz - 1);
2007 /* Use QImode because an odd displacement may be automatically invalid
2008 for any wider mode. But it should be valid for a single byte. */
2009 good = (*addressp) (QImode, y, as);
2011 /* In any case, restore old contents of memory. */
2012 *y2 = y1;
2013 return good;
2016 if (GET_RTX_CLASS (ycode) == RTX_AUTOINC)
2017 return 0;
2019 /* The offset added here is chosen as the maximum offset that
2020 any instruction could need to add when operating on something
2021 of the specified mode. We assume that if Y and Y+c are
2022 valid addresses then so is Y+d for all 0<d<c. adjust_address will
2023 go inside a LO_SUM here, so we do so as well. */
2024 if (GET_CODE (y) == LO_SUM
2025 && mode != BLKmode
2026 && mode_sz <= GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT)
2027 z = gen_rtx_LO_SUM (address_mode, XEXP (y, 0),
2028 plus_constant (address_mode, XEXP (y, 1),
2029 mode_sz - 1));
2030 #ifdef POINTERS_EXTEND_UNSIGNED
2031 /* Likewise for a ZERO_EXTEND from pointer_mode. */
2032 else if (POINTERS_EXTEND_UNSIGNED > 0
2033 && GET_CODE (y) == ZERO_EXTEND
2034 && GET_MODE (XEXP (y, 0)) == pointer_mode)
2035 z = gen_rtx_ZERO_EXTEND (address_mode,
2036 plus_constant (pointer_mode, XEXP (y, 0),
2037 mode_sz - 1));
2038 #endif
2039 else
2040 z = plus_constant (address_mode, y, mode_sz - 1);
2042 /* Use QImode because an odd displacement may be automatically invalid
2043 for any wider mode. But it should be valid for a single byte. */
2044 return (*addressp) (QImode, z, as);
2047 /* Return 1 if ADDR is an address-expression whose effect depends
2048 on the mode of the memory reference it is used in.
2050 ADDRSPACE is the address space associated with the address.
2052 Autoincrement addressing is a typical example of mode-dependence
2053 because the amount of the increment depends on the mode. */
2055 bool
2056 mode_dependent_address_p (rtx addr, addr_space_t addrspace)
2058 /* Auto-increment addressing with anything other than post_modify
2059 or pre_modify always introduces a mode dependency. Catch such
2060 cases now instead of deferring to the target. */
2061 if (GET_CODE (addr) == PRE_INC
2062 || GET_CODE (addr) == POST_INC
2063 || GET_CODE (addr) == PRE_DEC
2064 || GET_CODE (addr) == POST_DEC)
2065 return true;
2067 return targetm.mode_dependent_address_p (addr, addrspace);
2070 /* Return true if boolean attribute ATTR is supported. */
2072 static bool
2073 have_bool_attr (bool_attr attr)
2075 switch (attr)
2077 case BA_ENABLED:
2078 return HAVE_ATTR_enabled;
2079 case BA_PREFERRED_FOR_SIZE:
2080 return HAVE_ATTR_enabled || HAVE_ATTR_preferred_for_size;
2081 case BA_PREFERRED_FOR_SPEED:
2082 return HAVE_ATTR_enabled || HAVE_ATTR_preferred_for_speed;
2084 gcc_unreachable ();
2087 /* Return the value of ATTR for instruction INSN. */
2089 static bool
2090 get_bool_attr (rtx_insn *insn, bool_attr attr)
2092 switch (attr)
2094 case BA_ENABLED:
2095 return get_attr_enabled (insn);
2096 case BA_PREFERRED_FOR_SIZE:
2097 return get_attr_enabled (insn) && get_attr_preferred_for_size (insn);
2098 case BA_PREFERRED_FOR_SPEED:
2099 return get_attr_enabled (insn) && get_attr_preferred_for_speed (insn);
2101 gcc_unreachable ();
2104 /* Like get_bool_attr_mask, but don't use the cache. */
2106 static alternative_mask
2107 get_bool_attr_mask_uncached (rtx_insn *insn, bool_attr attr)
2109 /* Temporarily install enough information for get_attr_<foo> to assume
2110 that the insn operands are already cached. As above, the attribute
2111 mustn't depend on the values of operands, so we don't provide their
2112 real values here. */
2113 rtx old_insn = recog_data.insn;
2114 int old_alternative = which_alternative;
2116 recog_data.insn = insn;
2117 alternative_mask mask = ALL_ALTERNATIVES;
2118 int n_alternatives = insn_data[INSN_CODE (insn)].n_alternatives;
2119 for (int i = 0; i < n_alternatives; i++)
2121 which_alternative = i;
2122 if (!get_bool_attr (insn, attr))
2123 mask &= ~ALTERNATIVE_BIT (i);
2126 recog_data.insn = old_insn;
2127 which_alternative = old_alternative;
2128 return mask;
2131 /* Return the mask of operand alternatives that are allowed for INSN
2132 by boolean attribute ATTR. This mask depends only on INSN and on
2133 the current target; it does not depend on things like the values of
2134 operands. */
2136 static alternative_mask
2137 get_bool_attr_mask (rtx_insn *insn, bool_attr attr)
2139 /* Quick exit for asms and for targets that don't use these attributes. */
2140 int code = INSN_CODE (insn);
2141 if (code < 0 || !have_bool_attr (attr))
2142 return ALL_ALTERNATIVES;
2144 /* Calling get_attr_<foo> can be expensive, so cache the mask
2145 for speed. */
2146 if (!this_target_recog->x_bool_attr_masks[code][attr])
2147 this_target_recog->x_bool_attr_masks[code][attr]
2148 = get_bool_attr_mask_uncached (insn, attr);
2149 return this_target_recog->x_bool_attr_masks[code][attr];
2152 /* Return the set of alternatives of INSN that are allowed by the current
2153 target. */
2155 alternative_mask
2156 get_enabled_alternatives (rtx_insn *insn)
2158 return get_bool_attr_mask (insn, BA_ENABLED);
2161 /* Return the set of alternatives of INSN that are allowed by the current
2162 target and are preferred for the current size/speed optimization
2163 choice. */
2165 alternative_mask
2166 get_preferred_alternatives (rtx_insn *insn)
2168 if (optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn)))
2169 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SPEED);
2170 else
2171 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SIZE);
2174 /* Return the set of alternatives of INSN that are allowed by the current
2175 target and are preferred for the size/speed optimization choice
2176 associated with BB. Passing a separate BB is useful if INSN has not
2177 been emitted yet or if we are considering moving it to a different
2178 block. */
2180 alternative_mask
2181 get_preferred_alternatives (rtx_insn *insn, basic_block bb)
2183 if (optimize_bb_for_speed_p (bb))
2184 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SPEED);
2185 else
2186 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SIZE);
2189 /* Assert that the cached boolean attributes for INSN are still accurate.
2190 The backend is required to define these attributes in a way that only
2191 depends on the current target (rather than operands, compiler phase,
2192 etc.). */
2194 bool
2195 check_bool_attrs (rtx_insn *insn)
2197 int code = INSN_CODE (insn);
2198 if (code >= 0)
2199 for (int i = 0; i <= BA_LAST; ++i)
2201 enum bool_attr attr = (enum bool_attr) i;
2202 if (this_target_recog->x_bool_attr_masks[code][attr])
2203 gcc_assert (this_target_recog->x_bool_attr_masks[code][attr]
2204 == get_bool_attr_mask_uncached (insn, attr));
2206 return true;
2209 /* Like extract_insn, but save insn extracted and don't extract again, when
2210 called again for the same insn expecting that recog_data still contain the
2211 valid information. This is used primary by gen_attr infrastructure that
2212 often does extract insn again and again. */
2213 void
2214 extract_insn_cached (rtx_insn *insn)
2216 if (recog_data.insn == insn && INSN_CODE (insn) >= 0)
2217 return;
2218 extract_insn (insn);
2219 recog_data.insn = insn;
2222 /* Do uncached extract_insn, constrain_operands and complain about failures.
2223 This should be used when extracting a pre-existing constrained instruction
2224 if the caller wants to know which alternative was chosen. */
2225 void
2226 extract_constrain_insn (rtx_insn *insn)
2228 extract_insn (insn);
2229 if (!constrain_operands (reload_completed, get_enabled_alternatives (insn)))
2230 fatal_insn_not_found (insn);
2233 /* Do cached extract_insn, constrain_operands and complain about failures.
2234 Used by insn_attrtab. */
2235 void
2236 extract_constrain_insn_cached (rtx_insn *insn)
2238 extract_insn_cached (insn);
2239 if (which_alternative == -1
2240 && !constrain_operands (reload_completed,
2241 get_enabled_alternatives (insn)))
2242 fatal_insn_not_found (insn);
2245 /* Do cached constrain_operands on INSN and complain about failures. */
2247 constrain_operands_cached (rtx_insn *insn, int strict)
2249 if (which_alternative == -1)
2250 return constrain_operands (strict, get_enabled_alternatives (insn));
2251 else
2252 return 1;
2255 /* Analyze INSN and fill in recog_data. */
2257 void
2258 extract_insn (rtx_insn *insn)
2260 int i;
2261 int icode;
2262 int noperands;
2263 rtx body = PATTERN (insn);
2265 recog_data.n_operands = 0;
2266 recog_data.n_alternatives = 0;
2267 recog_data.n_dups = 0;
2268 recog_data.is_asm = false;
2270 switch (GET_CODE (body))
2272 case USE:
2273 case CLOBBER:
2274 case ASM_INPUT:
2275 case ADDR_VEC:
2276 case ADDR_DIFF_VEC:
2277 case VAR_LOCATION:
2278 return;
2280 case SET:
2281 if (GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
2282 goto asm_insn;
2283 else
2284 goto normal_insn;
2285 case PARALLEL:
2286 if ((GET_CODE (XVECEXP (body, 0, 0)) == SET
2287 && GET_CODE (SET_SRC (XVECEXP (body, 0, 0))) == ASM_OPERANDS)
2288 || GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
2289 goto asm_insn;
2290 else
2291 goto normal_insn;
2292 case ASM_OPERANDS:
2293 asm_insn:
2294 recog_data.n_operands = noperands = asm_noperands (body);
2295 if (noperands >= 0)
2297 /* This insn is an `asm' with operands. */
2299 /* expand_asm_operands makes sure there aren't too many operands. */
2300 gcc_assert (noperands <= MAX_RECOG_OPERANDS);
2302 /* Now get the operand values and constraints out of the insn. */
2303 decode_asm_operands (body, recog_data.operand,
2304 recog_data.operand_loc,
2305 recog_data.constraints,
2306 recog_data.operand_mode, NULL);
2307 memset (recog_data.is_operator, 0, sizeof recog_data.is_operator);
2308 if (noperands > 0)
2310 const char *p = recog_data.constraints[0];
2311 recog_data.n_alternatives = 1;
2312 while (*p)
2313 recog_data.n_alternatives += (*p++ == ',');
2315 recog_data.is_asm = true;
2316 break;
2318 fatal_insn_not_found (insn);
2320 default:
2321 normal_insn:
2322 /* Ordinary insn: recognize it, get the operands via insn_extract
2323 and get the constraints. */
2325 icode = recog_memoized (insn);
2326 if (icode < 0)
2327 fatal_insn_not_found (insn);
2329 recog_data.n_operands = noperands = insn_data[icode].n_operands;
2330 recog_data.n_alternatives = insn_data[icode].n_alternatives;
2331 recog_data.n_dups = insn_data[icode].n_dups;
2333 insn_extract (insn);
2335 for (i = 0; i < noperands; i++)
2337 recog_data.constraints[i] = insn_data[icode].operand[i].constraint;
2338 recog_data.is_operator[i] = insn_data[icode].operand[i].is_operator;
2339 recog_data.operand_mode[i] = insn_data[icode].operand[i].mode;
2340 /* VOIDmode match_operands gets mode from their real operand. */
2341 if (recog_data.operand_mode[i] == VOIDmode)
2342 recog_data.operand_mode[i] = GET_MODE (recog_data.operand[i]);
2345 for (i = 0; i < noperands; i++)
2346 recog_data.operand_type[i]
2347 = (recog_data.constraints[i][0] == '=' ? OP_OUT
2348 : recog_data.constraints[i][0] == '+' ? OP_INOUT
2349 : OP_IN);
2351 gcc_assert (recog_data.n_alternatives <= MAX_RECOG_ALTERNATIVES);
2353 recog_data.insn = NULL;
2354 which_alternative = -1;
2357 /* Fill in OP_ALT_BASE for an instruction that has N_OPERANDS operands,
2358 N_ALTERNATIVES alternatives and constraint strings CONSTRAINTS.
2359 OP_ALT_BASE has N_ALTERNATIVES * N_OPERANDS entries and CONSTRAINTS
2360 has N_OPERANDS entries. */
2362 void
2363 preprocess_constraints (int n_operands, int n_alternatives,
2364 const char **constraints,
2365 operand_alternative *op_alt_base)
2367 for (int i = 0; i < n_operands; i++)
2369 int j;
2370 struct operand_alternative *op_alt;
2371 const char *p = constraints[i];
2373 op_alt = op_alt_base;
2375 for (j = 0; j < n_alternatives; j++, op_alt += n_operands)
2377 op_alt[i].cl = NO_REGS;
2378 op_alt[i].constraint = p;
2379 op_alt[i].matches = -1;
2380 op_alt[i].matched = -1;
2382 if (*p == '\0' || *p == ',')
2384 op_alt[i].anything_ok = 1;
2385 continue;
2388 for (;;)
2390 char c = *p;
2391 if (c == '#')
2393 c = *++p;
2394 while (c != ',' && c != '\0');
2395 if (c == ',' || c == '\0')
2397 p++;
2398 break;
2401 switch (c)
2403 case '?':
2404 op_alt[i].reject += 6;
2405 break;
2406 case '!':
2407 op_alt[i].reject += 600;
2408 break;
2409 case '&':
2410 op_alt[i].earlyclobber = 1;
2411 break;
2413 case '0': case '1': case '2': case '3': case '4':
2414 case '5': case '6': case '7': case '8': case '9':
2416 char *end;
2417 op_alt[i].matches = strtoul (p, &end, 10);
2418 op_alt[op_alt[i].matches].matched = i;
2419 p = end;
2421 continue;
2423 case 'X':
2424 op_alt[i].anything_ok = 1;
2425 break;
2427 case 'g':
2428 op_alt[i].cl =
2429 reg_class_subunion[(int) op_alt[i].cl][(int) GENERAL_REGS];
2430 break;
2432 default:
2433 enum constraint_num cn = lookup_constraint (p);
2434 enum reg_class cl;
2435 switch (get_constraint_type (cn))
2437 case CT_REGISTER:
2438 cl = reg_class_for_constraint (cn);
2439 if (cl != NO_REGS)
2440 op_alt[i].cl = reg_class_subunion[op_alt[i].cl][cl];
2441 break;
2443 case CT_CONST_INT:
2444 break;
2446 case CT_MEMORY:
2447 op_alt[i].memory_ok = 1;
2448 break;
2450 case CT_ADDRESS:
2451 op_alt[i].is_address = 1;
2452 op_alt[i].cl
2453 = (reg_class_subunion
2454 [(int) op_alt[i].cl]
2455 [(int) base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2456 ADDRESS, SCRATCH)]);
2457 break;
2459 case CT_FIXED_FORM:
2460 break;
2462 break;
2464 p += CONSTRAINT_LEN (c, p);
2470 /* Return an array of operand_alternative instructions for
2471 instruction ICODE. */
2473 const operand_alternative *
2474 preprocess_insn_constraints (int icode)
2476 gcc_checking_assert (IN_RANGE (icode, 0, LAST_INSN_CODE));
2477 if (this_target_recog->x_op_alt[icode])
2478 return this_target_recog->x_op_alt[icode];
2480 int n_operands = insn_data[icode].n_operands;
2481 if (n_operands == 0)
2482 return 0;
2483 /* Always provide at least one alternative so that which_op_alt ()
2484 works correctly. If the instruction has 0 alternatives (i.e. all
2485 constraint strings are empty) then each operand in this alternative
2486 will have anything_ok set. */
2487 int n_alternatives = MAX (insn_data[icode].n_alternatives, 1);
2488 int n_entries = n_operands * n_alternatives;
2490 operand_alternative *op_alt = XCNEWVEC (operand_alternative, n_entries);
2491 const char **constraints = XALLOCAVEC (const char *, n_operands);
2493 for (int i = 0; i < n_operands; ++i)
2494 constraints[i] = insn_data[icode].operand[i].constraint;
2495 preprocess_constraints (n_operands, n_alternatives, constraints, op_alt);
2497 this_target_recog->x_op_alt[icode] = op_alt;
2498 return op_alt;
2501 /* After calling extract_insn, you can use this function to extract some
2502 information from the constraint strings into a more usable form.
2503 The collected data is stored in recog_op_alt. */
2505 void
2506 preprocess_constraints (rtx insn)
2508 int icode = INSN_CODE (insn);
2509 if (icode >= 0)
2510 recog_op_alt = preprocess_insn_constraints (icode);
2511 else
2513 int n_operands = recog_data.n_operands;
2514 int n_alternatives = recog_data.n_alternatives;
2515 int n_entries = n_operands * n_alternatives;
2516 memset (asm_op_alt, 0, n_entries * sizeof (operand_alternative));
2517 preprocess_constraints (n_operands, n_alternatives,
2518 recog_data.constraints, asm_op_alt);
2519 recog_op_alt = asm_op_alt;
2523 /* Check the operands of an insn against the insn's operand constraints
2524 and return 1 if they match any of the alternatives in ALTERNATIVES.
2526 The information about the insn's operands, constraints, operand modes
2527 etc. is obtained from the global variables set up by extract_insn.
2529 WHICH_ALTERNATIVE is set to a number which indicates which
2530 alternative of constraints was matched: 0 for the first alternative,
2531 1 for the next, etc.
2533 In addition, when two operands are required to match
2534 and it happens that the output operand is (reg) while the
2535 input operand is --(reg) or ++(reg) (a pre-inc or pre-dec),
2536 make the output operand look like the input.
2537 This is because the output operand is the one the template will print.
2539 This is used in final, just before printing the assembler code and by
2540 the routines that determine an insn's attribute.
2542 If STRICT is a positive nonzero value, it means that we have been
2543 called after reload has been completed. In that case, we must
2544 do all checks strictly. If it is zero, it means that we have been called
2545 before reload has completed. In that case, we first try to see if we can
2546 find an alternative that matches strictly. If not, we try again, this
2547 time assuming that reload will fix up the insn. This provides a "best
2548 guess" for the alternative and is used to compute attributes of insns prior
2549 to reload. A negative value of STRICT is used for this internal call. */
2551 struct funny_match
2553 int this_op, other;
2557 constrain_operands (int strict, alternative_mask alternatives)
2559 const char *constraints[MAX_RECOG_OPERANDS];
2560 int matching_operands[MAX_RECOG_OPERANDS];
2561 int earlyclobber[MAX_RECOG_OPERANDS];
2562 int c;
2564 struct funny_match funny_match[MAX_RECOG_OPERANDS];
2565 int funny_match_index;
2567 which_alternative = 0;
2568 if (recog_data.n_operands == 0 || recog_data.n_alternatives == 0)
2569 return 1;
2571 for (c = 0; c < recog_data.n_operands; c++)
2573 constraints[c] = recog_data.constraints[c];
2574 matching_operands[c] = -1;
2579 int seen_earlyclobber_at = -1;
2580 int opno;
2581 int lose = 0;
2582 funny_match_index = 0;
2584 if (!TEST_BIT (alternatives, which_alternative))
2586 int i;
2588 for (i = 0; i < recog_data.n_operands; i++)
2589 constraints[i] = skip_alternative (constraints[i]);
2591 which_alternative++;
2592 continue;
2595 for (opno = 0; opno < recog_data.n_operands; opno++)
2597 rtx op = recog_data.operand[opno];
2598 machine_mode mode = GET_MODE (op);
2599 const char *p = constraints[opno];
2600 int offset = 0;
2601 int win = 0;
2602 int val;
2603 int len;
2605 earlyclobber[opno] = 0;
2607 /* A unary operator may be accepted by the predicate, but it
2608 is irrelevant for matching constraints. */
2609 if (UNARY_P (op))
2610 op = XEXP (op, 0);
2612 if (GET_CODE (op) == SUBREG)
2614 if (REG_P (SUBREG_REG (op))
2615 && REGNO (SUBREG_REG (op)) < FIRST_PSEUDO_REGISTER)
2616 offset = subreg_regno_offset (REGNO (SUBREG_REG (op)),
2617 GET_MODE (SUBREG_REG (op)),
2618 SUBREG_BYTE (op),
2619 GET_MODE (op));
2620 op = SUBREG_REG (op);
2623 /* An empty constraint or empty alternative
2624 allows anything which matched the pattern. */
2625 if (*p == 0 || *p == ',')
2626 win = 1;
2629 switch (c = *p, len = CONSTRAINT_LEN (c, p), c)
2631 case '\0':
2632 len = 0;
2633 break;
2634 case ',':
2635 c = '\0';
2636 break;
2638 case '#':
2639 /* Ignore rest of this alternative as far as
2640 constraint checking is concerned. */
2642 p++;
2643 while (*p && *p != ',');
2644 len = 0;
2645 break;
2647 case '&':
2648 earlyclobber[opno] = 1;
2649 if (seen_earlyclobber_at < 0)
2650 seen_earlyclobber_at = opno;
2651 break;
2653 case '0': case '1': case '2': case '3': case '4':
2654 case '5': case '6': case '7': case '8': case '9':
2656 /* This operand must be the same as a previous one.
2657 This kind of constraint is used for instructions such
2658 as add when they take only two operands.
2660 Note that the lower-numbered operand is passed first.
2662 If we are not testing strictly, assume that this
2663 constraint will be satisfied. */
2665 char *end;
2666 int match;
2668 match = strtoul (p, &end, 10);
2669 p = end;
2671 if (strict < 0)
2672 val = 1;
2673 else
2675 rtx op1 = recog_data.operand[match];
2676 rtx op2 = recog_data.operand[opno];
2678 /* A unary operator may be accepted by the predicate,
2679 but it is irrelevant for matching constraints. */
2680 if (UNARY_P (op1))
2681 op1 = XEXP (op1, 0);
2682 if (UNARY_P (op2))
2683 op2 = XEXP (op2, 0);
2685 val = operands_match_p (op1, op2);
2688 matching_operands[opno] = match;
2689 matching_operands[match] = opno;
2691 if (val != 0)
2692 win = 1;
2694 /* If output is *x and input is *--x, arrange later
2695 to change the output to *--x as well, since the
2696 output op is the one that will be printed. */
2697 if (val == 2 && strict > 0)
2699 funny_match[funny_match_index].this_op = opno;
2700 funny_match[funny_match_index++].other = match;
2703 len = 0;
2704 break;
2706 case 'p':
2707 /* p is used for address_operands. When we are called by
2708 gen_reload, no one will have checked that the address is
2709 strictly valid, i.e., that all pseudos requiring hard regs
2710 have gotten them. */
2711 if (strict <= 0
2712 || (strict_memory_address_p (recog_data.operand_mode[opno],
2713 op)))
2714 win = 1;
2715 break;
2717 /* No need to check general_operand again;
2718 it was done in insn-recog.c. Well, except that reload
2719 doesn't check the validity of its replacements, but
2720 that should only matter when there's a bug. */
2721 case 'g':
2722 /* Anything goes unless it is a REG and really has a hard reg
2723 but the hard reg is not in the class GENERAL_REGS. */
2724 if (REG_P (op))
2726 if (strict < 0
2727 || GENERAL_REGS == ALL_REGS
2728 || (reload_in_progress
2729 && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2730 || reg_fits_class_p (op, GENERAL_REGS, offset, mode))
2731 win = 1;
2733 else if (strict < 0 || general_operand (op, mode))
2734 win = 1;
2735 break;
2737 default:
2739 enum constraint_num cn = lookup_constraint (p);
2740 enum reg_class cl = reg_class_for_constraint (cn);
2741 if (cl != NO_REGS)
2743 if (strict < 0
2744 || (strict == 0
2745 && REG_P (op)
2746 && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2747 || (strict == 0 && GET_CODE (op) == SCRATCH)
2748 || (REG_P (op)
2749 && reg_fits_class_p (op, cl, offset, mode)))
2750 win = 1;
2753 else if (constraint_satisfied_p (op, cn))
2754 win = 1;
2756 else if (insn_extra_memory_constraint (cn)
2757 /* Every memory operand can be reloaded to fit. */
2758 && ((strict < 0 && MEM_P (op))
2759 /* Before reload, accept what reload can turn
2760 into mem. */
2761 || (strict < 0 && CONSTANT_P (op))
2762 /* During reload, accept a pseudo */
2763 || (reload_in_progress && REG_P (op)
2764 && REGNO (op) >= FIRST_PSEUDO_REGISTER)))
2765 win = 1;
2766 else if (insn_extra_address_constraint (cn)
2767 /* Every address operand can be reloaded to fit. */
2768 && strict < 0)
2769 win = 1;
2770 /* Cater to architectures like IA-64 that define extra memory
2771 constraints without using define_memory_constraint. */
2772 else if (reload_in_progress
2773 && REG_P (op)
2774 && REGNO (op) >= FIRST_PSEUDO_REGISTER
2775 && reg_renumber[REGNO (op)] < 0
2776 && reg_equiv_mem (REGNO (op)) != 0
2777 && constraint_satisfied_p
2778 (reg_equiv_mem (REGNO (op)), cn))
2779 win = 1;
2780 break;
2783 while (p += len, c);
2785 constraints[opno] = p;
2786 /* If this operand did not win somehow,
2787 this alternative loses. */
2788 if (! win)
2789 lose = 1;
2791 /* This alternative won; the operands are ok.
2792 Change whichever operands this alternative says to change. */
2793 if (! lose)
2795 int opno, eopno;
2797 /* See if any earlyclobber operand conflicts with some other
2798 operand. */
2800 if (strict > 0 && seen_earlyclobber_at >= 0)
2801 for (eopno = seen_earlyclobber_at;
2802 eopno < recog_data.n_operands;
2803 eopno++)
2804 /* Ignore earlyclobber operands now in memory,
2805 because we would often report failure when we have
2806 two memory operands, one of which was formerly a REG. */
2807 if (earlyclobber[eopno]
2808 && REG_P (recog_data.operand[eopno]))
2809 for (opno = 0; opno < recog_data.n_operands; opno++)
2810 if ((MEM_P (recog_data.operand[opno])
2811 || recog_data.operand_type[opno] != OP_OUT)
2812 && opno != eopno
2813 /* Ignore things like match_operator operands. */
2814 && *recog_data.constraints[opno] != 0
2815 && ! (matching_operands[opno] == eopno
2816 && operands_match_p (recog_data.operand[opno],
2817 recog_data.operand[eopno]))
2818 && ! safe_from_earlyclobber (recog_data.operand[opno],
2819 recog_data.operand[eopno]))
2820 lose = 1;
2822 if (! lose)
2824 while (--funny_match_index >= 0)
2826 recog_data.operand[funny_match[funny_match_index].other]
2827 = recog_data.operand[funny_match[funny_match_index].this_op];
2830 #ifdef AUTO_INC_DEC
2831 /* For operands without < or > constraints reject side-effects. */
2832 if (recog_data.is_asm)
2834 for (opno = 0; opno < recog_data.n_operands; opno++)
2835 if (MEM_P (recog_data.operand[opno]))
2836 switch (GET_CODE (XEXP (recog_data.operand[opno], 0)))
2838 case PRE_INC:
2839 case POST_INC:
2840 case PRE_DEC:
2841 case POST_DEC:
2842 case PRE_MODIFY:
2843 case POST_MODIFY:
2844 if (strchr (recog_data.constraints[opno], '<') == NULL
2845 && strchr (recog_data.constraints[opno], '>')
2846 == NULL)
2847 return 0;
2848 break;
2849 default:
2850 break;
2853 #endif
2854 return 1;
2858 which_alternative++;
2860 while (which_alternative < recog_data.n_alternatives);
2862 which_alternative = -1;
2863 /* If we are about to reject this, but we are not to test strictly,
2864 try a very loose test. Only return failure if it fails also. */
2865 if (strict == 0)
2866 return constrain_operands (-1, alternatives);
2867 else
2868 return 0;
2871 /* Return true iff OPERAND (assumed to be a REG rtx)
2872 is a hard reg in class CLASS when its regno is offset by OFFSET
2873 and changed to mode MODE.
2874 If REG occupies multiple hard regs, all of them must be in CLASS. */
2876 bool
2877 reg_fits_class_p (const_rtx operand, reg_class_t cl, int offset,
2878 machine_mode mode)
2880 unsigned int regno = REGNO (operand);
2882 if (cl == NO_REGS)
2883 return false;
2885 /* Regno must not be a pseudo register. Offset may be negative. */
2886 return (HARD_REGISTER_NUM_P (regno)
2887 && HARD_REGISTER_NUM_P (regno + offset)
2888 && in_hard_reg_set_p (reg_class_contents[(int) cl], mode,
2889 regno + offset));
2892 /* Split single instruction. Helper function for split_all_insns and
2893 split_all_insns_noflow. Return last insn in the sequence if successful,
2894 or NULL if unsuccessful. */
2896 static rtx
2897 split_insn (rtx_insn *insn)
2899 /* Split insns here to get max fine-grain parallelism. */
2900 rtx_insn *first = PREV_INSN (insn);
2901 rtx_insn *last = try_split (PATTERN (insn), insn, 1);
2902 rtx insn_set, last_set, note;
2904 if (last == insn)
2905 return NULL_RTX;
2907 /* If the original instruction was a single set that was known to be
2908 equivalent to a constant, see if we can say the same about the last
2909 instruction in the split sequence. The two instructions must set
2910 the same destination. */
2911 insn_set = single_set (insn);
2912 if (insn_set)
2914 last_set = single_set (last);
2915 if (last_set && rtx_equal_p (SET_DEST (last_set), SET_DEST (insn_set)))
2917 note = find_reg_equal_equiv_note (insn);
2918 if (note && CONSTANT_P (XEXP (note, 0)))
2919 set_unique_reg_note (last, REG_EQUAL, XEXP (note, 0));
2920 else if (CONSTANT_P (SET_SRC (insn_set)))
2921 set_unique_reg_note (last, REG_EQUAL,
2922 copy_rtx (SET_SRC (insn_set)));
2926 /* try_split returns the NOTE that INSN became. */
2927 SET_INSN_DELETED (insn);
2929 /* ??? Coddle to md files that generate subregs in post-reload
2930 splitters instead of computing the proper hard register. */
2931 if (reload_completed && first != last)
2933 first = NEXT_INSN (first);
2934 for (;;)
2936 if (INSN_P (first))
2937 cleanup_subreg_operands (first);
2938 if (first == last)
2939 break;
2940 first = NEXT_INSN (first);
2944 return last;
2947 /* Split all insns in the function. If UPD_LIFE, update life info after. */
2949 void
2950 split_all_insns (void)
2952 sbitmap blocks;
2953 bool changed;
2954 basic_block bb;
2956 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
2957 bitmap_clear (blocks);
2958 changed = false;
2960 FOR_EACH_BB_REVERSE_FN (bb, cfun)
2962 rtx_insn *insn, *next;
2963 bool finish = false;
2965 rtl_profile_for_bb (bb);
2966 for (insn = BB_HEAD (bb); !finish ; insn = next)
2968 /* Can't use `next_real_insn' because that might go across
2969 CODE_LABELS and short-out basic blocks. */
2970 next = NEXT_INSN (insn);
2971 finish = (insn == BB_END (bb));
2972 if (INSN_P (insn))
2974 rtx set = single_set (insn);
2976 /* Don't split no-op move insns. These should silently
2977 disappear later in final. Splitting such insns would
2978 break the code that handles LIBCALL blocks. */
2979 if (set && set_noop_p (set))
2981 /* Nops get in the way while scheduling, so delete them
2982 now if register allocation has already been done. It
2983 is too risky to try to do this before register
2984 allocation, and there are unlikely to be very many
2985 nops then anyways. */
2986 if (reload_completed)
2987 delete_insn_and_edges (insn);
2989 else
2991 if (split_insn (insn))
2993 bitmap_set_bit (blocks, bb->index);
2994 changed = true;
3001 default_rtl_profile ();
3002 if (changed)
3003 find_many_sub_basic_blocks (blocks);
3005 #ifdef ENABLE_CHECKING
3006 verify_flow_info ();
3007 #endif
3009 sbitmap_free (blocks);
3012 /* Same as split_all_insns, but do not expect CFG to be available.
3013 Used by machine dependent reorg passes. */
3015 unsigned int
3016 split_all_insns_noflow (void)
3018 rtx_insn *next, *insn;
3020 for (insn = get_insns (); insn; insn = next)
3022 next = NEXT_INSN (insn);
3023 if (INSN_P (insn))
3025 /* Don't split no-op move insns. These should silently
3026 disappear later in final. Splitting such insns would
3027 break the code that handles LIBCALL blocks. */
3028 rtx set = single_set (insn);
3029 if (set && set_noop_p (set))
3031 /* Nops get in the way while scheduling, so delete them
3032 now if register allocation has already been done. It
3033 is too risky to try to do this before register
3034 allocation, and there are unlikely to be very many
3035 nops then anyways.
3037 ??? Should we use delete_insn when the CFG isn't valid? */
3038 if (reload_completed)
3039 delete_insn_and_edges (insn);
3041 else
3042 split_insn (insn);
3045 return 0;
3048 #ifdef HAVE_peephole2
3049 struct peep2_insn_data
3051 rtx insn;
3052 regset live_before;
3055 static struct peep2_insn_data peep2_insn_data[MAX_INSNS_PER_PEEP2 + 1];
3056 static int peep2_current;
3058 static bool peep2_do_rebuild_jump_labels;
3059 static bool peep2_do_cleanup_cfg;
3061 /* The number of instructions available to match a peep2. */
3062 int peep2_current_count;
3064 /* A non-insn marker indicating the last insn of the block.
3065 The live_before regset for this element is correct, indicating
3066 DF_LIVE_OUT for the block. */
3067 #define PEEP2_EOB pc_rtx
3069 /* Wrap N to fit into the peep2_insn_data buffer. */
3071 static int
3072 peep2_buf_position (int n)
3074 if (n >= MAX_INSNS_PER_PEEP2 + 1)
3075 n -= MAX_INSNS_PER_PEEP2 + 1;
3076 return n;
3079 /* Return the Nth non-note insn after `current', or return NULL_RTX if it
3080 does not exist. Used by the recognizer to find the next insn to match
3081 in a multi-insn pattern. */
3084 peep2_next_insn (int n)
3086 gcc_assert (n <= peep2_current_count);
3088 n = peep2_buf_position (peep2_current + n);
3090 return peep2_insn_data[n].insn;
3093 /* Return true if REGNO is dead before the Nth non-note insn
3094 after `current'. */
3097 peep2_regno_dead_p (int ofs, int regno)
3099 gcc_assert (ofs < MAX_INSNS_PER_PEEP2 + 1);
3101 ofs = peep2_buf_position (peep2_current + ofs);
3103 gcc_assert (peep2_insn_data[ofs].insn != NULL_RTX);
3105 return ! REGNO_REG_SET_P (peep2_insn_data[ofs].live_before, regno);
3108 /* Similarly for a REG. */
3111 peep2_reg_dead_p (int ofs, rtx reg)
3113 int regno, n;
3115 gcc_assert (ofs < MAX_INSNS_PER_PEEP2 + 1);
3117 ofs = peep2_buf_position (peep2_current + ofs);
3119 gcc_assert (peep2_insn_data[ofs].insn != NULL_RTX);
3121 regno = REGNO (reg);
3122 n = hard_regno_nregs[regno][GET_MODE (reg)];
3123 while (--n >= 0)
3124 if (REGNO_REG_SET_P (peep2_insn_data[ofs].live_before, regno + n))
3125 return 0;
3126 return 1;
3129 /* Regno offset to be used in the register search. */
3130 static int search_ofs;
3132 /* Try to find a hard register of mode MODE, matching the register class in
3133 CLASS_STR, which is available at the beginning of insn CURRENT_INSN and
3134 remains available until the end of LAST_INSN. LAST_INSN may be NULL_RTX,
3135 in which case the only condition is that the register must be available
3136 before CURRENT_INSN.
3137 Registers that already have bits set in REG_SET will not be considered.
3139 If an appropriate register is available, it will be returned and the
3140 corresponding bit(s) in REG_SET will be set; otherwise, NULL_RTX is
3141 returned. */
3144 peep2_find_free_register (int from, int to, const char *class_str,
3145 machine_mode mode, HARD_REG_SET *reg_set)
3147 enum reg_class cl;
3148 HARD_REG_SET live;
3149 df_ref def;
3150 int i;
3152 gcc_assert (from < MAX_INSNS_PER_PEEP2 + 1);
3153 gcc_assert (to < MAX_INSNS_PER_PEEP2 + 1);
3155 from = peep2_buf_position (peep2_current + from);
3156 to = peep2_buf_position (peep2_current + to);
3158 gcc_assert (peep2_insn_data[from].insn != NULL_RTX);
3159 REG_SET_TO_HARD_REG_SET (live, peep2_insn_data[from].live_before);
3161 while (from != to)
3163 gcc_assert (peep2_insn_data[from].insn != NULL_RTX);
3165 /* Don't use registers set or clobbered by the insn. */
3166 FOR_EACH_INSN_DEF (def, peep2_insn_data[from].insn)
3167 SET_HARD_REG_BIT (live, DF_REF_REGNO (def));
3169 from = peep2_buf_position (from + 1);
3172 cl = reg_class_for_constraint (lookup_constraint (class_str));
3174 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3176 int raw_regno, regno, success, j;
3178 /* Distribute the free registers as much as possible. */
3179 raw_regno = search_ofs + i;
3180 if (raw_regno >= FIRST_PSEUDO_REGISTER)
3181 raw_regno -= FIRST_PSEUDO_REGISTER;
3182 #ifdef REG_ALLOC_ORDER
3183 regno = reg_alloc_order[raw_regno];
3184 #else
3185 regno = raw_regno;
3186 #endif
3188 /* Can it support the mode we need? */
3189 if (! HARD_REGNO_MODE_OK (regno, mode))
3190 continue;
3192 success = 1;
3193 for (j = 0; success && j < hard_regno_nregs[regno][mode]; j++)
3195 /* Don't allocate fixed registers. */
3196 if (fixed_regs[regno + j])
3198 success = 0;
3199 break;
3201 /* Don't allocate global registers. */
3202 if (global_regs[regno + j])
3204 success = 0;
3205 break;
3207 /* Make sure the register is of the right class. */
3208 if (! TEST_HARD_REG_BIT (reg_class_contents[cl], regno + j))
3210 success = 0;
3211 break;
3213 /* And that we don't create an extra save/restore. */
3214 if (! call_used_regs[regno + j] && ! df_regs_ever_live_p (regno + j))
3216 success = 0;
3217 break;
3220 if (! targetm.hard_regno_scratch_ok (regno + j))
3222 success = 0;
3223 break;
3226 /* And we don't clobber traceback for noreturn functions. */
3227 if ((regno + j == FRAME_POINTER_REGNUM
3228 || regno + j == HARD_FRAME_POINTER_REGNUM)
3229 && (! reload_completed || frame_pointer_needed))
3231 success = 0;
3232 break;
3235 if (TEST_HARD_REG_BIT (*reg_set, regno + j)
3236 || TEST_HARD_REG_BIT (live, regno + j))
3238 success = 0;
3239 break;
3243 if (success)
3245 add_to_hard_reg_set (reg_set, mode, regno);
3247 /* Start the next search with the next register. */
3248 if (++raw_regno >= FIRST_PSEUDO_REGISTER)
3249 raw_regno = 0;
3250 search_ofs = raw_regno;
3252 return gen_rtx_REG (mode, regno);
3256 search_ofs = 0;
3257 return NULL_RTX;
3260 /* Forget all currently tracked instructions, only remember current
3261 LIVE regset. */
3263 static void
3264 peep2_reinit_state (regset live)
3266 int i;
3268 /* Indicate that all slots except the last holds invalid data. */
3269 for (i = 0; i < MAX_INSNS_PER_PEEP2; ++i)
3270 peep2_insn_data[i].insn = NULL_RTX;
3271 peep2_current_count = 0;
3273 /* Indicate that the last slot contains live_after data. */
3274 peep2_insn_data[MAX_INSNS_PER_PEEP2].insn = PEEP2_EOB;
3275 peep2_current = MAX_INSNS_PER_PEEP2;
3277 COPY_REG_SET (peep2_insn_data[MAX_INSNS_PER_PEEP2].live_before, live);
3280 /* While scanning basic block BB, we found a match of length MATCH_LEN,
3281 starting at INSN. Perform the replacement, removing the old insns and
3282 replacing them with ATTEMPT. Returns the last insn emitted, or NULL
3283 if the replacement is rejected. */
3285 static rtx_insn *
3286 peep2_attempt (basic_block bb, rtx uncast_insn, int match_len, rtx_insn *attempt)
3288 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3289 int i;
3290 rtx_insn *last, *before_try, *x;
3291 rtx eh_note, as_note;
3292 rtx_insn *old_insn;
3293 rtx_insn *new_insn;
3294 bool was_call = false;
3296 /* If we are splitting an RTX_FRAME_RELATED_P insn, do not allow it to
3297 match more than one insn, or to be split into more than one insn. */
3298 old_insn = as_a <rtx_insn *> (peep2_insn_data[peep2_current].insn);
3299 if (RTX_FRAME_RELATED_P (old_insn))
3301 bool any_note = false;
3302 rtx note;
3304 if (match_len != 0)
3305 return NULL;
3307 /* Look for one "active" insn. I.e. ignore any "clobber" insns that
3308 may be in the stream for the purpose of register allocation. */
3309 if (active_insn_p (attempt))
3310 new_insn = attempt;
3311 else
3312 new_insn = next_active_insn (attempt);
3313 if (next_active_insn (new_insn))
3314 return NULL;
3316 /* We have a 1-1 replacement. Copy over any frame-related info. */
3317 RTX_FRAME_RELATED_P (new_insn) = 1;
3319 /* Allow the backend to fill in a note during the split. */
3320 for (note = REG_NOTES (new_insn); note ; note = XEXP (note, 1))
3321 switch (REG_NOTE_KIND (note))
3323 case REG_FRAME_RELATED_EXPR:
3324 case REG_CFA_DEF_CFA:
3325 case REG_CFA_ADJUST_CFA:
3326 case REG_CFA_OFFSET:
3327 case REG_CFA_REGISTER:
3328 case REG_CFA_EXPRESSION:
3329 case REG_CFA_RESTORE:
3330 case REG_CFA_SET_VDRAP:
3331 any_note = true;
3332 break;
3333 default:
3334 break;
3337 /* If the backend didn't supply a note, copy one over. */
3338 if (!any_note)
3339 for (note = REG_NOTES (old_insn); note ; note = XEXP (note, 1))
3340 switch (REG_NOTE_KIND (note))
3342 case REG_FRAME_RELATED_EXPR:
3343 case REG_CFA_DEF_CFA:
3344 case REG_CFA_ADJUST_CFA:
3345 case REG_CFA_OFFSET:
3346 case REG_CFA_REGISTER:
3347 case REG_CFA_EXPRESSION:
3348 case REG_CFA_RESTORE:
3349 case REG_CFA_SET_VDRAP:
3350 add_reg_note (new_insn, REG_NOTE_KIND (note), XEXP (note, 0));
3351 any_note = true;
3352 break;
3353 default:
3354 break;
3357 /* If there still isn't a note, make sure the unwind info sees the
3358 same expression as before the split. */
3359 if (!any_note)
3361 rtx old_set, new_set;
3363 /* The old insn had better have been simple, or annotated. */
3364 old_set = single_set (old_insn);
3365 gcc_assert (old_set != NULL);
3367 new_set = single_set (new_insn);
3368 if (!new_set || !rtx_equal_p (new_set, old_set))
3369 add_reg_note (new_insn, REG_FRAME_RELATED_EXPR, old_set);
3372 /* Copy prologue/epilogue status. This is required in order to keep
3373 proper placement of EPILOGUE_BEG and the DW_CFA_remember_state. */
3374 maybe_copy_prologue_epilogue_insn (old_insn, new_insn);
3377 /* If we are splitting a CALL_INSN, look for the CALL_INSN
3378 in SEQ and copy our CALL_INSN_FUNCTION_USAGE and other
3379 cfg-related call notes. */
3380 for (i = 0; i <= match_len; ++i)
3382 int j;
3383 rtx note;
3385 j = peep2_buf_position (peep2_current + i);
3386 old_insn = as_a <rtx_insn *> (peep2_insn_data[j].insn);
3387 if (!CALL_P (old_insn))
3388 continue;
3389 was_call = true;
3391 new_insn = attempt;
3392 while (new_insn != NULL_RTX)
3394 if (CALL_P (new_insn))
3395 break;
3396 new_insn = NEXT_INSN (new_insn);
3399 gcc_assert (new_insn != NULL_RTX);
3401 CALL_INSN_FUNCTION_USAGE (new_insn)
3402 = CALL_INSN_FUNCTION_USAGE (old_insn);
3403 SIBLING_CALL_P (new_insn) = SIBLING_CALL_P (old_insn);
3405 for (note = REG_NOTES (old_insn);
3406 note;
3407 note = XEXP (note, 1))
3408 switch (REG_NOTE_KIND (note))
3410 case REG_NORETURN:
3411 case REG_SETJMP:
3412 case REG_TM:
3413 add_reg_note (new_insn, REG_NOTE_KIND (note),
3414 XEXP (note, 0));
3415 break;
3416 default:
3417 /* Discard all other reg notes. */
3418 break;
3421 /* Croak if there is another call in the sequence. */
3422 while (++i <= match_len)
3424 j = peep2_buf_position (peep2_current + i);
3425 old_insn = as_a <rtx_insn *> (peep2_insn_data[j].insn);
3426 gcc_assert (!CALL_P (old_insn));
3428 break;
3431 /* If we matched any instruction that had a REG_ARGS_SIZE, then
3432 move those notes over to the new sequence. */
3433 as_note = NULL;
3434 for (i = match_len; i >= 0; --i)
3436 int j = peep2_buf_position (peep2_current + i);
3437 old_insn = as_a <rtx_insn *> (peep2_insn_data[j].insn);
3439 as_note = find_reg_note (old_insn, REG_ARGS_SIZE, NULL);
3440 if (as_note)
3441 break;
3444 i = peep2_buf_position (peep2_current + match_len);
3445 eh_note = find_reg_note (peep2_insn_data[i].insn, REG_EH_REGION, NULL_RTX);
3447 /* Replace the old sequence with the new. */
3448 rtx_insn *peepinsn = as_a <rtx_insn *> (peep2_insn_data[i].insn);
3449 last = emit_insn_after_setloc (attempt,
3450 peep2_insn_data[i].insn,
3451 INSN_LOCATION (peepinsn));
3452 before_try = PREV_INSN (insn);
3453 delete_insn_chain (insn, peep2_insn_data[i].insn, false);
3455 /* Re-insert the EH_REGION notes. */
3456 if (eh_note || (was_call && nonlocal_goto_handler_labels))
3458 edge eh_edge;
3459 edge_iterator ei;
3461 FOR_EACH_EDGE (eh_edge, ei, bb->succs)
3462 if (eh_edge->flags & (EDGE_EH | EDGE_ABNORMAL_CALL))
3463 break;
3465 if (eh_note)
3466 copy_reg_eh_region_note_backward (eh_note, last, before_try);
3468 if (eh_edge)
3469 for (x = last; x != before_try; x = PREV_INSN (x))
3470 if (x != BB_END (bb)
3471 && (can_throw_internal (x)
3472 || can_nonlocal_goto (x)))
3474 edge nfte, nehe;
3475 int flags;
3477 nfte = split_block (bb, x);
3478 flags = (eh_edge->flags
3479 & (EDGE_EH | EDGE_ABNORMAL));
3480 if (CALL_P (x))
3481 flags |= EDGE_ABNORMAL_CALL;
3482 nehe = make_edge (nfte->src, eh_edge->dest,
3483 flags);
3485 nehe->probability = eh_edge->probability;
3486 nfte->probability
3487 = REG_BR_PROB_BASE - nehe->probability;
3489 peep2_do_cleanup_cfg |= purge_dead_edges (nfte->dest);
3490 bb = nfte->src;
3491 eh_edge = nehe;
3494 /* Converting possibly trapping insn to non-trapping is
3495 possible. Zap dummy outgoing edges. */
3496 peep2_do_cleanup_cfg |= purge_dead_edges (bb);
3499 /* Re-insert the ARGS_SIZE notes. */
3500 if (as_note)
3501 fixup_args_size_notes (before_try, last, INTVAL (XEXP (as_note, 0)));
3503 /* If we generated a jump instruction, it won't have
3504 JUMP_LABEL set. Recompute after we're done. */
3505 for (x = last; x != before_try; x = PREV_INSN (x))
3506 if (JUMP_P (x))
3508 peep2_do_rebuild_jump_labels = true;
3509 break;
3512 return last;
3515 /* After performing a replacement in basic block BB, fix up the life
3516 information in our buffer. LAST is the last of the insns that we
3517 emitted as a replacement. PREV is the insn before the start of
3518 the replacement. MATCH_LEN is the number of instructions that were
3519 matched, and which now need to be replaced in the buffer. */
3521 static void
3522 peep2_update_life (basic_block bb, int match_len, rtx_insn *last,
3523 rtx_insn *prev)
3525 int i = peep2_buf_position (peep2_current + match_len + 1);
3526 rtx_insn *x;
3527 regset_head live;
3529 INIT_REG_SET (&live);
3530 COPY_REG_SET (&live, peep2_insn_data[i].live_before);
3532 gcc_assert (peep2_current_count >= match_len + 1);
3533 peep2_current_count -= match_len + 1;
3535 x = last;
3538 if (INSN_P (x))
3540 df_insn_rescan (x);
3541 if (peep2_current_count < MAX_INSNS_PER_PEEP2)
3543 peep2_current_count++;
3544 if (--i < 0)
3545 i = MAX_INSNS_PER_PEEP2;
3546 peep2_insn_data[i].insn = x;
3547 df_simulate_one_insn_backwards (bb, x, &live);
3548 COPY_REG_SET (peep2_insn_data[i].live_before, &live);
3551 x = PREV_INSN (x);
3553 while (x != prev);
3554 CLEAR_REG_SET (&live);
3556 peep2_current = i;
3559 /* Add INSN, which is in BB, at the end of the peep2 insn buffer if possible.
3560 Return true if we added it, false otherwise. The caller will try to match
3561 peepholes against the buffer if we return false; otherwise it will try to
3562 add more instructions to the buffer. */
3564 static bool
3565 peep2_fill_buffer (basic_block bb, rtx insn, regset live)
3567 int pos;
3569 /* Once we have filled the maximum number of insns the buffer can hold,
3570 allow the caller to match the insns against peepholes. We wait until
3571 the buffer is full in case the target has similar peepholes of different
3572 length; we always want to match the longest if possible. */
3573 if (peep2_current_count == MAX_INSNS_PER_PEEP2)
3574 return false;
3576 /* If an insn has RTX_FRAME_RELATED_P set, do not allow it to be matched with
3577 any other pattern, lest it change the semantics of the frame info. */
3578 if (RTX_FRAME_RELATED_P (insn))
3580 /* Let the buffer drain first. */
3581 if (peep2_current_count > 0)
3582 return false;
3583 /* Now the insn will be the only thing in the buffer. */
3586 pos = peep2_buf_position (peep2_current + peep2_current_count);
3587 peep2_insn_data[pos].insn = insn;
3588 COPY_REG_SET (peep2_insn_data[pos].live_before, live);
3589 peep2_current_count++;
3591 df_simulate_one_insn_forwards (bb, as_a <rtx_insn *> (insn), live);
3592 return true;
3595 /* Perform the peephole2 optimization pass. */
3597 static void
3598 peephole2_optimize (void)
3600 rtx_insn *insn;
3601 bitmap live;
3602 int i;
3603 basic_block bb;
3605 peep2_do_cleanup_cfg = false;
3606 peep2_do_rebuild_jump_labels = false;
3608 df_set_flags (DF_LR_RUN_DCE);
3609 df_note_add_problem ();
3610 df_analyze ();
3612 /* Initialize the regsets we're going to use. */
3613 for (i = 0; i < MAX_INSNS_PER_PEEP2 + 1; ++i)
3614 peep2_insn_data[i].live_before = BITMAP_ALLOC (&reg_obstack);
3615 search_ofs = 0;
3616 live = BITMAP_ALLOC (&reg_obstack);
3618 FOR_EACH_BB_REVERSE_FN (bb, cfun)
3620 bool past_end = false;
3621 int pos;
3623 rtl_profile_for_bb (bb);
3625 /* Start up propagation. */
3626 bitmap_copy (live, DF_LR_IN (bb));
3627 df_simulate_initialize_forwards (bb, live);
3628 peep2_reinit_state (live);
3630 insn = BB_HEAD (bb);
3631 for (;;)
3633 rtx_insn *attempt;
3634 rtx 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;