1 /* Copy propagation on hard registers for the GNU compiler.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License 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/>. */
22 #include "coretypes.h"
26 #include "insn-config.h"
28 #include "addresses.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
35 #include "diagnostic-core.h"
37 #include "tree-pass.h"
41 /* The following code does forward propagation of hard register copies.
42 The object is to eliminate as many dependencies as possible, so that
43 we have the most scheduling freedom. As a side effect, we also clean
44 up some silly register allocation decisions made by reload. This
45 code may be obsoleted by a new register allocator. */
47 /* DEBUG_INSNs aren't changed right away, as doing so might extend the
48 lifetime of a register and get the DEBUG_INSN subsequently reset.
49 So they are queued instead, and updated only when the register is
50 used in some subsequent real insn before it is set. */
51 struct queued_debug_insn_change
53 struct queued_debug_insn_change *next;
59 /* For each register, we have a list of registers that contain the same
60 value. The OLDEST_REGNO field points to the head of the list, and
61 the NEXT_REGNO field runs through the list. The MODE field indicates
62 what mode the data is known to be in; this field is VOIDmode when the
63 register is not known to contain valid data. */
65 struct value_data_entry
67 enum machine_mode mode;
68 unsigned int oldest_regno;
69 unsigned int next_regno;
70 struct queued_debug_insn_change *debug_insn_changes;
75 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
76 unsigned int max_value_regs;
77 unsigned int n_debug_insn_changes;
80 static alloc_pool debug_insn_changes_pool;
81 static bool skip_debug_insn_p;
83 static void kill_value_one_regno (unsigned, struct value_data *);
84 static void kill_value_regno (unsigned, unsigned, struct value_data *);
85 static void kill_value (const_rtx, struct value_data *);
86 static void set_value_regno (unsigned, enum machine_mode, struct value_data *);
87 static void init_value_data (struct value_data *);
88 static void kill_clobbered_value (rtx, const_rtx, void *);
89 static void kill_set_value (rtx, const_rtx, void *);
90 static void copy_value (rtx, rtx, struct value_data *);
91 static bool mode_change_ok (enum machine_mode, enum machine_mode,
93 static rtx maybe_mode_change (enum machine_mode, enum machine_mode,
94 enum machine_mode, unsigned int, unsigned int);
95 static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
96 static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx_insn *,
98 static bool replace_oldest_value_addr (rtx *, enum reg_class,
99 enum machine_mode, addr_space_t,
100 rtx_insn *, struct value_data *);
101 static bool replace_oldest_value_mem (rtx, rtx_insn *, struct value_data *);
102 static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
103 extern void debug_value_data (struct value_data *);
104 #ifdef ENABLE_CHECKING
105 static void validate_value_data (struct value_data *);
108 /* Free all queued updates for DEBUG_INSNs that change some reg to
112 free_debug_insn_changes (struct value_data *vd, unsigned int regno)
114 struct queued_debug_insn_change *cur, *next;
115 for (cur = vd->e[regno].debug_insn_changes; cur; cur = next)
118 --vd->n_debug_insn_changes;
119 pool_free (debug_insn_changes_pool, cur);
121 vd->e[regno].debug_insn_changes = NULL;
124 /* Kill register REGNO. This involves removing it from any value
125 lists, and resetting the value mode to VOIDmode. This is only a
126 helper function; it does not handle any hard registers overlapping
130 kill_value_one_regno (unsigned int regno, struct value_data *vd)
132 unsigned int i, next;
134 if (vd->e[regno].oldest_regno != regno)
136 for (i = vd->e[regno].oldest_regno;
137 vd->e[i].next_regno != regno;
138 i = vd->e[i].next_regno)
140 vd->e[i].next_regno = vd->e[regno].next_regno;
142 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
144 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
145 vd->e[i].oldest_regno = next;
148 vd->e[regno].mode = VOIDmode;
149 vd->e[regno].oldest_regno = regno;
150 vd->e[regno].next_regno = INVALID_REGNUM;
151 if (vd->e[regno].debug_insn_changes)
152 free_debug_insn_changes (vd, regno);
154 #ifdef ENABLE_CHECKING
155 validate_value_data (vd);
159 /* Kill the value in register REGNO for NREGS, and any other registers
160 whose values overlap. */
163 kill_value_regno (unsigned int regno, unsigned int nregs,
164 struct value_data *vd)
168 /* Kill the value we're told to kill. */
169 for (j = 0; j < nregs; ++j)
170 kill_value_one_regno (regno + j, vd);
172 /* Kill everything that overlapped what we're told to kill. */
173 if (regno < vd->max_value_regs)
176 j = regno - vd->max_value_regs;
177 for (; j < regno; ++j)
180 if (vd->e[j].mode == VOIDmode)
182 n = hard_regno_nregs[j][vd->e[j].mode];
184 for (i = 0; i < n; ++i)
185 kill_value_one_regno (j + i, vd);
189 /* Kill X. This is a convenience function wrapping kill_value_regno
190 so that we mind the mode the register is in. */
193 kill_value (const_rtx x, struct value_data *vd)
195 if (GET_CODE (x) == SUBREG)
197 rtx tmp = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
198 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
199 x = tmp ? tmp : SUBREG_REG (x);
203 unsigned int regno = REGNO (x);
204 unsigned int n = hard_regno_nregs[regno][GET_MODE (x)];
206 kill_value_regno (regno, n, vd);
210 /* Remember that REGNO is valid in MODE. */
213 set_value_regno (unsigned int regno, enum machine_mode mode,
214 struct value_data *vd)
218 vd->e[regno].mode = mode;
220 nregs = hard_regno_nregs[regno][mode];
221 if (nregs > vd->max_value_regs)
222 vd->max_value_regs = nregs;
225 /* Initialize VD such that there are no known relationships between regs. */
228 init_value_data (struct value_data *vd)
231 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
233 vd->e[i].mode = VOIDmode;
234 vd->e[i].oldest_regno = i;
235 vd->e[i].next_regno = INVALID_REGNUM;
236 vd->e[i].debug_insn_changes = NULL;
238 vd->max_value_regs = 0;
239 vd->n_debug_insn_changes = 0;
242 /* Called through note_stores. If X is clobbered, kill its value. */
245 kill_clobbered_value (rtx x, const_rtx set, void *data)
247 struct value_data *const vd = (struct value_data *) data;
248 if (GET_CODE (set) == CLOBBER)
252 /* A structure passed as data to kill_set_value through note_stores. */
253 struct kill_set_value_data
255 struct value_data *vd;
259 /* Called through note_stores. If X is set, not clobbered, kill its
260 current value and install it as the root of its own value list. */
263 kill_set_value (rtx x, const_rtx set, void *data)
265 struct kill_set_value_data *ksvd = (struct kill_set_value_data *) data;
266 if (rtx_equal_p (x, ksvd->ignore_set_reg))
268 if (GET_CODE (set) != CLOBBER)
270 kill_value (x, ksvd->vd);
272 set_value_regno (REGNO (x), GET_MODE (x), ksvd->vd);
276 /* Kill any register used in X as the base of an auto-increment expression,
277 and install that register as the root of its own value list. */
280 kill_autoinc_value (rtx insn, struct value_data *vd)
282 subrtx_iterator::array_type array;
283 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
286 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
290 set_value_regno (REGNO (x), GET_MODE (x), vd);
291 iter.skip_subrtxes ();
296 /* Assert that SRC has been copied to DEST. Adjust the data structures
297 to reflect that SRC contains an older copy of the shared value. */
300 copy_value (rtx dest, rtx src, struct value_data *vd)
302 unsigned int dr = REGNO (dest);
303 unsigned int sr = REGNO (src);
307 /* ??? At present, it's possible to see noop sets. It'd be nice if
308 this were cleaned up beforehand... */
312 /* Do not propagate copies to the stack pointer, as that can leave
313 memory accesses with no scheduling dependency on the stack update. */
314 if (dr == STACK_POINTER_REGNUM)
317 /* Likewise with the frame pointer, if we're using one. */
318 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
321 /* Do not propagate copies to fixed or global registers, patterns
322 can be relying to see particular fixed register or users can
323 expect the chosen global register in asm. */
324 if (fixed_regs[dr] || global_regs[dr])
327 /* If SRC and DEST overlap, don't record anything. */
328 dn = hard_regno_nregs[dr][GET_MODE (dest)];
329 sn = hard_regno_nregs[sr][GET_MODE (dest)];
330 if ((dr > sr && dr < sr + sn)
331 || (sr > dr && sr < dr + dn))
334 /* If SRC had no assigned mode (i.e. we didn't know it was live)
335 assign it now and assume the value came from an input argument
337 if (vd->e[sr].mode == VOIDmode)
338 set_value_regno (sr, vd->e[dr].mode, vd);
340 /* If we are narrowing the input to a smaller number of hard regs,
341 and it is in big endian, we are really extracting a high part.
342 Since we generally associate a low part of a value with the value itself,
343 we must not do the same for the high part.
344 Note we can still get low parts for the same mode combination through
345 a two-step copy involving differently sized hard regs.
346 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
347 (set (reg:DI r0) (reg:DI fr0))
348 (set (reg:SI fr2) (reg:SI r0))
349 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
350 (set (reg:SI fr2) (reg:SI fr0))
351 loads the high part of (reg:DI fr0) into fr2.
353 We can't properly represent the latter case in our tables, so don't
354 record anything then. */
355 else if (sn < (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode]
356 && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
357 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
360 /* If SRC had been assigned a mode narrower than the copy, we can't
361 link DEST into the chain, because not all of the pieces of the
362 copy came from oldest_regno. */
363 else if (sn > (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode])
366 /* Link DR at the end of the value chain used by SR. */
368 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
370 for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
372 vd->e[i].next_regno = dr;
374 #ifdef ENABLE_CHECKING
375 validate_value_data (vd);
379 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
382 mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
383 unsigned int regno ATTRIBUTE_UNUSED)
385 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
388 #ifdef CANNOT_CHANGE_MODE_CLASS
389 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
395 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
396 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
398 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
401 maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
402 enum machine_mode new_mode, unsigned int regno,
403 unsigned int copy_regno ATTRIBUTE_UNUSED)
405 if (GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (orig_mode)
406 && GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (new_mode))
409 if (orig_mode == new_mode)
410 return gen_rtx_raw_REG (new_mode, regno);
411 else if (mode_change_ok (orig_mode, new_mode, regno))
413 int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
414 int use_nregs = hard_regno_nregs[copy_regno][new_mode];
416 = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
418 = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
419 int byteoffset = offset % UNITS_PER_WORD;
420 int wordoffset = offset - byteoffset;
422 offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
423 + (BYTES_BIG_ENDIAN ? byteoffset : 0));
424 regno += subreg_regno_offset (regno, orig_mode, offset, new_mode);
425 if (HARD_REGNO_MODE_OK (regno, new_mode))
426 return gen_rtx_raw_REG (new_mode, regno);
431 /* Find the oldest copy of the value contained in REGNO that is in
432 register class CL and has mode MODE. If found, return an rtx
433 of that oldest register, otherwise return NULL. */
436 find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
438 unsigned int regno = REGNO (reg);
439 enum machine_mode mode = GET_MODE (reg);
442 /* If we are accessing REG in some mode other that what we set it in,
443 make sure that the replacement is valid. In particular, consider
444 (set (reg:DI r11) (...))
445 (set (reg:SI r9) (reg:SI r11))
446 (set (reg:SI r10) (...))
447 (set (...) (reg:DI r9))
448 Replacing r9 with r11 is invalid. */
449 if (mode != vd->e[regno].mode)
451 if (hard_regno_nregs[regno][mode]
452 > hard_regno_nregs[regno][vd->e[regno].mode])
456 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
458 enum machine_mode oldmode = vd->e[i].mode;
461 if (!in_hard_reg_set_p (reg_class_contents[cl], mode, i))
464 new_rtx = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
467 ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (reg);
468 REG_ATTRS (new_rtx) = REG_ATTRS (reg);
469 REG_POINTER (new_rtx) = REG_POINTER (reg);
477 /* If possible, replace the register at *LOC with the oldest register
478 in register class CL. Return true if successfully replaced. */
481 replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx_insn *insn,
482 struct value_data *vd)
484 rtx new_rtx = find_oldest_value_reg (cl, *loc, vd);
485 if (new_rtx && (!DEBUG_INSN_P (insn) || !skip_debug_insn_p))
487 if (DEBUG_INSN_P (insn))
489 struct queued_debug_insn_change *change;
492 fprintf (dump_file, "debug_insn %u: queued replacing reg %u with %u\n",
493 INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
495 change = (struct queued_debug_insn_change *)
496 pool_alloc (debug_insn_changes_pool);
497 change->next = vd->e[REGNO (new_rtx)].debug_insn_changes;
500 change->new_rtx = new_rtx;
501 vd->e[REGNO (new_rtx)].debug_insn_changes = change;
502 ++vd->n_debug_insn_changes;
506 fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
507 INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
509 validate_change (insn, loc, new_rtx, 1);
515 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
516 Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
517 BASE_REG_CLASS depending on how the register is being considered. */
520 replace_oldest_value_addr (rtx *loc, enum reg_class cl,
521 enum machine_mode mode, addr_space_t as,
522 rtx_insn *insn, struct value_data *vd)
525 RTX_CODE code = GET_CODE (x);
528 bool changed = false;
533 if (DEBUG_INSN_P (insn))
537 rtx orig_op0 = XEXP (x, 0);
538 rtx orig_op1 = XEXP (x, 1);
539 RTX_CODE code0 = GET_CODE (orig_op0);
540 RTX_CODE code1 = GET_CODE (orig_op1);
545 enum rtx_code index_code = SCRATCH;
547 if (GET_CODE (op0) == SUBREG)
549 op0 = SUBREG_REG (op0);
550 code0 = GET_CODE (op0);
553 if (GET_CODE (op1) == SUBREG)
555 op1 = SUBREG_REG (op1);
556 code1 = GET_CODE (op1);
559 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
560 || code0 == ZERO_EXTEND || code1 == MEM)
564 index_code = GET_CODE (*locI);
566 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
567 || code1 == ZERO_EXTEND || code0 == MEM)
571 index_code = GET_CODE (*locI);
573 else if (code0 == CONST_INT || code0 == CONST
574 || code0 == SYMBOL_REF || code0 == LABEL_REF)
577 index_code = GET_CODE (XEXP (x, 0));
579 else if (code1 == CONST_INT || code1 == CONST
580 || code1 == SYMBOL_REF || code1 == LABEL_REF)
583 index_code = GET_CODE (XEXP (x, 1));
585 else if (code0 == REG && code1 == REG)
588 unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
590 if (REGNO_OK_FOR_INDEX_P (regno1)
591 && regno_ok_for_base_p (regno0, mode, as, PLUS, REG))
593 else if (REGNO_OK_FOR_INDEX_P (regno0)
594 && regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
596 else if (regno_ok_for_base_p (regno0, mode, as, PLUS, REG)
597 || REGNO_OK_FOR_INDEX_P (regno1))
599 else if (regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
604 locI = &XEXP (x, index_op);
605 locB = &XEXP (x, !index_op);
606 index_code = GET_CODE (*locI);
608 else if (code0 == REG)
612 index_code = GET_CODE (*locI);
614 else if (code1 == REG)
618 index_code = GET_CODE (*locI);
622 changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS,
625 changed |= replace_oldest_value_addr (locB,
626 base_reg_class (mode, as, PLUS,
641 return replace_oldest_value_mem (x, insn, vd);
644 return replace_oldest_value_reg (loc, cl, insn, vd);
650 fmt = GET_RTX_FORMAT (code);
651 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
654 changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode, as,
656 else if (fmt[i] == 'E')
657 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
658 changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
665 /* Similar to replace_oldest_value_reg, but X contains a memory. */
668 replace_oldest_value_mem (rtx x, rtx_insn *insn, struct value_data *vd)
672 if (DEBUG_INSN_P (insn))
675 cl = base_reg_class (GET_MODE (x), MEM_ADDR_SPACE (x), MEM, SCRATCH);
677 return replace_oldest_value_addr (&XEXP (x, 0), cl,
678 GET_MODE (x), MEM_ADDR_SPACE (x),
682 /* Apply all queued updates for DEBUG_INSNs that change some reg to
686 apply_debug_insn_changes (struct value_data *vd, unsigned int regno)
688 struct queued_debug_insn_change *change;
689 rtx_insn *last_insn = vd->e[regno].debug_insn_changes->insn;
691 for (change = vd->e[regno].debug_insn_changes;
693 change = change->next)
695 if (last_insn != change->insn)
697 apply_change_group ();
698 last_insn = change->insn;
700 validate_change (change->insn, change->loc, change->new_rtx, 1);
702 apply_change_group ();
705 /* Called via for_each_rtx, for all used registers in a real
706 insn apply DEBUG_INSN changes that change registers to the
710 cprop_find_used_regs_1 (rtx *loc, void *data)
714 struct value_data *vd = (struct value_data *) data;
715 if (vd->e[REGNO (*loc)].debug_insn_changes)
717 apply_debug_insn_changes (vd, REGNO (*loc));
718 free_debug_insn_changes (vd, REGNO (*loc));
724 /* Called via note_uses, for all used registers in a real insn
725 apply DEBUG_INSN changes that change registers to the used
729 cprop_find_used_regs (rtx *loc, void *vd)
731 for_each_rtx (loc, cprop_find_used_regs_1, vd);
734 /* Perform the forward copy propagation on basic block BB. */
737 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
739 bool anything_changed = false;
742 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
744 int n_ops, i, predicated;
745 bool is_asm, any_replacements;
748 bool replaced[MAX_RECOG_OPERANDS];
749 bool changed = false;
750 struct kill_set_value_data ksvd;
752 if (!NONDEBUG_INSN_P (insn))
754 if (DEBUG_INSN_P (insn))
756 rtx loc = INSN_VAR_LOCATION_LOC (insn);
757 if (!VAR_LOC_UNKNOWN_P (loc))
758 replace_oldest_value_addr (&INSN_VAR_LOCATION_LOC (insn),
759 ALL_REGS, GET_MODE (loc),
760 ADDR_SPACE_GENERIC, insn, vd);
763 if (insn == BB_END (bb))
769 set = single_set (insn);
771 if (! constrain_operands (1))
772 fatal_insn_not_found (insn);
773 preprocess_constraints (insn);
774 const operand_alternative *op_alt = which_op_alt ();
775 n_ops = recog_data.n_operands;
776 is_asm = asm_noperands (PATTERN (insn)) >= 0;
778 /* Simplify the code below by promoting OP_OUT to OP_INOUT
779 in predicated instructions. */
781 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
782 for (i = 0; i < n_ops; ++i)
784 int matches = op_alt[i].matches;
785 if (matches >= 0 || op_alt[i].matched >= 0
786 || (predicated && recog_data.operand_type[i] == OP_OUT))
787 recog_data.operand_type[i] = OP_INOUT;
790 /* Apply changes to earlier DEBUG_INSNs if possible. */
791 if (vd->n_debug_insn_changes)
792 note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
794 /* For each earlyclobber operand, zap the value data. */
795 for (i = 0; i < n_ops; i++)
796 if (op_alt[i].earlyclobber)
797 kill_value (recog_data.operand[i], vd);
799 /* Within asms, a clobber cannot overlap inputs or outputs.
800 I wouldn't think this were true for regular insns, but
801 scan_rtx treats them like that... */
802 note_stores (PATTERN (insn), kill_clobbered_value, vd);
804 /* Kill all auto-incremented values. */
805 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
806 kill_autoinc_value (insn, vd);
808 /* Kill all early-clobbered operands. */
809 for (i = 0; i < n_ops; i++)
810 if (op_alt[i].earlyclobber)
811 kill_value (recog_data.operand[i], vd);
813 /* If we have dead sets in the insn, then we need to note these as we
815 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
817 if (REG_NOTE_KIND (link) == REG_UNUSED)
819 kill_value (XEXP (link, 0), vd);
820 /* Furthermore, if the insn looked like a single-set,
821 but the dead store kills the source value of that
822 set, then we can no-longer use the plain move
823 special case below. */
825 && reg_overlap_mentioned_p (XEXP (link, 0), SET_SRC (set)))
830 /* Special-case plain move instructions, since we may well
831 be able to do the move from a different register class. */
832 if (set && REG_P (SET_SRC (set)))
834 rtx src = SET_SRC (set);
835 unsigned int regno = REGNO (src);
836 enum machine_mode mode = GET_MODE (src);
840 /* If we are accessing SRC in some mode other that what we
841 set it in, make sure that the replacement is valid. */
842 if (mode != vd->e[regno].mode)
844 if (hard_regno_nregs[regno][mode]
845 > hard_regno_nregs[regno][vd->e[regno].mode])
846 goto no_move_special_case;
848 /* And likewise, if we are narrowing on big endian the transformation
850 if (hard_regno_nregs[regno][mode]
851 < hard_regno_nregs[regno][vd->e[regno].mode]
852 && (GET_MODE_SIZE (vd->e[regno].mode) > UNITS_PER_WORD
853 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
854 goto no_move_special_case;
857 /* If the destination is also a register, try to find a source
858 register in the same class. */
859 if (REG_P (SET_DEST (set)))
861 new_rtx = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
862 if (new_rtx && validate_change (insn, &SET_SRC (set), new_rtx, 0))
866 "insn %u: replaced reg %u with %u\n",
867 INSN_UID (insn), regno, REGNO (new_rtx));
869 goto did_replacement;
871 /* We need to re-extract as validate_change clobbers
874 if (! constrain_operands (1))
875 fatal_insn_not_found (insn);
876 preprocess_constraints (insn);
879 /* Otherwise, try all valid registers and see if its valid. */
880 for (i = vd->e[regno].oldest_regno; i != regno;
881 i = vd->e[i].next_regno)
883 new_rtx = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
885 if (new_rtx != NULL_RTX)
887 if (validate_change (insn, &SET_SRC (set), new_rtx, 0))
889 ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (src);
890 REG_ATTRS (new_rtx) = REG_ATTRS (src);
891 REG_POINTER (new_rtx) = REG_POINTER (src);
894 "insn %u: replaced reg %u with %u\n",
895 INSN_UID (insn), regno, REGNO (new_rtx));
897 goto did_replacement;
899 /* We need to re-extract as validate_change clobbers
902 if (! constrain_operands (1))
903 fatal_insn_not_found (insn);
904 preprocess_constraints (insn);
908 no_move_special_case:
910 any_replacements = false;
912 /* For each input operand, replace a hard register with the
913 eldest live copy that's in an appropriate register class. */
914 for (i = 0; i < n_ops; i++)
918 /* Don't scan match_operand here, since we've no reg class
919 information to pass down. Any operands that we could
920 substitute in will be represented elsewhere. */
921 if (recog_data.constraints[i][0] == '\0')
924 /* Don't replace in asms intentionally referencing hard regs. */
925 if (is_asm && REG_P (recog_data.operand[i])
926 && (REGNO (recog_data.operand[i])
927 == ORIGINAL_REGNO (recog_data.operand[i])))
930 if (recog_data.operand_type[i] == OP_IN)
932 if (op_alt[i].is_address)
934 = replace_oldest_value_addr (recog_data.operand_loc[i],
935 alternative_class (op_alt, i),
936 VOIDmode, ADDR_SPACE_GENERIC,
938 else if (REG_P (recog_data.operand[i]))
940 = replace_oldest_value_reg (recog_data.operand_loc[i],
941 alternative_class (op_alt, i),
943 else if (MEM_P (recog_data.operand[i]))
944 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
947 else if (MEM_P (recog_data.operand[i]))
948 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
951 /* If we performed any replacement, update match_dups. */
957 new_rtx = *recog_data.operand_loc[i];
958 recog_data.operand[i] = new_rtx;
959 for (j = 0; j < recog_data.n_dups; j++)
960 if (recog_data.dup_num[j] == i)
961 validate_unshare_change (insn, recog_data.dup_loc[j], new_rtx, 1);
963 any_replacements = true;
967 if (any_replacements)
969 if (! apply_change_group ())
971 for (i = 0; i < n_ops; i++)
974 rtx old = *recog_data.operand_loc[i];
975 recog_data.operand[i] = old;
980 "insn %u: reg replacements not verified\n",
990 anything_changed = true;
992 /* If something changed, perhaps further changes to earlier
993 DEBUG_INSNs can be applied. */
994 if (vd->n_debug_insn_changes)
995 note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
999 ksvd.ignore_set_reg = NULL_RTX;
1001 /* Clobber call-clobbered registers. */
1004 unsigned int set_regno = INVALID_REGNUM;
1005 unsigned int set_nregs = 0;
1009 for (exp = CALL_INSN_FUNCTION_USAGE (insn); exp; exp = XEXP (exp, 1))
1011 rtx x = XEXP (exp, 0);
1012 if (GET_CODE (x) == SET)
1014 rtx dest = SET_DEST (x);
1015 kill_value (dest, vd);
1016 set_value_regno (REGNO (dest), GET_MODE (dest), vd);
1017 copy_value (dest, SET_SRC (x), vd);
1018 ksvd.ignore_set_reg = dest;
1019 set_regno = REGNO (dest);
1021 = hard_regno_nregs[set_regno][GET_MODE (dest)];
1026 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1027 if ((TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)
1028 || HARD_REGNO_CALL_PART_CLOBBERED (regno, vd->e[regno].mode))
1029 && (regno < set_regno || regno >= set_regno + set_nregs))
1030 kill_value_regno (regno, 1, vd);
1032 /* If SET was seen in CALL_INSN_FUNCTION_USAGE, and SET_SRC
1033 of the SET isn't in regs_invalidated_by_call hard reg set,
1034 but instead among CLOBBERs on the CALL_INSN, we could wrongly
1035 assume the value in it is still live. */
1036 if (ksvd.ignore_set_reg)
1037 note_stores (PATTERN (insn), kill_clobbered_value, vd);
1040 /* Notice stores. */
1041 note_stores (PATTERN (insn), kill_set_value, &ksvd);
1043 /* Notice copies. */
1044 if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1045 copy_value (SET_DEST (set), SET_SRC (set), vd);
1047 if (insn == BB_END (bb))
1051 return anything_changed;
1054 /* Dump the value chain data to stderr. */
1057 debug_value_data (struct value_data *vd)
1062 CLEAR_HARD_REG_SET (set);
1064 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1065 if (vd->e[i].oldest_regno == i)
1067 if (vd->e[i].mode == VOIDmode)
1069 if (vd->e[i].next_regno != INVALID_REGNUM)
1070 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1071 i, vd->e[i].next_regno);
1075 SET_HARD_REG_BIT (set, i);
1076 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1078 for (j = vd->e[i].next_regno;
1079 j != INVALID_REGNUM;
1080 j = vd->e[j].next_regno)
1082 if (TEST_HARD_REG_BIT (set, j))
1084 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1088 if (vd->e[j].oldest_regno != i)
1090 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1091 j, vd->e[j].oldest_regno);
1094 SET_HARD_REG_BIT (set, j);
1095 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1097 fputc ('\n', stderr);
1100 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1101 if (! TEST_HARD_REG_BIT (set, i)
1102 && (vd->e[i].mode != VOIDmode
1103 || vd->e[i].oldest_regno != i
1104 || vd->e[i].next_regno != INVALID_REGNUM))
1105 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1106 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1107 vd->e[i].next_regno);
1110 /* Do copyprop_hardreg_forward_1 for a single basic block BB.
1111 DEBUG_INSN is skipped since we do not want to involve DF related
1112 staff as how it is handled in function pass_cprop_hardreg::execute.
1114 NOTE: Currently it is only used for shrink-wrap. Maybe extend it
1115 to handle DEBUG_INSN for other uses. */
1118 copyprop_hardreg_forward_bb_without_debug_insn (basic_block bb)
1120 struct value_data *vd;
1121 vd = XNEWVEC (struct value_data, 1);
1122 init_value_data (vd);
1124 skip_debug_insn_p = true;
1125 copyprop_hardreg_forward_1 (bb, vd);
1127 skip_debug_insn_p = false;
1130 #ifdef ENABLE_CHECKING
1132 validate_value_data (struct value_data *vd)
1137 CLEAR_HARD_REG_SET (set);
1139 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1140 if (vd->e[i].oldest_regno == i)
1142 if (vd->e[i].mode == VOIDmode)
1144 if (vd->e[i].next_regno != INVALID_REGNUM)
1145 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1146 i, vd->e[i].next_regno);
1150 SET_HARD_REG_BIT (set, i);
1152 for (j = vd->e[i].next_regno;
1153 j != INVALID_REGNUM;
1154 j = vd->e[j].next_regno)
1156 if (TEST_HARD_REG_BIT (set, j))
1157 internal_error ("validate_value_data: Loop in regno chain (%u)",
1159 if (vd->e[j].oldest_regno != i)
1160 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1161 j, vd->e[j].oldest_regno);
1163 SET_HARD_REG_BIT (set, j);
1167 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1168 if (! TEST_HARD_REG_BIT (set, i)
1169 && (vd->e[i].mode != VOIDmode
1170 || vd->e[i].oldest_regno != i
1171 || vd->e[i].next_regno != INVALID_REGNUM))
1172 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1173 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1174 vd->e[i].next_regno);
1180 const pass_data pass_data_cprop_hardreg =
1182 RTL_PASS, /* type */
1183 "cprop_hardreg", /* name */
1184 OPTGROUP_NONE, /* optinfo_flags */
1185 TV_CPROP_REGISTERS, /* tv_id */
1186 0, /* properties_required */
1187 0, /* properties_provided */
1188 0, /* properties_destroyed */
1189 0, /* todo_flags_start */
1190 TODO_df_finish, /* todo_flags_finish */
1193 class pass_cprop_hardreg : public rtl_opt_pass
1196 pass_cprop_hardreg (gcc::context *ctxt)
1197 : rtl_opt_pass (pass_data_cprop_hardreg, ctxt)
1200 /* opt_pass methods: */
1201 virtual bool gate (function *)
1203 return (optimize > 0 && (flag_cprop_registers));
1206 virtual unsigned int execute (function *);
1208 }; // class pass_cprop_hardreg
1211 pass_cprop_hardreg::execute (function *fun)
1213 struct value_data *all_vd;
1216 bool analyze_called = false;
1218 all_vd = XNEWVEC (struct value_data, last_basic_block_for_fn (fun));
1220 visited = sbitmap_alloc (last_basic_block_for_fn (fun));
1221 bitmap_clear (visited);
1223 if (MAY_HAVE_DEBUG_INSNS)
1224 debug_insn_changes_pool
1225 = create_alloc_pool ("debug insn changes pool",
1226 sizeof (struct queued_debug_insn_change), 256);
1228 FOR_EACH_BB_FN (bb, fun)
1230 bitmap_set_bit (visited, bb->index);
1232 /* If a block has a single predecessor, that we've already
1233 processed, begin with the value data that was live at
1234 the end of the predecessor block. */
1235 /* ??? Ought to use more intelligent queuing of blocks. */
1236 if (single_pred_p (bb)
1237 && bitmap_bit_p (visited, single_pred (bb)->index)
1238 && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
1240 all_vd[bb->index] = all_vd[single_pred (bb)->index];
1241 if (all_vd[bb->index].n_debug_insn_changes)
1245 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1247 if (all_vd[bb->index].e[regno].debug_insn_changes)
1249 all_vd[bb->index].e[regno].debug_insn_changes = NULL;
1250 if (--all_vd[bb->index].n_debug_insn_changes == 0)
1257 init_value_data (all_vd + bb->index);
1259 copyprop_hardreg_forward_1 (bb, all_vd + bb->index);
1262 if (MAY_HAVE_DEBUG_INSNS)
1264 FOR_EACH_BB_FN (bb, fun)
1265 if (bitmap_bit_p (visited, bb->index)
1266 && all_vd[bb->index].n_debug_insn_changes)
1271 if (!analyze_called)
1274 analyze_called = true;
1276 live = df_get_live_out (bb);
1277 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1278 if (all_vd[bb->index].e[regno].debug_insn_changes)
1280 if (REGNO_REG_SET_P (live, regno))
1281 apply_debug_insn_changes (all_vd + bb->index, regno);
1282 if (all_vd[bb->index].n_debug_insn_changes == 0)
1287 free_alloc_pool (debug_insn_changes_pool);
1290 sbitmap_free (visited);
1298 make_pass_cprop_hardreg (gcc::context *ctxt)
1300 return new pass_cprop_hardreg (ctxt);