* testsuite/experimental/random/randint.cc: Add dg-add-options tls.
[official-gcc.git] / gcc / regcprop.c
blobf0723a13e9d397a7bf8e9ebccc4a56a1ab45d706
1 /* Copy propagation on hard registers for the GNU compiler.
2 Copyright (C) 2000-2015 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 "backend.h"
24 #include "rtl.h"
25 #include "df.h"
26 #include "tm_p.h"
27 #include "insn-config.h"
28 #include "regs.h"
29 #include "emit-rtl.h"
30 #include "recog.h"
31 #include "diagnostic-core.h"
32 #include "addresses.h"
33 #include "tree-pass.h"
34 #include "rtl-iter.h"
36 /* The following code does forward propagation of hard register copies.
37 The object is to eliminate as many dependencies as possible, so that
38 we have the most scheduling freedom. As a side effect, we also clean
39 up some silly register allocation decisions made by reload. This
40 code may be obsoleted by a new register allocator. */
42 /* DEBUG_INSNs aren't changed right away, as doing so might extend the
43 lifetime of a register and get the DEBUG_INSN subsequently reset.
44 So they are queued instead, and updated only when the register is
45 used in some subsequent real insn before it is set. */
46 struct queued_debug_insn_change
48 struct queued_debug_insn_change *next;
49 rtx_insn *insn;
50 rtx *loc;
51 rtx new_rtx;
54 /* For each register, we have a list of registers that contain the same
55 value. The OLDEST_REGNO field points to the head of the list, and
56 the NEXT_REGNO field runs through the list. The MODE field indicates
57 what mode the data is known to be in; this field is VOIDmode when the
58 register is not known to contain valid data. */
60 struct value_data_entry
62 machine_mode mode;
63 unsigned int oldest_regno;
64 unsigned int next_regno;
65 struct queued_debug_insn_change *debug_insn_changes;
68 struct value_data
70 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
71 unsigned int max_value_regs;
72 unsigned int n_debug_insn_changes;
75 static object_allocator<queued_debug_insn_change> queued_debug_insn_change_pool
76 ("debug insn changes pool");
78 static bool skip_debug_insn_p;
80 static void kill_value_one_regno (unsigned, struct value_data *);
81 static void kill_value_regno (unsigned, unsigned, struct value_data *);
82 static void kill_value (const_rtx, struct value_data *);
83 static void set_value_regno (unsigned, machine_mode, struct value_data *);
84 static void init_value_data (struct value_data *);
85 static void kill_clobbered_value (rtx, const_rtx, void *);
86 static void kill_set_value (rtx, const_rtx, void *);
87 static void copy_value (rtx, rtx, struct value_data *);
88 static bool mode_change_ok (machine_mode, machine_mode,
89 unsigned int);
90 static rtx maybe_mode_change (machine_mode, machine_mode,
91 machine_mode, unsigned int, unsigned int);
92 static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
93 static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx_insn *,
94 struct value_data *);
95 static bool replace_oldest_value_addr (rtx *, enum reg_class,
96 machine_mode, addr_space_t,
97 rtx_insn *, struct value_data *);
98 static bool replace_oldest_value_mem (rtx, rtx_insn *, struct value_data *);
99 static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
100 extern void debug_value_data (struct value_data *);
101 static void validate_value_data (struct value_data *);
103 /* Free all queued updates for DEBUG_INSNs that change some reg to
104 register REGNO. */
106 static void
107 free_debug_insn_changes (struct value_data *vd, unsigned int regno)
109 struct queued_debug_insn_change *cur, *next;
110 for (cur = vd->e[regno].debug_insn_changes; cur; cur = next)
112 next = cur->next;
113 --vd->n_debug_insn_changes;
114 queued_debug_insn_change_pool.remove (cur);
116 vd->e[regno].debug_insn_changes = NULL;
119 /* Kill register REGNO. This involves removing it from any value
120 lists, and resetting the value mode to VOIDmode. This is only a
121 helper function; it does not handle any hard registers overlapping
122 with REGNO. */
124 static void
125 kill_value_one_regno (unsigned int regno, struct value_data *vd)
127 unsigned int i, next;
129 if (vd->e[regno].oldest_regno != regno)
131 for (i = vd->e[regno].oldest_regno;
132 vd->e[i].next_regno != regno;
133 i = vd->e[i].next_regno)
134 continue;
135 vd->e[i].next_regno = vd->e[regno].next_regno;
137 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
139 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
140 vd->e[i].oldest_regno = next;
143 vd->e[regno].mode = VOIDmode;
144 vd->e[regno].oldest_regno = regno;
145 vd->e[regno].next_regno = INVALID_REGNUM;
146 if (vd->e[regno].debug_insn_changes)
147 free_debug_insn_changes (vd, regno);
149 if (flag_checking)
150 validate_value_data (vd);
153 /* Kill the value in register REGNO for NREGS, and any other registers
154 whose values overlap. */
156 static void
157 kill_value_regno (unsigned int regno, unsigned int nregs,
158 struct value_data *vd)
160 unsigned int j;
162 /* Kill the value we're told to kill. */
163 for (j = 0; j < nregs; ++j)
164 kill_value_one_regno (regno + j, vd);
166 /* Kill everything that overlapped what we're told to kill. */
167 if (regno < vd->max_value_regs)
168 j = 0;
169 else
170 j = regno - vd->max_value_regs;
171 for (; j < regno; ++j)
173 unsigned int i, n;
174 if (vd->e[j].mode == VOIDmode)
175 continue;
176 n = hard_regno_nregs[j][vd->e[j].mode];
177 if (j + n > regno)
178 for (i = 0; i < n; ++i)
179 kill_value_one_regno (j + i, vd);
183 /* Kill X. This is a convenience function wrapping kill_value_regno
184 so that we mind the mode the register is in. */
186 static void
187 kill_value (const_rtx x, struct value_data *vd)
189 if (GET_CODE (x) == SUBREG)
191 rtx tmp = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
192 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
193 x = tmp ? tmp : SUBREG_REG (x);
195 if (REG_P (x))
196 kill_value_regno (REGNO (x), REG_NREGS (x), vd);
199 /* Remember that REGNO is valid in MODE. */
201 static void
202 set_value_regno (unsigned int regno, machine_mode mode,
203 struct value_data *vd)
205 unsigned int nregs;
207 vd->e[regno].mode = mode;
209 nregs = hard_regno_nregs[regno][mode];
210 if (nregs > vd->max_value_regs)
211 vd->max_value_regs = nregs;
214 /* Initialize VD such that there are no known relationships between regs. */
216 static void
217 init_value_data (struct value_data *vd)
219 int i;
220 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
222 vd->e[i].mode = VOIDmode;
223 vd->e[i].oldest_regno = i;
224 vd->e[i].next_regno = INVALID_REGNUM;
225 vd->e[i].debug_insn_changes = NULL;
227 vd->max_value_regs = 0;
228 vd->n_debug_insn_changes = 0;
231 /* Called through note_stores. If X is clobbered, kill its value. */
233 static void
234 kill_clobbered_value (rtx x, const_rtx set, void *data)
236 struct value_data *const vd = (struct value_data *) data;
237 if (GET_CODE (set) == CLOBBER)
238 kill_value (x, vd);
241 /* A structure passed as data to kill_set_value through note_stores. */
242 struct kill_set_value_data
244 struct value_data *vd;
245 rtx ignore_set_reg;
248 /* Called through note_stores. If X is set, not clobbered, kill its
249 current value and install it as the root of its own value list. */
251 static void
252 kill_set_value (rtx x, const_rtx set, void *data)
254 struct kill_set_value_data *ksvd = (struct kill_set_value_data *) data;
255 if (rtx_equal_p (x, ksvd->ignore_set_reg))
256 return;
257 if (GET_CODE (set) != CLOBBER)
259 kill_value (x, ksvd->vd);
260 if (REG_P (x))
261 set_value_regno (REGNO (x), GET_MODE (x), ksvd->vd);
265 /* Kill any register used in X as the base of an auto-increment expression,
266 and install that register as the root of its own value list. */
268 static void
269 kill_autoinc_value (rtx_insn *insn, struct value_data *vd)
271 subrtx_iterator::array_type array;
272 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
274 const_rtx x = *iter;
275 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
277 x = XEXP (x, 0);
278 kill_value (x, vd);
279 set_value_regno (REGNO (x), GET_MODE (x), vd);
280 iter.skip_subrtxes ();
285 /* Assert that SRC has been copied to DEST. Adjust the data structures
286 to reflect that SRC contains an older copy of the shared value. */
288 static void
289 copy_value (rtx dest, rtx src, struct value_data *vd)
291 unsigned int dr = REGNO (dest);
292 unsigned int sr = REGNO (src);
293 unsigned int dn, sn;
294 unsigned int i;
296 /* ??? At present, it's possible to see noop sets. It'd be nice if
297 this were cleaned up beforehand... */
298 if (sr == dr)
299 return;
301 /* Do not propagate copies to the stack pointer, as that can leave
302 memory accesses with no scheduling dependency on the stack update. */
303 if (dr == STACK_POINTER_REGNUM)
304 return;
306 /* Likewise with the frame pointer, if we're using one. */
307 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
308 return;
310 /* Do not propagate copies to fixed or global registers, patterns
311 can be relying to see particular fixed register or users can
312 expect the chosen global register in asm. */
313 if (fixed_regs[dr] || global_regs[dr])
314 return;
316 /* If SRC and DEST overlap, don't record anything. */
317 dn = REG_NREGS (dest);
318 sn = REG_NREGS (src);
319 if ((dr > sr && dr < sr + sn)
320 || (sr > dr && sr < dr + dn))
321 return;
323 /* If SRC had no assigned mode (i.e. we didn't know it was live)
324 assign it now and assume the value came from an input argument
325 or somesuch. */
326 if (vd->e[sr].mode == VOIDmode)
327 set_value_regno (sr, vd->e[dr].mode, vd);
329 /* If we are narrowing the input to a smaller number of hard regs,
330 and it is in big endian, we are really extracting a high part.
331 Since we generally associate a low part of a value with the value itself,
332 we must not do the same for the high part.
333 Note we can still get low parts for the same mode combination through
334 a two-step copy involving differently sized hard regs.
335 Assume hard regs fr* are 32 bits each, while r* are 64 bits each:
336 (set (reg:DI r0) (reg:DI fr0))
337 (set (reg:SI fr2) (reg:SI r0))
338 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
339 (set (reg:SI fr2) (reg:SI fr0))
340 loads the high part of (reg:DI fr0) into fr2.
342 We can't properly represent the latter case in our tables, so don't
343 record anything then. */
344 else if (sn < (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode]
345 && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
346 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
347 return;
349 /* If SRC had been assigned a mode narrower than the copy, we can't
350 link DEST into the chain, because not all of the pieces of the
351 copy came from oldest_regno. */
352 else if (sn > (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode])
353 return;
355 /* Link DR at the end of the value chain used by SR. */
357 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
359 for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
360 continue;
361 vd->e[i].next_regno = dr;
363 if (flag_checking)
364 validate_value_data (vd);
367 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
369 static bool
370 mode_change_ok (machine_mode orig_mode, machine_mode new_mode,
371 unsigned int regno ATTRIBUTE_UNUSED)
373 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
374 return false;
376 #ifdef CANNOT_CHANGE_MODE_CLASS
377 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
378 #endif
380 return true;
383 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
384 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
385 in NEW_MODE.
386 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
388 static rtx
389 maybe_mode_change (machine_mode orig_mode, machine_mode copy_mode,
390 machine_mode new_mode, unsigned int regno,
391 unsigned int copy_regno ATTRIBUTE_UNUSED)
393 if (GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (orig_mode)
394 && GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (new_mode))
395 return NULL_RTX;
397 if (orig_mode == new_mode)
398 return gen_raw_REG (new_mode, regno);
399 else if (mode_change_ok (orig_mode, new_mode, regno))
401 int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
402 int use_nregs = hard_regno_nregs[copy_regno][new_mode];
403 int copy_offset
404 = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
405 int offset
406 = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
407 int byteoffset = offset % UNITS_PER_WORD;
408 int wordoffset = offset - byteoffset;
410 offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
411 + (BYTES_BIG_ENDIAN ? byteoffset : 0));
412 regno += subreg_regno_offset (regno, orig_mode, offset, new_mode);
413 if (HARD_REGNO_MODE_OK (regno, new_mode))
414 return gen_raw_REG (new_mode, regno);
416 return NULL_RTX;
419 /* Find the oldest copy of the value contained in REGNO that is in
420 register class CL and has mode MODE. If found, return an rtx
421 of that oldest register, otherwise return NULL. */
423 static rtx
424 find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
426 unsigned int regno = REGNO (reg);
427 machine_mode mode = GET_MODE (reg);
428 unsigned int i;
430 /* If we are accessing REG in some mode other that what we set it in,
431 make sure that the replacement is valid. In particular, consider
432 (set (reg:DI r11) (...))
433 (set (reg:SI r9) (reg:SI r11))
434 (set (reg:SI r10) (...))
435 (set (...) (reg:DI r9))
436 Replacing r9 with r11 is invalid. */
437 if (mode != vd->e[regno].mode)
439 if (hard_regno_nregs[regno][mode]
440 > hard_regno_nregs[regno][vd->e[regno].mode])
441 return NULL_RTX;
444 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
446 machine_mode oldmode = vd->e[i].mode;
447 rtx new_rtx;
449 if (!in_hard_reg_set_p (reg_class_contents[cl], mode, i))
450 continue;
452 new_rtx = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
453 if (new_rtx)
455 ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (reg);
456 REG_ATTRS (new_rtx) = REG_ATTRS (reg);
457 REG_POINTER (new_rtx) = REG_POINTER (reg);
458 return new_rtx;
462 return NULL_RTX;
465 /* If possible, replace the register at *LOC with the oldest register
466 in register class CL. Return true if successfully replaced. */
468 static bool
469 replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx_insn *insn,
470 struct value_data *vd)
472 rtx new_rtx = find_oldest_value_reg (cl, *loc, vd);
473 if (new_rtx && (!DEBUG_INSN_P (insn) || !skip_debug_insn_p))
475 if (DEBUG_INSN_P (insn))
477 struct queued_debug_insn_change *change;
479 if (dump_file)
480 fprintf (dump_file, "debug_insn %u: queued replacing reg %u with %u\n",
481 INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
483 change = queued_debug_insn_change_pool.allocate ();
484 change->next = vd->e[REGNO (new_rtx)].debug_insn_changes;
485 change->insn = insn;
486 change->loc = loc;
487 change->new_rtx = new_rtx;
488 vd->e[REGNO (new_rtx)].debug_insn_changes = change;
489 ++vd->n_debug_insn_changes;
490 return true;
492 if (dump_file)
493 fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
494 INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
496 validate_change (insn, loc, new_rtx, 1);
497 return true;
499 return false;
502 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
503 Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
504 BASE_REG_CLASS depending on how the register is being considered. */
506 static bool
507 replace_oldest_value_addr (rtx *loc, enum reg_class cl,
508 machine_mode mode, addr_space_t as,
509 rtx_insn *insn, struct value_data *vd)
511 rtx x = *loc;
512 RTX_CODE code = GET_CODE (x);
513 const char *fmt;
514 int i, j;
515 bool changed = false;
517 switch (code)
519 case PLUS:
520 if (DEBUG_INSN_P (insn))
521 break;
524 rtx orig_op0 = XEXP (x, 0);
525 rtx orig_op1 = XEXP (x, 1);
526 RTX_CODE code0 = GET_CODE (orig_op0);
527 RTX_CODE code1 = GET_CODE (orig_op1);
528 rtx op0 = orig_op0;
529 rtx op1 = orig_op1;
530 rtx *locI = NULL;
531 rtx *locB = NULL;
532 enum rtx_code index_code = SCRATCH;
534 if (GET_CODE (op0) == SUBREG)
536 op0 = SUBREG_REG (op0);
537 code0 = GET_CODE (op0);
540 if (GET_CODE (op1) == SUBREG)
542 op1 = SUBREG_REG (op1);
543 code1 = GET_CODE (op1);
546 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
547 || code0 == ZERO_EXTEND || code1 == MEM)
549 locI = &XEXP (x, 0);
550 locB = &XEXP (x, 1);
551 index_code = GET_CODE (*locI);
553 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
554 || code1 == ZERO_EXTEND || code0 == MEM)
556 locI = &XEXP (x, 1);
557 locB = &XEXP (x, 0);
558 index_code = GET_CODE (*locI);
560 else if (code0 == CONST_INT || code0 == CONST
561 || code0 == SYMBOL_REF || code0 == LABEL_REF)
563 locB = &XEXP (x, 1);
564 index_code = GET_CODE (XEXP (x, 0));
566 else if (code1 == CONST_INT || code1 == CONST
567 || code1 == SYMBOL_REF || code1 == LABEL_REF)
569 locB = &XEXP (x, 0);
570 index_code = GET_CODE (XEXP (x, 1));
572 else if (code0 == REG && code1 == REG)
574 int index_op;
575 unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
577 if (REGNO_OK_FOR_INDEX_P (regno1)
578 && regno_ok_for_base_p (regno0, mode, as, PLUS, REG))
579 index_op = 1;
580 else if (REGNO_OK_FOR_INDEX_P (regno0)
581 && regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
582 index_op = 0;
583 else if (regno_ok_for_base_p (regno0, mode, as, PLUS, REG)
584 || REGNO_OK_FOR_INDEX_P (regno1))
585 index_op = 1;
586 else if (regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
587 index_op = 0;
588 else
589 index_op = 1;
591 locI = &XEXP (x, index_op);
592 locB = &XEXP (x, !index_op);
593 index_code = GET_CODE (*locI);
595 else if (code0 == REG)
597 locI = &XEXP (x, 0);
598 locB = &XEXP (x, 1);
599 index_code = GET_CODE (*locI);
601 else if (code1 == REG)
603 locI = &XEXP (x, 1);
604 locB = &XEXP (x, 0);
605 index_code = GET_CODE (*locI);
608 if (locI)
609 changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS,
610 mode, as, insn, vd);
611 if (locB)
612 changed |= replace_oldest_value_addr (locB,
613 base_reg_class (mode, as, PLUS,
614 index_code),
615 mode, as, insn, vd);
616 return changed;
619 case POST_INC:
620 case POST_DEC:
621 case POST_MODIFY:
622 case PRE_INC:
623 case PRE_DEC:
624 case PRE_MODIFY:
625 return false;
627 case MEM:
628 return replace_oldest_value_mem (x, insn, vd);
630 case REG:
631 return replace_oldest_value_reg (loc, cl, insn, vd);
633 default:
634 break;
637 fmt = GET_RTX_FORMAT (code);
638 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
640 if (fmt[i] == 'e')
641 changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode, as,
642 insn, vd);
643 else if (fmt[i] == 'E')
644 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
645 changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
646 mode, as, insn, vd);
649 return changed;
652 /* Similar to replace_oldest_value_reg, but X contains a memory. */
654 static bool
655 replace_oldest_value_mem (rtx x, rtx_insn *insn, struct value_data *vd)
657 enum reg_class cl;
659 if (DEBUG_INSN_P (insn))
660 cl = ALL_REGS;
661 else
662 cl = base_reg_class (GET_MODE (x), MEM_ADDR_SPACE (x), MEM, SCRATCH);
664 return replace_oldest_value_addr (&XEXP (x, 0), cl,
665 GET_MODE (x), MEM_ADDR_SPACE (x),
666 insn, vd);
669 /* Apply all queued updates for DEBUG_INSNs that change some reg to
670 register REGNO. */
672 static void
673 apply_debug_insn_changes (struct value_data *vd, unsigned int regno)
675 struct queued_debug_insn_change *change;
676 rtx_insn *last_insn = vd->e[regno].debug_insn_changes->insn;
678 for (change = vd->e[regno].debug_insn_changes;
679 change;
680 change = change->next)
682 if (last_insn != change->insn)
684 apply_change_group ();
685 last_insn = change->insn;
687 validate_change (change->insn, change->loc, change->new_rtx, 1);
689 apply_change_group ();
692 /* Called via note_uses, for all used registers in a real insn
693 apply DEBUG_INSN changes that change registers to the used
694 registers. */
696 static void
697 cprop_find_used_regs (rtx *loc, void *data)
699 struct value_data *const vd = (struct value_data *) data;
700 subrtx_iterator::array_type array;
701 FOR_EACH_SUBRTX (iter, array, *loc, NONCONST)
703 const_rtx x = *iter;
704 if (REG_P (x))
706 unsigned int regno = REGNO (x);
707 if (vd->e[regno].debug_insn_changes)
709 apply_debug_insn_changes (vd, regno);
710 free_debug_insn_changes (vd, regno);
716 /* Apply clobbers of INSN in PATTERN and C_I_F_U to value_data VD. */
718 static void
719 kill_clobbered_values (rtx_insn *insn, struct value_data *vd)
721 note_stores (PATTERN (insn), kill_clobbered_value, vd);
723 if (CALL_P (insn))
725 rtx exp;
727 for (exp = CALL_INSN_FUNCTION_USAGE (insn); exp; exp = XEXP (exp, 1))
729 rtx x = XEXP (exp, 0);
730 if (GET_CODE (x) == CLOBBER)
731 kill_value (SET_DEST (x), vd);
736 /* Perform the forward copy propagation on basic block BB. */
738 static bool
739 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
741 bool anything_changed = false;
742 rtx_insn *insn;
744 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
746 int n_ops, i, predicated;
747 bool is_asm, any_replacements;
748 rtx set;
749 rtx link;
750 bool replaced[MAX_RECOG_OPERANDS];
751 bool changed = false;
752 struct kill_set_value_data ksvd;
754 if (!NONDEBUG_INSN_P (insn))
756 if (DEBUG_INSN_P (insn))
758 rtx loc = INSN_VAR_LOCATION_LOC (insn);
759 if (!VAR_LOC_UNKNOWN_P (loc))
760 replace_oldest_value_addr (&INSN_VAR_LOCATION_LOC (insn),
761 ALL_REGS, GET_MODE (loc),
762 ADDR_SPACE_GENERIC, insn, vd);
765 if (insn == BB_END (bb))
766 break;
767 else
768 continue;
771 set = single_set (insn);
772 extract_constrain_insn (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 kill_clobbered_values (insn, 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
814 would clobbers. */
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. */
824 if (set
825 && reg_overlap_mentioned_p (XEXP (link, 0), SET_SRC (set)))
826 set = NULL;
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 machine_mode mode = GET_MODE (src);
837 unsigned int i;
838 rtx new_rtx;
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
849 is also invalid. */
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))
864 if (dump_file)
865 fprintf (dump_file,
866 "insn %u: replaced reg %u with %u\n",
867 INSN_UID (insn), regno, REGNO (new_rtx));
868 changed = true;
869 goto did_replacement;
871 /* We need to re-extract as validate_change clobbers
872 recog_data. */
873 extract_constrain_insn (insn);
874 preprocess_constraints (insn);
877 /* Otherwise, try all valid registers and see if its valid. */
878 for (i = vd->e[regno].oldest_regno; i != regno;
879 i = vd->e[i].next_regno)
881 new_rtx = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
882 mode, i, regno);
883 if (new_rtx != NULL_RTX)
885 if (validate_change (insn, &SET_SRC (set), new_rtx, 0))
887 ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (src);
888 REG_ATTRS (new_rtx) = REG_ATTRS (src);
889 REG_POINTER (new_rtx) = REG_POINTER (src);
890 if (dump_file)
891 fprintf (dump_file,
892 "insn %u: replaced reg %u with %u\n",
893 INSN_UID (insn), regno, REGNO (new_rtx));
894 changed = true;
895 goto did_replacement;
897 /* We need to re-extract as validate_change clobbers
898 recog_data. */
899 extract_constrain_insn (insn);
900 preprocess_constraints (insn);
904 no_move_special_case:
906 any_replacements = false;
908 /* For each input operand, replace a hard register with the
909 eldest live copy that's in an appropriate register class. */
910 for (i = 0; i < n_ops; i++)
912 replaced[i] = false;
914 /* Don't scan match_operand here, since we've no reg class
915 information to pass down. Any operands that we could
916 substitute in will be represented elsewhere. */
917 if (recog_data.constraints[i][0] == '\0')
918 continue;
920 /* Don't replace in asms intentionally referencing hard regs. */
921 if (is_asm && REG_P (recog_data.operand[i])
922 && (REGNO (recog_data.operand[i])
923 == ORIGINAL_REGNO (recog_data.operand[i])))
924 continue;
926 if (recog_data.operand_type[i] == OP_IN)
928 if (op_alt[i].is_address)
929 replaced[i]
930 = replace_oldest_value_addr (recog_data.operand_loc[i],
931 alternative_class (op_alt, i),
932 VOIDmode, ADDR_SPACE_GENERIC,
933 insn, vd);
934 else if (REG_P (recog_data.operand[i]))
935 replaced[i]
936 = replace_oldest_value_reg (recog_data.operand_loc[i],
937 alternative_class (op_alt, i),
938 insn, vd);
939 else if (MEM_P (recog_data.operand[i]))
940 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
941 insn, vd);
943 else if (MEM_P (recog_data.operand[i]))
944 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
945 insn, vd);
947 /* If we performed any replacement, update match_dups. */
948 if (replaced[i])
950 int j;
951 rtx new_rtx;
953 new_rtx = *recog_data.operand_loc[i];
954 recog_data.operand[i] = new_rtx;
955 for (j = 0; j < recog_data.n_dups; j++)
956 if (recog_data.dup_num[j] == i)
957 validate_unshare_change (insn, recog_data.dup_loc[j], new_rtx, 1);
959 any_replacements = true;
963 if (any_replacements)
965 if (! apply_change_group ())
967 for (i = 0; i < n_ops; i++)
968 if (replaced[i])
970 rtx old = *recog_data.operand_loc[i];
971 recog_data.operand[i] = old;
974 if (dump_file)
975 fprintf (dump_file,
976 "insn %u: reg replacements not verified\n",
977 INSN_UID (insn));
979 else
980 changed = true;
983 did_replacement:
984 if (changed)
986 anything_changed = true;
988 /* If something changed, perhaps further changes to earlier
989 DEBUG_INSNs can be applied. */
990 if (vd->n_debug_insn_changes)
991 note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
994 ksvd.vd = vd;
995 ksvd.ignore_set_reg = NULL_RTX;
997 /* Clobber call-clobbered registers. */
998 if (CALL_P (insn))
1000 unsigned int set_regno = INVALID_REGNUM;
1001 unsigned int set_nregs = 0;
1002 unsigned int regno;
1003 rtx exp;
1004 HARD_REG_SET regs_invalidated_by_this_call;
1006 for (exp = CALL_INSN_FUNCTION_USAGE (insn); exp; exp = XEXP (exp, 1))
1008 rtx x = XEXP (exp, 0);
1009 if (GET_CODE (x) == SET)
1011 rtx dest = SET_DEST (x);
1012 kill_value (dest, vd);
1013 set_value_regno (REGNO (dest), GET_MODE (dest), vd);
1014 copy_value (dest, SET_SRC (x), vd);
1015 ksvd.ignore_set_reg = dest;
1016 set_regno = REGNO (dest);
1017 set_nregs = REG_NREGS (dest);
1018 break;
1022 get_call_reg_set_usage (insn,
1023 &regs_invalidated_by_this_call,
1024 regs_invalidated_by_call);
1025 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1026 if ((TEST_HARD_REG_BIT (regs_invalidated_by_this_call, regno)
1027 || HARD_REGNO_CALL_PART_CLOBBERED (regno, vd->e[regno].mode))
1028 && (regno < set_regno || regno >= set_regno + set_nregs))
1029 kill_value_regno (regno, 1, vd);
1031 /* If SET was seen in CALL_INSN_FUNCTION_USAGE, and SET_SRC
1032 of the SET isn't in regs_invalidated_by_call hard reg set,
1033 but instead among CLOBBERs on the CALL_INSN, we could wrongly
1034 assume the value in it is still live. */
1035 if (ksvd.ignore_set_reg)
1036 kill_clobbered_values (insn, vd);
1039 bool copy_p = (set
1040 && REG_P (SET_DEST (set))
1041 && REG_P (SET_SRC (set)));
1042 bool noop_p = (copy_p
1043 && rtx_equal_p (SET_DEST (set), SET_SRC (set)));
1045 if (!noop_p)
1047 /* Notice stores. */
1048 note_stores (PATTERN (insn), kill_set_value, &ksvd);
1050 /* Notice copies. */
1051 if (copy_p)
1052 copy_value (SET_DEST (set), SET_SRC (set), vd);
1055 if (insn == BB_END (bb))
1056 break;
1059 return anything_changed;
1062 /* Dump the value chain data to stderr. */
1064 DEBUG_FUNCTION void
1065 debug_value_data (struct value_data *vd)
1067 HARD_REG_SET set;
1068 unsigned int i, j;
1070 CLEAR_HARD_REG_SET (set);
1072 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1073 if (vd->e[i].oldest_regno == i)
1075 if (vd->e[i].mode == VOIDmode)
1077 if (vd->e[i].next_regno != INVALID_REGNUM)
1078 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1079 i, vd->e[i].next_regno);
1080 continue;
1083 SET_HARD_REG_BIT (set, i);
1084 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1086 for (j = vd->e[i].next_regno;
1087 j != INVALID_REGNUM;
1088 j = vd->e[j].next_regno)
1090 if (TEST_HARD_REG_BIT (set, j))
1092 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1093 return;
1096 if (vd->e[j].oldest_regno != i)
1098 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1099 j, vd->e[j].oldest_regno);
1100 return;
1102 SET_HARD_REG_BIT (set, j);
1103 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1105 fputc ('\n', stderr);
1108 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1109 if (! TEST_HARD_REG_BIT (set, i)
1110 && (vd->e[i].mode != VOIDmode
1111 || vd->e[i].oldest_regno != i
1112 || vd->e[i].next_regno != INVALID_REGNUM))
1113 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1114 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1115 vd->e[i].next_regno);
1118 /* Do copyprop_hardreg_forward_1 for a single basic block BB.
1119 DEBUG_INSN is skipped since we do not want to involve DF related
1120 staff as how it is handled in function pass_cprop_hardreg::execute.
1122 NOTE: Currently it is only used for shrink-wrap. Maybe extend it
1123 to handle DEBUG_INSN for other uses. */
1125 void
1126 copyprop_hardreg_forward_bb_without_debug_insn (basic_block bb)
1128 struct value_data *vd;
1129 vd = XNEWVEC (struct value_data, 1);
1130 init_value_data (vd);
1132 skip_debug_insn_p = true;
1133 copyprop_hardreg_forward_1 (bb, vd);
1134 free (vd);
1135 skip_debug_insn_p = false;
1138 static void
1139 validate_value_data (struct value_data *vd)
1141 HARD_REG_SET set;
1142 unsigned int i, j;
1144 CLEAR_HARD_REG_SET (set);
1146 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1147 if (vd->e[i].oldest_regno == i)
1149 if (vd->e[i].mode == VOIDmode)
1151 if (vd->e[i].next_regno != INVALID_REGNUM)
1152 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1153 i, vd->e[i].next_regno);
1154 continue;
1157 SET_HARD_REG_BIT (set, i);
1159 for (j = vd->e[i].next_regno;
1160 j != INVALID_REGNUM;
1161 j = vd->e[j].next_regno)
1163 if (TEST_HARD_REG_BIT (set, j))
1164 internal_error ("validate_value_data: Loop in regno chain (%u)",
1166 if (vd->e[j].oldest_regno != i)
1167 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1168 j, vd->e[j].oldest_regno);
1170 SET_HARD_REG_BIT (set, j);
1174 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1175 if (! TEST_HARD_REG_BIT (set, i)
1176 && (vd->e[i].mode != VOIDmode
1177 || vd->e[i].oldest_regno != i
1178 || vd->e[i].next_regno != INVALID_REGNUM))
1179 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1180 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1181 vd->e[i].next_regno);
1185 namespace {
1187 const pass_data pass_data_cprop_hardreg =
1189 RTL_PASS, /* type */
1190 "cprop_hardreg", /* name */
1191 OPTGROUP_NONE, /* optinfo_flags */
1192 TV_CPROP_REGISTERS, /* tv_id */
1193 0, /* properties_required */
1194 0, /* properties_provided */
1195 0, /* properties_destroyed */
1196 0, /* todo_flags_start */
1197 TODO_df_finish, /* todo_flags_finish */
1200 class pass_cprop_hardreg : public rtl_opt_pass
1202 public:
1203 pass_cprop_hardreg (gcc::context *ctxt)
1204 : rtl_opt_pass (pass_data_cprop_hardreg, ctxt)
1207 /* opt_pass methods: */
1208 virtual bool gate (function *)
1210 return (optimize > 0 && (flag_cprop_registers));
1213 virtual unsigned int execute (function *);
1215 }; // class pass_cprop_hardreg
1217 unsigned int
1218 pass_cprop_hardreg::execute (function *fun)
1220 struct value_data *all_vd;
1221 basic_block bb;
1222 sbitmap visited;
1223 bool analyze_called = false;
1225 all_vd = XNEWVEC (struct value_data, last_basic_block_for_fn (fun));
1227 visited = sbitmap_alloc (last_basic_block_for_fn (fun));
1228 bitmap_clear (visited);
1230 FOR_EACH_BB_FN (bb, fun)
1232 bitmap_set_bit (visited, bb->index);
1234 /* If a block has a single predecessor, that we've already
1235 processed, begin with the value data that was live at
1236 the end of the predecessor block. */
1237 /* ??? Ought to use more intelligent queuing of blocks. */
1238 if (single_pred_p (bb)
1239 && bitmap_bit_p (visited, single_pred (bb)->index)
1240 && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
1242 all_vd[bb->index] = all_vd[single_pred (bb)->index];
1243 if (all_vd[bb->index].n_debug_insn_changes)
1245 unsigned int regno;
1247 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1249 if (all_vd[bb->index].e[regno].debug_insn_changes)
1251 all_vd[bb->index].e[regno].debug_insn_changes = NULL;
1252 if (--all_vd[bb->index].n_debug_insn_changes == 0)
1253 break;
1258 else
1259 init_value_data (all_vd + bb->index);
1261 copyprop_hardreg_forward_1 (bb, all_vd + bb->index);
1264 if (MAY_HAVE_DEBUG_INSNS)
1266 FOR_EACH_BB_FN (bb, fun)
1267 if (bitmap_bit_p (visited, bb->index)
1268 && all_vd[bb->index].n_debug_insn_changes)
1270 unsigned int regno;
1271 bitmap live;
1273 if (!analyze_called)
1275 df_analyze ();
1276 analyze_called = true;
1278 live = df_get_live_out (bb);
1279 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1280 if (all_vd[bb->index].e[regno].debug_insn_changes)
1282 if (REGNO_REG_SET_P (live, regno))
1283 apply_debug_insn_changes (all_vd + bb->index, regno);
1284 if (all_vd[bb->index].n_debug_insn_changes == 0)
1285 break;
1289 queued_debug_insn_change_pool.release ();
1292 sbitmap_free (visited);
1293 free (all_vd);
1294 return 0;
1297 } // anon namespace
1299 rtl_opt_pass *
1300 make_pass_cprop_hardreg (gcc::context *ctxt)
1302 return new pass_cprop_hardreg (ctxt);