blob: 51a2ef944d000211bccbee341a469ced424e69d3 [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;
62 MachineInstr* instr;
63
64 void addChild(DomForestNode* DFN) { children.push_back(DFN); }
65
66 public:
67 typedef std::vector<DomForestNode*>::iterator iterator;
68
69 DomForestNode(MachineInstr* MI, DomForestNode* parent) : instr(MI) {
70 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 Andersona4ad2e72007-11-06 04:49:43 +000079 inline MachineInstr* getInstr() { return instr; }
80
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 Anderson83430bc2007-11-04 22:33:26 +000092
93 std::vector<DomForestNode*>
94 computeDomForest(SmallPtrSet<MachineInstr*, 8>& instrs);
95
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
149class PreorderSorter {
150private:
151 DenseMap<MachineBasicBlock*, unsigned>& preorder;
152
153public:
154 PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p) : preorder(p) { }
155
156 bool operator()(MachineInstr* A, MachineInstr* B) {
157 if (A == B)
158 return false;
159
160 if (preorder[A->getParent()] < preorder[B->getParent()])
161 return true;
162 else if (preorder[A->getParent()] > preorder[B->getParent()])
163 return false;
164
165 if (A->getOpcode() == TargetInstrInfo::PHI &&
166 B->getOpcode() == TargetInstrInfo::PHI)
167 return A < B;
168
169 MachineInstr* begin = A->getParent()->begin();
170 return std::distance(begin, A) < std::distance(begin, B);
171 }
172};
173
174std::vector<StrongPHIElimination::DomForestNode*>
175StrongPHIElimination::computeDomForest(SmallPtrSet<MachineInstr*, 8>& instrs) {
176 DomForestNode* VirtualRoot = new DomForestNode(0, 0);
177 maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL));
178
179 std::vector<MachineInstr*> worklist;
180 worklist.reserve(instrs.size());
181 for (SmallPtrSet<MachineInstr*, 8>::iterator I = instrs.begin(),
182 E = instrs.end(); I != E; ++I)
183 worklist.push_back(*I);
184 PreorderSorter PS(preorder);
185 std::sort(worklist.begin(), worklist.end(), PS);
186
187 DomForestNode* CurrentParent = VirtualRoot;
188 std::vector<DomForestNode*> stack;
189 stack.push_back(VirtualRoot);
190
191 for (std::vector<MachineInstr*>::iterator I = worklist.begin(),
192 E = worklist.end(); I != E; ++I) {
193 while (preorder[(*I)->getParent()] >
194 maxpreorder[CurrentParent->getInstr()->getParent()]) {
195 stack.pop_back();
196 CurrentParent = stack.back();
197 }
198
199 DomForestNode* child = new DomForestNode(*I, CurrentParent);
200 stack.push_back(child);
201 CurrentParent = child;
202 }
203
204 std::vector<DomForestNode*> ret;
205 ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end());
206 return ret;
207}
Owen Andersona4ad2e72007-11-06 04:49:43 +0000208
209bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
210 computeDFS(Fn);
211
212
213 return false;
214}