llvm.org GIT mirror llvm / f235f13
Teach PHIElimination to split critical edges when -split-phi-edges is enabled. Critical edges leading to a PHI node are split when the PHI source variable is live out from the predecessor block. This help the coalescer eliminate more PHI joins. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@86725 91177308-0d34-0410-b5e6-96231b3b80d8 Jakob Stoklund Olesen 10 years ago
4 changed file(s) with 133 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
102102 Kills.erase(I);
103103 return true;
104104 }
105
105
106 /// findKill - Find a kill instruction in MBB. Return NULL if none is found.
107 MachineInstr *findKill(const MachineBasicBlock *MBB) const;
108
106109 void dump() const;
107110 };
108111
262265 void HandleVirtRegDef(unsigned reg, MachineInstr *MI);
263266 void HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
264267 MachineInstr *MI);
268
269 /// addNewBlock - Add a new basic block A as an empty predecessor of B. All
270 /// variables that are live into B will be marked as passing live through A.
271 void addNewBlock(MachineBasicBlock *A, MachineBasicBlock *B);
265272 };
266273
267274 } // End llvm namespace
4747 AU.addRequiredID(UnreachableMachineBlockElimID);
4848 AU.setPreservesAll();
4949 MachineFunctionPass::getAnalysisUsage(AU);
50 }
51
52 MachineInstr *
53 LiveVariables::VarInfo::findKill(const MachineBasicBlock *MBB) const {
54 for (unsigned i = 0, e = Kills.size(); i != e; ++i)
55 if (Kills[i]->getParent() == MBB)
56 return Kills[i];
57 return NULL;
5058 }
5159
5260 void LiveVariables::VarInfo::dump() const {
640648 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()]
641649 .push_back(BBI->getOperand(i).getReg());
642650 }
651
652 void LiveVariables::addNewBlock(MachineBasicBlock *A, MachineBasicBlock *B) {
653 unsigned NumA = A->getNumber();
654 unsigned NumB = B->getNumber();
655
656 // Update info for all live variables
657 for (unsigned i = 0, e = VirtRegInfo.size(); i != e; ++i) {
658 VarInfo &VI = VirtRegInfo[i];
659
660 // Anything live through B is also live through A.
661 if (VI.AliveBlocks.test(NumB)) {
662 VI.AliveBlocks.set(NumA);
663 continue;
664 }
665
666 // If we're not killed in B, we are not live in
667 if (!VI.findKill(B))
668 continue;
669
670 unsigned Reg = i+TargetRegisterInfo::FirstVirtualRegister;
671
672 // Find a def outside B
673 for (MachineRegisterInfo::def_iterator di = MRI->def_begin(Reg),
674 de=MRI->def_end(); di != de; ++di) {
675 if (di->getParent() != B) {
676 // Reg was defined outside B and killed in B - it must be live in.
677 VI.AliveBlocks.set(NumA);
678 break;
679 }
680 }
681 }
682 }
2626 #include "llvm/ADT/SmallPtrSet.h"
2727 #include "llvm/ADT/STLExtras.h"
2828 #include "llvm/ADT/Statistic.h"
29 #include "llvm/Support/CommandLine.h"
2930 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/Debug.h"
3032 #include
3133 #include
3234 using namespace llvm;
3335
3436 STATISTIC(NumAtomic, "Number of atomic phis lowered");
37 STATISTIC(NumSplits, "Number of critical edges split on demand");
38
39 static cl::opt
40 SplitEdges("split-phi-edges",
41 cl::desc("Split critical edges during phi elimination"),
42 cl::init(false), cl::Hidden);
3543
3644 char PHIElimination::ID = 0;
3745 static RegisterPass
4048 const PassInfo *const llvm::PHIEliminationID = &X;
4149
4250 void llvm::PHIElimination::getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.setPreservesCFG();
4451 AU.addPreserved();
45 AU.addPreservedID(MachineLoopInfoID);
46 AU.addPreservedID(MachineDominatorsID);
52 if (SplitEdges) {
53 AU.addRequired();
54 } else {
55 AU.setPreservesCFG();
56 AU.addPreservedID(MachineLoopInfoID);
57 AU.addPreservedID(MachineDominatorsID);
58 }
4759 MachineFunctionPass::getAnalysisUsage(AU);
4860 }
4961
8294 MachineBasicBlock &MBB) {
8395 if (MBB.empty() || MBB.front().getOpcode() != TargetInstrInfo::PHI)
8496 return false; // Quick exit for basic blocks without PHIs.
97
98 if (SplitEdges)
99 SplitPHIEdges(MF, MBB);
85100
86101 // Get an iterator to the first instruction after the last PHI node (this may
87102 // also be the end of the basic block).
276291
277292 // Okay, if we now know that the value is not live out of the block, we can
278293 // add a kill marker in this block saying that it kills the incoming value!
279 if (!ValueIsUsed && !isLiveOut(SrcReg, opBlock, *LV)) {
294 // When SplitEdges is enabled, the value is never live out.
295 if (!ValueIsUsed && (SplitEdges || !isLiveOut(SrcReg, opBlock, *LV))) {
280296 // In our final twist, we have to decide which instruction kills the
281297 // register. In most cases this is the copy, however, the first
282298 // terminator instruction at the end of the block may also use the value.
328344 BBI->getOperand(i).getReg())];
329345 }
330346
347 void llvm::PHIElimination::SplitPHIEdges(MachineFunction &MF,
348 MachineBasicBlock &MBB) {
349 LiveVariables &LV = getAnalysis();
350 for (MachineBasicBlock::const_iterator BBI = MBB.begin(), BBE = MBB.end();
351 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI) {
352 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
353 unsigned Reg = BBI->getOperand(i).getReg();
354 MachineBasicBlock *PreMBB = BBI->getOperand(i+1).getMBB();
355 // We break edges when registers are live out from the predecessor block
356 // (not considering PHI nodes).
357 if (isLiveOut(Reg, *PreMBB, LV))
358 SplitCriticalEdge(PreMBB, &MBB);
359 }
360 }
361 }
362
331363 bool llvm::PHIElimination::isLiveOut(unsigned Reg, const MachineBasicBlock &MBB,
332364 LiveVariables &LV) {
333365 LiveVariables::VarInfo &InRegVI = LV.getVarInfo(Reg);
375407 }
376408 return false;
377409 }
410
411 MachineBasicBlock *PHIElimination::SplitCriticalEdge(MachineBasicBlock *A,
412 MachineBasicBlock *B) {
413 assert(A && B && "Missing MBB end point");
414 ++NumSplits;
415
416 MachineFunction *MF = A->getParent();
417 MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(B->getBasicBlock());
418 MF->push_back(NMBB);
419 const unsigned NewNum = NMBB->getNumber();
420 DEBUG(errs() << "PHIElimination splitting critical edge:"
421 " BB#" << A->getNumber()
422 << " -- BB#" << NewNum
423 << " -- BB#" << B->getNumber() << '\n');
424
425 A->ReplaceUsesOfBlockWith(B, NMBB);
426 NMBB->addSuccessor(B);
427
428 // Insert unconditional "jump B" instruction in NMBB.
429 SmallVector Cond;
430 MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, B, NULL, Cond);
431
432 LiveVariables *LV = getAnalysisIfAvailable();
433 if (LV)
434 LV->addNewBlock(NMBB, B);
435
436 // Fix PHI nodes in B so they refer to NMBB instead of A
437 for (MachineBasicBlock::iterator i = B->begin(), e = B->end();
438 i != e && i->getOpcode() == TargetInstrInfo::PHI; ++i)
439 for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2)
440 if (i->getOperand(ni+1).getMBB() == A) {
441 i->getOperand(ni+1).setMBB(NMBB);
442 // Mark PHI sources as passing live through NMBB
443 if (LV)
444 LV->getVarInfo(i->getOperand(ni).getReg()).AliveBlocks.set(NewNum);
445 }
446 return NMBB;
447 }
448
449
8888 ///
8989 void analyzePHINodes(const MachineFunction& Fn);
9090
91 /// Split critical edges where necessary for good coalescer performance.
92 void SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB);
93
9194 /// isLiveOut - Determine if Reg is live out from MBB, when not
9295 /// considering PHI nodes. This means that Reg is either killed by
9396 /// a successor block or passed through one.
9497 bool isLiveOut(unsigned Reg, const MachineBasicBlock &MBB,
9598 LiveVariables &LV);
99
100 /// SplitCriticalEdge - Split a critical edge from A to B by
101 /// inserting a new MBB. Update branches in A and PHI instructions
102 /// in B. Return the new block.
103 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *A,
104 MachineBasicBlock *B);
96105
97106 // FindCopyInsertPoint - Find a safe place in MBB to insert a copy from
98107 // SrcReg. This needs to be after any def or uses of SrcReg, but before