1 //===- StrongPhiElimination.cpp - Eliminate PHI nodes by inserting copies -===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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:
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
20 //===----------------------------------------------------------------------===//
22 #define DEBUG_TYPE "strongphielim"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/CodeGen/BreakCriticalMachineEdge.h"
25 #include "llvm/CodeGen/LiveVariables.h"
26 #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"
37 struct VISIBILITY_HIDDEN StrongPHIElimination : public MachineFunctionPass {
38 static char ID; // Pass identification, replacement for typeid
39 StrongPHIElimination() : MachineFunctionPass((intptr_t)&ID) {}
41 bool runOnMachineFunction(MachineFunction &Fn);
43 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.addPreserved<LiveVariables>();
45 AU.addPreservedID(PHIEliminationID);
46 AU.addRequired<MachineDominatorTree>();
47 AU.addRequired<LiveVariables>();
49 MachineFunctionPass::getAnalysisUsage(AU);
52 virtual void releaseMemory() {
60 struct DomForestNode {
62 std::vector<DomForestNode*> children;
65 void addChild(DomForestNode* DFN) { children.push_back(DFN); }
68 typedef std::vector<DomForestNode*>::iterator iterator;
70 DomForestNode(unsigned r, DomForestNode* parent) : reg(r) {
72 parent->addChild(this);
76 for (iterator I = begin(), E = end(); I != E; ++I)
80 inline unsigned getReg() { return reg; }
82 inline DomForestNode::iterator begin() { return children.begin(); }
83 inline DomForestNode::iterator end() { return children.end(); }
86 DenseMap<MachineBasicBlock*, unsigned> preorder;
87 DenseMap<MachineBasicBlock*, unsigned> maxpreorder;
89 DenseMap<MachineBasicBlock*, std::vector<MachineInstr*> > waiting;
92 void computeDFS(MachineFunction& MF);
93 void processBlock(MachineBasicBlock* MBB);
95 std::vector<DomForestNode*> computeDomForest(std::set<unsigned>& instrs);
99 char StrongPHIElimination::ID = 0;
100 RegisterPass<StrongPHIElimination> X("strong-phi-node-elimination",
101 "Eliminate PHI nodes for register allocation, intelligently");
104 const PassInfo *llvm::StrongPHIEliminationID = X.getPassInfo();
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.
109 void StrongPHIElimination::computeDFS(MachineFunction& MF) {
110 SmallPtrSet<MachineDomTreeNode*, 8> frontier;
111 SmallPtrSet<MachineDomTreeNode*, 8> visited;
115 MachineDominatorTree& DT = getAnalysis<MachineDominatorTree>();
117 MachineDomTreeNode* node = DT.getRootNode();
119 std::vector<MachineDomTreeNode*> worklist;
120 worklist.push_back(node);
122 while (!worklist.empty()) {
123 MachineDomTreeNode* currNode = worklist.back();
125 if (!frontier.count(currNode)) {
126 frontier.insert(currNode);
128 preorder.insert(std::make_pair(currNode->getBlock(), time));
131 bool inserted = false;
132 for (MachineDomTreeNode::iterator I = node->begin(), E = node->end();
134 if (!frontier.count(*I) && !visited.count(*I)) {
135 worklist.push_back(*I);
141 frontier.erase(currNode);
142 visited.insert(currNode);
143 maxpreorder.insert(std::make_pair(currNode->getBlock(), time));
150 /// PreorderSorter - a helper class that is used to sort registers
151 /// according to the preorder number of their defining blocks
152 class PreorderSorter {
154 DenseMap<MachineBasicBlock*, unsigned>& preorder;
158 PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p,
159 LiveVariables& L) : preorder(p), LV(L) { }
161 bool operator()(unsigned A, unsigned B) {
165 MachineBasicBlock* ABlock = LV.getVarInfo(A).DefInst->getParent();
166 MachineBasicBlock* BBlock = LV.getVarInfo(A).DefInst->getParent();
168 if (preorder[ABlock] < preorder[BBlock])
170 else if (preorder[ABlock] > preorder[BBlock])
173 assert(0 && "Error sorting by dominance!");
178 /// computeDomForest - compute the subforest of the DomTree corresponding
179 /// to the defining blocks of the registers in question
180 std::vector<StrongPHIElimination::DomForestNode*>
181 StrongPHIElimination::computeDomForest(std::set<unsigned>& regs) {
182 LiveVariables& LV = getAnalysis<LiveVariables>();
184 DomForestNode* VirtualRoot = new DomForestNode(0, 0);
185 maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL));
187 std::vector<unsigned> worklist;
188 worklist.reserve(regs.size());
189 for (std::set<unsigned>::iterator I = regs.begin(), E = regs.end();
191 worklist.push_back(*I);
193 PreorderSorter PS(preorder, LV);
194 std::sort(worklist.begin(), worklist.end(), PS);
196 DomForestNode* CurrentParent = VirtualRoot;
197 std::vector<DomForestNode*> stack;
198 stack.push_back(VirtualRoot);
200 for (std::vector<unsigned>::iterator I = worklist.begin(), E = worklist.end();
202 unsigned pre = preorder[LV.getVarInfo(*I).DefInst->getParent()];
203 MachineBasicBlock* parentBlock =
204 LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent();
206 while (pre > maxpreorder[parentBlock]) {
208 CurrentParent = stack.back();
210 parentBlock = LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent();
213 DomForestNode* child = new DomForestNode(*I, CurrentParent);
214 stack.push_back(child);
215 CurrentParent = child;
218 std::vector<DomForestNode*> ret;
219 ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end());
223 /// isLiveIn - helper method that determines, from a VarInfo, if a register
224 /// is live into a block
225 bool isLiveIn(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) {
226 if (V.AliveBlocks.test(MBB->getNumber()))
229 if (V.DefInst->getParent() != MBB &&
230 V.UsedBlocks.test(MBB->getNumber()))
236 /// isLiveOut - help method that determines, from a VarInfo, if a register is
237 /// live out of a block.
238 bool isLiveOut(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) {
239 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)
252 /// processBlock - Eliminate PHIs in the given block
253 void StrongPHIElimination::processBlock(MachineBasicBlock* MBB) {
254 LiveVariables& LV = getAnalysis<LiveVariables>();
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;
260 MachineBasicBlock::iterator P = MBB->begin();
261 while (P->getOpcode() == TargetInstrInfo::PHI) {
262 LiveVariables::VarInfo& PHIInfo = LV.getVarInfo(P->getOperand(0).getReg());
264 // Hold the names that are currently in the candidate set.
265 std::set<unsigned> PHIUnion;
266 std::set<MachineBasicBlock*> UnionedBlocks;
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);
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]]
284 PHIUnion.insert(SrcReg);
285 UnionedBlocks.insert(SrcInfo.DefInst->getParent());
291 ProcessedNames.insert(PHIUnion.begin(), PHIUnion.end());
298 bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
301 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
303 I->begin()->getOpcode() == TargetInstrInfo::PHI)