* lib/old-dejagnu.exp: Don't delete output of executable.
[official-gcc.git] / gcc / regmove.c
blobcbbeb835c7ee2a98175da034f71e47c33ed260f8
1 /* Move registers around to reduce number of move instructions needed.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This module looks for cases where matching constraints would force
24 an instruction to need a reload, and this reload would be a register
25 to register move. It then attempts to change the registers used by the
26 instruction to avoid the move instruction. */
28 #include "config.h"
29 #include "system.h"
30 #include "rtl.h" /* stdio.h must precede rtl.h for FFS. */
31 #include "tm_p.h"
32 #include "insn-config.h"
33 #include "recog.h"
34 #include "output.h"
35 #include "regs.h"
36 #include "hard-reg-set.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "expr.h"
40 #include "insn-flags.h"
41 #include "basic-block.h"
42 #include "toplev.h"
44 static int perhaps_ends_bb_p PARAMS ((rtx));
45 static int optimize_reg_copy_1 PARAMS ((rtx, rtx, rtx));
46 static void optimize_reg_copy_2 PARAMS ((rtx, rtx, rtx));
47 static void optimize_reg_copy_3 PARAMS ((rtx, rtx, rtx));
48 static rtx gen_add3_insn PARAMS ((rtx, rtx, rtx));
49 static void copy_src_to_dest PARAMS ((rtx, rtx, rtx, int));
50 static int *regmove_bb_head;
52 struct match {
53 int with[MAX_RECOG_OPERANDS];
54 enum { READ, WRITE, READWRITE } use[MAX_RECOG_OPERANDS];
55 int commutative[MAX_RECOG_OPERANDS];
56 int early_clobber[MAX_RECOG_OPERANDS];
59 static rtx discover_flags_reg PARAMS ((void));
60 static void mark_flags_life_zones PARAMS ((rtx));
61 static void flags_set_1 PARAMS ((rtx, rtx, void *));
63 static int try_auto_increment PARAMS ((rtx, rtx, rtx, rtx, HOST_WIDE_INT, int));
64 static int find_matches PARAMS ((rtx, struct match *));
65 static void replace_in_call_usage PARAMS ((rtx *, int, rtx, rtx));
66 static int fixup_match_1 PARAMS ((rtx, rtx, rtx, rtx, rtx, int, int, int, FILE *))
68 static int reg_is_remote_constant_p PARAMS ((rtx, rtx, rtx));
69 static int stable_and_no_regs_but_for_p PARAMS ((rtx, rtx, rtx));
70 static int regclass_compatible_p PARAMS ((int, int));
71 static int replacement_quality PARAMS ((rtx));
72 static int fixup_match_2 PARAMS ((rtx, rtx, rtx, rtx, FILE *));
74 /* Return non-zero if registers with CLASS1 and CLASS2 can be merged without
75 causing too much register allocation problems. */
76 static int
77 regclass_compatible_p (class0, class1)
78 int class0, class1;
80 return (class0 == class1
81 || (reg_class_subset_p (class0, class1)
82 && ! CLASS_LIKELY_SPILLED_P (class0))
83 || (reg_class_subset_p (class1, class0)
84 && ! CLASS_LIKELY_SPILLED_P (class1)));
87 /* Generate and return an insn body to add r1 and c,
88 storing the result in r0. */
89 static rtx
90 gen_add3_insn (r0, r1, c)
91 rtx r0, r1, c;
93 int icode = (int) add_optab->handlers[(int) GET_MODE (r0)].insn_code;
95 if (icode == CODE_FOR_nothing
96 || ! ((*insn_data[icode].operand[0].predicate)
97 (r0, insn_data[icode].operand[0].mode))
98 || ! ((*insn_data[icode].operand[1].predicate)
99 (r1, insn_data[icode].operand[1].mode))
100 || ! ((*insn_data[icode].operand[2].predicate)
101 (c, insn_data[icode].operand[2].mode)))
102 return NULL_RTX;
104 return (GEN_FCN (icode) (r0, r1, c));
108 /* INC_INSN is an instruction that adds INCREMENT to REG.
109 Try to fold INC_INSN as a post/pre in/decrement into INSN.
110 Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
111 Return nonzero for success. */
112 static int
113 try_auto_increment (insn, inc_insn, inc_insn_set, reg, increment, pre)
114 rtx reg, insn, inc_insn ,inc_insn_set;
115 HOST_WIDE_INT increment;
116 int pre;
118 enum rtx_code inc_code;
120 rtx pset = single_set (insn);
121 if (pset)
123 /* Can't use the size of SET_SRC, we might have something like
124 (sign_extend:SI (mem:QI ... */
125 rtx use = find_use_as_address (pset, reg, 0);
126 if (use != 0 && use != (rtx) 1)
128 int size = GET_MODE_SIZE (GET_MODE (use));
129 if (0
130 || (HAVE_POST_INCREMENT
131 && pre == 0 && (inc_code = POST_INC, increment == size))
132 || (HAVE_PRE_INCREMENT
133 && pre == 1 && (inc_code = PRE_INC, increment == size))
134 || (HAVE_POST_DECREMENT
135 && pre == 0 && (inc_code = POST_DEC, increment == -size))
136 || (HAVE_PRE_DECREMENT
137 && pre == 1 && (inc_code = PRE_DEC, increment == -size))
140 if (inc_insn_set)
141 validate_change
142 (inc_insn,
143 &SET_SRC (inc_insn_set),
144 XEXP (SET_SRC (inc_insn_set), 0), 1);
145 validate_change (insn, &XEXP (use, 0),
146 gen_rtx_fmt_e (inc_code, Pmode, reg), 1);
147 if (apply_change_group ())
149 /* If there is a REG_DEAD note on this insn, we must
150 change this not to REG_UNUSED meaning that the register
151 is set, but the value is dead. Failure to do so will
152 result in a sched1 abort -- when it recomputes lifetime
153 information, the number of REG_DEAD notes will have
154 changed. */
155 rtx note = find_reg_note (insn, REG_DEAD, reg);
156 if (note)
157 PUT_MODE (note, REG_UNUSED);
159 REG_NOTES (insn)
160 = gen_rtx_EXPR_LIST (REG_INC,
161 reg, REG_NOTES (insn));
162 if (! inc_insn_set)
164 PUT_CODE (inc_insn, NOTE);
165 NOTE_LINE_NUMBER (inc_insn) = NOTE_INSN_DELETED;
166 NOTE_SOURCE_FILE (inc_insn) = 0;
168 return 1;
173 return 0;
176 /* Determine if the pattern generated by add_optab has a clobber,
177 such as might be issued for a flags hard register. To make the
178 code elsewhere simpler, we handle cc0 in this same framework.
180 Return the register if one was discovered. Return NULL_RTX if
181 if no flags were found. Return pc_rtx if we got confused. */
183 static rtx
184 discover_flags_reg ()
186 rtx tmp;
187 tmp = gen_rtx_REG (word_mode, 10000);
188 tmp = gen_add3_insn (tmp, tmp, GEN_INT (2));
190 /* If we get something that isn't a simple set, or a
191 [(set ..) (clobber ..)], this whole function will go wrong. */
192 if (GET_CODE (tmp) == SET)
193 return NULL_RTX;
194 else if (GET_CODE (tmp) == PARALLEL)
196 int found;
198 if (XVECLEN (tmp, 0) != 2)
199 return pc_rtx;
200 tmp = XVECEXP (tmp, 0, 1);
201 if (GET_CODE (tmp) != CLOBBER)
202 return pc_rtx;
203 tmp = XEXP (tmp, 0);
205 /* Don't do anything foolish if the md wanted to clobber a
206 scratch or something. We only care about hard regs.
207 Moreover we don't like the notion of subregs of hard regs. */
208 if (GET_CODE (tmp) == SUBREG
209 && GET_CODE (SUBREG_REG (tmp)) == REG
210 && REGNO (SUBREG_REG (tmp)) < FIRST_PSEUDO_REGISTER)
211 return pc_rtx;
212 found = (GET_CODE (tmp) == REG && REGNO (tmp) < FIRST_PSEUDO_REGISTER);
214 return (found ? tmp : NULL_RTX);
217 return pc_rtx;
220 /* It is a tedious task identifying when the flags register is live and
221 when it is safe to optimize. Since we process the instruction stream
222 multiple times, locate and record these live zones by marking the
223 mode of the instructions --
225 QImode is used on the instruction at which the flags becomes live.
227 HImode is used within the range (exclusive) that the flags are
228 live. Thus the user of the flags is not marked.
230 All other instructions are cleared to VOIDmode. */
232 /* Used to communicate with flags_set_1. */
233 static rtx flags_set_1_rtx;
234 static int flags_set_1_set;
236 static void
237 mark_flags_life_zones (flags)
238 rtx flags;
240 int flags_regno;
241 int flags_nregs;
242 int block;
244 #ifdef HAVE_cc0
245 /* If we found a flags register on a cc0 host, bail. */
246 if (flags == NULL_RTX)
247 flags = cc0_rtx;
248 else if (flags != cc0_rtx)
249 flags = pc_rtx;
250 #endif
252 /* Simple cases first: if no flags, clear all modes. If confusing,
253 mark the entire function as being in a flags shadow. */
254 if (flags == NULL_RTX || flags == pc_rtx)
256 enum machine_mode mode = (flags ? HImode : VOIDmode);
257 rtx insn;
258 for (insn = get_insns(); insn; insn = NEXT_INSN (insn))
259 PUT_MODE (insn, mode);
260 return;
263 #ifdef HAVE_cc0
264 flags_regno = -1;
265 flags_nregs = 1;
266 #else
267 flags_regno = REGNO (flags);
268 flags_nregs = HARD_REGNO_NREGS (flags_regno, GET_MODE (flags));
269 #endif
270 flags_set_1_rtx = flags;
272 /* Process each basic block. */
273 for (block = n_basic_blocks - 1; block >= 0; block--)
275 rtx insn, end;
276 int live;
278 insn = BLOCK_HEAD (block);
279 end = BLOCK_END (block);
281 /* Look out for the (unlikely) case of flags being live across
282 basic block boundaries. */
283 live = 0;
284 #ifndef HAVE_cc0
286 int i;
287 for (i = 0; i < flags_nregs; ++i)
288 live |= REGNO_REG_SET_P (BASIC_BLOCK (block)->global_live_at_start,
289 flags_regno + i);
291 #endif
293 while (1)
295 /* Process liveness in reverse order of importance --
296 alive, death, birth. This lets more important info
297 overwrite the mode of lesser info. */
299 if (INSN_P (insn))
301 #ifdef HAVE_cc0
302 /* In the cc0 case, death is not marked in reg notes,
303 but is instead the mere use of cc0 when it is alive. */
304 if (live && reg_mentioned_p (cc0_rtx, PATTERN (insn)))
305 live = 0;
306 #else
307 /* In the hard reg case, we watch death notes. */
308 if (live && find_regno_note (insn, REG_DEAD, flags_regno))
309 live = 0;
310 #endif
311 PUT_MODE (insn, (live ? HImode : VOIDmode));
313 /* In either case, birth is denoted simply by it's presence
314 as the destination of a set. */
315 flags_set_1_set = 0;
316 note_stores (PATTERN (insn), flags_set_1, NULL);
317 if (flags_set_1_set)
319 live = 1;
320 PUT_MODE (insn, QImode);
323 else
324 PUT_MODE (insn, (live ? HImode : VOIDmode));
326 if (insn == end)
327 break;
328 insn = NEXT_INSN (insn);
333 /* A subroutine of mark_flags_life_zones, called through note_stores. */
335 static void
336 flags_set_1 (x, pat, data)
337 rtx x, pat;
338 void *data ATTRIBUTE_UNUSED;
340 if (GET_CODE (pat) == SET
341 && reg_overlap_mentioned_p (x, flags_set_1_rtx))
342 flags_set_1_set = 1;
345 static int *regno_src_regno;
347 /* Indicate how good a choice REG (which appears as a source) is to replace
348 a destination register with. The higher the returned value, the better
349 the choice. The main objective is to avoid using a register that is
350 a candidate for tying to a hard register, since the output might in
351 turn be a candidate to be tied to a different hard register. */
352 static int
353 replacement_quality(reg)
354 rtx reg;
356 int src_regno;
358 /* Bad if this isn't a register at all. */
359 if (GET_CODE (reg) != REG)
360 return 0;
362 /* If this register is not meant to get a hard register,
363 it is a poor choice. */
364 if (REG_LIVE_LENGTH (REGNO (reg)) < 0)
365 return 0;
367 src_regno = regno_src_regno[REGNO (reg)];
369 /* If it was not copied from another register, it is fine. */
370 if (src_regno < 0)
371 return 3;
373 /* Copied from a hard register? */
374 if (src_regno < FIRST_PSEUDO_REGISTER)
375 return 1;
377 /* Copied from a pseudo register - not as bad as from a hard register,
378 yet still cumbersome, since the register live length will be lengthened
379 when the registers get tied. */
380 return 2;
383 /* Return 1 if INSN might end a basic block. */
385 static int perhaps_ends_bb_p (insn)
386 rtx insn;
388 switch (GET_CODE (insn))
390 case CODE_LABEL:
391 case JUMP_INSN:
392 /* These always end a basic block. */
393 return 1;
395 case CALL_INSN:
396 /* A CALL_INSN might be the last insn of a basic block, if it is inside
397 an EH region or if there are nonlocal gotos. Note that this test is
398 very conservative. */
399 return flag_exceptions || nonlocal_goto_handler_labels;
401 default:
402 /* All others never end a basic block. */
403 return 0;
407 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
408 in INSN.
410 Search forward to see if SRC dies before either it or DEST is modified,
411 but don't scan past the end of a basic block. If so, we can replace SRC
412 with DEST and let SRC die in INSN.
414 This will reduce the number of registers live in that range and may enable
415 DEST to be tied to SRC, thus often saving one register in addition to a
416 register-register copy. */
418 static int
419 optimize_reg_copy_1 (insn, dest, src)
420 rtx insn;
421 rtx dest;
422 rtx src;
424 rtx p, q;
425 rtx note;
426 rtx dest_death = 0;
427 int sregno = REGNO (src);
428 int dregno = REGNO (dest);
430 /* We don't want to mess with hard regs if register classes are small. */
431 if (sregno == dregno
432 || (SMALL_REGISTER_CLASSES
433 && (sregno < FIRST_PSEUDO_REGISTER
434 || dregno < FIRST_PSEUDO_REGISTER))
435 /* We don't see all updates to SP if they are in an auto-inc memory
436 reference, so we must disallow this optimization on them. */
437 || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
438 return 0;
440 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
442 /* ??? We can't scan past the end of a basic block without updating
443 the register lifetime info (REG_DEAD/basic_block_live_at_start). */
444 if (perhaps_ends_bb_p (p))
445 break;
446 else if (! INSN_P (p))
447 continue;
449 if (reg_set_p (src, p) || reg_set_p (dest, p)
450 /* Don't change a USE of a register. */
451 || (GET_CODE (PATTERN (p)) == USE
452 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
453 break;
455 /* See if all of SRC dies in P. This test is slightly more
456 conservative than it needs to be. */
457 if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
458 && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
460 int failed = 0;
461 int d_length = 0;
462 int s_length = 0;
463 int d_n_calls = 0;
464 int s_n_calls = 0;
466 /* We can do the optimization. Scan forward from INSN again,
467 replacing regs as we go. Set FAILED if a replacement can't
468 be done. In that case, we can't move the death note for SRC.
469 This should be rare. */
471 /* Set to stop at next insn. */
472 for (q = next_real_insn (insn);
473 q != next_real_insn (p);
474 q = next_real_insn (q))
476 if (reg_overlap_mentioned_p (src, PATTERN (q)))
478 /* If SRC is a hard register, we might miss some
479 overlapping registers with validate_replace_rtx,
480 so we would have to undo it. We can't if DEST is
481 present in the insn, so fail in that combination
482 of cases. */
483 if (sregno < FIRST_PSEUDO_REGISTER
484 && reg_mentioned_p (dest, PATTERN (q)))
485 failed = 1;
487 /* Replace all uses and make sure that the register
488 isn't still present. */
489 else if (validate_replace_rtx (src, dest, q)
490 && (sregno >= FIRST_PSEUDO_REGISTER
491 || ! reg_overlap_mentioned_p (src,
492 PATTERN (q))))
494 else
496 validate_replace_rtx (dest, src, q);
497 failed = 1;
501 /* For SREGNO, count the total number of insns scanned.
502 For DREGNO, count the total number of insns scanned after
503 passing the death note for DREGNO. */
504 s_length++;
505 if (dest_death)
506 d_length++;
508 /* If the insn in which SRC dies is a CALL_INSN, don't count it
509 as a call that has been crossed. Otherwise, count it. */
510 if (q != p && GET_CODE (q) == CALL_INSN)
512 /* Similarly, total calls for SREGNO, total calls beyond
513 the death note for DREGNO. */
514 s_n_calls++;
515 if (dest_death)
516 d_n_calls++;
519 /* If DEST dies here, remove the death note and save it for
520 later. Make sure ALL of DEST dies here; again, this is
521 overly conservative. */
522 if (dest_death == 0
523 && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0)
525 if (GET_MODE (XEXP (dest_death, 0)) != GET_MODE (dest))
526 failed = 1, dest_death = 0;
527 else
528 remove_note (q, dest_death);
532 if (! failed)
534 /* These counters need to be updated if and only if we are
535 going to move the REG_DEAD note. */
536 if (sregno >= FIRST_PSEUDO_REGISTER)
538 if (REG_LIVE_LENGTH (sregno) >= 0)
540 REG_LIVE_LENGTH (sregno) -= s_length;
541 /* REG_LIVE_LENGTH is only an approximation after
542 combine if sched is not run, so make sure that we
543 still have a reasonable value. */
544 if (REG_LIVE_LENGTH (sregno) < 2)
545 REG_LIVE_LENGTH (sregno) = 2;
548 REG_N_CALLS_CROSSED (sregno) -= s_n_calls;
551 /* Move death note of SRC from P to INSN. */
552 remove_note (p, note);
553 XEXP (note, 1) = REG_NOTES (insn);
554 REG_NOTES (insn) = note;
557 /* DEST is also dead if INSN has a REG_UNUSED note for DEST. */
558 if (! dest_death
559 && (dest_death = find_regno_note (insn, REG_UNUSED, dregno)))
561 PUT_REG_NOTE_KIND (dest_death, REG_DEAD);
562 remove_note (insn, dest_death);
565 /* Put death note of DEST on P if we saw it die. */
566 if (dest_death)
568 XEXP (dest_death, 1) = REG_NOTES (p);
569 REG_NOTES (p) = dest_death;
571 if (dregno >= FIRST_PSEUDO_REGISTER)
573 /* If and only if we are moving the death note for DREGNO,
574 then we need to update its counters. */
575 if (REG_LIVE_LENGTH (dregno) >= 0)
576 REG_LIVE_LENGTH (dregno) += d_length;
577 REG_N_CALLS_CROSSED (dregno) += d_n_calls;
581 return ! failed;
584 /* If SRC is a hard register which is set or killed in some other
585 way, we can't do this optimization. */
586 else if (sregno < FIRST_PSEUDO_REGISTER
587 && dead_or_set_p (p, src))
588 break;
590 return 0;
593 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
594 a sequence of insns that modify DEST followed by an insn that sets
595 SRC to DEST in which DEST dies, with no prior modification of DEST.
596 (There is no need to check if the insns in between actually modify
597 DEST. We should not have cases where DEST is not modified, but
598 the optimization is safe if no such modification is detected.)
599 In that case, we can replace all uses of DEST, starting with INSN and
600 ending with the set of SRC to DEST, with SRC. We do not do this
601 optimization if a CALL_INSN is crossed unless SRC already crosses a
602 call or if DEST dies before the copy back to SRC.
604 It is assumed that DEST and SRC are pseudos; it is too complicated to do
605 this for hard registers since the substitutions we may make might fail. */
607 static void
608 optimize_reg_copy_2 (insn, dest, src)
609 rtx insn;
610 rtx dest;
611 rtx src;
613 rtx p, q;
614 rtx set;
615 int sregno = REGNO (src);
616 int dregno = REGNO (dest);
618 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
620 /* ??? We can't scan past the end of a basic block without updating
621 the register lifetime info (REG_DEAD/basic_block_live_at_start). */
622 if (perhaps_ends_bb_p (p))
623 break;
624 else if (! INSN_P (p))
625 continue;
627 set = single_set (p);
628 if (set && SET_SRC (set) == dest && SET_DEST (set) == src
629 && find_reg_note (p, REG_DEAD, dest))
631 /* We can do the optimization. Scan forward from INSN again,
632 replacing regs as we go. */
634 /* Set to stop at next insn. */
635 for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
636 if (INSN_P (q))
638 if (reg_mentioned_p (dest, PATTERN (q)))
639 PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
642 if (GET_CODE (q) == CALL_INSN)
644 REG_N_CALLS_CROSSED (dregno)--;
645 REG_N_CALLS_CROSSED (sregno)++;
649 remove_note (p, find_reg_note (p, REG_DEAD, dest));
650 REG_N_DEATHS (dregno)--;
651 remove_note (insn, find_reg_note (insn, REG_DEAD, src));
652 REG_N_DEATHS (sregno)--;
653 return;
656 if (reg_set_p (src, p)
657 || find_reg_note (p, REG_DEAD, dest)
658 || (GET_CODE (p) == CALL_INSN && REG_N_CALLS_CROSSED (sregno) == 0))
659 break;
662 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
663 Look if SRC dies there, and if it is only set once, by loading
664 it from memory. If so, try to encorporate the zero/sign extension
665 into the memory read, change SRC to the mode of DEST, and alter
666 the remaining accesses to use the appropriate SUBREG. This allows
667 SRC and DEST to be tied later. */
668 static void
669 optimize_reg_copy_3 (insn, dest, src)
670 rtx insn;
671 rtx dest;
672 rtx src;
674 rtx src_reg = XEXP (src, 0);
675 int src_no = REGNO (src_reg);
676 int dst_no = REGNO (dest);
677 rtx p, set, subreg;
678 enum machine_mode old_mode;
680 if (src_no < FIRST_PSEUDO_REGISTER
681 || dst_no < FIRST_PSEUDO_REGISTER
682 || ! find_reg_note (insn, REG_DEAD, src_reg)
683 || REG_N_SETS (src_no) != 1)
684 return;
685 for (p = PREV_INSN (insn); p && ! reg_set_p (src_reg, p); p = PREV_INSN (p))
686 /* ??? We can't scan past the end of a basic block without updating
687 the register lifetime info (REG_DEAD/basic_block_live_at_start). */
688 if (perhaps_ends_bb_p (p))
689 break;
691 if (! p)
692 return;
694 if (! (set = single_set (p))
695 || GET_CODE (SET_SRC (set)) != MEM
696 || SET_DEST (set) != src_reg)
697 return;
699 /* Be conserative: although this optimization is also valid for
700 volatile memory references, that could cause trouble in later passes. */
701 if (MEM_VOLATILE_P (SET_SRC (set)))
702 return;
704 /* Do not use a SUBREG to truncate from one mode to another if truncation
705 is not a nop. */
706 if (GET_MODE_BITSIZE (GET_MODE (src_reg)) <= GET_MODE_BITSIZE (GET_MODE (src))
707 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (src)),
708 GET_MODE_BITSIZE (GET_MODE (src_reg))))
709 return;
711 old_mode = GET_MODE (src_reg);
712 PUT_MODE (src_reg, GET_MODE (src));
713 XEXP (src, 0) = SET_SRC (set);
715 /* Include this change in the group so that it's easily undone if
716 one of the changes in the group is invalid. */
717 validate_change (p, &SET_SRC (set), src, 1);
719 /* Now walk forward making additional replacements. We want to be able
720 to undo all the changes if a later substitution fails. */
721 subreg = gen_rtx_SUBREG (old_mode, src_reg, 0);
722 while (p = NEXT_INSN (p), p != insn)
724 if (! INSN_P (p))
725 continue;
727 /* Make a tenative change. */
728 validate_replace_rtx_group (src_reg, subreg, p);
731 validate_replace_rtx_group (src, src_reg, insn);
733 /* Now see if all the changes are valid. */
734 if (! apply_change_group ())
736 /* One or more changes were no good. Back out everything. */
737 PUT_MODE (src_reg, old_mode);
738 XEXP (src, 0) = src_reg;
743 /* If we were not able to update the users of src to use dest directly, try
744 instead moving the value to dest directly before the operation. */
746 static void
747 copy_src_to_dest (insn, src, dest, old_max_uid)
748 rtx insn;
749 rtx src;
750 rtx dest;
751 int old_max_uid;
753 rtx seq;
754 rtx link;
755 rtx next;
756 rtx set;
757 rtx move_insn;
758 rtx *p_insn_notes;
759 rtx *p_move_notes;
760 int src_regno;
761 int dest_regno;
762 int bb;
763 int insn_uid;
764 int move_uid;
766 /* A REG_LIVE_LENGTH of -1 indicates the register is equivalent to a constant
767 or memory location and is used infrequently; a REG_LIVE_LENGTH of -2 is
768 parameter when there is no frame pointer that is not allocated a register.
769 For now, we just reject them, rather than incrementing the live length. */
771 if (GET_CODE (src) == REG
772 && REG_LIVE_LENGTH (REGNO (src)) > 0
773 && GET_CODE (dest) == REG
774 && !RTX_UNCHANGING_P (dest)
775 && REG_LIVE_LENGTH (REGNO (dest)) > 0
776 && (set = single_set (insn)) != NULL_RTX
777 && !reg_mentioned_p (dest, SET_SRC (set))
778 && GET_MODE (src) == GET_MODE (dest))
780 int old_num_regs = reg_rtx_no;
782 /* Generate the src->dest move. */
783 start_sequence ();
784 emit_move_insn (dest, src);
785 seq = gen_sequence ();
786 end_sequence ();
787 /* If this sequence uses new registers, we may not use it. */
788 if (old_num_regs != reg_rtx_no
789 || ! validate_replace_rtx (src, dest, insn))
791 /* We have to restore reg_rtx_no to its old value, lest
792 recompute_reg_usage will try to compute the usage of the
793 new regs, yet reg_n_info is not valid for them. */
794 reg_rtx_no = old_num_regs;
795 return;
797 emit_insn_before (seq, insn);
798 move_insn = PREV_INSN (insn);
799 p_move_notes = &REG_NOTES (move_insn);
800 p_insn_notes = &REG_NOTES (insn);
802 /* Move any notes mentioning src to the move instruction */
803 for (link = REG_NOTES (insn); link != NULL_RTX; link = next)
805 next = XEXP (link, 1);
806 if (XEXP (link, 0) == src)
808 *p_move_notes = link;
809 p_move_notes = &XEXP (link, 1);
811 else
813 *p_insn_notes = link;
814 p_insn_notes = &XEXP (link, 1);
818 *p_move_notes = NULL_RTX;
819 *p_insn_notes = NULL_RTX;
821 /* Is the insn the head of a basic block? If so extend it */
822 insn_uid = INSN_UID (insn);
823 move_uid = INSN_UID (move_insn);
824 if (insn_uid < old_max_uid)
826 bb = regmove_bb_head[insn_uid];
827 if (bb >= 0)
829 BLOCK_HEAD (bb) = move_insn;
830 regmove_bb_head[insn_uid] = -1;
834 /* Update the various register tables. */
835 dest_regno = REGNO (dest);
836 REG_N_SETS (dest_regno) ++;
837 REG_LIVE_LENGTH (dest_regno)++;
838 if (REGNO_FIRST_UID (dest_regno) == insn_uid)
839 REGNO_FIRST_UID (dest_regno) = move_uid;
841 src_regno = REGNO (src);
842 if (! find_reg_note (move_insn, REG_DEAD, src))
843 REG_LIVE_LENGTH (src_regno)++;
845 if (REGNO_FIRST_UID (src_regno) == insn_uid)
846 REGNO_FIRST_UID (src_regno) = move_uid;
848 if (REGNO_LAST_UID (src_regno) == insn_uid)
849 REGNO_LAST_UID (src_regno) = move_uid;
851 if (REGNO_LAST_NOTE_UID (src_regno) == insn_uid)
852 REGNO_LAST_NOTE_UID (src_regno) = move_uid;
857 /* Return whether REG is set in only one location, and is set to a
858 constant, but is set in a different basic block from INSN (an
859 instructions which uses REG). In this case REG is equivalent to a
860 constant, and we don't want to break that equivalence, because that
861 may increase register pressure and make reload harder. If REG is
862 set in the same basic block as INSN, we don't worry about it,
863 because we'll probably need a register anyhow (??? but what if REG
864 is used in a different basic block as well as this one?). FIRST is
865 the first insn in the function. */
867 static int
868 reg_is_remote_constant_p (reg, insn, first)
869 rtx reg;
870 rtx insn;
871 rtx first;
873 register rtx p;
875 if (REG_N_SETS (REGNO (reg)) != 1)
876 return 0;
878 /* Look for the set. */
879 for (p = LOG_LINKS (insn); p; p = XEXP (p, 1))
881 rtx s;
883 if (REG_NOTE_KIND (p) != 0)
884 continue;
885 s = single_set (XEXP (p, 0));
886 if (s != 0
887 && GET_CODE (SET_DEST (s)) == REG
888 && REGNO (SET_DEST (s)) == REGNO (reg))
890 /* The register is set in the same basic block. */
891 return 0;
895 for (p = first; p && p != insn; p = NEXT_INSN (p))
897 rtx s;
899 if (! INSN_P (p))
900 continue;
901 s = single_set (p);
902 if (s != 0
903 && GET_CODE (SET_DEST (s)) == REG
904 && REGNO (SET_DEST (s)) == REGNO (reg))
906 /* This is the instruction which sets REG. If there is a
907 REG_EQUAL note, then REG is equivalent to a constant. */
908 if (find_reg_note (p, REG_EQUAL, NULL_RTX))
909 return 1;
910 return 0;
914 return 0;
917 /* INSN is adding a CONST_INT to a REG. We search backwards looking for
918 another add immediate instruction with the same source and dest registers,
919 and if we find one, we change INSN to an increment, and return 1. If
920 no changes are made, we return 0.
922 This changes
923 (set (reg100) (plus reg1 offset1))
925 (set (reg100) (plus reg1 offset2))
927 (set (reg100) (plus reg1 offset1))
929 (set (reg100) (plus reg100 offset2-offset1)) */
931 /* ??? What does this comment mean? */
932 /* cse disrupts preincrement / postdecrement squences when it finds a
933 hard register as ultimate source, like the frame pointer. */
935 static int
936 fixup_match_2 (insn, dst, src, offset, regmove_dump_file)
937 rtx insn, dst, src, offset;
938 FILE *regmove_dump_file;
940 rtx p, dst_death = 0;
941 int length, num_calls = 0;
943 /* If SRC dies in INSN, we'd have to move the death note. This is
944 considered to be very unlikely, so we just skip the optimization
945 in this case. */
946 if (find_regno_note (insn, REG_DEAD, REGNO (src)))
947 return 0;
949 /* Scan backward to find the first instruction that sets DST. */
951 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
953 rtx pset;
955 /* ??? We can't scan past the end of a basic block without updating
956 the register lifetime info (REG_DEAD/basic_block_live_at_start). */
957 if (perhaps_ends_bb_p (p))
958 break;
959 else if (! INSN_P (p))
960 continue;
962 if (find_regno_note (p, REG_DEAD, REGNO (dst)))
963 dst_death = p;
964 if (! dst_death)
965 length++;
967 pset = single_set (p);
968 if (pset && SET_DEST (pset) == dst
969 && GET_CODE (SET_SRC (pset)) == PLUS
970 && XEXP (SET_SRC (pset), 0) == src
971 && GET_CODE (XEXP (SET_SRC (pset), 1)) == CONST_INT)
973 HOST_WIDE_INT newconst
974 = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
975 rtx add = gen_add3_insn (dst, dst, GEN_INT (newconst));
977 if (add && validate_change (insn, &PATTERN (insn), add, 0))
979 /* Remove the death note for DST from DST_DEATH. */
980 if (dst_death)
982 remove_death (REGNO (dst), dst_death);
983 REG_LIVE_LENGTH (REGNO (dst)) += length;
984 REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
987 if (regmove_dump_file)
988 fprintf (regmove_dump_file,
989 "Fixed operand of insn %d.\n",
990 INSN_UID (insn));
992 #ifdef AUTO_INC_DEC
993 for (p = PREV_INSN (insn); p; p = PREV_INSN (p))
995 if (GET_CODE (p) == CODE_LABEL
996 || GET_CODE (p) == JUMP_INSN)
997 break;
998 if (! INSN_P (p))
999 continue;
1000 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1002 if (try_auto_increment (p, insn, 0, dst, newconst, 0))
1003 return 1;
1004 break;
1007 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
1009 if (GET_CODE (p) == CODE_LABEL
1010 || GET_CODE (p) == JUMP_INSN)
1011 break;
1012 if (! INSN_P (p))
1013 continue;
1014 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1016 try_auto_increment (p, insn, 0, dst, newconst, 1);
1017 break;
1020 #endif
1021 return 1;
1025 if (reg_set_p (dst, PATTERN (p)))
1026 break;
1028 /* If we have passed a call instruction, and the
1029 pseudo-reg SRC is not already live across a call,
1030 then don't perform the optimization. */
1031 /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
1032 hard regs are clobbered. Thus, we only use it for src for
1033 non-call insns. */
1034 if (GET_CODE (p) == CALL_INSN)
1036 if (! dst_death)
1037 num_calls++;
1039 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1040 break;
1042 if (call_used_regs [REGNO (dst)]
1043 || find_reg_fusage (p, CLOBBER, dst))
1044 break;
1046 else if (reg_set_p (src, PATTERN (p)))
1047 break;
1050 return 0;
1053 void
1054 regmove_optimize (f, nregs, regmove_dump_file)
1055 rtx f;
1056 int nregs;
1057 FILE *regmove_dump_file;
1059 int old_max_uid = get_max_uid ();
1060 rtx insn;
1061 struct match match;
1062 int pass;
1063 int i;
1064 rtx copy_src, copy_dst;
1066 /* Find out where a potential flags register is live, and so that we
1067 can supress some optimizations in those zones. */
1068 mark_flags_life_zones (discover_flags_reg ());
1070 regno_src_regno = (int *) xmalloc (sizeof *regno_src_regno * nregs);
1071 for (i = nregs; --i >= 0; ) regno_src_regno[i] = -1;
1073 regmove_bb_head = (int *) xmalloc (sizeof (int) * (old_max_uid + 1));
1074 for (i = old_max_uid; i >= 0; i--) regmove_bb_head[i] = -1;
1075 for (i = 0; i < n_basic_blocks; i++)
1076 regmove_bb_head[INSN_UID (BLOCK_HEAD (i))] = i;
1078 /* A forward/backward pass. Replace output operands with input operands. */
1080 for (pass = 0; pass <= 2; pass++)
1082 if (! flag_regmove && pass >= flag_expensive_optimizations)
1083 goto done;
1085 if (regmove_dump_file)
1086 fprintf (regmove_dump_file, "Starting %s pass...\n",
1087 pass ? "backward" : "forward");
1089 for (insn = pass ? get_last_insn () : f; insn;
1090 insn = pass ? PREV_INSN (insn) : NEXT_INSN (insn))
1092 rtx set;
1093 int op_no, match_no;
1095 set = single_set (insn);
1096 if (! set)
1097 continue;
1099 if (flag_expensive_optimizations && ! pass
1100 && (GET_CODE (SET_SRC (set)) == SIGN_EXTEND
1101 || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
1102 && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
1103 && GET_CODE (SET_DEST(set)) == REG)
1104 optimize_reg_copy_3 (insn, SET_DEST (set), SET_SRC (set));
1106 if (flag_expensive_optimizations && ! pass
1107 && GET_CODE (SET_SRC (set)) == REG
1108 && GET_CODE (SET_DEST(set)) == REG)
1110 /* If this is a register-register copy where SRC is not dead,
1111 see if we can optimize it. If this optimization succeeds,
1112 it will become a copy where SRC is dead. */
1113 if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
1114 || optimize_reg_copy_1 (insn, SET_DEST (set), SET_SRC (set)))
1115 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
1117 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
1118 if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1119 optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
1120 if (regno_src_regno[REGNO (SET_DEST (set))] < 0
1121 && SET_SRC (set) != SET_DEST (set))
1123 int srcregno = REGNO (SET_SRC(set));
1124 if (regno_src_regno[srcregno] >= 0)
1125 srcregno = regno_src_regno[srcregno];
1126 regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
1130 if (! flag_regmove)
1131 continue;
1133 if (! find_matches (insn, &match))
1134 continue;
1136 /* Now scan through the operands looking for a source operand
1137 which is supposed to match the destination operand.
1138 Then scan forward for an instruction which uses the dest
1139 operand.
1140 If it dies there, then replace the dest in both operands with
1141 the source operand. */
1143 for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1145 rtx src, dst, src_subreg;
1146 enum reg_class src_class, dst_class;
1148 match_no = match.with[op_no];
1150 /* Nothing to do if the two operands aren't supposed to match. */
1151 if (match_no < 0)
1152 continue;
1154 src = recog_data.operand[op_no];
1155 dst = recog_data.operand[match_no];
1157 if (GET_CODE (src) != REG)
1158 continue;
1160 src_subreg = src;
1161 if (GET_CODE (dst) == SUBREG
1162 && GET_MODE_SIZE (GET_MODE (dst))
1163 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dst))))
1165 src_subreg
1166 = gen_rtx_SUBREG (GET_MODE (SUBREG_REG (dst)),
1167 src, SUBREG_WORD (dst));
1168 dst = SUBREG_REG (dst);
1170 if (GET_CODE (dst) != REG
1171 || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1172 continue;
1174 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1176 if (match.commutative[op_no] < op_no)
1177 regno_src_regno[REGNO (dst)] = REGNO (src);
1178 continue;
1181 if (REG_LIVE_LENGTH (REGNO (src)) < 0)
1182 continue;
1184 /* op_no/src must be a read-only operand, and
1185 match_operand/dst must be a write-only operand. */
1186 if (match.use[op_no] != READ
1187 || match.use[match_no] != WRITE)
1188 continue;
1190 if (match.early_clobber[match_no]
1191 && count_occurrences (PATTERN (insn), src, 0) > 1)
1192 continue;
1194 /* Make sure match_operand is the destination. */
1195 if (recog_data.operand[match_no] != SET_DEST (set))
1196 continue;
1198 /* If the operands already match, then there is nothing to do. */
1199 if (operands_match_p (src, dst))
1200 continue;
1202 /* But in the commutative case, we might find a better match. */
1203 if (match.commutative[op_no] >= 0)
1205 rtx comm = recog_data.operand[match.commutative[op_no]];
1206 if (operands_match_p (comm, dst)
1207 && (replacement_quality (comm)
1208 >= replacement_quality (src)))
1209 continue;
1212 src_class = reg_preferred_class (REGNO (src));
1213 dst_class = reg_preferred_class (REGNO (dst));
1214 if (! regclass_compatible_p (src_class, dst_class))
1215 continue;
1217 if (fixup_match_1 (insn, set, src, src_subreg, dst, pass,
1218 op_no, match_no,
1219 regmove_dump_file))
1220 break;
1225 /* A backward pass. Replace input operands with output operands. */
1227 if (regmove_dump_file)
1228 fprintf (regmove_dump_file, "Starting backward pass...\n");
1230 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1232 if (INSN_P (insn))
1234 int op_no, match_no;
1235 int success = 0;
1237 if (! find_matches (insn, &match))
1238 continue;
1240 /* Now scan through the operands looking for a destination operand
1241 which is supposed to match a source operand.
1242 Then scan backward for an instruction which sets the source
1243 operand. If safe, then replace the source operand with the
1244 dest operand in both instructions. */
1246 copy_src = NULL_RTX;
1247 copy_dst = NULL_RTX;
1248 for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1250 rtx set, p, src, dst;
1251 rtx src_note, dst_note;
1252 int num_calls = 0;
1253 enum reg_class src_class, dst_class;
1254 int length;
1256 match_no = match.with[op_no];
1258 /* Nothing to do if the two operands aren't supposed to match. */
1259 if (match_no < 0)
1260 continue;
1262 dst = recog_data.operand[match_no];
1263 src = recog_data.operand[op_no];
1265 if (GET_CODE (src) != REG)
1266 continue;
1268 if (GET_CODE (dst) != REG
1269 || REGNO (dst) < FIRST_PSEUDO_REGISTER
1270 || REG_LIVE_LENGTH (REGNO (dst)) < 0)
1271 continue;
1273 /* If the operands already match, then there is nothing to do. */
1274 if (operands_match_p (src, dst))
1275 continue;
1277 if (match.commutative[op_no] >= 0)
1279 rtx comm = recog_data.operand[match.commutative[op_no]];
1280 if (operands_match_p (comm, dst))
1281 continue;
1284 set = single_set (insn);
1285 if (! set)
1286 continue;
1288 /* match_no/dst must be a write-only operand, and
1289 operand_operand/src must be a read-only operand. */
1290 if (match.use[op_no] != READ
1291 || match.use[match_no] != WRITE)
1292 continue;
1294 if (match.early_clobber[match_no]
1295 && count_occurrences (PATTERN (insn), src, 0) > 1)
1296 continue;
1298 /* Make sure match_no is the destination. */
1299 if (recog_data.operand[match_no] != SET_DEST (set))
1300 continue;
1302 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1304 if (GET_CODE (SET_SRC (set)) == PLUS
1305 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
1306 && XEXP (SET_SRC (set), 0) == src
1307 && fixup_match_2 (insn, dst, src,
1308 XEXP (SET_SRC (set), 1),
1309 regmove_dump_file))
1310 break;
1311 continue;
1313 src_class = reg_preferred_class (REGNO (src));
1314 dst_class = reg_preferred_class (REGNO (dst));
1315 if (! regclass_compatible_p (src_class, dst_class))
1317 if (!copy_src)
1319 copy_src = src;
1320 copy_dst = dst;
1322 continue;
1325 /* Can not modify an earlier insn to set dst if this insn
1326 uses an old value in the source. */
1327 if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
1329 if (!copy_src)
1331 copy_src = src;
1332 copy_dst = dst;
1334 continue;
1337 if (! (src_note = find_reg_note (insn, REG_DEAD, src)))
1339 if (!copy_src)
1341 copy_src = src;
1342 copy_dst = dst;
1344 continue;
1348 /* If src is set once in a different basic block,
1349 and is set equal to a constant, then do not use
1350 it for this optimization, as this would make it
1351 no longer equivalent to a constant. */
1353 if (reg_is_remote_constant_p (src, insn, f))
1355 if (!copy_src)
1357 copy_src = src;
1358 copy_dst = dst;
1360 continue;
1364 if (regmove_dump_file)
1365 fprintf (regmove_dump_file,
1366 "Could fix operand %d of insn %d matching operand %d.\n",
1367 op_no, INSN_UID (insn), match_no);
1369 /* Scan backward to find the first instruction that uses
1370 the input operand. If the operand is set here, then
1371 replace it in both instructions with match_no. */
1373 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
1375 rtx pset;
1377 /* ??? We can't scan past the end of a basic block without
1378 updating the register lifetime info
1379 (REG_DEAD/basic_block_live_at_start). */
1380 if (perhaps_ends_bb_p (p))
1381 break;
1382 else if (! INSN_P (p))
1383 continue;
1385 length++;
1387 /* ??? See if all of SRC is set in P. This test is much
1388 more conservative than it needs to be. */
1389 pset = single_set (p);
1390 if (pset && SET_DEST (pset) == src)
1392 /* We use validate_replace_rtx, in case there
1393 are multiple identical source operands. All of
1394 them have to be changed at the same time. */
1395 if (validate_replace_rtx (src, dst, insn))
1397 if (validate_change (p, &SET_DEST (pset),
1398 dst, 0))
1399 success = 1;
1400 else
1402 /* Change all source operands back.
1403 This modifies the dst as a side-effect. */
1404 validate_replace_rtx (dst, src, insn);
1405 /* Now make sure the dst is right. */
1406 validate_change (insn,
1407 recog_data.operand_loc[match_no],
1408 dst, 0);
1411 break;
1414 if (reg_overlap_mentioned_p (src, PATTERN (p))
1415 || reg_overlap_mentioned_p (dst, PATTERN (p)))
1416 break;
1418 /* If we have passed a call instruction, and the
1419 pseudo-reg DST is not already live across a call,
1420 then don't perform the optimization. */
1421 if (GET_CODE (p) == CALL_INSN)
1423 num_calls++;
1425 if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
1426 break;
1430 if (success)
1432 int dstno, srcno;
1434 /* Remove the death note for SRC from INSN. */
1435 remove_note (insn, src_note);
1436 /* Move the death note for SRC to P if it is used
1437 there. */
1438 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1440 XEXP (src_note, 1) = REG_NOTES (p);
1441 REG_NOTES (p) = src_note;
1443 /* If there is a REG_DEAD note for DST on P, then remove
1444 it, because DST is now set there. */
1445 if ((dst_note = find_reg_note (p, REG_DEAD, dst)))
1446 remove_note (p, dst_note);
1448 dstno = REGNO (dst);
1449 srcno = REGNO (src);
1451 REG_N_SETS (dstno)++;
1452 REG_N_SETS (srcno)--;
1454 REG_N_CALLS_CROSSED (dstno) += num_calls;
1455 REG_N_CALLS_CROSSED (srcno) -= num_calls;
1457 REG_LIVE_LENGTH (dstno) += length;
1458 if (REG_LIVE_LENGTH (srcno) >= 0)
1460 REG_LIVE_LENGTH (srcno) -= length;
1461 /* REG_LIVE_LENGTH is only an approximation after
1462 combine if sched is not run, so make sure that we
1463 still have a reasonable value. */
1464 if (REG_LIVE_LENGTH (srcno) < 2)
1465 REG_LIVE_LENGTH (srcno) = 2;
1468 if (regmove_dump_file)
1469 fprintf (regmove_dump_file,
1470 "Fixed operand %d of insn %d matching operand %d.\n",
1471 op_no, INSN_UID (insn), match_no);
1473 break;
1477 /* If we weren't able to replace any of the alternatives, try an
1478 alternative appoach of copying the source to the destination. */
1479 if (!success && copy_src != NULL_RTX)
1480 copy_src_to_dest (insn, copy_src, copy_dst, old_max_uid);
1485 /* In fixup_match_1, some insns may have been inserted after basic block
1486 ends. Fix that here. */
1487 for (i = 0; i < n_basic_blocks; i++)
1489 rtx end = BLOCK_END (i);
1490 rtx new = end;
1491 rtx next = NEXT_INSN (new);
1492 while (next != 0 && INSN_UID (next) >= old_max_uid
1493 && (i == n_basic_blocks - 1 || BLOCK_HEAD (i + 1) != next))
1494 new = next, next = NEXT_INSN (new);
1495 BLOCK_END (i) = new;
1498 done:
1499 /* Clean up. */
1500 free (regno_src_regno);
1501 free (regmove_bb_head);
1504 /* Returns nonzero if INSN's pattern has matching constraints for any operand.
1505 Returns 0 if INSN can't be recognized, or if the alternative can't be
1506 determined.
1508 Initialize the info in MATCHP based on the constraints. */
1510 static int
1511 find_matches (insn, matchp)
1512 rtx insn;
1513 struct match *matchp;
1515 int likely_spilled[MAX_RECOG_OPERANDS];
1516 int op_no;
1517 int any_matches = 0;
1519 extract_insn (insn);
1520 if (! constrain_operands (0))
1521 return 0;
1523 /* Must initialize this before main loop, because the code for
1524 the commutative case may set matches for operands other than
1525 the current one. */
1526 for (op_no = recog_data.n_operands; --op_no >= 0; )
1527 matchp->with[op_no] = matchp->commutative[op_no] = -1;
1529 for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1531 const char *p;
1532 char c;
1533 int i = 0;
1535 p = recog_data.constraints[op_no];
1537 likely_spilled[op_no] = 0;
1538 matchp->use[op_no] = READ;
1539 matchp->early_clobber[op_no] = 0;
1540 if (*p == '=')
1541 matchp->use[op_no] = WRITE;
1542 else if (*p == '+')
1543 matchp->use[op_no] = READWRITE;
1545 for (;*p && i < which_alternative; p++)
1546 if (*p == ',')
1547 i++;
1549 while ((c = *p++) != '\0' && c != ',')
1550 switch (c)
1552 case '=':
1553 break;
1554 case '+':
1555 break;
1556 case '&':
1557 matchp->early_clobber[op_no] = 1;
1558 break;
1559 case '%':
1560 matchp->commutative[op_no] = op_no + 1;
1561 matchp->commutative[op_no + 1] = op_no;
1562 break;
1563 case '0': case '1': case '2': case '3': case '4':
1564 case '5': case '6': case '7': case '8': case '9':
1565 c -= '0';
1566 if (c < op_no && likely_spilled[(unsigned char) c])
1567 break;
1568 matchp->with[op_no] = c;
1569 any_matches = 1;
1570 if (matchp->commutative[op_no] >= 0)
1571 matchp->with[matchp->commutative[op_no]] = c;
1572 break;
1573 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1574 case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1575 case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1576 case 'C': case 'D': case 'W': case 'Y': case 'Z':
1577 if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_LETTER ((unsigned char)c)))
1578 likely_spilled[op_no] = 1;
1579 break;
1582 return any_matches;
1585 /* Try to replace all occurrences of DST_REG with SRC in LOC, that is
1586 assumed to be in INSN. */
1588 static void
1589 replace_in_call_usage (loc, dst_reg, src, insn)
1590 rtx *loc;
1591 int dst_reg;
1592 rtx src;
1593 rtx insn;
1595 rtx x = *loc;
1596 enum rtx_code code;
1597 const char *fmt;
1598 int i, j;
1600 if (! x)
1601 return;
1603 code = GET_CODE (x);
1604 if (code == REG)
1606 if (REGNO (x) != dst_reg)
1607 return;
1609 validate_change (insn, loc, src, 1);
1611 return;
1614 /* Process each of our operands recursively. */
1615 fmt = GET_RTX_FORMAT (code);
1616 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
1617 if (*fmt == 'e')
1618 replace_in_call_usage (&XEXP (x, i), dst_reg, src, insn);
1619 else if (*fmt == 'E')
1620 for (j = 0; j < XVECLEN (x, i); j++)
1621 replace_in_call_usage (& XVECEXP (x, i, j), dst_reg, src, insn);
1624 /* Try to replace output operand DST in SET, with input operand SRC. SET is
1625 the only set in INSN. INSN has just been recognized and constrained.
1626 SRC is operand number OPERAND_NUMBER in INSN.
1627 DST is operand number MATCH_NUMBER in INSN.
1628 If BACKWARD is nonzero, we have been called in a backward pass.
1629 Return nonzero for success. */
1631 static int
1632 fixup_match_1 (insn, set, src, src_subreg, dst, backward, operand_number,
1633 match_number, regmove_dump_file)
1634 rtx insn, set, src, src_subreg, dst;
1635 int backward, operand_number, match_number;
1636 FILE *regmove_dump_file;
1638 rtx p;
1639 rtx post_inc = 0, post_inc_set = 0, search_end = 0;
1640 int success = 0;
1641 int num_calls = 0, s_num_calls = 0;
1642 enum rtx_code code = NOTE;
1643 HOST_WIDE_INT insn_const = 0, newconst;
1644 rtx overlap = 0; /* need to move insn ? */
1645 rtx src_note = find_reg_note (insn, REG_DEAD, src), dst_note = NULL_RTX;
1646 int length, s_length;
1648 /* If SRC is marked as unchanging, we may not change it.
1649 ??? Maybe we could get better code by removing the unchanging bit
1650 instead, and changing it back if we don't succeed? */
1651 if (RTX_UNCHANGING_P (src))
1652 return 0;
1654 if (! src_note)
1656 /* Look for (set (regX) (op regA constX))
1657 (set (regY) (op regA constY))
1658 and change that to
1659 (set (regA) (op regA constX)).
1660 (set (regY) (op regA constY-constX)).
1661 This works for add and shift operations, if
1662 regA is dead after or set by the second insn. */
1664 code = GET_CODE (SET_SRC (set));
1665 if ((code == PLUS || code == LSHIFTRT
1666 || code == ASHIFT || code == ASHIFTRT)
1667 && XEXP (SET_SRC (set), 0) == src
1668 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
1669 insn_const = INTVAL (XEXP (SET_SRC (set), 1));
1670 else if (! stable_and_no_regs_but_for_p (SET_SRC (set), src, dst))
1671 return 0;
1672 else
1673 /* We might find a src_note while scanning. */
1674 code = NOTE;
1677 if (regmove_dump_file)
1678 fprintf (regmove_dump_file,
1679 "Could fix operand %d of insn %d matching operand %d.\n",
1680 operand_number, INSN_UID (insn), match_number);
1682 /* If SRC is equivalent to a constant set in a different basic block,
1683 then do not use it for this optimization. We want the equivalence
1684 so that if we have to reload this register, we can reload the
1685 constant, rather than extending the lifespan of the register. */
1686 if (reg_is_remote_constant_p (src, insn, get_insns ()))
1687 return 0;
1689 /* Scan forward to find the next instruction that
1690 uses the output operand. If the operand dies here,
1691 then replace it in both instructions with
1692 operand_number. */
1694 for (length = s_length = 0, p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
1696 if (GET_CODE (p) == CALL_INSN)
1697 replace_in_call_usage (& CALL_INSN_FUNCTION_USAGE (p),
1698 REGNO (dst), src, p);
1700 /* ??? We can't scan past the end of a basic block without updating
1701 the register lifetime info (REG_DEAD/basic_block_live_at_start). */
1702 if (perhaps_ends_bb_p (p))
1703 break;
1704 else if (! INSN_P (p))
1705 continue;
1707 length++;
1708 if (src_note)
1709 s_length++;
1711 if (reg_set_p (src, p) || reg_set_p (dst, p)
1712 || (GET_CODE (PATTERN (p)) == USE
1713 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
1714 break;
1716 /* See if all of DST dies in P. This test is
1717 slightly more conservative than it needs to be. */
1718 if ((dst_note = find_regno_note (p, REG_DEAD, REGNO (dst)))
1719 && (GET_MODE (XEXP (dst_note, 0)) == GET_MODE (dst)))
1721 /* If we would be moving INSN, check that we won't move it
1722 into the shadow of a live a live flags register. */
1723 /* ??? We only try to move it in front of P, although
1724 we could move it anywhere between OVERLAP and P. */
1725 if (overlap && GET_MODE (PREV_INSN (p)) != VOIDmode)
1726 break;
1728 if (! src_note)
1730 rtx q;
1731 rtx set2 = NULL_RTX;
1733 /* If an optimization is done, the value of SRC while P
1734 is executed will be changed. Check that this is OK. */
1735 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1736 break;
1737 for (q = p; q; q = NEXT_INSN (q))
1739 /* ??? We can't scan past the end of a basic block without
1740 updating the register lifetime info
1741 (REG_DEAD/basic_block_live_at_start). */
1742 if (perhaps_ends_bb_p (q))
1744 q = 0;
1745 break;
1747 else if (! INSN_P (q))
1748 continue;
1749 else if (reg_overlap_mentioned_p (src, PATTERN (q))
1750 || reg_set_p (src, q))
1751 break;
1753 if (q)
1754 set2 = single_set (q);
1755 if (! q || ! set2 || GET_CODE (SET_SRC (set2)) != code
1756 || XEXP (SET_SRC (set2), 0) != src
1757 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT
1758 || (SET_DEST (set2) != src
1759 && ! find_reg_note (q, REG_DEAD, src)))
1761 /* If this is a PLUS, we can still save a register by doing
1762 src += insn_const;
1764 src -= insn_const; .
1765 This also gives opportunities for subsequent
1766 optimizations in the backward pass, so do it there. */
1767 if (code == PLUS && backward
1768 /* Don't do this if we can likely tie DST to SET_DEST
1769 of P later; we can't do this tying here if we got a
1770 hard register. */
1771 && ! (dst_note && ! REG_N_CALLS_CROSSED (REGNO (dst))
1772 && single_set (p)
1773 && GET_CODE (SET_DEST (single_set (p))) == REG
1774 && (REGNO (SET_DEST (single_set (p)))
1775 < FIRST_PSEUDO_REGISTER))
1776 /* We may only emit an insn directly after P if we
1777 are not in the shadow of a live flags register. */
1778 && GET_MODE (p) == VOIDmode)
1780 search_end = q;
1781 q = insn;
1782 set2 = set;
1783 newconst = -insn_const;
1784 code = MINUS;
1786 else
1787 break;
1789 else
1791 newconst = INTVAL (XEXP (SET_SRC (set2), 1)) - insn_const;
1792 /* Reject out of range shifts. */
1793 if (code != PLUS
1794 && (newconst < 0
1795 || ((unsigned HOST_WIDE_INT) newconst
1796 >= (GET_MODE_BITSIZE (GET_MODE
1797 (SET_SRC (set2)))))))
1798 break;
1799 if (code == PLUS)
1801 post_inc = q;
1802 if (SET_DEST (set2) != src)
1803 post_inc_set = set2;
1806 /* We use 1 as last argument to validate_change so that all
1807 changes are accepted or rejected together by apply_change_group
1808 when it is called by validate_replace_rtx . */
1809 validate_change (q, &XEXP (SET_SRC (set2), 1),
1810 GEN_INT (newconst), 1);
1812 validate_change (insn, recog_data.operand_loc[match_number], src, 1);
1813 if (validate_replace_rtx (dst, src_subreg, p))
1814 success = 1;
1815 break;
1818 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1819 break;
1820 if (! src_note && reg_overlap_mentioned_p (src, PATTERN (p)))
1822 /* INSN was already checked to be movable wrt. the registers that it
1823 sets / uses when we found no REG_DEAD note for src on it, but it
1824 still might clobber the flags register. We'll have to check that
1825 we won't insert it into the shadow of a live flags register when
1826 we finally know where we are to move it. */
1827 overlap = p;
1828 src_note = find_reg_note (p, REG_DEAD, src);
1831 /* If we have passed a call instruction, and the pseudo-reg SRC is not
1832 already live across a call, then don't perform the optimization. */
1833 if (GET_CODE (p) == CALL_INSN)
1835 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1836 break;
1838 num_calls++;
1840 if (src_note)
1841 s_num_calls++;
1846 if (! success)
1847 return 0;
1849 /* Remove the death note for DST from P. */
1850 remove_note (p, dst_note);
1851 if (code == MINUS)
1853 post_inc = emit_insn_after (copy_rtx (PATTERN (insn)), p);
1854 if ((HAVE_PRE_INCREMENT || HAVE_PRE_DECREMENT)
1855 && search_end
1856 && try_auto_increment (search_end, post_inc, 0, src, newconst, 1))
1857 post_inc = 0;
1858 validate_change (insn, &XEXP (SET_SRC (set), 1), GEN_INT (insn_const), 0);
1859 REG_N_SETS (REGNO (src))++;
1860 REG_LIVE_LENGTH (REGNO (src))++;
1862 if (overlap)
1864 /* The lifetime of src and dest overlap,
1865 but we can change this by moving insn. */
1866 rtx pat = PATTERN (insn);
1867 if (src_note)
1868 remove_note (overlap, src_note);
1869 if ((HAVE_POST_INCREMENT || HAVE_POST_DECREMENT)
1870 && code == PLUS
1871 && try_auto_increment (overlap, insn, 0, src, insn_const, 0))
1872 insn = overlap;
1873 else
1875 rtx notes = REG_NOTES (insn);
1877 emit_insn_after_with_line_notes (pat, PREV_INSN (p), insn);
1878 PUT_CODE (insn, NOTE);
1879 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1880 NOTE_SOURCE_FILE (insn) = 0;
1881 /* emit_insn_after_with_line_notes has no
1882 return value, so search for the new insn. */
1883 insn = p;
1884 while (! INSN_P (insn) || PATTERN (insn) != pat)
1885 insn = PREV_INSN (insn);
1887 REG_NOTES (insn) = notes;
1890 /* Sometimes we'd generate src = const; src += n;
1891 if so, replace the instruction that set src
1892 in the first place. */
1894 if (! overlap && (code == PLUS || code == MINUS))
1896 rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
1897 rtx q, set2 = NULL_RTX;
1898 int num_calls2 = 0, s_length2 = 0;
1900 if (note && CONSTANT_P (XEXP (note, 0)))
1902 for (q = PREV_INSN (insn); q; q = PREV_INSN(q))
1904 /* ??? We can't scan past the end of a basic block without
1905 updating the register lifetime info
1906 (REG_DEAD/basic_block_live_at_start). */
1907 if (perhaps_ends_bb_p (q))
1909 q = 0;
1910 break;
1912 else if (! INSN_P (q))
1913 continue;
1915 s_length2++;
1916 if (reg_set_p (src, q))
1918 set2 = single_set (q);
1919 break;
1921 if (reg_overlap_mentioned_p (src, PATTERN (q)))
1923 q = 0;
1924 break;
1926 if (GET_CODE (p) == CALL_INSN)
1927 num_calls2++;
1929 if (q && set2 && SET_DEST (set2) == src && CONSTANT_P (SET_SRC (set2))
1930 && validate_change (insn, &SET_SRC (set), XEXP (note, 0), 0))
1932 PUT_CODE (q, NOTE);
1933 NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
1934 NOTE_SOURCE_FILE (q) = 0;
1935 REG_N_SETS (REGNO (src))--;
1936 REG_N_CALLS_CROSSED (REGNO (src)) -= num_calls2;
1937 REG_LIVE_LENGTH (REGNO (src)) -= s_length2;
1938 insn_const = 0;
1943 if ((HAVE_PRE_INCREMENT || HAVE_PRE_DECREMENT)
1944 && (code == PLUS || code == MINUS) && insn_const
1945 && try_auto_increment (p, insn, 0, src, insn_const, 1))
1946 insn = p;
1947 else if ((HAVE_POST_INCREMENT || HAVE_POST_DECREMENT)
1948 && post_inc
1949 && try_auto_increment (p, post_inc, post_inc_set, src, newconst, 0))
1950 post_inc = 0;
1951 /* If post_inc still prevails, try to find an
1952 insn where it can be used as a pre-in/decrement.
1953 If code is MINUS, this was already tried. */
1954 if (post_inc && code == PLUS
1955 /* Check that newconst is likely to be usable
1956 in a pre-in/decrement before starting the search. */
1957 && ((HAVE_PRE_INCREMENT && newconst > 0 && newconst <= MOVE_MAX)
1958 || (HAVE_PRE_DECREMENT && newconst < 0 && newconst >= -MOVE_MAX))
1959 && exact_log2 (newconst))
1961 rtx q, inc_dest;
1963 inc_dest = post_inc_set ? SET_DEST (post_inc_set) : src;
1964 for (q = post_inc; (q = NEXT_INSN (q)); )
1966 /* ??? We can't scan past the end of a basic block without updating
1967 the register lifetime info
1968 (REG_DEAD/basic_block_live_at_start). */
1969 if (perhaps_ends_bb_p (q))
1970 break;
1971 else if (! INSN_P (q))
1972 continue;
1973 else if (src != inc_dest
1974 && (reg_overlap_mentioned_p (src, PATTERN (q))
1975 || reg_set_p (src, q)))
1976 break;
1977 else if (reg_set_p (inc_dest, q))
1978 break;
1979 else if (reg_overlap_mentioned_p (inc_dest, PATTERN (q)))
1981 try_auto_increment (q, post_inc,
1982 post_inc_set, inc_dest, newconst, 1);
1983 break;
1988 /* Move the death note for DST to INSN if it is used
1989 there. */
1990 if (reg_overlap_mentioned_p (dst, PATTERN (insn)))
1992 XEXP (dst_note, 1) = REG_NOTES (insn);
1993 REG_NOTES (insn) = dst_note;
1996 if (src_note)
1998 /* Move the death note for SRC from INSN to P. */
1999 if (! overlap)
2000 remove_note (insn, src_note);
2001 XEXP (src_note, 1) = REG_NOTES (p);
2002 REG_NOTES (p) = src_note;
2004 REG_N_CALLS_CROSSED (REGNO (src)) += s_num_calls;
2007 REG_N_SETS (REGNO (src))++;
2008 REG_N_SETS (REGNO (dst))--;
2010 REG_N_CALLS_CROSSED (REGNO (dst)) -= num_calls;
2012 REG_LIVE_LENGTH (REGNO (src)) += s_length;
2013 if (REG_LIVE_LENGTH (REGNO (dst)) >= 0)
2015 REG_LIVE_LENGTH (REGNO (dst)) -= length;
2016 /* REG_LIVE_LENGTH is only an approximation after
2017 combine if sched is not run, so make sure that we
2018 still have a reasonable value. */
2019 if (REG_LIVE_LENGTH (REGNO (dst)) < 2)
2020 REG_LIVE_LENGTH (REGNO (dst)) = 2;
2022 if (regmove_dump_file)
2023 fprintf (regmove_dump_file,
2024 "Fixed operand %d of insn %d matching operand %d.\n",
2025 operand_number, INSN_UID (insn), match_number);
2026 return 1;
2030 /* return nonzero if X is stable and mentions no regsiters but for
2031 mentioning SRC or mentioning / changing DST . If in doubt, presume
2032 it is unstable.
2033 The rationale is that we want to check if we can move an insn easily
2034 while just paying attention to SRC and DST. A register is considered
2035 stable if it has the RTX_UNCHANGING_P bit set, but that would still
2036 leave the burden to update REG_DEAD / REG_UNUSED notes, so we don't
2037 want any registers but SRC and DST. */
2038 static int
2039 stable_and_no_regs_but_for_p (x, src, dst)
2040 rtx x, src, dst;
2042 RTX_CODE code = GET_CODE (x);
2043 switch (GET_RTX_CLASS (code))
2045 case '<': case '1': case 'c': case '2': case 'b': case '3':
2047 int i;
2048 const char *fmt = GET_RTX_FORMAT (code);
2049 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2050 if (fmt[i] == 'e'
2051 && ! stable_and_no_regs_but_for_p (XEXP (x, i), src, dst))
2052 return 0;
2053 return 1;
2055 case 'o':
2056 if (code == REG)
2057 return x == src || x == dst;
2058 /* If this is a MEM, look inside - there might be a register hidden in
2059 the address of an unchanging MEM. */
2060 if (code == MEM
2061 && ! stable_and_no_regs_but_for_p (XEXP (x, 0), src, dst))
2062 return 0;
2063 /* fall through */
2064 default:
2065 return ! rtx_unstable_p (x);
2069 /* Track stack adjustments and stack memory references. Attempt to
2070 reduce the number of stack adjustments by back-propogating across
2071 the memory references.
2073 This is intended primarily for use with targets that do not define
2074 ACCUMULATE_OUTGOING_ARGS. It is of significantly more value to
2075 targets that define PREFERRED_STACK_BOUNDARY more aligned than
2076 STACK_BOUNDARY (e.g. x86), or if not all registers can be pushed
2077 (e.g. x86 fp regs) which would ordinarily have to be implemented
2078 as a sub/mov pair due to restrictions in calls.c.
2080 Propogation stops when any of the insns that need adjusting are
2081 (a) no longer valid because we've exceeded their range, (b) a
2082 non-trivial push instruction, or (c) a call instruction.
2084 Restriction B is based on the assumption that push instructions
2085 are smaller or faster. If a port really wants to remove all
2086 pushes, it should have defined ACCUMULATE_OUTGOING_ARGS. The
2087 one exception that is made is for an add immediately followed
2088 by a push. */
2090 /* This structure records stack memory references between stack adjusting
2091 instructions. */
2093 struct csa_memlist
2095 HOST_WIDE_INT sp_offset;
2096 rtx insn, *mem;
2097 struct csa_memlist *next;
2100 static int stack_memref_p PARAMS ((rtx));
2101 static rtx single_set_for_csa PARAMS ((rtx));
2102 static void free_csa_memlist PARAMS ((struct csa_memlist *));
2103 static struct csa_memlist *record_one_stack_memref
2104 PARAMS ((rtx, rtx *, struct csa_memlist *));
2105 static int try_apply_stack_adjustment
2106 PARAMS ((rtx, struct csa_memlist *, HOST_WIDE_INT, HOST_WIDE_INT));
2107 static void combine_stack_adjustments_for_block PARAMS ((basic_block));
2108 static int record_stack_memrefs PARAMS ((rtx *, void *));
2111 /* Main entry point for stack adjustment combination. */
2113 void
2114 combine_stack_adjustments ()
2116 int i;
2118 for (i = 0; i < n_basic_blocks; ++i)
2119 combine_stack_adjustments_for_block (BASIC_BLOCK (i));
2122 /* Recognize a MEM of the form (sp) or (plus sp const). */
2124 static int
2125 stack_memref_p (x)
2126 rtx x;
2128 if (GET_CODE (x) != MEM)
2129 return 0;
2130 x = XEXP (x, 0);
2132 if (x == stack_pointer_rtx)
2133 return 1;
2134 if (GET_CODE (x) == PLUS
2135 && XEXP (x, 0) == stack_pointer_rtx
2136 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2137 return 1;
2139 return 0;
2142 /* Recognize either normal single_set or the hack in i386.md for
2143 tying fp and sp adjustments. */
2145 static rtx
2146 single_set_for_csa (insn)
2147 rtx insn;
2149 int i;
2150 rtx tmp = single_set (insn);
2151 if (tmp)
2152 return tmp;
2154 if (GET_CODE (insn) != INSN
2155 || GET_CODE (PATTERN (insn)) != PARALLEL)
2156 return NULL_RTX;
2158 tmp = PATTERN (insn);
2159 if (GET_CODE (XVECEXP (tmp, 0, 0)) != SET)
2160 return NULL_RTX;
2162 for (i = 1; i < XVECLEN (tmp, 0); ++i)
2164 rtx this = XVECEXP (tmp, 0, i);
2166 /* The special case is allowing a no-op set. */
2167 if (GET_CODE (this) == SET
2168 && SET_SRC (this) == SET_DEST (this))
2170 else if (GET_CODE (this) != CLOBBER
2171 && GET_CODE (this) != USE)
2172 return NULL_RTX;
2175 return XVECEXP (tmp, 0, 0);
2178 /* Free the list of csa_memlist nodes. */
2180 static void
2181 free_csa_memlist (memlist)
2182 struct csa_memlist *memlist;
2184 struct csa_memlist *next;
2185 for (; memlist ; memlist = next)
2187 next = memlist->next;
2188 free (memlist);
2192 /* Create a new csa_memlist node from the given memory reference.
2193 It is already known that the memory is stack_memref_p. */
2195 static struct csa_memlist *
2196 record_one_stack_memref (insn, mem, next_memlist)
2197 rtx insn, *mem;
2198 struct csa_memlist *next_memlist;
2200 struct csa_memlist *ml;
2202 ml = (struct csa_memlist *) xmalloc (sizeof (*ml));
2204 if (XEXP (*mem, 0) == stack_pointer_rtx)
2205 ml->sp_offset = 0;
2206 else
2207 ml->sp_offset = INTVAL (XEXP (XEXP (*mem, 0), 1));
2209 ml->insn = insn;
2210 ml->mem = mem;
2211 ml->next = next_memlist;
2213 return ml;
2216 /* Attempt to apply ADJUST to the stack adjusting insn INSN, as well
2217 as each of the memories in MEMLIST. Return true on success. */
2219 static int
2220 try_apply_stack_adjustment (insn, memlist, new_adjust, delta)
2221 rtx insn;
2222 struct csa_memlist *memlist;
2223 HOST_WIDE_INT new_adjust, delta;
2225 struct csa_memlist *ml;
2226 rtx set;
2228 /* We know INSN matches single_set_for_csa, because that's what we
2229 recognized earlier. However, if INSN is not single_set, it is
2230 doing double duty as a barrier for frame pointer memory accesses,
2231 which we are not recording. Therefore, an adjust insn that is not
2232 single_set may not have a positive delta applied. */
2234 if (delta > 0 && ! single_set (insn))
2235 return 0;
2236 set = single_set_for_csa (insn);
2237 validate_change (insn, &XEXP (SET_SRC (set), 1), GEN_INT (new_adjust), 1);
2239 for (ml = memlist; ml ; ml = ml->next)
2241 HOST_WIDE_INT c = ml->sp_offset - delta;
2242 rtx new = gen_rtx_MEM (GET_MODE (*ml->mem),
2243 plus_constant (stack_pointer_rtx, c));
2245 /* Don't reference memory below the stack pointer. */
2246 if (c < 0)
2248 cancel_changes (0);
2249 return 0;
2252 MEM_COPY_ATTRIBUTES (new, *ml->mem);
2253 validate_change (ml->insn, ml->mem, new, 1);
2256 if (apply_change_group ())
2258 /* Succeeded. Update our knowledge of the memory references. */
2259 for (ml = memlist; ml ; ml = ml->next)
2260 ml->sp_offset -= delta;
2262 return 1;
2264 else
2265 return 0;
2268 /* Called via for_each_rtx and used to record all stack memory references in
2269 the insn and discard all other stack pointer references. */
2270 struct record_stack_memrefs_data
2272 rtx insn;
2273 struct csa_memlist *memlist;
2276 static int
2277 record_stack_memrefs (xp, data)
2278 rtx *xp;
2279 void *data;
2281 rtx x = *xp;
2282 struct record_stack_memrefs_data *d =
2283 (struct record_stack_memrefs_data *) data;
2284 if (!x)
2285 return 0;
2286 switch (GET_CODE (x))
2288 case MEM:
2289 if (!reg_mentioned_p (stack_pointer_rtx, x))
2290 return -1;
2291 /* We are not able to handle correctly all possible memrefs containing
2292 stack pointer, so this check is neccesary. */
2293 if (stack_memref_p (x))
2295 d->memlist = record_one_stack_memref (d->insn, xp, d->memlist);
2296 return -1;
2298 return 1;
2299 case REG:
2300 /* ??? We want be able to handle non-memory stack pointer references
2301 later. For now just discard all insns refering to stack pointer
2302 outside mem expressions. We would probably want to teach
2303 validate_replace to simplify expressions first. */
2304 if (x == stack_pointer_rtx)
2305 return 1;
2306 break;
2307 default:
2308 break;
2310 return 0;
2313 /* Subroutine of combine_stack_adjustments, called for each basic block. */
2315 static void
2316 combine_stack_adjustments_for_block (bb)
2317 basic_block bb;
2319 HOST_WIDE_INT last_sp_adjust = 0;
2320 rtx last_sp_set = NULL_RTX;
2321 struct csa_memlist *memlist = NULL;
2322 rtx pending_delete;
2323 rtx insn, next;
2324 struct record_stack_memrefs_data data;
2326 for (insn = bb->head; ; insn = next)
2328 rtx set;
2330 pending_delete = NULL_RTX;
2331 next = NEXT_INSN (insn);
2333 if (! INSN_P (insn))
2334 goto processed;
2336 set = single_set_for_csa (insn);
2337 if (set)
2339 rtx dest = SET_DEST (set);
2340 rtx src = SET_SRC (set);
2342 /* Find constant additions to the stack pointer. */
2343 if (dest == stack_pointer_rtx
2344 && GET_CODE (src) == PLUS
2345 && XEXP (src, 0) == stack_pointer_rtx
2346 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2348 HOST_WIDE_INT this_adjust = INTVAL (XEXP (src, 1));
2350 /* If we've not seen an adjustment previously, record
2351 it now and continue. */
2352 if (! last_sp_set)
2354 last_sp_set = insn;
2355 last_sp_adjust = this_adjust;
2356 goto processed;
2359 /* If not all recorded memrefs can be adjusted, or the
2360 adjustment is now too large for a constant addition,
2361 we cannot merge the two stack adjustments. */
2362 if (! try_apply_stack_adjustment (last_sp_set, memlist,
2363 last_sp_adjust + this_adjust,
2364 this_adjust))
2366 free_csa_memlist (memlist);
2367 memlist = NULL;
2368 last_sp_set = insn;
2369 last_sp_adjust = this_adjust;
2370 goto processed;
2373 /* It worked! */
2374 pending_delete = insn;
2375 last_sp_adjust += this_adjust;
2377 /* If, by some accident, the adjustments cancel out,
2378 delete both insns and start from scratch. */
2379 if (last_sp_adjust == 0)
2381 if (last_sp_set == bb->head)
2382 bb->head = NEXT_INSN (last_sp_set);
2383 flow_delete_insn (last_sp_set);
2385 free_csa_memlist (memlist);
2386 memlist = NULL;
2387 last_sp_set = NULL_RTX;
2390 goto processed;
2393 /* Find a predecrement of exactly the previous adjustment and
2394 turn it into a direct store. Obviously we can't do this if
2395 there were any intervening uses of the stack pointer. */
2396 if (memlist == NULL
2397 && GET_CODE (dest) == MEM
2398 && ((GET_CODE (XEXP (dest, 0)) == PRE_DEC
2399 && (last_sp_adjust
2400 == (HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (dest))))
2401 || (GET_CODE (XEXP (dest, 0)) == PRE_MODIFY
2402 && GET_CODE (XEXP (XEXP (dest, 0), 1)) == PLUS
2403 && XEXP (XEXP (XEXP (dest, 0), 1), 0) == stack_pointer_rtx
2404 && (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2405 == CONST_INT)
2406 && (INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2407 == -last_sp_adjust)))
2408 && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx
2409 && ! reg_mentioned_p (stack_pointer_rtx, src)
2410 && memory_address_p (GET_MODE (dest), stack_pointer_rtx)
2411 && validate_change (insn, &SET_DEST (set),
2412 change_address (dest, VOIDmode,
2413 stack_pointer_rtx), 0))
2415 if (last_sp_set == bb->head)
2416 bb->head = NEXT_INSN (last_sp_set);
2417 flow_delete_insn (last_sp_set);
2419 free_csa_memlist (memlist);
2420 memlist = NULL;
2421 last_sp_set = NULL_RTX;
2422 last_sp_adjust = 0;
2423 goto processed;
2427 data.insn = insn;
2428 data.memlist = memlist;
2429 if (GET_CODE (insn) != CALL_INSN && last_sp_set
2430 && !for_each_rtx (&PATTERN (insn), record_stack_memrefs, &data))
2432 memlist = data.memlist;
2433 goto processed;
2435 memlist = data.memlist;
2437 /* Otherwise, we were not able to process the instruction.
2438 Do not continue collecting data across such a one. */
2439 if (last_sp_set
2440 && (GET_CODE (insn) == CALL_INSN
2441 || reg_mentioned_p (stack_pointer_rtx, PATTERN (insn))))
2443 free_csa_memlist (memlist);
2444 memlist = NULL;
2445 last_sp_set = NULL_RTX;
2446 last_sp_adjust = 0;
2449 processed:
2450 if (insn == bb->end)
2451 break;
2453 if (pending_delete)
2454 flow_delete_insn (pending_delete);
2457 if (pending_delete)
2459 bb->end = PREV_INSN (pending_delete);
2460 flow_delete_insn (pending_delete);