llvm.org GIT mirror llvm / dafcef6
[IPSCCP] Add getCompare which returns either true, false, undef or null. getCompare returns true, false or undef constants if the comparison can be evaluated, or nullptr if it cannot. This is in line with what ConstantExpr::getCompare returns. It also allows us to use ConstantExpr::getCompare for comparing constants. Reviewers: davide, mssimpso, dberlin, anna Reviewed By: davide Differential Revision: https://reviews.llvm.org/D43761 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326720 91177308-0d34-0410-b5e6-96231b3b80d8 Florian Hahn 2 years ago
4 changed file(s) with 116 addition(s) and 61 deletion(s). Raw diff Collapse all Expand all
285285 return cast(getConstant());
286286 }
287287
288 bool satisfiesPredicate(CmpInst::Predicate Pred,
289 const ValueLatticeElement &Other) const {
290 // TODO: share with LVI getPredicateResult.
291
288 /// Compares this symbolic value with Other using Pred and returns either
289 /// true, false or undef constants, or nullptr if the comparison cannot be
290 /// evaluated.
291 Constant *getCompare(CmpInst::Predicate Pred, Type *Ty,
292 const ValueLatticeElement &Other) const {
292293 if (isUndefined() || Other.isUndefined())
293 return true;
294
295 if (isConstant() && Other.isConstant() && Pred == CmpInst::FCMP_OEQ)
296 return getConstant() == Other.getConstant();
294 return UndefValue::get(Ty);
295
296 if (isConstant() && Other.isConstant())
297 return ConstantExpr::getCompare(Pred, getConstant(), Other.getConstant());
297298
298299 // Integer constants are represented as ConstantRanges with single
299300 // elements.
300301 if (!isConstantRange() || !Other.isConstantRange())
301 return false;
302 return nullptr;
302303
303304 const auto &CR = getConstantRange();
304305 const auto &OtherCR = Other.getConstantRange();
305 return ConstantRange::makeSatisfyingICmpRegion(Pred, OtherCR).contains(CR);
306 if (ConstantRange::makeSatisfyingICmpRegion(Pred, OtherCR).contains(CR))
307 return ConstantInt::getTrue(Ty);
308 if (ConstantRange::makeSatisfyingICmpRegion(
309 CmpInst::getInversePredicate(Pred), OtherCR)
310 .contains(CR))
311 return ConstantInt::getFalse(Ty);
312
313 return nullptr;
306314 }
307315 };
308316
16431643 ValueLatticeElement A = getIcmpLatticeValue(Icmp->getOperand(0));
16441644 ValueLatticeElement B = getIcmpLatticeValue(Icmp->getOperand(1));
16451645
1646 Constant *C = nullptr;
1647 if (A.satisfiesPredicate(Icmp->getPredicate(), B))
1648 C = ConstantInt::getTrue(Icmp->getType());
1649 else if (A.satisfiesPredicate(Icmp->getInversePredicate(), B))
1650 C = ConstantInt::getFalse(Icmp->getType());
1651
1646 Constant *C = A.getCompare(Icmp->getPredicate(), Icmp->getType(), B);
16521647 if (C) {
16531648 Icmp->replaceAllUsesWith(C);
16541649 DEBUG(dbgs() << "Replacing " << *Icmp << " with " << *C
140140 %r = fmul double %v, %v
141141 ret double %r
142142 }
143
144 ; Constant range for %x is [47, 302)
145 ; CHECK-LABEL: @f5
146 ; CHECK-NEXT: entry:
147 ; CHECK-NEXT: %res1 = select i1 undef, i32 1, i32 2
148 ; CHECK-NEXT: %res2 = select i1 undef, i32 3, i32 4
149 ; CHECK-NEXT: %res = add i32 %res1, %res2
150 ; CHECK-NEXT: ret i32 %res
151 define internal i32 @f5(i32 %x) {
152 entry:
153 %cmp = icmp sgt i32 %x, undef
154 %cmp2 = icmp ne i32 undef, %x
155 %res1 = select i1 %cmp, i32 1, i32 2
156 %res2 = select i1 %cmp2, i32 3, i32 4
157
158 %res = add i32 %res1, %res2
159 ret i32 %res
160 }
161
162 define i32 @caller4() {
163 entry:
164 %call1 = tail call i32 @f5(i32 47)
165 %call2 = tail call i32 @f5(i32 301)
166 %res = add nsw i32 %call1, %call2
167 ret i32 %res
168 }
7575 EXPECT_TRUE(LV1.isOverdefined());
7676 }
7777
78 TEST_F(ValueLatticeTest, satisfiesPredicateIntegers) {
79 auto I32Ty = IntegerType::get(Context, 32);
78 TEST_F(ValueLatticeTest, getCompareIntegers) {
79 auto *I32Ty = IntegerType::get(Context, 32);
80 auto *I1Ty = IntegerType::get(Context, 1);
8081 auto *C1 = ConstantInt::get(I32Ty, 1);
8182 auto LV1 = ValueLatticeElement::get(C1);
8283
83 // Check satisfiesPredicate for equal integer constants.
84 EXPECT_TRUE(LV1.satisfiesPredicate(CmpInst::ICMP_EQ, LV1));
85 EXPECT_TRUE(LV1.satisfiesPredicate(CmpInst::ICMP_SGE, LV1));
86 EXPECT_TRUE(LV1.satisfiesPredicate(CmpInst::ICMP_SLE, LV1));
87 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::ICMP_NE, LV1));
88 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::ICMP_SLT, LV1));
89 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::ICMP_SGT, LV1));
84 // Check getCompare for equal integer constants.
85 EXPECT_TRUE(LV1.getCompare(CmpInst::ICMP_EQ, I1Ty, LV1)->isOneValue());
86 EXPECT_TRUE(LV1.getCompare(CmpInst::ICMP_SGE, I1Ty, LV1)->isOneValue());
87 EXPECT_TRUE(LV1.getCompare(CmpInst::ICMP_SLE, I1Ty, LV1)->isOneValue());
88 EXPECT_TRUE(LV1.getCompare(CmpInst::ICMP_NE, I1Ty, LV1)->isZeroValue());
89 EXPECT_TRUE(LV1.getCompare(CmpInst::ICMP_SLT, I1Ty, LV1)->isZeroValue());
90 EXPECT_TRUE(LV1.getCompare(CmpInst::ICMP_SGT, I1Ty, LV1)->isZeroValue());
9091
9192 auto LV2 =
9293 ValueLatticeElement::getRange({APInt(32, 10, true), APInt(32, 20, true)});
93 // Check satisfiesPredicate with distinct integer ranges.
94 EXPECT_TRUE(LV1.satisfiesPredicate(CmpInst::ICMP_SLT, LV2));
95 EXPECT_TRUE(LV1.satisfiesPredicate(CmpInst::ICMP_SLE, LV2));
96 EXPECT_TRUE(LV1.satisfiesPredicate(CmpInst::ICMP_NE, LV2));
97 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::ICMP_EQ, LV2));
98 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::ICMP_SGE, LV2));
99 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::ICMP_SGT, LV2));
94 // Check getCompare with distinct integer ranges.
95 EXPECT_TRUE(LV1.getCompare(CmpInst::ICMP_SLT, I1Ty, LV2)->isOneValue());
96 EXPECT_TRUE(LV1.getCompare(CmpInst::ICMP_SLE, I1Ty, LV2)->isOneValue());
97 EXPECT_TRUE(LV1.getCompare(CmpInst::ICMP_NE, I1Ty, LV2)->isOneValue());
98 EXPECT_TRUE(LV1.getCompare(CmpInst::ICMP_EQ, I1Ty, LV2)->isZeroValue());
99 EXPECT_TRUE(LV1.getCompare(CmpInst::ICMP_SGE, I1Ty, LV2)->isZeroValue());
100 EXPECT_TRUE(LV1.getCompare(CmpInst::ICMP_SGT, I1Ty, LV2)->isZeroValue());
100101
101102 auto LV3 =
102103 ValueLatticeElement::getRange({APInt(32, 15, true), APInt(32, 19, true)});
103 // Check satisfiesPredicate with a subset integer ranges.
104 EXPECT_FALSE(LV2.satisfiesPredicate(CmpInst::ICMP_SLT, LV3));
105 EXPECT_FALSE(LV2.satisfiesPredicate(CmpInst::ICMP_SLE, LV3));
106 EXPECT_FALSE(LV2.satisfiesPredicate(CmpInst::ICMP_NE, LV3));
107 EXPECT_FALSE(LV2.satisfiesPredicate(CmpInst::ICMP_EQ, LV3));
108 EXPECT_FALSE(LV2.satisfiesPredicate(CmpInst::ICMP_SGE, LV3));
109 EXPECT_FALSE(LV2.satisfiesPredicate(CmpInst::ICMP_SGT, LV3));
104 // Check getCompare with a subset integer ranges.
105 EXPECT_EQ(LV2.getCompare(CmpInst::ICMP_SLT, I1Ty, LV3), nullptr);
106 EXPECT_EQ(LV2.getCompare(CmpInst::ICMP_SLE, I1Ty, LV3), nullptr);
107 EXPECT_EQ(LV2.getCompare(CmpInst::ICMP_NE, I1Ty, LV3), nullptr);
108 EXPECT_EQ(LV2.getCompare(CmpInst::ICMP_EQ, I1Ty, LV3), nullptr);
109 EXPECT_EQ(LV2.getCompare(CmpInst::ICMP_SGE, I1Ty, LV3), nullptr);
110 EXPECT_EQ(LV2.getCompare(CmpInst::ICMP_SGT, I1Ty, LV3), nullptr);
110111
111112 auto LV4 =
112113 ValueLatticeElement::getRange({APInt(32, 15, true), APInt(32, 25, true)});
113 // Check satisfiesPredicate with overlapping integer ranges.
114 EXPECT_FALSE(LV3.satisfiesPredicate(CmpInst::ICMP_SLT, LV4));
115 EXPECT_FALSE(LV3.satisfiesPredicate(CmpInst::ICMP_SLE, LV4));
116 EXPECT_FALSE(LV3.satisfiesPredicate(CmpInst::ICMP_NE, LV4));
117 EXPECT_FALSE(LV3.satisfiesPredicate(CmpInst::ICMP_EQ, LV4));
118 EXPECT_FALSE(LV3.satisfiesPredicate(CmpInst::ICMP_SGE, LV4));
119 EXPECT_FALSE(LV3.satisfiesPredicate(CmpInst::ICMP_SGT, LV4));
114 // Check getCompare with overlapping integer ranges.
115 EXPECT_EQ(LV3.getCompare(CmpInst::ICMP_SLT, I1Ty, LV4), nullptr);
116 EXPECT_EQ(LV3.getCompare(CmpInst::ICMP_SLE, I1Ty, LV4), nullptr);
117 EXPECT_EQ(LV3.getCompare(CmpInst::ICMP_NE, I1Ty, LV4), nullptr);
118 EXPECT_EQ(LV3.getCompare(CmpInst::ICMP_EQ, I1Ty, LV4), nullptr);
119 EXPECT_EQ(LV3.getCompare(CmpInst::ICMP_SGE, I1Ty, LV4), nullptr);
120 EXPECT_EQ(LV3.getCompare(CmpInst::ICMP_SGT, I1Ty, LV4), nullptr);
120121 }
121122
122 TEST_F(ValueLatticeTest, satisfiesPredicateFloat) {
123 auto FloatTy = IntegerType::getFloatTy(Context);
123 TEST_F(ValueLatticeTest, getCompareFloat) {
124 auto *FloatTy = IntegerType::getFloatTy(Context);
125 auto *I1Ty = IntegerType::get(Context, 1);
124126 auto *C1 = ConstantFP::get(FloatTy, 1.0);
125127 auto LV1 = ValueLatticeElement::get(C1);
126128 auto LV2 = ValueLatticeElement::get(C1);
127129
128 // Check satisfiesPredicate for equal floating point constants.
129 EXPECT_TRUE(LV1.satisfiesPredicate(CmpInst::FCMP_OEQ, LV2));
130 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OGE, LV2));
131 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OLE, LV2));
132 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_ONE, LV2));
133 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OLT, LV2));
134 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OGT, LV2));
130 // Check getCompare for equal floating point constants.
131 EXPECT_TRUE(LV1.getCompare(CmpInst::FCMP_OEQ, I1Ty, LV2)->isOneValue());
132 EXPECT_TRUE(LV1.getCompare(CmpInst::FCMP_OGE, I1Ty, LV2)->isOneValue());
133 EXPECT_TRUE(LV1.getCompare(CmpInst::FCMP_OLE, I1Ty, LV2)->isOneValue());
134 EXPECT_TRUE(LV1.getCompare(CmpInst::FCMP_ONE, I1Ty, LV2)->isZeroValue());
135 EXPECT_TRUE(LV1.getCompare(CmpInst::FCMP_OLT, I1Ty, LV2)->isZeroValue());
136 EXPECT_TRUE(LV1.getCompare(CmpInst::FCMP_OGT, I1Ty, LV2)->isZeroValue());
135137
136138 LV1.mergeIn(ValueLatticeElement::get(ConstantFP::get(FloatTy, 2.2)),
137139 M.getDataLayout());
138 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OEQ, LV2));
139 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OGE, LV2));
140 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OLE, LV2));
141 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_ONE, LV2));
142 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OLT, LV2));
143 EXPECT_FALSE(LV1.satisfiesPredicate(CmpInst::FCMP_OGT, LV2));
140 EXPECT_EQ(LV1.getCompare(CmpInst::FCMP_OEQ, I1Ty, LV2), nullptr);
141 EXPECT_EQ(LV1.getCompare(CmpInst::FCMP_OGE, I1Ty, LV2), nullptr);
142 EXPECT_EQ(LV1.getCompare(CmpInst::FCMP_OLE, I1Ty, LV2), nullptr);
143 EXPECT_EQ(LV1.getCompare(CmpInst::FCMP_ONE, I1Ty, LV2), nullptr);
144 EXPECT_EQ(LV1.getCompare(CmpInst::FCMP_OLT, I1Ty, LV2), nullptr);
145 EXPECT_EQ(LV1.getCompare(CmpInst::FCMP_OGT, I1Ty, LV2), nullptr);
146 }
147
148 TEST_F(ValueLatticeTest, getCompareUndef) {
149 auto *I32Ty = IntegerType::get(Context, 32);
150 auto *I1Ty = IntegerType::get(Context, 1);
151
152 auto LV1 = ValueLatticeElement::get(UndefValue::get(I32Ty));
153 auto LV2 =
154 ValueLatticeElement::getRange({APInt(32, 10, true), APInt(32, 20, true)});
155 EXPECT_TRUE(isa(LV1.getCompare(CmpInst::ICMP_SLT, I1Ty, LV2)));
156 EXPECT_TRUE(isa(LV1.getCompare(CmpInst::ICMP_SLE, I1Ty, LV2)));
157 EXPECT_TRUE(isa(LV1.getCompare(CmpInst::ICMP_NE, I1Ty, LV2)));
158 EXPECT_TRUE(isa(LV1.getCompare(CmpInst::ICMP_EQ, I1Ty, LV2)));
159 EXPECT_TRUE(isa(LV1.getCompare(CmpInst::ICMP_SGE, I1Ty, LV2)));
160 EXPECT_TRUE(isa(LV1.getCompare(CmpInst::ICMP_SGT, I1Ty, LV2)));
161
162 auto *FloatTy = IntegerType::getFloatTy(Context);
163 auto LV3 = ValueLatticeElement::get(ConstantFP::get(FloatTy, 1.0));
164 EXPECT_TRUE(isa(LV1.getCompare(CmpInst::FCMP_OEQ, I1Ty, LV3)));
165 EXPECT_TRUE(isa(LV1.getCompare(CmpInst::FCMP_OGE, I1Ty, LV3)));
166 EXPECT_TRUE(isa(LV1.getCompare(CmpInst::FCMP_OLE, I1Ty, LV3)));
167 EXPECT_TRUE(isa(LV1.getCompare(CmpInst::FCMP_ONE, I1Ty, LV3)));
168 EXPECT_TRUE(isa(LV1.getCompare(CmpInst::FCMP_OLT, I1Ty, LV3)));
169 EXPECT_TRUE(isa(LV1.getCompare(CmpInst::FCMP_OGT, I1Ty, LV3)));
144170 }
145171
146172 } // end anonymous namespace