blob: f64186e8e8cdc17b48d6e1798b847bebd3cb9740 [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"
29#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
31#include "llvm/ADT/Statistic.h"
32#include "llvm/Support/Compiler.h"
33using namespace llvm;
34
35
36namespace {
37 struct VISIBILITY_HIDDEN StrongPHIElimination : public MachineFunctionPass {
38 static char ID; // Pass identification, replacement for typeid
39 StrongPHIElimination() : MachineFunctionPass((intptr_t)&ID) {}
40
Owen Andersona4ad2e72007-11-06 04:49:43 +000041 bool runOnMachineFunction(MachineFunction &Fn);
42
Owen Anderson0bda0e82007-10-31 03:37:57 +000043 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson83430bc2007-11-04 22:33:26 +000044 AU.addPreserved<LiveVariables>();
45 AU.addPreservedID(PHIEliminationID);
Owen Anderson0bda0e82007-10-31 03:37:57 +000046 AU.addRequired<MachineDominatorTree>();
Owen Andersona4ad2e72007-11-06 04:49:43 +000047 AU.addRequired<LiveVariables>();
48 AU.setPreservesAll();
Owen Anderson0bda0e82007-10-31 03:37:57 +000049 MachineFunctionPass::getAnalysisUsage(AU);
50 }
51
52 virtual void releaseMemory() {
53 preorder.clear();
54 maxpreorder.clear();
Owen Andersona4ad2e72007-11-06 04:49:43 +000055
56 waiting.clear();
Owen Anderson0bda0e82007-10-31 03:37:57 +000057 }
58
59 private:
Owen Anderson83430bc2007-11-04 22:33:26 +000060 struct DomForestNode {
61 private:
62 std::vector<DomForestNode*> children;
Owen Andersonee49b532007-11-06 05:22:43 +000063 unsigned reg;
Owen Anderson83430bc2007-11-04 22:33:26 +000064
65 void addChild(DomForestNode* DFN) { children.push_back(DFN); }
66
67 public:
68 typedef std::vector<DomForestNode*>::iterator iterator;
69
Owen Andersonee49b532007-11-06 05:22:43 +000070 DomForestNode(unsigned r, DomForestNode* parent) : reg(r) {
Owen Anderson83430bc2007-11-04 22:33:26 +000071 if (parent)
72 parent->addChild(this);
73 }
74
Owen Andersona4ad2e72007-11-06 04:49:43 +000075 ~DomForestNode() {
76 for (iterator I = begin(), E = end(); I != E; ++I)
77 delete *I;
78 }
Owen Anderson83430bc2007-11-04 22:33:26 +000079
Owen Andersonee49b532007-11-06 05:22:43 +000080 inline unsigned getReg() { return reg; }
Owen Andersona4ad2e72007-11-06 04:49:43 +000081
82 inline DomForestNode::iterator begin() { return children.begin(); }
83 inline DomForestNode::iterator end() { return children.end(); }
Owen Anderson83430bc2007-11-04 22:33:26 +000084 };
85
Owen Anderson0bda0e82007-10-31 03:37:57 +000086 DenseMap<MachineBasicBlock*, unsigned> preorder;
87 DenseMap<MachineBasicBlock*, unsigned> maxpreorder;
88
Owen Andersona4ad2e72007-11-06 04:49:43 +000089 DenseMap<MachineBasicBlock*, std::vector<MachineInstr*> > waiting;
90
91
Owen Anderson0bda0e82007-10-31 03:37:57 +000092 void computeDFS(MachineFunction& MF);
Owen Anderson60a877d2007-11-07 05:17:15 +000093 void processBlock(MachineBasicBlock* MBB);
Owen Anderson83430bc2007-11-04 22:33:26 +000094
Owen Andersonee49b532007-11-06 05:22:43 +000095 std::vector<DomForestNode*> computeDomForest(std::set<unsigned>& instrs);
Owen Anderson83430bc2007-11-04 22:33:26 +000096
Owen Anderson0bda0e82007-10-31 03:37:57 +000097 };
98
99 char StrongPHIElimination::ID = 0;
100 RegisterPass<StrongPHIElimination> X("strong-phi-node-elimination",
101 "Eliminate PHI nodes for register allocation, intelligently");
102}
103
104const PassInfo *llvm::StrongPHIEliminationID = X.getPassInfo();
105
106/// computeDFS - Computes the DFS-in and DFS-out numbers of the dominator tree
107/// of the given MachineFunction. These numbers are then used in other parts
108/// of the PHI elimination process.
109void StrongPHIElimination::computeDFS(MachineFunction& MF) {
110 SmallPtrSet<MachineDomTreeNode*, 8> frontier;
111 SmallPtrSet<MachineDomTreeNode*, 8> visited;
112
113 unsigned time = 0;
114
115 MachineDominatorTree& DT = getAnalysis<MachineDominatorTree>();
116
117 MachineDomTreeNode* node = DT.getRootNode();
118
119 std::vector<MachineDomTreeNode*> worklist;
120 worklist.push_back(node);
121
122 while (!worklist.empty()) {
123 MachineDomTreeNode* currNode = worklist.back();
124
125 if (!frontier.count(currNode)) {
126 frontier.insert(currNode);
127 ++time;
128 preorder.insert(std::make_pair(currNode->getBlock(), time));
129 }
130
131 bool inserted = false;
132 for (MachineDomTreeNode::iterator I = node->begin(), E = node->end();
133 I != E; ++I)
134 if (!frontier.count(*I) && !visited.count(*I)) {
135 worklist.push_back(*I);
136 inserted = true;
137 break;
138 }
139
140 if (!inserted) {
141 frontier.erase(currNode);
142 visited.insert(currNode);
143 maxpreorder.insert(std::make_pair(currNode->getBlock(), time));
144
145 worklist.pop_back();
146 }
147 }
Duncan Sands1bd32712007-10-31 08:49:24 +0000148}
Owen Anderson83430bc2007-11-04 22:33:26 +0000149
Owen Anderson8b96b9f2007-11-06 05:26:02 +0000150/// PreorderSorter - a helper class that is used to sort registers
151/// according to the preorder number of their defining blocks
Owen Anderson83430bc2007-11-04 22:33:26 +0000152class PreorderSorter {
153private:
154 DenseMap<MachineBasicBlock*, unsigned>& preorder;
Owen Andersonee49b532007-11-06 05:22:43 +0000155 LiveVariables& LV;
Owen Anderson83430bc2007-11-04 22:33:26 +0000156
157public:
Owen Andersonee49b532007-11-06 05:22:43 +0000158 PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p,
159 LiveVariables& L) : preorder(p), LV(L) { }
Owen Anderson83430bc2007-11-04 22:33:26 +0000160
Owen Andersonee49b532007-11-06 05:22:43 +0000161 bool operator()(unsigned A, unsigned B) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000162 if (A == B)
163 return false;
164
Owen Andersonee49b532007-11-06 05:22:43 +0000165 MachineBasicBlock* ABlock = LV.getVarInfo(A).DefInst->getParent();
166 MachineBasicBlock* BBlock = LV.getVarInfo(A).DefInst->getParent();
167
168 if (preorder[ABlock] < preorder[BBlock])
Owen Anderson83430bc2007-11-04 22:33:26 +0000169 return true;
Owen Andersonee49b532007-11-06 05:22:43 +0000170 else if (preorder[ABlock] > preorder[BBlock])
Owen Anderson83430bc2007-11-04 22:33:26 +0000171 return false;
172
Owen Andersonee49b532007-11-06 05:22:43 +0000173 assert(0 && "Error sorting by dominance!");
174 return false;
Owen Anderson83430bc2007-11-04 22:33:26 +0000175 }
176};
177
Owen Anderson8b96b9f2007-11-06 05:26:02 +0000178/// computeDomForest - compute the subforest of the DomTree corresponding
179/// to the defining blocks of the registers in question
Owen Anderson83430bc2007-11-04 22:33:26 +0000180std::vector<StrongPHIElimination::DomForestNode*>
Owen Andersonee49b532007-11-06 05:22:43 +0000181StrongPHIElimination::computeDomForest(std::set<unsigned>& regs) {
182 LiveVariables& LV = getAnalysis<LiveVariables>();
183
Owen Anderson83430bc2007-11-04 22:33:26 +0000184 DomForestNode* VirtualRoot = new DomForestNode(0, 0);
185 maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL));
186
Owen Andersonee49b532007-11-06 05:22:43 +0000187 std::vector<unsigned> worklist;
188 worklist.reserve(regs.size());
189 for (std::set<unsigned>::iterator I = regs.begin(), E = regs.end();
190 I != E; ++I)
Owen Anderson83430bc2007-11-04 22:33:26 +0000191 worklist.push_back(*I);
Owen Andersonee49b532007-11-06 05:22:43 +0000192
193 PreorderSorter PS(preorder, LV);
Owen Anderson83430bc2007-11-04 22:33:26 +0000194 std::sort(worklist.begin(), worklist.end(), PS);
195
196 DomForestNode* CurrentParent = VirtualRoot;
197 std::vector<DomForestNode*> stack;
198 stack.push_back(VirtualRoot);
199
Owen Andersonee49b532007-11-06 05:22:43 +0000200 for (std::vector<unsigned>::iterator I = worklist.begin(), E = worklist.end();
201 I != E; ++I) {
202 unsigned pre = preorder[LV.getVarInfo(*I).DefInst->getParent()];
203 MachineBasicBlock* parentBlock =
204 LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent();
205
206 while (pre > maxpreorder[parentBlock]) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000207 stack.pop_back();
208 CurrentParent = stack.back();
Owen Andersonee49b532007-11-06 05:22:43 +0000209
210 parentBlock = LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent();
Owen Anderson83430bc2007-11-04 22:33:26 +0000211 }
212
213 DomForestNode* child = new DomForestNode(*I, CurrentParent);
214 stack.push_back(child);
215 CurrentParent = child;
216 }
217
218 std::vector<DomForestNode*> ret;
219 ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end());
220 return ret;
221}
Owen Andersona4ad2e72007-11-06 04:49:43 +0000222
Owen Anderson60a877d2007-11-07 05:17:15 +0000223/// isLiveIn - helper method that determines, from a VarInfo, if a register
224/// is live into a block
225bool isLiveIn(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) {
226 if (V.AliveBlocks.test(MBB->getNumber()))
227 return true;
Owen Andersonee49b532007-11-06 05:22:43 +0000228
Owen Anderson14b3fb72007-11-08 01:32:45 +0000229 if (V.DefInst->getParent() != MBB &&
230 V.UsedBlocks.test(MBB->getNumber()))
231 return true;
Owen Anderson60a877d2007-11-07 05:17:15 +0000232
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) {
Owen Anderson14b3fb72007-11-08 01:32:45 +0000239 if (MBB == V.DefInst->getParent() ||
240 V.UsedBlocks.test(MBB->getNumber())) {
241 for (std::vector<MachineInstr*>::iterator I = V.Kills.begin(),
242 E = V.Kills.end(); I != E; ++I)
243 if ((*I)->getParent() == MBB)
244 return false;
245
Owen Anderson60a877d2007-11-07 05:17:15 +0000246 return true;
Owen Anderson14b3fb72007-11-08 01:32:45 +0000247 }
Owen Anderson60a877d2007-11-07 05:17:15 +0000248
249 return false;
250}
251
252/// processBlock - Eliminate PHIs in the given block
253void StrongPHIElimination::processBlock(MachineBasicBlock* MBB) {
254 LiveVariables& LV = getAnalysis<LiveVariables>();
255
256 // Holds names that have been added to a set in any PHI within this block
257 // before the current one.
258 std::set<unsigned> ProcessedNames;
259
260 MachineBasicBlock::iterator P = MBB->begin();
261 while (P->getOpcode() == TargetInstrInfo::PHI) {
262 LiveVariables::VarInfo& PHIInfo = LV.getVarInfo(P->getOperand(0).getReg());
263
264 // Hold the names that are currently in the candidate set.
265 std::set<unsigned> PHIUnion;
266 std::set<MachineBasicBlock*> UnionedBlocks;
267
268 for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
269 unsigned SrcReg = P->getOperand(i-1).getReg();
270 LiveVariables::VarInfo& SrcInfo = LV.getVarInfo(SrcReg);
271
272 if (isLiveIn(SrcInfo, P->getParent())) {
273 // add a copy from a_i to p in Waiting[From[a_i]]
274 } else if (isLiveOut(PHIInfo, SrcInfo.DefInst->getParent())) {
275 // add a copy to Waiting[From[a_i]]
276 } else if (PHIInfo.DefInst->getOpcode() == TargetInstrInfo::PHI &&
277 isLiveIn(PHIInfo, SrcInfo.DefInst->getParent())) {
278 // add a copy to Waiting[From[a_i]]
279 } else if (ProcessedNames.count(SrcReg)) {
280 // add a copy to Waiting[From[a_i]]
281 } else if (UnionedBlocks.count(SrcInfo.DefInst->getParent())) {
282 // add a copy to Waiting[From[a_i]]
283 } else {
284 PHIUnion.insert(SrcReg);
285 UnionedBlocks.insert(SrcInfo.DefInst->getParent());
286
287 // DO STUFF HERE
288
289 }
290
291 ProcessedNames.insert(PHIUnion.begin(), PHIUnion.end());
292 }
293
294 ++P;
295 }
Owen Andersonee49b532007-11-06 05:22:43 +0000296}
297
Owen Andersona4ad2e72007-11-06 04:49:43 +0000298bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
299 computeDFS(Fn);
300
Owen Anderson60a877d2007-11-07 05:17:15 +0000301 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
302 if (!I->empty() &&
303 I->begin()->getOpcode() == TargetInstrInfo::PHI)
304 processBlock(I);
Owen Andersona4ad2e72007-11-06 04:49:43 +0000305
306 return false;
307}