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/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"
36 struct VISIBILITY_HIDDEN StrongPHIElimination : public MachineFunctionPass {
37 static char ID; // Pass identification, replacement for typeid
38 StrongPHIElimination() : MachineFunctionPass((intptr_t)&ID) {}
40 bool runOnMachineFunction(MachineFunction &Fn);
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addPreserved<LiveVariables>();
44 AU.addPreservedID(PHIEliminationID);
45 AU.addRequired<MachineDominatorTree>();
46 AU.addRequired<LiveVariables>();
48 MachineFunctionPass::getAnalysisUsage(AU);
51 virtual void releaseMemory() {
59 struct DomForestNode {
61 std::vector<DomForestNode*> children;
64 void addChild(DomForestNode* DFN) { children.push_back(DFN); }
67 typedef std::vector<DomForestNode*>::iterator iterator;
69 DomForestNode(unsigned r, DomForestNode* parent) : reg(r) {
71 parent->addChild(this);
75 for (iterator I = begin(), E = end(); I != E; ++I)
79 inline unsigned getReg() { return reg; }
81 inline DomForestNode::iterator begin() { return children.begin(); }
82 inline DomForestNode::iterator end() { return children.end(); }
85 DenseMap<MachineBasicBlock*, unsigned> preorder;
86 DenseMap<MachineBasicBlock*, unsigned> maxpreorder;
88 DenseMap<MachineBasicBlock*, std::vector<MachineInstr*> > waiting;
91 void computeDFS(MachineFunction& MF);
92 void processBlock(MachineBasicBlock* MBB);
94 std::vector<DomForestNode*> computeDomForest(std::set<unsigned>& instrs);
98 char StrongPHIElimination::ID = 0;
99 RegisterPass<StrongPHIElimination> X("strong-phi-node-elimination",
100 "Eliminate PHI nodes for register allocation, intelligently");
103 const PassInfo *llvm::StrongPHIEliminationID = X.getPassInfo();
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;
114 MachineDominatorTree& DT = getAnalysis<MachineDominatorTree>();
116 MachineDomTreeNode* node = DT.getRootNode();
118 std::vector<MachineDomTreeNode*> worklist;
119 worklist.push_back(node);
121 while (!worklist.empty()) {
122 MachineDomTreeNode* currNode = worklist.back();
124 if (!frontier.count(currNode)) {
125 frontier.insert(currNode);
127 preorder.insert(std::make_pair(currNode->getBlock(), time));
130 bool inserted = false;
131 for (MachineDomTreeNode::iterator I = node->begin(), E = node->end();
133 if (!frontier.count(*I) && !visited.count(*I)) {
134 worklist.push_back(*I);
140 frontier.erase(currNode);
141 visited.insert(currNode);
142 maxpreorder.insert(std::make_pair(currNode->getBlock(), time));
149 /// PreorderSorter - a helper class that is used to sort registers
150 /// according to the preorder number of their defining blocks
151 class PreorderSorter {
153 DenseMap<MachineBasicBlock*, unsigned>& preorder;
157 PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p,
158 LiveVariables& L) : preorder(p), LV(L) { }
160 bool operator()(unsigned A, unsigned B) {
164 MachineBasicBlock* ABlock = LV.getVarInfo(A).DefInst->getParent();
165 MachineBasicBlock* BBlock = LV.getVarInfo(A).DefInst->getParent();
167 if (preorder[ABlock] < preorder[BBlock])
169 else if (preorder[ABlock] > preorder[BBlock])
172 assert(0 && "Error sorting by dominance!");
177 /// computeDomForest - compute the subforest of the DomTree corresponding
178 /// to the defining blocks of the registers in question
179 std::vector<StrongPHIElimination::DomForestNode*>
180 StrongPHIElimination::computeDomForest(std::set<unsigned>& regs) {
181 LiveVariables& LV = getAnalysis<LiveVariables>();
183 DomForestNode* VirtualRoot = new DomForestNode(0, 0);
184 maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL));
186 std::vector<unsigned> worklist;
187 worklist.reserve(regs.size());
188 for (std::set<unsigned>::iterator I = regs.begin(), E = regs.end();
190 worklist.push_back(*I);
192 PreorderSorter PS(preorder, LV);
193 std::sort(worklist.begin(), worklist.end(), PS);
195 DomForestNode* CurrentParent = VirtualRoot;
196 std::vector<DomForestNode*> stack;
197 stack.push_back(VirtualRoot);
199 for (std::vector<unsigned>::iterator I = worklist.begin(), E = worklist.end();
201 unsigned pre = preorder[LV.getVarInfo(*I).DefInst->getParent()];
202 MachineBasicBlock* parentBlock =
203 LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent();
205 while (pre > maxpreorder[parentBlock]) {
207 CurrentParent = stack.back();
209 parentBlock = LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent();
212 DomForestNode* child = new DomForestNode(*I, CurrentParent);
213 stack.push_back(child);
214 CurrentParent = child;
217 std::vector<DomForestNode*> ret;
218 ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end());
222 /// isLiveIn - helper method that determines, from a VarInfo, if a register
223 /// is live into a block
224 bool isLiveIn(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) {
225 if (V.AliveBlocks.test(MBB->getNumber()))
228 if (V.DefInst->getParent() != MBB &&
229 V.UsedBlocks.test(MBB->getNumber()))
235 /// isLiveOut - help method that determines, from a VarInfo, if a register is
236 /// live out of a block.
237 bool isLiveOut(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) {
238 if (MBB == V.DefInst->getParent() ||
239 V.UsedBlocks.test(MBB->getNumber())) {
240 for (std::vector<MachineInstr*>::iterator I = V.Kills.begin(),
241 E = V.Kills.end(); I != E; ++I)
242 if ((*I)->getParent() == MBB)
251 /// processBlock - Eliminate PHIs in the given block
252 void StrongPHIElimination::processBlock(MachineBasicBlock* MBB) {
253 LiveVariables& LV = getAnalysis<LiveVariables>();
255 // Holds names that have been added to a set in any PHI within this block
256 // before the current one.
257 std::set<unsigned> ProcessedNames;
259 MachineBasicBlock::iterator P = MBB->begin();
260 while (P->getOpcode() == TargetInstrInfo::PHI) {
261 LiveVariables::VarInfo& PHIInfo = LV.getVarInfo(P->getOperand(0).getReg());
263 // Hold the names that are currently in the candidate set.
264 std::set<unsigned> PHIUnion;
265 std::set<MachineBasicBlock*> UnionedBlocks;
267 for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
268 unsigned SrcReg = P->getOperand(i-1).getReg();
269 LiveVariables::VarInfo& SrcInfo = LV.getVarInfo(SrcReg);
271 if (isLiveIn(SrcInfo, P->getParent())) {
272 // add a copy from a_i to p in Waiting[From[a_i]]
273 } else if (isLiveOut(PHIInfo, SrcInfo.DefInst->getParent())) {
274 // add a copy to Waiting[From[a_i]]
275 } else if (PHIInfo.DefInst->getOpcode() == TargetInstrInfo::PHI &&
276 isLiveIn(PHIInfo, SrcInfo.DefInst->getParent())) {
277 // add a copy to Waiting[From[a_i]]
278 } else if (ProcessedNames.count(SrcReg)) {
279 // add a copy to Waiting[From[a_i]]
280 } else if (UnionedBlocks.count(SrcInfo.DefInst->getParent())) {
281 // add a copy to Waiting[From[a_i]]
283 PHIUnion.insert(SrcReg);
284 UnionedBlocks.insert(SrcInfo.DefInst->getParent());
290 ProcessedNames.insert(PHIUnion.begin(), PHIUnion.end());
297 bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
300 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
302 I->begin()->getOpcode() == TargetInstrInfo::PHI)