OSDN Git Service

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