Revert the optimization in r122596. It is correct for all current targets, but
[oota-llvm.git] / lib / CodeGen / StrongPHIElimination.cpp
1 //===- StrongPHIElimination.cpp - Eliminate PHI nodes by inserting copies -===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass eliminates PHI instructions by aggressively coalescing the copies
11 // that would be inserted by a naive algorithm and only inserting the copies
12 // that are necessary. The coalescing technique initially assumes that all
13 // registers appearing in a PHI instruction do not interfere. It then eliminates
14 // proven interferences, using dominators to only perform a linear number of
15 // interference tests instead of the quadratic number of interference tests
16 // that this would naively require. This is a technique derived from:
17 // 
18 //    Budimlic, et al. Fast copy coalescing and live-range identification.
19 //    In Proceedings of the ACM SIGPLAN 2002 Conference on Programming Language
20 //    Design and Implementation (Berlin, Germany, June 17 - 19, 2002).
21 //    PLDI '02. ACM, New York, NY, 25-32.
22 //
23 // The original implementation constructs a data structure they call a dominance
24 // forest for this purpose. The dominance forest was shown to be unnecessary,
25 // as it is possible to emulate the creation and traversal of a dominance forest
26 // by directly using the dominator tree, rather than actually constructing the
27 // dominance forest.  This technique is explained in:
28 //
29 //   Boissinot, et al. Revisiting Out-of-SSA Translation for Correctness, Code
30 //     Quality and Efficiency,
31 //   In Proceedings of the 7th annual IEEE/ACM International Symposium on Code
32 //   Generation and Optimization (Seattle, Washington, March 22 - 25, 2009).
33 //   CGO '09. IEEE, Washington, DC, 114-125.
34 //
35 // Careful implementation allows for all of the dominator forest interference
36 // checks to be performed at once in a single depth-first traversal of the
37 // dominator tree, which is what is implemented here.
38 //
39 //===----------------------------------------------------------------------===//
40
41 #define DEBUG_TYPE "strongphielim"
42 #include "PHIEliminationUtils.h"
43 #include "llvm/CodeGen/Passes.h"
44 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
45 #include "llvm/CodeGen/MachineDominators.h"
46 #include "llvm/CodeGen/MachineFunctionPass.h"
47 #include "llvm/CodeGen/MachineInstrBuilder.h"
48 #include "llvm/CodeGen/MachineRegisterInfo.h"
49 #include "llvm/Target/TargetInstrInfo.h"
50 #include "llvm/Support/Debug.h"
51 using namespace llvm;
52
53 namespace {
54   class StrongPHIElimination : public MachineFunctionPass {
55   public:
56     static char ID; // Pass identification, replacement for typeid
57     StrongPHIElimination() : MachineFunctionPass(ID) {
58       initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
59     }
60
61     virtual void getAnalysisUsage(AnalysisUsage&) const;
62     bool runOnMachineFunction(MachineFunction&);
63
64   private:
65     /// This struct represents a single node in the union-find data structure
66     /// representing the variable congruence classes. There is one difference
67     /// from a normal union-find data structure. We steal two bits from the parent
68     /// pointer . One of these bits is used to represent whether the register
69     /// itself has been isolated, and the other is used to represent whether the
70     /// PHI with that register as its destination has been isolated.
71     ///
72     /// Note that this leads to the strange situation where the leader of a
73     /// congruence class may no longer logically be a member, due to being
74     /// isolated.
75     struct Node {
76       enum Flags {
77         kRegisterIsolatedFlag = 1,
78         kPHIIsolatedFlag = 2
79       };
80       Node(unsigned v) : value(v), rank(0) { parent.setPointer(this); }
81
82       Node* getLeader();
83
84       PointerIntPair<Node*, 2> parent;
85       unsigned value;
86       unsigned rank;
87     };
88
89     /// Add a register in a new congruence class containing only itself.
90     void addReg(unsigned);
91
92     /// Join the congruence classes of two registers.
93     void unionRegs(unsigned, unsigned);
94
95     /// Get the color of a register. The color is 0 if the register has been
96     /// isolated.
97     unsigned getRegColor(unsigned);
98
99     // Isolate a register.
100     void isolateReg(unsigned);
101
102     /// Get the color of a PHI. The color of a PHI is 0 if the PHI has been
103     /// isolated. Otherwise, it is the original color of its destination and
104     /// all of its operands (before they were isolated, if they were).
105     unsigned getPHIColor(MachineInstr*);
106
107     /// Isolate a PHI.
108     void isolatePHI(MachineInstr*);
109
110     void PartitionRegisters(MachineFunction& MF);
111
112     /// Traverses a basic block, splitting any interferences found between
113     /// registers in the same congruence class. It takes two DenseMaps as
114     /// arguments that it also updates: CurrentDominatingParent, which maps
115     /// a color to the register in that congruence class whose definition was
116     /// most recently seen, and ImmediateDominatingParent, which maps a register
117     /// to the register in the same congruence class that most immediately
118     /// dominates it.
119     ///
120     /// This function assumes that it is being called in a depth-first traversal
121     /// of the dominator tree.
122     void SplitInterferencesForBasicBlock(
123       MachineBasicBlock&,
124       DenseMap<unsigned, unsigned>& CurrentDominatingParent,
125       DenseMap<unsigned, unsigned>& ImmediateDominatingParent);
126
127     // Lowers a PHI instruction, inserting copies of the source and destination
128     // registers as necessary.
129     void InsertCopiesForPHI(MachineInstr*, MachineBasicBlock*);
130
131     // Merges the live interval of Reg into NewReg and renames Reg to NewReg
132     // everywhere that Reg appears. Requires Reg and NewReg to have non-
133     // overlapping lifetimes.
134     void MergeLIsAndRename(unsigned Reg, unsigned NewReg);
135
136     MachineRegisterInfo* MRI;
137     const TargetInstrInfo* TII;
138     MachineDominatorTree* DT;
139     LiveIntervals* LI;
140
141     BumpPtrAllocator Allocator;
142
143     DenseMap<unsigned, Node*> RegNodeMap;
144
145     // FIXME: Can these two data structures be combined? Would a std::multimap
146     // be any better?
147
148     // Stores pairs of predecessor basic blocks and the source registers of
149     // inserted copy instructions.
150     typedef DenseSet<std::pair<MachineBasicBlock*, unsigned> > SrcCopySet;
151     SrcCopySet InsertedSrcCopySet;
152
153     // Maps pairs of predecessor basic blocks and colors to their defining copy
154     // instructions.
155     typedef DenseMap<std::pair<MachineBasicBlock*, unsigned>, MachineInstr*>
156       SrcCopyMap;
157     SrcCopyMap InsertedSrcCopyMap;
158
159     // Maps inserted destination copy registers to their defining copy
160     // instructions.
161     typedef DenseMap<unsigned, MachineInstr*> DestCopyMap;
162     DestCopyMap InsertedDestCopies;
163   };
164 } // namespace
165
166 char StrongPHIElimination::ID = 0;
167 INITIALIZE_PASS_BEGIN(StrongPHIElimination, "strong-phi-node-elimination",
168   "Eliminate PHI nodes for register allocation, intelligently", false, false)
169 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
170 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
171 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
172 INITIALIZE_PASS_END(StrongPHIElimination, "strong-phi-node-elimination",
173   "Eliminate PHI nodes for register allocation, intelligently", false, false)
174
175 char &llvm::StrongPHIEliminationID = StrongPHIElimination::ID;
176
177 void StrongPHIElimination::getAnalysisUsage(AnalysisUsage& AU) const {
178   AU.setPreservesCFG();
179   AU.addRequired<MachineDominatorTree>();
180   AU.addRequired<SlotIndexes>();
181   AU.addPreserved<SlotIndexes>();
182   AU.addRequired<LiveIntervals>();
183   AU.addPreserved<LiveIntervals>();
184   MachineFunctionPass::getAnalysisUsage(AU);
185 }
186
187 static MachineOperand* findLastUse(MachineBasicBlock* MBB, unsigned Reg) {
188   // FIXME: This only needs to check from the first terminator, as only the
189   // first terminator can use a virtual register.
190   for (MachineBasicBlock::reverse_iterator RI = MBB->rbegin(); ; ++RI) {
191     assert (RI != MBB->rend());
192     MachineInstr* MI = &*RI;
193
194     for (MachineInstr::mop_iterator OI = MI->operands_begin(),
195          OE = MI->operands_end(); OI != OE; ++OI) {
196       MachineOperand& MO = *OI;
197       if (MO.isReg() && MO.isUse() && MO.getReg() == Reg)
198         return &MO;
199     }
200   }
201   return NULL;
202 }
203
204 bool StrongPHIElimination::runOnMachineFunction(MachineFunction& MF) {
205   MRI = &MF.getRegInfo();
206   TII = MF.getTarget().getInstrInfo();
207   DT = &getAnalysis<MachineDominatorTree>();
208   LI = &getAnalysis<LiveIntervals>();
209
210   PartitionRegisters(MF);
211
212   // Perform a depth-first traversal of the dominator tree, splitting
213   // interferences amongst PHI-congruence classes.
214   DenseMap<unsigned, unsigned> CurrentDominatingParent;
215   DenseMap<unsigned, unsigned> ImmediateDominatingParent;
216   for (df_iterator<MachineDomTreeNode*> DI = df_begin(DT->getRootNode()),
217        DE = df_end(DT->getRootNode()); DI != DE; ++DI) {
218     SplitInterferencesForBasicBlock(*DI->getBlock(),
219                                     CurrentDominatingParent,
220                                     ImmediateDominatingParent);
221   }
222
223   // Insert copies for all PHI source and destination registers.
224   for (MachineFunction::iterator I = MF.begin(), E = MF.end();
225        I != E; ++I) {
226     for (MachineBasicBlock::iterator BBI = I->begin(), BBE = I->end();
227          BBI != BBE && BBI->isPHI(); ++BBI) {
228       InsertCopiesForPHI(BBI, I);
229     }
230   }
231
232   // FIXME: Preserve the equivalence classes during copy insertion and use
233   // the preversed equivalence classes instead of recomputing them.
234   RegNodeMap.clear();
235   PartitionRegisters(MF);
236
237   DenseMap<unsigned, unsigned> RegRenamingMap;
238   bool Changed = false;
239   for (MachineFunction::iterator I = MF.begin(), E = MF.end();
240        I != E; ++I) {
241     MachineBasicBlock::iterator BBI = I->begin(), BBE = I->end();
242     while (BBI != BBE && BBI->isPHI()) {
243       MachineInstr* PHI = BBI;
244
245       assert(PHI->getNumOperands() > 0);
246
247       unsigned SrcReg = PHI->getOperand(1).getReg();
248       unsigned SrcColor = getRegColor(SrcReg);
249       unsigned NewReg = RegRenamingMap[SrcColor];
250       if (!NewReg) {
251         NewReg = SrcReg;
252         RegRenamingMap[SrcColor] = SrcReg;
253       }
254       MergeLIsAndRename(SrcReg, NewReg);
255
256       unsigned DestReg = PHI->getOperand(0).getReg();
257       if (!InsertedDestCopies.count(DestReg))
258         MergeLIsAndRename(DestReg, NewReg);
259
260       for (unsigned i = 3; i < PHI->getNumOperands(); i += 2) {
261         unsigned SrcReg = PHI->getOperand(i).getReg();
262         MergeLIsAndRename(SrcReg, NewReg);
263       }
264
265       ++BBI;
266       LI->RemoveMachineInstrFromMaps(PHI);
267       PHI->eraseFromParent();
268       Changed = true;
269     }
270   }
271
272   // Due to the insertion of copies to split live ranges, the live intervals are
273   // guaranteed to not overlap, except in one case: an original PHI source and a
274   // PHI destination copy. In this case, they have the same value and thus don't
275   // truly intersect, so we merge them into the value live at that point.
276   // FIXME: Is there some better way we can handle this?
277   for (DestCopyMap::iterator I = InsertedDestCopies.begin(),
278        E = InsertedDestCopies.end(); I != E; ++I) {
279     unsigned DestReg = I->first;
280     unsigned DestColor = getRegColor(DestReg);
281     unsigned NewReg = RegRenamingMap[DestColor];
282
283     LiveInterval& DestLI = LI->getInterval(DestReg);
284     LiveInterval& NewLI = LI->getInterval(NewReg);
285
286     assert(DestLI.ranges.size() == 1);
287     LiveRange* DestLR = DestLI.begin();
288     VNInfo* NewVNI = NewLI.getVNInfoAt(DestLR->start);
289     if (!NewVNI) {
290       NewVNI = NewLI.createValueCopy(DestLR->valno, LI->getVNInfoAllocator());
291       MachineInstr* CopyInstr = I->second;
292       CopyInstr->getOperand(1).setIsKill(true);
293     }
294
295     LiveRange NewLR(DestLR->start, DestLR->end, NewVNI);
296     NewLI.addRange(NewLR);
297
298     LI->removeInterval(DestReg);
299     MRI->replaceRegWith(DestReg, NewReg);
300   }
301
302   // Adjust the live intervals of all PHI source registers to handle the case
303   // where the PHIs in successor blocks were the only later uses of the source
304   // register.
305   for (SrcCopySet::iterator I = InsertedSrcCopySet.begin(),
306        E = InsertedSrcCopySet.end(); I != E; ++I) {
307     MachineBasicBlock* MBB = I->first;
308     unsigned SrcReg = I->second;
309     if (unsigned RenamedRegister = RegRenamingMap[getRegColor(SrcReg)])
310       SrcReg = RenamedRegister;
311
312     LiveInterval& SrcLI = LI->getInterval(SrcReg);
313
314     bool isLiveOut = false;
315     for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
316          SE = MBB->succ_end(); SI != SE; ++SI) {
317       if (SrcLI.liveAt(LI->getMBBStartIdx(*SI))) {
318         isLiveOut = true;
319         break;
320       }
321     }
322
323     if (isLiveOut)
324       continue;
325
326     MachineOperand* LastUse = findLastUse(MBB, SrcReg);
327     assert(LastUse);
328     SlotIndex LastUseIndex = LI->getInstructionIndex(LastUse->getParent());
329     SrcLI.removeRange(LastUseIndex.getDefIndex(), LI->getMBBEndIdx(MBB));
330     LastUse->setIsKill(true);
331   }
332
333   LI->renumber();
334
335   Allocator.Reset();
336   RegNodeMap.clear();
337   InsertedSrcCopySet.clear();
338   InsertedSrcCopyMap.clear();
339   InsertedDestCopies.clear();
340
341   return Changed;
342 }
343
344 void StrongPHIElimination::addReg(unsigned Reg) {
345   if (RegNodeMap.count(Reg))
346     return;
347   RegNodeMap[Reg] = new (Allocator) Node(Reg);
348 }
349
350 StrongPHIElimination::Node*
351 StrongPHIElimination::Node::getLeader() {
352   Node* parentPointer = parent.getPointer();
353   if (parentPointer == this)
354     return this;
355   Node* newParent = parentPointer->getLeader();
356   parent.setPointer(newParent);
357   return newParent;
358 }
359
360 unsigned StrongPHIElimination::getRegColor(unsigned Reg) {
361   DenseMap<unsigned, Node*>::iterator RI = RegNodeMap.find(Reg);
362   if (RI == RegNodeMap.end())
363     return 0;
364   Node* Node = RI->second;
365   if (Node->parent.getInt() & Node::kRegisterIsolatedFlag)
366     return 0;
367   return Node->getLeader()->value;
368 }
369
370 void StrongPHIElimination::unionRegs(unsigned Reg1, unsigned Reg2) {
371   Node* Node1 = RegNodeMap[Reg1]->getLeader();
372   Node* Node2 = RegNodeMap[Reg2]->getLeader();
373
374   if (Node1->rank > Node2->rank) {
375     Node2->parent.setPointer(Node1->getLeader());
376   } else if (Node1->rank < Node2->rank) {
377     Node1->parent.setPointer(Node2->getLeader());
378   } else if (Node1 != Node2) {
379     Node2->parent.setPointer(Node1->getLeader());
380     Node1->rank++;
381   }
382 }
383
384 void StrongPHIElimination::isolateReg(unsigned Reg) {
385   Node* Node = RegNodeMap[Reg];
386   Node->parent.setInt(Node->parent.getInt() | Node::kRegisterIsolatedFlag);
387 }
388
389 unsigned StrongPHIElimination::getPHIColor(MachineInstr* PHI) {
390   assert(PHI->isPHI());
391
392   unsigned DestReg = PHI->getOperand(0).getReg();
393   Node* DestNode = RegNodeMap[DestReg];
394   if (DestNode->parent.getInt() & Node::kPHIIsolatedFlag)
395     return 0;
396
397   for (unsigned i = 1; i < PHI->getNumOperands(); i += 2) {
398     unsigned SrcColor = getRegColor(PHI->getOperand(i).getReg());
399     if (SrcColor)
400       return SrcColor;
401   }
402   return 0;
403 }
404
405 void StrongPHIElimination::isolatePHI(MachineInstr* PHI) {
406   assert(PHI->isPHI());
407   Node* Node = RegNodeMap[PHI->getOperand(0).getReg()];
408   Node->parent.setInt(Node->parent.getInt() | Node::kPHIIsolatedFlag);
409 }
410
411 void StrongPHIElimination::PartitionRegisters(MachineFunction& MF) {
412   for (MachineFunction::iterator I = MF.begin(), E = MF.end();
413        I != E; ++I) {
414     for (MachineBasicBlock::iterator BBI = I->begin(), BBE = I->end();
415          BBI != BBE && BBI->isPHI(); ++BBI) {
416       unsigned DestReg = BBI->getOperand(0).getReg();
417       addReg(DestReg);
418
419       for (unsigned i = 1; i < BBI->getNumOperands(); i += 2) {
420         unsigned SrcReg = BBI->getOperand(i).getReg();
421         addReg(SrcReg);
422         unionRegs(DestReg, SrcReg);
423       }
424     }
425   }
426 }
427
428 /// SplitInterferencesForBasicBlock - traverses a basic block, splitting any
429 /// interferences found between registers in the same congruence class. It
430 /// takes two DenseMaps as arguments that it also updates:
431 ///
432 /// 1) CurrentDominatingParent, which maps a color to the register in that
433 ///    congruence class whose definition was most recently seen.
434 ///
435 /// 2) ImmediateDominatingParent, which maps a register to the register in the
436 ///    same congruence class that most immediately dominates it.
437 ///
438 /// This function assumes that it is being called in a depth-first traversal
439 /// of the dominator tree.
440 ///
441 /// The algorithm used here is a generalization of the dominance-based SSA test
442 /// for two variables. If there are variables a_1, ..., a_n such that
443 ///
444 ///   def(a_1) dom ... dom def(a_n),
445 ///
446 /// then we can test for an interference between any two a_i by only using O(n)
447 /// interference tests between pairs of variables. If i < j and a_i and a_j
448 /// interfere, then a_i is alive at def(a_j), so it is also alive at def(a_i+1).
449 /// Thus, in order to test for an interference involving a_i, we need only check
450 /// for a potential interference with a_i+1.
451 ///
452 /// This method can be generalized to arbitrary sets of variables by performing
453 /// a depth-first traversal of the dominator tree. As we traverse down a branch
454 /// of the dominator tree, we keep track of the current dominating variable and
455 /// only perform an interference test with that variable. However, when we go to
456 /// another branch of the dominator tree, the definition of the current dominating
457 /// variable may no longer dominate the current block. In order to correct this,
458 /// we need to use a stack of past choices of the current dominating variable
459 /// and pop from this stack until we find a variable whose definition actually
460 /// dominates the current block.
461 /// 
462 /// There will be one push on this stack for each variable that has become the
463 /// current dominating variable, so instead of using an explicit stack we can
464 /// simply associate the previous choice for a current dominating variable with
465 /// the new choice. This works better in our implementation, where we test for
466 /// interference in multiple distinct sets at once.
467 void
468 StrongPHIElimination::SplitInterferencesForBasicBlock(
469     MachineBasicBlock& MBB,
470     DenseMap<unsigned, unsigned>& CurrentDominatingParent,
471     DenseMap<unsigned, unsigned>& ImmediateDominatingParent) {
472   for (MachineBasicBlock::iterator BBI = MBB.begin(), BBE = MBB.end();
473   BBI != BBE; ++BBI) {
474     for (MachineInstr::const_mop_iterator I = BBI->operands_begin(),
475          E = BBI->operands_end(); I != E; ++I) {
476       const MachineOperand& MO = *I;
477
478       // FIXME: This would be faster if it were possible to bail out of checking
479       // an instruction's operands after the explicit defs, but this is incorrect
480       // for variadic instructions, which may appear before register allocation
481       // in the future.
482       if (!MO.isReg() || !MO.isDef())
483         continue;
484
485       unsigned DestReg = MO.getReg();
486       if (!DestReg || !TargetRegisterInfo::isVirtualRegister(DestReg))
487         continue;
488
489       // If the virtual register being defined is not used in any PHI or has
490       // already been isolated, then there are no more interferences to check.
491       unsigned DestColor = getRegColor(DestReg);
492       if (!DestColor)
493         continue;
494
495       // The input to this pass sometimes is not in SSA form in every basic
496       // block, as some virtual registers have redefinitions. We could eliminate
497       // this by fixing the passes that generate the non-SSA code, or we could
498       // handle it here by tracking defining machine instructions rather than
499       // virtual registers. For now, we just handle the situation conservatively
500       // in a way that will possibly lead to false interferences.
501       unsigned NewParent = CurrentDominatingParent[DestColor];
502       if (NewParent == DestReg)
503         continue;
504
505       // Pop registers from the stack represented by ImmediateDominatingParent
506       // until we find a parent that dominates the current instruction.
507       while (NewParent && (!DT->dominates(MRI->getVRegDef(NewParent), BBI)
508                            || !getRegColor(NewParent)))
509         NewParent = ImmediateDominatingParent[NewParent];
510
511       // If NewParent is nonzero, then its definition dominates the current
512       // instruction, so it is only necessary to check for the liveness of
513       // NewParent in order to check for an interference.
514       if (NewParent
515           && LI->getInterval(NewParent).liveAt(LI->getInstructionIndex(BBI))) {
516         // If there is an interference, always isolate the new register. This
517         // could be improved by using a heuristic that decides which of the two
518         // registers to isolate.
519         isolateReg(DestReg);
520         CurrentDominatingParent[DestColor] = NewParent;
521       } else {
522         // If there is no interference, update ImmediateDominatingParent and set
523         // the CurrentDominatingParent for this color to the current register.
524         ImmediateDominatingParent[DestReg] = NewParent;
525         CurrentDominatingParent[DestColor] = DestReg;
526       }
527     }
528   }
529
530   // We now walk the PHIs in successor blocks and check for interferences. This
531   // is necesary because the use of a PHI's operands are logically contained in
532   // the predecessor block. The def of a PHI's destination register is processed
533   // along with the other defs in a basic block.
534
535   // The map CurrentPHIForColor maps a color to a pair of a MachineInstr* and a
536   // virtual register, which is the operand of that PHI corresponding to the
537   // current basic block.
538   // FIXME: This should use a container that doesn't always perform heap
539   // allocation.
540   DenseMap<unsigned, std::pair<MachineInstr*, unsigned> > CurrentPHIForColor;
541
542   for (MachineBasicBlock::succ_iterator SI = MBB.succ_begin(),
543        SE = MBB.succ_end(); SI != SE; ++SI) {
544     for (MachineBasicBlock::iterator BBI = (*SI)->begin(), BBE = (*SI)->end();
545          BBI != BBE && BBI->isPHI(); ++BBI) {
546       MachineInstr* PHI = BBI;
547
548       // If a PHI is already isolated, either by being isolated directly or
549       // having all of its operands isolated, ignore it.
550       unsigned Color = getPHIColor(PHI);
551       if (!Color)
552         continue;
553
554       // Find the index of the PHI operand that corresponds to this basic block.
555       unsigned PredIndex;
556       for (PredIndex = 1; PredIndex < PHI->getNumOperands(); PredIndex += 2) {
557         if (PHI->getOperand(PredIndex + 1).getMBB() == &MBB)
558           break;
559       }
560       assert(PredIndex < PHI->getNumOperands());
561       unsigned PredOperandReg = PHI->getOperand(PredIndex).getReg();
562
563       // Pop registers from the stack represented by ImmediateDominatingParent
564       // until we find a parent that dominates the current instruction.
565       unsigned NewParent = CurrentDominatingParent[Color];
566       while (NewParent
567              && (!DT->dominates(MRI->getVRegDef(NewParent)->getParent(), &MBB)
568                  || !getRegColor(NewParent)))
569         NewParent = ImmediateDominatingParent[NewParent];
570       CurrentDominatingParent[Color] = NewParent;
571
572       // If there is an interference with a register, always isolate the
573       // register rather than the PHI. It is also possible to isolate the
574       // PHI, but that introduces copies for all of the registers involved
575       // in that PHI.
576       if (NewParent && LI->isLiveOutOfMBB(LI->getInterval(NewParent), &MBB)
577                     && NewParent != PredOperandReg)
578         isolateReg(NewParent);
579
580       std::pair<MachineInstr*, unsigned> CurrentPHI = CurrentPHIForColor[Color];
581
582       // If two PHIs have the same operand from every shared predecessor, then
583       // they don't actually interfere. Otherwise, isolate the current PHI. This
584       // could possibly be improved, e.g. we could isolate the PHI with the
585       // fewest operands.
586       if (CurrentPHI.first && CurrentPHI.second != PredOperandReg)
587         isolatePHI(PHI);
588       else
589         CurrentPHIForColor[Color] = std::make_pair(PHI, PredOperandReg);
590     }
591   }
592 }
593
594 void StrongPHIElimination::InsertCopiesForPHI(MachineInstr* PHI,
595                                               MachineBasicBlock* MBB) {
596   assert(PHI->isPHI());
597   unsigned PHIColor = getPHIColor(PHI);
598
599   for (unsigned i = 1; i < PHI->getNumOperands(); i += 2) {
600     MachineOperand& SrcMO = PHI->getOperand(i);
601
602     // If a source is defined by an implicit def, there is no need to insert a
603     // copy in the predecessor.
604     if (SrcMO.isUndef())
605       continue;
606
607     unsigned SrcReg = SrcMO.getReg();
608     assert(TargetRegisterInfo::isVirtualRegister(SrcReg) &&
609            "Machine PHI Operands must all be virtual registers!");
610
611     MachineBasicBlock* PredBB = PHI->getOperand(i + 1).getMBB();
612     unsigned SrcColor = getRegColor(SrcReg);
613
614     // If neither the PHI nor the operand were isolated, then we only need to
615     // set the phi-kill flag on the VNInfo at this PHI.
616     if (PHIColor && SrcColor == PHIColor) {
617       LiveInterval& SrcInterval = LI->getInterval(SrcReg);
618       SlotIndex PredIndex = LI->getMBBEndIdx(PredBB);
619       VNInfo* SrcVNI = SrcInterval.getVNInfoAt(PredIndex.getPrevIndex());
620       assert(SrcVNI);
621       SrcVNI->setHasPHIKill(true);
622       continue;
623     }
624
625     unsigned CopyReg = 0;
626     if (PHIColor) {
627       SrcCopyMap::const_iterator I
628         = InsertedSrcCopyMap.find(std::make_pair(PredBB, PHIColor));
629       CopyReg
630         = I != InsertedSrcCopyMap.end() ? I->second->getOperand(0).getReg() : 0;
631     }
632
633     if (!CopyReg) {
634       const TargetRegisterClass* RC = MRI->getRegClass(SrcReg);
635       CopyReg = MRI->createVirtualRegister(RC);
636
637       MachineBasicBlock::iterator
638         CopyInsertPoint = findPHICopyInsertPoint(PredBB, MBB, SrcReg);
639       unsigned SrcSubReg = SrcMO.getSubReg();
640       MachineInstr* CopyInstr = BuildMI(*PredBB,
641                                         CopyInsertPoint,
642                                         PHI->getDebugLoc(),
643                                         TII->get(TargetOpcode::COPY),
644                                         CopyReg).addReg(SrcReg, 0, SrcSubReg);
645       LI->InsertMachineInstrInMaps(CopyInstr);
646
647       // addLiveRangeToEndOfBlock() also adds the phikill flag to the VNInfo for
648       // the newly added range.
649       LI->addLiveRangeToEndOfBlock(CopyReg, CopyInstr);
650       InsertedSrcCopySet.insert(std::make_pair(PredBB, SrcReg));
651
652       addReg(CopyReg);
653       if (PHIColor) {
654         unionRegs(PHIColor, CopyReg);
655         assert(getRegColor(CopyReg) != CopyReg);
656       } else {
657         PHIColor = CopyReg;
658         assert(getRegColor(CopyReg) == CopyReg);
659       }
660
661       if (!InsertedSrcCopyMap.count(std::make_pair(PredBB, PHIColor)))
662         InsertedSrcCopyMap[std::make_pair(PredBB, PHIColor)] = CopyInstr;
663     }
664
665     SrcMO.setReg(CopyReg);
666
667     // If SrcReg is not live beyond the PHI, trim its interval so that it is no
668     // longer live-in to MBB. Note that SrcReg may appear in other PHIs that are
669     // processed later, but this is still correct to do at this point because we
670     // never rely on LiveIntervals being correct while inserting copies.
671     // FIXME: Should this just count uses at PHIs like the normal PHIElimination
672     // pass does?
673     LiveInterval& SrcLI = LI->getInterval(SrcReg);
674     SlotIndex MBBStartIndex = LI->getMBBStartIdx(MBB);
675     SlotIndex PHIIndex = LI->getInstructionIndex(PHI);
676     SlotIndex NextInstrIndex = PHIIndex.getNextIndex();
677     if (SrcLI.liveAt(MBBStartIndex) && SrcLI.expiredAt(NextInstrIndex))
678       SrcLI.removeRange(MBBStartIndex, PHIIndex, true);
679   }
680
681   unsigned DestReg = PHI->getOperand(0).getReg();
682   unsigned DestColor = getRegColor(DestReg);
683
684   if (PHIColor && DestColor == PHIColor) {
685     LiveInterval& DestLI = LI->getInterval(DestReg);
686
687     // Set the phi-def flag for the VN at this PHI.
688     SlotIndex PHIIndex = LI->getInstructionIndex(PHI);
689     VNInfo* DestVNI = DestLI.getVNInfoAt(PHIIndex.getDefIndex());
690     assert(DestVNI);
691     DestVNI->setIsPHIDef(true);
692   
693     // Prior to PHI elimination, the live ranges of PHIs begin at their defining
694     // instruction. After PHI elimination, PHI instructions are replaced by VNs
695     // with the phi-def flag set, and the live ranges of these VNs start at the
696     // beginning of the basic block.
697     SlotIndex MBBStartIndex = LI->getMBBStartIdx(MBB);
698     DestVNI->def = MBBStartIndex;
699     DestLI.addRange(LiveRange(MBBStartIndex,
700                               PHIIndex.getDefIndex(),
701                               DestVNI));
702     return;
703   }
704
705   const TargetRegisterClass* RC = MRI->getRegClass(DestReg);
706   unsigned CopyReg = MRI->createVirtualRegister(RC);
707
708   MachineInstr* CopyInstr = BuildMI(*MBB,
709                                     MBB->SkipPHIsAndLabels(MBB->begin()),
710                                     PHI->getDebugLoc(),
711                                     TII->get(TargetOpcode::COPY),
712                                     DestReg).addReg(CopyReg);
713   LI->InsertMachineInstrInMaps(CopyInstr);
714   PHI->getOperand(0).setReg(CopyReg);
715
716   // Add the region from the beginning of MBB to the copy instruction to
717   // CopyReg's live interval, and give the VNInfo the phidef flag.
718   LiveInterval& CopyLI = LI->getOrCreateInterval(CopyReg);
719   SlotIndex MBBStartIndex = LI->getMBBStartIdx(MBB);
720   SlotIndex DestCopyIndex = LI->getInstructionIndex(CopyInstr);
721   VNInfo* CopyVNI = CopyLI.getNextValue(MBBStartIndex,
722                                         CopyInstr,
723                                         LI->getVNInfoAllocator());
724   CopyVNI->setIsPHIDef(true);
725   CopyLI.addRange(LiveRange(MBBStartIndex,
726                             DestCopyIndex.getDefIndex(),
727                             CopyVNI));
728
729   // Adjust DestReg's live interval to adjust for its new definition at
730   // CopyInstr.
731   LiveInterval& DestLI = LI->getOrCreateInterval(DestReg);
732   SlotIndex PHIIndex = LI->getInstructionIndex(PHI);
733   DestLI.removeRange(PHIIndex.getDefIndex(), DestCopyIndex.getDefIndex());
734
735   VNInfo* DestVNI = DestLI.getVNInfoAt(DestCopyIndex.getDefIndex());
736   assert(DestVNI);
737   DestVNI->def = DestCopyIndex.getDefIndex();
738
739   InsertedDestCopies[CopyReg] = CopyInstr;
740 }
741
742 void StrongPHIElimination::MergeLIsAndRename(unsigned Reg, unsigned NewReg) {
743   if (Reg == NewReg)
744     return;
745
746   LiveInterval& OldLI = LI->getInterval(Reg);
747   LiveInterval& NewLI = LI->getInterval(NewReg);
748
749   // Merge the live ranges of the two registers.
750   DenseMap<VNInfo*, VNInfo*> VNMap;
751   for (LiveInterval::iterator LRI = OldLI.begin(), LRE = OldLI.end();
752        LRI != LRE; ++LRI) {
753     LiveRange OldLR = *LRI;
754     VNInfo* OldVN = OldLR.valno;
755
756     VNInfo*& NewVN = VNMap[OldVN];
757     if (!NewVN) {
758       NewVN = NewLI.createValueCopy(OldVN, LI->getVNInfoAllocator());
759       VNMap[OldVN] = NewVN;
760     }
761
762     LiveRange LR(OldLR.start, OldLR.end, NewVN);
763     NewLI.addRange(LR);
764   }
765
766   // Remove the LiveInterval for the register being renamed and replace all
767   // of its defs and uses with the new register.
768   LI->removeInterval(Reg);
769   MRI->replaceRegWith(Reg, NewReg);
770 }