blob: c7809c75b702a980842f099bd277551e7c494e2e [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Owen Anderson0bda0e82007-10-31 03:37:57 +00007//
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"
Owen Anderson0d893b42008-01-08 05:16:15 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Owen Anderson0bda0e82007-10-31 03:37:57 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
Owen Andersonefbcebc2007-12-23 15:37:26 +000031#include "llvm/ADT/DepthFirstIterator.h"
Owen Anderson0bda0e82007-10-31 03:37:57 +000032#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 Andersonec1213f2008-01-09 22:40:54 +000042 // Waiting stores, for each MBB, the set of copies that need to
43 // be inserted into that MBB
Owen Andersonafc6de02007-12-10 08:07:09 +000044 DenseMap<MachineBasicBlock*,
Owen Andersonefbcebc2007-12-23 15:37:26 +000045 std::map<unsigned, unsigned> > Waiting;
46
Owen Andersonec1213f2008-01-09 22:40:54 +000047 // Stacks holds the renaming stack for each register
Owen Andersonefbcebc2007-12-23 15:37:26 +000048 std::map<unsigned, std::vector<unsigned> > Stacks;
Owen Andersonec1213f2008-01-09 22:40:54 +000049
50 // Registers in UsedByAnother are PHI nodes that are themselves
51 // used as operands to another another PHI node
Owen Andersonefbcebc2007-12-23 15:37:26 +000052 std::set<unsigned> UsedByAnother;
Owen Andersonec1213f2008-01-09 22:40:54 +000053
54 // RenameSets are the sets of operands to a PHI (the defining instruction
55 // of the key) that can be renamed without copies
Owen Anderson0c5714b2008-01-08 21:54:52 +000056 std::map<unsigned, std::set<unsigned> > RenameSets;
Owen Andersonafc6de02007-12-10 08:07:09 +000057
Owen Andersonec1213f2008-01-09 22:40:54 +000058 // Store the DFS-in number of each block
59 DenseMap<MachineBasicBlock*, unsigned> preorder;
60
61 // Store the DFS-out number of each block
62 DenseMap<MachineBasicBlock*, unsigned> maxpreorder;
63
Owen Andersona4ad2e72007-11-06 04:49:43 +000064 bool runOnMachineFunction(MachineFunction &Fn);
65
Owen Anderson0bda0e82007-10-31 03:37:57 +000066 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
67 AU.addRequired<MachineDominatorTree>();
Owen Andersona4ad2e72007-11-06 04:49:43 +000068 AU.addRequired<LiveVariables>();
Owen Anderson0bda0e82007-10-31 03:37:57 +000069 MachineFunctionPass::getAnalysisUsage(AU);
70 }
71
72 virtual void releaseMemory() {
73 preorder.clear();
74 maxpreorder.clear();
Owen Andersona4ad2e72007-11-06 04:49:43 +000075
Owen Andersonefbcebc2007-12-23 15:37:26 +000076 Waiting.clear();
Owen Andersonec1213f2008-01-09 22:40:54 +000077 Stacks.clear();
78 UsedByAnother.clear();
79 RenameSets.clear();
Owen Anderson0bda0e82007-10-31 03:37:57 +000080 }
81
82 private:
Owen Andersonec1213f2008-01-09 22:40:54 +000083
84 /// DomForestNode - Represents a node in the "dominator forest". This is
85 /// a forest in which the nodes represent registers and the edges
86 /// represent a dominance relation in the block defining those registers.
Owen Anderson83430bc2007-11-04 22:33:26 +000087 struct DomForestNode {
88 private:
Owen Andersonec1213f2008-01-09 22:40:54 +000089 // Store references to our children
Owen Anderson83430bc2007-11-04 22:33:26 +000090 std::vector<DomForestNode*> children;
Owen Andersonec1213f2008-01-09 22:40:54 +000091 // The register we represent
Owen Andersonee49b532007-11-06 05:22:43 +000092 unsigned reg;
Owen Anderson83430bc2007-11-04 22:33:26 +000093
Owen Andersonec1213f2008-01-09 22:40:54 +000094 // Add another node as our child
Owen Anderson83430bc2007-11-04 22:33:26 +000095 void addChild(DomForestNode* DFN) { children.push_back(DFN); }
96
97 public:
98 typedef std::vector<DomForestNode*>::iterator iterator;
99
Owen Andersonec1213f2008-01-09 22:40:54 +0000100 // Create a DomForestNode by providing the register it represents, and
101 // the node to be its parent. The virtual root node has register 0
102 // and a null parent.
Owen Andersonee49b532007-11-06 05:22:43 +0000103 DomForestNode(unsigned r, DomForestNode* parent) : reg(r) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000104 if (parent)
105 parent->addChild(this);
106 }
107
Owen Andersona4ad2e72007-11-06 04:49:43 +0000108 ~DomForestNode() {
109 for (iterator I = begin(), E = end(); I != E; ++I)
110 delete *I;
111 }
Owen Anderson83430bc2007-11-04 22:33:26 +0000112
Owen Andersonec1213f2008-01-09 22:40:54 +0000113 /// getReg - Return the regiser that this node represents
Owen Andersonee49b532007-11-06 05:22:43 +0000114 inline unsigned getReg() { return reg; }
Owen Andersona4ad2e72007-11-06 04:49:43 +0000115
Owen Andersonec1213f2008-01-09 22:40:54 +0000116 // Provide iterator access to our children
Owen Andersona4ad2e72007-11-06 04:49:43 +0000117 inline DomForestNode::iterator begin() { return children.begin(); }
118 inline DomForestNode::iterator end() { return children.end(); }
Owen Anderson83430bc2007-11-04 22:33:26 +0000119 };
120
Owen Anderson0bda0e82007-10-31 03:37:57 +0000121 void computeDFS(MachineFunction& MF);
Owen Anderson60a877d2007-11-07 05:17:15 +0000122 void processBlock(MachineBasicBlock* MBB);
Owen Anderson83430bc2007-11-04 22:33:26 +0000123
Owen Andersonee49b532007-11-06 05:22:43 +0000124 std::vector<DomForestNode*> computeDomForest(std::set<unsigned>& instrs);
Owen Andersond525f662007-12-11 20:12:11 +0000125 void processPHIUnion(MachineInstr* Inst,
126 std::set<unsigned>& PHIUnion,
Owen Anderson62d67dd2007-12-13 05:53:03 +0000127 std::vector<StrongPHIElimination::DomForestNode*>& DF,
128 std::vector<std::pair<unsigned, unsigned> >& locals);
Owen Andersonf1519e82007-12-24 22:12:23 +0000129 void ScheduleCopies(MachineBasicBlock* MBB, std::set<unsigned>& pushed);
Owen Anderson719fef62008-01-09 10:32:30 +0000130 void InsertCopies(MachineBasicBlock* MBB, std::set<MachineBasicBlock*>& v);
Owen Anderson0bda0e82007-10-31 03:37:57 +0000131 };
132
133 char StrongPHIElimination::ID = 0;
134 RegisterPass<StrongPHIElimination> X("strong-phi-node-elimination",
135 "Eliminate PHI nodes for register allocation, intelligently");
136}
137
138const PassInfo *llvm::StrongPHIEliminationID = X.getPassInfo();
139
140/// computeDFS - Computes the DFS-in and DFS-out numbers of the dominator tree
141/// of the given MachineFunction. These numbers are then used in other parts
142/// of the PHI elimination process.
143void StrongPHIElimination::computeDFS(MachineFunction& MF) {
144 SmallPtrSet<MachineDomTreeNode*, 8> frontier;
145 SmallPtrSet<MachineDomTreeNode*, 8> visited;
146
147 unsigned time = 0;
148
149 MachineDominatorTree& DT = getAnalysis<MachineDominatorTree>();
150
151 MachineDomTreeNode* node = DT.getRootNode();
152
153 std::vector<MachineDomTreeNode*> worklist;
154 worklist.push_back(node);
155
156 while (!worklist.empty()) {
157 MachineDomTreeNode* currNode = worklist.back();
158
159 if (!frontier.count(currNode)) {
160 frontier.insert(currNode);
161 ++time;
162 preorder.insert(std::make_pair(currNode->getBlock(), time));
163 }
164
165 bool inserted = false;
166 for (MachineDomTreeNode::iterator I = node->begin(), E = node->end();
167 I != E; ++I)
168 if (!frontier.count(*I) && !visited.count(*I)) {
169 worklist.push_back(*I);
170 inserted = true;
171 break;
172 }
173
174 if (!inserted) {
175 frontier.erase(currNode);
176 visited.insert(currNode);
177 maxpreorder.insert(std::make_pair(currNode->getBlock(), time));
178
179 worklist.pop_back();
180 }
181 }
Duncan Sands1bd32712007-10-31 08:49:24 +0000182}
Owen Anderson83430bc2007-11-04 22:33:26 +0000183
Owen Anderson8b96b9f2007-11-06 05:26:02 +0000184/// PreorderSorter - a helper class that is used to sort registers
185/// according to the preorder number of their defining blocks
Owen Anderson83430bc2007-11-04 22:33:26 +0000186class PreorderSorter {
187private:
188 DenseMap<MachineBasicBlock*, unsigned>& preorder;
Owen Andersonee49b532007-11-06 05:22:43 +0000189 LiveVariables& LV;
Owen Anderson83430bc2007-11-04 22:33:26 +0000190
191public:
Owen Andersonee49b532007-11-06 05:22:43 +0000192 PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p,
193 LiveVariables& L) : preorder(p), LV(L) { }
Owen Anderson83430bc2007-11-04 22:33:26 +0000194
Owen Andersonee49b532007-11-06 05:22:43 +0000195 bool operator()(unsigned A, unsigned B) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000196 if (A == B)
197 return false;
198
Owen Andersonee49b532007-11-06 05:22:43 +0000199 MachineBasicBlock* ABlock = LV.getVarInfo(A).DefInst->getParent();
200 MachineBasicBlock* BBlock = LV.getVarInfo(A).DefInst->getParent();
201
202 if (preorder[ABlock] < preorder[BBlock])
Owen Anderson83430bc2007-11-04 22:33:26 +0000203 return true;
Owen Andersonee49b532007-11-06 05:22:43 +0000204 else if (preorder[ABlock] > preorder[BBlock])
Owen Anderson83430bc2007-11-04 22:33:26 +0000205 return false;
206
Owen Andersonee49b532007-11-06 05:22:43 +0000207 return false;
Owen Anderson83430bc2007-11-04 22:33:26 +0000208 }
209};
210
Owen Anderson8b96b9f2007-11-06 05:26:02 +0000211/// computeDomForest - compute the subforest of the DomTree corresponding
212/// to the defining blocks of the registers in question
Owen Anderson83430bc2007-11-04 22:33:26 +0000213std::vector<StrongPHIElimination::DomForestNode*>
Owen Andersonee49b532007-11-06 05:22:43 +0000214StrongPHIElimination::computeDomForest(std::set<unsigned>& regs) {
215 LiveVariables& LV = getAnalysis<LiveVariables>();
216
Owen Andersonec1213f2008-01-09 22:40:54 +0000217 // Begin by creating a virtual root node, since the actual results
218 // may well be a forest. Assume this node has maximum DFS-out number.
Owen Anderson83430bc2007-11-04 22:33:26 +0000219 DomForestNode* VirtualRoot = new DomForestNode(0, 0);
220 maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL));
221
Owen Andersonec1213f2008-01-09 22:40:54 +0000222 // Populate a worklist with the registers
Owen Andersonee49b532007-11-06 05:22:43 +0000223 std::vector<unsigned> worklist;
224 worklist.reserve(regs.size());
225 for (std::set<unsigned>::iterator I = regs.begin(), E = regs.end();
226 I != E; ++I)
Owen Anderson83430bc2007-11-04 22:33:26 +0000227 worklist.push_back(*I);
Owen Andersonee49b532007-11-06 05:22:43 +0000228
Owen Andersonec1213f2008-01-09 22:40:54 +0000229 // Sort the registers by the DFS-in number of their defining block
Owen Andersonee49b532007-11-06 05:22:43 +0000230 PreorderSorter PS(preorder, LV);
Owen Anderson83430bc2007-11-04 22:33:26 +0000231 std::sort(worklist.begin(), worklist.end(), PS);
232
Owen Andersonec1213f2008-01-09 22:40:54 +0000233 // Create a "current parent" stack, and put the virtual root on top of it
Owen Anderson83430bc2007-11-04 22:33:26 +0000234 DomForestNode* CurrentParent = VirtualRoot;
235 std::vector<DomForestNode*> stack;
236 stack.push_back(VirtualRoot);
237
Owen Andersonec1213f2008-01-09 22:40:54 +0000238 // Iterate over all the registers in the previously computed order
Owen Andersonee49b532007-11-06 05:22:43 +0000239 for (std::vector<unsigned>::iterator I = worklist.begin(), E = worklist.end();
240 I != E; ++I) {
241 unsigned pre = preorder[LV.getVarInfo(*I).DefInst->getParent()];
Owen Andersoncb7d9492008-01-09 06:19:05 +0000242 MachineBasicBlock* parentBlock = CurrentParent->getReg() ?
243 LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent() :
244 0;
Owen Andersonee49b532007-11-06 05:22:43 +0000245
Owen Andersonec1213f2008-01-09 22:40:54 +0000246 // If the DFS-in number of the register is greater than the DFS-out number
247 // of the current parent, repeatedly pop the parent stack until it isn't.
Owen Andersonee49b532007-11-06 05:22:43 +0000248 while (pre > maxpreorder[parentBlock]) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000249 stack.pop_back();
250 CurrentParent = stack.back();
Owen Andersonee49b532007-11-06 05:22:43 +0000251
Owen Anderson864e3a32008-01-09 10:41:39 +0000252 parentBlock = CurrentParent->getReg() ?
253 LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent() :
254 0;
Owen Anderson83430bc2007-11-04 22:33:26 +0000255 }
256
Owen Andersonec1213f2008-01-09 22:40:54 +0000257 // Now that we've found the appropriate parent, create a DomForestNode for
258 // this register and attach it to the forest
Owen Anderson83430bc2007-11-04 22:33:26 +0000259 DomForestNode* child = new DomForestNode(*I, CurrentParent);
Owen Andersonec1213f2008-01-09 22:40:54 +0000260
261 // Push this new node on the "current parent" stack
Owen Anderson83430bc2007-11-04 22:33:26 +0000262 stack.push_back(child);
263 CurrentParent = child;
264 }
265
Owen Andersonec1213f2008-01-09 22:40:54 +0000266 // Return a vector containing the children of the virtual root node
Owen Anderson83430bc2007-11-04 22:33:26 +0000267 std::vector<DomForestNode*> ret;
268 ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end());
269 return ret;
270}
Owen Andersona4ad2e72007-11-06 04:49:43 +0000271
Owen Anderson60a877d2007-11-07 05:17:15 +0000272/// isLiveIn - helper method that determines, from a VarInfo, if a register
273/// is live into a block
Owen Anderson9e549202008-01-07 21:30:40 +0000274static bool isLiveIn(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) {
Owen Anderson60a877d2007-11-07 05:17:15 +0000275 if (V.AliveBlocks.test(MBB->getNumber()))
276 return true;
Owen Andersonee49b532007-11-06 05:22:43 +0000277
Owen Anderson14b3fb72007-11-08 01:32:45 +0000278 if (V.DefInst->getParent() != MBB &&
279 V.UsedBlocks.test(MBB->getNumber()))
280 return true;
Owen Anderson60a877d2007-11-07 05:17:15 +0000281
282 return false;
283}
284
285/// isLiveOut - help method that determines, from a VarInfo, if a register is
286/// live out of a block.
Owen Anderson9e549202008-01-07 21:30:40 +0000287static bool isLiveOut(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) {
Owen Anderson14b3fb72007-11-08 01:32:45 +0000288 if (MBB == V.DefInst->getParent() ||
289 V.UsedBlocks.test(MBB->getNumber())) {
290 for (std::vector<MachineInstr*>::iterator I = V.Kills.begin(),
291 E = V.Kills.end(); I != E; ++I)
292 if ((*I)->getParent() == MBB)
293 return false;
294
Owen Anderson60a877d2007-11-07 05:17:15 +0000295 return true;
Owen Anderson14b3fb72007-11-08 01:32:45 +0000296 }
Owen Anderson60a877d2007-11-07 05:17:15 +0000297
298 return false;
299}
300
Owen Anderson87a702b2007-12-16 05:44:27 +0000301/// interferes - checks for local interferences by scanning a block. The only
302/// trick parameter is 'mode' which tells it the relationship of the two
303/// registers. 0 - defined in the same block, 1 - first properly dominates
304/// second, 2 - second properly dominates first
Owen Andersonb199cbe2008-01-10 00:33:11 +0000305static bool interferes(unsigned a, unsigned b, MachineBasicBlock* scan,
306 LiveVariables& LV, unsigned mode) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000307 MachineInstr* def = 0;
308 MachineInstr* kill = 0;
309
Owen Andersonb199cbe2008-01-10 00:33:11 +0000310 LiveVariables::VarInfo& First = LV.getVarInfo(a);
311 LiveVariables::VarInfo& Second = LV.getVarInfo(b);
312
Owen Anderson87a702b2007-12-16 05:44:27 +0000313 bool interference = false;
314
315 // Wallk the block, checking for interferences
316 for (MachineBasicBlock::iterator MBI = scan->begin(), MBE = scan->end();
317 MBI != MBE; ++MBI) {
318 MachineInstr* curr = MBI;
319
320 // Same defining block...
321 if (mode == 0) {
322 if (curr == First.DefInst) {
323 // If we find our first DefInst, save it
324 if (!def) {
325 def = curr;
326 // If there's already an unkilled DefInst, then
327 // this is an interference
328 } else if (!kill) {
329 interference = true;
330 break;
331 // If there's a DefInst followed by a KillInst, then
332 // they can't interfere
333 } else {
334 interference = false;
335 break;
336 }
337 // Symmetric with the above
338 } else if (curr == Second.DefInst ) {
339 if (!def) {
340 def = curr;
341 } else if (!kill) {
342 interference = true;
343 break;
344 } else {
345 interference = false;
346 break;
347 }
348 // Store KillInsts if they match up with the DefInst
Owen Andersonb199cbe2008-01-10 00:33:11 +0000349 } else if (LV.KillsRegister(curr, a)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000350 if (def == First.DefInst) {
351 kill = curr;
Owen Andersonb199cbe2008-01-10 00:33:11 +0000352 } else if (LV.KillsRegister(curr, b)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000353 if (def == Second.DefInst) {
354 kill = curr;
355 }
356 }
357 }
358 // First properly dominates second...
359 } else if (mode == 1) {
360 if (curr == Second.DefInst) {
361 // DefInst of second without kill of first is an interference
362 if (!kill) {
363 interference = true;
364 break;
365 // DefInst after a kill is a non-interference
366 } else {
367 interference = false;
368 break;
369 }
370 // Save KillInsts of First
Owen Andersonb199cbe2008-01-10 00:33:11 +0000371 } else if (LV.KillsRegister(curr, a)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000372 kill = curr;
373 }
374 // Symmetric with the above
375 } else if (mode == 2) {
376 if (curr == First.DefInst) {
377 if (!kill) {
378 interference = true;
379 break;
380 } else {
381 interference = false;
382 break;
383 }
Owen Andersonb199cbe2008-01-10 00:33:11 +0000384 } else if (LV.KillsRegister(curr, b)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000385 kill = curr;
386 }
387 }
388 }
389
390 return interference;
391}
392
Owen Anderson60a877d2007-11-07 05:17:15 +0000393/// processBlock - Eliminate PHIs in the given block
394void StrongPHIElimination::processBlock(MachineBasicBlock* MBB) {
395 LiveVariables& LV = getAnalysis<LiveVariables>();
396
397 // Holds names that have been added to a set in any PHI within this block
398 // before the current one.
399 std::set<unsigned> ProcessedNames;
400
401 MachineBasicBlock::iterator P = MBB->begin();
Owen Anderson864e3a32008-01-09 10:41:39 +0000402 while (P != MBB->end() && P->getOpcode() == TargetInstrInfo::PHI) {
Owen Anderson60a877d2007-11-07 05:17:15 +0000403 LiveVariables::VarInfo& PHIInfo = LV.getVarInfo(P->getOperand(0).getReg());
404
Owen Andersonafc6de02007-12-10 08:07:09 +0000405 unsigned DestReg = P->getOperand(0).getReg();
406
Owen Anderson60a877d2007-11-07 05:17:15 +0000407 // Hold the names that are currently in the candidate set.
408 std::set<unsigned> PHIUnion;
409 std::set<MachineBasicBlock*> UnionedBlocks;
410
411 for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
412 unsigned SrcReg = P->getOperand(i-1).getReg();
413 LiveVariables::VarInfo& SrcInfo = LV.getVarInfo(SrcReg);
414
Owen Andersonafc6de02007-12-10 08:07:09 +0000415 // Check for trivial interferences
416 if (isLiveIn(SrcInfo, P->getParent()) ||
417 isLiveOut(PHIInfo, SrcInfo.DefInst->getParent()) ||
418 ( PHIInfo.DefInst->getOpcode() == TargetInstrInfo::PHI &&
419 isLiveIn(PHIInfo, SrcInfo.DefInst->getParent()) ) ||
420 ProcessedNames.count(SrcReg) ||
421 UnionedBlocks.count(SrcInfo.DefInst->getParent())) {
422
Owen Anderson60a877d2007-11-07 05:17:15 +0000423 // add a copy from a_i to p in Waiting[From[a_i]]
Chris Lattner8aa797a2007-12-30 23:10:15 +0000424 MachineBasicBlock* From = P->getOperand(i).getMBB();
Owen Andersonefbcebc2007-12-23 15:37:26 +0000425 Waiting[From].insert(std::make_pair(SrcReg, DestReg));
426 UsedByAnother.insert(SrcReg);
Owen Anderson60a877d2007-11-07 05:17:15 +0000427 } else {
428 PHIUnion.insert(SrcReg);
429 UnionedBlocks.insert(SrcInfo.DefInst->getParent());
Owen Anderson60a877d2007-11-07 05:17:15 +0000430 }
Owen Anderson60a877d2007-11-07 05:17:15 +0000431 }
432
Owen Anderson42f9e962007-11-13 20:13:24 +0000433 std::vector<StrongPHIElimination::DomForestNode*> DF =
434 computeDomForest(PHIUnion);
435
Owen Andersonafc6de02007-12-10 08:07:09 +0000436 // Walk DomForest to resolve interferences
Owen Anderson62d67dd2007-12-13 05:53:03 +0000437 std::vector<std::pair<unsigned, unsigned> > localInterferences;
438 processPHIUnion(P, PHIUnion, DF, localInterferences);
439
Owen Andersoncae8d8d2007-12-22 04:59:10 +0000440 // Check for local interferences
Owen Anderson87a702b2007-12-16 05:44:27 +0000441 for (std::vector<std::pair<unsigned, unsigned> >::iterator I =
442 localInterferences.begin(), E = localInterferences.end(); I != E; ++I) {
443 std::pair<unsigned, unsigned> p = *I;
444
445 LiveVariables::VarInfo& FirstInfo = LV.getVarInfo(p.first);
446 LiveVariables::VarInfo& SecondInfo = LV.getVarInfo(p.second);
447
448 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
449
450 // Determine the block we need to scan and the relationship between
451 // the two registers
452 MachineBasicBlock* scan = 0;
453 unsigned mode = 0;
454 if (FirstInfo.DefInst->getParent() == SecondInfo.DefInst->getParent()) {
455 scan = FirstInfo.DefInst->getParent();
456 mode = 0; // Same block
457 } else if (MDT.dominates(FirstInfo.DefInst->getParent(),
458 SecondInfo.DefInst->getParent())) {
459 scan = SecondInfo.DefInst->getParent();
460 mode = 1; // First dominates second
461 } else {
462 scan = FirstInfo.DefInst->getParent();
463 mode = 2; // Second dominates first
464 }
465
466 // If there's an interference, we need to insert copies
Owen Andersonb199cbe2008-01-10 00:33:11 +0000467 if (interferes(p.first, p.second, scan, LV, mode)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000468 // Insert copies for First
469 for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
470 if (P->getOperand(i-1).getReg() == p.first) {
471 unsigned SrcReg = p.first;
472 MachineBasicBlock* From = P->getOperand(i).getMBB();
473
Owen Andersonefbcebc2007-12-23 15:37:26 +0000474 Waiting[From].insert(std::make_pair(SrcReg,
475 P->getOperand(0).getReg()));
476 UsedByAnother.insert(SrcReg);
477
Owen Anderson87a702b2007-12-16 05:44:27 +0000478 PHIUnion.erase(SrcReg);
479 }
480 }
481 }
482 }
Owen Anderson42f9e962007-11-13 20:13:24 +0000483
Owen Anderson0c5714b2008-01-08 21:54:52 +0000484 // Cache renaming information
485 RenameSets.insert(std::make_pair(P->getOperand(0).getReg(), PHIUnion));
Owen Andersoncae8d8d2007-12-22 04:59:10 +0000486
Owen Anderson42f9e962007-11-13 20:13:24 +0000487 ProcessedNames.insert(PHIUnion.begin(), PHIUnion.end());
Owen Anderson60a877d2007-11-07 05:17:15 +0000488 ++P;
489 }
Owen Andersonee49b532007-11-06 05:22:43 +0000490}
491
Owen Anderson965b4672007-12-16 04:07:23 +0000492/// processPHIUnion - Take a set of candidate registers to be coallesced when
493/// decomposing the PHI instruction. Use the DominanceForest to remove the ones
494/// that are known to interfere, and flag others that need to be checked for
495/// local interferences.
Owen Andersond525f662007-12-11 20:12:11 +0000496void StrongPHIElimination::processPHIUnion(MachineInstr* Inst,
497 std::set<unsigned>& PHIUnion,
Owen Anderson62d67dd2007-12-13 05:53:03 +0000498 std::vector<StrongPHIElimination::DomForestNode*>& DF,
499 std::vector<std::pair<unsigned, unsigned> >& locals) {
Owen Andersond525f662007-12-11 20:12:11 +0000500
501 std::vector<DomForestNode*> worklist(DF.begin(), DF.end());
502 SmallPtrSet<DomForestNode*, 4> visited;
503
504 LiveVariables& LV = getAnalysis<LiveVariables>();
505 unsigned DestReg = Inst->getOperand(0).getReg();
506
Owen Anderson965b4672007-12-16 04:07:23 +0000507 // DF walk on the DomForest
Owen Andersond525f662007-12-11 20:12:11 +0000508 while (!worklist.empty()) {
509 DomForestNode* DFNode = worklist.back();
510
511 LiveVariables::VarInfo& Info = LV.getVarInfo(DFNode->getReg());
512 visited.insert(DFNode);
513
514 bool inserted = false;
Owen Andersond525f662007-12-11 20:12:11 +0000515 for (DomForestNode::iterator CI = DFNode->begin(), CE = DFNode->end();
516 CI != CE; ++CI) {
517 DomForestNode* child = *CI;
518 LiveVariables::VarInfo& CInfo = LV.getVarInfo(child->getReg());
519
520 if (isLiveOut(Info, CInfo.DefInst->getParent())) {
Owen Andersond525f662007-12-11 20:12:11 +0000521 // Insert copies for parent
522 for (int i = Inst->getNumOperands() - 1; i >= 2; i-=2) {
523 if (Inst->getOperand(i-1).getReg() == DFNode->getReg()) {
Owen Andersoned2ffa22007-12-12 01:25:08 +0000524 unsigned SrcReg = DFNode->getReg();
Owen Andersond525f662007-12-11 20:12:11 +0000525 MachineBasicBlock* From = Inst->getOperand(i).getMBB();
526
Owen Andersonefbcebc2007-12-23 15:37:26 +0000527 Waiting[From].insert(std::make_pair(SrcReg, DestReg));
528 UsedByAnother.insert(SrcReg);
529
Owen Andersoned2ffa22007-12-12 01:25:08 +0000530 PHIUnion.erase(SrcReg);
Owen Andersond525f662007-12-11 20:12:11 +0000531 }
532 }
Owen Anderson4ba08ec2007-12-13 05:43:37 +0000533 } else if (isLiveIn(Info, CInfo.DefInst->getParent()) ||
534 Info.DefInst->getParent() == CInfo.DefInst->getParent()) {
Owen Anderson62d67dd2007-12-13 05:53:03 +0000535 // Add (p, c) to possible local interferences
536 locals.push_back(std::make_pair(DFNode->getReg(), child->getReg()));
Owen Andersond525f662007-12-11 20:12:11 +0000537 }
Owen Anderson965b4672007-12-16 04:07:23 +0000538
Owen Anderson4ba08ec2007-12-13 05:43:37 +0000539 if (!visited.count(child)) {
540 worklist.push_back(child);
541 inserted = true;
Owen Andersond525f662007-12-11 20:12:11 +0000542 }
543 }
544
545 if (!inserted) worklist.pop_back();
546 }
547}
548
Owen Andersonefbcebc2007-12-23 15:37:26 +0000549/// ScheduleCopies - Insert copies into predecessor blocks, scheduling
550/// them properly so as to avoid the 'lost copy' and the 'virtual swap'
551/// problems.
552///
553/// Based on "Practical Improvements to the Construction and Destruction
554/// of Static Single Assignment Form" by Briggs, et al.
Owen Andersonf1519e82007-12-24 22:12:23 +0000555void StrongPHIElimination::ScheduleCopies(MachineBasicBlock* MBB,
556 std::set<unsigned>& pushed) {
Owen Anderson0d893b42008-01-08 05:16:15 +0000557 // FIXME: This function needs to update LiveVariables
Owen Andersonefbcebc2007-12-23 15:37:26 +0000558 std::map<unsigned, unsigned>& copy_set= Waiting[MBB];
559
560 std::map<unsigned, unsigned> worklist;
561 std::map<unsigned, unsigned> map;
562
563 // Setup worklist of initial copies
564 for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(),
565 E = copy_set.end(); I != E; ) {
566 map.insert(std::make_pair(I->first, I->first));
567 map.insert(std::make_pair(I->second, I->second));
568
569 if (!UsedByAnother.count(I->first)) {
570 worklist.insert(*I);
571
572 // Avoid iterator invalidation
573 unsigned first = I->first;
574 ++I;
575 copy_set.erase(first);
576 } else {
577 ++I;
578 }
579 }
580
581 LiveVariables& LV = getAnalysis<LiveVariables>();
Owen Anderson0d893b42008-01-08 05:16:15 +0000582 MachineFunction* MF = MBB->getParent();
583 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
Owen Andersonefbcebc2007-12-23 15:37:26 +0000584
585 // Iterate over the worklist, inserting copies
586 while (!worklist.empty() || !copy_set.empty()) {
587 while (!worklist.empty()) {
588 std::pair<unsigned, unsigned> curr = *worklist.begin();
589 worklist.erase(curr.first);
590
Owen Anderson0d893b42008-01-08 05:16:15 +0000591 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first);
592
Owen Andersonefbcebc2007-12-23 15:37:26 +0000593 if (isLiveOut(LV.getVarInfo(curr.second), MBB)) {
Owen Anderson0d893b42008-01-08 05:16:15 +0000594 // Create a temporary
595 unsigned t = MF->getRegInfo().createVirtualRegister(RC);
596
597 // Insert copy from curr.second to a temporary at
598 // the Phi defining curr.second
599 LiveVariables::VarInfo VI = LV.getVarInfo(curr.second);
600 MachineBasicBlock::iterator PI = VI.DefInst;
601 TII->copyRegToReg(*VI.DefInst->getParent(), PI, t,
602 curr.second, RC, RC);
603
Owen Andersonefbcebc2007-12-23 15:37:26 +0000604 // Push temporary on Stacks
Owen Anderson0d893b42008-01-08 05:16:15 +0000605 Stacks[curr.second].push_back(t);
606
607 // Insert curr.second in pushed
608 pushed.insert(curr.second);
Owen Andersonefbcebc2007-12-23 15:37:26 +0000609 }
610
611 // Insert copy from map[curr.first] to curr.second
Owen Anderson9c2efa82008-01-10 00:01:41 +0000612 TII->copyRegToReg(*MBB, MBB->getFirstTerminator(), curr.second,
Owen Anderson0d893b42008-01-08 05:16:15 +0000613 map[curr.first], RC, RC);
Owen Andersonefbcebc2007-12-23 15:37:26 +0000614 map[curr.first] = curr.second;
615
616 // If curr.first is a destination in copy_set...
617 for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(),
618 E = copy_set.end(); I != E; )
619 if (curr.first == I->second) {
620 std::pair<unsigned, unsigned> temp = *I;
621
622 // Avoid iterator invalidation
623 ++I;
624 copy_set.erase(temp.first);
625 worklist.insert(temp);
626
627 break;
628 } else {
629 ++I;
630 }
631 }
632
633 if (!copy_set.empty()) {
634 std::pair<unsigned, unsigned> curr = *copy_set.begin();
635 copy_set.erase(curr.first);
636
Owen Anderson0d893b42008-01-08 05:16:15 +0000637 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first);
638
Owen Andersonefbcebc2007-12-23 15:37:26 +0000639 // Insert a copy from dest to a new temporary t at the end of b
Owen Anderson0d893b42008-01-08 05:16:15 +0000640 unsigned t = MF->getRegInfo().createVirtualRegister(RC);
Owen Anderson9c2efa82008-01-10 00:01:41 +0000641 TII->copyRegToReg(*MBB, MBB->getFirstTerminator(), t,
Owen Anderson0d893b42008-01-08 05:16:15 +0000642 curr.second, RC, RC);
643 map[curr.second] = t;
Owen Andersonefbcebc2007-12-23 15:37:26 +0000644
645 worklist.insert(curr);
646 }
647 }
648}
649
Owen Andersonf1519e82007-12-24 22:12:23 +0000650/// InsertCopies - insert copies into MBB and all of its successors
Owen Anderson719fef62008-01-09 10:32:30 +0000651void StrongPHIElimination::InsertCopies(MachineBasicBlock* MBB,
652 std::set<MachineBasicBlock*>& visited) {
653 visited.insert(MBB);
654
Owen Andersonf1519e82007-12-24 22:12:23 +0000655 std::set<unsigned> pushed;
656
657 // Rewrite register uses from Stacks
658 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
659 I != E; ++I)
660 for (unsigned i = 0; i < I->getNumOperands(); ++i)
661 if (I->getOperand(i).isRegister() &&
662 Stacks[I->getOperand(i).getReg()].size()) {
663 I->getOperand(i).setReg(Stacks[I->getOperand(i).getReg()].back());
664 }
665
666 // Schedule the copies for this block
667 ScheduleCopies(MBB, pushed);
668
669 // Recur to our successors
670 for (GraphTraits<MachineBasicBlock*>::ChildIteratorType I =
671 GraphTraits<MachineBasicBlock*>::child_begin(MBB), E =
672 GraphTraits<MachineBasicBlock*>::child_end(MBB); I != E; ++I)
Owen Anderson719fef62008-01-09 10:32:30 +0000673 if (!visited.count(*I))
674 InsertCopies(*I, visited);
Owen Andersonf1519e82007-12-24 22:12:23 +0000675
676 // As we exit this block, pop the names we pushed while processing it
677 for (std::set<unsigned>::iterator I = pushed.begin(),
678 E = pushed.end(); I != E; ++I)
679 Stacks[*I].pop_back();
680}
681
Owen Andersona4ad2e72007-11-06 04:49:43 +0000682bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
Owen Andersonefbcebc2007-12-23 15:37:26 +0000683 // Compute DFS numbers of each block
Owen Andersona4ad2e72007-11-06 04:49:43 +0000684 computeDFS(Fn);
685
Owen Andersonefbcebc2007-12-23 15:37:26 +0000686 // Determine which phi node operands need copies
Owen Anderson60a877d2007-11-07 05:17:15 +0000687 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
688 if (!I->empty() &&
689 I->begin()->getOpcode() == TargetInstrInfo::PHI)
690 processBlock(I);
Owen Andersona4ad2e72007-11-06 04:49:43 +0000691
Owen Andersonefbcebc2007-12-23 15:37:26 +0000692 // Insert copies
Owen Andersonf1519e82007-12-24 22:12:23 +0000693 // FIXME: This process should probably preserve LiveVariables
Owen Anderson719fef62008-01-09 10:32:30 +0000694 std::set<MachineBasicBlock*> visited;
695 InsertCopies(Fn.begin(), visited);
Owen Andersonefbcebc2007-12-23 15:37:26 +0000696
Owen Anderson0c5714b2008-01-08 21:54:52 +0000697 // Perform renaming
698 typedef std::map<unsigned, std::set<unsigned> > RenameSetType;
699 for (RenameSetType::iterator I = RenameSets.begin(), E = RenameSets.end();
700 I != E; ++I)
701 for (std::set<unsigned>::iterator SI = I->second.begin(),
702 SE = I->second.end(); SI != SE; ++SI)
703 Fn.getRegInfo().replaceRegWith(*SI, I->first);
704
705 // FIXME: Insert last-minute copies
706
707 // Remove PHIs
708 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
709 for (MachineBasicBlock::iterator BI = I->begin(), BE = I->end();
710 BI != BE; ++BI)
711 if (BI->getOpcode() == TargetInstrInfo::PHI)
712 BI->eraseFromParent();
Owen Andersoncae8d8d2007-12-22 04:59:10 +0000713
Owen Andersona4ad2e72007-11-06 04:49:43 +0000714 return false;
715}