- return true;
-}
-
-bool llvm::PHIElimination::isLiveOut(unsigned Reg, const MachineBasicBlock &MBB,
- LiveVariables &LV) {
- LiveVariables::VarInfo &VI = LV.getVarInfo(Reg);
-
- // Loop over all of the successors of the basic block, checking to see if
- // the value is either live in the block, or if it is killed in the block.
- std::vector<MachineBasicBlock*> OpSuccBlocks;
- for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
- E = MBB.succ_end(); SI != E; ++SI) {
- MachineBasicBlock *SuccMBB = *SI;
-
- // Is it alive in this successor?
- unsigned SuccIdx = SuccMBB->getNumber();
- if (VI.AliveBlocks.test(SuccIdx))
- return true;
- OpSuccBlocks.push_back(SuccMBB);
- }
-
- // Check to see if this value is live because there is a use in a successor
- // that kills it.
- switch (OpSuccBlocks.size()) {
- case 1: {
- MachineBasicBlock *SuccMBB = OpSuccBlocks[0];
- for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
- if (VI.Kills[i]->getParent() == SuccMBB)
- return true;
- break;
- }
- case 2: {
- MachineBasicBlock *SuccMBB1 = OpSuccBlocks[0], *SuccMBB2 = OpSuccBlocks[1];
- for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
- if (VI.Kills[i]->getParent() == SuccMBB1 ||
- VI.Kills[i]->getParent() == SuccMBB2)
- return true;
- break;
- }
- default:
- std::sort(OpSuccBlocks.begin(), OpSuccBlocks.end());
- for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
- if (std::binary_search(OpSuccBlocks.begin(), OpSuccBlocks.end(),
- VI.Kills[i]->getParent()))
- return true;
- }
- return false;
-}
-
-bool llvm::PHIElimination::isLiveIn(unsigned Reg, const MachineBasicBlock &MBB,
- LiveVariables &LV) {
- LiveVariables::VarInfo &VI = LV.getVarInfo(Reg);
-
- return VI.AliveBlocks.test(MBB.getNumber()) || VI.findKill(&MBB);
-}
-
-MachineBasicBlock *PHIElimination::SplitCriticalEdge(MachineBasicBlock *A,
- MachineBasicBlock *B) {
- assert(A && B && "Missing MBB end point");
- ++NumSplits;
-
- BasicBlock *ABB = const_cast<BasicBlock*>(A->getBasicBlock());
- BasicBlock *BBB = const_cast<BasicBlock*>(B->getBasicBlock());
- assert(ABB && BBB && "End points must have a basic block");
- BasicBlock *BB = BasicBlock::Create(BBB->getContext(),
- ABB->getName() + "." + BBB->getName() +
- "_phi_edge");
- Function *F = ABB->getParent();
- F->getBasicBlockList().insert(F->end(), BB);
-
- BranchInst::Create(BBB, BB);
- // We could do more here to produce correct IR, compare
- // llvm::SplitCriticalEdge
-
- MachineFunction *MF = A->getParent();
- MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(BB);
- MF->push_back(NMBB);
- DEBUG(errs() << "PHIElimination splitting critical edge:"
- " BB#" << A->getNumber()
- << " -- BB#" << NMBB->getNumber()
- << " -- BB#" << B->getNumber() << '\n');
-
- A->ReplaceUsesOfBlockWith(B, NMBB);
- NMBB->addSuccessor(B);
-
- // Insert unconditional "jump B" instruction in NMBB.
- SmallVector<MachineOperand, 4> Cond;
- MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, B, NULL, Cond);
-
- if (LiveVariables *LV = getAnalysisIfAvailable<LiveVariables>())
- LV->addNewBlock(NMBB, A);
-
- // Fix PHI nodes in B so they refer to NMBB instead of A
- for (MachineBasicBlock::iterator i = B->begin(), e = B->end();
- i != e && i->getOpcode() == TargetInstrInfo::PHI; ++i)
- for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2)
- if (i->getOperand(ni+1).getMBB() == A)
- i->getOperand(ni+1).setMBB(NMBB);
- return NMBB;