Multiple exit loop handling in ivopts. Regression tested on x86-64/linux
[official-gcc.git] / gcc / regmove.c
blobeb205f89ee981ab7841d979baf9fe19d54ca5017
1 /* Move registers around to reduce number of move instructions needed.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 "output.h"
36 #include "regs.h"
37 #include "hard-reg-set.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "expr.h"
41 #include "basic-block.h"
42 #include "except.h"
43 #include "diagnostic-core.h"
44 #include "toplev.h"
45 #include "reload.h"
46 #include "timevar.h"
47 #include "tree-pass.h"
48 #include "df.h"
49 #include "ira.h"
51 static int optimize_reg_copy_1 (rtx, rtx, rtx);
52 static void optimize_reg_copy_2 (rtx, rtx, rtx);
53 static void optimize_reg_copy_3 (rtx, rtx, rtx);
54 static void copy_src_to_dest (rtx, rtx, rtx);
56 enum match_use
58 READ,
59 WRITE,
60 READWRITE
63 struct match {
64 int with[MAX_RECOG_OPERANDS];
65 enum match_use use[MAX_RECOG_OPERANDS];
66 int commutative[MAX_RECOG_OPERANDS];
67 int early_clobber[MAX_RECOG_OPERANDS];
70 static int find_matches (rtx, struct match *);
71 static int fixup_match_2 (rtx, rtx, rtx, rtx);
73 /* Return nonzero if registers with CLASS1 and CLASS2 can be merged without
74 causing too much register allocation problems. */
75 static int
76 regclass_compatible_p (enum reg_class class0, enum reg_class class1)
78 return (class0 == class1
79 || (reg_class_subset_p (class0, class1)
80 && ! CLASS_LIKELY_SPILLED_P (class0))
81 || (reg_class_subset_p (class1, class0)
82 && ! CLASS_LIKELY_SPILLED_P (class1)));
86 #ifdef AUTO_INC_DEC
88 /* Find the place in the rtx X where REG is used as a memory address.
89 Return the MEM rtx that so uses it.
90 If PLUSCONST is nonzero, search instead for a memory address equivalent to
91 (plus REG (const_int PLUSCONST)).
93 If such an address does not appear, return 0.
94 If REG appears more than once, or is used other than in such an address,
95 return (rtx) 1. */
97 static rtx
98 find_use_as_address (rtx x, rtx reg, HOST_WIDE_INT plusconst)
100 enum rtx_code code = GET_CODE (x);
101 const char * const fmt = GET_RTX_FORMAT (code);
102 int i;
103 rtx value = 0;
104 rtx tem;
106 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
107 return x;
109 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
110 && XEXP (XEXP (x, 0), 0) == reg
111 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
112 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
113 return x;
115 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
117 /* If REG occurs inside a MEM used in a bit-field reference,
118 that is unacceptable. */
119 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
120 return (rtx) (size_t) 1;
123 if (x == reg)
124 return (rtx) (size_t) 1;
126 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
128 if (fmt[i] == 'e')
130 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
131 if (value == 0)
132 value = tem;
133 else if (tem != 0)
134 return (rtx) (size_t) 1;
136 else if (fmt[i] == 'E')
138 int j;
139 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
141 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
142 if (value == 0)
143 value = tem;
144 else if (tem != 0)
145 return (rtx) (size_t) 1;
150 return value;
154 /* INC_INSN is an instruction that adds INCREMENT to REG.
155 Try to fold INC_INSN as a post/pre in/decrement into INSN.
156 Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
157 Return nonzero for success. */
158 static int
159 try_auto_increment (rtx insn, rtx inc_insn, rtx inc_insn_set, rtx reg,
160 HOST_WIDE_INT increment, int pre)
162 enum rtx_code inc_code;
164 rtx pset = single_set (insn);
165 if (pset)
167 /* Can't use the size of SET_SRC, we might have something like
168 (sign_extend:SI (mem:QI ... */
169 rtx use = find_use_as_address (pset, reg, 0);
170 if (use != 0 && use != (rtx) (size_t) 1)
172 int size = GET_MODE_SIZE (GET_MODE (use));
173 if (0
174 || (HAVE_POST_INCREMENT
175 && pre == 0 && (inc_code = POST_INC, increment == size))
176 || (HAVE_PRE_INCREMENT
177 && pre == 1 && (inc_code = PRE_INC, increment == size))
178 || (HAVE_POST_DECREMENT
179 && pre == 0 && (inc_code = POST_DEC, increment == -size))
180 || (HAVE_PRE_DECREMENT
181 && pre == 1 && (inc_code = PRE_DEC, increment == -size))
184 if (inc_insn_set)
185 validate_change
186 (inc_insn,
187 &SET_SRC (inc_insn_set),
188 XEXP (SET_SRC (inc_insn_set), 0), 1);
189 validate_change (insn, &XEXP (use, 0),
190 gen_rtx_fmt_e (inc_code,
191 GET_MODE (XEXP (use, 0)), reg),
193 if (apply_change_group ())
195 /* If there is a REG_DEAD note on this insn, we must
196 change this not to REG_UNUSED meaning that the register
197 is set, but the value is dead. Failure to do so will
198 result in sched1 dying -- when it recomputes lifetime
199 information, the number of REG_DEAD notes will have
200 changed. */
201 rtx note = find_reg_note (insn, REG_DEAD, reg);
202 if (note)
203 PUT_REG_NOTE_KIND (note, REG_UNUSED);
205 add_reg_note (insn, REG_INC, reg);
207 if (! inc_insn_set)
208 delete_insn (inc_insn);
209 return 1;
214 return 0;
216 #endif
219 static int *regno_src_regno;
221 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
222 in INSN.
224 Search forward to see if SRC dies before either it or DEST is modified,
225 but don't scan past the end of a basic block. If so, we can replace SRC
226 with DEST and let SRC die in INSN.
228 This will reduce the number of registers live in that range and may enable
229 DEST to be tied to SRC, thus often saving one register in addition to a
230 register-register copy. */
232 static int
233 optimize_reg_copy_1 (rtx insn, rtx dest, rtx src)
235 rtx p, q;
236 rtx note;
237 rtx dest_death = 0;
238 int sregno = REGNO (src);
239 int dregno = REGNO (dest);
240 basic_block bb = BLOCK_FOR_INSN (insn);
242 /* We don't want to mess with hard regs if register classes are small. */
243 if (sregno == dregno
244 || (targetm.small_register_classes_for_mode_p (GET_MODE (src))
245 && (sregno < FIRST_PSEUDO_REGISTER
246 || dregno < FIRST_PSEUDO_REGISTER))
247 /* We don't see all updates to SP if they are in an auto-inc memory
248 reference, so we must disallow this optimization on them. */
249 || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
250 return 0;
252 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
254 if (! INSN_P (p))
255 continue;
256 if (BLOCK_FOR_INSN (p) != bb)
257 break;
259 if (reg_set_p (src, p) || reg_set_p (dest, p)
260 /* If SRC is an asm-declared register, it must not be replaced
261 in any asm. Unfortunately, the REG_EXPR tree for the asm
262 variable may be absent in the SRC rtx, so we can't check the
263 actual register declaration easily (the asm operand will have
264 it, though). To avoid complicating the test for a rare case,
265 we just don't perform register replacement for a hard reg
266 mentioned in an asm. */
267 || (sregno < FIRST_PSEUDO_REGISTER
268 && asm_noperands (PATTERN (p)) >= 0
269 && reg_overlap_mentioned_p (src, PATTERN (p)))
270 /* Don't change hard registers used by a call. */
271 || (CALL_P (p) && sregno < FIRST_PSEUDO_REGISTER
272 && find_reg_fusage (p, USE, src))
273 /* Don't change a USE of a register. */
274 || (GET_CODE (PATTERN (p)) == USE
275 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
276 break;
278 /* See if all of SRC dies in P. This test is slightly more
279 conservative than it needs to be. */
280 if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
281 && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
283 int failed = 0;
284 int d_length = 0;
285 int s_length = 0;
286 int d_n_calls = 0;
287 int s_n_calls = 0;
288 int s_freq_calls = 0;
289 int d_freq_calls = 0;
291 /* We can do the optimization. Scan forward from INSN again,
292 replacing regs as we go. Set FAILED if a replacement can't
293 be done. In that case, we can't move the death note for SRC.
294 This should be rare. */
296 /* Set to stop at next insn. */
297 for (q = next_real_insn (insn);
298 q != next_real_insn (p);
299 q = next_real_insn (q))
301 if (reg_overlap_mentioned_p (src, PATTERN (q)))
303 /* If SRC is a hard register, we might miss some
304 overlapping registers with validate_replace_rtx,
305 so we would have to undo it. We can't if DEST is
306 present in the insn, so fail in that combination
307 of cases. */
308 if (sregno < FIRST_PSEUDO_REGISTER
309 && reg_mentioned_p (dest, PATTERN (q)))
310 failed = 1;
312 /* Attempt to replace all uses. */
313 else if (!validate_replace_rtx (src, dest, q))
314 failed = 1;
316 /* If this succeeded, but some part of the register
317 is still present, undo the replacement. */
318 else if (sregno < FIRST_PSEUDO_REGISTER
319 && reg_overlap_mentioned_p (src, PATTERN (q)))
321 validate_replace_rtx (dest, src, q);
322 failed = 1;
326 /* For SREGNO, count the total number of insns scanned.
327 For DREGNO, count the total number of insns scanned after
328 passing the death note for DREGNO. */
329 if (!DEBUG_INSN_P (p))
331 s_length++;
332 if (dest_death)
333 d_length++;
336 /* If the insn in which SRC dies is a CALL_INSN, don't count it
337 as a call that has been crossed. Otherwise, count it. */
338 if (q != p && CALL_P (q))
340 /* Similarly, total calls for SREGNO, total calls beyond
341 the death note for DREGNO. */
342 s_n_calls++;
343 s_freq_calls += REG_FREQ_FROM_BB (BLOCK_FOR_INSN (q));
344 if (dest_death)
346 d_n_calls++;
347 d_freq_calls += REG_FREQ_FROM_BB (BLOCK_FOR_INSN (q));
351 /* If DEST dies here, remove the death note and save it for
352 later. Make sure ALL of DEST dies here; again, this is
353 overly conservative. */
354 if (dest_death == 0
355 && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0)
357 if (GET_MODE (XEXP (dest_death, 0)) != GET_MODE (dest))
358 failed = 1, dest_death = 0;
359 else
360 remove_note (q, dest_death);
364 if (! failed)
366 /* These counters need to be updated if and only if we are
367 going to move the REG_DEAD note. */
368 if (sregno >= FIRST_PSEUDO_REGISTER)
370 if (REG_LIVE_LENGTH (sregno) >= 0)
372 REG_LIVE_LENGTH (sregno) -= s_length;
373 /* REG_LIVE_LENGTH is only an approximation after
374 combine if sched is not run, so make sure that we
375 still have a reasonable value. */
376 if (REG_LIVE_LENGTH (sregno) < 2)
377 REG_LIVE_LENGTH (sregno) = 2;
380 REG_N_CALLS_CROSSED (sregno) -= s_n_calls;
381 REG_FREQ_CALLS_CROSSED (sregno) -= s_freq_calls;
384 /* Move death note of SRC from P to INSN. */
385 remove_note (p, note);
386 XEXP (note, 1) = REG_NOTES (insn);
387 REG_NOTES (insn) = note;
390 /* DEST is also dead if INSN has a REG_UNUSED note for DEST. */
391 if (! dest_death
392 && (dest_death = find_regno_note (insn, REG_UNUSED, dregno)))
394 PUT_REG_NOTE_KIND (dest_death, REG_DEAD);
395 remove_note (insn, dest_death);
398 /* Put death note of DEST on P if we saw it die. */
399 if (dest_death)
401 XEXP (dest_death, 1) = REG_NOTES (p);
402 REG_NOTES (p) = dest_death;
404 if (dregno >= FIRST_PSEUDO_REGISTER)
406 /* If and only if we are moving the death note for DREGNO,
407 then we need to update its counters. */
408 if (REG_LIVE_LENGTH (dregno) >= 0)
409 REG_LIVE_LENGTH (dregno) += d_length;
410 REG_N_CALLS_CROSSED (dregno) += d_n_calls;
411 REG_FREQ_CALLS_CROSSED (dregno) += d_freq_calls;
415 return ! failed;
418 /* If SRC is a hard register which is set or killed in some other
419 way, we can't do this optimization. */
420 else if (sregno < FIRST_PSEUDO_REGISTER
421 && dead_or_set_p (p, src))
422 break;
424 return 0;
427 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
428 a sequence of insns that modify DEST followed by an insn that sets
429 SRC to DEST in which DEST dies, with no prior modification of DEST.
430 (There is no need to check if the insns in between actually modify
431 DEST. We should not have cases where DEST is not modified, but
432 the optimization is safe if no such modification is detected.)
433 In that case, we can replace all uses of DEST, starting with INSN and
434 ending with the set of SRC to DEST, with SRC. We do not do this
435 optimization if a CALL_INSN is crossed unless SRC already crosses a
436 call or if DEST dies before the copy back to SRC.
438 It is assumed that DEST and SRC are pseudos; it is too complicated to do
439 this for hard registers since the substitutions we may make might fail. */
441 static void
442 optimize_reg_copy_2 (rtx insn, rtx dest, rtx src)
444 rtx p, q;
445 rtx set;
446 int sregno = REGNO (src);
447 int dregno = REGNO (dest);
448 basic_block bb = BLOCK_FOR_INSN (insn);
450 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
452 if (! INSN_P (p))
453 continue;
454 if (BLOCK_FOR_INSN (p) != bb)
455 break;
457 set = single_set (p);
458 if (set && SET_SRC (set) == dest && SET_DEST (set) == src
459 && find_reg_note (p, REG_DEAD, dest))
461 /* We can do the optimization. Scan forward from INSN again,
462 replacing regs as we go. */
464 /* Set to stop at next insn. */
465 for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
466 if (INSN_P (q))
468 if (reg_mentioned_p (dest, PATTERN (q)))
470 rtx note;
472 PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
473 note = FIND_REG_INC_NOTE (q, dest);
474 if (note)
476 remove_note (q, note);
477 add_reg_note (q, REG_INC, src);
479 df_insn_rescan (q);
482 if (CALL_P (q))
484 int freq = REG_FREQ_FROM_BB (BLOCK_FOR_INSN (q));
485 REG_N_CALLS_CROSSED (dregno)--;
486 REG_N_CALLS_CROSSED (sregno)++;
487 REG_FREQ_CALLS_CROSSED (dregno) -= freq;
488 REG_FREQ_CALLS_CROSSED (sregno) += freq;
492 remove_note (p, find_reg_note (p, REG_DEAD, dest));
493 REG_N_DEATHS (dregno)--;
494 remove_note (insn, find_reg_note (insn, REG_DEAD, src));
495 REG_N_DEATHS (sregno)--;
496 return;
499 if (reg_set_p (src, p)
500 || find_reg_note (p, REG_DEAD, dest)
501 || (CALL_P (p) && REG_N_CALLS_CROSSED (sregno) == 0))
502 break;
506 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
507 Look if SRC dies there, and if it is only set once, by loading
508 it from memory. If so, try to incorporate the zero/sign extension
509 into the memory read, change SRC to the mode of DEST, and alter
510 the remaining accesses to use the appropriate SUBREG. This allows
511 SRC and DEST to be tied later. */
512 static void
513 optimize_reg_copy_3 (rtx insn, rtx dest, rtx src)
515 rtx src_reg = XEXP (src, 0);
516 int src_no = REGNO (src_reg);
517 int dst_no = REGNO (dest);
518 rtx p, set;
519 enum machine_mode old_mode;
520 basic_block bb = BLOCK_FOR_INSN (insn);
522 if (src_no < FIRST_PSEUDO_REGISTER
523 || dst_no < FIRST_PSEUDO_REGISTER
524 || ! find_reg_note (insn, REG_DEAD, src_reg)
525 || REG_N_DEATHS (src_no) != 1
526 || REG_N_SETS (src_no) != 1)
527 return;
529 for (p = PREV_INSN (insn); p && ! reg_set_p (src_reg, p); p = PREV_INSN (p))
530 if (INSN_P (p) && BLOCK_FOR_INSN (p) != bb)
531 break;
533 if (! p || BLOCK_FOR_INSN (p) != bb)
534 return;
536 if (! (set = single_set (p))
537 || !MEM_P (SET_SRC (set))
538 /* If there's a REG_EQUIV note, this must be an insn that loads an
539 argument. Prefer keeping the note over doing this optimization. */
540 || find_reg_note (p, REG_EQUIV, NULL_RTX)
541 || SET_DEST (set) != src_reg)
542 return;
544 /* Be conservative: although this optimization is also valid for
545 volatile memory references, that could cause trouble in later passes. */
546 if (MEM_VOLATILE_P (SET_SRC (set)))
547 return;
549 /* Do not use a SUBREG to truncate from one mode to another if truncation
550 is not a nop. */
551 if (GET_MODE_BITSIZE (GET_MODE (src_reg)) <= GET_MODE_BITSIZE (GET_MODE (src))
552 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (src)),
553 GET_MODE_BITSIZE (GET_MODE (src_reg))))
554 return;
556 old_mode = GET_MODE (src_reg);
557 PUT_MODE (src_reg, GET_MODE (src));
558 XEXP (src, 0) = SET_SRC (set);
560 /* Include this change in the group so that it's easily undone if
561 one of the changes in the group is invalid. */
562 validate_change (p, &SET_SRC (set), src, 1);
564 /* Now walk forward making additional replacements. We want to be able
565 to undo all the changes if a later substitution fails. */
566 while (p = NEXT_INSN (p), p != insn)
568 if (! INSN_P (p))
569 continue;
571 /* Make a tentative change. */
572 validate_replace_rtx_group (src_reg,
573 gen_lowpart_SUBREG (old_mode, src_reg),
577 validate_replace_rtx_group (src, src_reg, insn);
579 /* Now see if all the changes are valid. */
580 if (! apply_change_group ())
582 /* One or more changes were no good. Back out everything. */
583 PUT_MODE (src_reg, old_mode);
584 XEXP (src, 0) = src_reg;
586 else
588 rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
589 if (note)
590 remove_note (p, note);
595 /* If we were not able to update the users of src to use dest directly, try
596 instead moving the value to dest directly before the operation. */
598 static void
599 copy_src_to_dest (rtx insn, rtx src, rtx dest)
601 rtx seq;
602 rtx link;
603 rtx next;
604 rtx set;
605 rtx move_insn;
606 rtx *p_insn_notes;
607 rtx *p_move_notes;
608 int src_regno;
609 int dest_regno;
611 /* A REG_LIVE_LENGTH of -1 indicates the register is equivalent to a constant
612 or memory location and is used infrequently; a REG_LIVE_LENGTH of -2 is
613 parameter when there is no frame pointer that is not allocated a register.
614 For now, we just reject them, rather than incrementing the live length. */
616 if (REG_P (src)
617 && REG_LIVE_LENGTH (REGNO (src)) > 0
618 && REG_P (dest)
619 && REG_LIVE_LENGTH (REGNO (dest)) > 0
620 && (set = single_set (insn)) != NULL_RTX
621 && !reg_mentioned_p (dest, SET_SRC (set))
622 && GET_MODE (src) == GET_MODE (dest))
624 int old_num_regs = reg_rtx_no;
626 /* Generate the src->dest move. */
627 start_sequence ();
628 emit_move_insn (dest, src);
629 seq = get_insns ();
630 end_sequence ();
631 /* If this sequence uses new registers, we may not use it. */
632 if (old_num_regs != reg_rtx_no
633 || ! validate_replace_rtx (src, dest, insn))
635 /* We have to restore reg_rtx_no to its old value, lest
636 recompute_reg_usage will try to compute the usage of the
637 new regs, yet reg_n_info is not valid for them. */
638 reg_rtx_no = old_num_regs;
639 return;
641 emit_insn_before (seq, insn);
642 move_insn = PREV_INSN (insn);
643 p_move_notes = &REG_NOTES (move_insn);
644 p_insn_notes = &REG_NOTES (insn);
646 /* Move any notes mentioning src to the move instruction. */
647 for (link = REG_NOTES (insn); link != NULL_RTX; link = next)
649 next = XEXP (link, 1);
650 if (XEXP (link, 0) == src)
652 *p_move_notes = link;
653 p_move_notes = &XEXP (link, 1);
655 else
657 *p_insn_notes = link;
658 p_insn_notes = &XEXP (link, 1);
662 *p_move_notes = NULL_RTX;
663 *p_insn_notes = NULL_RTX;
665 /* Update the various register tables. */
666 dest_regno = REGNO (dest);
667 INC_REG_N_SETS (dest_regno, 1);
668 REG_LIVE_LENGTH (dest_regno)++;
669 src_regno = REGNO (src);
670 if (! find_reg_note (move_insn, REG_DEAD, src))
671 REG_LIVE_LENGTH (src_regno)++;
675 /* reg_set_in_bb[REGNO] points to basic block iff the register is set
676 only once in the given block and has REG_EQUAL note. */
678 static basic_block *reg_set_in_bb;
680 /* Size of reg_set_in_bb array. */
681 static unsigned int max_reg_computed;
684 /* Return whether REG is set in only one location, and is set to a
685 constant, but is set in a different basic block from INSN (an
686 instructions which uses REG). In this case REG is equivalent to a
687 constant, and we don't want to break that equivalence, because that
688 may increase register pressure and make reload harder. If REG is
689 set in the same basic block as INSN, we don't worry about it,
690 because we'll probably need a register anyhow (??? but what if REG
691 is used in a different basic block as well as this one?). */
693 static bool
694 reg_is_remote_constant_p (rtx reg, rtx insn)
696 basic_block bb;
697 rtx p;
698 int max;
700 if (!reg_set_in_bb)
702 max_reg_computed = max = max_reg_num ();
703 reg_set_in_bb = XCNEWVEC (basic_block, max);
705 FOR_EACH_BB (bb)
706 FOR_BB_INSNS (bb, p)
708 rtx s;
710 if (!INSN_P (p))
711 continue;
712 s = single_set (p);
713 /* This is the instruction which sets REG. If there is a
714 REG_EQUAL note, then REG is equivalent to a constant. */
715 if (s != 0
716 && REG_P (SET_DEST (s))
717 && REG_N_SETS (REGNO (SET_DEST (s))) == 1
718 && find_reg_note (p, REG_EQUAL, NULL_RTX))
719 reg_set_in_bb[REGNO (SET_DEST (s))] = bb;
723 gcc_assert (REGNO (reg) < max_reg_computed);
724 if (reg_set_in_bb[REGNO (reg)] == NULL)
725 return false;
726 return (reg_set_in_bb[REGNO (reg)] != BLOCK_FOR_INSN (insn));
729 /* INSN is adding a CONST_INT to a REG. We search backwards looking for
730 another add immediate instruction with the same source and dest registers,
731 and if we find one, we change INSN to an increment, and return 1. If
732 no changes are made, we return 0.
734 This changes
735 (set (reg100) (plus reg1 offset1))
737 (set (reg100) (plus reg1 offset2))
739 (set (reg100) (plus reg1 offset1))
741 (set (reg100) (plus reg100 offset2-offset1)) */
743 /* ??? What does this comment mean? */
744 /* cse disrupts preincrement / postdecrement sequences when it finds a
745 hard register as ultimate source, like the frame pointer. */
747 static int
748 fixup_match_2 (rtx insn, rtx dst, rtx src, rtx offset)
750 rtx p, dst_death = 0;
751 int length, num_calls = 0, freq_calls = 0;
752 basic_block bb = BLOCK_FOR_INSN (insn);
754 /* If SRC dies in INSN, we'd have to move the death note. This is
755 considered to be very unlikely, so we just skip the optimization
756 in this case. */
757 if (find_regno_note (insn, REG_DEAD, REGNO (src)))
758 return 0;
760 /* Scan backward to find the first instruction that sets DST. */
762 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
764 rtx pset;
766 if (! INSN_P (p))
767 continue;
768 if (BLOCK_FOR_INSN (p) != bb)
769 break;
771 if (find_regno_note (p, REG_DEAD, REGNO (dst)))
772 dst_death = p;
773 if (! dst_death && !DEBUG_INSN_P (p))
774 length++;
776 pset = single_set (p);
777 if (pset && SET_DEST (pset) == dst
778 && GET_CODE (SET_SRC (pset)) == PLUS
779 && XEXP (SET_SRC (pset), 0) == src
780 && CONST_INT_P (XEXP (SET_SRC (pset), 1)))
782 HOST_WIDE_INT newconst
783 = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
784 rtx add = gen_add3_insn (dst, dst, GEN_INT (newconst));
786 if (add && validate_change (insn, &PATTERN (insn), add, 0))
788 /* Remove the death note for DST from DST_DEATH. */
789 if (dst_death)
791 remove_death (REGNO (dst), dst_death);
792 REG_LIVE_LENGTH (REGNO (dst)) += length;
793 REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
794 REG_FREQ_CALLS_CROSSED (REGNO (dst)) += freq_calls;
797 if (dump_file)
798 fprintf (dump_file,
799 "Fixed operand of insn %d.\n",
800 INSN_UID (insn));
802 #ifdef AUTO_INC_DEC
803 for (p = PREV_INSN (insn); p; p = PREV_INSN (p))
805 if (! INSN_P (p))
806 continue;
807 if (BLOCK_FOR_INSN (p) != bb)
808 break;
809 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
811 if (try_auto_increment (p, insn, 0, dst, newconst, 0))
812 return 1;
813 break;
816 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
818 if (! INSN_P (p))
819 continue;
820 if (BLOCK_FOR_INSN (p) != bb)
821 break;
822 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
824 try_auto_increment (p, insn, 0, dst, newconst, 1);
825 break;
828 #endif
829 return 1;
833 if (reg_set_p (dst, PATTERN (p)))
834 break;
836 /* If we have passed a call instruction, and the
837 pseudo-reg SRC is not already live across a call,
838 then don't perform the optimization. */
839 /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
840 hard regs are clobbered. Thus, we only use it for src for
841 non-call insns. */
842 if (CALL_P (p))
844 if (! dst_death)
846 num_calls++;
847 freq_calls += REG_FREQ_FROM_BB (BLOCK_FOR_INSN (p));
850 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
851 break;
853 if (call_used_regs [REGNO (dst)]
854 || find_reg_fusage (p, CLOBBER, dst))
855 break;
857 else if (reg_set_p (src, PATTERN (p)))
858 break;
861 return 0;
864 /* A forward pass. Replace output operands with input operands. */
866 static void
867 regmove_forward_pass (void)
869 basic_block bb;
870 rtx insn;
872 if (! flag_expensive_optimizations)
873 return;
875 if (dump_file)
876 fprintf (dump_file, "Starting forward pass...\n");
878 FOR_EACH_BB (bb)
880 FOR_BB_INSNS (bb, insn)
882 rtx set = single_set (insn);
883 if (! set)
884 continue;
886 if ((GET_CODE (SET_SRC (set)) == SIGN_EXTEND
887 || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
888 && REG_P (XEXP (SET_SRC (set), 0))
889 && REG_P (SET_DEST (set)))
890 optimize_reg_copy_3 (insn, SET_DEST (set), SET_SRC (set));
892 if (REG_P (SET_SRC (set))
893 && REG_P (SET_DEST (set)))
895 /* If this is a register-register copy where SRC is not dead,
896 see if we can optimize it. If this optimization succeeds,
897 it will become a copy where SRC is dead. */
898 if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
899 || optimize_reg_copy_1 (insn, SET_DEST (set), SET_SRC (set)))
900 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
902 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
903 if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
904 optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
905 if (regno_src_regno[REGNO (SET_DEST (set))] < 0
906 && SET_SRC (set) != SET_DEST (set))
908 int srcregno = REGNO (SET_SRC (set));
909 if (regno_src_regno[srcregno] >= 0)
910 srcregno = regno_src_regno[srcregno];
911 regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
919 /* A backward pass. Replace input operands with output operands. */
921 static void
922 regmove_backward_pass (void)
924 basic_block bb;
925 rtx insn, prev;
927 if (dump_file)
928 fprintf (dump_file, "Starting backward pass...\n");
930 FOR_EACH_BB_REVERSE (bb)
932 /* ??? Use the safe iterator because fixup_match_2 can remove
933 insns via try_auto_increment. */
934 FOR_BB_INSNS_REVERSE_SAFE (bb, insn, prev)
936 struct match match;
937 rtx copy_src, copy_dst;
938 int op_no, match_no;
939 int success = 0;
941 if (! INSN_P (insn))
942 continue;
944 if (! find_matches (insn, &match))
945 continue;
947 /* Now scan through the operands looking for a destination operand
948 which is supposed to match a source operand.
949 Then scan backward for an instruction which sets the source
950 operand. If safe, then replace the source operand with the
951 dest operand in both instructions. */
953 copy_src = NULL_RTX;
954 copy_dst = NULL_RTX;
955 for (op_no = 0; op_no < recog_data.n_operands; op_no++)
957 rtx set, p, src, dst;
958 rtx src_note, dst_note;
959 int num_calls = 0, freq_calls = 0;
960 enum reg_class src_class, dst_class;
961 int length;
963 match_no = match.with[op_no];
965 /* Nothing to do if the two operands aren't supposed to match. */
966 if (match_no < 0)
967 continue;
969 dst = recog_data.operand[match_no];
970 src = recog_data.operand[op_no];
972 if (!REG_P (src))
973 continue;
975 if (!REG_P (dst)
976 || REGNO (dst) < FIRST_PSEUDO_REGISTER
977 || REG_LIVE_LENGTH (REGNO (dst)) < 0
978 || GET_MODE (src) != GET_MODE (dst))
979 continue;
981 /* If the operands already match, then there is nothing to do. */
982 if (operands_match_p (src, dst))
983 continue;
985 if (match.commutative[op_no] >= 0)
987 rtx comm = recog_data.operand[match.commutative[op_no]];
988 if (operands_match_p (comm, dst))
989 continue;
992 set = single_set (insn);
993 if (! set)
994 continue;
996 /* Note that single_set ignores parts of a parallel set for
997 which one of the destinations is REG_UNUSED. We can't
998 handle that here, since we can wind up rewriting things
999 such that a single register is set twice within a single
1000 parallel. */
1001 if (reg_set_p (src, insn))
1002 continue;
1004 /* match_no/dst must be a write-only operand, and
1005 operand_operand/src must be a read-only operand. */
1006 if (match.use[op_no] != READ
1007 || match.use[match_no] != WRITE)
1008 continue;
1010 if (match.early_clobber[match_no]
1011 && count_occurrences (PATTERN (insn), src, 0) > 1)
1012 continue;
1014 /* Make sure match_no is the destination. */
1015 if (recog_data.operand[match_no] != SET_DEST (set))
1016 continue;
1018 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1020 if (GET_CODE (SET_SRC (set)) == PLUS
1021 && CONST_INT_P (XEXP (SET_SRC (set), 1))
1022 && XEXP (SET_SRC (set), 0) == src
1023 && fixup_match_2 (insn, dst, src,
1024 XEXP (SET_SRC (set), 1)))
1025 break;
1026 continue;
1028 src_class = reg_preferred_class (REGNO (src));
1029 dst_class = reg_preferred_class (REGNO (dst));
1031 if (! (src_note = find_reg_note (insn, REG_DEAD, src)))
1033 /* We used to force the copy here like in other cases, but
1034 it produces worse code, as it eliminates no copy
1035 instructions and the copy emitted will be produced by
1036 reload anyway. On patterns with multiple alternatives,
1037 there may be better solution available.
1039 In particular this change produced slower code for numeric
1040 i387 programs. */
1042 continue;
1045 if (! regclass_compatible_p (src_class, dst_class))
1047 if (!copy_src)
1049 copy_src = src;
1050 copy_dst = dst;
1052 continue;
1055 /* Can not modify an earlier insn to set dst if this insn
1056 uses an old value in the source. */
1057 if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
1059 if (!copy_src)
1061 copy_src = src;
1062 copy_dst = dst;
1064 continue;
1067 /* If src is set once in a different basic block,
1068 and is set equal to a constant, then do not use
1069 it for this optimization, as this would make it
1070 no longer equivalent to a constant. */
1072 if (reg_is_remote_constant_p (src, insn))
1074 if (!copy_src)
1076 copy_src = src;
1077 copy_dst = dst;
1079 continue;
1083 if (dump_file)
1084 fprintf (dump_file,
1085 "Could fix operand %d of insn %d matching operand %d.\n",
1086 op_no, INSN_UID (insn), match_no);
1088 /* Scan backward to find the first instruction that uses
1089 the input operand. If the operand is set here, then
1090 replace it in both instructions with match_no. */
1092 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
1094 rtx pset;
1096 if (! INSN_P (p))
1097 continue;
1098 if (BLOCK_FOR_INSN (p) != bb)
1099 break;
1101 if (!DEBUG_INSN_P (p))
1102 length++;
1104 /* ??? See if all of SRC is set in P. This test is much
1105 more conservative than it needs to be. */
1106 pset = single_set (p);
1107 if (pset && SET_DEST (pset) == src)
1109 /* We use validate_replace_rtx, in case there
1110 are multiple identical source operands. All
1111 of them have to be changed at the same time:
1112 when validate_replace_rtx() calls
1113 apply_change_group(). */
1114 validate_change (p, &SET_DEST (pset), dst, 1);
1115 if (validate_replace_rtx (src, dst, insn))
1116 success = 1;
1117 break;
1120 /* We can't make this change if DST is mentioned at
1121 all in P, since we are going to change its value.
1122 We can't make this change if SRC is read or
1123 partially written in P, since we are going to
1124 eliminate SRC. However, if it's a debug insn, we
1125 can't refrain from making the change, for this
1126 would cause codegen differences, so instead we
1127 invalidate debug expressions that reference DST,
1128 and adjust references to SRC in them so that they
1129 become references to DST. */
1130 if (reg_mentioned_p (dst, PATTERN (p)))
1132 if (DEBUG_INSN_P (p))
1133 validate_change (p, &INSN_VAR_LOCATION_LOC (p),
1134 gen_rtx_UNKNOWN_VAR_LOC (), 1);
1135 else
1136 break;
1138 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1140 if (DEBUG_INSN_P (p))
1141 validate_replace_rtx_group (src, dst, p);
1142 else
1143 break;
1146 /* If we have passed a call instruction, and the
1147 pseudo-reg DST is not already live across a call,
1148 then don't perform the optimization. */
1149 if (CALL_P (p))
1151 num_calls++;
1152 freq_calls += REG_FREQ_FROM_BB (BLOCK_FOR_INSN (p));
1154 if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
1155 break;
1159 if (success)
1161 int dstno, srcno;
1163 /* Remove the death note for SRC from INSN. */
1164 remove_note (insn, src_note);
1165 /* Move the death note for SRC to P if it is used
1166 there. */
1167 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1169 XEXP (src_note, 1) = REG_NOTES (p);
1170 REG_NOTES (p) = src_note;
1172 /* If there is a REG_DEAD note for DST on P, then remove
1173 it, because DST is now set there. */
1174 if ((dst_note = find_reg_note (p, REG_DEAD, dst)))
1175 remove_note (p, dst_note);
1177 dstno = REGNO (dst);
1178 srcno = REGNO (src);
1180 INC_REG_N_SETS (dstno, 1);
1181 INC_REG_N_SETS (srcno, -1);
1183 REG_N_CALLS_CROSSED (dstno) += num_calls;
1184 REG_N_CALLS_CROSSED (srcno) -= num_calls;
1185 REG_FREQ_CALLS_CROSSED (dstno) += freq_calls;
1186 REG_FREQ_CALLS_CROSSED (srcno) -= freq_calls;
1188 REG_LIVE_LENGTH (dstno) += length;
1189 if (REG_LIVE_LENGTH (srcno) >= 0)
1191 REG_LIVE_LENGTH (srcno) -= length;
1192 /* REG_LIVE_LENGTH is only an approximation after
1193 combine if sched is not run, so make sure that we
1194 still have a reasonable value. */
1195 if (REG_LIVE_LENGTH (srcno) < 2)
1196 REG_LIVE_LENGTH (srcno) = 2;
1199 if (dump_file)
1200 fprintf (dump_file,
1201 "Fixed operand %d of insn %d matching operand %d.\n",
1202 op_no, INSN_UID (insn), match_no);
1204 break;
1206 else if (num_changes_pending () > 0)
1207 cancel_changes (0);
1210 /* If we weren't able to replace any of the alternatives, try an
1211 alternative approach of copying the source to the destination. */
1212 if (!success && copy_src != NULL_RTX)
1213 copy_src_to_dest (insn, copy_src, copy_dst);
1218 /* Main entry for the register move optimization. */
1220 static unsigned int
1221 regmove_optimize (void)
1223 int i;
1224 int nregs = max_reg_num ();
1226 df_note_add_problem ();
1227 df_analyze ();
1229 if (flag_ira_loop_pressure)
1230 ira_set_pseudo_classes (dump_file);
1232 regstat_init_n_sets_and_refs ();
1233 regstat_compute_ri ();
1235 regno_src_regno = XNEWVEC (int, nregs);
1236 for (i = nregs; --i >= 0; )
1237 regno_src_regno[i] = -1;
1239 /* A forward pass. Replace output operands with input operands. */
1240 regmove_forward_pass ();
1242 /* A backward pass. Replace input operands with output operands. */
1243 regmove_backward_pass ();
1245 /* Clean up. */
1246 free (regno_src_regno);
1247 if (reg_set_in_bb)
1249 free (reg_set_in_bb);
1250 reg_set_in_bb = NULL;
1252 regstat_free_n_sets_and_refs ();
1253 regstat_free_ri ();
1254 if (flag_ira_loop_pressure)
1255 free_reg_info ();
1256 return 0;
1259 /* Returns nonzero if INSN's pattern has matching constraints for any operand.
1260 Returns 0 if INSN can't be recognized, or if the alternative can't be
1261 determined.
1263 Initialize the info in MATCHP based on the constraints. */
1265 static int
1266 find_matches (rtx insn, struct match *matchp)
1268 int likely_spilled[MAX_RECOG_OPERANDS];
1269 int op_no;
1270 int any_matches = 0;
1272 extract_insn (insn);
1273 if (! constrain_operands (0))
1274 return 0;
1276 /* Must initialize this before main loop, because the code for
1277 the commutative case may set matches for operands other than
1278 the current one. */
1279 for (op_no = recog_data.n_operands; --op_no >= 0; )
1280 matchp->with[op_no] = matchp->commutative[op_no] = -1;
1282 for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1284 const char *p;
1285 char c;
1286 int i = 0;
1288 p = recog_data.constraints[op_no];
1290 likely_spilled[op_no] = 0;
1291 matchp->use[op_no] = READ;
1292 matchp->early_clobber[op_no] = 0;
1293 if (*p == '=')
1294 matchp->use[op_no] = WRITE;
1295 else if (*p == '+')
1296 matchp->use[op_no] = READWRITE;
1298 for (;*p && i < which_alternative; p++)
1299 if (*p == ',')
1300 i++;
1302 while ((c = *p) != '\0' && c != ',')
1304 switch (c)
1306 case '=':
1307 break;
1308 case '+':
1309 break;
1310 case '&':
1311 matchp->early_clobber[op_no] = 1;
1312 break;
1313 case '%':
1314 matchp->commutative[op_no] = op_no + 1;
1315 matchp->commutative[op_no + 1] = op_no;
1316 break;
1318 case '0': case '1': case '2': case '3': case '4':
1319 case '5': case '6': case '7': case '8': case '9':
1321 char *end;
1322 unsigned long match_ul = strtoul (p, &end, 10);
1323 int match = match_ul;
1325 p = end;
1327 if (match < op_no && likely_spilled[match])
1328 continue;
1329 matchp->with[op_no] = match;
1330 any_matches = 1;
1331 if (matchp->commutative[op_no] >= 0)
1332 matchp->with[matchp->commutative[op_no]] = match;
1334 continue;
1336 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1337 case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1338 case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1339 case 'C': case 'D': case 'W': case 'Y': case 'Z':
1340 if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_CONSTRAINT ((unsigned char) c, p) ))
1341 likely_spilled[op_no] = 1;
1342 break;
1344 p += CONSTRAINT_LEN (c, p);
1347 return any_matches;
1352 static bool
1353 gate_handle_regmove (void)
1355 return (optimize > 0 && flag_regmove);
1359 struct rtl_opt_pass pass_regmove =
1362 RTL_PASS,
1363 "regmove", /* name */
1364 gate_handle_regmove, /* gate */
1365 regmove_optimize, /* execute */
1366 NULL, /* sub */
1367 NULL, /* next */
1368 0, /* static_pass_number */
1369 TV_REGMOVE, /* tv_id */
1370 0, /* properties_required */
1371 0, /* properties_provided */
1372 0, /* properties_destroyed */
1373 0, /* todo_flags_start */
1374 TODO_df_finish | TODO_verify_rtl_sharing |
1375 TODO_dump_func |
1376 TODO_ggc_collect /* todo_flags_finish */