blob: 6467afe28f278ce27f1025a14045bc63aeb1c2c9 [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 Andersoneb37ecc2008-03-10 07:22:36 +000024#include "llvm/CodeGen/LiveIntervalAnalysis.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
Owen Anderson00316712008-03-12 03:13:29 +000054 // RenameSets are the sets of operands (and their VNInfo IDs) to a PHI
55 // (the defining instruction of the key) that can be renamed without copies
56 std::map<unsigned, std::map<unsigned, 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 Andersoneb37ecc2008-03-10 07:22:36 +000068 AU.addRequired<LiveIntervals>();
69
70 // TODO: Actually make this true.
71 AU.addPreserved<LiveIntervals>();
Owen Anderson0bda0e82007-10-31 03:37:57 +000072 MachineFunctionPass::getAnalysisUsage(AU);
73 }
74
75 virtual void releaseMemory() {
76 preorder.clear();
77 maxpreorder.clear();
Owen Andersona4ad2e72007-11-06 04:49:43 +000078
Owen Andersonefbcebc2007-12-23 15:37:26 +000079 Waiting.clear();
Owen Andersonec1213f2008-01-09 22:40:54 +000080 Stacks.clear();
81 UsedByAnother.clear();
82 RenameSets.clear();
Owen Anderson0bda0e82007-10-31 03:37:57 +000083 }
84
85 private:
Owen Andersonec1213f2008-01-09 22:40:54 +000086
87 /// DomForestNode - Represents a node in the "dominator forest". This is
88 /// a forest in which the nodes represent registers and the edges
89 /// represent a dominance relation in the block defining those registers.
Owen Anderson83430bc2007-11-04 22:33:26 +000090 struct DomForestNode {
91 private:
Owen Andersonec1213f2008-01-09 22:40:54 +000092 // Store references to our children
Owen Anderson83430bc2007-11-04 22:33:26 +000093 std::vector<DomForestNode*> children;
Owen Andersonec1213f2008-01-09 22:40:54 +000094 // The register we represent
Owen Andersonee49b532007-11-06 05:22:43 +000095 unsigned reg;
Owen Anderson83430bc2007-11-04 22:33:26 +000096
Owen Andersonec1213f2008-01-09 22:40:54 +000097 // Add another node as our child
Owen Anderson83430bc2007-11-04 22:33:26 +000098 void addChild(DomForestNode* DFN) { children.push_back(DFN); }
99
100 public:
101 typedef std::vector<DomForestNode*>::iterator iterator;
102
Owen Andersonec1213f2008-01-09 22:40:54 +0000103 // Create a DomForestNode by providing the register it represents, and
104 // the node to be its parent. The virtual root node has register 0
105 // and a null parent.
Owen Andersonee49b532007-11-06 05:22:43 +0000106 DomForestNode(unsigned r, DomForestNode* parent) : reg(r) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000107 if (parent)
108 parent->addChild(this);
109 }
110
Owen Andersona4ad2e72007-11-06 04:49:43 +0000111 ~DomForestNode() {
112 for (iterator I = begin(), E = end(); I != E; ++I)
113 delete *I;
114 }
Owen Anderson83430bc2007-11-04 22:33:26 +0000115
Owen Andersonec1213f2008-01-09 22:40:54 +0000116 /// getReg - Return the regiser that this node represents
Owen Andersonee49b532007-11-06 05:22:43 +0000117 inline unsigned getReg() { return reg; }
Owen Andersona4ad2e72007-11-06 04:49:43 +0000118
Owen Andersonec1213f2008-01-09 22:40:54 +0000119 // Provide iterator access to our children
Owen Andersona4ad2e72007-11-06 04:49:43 +0000120 inline DomForestNode::iterator begin() { return children.begin(); }
121 inline DomForestNode::iterator end() { return children.end(); }
Owen Anderson83430bc2007-11-04 22:33:26 +0000122 };
123
Owen Anderson0bda0e82007-10-31 03:37:57 +0000124 void computeDFS(MachineFunction& MF);
Owen Anderson60a877d2007-11-07 05:17:15 +0000125 void processBlock(MachineBasicBlock* MBB);
Owen Anderson83430bc2007-11-04 22:33:26 +0000126
Owen Anderson00316712008-03-12 03:13:29 +0000127 std::vector<DomForestNode*> computeDomForest(std::map<unsigned, unsigned>& instrs,
Owen Andersonddd060f2008-01-10 01:36:43 +0000128 MachineRegisterInfo& MRI);
Owen Andersond525f662007-12-11 20:12:11 +0000129 void processPHIUnion(MachineInstr* Inst,
Owen Anderson00316712008-03-12 03:13:29 +0000130 std::map<unsigned, unsigned>& PHIUnion,
Owen Anderson62d67dd2007-12-13 05:53:03 +0000131 std::vector<StrongPHIElimination::DomForestNode*>& DF,
132 std::vector<std::pair<unsigned, unsigned> >& locals);
Owen Andersonf1519e82007-12-24 22:12:23 +0000133 void ScheduleCopies(MachineBasicBlock* MBB, std::set<unsigned>& pushed);
Owen Anderson719fef62008-01-09 10:32:30 +0000134 void InsertCopies(MachineBasicBlock* MBB, std::set<MachineBasicBlock*>& v);
Owen Anderson00316712008-03-12 03:13:29 +0000135 void mergeLiveIntervals(unsigned primary, unsigned secondary, unsigned VN);
Owen Anderson0bda0e82007-10-31 03:37:57 +0000136 };
137
138 char StrongPHIElimination::ID = 0;
139 RegisterPass<StrongPHIElimination> X("strong-phi-node-elimination",
140 "Eliminate PHI nodes for register allocation, intelligently");
141}
142
143const PassInfo *llvm::StrongPHIEliminationID = X.getPassInfo();
144
145/// computeDFS - Computes the DFS-in and DFS-out numbers of the dominator tree
146/// of the given MachineFunction. These numbers are then used in other parts
147/// of the PHI elimination process.
148void StrongPHIElimination::computeDFS(MachineFunction& MF) {
149 SmallPtrSet<MachineDomTreeNode*, 8> frontier;
150 SmallPtrSet<MachineDomTreeNode*, 8> visited;
151
152 unsigned time = 0;
153
154 MachineDominatorTree& DT = getAnalysis<MachineDominatorTree>();
155
156 MachineDomTreeNode* node = DT.getRootNode();
157
158 std::vector<MachineDomTreeNode*> worklist;
159 worklist.push_back(node);
160
161 while (!worklist.empty()) {
162 MachineDomTreeNode* currNode = worklist.back();
163
164 if (!frontier.count(currNode)) {
165 frontier.insert(currNode);
166 ++time;
167 preorder.insert(std::make_pair(currNode->getBlock(), time));
168 }
169
170 bool inserted = false;
171 for (MachineDomTreeNode::iterator I = node->begin(), E = node->end();
172 I != E; ++I)
173 if (!frontier.count(*I) && !visited.count(*I)) {
174 worklist.push_back(*I);
175 inserted = true;
176 break;
177 }
178
179 if (!inserted) {
180 frontier.erase(currNode);
181 visited.insert(currNode);
182 maxpreorder.insert(std::make_pair(currNode->getBlock(), time));
183
184 worklist.pop_back();
185 }
186 }
Duncan Sands1bd32712007-10-31 08:49:24 +0000187}
Owen Anderson83430bc2007-11-04 22:33:26 +0000188
Owen Anderson8b96b9f2007-11-06 05:26:02 +0000189/// PreorderSorter - a helper class that is used to sort registers
190/// according to the preorder number of their defining blocks
Owen Anderson83430bc2007-11-04 22:33:26 +0000191class PreorderSorter {
192private:
193 DenseMap<MachineBasicBlock*, unsigned>& preorder;
Owen Andersonddd060f2008-01-10 01:36:43 +0000194 MachineRegisterInfo& MRI;
Owen Anderson83430bc2007-11-04 22:33:26 +0000195
196public:
Owen Andersonee49b532007-11-06 05:22:43 +0000197 PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p,
Owen Andersonddd060f2008-01-10 01:36:43 +0000198 MachineRegisterInfo& M) : preorder(p), MRI(M) { }
Owen Anderson83430bc2007-11-04 22:33:26 +0000199
Owen Andersonee49b532007-11-06 05:22:43 +0000200 bool operator()(unsigned A, unsigned B) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000201 if (A == B)
202 return false;
203
Owen Andersonddd060f2008-01-10 01:36:43 +0000204 MachineBasicBlock* ABlock = MRI.getVRegDef(A)->getParent();
205 MachineBasicBlock* BBlock = MRI.getVRegDef(B)->getParent();
Owen Andersonee49b532007-11-06 05:22:43 +0000206
207 if (preorder[ABlock] < preorder[BBlock])
Owen Anderson83430bc2007-11-04 22:33:26 +0000208 return true;
Owen Andersonee49b532007-11-06 05:22:43 +0000209 else if (preorder[ABlock] > preorder[BBlock])
Owen Anderson83430bc2007-11-04 22:33:26 +0000210 return false;
211
Owen Andersonee49b532007-11-06 05:22:43 +0000212 return false;
Owen Anderson83430bc2007-11-04 22:33:26 +0000213 }
214};
215
Owen Anderson8b96b9f2007-11-06 05:26:02 +0000216/// computeDomForest - compute the subforest of the DomTree corresponding
217/// to the defining blocks of the registers in question
Owen Anderson83430bc2007-11-04 22:33:26 +0000218std::vector<StrongPHIElimination::DomForestNode*>
Owen Anderson00316712008-03-12 03:13:29 +0000219StrongPHIElimination::computeDomForest(std::map<unsigned, unsigned>& regs,
Owen Andersonddd060f2008-01-10 01:36:43 +0000220 MachineRegisterInfo& MRI) {
Owen Andersonec1213f2008-01-09 22:40:54 +0000221 // Begin by creating a virtual root node, since the actual results
222 // may well be a forest. Assume this node has maximum DFS-out number.
Owen Anderson83430bc2007-11-04 22:33:26 +0000223 DomForestNode* VirtualRoot = new DomForestNode(0, 0);
224 maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL));
225
Owen Andersonec1213f2008-01-09 22:40:54 +0000226 // Populate a worklist with the registers
Owen Andersonee49b532007-11-06 05:22:43 +0000227 std::vector<unsigned> worklist;
228 worklist.reserve(regs.size());
Owen Anderson00316712008-03-12 03:13:29 +0000229 for (std::map<unsigned, unsigned>::iterator I = regs.begin(), E = regs.end();
Owen Andersonee49b532007-11-06 05:22:43 +0000230 I != E; ++I)
Owen Anderson00316712008-03-12 03:13:29 +0000231 worklist.push_back(I->first);
Owen Andersonee49b532007-11-06 05:22:43 +0000232
Owen Andersonec1213f2008-01-09 22:40:54 +0000233 // Sort the registers by the DFS-in number of their defining block
Owen Andersonddd060f2008-01-10 01:36:43 +0000234 PreorderSorter PS(preorder, MRI);
Owen Anderson83430bc2007-11-04 22:33:26 +0000235 std::sort(worklist.begin(), worklist.end(), PS);
236
Owen Andersonec1213f2008-01-09 22:40:54 +0000237 // Create a "current parent" stack, and put the virtual root on top of it
Owen Anderson83430bc2007-11-04 22:33:26 +0000238 DomForestNode* CurrentParent = VirtualRoot;
239 std::vector<DomForestNode*> stack;
240 stack.push_back(VirtualRoot);
241
Owen Andersonec1213f2008-01-09 22:40:54 +0000242 // Iterate over all the registers in the previously computed order
Owen Andersonee49b532007-11-06 05:22:43 +0000243 for (std::vector<unsigned>::iterator I = worklist.begin(), E = worklist.end();
244 I != E; ++I) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000245 unsigned pre = preorder[MRI.getVRegDef(*I)->getParent()];
Owen Andersoncb7d9492008-01-09 06:19:05 +0000246 MachineBasicBlock* parentBlock = CurrentParent->getReg() ?
Owen Andersonddd060f2008-01-10 01:36:43 +0000247 MRI.getVRegDef(CurrentParent->getReg())->getParent() :
Owen Andersoncb7d9492008-01-09 06:19:05 +0000248 0;
Owen Andersonee49b532007-11-06 05:22:43 +0000249
Owen Andersonec1213f2008-01-09 22:40:54 +0000250 // If the DFS-in number of the register is greater than the DFS-out number
251 // of the current parent, repeatedly pop the parent stack until it isn't.
Owen Andersonee49b532007-11-06 05:22:43 +0000252 while (pre > maxpreorder[parentBlock]) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000253 stack.pop_back();
254 CurrentParent = stack.back();
Owen Andersonee49b532007-11-06 05:22:43 +0000255
Owen Anderson864e3a32008-01-09 10:41:39 +0000256 parentBlock = CurrentParent->getReg() ?
Owen Andersonddd060f2008-01-10 01:36:43 +0000257 MRI.getVRegDef(CurrentParent->getReg())->getParent() :
Owen Anderson864e3a32008-01-09 10:41:39 +0000258 0;
Owen Anderson83430bc2007-11-04 22:33:26 +0000259 }
260
Owen Andersonec1213f2008-01-09 22:40:54 +0000261 // Now that we've found the appropriate parent, create a DomForestNode for
262 // this register and attach it to the forest
Owen Anderson83430bc2007-11-04 22:33:26 +0000263 DomForestNode* child = new DomForestNode(*I, CurrentParent);
Owen Andersonec1213f2008-01-09 22:40:54 +0000264
265 // Push this new node on the "current parent" stack
Owen Anderson83430bc2007-11-04 22:33:26 +0000266 stack.push_back(child);
267 CurrentParent = child;
268 }
269
Owen Andersonec1213f2008-01-09 22:40:54 +0000270 // Return a vector containing the children of the virtual root node
Owen Anderson83430bc2007-11-04 22:33:26 +0000271 std::vector<DomForestNode*> ret;
272 ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end());
273 return ret;
274}
Owen Andersona4ad2e72007-11-06 04:49:43 +0000275
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000276/// isLiveIn - helper method that determines, from a regno, if a register
Owen Anderson60a877d2007-11-07 05:17:15 +0000277/// is live into a block
Owen Andersonddd060f2008-01-10 01:36:43 +0000278static bool isLiveIn(unsigned r, MachineBasicBlock* MBB,
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000279 LiveIntervals& LI) {
280 LiveInterval& I = LI.getOrCreateInterval(r);
281 unsigned idx = LI.getMBBStartIdx(MBB);
282 return I.liveBeforeAndAt(idx);
Owen Anderson60a877d2007-11-07 05:17:15 +0000283}
284
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000285/// isLiveOut - help method that determines, from a regno, if a register is
Owen Anderson60a877d2007-11-07 05:17:15 +0000286/// live out of a block.
Owen Andersonddd060f2008-01-10 01:36:43 +0000287static bool isLiveOut(unsigned r, MachineBasicBlock* MBB,
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000288 LiveIntervals& LI) {
289 for (MachineBasicBlock::succ_iterator PI = MBB->succ_begin(),
290 E = MBB->succ_end(); PI != E; ++PI) {
291 if (isLiveIn(r, *PI, LI))
292 return true;
Owen Anderson14b3fb72007-11-08 01:32:45 +0000293 }
Owen Anderson60a877d2007-11-07 05:17:15 +0000294
295 return false;
296}
297
Owen Anderson87a702b2007-12-16 05:44:27 +0000298/// interferes - checks for local interferences by scanning a block. The only
299/// trick parameter is 'mode' which tells it the relationship of the two
300/// registers. 0 - defined in the same block, 1 - first properly dominates
301/// second, 2 - second properly dominates first
Owen Andersonb199cbe2008-01-10 00:33:11 +0000302static bool interferes(unsigned a, unsigned b, MachineBasicBlock* scan,
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000303 LiveIntervals& LV, unsigned mode) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000304 MachineInstr* def = 0;
305 MachineInstr* kill = 0;
306
Owen Andersonddd060f2008-01-10 01:36:43 +0000307 // The code is still in SSA form at this point, so there is only one
308 // definition per VReg. Thus we can safely use MRI->getVRegDef().
309 const MachineRegisterInfo* MRI = &scan->getParent()->getRegInfo();
Owen Andersonb199cbe2008-01-10 00:33:11 +0000310
Owen Anderson87a702b2007-12-16 05:44:27 +0000311 bool interference = false;
312
313 // Wallk the block, checking for interferences
314 for (MachineBasicBlock::iterator MBI = scan->begin(), MBE = scan->end();
315 MBI != MBE; ++MBI) {
316 MachineInstr* curr = MBI;
317
318 // Same defining block...
319 if (mode == 0) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000320 if (curr == MRI->getVRegDef(a)) {
321 // If we find our first definition, save it
Owen Anderson87a702b2007-12-16 05:44:27 +0000322 if (!def) {
323 def = curr;
Owen Andersonddd060f2008-01-10 01:36:43 +0000324 // If there's already an unkilled definition, then
Owen Anderson87a702b2007-12-16 05:44:27 +0000325 // this is an interference
326 } else if (!kill) {
327 interference = true;
328 break;
Owen Andersonddd060f2008-01-10 01:36:43 +0000329 // If there's a definition followed by a KillInst, then
Owen Anderson87a702b2007-12-16 05:44:27 +0000330 // they can't interfere
331 } else {
332 interference = false;
333 break;
334 }
335 // Symmetric with the above
Owen Andersonddd060f2008-01-10 01:36:43 +0000336 } else if (curr == MRI->getVRegDef(b)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000337 if (!def) {
338 def = curr;
339 } else if (!kill) {
340 interference = true;
341 break;
342 } else {
343 interference = false;
344 break;
345 }
Owen Andersonddd060f2008-01-10 01:36:43 +0000346 // Store KillInsts if they match up with the definition
Evan Cheng6130f662008-03-05 00:59:57 +0000347 } else if (curr->killsRegister(a)) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000348 if (def == MRI->getVRegDef(a)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000349 kill = curr;
Evan Cheng6130f662008-03-05 00:59:57 +0000350 } else if (curr->killsRegister(b)) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000351 if (def == MRI->getVRegDef(b)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000352 kill = curr;
353 }
354 }
355 }
356 // First properly dominates second...
357 } else if (mode == 1) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000358 if (curr == MRI->getVRegDef(b)) {
359 // Definition of second without kill of first is an interference
Owen Anderson87a702b2007-12-16 05:44:27 +0000360 if (!kill) {
361 interference = true;
362 break;
Owen Andersonddd060f2008-01-10 01:36:43 +0000363 // Definition after a kill is a non-interference
Owen Anderson87a702b2007-12-16 05:44:27 +0000364 } else {
365 interference = false;
366 break;
367 }
368 // Save KillInsts of First
Evan Cheng6130f662008-03-05 00:59:57 +0000369 } else if (curr->killsRegister(a)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000370 kill = curr;
371 }
372 // Symmetric with the above
373 } else if (mode == 2) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000374 if (curr == MRI->getVRegDef(a)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000375 if (!kill) {
376 interference = true;
377 break;
378 } else {
379 interference = false;
380 break;
381 }
Evan Cheng6130f662008-03-05 00:59:57 +0000382 } else if (curr->killsRegister(b)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000383 kill = curr;
384 }
385 }
386 }
387
388 return interference;
389}
390
Owen Andersondc4d6552008-01-10 00:47:01 +0000391/// processBlock - Determine how to break up PHIs in the current block. Each
392/// PHI is broken up by some combination of renaming its operands and inserting
393/// copies. This method is responsible for determining which operands receive
394/// which treatment.
Owen Anderson60a877d2007-11-07 05:17:15 +0000395void StrongPHIElimination::processBlock(MachineBasicBlock* MBB) {
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000396 LiveIntervals& LI = getAnalysis<LiveIntervals>();
Owen Andersonddd060f2008-01-10 01:36:43 +0000397 MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo();
Owen Anderson60a877d2007-11-07 05:17:15 +0000398
399 // Holds names that have been added to a set in any PHI within this block
400 // before the current one.
401 std::set<unsigned> ProcessedNames;
402
Owen Andersondc4d6552008-01-10 00:47:01 +0000403 // Iterate over all the PHI nodes in this block
Owen Anderson60a877d2007-11-07 05:17:15 +0000404 MachineBasicBlock::iterator P = MBB->begin();
Owen Anderson864e3a32008-01-09 10:41:39 +0000405 while (P != MBB->end() && P->getOpcode() == TargetInstrInfo::PHI) {
Owen Andersonafc6de02007-12-10 08:07:09 +0000406 unsigned DestReg = P->getOperand(0).getReg();
407
Owen Andersondc4d6552008-01-10 00:47:01 +0000408 // PHIUnion is the set of incoming registers to the PHI node that
409 // are going to be renames rather than having copies inserted. This set
410 // is refinded over the course of this function. UnionedBlocks is the set
411 // of corresponding MBBs.
Owen Anderson00316712008-03-12 03:13:29 +0000412 std::map<unsigned, unsigned> PHIUnion;
Owen Anderson60a877d2007-11-07 05:17:15 +0000413 std::set<MachineBasicBlock*> UnionedBlocks;
414
Owen Andersondc4d6552008-01-10 00:47:01 +0000415 // Iterate over the operands of the PHI node
Owen Anderson60a877d2007-11-07 05:17:15 +0000416 for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
417 unsigned SrcReg = P->getOperand(i-1).getReg();
Owen Anderson60a877d2007-11-07 05:17:15 +0000418
Owen Andersondc4d6552008-01-10 00:47:01 +0000419 // Check for trivial interferences via liveness information, allowing us
420 // to avoid extra work later. Any registers that interfere cannot both
421 // be in the renaming set, so choose one and add copies for it instead.
422 // The conditions are:
423 // 1) if the operand is live into the PHI node's block OR
424 // 2) if the PHI node is live out of the operand's defining block OR
425 // 3) if the operand is itself a PHI node and the original PHI is
426 // live into the operand's defining block OR
427 // 4) if the operand is already being renamed for another PHI node
428 // in this block OR
429 // 5) if any two operands are defined in the same block, insert copies
430 // for one of them
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000431 if (isLiveIn(SrcReg, P->getParent(), LI) ||
Owen Andersonddd060f2008-01-10 01:36:43 +0000432 isLiveOut(P->getOperand(0).getReg(),
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000433 MRI.getVRegDef(SrcReg)->getParent(), LI) ||
Owen Andersonddd060f2008-01-10 01:36:43 +0000434 ( MRI.getVRegDef(SrcReg)->getOpcode() == TargetInstrInfo::PHI &&
435 isLiveIn(P->getOperand(0).getReg(),
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000436 MRI.getVRegDef(SrcReg)->getParent(), LI) ) ||
Owen Andersonafc6de02007-12-10 08:07:09 +0000437 ProcessedNames.count(SrcReg) ||
Owen Andersonddd060f2008-01-10 01:36:43 +0000438 UnionedBlocks.count(MRI.getVRegDef(SrcReg)->getParent())) {
Owen Andersonafc6de02007-12-10 08:07:09 +0000439
Owen Andersondc4d6552008-01-10 00:47:01 +0000440 // Add a copy for the selected register
Chris Lattner8aa797a2007-12-30 23:10:15 +0000441 MachineBasicBlock* From = P->getOperand(i).getMBB();
Owen Andersonefbcebc2007-12-23 15:37:26 +0000442 Waiting[From].insert(std::make_pair(SrcReg, DestReg));
443 UsedByAnother.insert(SrcReg);
Owen Anderson60a877d2007-11-07 05:17:15 +0000444 } else {
Owen Andersondc4d6552008-01-10 00:47:01 +0000445 // Otherwise, add it to the renaming set
Owen Anderson00316712008-03-12 03:13:29 +0000446 LiveInterval& I = LI.getOrCreateInterval(SrcReg);
447 unsigned idx = LI.getMBBEndIdx(P->getOperand(i).getMBB());
448 VNInfo* VN = I.getLiveRangeContaining(idx)->valno;
449
450 assert(VN && "No VNInfo for register?");
451
452 PHIUnion.insert(std::make_pair(SrcReg, VN->id));
Owen Andersonddd060f2008-01-10 01:36:43 +0000453 UnionedBlocks.insert(MRI.getVRegDef(SrcReg)->getParent());
Owen Anderson60a877d2007-11-07 05:17:15 +0000454 }
Owen Anderson60a877d2007-11-07 05:17:15 +0000455 }
456
Owen Andersondc4d6552008-01-10 00:47:01 +0000457 // Compute the dominator forest for the renaming set. This is a forest
458 // where the nodes are the registers and the edges represent dominance
459 // relations between the defining blocks of the registers
Owen Anderson42f9e962007-11-13 20:13:24 +0000460 std::vector<StrongPHIElimination::DomForestNode*> DF =
Owen Andersonddd060f2008-01-10 01:36:43 +0000461 computeDomForest(PHIUnion, MRI);
Owen Anderson42f9e962007-11-13 20:13:24 +0000462
Owen Andersondc4d6552008-01-10 00:47:01 +0000463 // Walk DomForest to resolve interferences at an inter-block level. This
464 // will remove registers from the renaming set (and insert copies for them)
465 // if interferences are found.
Owen Anderson62d67dd2007-12-13 05:53:03 +0000466 std::vector<std::pair<unsigned, unsigned> > localInterferences;
467 processPHIUnion(P, PHIUnion, DF, localInterferences);
468
Owen Andersondc4d6552008-01-10 00:47:01 +0000469 // The dominator forest walk may have returned some register pairs whose
470 // interference cannot be determines from dominator analysis. We now
471 // examine these pairs for local interferences.
Owen Anderson87a702b2007-12-16 05:44:27 +0000472 for (std::vector<std::pair<unsigned, unsigned> >::iterator I =
473 localInterferences.begin(), E = localInterferences.end(); I != E; ++I) {
474 std::pair<unsigned, unsigned> p = *I;
475
Owen Anderson87a702b2007-12-16 05:44:27 +0000476 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
477
478 // Determine the block we need to scan and the relationship between
479 // the two registers
480 MachineBasicBlock* scan = 0;
481 unsigned mode = 0;
Owen Andersonddd060f2008-01-10 01:36:43 +0000482 if (MRI.getVRegDef(p.first)->getParent() ==
483 MRI.getVRegDef(p.second)->getParent()) {
484 scan = MRI.getVRegDef(p.first)->getParent();
Owen Anderson87a702b2007-12-16 05:44:27 +0000485 mode = 0; // Same block
Owen Andersonddd060f2008-01-10 01:36:43 +0000486 } else if (MDT.dominates(MRI.getVRegDef(p.first)->getParent(),
487 MRI.getVRegDef(p.second)->getParent())) {
488 scan = MRI.getVRegDef(p.second)->getParent();
Owen Anderson87a702b2007-12-16 05:44:27 +0000489 mode = 1; // First dominates second
490 } else {
Owen Andersonddd060f2008-01-10 01:36:43 +0000491 scan = MRI.getVRegDef(p.first)->getParent();
Owen Anderson87a702b2007-12-16 05:44:27 +0000492 mode = 2; // Second dominates first
493 }
494
495 // If there's an interference, we need to insert copies
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000496 if (interferes(p.first, p.second, scan, LI, mode)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000497 // Insert copies for First
498 for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
499 if (P->getOperand(i-1).getReg() == p.first) {
500 unsigned SrcReg = p.first;
501 MachineBasicBlock* From = P->getOperand(i).getMBB();
502
Owen Andersonefbcebc2007-12-23 15:37:26 +0000503 Waiting[From].insert(std::make_pair(SrcReg,
504 P->getOperand(0).getReg()));
505 UsedByAnother.insert(SrcReg);
506
Owen Anderson87a702b2007-12-16 05:44:27 +0000507 PHIUnion.erase(SrcReg);
508 }
509 }
510 }
511 }
Owen Anderson42f9e962007-11-13 20:13:24 +0000512
Owen Andersondc4d6552008-01-10 00:47:01 +0000513 // Add the renaming set for this PHI node to our overal renaming information
Owen Anderson0c5714b2008-01-08 21:54:52 +0000514 RenameSets.insert(std::make_pair(P->getOperand(0).getReg(), PHIUnion));
Owen Andersoncae8d8d2007-12-22 04:59:10 +0000515
Owen Andersondc4d6552008-01-10 00:47:01 +0000516 // Remember which registers are already renamed, so that we don't try to
517 // rename them for another PHI node in this block
Owen Anderson00316712008-03-12 03:13:29 +0000518 for (std::map<unsigned, unsigned>::iterator I = PHIUnion.begin(),
519 E = PHIUnion.end(); I != E; ++I)
520 ProcessedNames.insert(I->first);
Owen Andersondc4d6552008-01-10 00:47:01 +0000521
Owen Anderson60a877d2007-11-07 05:17:15 +0000522 ++P;
523 }
Owen Andersonee49b532007-11-06 05:22:43 +0000524}
525
Gabor Greif2cf36e02008-03-06 10:51:21 +0000526/// processPHIUnion - Take a set of candidate registers to be coalesced when
Owen Anderson965b4672007-12-16 04:07:23 +0000527/// decomposing the PHI instruction. Use the DominanceForest to remove the ones
528/// that are known to interfere, and flag others that need to be checked for
529/// local interferences.
Owen Andersond525f662007-12-11 20:12:11 +0000530void StrongPHIElimination::processPHIUnion(MachineInstr* Inst,
Owen Anderson00316712008-03-12 03:13:29 +0000531 std::map<unsigned, unsigned>& PHIUnion,
Owen Anderson62d67dd2007-12-13 05:53:03 +0000532 std::vector<StrongPHIElimination::DomForestNode*>& DF,
533 std::vector<std::pair<unsigned, unsigned> >& locals) {
Owen Andersond525f662007-12-11 20:12:11 +0000534
535 std::vector<DomForestNode*> worklist(DF.begin(), DF.end());
536 SmallPtrSet<DomForestNode*, 4> visited;
537
Owen Andersonddd060f2008-01-10 01:36:43 +0000538 // Code is still in SSA form, so we can use MRI::getVRegDef()
539 MachineRegisterInfo& MRI = Inst->getParent()->getParent()->getRegInfo();
540
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000541 LiveIntervals& LI = getAnalysis<LiveIntervals>();
Owen Andersond525f662007-12-11 20:12:11 +0000542 unsigned DestReg = Inst->getOperand(0).getReg();
543
Owen Anderson965b4672007-12-16 04:07:23 +0000544 // DF walk on the DomForest
Owen Andersond525f662007-12-11 20:12:11 +0000545 while (!worklist.empty()) {
546 DomForestNode* DFNode = worklist.back();
547
Owen Andersond525f662007-12-11 20:12:11 +0000548 visited.insert(DFNode);
549
550 bool inserted = false;
Owen Andersond525f662007-12-11 20:12:11 +0000551 for (DomForestNode::iterator CI = DFNode->begin(), CE = DFNode->end();
552 CI != CE; ++CI) {
553 DomForestNode* child = *CI;
Owen Anderson3b489522008-01-21 22:01:01 +0000554
555 // If the current node is live-out of the defining block of one of its
Owen Andersona6b19262008-01-21 22:03:00 +0000556 // children, insert a copy for it. NOTE: The paper actually calls for
557 // a more elaborate heuristic for determining whether to insert copies
558 // for the child or the parent. In the interest of simplicity, we're
559 // just always choosing the parent.
Owen Andersonddd060f2008-01-10 01:36:43 +0000560 if (isLiveOut(DFNode->getReg(),
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000561 MRI.getVRegDef(child->getReg())->getParent(), LI)) {
Owen Andersond525f662007-12-11 20:12:11 +0000562 // Insert copies for parent
563 for (int i = Inst->getNumOperands() - 1; i >= 2; i-=2) {
564 if (Inst->getOperand(i-1).getReg() == DFNode->getReg()) {
Owen Andersoned2ffa22007-12-12 01:25:08 +0000565 unsigned SrcReg = DFNode->getReg();
Owen Andersond525f662007-12-11 20:12:11 +0000566 MachineBasicBlock* From = Inst->getOperand(i).getMBB();
567
Owen Andersonefbcebc2007-12-23 15:37:26 +0000568 Waiting[From].insert(std::make_pair(SrcReg, DestReg));
569 UsedByAnother.insert(SrcReg);
570
Owen Andersoned2ffa22007-12-12 01:25:08 +0000571 PHIUnion.erase(SrcReg);
Owen Andersond525f662007-12-11 20:12:11 +0000572 }
573 }
Owen Anderson3b489522008-01-21 22:01:01 +0000574
575 // If a node is live-in to the defining block of one of its children, but
576 // not live-out, then we need to scan that block for local interferences.
Owen Andersonddd060f2008-01-10 01:36:43 +0000577 } else if (isLiveIn(DFNode->getReg(),
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000578 MRI.getVRegDef(child->getReg())->getParent(), LI) ||
Owen Andersonddd060f2008-01-10 01:36:43 +0000579 MRI.getVRegDef(DFNode->getReg())->getParent() ==
580 MRI.getVRegDef(child->getReg())->getParent()) {
Owen Anderson62d67dd2007-12-13 05:53:03 +0000581 // Add (p, c) to possible local interferences
582 locals.push_back(std::make_pair(DFNode->getReg(), child->getReg()));
Owen Andersond525f662007-12-11 20:12:11 +0000583 }
Owen Anderson965b4672007-12-16 04:07:23 +0000584
Owen Anderson4ba08ec2007-12-13 05:43:37 +0000585 if (!visited.count(child)) {
586 worklist.push_back(child);
587 inserted = true;
Owen Andersond525f662007-12-11 20:12:11 +0000588 }
589 }
590
591 if (!inserted) worklist.pop_back();
592 }
593}
594
Owen Andersonefbcebc2007-12-23 15:37:26 +0000595/// ScheduleCopies - Insert copies into predecessor blocks, scheduling
596/// them properly so as to avoid the 'lost copy' and the 'virtual swap'
597/// problems.
598///
599/// Based on "Practical Improvements to the Construction and Destruction
600/// of Static Single Assignment Form" by Briggs, et al.
Owen Andersonf1519e82007-12-24 22:12:23 +0000601void StrongPHIElimination::ScheduleCopies(MachineBasicBlock* MBB,
602 std::set<unsigned>& pushed) {
Owen Anderson0d893b42008-01-08 05:16:15 +0000603 // FIXME: This function needs to update LiveVariables
Owen Andersonefbcebc2007-12-23 15:37:26 +0000604 std::map<unsigned, unsigned>& copy_set= Waiting[MBB];
605
606 std::map<unsigned, unsigned> worklist;
607 std::map<unsigned, unsigned> map;
608
609 // Setup worklist of initial copies
610 for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(),
611 E = copy_set.end(); I != E; ) {
612 map.insert(std::make_pair(I->first, I->first));
613 map.insert(std::make_pair(I->second, I->second));
614
615 if (!UsedByAnother.count(I->first)) {
616 worklist.insert(*I);
617
618 // Avoid iterator invalidation
619 unsigned first = I->first;
620 ++I;
621 copy_set.erase(first);
622 } else {
623 ++I;
624 }
625 }
626
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000627 LiveIntervals& LI = getAnalysis<LiveIntervals>();
Owen Anderson0d893b42008-01-08 05:16:15 +0000628 MachineFunction* MF = MBB->getParent();
Owen Andersonddd060f2008-01-10 01:36:43 +0000629 MachineRegisterInfo& MRI = MF->getRegInfo();
Owen Anderson0d893b42008-01-08 05:16:15 +0000630 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
Owen Andersonefbcebc2007-12-23 15:37:26 +0000631
632 // Iterate over the worklist, inserting copies
633 while (!worklist.empty() || !copy_set.empty()) {
634 while (!worklist.empty()) {
635 std::pair<unsigned, unsigned> curr = *worklist.begin();
636 worklist.erase(curr.first);
637
Owen Anderson0d893b42008-01-08 05:16:15 +0000638 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first);
639
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000640 if (isLiveOut(curr.second, MBB, LI)) {
Owen Anderson0d893b42008-01-08 05:16:15 +0000641 // Create a temporary
642 unsigned t = MF->getRegInfo().createVirtualRegister(RC);
643
644 // Insert copy from curr.second to a temporary at
645 // the Phi defining curr.second
Owen Andersonddd060f2008-01-10 01:36:43 +0000646 MachineBasicBlock::iterator PI = MRI.getVRegDef(curr.second);
647 TII->copyRegToReg(*PI->getParent(), PI, t,
Owen Anderson0d893b42008-01-08 05:16:15 +0000648 curr.second, RC, RC);
649
Owen Andersonefbcebc2007-12-23 15:37:26 +0000650 // Push temporary on Stacks
Owen Anderson0d893b42008-01-08 05:16:15 +0000651 Stacks[curr.second].push_back(t);
652
653 // Insert curr.second in pushed
654 pushed.insert(curr.second);
Owen Andersonefbcebc2007-12-23 15:37:26 +0000655 }
656
657 // Insert copy from map[curr.first] to curr.second
Owen Anderson9c2efa82008-01-10 00:01:41 +0000658 TII->copyRegToReg(*MBB, MBB->getFirstTerminator(), curr.second,
Owen Anderson0d893b42008-01-08 05:16:15 +0000659 map[curr.first], RC, RC);
Owen Andersonefbcebc2007-12-23 15:37:26 +0000660 map[curr.first] = curr.second;
661
662 // If curr.first is a destination in copy_set...
663 for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(),
664 E = copy_set.end(); I != E; )
665 if (curr.first == I->second) {
666 std::pair<unsigned, unsigned> temp = *I;
667
668 // Avoid iterator invalidation
669 ++I;
670 copy_set.erase(temp.first);
671 worklist.insert(temp);
672
673 break;
674 } else {
675 ++I;
676 }
677 }
678
679 if (!copy_set.empty()) {
680 std::pair<unsigned, unsigned> curr = *copy_set.begin();
681 copy_set.erase(curr.first);
682
Owen Anderson0d893b42008-01-08 05:16:15 +0000683 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first);
684
Owen Andersonefbcebc2007-12-23 15:37:26 +0000685 // Insert a copy from dest to a new temporary t at the end of b
Owen Anderson0d893b42008-01-08 05:16:15 +0000686 unsigned t = MF->getRegInfo().createVirtualRegister(RC);
Owen Anderson9c2efa82008-01-10 00:01:41 +0000687 TII->copyRegToReg(*MBB, MBB->getFirstTerminator(), t,
Owen Anderson0d893b42008-01-08 05:16:15 +0000688 curr.second, RC, RC);
689 map[curr.second] = t;
Owen Andersonefbcebc2007-12-23 15:37:26 +0000690
691 worklist.insert(curr);
692 }
693 }
694}
695
Owen Andersonf1519e82007-12-24 22:12:23 +0000696/// InsertCopies - insert copies into MBB and all of its successors
Owen Anderson719fef62008-01-09 10:32:30 +0000697void StrongPHIElimination::InsertCopies(MachineBasicBlock* MBB,
698 std::set<MachineBasicBlock*>& visited) {
699 visited.insert(MBB);
700
Owen Andersonf1519e82007-12-24 22:12:23 +0000701 std::set<unsigned> pushed;
702
703 // Rewrite register uses from Stacks
704 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
705 I != E; ++I)
706 for (unsigned i = 0; i < I->getNumOperands(); ++i)
707 if (I->getOperand(i).isRegister() &&
708 Stacks[I->getOperand(i).getReg()].size()) {
709 I->getOperand(i).setReg(Stacks[I->getOperand(i).getReg()].back());
710 }
711
712 // Schedule the copies for this block
713 ScheduleCopies(MBB, pushed);
714
715 // Recur to our successors
716 for (GraphTraits<MachineBasicBlock*>::ChildIteratorType I =
717 GraphTraits<MachineBasicBlock*>::child_begin(MBB), E =
718 GraphTraits<MachineBasicBlock*>::child_end(MBB); I != E; ++I)
Owen Anderson719fef62008-01-09 10:32:30 +0000719 if (!visited.count(*I))
720 InsertCopies(*I, visited);
Owen Andersonf1519e82007-12-24 22:12:23 +0000721
722 // As we exit this block, pop the names we pushed while processing it
723 for (std::set<unsigned>::iterator I = pushed.begin(),
724 E = pushed.end(); I != E; ++I)
725 Stacks[*I].pop_back();
726}
727
Owen Anderson00316712008-03-12 03:13:29 +0000728void StrongPHIElimination::mergeLiveIntervals(unsigned primary,
729 unsigned secondary, unsigned VN) {
730 // FIXME: Update LiveIntervals
731}
732
Owen Andersona4ad2e72007-11-06 04:49:43 +0000733bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
Owen Andersonefbcebc2007-12-23 15:37:26 +0000734 // Compute DFS numbers of each block
Owen Andersona4ad2e72007-11-06 04:49:43 +0000735 computeDFS(Fn);
736
Owen Andersonefbcebc2007-12-23 15:37:26 +0000737 // Determine which phi node operands need copies
Owen Anderson60a877d2007-11-07 05:17:15 +0000738 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
739 if (!I->empty() &&
740 I->begin()->getOpcode() == TargetInstrInfo::PHI)
741 processBlock(I);
Owen Andersona4ad2e72007-11-06 04:49:43 +0000742
Owen Andersonefbcebc2007-12-23 15:37:26 +0000743 // Insert copies
Owen Andersonf1519e82007-12-24 22:12:23 +0000744 // FIXME: This process should probably preserve LiveVariables
Owen Anderson719fef62008-01-09 10:32:30 +0000745 std::set<MachineBasicBlock*> visited;
746 InsertCopies(Fn.begin(), visited);
Owen Andersonefbcebc2007-12-23 15:37:26 +0000747
Owen Anderson0c5714b2008-01-08 21:54:52 +0000748 // Perform renaming
Owen Anderson00316712008-03-12 03:13:29 +0000749 typedef std::map<unsigned, std::map<unsigned, unsigned> > RenameSetType;
Owen Anderson0c5714b2008-01-08 21:54:52 +0000750 for (RenameSetType::iterator I = RenameSets.begin(), E = RenameSets.end();
751 I != E; ++I)
Owen Anderson00316712008-03-12 03:13:29 +0000752 for (std::map<unsigned, unsigned>::iterator SI = I->second.begin(),
753 SE = I->second.end(); SI != SE; ++SI) {
754 mergeLiveIntervals(I->first, SI->first, SI->second);
755 Fn.getRegInfo().replaceRegWith(SI->first, I->first);
756 }
Owen Anderson0c5714b2008-01-08 21:54:52 +0000757
758 // FIXME: Insert last-minute copies
759
760 // Remove PHIs
Owen Anderson97ca75e2008-01-22 23:58:54 +0000761 std::vector<MachineInstr*> phis;
762 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
Owen Anderson0c5714b2008-01-08 21:54:52 +0000763 for (MachineBasicBlock::iterator BI = I->begin(), BE = I->end();
764 BI != BE; ++BI)
765 if (BI->getOpcode() == TargetInstrInfo::PHI)
Owen Anderson97ca75e2008-01-22 23:58:54 +0000766 phis.push_back(BI);
767 }
768
769 for (std::vector<MachineInstr*>::iterator I = phis.begin(), E = phis.end();
770 I != E; ++I)
771 (*I)->eraseFromParent();
Owen Andersoncae8d8d2007-12-22 04:59:10 +0000772
Owen Andersona4ad2e72007-11-06 04:49:43 +0000773 return false;
774}