]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/recog.c
always define STACK_GROWS_DOWNWARD
[thirdparty/gcc.git] / gcc / recog.c
1 /* Subroutines used by or related to instruction recognition.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "rtl-error.h"
36 #include "tm_p.h"
37 #include "insn-config.h"
38 #include "insn-attr.h"
39 #include "hard-reg-set.h"
40 #include "recog.h"
41 #include "regs.h"
42 #include "addresses.h"
43 #include "hashtab.h"
44 #include "function.h"
45 #include "rtl.h"
46 #include "flags.h"
47 #include "statistics.h"
48 #include "real.h"
49 #include "fixed-value.h"
50 #include "expmed.h"
51 #include "dojump.h"
52 #include "explow.h"
53 #include "calls.h"
54 #include "emit-rtl.h"
55 #include "varasm.h"
56 #include "stmt.h"
57 #include "expr.h"
58 #include "predict.h"
59 #include "dominance.h"
60 #include "cfg.h"
61 #include "cfgrtl.h"
62 #include "cfgbuild.h"
63 #include "cfgcleanup.h"
64 #include "basic-block.h"
65 #include "reload.h"
66 #include "target.h"
67 #include "tree-pass.h"
68 #include "df.h"
69 #include "insn-codes.h"
70
71 #ifndef STACK_PUSH_CODE
72 #if STACK_GROWS_DOWNWARD
73 #define STACK_PUSH_CODE PRE_DEC
74 #else
75 #define STACK_PUSH_CODE PRE_INC
76 #endif
77 #endif
78
79 #ifndef STACK_POP_CODE
80 #if STACK_GROWS_DOWNWARD
81 #define STACK_POP_CODE POST_INC
82 #else
83 #define STACK_POP_CODE POST_DEC
84 #endif
85 #endif
86
87 static void validate_replace_rtx_1 (rtx *, rtx, rtx, rtx_insn *, bool);
88 static void validate_replace_src_1 (rtx *, void *);
89 static rtx_insn *split_insn (rtx_insn *);
90
91 struct target_recog default_target_recog;
92 #if SWITCHABLE_TARGET
93 struct target_recog *this_target_recog = &default_target_recog;
94 #endif
95
96 /* Nonzero means allow operands to be volatile.
97 This should be 0 if you are generating rtl, such as if you are calling
98 the functions in optabs.c and expmed.c (most of the time).
99 This should be 1 if all valid insns need to be recognized,
100 such as in reginfo.c and final.c and reload.c.
101
102 init_recog and init_recog_no_volatile are responsible for setting this. */
103
104 int volatile_ok;
105
106 struct recog_data_d recog_data;
107
108 /* Contains a vector of operand_alternative structures, such that
109 operand OP of alternative A is at index A * n_operands + OP.
110 Set up by preprocess_constraints. */
111 const operand_alternative *recog_op_alt;
112
113 /* Used to provide recog_op_alt for asms. */
114 static operand_alternative asm_op_alt[MAX_RECOG_OPERANDS
115 * MAX_RECOG_ALTERNATIVES];
116
117 /* On return from `constrain_operands', indicate which alternative
118 was satisfied. */
119
120 int which_alternative;
121
122 /* Nonzero after end of reload pass.
123 Set to 1 or 0 by toplev.c.
124 Controls the significance of (SUBREG (MEM)). */
125
126 int reload_completed;
127
128 /* Nonzero after thread_prologue_and_epilogue_insns has run. */
129 int epilogue_completed;
130
131 /* Initialize data used by the function `recog'.
132 This must be called once in the compilation of a function
133 before any insn recognition may be done in the function. */
134
135 void
136 init_recog_no_volatile (void)
137 {
138 volatile_ok = 0;
139 }
140
141 void
142 init_recog (void)
143 {
144 volatile_ok = 1;
145 }
146
147 \f
148 /* Return true if labels in asm operands BODY are LABEL_REFs. */
149
150 static bool
151 asm_labels_ok (rtx body)
152 {
153 rtx asmop;
154 int i;
155
156 asmop = extract_asm_operands (body);
157 if (asmop == NULL_RTX)
158 return true;
159
160 for (i = 0; i < ASM_OPERANDS_LABEL_LENGTH (asmop); i++)
161 if (GET_CODE (ASM_OPERANDS_LABEL (asmop, i)) != LABEL_REF)
162 return false;
163
164 return true;
165 }
166
167 /* Check that X is an insn-body for an `asm' with operands
168 and that the operands mentioned in it are legitimate. */
169
170 int
171 check_asm_operands (rtx x)
172 {
173 int noperands;
174 rtx *operands;
175 const char **constraints;
176 int i;
177
178 if (!asm_labels_ok (x))
179 return 0;
180
181 /* Post-reload, be more strict with things. */
182 if (reload_completed)
183 {
184 /* ??? Doh! We've not got the wrapping insn. Cook one up. */
185 rtx_insn *insn = make_insn_raw (x);
186 extract_insn (insn);
187 constrain_operands (1, get_enabled_alternatives (insn));
188 return which_alternative >= 0;
189 }
190
191 noperands = asm_noperands (x);
192 if (noperands < 0)
193 return 0;
194 if (noperands == 0)
195 return 1;
196
197 operands = XALLOCAVEC (rtx, noperands);
198 constraints = XALLOCAVEC (const char *, noperands);
199
200 decode_asm_operands (x, operands, NULL, constraints, NULL, NULL);
201
202 for (i = 0; i < noperands; i++)
203 {
204 const char *c = constraints[i];
205 if (c[0] == '%')
206 c++;
207 if (! asm_operand_ok (operands[i], c, constraints))
208 return 0;
209 }
210
211 return 1;
212 }
213 \f
214 /* Static data for the next two routines. */
215
216 typedef struct change_t
217 {
218 rtx object;
219 int old_code;
220 rtx *loc;
221 rtx old;
222 bool unshare;
223 } change_t;
224
225 static change_t *changes;
226 static int changes_allocated;
227
228 static int num_changes = 0;
229
230 /* Validate a proposed change to OBJECT. LOC is the location in the rtl
231 at which NEW_RTX will be placed. If OBJECT is zero, no validation is done,
232 the change is simply made.
233
234 Two types of objects are supported: If OBJECT is a MEM, memory_address_p
235 will be called with the address and mode as parameters. If OBJECT is
236 an INSN, CALL_INSN, or JUMP_INSN, the insn will be re-recognized with
237 the change in place.
238
239 IN_GROUP is nonzero if this is part of a group of changes that must be
240 performed as a group. In that case, the changes will be stored. The
241 function `apply_change_group' will validate and apply the changes.
242
243 If IN_GROUP is zero, this is a single change. Try to recognize the insn
244 or validate the memory reference with the change applied. If the result
245 is not valid for the machine, suppress the change and return zero.
246 Otherwise, perform the change and return 1. */
247
248 static bool
249 validate_change_1 (rtx object, rtx *loc, rtx new_rtx, bool in_group, bool unshare)
250 {
251 rtx old = *loc;
252
253 if (old == new_rtx || rtx_equal_p (old, new_rtx))
254 return 1;
255
256 gcc_assert (in_group != 0 || num_changes == 0);
257
258 *loc = new_rtx;
259
260 /* Save the information describing this change. */
261 if (num_changes >= changes_allocated)
262 {
263 if (changes_allocated == 0)
264 /* This value allows for repeated substitutions inside complex
265 indexed addresses, or changes in up to 5 insns. */
266 changes_allocated = MAX_RECOG_OPERANDS * 5;
267 else
268 changes_allocated *= 2;
269
270 changes = XRESIZEVEC (change_t, changes, changes_allocated);
271 }
272
273 changes[num_changes].object = object;
274 changes[num_changes].loc = loc;
275 changes[num_changes].old = old;
276 changes[num_changes].unshare = unshare;
277
278 if (object && !MEM_P (object))
279 {
280 /* Set INSN_CODE to force rerecognition of insn. Save old code in
281 case invalid. */
282 changes[num_changes].old_code = INSN_CODE (object);
283 INSN_CODE (object) = -1;
284 }
285
286 num_changes++;
287
288 /* If we are making a group of changes, return 1. Otherwise, validate the
289 change group we made. */
290
291 if (in_group)
292 return 1;
293 else
294 return apply_change_group ();
295 }
296
297 /* Wrapper for validate_change_1 without the UNSHARE argument defaulting
298 UNSHARE to false. */
299
300 bool
301 validate_change (rtx object, rtx *loc, rtx new_rtx, bool in_group)
302 {
303 return validate_change_1 (object, loc, new_rtx, in_group, false);
304 }
305
306 /* Wrapper for validate_change_1 without the UNSHARE argument defaulting
307 UNSHARE to true. */
308
309 bool
310 validate_unshare_change (rtx object, rtx *loc, rtx new_rtx, bool in_group)
311 {
312 return validate_change_1 (object, loc, new_rtx, in_group, true);
313 }
314
315
316 /* Keep X canonicalized if some changes have made it non-canonical; only
317 modifies the operands of X, not (for example) its code. Simplifications
318 are not the job of this routine.
319
320 Return true if anything was changed. */
321 bool
322 canonicalize_change_group (rtx_insn *insn, rtx x)
323 {
324 if (COMMUTATIVE_P (x)
325 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
326 {
327 /* Oops, the caller has made X no longer canonical.
328 Let's redo the changes in the correct order. */
329 rtx tem = XEXP (x, 0);
330 validate_unshare_change (insn, &XEXP (x, 0), XEXP (x, 1), 1);
331 validate_unshare_change (insn, &XEXP (x, 1), tem, 1);
332 return true;
333 }
334 else
335 return false;
336 }
337
338
339 /* This subroutine of apply_change_group verifies whether the changes to INSN
340 were valid; i.e. whether INSN can still be recognized.
341
342 If IN_GROUP is true clobbers which have to be added in order to
343 match the instructions will be added to the current change group.
344 Otherwise the changes will take effect immediately. */
345
346 int
347 insn_invalid_p (rtx_insn *insn, bool in_group)
348 {
349 rtx pat = PATTERN (insn);
350 int num_clobbers = 0;
351 /* If we are before reload and the pattern is a SET, see if we can add
352 clobbers. */
353 int icode = recog (pat, insn,
354 (GET_CODE (pat) == SET
355 && ! reload_completed
356 && ! reload_in_progress)
357 ? &num_clobbers : 0);
358 int is_asm = icode < 0 && asm_noperands (PATTERN (insn)) >= 0;
359
360
361 /* If this is an asm and the operand aren't legal, then fail. Likewise if
362 this is not an asm and the insn wasn't recognized. */
363 if ((is_asm && ! check_asm_operands (PATTERN (insn)))
364 || (!is_asm && icode < 0))
365 return 1;
366
367 /* If we have to add CLOBBERs, fail if we have to add ones that reference
368 hard registers since our callers can't know if they are live or not.
369 Otherwise, add them. */
370 if (num_clobbers > 0)
371 {
372 rtx newpat;
373
374 if (added_clobbers_hard_reg_p (icode))
375 return 1;
376
377 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_clobbers + 1));
378 XVECEXP (newpat, 0, 0) = pat;
379 add_clobbers (newpat, icode);
380 if (in_group)
381 validate_change (insn, &PATTERN (insn), newpat, 1);
382 else
383 PATTERN (insn) = pat = newpat;
384 }
385
386 /* After reload, verify that all constraints are satisfied. */
387 if (reload_completed)
388 {
389 extract_insn (insn);
390
391 if (! constrain_operands (1, get_preferred_alternatives (insn)))
392 return 1;
393 }
394
395 INSN_CODE (insn) = icode;
396 return 0;
397 }
398
399 /* Return number of changes made and not validated yet. */
400 int
401 num_changes_pending (void)
402 {
403 return num_changes;
404 }
405
406 /* Tentatively apply the changes numbered NUM and up.
407 Return 1 if all changes are valid, zero otherwise. */
408
409 int
410 verify_changes (int num)
411 {
412 int i;
413 rtx last_validated = NULL_RTX;
414
415 /* The changes have been applied and all INSN_CODEs have been reset to force
416 rerecognition.
417
418 The changes are valid if we aren't given an object, or if we are
419 given a MEM and it still is a valid address, or if this is in insn
420 and it is recognized. In the latter case, if reload has completed,
421 we also require that the operands meet the constraints for
422 the insn. */
423
424 for (i = num; i < num_changes; i++)
425 {
426 rtx object = changes[i].object;
427
428 /* If there is no object to test or if it is the same as the one we
429 already tested, ignore it. */
430 if (object == 0 || object == last_validated)
431 continue;
432
433 if (MEM_P (object))
434 {
435 if (! memory_address_addr_space_p (GET_MODE (object),
436 XEXP (object, 0),
437 MEM_ADDR_SPACE (object)))
438 break;
439 }
440 else if (/* changes[i].old might be zero, e.g. when putting a
441 REG_FRAME_RELATED_EXPR into a previously empty list. */
442 changes[i].old
443 && REG_P (changes[i].old)
444 && asm_noperands (PATTERN (object)) > 0
445 && REG_EXPR (changes[i].old) != NULL_TREE
446 && DECL_ASSEMBLER_NAME_SET_P (REG_EXPR (changes[i].old))
447 && DECL_REGISTER (REG_EXPR (changes[i].old)))
448 {
449 /* Don't allow changes of hard register operands to inline
450 assemblies if they have been defined as register asm ("x"). */
451 break;
452 }
453 else if (DEBUG_INSN_P (object))
454 continue;
455 else if (insn_invalid_p (as_a <rtx_insn *> (object), true))
456 {
457 rtx pat = PATTERN (object);
458
459 /* Perhaps we couldn't recognize the insn because there were
460 extra CLOBBERs at the end. If so, try to re-recognize
461 without the last CLOBBER (later iterations will cause each of
462 them to be eliminated, in turn). But don't do this if we
463 have an ASM_OPERAND. */
464 if (GET_CODE (pat) == PARALLEL
465 && GET_CODE (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1)) == CLOBBER
466 && asm_noperands (PATTERN (object)) < 0)
467 {
468 rtx newpat;
469
470 if (XVECLEN (pat, 0) == 2)
471 newpat = XVECEXP (pat, 0, 0);
472 else
473 {
474 int j;
475
476 newpat
477 = gen_rtx_PARALLEL (VOIDmode,
478 rtvec_alloc (XVECLEN (pat, 0) - 1));
479 for (j = 0; j < XVECLEN (newpat, 0); j++)
480 XVECEXP (newpat, 0, j) = XVECEXP (pat, 0, j);
481 }
482
483 /* Add a new change to this group to replace the pattern
484 with this new pattern. Then consider this change
485 as having succeeded. The change we added will
486 cause the entire call to fail if things remain invalid.
487
488 Note that this can lose if a later change than the one
489 we are processing specified &XVECEXP (PATTERN (object), 0, X)
490 but this shouldn't occur. */
491
492 validate_change (object, &PATTERN (object), newpat, 1);
493 continue;
494 }
495 else if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER
496 || GET_CODE (pat) == VAR_LOCATION)
497 /* If this insn is a CLOBBER or USE, it is always valid, but is
498 never recognized. */
499 continue;
500 else
501 break;
502 }
503 last_validated = object;
504 }
505
506 return (i == num_changes);
507 }
508
509 /* A group of changes has previously been issued with validate_change
510 and verified with verify_changes. Call df_insn_rescan for each of
511 the insn changed and clear num_changes. */
512
513 void
514 confirm_change_group (void)
515 {
516 int i;
517 rtx last_object = NULL;
518
519 for (i = 0; i < num_changes; i++)
520 {
521 rtx object = changes[i].object;
522
523 if (changes[i].unshare)
524 *changes[i].loc = copy_rtx (*changes[i].loc);
525
526 /* Avoid unnecessary rescanning when multiple changes to same instruction
527 are made. */
528 if (object)
529 {
530 if (object != last_object && last_object && INSN_P (last_object))
531 df_insn_rescan (as_a <rtx_insn *> (last_object));
532 last_object = object;
533 }
534 }
535
536 if (last_object && INSN_P (last_object))
537 df_insn_rescan (as_a <rtx_insn *> (last_object));
538 num_changes = 0;
539 }
540
541 /* Apply a group of changes previously issued with `validate_change'.
542 If all changes are valid, call confirm_change_group and return 1,
543 otherwise, call cancel_changes and return 0. */
544
545 int
546 apply_change_group (void)
547 {
548 if (verify_changes (0))
549 {
550 confirm_change_group ();
551 return 1;
552 }
553 else
554 {
555 cancel_changes (0);
556 return 0;
557 }
558 }
559
560
561 /* Return the number of changes so far in the current group. */
562
563 int
564 num_validated_changes (void)
565 {
566 return num_changes;
567 }
568
569 /* Retract the changes numbered NUM and up. */
570
571 void
572 cancel_changes (int num)
573 {
574 int i;
575
576 /* Back out all the changes. Do this in the opposite order in which
577 they were made. */
578 for (i = num_changes - 1; i >= num; i--)
579 {
580 *changes[i].loc = changes[i].old;
581 if (changes[i].object && !MEM_P (changes[i].object))
582 INSN_CODE (changes[i].object) = changes[i].old_code;
583 }
584 num_changes = num;
585 }
586
587 /* Reduce conditional compilation elsewhere. */
588 #ifndef HAVE_extv
589 #define HAVE_extv 0
590 #define CODE_FOR_extv CODE_FOR_nothing
591 #endif
592 #ifndef HAVE_extzv
593 #define HAVE_extzv 0
594 #define CODE_FOR_extzv CODE_FOR_nothing
595 #endif
596
597 /* A subroutine of validate_replace_rtx_1 that tries to simplify the resulting
598 rtx. */
599
600 static void
601 simplify_while_replacing (rtx *loc, rtx to, rtx_insn *object,
602 machine_mode op0_mode)
603 {
604 rtx x = *loc;
605 enum rtx_code code = GET_CODE (x);
606 rtx new_rtx = NULL_RTX;
607
608 if (SWAPPABLE_OPERANDS_P (x)
609 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
610 {
611 validate_unshare_change (object, loc,
612 gen_rtx_fmt_ee (COMMUTATIVE_ARITH_P (x) ? code
613 : swap_condition (code),
614 GET_MODE (x), XEXP (x, 1),
615 XEXP (x, 0)), 1);
616 x = *loc;
617 code = GET_CODE (x);
618 }
619
620 /* Canonicalize arithmetics with all constant operands. */
621 switch (GET_RTX_CLASS (code))
622 {
623 case RTX_UNARY:
624 if (CONSTANT_P (XEXP (x, 0)))
625 new_rtx = simplify_unary_operation (code, GET_MODE (x), XEXP (x, 0),
626 op0_mode);
627 break;
628 case RTX_COMM_ARITH:
629 case RTX_BIN_ARITH:
630 if (CONSTANT_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
631 new_rtx = simplify_binary_operation (code, GET_MODE (x), XEXP (x, 0),
632 XEXP (x, 1));
633 break;
634 case RTX_COMPARE:
635 case RTX_COMM_COMPARE:
636 if (CONSTANT_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
637 new_rtx = simplify_relational_operation (code, GET_MODE (x), op0_mode,
638 XEXP (x, 0), XEXP (x, 1));
639 break;
640 default:
641 break;
642 }
643 if (new_rtx)
644 {
645 validate_change (object, loc, new_rtx, 1);
646 return;
647 }
648
649 switch (code)
650 {
651 case PLUS:
652 /* If we have a PLUS whose second operand is now a CONST_INT, use
653 simplify_gen_binary to try to simplify it.
654 ??? We may want later to remove this, once simplification is
655 separated from this function. */
656 if (CONST_INT_P (XEXP (x, 1)) && XEXP (x, 1) == to)
657 validate_change (object, loc,
658 simplify_gen_binary
659 (PLUS, GET_MODE (x), XEXP (x, 0), XEXP (x, 1)), 1);
660 break;
661 case MINUS:
662 if (CONST_SCALAR_INT_P (XEXP (x, 1)))
663 validate_change (object, loc,
664 simplify_gen_binary
665 (PLUS, GET_MODE (x), XEXP (x, 0),
666 simplify_gen_unary (NEG,
667 GET_MODE (x), XEXP (x, 1),
668 GET_MODE (x))), 1);
669 break;
670 case ZERO_EXTEND:
671 case SIGN_EXTEND:
672 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
673 {
674 new_rtx = simplify_gen_unary (code, GET_MODE (x), XEXP (x, 0),
675 op0_mode);
676 /* If any of the above failed, substitute in something that
677 we know won't be recognized. */
678 if (!new_rtx)
679 new_rtx = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
680 validate_change (object, loc, new_rtx, 1);
681 }
682 break;
683 case SUBREG:
684 /* All subregs possible to simplify should be simplified. */
685 new_rtx = simplify_subreg (GET_MODE (x), SUBREG_REG (x), op0_mode,
686 SUBREG_BYTE (x));
687
688 /* Subregs of VOIDmode operands are incorrect. */
689 if (!new_rtx && GET_MODE (SUBREG_REG (x)) == VOIDmode)
690 new_rtx = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
691 if (new_rtx)
692 validate_change (object, loc, new_rtx, 1);
693 break;
694 case ZERO_EXTRACT:
695 case SIGN_EXTRACT:
696 /* If we are replacing a register with memory, try to change the memory
697 to be the mode required for memory in extract operations (this isn't
698 likely to be an insertion operation; if it was, nothing bad will
699 happen, we might just fail in some cases). */
700
701 if (MEM_P (XEXP (x, 0))
702 && CONST_INT_P (XEXP (x, 1))
703 && CONST_INT_P (XEXP (x, 2))
704 && !mode_dependent_address_p (XEXP (XEXP (x, 0), 0),
705 MEM_ADDR_SPACE (XEXP (x, 0)))
706 && !MEM_VOLATILE_P (XEXP (x, 0)))
707 {
708 machine_mode wanted_mode = VOIDmode;
709 machine_mode is_mode = GET_MODE (XEXP (x, 0));
710 int pos = INTVAL (XEXP (x, 2));
711
712 if (GET_CODE (x) == ZERO_EXTRACT && HAVE_extzv)
713 {
714 wanted_mode = insn_data[CODE_FOR_extzv].operand[1].mode;
715 if (wanted_mode == VOIDmode)
716 wanted_mode = word_mode;
717 }
718 else if (GET_CODE (x) == SIGN_EXTRACT && HAVE_extv)
719 {
720 wanted_mode = insn_data[CODE_FOR_extv].operand[1].mode;
721 if (wanted_mode == VOIDmode)
722 wanted_mode = word_mode;
723 }
724
725 /* If we have a narrower mode, we can do something. */
726 if (wanted_mode != VOIDmode
727 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
728 {
729 int offset = pos / BITS_PER_UNIT;
730 rtx newmem;
731
732 /* If the bytes and bits are counted differently, we
733 must adjust the offset. */
734 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
735 offset =
736 (GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (wanted_mode) -
737 offset);
738
739 gcc_assert (GET_MODE_PRECISION (wanted_mode)
740 == GET_MODE_BITSIZE (wanted_mode));
741 pos %= GET_MODE_BITSIZE (wanted_mode);
742
743 newmem = adjust_address_nv (XEXP (x, 0), wanted_mode, offset);
744
745 validate_change (object, &XEXP (x, 2), GEN_INT (pos), 1);
746 validate_change (object, &XEXP (x, 0), newmem, 1);
747 }
748 }
749
750 break;
751
752 default:
753 break;
754 }
755 }
756
757 /* Replace every occurrence of FROM in X with TO. Mark each change with
758 validate_change passing OBJECT. */
759
760 static void
761 validate_replace_rtx_1 (rtx *loc, rtx from, rtx to, rtx_insn *object,
762 bool simplify)
763 {
764 int i, j;
765 const char *fmt;
766 rtx x = *loc;
767 enum rtx_code code;
768 machine_mode op0_mode = VOIDmode;
769 int prev_changes = num_changes;
770
771 if (!x)
772 return;
773
774 code = GET_CODE (x);
775 fmt = GET_RTX_FORMAT (code);
776 if (fmt[0] == 'e')
777 op0_mode = GET_MODE (XEXP (x, 0));
778
779 /* X matches FROM if it is the same rtx or they are both referring to the
780 same register in the same mode. Avoid calling rtx_equal_p unless the
781 operands look similar. */
782
783 if (x == from
784 || (REG_P (x) && REG_P (from)
785 && GET_MODE (x) == GET_MODE (from)
786 && REGNO (x) == REGNO (from))
787 || (GET_CODE (x) == GET_CODE (from) && GET_MODE (x) == GET_MODE (from)
788 && rtx_equal_p (x, from)))
789 {
790 validate_unshare_change (object, loc, to, 1);
791 return;
792 }
793
794 /* Call ourself recursively to perform the replacements.
795 We must not replace inside already replaced expression, otherwise we
796 get infinite recursion for replacements like (reg X)->(subreg (reg X))
797 so we must special case shared ASM_OPERANDS. */
798
799 if (GET_CODE (x) == PARALLEL)
800 {
801 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
802 {
803 if (j && GET_CODE (XVECEXP (x, 0, j)) == SET
804 && GET_CODE (SET_SRC (XVECEXP (x, 0, j))) == ASM_OPERANDS)
805 {
806 /* Verify that operands are really shared. */
807 gcc_assert (ASM_OPERANDS_INPUT_VEC (SET_SRC (XVECEXP (x, 0, 0)))
808 == ASM_OPERANDS_INPUT_VEC (SET_SRC (XVECEXP
809 (x, 0, j))));
810 validate_replace_rtx_1 (&SET_DEST (XVECEXP (x, 0, j)),
811 from, to, object, simplify);
812 }
813 else
814 validate_replace_rtx_1 (&XVECEXP (x, 0, j), from, to, object,
815 simplify);
816 }
817 }
818 else
819 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
820 {
821 if (fmt[i] == 'e')
822 validate_replace_rtx_1 (&XEXP (x, i), from, to, object, simplify);
823 else if (fmt[i] == 'E')
824 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
825 validate_replace_rtx_1 (&XVECEXP (x, i, j), from, to, object,
826 simplify);
827 }
828
829 /* If we didn't substitute, there is nothing more to do. */
830 if (num_changes == prev_changes)
831 return;
832
833 /* ??? The regmove is no more, so is this aberration still necessary? */
834 /* Allow substituted expression to have different mode. This is used by
835 regmove to change mode of pseudo register. */
836 if (fmt[0] == 'e' && GET_MODE (XEXP (x, 0)) != VOIDmode)
837 op0_mode = GET_MODE (XEXP (x, 0));
838
839 /* Do changes needed to keep rtx consistent. Don't do any other
840 simplifications, as it is not our job. */
841 if (simplify)
842 simplify_while_replacing (loc, to, object, op0_mode);
843 }
844
845 /* Try replacing every occurrence of FROM in subexpression LOC of INSN
846 with TO. After all changes have been made, validate by seeing
847 if INSN is still valid. */
848
849 int
850 validate_replace_rtx_subexp (rtx from, rtx to, rtx_insn *insn, rtx *loc)
851 {
852 validate_replace_rtx_1 (loc, from, to, insn, true);
853 return apply_change_group ();
854 }
855
856 /* Try replacing every occurrence of FROM in INSN with TO. After all
857 changes have been made, validate by seeing if INSN is still valid. */
858
859 int
860 validate_replace_rtx (rtx from, rtx to, rtx_insn *insn)
861 {
862 validate_replace_rtx_1 (&PATTERN (insn), from, to, insn, true);
863 return apply_change_group ();
864 }
865
866 /* Try replacing every occurrence of FROM in WHERE with TO. Assume that WHERE
867 is a part of INSN. After all changes have been made, validate by seeing if
868 INSN is still valid.
869 validate_replace_rtx (from, to, insn) is equivalent to
870 validate_replace_rtx_part (from, to, &PATTERN (insn), insn). */
871
872 int
873 validate_replace_rtx_part (rtx from, rtx to, rtx *where, rtx_insn *insn)
874 {
875 validate_replace_rtx_1 (where, from, to, insn, true);
876 return apply_change_group ();
877 }
878
879 /* Same as above, but do not simplify rtx afterwards. */
880 int
881 validate_replace_rtx_part_nosimplify (rtx from, rtx to, rtx *where,
882 rtx_insn *insn)
883 {
884 validate_replace_rtx_1 (where, from, to, insn, false);
885 return apply_change_group ();
886
887 }
888
889 /* Try replacing every occurrence of FROM in INSN with TO. This also
890 will replace in REG_EQUAL and REG_EQUIV notes. */
891
892 void
893 validate_replace_rtx_group (rtx from, rtx to, rtx_insn *insn)
894 {
895 rtx note;
896 validate_replace_rtx_1 (&PATTERN (insn), from, to, insn, true);
897 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
898 if (REG_NOTE_KIND (note) == REG_EQUAL
899 || REG_NOTE_KIND (note) == REG_EQUIV)
900 validate_replace_rtx_1 (&XEXP (note, 0), from, to, insn, true);
901 }
902
903 /* Function called by note_uses to replace used subexpressions. */
904 struct validate_replace_src_data
905 {
906 rtx from; /* Old RTX */
907 rtx to; /* New RTX */
908 rtx_insn *insn; /* Insn in which substitution is occurring. */
909 };
910
911 static void
912 validate_replace_src_1 (rtx *x, void *data)
913 {
914 struct validate_replace_src_data *d
915 = (struct validate_replace_src_data *) data;
916
917 validate_replace_rtx_1 (x, d->from, d->to, d->insn, true);
918 }
919
920 /* Try replacing every occurrence of FROM in INSN with TO, avoiding
921 SET_DESTs. */
922
923 void
924 validate_replace_src_group (rtx from, rtx to, rtx_insn *insn)
925 {
926 struct validate_replace_src_data d;
927
928 d.from = from;
929 d.to = to;
930 d.insn = insn;
931 note_uses (&PATTERN (insn), validate_replace_src_1, &d);
932 }
933
934 /* Try simplify INSN.
935 Invoke simplify_rtx () on every SET_SRC and SET_DEST inside the INSN's
936 pattern and return true if something was simplified. */
937
938 bool
939 validate_simplify_insn (rtx_insn *insn)
940 {
941 int i;
942 rtx pat = NULL;
943 rtx newpat = NULL;
944
945 pat = PATTERN (insn);
946
947 if (GET_CODE (pat) == SET)
948 {
949 newpat = simplify_rtx (SET_SRC (pat));
950 if (newpat && !rtx_equal_p (SET_SRC (pat), newpat))
951 validate_change (insn, &SET_SRC (pat), newpat, 1);
952 newpat = simplify_rtx (SET_DEST (pat));
953 if (newpat && !rtx_equal_p (SET_DEST (pat), newpat))
954 validate_change (insn, &SET_DEST (pat), newpat, 1);
955 }
956 else if (GET_CODE (pat) == PARALLEL)
957 for (i = 0; i < XVECLEN (pat, 0); i++)
958 {
959 rtx s = XVECEXP (pat, 0, i);
960
961 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
962 {
963 newpat = simplify_rtx (SET_SRC (s));
964 if (newpat && !rtx_equal_p (SET_SRC (s), newpat))
965 validate_change (insn, &SET_SRC (s), newpat, 1);
966 newpat = simplify_rtx (SET_DEST (s));
967 if (newpat && !rtx_equal_p (SET_DEST (s), newpat))
968 validate_change (insn, &SET_DEST (s), newpat, 1);
969 }
970 }
971 return ((num_changes_pending () > 0) && (apply_change_group () > 0));
972 }
973 \f
974 /* Return 1 if the insn using CC0 set by INSN does not contain
975 any ordered tests applied to the condition codes.
976 EQ and NE tests do not count. */
977
978 int
979 next_insn_tests_no_inequality (rtx_insn *insn)
980 {
981 rtx_insn *next = next_cc0_user (insn);
982
983 /* If there is no next insn, we have to take the conservative choice. */
984 if (next == 0)
985 return 0;
986
987 return (INSN_P (next)
988 && ! inequality_comparisons_p (PATTERN (next)));
989 }
990 \f
991 /* Return 1 if OP is a valid general operand for machine mode MODE.
992 This is either a register reference, a memory reference,
993 or a constant. In the case of a memory reference, the address
994 is checked for general validity for the target machine.
995
996 Register and memory references must have mode MODE in order to be valid,
997 but some constants have no machine mode and are valid for any mode.
998
999 If MODE is VOIDmode, OP is checked for validity for whatever mode
1000 it has.
1001
1002 The main use of this function is as a predicate in match_operand
1003 expressions in the machine description. */
1004
1005 int
1006 general_operand (rtx op, machine_mode mode)
1007 {
1008 enum rtx_code code = GET_CODE (op);
1009
1010 if (mode == VOIDmode)
1011 mode = GET_MODE (op);
1012
1013 /* Don't accept CONST_INT or anything similar
1014 if the caller wants something floating. */
1015 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1016 && GET_MODE_CLASS (mode) != MODE_INT
1017 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1018 return 0;
1019
1020 if (CONST_INT_P (op)
1021 && mode != VOIDmode
1022 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
1023 return 0;
1024
1025 if (CONSTANT_P (op))
1026 return ((GET_MODE (op) == VOIDmode || GET_MODE (op) == mode
1027 || mode == VOIDmode)
1028 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1029 && targetm.legitimate_constant_p (mode == VOIDmode
1030 ? GET_MODE (op)
1031 : mode, op));
1032
1033 /* Except for certain constants with VOIDmode, already checked for,
1034 OP's mode must match MODE if MODE specifies a mode. */
1035
1036 if (GET_MODE (op) != mode)
1037 return 0;
1038
1039 if (code == SUBREG)
1040 {
1041 rtx sub = SUBREG_REG (op);
1042
1043 #ifdef INSN_SCHEDULING
1044 /* On machines that have insn scheduling, we want all memory
1045 reference to be explicit, so outlaw paradoxical SUBREGs.
1046 However, we must allow them after reload so that they can
1047 get cleaned up by cleanup_subreg_operands. */
1048 if (!reload_completed && MEM_P (sub)
1049 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (sub)))
1050 return 0;
1051 #endif
1052 /* Avoid memories with nonzero SUBREG_BYTE, as offsetting the memory
1053 may result in incorrect reference. We should simplify all valid
1054 subregs of MEM anyway. But allow this after reload because we
1055 might be called from cleanup_subreg_operands.
1056
1057 ??? This is a kludge. */
1058 if (!reload_completed && SUBREG_BYTE (op) != 0
1059 && MEM_P (sub))
1060 return 0;
1061
1062 #ifdef CANNOT_CHANGE_MODE_CLASS
1063 if (REG_P (sub)
1064 && REGNO (sub) < FIRST_PSEUDO_REGISTER
1065 && REG_CANNOT_CHANGE_MODE_P (REGNO (sub), GET_MODE (sub), mode)
1066 && GET_MODE_CLASS (GET_MODE (sub)) != MODE_COMPLEX_INT
1067 && GET_MODE_CLASS (GET_MODE (sub)) != MODE_COMPLEX_FLOAT
1068 /* LRA can generate some invalid SUBREGS just for matched
1069 operand reload presentation. LRA needs to treat them as
1070 valid. */
1071 && ! LRA_SUBREG_P (op))
1072 return 0;
1073 #endif
1074
1075 /* FLOAT_MODE subregs can't be paradoxical. Combine will occasionally
1076 create such rtl, and we must reject it. */
1077 if (SCALAR_FLOAT_MODE_P (GET_MODE (op))
1078 /* LRA can use subreg to store a floating point value in an
1079 integer mode. Although the floating point and the
1080 integer modes need the same number of hard registers, the
1081 size of floating point mode can be less than the integer
1082 mode. */
1083 && ! lra_in_progress
1084 && GET_MODE_SIZE (GET_MODE (op)) > GET_MODE_SIZE (GET_MODE (sub)))
1085 return 0;
1086
1087 op = sub;
1088 code = GET_CODE (op);
1089 }
1090
1091 if (code == REG)
1092 return (REGNO (op) >= FIRST_PSEUDO_REGISTER
1093 || in_hard_reg_set_p (operand_reg_set, GET_MODE (op), REGNO (op)));
1094
1095 if (code == MEM)
1096 {
1097 rtx y = XEXP (op, 0);
1098
1099 if (! volatile_ok && MEM_VOLATILE_P (op))
1100 return 0;
1101
1102 /* Use the mem's mode, since it will be reloaded thus. LRA can
1103 generate move insn with invalid addresses which is made valid
1104 and efficiently calculated by LRA through further numerous
1105 transformations. */
1106 if (lra_in_progress
1107 || memory_address_addr_space_p (GET_MODE (op), y, MEM_ADDR_SPACE (op)))
1108 return 1;
1109 }
1110
1111 return 0;
1112 }
1113 \f
1114 /* Return 1 if OP is a valid memory address for a memory reference
1115 of mode MODE.
1116
1117 The main use of this function is as a predicate in match_operand
1118 expressions in the machine description. */
1119
1120 int
1121 address_operand (rtx op, machine_mode mode)
1122 {
1123 return memory_address_p (mode, op);
1124 }
1125
1126 /* Return 1 if OP is a register reference of mode MODE.
1127 If MODE is VOIDmode, accept a register in any mode.
1128
1129 The main use of this function is as a predicate in match_operand
1130 expressions in the machine description. */
1131
1132 int
1133 register_operand (rtx op, machine_mode mode)
1134 {
1135 if (GET_CODE (op) == SUBREG)
1136 {
1137 rtx sub = SUBREG_REG (op);
1138
1139 /* Before reload, we can allow (SUBREG (MEM...)) as a register operand
1140 because it is guaranteed to be reloaded into one.
1141 Just make sure the MEM is valid in itself.
1142 (Ideally, (SUBREG (MEM)...) should not exist after reload,
1143 but currently it does result from (SUBREG (REG)...) where the
1144 reg went on the stack.) */
1145 if (!REG_P (sub) && (reload_completed || !MEM_P (sub)))
1146 return 0;
1147 }
1148 else if (!REG_P (op))
1149 return 0;
1150 return general_operand (op, mode);
1151 }
1152
1153 /* Return 1 for a register in Pmode; ignore the tested mode. */
1154
1155 int
1156 pmode_register_operand (rtx op, machine_mode mode ATTRIBUTE_UNUSED)
1157 {
1158 return register_operand (op, Pmode);
1159 }
1160
1161 /* Return 1 if OP should match a MATCH_SCRATCH, i.e., if it is a SCRATCH
1162 or a hard register. */
1163
1164 int
1165 scratch_operand (rtx op, machine_mode mode)
1166 {
1167 if (GET_MODE (op) != mode && mode != VOIDmode)
1168 return 0;
1169
1170 return (GET_CODE (op) == SCRATCH
1171 || (REG_P (op)
1172 && (lra_in_progress
1173 || (REGNO (op) < FIRST_PSEUDO_REGISTER
1174 && REGNO_REG_CLASS (REGNO (op)) != NO_REGS))));
1175 }
1176
1177 /* Return 1 if OP is a valid immediate operand for mode MODE.
1178
1179 The main use of this function is as a predicate in match_operand
1180 expressions in the machine description. */
1181
1182 int
1183 immediate_operand (rtx op, machine_mode mode)
1184 {
1185 /* Don't accept CONST_INT or anything similar
1186 if the caller wants something floating. */
1187 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1188 && GET_MODE_CLASS (mode) != MODE_INT
1189 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1190 return 0;
1191
1192 if (CONST_INT_P (op)
1193 && mode != VOIDmode
1194 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
1195 return 0;
1196
1197 return (CONSTANT_P (op)
1198 && (GET_MODE (op) == mode || mode == VOIDmode
1199 || GET_MODE (op) == VOIDmode)
1200 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1201 && targetm.legitimate_constant_p (mode == VOIDmode
1202 ? GET_MODE (op)
1203 : mode, op));
1204 }
1205
1206 /* Returns 1 if OP is an operand that is a CONST_INT of mode MODE. */
1207
1208 int
1209 const_int_operand (rtx op, machine_mode mode)
1210 {
1211 if (!CONST_INT_P (op))
1212 return 0;
1213
1214 if (mode != VOIDmode
1215 && trunc_int_for_mode (INTVAL (op), mode) != INTVAL (op))
1216 return 0;
1217
1218 return 1;
1219 }
1220
1221 #if TARGET_SUPPORTS_WIDE_INT
1222 /* Returns 1 if OP is an operand that is a CONST_INT or CONST_WIDE_INT
1223 of mode MODE. */
1224 int
1225 const_scalar_int_operand (rtx op, machine_mode mode)
1226 {
1227 if (!CONST_SCALAR_INT_P (op))
1228 return 0;
1229
1230 if (CONST_INT_P (op))
1231 return const_int_operand (op, mode);
1232
1233 if (mode != VOIDmode)
1234 {
1235 int prec = GET_MODE_PRECISION (mode);
1236 int bitsize = GET_MODE_BITSIZE (mode);
1237
1238 if (CONST_WIDE_INT_NUNITS (op) * HOST_BITS_PER_WIDE_INT > bitsize)
1239 return 0;
1240
1241 if (prec == bitsize)
1242 return 1;
1243 else
1244 {
1245 /* Multiword partial int. */
1246 HOST_WIDE_INT x
1247 = CONST_WIDE_INT_ELT (op, CONST_WIDE_INT_NUNITS (op) - 1);
1248 return (sext_hwi (x, prec & (HOST_BITS_PER_WIDE_INT - 1)) == x);
1249 }
1250 }
1251 return 1;
1252 }
1253
1254 /* Returns 1 if OP is an operand that is a constant integer or constant
1255 floating-point number of MODE. */
1256
1257 int
1258 const_double_operand (rtx op, machine_mode mode)
1259 {
1260 return (GET_CODE (op) == CONST_DOUBLE)
1261 && (GET_MODE (op) == mode || mode == VOIDmode);
1262 }
1263 #else
1264 /* Returns 1 if OP is an operand that is a constant integer or constant
1265 floating-point number of MODE. */
1266
1267 int
1268 const_double_operand (rtx op, machine_mode mode)
1269 {
1270 /* Don't accept CONST_INT or anything similar
1271 if the caller wants something floating. */
1272 if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1273 && GET_MODE_CLASS (mode) != MODE_INT
1274 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1275 return 0;
1276
1277 return ((CONST_DOUBLE_P (op) || CONST_INT_P (op))
1278 && (mode == VOIDmode || GET_MODE (op) == mode
1279 || GET_MODE (op) == VOIDmode));
1280 }
1281 #endif
1282 /* Return 1 if OP is a general operand that is not an immediate
1283 operand of mode MODE. */
1284
1285 int
1286 nonimmediate_operand (rtx op, machine_mode mode)
1287 {
1288 return (general_operand (op, mode) && ! CONSTANT_P (op));
1289 }
1290
1291 /* Return 1 if OP is a register reference or immediate value of mode MODE. */
1292
1293 int
1294 nonmemory_operand (rtx op, machine_mode mode)
1295 {
1296 if (CONSTANT_P (op))
1297 return immediate_operand (op, mode);
1298 return register_operand (op, mode);
1299 }
1300
1301 /* Return 1 if OP is a valid operand that stands for pushing a
1302 value of mode MODE onto the stack.
1303
1304 The main use of this function is as a predicate in match_operand
1305 expressions in the machine description. */
1306
1307 int
1308 push_operand (rtx op, machine_mode mode)
1309 {
1310 unsigned int rounded_size = GET_MODE_SIZE (mode);
1311
1312 #ifdef PUSH_ROUNDING
1313 rounded_size = PUSH_ROUNDING (rounded_size);
1314 #endif
1315
1316 if (!MEM_P (op))
1317 return 0;
1318
1319 if (mode != VOIDmode && GET_MODE (op) != mode)
1320 return 0;
1321
1322 op = XEXP (op, 0);
1323
1324 if (rounded_size == GET_MODE_SIZE (mode))
1325 {
1326 if (GET_CODE (op) != STACK_PUSH_CODE)
1327 return 0;
1328 }
1329 else
1330 {
1331 if (GET_CODE (op) != PRE_MODIFY
1332 || GET_CODE (XEXP (op, 1)) != PLUS
1333 || XEXP (XEXP (op, 1), 0) != XEXP (op, 0)
1334 || !CONST_INT_P (XEXP (XEXP (op, 1), 1))
1335 #if STACK_GROWS_DOWNWARD
1336 || INTVAL (XEXP (XEXP (op, 1), 1)) != - (int) rounded_size
1337 #else
1338 || INTVAL (XEXP (XEXP (op, 1), 1)) != (int) rounded_size
1339 #endif
1340 )
1341 return 0;
1342 }
1343
1344 return XEXP (op, 0) == stack_pointer_rtx;
1345 }
1346
1347 /* Return 1 if OP is a valid operand that stands for popping a
1348 value of mode MODE off the stack.
1349
1350 The main use of this function is as a predicate in match_operand
1351 expressions in the machine description. */
1352
1353 int
1354 pop_operand (rtx op, machine_mode mode)
1355 {
1356 if (!MEM_P (op))
1357 return 0;
1358
1359 if (mode != VOIDmode && GET_MODE (op) != mode)
1360 return 0;
1361
1362 op = XEXP (op, 0);
1363
1364 if (GET_CODE (op) != STACK_POP_CODE)
1365 return 0;
1366
1367 return XEXP (op, 0) == stack_pointer_rtx;
1368 }
1369
1370 /* Return 1 if ADDR is a valid memory address
1371 for mode MODE in address space AS. */
1372
1373 int
1374 memory_address_addr_space_p (machine_mode mode ATTRIBUTE_UNUSED,
1375 rtx addr, addr_space_t as)
1376 {
1377 #ifdef GO_IF_LEGITIMATE_ADDRESS
1378 gcc_assert (ADDR_SPACE_GENERIC_P (as));
1379 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
1380 return 0;
1381
1382 win:
1383 return 1;
1384 #else
1385 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
1386 #endif
1387 }
1388
1389 /* Return 1 if OP is a valid memory reference with mode MODE,
1390 including a valid address.
1391
1392 The main use of this function is as a predicate in match_operand
1393 expressions in the machine description. */
1394
1395 int
1396 memory_operand (rtx op, machine_mode mode)
1397 {
1398 rtx inner;
1399
1400 if (! reload_completed)
1401 /* Note that no SUBREG is a memory operand before end of reload pass,
1402 because (SUBREG (MEM...)) forces reloading into a register. */
1403 return MEM_P (op) && general_operand (op, mode);
1404
1405 if (mode != VOIDmode && GET_MODE (op) != mode)
1406 return 0;
1407
1408 inner = op;
1409 if (GET_CODE (inner) == SUBREG)
1410 inner = SUBREG_REG (inner);
1411
1412 return (MEM_P (inner) && general_operand (op, mode));
1413 }
1414
1415 /* Return 1 if OP is a valid indirect memory reference with mode MODE;
1416 that is, a memory reference whose address is a general_operand. */
1417
1418 int
1419 indirect_operand (rtx op, machine_mode mode)
1420 {
1421 /* Before reload, a SUBREG isn't in memory (see memory_operand, above). */
1422 if (! reload_completed
1423 && GET_CODE (op) == SUBREG && MEM_P (SUBREG_REG (op)))
1424 {
1425 int offset = SUBREG_BYTE (op);
1426 rtx inner = SUBREG_REG (op);
1427
1428 if (mode != VOIDmode && GET_MODE (op) != mode)
1429 return 0;
1430
1431 /* The only way that we can have a general_operand as the resulting
1432 address is if OFFSET is zero and the address already is an operand
1433 or if the address is (plus Y (const_int -OFFSET)) and Y is an
1434 operand. */
1435
1436 return ((offset == 0 && general_operand (XEXP (inner, 0), Pmode))
1437 || (GET_CODE (XEXP (inner, 0)) == PLUS
1438 && CONST_INT_P (XEXP (XEXP (inner, 0), 1))
1439 && INTVAL (XEXP (XEXP (inner, 0), 1)) == -offset
1440 && general_operand (XEXP (XEXP (inner, 0), 0), Pmode)));
1441 }
1442
1443 return (MEM_P (op)
1444 && memory_operand (op, mode)
1445 && general_operand (XEXP (op, 0), Pmode));
1446 }
1447
1448 /* Return 1 if this is an ordered comparison operator (not including
1449 ORDERED and UNORDERED). */
1450
1451 int
1452 ordered_comparison_operator (rtx op, machine_mode mode)
1453 {
1454 if (mode != VOIDmode && GET_MODE (op) != mode)
1455 return false;
1456 switch (GET_CODE (op))
1457 {
1458 case EQ:
1459 case NE:
1460 case LT:
1461 case LTU:
1462 case LE:
1463 case LEU:
1464 case GT:
1465 case GTU:
1466 case GE:
1467 case GEU:
1468 return true;
1469 default:
1470 return false;
1471 }
1472 }
1473
1474 /* Return 1 if this is a comparison operator. This allows the use of
1475 MATCH_OPERATOR to recognize all the branch insns. */
1476
1477 int
1478 comparison_operator (rtx op, machine_mode mode)
1479 {
1480 return ((mode == VOIDmode || GET_MODE (op) == mode)
1481 && COMPARISON_P (op));
1482 }
1483 \f
1484 /* If BODY is an insn body that uses ASM_OPERANDS, return it. */
1485
1486 rtx
1487 extract_asm_operands (rtx body)
1488 {
1489 rtx tmp;
1490 switch (GET_CODE (body))
1491 {
1492 case ASM_OPERANDS:
1493 return body;
1494
1495 case SET:
1496 /* Single output operand: BODY is (set OUTPUT (asm_operands ...)). */
1497 tmp = SET_SRC (body);
1498 if (GET_CODE (tmp) == ASM_OPERANDS)
1499 return tmp;
1500 break;
1501
1502 case PARALLEL:
1503 tmp = XVECEXP (body, 0, 0);
1504 if (GET_CODE (tmp) == ASM_OPERANDS)
1505 return tmp;
1506 if (GET_CODE (tmp) == SET)
1507 {
1508 tmp = SET_SRC (tmp);
1509 if (GET_CODE (tmp) == ASM_OPERANDS)
1510 return tmp;
1511 }
1512 break;
1513
1514 default:
1515 break;
1516 }
1517 return NULL;
1518 }
1519
1520 /* If BODY is an insn body that uses ASM_OPERANDS,
1521 return the number of operands (both input and output) in the insn.
1522 Otherwise return -1. */
1523
1524 int
1525 asm_noperands (const_rtx body)
1526 {
1527 rtx asm_op = extract_asm_operands (CONST_CAST_RTX (body));
1528 int n_sets = 0;
1529
1530 if (asm_op == NULL)
1531 return -1;
1532
1533 if (GET_CODE (body) == SET)
1534 n_sets = 1;
1535 else if (GET_CODE (body) == PARALLEL)
1536 {
1537 int i;
1538 if (GET_CODE (XVECEXP (body, 0, 0)) == SET)
1539 {
1540 /* Multiple output operands, or 1 output plus some clobbers:
1541 body is
1542 [(set OUTPUT (asm_operands ...))... (clobber (reg ...))...]. */
1543 /* Count backwards through CLOBBERs to determine number of SETs. */
1544 for (i = XVECLEN (body, 0); i > 0; i--)
1545 {
1546 if (GET_CODE (XVECEXP (body, 0, i - 1)) == SET)
1547 break;
1548 if (GET_CODE (XVECEXP (body, 0, i - 1)) != CLOBBER)
1549 return -1;
1550 }
1551
1552 /* N_SETS is now number of output operands. */
1553 n_sets = i;
1554
1555 /* Verify that all the SETs we have
1556 came from a single original asm_operands insn
1557 (so that invalid combinations are blocked). */
1558 for (i = 0; i < n_sets; i++)
1559 {
1560 rtx elt = XVECEXP (body, 0, i);
1561 if (GET_CODE (elt) != SET)
1562 return -1;
1563 if (GET_CODE (SET_SRC (elt)) != ASM_OPERANDS)
1564 return -1;
1565 /* If these ASM_OPERANDS rtx's came from different original insns
1566 then they aren't allowed together. */
1567 if (ASM_OPERANDS_INPUT_VEC (SET_SRC (elt))
1568 != ASM_OPERANDS_INPUT_VEC (asm_op))
1569 return -1;
1570 }
1571 }
1572 else
1573 {
1574 /* 0 outputs, but some clobbers:
1575 body is [(asm_operands ...) (clobber (reg ...))...]. */
1576 /* Make sure all the other parallel things really are clobbers. */
1577 for (i = XVECLEN (body, 0) - 1; i > 0; i--)
1578 if (GET_CODE (XVECEXP (body, 0, i)) != CLOBBER)
1579 return -1;
1580 }
1581 }
1582
1583 return (ASM_OPERANDS_INPUT_LENGTH (asm_op)
1584 + ASM_OPERANDS_LABEL_LENGTH (asm_op) + n_sets);
1585 }
1586
1587 /* Assuming BODY is an insn body that uses ASM_OPERANDS,
1588 copy its operands (both input and output) into the vector OPERANDS,
1589 the locations of the operands within the insn into the vector OPERAND_LOCS,
1590 and the constraints for the operands into CONSTRAINTS.
1591 Write the modes of the operands into MODES.
1592 Return the assembler-template.
1593
1594 If MODES, OPERAND_LOCS, CONSTRAINTS or OPERANDS is 0,
1595 we don't store that info. */
1596
1597 const char *
1598 decode_asm_operands (rtx body, rtx *operands, rtx **operand_locs,
1599 const char **constraints, machine_mode *modes,
1600 location_t *loc)
1601 {
1602 int nbase = 0, n, i;
1603 rtx asmop;
1604
1605 switch (GET_CODE (body))
1606 {
1607 case ASM_OPERANDS:
1608 /* Zero output asm: BODY is (asm_operands ...). */
1609 asmop = body;
1610 break;
1611
1612 case SET:
1613 /* Single output asm: BODY is (set OUTPUT (asm_operands ...)). */
1614 asmop = SET_SRC (body);
1615
1616 /* The output is in the SET.
1617 Its constraint is in the ASM_OPERANDS itself. */
1618 if (operands)
1619 operands[0] = SET_DEST (body);
1620 if (operand_locs)
1621 operand_locs[0] = &SET_DEST (body);
1622 if (constraints)
1623 constraints[0] = ASM_OPERANDS_OUTPUT_CONSTRAINT (asmop);
1624 if (modes)
1625 modes[0] = GET_MODE (SET_DEST (body));
1626 nbase = 1;
1627 break;
1628
1629 case PARALLEL:
1630 {
1631 int nparallel = XVECLEN (body, 0); /* Includes CLOBBERs. */
1632
1633 asmop = XVECEXP (body, 0, 0);
1634 if (GET_CODE (asmop) == SET)
1635 {
1636 asmop = SET_SRC (asmop);
1637
1638 /* At least one output, plus some CLOBBERs. The outputs are in
1639 the SETs. Their constraints are in the ASM_OPERANDS itself. */
1640 for (i = 0; i < nparallel; i++)
1641 {
1642 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
1643 break; /* Past last SET */
1644 if (operands)
1645 operands[i] = SET_DEST (XVECEXP (body, 0, i));
1646 if (operand_locs)
1647 operand_locs[i] = &SET_DEST (XVECEXP (body, 0, i));
1648 if (constraints)
1649 constraints[i] = XSTR (SET_SRC (XVECEXP (body, 0, i)), 1);
1650 if (modes)
1651 modes[i] = GET_MODE (SET_DEST (XVECEXP (body, 0, i)));
1652 }
1653 nbase = i;
1654 }
1655 break;
1656 }
1657
1658 default:
1659 gcc_unreachable ();
1660 }
1661
1662 n = ASM_OPERANDS_INPUT_LENGTH (asmop);
1663 for (i = 0; i < n; i++)
1664 {
1665 if (operand_locs)
1666 operand_locs[nbase + i] = &ASM_OPERANDS_INPUT (asmop, i);
1667 if (operands)
1668 operands[nbase + i] = ASM_OPERANDS_INPUT (asmop, i);
1669 if (constraints)
1670 constraints[nbase + i] = ASM_OPERANDS_INPUT_CONSTRAINT (asmop, i);
1671 if (modes)
1672 modes[nbase + i] = ASM_OPERANDS_INPUT_MODE (asmop, i);
1673 }
1674 nbase += n;
1675
1676 n = ASM_OPERANDS_LABEL_LENGTH (asmop);
1677 for (i = 0; i < n; i++)
1678 {
1679 if (operand_locs)
1680 operand_locs[nbase + i] = &ASM_OPERANDS_LABEL (asmop, i);
1681 if (operands)
1682 operands[nbase + i] = ASM_OPERANDS_LABEL (asmop, i);
1683 if (constraints)
1684 constraints[nbase + i] = "";
1685 if (modes)
1686 modes[nbase + i] = Pmode;
1687 }
1688
1689 if (loc)
1690 *loc = ASM_OPERANDS_SOURCE_LOCATION (asmop);
1691
1692 return ASM_OPERANDS_TEMPLATE (asmop);
1693 }
1694
1695 /* Parse inline assembly string STRING and determine which operands are
1696 referenced by % markers. For the first NOPERANDS operands, set USED[I]
1697 to true if operand I is referenced.
1698
1699 This is intended to distinguish barrier-like asms such as:
1700
1701 asm ("" : "=m" (...));
1702
1703 from real references such as:
1704
1705 asm ("sw\t$0, %0" : "=m" (...)); */
1706
1707 void
1708 get_referenced_operands (const char *string, bool *used,
1709 unsigned int noperands)
1710 {
1711 memset (used, 0, sizeof (bool) * noperands);
1712 const char *p = string;
1713 while (*p)
1714 switch (*p)
1715 {
1716 case '%':
1717 p += 1;
1718 /* A letter followed by a digit indicates an operand number. */
1719 if (ISALPHA (p[0]) && ISDIGIT (p[1]))
1720 p += 1;
1721 if (ISDIGIT (*p))
1722 {
1723 char *endptr;
1724 unsigned long opnum = strtoul (p, &endptr, 10);
1725 if (endptr != p && opnum < noperands)
1726 used[opnum] = true;
1727 p = endptr;
1728 }
1729 else
1730 p += 1;
1731 break;
1732
1733 default:
1734 p++;
1735 break;
1736 }
1737 }
1738
1739 /* Check if an asm_operand matches its constraints.
1740 Return > 0 if ok, = 0 if bad, < 0 if inconclusive. */
1741
1742 int
1743 asm_operand_ok (rtx op, const char *constraint, const char **constraints)
1744 {
1745 int result = 0;
1746 #ifdef AUTO_INC_DEC
1747 bool incdec_ok = false;
1748 #endif
1749
1750 /* Use constrain_operands after reload. */
1751 gcc_assert (!reload_completed);
1752
1753 /* Empty constraint string is the same as "X,...,X", i.e. X for as
1754 many alternatives as required to match the other operands. */
1755 if (*constraint == '\0')
1756 result = 1;
1757
1758 while (*constraint)
1759 {
1760 enum constraint_num cn;
1761 char c = *constraint;
1762 int len;
1763 switch (c)
1764 {
1765 case ',':
1766 constraint++;
1767 continue;
1768
1769 case '0': case '1': case '2': case '3': case '4':
1770 case '5': case '6': case '7': case '8': case '9':
1771 /* If caller provided constraints pointer, look up
1772 the matching constraint. Otherwise, our caller should have
1773 given us the proper matching constraint, but we can't
1774 actually fail the check if they didn't. Indicate that
1775 results are inconclusive. */
1776 if (constraints)
1777 {
1778 char *end;
1779 unsigned long match;
1780
1781 match = strtoul (constraint, &end, 10);
1782 if (!result)
1783 result = asm_operand_ok (op, constraints[match], NULL);
1784 constraint = (const char *) end;
1785 }
1786 else
1787 {
1788 do
1789 constraint++;
1790 while (ISDIGIT (*constraint));
1791 if (! result)
1792 result = -1;
1793 }
1794 continue;
1795
1796 /* The rest of the compiler assumes that reloading the address
1797 of a MEM into a register will make it fit an 'o' constraint.
1798 That is, if it sees a MEM operand for an 'o' constraint,
1799 it assumes that (mem (base-reg)) will fit.
1800
1801 That assumption fails on targets that don't have offsettable
1802 addresses at all. We therefore need to treat 'o' asm
1803 constraints as a special case and only accept operands that
1804 are already offsettable, thus proving that at least one
1805 offsettable address exists. */
1806 case 'o': /* offsettable */
1807 if (offsettable_nonstrict_memref_p (op))
1808 result = 1;
1809 break;
1810
1811 case 'g':
1812 if (general_operand (op, VOIDmode))
1813 result = 1;
1814 break;
1815
1816 #ifdef AUTO_INC_DEC
1817 case '<':
1818 case '>':
1819 /* ??? Before auto-inc-dec, auto inc/dec insns are not supposed
1820 to exist, excepting those that expand_call created. Further,
1821 on some machines which do not have generalized auto inc/dec,
1822 an inc/dec is not a memory_operand.
1823
1824 Match any memory and hope things are resolved after reload. */
1825 incdec_ok = true;
1826 #endif
1827 default:
1828 cn = lookup_constraint (constraint);
1829 switch (get_constraint_type (cn))
1830 {
1831 case CT_REGISTER:
1832 if (!result
1833 && reg_class_for_constraint (cn) != NO_REGS
1834 && GET_MODE (op) != BLKmode
1835 && register_operand (op, VOIDmode))
1836 result = 1;
1837 break;
1838
1839 case CT_CONST_INT:
1840 if (!result
1841 && CONST_INT_P (op)
1842 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
1843 result = 1;
1844 break;
1845
1846 case CT_MEMORY:
1847 /* Every memory operand can be reloaded to fit. */
1848 result = result || memory_operand (op, VOIDmode);
1849 break;
1850
1851 case CT_ADDRESS:
1852 /* Every address operand can be reloaded to fit. */
1853 result = result || address_operand (op, VOIDmode);
1854 break;
1855
1856 case CT_FIXED_FORM:
1857 result = result || constraint_satisfied_p (op, cn);
1858 break;
1859 }
1860 break;
1861 }
1862 len = CONSTRAINT_LEN (c, constraint);
1863 do
1864 constraint++;
1865 while (--len && *constraint);
1866 if (len)
1867 return 0;
1868 }
1869
1870 #ifdef AUTO_INC_DEC
1871 /* For operands without < or > constraints reject side-effects. */
1872 if (!incdec_ok && result && MEM_P (op))
1873 switch (GET_CODE (XEXP (op, 0)))
1874 {
1875 case PRE_INC:
1876 case POST_INC:
1877 case PRE_DEC:
1878 case POST_DEC:
1879 case PRE_MODIFY:
1880 case POST_MODIFY:
1881 return 0;
1882 default:
1883 break;
1884 }
1885 #endif
1886
1887 return result;
1888 }
1889 \f
1890 /* Given an rtx *P, if it is a sum containing an integer constant term,
1891 return the location (type rtx *) of the pointer to that constant term.
1892 Otherwise, return a null pointer. */
1893
1894 rtx *
1895 find_constant_term_loc (rtx *p)
1896 {
1897 rtx *tem;
1898 enum rtx_code code = GET_CODE (*p);
1899
1900 /* If *P IS such a constant term, P is its location. */
1901
1902 if (code == CONST_INT || code == SYMBOL_REF || code == LABEL_REF
1903 || code == CONST)
1904 return p;
1905
1906 /* Otherwise, if not a sum, it has no constant term. */
1907
1908 if (GET_CODE (*p) != PLUS)
1909 return 0;
1910
1911 /* If one of the summands is constant, return its location. */
1912
1913 if (XEXP (*p, 0) && CONSTANT_P (XEXP (*p, 0))
1914 && XEXP (*p, 1) && CONSTANT_P (XEXP (*p, 1)))
1915 return p;
1916
1917 /* Otherwise, check each summand for containing a constant term. */
1918
1919 if (XEXP (*p, 0) != 0)
1920 {
1921 tem = find_constant_term_loc (&XEXP (*p, 0));
1922 if (tem != 0)
1923 return tem;
1924 }
1925
1926 if (XEXP (*p, 1) != 0)
1927 {
1928 tem = find_constant_term_loc (&XEXP (*p, 1));
1929 if (tem != 0)
1930 return tem;
1931 }
1932
1933 return 0;
1934 }
1935 \f
1936 /* Return 1 if OP is a memory reference
1937 whose address contains no side effects
1938 and remains valid after the addition
1939 of a positive integer less than the
1940 size of the object being referenced.
1941
1942 We assume that the original address is valid and do not check it.
1943
1944 This uses strict_memory_address_p as a subroutine, so
1945 don't use it before reload. */
1946
1947 int
1948 offsettable_memref_p (rtx op)
1949 {
1950 return ((MEM_P (op))
1951 && offsettable_address_addr_space_p (1, GET_MODE (op), XEXP (op, 0),
1952 MEM_ADDR_SPACE (op)));
1953 }
1954
1955 /* Similar, but don't require a strictly valid mem ref:
1956 consider pseudo-regs valid as index or base regs. */
1957
1958 int
1959 offsettable_nonstrict_memref_p (rtx op)
1960 {
1961 return ((MEM_P (op))
1962 && offsettable_address_addr_space_p (0, GET_MODE (op), XEXP (op, 0),
1963 MEM_ADDR_SPACE (op)));
1964 }
1965
1966 /* Return 1 if Y is a memory address which contains no side effects
1967 and would remain valid for address space AS after the addition of
1968 a positive integer less than the size of that mode.
1969
1970 We assume that the original address is valid and do not check it.
1971 We do check that it is valid for narrower modes.
1972
1973 If STRICTP is nonzero, we require a strictly valid address,
1974 for the sake of use in reload.c. */
1975
1976 int
1977 offsettable_address_addr_space_p (int strictp, machine_mode mode, rtx y,
1978 addr_space_t as)
1979 {
1980 enum rtx_code ycode = GET_CODE (y);
1981 rtx z;
1982 rtx y1 = y;
1983 rtx *y2;
1984 int (*addressp) (machine_mode, rtx, addr_space_t) =
1985 (strictp ? strict_memory_address_addr_space_p
1986 : memory_address_addr_space_p);
1987 unsigned int mode_sz = GET_MODE_SIZE (mode);
1988
1989 if (CONSTANT_ADDRESS_P (y))
1990 return 1;
1991
1992 /* Adjusting an offsettable address involves changing to a narrower mode.
1993 Make sure that's OK. */
1994
1995 if (mode_dependent_address_p (y, as))
1996 return 0;
1997
1998 machine_mode address_mode = GET_MODE (y);
1999 if (address_mode == VOIDmode)
2000 address_mode = targetm.addr_space.address_mode (as);
2001 #ifdef POINTERS_EXTEND_UNSIGNED
2002 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
2003 #endif
2004
2005 /* ??? How much offset does an offsettable BLKmode reference need?
2006 Clearly that depends on the situation in which it's being used.
2007 However, the current situation in which we test 0xffffffff is
2008 less than ideal. Caveat user. */
2009 if (mode_sz == 0)
2010 mode_sz = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
2011
2012 /* If the expression contains a constant term,
2013 see if it remains valid when max possible offset is added. */
2014
2015 if ((ycode == PLUS) && (y2 = find_constant_term_loc (&y1)))
2016 {
2017 int good;
2018
2019 y1 = *y2;
2020 *y2 = plus_constant (address_mode, *y2, mode_sz - 1);
2021 /* Use QImode because an odd displacement may be automatically invalid
2022 for any wider mode. But it should be valid for a single byte. */
2023 good = (*addressp) (QImode, y, as);
2024
2025 /* In any case, restore old contents of memory. */
2026 *y2 = y1;
2027 return good;
2028 }
2029
2030 if (GET_RTX_CLASS (ycode) == RTX_AUTOINC)
2031 return 0;
2032
2033 /* The offset added here is chosen as the maximum offset that
2034 any instruction could need to add when operating on something
2035 of the specified mode. We assume that if Y and Y+c are
2036 valid addresses then so is Y+d for all 0<d<c. adjust_address will
2037 go inside a LO_SUM here, so we do so as well. */
2038 if (GET_CODE (y) == LO_SUM
2039 && mode != BLKmode
2040 && mode_sz <= GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT)
2041 z = gen_rtx_LO_SUM (address_mode, XEXP (y, 0),
2042 plus_constant (address_mode, XEXP (y, 1),
2043 mode_sz - 1));
2044 #ifdef POINTERS_EXTEND_UNSIGNED
2045 /* Likewise for a ZERO_EXTEND from pointer_mode. */
2046 else if (POINTERS_EXTEND_UNSIGNED > 0
2047 && GET_CODE (y) == ZERO_EXTEND
2048 && GET_MODE (XEXP (y, 0)) == pointer_mode)
2049 z = gen_rtx_ZERO_EXTEND (address_mode,
2050 plus_constant (pointer_mode, XEXP (y, 0),
2051 mode_sz - 1));
2052 #endif
2053 else
2054 z = plus_constant (address_mode, y, mode_sz - 1);
2055
2056 /* Use QImode because an odd displacement may be automatically invalid
2057 for any wider mode. But it should be valid for a single byte. */
2058 return (*addressp) (QImode, z, as);
2059 }
2060
2061 /* Return 1 if ADDR is an address-expression whose effect depends
2062 on the mode of the memory reference it is used in.
2063
2064 ADDRSPACE is the address space associated with the address.
2065
2066 Autoincrement addressing is a typical example of mode-dependence
2067 because the amount of the increment depends on the mode. */
2068
2069 bool
2070 mode_dependent_address_p (rtx addr, addr_space_t addrspace)
2071 {
2072 /* Auto-increment addressing with anything other than post_modify
2073 or pre_modify always introduces a mode dependency. Catch such
2074 cases now instead of deferring to the target. */
2075 if (GET_CODE (addr) == PRE_INC
2076 || GET_CODE (addr) == POST_INC
2077 || GET_CODE (addr) == PRE_DEC
2078 || GET_CODE (addr) == POST_DEC)
2079 return true;
2080
2081 return targetm.mode_dependent_address_p (addr, addrspace);
2082 }
2083 \f
2084 /* Return true if boolean attribute ATTR is supported. */
2085
2086 static bool
2087 have_bool_attr (bool_attr attr)
2088 {
2089 switch (attr)
2090 {
2091 case BA_ENABLED:
2092 return HAVE_ATTR_enabled;
2093 case BA_PREFERRED_FOR_SIZE:
2094 return HAVE_ATTR_enabled || HAVE_ATTR_preferred_for_size;
2095 case BA_PREFERRED_FOR_SPEED:
2096 return HAVE_ATTR_enabled || HAVE_ATTR_preferred_for_speed;
2097 }
2098 gcc_unreachable ();
2099 }
2100
2101 /* Return the value of ATTR for instruction INSN. */
2102
2103 static bool
2104 get_bool_attr (rtx_insn *insn, bool_attr attr)
2105 {
2106 switch (attr)
2107 {
2108 case BA_ENABLED:
2109 return get_attr_enabled (insn);
2110 case BA_PREFERRED_FOR_SIZE:
2111 return get_attr_enabled (insn) && get_attr_preferred_for_size (insn);
2112 case BA_PREFERRED_FOR_SPEED:
2113 return get_attr_enabled (insn) && get_attr_preferred_for_speed (insn);
2114 }
2115 gcc_unreachable ();
2116 }
2117
2118 /* Like get_bool_attr_mask, but don't use the cache. */
2119
2120 static alternative_mask
2121 get_bool_attr_mask_uncached (rtx_insn *insn, bool_attr attr)
2122 {
2123 /* Temporarily install enough information for get_attr_<foo> to assume
2124 that the insn operands are already cached. As above, the attribute
2125 mustn't depend on the values of operands, so we don't provide their
2126 real values here. */
2127 rtx_insn *old_insn = recog_data.insn;
2128 int old_alternative = which_alternative;
2129
2130 recog_data.insn = insn;
2131 alternative_mask mask = ALL_ALTERNATIVES;
2132 int n_alternatives = insn_data[INSN_CODE (insn)].n_alternatives;
2133 for (int i = 0; i < n_alternatives; i++)
2134 {
2135 which_alternative = i;
2136 if (!get_bool_attr (insn, attr))
2137 mask &= ~ALTERNATIVE_BIT (i);
2138 }
2139
2140 recog_data.insn = old_insn;
2141 which_alternative = old_alternative;
2142 return mask;
2143 }
2144
2145 /* Return the mask of operand alternatives that are allowed for INSN
2146 by boolean attribute ATTR. This mask depends only on INSN and on
2147 the current target; it does not depend on things like the values of
2148 operands. */
2149
2150 static alternative_mask
2151 get_bool_attr_mask (rtx_insn *insn, bool_attr attr)
2152 {
2153 /* Quick exit for asms and for targets that don't use these attributes. */
2154 int code = INSN_CODE (insn);
2155 if (code < 0 || !have_bool_attr (attr))
2156 return ALL_ALTERNATIVES;
2157
2158 /* Calling get_attr_<foo> can be expensive, so cache the mask
2159 for speed. */
2160 if (!this_target_recog->x_bool_attr_masks[code][attr])
2161 this_target_recog->x_bool_attr_masks[code][attr]
2162 = get_bool_attr_mask_uncached (insn, attr);
2163 return this_target_recog->x_bool_attr_masks[code][attr];
2164 }
2165
2166 /* Return the set of alternatives of INSN that are allowed by the current
2167 target. */
2168
2169 alternative_mask
2170 get_enabled_alternatives (rtx_insn *insn)
2171 {
2172 return get_bool_attr_mask (insn, BA_ENABLED);
2173 }
2174
2175 /* Return the set of alternatives of INSN that are allowed by the current
2176 target and are preferred for the current size/speed optimization
2177 choice. */
2178
2179 alternative_mask
2180 get_preferred_alternatives (rtx_insn *insn)
2181 {
2182 if (optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn)))
2183 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SPEED);
2184 else
2185 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SIZE);
2186 }
2187
2188 /* Return the set of alternatives of INSN that are allowed by the current
2189 target and are preferred for the size/speed optimization choice
2190 associated with BB. Passing a separate BB is useful if INSN has not
2191 been emitted yet or if we are considering moving it to a different
2192 block. */
2193
2194 alternative_mask
2195 get_preferred_alternatives (rtx_insn *insn, basic_block bb)
2196 {
2197 if (optimize_bb_for_speed_p (bb))
2198 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SPEED);
2199 else
2200 return get_bool_attr_mask (insn, BA_PREFERRED_FOR_SIZE);
2201 }
2202
2203 /* Assert that the cached boolean attributes for INSN are still accurate.
2204 The backend is required to define these attributes in a way that only
2205 depends on the current target (rather than operands, compiler phase,
2206 etc.). */
2207
2208 bool
2209 check_bool_attrs (rtx_insn *insn)
2210 {
2211 int code = INSN_CODE (insn);
2212 if (code >= 0)
2213 for (int i = 0; i <= BA_LAST; ++i)
2214 {
2215 enum bool_attr attr = (enum bool_attr) i;
2216 if (this_target_recog->x_bool_attr_masks[code][attr])
2217 gcc_assert (this_target_recog->x_bool_attr_masks[code][attr]
2218 == get_bool_attr_mask_uncached (insn, attr));
2219 }
2220 return true;
2221 }
2222
2223 /* Like extract_insn, but save insn extracted and don't extract again, when
2224 called again for the same insn expecting that recog_data still contain the
2225 valid information. This is used primary by gen_attr infrastructure that
2226 often does extract insn again and again. */
2227 void
2228 extract_insn_cached (rtx_insn *insn)
2229 {
2230 if (recog_data.insn == insn && INSN_CODE (insn) >= 0)
2231 return;
2232 extract_insn (insn);
2233 recog_data.insn = insn;
2234 }
2235
2236 /* Do uncached extract_insn, constrain_operands and complain about failures.
2237 This should be used when extracting a pre-existing constrained instruction
2238 if the caller wants to know which alternative was chosen. */
2239 void
2240 extract_constrain_insn (rtx_insn *insn)
2241 {
2242 extract_insn (insn);
2243 if (!constrain_operands (reload_completed, get_enabled_alternatives (insn)))
2244 fatal_insn_not_found (insn);
2245 }
2246
2247 /* Do cached extract_insn, constrain_operands and complain about failures.
2248 Used by insn_attrtab. */
2249 void
2250 extract_constrain_insn_cached (rtx_insn *insn)
2251 {
2252 extract_insn_cached (insn);
2253 if (which_alternative == -1
2254 && !constrain_operands (reload_completed,
2255 get_enabled_alternatives (insn)))
2256 fatal_insn_not_found (insn);
2257 }
2258
2259 /* Do cached constrain_operands on INSN and complain about failures. */
2260 int
2261 constrain_operands_cached (rtx_insn *insn, int strict)
2262 {
2263 if (which_alternative == -1)
2264 return constrain_operands (strict, get_enabled_alternatives (insn));
2265 else
2266 return 1;
2267 }
2268 \f
2269 /* Analyze INSN and fill in recog_data. */
2270
2271 void
2272 extract_insn (rtx_insn *insn)
2273 {
2274 int i;
2275 int icode;
2276 int noperands;
2277 rtx body = PATTERN (insn);
2278
2279 recog_data.n_operands = 0;
2280 recog_data.n_alternatives = 0;
2281 recog_data.n_dups = 0;
2282 recog_data.is_asm = false;
2283
2284 switch (GET_CODE (body))
2285 {
2286 case USE:
2287 case CLOBBER:
2288 case ASM_INPUT:
2289 case ADDR_VEC:
2290 case ADDR_DIFF_VEC:
2291 case VAR_LOCATION:
2292 return;
2293
2294 case SET:
2295 if (GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
2296 goto asm_insn;
2297 else
2298 goto normal_insn;
2299 case PARALLEL:
2300 if ((GET_CODE (XVECEXP (body, 0, 0)) == SET
2301 && GET_CODE (SET_SRC (XVECEXP (body, 0, 0))) == ASM_OPERANDS)
2302 || GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
2303 goto asm_insn;
2304 else
2305 goto normal_insn;
2306 case ASM_OPERANDS:
2307 asm_insn:
2308 recog_data.n_operands = noperands = asm_noperands (body);
2309 if (noperands >= 0)
2310 {
2311 /* This insn is an `asm' with operands. */
2312
2313 /* expand_asm_operands makes sure there aren't too many operands. */
2314 gcc_assert (noperands <= MAX_RECOG_OPERANDS);
2315
2316 /* Now get the operand values and constraints out of the insn. */
2317 decode_asm_operands (body, recog_data.operand,
2318 recog_data.operand_loc,
2319 recog_data.constraints,
2320 recog_data.operand_mode, NULL);
2321 memset (recog_data.is_operator, 0, sizeof recog_data.is_operator);
2322 if (noperands > 0)
2323 {
2324 const char *p = recog_data.constraints[0];
2325 recog_data.n_alternatives = 1;
2326 while (*p)
2327 recog_data.n_alternatives += (*p++ == ',');
2328 }
2329 recog_data.is_asm = true;
2330 break;
2331 }
2332 fatal_insn_not_found (insn);
2333
2334 default:
2335 normal_insn:
2336 /* Ordinary insn: recognize it, get the operands via insn_extract
2337 and get the constraints. */
2338
2339 icode = recog_memoized (insn);
2340 if (icode < 0)
2341 fatal_insn_not_found (insn);
2342
2343 recog_data.n_operands = noperands = insn_data[icode].n_operands;
2344 recog_data.n_alternatives = insn_data[icode].n_alternatives;
2345 recog_data.n_dups = insn_data[icode].n_dups;
2346
2347 insn_extract (insn);
2348
2349 for (i = 0; i < noperands; i++)
2350 {
2351 recog_data.constraints[i] = insn_data[icode].operand[i].constraint;
2352 recog_data.is_operator[i] = insn_data[icode].operand[i].is_operator;
2353 recog_data.operand_mode[i] = insn_data[icode].operand[i].mode;
2354 /* VOIDmode match_operands gets mode from their real operand. */
2355 if (recog_data.operand_mode[i] == VOIDmode)
2356 recog_data.operand_mode[i] = GET_MODE (recog_data.operand[i]);
2357 }
2358 }
2359 for (i = 0; i < noperands; i++)
2360 recog_data.operand_type[i]
2361 = (recog_data.constraints[i][0] == '=' ? OP_OUT
2362 : recog_data.constraints[i][0] == '+' ? OP_INOUT
2363 : OP_IN);
2364
2365 gcc_assert (recog_data.n_alternatives <= MAX_RECOG_ALTERNATIVES);
2366
2367 recog_data.insn = NULL;
2368 which_alternative = -1;
2369 }
2370
2371 /* Fill in OP_ALT_BASE for an instruction that has N_OPERANDS operands,
2372 N_ALTERNATIVES alternatives and constraint strings CONSTRAINTS.
2373 OP_ALT_BASE has N_ALTERNATIVES * N_OPERANDS entries and CONSTRAINTS
2374 has N_OPERANDS entries. */
2375
2376 void
2377 preprocess_constraints (int n_operands, int n_alternatives,
2378 const char **constraints,
2379 operand_alternative *op_alt_base)
2380 {
2381 for (int i = 0; i < n_operands; i++)
2382 {
2383 int j;
2384 struct operand_alternative *op_alt;
2385 const char *p = constraints[i];
2386
2387 op_alt = op_alt_base;
2388
2389 for (j = 0; j < n_alternatives; j++, op_alt += n_operands)
2390 {
2391 op_alt[i].cl = NO_REGS;
2392 op_alt[i].constraint = p;
2393 op_alt[i].matches = -1;
2394 op_alt[i].matched = -1;
2395
2396 if (*p == '\0' || *p == ',')
2397 {
2398 op_alt[i].anything_ok = 1;
2399 continue;
2400 }
2401
2402 for (;;)
2403 {
2404 char c = *p;
2405 if (c == '#')
2406 do
2407 c = *++p;
2408 while (c != ',' && c != '\0');
2409 if (c == ',' || c == '\0')
2410 {
2411 p++;
2412 break;
2413 }
2414
2415 switch (c)
2416 {
2417 case '?':
2418 op_alt[i].reject += 6;
2419 break;
2420 case '!':
2421 op_alt[i].reject += 600;
2422 break;
2423 case '&':
2424 op_alt[i].earlyclobber = 1;
2425 break;
2426
2427 case '0': case '1': case '2': case '3': case '4':
2428 case '5': case '6': case '7': case '8': case '9':
2429 {
2430 char *end;
2431 op_alt[i].matches = strtoul (p, &end, 10);
2432 op_alt[op_alt[i].matches].matched = i;
2433 p = end;
2434 }
2435 continue;
2436
2437 case 'X':
2438 op_alt[i].anything_ok = 1;
2439 break;
2440
2441 case 'g':
2442 op_alt[i].cl =
2443 reg_class_subunion[(int) op_alt[i].cl][(int) GENERAL_REGS];
2444 break;
2445
2446 default:
2447 enum constraint_num cn = lookup_constraint (p);
2448 enum reg_class cl;
2449 switch (get_constraint_type (cn))
2450 {
2451 case CT_REGISTER:
2452 cl = reg_class_for_constraint (cn);
2453 if (cl != NO_REGS)
2454 op_alt[i].cl = reg_class_subunion[op_alt[i].cl][cl];
2455 break;
2456
2457 case CT_CONST_INT:
2458 break;
2459
2460 case CT_MEMORY:
2461 op_alt[i].memory_ok = 1;
2462 break;
2463
2464 case CT_ADDRESS:
2465 op_alt[i].is_address = 1;
2466 op_alt[i].cl
2467 = (reg_class_subunion
2468 [(int) op_alt[i].cl]
2469 [(int) base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2470 ADDRESS, SCRATCH)]);
2471 break;
2472
2473 case CT_FIXED_FORM:
2474 break;
2475 }
2476 break;
2477 }
2478 p += CONSTRAINT_LEN (c, p);
2479 }
2480 }
2481 }
2482 }
2483
2484 /* Return an array of operand_alternative instructions for
2485 instruction ICODE. */
2486
2487 const operand_alternative *
2488 preprocess_insn_constraints (int icode)
2489 {
2490 gcc_checking_assert (IN_RANGE (icode, 0, LAST_INSN_CODE));
2491 if (this_target_recog->x_op_alt[icode])
2492 return this_target_recog->x_op_alt[icode];
2493
2494 int n_operands = insn_data[icode].n_operands;
2495 if (n_operands == 0)
2496 return 0;
2497 /* Always provide at least one alternative so that which_op_alt ()
2498 works correctly. If the instruction has 0 alternatives (i.e. all
2499 constraint strings are empty) then each operand in this alternative
2500 will have anything_ok set. */
2501 int n_alternatives = MAX (insn_data[icode].n_alternatives, 1);
2502 int n_entries = n_operands * n_alternatives;
2503
2504 operand_alternative *op_alt = XCNEWVEC (operand_alternative, n_entries);
2505 const char **constraints = XALLOCAVEC (const char *, n_operands);
2506
2507 for (int i = 0; i < n_operands; ++i)
2508 constraints[i] = insn_data[icode].operand[i].constraint;
2509 preprocess_constraints (n_operands, n_alternatives, constraints, op_alt);
2510
2511 this_target_recog->x_op_alt[icode] = op_alt;
2512 return op_alt;
2513 }
2514
2515 /* After calling extract_insn, you can use this function to extract some
2516 information from the constraint strings into a more usable form.
2517 The collected data is stored in recog_op_alt. */
2518
2519 void
2520 preprocess_constraints (rtx_insn *insn)
2521 {
2522 int icode = INSN_CODE (insn);
2523 if (icode >= 0)
2524 recog_op_alt = preprocess_insn_constraints (icode);
2525 else
2526 {
2527 int n_operands = recog_data.n_operands;
2528 int n_alternatives = recog_data.n_alternatives;
2529 int n_entries = n_operands * n_alternatives;
2530 memset (asm_op_alt, 0, n_entries * sizeof (operand_alternative));
2531 preprocess_constraints (n_operands, n_alternatives,
2532 recog_data.constraints, asm_op_alt);
2533 recog_op_alt = asm_op_alt;
2534 }
2535 }
2536
2537 /* Check the operands of an insn against the insn's operand constraints
2538 and return 1 if they match any of the alternatives in ALTERNATIVES.
2539
2540 The information about the insn's operands, constraints, operand modes
2541 etc. is obtained from the global variables set up by extract_insn.
2542
2543 WHICH_ALTERNATIVE is set to a number which indicates which
2544 alternative of constraints was matched: 0 for the first alternative,
2545 1 for the next, etc.
2546
2547 In addition, when two operands are required to match
2548 and it happens that the output operand is (reg) while the
2549 input operand is --(reg) or ++(reg) (a pre-inc or pre-dec),
2550 make the output operand look like the input.
2551 This is because the output operand is the one the template will print.
2552
2553 This is used in final, just before printing the assembler code and by
2554 the routines that determine an insn's attribute.
2555
2556 If STRICT is a positive nonzero value, it means that we have been
2557 called after reload has been completed. In that case, we must
2558 do all checks strictly. If it is zero, it means that we have been called
2559 before reload has completed. In that case, we first try to see if we can
2560 find an alternative that matches strictly. If not, we try again, this
2561 time assuming that reload will fix up the insn. This provides a "best
2562 guess" for the alternative and is used to compute attributes of insns prior
2563 to reload. A negative value of STRICT is used for this internal call. */
2564
2565 struct funny_match
2566 {
2567 int this_op, other;
2568 };
2569
2570 int
2571 constrain_operands (int strict, alternative_mask alternatives)
2572 {
2573 const char *constraints[MAX_RECOG_OPERANDS];
2574 int matching_operands[MAX_RECOG_OPERANDS];
2575 int earlyclobber[MAX_RECOG_OPERANDS];
2576 int c;
2577
2578 struct funny_match funny_match[MAX_RECOG_OPERANDS];
2579 int funny_match_index;
2580
2581 which_alternative = 0;
2582 if (recog_data.n_operands == 0 || recog_data.n_alternatives == 0)
2583 return 1;
2584
2585 for (c = 0; c < recog_data.n_operands; c++)
2586 {
2587 constraints[c] = recog_data.constraints[c];
2588 matching_operands[c] = -1;
2589 }
2590
2591 do
2592 {
2593 int seen_earlyclobber_at = -1;
2594 int opno;
2595 int lose = 0;
2596 funny_match_index = 0;
2597
2598 if (!TEST_BIT (alternatives, which_alternative))
2599 {
2600 int i;
2601
2602 for (i = 0; i < recog_data.n_operands; i++)
2603 constraints[i] = skip_alternative (constraints[i]);
2604
2605 which_alternative++;
2606 continue;
2607 }
2608
2609 for (opno = 0; opno < recog_data.n_operands; opno++)
2610 {
2611 rtx op = recog_data.operand[opno];
2612 machine_mode mode = GET_MODE (op);
2613 const char *p = constraints[opno];
2614 int offset = 0;
2615 int win = 0;
2616 int val;
2617 int len;
2618
2619 earlyclobber[opno] = 0;
2620
2621 /* A unary operator may be accepted by the predicate, but it
2622 is irrelevant for matching constraints. */
2623 if (UNARY_P (op))
2624 op = XEXP (op, 0);
2625
2626 if (GET_CODE (op) == SUBREG)
2627 {
2628 if (REG_P (SUBREG_REG (op))
2629 && REGNO (SUBREG_REG (op)) < FIRST_PSEUDO_REGISTER)
2630 offset = subreg_regno_offset (REGNO (SUBREG_REG (op)),
2631 GET_MODE (SUBREG_REG (op)),
2632 SUBREG_BYTE (op),
2633 GET_MODE (op));
2634 op = SUBREG_REG (op);
2635 }
2636
2637 /* An empty constraint or empty alternative
2638 allows anything which matched the pattern. */
2639 if (*p == 0 || *p == ',')
2640 win = 1;
2641
2642 do
2643 switch (c = *p, len = CONSTRAINT_LEN (c, p), c)
2644 {
2645 case '\0':
2646 len = 0;
2647 break;
2648 case ',':
2649 c = '\0';
2650 break;
2651
2652 case '#':
2653 /* Ignore rest of this alternative as far as
2654 constraint checking is concerned. */
2655 do
2656 p++;
2657 while (*p && *p != ',');
2658 len = 0;
2659 break;
2660
2661 case '&':
2662 earlyclobber[opno] = 1;
2663 if (seen_earlyclobber_at < 0)
2664 seen_earlyclobber_at = opno;
2665 break;
2666
2667 case '0': case '1': case '2': case '3': case '4':
2668 case '5': case '6': case '7': case '8': case '9':
2669 {
2670 /* This operand must be the same as a previous one.
2671 This kind of constraint is used for instructions such
2672 as add when they take only two operands.
2673
2674 Note that the lower-numbered operand is passed first.
2675
2676 If we are not testing strictly, assume that this
2677 constraint will be satisfied. */
2678
2679 char *end;
2680 int match;
2681
2682 match = strtoul (p, &end, 10);
2683 p = end;
2684
2685 if (strict < 0)
2686 val = 1;
2687 else
2688 {
2689 rtx op1 = recog_data.operand[match];
2690 rtx op2 = recog_data.operand[opno];
2691
2692 /* A unary operator may be accepted by the predicate,
2693 but it is irrelevant for matching constraints. */
2694 if (UNARY_P (op1))
2695 op1 = XEXP (op1, 0);
2696 if (UNARY_P (op2))
2697 op2 = XEXP (op2, 0);
2698
2699 val = operands_match_p (op1, op2);
2700 }
2701
2702 matching_operands[opno] = match;
2703 matching_operands[match] = opno;
2704
2705 if (val != 0)
2706 win = 1;
2707
2708 /* If output is *x and input is *--x, arrange later
2709 to change the output to *--x as well, since the
2710 output op is the one that will be printed. */
2711 if (val == 2 && strict > 0)
2712 {
2713 funny_match[funny_match_index].this_op = opno;
2714 funny_match[funny_match_index++].other = match;
2715 }
2716 }
2717 len = 0;
2718 break;
2719
2720 case 'p':
2721 /* p is used for address_operands. When we are called by
2722 gen_reload, no one will have checked that the address is
2723 strictly valid, i.e., that all pseudos requiring hard regs
2724 have gotten them. */
2725 if (strict <= 0
2726 || (strict_memory_address_p (recog_data.operand_mode[opno],
2727 op)))
2728 win = 1;
2729 break;
2730
2731 /* No need to check general_operand again;
2732 it was done in insn-recog.c. Well, except that reload
2733 doesn't check the validity of its replacements, but
2734 that should only matter when there's a bug. */
2735 case 'g':
2736 /* Anything goes unless it is a REG and really has a hard reg
2737 but the hard reg is not in the class GENERAL_REGS. */
2738 if (REG_P (op))
2739 {
2740 if (strict < 0
2741 || GENERAL_REGS == ALL_REGS
2742 || (reload_in_progress
2743 && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2744 || reg_fits_class_p (op, GENERAL_REGS, offset, mode))
2745 win = 1;
2746 }
2747 else if (strict < 0 || general_operand (op, mode))
2748 win = 1;
2749 break;
2750
2751 default:
2752 {
2753 enum constraint_num cn = lookup_constraint (p);
2754 enum reg_class cl = reg_class_for_constraint (cn);
2755 if (cl != NO_REGS)
2756 {
2757 if (strict < 0
2758 || (strict == 0
2759 && REG_P (op)
2760 && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2761 || (strict == 0 && GET_CODE (op) == SCRATCH)
2762 || (REG_P (op)
2763 && reg_fits_class_p (op, cl, offset, mode)))
2764 win = 1;
2765 }
2766
2767 else if (constraint_satisfied_p (op, cn))
2768 win = 1;
2769
2770 else if (insn_extra_memory_constraint (cn)
2771 /* Every memory operand can be reloaded to fit. */
2772 && ((strict < 0 && MEM_P (op))
2773 /* Before reload, accept what reload can turn
2774 into a mem. */
2775 || (strict < 0 && CONSTANT_P (op))
2776 /* Before reload, accept a pseudo,
2777 since LRA can turn it into a mem. */
2778 || (strict < 0 && targetm.lra_p () && REG_P (op)
2779 && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2780 /* During reload, accept a pseudo */
2781 || (reload_in_progress && REG_P (op)
2782 && REGNO (op) >= FIRST_PSEUDO_REGISTER)))
2783 win = 1;
2784 else if (insn_extra_address_constraint (cn)
2785 /* Every address operand can be reloaded to fit. */
2786 && strict < 0)
2787 win = 1;
2788 /* Cater to architectures like IA-64 that define extra memory
2789 constraints without using define_memory_constraint. */
2790 else if (reload_in_progress
2791 && REG_P (op)
2792 && REGNO (op) >= FIRST_PSEUDO_REGISTER
2793 && reg_renumber[REGNO (op)] < 0
2794 && reg_equiv_mem (REGNO (op)) != 0
2795 && constraint_satisfied_p
2796 (reg_equiv_mem (REGNO (op)), cn))
2797 win = 1;
2798 break;
2799 }
2800 }
2801 while (p += len, c);
2802
2803 constraints[opno] = p;
2804 /* If this operand did not win somehow,
2805 this alternative loses. */
2806 if (! win)
2807 lose = 1;
2808 }
2809 /* This alternative won; the operands are ok.
2810 Change whichever operands this alternative says to change. */
2811 if (! lose)
2812 {
2813 int opno, eopno;
2814
2815 /* See if any earlyclobber operand conflicts with some other
2816 operand. */
2817
2818 if (strict > 0 && seen_earlyclobber_at >= 0)
2819 for (eopno = seen_earlyclobber_at;
2820 eopno < recog_data.n_operands;
2821 eopno++)
2822 /* Ignore earlyclobber operands now in memory,
2823 because we would often report failure when we have
2824 two memory operands, one of which was formerly a REG. */
2825 if (earlyclobber[eopno]
2826 && REG_P (recog_data.operand[eopno]))
2827 for (opno = 0; opno < recog_data.n_operands; opno++)
2828 if ((MEM_P (recog_data.operand[opno])
2829 || recog_data.operand_type[opno] != OP_OUT)
2830 && opno != eopno
2831 /* Ignore things like match_operator operands. */
2832 && *recog_data.constraints[opno] != 0
2833 && ! (matching_operands[opno] == eopno
2834 && operands_match_p (recog_data.operand[opno],
2835 recog_data.operand[eopno]))
2836 && ! safe_from_earlyclobber (recog_data.operand[opno],
2837 recog_data.operand[eopno]))
2838 lose = 1;
2839
2840 if (! lose)
2841 {
2842 while (--funny_match_index >= 0)
2843 {
2844 recog_data.operand[funny_match[funny_match_index].other]
2845 = recog_data.operand[funny_match[funny_match_index].this_op];
2846 }
2847
2848 #ifdef AUTO_INC_DEC
2849 /* For operands without < or > constraints reject side-effects. */
2850 if (recog_data.is_asm)
2851 {
2852 for (opno = 0; opno < recog_data.n_operands; opno++)
2853 if (MEM_P (recog_data.operand[opno]))
2854 switch (GET_CODE (XEXP (recog_data.operand[opno], 0)))
2855 {
2856 case PRE_INC:
2857 case POST_INC:
2858 case PRE_DEC:
2859 case POST_DEC:
2860 case PRE_MODIFY:
2861 case POST_MODIFY:
2862 if (strchr (recog_data.constraints[opno], '<') == NULL
2863 && strchr (recog_data.constraints[opno], '>')
2864 == NULL)
2865 return 0;
2866 break;
2867 default:
2868 break;
2869 }
2870 }
2871 #endif
2872 return 1;
2873 }
2874 }
2875
2876 which_alternative++;
2877 }
2878 while (which_alternative < recog_data.n_alternatives);
2879
2880 which_alternative = -1;
2881 /* If we are about to reject this, but we are not to test strictly,
2882 try a very loose test. Only return failure if it fails also. */
2883 if (strict == 0)
2884 return constrain_operands (-1, alternatives);
2885 else
2886 return 0;
2887 }
2888
2889 /* Return true iff OPERAND (assumed to be a REG rtx)
2890 is a hard reg in class CLASS when its regno is offset by OFFSET
2891 and changed to mode MODE.
2892 If REG occupies multiple hard regs, all of them must be in CLASS. */
2893
2894 bool
2895 reg_fits_class_p (const_rtx operand, reg_class_t cl, int offset,
2896 machine_mode mode)
2897 {
2898 unsigned int regno = REGNO (operand);
2899
2900 if (cl == NO_REGS)
2901 return false;
2902
2903 /* Regno must not be a pseudo register. Offset may be negative. */
2904 return (HARD_REGISTER_NUM_P (regno)
2905 && HARD_REGISTER_NUM_P (regno + offset)
2906 && in_hard_reg_set_p (reg_class_contents[(int) cl], mode,
2907 regno + offset));
2908 }
2909 \f
2910 /* Split single instruction. Helper function for split_all_insns and
2911 split_all_insns_noflow. Return last insn in the sequence if successful,
2912 or NULL if unsuccessful. */
2913
2914 static rtx_insn *
2915 split_insn (rtx_insn *insn)
2916 {
2917 /* Split insns here to get max fine-grain parallelism. */
2918 rtx_insn *first = PREV_INSN (insn);
2919 rtx_insn *last = try_split (PATTERN (insn), insn, 1);
2920 rtx insn_set, last_set, note;
2921
2922 if (last == insn)
2923 return NULL;
2924
2925 /* If the original instruction was a single set that was known to be
2926 equivalent to a constant, see if we can say the same about the last
2927 instruction in the split sequence. The two instructions must set
2928 the same destination. */
2929 insn_set = single_set (insn);
2930 if (insn_set)
2931 {
2932 last_set = single_set (last);
2933 if (last_set && rtx_equal_p (SET_DEST (last_set), SET_DEST (insn_set)))
2934 {
2935 note = find_reg_equal_equiv_note (insn);
2936 if (note && CONSTANT_P (XEXP (note, 0)))
2937 set_unique_reg_note (last, REG_EQUAL, XEXP (note, 0));
2938 else if (CONSTANT_P (SET_SRC (insn_set)))
2939 set_unique_reg_note (last, REG_EQUAL,
2940 copy_rtx (SET_SRC (insn_set)));
2941 }
2942 }
2943
2944 /* try_split returns the NOTE that INSN became. */
2945 SET_INSN_DELETED (insn);
2946
2947 /* ??? Coddle to md files that generate subregs in post-reload
2948 splitters instead of computing the proper hard register. */
2949 if (reload_completed && first != last)
2950 {
2951 first = NEXT_INSN (first);
2952 for (;;)
2953 {
2954 if (INSN_P (first))
2955 cleanup_subreg_operands (first);
2956 if (first == last)
2957 break;
2958 first = NEXT_INSN (first);
2959 }
2960 }
2961
2962 return last;
2963 }
2964
2965 /* Split all insns in the function. If UPD_LIFE, update life info after. */
2966
2967 void
2968 split_all_insns (void)
2969 {
2970 sbitmap blocks;
2971 bool changed;
2972 basic_block bb;
2973
2974 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
2975 bitmap_clear (blocks);
2976 changed = false;
2977
2978 FOR_EACH_BB_REVERSE_FN (bb, cfun)
2979 {
2980 rtx_insn *insn, *next;
2981 bool finish = false;
2982
2983 rtl_profile_for_bb (bb);
2984 for (insn = BB_HEAD (bb); !finish ; insn = next)
2985 {
2986 /* Can't use `next_real_insn' because that might go across
2987 CODE_LABELS and short-out basic blocks. */
2988 next = NEXT_INSN (insn);
2989 finish = (insn == BB_END (bb));
2990 if (INSN_P (insn))
2991 {
2992 rtx set = single_set (insn);
2993
2994 /* Don't split no-op move insns. These should silently
2995 disappear later in final. Splitting such insns would
2996 break the code that handles LIBCALL blocks. */
2997 if (set && set_noop_p (set))
2998 {
2999 /* Nops get in the way while scheduling, so delete them
3000 now if register allocation has already been done. It
3001 is too risky to try to do this before register
3002 allocation, and there are unlikely to be very many
3003 nops then anyways. */
3004 if (reload_completed)
3005 delete_insn_and_edges (insn);
3006 }
3007 else
3008 {
3009 if (split_insn (insn))
3010 {
3011 bitmap_set_bit (blocks, bb->index);
3012 changed = true;
3013 }
3014 }
3015 }
3016 }
3017 }
3018
3019 default_rtl_profile ();
3020 if (changed)
3021 find_many_sub_basic_blocks (blocks);
3022
3023 #ifdef ENABLE_CHECKING
3024 verify_flow_info ();
3025 #endif
3026
3027 sbitmap_free (blocks);
3028 }
3029
3030 /* Same as split_all_insns, but do not expect CFG to be available.
3031 Used by machine dependent reorg passes. */
3032
3033 unsigned int
3034 split_all_insns_noflow (void)
3035 {
3036 rtx_insn *next, *insn;
3037
3038 for (insn = get_insns (); insn; insn = next)
3039 {
3040 next = NEXT_INSN (insn);
3041 if (INSN_P (insn))
3042 {
3043 /* Don't split no-op move insns. These should silently
3044 disappear later in final. Splitting such insns would
3045 break the code that handles LIBCALL blocks. */
3046 rtx set = single_set (insn);
3047 if (set && set_noop_p (set))
3048 {
3049 /* Nops get in the way while scheduling, so delete them
3050 now if register allocation has already been done. It
3051 is too risky to try to do this before register
3052 allocation, and there are unlikely to be very many
3053 nops then anyways.
3054
3055 ??? Should we use delete_insn when the CFG isn't valid? */
3056 if (reload_completed)
3057 delete_insn_and_edges (insn);
3058 }
3059 else
3060 split_insn (insn);
3061 }
3062 }
3063 return 0;
3064 }
3065 \f
3066 #ifdef HAVE_peephole2
3067 struct peep2_insn_data
3068 {
3069 rtx_insn *insn;
3070 regset live_before;
3071 };
3072
3073 static struct peep2_insn_data peep2_insn_data[MAX_INSNS_PER_PEEP2 + 1];
3074 static int peep2_current;
3075
3076 static bool peep2_do_rebuild_jump_labels;
3077 static bool peep2_do_cleanup_cfg;
3078
3079 /* The number of instructions available to match a peep2. */
3080 int peep2_current_count;
3081
3082 /* A marker indicating the last insn of the block. The live_before regset
3083 for this element is correct, indicating DF_LIVE_OUT for the block. */
3084 #define PEEP2_EOB invalid_insn_rtx
3085
3086 /* Wrap N to fit into the peep2_insn_data buffer. */
3087
3088 static int
3089 peep2_buf_position (int n)
3090 {
3091 if (n >= MAX_INSNS_PER_PEEP2 + 1)
3092 n -= MAX_INSNS_PER_PEEP2 + 1;
3093 return n;
3094 }
3095
3096 /* Return the Nth non-note insn after `current', or return NULL_RTX if it
3097 does not exist. Used by the recognizer to find the next insn to match
3098 in a multi-insn pattern. */
3099
3100 rtx
3101 peep2_next_insn (int n)
3102 {
3103 gcc_assert (n <= peep2_current_count);
3104
3105 n = peep2_buf_position (peep2_current + n);
3106
3107 return peep2_insn_data[n].insn;
3108 }
3109
3110 /* Return true if REGNO is dead before the Nth non-note insn
3111 after `current'. */
3112
3113 int
3114 peep2_regno_dead_p (int ofs, int regno)
3115 {
3116 gcc_assert (ofs < MAX_INSNS_PER_PEEP2 + 1);
3117
3118 ofs = peep2_buf_position (peep2_current + ofs);
3119
3120 gcc_assert (peep2_insn_data[ofs].insn != NULL_RTX);
3121
3122 return ! REGNO_REG_SET_P (peep2_insn_data[ofs].live_before, regno);
3123 }
3124
3125 /* Similarly for a REG. */
3126
3127 int
3128 peep2_reg_dead_p (int ofs, rtx reg)
3129 {
3130 gcc_assert (ofs < MAX_INSNS_PER_PEEP2 + 1);
3131
3132 ofs = peep2_buf_position (peep2_current + ofs);
3133
3134 gcc_assert (peep2_insn_data[ofs].insn != NULL_RTX);
3135
3136 unsigned int end_regno = END_REGNO (reg);
3137 for (unsigned int regno = REGNO (reg); regno < end_regno; ++regno)
3138 if (REGNO_REG_SET_P (peep2_insn_data[ofs].live_before, regno))
3139 return 0;
3140 return 1;
3141 }
3142
3143 /* Regno offset to be used in the register search. */
3144 static int search_ofs;
3145
3146 /* Try to find a hard register of mode MODE, matching the register class in
3147 CLASS_STR, which is available at the beginning of insn CURRENT_INSN and
3148 remains available until the end of LAST_INSN. LAST_INSN may be NULL_RTX,
3149 in which case the only condition is that the register must be available
3150 before CURRENT_INSN.
3151 Registers that already have bits set in REG_SET will not be considered.
3152
3153 If an appropriate register is available, it will be returned and the
3154 corresponding bit(s) in REG_SET will be set; otherwise, NULL_RTX is
3155 returned. */
3156
3157 rtx
3158 peep2_find_free_register (int from, int to, const char *class_str,
3159 machine_mode mode, HARD_REG_SET *reg_set)
3160 {
3161 enum reg_class cl;
3162 HARD_REG_SET live;
3163 df_ref def;
3164 int i;
3165
3166 gcc_assert (from < MAX_INSNS_PER_PEEP2 + 1);
3167 gcc_assert (to < MAX_INSNS_PER_PEEP2 + 1);
3168
3169 from = peep2_buf_position (peep2_current + from);
3170 to = peep2_buf_position (peep2_current + to);
3171
3172 gcc_assert (peep2_insn_data[from].insn != NULL_RTX);
3173 REG_SET_TO_HARD_REG_SET (live, peep2_insn_data[from].live_before);
3174
3175 while (from != to)
3176 {
3177 gcc_assert (peep2_insn_data[from].insn != NULL_RTX);
3178
3179 /* Don't use registers set or clobbered by the insn. */
3180 FOR_EACH_INSN_DEF (def, peep2_insn_data[from].insn)
3181 SET_HARD_REG_BIT (live, DF_REF_REGNO (def));
3182
3183 from = peep2_buf_position (from + 1);
3184 }
3185
3186 cl = reg_class_for_constraint (lookup_constraint (class_str));
3187
3188 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3189 {
3190 int raw_regno, regno, success, j;
3191
3192 /* Distribute the free registers as much as possible. */
3193 raw_regno = search_ofs + i;
3194 if (raw_regno >= FIRST_PSEUDO_REGISTER)
3195 raw_regno -= FIRST_PSEUDO_REGISTER;
3196 #ifdef REG_ALLOC_ORDER
3197 regno = reg_alloc_order[raw_regno];
3198 #else
3199 regno = raw_regno;
3200 #endif
3201
3202 /* Can it support the mode we need? */
3203 if (! HARD_REGNO_MODE_OK (regno, mode))
3204 continue;
3205
3206 success = 1;
3207 for (j = 0; success && j < hard_regno_nregs[regno][mode]; j++)
3208 {
3209 /* Don't allocate fixed registers. */
3210 if (fixed_regs[regno + j])
3211 {
3212 success = 0;
3213 break;
3214 }
3215 /* Don't allocate global registers. */
3216 if (global_regs[regno + j])
3217 {
3218 success = 0;
3219 break;
3220 }
3221 /* Make sure the register is of the right class. */
3222 if (! TEST_HARD_REG_BIT (reg_class_contents[cl], regno + j))
3223 {
3224 success = 0;
3225 break;
3226 }
3227 /* And that we don't create an extra save/restore. */
3228 if (! call_used_regs[regno + j] && ! df_regs_ever_live_p (regno + j))
3229 {
3230 success = 0;
3231 break;
3232 }
3233
3234 if (! targetm.hard_regno_scratch_ok (regno + j))
3235 {
3236 success = 0;
3237 break;
3238 }
3239
3240 /* And we don't clobber traceback for noreturn functions. */
3241 if ((regno + j == FRAME_POINTER_REGNUM
3242 || regno + j == HARD_FRAME_POINTER_REGNUM)
3243 && (! reload_completed || frame_pointer_needed))
3244 {
3245 success = 0;
3246 break;
3247 }
3248
3249 if (TEST_HARD_REG_BIT (*reg_set, regno + j)
3250 || TEST_HARD_REG_BIT (live, regno + j))
3251 {
3252 success = 0;
3253 break;
3254 }
3255 }
3256
3257 if (success)
3258 {
3259 add_to_hard_reg_set (reg_set, mode, regno);
3260
3261 /* Start the next search with the next register. */
3262 if (++raw_regno >= FIRST_PSEUDO_REGISTER)
3263 raw_regno = 0;
3264 search_ofs = raw_regno;
3265
3266 return gen_rtx_REG (mode, regno);
3267 }
3268 }
3269
3270 search_ofs = 0;
3271 return NULL_RTX;
3272 }
3273
3274 /* Forget all currently tracked instructions, only remember current
3275 LIVE regset. */
3276
3277 static void
3278 peep2_reinit_state (regset live)
3279 {
3280 int i;
3281
3282 /* Indicate that all slots except the last holds invalid data. */
3283 for (i = 0; i < MAX_INSNS_PER_PEEP2; ++i)
3284 peep2_insn_data[i].insn = NULL;
3285 peep2_current_count = 0;
3286
3287 /* Indicate that the last slot contains live_after data. */
3288 peep2_insn_data[MAX_INSNS_PER_PEEP2].insn = PEEP2_EOB;
3289 peep2_current = MAX_INSNS_PER_PEEP2;
3290
3291 COPY_REG_SET (peep2_insn_data[MAX_INSNS_PER_PEEP2].live_before, live);
3292 }
3293
3294 /* While scanning basic block BB, we found a match of length MATCH_LEN,
3295 starting at INSN. Perform the replacement, removing the old insns and
3296 replacing them with ATTEMPT. Returns the last insn emitted, or NULL
3297 if the replacement is rejected. */
3298
3299 static rtx_insn *
3300 peep2_attempt (basic_block bb, rtx_insn *insn, int match_len, rtx_insn *attempt)
3301 {
3302 int i;
3303 rtx_insn *last, *before_try, *x;
3304 rtx eh_note, as_note;
3305 rtx_insn *old_insn;
3306 rtx_insn *new_insn;
3307 bool was_call = false;
3308
3309 /* If we are splitting an RTX_FRAME_RELATED_P insn, do not allow it to
3310 match more than one insn, or to be split into more than one insn. */
3311 old_insn = peep2_insn_data[peep2_current].insn;
3312 if (RTX_FRAME_RELATED_P (old_insn))
3313 {
3314 bool any_note = false;
3315 rtx note;
3316
3317 if (match_len != 0)
3318 return NULL;
3319
3320 /* Look for one "active" insn. I.e. ignore any "clobber" insns that
3321 may be in the stream for the purpose of register allocation. */
3322 if (active_insn_p (attempt))
3323 new_insn = attempt;
3324 else
3325 new_insn = next_active_insn (attempt);
3326 if (next_active_insn (new_insn))
3327 return NULL;
3328
3329 /* We have a 1-1 replacement. Copy over any frame-related info. */
3330 RTX_FRAME_RELATED_P (new_insn) = 1;
3331
3332 /* Allow the backend to fill in a note during the split. */
3333 for (note = REG_NOTES (new_insn); note ; note = XEXP (note, 1))
3334 switch (REG_NOTE_KIND (note))
3335 {
3336 case REG_FRAME_RELATED_EXPR:
3337 case REG_CFA_DEF_CFA:
3338 case REG_CFA_ADJUST_CFA:
3339 case REG_CFA_OFFSET:
3340 case REG_CFA_REGISTER:
3341 case REG_CFA_EXPRESSION:
3342 case REG_CFA_RESTORE:
3343 case REG_CFA_SET_VDRAP:
3344 any_note = true;
3345 break;
3346 default:
3347 break;
3348 }
3349
3350 /* If the backend didn't supply a note, copy one over. */
3351 if (!any_note)
3352 for (note = REG_NOTES (old_insn); note ; note = XEXP (note, 1))
3353 switch (REG_NOTE_KIND (note))
3354 {
3355 case REG_FRAME_RELATED_EXPR:
3356 case REG_CFA_DEF_CFA:
3357 case REG_CFA_ADJUST_CFA:
3358 case REG_CFA_OFFSET:
3359 case REG_CFA_REGISTER:
3360 case REG_CFA_EXPRESSION:
3361 case REG_CFA_RESTORE:
3362 case REG_CFA_SET_VDRAP:
3363 add_reg_note (new_insn, REG_NOTE_KIND (note), XEXP (note, 0));
3364 any_note = true;
3365 break;
3366 default:
3367 break;
3368 }
3369
3370 /* If there still isn't a note, make sure the unwind info sees the
3371 same expression as before the split. */
3372 if (!any_note)
3373 {
3374 rtx old_set, new_set;
3375
3376 /* The old insn had better have been simple, or annotated. */
3377 old_set = single_set (old_insn);
3378 gcc_assert (old_set != NULL);
3379
3380 new_set = single_set (new_insn);
3381 if (!new_set || !rtx_equal_p (new_set, old_set))
3382 add_reg_note (new_insn, REG_FRAME_RELATED_EXPR, old_set);
3383 }
3384
3385 /* Copy prologue/epilogue status. This is required in order to keep
3386 proper placement of EPILOGUE_BEG and the DW_CFA_remember_state. */
3387 maybe_copy_prologue_epilogue_insn (old_insn, new_insn);
3388 }
3389
3390 /* If we are splitting a CALL_INSN, look for the CALL_INSN
3391 in SEQ and copy our CALL_INSN_FUNCTION_USAGE and other
3392 cfg-related call notes. */
3393 for (i = 0; i <= match_len; ++i)
3394 {
3395 int j;
3396 rtx note;
3397
3398 j = peep2_buf_position (peep2_current + i);
3399 old_insn = peep2_insn_data[j].insn;
3400 if (!CALL_P (old_insn))
3401 continue;
3402 was_call = true;
3403
3404 new_insn = attempt;
3405 while (new_insn != NULL_RTX)
3406 {
3407 if (CALL_P (new_insn))
3408 break;
3409 new_insn = NEXT_INSN (new_insn);
3410 }
3411
3412 gcc_assert (new_insn != NULL_RTX);
3413
3414 CALL_INSN_FUNCTION_USAGE (new_insn)
3415 = CALL_INSN_FUNCTION_USAGE (old_insn);
3416 SIBLING_CALL_P (new_insn) = SIBLING_CALL_P (old_insn);
3417
3418 for (note = REG_NOTES (old_insn);
3419 note;
3420 note = XEXP (note, 1))
3421 switch (REG_NOTE_KIND (note))
3422 {
3423 case REG_NORETURN:
3424 case REG_SETJMP:
3425 case REG_TM:
3426 add_reg_note (new_insn, REG_NOTE_KIND (note),
3427 XEXP (note, 0));
3428 break;
3429 default:
3430 /* Discard all other reg notes. */
3431 break;
3432 }
3433
3434 /* Croak if there is another call in the sequence. */
3435 while (++i <= match_len)
3436 {
3437 j = peep2_buf_position (peep2_current + i);
3438 old_insn = peep2_insn_data[j].insn;
3439 gcc_assert (!CALL_P (old_insn));
3440 }
3441 break;
3442 }
3443
3444 /* If we matched any instruction that had a REG_ARGS_SIZE, then
3445 move those notes over to the new sequence. */
3446 as_note = NULL;
3447 for (i = match_len; i >= 0; --i)
3448 {
3449 int j = peep2_buf_position (peep2_current + i);
3450 old_insn = peep2_insn_data[j].insn;
3451
3452 as_note = find_reg_note (old_insn, REG_ARGS_SIZE, NULL);
3453 if (as_note)
3454 break;
3455 }
3456
3457 i = peep2_buf_position (peep2_current + match_len);
3458 eh_note = find_reg_note (peep2_insn_data[i].insn, REG_EH_REGION, NULL_RTX);
3459
3460 /* Replace the old sequence with the new. */
3461 rtx_insn *peepinsn = peep2_insn_data[i].insn;
3462 last = emit_insn_after_setloc (attempt,
3463 peep2_insn_data[i].insn,
3464 INSN_LOCATION (peepinsn));
3465 before_try = PREV_INSN (insn);
3466 delete_insn_chain (insn, peep2_insn_data[i].insn, false);
3467
3468 /* Re-insert the EH_REGION notes. */
3469 if (eh_note || (was_call && nonlocal_goto_handler_labels))
3470 {
3471 edge eh_edge;
3472 edge_iterator ei;
3473
3474 FOR_EACH_EDGE (eh_edge, ei, bb->succs)
3475 if (eh_edge->flags & (EDGE_EH | EDGE_ABNORMAL_CALL))
3476 break;
3477
3478 if (eh_note)
3479 copy_reg_eh_region_note_backward (eh_note, last, before_try);
3480
3481 if (eh_edge)
3482 for (x = last; x != before_try; x = PREV_INSN (x))
3483 if (x != BB_END (bb)
3484 && (can_throw_internal (x)
3485 || can_nonlocal_goto (x)))
3486 {
3487 edge nfte, nehe;
3488 int flags;
3489
3490 nfte = split_block (bb, x);
3491 flags = (eh_edge->flags
3492 & (EDGE_EH | EDGE_ABNORMAL));
3493 if (CALL_P (x))
3494 flags |= EDGE_ABNORMAL_CALL;
3495 nehe = make_edge (nfte->src, eh_edge->dest,
3496 flags);
3497
3498 nehe->probability = eh_edge->probability;
3499 nfte->probability
3500 = REG_BR_PROB_BASE - nehe->probability;
3501
3502 peep2_do_cleanup_cfg |= purge_dead_edges (nfte->dest);
3503 bb = nfte->src;
3504 eh_edge = nehe;
3505 }
3506
3507 /* Converting possibly trapping insn to non-trapping is
3508 possible. Zap dummy outgoing edges. */
3509 peep2_do_cleanup_cfg |= purge_dead_edges (bb);
3510 }
3511
3512 /* Re-insert the ARGS_SIZE notes. */
3513 if (as_note)
3514 fixup_args_size_notes (before_try, last, INTVAL (XEXP (as_note, 0)));
3515
3516 /* If we generated a jump instruction, it won't have
3517 JUMP_LABEL set. Recompute after we're done. */
3518 for (x = last; x != before_try; x = PREV_INSN (x))
3519 if (JUMP_P (x))
3520 {
3521 peep2_do_rebuild_jump_labels = true;
3522 break;
3523 }
3524
3525 return last;
3526 }
3527
3528 /* After performing a replacement in basic block BB, fix up the life
3529 information in our buffer. LAST is the last of the insns that we
3530 emitted as a replacement. PREV is the insn before the start of
3531 the replacement. MATCH_LEN is the number of instructions that were
3532 matched, and which now need to be replaced in the buffer. */
3533
3534 static void
3535 peep2_update_life (basic_block bb, int match_len, rtx_insn *last,
3536 rtx_insn *prev)
3537 {
3538 int i = peep2_buf_position (peep2_current + match_len + 1);
3539 rtx_insn *x;
3540 regset_head live;
3541
3542 INIT_REG_SET (&live);
3543 COPY_REG_SET (&live, peep2_insn_data[i].live_before);
3544
3545 gcc_assert (peep2_current_count >= match_len + 1);
3546 peep2_current_count -= match_len + 1;
3547
3548 x = last;
3549 do
3550 {
3551 if (INSN_P (x))
3552 {
3553 df_insn_rescan (x);
3554 if (peep2_current_count < MAX_INSNS_PER_PEEP2)
3555 {
3556 peep2_current_count++;
3557 if (--i < 0)
3558 i = MAX_INSNS_PER_PEEP2;
3559 peep2_insn_data[i].insn = x;
3560 df_simulate_one_insn_backwards (bb, x, &live);
3561 COPY_REG_SET (peep2_insn_data[i].live_before, &live);
3562 }
3563 }
3564 x = PREV_INSN (x);
3565 }
3566 while (x != prev);
3567 CLEAR_REG_SET (&live);
3568
3569 peep2_current = i;
3570 }
3571
3572 /* Add INSN, which is in BB, at the end of the peep2 insn buffer if possible.
3573 Return true if we added it, false otherwise. The caller will try to match
3574 peepholes against the buffer if we return false; otherwise it will try to
3575 add more instructions to the buffer. */
3576
3577 static bool
3578 peep2_fill_buffer (basic_block bb, rtx_insn *insn, regset live)
3579 {
3580 int pos;
3581
3582 /* Once we have filled the maximum number of insns the buffer can hold,
3583 allow the caller to match the insns against peepholes. We wait until
3584 the buffer is full in case the target has similar peepholes of different
3585 length; we always want to match the longest if possible. */
3586 if (peep2_current_count == MAX_INSNS_PER_PEEP2)
3587 return false;
3588
3589 /* If an insn has RTX_FRAME_RELATED_P set, do not allow it to be matched with
3590 any other pattern, lest it change the semantics of the frame info. */
3591 if (RTX_FRAME_RELATED_P (insn))
3592 {
3593 /* Let the buffer drain first. */
3594 if (peep2_current_count > 0)
3595 return false;
3596 /* Now the insn will be the only thing in the buffer. */
3597 }
3598
3599 pos = peep2_buf_position (peep2_current + peep2_current_count);
3600 peep2_insn_data[pos].insn = insn;
3601 COPY_REG_SET (peep2_insn_data[pos].live_before, live);
3602 peep2_current_count++;
3603
3604 df_simulate_one_insn_forwards (bb, insn, live);
3605 return true;
3606 }
3607
3608 /* Perform the peephole2 optimization pass. */
3609
3610 static void
3611 peephole2_optimize (void)
3612 {
3613 rtx_insn *insn;
3614 bitmap live;
3615 int i;
3616 basic_block bb;
3617
3618 peep2_do_cleanup_cfg = false;
3619 peep2_do_rebuild_jump_labels = false;
3620
3621 df_set_flags (DF_LR_RUN_DCE);
3622 df_note_add_problem ();
3623 df_analyze ();
3624
3625 /* Initialize the regsets we're going to use. */
3626 for (i = 0; i < MAX_INSNS_PER_PEEP2 + 1; ++i)
3627 peep2_insn_data[i].live_before = BITMAP_ALLOC (&reg_obstack);
3628 search_ofs = 0;
3629 live = BITMAP_ALLOC (&reg_obstack);
3630
3631 FOR_EACH_BB_REVERSE_FN (bb, cfun)
3632 {
3633 bool past_end = false;
3634 int pos;
3635
3636 rtl_profile_for_bb (bb);
3637
3638 /* Start up propagation. */
3639 bitmap_copy (live, DF_LR_IN (bb));
3640 df_simulate_initialize_forwards (bb, live);
3641 peep2_reinit_state (live);
3642
3643 insn = BB_HEAD (bb);
3644 for (;;)
3645 {
3646 rtx_insn *attempt, *head;
3647 int match_len;
3648
3649 if (!past_end && !NONDEBUG_INSN_P (insn))
3650 {
3651 next_insn:
3652 insn = NEXT_INSN (insn);
3653 if (insn == NEXT_INSN (BB_END (bb)))
3654 past_end = true;
3655 continue;
3656 }
3657 if (!past_end && peep2_fill_buffer (bb, insn, live))
3658 goto next_insn;
3659
3660 /* If we did not fill an empty buffer, it signals the end of the
3661 block. */
3662 if (peep2_current_count == 0)
3663 break;
3664
3665 /* The buffer filled to the current maximum, so try to match. */
3666
3667 pos = peep2_buf_position (peep2_current + peep2_current_count);
3668 peep2_insn_data[pos].insn = PEEP2_EOB;
3669 COPY_REG_SET (peep2_insn_data[pos].live_before, live);
3670
3671 /* Match the peephole. */
3672 head = peep2_insn_data[peep2_current].insn;
3673 attempt = safe_as_a <rtx_insn *> (
3674 peephole2_insns (PATTERN (head), head, &match_len));
3675 if (attempt != NULL)
3676 {
3677 rtx_insn *last = peep2_attempt (bb, head, match_len, attempt);
3678 if (last)
3679 {
3680 peep2_update_life (bb, match_len, last, PREV_INSN (attempt));
3681 continue;
3682 }
3683 }
3684
3685 /* No match: advance the buffer by one insn. */
3686 peep2_current = peep2_buf_position (peep2_current + 1);
3687 peep2_current_count--;
3688 }
3689 }
3690
3691 default_rtl_profile ();
3692 for (i = 0; i < MAX_INSNS_PER_PEEP2 + 1; ++i)
3693 BITMAP_FREE (peep2_insn_data[i].live_before);
3694 BITMAP_FREE (live);
3695 if (peep2_do_rebuild_jump_labels)
3696 rebuild_jump_labels (get_insns ());
3697 if (peep2_do_cleanup_cfg)
3698 cleanup_cfg (CLEANUP_CFG_CHANGED);
3699 }
3700 #endif /* HAVE_peephole2 */
3701
3702 /* Common predicates for use with define_bypass. */
3703
3704 /* True if the dependency between OUT_INSN and IN_INSN is on the store
3705 data not the address operand(s) of the store. IN_INSN and OUT_INSN
3706 must be either a single_set or a PARALLEL with SETs inside. */
3707
3708 int
3709 store_data_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
3710 {
3711 rtx out_set, in_set;
3712 rtx out_pat, in_pat;
3713 rtx out_exp, in_exp;
3714 int i, j;
3715
3716 in_set = single_set (in_insn);
3717 if (in_set)
3718 {
3719 if (!MEM_P (SET_DEST (in_set)))
3720 return false;
3721
3722 out_set = single_set (out_insn);
3723 if (out_set)
3724 {
3725 if (reg_mentioned_p (SET_DEST (out_set), SET_DEST (in_set)))
3726 return false;
3727 }
3728 else
3729 {
3730 out_pat = PATTERN (out_insn);
3731
3732 if (GET_CODE (out_pat) != PARALLEL)
3733 return false;
3734
3735 for (i = 0; i < XVECLEN (out_pat, 0); i++)
3736 {
3737 out_exp = XVECEXP (out_pat, 0, i);
3738
3739 if (GET_CODE (out_exp) == CLOBBER)
3740 continue;
3741
3742 gcc_assert (GET_CODE (out_exp) == SET);
3743
3744 if (reg_mentioned_p (SET_DEST (out_exp), SET_DEST (in_set)))
3745 return false;
3746 }
3747 }
3748 }
3749 else
3750 {
3751 in_pat = PATTERN (in_insn);
3752 gcc_assert (GET_CODE (in_pat) == PARALLEL);
3753
3754 for (i = 0; i < XVECLEN (in_pat, 0); i++)
3755 {
3756 in_exp = XVECEXP (in_pat, 0, i);
3757
3758 if (GET_CODE (in_exp) == CLOBBER)
3759 continue;
3760
3761 gcc_assert (GET_CODE (in_exp) == SET);
3762
3763 if (!MEM_P (SET_DEST (in_exp)))
3764 return false;
3765
3766 out_set = single_set (out_insn);
3767 if (out_set)
3768 {
3769 if (reg_mentioned_p (SET_DEST (out_set), SET_DEST (in_exp)))
3770 return false;
3771 }
3772 else
3773 {
3774 out_pat = PATTERN (out_insn);
3775 gcc_assert (GET_CODE (out_pat) == PARALLEL);
3776
3777 for (j = 0; j < XVECLEN (out_pat, 0); j++)
3778 {
3779 out_exp = XVECEXP (out_pat, 0, j);
3780
3781 if (GET_CODE (out_exp) == CLOBBER)
3782 continue;
3783
3784 gcc_assert (GET_CODE (out_exp) == SET);
3785
3786 if (reg_mentioned_p (SET_DEST (out_exp), SET_DEST (in_exp)))
3787 return false;
3788 }
3789 }
3790 }
3791 }
3792
3793 return true;
3794 }
3795
3796 /* True if the dependency between OUT_INSN and IN_INSN is in the IF_THEN_ELSE
3797 condition, and not the THEN or ELSE branch. OUT_INSN may be either a single
3798 or multiple set; IN_INSN should be single_set for truth, but for convenience
3799 of insn categorization may be any JUMP or CALL insn. */
3800
3801 int
3802 if_test_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
3803 {
3804 rtx out_set, in_set;
3805
3806 in_set = single_set (in_insn);
3807 if (! in_set)
3808 {
3809 gcc_assert (JUMP_P (in_insn) || CALL_P (in_insn));
3810 return false;
3811 }
3812
3813 if (GET_CODE (SET_SRC (in_set)) != IF_THEN_ELSE)
3814 return false;
3815 in_set = SET_SRC (in_set);
3816
3817 out_set = single_set (out_insn);
3818 if (out_set)
3819 {
3820 if (reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 1))
3821 || reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 2)))
3822 return false;
3823 }
3824 else
3825 {
3826 rtx out_pat;
3827 int i;
3828
3829 out_pat = PATTERN (out_insn);
3830 gcc_assert (GET_CODE (out_pat) == PARALLEL);
3831
3832 for (i = 0; i < XVECLEN (out_pat, 0); i++)
3833 {
3834 rtx exp = XVECEXP (out_pat, 0, i);
3835
3836 if (GET_CODE (exp) == CLOBBER)
3837 continue;
3838
3839 gcc_assert (GET_CODE (exp) == SET);
3840
3841 if (reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 1))
3842 || reg_mentioned_p (SET_DEST (out_set), XEXP (in_set, 2)))
3843 return false;
3844 }
3845 }
3846
3847 return true;
3848 }
3849 \f
3850 static unsigned int
3851 rest_of_handle_peephole2 (void)
3852 {
3853 #ifdef HAVE_peephole2
3854 peephole2_optimize ();
3855 #endif
3856 return 0;
3857 }
3858
3859 namespace {
3860
3861 const pass_data pass_data_peephole2 =
3862 {
3863 RTL_PASS, /* type */
3864 "peephole2", /* name */
3865 OPTGROUP_NONE, /* optinfo_flags */
3866 TV_PEEPHOLE2, /* tv_id */
3867 0, /* properties_required */
3868 0, /* properties_provided */
3869 0, /* properties_destroyed */
3870 0, /* todo_flags_start */
3871 TODO_df_finish, /* todo_flags_finish */
3872 };
3873
3874 class pass_peephole2 : public rtl_opt_pass
3875 {
3876 public:
3877 pass_peephole2 (gcc::context *ctxt)
3878 : rtl_opt_pass (pass_data_peephole2, ctxt)
3879 {}
3880
3881 /* opt_pass methods: */
3882 /* The epiphany backend creates a second instance of this pass, so we need
3883 a clone method. */
3884 opt_pass * clone () { return new pass_peephole2 (m_ctxt); }
3885 virtual bool gate (function *) { return (optimize > 0 && flag_peephole2); }
3886 virtual unsigned int execute (function *)
3887 {
3888 return rest_of_handle_peephole2 ();
3889 }
3890
3891 }; // class pass_peephole2
3892
3893 } // anon namespace
3894
3895 rtl_opt_pass *
3896 make_pass_peephole2 (gcc::context *ctxt)
3897 {
3898 return new pass_peephole2 (ctxt);
3899 }
3900
3901 namespace {
3902
3903 const pass_data pass_data_split_all_insns =
3904 {
3905 RTL_PASS, /* type */
3906 "split1", /* name */
3907 OPTGROUP_NONE, /* optinfo_flags */
3908 TV_NONE, /* tv_id */
3909 0, /* properties_required */
3910 0, /* properties_provided */
3911 0, /* properties_destroyed */
3912 0, /* todo_flags_start */
3913 0, /* todo_flags_finish */
3914 };
3915
3916 class pass_split_all_insns : public rtl_opt_pass
3917 {
3918 public:
3919 pass_split_all_insns (gcc::context *ctxt)
3920 : rtl_opt_pass (pass_data_split_all_insns, ctxt)
3921 {}
3922
3923 /* opt_pass methods: */
3924 /* The epiphany backend creates a second instance of this pass, so
3925 we need a clone method. */
3926 opt_pass * clone () { return new pass_split_all_insns (m_ctxt); }
3927 virtual unsigned int execute (function *)
3928 {
3929 split_all_insns ();
3930 return 0;
3931 }
3932
3933 }; // class pass_split_all_insns
3934
3935 } // anon namespace
3936
3937 rtl_opt_pass *
3938 make_pass_split_all_insns (gcc::context *ctxt)
3939 {
3940 return new pass_split_all_insns (ctxt);
3941 }
3942
3943 static unsigned int
3944 rest_of_handle_split_after_reload (void)
3945 {
3946 /* If optimizing, then go ahead and split insns now. */
3947 #ifndef STACK_REGS
3948 if (optimize > 0)
3949 #endif
3950 split_all_insns ();
3951 return 0;
3952 }
3953
3954 namespace {
3955
3956 const pass_data pass_data_split_after_reload =
3957 {
3958 RTL_PASS, /* type */
3959 "split2", /* name */
3960 OPTGROUP_NONE, /* optinfo_flags */
3961 TV_NONE, /* tv_id */
3962 0, /* properties_required */
3963 0, /* properties_provided */
3964 0, /* properties_destroyed */
3965 0, /* todo_flags_start */
3966 0, /* todo_flags_finish */
3967 };
3968
3969 class pass_split_after_reload : public rtl_opt_pass
3970 {
3971 public:
3972 pass_split_after_reload (gcc::context *ctxt)
3973 : rtl_opt_pass (pass_data_split_after_reload, ctxt)
3974 {}
3975
3976 /* opt_pass methods: */
3977 virtual unsigned int execute (function *)
3978 {
3979 return rest_of_handle_split_after_reload ();
3980 }
3981
3982 }; // class pass_split_after_reload
3983
3984 } // anon namespace
3985
3986 rtl_opt_pass *
3987 make_pass_split_after_reload (gcc::context *ctxt)
3988 {
3989 return new pass_split_after_reload (ctxt);
3990 }
3991
3992 namespace {
3993
3994 const pass_data pass_data_split_before_regstack =
3995 {
3996 RTL_PASS, /* type */
3997 "split3", /* name */
3998 OPTGROUP_NONE, /* optinfo_flags */
3999 TV_NONE, /* tv_id */
4000 0, /* properties_required */
4001 0, /* properties_provided */
4002 0, /* properties_destroyed */
4003 0, /* todo_flags_start */
4004 0, /* todo_flags_finish */
4005 };
4006
4007 class pass_split_before_regstack : public rtl_opt_pass
4008 {
4009 public:
4010 pass_split_before_regstack (gcc::context *ctxt)
4011 : rtl_opt_pass (pass_data_split_before_regstack, ctxt)
4012 {}
4013
4014 /* opt_pass methods: */
4015 virtual bool gate (function *);
4016 virtual unsigned int execute (function *)
4017 {
4018 split_all_insns ();
4019 return 0;
4020 }
4021
4022 }; // class pass_split_before_regstack
4023
4024 bool
4025 pass_split_before_regstack::gate (function *)
4026 {
4027 #if HAVE_ATTR_length && defined (STACK_REGS)
4028 /* If flow2 creates new instructions which need splitting
4029 and scheduling after reload is not done, they might not be
4030 split until final which doesn't allow splitting
4031 if HAVE_ATTR_length. */
4032 # ifdef INSN_SCHEDULING
4033 return (optimize && !flag_schedule_insns_after_reload);
4034 # else
4035 return (optimize);
4036 # endif
4037 #else
4038 return 0;
4039 #endif
4040 }
4041
4042 } // anon namespace
4043
4044 rtl_opt_pass *
4045 make_pass_split_before_regstack (gcc::context *ctxt)
4046 {
4047 return new pass_split_before_regstack (ctxt);
4048 }
4049
4050 static unsigned int
4051 rest_of_handle_split_before_sched2 (void)
4052 {
4053 #ifdef INSN_SCHEDULING
4054 split_all_insns ();
4055 #endif
4056 return 0;
4057 }
4058
4059 namespace {
4060
4061 const pass_data pass_data_split_before_sched2 =
4062 {
4063 RTL_PASS, /* type */
4064 "split4", /* name */
4065 OPTGROUP_NONE, /* optinfo_flags */
4066 TV_NONE, /* tv_id */
4067 0, /* properties_required */
4068 0, /* properties_provided */
4069 0, /* properties_destroyed */
4070 0, /* todo_flags_start */
4071 0, /* todo_flags_finish */
4072 };
4073
4074 class pass_split_before_sched2 : public rtl_opt_pass
4075 {
4076 public:
4077 pass_split_before_sched2 (gcc::context *ctxt)
4078 : rtl_opt_pass (pass_data_split_before_sched2, ctxt)
4079 {}
4080
4081 /* opt_pass methods: */
4082 virtual bool gate (function *)
4083 {
4084 #ifdef INSN_SCHEDULING
4085 return optimize > 0 && flag_schedule_insns_after_reload;
4086 #else
4087 return false;
4088 #endif
4089 }
4090
4091 virtual unsigned int execute (function *)
4092 {
4093 return rest_of_handle_split_before_sched2 ();
4094 }
4095
4096 }; // class pass_split_before_sched2
4097
4098 } // anon namespace
4099
4100 rtl_opt_pass *
4101 make_pass_split_before_sched2 (gcc::context *ctxt)
4102 {
4103 return new pass_split_before_sched2 (ctxt);
4104 }
4105
4106 namespace {
4107
4108 const pass_data pass_data_split_for_shorten_branches =
4109 {
4110 RTL_PASS, /* type */
4111 "split5", /* name */
4112 OPTGROUP_NONE, /* optinfo_flags */
4113 TV_NONE, /* tv_id */
4114 0, /* properties_required */
4115 0, /* properties_provided */
4116 0, /* properties_destroyed */
4117 0, /* todo_flags_start */
4118 0, /* todo_flags_finish */
4119 };
4120
4121 class pass_split_for_shorten_branches : public rtl_opt_pass
4122 {
4123 public:
4124 pass_split_for_shorten_branches (gcc::context *ctxt)
4125 : rtl_opt_pass (pass_data_split_for_shorten_branches, ctxt)
4126 {}
4127
4128 /* opt_pass methods: */
4129 virtual bool gate (function *)
4130 {
4131 /* The placement of the splitting that we do for shorten_branches
4132 depends on whether regstack is used by the target or not. */
4133 #if HAVE_ATTR_length && !defined (STACK_REGS)
4134 return true;
4135 #else
4136 return false;
4137 #endif
4138 }
4139
4140 virtual unsigned int execute (function *)
4141 {
4142 return split_all_insns_noflow ();
4143 }
4144
4145 }; // class pass_split_for_shorten_branches
4146
4147 } // anon namespace
4148
4149 rtl_opt_pass *
4150 make_pass_split_for_shorten_branches (gcc::context *ctxt)
4151 {
4152 return new pass_split_for_shorten_branches (ctxt);
4153 }
4154
4155 /* (Re)initialize the target information after a change in target. */
4156
4157 void
4158 recog_init ()
4159 {
4160 /* The information is zero-initialized, so we don't need to do anything
4161 first time round. */
4162 if (!this_target_recog->x_initialized)
4163 {
4164 this_target_recog->x_initialized = true;
4165 return;
4166 }
4167 memset (this_target_recog->x_bool_attr_masks, 0,
4168 sizeof (this_target_recog->x_bool_attr_masks));
4169 for (int i = 0; i < LAST_INSN_CODE; ++i)
4170 if (this_target_recog->x_op_alt[i])
4171 {
4172 free (this_target_recog->x_op_alt[i]);
4173 this_target_recog->x_op_alt[i] = 0;
4174 }
4175 }