本文整理汇总了C++中ConstantInt::getBitWidth方法的典型用法代码示例。如果您正苦于以下问题:C++ ConstantInt::getBitWidth方法的具体用法?C++ ConstantInt::getBitWidth怎么用?C++ ConstantInt::getBitWidth使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConstantInt
的用法示例。
在下文中一共展示了ConstantInt::getBitWidth方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: factorArrayIndex
void StraightLineStrengthReduce::factorArrayIndex(Value *ArrayIdx,
const SCEV *Base,
uint64_t ElementSize,
GetElementPtrInst *GEP) {
// At least, ArrayIdx = ArrayIdx *nsw 1.
allocateCandidatesAndFindBasisForGEP(
Base, ConstantInt::get(cast<IntegerType>(ArrayIdx->getType()), 1),
ArrayIdx, ElementSize, GEP);
Value *LHS = nullptr;
ConstantInt *RHS = nullptr;
// One alternative is matching the SCEV of ArrayIdx instead of ArrayIdx
// itself. This would allow us to handle the shl case for free. However,
// matching SCEVs has two issues:
//
// 1. this would complicate rewriting because the rewriting procedure
// would have to translate SCEVs back to IR instructions. This translation
// is difficult when LHS is further evaluated to a composite SCEV.
//
// 2. ScalarEvolution is designed to be control-flow oblivious. It tends
// to strip nsw/nuw flags which are critical for SLSR to trace into
// sext'ed multiplication.
if (match(ArrayIdx, m_NSWMul(m_Value(LHS), m_ConstantInt(RHS)))) {
// SLSR is currently unsafe if i * S may overflow.
// GEP = Base + sext(LHS *nsw RHS) * ElementSize
allocateCandidatesAndFindBasisForGEP(Base, RHS, LHS, ElementSize, GEP);
} else if (match(ArrayIdx, m_NSWShl(m_Value(LHS), m_ConstantInt(RHS)))) {
// GEP = Base + sext(LHS <<nsw RHS) * ElementSize
// = Base + sext(LHS *nsw (1 << RHS)) * ElementSize
APInt One(RHS->getBitWidth(), 1);
ConstantInt *PowerOf2 =
ConstantInt::get(RHS->getContext(), One << RHS->getValue());
allocateCandidatesAndFindBasisForGEP(Base, PowerOf2, LHS, ElementSize, GEP);
}
}
示例2: decomposeBitTestICmp
bool llvm::decomposeBitTestICmp(const ICmpInst *I, CmpInst::Predicate &Pred,
Value *&X, Value *&Y, Value *&Z) {
ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1));
if (!C)
return false;
switch (I->getPredicate()) {
default:
return false;
case ICmpInst::ICMP_SLT:
// X < 0 is equivalent to (X & SignBit) != 0.
if (!C->isZero())
return false;
Y = ConstantInt::get(I->getContext(), APInt::getSignBit(C->getBitWidth()));
Pred = ICmpInst::ICMP_NE;
break;
case ICmpInst::ICMP_SGT:
// X > -1 is equivalent to (X & SignBit) == 0.
if (!C->isAllOnesValue())
return false;
Y = ConstantInt::get(I->getContext(), APInt::getSignBit(C->getBitWidth()));
Pred = ICmpInst::ICMP_EQ;
break;
case ICmpInst::ICMP_ULT:
// X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
if (!C->getValue().isPowerOf2())
return false;
Y = ConstantInt::get(I->getContext(), -C->getValue());
Pred = ICmpInst::ICMP_EQ;
break;
case ICmpInst::ICMP_UGT:
// X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
if (!(C->getValue() + 1).isPowerOf2())
return false;
Y = ConstantInt::get(I->getContext(), ~C->getValue());
Pred = ICmpInst::ICMP_NE;
break;
}
X = I->getOperand(0);
Z = ConstantInt::getNullValue(C->getType());
return true;
}
示例3: allocateCandidatesAndFindBasisForAdd
void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForAdd(
Value *LHS, Value *RHS, Instruction *I) {
Value *S = nullptr;
ConstantInt *Idx = nullptr;
if (match(RHS, m_Mul(m_Value(S), m_ConstantInt(Idx)))) {
// I = LHS + RHS = LHS + Idx * S
allocateCandidatesAndFindBasis(Candidate::Add, SE->getSCEV(LHS), Idx, S, I);
} else if (match(RHS, m_Shl(m_Value(S), m_ConstantInt(Idx)))) {
// I = LHS + RHS = LHS + (S << Idx) = LHS + S * (1 << Idx)
APInt One(Idx->getBitWidth(), 1);
Idx = ConstantInt::get(Idx->getContext(), One << Idx->getValue());
allocateCandidatesAndFindBasis(Candidate::Add, SE->getSCEV(LHS), Idx, S, I);
} else {
// At least, I = LHS + 1 * RHS
ConstantInt *One = ConstantInt::get(cast<IntegerType>(I->getType()), 1);
allocateCandidatesAndFindBasis(Candidate::Add, SE->getSCEV(LHS), One, RHS,
I);
}
}
示例4: if
/// foldSelectICmpAnd - If one of the constants is zero (we know they can't
/// both be) and we have an icmp instruction with zero, and we have an 'and'
/// with the non-constant value and a power of two we can turn the select
/// into a shift on the result of the 'and'.
static Value *foldSelectICmpAnd(const SelectInst &SI, ConstantInt *TrueVal,
ConstantInt *FalseVal,
InstCombiner::BuilderTy *Builder) {
const ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition());
if (!IC || !IC->isEquality() || !SI.getType()->isIntegerTy())
return nullptr;
if (!match(IC->getOperand(1), m_Zero()))
return nullptr;
ConstantInt *AndRHS;
Value *LHS = IC->getOperand(0);
if (!match(LHS, m_And(m_Value(), m_ConstantInt(AndRHS))))
return nullptr;
// If both select arms are non-zero see if we have a select of the form
// 'x ? 2^n + C : C'. Then we can offset both arms by C, use the logic
// for 'x ? 2^n : 0' and fix the thing up at the end.
ConstantInt *Offset = nullptr;
if (!TrueVal->isZero() && !FalseVal->isZero()) {
if ((TrueVal->getValue() - FalseVal->getValue()).isPowerOf2())
Offset = FalseVal;
else if ((FalseVal->getValue() - TrueVal->getValue()).isPowerOf2())
Offset = TrueVal;
else
return nullptr;
// Adjust TrueVal and FalseVal to the offset.
TrueVal = ConstantInt::get(Builder->getContext(),
TrueVal->getValue() - Offset->getValue());
FalseVal = ConstantInt::get(Builder->getContext(),
FalseVal->getValue() - Offset->getValue());
}
// Make sure the mask in the 'and' and one of the select arms is a power of 2.
if (!AndRHS->getValue().isPowerOf2() ||
(!TrueVal->getValue().isPowerOf2() &&
!FalseVal->getValue().isPowerOf2()))
return nullptr;
// Determine which shift is needed to transform result of the 'and' into the
// desired result.
ConstantInt *ValC = !TrueVal->isZero() ? TrueVal : FalseVal;
unsigned ValZeros = ValC->getValue().logBase2();
unsigned AndZeros = AndRHS->getValue().logBase2();
// If types don't match we can still convert the select by introducing a zext
// or a trunc of the 'and'. The trunc case requires that all of the truncated
// bits are zero, we can figure that out by looking at the 'and' mask.
if (AndZeros >= ValC->getBitWidth())
return nullptr;
Value *V = Builder->CreateZExtOrTrunc(LHS, SI.getType());
if (ValZeros > AndZeros)
V = Builder->CreateShl(V, ValZeros - AndZeros);
else if (ValZeros < AndZeros)
V = Builder->CreateLShr(V, AndZeros - ValZeros);
// Okay, now we know that everything is set up, we just don't know whether we
// have a icmp_ne or icmp_eq and whether the true or false val is the zero.
bool ShouldNotVal = !TrueVal->isZero();
ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE;
if (ShouldNotVal)
V = Builder->CreateXor(V, ValC);
// Apply an offset if needed.
if (Offset)
V = Builder->CreateAdd(V, Offset);
return V;
}
示例5: runOnBasicBlock
//.........这里部分代码省略.........
ConstantInt::get(op1->getType(), APInt::getSignedMaxValue(bw));
Value *int_min_s =
ConstantInt::get(op1->getType(), APInt::getSignedMinValue(bw));
Value *int_max =
builder.CreateSExt(int_max_s, IntegerType::get(M.getContext(), bw2));
Value *int_min =
builder.CreateSExt(int_min_s, IntegerType::get(M.getContext(), bw2));
if (ii->getIntrinsicID() == Intrinsic::sadd_with_overflow){
result_ext = builder.CreateAdd(op1ext, op2ext);
} else if (ii->getIntrinsicID() == Intrinsic::ssub_with_overflow){
result_ext = builder.CreateSub(op1ext, op2ext);
} else if (ii->getIntrinsicID() == Intrinsic::smul_with_overflow){
result_ext = builder.CreateMul(op1ext, op2ext);
}
overflow = builder.CreateOr(builder.CreateICmpSGT(result_ext, int_max),
builder.CreateICmpSLT(result_ext, int_min));
}
// This trunc cound be replaced by a more general trunc replacement
// that allows to detect also undefined behavior in assignments or
// overflow in operation with integers whose dimension is smaller than
// int's dimension, e.g.
// uint8_t = uint8_t + uint8_t;
// if one desires the wrapping should write
// uint8_t = (uint8_t + uint8_t) & 0xFF;
// before this, must check if it has side effects on other operations
result = builder.CreateTrunc(result_ext, op1->getType());
Value *resultStruct =
builder.CreateInsertValue(UndefValue::get(ii->getType()), result, 0);
resultStruct = builder.CreateInsertValue(resultStruct, overflow, 1);
ii->replaceAllUsesWith(resultStruct);
ii->removeFromParent();
delete ii;
dirty = true;
break;
}
case Intrinsic::dbg_value:
case Intrinsic::dbg_declare:
// Remove these regardless of lower intrinsics flag. This can
// be removed once IntrinsicLowering is fixed to not have bad
// caches.
ii->eraseFromParent();
dirty = true;
break;
case Intrinsic::trap: {
// Intrisic instruction "llvm.trap" found. Directly lower it to
// a call of the abort() function.
Function *F = cast<Function>(
M.getOrInsertFunction(
"abort", Type::getVoidTy(getGlobalContext()), NULL));
F->setDoesNotReturn();
F->setDoesNotThrow();
CallInst::Create(F, Twine(), ii);
new UnreachableInst(getGlobalContext(), ii);
ii->eraseFromParent();
dirty = true;
break;
}
case Intrinsic::objectsize: {
// We don't know the size of an object in general so we replace
// with 0 or -1 depending on the second argument to the intrinsic.
assert(ii->getNumArgOperands() == 2 && "wrong number of arguments");
Value *minArg = ii->getArgOperand(1);
assert(minArg && "Failed to get second argument");
ConstantInt *minArgAsInt = dyn_cast<ConstantInt>(minArg);
assert(minArgAsInt && "Second arg is not a ConstantInt");
assert(minArgAsInt->getBitWidth() == 1 && "Second argument is not an i1");
Value *replacement = NULL;
LLVM_TYPE_Q IntegerType *intType = dyn_cast<IntegerType>(ii->getType());
assert(intType && "intrinsic does not have integer return type");
if (minArgAsInt->isZero()) {
// min=false
replacement = ConstantInt::get(intType, -1, /*isSigned=*/true);
} else {
// min=true
replacement = ConstantInt::get(intType, 0, /*isSigned=*/false);
}
ii->replaceAllUsesWith(replacement);
ii->eraseFromParent();
dirty = true;
break;
}
default:
if (LowerIntrinsics)
IL->LowerIntrinsicCall(ii);
dirty = true;
break;
}
}
}
return dirty;
}