1 /* Static Single Assignment conversion routines for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 Building an Optimizing Compiler
26 Butterworth-Heinemann, 1998
28 Static Single Assignment Construction
29 Preston Briggs, Tim Harvey, Taylor Simpson
30 Technical Report, Rice University, 1995
31 ftp://ftp.cs.rice.edu/public/preston/optimizer/SSA.ps.gz. */
35 #include "coretypes.h"
41 #include "partition.h"
45 #include "hard-reg-set.h"
49 #include "insn-config.h"
51 #include "basic-block.h"
57 Handle subregs better, maybe. For now, if a reg that's set in a
58 subreg expression is duplicated going into SSA form, an extra copy
59 is inserted first that copies the entire reg into the duplicate, so
60 that the other bits are preserved. This isn't strictly SSA, since
61 at least part of the reg is assigned in more than one place (though
64 ??? What to do about strict_low_part. Probably I'll have to split
65 them out of their current instructions first thing.
67 Actually the best solution may be to have a kind of "mid-level rtl"
68 in which the RTL encodes exactly what we want, without exposing a
69 lot of niggling processor details. At some later point we lower
70 the representation, calling back into optabs to finish any necessary
73 /* All pseudo-registers and select hard registers are converted to SSA
74 form. When converting out of SSA, these select hard registers are
75 guaranteed to be mapped to their original register number. Each
76 machine's .h file should define CONVERT_HARD_REGISTER_TO_SSA_P
77 indicating which hard registers should be converted.
79 When converting out of SSA, temporaries for all registers are
80 partitioned. The partition is checked to ensure that all uses of
81 the same hard register in the same machine mode are in the same
84 /* If conservative_reg_partition is nonzero, use a conservative
85 register partitioning algorithm (which leaves more regs after
86 emerging from SSA) instead of the coalescing one. This is being
87 left in for a limited time only, as a debugging tool until the
88 coalescing algorithm is validated. */
90 static int conservative_reg_partition
;
92 /* This flag is set when the CFG is in SSA form. */
95 /* Element I is the single instruction that sets register I. */
96 varray_type ssa_definition
;
98 /* Element I-PSEUDO is the normal register that originated the ssa
99 register in question. */
100 varray_type ssa_rename_from
;
102 /* Element I is the normal register that originated the ssa
103 register in question.
105 A hash table stores the (register, rtl) pairs. These are each
106 xmalloc'ed and deleted when the hash table is destroyed. */
107 htab_t ssa_rename_from_ht
;
109 /* The running target ssa register for a given pseudo register.
110 (Pseudo registers appear in only one mode.) */
111 static rtx
*ssa_rename_to_pseudo
;
112 /* Similar, but for hard registers. A hard register can appear in
113 many modes, so we store an equivalent pseudo for each of the
115 static rtx ssa_rename_to_hard
[FIRST_PSEUDO_REGISTER
][NUM_MACHINE_MODES
];
117 /* ssa_rename_from maps pseudo registers to the original corresponding
118 RTL. It is implemented as using a hash table. */
123 } ssa_rename_from_pair
;
125 struct ssa_rename_from_hash_table_data
{
126 sbitmap canonical_elements
;
127 partition reg_partition
;
130 static rtx gen_sequence
132 static void ssa_rename_from_initialize
134 static rtx ssa_rename_from_lookup
136 static unsigned int original_register
137 PARAMS ((unsigned int regno
));
138 static void ssa_rename_from_insert
139 PARAMS ((unsigned int reg
, rtx r
));
140 static void ssa_rename_from_free
142 typedef int (*srf_trav
) PARAMS ((int regno
, rtx r
, sbitmap canonical_elements
, partition reg_partition
));
143 static void ssa_rename_from_traverse
144 PARAMS ((htab_trav callback_function
, sbitmap canonical_elements
, partition reg_partition
));
145 /*static Avoid warnign message. */ void ssa_rename_from_print
147 static int ssa_rename_from_print_1
148 PARAMS ((void **slot
, void *data
));
149 static hashval_t ssa_rename_from_hash_function
150 PARAMS ((const void * srfp
));
151 static int ssa_rename_from_equal
152 PARAMS ((const void *srfp1
, const void *srfp2
));
153 static void ssa_rename_from_delete
154 PARAMS ((void *srfp
));
156 static rtx ssa_rename_to_lookup
158 static void ssa_rename_to_insert
159 PARAMS ((rtx reg
, rtx r
));
161 /* The number of registers that were live on entry to the SSA routines. */
162 static unsigned int ssa_max_reg_num
;
164 /* Local function prototypes. */
166 struct rename_context
;
168 static inline rtx
* phi_alternative
170 static void compute_dominance_frontiers_1
171 PARAMS ((sbitmap
*frontiers
, dominance_info idom
, int bb
, sbitmap done
));
172 static void find_evaluations_1
173 PARAMS ((rtx dest
, rtx set
, void *data
));
174 static void find_evaluations
175 PARAMS ((sbitmap
*evals
, int nregs
));
176 static void compute_iterated_dominance_frontiers
177 PARAMS ((sbitmap
*idfs
, sbitmap
*frontiers
, sbitmap
*evals
, int nregs
));
178 static void insert_phi_node
179 PARAMS ((int regno
, int b
));
180 static void insert_phi_nodes
181 PARAMS ((sbitmap
*idfs
, sbitmap
*evals
, int nregs
));
182 static void create_delayed_rename
183 PARAMS ((struct rename_context
*, rtx
*));
184 static void apply_delayed_renames
185 PARAMS ((struct rename_context
*));
186 static int rename_insn_1
187 PARAMS ((rtx
*ptr
, void *data
));
188 static void rename_block
189 PARAMS ((int b
, dominance_info dom
));
190 static void rename_registers
191 PARAMS ((int nregs
, dominance_info idom
));
193 static inline int ephi_add_node
194 PARAMS ((rtx reg
, rtx
*nodes
, int *n_nodes
));
195 static int * ephi_forward
196 PARAMS ((int t
, sbitmap visited
, sbitmap
*succ
, int *tstack
));
197 static void ephi_backward
198 PARAMS ((int t
, sbitmap visited
, sbitmap
*pred
, rtx
*nodes
));
199 static void ephi_create
200 PARAMS ((int t
, sbitmap visited
, sbitmap
*pred
, sbitmap
*succ
, rtx
*nodes
));
201 static void eliminate_phi
202 PARAMS ((edge e
, partition reg_partition
));
203 static int make_regs_equivalent_over_bad_edges
204 PARAMS ((int bb
, partition reg_partition
));
206 /* These are used only in the conservative register partitioning
208 static int make_equivalent_phi_alternatives_equivalent
209 PARAMS ((int bb
, partition reg_partition
));
210 static partition compute_conservative_reg_partition
212 static int record_canonical_element_1
213 PARAMS ((void **srfp
, void *data
));
214 static int check_hard_regs_in_partition
215 PARAMS ((partition reg_partition
));
217 /* These are used in the register coalescing algorithm. */
218 static int coalesce_if_unconflicting
219 PARAMS ((partition p
, conflict_graph conflicts
, int reg1
, int reg2
));
220 static int coalesce_regs_in_copies
221 PARAMS ((basic_block bb
, partition p
, conflict_graph conflicts
));
222 static int coalesce_reg_in_phi
223 PARAMS ((rtx
, int dest_regno
, int src_regno
, void *data
));
224 static int coalesce_regs_in_successor_phi_nodes
225 PARAMS ((basic_block bb
, partition p
, conflict_graph conflicts
));
226 static partition compute_coalesced_reg_partition
228 static int mark_reg_in_phi
229 PARAMS ((rtx
*ptr
, void *data
));
230 static void mark_phi_and_copy_regs
231 PARAMS ((regset phi_set
));
233 static int rename_equivalent_regs_in_insn
234 PARAMS ((rtx
*ptr
, void *data
));
235 static void rename_equivalent_regs
236 PARAMS ((partition reg_partition
));
238 /* Deal with hard registers. */
239 static int conflicting_hard_regs_p
240 PARAMS ((int reg1
, int reg2
));
242 /* ssa_rename_to maps registers and machine modes to SSA pseudo registers. */
244 /* Find the register associated with REG in the indicated mode. */
247 ssa_rename_to_lookup (reg
)
250 if (!HARD_REGISTER_P (reg
))
251 return ssa_rename_to_pseudo
[REGNO (reg
) - FIRST_PSEUDO_REGISTER
];
253 return ssa_rename_to_hard
[REGNO (reg
)][GET_MODE (reg
)];
256 /* Store a new value mapping REG to R in ssa_rename_to. */
259 ssa_rename_to_insert(reg
, r
)
263 if (!HARD_REGISTER_P (reg
))
264 ssa_rename_to_pseudo
[REGNO (reg
) - FIRST_PSEUDO_REGISTER
] = r
;
266 ssa_rename_to_hard
[REGNO (reg
)][GET_MODE (reg
)] = r
;
269 /* Prepare ssa_rename_from for use. */
272 ssa_rename_from_initialize ()
274 /* We use an arbitrary initial hash table size of 64. */
275 ssa_rename_from_ht
= htab_create (64,
276 &ssa_rename_from_hash_function
,
277 &ssa_rename_from_equal
,
278 &ssa_rename_from_delete
);
281 /* Find the REG entry in ssa_rename_from. Return NULL_RTX if no entry is
285 ssa_rename_from_lookup (reg
)
288 ssa_rename_from_pair srfp
;
289 ssa_rename_from_pair
*answer
;
291 srfp
.original
= NULL_RTX
;
292 answer
= (ssa_rename_from_pair
*)
293 htab_find_with_hash (ssa_rename_from_ht
, (void *) &srfp
, reg
);
294 return (answer
== 0 ? NULL_RTX
: answer
->original
);
297 /* Find the number of the original register specified by REGNO. If
298 the register is a pseudo, return the original register's number.
299 Otherwise, return this register number REGNO. */
302 original_register (regno
)
305 rtx original_rtx
= ssa_rename_from_lookup (regno
);
306 return original_rtx
!= NULL_RTX
? REGNO (original_rtx
) : regno
;
309 /* Add mapping from R to REG to ssa_rename_from even if already present. */
312 ssa_rename_from_insert (reg
, r
)
317 ssa_rename_from_pair
*srfp
= xmalloc (sizeof (ssa_rename_from_pair
));
320 slot
= htab_find_slot_with_hash (ssa_rename_from_ht
, (const void *) srfp
,
323 free ((void *) *slot
);
327 /* Apply the CALLBACK_FUNCTION to each element in ssa_rename_from.
328 CANONICAL_ELEMENTS and REG_PARTITION pass data needed by the only
329 current use of this function. */
332 ssa_rename_from_traverse (callback_function
,
333 canonical_elements
, reg_partition
)
334 htab_trav callback_function
;
335 sbitmap canonical_elements
;
336 partition reg_partition
;
338 struct ssa_rename_from_hash_table_data srfhd
;
339 srfhd
.canonical_elements
= canonical_elements
;
340 srfhd
.reg_partition
= reg_partition
;
341 htab_traverse (ssa_rename_from_ht
, callback_function
, (void *) &srfhd
);
344 /* Destroy ssa_rename_from. */
347 ssa_rename_from_free ()
349 htab_delete (ssa_rename_from_ht
);
352 /* Print the contents of ssa_rename_from. */
354 /* static Avoid erroneous error message. */
356 ssa_rename_from_print ()
358 printf ("ssa_rename_from's hash table contents:\n");
359 htab_traverse (ssa_rename_from_ht
, &ssa_rename_from_print_1
, NULL
);
362 /* Print the contents of the hash table entry SLOT, passing the unused
363 sttribute DATA. Used as a callback function with htab_traverse (). */
366 ssa_rename_from_print_1 (slot
, data
)
368 void *data ATTRIBUTE_UNUSED
;
370 ssa_rename_from_pair
* p
= *slot
;
371 printf ("ssa_rename_from maps pseudo %i to original %i.\n",
372 p
->reg
, REGNO (p
->original
));
376 /* Given a hash entry SRFP, yield a hash value. */
379 ssa_rename_from_hash_function (srfp
)
382 return ((const ssa_rename_from_pair
*) srfp
)->reg
;
385 /* Test whether two hash table entries SRFP1 and SRFP2 are equal. */
388 ssa_rename_from_equal (srfp1
, srfp2
)
392 return ssa_rename_from_hash_function (srfp1
) ==
393 ssa_rename_from_hash_function (srfp2
);
396 /* Delete the hash table entry SRFP. */
399 ssa_rename_from_delete (srfp
)
405 /* Given the SET of a PHI node, return the address of the alternative
406 for predecessor block C. */
409 phi_alternative (set
, c
)
413 rtvec phi_vec
= XVEC (SET_SRC (set
), 0);
416 for (v
= GET_NUM_ELEM (phi_vec
) - 2; v
>= 0; v
-= 2)
417 if (INTVAL (RTVEC_ELT (phi_vec
, v
+ 1)) == c
)
418 return &RTVEC_ELT (phi_vec
, v
);
423 /* Given the SET of a phi node, remove the alternative for predecessor
424 block C. Return nonzero on success, or zero if no alternative is
428 remove_phi_alternative (set
, block
)
432 rtvec phi_vec
= XVEC (SET_SRC (set
), 0);
433 int num_elem
= GET_NUM_ELEM (phi_vec
);
437 for (v
= num_elem
- 2; v
>= 0; v
-= 2)
438 if (INTVAL (RTVEC_ELT (phi_vec
, v
+ 1)) == c
)
440 if (v
< num_elem
- 2)
442 RTVEC_ELT (phi_vec
, v
) = RTVEC_ELT (phi_vec
, num_elem
- 2);
443 RTVEC_ELT (phi_vec
, v
+ 1) = RTVEC_ELT (phi_vec
, num_elem
- 1);
445 PUT_NUM_ELEM (phi_vec
, num_elem
- 2);
452 /* For all registers, find all blocks in which they are set.
454 This is the transform of what would be local kill information that
455 we ought to be getting from flow. */
457 static sbitmap
*fe_evals
;
458 static int fe_current_bb
;
461 find_evaluations_1 (dest
, set
, data
)
463 rtx set ATTRIBUTE_UNUSED
;
464 void *data ATTRIBUTE_UNUSED
;
466 if (GET_CODE (dest
) == REG
467 && CONVERT_REGISTER_TO_SSA_P (REGNO (dest
)))
468 SET_BIT (fe_evals
[REGNO (dest
)], fe_current_bb
);
472 find_evaluations (evals
, nregs
)
478 sbitmap_vector_zero (evals
, nregs
);
481 FOR_EACH_BB_REVERSE (bb
)
485 fe_current_bb
= bb
->index
;
491 note_stores (PATTERN (p
), find_evaluations_1
, NULL
);
500 /* Computing the Dominance Frontier:
502 As decribed in Morgan, section 3.5, this may be done simply by
503 walking the dominator tree bottom-up, computing the frontier for
504 the children before the parent. When considering a block B,
507 (1) A flow graph edge leaving B that does not lead to a child
508 of B in the dominator tree must be a block that is either equal
509 to B or not dominated by B. Such blocks belong in the frontier
512 (2) Consider a block X in the frontier of one of the children C
513 of B. If X is not equal to B and is not dominated by B, it
514 is in the frontier of B.
518 compute_dominance_frontiers_1 (frontiers
, idom
, bb
, done
)
524 basic_block b
= BASIC_BLOCK (bb
);
529 sbitmap_zero (frontiers
[bb
]);
531 /* Do the frontier of the children first. Not all children in the
532 dominator tree (blocks dominated by this one) are children in the
533 CFG, so check all blocks. */
535 if (get_immediate_dominator (idom
, c
)->index
== bb
536 && ! TEST_BIT (done
, c
->index
))
537 compute_dominance_frontiers_1 (frontiers
, idom
, c
->index
, done
);
539 /* Find blocks conforming to rule (1) above. */
540 for (e
= b
->succ
; e
; e
= e
->succ_next
)
542 if (e
->dest
== EXIT_BLOCK_PTR
)
544 if (get_immediate_dominator (idom
, e
->dest
)->index
!= bb
)
545 SET_BIT (frontiers
[bb
], e
->dest
->index
);
548 /* Find blocks conforming to rule (2). */
550 if (get_immediate_dominator (idom
, c
)->index
== bb
)
553 EXECUTE_IF_SET_IN_SBITMAP (frontiers
[c
->index
], 0, x
,
555 if (get_immediate_dominator (idom
, BASIC_BLOCK (x
))->index
!= bb
)
556 SET_BIT (frontiers
[bb
], x
);
562 compute_dominance_frontiers (frontiers
, idom
)
566 sbitmap done
= sbitmap_alloc (last_basic_block
);
569 compute_dominance_frontiers_1 (frontiers
, idom
, 0, done
);
574 /* Computing the Iterated Dominance Frontier:
576 This is the set of merge points for a given register.
578 This is not particularly intuitive. See section 7.1 of Morgan, in
579 particular figures 7.3 and 7.4 and the immediately surrounding text.
583 compute_iterated_dominance_frontiers (idfs
, frontiers
, evals
, nregs
)
592 worklist
= sbitmap_alloc (last_basic_block
);
594 for (reg
= 0; reg
< nregs
; ++reg
)
596 sbitmap idf
= idfs
[reg
];
599 /* Start the iterative process by considering those blocks that
600 evaluate REG. We'll add their dominance frontiers to the
601 IDF, and then consider the blocks we just added. */
602 sbitmap_copy (worklist
, evals
[reg
]);
604 /* Morgan's algorithm is incorrect here. Blocks that evaluate
605 REG aren't necessarily in REG's IDF. Start with an empty IDF. */
608 /* Iterate until the worklist is empty. */
613 EXECUTE_IF_SET_IN_SBITMAP (worklist
, 0, b
,
615 RESET_BIT (worklist
, b
);
616 /* For each block on the worklist, add to the IDF all
617 blocks on its dominance frontier that aren't already
618 on the IDF. Every block that's added is also added
620 sbitmap_union_of_diff (worklist
, worklist
, frontiers
[b
], idf
);
621 sbitmap_a_or_b (idf
, idf
, frontiers
[b
]);
628 sbitmap_free (worklist
);
632 fprintf (rtl_dump_file
,
633 "Iterated dominance frontier: %d passes on %d regs.\n",
638 /* Insert the phi nodes. */
641 insert_phi_node (regno
, bb
)
644 basic_block b
= BASIC_BLOCK (bb
);
652 /* Find out how many predecessors there are. */
653 for (e
= b
->pred
, npred
= 0; e
; e
= e
->pred_next
)
654 if (e
->src
!= ENTRY_BLOCK_PTR
)
657 /* If this block has no "interesting" preds, then there is nothing to
658 do. Consider a block that only has the entry block as a pred. */
662 /* This is the register to which the phi function will be assigned. */
663 reg
= regno_reg_rtx
[regno
];
665 /* Construct the arguments to the PHI node. The use of pc_rtx is just
666 a placeholder; we'll insert the proper value in rename_registers. */
667 vec
= rtvec_alloc (npred
* 2);
668 for (e
= b
->pred
, i
= 0; e
; e
= e
->pred_next
, i
+= 2)
669 if (e
->src
!= ENTRY_BLOCK_PTR
)
671 RTVEC_ELT (vec
, i
+ 0) = pc_rtx
;
672 RTVEC_ELT (vec
, i
+ 1) = GEN_INT (e
->src
->index
);
675 phi
= gen_rtx_PHI (VOIDmode
, vec
);
676 phi
= gen_rtx_SET (VOIDmode
, reg
, phi
);
678 insn
= first_insn_after_basic_block_note (b
);
679 end_p
= PREV_INSN (insn
) == b
->end
;
680 emit_insn_before (phi
, insn
);
682 b
->end
= PREV_INSN (insn
);
686 insert_phi_nodes (idfs
, evals
, nregs
)
688 sbitmap
*evals ATTRIBUTE_UNUSED
;
693 for (reg
= 0; reg
< nregs
; ++reg
)
694 if (CONVERT_REGISTER_TO_SSA_P (reg
))
697 EXECUTE_IF_SET_IN_SBITMAP (idfs
[reg
], 0, b
,
699 if (REGNO_REG_SET_P (BASIC_BLOCK (b
)->global_live_at_start
, reg
))
700 insert_phi_node (reg
, b
);
705 /* Rename the registers to conform to SSA.
707 This is essentially the algorithm presented in Figure 7.8 of Morgan,
708 with a few changes to reduce pattern search time in favor of a bit
709 more memory usage. */
711 /* One of these is created for each set. It will live in a list local
712 to its basic block for the duration of that block's processing. */
713 struct rename_set_data
715 struct rename_set_data
*next
;
716 /* This is the SET_DEST of the (first) SET that sets the REG. */
718 /* This is what used to be at *REG_LOC. */
720 /* This is the REG that will replace OLD_REG. It's set only
721 when the rename data is moved onto the DONE_RENAMES queue. */
723 /* This is what to restore ssa_rename_to_lookup (old_reg) to. It is
724 usually the previous contents of ssa_rename_to_lookup (old_reg). */
726 /* This is the insn that contains all the SETs of the REG. */
730 /* This struct is used to pass information to callback functions while
731 renaming registers. */
732 struct rename_context
734 struct rename_set_data
*new_renames
;
735 struct rename_set_data
*done_renames
;
739 /* Queue the rename of *REG_LOC. */
741 create_delayed_rename (c
, reg_loc
)
742 struct rename_context
*c
;
745 struct rename_set_data
*r
;
746 r
= (struct rename_set_data
*) xmalloc (sizeof(*r
));
748 if (GET_CODE (*reg_loc
) != REG
749 || !CONVERT_REGISTER_TO_SSA_P (REGNO (*reg_loc
)))
752 r
->reg_loc
= reg_loc
;
753 r
->old_reg
= *reg_loc
;
754 r
->prev_reg
= ssa_rename_to_lookup(r
->old_reg
);
755 r
->set_insn
= c
->current_insn
;
756 r
->next
= c
->new_renames
;
760 /* This is part of a rather ugly hack to allow the pre-ssa regno to be
761 reused. If, during processing, a register has not yet been touched,
762 ssa_rename_to[regno][machno] will be NULL. Now, in the course of pushing
763 and popping values from ssa_rename_to, when we would ordinarily
764 pop NULL back in, we pop RENAME_NO_RTX. We treat this exactly the
765 same as NULL, except that it signals that the original regno has
766 already been reused. */
767 #define RENAME_NO_RTX pc_rtx
769 /* Move all the entries from NEW_RENAMES onto DONE_RENAMES by
770 applying all the renames on NEW_RENAMES. */
773 apply_delayed_renames (c
)
774 struct rename_context
*c
;
776 struct rename_set_data
*r
;
777 struct rename_set_data
*last_r
= NULL
;
779 for (r
= c
->new_renames
; r
!= NULL
; r
= r
->next
)
783 /* Failure here means that someone has a PARALLEL that sets
784 a register twice (bad!). */
785 if (ssa_rename_to_lookup (r
->old_reg
) != r
->prev_reg
)
787 /* Failure here means we have changed REG_LOC before applying
789 /* For the first set we come across, reuse the original regno. */
790 if (r
->prev_reg
== NULL_RTX
&& !HARD_REGISTER_P (r
->old_reg
))
792 r
->new_reg
= r
->old_reg
;
793 /* We want to restore RENAME_NO_RTX rather than NULL_RTX. */
794 r
->prev_reg
= RENAME_NO_RTX
;
797 r
->new_reg
= gen_reg_rtx (GET_MODE (r
->old_reg
));
798 new_regno
= REGNO (r
->new_reg
);
799 ssa_rename_to_insert (r
->old_reg
, r
->new_reg
);
801 if (new_regno
>= (int) ssa_definition
->num_elements
)
803 int new_limit
= new_regno
* 5 / 4;
804 VARRAY_GROW (ssa_definition
, new_limit
);
807 VARRAY_RTX (ssa_definition
, new_regno
) = r
->set_insn
;
808 ssa_rename_from_insert (new_regno
, r
->old_reg
);
813 last_r
->next
= c
->done_renames
;
814 c
->done_renames
= c
->new_renames
;
815 c
->new_renames
= NULL
;
819 /* Part one of the first step of rename_block, called through for_each_rtx.
820 Mark pseudos that are set for later update. Transform uses of pseudos. */
823 rename_insn_1 (ptr
, data
)
828 struct rename_context
*context
= data
;
833 switch (GET_CODE (x
))
837 rtx
*destp
= &SET_DEST (x
);
838 rtx dest
= SET_DEST (x
);
840 /* An assignment to a paradoxical SUBREG does not read from
841 the destination operand, and thus does not need to be
842 wrapped into a SEQUENCE when translating into SSA form.
843 We merely strip off the SUBREG and proceed normally for
845 if (GET_CODE (dest
) == SUBREG
846 && (GET_MODE_SIZE (GET_MODE (dest
))
847 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest
))))
848 && GET_CODE (SUBREG_REG (dest
)) == REG
849 && CONVERT_REGISTER_TO_SSA_P (REGNO (SUBREG_REG (dest
))))
851 destp
= &XEXP (dest
, 0);
852 dest
= XEXP (dest
, 0);
855 /* Some SETs also use the REG specified in their LHS.
856 These can be detected by the presence of
857 STRICT_LOW_PART, SUBREG, SIGN_EXTRACT, and ZERO_EXTRACT
858 in the LHS. Handle these by changing
859 (set (subreg (reg foo)) ...)
861 (sequence [(set (reg foo_1) (reg foo))
862 (set (subreg (reg foo_1)) ...)])
864 FIXME: Much of the time this is too much. For some constructs
865 we know that the output register is strictly an output
866 (paradoxical SUBREGs and some libcalls for example).
868 For those cases we are better off not making the false
870 if (GET_CODE (dest
) == STRICT_LOW_PART
871 || GET_CODE (dest
) == SUBREG
872 || GET_CODE (dest
) == SIGN_EXTRACT
873 || GET_CODE (dest
) == ZERO_EXTRACT
)
878 while (GET_CODE (reg
) == STRICT_LOW_PART
879 || GET_CODE (reg
) == SUBREG
880 || GET_CODE (reg
) == SIGN_EXTRACT
881 || GET_CODE (reg
) == ZERO_EXTRACT
)
884 if (GET_CODE (reg
) == REG
885 && CONVERT_REGISTER_TO_SSA_P (REGNO (reg
)))
887 /* Generate (set reg reg), and do renaming on it so
888 that it becomes (set reg_1 reg_0), and we will
889 replace reg with reg_1 in the SUBREG. */
891 struct rename_set_data
*saved_new_renames
;
892 saved_new_renames
= context
->new_renames
;
893 context
->new_renames
= NULL
;
894 i
= emit_insn (gen_rtx_SET (VOIDmode
, reg
, reg
));
895 for_each_rtx (&i
, rename_insn_1
, data
);
896 apply_delayed_renames (context
);
897 context
->new_renames
= saved_new_renames
;
900 else if (GET_CODE (dest
) == REG
901 && CONVERT_REGISTER_TO_SSA_P (REGNO (dest
)))
903 /* We found a genuine set of an interesting register. Tag
904 it so that we can create a new name for it after we finish
905 processing this insn. */
907 create_delayed_rename (context
, destp
);
909 /* Since we do not wish to (directly) traverse the
910 SET_DEST, recurse through for_each_rtx for the SET_SRC
912 if (GET_CODE (x
) == SET
)
913 for_each_rtx (&SET_SRC (x
), rename_insn_1
, data
);
917 /* Otherwise, this was not an interesting destination. Continue
918 on, marking uses as normal. */
923 if (CONVERT_REGISTER_TO_SSA_P (REGNO (x
))
924 && REGNO (x
) < ssa_max_reg_num
)
926 rtx new_reg
= ssa_rename_to_lookup (x
);
928 if (new_reg
!= RENAME_NO_RTX
&& new_reg
!= NULL_RTX
)
930 if (GET_MODE (x
) != GET_MODE (new_reg
))
936 /* Undefined value used, rename it to a new pseudo register so
937 that it cannot conflict with an existing register. */
938 *ptr
= gen_reg_rtx (GET_MODE (x
));
944 /* There is considerable debate on how CLOBBERs ought to be
945 handled in SSA. For now, we're keeping the CLOBBERs, which
946 means that we don't really have SSA form. There are a couple
947 of proposals for how to fix this problem, but neither is
950 rtx dest
= XCEXP (x
, 0, CLOBBER
);
953 if (CONVERT_REGISTER_TO_SSA_P (REGNO (dest
))
954 && REGNO (dest
) < ssa_max_reg_num
)
956 rtx new_reg
= ssa_rename_to_lookup (dest
);
957 if (new_reg
!= NULL_RTX
&& new_reg
!= RENAME_NO_RTX
)
958 XCEXP (x
, 0, CLOBBER
) = new_reg
;
960 /* Stop traversing. */
964 /* Continue traversing. */
969 /* Never muck with the phi. We do that elsewhere, special-like. */
973 /* Anything else, continue traversing. */
981 rtx first_insn
= get_insns ();
987 /* Count the insns in the chain. */
989 for (tem
= first_insn
; tem
; tem
= NEXT_INSN (tem
))
992 result
= gen_rtx_SEQUENCE (VOIDmode
, rtvec_alloc (len
));
994 for (i
= 0, tem
= first_insn
; tem
; tem
= NEXT_INSN (tem
), i
++)
995 XVECEXP (result
, 0, i
) = tem
;
1001 rename_block (bb
, idom
)
1003 dominance_info idom
;
1005 basic_block b
= BASIC_BLOCK (bb
);
1007 rtx insn
, next
, last
;
1008 struct rename_set_data
*set_data
= NULL
;
1011 /* Step One: Walk the basic block, adding new names for sets and
1021 struct rename_context context
;
1022 context
.done_renames
= set_data
;
1023 context
.new_renames
= NULL
;
1024 context
.current_insn
= insn
;
1027 for_each_rtx (&PATTERN (insn
), rename_insn_1
, &context
);
1028 for_each_rtx (®_NOTES (insn
), rename_insn_1
, &context
);
1030 /* Sometimes, we end up with a sequence of insns that
1031 SSA needs to treat as a single insn. Wrap these in a
1032 SEQUENCE. (Any notes now get attached to the SEQUENCE,
1033 not to the old version inner insn.) */
1034 if (get_insns () != NULL_RTX
)
1039 emit (PATTERN (insn
));
1040 seq
= gen_sequence ();
1041 /* We really want a SEQUENCE of SETs, not a SEQUENCE
1043 for (i
= 0; i
< XVECLEN (seq
, 0); i
++)
1044 XVECEXP (seq
, 0, i
) = PATTERN (XVECEXP (seq
, 0, i
));
1045 PATTERN (insn
) = seq
;
1049 apply_delayed_renames (&context
);
1050 set_data
= context
.done_renames
;
1053 next
= NEXT_INSN (insn
);
1055 while (insn
!= last
);
1057 /* Step Two: Update the phi nodes of this block's successors. */
1059 for (e
= b
->succ
; e
; e
= e
->succ_next
)
1061 if (e
->dest
== EXIT_BLOCK_PTR
)
1064 insn
= first_insn_after_basic_block_note (e
->dest
);
1066 while (PHI_NODE_P (insn
))
1068 rtx phi
= PATTERN (insn
);
1071 /* Find out which of our outgoing registers this node is
1072 intended to replace. Note that if this is not the first PHI
1073 node to have been created for this register, we have to
1074 jump through rename links to figure out which register
1075 we're talking about. This can easily be recognized by
1076 noting that the regno is new to this pass. */
1077 reg
= SET_DEST (phi
);
1078 if (REGNO (reg
) >= ssa_max_reg_num
)
1079 reg
= ssa_rename_from_lookup (REGNO (reg
));
1080 if (reg
== NULL_RTX
)
1082 reg
= ssa_rename_to_lookup (reg
);
1084 /* It is possible for the variable to be uninitialized on
1085 edges in. Reduce the arity of the PHI so that we don't
1086 consider those edges. */
1087 if (reg
== NULL
|| reg
== RENAME_NO_RTX
)
1089 if (! remove_phi_alternative (phi
, b
))
1094 /* When we created the PHI nodes, we did not know what mode
1095 the register should be. Now that we've found an original,
1096 we can fill that in. */
1097 if (GET_MODE (SET_DEST (phi
)) == VOIDmode
)
1098 PUT_MODE (SET_DEST (phi
), GET_MODE (reg
));
1099 else if (GET_MODE (SET_DEST (phi
)) != GET_MODE (reg
))
1102 *phi_alternative (phi
, bb
) = reg
;
1105 insn
= NEXT_INSN (insn
);
1109 /* Step Three: Do the same to the children of this block in
1113 if (get_immediate_dominator (idom
, c
)->index
== bb
)
1114 rename_block (c
->index
, idom
);
1116 /* Step Four: Update the sets to refer to their new register,
1117 and restore ssa_rename_to to its previous state. */
1121 struct rename_set_data
*next
;
1122 rtx old_reg
= *set_data
->reg_loc
;
1124 if (*set_data
->reg_loc
!= set_data
->old_reg
)
1126 *set_data
->reg_loc
= set_data
->new_reg
;
1128 ssa_rename_to_insert (old_reg
, set_data
->prev_reg
);
1130 next
= set_data
->next
;
1137 rename_registers (nregs
, idom
)
1139 dominance_info idom
;
1141 VARRAY_RTX_INIT (ssa_definition
, nregs
* 3, "ssa_definition");
1142 ssa_rename_from_initialize ();
1144 ssa_rename_to_pseudo
= (rtx
*) alloca (nregs
* sizeof(rtx
));
1145 memset ((char *) ssa_rename_to_pseudo
, 0, nregs
* sizeof(rtx
));
1146 memset ((char *) ssa_rename_to_hard
, 0,
1147 FIRST_PSEUDO_REGISTER
* NUM_MACHINE_MODES
* sizeof (rtx
));
1149 rename_block (0, idom
);
1151 /* ??? Update basic_block_live_at_start, and other flow info
1154 ssa_rename_to_pseudo
= NULL
;
1157 /* The main entry point for moving to SSA. */
1162 /* Element I is the set of blocks that set register I. */
1165 /* Dominator bitmaps. */
1169 /* Element I is the immediate dominator of block I. */
1170 dominance_info idom
;
1176 /* Don't do it twice. */
1180 /* Need global_live_at_{start,end} up to date. Do not remove any
1181 dead code. We'll let the SSA optimizers do that. */
1182 life_analysis (get_insns (), NULL
, 0);
1184 idom
= calculate_dominance_info (CDI_DOMINATORS
);
1188 fputs (";; Immediate Dominators:\n", rtl_dump_file
);
1190 fprintf (rtl_dump_file
, ";\t%3d = %3d\n", bb
->index
,
1191 get_immediate_dominator (idom
, bb
)->index
);
1192 fflush (rtl_dump_file
);
1195 /* Compute dominance frontiers. */
1197 dfs
= sbitmap_vector_alloc (last_basic_block
, last_basic_block
);
1198 compute_dominance_frontiers (dfs
, idom
);
1202 dump_sbitmap_vector (rtl_dump_file
, ";; Dominance Frontiers:",
1203 "; Basic Block", dfs
, last_basic_block
);
1204 fflush (rtl_dump_file
);
1207 /* Compute register evaluations. */
1209 ssa_max_reg_num
= max_reg_num ();
1210 nregs
= ssa_max_reg_num
;
1211 evals
= sbitmap_vector_alloc (nregs
, last_basic_block
);
1212 find_evaluations (evals
, nregs
);
1214 /* Compute the iterated dominance frontier for each register. */
1216 idfs
= sbitmap_vector_alloc (nregs
, last_basic_block
);
1217 compute_iterated_dominance_frontiers (idfs
, dfs
, evals
, nregs
);
1221 dump_sbitmap_vector (rtl_dump_file
, ";; Iterated Dominance Frontiers:",
1222 "; Register", idfs
, nregs
);
1223 fflush (rtl_dump_file
);
1226 /* Insert the phi nodes. */
1228 insert_phi_nodes (idfs
, evals
, nregs
);
1230 /* Rename the registers to satisfy SSA. */
1232 rename_registers (nregs
, idom
);
1234 /* All done! Clean up and go home. */
1236 sbitmap_vector_free (dfs
);
1237 sbitmap_vector_free (evals
);
1238 sbitmap_vector_free (idfs
);
1241 reg_scan (get_insns (), max_reg_num (), 1);
1242 free_dominance_info (idom
);
1245 /* REG is the representative temporary of its partition. Add it to the
1246 set of nodes to be processed, if it hasn't been already. Return the
1247 index of this register in the node set. */
1250 ephi_add_node (reg
, nodes
, n_nodes
)
1255 for (i
= *n_nodes
- 1; i
>= 0; --i
)
1256 if (REGNO (reg
) == REGNO (nodes
[i
]))
1259 nodes
[i
= (*n_nodes
)++] = reg
;
1263 /* Part one of the topological sort. This is a forward (downward) search
1264 through the graph collecting a stack of nodes to process. Assuming no
1265 cycles, the nodes at top of the stack when we are finished will have
1266 no other dependencies. */
1269 ephi_forward (t
, visited
, succ
, tstack
)
1277 SET_BIT (visited
, t
);
1279 EXECUTE_IF_SET_IN_SBITMAP (succ
[t
], 0, s
,
1281 if (! TEST_BIT (visited
, s
))
1282 tstack
= ephi_forward (s
, visited
, succ
, tstack
);
1289 /* Part two of the topological sort. The is a backward search through
1290 a cycle in the graph, copying the data forward as we go. */
1293 ephi_backward (t
, visited
, pred
, nodes
)
1295 sbitmap visited
, *pred
;
1300 SET_BIT (visited
, t
);
1302 EXECUTE_IF_SET_IN_SBITMAP (pred
[t
], 0, p
,
1304 if (! TEST_BIT (visited
, p
))
1306 ephi_backward (p
, visited
, pred
, nodes
);
1307 emit_move_insn (nodes
[p
], nodes
[t
]);
1312 /* Part two of the topological sort. Create the copy for a register
1313 and any cycle of which it is a member. */
1316 ephi_create (t
, visited
, pred
, succ
, nodes
)
1318 sbitmap visited
, *pred
, *succ
;
1321 rtx reg_u
= NULL_RTX
;
1322 int unvisited_predecessors
= 0;
1325 /* Iterate through the predecessor list looking for unvisited nodes.
1326 If there are any, we have a cycle, and must deal with that. At
1327 the same time, look for a visited predecessor. If there is one,
1328 we won't need to create a temporary. */
1330 EXECUTE_IF_SET_IN_SBITMAP (pred
[t
], 0, p
,
1332 if (! TEST_BIT (visited
, p
))
1333 unvisited_predecessors
= 1;
1338 if (unvisited_predecessors
)
1340 /* We found a cycle. Copy out one element of the ring (if necessary),
1341 then traverse the ring copying as we go. */
1345 reg_u
= gen_reg_rtx (GET_MODE (nodes
[t
]));
1346 emit_move_insn (reg_u
, nodes
[t
]);
1349 EXECUTE_IF_SET_IN_SBITMAP (pred
[t
], 0, p
,
1351 if (! TEST_BIT (visited
, p
))
1353 ephi_backward (p
, visited
, pred
, nodes
);
1354 emit_move_insn (nodes
[p
], reg_u
);
1360 /* No cycle. Just copy the value from a successor. */
1363 EXECUTE_IF_SET_IN_SBITMAP (succ
[t
], 0, s
,
1365 SET_BIT (visited
, t
);
1366 emit_move_insn (nodes
[t
], nodes
[s
]);
1372 /* Convert the edge to normal form. */
1375 eliminate_phi (e
, reg_partition
)
1377 partition reg_partition
;
1380 sbitmap
*pred
, *succ
;
1383 int *stack
, *tstack
;
1387 /* Collect an upper bound on the number of registers needing processing. */
1389 insn
= first_insn_after_basic_block_note (e
->dest
);
1392 while (PHI_NODE_P (insn
))
1394 insn
= next_nonnote_insn (insn
);
1401 /* Build the auxiliary graph R(B).
1403 The nodes of the graph are the members of the register partition
1404 present in Phi(B). There is an edge from FIND(T0)->FIND(T1) for
1405 each T0 = PHI(...,T1,...), where T1 is for the edge from block C. */
1407 nodes
= (rtx
*) alloca (n_nodes
* sizeof(rtx
));
1408 pred
= sbitmap_vector_alloc (n_nodes
, n_nodes
);
1409 succ
= sbitmap_vector_alloc (n_nodes
, n_nodes
);
1410 sbitmap_vector_zero (pred
, n_nodes
);
1411 sbitmap_vector_zero (succ
, n_nodes
);
1413 insn
= first_insn_after_basic_block_note (e
->dest
);
1416 for (; PHI_NODE_P (insn
); insn
= next_nonnote_insn (insn
))
1418 rtx
* preg
= phi_alternative (PATTERN (insn
), e
->src
->index
);
1419 rtx tgt
= SET_DEST (PATTERN (insn
));
1422 /* There may be no phi alternative corresponding to this edge.
1423 This indicates that the phi variable is undefined along this
1429 if (GET_CODE (reg
) != REG
|| GET_CODE (tgt
) != REG
)
1432 reg
= regno_reg_rtx
[partition_find (reg_partition
, REGNO (reg
))];
1433 tgt
= regno_reg_rtx
[partition_find (reg_partition
, REGNO (tgt
))];
1434 /* If the two registers are already in the same partition,
1435 nothing will need to be done. */
1440 ireg
= ephi_add_node (reg
, nodes
, &n_nodes
);
1441 itgt
= ephi_add_node (tgt
, nodes
, &n_nodes
);
1443 SET_BIT (pred
[ireg
], itgt
);
1444 SET_BIT (succ
[itgt
], ireg
);
1451 /* Begin a topological sort of the graph. */
1453 visited
= sbitmap_alloc (n_nodes
);
1454 sbitmap_zero (visited
);
1456 tstack
= stack
= (int *) alloca (n_nodes
* sizeof (int));
1458 for (i
= 0; i
< n_nodes
; ++i
)
1459 if (! TEST_BIT (visited
, i
))
1460 tstack
= ephi_forward (i
, visited
, succ
, tstack
);
1462 sbitmap_zero (visited
);
1464 /* As we find a solution to the tsort, collect the implementation
1465 insns in a sequence. */
1468 while (tstack
!= stack
)
1471 if (! TEST_BIT (visited
, i
))
1472 ephi_create (i
, visited
, pred
, succ
, nodes
);
1475 insn
= get_insns ();
1477 insert_insn_on_edge (insn
, e
);
1479 fprintf (rtl_dump_file
, "Emitting copy on edge (%d,%d)\n",
1480 e
->src
->index
, e
->dest
->index
);
1482 sbitmap_free (visited
);
1484 sbitmap_vector_free (pred
);
1485 sbitmap_vector_free (succ
);
1488 /* For basic block B, consider all phi insns which provide an
1489 alternative corresponding to an incoming abnormal critical edge.
1490 Place the phi alternative corresponding to that abnormal critical
1491 edge in the same register class as the destination of the set.
1493 From Morgan, p. 178:
1495 For each abnormal critical edge (C, B),
1496 if T0 = phi (T1, ..., Ti, ..., Tm) is a phi node in B,
1497 and C is the ith predecessor of B,
1498 then T0 and Ti must be equivalent.
1500 Return nonzero iff any such cases were found for which the two
1501 regs were not already in the same class. */
1504 make_regs_equivalent_over_bad_edges (bb
, reg_partition
)
1506 partition reg_partition
;
1509 basic_block b
= BASIC_BLOCK (bb
);
1512 /* Advance to the first phi node. */
1513 phi
= first_insn_after_basic_block_note (b
);
1515 /* Scan all the phi nodes. */
1518 phi
= next_nonnote_insn (phi
))
1522 rtx set
= PATTERN (phi
);
1523 rtx tgt
= SET_DEST (set
);
1525 /* The set target is expected to be an SSA register. */
1526 if (GET_CODE (tgt
) != REG
1527 || !CONVERT_REGISTER_TO_SSA_P (REGNO (tgt
)))
1529 tgt_regno
= REGNO (tgt
);
1531 /* Scan incoming abnormal critical edges. */
1532 for (e
= b
->pred
; e
; e
= e
->pred_next
)
1533 if ((e
->flags
& EDGE_ABNORMAL
) && EDGE_CRITICAL_P (e
))
1535 rtx
*alt
= phi_alternative (set
, e
->src
->index
);
1538 /* If there is no alternative corresponding to this edge,
1539 the value is undefined along the edge, so just go on. */
1543 /* The phi alternative is expected to be an SSA register. */
1544 if (GET_CODE (*alt
) != REG
1545 || !CONVERT_REGISTER_TO_SSA_P (REGNO (*alt
)))
1547 alt_regno
= REGNO (*alt
);
1549 /* If the set destination and the phi alternative aren't
1550 already in the same class... */
1551 if (partition_find (reg_partition
, tgt_regno
)
1552 != partition_find (reg_partition
, alt_regno
))
1554 /* ... make them such. */
1555 if (conflicting_hard_regs_p (tgt_regno
, alt_regno
))
1556 /* It is illegal to unify a hard register with a
1557 different register. */
1560 partition_union (reg_partition
,
1561 tgt_regno
, alt_regno
);
1570 /* Consider phi insns in basic block BB pairwise. If the set target
1571 of both isns are equivalent pseudos, make the corresponding phi
1572 alternatives in each phi corresponding equivalent.
1574 Return nonzero if any new register classes were unioned. */
1577 make_equivalent_phi_alternatives_equivalent (bb
, reg_partition
)
1579 partition reg_partition
;
1582 basic_block b
= BASIC_BLOCK (bb
);
1585 /* Advance to the first phi node. */
1586 phi
= first_insn_after_basic_block_note (b
);
1588 /* Scan all the phi nodes. */
1591 phi
= next_nonnote_insn (phi
))
1593 rtx set
= PATTERN (phi
);
1594 /* The regno of the destination of the set. */
1595 int tgt_regno
= REGNO (SET_DEST (PATTERN (phi
)));
1597 rtx phi2
= next_nonnote_insn (phi
);
1599 /* Scan all phi nodes following this one. */
1602 phi2
= next_nonnote_insn (phi2
))
1604 rtx set2
= PATTERN (phi2
);
1605 /* The regno of the destination of the set. */
1606 int tgt2_regno
= REGNO (SET_DEST (set2
));
1608 /* Are the set destinations equivalent regs? */
1609 if (partition_find (reg_partition
, tgt_regno
) ==
1610 partition_find (reg_partition
, tgt2_regno
))
1613 /* Scan over edges. */
1614 for (e
= b
->pred
; e
; e
= e
->pred_next
)
1616 int pred_block
= e
->src
->index
;
1617 /* Identify the phi alternatives from both phi
1618 nodes corresponding to this edge. */
1619 rtx
*alt
= phi_alternative (set
, pred_block
);
1620 rtx
*alt2
= phi_alternative (set2
, pred_block
);
1622 /* If one of the phi nodes doesn't have a
1623 corresponding alternative, just skip it. */
1624 if (alt
== 0 || alt2
== 0)
1627 /* Both alternatives should be SSA registers. */
1628 if (GET_CODE (*alt
) != REG
1629 || !CONVERT_REGISTER_TO_SSA_P (REGNO (*alt
)))
1631 if (GET_CODE (*alt2
) != REG
1632 || !CONVERT_REGISTER_TO_SSA_P (REGNO (*alt2
)))
1635 /* If the alternatives aren't already in the same
1637 if (partition_find (reg_partition
, REGNO (*alt
))
1638 != partition_find (reg_partition
, REGNO (*alt2
)))
1640 /* ... make them so. */
1641 if (conflicting_hard_regs_p (REGNO (*alt
), REGNO (*alt2
)))
1642 /* It is illegal to unify a hard register with
1643 a different register. */
1646 partition_union (reg_partition
,
1647 REGNO (*alt
), REGNO (*alt2
));
1658 /* Compute a conservative partition of outstanding pseudo registers.
1659 See Morgan 7.3.1. */
1662 compute_conservative_reg_partition ()
1667 /* We don't actually work with hard registers, but it's easier to
1668 carry them around anyway rather than constantly doing register
1669 number arithmetic. */
1671 partition_new (ssa_definition
->num_elements
);
1673 /* The first priority is to make sure registers that might have to
1674 be copied on abnormal critical edges are placed in the same
1675 partition. This saves us from having to split abnormal critical
1677 FOR_EACH_BB_REVERSE (bb
)
1678 changed
+= make_regs_equivalent_over_bad_edges (bb
->index
, p
);
1680 /* Now we have to insure that corresponding arguments of phi nodes
1681 assigning to corresponding regs are equivalent. Iterate until
1686 FOR_EACH_BB_REVERSE (bb
)
1687 changed
+= make_equivalent_phi_alternatives_equivalent (bb
->index
, p
);
1693 /* The following functions compute a register partition that attempts
1694 to eliminate as many reg copies and phi node copies as possible by
1695 coalescing registers. This is the strategy:
1697 1. As in the conservative case, the top priority is to coalesce
1698 registers that otherwise would cause copies to be placed on
1699 abnormal critical edges (which isn't possible).
1701 2. Figure out which regs are involved (in the LHS or RHS) of
1702 copies and phi nodes. Compute conflicts among these regs.
1704 3. Walk around the instruction stream, placing two regs in the
1705 same class of the partition if one appears on the LHS and the
1706 other on the RHS of a copy or phi node and the two regs don't
1707 conflict. The conflict information of course needs to be
1710 4. If anything has changed, there may be new opportunities to
1711 coalesce regs, so go back to 2.
1714 /* If REG1 and REG2 don't conflict in CONFLICTS, place them in the
1715 same class of partition P, if they aren't already. Update
1716 CONFLICTS appropriately.
1718 Returns one if REG1 and REG2 were placed in the same class but were
1719 not previously; zero otherwise.
1721 See Morgan figure 11.15. */
1724 coalesce_if_unconflicting (p
, conflicts
, reg1
, reg2
)
1726 conflict_graph conflicts
;
1732 /* Work only on SSA registers. */
1733 if (!CONVERT_REGISTER_TO_SSA_P (reg1
) || !CONVERT_REGISTER_TO_SSA_P (reg2
))
1736 /* Find the canonical regs for the classes containing REG1 and
1738 reg1
= partition_find (p
, reg1
);
1739 reg2
= partition_find (p
, reg2
);
1741 /* If they're already in the same class, there's nothing to do. */
1745 /* If the regs conflict, our hands are tied. */
1746 if (conflicting_hard_regs_p (reg1
, reg2
) ||
1747 conflict_graph_conflict_p (conflicts
, reg1
, reg2
))
1750 /* We're good to go. Put the regs in the same partition. */
1751 partition_union (p
, reg1
, reg2
);
1753 /* Find the new canonical reg for the merged class. */
1754 reg
= partition_find (p
, reg1
);
1756 /* Merge conflicts from the two previous classes. */
1757 conflict_graph_merge_regs (conflicts
, reg
, reg1
);
1758 conflict_graph_merge_regs (conflicts
, reg
, reg2
);
1763 /* For each register copy insn in basic block BB, place the LHS and
1764 RHS regs in the same class in partition P if they do not conflict
1765 according to CONFLICTS.
1767 Returns the number of changes that were made to P.
1769 See Morgan figure 11.14. */
1772 coalesce_regs_in_copies (bb
, p
, conflicts
)
1775 conflict_graph conflicts
;
1781 /* Scan the instruction stream of the block. */
1782 for (insn
= bb
->head
; insn
!= end
; insn
= NEXT_INSN (insn
))
1788 /* If this isn't a set insn, go to the next insn. */
1789 if (GET_CODE (insn
) != INSN
)
1791 pattern
= PATTERN (insn
);
1792 if (GET_CODE (pattern
) != SET
)
1795 src
= SET_SRC (pattern
);
1796 dest
= SET_DEST (pattern
);
1798 /* We're only looking for copies. */
1799 if (GET_CODE (src
) != REG
|| GET_CODE (dest
) != REG
)
1802 /* Coalesce only if the reg modes are the same. As long as
1803 each reg's rtx is unique, it can have only one mode, so two
1804 pseudos of different modes can't be coalesced into one.
1806 FIXME: We can probably get around this by inserting SUBREGs
1807 where appropriate, but for now we don't bother. */
1808 if (GET_MODE (src
) != GET_MODE (dest
))
1811 /* Found a copy; see if we can use the same reg for both the
1812 source and destination (and thus eliminate the copy,
1814 changed
+= coalesce_if_unconflicting (p
, conflicts
,
1815 REGNO (src
), REGNO (dest
));
1821 struct phi_coalesce_context
1824 conflict_graph conflicts
;
1828 /* Callback function for for_each_successor_phi. If the set
1829 destination and the phi alternative regs do not conflict, place
1830 them in the same partition class. DATA is a pointer to a
1831 phi_coalesce_context struct. */
1834 coalesce_reg_in_phi (insn
, dest_regno
, src_regno
, data
)
1835 rtx insn ATTRIBUTE_UNUSED
;
1840 struct phi_coalesce_context
*context
=
1841 (struct phi_coalesce_context
*) data
;
1843 /* Attempt to use the same reg, if they don't conflict. */
1845 += coalesce_if_unconflicting (context
->p
, context
->conflicts
,
1846 dest_regno
, src_regno
);
1850 /* For each alternative in a phi function corresponding to basic block
1851 BB (in phi nodes in successor block to BB), place the reg in the
1852 phi alternative and the reg to which the phi value is set into the
1853 same class in partition P, if allowed by CONFLICTS.
1855 Return the number of changes that were made to P.
1857 See Morgan figure 11.14. */
1860 coalesce_regs_in_successor_phi_nodes (bb
, p
, conflicts
)
1863 conflict_graph conflicts
;
1865 struct phi_coalesce_context context
;
1867 context
.conflicts
= conflicts
;
1868 context
.changed
= 0;
1870 for_each_successor_phi (bb
, &coalesce_reg_in_phi
, &context
);
1872 return context
.changed
;
1875 /* Compute and return a partition of pseudos. Where possible,
1876 non-conflicting pseudos are placed in the same class.
1878 The caller is responsible for deallocating the returned partition. */
1881 compute_coalesced_reg_partition ()
1885 regset_head phi_set_head
;
1886 regset phi_set
= &phi_set_head
;
1889 partition_new (ssa_definition
->num_elements
);
1891 /* The first priority is to make sure registers that might have to
1892 be copied on abnormal critical edges are placed in the same
1893 partition. This saves us from having to split abnormal critical
1894 edges (which can't be done). */
1895 FOR_EACH_BB_REVERSE (bb
)
1896 make_regs_equivalent_over_bad_edges (bb
->index
, p
);
1898 INIT_REG_SET (phi_set
);
1902 conflict_graph conflicts
;
1906 /* Build the set of registers involved in phi nodes, either as
1907 arguments to the phi function or as the target of a set. */
1908 CLEAR_REG_SET (phi_set
);
1909 mark_phi_and_copy_regs (phi_set
);
1911 /* Compute conflicts. */
1912 conflicts
= conflict_graph_compute (phi_set
, p
);
1914 /* FIXME: Better would be to process most frequently executed
1915 blocks first, so that most frequently executed copies would
1916 be more likely to be removed by register coalescing. But any
1917 order will generate correct, if non-optimal, results. */
1918 FOR_EACH_BB_REVERSE (bb
)
1920 changed
+= coalesce_regs_in_copies (bb
, p
, conflicts
);
1922 coalesce_regs_in_successor_phi_nodes (bb
, p
, conflicts
);
1925 conflict_graph_delete (conflicts
);
1927 while (changed
> 0);
1929 FREE_REG_SET (phi_set
);
1934 /* Mark the regs in a phi node. PTR is a phi expression or one of its
1935 components (a REG or a CONST_INT). DATA is a reg set in which to
1936 set all regs. Called from for_each_rtx. */
1939 mark_reg_in_phi (ptr
, data
)
1944 regset set
= (regset
) data
;
1946 switch (GET_CODE (expr
))
1949 SET_REGNO_REG_SET (set
, REGNO (expr
));
1959 /* Mark in PHI_SET all pseudos that are used in a phi node -- either
1960 set from a phi expression, or used as an argument in one. Also
1961 mark regs that are the source or target of a reg copy. Uses
1965 mark_phi_and_copy_regs (phi_set
)
1970 /* Scan the definitions of all regs. */
1971 for (reg
= 0; reg
< VARRAY_SIZE (ssa_definition
); ++reg
)
1972 if (CONVERT_REGISTER_TO_SSA_P (reg
))
1974 rtx insn
= VARRAY_RTX (ssa_definition
, reg
);
1979 || (GET_CODE (insn
) == NOTE
1980 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_DELETED
))
1982 pattern
= PATTERN (insn
);
1983 /* Sometimes we get PARALLEL insns. These aren't phi nodes or
1985 if (GET_CODE (pattern
) != SET
)
1987 src
= SET_SRC (pattern
);
1989 if (GET_CODE (src
) == REG
)
1991 /* It's a reg copy. */
1992 SET_REGNO_REG_SET (phi_set
, reg
);
1993 SET_REGNO_REG_SET (phi_set
, REGNO (src
));
1995 else if (GET_CODE (src
) == PHI
)
1997 /* It's a phi node. Mark the reg being set. */
1998 SET_REGNO_REG_SET (phi_set
, reg
);
1999 /* Mark the regs used in the phi function. */
2000 for_each_rtx (&src
, mark_reg_in_phi
, phi_set
);
2002 /* ... else nothing to do. */
2006 /* Rename regs in insn PTR that are equivalent. DATA is the register
2007 partition which specifies equivalences. */
2010 rename_equivalent_regs_in_insn (ptr
, data
)
2015 partition reg_partition
= (partition
) data
;
2020 switch (GET_CODE (x
))
2023 if (CONVERT_REGISTER_TO_SSA_P (REGNO (x
)))
2025 unsigned int regno
= REGNO (x
);
2026 unsigned int new_regno
= partition_find (reg_partition
, regno
);
2027 rtx canonical_element_rtx
= ssa_rename_from_lookup (new_regno
);
2029 if (canonical_element_rtx
!= NULL_RTX
&&
2030 HARD_REGISTER_P (canonical_element_rtx
))
2032 if (REGNO (canonical_element_rtx
) != regno
)
2033 *ptr
= canonical_element_rtx
;
2035 else if (regno
!= new_regno
)
2037 rtx new_reg
= regno_reg_rtx
[new_regno
];
2038 if (GET_MODE (x
) != GET_MODE (new_reg
))
2046 /* No need to rename the phi nodes. We'll check equivalence
2047 when inserting copies. */
2051 /* Anything else, continue traversing. */
2056 /* Record the register's canonical element stored in SRFP in the
2057 canonical_elements sbitmap packaged in DATA. This function is used
2058 as a callback function for traversing ssa_rename_from. */
2061 record_canonical_element_1 (srfp
, data
)
2065 unsigned int reg
= ((ssa_rename_from_pair
*) *srfp
)->reg
;
2066 sbitmap canonical_elements
=
2067 ((struct ssa_rename_from_hash_table_data
*) data
)->canonical_elements
;
2068 partition reg_partition
=
2069 ((struct ssa_rename_from_hash_table_data
*) data
)->reg_partition
;
2071 SET_BIT (canonical_elements
, partition_find (reg_partition
, reg
));
2075 /* For each class in the REG_PARTITION corresponding to a particular
2076 hard register and machine mode, check that there are no other
2077 classes with the same hard register and machine mode. Returns
2078 nonzero if this is the case, i.e., the partition is acceptable. */
2081 check_hard_regs_in_partition (reg_partition
)
2082 partition reg_partition
;
2084 /* CANONICAL_ELEMENTS has a nonzero bit if a class with the given register
2085 number and machine mode has already been seen. This is a
2086 problem with the partition. */
2087 sbitmap canonical_elements
;
2089 int already_seen
[FIRST_PSEUDO_REGISTER
][NUM_MACHINE_MODES
];
2093 /* Collect a list of canonical elements. */
2094 canonical_elements
= sbitmap_alloc (max_reg_num ());
2095 sbitmap_zero (canonical_elements
);
2096 ssa_rename_from_traverse (&record_canonical_element_1
,
2097 canonical_elements
, reg_partition
);
2099 /* We have not seen any hard register uses. */
2100 for (reg
= 0; reg
< FIRST_PSEUDO_REGISTER
; ++reg
)
2101 for (mach_mode
= 0; mach_mode
< NUM_MACHINE_MODES
; ++mach_mode
)
2102 already_seen
[reg
][mach_mode
] = 0;
2104 /* Check for classes with the same hard register and machine mode. */
2105 EXECUTE_IF_SET_IN_SBITMAP (canonical_elements
, 0, element_index
,
2107 rtx hard_reg_rtx
= ssa_rename_from_lookup (element_index
);
2108 if (hard_reg_rtx
!= NULL_RTX
&&
2109 HARD_REGISTER_P (hard_reg_rtx
) &&
2110 already_seen
[REGNO (hard_reg_rtx
)][GET_MODE (hard_reg_rtx
)] != 0)
2111 /* Two distinct partition classes should be mapped to the same
2116 sbitmap_free (canonical_elements
);
2121 /* Rename regs that are equivalent in REG_PARTITION. Also collapse
2122 any SEQUENCE insns. */
2125 rename_equivalent_regs (reg_partition
)
2126 partition reg_partition
;
2130 FOR_EACH_BB_REVERSE (b
)
2141 for_each_rtx (&PATTERN (insn
),
2142 rename_equivalent_regs_in_insn
,
2144 for_each_rtx (®_NOTES (insn
),
2145 rename_equivalent_regs_in_insn
,
2148 if (GET_CODE (PATTERN (insn
)) == SEQUENCE
)
2150 rtx s
= PATTERN (insn
);
2151 int slen
= XVECLEN (s
, 0);
2157 PATTERN (insn
) = XVECEXP (s
, 0, slen
-1);
2158 for (i
= 0; i
< slen
- 1; i
++)
2159 emit_insn_before (XVECEXP (s
, 0, i
), insn
);
2163 next
= NEXT_INSN (insn
);
2165 while (insn
!= last
);
2169 /* The main entry point for moving from SSA. */
2175 partition reg_partition
;
2176 rtx insns
= get_insns ();
2178 /* Need global_live_at_{start,end} up to date. There should not be
2179 any significant dead code at this point, except perhaps dead
2180 stores. So do not take the time to perform dead code elimination.
2182 Register coalescing needs death notes, so generate them. */
2183 life_analysis (insns
, NULL
, PROP_DEATH_NOTES
);
2185 /* Figure out which regs in copies and phi nodes don't conflict and
2186 therefore can be coalesced. */
2187 if (conservative_reg_partition
)
2188 reg_partition
= compute_conservative_reg_partition ();
2190 reg_partition
= compute_coalesced_reg_partition ();
2192 if (!check_hard_regs_in_partition (reg_partition
))
2193 /* Two separate partitions should correspond to the same hard
2194 register but do not. */
2197 rename_equivalent_regs (reg_partition
);
2199 /* Eliminate the PHI nodes. */
2200 FOR_EACH_BB_REVERSE (b
)
2204 for (e
= b
->pred
; e
; e
= e
->pred_next
)
2205 if (e
->src
!= ENTRY_BLOCK_PTR
)
2206 eliminate_phi (e
, reg_partition
);
2209 partition_delete (reg_partition
);
2211 /* Actually delete the PHI nodes. */
2212 FOR_EACH_BB_REVERSE (bb
)
2214 rtx insn
= bb
->head
;
2218 /* If this is a PHI node delete it. */
2219 if (PHI_NODE_P (insn
))
2221 if (insn
== bb
->end
)
2222 bb
->end
= PREV_INSN (insn
);
2223 insn
= delete_insn (insn
);
2225 /* Since all the phi nodes come at the beginning of the
2226 block, if we find an ordinary insn, we can stop looking
2227 for more phi nodes. */
2228 else if (INSN_P (insn
))
2230 /* If we've reached the end of the block, stop. */
2231 else if (insn
== bb
->end
)
2234 insn
= NEXT_INSN (insn
);
2238 /* Commit all the copy nodes needed to convert out of SSA form. */
2239 commit_edge_insertions ();
2243 count_or_remove_death_notes (NULL
, 1);
2245 /* Deallocate the data structures. */
2247 ssa_rename_from_free ();
2250 /* Scan phi nodes in successors to BB. For each such phi node that
2251 has a phi alternative value corresponding to BB, invoke FN. FN
2252 is passed the entire phi node insn, the regno of the set
2253 destination, the regno of the phi argument corresponding to BB,
2256 If FN ever returns nonzero, stops immediately and returns this
2257 value. Otherwise, returns zero. */
2260 for_each_successor_phi (bb
, fn
, data
)
2262 successor_phi_fn fn
;
2267 if (bb
== EXIT_BLOCK_PTR
)
2270 /* Scan outgoing edges. */
2271 for (e
= bb
->succ
; e
!= NULL
; e
= e
->succ_next
)
2275 basic_block successor
= e
->dest
;
2276 if (successor
== ENTRY_BLOCK_PTR
2277 || successor
== EXIT_BLOCK_PTR
)
2280 /* Advance to the first non-label insn of the successor block. */
2281 insn
= first_insn_after_basic_block_note (successor
);
2286 /* Scan phi nodes in the successor. */
2287 for ( ; PHI_NODE_P (insn
); insn
= NEXT_INSN (insn
))
2290 rtx phi_set
= PATTERN (insn
);
2291 rtx
*alternative
= phi_alternative (phi_set
, bb
->index
);
2294 /* This phi function may not have an alternative
2295 corresponding to the incoming edge, indicating the
2296 assigned variable is not defined along the edge. */
2297 if (alternative
== NULL
)
2299 phi_src
= *alternative
;
2301 /* Invoke the callback. */
2302 result
= (*fn
) (insn
, REGNO (SET_DEST (phi_set
)),
2303 REGNO (phi_src
), data
);
2305 /* Terminate if requested. */
2314 /* Assuming the ssa_rename_from mapping has been established, yields
2315 nonzero if 1) only one SSA register of REG1 and REG2 comes from a
2316 hard register or 2) both SSA registers REG1 and REG2 come from
2317 different hard registers. */
2320 conflicting_hard_regs_p (reg1
, reg2
)
2324 int orig_reg1
= original_register (reg1
);
2325 int orig_reg2
= original_register (reg2
);
2326 if (HARD_REGISTER_NUM_P (orig_reg1
) && HARD_REGISTER_NUM_P (orig_reg2
)
2327 && orig_reg1
!= orig_reg2
)
2329 if (HARD_REGISTER_NUM_P (orig_reg1
) && !HARD_REGISTER_NUM_P (orig_reg2
))
2331 if (!HARD_REGISTER_NUM_P (orig_reg1
) && HARD_REGISTER_NUM_P (orig_reg2
))