blob: 4749d8eca8865f20689018585f7d6ae9a75a7f27 [file] [log] [blame]
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +00001//===- StrongPHIElimination.cpp - Eliminate PHI nodes by inserting copies -===//
Owen Anderson0bda0e82007-10-31 03:37:57 +00002//
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//
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +000010// This pass eliminates PHI instructions by aggressively coalescing the copies
11// that would be inserted by a naive algorithm and only inserting the copies
12// that are necessary. The coalescing technique initially assumes that all
13// registers appearing in a PHI instruction do not interfere. It then eliminates
14// proven interferences, using dominators to only perform a linear number of
15// interference tests instead of the quadratic number of interference tests
16// that this would naively require. This is a technique derived from:
17//
18// Budimlic, et al. Fast copy coalescing and live-range identification.
19// In Proceedings of the ACM SIGPLAN 2002 Conference on Programming Language
20// Design and Implementation (Berlin, Germany, June 17 - 19, 2002).
21// PLDI '02. ACM, New York, NY, 25-32.
22//
23// The original implementation constructs a data structure they call a dominance
24// forest for this purpose. The dominance forest was shown to be unnecessary,
25// as it is possible to emulate the creation and traversal of a dominance forest
26// by directly using the dominator tree, rather than actually constructing the
27// dominance forest. This technique is explained in:
28//
29// Boissinot, et al. Revisiting Out-of-SSA Translation for Correctness, Code
30// Quality and Efficiency,
31// In Proceedings of the 7th annual IEEE/ACM International Symposium on Code
32// Generation and Optimization (Seattle, Washington, March 22 - 25, 2009).
33// CGO '09. IEEE, Washington, DC, 114-125.
34//
35// Careful implementation allows for all of the dominator forest interference
36// checks to be performed at once in a single depth-first traversal of the
37// dominator tree, which is what is implemented here.
Owen Anderson0bda0e82007-10-31 03:37:57 +000038//
39//===----------------------------------------------------------------------===//
40
41#define DEBUG_TYPE "strongphielim"
Cameron Zwarich47bce432010-12-21 06:54:43 +000042#include "PHIEliminationUtils.h"
Owen Anderson0bda0e82007-10-31 03:37:57 +000043#include "llvm/CodeGen/Passes.h"
Owen Andersoneb37ecc2008-03-10 07:22:36 +000044#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Owen Anderson0bda0e82007-10-31 03:37:57 +000045#include "llvm/CodeGen/MachineDominators.h"
46#include "llvm/CodeGen/MachineFunctionPass.h"
Cameron Zwarich47bce432010-12-21 06:54:43 +000047#include "llvm/CodeGen/MachineInstrBuilder.h"
48#include "llvm/CodeGen/MachineRegisterInfo.h"
49#include "llvm/Target/TargetInstrInfo.h"
50#include "llvm/Support/Debug.h"
Owen Anderson0bda0e82007-10-31 03:37:57 +000051using namespace llvm;
52
Owen Anderson0bda0e82007-10-31 03:37:57 +000053namespace {
Cameron Zwarich9eaf49b2010-12-05 22:34:08 +000054 class StrongPHIElimination : public MachineFunctionPass {
55 public:
56 static char ID; // Pass identification, replacement for typeid
57 StrongPHIElimination() : MachineFunctionPass(ID) {
58 initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
59 }
Owen Anderson0bda0e82007-10-31 03:37:57 +000060
Cameron Zwarich9eaf49b2010-12-05 22:34:08 +000061 virtual void getAnalysisUsage(AnalysisUsage&) const;
62 bool runOnMachineFunction(MachineFunction&);
Cameron Zwarich47bce432010-12-21 06:54:43 +000063
64 private:
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +000065 /// This struct represents a single node in the union-find data structure
66 /// representing the variable congruence classes. There is one difference
67 /// from a normal union-find data structure. We steal two bits from the parent
68 /// pointer . One of these bits is used to represent whether the register
69 /// itself has been isolated, and the other is used to represent whether the
70 /// PHI with that register as its destination has been isolated.
71 ///
72 /// Note that this leads to the strange situation where the leader of a
73 /// congruence class may no longer logically be a member, due to being
74 /// isolated.
75 struct Node {
76 enum Flags {
77 kRegisterIsolatedFlag = 1,
78 kPHIIsolatedFlag = 2
79 };
80 Node(unsigned v) : value(v), rank(0) { parent.setPointer(this); }
81
82 Node* getLeader();
83
84 PointerIntPair<Node*, 2> parent;
85 unsigned value;
86 unsigned rank;
87 };
88
89 /// Add a register in a new congruence class containing only itself.
90 void addReg(unsigned);
91
92 /// Join the congruence classes of two registers.
93 void unionRegs(unsigned, unsigned);
94
95 /// Get the color of a register. The color is 0 if the register has been
96 /// isolated.
97 unsigned getRegColor(unsigned);
98
99 // Isolate a register.
100 void isolateReg(unsigned);
101
102 /// Get the color of a PHI. The color of a PHI is 0 if the PHI has been
103 /// isolated. Otherwise, it is the original color of its destination and
104 /// all of its operands (before they were isolated, if they were).
105 unsigned getPHIColor(MachineInstr*);
106
107 /// Isolate a PHI.
108 void isolatePHI(MachineInstr*);
109
110 void PartitionRegisters(MachineFunction& MF);
111
112 /// Traverses a basic block, splitting any interferences found between
113 /// registers in the same congruence class. It takes two DenseMaps as
114 /// arguments that it also updates: CurrentDominatingParent, which maps
115 /// a color to the register in that congruence class whose definition was
116 /// most recently seen, and ImmediateDominatingParent, which maps a register
117 /// to the register in the same congruence class that most immediately
118 /// dominates it.
119 ///
120 /// This function assumes that it is being called in a depth-first traversal
121 /// of the dominator tree.
122 void SplitInterferencesForBasicBlock(
123 MachineBasicBlock&,
124 DenseMap<unsigned, unsigned>& CurrentDominatingParent,
125 DenseMap<unsigned, unsigned>& ImmediateDominatingParent);
126
127 // Lowers a PHI instruction, inserting copies of the source and destination
128 // registers as necessary.
Cameron Zwarich47bce432010-12-21 06:54:43 +0000129 void InsertCopiesForPHI(MachineInstr*, MachineBasicBlock*);
130
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000131 // Merges the live interval of Reg into NewReg and renames Reg to NewReg
132 // everywhere that Reg appears. Requires Reg and NewReg to have non-
133 // overlapping lifetimes.
134 void MergeLIsAndRename(unsigned Reg, unsigned NewReg);
135
Cameron Zwarich47bce432010-12-21 06:54:43 +0000136 MachineRegisterInfo* MRI;
137 const TargetInstrInfo* TII;
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000138 MachineDominatorTree* DT;
Cameron Zwarich47bce432010-12-21 06:54:43 +0000139 LiveIntervals* LI;
140
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000141 BumpPtrAllocator Allocator;
142
143 DenseMap<unsigned, Node*> RegNodeMap;
144
145 // FIXME: Can these two data structures be combined? Would a std::multimap
146 // be any better?
147
148 // Stores pairs of predecessor basic blocks and the source registers of
149 // inserted copy instructions.
150 typedef DenseSet<std::pair<MachineBasicBlock*, unsigned> > SrcCopySet;
151 SrcCopySet InsertedSrcCopySet;
152
153 // Maps pairs of predecessor basic blocks and colors to their defining copy
154 // instructions.
155 typedef DenseMap<std::pair<MachineBasicBlock*, unsigned>, MachineInstr*>
156 SrcCopyMap;
157 SrcCopyMap InsertedSrcCopyMap;
158
159 // Maps inserted destination copy registers to their defining copy
160 // instructions.
161 typedef DenseMap<unsigned, MachineInstr*> DestCopyMap;
162 DestCopyMap InsertedDestCopies;
Cameron Zwarich9eaf49b2010-12-05 22:34:08 +0000163 };
Jakob Stoklund Olesen68be9562010-12-03 19:21:53 +0000164} // namespace
Owen Anderson0bda0e82007-10-31 03:37:57 +0000165
Dan Gohman844731a2008-05-13 00:00:25 +0000166char StrongPHIElimination::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +0000167INITIALIZE_PASS_BEGIN(StrongPHIElimination, "strong-phi-node-elimination",
168 "Eliminate PHI nodes for register allocation, intelligently", false, false)
169INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
170INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
171INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
172INITIALIZE_PASS_END(StrongPHIElimination, "strong-phi-node-elimination",
Owen Andersonce665bd2010-10-07 22:25:06 +0000173 "Eliminate PHI nodes for register allocation, intelligently", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +0000174
Owen Anderson90c579d2010-08-06 18:33:48 +0000175char &llvm::StrongPHIEliminationID = StrongPHIElimination::ID;
Owen Anderson0bda0e82007-10-31 03:37:57 +0000176
Cameron Zwarich9eaf49b2010-12-05 22:34:08 +0000177void StrongPHIElimination::getAnalysisUsage(AnalysisUsage& AU) const {
178 AU.setPreservesCFG();
179 AU.addRequired<MachineDominatorTree>();
180 AU.addRequired<SlotIndexes>();
181 AU.addPreserved<SlotIndexes>();
182 AU.addRequired<LiveIntervals>();
183 AU.addPreserved<LiveIntervals>();
184 MachineFunctionPass::getAnalysisUsage(AU);
185}
186
Cameron Zwarich47bce432010-12-21 06:54:43 +0000187static MachineOperand* findLastUse(MachineBasicBlock* MBB, unsigned Reg) {
188 // FIXME: This only needs to check from the first terminator, as only the
189 // first terminator can use a virtual register.
190 for (MachineBasicBlock::reverse_iterator RI = MBB->rbegin(); ; ++RI) {
191 assert (RI != MBB->rend());
192 MachineInstr* MI = &*RI;
193
194 for (MachineInstr::mop_iterator OI = MI->operands_begin(),
195 OE = MI->operands_end(); OI != OE; ++OI) {
196 MachineOperand& MO = *OI;
197 if (MO.isReg() && MO.isUse() && MO.getReg() == Reg)
198 return &MO;
199 }
200 }
201 return NULL;
202}
203
204bool StrongPHIElimination::runOnMachineFunction(MachineFunction& MF) {
205 MRI = &MF.getRegInfo();
206 TII = MF.getTarget().getInstrInfo();
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000207 DT = &getAnalysis<MachineDominatorTree>();
Cameron Zwarich47bce432010-12-21 06:54:43 +0000208 LI = &getAnalysis<LiveIntervals>();
209
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000210 PartitionRegisters(MF);
211
212 // Perform a depth-first traversal of the dominator tree, splitting
213 // interferences amongst PHI-congruence classes.
214 DenseMap<unsigned, unsigned> CurrentDominatingParent;
215 DenseMap<unsigned, unsigned> ImmediateDominatingParent;
216 for (df_iterator<MachineDomTreeNode*> DI = df_begin(DT->getRootNode()),
217 DE = df_end(DT->getRootNode()); DI != DE; ++DI) {
218 SplitInterferencesForBasicBlock(*DI->getBlock(),
219 CurrentDominatingParent,
220 ImmediateDominatingParent);
221 }
222
Cameron Zwarich47bce432010-12-21 06:54:43 +0000223 // Insert copies for all PHI source and destination registers.
224 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
225 I != E; ++I) {
226 for (MachineBasicBlock::iterator BBI = I->begin(), BBE = I->end();
227 BBI != BBE && BBI->isPHI(); ++BBI) {
228 InsertCopiesForPHI(BBI, I);
229 }
230 }
231
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000232 // FIXME: Preserve the equivalence classes during copy insertion and use
233 // the preversed equivalence classes instead of recomputing them.
234 RegNodeMap.clear();
235 PartitionRegisters(MF);
236
237 DenseMap<unsigned, unsigned> RegRenamingMap;
238 bool Changed = false;
239 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
240 I != E; ++I) {
241 MachineBasicBlock::iterator BBI = I->begin(), BBE = I->end();
242 while (BBI != BBE && BBI->isPHI()) {
243 MachineInstr* PHI = BBI;
244
245 assert(PHI->getNumOperands() > 0);
246
247 unsigned SrcReg = PHI->getOperand(1).getReg();
248 unsigned SrcColor = getRegColor(SrcReg);
249 unsigned NewReg = RegRenamingMap[SrcColor];
250 if (!NewReg) {
251 NewReg = SrcReg;
252 RegRenamingMap[SrcColor] = SrcReg;
253 }
254 MergeLIsAndRename(SrcReg, NewReg);
255
256 unsigned DestReg = PHI->getOperand(0).getReg();
257 if (!InsertedDestCopies.count(DestReg))
258 MergeLIsAndRename(DestReg, NewReg);
259
260 for (unsigned i = 3; i < PHI->getNumOperands(); i += 2) {
261 unsigned SrcReg = PHI->getOperand(i).getReg();
262 MergeLIsAndRename(SrcReg, NewReg);
263 }
264
265 ++BBI;
266 LI->RemoveMachineInstrFromMaps(PHI);
267 PHI->eraseFromParent();
268 Changed = true;
269 }
270 }
271
272 // Due to the insertion of copies to split live ranges, the live intervals are
273 // guaranteed to not overlap, except in one case: an original PHI source and a
274 // PHI destination copy. In this case, they have the same value and thus don't
275 // truly intersect, so we merge them into the value live at that point.
276 // FIXME: Is there some better way we can handle this?
277 for (DestCopyMap::iterator I = InsertedDestCopies.begin(),
278 E = InsertedDestCopies.end(); I != E; ++I) {
279 unsigned DestReg = I->first;
280 unsigned DestColor = getRegColor(DestReg);
281 unsigned NewReg = RegRenamingMap[DestColor];
282
283 LiveInterval& DestLI = LI->getInterval(DestReg);
284 LiveInterval& NewLI = LI->getInterval(NewReg);
285
Cameron Zwarich480b80a2010-12-29 03:52:51 +0000286 assert(DestLI.ranges.size() == 1
287 && "PHI destination copy's live interval should be a single live "
288 "range from the beginning of the BB to the copy instruction.");
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000289 LiveRange* DestLR = DestLI.begin();
290 VNInfo* NewVNI = NewLI.getVNInfoAt(DestLR->start);
291 if (!NewVNI) {
292 NewVNI = NewLI.createValueCopy(DestLR->valno, LI->getVNInfoAllocator());
293 MachineInstr* CopyInstr = I->second;
294 CopyInstr->getOperand(1).setIsKill(true);
295 }
296
297 LiveRange NewLR(DestLR->start, DestLR->end, NewVNI);
298 NewLI.addRange(NewLR);
299
300 LI->removeInterval(DestReg);
301 MRI->replaceRegWith(DestReg, NewReg);
302 }
303
Cameron Zwarich47bce432010-12-21 06:54:43 +0000304 // Adjust the live intervals of all PHI source registers to handle the case
305 // where the PHIs in successor blocks were the only later uses of the source
306 // register.
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000307 for (SrcCopySet::iterator I = InsertedSrcCopySet.begin(),
308 E = InsertedSrcCopySet.end(); I != E; ++I) {
Cameron Zwarich47bce432010-12-21 06:54:43 +0000309 MachineBasicBlock* MBB = I->first;
310 unsigned SrcReg = I->second;
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000311 if (unsigned RenamedRegister = RegRenamingMap[getRegColor(SrcReg)])
312 SrcReg = RenamedRegister;
313
Cameron Zwarich47bce432010-12-21 06:54:43 +0000314 LiveInterval& SrcLI = LI->getInterval(SrcReg);
315
316 bool isLiveOut = false;
317 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
318 SE = MBB->succ_end(); SI != SE; ++SI) {
319 if (SrcLI.liveAt(LI->getMBBStartIdx(*SI))) {
320 isLiveOut = true;
321 break;
322 }
323 }
324
325 if (isLiveOut)
326 continue;
327
328 MachineOperand* LastUse = findLastUse(MBB, SrcReg);
329 assert(LastUse);
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000330 SlotIndex LastUseIndex = LI->getInstructionIndex(LastUse->getParent());
331 SrcLI.removeRange(LastUseIndex.getDefIndex(), LI->getMBBEndIdx(MBB));
Cameron Zwarich47bce432010-12-21 06:54:43 +0000332 LastUse->setIsKill(true);
333 }
334
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000335 LI->renumber();
336
337 Allocator.Reset();
338 RegNodeMap.clear();
339 InsertedSrcCopySet.clear();
340 InsertedSrcCopyMap.clear();
341 InsertedDestCopies.clear();
342
343 return Changed;
344}
345
346void StrongPHIElimination::addReg(unsigned Reg) {
347 if (RegNodeMap.count(Reg))
348 return;
349 RegNodeMap[Reg] = new (Allocator) Node(Reg);
350}
351
352StrongPHIElimination::Node*
353StrongPHIElimination::Node::getLeader() {
354 Node* parentPointer = parent.getPointer();
355 if (parentPointer == this)
356 return this;
357 Node* newParent = parentPointer->getLeader();
358 parent.setPointer(newParent);
359 return newParent;
360}
361
362unsigned StrongPHIElimination::getRegColor(unsigned Reg) {
363 DenseMap<unsigned, Node*>::iterator RI = RegNodeMap.find(Reg);
364 if (RI == RegNodeMap.end())
365 return 0;
366 Node* Node = RI->second;
367 if (Node->parent.getInt() & Node::kRegisterIsolatedFlag)
368 return 0;
369 return Node->getLeader()->value;
370}
371
372void StrongPHIElimination::unionRegs(unsigned Reg1, unsigned Reg2) {
373 Node* Node1 = RegNodeMap[Reg1]->getLeader();
374 Node* Node2 = RegNodeMap[Reg2]->getLeader();
375
376 if (Node1->rank > Node2->rank) {
377 Node2->parent.setPointer(Node1->getLeader());
378 } else if (Node1->rank < Node2->rank) {
379 Node1->parent.setPointer(Node2->getLeader());
380 } else if (Node1 != Node2) {
381 Node2->parent.setPointer(Node1->getLeader());
382 Node1->rank++;
383 }
384}
385
386void StrongPHIElimination::isolateReg(unsigned Reg) {
387 Node* Node = RegNodeMap[Reg];
388 Node->parent.setInt(Node->parent.getInt() | Node::kRegisterIsolatedFlag);
389}
390
391unsigned StrongPHIElimination::getPHIColor(MachineInstr* PHI) {
392 assert(PHI->isPHI());
393
394 unsigned DestReg = PHI->getOperand(0).getReg();
395 Node* DestNode = RegNodeMap[DestReg];
396 if (DestNode->parent.getInt() & Node::kPHIIsolatedFlag)
397 return 0;
398
399 for (unsigned i = 1; i < PHI->getNumOperands(); i += 2) {
400 unsigned SrcColor = getRegColor(PHI->getOperand(i).getReg());
401 if (SrcColor)
402 return SrcColor;
403 }
404 return 0;
405}
406
407void StrongPHIElimination::isolatePHI(MachineInstr* PHI) {
408 assert(PHI->isPHI());
409 Node* Node = RegNodeMap[PHI->getOperand(0).getReg()];
410 Node->parent.setInt(Node->parent.getInt() | Node::kPHIIsolatedFlag);
411}
412
413void StrongPHIElimination::PartitionRegisters(MachineFunction& MF) {
Cameron Zwarich47bce432010-12-21 06:54:43 +0000414 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
415 I != E; ++I) {
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000416 for (MachineBasicBlock::iterator BBI = I->begin(), BBE = I->end();
417 BBI != BBE && BBI->isPHI(); ++BBI) {
418 unsigned DestReg = BBI->getOperand(0).getReg();
419 addReg(DestReg);
420
421 for (unsigned i = 1; i < BBI->getNumOperands(); i += 2) {
422 unsigned SrcReg = BBI->getOperand(i).getReg();
423 addReg(SrcReg);
424 unionRegs(DestReg, SrcReg);
425 }
426 }
427 }
428}
429
430/// SplitInterferencesForBasicBlock - traverses a basic block, splitting any
431/// interferences found between registers in the same congruence class. It
432/// takes two DenseMaps as arguments that it also updates:
433///
434/// 1) CurrentDominatingParent, which maps a color to the register in that
435/// congruence class whose definition was most recently seen.
436///
437/// 2) ImmediateDominatingParent, which maps a register to the register in the
438/// same congruence class that most immediately dominates it.
439///
440/// This function assumes that it is being called in a depth-first traversal
441/// of the dominator tree.
442///
443/// The algorithm used here is a generalization of the dominance-based SSA test
444/// for two variables. If there are variables a_1, ..., a_n such that
445///
446/// def(a_1) dom ... dom def(a_n),
447///
448/// then we can test for an interference between any two a_i by only using O(n)
449/// interference tests between pairs of variables. If i < j and a_i and a_j
450/// interfere, then a_i is alive at def(a_j), so it is also alive at def(a_i+1).
451/// Thus, in order to test for an interference involving a_i, we need only check
452/// for a potential interference with a_i+1.
453///
454/// This method can be generalized to arbitrary sets of variables by performing
455/// a depth-first traversal of the dominator tree. As we traverse down a branch
456/// of the dominator tree, we keep track of the current dominating variable and
457/// only perform an interference test with that variable. However, when we go to
458/// another branch of the dominator tree, the definition of the current dominating
459/// variable may no longer dominate the current block. In order to correct this,
460/// we need to use a stack of past choices of the current dominating variable
461/// and pop from this stack until we find a variable whose definition actually
462/// dominates the current block.
463///
464/// There will be one push on this stack for each variable that has become the
465/// current dominating variable, so instead of using an explicit stack we can
466/// simply associate the previous choice for a current dominating variable with
467/// the new choice. This works better in our implementation, where we test for
468/// interference in multiple distinct sets at once.
469void
470StrongPHIElimination::SplitInterferencesForBasicBlock(
471 MachineBasicBlock& MBB,
472 DenseMap<unsigned, unsigned>& CurrentDominatingParent,
473 DenseMap<unsigned, unsigned>& ImmediateDominatingParent) {
474 for (MachineBasicBlock::iterator BBI = MBB.begin(), BBE = MBB.end();
475 BBI != BBE; ++BBI) {
Cameron Zwarich92f0fcb62010-12-28 10:49:33 +0000476 for (MachineInstr::const_mop_iterator I = BBI->operands_begin(),
Cameron Zwarich438e25c2010-12-28 23:02:56 +0000477 E = BBI->operands_end(); I != E; ++I) {
Cameron Zwarich92f0fcb62010-12-28 10:49:33 +0000478 const MachineOperand& MO = *I;
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000479
Cameron Zwarich438e25c2010-12-28 23:02:56 +0000480 // FIXME: This would be faster if it were possible to bail out of checking
481 // an instruction's operands after the explicit defs, but this is incorrect
482 // for variadic instructions, which may appear before register allocation
483 // in the future.
484 if (!MO.isReg() || !MO.isDef())
485 continue;
486
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000487 unsigned DestReg = MO.getReg();
488 if (!DestReg || !TargetRegisterInfo::isVirtualRegister(DestReg))
489 continue;
490
491 // If the virtual register being defined is not used in any PHI or has
492 // already been isolated, then there are no more interferences to check.
493 unsigned DestColor = getRegColor(DestReg);
494 if (!DestColor)
495 continue;
496
497 // The input to this pass sometimes is not in SSA form in every basic
498 // block, as some virtual registers have redefinitions. We could eliminate
499 // this by fixing the passes that generate the non-SSA code, or we could
500 // handle it here by tracking defining machine instructions rather than
501 // virtual registers. For now, we just handle the situation conservatively
502 // in a way that will possibly lead to false interferences.
503 unsigned NewParent = CurrentDominatingParent[DestColor];
504 if (NewParent == DestReg)
505 continue;
506
507 // Pop registers from the stack represented by ImmediateDominatingParent
508 // until we find a parent that dominates the current instruction.
509 while (NewParent && (!DT->dominates(MRI->getVRegDef(NewParent), BBI)
510 || !getRegColor(NewParent)))
511 NewParent = ImmediateDominatingParent[NewParent];
512
513 // If NewParent is nonzero, then its definition dominates the current
514 // instruction, so it is only necessary to check for the liveness of
515 // NewParent in order to check for an interference.
516 if (NewParent
517 && LI->getInterval(NewParent).liveAt(LI->getInstructionIndex(BBI))) {
518 // If there is an interference, always isolate the new register. This
519 // could be improved by using a heuristic that decides which of the two
520 // registers to isolate.
521 isolateReg(DestReg);
522 CurrentDominatingParent[DestColor] = NewParent;
523 } else {
524 // If there is no interference, update ImmediateDominatingParent and set
525 // the CurrentDominatingParent for this color to the current register.
526 ImmediateDominatingParent[DestReg] = NewParent;
527 CurrentDominatingParent[DestColor] = DestReg;
528 }
Cameron Zwarich47bce432010-12-21 06:54:43 +0000529 }
530 }
531
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000532 // We now walk the PHIs in successor blocks and check for interferences. This
533 // is necesary because the use of a PHI's operands are logically contained in
534 // the predecessor block. The def of a PHI's destination register is processed
535 // along with the other defs in a basic block.
Cameron Zwarich47bce432010-12-21 06:54:43 +0000536
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000537 // The map CurrentPHIForColor maps a color to a pair of a MachineInstr* and a
538 // virtual register, which is the operand of that PHI corresponding to the
539 // current basic block.
540 // FIXME: This should use a container that doesn't always perform heap
541 // allocation.
542 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> > CurrentPHIForColor;
Cameron Zwarich47bce432010-12-21 06:54:43 +0000543
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000544 for (MachineBasicBlock::succ_iterator SI = MBB.succ_begin(),
545 SE = MBB.succ_end(); SI != SE; ++SI) {
546 for (MachineBasicBlock::iterator BBI = (*SI)->begin(), BBE = (*SI)->end();
547 BBI != BBE && BBI->isPHI(); ++BBI) {
548 MachineInstr* PHI = BBI;
549
550 // If a PHI is already isolated, either by being isolated directly or
551 // having all of its operands isolated, ignore it.
552 unsigned Color = getPHIColor(PHI);
553 if (!Color)
554 continue;
555
556 // Find the index of the PHI operand that corresponds to this basic block.
557 unsigned PredIndex;
558 for (PredIndex = 1; PredIndex < PHI->getNumOperands(); PredIndex += 2) {
559 if (PHI->getOperand(PredIndex + 1).getMBB() == &MBB)
560 break;
561 }
562 assert(PredIndex < PHI->getNumOperands());
563 unsigned PredOperandReg = PHI->getOperand(PredIndex).getReg();
564
565 // Pop registers from the stack represented by ImmediateDominatingParent
566 // until we find a parent that dominates the current instruction.
567 unsigned NewParent = CurrentDominatingParent[Color];
568 while (NewParent
569 && (!DT->dominates(MRI->getVRegDef(NewParent)->getParent(), &MBB)
570 || !getRegColor(NewParent)))
571 NewParent = ImmediateDominatingParent[NewParent];
572 CurrentDominatingParent[Color] = NewParent;
573
574 // If there is an interference with a register, always isolate the
575 // register rather than the PHI. It is also possible to isolate the
576 // PHI, but that introduces copies for all of the registers involved
577 // in that PHI.
578 if (NewParent && LI->isLiveOutOfMBB(LI->getInterval(NewParent), &MBB)
579 && NewParent != PredOperandReg)
580 isolateReg(NewParent);
581
582 std::pair<MachineInstr*, unsigned> CurrentPHI = CurrentPHIForColor[Color];
583
584 // If two PHIs have the same operand from every shared predecessor, then
585 // they don't actually interfere. Otherwise, isolate the current PHI. This
586 // could possibly be improved, e.g. we could isolate the PHI with the
587 // fewest operands.
588 if (CurrentPHI.first && CurrentPHI.second != PredOperandReg)
589 isolatePHI(PHI);
590 else
591 CurrentPHIForColor[Color] = std::make_pair(PHI, PredOperandReg);
592 }
593 }
Cameron Zwarich47bce432010-12-21 06:54:43 +0000594}
595
596void StrongPHIElimination::InsertCopiesForPHI(MachineInstr* PHI,
597 MachineBasicBlock* MBB) {
598 assert(PHI->isPHI());
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000599 unsigned PHIColor = getPHIColor(PHI);
600
601 for (unsigned i = 1; i < PHI->getNumOperands(); i += 2) {
602 MachineOperand& SrcMO = PHI->getOperand(i);
603
604 // If a source is defined by an implicit def, there is no need to insert a
605 // copy in the predecessor.
606 if (SrcMO.isUndef())
607 continue;
608
609 unsigned SrcReg = SrcMO.getReg();
610 assert(TargetRegisterInfo::isVirtualRegister(SrcReg) &&
611 "Machine PHI Operands must all be virtual registers!");
612
613 MachineBasicBlock* PredBB = PHI->getOperand(i + 1).getMBB();
614 unsigned SrcColor = getRegColor(SrcReg);
615
616 // If neither the PHI nor the operand were isolated, then we only need to
617 // set the phi-kill flag on the VNInfo at this PHI.
618 if (PHIColor && SrcColor == PHIColor) {
619 LiveInterval& SrcInterval = LI->getInterval(SrcReg);
620 SlotIndex PredIndex = LI->getMBBEndIdx(PredBB);
621 VNInfo* SrcVNI = SrcInterval.getVNInfoAt(PredIndex.getPrevIndex());
622 assert(SrcVNI);
623 SrcVNI->setHasPHIKill(true);
624 continue;
625 }
626
627 unsigned CopyReg = 0;
628 if (PHIColor) {
629 SrcCopyMap::const_iterator I
630 = InsertedSrcCopyMap.find(std::make_pair(PredBB, PHIColor));
631 CopyReg
632 = I != InsertedSrcCopyMap.end() ? I->second->getOperand(0).getReg() : 0;
633 }
634
635 if (!CopyReg) {
636 const TargetRegisterClass* RC = MRI->getRegClass(SrcReg);
637 CopyReg = MRI->createVirtualRegister(RC);
638
639 MachineBasicBlock::iterator
640 CopyInsertPoint = findPHICopyInsertPoint(PredBB, MBB, SrcReg);
641 unsigned SrcSubReg = SrcMO.getSubReg();
642 MachineInstr* CopyInstr = BuildMI(*PredBB,
643 CopyInsertPoint,
644 PHI->getDebugLoc(),
645 TII->get(TargetOpcode::COPY),
646 CopyReg).addReg(SrcReg, 0, SrcSubReg);
647 LI->InsertMachineInstrInMaps(CopyInstr);
648
649 // addLiveRangeToEndOfBlock() also adds the phikill flag to the VNInfo for
650 // the newly added range.
651 LI->addLiveRangeToEndOfBlock(CopyReg, CopyInstr);
652 InsertedSrcCopySet.insert(std::make_pair(PredBB, SrcReg));
653
654 addReg(CopyReg);
655 if (PHIColor) {
656 unionRegs(PHIColor, CopyReg);
657 assert(getRegColor(CopyReg) != CopyReg);
658 } else {
659 PHIColor = CopyReg;
660 assert(getRegColor(CopyReg) == CopyReg);
661 }
662
663 if (!InsertedSrcCopyMap.count(std::make_pair(PredBB, PHIColor)))
664 InsertedSrcCopyMap[std::make_pair(PredBB, PHIColor)] = CopyInstr;
665 }
666
667 SrcMO.setReg(CopyReg);
668
669 // If SrcReg is not live beyond the PHI, trim its interval so that it is no
670 // longer live-in to MBB. Note that SrcReg may appear in other PHIs that are
671 // processed later, but this is still correct to do at this point because we
672 // never rely on LiveIntervals being correct while inserting copies.
673 // FIXME: Should this just count uses at PHIs like the normal PHIElimination
674 // pass does?
675 LiveInterval& SrcLI = LI->getInterval(SrcReg);
676 SlotIndex MBBStartIndex = LI->getMBBStartIdx(MBB);
677 SlotIndex PHIIndex = LI->getInstructionIndex(PHI);
678 SlotIndex NextInstrIndex = PHIIndex.getNextIndex();
679 if (SrcLI.liveAt(MBBStartIndex) && SrcLI.expiredAt(NextInstrIndex))
680 SrcLI.removeRange(MBBStartIndex, PHIIndex, true);
681 }
682
Cameron Zwarich47bce432010-12-21 06:54:43 +0000683 unsigned DestReg = PHI->getOperand(0).getReg();
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000684 unsigned DestColor = getRegColor(DestReg);
685
686 if (PHIColor && DestColor == PHIColor) {
687 LiveInterval& DestLI = LI->getInterval(DestReg);
688
689 // Set the phi-def flag for the VN at this PHI.
690 SlotIndex PHIIndex = LI->getInstructionIndex(PHI);
691 VNInfo* DestVNI = DestLI.getVNInfoAt(PHIIndex.getDefIndex());
692 assert(DestVNI);
693 DestVNI->setIsPHIDef(true);
694
695 // Prior to PHI elimination, the live ranges of PHIs begin at their defining
696 // instruction. After PHI elimination, PHI instructions are replaced by VNs
697 // with the phi-def flag set, and the live ranges of these VNs start at the
698 // beginning of the basic block.
699 SlotIndex MBBStartIndex = LI->getMBBStartIdx(MBB);
700 DestVNI->def = MBBStartIndex;
701 DestLI.addRange(LiveRange(MBBStartIndex,
702 PHIIndex.getDefIndex(),
703 DestVNI));
704 return;
705 }
Cameron Zwarich47bce432010-12-21 06:54:43 +0000706
707 const TargetRegisterClass* RC = MRI->getRegClass(DestReg);
708 unsigned CopyReg = MRI->createVirtualRegister(RC);
709
710 MachineInstr* CopyInstr = BuildMI(*MBB,
711 MBB->SkipPHIsAndLabels(MBB->begin()),
712 PHI->getDebugLoc(),
713 TII->get(TargetOpcode::COPY),
714 DestReg).addReg(CopyReg);
715 LI->InsertMachineInstrInMaps(CopyInstr);
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000716 PHI->getOperand(0).setReg(CopyReg);
Cameron Zwarich47bce432010-12-21 06:54:43 +0000717
718 // Add the region from the beginning of MBB to the copy instruction to
719 // CopyReg's live interval, and give the VNInfo the phidef flag.
720 LiveInterval& CopyLI = LI->getOrCreateInterval(CopyReg);
721 SlotIndex MBBStartIndex = LI->getMBBStartIdx(MBB);
722 SlotIndex DestCopyIndex = LI->getInstructionIndex(CopyInstr);
723 VNInfo* CopyVNI = CopyLI.getNextValue(MBBStartIndex,
724 CopyInstr,
725 LI->getVNInfoAllocator());
726 CopyVNI->setIsPHIDef(true);
727 CopyLI.addRange(LiveRange(MBBStartIndex,
728 DestCopyIndex.getDefIndex(),
729 CopyVNI));
730
731 // Adjust DestReg's live interval to adjust for its new definition at
732 // CopyInstr.
733 LiveInterval& DestLI = LI->getOrCreateInterval(DestReg);
734 SlotIndex PHIIndex = LI->getInstructionIndex(PHI);
735 DestLI.removeRange(PHIIndex.getDefIndex(), DestCopyIndex.getDefIndex());
736
737 VNInfo* DestVNI = DestLI.getVNInfoAt(DestCopyIndex.getDefIndex());
738 assert(DestVNI);
739 DestVNI->def = DestCopyIndex.getDefIndex();
740
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000741 InsertedDestCopies[CopyReg] = CopyInstr;
742}
Cameron Zwarichef485d82010-12-24 03:09:36 +0000743
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000744void StrongPHIElimination::MergeLIsAndRename(unsigned Reg, unsigned NewReg) {
745 if (Reg == NewReg)
746 return;
Cameron Zwarichef485d82010-12-24 03:09:36 +0000747
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000748 LiveInterval& OldLI = LI->getInterval(Reg);
749 LiveInterval& NewLI = LI->getInterval(NewReg);
Cameron Zwarich47bce432010-12-21 06:54:43 +0000750
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000751 // Merge the live ranges of the two registers.
752 DenseMap<VNInfo*, VNInfo*> VNMap;
753 for (LiveInterval::iterator LRI = OldLI.begin(), LRE = OldLI.end();
754 LRI != LRE; ++LRI) {
755 LiveRange OldLR = *LRI;
756 VNInfo* OldVN = OldLR.valno;
Cameron Zwarich47bce432010-12-21 06:54:43 +0000757
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000758 VNInfo*& NewVN = VNMap[OldVN];
759 if (!NewVN) {
760 NewVN = NewLI.createValueCopy(OldVN, LI->getVNInfoAllocator());
761 VNMap[OldVN] = NewVN;
762 }
Cameron Zwarich47bce432010-12-21 06:54:43 +0000763
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000764 LiveRange LR(OldLR.start, OldLR.end, NewVN);
765 NewLI.addRange(LR);
Cameron Zwarich47bce432010-12-21 06:54:43 +0000766 }
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000767
768 // Remove the LiveInterval for the register being renamed and replace all
769 // of its defs and uses with the new register.
770 LI->removeInterval(Reg);
771 MRI->replaceRegWith(Reg, NewReg);
Cameron Zwarich9eaf49b2010-12-05 22:34:08 +0000772}