1 //===- StrongPhiElimination.cpp - Eliminate PHI nodes by inserting copies -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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:
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
20 //===----------------------------------------------------------------------===//
22 #define DEBUG_TYPE "strongphielim"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/CodeGen/LiveVariables.h"
25 #include "llvm/CodeGen/MachineDominators.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/Target/TargetInstrInfo.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/ADT/DepthFirstIterator.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/Support/Compiler.h"
38 struct VISIBILITY_HIDDEN StrongPHIElimination : public MachineFunctionPass {
39 static char ID; // Pass identification, replacement for typeid
40 StrongPHIElimination() : MachineFunctionPass((intptr_t)&ID) {}
42 // Waiting stores, for each MBB, the set of copies that need to
43 // be inserted into that MBB
44 DenseMap<MachineBasicBlock*,
45 std::map<unsigned, unsigned> > Waiting;
47 // Stacks holds the renaming stack for each register
48 std::map<unsigned, std::vector<unsigned> > Stacks;
50 // Registers in UsedByAnother are PHI nodes that are themselves
51 // used as operands to another another PHI node
52 std::set<unsigned> UsedByAnother;
54 // RenameSets are the sets of operands to a PHI (the defining instruction
55 // of the key) that can be renamed without copies
56 std::map<unsigned, std::set<unsigned> > RenameSets;
58 // Store the DFS-in number of each block
59 DenseMap<MachineBasicBlock*, unsigned> preorder;
61 // Store the DFS-out number of each block
62 DenseMap<MachineBasicBlock*, unsigned> maxpreorder;
64 bool runOnMachineFunction(MachineFunction &Fn);
66 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
67 AU.addRequired<MachineDominatorTree>();
68 AU.addRequired<LiveVariables>();
69 MachineFunctionPass::getAnalysisUsage(AU);
72 virtual void releaseMemory() {
78 UsedByAnother.clear();
84 /// DomForestNode - Represents a node in the "dominator forest". This is
85 /// a forest in which the nodes represent registers and the edges
86 /// represent a dominance relation in the block defining those registers.
87 struct DomForestNode {
89 // Store references to our children
90 std::vector<DomForestNode*> children;
91 // The register we represent
94 // Add another node as our child
95 void addChild(DomForestNode* DFN) { children.push_back(DFN); }
98 typedef std::vector<DomForestNode*>::iterator iterator;
100 // Create a DomForestNode by providing the register it represents, and
101 // the node to be its parent. The virtual root node has register 0
102 // and a null parent.
103 DomForestNode(unsigned r, DomForestNode* parent) : reg(r) {
105 parent->addChild(this);
109 for (iterator I = begin(), E = end(); I != E; ++I)
113 /// getReg - Return the regiser that this node represents
114 inline unsigned getReg() { return reg; }
116 // Provide iterator access to our children
117 inline DomForestNode::iterator begin() { return children.begin(); }
118 inline DomForestNode::iterator end() { return children.end(); }
121 void computeDFS(MachineFunction& MF);
122 void processBlock(MachineBasicBlock* MBB);
124 std::vector<DomForestNode*> computeDomForest(std::set<unsigned>& instrs);
125 void processPHIUnion(MachineInstr* Inst,
126 std::set<unsigned>& PHIUnion,
127 std::vector<StrongPHIElimination::DomForestNode*>& DF,
128 std::vector<std::pair<unsigned, unsigned> >& locals);
129 void ScheduleCopies(MachineBasicBlock* MBB, std::set<unsigned>& pushed);
130 void InsertCopies(MachineBasicBlock* MBB, std::set<MachineBasicBlock*>& v);
133 char StrongPHIElimination::ID = 0;
134 RegisterPass<StrongPHIElimination> X("strong-phi-node-elimination",
135 "Eliminate PHI nodes for register allocation, intelligently");
138 const PassInfo *llvm::StrongPHIEliminationID = X.getPassInfo();
140 /// computeDFS - Computes the DFS-in and DFS-out numbers of the dominator tree
141 /// of the given MachineFunction. These numbers are then used in other parts
142 /// of the PHI elimination process.
143 void StrongPHIElimination::computeDFS(MachineFunction& MF) {
144 SmallPtrSet<MachineDomTreeNode*, 8> frontier;
145 SmallPtrSet<MachineDomTreeNode*, 8> visited;
149 MachineDominatorTree& DT = getAnalysis<MachineDominatorTree>();
151 MachineDomTreeNode* node = DT.getRootNode();
153 std::vector<MachineDomTreeNode*> worklist;
154 worklist.push_back(node);
156 while (!worklist.empty()) {
157 MachineDomTreeNode* currNode = worklist.back();
159 if (!frontier.count(currNode)) {
160 frontier.insert(currNode);
162 preorder.insert(std::make_pair(currNode->getBlock(), time));
165 bool inserted = false;
166 for (MachineDomTreeNode::iterator I = node->begin(), E = node->end();
168 if (!frontier.count(*I) && !visited.count(*I)) {
169 worklist.push_back(*I);
175 frontier.erase(currNode);
176 visited.insert(currNode);
177 maxpreorder.insert(std::make_pair(currNode->getBlock(), time));
184 /// PreorderSorter - a helper class that is used to sort registers
185 /// according to the preorder number of their defining blocks
186 class PreorderSorter {
188 DenseMap<MachineBasicBlock*, unsigned>& preorder;
192 PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p,
193 LiveVariables& L) : preorder(p), LV(L) { }
195 bool operator()(unsigned A, unsigned B) {
199 MachineBasicBlock* ABlock = LV.getVarInfo(A).DefInst->getParent();
200 MachineBasicBlock* BBlock = LV.getVarInfo(A).DefInst->getParent();
202 if (preorder[ABlock] < preorder[BBlock])
204 else if (preorder[ABlock] > preorder[BBlock])
211 /// computeDomForest - compute the subforest of the DomTree corresponding
212 /// to the defining blocks of the registers in question
213 std::vector<StrongPHIElimination::DomForestNode*>
214 StrongPHIElimination::computeDomForest(std::set<unsigned>& regs) {
215 LiveVariables& LV = getAnalysis<LiveVariables>();
217 // Begin by creating a virtual root node, since the actual results
218 // may well be a forest. Assume this node has maximum DFS-out number.
219 DomForestNode* VirtualRoot = new DomForestNode(0, 0);
220 maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL));
222 // Populate a worklist with the registers
223 std::vector<unsigned> worklist;
224 worklist.reserve(regs.size());
225 for (std::set<unsigned>::iterator I = regs.begin(), E = regs.end();
227 worklist.push_back(*I);
229 // Sort the registers by the DFS-in number of their defining block
230 PreorderSorter PS(preorder, LV);
231 std::sort(worklist.begin(), worklist.end(), PS);
233 // Create a "current parent" stack, and put the virtual root on top of it
234 DomForestNode* CurrentParent = VirtualRoot;
235 std::vector<DomForestNode*> stack;
236 stack.push_back(VirtualRoot);
238 // Iterate over all the registers in the previously computed order
239 for (std::vector<unsigned>::iterator I = worklist.begin(), E = worklist.end();
241 unsigned pre = preorder[LV.getVarInfo(*I).DefInst->getParent()];
242 MachineBasicBlock* parentBlock = CurrentParent->getReg() ?
243 LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent() :
246 // If the DFS-in number of the register is greater than the DFS-out number
247 // of the current parent, repeatedly pop the parent stack until it isn't.
248 while (pre > maxpreorder[parentBlock]) {
250 CurrentParent = stack.back();
252 parentBlock = CurrentParent->getReg() ?
253 LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent() :
257 // Now that we've found the appropriate parent, create a DomForestNode for
258 // this register and attach it to the forest
259 DomForestNode* child = new DomForestNode(*I, CurrentParent);
261 // Push this new node on the "current parent" stack
262 stack.push_back(child);
263 CurrentParent = child;
266 // Return a vector containing the children of the virtual root node
267 std::vector<DomForestNode*> ret;
268 ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end());
272 /// isLiveIn - helper method that determines, from a VarInfo, if a register
273 /// is live into a block
274 static bool isLiveIn(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) {
275 if (V.AliveBlocks.test(MBB->getNumber()))
278 if (V.DefInst->getParent() != MBB &&
279 V.UsedBlocks.test(MBB->getNumber()))
285 /// isLiveOut - help method that determines, from a VarInfo, if a register is
286 /// live out of a block.
287 static bool isLiveOut(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) {
288 if (MBB == V.DefInst->getParent() ||
289 V.UsedBlocks.test(MBB->getNumber())) {
290 for (std::vector<MachineInstr*>::iterator I = V.Kills.begin(),
291 E = V.Kills.end(); I != E; ++I)
292 if ((*I)->getParent() == MBB)
301 /// isKillInst - helper method that determines, from a VarInfo, if an
302 /// instruction kills a given register
303 static bool isKillInst(LiveVariables::VarInfo& V, MachineInstr* MI) {
304 return std::find(V.Kills.begin(), V.Kills.end(), MI) != V.Kills.end();
307 /// interferes - checks for local interferences by scanning a block. The only
308 /// trick parameter is 'mode' which tells it the relationship of the two
309 /// registers. 0 - defined in the same block, 1 - first properly dominates
310 /// second, 2 - second properly dominates first
311 static bool interferes(LiveVariables::VarInfo& First,
312 LiveVariables::VarInfo& Second,
313 MachineBasicBlock* scan, unsigned mode) {
314 MachineInstr* def = 0;
315 MachineInstr* kill = 0;
317 bool interference = false;
319 // Wallk the block, checking for interferences
320 for (MachineBasicBlock::iterator MBI = scan->begin(), MBE = scan->end();
322 MachineInstr* curr = MBI;
324 // Same defining block...
326 if (curr == First.DefInst) {
327 // If we find our first DefInst, save it
330 // If there's already an unkilled DefInst, then
331 // this is an interference
335 // If there's a DefInst followed by a KillInst, then
336 // they can't interfere
338 interference = false;
341 // Symmetric with the above
342 } else if (curr == Second.DefInst ) {
349 interference = false;
352 // Store KillInsts if they match up with the DefInst
353 } else if (isKillInst(First, curr)) {
354 if (def == First.DefInst) {
356 } else if (isKillInst(Second, curr)) {
357 if (def == Second.DefInst) {
362 // First properly dominates second...
363 } else if (mode == 1) {
364 if (curr == Second.DefInst) {
365 // DefInst of second without kill of first is an interference
369 // DefInst after a kill is a non-interference
371 interference = false;
374 // Save KillInsts of First
375 } else if (isKillInst(First, curr)) {
378 // Symmetric with the above
379 } else if (mode == 2) {
380 if (curr == First.DefInst) {
385 interference = false;
388 } else if (isKillInst(Second, curr)) {
397 /// processBlock - Eliminate PHIs in the given block
398 void StrongPHIElimination::processBlock(MachineBasicBlock* MBB) {
399 LiveVariables& LV = getAnalysis<LiveVariables>();
401 // Holds names that have been added to a set in any PHI within this block
402 // before the current one.
403 std::set<unsigned> ProcessedNames;
405 MachineBasicBlock::iterator P = MBB->begin();
406 while (P != MBB->end() && P->getOpcode() == TargetInstrInfo::PHI) {
407 LiveVariables::VarInfo& PHIInfo = LV.getVarInfo(P->getOperand(0).getReg());
409 unsigned DestReg = P->getOperand(0).getReg();
411 // Hold the names that are currently in the candidate set.
412 std::set<unsigned> PHIUnion;
413 std::set<MachineBasicBlock*> UnionedBlocks;
415 for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
416 unsigned SrcReg = P->getOperand(i-1).getReg();
417 LiveVariables::VarInfo& SrcInfo = LV.getVarInfo(SrcReg);
419 // Check for trivial interferences
420 if (isLiveIn(SrcInfo, P->getParent()) ||
421 isLiveOut(PHIInfo, SrcInfo.DefInst->getParent()) ||
422 ( PHIInfo.DefInst->getOpcode() == TargetInstrInfo::PHI &&
423 isLiveIn(PHIInfo, SrcInfo.DefInst->getParent()) ) ||
424 ProcessedNames.count(SrcReg) ||
425 UnionedBlocks.count(SrcInfo.DefInst->getParent())) {
427 // add a copy from a_i to p in Waiting[From[a_i]]
428 MachineBasicBlock* From = P->getOperand(i).getMBB();
429 Waiting[From].insert(std::make_pair(SrcReg, DestReg));
430 UsedByAnother.insert(SrcReg);
432 PHIUnion.insert(SrcReg);
433 UnionedBlocks.insert(SrcInfo.DefInst->getParent());
437 std::vector<StrongPHIElimination::DomForestNode*> DF =
438 computeDomForest(PHIUnion);
440 // Walk DomForest to resolve interferences
441 std::vector<std::pair<unsigned, unsigned> > localInterferences;
442 processPHIUnion(P, PHIUnion, DF, localInterferences);
444 // Check for local interferences
445 for (std::vector<std::pair<unsigned, unsigned> >::iterator I =
446 localInterferences.begin(), E = localInterferences.end(); I != E; ++I) {
447 std::pair<unsigned, unsigned> p = *I;
449 LiveVariables::VarInfo& FirstInfo = LV.getVarInfo(p.first);
450 LiveVariables::VarInfo& SecondInfo = LV.getVarInfo(p.second);
452 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
454 // Determine the block we need to scan and the relationship between
456 MachineBasicBlock* scan = 0;
458 if (FirstInfo.DefInst->getParent() == SecondInfo.DefInst->getParent()) {
459 scan = FirstInfo.DefInst->getParent();
460 mode = 0; // Same block
461 } else if (MDT.dominates(FirstInfo.DefInst->getParent(),
462 SecondInfo.DefInst->getParent())) {
463 scan = SecondInfo.DefInst->getParent();
464 mode = 1; // First dominates second
466 scan = FirstInfo.DefInst->getParent();
467 mode = 2; // Second dominates first
470 // If there's an interference, we need to insert copies
471 if (interferes(FirstInfo, SecondInfo, scan, mode)) {
472 // Insert copies for First
473 for (int i = P->getNumOperands() - 1; i >= 2; i-=2) {
474 if (P->getOperand(i-1).getReg() == p.first) {
475 unsigned SrcReg = p.first;
476 MachineBasicBlock* From = P->getOperand(i).getMBB();
478 Waiting[From].insert(std::make_pair(SrcReg,
479 P->getOperand(0).getReg()));
480 UsedByAnother.insert(SrcReg);
482 PHIUnion.erase(SrcReg);
488 // Cache renaming information
489 RenameSets.insert(std::make_pair(P->getOperand(0).getReg(), PHIUnion));
491 ProcessedNames.insert(PHIUnion.begin(), PHIUnion.end());
496 /// processPHIUnion - Take a set of candidate registers to be coallesced when
497 /// decomposing the PHI instruction. Use the DominanceForest to remove the ones
498 /// that are known to interfere, and flag others that need to be checked for
499 /// local interferences.
500 void StrongPHIElimination::processPHIUnion(MachineInstr* Inst,
501 std::set<unsigned>& PHIUnion,
502 std::vector<StrongPHIElimination::DomForestNode*>& DF,
503 std::vector<std::pair<unsigned, unsigned> >& locals) {
505 std::vector<DomForestNode*> worklist(DF.begin(), DF.end());
506 SmallPtrSet<DomForestNode*, 4> visited;
508 LiveVariables& LV = getAnalysis<LiveVariables>();
509 unsigned DestReg = Inst->getOperand(0).getReg();
511 // DF walk on the DomForest
512 while (!worklist.empty()) {
513 DomForestNode* DFNode = worklist.back();
515 LiveVariables::VarInfo& Info = LV.getVarInfo(DFNode->getReg());
516 visited.insert(DFNode);
518 bool inserted = false;
519 for (DomForestNode::iterator CI = DFNode->begin(), CE = DFNode->end();
521 DomForestNode* child = *CI;
522 LiveVariables::VarInfo& CInfo = LV.getVarInfo(child->getReg());
524 if (isLiveOut(Info, CInfo.DefInst->getParent())) {
525 // Insert copies for parent
526 for (int i = Inst->getNumOperands() - 1; i >= 2; i-=2) {
527 if (Inst->getOperand(i-1).getReg() == DFNode->getReg()) {
528 unsigned SrcReg = DFNode->getReg();
529 MachineBasicBlock* From = Inst->getOperand(i).getMBB();
531 Waiting[From].insert(std::make_pair(SrcReg, DestReg));
532 UsedByAnother.insert(SrcReg);
534 PHIUnion.erase(SrcReg);
537 } else if (isLiveIn(Info, CInfo.DefInst->getParent()) ||
538 Info.DefInst->getParent() == CInfo.DefInst->getParent()) {
539 // Add (p, c) to possible local interferences
540 locals.push_back(std::make_pair(DFNode->getReg(), child->getReg()));
543 if (!visited.count(child)) {
544 worklist.push_back(child);
549 if (!inserted) worklist.pop_back();
553 /// ScheduleCopies - Insert copies into predecessor blocks, scheduling
554 /// them properly so as to avoid the 'lost copy' and the 'virtual swap'
557 /// Based on "Practical Improvements to the Construction and Destruction
558 /// of Static Single Assignment Form" by Briggs, et al.
559 void StrongPHIElimination::ScheduleCopies(MachineBasicBlock* MBB,
560 std::set<unsigned>& pushed) {
561 // FIXME: This function needs to update LiveVariables
562 std::map<unsigned, unsigned>& copy_set= Waiting[MBB];
564 std::map<unsigned, unsigned> worklist;
565 std::map<unsigned, unsigned> map;
567 // Setup worklist of initial copies
568 for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(),
569 E = copy_set.end(); I != E; ) {
570 map.insert(std::make_pair(I->first, I->first));
571 map.insert(std::make_pair(I->second, I->second));
573 if (!UsedByAnother.count(I->first)) {
576 // Avoid iterator invalidation
577 unsigned first = I->first;
579 copy_set.erase(first);
585 LiveVariables& LV = getAnalysis<LiveVariables>();
586 MachineFunction* MF = MBB->getParent();
587 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
589 // Iterate over the worklist, inserting copies
590 while (!worklist.empty() || !copy_set.empty()) {
591 while (!worklist.empty()) {
592 std::pair<unsigned, unsigned> curr = *worklist.begin();
593 worklist.erase(curr.first);
595 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first);
597 if (isLiveOut(LV.getVarInfo(curr.second), MBB)) {
598 // Create a temporary
599 unsigned t = MF->getRegInfo().createVirtualRegister(RC);
601 // Insert copy from curr.second to a temporary at
602 // the Phi defining curr.second
603 LiveVariables::VarInfo VI = LV.getVarInfo(curr.second);
604 MachineBasicBlock::iterator PI = VI.DefInst;
605 TII->copyRegToReg(*VI.DefInst->getParent(), PI, t,
606 curr.second, RC, RC);
608 // Push temporary on Stacks
609 Stacks[curr.second].push_back(t);
611 // Insert curr.second in pushed
612 pushed.insert(curr.second);
615 // Insert copy from map[curr.first] to curr.second
616 TII->copyRegToReg(*MBB, MBB->getFirstTerminator(), curr.second,
617 map[curr.first], RC, RC);
618 map[curr.first] = curr.second;
620 // If curr.first is a destination in copy_set...
621 for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(),
622 E = copy_set.end(); I != E; )
623 if (curr.first == I->second) {
624 std::pair<unsigned, unsigned> temp = *I;
626 // Avoid iterator invalidation
628 copy_set.erase(temp.first);
629 worklist.insert(temp);
637 if (!copy_set.empty()) {
638 std::pair<unsigned, unsigned> curr = *copy_set.begin();
639 copy_set.erase(curr.first);
641 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first);
643 // Insert a copy from dest to a new temporary t at the end of b
644 unsigned t = MF->getRegInfo().createVirtualRegister(RC);
645 TII->copyRegToReg(*MBB, MBB->getFirstTerminator(), t,
646 curr.second, RC, RC);
647 map[curr.second] = t;
649 worklist.insert(curr);
654 /// InsertCopies - insert copies into MBB and all of its successors
655 void StrongPHIElimination::InsertCopies(MachineBasicBlock* MBB,
656 std::set<MachineBasicBlock*>& visited) {
659 std::set<unsigned> pushed;
661 // Rewrite register uses from Stacks
662 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
664 for (unsigned i = 0; i < I->getNumOperands(); ++i)
665 if (I->getOperand(i).isRegister() &&
666 Stacks[I->getOperand(i).getReg()].size()) {
667 I->getOperand(i).setReg(Stacks[I->getOperand(i).getReg()].back());
670 // Schedule the copies for this block
671 ScheduleCopies(MBB, pushed);
673 // Recur to our successors
674 for (GraphTraits<MachineBasicBlock*>::ChildIteratorType I =
675 GraphTraits<MachineBasicBlock*>::child_begin(MBB), E =
676 GraphTraits<MachineBasicBlock*>::child_end(MBB); I != E; ++I)
677 if (!visited.count(*I))
678 InsertCopies(*I, visited);
680 // As we exit this block, pop the names we pushed while processing it
681 for (std::set<unsigned>::iterator I = pushed.begin(),
682 E = pushed.end(); I != E; ++I)
683 Stacks[*I].pop_back();
686 bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
687 // Compute DFS numbers of each block
690 // Determine which phi node operands need copies
691 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
693 I->begin()->getOpcode() == TargetInstrInfo::PHI)
697 // FIXME: This process should probably preserve LiveVariables
698 std::set<MachineBasicBlock*> visited;
699 InsertCopies(Fn.begin(), visited);
702 typedef std::map<unsigned, std::set<unsigned> > RenameSetType;
703 for (RenameSetType::iterator I = RenameSets.begin(), E = RenameSets.end();
705 for (std::set<unsigned>::iterator SI = I->second.begin(),
706 SE = I->second.end(); SI != SE; ++SI)
707 Fn.getRegInfo().replaceRegWith(*SI, I->first);
709 // FIXME: Insert last-minute copies
712 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
713 for (MachineBasicBlock::iterator BI = I->begin(), BE = I->end();
715 if (BI->getOpcode() == TargetInstrInfo::PHI)
716 BI->eraseFromParent();