Get rid of static constructors for pass registration. Instead, every pass exposes...
[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 machine instruction PHI nodes by inserting copy
11 // instructions, using an intelligent copy-folding technique based on
12 // dominator information.  This is technique is derived from:
13 // 
14 //    Budimlic, et al. Fast copy coalescing and live-range identification.
15 //    In Proceedings of the ACM SIGPLAN 2002 Conference on Programming Language
16 //    Design and Implementation (Berlin, Germany, June 17 - 19, 2002).
17 //    PLDI '02. ACM, New York, NY, 25-32.
18 //    DOI= http://doi.acm.org/10.1145/512529.512534
19 //
20 //===----------------------------------------------------------------------===//
21
22 #define DEBUG_TYPE "strongphielim"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
25 #include "llvm/CodeGen/MachineDominators.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/MachineLoopInfo.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/RegisterCoalescer.h"
32 #include "llvm/Target/TargetInstrInfo.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include "llvm/ADT/DepthFirstIterator.h"
35 #include "llvm/ADT/Statistic.h"
36 #include "llvm/Support/Debug.h"
37 using namespace llvm;
38
39 namespace {
40   struct StrongPHIElimination : public MachineFunctionPass {
41     static char ID; // Pass identification, replacement for typeid
42     StrongPHIElimination() : MachineFunctionPass(ID) {
43       initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
44     }
45
46     // Waiting stores, for each MBB, the set of copies that need to
47     // be inserted into that MBB
48     DenseMap<MachineBasicBlock*,
49              std::multimap<unsigned, unsigned> > Waiting;
50     
51     // Stacks holds the renaming stack for each register
52     std::map<unsigned, std::vector<unsigned> > Stacks;
53     
54     // Registers in UsedByAnother are PHI nodes that are themselves
55     // used as operands to another PHI node
56     std::set<unsigned> UsedByAnother;
57     
58     // RenameSets are the is a map from a PHI-defined register
59     // to the input registers to be coalesced along with the 
60     // predecessor block for those input registers.
61     std::map<unsigned, std::map<unsigned, MachineBasicBlock*> > RenameSets;
62     
63     // PhiValueNumber holds the ID numbers of the VNs for each phi that we're
64     // eliminating, indexed by the register defined by that phi.
65     std::map<unsigned, unsigned> PhiValueNumber;
66
67     // Store the DFS-in number of each block
68     DenseMap<MachineBasicBlock*, unsigned> preorder;
69     
70     // Store the DFS-out number of each block
71     DenseMap<MachineBasicBlock*, unsigned> maxpreorder;
72
73     bool runOnMachineFunction(MachineFunction &Fn);
74     
75     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
76       AU.setPreservesCFG();
77       AU.addRequired<MachineDominatorTree>();
78       AU.addRequired<SlotIndexes>();
79       AU.addPreserved<SlotIndexes>();
80       AU.addRequired<LiveIntervals>();
81       
82       // TODO: Actually make this true.
83       AU.addPreserved<LiveIntervals>();
84       AU.addPreserved<RegisterCoalescer>();
85       MachineFunctionPass::getAnalysisUsage(AU);
86     }
87     
88     virtual void releaseMemory() {
89       preorder.clear();
90       maxpreorder.clear();
91       
92       Waiting.clear();
93       Stacks.clear();
94       UsedByAnother.clear();
95       RenameSets.clear();
96     }
97
98   private:
99     
100     /// DomForestNode - Represents a node in the "dominator forest".  This is
101     /// a forest in which the nodes represent registers and the edges
102     /// represent a dominance relation in the block defining those registers.
103     struct DomForestNode {
104     private:
105       // Store references to our children
106       std::vector<DomForestNode*> children;
107       // The register we represent
108       unsigned reg;
109       
110       // Add another node as our child
111       void addChild(DomForestNode* DFN) { children.push_back(DFN); }
112       
113     public:
114       typedef std::vector<DomForestNode*>::iterator iterator;
115       
116       // Create a DomForestNode by providing the register it represents, and
117       // the node to be its parent.  The virtual root node has register 0
118       // and a null parent.
119       DomForestNode(unsigned r, DomForestNode* parent) : reg(r) {
120         if (parent)
121           parent->addChild(this);
122       }
123       
124       ~DomForestNode() {
125         for (iterator I = begin(), E = end(); I != E; ++I)
126           delete *I;
127       }
128       
129       /// getReg - Return the regiser that this node represents
130       inline unsigned getReg() { return reg; }
131       
132       // Provide iterator access to our children
133       inline DomForestNode::iterator begin() { return children.begin(); }
134       inline DomForestNode::iterator end() { return children.end(); }
135     };
136     
137     void computeDFS(MachineFunction& MF);
138     void processBlock(MachineBasicBlock* MBB);
139     
140     std::vector<DomForestNode*> computeDomForest(
141                            std::map<unsigned, MachineBasicBlock*>& instrs,
142                                                  MachineRegisterInfo& MRI);
143     void processPHIUnion(MachineInstr* Inst,
144                          std::map<unsigned, MachineBasicBlock*>& PHIUnion,
145                          std::vector<StrongPHIElimination::DomForestNode*>& DF,
146                          std::vector<std::pair<unsigned, unsigned> >& locals);
147     void ScheduleCopies(MachineBasicBlock* MBB, std::set<unsigned>& pushed);
148     void InsertCopies(MachineDomTreeNode* MBB,
149                       SmallPtrSet<MachineBasicBlock*, 16>& v);
150     bool mergeLiveIntervals(unsigned primary, unsigned secondary);
151   };
152 }
153
154 char StrongPHIElimination::ID = 0;
155 INITIALIZE_PASS_BEGIN(StrongPHIElimination, "strong-phi-node-elimination",
156   "Eliminate PHI nodes for register allocation, intelligently", false, false)
157 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
158 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
159 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
160 INITIALIZE_PASS_END(StrongPHIElimination, "strong-phi-node-elimination",
161   "Eliminate PHI nodes for register allocation, intelligently", false, false)
162
163 char &llvm::StrongPHIEliminationID = StrongPHIElimination::ID;
164
165 /// computeDFS - Computes the DFS-in and DFS-out numbers of the dominator tree
166 /// of the given MachineFunction.  These numbers are then used in other parts
167 /// of the PHI elimination process.
168 void StrongPHIElimination::computeDFS(MachineFunction& MF) {
169   SmallPtrSet<MachineDomTreeNode*, 8> frontier;
170   SmallPtrSet<MachineDomTreeNode*, 8> visited;
171   
172   unsigned time = 0;
173   
174   MachineDominatorTree& DT = getAnalysis<MachineDominatorTree>();
175   
176   MachineDomTreeNode* node = DT.getRootNode();
177   
178   std::vector<MachineDomTreeNode*> worklist;
179   worklist.push_back(node);
180   
181   while (!worklist.empty()) {
182     MachineDomTreeNode* currNode = worklist.back();
183     
184     if (!frontier.count(currNode)) {
185       frontier.insert(currNode);
186       ++time;
187       preorder.insert(std::make_pair(currNode->getBlock(), time));
188     }
189     
190     bool inserted = false;
191     for (MachineDomTreeNode::iterator I = currNode->begin(), E = currNode->end();
192          I != E; ++I)
193       if (!frontier.count(*I) && !visited.count(*I)) {
194         worklist.push_back(*I);
195         inserted = true;
196         break;
197       }
198     
199     if (!inserted) {
200       frontier.erase(currNode);
201       visited.insert(currNode);
202       maxpreorder.insert(std::make_pair(currNode->getBlock(), time));
203       
204       worklist.pop_back();
205     }
206   }
207 }
208
209 namespace {
210
211 /// PreorderSorter - a helper class that is used to sort registers
212 /// according to the preorder number of their defining blocks
213 class PreorderSorter {
214 private:
215   DenseMap<MachineBasicBlock*, unsigned>& preorder;
216   MachineRegisterInfo& MRI;
217   
218 public:
219   PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p,
220                 MachineRegisterInfo& M) : preorder(p), MRI(M) { }
221   
222   bool operator()(unsigned A, unsigned B) {
223     if (A == B)
224       return false;
225     
226     MachineBasicBlock* ABlock = MRI.getVRegDef(A)->getParent();
227     MachineBasicBlock* BBlock = MRI.getVRegDef(B)->getParent();
228     
229     if (preorder[ABlock] < preorder[BBlock])
230       return true;
231     else if (preorder[ABlock] > preorder[BBlock])
232       return false;
233     
234     return false;
235   }
236 };
237
238 }
239
240 /// computeDomForest - compute the subforest of the DomTree corresponding
241 /// to the defining blocks of the registers in question
242 std::vector<StrongPHIElimination::DomForestNode*>
243 StrongPHIElimination::computeDomForest(
244                   std::map<unsigned, MachineBasicBlock*>& regs, 
245                                        MachineRegisterInfo& MRI) {
246   // Begin by creating a virtual root node, since the actual results
247   // may well be a forest.  Assume this node has maximum DFS-out number.
248   DomForestNode* VirtualRoot = new DomForestNode(0, 0);
249   maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL));
250   
251   // Populate a worklist with the registers
252   std::vector<unsigned> worklist;
253   worklist.reserve(regs.size());
254   for (std::map<unsigned, MachineBasicBlock*>::iterator I = regs.begin(),
255        E = regs.end(); I != E; ++I)
256     worklist.push_back(I->first);
257   
258   // Sort the registers by the DFS-in number of their defining block
259   PreorderSorter PS(preorder, MRI);
260   std::sort(worklist.begin(), worklist.end(), PS);
261   
262   // Create a "current parent" stack, and put the virtual root on top of it
263   DomForestNode* CurrentParent = VirtualRoot;
264   std::vector<DomForestNode*> stack;
265   stack.push_back(VirtualRoot);
266   
267   // Iterate over all the registers in the previously computed order
268   for (std::vector<unsigned>::iterator I = worklist.begin(), E = worklist.end();
269        I != E; ++I) {
270     unsigned pre = preorder[MRI.getVRegDef(*I)->getParent()];
271     MachineBasicBlock* parentBlock = CurrentParent->getReg() ?
272                  MRI.getVRegDef(CurrentParent->getReg())->getParent() :
273                  0;
274     
275     // If the DFS-in number of the register is greater than the DFS-out number
276     // of the current parent, repeatedly pop the parent stack until it isn't.
277     while (pre > maxpreorder[parentBlock]) {
278       stack.pop_back();
279       CurrentParent = stack.back();
280       
281       parentBlock = CurrentParent->getReg() ?
282                    MRI.getVRegDef(CurrentParent->getReg())->getParent() :
283                    0;
284     }
285     
286     // Now that we've found the appropriate parent, create a DomForestNode for
287     // this register and attach it to the forest
288     DomForestNode* child = new DomForestNode(*I, CurrentParent);
289     
290     // Push this new node on the "current parent" stack
291     stack.push_back(child);
292     CurrentParent = child;
293   }
294   
295   // Return a vector containing the children of the virtual root node
296   std::vector<DomForestNode*> ret;
297   ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end());
298   return ret;
299 }
300
301 /// isLiveIn - helper method that determines, from a regno, if a register
302 /// is live into a block
303 static bool isLiveIn(unsigned r, MachineBasicBlock* MBB,
304                      LiveIntervals& LI) {
305   LiveInterval& I = LI.getOrCreateInterval(r);
306   SlotIndex idx = LI.getMBBStartIdx(MBB);
307   return I.liveAt(idx);
308 }
309
310 /// isLiveOut - help method that determines, from a regno, if a register is
311 /// live out of a block.
312 static bool isLiveOut(unsigned r, MachineBasicBlock* MBB,
313                       LiveIntervals& LI) {
314   for (MachineBasicBlock::succ_iterator PI = MBB->succ_begin(),
315        E = MBB->succ_end(); PI != E; ++PI)
316     if (isLiveIn(r, *PI, LI))
317       return true;
318   
319   return false;
320 }
321
322 /// interferes - checks for local interferences by scanning a block.  The only
323 /// trick parameter is 'mode' which tells it the relationship of the two
324 /// registers. 0 - defined in the same block, 1 - first properly dominates
325 /// second, 2 - second properly dominates first 
326 static bool interferes(unsigned a, unsigned b, MachineBasicBlock* scan,
327                        LiveIntervals& LV, unsigned mode) {
328   MachineInstr* def = 0;
329   MachineInstr* kill = 0;
330   
331   // The code is still in SSA form at this point, so there is only one
332   // definition per VReg.  Thus we can safely use MRI->getVRegDef().
333   const MachineRegisterInfo* MRI = &scan->getParent()->getRegInfo();
334   
335   bool interference = false;
336   
337   // Wallk the block, checking for interferences
338   for (MachineBasicBlock::iterator MBI = scan->begin(), MBE = scan->end();
339        MBI != MBE; ++MBI) {
340     MachineInstr* curr = MBI;
341     
342     // Same defining block...
343     if (mode == 0) {
344       if (curr == MRI->getVRegDef(a)) {
345         // If we find our first definition, save it
346         if (!def) {
347           def = curr;
348         // If there's already an unkilled definition, then 
349         // this is an interference
350         } else if (!kill) {
351           interference = true;
352           break;
353         // If there's a definition followed by a KillInst, then
354         // they can't interfere
355         } else {
356           interference = false;
357           break;
358         }
359       // Symmetric with the above
360       } else if (curr == MRI->getVRegDef(b)) {
361         if (!def) {
362           def = curr;
363         } else if (!kill) {
364           interference = true;
365           break;
366         } else {
367           interference = false;
368           break;
369         }
370       // Store KillInsts if they match up with the definition
371       } else if (curr->killsRegister(a)) {
372         if (def == MRI->getVRegDef(a)) {
373           kill = curr;
374         } else if (curr->killsRegister(b)) {
375           if (def == MRI->getVRegDef(b)) {
376             kill = curr;
377           }
378         }
379       }
380     // First properly dominates second...
381     } else if (mode == 1) {
382       if (curr == MRI->getVRegDef(b)) {
383         // Definition of second without kill of first is an interference
384         if (!kill) {
385           interference = true;
386           break;
387         // Definition after a kill is a non-interference
388         } else {
389           interference = false;
390           break;
391         }
392       // Save KillInsts of First
393       } else if (curr->killsRegister(a)) {
394         kill = curr;
395       }
396     // Symmetric with the above
397     } else if (mode == 2) {
398       if (curr == MRI->getVRegDef(a)) {
399         if (!kill) {
400           interference = true;
401           break;
402         } else {
403           interference = false;
404           break;
405         }
406       } else if (curr->killsRegister(b)) {
407         kill = curr;
408       }
409     }
410   }
411   
412   return interference;
413 }
414
415 /// processBlock - Determine how to break up PHIs in the current block.  Each
416 /// PHI is broken up by some combination of renaming its operands and inserting
417 /// copies.  This method is responsible for determining which operands receive
418 /// which treatment.
419 void StrongPHIElimination::processBlock(MachineBasicBlock* MBB) {
420   LiveIntervals& LI = getAnalysis<LiveIntervals>();
421   MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo();
422   
423   // Holds names that have been added to a set in any PHI within this block
424   // before the current one.
425   std::set<unsigned> ProcessedNames;
426   
427   // Iterate over all the PHI nodes in this block
428   MachineBasicBlock::iterator P = MBB->begin();
429   while (P != MBB->end() && P->isPHI()) {
430     unsigned DestReg = P->getOperand(0).getReg();
431     
432     // Don't both doing PHI elimination for dead PHI's.
433     if (P->registerDefIsDead(DestReg)) {
434       ++P;
435       continue;
436     }
437
438     LiveInterval& PI = LI.getOrCreateInterval(DestReg);
439     SlotIndex pIdx = LI.getInstructionIndex(P).getDefIndex();
440     VNInfo* PVN = PI.getLiveRangeContaining(pIdx)->valno;
441     PhiValueNumber.insert(std::make_pair(DestReg, PVN->id));
442
443     // PHIUnion is the set of incoming registers to the PHI node that
444     // are going to be renames rather than having copies inserted.  This set
445     // is refinded over the course of this function.  UnionedBlocks is the set
446     // of corresponding MBBs.
447     std::map<unsigned, MachineBasicBlock*> PHIUnion;
448     SmallPtrSet<MachineBasicBlock*, 8> UnionedBlocks;
449   
450     // Iterate over the operands of the PHI node
451     for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
452       unsigned SrcReg = P->getOperand(i-1).getReg();
453       
454       // Don't need to try to coalesce a register with itself.
455       if (SrcReg == DestReg) {
456         ProcessedNames.insert(SrcReg);
457         continue;
458       }
459       
460       // We don't need to insert copies for implicit_defs.
461       MachineInstr* DefMI = MRI.getVRegDef(SrcReg);
462       if (DefMI->isImplicitDef())
463         ProcessedNames.insert(SrcReg);
464     
465       // Check for trivial interferences via liveness information, allowing us
466       // to avoid extra work later.  Any registers that interfere cannot both
467       // be in the renaming set, so choose one and add copies for it instead.
468       // The conditions are:
469       //   1) if the operand is live into the PHI node's block OR
470       //   2) if the PHI node is live out of the operand's defining block OR
471       //   3) if the operand is itself a PHI node and the original PHI is
472       //      live into the operand's defining block OR
473       //   4) if the operand is already being renamed for another PHI node
474       //      in this block OR
475       //   5) if any two operands are defined in the same block, insert copies
476       //      for one of them
477       if (isLiveIn(SrcReg, P->getParent(), LI) ||
478           isLiveOut(P->getOperand(0).getReg(),
479                     MRI.getVRegDef(SrcReg)->getParent(), LI) ||
480           ( MRI.getVRegDef(SrcReg)->isPHI() &&
481             isLiveIn(P->getOperand(0).getReg(),
482                      MRI.getVRegDef(SrcReg)->getParent(), LI) ) ||
483           ProcessedNames.count(SrcReg) ||
484           UnionedBlocks.count(MRI.getVRegDef(SrcReg)->getParent())) {
485         
486         // Add a copy for the selected register
487         MachineBasicBlock* From = P->getOperand(i).getMBB();
488         Waiting[From].insert(std::make_pair(SrcReg, DestReg));
489         UsedByAnother.insert(SrcReg);
490       } else {
491         // Otherwise, add it to the renaming set
492         PHIUnion.insert(std::make_pair(SrcReg,P->getOperand(i).getMBB()));
493         UnionedBlocks.insert(MRI.getVRegDef(SrcReg)->getParent());
494       }
495     }
496     
497     // Compute the dominator forest for the renaming set.  This is a forest
498     // where the nodes are the registers and the edges represent dominance 
499     // relations between the defining blocks of the registers
500     std::vector<StrongPHIElimination::DomForestNode*> DF = 
501                                                 computeDomForest(PHIUnion, MRI);
502     
503     // Walk DomForest to resolve interferences at an inter-block level.  This
504     // will remove registers from the renaming set (and insert copies for them)
505     // if interferences are found.
506     std::vector<std::pair<unsigned, unsigned> > localInterferences;
507     processPHIUnion(P, PHIUnion, DF, localInterferences);
508     
509     // If one of the inputs is defined in the same block as the current PHI
510     // then we need to check for a local interference between that input and
511     // the PHI.
512     for (std::map<unsigned, MachineBasicBlock*>::iterator I = PHIUnion.begin(),
513          E = PHIUnion.end(); I != E; ++I)
514       if (MRI.getVRegDef(I->first)->getParent() == P->getParent())
515         localInterferences.push_back(std::make_pair(I->first,
516                                                     P->getOperand(0).getReg()));
517     
518     // The dominator forest walk may have returned some register pairs whose
519     // interference cannot be determined from dominator analysis.  We now 
520     // examine these pairs for local interferences.
521     for (std::vector<std::pair<unsigned, unsigned> >::iterator I =
522         localInterferences.begin(), E = localInterferences.end(); I != E; ++I) {
523       std::pair<unsigned, unsigned> p = *I;
524       
525       MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
526       
527       // Determine the block we need to scan and the relationship between
528       // the two registers
529       MachineBasicBlock* scan = 0;
530       unsigned mode = 0;
531       if (MRI.getVRegDef(p.first)->getParent() ==
532           MRI.getVRegDef(p.second)->getParent()) {
533         scan = MRI.getVRegDef(p.first)->getParent();
534         mode = 0; // Same block
535       } else if (MDT.dominates(MRI.getVRegDef(p.first)->getParent(),
536                                MRI.getVRegDef(p.second)->getParent())) {
537         scan = MRI.getVRegDef(p.second)->getParent();
538         mode = 1; // First dominates second
539       } else {
540         scan = MRI.getVRegDef(p.first)->getParent();
541         mode = 2; // Second dominates first
542       }
543       
544       // If there's an interference, we need to insert  copies
545       if (interferes(p.first, p.second, scan, LI, mode)) {
546         // Insert copies for First
547         for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
548           if (P->getOperand(i-1).getReg() == p.first) {
549             unsigned SrcReg = p.first;
550             MachineBasicBlock* From = P->getOperand(i).getMBB();
551             
552             Waiting[From].insert(std::make_pair(SrcReg,
553                                                 P->getOperand(0).getReg()));
554             UsedByAnother.insert(SrcReg);
555             
556             PHIUnion.erase(SrcReg);
557           }
558         }
559       }
560     }
561     
562     // Add the renaming set for this PHI node to our overall renaming information
563     for (std::map<unsigned, MachineBasicBlock*>::iterator QI = PHIUnion.begin(),
564          QE = PHIUnion.end(); QI != QE; ++QI) {
565       DEBUG(dbgs() << "Adding Renaming: " << QI->first << " -> "
566                    << P->getOperand(0).getReg() << "\n");
567     }
568     
569     RenameSets.insert(std::make_pair(P->getOperand(0).getReg(), PHIUnion));
570     
571     // Remember which registers are already renamed, so that we don't try to 
572     // rename them for another PHI node in this block
573     for (std::map<unsigned, MachineBasicBlock*>::iterator I = PHIUnion.begin(),
574          E = PHIUnion.end(); I != E; ++I)
575       ProcessedNames.insert(I->first);
576     
577     ++P;
578   }
579 }
580
581 /// processPHIUnion - Take a set of candidate registers to be coalesced when
582 /// decomposing the PHI instruction.  Use the DominanceForest to remove the ones
583 /// that are known to interfere, and flag others that need to be checked for
584 /// local interferences.
585 void StrongPHIElimination::processPHIUnion(MachineInstr* Inst,
586                         std::map<unsigned, MachineBasicBlock*>& PHIUnion,
587                         std::vector<StrongPHIElimination::DomForestNode*>& DF,
588                         std::vector<std::pair<unsigned, unsigned> >& locals) {
589   
590   std::vector<DomForestNode*> worklist(DF.begin(), DF.end());
591   SmallPtrSet<DomForestNode*, 4> visited;
592   
593   // Code is still in SSA form, so we can use MRI::getVRegDef()
594   MachineRegisterInfo& MRI = Inst->getParent()->getParent()->getRegInfo();
595   
596   LiveIntervals& LI = getAnalysis<LiveIntervals>();
597   unsigned DestReg = Inst->getOperand(0).getReg();
598   
599   // DF walk on the DomForest
600   while (!worklist.empty()) {
601     DomForestNode* DFNode = worklist.back();
602     
603     visited.insert(DFNode);
604     
605     bool inserted = false;
606     for (DomForestNode::iterator CI = DFNode->begin(), CE = DFNode->end();
607          CI != CE; ++CI) {
608       DomForestNode* child = *CI;   
609       
610       // If the current node is live-out of the defining block of one of its
611       // children, insert a copy for it.  NOTE: The paper actually calls for
612       // a more elaborate heuristic for determining whether to insert copies
613       // for the child or the parent.  In the interest of simplicity, we're
614       // just always choosing the parent.
615       if (isLiveOut(DFNode->getReg(),
616           MRI.getVRegDef(child->getReg())->getParent(), LI)) {
617         // Insert copies for parent
618         for (int i = Inst->getNumOperands() - 1; i >= 2; i-=2) {
619           if (Inst->getOperand(i-1).getReg() == DFNode->getReg()) {
620             unsigned SrcReg = DFNode->getReg();
621             MachineBasicBlock* From = Inst->getOperand(i).getMBB();
622             
623             Waiting[From].insert(std::make_pair(SrcReg, DestReg));
624             UsedByAnother.insert(SrcReg);
625             
626             PHIUnion.erase(SrcReg);
627           }
628         }
629       
630       // If a node is live-in to the defining block of one of its children, but
631       // not live-out, then we need to scan that block for local interferences.
632       } else if (isLiveIn(DFNode->getReg(),
633                           MRI.getVRegDef(child->getReg())->getParent(), LI) ||
634                  MRI.getVRegDef(DFNode->getReg())->getParent() ==
635                                  MRI.getVRegDef(child->getReg())->getParent()) {
636         // Add (p, c) to possible local interferences
637         locals.push_back(std::make_pair(DFNode->getReg(), child->getReg()));
638       }
639       
640       if (!visited.count(child)) {
641         worklist.push_back(child);
642         inserted = true;
643       }
644     }
645     
646     if (!inserted) worklist.pop_back();
647   }
648 }
649
650 /// ScheduleCopies - Insert copies into predecessor blocks, scheduling
651 /// them properly so as to avoid the 'lost copy' and the 'virtual swap'
652 /// problems.
653 ///
654 /// Based on "Practical Improvements to the Construction and Destruction
655 /// of Static Single Assignment Form" by Briggs, et al.
656 void StrongPHIElimination::ScheduleCopies(MachineBasicBlock* MBB,
657                                           std::set<unsigned>& pushed) {
658   // FIXME: This function needs to update LiveIntervals
659   std::multimap<unsigned, unsigned>& copy_set= Waiting[MBB];
660   
661   std::multimap<unsigned, unsigned> worklist;
662   std::map<unsigned, unsigned> map;
663   
664   // Setup worklist of initial copies
665   for (std::multimap<unsigned, unsigned>::iterator I = copy_set.begin(),
666        E = copy_set.end(); I != E; ) {
667     map.insert(std::make_pair(I->first, I->first));
668     map.insert(std::make_pair(I->second, I->second));
669          
670     if (!UsedByAnother.count(I->second)) {
671       worklist.insert(*I);
672       
673       // Avoid iterator invalidation
674       std::multimap<unsigned, unsigned>::iterator OI = I;
675       ++I;
676       copy_set.erase(OI);
677     } else {
678       ++I;
679     }
680   }
681   
682   LiveIntervals& LI = getAnalysis<LiveIntervals>();
683   MachineFunction* MF = MBB->getParent();
684   MachineRegisterInfo& MRI = MF->getRegInfo();
685   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
686   
687   SmallVector<std::pair<unsigned, MachineInstr*>, 4> InsertedPHIDests;
688   
689   // Iterate over the worklist, inserting copies
690   while (!worklist.empty() || !copy_set.empty()) {
691     while (!worklist.empty()) {
692       std::multimap<unsigned, unsigned>::iterator WI = worklist.begin();
693       std::pair<unsigned, unsigned> curr = *WI;
694       worklist.erase(WI);
695       
696       const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first);
697       
698       if (isLiveOut(curr.second, MBB, LI)) {
699         // Create a temporary
700         unsigned t = MF->getRegInfo().createVirtualRegister(RC);
701         
702         // Insert copy from curr.second to a temporary at
703         // the Phi defining curr.second
704         MachineBasicBlock::iterator PI = MRI.getVRegDef(curr.second);
705         BuildMI(*PI->getParent(), PI, DebugLoc(), TII->get(TargetOpcode::COPY),
706                 t).addReg(curr.second);
707         DEBUG(dbgs() << "Inserted copy from " << curr.second << " to " << t
708                      << "\n");
709         
710         // Push temporary on Stacks
711         Stacks[curr.second].push_back(t);
712         
713         // Insert curr.second in pushed
714         pushed.insert(curr.second);
715         
716         // Create a live interval for this temporary
717         InsertedPHIDests.push_back(std::make_pair(t, --PI));
718       }
719       
720       // Insert copy from map[curr.first] to curr.second
721       BuildMI(*MBB, MBB->getFirstTerminator(), DebugLoc(),
722              TII->get(TargetOpcode::COPY), curr.second).addReg(map[curr.first]);
723       map[curr.first] = curr.second;
724       DEBUG(dbgs() << "Inserted copy from " << curr.first << " to "
725                    << curr.second << "\n");
726       
727       // Push this copy onto InsertedPHICopies so we can
728       // update LiveIntervals with it.
729       MachineBasicBlock::iterator MI = MBB->getFirstTerminator();
730       InsertedPHIDests.push_back(std::make_pair(curr.second, --MI));
731       
732       // If curr.first is a destination in copy_set...
733       for (std::multimap<unsigned, unsigned>::iterator I = copy_set.begin(),
734            E = copy_set.end(); I != E; )
735         if (curr.first == I->second) {
736           std::pair<unsigned, unsigned> temp = *I;
737           worklist.insert(temp);
738           
739           // Avoid iterator invalidation
740           std::multimap<unsigned, unsigned>::iterator OI = I;
741           ++I;
742           copy_set.erase(OI);
743           
744           break;
745         } else {
746           ++I;
747         }
748     }
749     
750     if (!copy_set.empty()) {
751       std::multimap<unsigned, unsigned>::iterator CI = copy_set.begin();
752       std::pair<unsigned, unsigned> curr = *CI;
753       worklist.insert(curr);
754       copy_set.erase(CI);
755       
756       LiveInterval& I = LI.getInterval(curr.second);
757       MachineBasicBlock::iterator term = MBB->getFirstTerminator();
758       SlotIndex endIdx = SlotIndex();
759       if (term != MBB->end())
760         endIdx = LI.getInstructionIndex(term);
761       else
762         endIdx = LI.getMBBEndIdx(MBB);
763       
764       if (I.liveAt(endIdx)) {
765         const TargetRegisterClass *RC =
766                                        MF->getRegInfo().getRegClass(curr.first);
767         
768         // Insert a copy from dest to a new temporary t at the end of b
769         unsigned t = MF->getRegInfo().createVirtualRegister(RC);
770         BuildMI(*MBB, MBB->getFirstTerminator(), DebugLoc(),
771                 TII->get(TargetOpcode::COPY), t).addReg(curr.second);
772         map[curr.second] = t;
773         
774         MachineBasicBlock::iterator TI = MBB->getFirstTerminator();
775         InsertedPHIDests.push_back(std::make_pair(t, --TI));
776       }
777     }
778   }
779   
780   // Renumber the instructions so that we can perform the index computations
781   // needed to create new live intervals.
782   LI.renumber();
783   
784   // For copies that we inserted at the ends of predecessors, we construct
785   // live intervals.  This is pretty easy, since we know that the destination
786   // register cannot have be in live at that point previously.  We just have
787   // to make sure that, for registers that serve as inputs to more than one
788   // PHI, we don't create multiple overlapping live intervals.
789   std::set<unsigned> RegHandled;
790   for (SmallVector<std::pair<unsigned, MachineInstr*>, 4>::iterator I =
791        InsertedPHIDests.begin(), E = InsertedPHIDests.end(); I != E; ++I) {
792     if (RegHandled.insert(I->first).second) {
793       LiveInterval& Int = LI.getOrCreateInterval(I->first);
794       SlotIndex instrIdx = LI.getInstructionIndex(I->second);
795       if (Int.liveAt(instrIdx.getDefIndex()))
796         Int.removeRange(instrIdx.getDefIndex(),
797                         LI.getMBBEndIdx(I->second->getParent()).getNextSlot(),
798                         true);
799       
800       LiveRange R = LI.addLiveRangeToEndOfBlock(I->first, I->second);
801       R.valno->setCopy(I->second);
802       R.valno->def = LI.getInstructionIndex(I->second).getDefIndex();
803     }
804   }
805 }
806
807 /// InsertCopies - insert copies into MBB and all of its successors
808 void StrongPHIElimination::InsertCopies(MachineDomTreeNode* MDTN,
809                                  SmallPtrSet<MachineBasicBlock*, 16>& visited) {
810   MachineBasicBlock* MBB = MDTN->getBlock();
811   visited.insert(MBB);
812   
813   std::set<unsigned> pushed;
814   
815   LiveIntervals& LI = getAnalysis<LiveIntervals>();
816   // Rewrite register uses from Stacks
817   for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
818       I != E; ++I) {
819     if (I->isPHI())
820       continue;
821     
822     for (unsigned i = 0; i < I->getNumOperands(); ++i)
823       if (I->getOperand(i).isReg() &&
824           Stacks[I->getOperand(i).getReg()].size()) {
825         // Remove the live range for the old vreg.
826         LiveInterval& OldInt = LI.getInterval(I->getOperand(i).getReg());
827         LiveInterval::iterator OldLR =
828           OldInt.FindLiveRangeContaining(LI.getInstructionIndex(I).getUseIndex());
829         if (OldLR != OldInt.end())
830           OldInt.removeRange(*OldLR, true);
831         
832         // Change the register
833         I->getOperand(i).setReg(Stacks[I->getOperand(i).getReg()].back());
834         
835         // Add a live range for the new vreg
836         LiveInterval& Int = LI.getInterval(I->getOperand(i).getReg());
837         VNInfo* FirstVN = *Int.vni_begin();
838         FirstVN->setHasPHIKill(false);
839         LiveRange LR (LI.getMBBStartIdx(I->getParent()),
840                       LI.getInstructionIndex(I).getUseIndex().getNextSlot(),
841                       FirstVN);
842         
843         Int.addRange(LR);
844       }
845   }    
846   
847   // Schedule the copies for this block
848   ScheduleCopies(MBB, pushed);
849   
850   // Recur down the dominator tree.
851   for (MachineDomTreeNode::iterator I = MDTN->begin(),
852        E = MDTN->end(); I != E; ++I)
853     if (!visited.count((*I)->getBlock()))
854       InsertCopies(*I, visited);
855   
856   // As we exit this block, pop the names we pushed while processing it
857   for (std::set<unsigned>::iterator I = pushed.begin(), 
858        E = pushed.end(); I != E; ++I)
859     Stacks[*I].pop_back();
860 }
861
862 bool StrongPHIElimination::mergeLiveIntervals(unsigned primary,
863                                               unsigned secondary) {
864   
865   LiveIntervals& LI = getAnalysis<LiveIntervals>();
866   LiveInterval& LHS = LI.getOrCreateInterval(primary);
867   LiveInterval& RHS = LI.getOrCreateInterval(secondary);
868   
869   LI.renumber();
870   
871   DenseMap<VNInfo*, VNInfo*> VNMap;
872   for (LiveInterval::iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
873     LiveRange R = *I;
874  
875     SlotIndex Start = R.start;
876     SlotIndex End = R.end;
877     if (LHS.getLiveRangeContaining(Start))
878       return false;
879     
880     if (LHS.getLiveRangeContaining(End))
881       return false;
882     
883     LiveInterval::iterator RI = std::upper_bound(LHS.begin(), LHS.end(), R);
884     if (RI != LHS.end() && RI->start < End)
885       return false;
886   }
887   
888   for (LiveInterval::iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
889     LiveRange R = *I;
890     VNInfo* OldVN = R.valno;
891     VNInfo*& NewVN = VNMap[OldVN];
892     if (!NewVN) {
893       NewVN = LHS.createValueCopy(OldVN, LI.getVNInfoAllocator());
894     }
895     
896     LiveRange LR (R.start, R.end, NewVN);
897     LHS.addRange(LR);
898   }
899   
900   LI.removeInterval(RHS.reg);
901   
902   return true;
903 }
904
905 bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
906   LiveIntervals& LI = getAnalysis<LiveIntervals>();
907   
908   // Compute DFS numbers of each block
909   computeDFS(Fn);
910   
911   // Determine which phi node operands need copies
912   for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
913     if (!I->empty() && I->begin()->isPHI())
914       processBlock(I);
915   
916   // Break interferences where two different phis want to coalesce
917   // in the same register.
918   std::set<unsigned> seen;
919   typedef std::map<unsigned, std::map<unsigned, MachineBasicBlock*> >
920           RenameSetType;
921   for (RenameSetType::iterator I = RenameSets.begin(), E = RenameSets.end();
922        I != E; ++I) {
923     for (std::map<unsigned, MachineBasicBlock*>::iterator
924          OI = I->second.begin(), OE = I->second.end(); OI != OE; ) {
925       if (!seen.count(OI->first)) {
926         seen.insert(OI->first);
927         ++OI;
928       } else {
929         Waiting[OI->second].insert(std::make_pair(OI->first, I->first));
930         unsigned reg = OI->first;
931         ++OI;
932         I->second.erase(reg);
933         DEBUG(dbgs() << "Removing Renaming: " << reg << " -> " << I->first
934                      << "\n");
935       }
936     }
937   }
938   
939   // Insert copies
940   // FIXME: This process should probably preserve LiveIntervals
941   SmallPtrSet<MachineBasicBlock*, 16> visited;
942   MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
943   InsertCopies(MDT.getRootNode(), visited);
944   
945   // Perform renaming
946   for (RenameSetType::iterator I = RenameSets.begin(), E = RenameSets.end();
947        I != E; ++I)
948     while (I->second.size()) {
949       std::map<unsigned, MachineBasicBlock*>::iterator SI = I->second.begin();
950       
951       DEBUG(dbgs() << "Renaming: " << SI->first << " -> " << I->first << "\n");
952       
953       if (SI->first != I->first) {
954         if (mergeLiveIntervals(I->first, SI->first)) {
955           Fn.getRegInfo().replaceRegWith(SI->first, I->first);
956       
957           if (RenameSets.count(SI->first)) {
958             I->second.insert(RenameSets[SI->first].begin(),
959                              RenameSets[SI->first].end());
960             RenameSets.erase(SI->first);
961           }
962         } else {
963           // Insert a last-minute copy if a conflict was detected.
964           const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
965           BuildMI(*SI->second, SI->second->getFirstTerminator(), DebugLoc(),
966                   TII->get(TargetOpcode::COPY), I->first).addReg(SI->first);
967           
968           LI.renumber();
969           
970           LiveInterval& Int = LI.getOrCreateInterval(I->first);
971           SlotIndex instrIdx =
972                      LI.getInstructionIndex(--SI->second->getFirstTerminator());
973           if (Int.liveAt(instrIdx.getDefIndex()))
974             Int.removeRange(instrIdx.getDefIndex(),
975                             LI.getMBBEndIdx(SI->second).getNextSlot(), true);
976
977           LiveRange R = LI.addLiveRangeToEndOfBlock(I->first,
978                                             --SI->second->getFirstTerminator());
979           R.valno->setCopy(--SI->second->getFirstTerminator());
980           R.valno->def = instrIdx.getDefIndex();
981           
982           DEBUG(dbgs() << "Renaming failed: " << SI->first << " -> "
983                        << I->first << "\n");
984         }
985       }
986       
987       LiveInterval& Int = LI.getOrCreateInterval(I->first);
988       const LiveRange* LR =
989                        Int.getLiveRangeContaining(LI.getMBBEndIdx(SI->second));
990       LR->valno->setHasPHIKill(true);
991       
992       I->second.erase(SI->first);
993     }
994   
995   // Remove PHIs
996   std::vector<MachineInstr*> phis;
997   for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
998     for (MachineBasicBlock::iterator BI = I->begin(), BE = I->end();
999          BI != BE; ++BI)
1000       if (BI->isPHI())
1001         phis.push_back(BI);
1002   }
1003   
1004   for (std::vector<MachineInstr*>::iterator I = phis.begin(), E = phis.end();
1005        I != E; ) {
1006     MachineInstr* PInstr = *(I++);
1007     
1008     // If this is a dead PHI node, then remove it from LiveIntervals.
1009     unsigned DestReg = PInstr->getOperand(0).getReg();
1010     LiveInterval& PI = LI.getInterval(DestReg);
1011     if (PInstr->registerDefIsDead(DestReg)) {
1012       if (PI.containsOneValue()) {
1013         LI.removeInterval(DestReg);
1014       } else {
1015         SlotIndex idx = LI.getInstructionIndex(PInstr).getDefIndex();
1016         PI.removeRange(*PI.getLiveRangeContaining(idx), true);
1017       }
1018     } else {
1019       // Trim live intervals of input registers.  They are no longer live into
1020       // this block if they died after the PHI.  If they lived after it, don't
1021       // trim them because they might have other legitimate uses.
1022       for (unsigned i = 1; i < PInstr->getNumOperands(); i += 2) {
1023         unsigned reg = PInstr->getOperand(i).getReg();
1024         
1025         MachineBasicBlock* MBB = PInstr->getOperand(i+1).getMBB();
1026         LiveInterval& InputI = LI.getInterval(reg);
1027         if (MBB != PInstr->getParent() &&
1028             InputI.liveAt(LI.getMBBStartIdx(PInstr->getParent())) &&
1029             InputI.expiredAt(LI.getInstructionIndex(PInstr).getNextIndex()))
1030           InputI.removeRange(LI.getMBBStartIdx(PInstr->getParent()),
1031                              LI.getInstructionIndex(PInstr),
1032                              true);
1033       }
1034       
1035       // If the PHI is not dead, then the valno defined by the PHI
1036       // now has an unknown def.
1037       SlotIndex idx = LI.getInstructionIndex(PInstr).getDefIndex();
1038       const LiveRange* PLR = PI.getLiveRangeContaining(idx);
1039       PLR->valno->setIsPHIDef(true);
1040       LiveRange R (LI.getMBBStartIdx(PInstr->getParent()),
1041                    PLR->start, PLR->valno);
1042       PI.addRange(R);
1043     }
1044     
1045     LI.RemoveMachineInstrFromMaps(PInstr);
1046     PInstr->eraseFromParent();
1047   }
1048   
1049   LI.renumber();
1050   
1051   return true;
1052 }