c5e9c343fc7c92a05b9f3ab077bacab209d8684d
[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 was developed by Owen Anderson and is distributed under
6 // the University of Illinois Open Source 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/LiveVariables.h"
25 #include "llvm/CodeGen/MachineDominators.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/Target/TargetInstrInfo.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/Support/Compiler.h"
32 using namespace llvm;
33
34
35 namespace {
36   struct VISIBILITY_HIDDEN StrongPHIElimination : public MachineFunctionPass {
37     static char ID; // Pass identification, replacement for typeid
38     StrongPHIElimination() : MachineFunctionPass((intptr_t)&ID) {}
39
40     bool runOnMachineFunction(MachineFunction &Fn);
41     
42     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43       AU.addPreserved<LiveVariables>();
44       AU.addPreservedID(PHIEliminationID);
45       AU.addRequired<MachineDominatorTree>();
46       AU.addRequired<LiveVariables>();
47       AU.setPreservesAll();
48       MachineFunctionPass::getAnalysisUsage(AU);
49     }
50     
51     virtual void releaseMemory() {
52       preorder.clear();
53       maxpreorder.clear();
54       
55       waiting.clear();
56     }
57
58   private:
59     struct DomForestNode {
60     private:
61       std::vector<DomForestNode*> children;
62       unsigned reg;
63       
64       void addChild(DomForestNode* DFN) { children.push_back(DFN); }
65       
66     public:
67       typedef std::vector<DomForestNode*>::iterator iterator;
68       
69       DomForestNode(unsigned r, DomForestNode* parent) : reg(r) {
70         if (parent)
71           parent->addChild(this);
72       }
73       
74       ~DomForestNode() {
75         for (iterator I = begin(), E = end(); I != E; ++I)
76           delete *I;
77       }
78       
79       inline unsigned getReg() { return reg; }
80       
81       inline DomForestNode::iterator begin() { return children.begin(); }
82       inline DomForestNode::iterator end() { return children.end(); }
83     };
84     
85     DenseMap<MachineBasicBlock*, unsigned> preorder;
86     DenseMap<MachineBasicBlock*, unsigned> maxpreorder;
87     
88     DenseMap<MachineBasicBlock*, std::vector<MachineInstr*> > waiting;
89     
90     
91     void computeDFS(MachineFunction& MF);
92     void processPHI(MachineInstr* P);
93     
94     std::vector<DomForestNode*> computeDomForest(std::set<unsigned>& instrs);
95     
96   };
97
98   char StrongPHIElimination::ID = 0;
99   RegisterPass<StrongPHIElimination> X("strong-phi-node-elimination",
100                   "Eliminate PHI nodes for register allocation, intelligently");
101 }
102
103 const PassInfo *llvm::StrongPHIEliminationID = X.getPassInfo();
104
105 /// computeDFS - Computes the DFS-in and DFS-out numbers of the dominator tree
106 /// of the given MachineFunction.  These numbers are then used in other parts
107 /// of the PHI elimination process.
108 void StrongPHIElimination::computeDFS(MachineFunction& MF) {
109   SmallPtrSet<MachineDomTreeNode*, 8> frontier;
110   SmallPtrSet<MachineDomTreeNode*, 8> visited;
111   
112   unsigned time = 0;
113   
114   MachineDominatorTree& DT = getAnalysis<MachineDominatorTree>();
115   
116   MachineDomTreeNode* node = DT.getRootNode();
117   
118   std::vector<MachineDomTreeNode*> worklist;
119   worklist.push_back(node);
120   
121   while (!worklist.empty()) {
122     MachineDomTreeNode* currNode = worklist.back();
123     
124     if (!frontier.count(currNode)) {
125       frontier.insert(currNode);
126       ++time;
127       preorder.insert(std::make_pair(currNode->getBlock(), time));
128     }
129     
130     bool inserted = false;
131     for (MachineDomTreeNode::iterator I = node->begin(), E = node->end();
132          I != E; ++I)
133       if (!frontier.count(*I) && !visited.count(*I)) {
134         worklist.push_back(*I);
135         inserted = true;
136         break;
137       }
138     
139     if (!inserted) {
140       frontier.erase(currNode);
141       visited.insert(currNode);
142       maxpreorder.insert(std::make_pair(currNode->getBlock(), time));
143       
144       worklist.pop_back();
145     }
146   }
147 }
148
149 class PreorderSorter {
150 private:
151   DenseMap<MachineBasicBlock*, unsigned>& preorder;
152   LiveVariables& LV;
153   
154 public:
155   PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p,
156                 LiveVariables& L) : preorder(p), LV(L) { }
157   
158   bool operator()(unsigned A, unsigned B) {
159     if (A == B)
160       return false;
161     
162     MachineBasicBlock* ABlock = LV.getVarInfo(A).DefInst->getParent();
163     MachineBasicBlock* BBlock = LV.getVarInfo(A).DefInst->getParent();
164     
165     if (preorder[ABlock] < preorder[BBlock])
166       return true;
167     else if (preorder[ABlock] > preorder[BBlock])
168       return false;
169     
170     assert(0 && "Error sorting by dominance!");
171     return false;
172   }
173 };
174
175 std::vector<StrongPHIElimination::DomForestNode*>
176 StrongPHIElimination::computeDomForest(std::set<unsigned>& regs) {
177   LiveVariables& LV = getAnalysis<LiveVariables>();
178   
179   DomForestNode* VirtualRoot = new DomForestNode(0, 0);
180   maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL));
181   
182   std::vector<unsigned> worklist;
183   worklist.reserve(regs.size());
184   for (std::set<unsigned>::iterator I = regs.begin(), E = regs.end();
185        I != E; ++I)
186     worklist.push_back(*I);
187   
188   PreorderSorter PS(preorder, LV);
189   std::sort(worklist.begin(), worklist.end(), PS);
190   
191   DomForestNode* CurrentParent = VirtualRoot;
192   std::vector<DomForestNode*> stack;
193   stack.push_back(VirtualRoot);
194   
195   for (std::vector<unsigned>::iterator I = worklist.begin(), E = worklist.end();
196        I != E; ++I) {
197     unsigned pre = preorder[LV.getVarInfo(*I).DefInst->getParent()];
198     MachineBasicBlock* parentBlock =
199       LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent();
200     
201     while (pre > maxpreorder[parentBlock]) {
202       stack.pop_back();
203       CurrentParent = stack.back();
204       
205       parentBlock = LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent();
206     }
207     
208     DomForestNode* child = new DomForestNode(*I, CurrentParent);
209     stack.push_back(child);
210     CurrentParent = child;
211   }
212   
213   std::vector<DomForestNode*> ret;
214   ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end());
215   return ret;
216 }
217
218 void StrongPHIElimination::processPHI(MachineInstr* P) {
219   
220 }
221
222 bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
223   computeDFS(Fn);
224   
225   for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
226     for (MachineBasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;
227          ++BI) {
228       if (BI->getOpcode() == TargetInstrInfo::PHI)
229         processPHI(BI);
230       else
231         break;
232     }
233   }
234   
235   return false;
236 }