本文整理汇总了C++中TerminatorInst::getNumSuccessors方法的典型用法代码示例。如果您正苦于以下问题:C++ TerminatorInst::getNumSuccessors方法的具体用法?C++ TerminatorInst::getNumSuccessors怎么用?C++ TerminatorInst::getNumSuccessors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TerminatorInst
的用法示例。
在下文中一共展示了TerminatorInst::getNumSuccessors方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: calcMetadataWeights
// Propagate existing explicit probabilities from either profile data or
// 'expect' intrinsic processing.
bool BranchProbabilityAnalysis::calcMetadataWeights(BasicBlock *BB) {
TerminatorInst *TI = BB->getTerminator();
if (TI->getNumSuccessors() == 1)
return false;
if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
return false;
MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
if (!WeightsNode)
return false;
// Ensure there are weights for all of the successors. Note that the first
// operand to the metadata node is a name, not a weight.
if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
return false;
// Build up the final weights that will be used in a temporary buffer, but
// don't add them until all weihts are present. Each weight value is clamped
// to [1, getMaxWeightFor(BB)].
uint32_t WeightLimit = getMaxWeightFor(BB);
SmallVector<uint32_t, 2> Weights;
Weights.reserve(TI->getNumSuccessors());
for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) {
ConstantInt *Weight = dyn_cast<ConstantInt>(WeightsNode->getOperand(i));
if (!Weight)
return false;
Weights.push_back(
std::max<uint32_t>(1, Weight->getLimitedValue(WeightLimit)));
}
assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
BP->setEdgeWeight(BB, TI->getSuccessor(i), Weights[i]);
return true;
}
示例2: emitAnnotations
/// \brief Generate branch weight metadata for all branches in \p F.
///
/// For every branch instruction B in \p F, we compute the weight of the
/// target block for each of the edges out of B. This is the weight
/// that we associate with that branch.
///
/// TODO - This weight assignment will most likely be wrong if the
/// target branch has more than two predecessors. This needs to be done
/// using some form of flow propagation.
///
/// Once all the branch weights are computed, we emit the MD_prof
/// metadata on B using the computed values.
///
/// \param F The function to query.
bool SampleProfile::emitAnnotations(Function &F) {
bool Changed = false;
FunctionProfile &FProfile = Profiles[F.getName()];
unsigned FirstLineno = inst_begin(F)->getDebugLoc().getLine();
MDBuilder MDB(F.getContext());
// Clear the block weights cache.
FProfile.BlockWeights.clear();
// When we find a branch instruction: For each edge E out of the branch,
// the weight of E is the weight of the target block.
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
BasicBlock *B = I;
TerminatorInst *TI = B->getTerminator();
if (TI->getNumSuccessors() == 1)
continue;
if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
continue;
SmallVector<uint32_t, 4> Weights;
unsigned NSuccs = TI->getNumSuccessors();
for (unsigned I = 0; I < NSuccs; ++I) {
BasicBlock *Succ = TI->getSuccessor(I);
uint32_t Weight =
computeBlockWeight(Succ, FirstLineno, FProfile.BodySamples);
Weights.push_back(Weight);
}
TI->setMetadata(llvm::LLVMContext::MD_prof,
MDB.createBranchWeights(Weights));
Changed = true;
}
return Changed;
}
示例3: calcUnreachableHeuristics
/// \brief Calculate edge weights for successors lead to unreachable.
///
/// Predict that a successor which leads necessarily to an
/// unreachable-terminated block as extremely unlikely.
bool BranchProbabilityInfo::calcUnreachableHeuristics(BasicBlock *BB) {
TerminatorInst *TI = BB->getTerminator();
if (TI->getNumSuccessors() == 0) {
if (isa<UnreachableInst>(TI))
PostDominatedByUnreachable.insert(BB);
return false;
}
SmallVector<unsigned, 4> UnreachableEdges;
SmallVector<unsigned, 4> ReachableEdges;
for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
if (PostDominatedByUnreachable.count(*I))
UnreachableEdges.push_back(I.getSuccessorIndex());
else
ReachableEdges.push_back(I.getSuccessorIndex());
}
// If all successors are in the set of blocks post-dominated by unreachable,
// this block is too.
if (UnreachableEdges.size() == TI->getNumSuccessors())
PostDominatedByUnreachable.insert(BB);
// Skip probabilities if this block has a single successor or if all were
// reachable.
if (TI->getNumSuccessors() == 1 || UnreachableEdges.empty())
return false;
// If the terminator is an InvokeInst, check only the normal destination block
// as the unwind edge of InvokeInst is also very unlikely taken.
if (auto *II = dyn_cast<InvokeInst>(TI))
if (PostDominatedByUnreachable.count(II->getNormalDest())) {
PostDominatedByUnreachable.insert(BB);
// Return false here so that edge weights for InvokeInst could be decided
// in calcInvokeHeuristics().
return false;
}
if (ReachableEdges.empty()) {
BranchProbability Prob(1, UnreachableEdges.size());
for (unsigned SuccIdx : UnreachableEdges)
setEdgeProbability(BB, SuccIdx, Prob);
return true;
}
BranchProbability UnreachableProb(UR_TAKEN_WEIGHT,
(UR_TAKEN_WEIGHT + UR_NONTAKEN_WEIGHT) *
UnreachableEdges.size());
BranchProbability ReachableProb(UR_NONTAKEN_WEIGHT,
(UR_TAKEN_WEIGHT + UR_NONTAKEN_WEIGHT) *
ReachableEdges.size());
for (unsigned SuccIdx : UnreachableEdges)
setEdgeProbability(BB, SuccIdx, UnreachableProb);
for (unsigned SuccIdx : ReachableEdges)
setEdgeProbability(BB, SuccIdx, ReachableProb);
return true;
}
示例4: insertCycleChecks
void CheckInserter::insertCycleChecks(Function &F) {
IdentifyBackEdges &IBE = getAnalysis<IdentifyBackEdges>();
for (Function::iterator B1 = F.begin(); B1 != F.end(); ++B1) {
TerminatorInst *TI = B1->getTerminator();
for (unsigned j = 0; j < TI->getNumSuccessors(); ++j) {
BasicBlock *B2 = TI->getSuccessor(j);
unsigned BackEdgeID = IBE.getID(B1, B2);
if (BackEdgeID != (unsigned)-1) {
assert(BackEdgeID < MaxNumBackEdges);
BasicBlock *BackEdgeBlock = BasicBlock::Create(
F.getContext(),
"backedge_" + B1->getName() + "_" + B2->getName(),
&F);
CallInst::Create(CycleCheck,
ConstantInt::get(IntType, BackEdgeID),
"",
BackEdgeBlock);
// BackEdgeBlock -> B2
// Fix the PHINodes in B2.
BranchInst::Create(B2, BackEdgeBlock);
for (BasicBlock::iterator I = B2->begin();
B2->getFirstNonPHI() != I;
++I) {
PHINode *PHI = cast<PHINode>(I);
// Note: If B2 has multiple incoming edges from B1 (e.g. B1 terminates
// with a SelectInst), its PHINodes must also have multiple incoming
// edges from B1. However, after adding BackEdgeBlock and essentially
// merging the multiple incoming edges from B1, there will be only one
// edge from BackEdgeBlock to B2. Therefore, we need to remove the
// redundant incoming edges from B2's PHINodes.
bool FirstIncomingFromB1 = true;
for (unsigned k = 0; k < PHI->getNumIncomingValues(); ++k) {
if (PHI->getIncomingBlock(k) == B1) {
if (FirstIncomingFromB1) {
FirstIncomingFromB1 = false;
PHI->setIncomingBlock(k, BackEdgeBlock);
} else {
PHI->removeIncomingValue(k, false);
--k;
}
}
}
}
// B1 -> BackEdgeBlock
// There might be multiple back edges from B1 to B2. Need to replace
// them all.
for (unsigned j2 = j; j2 < TI->getNumSuccessors(); ++j2) {
if (TI->getSuccessor(j2) == B2) {
TI->setSuccessor(j2, BackEdgeBlock);
}
}
}
}
}
}
示例5: calcColdCallHeuristics
/// \brief Calculate edge weights for edges leading to cold blocks.
///
/// A cold block is one post-dominated by a block with a call to a
/// cold function. Those edges are unlikely to be taken, so we give
/// them relatively low weight.
///
/// Return true if we could compute the weights for cold edges.
/// Return false, otherwise.
bool BranchProbabilityInfo::calcColdCallHeuristics(BasicBlock *BB) {
TerminatorInst *TI = BB->getTerminator();
if (TI->getNumSuccessors() == 0)
return false;
// Determine which successors are post-dominated by a cold block.
SmallVector<unsigned, 4> ColdEdges;
SmallVector<unsigned, 4> NormalEdges;
for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
if (PostDominatedByColdCall.count(*I))
ColdEdges.push_back(I.getSuccessorIndex());
else
NormalEdges.push_back(I.getSuccessorIndex());
// If all successors are in the set of blocks post-dominated by cold calls,
// this block is in the set post-dominated by cold calls.
if (ColdEdges.size() == TI->getNumSuccessors())
PostDominatedByColdCall.insert(BB);
else {
// Otherwise, if the block itself contains a cold function, add it to the
// set of blocks postdominated by a cold call.
assert(!PostDominatedByColdCall.count(BB));
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
if (CallInst *CI = dyn_cast<CallInst>(I))
if (CI->hasFnAttr(Attribute::Cold)) {
PostDominatedByColdCall.insert(BB);
break;
}
}
// Skip probabilities if this block has a single successor.
if (TI->getNumSuccessors() == 1 || ColdEdges.empty())
return false;
if (NormalEdges.empty()) {
BranchProbability Prob(1, ColdEdges.size());
for (unsigned SuccIdx : ColdEdges)
setEdgeProbability(BB, SuccIdx, Prob);
return true;
}
BranchProbability ColdProb(CC_TAKEN_WEIGHT,
(CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) *
ColdEdges.size());
BranchProbability NormalProb(CC_NONTAKEN_WEIGHT,
(CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) *
NormalEdges.size());
for (unsigned SuccIdx : ColdEdges)
setEdgeProbability(BB, SuccIdx, ColdProb);
for (unsigned SuccIdx : NormalEdges)
setEdgeProbability(BB, SuccIdx, NormalProb);
return true;
}
示例6:
unsigned
llvm::SplitAllCriticalEdges(Function &F,
const CriticalEdgeSplittingOptions &Options) {
unsigned NumBroken = 0;
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
TerminatorInst *TI = I->getTerminator();
if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
if (SplitCriticalEdge(TI, i, Options))
++NumBroken;
}
return NumBroken;
}
示例7:
unsigned
llvm::SplitAllCriticalEdges(Function &F,
const CriticalEdgeSplittingOptions &Options) {
unsigned NumBroken = 0;
for (BasicBlock &BB : F) {
TerminatorInst *TI = BB.getTerminator();
if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
if (SplitCriticalEdge(TI, i, Options))
++NumBroken;
}
return NumBroken;
}
示例8: runOnFunction
// runOnFunction - Loop over all of the edges in the CFG, breaking critical
// edges as they are found.
//
bool BreakCriticalEdges::runOnFunction(Function &F) {
bool Changed = false;
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
TerminatorInst *TI = I->getTerminator();
if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
if (SplitCriticalEdge(TI, i, this)) {
++NumBroken;
Changed = true;
}
}
return Changed;
}
示例9: isRefAfterCallSite
/*
* Verify if a given value has references after a call site.
*/
bool DeadStoreEliminationPass::isRefAfterCallSite(Value* v, CallSite &CS) {
BasicBlock* CSBB = CS.getInstruction()->getParent();
// Collect basic blocks to inspect
std::vector<BasicBlock*> BBToInspect;
std::set<BasicBlock*> BBToInspectSet;
BBToInspect.push_back(CSBB);
BBToInspectSet.insert(CSBB);
for (unsigned int i = 0; i < BBToInspect.size(); ++i) {
BasicBlock* BB = BBToInspect.at(i);
TerminatorInst* terminator = BB->getTerminator();
if (terminator && terminator->getNumSuccessors() > 0) {
unsigned numSuccessors = terminator->getNumSuccessors();
for (unsigned i = 0; i < numSuccessors; ++i) {
// Collect successors
BasicBlock* successor = terminator->getSuccessor(i);
if (!BBToInspectSet.count(successor)) {
BBToInspect.push_back(successor);
BBToInspectSet.insert(successor);
}
}
}
}
// Inspect if any instruction after CS references v
AliasAnalysis::Location loc(v, getPointerSize(v, *AA), NULL);
for (unsigned int i = 0; i < BBToInspect.size(); ++i) {
BasicBlock* BB = BBToInspect.at(i);
BasicBlock::iterator I = BB->begin();
if (BB == CSBB) {
Instruction* callInst = CS.getInstruction();
Instruction* inst;
do {
inst = I;
++I;
} while (inst != callInst);
}
for (BasicBlock::iterator IE = BB->end(); I != IE; ++I) {
Instruction* inst = I;
DEBUG(errs() << "Verifying if instruction " << *inst << " refs " << *v << ": ");
AliasAnalysis::ModRefResult mrf = AA->getModRefInfo(inst, loc);
DEBUG(errs() << mrf << "\n");
if (mrf == AliasAnalysis::Ref || mrf == AliasAnalysis::ModRef) {
return true;
}
}
}
return false;
}
示例10: setBranchWeights
// Assign the scaled count values to the BB with multiple out edges.
void PGOUseFunc::setBranchWeights() {
// Generate MD_prof metadata for every branch instruction.
DEBUG(dbgs() << "\nSetting branch weights.\n");
for (auto &BB : F) {
TerminatorInst *TI = BB.getTerminator();
if (TI->getNumSuccessors() < 2)
continue;
if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
continue;
if (getBBInfo(&BB).CountValue == 0)
continue;
// We have a non-zero Branch BB.
const UseBBInfo &BBCountInfo = getBBInfo(&BB);
unsigned Size = BBCountInfo.OutEdges.size();
SmallVector<uint64_t, 2> EdgeCounts(Size, 0);
uint64_t MaxCount = 0;
for (unsigned s = 0; s < Size; s++) {
const PGOUseEdge *E = BBCountInfo.OutEdges[s];
const BasicBlock *SrcBB = E->SrcBB;
const BasicBlock *DestBB = E->DestBB;
if (DestBB == nullptr)
continue;
unsigned SuccNum = GetSuccessorNumber(SrcBB, DestBB);
uint64_t EdgeCount = E->CountValue;
if (EdgeCount > MaxCount)
MaxCount = EdgeCount;
EdgeCounts[SuccNum] = EdgeCount;
}
setProfMetadata(M, TI, EdgeCounts, MaxCount);
}
}
示例11: partitionOuterLoopBlocks
// Partition blocks in an outer/inner loop pair into blocks before and after
// the loop
static bool partitionOuterLoopBlocks(Loop *L, Loop *SubLoop,
BasicBlockSet &ForeBlocks,
BasicBlockSet &SubLoopBlocks,
BasicBlockSet &AftBlocks,
DominatorTree *DT) {
BasicBlock *SubLoopLatch = SubLoop->getLoopLatch();
SubLoopBlocks.insert(SubLoop->block_begin(), SubLoop->block_end());
for (BasicBlock *BB : L->blocks()) {
if (!SubLoop->contains(BB)) {
if (DT->dominates(SubLoopLatch, BB))
AftBlocks.insert(BB);
else
ForeBlocks.insert(BB);
}
}
// Check that all blocks in ForeBlocks together dominate the subloop
// TODO: This might ideally be done better with a dominator/postdominators.
BasicBlock *SubLoopPreHeader = SubLoop->getLoopPreheader();
for (BasicBlock *BB : ForeBlocks) {
if (BB == SubLoopPreHeader)
continue;
TerminatorInst *TI = BB->getTerminator();
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
if (!ForeBlocks.count(TI->getSuccessor(i)))
return false;
}
return true;
}
示例12: DeleteDeadBlock
/// DeleteDeadBlock - Delete the specified block, which must have no
/// predecessors.
void llvm::DeleteDeadBlock(BasicBlock *BB) {
assert((pred_begin(BB) == pred_end(BB) ||
// Can delete self loop.
BB->getSinglePredecessor() == BB) && "Block is not dead!");
TerminatorInst *BBTerm = BB->getTerminator();
// Loop through all of our successors and make sure they know that one
// of their predecessors is going away.
for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i)
BBTerm->getSuccessor(i)->removePredecessor(BB);
// Zap all the instructions in the block.
while (!BB->empty()) {
Instruction &I = BB->back();
// If this instruction is used, replace uses with an arbitrary value.
// Because control flow can't get here, we don't care what we replace the
// value with. Note that since this block is unreachable, and all values
// contained within it must dominate their uses, that all uses will
// eventually be removed (they are themselves dead).
if (!I.use_empty())
I.replaceAllUsesWith(UndefValue::get(I.getType()));
BB->getInstList().pop_back();
}
// Zap the block!
BB->eraseFromParent();
}
示例13: CleanupBasicBlock
void CleanupBasicBlock(BasicBlock *BB){
std::vector<llvm::BasicBlock *>removedSuccessorsOfMore;
if(BB->getParent()){
TerminatorInst *BBTerm = BB->getTerminator();
// Loop through all of our successors and make sure they know that one
// of their predecessors is going away.
for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i){
BBTerm->getSuccessor(i)->removePredecessor(BB);
}
// Zap all the instructions in the block.
while (!BB->empty()) {
Instruction &I = BB->back();
// If this instruction is used, replace uses with an arbitrary value.
// Because control flow can't get here, we don't care what we replace the
// value with. Note that since this block is unreachable, and all values
// contained within it must dominate their uses, that all uses will
// eventually be removed (they are themselves dead).
if (!I.use_empty())
I.replaceAllUsesWith(UndefValue::get(I.getType()));
BB->getInstList().pop_back();
I.dropAllReferences();
}
BB->dropAllReferences();
BB->eraseFromParent();
}
}
示例14: changeBlockTarget
//------------------------------------------------------------------------------
void changeBlockTarget(BasicBlock *block, BasicBlock *newTarget,
unsigned int branchIndex) {
TerminatorInst *terminator = block->getTerminator();
assert(terminator->getNumSuccessors() &&
"The target can be change only if it is unique");
terminator->setSuccessor(branchIndex, newTarget);
}
示例15: if
MDNode *Loop::getLoopID() const {
MDNode *LoopID = nullptr;
if (isLoopSimplifyForm()) {
LoopID = getLoopLatch()->getTerminator()->getMetadata(LoopMDName);
} else {
// Go through each predecessor of the loop header and check the
// terminator for the metadata.
BasicBlock *H = getHeader();
for (block_iterator I = block_begin(), IE = block_end(); I != IE; ++I) {
TerminatorInst *TI = (*I)->getTerminator();
MDNode *MD = nullptr;
// Check if this terminator branches to the loop header.
for (unsigned i = 0, ie = TI->getNumSuccessors(); i != ie; ++i) {
if (TI->getSuccessor(i) == H) {
MD = TI->getMetadata(LoopMDName);
break;
}
}
if (!MD)
return nullptr;
if (!LoopID)
LoopID = MD;
else if (MD != LoopID)
return nullptr;
}
}
if (!LoopID || LoopID->getNumOperands() == 0 ||
LoopID->getOperand(0) != LoopID)
return nullptr;
return LoopID;
}