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