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 "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 271678 : free_debug_insn_changes (struct value_data *vd, unsigned int regno)
117 : {
118 271678 : struct queued_debug_insn_change *cur, *next;
119 737464 : for (cur = vd->e[regno].debug_insn_changes; cur; cur = next)
120 : {
121 465786 : next = cur->next;
122 465786 : --vd->n_debug_insn_changes;
123 465786 : queued_debug_insn_change_pool.remove (cur);
124 : }
125 271678 : vd->e[regno].debug_insn_changes = NULL;
126 271678 : }
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 85254952 : kill_value_one_regno (unsigned int regno, struct value_data *vd)
135 : {
136 85254952 : unsigned int i, next;
137 :
138 85254952 : if (vd->e[regno].oldest_regno != regno)
139 : {
140 121137 : for (i = vd->e[regno].oldest_regno;
141 3171084 : vd->e[i].next_regno != regno;
142 121137 : i = vd->e[i].next_regno)
143 121137 : continue;
144 3049947 : vd->e[i].next_regno = vd->e[regno].next_regno;
145 : }
146 82205005 : else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
147 : {
148 4706828 : for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
149 2420090 : vd->e[i].oldest_regno = next;
150 : }
151 :
152 85254952 : vd->e[regno].mode = VOIDmode;
153 85254952 : vd->e[regno].oldest_regno = regno;
154 85254952 : vd->e[regno].next_regno = INVALID_REGNUM;
155 85254952 : vd->e[regno].frame_related = false;
156 85254952 : if (vd->e[regno].debug_insn_changes)
157 107691 : free_debug_insn_changes (vd, regno);
158 :
159 85254952 : if (flag_checking)
160 85254695 : validate_value_data (vd);
161 85254952 : }
162 :
163 : /* Kill the value in register REGNO for NREGS, and any other registers
164 : whose values overlap. */
165 :
166 : static void
167 84971954 : kill_value_regno (unsigned int regno, unsigned int nregs,
168 : struct value_data *vd)
169 : {
170 84971954 : unsigned int j;
171 :
172 : /* Kill the value we're told to kill. */
173 170147642 : for (j = 0; j < nregs; ++j)
174 85175688 : kill_value_one_regno (regno + j, vd);
175 :
176 : /* Kill everything that overlapped what we're told to kill. */
177 84971954 : if (regno < vd->max_value_regs)
178 : j = 0;
179 : else
180 72960847 : j = regno - vd->max_value_regs;
181 153768874 : for (; j < regno; ++j)
182 : {
183 68796920 : unsigned int i, n;
184 68796920 : if (vd->e[j].mode == VOIDmode)
185 53586391 : continue;
186 15210529 : n = hard_regno_nregs (j, vd->e[j].mode);
187 15210529 : if (j + n > regno)
188 118896 : for (i = 0; i < n; ++i)
189 79264 : kill_value_one_regno (j + i, vd);
190 : }
191 84971954 : }
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 93440593 : kill_value (const_rtx x, struct value_data *vd)
198 : {
199 93440593 : 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 93440593 : if (REG_P (x))
206 69711068 : kill_value_regno (REGNO (x), REG_NREGS (x), vd);
207 93440593 : }
208 :
209 : /* Remember that REGNO is valid in MODE. */
210 :
211 : static void
212 54207435 : set_value_regno (unsigned int regno, machine_mode mode,
213 : struct value_data *vd)
214 : {
215 54207435 : unsigned int nregs;
216 :
217 54207435 : vd->e[regno].mode = mode;
218 :
219 51639629 : nregs = hard_regno_nregs (regno, mode);
220 54207435 : if (nregs > vd->max_value_regs)
221 4926801 : 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 5127980 : init_value_data (struct value_data *vd)
228 : {
229 5127980 : int i;
230 476902140 : for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
231 : {
232 471774160 : vd->e[i].mode = VOIDmode;
233 471774160 : vd->e[i].oldest_regno = i;
234 471774160 : vd->e[i].next_regno = INVALID_REGNUM;
235 471774160 : vd->e[i].debug_insn_changes = NULL;
236 471774160 : vd->e[i].frame_related = false;
237 : }
238 5127980 : vd->max_value_regs = 0;
239 5127980 : vd->n_debug_insn_changes = 0;
240 5127980 : }
241 :
242 : /* Called through note_stores. If X is clobbered, kill its value. */
243 :
244 : static void
245 79775886 : kill_clobbered_value (rtx x, const_rtx set, void *data)
246 : {
247 79775886 : struct value_data *const vd = (struct value_data *) data;
248 :
249 79775886 : if (GET_CODE (set) == CLOBBER)
250 9996801 : kill_value (x, vd);
251 79775886 : }
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 79646293 : kill_set_value (rtx x, const_rtx set, void *data)
266 : {
267 79646293 : struct kill_set_value_data *ksvd = (struct kill_set_value_data *) data;
268 79646293 : if (rtx_equal_p (x, ksvd->ignore_set_reg))
269 : return;
270 :
271 79549497 : if (GET_CODE (set) != CLOBBER)
272 : {
273 69552696 : kill_value (x, ksvd->vd);
274 69552696 : if (REG_P (x))
275 : {
276 47104447 : set_value_regno (REGNO (x), GET_MODE (x), ksvd->vd);
277 47104447 : 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 74337494 : kill_autoinc_value (rtx_insn *insn, struct value_data *vd)
287 : {
288 74337494 : subrtx_iterator::array_type array;
289 480716263 : FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
290 : {
291 406378769 : const_rtx x = *iter;
292 406378769 : if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
293 : {
294 4438386 : x = XEXP (x, 0);
295 4438386 : kill_value (x, vd);
296 4438386 : set_value_regno (REGNO (x), GET_MODE (x), vd);
297 4438386 : vd->e[REGNO(x)].frame_related = RTX_FRAME_RELATED_P (insn);
298 4438386 : iter.skip_subrtxes ();
299 : }
300 : }
301 74337494 : }
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 6358120 : copy_value (rtx dest, rtx src, struct value_data *vd, bool frame_related)
308 : {
309 6358120 : unsigned int dr = REGNO (dest);
310 6358120 : unsigned int sr = REGNO (src);
311 6358120 : unsigned int dn, sn;
312 6358120 : 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 6358120 : 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 6358120 : if (dr == STACK_POINTER_REGNUM)
322 : return;
323 :
324 : /* Likewise with the frame pointer, if we're using one. */
325 6354550 : 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 6305770 : if (fixed_regs[dr] || global_regs[dr])
332 : return;
333 :
334 : /* If SRC and DEST overlap, don't record anything. */
335 6305753 : dn = REG_NREGS (dest);
336 6305753 : sn = REG_NREGS (src);
337 6305753 : if ((dr > sr && dr < sr + sn)
338 6305753 : || (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 6305753 : if (vd->e[sr].mode == VOIDmode)
345 2549857 : set_value_regno (sr, vd->e[dr].mode, vd);
346 :
347 3755896 : else if (!ordered_p (GET_MODE_PRECISION (vd->e[sr].mode),
348 3755896 : 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 3755896 : else if (sn < hard_regno_nregs (sr, vd->e[sr].mode)
367 3755896 : && 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 3755896 : 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 3755896 : else if (partial_subreg_p (vd->e[sr].mode, GET_MODE (src)))
391 : {
392 17969 : if (!REG_CAN_CHANGE_MODE_P (sr, GET_MODE (src), vd->e[sr].mode)
393 17969 : || !REG_CAN_CHANGE_MODE_P (dr, vd->e[sr].mode, GET_MODE (dest)))
394 20 : return;
395 17949 : 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 6305733 : vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
401 :
402 6648616 : for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
403 342883 : continue;
404 6305733 : vd->e[i].next_regno = dr;
405 :
406 6305733 : vd->e[dr].frame_related = frame_related;
407 :
408 6305733 : if (flag_checking)
409 6305704 : validate_value_data (vd);
410 342883 : }
411 :
412 : /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
413 :
414 : static bool
415 297498 : mode_change_ok (machine_mode orig_mode, machine_mode new_mode,
416 : unsigned int regno ATTRIBUTE_UNUSED)
417 : {
418 297498 : if (partial_subreg_p (orig_mode, new_mode))
419 : return false;
420 :
421 291294 : 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 1759737 : 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 1759737 : if (!ordered_p (GET_MODE_PRECISION (orig_mode),
437 1759737 : GET_MODE_PRECISION (copy_mode))
438 1759737 : || !ordered_p (GET_MODE_PRECISION (copy_mode),
439 1759737 : GET_MODE_PRECISION (new_mode)))
440 : return NULL_RTX;
441 :
442 1759737 : if (partial_subreg_p (copy_mode, orig_mode)
443 1759737 : && 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 1758617 : if (regno == STACK_POINTER_REGNUM)
451 : {
452 25286 : if (orig_mode == new_mode && new_mode == GET_MODE (stack_pointer_rtx))
453 : return stack_pointer_rtx;
454 : else
455 : return NULL_RTX;
456 : }
457 :
458 1733331 : if (orig_mode == new_mode)
459 1580227 : return gen_raw_REG (new_mode, regno);
460 153104 : else if (mode_change_ok (orig_mode, new_mode, regno)
461 153104 : && mode_change_ok (copy_mode, new_mode, copy_regno))
462 : {
463 144394 : int copy_nregs = hard_regno_nregs (copy_regno, copy_mode);
464 144394 : int use_nregs = hard_regno_nregs (copy_regno, new_mode);
465 144394 : poly_uint64 bytes_per_reg;
466 288788 : if (!can_div_trunc_p (GET_MODE_SIZE (copy_mode),
467 : copy_nregs, &bytes_per_reg))
468 144280 : return NULL_RTX;
469 144394 : poly_uint64 copy_offset = bytes_per_reg * (copy_nregs - use_nregs);
470 144394 : poly_uint64 offset
471 144394 : = subreg_size_lowpart_offset (GET_MODE_SIZE (new_mode) + copy_offset,
472 144394 : GET_MODE_SIZE (orig_mode));
473 144394 : regno += subreg_regno_offset (regno, orig_mode, offset, new_mode);
474 144394 : if (targetm.hard_regno_mode_ok (regno, new_mode))
475 144280 : 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 1759737 : incompatible_frame_status (bool to_frame_related, bool from_frame_related)
492 : {
493 1759737 : 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 82422323 : find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd, bool frame_related)
502 : {
503 82422323 : unsigned int regno = REGNO (reg);
504 82422323 : machine_mode mode = GET_MODE (reg);
505 82422323 : unsigned int i;
506 :
507 82422323 : 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 82422323 : if (mode != vd->e[regno].mode
517 82422323 : && (REG_NREGS (reg) > hard_regno_nregs (regno, vd->e[regno].mode)
518 4469780 : || !REG_CAN_CHANGE_MODE_P (regno, mode, vd->e[regno].mode)))
519 36575249 : return NULL_RTX;
520 :
521 46466059 : for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
522 : {
523 2070974 : machine_mode oldmode = vd->e[i].mode;
524 2070974 : rtx new_rtx;
525 :
526 2070974 : if (!in_hard_reg_set_p (reg_class_contents[cl], mode, i))
527 610878 : continue;
528 :
529 1460096 : if (incompatible_frame_status (frame_related, vd->e[i].frame_related))
530 0 : continue;
531 :
532 1460096 : new_rtx = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
533 1460096 : 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 1451989 : if (new_rtx != stack_pointer_rtx)
538 : {
539 1443710 : ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (reg);
540 1443710 : REG_ATTRS (new_rtx) = REG_ATTRS (reg);
541 1443710 : REG_POINTER (new_rtx) = REG_POINTER (reg);
542 : }
543 1451989 : 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 65610614 : replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx_insn *insn,
555 : struct value_data *vd)
556 : {
557 65618894 : rtx new_rtx = find_oldest_value_reg (cl, *loc, vd, RTX_FRAME_RELATED_P (insn));
558 65610614 : if (new_rtx && (!DEBUG_INSN_P (insn) || !skip_debug_insn_p))
559 : {
560 1141842 : if (DEBUG_INSN_P (insn))
561 : {
562 600211 : struct queued_debug_insn_change *change;
563 :
564 600211 : 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 600211 : change = queued_debug_insn_change_pool.allocate ();
569 600211 : change->next = vd->e[REGNO (new_rtx)].debug_insn_changes;
570 600211 : change->insn = insn;
571 600211 : change->loc = loc;
572 600211 : change->new_rtx = new_rtx;
573 600211 : vd->e[REGNO (new_rtx)].debug_insn_changes = change;
574 600211 : ++vd->n_debug_insn_changes;
575 600211 : return true;
576 : }
577 541631 : if (dump_file)
578 29 : fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
579 29 : INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
580 :
581 541631 : validate_change (insn, loc, new_rtx, 1);
582 541631 : 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 100379226 : 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 100379226 : rtx x = *loc;
597 100379226 : RTX_CODE code = GET_CODE (x);
598 100379226 : const char *fmt;
599 100379226 : int i, j;
600 100379226 : bool changed = false;
601 :
602 100379226 : switch (code)
603 : {
604 28617356 : case PLUS:
605 28617356 : if (DEBUG_INSN_P (insn))
606 : break;
607 :
608 22327552 : {
609 22327552 : rtx orig_op0 = XEXP (x, 0);
610 22327552 : rtx orig_op1 = XEXP (x, 1);
611 22327552 : RTX_CODE code0 = GET_CODE (orig_op0);
612 22327552 : RTX_CODE code1 = GET_CODE (orig_op1);
613 22327552 : rtx op0 = orig_op0;
614 22327552 : rtx op1 = orig_op1;
615 22327552 : rtx *locI = NULL;
616 22327552 : rtx *locB = NULL;
617 22327552 : enum rtx_code index_code = SCRATCH;
618 :
619 22327552 : if (GET_CODE (op0) == SUBREG)
620 : {
621 0 : op0 = SUBREG_REG (op0);
622 0 : code0 = GET_CODE (op0);
623 : }
624 :
625 22327552 : if (GET_CODE (op1) == SUBREG)
626 : {
627 0 : op1 = SUBREG_REG (op1);
628 0 : code1 = GET_CODE (op1);
629 : }
630 :
631 22327552 : if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
632 21821190 : || code0 == ZERO_EXTEND || code1 == MEM)
633 : {
634 506362 : locI = &XEXP (x, 0);
635 506362 : locB = &XEXP (x, 1);
636 506362 : index_code = GET_CODE (*locI);
637 : }
638 21821190 : else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
639 21819773 : || code1 == ZERO_EXTEND || code0 == MEM)
640 : {
641 1417 : locI = &XEXP (x, 1);
642 1417 : locB = &XEXP (x, 0);
643 1417 : index_code = GET_CODE (*locI);
644 : }
645 21819773 : else if (code0 == CONST_INT || code0 == CONST
646 21819773 : || code0 == SYMBOL_REF || code0 == LABEL_REF)
647 : {
648 750542 : locB = &XEXP (x, 1);
649 750542 : index_code = GET_CODE (XEXP (x, 0));
650 : }
651 21069231 : else if (code1 == CONST_INT || code1 == CONST
652 : || code1 == SYMBOL_REF || code1 == LABEL_REF)
653 : {
654 20014708 : locB = &XEXP (x, 0);
655 20014708 : index_code = GET_CODE (XEXP (x, 1));
656 : }
657 1054523 : else if (code0 == REG && code1 == REG)
658 : {
659 858913 : int index_op;
660 858913 : unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
661 :
662 356 : if (REGNO_OK_FOR_INDEX_P (regno1)
663 858913 : && 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 356 : && 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 858557 : index_op = 1;
675 :
676 858913 : locI = &XEXP (x, index_op);
677 858913 : locB = &XEXP (x, !index_op);
678 858913 : index_code = GET_CODE (*locI);
679 : }
680 195610 : 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 195610 : else if (code1 == REG)
687 : {
688 195610 : locI = &XEXP (x, 1);
689 195610 : locB = &XEXP (x, 0);
690 195610 : index_code = GET_CODE (*locI);
691 : }
692 :
693 22327552 : if (locI)
694 1562302 : changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS,
695 : mode, as, insn, vd);
696 22327552 : if (locB)
697 22327552 : changed |= replace_oldest_value_addr (locB,
698 : base_reg_class (mode, as, PLUS,
699 : index_code),
700 : mode, as, insn, vd);
701 : return changed;
702 : }
703 :
704 : case POST_INC:
705 : case POST_DEC:
706 : case POST_MODIFY:
707 : case PRE_INC:
708 : case PRE_DEC:
709 : case PRE_MODIFY:
710 : return false;
711 :
712 1971620 : case MEM:
713 1971620 : return replace_oldest_value_mem (x, insn, vd);
714 :
715 38077565 : case REG:
716 38077565 : return replace_oldest_value_reg (loc, cl, insn, vd);
717 :
718 : default:
719 : break;
720 : }
721 :
722 33595823 : fmt = GET_RTX_FORMAT (code);
723 78532415 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
724 : {
725 44936592 : if (fmt[i] == 'e')
726 17178168 : changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode, as,
727 : insn, vd);
728 27758424 : else if (fmt[i] == 'E')
729 387276 : for (j = XVECLEN (x, i) - 1; j >= 0; j--)
730 234924 : changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
731 : mode, as, insn, vd);
732 : }
733 :
734 : return changed;
735 : }
736 :
737 : /* Similar to replace_oldest_value_reg, but X contains a memory. */
738 :
739 : static bool
740 30689053 : replace_oldest_value_mem (rtx x, rtx_insn *insn, struct value_data *vd)
741 : {
742 30689053 : enum reg_class cl;
743 :
744 30689053 : if (DEBUG_INSN_P (insn))
745 : cl = ALL_REGS;
746 : else
747 28717433 : cl = base_reg_class (GET_MODE (x), MEM_ADDR_SPACE (x), MEM, SCRATCH);
748 :
749 30689053 : return replace_oldest_value_addr (&XEXP (x, 0), cl,
750 30689053 : GET_MODE (x), MEM_ADDR_SPACE (x),
751 30689053 : insn, vd);
752 : }
753 :
754 : /* Apply all queued updates for DEBUG_INSNs that change some reg to
755 : register REGNO. */
756 :
757 : static void
758 188448 : apply_debug_insn_changes (struct value_data *vd, unsigned int regno)
759 : {
760 188448 : struct queued_debug_insn_change *change;
761 188448 : rtx_insn *last_insn = vd->e[regno].debug_insn_changes->insn;
762 :
763 188448 : for (change = vd->e[regno].debug_insn_changes;
764 538260 : change;
765 349812 : change = change->next)
766 : {
767 349812 : if (last_insn != change->insn)
768 : {
769 156696 : apply_change_group ();
770 156696 : last_insn = change->insn;
771 : }
772 349812 : validate_change (change->insn, change->loc, change->new_rtx, 1);
773 : }
774 188448 : apply_change_group ();
775 188448 : }
776 :
777 : /* Called via note_uses, for all used registers in a real insn
778 : apply DEBUG_INSN changes that change registers to the used
779 : registers. */
780 :
781 : static void
782 974662 : cprop_find_used_regs (rtx *loc, void *data)
783 : {
784 974662 : struct value_data *const vd = (struct value_data *) data;
785 974662 : subrtx_iterator::array_type array;
786 3575870 : FOR_EACH_SUBRTX (iter, array, *loc, NONCONST)
787 : {
788 2601208 : const_rtx x = *iter;
789 2601208 : if (REG_P (x))
790 : {
791 888455 : unsigned int regno = REGNO (x);
792 888455 : if (vd->e[regno].debug_insn_changes)
793 : {
794 163987 : apply_debug_insn_changes (vd, regno);
795 163987 : free_debug_insn_changes (vd, regno);
796 : }
797 : }
798 : }
799 974662 : }
800 :
801 : /* Apply clobbers of INSN in PATTERN and C_I_F_U to value_data VD. */
802 :
803 : static void
804 74434290 : kill_clobbered_values (rtx_insn *insn, struct value_data *vd)
805 : {
806 0 : note_stores (insn, kill_clobbered_value, vd);
807 96796 : }
808 :
809 : /* Perform the forward copy propagation on basic block BB. */
810 :
811 : static bool
812 12392376 : copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
813 : {
814 12392376 : bool anything_changed = false;
815 12392376 : rtx_insn *insn, *next;
816 :
817 12392376 : for (insn = BB_HEAD (bb); ; insn = next)
818 : {
819 161277725 : int n_ops, i, predicated;
820 161277725 : bool is_asm, any_replacements;
821 161277725 : rtx set;
822 161277725 : rtx link;
823 161277725 : bool changed = false;
824 161277725 : struct kill_set_value_data ksvd;
825 :
826 161277725 : next = NEXT_INSN (insn);
827 161277725 : if (!NONDEBUG_INSN_P (insn))
828 : {
829 86753302 : if (DEBUG_BIND_INSN_P (insn))
830 : {
831 44903316 : rtx loc = INSN_VAR_LOCATION_LOC (insn);
832 44903316 : if (!VAR_LOC_UNKNOWN_P (loc))
833 25095442 : replace_oldest_value_addr (&INSN_VAR_LOCATION_LOC (insn),
834 25095442 : ALL_REGS, GET_MODE (loc),
835 : ADDR_SPACE_GENERIC, insn, vd);
836 : }
837 :
838 86753302 : if (insn == BB_END (bb))
839 : break;
840 : else
841 86496877 : continue;
842 : }
843 :
844 74524423 : set = single_set (insn);
845 :
846 : /* Detect noop sets and remove them before processing side effects. */
847 74524423 : if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
848 : {
849 6480985 : unsigned int regno = REGNO (SET_SRC (set));
850 6480985 : rtx r1 = find_oldest_value_reg (REGNO_REG_CLASS (regno),
851 : SET_DEST (set), vd, false);
852 6480985 : rtx r2 = find_oldest_value_reg (REGNO_REG_CLASS (regno),
853 : SET_SRC (set), vd, false);
854 6480985 : if (rtx_equal_p (r1 ? r1 : SET_DEST (set), r2 ? r2 : SET_SRC (set)))
855 : {
856 63822 : bool last = insn == BB_END (bb);
857 63822 : delete_insn (insn);
858 63822 : if (last)
859 : break;
860 61706 : continue;
861 61706 : }
862 : }
863 :
864 : /* Detect obviously dead sets (via REG_UNUSED notes) and remove them. */
865 : if (set
866 68391611 : && !RTX_FRAME_RELATED_P (insn)
867 64415185 : && NONJUMP_INSN_P (insn)
868 54147852 : && !may_trap_p (set)
869 47138175 : && find_reg_note (insn, REG_UNUSED, SET_DEST (set))
870 248758 : && !side_effects_p (SET_SRC (set))
871 123107 : && !side_effects_p (SET_DEST (set)))
872 : {
873 123107 : bool last = insn == BB_END (bb);
874 123107 : delete_insn_and_edges (insn);
875 123107 : if (last)
876 : break;
877 123102 : continue;
878 123102 : }
879 :
880 :
881 74337494 : extract_constrain_insn (insn);
882 74337494 : preprocess_constraints (insn);
883 74337494 : const operand_alternative *op_alt = which_op_alt ();
884 74337494 : n_ops = recog_data.n_operands;
885 74337494 : is_asm = asm_noperands (PATTERN (insn)) >= 0;
886 :
887 : /* Simplify the code below by promoting OP_OUT to OP_INOUT
888 : in predicated instructions. */
889 :
890 74337494 : predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
891 229933595 : for (i = 0; i < n_ops; ++i)
892 : {
893 155596101 : int matches = op_alt[i].matches;
894 155596101 : if (matches >= 0 || op_alt[i].matched >= 0
895 137842552 : || (predicated && recog_data.operand_type[i] == OP_OUT))
896 17753549 : recog_data.operand_type[i] = OP_INOUT;
897 : }
898 :
899 : /* Apply changes to earlier DEBUG_INSNs if possible. */
900 74337494 : if (vd->n_debug_insn_changes)
901 716341 : note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
902 :
903 : /* For each earlyclobber operand, zap the value data. */
904 229933595 : for (i = 0; i < n_ops; i++)
905 155596101 : if (op_alt[i].earlyclobber)
906 12652 : kill_value (recog_data.operand[i], vd);
907 :
908 : /* Within asms, a clobber cannot overlap inputs or outputs.
909 : I wouldn't think this were true for regular insns, but
910 : scan_rtx treats them like that... */
911 74337494 : kill_clobbered_values (insn, vd);
912 :
913 : /* Kill all auto-incremented values. */
914 : /* ??? REG_INC is useless, since stack pushes aren't done that way. */
915 74337494 : kill_autoinc_value (insn, vd);
916 :
917 : /* Kill all early-clobbered operands. */
918 304271089 : for (i = 0; i < n_ops; i++)
919 155596101 : if (op_alt[i].earlyclobber)
920 12652 : kill_value (recog_data.operand[i], vd);
921 :
922 : /* If we have dead sets in the insn, then we need to note these as we
923 : would clobbers. */
924 143807906 : for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
925 : {
926 69470435 : if (REG_NOTE_KIND (link) == REG_UNUSED)
927 : {
928 9330610 : kill_value (XEXP (link, 0), vd);
929 : /* Furthermore, if the insn looked like a single-set,
930 : but the dead store kills the source value of that
931 : set, then we can no-longer use the plain move
932 : special case below. */
933 9330610 : if (set
934 9330610 : && reg_overlap_mentioned_p (XEXP (link, 0), SET_SRC (set)))
935 : set = NULL;
936 : }
937 :
938 : /* We need to keep CFI info correct, and the same on all paths,
939 : so we cannot normally replace the registers REG_CFA_REGISTER
940 : refers to. Bail. */
941 69470435 : if (REG_NOTE_KIND (link) == REG_CFA_REGISTER)
942 23 : goto did_replacement;
943 : }
944 :
945 : /* Special-case plain move instructions, since we may well
946 : be able to do the move from a different register class. */
947 74337471 : if (set && REG_P (SET_SRC (set)))
948 : {
949 16667042 : rtx src = SET_SRC (set);
950 16667042 : rtx dest = SET_DEST (set);
951 16667042 : unsigned int regno = REGNO (src);
952 16667042 : machine_mode mode = GET_MODE (src);
953 16667042 : unsigned int i;
954 16667042 : rtx new_rtx;
955 :
956 : /* If we are accessing SRC in some mode other that what we
957 : set it in, make sure that the replacement is valid. */
958 16667042 : if (mode != vd->e[regno].mode)
959 : {
960 6648926 : if (REG_NREGS (src)
961 6648926 : > hard_regno_nregs (regno, vd->e[regno].mode))
962 5282485 : goto no_move_special_case;
963 :
964 : /* And likewise, if we are narrowing on big endian the transformation
965 : is also invalid. */
966 1366441 : if (REG_NREGS (src) < hard_regno_nregs (regno, vd->e[regno].mode)
967 1366441 : && maybe_ne (subreg_lowpart_offset (mode,
968 : vd->e[regno].mode), 0U))
969 0 : goto no_move_special_case;
970 : }
971 :
972 : /* If the destination is also a register, try to find a source
973 : register in the same class. */
974 11384557 : if (REG_P (dest))
975 : {
976 11549217 : new_rtx = find_oldest_value_reg (REGNO_REG_CLASS (regno),
977 3849739 : src, vd, RTX_FRAME_RELATED_P (insn));
978 :
979 3849739 : if (new_rtx && validate_change (insn, &SET_SRC (set), new_rtx, 0))
980 : {
981 73087 : if (dump_file)
982 2 : fprintf (dump_file,
983 : "insn %u: replaced reg %u with %u\n",
984 2 : INSN_UID (insn), regno, REGNO (new_rtx));
985 73087 : changed = true;
986 73087 : goto did_replacement;
987 : }
988 : /* We need to re-extract as validate_change clobbers
989 : recog_data. */
990 3776652 : extract_constrain_insn (insn);
991 3776652 : preprocess_constraints (insn);
992 : }
993 :
994 : /* Otherwise, try all valid registers and see if its valid. */
995 11329492 : for (i = vd->e[regno].oldest_regno; i != regno;
996 18022 : i = vd->e[i].next_regno)
997 : {
998 299641 : if (incompatible_frame_status (RTX_FRAME_RELATED_P (insn), vd->e[i].frame_related))
999 0 : continue;
1000 299641 : new_rtx = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
1001 : mode, i, regno);
1002 299641 : if (new_rtx != NULL_RTX)
1003 : {
1004 : /* Don't propagate for a more expensive reg-reg move. */
1005 297605 : if (REG_P (dest))
1006 : {
1007 143871 : enum reg_class from = REGNO_REG_CLASS (regno);
1008 143871 : enum reg_class to = REGNO_REG_CLASS (REGNO (dest));
1009 143871 : enum reg_class new_from = REGNO_REG_CLASS (i);
1010 143871 : unsigned int original_cost
1011 143871 : = targetm.register_move_cost (mode, from, to);
1012 143871 : unsigned int after_cost
1013 143871 : = targetm.register_move_cost (mode, new_from, to);
1014 143871 : if (after_cost > original_cost)
1015 9598 : continue;
1016 : }
1017 :
1018 288007 : if (validate_change (insn, &SET_SRC (set), new_rtx, 0))
1019 : {
1020 : /* NEW_RTX may be the global stack pointer rtx, in which
1021 : case we must not modify it's attributes. */
1022 281619 : if (new_rtx != stack_pointer_rtx)
1023 : {
1024 264815 : ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (src);
1025 264815 : REG_ATTRS (new_rtx) = REG_ATTRS (src);
1026 264815 : REG_POINTER (new_rtx) = REG_POINTER (src);
1027 : }
1028 281619 : if (dump_file)
1029 1 : fprintf (dump_file,
1030 : "insn %u: replaced reg %u with %u\n",
1031 1 : INSN_UID (insn), regno, REGNO (new_rtx));
1032 281619 : changed = true;
1033 281619 : goto did_replacement;
1034 : }
1035 : /* We need to re-extract as validate_change clobbers
1036 : recog_data. */
1037 6388 : extract_constrain_insn (insn);
1038 6388 : preprocess_constraints (insn);
1039 : }
1040 : }
1041 : }
1042 57670429 : no_move_special_case:
1043 :
1044 : any_replacements = false;
1045 :
1046 : /* For each input operand, replace a hard register with the
1047 : eldest live copy that's in an appropriate register class. */
1048 228869419 : for (i = 0; i < n_ops; i++)
1049 : {
1050 154886654 : bool replaced = false;
1051 :
1052 : /* Don't scan match_operand here, since we've no reg class
1053 : information to pass down. Any operands that we could
1054 : substitute in will be represented elsewhere. */
1055 154886654 : if (recog_data.constraints[i][0] == '\0')
1056 27899153 : continue;
1057 :
1058 : /* Don't replace in asms intentionally referencing hard regs. */
1059 161416 : if (is_asm && REG_P (recog_data.operand[i])
1060 127125027 : && (REGNO (recog_data.operand[i])
1061 137526 : == ORIGINAL_REGNO (recog_data.operand[i])))
1062 2015 : continue;
1063 :
1064 126985486 : if (recog_data.operand_type[i] == OP_IN)
1065 : {
1066 65652707 : if (op_alt[i].is_address)
1067 3291785 : replaced
1068 3291785 : = replace_oldest_value_addr (recog_data.operand_loc[i],
1069 : alternative_class (op_alt, i),
1070 : VOIDmode, ADDR_SPACE_GENERIC,
1071 : insn, vd);
1072 62360922 : else if (REG_P (recog_data.operand[i]))
1073 27533049 : replaced
1074 27533049 : = replace_oldest_value_reg (recog_data.operand_loc[i],
1075 : alternative_class (op_alt, i),
1076 : insn, vd);
1077 34827873 : else if (MEM_P (recog_data.operand[i]))
1078 14403684 : replaced = replace_oldest_value_mem (recog_data.operand[i],
1079 : insn, vd);
1080 : }
1081 61332779 : else if (MEM_P (recog_data.operand[i]))
1082 14313749 : replaced = replace_oldest_value_mem (recog_data.operand[i],
1083 : insn, vd);
1084 :
1085 : /* If we performed any replacement, update match_dups. */
1086 59542267 : if (replaced)
1087 : {
1088 540057 : int j;
1089 540057 : rtx new_rtx;
1090 :
1091 540057 : new_rtx = *recog_data.operand_loc[i];
1092 540057 : recog_data.operand[i] = new_rtx;
1093 565323 : for (j = 0; j < recog_data.n_dups; j++)
1094 25266 : if (recog_data.dup_num[j] == i)
1095 8538 : validate_unshare_change (insn, recog_data.dup_loc[j], new_rtx, 1);
1096 :
1097 : any_replacements = true;
1098 : }
1099 : }
1100 :
1101 73982765 : if (any_replacements)
1102 : {
1103 536908 : if (! apply_change_group ())
1104 : {
1105 387 : if (dump_file)
1106 0 : fprintf (dump_file,
1107 : "insn %u: reg replacements not verified\n",
1108 0 : INSN_UID (insn));
1109 : }
1110 : else
1111 : changed = true;
1112 : }
1113 :
1114 354729 : did_replacement:
1115 354729 : if (changed)
1116 : {
1117 891227 : anything_changed = true;
1118 :
1119 : /* If something changed, perhaps further changes to earlier
1120 : DEBUG_INSNs can be applied. */
1121 891227 : if (vd->n_debug_insn_changes)
1122 126037 : note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
1123 891227 : df_insn_rescan (insn);
1124 : }
1125 :
1126 74337494 : ksvd.vd = vd;
1127 74337494 : ksvd.ignore_set_reg = NULL_RTX;
1128 74337494 : ksvd.insn_is_frame_related = false;
1129 :
1130 : /* Clobber call-clobbered registers. */
1131 74337494 : if (CALL_P (insn))
1132 : {
1133 5597795 : unsigned int set_regno = INVALID_REGNUM;
1134 5597795 : unsigned int set_nregs = 0;
1135 5597795 : unsigned int regno;
1136 5597795 : rtx exp;
1137 :
1138 16029535 : for (exp = CALL_INSN_FUNCTION_USAGE (insn); exp; exp = XEXP (exp, 1))
1139 : {
1140 10528536 : rtx x = XEXP (exp, 0);
1141 10528536 : if (GET_CODE (x) == SET)
1142 : {
1143 96796 : rtx dest = SET_DEST (x);
1144 96796 : kill_value (dest, vd);
1145 96796 : set_value_regno (REGNO (dest), GET_MODE (dest), vd);
1146 96796 : copy_value (dest, SET_SRC (x), vd, false);
1147 96796 : ksvd.ignore_set_reg = dest;
1148 96796 : set_regno = REGNO (dest);
1149 96796 : set_nregs = REG_NREGS (dest);
1150 96796 : break;
1151 : }
1152 : }
1153 :
1154 5597795 : function_abi callee_abi = insn_callee_abi (insn);
1155 526192730 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1156 514997140 : if (vd->e[regno].mode != VOIDmode
1157 25219113 : && callee_abi.clobbers_reg_p (vd->e[regno].mode, regno)
1158 530354822 : && (regno < set_regno || regno >= set_regno + set_nregs))
1159 15260886 : kill_value_regno (regno, 1, vd);
1160 :
1161 : /* If SET was seen in CALL_INSN_FUNCTION_USAGE, and SET_SRC
1162 : of the SET isn't clobbered by CALLEE_ABI, but instead among
1163 : CLOBBERs on the CALL_INSN, we could wrongly assume the
1164 : value in it is still live. */
1165 5597795 : if (ksvd.ignore_set_reg)
1166 96796 : kill_clobbered_values (insn, vd);
1167 : }
1168 :
1169 154969087 : bool copy_p = (set
1170 67944604 : && REG_P (SET_DEST (set))
1171 119916180 : && REG_P (SET_SRC (set)));
1172 6294121 : bool noop_p = (copy_p
1173 6294121 : && rtx_equal_p (SET_DEST (set), SET_SRC (set)));
1174 :
1175 : /* If a noop move is using narrower mode than we have recorded,
1176 : we need to either remove the noop move, or kill_set_value. */
1177 32797 : if (noop_p
1178 32797 : && partial_subreg_p (GET_MODE (SET_DEST (set)),
1179 32797 : vd->e[REGNO (SET_DEST (set))].mode))
1180 : {
1181 1962 : if (noop_move_p (insn))
1182 : {
1183 1962 : bool last = insn == BB_END (bb);
1184 1962 : delete_insn (insn);
1185 1962 : if (last)
1186 : break;
1187 : }
1188 : else
1189 : noop_p = false;
1190 : }
1191 :
1192 74337472 : if (!noop_p)
1193 : {
1194 74304697 : ksvd.insn_is_frame_related = RTX_FRAME_RELATED_P (insn);
1195 :
1196 : /* Notice stores. */
1197 74304697 : note_stores (insn, kill_set_value, &ksvd);
1198 :
1199 : /* Notice copies. */
1200 74304697 : if (copy_p)
1201 : {
1202 6261324 : df_insn_rescan (insn);
1203 6261324 : copy_value (SET_DEST (set), SET_SRC (set), vd, RTX_FRAME_RELATED_P (insn));
1204 : }
1205 : }
1206 :
1207 74337472 : if (insn == BB_END (bb))
1208 : break;
1209 : }
1210 :
1211 12392376 : return anything_changed;
1212 : }
1213 :
1214 : /* Dump the value chain data to stderr. */
1215 :
1216 : DEBUG_FUNCTION void
1217 0 : debug_value_data (struct value_data *vd)
1218 : {
1219 0 : HARD_REG_SET set;
1220 0 : unsigned int i, j;
1221 :
1222 0 : CLEAR_HARD_REG_SET (set);
1223 :
1224 0 : for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1225 0 : if (vd->e[i].oldest_regno == i)
1226 : {
1227 0 : if (vd->e[i].mode == VOIDmode)
1228 : {
1229 0 : if (vd->e[i].next_regno != INVALID_REGNUM)
1230 0 : fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1231 : i, vd->e[i].next_regno);
1232 0 : continue;
1233 : }
1234 :
1235 0 : SET_HARD_REG_BIT (set, i);
1236 0 : fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1237 :
1238 0 : for (j = vd->e[i].next_regno;
1239 0 : j != INVALID_REGNUM;
1240 0 : j = vd->e[j].next_regno)
1241 : {
1242 0 : if (TEST_HARD_REG_BIT (set, j))
1243 : {
1244 0 : fprintf (stderr, "[%u] Loop in regno chain\n", j);
1245 0 : return;
1246 : }
1247 :
1248 0 : if (vd->e[j].oldest_regno != i)
1249 : {
1250 0 : fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1251 : j, vd->e[j].oldest_regno);
1252 0 : return;
1253 : }
1254 0 : SET_HARD_REG_BIT (set, j);
1255 0 : fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1256 : }
1257 0 : fputc ('\n', stderr);
1258 : }
1259 :
1260 0 : for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1261 0 : if (! TEST_HARD_REG_BIT (set, i)
1262 0 : && (vd->e[i].mode != VOIDmode
1263 0 : || vd->e[i].oldest_regno != i
1264 0 : || vd->e[i].next_regno != INVALID_REGNUM))
1265 0 : fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1266 0 : i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1267 : vd->e[i].next_regno);
1268 : }
1269 :
1270 : /* Do copyprop_hardreg_forward_1 for a single basic block BB.
1271 : DEBUG_INSN is skipped since we do not want to involve DF related
1272 : staff as how it is handled in function pass_cprop_hardreg::execute.
1273 :
1274 : NOTE: Currently it is only used for shrink-wrap. Maybe extend it
1275 : to handle DEBUG_INSN for other uses. */
1276 :
1277 : void
1278 362027 : copyprop_hardreg_forward_bb_without_debug_insn (basic_block bb)
1279 : {
1280 362027 : struct value_data *vd;
1281 362027 : vd = XNEWVEC (struct value_data, 1);
1282 362027 : init_value_data (vd);
1283 :
1284 362027 : skip_debug_insn_p = true;
1285 362027 : copyprop_hardreg_forward_1 (bb, vd);
1286 362027 : free (vd);
1287 362027 : skip_debug_insn_p = false;
1288 362027 : }
1289 :
1290 : static void
1291 91560399 : validate_value_data (struct value_data *vd)
1292 : {
1293 91560399 : HARD_REG_SET set;
1294 91560399 : unsigned int i, j;
1295 :
1296 91560399 : CLEAR_HARD_REG_SET (set);
1297 :
1298 8515117107 : for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1299 8423556708 : if (vd->e[i].oldest_regno == i)
1300 : {
1301 8395163803 : if (vd->e[i].mode == VOIDmode)
1302 : {
1303 8067094463 : if (vd->e[i].next_regno != INVALID_REGNUM)
1304 0 : internal_error ("%qs: [%u] bad %<next_regno%> for empty chain (%u)",
1305 : __func__, i, vd->e[i].next_regno);
1306 8067094463 : continue;
1307 : }
1308 :
1309 328069340 : SET_HARD_REG_BIT (set, i);
1310 :
1311 328069340 : for (j = vd->e[i].next_regno;
1312 356462245 : j != INVALID_REGNUM;
1313 28392905 : j = vd->e[j].next_regno)
1314 : {
1315 28392905 : if (TEST_HARD_REG_BIT (set, j))
1316 0 : internal_error ("%qs: loop in %<next_regno%> chain (%u)",
1317 : __func__, j);
1318 28392905 : if (vd->e[j].oldest_regno != i)
1319 0 : internal_error ("%qs: [%u] bad %<oldest_regno%> (%u)",
1320 : __func__, j, vd->e[j].oldest_regno);
1321 :
1322 28392905 : SET_HARD_REG_BIT (set, j);
1323 : }
1324 : }
1325 :
1326 8515117107 : for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1327 8423556708 : if (! TEST_HARD_REG_BIT (set, i)
1328 8423556708 : && (vd->e[i].mode != VOIDmode
1329 8067094463 : || vd->e[i].oldest_regno != i
1330 8067094463 : || vd->e[i].next_regno != INVALID_REGNUM))
1331 0 : internal_error ("%qs: [%u] non-empty register in chain (%s %u %i)",
1332 : __func__, i,
1333 0 : GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1334 : vd->e[i].next_regno);
1335 91560399 : }
1336 :
1337 :
1338 : namespace {
1339 :
1340 : const pass_data pass_data_cprop_hardreg =
1341 : {
1342 : RTL_PASS, /* type */
1343 : "cprop_hardreg", /* name */
1344 : OPTGROUP_NONE, /* optinfo_flags */
1345 : TV_CPROP_REGISTERS, /* tv_id */
1346 : 0, /* properties_required */
1347 : 0, /* properties_provided */
1348 : 0, /* properties_destroyed */
1349 : 0, /* todo_flags_start */
1350 : TODO_df_finish, /* todo_flags_finish */
1351 : };
1352 :
1353 : class pass_cprop_hardreg : public rtl_opt_pass
1354 : {
1355 : public:
1356 292371 : pass_cprop_hardreg (gcc::context *ctxt)
1357 584742 : : rtl_opt_pass (pass_data_cprop_hardreg, ctxt)
1358 : {}
1359 :
1360 : /* opt_pass methods: */
1361 1504958 : bool gate (function *) final override
1362 : {
1363 1504958 : return (optimize > 0 && (flag_cprop_registers));
1364 : }
1365 :
1366 : unsigned int execute (function *) final override;
1367 :
1368 : }; // class pass_cprop_hardreg
1369 :
1370 : static bool
1371 12030349 : cprop_hardreg_bb (basic_block bb, struct value_data *all_vd, sbitmap visited)
1372 : {
1373 12030349 : bitmap_set_bit (visited, bb->index);
1374 :
1375 : /* If a block has a single predecessor, that we've already
1376 : processed, begin with the value data that was live at
1377 : the end of the predecessor block. */
1378 : /* ??? Ought to use more intelligent queuing of blocks. */
1379 12030349 : if (single_pred_p (bb)
1380 8923593 : && bitmap_bit_p (visited, single_pred (bb)->index)
1381 19422847 : && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
1382 : {
1383 7264396 : all_vd[bb->index] = all_vd[single_pred (bb)->index];
1384 7264396 : if (all_vd[bb->index].n_debug_insn_changes)
1385 : {
1386 : unsigned int regno;
1387 :
1388 486227 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1389 : {
1390 486227 : if (all_vd[bb->index].e[regno].debug_insn_changes)
1391 : {
1392 : struct queued_debug_insn_change *cur;
1393 102019 : for (cur = all_vd[bb->index].e[regno].debug_insn_changes;
1394 170481 : cur; cur = cur->next)
1395 102019 : --all_vd[bb->index].n_debug_insn_changes;
1396 68462 : all_vd[bb->index].e[regno].debug_insn_changes = NULL;
1397 68462 : if (all_vd[bb->index].n_debug_insn_changes == 0)
1398 : break;
1399 : }
1400 : }
1401 : }
1402 : }
1403 : else
1404 4765953 : init_value_data (all_vd + bb->index);
1405 :
1406 12030349 : return copyprop_hardreg_forward_1 (bb, all_vd + bb->index);
1407 : }
1408 :
1409 : static void
1410 49390 : cprop_hardreg_debug (function *fun, struct value_data *all_vd)
1411 : {
1412 49390 : basic_block bb;
1413 :
1414 3343408 : FOR_EACH_BB_FN (bb, fun)
1415 3294018 : if (all_vd[bb->index].n_debug_insn_changes)
1416 : {
1417 81020 : unsigned int regno;
1418 81020 : bitmap live;
1419 :
1420 81020 : live = df_get_live_out (bb);
1421 586503 : for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1422 586503 : if (all_vd[bb->index].e[regno].debug_insn_changes)
1423 : {
1424 83627 : if (REGNO_REG_SET_P (live, regno))
1425 24461 : apply_debug_insn_changes (all_vd + bb->index, regno);
1426 :
1427 83627 : struct queued_debug_insn_change *cur;
1428 83627 : for (cur = all_vd[bb->index].e[regno].debug_insn_changes;
1429 218052 : cur; cur = cur->next)
1430 134425 : --all_vd[bb->index].n_debug_insn_changes;
1431 83627 : all_vd[bb->index].e[regno].debug_insn_changes = NULL;
1432 83627 : if (all_vd[bb->index].n_debug_insn_changes == 0)
1433 : break;
1434 : }
1435 : }
1436 :
1437 49390 : queued_debug_insn_change_pool.release ();
1438 49390 : }
1439 :
1440 : unsigned int
1441 1057056 : pass_cprop_hardreg::execute (function *fun)
1442 : {
1443 1057056 : struct value_data *all_vd;
1444 1057056 : basic_block bb;
1445 :
1446 1057056 : all_vd = XNEWVEC (struct value_data, last_basic_block_for_fn (fun));
1447 :
1448 1057056 : auto_sbitmap visited (last_basic_block_for_fn (fun));
1449 1057056 : bitmap_clear (visited);
1450 :
1451 1057056 : auto_vec<int> worklist1, worklist2;
1452 1057056 : auto_vec<int> *curr = &worklist1;
1453 1057056 : auto_vec<int> *next = &worklist2;
1454 1057056 : bool any_debug_changes = false;
1455 1057056 : bool any_changes = false;
1456 :
1457 : /* We need accurate notes. Earlier passes such as if-conversion may
1458 : leave notes in an inconsistent state. */
1459 1057056 : df_note_add_problem ();
1460 1057056 : df_analyze ();
1461 :
1462 : /* It is tempting to set DF_LR_RUN_DCE, but DCE may choose to delete
1463 : an insn and this pass would not have visibility into the removal.
1464 : This pass would then potentially use the source of that
1465 : INSN for propagation purposes, generating invalid code.
1466 :
1467 : So we just ask for updated notes and handle trivial deletions
1468 : within this pass where we can update this passes internal
1469 : data structures appropriately. */
1470 1057056 : df_set_flags (DF_DEFER_INSN_RESCAN);
1471 :
1472 12605701 : FOR_EACH_BB_FN (bb, fun)
1473 : {
1474 11548645 : if (cprop_hardreg_bb (bb, all_vd, visited))
1475 : {
1476 478007 : curr->safe_push (bb->index);
1477 478007 : any_changes = true;
1478 : }
1479 11548645 : if (all_vd[bb->index].n_debug_insn_changes)
1480 68502 : any_debug_changes = true;
1481 : }
1482 :
1483 : /* We must call df_analyze here unconditionally to ensure that the
1484 : REG_UNUSED and REG_DEAD notes are consistent with and without -g. */
1485 1057056 : df_analyze ();
1486 :
1487 1057056 : if (MAY_HAVE_DEBUG_BIND_INSNS && any_debug_changes)
1488 40521 : cprop_hardreg_debug (fun, all_vd);
1489 :
1490 : /* Repeat pass up to PASSES times, but only processing basic blocks
1491 : that have changed on the previous iteration. CURR points to the
1492 : current worklist, and each iteration populates the NEXT worklist,
1493 : swapping pointers after each cycle. */
1494 :
1495 1057056 : unsigned int passes = optimize > 1 ? 3 : 2;
1496 1242162 : for (unsigned int pass = 2; pass <= passes && !curr->is_empty (); pass++)
1497 : {
1498 185106 : any_debug_changes = false;
1499 185106 : bitmap_clear (visited);
1500 185106 : next->truncate (0);
1501 666810 : for (int index : *curr)
1502 : {
1503 481704 : bb = BASIC_BLOCK_FOR_FN (fun, index);
1504 481704 : if (cprop_hardreg_bb (bb, all_vd, visited))
1505 : {
1506 4272 : next->safe_push (bb->index);
1507 4272 : any_changes = true;
1508 : }
1509 481704 : if (all_vd[bb->index].n_debug_insn_changes)
1510 12518 : any_debug_changes = true;
1511 : }
1512 :
1513 185106 : df_analyze ();
1514 185106 : if (MAY_HAVE_DEBUG_BIND_INSNS && any_debug_changes)
1515 8869 : cprop_hardreg_debug (fun, all_vd);
1516 185106 : std::swap (curr, next);
1517 : }
1518 :
1519 1057056 : free (all_vd);
1520 :
1521 : /* Copy propagation of the sp register into an mem's address
1522 : can cause what was a trapping instruction into
1523 : a non-trapping one so cleanup after that in the case of non-call
1524 : exceptions. */
1525 1057056 : if (any_changes
1526 182238 : && cfun->can_throw_non_call_exceptions
1527 1108485 : && purge_all_dead_edges ())
1528 4 : cleanup_cfg (0);
1529 1057056 : return 0;
1530 1057056 : }
1531 :
1532 : } // anon namespace
1533 :
1534 : rtl_opt_pass *
1535 292371 : make_pass_cprop_hardreg (gcc::context *ctxt)
1536 : {
1537 292371 : return new pass_cprop_hardreg (ctxt);
1538 : }
|