Remove LIBGCC2_HAS_?F_MODE target macros.
[official-gcc.git] / gcc / regcprop.c
blob3297721df3919842986f51c44be3e910eede3996
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)
9 any later version.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "tm_p.h"
26 #include "insn-config.h"
27 #include "regs.h"
28 #include "addresses.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "reload.h"
32 #include "function.h"
33 #include "recog.h"
34 #include "flags.h"
35 #include "diagnostic-core.h"
36 #include "obstack.h"
37 #include "tree-pass.h"
38 #include "df.h"
39 #include "rtl-iter.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;
54 rtx_insn *insn;
55 rtx *loc;
56 rtx new_rtx;
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;
73 struct value_data
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,
92 unsigned int);
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 *,
97 struct value_data *);
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 *);
106 #endif
108 /* Free all queued updates for DEBUG_INSNs that change some reg to
109 register REGNO. */
111 static void
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)
117 next = 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
127 with REGNO. */
129 static void
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)
139 continue;
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);
156 #endif
159 /* Kill the value in register REGNO for NREGS, and any other registers
160 whose values overlap. */
162 static void
163 kill_value_regno (unsigned int regno, unsigned int nregs,
164 struct value_data *vd)
166 unsigned int j;
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)
174 j = 0;
175 else
176 j = regno - vd->max_value_regs;
177 for (; j < regno; ++j)
179 unsigned int i, n;
180 if (vd->e[j].mode == VOIDmode)
181 continue;
182 n = hard_regno_nregs[j][vd->e[j].mode];
183 if (j + n > regno)
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. */
192 static void
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);
201 if (REG_P (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. */
212 static void
213 set_value_regno (unsigned int regno, enum machine_mode mode,
214 struct value_data *vd)
216 unsigned int nregs;
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. */
227 static void
228 init_value_data (struct value_data *vd)
230 int i;
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. */
244 static void
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)
249 kill_value (x, vd);
252 /* A structure passed as data to kill_set_value through note_stores. */
253 struct kill_set_value_data
255 struct value_data *vd;
256 rtx ignore_set_reg;
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. */
262 static void
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))
267 return;
268 if (GET_CODE (set) != CLOBBER)
270 kill_value (x, ksvd->vd);
271 if (REG_P (x))
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. */
279 static void
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)
285 const_rtx x = *iter;
286 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
288 x = XEXP (x, 0);
289 kill_value (x, vd);
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. */
299 static void
300 copy_value (rtx dest, rtx src, struct value_data *vd)
302 unsigned int dr = REGNO (dest);
303 unsigned int sr = REGNO (src);
304 unsigned int dn, sn;
305 unsigned int i;
307 /* ??? At present, it's possible to see noop sets. It'd be nice if
308 this were cleaned up beforehand... */
309 if (sr == dr)
310 return;
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)
315 return;
317 /* Likewise with the frame pointer, if we're using one. */
318 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
319 return;
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])
325 return;
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))
332 return;
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
336 or somesuch. */
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))
358 return;
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])
364 return;
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)
371 continue;
372 vd->e[i].next_regno = dr;
374 #ifdef ENABLE_CHECKING
375 validate_value_data (vd);
376 #endif
379 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
381 static bool
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))
386 return false;
388 #ifdef CANNOT_CHANGE_MODE_CLASS
389 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
390 #endif
392 return true;
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
397 in NEW_MODE.
398 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
400 static 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))
407 return NULL_RTX;
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];
415 int copy_offset
416 = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
417 int offset
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);
428 return NULL_RTX;
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. */
435 static rtx
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);
440 unsigned int i;
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])
453 return NULL_RTX;
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;
459 rtx new_rtx;
461 if (!in_hard_reg_set_p (reg_class_contents[cl], mode, i))
462 continue;
464 new_rtx = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
465 if (new_rtx)
467 ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (reg);
468 REG_ATTRS (new_rtx) = REG_ATTRS (reg);
469 REG_POINTER (new_rtx) = REG_POINTER (reg);
470 return new_rtx;
474 return NULL_RTX;
477 /* If possible, replace the register at *LOC with the oldest register
478 in register class CL. Return true if successfully replaced. */
480 static bool
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;
491 if (dump_file)
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;
498 change->insn = insn;
499 change->loc = loc;
500 change->new_rtx = new_rtx;
501 vd->e[REGNO (new_rtx)].debug_insn_changes = change;
502 ++vd->n_debug_insn_changes;
503 return true;
505 if (dump_file)
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);
510 return true;
512 return false;
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. */
519 static bool
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)
524 rtx x = *loc;
525 RTX_CODE code = GET_CODE (x);
526 const char *fmt;
527 int i, j;
528 bool changed = false;
530 switch (code)
532 case PLUS:
533 if (DEBUG_INSN_P (insn))
534 break;
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);
541 rtx op0 = orig_op0;
542 rtx op1 = orig_op1;
543 rtx *locI = NULL;
544 rtx *locB = NULL;
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)
562 locI = &XEXP (x, 0);
563 locB = &XEXP (x, 1);
564 index_code = GET_CODE (*locI);
566 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
567 || code1 == ZERO_EXTEND || code0 == MEM)
569 locI = &XEXP (x, 1);
570 locB = &XEXP (x, 0);
571 index_code = GET_CODE (*locI);
573 else if (code0 == CONST_INT || code0 == CONST
574 || code0 == SYMBOL_REF || code0 == LABEL_REF)
576 locB = &XEXP (x, 1);
577 index_code = GET_CODE (XEXP (x, 0));
579 else if (code1 == CONST_INT || code1 == CONST
580 || code1 == SYMBOL_REF || code1 == LABEL_REF)
582 locB = &XEXP (x, 0);
583 index_code = GET_CODE (XEXP (x, 1));
585 else if (code0 == REG && code1 == REG)
587 int index_op;
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))
592 index_op = 1;
593 else if (REGNO_OK_FOR_INDEX_P (regno0)
594 && regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
595 index_op = 0;
596 else if (regno_ok_for_base_p (regno0, mode, as, PLUS, REG)
597 || REGNO_OK_FOR_INDEX_P (regno1))
598 index_op = 1;
599 else if (regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
600 index_op = 0;
601 else
602 index_op = 1;
604 locI = &XEXP (x, index_op);
605 locB = &XEXP (x, !index_op);
606 index_code = GET_CODE (*locI);
608 else if (code0 == REG)
610 locI = &XEXP (x, 0);
611 locB = &XEXP (x, 1);
612 index_code = GET_CODE (*locI);
614 else if (code1 == REG)
616 locI = &XEXP (x, 1);
617 locB = &XEXP (x, 0);
618 index_code = GET_CODE (*locI);
621 if (locI)
622 changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS,
623 mode, as, insn, vd);
624 if (locB)
625 changed |= replace_oldest_value_addr (locB,
626 base_reg_class (mode, as, PLUS,
627 index_code),
628 mode, as, insn, vd);
629 return changed;
632 case POST_INC:
633 case POST_DEC:
634 case POST_MODIFY:
635 case PRE_INC:
636 case PRE_DEC:
637 case PRE_MODIFY:
638 return false;
640 case MEM:
641 return replace_oldest_value_mem (x, insn, vd);
643 case REG:
644 return replace_oldest_value_reg (loc, cl, insn, vd);
646 default:
647 break;
650 fmt = GET_RTX_FORMAT (code);
651 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
653 if (fmt[i] == 'e')
654 changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode, as,
655 insn, vd);
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,
659 mode, as, insn, vd);
662 return changed;
665 /* Similar to replace_oldest_value_reg, but X contains a memory. */
667 static bool
668 replace_oldest_value_mem (rtx x, rtx_insn *insn, struct value_data *vd)
670 enum reg_class cl;
672 if (DEBUG_INSN_P (insn))
673 cl = ALL_REGS;
674 else
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),
679 insn, vd);
682 /* Apply all queued updates for DEBUG_INSNs that change some reg to
683 register REGNO. */
685 static void
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;
692 change;
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 note_uses, for all used registers in a real insn
706 apply DEBUG_INSN changes that change registers to the used
707 registers. */
709 static void
710 cprop_find_used_regs (rtx *loc, void *data)
712 struct value_data *const vd = (struct value_data *) data;
713 subrtx_iterator::array_type array;
714 FOR_EACH_SUBRTX (iter, array, *loc, NONCONST)
716 const_rtx x = *iter;
717 if (REG_P (x))
719 unsigned int regno = REGNO (x);
720 if (vd->e[regno].debug_insn_changes)
722 apply_debug_insn_changes (vd, regno);
723 free_debug_insn_changes (vd, regno);
729 /* Perform the forward copy propagation on basic block BB. */
731 static bool
732 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
734 bool anything_changed = false;
735 rtx_insn *insn;
737 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
739 int n_ops, i, predicated;
740 bool is_asm, any_replacements;
741 rtx set;
742 rtx link;
743 bool replaced[MAX_RECOG_OPERANDS];
744 bool changed = false;
745 struct kill_set_value_data ksvd;
747 if (!NONDEBUG_INSN_P (insn))
749 if (DEBUG_INSN_P (insn))
751 rtx loc = INSN_VAR_LOCATION_LOC (insn);
752 if (!VAR_LOC_UNKNOWN_P (loc))
753 replace_oldest_value_addr (&INSN_VAR_LOCATION_LOC (insn),
754 ALL_REGS, GET_MODE (loc),
755 ADDR_SPACE_GENERIC, insn, vd);
758 if (insn == BB_END (bb))
759 break;
760 else
761 continue;
764 set = single_set (insn);
765 extract_insn (insn);
766 if (! constrain_operands (1))
767 fatal_insn_not_found (insn);
768 preprocess_constraints (insn);
769 const operand_alternative *op_alt = which_op_alt ();
770 n_ops = recog_data.n_operands;
771 is_asm = asm_noperands (PATTERN (insn)) >= 0;
773 /* Simplify the code below by promoting OP_OUT to OP_INOUT
774 in predicated instructions. */
776 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
777 for (i = 0; i < n_ops; ++i)
779 int matches = op_alt[i].matches;
780 if (matches >= 0 || op_alt[i].matched >= 0
781 || (predicated && recog_data.operand_type[i] == OP_OUT))
782 recog_data.operand_type[i] = OP_INOUT;
785 /* Apply changes to earlier DEBUG_INSNs if possible. */
786 if (vd->n_debug_insn_changes)
787 note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
789 /* For each earlyclobber operand, zap the value data. */
790 for (i = 0; i < n_ops; i++)
791 if (op_alt[i].earlyclobber)
792 kill_value (recog_data.operand[i], vd);
794 /* Within asms, a clobber cannot overlap inputs or outputs.
795 I wouldn't think this were true for regular insns, but
796 scan_rtx treats them like that... */
797 note_stores (PATTERN (insn), kill_clobbered_value, vd);
799 /* Kill all auto-incremented values. */
800 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
801 kill_autoinc_value (insn, vd);
803 /* Kill all early-clobbered operands. */
804 for (i = 0; i < n_ops; i++)
805 if (op_alt[i].earlyclobber)
806 kill_value (recog_data.operand[i], vd);
808 /* If we have dead sets in the insn, then we need to note these as we
809 would clobbers. */
810 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
812 if (REG_NOTE_KIND (link) == REG_UNUSED)
814 kill_value (XEXP (link, 0), vd);
815 /* Furthermore, if the insn looked like a single-set,
816 but the dead store kills the source value of that
817 set, then we can no-longer use the plain move
818 special case below. */
819 if (set
820 && reg_overlap_mentioned_p (XEXP (link, 0), SET_SRC (set)))
821 set = NULL;
825 /* Special-case plain move instructions, since we may well
826 be able to do the move from a different register class. */
827 if (set && REG_P (SET_SRC (set)))
829 rtx src = SET_SRC (set);
830 unsigned int regno = REGNO (src);
831 enum machine_mode mode = GET_MODE (src);
832 unsigned int i;
833 rtx new_rtx;
835 /* If we are accessing SRC in some mode other that what we
836 set it in, make sure that the replacement is valid. */
837 if (mode != vd->e[regno].mode)
839 if (hard_regno_nregs[regno][mode]
840 > hard_regno_nregs[regno][vd->e[regno].mode])
841 goto no_move_special_case;
843 /* And likewise, if we are narrowing on big endian the transformation
844 is also invalid. */
845 if (hard_regno_nregs[regno][mode]
846 < hard_regno_nregs[regno][vd->e[regno].mode]
847 && (GET_MODE_SIZE (vd->e[regno].mode) > UNITS_PER_WORD
848 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
849 goto no_move_special_case;
852 /* If the destination is also a register, try to find a source
853 register in the same class. */
854 if (REG_P (SET_DEST (set)))
856 new_rtx = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
857 if (new_rtx && validate_change (insn, &SET_SRC (set), new_rtx, 0))
859 if (dump_file)
860 fprintf (dump_file,
861 "insn %u: replaced reg %u with %u\n",
862 INSN_UID (insn), regno, REGNO (new_rtx));
863 changed = true;
864 goto did_replacement;
866 /* We need to re-extract as validate_change clobbers
867 recog_data. */
868 extract_insn (insn);
869 if (! constrain_operands (1))
870 fatal_insn_not_found (insn);
871 preprocess_constraints (insn);
874 /* Otherwise, try all valid registers and see if its valid. */
875 for (i = vd->e[regno].oldest_regno; i != regno;
876 i = vd->e[i].next_regno)
878 new_rtx = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
879 mode, i, regno);
880 if (new_rtx != NULL_RTX)
882 if (validate_change (insn, &SET_SRC (set), new_rtx, 0))
884 ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (src);
885 REG_ATTRS (new_rtx) = REG_ATTRS (src);
886 REG_POINTER (new_rtx) = REG_POINTER (src);
887 if (dump_file)
888 fprintf (dump_file,
889 "insn %u: replaced reg %u with %u\n",
890 INSN_UID (insn), regno, REGNO (new_rtx));
891 changed = true;
892 goto did_replacement;
894 /* We need to re-extract as validate_change clobbers
895 recog_data. */
896 extract_insn (insn);
897 if (! constrain_operands (1))
898 fatal_insn_not_found (insn);
899 preprocess_constraints (insn);
903 no_move_special_case:
905 any_replacements = false;
907 /* For each input operand, replace a hard register with the
908 eldest live copy that's in an appropriate register class. */
909 for (i = 0; i < n_ops; i++)
911 replaced[i] = false;
913 /* Don't scan match_operand here, since we've no reg class
914 information to pass down. Any operands that we could
915 substitute in will be represented elsewhere. */
916 if (recog_data.constraints[i][0] == '\0')
917 continue;
919 /* Don't replace in asms intentionally referencing hard regs. */
920 if (is_asm && REG_P (recog_data.operand[i])
921 && (REGNO (recog_data.operand[i])
922 == ORIGINAL_REGNO (recog_data.operand[i])))
923 continue;
925 if (recog_data.operand_type[i] == OP_IN)
927 if (op_alt[i].is_address)
928 replaced[i]
929 = replace_oldest_value_addr (recog_data.operand_loc[i],
930 alternative_class (op_alt, i),
931 VOIDmode, ADDR_SPACE_GENERIC,
932 insn, vd);
933 else if (REG_P (recog_data.operand[i]))
934 replaced[i]
935 = replace_oldest_value_reg (recog_data.operand_loc[i],
936 alternative_class (op_alt, i),
937 insn, vd);
938 else if (MEM_P (recog_data.operand[i]))
939 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
940 insn, vd);
942 else if (MEM_P (recog_data.operand[i]))
943 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
944 insn, vd);
946 /* If we performed any replacement, update match_dups. */
947 if (replaced[i])
949 int j;
950 rtx new_rtx;
952 new_rtx = *recog_data.operand_loc[i];
953 recog_data.operand[i] = new_rtx;
954 for (j = 0; j < recog_data.n_dups; j++)
955 if (recog_data.dup_num[j] == i)
956 validate_unshare_change (insn, recog_data.dup_loc[j], new_rtx, 1);
958 any_replacements = true;
962 if (any_replacements)
964 if (! apply_change_group ())
966 for (i = 0; i < n_ops; i++)
967 if (replaced[i])
969 rtx old = *recog_data.operand_loc[i];
970 recog_data.operand[i] = old;
973 if (dump_file)
974 fprintf (dump_file,
975 "insn %u: reg replacements not verified\n",
976 INSN_UID (insn));
978 else
979 changed = true;
982 did_replacement:
983 if (changed)
985 anything_changed = true;
987 /* If something changed, perhaps further changes to earlier
988 DEBUG_INSNs can be applied. */
989 if (vd->n_debug_insn_changes)
990 note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
993 ksvd.vd = vd;
994 ksvd.ignore_set_reg = NULL_RTX;
996 /* Clobber call-clobbered registers. */
997 if (CALL_P (insn))
999 unsigned int set_regno = INVALID_REGNUM;
1000 unsigned int set_nregs = 0;
1001 unsigned int regno;
1002 rtx exp;
1004 for (exp = CALL_INSN_FUNCTION_USAGE (insn); exp; exp = XEXP (exp, 1))
1006 rtx x = XEXP (exp, 0);
1007 if (GET_CODE (x) == SET)
1009 rtx dest = SET_DEST (x);
1010 kill_value (dest, vd);
1011 set_value_regno (REGNO (dest), GET_MODE (dest), vd);
1012 copy_value (dest, SET_SRC (x), vd);
1013 ksvd.ignore_set_reg = dest;
1014 set_regno = REGNO (dest);
1015 set_nregs
1016 = hard_regno_nregs[set_regno][GET_MODE (dest)];
1017 break;
1021 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1022 if ((TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)
1023 || HARD_REGNO_CALL_PART_CLOBBERED (regno, vd->e[regno].mode))
1024 && (regno < set_regno || regno >= set_regno + set_nregs))
1025 kill_value_regno (regno, 1, vd);
1027 /* If SET was seen in CALL_INSN_FUNCTION_USAGE, and SET_SRC
1028 of the SET isn't in regs_invalidated_by_call hard reg set,
1029 but instead among CLOBBERs on the CALL_INSN, we could wrongly
1030 assume the value in it is still live. */
1031 if (ksvd.ignore_set_reg)
1032 note_stores (PATTERN (insn), kill_clobbered_value, vd);
1035 /* Notice stores. */
1036 note_stores (PATTERN (insn), kill_set_value, &ksvd);
1038 /* Notice copies. */
1039 if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1040 copy_value (SET_DEST (set), SET_SRC (set), vd);
1042 if (insn == BB_END (bb))
1043 break;
1046 return anything_changed;
1049 /* Dump the value chain data to stderr. */
1051 DEBUG_FUNCTION void
1052 debug_value_data (struct value_data *vd)
1054 HARD_REG_SET set;
1055 unsigned int i, j;
1057 CLEAR_HARD_REG_SET (set);
1059 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1060 if (vd->e[i].oldest_regno == i)
1062 if (vd->e[i].mode == VOIDmode)
1064 if (vd->e[i].next_regno != INVALID_REGNUM)
1065 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1066 i, vd->e[i].next_regno);
1067 continue;
1070 SET_HARD_REG_BIT (set, i);
1071 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1073 for (j = vd->e[i].next_regno;
1074 j != INVALID_REGNUM;
1075 j = vd->e[j].next_regno)
1077 if (TEST_HARD_REG_BIT (set, j))
1079 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1080 return;
1083 if (vd->e[j].oldest_regno != i)
1085 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1086 j, vd->e[j].oldest_regno);
1087 return;
1089 SET_HARD_REG_BIT (set, j);
1090 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1092 fputc ('\n', stderr);
1095 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1096 if (! TEST_HARD_REG_BIT (set, i)
1097 && (vd->e[i].mode != VOIDmode
1098 || vd->e[i].oldest_regno != i
1099 || vd->e[i].next_regno != INVALID_REGNUM))
1100 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1101 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1102 vd->e[i].next_regno);
1105 /* Do copyprop_hardreg_forward_1 for a single basic block BB.
1106 DEBUG_INSN is skipped since we do not want to involve DF related
1107 staff as how it is handled in function pass_cprop_hardreg::execute.
1109 NOTE: Currently it is only used for shrink-wrap. Maybe extend it
1110 to handle DEBUG_INSN for other uses. */
1112 void
1113 copyprop_hardreg_forward_bb_without_debug_insn (basic_block bb)
1115 struct value_data *vd;
1116 vd = XNEWVEC (struct value_data, 1);
1117 init_value_data (vd);
1119 skip_debug_insn_p = true;
1120 copyprop_hardreg_forward_1 (bb, vd);
1121 free (vd);
1122 skip_debug_insn_p = false;
1125 #ifdef ENABLE_CHECKING
1126 static void
1127 validate_value_data (struct value_data *vd)
1129 HARD_REG_SET set;
1130 unsigned int i, j;
1132 CLEAR_HARD_REG_SET (set);
1134 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1135 if (vd->e[i].oldest_regno == i)
1137 if (vd->e[i].mode == VOIDmode)
1139 if (vd->e[i].next_regno != INVALID_REGNUM)
1140 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1141 i, vd->e[i].next_regno);
1142 continue;
1145 SET_HARD_REG_BIT (set, i);
1147 for (j = vd->e[i].next_regno;
1148 j != INVALID_REGNUM;
1149 j = vd->e[j].next_regno)
1151 if (TEST_HARD_REG_BIT (set, j))
1152 internal_error ("validate_value_data: Loop in regno chain (%u)",
1154 if (vd->e[j].oldest_regno != i)
1155 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1156 j, vd->e[j].oldest_regno);
1158 SET_HARD_REG_BIT (set, j);
1162 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1163 if (! TEST_HARD_REG_BIT (set, i)
1164 && (vd->e[i].mode != VOIDmode
1165 || vd->e[i].oldest_regno != i
1166 || vd->e[i].next_regno != INVALID_REGNUM))
1167 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1168 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1169 vd->e[i].next_regno);
1171 #endif
1173 namespace {
1175 const pass_data pass_data_cprop_hardreg =
1177 RTL_PASS, /* type */
1178 "cprop_hardreg", /* name */
1179 OPTGROUP_NONE, /* optinfo_flags */
1180 TV_CPROP_REGISTERS, /* tv_id */
1181 0, /* properties_required */
1182 0, /* properties_provided */
1183 0, /* properties_destroyed */
1184 0, /* todo_flags_start */
1185 TODO_df_finish, /* todo_flags_finish */
1188 class pass_cprop_hardreg : public rtl_opt_pass
1190 public:
1191 pass_cprop_hardreg (gcc::context *ctxt)
1192 : rtl_opt_pass (pass_data_cprop_hardreg, ctxt)
1195 /* opt_pass methods: */
1196 virtual bool gate (function *)
1198 return (optimize > 0 && (flag_cprop_registers));
1201 virtual unsigned int execute (function *);
1203 }; // class pass_cprop_hardreg
1205 unsigned int
1206 pass_cprop_hardreg::execute (function *fun)
1208 struct value_data *all_vd;
1209 basic_block bb;
1210 sbitmap visited;
1211 bool analyze_called = false;
1213 all_vd = XNEWVEC (struct value_data, last_basic_block_for_fn (fun));
1215 visited = sbitmap_alloc (last_basic_block_for_fn (fun));
1216 bitmap_clear (visited);
1218 if (MAY_HAVE_DEBUG_INSNS)
1219 debug_insn_changes_pool
1220 = create_alloc_pool ("debug insn changes pool",
1221 sizeof (struct queued_debug_insn_change), 256);
1223 FOR_EACH_BB_FN (bb, fun)
1225 bitmap_set_bit (visited, bb->index);
1227 /* If a block has a single predecessor, that we've already
1228 processed, begin with the value data that was live at
1229 the end of the predecessor block. */
1230 /* ??? Ought to use more intelligent queuing of blocks. */
1231 if (single_pred_p (bb)
1232 && bitmap_bit_p (visited, single_pred (bb)->index)
1233 && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
1235 all_vd[bb->index] = all_vd[single_pred (bb)->index];
1236 if (all_vd[bb->index].n_debug_insn_changes)
1238 unsigned int regno;
1240 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1242 if (all_vd[bb->index].e[regno].debug_insn_changes)
1244 all_vd[bb->index].e[regno].debug_insn_changes = NULL;
1245 if (--all_vd[bb->index].n_debug_insn_changes == 0)
1246 break;
1251 else
1252 init_value_data (all_vd + bb->index);
1254 copyprop_hardreg_forward_1 (bb, all_vd + bb->index);
1257 if (MAY_HAVE_DEBUG_INSNS)
1259 FOR_EACH_BB_FN (bb, fun)
1260 if (bitmap_bit_p (visited, bb->index)
1261 && all_vd[bb->index].n_debug_insn_changes)
1263 unsigned int regno;
1264 bitmap live;
1266 if (!analyze_called)
1268 df_analyze ();
1269 analyze_called = true;
1271 live = df_get_live_out (bb);
1272 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1273 if (all_vd[bb->index].e[regno].debug_insn_changes)
1275 if (REGNO_REG_SET_P (live, regno))
1276 apply_debug_insn_changes (all_vd + bb->index, regno);
1277 if (all_vd[bb->index].n_debug_insn_changes == 0)
1278 break;
1282 free_alloc_pool (debug_insn_changes_pool);
1285 sbitmap_free (visited);
1286 free (all_vd);
1287 return 0;
1290 } // anon namespace
1292 rtl_opt_pass *
1293 make_pass_cprop_hardreg (gcc::context *ctxt)
1295 return new pass_cprop_hardreg (ctxt);