1 /* Copy propagation on hard registers for the GNU compiler.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
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)
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.
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/>. */
22 #include "coretypes.h"
26 #include "insn-config.h"
28 #include "addresses.h"
29 #include "hard-reg-set.h"
32 #include "dominance.h"
34 #include "basic-block.h"
38 #include "diagnostic-core.h"
40 #include "tree-pass.h"
44 /* The following code does forward propagation of hard register copies.
45 The object is to eliminate as many dependencies as possible, so that
46 we have the most scheduling freedom. As a side effect, we also clean
47 up some silly register allocation decisions made by reload. This
48 code may be obsoleted by a new register allocator. */
50 /* DEBUG_INSNs aren't changed right away, as doing so might extend the
51 lifetime of a register and get the DEBUG_INSN subsequently reset.
52 So they are queued instead, and updated only when the register is
53 used in some subsequent real insn before it is set. */
54 struct queued_debug_insn_change
56 struct queued_debug_insn_change
*next
;
61 /* Pool allocation new operator. */
62 inline void *operator new (size_t)
64 return pool
.allocate ();
67 /* Delete operator utilizing pool allocation. */
68 inline void operator delete (void *ptr
)
70 pool
.remove ((queued_debug_insn_change
*) ptr
);
73 /* Memory allocation pool. */
74 static pool_allocator
<queued_debug_insn_change
> pool
;
77 /* For each register, we have a list of registers that contain the same
78 value. The OLDEST_REGNO field points to the head of the list, and
79 the NEXT_REGNO field runs through the list. The MODE field indicates
80 what mode the data is known to be in; this field is VOIDmode when the
81 register is not known to contain valid data. */
83 struct value_data_entry
86 unsigned int oldest_regno
;
87 unsigned int next_regno
;
88 struct queued_debug_insn_change
*debug_insn_changes
;
93 struct value_data_entry e
[FIRST_PSEUDO_REGISTER
];
94 unsigned int max_value_regs
;
95 unsigned int n_debug_insn_changes
;
98 pool_allocator
<queued_debug_insn_change
> queued_debug_insn_change::pool
99 ("debug insn changes pool", 256);
101 static bool skip_debug_insn_p
;
103 static void kill_value_one_regno (unsigned, struct value_data
*);
104 static void kill_value_regno (unsigned, unsigned, struct value_data
*);
105 static void kill_value (const_rtx
, struct value_data
*);
106 static void set_value_regno (unsigned, machine_mode
, struct value_data
*);
107 static void init_value_data (struct value_data
*);
108 static void kill_clobbered_value (rtx
, const_rtx
, void *);
109 static void kill_set_value (rtx
, const_rtx
, void *);
110 static void copy_value (rtx
, rtx
, struct value_data
*);
111 static bool mode_change_ok (machine_mode
, machine_mode
,
113 static rtx
maybe_mode_change (machine_mode
, machine_mode
,
114 machine_mode
, unsigned int, unsigned int);
115 static rtx
find_oldest_value_reg (enum reg_class
, rtx
, struct value_data
*);
116 static bool replace_oldest_value_reg (rtx
*, enum reg_class
, rtx_insn
*,
117 struct value_data
*);
118 static bool replace_oldest_value_addr (rtx
*, enum reg_class
,
119 machine_mode
, addr_space_t
,
120 rtx_insn
*, struct value_data
*);
121 static bool replace_oldest_value_mem (rtx
, rtx_insn
*, struct value_data
*);
122 static bool copyprop_hardreg_forward_1 (basic_block
, struct value_data
*);
123 extern void debug_value_data (struct value_data
*);
124 #ifdef ENABLE_CHECKING
125 static void validate_value_data (struct value_data
*);
128 /* Free all queued updates for DEBUG_INSNs that change some reg to
132 free_debug_insn_changes (struct value_data
*vd
, unsigned int regno
)
134 struct queued_debug_insn_change
*cur
, *next
;
135 for (cur
= vd
->e
[regno
].debug_insn_changes
; cur
; cur
= next
)
138 --vd
->n_debug_insn_changes
;
141 vd
->e
[regno
].debug_insn_changes
= NULL
;
144 /* Kill register REGNO. This involves removing it from any value
145 lists, and resetting the value mode to VOIDmode. This is only a
146 helper function; it does not handle any hard registers overlapping
150 kill_value_one_regno (unsigned int regno
, struct value_data
*vd
)
152 unsigned int i
, next
;
154 if (vd
->e
[regno
].oldest_regno
!= regno
)
156 for (i
= vd
->e
[regno
].oldest_regno
;
157 vd
->e
[i
].next_regno
!= regno
;
158 i
= vd
->e
[i
].next_regno
)
160 vd
->e
[i
].next_regno
= vd
->e
[regno
].next_regno
;
162 else if ((next
= vd
->e
[regno
].next_regno
) != INVALID_REGNUM
)
164 for (i
= next
; i
!= INVALID_REGNUM
; i
= vd
->e
[i
].next_regno
)
165 vd
->e
[i
].oldest_regno
= next
;
168 vd
->e
[regno
].mode
= VOIDmode
;
169 vd
->e
[regno
].oldest_regno
= regno
;
170 vd
->e
[regno
].next_regno
= INVALID_REGNUM
;
171 if (vd
->e
[regno
].debug_insn_changes
)
172 free_debug_insn_changes (vd
, regno
);
174 #ifdef ENABLE_CHECKING
175 validate_value_data (vd
);
179 /* Kill the value in register REGNO for NREGS, and any other registers
180 whose values overlap. */
183 kill_value_regno (unsigned int regno
, unsigned int nregs
,
184 struct value_data
*vd
)
188 /* Kill the value we're told to kill. */
189 for (j
= 0; j
< nregs
; ++j
)
190 kill_value_one_regno (regno
+ j
, vd
);
192 /* Kill everything that overlapped what we're told to kill. */
193 if (regno
< vd
->max_value_regs
)
196 j
= regno
- vd
->max_value_regs
;
197 for (; j
< regno
; ++j
)
200 if (vd
->e
[j
].mode
== VOIDmode
)
202 n
= hard_regno_nregs
[j
][vd
->e
[j
].mode
];
204 for (i
= 0; i
< n
; ++i
)
205 kill_value_one_regno (j
+ i
, vd
);
209 /* Kill X. This is a convenience function wrapping kill_value_regno
210 so that we mind the mode the register is in. */
213 kill_value (const_rtx x
, struct value_data
*vd
)
215 if (GET_CODE (x
) == SUBREG
)
217 rtx tmp
= simplify_subreg (GET_MODE (x
), SUBREG_REG (x
),
218 GET_MODE (SUBREG_REG (x
)), SUBREG_BYTE (x
));
219 x
= tmp
? tmp
: SUBREG_REG (x
);
222 kill_value_regno (REGNO (x
), REG_NREGS (x
), vd
);
225 /* Remember that REGNO is valid in MODE. */
228 set_value_regno (unsigned int regno
, machine_mode mode
,
229 struct value_data
*vd
)
233 vd
->e
[regno
].mode
= mode
;
235 nregs
= hard_regno_nregs
[regno
][mode
];
236 if (nregs
> vd
->max_value_regs
)
237 vd
->max_value_regs
= nregs
;
240 /* Initialize VD such that there are no known relationships between regs. */
243 init_value_data (struct value_data
*vd
)
246 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
248 vd
->e
[i
].mode
= VOIDmode
;
249 vd
->e
[i
].oldest_regno
= i
;
250 vd
->e
[i
].next_regno
= INVALID_REGNUM
;
251 vd
->e
[i
].debug_insn_changes
= NULL
;
253 vd
->max_value_regs
= 0;
254 vd
->n_debug_insn_changes
= 0;
257 /* Called through note_stores. If X is clobbered, kill its value. */
260 kill_clobbered_value (rtx x
, const_rtx set
, void *data
)
262 struct value_data
*const vd
= (struct value_data
*) data
;
263 if (GET_CODE (set
) == CLOBBER
)
267 /* A structure passed as data to kill_set_value through note_stores. */
268 struct kill_set_value_data
270 struct value_data
*vd
;
274 /* Called through note_stores. If X is set, not clobbered, kill its
275 current value and install it as the root of its own value list. */
278 kill_set_value (rtx x
, const_rtx set
, void *data
)
280 struct kill_set_value_data
*ksvd
= (struct kill_set_value_data
*) data
;
281 if (rtx_equal_p (x
, ksvd
->ignore_set_reg
))
283 if (GET_CODE (set
) != CLOBBER
)
285 kill_value (x
, ksvd
->vd
);
287 set_value_regno (REGNO (x
), GET_MODE (x
), ksvd
->vd
);
291 /* Kill any register used in X as the base of an auto-increment expression,
292 and install that register as the root of its own value list. */
295 kill_autoinc_value (rtx_insn
*insn
, struct value_data
*vd
)
297 subrtx_iterator::array_type array
;
298 FOR_EACH_SUBRTX (iter
, array
, PATTERN (insn
), NONCONST
)
301 if (GET_RTX_CLASS (GET_CODE (x
)) == RTX_AUTOINC
)
305 set_value_regno (REGNO (x
), GET_MODE (x
), vd
);
306 iter
.skip_subrtxes ();
311 /* Assert that SRC has been copied to DEST. Adjust the data structures
312 to reflect that SRC contains an older copy of the shared value. */
315 copy_value (rtx dest
, rtx src
, struct value_data
*vd
)
317 unsigned int dr
= REGNO (dest
);
318 unsigned int sr
= REGNO (src
);
322 /* ??? At present, it's possible to see noop sets. It'd be nice if
323 this were cleaned up beforehand... */
327 /* Do not propagate copies to the stack pointer, as that can leave
328 memory accesses with no scheduling dependency on the stack update. */
329 if (dr
== STACK_POINTER_REGNUM
)
332 /* Likewise with the frame pointer, if we're using one. */
333 if (frame_pointer_needed
&& dr
== HARD_FRAME_POINTER_REGNUM
)
336 /* Do not propagate copies to fixed or global registers, patterns
337 can be relying to see particular fixed register or users can
338 expect the chosen global register in asm. */
339 if (fixed_regs
[dr
] || global_regs
[dr
])
342 /* If SRC and DEST overlap, don't record anything. */
343 dn
= REG_NREGS (dest
);
344 sn
= REG_NREGS (src
);
345 if ((dr
> sr
&& dr
< sr
+ sn
)
346 || (sr
> dr
&& sr
< dr
+ dn
))
349 /* If SRC had no assigned mode (i.e. we didn't know it was live)
350 assign it now and assume the value came from an input argument
352 if (vd
->e
[sr
].mode
== VOIDmode
)
353 set_value_regno (sr
, vd
->e
[dr
].mode
, vd
);
355 /* If we are narrowing the input to a smaller number of hard regs,
356 and it is in big endian, we are really extracting a high part.
357 Since we generally associate a low part of a value with the value itself,
358 we must not do the same for the high part.
359 Note we can still get low parts for the same mode combination through
360 a two-step copy involving differently sized hard regs.
361 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
362 (set (reg:DI r0) (reg:DI fr0))
363 (set (reg:SI fr2) (reg:SI r0))
364 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
365 (set (reg:SI fr2) (reg:SI fr0))
366 loads the high part of (reg:DI fr0) into fr2.
368 We can't properly represent the latter case in our tables, so don't
369 record anything then. */
370 else if (sn
< (unsigned int) hard_regno_nregs
[sr
][vd
->e
[sr
].mode
]
371 && (GET_MODE_SIZE (vd
->e
[sr
].mode
) > UNITS_PER_WORD
372 ? WORDS_BIG_ENDIAN
: BYTES_BIG_ENDIAN
))
375 /* If SRC had been assigned a mode narrower than the copy, we can't
376 link DEST into the chain, because not all of the pieces of the
377 copy came from oldest_regno. */
378 else if (sn
> (unsigned int) hard_regno_nregs
[sr
][vd
->e
[sr
].mode
])
381 /* Link DR at the end of the value chain used by SR. */
383 vd
->e
[dr
].oldest_regno
= vd
->e
[sr
].oldest_regno
;
385 for (i
= sr
; vd
->e
[i
].next_regno
!= INVALID_REGNUM
; i
= vd
->e
[i
].next_regno
)
387 vd
->e
[i
].next_regno
= dr
;
389 #ifdef ENABLE_CHECKING
390 validate_value_data (vd
);
394 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
397 mode_change_ok (machine_mode orig_mode
, machine_mode new_mode
,
398 unsigned int regno ATTRIBUTE_UNUSED
)
400 if (GET_MODE_SIZE (orig_mode
) < GET_MODE_SIZE (new_mode
))
403 #ifdef CANNOT_CHANGE_MODE_CLASS
404 return !REG_CANNOT_CHANGE_MODE_P (regno
, orig_mode
, new_mode
);
410 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
411 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
413 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
416 maybe_mode_change (machine_mode orig_mode
, machine_mode copy_mode
,
417 machine_mode new_mode
, unsigned int regno
,
418 unsigned int copy_regno ATTRIBUTE_UNUSED
)
420 if (GET_MODE_SIZE (copy_mode
) < GET_MODE_SIZE (orig_mode
)
421 && GET_MODE_SIZE (copy_mode
) < GET_MODE_SIZE (new_mode
))
424 if (orig_mode
== new_mode
)
425 return gen_raw_REG (new_mode
, regno
);
426 else if (mode_change_ok (orig_mode
, new_mode
, regno
))
428 int copy_nregs
= hard_regno_nregs
[copy_regno
][copy_mode
];
429 int use_nregs
= hard_regno_nregs
[copy_regno
][new_mode
];
431 = GET_MODE_SIZE (copy_mode
) / copy_nregs
* (copy_nregs
- use_nregs
);
433 = GET_MODE_SIZE (orig_mode
) - GET_MODE_SIZE (new_mode
) - copy_offset
;
434 int byteoffset
= offset
% UNITS_PER_WORD
;
435 int wordoffset
= offset
- byteoffset
;
437 offset
= ((WORDS_BIG_ENDIAN
? wordoffset
: 0)
438 + (BYTES_BIG_ENDIAN
? byteoffset
: 0));
439 regno
+= subreg_regno_offset (regno
, orig_mode
, offset
, new_mode
);
440 if (HARD_REGNO_MODE_OK (regno
, new_mode
))
441 return gen_raw_REG (new_mode
, regno
);
446 /* Find the oldest copy of the value contained in REGNO that is in
447 register class CL and has mode MODE. If found, return an rtx
448 of that oldest register, otherwise return NULL. */
451 find_oldest_value_reg (enum reg_class cl
, rtx reg
, struct value_data
*vd
)
453 unsigned int regno
= REGNO (reg
);
454 machine_mode mode
= GET_MODE (reg
);
457 /* If we are accessing REG in some mode other that what we set it in,
458 make sure that the replacement is valid. In particular, consider
459 (set (reg:DI r11) (...))
460 (set (reg:SI r9) (reg:SI r11))
461 (set (reg:SI r10) (...))
462 (set (...) (reg:DI r9))
463 Replacing r9 with r11 is invalid. */
464 if (mode
!= vd
->e
[regno
].mode
)
466 if (hard_regno_nregs
[regno
][mode
]
467 > hard_regno_nregs
[regno
][vd
->e
[regno
].mode
])
471 for (i
= vd
->e
[regno
].oldest_regno
; i
!= regno
; i
= vd
->e
[i
].next_regno
)
473 machine_mode oldmode
= vd
->e
[i
].mode
;
476 if (!in_hard_reg_set_p (reg_class_contents
[cl
], mode
, i
))
479 new_rtx
= maybe_mode_change (oldmode
, vd
->e
[regno
].mode
, mode
, i
, regno
);
482 ORIGINAL_REGNO (new_rtx
) = ORIGINAL_REGNO (reg
);
483 REG_ATTRS (new_rtx
) = REG_ATTRS (reg
);
484 REG_POINTER (new_rtx
) = REG_POINTER (reg
);
492 /* If possible, replace the register at *LOC with the oldest register
493 in register class CL. Return true if successfully replaced. */
496 replace_oldest_value_reg (rtx
*loc
, enum reg_class cl
, rtx_insn
*insn
,
497 struct value_data
*vd
)
499 rtx new_rtx
= find_oldest_value_reg (cl
, *loc
, vd
);
500 if (new_rtx
&& (!DEBUG_INSN_P (insn
) || !skip_debug_insn_p
))
502 if (DEBUG_INSN_P (insn
))
504 struct queued_debug_insn_change
*change
;
507 fprintf (dump_file
, "debug_insn %u: queued replacing reg %u with %u\n",
508 INSN_UID (insn
), REGNO (*loc
), REGNO (new_rtx
));
510 change
= new queued_debug_insn_change
;
511 change
->next
= vd
->e
[REGNO (new_rtx
)].debug_insn_changes
;
514 change
->new_rtx
= new_rtx
;
515 vd
->e
[REGNO (new_rtx
)].debug_insn_changes
= change
;
516 ++vd
->n_debug_insn_changes
;
520 fprintf (dump_file
, "insn %u: replaced reg %u with %u\n",
521 INSN_UID (insn
), REGNO (*loc
), REGNO (new_rtx
));
523 validate_change (insn
, loc
, new_rtx
, 1);
529 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
530 Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
531 BASE_REG_CLASS depending on how the register is being considered. */
534 replace_oldest_value_addr (rtx
*loc
, enum reg_class cl
,
535 machine_mode mode
, addr_space_t as
,
536 rtx_insn
*insn
, struct value_data
*vd
)
539 RTX_CODE code
= GET_CODE (x
);
542 bool changed
= false;
547 if (DEBUG_INSN_P (insn
))
551 rtx orig_op0
= XEXP (x
, 0);
552 rtx orig_op1
= XEXP (x
, 1);
553 RTX_CODE code0
= GET_CODE (orig_op0
);
554 RTX_CODE code1
= GET_CODE (orig_op1
);
559 enum rtx_code index_code
= SCRATCH
;
561 if (GET_CODE (op0
) == SUBREG
)
563 op0
= SUBREG_REG (op0
);
564 code0
= GET_CODE (op0
);
567 if (GET_CODE (op1
) == SUBREG
)
569 op1
= SUBREG_REG (op1
);
570 code1
= GET_CODE (op1
);
573 if (code0
== MULT
|| code0
== SIGN_EXTEND
|| code0
== TRUNCATE
574 || code0
== ZERO_EXTEND
|| code1
== MEM
)
578 index_code
= GET_CODE (*locI
);
580 else if (code1
== MULT
|| code1
== SIGN_EXTEND
|| code1
== TRUNCATE
581 || code1
== ZERO_EXTEND
|| code0
== MEM
)
585 index_code
= GET_CODE (*locI
);
587 else if (code0
== CONST_INT
|| code0
== CONST
588 || code0
== SYMBOL_REF
|| code0
== LABEL_REF
)
591 index_code
= GET_CODE (XEXP (x
, 0));
593 else if (code1
== CONST_INT
|| code1
== CONST
594 || code1
== SYMBOL_REF
|| code1
== LABEL_REF
)
597 index_code
= GET_CODE (XEXP (x
, 1));
599 else if (code0
== REG
&& code1
== REG
)
602 unsigned regno0
= REGNO (op0
), regno1
= REGNO (op1
);
604 if (REGNO_OK_FOR_INDEX_P (regno1
)
605 && regno_ok_for_base_p (regno0
, mode
, as
, PLUS
, REG
))
607 else if (REGNO_OK_FOR_INDEX_P (regno0
)
608 && regno_ok_for_base_p (regno1
, mode
, as
, PLUS
, REG
))
610 else if (regno_ok_for_base_p (regno0
, mode
, as
, PLUS
, REG
)
611 || REGNO_OK_FOR_INDEX_P (regno1
))
613 else if (regno_ok_for_base_p (regno1
, mode
, as
, PLUS
, REG
))
618 locI
= &XEXP (x
, index_op
);
619 locB
= &XEXP (x
, !index_op
);
620 index_code
= GET_CODE (*locI
);
622 else if (code0
== REG
)
626 index_code
= GET_CODE (*locI
);
628 else if (code1
== REG
)
632 index_code
= GET_CODE (*locI
);
636 changed
|= replace_oldest_value_addr (locI
, INDEX_REG_CLASS
,
639 changed
|= replace_oldest_value_addr (locB
,
640 base_reg_class (mode
, as
, PLUS
,
655 return replace_oldest_value_mem (x
, insn
, vd
);
658 return replace_oldest_value_reg (loc
, cl
, insn
, vd
);
664 fmt
= GET_RTX_FORMAT (code
);
665 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
668 changed
|= replace_oldest_value_addr (&XEXP (x
, i
), cl
, mode
, as
,
670 else if (fmt
[i
] == 'E')
671 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
672 changed
|= replace_oldest_value_addr (&XVECEXP (x
, i
, j
), cl
,
679 /* Similar to replace_oldest_value_reg, but X contains a memory. */
682 replace_oldest_value_mem (rtx x
, rtx_insn
*insn
, struct value_data
*vd
)
686 if (DEBUG_INSN_P (insn
))
689 cl
= base_reg_class (GET_MODE (x
), MEM_ADDR_SPACE (x
), MEM
, SCRATCH
);
691 return replace_oldest_value_addr (&XEXP (x
, 0), cl
,
692 GET_MODE (x
), MEM_ADDR_SPACE (x
),
696 /* Apply all queued updates for DEBUG_INSNs that change some reg to
700 apply_debug_insn_changes (struct value_data
*vd
, unsigned int regno
)
702 struct queued_debug_insn_change
*change
;
703 rtx_insn
*last_insn
= vd
->e
[regno
].debug_insn_changes
->insn
;
705 for (change
= vd
->e
[regno
].debug_insn_changes
;
707 change
= change
->next
)
709 if (last_insn
!= change
->insn
)
711 apply_change_group ();
712 last_insn
= change
->insn
;
714 validate_change (change
->insn
, change
->loc
, change
->new_rtx
, 1);
716 apply_change_group ();
719 /* Called via note_uses, for all used registers in a real insn
720 apply DEBUG_INSN changes that change registers to the used
724 cprop_find_used_regs (rtx
*loc
, void *data
)
726 struct value_data
*const vd
= (struct value_data
*) data
;
727 subrtx_iterator::array_type array
;
728 FOR_EACH_SUBRTX (iter
, array
, *loc
, NONCONST
)
733 unsigned int regno
= REGNO (x
);
734 if (vd
->e
[regno
].debug_insn_changes
)
736 apply_debug_insn_changes (vd
, regno
);
737 free_debug_insn_changes (vd
, regno
);
743 /* Apply clobbers of INSN in PATTERN and C_I_F_U to value_data VD. */
746 kill_clobbered_values (rtx_insn
*insn
, struct value_data
*vd
)
748 note_stores (PATTERN (insn
), kill_clobbered_value
, vd
);
754 for (exp
= CALL_INSN_FUNCTION_USAGE (insn
); exp
; exp
= XEXP (exp
, 1))
756 rtx x
= XEXP (exp
, 0);
757 if (GET_CODE (x
) == CLOBBER
)
758 kill_value (SET_DEST (x
), vd
);
763 /* Perform the forward copy propagation on basic block BB. */
766 copyprop_hardreg_forward_1 (basic_block bb
, struct value_data
*vd
)
768 bool anything_changed
= false;
771 for (insn
= BB_HEAD (bb
); ; insn
= NEXT_INSN (insn
))
773 int n_ops
, i
, predicated
;
774 bool is_asm
, any_replacements
;
777 bool replaced
[MAX_RECOG_OPERANDS
];
778 bool changed
= false;
779 struct kill_set_value_data ksvd
;
781 if (!NONDEBUG_INSN_P (insn
))
783 if (DEBUG_INSN_P (insn
))
785 rtx loc
= INSN_VAR_LOCATION_LOC (insn
);
786 if (!VAR_LOC_UNKNOWN_P (loc
))
787 replace_oldest_value_addr (&INSN_VAR_LOCATION_LOC (insn
),
788 ALL_REGS
, GET_MODE (loc
),
789 ADDR_SPACE_GENERIC
, insn
, vd
);
792 if (insn
== BB_END (bb
))
798 set
= single_set (insn
);
799 extract_constrain_insn (insn
);
800 preprocess_constraints (insn
);
801 const operand_alternative
*op_alt
= which_op_alt ();
802 n_ops
= recog_data
.n_operands
;
803 is_asm
= asm_noperands (PATTERN (insn
)) >= 0;
805 /* Simplify the code below by promoting OP_OUT to OP_INOUT
806 in predicated instructions. */
808 predicated
= GET_CODE (PATTERN (insn
)) == COND_EXEC
;
809 for (i
= 0; i
< n_ops
; ++i
)
811 int matches
= op_alt
[i
].matches
;
812 if (matches
>= 0 || op_alt
[i
].matched
>= 0
813 || (predicated
&& recog_data
.operand_type
[i
] == OP_OUT
))
814 recog_data
.operand_type
[i
] = OP_INOUT
;
817 /* Apply changes to earlier DEBUG_INSNs if possible. */
818 if (vd
->n_debug_insn_changes
)
819 note_uses (&PATTERN (insn
), cprop_find_used_regs
, vd
);
821 /* For each earlyclobber operand, zap the value data. */
822 for (i
= 0; i
< n_ops
; i
++)
823 if (op_alt
[i
].earlyclobber
)
824 kill_value (recog_data
.operand
[i
], vd
);
826 /* Within asms, a clobber cannot overlap inputs or outputs.
827 I wouldn't think this were true for regular insns, but
828 scan_rtx treats them like that... */
829 kill_clobbered_values (insn
, vd
);
831 /* Kill all auto-incremented values. */
832 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
833 kill_autoinc_value (insn
, vd
);
835 /* Kill all early-clobbered operands. */
836 for (i
= 0; i
< n_ops
; i
++)
837 if (op_alt
[i
].earlyclobber
)
838 kill_value (recog_data
.operand
[i
], vd
);
840 /* If we have dead sets in the insn, then we need to note these as we
842 for (link
= REG_NOTES (insn
); link
; link
= XEXP (link
, 1))
844 if (REG_NOTE_KIND (link
) == REG_UNUSED
)
846 kill_value (XEXP (link
, 0), vd
);
847 /* Furthermore, if the insn looked like a single-set,
848 but the dead store kills the source value of that
849 set, then we can no-longer use the plain move
850 special case below. */
852 && reg_overlap_mentioned_p (XEXP (link
, 0), SET_SRC (set
)))
857 /* Special-case plain move instructions, since we may well
858 be able to do the move from a different register class. */
859 if (set
&& REG_P (SET_SRC (set
)))
861 rtx src
= SET_SRC (set
);
862 unsigned int regno
= REGNO (src
);
863 machine_mode mode
= GET_MODE (src
);
867 /* If we are accessing SRC in some mode other that what we
868 set it in, make sure that the replacement is valid. */
869 if (mode
!= vd
->e
[regno
].mode
)
871 if (hard_regno_nregs
[regno
][mode
]
872 > hard_regno_nregs
[regno
][vd
->e
[regno
].mode
])
873 goto no_move_special_case
;
875 /* And likewise, if we are narrowing on big endian the transformation
877 if (hard_regno_nregs
[regno
][mode
]
878 < hard_regno_nregs
[regno
][vd
->e
[regno
].mode
]
879 && (GET_MODE_SIZE (vd
->e
[regno
].mode
) > UNITS_PER_WORD
880 ? WORDS_BIG_ENDIAN
: BYTES_BIG_ENDIAN
))
881 goto no_move_special_case
;
884 /* If the destination is also a register, try to find a source
885 register in the same class. */
886 if (REG_P (SET_DEST (set
)))
888 new_rtx
= find_oldest_value_reg (REGNO_REG_CLASS (regno
), src
, vd
);
889 if (new_rtx
&& validate_change (insn
, &SET_SRC (set
), new_rtx
, 0))
893 "insn %u: replaced reg %u with %u\n",
894 INSN_UID (insn
), regno
, REGNO (new_rtx
));
896 goto did_replacement
;
898 /* We need to re-extract as validate_change clobbers
900 extract_constrain_insn (insn
);
901 preprocess_constraints (insn
);
904 /* Otherwise, try all valid registers and see if its valid. */
905 for (i
= vd
->e
[regno
].oldest_regno
; i
!= regno
;
906 i
= vd
->e
[i
].next_regno
)
908 new_rtx
= maybe_mode_change (vd
->e
[i
].mode
, vd
->e
[regno
].mode
,
910 if (new_rtx
!= NULL_RTX
)
912 if (validate_change (insn
, &SET_SRC (set
), new_rtx
, 0))
914 ORIGINAL_REGNO (new_rtx
) = ORIGINAL_REGNO (src
);
915 REG_ATTRS (new_rtx
) = REG_ATTRS (src
);
916 REG_POINTER (new_rtx
) = REG_POINTER (src
);
919 "insn %u: replaced reg %u with %u\n",
920 INSN_UID (insn
), regno
, REGNO (new_rtx
));
922 goto did_replacement
;
924 /* We need to re-extract as validate_change clobbers
926 extract_constrain_insn (insn
);
927 preprocess_constraints (insn
);
931 no_move_special_case
:
933 any_replacements
= false;
935 /* For each input operand, replace a hard register with the
936 eldest live copy that's in an appropriate register class. */
937 for (i
= 0; i
< n_ops
; i
++)
941 /* Don't scan match_operand here, since we've no reg class
942 information to pass down. Any operands that we could
943 substitute in will be represented elsewhere. */
944 if (recog_data
.constraints
[i
][0] == '\0')
947 /* Don't replace in asms intentionally referencing hard regs. */
948 if (is_asm
&& REG_P (recog_data
.operand
[i
])
949 && (REGNO (recog_data
.operand
[i
])
950 == ORIGINAL_REGNO (recog_data
.operand
[i
])))
953 if (recog_data
.operand_type
[i
] == OP_IN
)
955 if (op_alt
[i
].is_address
)
957 = replace_oldest_value_addr (recog_data
.operand_loc
[i
],
958 alternative_class (op_alt
, i
),
959 VOIDmode
, ADDR_SPACE_GENERIC
,
961 else if (REG_P (recog_data
.operand
[i
]))
963 = replace_oldest_value_reg (recog_data
.operand_loc
[i
],
964 alternative_class (op_alt
, i
),
966 else if (MEM_P (recog_data
.operand
[i
]))
967 replaced
[i
] = replace_oldest_value_mem (recog_data
.operand
[i
],
970 else if (MEM_P (recog_data
.operand
[i
]))
971 replaced
[i
] = replace_oldest_value_mem (recog_data
.operand
[i
],
974 /* If we performed any replacement, update match_dups. */
980 new_rtx
= *recog_data
.operand_loc
[i
];
981 recog_data
.operand
[i
] = new_rtx
;
982 for (j
= 0; j
< recog_data
.n_dups
; j
++)
983 if (recog_data
.dup_num
[j
] == i
)
984 validate_unshare_change (insn
, recog_data
.dup_loc
[j
], new_rtx
, 1);
986 any_replacements
= true;
990 if (any_replacements
)
992 if (! apply_change_group ())
994 for (i
= 0; i
< n_ops
; i
++)
997 rtx old
= *recog_data
.operand_loc
[i
];
998 recog_data
.operand
[i
] = old
;
1003 "insn %u: reg replacements not verified\n",
1013 anything_changed
= true;
1015 /* If something changed, perhaps further changes to earlier
1016 DEBUG_INSNs can be applied. */
1017 if (vd
->n_debug_insn_changes
)
1018 note_uses (&PATTERN (insn
), cprop_find_used_regs
, vd
);
1022 ksvd
.ignore_set_reg
= NULL_RTX
;
1024 /* Clobber call-clobbered registers. */
1027 unsigned int set_regno
= INVALID_REGNUM
;
1028 unsigned int set_nregs
= 0;
1031 HARD_REG_SET regs_invalidated_by_this_call
;
1033 for (exp
= CALL_INSN_FUNCTION_USAGE (insn
); exp
; exp
= XEXP (exp
, 1))
1035 rtx x
= XEXP (exp
, 0);
1036 if (GET_CODE (x
) == SET
)
1038 rtx dest
= SET_DEST (x
);
1039 kill_value (dest
, vd
);
1040 set_value_regno (REGNO (dest
), GET_MODE (dest
), vd
);
1041 copy_value (dest
, SET_SRC (x
), vd
);
1042 ksvd
.ignore_set_reg
= dest
;
1043 set_regno
= REGNO (dest
);
1044 set_nregs
= REG_NREGS (dest
);
1049 get_call_reg_set_usage (insn
,
1050 ®s_invalidated_by_this_call
,
1051 regs_invalidated_by_call
);
1052 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1053 if ((TEST_HARD_REG_BIT (regs_invalidated_by_this_call
, regno
)
1054 || HARD_REGNO_CALL_PART_CLOBBERED (regno
, vd
->e
[regno
].mode
))
1055 && (regno
< set_regno
|| regno
>= set_regno
+ set_nregs
))
1056 kill_value_regno (regno
, 1, vd
);
1058 /* If SET was seen in CALL_INSN_FUNCTION_USAGE, and SET_SRC
1059 of the SET isn't in regs_invalidated_by_call hard reg set,
1060 but instead among CLOBBERs on the CALL_INSN, we could wrongly
1061 assume the value in it is still live. */
1062 if (ksvd
.ignore_set_reg
)
1063 kill_clobbered_values (insn
, vd
);
1067 && REG_P (SET_DEST (set
))
1068 && REG_P (SET_SRC (set
)));
1069 bool noop_p
= (copy_p
1070 && rtx_equal_p (SET_DEST (set
), SET_SRC (set
)));
1074 /* Notice stores. */
1075 note_stores (PATTERN (insn
), kill_set_value
, &ksvd
);
1077 /* Notice copies. */
1079 copy_value (SET_DEST (set
), SET_SRC (set
), vd
);
1082 if (insn
== BB_END (bb
))
1086 return anything_changed
;
1089 /* Dump the value chain data to stderr. */
1092 debug_value_data (struct value_data
*vd
)
1097 CLEAR_HARD_REG_SET (set
);
1099 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
1100 if (vd
->e
[i
].oldest_regno
== i
)
1102 if (vd
->e
[i
].mode
== VOIDmode
)
1104 if (vd
->e
[i
].next_regno
!= INVALID_REGNUM
)
1105 fprintf (stderr
, "[%u] Bad next_regno for empty chain (%u)\n",
1106 i
, vd
->e
[i
].next_regno
);
1110 SET_HARD_REG_BIT (set
, i
);
1111 fprintf (stderr
, "[%u %s] ", i
, GET_MODE_NAME (vd
->e
[i
].mode
));
1113 for (j
= vd
->e
[i
].next_regno
;
1114 j
!= INVALID_REGNUM
;
1115 j
= vd
->e
[j
].next_regno
)
1117 if (TEST_HARD_REG_BIT (set
, j
))
1119 fprintf (stderr
, "[%u] Loop in regno chain\n", j
);
1123 if (vd
->e
[j
].oldest_regno
!= i
)
1125 fprintf (stderr
, "[%u] Bad oldest_regno (%u)\n",
1126 j
, vd
->e
[j
].oldest_regno
);
1129 SET_HARD_REG_BIT (set
, j
);
1130 fprintf (stderr
, "[%u %s] ", j
, GET_MODE_NAME (vd
->e
[j
].mode
));
1132 fputc ('\n', stderr
);
1135 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
1136 if (! TEST_HARD_REG_BIT (set
, i
)
1137 && (vd
->e
[i
].mode
!= VOIDmode
1138 || vd
->e
[i
].oldest_regno
!= i
1139 || vd
->e
[i
].next_regno
!= INVALID_REGNUM
))
1140 fprintf (stderr
, "[%u] Non-empty reg in chain (%s %u %i)\n",
1141 i
, GET_MODE_NAME (vd
->e
[i
].mode
), vd
->e
[i
].oldest_regno
,
1142 vd
->e
[i
].next_regno
);
1145 /* Do copyprop_hardreg_forward_1 for a single basic block BB.
1146 DEBUG_INSN is skipped since we do not want to involve DF related
1147 staff as how it is handled in function pass_cprop_hardreg::execute.
1149 NOTE: Currently it is only used for shrink-wrap. Maybe extend it
1150 to handle DEBUG_INSN for other uses. */
1153 copyprop_hardreg_forward_bb_without_debug_insn (basic_block bb
)
1155 struct value_data
*vd
;
1156 vd
= XNEWVEC (struct value_data
, 1);
1157 init_value_data (vd
);
1159 skip_debug_insn_p
= true;
1160 copyprop_hardreg_forward_1 (bb
, vd
);
1162 skip_debug_insn_p
= false;
1165 #ifdef ENABLE_CHECKING
1167 validate_value_data (struct value_data
*vd
)
1172 CLEAR_HARD_REG_SET (set
);
1174 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
1175 if (vd
->e
[i
].oldest_regno
== i
)
1177 if (vd
->e
[i
].mode
== VOIDmode
)
1179 if (vd
->e
[i
].next_regno
!= INVALID_REGNUM
)
1180 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1181 i
, vd
->e
[i
].next_regno
);
1185 SET_HARD_REG_BIT (set
, i
);
1187 for (j
= vd
->e
[i
].next_regno
;
1188 j
!= INVALID_REGNUM
;
1189 j
= vd
->e
[j
].next_regno
)
1191 if (TEST_HARD_REG_BIT (set
, j
))
1192 internal_error ("validate_value_data: Loop in regno chain (%u)",
1194 if (vd
->e
[j
].oldest_regno
!= i
)
1195 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1196 j
, vd
->e
[j
].oldest_regno
);
1198 SET_HARD_REG_BIT (set
, j
);
1202 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
1203 if (! TEST_HARD_REG_BIT (set
, i
)
1204 && (vd
->e
[i
].mode
!= VOIDmode
1205 || vd
->e
[i
].oldest_regno
!= i
1206 || vd
->e
[i
].next_regno
!= INVALID_REGNUM
))
1207 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1208 i
, GET_MODE_NAME (vd
->e
[i
].mode
), vd
->e
[i
].oldest_regno
,
1209 vd
->e
[i
].next_regno
);
1215 const pass_data pass_data_cprop_hardreg
=
1217 RTL_PASS
, /* type */
1218 "cprop_hardreg", /* name */
1219 OPTGROUP_NONE
, /* optinfo_flags */
1220 TV_CPROP_REGISTERS
, /* tv_id */
1221 0, /* properties_required */
1222 0, /* properties_provided */
1223 0, /* properties_destroyed */
1224 0, /* todo_flags_start */
1225 TODO_df_finish
, /* todo_flags_finish */
1228 class pass_cprop_hardreg
: public rtl_opt_pass
1231 pass_cprop_hardreg (gcc::context
*ctxt
)
1232 : rtl_opt_pass (pass_data_cprop_hardreg
, ctxt
)
1235 /* opt_pass methods: */
1236 virtual bool gate (function
*)
1238 return (optimize
> 0 && (flag_cprop_registers
));
1241 virtual unsigned int execute (function
*);
1243 }; // class pass_cprop_hardreg
1246 pass_cprop_hardreg::execute (function
*fun
)
1248 struct value_data
*all_vd
;
1251 bool analyze_called
= false;
1253 all_vd
= XNEWVEC (struct value_data
, last_basic_block_for_fn (fun
));
1255 visited
= sbitmap_alloc (last_basic_block_for_fn (fun
));
1256 bitmap_clear (visited
);
1258 FOR_EACH_BB_FN (bb
, fun
)
1260 bitmap_set_bit (visited
, bb
->index
);
1262 /* If a block has a single predecessor, that we've already
1263 processed, begin with the value data that was live at
1264 the end of the predecessor block. */
1265 /* ??? Ought to use more intelligent queuing of blocks. */
1266 if (single_pred_p (bb
)
1267 && bitmap_bit_p (visited
, single_pred (bb
)->index
)
1268 && ! (single_pred_edge (bb
)->flags
& (EDGE_ABNORMAL_CALL
| EDGE_EH
)))
1270 all_vd
[bb
->index
] = all_vd
[single_pred (bb
)->index
];
1271 if (all_vd
[bb
->index
].n_debug_insn_changes
)
1275 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1277 if (all_vd
[bb
->index
].e
[regno
].debug_insn_changes
)
1279 all_vd
[bb
->index
].e
[regno
].debug_insn_changes
= NULL
;
1280 if (--all_vd
[bb
->index
].n_debug_insn_changes
== 0)
1287 init_value_data (all_vd
+ bb
->index
);
1289 copyprop_hardreg_forward_1 (bb
, all_vd
+ bb
->index
);
1292 if (MAY_HAVE_DEBUG_INSNS
)
1294 FOR_EACH_BB_FN (bb
, fun
)
1295 if (bitmap_bit_p (visited
, bb
->index
)
1296 && all_vd
[bb
->index
].n_debug_insn_changes
)
1301 if (!analyze_called
)
1304 analyze_called
= true;
1306 live
= df_get_live_out (bb
);
1307 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1308 if (all_vd
[bb
->index
].e
[regno
].debug_insn_changes
)
1310 if (REGNO_REG_SET_P (live
, regno
))
1311 apply_debug_insn_changes (all_vd
+ bb
->index
, regno
);
1312 if (all_vd
[bb
->index
].n_debug_insn_changes
== 0)
1317 queued_debug_insn_change::pool
.release ();
1320 sbitmap_free (visited
);
1328 make_pass_cprop_hardreg (gcc::context
*ctxt
)
1330 return new pass_cprop_hardreg (ctxt
);