diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -961,6 +961,20 @@ return BinaryOperator::CreateAdd(NarrowCtlz, WidthDiff); } } + + if (match(Src, m_VScale(DL))) { + if (Trunc.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { + unsigned MaxVScale = Trunc.getFunction() + ->getFnAttribute(Attribute::VScaleRange) + .getVScaleRangeArgs() + .second; + if (Log2_32(MaxVScale) < DestWidth) { + Value *VScale = Builder.CreateVScale(ConstantInt::get(DestTy, 1)); + return replaceInstUsesWith(Trunc, VScale); + } + } + } + return nullptr; } diff --git a/llvm/test/Transforms/InstCombine/vscale_trunc.ll b/llvm/test/Transforms/InstCombine/vscale_trunc.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/vscale_trunc.ll @@ -0,0 +1,42 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -instcombine -S | FileCheck %s + +define i32 @vscale_trunc_i64toi32() #0 { +; CHECK-LABEL: @vscale_trunc_i64toi32( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.vscale.i32() +; CHECK-NEXT: ret i32 [[TMP0]] +entry: + %0 = call i64 @llvm.vscale.i64() + %1 = trunc i64 %0 to i32 + ret i32 %1 +} + +define i8 @vscale_SExt_i32toi8() #0 { +; CHECK-LABEL: @vscale_SExt_i32toi8( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = call i8 @llvm.vscale.i8() +; CHECK-NEXT: ret i8 [[TMP0]] +entry: + %0 = call i32 @llvm.vscale.i32() + %1 = trunc i32 %0 to i8 + ret i8 %1 +} + + +define i8 @vscale_SExt_i32toi8_poison() vscale_range(0, 1024) { +; CHECK-LABEL: @vscale_SExt_i32toi8_poison( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.vscale.i32() +; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[TMP0]] to i8 +; CHECK-NEXT: ret i8 [[TMP1]] + entry: + %0 = call i32 @llvm.vscale.i32() + %1 = trunc i32 %0 to i8 + ret i8 %1 +} + +attributes #0 = { vscale_range(0, 16) } + +declare i32 @llvm.vscale.i32() +declare i64 @llvm.vscale.i64()