Skip to content

Commit 54bb4be

Browse files
authored
[InstSimplify] Handle vec values when simplifying comparisons using range metadata (#84673)
Found that this failed with an assertion when vec was used in this optimization while working on #84627.
1 parent fa4cc39 commit 54bb4be

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3788,10 +3788,10 @@ static Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
37883788
*LHS_Instr->getMetadata(LLVMContext::MD_range));
37893789

37903790
if (LHS_CR.icmp(Pred, RHS_CR))
3791-
return ConstantInt::getTrue(RHS->getContext());
3791+
return ConstantInt::getTrue(ITy);
37923792

37933793
if (LHS_CR.icmp(CmpInst::getInversePredicate(Pred), RHS_CR))
3794-
return ConstantInt::getFalse(RHS->getContext());
3794+
return ConstantInt::getFalse(ITy);
37953795
}
37963796
}
37973797

llvm/test/Transforms/InstCombine/icmp-range.ll

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,42 @@ define i1 @test_two_ranges3(ptr nocapture readonly %arg1, ptr nocapture readonly
171171
ret i1 %rval
172172
}
173173

174+
; Values' ranges overlap each other, so it can not be simplified.
175+
define <2 x i1> @test_two_ranges_vec(ptr nocapture readonly %arg1, ptr nocapture readonly %arg2) {
176+
; CHECK-LABEL: @test_two_ranges_vec(
177+
; CHECK-NEXT: [[VAL1:%.*]] = load <2 x i32>, ptr [[ARG1:%.*]], align 8, !range [[RNG4]]
178+
; CHECK-NEXT: [[VAL2:%.*]] = load <2 x i32>, ptr [[ARG2:%.*]], align 8, !range [[RNG5]]
179+
; CHECK-NEXT: [[RVAL:%.*]] = icmp ult <2 x i32> [[VAL2]], [[VAL1]]
180+
; CHECK-NEXT: ret <2 x i1> [[RVAL]]
181+
;
182+
%val1 = load <2 x i32>, ptr %arg1, !range !5
183+
%val2 = load <2 x i32>, ptr %arg2, !range !6
184+
%rval = icmp ult <2 x i32> %val2, %val1
185+
ret <2 x i1> %rval
186+
}
187+
188+
; Values' ranges do not overlap each other, so it can simplified to false.
189+
define <2 x i1> @test_two_ranges_vec_true(ptr nocapture readonly %arg1, ptr nocapture readonly %arg2) {
190+
; CHECK-LABEL: @test_two_ranges_vec_true(
191+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
192+
;
193+
%val1 = load <2 x i32>, ptr %arg1, !range !0
194+
%val2 = load <2 x i32>, ptr %arg2, !range !6
195+
%rval = icmp ult <2 x i32> %val2, %val1
196+
ret <2 x i1> %rval
197+
}
198+
199+
; Values' ranges do not overlap each other, so it can simplified to false.
200+
define <2 x i1> @test_two_ranges_vec_false(ptr nocapture readonly %arg1, ptr nocapture readonly %arg2) {
201+
; CHECK-LABEL: @test_two_ranges_vec_false(
202+
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
203+
;
204+
%val1 = load <2 x i32>, ptr %arg1, !range !0
205+
%val2 = load <2 x i32>, ptr %arg2, !range !6
206+
%rval = icmp ugt <2 x i32> %val2, %val1
207+
ret <2 x i1> %rval
208+
}
209+
174210
define i1 @ugt_zext(i1 %b, i8 %x) {
175211
; CHECK-LABEL: @ugt_zext(
176212
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], 0

0 commit comments

Comments
 (0)