llvm.org GIT mirror llvm / 3a4a884
Replace intersectWith with maximalIntersectWith. The latter guarantees that all values belonging to the intersection will belong to the resulting range. The former was inconsistent about that point (either way is fine, just pick one.) This is part of PR4545. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76289 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 10 years ago
5 changed file(s) with 32 addition(s) and 88 deletion(s). Raw diff Collapse all Expand all
152152 ConstantRange subtract(const APInt &CI) const;
153153
154154 /// intersectWith - Return the range that results from the intersection of
155 /// this range with another range. The resultant range is pruned as much as
156 /// possible, but there may be cases where elements are included that are in
157 /// one of the sets but not the other. For example: [100, 8) intersect [3,
158 /// 120) yields [3, 120)
159 ///
160 ConstantRange intersectWith(const ConstantRange &CR) const;
161
162 /// maximalIntersectWith - Return the range that results from the intersection
163 /// of this range with another range. The resultant range is guaranteed to
155 /// this range with another range. The resultant range is guaranteed to
164156 /// include all elements contained in both input ranges, and to have the
165157 /// smallest possible set size that does so. Because there may be two
166 /// intersections with the same set size, A.maximalIntersectWith(B) might not
167 /// be equal to B.maximalIntersectWith(A).
168 ///
169 ConstantRange maximalIntersectWith(const ConstantRange &CR) const;
158 /// intersections with the same set size, A.intersectWith(B) might not
159 /// be equal to B.intersectWith(A).
160 ///
161 ConstantRange intersectWith(const ConstantRange &CR) const;
170162
171163 /// unionWith - Return the range that results from the union of this range
172164 /// with another range. The resultant range is guaranteed to include the
141141
142142 if (R.getUnsignedMin() == 0) {
143143 // Just because it contains zero, doesn't mean it will also contain one.
144 // Use maximalIntersectWith to get the right behaviour.
145144 ConstantRange NotZero(APInt(L.getBitWidth(), 1),
146145 APInt::getNullValue(L.getBitWidth()));
147 R = R.maximalIntersectWith(NotZero);
146 R = R.intersectWith(NotZero);
148147 }
149148
150 // But, the maximal intersection might still include zero. If it does, then
151 // we know it also included one.
149 // But, the intersection might still include zero. If it does, then we know
150 // it also included one.
152151 if (R.contains(APInt::getNullValue(L.getBitWidth())))
153152 Upper = L.getUnsignedMax();
154153 else
294293 if (I == Map.end())
295294 Map[V] = new ConstantRange(CR);
296295 else
297 Map[V] = new ConstantRange(Map[V]->maximalIntersectWith(CR));
298 }
296 Map[V] = new ConstantRange(Map[V]->intersectWith(CR));
297 }
274274 }
275275
276276 /// intersectWith - Return the range that results from the intersection of this
277 /// range with another range.
278 ///
277 /// range with another range. The resultant range is guaranteed to include all
278 /// elements contained in both input ranges, and to have the smallest possible
279 /// set size that does so. Because there may be two intersections with the
280 /// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
279281 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
280282 assert(getBitWidth() == CR.getBitWidth() &&
281283 "ConstantRange types don't agree!");
282 // Handle common special cases
283 if (isEmptySet() || CR.isFullSet())
284 return *this;
285 if (isFullSet() || CR.isEmptySet())
286 return CR;
287
288 if (!isWrappedSet()) {
289 if (!CR.isWrappedSet()) {
290 APInt L = APIntOps::umax(Lower, CR.Lower);
291 APInt U = APIntOps::umin(Upper, CR.Upper);
292
293 if (L.ult(U)) // If range isn't empty...
294 return ConstantRange(L, U);
295 else
296 return ConstantRange(getBitWidth(), false);// Otherwise, empty set
297 } else
298 return intersect1Wrapped(CR, *this);
299 } else { // We know "this" is wrapped...
300 if (!CR.isWrappedSet())
301 return intersect1Wrapped(*this, CR);
302 else {
303 // Both ranges are wrapped...
304 APInt L = APIntOps::umax(Lower, CR.Lower);
305 APInt U = APIntOps::umin(Upper, CR.Upper);
306 return ConstantRange(L, U);
307 }
308 }
309 return *this;
310 }
311
312 /// maximalIntersectWith - Return the range that results from the intersection
313 /// of this range with another range. The resultant range is guaranteed to
314 /// include all elements contained in both input ranges, and to have the
315 /// smallest possible set size that does so. Because there may be two
316 /// intersections with the same set size, A.maximalIntersectWith(B) might not
317 /// be equal to B.maximalIntersect(A).
318 ConstantRange
319 ConstantRange::maximalIntersectWith(const ConstantRange &CR) const {
320 assert(getBitWidth() == CR.getBitWidth() &&
321 "ConstantRange types don't agree!");
322284
323285 // Handle common cases.
324286 if ( isEmptySet() || CR.isFullSet()) return *this;
325287 if (CR.isEmptySet() || isFullSet()) return CR;
326288
327289 if (!isWrappedSet() && CR.isWrappedSet())
328 return CR.maximalIntersectWith(*this);
290 return CR.intersectWith(*this);
329291
330292 if (!isWrappedSet() && !CR.isWrappedSet()) {
331293 if (Lower.ult(CR.Lower)) {
967967 std::lower_bound(begin(), E, std::make_pair(Subtree, empty), swo);
968968
969969 if (I != end() && I->first == Subtree) {
970 ConstantRange CR2 = I->second.maximalIntersectWith(CR);
970 ConstantRange CR2 = I->second.intersectWith(CR);
971971 assert(!CR2.isEmptySet() && !CR2.isSingleElement() &&
972972 "Invalid union of ranges.");
973973 I->second = CR2;
999999 ConstantRange Range(CR.getBitWidth());
10001000
10011001 if (LV_s == SGT_BIT) {
1002 Range = Range.maximalIntersectWith(ConstantRange::makeICmpRegion(
1002 Range = Range.intersectWith(ConstantRange::makeICmpRegion(
10031003 hasEQ ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_SGT, CR));
10041004 } else if (LV_s == SLT_BIT) {
1005 Range = Range.maximalIntersectWith(ConstantRange::makeICmpRegion(
1005 Range = Range.intersectWith(ConstantRange::makeICmpRegion(
10061006 hasEQ ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_SLT, CR));
10071007 }
10081008
10091009 if (LV_u == UGT_BIT) {
1010 Range = Range.maximalIntersectWith(ConstantRange::makeICmpRegion(
1010 Range = Range.intersectWith(ConstantRange::makeICmpRegion(
10111011 hasEQ ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_UGT, CR));
10121012 } else if (LV_u == ULT_BIT) {
1013 Range = Range.maximalIntersectWith(ConstantRange::makeICmpRegion(
1013 Range = Range.intersectWith(ConstantRange::makeICmpRegion(
10141014 hasEQ ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_ULT, CR));
10151015 }
10161016
10821082 switch (LV) {
10831083 default: assert(!"Impossible lattice value!");
10841084 case NE:
1085 return CR1.maximalIntersectWith(CR2).isEmptySet();
1085 return CR1.intersectWith(CR2).isEmptySet();
10861086 case ULT:
10871087 return CR1.getUnsignedMax().ult(CR2.getUnsignedMin());
10881088 case ULE:
11481148 unsigned i = VN.valueNumber(*I, Subtree);
11491149 ConstantRange CR_Kill = i ? range(i, Subtree) : range(*I);
11501150 if (CR_Kill.isFullSet()) continue;
1151 Merged = Merged.maximalIntersectWith(CR_Kill);
1151 Merged = Merged.intersectWith(CR_Kill);
11521152 }
11531153
11541154 if (Merged.isFullSet() || Merged == CR_New) return;
11581158
11591159 void applyRange(unsigned n, const ConstantRange &CR,
11601160 DomTreeDFS::Node *Subtree, VRPSolver *VRP) {
1161 ConstantRange Merged = CR.maximalIntersectWith(range(n, Subtree));
1161 ConstantRange Merged = CR.intersectWith(range(n, Subtree));
11621162 if (Merged.isEmptySet()) {
11631163 markBlock(VRP);
11641164 return;
12481248 ConstantRange CR2 = range(n2, Subtree);
12491249
12501250 if (!CR1.isSingleElement()) {
1251 ConstantRange NewCR1 = CR1.maximalIntersectWith(create(LV, CR2));
1251 ConstantRange NewCR1 = CR1.intersectWith(create(LV, CR2));
12521252 if (NewCR1 != CR1)
12531253 applyRange(n1, NewCR1, Subtree, VRP);
12541254 }
12551255
12561256 if (!CR2.isSingleElement()) {
1257 ConstantRange NewCR2 = CR2.maximalIntersectWith(
1257 ConstantRange NewCR2 = CR2.intersectWith(
12581258 create(reversePredicate(LV), CR1));
12591259 if (NewCR2 != CR2)
12601260 applyRange(n2, NewCR2, Subtree, VRP);
202202 EXPECT_TRUE(Some.intersectWith(Wrap).isEmptySet());
203203 EXPECT_TRUE(One.intersectWith(Wrap).isEmptySet());
204204 EXPECT_EQ(One.intersectWith(Wrap), Wrap.intersectWith(One));
205 }
206
207 TEST_F(ConstantRangeTest, MaximalIntersectWith) {
208 EXPECT_TRUE(Empty.maximalIntersectWith(Full).isEmptySet());
209 EXPECT_TRUE(Empty.maximalIntersectWith(Empty).isEmptySet());
210 EXPECT_TRUE(Empty.maximalIntersectWith(One).isEmptySet());
211 EXPECT_TRUE(Empty.maximalIntersectWith(Some).isEmptySet());
212 EXPECT_TRUE(Empty.maximalIntersectWith(Wrap).isEmptySet());
213 EXPECT_TRUE(Full.maximalIntersectWith(Full).isFullSet());
214 EXPECT_TRUE(Some.maximalIntersectWith(Some) == Some);
215 EXPECT_TRUE(Some.maximalIntersectWith(One) == One);
216 EXPECT_TRUE(Full.maximalIntersectWith(One) == One);
217 EXPECT_TRUE(Full.maximalIntersectWith(Some) == Some);
218 EXPECT_TRUE(Some.maximalIntersectWith(Wrap).isEmptySet());
219 EXPECT_TRUE(One.maximalIntersectWith(Wrap).isEmptySet());
220 EXPECT_EQ(One.maximalIntersectWith(Wrap), Wrap.maximalIntersectWith(One));
205
206 // Klee generated testcase from PR4545.
207 // The intersection of i16 [4, 2) and [6, 5) is disjoint, looking like
208 // 01..4.6789ABCDEF where the dots represent values not in the intersection.
209 ConstantRange LHS(APInt(16, 4), APInt(16, 2));
210 ConstantRange RHS(APInt(16, 6), APInt(16, 5));
211 EXPECT_EQ(LHS.intersectWith(RHS), LHS);
221212 }
222213
223214 TEST_F(ConstantRangeTest, UnionWith) {