1 /* Static Single Assignment conversion routines for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 Building an Optimizing Compiler
25 Butterworth-Heinemann, 1998
27 Static Single Assignment Construction
28 Preston Briggs, Tim Harvey, Taylor Simpson
29 Technical Report, Rice University, 1995
30 ftp://ftp.cs.rice.edu/public/preston/optimizer/SSA.ps.gz. */
38 #include "partition.h"
42 #include "hard-reg-set.h"
46 #include "insn-config.h"
48 #include "basic-block.h"
54 Handle subregs better, maybe. For now, if a reg that's set in a
55 subreg expression is duplicated going into SSA form, an extra copy
56 is inserted first that copies the entire reg into the duplicate, so
57 that the other bits are preserved. This isn't strictly SSA, since
58 at least part of the reg is assigned in more than one place (though
61 ??? What to do about strict_low_part. Probably I'll have to split
62 them out of their current instructions first thing.
64 Actually the best solution may be to have a kind of "mid-level rtl"
65 in which the RTL encodes exactly what we want, without exposing a
66 lot of niggling processor details. At some later point we lower
67 the representation, calling back into optabs to finish any necessary
70 /* All pseudo-registers and select hard registers are converted to SSA
71 form. When converting out of SSA, these select hard registers are
72 guaranteed to be mapped to their original register number. Each
73 machine's .h file should define CONVERT_HARD_REGISTER_TO_SSA_P
74 indicating which hard registers should be converted.
76 When converting out of SSA, temporaries for all registers are
77 partitioned. The partition is checked to ensure that all uses of
78 the same hard register in the same machine mode are in the same
81 /* If conservative_reg_partition is non-zero, use a conservative
82 register partitioning algorithm (which leaves more regs after
83 emerging from SSA) instead of the coalescing one. This is being
84 left in for a limited time only, as a debugging tool until the
85 coalescing algorithm is validated. */
87 static int conservative_reg_partition
;
89 /* This flag is set when the CFG is in SSA form. */
92 /* Element I is the single instruction that sets register I. */
93 varray_type ssa_definition
;
95 /* Element I-PSEUDO is the normal register that originated the ssa
96 register in question. */
97 varray_type ssa_rename_from
;
99 /* Element I is the normal register that originated the ssa
100 register in question.
102 A hash table stores the (register, rtl) pairs. These are each
103 xmalloc'ed and deleted when the hash table is destroyed. */
104 htab_t ssa_rename_from_ht
;
106 /* The running target ssa register for a given pseudo register.
107 (Pseudo registers appear in only one mode.) */
108 static rtx
*ssa_rename_to_pseudo
;
109 /* Similar, but for hard registers. A hard register can appear in
110 many modes, so we store an equivalent pseudo for each of the
112 static rtx ssa_rename_to_hard
[FIRST_PSEUDO_REGISTER
][NUM_MACHINE_MODES
];
114 /* ssa_rename_from maps pseudo registers to the original corresponding
115 RTL. It is implemented as using a hash table. */
120 } ssa_rename_from_pair
;
122 struct ssa_rename_from_hash_table_data
{
123 sbitmap canonical_elements
;
124 partition reg_partition
;
127 static rtx gen_sequence
129 static void ssa_rename_from_initialize
131 static rtx ssa_rename_from_lookup
133 static unsigned int original_register
134 PARAMS ((unsigned int regno
));
135 static void ssa_rename_from_insert
136 PARAMS ((unsigned int reg
, rtx r
));
137 static void ssa_rename_from_free
139 typedef int (*srf_trav
) PARAMS ((int regno
, rtx r
, sbitmap canonical_elements
, partition reg_partition
));
140 static void ssa_rename_from_traverse
141 PARAMS ((htab_trav callback_function
, sbitmap canonical_elements
, partition reg_partition
));
142 /*static Avoid warnign message. */ void ssa_rename_from_print
144 static int ssa_rename_from_print_1
145 PARAMS ((void **slot
, void *data
));
146 static hashval_t ssa_rename_from_hash_function
147 PARAMS ((const void * srfp
));
148 static int ssa_rename_from_equal
149 PARAMS ((const void *srfp1
, const void *srfp2
));
150 static void ssa_rename_from_delete
151 PARAMS ((void *srfp
));
153 static rtx ssa_rename_to_lookup
155 static void ssa_rename_to_insert
156 PARAMS ((rtx reg
, rtx r
));
158 /* The number of registers that were live on entry to the SSA routines. */
159 static unsigned int ssa_max_reg_num
;
161 /* Local function prototypes. */
163 struct rename_context
;
165 static inline rtx
* phi_alternative
167 static void compute_dominance_frontiers_1
168 PARAMS ((sbitmap
*frontiers
, dominance_info idom
, int bb
, sbitmap done
));
169 static void find_evaluations_1
170 PARAMS ((rtx dest
, rtx set
, void *data
));
171 static void find_evaluations
172 PARAMS ((sbitmap
*evals
, int nregs
));
173 static void compute_iterated_dominance_frontiers
174 PARAMS ((sbitmap
*idfs
, sbitmap
*frontiers
, sbitmap
*evals
, int nregs
));
175 static void insert_phi_node
176 PARAMS ((int regno
, int b
));
177 static void insert_phi_nodes
178 PARAMS ((sbitmap
*idfs
, sbitmap
*evals
, int nregs
));
179 static void create_delayed_rename
180 PARAMS ((struct rename_context
*, rtx
*));
181 static void apply_delayed_renames
182 PARAMS ((struct rename_context
*));
183 static int rename_insn_1
184 PARAMS ((rtx
*ptr
, void *data
));
185 static void rename_block
186 PARAMS ((int b
, dominance_info dom
));
187 static void rename_registers
188 PARAMS ((int nregs
, dominance_info idom
));
190 static inline int ephi_add_node
191 PARAMS ((rtx reg
, rtx
*nodes
, int *n_nodes
));
192 static int * ephi_forward
193 PARAMS ((int t
, sbitmap visited
, sbitmap
*succ
, int *tstack
));
194 static void ephi_backward
195 PARAMS ((int t
, sbitmap visited
, sbitmap
*pred
, rtx
*nodes
));
196 static void ephi_create
197 PARAMS ((int t
, sbitmap visited
, sbitmap
*pred
, sbitmap
*succ
, rtx
*nodes
));
198 static void eliminate_phi
199 PARAMS ((edge e
, partition reg_partition
));
200 static int make_regs_equivalent_over_bad_edges
201 PARAMS ((int bb
, partition reg_partition
));
203 /* These are used only in the conservative register partitioning
205 static int make_equivalent_phi_alternatives_equivalent
206 PARAMS ((int bb
, partition reg_partition
));
207 static partition compute_conservative_reg_partition
209 static int record_canonical_element_1
210 PARAMS ((void **srfp
, void *data
));
211 static int check_hard_regs_in_partition
212 PARAMS ((partition reg_partition
));
213 static int rename_equivalent_regs_in_insn
214 PARAMS ((rtx
*ptr
, void *data
));
216 /* These are used in the register coalescing algorithm. */
217 static int coalesce_if_unconflicting
218 PARAMS ((partition p
, conflict_graph conflicts
, int reg1
, int reg2
));
219 static int coalesce_regs_in_copies
220 PARAMS ((basic_block bb
, partition p
, conflict_graph conflicts
));
221 static int coalesce_reg_in_phi
222 PARAMS ((rtx
, int dest_regno
, int src_regno
, void *data
));
223 static int coalesce_regs_in_successor_phi_nodes
224 PARAMS ((basic_block bb
, partition p
, conflict_graph conflicts
));
225 static partition compute_coalesced_reg_partition
227 static int mark_reg_in_phi
228 PARAMS ((rtx
*ptr
, void *data
));
229 static void mark_phi_and_copy_regs
230 PARAMS ((regset phi_set
));
232 static int rename_equivalent_regs_in_insn
233 PARAMS ((rtx
*ptr
, void *data
));
234 static void rename_equivalent_regs
235 PARAMS ((partition reg_partition
));
237 /* Deal with hard registers. */
238 static int conflicting_hard_regs_p
239 PARAMS ((int reg1
, int reg2
));
241 /* ssa_rename_to maps registers and machine modes to SSA pseudo registers. */
243 /* Find the register associated with REG in the indicated mode. */
246 ssa_rename_to_lookup (reg
)
249 if (!HARD_REGISTER_P (reg
))
250 return ssa_rename_to_pseudo
[REGNO (reg
) - FIRST_PSEUDO_REGISTER
];
252 return ssa_rename_to_hard
[REGNO (reg
)][GET_MODE (reg
)];
255 /* Store a new value mapping REG to R in ssa_rename_to. */
258 ssa_rename_to_insert(reg
, r
)
262 if (!HARD_REGISTER_P (reg
))
263 ssa_rename_to_pseudo
[REGNO (reg
) - FIRST_PSEUDO_REGISTER
] = r
;
265 ssa_rename_to_hard
[REGNO (reg
)][GET_MODE (reg
)] = r
;
268 /* Prepare ssa_rename_from for use. */
271 ssa_rename_from_initialize ()
273 /* We use an arbitrary initial hash table size of 64. */
274 ssa_rename_from_ht
= htab_create (64,
275 &ssa_rename_from_hash_function
,
276 &ssa_rename_from_equal
,
277 &ssa_rename_from_delete
);
280 /* Find the REG entry in ssa_rename_from. Return NULL_RTX if no entry is
284 ssa_rename_from_lookup (reg
)
287 ssa_rename_from_pair srfp
;
288 ssa_rename_from_pair
*answer
;
290 srfp
.original
= NULL_RTX
;
291 answer
= (ssa_rename_from_pair
*)
292 htab_find_with_hash (ssa_rename_from_ht
, (void *) &srfp
, reg
);
293 return (answer
== 0 ? NULL_RTX
: answer
->original
);
296 /* Find the number of the original register specified by REGNO. If
297 the register is a pseudo, return the original register's number.
298 Otherwise, return this register number REGNO. */
301 original_register (regno
)
304 rtx original_rtx
= ssa_rename_from_lookup (regno
);
305 return original_rtx
!= NULL_RTX
? REGNO (original_rtx
) : regno
;
308 /* Add mapping from R to REG to ssa_rename_from even if already present. */
311 ssa_rename_from_insert (reg
, r
)
316 ssa_rename_from_pair
*srfp
= xmalloc (sizeof (ssa_rename_from_pair
));
319 slot
= htab_find_slot_with_hash (ssa_rename_from_ht
, (const void *) srfp
,
322 free ((void *) *slot
);
326 /* Apply the CALLBACK_FUNCTION to each element in ssa_rename_from.
327 CANONICAL_ELEMENTS and REG_PARTITION pass data needed by the only
328 current use of this function. */
331 ssa_rename_from_traverse (callback_function
,
332 canonical_elements
, reg_partition
)
333 htab_trav callback_function
;
334 sbitmap canonical_elements
;
335 partition reg_partition
;
337 struct ssa_rename_from_hash_table_data srfhd
;
338 srfhd
.canonical_elements
= canonical_elements
;
339 srfhd
.reg_partition
= reg_partition
;
340 htab_traverse (ssa_rename_from_ht
, callback_function
, (void *) &srfhd
);
343 /* Destroy ssa_rename_from. */
346 ssa_rename_from_free ()
348 htab_delete (ssa_rename_from_ht
);
351 /* Print the contents of ssa_rename_from. */
353 /* static Avoid erroneous error message. */
355 ssa_rename_from_print ()
357 printf ("ssa_rename_from's hash table contents:\n");
358 htab_traverse (ssa_rename_from_ht
, &ssa_rename_from_print_1
, NULL
);
361 /* Print the contents of the hash table entry SLOT, passing the unused
362 sttribute DATA. Used as a callback function with htab_traverse (). */
365 ssa_rename_from_print_1 (slot
, data
)
367 void *data ATTRIBUTE_UNUSED
;
369 ssa_rename_from_pair
* p
= *slot
;
370 printf ("ssa_rename_from maps pseudo %i to original %i.\n",
371 p
->reg
, REGNO (p
->original
));
375 /* Given a hash entry SRFP, yield a hash value. */
378 ssa_rename_from_hash_function (srfp
)
381 return ((const ssa_rename_from_pair
*) srfp
)->reg
;
384 /* Test whether two hash table entries SRFP1 and SRFP2 are equal. */
387 ssa_rename_from_equal (srfp1
, srfp2
)
391 return ssa_rename_from_hash_function (srfp1
) ==
392 ssa_rename_from_hash_function (srfp2
);
395 /* Delete the hash table entry SRFP. */
398 ssa_rename_from_delete (srfp
)
404 /* Given the SET of a PHI node, return the address of the alternative
405 for predecessor block C. */
408 phi_alternative (set
, c
)
412 rtvec phi_vec
= XVEC (SET_SRC (set
), 0);
415 for (v
= GET_NUM_ELEM (phi_vec
) - 2; v
>= 0; v
-= 2)
416 if (INTVAL (RTVEC_ELT (phi_vec
, v
+ 1)) == c
)
417 return &RTVEC_ELT (phi_vec
, v
);
422 /* Given the SET of a phi node, remove the alternative for predecessor
423 block C. Return non-zero on success, or zero if no alternative is
427 remove_phi_alternative (set
, block
)
431 rtvec phi_vec
= XVEC (SET_SRC (set
), 0);
432 int num_elem
= GET_NUM_ELEM (phi_vec
);
436 for (v
= num_elem
- 2; v
>= 0; v
-= 2)
437 if (INTVAL (RTVEC_ELT (phi_vec
, v
+ 1)) == c
)
439 if (v
< num_elem
- 2)
441 RTVEC_ELT (phi_vec
, v
) = RTVEC_ELT (phi_vec
, num_elem
- 2);
442 RTVEC_ELT (phi_vec
, v
+ 1) = RTVEC_ELT (phi_vec
, num_elem
- 1);
444 PUT_NUM_ELEM (phi_vec
, num_elem
- 2);
451 /* For all registers, find all blocks in which they are set.
453 This is the transform of what would be local kill information that
454 we ought to be getting from flow. */
456 static sbitmap
*fe_evals
;
457 static int fe_current_bb
;
460 find_evaluations_1 (dest
, set
, data
)
462 rtx set ATTRIBUTE_UNUSED
;
463 void *data ATTRIBUTE_UNUSED
;
465 if (GET_CODE (dest
) == REG
466 && CONVERT_REGISTER_TO_SSA_P (REGNO (dest
)))
467 SET_BIT (fe_evals
[REGNO (dest
)], fe_current_bb
);
471 find_evaluations (evals
, nregs
)
477 sbitmap_vector_zero (evals
, nregs
);
480 FOR_EACH_BB_REVERSE (bb
)
484 fe_current_bb
= bb
->index
;
490 note_stores (PATTERN (p
), find_evaluations_1
, NULL
);
499 /* Computing the Dominance Frontier:
501 As decribed in Morgan, section 3.5, this may be done simply by
502 walking the dominator tree bottom-up, computing the frontier for
503 the children before the parent. When considering a block B,
506 (1) A flow graph edge leaving B that does not lead to a child
507 of B in the dominator tree must be a block that is either equal
508 to B or not dominated by B. Such blocks belong in the frontier
511 (2) Consider a block X in the frontier of one of the children C
512 of B. If X is not equal to B and is not dominated by B, it
513 is in the frontier of B.
517 compute_dominance_frontiers_1 (frontiers
, idom
, bb
, done
)
523 basic_block b
= BASIC_BLOCK (bb
);
528 sbitmap_zero (frontiers
[bb
]);
530 /* Do the frontier of the children first. Not all children in the
531 dominator tree (blocks dominated by this one) are children in the
532 CFG, so check all blocks. */
534 if (get_immediate_dominator (idom
, c
)->index
== bb
535 && ! TEST_BIT (done
, c
->index
))
536 compute_dominance_frontiers_1 (frontiers
, idom
, c
->index
, done
);
538 /* Find blocks conforming to rule (1) above. */
539 for (e
= b
->succ
; e
; e
= e
->succ_next
)
541 if (e
->dest
== EXIT_BLOCK_PTR
)
543 if (get_immediate_dominator (idom
, e
->dest
)->index
!= bb
)
544 SET_BIT (frontiers
[bb
], e
->dest
->index
);
547 /* Find blocks conforming to rule (2). */
549 if (get_immediate_dominator (idom
, c
)->index
== bb
)
552 EXECUTE_IF_SET_IN_SBITMAP (frontiers
[c
->index
], 0, x
,
554 if (get_immediate_dominator (idom
, BASIC_BLOCK (x
))->index
!= bb
)
555 SET_BIT (frontiers
[bb
], x
);
561 compute_dominance_frontiers (frontiers
, idom
)
565 sbitmap done
= sbitmap_alloc (last_basic_block
);
568 compute_dominance_frontiers_1 (frontiers
, idom
, 0, done
);
573 /* Computing the Iterated Dominance Frontier:
575 This is the set of merge points for a given register.
577 This is not particularly intuitive. See section 7.1 of Morgan, in
578 particular figures 7.3 and 7.4 and the immediately surrounding text.
582 compute_iterated_dominance_frontiers (idfs
, frontiers
, evals
, nregs
)
591 worklist
= sbitmap_alloc (last_basic_block
);
593 for (reg
= 0; reg
< nregs
; ++reg
)
595 sbitmap idf
= idfs
[reg
];
598 /* Start the iterative process by considering those blocks that
599 evaluate REG. We'll add their dominance frontiers to the
600 IDF, and then consider the blocks we just added. */
601 sbitmap_copy (worklist
, evals
[reg
]);
603 /* Morgan's algorithm is incorrect here. Blocks that evaluate
604 REG aren't necessarily in REG's IDF. Start with an empty IDF. */
607 /* Iterate until the worklist is empty. */
612 EXECUTE_IF_SET_IN_SBITMAP (worklist
, 0, b
,
614 RESET_BIT (worklist
, b
);
615 /* For each block on the worklist, add to the IDF all
616 blocks on its dominance frontier that aren't already
617 on the IDF. Every block that's added is also added
619 sbitmap_union_of_diff (worklist
, worklist
, frontiers
[b
], idf
);
620 sbitmap_a_or_b (idf
, idf
, frontiers
[b
]);
627 sbitmap_free (worklist
);
631 fprintf (rtl_dump_file
,
632 "Iterated dominance frontier: %d passes on %d regs.\n",
637 /* Insert the phi nodes. */
640 insert_phi_node (regno
, bb
)
643 basic_block b
= BASIC_BLOCK (bb
);
651 /* Find out how many predecessors there are. */
652 for (e
= b
->pred
, npred
= 0; e
; e
= e
->pred_next
)
653 if (e
->src
!= ENTRY_BLOCK_PTR
)
656 /* If this block has no "interesting" preds, then there is nothing to
657 do. Consider a block that only has the entry block as a pred. */
661 /* This is the register to which the phi function will be assigned. */
662 reg
= regno_reg_rtx
[regno
];
664 /* Construct the arguments to the PHI node. The use of pc_rtx is just
665 a placeholder; we'll insert the proper value in rename_registers. */
666 vec
= rtvec_alloc (npred
* 2);
667 for (e
= b
->pred
, i
= 0; e
; e
= e
->pred_next
, i
+= 2)
668 if (e
->src
!= ENTRY_BLOCK_PTR
)
670 RTVEC_ELT (vec
, i
+ 0) = pc_rtx
;
671 RTVEC_ELT (vec
, i
+ 1) = GEN_INT (e
->src
->index
);
674 phi
= gen_rtx_PHI (VOIDmode
, vec
);
675 phi
= gen_rtx_SET (VOIDmode
, reg
, phi
);
677 insn
= first_insn_after_basic_block_note (b
);
678 end_p
= PREV_INSN (insn
) == b
->end
;
679 emit_insn_before (phi
, insn
);
681 b
->end
= PREV_INSN (insn
);
685 insert_phi_nodes (idfs
, evals
, nregs
)
687 sbitmap
*evals ATTRIBUTE_UNUSED
;
692 for (reg
= 0; reg
< nregs
; ++reg
)
693 if (CONVERT_REGISTER_TO_SSA_P (reg
))
696 EXECUTE_IF_SET_IN_SBITMAP (idfs
[reg
], 0, b
,
698 if (REGNO_REG_SET_P (BASIC_BLOCK (b
)->global_live_at_start
, reg
))
699 insert_phi_node (reg
, b
);
704 /* Rename the registers to conform to SSA.
706 This is essentially the algorithm presented in Figure 7.8 of Morgan,
707 with a few changes to reduce pattern search time in favour of a bit
708 more memory usage. */
710 /* One of these is created for each set. It will live in a list local
711 to its basic block for the duration of that block's processing. */
712 struct rename_set_data
714 struct rename_set_data
*next
;
715 /* This is the SET_DEST of the (first) SET that sets the REG. */
717 /* This is what used to be at *REG_LOC. */
719 /* This is the REG that will replace OLD_REG. It's set only
720 when the rename data is moved onto the DONE_RENAMES queue. */
722 /* This is what to restore ssa_rename_to_lookup (old_reg) to. It is
723 usually the previous contents of ssa_rename_to_lookup (old_reg). */
725 /* This is the insn that contains all the SETs of the REG. */
729 /* This struct is used to pass information to callback functions while
730 renaming registers. */
731 struct rename_context
733 struct rename_set_data
*new_renames
;
734 struct rename_set_data
*done_renames
;
738 /* Queue the rename of *REG_LOC. */
740 create_delayed_rename (c
, reg_loc
)
741 struct rename_context
*c
;
744 struct rename_set_data
*r
;
745 r
= (struct rename_set_data
*) xmalloc (sizeof(*r
));
747 if (GET_CODE (*reg_loc
) != REG
748 || !CONVERT_REGISTER_TO_SSA_P (REGNO (*reg_loc
)))
751 r
->reg_loc
= reg_loc
;
752 r
->old_reg
= *reg_loc
;
753 r
->prev_reg
= ssa_rename_to_lookup(r
->old_reg
);
754 r
->set_insn
= c
->current_insn
;
755 r
->next
= c
->new_renames
;
759 /* This is part of a rather ugly hack to allow the pre-ssa regno to be
760 reused. If, during processing, a register has not yet been touched,
761 ssa_rename_to[regno][machno] will be NULL. Now, in the course of pushing
762 and popping values from ssa_rename_to, when we would ordinarily
763 pop NULL back in, we pop RENAME_NO_RTX. We treat this exactly the
764 same as NULL, except that it signals that the original regno has
765 already been reused. */
766 #define RENAME_NO_RTX pc_rtx
768 /* Move all the entries from NEW_RENAMES onto DONE_RENAMES by
769 applying all the renames on NEW_RENAMES. */
772 apply_delayed_renames (c
)
773 struct rename_context
*c
;
775 struct rename_set_data
*r
;
776 struct rename_set_data
*last_r
= NULL
;
778 for (r
= c
->new_renames
; r
!= NULL
; r
= r
->next
)
782 /* Failure here means that someone has a PARALLEL that sets
783 a register twice (bad!). */
784 if (ssa_rename_to_lookup (r
->old_reg
) != r
->prev_reg
)
786 /* Failure here means we have changed REG_LOC before applying
788 /* For the first set we come across, reuse the original regno. */
789 if (r
->prev_reg
== NULL_RTX
&& !HARD_REGISTER_P (r
->old_reg
))
791 r
->new_reg
= r
->old_reg
;
792 /* We want to restore RENAME_NO_RTX rather than NULL_RTX. */
793 r
->prev_reg
= RENAME_NO_RTX
;
796 r
->new_reg
= gen_reg_rtx (GET_MODE (r
->old_reg
));
797 new_regno
= REGNO (r
->new_reg
);
798 ssa_rename_to_insert (r
->old_reg
, r
->new_reg
);
800 if (new_regno
>= (int) ssa_definition
->num_elements
)
802 int new_limit
= new_regno
* 5 / 4;
803 VARRAY_GROW (ssa_definition
, new_limit
);
806 VARRAY_RTX (ssa_definition
, new_regno
) = r
->set_insn
;
807 ssa_rename_from_insert (new_regno
, r
->old_reg
);
812 last_r
->next
= c
->done_renames
;
813 c
->done_renames
= c
->new_renames
;
814 c
->new_renames
= NULL
;
818 /* Part one of the first step of rename_block, called through for_each_rtx.
819 Mark pseudos that are set for later update. Transform uses of pseudos. */
822 rename_insn_1 (ptr
, data
)
827 struct rename_context
*context
= data
;
832 switch (GET_CODE (x
))
836 rtx
*destp
= &SET_DEST (x
);
837 rtx dest
= SET_DEST (x
);
839 /* An assignment to a paradoxical SUBREG does not read from
840 the destination operand, and thus does not need to be
841 wrapped into a SEQUENCE when translating into SSA form.
842 We merely strip off the SUBREG and proceed normally for
844 if (GET_CODE (dest
) == SUBREG
845 && (GET_MODE_SIZE (GET_MODE (dest
))
846 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest
))))
847 && GET_CODE (SUBREG_REG (dest
)) == REG
848 && CONVERT_REGISTER_TO_SSA_P (REGNO (SUBREG_REG (dest
))))
850 destp
= &XEXP (dest
, 0);
851 dest
= XEXP (dest
, 0);
854 /* Some SETs also use the REG specified in their LHS.
855 These can be detected by the presence of
856 STRICT_LOW_PART, SUBREG, SIGN_EXTRACT, and ZERO_EXTRACT
857 in the LHS. Handle these by changing
858 (set (subreg (reg foo)) ...)
860 (sequence [(set (reg foo_1) (reg foo))
861 (set (subreg (reg foo_1)) ...)])
863 FIXME: Much of the time this is too much. For some constructs
864 we know that the output register is strictly an output
865 (paradoxical SUBREGs and some libcalls for example).
867 For those cases we are better off not making the false
869 if (GET_CODE (dest
) == STRICT_LOW_PART
870 || GET_CODE (dest
) == SUBREG
871 || GET_CODE (dest
) == SIGN_EXTRACT
872 || GET_CODE (dest
) == ZERO_EXTRACT
)
877 while (GET_CODE (reg
) == STRICT_LOW_PART
878 || GET_CODE (reg
) == SUBREG
879 || GET_CODE (reg
) == SIGN_EXTRACT
880 || GET_CODE (reg
) == ZERO_EXTRACT
)
883 if (GET_CODE (reg
) == REG
884 && CONVERT_REGISTER_TO_SSA_P (REGNO (reg
)))
886 /* Generate (set reg reg), and do renaming on it so
887 that it becomes (set reg_1 reg_0), and we will
888 replace reg with reg_1 in the SUBREG. */
890 struct rename_set_data
*saved_new_renames
;
891 saved_new_renames
= context
->new_renames
;
892 context
->new_renames
= NULL
;
893 i
= emit_insn (gen_rtx_SET (VOIDmode
, reg
, reg
));
894 for_each_rtx (&i
, rename_insn_1
, data
);
895 apply_delayed_renames (context
);
896 context
->new_renames
= saved_new_renames
;
899 else if (GET_CODE (dest
) == REG
900 && CONVERT_REGISTER_TO_SSA_P (REGNO (dest
)))
902 /* We found a genuine set of an interesting register. Tag
903 it so that we can create a new name for it after we finish
904 processing this insn. */
906 create_delayed_rename (context
, destp
);
908 /* Since we do not wish to (directly) traverse the
909 SET_DEST, recurse through for_each_rtx for the SET_SRC
911 if (GET_CODE (x
) == SET
)
912 for_each_rtx (&SET_SRC (x
), rename_insn_1
, data
);
916 /* Otherwise, this was not an interesting destination. Continue
917 on, marking uses as normal. */
922 if (CONVERT_REGISTER_TO_SSA_P (REGNO (x
))
923 && REGNO (x
) < ssa_max_reg_num
)
925 rtx new_reg
= ssa_rename_to_lookup (x
);
927 if (new_reg
!= RENAME_NO_RTX
)
929 if (new_reg
!= NULL_RTX
)
931 if (GET_MODE (x
) != GET_MODE (new_reg
))
937 /* Undefined value used, rename it to a new pseudo register so
938 that it cannot conflict with an existing register */
939 *ptr
= gen_reg_rtx (GET_MODE(x
));
946 /* There is considerable debate on how CLOBBERs ought to be
947 handled in SSA. For now, we're keeping the CLOBBERs, which
948 means that we don't really have SSA form. There are a couple
949 of proposals for how to fix this problem, but neither is
952 rtx dest
= XCEXP (x
, 0, CLOBBER
);
955 if (CONVERT_REGISTER_TO_SSA_P (REGNO (dest
))
956 && REGNO (dest
) < ssa_max_reg_num
)
958 rtx new_reg
= ssa_rename_to_lookup (dest
);
959 if (new_reg
!= NULL_RTX
&& new_reg
!= RENAME_NO_RTX
)
960 XCEXP (x
, 0, CLOBBER
) = new_reg
;
962 /* Stop traversing. */
966 /* Continue traversing. */
971 /* Never muck with the phi. We do that elsewhere, special-like. */
975 /* Anything else, continue traversing. */
983 rtx first_insn
= get_insns ();
989 /* Count the insns in the chain. */
991 for (tem
= first_insn
; tem
; tem
= NEXT_INSN (tem
))
994 result
= gen_rtx_SEQUENCE (VOIDmode
, rtvec_alloc (len
));
996 for (i
= 0, tem
= first_insn
; tem
; tem
= NEXT_INSN (tem
), i
++)
997 XVECEXP (result
, 0, i
) = tem
;
1003 rename_block (bb
, idom
)
1005 dominance_info idom
;
1007 basic_block b
= BASIC_BLOCK (bb
);
1009 rtx insn
, next
, last
;
1010 struct rename_set_data
*set_data
= NULL
;
1013 /* Step One: Walk the basic block, adding new names for sets and
1023 struct rename_context context
;
1024 context
.done_renames
= set_data
;
1025 context
.new_renames
= NULL
;
1026 context
.current_insn
= insn
;
1029 for_each_rtx (&PATTERN (insn
), rename_insn_1
, &context
);
1030 for_each_rtx (®_NOTES (insn
), rename_insn_1
, &context
);
1032 /* Sometimes, we end up with a sequence of insns that
1033 SSA needs to treat as a single insn. Wrap these in a
1034 SEQUENCE. (Any notes now get attached to the SEQUENCE,
1035 not to the old version inner insn.) */
1036 if (get_insns () != NULL_RTX
)
1041 emit (PATTERN (insn
));
1042 seq
= gen_sequence ();
1043 /* We really want a SEQUENCE of SETs, not a SEQUENCE
1045 for (i
= 0; i
< XVECLEN (seq
, 0); i
++)
1046 XVECEXP (seq
, 0, i
) = PATTERN (XVECEXP (seq
, 0, i
));
1047 PATTERN (insn
) = seq
;
1051 apply_delayed_renames (&context
);
1052 set_data
= context
.done_renames
;
1055 next
= NEXT_INSN (insn
);
1057 while (insn
!= last
);
1059 /* Step Two: Update the phi nodes of this block's successors. */
1061 for (e
= b
->succ
; e
; e
= e
->succ_next
)
1063 if (e
->dest
== EXIT_BLOCK_PTR
)
1066 insn
= first_insn_after_basic_block_note (e
->dest
);
1068 while (PHI_NODE_P (insn
))
1070 rtx phi
= PATTERN (insn
);
1073 /* Find out which of our outgoing registers this node is
1074 intended to replace. Note that if this is not the first PHI
1075 node to have been created for this register, we have to
1076 jump through rename links to figure out which register
1077 we're talking about. This can easily be recognized by
1078 noting that the regno is new to this pass. */
1079 reg
= SET_DEST (phi
);
1080 if (REGNO (reg
) >= ssa_max_reg_num
)
1081 reg
= ssa_rename_from_lookup (REGNO (reg
));
1082 if (reg
== NULL_RTX
)
1084 reg
= ssa_rename_to_lookup (reg
);
1086 /* It is possible for the variable to be uninitialized on
1087 edges in. Reduce the arity of the PHI so that we don't
1088 consider those edges. */
1089 if (reg
== NULL
|| reg
== RENAME_NO_RTX
)
1091 if (! remove_phi_alternative (phi
, b
))
1096 /* When we created the PHI nodes, we did not know what mode
1097 the register should be. Now that we've found an original,
1098 we can fill that in. */
1099 if (GET_MODE (SET_DEST (phi
)) == VOIDmode
)
1100 PUT_MODE (SET_DEST (phi
), GET_MODE (reg
));
1101 else if (GET_MODE (SET_DEST (phi
)) != GET_MODE (reg
))
1104 *phi_alternative (phi
, bb
) = reg
;
1107 insn
= NEXT_INSN (insn
);
1111 /* Step Three: Do the same to the children of this block in
1115 if (get_immediate_dominator (idom
, c
)->index
== bb
)
1116 rename_block (c
->index
, idom
);
1118 /* Step Four: Update the sets to refer to their new register,
1119 and restore ssa_rename_to to its previous state. */
1123 struct rename_set_data
*next
;
1124 rtx old_reg
= *set_data
->reg_loc
;
1126 if (*set_data
->reg_loc
!= set_data
->old_reg
)
1128 *set_data
->reg_loc
= set_data
->new_reg
;
1130 ssa_rename_to_insert (old_reg
, set_data
->prev_reg
);
1132 next
= set_data
->next
;
1139 rename_registers (nregs
, idom
)
1141 dominance_info idom
;
1143 VARRAY_RTX_INIT (ssa_definition
, nregs
* 3, "ssa_definition");
1144 ssa_rename_from_initialize ();
1146 ssa_rename_to_pseudo
= (rtx
*) alloca (nregs
* sizeof(rtx
));
1147 memset ((char *) ssa_rename_to_pseudo
, 0, nregs
* sizeof(rtx
));
1148 memset ((char *) ssa_rename_to_hard
, 0,
1149 FIRST_PSEUDO_REGISTER
* NUM_MACHINE_MODES
* sizeof (rtx
));
1151 rename_block (0, idom
);
1153 /* ??? Update basic_block_live_at_start, and other flow info
1156 ssa_rename_to_pseudo
= NULL
;
1159 /* The main entry point for moving to SSA. */
1164 /* Element I is the set of blocks that set register I. */
1167 /* Dominator bitmaps. */
1171 /* Element I is the immediate dominator of block I. */
1172 dominance_info idom
;
1178 /* Don't do it twice. */
1182 /* Need global_live_at_{start,end} up to date. Do not remove any
1183 dead code. We'll let the SSA optimizers do that. */
1184 life_analysis (get_insns (), NULL
, 0);
1186 idom
= calculate_dominance_info (CDI_DOMINATORS
);
1190 fputs (";; Immediate Dominators:\n", rtl_dump_file
);
1192 fprintf (rtl_dump_file
, ";\t%3d = %3d\n", bb
->index
,
1193 get_immediate_dominator (idom
, bb
)->index
);
1194 fflush (rtl_dump_file
);
1197 /* Compute dominance frontiers. */
1199 dfs
= sbitmap_vector_alloc (last_basic_block
, last_basic_block
);
1200 compute_dominance_frontiers (dfs
, idom
);
1204 dump_sbitmap_vector (rtl_dump_file
, ";; Dominance Frontiers:",
1205 "; Basic Block", dfs
, last_basic_block
);
1206 fflush (rtl_dump_file
);
1209 /* Compute register evaluations. */
1211 ssa_max_reg_num
= max_reg_num ();
1212 nregs
= ssa_max_reg_num
;
1213 evals
= sbitmap_vector_alloc (nregs
, last_basic_block
);
1214 find_evaluations (evals
, nregs
);
1216 /* Compute the iterated dominance frontier for each register. */
1218 idfs
= sbitmap_vector_alloc (nregs
, last_basic_block
);
1219 compute_iterated_dominance_frontiers (idfs
, dfs
, evals
, nregs
);
1223 dump_sbitmap_vector (rtl_dump_file
, ";; Iterated Dominance Frontiers:",
1224 "; Register", idfs
, nregs
);
1225 fflush (rtl_dump_file
);
1228 /* Insert the phi nodes. */
1230 insert_phi_nodes (idfs
, evals
, nregs
);
1232 /* Rename the registers to satisfy SSA. */
1234 rename_registers (nregs
, idom
);
1236 /* All done! Clean up and go home. */
1238 sbitmap_vector_free (dfs
);
1239 sbitmap_vector_free (evals
);
1240 sbitmap_vector_free (idfs
);
1243 reg_scan (get_insns (), max_reg_num (), 1);
1244 free_dominance_info (idom
);
1247 /* REG is the representative temporary of its partition. Add it to the
1248 set of nodes to be processed, if it hasn't been already. Return the
1249 index of this register in the node set. */
1252 ephi_add_node (reg
, nodes
, n_nodes
)
1257 for (i
= *n_nodes
- 1; i
>= 0; --i
)
1258 if (REGNO (reg
) == REGNO (nodes
[i
]))
1261 nodes
[i
= (*n_nodes
)++] = reg
;
1265 /* Part one of the topological sort. This is a forward (downward) search
1266 through the graph collecting a stack of nodes to process. Assuming no
1267 cycles, the nodes at top of the stack when we are finished will have
1268 no other dependencies. */
1271 ephi_forward (t
, visited
, succ
, tstack
)
1279 SET_BIT (visited
, t
);
1281 EXECUTE_IF_SET_IN_SBITMAP (succ
[t
], 0, s
,
1283 if (! TEST_BIT (visited
, s
))
1284 tstack
= ephi_forward (s
, visited
, succ
, tstack
);
1291 /* Part two of the topological sort. The is a backward search through
1292 a cycle in the graph, copying the data forward as we go. */
1295 ephi_backward (t
, visited
, pred
, nodes
)
1297 sbitmap visited
, *pred
;
1302 SET_BIT (visited
, t
);
1304 EXECUTE_IF_SET_IN_SBITMAP (pred
[t
], 0, p
,
1306 if (! TEST_BIT (visited
, p
))
1308 ephi_backward (p
, visited
, pred
, nodes
);
1309 emit_move_insn (nodes
[p
], nodes
[t
]);
1314 /* Part two of the topological sort. Create the copy for a register
1315 and any cycle of which it is a member. */
1318 ephi_create (t
, visited
, pred
, succ
, nodes
)
1320 sbitmap visited
, *pred
, *succ
;
1323 rtx reg_u
= NULL_RTX
;
1324 int unvisited_predecessors
= 0;
1327 /* Iterate through the predecessor list looking for unvisited nodes.
1328 If there are any, we have a cycle, and must deal with that. At
1329 the same time, look for a visited predecessor. If there is one,
1330 we won't need to create a temporary. */
1332 EXECUTE_IF_SET_IN_SBITMAP (pred
[t
], 0, p
,
1334 if (! TEST_BIT (visited
, p
))
1335 unvisited_predecessors
= 1;
1340 if (unvisited_predecessors
)
1342 /* We found a cycle. Copy out one element of the ring (if necessary),
1343 then traverse the ring copying as we go. */
1347 reg_u
= gen_reg_rtx (GET_MODE (nodes
[t
]));
1348 emit_move_insn (reg_u
, nodes
[t
]);
1351 EXECUTE_IF_SET_IN_SBITMAP (pred
[t
], 0, p
,
1353 if (! TEST_BIT (visited
, p
))
1355 ephi_backward (p
, visited
, pred
, nodes
);
1356 emit_move_insn (nodes
[p
], reg_u
);
1362 /* No cycle. Just copy the value from a successor. */
1365 EXECUTE_IF_SET_IN_SBITMAP (succ
[t
], 0, s
,
1367 SET_BIT (visited
, t
);
1368 emit_move_insn (nodes
[t
], nodes
[s
]);
1374 /* Convert the edge to normal form. */
1377 eliminate_phi (e
, reg_partition
)
1379 partition reg_partition
;
1382 sbitmap
*pred
, *succ
;
1385 int *stack
, *tstack
;
1389 /* Collect an upper bound on the number of registers needing processing. */
1391 insn
= first_insn_after_basic_block_note (e
->dest
);
1394 while (PHI_NODE_P (insn
))
1396 insn
= next_nonnote_insn (insn
);
1403 /* Build the auxiliary graph R(B).
1405 The nodes of the graph are the members of the register partition
1406 present in Phi(B). There is an edge from FIND(T0)->FIND(T1) for
1407 each T0 = PHI(...,T1,...), where T1 is for the edge from block C. */
1409 nodes
= (rtx
*) alloca (n_nodes
* sizeof(rtx
));
1410 pred
= sbitmap_vector_alloc (n_nodes
, n_nodes
);
1411 succ
= sbitmap_vector_alloc (n_nodes
, n_nodes
);
1412 sbitmap_vector_zero (pred
, n_nodes
);
1413 sbitmap_vector_zero (succ
, n_nodes
);
1415 insn
= first_insn_after_basic_block_note (e
->dest
);
1418 for (; PHI_NODE_P (insn
); insn
= next_nonnote_insn (insn
))
1420 rtx
* preg
= phi_alternative (PATTERN (insn
), e
->src
->index
);
1421 rtx tgt
= SET_DEST (PATTERN (insn
));
1424 /* There may be no phi alternative corresponding to this edge.
1425 This indicates that the phi variable is undefined along this
1431 if (GET_CODE (reg
) != REG
|| GET_CODE (tgt
) != REG
)
1434 reg
= regno_reg_rtx
[partition_find (reg_partition
, REGNO (reg
))];
1435 tgt
= regno_reg_rtx
[partition_find (reg_partition
, REGNO (tgt
))];
1436 /* If the two registers are already in the same partition,
1437 nothing will need to be done. */
1442 ireg
= ephi_add_node (reg
, nodes
, &n_nodes
);
1443 itgt
= ephi_add_node (tgt
, nodes
, &n_nodes
);
1445 SET_BIT (pred
[ireg
], itgt
);
1446 SET_BIT (succ
[itgt
], ireg
);
1453 /* Begin a topological sort of the graph. */
1455 visited
= sbitmap_alloc (n_nodes
);
1456 sbitmap_zero (visited
);
1458 tstack
= stack
= (int *) alloca (n_nodes
* sizeof (int));
1460 for (i
= 0; i
< n_nodes
; ++i
)
1461 if (! TEST_BIT (visited
, i
))
1462 tstack
= ephi_forward (i
, visited
, succ
, tstack
);
1464 sbitmap_zero (visited
);
1466 /* As we find a solution to the tsort, collect the implementation
1467 insns in a sequence. */
1470 while (tstack
!= stack
)
1473 if (! TEST_BIT (visited
, i
))
1474 ephi_create (i
, visited
, pred
, succ
, nodes
);
1477 insn
= get_insns ();
1479 insert_insn_on_edge (insn
, e
);
1481 fprintf (rtl_dump_file
, "Emitting copy on edge (%d,%d)\n",
1482 e
->src
->index
, e
->dest
->index
);
1484 sbitmap_free (visited
);
1486 sbitmap_vector_free (pred
);
1487 sbitmap_vector_free (succ
);
1490 /* For basic block B, consider all phi insns which provide an
1491 alternative corresponding to an incoming abnormal critical edge.
1492 Place the phi alternative corresponding to that abnormal critical
1493 edge in the same register class as the destination of the set.
1495 From Morgan, p. 178:
1497 For each abnormal critical edge (C, B),
1498 if T0 = phi (T1, ..., Ti, ..., Tm) is a phi node in B,
1499 and C is the ith predecessor of B,
1500 then T0 and Ti must be equivalent.
1502 Return non-zero iff any such cases were found for which the two
1503 regs were not already in the same class. */
1506 make_regs_equivalent_over_bad_edges (bb
, reg_partition
)
1508 partition reg_partition
;
1511 basic_block b
= BASIC_BLOCK (bb
);
1514 /* Advance to the first phi node. */
1515 phi
= first_insn_after_basic_block_note (b
);
1517 /* Scan all the phi nodes. */
1520 phi
= next_nonnote_insn (phi
))
1524 rtx set
= PATTERN (phi
);
1525 rtx tgt
= SET_DEST (set
);
1527 /* The set target is expected to be an SSA register. */
1528 if (GET_CODE (tgt
) != REG
1529 || !CONVERT_REGISTER_TO_SSA_P (REGNO (tgt
)))
1531 tgt_regno
= REGNO (tgt
);
1533 /* Scan incoming abnormal critical edges. */
1534 for (e
= b
->pred
; e
; e
= e
->pred_next
)
1535 if ((e
->flags
& EDGE_ABNORMAL
) && EDGE_CRITICAL_P (e
))
1537 rtx
*alt
= phi_alternative (set
, e
->src
->index
);
1540 /* If there is no alternative corresponding to this edge,
1541 the value is undefined along the edge, so just go on. */
1545 /* The phi alternative is expected to be an SSA register. */
1546 if (GET_CODE (*alt
) != REG
1547 || !CONVERT_REGISTER_TO_SSA_P (REGNO (*alt
)))
1549 alt_regno
= REGNO (*alt
);
1551 /* If the set destination and the phi alternative aren't
1552 already in the same class... */
1553 if (partition_find (reg_partition
, tgt_regno
)
1554 != partition_find (reg_partition
, alt_regno
))
1556 /* ... make them such. */
1557 if (conflicting_hard_regs_p (tgt_regno
, alt_regno
))
1558 /* It is illegal to unify a hard register with a
1559 different register. */
1562 partition_union (reg_partition
,
1563 tgt_regno
, alt_regno
);
1572 /* Consider phi insns in basic block BB pairwise. If the set target
1573 of both isns are equivalent pseudos, make the corresponding phi
1574 alternatives in each phi corresponding equivalent.
1576 Return nonzero if any new register classes were unioned. */
1579 make_equivalent_phi_alternatives_equivalent (bb
, reg_partition
)
1581 partition reg_partition
;
1584 basic_block b
= BASIC_BLOCK (bb
);
1587 /* Advance to the first phi node. */
1588 phi
= first_insn_after_basic_block_note (b
);
1590 /* Scan all the phi nodes. */
1593 phi
= next_nonnote_insn (phi
))
1595 rtx set
= PATTERN (phi
);
1596 /* The regno of the destination of the set. */
1597 int tgt_regno
= REGNO (SET_DEST (PATTERN (phi
)));
1599 rtx phi2
= next_nonnote_insn (phi
);
1601 /* Scan all phi nodes following this one. */
1604 phi2
= next_nonnote_insn (phi2
))
1606 rtx set2
= PATTERN (phi2
);
1607 /* The regno of the destination of the set. */
1608 int tgt2_regno
= REGNO (SET_DEST (set2
));
1610 /* Are the set destinations equivalent regs? */
1611 if (partition_find (reg_partition
, tgt_regno
) ==
1612 partition_find (reg_partition
, tgt2_regno
))
1615 /* Scan over edges. */
1616 for (e
= b
->pred
; e
; e
= e
->pred_next
)
1618 int pred_block
= e
->src
->index
;
1619 /* Identify the phi alternatives from both phi
1620 nodes corresponding to this edge. */
1621 rtx
*alt
= phi_alternative (set
, pred_block
);
1622 rtx
*alt2
= phi_alternative (set2
, pred_block
);
1624 /* If one of the phi nodes doesn't have a
1625 corresponding alternative, just skip it. */
1626 if (alt
== 0 || alt2
== 0)
1629 /* Both alternatives should be SSA registers. */
1630 if (GET_CODE (*alt
) != REG
1631 || !CONVERT_REGISTER_TO_SSA_P (REGNO (*alt
)))
1633 if (GET_CODE (*alt2
) != REG
1634 || !CONVERT_REGISTER_TO_SSA_P (REGNO (*alt2
)))
1637 /* If the alternatives aren't already in the same
1639 if (partition_find (reg_partition
, REGNO (*alt
))
1640 != partition_find (reg_partition
, REGNO (*alt2
)))
1642 /* ... make them so. */
1643 if (conflicting_hard_regs_p (REGNO (*alt
), REGNO (*alt2
)))
1644 /* It is illegal to unify a hard register with
1645 a different register. */
1648 partition_union (reg_partition
,
1649 REGNO (*alt
), REGNO (*alt2
));
1660 /* Compute a conservative partition of outstanding pseudo registers.
1661 See Morgan 7.3.1. */
1664 compute_conservative_reg_partition ()
1669 /* We don't actually work with hard registers, but it's easier to
1670 carry them around anyway rather than constantly doing register
1671 number arithmetic. */
1673 partition_new (ssa_definition
->num_elements
);
1675 /* The first priority is to make sure registers that might have to
1676 be copied on abnormal critical edges are placed in the same
1677 partition. This saves us from having to split abnormal critical
1679 FOR_EACH_BB_REVERSE (bb
)
1680 changed
+= make_regs_equivalent_over_bad_edges (bb
->index
, p
);
1682 /* Now we have to insure that corresponding arguments of phi nodes
1683 assigning to corresponding regs are equivalent. Iterate until
1688 FOR_EACH_BB_REVERSE (bb
)
1689 changed
+= make_equivalent_phi_alternatives_equivalent (bb
->index
, p
);
1695 /* The following functions compute a register partition that attempts
1696 to eliminate as many reg copies and phi node copies as possible by
1697 coalescing registers. This is the strategy:
1699 1. As in the conservative case, the top priority is to coalesce
1700 registers that otherwise would cause copies to be placed on
1701 abnormal critical edges (which isn't possible).
1703 2. Figure out which regs are involved (in the LHS or RHS) of
1704 copies and phi nodes. Compute conflicts among these regs.
1706 3. Walk around the instruction stream, placing two regs in the
1707 same class of the partition if one appears on the LHS and the
1708 other on the RHS of a copy or phi node and the two regs don't
1709 conflict. The conflict information of course needs to be
1712 4. If anything has changed, there may be new opportunities to
1713 coalesce regs, so go back to 2.
1716 /* If REG1 and REG2 don't conflict in CONFLICTS, place them in the
1717 same class of partition P, if they aren't already. Update
1718 CONFLICTS appropriately.
1720 Returns one if REG1 and REG2 were placed in the same class but were
1721 not previously; zero otherwise.
1723 See Morgan figure 11.15. */
1726 coalesce_if_unconflicting (p
, conflicts
, reg1
, reg2
)
1728 conflict_graph conflicts
;
1734 /* Work only on SSA registers. */
1735 if (!CONVERT_REGISTER_TO_SSA_P (reg1
) || !CONVERT_REGISTER_TO_SSA_P (reg2
))
1738 /* Find the canonical regs for the classes containing REG1 and
1740 reg1
= partition_find (p
, reg1
);
1741 reg2
= partition_find (p
, reg2
);
1743 /* If they're already in the same class, there's nothing to do. */
1747 /* If the regs conflict, our hands are tied. */
1748 if (conflicting_hard_regs_p (reg1
, reg2
) ||
1749 conflict_graph_conflict_p (conflicts
, reg1
, reg2
))
1752 /* We're good to go. Put the regs in the same partition. */
1753 partition_union (p
, reg1
, reg2
);
1755 /* Find the new canonical reg for the merged class. */
1756 reg
= partition_find (p
, reg1
);
1758 /* Merge conflicts from the two previous classes. */
1759 conflict_graph_merge_regs (conflicts
, reg
, reg1
);
1760 conflict_graph_merge_regs (conflicts
, reg
, reg2
);
1765 /* For each register copy insn in basic block BB, place the LHS and
1766 RHS regs in the same class in partition P if they do not conflict
1767 according to CONFLICTS.
1769 Returns the number of changes that were made to P.
1771 See Morgan figure 11.14. */
1774 coalesce_regs_in_copies (bb
, p
, conflicts
)
1777 conflict_graph conflicts
;
1783 /* Scan the instruction stream of the block. */
1784 for (insn
= bb
->head
; insn
!= end
; insn
= NEXT_INSN (insn
))
1790 /* If this isn't a set insn, go to the next insn. */
1791 if (GET_CODE (insn
) != INSN
)
1793 pattern
= PATTERN (insn
);
1794 if (GET_CODE (pattern
) != SET
)
1797 src
= SET_SRC (pattern
);
1798 dest
= SET_DEST (pattern
);
1800 /* We're only looking for copies. */
1801 if (GET_CODE (src
) != REG
|| GET_CODE (dest
) != REG
)
1804 /* Coalesce only if the reg modes are the same. As long as
1805 each reg's rtx is unique, it can have only one mode, so two
1806 pseudos of different modes can't be coalesced into one.
1808 FIXME: We can probably get around this by inserting SUBREGs
1809 where appropriate, but for now we don't bother. */
1810 if (GET_MODE (src
) != GET_MODE (dest
))
1813 /* Found a copy; see if we can use the same reg for both the
1814 source and destination (and thus eliminate the copy,
1816 changed
+= coalesce_if_unconflicting (p
, conflicts
,
1817 REGNO (src
), REGNO (dest
));
1823 struct phi_coalesce_context
1826 conflict_graph conflicts
;
1830 /* Callback function for for_each_successor_phi. If the set
1831 destination and the phi alternative regs do not conflict, place
1832 them in the same paritition class. DATA is a pointer to a
1833 phi_coalesce_context struct. */
1836 coalesce_reg_in_phi (insn
, dest_regno
, src_regno
, data
)
1837 rtx insn ATTRIBUTE_UNUSED
;
1842 struct phi_coalesce_context
*context
=
1843 (struct phi_coalesce_context
*) data
;
1845 /* Attempt to use the same reg, if they don't conflict. */
1847 += coalesce_if_unconflicting (context
->p
, context
->conflicts
,
1848 dest_regno
, src_regno
);
1852 /* For each alternative in a phi function corresponding to basic block
1853 BB (in phi nodes in successor block to BB), place the reg in the
1854 phi alternative and the reg to which the phi value is set into the
1855 same class in partition P, if allowed by CONFLICTS.
1857 Return the number of changes that were made to P.
1859 See Morgan figure 11.14. */
1862 coalesce_regs_in_successor_phi_nodes (bb
, p
, conflicts
)
1865 conflict_graph conflicts
;
1867 struct phi_coalesce_context context
;
1869 context
.conflicts
= conflicts
;
1870 context
.changed
= 0;
1872 for_each_successor_phi (bb
, &coalesce_reg_in_phi
, &context
);
1874 return context
.changed
;
1877 /* Compute and return a partition of pseudos. Where possible,
1878 non-conflicting pseudos are placed in the same class.
1880 The caller is responsible for deallocating the returned partition. */
1883 compute_coalesced_reg_partition ()
1887 regset_head phi_set_head
;
1888 regset phi_set
= &phi_set_head
;
1891 partition_new (ssa_definition
->num_elements
);
1893 /* The first priority is to make sure registers that might have to
1894 be copied on abnormal critical edges are placed in the same
1895 partition. This saves us from having to split abnormal critical
1896 edges (which can't be done). */
1897 FOR_EACH_BB_REVERSE (bb
)
1898 make_regs_equivalent_over_bad_edges (bb
->index
, p
);
1900 INIT_REG_SET (phi_set
);
1904 conflict_graph conflicts
;
1908 /* Build the set of registers involved in phi nodes, either as
1909 arguments to the phi function or as the target of a set. */
1910 CLEAR_REG_SET (phi_set
);
1911 mark_phi_and_copy_regs (phi_set
);
1913 /* Compute conflicts. */
1914 conflicts
= conflict_graph_compute (phi_set
, p
);
1916 /* FIXME: Better would be to process most frequently executed
1917 blocks first, so that most frequently executed copies would
1918 be more likely to be removed by register coalescing. But any
1919 order will generate correct, if non-optimal, results. */
1920 FOR_EACH_BB_REVERSE (bb
)
1922 changed
+= coalesce_regs_in_copies (bb
, p
, conflicts
);
1924 coalesce_regs_in_successor_phi_nodes (bb
, p
, conflicts
);
1927 conflict_graph_delete (conflicts
);
1929 while (changed
> 0);
1931 FREE_REG_SET (phi_set
);
1936 /* Mark the regs in a phi node. PTR is a phi expression or one of its
1937 components (a REG or a CONST_INT). DATA is a reg set in which to
1938 set all regs. Called from for_each_rtx. */
1941 mark_reg_in_phi (ptr
, data
)
1946 regset set
= (regset
) data
;
1948 switch (GET_CODE (expr
))
1951 SET_REGNO_REG_SET (set
, REGNO (expr
));
1961 /* Mark in PHI_SET all pseudos that are used in a phi node -- either
1962 set from a phi expression, or used as an argument in one. Also
1963 mark regs that are the source or target of a reg copy. Uses
1967 mark_phi_and_copy_regs (phi_set
)
1972 /* Scan the definitions of all regs. */
1973 for (reg
= 0; reg
< VARRAY_SIZE (ssa_definition
); ++reg
)
1974 if (CONVERT_REGISTER_TO_SSA_P (reg
))
1976 rtx insn
= VARRAY_RTX (ssa_definition
, reg
);
1981 || (GET_CODE (insn
) == NOTE
1982 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_DELETED
))
1984 pattern
= PATTERN (insn
);
1985 /* Sometimes we get PARALLEL insns. These aren't phi nodes or
1987 if (GET_CODE (pattern
) != SET
)
1989 src
= SET_SRC (pattern
);
1991 if (GET_CODE (src
) == REG
)
1993 /* It's a reg copy. */
1994 SET_REGNO_REG_SET (phi_set
, reg
);
1995 SET_REGNO_REG_SET (phi_set
, REGNO (src
));
1997 else if (GET_CODE (src
) == PHI
)
1999 /* It's a phi node. Mark the reg being set. */
2000 SET_REGNO_REG_SET (phi_set
, reg
);
2001 /* Mark the regs used in the phi function. */
2002 for_each_rtx (&src
, mark_reg_in_phi
, phi_set
);
2004 /* ... else nothing to do. */
2008 /* Rename regs in insn PTR that are equivalent. DATA is the register
2009 partition which specifies equivalences. */
2012 rename_equivalent_regs_in_insn (ptr
, data
)
2017 partition reg_partition
= (partition
) data
;
2022 switch (GET_CODE (x
))
2025 if (CONVERT_REGISTER_TO_SSA_P (REGNO (x
)))
2027 unsigned int regno
= REGNO (x
);
2028 unsigned int new_regno
= partition_find (reg_partition
, regno
);
2029 rtx canonical_element_rtx
= ssa_rename_from_lookup (new_regno
);
2031 if (canonical_element_rtx
!= NULL_RTX
&&
2032 HARD_REGISTER_P (canonical_element_rtx
))
2034 if (REGNO (canonical_element_rtx
) != regno
)
2035 *ptr
= canonical_element_rtx
;
2037 else if (regno
!= new_regno
)
2039 rtx new_reg
= regno_reg_rtx
[new_regno
];
2040 if (GET_MODE (x
) != GET_MODE (new_reg
))
2048 /* No need to rename the phi nodes. We'll check equivalence
2049 when inserting copies. */
2053 /* Anything else, continue traversing. */
2058 /* Record the register's canonical element stored in SRFP in the
2059 canonical_elements sbitmap packaged in DATA. This function is used
2060 as a callback function for traversing ssa_rename_from. */
2063 record_canonical_element_1 (srfp
, data
)
2067 unsigned int reg
= ((ssa_rename_from_pair
*) *srfp
)->reg
;
2068 sbitmap canonical_elements
=
2069 ((struct ssa_rename_from_hash_table_data
*) data
)->canonical_elements
;
2070 partition reg_partition
=
2071 ((struct ssa_rename_from_hash_table_data
*) data
)->reg_partition
;
2073 SET_BIT (canonical_elements
, partition_find (reg_partition
, reg
));
2077 /* For each class in the REG_PARTITION corresponding to a particular
2078 hard register and machine mode, check that there are no other
2079 classes with the same hard register and machine mode. Returns
2080 nonzero if this is the case, i.e., the partition is acceptable. */
2083 check_hard_regs_in_partition (reg_partition
)
2084 partition reg_partition
;
2086 /* CANONICAL_ELEMENTS has a nonzero bit if a class with the given register
2087 number and machine mode has already been seen. This is a
2088 problem with the partition. */
2089 sbitmap canonical_elements
;
2091 int already_seen
[FIRST_PSEUDO_REGISTER
][NUM_MACHINE_MODES
];
2095 /* Collect a list of canonical elements. */
2096 canonical_elements
= sbitmap_alloc (max_reg_num ());
2097 sbitmap_zero (canonical_elements
);
2098 ssa_rename_from_traverse (&record_canonical_element_1
,
2099 canonical_elements
, reg_partition
);
2101 /* We have not seen any hard register uses. */
2102 for (reg
= 0; reg
< FIRST_PSEUDO_REGISTER
; ++reg
)
2103 for (mach_mode
= 0; mach_mode
< NUM_MACHINE_MODES
; ++mach_mode
)
2104 already_seen
[reg
][mach_mode
] = 0;
2106 /* Check for classes with the same hard register and machine mode. */
2107 EXECUTE_IF_SET_IN_SBITMAP (canonical_elements
, 0, element_index
,
2109 rtx hard_reg_rtx
= ssa_rename_from_lookup (element_index
);
2110 if (hard_reg_rtx
!= NULL_RTX
&&
2111 HARD_REGISTER_P (hard_reg_rtx
) &&
2112 already_seen
[REGNO (hard_reg_rtx
)][GET_MODE (hard_reg_rtx
)] != 0)
2113 /* Two distinct partition classes should be mapped to the same
2118 sbitmap_free (canonical_elements
);
2123 /* Rename regs that are equivalent in REG_PARTITION. Also collapse
2124 any SEQUENCE insns. */
2127 rename_equivalent_regs (reg_partition
)
2128 partition reg_partition
;
2132 FOR_EACH_BB_REVERSE (b
)
2143 for_each_rtx (&PATTERN (insn
),
2144 rename_equivalent_regs_in_insn
,
2146 for_each_rtx (®_NOTES (insn
),
2147 rename_equivalent_regs_in_insn
,
2150 if (GET_CODE (PATTERN (insn
)) == SEQUENCE
)
2152 rtx s
= PATTERN (insn
);
2153 int slen
= XVECLEN (s
, 0);
2159 PATTERN (insn
) = XVECEXP (s
, 0, slen
-1);
2160 for (i
= 0; i
< slen
- 1; i
++)
2161 emit_insn_before (XVECEXP (s
, 0, i
), insn
);
2165 next
= NEXT_INSN (insn
);
2167 while (insn
!= last
);
2171 /* The main entry point for moving from SSA. */
2177 partition reg_partition
;
2178 rtx insns
= get_insns ();
2180 /* Need global_live_at_{start,end} up to date. There should not be
2181 any significant dead code at this point, except perhaps dead
2182 stores. So do not take the time to perform dead code elimination.
2184 Register coalescing needs death notes, so generate them. */
2185 life_analysis (insns
, NULL
, PROP_DEATH_NOTES
);
2187 /* Figure out which regs in copies and phi nodes don't conflict and
2188 therefore can be coalesced. */
2189 if (conservative_reg_partition
)
2190 reg_partition
= compute_conservative_reg_partition ();
2192 reg_partition
= compute_coalesced_reg_partition ();
2194 if (!check_hard_regs_in_partition (reg_partition
))
2195 /* Two separate partitions should correspond to the same hard
2196 register but do not. */
2199 rename_equivalent_regs (reg_partition
);
2201 /* Eliminate the PHI nodes. */
2202 FOR_EACH_BB_REVERSE (b
)
2206 for (e
= b
->pred
; e
; e
= e
->pred_next
)
2207 if (e
->src
!= ENTRY_BLOCK_PTR
)
2208 eliminate_phi (e
, reg_partition
);
2211 partition_delete (reg_partition
);
2213 /* Actually delete the PHI nodes. */
2214 FOR_EACH_BB_REVERSE (bb
)
2216 rtx insn
= bb
->head
;
2220 /* If this is a PHI node delete it. */
2221 if (PHI_NODE_P (insn
))
2223 if (insn
== bb
->end
)
2224 bb
->end
= PREV_INSN (insn
);
2225 insn
= delete_insn (insn
);
2227 /* Since all the phi nodes come at the beginning of the
2228 block, if we find an ordinary insn, we can stop looking
2229 for more phi nodes. */
2230 else if (INSN_P (insn
))
2232 /* If we've reached the end of the block, stop. */
2233 else if (insn
== bb
->end
)
2236 insn
= NEXT_INSN (insn
);
2240 /* Commit all the copy nodes needed to convert out of SSA form. */
2241 commit_edge_insertions ();
2245 count_or_remove_death_notes (NULL
, 1);
2247 /* Deallocate the data structures. */
2249 ssa_rename_from_free ();
2252 /* Scan phi nodes in successors to BB. For each such phi node that
2253 has a phi alternative value corresponding to BB, invoke FN. FN
2254 is passed the entire phi node insn, the regno of the set
2255 destination, the regno of the phi argument corresponding to BB,
2258 If FN ever returns non-zero, stops immediately and returns this
2259 value. Otherwise, returns zero. */
2262 for_each_successor_phi (bb
, fn
, data
)
2264 successor_phi_fn fn
;
2269 if (bb
== EXIT_BLOCK_PTR
)
2272 /* Scan outgoing edges. */
2273 for (e
= bb
->succ
; e
!= NULL
; e
= e
->succ_next
)
2277 basic_block successor
= e
->dest
;
2278 if (successor
== ENTRY_BLOCK_PTR
2279 || successor
== EXIT_BLOCK_PTR
)
2282 /* Advance to the first non-label insn of the successor block. */
2283 insn
= first_insn_after_basic_block_note (successor
);
2288 /* Scan phi nodes in the successor. */
2289 for ( ; PHI_NODE_P (insn
); insn
= NEXT_INSN (insn
))
2292 rtx phi_set
= PATTERN (insn
);
2293 rtx
*alternative
= phi_alternative (phi_set
, bb
->index
);
2296 /* This phi function may not have an alternative
2297 corresponding to the incoming edge, indicating the
2298 assigned variable is not defined along the edge. */
2299 if (alternative
== NULL
)
2301 phi_src
= *alternative
;
2303 /* Invoke the callback. */
2304 result
= (*fn
) (insn
, REGNO (SET_DEST (phi_set
)),
2305 REGNO (phi_src
), data
);
2307 /* Terminate if requested. */
2316 /* Assuming the ssa_rename_from mapping has been established, yields
2317 nonzero if 1) only one SSA register of REG1 and REG2 comes from a
2318 hard register or 2) both SSA registers REG1 and REG2 come from
2319 different hard registers. */
2322 conflicting_hard_regs_p (reg1
, reg2
)
2326 int orig_reg1
= original_register (reg1
);
2327 int orig_reg2
= original_register (reg2
);
2328 if (HARD_REGISTER_NUM_P (orig_reg1
) && HARD_REGISTER_NUM_P (orig_reg2
)
2329 && orig_reg1
!= orig_reg2
)
2331 if (HARD_REGISTER_NUM_P (orig_reg1
) && !HARD_REGISTER_NUM_P (orig_reg2
))
2333 if (!HARD_REGISTER_NUM_P (orig_reg1
) && HARD_REGISTER_NUM_P (orig_reg2
))