本文整理汇总了C++中APInt::logBase2方法的典型用法代码示例。如果您正苦于以下问题:C++ APInt::logBase2方法的具体用法?C++ APInt::logBase2怎么用?C++ APInt::logBase2使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类APInt
的用法示例。
在下文中一共展示了APInt::logBase2方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: lowerToSingleIndexGEPs
void SeparateConstOffsetFromGEP::lowerToSingleIndexGEPs(
GetElementPtrInst *Variadic, int64_t AccumulativeByteOffset) {
IRBuilder<> Builder(Variadic);
Type *IntPtrTy = DL->getIntPtrType(Variadic->getType());
Type *I8PtrTy =
Builder.getInt8PtrTy(Variadic->getType()->getPointerAddressSpace());
Value *ResultPtr = Variadic->getOperand(0);
if (ResultPtr->getType() != I8PtrTy)
ResultPtr = Builder.CreateBitCast(ResultPtr, I8PtrTy);
gep_type_iterator GTI = gep_type_begin(*Variadic);
// Create an ugly GEP for each sequential index. We don't create GEPs for
// structure indices, as they are accumulated in the constant offset index.
for (unsigned I = 1, E = Variadic->getNumOperands(); I != E; ++I, ++GTI) {
if (isa<SequentialType>(*GTI)) {
Value *Idx = Variadic->getOperand(I);
// Skip zero indices.
if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx))
if (CI->isZero())
continue;
APInt ElementSize = APInt(IntPtrTy->getIntegerBitWidth(),
DL->getTypeAllocSize(GTI.getIndexedType()));
// Scale the index by element size.
if (ElementSize != 1) {
if (ElementSize.isPowerOf2()) {
Idx = Builder.CreateShl(
Idx, ConstantInt::get(IntPtrTy, ElementSize.logBase2()));
} else {
Idx = Builder.CreateMul(Idx, ConstantInt::get(IntPtrTy, ElementSize));
}
}
// Create an ugly GEP with a single index for each index.
ResultPtr =
Builder.CreateGEP(Builder.getInt8Ty(), ResultPtr, Idx, "uglygep");
}
}
// Create a GEP with the constant offset index.
if (AccumulativeByteOffset != 0) {
Value *Offset = ConstantInt::get(IntPtrTy, AccumulativeByteOffset);
ResultPtr =
Builder.CreateGEP(Builder.getInt8Ty(), ResultPtr, Offset, "uglygep");
}
if (ResultPtr->getType() != Variadic->getType())
ResultPtr = Builder.CreateBitCast(ResultPtr, Variadic->getType());
Variadic->replaceAllUsesWith(ResultPtr);
Variadic->eraseFromParent();
}
示例2: ElementSize
Value *StraightLineStrengthReduce::emitBump(const Candidate &Basis,
const Candidate &C,
IRBuilder<> &Builder,
const DataLayout *DL,
bool &BumpWithUglyGEP) {
APInt Idx = C.Index->getValue(), BasisIdx = Basis.Index->getValue();
unifyBitWidth(Idx, BasisIdx);
APInt IndexOffset = Idx - BasisIdx;
BumpWithUglyGEP = false;
if (Basis.CandidateKind == Candidate::GEP) {
APInt ElementSize(
IndexOffset.getBitWidth(),
DL->getTypeAllocSize(
cast<GetElementPtrInst>(Basis.Ins)->getType()->getElementType()));
APInt Q, R;
APInt::sdivrem(IndexOffset, ElementSize, Q, R);
if (R.getSExtValue() == 0)
IndexOffset = Q;
else
BumpWithUglyGEP = true;
}
// Compute Bump = C - Basis = (i' - i) * S.
// Common case 1: if (i' - i) is 1, Bump = S.
if (IndexOffset.getSExtValue() == 1)
return C.Stride;
// Common case 2: if (i' - i) is -1, Bump = -S.
if (IndexOffset.getSExtValue() == -1)
return Builder.CreateNeg(C.Stride);
// Otherwise, Bump = (i' - i) * sext/trunc(S). Note that (i' - i) and S may
// have different bit widths.
IntegerType *DeltaType =
IntegerType::get(Basis.Ins->getContext(), IndexOffset.getBitWidth());
Value *ExtendedStride = Builder.CreateSExtOrTrunc(C.Stride, DeltaType);
if (IndexOffset.isPowerOf2()) {
// If (i' - i) is a power of 2, Bump = sext/trunc(S) << log(i' - i).
ConstantInt *Exponent = ConstantInt::get(DeltaType, IndexOffset.logBase2());
return Builder.CreateShl(ExtendedStride, Exponent);
}
if ((-IndexOffset).isPowerOf2()) {
// If (i - i') is a power of 2, Bump = -sext/trunc(S) << log(i' - i).
ConstantInt *Exponent =
ConstantInt::get(DeltaType, (-IndexOffset).logBase2());
return Builder.CreateNeg(Builder.CreateShl(ExtendedStride, Exponent));
}
Constant *Delta = ConstantInt::get(DeltaType, IndexOffset);
return Builder.CreateMul(ExtendedStride, Delta);
}
示例3: Builder
void
SeparateConstOffsetFromGEP::lowerToArithmetics(GetElementPtrInst *Variadic,
int64_t AccumulativeByteOffset) {
IRBuilder<> Builder(Variadic);
const DataLayout &DL = Variadic->getModule()->getDataLayout();
Type *IntPtrTy = DL.getIntPtrType(Variadic->getType());
Value *ResultPtr = Builder.CreatePtrToInt(Variadic->getOperand(0), IntPtrTy);
gep_type_iterator GTI = gep_type_begin(*Variadic);
// Create ADD/SHL/MUL arithmetic operations for each sequential indices. We
// don't create arithmetics for structure indices, as they are accumulated
// in the constant offset index.
for (unsigned I = 1, E = Variadic->getNumOperands(); I != E; ++I, ++GTI) {
if (isa<SequentialType>(*GTI)) {
Value *Idx = Variadic->getOperand(I);
// Skip zero indices.
if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx))
if (CI->isZero())
continue;
APInt ElementSize = APInt(IntPtrTy->getIntegerBitWidth(),
DL.getTypeAllocSize(GTI.getIndexedType()));
// Scale the index by element size.
if (ElementSize != 1) {
if (ElementSize.isPowerOf2()) {
Idx = Builder.CreateShl(
Idx, ConstantInt::get(IntPtrTy, ElementSize.logBase2()));
} else {
Idx = Builder.CreateMul(Idx, ConstantInt::get(IntPtrTy, ElementSize));
}
}
// Create an ADD for each index.
ResultPtr = Builder.CreateAdd(ResultPtr, Idx);
}
}
// Create an ADD for the constant offset index.
if (AccumulativeByteOffset != 0) {
ResultPtr = Builder.CreateAdd(
ResultPtr, ConstantInt::get(IntPtrTy, AccumulativeByteOffset));
}
ResultPtr = Builder.CreateIntToPtr(ResultPtr, Variadic->getType());
Variadic->replaceAllUsesWith(ResultPtr);
Variadic->eraseFromParent();
}