blob: 200ae8f84e65178963c710af5cfe23fbd2197cf1 [file] [log] [blame]
Owen Anderson0bda0e82007-10-31 03:37:57 +00001//===- 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"
Owen Anderson83430bc2007-11-04 22:33:26 +000024#include "llvm/CodeGen/LiveVariables.h"
Owen Anderson0bda0e82007-10-31 03:37:57 +000025#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"
32using namespace llvm;
33
34
35namespace {
36 struct VISIBILITY_HIDDEN StrongPHIElimination : public MachineFunctionPass {
37 static char ID; // Pass identification, replacement for typeid
38 StrongPHIElimination() : MachineFunctionPass((intptr_t)&ID) {}
39
Owen Andersona4ad2e72007-11-06 04:49:43 +000040 bool runOnMachineFunction(MachineFunction &Fn);
41
Owen Anderson0bda0e82007-10-31 03:37:57 +000042 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson83430bc2007-11-04 22:33:26 +000043 AU.addPreserved<LiveVariables>();
44 AU.addPreservedID(PHIEliminationID);
Owen Anderson0bda0e82007-10-31 03:37:57 +000045 AU.addRequired<MachineDominatorTree>();
Owen Andersona4ad2e72007-11-06 04:49:43 +000046 AU.addRequired<LiveVariables>();
47 AU.setPreservesAll();
Owen Anderson0bda0e82007-10-31 03:37:57 +000048 MachineFunctionPass::getAnalysisUsage(AU);
49 }
50
51 virtual void releaseMemory() {
52 preorder.clear();
53 maxpreorder.clear();
Owen Andersona4ad2e72007-11-06 04:49:43 +000054
55 waiting.clear();
Owen Anderson0bda0e82007-10-31 03:37:57 +000056 }
57
58 private:
Owen Anderson83430bc2007-11-04 22:33:26 +000059 struct DomForestNode {
60 private:
61 std::vector<DomForestNode*> children;
Owen Andersonee49b532007-11-06 05:22:43 +000062 unsigned reg;
Owen Anderson83430bc2007-11-04 22:33:26 +000063
64 void addChild(DomForestNode* DFN) { children.push_back(DFN); }
65
66 public:
67 typedef std::vector<DomForestNode*>::iterator iterator;
68
Owen Andersonee49b532007-11-06 05:22:43 +000069 DomForestNode(unsigned r, DomForestNode* parent) : reg(r) {
Owen Anderson83430bc2007-11-04 22:33:26 +000070 if (parent)
71 parent->addChild(this);
72 }
73
Owen Andersona4ad2e72007-11-06 04:49:43 +000074 ~DomForestNode() {
75 for (iterator I = begin(), E = end(); I != E; ++I)
76 delete *I;
77 }
Owen Anderson83430bc2007-11-04 22:33:26 +000078
Owen Andersonee49b532007-11-06 05:22:43 +000079 inline unsigned getReg() { return reg; }
Owen Andersona4ad2e72007-11-06 04:49:43 +000080
81 inline DomForestNode::iterator begin() { return children.begin(); }
82 inline DomForestNode::iterator end() { return children.end(); }
Owen Anderson83430bc2007-11-04 22:33:26 +000083 };
84
Owen Anderson0bda0e82007-10-31 03:37:57 +000085 DenseMap<MachineBasicBlock*, unsigned> preorder;
86 DenseMap<MachineBasicBlock*, unsigned> maxpreorder;
87
Owen Andersona4ad2e72007-11-06 04:49:43 +000088 DenseMap<MachineBasicBlock*, std::vector<MachineInstr*> > waiting;
89
90
Owen Anderson0bda0e82007-10-31 03:37:57 +000091 void computeDFS(MachineFunction& MF);
Owen Anderson60a877d2007-11-07 05:17:15 +000092 void processBlock(MachineBasicBlock* MBB);
Owen Anderson83430bc2007-11-04 22:33:26 +000093
Owen Andersonee49b532007-11-06 05:22:43 +000094 std::vector<DomForestNode*> computeDomForest(std::set<unsigned>& instrs);
Owen Anderson83430bc2007-11-04 22:33:26 +000095
Owen Anderson0bda0e82007-10-31 03:37:57 +000096 };
97
98 char StrongPHIElimination::ID = 0;
99 RegisterPass<StrongPHIElimination> X("strong-phi-node-elimination",
100 "Eliminate PHI nodes for register allocation, intelligently");
101}
102
103const 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.
108void 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 }
Duncan Sands1bd32712007-10-31 08:49:24 +0000147}
Owen Anderson83430bc2007-11-04 22:33:26 +0000148
Owen Anderson8b96b9f2007-11-06 05:26:02 +0000149/// PreorderSorter - a helper class that is used to sort registers
150/// according to the preorder number of their defining blocks
Owen Anderson83430bc2007-11-04 22:33:26 +0000151class PreorderSorter {
152private:
153 DenseMap<MachineBasicBlock*, unsigned>& preorder;
Owen Andersonee49b532007-11-06 05:22:43 +0000154 LiveVariables& LV;
Owen Anderson83430bc2007-11-04 22:33:26 +0000155
156public:
Owen Andersonee49b532007-11-06 05:22:43 +0000157 PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p,
158 LiveVariables& L) : preorder(p), LV(L) { }
Owen Anderson83430bc2007-11-04 22:33:26 +0000159
Owen Andersonee49b532007-11-06 05:22:43 +0000160 bool operator()(unsigned A, unsigned B) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000161 if (A == B)
162 return false;
163
Owen Andersonee49b532007-11-06 05:22:43 +0000164 MachineBasicBlock* ABlock = LV.getVarInfo(A).DefInst->getParent();
165 MachineBasicBlock* BBlock = LV.getVarInfo(A).DefInst->getParent();
166
167 if (preorder[ABlock] < preorder[BBlock])
Owen Anderson83430bc2007-11-04 22:33:26 +0000168 return true;
Owen Andersonee49b532007-11-06 05:22:43 +0000169 else if (preorder[ABlock] > preorder[BBlock])
Owen Anderson83430bc2007-11-04 22:33:26 +0000170 return false;
171
Owen Andersonee49b532007-11-06 05:22:43 +0000172 assert(0 && "Error sorting by dominance!");
173 return false;
Owen Anderson83430bc2007-11-04 22:33:26 +0000174 }
175};
176
Owen Anderson8b96b9f2007-11-06 05:26:02 +0000177/// computeDomForest - compute the subforest of the DomTree corresponding
178/// to the defining blocks of the registers in question
Owen Anderson83430bc2007-11-04 22:33:26 +0000179std::vector<StrongPHIElimination::DomForestNode*>
Owen Andersonee49b532007-11-06 05:22:43 +0000180StrongPHIElimination::computeDomForest(std::set<unsigned>& regs) {
181 LiveVariables& LV = getAnalysis<LiveVariables>();
182
Owen Anderson83430bc2007-11-04 22:33:26 +0000183 DomForestNode* VirtualRoot = new DomForestNode(0, 0);
184 maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL));
185
Owen Andersonee49b532007-11-06 05:22:43 +0000186 std::vector<unsigned> worklist;
187 worklist.reserve(regs.size());
188 for (std::set<unsigned>::iterator I = regs.begin(), E = regs.end();
189 I != E; ++I)
Owen Anderson83430bc2007-11-04 22:33:26 +0000190 worklist.push_back(*I);
Owen Andersonee49b532007-11-06 05:22:43 +0000191
192 PreorderSorter PS(preorder, LV);
Owen Anderson83430bc2007-11-04 22:33:26 +0000193 std::sort(worklist.begin(), worklist.end(), PS);
194
195 DomForestNode* CurrentParent = VirtualRoot;
196 std::vector<DomForestNode*> stack;
197 stack.push_back(VirtualRoot);
198
Owen Andersonee49b532007-11-06 05:22:43 +0000199 for (std::vector<unsigned>::iterator I = worklist.begin(), E = worklist.end();
200 I != E; ++I) {
201 unsigned pre = preorder[LV.getVarInfo(*I).DefInst->getParent()];
202 MachineBasicBlock* parentBlock =
203 LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent();
204
205 while (pre > maxpreorder[parentBlock]) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000206 stack.pop_back();
207 CurrentParent = stack.back();
Owen Andersonee49b532007-11-06 05:22:43 +0000208
209 parentBlock = LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent();
Owen Anderson83430bc2007-11-04 22:33:26 +0000210 }
211
212 DomForestNode* child = new DomForestNode(*I, CurrentParent);
213 stack.push_back(child);
214 CurrentParent = child;
215 }
216
217 std::vector<DomForestNode*> ret;
218 ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end());
219 return ret;
220}
Owen Andersona4ad2e72007-11-06 04:49:43 +0000221
Owen Anderson60a877d2007-11-07 05:17:15 +0000222/// isLiveIn - helper method that determines, from a VarInfo, if a register
223/// is live into a block
224bool isLiveIn(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) {
225 if (V.AliveBlocks.test(MBB->getNumber()))
226 return true;
Owen Andersonee49b532007-11-06 05:22:43 +0000227
Owen Anderson60a877d2007-11-07 05:17:15 +0000228 for (std::vector<MachineInstr*>::iterator I = V.Kills.begin(),
229 E = V.Kills.end(); I != E; ++I)
230 if ((*I)->getParent() == MBB)
231 return true;
232
233 return false;
234}
235
236/// isLiveOut - help method that determines, from a VarInfo, if a register is
237/// live out of a block.
238bool isLiveOut(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) {
239 if (V.AliveBlocks.test(MBB->getNumber()))
240 return true;
241
242 if (V.DefInst->getParent() == MBB)
243 return true;
244
245 return false;
246}
247
248/// processBlock - Eliminate PHIs in the given block
249void StrongPHIElimination::processBlock(MachineBasicBlock* MBB) {
250 LiveVariables& LV = getAnalysis<LiveVariables>();
251
252 // Holds names that have been added to a set in any PHI within this block
253 // before the current one.
254 std::set<unsigned> ProcessedNames;
255
256 MachineBasicBlock::iterator P = MBB->begin();
257 while (P->getOpcode() == TargetInstrInfo::PHI) {
258 LiveVariables::VarInfo& PHIInfo = LV.getVarInfo(P->getOperand(0).getReg());
259
260 // Hold the names that are currently in the candidate set.
261 std::set<unsigned> PHIUnion;
262 std::set<MachineBasicBlock*> UnionedBlocks;
263
264 for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
265 unsigned SrcReg = P->getOperand(i-1).getReg();
266 LiveVariables::VarInfo& SrcInfo = LV.getVarInfo(SrcReg);
267
268 if (isLiveIn(SrcInfo, P->getParent())) {
269 // add a copy from a_i to p in Waiting[From[a_i]]
270 } else if (isLiveOut(PHIInfo, SrcInfo.DefInst->getParent())) {
271 // add a copy to Waiting[From[a_i]]
272 } else if (PHIInfo.DefInst->getOpcode() == TargetInstrInfo::PHI &&
273 isLiveIn(PHIInfo, SrcInfo.DefInst->getParent())) {
274 // add a copy to Waiting[From[a_i]]
275 } else if (ProcessedNames.count(SrcReg)) {
276 // add a copy to Waiting[From[a_i]]
277 } else if (UnionedBlocks.count(SrcInfo.DefInst->getParent())) {
278 // add a copy to Waiting[From[a_i]]
279 } else {
280 PHIUnion.insert(SrcReg);
281 UnionedBlocks.insert(SrcInfo.DefInst->getParent());
282
283 // DO STUFF HERE
284
285 }
286
287 ProcessedNames.insert(PHIUnion.begin(), PHIUnion.end());
288 }
289
290 ++P;
291 }
Owen Andersonee49b532007-11-06 05:22:43 +0000292}
293
Owen Andersona4ad2e72007-11-06 04:49:43 +0000294bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
295 computeDFS(Fn);
296
Owen Anderson60a877d2007-11-07 05:17:15 +0000297 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
298 if (!I->empty() &&
299 I->begin()->getOpcode() == TargetInstrInfo::PHI)
300 processBlock(I);
Owen Andersona4ad2e72007-11-06 04:49:43 +0000301
302 return false;
303}