regcprop.c: Include rtl-iter.h.
[gcc.git] / gcc / regcprop.c
1 /* Copy propagation on hard registers for the GNU compiler.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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"
40
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. */
46
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
52 {
53 struct queued_debug_insn_change *next;
54 rtx_insn *insn;
55 rtx *loc;
56 rtx new_rtx;
57 };
58
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. */
64
65 struct value_data_entry
66 {
67 enum machine_mode mode;
68 unsigned int oldest_regno;
69 unsigned int next_regno;
70 struct queued_debug_insn_change *debug_insn_changes;
71 };
72
73 struct value_data
74 {
75 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
76 unsigned int max_value_regs;
77 unsigned int n_debug_insn_changes;
78 };
79
80 static alloc_pool debug_insn_changes_pool;
81 static bool skip_debug_insn_p;
82
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
107
108 /* Free all queued updates for DEBUG_INSNs that change some reg to
109 register REGNO. */
110
111 static void
112 free_debug_insn_changes (struct value_data *vd, unsigned int regno)
113 {
114 struct queued_debug_insn_change *cur, *next;
115 for (cur = vd->e[regno].debug_insn_changes; cur; cur = next)
116 {
117 next = cur->next;
118 --vd->n_debug_insn_changes;
119 pool_free (debug_insn_changes_pool, cur);
120 }
121 vd->e[regno].debug_insn_changes = NULL;
122 }
123
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. */
128
129 static void
130 kill_value_one_regno (unsigned int regno, struct value_data *vd)
131 {
132 unsigned int i, next;
133
134 if (vd->e[regno].oldest_regno != regno)
135 {
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;
141 }
142 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
143 {
144 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
145 vd->e[i].oldest_regno = next;
146 }
147
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);
153
154 #ifdef ENABLE_CHECKING
155 validate_value_data (vd);
156 #endif
157 }
158
159 /* Kill the value in register REGNO for NREGS, and any other registers
160 whose values overlap. */
161
162 static void
163 kill_value_regno (unsigned int regno, unsigned int nregs,
164 struct value_data *vd)
165 {
166 unsigned int j;
167
168 /* Kill the value we're told to kill. */
169 for (j = 0; j < nregs; ++j)
170 kill_value_one_regno (regno + j, vd);
171
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)
178 {
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);
186 }
187 }
188
189 /* Kill X. This is a convenience function wrapping kill_value_regno
190 so that we mind the mode the register is in. */
191
192 static void
193 kill_value (const_rtx x, struct value_data *vd)
194 {
195 if (GET_CODE (x) == SUBREG)
196 {
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);
200 }
201 if (REG_P (x))
202 {
203 unsigned int regno = REGNO (x);
204 unsigned int n = hard_regno_nregs[regno][GET_MODE (x)];
205
206 kill_value_regno (regno, n, vd);
207 }
208 }
209
210 /* Remember that REGNO is valid in MODE. */
211
212 static void
213 set_value_regno (unsigned int regno, enum machine_mode mode,
214 struct value_data *vd)
215 {
216 unsigned int nregs;
217
218 vd->e[regno].mode = mode;
219
220 nregs = hard_regno_nregs[regno][mode];
221 if (nregs > vd->max_value_regs)
222 vd->max_value_regs = nregs;
223 }
224
225 /* Initialize VD such that there are no known relationships between regs. */
226
227 static void
228 init_value_data (struct value_data *vd)
229 {
230 int i;
231 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
232 {
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;
237 }
238 vd->max_value_regs = 0;
239 vd->n_debug_insn_changes = 0;
240 }
241
242 /* Called through note_stores. If X is clobbered, kill its value. */
243
244 static void
245 kill_clobbered_value (rtx x, const_rtx set, void *data)
246 {
247 struct value_data *const vd = (struct value_data *) data;
248 if (GET_CODE (set) == CLOBBER)
249 kill_value (x, vd);
250 }
251
252 /* A structure passed as data to kill_set_value through note_stores. */
253 struct kill_set_value_data
254 {
255 struct value_data *vd;
256 rtx ignore_set_reg;
257 };
258
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. */
261
262 static void
263 kill_set_value (rtx x, const_rtx set, void *data)
264 {
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)
269 {
270 kill_value (x, ksvd->vd);
271 if (REG_P (x))
272 set_value_regno (REGNO (x), GET_MODE (x), ksvd->vd);
273 }
274 }
275
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. */
278
279 static void
280 kill_autoinc_value (rtx insn, struct value_data *vd)
281 {
282 subrtx_iterator::array_type array;
283 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
284 {
285 const_rtx x = *iter;
286 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
287 {
288 x = XEXP (x, 0);
289 kill_value (x, vd);
290 set_value_regno (REGNO (x), GET_MODE (x), vd);
291 iter.skip_subrtxes ();
292 }
293 }
294 }
295
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. */
298
299 static void
300 copy_value (rtx dest, rtx src, struct value_data *vd)
301 {
302 unsigned int dr = REGNO (dest);
303 unsigned int sr = REGNO (src);
304 unsigned int dn, sn;
305 unsigned int i;
306
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;
311
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;
316
317 /* Likewise with the frame pointer, if we're using one. */
318 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
319 return;
320
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;
326
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;
333
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);
339
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.
352
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;
359
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;
365
366 /* Link DR at the end of the value chain used by SR. */
367
368 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
369
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;
373
374 #ifdef ENABLE_CHECKING
375 validate_value_data (vd);
376 #endif
377 }
378
379 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
380
381 static bool
382 mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
383 unsigned int regno ATTRIBUTE_UNUSED)
384 {
385 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
386 return false;
387
388 #ifdef CANNOT_CHANGE_MODE_CLASS
389 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
390 #endif
391
392 return true;
393 }
394
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. */
399
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)
404 {
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;
408
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))
412 {
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;
421
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);
427 }
428 return NULL_RTX;
429 }
430
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. */
434
435 static rtx
436 find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
437 {
438 unsigned int regno = REGNO (reg);
439 enum machine_mode mode = GET_MODE (reg);
440 unsigned int i;
441
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)
450 {
451 if (hard_regno_nregs[regno][mode]
452 > hard_regno_nregs[regno][vd->e[regno].mode])
453 return NULL_RTX;
454 }
455
456 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
457 {
458 enum machine_mode oldmode = vd->e[i].mode;
459 rtx new_rtx;
460
461 if (!in_hard_reg_set_p (reg_class_contents[cl], mode, i))
462 continue;
463
464 new_rtx = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
465 if (new_rtx)
466 {
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;
471 }
472 }
473
474 return NULL_RTX;
475 }
476
477 /* If possible, replace the register at *LOC with the oldest register
478 in register class CL. Return true if successfully replaced. */
479
480 static bool
481 replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx_insn *insn,
482 struct value_data *vd)
483 {
484 rtx new_rtx = find_oldest_value_reg (cl, *loc, vd);
485 if (new_rtx && (!DEBUG_INSN_P (insn) || !skip_debug_insn_p))
486 {
487 if (DEBUG_INSN_P (insn))
488 {
489 struct queued_debug_insn_change *change;
490
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));
494
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;
504 }
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));
508
509 validate_change (insn, loc, new_rtx, 1);
510 return true;
511 }
512 return false;
513 }
514
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. */
518
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)
523 {
524 rtx x = *loc;
525 RTX_CODE code = GET_CODE (x);
526 const char *fmt;
527 int i, j;
528 bool changed = false;
529
530 switch (code)
531 {
532 case PLUS:
533 if (DEBUG_INSN_P (insn))
534 break;
535
536 {
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;
546
547 if (GET_CODE (op0) == SUBREG)
548 {
549 op0 = SUBREG_REG (op0);
550 code0 = GET_CODE (op0);
551 }
552
553 if (GET_CODE (op1) == SUBREG)
554 {
555 op1 = SUBREG_REG (op1);
556 code1 = GET_CODE (op1);
557 }
558
559 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
560 || code0 == ZERO_EXTEND || code1 == MEM)
561 {
562 locI = &XEXP (x, 0);
563 locB = &XEXP (x, 1);
564 index_code = GET_CODE (*locI);
565 }
566 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
567 || code1 == ZERO_EXTEND || code0 == MEM)
568 {
569 locI = &XEXP (x, 1);
570 locB = &XEXP (x, 0);
571 index_code = GET_CODE (*locI);
572 }
573 else if (code0 == CONST_INT || code0 == CONST
574 || code0 == SYMBOL_REF || code0 == LABEL_REF)
575 {
576 locB = &XEXP (x, 1);
577 index_code = GET_CODE (XEXP (x, 0));
578 }
579 else if (code1 == CONST_INT || code1 == CONST
580 || code1 == SYMBOL_REF || code1 == LABEL_REF)
581 {
582 locB = &XEXP (x, 0);
583 index_code = GET_CODE (XEXP (x, 1));
584 }
585 else if (code0 == REG && code1 == REG)
586 {
587 int index_op;
588 unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
589
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;
603
604 locI = &XEXP (x, index_op);
605 locB = &XEXP (x, !index_op);
606 index_code = GET_CODE (*locI);
607 }
608 else if (code0 == REG)
609 {
610 locI = &XEXP (x, 0);
611 locB = &XEXP (x, 1);
612 index_code = GET_CODE (*locI);
613 }
614 else if (code1 == REG)
615 {
616 locI = &XEXP (x, 1);
617 locB = &XEXP (x, 0);
618 index_code = GET_CODE (*locI);
619 }
620
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;
630 }
631
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;
639
640 case MEM:
641 return replace_oldest_value_mem (x, insn, vd);
642
643 case REG:
644 return replace_oldest_value_reg (loc, cl, insn, vd);
645
646 default:
647 break;
648 }
649
650 fmt = GET_RTX_FORMAT (code);
651 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
652 {
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);
660 }
661
662 return changed;
663 }
664
665 /* Similar to replace_oldest_value_reg, but X contains a memory. */
666
667 static bool
668 replace_oldest_value_mem (rtx x, rtx_insn *insn, struct value_data *vd)
669 {
670 enum reg_class cl;
671
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);
676
677 return replace_oldest_value_addr (&XEXP (x, 0), cl,
678 GET_MODE (x), MEM_ADDR_SPACE (x),
679 insn, vd);
680 }
681
682 /* Apply all queued updates for DEBUG_INSNs that change some reg to
683 register REGNO. */
684
685 static void
686 apply_debug_insn_changes (struct value_data *vd, unsigned int regno)
687 {
688 struct queued_debug_insn_change *change;
689 rtx_insn *last_insn = vd->e[regno].debug_insn_changes->insn;
690
691 for (change = vd->e[regno].debug_insn_changes;
692 change;
693 change = change->next)
694 {
695 if (last_insn != change->insn)
696 {
697 apply_change_group ();
698 last_insn = change->insn;
699 }
700 validate_change (change->insn, change->loc, change->new_rtx, 1);
701 }
702 apply_change_group ();
703 }
704
705 /* Called via for_each_rtx, for all used registers in a real
706 insn apply DEBUG_INSN changes that change registers to the
707 used register. */
708
709 static int
710 cprop_find_used_regs_1 (rtx *loc, void *data)
711 {
712 if (REG_P (*loc))
713 {
714 struct value_data *vd = (struct value_data *) data;
715 if (vd->e[REGNO (*loc)].debug_insn_changes)
716 {
717 apply_debug_insn_changes (vd, REGNO (*loc));
718 free_debug_insn_changes (vd, REGNO (*loc));
719 }
720 }
721 return 0;
722 }
723
724 /* Called via note_uses, for all used registers in a real insn
725 apply DEBUG_INSN changes that change registers to the used
726 registers. */
727
728 static void
729 cprop_find_used_regs (rtx *loc, void *vd)
730 {
731 for_each_rtx (loc, cprop_find_used_regs_1, vd);
732 }
733
734 /* Perform the forward copy propagation on basic block BB. */
735
736 static bool
737 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
738 {
739 bool anything_changed = false;
740 rtx_insn *insn;
741
742 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
743 {
744 int n_ops, i, predicated;
745 bool is_asm, any_replacements;
746 rtx set;
747 rtx link;
748 bool replaced[MAX_RECOG_OPERANDS];
749 bool changed = false;
750 struct kill_set_value_data ksvd;
751
752 if (!NONDEBUG_INSN_P (insn))
753 {
754 if (DEBUG_INSN_P (insn))
755 {
756 rtx loc = INSN_VAR_LOCATION_LOC (insn);
757 if (!VAR_LOC_UNKNOWN_P (loc))
758 replace_oldest_value_addr (&INSN_VAR_LOCATION_LOC (insn),
759 ALL_REGS, GET_MODE (loc),
760 ADDR_SPACE_GENERIC, insn, vd);
761 }
762
763 if (insn == BB_END (bb))
764 break;
765 else
766 continue;
767 }
768
769 set = single_set (insn);
770 extract_insn (insn);
771 if (! constrain_operands (1))
772 fatal_insn_not_found (insn);
773 preprocess_constraints (insn);
774 const operand_alternative *op_alt = which_op_alt ();
775 n_ops = recog_data.n_operands;
776 is_asm = asm_noperands (PATTERN (insn)) >= 0;
777
778 /* Simplify the code below by promoting OP_OUT to OP_INOUT
779 in predicated instructions. */
780
781 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
782 for (i = 0; i < n_ops; ++i)
783 {
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;
788 }
789
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);
793
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);
798
799 /* Within asms, a clobber cannot overlap inputs or outputs.
800 I wouldn't think this were true for regular insns, but
801 scan_rtx treats them like that... */
802 note_stores (PATTERN (insn), kill_clobbered_value, vd);
803
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);
807
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);
812
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))
816 {
817 if (REG_NOTE_KIND (link) == REG_UNUSED)
818 {
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;
827 }
828 }
829
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)))
833 {
834 rtx src = SET_SRC (set);
835 unsigned int regno = REGNO (src);
836 enum machine_mode mode = GET_MODE (src);
837 unsigned int i;
838 rtx new_rtx;
839
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)
843 {
844 if (hard_regno_nregs[regno][mode]
845 > hard_regno_nregs[regno][vd->e[regno].mode])
846 goto no_move_special_case;
847
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;
855 }
856
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)))
860 {
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))
863 {
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;
870 }
871 /* We need to re-extract as validate_change clobbers
872 recog_data. */
873 extract_insn (insn);
874 if (! constrain_operands (1))
875 fatal_insn_not_found (insn);
876 preprocess_constraints (insn);
877 }
878
879 /* Otherwise, try all valid registers and see if its valid. */
880 for (i = vd->e[regno].oldest_regno; i != regno;
881 i = vd->e[i].next_regno)
882 {
883 new_rtx = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
884 mode, i, regno);
885 if (new_rtx != NULL_RTX)
886 {
887 if (validate_change (insn, &SET_SRC (set), new_rtx, 0))
888 {
889 ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (src);
890 REG_ATTRS (new_rtx) = REG_ATTRS (src);
891 REG_POINTER (new_rtx) = REG_POINTER (src);
892 if (dump_file)
893 fprintf (dump_file,
894 "insn %u: replaced reg %u with %u\n",
895 INSN_UID (insn), regno, REGNO (new_rtx));
896 changed = true;
897 goto did_replacement;
898 }
899 /* We need to re-extract as validate_change clobbers
900 recog_data. */
901 extract_insn (insn);
902 if (! constrain_operands (1))
903 fatal_insn_not_found (insn);
904 preprocess_constraints (insn);
905 }
906 }
907 }
908 no_move_special_case:
909
910 any_replacements = false;
911
912 /* For each input operand, replace a hard register with the
913 eldest live copy that's in an appropriate register class. */
914 for (i = 0; i < n_ops; i++)
915 {
916 replaced[i] = false;
917
918 /* Don't scan match_operand here, since we've no reg class
919 information to pass down. Any operands that we could
920 substitute in will be represented elsewhere. */
921 if (recog_data.constraints[i][0] == '\0')
922 continue;
923
924 /* Don't replace in asms intentionally referencing hard regs. */
925 if (is_asm && REG_P (recog_data.operand[i])
926 && (REGNO (recog_data.operand[i])
927 == ORIGINAL_REGNO (recog_data.operand[i])))
928 continue;
929
930 if (recog_data.operand_type[i] == OP_IN)
931 {
932 if (op_alt[i].is_address)
933 replaced[i]
934 = replace_oldest_value_addr (recog_data.operand_loc[i],
935 alternative_class (op_alt, i),
936 VOIDmode, ADDR_SPACE_GENERIC,
937 insn, vd);
938 else if (REG_P (recog_data.operand[i]))
939 replaced[i]
940 = replace_oldest_value_reg (recog_data.operand_loc[i],
941 alternative_class (op_alt, i),
942 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);
946 }
947 else if (MEM_P (recog_data.operand[i]))
948 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
949 insn, vd);
950
951 /* If we performed any replacement, update match_dups. */
952 if (replaced[i])
953 {
954 int j;
955 rtx new_rtx;
956
957 new_rtx = *recog_data.operand_loc[i];
958 recog_data.operand[i] = new_rtx;
959 for (j = 0; j < recog_data.n_dups; j++)
960 if (recog_data.dup_num[j] == i)
961 validate_unshare_change (insn, recog_data.dup_loc[j], new_rtx, 1);
962
963 any_replacements = true;
964 }
965 }
966
967 if (any_replacements)
968 {
969 if (! apply_change_group ())
970 {
971 for (i = 0; i < n_ops; i++)
972 if (replaced[i])
973 {
974 rtx old = *recog_data.operand_loc[i];
975 recog_data.operand[i] = old;
976 }
977
978 if (dump_file)
979 fprintf (dump_file,
980 "insn %u: reg replacements not verified\n",
981 INSN_UID (insn));
982 }
983 else
984 changed = true;
985 }
986
987 did_replacement:
988 if (changed)
989 {
990 anything_changed = true;
991
992 /* If something changed, perhaps further changes to earlier
993 DEBUG_INSNs can be applied. */
994 if (vd->n_debug_insn_changes)
995 note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
996 }
997
998 ksvd.vd = vd;
999 ksvd.ignore_set_reg = NULL_RTX;
1000
1001 /* Clobber call-clobbered registers. */
1002 if (CALL_P (insn))
1003 {
1004 unsigned int set_regno = INVALID_REGNUM;
1005 unsigned int set_nregs = 0;
1006 unsigned int regno;
1007 rtx exp;
1008
1009 for (exp = CALL_INSN_FUNCTION_USAGE (insn); exp; exp = XEXP (exp, 1))
1010 {
1011 rtx x = XEXP (exp, 0);
1012 if (GET_CODE (x) == SET)
1013 {
1014 rtx dest = SET_DEST (x);
1015 kill_value (dest, vd);
1016 set_value_regno (REGNO (dest), GET_MODE (dest), vd);
1017 copy_value (dest, SET_SRC (x), vd);
1018 ksvd.ignore_set_reg = dest;
1019 set_regno = REGNO (dest);
1020 set_nregs
1021 = hard_regno_nregs[set_regno][GET_MODE (dest)];
1022 break;
1023 }
1024 }
1025
1026 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1027 if ((TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)
1028 || HARD_REGNO_CALL_PART_CLOBBERED (regno, vd->e[regno].mode))
1029 && (regno < set_regno || regno >= set_regno + set_nregs))
1030 kill_value_regno (regno, 1, vd);
1031
1032 /* If SET was seen in CALL_INSN_FUNCTION_USAGE, and SET_SRC
1033 of the SET isn't in regs_invalidated_by_call hard reg set,
1034 but instead among CLOBBERs on the CALL_INSN, we could wrongly
1035 assume the value in it is still live. */
1036 if (ksvd.ignore_set_reg)
1037 note_stores (PATTERN (insn), kill_clobbered_value, vd);
1038 }
1039
1040 /* Notice stores. */
1041 note_stores (PATTERN (insn), kill_set_value, &ksvd);
1042
1043 /* Notice copies. */
1044 if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1045 copy_value (SET_DEST (set), SET_SRC (set), vd);
1046
1047 if (insn == BB_END (bb))
1048 break;
1049 }
1050
1051 return anything_changed;
1052 }
1053
1054 /* Dump the value chain data to stderr. */
1055
1056 DEBUG_FUNCTION void
1057 debug_value_data (struct value_data *vd)
1058 {
1059 HARD_REG_SET set;
1060 unsigned int i, j;
1061
1062 CLEAR_HARD_REG_SET (set);
1063
1064 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1065 if (vd->e[i].oldest_regno == i)
1066 {
1067 if (vd->e[i].mode == VOIDmode)
1068 {
1069 if (vd->e[i].next_regno != INVALID_REGNUM)
1070 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1071 i, vd->e[i].next_regno);
1072 continue;
1073 }
1074
1075 SET_HARD_REG_BIT (set, i);
1076 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1077
1078 for (j = vd->e[i].next_regno;
1079 j != INVALID_REGNUM;
1080 j = vd->e[j].next_regno)
1081 {
1082 if (TEST_HARD_REG_BIT (set, j))
1083 {
1084 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1085 return;
1086 }
1087
1088 if (vd->e[j].oldest_regno != i)
1089 {
1090 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1091 j, vd->e[j].oldest_regno);
1092 return;
1093 }
1094 SET_HARD_REG_BIT (set, j);
1095 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1096 }
1097 fputc ('\n', stderr);
1098 }
1099
1100 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1101 if (! TEST_HARD_REG_BIT (set, i)
1102 && (vd->e[i].mode != VOIDmode
1103 || vd->e[i].oldest_regno != i
1104 || vd->e[i].next_regno != INVALID_REGNUM))
1105 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1106 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1107 vd->e[i].next_regno);
1108 }
1109
1110 /* Do copyprop_hardreg_forward_1 for a single basic block BB.
1111 DEBUG_INSN is skipped since we do not want to involve DF related
1112 staff as how it is handled in function pass_cprop_hardreg::execute.
1113
1114 NOTE: Currently it is only used for shrink-wrap. Maybe extend it
1115 to handle DEBUG_INSN for other uses. */
1116
1117 void
1118 copyprop_hardreg_forward_bb_without_debug_insn (basic_block bb)
1119 {
1120 struct value_data *vd;
1121 vd = XNEWVEC (struct value_data, 1);
1122 init_value_data (vd);
1123
1124 skip_debug_insn_p = true;
1125 copyprop_hardreg_forward_1 (bb, vd);
1126 free (vd);
1127 skip_debug_insn_p = false;
1128 }
1129
1130 #ifdef ENABLE_CHECKING
1131 static void
1132 validate_value_data (struct value_data *vd)
1133 {
1134 HARD_REG_SET set;
1135 unsigned int i, j;
1136
1137 CLEAR_HARD_REG_SET (set);
1138
1139 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1140 if (vd->e[i].oldest_regno == i)
1141 {
1142 if (vd->e[i].mode == VOIDmode)
1143 {
1144 if (vd->e[i].next_regno != INVALID_REGNUM)
1145 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1146 i, vd->e[i].next_regno);
1147 continue;
1148 }
1149
1150 SET_HARD_REG_BIT (set, i);
1151
1152 for (j = vd->e[i].next_regno;
1153 j != INVALID_REGNUM;
1154 j = vd->e[j].next_regno)
1155 {
1156 if (TEST_HARD_REG_BIT (set, j))
1157 internal_error ("validate_value_data: Loop in regno chain (%u)",
1158 j);
1159 if (vd->e[j].oldest_regno != i)
1160 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1161 j, vd->e[j].oldest_regno);
1162
1163 SET_HARD_REG_BIT (set, j);
1164 }
1165 }
1166
1167 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1168 if (! TEST_HARD_REG_BIT (set, i)
1169 && (vd->e[i].mode != VOIDmode
1170 || vd->e[i].oldest_regno != i
1171 || vd->e[i].next_regno != INVALID_REGNUM))
1172 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1173 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1174 vd->e[i].next_regno);
1175 }
1176 #endif
1177 \f
1178 namespace {
1179
1180 const pass_data pass_data_cprop_hardreg =
1181 {
1182 RTL_PASS, /* type */
1183 "cprop_hardreg", /* name */
1184 OPTGROUP_NONE, /* optinfo_flags */
1185 TV_CPROP_REGISTERS, /* tv_id */
1186 0, /* properties_required */
1187 0, /* properties_provided */
1188 0, /* properties_destroyed */
1189 0, /* todo_flags_start */
1190 TODO_df_finish, /* todo_flags_finish */
1191 };
1192
1193 class pass_cprop_hardreg : public rtl_opt_pass
1194 {
1195 public:
1196 pass_cprop_hardreg (gcc::context *ctxt)
1197 : rtl_opt_pass (pass_data_cprop_hardreg, ctxt)
1198 {}
1199
1200 /* opt_pass methods: */
1201 virtual bool gate (function *)
1202 {
1203 return (optimize > 0 && (flag_cprop_registers));
1204 }
1205
1206 virtual unsigned int execute (function *);
1207
1208 }; // class pass_cprop_hardreg
1209
1210 unsigned int
1211 pass_cprop_hardreg::execute (function *fun)
1212 {
1213 struct value_data *all_vd;
1214 basic_block bb;
1215 sbitmap visited;
1216 bool analyze_called = false;
1217
1218 all_vd = XNEWVEC (struct value_data, last_basic_block_for_fn (fun));
1219
1220 visited = sbitmap_alloc (last_basic_block_for_fn (fun));
1221 bitmap_clear (visited);
1222
1223 if (MAY_HAVE_DEBUG_INSNS)
1224 debug_insn_changes_pool
1225 = create_alloc_pool ("debug insn changes pool",
1226 sizeof (struct queued_debug_insn_change), 256);
1227
1228 FOR_EACH_BB_FN (bb, fun)
1229 {
1230 bitmap_set_bit (visited, bb->index);
1231
1232 /* If a block has a single predecessor, that we've already
1233 processed, begin with the value data that was live at
1234 the end of the predecessor block. */
1235 /* ??? Ought to use more intelligent queuing of blocks. */
1236 if (single_pred_p (bb)
1237 && bitmap_bit_p (visited, single_pred (bb)->index)
1238 && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
1239 {
1240 all_vd[bb->index] = all_vd[single_pred (bb)->index];
1241 if (all_vd[bb->index].n_debug_insn_changes)
1242 {
1243 unsigned int regno;
1244
1245 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1246 {
1247 if (all_vd[bb->index].e[regno].debug_insn_changes)
1248 {
1249 all_vd[bb->index].e[regno].debug_insn_changes = NULL;
1250 if (--all_vd[bb->index].n_debug_insn_changes == 0)
1251 break;
1252 }
1253 }
1254 }
1255 }
1256 else
1257 init_value_data (all_vd + bb->index);
1258
1259 copyprop_hardreg_forward_1 (bb, all_vd + bb->index);
1260 }
1261
1262 if (MAY_HAVE_DEBUG_INSNS)
1263 {
1264 FOR_EACH_BB_FN (bb, fun)
1265 if (bitmap_bit_p (visited, bb->index)
1266 && all_vd[bb->index].n_debug_insn_changes)
1267 {
1268 unsigned int regno;
1269 bitmap live;
1270
1271 if (!analyze_called)
1272 {
1273 df_analyze ();
1274 analyze_called = true;
1275 }
1276 live = df_get_live_out (bb);
1277 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1278 if (all_vd[bb->index].e[regno].debug_insn_changes)
1279 {
1280 if (REGNO_REG_SET_P (live, regno))
1281 apply_debug_insn_changes (all_vd + bb->index, regno);
1282 if (all_vd[bb->index].n_debug_insn_changes == 0)
1283 break;
1284 }
1285 }
1286
1287 free_alloc_pool (debug_insn_changes_pool);
1288 }
1289
1290 sbitmap_free (visited);
1291 free (all_vd);
1292 return 0;
1293 }
1294
1295 } // anon namespace
1296
1297 rtl_opt_pass *
1298 make_pass_cprop_hardreg (gcc::context *ctxt)
1299 {
1300 return new pass_cprop_hardreg (ctxt);
1301 }