* configure: Regenerated.
[official-gcc.git] / gcc / regmove.c
blob6ff8ef6191c5d0f4ccf456e383b1b004196524e8
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, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This module makes some simple RTL code transformations which
24 improve the subsequent register allocation. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "rtl.h"
31 #include "tm_p.h"
32 #include "insn-config.h"
33 #include "recog.h"
34 #include "target.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 "basic-block.h"
41 #include "except.h"
42 #include "diagnostic-core.h"
43 #include "reload.h"
44 #include "tree-pass.h"
45 #include "df.h"
46 #include "ira.h"
48 static int optimize_reg_copy_1 (rtx, rtx, rtx);
49 static void optimize_reg_copy_2 (rtx, rtx, rtx);
50 static void optimize_reg_copy_3 (rtx, rtx, rtx);
51 static void copy_src_to_dest (rtx, rtx, rtx);
53 enum match_use
55 READ,
56 WRITE,
57 READWRITE
60 struct match {
61 int with[MAX_RECOG_OPERANDS];
62 enum match_use use[MAX_RECOG_OPERANDS];
63 int commutative[MAX_RECOG_OPERANDS];
64 int early_clobber[MAX_RECOG_OPERANDS];
67 static int find_matches (rtx, struct match *);
68 static int fixup_match_2 (rtx, rtx, rtx, rtx);
70 /* Return nonzero if registers with CLASS1 and CLASS2 can be merged without
71 causing too much register allocation problems. */
72 static int
73 regclass_compatible_p (reg_class_t class0, reg_class_t class1)
75 return (class0 == class1
76 || (reg_class_subset_p (class0, class1)
77 && ! targetm.class_likely_spilled_p (class0))
78 || (reg_class_subset_p (class1, class0)
79 && ! targetm.class_likely_spilled_p (class1)));
83 #ifdef AUTO_INC_DEC
85 /* Find the place in the rtx X where REG is used as a memory address.
86 Return the MEM rtx that so uses it.
87 If PLUSCONST is nonzero, search instead for a memory address equivalent to
88 (plus REG (const_int PLUSCONST)).
90 If such an address does not appear, return 0.
91 If REG appears more than once, or is used other than in such an address,
92 return (rtx) 1. */
94 static rtx
95 find_use_as_address (rtx x, rtx reg, HOST_WIDE_INT plusconst)
97 enum rtx_code code = GET_CODE (x);
98 const char * const fmt = GET_RTX_FORMAT (code);
99 int i;
100 rtx value = 0;
101 rtx tem;
103 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
104 return x;
106 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
107 && XEXP (XEXP (x, 0), 0) == reg
108 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
109 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
110 return x;
112 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
114 /* If REG occurs inside a MEM used in a bit-field reference,
115 that is unacceptable. */
116 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
117 return (rtx) (size_t) 1;
120 if (x == reg)
121 return (rtx) (size_t) 1;
123 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
125 if (fmt[i] == 'e')
127 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
128 if (value == 0)
129 value = tem;
130 else if (tem != 0)
131 return (rtx) (size_t) 1;
133 else if (fmt[i] == 'E')
135 int j;
136 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
138 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
139 if (value == 0)
140 value = tem;
141 else if (tem != 0)
142 return (rtx) (size_t) 1;
147 return value;
151 /* INC_INSN is an instruction that adds INCREMENT to REG.
152 Try to fold INC_INSN as a post/pre in/decrement into INSN.
153 Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
154 Return nonzero for success. */
155 static int
156 try_auto_increment (rtx insn, rtx inc_insn, rtx inc_insn_set, rtx reg,
157 HOST_WIDE_INT increment, int pre)
159 enum rtx_code inc_code;
161 rtx pset = single_set (insn);
162 if (pset)
164 /* Can't use the size of SET_SRC, we might have something like
165 (sign_extend:SI (mem:QI ... */
166 rtx use = find_use_as_address (pset, reg, 0);
167 if (use != 0 && use != (rtx) (size_t) 1)
169 int size = GET_MODE_SIZE (GET_MODE (use));
170 if (0
171 || (HAVE_POST_INCREMENT
172 && pre == 0 && (inc_code = POST_INC, increment == size))
173 || (HAVE_PRE_INCREMENT
174 && pre == 1 && (inc_code = PRE_INC, increment == size))
175 || (HAVE_POST_DECREMENT
176 && pre == 0 && (inc_code = POST_DEC, increment == -size))
177 || (HAVE_PRE_DECREMENT
178 && pre == 1 && (inc_code = PRE_DEC, increment == -size))
181 if (inc_insn_set)
182 validate_change
183 (inc_insn,
184 &SET_SRC (inc_insn_set),
185 XEXP (SET_SRC (inc_insn_set), 0), 1);
186 validate_change (insn, &XEXP (use, 0),
187 gen_rtx_fmt_e (inc_code,
188 GET_MODE (XEXP (use, 0)), reg),
190 if (apply_change_group ())
192 /* If there is a REG_DEAD note on this insn, we must
193 change this not to REG_UNUSED meaning that the register
194 is set, but the value is dead. Failure to do so will
195 result in sched1 dying -- when it recomputes lifetime
196 information, the number of REG_DEAD notes will have
197 changed. */
198 rtx note = find_reg_note (insn, REG_DEAD, reg);
199 if (note)
200 PUT_REG_NOTE_KIND (note, REG_UNUSED);
202 add_reg_note (insn, REG_INC, reg);
204 if (! inc_insn_set)
205 delete_insn (inc_insn);
206 return 1;
211 return 0;
213 #endif
216 static int *regno_src_regno;
218 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
219 in INSN.
221 Search forward to see if SRC dies before either it or DEST is modified,
222 but don't scan past the end of a basic block. If so, we can replace SRC
223 with DEST and let SRC die in INSN.
225 This will reduce the number of registers live in that range and may enable
226 DEST to be tied to SRC, thus often saving one register in addition to a
227 register-register copy. */
229 static int
230 optimize_reg_copy_1 (rtx insn, rtx dest, rtx src)
232 rtx p, q;
233 rtx note;
234 rtx dest_death = 0;
235 int sregno = REGNO (src);
236 int dregno = REGNO (dest);
237 basic_block bb = BLOCK_FOR_INSN (insn);
239 /* We don't want to mess with hard regs if register classes are small. */
240 if (sregno == dregno
241 || (targetm.small_register_classes_for_mode_p (GET_MODE (src))
242 && (sregno < FIRST_PSEUDO_REGISTER
243 || dregno < FIRST_PSEUDO_REGISTER))
244 /* We don't see all updates to SP if they are in an auto-inc memory
245 reference, so we must disallow this optimization on them. */
246 || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
247 return 0;
249 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
251 if (! INSN_P (p))
252 continue;
253 if (BLOCK_FOR_INSN (p) != bb)
254 break;
256 if (reg_set_p (src, p) || reg_set_p (dest, p)
257 /* If SRC is an asm-declared register, it must not be replaced
258 in any asm. Unfortunately, the REG_EXPR tree for the asm
259 variable may be absent in the SRC rtx, so we can't check the
260 actual register declaration easily (the asm operand will have
261 it, though). To avoid complicating the test for a rare case,
262 we just don't perform register replacement for a hard reg
263 mentioned in an asm. */
264 || (sregno < FIRST_PSEUDO_REGISTER
265 && asm_noperands (PATTERN (p)) >= 0
266 && reg_overlap_mentioned_p (src, PATTERN (p)))
267 /* Don't change hard registers used by a call. */
268 || (CALL_P (p) && sregno < FIRST_PSEUDO_REGISTER
269 && find_reg_fusage (p, USE, src))
270 /* Don't change a USE of a register. */
271 || (GET_CODE (PATTERN (p)) == USE
272 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
273 break;
275 /* See if all of SRC dies in P. This test is slightly more
276 conservative than it needs to be. */
277 if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
278 && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
280 int failed = 0;
281 int d_length = 0;
282 int s_length = 0;
283 int d_n_calls = 0;
284 int s_n_calls = 0;
285 int s_freq_calls = 0;
286 int d_freq_calls = 0;
288 /* We can do the optimization. Scan forward from INSN again,
289 replacing regs as we go. Set FAILED if a replacement can't
290 be done. In that case, we can't move the death note for SRC.
291 This should be rare. */
293 /* Set to stop at next insn. */
294 for (q = next_real_insn (insn);
295 q != next_real_insn (p);
296 q = next_real_insn (q))
298 if (reg_overlap_mentioned_p (src, PATTERN (q)))
300 /* If SRC is a hard register, we might miss some
301 overlapping registers with validate_replace_rtx,
302 so we would have to undo it. We can't if DEST is
303 present in the insn, so fail in that combination
304 of cases. */
305 if (sregno < FIRST_PSEUDO_REGISTER
306 && reg_mentioned_p (dest, PATTERN (q)))
307 failed = 1;
309 /* Attempt to replace all uses. */
310 else if (!validate_replace_rtx (src, dest, q))
311 failed = 1;
313 /* If this succeeded, but some part of the register
314 is still present, undo the replacement. */
315 else if (sregno < FIRST_PSEUDO_REGISTER
316 && reg_overlap_mentioned_p (src, PATTERN (q)))
318 validate_replace_rtx (dest, src, q);
319 failed = 1;
323 /* For SREGNO, count the total number of insns scanned.
324 For DREGNO, count the total number of insns scanned after
325 passing the death note for DREGNO. */
326 if (!DEBUG_INSN_P (p))
328 s_length++;
329 if (dest_death)
330 d_length++;
333 /* If the insn in which SRC dies is a CALL_INSN, don't count it
334 as a call that has been crossed. Otherwise, count it. */
335 if (q != p && CALL_P (q))
337 /* Similarly, total calls for SREGNO, total calls beyond
338 the death note for DREGNO. */
339 s_n_calls++;
340 s_freq_calls += REG_FREQ_FROM_BB (BLOCK_FOR_INSN (q));
341 if (dest_death)
343 d_n_calls++;
344 d_freq_calls += REG_FREQ_FROM_BB (BLOCK_FOR_INSN (q));
348 /* If DEST dies here, remove the death note and save it for
349 later. Make sure ALL of DEST dies here; again, this is
350 overly conservative. */
351 if (dest_death == 0
352 && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0)
354 if (GET_MODE (XEXP (dest_death, 0)) != GET_MODE (dest))
355 failed = 1, dest_death = 0;
356 else
357 remove_note (q, dest_death);
361 if (! failed)
363 /* These counters need to be updated if and only if we are
364 going to move the REG_DEAD note. */
365 if (sregno >= FIRST_PSEUDO_REGISTER)
367 if (REG_LIVE_LENGTH (sregno) >= 0)
369 REG_LIVE_LENGTH (sregno) -= s_length;
370 /* REG_LIVE_LENGTH is only an approximation after
371 combine if sched is not run, so make sure that we
372 still have a reasonable value. */
373 if (REG_LIVE_LENGTH (sregno) < 2)
374 REG_LIVE_LENGTH (sregno) = 2;
377 REG_N_CALLS_CROSSED (sregno) -= s_n_calls;
378 REG_FREQ_CALLS_CROSSED (sregno) -= s_freq_calls;
381 /* Move death note of SRC from P to INSN. */
382 remove_note (p, note);
383 XEXP (note, 1) = REG_NOTES (insn);
384 REG_NOTES (insn) = note;
387 /* DEST is also dead if INSN has a REG_UNUSED note for DEST. */
388 if (! dest_death
389 && (dest_death = find_regno_note (insn, REG_UNUSED, dregno)))
391 PUT_REG_NOTE_KIND (dest_death, REG_DEAD);
392 remove_note (insn, dest_death);
395 /* Put death note of DEST on P if we saw it die. */
396 if (dest_death)
398 XEXP (dest_death, 1) = REG_NOTES (p);
399 REG_NOTES (p) = dest_death;
401 if (dregno >= FIRST_PSEUDO_REGISTER)
403 /* If and only if we are moving the death note for DREGNO,
404 then we need to update its counters. */
405 if (REG_LIVE_LENGTH (dregno) >= 0)
406 REG_LIVE_LENGTH (dregno) += d_length;
407 REG_N_CALLS_CROSSED (dregno) += d_n_calls;
408 REG_FREQ_CALLS_CROSSED (dregno) += d_freq_calls;
412 return ! failed;
415 /* If SRC is a hard register which is set or killed in some other
416 way, we can't do this optimization. */
417 else if (sregno < FIRST_PSEUDO_REGISTER
418 && dead_or_set_p (p, src))
419 break;
421 return 0;
424 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
425 a sequence of insns that modify DEST followed by an insn that sets
426 SRC to DEST in which DEST dies, with no prior modification of DEST.
427 (There is no need to check if the insns in between actually modify
428 DEST. We should not have cases where DEST is not modified, but
429 the optimization is safe if no such modification is detected.)
430 In that case, we can replace all uses of DEST, starting with INSN and
431 ending with the set of SRC to DEST, with SRC. We do not do this
432 optimization if a CALL_INSN is crossed unless SRC already crosses a
433 call or if DEST dies before the copy back to SRC.
435 It is assumed that DEST and SRC are pseudos; it is too complicated to do
436 this for hard registers since the substitutions we may make might fail. */
438 static void
439 optimize_reg_copy_2 (rtx insn, rtx dest, rtx src)
441 rtx p, q;
442 rtx set;
443 int sregno = REGNO (src);
444 int dregno = REGNO (dest);
445 basic_block bb = BLOCK_FOR_INSN (insn);
447 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
449 if (! INSN_P (p))
450 continue;
451 if (BLOCK_FOR_INSN (p) != bb)
452 break;
454 set = single_set (p);
455 if (set && SET_SRC (set) == dest && SET_DEST (set) == src
456 && find_reg_note (p, REG_DEAD, dest))
458 /* We can do the optimization. Scan forward from INSN again,
459 replacing regs as we go. */
461 /* Set to stop at next insn. */
462 for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
463 if (INSN_P (q))
465 if (reg_mentioned_p (dest, PATTERN (q)))
467 rtx note;
469 PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
470 note = FIND_REG_INC_NOTE (q, dest);
471 if (note)
473 remove_note (q, note);
474 add_reg_note (q, REG_INC, src);
476 df_insn_rescan (q);
479 if (CALL_P (q))
481 int freq = REG_FREQ_FROM_BB (BLOCK_FOR_INSN (q));
482 REG_N_CALLS_CROSSED (dregno)--;
483 REG_N_CALLS_CROSSED (sregno)++;
484 REG_FREQ_CALLS_CROSSED (dregno) -= freq;
485 REG_FREQ_CALLS_CROSSED (sregno) += freq;
489 remove_note (p, find_reg_note (p, REG_DEAD, dest));
490 REG_N_DEATHS (dregno)--;
491 remove_note (insn, find_reg_note (insn, REG_DEAD, src));
492 REG_N_DEATHS (sregno)--;
493 return;
496 if (reg_set_p (src, p)
497 || find_reg_note (p, REG_DEAD, dest)
498 || (CALL_P (p) && REG_N_CALLS_CROSSED (sregno) == 0))
499 break;
503 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
504 Look if SRC dies there, and if it is only set once, by loading
505 it from memory. If so, try to incorporate the zero/sign extension
506 into the memory read, change SRC to the mode of DEST, and alter
507 the remaining accesses to use the appropriate SUBREG. This allows
508 SRC and DEST to be tied later. */
509 static void
510 optimize_reg_copy_3 (rtx insn, rtx dest, rtx src)
512 rtx src_reg = XEXP (src, 0);
513 int src_no = REGNO (src_reg);
514 int dst_no = REGNO (dest);
515 rtx p, set, set_insn;
516 enum machine_mode old_mode;
517 basic_block bb = BLOCK_FOR_INSN (insn);
519 if (src_no < FIRST_PSEUDO_REGISTER
520 || dst_no < FIRST_PSEUDO_REGISTER
521 || ! find_reg_note (insn, REG_DEAD, src_reg)
522 || REG_N_DEATHS (src_no) != 1
523 || REG_N_SETS (src_no) != 1)
524 return;
526 for (p = PREV_INSN (insn); p && ! reg_set_p (src_reg, p); p = PREV_INSN (p))
527 if (INSN_P (p) && BLOCK_FOR_INSN (p) != bb)
528 break;
530 if (! p || BLOCK_FOR_INSN (p) != bb)
531 return;
533 if (! (set = single_set (p))
534 || !MEM_P (SET_SRC (set))
535 /* If there's a REG_EQUIV note, this must be an insn that loads an
536 argument. Prefer keeping the note over doing this optimization. */
537 || find_reg_note (p, REG_EQUIV, NULL_RTX)
538 || SET_DEST (set) != src_reg)
539 return;
541 /* Be conservative: although this optimization is also valid for
542 volatile memory references, that could cause trouble in later passes. */
543 if (MEM_VOLATILE_P (SET_SRC (set)))
544 return;
546 /* Do not use a SUBREG to truncate from one mode to another if truncation
547 is not a nop. */
548 if (GET_MODE_BITSIZE (GET_MODE (src_reg)) <= GET_MODE_BITSIZE (GET_MODE (src))
549 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (src), GET_MODE (src_reg)))
550 return;
552 set_insn = p;
553 old_mode = GET_MODE (src_reg);
554 PUT_MODE (src_reg, GET_MODE (src));
555 XEXP (src, 0) = SET_SRC (set);
557 /* Include this change in the group so that it's easily undone if
558 one of the changes in the group is invalid. */
559 validate_change (p, &SET_SRC (set), src, 1);
561 /* Now walk forward making additional replacements. We want to be able
562 to undo all the changes if a later substitution fails. */
563 while (p = NEXT_INSN (p), p != insn)
565 if (! INSN_P (p))
566 continue;
568 /* Make a tentative change. */
569 validate_replace_rtx_group (src_reg,
570 gen_lowpart_SUBREG (old_mode, src_reg),
574 validate_replace_rtx_group (src, src_reg, insn);
576 /* Now see if all the changes are valid. */
577 if (! apply_change_group ())
579 /* One or more changes were no good. Back out everything. */
580 PUT_MODE (src_reg, old_mode);
581 XEXP (src, 0) = src_reg;
583 else
585 rtx note = find_reg_note (set_insn, REG_EQUAL, NULL_RTX);
586 if (note)
588 if (rtx_equal_p (XEXP (note, 0), XEXP (src, 0)))
590 XEXP (note, 0)
591 = gen_rtx_fmt_e (GET_CODE (src), GET_MODE (src),
592 XEXP (note, 0));
593 df_notes_rescan (set_insn);
595 else
596 remove_note (set_insn, note);
602 /* If we were not able to update the users of src to use dest directly, try
603 instead moving the value to dest directly before the operation. */
605 static void
606 copy_src_to_dest (rtx insn, rtx src, rtx dest)
608 rtx seq;
609 rtx link;
610 rtx next;
611 rtx set;
612 rtx move_insn;
613 rtx *p_insn_notes;
614 rtx *p_move_notes;
615 int src_regno;
616 int dest_regno;
618 /* A REG_LIVE_LENGTH of -1 indicates the register is equivalent to a constant
619 or memory location and is used infrequently; a REG_LIVE_LENGTH of -2 is
620 parameter when there is no frame pointer that is not allocated a register.
621 For now, we just reject them, rather than incrementing the live length. */
623 if (REG_P (src)
624 && REG_LIVE_LENGTH (REGNO (src)) > 0
625 && REG_P (dest)
626 && REG_LIVE_LENGTH (REGNO (dest)) > 0
627 && (set = single_set (insn)) != NULL_RTX
628 && !reg_mentioned_p (dest, SET_SRC (set))
629 && GET_MODE (src) == GET_MODE (dest))
631 int old_num_regs = reg_rtx_no;
633 /* Generate the src->dest move. */
634 start_sequence ();
635 emit_move_insn (dest, src);
636 seq = get_insns ();
637 end_sequence ();
638 /* If this sequence uses new registers, we may not use it. */
639 if (old_num_regs != reg_rtx_no
640 || ! validate_replace_rtx (src, dest, insn))
642 /* We have to restore reg_rtx_no to its old value, lest
643 recompute_reg_usage will try to compute the usage of the
644 new regs, yet reg_n_info is not valid for them. */
645 reg_rtx_no = old_num_regs;
646 return;
648 emit_insn_before (seq, insn);
649 move_insn = PREV_INSN (insn);
650 p_move_notes = &REG_NOTES (move_insn);
651 p_insn_notes = &REG_NOTES (insn);
653 /* Move any notes mentioning src to the move instruction. */
654 for (link = REG_NOTES (insn); link != NULL_RTX; link = next)
656 next = XEXP (link, 1);
657 if (XEXP (link, 0) == src)
659 *p_move_notes = link;
660 p_move_notes = &XEXP (link, 1);
662 else
664 *p_insn_notes = link;
665 p_insn_notes = &XEXP (link, 1);
669 *p_move_notes = NULL_RTX;
670 *p_insn_notes = NULL_RTX;
672 /* Update the various register tables. */
673 dest_regno = REGNO (dest);
674 INC_REG_N_SETS (dest_regno, 1);
675 REG_LIVE_LENGTH (dest_regno)++;
676 src_regno = REGNO (src);
677 if (! find_reg_note (move_insn, REG_DEAD, src))
678 REG_LIVE_LENGTH (src_regno)++;
682 /* reg_set_in_bb[REGNO] points to basic block iff the register is set
683 only once in the given block and has REG_EQUAL note. */
685 static basic_block *reg_set_in_bb;
687 /* Size of reg_set_in_bb array. */
688 static unsigned int max_reg_computed;
691 /* Return whether REG is set in only one location, and is set to a
692 constant, but is set in a different basic block from INSN (an
693 instructions which uses REG). In this case REG is equivalent to a
694 constant, and we don't want to break that equivalence, because that
695 may increase register pressure and make reload harder. If REG is
696 set in the same basic block as INSN, we don't worry about it,
697 because we'll probably need a register anyhow (??? but what if REG
698 is used in a different basic block as well as this one?). */
700 static bool
701 reg_is_remote_constant_p (rtx reg, rtx insn)
703 basic_block bb;
704 rtx p;
705 int max;
707 if (!reg_set_in_bb)
709 max_reg_computed = max = max_reg_num ();
710 reg_set_in_bb = XCNEWVEC (basic_block, max);
712 FOR_EACH_BB (bb)
713 FOR_BB_INSNS (bb, p)
715 rtx s;
717 if (!INSN_P (p))
718 continue;
719 s = single_set (p);
720 /* This is the instruction which sets REG. If there is a
721 REG_EQUAL note, then REG is equivalent to a constant. */
722 if (s != 0
723 && REG_P (SET_DEST (s))
724 && REG_N_SETS (REGNO (SET_DEST (s))) == 1
725 && find_reg_note (p, REG_EQUAL, NULL_RTX))
726 reg_set_in_bb[REGNO (SET_DEST (s))] = bb;
730 gcc_assert (REGNO (reg) < max_reg_computed);
731 if (reg_set_in_bb[REGNO (reg)] == NULL)
732 return false;
733 return (reg_set_in_bb[REGNO (reg)] != BLOCK_FOR_INSN (insn));
736 /* INSN is adding a CONST_INT to a REG. We search backwards looking for
737 another add immediate instruction with the same source and dest registers,
738 and if we find one, we change INSN to an increment, and return 1. If
739 no changes are made, we return 0.
741 This changes
742 (set (reg100) (plus reg1 offset1))
744 (set (reg100) (plus reg1 offset2))
746 (set (reg100) (plus reg1 offset1))
748 (set (reg100) (plus reg100 offset2-offset1)) */
750 /* ??? What does this comment mean? */
751 /* cse disrupts preincrement / postdecrement sequences when it finds a
752 hard register as ultimate source, like the frame pointer. */
754 static int
755 fixup_match_2 (rtx insn, rtx dst, rtx src, rtx offset)
757 rtx p, dst_death = 0;
758 int length, num_calls = 0, freq_calls = 0;
759 basic_block bb = BLOCK_FOR_INSN (insn);
761 /* If SRC dies in INSN, we'd have to move the death note. This is
762 considered to be very unlikely, so we just skip the optimization
763 in this case. */
764 if (find_regno_note (insn, REG_DEAD, REGNO (src)))
765 return 0;
767 /* Scan backward to find the first instruction that sets DST. */
769 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
771 rtx pset;
773 if (! INSN_P (p))
774 continue;
775 if (BLOCK_FOR_INSN (p) != bb)
776 break;
778 if (find_regno_note (p, REG_DEAD, REGNO (dst)))
779 dst_death = p;
780 if (! dst_death && !DEBUG_INSN_P (p))
781 length++;
783 pset = single_set (p);
784 if (pset && SET_DEST (pset) == dst
785 && GET_CODE (SET_SRC (pset)) == PLUS
786 && XEXP (SET_SRC (pset), 0) == src
787 && CONST_INT_P (XEXP (SET_SRC (pset), 1)))
789 HOST_WIDE_INT newconst
790 = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
791 rtx add = gen_add3_insn (dst, dst, GEN_INT (newconst));
793 if (add && validate_change (insn, &PATTERN (insn), add, 0))
795 /* Remove the death note for DST from DST_DEATH. */
796 if (dst_death)
798 remove_death (REGNO (dst), dst_death);
799 REG_LIVE_LENGTH (REGNO (dst)) += length;
800 REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
801 REG_FREQ_CALLS_CROSSED (REGNO (dst)) += freq_calls;
804 if (dump_file)
805 fprintf (dump_file,
806 "Fixed operand of insn %d.\n",
807 INSN_UID (insn));
809 #ifdef AUTO_INC_DEC
810 for (p = PREV_INSN (insn); p; p = PREV_INSN (p))
812 if (! INSN_P (p))
813 continue;
814 if (BLOCK_FOR_INSN (p) != bb)
815 break;
816 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
818 if (try_auto_increment (p, insn, 0, dst, newconst, 0))
819 return 1;
820 break;
823 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
825 if (! INSN_P (p))
826 continue;
827 if (BLOCK_FOR_INSN (p) != bb)
828 break;
829 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
831 try_auto_increment (p, insn, 0, dst, newconst, 1);
832 break;
835 #endif
836 return 1;
840 if (reg_set_p (dst, PATTERN (p)))
841 break;
843 /* If we have passed a call instruction, and the
844 pseudo-reg SRC is not already live across a call,
845 then don't perform the optimization. */
846 /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
847 hard regs are clobbered. Thus, we only use it for src for
848 non-call insns. */
849 if (CALL_P (p))
851 if (! dst_death)
853 num_calls++;
854 freq_calls += REG_FREQ_FROM_BB (BLOCK_FOR_INSN (p));
857 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
858 break;
860 if ((HARD_REGISTER_P (dst) && call_used_regs [REGNO (dst)])
861 || find_reg_fusage (p, CLOBBER, dst))
862 break;
864 else if (reg_set_p (src, PATTERN (p)))
865 break;
868 return 0;
871 /* A forward pass. Replace output operands with input operands. */
873 static void
874 regmove_forward_pass (void)
876 basic_block bb;
877 rtx insn;
879 if (! flag_expensive_optimizations)
880 return;
882 if (dump_file)
883 fprintf (dump_file, "Starting forward pass...\n");
885 FOR_EACH_BB (bb)
887 FOR_BB_INSNS (bb, insn)
889 rtx set = single_set (insn);
890 if (! set)
891 continue;
893 if ((GET_CODE (SET_SRC (set)) == SIGN_EXTEND
894 || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
895 && REG_P (XEXP (SET_SRC (set), 0))
896 && REG_P (SET_DEST (set)))
897 optimize_reg_copy_3 (insn, SET_DEST (set), SET_SRC (set));
899 if (REG_P (SET_SRC (set))
900 && REG_P (SET_DEST (set)))
902 /* If this is a register-register copy where SRC is not dead,
903 see if we can optimize it. If this optimization succeeds,
904 it will become a copy where SRC is dead. */
905 if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
906 || optimize_reg_copy_1 (insn, SET_DEST (set), SET_SRC (set)))
907 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
909 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
910 if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
911 optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
912 if (regno_src_regno[REGNO (SET_DEST (set))] < 0
913 && SET_SRC (set) != SET_DEST (set))
915 int srcregno = REGNO (SET_SRC (set));
916 if (regno_src_regno[srcregno] >= 0)
917 srcregno = regno_src_regno[srcregno];
918 regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
926 /* A backward pass. Replace input operands with output operands. */
928 static void
929 regmove_backward_pass (void)
931 basic_block bb;
932 rtx insn, prev;
934 if (dump_file)
935 fprintf (dump_file, "Starting backward pass...\n");
937 FOR_EACH_BB_REVERSE (bb)
939 /* ??? Use the safe iterator because fixup_match_2 can remove
940 insns via try_auto_increment. */
941 FOR_BB_INSNS_REVERSE_SAFE (bb, insn, prev)
943 struct match match;
944 rtx copy_src, copy_dst;
945 int op_no, match_no;
946 int success = 0;
948 if (! INSN_P (insn))
949 continue;
951 if (! find_matches (insn, &match))
952 continue;
954 /* Now scan through the operands looking for a destination operand
955 which is supposed to match a source operand.
956 Then scan backward for an instruction which sets the source
957 operand. If safe, then replace the source operand with the
958 dest operand in both instructions. */
960 copy_src = NULL_RTX;
961 copy_dst = NULL_RTX;
962 for (op_no = 0; op_no < recog_data.n_operands; op_no++)
964 rtx set, p, src, dst;
965 rtx src_note, dst_note;
966 int num_calls = 0, freq_calls = 0;
967 enum reg_class src_class, dst_class;
968 int length;
970 match_no = match.with[op_no];
972 /* Nothing to do if the two operands aren't supposed to match. */
973 if (match_no < 0)
974 continue;
976 dst = recog_data.operand[match_no];
977 src = recog_data.operand[op_no];
979 if (!REG_P (src))
980 continue;
982 if (!REG_P (dst)
983 || REGNO (dst) < FIRST_PSEUDO_REGISTER
984 || REG_LIVE_LENGTH (REGNO (dst)) < 0
985 || GET_MODE (src) != GET_MODE (dst))
986 continue;
988 /* If the operands already match, then there is nothing to do. */
989 if (operands_match_p (src, dst))
990 continue;
992 if (match.commutative[op_no] >= 0)
994 rtx comm = recog_data.operand[match.commutative[op_no]];
995 if (operands_match_p (comm, dst))
996 continue;
999 set = single_set (insn);
1000 if (! set)
1001 continue;
1003 /* Note that single_set ignores parts of a parallel set for
1004 which one of the destinations is REG_UNUSED. We can't
1005 handle that here, since we can wind up rewriting things
1006 such that a single register is set twice within a single
1007 parallel. */
1008 if (reg_set_p (src, insn))
1009 continue;
1011 /* match_no/dst must be a write-only operand, and
1012 operand_operand/src must be a read-only operand. */
1013 if (match.use[op_no] != READ
1014 || match.use[match_no] != WRITE)
1015 continue;
1017 if (match.early_clobber[match_no]
1018 && count_occurrences (PATTERN (insn), src, 0) > 1)
1019 continue;
1021 /* Make sure match_no is the destination. */
1022 if (recog_data.operand[match_no] != SET_DEST (set))
1023 continue;
1025 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1027 if (GET_CODE (SET_SRC (set)) == PLUS
1028 && CONST_INT_P (XEXP (SET_SRC (set), 1))
1029 && XEXP (SET_SRC (set), 0) == src
1030 && fixup_match_2 (insn, dst, src,
1031 XEXP (SET_SRC (set), 1)))
1032 break;
1033 continue;
1035 src_class = reg_preferred_class (REGNO (src));
1036 dst_class = reg_preferred_class (REGNO (dst));
1038 if (! (src_note = find_reg_note (insn, REG_DEAD, src)))
1040 /* We used to force the copy here like in other cases, but
1041 it produces worse code, as it eliminates no copy
1042 instructions and the copy emitted will be produced by
1043 reload anyway. On patterns with multiple alternatives,
1044 there may be better solution available.
1046 In particular this change produced slower code for numeric
1047 i387 programs. */
1049 continue;
1052 if (! regclass_compatible_p (src_class, dst_class))
1054 if (!copy_src)
1056 copy_src = src;
1057 copy_dst = dst;
1059 continue;
1062 /* Can not modify an earlier insn to set dst if this insn
1063 uses an old value in the source. */
1064 if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
1066 if (!copy_src)
1068 copy_src = src;
1069 copy_dst = dst;
1071 continue;
1074 /* If src is set once in a different basic block,
1075 and is set equal to a constant, then do not use
1076 it for this optimization, as this would make it
1077 no longer equivalent to a constant. */
1079 if (reg_is_remote_constant_p (src, insn))
1081 if (!copy_src)
1083 copy_src = src;
1084 copy_dst = dst;
1086 continue;
1090 if (dump_file)
1091 fprintf (dump_file,
1092 "Could fix operand %d of insn %d matching operand %d.\n",
1093 op_no, INSN_UID (insn), match_no);
1095 /* Scan backward to find the first instruction that uses
1096 the input operand. If the operand is set here, then
1097 replace it in both instructions with match_no. */
1099 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
1101 rtx pset;
1103 if (! INSN_P (p))
1104 continue;
1105 if (BLOCK_FOR_INSN (p) != bb)
1106 break;
1108 if (!DEBUG_INSN_P (p))
1109 length++;
1111 /* ??? See if all of SRC is set in P. This test is much
1112 more conservative than it needs to be. */
1113 pset = single_set (p);
1114 if (pset && SET_DEST (pset) == src)
1116 /* We use validate_replace_rtx, in case there
1117 are multiple identical source operands. All
1118 of them have to be changed at the same time:
1119 when validate_replace_rtx() calls
1120 apply_change_group(). */
1121 validate_change (p, &SET_DEST (pset), dst, 1);
1122 if (validate_replace_rtx (src, dst, insn))
1123 success = 1;
1124 break;
1127 /* We can't make this change if DST is mentioned at
1128 all in P, since we are going to change its value.
1129 We can't make this change if SRC is read or
1130 partially written in P, since we are going to
1131 eliminate SRC. However, if it's a debug insn, we
1132 can't refrain from making the change, for this
1133 would cause codegen differences, so instead we
1134 invalidate debug expressions that reference DST,
1135 and adjust references to SRC in them so that they
1136 become references to DST. */
1137 if (reg_mentioned_p (dst, PATTERN (p)))
1139 if (DEBUG_INSN_P (p))
1140 validate_change (p, &INSN_VAR_LOCATION_LOC (p),
1141 gen_rtx_UNKNOWN_VAR_LOC (), 1);
1142 else
1143 break;
1145 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1147 if (DEBUG_INSN_P (p))
1148 validate_replace_rtx_group (src, dst, p);
1149 else
1150 break;
1153 /* If we have passed a call instruction, and the
1154 pseudo-reg DST is not already live across a call,
1155 then don't perform the optimization. */
1156 if (CALL_P (p))
1158 num_calls++;
1159 freq_calls += REG_FREQ_FROM_BB (BLOCK_FOR_INSN (p));
1161 if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
1162 break;
1166 if (success)
1168 int dstno, srcno;
1170 /* Remove the death note for SRC from INSN. */
1171 remove_note (insn, src_note);
1172 /* Move the death note for SRC to P if it is used
1173 there. */
1174 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1176 XEXP (src_note, 1) = REG_NOTES (p);
1177 REG_NOTES (p) = src_note;
1179 /* If there is a REG_DEAD note for DST on P, then remove
1180 it, because DST is now set there. */
1181 if ((dst_note = find_reg_note (p, REG_DEAD, dst)))
1182 remove_note (p, dst_note);
1184 dstno = REGNO (dst);
1185 srcno = REGNO (src);
1187 INC_REG_N_SETS (dstno, 1);
1188 INC_REG_N_SETS (srcno, -1);
1190 REG_N_CALLS_CROSSED (dstno) += num_calls;
1191 REG_N_CALLS_CROSSED (srcno) -= num_calls;
1192 REG_FREQ_CALLS_CROSSED (dstno) += freq_calls;
1193 REG_FREQ_CALLS_CROSSED (srcno) -= freq_calls;
1195 REG_LIVE_LENGTH (dstno) += length;
1196 if (REG_LIVE_LENGTH (srcno) >= 0)
1198 REG_LIVE_LENGTH (srcno) -= length;
1199 /* REG_LIVE_LENGTH is only an approximation after
1200 combine if sched is not run, so make sure that we
1201 still have a reasonable value. */
1202 if (REG_LIVE_LENGTH (srcno) < 2)
1203 REG_LIVE_LENGTH (srcno) = 2;
1206 if (dump_file)
1207 fprintf (dump_file,
1208 "Fixed operand %d of insn %d matching operand %d.\n",
1209 op_no, INSN_UID (insn), match_no);
1211 break;
1213 else if (num_changes_pending () > 0)
1214 cancel_changes (0);
1217 /* If we weren't able to replace any of the alternatives, try an
1218 alternative approach of copying the source to the destination. */
1219 if (!success && copy_src != NULL_RTX)
1220 copy_src_to_dest (insn, copy_src, copy_dst);
1225 /* Main entry for the register move optimization. */
1227 static unsigned int
1228 regmove_optimize (void)
1230 int i;
1231 int nregs = max_reg_num ();
1233 df_note_add_problem ();
1234 df_analyze ();
1236 regstat_init_n_sets_and_refs ();
1237 regstat_compute_ri ();
1239 if (flag_ira_loop_pressure)
1240 ira_set_pseudo_classes (dump_file);
1242 regno_src_regno = XNEWVEC (int, nregs);
1243 for (i = nregs; --i >= 0; )
1244 regno_src_regno[i] = -1;
1246 /* A forward pass. Replace output operands with input operands. */
1247 regmove_forward_pass ();
1249 /* A backward pass. Replace input operands with output operands. */
1250 regmove_backward_pass ();
1252 /* Clean up. */
1253 free (regno_src_regno);
1254 if (reg_set_in_bb)
1256 free (reg_set_in_bb);
1257 reg_set_in_bb = NULL;
1259 regstat_free_n_sets_and_refs ();
1260 regstat_free_ri ();
1261 if (flag_ira_loop_pressure)
1262 free_reg_info ();
1263 return 0;
1266 /* Returns nonzero if INSN's pattern has matching constraints for any operand.
1267 Returns 0 if INSN can't be recognized, or if the alternative can't be
1268 determined.
1270 Initialize the info in MATCHP based on the constraints. */
1272 static int
1273 find_matches (rtx insn, struct match *matchp)
1275 int likely_spilled[MAX_RECOG_OPERANDS];
1276 int op_no;
1277 int any_matches = 0;
1279 extract_insn (insn);
1280 if (! constrain_operands (0))
1281 return 0;
1283 /* Must initialize this before main loop, because the code for
1284 the commutative case may set matches for operands other than
1285 the current one. */
1286 for (op_no = recog_data.n_operands; --op_no >= 0; )
1287 matchp->with[op_no] = matchp->commutative[op_no] = -1;
1289 for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1291 const char *p;
1292 char c;
1293 int i = 0;
1295 p = recog_data.constraints[op_no];
1297 likely_spilled[op_no] = 0;
1298 matchp->use[op_no] = READ;
1299 matchp->early_clobber[op_no] = 0;
1300 if (*p == '=')
1301 matchp->use[op_no] = WRITE;
1302 else if (*p == '+')
1303 matchp->use[op_no] = READWRITE;
1305 for (;*p && i < which_alternative; p++)
1306 if (*p == ',')
1307 i++;
1309 while ((c = *p) != '\0' && c != ',')
1311 switch (c)
1313 case '=':
1314 break;
1315 case '+':
1316 break;
1317 case '&':
1318 matchp->early_clobber[op_no] = 1;
1319 break;
1320 case '%':
1321 matchp->commutative[op_no] = op_no + 1;
1322 matchp->commutative[op_no + 1] = op_no;
1323 break;
1325 case '0': case '1': case '2': case '3': case '4':
1326 case '5': case '6': case '7': case '8': case '9':
1328 char *end;
1329 unsigned long match_ul = strtoul (p, &end, 10);
1330 int match = match_ul;
1332 p = end;
1334 if (match < op_no && likely_spilled[match])
1335 continue;
1336 matchp->with[op_no] = match;
1337 any_matches = 1;
1338 if (matchp->commutative[op_no] >= 0)
1339 matchp->with[matchp->commutative[op_no]] = match;
1341 continue;
1343 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1344 case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1345 case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1346 case 'C': case 'D': case 'W': case 'Y': case 'Z':
1347 if (targetm.class_likely_spilled_p (REG_CLASS_FROM_CONSTRAINT ((unsigned char) c, p)))
1348 likely_spilled[op_no] = 1;
1349 break;
1351 p += CONSTRAINT_LEN (c, p);
1354 return any_matches;
1359 static bool
1360 gate_handle_regmove (void)
1362 return (optimize > 0 && flag_regmove);
1366 struct rtl_opt_pass pass_regmove =
1369 RTL_PASS,
1370 "regmove", /* name */
1371 gate_handle_regmove, /* gate */
1372 regmove_optimize, /* execute */
1373 NULL, /* sub */
1374 NULL, /* next */
1375 0, /* static_pass_number */
1376 TV_REGMOVE, /* tv_id */
1377 0, /* properties_required */
1378 0, /* properties_provided */
1379 0, /* properties_destroyed */
1380 0, /* todo_flags_start */
1381 TODO_df_finish | TODO_verify_rtl_sharing |
1382 TODO_ggc_collect /* todo_flags_finish */