blob: d023b59277a7843100b334fc688a0dc2a5a06f70 [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 Andersonfe0c8822007-11-12 01:05:09 +000024#include "llvm/CodeGen/BreakCriticalMachineEdge.h"
Owen Anderson83430bc2007-11-04 22:33:26 +000025#include "llvm/CodeGen/LiveVariables.h"
Owen Anderson0bda0e82007-10-31 03:37:57 +000026#include "llvm/CodeGen/MachineDominators.h"
27#include "llvm/CodeGen/MachineFunctionPass.h"
28#include "llvm/CodeGen/MachineInstr.h"
Owen Anderson17b14182007-11-13 20:04:45 +000029#include "llvm/CodeGen/SSARegMap.h"
Owen Anderson0bda0e82007-10-31 03:37:57 +000030#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetMachine.h"
32#include "llvm/ADT/Statistic.h"
33#include "llvm/Support/Compiler.h"
34using namespace llvm;
35
36
37namespace {
38 struct VISIBILITY_HIDDEN StrongPHIElimination : public MachineFunctionPass {
39 static char ID; // Pass identification, replacement for typeid
40 StrongPHIElimination() : MachineFunctionPass((intptr_t)&ID) {}
41
Owen Andersona4ad2e72007-11-06 04:49:43 +000042 bool runOnMachineFunction(MachineFunction &Fn);
43
Owen Anderson0bda0e82007-10-31 03:37:57 +000044 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson83430bc2007-11-04 22:33:26 +000045 AU.addPreserved<LiveVariables>();
46 AU.addPreservedID(PHIEliminationID);
Owen Anderson0bda0e82007-10-31 03:37:57 +000047 AU.addRequired<MachineDominatorTree>();
Owen Andersona4ad2e72007-11-06 04:49:43 +000048 AU.addRequired<LiveVariables>();
49 AU.setPreservesAll();
Owen Anderson0bda0e82007-10-31 03:37:57 +000050 MachineFunctionPass::getAnalysisUsage(AU);
51 }
52
53 virtual void releaseMemory() {
54 preorder.clear();
55 maxpreorder.clear();
Owen Andersona4ad2e72007-11-06 04:49:43 +000056
57 waiting.clear();
Owen Anderson0bda0e82007-10-31 03:37:57 +000058 }
59
60 private:
Owen Anderson83430bc2007-11-04 22:33:26 +000061 struct DomForestNode {
62 private:
63 std::vector<DomForestNode*> children;
Owen Andersonee49b532007-11-06 05:22:43 +000064 unsigned reg;
Owen Anderson83430bc2007-11-04 22:33:26 +000065
66 void addChild(DomForestNode* DFN) { children.push_back(DFN); }
67
68 public:
69 typedef std::vector<DomForestNode*>::iterator iterator;
70
Owen Andersonee49b532007-11-06 05:22:43 +000071 DomForestNode(unsigned r, DomForestNode* parent) : reg(r) {
Owen Anderson83430bc2007-11-04 22:33:26 +000072 if (parent)
73 parent->addChild(this);
74 }
75
Owen Andersona4ad2e72007-11-06 04:49:43 +000076 ~DomForestNode() {
77 for (iterator I = begin(), E = end(); I != E; ++I)
78 delete *I;
79 }
Owen Anderson83430bc2007-11-04 22:33:26 +000080
Owen Andersonee49b532007-11-06 05:22:43 +000081 inline unsigned getReg() { return reg; }
Owen Andersona4ad2e72007-11-06 04:49:43 +000082
83 inline DomForestNode::iterator begin() { return children.begin(); }
84 inline DomForestNode::iterator end() { return children.end(); }
Owen Anderson83430bc2007-11-04 22:33:26 +000085 };
86
Owen Anderson0bda0e82007-10-31 03:37:57 +000087 DenseMap<MachineBasicBlock*, unsigned> preorder;
88 DenseMap<MachineBasicBlock*, unsigned> maxpreorder;
89
Owen Andersona4ad2e72007-11-06 04:49:43 +000090 DenseMap<MachineBasicBlock*, std::vector<MachineInstr*> > waiting;
91
92
Owen Anderson0bda0e82007-10-31 03:37:57 +000093 void computeDFS(MachineFunction& MF);
Owen Anderson60a877d2007-11-07 05:17:15 +000094 void processBlock(MachineBasicBlock* MBB);
Owen Anderson83430bc2007-11-04 22:33:26 +000095
Owen Andersonee49b532007-11-06 05:22:43 +000096 std::vector<DomForestNode*> computeDomForest(std::set<unsigned>& instrs);
Owen Anderson35e2dba2007-11-12 17:27:27 +000097 void breakCriticalEdges(MachineFunction &Fn);
Owen Anderson83430bc2007-11-04 22:33:26 +000098
Owen Anderson0bda0e82007-10-31 03:37:57 +000099 };
100
101 char StrongPHIElimination::ID = 0;
102 RegisterPass<StrongPHIElimination> X("strong-phi-node-elimination",
103 "Eliminate PHI nodes for register allocation, intelligently");
104}
105
106const PassInfo *llvm::StrongPHIEliminationID = X.getPassInfo();
107
108/// computeDFS - Computes the DFS-in and DFS-out numbers of the dominator tree
109/// of the given MachineFunction. These numbers are then used in other parts
110/// of the PHI elimination process.
111void StrongPHIElimination::computeDFS(MachineFunction& MF) {
112 SmallPtrSet<MachineDomTreeNode*, 8> frontier;
113 SmallPtrSet<MachineDomTreeNode*, 8> visited;
114
115 unsigned time = 0;
116
117 MachineDominatorTree& DT = getAnalysis<MachineDominatorTree>();
118
119 MachineDomTreeNode* node = DT.getRootNode();
120
121 std::vector<MachineDomTreeNode*> worklist;
122 worklist.push_back(node);
123
124 while (!worklist.empty()) {
125 MachineDomTreeNode* currNode = worklist.back();
126
127 if (!frontier.count(currNode)) {
128 frontier.insert(currNode);
129 ++time;
130 preorder.insert(std::make_pair(currNode->getBlock(), time));
131 }
132
133 bool inserted = false;
134 for (MachineDomTreeNode::iterator I = node->begin(), E = node->end();
135 I != E; ++I)
136 if (!frontier.count(*I) && !visited.count(*I)) {
137 worklist.push_back(*I);
138 inserted = true;
139 break;
140 }
141
142 if (!inserted) {
143 frontier.erase(currNode);
144 visited.insert(currNode);
145 maxpreorder.insert(std::make_pair(currNode->getBlock(), time));
146
147 worklist.pop_back();
148 }
149 }
Duncan Sands1bd32712007-10-31 08:49:24 +0000150}
Owen Anderson83430bc2007-11-04 22:33:26 +0000151
Owen Anderson8b96b9f2007-11-06 05:26:02 +0000152/// PreorderSorter - a helper class that is used to sort registers
153/// according to the preorder number of their defining blocks
Owen Anderson83430bc2007-11-04 22:33:26 +0000154class PreorderSorter {
155private:
156 DenseMap<MachineBasicBlock*, unsigned>& preorder;
Owen Andersonee49b532007-11-06 05:22:43 +0000157 LiveVariables& LV;
Owen Anderson83430bc2007-11-04 22:33:26 +0000158
159public:
Owen Andersonee49b532007-11-06 05:22:43 +0000160 PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p,
161 LiveVariables& L) : preorder(p), LV(L) { }
Owen Anderson83430bc2007-11-04 22:33:26 +0000162
Owen Andersonee49b532007-11-06 05:22:43 +0000163 bool operator()(unsigned A, unsigned B) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000164 if (A == B)
165 return false;
166
Owen Andersonee49b532007-11-06 05:22:43 +0000167 MachineBasicBlock* ABlock = LV.getVarInfo(A).DefInst->getParent();
168 MachineBasicBlock* BBlock = LV.getVarInfo(A).DefInst->getParent();
169
170 if (preorder[ABlock] < preorder[BBlock])
Owen Anderson83430bc2007-11-04 22:33:26 +0000171 return true;
Owen Andersonee49b532007-11-06 05:22:43 +0000172 else if (preorder[ABlock] > preorder[BBlock])
Owen Anderson83430bc2007-11-04 22:33:26 +0000173 return false;
174
Owen Andersonee49b532007-11-06 05:22:43 +0000175 assert(0 && "Error sorting by dominance!");
176 return false;
Owen Anderson83430bc2007-11-04 22:33:26 +0000177 }
178};
179
Owen Anderson8b96b9f2007-11-06 05:26:02 +0000180/// computeDomForest - compute the subforest of the DomTree corresponding
181/// to the defining blocks of the registers in question
Owen Anderson83430bc2007-11-04 22:33:26 +0000182std::vector<StrongPHIElimination::DomForestNode*>
Owen Andersonee49b532007-11-06 05:22:43 +0000183StrongPHIElimination::computeDomForest(std::set<unsigned>& regs) {
184 LiveVariables& LV = getAnalysis<LiveVariables>();
185
Owen Anderson83430bc2007-11-04 22:33:26 +0000186 DomForestNode* VirtualRoot = new DomForestNode(0, 0);
187 maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL));
188
Owen Andersonee49b532007-11-06 05:22:43 +0000189 std::vector<unsigned> worklist;
190 worklist.reserve(regs.size());
191 for (std::set<unsigned>::iterator I = regs.begin(), E = regs.end();
192 I != E; ++I)
Owen Anderson83430bc2007-11-04 22:33:26 +0000193 worklist.push_back(*I);
Owen Andersonee49b532007-11-06 05:22:43 +0000194
195 PreorderSorter PS(preorder, LV);
Owen Anderson83430bc2007-11-04 22:33:26 +0000196 std::sort(worklist.begin(), worklist.end(), PS);
197
198 DomForestNode* CurrentParent = VirtualRoot;
199 std::vector<DomForestNode*> stack;
200 stack.push_back(VirtualRoot);
201
Owen Andersonee49b532007-11-06 05:22:43 +0000202 for (std::vector<unsigned>::iterator I = worklist.begin(), E = worklist.end();
203 I != E; ++I) {
204 unsigned pre = preorder[LV.getVarInfo(*I).DefInst->getParent()];
205 MachineBasicBlock* parentBlock =
206 LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent();
207
208 while (pre > maxpreorder[parentBlock]) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000209 stack.pop_back();
210 CurrentParent = stack.back();
Owen Andersonee49b532007-11-06 05:22:43 +0000211
212 parentBlock = LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent();
Owen Anderson83430bc2007-11-04 22:33:26 +0000213 }
214
215 DomForestNode* child = new DomForestNode(*I, CurrentParent);
216 stack.push_back(child);
217 CurrentParent = child;
218 }
219
220 std::vector<DomForestNode*> ret;
221 ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end());
222 return ret;
223}
Owen Andersona4ad2e72007-11-06 04:49:43 +0000224
Owen Anderson60a877d2007-11-07 05:17:15 +0000225/// isLiveIn - helper method that determines, from a VarInfo, if a register
226/// is live into a block
227bool isLiveIn(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) {
228 if (V.AliveBlocks.test(MBB->getNumber()))
229 return true;
Owen Andersonee49b532007-11-06 05:22:43 +0000230
Owen Anderson14b3fb72007-11-08 01:32:45 +0000231 if (V.DefInst->getParent() != MBB &&
232 V.UsedBlocks.test(MBB->getNumber()))
233 return true;
Owen Anderson60a877d2007-11-07 05:17:15 +0000234
235 return false;
236}
237
238/// isLiveOut - help method that determines, from a VarInfo, if a register is
239/// live out of a block.
240bool isLiveOut(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) {
Owen Anderson14b3fb72007-11-08 01:32:45 +0000241 if (MBB == V.DefInst->getParent() ||
242 V.UsedBlocks.test(MBB->getNumber())) {
243 for (std::vector<MachineInstr*>::iterator I = V.Kills.begin(),
244 E = V.Kills.end(); I != E; ++I)
245 if ((*I)->getParent() == MBB)
246 return false;
247
Owen Anderson60a877d2007-11-07 05:17:15 +0000248 return true;
Owen Anderson14b3fb72007-11-08 01:32:45 +0000249 }
Owen Anderson60a877d2007-11-07 05:17:15 +0000250
251 return false;
252}
253
254/// processBlock - Eliminate PHIs in the given block
255void StrongPHIElimination::processBlock(MachineBasicBlock* MBB) {
256 LiveVariables& LV = getAnalysis<LiveVariables>();
257
258 // Holds names that have been added to a set in any PHI within this block
259 // before the current one.
260 std::set<unsigned> ProcessedNames;
261
262 MachineBasicBlock::iterator P = MBB->begin();
263 while (P->getOpcode() == TargetInstrInfo::PHI) {
264 LiveVariables::VarInfo& PHIInfo = LV.getVarInfo(P->getOperand(0).getReg());
265
266 // Hold the names that are currently in the candidate set.
267 std::set<unsigned> PHIUnion;
268 std::set<MachineBasicBlock*> UnionedBlocks;
269
270 for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
271 unsigned SrcReg = P->getOperand(i-1).getReg();
272 LiveVariables::VarInfo& SrcInfo = LV.getVarInfo(SrcReg);
273
274 if (isLiveIn(SrcInfo, P->getParent())) {
275 // add a copy from a_i to p in Waiting[From[a_i]]
276 } else if (isLiveOut(PHIInfo, SrcInfo.DefInst->getParent())) {
277 // add a copy to Waiting[From[a_i]]
278 } else if (PHIInfo.DefInst->getOpcode() == TargetInstrInfo::PHI &&
279 isLiveIn(PHIInfo, SrcInfo.DefInst->getParent())) {
280 // add a copy to Waiting[From[a_i]]
281 } else if (ProcessedNames.count(SrcReg)) {
282 // add a copy to Waiting[From[a_i]]
283 } else if (UnionedBlocks.count(SrcInfo.DefInst->getParent())) {
284 // add a copy to Waiting[From[a_i]]
285 } else {
286 PHIUnion.insert(SrcReg);
287 UnionedBlocks.insert(SrcInfo.DefInst->getParent());
Owen Anderson60a877d2007-11-07 05:17:15 +0000288 }
Owen Anderson60a877d2007-11-07 05:17:15 +0000289 }
290
Owen Anderson42f9e962007-11-13 20:13:24 +0000291 std::vector<StrongPHIElimination::DomForestNode*> DF =
292 computeDomForest(PHIUnion);
293
294 // DO STUFF HERE
295
296 ProcessedNames.insert(PHIUnion.begin(), PHIUnion.end());
Owen Anderson60a877d2007-11-07 05:17:15 +0000297 ++P;
298 }
Owen Andersonee49b532007-11-06 05:22:43 +0000299}
300
Owen Anderson17b14182007-11-13 20:04:45 +0000301/// breakCriticalEdges - Break critical edges coming into blocks with PHI
302/// nodes, preserving dominator and livevariable info.
Owen Anderson35e2dba2007-11-12 17:27:27 +0000303void StrongPHIElimination::breakCriticalEdges(MachineFunction &Fn) {
304 typedef std::pair<MachineBasicBlock*, MachineBasicBlock*> MBB_pair;
305
306 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
Owen Anderson17b14182007-11-13 20:04:45 +0000307 LiveVariables& LV = getAnalysis<LiveVariables>();
Owen Anderson35e2dba2007-11-12 17:27:27 +0000308
Owen Anderson17b14182007-11-13 20:04:45 +0000309 // Find critical edges
Owen Anderson35e2dba2007-11-12 17:27:27 +0000310 std::vector<MBB_pair> criticals;
311 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
312 if (!I->empty() &&
313 I->begin()->getOpcode() == TargetInstrInfo::PHI &&
314 I->pred_size() > 1)
315 for (MachineBasicBlock::pred_iterator PI = I->pred_begin(),
316 PE = I->pred_end(); PI != PE; ++PI)
317 if ((*PI)->succ_size() > 1)
318 criticals.push_back(std::make_pair(*PI, I));
319
320 for (std::vector<MBB_pair>::iterator I = criticals.begin(),
321 E = criticals.end(); I != E; ++I) {
Owen Anderson17b14182007-11-13 20:04:45 +0000322 // Split the edge
323 MachineBasicBlock* new_bb = SplitCriticalMachineEdge(I->first, I->second);
Owen Anderson35e2dba2007-11-12 17:27:27 +0000324
Owen Anderson17b14182007-11-13 20:04:45 +0000325 // Update dominators
Owen Anderson35e2dba2007-11-12 17:27:27 +0000326 MDT.splitBlock(I->first);
Owen Anderson17b14182007-11-13 20:04:45 +0000327
328 // Update livevariables
329 for (unsigned var = 1024; var < Fn.getSSARegMap()->getLastVirtReg(); ++var)
330 if (isLiveOut(LV.getVarInfo(var), I->first))
331 LV.getVarInfo(var).AliveBlocks.set(new_bb->getNumber());
Owen Anderson35e2dba2007-11-12 17:27:27 +0000332 }
333}
334
Owen Andersona4ad2e72007-11-06 04:49:43 +0000335bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
Owen Anderson35e2dba2007-11-12 17:27:27 +0000336 breakCriticalEdges(Fn);
Owen Andersona4ad2e72007-11-06 04:49:43 +0000337 computeDFS(Fn);
338
Owen Anderson60a877d2007-11-07 05:17:15 +0000339 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
340 if (!I->empty() &&
341 I->begin()->getOpcode() == TargetInstrInfo::PHI)
342 processBlock(I);
Owen Andersona4ad2e72007-11-06 04:49:43 +0000343
344 return false;
345}