Index: llvm/include/llvm/Analysis/TargetTransformInfoImpl.h =================================================================== --- llvm/include/llvm/Analysis/TargetTransformInfoImpl.h +++ llvm/include/llvm/Analysis/TargetTransformInfoImpl.h @@ -718,10 +718,15 @@ // Assumes the address space is 0 when Ptr is nullptr. unsigned AS = (Ptr == nullptr ? 0 : Ptr->getType()->getPointerAddressSpace()); + + // Convert BaseOffset to an int64_t, clamping to int64_t::max if necessary. + int64_t BaseOffsetInt64 = + BaseOffset.sgt(std::numeric_limits::max()) + ? std::numeric_limits::max() + : BaseOffset.getSExtValue(); if (static_cast(this)->isLegalAddressingMode( - TargetType, const_cast(BaseGV), - static_cast(BaseOffset.getLimitedValue()), HasBaseReg, - Scale, AS)) + TargetType, const_cast(BaseGV), BaseOffsetInt64, + HasBaseReg, Scale, AS)) return TTI::TCC_Free; return TTI::TCC_Basic; } Index: llvm/test/Analysis/CostModel/ARM/gep.ll =================================================================== --- llvm/test/Analysis/CostModel/ARM/gep.ll +++ llvm/test/Analysis/CostModel/ARM/gep.ll @@ -83,5 +83,8 @@ ;CHECK: cost of 1 for instruction: {{.*}} getelementptr inbounds <4 x double>, <4 x double>* %c12 = getelementptr inbounds <4 x double>, <4 x double>* undef, i32 %i +;CHECK: cost of 0 for instruction: {{.*}} getelementptr inbounds i8, i8* + %d0 = getelementptr inbounds i8, i8* undef, i32 -1 + ret void }