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