1 /* Copy propagation on hard registers for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "insn-config.h"
29 #include "addresses.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
40 #include "tree-pass.h"
43 /* The following code does forward propagation of hard register copies.
44 The object is to eliminate as many dependencies as possible, so that
45 we have the most scheduling freedom. As a side effect, we also clean
46 up some silly register allocation decisions made by reload. This
47 code may be obsoleted by a new register allocator. */
49 /* For each register, we have a list of registers that contain the same
50 value. The OLDEST_REGNO field points to the head of the list, and
51 the NEXT_REGNO field runs through the list. The MODE field indicates
52 what mode the data is known to be in; this field is VOIDmode when the
53 register is not known to contain valid data. */
55 struct value_data_entry
57 enum machine_mode mode
;
58 unsigned int oldest_regno
;
59 unsigned int next_regno
;
64 struct value_data_entry e
[FIRST_PSEUDO_REGISTER
];
65 unsigned int max_value_regs
;
68 static void kill_value_one_regno (unsigned, struct value_data
*);
69 static void kill_value_regno (unsigned, unsigned, struct value_data
*);
70 static void kill_value (rtx
, struct value_data
*);
71 static void set_value_regno (unsigned, enum machine_mode
, struct value_data
*);
72 static void init_value_data (struct value_data
*);
73 static void kill_clobbered_value (rtx
, const_rtx
, void *);
74 static void kill_set_value (rtx
, const_rtx
, void *);
75 static int kill_autoinc_value (rtx
*, void *);
76 static void copy_value (rtx
, rtx
, struct value_data
*);
77 static bool mode_change_ok (enum machine_mode
, enum machine_mode
,
79 static rtx
maybe_mode_change (enum machine_mode
, enum machine_mode
,
80 enum machine_mode
, unsigned int, unsigned int);
81 static rtx
find_oldest_value_reg (enum reg_class
, rtx
, struct value_data
*);
82 static bool replace_oldest_value_reg (rtx
*, enum reg_class
, rtx
,
84 static bool replace_oldest_value_addr (rtx
*, enum reg_class
,
85 enum machine_mode
, rtx
,
87 static bool replace_oldest_value_mem (rtx
, rtx
, struct value_data
*);
88 static bool copyprop_hardreg_forward_1 (basic_block
, struct value_data
*);
89 extern void debug_value_data (struct value_data
*);
90 #ifdef ENABLE_CHECKING
91 static void validate_value_data (struct value_data
*);
94 /* Kill register REGNO. This involves removing it from any value
95 lists, and resetting the value mode to VOIDmode. This is only a
96 helper function; it does not handle any hard registers overlapping
100 kill_value_one_regno (unsigned int regno
, struct value_data
*vd
)
102 unsigned int i
, next
;
104 if (vd
->e
[regno
].oldest_regno
!= regno
)
106 for (i
= vd
->e
[regno
].oldest_regno
;
107 vd
->e
[i
].next_regno
!= regno
;
108 i
= vd
->e
[i
].next_regno
)
110 vd
->e
[i
].next_regno
= vd
->e
[regno
].next_regno
;
112 else if ((next
= vd
->e
[regno
].next_regno
) != INVALID_REGNUM
)
114 for (i
= next
; i
!= INVALID_REGNUM
; i
= vd
->e
[i
].next_regno
)
115 vd
->e
[i
].oldest_regno
= next
;
118 vd
->e
[regno
].mode
= VOIDmode
;
119 vd
->e
[regno
].oldest_regno
= regno
;
120 vd
->e
[regno
].next_regno
= INVALID_REGNUM
;
122 #ifdef ENABLE_CHECKING
123 validate_value_data (vd
);
127 /* Kill the value in register REGNO for NREGS, and any other registers
128 whose values overlap. */
131 kill_value_regno (unsigned int regno
, unsigned int nregs
,
132 struct value_data
*vd
)
136 /* Kill the value we're told to kill. */
137 for (j
= 0; j
< nregs
; ++j
)
138 kill_value_one_regno (regno
+ j
, vd
);
140 /* Kill everything that overlapped what we're told to kill. */
141 if (regno
< vd
->max_value_regs
)
144 j
= regno
- vd
->max_value_regs
;
145 for (; j
< regno
; ++j
)
148 if (vd
->e
[j
].mode
== VOIDmode
)
150 n
= hard_regno_nregs
[j
][vd
->e
[j
].mode
];
152 for (i
= 0; i
< n
; ++i
)
153 kill_value_one_regno (j
+ i
, vd
);
157 /* Kill X. This is a convenience function wrapping kill_value_regno
158 so that we mind the mode the register is in. */
161 kill_value (rtx x
, struct value_data
*vd
)
165 if (GET_CODE (x
) == SUBREG
)
167 x
= simplify_subreg (GET_MODE (x
), SUBREG_REG (x
),
168 GET_MODE (SUBREG_REG (x
)), SUBREG_BYTE (x
));
170 x
= SUBREG_REG (orig_rtx
);
174 unsigned int regno
= REGNO (x
);
175 unsigned int n
= hard_regno_nregs
[regno
][GET_MODE (x
)];
177 kill_value_regno (regno
, n
, vd
);
181 /* Remember that REGNO is valid in MODE. */
184 set_value_regno (unsigned int regno
, enum machine_mode mode
,
185 struct value_data
*vd
)
189 vd
->e
[regno
].mode
= mode
;
191 nregs
= hard_regno_nregs
[regno
][mode
];
192 if (nregs
> vd
->max_value_regs
)
193 vd
->max_value_regs
= nregs
;
196 /* Initialize VD such that there are no known relationships between regs. */
199 init_value_data (struct value_data
*vd
)
202 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
204 vd
->e
[i
].mode
= VOIDmode
;
205 vd
->e
[i
].oldest_regno
= i
;
206 vd
->e
[i
].next_regno
= INVALID_REGNUM
;
208 vd
->max_value_regs
= 0;
211 /* Called through note_stores. If X is clobbered, kill its value. */
214 kill_clobbered_value (rtx x
, const_rtx set
, void *data
)
216 struct value_data
*const vd
= (struct value_data
*) data
;
217 if (GET_CODE (set
) == CLOBBER
)
221 /* Called through note_stores. If X is set, not clobbered, kill its
222 current value and install it as the root of its own value list. */
225 kill_set_value (rtx x
, const_rtx set
, void *data
)
227 struct value_data
*const vd
= (struct value_data
*) data
;
228 if (GET_CODE (set
) != CLOBBER
)
232 set_value_regno (REGNO (x
), GET_MODE (x
), vd
);
236 /* Called through for_each_rtx. Kill any register used as the base of an
237 auto-increment expression, and install that register as the root of its
241 kill_autoinc_value (rtx
*px
, void *data
)
244 struct value_data
*const vd
= (struct value_data
*) data
;
246 if (GET_RTX_CLASS (GET_CODE (x
)) == RTX_AUTOINC
)
250 set_value_regno (REGNO (x
), GET_MODE (x
), vd
);
257 /* Assert that SRC has been copied to DEST. Adjust the data structures
258 to reflect that SRC contains an older copy of the shared value. */
261 copy_value (rtx dest
, rtx src
, struct value_data
*vd
)
263 unsigned int dr
= REGNO (dest
);
264 unsigned int sr
= REGNO (src
);
268 /* ??? At present, it's possible to see noop sets. It'd be nice if
269 this were cleaned up beforehand... */
273 /* Do not propagate copies to the stack pointer, as that can leave
274 memory accesses with no scheduling dependency on the stack update. */
275 if (dr
== STACK_POINTER_REGNUM
)
278 /* Likewise with the frame pointer, if we're using one. */
279 if (frame_pointer_needed
&& dr
== HARD_FRAME_POINTER_REGNUM
)
282 /* Do not propagate copies to fixed or global registers, patterns
283 can be relying to see particular fixed register or users can
284 expect the chosen global register in asm. */
285 if (fixed_regs
[dr
] || global_regs
[dr
])
288 /* If SRC and DEST overlap, don't record anything. */
289 dn
= hard_regno_nregs
[dr
][GET_MODE (dest
)];
290 sn
= hard_regno_nregs
[sr
][GET_MODE (dest
)];
291 if ((dr
> sr
&& dr
< sr
+ sn
)
292 || (sr
> dr
&& sr
< dr
+ dn
))
295 /* If SRC had no assigned mode (i.e. we didn't know it was live)
296 assign it now and assume the value came from an input argument
298 if (vd
->e
[sr
].mode
== VOIDmode
)
299 set_value_regno (sr
, vd
->e
[dr
].mode
, vd
);
301 /* If we are narrowing the input to a smaller number of hard regs,
302 and it is in big endian, we are really extracting a high part.
303 Since we generally associate a low part of a value with the value itself,
304 we must not do the same for the high part.
305 Note we can still get low parts for the same mode combination through
306 a two-step copy involving differently sized hard regs.
307 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
308 (set (reg:DI r0) (reg:DI fr0))
309 (set (reg:SI fr2) (reg:SI r0))
310 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
311 (set (reg:SI fr2) (reg:SI fr0))
312 loads the high part of (reg:DI fr0) into fr2.
314 We can't properly represent the latter case in our tables, so don't
315 record anything then. */
316 else if (sn
< (unsigned int) hard_regno_nregs
[sr
][vd
->e
[sr
].mode
]
317 && (GET_MODE_SIZE (vd
->e
[sr
].mode
) > UNITS_PER_WORD
318 ? WORDS_BIG_ENDIAN
: BYTES_BIG_ENDIAN
))
321 /* If SRC had been assigned a mode narrower than the copy, we can't
322 link DEST into the chain, because not all of the pieces of the
323 copy came from oldest_regno. */
324 else if (sn
> (unsigned int) hard_regno_nregs
[sr
][vd
->e
[sr
].mode
])
327 /* Link DR at the end of the value chain used by SR. */
329 vd
->e
[dr
].oldest_regno
= vd
->e
[sr
].oldest_regno
;
331 for (i
= sr
; vd
->e
[i
].next_regno
!= INVALID_REGNUM
; i
= vd
->e
[i
].next_regno
)
333 vd
->e
[i
].next_regno
= dr
;
335 #ifdef ENABLE_CHECKING
336 validate_value_data (vd
);
340 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
343 mode_change_ok (enum machine_mode orig_mode
, enum machine_mode new_mode
,
344 unsigned int regno ATTRIBUTE_UNUSED
)
346 if (GET_MODE_SIZE (orig_mode
) < GET_MODE_SIZE (new_mode
))
349 #ifdef CANNOT_CHANGE_MODE_CLASS
350 return !REG_CANNOT_CHANGE_MODE_P (regno
, orig_mode
, new_mode
);
356 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
357 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
359 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
362 maybe_mode_change (enum machine_mode orig_mode
, enum machine_mode copy_mode
,
363 enum machine_mode new_mode
, unsigned int regno
,
364 unsigned int copy_regno ATTRIBUTE_UNUSED
)
366 if (GET_MODE_SIZE (copy_mode
) < GET_MODE_SIZE (orig_mode
)
367 && GET_MODE_SIZE (copy_mode
) < GET_MODE_SIZE (new_mode
))
370 if (orig_mode
== new_mode
)
371 return gen_rtx_raw_REG (new_mode
, regno
);
372 else if (mode_change_ok (orig_mode
, new_mode
, regno
))
374 int copy_nregs
= hard_regno_nregs
[copy_regno
][copy_mode
];
375 int use_nregs
= hard_regno_nregs
[copy_regno
][new_mode
];
377 = GET_MODE_SIZE (copy_mode
) / copy_nregs
* (copy_nregs
- use_nregs
);
379 = GET_MODE_SIZE (orig_mode
) - GET_MODE_SIZE (new_mode
) - copy_offset
;
380 int byteoffset
= offset
% UNITS_PER_WORD
;
381 int wordoffset
= offset
- byteoffset
;
383 offset
= ((WORDS_BIG_ENDIAN
? wordoffset
: 0)
384 + (BYTES_BIG_ENDIAN
? byteoffset
: 0));
385 return gen_rtx_raw_REG (new_mode
,
386 regno
+ subreg_regno_offset (regno
, orig_mode
,
393 /* Find the oldest copy of the value contained in REGNO that is in
394 register class CL and has mode MODE. If found, return an rtx
395 of that oldest register, otherwise return NULL. */
398 find_oldest_value_reg (enum reg_class cl
, rtx reg
, struct value_data
*vd
)
400 unsigned int regno
= REGNO (reg
);
401 enum machine_mode mode
= GET_MODE (reg
);
404 /* If we are accessing REG in some mode other that what we set it in,
405 make sure that the replacement is valid. In particular, consider
406 (set (reg:DI r11) (...))
407 (set (reg:SI r9) (reg:SI r11))
408 (set (reg:SI r10) (...))
409 (set (...) (reg:DI r9))
410 Replacing r9 with r11 is invalid. */
411 if (mode
!= vd
->e
[regno
].mode
)
413 if (hard_regno_nregs
[regno
][mode
]
414 > hard_regno_nregs
[regno
][vd
->e
[regno
].mode
])
418 for (i
= vd
->e
[regno
].oldest_regno
; i
!= regno
; i
= vd
->e
[i
].next_regno
)
420 enum machine_mode oldmode
= vd
->e
[i
].mode
;
423 if (!in_hard_reg_set_p (reg_class_contents
[cl
], mode
, i
))
426 new_rtx
= maybe_mode_change (oldmode
, vd
->e
[regno
].mode
, mode
, i
, regno
);
429 ORIGINAL_REGNO (new_rtx
) = ORIGINAL_REGNO (reg
);
430 REG_ATTRS (new_rtx
) = REG_ATTRS (reg
);
431 REG_POINTER (new_rtx
) = REG_POINTER (reg
);
439 /* If possible, replace the register at *LOC with the oldest register
440 in register class CL. Return true if successfully replaced. */
443 replace_oldest_value_reg (rtx
*loc
, enum reg_class cl
, rtx insn
,
444 struct value_data
*vd
)
446 rtx new_rtx
= find_oldest_value_reg (cl
, *loc
, vd
);
450 fprintf (dump_file
, "insn %u: replaced reg %u with %u\n",
451 INSN_UID (insn
), REGNO (*loc
), REGNO (new_rtx
));
453 validate_change (insn
, loc
, new_rtx
, 1);
459 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
460 Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
461 BASE_REG_CLASS depending on how the register is being considered. */
464 replace_oldest_value_addr (rtx
*loc
, enum reg_class cl
,
465 enum machine_mode mode
, rtx insn
,
466 struct value_data
*vd
)
469 RTX_CODE code
= GET_CODE (x
);
472 bool changed
= false;
477 if (DEBUG_INSN_P (insn
))
481 rtx orig_op0
= XEXP (x
, 0);
482 rtx orig_op1
= XEXP (x
, 1);
483 RTX_CODE code0
= GET_CODE (orig_op0
);
484 RTX_CODE code1
= GET_CODE (orig_op1
);
489 enum rtx_code index_code
= SCRATCH
;
491 if (GET_CODE (op0
) == SUBREG
)
493 op0
= SUBREG_REG (op0
);
494 code0
= GET_CODE (op0
);
497 if (GET_CODE (op1
) == SUBREG
)
499 op1
= SUBREG_REG (op1
);
500 code1
= GET_CODE (op1
);
503 if (code0
== MULT
|| code0
== SIGN_EXTEND
|| code0
== TRUNCATE
504 || code0
== ZERO_EXTEND
|| code1
== MEM
)
508 index_code
= GET_CODE (*locI
);
510 else if (code1
== MULT
|| code1
== SIGN_EXTEND
|| code1
== TRUNCATE
511 || code1
== ZERO_EXTEND
|| code0
== MEM
)
515 index_code
= GET_CODE (*locI
);
517 else if (code0
== CONST_INT
|| code0
== CONST
518 || code0
== SYMBOL_REF
|| code0
== LABEL_REF
)
521 index_code
= GET_CODE (XEXP (x
, 0));
523 else if (code1
== CONST_INT
|| code1
== CONST
524 || code1
== SYMBOL_REF
|| code1
== LABEL_REF
)
527 index_code
= GET_CODE (XEXP (x
, 1));
529 else if (code0
== REG
&& code1
== REG
)
532 unsigned regno0
= REGNO (op0
), regno1
= REGNO (op1
);
534 if (REGNO_OK_FOR_INDEX_P (regno1
)
535 && regno_ok_for_base_p (regno0
, mode
, PLUS
, REG
))
537 else if (REGNO_OK_FOR_INDEX_P (regno0
)
538 && regno_ok_for_base_p (regno1
, mode
, PLUS
, REG
))
540 else if (regno_ok_for_base_p (regno0
, mode
, PLUS
, REG
)
541 || REGNO_OK_FOR_INDEX_P (regno1
))
543 else if (regno_ok_for_base_p (regno1
, mode
, PLUS
, REG
))
548 locI
= &XEXP (x
, index_op
);
549 locB
= &XEXP (x
, !index_op
);
550 index_code
= GET_CODE (*locI
);
552 else if (code0
== REG
)
556 index_code
= GET_CODE (*locI
);
558 else if (code1
== REG
)
562 index_code
= GET_CODE (*locI
);
566 changed
|= replace_oldest_value_addr (locI
, INDEX_REG_CLASS
, mode
,
569 changed
|= replace_oldest_value_addr (locB
,
570 base_reg_class (mode
, PLUS
,
585 return replace_oldest_value_mem (x
, insn
, vd
);
588 return replace_oldest_value_reg (loc
, cl
, insn
, vd
);
594 fmt
= GET_RTX_FORMAT (code
);
595 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
598 changed
|= replace_oldest_value_addr (&XEXP (x
, i
), cl
, mode
,
600 else if (fmt
[i
] == 'E')
601 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
602 changed
|= replace_oldest_value_addr (&XVECEXP (x
, i
, j
), cl
,
609 /* Similar to replace_oldest_value_reg, but X contains a memory. */
612 replace_oldest_value_mem (rtx x
, rtx insn
, struct value_data
*vd
)
616 if (DEBUG_INSN_P (insn
))
619 cl
= base_reg_class (GET_MODE (x
), MEM
, SCRATCH
);
621 return replace_oldest_value_addr (&XEXP (x
, 0), cl
,
622 GET_MODE (x
), insn
, vd
);
625 /* Perform the forward copy propagation on basic block BB. */
628 copyprop_hardreg_forward_1 (basic_block bb
, struct value_data
*vd
)
630 bool anything_changed
= false;
633 for (insn
= BB_HEAD (bb
); ; insn
= NEXT_INSN (insn
))
635 int n_ops
, i
, alt
, predicated
;
636 bool is_asm
, any_replacements
;
638 bool replaced
[MAX_RECOG_OPERANDS
];
639 bool changed
= false;
641 if (!NONDEBUG_INSN_P (insn
))
643 if (DEBUG_INSN_P (insn
))
645 rtx loc
= INSN_VAR_LOCATION_LOC (insn
);
646 if (!VAR_LOC_UNKNOWN_P (loc
)
647 && replace_oldest_value_addr (&INSN_VAR_LOCATION_LOC (insn
),
648 ALL_REGS
, GET_MODE (loc
),
651 changed
= apply_change_group ();
652 gcc_assert (changed
);
653 anything_changed
= true;
657 if (insn
== BB_END (bb
))
663 set
= single_set (insn
);
665 if (! constrain_operands (1))
666 fatal_insn_not_found (insn
);
667 preprocess_constraints ();
668 alt
= which_alternative
;
669 n_ops
= recog_data
.n_operands
;
670 is_asm
= asm_noperands (PATTERN (insn
)) >= 0;
672 /* Simplify the code below by rewriting things to reflect
673 matching constraints. Also promote OP_OUT to OP_INOUT
674 in predicated instructions. */
676 predicated
= GET_CODE (PATTERN (insn
)) == COND_EXEC
;
677 for (i
= 0; i
< n_ops
; ++i
)
679 int matches
= recog_op_alt
[i
][alt
].matches
;
681 recog_op_alt
[i
][alt
].cl
= recog_op_alt
[matches
][alt
].cl
;
682 if (matches
>= 0 || recog_op_alt
[i
][alt
].matched
>= 0
683 || (predicated
&& recog_data
.operand_type
[i
] == OP_OUT
))
684 recog_data
.operand_type
[i
] = OP_INOUT
;
687 /* For each earlyclobber operand, zap the value data. */
688 for (i
= 0; i
< n_ops
; i
++)
689 if (recog_op_alt
[i
][alt
].earlyclobber
)
690 kill_value (recog_data
.operand
[i
], vd
);
692 /* Within asms, a clobber cannot overlap inputs or outputs.
693 I wouldn't think this were true for regular insns, but
694 scan_rtx treats them like that... */
695 note_stores (PATTERN (insn
), kill_clobbered_value
, vd
);
697 /* Kill all auto-incremented values. */
698 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
699 for_each_rtx (&PATTERN (insn
), kill_autoinc_value
, vd
);
701 /* Kill all early-clobbered operands. */
702 for (i
= 0; i
< n_ops
; i
++)
703 if (recog_op_alt
[i
][alt
].earlyclobber
)
704 kill_value (recog_data
.operand
[i
], vd
);
706 /* Special-case plain move instructions, since we may well
707 be able to do the move from a different register class. */
708 if (set
&& REG_P (SET_SRC (set
)))
710 rtx src
= SET_SRC (set
);
711 unsigned int regno
= REGNO (src
);
712 enum machine_mode mode
= GET_MODE (src
);
716 /* If we are accessing SRC in some mode other that what we
717 set it in, make sure that the replacement is valid. */
718 if (mode
!= vd
->e
[regno
].mode
)
720 if (hard_regno_nregs
[regno
][mode
]
721 > hard_regno_nregs
[regno
][vd
->e
[regno
].mode
])
722 goto no_move_special_case
;
725 /* If the destination is also a register, try to find a source
726 register in the same class. */
727 if (REG_P (SET_DEST (set
)))
729 new_rtx
= find_oldest_value_reg (REGNO_REG_CLASS (regno
), src
, vd
);
730 if (new_rtx
&& validate_change (insn
, &SET_SRC (set
), new_rtx
, 0))
734 "insn %u: replaced reg %u with %u\n",
735 INSN_UID (insn
), regno
, REGNO (new_rtx
));
737 goto did_replacement
;
741 /* Otherwise, try all valid registers and see if its valid. */
742 for (i
= vd
->e
[regno
].oldest_regno
; i
!= regno
;
743 i
= vd
->e
[i
].next_regno
)
745 new_rtx
= maybe_mode_change (vd
->e
[i
].mode
, vd
->e
[regno
].mode
,
747 if (new_rtx
!= NULL_RTX
)
749 if (validate_change (insn
, &SET_SRC (set
), new_rtx
, 0))
751 ORIGINAL_REGNO (new_rtx
) = ORIGINAL_REGNO (src
);
752 REG_ATTRS (new_rtx
) = REG_ATTRS (src
);
753 REG_POINTER (new_rtx
) = REG_POINTER (src
);
756 "insn %u: replaced reg %u with %u\n",
757 INSN_UID (insn
), regno
, REGNO (new_rtx
));
759 goto did_replacement
;
764 no_move_special_case
:
766 any_replacements
= false;
768 /* For each input operand, replace a hard register with the
769 eldest live copy that's in an appropriate register class. */
770 for (i
= 0; i
< n_ops
; i
++)
774 /* Don't scan match_operand here, since we've no reg class
775 information to pass down. Any operands that we could
776 substitute in will be represented elsewhere. */
777 if (recog_data
.constraints
[i
][0] == '\0')
780 /* Don't replace in asms intentionally referencing hard regs. */
781 if (is_asm
&& REG_P (recog_data
.operand
[i
])
782 && (REGNO (recog_data
.operand
[i
])
783 == ORIGINAL_REGNO (recog_data
.operand
[i
])))
786 if (recog_data
.operand_type
[i
] == OP_IN
)
788 if (recog_op_alt
[i
][alt
].is_address
)
790 = replace_oldest_value_addr (recog_data
.operand_loc
[i
],
791 recog_op_alt
[i
][alt
].cl
,
793 else if (REG_P (recog_data
.operand
[i
]))
795 = replace_oldest_value_reg (recog_data
.operand_loc
[i
],
796 recog_op_alt
[i
][alt
].cl
,
798 else if (MEM_P (recog_data
.operand
[i
]))
799 replaced
[i
] = replace_oldest_value_mem (recog_data
.operand
[i
],
802 else if (MEM_P (recog_data
.operand
[i
]))
803 replaced
[i
] = replace_oldest_value_mem (recog_data
.operand
[i
],
806 /* If we performed any replacement, update match_dups. */
812 new_rtx
= *recog_data
.operand_loc
[i
];
813 recog_data
.operand
[i
] = new_rtx
;
814 for (j
= 0; j
< recog_data
.n_dups
; j
++)
815 if (recog_data
.dup_num
[j
] == i
)
816 validate_unshare_change (insn
, recog_data
.dup_loc
[j
], new_rtx
, 1);
818 any_replacements
= true;
822 if (any_replacements
)
824 if (! apply_change_group ())
826 for (i
= 0; i
< n_ops
; i
++)
829 rtx old
= *recog_data
.operand_loc
[i
];
830 recog_data
.operand
[i
] = old
;
835 "insn %u: reg replacements not verified\n",
844 anything_changed
= true;
846 /* Clobber call-clobbered registers. */
848 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
849 if (TEST_HARD_REG_BIT (regs_invalidated_by_call
, i
))
850 kill_value_regno (i
, 1, vd
);
853 note_stores (PATTERN (insn
), kill_set_value
, vd
);
856 if (set
&& REG_P (SET_DEST (set
)) && REG_P (SET_SRC (set
)))
857 copy_value (SET_DEST (set
), SET_SRC (set
), vd
);
859 if (insn
== BB_END (bb
))
863 return anything_changed
;
866 /* Main entry point for the forward copy propagation optimization. */
869 copyprop_hardreg_forward (void)
871 struct value_data
*all_vd
;
875 all_vd
= XNEWVEC (struct value_data
, last_basic_block
);
877 visited
= sbitmap_alloc (last_basic_block
);
878 sbitmap_zero (visited
);
882 SET_BIT (visited
, bb
->index
);
884 /* If a block has a single predecessor, that we've already
885 processed, begin with the value data that was live at
886 the end of the predecessor block. */
887 /* ??? Ought to use more intelligent queuing of blocks. */
888 if (single_pred_p (bb
)
889 && TEST_BIT (visited
, single_pred (bb
)->index
)
890 && ! (single_pred_edge (bb
)->flags
& (EDGE_ABNORMAL_CALL
| EDGE_EH
)))
891 all_vd
[bb
->index
] = all_vd
[single_pred (bb
)->index
];
893 init_value_data (all_vd
+ bb
->index
);
895 copyprop_hardreg_forward_1 (bb
, all_vd
+ bb
->index
);
898 sbitmap_free (visited
);
903 /* Dump the value chain data to stderr. */
906 debug_value_data (struct value_data
*vd
)
911 CLEAR_HARD_REG_SET (set
);
913 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
914 if (vd
->e
[i
].oldest_regno
== i
)
916 if (vd
->e
[i
].mode
== VOIDmode
)
918 if (vd
->e
[i
].next_regno
!= INVALID_REGNUM
)
919 fprintf (stderr
, "[%u] Bad next_regno for empty chain (%u)\n",
920 i
, vd
->e
[i
].next_regno
);
924 SET_HARD_REG_BIT (set
, i
);
925 fprintf (stderr
, "[%u %s] ", i
, GET_MODE_NAME (vd
->e
[i
].mode
));
927 for (j
= vd
->e
[i
].next_regno
;
929 j
= vd
->e
[j
].next_regno
)
931 if (TEST_HARD_REG_BIT (set
, j
))
933 fprintf (stderr
, "[%u] Loop in regno chain\n", j
);
937 if (vd
->e
[j
].oldest_regno
!= i
)
939 fprintf (stderr
, "[%u] Bad oldest_regno (%u)\n",
940 j
, vd
->e
[j
].oldest_regno
);
943 SET_HARD_REG_BIT (set
, j
);
944 fprintf (stderr
, "[%u %s] ", j
, GET_MODE_NAME (vd
->e
[j
].mode
));
946 fputc ('\n', stderr
);
949 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
950 if (! TEST_HARD_REG_BIT (set
, i
)
951 && (vd
->e
[i
].mode
!= VOIDmode
952 || vd
->e
[i
].oldest_regno
!= i
953 || vd
->e
[i
].next_regno
!= INVALID_REGNUM
))
954 fprintf (stderr
, "[%u] Non-empty reg in chain (%s %u %i)\n",
955 i
, GET_MODE_NAME (vd
->e
[i
].mode
), vd
->e
[i
].oldest_regno
,
956 vd
->e
[i
].next_regno
);
959 #ifdef ENABLE_CHECKING
961 validate_value_data (struct value_data
*vd
)
966 CLEAR_HARD_REG_SET (set
);
968 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
969 if (vd
->e
[i
].oldest_regno
== i
)
971 if (vd
->e
[i
].mode
== VOIDmode
)
973 if (vd
->e
[i
].next_regno
!= INVALID_REGNUM
)
974 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
975 i
, vd
->e
[i
].next_regno
);
979 SET_HARD_REG_BIT (set
, i
);
981 for (j
= vd
->e
[i
].next_regno
;
983 j
= vd
->e
[j
].next_regno
)
985 if (TEST_HARD_REG_BIT (set
, j
))
986 internal_error ("validate_value_data: Loop in regno chain (%u)",
988 if (vd
->e
[j
].oldest_regno
!= i
)
989 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
990 j
, vd
->e
[j
].oldest_regno
);
992 SET_HARD_REG_BIT (set
, j
);
996 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
997 if (! TEST_HARD_REG_BIT (set
, i
)
998 && (vd
->e
[i
].mode
!= VOIDmode
999 || vd
->e
[i
].oldest_regno
!= i
1000 || vd
->e
[i
].next_regno
!= INVALID_REGNUM
))
1001 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1002 i
, GET_MODE_NAME (vd
->e
[i
].mode
), vd
->e
[i
].oldest_regno
,
1003 vd
->e
[i
].next_regno
);
1008 gate_handle_cprop (void)
1010 return (optimize
> 0 && (flag_cprop_registers
));
1014 struct rtl_opt_pass pass_cprop_hardreg
=
1018 "cprop_hardreg", /* name */
1019 gate_handle_cprop
, /* gate */
1020 copyprop_hardreg_forward
, /* execute */
1023 0, /* static_pass_number */
1024 TV_CPROP_REGISTERS
, /* tv_id */
1025 0, /* properties_required */
1026 0, /* properties_provided */
1027 0, /* properties_destroyed */
1028 0, /* todo_flags_start */
1029 TODO_dump_func
| TODO_verify_rtl_sharing
/* todo_flags_finish */