These idioms already appear a number of places in code, and upcoming changes to the various sanitizers continue to need more instances of the same patterns.
Differential Revision: https://reviews.llvm.org/D145945
/// will be the same type as that of \p Scaling.
Value *CreateVScale(Constant *Scaling, const Twine &Name = "");
+ /// Create an expression which evaluates to the number of elements in \p EC
+ /// at runtime.
+ Value *CreateElementCount(Type *DstType, ElementCount EC);
+
+ /// Create an expression which evaluates to the number of units in \p Size
+ /// at runtime. This works for both units of bits and bytes.
+ Value *CreateTypeSize(Type *DstType, TypeSize Size);
+
/// Creates a vector of type \p DstType with the linear sequence <0, 1, ...>
Value *CreateStepVector(Type *DstType, const Twine &Name = "");
: CreateMul(CI, Scaling);
}
+Value *IRBuilderBase::CreateElementCount(Type *DstType, ElementCount EC) {
+ Constant *MinEC = ConstantInt::get(DstType, EC.getKnownMinValue());
+ return EC.isScalable() ? CreateVScale(MinEC) : MinEC;
+}
+
+Value *IRBuilderBase::CreateTypeSize(Type *DstType, TypeSize Size) {
+ Constant *MinSize = ConstantInt::get(DstType, Size.getKnownMinValue());
+ return Size.isScalable() ? CreateVScale(MinSize) : MinSize;
+}
+
Value *IRBuilderBase::CreateStepVector(Type *DstType, const Twine &Name) {
Type *STy = DstType->getScalarType();
if (isa<ScalableVectorType>(DstType)) {
Align Alignment = cast<ConstantInt>(II.getArgOperand(2))->getAlignValue();
VectorType *WideLoadTy = cast<VectorType>(II.getArgOperand(1)->getType());
ElementCount VF = WideLoadTy->getElementCount();
- Constant *EC =
- ConstantInt::get(Builder.getInt32Ty(), VF.getKnownMinValue());
- Value *RunTimeVF = VF.isScalable() ? Builder.CreateVScale(EC) : EC;
+ Value *RunTimeVF = Builder.CreateElementCount(Builder.getInt32Ty(), VF);
Value *LastLane = Builder.CreateSub(RunTimeVF, Builder.getInt32(1));
Value *Extract =
Builder.CreateExtractElement(II.getArgOperand(0), LastLane);
IRBuilder<> IRB(I);
- Constant *MinNumElem =
- ConstantInt::get(IntptrTy, VTy->getElementCount().getKnownMinValue());
- assert(isa<ScalableVectorType>(VTy) && "generalize if reused for fixed length");
- Value *NumElements = IRB.CreateVScale(MinNumElem);
+ Value *NumElements = IRB.CreateElementCount(IntptrTy, VTy->getElementCount());
Instruction *BodyIP;
Value *Index;
Instruction *I, Instruction *InsertBefore, Value *Addr, TypeSize TypeStoreSize,
bool IsWrite, Value *SizeArgument, bool UseCalls, uint32_t Exp) {
IRBuilder<> IRB(InsertBefore);
- Constant *MinBits =
- ConstantInt::get(IntptrTy, TypeStoreSize.getKnownMinValue());
- Value *NumBits =
- !TypeStoreSize.isScalable() ? MinBits : IRB.CreateVScale(MinBits);
+ Value *NumBits = IRB.CreateTypeSize(IntptrTy, TypeStoreSize);
Value *Size = IRB.CreateLShr(NumBits, ConstantInt::get(IntptrTy, 3));
Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
Value *createStepForVF(IRBuilderBase &B, Type *Ty, ElementCount VF,
int64_t Step) {
assert(Ty->isIntegerTy() && "Expected an integer step");
- Constant *StepVal = ConstantInt::get(Ty, Step * VF.getKnownMinValue());
- return VF.isScalable() ? B.CreateVScale(StepVal) : StepVal;
+ return B.CreateElementCount(Ty, VF.multiplyCoefficientBy(Step));
}
/// Return the runtime value for VF.
Value *getRuntimeVF(IRBuilderBase &B, Type *Ty, ElementCount VF) {
- Constant *EC = ConstantInt::get(Ty, VF.getKnownMinValue());
- return VF.isScalable() ? B.CreateVScale(EC) : EC;
+ return B.CreateElementCount(Ty, VF);
}
const SCEV *createTripCountSCEV(Type *IdxTy, PredicatedScalarEvolution &PSE) {