diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -893,6 +893,14 @@ /// 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 = ""); diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp --- a/llvm/lib/IR/IRBuilder.cpp +++ b/llvm/lib/IR/IRBuilder.cpp @@ -107,6 +107,16 @@ : 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(DstType)) { diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -439,9 +439,7 @@ Align Alignment = cast(II.getArgOperand(2))->getAlignValue(); VectorType *WideLoadTy = cast(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); diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp --- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -1518,10 +1518,7 @@ IRBuilder<> IRB(I); - Constant *MinNumElem = - ConstantInt::get(IntptrTy, VTy->getElementCount().getKnownMinValue()); - assert(isa(VTy) && "generalize if reused for fixed length"); - Value *NumElements = IRB.CreateVScale(MinNumElem); + Value *NumElements = IRB.CreateElementCount(IntptrTy, VTy->getElementCount()); Instruction *BodyIP; Value *Index; @@ -1745,10 +1742,7 @@ 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); diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -975,14 +975,12 @@ 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) {