OSDN Git Service

* sbitmap.c: Convert prototypes to ISO C90.
[pf3gnuchains/gcc-fork.git] / gcc / ssa.c
1 /* Static Single Assignment conversion routines for the GNU compiler.
2    Copyright (C) 2000, 2001, 2002, 2003
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.  */
21
22 /* References:
23
24    Building an Optimizing Compiler
25    Robert Morgan
26    Butterworth-Heinemann, 1998
27
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.  */
32
33 #include "config.h"
34 #include "system.h"
35 #include "coretypes.h"
36 #include "tm.h"
37
38 #include "rtl.h"
39 #include "expr.h"
40 #include "varray.h"
41 #include "partition.h"
42 #include "sbitmap.h"
43 #include "hashtab.h"
44 #include "regs.h"
45 #include "hard-reg-set.h"
46 #include "flags.h"
47 #include "function.h"
48 #include "real.h"
49 #include "insn-config.h"
50 #include "recog.h"
51 #include "basic-block.h"
52 #include "output.h"
53 #include "ssa.h"
54
55 /* TODO:
56
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
62    they are adjacent).
63
64    ??? What to do about strict_low_part.  Probably I'll have to split
65    them out of their current instructions first thing.
66
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
71    expansion.  */
72
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.
78
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
82    class.  */
83
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.  */
89
90 static int conservative_reg_partition;
91
92 /* This flag is set when the CFG is in SSA form.  */
93 int in_ssa_form = 0;
94
95 /* Element I is the single instruction that sets register I.  */
96 varray_type ssa_definition;
97
98 /* Element I-PSEUDO is the normal register that originated the ssa
99    register in question.  */
100 varray_type ssa_rename_from;
101
102 /* Element I is the normal register that originated the ssa
103    register in question.
104
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;
108
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
114    modes.  */
115 static rtx ssa_rename_to_hard[FIRST_PSEUDO_REGISTER][NUM_MACHINE_MODES];
116
117 /* ssa_rename_from maps pseudo registers to the original corresponding
118    RTL.  It is implemented as using a hash table.  */
119
120 typedef struct {
121   unsigned int reg;
122   rtx original;
123 } ssa_rename_from_pair;
124
125 struct ssa_rename_from_hash_table_data {
126   sbitmap canonical_elements;
127   partition reg_partition;
128 };
129
130 static rtx gen_sequence (void);
131 static void ssa_rename_from_initialize (void);
132 static rtx ssa_rename_from_lookup (int reg);
133 static unsigned int original_register (unsigned int regno);
134 static void ssa_rename_from_insert (unsigned int reg, rtx r);
135 static void ssa_rename_from_free (void);
136 typedef int (*srf_trav) (int regno, rtx r, sbitmap canonical_elements,
137                          partition reg_partition);
138 static void ssa_rename_from_traverse (htab_trav callback_function,
139                                       sbitmap canonical_elements, partition reg_partition);
140 /*static Avoid warning message.  */ void ssa_rename_from_print (void);
141 static int ssa_rename_from_print_1 (void **slot, void *data);
142 static hashval_t ssa_rename_from_hash_function (const void * srfp);
143 static int ssa_rename_from_equal (const void *srfp1, const void *srfp2);
144 static void ssa_rename_from_delete (void *srfp);
145
146 static rtx ssa_rename_to_lookup (rtx reg);
147 static void ssa_rename_to_insert (rtx reg, rtx r);
148
149 /* The number of registers that were live on entry to the SSA routines.  */
150 static unsigned int ssa_max_reg_num;
151
152 /* Local function prototypes.  */
153
154 struct rename_context;
155
156 static inline rtx * phi_alternative (rtx, int);
157 static void compute_dominance_frontiers_1 (sbitmap *frontiers,
158                                            dominance_info idom, int bb,
159                                            sbitmap done);
160 static void find_evaluations_1 (rtx dest, rtx set, void *data);
161 static void find_evaluations (sbitmap *evals, int nregs);
162 static void compute_iterated_dominance_frontiers (sbitmap *idfs,
163                                                   sbitmap *frontiers,
164                                                   sbitmap *evals, int nregs);
165 static void insert_phi_node (int regno, int b);
166 static void insert_phi_nodes (sbitmap *idfs, sbitmap *evals, int nregs);
167 static void create_delayed_rename (struct rename_context *, rtx *);
168 static void apply_delayed_renames (struct rename_context *);
169 static int rename_insn_1 (rtx *ptr, void *data);
170 static void rename_block (int b, dominance_info dom);
171 static void rename_registers (int nregs, dominance_info idom);
172
173 static inline int ephi_add_node (rtx reg, rtx *nodes, int *n_nodes);
174 static int * ephi_forward (int t, sbitmap visited, sbitmap *succ, int *tstack);
175 static void ephi_backward (int t, sbitmap visited, sbitmap *pred, rtx *nodes);
176 static void ephi_create (int t, sbitmap visited, sbitmap *pred,
177                          sbitmap *succ, rtx *nodes);
178 static void eliminate_phi (edge e, partition reg_partition);
179 static int make_regs_equivalent_over_bad_edges (int bb,
180                                                 partition reg_partition);
181
182 /* These are used only in the conservative register partitioning
183    algorithms.  */
184 static int make_equivalent_phi_alternatives_equivalent
185   (int bb, partition reg_partition);
186 static partition compute_conservative_reg_partition (void);
187 static int record_canonical_element_1 (void **srfp, void *data);
188 static int check_hard_regs_in_partition (partition reg_partition);
189
190 /* These are used in the register coalescing algorithm.  */
191 static int coalesce_if_unconflicting (partition p, conflict_graph conflicts,
192                                       int reg1, int reg2);
193 static int coalesce_regs_in_copies (basic_block bb, partition p,
194                                     conflict_graph conflicts);
195 static int coalesce_reg_in_phi (rtx, int dest_regno, int src_regno,
196                                 void *data);
197 static int coalesce_regs_in_successor_phi_nodes (basic_block bb,
198                                                  partition p,
199                                                  conflict_graph conflicts);
200 static partition compute_coalesced_reg_partition (void);
201 static int mark_reg_in_phi (rtx *ptr, void *data);
202 static void mark_phi_and_copy_regs (regset phi_set);
203
204 static int rename_equivalent_regs_in_insn (rtx *ptr, void *data);
205 static void rename_equivalent_regs (partition reg_partition);
206
207 /* Deal with hard registers.  */
208 static int conflicting_hard_regs_p (int reg1, int reg2);
209
210 /* ssa_rename_to maps registers and machine modes to SSA pseudo registers.  */
211
212 /* Find the register associated with REG in the indicated mode.  */
213
214 static rtx
215 ssa_rename_to_lookup (rtx reg)
216 {
217   if (!HARD_REGISTER_P (reg))
218     return ssa_rename_to_pseudo[REGNO (reg) - FIRST_PSEUDO_REGISTER];
219   else
220     return ssa_rename_to_hard[REGNO (reg)][GET_MODE (reg)];
221 }
222
223 /* Store a new value mapping REG to R in ssa_rename_to.  */
224
225 static void
226 ssa_rename_to_insert (rtx reg, rtx r)
227 {
228   if (!HARD_REGISTER_P (reg))
229     ssa_rename_to_pseudo[REGNO (reg) - FIRST_PSEUDO_REGISTER] = r;
230   else
231     ssa_rename_to_hard[REGNO (reg)][GET_MODE (reg)] = r;
232 }
233
234 /* Prepare ssa_rename_from for use.  */
235
236 static void
237 ssa_rename_from_initialize (void)
238 {
239   /* We use an arbitrary initial hash table size of 64.  */
240   ssa_rename_from_ht = htab_create (64,
241                                     &ssa_rename_from_hash_function,
242                                     &ssa_rename_from_equal,
243                                     &ssa_rename_from_delete);
244 }
245
246 /* Find the REG entry in ssa_rename_from.  Return NULL_RTX if no entry is
247    found.  */
248
249 static rtx
250 ssa_rename_from_lookup (int reg)
251 {
252   ssa_rename_from_pair srfp;
253   ssa_rename_from_pair *answer;
254   srfp.reg = reg;
255   srfp.original = NULL_RTX;
256   answer = (ssa_rename_from_pair *)
257     htab_find_with_hash (ssa_rename_from_ht, (void *) &srfp, reg);
258   return (answer == 0 ? NULL_RTX : answer->original);
259 }
260
261 /* Find the number of the original register specified by REGNO.  If
262    the register is a pseudo, return the original register's number.
263    Otherwise, return this register number REGNO.  */
264
265 static unsigned int
266 original_register (unsigned int regno)
267 {
268   rtx original_rtx = ssa_rename_from_lookup (regno);
269   return original_rtx != NULL_RTX ? REGNO (original_rtx) : regno;
270 }
271
272 /* Add mapping from R to REG to ssa_rename_from even if already present.  */
273
274 static void
275 ssa_rename_from_insert (unsigned int reg, rtx r)
276 {
277   void **slot;
278   ssa_rename_from_pair *srfp = xmalloc (sizeof (ssa_rename_from_pair));
279   srfp->reg = reg;
280   srfp->original = r;
281   slot = htab_find_slot_with_hash (ssa_rename_from_ht, (const void *) srfp,
282                                    reg, INSERT);
283   if (*slot != 0)
284     free ((void *) *slot);
285   *slot = srfp;
286 }
287
288 /* Apply the CALLBACK_FUNCTION to each element in ssa_rename_from.
289    CANONICAL_ELEMENTS and REG_PARTITION pass data needed by the only
290    current use of this function.  */
291
292 static void
293 ssa_rename_from_traverse (htab_trav callback_function,
294                           sbitmap canonical_elements, partition reg_partition)
295 {
296   struct ssa_rename_from_hash_table_data srfhd;
297   srfhd.canonical_elements = canonical_elements;
298   srfhd.reg_partition = reg_partition;
299   htab_traverse (ssa_rename_from_ht, callback_function, (void *) &srfhd);
300 }
301
302 /* Destroy ssa_rename_from.  */
303
304 static void
305 ssa_rename_from_free (void)
306 {
307   htab_delete (ssa_rename_from_ht);
308 }
309
310 /* Print the contents of ssa_rename_from.  */
311
312 /* static  Avoid erroneous error message.  */
313 void
314 ssa_rename_from_print (void)
315 {
316   printf ("ssa_rename_from's hash table contents:\n");
317   htab_traverse (ssa_rename_from_ht, &ssa_rename_from_print_1, NULL);
318 }
319
320 /* Print the contents of the hash table entry SLOT, passing the unused
321    attribute DATA.  Used as a callback function with htab_traverse ().  */
322
323 static int
324 ssa_rename_from_print_1 (void **slot, void *data ATTRIBUTE_UNUSED)
325 {
326   ssa_rename_from_pair * p = *slot;
327   printf ("ssa_rename_from maps pseudo %i to original %i.\n",
328           p->reg, REGNO (p->original));
329   return 1;
330 }
331
332 /* Given a hash entry SRFP, yield a hash value.  */
333
334 static hashval_t
335 ssa_rename_from_hash_function (const void *srfp)
336 {
337   return ((const ssa_rename_from_pair *) srfp)->reg;
338 }
339
340 /* Test whether two hash table entries SRFP1 and SRFP2 are equal.  */
341
342 static int
343 ssa_rename_from_equal (const void *srfp1, const void *srfp2)
344 {
345   return ssa_rename_from_hash_function (srfp1) ==
346     ssa_rename_from_hash_function (srfp2);
347 }
348
349 /* Delete the hash table entry SRFP.  */
350
351 static void
352 ssa_rename_from_delete (void *srfp)
353 {
354   free (srfp);
355 }
356
357 /* Given the SET of a PHI node, return the address of the alternative
358    for predecessor block C.  */
359
360 static inline rtx *
361 phi_alternative (rtx set, int c)
362 {
363   rtvec phi_vec = XVEC (SET_SRC (set), 0);
364   int v;
365
366   for (v = GET_NUM_ELEM (phi_vec) - 2; v >= 0; v -= 2)
367     if (INTVAL (RTVEC_ELT (phi_vec, v + 1)) == c)
368       return &RTVEC_ELT (phi_vec, v);
369
370   return NULL;
371 }
372
373 /* Given the SET of a phi node, remove the alternative for predecessor
374    block C.  Return nonzero on success, or zero if no alternative is
375    found for C.  */
376
377 int
378 remove_phi_alternative (rtx set, basic_block block)
379 {
380   rtvec phi_vec = XVEC (SET_SRC (set), 0);
381   int num_elem = GET_NUM_ELEM (phi_vec);
382   int v, c;
383
384   c = block->index;
385   for (v = num_elem - 2; v >= 0; v -= 2)
386     if (INTVAL (RTVEC_ELT (phi_vec, v + 1)) == c)
387       {
388         if (v < num_elem - 2)
389           {
390             RTVEC_ELT (phi_vec, v) = RTVEC_ELT (phi_vec, num_elem - 2);
391             RTVEC_ELT (phi_vec, v + 1) = RTVEC_ELT (phi_vec, num_elem - 1);
392           }
393         PUT_NUM_ELEM (phi_vec, num_elem - 2);
394         return 1;
395       }
396
397   return 0;
398 }
399
400 /* For all registers, find all blocks in which they are set.
401
402    This is the transform of what would be local kill information that
403    we ought to be getting from flow.  */
404
405 static sbitmap *fe_evals;
406 static int fe_current_bb;
407
408 static void
409 find_evaluations_1 (rtx dest, rtx set ATTRIBUTE_UNUSED,
410                     void *data ATTRIBUTE_UNUSED)
411 {
412   if (GET_CODE (dest) == REG
413       && CONVERT_REGISTER_TO_SSA_P (REGNO (dest)))
414     SET_BIT (fe_evals[REGNO (dest)], fe_current_bb);
415 }
416
417 static void
418 find_evaluations (sbitmap *evals, int nregs)
419 {
420   basic_block bb;
421
422   sbitmap_vector_zero (evals, nregs);
423   fe_evals = evals;
424
425   FOR_EACH_BB_REVERSE (bb)
426     {
427       rtx p, last;
428
429       fe_current_bb = bb->index;
430       p = bb->head;
431       last = bb->end;
432       while (1)
433         {
434           if (INSN_P (p))
435             note_stores (PATTERN (p), find_evaluations_1, NULL);
436
437           if (p == last)
438             break;
439           p = NEXT_INSN (p);
440         }
441     }
442 }
443
444 /* Computing the Dominance Frontier:
445
446    As described in Morgan, section 3.5, this may be done simply by
447    walking the dominator tree bottom-up, computing the frontier for
448    the children before the parent.  When considering a block B,
449    there are two cases:
450
451    (1) A flow graph edge leaving B that does not lead to a child
452    of B in the dominator tree must be a block that is either equal
453    to B or not dominated by B.  Such blocks belong in the frontier
454    of B.
455
456    (2) Consider a block X in the frontier of one of the children C
457    of B.  If X is not equal to B and is not dominated by B, it
458    is in the frontier of B.
459 */
460
461 static void
462 compute_dominance_frontiers_1 (sbitmap *frontiers, dominance_info idom,
463                                int bb, sbitmap done)
464 {
465   basic_block b = BASIC_BLOCK (bb);
466   edge e;
467   basic_block c;
468
469   SET_BIT (done, bb);
470   sbitmap_zero (frontiers[bb]);
471
472   /* Do the frontier of the children first.  Not all children in the
473      dominator tree (blocks dominated by this one) are children in the
474      CFG, so check all blocks.  */
475   FOR_EACH_BB (c)
476     if (get_immediate_dominator (idom, c)->index == bb
477         && ! TEST_BIT (done, c->index))
478       compute_dominance_frontiers_1 (frontiers, idom, c->index, done);
479
480   /* Find blocks conforming to rule (1) above.  */
481   for (e = b->succ; e; e = e->succ_next)
482     {
483       if (e->dest == EXIT_BLOCK_PTR)
484         continue;
485       if (get_immediate_dominator (idom, e->dest)->index != bb)
486         SET_BIT (frontiers[bb], e->dest->index);
487     }
488
489   /* Find blocks conforming to rule (2).  */
490   FOR_EACH_BB (c)
491     if (get_immediate_dominator (idom, c)->index == bb)
492       {
493         int x;
494         EXECUTE_IF_SET_IN_SBITMAP (frontiers[c->index], 0, x,
495           {
496             if (get_immediate_dominator (idom, BASIC_BLOCK (x))->index != bb)
497               SET_BIT (frontiers[bb], x);
498           });
499       }
500 }
501
502 void
503 compute_dominance_frontiers (sbitmap *frontiers, dominance_info idom)
504 {
505   sbitmap done = sbitmap_alloc (last_basic_block);
506   sbitmap_zero (done);
507
508   compute_dominance_frontiers_1 (frontiers, idom, 0, done);
509
510   sbitmap_free (done);
511 }
512
513 /* Computing the Iterated Dominance Frontier:
514
515    This is the set of merge points for a given register.
516
517    This is not particularly intuitive.  See section 7.1 of Morgan, in
518    particular figures 7.3 and 7.4 and the immediately surrounding text.
519 */
520
521 static void
522 compute_iterated_dominance_frontiers (sbitmap *idfs, sbitmap *frontiers,
523                                       sbitmap *evals, int nregs)
524 {
525   sbitmap worklist;
526   int reg, passes = 0;
527
528   worklist = sbitmap_alloc (last_basic_block);
529
530   for (reg = 0; reg < nregs; ++reg)
531     {
532       sbitmap idf = idfs[reg];
533       int b, changed;
534
535       /* Start the iterative process by considering those blocks that
536          evaluate REG.  We'll add their dominance frontiers to the
537          IDF, and then consider the blocks we just added.  */
538       sbitmap_copy (worklist, evals[reg]);
539
540       /* Morgan's algorithm is incorrect here.  Blocks that evaluate
541          REG aren't necessarily in REG's IDF.  Start with an empty IDF.  */
542       sbitmap_zero (idf);
543
544       /* Iterate until the worklist is empty.  */
545       do
546         {
547           changed = 0;
548           passes++;
549           EXECUTE_IF_SET_IN_SBITMAP (worklist, 0, b,
550             {
551               RESET_BIT (worklist, b);
552               /* For each block on the worklist, add to the IDF all
553                  blocks on its dominance frontier that aren't already
554                  on the IDF.  Every block that's added is also added
555                  to the worklist.  */
556               sbitmap_union_of_diff (worklist, worklist, frontiers[b], idf);
557               sbitmap_a_or_b (idf, idf, frontiers[b]);
558               changed = 1;
559             });
560         }
561       while (changed);
562     }
563
564   sbitmap_free (worklist);
565
566   if (rtl_dump_file)
567     {
568       fprintf (rtl_dump_file,
569                "Iterated dominance frontier: %d passes on %d regs.\n",
570                passes, nregs);
571     }
572 }
573
574 /* Insert the phi nodes.  */
575
576 static void
577 insert_phi_node (int regno, int bb)
578 {
579   basic_block b = BASIC_BLOCK (bb);
580   edge e;
581   int npred, i;
582   rtvec vec;
583   rtx phi, reg;
584   rtx insn;
585   int end_p;
586
587   /* Find out how many predecessors there are.  */
588   for (e = b->pred, npred = 0; e; e = e->pred_next)
589     if (e->src != ENTRY_BLOCK_PTR)
590       npred++;
591
592   /* If this block has no "interesting" preds, then there is nothing to
593      do.  Consider a block that only has the entry block as a pred.  */
594   if (npred == 0)
595     return;
596
597   /* This is the register to which the phi function will be assigned.  */
598   reg = regno_reg_rtx[regno];
599
600   /* Construct the arguments to the PHI node.  The use of pc_rtx is just
601      a placeholder; we'll insert the proper value in rename_registers.  */
602   vec = rtvec_alloc (npred * 2);
603   for (e = b->pred, i = 0; e ; e = e->pred_next, i += 2)
604     if (e->src != ENTRY_BLOCK_PTR)
605       {
606         RTVEC_ELT (vec, i + 0) = pc_rtx;
607         RTVEC_ELT (vec, i + 1) = GEN_INT (e->src->index);
608       }
609
610   phi = gen_rtx_PHI (VOIDmode, vec);
611   phi = gen_rtx_SET (VOIDmode, reg, phi);
612
613   insn = first_insn_after_basic_block_note (b);
614   end_p = PREV_INSN (insn) == b->end;
615   emit_insn_before (phi, insn);
616   if (end_p)
617     b->end = PREV_INSN (insn);
618 }
619
620 static void
621 insert_phi_nodes (sbitmap *idfs, sbitmap *evals ATTRIBUTE_UNUSED, int nregs)
622 {
623   int reg;
624
625   for (reg = 0; reg < nregs; ++reg)
626     if (CONVERT_REGISTER_TO_SSA_P (reg))
627     {
628       int b;
629       EXECUTE_IF_SET_IN_SBITMAP (idfs[reg], 0, b,
630         {
631           if (REGNO_REG_SET_P (BASIC_BLOCK (b)->global_live_at_start, reg))
632             insert_phi_node (reg, b);
633         });
634     }
635 }
636
637 /* Rename the registers to conform to SSA.
638
639    This is essentially the algorithm presented in Figure 7.8 of Morgan,
640    with a few changes to reduce pattern search time in favor of a bit
641    more memory usage.  */
642
643 /* One of these is created for each set.  It will live in a list local
644    to its basic block for the duration of that block's processing.  */
645 struct rename_set_data
646 {
647   struct rename_set_data *next;
648   /* This is the SET_DEST of the (first) SET that sets the REG.  */
649   rtx *reg_loc;
650   /* This is what used to be at *REG_LOC.  */
651   rtx old_reg;
652   /* This is the REG that will replace OLD_REG.  It's set only
653      when the rename data is moved onto the DONE_RENAMES queue.  */
654   rtx new_reg;
655   /* This is what to restore ssa_rename_to_lookup (old_reg) to.  It is
656      usually the previous contents of ssa_rename_to_lookup (old_reg).  */
657   rtx prev_reg;
658   /* This is the insn that contains all the SETs of the REG.  */
659   rtx set_insn;
660 };
661
662 /* This struct is used to pass information to callback functions while
663    renaming registers.  */
664 struct rename_context
665 {
666   struct rename_set_data *new_renames;
667   struct rename_set_data *done_renames;
668   rtx current_insn;
669 };
670
671 /* Queue the rename of *REG_LOC.  */
672 static void
673 create_delayed_rename (struct rename_context *c, rtx *reg_loc)
674 {
675   struct rename_set_data *r;
676   r = (struct rename_set_data *) xmalloc (sizeof(*r));
677
678   if (GET_CODE (*reg_loc) != REG
679       || !CONVERT_REGISTER_TO_SSA_P (REGNO (*reg_loc)))
680     abort ();
681
682   r->reg_loc = reg_loc;
683   r->old_reg = *reg_loc;
684   r->prev_reg = ssa_rename_to_lookup(r->old_reg);
685   r->set_insn = c->current_insn;
686   r->next = c->new_renames;
687   c->new_renames = r;
688 }
689
690 /* This is part of a rather ugly hack to allow the pre-ssa regno to be
691    reused.  If, during processing, a register has not yet been touched,
692    ssa_rename_to[regno][machno] will be NULL.  Now, in the course of pushing
693    and popping values from ssa_rename_to, when we would ordinarily
694    pop NULL back in, we pop RENAME_NO_RTX.  We treat this exactly the
695    same as NULL, except that it signals that the original regno has
696    already been reused.  */
697 #define RENAME_NO_RTX  pc_rtx
698
699 /* Move all the entries from NEW_RENAMES onto DONE_RENAMES by
700    applying all the renames on NEW_RENAMES.  */
701
702 static void
703 apply_delayed_renames (struct rename_context *c)
704 {
705   struct rename_set_data *r;
706   struct rename_set_data *last_r = NULL;
707
708   for (r = c->new_renames; r != NULL; r = r->next)
709     {
710       int new_regno;
711
712       /* Failure here means that someone has a PARALLEL that sets
713          a register twice (bad!).  */
714       if (ssa_rename_to_lookup (r->old_reg) != r->prev_reg)
715         abort ();
716       /* Failure here means we have changed REG_LOC before applying
717          the rename.  */
718       /* For the first set we come across, reuse the original regno.  */
719       if (r->prev_reg == NULL_RTX && !HARD_REGISTER_P (r->old_reg))
720         {
721           r->new_reg = r->old_reg;
722           /* We want to restore RENAME_NO_RTX rather than NULL_RTX.  */
723           r->prev_reg = RENAME_NO_RTX;
724         }
725       else
726         r->new_reg = gen_reg_rtx (GET_MODE (r->old_reg));
727       new_regno = REGNO (r->new_reg);
728       ssa_rename_to_insert (r->old_reg, r->new_reg);
729
730       if (new_regno >= (int) ssa_definition->num_elements)
731         {
732           int new_limit = new_regno * 5 / 4;
733           VARRAY_GROW (ssa_definition, new_limit);
734         }
735
736       VARRAY_RTX (ssa_definition, new_regno) = r->set_insn;
737       ssa_rename_from_insert (new_regno, r->old_reg);
738       last_r = r;
739     }
740   if (last_r != NULL)
741     {
742       last_r->next = c->done_renames;
743       c->done_renames = c->new_renames;
744       c->new_renames = NULL;
745     }
746 }
747
748 /* Part one of the first step of rename_block, called through for_each_rtx.
749    Mark pseudos that are set for later update.  Transform uses of pseudos.  */
750
751 static int
752 rename_insn_1 (rtx *ptr, void *data)
753 {
754   rtx x = *ptr;
755   struct rename_context *context = data;
756
757   if (x == NULL_RTX)
758     return 0;
759
760   switch (GET_CODE (x))
761     {
762     case SET:
763       {
764         rtx *destp = &SET_DEST (x);
765         rtx dest = SET_DEST (x);
766
767         /* An assignment to a paradoxical SUBREG does not read from
768            the destination operand, and thus does not need to be
769            wrapped into a SEQUENCE when translating into SSA form.
770            We merely strip off the SUBREG and proceed normally for
771            this case.  */
772         if (GET_CODE (dest) == SUBREG
773             && (GET_MODE_SIZE (GET_MODE (dest))
774                 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))
775             && GET_CODE (SUBREG_REG (dest)) == REG
776             && CONVERT_REGISTER_TO_SSA_P (REGNO (SUBREG_REG (dest))))
777           {
778             destp = &XEXP (dest, 0);
779             dest = XEXP (dest, 0);
780           }
781
782         /* Some SETs also use the REG specified in their LHS.
783            These can be detected by the presence of
784            STRICT_LOW_PART, SUBREG, SIGN_EXTRACT, and ZERO_EXTRACT
785            in the LHS.  Handle these by changing
786            (set (subreg (reg foo)) ...)
787            into
788            (sequence [(set (reg foo_1) (reg foo))
789                       (set (subreg (reg foo_1)) ...)])
790
791            FIXME: Much of the time this is too much.  For some constructs
792            we know that the output register is strictly an output
793            (paradoxical SUBREGs and some libcalls for example).
794
795            For those cases we are better off not making the false
796            dependency.  */
797         if (GET_CODE (dest) == STRICT_LOW_PART
798             || GET_CODE (dest) == SUBREG
799             || GET_CODE (dest) == SIGN_EXTRACT
800             || GET_CODE (dest) == ZERO_EXTRACT)
801           {
802             rtx i, reg;
803             reg = dest;
804
805             while (GET_CODE (reg) == STRICT_LOW_PART
806                    || GET_CODE (reg) == SUBREG
807                    || GET_CODE (reg) == SIGN_EXTRACT
808                    || GET_CODE (reg) == ZERO_EXTRACT)
809                 reg = XEXP (reg, 0);
810
811             if (GET_CODE (reg) == REG
812                 && CONVERT_REGISTER_TO_SSA_P (REGNO (reg)))
813               {
814                 /* Generate (set reg reg), and do renaming on it so
815                    that it becomes (set reg_1 reg_0), and we will
816                    replace reg with reg_1 in the SUBREG.  */
817
818                 struct rename_set_data *saved_new_renames;
819                 saved_new_renames = context->new_renames;
820                 context->new_renames = NULL;
821                 i = emit_insn (gen_rtx_SET (VOIDmode, reg, reg));
822                 for_each_rtx (&i, rename_insn_1, data);
823                 apply_delayed_renames (context);
824                 context->new_renames = saved_new_renames;
825               }
826           }
827         else if (GET_CODE (dest) == REG
828                  && CONVERT_REGISTER_TO_SSA_P (REGNO (dest)))
829           {
830             /* We found a genuine set of an interesting register.  Tag
831                it so that we can create a new name for it after we finish
832                processing this insn.  */
833
834             create_delayed_rename (context, destp);
835
836             /* Since we do not wish to (directly) traverse the
837                SET_DEST, recurse through for_each_rtx for the SET_SRC
838                and return.  */
839             if (GET_CODE (x) == SET)
840               for_each_rtx (&SET_SRC (x), rename_insn_1, data);
841             return -1;
842           }
843
844         /* Otherwise, this was not an interesting destination.  Continue
845            on, marking uses as normal.  */
846         return 0;
847       }
848
849     case REG:
850       if (CONVERT_REGISTER_TO_SSA_P (REGNO (x))
851           && REGNO (x) < ssa_max_reg_num)
852         {
853           rtx new_reg = ssa_rename_to_lookup (x);
854
855           if (new_reg != RENAME_NO_RTX && new_reg != NULL_RTX)
856             {
857               if (GET_MODE (x) != GET_MODE (new_reg))
858                 abort ();
859               *ptr = new_reg;
860             }
861           else
862             {
863               /* Undefined value used, rename it to a new pseudo register so
864                  that it cannot conflict with an existing register.  */
865               *ptr = gen_reg_rtx (GET_MODE (x));
866             }
867         }
868       return -1;
869
870     case CLOBBER:
871       /* There is considerable debate on how CLOBBERs ought to be
872          handled in SSA.  For now, we're keeping the CLOBBERs, which
873          means that we don't really have SSA form.  There are a couple
874          of proposals for how to fix this problem, but neither is
875          implemented yet.  */
876       {
877         rtx dest = XCEXP (x, 0, CLOBBER);
878         if (REG_P (dest))
879           {
880             if (CONVERT_REGISTER_TO_SSA_P (REGNO (dest))
881                 && REGNO (dest) < ssa_max_reg_num)
882               {
883                 rtx new_reg = ssa_rename_to_lookup (dest);
884                 if (new_reg != NULL_RTX && new_reg != RENAME_NO_RTX)
885                     XCEXP (x, 0, CLOBBER) = new_reg;
886               }
887             /* Stop traversing.  */
888             return -1;
889           }
890         else
891           /* Continue traversing.  */
892           return 0;
893       }
894
895     case PHI:
896       /* Never muck with the phi.  We do that elsewhere, special-like.  */
897       return -1;
898
899     default:
900       /* Anything else, continue traversing.  */
901       return 0;
902     }
903 }
904
905 static rtx
906 gen_sequence (void)
907 {
908   rtx first_insn = get_insns ();
909   rtx result;
910   rtx tem;
911   int i;
912   int len;
913
914   /* Count the insns in the chain.  */
915   len = 0;
916   for (tem = first_insn; tem; tem = NEXT_INSN (tem))
917     len++;
918
919   result = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (len));
920
921   for (i = 0, tem = first_insn; tem; tem = NEXT_INSN (tem), i++)
922     XVECEXP (result, 0, i) = tem;
923
924   return result;
925 }
926
927 static void
928 rename_block (int bb, dominance_info idom)
929 {
930   basic_block b = BASIC_BLOCK (bb);
931   edge e;
932   rtx insn, next, last;
933   struct rename_set_data *set_data = NULL;
934   basic_block c;
935
936   /* Step One: Walk the basic block, adding new names for sets and
937      replacing uses.  */
938
939   next = b->head;
940   last = b->end;
941   do
942     {
943       insn = next;
944       if (INSN_P (insn))
945         {
946           struct rename_context context;
947           context.done_renames = set_data;
948           context.new_renames = NULL;
949           context.current_insn = insn;
950
951           start_sequence ();
952           for_each_rtx (&PATTERN (insn), rename_insn_1, &context);
953           for_each_rtx (&REG_NOTES (insn), rename_insn_1, &context);
954
955           /* Sometimes, we end up with a sequence of insns that
956              SSA needs to treat as a single insn.  Wrap these in a
957              SEQUENCE.  (Any notes now get attached to the SEQUENCE,
958              not to the old version inner insn.)  */
959           if (get_insns () != NULL_RTX)
960             {
961               rtx seq;
962               int i;
963
964               emit (PATTERN (insn));
965               seq = gen_sequence ();
966               /* We really want a SEQUENCE of SETs, not a SEQUENCE
967                  of INSNs.  */
968               for (i = 0; i < XVECLEN (seq, 0); i++)
969                 XVECEXP (seq, 0, i) = PATTERN (XVECEXP (seq, 0, i));
970               PATTERN (insn) = seq;
971             }
972           end_sequence ();
973
974           apply_delayed_renames (&context);
975           set_data = context.done_renames;
976         }
977
978       next = NEXT_INSN (insn);
979     }
980   while (insn != last);
981
982   /* Step Two: Update the phi nodes of this block's successors.  */
983
984   for (e = b->succ; e; e = e->succ_next)
985     {
986       if (e->dest == EXIT_BLOCK_PTR)
987         continue;
988
989       insn = first_insn_after_basic_block_note (e->dest);
990
991       while (PHI_NODE_P (insn))
992         {
993           rtx phi = PATTERN (insn);
994           rtx reg;
995
996           /* Find out which of our outgoing registers this node is
997              intended to replace.  Note that if this is not the first PHI
998              node to have been created for this register, we have to
999              jump through rename links to figure out which register
1000              we're talking about.  This can easily be recognized by
1001              noting that the regno is new to this pass.  */
1002           reg = SET_DEST (phi);
1003           if (REGNO (reg) >= ssa_max_reg_num)
1004             reg = ssa_rename_from_lookup (REGNO (reg));
1005           if (reg == NULL_RTX)
1006             abort ();
1007           reg = ssa_rename_to_lookup (reg);
1008
1009           /* It is possible for the variable to be uninitialized on
1010              edges in.  Reduce the arity of the PHI so that we don't
1011              consider those edges.  */
1012           if (reg == NULL || reg == RENAME_NO_RTX)
1013             {
1014               if (! remove_phi_alternative (phi, b))
1015                 abort ();
1016             }
1017           else
1018             {
1019               /* When we created the PHI nodes, we did not know what mode
1020                  the register should be.  Now that we've found an original,
1021                  we can fill that in.  */
1022               if (GET_MODE (SET_DEST (phi)) == VOIDmode)
1023                 PUT_MODE (SET_DEST (phi), GET_MODE (reg));
1024               else if (GET_MODE (SET_DEST (phi)) != GET_MODE (reg))
1025                 abort ();
1026
1027               *phi_alternative (phi, bb) = reg;
1028             }
1029
1030           insn = NEXT_INSN (insn);
1031         }
1032     }
1033
1034   /* Step Three: Do the same to the children of this block in
1035      dominator order.  */
1036
1037   FOR_EACH_BB (c)
1038     if (get_immediate_dominator (idom, c)->index == bb)
1039       rename_block (c->index, idom);
1040
1041   /* Step Four: Update the sets to refer to their new register,
1042      and restore ssa_rename_to to its previous state.  */
1043
1044   while (set_data)
1045     {
1046       struct rename_set_data *next;
1047       rtx old_reg = *set_data->reg_loc;
1048
1049       if (*set_data->reg_loc != set_data->old_reg)
1050         abort ();
1051       *set_data->reg_loc = set_data->new_reg;
1052
1053       ssa_rename_to_insert (old_reg, set_data->prev_reg);
1054
1055       next = set_data->next;
1056       free (set_data);
1057       set_data = next;
1058     }
1059 }
1060
1061 static void
1062 rename_registers (int nregs, dominance_info idom)
1063 {
1064   VARRAY_RTX_INIT (ssa_definition, nregs * 3, "ssa_definition");
1065   ssa_rename_from_initialize ();
1066
1067   ssa_rename_to_pseudo = (rtx *) alloca (nregs * sizeof(rtx));
1068   memset ((char *) ssa_rename_to_pseudo, 0, nregs * sizeof(rtx));
1069   memset ((char *) ssa_rename_to_hard, 0,
1070          FIRST_PSEUDO_REGISTER * NUM_MACHINE_MODES * sizeof (rtx));
1071
1072   rename_block (0, idom);
1073
1074   /* ??? Update basic_block_live_at_start, and other flow info
1075      as needed.  */
1076
1077   ssa_rename_to_pseudo = NULL;
1078 }
1079
1080 /* The main entry point for moving to SSA.  */
1081
1082 void
1083 convert_to_ssa (void)
1084 {
1085   /* Element I is the set of blocks that set register I.  */
1086   sbitmap *evals;
1087
1088   /* Dominator bitmaps.  */
1089   sbitmap *dfs;
1090   sbitmap *idfs;
1091
1092   /* Element I is the immediate dominator of block I.  */
1093   dominance_info idom;
1094
1095   int nregs;
1096
1097   basic_block bb;
1098
1099   /* Don't do it twice.  */
1100   if (in_ssa_form)
1101     abort ();
1102
1103   /* Need global_live_at_{start,end} up to date.  Do not remove any
1104      dead code.  We'll let the SSA optimizers do that.  */
1105   life_analysis (get_insns (), NULL, 0);
1106
1107   idom = calculate_dominance_info (CDI_DOMINATORS);
1108
1109   if (rtl_dump_file)
1110     {
1111       fputs (";; Immediate Dominators:\n", rtl_dump_file);
1112       FOR_EACH_BB (bb)
1113         fprintf (rtl_dump_file, ";\t%3d = %3d\n", bb->index,
1114                  get_immediate_dominator (idom, bb)->index);
1115       fflush (rtl_dump_file);
1116     }
1117
1118   /* Compute dominance frontiers.  */
1119
1120   dfs = sbitmap_vector_alloc (last_basic_block, last_basic_block);
1121   compute_dominance_frontiers (dfs, idom);
1122
1123   if (rtl_dump_file)
1124     {
1125       dump_sbitmap_vector (rtl_dump_file, ";; Dominance Frontiers:",
1126                            "; Basic Block", dfs, last_basic_block);
1127       fflush (rtl_dump_file);
1128     }
1129
1130   /* Compute register evaluations.  */
1131
1132   ssa_max_reg_num = max_reg_num ();
1133   nregs = ssa_max_reg_num;
1134   evals = sbitmap_vector_alloc (nregs, last_basic_block);
1135   find_evaluations (evals, nregs);
1136
1137   /* Compute the iterated dominance frontier for each register.  */
1138
1139   idfs = sbitmap_vector_alloc (nregs, last_basic_block);
1140   compute_iterated_dominance_frontiers (idfs, dfs, evals, nregs);
1141
1142   if (rtl_dump_file)
1143     {
1144       dump_sbitmap_vector (rtl_dump_file, ";; Iterated Dominance Frontiers:",
1145                            "; Register", idfs, nregs);
1146       fflush (rtl_dump_file);
1147     }
1148
1149   /* Insert the phi nodes.  */
1150
1151   insert_phi_nodes (idfs, evals, nregs);
1152
1153   /* Rename the registers to satisfy SSA.  */
1154
1155   rename_registers (nregs, idom);
1156
1157   /* All done!  Clean up and go home.  */
1158
1159   sbitmap_vector_free (dfs);
1160   sbitmap_vector_free (evals);
1161   sbitmap_vector_free (idfs);
1162   in_ssa_form = 1;
1163
1164   reg_scan (get_insns (), max_reg_num (), 1);
1165   free_dominance_info (idom);
1166 }
1167
1168 /* REG is the representative temporary of its partition.  Add it to the
1169    set of nodes to be processed, if it hasn't been already.  Return the
1170    index of this register in the node set.  */
1171
1172 static inline int
1173 ephi_add_node (rtx reg, rtx *nodes, int *n_nodes)
1174 {
1175   int i;
1176   for (i = *n_nodes - 1; i >= 0; --i)
1177     if (REGNO (reg) == REGNO (nodes[i]))
1178       return i;
1179
1180   nodes[i = (*n_nodes)++] = reg;
1181   return i;
1182 }
1183
1184 /* Part one of the topological sort.  This is a forward (downward) search
1185    through the graph collecting a stack of nodes to process.  Assuming no
1186    cycles, the nodes at top of the stack when we are finished will have
1187    no other dependencies.  */
1188
1189 static int *
1190 ephi_forward (int t, sbitmap visited, sbitmap *succ, int *tstack)
1191 {
1192   int s;
1193
1194   SET_BIT (visited, t);
1195
1196   EXECUTE_IF_SET_IN_SBITMAP (succ[t], 0, s,
1197     {
1198       if (! TEST_BIT (visited, s))
1199         tstack = ephi_forward (s, visited, succ, tstack);
1200     });
1201
1202   *tstack++ = t;
1203   return tstack;
1204 }
1205
1206 /* Part two of the topological sort.  The is a backward search through
1207    a cycle in the graph, copying the data forward as we go.  */
1208
1209 static void
1210 ephi_backward (int t, sbitmap visited, sbitmap *pred, rtx *nodes)
1211 {
1212   int p;
1213
1214   SET_BIT (visited, t);
1215
1216   EXECUTE_IF_SET_IN_SBITMAP (pred[t], 0, p,
1217     {
1218       if (! TEST_BIT (visited, p))
1219         {
1220           ephi_backward (p, visited, pred, nodes);
1221           emit_move_insn (nodes[p], nodes[t]);
1222         }
1223     });
1224 }
1225
1226 /* Part two of the topological sort.  Create the copy for a register
1227    and any cycle of which it is a member.  */
1228
1229 static void
1230 ephi_create (int t, sbitmap visited, sbitmap *pred, sbitmap *succ, rtx *nodes)
1231 {
1232   rtx reg_u = NULL_RTX;
1233   int unvisited_predecessors = 0;
1234   int p;
1235
1236   /* Iterate through the predecessor list looking for unvisited nodes.
1237      If there are any, we have a cycle, and must deal with that.  At
1238      the same time, look for a visited predecessor.  If there is one,
1239      we won't need to create a temporary.  */
1240
1241   EXECUTE_IF_SET_IN_SBITMAP (pred[t], 0, p,
1242     {
1243       if (! TEST_BIT (visited, p))
1244         unvisited_predecessors = 1;
1245       else if (!reg_u)
1246         reg_u = nodes[p];
1247     });
1248
1249   if (unvisited_predecessors)
1250     {
1251       /* We found a cycle.  Copy out one element of the ring (if necessary),
1252          then traverse the ring copying as we go.  */
1253
1254       if (!reg_u)
1255         {
1256           reg_u = gen_reg_rtx (GET_MODE (nodes[t]));
1257           emit_move_insn (reg_u, nodes[t]);
1258         }
1259
1260       EXECUTE_IF_SET_IN_SBITMAP (pred[t], 0, p,
1261         {
1262           if (! TEST_BIT (visited, p))
1263             {
1264               ephi_backward (p, visited, pred, nodes);
1265               emit_move_insn (nodes[p], reg_u);
1266             }
1267         });
1268     }
1269   else
1270     {
1271       /* No cycle.  Just copy the value from a successor.  */
1272
1273       int s;
1274       EXECUTE_IF_SET_IN_SBITMAP (succ[t], 0, s,
1275         {
1276           SET_BIT (visited, t);
1277           emit_move_insn (nodes[t], nodes[s]);
1278           return;
1279         });
1280     }
1281 }
1282
1283 /* Convert the edge to normal form.  */
1284
1285 static void
1286 eliminate_phi (edge e, partition reg_partition)
1287 {
1288   int n_nodes;
1289   sbitmap *pred, *succ;
1290   sbitmap visited;
1291   rtx *nodes;
1292   int *stack, *tstack;
1293   rtx insn;
1294   int i;
1295
1296   /* Collect an upper bound on the number of registers needing processing.  */
1297
1298   insn = first_insn_after_basic_block_note (e->dest);
1299
1300   n_nodes = 0;
1301   while (PHI_NODE_P (insn))
1302     {
1303       insn = next_nonnote_insn (insn);
1304       n_nodes += 2;
1305     }
1306
1307   if (n_nodes == 0)
1308     return;
1309
1310   /* Build the auxiliary graph R(B).
1311
1312      The nodes of the graph are the members of the register partition
1313      present in Phi(B).  There is an edge from FIND(T0)->FIND(T1) for
1314      each T0 = PHI(...,T1,...), where T1 is for the edge from block C.  */
1315
1316   nodes = (rtx *) alloca (n_nodes * sizeof(rtx));
1317   pred = sbitmap_vector_alloc (n_nodes, n_nodes);
1318   succ = sbitmap_vector_alloc (n_nodes, n_nodes);
1319   sbitmap_vector_zero (pred, n_nodes);
1320   sbitmap_vector_zero (succ, n_nodes);
1321
1322   insn = first_insn_after_basic_block_note (e->dest);
1323
1324   n_nodes = 0;
1325   for (; PHI_NODE_P (insn); insn = next_nonnote_insn (insn))
1326     {
1327       rtx* preg = phi_alternative (PATTERN (insn), e->src->index);
1328       rtx tgt = SET_DEST (PATTERN (insn));
1329       rtx reg;
1330
1331       /* There may be no phi alternative corresponding to this edge.
1332          This indicates that the phi variable is undefined along this
1333          edge.  */
1334       if (preg == NULL)
1335         continue;
1336       reg = *preg;
1337
1338       if (GET_CODE (reg) != REG || GET_CODE (tgt) != REG)
1339         abort ();
1340
1341       reg = regno_reg_rtx[partition_find (reg_partition, REGNO (reg))];
1342       tgt = regno_reg_rtx[partition_find (reg_partition, REGNO (tgt))];
1343       /* If the two registers are already in the same partition,
1344          nothing will need to be done.  */
1345       if (reg != tgt)
1346         {
1347           int ireg, itgt;
1348
1349           ireg = ephi_add_node (reg, nodes, &n_nodes);
1350           itgt = ephi_add_node (tgt, nodes, &n_nodes);
1351
1352           SET_BIT (pred[ireg], itgt);
1353           SET_BIT (succ[itgt], ireg);
1354         }
1355     }
1356
1357   if (n_nodes == 0)
1358     goto out;
1359
1360   /* Begin a topological sort of the graph.  */
1361
1362   visited = sbitmap_alloc (n_nodes);
1363   sbitmap_zero (visited);
1364
1365   tstack = stack = (int *) alloca (n_nodes * sizeof (int));
1366
1367   for (i = 0; i < n_nodes; ++i)
1368     if (! TEST_BIT (visited, i))
1369       tstack = ephi_forward (i, visited, succ, tstack);
1370
1371   sbitmap_zero (visited);
1372
1373   /* As we find a solution to the tsort, collect the implementation
1374      insns in a sequence.  */
1375   start_sequence ();
1376
1377   while (tstack != stack)
1378     {
1379       i = *--tstack;
1380       if (! TEST_BIT (visited, i))
1381         ephi_create (i, visited, pred, succ, nodes);
1382     }
1383
1384   insn = get_insns ();
1385   end_sequence ();
1386   insert_insn_on_edge (insn, e);
1387   if (rtl_dump_file)
1388     fprintf (rtl_dump_file, "Emitting copy on edge (%d,%d)\n",
1389              e->src->index, e->dest->index);
1390
1391   sbitmap_free (visited);
1392 out:
1393   sbitmap_vector_free (pred);
1394   sbitmap_vector_free (succ);
1395 }
1396
1397 /* For basic block B, consider all phi insns which provide an
1398    alternative corresponding to an incoming abnormal critical edge.
1399    Place the phi alternative corresponding to that abnormal critical
1400    edge in the same register class as the destination of the set.
1401
1402    From Morgan, p. 178:
1403
1404      For each abnormal critical edge (C, B),
1405      if T0 = phi (T1, ..., Ti, ..., Tm) is a phi node in B,
1406      and C is the ith predecessor of B,
1407      then T0 and Ti must be equivalent.
1408
1409    Return nonzero iff any such cases were found for which the two
1410    regs were not already in the same class.  */
1411
1412 static int
1413 make_regs_equivalent_over_bad_edges (int bb, partition reg_partition)
1414 {
1415   int changed = 0;
1416   basic_block b = BASIC_BLOCK (bb);
1417   rtx phi;
1418
1419   /* Advance to the first phi node.  */
1420   phi = first_insn_after_basic_block_note (b);
1421
1422   /* Scan all the phi nodes.  */
1423   for (;
1424        PHI_NODE_P (phi);
1425        phi = next_nonnote_insn (phi))
1426     {
1427       edge e;
1428       int tgt_regno;
1429       rtx set = PATTERN (phi);
1430       rtx tgt = SET_DEST (set);
1431
1432       /* The set target is expected to be an SSA register.  */
1433       if (GET_CODE (tgt) != REG
1434           || !CONVERT_REGISTER_TO_SSA_P (REGNO (tgt)))
1435         abort ();
1436       tgt_regno = REGNO (tgt);
1437
1438       /* Scan incoming abnormal critical edges.  */
1439       for (e = b->pred; e; e = e->pred_next)
1440         if ((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
1441           {
1442             rtx *alt = phi_alternative (set, e->src->index);
1443             int alt_regno;
1444
1445             /* If there is no alternative corresponding to this edge,
1446                the value is undefined along the edge, so just go on.  */
1447             if (alt == 0)
1448               continue;
1449
1450             /* The phi alternative is expected to be an SSA register.  */
1451             if (GET_CODE (*alt) != REG
1452                 || !CONVERT_REGISTER_TO_SSA_P (REGNO (*alt)))
1453               abort ();
1454             alt_regno = REGNO (*alt);
1455
1456             /* If the set destination and the phi alternative aren't
1457                already in the same class...  */
1458             if (partition_find (reg_partition, tgt_regno)
1459                 != partition_find (reg_partition, alt_regno))
1460               {
1461                 /* ... make them such.  */
1462                 if (conflicting_hard_regs_p (tgt_regno, alt_regno))
1463                   /* It is illegal to unify a hard register with a
1464                      different register.  */
1465                   abort ();
1466
1467                 partition_union (reg_partition,
1468                                  tgt_regno, alt_regno);
1469                 ++changed;
1470               }
1471           }
1472     }
1473
1474   return changed;
1475 }
1476
1477 /* Consider phi insns in basic block BB pairwise.  If the set target
1478    of both insns are equivalent pseudos, make the corresponding phi
1479    alternatives in each phi corresponding equivalent.
1480
1481    Return nonzero if any new register classes were unioned.  */
1482
1483 static int
1484 make_equivalent_phi_alternatives_equivalent (int bb, partition reg_partition)
1485 {
1486   int changed = 0;
1487   basic_block b = BASIC_BLOCK (bb);
1488   rtx phi;
1489
1490   /* Advance to the first phi node.  */
1491   phi = first_insn_after_basic_block_note (b);
1492
1493   /* Scan all the phi nodes.  */
1494   for (;
1495        PHI_NODE_P (phi);
1496        phi = next_nonnote_insn (phi))
1497     {
1498       rtx set = PATTERN (phi);
1499       /* The regno of the destination of the set.  */
1500       int tgt_regno = REGNO (SET_DEST (PATTERN (phi)));
1501
1502       rtx phi2 = next_nonnote_insn (phi);
1503
1504       /* Scan all phi nodes following this one.  */
1505       for (;
1506            PHI_NODE_P (phi2);
1507            phi2 = next_nonnote_insn (phi2))
1508         {
1509           rtx set2 = PATTERN (phi2);
1510           /* The regno of the destination of the set.  */
1511           int tgt2_regno = REGNO (SET_DEST (set2));
1512
1513           /* Are the set destinations equivalent regs?  */
1514           if (partition_find (reg_partition, tgt_regno) ==
1515               partition_find (reg_partition, tgt2_regno))
1516             {
1517               edge e;
1518               /* Scan over edges.  */
1519               for (e = b->pred; e; e = e->pred_next)
1520                 {
1521                   int pred_block = e->src->index;
1522                   /* Identify the phi alternatives from both phi
1523                      nodes corresponding to this edge.  */
1524                   rtx *alt = phi_alternative (set, pred_block);
1525                   rtx *alt2 = phi_alternative (set2, pred_block);
1526
1527                   /* If one of the phi nodes doesn't have a
1528                      corresponding alternative, just skip it.  */
1529                   if (alt == 0 || alt2 == 0)
1530                     continue;
1531
1532                   /* Both alternatives should be SSA registers.  */
1533                   if (GET_CODE (*alt) != REG
1534                       || !CONVERT_REGISTER_TO_SSA_P (REGNO (*alt)))
1535                     abort ();
1536                   if (GET_CODE (*alt2) != REG
1537                       || !CONVERT_REGISTER_TO_SSA_P (REGNO (*alt2)))
1538                     abort ();
1539
1540                   /* If the alternatives aren't already in the same
1541                      class ...  */
1542                   if (partition_find (reg_partition, REGNO (*alt))
1543                       != partition_find (reg_partition, REGNO (*alt2)))
1544                     {
1545                       /* ... make them so.  */
1546                       if (conflicting_hard_regs_p (REGNO (*alt), REGNO (*alt2)))
1547                         /* It is illegal to unify a hard register with
1548                            a different register.  */
1549                         abort ();
1550
1551                       partition_union (reg_partition,
1552                                        REGNO (*alt), REGNO (*alt2));
1553                       ++changed;
1554                     }
1555                 }
1556             }
1557         }
1558     }
1559
1560   return changed;
1561 }
1562
1563 /* Compute a conservative partition of outstanding pseudo registers.
1564    See Morgan 7.3.1.  */
1565
1566 static partition
1567 compute_conservative_reg_partition (void)
1568 {
1569   basic_block bb;
1570   int changed = 0;
1571
1572   /* We don't actually work with hard registers, but it's easier to
1573      carry them around anyway rather than constantly doing register
1574      number arithmetic.  */
1575   partition p =
1576     partition_new (ssa_definition->num_elements);
1577
1578   /* The first priority is to make sure registers that might have to
1579      be copied on abnormal critical edges are placed in the same
1580      partition.  This saves us from having to split abnormal critical
1581      edges.  */
1582   FOR_EACH_BB_REVERSE (bb)
1583     changed += make_regs_equivalent_over_bad_edges (bb->index, p);
1584
1585   /* Now we have to insure that corresponding arguments of phi nodes
1586      assigning to corresponding regs are equivalent.  Iterate until
1587      nothing changes.  */
1588   while (changed > 0)
1589     {
1590       changed = 0;
1591       FOR_EACH_BB_REVERSE (bb)
1592         changed += make_equivalent_phi_alternatives_equivalent (bb->index, p);
1593     }
1594
1595   return p;
1596 }
1597
1598 /* The following functions compute a register partition that attempts
1599    to eliminate as many reg copies and phi node copies as possible by
1600    coalescing registers.   This is the strategy:
1601
1602     1. As in the conservative case, the top priority is to coalesce
1603        registers that otherwise would cause copies to be placed on
1604        abnormal critical edges (which isn't possible).
1605
1606     2. Figure out which regs are involved (in the LHS or RHS) of
1607        copies and phi nodes.  Compute conflicts among these regs.
1608
1609     3. Walk around the instruction stream, placing two regs in the
1610        same class of the partition if one appears on the LHS and the
1611        other on the RHS of a copy or phi node and the two regs don't
1612        conflict.  The conflict information of course needs to be
1613        updated.
1614
1615     4. If anything has changed, there may be new opportunities to
1616        coalesce regs, so go back to 2.
1617 */
1618
1619 /* If REG1 and REG2 don't conflict in CONFLICTS, place them in the
1620    same class of partition P, if they aren't already.  Update
1621    CONFLICTS appropriately.
1622
1623    Returns one if REG1 and REG2 were placed in the same class but were
1624    not previously; zero otherwise.
1625
1626    See Morgan figure 11.15.  */
1627
1628 static int
1629 coalesce_if_unconflicting (partition p, conflict_graph conflicts,
1630                            int reg1, int reg2)
1631 {
1632   int reg;
1633
1634   /* Work only on SSA registers.  */
1635   if (!CONVERT_REGISTER_TO_SSA_P (reg1) || !CONVERT_REGISTER_TO_SSA_P (reg2))
1636     return 0;
1637
1638   /* Find the canonical regs for the classes containing REG1 and
1639      REG2.  */
1640   reg1 = partition_find (p, reg1);
1641   reg2 = partition_find (p, reg2);
1642
1643   /* If they're already in the same class, there's nothing to do.  */
1644   if (reg1 == reg2)
1645     return 0;
1646
1647   /* If the regs conflict, our hands are tied.  */
1648   if (conflicting_hard_regs_p (reg1, reg2) ||
1649       conflict_graph_conflict_p (conflicts, reg1, reg2))
1650     return 0;
1651
1652   /* We're good to go.  Put the regs in the same partition.  */
1653   partition_union (p, reg1, reg2);
1654
1655   /* Find the new canonical reg for the merged class.  */
1656   reg = partition_find (p, reg1);
1657
1658   /* Merge conflicts from the two previous classes.  */
1659   conflict_graph_merge_regs (conflicts, reg, reg1);
1660   conflict_graph_merge_regs (conflicts, reg, reg2);
1661
1662   return 1;
1663 }
1664
1665 /* For each register copy insn in basic block BB, place the LHS and
1666    RHS regs in the same class in partition P if they do not conflict
1667    according to CONFLICTS.
1668
1669    Returns the number of changes that were made to P.
1670
1671    See Morgan figure 11.14.  */
1672
1673 static int
1674 coalesce_regs_in_copies (basic_block bb, partition p, conflict_graph conflicts)
1675 {
1676   int changed = 0;
1677   rtx insn;
1678   rtx end = bb->end;
1679
1680   /* Scan the instruction stream of the block.  */
1681   for (insn = bb->head; insn != end; insn = NEXT_INSN (insn))
1682     {
1683       rtx pattern;
1684       rtx src;
1685       rtx dest;
1686
1687       /* If this isn't a set insn, go to the next insn.  */
1688       if (GET_CODE (insn) != INSN)
1689         continue;
1690       pattern = PATTERN (insn);
1691       if (GET_CODE (pattern) != SET)
1692         continue;
1693
1694       src = SET_SRC (pattern);
1695       dest = SET_DEST (pattern);
1696
1697       /* We're only looking for copies.  */
1698       if (GET_CODE (src) != REG || GET_CODE (dest) != REG)
1699         continue;
1700
1701       /* Coalesce only if the reg modes are the same.  As long as
1702          each reg's rtx is unique, it can have only one mode, so two
1703          pseudos of different modes can't be coalesced into one.
1704
1705          FIXME: We can probably get around this by inserting SUBREGs
1706          where appropriate, but for now we don't bother.  */
1707       if (GET_MODE (src) != GET_MODE (dest))
1708         continue;
1709
1710       /* Found a copy; see if we can use the same reg for both the
1711          source and destination (and thus eliminate the copy,
1712          ultimately).  */
1713       changed += coalesce_if_unconflicting (p, conflicts,
1714                                             REGNO (src), REGNO (dest));
1715     }
1716
1717   return changed;
1718 }
1719
1720 struct phi_coalesce_context
1721 {
1722   partition p;
1723   conflict_graph conflicts;
1724   int changed;
1725 };
1726
1727 /* Callback function for for_each_successor_phi.  If the set
1728    destination and the phi alternative regs do not conflict, place
1729    them in the same partition class.  DATA is a pointer to a
1730    phi_coalesce_context struct.  */
1731
1732 static int
1733 coalesce_reg_in_phi (rtx insn ATTRIBUTE_UNUSED, int dest_regno,
1734                      int src_regno, void *data)
1735 {
1736   struct phi_coalesce_context *context =
1737     (struct phi_coalesce_context *) data;
1738
1739   /* Attempt to use the same reg, if they don't conflict.  */
1740   context->changed
1741     += coalesce_if_unconflicting (context->p, context->conflicts,
1742                                   dest_regno, src_regno);
1743   return 0;
1744 }
1745
1746 /* For each alternative in a phi function corresponding to basic block
1747    BB (in phi nodes in successor block to BB), place the reg in the
1748    phi alternative and the reg to which the phi value is set into the
1749    same class in partition P, if allowed by CONFLICTS.
1750
1751    Return the number of changes that were made to P.
1752
1753    See Morgan figure 11.14.  */
1754
1755 static int
1756 coalesce_regs_in_successor_phi_nodes (basic_block bb, partition p,
1757                                       conflict_graph conflicts)
1758 {
1759   struct phi_coalesce_context context;
1760   context.p = p;
1761   context.conflicts = conflicts;
1762   context.changed = 0;
1763
1764   for_each_successor_phi (bb, &coalesce_reg_in_phi, &context);
1765
1766   return context.changed;
1767 }
1768
1769 /* Compute and return a partition of pseudos.  Where possible,
1770    non-conflicting pseudos are placed in the same class.
1771
1772    The caller is responsible for deallocating the returned partition.  */
1773
1774 static partition
1775 compute_coalesced_reg_partition (void)
1776 {
1777   basic_block bb;
1778   int changed = 0;
1779   regset_head phi_set_head;
1780   regset phi_set = &phi_set_head;
1781
1782   partition p =
1783     partition_new (ssa_definition->num_elements);
1784
1785   /* The first priority is to make sure registers that might have to
1786      be copied on abnormal critical edges are placed in the same
1787      partition.  This saves us from having to split abnormal critical
1788      edges (which can't be done).  */
1789   FOR_EACH_BB_REVERSE (bb)
1790     make_regs_equivalent_over_bad_edges (bb->index, p);
1791
1792   INIT_REG_SET (phi_set);
1793
1794   do
1795     {
1796       conflict_graph conflicts;
1797
1798       changed = 0;
1799
1800       /* Build the set of registers involved in phi nodes, either as
1801          arguments to the phi function or as the target of a set.  */
1802       CLEAR_REG_SET (phi_set);
1803       mark_phi_and_copy_regs (phi_set);
1804
1805       /* Compute conflicts.  */
1806       conflicts = conflict_graph_compute (phi_set, p);
1807
1808       /* FIXME: Better would be to process most frequently executed
1809          blocks first, so that most frequently executed copies would
1810          be more likely to be removed by register coalescing.  But any
1811          order will generate correct, if non-optimal, results.  */
1812       FOR_EACH_BB_REVERSE (bb)
1813         {
1814           changed += coalesce_regs_in_copies (bb, p, conflicts);
1815           changed +=
1816             coalesce_regs_in_successor_phi_nodes (bb, p, conflicts);
1817         }
1818
1819       conflict_graph_delete (conflicts);
1820     }
1821   while (changed > 0);
1822
1823   FREE_REG_SET (phi_set);
1824
1825   return p;
1826 }
1827
1828 /* Mark the regs in a phi node.  PTR is a phi expression or one of its
1829    components (a REG or a CONST_INT).  DATA is a reg set in which to
1830    set all regs.  Called from for_each_rtx.  */
1831
1832 static int
1833 mark_reg_in_phi (rtx *ptr, void *data)
1834 {
1835   rtx expr = *ptr;
1836   regset set = (regset) data;
1837
1838   switch (GET_CODE (expr))
1839     {
1840     case REG:
1841       SET_REGNO_REG_SET (set, REGNO (expr));
1842       /* Fall through.  */
1843     case CONST_INT:
1844     case PHI:
1845       return 0;
1846     default:
1847       abort ();
1848     }
1849 }
1850
1851 /* Mark in PHI_SET all pseudos that are used in a phi node -- either
1852    set from a phi expression, or used as an argument in one.  Also
1853    mark regs that are the source or target of a reg copy.  Uses
1854    ssa_definition.  */
1855
1856 static void
1857 mark_phi_and_copy_regs (regset phi_set)
1858 {
1859   unsigned int reg;
1860
1861   /* Scan the definitions of all regs.  */
1862   for (reg = 0; reg < VARRAY_SIZE (ssa_definition); ++reg)
1863     if (CONVERT_REGISTER_TO_SSA_P (reg))
1864       {
1865         rtx insn = VARRAY_RTX (ssa_definition, reg);
1866         rtx pattern;
1867         rtx src;
1868
1869         if (insn == NULL
1870             || (GET_CODE (insn) == NOTE
1871                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED))
1872           continue;
1873         pattern = PATTERN (insn);
1874         /* Sometimes we get PARALLEL insns.  These aren't phi nodes or
1875            copies.  */
1876         if (GET_CODE (pattern) != SET)
1877           continue;
1878         src = SET_SRC (pattern);
1879
1880         if (GET_CODE (src) == REG)
1881           {
1882             /* It's a reg copy.  */
1883             SET_REGNO_REG_SET (phi_set, reg);
1884             SET_REGNO_REG_SET (phi_set, REGNO (src));
1885           }
1886         else if (GET_CODE (src) == PHI)
1887           {
1888             /* It's a phi node.  Mark the reg being set.  */
1889             SET_REGNO_REG_SET (phi_set, reg);
1890             /* Mark the regs used in the phi function.  */
1891             for_each_rtx (&src, mark_reg_in_phi, phi_set);
1892           }
1893         /* ... else nothing to do.  */
1894       }
1895 }
1896
1897 /* Rename regs in insn PTR that are equivalent.  DATA is the register
1898    partition which specifies equivalences.  */
1899
1900 static int
1901 rename_equivalent_regs_in_insn (rtx *ptr, void* data)
1902 {
1903   rtx x = *ptr;
1904   partition reg_partition = (partition) data;
1905
1906   if (x == NULL_RTX)
1907     return 0;
1908
1909   switch (GET_CODE (x))
1910     {
1911     case REG:
1912       if (CONVERT_REGISTER_TO_SSA_P (REGNO (x)))
1913         {
1914           unsigned int regno = REGNO (x);
1915           unsigned int new_regno = partition_find (reg_partition, regno);
1916           rtx canonical_element_rtx = ssa_rename_from_lookup (new_regno);
1917
1918           if (canonical_element_rtx != NULL_RTX &&
1919               HARD_REGISTER_P (canonical_element_rtx))
1920             {
1921               if (REGNO (canonical_element_rtx) != regno)
1922                 *ptr = canonical_element_rtx;
1923             }
1924           else if (regno != new_regno)
1925             {
1926               rtx new_reg = regno_reg_rtx[new_regno];
1927               if (GET_MODE (x) != GET_MODE (new_reg))
1928                 abort ();
1929               *ptr = new_reg;
1930             }
1931         }
1932       return -1;
1933
1934     case PHI:
1935       /* No need to rename the phi nodes.  We'll check equivalence
1936          when inserting copies.  */
1937       return -1;
1938
1939     default:
1940       /* Anything else, continue traversing.  */
1941       return 0;
1942     }
1943 }
1944
1945 /* Record the register's canonical element stored in SRFP in the
1946    canonical_elements sbitmap packaged in DATA.  This function is used
1947    as a callback function for traversing ssa_rename_from.  */
1948
1949 static int
1950 record_canonical_element_1 (void **srfp, void *data)
1951 {
1952   unsigned int reg = ((ssa_rename_from_pair *) *srfp)->reg;
1953   sbitmap canonical_elements =
1954     ((struct ssa_rename_from_hash_table_data *) data)->canonical_elements;
1955   partition reg_partition =
1956     ((struct ssa_rename_from_hash_table_data *) data)->reg_partition;
1957
1958   SET_BIT (canonical_elements, partition_find (reg_partition, reg));
1959   return 1;
1960 }
1961
1962 /* For each class in the REG_PARTITION corresponding to a particular
1963    hard register and machine mode, check that there are no other
1964    classes with the same hard register and machine mode.  Returns
1965    nonzero if this is the case, i.e., the partition is acceptable.  */
1966
1967 static int
1968 check_hard_regs_in_partition (partition reg_partition)
1969 {
1970   /* CANONICAL_ELEMENTS has a nonzero bit if a class with the given register
1971      number and machine mode has already been seen.  This is a
1972      problem with the partition.  */
1973   sbitmap canonical_elements;
1974   int element_index;
1975   int already_seen[FIRST_PSEUDO_REGISTER][NUM_MACHINE_MODES];
1976   int reg;
1977   int mach_mode;
1978
1979   /* Collect a list of canonical elements.  */
1980   canonical_elements = sbitmap_alloc (max_reg_num ());
1981   sbitmap_zero (canonical_elements);
1982   ssa_rename_from_traverse (&record_canonical_element_1,
1983                             canonical_elements, reg_partition);
1984
1985   /* We have not seen any hard register uses.  */
1986   for (reg = 0; reg < FIRST_PSEUDO_REGISTER; ++reg)
1987     for (mach_mode = 0; mach_mode < NUM_MACHINE_MODES; ++mach_mode)
1988       already_seen[reg][mach_mode] = 0;
1989
1990   /* Check for classes with the same hard register and machine mode.  */
1991   EXECUTE_IF_SET_IN_SBITMAP (canonical_elements, 0, element_index,
1992   {
1993     rtx hard_reg_rtx = ssa_rename_from_lookup (element_index);
1994     if (hard_reg_rtx != NULL_RTX &&
1995         HARD_REGISTER_P (hard_reg_rtx) &&
1996         already_seen[REGNO (hard_reg_rtx)][GET_MODE (hard_reg_rtx)] != 0)
1997           /* Two distinct partition classes should be mapped to the same
1998              hard register.  */
1999           return 0;
2000   });
2001
2002   sbitmap_free (canonical_elements);
2003
2004   return 1;
2005 }
2006
2007 /* Rename regs that are equivalent in REG_PARTITION.  Also collapse
2008    any SEQUENCE insns.  */
2009
2010 static void
2011 rename_equivalent_regs (partition reg_partition)
2012 {
2013   basic_block b;
2014
2015   FOR_EACH_BB_REVERSE (b)
2016     {
2017       rtx next = b->head;
2018       rtx last = b->end;
2019       rtx insn;
2020
2021       do
2022         {
2023           insn = next;
2024           if (INSN_P (insn))
2025             {
2026               for_each_rtx (&PATTERN (insn),
2027                             rename_equivalent_regs_in_insn,
2028                             reg_partition);
2029               for_each_rtx (&REG_NOTES (insn),
2030                             rename_equivalent_regs_in_insn,
2031                             reg_partition);
2032
2033               if (GET_CODE (PATTERN (insn)) == SEQUENCE)
2034                 {
2035                   rtx s = PATTERN (insn);
2036                   int slen = XVECLEN (s, 0);
2037                   int i;
2038
2039                   if (slen <= 1)
2040                     abort ();
2041
2042                   PATTERN (insn) = XVECEXP (s, 0, slen-1);
2043                   for (i = 0; i < slen - 1; i++)
2044                     emit_insn_before (XVECEXP (s, 0, i), insn);
2045                 }
2046             }
2047
2048           next = NEXT_INSN (insn);
2049         }
2050       while (insn != last);
2051     }
2052 }
2053
2054 /* The main entry point for moving from SSA.  */
2055
2056 void
2057 convert_from_ssa (void)
2058 {
2059   basic_block b, bb;
2060   partition reg_partition;
2061   rtx insns = get_insns ();
2062
2063   /* Need global_live_at_{start,end} up to date.  There should not be
2064      any significant dead code at this point, except perhaps dead
2065      stores.  So do not take the time to perform dead code elimination.
2066
2067      Register coalescing needs death notes, so generate them.  */
2068   life_analysis (insns, NULL, PROP_DEATH_NOTES);
2069
2070   /* Figure out which regs in copies and phi nodes don't conflict and
2071      therefore can be coalesced.  */
2072   if (conservative_reg_partition)
2073     reg_partition = compute_conservative_reg_partition ();
2074   else
2075     reg_partition = compute_coalesced_reg_partition ();
2076
2077   if (!check_hard_regs_in_partition (reg_partition))
2078     /* Two separate partitions should correspond to the same hard
2079        register but do not.  */
2080     abort ();
2081
2082   rename_equivalent_regs (reg_partition);
2083
2084   /* Eliminate the PHI nodes.  */
2085   FOR_EACH_BB_REVERSE (b)
2086     {
2087       edge e;
2088
2089       for (e = b->pred; e; e = e->pred_next)
2090         if (e->src != ENTRY_BLOCK_PTR)
2091           eliminate_phi (e, reg_partition);
2092     }
2093
2094   partition_delete (reg_partition);
2095
2096   /* Actually delete the PHI nodes.  */
2097   FOR_EACH_BB_REVERSE (bb)
2098     {
2099       rtx insn = bb->head;
2100
2101       while (1)
2102         {
2103           /* If this is a PHI node delete it.  */
2104           if (PHI_NODE_P (insn))
2105             {
2106               if (insn == bb->end)
2107                 bb->end = PREV_INSN (insn);
2108               insn = delete_insn (insn);
2109             }
2110           /* Since all the phi nodes come at the beginning of the
2111              block, if we find an ordinary insn, we can stop looking
2112              for more phi nodes.  */
2113           else if (INSN_P (insn))
2114             break;
2115           /* If we've reached the end of the block, stop.  */
2116           else if (insn == bb->end)
2117             break;
2118           else
2119             insn = NEXT_INSN (insn);
2120         }
2121     }
2122
2123   /* Commit all the copy nodes needed to convert out of SSA form.  */
2124   commit_edge_insertions ();
2125
2126   in_ssa_form = 0;
2127
2128   count_or_remove_death_notes (NULL, 1);
2129
2130   /* Deallocate the data structures.  */
2131   ssa_definition = 0;
2132   ssa_rename_from_free ();
2133 }
2134
2135 /* Scan phi nodes in successors to BB.  For each such phi node that
2136    has a phi alternative value corresponding to BB, invoke FN.  FN
2137    is passed the entire phi node insn, the regno of the set
2138    destination, the regno of the phi argument corresponding to BB,
2139    and DATA.
2140
2141    If FN ever returns nonzero, stops immediately and returns this
2142    value.  Otherwise, returns zero.  */
2143
2144 int
2145 for_each_successor_phi (basic_block bb, successor_phi_fn fn, void *data)
2146 {
2147   edge e;
2148
2149   if (bb == EXIT_BLOCK_PTR)
2150     return 0;
2151
2152   /* Scan outgoing edges.  */
2153   for (e = bb->succ; e != NULL; e = e->succ_next)
2154     {
2155       rtx insn;
2156
2157       basic_block successor = e->dest;
2158       if (successor == ENTRY_BLOCK_PTR
2159           || successor == EXIT_BLOCK_PTR)
2160         continue;
2161
2162       /* Advance to the first non-label insn of the successor block.  */
2163       insn = first_insn_after_basic_block_note (successor);
2164
2165       if (insn == NULL)
2166         continue;
2167
2168       /* Scan phi nodes in the successor.  */
2169       for ( ; PHI_NODE_P (insn); insn = NEXT_INSN (insn))
2170         {
2171           int result;
2172           rtx phi_set = PATTERN (insn);
2173           rtx *alternative = phi_alternative (phi_set, bb->index);
2174           rtx phi_src;
2175
2176           /* This phi function may not have an alternative
2177              corresponding to the incoming edge, indicating the
2178              assigned variable is not defined along the edge.  */
2179           if (alternative == NULL)
2180             continue;
2181           phi_src = *alternative;
2182
2183           /* Invoke the callback.  */
2184           result = (*fn) (insn, REGNO (SET_DEST (phi_set)),
2185                           REGNO (phi_src), data);
2186
2187           /* Terminate if requested.  */
2188           if (result != 0)
2189             return result;
2190         }
2191     }
2192
2193   return 0;
2194 }
2195
2196 /* Assuming the ssa_rename_from mapping has been established, yields
2197    nonzero if 1) only one SSA register of REG1 and REG2 comes from a
2198    hard register or 2) both SSA registers REG1 and REG2 come from
2199    different hard registers.  */
2200
2201 static int
2202 conflicting_hard_regs_p (int reg1, int reg2)
2203 {
2204   int orig_reg1 = original_register (reg1);
2205   int orig_reg2 = original_register (reg2);
2206   if (HARD_REGISTER_NUM_P (orig_reg1) && HARD_REGISTER_NUM_P (orig_reg2)
2207       && orig_reg1 != orig_reg2)
2208     return 1;
2209   if (HARD_REGISTER_NUM_P (orig_reg1) && !HARD_REGISTER_NUM_P (orig_reg2))
2210     return 1;
2211   if (!HARD_REGISTER_NUM_P (orig_reg1) && HARD_REGISTER_NUM_P (orig_reg2))
2212     return 1;
2213
2214   return 0;
2215 }