Skip to content

Commit ade7091

Browse files
committed
Added missed optimization: need optimization for further adaption
Add test for optimization Extend the case further to signed intergers and more in/exclusive ranges
1 parent 7d6888e commit ade7091

File tree

2 files changed

+91
-7
lines changed

2 files changed

+91
-7
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,6 +2178,20 @@ static Value *simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
21782178
match(Op1, m_Xor(m_Specific(A), m_SpecificInt(~*C1))))
21792179
return Constant::getNullValue(Op0->getType());
21802180

2181+
// (X <= ~Y) && (Y > ~X) --> 0
2182+
CmpPredicate Pred0, Pred1;
2183+
if (match(Op0,
2184+
m_c_ICmp(Pred0, m_Value(X), m_c_Xor(m_Value(Y), m_AllOnes()))) &&
2185+
match(Op1, m_c_ICmp(Pred1, m_Specific(Y),
2186+
m_c_Xor(m_Specific(X), m_AllOnes())))) {
2187+
if (ICmpInst::isLE(Pred0) && ICmpInst::isGT(Pred1))
2188+
return ConstantInt::getFalse(Op0->getType());
2189+
if (ICmpInst::isLT(Pred0) && ICmpInst::isGE(Pred1))
2190+
return ConstantInt::getFalse(Op0->getType());
2191+
if (ICmpInst::isLT(Pred0) && ICmpInst::isGT(Pred1))
2192+
return ConstantInt::getFalse(Op0->getType());
2193+
}
2194+
21812195
if (Op0->getType()->isIntOrIntVectorTy(1)) {
21822196
if (std::optional<bool> Implied = isImpliedCondition(Op0, Op1, Q.DL)) {
21832197
// If Op0 is true implies Op1 is true, then Op0 is a subset of Op1.
Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
22
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3-
define i1 @test(i32 %0, i32 %1) {
3+
define i1 @test_pass_et(i32 %0, i32 %1) {
44
; CHECK-LABEL: define i1 @test(
55
; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) {
66
; CHECK-NEXT: [[COMMON_RET:.*:]]
7-
; CHECK-NEXT: [[TMP2:%.*]] = xor i32 [[TMP0]], -1
8-
; CHECK-NEXT: [[TMP3:%.*]] = icmp ule i32 [[TMP1]], [[TMP2]]
9-
; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP1]], -1
10-
; CHECK-NEXT: [[TMP5:%.*]] = icmp ugt i32 [[TMP0]], [[TMP4]]
11-
; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = and i1 [[TMP3]], [[TMP5]]
12-
; CHECK-NEXT: ret i1 [[COMMON_RET_OP]]
7+
; CHECK-NEXT: ret i1 false
138
;
149
common.ret:
1510
%2 = xor i32 %0, -1
@@ -19,3 +14,78 @@ common.ret:
1914
%common.ret.op = and i1 %3, %5
2015
ret i1 %common.ret.op
2116
}
17+
18+
define i1 @test_pass_signed(i32 %0, i32 %1) {
19+
; CHECK-LABEL: define i1 @test(
20+
; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) {
21+
; CHECK-NEXT: [[COMMON_RET:.*:]]
22+
; CHECK-NEXT: ret i1 false
23+
;
24+
common.ret:
25+
%2 = xor i32 %0, -1
26+
%3 = icmp sle i32 %1, %2
27+
%4 = xor i32 %1, -1
28+
%5 = icmp sgt i32 %0, %4
29+
%common.ret.op = and i1 %3, %5
30+
ret i1 %common.ret.op
31+
}
32+
33+
define i1 @test_pass_tt(i32 %0, i32 %1) {
34+
common.ret:
35+
%2 = xor i32 %0, -1
36+
%3 = icmp ult i32 %1, %2
37+
%4 = xor i32 %1, -1
38+
%5 = icmp ugt i32 %0, %4
39+
%common.ret.op = and i1 %3, %5
40+
ret i1 %common.ret.op
41+
}
42+
43+
define i1 @test_pass_te(i32 %0, i32 %1) {
44+
common.ret:
45+
%2 = xor i32 %0, -1
46+
%3 = icmp ult i32 %1, %2
47+
%4 = xor i32 %1, -1
48+
%5 = icmp uge i32 %0, %4
49+
%common.ret.op = and i1 %3, %5
50+
ret i1 %common.ret.op
51+
}
52+
53+
define i1 @test_nopass_ee(i32 %0, i32 %1) {
54+
common.ret:
55+
%2 = xor i32 %0, -1
56+
%3 = icmp ule i32 %1, %2
57+
%4 = xor i32 %1, -1
58+
%5 = icmp uge i32 %0, %4
59+
%common.ret.op = and i1 %3, %5
60+
ret i1 %common.ret.op
61+
}
62+
63+
define i1 @test_no_change_et(i32 %0, i32 %1, i32 %2) {
64+
common.ret:
65+
%3 = xor i32 %0, -1
66+
%4 = icmp ule i32 %1, %3
67+
%5 = xor i32 %1, -1
68+
%6 = icmp ugt i32 %1, %5
69+
%common.ret.op = and i1 %6, %4
70+
ret i1 %common.ret.op
71+
}
72+
73+
define i1 @test_no_change_te(i32 %0, i32 %1, i32 %2) {
74+
common.ret:
75+
%3 = xor i32 %0, -1
76+
%4 = icmp ult i32 %1, %3
77+
%5 = xor i32 %1, -1
78+
%6 = icmp uge i32 %1, %5
79+
%common.ret.op = and i1 %6, %4
80+
ret i1 %common.ret.op
81+
}
82+
83+
define i1 @test_no_change_tt(i32 %0, i32 %1, i32 %2) {
84+
common.ret:
85+
%3 = xor i32 %0, -1
86+
%4 = icmp ult i32 %1, %3
87+
%5 = xor i32 %1, -1
88+
%6 = icmp ugt i32 %1, %5
89+
%common.ret.op = and i1 %6, %4
90+
ret i1 %common.ret.op
91+
}

0 commit comments

Comments
 (0)