Debian Codegraph @

gcc/regcprop.cc

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