coretypes.h: Include input.h and as-a.h.
[gcc.git] / gcc / regcprop.c
1 /* Copy propagation on hard registers for the GNU compiler.
2 Copyright (C) 2000-2015 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 "predict.h"
31 #include "function.h"
32 #include "dominance.h"
33 #include "cfg.h"
34 #include "basic-block.h"
35 #include "reload.h"
36 #include "recog.h"
37 #include "flags.h"
38 #include "diagnostic-core.h"
39 #include "obstack.h"
40 #include "tree-pass.h"
41 #include "df.h"
42 #include "rtl-iter.h"
43
44 /* The following code does forward propagation of hard register copies.
45 The object is to eliminate as many dependencies as possible, so that
46 we have the most scheduling freedom. As a side effect, we also clean
47 up some silly register allocation decisions made by reload. This
48 code may be obsoleted by a new register allocator. */
49
50 /* DEBUG_INSNs aren't changed right away, as doing so might extend the
51 lifetime of a register and get the DEBUG_INSN subsequently reset.
52 So they are queued instead, and updated only when the register is
53 used in some subsequent real insn before it is set. */
54 struct queued_debug_insn_change
55 {
56 struct queued_debug_insn_change *next;
57 rtx_insn *insn;
58 rtx *loc;
59 rtx new_rtx;
60
61 /* Pool allocation new operator. */
62 inline void *operator new (size_t)
63 {
64 return pool.allocate ();
65 }
66
67 /* Delete operator utilizing pool allocation. */
68 inline void operator delete (void *ptr)
69 {
70 pool.remove ((queued_debug_insn_change *) ptr);
71 }
72
73 /* Memory allocation pool. */
74 static pool_allocator<queued_debug_insn_change> pool;
75 };
76
77 /* For each register, we have a list of registers that contain the same
78 value. The OLDEST_REGNO field points to the head of the list, and
79 the NEXT_REGNO field runs through the list. The MODE field indicates
80 what mode the data is known to be in; this field is VOIDmode when the
81 register is not known to contain valid data. */
82
83 struct value_data_entry
84 {
85 machine_mode mode;
86 unsigned int oldest_regno;
87 unsigned int next_regno;
88 struct queued_debug_insn_change *debug_insn_changes;
89 };
90
91 struct value_data
92 {
93 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
94 unsigned int max_value_regs;
95 unsigned int n_debug_insn_changes;
96 };
97
98 pool_allocator<queued_debug_insn_change> queued_debug_insn_change::pool
99 ("debug insn changes pool", 256);
100
101 static bool skip_debug_insn_p;
102
103 static void kill_value_one_regno (unsigned, struct value_data *);
104 static void kill_value_regno (unsigned, unsigned, struct value_data *);
105 static void kill_value (const_rtx, struct value_data *);
106 static void set_value_regno (unsigned, machine_mode, struct value_data *);
107 static void init_value_data (struct value_data *);
108 static void kill_clobbered_value (rtx, const_rtx, void *);
109 static void kill_set_value (rtx, const_rtx, void *);
110 static void copy_value (rtx, rtx, struct value_data *);
111 static bool mode_change_ok (machine_mode, machine_mode,
112 unsigned int);
113 static rtx maybe_mode_change (machine_mode, machine_mode,
114 machine_mode, unsigned int, unsigned int);
115 static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
116 static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx_insn *,
117 struct value_data *);
118 static bool replace_oldest_value_addr (rtx *, enum reg_class,
119 machine_mode, addr_space_t,
120 rtx_insn *, struct value_data *);
121 static bool replace_oldest_value_mem (rtx, rtx_insn *, struct value_data *);
122 static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
123 extern void debug_value_data (struct value_data *);
124 #ifdef ENABLE_CHECKING
125 static void validate_value_data (struct value_data *);
126 #endif
127
128 /* Free all queued updates for DEBUG_INSNs that change some reg to
129 register REGNO. */
130
131 static void
132 free_debug_insn_changes (struct value_data *vd, unsigned int regno)
133 {
134 struct queued_debug_insn_change *cur, *next;
135 for (cur = vd->e[regno].debug_insn_changes; cur; cur = next)
136 {
137 next = cur->next;
138 --vd->n_debug_insn_changes;
139 delete cur;
140 }
141 vd->e[regno].debug_insn_changes = NULL;
142 }
143
144 /* Kill register REGNO. This involves removing it from any value
145 lists, and resetting the value mode to VOIDmode. This is only a
146 helper function; it does not handle any hard registers overlapping
147 with REGNO. */
148
149 static void
150 kill_value_one_regno (unsigned int regno, struct value_data *vd)
151 {
152 unsigned int i, next;
153
154 if (vd->e[regno].oldest_regno != regno)
155 {
156 for (i = vd->e[regno].oldest_regno;
157 vd->e[i].next_regno != regno;
158 i = vd->e[i].next_regno)
159 continue;
160 vd->e[i].next_regno = vd->e[regno].next_regno;
161 }
162 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
163 {
164 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
165 vd->e[i].oldest_regno = next;
166 }
167
168 vd->e[regno].mode = VOIDmode;
169 vd->e[regno].oldest_regno = regno;
170 vd->e[regno].next_regno = INVALID_REGNUM;
171 if (vd->e[regno].debug_insn_changes)
172 free_debug_insn_changes (vd, regno);
173
174 #ifdef ENABLE_CHECKING
175 validate_value_data (vd);
176 #endif
177 }
178
179 /* Kill the value in register REGNO for NREGS, and any other registers
180 whose values overlap. */
181
182 static void
183 kill_value_regno (unsigned int regno, unsigned int nregs,
184 struct value_data *vd)
185 {
186 unsigned int j;
187
188 /* Kill the value we're told to kill. */
189 for (j = 0; j < nregs; ++j)
190 kill_value_one_regno (regno + j, vd);
191
192 /* Kill everything that overlapped what we're told to kill. */
193 if (regno < vd->max_value_regs)
194 j = 0;
195 else
196 j = regno - vd->max_value_regs;
197 for (; j < regno; ++j)
198 {
199 unsigned int i, n;
200 if (vd->e[j].mode == VOIDmode)
201 continue;
202 n = hard_regno_nregs[j][vd->e[j].mode];
203 if (j + n > regno)
204 for (i = 0; i < n; ++i)
205 kill_value_one_regno (j + i, vd);
206 }
207 }
208
209 /* Kill X. This is a convenience function wrapping kill_value_regno
210 so that we mind the mode the register is in. */
211
212 static void
213 kill_value (const_rtx x, struct value_data *vd)
214 {
215 if (GET_CODE (x) == SUBREG)
216 {
217 rtx tmp = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
218 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
219 x = tmp ? tmp : SUBREG_REG (x);
220 }
221 if (REG_P (x))
222 kill_value_regno (REGNO (x), REG_NREGS (x), vd);
223 }
224
225 /* Remember that REGNO is valid in MODE. */
226
227 static void
228 set_value_regno (unsigned int regno, machine_mode mode,
229 struct value_data *vd)
230 {
231 unsigned int nregs;
232
233 vd->e[regno].mode = mode;
234
235 nregs = hard_regno_nregs[regno][mode];
236 if (nregs > vd->max_value_regs)
237 vd->max_value_regs = nregs;
238 }
239
240 /* Initialize VD such that there are no known relationships between regs. */
241
242 static void
243 init_value_data (struct value_data *vd)
244 {
245 int i;
246 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
247 {
248 vd->e[i].mode = VOIDmode;
249 vd->e[i].oldest_regno = i;
250 vd->e[i].next_regno = INVALID_REGNUM;
251 vd->e[i].debug_insn_changes = NULL;
252 }
253 vd->max_value_regs = 0;
254 vd->n_debug_insn_changes = 0;
255 }
256
257 /* Called through note_stores. If X is clobbered, kill its value. */
258
259 static void
260 kill_clobbered_value (rtx x, const_rtx set, void *data)
261 {
262 struct value_data *const vd = (struct value_data *) data;
263 if (GET_CODE (set) == CLOBBER)
264 kill_value (x, vd);
265 }
266
267 /* A structure passed as data to kill_set_value through note_stores. */
268 struct kill_set_value_data
269 {
270 struct value_data *vd;
271 rtx ignore_set_reg;
272 };
273
274 /* Called through note_stores. If X is set, not clobbered, kill its
275 current value and install it as the root of its own value list. */
276
277 static void
278 kill_set_value (rtx x, const_rtx set, void *data)
279 {
280 struct kill_set_value_data *ksvd = (struct kill_set_value_data *) data;
281 if (rtx_equal_p (x, ksvd->ignore_set_reg))
282 return;
283 if (GET_CODE (set) != CLOBBER)
284 {
285 kill_value (x, ksvd->vd);
286 if (REG_P (x))
287 set_value_regno (REGNO (x), GET_MODE (x), ksvd->vd);
288 }
289 }
290
291 /* Kill any register used in X as the base of an auto-increment expression,
292 and install that register as the root of its own value list. */
293
294 static void
295 kill_autoinc_value (rtx_insn *insn, struct value_data *vd)
296 {
297 subrtx_iterator::array_type array;
298 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
299 {
300 const_rtx x = *iter;
301 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
302 {
303 x = XEXP (x, 0);
304 kill_value (x, vd);
305 set_value_regno (REGNO (x), GET_MODE (x), vd);
306 iter.skip_subrtxes ();
307 }
308 }
309 }
310
311 /* Assert that SRC has been copied to DEST. Adjust the data structures
312 to reflect that SRC contains an older copy of the shared value. */
313
314 static void
315 copy_value (rtx dest, rtx src, struct value_data *vd)
316 {
317 unsigned int dr = REGNO (dest);
318 unsigned int sr = REGNO (src);
319 unsigned int dn, sn;
320 unsigned int i;
321
322 /* ??? At present, it's possible to see noop sets. It'd be nice if
323 this were cleaned up beforehand... */
324 if (sr == dr)
325 return;
326
327 /* Do not propagate copies to the stack pointer, as that can leave
328 memory accesses with no scheduling dependency on the stack update. */
329 if (dr == STACK_POINTER_REGNUM)
330 return;
331
332 /* Likewise with the frame pointer, if we're using one. */
333 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
334 return;
335
336 /* Do not propagate copies to fixed or global registers, patterns
337 can be relying to see particular fixed register or users can
338 expect the chosen global register in asm. */
339 if (fixed_regs[dr] || global_regs[dr])
340 return;
341
342 /* If SRC and DEST overlap, don't record anything. */
343 dn = REG_NREGS (dest);
344 sn = REG_NREGS (src);
345 if ((dr > sr && dr < sr + sn)
346 || (sr > dr && sr < dr + dn))
347 return;
348
349 /* If SRC had no assigned mode (i.e. we didn't know it was live)
350 assign it now and assume the value came from an input argument
351 or somesuch. */
352 if (vd->e[sr].mode == VOIDmode)
353 set_value_regno (sr, vd->e[dr].mode, vd);
354
355 /* If we are narrowing the input to a smaller number of hard regs,
356 and it is in big endian, we are really extracting a high part.
357 Since we generally associate a low part of a value with the value itself,
358 we must not do the same for the high part.
359 Note we can still get low parts for the same mode combination through
360 a two-step copy involving differently sized hard regs.
361 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
362 (set (reg:DI r0) (reg:DI fr0))
363 (set (reg:SI fr2) (reg:SI r0))
364 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
365 (set (reg:SI fr2) (reg:SI fr0))
366 loads the high part of (reg:DI fr0) into fr2.
367
368 We can't properly represent the latter case in our tables, so don't
369 record anything then. */
370 else if (sn < (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode]
371 && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
372 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
373 return;
374
375 /* If SRC had been assigned a mode narrower than the copy, we can't
376 link DEST into the chain, because not all of the pieces of the
377 copy came from oldest_regno. */
378 else if (sn > (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode])
379 return;
380
381 /* Link DR at the end of the value chain used by SR. */
382
383 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
384
385 for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
386 continue;
387 vd->e[i].next_regno = dr;
388
389 #ifdef ENABLE_CHECKING
390 validate_value_data (vd);
391 #endif
392 }
393
394 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
395
396 static bool
397 mode_change_ok (machine_mode orig_mode, machine_mode new_mode,
398 unsigned int regno ATTRIBUTE_UNUSED)
399 {
400 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
401 return false;
402
403 #ifdef CANNOT_CHANGE_MODE_CLASS
404 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
405 #endif
406
407 return true;
408 }
409
410 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
411 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
412 in NEW_MODE.
413 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
414
415 static rtx
416 maybe_mode_change (machine_mode orig_mode, machine_mode copy_mode,
417 machine_mode new_mode, unsigned int regno,
418 unsigned int copy_regno ATTRIBUTE_UNUSED)
419 {
420 if (GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (orig_mode)
421 && GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (new_mode))
422 return NULL_RTX;
423
424 if (orig_mode == new_mode)
425 return gen_raw_REG (new_mode, regno);
426 else if (mode_change_ok (orig_mode, new_mode, regno))
427 {
428 int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
429 int use_nregs = hard_regno_nregs[copy_regno][new_mode];
430 int copy_offset
431 = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
432 int offset
433 = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
434 int byteoffset = offset % UNITS_PER_WORD;
435 int wordoffset = offset - byteoffset;
436
437 offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
438 + (BYTES_BIG_ENDIAN ? byteoffset : 0));
439 regno += subreg_regno_offset (regno, orig_mode, offset, new_mode);
440 if (HARD_REGNO_MODE_OK (regno, new_mode))
441 return gen_raw_REG (new_mode, regno);
442 }
443 return NULL_RTX;
444 }
445
446 /* Find the oldest copy of the value contained in REGNO that is in
447 register class CL and has mode MODE. If found, return an rtx
448 of that oldest register, otherwise return NULL. */
449
450 static rtx
451 find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
452 {
453 unsigned int regno = REGNO (reg);
454 machine_mode mode = GET_MODE (reg);
455 unsigned int i;
456
457 /* If we are accessing REG in some mode other that what we set it in,
458 make sure that the replacement is valid. In particular, consider
459 (set (reg:DI r11) (...))
460 (set (reg:SI r9) (reg:SI r11))
461 (set (reg:SI r10) (...))
462 (set (...) (reg:DI r9))
463 Replacing r9 with r11 is invalid. */
464 if (mode != vd->e[regno].mode)
465 {
466 if (hard_regno_nregs[regno][mode]
467 > hard_regno_nregs[regno][vd->e[regno].mode])
468 return NULL_RTX;
469 }
470
471 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
472 {
473 machine_mode oldmode = vd->e[i].mode;
474 rtx new_rtx;
475
476 if (!in_hard_reg_set_p (reg_class_contents[cl], mode, i))
477 continue;
478
479 new_rtx = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
480 if (new_rtx)
481 {
482 ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (reg);
483 REG_ATTRS (new_rtx) = REG_ATTRS (reg);
484 REG_POINTER (new_rtx) = REG_POINTER (reg);
485 return new_rtx;
486 }
487 }
488
489 return NULL_RTX;
490 }
491
492 /* If possible, replace the register at *LOC with the oldest register
493 in register class CL. Return true if successfully replaced. */
494
495 static bool
496 replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx_insn *insn,
497 struct value_data *vd)
498 {
499 rtx new_rtx = find_oldest_value_reg (cl, *loc, vd);
500 if (new_rtx && (!DEBUG_INSN_P (insn) || !skip_debug_insn_p))
501 {
502 if (DEBUG_INSN_P (insn))
503 {
504 struct queued_debug_insn_change *change;
505
506 if (dump_file)
507 fprintf (dump_file, "debug_insn %u: queued replacing reg %u with %u\n",
508 INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
509
510 change = new queued_debug_insn_change;
511 change->next = vd->e[REGNO (new_rtx)].debug_insn_changes;
512 change->insn = insn;
513 change->loc = loc;
514 change->new_rtx = new_rtx;
515 vd->e[REGNO (new_rtx)].debug_insn_changes = change;
516 ++vd->n_debug_insn_changes;
517 return true;
518 }
519 if (dump_file)
520 fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
521 INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
522
523 validate_change (insn, loc, new_rtx, 1);
524 return true;
525 }
526 return false;
527 }
528
529 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
530 Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
531 BASE_REG_CLASS depending on how the register is being considered. */
532
533 static bool
534 replace_oldest_value_addr (rtx *loc, enum reg_class cl,
535 machine_mode mode, addr_space_t as,
536 rtx_insn *insn, struct value_data *vd)
537 {
538 rtx x = *loc;
539 RTX_CODE code = GET_CODE (x);
540 const char *fmt;
541 int i, j;
542 bool changed = false;
543
544 switch (code)
545 {
546 case PLUS:
547 if (DEBUG_INSN_P (insn))
548 break;
549
550 {
551 rtx orig_op0 = XEXP (x, 0);
552 rtx orig_op1 = XEXP (x, 1);
553 RTX_CODE code0 = GET_CODE (orig_op0);
554 RTX_CODE code1 = GET_CODE (orig_op1);
555 rtx op0 = orig_op0;
556 rtx op1 = orig_op1;
557 rtx *locI = NULL;
558 rtx *locB = NULL;
559 enum rtx_code index_code = SCRATCH;
560
561 if (GET_CODE (op0) == SUBREG)
562 {
563 op0 = SUBREG_REG (op0);
564 code0 = GET_CODE (op0);
565 }
566
567 if (GET_CODE (op1) == SUBREG)
568 {
569 op1 = SUBREG_REG (op1);
570 code1 = GET_CODE (op1);
571 }
572
573 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
574 || code0 == ZERO_EXTEND || code1 == MEM)
575 {
576 locI = &XEXP (x, 0);
577 locB = &XEXP (x, 1);
578 index_code = GET_CODE (*locI);
579 }
580 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
581 || code1 == ZERO_EXTEND || code0 == MEM)
582 {
583 locI = &XEXP (x, 1);
584 locB = &XEXP (x, 0);
585 index_code = GET_CODE (*locI);
586 }
587 else if (code0 == CONST_INT || code0 == CONST
588 || code0 == SYMBOL_REF || code0 == LABEL_REF)
589 {
590 locB = &XEXP (x, 1);
591 index_code = GET_CODE (XEXP (x, 0));
592 }
593 else if (code1 == CONST_INT || code1 == CONST
594 || code1 == SYMBOL_REF || code1 == LABEL_REF)
595 {
596 locB = &XEXP (x, 0);
597 index_code = GET_CODE (XEXP (x, 1));
598 }
599 else if (code0 == REG && code1 == REG)
600 {
601 int index_op;
602 unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
603
604 if (REGNO_OK_FOR_INDEX_P (regno1)
605 && regno_ok_for_base_p (regno0, mode, as, PLUS, REG))
606 index_op = 1;
607 else if (REGNO_OK_FOR_INDEX_P (regno0)
608 && regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
609 index_op = 0;
610 else if (regno_ok_for_base_p (regno0, mode, as, PLUS, REG)
611 || REGNO_OK_FOR_INDEX_P (regno1))
612 index_op = 1;
613 else if (regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
614 index_op = 0;
615 else
616 index_op = 1;
617
618 locI = &XEXP (x, index_op);
619 locB = &XEXP (x, !index_op);
620 index_code = GET_CODE (*locI);
621 }
622 else if (code0 == REG)
623 {
624 locI = &XEXP (x, 0);
625 locB = &XEXP (x, 1);
626 index_code = GET_CODE (*locI);
627 }
628 else if (code1 == REG)
629 {
630 locI = &XEXP (x, 1);
631 locB = &XEXP (x, 0);
632 index_code = GET_CODE (*locI);
633 }
634
635 if (locI)
636 changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS,
637 mode, as, insn, vd);
638 if (locB)
639 changed |= replace_oldest_value_addr (locB,
640 base_reg_class (mode, as, PLUS,
641 index_code),
642 mode, as, insn, vd);
643 return changed;
644 }
645
646 case POST_INC:
647 case POST_DEC:
648 case POST_MODIFY:
649 case PRE_INC:
650 case PRE_DEC:
651 case PRE_MODIFY:
652 return false;
653
654 case MEM:
655 return replace_oldest_value_mem (x, insn, vd);
656
657 case REG:
658 return replace_oldest_value_reg (loc, cl, insn, vd);
659
660 default:
661 break;
662 }
663
664 fmt = GET_RTX_FORMAT (code);
665 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
666 {
667 if (fmt[i] == 'e')
668 changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode, as,
669 insn, vd);
670 else if (fmt[i] == 'E')
671 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
672 changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
673 mode, as, insn, vd);
674 }
675
676 return changed;
677 }
678
679 /* Similar to replace_oldest_value_reg, but X contains a memory. */
680
681 static bool
682 replace_oldest_value_mem (rtx x, rtx_insn *insn, struct value_data *vd)
683 {
684 enum reg_class cl;
685
686 if (DEBUG_INSN_P (insn))
687 cl = ALL_REGS;
688 else
689 cl = base_reg_class (GET_MODE (x), MEM_ADDR_SPACE (x), MEM, SCRATCH);
690
691 return replace_oldest_value_addr (&XEXP (x, 0), cl,
692 GET_MODE (x), MEM_ADDR_SPACE (x),
693 insn, vd);
694 }
695
696 /* Apply all queued updates for DEBUG_INSNs that change some reg to
697 register REGNO. */
698
699 static void
700 apply_debug_insn_changes (struct value_data *vd, unsigned int regno)
701 {
702 struct queued_debug_insn_change *change;
703 rtx_insn *last_insn = vd->e[regno].debug_insn_changes->insn;
704
705 for (change = vd->e[regno].debug_insn_changes;
706 change;
707 change = change->next)
708 {
709 if (last_insn != change->insn)
710 {
711 apply_change_group ();
712 last_insn = change->insn;
713 }
714 validate_change (change->insn, change->loc, change->new_rtx, 1);
715 }
716 apply_change_group ();
717 }
718
719 /* Called via note_uses, for all used registers in a real insn
720 apply DEBUG_INSN changes that change registers to the used
721 registers. */
722
723 static void
724 cprop_find_used_regs (rtx *loc, void *data)
725 {
726 struct value_data *const vd = (struct value_data *) data;
727 subrtx_iterator::array_type array;
728 FOR_EACH_SUBRTX (iter, array, *loc, NONCONST)
729 {
730 const_rtx x = *iter;
731 if (REG_P (x))
732 {
733 unsigned int regno = REGNO (x);
734 if (vd->e[regno].debug_insn_changes)
735 {
736 apply_debug_insn_changes (vd, regno);
737 free_debug_insn_changes (vd, regno);
738 }
739 }
740 }
741 }
742
743 /* Apply clobbers of INSN in PATTERN and C_I_F_U to value_data VD. */
744
745 static void
746 kill_clobbered_values (rtx_insn *insn, struct value_data *vd)
747 {
748 note_stores (PATTERN (insn), kill_clobbered_value, vd);
749
750 if (CALL_P (insn))
751 {
752 rtx exp;
753
754 for (exp = CALL_INSN_FUNCTION_USAGE (insn); exp; exp = XEXP (exp, 1))
755 {
756 rtx x = XEXP (exp, 0);
757 if (GET_CODE (x) == CLOBBER)
758 kill_value (SET_DEST (x), vd);
759 }
760 }
761 }
762
763 /* Perform the forward copy propagation on basic block BB. */
764
765 static bool
766 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
767 {
768 bool anything_changed = false;
769 rtx_insn *insn;
770
771 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
772 {
773 int n_ops, i, predicated;
774 bool is_asm, any_replacements;
775 rtx set;
776 rtx link;
777 bool replaced[MAX_RECOG_OPERANDS];
778 bool changed = false;
779 struct kill_set_value_data ksvd;
780
781 if (!NONDEBUG_INSN_P (insn))
782 {
783 if (DEBUG_INSN_P (insn))
784 {
785 rtx loc = INSN_VAR_LOCATION_LOC (insn);
786 if (!VAR_LOC_UNKNOWN_P (loc))
787 replace_oldest_value_addr (&INSN_VAR_LOCATION_LOC (insn),
788 ALL_REGS, GET_MODE (loc),
789 ADDR_SPACE_GENERIC, insn, vd);
790 }
791
792 if (insn == BB_END (bb))
793 break;
794 else
795 continue;
796 }
797
798 set = single_set (insn);
799 extract_constrain_insn (insn);
800 preprocess_constraints (insn);
801 const operand_alternative *op_alt = which_op_alt ();
802 n_ops = recog_data.n_operands;
803 is_asm = asm_noperands (PATTERN (insn)) >= 0;
804
805 /* Simplify the code below by promoting OP_OUT to OP_INOUT
806 in predicated instructions. */
807
808 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
809 for (i = 0; i < n_ops; ++i)
810 {
811 int matches = op_alt[i].matches;
812 if (matches >= 0 || op_alt[i].matched >= 0
813 || (predicated && recog_data.operand_type[i] == OP_OUT))
814 recog_data.operand_type[i] = OP_INOUT;
815 }
816
817 /* Apply changes to earlier DEBUG_INSNs if possible. */
818 if (vd->n_debug_insn_changes)
819 note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
820
821 /* For each earlyclobber operand, zap the value data. */
822 for (i = 0; i < n_ops; i++)
823 if (op_alt[i].earlyclobber)
824 kill_value (recog_data.operand[i], vd);
825
826 /* Within asms, a clobber cannot overlap inputs or outputs.
827 I wouldn't think this were true for regular insns, but
828 scan_rtx treats them like that... */
829 kill_clobbered_values (insn, vd);
830
831 /* Kill all auto-incremented values. */
832 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
833 kill_autoinc_value (insn, vd);
834
835 /* Kill all early-clobbered operands. */
836 for (i = 0; i < n_ops; i++)
837 if (op_alt[i].earlyclobber)
838 kill_value (recog_data.operand[i], vd);
839
840 /* If we have dead sets in the insn, then we need to note these as we
841 would clobbers. */
842 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
843 {
844 if (REG_NOTE_KIND (link) == REG_UNUSED)
845 {
846 kill_value (XEXP (link, 0), vd);
847 /* Furthermore, if the insn looked like a single-set,
848 but the dead store kills the source value of that
849 set, then we can no-longer use the plain move
850 special case below. */
851 if (set
852 && reg_overlap_mentioned_p (XEXP (link, 0), SET_SRC (set)))
853 set = NULL;
854 }
855 }
856
857 /* Special-case plain move instructions, since we may well
858 be able to do the move from a different register class. */
859 if (set && REG_P (SET_SRC (set)))
860 {
861 rtx src = SET_SRC (set);
862 unsigned int regno = REGNO (src);
863 machine_mode mode = GET_MODE (src);
864 unsigned int i;
865 rtx new_rtx;
866
867 /* If we are accessing SRC in some mode other that what we
868 set it in, make sure that the replacement is valid. */
869 if (mode != vd->e[regno].mode)
870 {
871 if (hard_regno_nregs[regno][mode]
872 > hard_regno_nregs[regno][vd->e[regno].mode])
873 goto no_move_special_case;
874
875 /* And likewise, if we are narrowing on big endian the transformation
876 is also invalid. */
877 if (hard_regno_nregs[regno][mode]
878 < hard_regno_nregs[regno][vd->e[regno].mode]
879 && (GET_MODE_SIZE (vd->e[regno].mode) > UNITS_PER_WORD
880 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
881 goto no_move_special_case;
882 }
883
884 /* If the destination is also a register, try to find a source
885 register in the same class. */
886 if (REG_P (SET_DEST (set)))
887 {
888 new_rtx = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
889 if (new_rtx && validate_change (insn, &SET_SRC (set), new_rtx, 0))
890 {
891 if (dump_file)
892 fprintf (dump_file,
893 "insn %u: replaced reg %u with %u\n",
894 INSN_UID (insn), regno, REGNO (new_rtx));
895 changed = true;
896 goto did_replacement;
897 }
898 /* We need to re-extract as validate_change clobbers
899 recog_data. */
900 extract_constrain_insn (insn);
901 preprocess_constraints (insn);
902 }
903
904 /* Otherwise, try all valid registers and see if its valid. */
905 for (i = vd->e[regno].oldest_regno; i != regno;
906 i = vd->e[i].next_regno)
907 {
908 new_rtx = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
909 mode, i, regno);
910 if (new_rtx != NULL_RTX)
911 {
912 if (validate_change (insn, &SET_SRC (set), new_rtx, 0))
913 {
914 ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (src);
915 REG_ATTRS (new_rtx) = REG_ATTRS (src);
916 REG_POINTER (new_rtx) = REG_POINTER (src);
917 if (dump_file)
918 fprintf (dump_file,
919 "insn %u: replaced reg %u with %u\n",
920 INSN_UID (insn), regno, REGNO (new_rtx));
921 changed = true;
922 goto did_replacement;
923 }
924 /* We need to re-extract as validate_change clobbers
925 recog_data. */
926 extract_constrain_insn (insn);
927 preprocess_constraints (insn);
928 }
929 }
930 }
931 no_move_special_case:
932
933 any_replacements = false;
934
935 /* For each input operand, replace a hard register with the
936 eldest live copy that's in an appropriate register class. */
937 for (i = 0; i < n_ops; i++)
938 {
939 replaced[i] = false;
940
941 /* Don't scan match_operand here, since we've no reg class
942 information to pass down. Any operands that we could
943 substitute in will be represented elsewhere. */
944 if (recog_data.constraints[i][0] == '\0')
945 continue;
946
947 /* Don't replace in asms intentionally referencing hard regs. */
948 if (is_asm && REG_P (recog_data.operand[i])
949 && (REGNO (recog_data.operand[i])
950 == ORIGINAL_REGNO (recog_data.operand[i])))
951 continue;
952
953 if (recog_data.operand_type[i] == OP_IN)
954 {
955 if (op_alt[i].is_address)
956 replaced[i]
957 = replace_oldest_value_addr (recog_data.operand_loc[i],
958 alternative_class (op_alt, i),
959 VOIDmode, ADDR_SPACE_GENERIC,
960 insn, vd);
961 else if (REG_P (recog_data.operand[i]))
962 replaced[i]
963 = replace_oldest_value_reg (recog_data.operand_loc[i],
964 alternative_class (op_alt, i),
965 insn, vd);
966 else if (MEM_P (recog_data.operand[i]))
967 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
968 insn, vd);
969 }
970 else if (MEM_P (recog_data.operand[i]))
971 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
972 insn, vd);
973
974 /* If we performed any replacement, update match_dups. */
975 if (replaced[i])
976 {
977 int j;
978 rtx new_rtx;
979
980 new_rtx = *recog_data.operand_loc[i];
981 recog_data.operand[i] = new_rtx;
982 for (j = 0; j < recog_data.n_dups; j++)
983 if (recog_data.dup_num[j] == i)
984 validate_unshare_change (insn, recog_data.dup_loc[j], new_rtx, 1);
985
986 any_replacements = true;
987 }
988 }
989
990 if (any_replacements)
991 {
992 if (! apply_change_group ())
993 {
994 for (i = 0; i < n_ops; i++)
995 if (replaced[i])
996 {
997 rtx old = *recog_data.operand_loc[i];
998 recog_data.operand[i] = old;
999 }
1000
1001 if (dump_file)
1002 fprintf (dump_file,
1003 "insn %u: reg replacements not verified\n",
1004 INSN_UID (insn));
1005 }
1006 else
1007 changed = true;
1008 }
1009
1010 did_replacement:
1011 if (changed)
1012 {
1013 anything_changed = true;
1014
1015 /* If something changed, perhaps further changes to earlier
1016 DEBUG_INSNs can be applied. */
1017 if (vd->n_debug_insn_changes)
1018 note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
1019 }
1020
1021 ksvd.vd = vd;
1022 ksvd.ignore_set_reg = NULL_RTX;
1023
1024 /* Clobber call-clobbered registers. */
1025 if (CALL_P (insn))
1026 {
1027 unsigned int set_regno = INVALID_REGNUM;
1028 unsigned int set_nregs = 0;
1029 unsigned int regno;
1030 rtx exp;
1031 HARD_REG_SET regs_invalidated_by_this_call;
1032
1033 for (exp = CALL_INSN_FUNCTION_USAGE (insn); exp; exp = XEXP (exp, 1))
1034 {
1035 rtx x = XEXP (exp, 0);
1036 if (GET_CODE (x) == SET)
1037 {
1038 rtx dest = SET_DEST (x);
1039 kill_value (dest, vd);
1040 set_value_regno (REGNO (dest), GET_MODE (dest), vd);
1041 copy_value (dest, SET_SRC (x), vd);
1042 ksvd.ignore_set_reg = dest;
1043 set_regno = REGNO (dest);
1044 set_nregs = REG_NREGS (dest);
1045 break;
1046 }
1047 }
1048
1049 get_call_reg_set_usage (insn,
1050 &regs_invalidated_by_this_call,
1051 regs_invalidated_by_call);
1052 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1053 if ((TEST_HARD_REG_BIT (regs_invalidated_by_this_call, regno)
1054 || HARD_REGNO_CALL_PART_CLOBBERED (regno, vd->e[regno].mode))
1055 && (regno < set_regno || regno >= set_regno + set_nregs))
1056 kill_value_regno (regno, 1, vd);
1057
1058 /* If SET was seen in CALL_INSN_FUNCTION_USAGE, and SET_SRC
1059 of the SET isn't in regs_invalidated_by_call hard reg set,
1060 but instead among CLOBBERs on the CALL_INSN, we could wrongly
1061 assume the value in it is still live. */
1062 if (ksvd.ignore_set_reg)
1063 kill_clobbered_values (insn, vd);
1064 }
1065
1066 bool copy_p = (set
1067 && REG_P (SET_DEST (set))
1068 && REG_P (SET_SRC (set)));
1069 bool noop_p = (copy_p
1070 && rtx_equal_p (SET_DEST (set), SET_SRC (set)));
1071
1072 if (!noop_p)
1073 {
1074 /* Notice stores. */
1075 note_stores (PATTERN (insn), kill_set_value, &ksvd);
1076
1077 /* Notice copies. */
1078 if (copy_p)
1079 copy_value (SET_DEST (set), SET_SRC (set), vd);
1080 }
1081
1082 if (insn == BB_END (bb))
1083 break;
1084 }
1085
1086 return anything_changed;
1087 }
1088
1089 /* Dump the value chain data to stderr. */
1090
1091 DEBUG_FUNCTION void
1092 debug_value_data (struct value_data *vd)
1093 {
1094 HARD_REG_SET set;
1095 unsigned int i, j;
1096
1097 CLEAR_HARD_REG_SET (set);
1098
1099 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1100 if (vd->e[i].oldest_regno == i)
1101 {
1102 if (vd->e[i].mode == VOIDmode)
1103 {
1104 if (vd->e[i].next_regno != INVALID_REGNUM)
1105 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1106 i, vd->e[i].next_regno);
1107 continue;
1108 }
1109
1110 SET_HARD_REG_BIT (set, i);
1111 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1112
1113 for (j = vd->e[i].next_regno;
1114 j != INVALID_REGNUM;
1115 j = vd->e[j].next_regno)
1116 {
1117 if (TEST_HARD_REG_BIT (set, j))
1118 {
1119 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1120 return;
1121 }
1122
1123 if (vd->e[j].oldest_regno != i)
1124 {
1125 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1126 j, vd->e[j].oldest_regno);
1127 return;
1128 }
1129 SET_HARD_REG_BIT (set, j);
1130 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1131 }
1132 fputc ('\n', stderr);
1133 }
1134
1135 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1136 if (! TEST_HARD_REG_BIT (set, i)
1137 && (vd->e[i].mode != VOIDmode
1138 || vd->e[i].oldest_regno != i
1139 || vd->e[i].next_regno != INVALID_REGNUM))
1140 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1141 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1142 vd->e[i].next_regno);
1143 }
1144
1145 /* Do copyprop_hardreg_forward_1 for a single basic block BB.
1146 DEBUG_INSN is skipped since we do not want to involve DF related
1147 staff as how it is handled in function pass_cprop_hardreg::execute.
1148
1149 NOTE: Currently it is only used for shrink-wrap. Maybe extend it
1150 to handle DEBUG_INSN for other uses. */
1151
1152 void
1153 copyprop_hardreg_forward_bb_without_debug_insn (basic_block bb)
1154 {
1155 struct value_data *vd;
1156 vd = XNEWVEC (struct value_data, 1);
1157 init_value_data (vd);
1158
1159 skip_debug_insn_p = true;
1160 copyprop_hardreg_forward_1 (bb, vd);
1161 free (vd);
1162 skip_debug_insn_p = false;
1163 }
1164
1165 #ifdef ENABLE_CHECKING
1166 static void
1167 validate_value_data (struct value_data *vd)
1168 {
1169 HARD_REG_SET set;
1170 unsigned int i, j;
1171
1172 CLEAR_HARD_REG_SET (set);
1173
1174 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1175 if (vd->e[i].oldest_regno == i)
1176 {
1177 if (vd->e[i].mode == VOIDmode)
1178 {
1179 if (vd->e[i].next_regno != INVALID_REGNUM)
1180 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1181 i, vd->e[i].next_regno);
1182 continue;
1183 }
1184
1185 SET_HARD_REG_BIT (set, i);
1186
1187 for (j = vd->e[i].next_regno;
1188 j != INVALID_REGNUM;
1189 j = vd->e[j].next_regno)
1190 {
1191 if (TEST_HARD_REG_BIT (set, j))
1192 internal_error ("validate_value_data: Loop in regno chain (%u)",
1193 j);
1194 if (vd->e[j].oldest_regno != i)
1195 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1196 j, vd->e[j].oldest_regno);
1197
1198 SET_HARD_REG_BIT (set, j);
1199 }
1200 }
1201
1202 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1203 if (! TEST_HARD_REG_BIT (set, i)
1204 && (vd->e[i].mode != VOIDmode
1205 || vd->e[i].oldest_regno != i
1206 || vd->e[i].next_regno != INVALID_REGNUM))
1207 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1208 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1209 vd->e[i].next_regno);
1210 }
1211 #endif
1212 \f
1213 namespace {
1214
1215 const pass_data pass_data_cprop_hardreg =
1216 {
1217 RTL_PASS, /* type */
1218 "cprop_hardreg", /* name */
1219 OPTGROUP_NONE, /* optinfo_flags */
1220 TV_CPROP_REGISTERS, /* tv_id */
1221 0, /* properties_required */
1222 0, /* properties_provided */
1223 0, /* properties_destroyed */
1224 0, /* todo_flags_start */
1225 TODO_df_finish, /* todo_flags_finish */
1226 };
1227
1228 class pass_cprop_hardreg : public rtl_opt_pass
1229 {
1230 public:
1231 pass_cprop_hardreg (gcc::context *ctxt)
1232 : rtl_opt_pass (pass_data_cprop_hardreg, ctxt)
1233 {}
1234
1235 /* opt_pass methods: */
1236 virtual bool gate (function *)
1237 {
1238 return (optimize > 0 && (flag_cprop_registers));
1239 }
1240
1241 virtual unsigned int execute (function *);
1242
1243 }; // class pass_cprop_hardreg
1244
1245 unsigned int
1246 pass_cprop_hardreg::execute (function *fun)
1247 {
1248 struct value_data *all_vd;
1249 basic_block bb;
1250 sbitmap visited;
1251 bool analyze_called = false;
1252
1253 all_vd = XNEWVEC (struct value_data, last_basic_block_for_fn (fun));
1254
1255 visited = sbitmap_alloc (last_basic_block_for_fn (fun));
1256 bitmap_clear (visited);
1257
1258 FOR_EACH_BB_FN (bb, fun)
1259 {
1260 bitmap_set_bit (visited, bb->index);
1261
1262 /* If a block has a single predecessor, that we've already
1263 processed, begin with the value data that was live at
1264 the end of the predecessor block. */
1265 /* ??? Ought to use more intelligent queuing of blocks. */
1266 if (single_pred_p (bb)
1267 && bitmap_bit_p (visited, single_pred (bb)->index)
1268 && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
1269 {
1270 all_vd[bb->index] = all_vd[single_pred (bb)->index];
1271 if (all_vd[bb->index].n_debug_insn_changes)
1272 {
1273 unsigned int regno;
1274
1275 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1276 {
1277 if (all_vd[bb->index].e[regno].debug_insn_changes)
1278 {
1279 all_vd[bb->index].e[regno].debug_insn_changes = NULL;
1280 if (--all_vd[bb->index].n_debug_insn_changes == 0)
1281 break;
1282 }
1283 }
1284 }
1285 }
1286 else
1287 init_value_data (all_vd + bb->index);
1288
1289 copyprop_hardreg_forward_1 (bb, all_vd + bb->index);
1290 }
1291
1292 if (MAY_HAVE_DEBUG_INSNS)
1293 {
1294 FOR_EACH_BB_FN (bb, fun)
1295 if (bitmap_bit_p (visited, bb->index)
1296 && all_vd[bb->index].n_debug_insn_changes)
1297 {
1298 unsigned int regno;
1299 bitmap live;
1300
1301 if (!analyze_called)
1302 {
1303 df_analyze ();
1304 analyze_called = true;
1305 }
1306 live = df_get_live_out (bb);
1307 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1308 if (all_vd[bb->index].e[regno].debug_insn_changes)
1309 {
1310 if (REGNO_REG_SET_P (live, regno))
1311 apply_debug_insn_changes (all_vd + bb->index, regno);
1312 if (all_vd[bb->index].n_debug_insn_changes == 0)
1313 break;
1314 }
1315 }
1316
1317 queued_debug_insn_change::pool.release ();
1318 }
1319
1320 sbitmap_free (visited);
1321 free (all_vd);
1322 return 0;
1323 }
1324
1325 } // anon namespace
1326
1327 rtl_opt_pass *
1328 make_pass_cprop_hardreg (gcc::context *ctxt)
1329 {
1330 return new pass_cprop_hardreg (ctxt);
1331 }