* config/rl78/mulsi3.S: Remove a few unneeded moves and branches.
[official-gcc.git] / gcc / regmove.c
blobe579a7a234d152a79d93dd7d89a9ac8f962ae52d
1 /* Move registers around to reduce number of move instructions needed.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This module makes some simple RTL code transformations which
22 improve the subsequent register allocation. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "insn-config.h"
31 #include "recog.h"
32 #include "target.h"
33 #include "regs.h"
34 #include "hard-reg-set.h"
35 #include "flags.h"
36 #include "function.h"
37 #include "expr.h"
38 #include "basic-block.h"
39 #include "except.h"
40 #include "diagnostic-core.h"
41 #include "reload.h"
42 #include "tree-pass.h"
43 #include "df.h"
44 #include "ira.h"
46 static int optimize_reg_copy_1 (rtx, rtx, rtx);
47 static void optimize_reg_copy_2 (rtx, rtx, rtx);
48 static void optimize_reg_copy_3 (rtx, rtx, rtx);
49 static void copy_src_to_dest (rtx, rtx, rtx);
51 enum match_use
53 READ,
54 WRITE,
55 READWRITE
58 struct match {
59 int with[MAX_RECOG_OPERANDS];
60 enum match_use use[MAX_RECOG_OPERANDS];
61 int commutative[MAX_RECOG_OPERANDS];
62 int early_clobber[MAX_RECOG_OPERANDS];
65 static int find_matches (rtx, struct match *);
66 static int fixup_match_2 (rtx, rtx, rtx, rtx);
68 /* Return nonzero if registers with CLASS1 and CLASS2 can be merged without
69 causing too much register allocation problems. */
70 static int
71 regclass_compatible_p (reg_class_t class0, reg_class_t class1)
73 return (class0 == class1
74 || (reg_class_subset_p (class0, class1)
75 && ! targetm.class_likely_spilled_p (class0))
76 || (reg_class_subset_p (class1, class0)
77 && ! targetm.class_likely_spilled_p (class1)));
81 #ifdef AUTO_INC_DEC
83 /* Find the place in the rtx X where REG is used as a memory address.
84 Return the MEM rtx that so uses it.
85 If PLUSCONST is nonzero, search instead for a memory address equivalent to
86 (plus REG (const_int PLUSCONST)).
88 If such an address does not appear, return 0.
89 If REG appears more than once, or is used other than in such an address,
90 return (rtx) 1. */
92 static rtx
93 find_use_as_address (rtx x, rtx reg, HOST_WIDE_INT plusconst)
95 enum rtx_code code = GET_CODE (x);
96 const char * const fmt = GET_RTX_FORMAT (code);
97 int i;
98 rtx value = 0;
99 rtx tem;
101 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
102 return x;
104 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
105 && XEXP (XEXP (x, 0), 0) == reg
106 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
107 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
108 return x;
110 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
112 /* If REG occurs inside a MEM used in a bit-field reference,
113 that is unacceptable. */
114 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
115 return (rtx) (size_t) 1;
118 if (x == reg)
119 return (rtx) (size_t) 1;
121 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
123 if (fmt[i] == 'e')
125 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
126 if (value == 0)
127 value = tem;
128 else if (tem != 0)
129 return (rtx) (size_t) 1;
131 else if (fmt[i] == 'E')
133 int j;
134 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
136 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
137 if (value == 0)
138 value = tem;
139 else if (tem != 0)
140 return (rtx) (size_t) 1;
145 return value;
149 /* INC_INSN is an instruction that adds INCREMENT to REG.
150 Try to fold INC_INSN as a post/pre in/decrement into INSN.
151 Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
152 Return nonzero for success. */
153 static int
154 try_auto_increment (rtx insn, rtx inc_insn, rtx inc_insn_set, rtx reg,
155 HOST_WIDE_INT increment, int pre)
157 enum rtx_code inc_code;
159 rtx pset = single_set (insn);
160 if (pset)
162 /* Can't use the size of SET_SRC, we might have something like
163 (sign_extend:SI (mem:QI ... */
164 rtx use = find_use_as_address (pset, reg, 0);
165 if (use != 0 && use != (rtx) (size_t) 1)
167 int size = GET_MODE_SIZE (GET_MODE (use));
168 if (0
169 || (HAVE_POST_INCREMENT
170 && pre == 0 && (inc_code = POST_INC, increment == size))
171 || (HAVE_PRE_INCREMENT
172 && pre == 1 && (inc_code = PRE_INC, increment == size))
173 || (HAVE_POST_DECREMENT
174 && pre == 0 && (inc_code = POST_DEC, increment == -size))
175 || (HAVE_PRE_DECREMENT
176 && pre == 1 && (inc_code = PRE_DEC, increment == -size))
179 if (inc_insn_set)
180 validate_change
181 (inc_insn,
182 &SET_SRC (inc_insn_set),
183 XEXP (SET_SRC (inc_insn_set), 0), 1);
184 validate_change (insn, &XEXP (use, 0),
185 gen_rtx_fmt_e (inc_code,
186 GET_MODE (XEXP (use, 0)), reg),
188 if (apply_change_group ())
190 /* If there is a REG_DEAD note on this insn, we must
191 change this not to REG_UNUSED meaning that the register
192 is set, but the value is dead. Failure to do so will
193 result in sched1 dying -- when it recomputes lifetime
194 information, the number of REG_DEAD notes will have
195 changed. */
196 rtx note = find_reg_note (insn, REG_DEAD, reg);
197 if (note)
198 PUT_REG_NOTE_KIND (note, REG_UNUSED);
200 add_reg_note (insn, REG_INC, reg);
202 if (! inc_insn_set)
203 delete_insn (inc_insn);
204 return 1;
209 return 0;
211 #endif
214 static int *regno_src_regno;
216 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
217 in INSN.
219 Search forward to see if SRC dies before either it or DEST is modified,
220 but don't scan past the end of a basic block. If so, we can replace SRC
221 with DEST and let SRC die in INSN.
223 This will reduce the number of registers live in that range and may enable
224 DEST to be tied to SRC, thus often saving one register in addition to a
225 register-register copy. */
227 static int
228 optimize_reg_copy_1 (rtx insn, rtx dest, rtx src)
230 rtx p, q;
231 rtx note;
232 rtx dest_death = 0;
233 int sregno = REGNO (src);
234 int dregno = REGNO (dest);
235 basic_block bb = BLOCK_FOR_INSN (insn);
237 /* We don't want to mess with hard regs if register classes are small. */
238 if (sregno == dregno
239 || (targetm.small_register_classes_for_mode_p (GET_MODE (src))
240 && (sregno < FIRST_PSEUDO_REGISTER
241 || dregno < FIRST_PSEUDO_REGISTER))
242 /* We don't see all updates to SP if they are in an auto-inc memory
243 reference, so we must disallow this optimization on them. */
244 || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
245 return 0;
247 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
249 if (! INSN_P (p))
250 continue;
251 if (BLOCK_FOR_INSN (p) != bb)
252 break;
254 if (reg_set_p (src, p) || reg_set_p (dest, p)
255 /* If SRC is an asm-declared register, it must not be replaced
256 in any asm. Unfortunately, the REG_EXPR tree for the asm
257 variable may be absent in the SRC rtx, so we can't check the
258 actual register declaration easily (the asm operand will have
259 it, though). To avoid complicating the test for a rare case,
260 we just don't perform register replacement for a hard reg
261 mentioned in an asm. */
262 || (sregno < FIRST_PSEUDO_REGISTER
263 && asm_noperands (PATTERN (p)) >= 0
264 && reg_overlap_mentioned_p (src, PATTERN (p)))
265 /* Don't change hard registers used by a call. */
266 || (CALL_P (p) && sregno < FIRST_PSEUDO_REGISTER
267 && find_reg_fusage (p, USE, src))
268 /* Don't change a USE of a register. */
269 || (GET_CODE (PATTERN (p)) == USE
270 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
271 break;
273 /* See if all of SRC dies in P. This test is slightly more
274 conservative than it needs to be. */
275 if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
276 && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
278 int failed = 0;
279 int d_length = 0;
280 int s_length = 0;
281 int d_n_calls = 0;
282 int s_n_calls = 0;
283 int s_freq_calls = 0;
284 int d_freq_calls = 0;
286 /* We can do the optimization. Scan forward from INSN again,
287 replacing regs as we go. Set FAILED if a replacement can't
288 be done. In that case, we can't move the death note for SRC.
289 This should be rare. */
291 /* Set to stop at next insn. */
292 for (q = next_real_insn (insn);
293 q != next_real_insn (p);
294 q = next_real_insn (q))
296 if (reg_overlap_mentioned_p (src, PATTERN (q)))
298 /* If SRC is a hard register, we might miss some
299 overlapping registers with validate_replace_rtx,
300 so we would have to undo it. We can't if DEST is
301 present in the insn, so fail in that combination
302 of cases. */
303 if (sregno < FIRST_PSEUDO_REGISTER
304 && reg_mentioned_p (dest, PATTERN (q)))
305 failed = 1;
307 /* Attempt to replace all uses. */
308 else if (!validate_replace_rtx (src, dest, q))
309 failed = 1;
311 /* If this succeeded, but some part of the register
312 is still present, undo the replacement. */
313 else if (sregno < FIRST_PSEUDO_REGISTER
314 && reg_overlap_mentioned_p (src, PATTERN (q)))
316 validate_replace_rtx (dest, src, q);
317 failed = 1;
321 /* For SREGNO, count the total number of insns scanned.
322 For DREGNO, count the total number of insns scanned after
323 passing the death note for DREGNO. */
324 if (!DEBUG_INSN_P (p))
326 s_length++;
327 if (dest_death)
328 d_length++;
331 /* If the insn in which SRC dies is a CALL_INSN, don't count it
332 as a call that has been crossed. Otherwise, count it. */
333 if (q != p && CALL_P (q))
335 /* Similarly, total calls for SREGNO, total calls beyond
336 the death note for DREGNO. */
337 s_n_calls++;
338 s_freq_calls += REG_FREQ_FROM_BB (BLOCK_FOR_INSN (q));
339 if (dest_death)
341 d_n_calls++;
342 d_freq_calls += REG_FREQ_FROM_BB (BLOCK_FOR_INSN (q));
346 /* If DEST dies here, remove the death note and save it for
347 later. Make sure ALL of DEST dies here; again, this is
348 overly conservative. */
349 if (dest_death == 0
350 && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0)
352 if (GET_MODE (XEXP (dest_death, 0)) != GET_MODE (dest))
353 failed = 1, dest_death = 0;
354 else
355 remove_note (q, dest_death);
359 if (! failed)
361 /* These counters need to be updated if and only if we are
362 going to move the REG_DEAD note. */
363 if (sregno >= FIRST_PSEUDO_REGISTER)
365 if (REG_LIVE_LENGTH (sregno) >= 0)
367 REG_LIVE_LENGTH (sregno) -= s_length;
368 /* REG_LIVE_LENGTH is only an approximation after
369 combine if sched is not run, so make sure that we
370 still have a reasonable value. */
371 if (REG_LIVE_LENGTH (sregno) < 2)
372 REG_LIVE_LENGTH (sregno) = 2;
375 REG_N_CALLS_CROSSED (sregno) -= s_n_calls;
376 REG_FREQ_CALLS_CROSSED (sregno) -= s_freq_calls;
379 /* Move death note of SRC from P to INSN. */
380 remove_note (p, note);
381 XEXP (note, 1) = REG_NOTES (insn);
382 REG_NOTES (insn) = note;
385 /* DEST is also dead if INSN has a REG_UNUSED note for DEST. */
386 if (! dest_death
387 && (dest_death = find_regno_note (insn, REG_UNUSED, dregno)))
389 PUT_REG_NOTE_KIND (dest_death, REG_DEAD);
390 remove_note (insn, dest_death);
393 /* Put death note of DEST on P if we saw it die. */
394 if (dest_death)
396 XEXP (dest_death, 1) = REG_NOTES (p);
397 REG_NOTES (p) = dest_death;
399 if (dregno >= FIRST_PSEUDO_REGISTER)
401 /* If and only if we are moving the death note for DREGNO,
402 then we need to update its counters. */
403 if (REG_LIVE_LENGTH (dregno) >= 0)
404 REG_LIVE_LENGTH (dregno) += d_length;
405 REG_N_CALLS_CROSSED (dregno) += d_n_calls;
406 REG_FREQ_CALLS_CROSSED (dregno) += d_freq_calls;
410 return ! failed;
413 /* If SRC is a hard register which is set or killed in some other
414 way, we can't do this optimization. */
415 else if (sregno < FIRST_PSEUDO_REGISTER
416 && dead_or_set_p (p, src))
417 break;
419 return 0;
422 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
423 a sequence of insns that modify DEST followed by an insn that sets
424 SRC to DEST in which DEST dies, with no prior modification of DEST.
425 (There is no need to check if the insns in between actually modify
426 DEST. We should not have cases where DEST is not modified, but
427 the optimization is safe if no such modification is detected.)
428 In that case, we can replace all uses of DEST, starting with INSN and
429 ending with the set of SRC to DEST, with SRC. We do not do this
430 optimization if a CALL_INSN is crossed unless SRC already crosses a
431 call or if DEST dies before the copy back to SRC.
433 It is assumed that DEST and SRC are pseudos; it is too complicated to do
434 this for hard registers since the substitutions we may make might fail. */
436 static void
437 optimize_reg_copy_2 (rtx insn, rtx dest, rtx src)
439 rtx p, q;
440 rtx set;
441 int sregno = REGNO (src);
442 int dregno = REGNO (dest);
443 basic_block bb = BLOCK_FOR_INSN (insn);
445 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
447 if (! INSN_P (p))
448 continue;
449 if (BLOCK_FOR_INSN (p) != bb)
450 break;
452 set = single_set (p);
453 if (set && SET_SRC (set) == dest && SET_DEST (set) == src
454 && find_reg_note (p, REG_DEAD, dest))
456 /* We can do the optimization. Scan forward from INSN again,
457 replacing regs as we go. */
459 /* Set to stop at next insn. */
460 for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
461 if (INSN_P (q))
463 if (reg_mentioned_p (dest, PATTERN (q)))
465 rtx note;
467 PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
468 note = FIND_REG_INC_NOTE (q, dest);
469 if (note)
471 remove_note (q, note);
472 add_reg_note (q, REG_INC, src);
474 df_insn_rescan (q);
477 if (CALL_P (q))
479 int freq = REG_FREQ_FROM_BB (BLOCK_FOR_INSN (q));
480 REG_N_CALLS_CROSSED (dregno)--;
481 REG_N_CALLS_CROSSED (sregno)++;
482 REG_FREQ_CALLS_CROSSED (dregno) -= freq;
483 REG_FREQ_CALLS_CROSSED (sregno) += freq;
487 remove_note (p, find_reg_note (p, REG_DEAD, dest));
488 REG_N_DEATHS (dregno)--;
489 remove_note (insn, find_reg_note (insn, REG_DEAD, src));
490 REG_N_DEATHS (sregno)--;
491 return;
494 if (reg_set_p (src, p)
495 || find_reg_note (p, REG_DEAD, dest)
496 || (CALL_P (p) && REG_N_CALLS_CROSSED (sregno) == 0))
497 break;
501 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
502 Look if SRC dies there, and if it is only set once, by loading
503 it from memory. If so, try to incorporate the zero/sign extension
504 into the memory read, change SRC to the mode of DEST, and alter
505 the remaining accesses to use the appropriate SUBREG. This allows
506 SRC and DEST to be tied later. */
507 static void
508 optimize_reg_copy_3 (rtx insn, rtx dest, rtx src)
510 rtx src_reg = XEXP (src, 0);
511 int src_no = REGNO (src_reg);
512 int dst_no = REGNO (dest);
513 rtx p, set, set_insn;
514 enum machine_mode old_mode;
515 basic_block bb = BLOCK_FOR_INSN (insn);
517 if (src_no < FIRST_PSEUDO_REGISTER
518 || dst_no < FIRST_PSEUDO_REGISTER
519 || ! find_reg_note (insn, REG_DEAD, src_reg)
520 || REG_N_DEATHS (src_no) != 1
521 || REG_N_SETS (src_no) != 1)
522 return;
524 for (p = PREV_INSN (insn); p && ! reg_set_p (src_reg, p); p = PREV_INSN (p))
525 if (INSN_P (p) && BLOCK_FOR_INSN (p) != bb)
526 break;
528 if (! p || BLOCK_FOR_INSN (p) != bb)
529 return;
531 if (! (set = single_set (p))
532 || !MEM_P (SET_SRC (set))
533 /* If there's a REG_EQUIV note, this must be an insn that loads an
534 argument. Prefer keeping the note over doing this optimization. */
535 || find_reg_note (p, REG_EQUIV, NULL_RTX)
536 || SET_DEST (set) != src_reg)
537 return;
539 /* Be conservative: although this optimization is also valid for
540 volatile memory references, that could cause trouble in later passes. */
541 if (MEM_VOLATILE_P (SET_SRC (set)))
542 return;
544 /* Do not use a SUBREG to truncate from one mode to another if truncation
545 is not a nop. */
546 if (GET_MODE_BITSIZE (GET_MODE (src_reg)) <= GET_MODE_BITSIZE (GET_MODE (src))
547 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (src), GET_MODE (src_reg)))
548 return;
550 set_insn = p;
551 old_mode = GET_MODE (src_reg);
552 PUT_MODE (src_reg, GET_MODE (src));
553 XEXP (src, 0) = SET_SRC (set);
555 /* Include this change in the group so that it's easily undone if
556 one of the changes in the group is invalid. */
557 validate_change (p, &SET_SRC (set), src, 1);
559 /* Now walk forward making additional replacements. We want to be able
560 to undo all the changes if a later substitution fails. */
561 while (p = NEXT_INSN (p), p != insn)
563 if (! INSN_P (p))
564 continue;
566 /* Make a tentative change. */
567 validate_replace_rtx_group (src_reg,
568 gen_lowpart_SUBREG (old_mode, src_reg),
572 validate_replace_rtx_group (src, src_reg, insn);
574 /* Now see if all the changes are valid. */
575 if (! apply_change_group ())
577 /* One or more changes were no good. Back out everything. */
578 PUT_MODE (src_reg, old_mode);
579 XEXP (src, 0) = src_reg;
581 else
583 rtx note = find_reg_note (set_insn, REG_EQUAL, NULL_RTX);
584 if (note)
586 if (rtx_equal_p (XEXP (note, 0), XEXP (src, 0)))
588 XEXP (note, 0)
589 = gen_rtx_fmt_e (GET_CODE (src), GET_MODE (src),
590 XEXP (note, 0));
591 df_notes_rescan (set_insn);
593 else
594 remove_note (set_insn, note);
600 /* If we were not able to update the users of src to use dest directly, try
601 instead moving the value to dest directly before the operation. */
603 static void
604 copy_src_to_dest (rtx insn, rtx src, rtx dest)
606 rtx seq;
607 rtx link;
608 rtx next;
609 rtx set;
610 rtx move_insn;
611 rtx *p_insn_notes;
612 rtx *p_move_notes;
613 int src_regno;
614 int dest_regno;
616 /* A REG_LIVE_LENGTH of -1 indicates the register must not go into
617 a hard register, e.g. because it crosses as setjmp. See the
618 comment in regstat.c:regstat_bb_compute_ri. Don't try to apply
619 any transformations to such regs. */
621 if (REG_P (src)
622 && REG_LIVE_LENGTH (REGNO (src)) > 0
623 && REG_P (dest)
624 && REG_LIVE_LENGTH (REGNO (dest)) > 0
625 && (set = single_set (insn)) != NULL_RTX
626 && !reg_mentioned_p (dest, SET_SRC (set))
627 && GET_MODE (src) == GET_MODE (dest))
629 int old_num_regs = reg_rtx_no;
631 /* Generate the src->dest move. */
632 start_sequence ();
633 emit_move_insn (dest, src);
634 seq = get_insns ();
635 end_sequence ();
636 /* If this sequence uses new registers, we may not use it. */
637 if (old_num_regs != reg_rtx_no
638 || ! validate_replace_rtx (src, dest, insn))
640 /* We have to restore reg_rtx_no to its old value, lest
641 recompute_reg_usage will try to compute the usage of the
642 new regs, yet reg_n_info is not valid for them. */
643 reg_rtx_no = old_num_regs;
644 return;
646 emit_insn_before (seq, insn);
647 move_insn = PREV_INSN (insn);
648 p_move_notes = &REG_NOTES (move_insn);
649 p_insn_notes = &REG_NOTES (insn);
651 /* Move any notes mentioning src to the move instruction. */
652 for (link = REG_NOTES (insn); link != NULL_RTX; link = next)
654 next = XEXP (link, 1);
655 if (XEXP (link, 0) == src)
657 *p_move_notes = link;
658 p_move_notes = &XEXP (link, 1);
660 else
662 *p_insn_notes = link;
663 p_insn_notes = &XEXP (link, 1);
667 *p_move_notes = NULL_RTX;
668 *p_insn_notes = NULL_RTX;
670 /* Update the various register tables. */
671 dest_regno = REGNO (dest);
672 INC_REG_N_SETS (dest_regno, 1);
673 REG_LIVE_LENGTH (dest_regno)++;
674 src_regno = REGNO (src);
675 if (! find_reg_note (move_insn, REG_DEAD, src))
676 REG_LIVE_LENGTH (src_regno)++;
680 /* reg_set_in_bb[REGNO] points to basic block iff the register is set
681 only once in the given block and has REG_EQUAL note. */
683 static basic_block *reg_set_in_bb;
685 /* Size of reg_set_in_bb array. */
686 static unsigned int max_reg_computed;
689 /* Return whether REG is set in only one location, and is set to a
690 constant, but is set in a different basic block from INSN (an
691 instructions which uses REG). In this case REG is equivalent to a
692 constant, and we don't want to break that equivalence, because that
693 may increase register pressure and make reload harder. If REG is
694 set in the same basic block as INSN, we don't worry about it,
695 because we'll probably need a register anyhow (??? but what if REG
696 is used in a different basic block as well as this one?). */
698 static bool
699 reg_is_remote_constant_p (rtx reg, rtx insn)
701 basic_block bb;
702 rtx p;
703 int max;
705 if (!reg_set_in_bb)
707 max_reg_computed = max = max_reg_num ();
708 reg_set_in_bb = XCNEWVEC (basic_block, max);
710 FOR_EACH_BB (bb)
711 FOR_BB_INSNS (bb, p)
713 rtx s;
715 if (!INSN_P (p))
716 continue;
717 s = single_set (p);
718 /* This is the instruction which sets REG. If there is a
719 REG_EQUAL note, then REG is equivalent to a constant. */
720 if (s != 0
721 && REG_P (SET_DEST (s))
722 && REG_N_SETS (REGNO (SET_DEST (s))) == 1
723 && find_reg_note (p, REG_EQUAL, NULL_RTX))
724 reg_set_in_bb[REGNO (SET_DEST (s))] = bb;
728 gcc_assert (REGNO (reg) < max_reg_computed);
729 if (reg_set_in_bb[REGNO (reg)] == NULL)
730 return false;
731 return (reg_set_in_bb[REGNO (reg)] != BLOCK_FOR_INSN (insn));
734 /* INSN is adding a CONST_INT to a REG. We search backwards looking for
735 another add immediate instruction with the same source and dest registers,
736 and if we find one, we change INSN to an increment, and return 1. If
737 no changes are made, we return 0.
739 This changes
740 (set (reg100) (plus reg1 offset1))
742 (set (reg100) (plus reg1 offset2))
744 (set (reg100) (plus reg1 offset1))
746 (set (reg100) (plus reg100 offset2-offset1)) */
748 /* ??? What does this comment mean? */
749 /* cse disrupts preincrement / postdecrement sequences when it finds a
750 hard register as ultimate source, like the frame pointer. */
752 static int
753 fixup_match_2 (rtx insn, rtx dst, rtx src, rtx offset)
755 rtx p, dst_death = 0;
756 int length, num_calls = 0, freq_calls = 0;
757 basic_block bb = BLOCK_FOR_INSN (insn);
759 /* If SRC dies in INSN, we'd have to move the death note. This is
760 considered to be very unlikely, so we just skip the optimization
761 in this case. */
762 if (find_regno_note (insn, REG_DEAD, REGNO (src)))
763 return 0;
765 /* Scan backward to find the first instruction that sets DST. */
767 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
769 rtx pset;
771 if (! INSN_P (p))
772 continue;
773 if (BLOCK_FOR_INSN (p) != bb)
774 break;
776 if (find_regno_note (p, REG_DEAD, REGNO (dst)))
777 dst_death = p;
778 if (! dst_death && !DEBUG_INSN_P (p))
779 length++;
781 pset = single_set (p);
782 if (pset && SET_DEST (pset) == dst
783 && GET_CODE (SET_SRC (pset)) == PLUS
784 && XEXP (SET_SRC (pset), 0) == src
785 && CONST_INT_P (XEXP (SET_SRC (pset), 1)))
787 HOST_WIDE_INT newconst
788 = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
789 rtx add = gen_add3_insn (dst, dst,
790 gen_int_mode (newconst, GET_MODE (dst)));
792 if (add && validate_change (insn, &PATTERN (insn), add, 0))
794 /* Remove the death note for DST from DST_DEATH. */
795 if (dst_death)
797 remove_death (REGNO (dst), dst_death);
798 REG_LIVE_LENGTH (REGNO (dst)) += length;
799 REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
800 REG_FREQ_CALLS_CROSSED (REGNO (dst)) += freq_calls;
803 if (dump_file)
804 fprintf (dump_file,
805 "Fixed operand of insn %d.\n",
806 INSN_UID (insn));
808 #ifdef AUTO_INC_DEC
809 for (p = PREV_INSN (insn); p; p = PREV_INSN (p))
811 if (! INSN_P (p))
812 continue;
813 if (BLOCK_FOR_INSN (p) != bb)
814 break;
815 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
817 if (try_auto_increment (p, insn, 0, dst, newconst, 0))
818 return 1;
819 break;
822 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
824 if (! INSN_P (p))
825 continue;
826 if (BLOCK_FOR_INSN (p) != bb)
827 break;
828 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
830 try_auto_increment (p, insn, 0, dst, newconst, 1);
831 break;
834 #endif
835 return 1;
839 if (reg_set_p (dst, PATTERN (p)))
840 break;
842 /* If we have passed a call instruction, and the
843 pseudo-reg SRC is not already live across a call,
844 then don't perform the optimization. */
845 /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
846 hard regs are clobbered. Thus, we only use it for src for
847 non-call insns. */
848 if (CALL_P (p))
850 if (! dst_death)
852 num_calls++;
853 freq_calls += REG_FREQ_FROM_BB (BLOCK_FOR_INSN (p));
856 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
857 break;
859 if ((HARD_REGISTER_P (dst) && call_used_regs [REGNO (dst)])
860 || find_reg_fusage (p, CLOBBER, dst))
861 break;
863 else if (reg_set_p (src, PATTERN (p)))
864 break;
867 return 0;
870 /* A forward pass. Replace output operands with input operands. */
872 static void
873 regmove_forward_pass (void)
875 basic_block bb;
876 rtx insn;
878 if (! flag_expensive_optimizations)
879 return;
881 if (dump_file)
882 fprintf (dump_file, "Starting forward pass...\n");
884 FOR_EACH_BB (bb)
886 FOR_BB_INSNS (bb, insn)
888 rtx set = single_set (insn);
889 if (! set)
890 continue;
892 if ((GET_CODE (SET_SRC (set)) == SIGN_EXTEND
893 || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
894 && REG_P (XEXP (SET_SRC (set), 0))
895 && REG_P (SET_DEST (set)))
896 optimize_reg_copy_3 (insn, SET_DEST (set), SET_SRC (set));
898 if (REG_P (SET_SRC (set))
899 && REG_P (SET_DEST (set)))
901 /* If this is a register-register copy where SRC is not dead,
902 see if we can optimize it. If this optimization succeeds,
903 it will become a copy where SRC is dead. */
904 if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
905 || optimize_reg_copy_1 (insn, SET_DEST (set), SET_SRC (set)))
906 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
908 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
909 if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
910 optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
911 if (regno_src_regno[REGNO (SET_DEST (set))] < 0
912 && SET_SRC (set) != SET_DEST (set))
914 int srcregno = REGNO (SET_SRC (set));
915 if (regno_src_regno[srcregno] >= 0)
916 srcregno = regno_src_regno[srcregno];
917 regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
925 /* A backward pass. Replace input operands with output operands. */
927 static void
928 regmove_backward_pass (void)
930 basic_block bb;
931 rtx insn, prev;
933 if (dump_file)
934 fprintf (dump_file, "Starting backward pass...\n");
936 FOR_EACH_BB_REVERSE (bb)
938 /* ??? Use the safe iterator because fixup_match_2 can remove
939 insns via try_auto_increment. */
940 FOR_BB_INSNS_REVERSE_SAFE (bb, insn, prev)
942 struct match match;
943 rtx copy_src, copy_dst;
944 int op_no, match_no;
945 int success = 0;
947 if (! INSN_P (insn))
948 continue;
950 if (! find_matches (insn, &match))
951 continue;
953 /* Now scan through the operands looking for a destination operand
954 which is supposed to match a source operand.
955 Then scan backward for an instruction which sets the source
956 operand. If safe, then replace the source operand with the
957 dest operand in both instructions. */
959 copy_src = NULL_RTX;
960 copy_dst = NULL_RTX;
961 for (op_no = 0; op_no < recog_data.n_operands; op_no++)
963 rtx set, p, src, dst;
964 rtx src_note, dst_note;
965 int num_calls = 0, freq_calls = 0;
966 enum reg_class src_class, dst_class;
967 int length;
969 match_no = match.with[op_no];
971 /* Nothing to do if the two operands aren't supposed to match. */
972 if (match_no < 0)
973 continue;
975 dst = recog_data.operand[match_no];
976 src = recog_data.operand[op_no];
978 if (!REG_P (src))
979 continue;
981 if (!REG_P (dst)
982 || REGNO (dst) < FIRST_PSEUDO_REGISTER
983 || REG_LIVE_LENGTH (REGNO (dst)) < 0
984 || GET_MODE (src) != GET_MODE (dst))
985 continue;
987 /* If the operands already match, then there is nothing to do. */
988 if (operands_match_p (src, dst))
989 continue;
991 if (match.commutative[op_no] >= 0)
993 rtx comm = recog_data.operand[match.commutative[op_no]];
994 if (operands_match_p (comm, dst))
995 continue;
998 set = single_set (insn);
999 if (! set)
1000 continue;
1002 /* Note that single_set ignores parts of a parallel set for
1003 which one of the destinations is REG_UNUSED. We can't
1004 handle that here, since we can wind up rewriting things
1005 such that a single register is set twice within a single
1006 parallel. */
1007 if (reg_set_p (src, insn))
1008 continue;
1010 /* match_no/dst must be a write-only operand, and
1011 operand_operand/src must be a read-only operand. */
1012 if (match.use[op_no] != READ
1013 || match.use[match_no] != WRITE)
1014 continue;
1016 if (match.early_clobber[match_no]
1017 && count_occurrences (PATTERN (insn), src, 0) > 1)
1018 continue;
1020 /* Make sure match_no is the destination. */
1021 if (recog_data.operand[match_no] != SET_DEST (set))
1022 continue;
1024 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1026 if (GET_CODE (SET_SRC (set)) == PLUS
1027 && CONST_INT_P (XEXP (SET_SRC (set), 1))
1028 && XEXP (SET_SRC (set), 0) == src
1029 && fixup_match_2 (insn, dst, src,
1030 XEXP (SET_SRC (set), 1)))
1031 break;
1032 continue;
1034 src_class = reg_preferred_class (REGNO (src));
1035 dst_class = reg_preferred_class (REGNO (dst));
1037 if (! (src_note = find_reg_note (insn, REG_DEAD, src)))
1039 /* We used to force the copy here like in other cases, but
1040 it produces worse code, as it eliminates no copy
1041 instructions and the copy emitted will be produced by
1042 reload anyway. On patterns with multiple alternatives,
1043 there may be better solution available.
1045 In particular this change produced slower code for numeric
1046 i387 programs. */
1048 continue;
1051 if (! regclass_compatible_p (src_class, dst_class))
1053 if (!copy_src)
1055 copy_src = src;
1056 copy_dst = dst;
1058 continue;
1061 /* Can not modify an earlier insn to set dst if this insn
1062 uses an old value in the source. */
1063 if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
1065 if (!copy_src)
1067 copy_src = src;
1068 copy_dst = dst;
1070 continue;
1073 /* If src is set once in a different basic block,
1074 and is set equal to a constant, then do not use
1075 it for this optimization, as this would make it
1076 no longer equivalent to a constant. */
1078 if (reg_is_remote_constant_p (src, insn))
1080 if (!copy_src)
1082 copy_src = src;
1083 copy_dst = dst;
1085 continue;
1089 if (dump_file)
1090 fprintf (dump_file,
1091 "Could fix operand %d of insn %d matching operand %d.\n",
1092 op_no, INSN_UID (insn), match_no);
1094 /* Scan backward to find the first instruction that uses
1095 the input operand. If the operand is set here, then
1096 replace it in both instructions with match_no. */
1098 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
1100 rtx pset;
1102 if (! INSN_P (p))
1103 continue;
1104 if (BLOCK_FOR_INSN (p) != bb)
1105 break;
1107 if (!DEBUG_INSN_P (p))
1108 length++;
1110 /* ??? See if all of SRC is set in P. This test is much
1111 more conservative than it needs to be. */
1112 pset = single_set (p);
1113 if (pset && SET_DEST (pset) == src)
1115 /* We use validate_replace_rtx, in case there
1116 are multiple identical source operands. All
1117 of them have to be changed at the same time:
1118 when validate_replace_rtx() calls
1119 apply_change_group(). */
1120 validate_change (p, &SET_DEST (pset), dst, 1);
1121 if (validate_replace_rtx (src, dst, insn))
1122 success = 1;
1123 break;
1126 /* We can't make this change if DST is mentioned at
1127 all in P, since we are going to change its value.
1128 We can't make this change if SRC is read or
1129 partially written in P, since we are going to
1130 eliminate SRC. However, if it's a debug insn, we
1131 can't refrain from making the change, for this
1132 would cause codegen differences, so instead we
1133 invalidate debug expressions that reference DST,
1134 and adjust references to SRC in them so that they
1135 become references to DST. */
1136 if (reg_mentioned_p (dst, PATTERN (p)))
1138 if (DEBUG_INSN_P (p))
1139 validate_change (p, &INSN_VAR_LOCATION_LOC (p),
1140 gen_rtx_UNKNOWN_VAR_LOC (), 1);
1141 else
1142 break;
1144 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1146 if (DEBUG_INSN_P (p))
1147 validate_replace_rtx_group (src, dst, p);
1148 else
1149 break;
1152 /* If we have passed a call instruction, and the
1153 pseudo-reg DST is not already live across a call,
1154 then don't perform the optimization. */
1155 if (CALL_P (p))
1157 num_calls++;
1158 freq_calls += REG_FREQ_FROM_BB (BLOCK_FOR_INSN (p));
1160 if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
1161 break;
1165 if (success)
1167 int dstno, srcno;
1169 /* Remove the death note for SRC from INSN. */
1170 remove_note (insn, src_note);
1171 /* Move the death note for SRC to P if it is used
1172 there. */
1173 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1175 XEXP (src_note, 1) = REG_NOTES (p);
1176 REG_NOTES (p) = src_note;
1178 /* If there is a REG_DEAD note for DST on P, then remove
1179 it, because DST is now set there. */
1180 if ((dst_note = find_reg_note (p, REG_DEAD, dst)))
1181 remove_note (p, dst_note);
1183 dstno = REGNO (dst);
1184 srcno = REGNO (src);
1186 INC_REG_N_SETS (dstno, 1);
1187 INC_REG_N_SETS (srcno, -1);
1189 REG_N_CALLS_CROSSED (dstno) += num_calls;
1190 REG_N_CALLS_CROSSED (srcno) -= num_calls;
1191 REG_FREQ_CALLS_CROSSED (dstno) += freq_calls;
1192 REG_FREQ_CALLS_CROSSED (srcno) -= freq_calls;
1194 REG_LIVE_LENGTH (dstno) += length;
1195 if (REG_LIVE_LENGTH (srcno) >= 0)
1197 REG_LIVE_LENGTH (srcno) -= length;
1198 /* REG_LIVE_LENGTH is only an approximation after
1199 combine if sched is not run, so make sure that we
1200 still have a reasonable value. */
1201 if (REG_LIVE_LENGTH (srcno) < 2)
1202 REG_LIVE_LENGTH (srcno) = 2;
1205 if (dump_file)
1206 fprintf (dump_file,
1207 "Fixed operand %d of insn %d matching operand %d.\n",
1208 op_no, INSN_UID (insn), match_no);
1210 break;
1212 else if (num_changes_pending () > 0)
1213 cancel_changes (0);
1216 /* If we weren't able to replace any of the alternatives, try an
1217 alternative approach of copying the source to the destination. */
1218 if (!success && copy_src != NULL_RTX)
1219 copy_src_to_dest (insn, copy_src, copy_dst);
1224 /* Main entry for the register move optimization. */
1226 static unsigned int
1227 regmove_optimize (void)
1229 int i;
1230 int nregs = max_reg_num ();
1232 df_note_add_problem ();
1233 df_analyze ();
1235 regstat_init_n_sets_and_refs ();
1236 regstat_compute_ri ();
1238 if (flag_ira_loop_pressure)
1239 ira_set_pseudo_classes (true, dump_file);
1241 regno_src_regno = XNEWVEC (int, nregs);
1242 for (i = nregs; --i >= 0; )
1243 regno_src_regno[i] = -1;
1245 /* A forward pass. Replace output operands with input operands. */
1246 regmove_forward_pass ();
1248 /* A backward pass. Replace input operands with output operands. */
1249 regmove_backward_pass ();
1251 /* Clean up. */
1252 free (regno_src_regno);
1253 if (reg_set_in_bb)
1255 free (reg_set_in_bb);
1256 reg_set_in_bb = NULL;
1258 regstat_free_n_sets_and_refs ();
1259 regstat_free_ri ();
1260 if (flag_ira_loop_pressure)
1261 free_reg_info ();
1262 return 0;
1265 /* Returns nonzero if INSN's pattern has matching constraints for any operand.
1266 Returns 0 if INSN can't be recognized, or if the alternative can't be
1267 determined.
1269 Initialize the info in MATCHP based on the constraints. */
1271 static int
1272 find_matches (rtx insn, struct match *matchp)
1274 int likely_spilled[MAX_RECOG_OPERANDS];
1275 int op_no;
1276 int any_matches = 0;
1278 extract_insn (insn);
1279 if (! constrain_operands (0))
1280 return 0;
1282 /* Must initialize this before main loop, because the code for
1283 the commutative case may set matches for operands other than
1284 the current one. */
1285 for (op_no = recog_data.n_operands; --op_no >= 0; )
1286 matchp->with[op_no] = matchp->commutative[op_no] = -1;
1288 for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1290 const char *p;
1291 char c;
1292 int i = 0;
1294 p = recog_data.constraints[op_no];
1296 likely_spilled[op_no] = 0;
1297 matchp->use[op_no] = READ;
1298 matchp->early_clobber[op_no] = 0;
1299 if (*p == '=')
1300 matchp->use[op_no] = WRITE;
1301 else if (*p == '+')
1302 matchp->use[op_no] = READWRITE;
1304 for (;*p && i < which_alternative; p++)
1305 if (*p == ',')
1306 i++;
1308 while ((c = *p) != '\0' && c != ',')
1310 switch (c)
1312 case '=':
1313 break;
1314 case '+':
1315 break;
1316 case '&':
1317 matchp->early_clobber[op_no] = 1;
1318 break;
1319 case '%':
1320 matchp->commutative[op_no] = op_no + 1;
1321 matchp->commutative[op_no + 1] = op_no;
1322 break;
1324 case '0': case '1': case '2': case '3': case '4':
1325 case '5': case '6': case '7': case '8': case '9':
1327 char *end;
1328 unsigned long match_ul = strtoul (p, &end, 10);
1329 int match = match_ul;
1331 p = end;
1333 if (match < op_no && likely_spilled[match])
1334 continue;
1335 matchp->with[op_no] = match;
1336 any_matches = 1;
1337 if (matchp->commutative[op_no] >= 0)
1338 matchp->with[matchp->commutative[op_no]] = match;
1340 continue;
1342 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1343 case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1344 case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1345 case 'C': case 'D': case 'W': case 'Y': case 'Z':
1346 if (targetm.class_likely_spilled_p (REG_CLASS_FROM_CONSTRAINT ((unsigned char) c, p)))
1347 likely_spilled[op_no] = 1;
1348 break;
1350 p += CONSTRAINT_LEN (c, p);
1353 return any_matches;
1358 static bool
1359 gate_handle_regmove (void)
1361 return (optimize > 0 && flag_regmove);
1365 namespace {
1367 const pass_data pass_data_regmove =
1369 RTL_PASS, /* type */
1370 "regmove", /* name */
1371 OPTGROUP_NONE, /* optinfo_flags */
1372 true, /* has_gate */
1373 true, /* has_execute */
1374 TV_REGMOVE, /* tv_id */
1375 0, /* properties_required */
1376 0, /* properties_provided */
1377 0, /* properties_destroyed */
1378 0, /* todo_flags_start */
1379 ( TODO_df_finish | TODO_verify_rtl_sharing ), /* todo_flags_finish */
1382 class pass_regmove : public rtl_opt_pass
1384 public:
1385 pass_regmove(gcc::context *ctxt)
1386 : rtl_opt_pass(pass_data_regmove, ctxt)
1389 /* opt_pass methods: */
1390 bool gate () { return gate_handle_regmove (); }
1391 unsigned int execute () { return regmove_optimize (); }
1393 }; // class pass_regmove
1395 } // anon namespace
1397 rtl_opt_pass *
1398 make_pass_regmove (gcc::context *ctxt)
1400 return new pass_regmove (ctxt);