blob: 91ec13df643c8101957721e56d8adfb76ef613ec [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
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000110 /// Traverses a basic block, splitting any interferences found between
111 /// registers in the same congruence class. It takes two DenseMaps as
112 /// arguments that it also updates: CurrentDominatingParent, which maps
113 /// a color to the register in that congruence class whose definition was
114 /// most recently seen, and ImmediateDominatingParent, which maps a register
115 /// to the register in the same congruence class that most immediately
116 /// dominates it.
117 ///
118 /// This function assumes that it is being called in a depth-first traversal
119 /// of the dominator tree.
120 void SplitInterferencesForBasicBlock(
121 MachineBasicBlock&,
122 DenseMap<unsigned, unsigned>& CurrentDominatingParent,
123 DenseMap<unsigned, unsigned>& ImmediateDominatingParent);
124
125 // Lowers a PHI instruction, inserting copies of the source and destination
126 // registers as necessary.
Cameron Zwarich47bce432010-12-21 06:54:43 +0000127 void InsertCopiesForPHI(MachineInstr*, MachineBasicBlock*);
128
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000129 // Merges the live interval of Reg into NewReg and renames Reg to NewReg
130 // everywhere that Reg appears. Requires Reg and NewReg to have non-
131 // overlapping lifetimes.
132 void MergeLIsAndRename(unsigned Reg, unsigned NewReg);
133
Cameron Zwarich47bce432010-12-21 06:54:43 +0000134 MachineRegisterInfo* MRI;
135 const TargetInstrInfo* TII;
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000136 MachineDominatorTree* DT;
Cameron Zwarich47bce432010-12-21 06:54:43 +0000137 LiveIntervals* LI;
138
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000139 BumpPtrAllocator Allocator;
140
141 DenseMap<unsigned, Node*> RegNodeMap;
142
Cameron Zwarich1558f5e2010-12-29 11:00:09 +0000143 // Maps a basic block to a list of its defs of registers that appear as PHI
144 // sources.
145 DenseMap<MachineBasicBlock*, std::vector<MachineInstr*> > PHISrcDefs;
146
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000147 // FIXME: Can these two data structures be combined? Would a std::multimap
148 // be any better?
149
150 // Stores pairs of predecessor basic blocks and the source registers of
151 // inserted copy instructions.
152 typedef DenseSet<std::pair<MachineBasicBlock*, unsigned> > SrcCopySet;
153 SrcCopySet InsertedSrcCopySet;
154
155 // Maps pairs of predecessor basic blocks and colors to their defining copy
156 // instructions.
157 typedef DenseMap<std::pair<MachineBasicBlock*, unsigned>, MachineInstr*>
158 SrcCopyMap;
159 SrcCopyMap InsertedSrcCopyMap;
160
161 // Maps inserted destination copy registers to their defining copy
162 // instructions.
163 typedef DenseMap<unsigned, MachineInstr*> DestCopyMap;
164 DestCopyMap InsertedDestCopies;
Cameron Zwarich9eaf49b2010-12-05 22:34:08 +0000165 };
Cameron Zwarich1558f5e2010-12-29 11:00:09 +0000166
167 struct MIIndexCompare {
168 MIIndexCompare(LiveIntervals* LiveIntervals) : LI(LiveIntervals) { }
169
170 bool operator()(const MachineInstr* LHS, const MachineInstr* RHS) const {
171 return LI->getInstructionIndex(LHS) < LI->getInstructionIndex(RHS);
172 }
173
174 LiveIntervals* LI;
175 };
Jakob Stoklund Olesen68be9562010-12-03 19:21:53 +0000176} // namespace
Owen Anderson0bda0e82007-10-31 03:37:57 +0000177
Dan Gohman844731a2008-05-13 00:00:25 +0000178char StrongPHIElimination::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +0000179INITIALIZE_PASS_BEGIN(StrongPHIElimination, "strong-phi-node-elimination",
180 "Eliminate PHI nodes for register allocation, intelligently", false, false)
181INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
182INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
183INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
184INITIALIZE_PASS_END(StrongPHIElimination, "strong-phi-node-elimination",
Owen Andersonce665bd2010-10-07 22:25:06 +0000185 "Eliminate PHI nodes for register allocation, intelligently", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +0000186
Owen Anderson90c579d2010-08-06 18:33:48 +0000187char &llvm::StrongPHIEliminationID = StrongPHIElimination::ID;
Owen Anderson0bda0e82007-10-31 03:37:57 +0000188
Cameron Zwarich9eaf49b2010-12-05 22:34:08 +0000189void StrongPHIElimination::getAnalysisUsage(AnalysisUsage& AU) const {
190 AU.setPreservesCFG();
191 AU.addRequired<MachineDominatorTree>();
192 AU.addRequired<SlotIndexes>();
193 AU.addPreserved<SlotIndexes>();
194 AU.addRequired<LiveIntervals>();
195 AU.addPreserved<LiveIntervals>();
196 MachineFunctionPass::getAnalysisUsage(AU);
197}
198
Cameron Zwarich47bce432010-12-21 06:54:43 +0000199static MachineOperand* findLastUse(MachineBasicBlock* MBB, unsigned Reg) {
200 // FIXME: This only needs to check from the first terminator, as only the
201 // first terminator can use a virtual register.
202 for (MachineBasicBlock::reverse_iterator RI = MBB->rbegin(); ; ++RI) {
203 assert (RI != MBB->rend());
204 MachineInstr* MI = &*RI;
205
206 for (MachineInstr::mop_iterator OI = MI->operands_begin(),
207 OE = MI->operands_end(); OI != OE; ++OI) {
208 MachineOperand& MO = *OI;
209 if (MO.isReg() && MO.isUse() && MO.getReg() == Reg)
210 return &MO;
211 }
212 }
213 return NULL;
214}
215
216bool StrongPHIElimination::runOnMachineFunction(MachineFunction& MF) {
217 MRI = &MF.getRegInfo();
218 TII = MF.getTarget().getInstrInfo();
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000219 DT = &getAnalysis<MachineDominatorTree>();
Cameron Zwarich47bce432010-12-21 06:54:43 +0000220 LI = &getAnalysis<LiveIntervals>();
221
Cameron Zwarich1558f5e2010-12-29 11:00:09 +0000222 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
223 I != E; ++I) {
224 for (MachineBasicBlock::iterator BBI = I->begin(), BBE = I->end();
225 BBI != BBE && BBI->isPHI(); ++BBI) {
226 unsigned DestReg = BBI->getOperand(0).getReg();
227 addReg(DestReg);
228 PHISrcDefs[I].push_back(BBI);
229
230 for (unsigned i = 1; i < BBI->getNumOperands(); i += 2) {
231 MachineOperand& SrcMO = BBI->getOperand(i);
232 unsigned SrcReg = SrcMO.getReg();
233 addReg(SrcReg);
234 unionRegs(DestReg, SrcReg);
235
Cameron Zwarichd16ad3e2010-12-30 00:42:23 +0000236 MachineInstr* DefMI = MRI->getVRegDef(SrcReg);
237 if (DefMI)
238 PHISrcDefs[DefMI->getParent()].push_back(DefMI);
Cameron Zwarich1558f5e2010-12-29 11:00:09 +0000239 }
240 }
241 }
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000242
243 // Perform a depth-first traversal of the dominator tree, splitting
244 // interferences amongst PHI-congruence classes.
245 DenseMap<unsigned, unsigned> CurrentDominatingParent;
246 DenseMap<unsigned, unsigned> ImmediateDominatingParent;
247 for (df_iterator<MachineDomTreeNode*> DI = df_begin(DT->getRootNode()),
248 DE = df_end(DT->getRootNode()); DI != DE; ++DI) {
249 SplitInterferencesForBasicBlock(*DI->getBlock(),
250 CurrentDominatingParent,
251 ImmediateDominatingParent);
252 }
253
Cameron Zwarich47bce432010-12-21 06:54:43 +0000254 // Insert copies for all PHI source and destination registers.
255 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
256 I != E; ++I) {
257 for (MachineBasicBlock::iterator BBI = I->begin(), BBE = I->end();
258 BBI != BBE && BBI->isPHI(); ++BBI) {
259 InsertCopiesForPHI(BBI, I);
260 }
261 }
262
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000263 // FIXME: Preserve the equivalence classes during copy insertion and use
264 // the preversed equivalence classes instead of recomputing them.
265 RegNodeMap.clear();
Cameron Zwarich1558f5e2010-12-29 11:00:09 +0000266 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
267 I != E; ++I) {
268 for (MachineBasicBlock::iterator BBI = I->begin(), BBE = I->end();
269 BBI != BBE && BBI->isPHI(); ++BBI) {
270 unsigned DestReg = BBI->getOperand(0).getReg();
271 addReg(DestReg);
272
273 for (unsigned i = 1; i < BBI->getNumOperands(); i += 2) {
274 unsigned SrcReg = BBI->getOperand(i).getReg();
275 addReg(SrcReg);
276 unionRegs(DestReg, SrcReg);
277 }
278 }
279 }
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000280
281 DenseMap<unsigned, unsigned> RegRenamingMap;
282 bool Changed = false;
283 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
284 I != E; ++I) {
285 MachineBasicBlock::iterator BBI = I->begin(), BBE = I->end();
286 while (BBI != BBE && BBI->isPHI()) {
287 MachineInstr* PHI = BBI;
288
289 assert(PHI->getNumOperands() > 0);
290
291 unsigned SrcReg = PHI->getOperand(1).getReg();
292 unsigned SrcColor = getRegColor(SrcReg);
293 unsigned NewReg = RegRenamingMap[SrcColor];
294 if (!NewReg) {
295 NewReg = SrcReg;
296 RegRenamingMap[SrcColor] = SrcReg;
297 }
298 MergeLIsAndRename(SrcReg, NewReg);
299
300 unsigned DestReg = PHI->getOperand(0).getReg();
301 if (!InsertedDestCopies.count(DestReg))
302 MergeLIsAndRename(DestReg, NewReg);
303
304 for (unsigned i = 3; i < PHI->getNumOperands(); i += 2) {
305 unsigned SrcReg = PHI->getOperand(i).getReg();
306 MergeLIsAndRename(SrcReg, NewReg);
307 }
308
309 ++BBI;
310 LI->RemoveMachineInstrFromMaps(PHI);
311 PHI->eraseFromParent();
312 Changed = true;
313 }
314 }
315
316 // Due to the insertion of copies to split live ranges, the live intervals are
317 // guaranteed to not overlap, except in one case: an original PHI source and a
318 // PHI destination copy. In this case, they have the same value and thus don't
319 // truly intersect, so we merge them into the value live at that point.
320 // FIXME: Is there some better way we can handle this?
321 for (DestCopyMap::iterator I = InsertedDestCopies.begin(),
322 E = InsertedDestCopies.end(); I != E; ++I) {
323 unsigned DestReg = I->first;
324 unsigned DestColor = getRegColor(DestReg);
325 unsigned NewReg = RegRenamingMap[DestColor];
326
327 LiveInterval& DestLI = LI->getInterval(DestReg);
328 LiveInterval& NewLI = LI->getInterval(NewReg);
329
Cameron Zwarich480b80a2010-12-29 03:52:51 +0000330 assert(DestLI.ranges.size() == 1
331 && "PHI destination copy's live interval should be a single live "
332 "range from the beginning of the BB to the copy instruction.");
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000333 LiveRange* DestLR = DestLI.begin();
334 VNInfo* NewVNI = NewLI.getVNInfoAt(DestLR->start);
335 if (!NewVNI) {
336 NewVNI = NewLI.createValueCopy(DestLR->valno, LI->getVNInfoAllocator());
337 MachineInstr* CopyInstr = I->second;
338 CopyInstr->getOperand(1).setIsKill(true);
339 }
340
341 LiveRange NewLR(DestLR->start, DestLR->end, NewVNI);
342 NewLI.addRange(NewLR);
343
344 LI->removeInterval(DestReg);
345 MRI->replaceRegWith(DestReg, NewReg);
346 }
347
Cameron Zwarich47bce432010-12-21 06:54:43 +0000348 // Adjust the live intervals of all PHI source registers to handle the case
349 // where the PHIs in successor blocks were the only later uses of the source
350 // register.
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000351 for (SrcCopySet::iterator I = InsertedSrcCopySet.begin(),
352 E = InsertedSrcCopySet.end(); I != E; ++I) {
Cameron Zwarich47bce432010-12-21 06:54:43 +0000353 MachineBasicBlock* MBB = I->first;
354 unsigned SrcReg = I->second;
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000355 if (unsigned RenamedRegister = RegRenamingMap[getRegColor(SrcReg)])
356 SrcReg = RenamedRegister;
357
Cameron Zwarich47bce432010-12-21 06:54:43 +0000358 LiveInterval& SrcLI = LI->getInterval(SrcReg);
359
360 bool isLiveOut = false;
361 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
362 SE = MBB->succ_end(); SI != SE; ++SI) {
363 if (SrcLI.liveAt(LI->getMBBStartIdx(*SI))) {
364 isLiveOut = true;
365 break;
366 }
367 }
368
369 if (isLiveOut)
370 continue;
371
372 MachineOperand* LastUse = findLastUse(MBB, SrcReg);
373 assert(LastUse);
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000374 SlotIndex LastUseIndex = LI->getInstructionIndex(LastUse->getParent());
375 SrcLI.removeRange(LastUseIndex.getDefIndex(), LI->getMBBEndIdx(MBB));
Cameron Zwarich47bce432010-12-21 06:54:43 +0000376 LastUse->setIsKill(true);
377 }
378
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000379 LI->renumber();
380
381 Allocator.Reset();
382 RegNodeMap.clear();
Cameron Zwarich1558f5e2010-12-29 11:00:09 +0000383 PHISrcDefs.clear();
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000384 InsertedSrcCopySet.clear();
385 InsertedSrcCopyMap.clear();
386 InsertedDestCopies.clear();
387
388 return Changed;
389}
390
391void StrongPHIElimination::addReg(unsigned Reg) {
392 if (RegNodeMap.count(Reg))
393 return;
394 RegNodeMap[Reg] = new (Allocator) Node(Reg);
395}
396
397StrongPHIElimination::Node*
398StrongPHIElimination::Node::getLeader() {
399 Node* parentPointer = parent.getPointer();
400 if (parentPointer == this)
401 return this;
402 Node* newParent = parentPointer->getLeader();
403 parent.setPointer(newParent);
404 return newParent;
405}
406
407unsigned StrongPHIElimination::getRegColor(unsigned Reg) {
408 DenseMap<unsigned, Node*>::iterator RI = RegNodeMap.find(Reg);
409 if (RI == RegNodeMap.end())
410 return 0;
411 Node* Node = RI->second;
412 if (Node->parent.getInt() & Node::kRegisterIsolatedFlag)
413 return 0;
414 return Node->getLeader()->value;
415}
416
417void StrongPHIElimination::unionRegs(unsigned Reg1, unsigned Reg2) {
418 Node* Node1 = RegNodeMap[Reg1]->getLeader();
419 Node* Node2 = RegNodeMap[Reg2]->getLeader();
420
421 if (Node1->rank > Node2->rank) {
422 Node2->parent.setPointer(Node1->getLeader());
423 } else if (Node1->rank < Node2->rank) {
424 Node1->parent.setPointer(Node2->getLeader());
425 } else if (Node1 != Node2) {
426 Node2->parent.setPointer(Node1->getLeader());
427 Node1->rank++;
428 }
429}
430
431void StrongPHIElimination::isolateReg(unsigned Reg) {
432 Node* Node = RegNodeMap[Reg];
433 Node->parent.setInt(Node->parent.getInt() | Node::kRegisterIsolatedFlag);
434}
435
436unsigned StrongPHIElimination::getPHIColor(MachineInstr* PHI) {
437 assert(PHI->isPHI());
438
439 unsigned DestReg = PHI->getOperand(0).getReg();
440 Node* DestNode = RegNodeMap[DestReg];
441 if (DestNode->parent.getInt() & Node::kPHIIsolatedFlag)
442 return 0;
443
444 for (unsigned i = 1; i < PHI->getNumOperands(); i += 2) {
445 unsigned SrcColor = getRegColor(PHI->getOperand(i).getReg());
446 if (SrcColor)
447 return SrcColor;
448 }
449 return 0;
450}
451
452void StrongPHIElimination::isolatePHI(MachineInstr* PHI) {
453 assert(PHI->isPHI());
454 Node* Node = RegNodeMap[PHI->getOperand(0).getReg()];
455 Node->parent.setInt(Node->parent.getInt() | Node::kPHIIsolatedFlag);
456}
457
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000458/// SplitInterferencesForBasicBlock - traverses a basic block, splitting any
459/// interferences found between registers in the same congruence class. It
460/// takes two DenseMaps as arguments that it also updates:
461///
462/// 1) CurrentDominatingParent, which maps a color to the register in that
463/// congruence class whose definition was most recently seen.
464///
465/// 2) ImmediateDominatingParent, which maps a register to the register in the
466/// same congruence class that most immediately dominates it.
467///
468/// This function assumes that it is being called in a depth-first traversal
469/// of the dominator tree.
470///
471/// The algorithm used here is a generalization of the dominance-based SSA test
472/// for two variables. If there are variables a_1, ..., a_n such that
473///
474/// def(a_1) dom ... dom def(a_n),
475///
476/// then we can test for an interference between any two a_i by only using O(n)
477/// interference tests between pairs of variables. If i < j and a_i and a_j
478/// interfere, then a_i is alive at def(a_j), so it is also alive at def(a_i+1).
479/// Thus, in order to test for an interference involving a_i, we need only check
480/// for a potential interference with a_i+1.
481///
482/// This method can be generalized to arbitrary sets of variables by performing
483/// a depth-first traversal of the dominator tree. As we traverse down a branch
484/// of the dominator tree, we keep track of the current dominating variable and
485/// only perform an interference test with that variable. However, when we go to
486/// another branch of the dominator tree, the definition of the current dominating
487/// variable may no longer dominate the current block. In order to correct this,
488/// we need to use a stack of past choices of the current dominating variable
489/// and pop from this stack until we find a variable whose definition actually
490/// dominates the current block.
491///
492/// There will be one push on this stack for each variable that has become the
493/// current dominating variable, so instead of using an explicit stack we can
494/// simply associate the previous choice for a current dominating variable with
495/// the new choice. This works better in our implementation, where we test for
496/// interference in multiple distinct sets at once.
497void
498StrongPHIElimination::SplitInterferencesForBasicBlock(
499 MachineBasicBlock& MBB,
500 DenseMap<unsigned, unsigned>& CurrentDominatingParent,
501 DenseMap<unsigned, unsigned>& ImmediateDominatingParent) {
Cameron Zwarich1558f5e2010-12-29 11:00:09 +0000502 // Sort defs by their order in the original basic block, as the code below
503 // assumes that it is processing definitions in dominance order.
504 std::vector<MachineInstr*>& DefInstrs = PHISrcDefs[&MBB];
505 std::sort(DefInstrs.begin(), DefInstrs.end(), MIIndexCompare(LI));
506
507 for (std::vector<MachineInstr*>::const_iterator BBI = DefInstrs.begin(),
508 BBE = DefInstrs.end(); BBI != BBE; ++BBI) {
509 for (MachineInstr::const_mop_iterator I = (*BBI)->operands_begin(),
510 E = (*BBI)->operands_end(); I != E; ++I) {
Cameron Zwarich92f0fcb62010-12-28 10:49:33 +0000511 const MachineOperand& MO = *I;
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000512
Cameron Zwarich438e25c2010-12-28 23:02:56 +0000513 // FIXME: This would be faster if it were possible to bail out of checking
514 // an instruction's operands after the explicit defs, but this is incorrect
515 // for variadic instructions, which may appear before register allocation
516 // in the future.
517 if (!MO.isReg() || !MO.isDef())
518 continue;
519
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000520 unsigned DestReg = MO.getReg();
521 if (!DestReg || !TargetRegisterInfo::isVirtualRegister(DestReg))
522 continue;
523
524 // If the virtual register being defined is not used in any PHI or has
525 // already been isolated, then there are no more interferences to check.
526 unsigned DestColor = getRegColor(DestReg);
527 if (!DestColor)
528 continue;
529
530 // The input to this pass sometimes is not in SSA form in every basic
531 // block, as some virtual registers have redefinitions. We could eliminate
532 // this by fixing the passes that generate the non-SSA code, or we could
533 // handle it here by tracking defining machine instructions rather than
534 // virtual registers. For now, we just handle the situation conservatively
535 // in a way that will possibly lead to false interferences.
536 unsigned NewParent = CurrentDominatingParent[DestColor];
537 if (NewParent == DestReg)
538 continue;
539
540 // Pop registers from the stack represented by ImmediateDominatingParent
541 // until we find a parent that dominates the current instruction.
Cameron Zwarich1558f5e2010-12-29 11:00:09 +0000542 while (NewParent && (!DT->dominates(MRI->getVRegDef(NewParent), *BBI)
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000543 || !getRegColor(NewParent)))
544 NewParent = ImmediateDominatingParent[NewParent];
545
546 // If NewParent is nonzero, then its definition dominates the current
547 // instruction, so it is only necessary to check for the liveness of
548 // NewParent in order to check for an interference.
549 if (NewParent
Cameron Zwarich1558f5e2010-12-29 11:00:09 +0000550 && LI->getInterval(NewParent).liveAt(LI->getInstructionIndex(*BBI))) {
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000551 // If there is an interference, always isolate the new register. This
552 // could be improved by using a heuristic that decides which of the two
553 // registers to isolate.
554 isolateReg(DestReg);
555 CurrentDominatingParent[DestColor] = NewParent;
556 } else {
557 // If there is no interference, update ImmediateDominatingParent and set
558 // the CurrentDominatingParent for this color to the current register.
559 ImmediateDominatingParent[DestReg] = NewParent;
560 CurrentDominatingParent[DestColor] = DestReg;
561 }
Cameron Zwarich47bce432010-12-21 06:54:43 +0000562 }
563 }
564
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000565 // We now walk the PHIs in successor blocks and check for interferences. This
566 // is necesary because the use of a PHI's operands are logically contained in
567 // the predecessor block. The def of a PHI's destination register is processed
568 // along with the other defs in a basic block.
Cameron Zwarich47bce432010-12-21 06:54:43 +0000569
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000570 // The map CurrentPHIForColor maps a color to a pair of a MachineInstr* and a
571 // virtual register, which is the operand of that PHI corresponding to the
572 // current basic block.
573 // FIXME: This should use a container that doesn't always perform heap
574 // allocation.
575 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> > CurrentPHIForColor;
Cameron Zwarich47bce432010-12-21 06:54:43 +0000576
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000577 for (MachineBasicBlock::succ_iterator SI = MBB.succ_begin(),
578 SE = MBB.succ_end(); SI != SE; ++SI) {
579 for (MachineBasicBlock::iterator BBI = (*SI)->begin(), BBE = (*SI)->end();
580 BBI != BBE && BBI->isPHI(); ++BBI) {
581 MachineInstr* PHI = BBI;
582
583 // If a PHI is already isolated, either by being isolated directly or
584 // having all of its operands isolated, ignore it.
585 unsigned Color = getPHIColor(PHI);
586 if (!Color)
587 continue;
588
589 // Find the index of the PHI operand that corresponds to this basic block.
590 unsigned PredIndex;
591 for (PredIndex = 1; PredIndex < PHI->getNumOperands(); PredIndex += 2) {
592 if (PHI->getOperand(PredIndex + 1).getMBB() == &MBB)
593 break;
594 }
595 assert(PredIndex < PHI->getNumOperands());
596 unsigned PredOperandReg = PHI->getOperand(PredIndex).getReg();
597
598 // Pop registers from the stack represented by ImmediateDominatingParent
599 // until we find a parent that dominates the current instruction.
600 unsigned NewParent = CurrentDominatingParent[Color];
601 while (NewParent
602 && (!DT->dominates(MRI->getVRegDef(NewParent)->getParent(), &MBB)
603 || !getRegColor(NewParent)))
604 NewParent = ImmediateDominatingParent[NewParent];
605 CurrentDominatingParent[Color] = NewParent;
606
607 // If there is an interference with a register, always isolate the
608 // register rather than the PHI. It is also possible to isolate the
609 // PHI, but that introduces copies for all of the registers involved
610 // in that PHI.
611 if (NewParent && LI->isLiveOutOfMBB(LI->getInterval(NewParent), &MBB)
612 && NewParent != PredOperandReg)
613 isolateReg(NewParent);
614
615 std::pair<MachineInstr*, unsigned> CurrentPHI = CurrentPHIForColor[Color];
616
617 // If two PHIs have the same operand from every shared predecessor, then
618 // they don't actually interfere. Otherwise, isolate the current PHI. This
619 // could possibly be improved, e.g. we could isolate the PHI with the
620 // fewest operands.
621 if (CurrentPHI.first && CurrentPHI.second != PredOperandReg)
622 isolatePHI(PHI);
623 else
624 CurrentPHIForColor[Color] = std::make_pair(PHI, PredOperandReg);
625 }
626 }
Cameron Zwarich47bce432010-12-21 06:54:43 +0000627}
628
629void StrongPHIElimination::InsertCopiesForPHI(MachineInstr* PHI,
630 MachineBasicBlock* MBB) {
631 assert(PHI->isPHI());
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000632 unsigned PHIColor = getPHIColor(PHI);
633
634 for (unsigned i = 1; i < PHI->getNumOperands(); i += 2) {
635 MachineOperand& SrcMO = PHI->getOperand(i);
636
637 // If a source is defined by an implicit def, there is no need to insert a
638 // copy in the predecessor.
639 if (SrcMO.isUndef())
640 continue;
641
642 unsigned SrcReg = SrcMO.getReg();
643 assert(TargetRegisterInfo::isVirtualRegister(SrcReg) &&
644 "Machine PHI Operands must all be virtual registers!");
645
646 MachineBasicBlock* PredBB = PHI->getOperand(i + 1).getMBB();
647 unsigned SrcColor = getRegColor(SrcReg);
648
649 // If neither the PHI nor the operand were isolated, then we only need to
650 // set the phi-kill flag on the VNInfo at this PHI.
651 if (PHIColor && SrcColor == PHIColor) {
652 LiveInterval& SrcInterval = LI->getInterval(SrcReg);
653 SlotIndex PredIndex = LI->getMBBEndIdx(PredBB);
654 VNInfo* SrcVNI = SrcInterval.getVNInfoAt(PredIndex.getPrevIndex());
655 assert(SrcVNI);
656 SrcVNI->setHasPHIKill(true);
657 continue;
658 }
659
660 unsigned CopyReg = 0;
661 if (PHIColor) {
662 SrcCopyMap::const_iterator I
663 = InsertedSrcCopyMap.find(std::make_pair(PredBB, PHIColor));
664 CopyReg
665 = I != InsertedSrcCopyMap.end() ? I->second->getOperand(0).getReg() : 0;
666 }
667
668 if (!CopyReg) {
669 const TargetRegisterClass* RC = MRI->getRegClass(SrcReg);
670 CopyReg = MRI->createVirtualRegister(RC);
671
672 MachineBasicBlock::iterator
673 CopyInsertPoint = findPHICopyInsertPoint(PredBB, MBB, SrcReg);
674 unsigned SrcSubReg = SrcMO.getSubReg();
675 MachineInstr* CopyInstr = BuildMI(*PredBB,
676 CopyInsertPoint,
677 PHI->getDebugLoc(),
678 TII->get(TargetOpcode::COPY),
679 CopyReg).addReg(SrcReg, 0, SrcSubReg);
680 LI->InsertMachineInstrInMaps(CopyInstr);
681
682 // addLiveRangeToEndOfBlock() also adds the phikill flag to the VNInfo for
683 // the newly added range.
684 LI->addLiveRangeToEndOfBlock(CopyReg, CopyInstr);
685 InsertedSrcCopySet.insert(std::make_pair(PredBB, SrcReg));
686
687 addReg(CopyReg);
688 if (PHIColor) {
689 unionRegs(PHIColor, CopyReg);
690 assert(getRegColor(CopyReg) != CopyReg);
691 } else {
692 PHIColor = CopyReg;
693 assert(getRegColor(CopyReg) == CopyReg);
694 }
695
696 if (!InsertedSrcCopyMap.count(std::make_pair(PredBB, PHIColor)))
697 InsertedSrcCopyMap[std::make_pair(PredBB, PHIColor)] = CopyInstr;
698 }
699
700 SrcMO.setReg(CopyReg);
701
702 // If SrcReg is not live beyond the PHI, trim its interval so that it is no
703 // longer live-in to MBB. Note that SrcReg may appear in other PHIs that are
704 // processed later, but this is still correct to do at this point because we
705 // never rely on LiveIntervals being correct while inserting copies.
706 // FIXME: Should this just count uses at PHIs like the normal PHIElimination
707 // pass does?
708 LiveInterval& SrcLI = LI->getInterval(SrcReg);
709 SlotIndex MBBStartIndex = LI->getMBBStartIdx(MBB);
710 SlotIndex PHIIndex = LI->getInstructionIndex(PHI);
711 SlotIndex NextInstrIndex = PHIIndex.getNextIndex();
712 if (SrcLI.liveAt(MBBStartIndex) && SrcLI.expiredAt(NextInstrIndex))
713 SrcLI.removeRange(MBBStartIndex, PHIIndex, true);
714 }
715
Cameron Zwarich47bce432010-12-21 06:54:43 +0000716 unsigned DestReg = PHI->getOperand(0).getReg();
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000717 unsigned DestColor = getRegColor(DestReg);
718
719 if (PHIColor && DestColor == PHIColor) {
720 LiveInterval& DestLI = LI->getInterval(DestReg);
721
722 // Set the phi-def flag for the VN at this PHI.
723 SlotIndex PHIIndex = LI->getInstructionIndex(PHI);
724 VNInfo* DestVNI = DestLI.getVNInfoAt(PHIIndex.getDefIndex());
725 assert(DestVNI);
726 DestVNI->setIsPHIDef(true);
727
728 // Prior to PHI elimination, the live ranges of PHIs begin at their defining
729 // instruction. After PHI elimination, PHI instructions are replaced by VNs
730 // with the phi-def flag set, and the live ranges of these VNs start at the
731 // beginning of the basic block.
732 SlotIndex MBBStartIndex = LI->getMBBStartIdx(MBB);
733 DestVNI->def = MBBStartIndex;
734 DestLI.addRange(LiveRange(MBBStartIndex,
735 PHIIndex.getDefIndex(),
736 DestVNI));
737 return;
738 }
Cameron Zwarich47bce432010-12-21 06:54:43 +0000739
740 const TargetRegisterClass* RC = MRI->getRegClass(DestReg);
741 unsigned CopyReg = MRI->createVirtualRegister(RC);
742
743 MachineInstr* CopyInstr = BuildMI(*MBB,
744 MBB->SkipPHIsAndLabels(MBB->begin()),
745 PHI->getDebugLoc(),
746 TII->get(TargetOpcode::COPY),
747 DestReg).addReg(CopyReg);
748 LI->InsertMachineInstrInMaps(CopyInstr);
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000749 PHI->getOperand(0).setReg(CopyReg);
Cameron Zwarich47bce432010-12-21 06:54:43 +0000750
751 // Add the region from the beginning of MBB to the copy instruction to
752 // CopyReg's live interval, and give the VNInfo the phidef flag.
753 LiveInterval& CopyLI = LI->getOrCreateInterval(CopyReg);
754 SlotIndex MBBStartIndex = LI->getMBBStartIdx(MBB);
755 SlotIndex DestCopyIndex = LI->getInstructionIndex(CopyInstr);
756 VNInfo* CopyVNI = CopyLI.getNextValue(MBBStartIndex,
757 CopyInstr,
758 LI->getVNInfoAllocator());
759 CopyVNI->setIsPHIDef(true);
760 CopyLI.addRange(LiveRange(MBBStartIndex,
761 DestCopyIndex.getDefIndex(),
762 CopyVNI));
763
764 // Adjust DestReg's live interval to adjust for its new definition at
765 // CopyInstr.
766 LiveInterval& DestLI = LI->getOrCreateInterval(DestReg);
767 SlotIndex PHIIndex = LI->getInstructionIndex(PHI);
768 DestLI.removeRange(PHIIndex.getDefIndex(), DestCopyIndex.getDefIndex());
769
770 VNInfo* DestVNI = DestLI.getVNInfoAt(DestCopyIndex.getDefIndex());
771 assert(DestVNI);
772 DestVNI->def = DestCopyIndex.getDefIndex();
773
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000774 InsertedDestCopies[CopyReg] = CopyInstr;
775}
Cameron Zwarichef485d82010-12-24 03:09:36 +0000776
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000777void StrongPHIElimination::MergeLIsAndRename(unsigned Reg, unsigned NewReg) {
778 if (Reg == NewReg)
779 return;
Cameron Zwarichef485d82010-12-24 03:09:36 +0000780
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000781 LiveInterval& OldLI = LI->getInterval(Reg);
782 LiveInterval& NewLI = LI->getInterval(NewReg);
Cameron Zwarich47bce432010-12-21 06:54:43 +0000783
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000784 // Merge the live ranges of the two registers.
785 DenseMap<VNInfo*, VNInfo*> VNMap;
786 for (LiveInterval::iterator LRI = OldLI.begin(), LRE = OldLI.end();
787 LRI != LRE; ++LRI) {
788 LiveRange OldLR = *LRI;
789 VNInfo* OldVN = OldLR.valno;
Cameron Zwarich47bce432010-12-21 06:54:43 +0000790
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000791 VNInfo*& NewVN = VNMap[OldVN];
792 if (!NewVN) {
793 NewVN = NewLI.createValueCopy(OldVN, LI->getVNInfoAllocator());
794 VNMap[OldVN] = NewVN;
795 }
Cameron Zwarich47bce432010-12-21 06:54:43 +0000796
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000797 LiveRange LR(OldLR.start, OldLR.end, NewVN);
798 NewLI.addRange(LR);
Cameron Zwarich47bce432010-12-21 06:54:43 +0000799 }
Cameron Zwarich4e7f23b2010-12-27 10:08:19 +0000800
801 // Remove the LiveInterval for the register being renamed and replace all
802 // of its defs and uses with the new register.
803 LI->removeInterval(Reg);
804 MRI->replaceRegWith(Reg, NewReg);
Cameron Zwarich9eaf49b2010-12-05 22:34:08 +0000805}