llvm.org GIT mirror llvm / 9cd99a0
InstSimplify: Optimize away pointless comparisons (X & INT_MIN) ? X & INT_MAX : X into X & INT_MAX (X & INT_MIN) ? X : X & INT_MAX into X (X & INT_MIN) ? X | INT_MIN : X into X (X & INT_MIN) ? X : X | INT_MIN into X | INT_MIN git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224669 91177308-0d34-0410-b5e6-96231b3b80d8 David Majnemer 4 years ago
3 changed file(s) with 121 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
270270 /// \brief Match an integer or vector power of 2.
271271 inline cst_pred_ty m_Power2() { return cst_pred_ty(); }
272272 inline api_pred_ty m_Power2(const APInt *&V) { return V; }
273
274 struct is_maxsignedvalue {
275 bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
276 };
277
278 inline cst_pred_ty m_MaxSignedValue() { return cst_pred_ty(); }
279 inline api_pred_ty m_MaxSignedValue(const APInt *&V) { return V; }
273280
274281 template struct bind_ty {
275282 Class *&VR;
31543154 if (const auto *ICI = dyn_cast(CondVal)) {
31553155 Value *X;
31563156 const APInt *Y;
3157 if (ICI->isEquality() &&
3157 ICmpInst::Predicate Pred = ICI->getPredicate();
3158 if (ICmpInst::isEquality(Pred) &&
31583159 match(ICI->getOperand(0), m_And(m_Value(X), m_APInt(Y))) &&
31593160 match(ICI->getOperand(1), m_Zero())) {
3160 ICmpInst::Predicate Pred = ICI->getPredicate();
31613161 const APInt *C;
31623162 // (X & Y) == 0 ? X & ~Y : X --> X
31633163 // (X & Y) != 0 ? X & ~Y : X --> X & ~Y
31823182 *Y == *C)
31833183 return Pred == ICmpInst::ICMP_EQ ? TrueVal : FalseVal;
31843184 }
3185 }
3186 if (Pred == ICmpInst::ICMP_SLT && match(ICI->getOperand(1), m_Zero())) {
3187 // (X < 0) ? X : X | SignBit --> X | SignBit
3188 if (TrueVal == ICI->getOperand(0) &&
3189 match(FalseVal, m_Or(m_Specific(TrueVal), m_SignBit())))
3190 return FalseVal;
3191 // (X < 0) ? X | SignBit : X --> X
3192 if (FalseVal == ICI->getOperand(0) &&
3193 match(TrueVal, m_Or(m_Specific(FalseVal), m_SignBit())))
3194 return FalseVal;
3195 // (X < 0) ? X & INT_MAX : X --> X & INT_MAX
3196 if (FalseVal == ICI->getOperand(0) &&
3197 match(TrueVal, m_And(m_Specific(FalseVal), m_MaxSignedValue())))
3198 return TrueVal;
3199 // (X < 0) ? X : X & INT_MAX --> X
3200 if (TrueVal == ICI->getOperand(0) &&
3201 match(FalseVal, m_And(m_Specific(TrueVal), m_MaxSignedValue())))
3202 return TrueVal;
3203 }
3204 if (Pred == ICmpInst::ICMP_SGT && match(ICI->getOperand(1), m_AllOnes())) {
3205 // (X > -1) ? X : X | SignBit --> X
3206 if (TrueVal == ICI->getOperand(0) &&
3207 match(FalseVal, m_Or(m_Specific(TrueVal), m_SignBit())))
3208 return TrueVal;
3209 // (X > -1) ? X | SignBit : X --> X | SignBit
3210 if (FalseVal == ICI->getOperand(0) &&
3211 match(TrueVal, m_Or(m_Specific(FalseVal), m_SignBit())))
3212 return TrueVal;
3213 // (X > -1) ? X & INT_MAX : X --> X
3214 if (FalseVal == ICI->getOperand(0) &&
3215 match(TrueVal, m_And(m_Specific(FalseVal), m_MaxSignedValue())))
3216 return FalseVal;
3217 // (X > -1) ? X : X & INT_MAX --> X & INT_MAX
3218 if (TrueVal == ICI->getOperand(0) &&
3219 match(FalseVal, m_And(m_Specific(TrueVal), m_MaxSignedValue())))
3220 return FalseVal;
31853221 }
31863222 }
31873223
2727 ret i32 %and1.x
2828 ; CHECK-LABEL: @test3(
2929 ; CHECK: %[[and:.*]] = and i32 %x, -2
30 ; CHECK: ret i32 %[[and]]
31 }
32
33 define i32 @test4(i32 %X) {
34 %cmp = icmp slt i32 %X, 0
35 %or = or i32 %X, -2147483648
36 %cond = select i1 %cmp, i32 %X, i32 %or
37 ret i32 %cond
38 ; CHECK-LABEL: @test4
39 ; CHECK: %[[or:.*]] = or i32 %X, -2147483648
40 ; CHECK: ret i32 %[[or]]
41 }
42
43 define i32 @test5(i32 %X) {
44 %cmp = icmp slt i32 %X, 0
45 %or = or i32 %X, -2147483648
46 %cond = select i1 %cmp, i32 %or, i32 %X
47 ret i32 %cond
48 ; CHECK-LABEL: @test5
49 ; CHECK: ret i32 %X
50 }
51
52 define i32 @test6(i32 %X) {
53 %cmp = icmp slt i32 %X, 0
54 %and = and i32 %X, 2147483647
55 %cond = select i1 %cmp, i32 %and, i32 %X
56 ret i32 %cond
57 ; CHECK-LABEL: @test6
58 ; CHECK: %[[and:.*]] = and i32 %X, 2147483647
59 ; CHECK: ret i32 %[[and]]
60 }
61
62 define i32 @test7(i32 %X) {
63 %cmp = icmp slt i32 %X, 0
64 %and = and i32 %X, 2147483647
65 %cond = select i1 %cmp, i32 %X, i32 %and
66 ret i32 %cond
67 ; CHECK-LABEL: @test7
68 ; CHECK: ret i32 %X
69 }
70
71 define i32 @test8(i32 %X) {
72 %cmp = icmp sgt i32 %X, -1
73 %or = or i32 %X, -2147483648
74 %cond = select i1 %cmp, i32 %X, i32 %or
75 ret i32 %cond
76 ; CHECK-LABEL: @test8
77 ; CHECK: ret i32 %X
78 }
79
80 define i32 @test9(i32 %X) {
81 %cmp = icmp sgt i32 %X, -1
82 %or = or i32 %X, -2147483648
83 %cond = select i1 %cmp, i32 %or, i32 %X
84 ret i32 %cond
85 ; CHECK-LABEL: @test9
86 ; CHECK: %[[or:.*]] = or i32 %X, -2147483648
87 ; CHECK: ret i32 %[[or]]
88 }
89
90 define i32 @test10(i32 %X) {
91 %cmp = icmp sgt i32 %X, -1
92 %and = and i32 %X, 2147483647
93 %cond = select i1 %cmp, i32 %and, i32 %X
94 ret i32 %cond
95 ; CHECK-LABEL: @test10
96 ; CHECK: ret i32 %X
97 }
98
99 define i32 @test11(i32 %X) {
100 %cmp = icmp sgt i32 %X, -1
101 %and = and i32 %X, 2147483647
102 %cond = select i1 %cmp, i32 %X, i32 %and
103 ret i32 %cond
104 ; CHECK-LABEL: @test11
105 ; CHECK: %[[and:.*]] = and i32 %X, 2147483647
30106 ; CHECK: ret i32 %[[and]]
31107 }
32108