Skip to content

Commit ca0fe34

Browse files
committed
[InstSimplify] Simplify llvm.vscale when vscale_range attribute exists
Reduce llvm.vscale to constant based on vscale_range attribute. Differential Revision: https://reviews.llvm.org/D106850
1 parent 3ad6437 commit ca0fe34

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5772,13 +5772,31 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
57725772

57735773
static Value *simplifyIntrinsic(CallBase *Call, const SimplifyQuery &Q) {
57745774

5775-
// Intrinsics with no operands have some kind of side effect. Don't simplify.
57765775
unsigned NumOperands = Call->getNumArgOperands();
5777-
if (!NumOperands)
5778-
return nullptr;
5779-
57805776
Function *F = cast<Function>(Call->getCalledFunction());
57815777
Intrinsic::ID IID = F->getIntrinsicID();
5778+
5779+
// Most of the intrinsics with no operands have some kind of side effect.
5780+
// Don't simplify.
5781+
if (!NumOperands) {
5782+
switch (IID) {
5783+
case Intrinsic::vscale: {
5784+
auto Attr = Call->getFunction()->getFnAttribute(Attribute::VScaleRange);
5785+
if (!Attr.isValid())
5786+
return nullptr;
5787+
unsigned MinScalarVectorSize, MaxScalarVectorSize;
5788+
std::tie(MinScalarVectorSize, MaxScalarVectorSize) =
5789+
Attr.getVScaleRangeArgs();
5790+
if (MinScalarVectorSize == MaxScalarVectorSize &&
5791+
MaxScalarVectorSize != 0)
5792+
return ConstantInt::get(F->getReturnType(), MinScalarVectorSize);
5793+
return nullptr;
5794+
}
5795+
default:
5796+
return nullptr;
5797+
}
5798+
}
5799+
57825800
if (NumOperands == 1)
57835801
return simplifyUnaryIntrinsic(F, Call->getArgOperand(0), Q);
57845802

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -instsimplify -S | FileCheck %s
3+
4+
define i64 @vscale_i64() #1 {
5+
; CHECK-LABEL: @vscale_i64(
6+
; CHECK-NEXT: ret i64 1
7+
;
8+
%out = call i64 @llvm.vscale.i64()
9+
ret i64 %out
10+
}
11+
12+
define i32 @vscale_i32() #2 {
13+
; CHECK-LABEL: @vscale_i32(
14+
; CHECK-NEXT: ret i32 2
15+
;
16+
%out = call i32 @llvm.vscale.i32()
17+
ret i32 %out
18+
}
19+
20+
define i64 @vscale_i64_diff() #3 {
21+
; CHECK-LABEL: @vscale_i64_diff(
22+
; CHECK-NEXT: [[OUT:%.*]] = call i64 @llvm.vscale.i64()
23+
; CHECK-NEXT: ret i64 [[OUT]]
24+
;
25+
%out = call i64 @llvm.vscale.i64()
26+
ret i64 %out
27+
}
28+
29+
; Function Attrs: nofree nosync nounwind readnone willreturn
30+
declare i64 @llvm.vscale.i64() #0
31+
32+
; Function Attrs: nofree nosync nounwind readnone willreturn
33+
declare i32 @llvm.vscale.i32() #0
34+
35+
attributes #0 = { nofree nosync nounwind readnone willreturn }
36+
attributes #1 = { mustprogress nofree nosync nounwind uwtable vscale_range(1,1) }
37+
attributes #2 = { mustprogress nofree nosync nounwind uwtable vscale_range(2,2) }
38+
attributes #3 = { mustprogress nofree nosync nounwind uwtable vscale_range(2,4) }
39+
40+

0 commit comments

Comments
 (0)