llvm.org GIT mirror llvm / 3e20475
Fix liveness calculation when splitting critical edges during PHI elimination. - Edges are split before any phis are eliminated, so the code is SSA. - Create a proper IR BasicBlock for the split edges. - LiveVariables::addNewBlock now has same syntax as MachineDominatorTree::addNewBlock. Algorithm calculates predecessor live-out set rather than successor live-in set. This feature still causes some miscompilations. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@86867 91177308-0d34-0410-b5e6-96231b3b80d8 Jakob Stoklund Olesen 10 years ago
4 changed file(s) with 88 addition(s) and 60 deletion(s). Raw diff Collapse all Expand all
266266 void HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
267267 MachineInstr *MI);
268268
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);
269 /// addNewBlock - Add a new basic block BB as an empty succcessor to
270 /// DomBB. All variables that are live out of DomBB will be marked as passing
271 /// live through BB. This method assumes that the machine code is still in SSA
272 /// form.
273 void addNewBlock(MachineBasicBlock *BB, MachineBasicBlock *DomBB);
272274 };
273275
274276 } // End llvm namespace
649649 .push_back(BBI->getOperand(i).getReg());
650650 }
651651
652 void LiveVariables::addNewBlock(MachineBasicBlock *A, MachineBasicBlock *B) {
653 unsigned NumA = A->getNumber();
654 unsigned NumB = B->getNumber();
652 /// addNewBlock - Add a new basic block BB as an empty succcessor to DomBB. All
653 /// variables that are live out of DomBB will be marked as passing live through
654 /// BB.
655 void LiveVariables::addNewBlock(MachineBasicBlock *BB,
656 MachineBasicBlock *DomBB) {
657 const unsigned NumNew = BB->getNumber();
658 const unsigned NumDom = DomBB->getNumber();
655659
656660 // 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);
661 for (unsigned Reg = TargetRegisterInfo::FirstVirtualRegister,
662 E = MRI->getLastVirtReg()+1; Reg != E; ++Reg) {
663 VarInfo &VI = getVarInfo(Reg);
664
665 // Anything live through DomBB is also live through BB.
666 if (VI.AliveBlocks.test(NumDom)) {
667 VI.AliveBlocks.set(NumNew);
663668 continue;
664669 }
665670
666 // If we're not killed in B, we are not live in
667 if (!VI.findKill(B))
671 // Variables not defined in DomBB cannot be live out.
672 const MachineInstr *Def = MRI->getVRegDef(Reg);
673 if (!Def || Def->getParent() != DomBB)
668674 continue;
669675
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 }
676 // Killed by DomBB?
677 if (VI.findKill(DomBB))
678 continue;
679
680 // This register is defined in DomBB and live out
681 VI.AliveBlocks.set(NumNew);
682 }
683 }
2222 #include "llvm/CodeGen/MachineInstr.h"
2323 #include "llvm/CodeGen/MachineInstrBuilder.h"
2424 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/Function.h"
2526 #include "llvm/Target/TargetMachine.h"
2627 #include "llvm/ADT/SmallPtrSet.h"
2728 #include "llvm/ADT/STLExtras.h"
6465
6566 PHIDefs.clear();
6667 PHIKills.clear();
68
69 bool Changed = false;
70
71 // Split critical edges to help the coalescer
72 if (SplitEdges)
73 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
74 Changed |= SplitPHIEdges(Fn, *I);
75
76 // Populate VRegPHIUseCount
6777 analyzePHINodes(Fn);
68
69 bool Changed = false;
7078
7179 // Eliminate PHI instructions by inserting copies into predecessor blocks.
7280 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
8694 return Changed;
8795 }
8896
89
9097 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
9198 /// predecessor basic blocks.
9299 ///
94101 MachineBasicBlock &MBB) {
95102 if (MBB.empty() || MBB.front().getOpcode() != TargetInstrInfo::PHI)
96103 return false; // Quick exit for basic blocks without PHIs.
97
98 if (SplitEdges)
99 SplitPHIEdges(MF, MBB);
100104
101105 // Get an iterator to the first instruction after the last PHI node (this may
102106 // also be the end of the basic block).
292296 // Okay, if we now know that the value is not live out of the block, we can
293297 // add a kill marker in this block saying that it kills the incoming value!
294298 // When SplitEdges is enabled, the value is never live out.
295 if (!ValueIsUsed && (SplitEdges || !isLiveOut(SrcReg, opBlock, *LV))) {
299 if (!ValueIsUsed && !isLiveOut(SrcReg, opBlock, *LV)) {
296300 // In our final twist, we have to decide which instruction kills the
297301 // register. In most cases this is the copy, however, the first
298302 // terminator instruction at the end of the block may also use the value.
344348 BBI->getOperand(i).getReg())];
345349 }
346350
347 void llvm::PHIElimination::SplitPHIEdges(MachineFunction &MF,
351 bool llvm::PHIElimination::SplitPHIEdges(MachineFunction &MF,
348352 MachineBasicBlock &MBB) {
353 if (MBB.empty() || MBB.front().getOpcode() != TargetInstrInfo::PHI)
354 return false; // Quick exit for basic blocks without PHIs.
349355 LiveVariables &LV = getAnalysis();
350356 for (MachineBasicBlock::const_iterator BBI = MBB.begin(), BBE = MBB.end();
351357 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI) {
353359 unsigned Reg = BBI->getOperand(i).getReg();
354360 MachineBasicBlock *PreMBB = BBI->getOperand(i+1).getMBB();
355361 // We break edges when registers are live out from the predecessor block
356 // (not considering PHI nodes).
357 if (isLiveOut(Reg, *PreMBB, LV))
362 // (not considering PHI nodes). If the register is live in to this block
363 // anyway, we would gain nothing from splitting.
364 if (isLiveOut(Reg, *PreMBB, LV) && !isLiveIn(Reg, MBB, LV))
358365 SplitCriticalEdge(PreMBB, &MBB);
359366 }
360367 }
368 return true;
361369 }
362370
363371 bool llvm::PHIElimination::isLiveOut(unsigned Reg, const MachineBasicBlock &MBB,
364372 LiveVariables &LV) {
365 LiveVariables::VarInfo &InRegVI = LV.getVarInfo(Reg);
373 LiveVariables::VarInfo &VI = LV.getVarInfo(Reg);
366374
367375 // Loop over all of the successors of the basic block, checking to see if
368376 // the value is either live in the block, or if it is killed in the block.
369377 std::vector OpSuccBlocks;
370
371 // Otherwise, scan successors, including the BB the PHI node lives in.
372378 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
373379 E = MBB.succ_end(); SI != E; ++SI) {
374380 MachineBasicBlock *SuccMBB = *SI;
375381
376382 // Is it alive in this successor?
377383 unsigned SuccIdx = SuccMBB->getNumber();
378 if (InRegVI.AliveBlocks.test(SuccIdx))
384 if (VI.AliveBlocks.test(SuccIdx))
379385 return true;
380386 OpSuccBlocks.push_back(SuccMBB);
381387 }
385391 switch (OpSuccBlocks.size()) {
386392 case 1: {
387393 MachineBasicBlock *SuccMBB = OpSuccBlocks[0];
388 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
389 if (InRegVI.Kills[i]->getParent() == SuccMBB)
394 for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
395 if (VI.Kills[i]->getParent() == SuccMBB)
390396 return true;
391397 break;
392398 }
393399 case 2: {
394400 MachineBasicBlock *SuccMBB1 = OpSuccBlocks[0], *SuccMBB2 = OpSuccBlocks[1];
395 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
396 if (InRegVI.Kills[i]->getParent() == SuccMBB1 ||
397 InRegVI.Kills[i]->getParent() == SuccMBB2)
401 for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
402 if (VI.Kills[i]->getParent() == SuccMBB1 ||
403 VI.Kills[i]->getParent() == SuccMBB2)
398404 return true;
399405 break;
400406 }
401407 default:
402408 std::sort(OpSuccBlocks.begin(), OpSuccBlocks.end());
403 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
409 for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
404410 if (std::binary_search(OpSuccBlocks.begin(), OpSuccBlocks.end(),
405 InRegVI.Kills[i]->getParent()))
411 VI.Kills[i]->getParent()))
406412 return true;
407413 }
408414 return false;
415 }
416
417 bool llvm::PHIElimination::isLiveIn(unsigned Reg, const MachineBasicBlock &MBB,
418 LiveVariables &LV) {
419 LiveVariables::VarInfo &VI = LV.getVarInfo(Reg);
420
421 return VI.AliveBlocks.test(MBB.getNumber()) || VI.findKill(&MBB);
409422 }
410423
411424 MachineBasicBlock *PHIElimination::SplitCriticalEdge(MachineBasicBlock *A,
413426 assert(A && B && "Missing MBB end point");
414427 ++NumSplits;
415428
429 BasicBlock *ABB = const_cast(A->getBasicBlock());
430 BasicBlock *BBB = const_cast(B->getBasicBlock());
431 assert(ABB && BBB && "End points must have a basic block");
432 BasicBlock *BB = BasicBlock::Create(BBB->getContext(),
433 ABB->getName() + "." + BBB->getName() +
434 "_phi_edge");
435 Function *F = ABB->getParent();
436 F->getBasicBlockList().insert(F->end(), BB);
437
438 BranchInst::Create(BBB, BB);
439 // We could do more here to produce correct IR, compare
440 // llvm::SplitCriticalEdge
441
416442 MachineFunction *MF = A->getParent();
417 MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(B->getBasicBlock());
443 MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(BB);
418444 MF->push_back(NMBB);
419445 const unsigned NewNum = NMBB->getNumber();
420446 DEBUG(errs() << "PHIElimination splitting critical edge:"
429455 SmallVector Cond;
430456 MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, B, NULL, Cond);
431457
432 LiveVariables *LV = getAnalysisIfAvailable();
433 if (LV)
434 LV->addNewBlock(NMBB, B);
458 if (LiveVariables *LV = getAnalysisIfAvailable())
459 LV->addNewBlock(NMBB, A);
435460
436461 // Fix PHI nodes in B so they refer to NMBB instead of A
437462 for (MachineBasicBlock::iterator i = B->begin(), e = B->end();
438463 i != e && i->getOpcode() == TargetInstrInfo::PHI; ++i)
439464 for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2)
440 if (i->getOperand(ni+1).getMBB() == A) {
465 if (i->getOperand(ni+1).getMBB() == A)
441466 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 }
446467 return NMBB;
447468 }
448
449
8989 void analyzePHINodes(const MachineFunction& Fn);
9090
9191 /// Split critical edges where necessary for good coalescer performance.
92 void SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB);
92 bool SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB);
9393
9494 /// isLiveOut - Determine if Reg is live out from MBB, when not
9595 /// considering PHI nodes. This means that Reg is either killed by
9696 /// a successor block or passed through one.
9797 bool isLiveOut(unsigned Reg, const MachineBasicBlock &MBB,
9898 LiveVariables &LV);
99
100 /// isLiveIn - Determine if Reg is live in to MBB, not considering PHI
101 /// source registers. This means that Reg is either killed by MBB or passes
102 /// through it.
103 bool isLiveIn(unsigned Reg, const MachineBasicBlock &MBB,
104 LiveVariables &LV);
99105
100106 /// SplitCriticalEdge - Split a critical edge from A to B by
101107 /// inserting a new MBB. Update branches in A and PHI instructions