blob: ff58e796bf5e7cf3e51f8a1065ea53321e6b68f6 [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 Andersonddd060f2008-01-10 01:36:43 +0000124 std::vector<DomForestNode*> computeDomForest(std::set<unsigned>& instrs,
125 MachineRegisterInfo& MRI);
Owen Andersond525f662007-12-11 20:12:11 +0000126 void processPHIUnion(MachineInstr* Inst,
127 std::set<unsigned>& PHIUnion,
Owen Anderson62d67dd2007-12-13 05:53:03 +0000128 std::vector<StrongPHIElimination::DomForestNode*>& DF,
129 std::vector<std::pair<unsigned, unsigned> >& locals);
Owen Andersonf1519e82007-12-24 22:12:23 +0000130 void ScheduleCopies(MachineBasicBlock* MBB, std::set<unsigned>& pushed);
Owen Anderson719fef62008-01-09 10:32:30 +0000131 void InsertCopies(MachineBasicBlock* MBB, std::set<MachineBasicBlock*>& v);
Owen Anderson0bda0e82007-10-31 03:37:57 +0000132 };
133
134 char StrongPHIElimination::ID = 0;
135 RegisterPass<StrongPHIElimination> X("strong-phi-node-elimination",
136 "Eliminate PHI nodes for register allocation, intelligently");
137}
138
139const PassInfo *llvm::StrongPHIEliminationID = X.getPassInfo();
140
141/// computeDFS - Computes the DFS-in and DFS-out numbers of the dominator tree
142/// of the given MachineFunction. These numbers are then used in other parts
143/// of the PHI elimination process.
144void StrongPHIElimination::computeDFS(MachineFunction& MF) {
145 SmallPtrSet<MachineDomTreeNode*, 8> frontier;
146 SmallPtrSet<MachineDomTreeNode*, 8> visited;
147
148 unsigned time = 0;
149
150 MachineDominatorTree& DT = getAnalysis<MachineDominatorTree>();
151
152 MachineDomTreeNode* node = DT.getRootNode();
153
154 std::vector<MachineDomTreeNode*> worklist;
155 worklist.push_back(node);
156
157 while (!worklist.empty()) {
158 MachineDomTreeNode* currNode = worklist.back();
159
160 if (!frontier.count(currNode)) {
161 frontier.insert(currNode);
162 ++time;
163 preorder.insert(std::make_pair(currNode->getBlock(), time));
164 }
165
166 bool inserted = false;
167 for (MachineDomTreeNode::iterator I = node->begin(), E = node->end();
168 I != E; ++I)
169 if (!frontier.count(*I) && !visited.count(*I)) {
170 worklist.push_back(*I);
171 inserted = true;
172 break;
173 }
174
175 if (!inserted) {
176 frontier.erase(currNode);
177 visited.insert(currNode);
178 maxpreorder.insert(std::make_pair(currNode->getBlock(), time));
179
180 worklist.pop_back();
181 }
182 }
Duncan Sands1bd32712007-10-31 08:49:24 +0000183}
Owen Anderson83430bc2007-11-04 22:33:26 +0000184
Owen Anderson8b96b9f2007-11-06 05:26:02 +0000185/// PreorderSorter - a helper class that is used to sort registers
186/// according to the preorder number of their defining blocks
Owen Anderson83430bc2007-11-04 22:33:26 +0000187class PreorderSorter {
188private:
189 DenseMap<MachineBasicBlock*, unsigned>& preorder;
Owen Andersonddd060f2008-01-10 01:36:43 +0000190 MachineRegisterInfo& MRI;
Owen Anderson83430bc2007-11-04 22:33:26 +0000191
192public:
Owen Andersonee49b532007-11-06 05:22:43 +0000193 PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p,
Owen Andersonddd060f2008-01-10 01:36:43 +0000194 MachineRegisterInfo& M) : preorder(p), MRI(M) { }
Owen Anderson83430bc2007-11-04 22:33:26 +0000195
Owen Andersonee49b532007-11-06 05:22:43 +0000196 bool operator()(unsigned A, unsigned B) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000197 if (A == B)
198 return false;
199
Owen Andersonddd060f2008-01-10 01:36:43 +0000200 MachineBasicBlock* ABlock = MRI.getVRegDef(A)->getParent();
201 MachineBasicBlock* BBlock = MRI.getVRegDef(B)->getParent();
Owen Andersonee49b532007-11-06 05:22:43 +0000202
203 if (preorder[ABlock] < preorder[BBlock])
Owen Anderson83430bc2007-11-04 22:33:26 +0000204 return true;
Owen Andersonee49b532007-11-06 05:22:43 +0000205 else if (preorder[ABlock] > preorder[BBlock])
Owen Anderson83430bc2007-11-04 22:33:26 +0000206 return false;
207
Owen Andersonee49b532007-11-06 05:22:43 +0000208 return false;
Owen Anderson83430bc2007-11-04 22:33:26 +0000209 }
210};
211
Owen Anderson8b96b9f2007-11-06 05:26:02 +0000212/// computeDomForest - compute the subforest of the DomTree corresponding
213/// to the defining blocks of the registers in question
Owen Anderson83430bc2007-11-04 22:33:26 +0000214std::vector<StrongPHIElimination::DomForestNode*>
Owen Andersonddd060f2008-01-10 01:36:43 +0000215StrongPHIElimination::computeDomForest(std::set<unsigned>& regs,
216 MachineRegisterInfo& MRI) {
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 Andersonddd060f2008-01-10 01:36:43 +0000230 PreorderSorter PS(preorder, MRI);
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) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000241 unsigned pre = preorder[MRI.getVRegDef(*I)->getParent()];
Owen Andersoncb7d9492008-01-09 06:19:05 +0000242 MachineBasicBlock* parentBlock = CurrentParent->getReg() ?
Owen Andersonddd060f2008-01-10 01:36:43 +0000243 MRI.getVRegDef(CurrentParent->getReg())->getParent() :
Owen Andersoncb7d9492008-01-09 06:19:05 +0000244 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() ?
Owen Andersonddd060f2008-01-10 01:36:43 +0000253 MRI.getVRegDef(CurrentParent->getReg())->getParent() :
Owen Anderson864e3a32008-01-09 10:41:39 +0000254 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 Andersonddd060f2008-01-10 01:36:43 +0000274static bool isLiveIn(unsigned r, MachineBasicBlock* MBB,
275 MachineRegisterInfo& MRI, LiveVariables& LV) {
276 LiveVariables::VarInfo V = LV.getVarInfo(r);
Owen Anderson60a877d2007-11-07 05:17:15 +0000277 if (V.AliveBlocks.test(MBB->getNumber()))
278 return true;
Owen Andersonee49b532007-11-06 05:22:43 +0000279
Owen Andersonddd060f2008-01-10 01:36:43 +0000280 if (MRI.getVRegDef(r)->getParent() != MBB &&
Owen Anderson14b3fb72007-11-08 01:32:45 +0000281 V.UsedBlocks.test(MBB->getNumber()))
282 return true;
Owen Anderson60a877d2007-11-07 05:17:15 +0000283
284 return false;
285}
286
287/// isLiveOut - help method that determines, from a VarInfo, if a register is
288/// live out of a block.
Owen Andersonddd060f2008-01-10 01:36:43 +0000289static bool isLiveOut(unsigned r, MachineBasicBlock* MBB,
290 MachineRegisterInfo& MRI, LiveVariables& LV) {
291 LiveVariables::VarInfo& V = LV.getVarInfo(r);
292 if (MBB == MRI.getVRegDef(r)->getParent() ||
Owen Anderson14b3fb72007-11-08 01:32:45 +0000293 V.UsedBlocks.test(MBB->getNumber())) {
294 for (std::vector<MachineInstr*>::iterator I = V.Kills.begin(),
295 E = V.Kills.end(); I != E; ++I)
296 if ((*I)->getParent() == MBB)
297 return false;
298
Owen Anderson60a877d2007-11-07 05:17:15 +0000299 return true;
Owen Anderson14b3fb72007-11-08 01:32:45 +0000300 }
Owen Anderson60a877d2007-11-07 05:17:15 +0000301
302 return false;
303}
304
Owen Anderson87a702b2007-12-16 05:44:27 +0000305/// interferes - checks for local interferences by scanning a block. The only
306/// trick parameter is 'mode' which tells it the relationship of the two
307/// registers. 0 - defined in the same block, 1 - first properly dominates
308/// second, 2 - second properly dominates first
Owen Andersonb199cbe2008-01-10 00:33:11 +0000309static bool interferes(unsigned a, unsigned b, MachineBasicBlock* scan,
310 LiveVariables& LV, unsigned mode) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000311 MachineInstr* def = 0;
312 MachineInstr* kill = 0;
313
Owen Andersonddd060f2008-01-10 01:36:43 +0000314 // The code is still in SSA form at this point, so there is only one
315 // definition per VReg. Thus we can safely use MRI->getVRegDef().
316 const MachineRegisterInfo* MRI = &scan->getParent()->getRegInfo();
Owen Andersonb199cbe2008-01-10 00:33:11 +0000317
Owen Anderson87a702b2007-12-16 05:44:27 +0000318 bool interference = false;
319
320 // Wallk the block, checking for interferences
321 for (MachineBasicBlock::iterator MBI = scan->begin(), MBE = scan->end();
322 MBI != MBE; ++MBI) {
323 MachineInstr* curr = MBI;
324
325 // Same defining block...
326 if (mode == 0) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000327 if (curr == MRI->getVRegDef(a)) {
328 // If we find our first definition, save it
Owen Anderson87a702b2007-12-16 05:44:27 +0000329 if (!def) {
330 def = curr;
Owen Andersonddd060f2008-01-10 01:36:43 +0000331 // If there's already an unkilled definition, then
Owen Anderson87a702b2007-12-16 05:44:27 +0000332 // this is an interference
333 } else if (!kill) {
334 interference = true;
335 break;
Owen Andersonddd060f2008-01-10 01:36:43 +0000336 // If there's a definition followed by a KillInst, then
Owen Anderson87a702b2007-12-16 05:44:27 +0000337 // they can't interfere
338 } else {
339 interference = false;
340 break;
341 }
342 // Symmetric with the above
Owen Andersonddd060f2008-01-10 01:36:43 +0000343 } else if (curr == MRI->getVRegDef(b)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000344 if (!def) {
345 def = curr;
346 } else if (!kill) {
347 interference = true;
348 break;
349 } else {
350 interference = false;
351 break;
352 }
Owen Andersonddd060f2008-01-10 01:36:43 +0000353 // Store KillInsts if they match up with the definition
Owen Andersonb199cbe2008-01-10 00:33:11 +0000354 } else if (LV.KillsRegister(curr, a)) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000355 if (def == MRI->getVRegDef(a)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000356 kill = curr;
Owen Andersonb199cbe2008-01-10 00:33:11 +0000357 } else if (LV.KillsRegister(curr, b)) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000358 if (def == MRI->getVRegDef(b)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000359 kill = curr;
360 }
361 }
362 }
363 // First properly dominates second...
364 } else if (mode == 1) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000365 if (curr == MRI->getVRegDef(b)) {
366 // Definition of second without kill of first is an interference
Owen Anderson87a702b2007-12-16 05:44:27 +0000367 if (!kill) {
368 interference = true;
369 break;
Owen Andersonddd060f2008-01-10 01:36:43 +0000370 // Definition after a kill is a non-interference
Owen Anderson87a702b2007-12-16 05:44:27 +0000371 } else {
372 interference = false;
373 break;
374 }
375 // Save KillInsts of First
Owen Andersonb199cbe2008-01-10 00:33:11 +0000376 } else if (LV.KillsRegister(curr, a)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000377 kill = curr;
378 }
379 // Symmetric with the above
380 } else if (mode == 2) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000381 if (curr == MRI->getVRegDef(a)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000382 if (!kill) {
383 interference = true;
384 break;
385 } else {
386 interference = false;
387 break;
388 }
Owen Andersonb199cbe2008-01-10 00:33:11 +0000389 } else if (LV.KillsRegister(curr, b)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000390 kill = curr;
391 }
392 }
393 }
394
395 return interference;
396}
397
Owen Andersondc4d6552008-01-10 00:47:01 +0000398/// processBlock - Determine how to break up PHIs in the current block. Each
399/// PHI is broken up by some combination of renaming its operands and inserting
400/// copies. This method is responsible for determining which operands receive
401/// which treatment.
Owen Anderson60a877d2007-11-07 05:17:15 +0000402void StrongPHIElimination::processBlock(MachineBasicBlock* MBB) {
403 LiveVariables& LV = getAnalysis<LiveVariables>();
Owen Andersonddd060f2008-01-10 01:36:43 +0000404 MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo();
Owen Anderson60a877d2007-11-07 05:17:15 +0000405
406 // Holds names that have been added to a set in any PHI within this block
407 // before the current one.
408 std::set<unsigned> ProcessedNames;
409
Owen Andersondc4d6552008-01-10 00:47:01 +0000410 // Iterate over all the PHI nodes in this block
Owen Anderson60a877d2007-11-07 05:17:15 +0000411 MachineBasicBlock::iterator P = MBB->begin();
Owen Anderson864e3a32008-01-09 10:41:39 +0000412 while (P != MBB->end() && P->getOpcode() == TargetInstrInfo::PHI) {
Owen Andersonafc6de02007-12-10 08:07:09 +0000413 unsigned DestReg = P->getOperand(0).getReg();
414
Owen Andersondc4d6552008-01-10 00:47:01 +0000415 // PHIUnion is the set of incoming registers to the PHI node that
416 // are going to be renames rather than having copies inserted. This set
417 // is refinded over the course of this function. UnionedBlocks is the set
418 // of corresponding MBBs.
Owen Anderson60a877d2007-11-07 05:17:15 +0000419 std::set<unsigned> PHIUnion;
420 std::set<MachineBasicBlock*> UnionedBlocks;
421
Owen Andersondc4d6552008-01-10 00:47:01 +0000422 // Iterate over the operands of the PHI node
Owen Anderson60a877d2007-11-07 05:17:15 +0000423 for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
424 unsigned SrcReg = P->getOperand(i-1).getReg();
Owen Anderson60a877d2007-11-07 05:17:15 +0000425
Owen Andersondc4d6552008-01-10 00:47:01 +0000426 // Check for trivial interferences via liveness information, allowing us
427 // to avoid extra work later. Any registers that interfere cannot both
428 // be in the renaming set, so choose one and add copies for it instead.
429 // The conditions are:
430 // 1) if the operand is live into the PHI node's block OR
431 // 2) if the PHI node is live out of the operand's defining block OR
432 // 3) if the operand is itself a PHI node and the original PHI is
433 // live into the operand's defining block OR
434 // 4) if the operand is already being renamed for another PHI node
435 // in this block OR
436 // 5) if any two operands are defined in the same block, insert copies
437 // for one of them
Owen Andersonddd060f2008-01-10 01:36:43 +0000438 if (isLiveIn(SrcReg, P->getParent(), MRI, LV) ||
439 isLiveOut(P->getOperand(0).getReg(),
440 MRI.getVRegDef(SrcReg)->getParent(), MRI, LV) ||
441 ( MRI.getVRegDef(SrcReg)->getOpcode() == TargetInstrInfo::PHI &&
442 isLiveIn(P->getOperand(0).getReg(),
443 MRI.getVRegDef(SrcReg)->getParent(), MRI, LV) ) ||
Owen Andersonafc6de02007-12-10 08:07:09 +0000444 ProcessedNames.count(SrcReg) ||
Owen Andersonddd060f2008-01-10 01:36:43 +0000445 UnionedBlocks.count(MRI.getVRegDef(SrcReg)->getParent())) {
Owen Andersonafc6de02007-12-10 08:07:09 +0000446
Owen Andersondc4d6552008-01-10 00:47:01 +0000447 // Add a copy for the selected register
Chris Lattner8aa797a2007-12-30 23:10:15 +0000448 MachineBasicBlock* From = P->getOperand(i).getMBB();
Owen Andersonefbcebc2007-12-23 15:37:26 +0000449 Waiting[From].insert(std::make_pair(SrcReg, DestReg));
450 UsedByAnother.insert(SrcReg);
Owen Anderson60a877d2007-11-07 05:17:15 +0000451 } else {
Owen Andersondc4d6552008-01-10 00:47:01 +0000452 // Otherwise, add it to the renaming set
Owen Anderson60a877d2007-11-07 05:17:15 +0000453 PHIUnion.insert(SrcReg);
Owen Andersonddd060f2008-01-10 01:36:43 +0000454 UnionedBlocks.insert(MRI.getVRegDef(SrcReg)->getParent());
Owen Anderson60a877d2007-11-07 05:17:15 +0000455 }
Owen Anderson60a877d2007-11-07 05:17:15 +0000456 }
457
Owen Andersondc4d6552008-01-10 00:47:01 +0000458 // Compute the dominator forest for the renaming set. This is a forest
459 // where the nodes are the registers and the edges represent dominance
460 // relations between the defining blocks of the registers
Owen Anderson42f9e962007-11-13 20:13:24 +0000461 std::vector<StrongPHIElimination::DomForestNode*> DF =
Owen Andersonddd060f2008-01-10 01:36:43 +0000462 computeDomForest(PHIUnion, MRI);
Owen Anderson42f9e962007-11-13 20:13:24 +0000463
Owen Andersondc4d6552008-01-10 00:47:01 +0000464 // Walk DomForest to resolve interferences at an inter-block level. This
465 // will remove registers from the renaming set (and insert copies for them)
466 // if interferences are found.
Owen Anderson62d67dd2007-12-13 05:53:03 +0000467 std::vector<std::pair<unsigned, unsigned> > localInterferences;
468 processPHIUnion(P, PHIUnion, DF, localInterferences);
469
Owen Andersondc4d6552008-01-10 00:47:01 +0000470 // The dominator forest walk may have returned some register pairs whose
471 // interference cannot be determines from dominator analysis. We now
472 // examine these pairs for local interferences.
Owen Anderson87a702b2007-12-16 05:44:27 +0000473 for (std::vector<std::pair<unsigned, unsigned> >::iterator I =
474 localInterferences.begin(), E = localInterferences.end(); I != E; ++I) {
475 std::pair<unsigned, unsigned> p = *I;
476
Owen Anderson87a702b2007-12-16 05:44:27 +0000477 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
478
479 // Determine the block we need to scan and the relationship between
480 // the two registers
481 MachineBasicBlock* scan = 0;
482 unsigned mode = 0;
Owen Andersonddd060f2008-01-10 01:36:43 +0000483 if (MRI.getVRegDef(p.first)->getParent() ==
484 MRI.getVRegDef(p.second)->getParent()) {
485 scan = MRI.getVRegDef(p.first)->getParent();
Owen Anderson87a702b2007-12-16 05:44:27 +0000486 mode = 0; // Same block
Owen Andersonddd060f2008-01-10 01:36:43 +0000487 } else if (MDT.dominates(MRI.getVRegDef(p.first)->getParent(),
488 MRI.getVRegDef(p.second)->getParent())) {
489 scan = MRI.getVRegDef(p.second)->getParent();
Owen Anderson87a702b2007-12-16 05:44:27 +0000490 mode = 1; // First dominates second
491 } else {
Owen Andersonddd060f2008-01-10 01:36:43 +0000492 scan = MRI.getVRegDef(p.first)->getParent();
Owen Anderson87a702b2007-12-16 05:44:27 +0000493 mode = 2; // Second dominates first
494 }
495
496 // If there's an interference, we need to insert copies
Owen Andersonb199cbe2008-01-10 00:33:11 +0000497 if (interferes(p.first, p.second, scan, LV, mode)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000498 // Insert copies for First
499 for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
500 if (P->getOperand(i-1).getReg() == p.first) {
501 unsigned SrcReg = p.first;
502 MachineBasicBlock* From = P->getOperand(i).getMBB();
503
Owen Andersonefbcebc2007-12-23 15:37:26 +0000504 Waiting[From].insert(std::make_pair(SrcReg,
505 P->getOperand(0).getReg()));
506 UsedByAnother.insert(SrcReg);
507
Owen Anderson87a702b2007-12-16 05:44:27 +0000508 PHIUnion.erase(SrcReg);
509 }
510 }
511 }
512 }
Owen Anderson42f9e962007-11-13 20:13:24 +0000513
Owen Andersondc4d6552008-01-10 00:47:01 +0000514 // Add the renaming set for this PHI node to our overal renaming information
Owen Anderson0c5714b2008-01-08 21:54:52 +0000515 RenameSets.insert(std::make_pair(P->getOperand(0).getReg(), PHIUnion));
Owen Andersoncae8d8d2007-12-22 04:59:10 +0000516
Owen Andersondc4d6552008-01-10 00:47:01 +0000517 // Remember which registers are already renamed, so that we don't try to
518 // rename them for another PHI node in this block
Owen Anderson42f9e962007-11-13 20:13:24 +0000519 ProcessedNames.insert(PHIUnion.begin(), PHIUnion.end());
Owen Andersondc4d6552008-01-10 00:47:01 +0000520
Owen Anderson60a877d2007-11-07 05:17:15 +0000521 ++P;
522 }
Owen Andersonee49b532007-11-06 05:22:43 +0000523}
524
Owen Anderson965b4672007-12-16 04:07:23 +0000525/// processPHIUnion - Take a set of candidate registers to be coallesced when
526/// decomposing the PHI instruction. Use the DominanceForest to remove the ones
527/// that are known to interfere, and flag others that need to be checked for
528/// local interferences.
Owen Andersond525f662007-12-11 20:12:11 +0000529void StrongPHIElimination::processPHIUnion(MachineInstr* Inst,
530 std::set<unsigned>& PHIUnion,
Owen Anderson62d67dd2007-12-13 05:53:03 +0000531 std::vector<StrongPHIElimination::DomForestNode*>& DF,
532 std::vector<std::pair<unsigned, unsigned> >& locals) {
Owen Andersond525f662007-12-11 20:12:11 +0000533
534 std::vector<DomForestNode*> worklist(DF.begin(), DF.end());
535 SmallPtrSet<DomForestNode*, 4> visited;
536
Owen Andersonddd060f2008-01-10 01:36:43 +0000537 // Code is still in SSA form, so we can use MRI::getVRegDef()
538 MachineRegisterInfo& MRI = Inst->getParent()->getParent()->getRegInfo();
539
Owen Andersond525f662007-12-11 20:12:11 +0000540 LiveVariables& LV = getAnalysis<LiveVariables>();
541 unsigned DestReg = Inst->getOperand(0).getReg();
542
Owen Anderson965b4672007-12-16 04:07:23 +0000543 // DF walk on the DomForest
Owen Andersond525f662007-12-11 20:12:11 +0000544 while (!worklist.empty()) {
545 DomForestNode* DFNode = worklist.back();
546
Owen Andersond525f662007-12-11 20:12:11 +0000547 visited.insert(DFNode);
548
549 bool inserted = false;
Owen Andersond525f662007-12-11 20:12:11 +0000550 for (DomForestNode::iterator CI = DFNode->begin(), CE = DFNode->end();
551 CI != CE; ++CI) {
552 DomForestNode* child = *CI;
Owen Anderson3b489522008-01-21 22:01:01 +0000553
554 // If the current node is live-out of the defining block of one of its
555 // children, insert a copy for it
Owen Andersonddd060f2008-01-10 01:36:43 +0000556 if (isLiveOut(DFNode->getReg(),
557 MRI.getVRegDef(child->getReg())->getParent(), MRI, LV)) {
Owen Andersond525f662007-12-11 20:12:11 +0000558 // Insert copies for parent
559 for (int i = Inst->getNumOperands() - 1; i >= 2; i-=2) {
560 if (Inst->getOperand(i-1).getReg() == DFNode->getReg()) {
Owen Andersoned2ffa22007-12-12 01:25:08 +0000561 unsigned SrcReg = DFNode->getReg();
Owen Andersond525f662007-12-11 20:12:11 +0000562 MachineBasicBlock* From = Inst->getOperand(i).getMBB();
563
Owen Andersonefbcebc2007-12-23 15:37:26 +0000564 Waiting[From].insert(std::make_pair(SrcReg, DestReg));
565 UsedByAnother.insert(SrcReg);
566
Owen Andersoned2ffa22007-12-12 01:25:08 +0000567 PHIUnion.erase(SrcReg);
Owen Andersond525f662007-12-11 20:12:11 +0000568 }
569 }
Owen Anderson3b489522008-01-21 22:01:01 +0000570
571 // If a node is live-in to the defining block of one of its children, but
572 // not live-out, then we need to scan that block for local interferences.
Owen Andersonddd060f2008-01-10 01:36:43 +0000573 } else if (isLiveIn(DFNode->getReg(),
574 MRI.getVRegDef(child->getReg())->getParent(),
575 MRI, LV) ||
576 MRI.getVRegDef(DFNode->getReg())->getParent() ==
577 MRI.getVRegDef(child->getReg())->getParent()) {
Owen Anderson62d67dd2007-12-13 05:53:03 +0000578 // Add (p, c) to possible local interferences
579 locals.push_back(std::make_pair(DFNode->getReg(), child->getReg()));
Owen Andersond525f662007-12-11 20:12:11 +0000580 }
Owen Anderson965b4672007-12-16 04:07:23 +0000581
Owen Anderson4ba08ec2007-12-13 05:43:37 +0000582 if (!visited.count(child)) {
583 worklist.push_back(child);
584 inserted = true;
Owen Andersond525f662007-12-11 20:12:11 +0000585 }
586 }
587
588 if (!inserted) worklist.pop_back();
589 }
590}
591
Owen Andersonefbcebc2007-12-23 15:37:26 +0000592/// ScheduleCopies - Insert copies into predecessor blocks, scheduling
593/// them properly so as to avoid the 'lost copy' and the 'virtual swap'
594/// problems.
595///
596/// Based on "Practical Improvements to the Construction and Destruction
597/// of Static Single Assignment Form" by Briggs, et al.
Owen Andersonf1519e82007-12-24 22:12:23 +0000598void StrongPHIElimination::ScheduleCopies(MachineBasicBlock* MBB,
599 std::set<unsigned>& pushed) {
Owen Anderson0d893b42008-01-08 05:16:15 +0000600 // FIXME: This function needs to update LiveVariables
Owen Andersonefbcebc2007-12-23 15:37:26 +0000601 std::map<unsigned, unsigned>& copy_set= Waiting[MBB];
602
603 std::map<unsigned, unsigned> worklist;
604 std::map<unsigned, unsigned> map;
605
606 // Setup worklist of initial copies
607 for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(),
608 E = copy_set.end(); I != E; ) {
609 map.insert(std::make_pair(I->first, I->first));
610 map.insert(std::make_pair(I->second, I->second));
611
612 if (!UsedByAnother.count(I->first)) {
613 worklist.insert(*I);
614
615 // Avoid iterator invalidation
616 unsigned first = I->first;
617 ++I;
618 copy_set.erase(first);
619 } else {
620 ++I;
621 }
622 }
623
624 LiveVariables& LV = getAnalysis<LiveVariables>();
Owen Anderson0d893b42008-01-08 05:16:15 +0000625 MachineFunction* MF = MBB->getParent();
Owen Andersonddd060f2008-01-10 01:36:43 +0000626 MachineRegisterInfo& MRI = MF->getRegInfo();
Owen Anderson0d893b42008-01-08 05:16:15 +0000627 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
Owen Andersonefbcebc2007-12-23 15:37:26 +0000628
629 // Iterate over the worklist, inserting copies
630 while (!worklist.empty() || !copy_set.empty()) {
631 while (!worklist.empty()) {
632 std::pair<unsigned, unsigned> curr = *worklist.begin();
633 worklist.erase(curr.first);
634
Owen Anderson0d893b42008-01-08 05:16:15 +0000635 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first);
636
Owen Andersonddd060f2008-01-10 01:36:43 +0000637 if (isLiveOut(curr.second, MBB, MRI, LV)) {
Owen Anderson0d893b42008-01-08 05:16:15 +0000638 // Create a temporary
639 unsigned t = MF->getRegInfo().createVirtualRegister(RC);
640
641 // Insert copy from curr.second to a temporary at
642 // the Phi defining curr.second
Owen Andersonddd060f2008-01-10 01:36:43 +0000643 MachineBasicBlock::iterator PI = MRI.getVRegDef(curr.second);
644 TII->copyRegToReg(*PI->getParent(), PI, t,
Owen Anderson0d893b42008-01-08 05:16:15 +0000645 curr.second, RC, RC);
646
Owen Andersonefbcebc2007-12-23 15:37:26 +0000647 // Push temporary on Stacks
Owen Anderson0d893b42008-01-08 05:16:15 +0000648 Stacks[curr.second].push_back(t);
649
650 // Insert curr.second in pushed
651 pushed.insert(curr.second);
Owen Andersonefbcebc2007-12-23 15:37:26 +0000652 }
653
654 // Insert copy from map[curr.first] to curr.second
Owen Anderson9c2efa82008-01-10 00:01:41 +0000655 TII->copyRegToReg(*MBB, MBB->getFirstTerminator(), curr.second,
Owen Anderson0d893b42008-01-08 05:16:15 +0000656 map[curr.first], RC, RC);
Owen Andersonefbcebc2007-12-23 15:37:26 +0000657 map[curr.first] = curr.second;
658
659 // If curr.first is a destination in copy_set...
660 for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(),
661 E = copy_set.end(); I != E; )
662 if (curr.first == I->second) {
663 std::pair<unsigned, unsigned> temp = *I;
664
665 // Avoid iterator invalidation
666 ++I;
667 copy_set.erase(temp.first);
668 worklist.insert(temp);
669
670 break;
671 } else {
672 ++I;
673 }
674 }
675
676 if (!copy_set.empty()) {
677 std::pair<unsigned, unsigned> curr = *copy_set.begin();
678 copy_set.erase(curr.first);
679
Owen Anderson0d893b42008-01-08 05:16:15 +0000680 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first);
681
Owen Andersonefbcebc2007-12-23 15:37:26 +0000682 // Insert a copy from dest to a new temporary t at the end of b
Owen Anderson0d893b42008-01-08 05:16:15 +0000683 unsigned t = MF->getRegInfo().createVirtualRegister(RC);
Owen Anderson9c2efa82008-01-10 00:01:41 +0000684 TII->copyRegToReg(*MBB, MBB->getFirstTerminator(), t,
Owen Anderson0d893b42008-01-08 05:16:15 +0000685 curr.second, RC, RC);
686 map[curr.second] = t;
Owen Andersonefbcebc2007-12-23 15:37:26 +0000687
688 worklist.insert(curr);
689 }
690 }
691}
692
Owen Andersonf1519e82007-12-24 22:12:23 +0000693/// InsertCopies - insert copies into MBB and all of its successors
Owen Anderson719fef62008-01-09 10:32:30 +0000694void StrongPHIElimination::InsertCopies(MachineBasicBlock* MBB,
695 std::set<MachineBasicBlock*>& visited) {
696 visited.insert(MBB);
697
Owen Andersonf1519e82007-12-24 22:12:23 +0000698 std::set<unsigned> pushed;
699
700 // Rewrite register uses from Stacks
701 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
702 I != E; ++I)
703 for (unsigned i = 0; i < I->getNumOperands(); ++i)
704 if (I->getOperand(i).isRegister() &&
705 Stacks[I->getOperand(i).getReg()].size()) {
706 I->getOperand(i).setReg(Stacks[I->getOperand(i).getReg()].back());
707 }
708
709 // Schedule the copies for this block
710 ScheduleCopies(MBB, pushed);
711
712 // Recur to our successors
713 for (GraphTraits<MachineBasicBlock*>::ChildIteratorType I =
714 GraphTraits<MachineBasicBlock*>::child_begin(MBB), E =
715 GraphTraits<MachineBasicBlock*>::child_end(MBB); I != E; ++I)
Owen Anderson719fef62008-01-09 10:32:30 +0000716 if (!visited.count(*I))
717 InsertCopies(*I, visited);
Owen Andersonf1519e82007-12-24 22:12:23 +0000718
719 // As we exit this block, pop the names we pushed while processing it
720 for (std::set<unsigned>::iterator I = pushed.begin(),
721 E = pushed.end(); I != E; ++I)
722 Stacks[*I].pop_back();
723}
724
Owen Andersona4ad2e72007-11-06 04:49:43 +0000725bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
Owen Andersonefbcebc2007-12-23 15:37:26 +0000726 // Compute DFS numbers of each block
Owen Andersona4ad2e72007-11-06 04:49:43 +0000727 computeDFS(Fn);
728
Owen Andersonefbcebc2007-12-23 15:37:26 +0000729 // Determine which phi node operands need copies
Owen Anderson60a877d2007-11-07 05:17:15 +0000730 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
731 if (!I->empty() &&
732 I->begin()->getOpcode() == TargetInstrInfo::PHI)
733 processBlock(I);
Owen Andersona4ad2e72007-11-06 04:49:43 +0000734
Owen Andersonefbcebc2007-12-23 15:37:26 +0000735 // Insert copies
Owen Andersonf1519e82007-12-24 22:12:23 +0000736 // FIXME: This process should probably preserve LiveVariables
Owen Anderson719fef62008-01-09 10:32:30 +0000737 std::set<MachineBasicBlock*> visited;
738 InsertCopies(Fn.begin(), visited);
Owen Andersonefbcebc2007-12-23 15:37:26 +0000739
Owen Anderson0c5714b2008-01-08 21:54:52 +0000740 // Perform renaming
741 typedef std::map<unsigned, std::set<unsigned> > RenameSetType;
742 for (RenameSetType::iterator I = RenameSets.begin(), E = RenameSets.end();
743 I != E; ++I)
744 for (std::set<unsigned>::iterator SI = I->second.begin(),
745 SE = I->second.end(); SI != SE; ++SI)
746 Fn.getRegInfo().replaceRegWith(*SI, I->first);
747
748 // FIXME: Insert last-minute copies
749
750 // Remove PHIs
751 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
752 for (MachineBasicBlock::iterator BI = I->begin(), BE = I->end();
753 BI != BE; ++BI)
754 if (BI->getOpcode() == TargetInstrInfo::PHI)
755 BI->eraseFromParent();
Owen Andersoncae8d8d2007-12-22 04:59:10 +0000756
Owen Andersona4ad2e72007-11-06 04:49:43 +0000757 return false;
758}