本文整理汇总了C++中BranchInst::isConditional方法的典型用法代码示例。如果您正苦于以下问题:C++ BranchInst::isConditional方法的具体用法?C++ BranchInst::isConditional怎么用?C++ BranchInst::isConditional使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BranchInst
的用法示例。
在下文中一共展示了BranchInst::isConditional方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: findLastCond
/////////////////////////////////////
//findLastSyn() //
//Find the last instruction in BB. Any instructions before this one must be replicated except Synchpoint
/////////////////////////////////////
Instruction* InsDuplica::findLastCond (BasicBlock *BB) {
TerminatorInst *lastIns = BB->getTerminator();
assert(!(isa<SwitchInst>(lastIns)) && "Find a SwitchInst! You need to lower SwitchInst.");
BranchInst * BI = dyn_cast<BranchInst>(lastIns);
if (BI && (BI->isConditional())) {
Value *cond = BI->getCondition();
Instruction* condIns = dyn_cast<Instruction>(cond);
//find condIns. But we have to make sure condIns is the second to the last instruction in BB
assert(condIns && "Branch Condition must not be trivial");
if ((condIns->getNextNode())!=lastIns) {
//assert((condIns->getParent() == BB) && "condIns is not in the same BB as br!");
//if condIns is not in the same BB as br. We have to leave it there
if (condIns->getParent() != BB) return lastIns;
//if condIns is a PHINode, since we can not move, we just return BI
//if condIns has more than one use, better not to reorder
if ((isa<PHINode>(condIns))||!(condIns->hasOneUse())) return lastIns;
condIns->moveBefore(lastIns); //we moved condInst right before br
#ifdef Jing_DEBUG
std::cerr << "adjust order of condIns "<< condIns->getName() <<" in " << BB->getName() <<"\n";
#endif
}
#ifdef Jing_DEBUG
std::cerr << "findLastCond returns " << condIns->getName() <<"\n";
#endif
return condIns;
}
//return the terminator instruction
return lastIns;
}
示例2: branchesOn
// Look for control dependencies on a read.
bool branchesOn(BasicBlock *bb, Value *load,
ICmpInst **icmpOut, int *outIdx) {
// XXX: make this platform configured; on some platforms maybe an
// atomic cmpxchg does /not/ behave like it branches on the old value
if (isa<AtomicCmpXchgInst>(load) || isa<AtomicRMWInst>(load)) {
if (icmpOut) *icmpOut = nullptr;
if (outIdx) *outIdx = 0;
return true;
}
// TODO: we should be able to follow values through phi nodes,
// since we are path dependent anyways.
BranchInst *br = dyn_cast<BranchInst>(bb->getTerminator());
if (!br || !br->isConditional()) return false;
// TODO: We only check one level of things. Check deeper?
// We pretty heavily restrict what operations we handle here.
// Some would just be wrong (like call), but really icmp is
// the main one, so. Probably we should be able to also
// pick through casts and wideness changes.
ICmpInst *icmp = dyn_cast<ICmpInst>(br->getCondition());
if (!icmp) return false;
int idx = 0;
for (auto v : icmp->operand_values()) {
if (getRealValue(v) == load) {
if (icmpOut) *icmpOut = icmp;
if (outIdx) *outIdx = idx;
return true;
}
++idx;
}
return false;
}
示例3: EliminateFallThrough
/// EliminateFallThrough - Merge basic blocks which are connected
/// by a single edge, where one of the basic blocks has a single successor
/// pointing to the other basic block, which has a single predecessor.
bool CodeGenPrepare::EliminateFallThrough(Function &F) {
bool Changed = false;
// Scan all of the blocks in the function, except for the entry block.
for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) {
BasicBlock *BB = I++;
// If the destination block has a single pred, then this is a trivial
// edge, just collapse it.
BasicBlock *SinglePred = BB->getSinglePredecessor();
// Don't merge if BB's address is taken.
if (!SinglePred || SinglePred == BB || BB->hasAddressTaken()) continue;
BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator());
if (Term && !Term->isConditional()) {
Changed = true;
DEBUG(dbgs() << "To merge:\n"<< *SinglePred << "\n\n\n");
// Remember if SinglePred was the entry block of the function.
// If so, we will need to move BB back to the entry position.
bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
MergeBasicBlockIntoOnlyPred(BB, this);
if (isEntry && BB != &BB->getParent()->getEntryBlock())
BB->moveBefore(&BB->getParent()->getEntryBlock());
// We have erased a block. Update the iterator.
I = BB;
}
}
return Changed;
}
示例4: makeTable
//It receives a BasicBLock and makes table of predicates and its respective gated instructions
void bSSA::makeTable (BasicBlock *BB, Function *F) {
Value *condition;
TerminatorInst *ti = BB->getTerminator();
BranchInst *bi = NULL;
SwitchInst *si=NULL;
PostDominatorTree &PD = getAnalysis<PostDominatorTree>(*F);
ProcessedBB.clear();
if ((bi = dyn_cast<BranchInst>(ti)) && bi->isConditional()) { //If the terminator instruction is a conditional branch
condition = bi->getCondition();
//Including the predicate on the predicatesVector
predicatesVector.push_back(new Pred(condition));
//Make a "Flooding" on each sucessor gated the instruction on Influence Region of the predicate
for (unsigned int i=0; i<bi->getNumSuccessors(); i++) {
findIR (BB, bi->getSuccessor(i),PD);
}
}else if ((si = dyn_cast<SwitchInst>(ti))) {
condition = si->getCondition();
//Including the predicate on the predicatesVector
predicatesVector.push_back(new Pred(condition));
//Make a "Flooding" on each sucessor gated the instruction on Influence Region of the predicate
for (unsigned int i=0; i<si->getNumSuccessors(); i++) {
findIR (BB, si->getSuccessor(i),PD);
}
}
}
示例5: ConvertCmp
void MakeDispatcherPass::ConvertCmp(Function& function)
{
typedef std::vector< Instruction * > InstList;
InstList insts;
for (Function::iterator BB = function.begin(), bbE = function.end(); BB != bbE; ++BB)
{
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;)
{
if (isa< CmpInst >(I))
{
insts.push_back(I);
}
if (isa< BranchInst >(I))
{
BasicBlock::iterator save = I;
BranchInst* branchInst = dynamic_cast< BranchInst *>(&*I);
if (branchInst->isConditional() && !insts.empty())
{
Value* valbranch = NULL;
valbranch = branchInst->getCondition();
ShowType(dynamic_cast<CmpInst*>(insts[0]));
CreateInt3(BB, I);
I++;
save->eraseFromParent();
insts.pop_back();
continue;
}
}
I++;
}
}
}
示例6: calcZeroHeuristics
bool BranchProbabilityAnalysis::calcZeroHeuristics(BasicBlock *BB) {
BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
if (!BI || !BI->isConditional())
return false;
Value *Cond = BI->getCondition();
ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
if (!CI)
return false;
Value *RHS = CI->getOperand(1);
ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
if (!CV)
return false;
bool isProb;
if (CV->isZero()) {
switch (CI->getPredicate()) {
case CmpInst::ICMP_EQ:
// X == 0 -> Unlikely
isProb = false;
break;
case CmpInst::ICMP_NE:
// X != 0 -> Likely
isProb = true;
break;
case CmpInst::ICMP_SLT:
// X < 0 -> Unlikely
isProb = false;
break;
case CmpInst::ICMP_SGT:
// X > 0 -> Likely
isProb = true;
break;
default:
return false;
}
} else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
// InstCombine canonicalizes X <= 0 into X < 1.
// X <= 0 -> Unlikely
isProb = false;
} else if (CV->isAllOnesValue() && CI->getPredicate() == CmpInst::ICMP_SGT) {
// InstCombine canonicalizes X >= 0 into X > -1.
// X >= 0 -> Likely
isProb = true;
} else {
return false;
}
BasicBlock *Taken = BI->getSuccessor(0);
BasicBlock *NonTaken = BI->getSuccessor(1);
if (!isProb)
std::swap(Taken, NonTaken);
BP->setEdgeWeight(BB, Taken, ZH_TAKEN_WEIGHT);
BP->setEdgeWeight(BB, NonTaken, ZH_NONTAKEN_WEIGHT);
return true;
}
示例7: calcFloatingPointHeuristics
bool BranchProbabilityInfo::calcFloatingPointHeuristics(BasicBlock *BB) {
BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
if (!BI || !BI->isConditional())
return false;
Value *Cond = BI->getCondition();
FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
if (!FCmp)
return false;
bool isProb;
if (FCmp->isEquality()) {
// f1 == f2 -> Unlikely
// f1 != f2 -> Likely
isProb = !FCmp->isTrueWhenEqual();
} else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) {
// !isnan -> Likely
isProb = true;
} else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) {
// isnan -> Unlikely
isProb = false;
} else {
return false;
}
unsigned TakenIdx = 0, NonTakenIdx = 1;
if (!isProb)
std::swap(TakenIdx, NonTakenIdx);
setEdgeWeight(BB, TakenIdx, FPH_TAKEN_WEIGHT);
setEdgeWeight(BB, NonTakenIdx, FPH_NONTAKEN_WEIGHT);
return true;
}
示例8: calcPointerHeuristics
// Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
// between two pointer or pointer and NULL will fail.
bool BranchProbabilityInfo::calcPointerHeuristics(BasicBlock *BB) {
BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
if (!BI || !BI->isConditional())
return false;
Value *Cond = BI->getCondition();
ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
if (!CI || !CI->isEquality())
return false;
Value *LHS = CI->getOperand(0);
if (!LHS->getType()->isPointerTy())
return false;
assert(CI->getOperand(1)->getType()->isPointerTy());
// p != 0 -> isProb = true
// p == 0 -> isProb = false
// p != q -> isProb = true
// p == q -> isProb = false;
unsigned TakenIdx = 0, NonTakenIdx = 1;
bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
if (!isProb)
std::swap(TakenIdx, NonTakenIdx);
setEdgeWeight(BB, TakenIdx, PH_TAKEN_WEIGHT);
setEdgeWeight(BB, NonTakenIdx, PH_NONTAKEN_WEIGHT);
return true;
}
示例9: gatherPredicates
/// \brief Analyze the predecessors of each block and build up predicates
void StructurizeCFG::gatherPredicates(RegionNode *N) {
RegionInfo *RI = ParentRegion->getRegionInfo();
BasicBlock *BB = N->getEntry();
BBPredicates &Pred = Predicates[BB];
BBPredicates &LPred = LoopPreds[BB];
for (BasicBlock *P : predecessors(BB)) {
// Ignore it if it's a branch from outside into our region entry
if (!ParentRegion->contains(P))
continue;
Region *R = RI->getRegionFor(P);
if (R == ParentRegion) {
// It's a top level block in our region
BranchInst *Term = cast<BranchInst>(P->getTerminator());
for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
BasicBlock *Succ = Term->getSuccessor(i);
if (Succ != BB)
continue;
if (Visited.count(P)) {
// Normal forward edge
if (Term->isConditional()) {
// Try to treat it like an ELSE block
BasicBlock *Other = Term->getSuccessor(!i);
if (Visited.count(Other) && !Loops.count(Other) &&
!Pred.count(Other) && !Pred.count(P)) {
Pred[Other] = BoolFalse;
Pred[P] = BoolTrue;
continue;
}
}
Pred[P] = buildCondition(Term, i, false);
} else {
// Back edge
LPred[P] = buildCondition(Term, i, true);
}
}
} else {
// It's an exit from a sub region
while (R->getParent() != ParentRegion)
R = R->getParent();
// Edge from inside a subregion to its entry, ignore it
if (*R == *N)
continue;
BasicBlock *Entry = R->getEntry();
if (Visited.count(Entry))
Pred[Entry] = BoolTrue;
else
LPred[Entry] = BoolFalse;
}
}
}
示例10: calcZeroHeuristics
bool BranchProbabilityAnalysis::calcZeroHeuristics(BasicBlock *BB) {
BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
if (!BI || !BI->isConditional())
return false;
Value *Cond = BI->getCondition();
ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
if (!CI)
return false;
Value *RHS = CI->getOperand(1);
ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
if (!CV || !CV->isZero())
return false;
bool isProb;
switch (CI->getPredicate()) {
case CmpInst::ICMP_EQ:
// Equal to zero is not expected to be taken.
isProb = false;
break;
case CmpInst::ICMP_NE:
// Not equal to zero is expected.
isProb = true;
break;
case CmpInst::ICMP_SLT:
// Less or equal to zero is not expected.
// X < 0 -> Unlikely
isProb = false;
break;
case CmpInst::ICMP_UGT:
case CmpInst::ICMP_SGT:
// Greater or equal to zero is expected.
// X > 0 -> Likely
isProb = true;
break;
default:
return false;
};
BasicBlock *Taken = BI->getSuccessor(0);
BasicBlock *NonTaken = BI->getSuccessor(1);
if (!isProb)
std::swap(Taken, NonTaken);
BP->setEdgeWeight(BB, Taken, ZH_TAKEN_WEIGHT);
BP->setEdgeWeight(BB, NonTaken, ZH_NONTAKEN_WEIGHT);
return true;
}
示例11: is_used_by_branch
bool ProfilingPass::is_used_by_branch(Instruction *I)
{
for (Value::use_iterator i = I->use_begin(), e = I->use_end(); i != e; ++i)
{
Instruction *use = dyn_cast<Instruction>(*i);
BranchInst *bi = dyn_cast<BranchInst>(use);
if(!bi)
continue;
if(bi->isConditional())
{ return true;}
}
return false;
}
示例12: optimizeCheckAway
// Tries to remove a sanity check; returns true if it worked.
bool AsapPass::optimizeCheckAway(llvm::Instruction *Inst) {
BranchInst *BI = cast<BranchInst>(Inst);
assert(BI->isConditional() && "Sanity check must be conditional branch.");
unsigned int RegularBranch = getRegularBranch(BI, SCI);
bool Changed = false;
if (RegularBranch == 0) {
BI->setCondition(ConstantInt::getTrue(Inst->getContext()));
Changed = true;
} else if (RegularBranch == 1) {
BI->setCondition(ConstantInt::getFalse(Inst->getContext()));
Changed = true;
} else {
// This can happen, e.g., in the following case:
// array[-1] = a + b;
// is transformed into
// if (a + b overflows)
// report_overflow()
// else
// report_index_out_of_bounds();
// In this case, removing the sanity check does not help much, so we
// just do nothing.
// Thanks to Will Dietz for his explanation at
// http://lists.cs.uiuc.edu/pipermail/llvmdev/2014-April/071958.html
dbgs() << "Warning: Sanity check with no regular branch found.\n";
dbgs() << "The sanity check has been kept intact.\n";
}
if (PrintRemovedChecks && Changed) {
DebugLoc DL = getSanityCheckDebugLoc(BI, RegularBranch);
printDebugLoc(DL, BI->getContext(), dbgs());
dbgs() << ": SanityCheck with cost ";
dbgs() << *BI->getMetadata("cost")->getOperand(0);
if (MDNode *IA = DL.getInlinedAt()) {
dbgs() << " (inlined at ";
printDebugLoc(DebugLoc(IA), BI->getContext(), dbgs());
dbgs() << ")";
}
BasicBlock *Succ = BI->getSuccessor(RegularBranch == 0 ? 1 : 0);
if (const CallInst *CI = SCI->findSanityCheckCall(Succ)) {
dbgs() << " " << CI->getCalledFunction()->getName();
}
dbgs() << "\n";
}
return Changed;
}
示例13: if
void Graph::DFS2_visit(DFSNode * DFS) {
DFS->C = GRAY;
if (DFS->T == UNKNOWN){
Instruction *I = dynamic_cast<Instruction *>(DFS->Bb->getTerminator());
BranchInst *BI = static_cast<BranchInst *>(I);
if (BI->isConditional()){
//branchmap[BI] = dfs->bb;
BranchMap[BI->getCondition()] = DFS->Bb;
if (TypeStack.empty())
DFS->T = IF;
else if (TypeStack.top() != ENDIF)
DFS->T = IF;
//else if (TypeStack.top() == ENDIF && Time == DFS->DTime - 2)
// DFS->T = ELSEIF;
else if (DFS->Bb->getName().substr(0,7) == "if.else")
DFS->T = ELSEIF;
//else if (TypeStack.top() == ELSEIF)
// DFS->T = ELSEIF;
else if(TypeStack.top() == ENDIF && (Time != DFS->DTime - 1))
DFS->T = IF;
else
DEBUG (errs() << "\n\n\n\n\n\n\nError at: " << " Top = " << TypeStack.top() << " Time = " << Time << " DTime = " << DFS->DTime << "\n\n\n\n\n\n\n\n\n");
}
else {
DEBUG (errs() << "Bb: " <<DFS->Bb << " is not conditional\n");
}
}
if (DFS->T != UNKNOWN){
Time = DFS->DTime;
TypeStack.push(DFS->T);
DEBUG (errs() << "Stack: " << TypeStack.top() <<'\n');
}
DEBUG (errs() << "Type of: " << DFS->Bb << " is: " << DFS->T
<< " global time: " << Time << " dfstime: " << DFS->DTime << '\n');
// for each vector adjacent to dfs
std::vector<DFSNode *> Svec = DFS->Vertex;
for (std::vector<DFSNode *>::iterator It = Svec.begin(); It != Svec.end(); ++It)
{
DFSNode *DN = *It;
if (DN->C == WHITE)
DFS2_visit(DN);
}
DFS->C = BLACK;
if (DFS->T != UNKNOWN)
Time = DFS->FTime;
}
示例14: ConvertSwitch
void MakeDispatcherPass::ConvertSwitch( Function& function )
{
BasicBlock* entryBB = &function.getEntryBlock();
std::cout << ":Processing " << function.getName().str() << std::endl;
for( Function::iterator i = function.begin(); i != function.end(); i++ )
{
BasicBlock* basicBlock = &*i;
Instruction* inst;
TerminatorInst* terminator = basicBlock->getTerminator();
assert( terminator && "Basic block is not well formed and has no terminator!" );
if( isa< BranchInst >( terminator ) )
{
// std::cout << "Branch Instruction !!!" << std::endl;
BranchInst* branchInst = dynamic_cast< BranchInst *>
(
basicBlock->getTerminator()
);
std::cout << "Branch Instruction, Opcode = " <<
branchInst->getOpcodeName();
if (branchInst->isConditional())
{
inst = dynamic_cast<Instruction *>(basicBlock->getTerminator());
std::cout << ", Is Conditional ";
// i->eraseFromParent();
}
else
{
std::cout << ", Is not conditionnal ";
}
// if (valbranch) // && !valbranch->getName().empty())
// {
// valbranch->getType()->dump();
// std::cout << valbranch->getName().str();
// }
// branchInst->eraseFromParent();
std::cout << std::endl;
// std::cout << branchInst->getCondition()->getName.str() << std::endl;
}
}
}
示例15: insertConditions
/// \brief Insert the missing branch conditions
void StructurizeCFG::insertConditions(bool Loops) {
BranchVector &Conds = Loops ? LoopConds : Conditions;
Value *Default = Loops ? BoolTrue : BoolFalse;
SSAUpdater PhiInserter;
for (BranchVector::iterator I = Conds.begin(),
E = Conds.end(); I != E; ++I) {
BranchInst *Term = *I;
assert(Term->isConditional());
BasicBlock *Parent = Term->getParent();
BasicBlock *SuccTrue = Term->getSuccessor(0);
BasicBlock *SuccFalse = Term->getSuccessor(1);
PhiInserter.Initialize(Boolean, "");
PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
NearestCommonDominator Dominator(DT);
Dominator.addBlock(Parent, false);
Value *ParentValue = 0;
for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
PI != PE; ++PI) {
if (PI->first == Parent) {
ParentValue = PI->second;
break;
}
PhiInserter.AddAvailableValue(PI->first, PI->second);
Dominator.addBlock(PI->first);
}
if (ParentValue) {
Term->setCondition(ParentValue);
} else {
if (!Dominator.wasResultExplicitMentioned())
PhiInserter.AddAvailableValue(Dominator.getResult(), Default);
Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent));
}
}
}