本文整理汇总了C++中switchinst::CaseIt::getCaseSuccessor方法的典型用法代码示例。如果您正苦于以下问题:C++ CaseIt::getCaseSuccessor方法的具体用法?C++ CaseIt::getCaseSuccessor怎么用?C++ CaseIt::getCaseSuccessor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类switchinst::CaseIt
的用法示例。
在下文中一共展示了CaseIt::getCaseSuccessor方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: LLVM_General_GetSwitchCases
void LLVM_General_GetSwitchCases(
LLVMValueRef v,
LLVMValueRef *values,
LLVMBasicBlockRef *dests
) {
SwitchInst *s = unwrap<SwitchInst>(v);
for(SwitchInst::CaseIt i = s->case_begin(); i != s->case_end(); ++i, ++values, ++dests) {
*values = wrap(i.getCaseValue());
*dests = wrap(i.getCaseSuccessor());
}
}
示例2: processSwitchInst
// processSwitchInst - Replace the specified switch instruction with a sequence
// of chained if-then instructions.
//
void LowerSwitchPass::processSwitchInst(SwitchInst *SI) {
BasicBlock *origBlock = SI->getParent();
BasicBlock *defaultBlock = SI->getDefaultDest();
Function *F = origBlock->getParent();
Value *switchValue = SI->getCondition();
// Create a new, empty default block so that the new hierarchy of
// if-then statements go to this and the PHI nodes are happy.
BasicBlock* newDefault = BasicBlock::Create(getGlobalContext(), "newDefault");
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 8)
F->getBasicBlockList().insert(defaultBlock->getIterator(), newDefault);
#else
F->getBasicBlockList().insert(defaultBlock, newDefault);
#endif
BranchInst::Create(defaultBlock, newDefault);
// If there is an entry in any PHI nodes for the default edge, make sure
// to update them as well.
for (BasicBlock::iterator I = defaultBlock->begin(); isa<PHINode>(I); ++I) {
PHINode *PN = cast<PHINode>(I);
int BlockIdx = PN->getBasicBlockIndex(origBlock);
assert(BlockIdx != -1 && "Switch didn't go to this successor??");
PN->setIncomingBlock((unsigned)BlockIdx, newDefault);
}
CaseVector cases;
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 1)
for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); i != e; ++i)
cases.push_back(SwitchCase(i.getCaseValue(),
i.getCaseSuccessor()));
#else
for (unsigned i = 1; i < SI->getNumSuccessors(); ++i)
cases.push_back(SwitchCase(SI->getSuccessorValue(i),
SI->getSuccessor(i)));
#endif
// reverse cases, as switchConvert constructs a chain of
// basic blocks by appending to the front. if we reverse,
// the if comparisons will happen in the same order
// as the cases appear in the switch
std::reverse(cases.begin(), cases.end());
switchConvert(cases.begin(), cases.end(), switchValue, origBlock, newDefault);
// We are now done with the switch instruction, so delete it
origBlock->getInstList().erase(SI);
}
示例3: processSwitch
/// processSwitch - Simplify a switch instruction by removing cases which can
/// never fire. If the uselessness of a case could be determined locally then
/// constant propagation would already have figured it out. Instead, walk the
/// predecessors and statically evaluate cases based on information available
/// on that edge. Cases that cannot fire no matter what the incoming edge can
/// safely be removed. If a case fires on every incoming edge then the entire
/// switch can be removed and replaced with a branch to the case destination.
bool CorrelatedValuePropagation::processSwitch(SwitchInst *SI) {
Value *Cond = SI->getCondition();
BasicBlock *BB = SI->getParent();
// If the condition was defined in same block as the switch then LazyValueInfo
// currently won't say anything useful about it, though in theory it could.
if (isa<Instruction>(Cond) && cast<Instruction>(Cond)->getParent() == BB)
return false;
// If the switch is unreachable then trying to improve it is a waste of time.
pred_iterator PB = pred_begin(BB), PE = pred_end(BB);
if (PB == PE) return false;
// Analyse each switch case in turn. This is done in reverse order so that
// removing a case doesn't cause trouble for the iteration.
bool Changed = false;
for (SwitchInst::CaseIt CI = SI->case_end(), CE = SI->case_begin(); CI-- != CE;
) {
ConstantInt *Case = CI.getCaseValue();
// Check to see if the switch condition is equal to/not equal to the case
// value on every incoming edge, equal/not equal being the same each time.
LazyValueInfo::Tristate State = LazyValueInfo::Unknown;
for (pred_iterator PI = PB; PI != PE; ++PI) {
// Is the switch condition equal to the case value?
LazyValueInfo::Tristate Value = LVI->getPredicateOnEdge(CmpInst::ICMP_EQ,
Cond, Case, *PI,
BB, SI);
// Give up on this case if nothing is known.
if (Value == LazyValueInfo::Unknown) {
State = LazyValueInfo::Unknown;
break;
}
// If this was the first edge to be visited, record that all other edges
// need to give the same result.
if (PI == PB) {
State = Value;
continue;
}
// If this case is known to fire for some edges and known not to fire for
// others then there is nothing we can do - give up.
if (Value != State) {
State = LazyValueInfo::Unknown;
break;
}
}
if (State == LazyValueInfo::False) {
// This case never fires - remove it.
CI.getCaseSuccessor()->removePredecessor(BB);
SI->removeCase(CI); // Does not invalidate the iterator.
// The condition can be modified by removePredecessor's PHI simplification
// logic.
Cond = SI->getCondition();
++NumDeadCases;
Changed = true;
} else if (State == LazyValueInfo::True) {
// This case always fires. Arrange for the switch to be turned into an
// unconditional branch by replacing the switch condition with the case
// value.
SI->setCondition(Case);
NumDeadCases += SI->getNumCases();
Changed = true;
break;
}
}
if (Changed)
// If the switch has been simplified to the point where it can be replaced
// by a branch then do so now.
ConstantFoldTerminator(BB);
return Changed;
}
示例4: getEdgeValueLocal
/// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
/// Val is not constrained on the edge.
static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
BasicBlock *BBTo, LVILatticeVal &Result) {
// TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
// know that v != 0.
if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
// If this is a conditional branch and only one successor goes to BBTo, then
// we maybe able to infer something from the condition.
if (BI->isConditional() &&
BI->getSuccessor(0) != BI->getSuccessor(1)) {
bool isTrueDest = BI->getSuccessor(0) == BBTo;
assert(BI->getSuccessor(!isTrueDest) == BBTo &&
"BBTo isn't a successor of BBFrom");
// If V is the condition of the branch itself, then we know exactly what
// it is.
if (BI->getCondition() == Val) {
Result = LVILatticeVal::get(ConstantInt::get(
Type::getInt1Ty(Val->getContext()), isTrueDest));
return true;
}
// If the condition of the branch is an equality comparison, we may be
// able to infer the value.
ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition());
if (ICI && isa<Constant>(ICI->getOperand(1))) {
if (ICI->isEquality() && ICI->getOperand(0) == Val) {
// We know that V has the RHS constant if this is a true SETEQ or
// false SETNE.
if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ))
Result = LVILatticeVal::get(cast<Constant>(ICI->getOperand(1)));
else
Result = LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1)));
return true;
}
// Recognize the range checking idiom that InstCombine produces.
// (X-C1) u< C2 --> [C1, C1+C2)
ConstantInt *NegOffset = 0;
if (ICI->getPredicate() == ICmpInst::ICMP_ULT)
match(ICI->getOperand(0), m_Add(m_Specific(Val),
m_ConstantInt(NegOffset)));
ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1));
if (CI && (ICI->getOperand(0) == Val || NegOffset)) {
// Calculate the range of values that would satisfy the comparison.
ConstantRange CmpRange(CI->getValue());
ConstantRange TrueValues =
ConstantRange::makeICmpRegion(ICI->getPredicate(), CmpRange);
if (NegOffset) // Apply the offset from above.
TrueValues = TrueValues.subtract(NegOffset->getValue());
// If we're interested in the false dest, invert the condition.
if (!isTrueDest) TrueValues = TrueValues.inverse();
Result = LVILatticeVal::getRange(TrueValues);
return true;
}
}
}
}
// If the edge was formed by a switch on the value, then we may know exactly
// what it is.
if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
if (SI->getCondition() != Val)
return false;
bool DefaultCase = SI->getDefaultDest() == BBTo;
unsigned BitWidth = Val->getType()->getIntegerBitWidth();
ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
i != e; ++i) {
ConstantRange EdgeVal(i.getCaseValue()->getValue());
if (DefaultCase) {
// It is possible that the default destination is the destination of
// some cases. There is no need to perform difference for those cases.
if (i.getCaseSuccessor() != BBTo)
EdgesVals = EdgesVals.difference(EdgeVal);
} else if (i.getCaseSuccessor() == BBTo)
EdgesVals = EdgesVals.unionWith(EdgeVal);
}
Result = LVILatticeVal::getRange(EdgesVals);
return true;
}
return false;
}
示例5: IsTrivialUnswitchCondition
/// IsTrivialUnswitchCondition - Check to see if this unswitch condition is
/// trivial: that is, that the condition controls whether or not the loop does
/// anything at all. If this is a trivial condition, unswitching produces no
/// code duplications (equivalently, it produces a simpler loop and a new empty
/// loop, which gets deleted).
///
/// If this is a trivial condition, return true, otherwise return false. When
/// returning true, this sets Cond and Val to the condition that controls the
/// trivial condition: when Cond dynamically equals Val, the loop is known to
/// exit. Finally, this sets LoopExit to the BB that the loop exits to when
/// Cond == Val.
///
bool LoopUnswitch::IsTrivialUnswitchCondition(Value *Cond, Constant **Val,
BasicBlock **LoopExit) {
BasicBlock *Header = currentLoop->getHeader();
TerminatorInst *HeaderTerm = Header->getTerminator();
LLVMContext &Context = Header->getContext();
BasicBlock *LoopExitBB = 0;
if (BranchInst *BI = dyn_cast<BranchInst>(HeaderTerm)) {
// If the header block doesn't end with a conditional branch on Cond, we
// can't handle it.
if (!BI->isConditional() || BI->getCondition() != Cond)
return false;
// Check to see if a successor of the branch is guaranteed to
// exit through a unique exit block without having any
// side-effects. If so, determine the value of Cond that causes it to do
// this.
if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop,
BI->getSuccessor(0)))) {
if (Val) *Val = ConstantInt::getTrue(Context);
} else if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop,
BI->getSuccessor(1)))) {
if (Val) *Val = ConstantInt::getFalse(Context);
}
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(HeaderTerm)) {
// If this isn't a switch on Cond, we can't handle it.
if (SI->getCondition() != Cond) return false;
// Check to see if a successor of the switch is guaranteed to go to the
// latch block or exit through a one exit block without having any
// side-effects. If so, determine the value of Cond that causes it to do
// this.
// Note that we can't trivially unswitch on the default case or
// on already unswitched cases.
for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
i != e; ++i) {
BasicBlock* LoopExitCandidate;
if ((LoopExitCandidate = isTrivialLoopExitBlock(currentLoop,
i.getCaseSuccessor()))) {
// Okay, we found a trivial case, remember the value that is trivial.
ConstantInt* CaseVal = i.getCaseValue();
// Check that it was not unswitched before, since already unswitched
// trivial vals are looks trivial too.
if (BranchesInfo.isUnswitched(SI, CaseVal))
continue;
LoopExitBB = LoopExitCandidate;
if (Val) *Val = CaseVal;
break;
}
}
}
// If we didn't find a single unique LoopExit block, or if the loop exit block
// contains phi nodes, this isn't trivial.
if (!LoopExitBB || isa<PHINode>(LoopExitBB->begin()))
return false; // Can't handle this.
if (LoopExit) *LoopExit = LoopExitBB;
// We already know that nothing uses any scalar values defined inside of this
// loop. As such, we just have to check to see if this loop will execute any
// side-effecting instructions (e.g. stores, calls, volatile loads) in the
// part of the loop that the code *would* execute. We already checked the
// tail, check the header now.
for (BasicBlock::iterator I = Header->begin(), E = Header->end(); I != E; ++I)
if (I->mayHaveSideEffects())
return false;
return true;
}
示例6: RewriteLoopBodyWithConditionConstant
// RewriteLoopBodyWithConditionConstant - We know either that the value LIC has
// the value specified by Val in the specified loop, or we know it does NOT have
// that value. Rewrite any uses of LIC or of properties correlated to it.
void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,
Constant *Val,
bool IsEqual) {
assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?");
// FIXME: Support correlated properties, like:
// for (...)
// if (li1 < li2)
// ...
// if (li1 > li2)
// ...
// FOLD boolean conditions (X|LIC), (X&LIC). Fold conditional branches,
// selects, switches.
std::vector<Instruction*> Worklist;
LLVMContext &Context = Val->getContext();
// If we know that LIC == Val, or that LIC == NotVal, just replace uses of LIC
// in the loop with the appropriate one directly.
if (IsEqual || (isa<ConstantInt>(Val) &&
Val->getType()->isIntegerTy(1))) {
Value *Replacement;
if (IsEqual)
Replacement = Val;
else
Replacement = ConstantInt::get(Type::getInt1Ty(Val->getContext()),
!cast<ConstantInt>(Val)->getZExtValue());
for (Value::use_iterator UI = LIC->use_begin(), E = LIC->use_end();
UI != E; ++UI) {
Instruction *U = dyn_cast<Instruction>(*UI);
if (!U || !L->contains(U))
continue;
Worklist.push_back(U);
}
for (std::vector<Instruction*>::iterator UI = Worklist.begin();
UI != Worklist.end(); ++UI)
(*UI)->replaceUsesOfWith(LIC, Replacement);
SimplifyCode(Worklist, L);
return;
}
// Otherwise, we don't know the precise value of LIC, but we do know that it
// is certainly NOT "Val". As such, simplify any uses in the loop that we
// can. This case occurs when we unswitch switch statements.
for (Value::use_iterator UI = LIC->use_begin(), E = LIC->use_end();
UI != E; ++UI) {
Instruction *U = dyn_cast<Instruction>(*UI);
if (!U || !L->contains(U))
continue;
Worklist.push_back(U);
// TODO: We could do other simplifications, for example, turning
// 'icmp eq LIC, Val' -> false.
// If we know that LIC is not Val, use this info to simplify code.
SwitchInst *SI = dyn_cast<SwitchInst>(U);
if (SI == 0 || !isa<ConstantInt>(Val)) continue;
SwitchInst::CaseIt DeadCase = SI->findCaseValue(cast<ConstantInt>(Val));
// Default case is live for multiple values.
if (DeadCase == SI->case_default()) continue;
// Found a dead case value. Don't remove PHI nodes in the
// successor if they become single-entry, those PHI nodes may
// be in the Users list.
BasicBlock *Switch = SI->getParent();
BasicBlock *SISucc = DeadCase.getCaseSuccessor();
BasicBlock *Latch = L->getLoopLatch();
BranchesInfo.setUnswitched(SI, Val);
if (!SI->findCaseDest(SISucc)) continue; // Edge is critical.
// If the DeadCase successor dominates the loop latch, then the
// transformation isn't safe since it will delete the sole predecessor edge
// to the latch.
if (Latch && DT->dominates(SISucc, Latch))
continue;
// FIXME: This is a hack. We need to keep the successor around
// and hooked up so as to preserve the loop structure, because
// trying to update it is complicated. So instead we preserve the
// loop structure and put the block on a dead code path.
SplitEdge(Switch, SISucc, this);
// Compute the successors instead of relying on the return value
// of SplitEdge, since it may have split the switch successor
// after PHI nodes.
BasicBlock *NewSISucc = DeadCase.getCaseSuccessor();
BasicBlock *OldSISucc = *succ_begin(NewSISucc);
// Create an "unreachable" destination.
BasicBlock *Abort = BasicBlock::Create(Context, "us-unreachable",
Switch->getParent(),
//.........这里部分代码省略.........
示例7: checkBlockOutgoingEdges
//.........这里部分代码省略.........
bool changed = false;
ConstantInt* ConstCondition = dyn_cast_or_null<ConstantInt>(getConstReplacement(Condition));
if(!ConstCondition) {
if(Condition.t == SHADOWVAL_INST || Condition.t == SHADOWVAL_ARG) {
// Switch statements can operate on a ptrtoint operand, of which only ptrtoint(null) is useful:
if(ImprovedValSetSingle* IVS = dyn_cast_or_null<ImprovedValSetSingle>(getIVSRef(Condition))) {
if(IVS->onlyContainsNulls()) {
ConstCondition = cast<ConstantInt>(Constant::getNullValue(SI->invar->I->getOperand(0)->getType()));
}
}
}
}
if(!ConstCondition) {
std::pair<ValSetType, ImprovedVal> PathVal;
if(tryGetPathValue(Condition, SI->parent, PathVal))
ConstCondition = dyn_cast_val<ConstantInt>(PathVal.second.V);
}
TerminatorInst* TI = cast_inst<TerminatorInst>(SI);
const unsigned NumSucc = TI->getNumSuccessors();
if(ConstCondition) {
BasicBlock* takenTarget = 0;
if(BranchInst* BI = dyn_cast_inst<BranchInst>(SI)) {
// This ought to be a boolean.
if(ConstCondition->isZero())
takenTarget = BI->getSuccessor(1);
else
takenTarget = BI->getSuccessor(0);
}
else {
SwitchInst* SwI = cast_inst<SwitchInst>(SI);
SwitchInst::CaseIt targetidx = SwI->findCaseValue(ConstCondition);
takenTarget = targetidx.getCaseSuccessor();
}
if(takenTarget) {
// We know where the instruction is going -- remove this block as a predecessor for its other targets.
LPDEBUG("Branch or switch instruction given known target: " << takenTarget->getName() << "\n");
return setEdgeAlive(TI, SI->parent, takenTarget);
}
// Else fall through to set all alive.
}
SwitchInst* Switch;
ImprovedValSetSingle* IVS;
if((Switch = dyn_cast_inst<SwitchInst>(SI)) &&
(IVS = dyn_cast<ImprovedValSetSingle>(getIVSRef(Condition))) &&
IVS->SetType == ValSetTypeScalar &&
!IVS->Values.empty()) {
// A set of values feeding a switch. Set each corresponding edge alive.
bool changed = false;
for (unsigned i = 0, ilim = IVS->Values.size(); i != ilim; ++i) {
SwitchInst::CaseIt targetit = Switch->findCaseValue(cast<ConstantInt>(getConstReplacement(IVS->Values[i].V)));
BasicBlock* target = targetit.getCaseSuccessor();
changed |= setEdgeAlive(TI, SI->parent, target);
}
return changed;
}
// Condition unknown -- set all successors alive.
for (unsigned I = 0; I != NumSucc; ++I) {
// Mark outgoing edge alive
if(!SI->parent->succsAlive[I])
changed = true;
SI->parent->succsAlive[I] = true;
}
return changed;
}
示例8: convertInstruction
//.........这里部分代码省略.........
// overflow. So clear them now.
case Instruction::Add:
case Instruction::Sub:
if (!(Binop->hasNoUnsignedWrap() && Binop->hasNoSignedWrap()))
NewInst = getClearUpper(NewInst, Binop->getType(), Binop);
break;
case Instruction::Shl:
if (!Binop->hasNoUnsignedWrap())
NewInst = getClearUpper(NewInst, Binop->getType(), Binop);
break;
// We modified the upper bits ourselves when implementing AShr
case Instruction::AShr:
NewInst = getClearUpper(NewInst, Binop->getType(), Binop);
break;
// We should not see FP operators here.
// We don't handle mul/div.
case Instruction::FAdd:
case Instruction::FSub:
case Instruction::Mul:
case Instruction::FMul:
case Instruction::UDiv:
case Instruction::SDiv:
case Instruction::FDiv:
case Instruction::URem:
case Instruction::SRem:
case Instruction::FRem:
case Instruction::BinaryOpsEnd:
errs() << *Inst << "\n";
llvm_unreachable("Cannot handle binary operator");
break;
}
State.recordConverted(Binop, NewInst);
} else if (ICmpInst *Cmp = dyn_cast<ICmpInst>(Inst)) {
Value *Op0, *Op1;
// For signed compares, operands are sign-extended to their
// promoted type. For unsigned or equality compares, the comparison
// is equivalent with the larger type because they are already
// zero-extended.
if (Cmp->isSigned()) {
Op0 = getSignExtend(State.getConverted(Cmp->getOperand(0)),
Cmp->getOperand(0),
Cmp);
Op1 = getSignExtend(State.getConverted(Cmp->getOperand(1)),
Cmp->getOperand(1),
Cmp);
} else {
Op0 = State.getConverted(Cmp->getOperand(0));
Op1 = State.getConverted(Cmp->getOperand(1));
}
ICmpInst *NewInst = new ICmpInst(
Cmp, Cmp->getPredicate(), Op0, Op1, "");
State.recordConverted(Cmp, NewInst);
} else if (SelectInst *Select = dyn_cast<SelectInst>(Inst)) {
SelectInst *NewInst = SelectInst::Create(
Select->getCondition(),
State.getConverted(Select->getTrueValue()),
State.getConverted(Select->getFalseValue()),
"", Select);
State.recordConverted(Select, NewInst);
} else if (PHINode *Phi = dyn_cast<PHINode>(Inst)) {
PHINode *NewPhi = PHINode::Create(
getPromotedType(Phi->getType()),
Phi->getNumIncomingValues(),
"", Phi);
for (unsigned I = 0, E = Phi->getNumIncomingValues(); I < E; ++I) {
NewPhi->addIncoming(State.getConverted(Phi->getIncomingValue(I)),
Phi->getIncomingBlock(I));
}
State.recordConverted(Phi, NewPhi);
} else if (SwitchInst *Switch = dyn_cast<SwitchInst>(Inst)) {
SwitchInst *NewInst = SwitchInst::Create(
State.getConverted(Switch->getCondition()),
Switch->getDefaultDest(),
Switch->getNumCases(),
Switch);
for (SwitchInst::CaseIt I = Switch->case_begin(),
E = Switch->case_end();
I != E; ++I) {
// Build a new case from the ranges that map to the successor BB. Each
// range consists of a high and low value which are typed, so the ranges
// must be rebuilt and a new case constructed from them.
IntegersSubset CaseRanges = I.getCaseValueEx();
IntegersSubsetToBB CaseBuilder;
for (unsigned RI = 0, RE = CaseRanges.getNumItems(); RI < RE; ++RI) {
CaseBuilder.add(
IntItem::fromConstantInt(cast<ConstantInt>(convertConstant(
CaseRanges.getItem(RI).getLow().toConstantInt()))),
IntItem::fromConstantInt(cast<ConstantInt>(convertConstant(
CaseRanges.getItem(RI).getHigh().toConstantInt()))));
}
IntegersSubset Case = CaseBuilder.getCase();
NewInst->addCase(Case, I.getCaseSuccessor());
}
Switch->eraseFromParent();
} else {
errs() << *Inst<<"\n";
llvm_unreachable("unhandled instruction");
}
}