OSDN Git Service

* basic-block.h: Fix comment typos.
[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
131   PARAMS ((void));
132 static void ssa_rename_from_initialize
133   PARAMS ((void));
134 static rtx ssa_rename_from_lookup
135   PARAMS ((int reg));
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
141   PARAMS ((void));
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
146   PARAMS ((void));
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));
155
156 static rtx ssa_rename_to_lookup
157   PARAMS ((rtx reg));
158 static void ssa_rename_to_insert
159   PARAMS ((rtx reg, rtx r));
160
161 /* The number of registers that were live on entry to the SSA routines.  */
162 static unsigned int ssa_max_reg_num;
163
164 /* Local function prototypes.  */
165
166 struct rename_context;
167
168 static inline rtx * phi_alternative
169   PARAMS ((rtx, int));
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));
192
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));
205
206 /* These are used only in the conservative register partitioning
207    algorithms.  */
208 static int make_equivalent_phi_alternatives_equivalent
209   PARAMS ((int bb, partition reg_partition));
210 static partition compute_conservative_reg_partition
211   PARAMS ((void));
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));
216
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
227   PARAMS ((void));
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));
232
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));
237
238 /* Deal with hard registers.  */
239 static int conflicting_hard_regs_p
240   PARAMS ((int reg1, int reg2));
241
242 /* ssa_rename_to maps registers and machine modes to SSA pseudo registers.  */
243
244 /* Find the register associated with REG in the indicated mode.  */
245
246 static rtx
247 ssa_rename_to_lookup (reg)
248      rtx reg;
249 {
250   if (!HARD_REGISTER_P (reg))
251     return ssa_rename_to_pseudo[REGNO (reg) - FIRST_PSEUDO_REGISTER];
252   else
253     return ssa_rename_to_hard[REGNO (reg)][GET_MODE (reg)];
254 }
255
256 /* Store a new value mapping REG to R in ssa_rename_to.  */
257
258 static void
259 ssa_rename_to_insert(reg, r)
260      rtx reg;
261      rtx r;
262 {
263   if (!HARD_REGISTER_P (reg))
264     ssa_rename_to_pseudo[REGNO (reg) - FIRST_PSEUDO_REGISTER] = r;
265   else
266     ssa_rename_to_hard[REGNO (reg)][GET_MODE (reg)] = r;
267 }
268
269 /* Prepare ssa_rename_from for use.  */
270
271 static void
272 ssa_rename_from_initialize ()
273 {
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);
279 }
280
281 /* Find the REG entry in ssa_rename_from.  Return NULL_RTX if no entry is
282    found.  */
283
284 static rtx
285 ssa_rename_from_lookup (reg)
286      int reg;
287 {
288   ssa_rename_from_pair srfp;
289   ssa_rename_from_pair *answer;
290   srfp.reg = reg;
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);
295 }
296
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.  */
300
301 static unsigned int
302 original_register (regno)
303      unsigned int regno;
304 {
305   rtx original_rtx = ssa_rename_from_lookup (regno);
306   return original_rtx != NULL_RTX ? REGNO (original_rtx) : regno;
307 }
308
309 /* Add mapping from R to REG to ssa_rename_from even if already present.  */
310
311 static void
312 ssa_rename_from_insert (reg, r)
313      unsigned int reg;
314      rtx r;
315 {
316   void **slot;
317   ssa_rename_from_pair *srfp = xmalloc (sizeof (ssa_rename_from_pair));
318   srfp->reg = reg;
319   srfp->original = r;
320   slot = htab_find_slot_with_hash (ssa_rename_from_ht, (const void *) srfp,
321                                    reg, INSERT);
322   if (*slot != 0)
323     free ((void *) *slot);
324   *slot = srfp;
325 }
326
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.  */
330
331 static void
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;
337 {
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);
342 }
343
344 /* Destroy ssa_rename_from.  */
345
346 static void
347 ssa_rename_from_free ()
348 {
349   htab_delete (ssa_rename_from_ht);
350 }
351
352 /* Print the contents of ssa_rename_from.  */
353
354 /* static  Avoid erroneous error message.  */
355 void
356 ssa_rename_from_print ()
357 {
358   printf ("ssa_rename_from's hash table contents:\n");
359   htab_traverse (ssa_rename_from_ht, &ssa_rename_from_print_1, NULL);
360 }
361
362 /* Print the contents of the hash table entry SLOT, passing the unused
363    sttribute DATA.  Used as a callback function with htab_traverse ().  */
364
365 static int
366 ssa_rename_from_print_1 (slot, data)
367      void **slot;
368      void *data ATTRIBUTE_UNUSED;
369 {
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));
373   return 1;
374 }
375
376 /* Given a hash entry SRFP, yield a hash value.  */
377
378 static hashval_t
379 ssa_rename_from_hash_function (srfp)
380      const void *srfp;
381 {
382   return ((const ssa_rename_from_pair *) srfp)->reg;
383 }
384
385 /* Test whether two hash table entries SRFP1 and SRFP2 are equal.  */
386
387 static int
388 ssa_rename_from_equal (srfp1, srfp2)
389      const void *srfp1;
390      const void *srfp2;
391 {
392   return ssa_rename_from_hash_function (srfp1) ==
393     ssa_rename_from_hash_function (srfp2);
394 }
395
396 /* Delete the hash table entry SRFP.  */
397
398 static void
399 ssa_rename_from_delete (srfp)
400      void *srfp;
401 {
402   free (srfp);
403 }
404
405 /* Given the SET of a PHI node, return the address of the alternative
406    for predecessor block C.  */
407
408 static inline rtx *
409 phi_alternative (set, c)
410      rtx set;
411      int c;
412 {
413   rtvec phi_vec = XVEC (SET_SRC (set), 0);
414   int v;
415
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);
419
420   return NULL;
421 }
422
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
425    found for C.  */
426
427 int
428 remove_phi_alternative (set, block)
429      rtx set;
430      basic_block block;
431 {
432   rtvec phi_vec = XVEC (SET_SRC (set), 0);
433   int num_elem = GET_NUM_ELEM (phi_vec);
434   int v, c;
435
436   c = block->index;
437   for (v = num_elem - 2; v >= 0; v -= 2)
438     if (INTVAL (RTVEC_ELT (phi_vec, v + 1)) == c)
439       {
440         if (v < num_elem - 2)
441           {
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);
444           }
445         PUT_NUM_ELEM (phi_vec, num_elem - 2);
446         return 1;
447       }
448
449   return 0;
450 }
451
452 /* For all registers, find all blocks in which they are set.
453
454    This is the transform of what would be local kill information that
455    we ought to be getting from flow.  */
456
457 static sbitmap *fe_evals;
458 static int fe_current_bb;
459
460 static void
461 find_evaluations_1 (dest, set, data)
462      rtx dest;
463      rtx set ATTRIBUTE_UNUSED;
464      void *data ATTRIBUTE_UNUSED;
465 {
466   if (GET_CODE (dest) == REG
467       && CONVERT_REGISTER_TO_SSA_P (REGNO (dest)))
468     SET_BIT (fe_evals[REGNO (dest)], fe_current_bb);
469 }
470
471 static void
472 find_evaluations (evals, nregs)
473      sbitmap *evals;
474      int nregs;
475 {
476   basic_block bb;
477
478   sbitmap_vector_zero (evals, nregs);
479   fe_evals = evals;
480
481   FOR_EACH_BB_REVERSE (bb)
482     {
483       rtx p, last;
484
485       fe_current_bb = bb->index;
486       p = bb->head;
487       last = bb->end;
488       while (1)
489         {
490           if (INSN_P (p))
491             note_stores (PATTERN (p), find_evaluations_1, NULL);
492
493           if (p == last)
494             break;
495           p = NEXT_INSN (p);
496         }
497     }
498 }
499
500 /* Computing the Dominance Frontier:
501
502    As described 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,
505    there are two cases:
506
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
510    of B.
511
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.
515 */
516
517 static void
518 compute_dominance_frontiers_1 (frontiers, idom, bb, done)
519      sbitmap *frontiers;
520      dominance_info idom;
521      int bb;
522      sbitmap done;
523 {
524   basic_block b = BASIC_BLOCK (bb);
525   edge e;
526   basic_block c;
527
528   SET_BIT (done, bb);
529   sbitmap_zero (frontiers[bb]);
530
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.  */
534   FOR_EACH_BB (c)
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);
538
539   /* Find blocks conforming to rule (1) above.  */
540   for (e = b->succ; e; e = e->succ_next)
541     {
542       if (e->dest == EXIT_BLOCK_PTR)
543         continue;
544       if (get_immediate_dominator (idom, e->dest)->index != bb)
545         SET_BIT (frontiers[bb], e->dest->index);
546     }
547
548   /* Find blocks conforming to rule (2).  */
549   FOR_EACH_BB (c)
550     if (get_immediate_dominator (idom, c)->index == bb)
551       {
552         int x;
553         EXECUTE_IF_SET_IN_SBITMAP (frontiers[c->index], 0, x,
554           {
555             if (get_immediate_dominator (idom, BASIC_BLOCK (x))->index != bb)
556               SET_BIT (frontiers[bb], x);
557           });
558       }
559 }
560
561 void
562 compute_dominance_frontiers (frontiers, idom)
563      sbitmap *frontiers;
564      dominance_info idom;
565 {
566   sbitmap done = sbitmap_alloc (last_basic_block);
567   sbitmap_zero (done);
568
569   compute_dominance_frontiers_1 (frontiers, idom, 0, done);
570
571   sbitmap_free (done);
572 }
573
574 /* Computing the Iterated Dominance Frontier:
575
576    This is the set of merge points for a given register.
577
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.
580 */
581
582 static void
583 compute_iterated_dominance_frontiers (idfs, frontiers, evals, nregs)
584      sbitmap *idfs;
585      sbitmap *frontiers;
586      sbitmap *evals;
587      int nregs;
588 {
589   sbitmap worklist;
590   int reg, passes = 0;
591
592   worklist = sbitmap_alloc (last_basic_block);
593
594   for (reg = 0; reg < nregs; ++reg)
595     {
596       sbitmap idf = idfs[reg];
597       int b, changed;
598
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]);
603
604       /* Morgan's algorithm is incorrect here.  Blocks that evaluate
605          REG aren't necessarily in REG's IDF.  Start with an empty IDF.  */
606       sbitmap_zero (idf);
607
608       /* Iterate until the worklist is empty.  */
609       do
610         {
611           changed = 0;
612           passes++;
613           EXECUTE_IF_SET_IN_SBITMAP (worklist, 0, b,
614             {
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
619                  to the worklist.  */
620               sbitmap_union_of_diff (worklist, worklist, frontiers[b], idf);
621               sbitmap_a_or_b (idf, idf, frontiers[b]);
622               changed = 1;
623             });
624         }
625       while (changed);
626     }
627
628   sbitmap_free (worklist);
629
630   if (rtl_dump_file)
631     {
632       fprintf (rtl_dump_file,
633                "Iterated dominance frontier: %d passes on %d regs.\n",
634                passes, nregs);
635     }
636 }
637
638 /* Insert the phi nodes.  */
639
640 static void
641 insert_phi_node (regno, bb)
642      int regno, bb;
643 {
644   basic_block b = BASIC_BLOCK (bb);
645   edge e;
646   int npred, i;
647   rtvec vec;
648   rtx phi, reg;
649   rtx insn;
650   int end_p;
651
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)
655       npred++;
656
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.  */
659   if (npred == 0)
660     return;
661
662   /* This is the register to which the phi function will be assigned.  */
663   reg = regno_reg_rtx[regno];
664
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)
670       {
671         RTVEC_ELT (vec, i + 0) = pc_rtx;
672         RTVEC_ELT (vec, i + 1) = GEN_INT (e->src->index);
673       }
674
675   phi = gen_rtx_PHI (VOIDmode, vec);
676   phi = gen_rtx_SET (VOIDmode, reg, phi);
677
678   insn = first_insn_after_basic_block_note (b);
679   end_p = PREV_INSN (insn) == b->end;
680   emit_insn_before (phi, insn);
681   if (end_p)
682     b->end = PREV_INSN (insn);
683 }
684
685 static void
686 insert_phi_nodes (idfs, evals, nregs)
687      sbitmap *idfs;
688      sbitmap *evals ATTRIBUTE_UNUSED;
689      int nregs;
690 {
691   int reg;
692
693   for (reg = 0; reg < nregs; ++reg)
694     if (CONVERT_REGISTER_TO_SSA_P (reg))
695     {
696       int b;
697       EXECUTE_IF_SET_IN_SBITMAP (idfs[reg], 0, b,
698         {
699           if (REGNO_REG_SET_P (BASIC_BLOCK (b)->global_live_at_start, reg))
700             insert_phi_node (reg, b);
701         });
702     }
703 }
704
705 /* Rename the registers to conform to SSA.
706
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.  */
710
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
714 {
715   struct rename_set_data *next;
716   /* This is the SET_DEST of the (first) SET that sets the REG.  */
717   rtx *reg_loc;
718   /* This is what used to be at *REG_LOC.  */
719   rtx old_reg;
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.  */
722   rtx new_reg;
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).  */
725   rtx prev_reg;
726   /* This is the insn that contains all the SETs of the REG.  */
727   rtx set_insn;
728 };
729
730 /* This struct is used to pass information to callback functions while
731    renaming registers.  */
732 struct rename_context
733 {
734   struct rename_set_data *new_renames;
735   struct rename_set_data *done_renames;
736   rtx current_insn;
737 };
738
739 /* Queue the rename of *REG_LOC.  */
740 static void
741 create_delayed_rename (c, reg_loc)
742      struct rename_context *c;
743      rtx *reg_loc;
744 {
745   struct rename_set_data *r;
746   r = (struct rename_set_data *) xmalloc (sizeof(*r));
747
748   if (GET_CODE (*reg_loc) != REG
749       || !CONVERT_REGISTER_TO_SSA_P (REGNO (*reg_loc)))
750     abort ();
751
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;
757   c->new_renames = r;
758 }
759
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
768
769 /* Move all the entries from NEW_RENAMES onto DONE_RENAMES by
770    applying all the renames on NEW_RENAMES.  */
771
772 static void
773 apply_delayed_renames (c)
774        struct rename_context *c;
775 {
776   struct rename_set_data *r;
777   struct rename_set_data *last_r = NULL;
778
779   for (r = c->new_renames; r != NULL; r = r->next)
780     {
781       int new_regno;
782
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)
786         abort ();
787       /* Failure here means we have changed REG_LOC before applying
788          the rename.  */
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))
791         {
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;
795         }
796       else
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);
800
801       if (new_regno >= (int) ssa_definition->num_elements)
802         {
803           int new_limit = new_regno * 5 / 4;
804           VARRAY_GROW (ssa_definition, new_limit);
805         }
806
807       VARRAY_RTX (ssa_definition, new_regno) = r->set_insn;
808       ssa_rename_from_insert (new_regno, r->old_reg);
809       last_r = r;
810     }
811   if (last_r != NULL)
812     {
813       last_r->next = c->done_renames;
814       c->done_renames = c->new_renames;
815       c->new_renames = NULL;
816     }
817 }
818
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.  */
821
822 static int
823 rename_insn_1 (ptr, data)
824      rtx *ptr;
825      void *data;
826 {
827   rtx x = *ptr;
828   struct rename_context *context = data;
829
830   if (x == NULL_RTX)
831     return 0;
832
833   switch (GET_CODE (x))
834     {
835     case SET:
836       {
837         rtx *destp = &SET_DEST (x);
838         rtx dest = SET_DEST (x);
839
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
844            this case.  */
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))))
850           {
851             destp = &XEXP (dest, 0);
852             dest = XEXP (dest, 0);
853           }
854
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)) ...)
860            into
861            (sequence [(set (reg foo_1) (reg foo))
862                       (set (subreg (reg foo_1)) ...)])
863
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).
867
868            For those cases we are better off not making the false
869            dependency.  */
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)
874           {
875             rtx i, reg;
876             reg = dest;
877
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)
882                 reg = XEXP (reg, 0);
883
884             if (GET_CODE (reg) == REG
885                 && CONVERT_REGISTER_TO_SSA_P (REGNO (reg)))
886               {
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.  */
890
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;
898               }
899           }
900         else if (GET_CODE (dest) == REG
901                  && CONVERT_REGISTER_TO_SSA_P (REGNO (dest)))
902           {
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.  */
906
907             create_delayed_rename (context, destp);
908
909             /* Since we do not wish to (directly) traverse the
910                SET_DEST, recurse through for_each_rtx for the SET_SRC
911                and return.  */
912             if (GET_CODE (x) == SET)
913               for_each_rtx (&SET_SRC (x), rename_insn_1, data);
914             return -1;
915           }
916
917         /* Otherwise, this was not an interesting destination.  Continue
918            on, marking uses as normal.  */
919         return 0;
920       }
921
922     case REG:
923       if (CONVERT_REGISTER_TO_SSA_P (REGNO (x))
924           && REGNO (x) < ssa_max_reg_num)
925         {
926           rtx new_reg = ssa_rename_to_lookup (x);
927
928           if (new_reg != RENAME_NO_RTX && new_reg != NULL_RTX)
929             {
930               if (GET_MODE (x) != GET_MODE (new_reg))
931                 abort ();
932               *ptr = new_reg;
933             }
934           else
935             {
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));
939             }
940         }
941       return -1;
942
943     case CLOBBER:
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
948          implemented yet.  */
949       {
950         rtx dest = XCEXP (x, 0, CLOBBER);
951         if (REG_P (dest))
952           {
953             if (CONVERT_REGISTER_TO_SSA_P (REGNO (dest))
954                 && REGNO (dest) < ssa_max_reg_num)
955               {
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;
959               }
960             /* Stop traversing.  */
961             return -1;
962           }
963         else
964           /* Continue traversing.  */
965           return 0;
966       }
967
968     case PHI:
969       /* Never muck with the phi.  We do that elsewhere, special-like.  */
970       return -1;
971
972     default:
973       /* Anything else, continue traversing.  */
974       return 0;
975     }
976 }
977
978 static rtx
979 gen_sequence ()
980 {
981   rtx first_insn = get_insns ();
982   rtx result;
983   rtx tem;
984   int i;
985   int len;
986
987   /* Count the insns in the chain.  */
988   len = 0;
989   for (tem = first_insn; tem; tem = NEXT_INSN (tem))
990     len++;
991
992   result = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (len));
993
994   for (i = 0, tem = first_insn; tem; tem = NEXT_INSN (tem), i++)
995     XVECEXP (result, 0, i) = tem;
996
997   return result;
998 }
999
1000 static void
1001 rename_block (bb, idom)
1002      int bb;
1003      dominance_info idom;
1004 {
1005   basic_block b = BASIC_BLOCK (bb);
1006   edge e;
1007   rtx insn, next, last;
1008   struct rename_set_data *set_data = NULL;
1009   basic_block c;
1010
1011   /* Step One: Walk the basic block, adding new names for sets and
1012      replacing uses.  */
1013
1014   next = b->head;
1015   last = b->end;
1016   do
1017     {
1018       insn = next;
1019       if (INSN_P (insn))
1020         {
1021           struct rename_context context;
1022           context.done_renames = set_data;
1023           context.new_renames = NULL;
1024           context.current_insn = insn;
1025
1026           start_sequence ();
1027           for_each_rtx (&PATTERN (insn), rename_insn_1, &context);
1028           for_each_rtx (&REG_NOTES (insn), rename_insn_1, &context);
1029
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)
1035             {
1036               rtx seq;
1037               int i;
1038
1039               emit (PATTERN (insn));
1040               seq = gen_sequence ();
1041               /* We really want a SEQUENCE of SETs, not a SEQUENCE
1042                  of INSNs.  */
1043               for (i = 0; i < XVECLEN (seq, 0); i++)
1044                 XVECEXP (seq, 0, i) = PATTERN (XVECEXP (seq, 0, i));
1045               PATTERN (insn) = seq;
1046             }
1047           end_sequence ();
1048
1049           apply_delayed_renames (&context);
1050           set_data = context.done_renames;
1051         }
1052
1053       next = NEXT_INSN (insn);
1054     }
1055   while (insn != last);
1056
1057   /* Step Two: Update the phi nodes of this block's successors.  */
1058
1059   for (e = b->succ; e; e = e->succ_next)
1060     {
1061       if (e->dest == EXIT_BLOCK_PTR)
1062         continue;
1063
1064       insn = first_insn_after_basic_block_note (e->dest);
1065
1066       while (PHI_NODE_P (insn))
1067         {
1068           rtx phi = PATTERN (insn);
1069           rtx reg;
1070
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)
1081             abort ();
1082           reg = ssa_rename_to_lookup (reg);
1083
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)
1088             {
1089               if (! remove_phi_alternative (phi, b))
1090                 abort ();
1091             }
1092           else
1093             {
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))
1100                 abort ();
1101
1102               *phi_alternative (phi, bb) = reg;
1103             }
1104
1105           insn = NEXT_INSN (insn);
1106         }
1107     }
1108
1109   /* Step Three: Do the same to the children of this block in
1110      dominator order.  */
1111
1112   FOR_EACH_BB (c)
1113     if (get_immediate_dominator (idom, c)->index == bb)
1114       rename_block (c->index, idom);
1115
1116   /* Step Four: Update the sets to refer to their new register,
1117      and restore ssa_rename_to to its previous state.  */
1118
1119   while (set_data)
1120     {
1121       struct rename_set_data *next;
1122       rtx old_reg = *set_data->reg_loc;
1123
1124       if (*set_data->reg_loc != set_data->old_reg)
1125         abort ();
1126       *set_data->reg_loc = set_data->new_reg;
1127
1128       ssa_rename_to_insert (old_reg, set_data->prev_reg);
1129
1130       next = set_data->next;
1131       free (set_data);
1132       set_data = next;
1133     }
1134 }
1135
1136 static void
1137 rename_registers (nregs, idom)
1138      int nregs;
1139      dominance_info idom;
1140 {
1141   VARRAY_RTX_INIT (ssa_definition, nregs * 3, "ssa_definition");
1142   ssa_rename_from_initialize ();
1143
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));
1148
1149   rename_block (0, idom);
1150
1151   /* ??? Update basic_block_live_at_start, and other flow info
1152      as needed.  */
1153
1154   ssa_rename_to_pseudo = NULL;
1155 }
1156
1157 /* The main entry point for moving to SSA.  */
1158
1159 void
1160 convert_to_ssa ()
1161 {
1162   /* Element I is the set of blocks that set register I.  */
1163   sbitmap *evals;
1164
1165   /* Dominator bitmaps.  */
1166   sbitmap *dfs;
1167   sbitmap *idfs;
1168
1169   /* Element I is the immediate dominator of block I.  */
1170   dominance_info idom;
1171
1172   int nregs;
1173
1174   basic_block bb;
1175
1176   /* Don't do it twice.  */
1177   if (in_ssa_form)
1178     abort ();
1179
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);
1183
1184   idom = calculate_dominance_info (CDI_DOMINATORS);
1185
1186   if (rtl_dump_file)
1187     {
1188       fputs (";; Immediate Dominators:\n", rtl_dump_file);
1189       FOR_EACH_BB (bb)
1190         fprintf (rtl_dump_file, ";\t%3d = %3d\n", bb->index,
1191                  get_immediate_dominator (idom, bb)->index);
1192       fflush (rtl_dump_file);
1193     }
1194
1195   /* Compute dominance frontiers.  */
1196
1197   dfs = sbitmap_vector_alloc (last_basic_block, last_basic_block);
1198   compute_dominance_frontiers (dfs, idom);
1199
1200   if (rtl_dump_file)
1201     {
1202       dump_sbitmap_vector (rtl_dump_file, ";; Dominance Frontiers:",
1203                            "; Basic Block", dfs, last_basic_block);
1204       fflush (rtl_dump_file);
1205     }
1206
1207   /* Compute register evaluations.  */
1208
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);
1213
1214   /* Compute the iterated dominance frontier for each register.  */
1215
1216   idfs = sbitmap_vector_alloc (nregs, last_basic_block);
1217   compute_iterated_dominance_frontiers (idfs, dfs, evals, nregs);
1218
1219   if (rtl_dump_file)
1220     {
1221       dump_sbitmap_vector (rtl_dump_file, ";; Iterated Dominance Frontiers:",
1222                            "; Register", idfs, nregs);
1223       fflush (rtl_dump_file);
1224     }
1225
1226   /* Insert the phi nodes.  */
1227
1228   insert_phi_nodes (idfs, evals, nregs);
1229
1230   /* Rename the registers to satisfy SSA.  */
1231
1232   rename_registers (nregs, idom);
1233
1234   /* All done!  Clean up and go home.  */
1235
1236   sbitmap_vector_free (dfs);
1237   sbitmap_vector_free (evals);
1238   sbitmap_vector_free (idfs);
1239   in_ssa_form = 1;
1240
1241   reg_scan (get_insns (), max_reg_num (), 1);
1242   free_dominance_info (idom);
1243 }
1244
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.  */
1248
1249 static inline int
1250 ephi_add_node (reg, nodes, n_nodes)
1251      rtx reg, *nodes;
1252      int *n_nodes;
1253 {
1254   int i;
1255   for (i = *n_nodes - 1; i >= 0; --i)
1256     if (REGNO (reg) == REGNO (nodes[i]))
1257       return i;
1258
1259   nodes[i = (*n_nodes)++] = reg;
1260   return i;
1261 }
1262
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.  */
1267
1268 static int *
1269 ephi_forward (t, visited, succ, tstack)
1270      int t;
1271      sbitmap visited;
1272      sbitmap *succ;
1273      int *tstack;
1274 {
1275   int s;
1276
1277   SET_BIT (visited, t);
1278
1279   EXECUTE_IF_SET_IN_SBITMAP (succ[t], 0, s,
1280     {
1281       if (! TEST_BIT (visited, s))
1282         tstack = ephi_forward (s, visited, succ, tstack);
1283     });
1284
1285   *tstack++ = t;
1286   return tstack;
1287 }
1288
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.  */
1291
1292 static void
1293 ephi_backward (t, visited, pred, nodes)
1294      int t;
1295      sbitmap visited, *pred;
1296      rtx *nodes;
1297 {
1298   int p;
1299
1300   SET_BIT (visited, t);
1301
1302   EXECUTE_IF_SET_IN_SBITMAP (pred[t], 0, p,
1303     {
1304       if (! TEST_BIT (visited, p))
1305         {
1306           ephi_backward (p, visited, pred, nodes);
1307           emit_move_insn (nodes[p], nodes[t]);
1308         }
1309     });
1310 }
1311
1312 /* Part two of the topological sort.  Create the copy for a register
1313    and any cycle of which it is a member.  */
1314
1315 static void
1316 ephi_create (t, visited, pred, succ, nodes)
1317      int t;
1318      sbitmap visited, *pred, *succ;
1319      rtx *nodes;
1320 {
1321   rtx reg_u = NULL_RTX;
1322   int unvisited_predecessors = 0;
1323   int p;
1324
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.  */
1329
1330   EXECUTE_IF_SET_IN_SBITMAP (pred[t], 0, p,
1331     {
1332       if (! TEST_BIT (visited, p))
1333         unvisited_predecessors = 1;
1334       else if (!reg_u)
1335         reg_u = nodes[p];
1336     });
1337
1338   if (unvisited_predecessors)
1339     {
1340       /* We found a cycle.  Copy out one element of the ring (if necessary),
1341          then traverse the ring copying as we go.  */
1342
1343       if (!reg_u)
1344         {
1345           reg_u = gen_reg_rtx (GET_MODE (nodes[t]));
1346           emit_move_insn (reg_u, nodes[t]);
1347         }
1348
1349       EXECUTE_IF_SET_IN_SBITMAP (pred[t], 0, p,
1350         {
1351           if (! TEST_BIT (visited, p))
1352             {
1353               ephi_backward (p, visited, pred, nodes);
1354               emit_move_insn (nodes[p], reg_u);
1355             }
1356         });
1357     }
1358   else
1359     {
1360       /* No cycle.  Just copy the value from a successor.  */
1361
1362       int s;
1363       EXECUTE_IF_SET_IN_SBITMAP (succ[t], 0, s,
1364         {
1365           SET_BIT (visited, t);
1366           emit_move_insn (nodes[t], nodes[s]);
1367           return;
1368         });
1369     }
1370 }
1371
1372 /* Convert the edge to normal form.  */
1373
1374 static void
1375 eliminate_phi (e, reg_partition)
1376      edge e;
1377      partition reg_partition;
1378 {
1379   int n_nodes;
1380   sbitmap *pred, *succ;
1381   sbitmap visited;
1382   rtx *nodes;
1383   int *stack, *tstack;
1384   rtx insn;
1385   int i;
1386
1387   /* Collect an upper bound on the number of registers needing processing.  */
1388
1389   insn = first_insn_after_basic_block_note (e->dest);
1390
1391   n_nodes = 0;
1392   while (PHI_NODE_P (insn))
1393     {
1394       insn = next_nonnote_insn (insn);
1395       n_nodes += 2;
1396     }
1397
1398   if (n_nodes == 0)
1399     return;
1400
1401   /* Build the auxiliary graph R(B).
1402
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.  */
1406
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);
1412
1413   insn = first_insn_after_basic_block_note (e->dest);
1414
1415   n_nodes = 0;
1416   for (; PHI_NODE_P (insn); insn = next_nonnote_insn (insn))
1417     {
1418       rtx* preg = phi_alternative (PATTERN (insn), e->src->index);
1419       rtx tgt = SET_DEST (PATTERN (insn));
1420       rtx reg;
1421
1422       /* There may be no phi alternative corresponding to this edge.
1423          This indicates that the phi variable is undefined along this
1424          edge.  */
1425       if (preg == NULL)
1426         continue;
1427       reg = *preg;
1428
1429       if (GET_CODE (reg) != REG || GET_CODE (tgt) != REG)
1430         abort ();
1431
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.  */
1436       if (reg != tgt)
1437         {
1438           int ireg, itgt;
1439
1440           ireg = ephi_add_node (reg, nodes, &n_nodes);
1441           itgt = ephi_add_node (tgt, nodes, &n_nodes);
1442
1443           SET_BIT (pred[ireg], itgt);
1444           SET_BIT (succ[itgt], ireg);
1445         }
1446     }
1447
1448   if (n_nodes == 0)
1449     goto out;
1450
1451   /* Begin a topological sort of the graph.  */
1452
1453   visited = sbitmap_alloc (n_nodes);
1454   sbitmap_zero (visited);
1455
1456   tstack = stack = (int *) alloca (n_nodes * sizeof (int));
1457
1458   for (i = 0; i < n_nodes; ++i)
1459     if (! TEST_BIT (visited, i))
1460       tstack = ephi_forward (i, visited, succ, tstack);
1461
1462   sbitmap_zero (visited);
1463
1464   /* As we find a solution to the tsort, collect the implementation
1465      insns in a sequence.  */
1466   start_sequence ();
1467
1468   while (tstack != stack)
1469     {
1470       i = *--tstack;
1471       if (! TEST_BIT (visited, i))
1472         ephi_create (i, visited, pred, succ, nodes);
1473     }
1474
1475   insn = get_insns ();
1476   end_sequence ();
1477   insert_insn_on_edge (insn, e);
1478   if (rtl_dump_file)
1479     fprintf (rtl_dump_file, "Emitting copy on edge (%d,%d)\n",
1480              e->src->index, e->dest->index);
1481
1482   sbitmap_free (visited);
1483 out:
1484   sbitmap_vector_free (pred);
1485   sbitmap_vector_free (succ);
1486 }
1487
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.
1492
1493    From Morgan, p. 178:
1494
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.
1499
1500    Return nonzero iff any such cases were found for which the two
1501    regs were not already in the same class.  */
1502
1503 static int
1504 make_regs_equivalent_over_bad_edges (bb, reg_partition)
1505      int bb;
1506      partition reg_partition;
1507 {
1508   int changed = 0;
1509   basic_block b = BASIC_BLOCK (bb);
1510   rtx phi;
1511
1512   /* Advance to the first phi node.  */
1513   phi = first_insn_after_basic_block_note (b);
1514
1515   /* Scan all the phi nodes.  */
1516   for (;
1517        PHI_NODE_P (phi);
1518        phi = next_nonnote_insn (phi))
1519     {
1520       edge e;
1521       int tgt_regno;
1522       rtx set = PATTERN (phi);
1523       rtx tgt = SET_DEST (set);
1524
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)))
1528         abort ();
1529       tgt_regno = REGNO (tgt);
1530
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))
1534           {
1535             rtx *alt = phi_alternative (set, e->src->index);
1536             int alt_regno;
1537
1538             /* If there is no alternative corresponding to this edge,
1539                the value is undefined along the edge, so just go on.  */
1540             if (alt == 0)
1541               continue;
1542
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)))
1546               abort ();
1547             alt_regno = REGNO (*alt);
1548
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))
1553               {
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.  */
1558                   abort ();
1559
1560                 partition_union (reg_partition,
1561                                  tgt_regno, alt_regno);
1562                 ++changed;
1563               }
1564           }
1565     }
1566
1567   return changed;
1568 }
1569
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.
1573
1574    Return nonzero if any new register classes were unioned.  */
1575
1576 static int
1577 make_equivalent_phi_alternatives_equivalent (bb, reg_partition)
1578      int bb;
1579      partition reg_partition;
1580 {
1581   int changed = 0;
1582   basic_block b = BASIC_BLOCK (bb);
1583   rtx phi;
1584
1585   /* Advance to the first phi node.  */
1586   phi = first_insn_after_basic_block_note (b);
1587
1588   /* Scan all the phi nodes.  */
1589   for (;
1590        PHI_NODE_P (phi);
1591        phi = next_nonnote_insn (phi))
1592     {
1593       rtx set = PATTERN (phi);
1594       /* The regno of the destination of the set.  */
1595       int tgt_regno = REGNO (SET_DEST (PATTERN (phi)));
1596
1597       rtx phi2 = next_nonnote_insn (phi);
1598
1599       /* Scan all phi nodes following this one.  */
1600       for (;
1601            PHI_NODE_P (phi2);
1602            phi2 = next_nonnote_insn (phi2))
1603         {
1604           rtx set2 = PATTERN (phi2);
1605           /* The regno of the destination of the set.  */
1606           int tgt2_regno = REGNO (SET_DEST (set2));
1607
1608           /* Are the set destinations equivalent regs?  */
1609           if (partition_find (reg_partition, tgt_regno) ==
1610               partition_find (reg_partition, tgt2_regno))
1611             {
1612               edge e;
1613               /* Scan over edges.  */
1614               for (e = b->pred; e; e = e->pred_next)
1615                 {
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);
1621
1622                   /* If one of the phi nodes doesn't have a
1623                      corresponding alternative, just skip it.  */
1624                   if (alt == 0 || alt2 == 0)
1625                     continue;
1626
1627                   /* Both alternatives should be SSA registers.  */
1628                   if (GET_CODE (*alt) != REG
1629                       || !CONVERT_REGISTER_TO_SSA_P (REGNO (*alt)))
1630                     abort ();
1631                   if (GET_CODE (*alt2) != REG
1632                       || !CONVERT_REGISTER_TO_SSA_P (REGNO (*alt2)))
1633                     abort ();
1634
1635                   /* If the alternatives aren't already in the same
1636                      class ...  */
1637                   if (partition_find (reg_partition, REGNO (*alt))
1638                       != partition_find (reg_partition, REGNO (*alt2)))
1639                     {
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.  */
1644                         abort ();
1645
1646                       partition_union (reg_partition,
1647                                        REGNO (*alt), REGNO (*alt2));
1648                       ++changed;
1649                     }
1650                 }
1651             }
1652         }
1653     }
1654
1655   return changed;
1656 }
1657
1658 /* Compute a conservative partition of outstanding pseudo registers.
1659    See Morgan 7.3.1.  */
1660
1661 static partition
1662 compute_conservative_reg_partition ()
1663 {
1664   basic_block bb;
1665   int changed = 0;
1666
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.  */
1670   partition p =
1671     partition_new (ssa_definition->num_elements);
1672
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
1676      edges.  */
1677   FOR_EACH_BB_REVERSE (bb)
1678     changed += make_regs_equivalent_over_bad_edges (bb->index, p);
1679
1680   /* Now we have to insure that corresponding arguments of phi nodes
1681      assigning to corresponding regs are equivalent.  Iterate until
1682      nothing changes.  */
1683   while (changed > 0)
1684     {
1685       changed = 0;
1686       FOR_EACH_BB_REVERSE (bb)
1687         changed += make_equivalent_phi_alternatives_equivalent (bb->index, p);
1688     }
1689
1690   return p;
1691 }
1692
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:
1696
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).
1700
1701     2. Figure out which regs are involved (in the LHS or RHS) of
1702        copies and phi nodes.  Compute conflicts among these regs.
1703
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
1708        updated.
1709
1710     4. If anything has changed, there may be new opportunities to
1711        coalesce regs, so go back to 2.
1712 */
1713
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.
1717
1718    Returns one if REG1 and REG2 were placed in the same class but were
1719    not previously; zero otherwise.
1720
1721    See Morgan figure 11.15.  */
1722
1723 static int
1724 coalesce_if_unconflicting (p, conflicts, reg1, reg2)
1725      partition p;
1726      conflict_graph conflicts;
1727      int reg1;
1728      int reg2;
1729 {
1730   int reg;
1731
1732   /* Work only on SSA registers.  */
1733   if (!CONVERT_REGISTER_TO_SSA_P (reg1) || !CONVERT_REGISTER_TO_SSA_P (reg2))
1734     return 0;
1735
1736   /* Find the canonical regs for the classes containing REG1 and
1737      REG2.  */
1738   reg1 = partition_find (p, reg1);
1739   reg2 = partition_find (p, reg2);
1740
1741   /* If they're already in the same class, there's nothing to do.  */
1742   if (reg1 == reg2)
1743     return 0;
1744
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))
1748     return 0;
1749
1750   /* We're good to go.  Put the regs in the same partition.  */
1751   partition_union (p, reg1, reg2);
1752
1753   /* Find the new canonical reg for the merged class.  */
1754   reg = partition_find (p, reg1);
1755
1756   /* Merge conflicts from the two previous classes.  */
1757   conflict_graph_merge_regs (conflicts, reg, reg1);
1758   conflict_graph_merge_regs (conflicts, reg, reg2);
1759
1760   return 1;
1761 }
1762
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.
1766
1767    Returns the number of changes that were made to P.
1768
1769    See Morgan figure 11.14.  */
1770
1771 static int
1772 coalesce_regs_in_copies (bb, p, conflicts)
1773      basic_block bb;
1774      partition p;
1775      conflict_graph conflicts;
1776 {
1777   int changed = 0;
1778   rtx insn;
1779   rtx end = bb->end;
1780
1781   /* Scan the instruction stream of the block.  */
1782   for (insn = bb->head; insn != end; insn = NEXT_INSN (insn))
1783     {
1784       rtx pattern;
1785       rtx src;
1786       rtx dest;
1787
1788       /* If this isn't a set insn, go to the next insn.  */
1789       if (GET_CODE (insn) != INSN)
1790         continue;
1791       pattern = PATTERN (insn);
1792       if (GET_CODE (pattern) != SET)
1793         continue;
1794
1795       src = SET_SRC (pattern);
1796       dest = SET_DEST (pattern);
1797
1798       /* We're only looking for copies.  */
1799       if (GET_CODE (src) != REG || GET_CODE (dest) != REG)
1800         continue;
1801
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.
1805
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))
1809         continue;
1810
1811       /* Found a copy; see if we can use the same reg for both the
1812          source and destination (and thus eliminate the copy,
1813          ultimately).  */
1814       changed += coalesce_if_unconflicting (p, conflicts,
1815                                             REGNO (src), REGNO (dest));
1816     }
1817
1818   return changed;
1819 }
1820
1821 struct phi_coalesce_context
1822 {
1823   partition p;
1824   conflict_graph conflicts;
1825   int changed;
1826 };
1827
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.  */
1832
1833 static int
1834 coalesce_reg_in_phi (insn, dest_regno, src_regno, data)
1835      rtx insn ATTRIBUTE_UNUSED;
1836      int dest_regno;
1837      int src_regno;
1838      void *data;
1839 {
1840   struct phi_coalesce_context *context =
1841     (struct phi_coalesce_context *) data;
1842
1843   /* Attempt to use the same reg, if they don't conflict.  */
1844   context->changed
1845     += coalesce_if_unconflicting (context->p, context->conflicts,
1846                                   dest_regno, src_regno);
1847   return 0;
1848 }
1849
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.
1854
1855    Return the number of changes that were made to P.
1856
1857    See Morgan figure 11.14.  */
1858
1859 static int
1860 coalesce_regs_in_successor_phi_nodes (bb, p, conflicts)
1861      basic_block bb;
1862      partition p;
1863      conflict_graph conflicts;
1864 {
1865   struct phi_coalesce_context context;
1866   context.p = p;
1867   context.conflicts = conflicts;
1868   context.changed = 0;
1869
1870   for_each_successor_phi (bb, &coalesce_reg_in_phi, &context);
1871
1872   return context.changed;
1873 }
1874
1875 /* Compute and return a partition of pseudos.  Where possible,
1876    non-conflicting pseudos are placed in the same class.
1877
1878    The caller is responsible for deallocating the returned partition.  */
1879
1880 static partition
1881 compute_coalesced_reg_partition ()
1882 {
1883   basic_block bb;
1884   int changed = 0;
1885   regset_head phi_set_head;
1886   regset phi_set = &phi_set_head;
1887
1888   partition p =
1889     partition_new (ssa_definition->num_elements);
1890
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);
1897
1898   INIT_REG_SET (phi_set);
1899
1900   do
1901     {
1902       conflict_graph conflicts;
1903
1904       changed = 0;
1905
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);
1910
1911       /* Compute conflicts.  */
1912       conflicts = conflict_graph_compute (phi_set, p);
1913
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)
1919         {
1920           changed += coalesce_regs_in_copies (bb, p, conflicts);
1921           changed +=
1922             coalesce_regs_in_successor_phi_nodes (bb, p, conflicts);
1923         }
1924
1925       conflict_graph_delete (conflicts);
1926     }
1927   while (changed > 0);
1928
1929   FREE_REG_SET (phi_set);
1930
1931   return p;
1932 }
1933
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.  */
1937
1938 static int
1939 mark_reg_in_phi (ptr, data)
1940      rtx *ptr;
1941      void *data;
1942 {
1943   rtx expr = *ptr;
1944   regset set = (regset) data;
1945
1946   switch (GET_CODE (expr))
1947     {
1948     case REG:
1949       SET_REGNO_REG_SET (set, REGNO (expr));
1950       /* Fall through.  */
1951     case CONST_INT:
1952     case PHI:
1953       return 0;
1954     default:
1955       abort ();
1956     }
1957 }
1958
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
1962    ssa_definition.  */
1963
1964 static void
1965 mark_phi_and_copy_regs (phi_set)
1966      regset phi_set;
1967 {
1968   unsigned int reg;
1969
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))
1973       {
1974         rtx insn = VARRAY_RTX (ssa_definition, reg);
1975         rtx pattern;
1976         rtx src;
1977
1978         if (insn == NULL
1979             || (GET_CODE (insn) == NOTE
1980                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED))
1981           continue;
1982         pattern = PATTERN (insn);
1983         /* Sometimes we get PARALLEL insns.  These aren't phi nodes or
1984            copies.  */
1985         if (GET_CODE (pattern) != SET)
1986           continue;
1987         src = SET_SRC (pattern);
1988
1989         if (GET_CODE (src) == REG)
1990           {
1991             /* It's a reg copy.  */
1992             SET_REGNO_REG_SET (phi_set, reg);
1993             SET_REGNO_REG_SET (phi_set, REGNO (src));
1994           }
1995         else if (GET_CODE (src) == PHI)
1996           {
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);
2001           }
2002         /* ... else nothing to do.  */
2003       }
2004 }
2005
2006 /* Rename regs in insn PTR that are equivalent.  DATA is the register
2007    partition which specifies equivalences.  */
2008
2009 static int
2010 rename_equivalent_regs_in_insn (ptr, data)
2011      rtx *ptr;
2012      void* data;
2013 {
2014   rtx x = *ptr;
2015   partition reg_partition = (partition) data;
2016
2017   if (x == NULL_RTX)
2018     return 0;
2019
2020   switch (GET_CODE (x))
2021     {
2022     case REG:
2023       if (CONVERT_REGISTER_TO_SSA_P (REGNO (x)))
2024         {
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);
2028
2029           if (canonical_element_rtx != NULL_RTX &&
2030               HARD_REGISTER_P (canonical_element_rtx))
2031             {
2032               if (REGNO (canonical_element_rtx) != regno)
2033                 *ptr = canonical_element_rtx;
2034             }
2035           else if (regno != new_regno)
2036             {
2037               rtx new_reg = regno_reg_rtx[new_regno];
2038               if (GET_MODE (x) != GET_MODE (new_reg))
2039                 abort ();
2040               *ptr = new_reg;
2041             }
2042         }
2043       return -1;
2044
2045     case PHI:
2046       /* No need to rename the phi nodes.  We'll check equivalence
2047          when inserting copies.  */
2048       return -1;
2049
2050     default:
2051       /* Anything else, continue traversing.  */
2052       return 0;
2053     }
2054 }
2055
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.  */
2059
2060 static int
2061 record_canonical_element_1 (srfp, data)
2062      void **srfp;
2063      void *data;
2064 {
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;
2070
2071   SET_BIT (canonical_elements, partition_find (reg_partition, reg));
2072   return 1;
2073 }
2074
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.  */
2079
2080 static int
2081 check_hard_regs_in_partition (reg_partition)
2082      partition reg_partition;
2083 {
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;
2088   int element_index;
2089   int already_seen[FIRST_PSEUDO_REGISTER][NUM_MACHINE_MODES];
2090   int reg;
2091   int mach_mode;
2092
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);
2098
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;
2103
2104   /* Check for classes with the same hard register and machine mode.  */
2105   EXECUTE_IF_SET_IN_SBITMAP (canonical_elements, 0, element_index,
2106   {
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
2112              hard register.  */
2113           return 0;
2114   });
2115
2116   sbitmap_free (canonical_elements);
2117
2118   return 1;
2119 }
2120
2121 /* Rename regs that are equivalent in REG_PARTITION.  Also collapse
2122    any SEQUENCE insns.  */
2123
2124 static void
2125 rename_equivalent_regs (reg_partition)
2126      partition reg_partition;
2127 {
2128   basic_block b;
2129
2130   FOR_EACH_BB_REVERSE (b)
2131     {
2132       rtx next = b->head;
2133       rtx last = b->end;
2134       rtx insn;
2135
2136       do
2137         {
2138           insn = next;
2139           if (INSN_P (insn))
2140             {
2141               for_each_rtx (&PATTERN (insn),
2142                             rename_equivalent_regs_in_insn,
2143                             reg_partition);
2144               for_each_rtx (&REG_NOTES (insn),
2145                             rename_equivalent_regs_in_insn,
2146                             reg_partition);
2147
2148               if (GET_CODE (PATTERN (insn)) == SEQUENCE)
2149                 {
2150                   rtx s = PATTERN (insn);
2151                   int slen = XVECLEN (s, 0);
2152                   int i;
2153
2154                   if (slen <= 1)
2155                     abort ();
2156
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);
2160                 }
2161             }
2162
2163           next = NEXT_INSN (insn);
2164         }
2165       while (insn != last);
2166     }
2167 }
2168
2169 /* The main entry point for moving from SSA.  */
2170
2171 void
2172 convert_from_ssa ()
2173 {
2174   basic_block b, bb;
2175   partition reg_partition;
2176   rtx insns = get_insns ();
2177
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.
2181
2182      Register coalescing needs death notes, so generate them.  */
2183   life_analysis (insns, NULL, PROP_DEATH_NOTES);
2184
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 ();
2189   else
2190     reg_partition = compute_coalesced_reg_partition ();
2191
2192   if (!check_hard_regs_in_partition (reg_partition))
2193     /* Two separate partitions should correspond to the same hard
2194        register but do not.  */
2195     abort ();
2196
2197   rename_equivalent_regs (reg_partition);
2198
2199   /* Eliminate the PHI nodes.  */
2200   FOR_EACH_BB_REVERSE (b)
2201     {
2202       edge e;
2203
2204       for (e = b->pred; e; e = e->pred_next)
2205         if (e->src != ENTRY_BLOCK_PTR)
2206           eliminate_phi (e, reg_partition);
2207     }
2208
2209   partition_delete (reg_partition);
2210
2211   /* Actually delete the PHI nodes.  */
2212   FOR_EACH_BB_REVERSE (bb)
2213     {
2214       rtx insn = bb->head;
2215
2216       while (1)
2217         {
2218           /* If this is a PHI node delete it.  */
2219           if (PHI_NODE_P (insn))
2220             {
2221               if (insn == bb->end)
2222                 bb->end = PREV_INSN (insn);
2223               insn = delete_insn (insn);
2224             }
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))
2229             break;
2230           /* If we've reached the end of the block, stop.  */
2231           else if (insn == bb->end)
2232             break;
2233           else
2234             insn = NEXT_INSN (insn);
2235         }
2236     }
2237
2238   /* Commit all the copy nodes needed to convert out of SSA form.  */
2239   commit_edge_insertions ();
2240
2241   in_ssa_form = 0;
2242
2243   count_or_remove_death_notes (NULL, 1);
2244
2245   /* Deallocate the data structures.  */
2246   ssa_definition = 0;
2247   ssa_rename_from_free ();
2248 }
2249
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,
2254    and DATA.
2255
2256    If FN ever returns nonzero, stops immediately and returns this
2257    value.  Otherwise, returns zero.  */
2258
2259 int
2260 for_each_successor_phi (bb, fn, data)
2261      basic_block bb;
2262      successor_phi_fn fn;
2263      void *data;
2264 {
2265   edge e;
2266
2267   if (bb == EXIT_BLOCK_PTR)
2268     return 0;
2269
2270   /* Scan outgoing edges.  */
2271   for (e = bb->succ; e != NULL; e = e->succ_next)
2272     {
2273       rtx insn;
2274
2275       basic_block successor = e->dest;
2276       if (successor == ENTRY_BLOCK_PTR
2277           || successor == EXIT_BLOCK_PTR)
2278         continue;
2279
2280       /* Advance to the first non-label insn of the successor block.  */
2281       insn = first_insn_after_basic_block_note (successor);
2282
2283       if (insn == NULL)
2284         continue;
2285
2286       /* Scan phi nodes in the successor.  */
2287       for ( ; PHI_NODE_P (insn); insn = NEXT_INSN (insn))
2288         {
2289           int result;
2290           rtx phi_set = PATTERN (insn);
2291           rtx *alternative = phi_alternative (phi_set, bb->index);
2292           rtx phi_src;
2293
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)
2298             continue;
2299           phi_src = *alternative;
2300
2301           /* Invoke the callback.  */
2302           result = (*fn) (insn, REGNO (SET_DEST (phi_set)),
2303                           REGNO (phi_src), data);
2304
2305           /* Terminate if requested.  */
2306           if (result != 0)
2307             return result;
2308         }
2309     }
2310
2311   return 0;
2312 }
2313
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.  */
2318
2319 static int
2320 conflicting_hard_regs_p (reg1, reg2)
2321      int reg1;
2322      int reg2;
2323 {
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)
2328     return 1;
2329   if (HARD_REGISTER_NUM_P (orig_reg1) && !HARD_REGISTER_NUM_P (orig_reg2))
2330     return 1;
2331   if (!HARD_REGISTER_NUM_P (orig_reg1) && HARD_REGISTER_NUM_P (orig_reg2))
2332     return 1;
2333
2334   return 0;
2335 }