* mf-impl.h: Fix typo.
[official-gcc.git] / gcc / regcprop.c
blob4d6ea29f7f7c2c9055e775e289ee7c704afdbfcf
1 /* Copy propagation on hard registers for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "insn-config.h"
28 #include "regs.h"
29 #include "addresses.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
32 #include "reload.h"
33 #include "output.h"
34 #include "function.h"
35 #include "recog.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "obstack.h"
39 #include "timevar.h"
40 #include "tree-pass.h"
41 #include "df.h"
43 /* The following code does forward propagation of hard register copies.
44 The object is to eliminate as many dependencies as possible, so that
45 we have the most scheduling freedom. As a side effect, we also clean
46 up some silly register allocation decisions made by reload. This
47 code may be obsoleted by a new register allocator. */
49 /* DEBUG_INSNs aren't changed right away, as doing so might extend the
50 lifetime of a register and get the DEBUG_INSN subsequently reset.
51 So they are queued instead, and updated only when the register is
52 used in some subsequent real insn before it is set. */
53 struct queued_debug_insn_change
55 struct queued_debug_insn_change *next;
56 rtx insn;
57 rtx *loc;
58 rtx new_rtx;
61 /* For each register, we have a list of registers that contain the same
62 value. The OLDEST_REGNO field points to the head of the list, and
63 the NEXT_REGNO field runs through the list. The MODE field indicates
64 what mode the data is known to be in; this field is VOIDmode when the
65 register is not known to contain valid data. */
67 struct value_data_entry
69 enum machine_mode mode;
70 unsigned int oldest_regno;
71 unsigned int next_regno;
72 struct queued_debug_insn_change *debug_insn_changes;
75 struct value_data
77 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
78 unsigned int max_value_regs;
79 unsigned int n_debug_insn_changes;
82 static alloc_pool debug_insn_changes_pool;
84 static void kill_value_one_regno (unsigned, struct value_data *);
85 static void kill_value_regno (unsigned, unsigned, struct value_data *);
86 static void kill_value (rtx, struct value_data *);
87 static void set_value_regno (unsigned, enum machine_mode, struct value_data *);
88 static void init_value_data (struct value_data *);
89 static void kill_clobbered_value (rtx, const_rtx, void *);
90 static void kill_set_value (rtx, const_rtx, void *);
91 static int kill_autoinc_value (rtx *, void *);
92 static void copy_value (rtx, rtx, struct value_data *);
93 static bool mode_change_ok (enum machine_mode, enum machine_mode,
94 unsigned int);
95 static rtx maybe_mode_change (enum machine_mode, enum machine_mode,
96 enum machine_mode, unsigned int, unsigned int);
97 static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
98 static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx,
99 struct value_data *);
100 static bool replace_oldest_value_addr (rtx *, enum reg_class,
101 enum machine_mode, addr_space_t, rtx,
102 struct value_data *);
103 static bool replace_oldest_value_mem (rtx, rtx, struct value_data *);
104 static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
105 extern void debug_value_data (struct value_data *);
106 #ifdef ENABLE_CHECKING
107 static void validate_value_data (struct value_data *);
108 #endif
110 /* Free all queued updates for DEBUG_INSNs that change some reg to
111 register REGNO. */
113 static void
114 free_debug_insn_changes (struct value_data *vd, unsigned int regno)
116 struct queued_debug_insn_change *cur, *next;
117 for (cur = vd->e[regno].debug_insn_changes; cur; cur = next)
119 next = cur->next;
120 --vd->n_debug_insn_changes;
121 pool_free (debug_insn_changes_pool, cur);
123 vd->e[regno].debug_insn_changes = NULL;
126 /* Kill register REGNO. This involves removing it from any value
127 lists, and resetting the value mode to VOIDmode. This is only a
128 helper function; it does not handle any hard registers overlapping
129 with REGNO. */
131 static void
132 kill_value_one_regno (unsigned int regno, struct value_data *vd)
134 unsigned int i, next;
136 if (vd->e[regno].oldest_regno != regno)
138 for (i = vd->e[regno].oldest_regno;
139 vd->e[i].next_regno != regno;
140 i = vd->e[i].next_regno)
141 continue;
142 vd->e[i].next_regno = vd->e[regno].next_regno;
144 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
146 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
147 vd->e[i].oldest_regno = next;
150 vd->e[regno].mode = VOIDmode;
151 vd->e[regno].oldest_regno = regno;
152 vd->e[regno].next_regno = INVALID_REGNUM;
153 if (vd->e[regno].debug_insn_changes)
154 free_debug_insn_changes (vd, regno);
156 #ifdef ENABLE_CHECKING
157 validate_value_data (vd);
158 #endif
161 /* Kill the value in register REGNO for NREGS, and any other registers
162 whose values overlap. */
164 static void
165 kill_value_regno (unsigned int regno, unsigned int nregs,
166 struct value_data *vd)
168 unsigned int j;
170 /* Kill the value we're told to kill. */
171 for (j = 0; j < nregs; ++j)
172 kill_value_one_regno (regno + j, vd);
174 /* Kill everything that overlapped what we're told to kill. */
175 if (regno < vd->max_value_regs)
176 j = 0;
177 else
178 j = regno - vd->max_value_regs;
179 for (; j < regno; ++j)
181 unsigned int i, n;
182 if (vd->e[j].mode == VOIDmode)
183 continue;
184 n = hard_regno_nregs[j][vd->e[j].mode];
185 if (j + n > regno)
186 for (i = 0; i < n; ++i)
187 kill_value_one_regno (j + i, vd);
191 /* Kill X. This is a convenience function wrapping kill_value_regno
192 so that we mind the mode the register is in. */
194 static void
195 kill_value (rtx x, struct value_data *vd)
197 rtx orig_rtx = x;
199 if (GET_CODE (x) == SUBREG)
201 x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
202 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
203 if (x == NULL_RTX)
204 x = SUBREG_REG (orig_rtx);
206 if (REG_P (x))
208 unsigned int regno = REGNO (x);
209 unsigned int n = hard_regno_nregs[regno][GET_MODE (x)];
211 kill_value_regno (regno, n, vd);
215 /* Remember that REGNO is valid in MODE. */
217 static void
218 set_value_regno (unsigned int regno, enum machine_mode mode,
219 struct value_data *vd)
221 unsigned int nregs;
223 vd->e[regno].mode = mode;
225 nregs = hard_regno_nregs[regno][mode];
226 if (nregs > vd->max_value_regs)
227 vd->max_value_regs = nregs;
230 /* Initialize VD such that there are no known relationships between regs. */
232 static void
233 init_value_data (struct value_data *vd)
235 int i;
236 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
238 vd->e[i].mode = VOIDmode;
239 vd->e[i].oldest_regno = i;
240 vd->e[i].next_regno = INVALID_REGNUM;
241 vd->e[i].debug_insn_changes = NULL;
243 vd->max_value_regs = 0;
244 vd->n_debug_insn_changes = 0;
247 /* Called through note_stores. If X is clobbered, kill its value. */
249 static void
250 kill_clobbered_value (rtx x, const_rtx set, void *data)
252 struct value_data *const vd = (struct value_data *) data;
253 if (GET_CODE (set) == CLOBBER)
254 kill_value (x, vd);
257 /* A structure passed as data to kill_set_value through note_stores. */
258 struct kill_set_value_data
260 struct value_data *vd;
261 rtx ignore_set_reg;
264 /* Called through note_stores. If X is set, not clobbered, kill its
265 current value and install it as the root of its own value list. */
267 static void
268 kill_set_value (rtx x, const_rtx set, void *data)
270 struct kill_set_value_data *ksvd = (struct kill_set_value_data *) data;
271 if (rtx_equal_p (x, ksvd->ignore_set_reg))
272 return;
273 if (GET_CODE (set) != CLOBBER)
275 kill_value (x, ksvd->vd);
276 if (REG_P (x))
277 set_value_regno (REGNO (x), GET_MODE (x), ksvd->vd);
281 /* Called through for_each_rtx. Kill any register used as the base of an
282 auto-increment expression, and install that register as the root of its
283 own value list. */
285 static int
286 kill_autoinc_value (rtx *px, void *data)
288 rtx x = *px;
289 struct value_data *const vd = (struct value_data *) data;
291 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
293 x = XEXP (x, 0);
294 kill_value (x, vd);
295 set_value_regno (REGNO (x), GET_MODE (x), vd);
296 return -1;
299 return 0;
302 /* Assert that SRC has been copied to DEST. Adjust the data structures
303 to reflect that SRC contains an older copy of the shared value. */
305 static void
306 copy_value (rtx dest, rtx src, struct value_data *vd)
308 unsigned int dr = REGNO (dest);
309 unsigned int sr = REGNO (src);
310 unsigned int dn, sn;
311 unsigned int i;
313 /* ??? At present, it's possible to see noop sets. It'd be nice if
314 this were cleaned up beforehand... */
315 if (sr == dr)
316 return;
318 /* Do not propagate copies to the stack pointer, as that can leave
319 memory accesses with no scheduling dependency on the stack update. */
320 if (dr == STACK_POINTER_REGNUM)
321 return;
323 /* Likewise with the frame pointer, if we're using one. */
324 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
325 return;
327 /* Do not propagate copies to fixed or global registers, patterns
328 can be relying to see particular fixed register or users can
329 expect the chosen global register in asm. */
330 if (fixed_regs[dr] || global_regs[dr])
331 return;
333 /* If SRC and DEST overlap, don't record anything. */
334 dn = hard_regno_nregs[dr][GET_MODE (dest)];
335 sn = hard_regno_nregs[sr][GET_MODE (dest)];
336 if ((dr > sr && dr < sr + sn)
337 || (sr > dr && sr < dr + dn))
338 return;
340 /* If SRC had no assigned mode (i.e. we didn't know it was live)
341 assign it now and assume the value came from an input argument
342 or somesuch. */
343 if (vd->e[sr].mode == VOIDmode)
344 set_value_regno (sr, vd->e[dr].mode, vd);
346 /* If we are narrowing the input to a smaller number of hard regs,
347 and it is in big endian, we are really extracting a high part.
348 Since we generally associate a low part of a value with the value itself,
349 we must not do the same for the high part.
350 Note we can still get low parts for the same mode combination through
351 a two-step copy involving differently sized hard regs.
352 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
353 (set (reg:DI r0) (reg:DI fr0))
354 (set (reg:SI fr2) (reg:SI r0))
355 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
356 (set (reg:SI fr2) (reg:SI fr0))
357 loads the high part of (reg:DI fr0) into fr2.
359 We can't properly represent the latter case in our tables, so don't
360 record anything then. */
361 else if (sn < (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode]
362 && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
363 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
364 return;
366 /* If SRC had been assigned a mode narrower than the copy, we can't
367 link DEST into the chain, because not all of the pieces of the
368 copy came from oldest_regno. */
369 else if (sn > (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode])
370 return;
372 /* Link DR at the end of the value chain used by SR. */
374 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
376 for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
377 continue;
378 vd->e[i].next_regno = dr;
380 #ifdef ENABLE_CHECKING
381 validate_value_data (vd);
382 #endif
385 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
387 static bool
388 mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
389 unsigned int regno ATTRIBUTE_UNUSED)
391 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
392 return false;
394 #ifdef CANNOT_CHANGE_MODE_CLASS
395 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
396 #endif
398 return true;
401 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
402 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
403 in NEW_MODE.
404 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
406 static rtx
407 maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
408 enum machine_mode new_mode, unsigned int regno,
409 unsigned int copy_regno ATTRIBUTE_UNUSED)
411 if (GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (orig_mode)
412 && GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (new_mode))
413 return NULL_RTX;
415 if (orig_mode == new_mode)
416 return gen_rtx_raw_REG (new_mode, regno);
417 else if (mode_change_ok (orig_mode, new_mode, regno))
419 int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
420 int use_nregs = hard_regno_nregs[copy_regno][new_mode];
421 int copy_offset
422 = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
423 int offset
424 = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
425 int byteoffset = offset % UNITS_PER_WORD;
426 int wordoffset = offset - byteoffset;
428 offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
429 + (BYTES_BIG_ENDIAN ? byteoffset : 0));
430 regno += subreg_regno_offset (regno, orig_mode, offset, new_mode);
431 if (HARD_REGNO_MODE_OK (regno, new_mode))
432 return gen_rtx_raw_REG (new_mode, regno);
434 return NULL_RTX;
437 /* Find the oldest copy of the value contained in REGNO that is in
438 register class CL and has mode MODE. If found, return an rtx
439 of that oldest register, otherwise return NULL. */
441 static rtx
442 find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
444 unsigned int regno = REGNO (reg);
445 enum machine_mode mode = GET_MODE (reg);
446 unsigned int i;
448 /* If we are accessing REG in some mode other that what we set it in,
449 make sure that the replacement is valid. In particular, consider
450 (set (reg:DI r11) (...))
451 (set (reg:SI r9) (reg:SI r11))
452 (set (reg:SI r10) (...))
453 (set (...) (reg:DI r9))
454 Replacing r9 with r11 is invalid. */
455 if (mode != vd->e[regno].mode)
457 if (hard_regno_nregs[regno][mode]
458 > hard_regno_nregs[regno][vd->e[regno].mode])
459 return NULL_RTX;
462 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
464 enum machine_mode oldmode = vd->e[i].mode;
465 rtx new_rtx;
467 if (!in_hard_reg_set_p (reg_class_contents[cl], mode, i))
468 continue;
470 new_rtx = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
471 if (new_rtx)
473 ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (reg);
474 REG_ATTRS (new_rtx) = REG_ATTRS (reg);
475 REG_POINTER (new_rtx) = REG_POINTER (reg);
476 return new_rtx;
480 return NULL_RTX;
483 /* If possible, replace the register at *LOC with the oldest register
484 in register class CL. Return true if successfully replaced. */
486 static bool
487 replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx insn,
488 struct value_data *vd)
490 rtx new_rtx = find_oldest_value_reg (cl, *loc, vd);
491 if (new_rtx)
493 if (DEBUG_INSN_P (insn))
495 struct queued_debug_insn_change *change;
497 if (dump_file)
498 fprintf (dump_file, "debug_insn %u: queued replacing reg %u with %u\n",
499 INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
501 change = (struct queued_debug_insn_change *)
502 pool_alloc (debug_insn_changes_pool);
503 change->next = vd->e[REGNO (new_rtx)].debug_insn_changes;
504 change->insn = insn;
505 change->loc = loc;
506 change->new_rtx = new_rtx;
507 vd->e[REGNO (new_rtx)].debug_insn_changes = change;
508 ++vd->n_debug_insn_changes;
509 return true;
511 if (dump_file)
512 fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
513 INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
515 validate_change (insn, loc, new_rtx, 1);
516 return true;
518 return false;
521 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
522 Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
523 BASE_REG_CLASS depending on how the register is being considered. */
525 static bool
526 replace_oldest_value_addr (rtx *loc, enum reg_class cl,
527 enum machine_mode mode, addr_space_t as,
528 rtx insn, struct value_data *vd)
530 rtx x = *loc;
531 RTX_CODE code = GET_CODE (x);
532 const char *fmt;
533 int i, j;
534 bool changed = false;
536 switch (code)
538 case PLUS:
539 if (DEBUG_INSN_P (insn))
540 break;
543 rtx orig_op0 = XEXP (x, 0);
544 rtx orig_op1 = XEXP (x, 1);
545 RTX_CODE code0 = GET_CODE (orig_op0);
546 RTX_CODE code1 = GET_CODE (orig_op1);
547 rtx op0 = orig_op0;
548 rtx op1 = orig_op1;
549 rtx *locI = NULL;
550 rtx *locB = NULL;
551 enum rtx_code index_code = SCRATCH;
553 if (GET_CODE (op0) == SUBREG)
555 op0 = SUBREG_REG (op0);
556 code0 = GET_CODE (op0);
559 if (GET_CODE (op1) == SUBREG)
561 op1 = SUBREG_REG (op1);
562 code1 = GET_CODE (op1);
565 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
566 || code0 == ZERO_EXTEND || code1 == MEM)
568 locI = &XEXP (x, 0);
569 locB = &XEXP (x, 1);
570 index_code = GET_CODE (*locI);
572 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
573 || code1 == ZERO_EXTEND || code0 == MEM)
575 locI = &XEXP (x, 1);
576 locB = &XEXP (x, 0);
577 index_code = GET_CODE (*locI);
579 else if (code0 == CONST_INT || code0 == CONST
580 || code0 == SYMBOL_REF || code0 == LABEL_REF)
582 locB = &XEXP (x, 1);
583 index_code = GET_CODE (XEXP (x, 0));
585 else if (code1 == CONST_INT || code1 == CONST
586 || code1 == SYMBOL_REF || code1 == LABEL_REF)
588 locB = &XEXP (x, 0);
589 index_code = GET_CODE (XEXP (x, 1));
591 else if (code0 == REG && code1 == REG)
593 int index_op;
594 unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
596 if (REGNO_OK_FOR_INDEX_P (regno1)
597 && regno_ok_for_base_p (regno0, mode, as, PLUS, REG))
598 index_op = 1;
599 else if (REGNO_OK_FOR_INDEX_P (regno0)
600 && regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
601 index_op = 0;
602 else if (regno_ok_for_base_p (regno0, mode, as, PLUS, REG)
603 || REGNO_OK_FOR_INDEX_P (regno1))
604 index_op = 1;
605 else if (regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
606 index_op = 0;
607 else
608 index_op = 1;
610 locI = &XEXP (x, index_op);
611 locB = &XEXP (x, !index_op);
612 index_code = GET_CODE (*locI);
614 else if (code0 == REG)
616 locI = &XEXP (x, 0);
617 locB = &XEXP (x, 1);
618 index_code = GET_CODE (*locI);
620 else if (code1 == REG)
622 locI = &XEXP (x, 1);
623 locB = &XEXP (x, 0);
624 index_code = GET_CODE (*locI);
627 if (locI)
628 changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS,
629 mode, as, insn, vd);
630 if (locB)
631 changed |= replace_oldest_value_addr (locB,
632 base_reg_class (mode, as, PLUS,
633 index_code),
634 mode, as, insn, vd);
635 return changed;
638 case POST_INC:
639 case POST_DEC:
640 case POST_MODIFY:
641 case PRE_INC:
642 case PRE_DEC:
643 case PRE_MODIFY:
644 return false;
646 case MEM:
647 return replace_oldest_value_mem (x, insn, vd);
649 case REG:
650 return replace_oldest_value_reg (loc, cl, insn, vd);
652 default:
653 break;
656 fmt = GET_RTX_FORMAT (code);
657 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
659 if (fmt[i] == 'e')
660 changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode, as,
661 insn, vd);
662 else if (fmt[i] == 'E')
663 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
664 changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
665 mode, as, insn, vd);
668 return changed;
671 /* Similar to replace_oldest_value_reg, but X contains a memory. */
673 static bool
674 replace_oldest_value_mem (rtx x, rtx insn, struct value_data *vd)
676 enum reg_class cl;
678 if (DEBUG_INSN_P (insn))
679 cl = ALL_REGS;
680 else
681 cl = base_reg_class (GET_MODE (x), MEM_ADDR_SPACE (x), MEM, SCRATCH);
683 return replace_oldest_value_addr (&XEXP (x, 0), cl,
684 GET_MODE (x), MEM_ADDR_SPACE (x),
685 insn, vd);
688 /* Apply all queued updates for DEBUG_INSNs that change some reg to
689 register REGNO. */
691 static void
692 apply_debug_insn_changes (struct value_data *vd, unsigned int regno)
694 struct queued_debug_insn_change *change;
695 rtx last_insn = vd->e[regno].debug_insn_changes->insn;
697 for (change = vd->e[regno].debug_insn_changes;
698 change;
699 change = change->next)
701 if (last_insn != change->insn)
703 apply_change_group ();
704 last_insn = change->insn;
706 validate_change (change->insn, change->loc, change->new_rtx, 1);
708 apply_change_group ();
711 /* Called via for_each_rtx, for all used registers in a real
712 insn apply DEBUG_INSN changes that change registers to the
713 used register. */
715 static int
716 cprop_find_used_regs_1 (rtx *loc, void *data)
718 if (REG_P (*loc))
720 struct value_data *vd = (struct value_data *) data;
721 if (vd->e[REGNO (*loc)].debug_insn_changes)
723 apply_debug_insn_changes (vd, REGNO (*loc));
724 free_debug_insn_changes (vd, REGNO (*loc));
727 return 0;
730 /* Called via note_uses, for all used registers in a real insn
731 apply DEBUG_INSN changes that change registers to the used
732 registers. */
734 static void
735 cprop_find_used_regs (rtx *loc, void *vd)
737 for_each_rtx (loc, cprop_find_used_regs_1, vd);
740 /* Perform the forward copy propagation on basic block BB. */
742 static bool
743 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
745 bool anything_changed = false;
746 rtx insn;
748 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
750 int n_ops, i, alt, predicated;
751 bool is_asm, any_replacements;
752 rtx set;
753 bool replaced[MAX_RECOG_OPERANDS];
754 bool changed = false;
755 struct kill_set_value_data ksvd;
757 if (!NONDEBUG_INSN_P (insn))
759 if (DEBUG_INSN_P (insn))
761 rtx loc = INSN_VAR_LOCATION_LOC (insn);
762 if (!VAR_LOC_UNKNOWN_P (loc))
763 replace_oldest_value_addr (&INSN_VAR_LOCATION_LOC (insn),
764 ALL_REGS, GET_MODE (loc),
765 ADDR_SPACE_GENERIC, insn, vd);
768 if (insn == BB_END (bb))
769 break;
770 else
771 continue;
774 set = single_set (insn);
775 extract_insn (insn);
776 if (! constrain_operands (1))
777 fatal_insn_not_found (insn);
778 preprocess_constraints ();
779 alt = which_alternative;
780 n_ops = recog_data.n_operands;
781 is_asm = asm_noperands (PATTERN (insn)) >= 0;
783 /* Simplify the code below by rewriting things to reflect
784 matching constraints. Also promote OP_OUT to OP_INOUT
785 in predicated instructions. */
787 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
788 for (i = 0; i < n_ops; ++i)
790 int matches = recog_op_alt[i][alt].matches;
791 if (matches >= 0)
792 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
793 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
794 || (predicated && recog_data.operand_type[i] == OP_OUT))
795 recog_data.operand_type[i] = OP_INOUT;
798 /* Apply changes to earlier DEBUG_INSNs if possible. */
799 if (vd->n_debug_insn_changes)
800 note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
802 /* For each earlyclobber operand, zap the value data. */
803 for (i = 0; i < n_ops; i++)
804 if (recog_op_alt[i][alt].earlyclobber)
805 kill_value (recog_data.operand[i], vd);
807 /* Within asms, a clobber cannot overlap inputs or outputs.
808 I wouldn't think this were true for regular insns, but
809 scan_rtx treats them like that... */
810 note_stores (PATTERN (insn), kill_clobbered_value, vd);
812 /* Kill all auto-incremented values. */
813 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
814 for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
816 /* Kill all early-clobbered operands. */
817 for (i = 0; i < n_ops; i++)
818 if (recog_op_alt[i][alt].earlyclobber)
819 kill_value (recog_data.operand[i], vd);
821 /* Special-case plain move instructions, since we may well
822 be able to do the move from a different register class. */
823 if (set && REG_P (SET_SRC (set)))
825 rtx src = SET_SRC (set);
826 unsigned int regno = REGNO (src);
827 enum machine_mode mode = GET_MODE (src);
828 unsigned int i;
829 rtx new_rtx;
831 /* If we are accessing SRC in some mode other that what we
832 set it in, make sure that the replacement is valid. */
833 if (mode != vd->e[regno].mode)
835 if (hard_regno_nregs[regno][mode]
836 > hard_regno_nregs[regno][vd->e[regno].mode])
837 goto no_move_special_case;
839 /* And likewise, if we are narrowing on big endian the transformation
840 is also invalid. */
841 if (hard_regno_nregs[regno][mode]
842 < hard_regno_nregs[regno][vd->e[regno].mode]
843 && (GET_MODE_SIZE (vd->e[regno].mode) > UNITS_PER_WORD
844 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
845 goto no_move_special_case;
848 /* If the destination is also a register, try to find a source
849 register in the same class. */
850 if (REG_P (SET_DEST (set)))
852 new_rtx = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
853 if (new_rtx && validate_change (insn, &SET_SRC (set), new_rtx, 0))
855 if (dump_file)
856 fprintf (dump_file,
857 "insn %u: replaced reg %u with %u\n",
858 INSN_UID (insn), regno, REGNO (new_rtx));
859 changed = true;
860 goto did_replacement;
862 /* We need to re-extract as validate_change clobbers
863 recog_data. */
864 extract_insn (insn);
865 if (! constrain_operands (1))
866 fatal_insn_not_found (insn);
867 preprocess_constraints ();
870 /* Otherwise, try all valid registers and see if its valid. */
871 for (i = vd->e[regno].oldest_regno; i != regno;
872 i = vd->e[i].next_regno)
874 new_rtx = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
875 mode, i, regno);
876 if (new_rtx != NULL_RTX)
878 if (validate_change (insn, &SET_SRC (set), new_rtx, 0))
880 ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (src);
881 REG_ATTRS (new_rtx) = REG_ATTRS (src);
882 REG_POINTER (new_rtx) = REG_POINTER (src);
883 if (dump_file)
884 fprintf (dump_file,
885 "insn %u: replaced reg %u with %u\n",
886 INSN_UID (insn), regno, REGNO (new_rtx));
887 changed = true;
888 goto did_replacement;
890 /* We need to re-extract as validate_change clobbers
891 recog_data. */
892 extract_insn (insn);
893 if (! constrain_operands (1))
894 fatal_insn_not_found (insn);
895 preprocess_constraints ();
899 no_move_special_case:
901 any_replacements = false;
903 /* For each input operand, replace a hard register with the
904 eldest live copy that's in an appropriate register class. */
905 for (i = 0; i < n_ops; i++)
907 replaced[i] = false;
909 /* Don't scan match_operand here, since we've no reg class
910 information to pass down. Any operands that we could
911 substitute in will be represented elsewhere. */
912 if (recog_data.constraints[i][0] == '\0')
913 continue;
915 /* Don't replace in asms intentionally referencing hard regs. */
916 if (is_asm && REG_P (recog_data.operand[i])
917 && (REGNO (recog_data.operand[i])
918 == ORIGINAL_REGNO (recog_data.operand[i])))
919 continue;
921 if (recog_data.operand_type[i] == OP_IN)
923 if (recog_op_alt[i][alt].is_address)
924 replaced[i]
925 = replace_oldest_value_addr (recog_data.operand_loc[i],
926 recog_op_alt[i][alt].cl,
927 VOIDmode, ADDR_SPACE_GENERIC,
928 insn, vd);
929 else if (REG_P (recog_data.operand[i]))
930 replaced[i]
931 = replace_oldest_value_reg (recog_data.operand_loc[i],
932 recog_op_alt[i][alt].cl,
933 insn, vd);
934 else if (MEM_P (recog_data.operand[i]))
935 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
936 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 /* If we performed any replacement, update match_dups. */
943 if (replaced[i])
945 int j;
946 rtx new_rtx;
948 new_rtx = *recog_data.operand_loc[i];
949 recog_data.operand[i] = new_rtx;
950 for (j = 0; j < recog_data.n_dups; j++)
951 if (recog_data.dup_num[j] == i)
952 validate_unshare_change (insn, recog_data.dup_loc[j], new_rtx, 1);
954 any_replacements = true;
958 if (any_replacements)
960 if (! apply_change_group ())
962 for (i = 0; i < n_ops; i++)
963 if (replaced[i])
965 rtx old = *recog_data.operand_loc[i];
966 recog_data.operand[i] = old;
969 if (dump_file)
970 fprintf (dump_file,
971 "insn %u: reg replacements not verified\n",
972 INSN_UID (insn));
974 else
975 changed = true;
978 did_replacement:
979 if (changed)
981 anything_changed = true;
983 /* If something changed, perhaps further changes to earlier
984 DEBUG_INSNs can be applied. */
985 if (vd->n_debug_insn_changes)
986 note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
989 ksvd.vd = vd;
990 ksvd.ignore_set_reg = NULL_RTX;
992 /* Clobber call-clobbered registers. */
993 if (CALL_P (insn))
995 int set_regno = INVALID_REGNUM;
996 int set_nregs = 0;
997 rtx exp;
998 for (exp = CALL_INSN_FUNCTION_USAGE (insn); exp; exp = XEXP (exp, 1))
1000 rtx x = XEXP (exp, 0);
1001 if (GET_CODE (x) == SET)
1003 rtx dest = SET_DEST (x);
1004 kill_value (dest, vd);
1005 set_value_regno (REGNO (dest), GET_MODE (dest), vd);
1006 copy_value (dest, SET_SRC (x), vd);
1007 ksvd.ignore_set_reg = dest;
1008 set_regno = REGNO (dest);
1009 set_nregs
1010 = hard_regno_nregs[set_regno][GET_MODE (dest)];
1011 break;
1014 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1015 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
1016 && (i < set_regno || i >= set_regno + set_nregs))
1017 kill_value_regno (i, 1, vd);
1020 /* Notice stores. */
1021 note_stores (PATTERN (insn), kill_set_value, &ksvd);
1023 /* Notice copies. */
1024 if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1025 copy_value (SET_DEST (set), SET_SRC (set), vd);
1027 if (insn == BB_END (bb))
1028 break;
1031 return anything_changed;
1034 /* Main entry point for the forward copy propagation optimization. */
1036 static unsigned int
1037 copyprop_hardreg_forward (void)
1039 struct value_data *all_vd;
1040 basic_block bb;
1041 sbitmap visited;
1042 bool analyze_called = false;
1044 all_vd = XNEWVEC (struct value_data, last_basic_block);
1046 visited = sbitmap_alloc (last_basic_block);
1047 sbitmap_zero (visited);
1049 if (MAY_HAVE_DEBUG_INSNS)
1050 debug_insn_changes_pool
1051 = create_alloc_pool ("debug insn changes pool",
1052 sizeof (struct queued_debug_insn_change), 256);
1054 FOR_EACH_BB (bb)
1056 SET_BIT (visited, bb->index);
1058 /* If a block has a single predecessor, that we've already
1059 processed, begin with the value data that was live at
1060 the end of the predecessor block. */
1061 /* ??? Ought to use more intelligent queuing of blocks. */
1062 if (single_pred_p (bb)
1063 && TEST_BIT (visited, single_pred (bb)->index)
1064 && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
1066 all_vd[bb->index] = all_vd[single_pred (bb)->index];
1067 if (all_vd[bb->index].n_debug_insn_changes)
1069 unsigned int regno;
1071 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1073 if (all_vd[bb->index].e[regno].debug_insn_changes)
1075 all_vd[bb->index].e[regno].debug_insn_changes = NULL;
1076 if (--all_vd[bb->index].n_debug_insn_changes == 0)
1077 break;
1082 else
1083 init_value_data (all_vd + bb->index);
1085 copyprop_hardreg_forward_1 (bb, all_vd + bb->index);
1088 if (MAY_HAVE_DEBUG_INSNS)
1090 FOR_EACH_BB (bb)
1091 if (TEST_BIT (visited, bb->index)
1092 && all_vd[bb->index].n_debug_insn_changes)
1094 unsigned int regno;
1095 bitmap live;
1097 if (!analyze_called)
1099 df_analyze ();
1100 analyze_called = true;
1102 live = df_get_live_out (bb);
1103 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1104 if (all_vd[bb->index].e[regno].debug_insn_changes)
1106 if (REGNO_REG_SET_P (live, regno))
1107 apply_debug_insn_changes (all_vd + bb->index, regno);
1108 if (all_vd[bb->index].n_debug_insn_changes == 0)
1109 break;
1113 free_alloc_pool (debug_insn_changes_pool);
1116 sbitmap_free (visited);
1117 free (all_vd);
1118 return 0;
1121 /* Dump the value chain data to stderr. */
1123 DEBUG_FUNCTION void
1124 debug_value_data (struct value_data *vd)
1126 HARD_REG_SET set;
1127 unsigned int i, j;
1129 CLEAR_HARD_REG_SET (set);
1131 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1132 if (vd->e[i].oldest_regno == i)
1134 if (vd->e[i].mode == VOIDmode)
1136 if (vd->e[i].next_regno != INVALID_REGNUM)
1137 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1138 i, vd->e[i].next_regno);
1139 continue;
1142 SET_HARD_REG_BIT (set, i);
1143 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1145 for (j = vd->e[i].next_regno;
1146 j != INVALID_REGNUM;
1147 j = vd->e[j].next_regno)
1149 if (TEST_HARD_REG_BIT (set, j))
1151 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1152 return;
1155 if (vd->e[j].oldest_regno != i)
1157 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1158 j, vd->e[j].oldest_regno);
1159 return;
1161 SET_HARD_REG_BIT (set, j);
1162 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1164 fputc ('\n', stderr);
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 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1173 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1174 vd->e[i].next_regno);
1177 #ifdef ENABLE_CHECKING
1178 static void
1179 validate_value_data (struct value_data *vd)
1181 HARD_REG_SET set;
1182 unsigned int i, j;
1184 CLEAR_HARD_REG_SET (set);
1186 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1187 if (vd->e[i].oldest_regno == i)
1189 if (vd->e[i].mode == VOIDmode)
1191 if (vd->e[i].next_regno != INVALID_REGNUM)
1192 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1193 i, vd->e[i].next_regno);
1194 continue;
1197 SET_HARD_REG_BIT (set, i);
1199 for (j = vd->e[i].next_regno;
1200 j != INVALID_REGNUM;
1201 j = vd->e[j].next_regno)
1203 if (TEST_HARD_REG_BIT (set, j))
1204 internal_error ("validate_value_data: Loop in regno chain (%u)",
1206 if (vd->e[j].oldest_regno != i)
1207 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1208 j, vd->e[j].oldest_regno);
1210 SET_HARD_REG_BIT (set, j);
1214 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1215 if (! TEST_HARD_REG_BIT (set, i)
1216 && (vd->e[i].mode != VOIDmode
1217 || vd->e[i].oldest_regno != i
1218 || vd->e[i].next_regno != INVALID_REGNUM))
1219 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1220 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1221 vd->e[i].next_regno);
1223 #endif
1225 static bool
1226 gate_handle_cprop (void)
1228 return (optimize > 0 && (flag_cprop_registers));
1232 struct rtl_opt_pass pass_cprop_hardreg =
1235 RTL_PASS,
1236 "cprop_hardreg", /* name */
1237 gate_handle_cprop, /* gate */
1238 copyprop_hardreg_forward, /* execute */
1239 NULL, /* sub */
1240 NULL, /* next */
1241 0, /* static_pass_number */
1242 TV_CPROP_REGISTERS, /* tv_id */
1243 0, /* properties_required */
1244 0, /* properties_provided */
1245 0, /* properties_destroyed */
1246 0, /* todo_flags_start */
1247 TODO_df_finish
1248 | TODO_verify_rtl_sharing /* todo_flags_finish */