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