blob: 9dd7ddc62d0415140f72a1491c346e142cc83eee [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
Owen Andersondfd07ea2008-03-12 04:22:57 +000055 // (the defining instruction of the key) that can be renamed without copies.
Owen Anderson00316712008-03-12 03:13:29 +000056 std::map<unsigned, std::map<unsigned, unsigned> > RenameSets;
Owen Andersondfd07ea2008-03-12 04:22:57 +000057
58 // PhiValueNumber holds the ID numbers of the VNs for each phi that we're
59 // eliminating, indexed by the register defined by that phi.
60 std::map<unsigned, unsigned> PhiValueNumber;
Owen Andersonafc6de02007-12-10 08:07:09 +000061
Owen Andersonec1213f2008-01-09 22:40:54 +000062 // Store the DFS-in number of each block
63 DenseMap<MachineBasicBlock*, unsigned> preorder;
64
65 // Store the DFS-out number of each block
66 DenseMap<MachineBasicBlock*, unsigned> maxpreorder;
67
Owen Andersona4ad2e72007-11-06 04:49:43 +000068 bool runOnMachineFunction(MachineFunction &Fn);
69
Owen Anderson0bda0e82007-10-31 03:37:57 +000070 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
71 AU.addRequired<MachineDominatorTree>();
Owen Andersoneb37ecc2008-03-10 07:22:36 +000072 AU.addRequired<LiveIntervals>();
73
74 // TODO: Actually make this true.
75 AU.addPreserved<LiveIntervals>();
Owen Anderson0bda0e82007-10-31 03:37:57 +000076 MachineFunctionPass::getAnalysisUsage(AU);
77 }
78
79 virtual void releaseMemory() {
80 preorder.clear();
81 maxpreorder.clear();
Owen Andersona4ad2e72007-11-06 04:49:43 +000082
Owen Andersonefbcebc2007-12-23 15:37:26 +000083 Waiting.clear();
Owen Andersonec1213f2008-01-09 22:40:54 +000084 Stacks.clear();
85 UsedByAnother.clear();
86 RenameSets.clear();
Owen Anderson0bda0e82007-10-31 03:37:57 +000087 }
88
89 private:
Owen Andersonec1213f2008-01-09 22:40:54 +000090
91 /// DomForestNode - Represents a node in the "dominator forest". This is
92 /// a forest in which the nodes represent registers and the edges
93 /// represent a dominance relation in the block defining those registers.
Owen Anderson83430bc2007-11-04 22:33:26 +000094 struct DomForestNode {
95 private:
Owen Andersonec1213f2008-01-09 22:40:54 +000096 // Store references to our children
Owen Anderson83430bc2007-11-04 22:33:26 +000097 std::vector<DomForestNode*> children;
Owen Andersonec1213f2008-01-09 22:40:54 +000098 // The register we represent
Owen Andersonee49b532007-11-06 05:22:43 +000099 unsigned reg;
Owen Anderson83430bc2007-11-04 22:33:26 +0000100
Owen Andersonec1213f2008-01-09 22:40:54 +0000101 // Add another node as our child
Owen Anderson83430bc2007-11-04 22:33:26 +0000102 void addChild(DomForestNode* DFN) { children.push_back(DFN); }
103
104 public:
105 typedef std::vector<DomForestNode*>::iterator iterator;
106
Owen Andersonec1213f2008-01-09 22:40:54 +0000107 // Create a DomForestNode by providing the register it represents, and
108 // the node to be its parent. The virtual root node has register 0
109 // and a null parent.
Owen Andersonee49b532007-11-06 05:22:43 +0000110 DomForestNode(unsigned r, DomForestNode* parent) : reg(r) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000111 if (parent)
112 parent->addChild(this);
113 }
114
Owen Andersona4ad2e72007-11-06 04:49:43 +0000115 ~DomForestNode() {
116 for (iterator I = begin(), E = end(); I != E; ++I)
117 delete *I;
118 }
Owen Anderson83430bc2007-11-04 22:33:26 +0000119
Owen Andersonec1213f2008-01-09 22:40:54 +0000120 /// getReg - Return the regiser that this node represents
Owen Andersonee49b532007-11-06 05:22:43 +0000121 inline unsigned getReg() { return reg; }
Owen Andersona4ad2e72007-11-06 04:49:43 +0000122
Owen Andersonec1213f2008-01-09 22:40:54 +0000123 // Provide iterator access to our children
Owen Andersona4ad2e72007-11-06 04:49:43 +0000124 inline DomForestNode::iterator begin() { return children.begin(); }
125 inline DomForestNode::iterator end() { return children.end(); }
Owen Anderson83430bc2007-11-04 22:33:26 +0000126 };
127
Owen Anderson0bda0e82007-10-31 03:37:57 +0000128 void computeDFS(MachineFunction& MF);
Owen Anderson60a877d2007-11-07 05:17:15 +0000129 void processBlock(MachineBasicBlock* MBB);
Owen Anderson83430bc2007-11-04 22:33:26 +0000130
Owen Anderson00316712008-03-12 03:13:29 +0000131 std::vector<DomForestNode*> computeDomForest(std::map<unsigned, unsigned>& instrs,
Owen Andersonddd060f2008-01-10 01:36:43 +0000132 MachineRegisterInfo& MRI);
Owen Andersond525f662007-12-11 20:12:11 +0000133 void processPHIUnion(MachineInstr* Inst,
Owen Anderson00316712008-03-12 03:13:29 +0000134 std::map<unsigned, unsigned>& PHIUnion,
Owen Anderson62d67dd2007-12-13 05:53:03 +0000135 std::vector<StrongPHIElimination::DomForestNode*>& DF,
136 std::vector<std::pair<unsigned, unsigned> >& locals);
Owen Andersonf1519e82007-12-24 22:12:23 +0000137 void ScheduleCopies(MachineBasicBlock* MBB, std::set<unsigned>& pushed);
Owen Anderson719fef62008-01-09 10:32:30 +0000138 void InsertCopies(MachineBasicBlock* MBB, std::set<MachineBasicBlock*>& v);
Owen Anderson00316712008-03-12 03:13:29 +0000139 void mergeLiveIntervals(unsigned primary, unsigned secondary, unsigned VN);
Owen Anderson0bda0e82007-10-31 03:37:57 +0000140 };
141
142 char StrongPHIElimination::ID = 0;
143 RegisterPass<StrongPHIElimination> X("strong-phi-node-elimination",
144 "Eliminate PHI nodes for register allocation, intelligently");
145}
146
147const PassInfo *llvm::StrongPHIEliminationID = X.getPassInfo();
148
149/// computeDFS - Computes the DFS-in and DFS-out numbers of the dominator tree
150/// of the given MachineFunction. These numbers are then used in other parts
151/// of the PHI elimination process.
152void StrongPHIElimination::computeDFS(MachineFunction& MF) {
153 SmallPtrSet<MachineDomTreeNode*, 8> frontier;
154 SmallPtrSet<MachineDomTreeNode*, 8> visited;
155
156 unsigned time = 0;
157
158 MachineDominatorTree& DT = getAnalysis<MachineDominatorTree>();
159
160 MachineDomTreeNode* node = DT.getRootNode();
161
162 std::vector<MachineDomTreeNode*> worklist;
163 worklist.push_back(node);
164
165 while (!worklist.empty()) {
166 MachineDomTreeNode* currNode = worklist.back();
167
168 if (!frontier.count(currNode)) {
169 frontier.insert(currNode);
170 ++time;
171 preorder.insert(std::make_pair(currNode->getBlock(), time));
172 }
173
174 bool inserted = false;
175 for (MachineDomTreeNode::iterator I = node->begin(), E = node->end();
176 I != E; ++I)
177 if (!frontier.count(*I) && !visited.count(*I)) {
178 worklist.push_back(*I);
179 inserted = true;
180 break;
181 }
182
183 if (!inserted) {
184 frontier.erase(currNode);
185 visited.insert(currNode);
186 maxpreorder.insert(std::make_pair(currNode->getBlock(), time));
187
188 worklist.pop_back();
189 }
190 }
Duncan Sands1bd32712007-10-31 08:49:24 +0000191}
Owen Anderson83430bc2007-11-04 22:33:26 +0000192
Owen Anderson8b96b9f2007-11-06 05:26:02 +0000193/// PreorderSorter - a helper class that is used to sort registers
194/// according to the preorder number of their defining blocks
Owen Anderson83430bc2007-11-04 22:33:26 +0000195class PreorderSorter {
196private:
197 DenseMap<MachineBasicBlock*, unsigned>& preorder;
Owen Andersonddd060f2008-01-10 01:36:43 +0000198 MachineRegisterInfo& MRI;
Owen Anderson83430bc2007-11-04 22:33:26 +0000199
200public:
Owen Andersonee49b532007-11-06 05:22:43 +0000201 PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p,
Owen Andersonddd060f2008-01-10 01:36:43 +0000202 MachineRegisterInfo& M) : preorder(p), MRI(M) { }
Owen Anderson83430bc2007-11-04 22:33:26 +0000203
Owen Andersonee49b532007-11-06 05:22:43 +0000204 bool operator()(unsigned A, unsigned B) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000205 if (A == B)
206 return false;
207
Owen Andersonddd060f2008-01-10 01:36:43 +0000208 MachineBasicBlock* ABlock = MRI.getVRegDef(A)->getParent();
209 MachineBasicBlock* BBlock = MRI.getVRegDef(B)->getParent();
Owen Andersonee49b532007-11-06 05:22:43 +0000210
211 if (preorder[ABlock] < preorder[BBlock])
Owen Anderson83430bc2007-11-04 22:33:26 +0000212 return true;
Owen Andersonee49b532007-11-06 05:22:43 +0000213 else if (preorder[ABlock] > preorder[BBlock])
Owen Anderson83430bc2007-11-04 22:33:26 +0000214 return false;
215
Owen Andersonee49b532007-11-06 05:22:43 +0000216 return false;
Owen Anderson83430bc2007-11-04 22:33:26 +0000217 }
218};
219
Owen Anderson8b96b9f2007-11-06 05:26:02 +0000220/// computeDomForest - compute the subforest of the DomTree corresponding
221/// to the defining blocks of the registers in question
Owen Anderson83430bc2007-11-04 22:33:26 +0000222std::vector<StrongPHIElimination::DomForestNode*>
Owen Anderson00316712008-03-12 03:13:29 +0000223StrongPHIElimination::computeDomForest(std::map<unsigned, unsigned>& regs,
Owen Andersonddd060f2008-01-10 01:36:43 +0000224 MachineRegisterInfo& MRI) {
Owen Andersonec1213f2008-01-09 22:40:54 +0000225 // Begin by creating a virtual root node, since the actual results
226 // may well be a forest. Assume this node has maximum DFS-out number.
Owen Anderson83430bc2007-11-04 22:33:26 +0000227 DomForestNode* VirtualRoot = new DomForestNode(0, 0);
228 maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL));
229
Owen Andersonec1213f2008-01-09 22:40:54 +0000230 // Populate a worklist with the registers
Owen Andersonee49b532007-11-06 05:22:43 +0000231 std::vector<unsigned> worklist;
232 worklist.reserve(regs.size());
Owen Anderson00316712008-03-12 03:13:29 +0000233 for (std::map<unsigned, unsigned>::iterator I = regs.begin(), E = regs.end();
Owen Andersonee49b532007-11-06 05:22:43 +0000234 I != E; ++I)
Owen Anderson00316712008-03-12 03:13:29 +0000235 worklist.push_back(I->first);
Owen Andersonee49b532007-11-06 05:22:43 +0000236
Owen Andersonec1213f2008-01-09 22:40:54 +0000237 // Sort the registers by the DFS-in number of their defining block
Owen Andersonddd060f2008-01-10 01:36:43 +0000238 PreorderSorter PS(preorder, MRI);
Owen Anderson83430bc2007-11-04 22:33:26 +0000239 std::sort(worklist.begin(), worklist.end(), PS);
240
Owen Andersonec1213f2008-01-09 22:40:54 +0000241 // Create a "current parent" stack, and put the virtual root on top of it
Owen Anderson83430bc2007-11-04 22:33:26 +0000242 DomForestNode* CurrentParent = VirtualRoot;
243 std::vector<DomForestNode*> stack;
244 stack.push_back(VirtualRoot);
245
Owen Andersonec1213f2008-01-09 22:40:54 +0000246 // Iterate over all the registers in the previously computed order
Owen Andersonee49b532007-11-06 05:22:43 +0000247 for (std::vector<unsigned>::iterator I = worklist.begin(), E = worklist.end();
248 I != E; ++I) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000249 unsigned pre = preorder[MRI.getVRegDef(*I)->getParent()];
Owen Andersoncb7d9492008-01-09 06:19:05 +0000250 MachineBasicBlock* parentBlock = CurrentParent->getReg() ?
Owen Andersonddd060f2008-01-10 01:36:43 +0000251 MRI.getVRegDef(CurrentParent->getReg())->getParent() :
Owen Andersoncb7d9492008-01-09 06:19:05 +0000252 0;
Owen Andersonee49b532007-11-06 05:22:43 +0000253
Owen Andersonec1213f2008-01-09 22:40:54 +0000254 // If the DFS-in number of the register is greater than the DFS-out number
255 // of the current parent, repeatedly pop the parent stack until it isn't.
Owen Andersonee49b532007-11-06 05:22:43 +0000256 while (pre > maxpreorder[parentBlock]) {
Owen Anderson83430bc2007-11-04 22:33:26 +0000257 stack.pop_back();
258 CurrentParent = stack.back();
Owen Andersonee49b532007-11-06 05:22:43 +0000259
Owen Anderson864e3a32008-01-09 10:41:39 +0000260 parentBlock = CurrentParent->getReg() ?
Owen Andersonddd060f2008-01-10 01:36:43 +0000261 MRI.getVRegDef(CurrentParent->getReg())->getParent() :
Owen Anderson864e3a32008-01-09 10:41:39 +0000262 0;
Owen Anderson83430bc2007-11-04 22:33:26 +0000263 }
264
Owen Andersonec1213f2008-01-09 22:40:54 +0000265 // Now that we've found the appropriate parent, create a DomForestNode for
266 // this register and attach it to the forest
Owen Anderson83430bc2007-11-04 22:33:26 +0000267 DomForestNode* child = new DomForestNode(*I, CurrentParent);
Owen Andersonec1213f2008-01-09 22:40:54 +0000268
269 // Push this new node on the "current parent" stack
Owen Anderson83430bc2007-11-04 22:33:26 +0000270 stack.push_back(child);
271 CurrentParent = child;
272 }
273
Owen Andersonec1213f2008-01-09 22:40:54 +0000274 // Return a vector containing the children of the virtual root node
Owen Anderson83430bc2007-11-04 22:33:26 +0000275 std::vector<DomForestNode*> ret;
276 ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end());
277 return ret;
278}
Owen Andersona4ad2e72007-11-06 04:49:43 +0000279
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000280/// isLiveIn - helper method that determines, from a regno, if a register
Owen Anderson60a877d2007-11-07 05:17:15 +0000281/// is live into a block
Owen Andersonddd060f2008-01-10 01:36:43 +0000282static bool isLiveIn(unsigned r, MachineBasicBlock* MBB,
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000283 LiveIntervals& LI) {
284 LiveInterval& I = LI.getOrCreateInterval(r);
285 unsigned idx = LI.getMBBStartIdx(MBB);
286 return I.liveBeforeAndAt(idx);
Owen Anderson60a877d2007-11-07 05:17:15 +0000287}
288
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000289/// isLiveOut - help method that determines, from a regno, if a register is
Owen Anderson60a877d2007-11-07 05:17:15 +0000290/// live out of a block.
Owen Andersonddd060f2008-01-10 01:36:43 +0000291static bool isLiveOut(unsigned r, MachineBasicBlock* MBB,
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000292 LiveIntervals& LI) {
293 for (MachineBasicBlock::succ_iterator PI = MBB->succ_begin(),
294 E = MBB->succ_end(); PI != E; ++PI) {
295 if (isLiveIn(r, *PI, LI))
296 return true;
Owen Anderson14b3fb72007-11-08 01:32:45 +0000297 }
Owen Anderson60a877d2007-11-07 05:17:15 +0000298
299 return false;
300}
301
Owen Anderson87a702b2007-12-16 05:44:27 +0000302/// interferes - checks for local interferences by scanning a block. The only
303/// trick parameter is 'mode' which tells it the relationship of the two
304/// registers. 0 - defined in the same block, 1 - first properly dominates
305/// second, 2 - second properly dominates first
Owen Andersonb199cbe2008-01-10 00:33:11 +0000306static bool interferes(unsigned a, unsigned b, MachineBasicBlock* scan,
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000307 LiveIntervals& LV, unsigned mode) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000308 MachineInstr* def = 0;
309 MachineInstr* kill = 0;
310
Owen Andersonddd060f2008-01-10 01:36:43 +0000311 // The code is still in SSA form at this point, so there is only one
312 // definition per VReg. Thus we can safely use MRI->getVRegDef().
313 const MachineRegisterInfo* MRI = &scan->getParent()->getRegInfo();
Owen Andersonb199cbe2008-01-10 00:33:11 +0000314
Owen Anderson87a702b2007-12-16 05:44:27 +0000315 bool interference = false;
316
317 // Wallk the block, checking for interferences
318 for (MachineBasicBlock::iterator MBI = scan->begin(), MBE = scan->end();
319 MBI != MBE; ++MBI) {
320 MachineInstr* curr = MBI;
321
322 // Same defining block...
323 if (mode == 0) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000324 if (curr == MRI->getVRegDef(a)) {
325 // If we find our first definition, save it
Owen Anderson87a702b2007-12-16 05:44:27 +0000326 if (!def) {
327 def = curr;
Owen Andersonddd060f2008-01-10 01:36:43 +0000328 // If there's already an unkilled definition, then
Owen Anderson87a702b2007-12-16 05:44:27 +0000329 // this is an interference
330 } else if (!kill) {
331 interference = true;
332 break;
Owen Andersonddd060f2008-01-10 01:36:43 +0000333 // If there's a definition followed by a KillInst, then
Owen Anderson87a702b2007-12-16 05:44:27 +0000334 // they can't interfere
335 } else {
336 interference = false;
337 break;
338 }
339 // Symmetric with the above
Owen Andersonddd060f2008-01-10 01:36:43 +0000340 } else if (curr == MRI->getVRegDef(b)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000341 if (!def) {
342 def = curr;
343 } else if (!kill) {
344 interference = true;
345 break;
346 } else {
347 interference = false;
348 break;
349 }
Owen Andersonddd060f2008-01-10 01:36:43 +0000350 // Store KillInsts if they match up with the definition
Evan Cheng6130f662008-03-05 00:59:57 +0000351 } else if (curr->killsRegister(a)) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000352 if (def == MRI->getVRegDef(a)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000353 kill = curr;
Evan Cheng6130f662008-03-05 00:59:57 +0000354 } else if (curr->killsRegister(b)) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000355 if (def == MRI->getVRegDef(b)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000356 kill = curr;
357 }
358 }
359 }
360 // First properly dominates second...
361 } else if (mode == 1) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000362 if (curr == MRI->getVRegDef(b)) {
363 // Definition of second without kill of first is an interference
Owen Anderson87a702b2007-12-16 05:44:27 +0000364 if (!kill) {
365 interference = true;
366 break;
Owen Andersonddd060f2008-01-10 01:36:43 +0000367 // Definition after a kill is a non-interference
Owen Anderson87a702b2007-12-16 05:44:27 +0000368 } else {
369 interference = false;
370 break;
371 }
372 // Save KillInsts of First
Evan Cheng6130f662008-03-05 00:59:57 +0000373 } else if (curr->killsRegister(a)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000374 kill = curr;
375 }
376 // Symmetric with the above
377 } else if (mode == 2) {
Owen Andersonddd060f2008-01-10 01:36:43 +0000378 if (curr == MRI->getVRegDef(a)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000379 if (!kill) {
380 interference = true;
381 break;
382 } else {
383 interference = false;
384 break;
385 }
Evan Cheng6130f662008-03-05 00:59:57 +0000386 } else if (curr->killsRegister(b)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000387 kill = curr;
388 }
389 }
390 }
391
392 return interference;
393}
394
Owen Andersondc4d6552008-01-10 00:47:01 +0000395/// processBlock - Determine how to break up PHIs in the current block. Each
396/// PHI is broken up by some combination of renaming its operands and inserting
397/// copies. This method is responsible for determining which operands receive
398/// which treatment.
Owen Anderson60a877d2007-11-07 05:17:15 +0000399void StrongPHIElimination::processBlock(MachineBasicBlock* MBB) {
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000400 LiveIntervals& LI = getAnalysis<LiveIntervals>();
Owen Andersonddd060f2008-01-10 01:36:43 +0000401 MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo();
Owen Anderson60a877d2007-11-07 05:17:15 +0000402
403 // Holds names that have been added to a set in any PHI within this block
404 // before the current one.
405 std::set<unsigned> ProcessedNames;
406
Owen Andersondc4d6552008-01-10 00:47:01 +0000407 // Iterate over all the PHI nodes in this block
Owen Anderson60a877d2007-11-07 05:17:15 +0000408 MachineBasicBlock::iterator P = MBB->begin();
Owen Anderson864e3a32008-01-09 10:41:39 +0000409 while (P != MBB->end() && P->getOpcode() == TargetInstrInfo::PHI) {
Owen Andersonafc6de02007-12-10 08:07:09 +0000410 unsigned DestReg = P->getOperand(0).getReg();
411
Owen Andersondfd07ea2008-03-12 04:22:57 +0000412 LiveInterval& PI = LI.getOrCreateInterval(DestReg);
413 unsigned pIdx = LI.getInstructionIndex(P);
414 VNInfo* PVN = PI.getLiveRangeContaining(pIdx)->valno;
415 PhiValueNumber.insert(std::make_pair(DestReg, PVN->id));
416
Owen Andersondc4d6552008-01-10 00:47:01 +0000417 // PHIUnion is the set of incoming registers to the PHI node that
418 // are going to be renames rather than having copies inserted. This set
419 // is refinded over the course of this function. UnionedBlocks is the set
420 // of corresponding MBBs.
Owen Anderson00316712008-03-12 03:13:29 +0000421 std::map<unsigned, unsigned> PHIUnion;
Owen Anderson60a877d2007-11-07 05:17:15 +0000422 std::set<MachineBasicBlock*> UnionedBlocks;
423
Owen Andersondc4d6552008-01-10 00:47:01 +0000424 // Iterate over the operands of the PHI node
Owen Anderson60a877d2007-11-07 05:17:15 +0000425 for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
426 unsigned SrcReg = P->getOperand(i-1).getReg();
Owen Anderson60a877d2007-11-07 05:17:15 +0000427
Owen Andersondc4d6552008-01-10 00:47:01 +0000428 // Check for trivial interferences via liveness information, allowing us
429 // to avoid extra work later. Any registers that interfere cannot both
430 // be in the renaming set, so choose one and add copies for it instead.
431 // The conditions are:
432 // 1) if the operand is live into the PHI node's block OR
433 // 2) if the PHI node is live out of the operand's defining block OR
434 // 3) if the operand is itself a PHI node and the original PHI is
435 // live into the operand's defining block OR
436 // 4) if the operand is already being renamed for another PHI node
437 // in this block OR
438 // 5) if any two operands are defined in the same block, insert copies
439 // for one of them
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000440 if (isLiveIn(SrcReg, P->getParent(), LI) ||
Owen Andersonddd060f2008-01-10 01:36:43 +0000441 isLiveOut(P->getOperand(0).getReg(),
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000442 MRI.getVRegDef(SrcReg)->getParent(), LI) ||
Owen Andersonddd060f2008-01-10 01:36:43 +0000443 ( MRI.getVRegDef(SrcReg)->getOpcode() == TargetInstrInfo::PHI &&
444 isLiveIn(P->getOperand(0).getReg(),
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000445 MRI.getVRegDef(SrcReg)->getParent(), LI) ) ||
Owen Andersonafc6de02007-12-10 08:07:09 +0000446 ProcessedNames.count(SrcReg) ||
Owen Andersonddd060f2008-01-10 01:36:43 +0000447 UnionedBlocks.count(MRI.getVRegDef(SrcReg)->getParent())) {
Owen Andersonafc6de02007-12-10 08:07:09 +0000448
Owen Andersondc4d6552008-01-10 00:47:01 +0000449 // Add a copy for the selected register
Chris Lattner8aa797a2007-12-30 23:10:15 +0000450 MachineBasicBlock* From = P->getOperand(i).getMBB();
Owen Andersonefbcebc2007-12-23 15:37:26 +0000451 Waiting[From].insert(std::make_pair(SrcReg, DestReg));
452 UsedByAnother.insert(SrcReg);
Owen Anderson60a877d2007-11-07 05:17:15 +0000453 } else {
Owen Andersondc4d6552008-01-10 00:47:01 +0000454 // Otherwise, add it to the renaming set
Owen Anderson00316712008-03-12 03:13:29 +0000455 LiveInterval& I = LI.getOrCreateInterval(SrcReg);
456 unsigned idx = LI.getMBBEndIdx(P->getOperand(i).getMBB());
457 VNInfo* VN = I.getLiveRangeContaining(idx)->valno;
458
459 assert(VN && "No VNInfo for register?");
460
461 PHIUnion.insert(std::make_pair(SrcReg, VN->id));
Owen Andersonddd060f2008-01-10 01:36:43 +0000462 UnionedBlocks.insert(MRI.getVRegDef(SrcReg)->getParent());
Owen Anderson60a877d2007-11-07 05:17:15 +0000463 }
Owen Anderson60a877d2007-11-07 05:17:15 +0000464 }
465
Owen Andersondc4d6552008-01-10 00:47:01 +0000466 // Compute the dominator forest for the renaming set. This is a forest
467 // where the nodes are the registers and the edges represent dominance
468 // relations between the defining blocks of the registers
Owen Anderson42f9e962007-11-13 20:13:24 +0000469 std::vector<StrongPHIElimination::DomForestNode*> DF =
Owen Andersonddd060f2008-01-10 01:36:43 +0000470 computeDomForest(PHIUnion, MRI);
Owen Anderson42f9e962007-11-13 20:13:24 +0000471
Owen Andersondc4d6552008-01-10 00:47:01 +0000472 // Walk DomForest to resolve interferences at an inter-block level. This
473 // will remove registers from the renaming set (and insert copies for them)
474 // if interferences are found.
Owen Anderson62d67dd2007-12-13 05:53:03 +0000475 std::vector<std::pair<unsigned, unsigned> > localInterferences;
476 processPHIUnion(P, PHIUnion, DF, localInterferences);
477
Owen Andersondc4d6552008-01-10 00:47:01 +0000478 // The dominator forest walk may have returned some register pairs whose
479 // interference cannot be determines from dominator analysis. We now
480 // examine these pairs for local interferences.
Owen Anderson87a702b2007-12-16 05:44:27 +0000481 for (std::vector<std::pair<unsigned, unsigned> >::iterator I =
482 localInterferences.begin(), E = localInterferences.end(); I != E; ++I) {
483 std::pair<unsigned, unsigned> p = *I;
484
Owen Anderson87a702b2007-12-16 05:44:27 +0000485 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
486
487 // Determine the block we need to scan and the relationship between
488 // the two registers
489 MachineBasicBlock* scan = 0;
490 unsigned mode = 0;
Owen Andersonddd060f2008-01-10 01:36:43 +0000491 if (MRI.getVRegDef(p.first)->getParent() ==
492 MRI.getVRegDef(p.second)->getParent()) {
493 scan = MRI.getVRegDef(p.first)->getParent();
Owen Anderson87a702b2007-12-16 05:44:27 +0000494 mode = 0; // Same block
Owen Andersonddd060f2008-01-10 01:36:43 +0000495 } else if (MDT.dominates(MRI.getVRegDef(p.first)->getParent(),
496 MRI.getVRegDef(p.second)->getParent())) {
497 scan = MRI.getVRegDef(p.second)->getParent();
Owen Anderson87a702b2007-12-16 05:44:27 +0000498 mode = 1; // First dominates second
499 } else {
Owen Andersonddd060f2008-01-10 01:36:43 +0000500 scan = MRI.getVRegDef(p.first)->getParent();
Owen Anderson87a702b2007-12-16 05:44:27 +0000501 mode = 2; // Second dominates first
502 }
503
504 // If there's an interference, we need to insert copies
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000505 if (interferes(p.first, p.second, scan, LI, mode)) {
Owen Anderson87a702b2007-12-16 05:44:27 +0000506 // Insert copies for First
507 for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
508 if (P->getOperand(i-1).getReg() == p.first) {
509 unsigned SrcReg = p.first;
510 MachineBasicBlock* From = P->getOperand(i).getMBB();
511
Owen Andersonefbcebc2007-12-23 15:37:26 +0000512 Waiting[From].insert(std::make_pair(SrcReg,
513 P->getOperand(0).getReg()));
514 UsedByAnother.insert(SrcReg);
515
Owen Anderson87a702b2007-12-16 05:44:27 +0000516 PHIUnion.erase(SrcReg);
517 }
518 }
519 }
520 }
Owen Anderson42f9e962007-11-13 20:13:24 +0000521
Owen Andersondc4d6552008-01-10 00:47:01 +0000522 // Add the renaming set for this PHI node to our overal renaming information
Owen Anderson0c5714b2008-01-08 21:54:52 +0000523 RenameSets.insert(std::make_pair(P->getOperand(0).getReg(), PHIUnion));
Owen Andersoncae8d8d2007-12-22 04:59:10 +0000524
Owen Andersondc4d6552008-01-10 00:47:01 +0000525 // Remember which registers are already renamed, so that we don't try to
526 // rename them for another PHI node in this block
Owen Anderson00316712008-03-12 03:13:29 +0000527 for (std::map<unsigned, unsigned>::iterator I = PHIUnion.begin(),
528 E = PHIUnion.end(); I != E; ++I)
529 ProcessedNames.insert(I->first);
Owen Andersondc4d6552008-01-10 00:47:01 +0000530
Owen Anderson60a877d2007-11-07 05:17:15 +0000531 ++P;
532 }
Owen Andersonee49b532007-11-06 05:22:43 +0000533}
534
Gabor Greif2cf36e02008-03-06 10:51:21 +0000535/// processPHIUnion - Take a set of candidate registers to be coalesced when
Owen Anderson965b4672007-12-16 04:07:23 +0000536/// decomposing the PHI instruction. Use the DominanceForest to remove the ones
537/// that are known to interfere, and flag others that need to be checked for
538/// local interferences.
Owen Andersond525f662007-12-11 20:12:11 +0000539void StrongPHIElimination::processPHIUnion(MachineInstr* Inst,
Owen Anderson00316712008-03-12 03:13:29 +0000540 std::map<unsigned, unsigned>& PHIUnion,
Owen Anderson62d67dd2007-12-13 05:53:03 +0000541 std::vector<StrongPHIElimination::DomForestNode*>& DF,
542 std::vector<std::pair<unsigned, unsigned> >& locals) {
Owen Andersond525f662007-12-11 20:12:11 +0000543
544 std::vector<DomForestNode*> worklist(DF.begin(), DF.end());
545 SmallPtrSet<DomForestNode*, 4> visited;
546
Owen Andersonddd060f2008-01-10 01:36:43 +0000547 // Code is still in SSA form, so we can use MRI::getVRegDef()
548 MachineRegisterInfo& MRI = Inst->getParent()->getParent()->getRegInfo();
549
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000550 LiveIntervals& LI = getAnalysis<LiveIntervals>();
Owen Andersond525f662007-12-11 20:12:11 +0000551 unsigned DestReg = Inst->getOperand(0).getReg();
552
Owen Anderson965b4672007-12-16 04:07:23 +0000553 // DF walk on the DomForest
Owen Andersond525f662007-12-11 20:12:11 +0000554 while (!worklist.empty()) {
555 DomForestNode* DFNode = worklist.back();
556
Owen Andersond525f662007-12-11 20:12:11 +0000557 visited.insert(DFNode);
558
559 bool inserted = false;
Owen Andersond525f662007-12-11 20:12:11 +0000560 for (DomForestNode::iterator CI = DFNode->begin(), CE = DFNode->end();
561 CI != CE; ++CI) {
562 DomForestNode* child = *CI;
Owen Anderson3b489522008-01-21 22:01:01 +0000563
564 // If the current node is live-out of the defining block of one of its
Owen Andersona6b19262008-01-21 22:03:00 +0000565 // children, insert a copy for it. NOTE: The paper actually calls for
566 // a more elaborate heuristic for determining whether to insert copies
567 // for the child or the parent. In the interest of simplicity, we're
568 // just always choosing the parent.
Owen Andersonddd060f2008-01-10 01:36:43 +0000569 if (isLiveOut(DFNode->getReg(),
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000570 MRI.getVRegDef(child->getReg())->getParent(), LI)) {
Owen Andersond525f662007-12-11 20:12:11 +0000571 // Insert copies for parent
572 for (int i = Inst->getNumOperands() - 1; i >= 2; i-=2) {
573 if (Inst->getOperand(i-1).getReg() == DFNode->getReg()) {
Owen Andersoned2ffa22007-12-12 01:25:08 +0000574 unsigned SrcReg = DFNode->getReg();
Owen Andersond525f662007-12-11 20:12:11 +0000575 MachineBasicBlock* From = Inst->getOperand(i).getMBB();
576
Owen Andersonefbcebc2007-12-23 15:37:26 +0000577 Waiting[From].insert(std::make_pair(SrcReg, DestReg));
578 UsedByAnother.insert(SrcReg);
579
Owen Andersoned2ffa22007-12-12 01:25:08 +0000580 PHIUnion.erase(SrcReg);
Owen Andersond525f662007-12-11 20:12:11 +0000581 }
582 }
Owen Anderson3b489522008-01-21 22:01:01 +0000583
584 // If a node is live-in to the defining block of one of its children, but
585 // not live-out, then we need to scan that block for local interferences.
Owen Andersonddd060f2008-01-10 01:36:43 +0000586 } else if (isLiveIn(DFNode->getReg(),
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000587 MRI.getVRegDef(child->getReg())->getParent(), LI) ||
Owen Andersonddd060f2008-01-10 01:36:43 +0000588 MRI.getVRegDef(DFNode->getReg())->getParent() ==
589 MRI.getVRegDef(child->getReg())->getParent()) {
Owen Anderson62d67dd2007-12-13 05:53:03 +0000590 // Add (p, c) to possible local interferences
591 locals.push_back(std::make_pair(DFNode->getReg(), child->getReg()));
Owen Andersond525f662007-12-11 20:12:11 +0000592 }
Owen Anderson965b4672007-12-16 04:07:23 +0000593
Owen Anderson4ba08ec2007-12-13 05:43:37 +0000594 if (!visited.count(child)) {
595 worklist.push_back(child);
596 inserted = true;
Owen Andersond525f662007-12-11 20:12:11 +0000597 }
598 }
599
600 if (!inserted) worklist.pop_back();
601 }
602}
603
Owen Andersonefbcebc2007-12-23 15:37:26 +0000604/// ScheduleCopies - Insert copies into predecessor blocks, scheduling
605/// them properly so as to avoid the 'lost copy' and the 'virtual swap'
606/// problems.
607///
608/// Based on "Practical Improvements to the Construction and Destruction
609/// of Static Single Assignment Form" by Briggs, et al.
Owen Andersonf1519e82007-12-24 22:12:23 +0000610void StrongPHIElimination::ScheduleCopies(MachineBasicBlock* MBB,
611 std::set<unsigned>& pushed) {
Owen Anderson0d893b42008-01-08 05:16:15 +0000612 // FIXME: This function needs to update LiveVariables
Owen Andersonefbcebc2007-12-23 15:37:26 +0000613 std::map<unsigned, unsigned>& copy_set= Waiting[MBB];
614
615 std::map<unsigned, unsigned> worklist;
616 std::map<unsigned, unsigned> map;
617
618 // Setup worklist of initial copies
619 for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(),
620 E = copy_set.end(); I != E; ) {
621 map.insert(std::make_pair(I->first, I->first));
622 map.insert(std::make_pair(I->second, I->second));
623
624 if (!UsedByAnother.count(I->first)) {
625 worklist.insert(*I);
626
627 // Avoid iterator invalidation
628 unsigned first = I->first;
629 ++I;
630 copy_set.erase(first);
631 } else {
632 ++I;
633 }
634 }
635
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000636 LiveIntervals& LI = getAnalysis<LiveIntervals>();
Owen Anderson0d893b42008-01-08 05:16:15 +0000637 MachineFunction* MF = MBB->getParent();
Owen Andersonddd060f2008-01-10 01:36:43 +0000638 MachineRegisterInfo& MRI = MF->getRegInfo();
Owen Anderson0d893b42008-01-08 05:16:15 +0000639 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
Owen Andersonefbcebc2007-12-23 15:37:26 +0000640
641 // Iterate over the worklist, inserting copies
642 while (!worklist.empty() || !copy_set.empty()) {
643 while (!worklist.empty()) {
644 std::pair<unsigned, unsigned> curr = *worklist.begin();
645 worklist.erase(curr.first);
646
Owen Anderson0d893b42008-01-08 05:16:15 +0000647 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first);
648
Owen Andersoneb37ecc2008-03-10 07:22:36 +0000649 if (isLiveOut(curr.second, MBB, LI)) {
Owen Anderson0d893b42008-01-08 05:16:15 +0000650 // Create a temporary
651 unsigned t = MF->getRegInfo().createVirtualRegister(RC);
652
653 // Insert copy from curr.second to a temporary at
654 // the Phi defining curr.second
Owen Andersonddd060f2008-01-10 01:36:43 +0000655 MachineBasicBlock::iterator PI = MRI.getVRegDef(curr.second);
656 TII->copyRegToReg(*PI->getParent(), PI, t,
Owen Anderson0d893b42008-01-08 05:16:15 +0000657 curr.second, RC, RC);
658
Owen Andersonefbcebc2007-12-23 15:37:26 +0000659 // Push temporary on Stacks
Owen Anderson0d893b42008-01-08 05:16:15 +0000660 Stacks[curr.second].push_back(t);
661
662 // Insert curr.second in pushed
663 pushed.insert(curr.second);
Owen Andersonefbcebc2007-12-23 15:37:26 +0000664 }
665
666 // Insert copy from map[curr.first] to curr.second
Owen Anderson9c2efa82008-01-10 00:01:41 +0000667 TII->copyRegToReg(*MBB, MBB->getFirstTerminator(), curr.second,
Owen Anderson0d893b42008-01-08 05:16:15 +0000668 map[curr.first], RC, RC);
Owen Andersonefbcebc2007-12-23 15:37:26 +0000669 map[curr.first] = curr.second;
670
671 // If curr.first is a destination in copy_set...
672 for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(),
673 E = copy_set.end(); I != E; )
674 if (curr.first == I->second) {
675 std::pair<unsigned, unsigned> temp = *I;
676
677 // Avoid iterator invalidation
678 ++I;
679 copy_set.erase(temp.first);
680 worklist.insert(temp);
681
682 break;
683 } else {
684 ++I;
685 }
686 }
687
688 if (!copy_set.empty()) {
689 std::pair<unsigned, unsigned> curr = *copy_set.begin();
690 copy_set.erase(curr.first);
691
Owen Anderson0d893b42008-01-08 05:16:15 +0000692 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first);
693
Owen Andersonefbcebc2007-12-23 15:37:26 +0000694 // Insert a copy from dest to a new temporary t at the end of b
Owen Anderson0d893b42008-01-08 05:16:15 +0000695 unsigned t = MF->getRegInfo().createVirtualRegister(RC);
Owen Anderson9c2efa82008-01-10 00:01:41 +0000696 TII->copyRegToReg(*MBB, MBB->getFirstTerminator(), t,
Owen Anderson0d893b42008-01-08 05:16:15 +0000697 curr.second, RC, RC);
698 map[curr.second] = t;
Owen Andersonefbcebc2007-12-23 15:37:26 +0000699
700 worklist.insert(curr);
701 }
702 }
703}
704
Owen Andersonf1519e82007-12-24 22:12:23 +0000705/// InsertCopies - insert copies into MBB and all of its successors
Owen Anderson719fef62008-01-09 10:32:30 +0000706void StrongPHIElimination::InsertCopies(MachineBasicBlock* MBB,
707 std::set<MachineBasicBlock*>& visited) {
708 visited.insert(MBB);
709
Owen Andersonf1519e82007-12-24 22:12:23 +0000710 std::set<unsigned> pushed;
711
712 // Rewrite register uses from Stacks
713 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
714 I != E; ++I)
715 for (unsigned i = 0; i < I->getNumOperands(); ++i)
716 if (I->getOperand(i).isRegister() &&
717 Stacks[I->getOperand(i).getReg()].size()) {
718 I->getOperand(i).setReg(Stacks[I->getOperand(i).getReg()].back());
719 }
720
721 // Schedule the copies for this block
722 ScheduleCopies(MBB, pushed);
723
724 // Recur to our successors
725 for (GraphTraits<MachineBasicBlock*>::ChildIteratorType I =
726 GraphTraits<MachineBasicBlock*>::child_begin(MBB), E =
727 GraphTraits<MachineBasicBlock*>::child_end(MBB); I != E; ++I)
Owen Anderson719fef62008-01-09 10:32:30 +0000728 if (!visited.count(*I))
729 InsertCopies(*I, visited);
Owen Andersonf1519e82007-12-24 22:12:23 +0000730
731 // As we exit this block, pop the names we pushed while processing it
732 for (std::set<unsigned>::iterator I = pushed.begin(),
733 E = pushed.end(); I != E; ++I)
734 Stacks[*I].pop_back();
735}
736
Owen Anderson00316712008-03-12 03:13:29 +0000737void StrongPHIElimination::mergeLiveIntervals(unsigned primary,
738 unsigned secondary, unsigned VN) {
739 // FIXME: Update LiveIntervals
740}
741
Owen Andersona4ad2e72007-11-06 04:49:43 +0000742bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
Owen Andersonefbcebc2007-12-23 15:37:26 +0000743 // Compute DFS numbers of each block
Owen Andersona4ad2e72007-11-06 04:49:43 +0000744 computeDFS(Fn);
745
Owen Andersonefbcebc2007-12-23 15:37:26 +0000746 // Determine which phi node operands need copies
Owen Anderson60a877d2007-11-07 05:17:15 +0000747 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
748 if (!I->empty() &&
749 I->begin()->getOpcode() == TargetInstrInfo::PHI)
750 processBlock(I);
Owen Andersona4ad2e72007-11-06 04:49:43 +0000751
Owen Andersonefbcebc2007-12-23 15:37:26 +0000752 // Insert copies
Owen Andersonf1519e82007-12-24 22:12:23 +0000753 // FIXME: This process should probably preserve LiveVariables
Owen Anderson719fef62008-01-09 10:32:30 +0000754 std::set<MachineBasicBlock*> visited;
755 InsertCopies(Fn.begin(), visited);
Owen Andersonefbcebc2007-12-23 15:37:26 +0000756
Owen Anderson0c5714b2008-01-08 21:54:52 +0000757 // Perform renaming
Owen Anderson00316712008-03-12 03:13:29 +0000758 typedef std::map<unsigned, std::map<unsigned, unsigned> > RenameSetType;
Owen Anderson0c5714b2008-01-08 21:54:52 +0000759 for (RenameSetType::iterator I = RenameSets.begin(), E = RenameSets.end();
760 I != E; ++I)
Owen Anderson00316712008-03-12 03:13:29 +0000761 for (std::map<unsigned, unsigned>::iterator SI = I->second.begin(),
762 SE = I->second.end(); SI != SE; ++SI) {
763 mergeLiveIntervals(I->first, SI->first, SI->second);
764 Fn.getRegInfo().replaceRegWith(SI->first, I->first);
765 }
Owen Anderson0c5714b2008-01-08 21:54:52 +0000766
767 // FIXME: Insert last-minute copies
768
769 // Remove PHIs
Owen Anderson97ca75e2008-01-22 23:58:54 +0000770 std::vector<MachineInstr*> phis;
771 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
Owen Anderson0c5714b2008-01-08 21:54:52 +0000772 for (MachineBasicBlock::iterator BI = I->begin(), BE = I->end();
773 BI != BE; ++BI)
774 if (BI->getOpcode() == TargetInstrInfo::PHI)
Owen Anderson97ca75e2008-01-22 23:58:54 +0000775 phis.push_back(BI);
776 }
777
778 for (std::vector<MachineInstr*>::iterator I = phis.begin(), E = phis.end();
779 I != E; ++I)
780 (*I)->eraseFromParent();
Owen Andersoncae8d8d2007-12-22 04:59:10 +0000781
Owen Andersona4ad2e72007-11-06 04:49:43 +0000782 return false;
783}