1 //===- llvm/unittest/Support/KnownBitsTest.cpp - KnownBits tests ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements unit tests for KnownBits functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Support/KnownBits.h"
14 #include "KnownBitsTest.h"
15 #include "gtest/gtest.h"
16 
17 using namespace llvm;
18 
19 namespace {
20 
TEST(KnownBitsTest,AddCarryExhaustive)21 TEST(KnownBitsTest, AddCarryExhaustive) {
22   unsigned Bits = 4;
23   ForeachKnownBits(Bits, [&](const KnownBits &Known1) {
24     ForeachKnownBits(Bits, [&](const KnownBits &Known2) {
25       ForeachKnownBits(1, [&](const KnownBits &KnownCarry) {
26         // Explicitly compute known bits of the addition by trying all
27         // possibilities.
28         KnownBits Known(Bits);
29         Known.Zero.setAllBits();
30         Known.One.setAllBits();
31         ForeachNumInKnownBits(Known1, [&](const APInt &N1) {
32           ForeachNumInKnownBits(Known2, [&](const APInt &N2) {
33             ForeachNumInKnownBits(KnownCarry, [&](const APInt &Carry) {
34               APInt Add = N1 + N2;
35               if (Carry.getBoolValue())
36                 ++Add;
37 
38               Known.One &= Add;
39               Known.Zero &= ~Add;
40             });
41           });
42         });
43 
44         KnownBits KnownComputed = KnownBits::computeForAddCarry(
45             Known1, Known2, KnownCarry);
46         EXPECT_EQ(Known.Zero, KnownComputed.Zero);
47         EXPECT_EQ(Known.One, KnownComputed.One);
48       });
49     });
50   });
51 }
52 
TestAddSubExhaustive(bool IsAdd)53 static void TestAddSubExhaustive(bool IsAdd) {
54   unsigned Bits = 4;
55   ForeachKnownBits(Bits, [&](const KnownBits &Known1) {
56     ForeachKnownBits(Bits, [&](const KnownBits &Known2) {
57       KnownBits Known(Bits), KnownNSW(Bits);
58       Known.Zero.setAllBits();
59       Known.One.setAllBits();
60       KnownNSW.Zero.setAllBits();
61       KnownNSW.One.setAllBits();
62 
63       ForeachNumInKnownBits(Known1, [&](const APInt &N1) {
64         ForeachNumInKnownBits(Known2, [&](const APInt &N2) {
65           bool Overflow;
66           APInt Res;
67           if (IsAdd)
68             Res = N1.sadd_ov(N2, Overflow);
69           else
70             Res = N1.ssub_ov(N2, Overflow);
71 
72           Known.One &= Res;
73           Known.Zero &= ~Res;
74 
75           if (!Overflow) {
76             KnownNSW.One &= Res;
77             KnownNSW.Zero &= ~Res;
78           }
79         });
80       });
81 
82       KnownBits KnownComputed = KnownBits::computeForAddSub(
83           IsAdd, /*NSW*/false, Known1, Known2);
84       EXPECT_EQ(Known.Zero, KnownComputed.Zero);
85       EXPECT_EQ(Known.One, KnownComputed.One);
86 
87       // The NSW calculation is not precise, only check that it's
88       // conservatively correct.
89       KnownBits KnownNSWComputed = KnownBits::computeForAddSub(
90           IsAdd, /*NSW*/true, Known1, Known2);
91       EXPECT_TRUE(KnownNSWComputed.Zero.isSubsetOf(KnownNSW.Zero));
92       EXPECT_TRUE(KnownNSWComputed.One.isSubsetOf(KnownNSW.One));
93     });
94   });
95 }
96 
TEST(KnownBitsTest,AddSubExhaustive)97 TEST(KnownBitsTest, AddSubExhaustive) {
98   TestAddSubExhaustive(true);
99   TestAddSubExhaustive(false);
100 }
101 
TEST(KnownBitsTest,BinaryExhaustive)102 TEST(KnownBitsTest, BinaryExhaustive) {
103   unsigned Bits = 4;
104   ForeachKnownBits(Bits, [&](const KnownBits &Known1) {
105     ForeachKnownBits(Bits, [&](const KnownBits &Known2) {
106       KnownBits KnownAnd(Bits);
107       KnownAnd.Zero.setAllBits();
108       KnownAnd.One.setAllBits();
109       KnownBits KnownOr(KnownAnd);
110       KnownBits KnownXor(KnownAnd);
111       KnownBits KnownUMax(KnownAnd);
112       KnownBits KnownUMin(KnownAnd);
113       KnownBits KnownSMax(KnownAnd);
114       KnownBits KnownSMin(KnownAnd);
115       KnownBits KnownMul(KnownAnd);
116       KnownBits KnownMulHS(KnownAnd);
117       KnownBits KnownMulHU(KnownAnd);
118       KnownBits KnownUDiv(KnownAnd);
119       KnownBits KnownURem(KnownAnd);
120       KnownBits KnownSRem(KnownAnd);
121       KnownBits KnownShl(KnownAnd);
122       KnownBits KnownLShr(KnownAnd);
123       KnownBits KnownAShr(KnownAnd);
124 
125       ForeachNumInKnownBits(Known1, [&](const APInt &N1) {
126         ForeachNumInKnownBits(Known2, [&](const APInt &N2) {
127           APInt Res;
128 
129           Res = N1 & N2;
130           KnownAnd.One &= Res;
131           KnownAnd.Zero &= ~Res;
132 
133           Res = N1 | N2;
134           KnownOr.One &= Res;
135           KnownOr.Zero &= ~Res;
136 
137           Res = N1 ^ N2;
138           KnownXor.One &= Res;
139           KnownXor.Zero &= ~Res;
140 
141           Res = APIntOps::umax(N1, N2);
142           KnownUMax.One &= Res;
143           KnownUMax.Zero &= ~Res;
144 
145           Res = APIntOps::umin(N1, N2);
146           KnownUMin.One &= Res;
147           KnownUMin.Zero &= ~Res;
148 
149           Res = APIntOps::smax(N1, N2);
150           KnownSMax.One &= Res;
151           KnownSMax.Zero &= ~Res;
152 
153           Res = APIntOps::smin(N1, N2);
154           KnownSMin.One &= Res;
155           KnownSMin.Zero &= ~Res;
156 
157           Res = N1 * N2;
158           KnownMul.One &= Res;
159           KnownMul.Zero &= ~Res;
160 
161           Res = (N1.sext(2 * Bits) * N2.sext(2 * Bits)).extractBits(Bits, Bits);
162           KnownMulHS.One &= Res;
163           KnownMulHS.Zero &= ~Res;
164 
165           Res = (N1.zext(2 * Bits) * N2.zext(2 * Bits)).extractBits(Bits, Bits);
166           KnownMulHU.One &= Res;
167           KnownMulHU.Zero &= ~Res;
168 
169           if (!N2.isZero()) {
170             Res = N1.udiv(N2);
171             KnownUDiv.One &= Res;
172             KnownUDiv.Zero &= ~Res;
173 
174             Res = N1.urem(N2);
175             KnownURem.One &= Res;
176             KnownURem.Zero &= ~Res;
177 
178             Res = N1.srem(N2);
179             KnownSRem.One &= Res;
180             KnownSRem.Zero &= ~Res;
181           }
182 
183           if (N2.ult(1ULL << N1.getBitWidth())) {
184             Res = N1.shl(N2);
185             KnownShl.One &= Res;
186             KnownShl.Zero &= ~Res;
187 
188             Res = N1.lshr(N2);
189             KnownLShr.One &= Res;
190             KnownLShr.Zero &= ~Res;
191 
192             Res = N1.ashr(N2);
193             KnownAShr.One &= Res;
194             KnownAShr.Zero &= ~Res;
195           } else {
196             KnownShl.resetAll();
197             KnownLShr.resetAll();
198             KnownAShr.resetAll();
199           }
200         });
201       });
202 
203       KnownBits ComputedAnd = Known1 & Known2;
204       EXPECT_EQ(KnownAnd.Zero, ComputedAnd.Zero);
205       EXPECT_EQ(KnownAnd.One, ComputedAnd.One);
206 
207       KnownBits ComputedOr = Known1 | Known2;
208       EXPECT_EQ(KnownOr.Zero, ComputedOr.Zero);
209       EXPECT_EQ(KnownOr.One, ComputedOr.One);
210 
211       KnownBits ComputedXor = Known1 ^ Known2;
212       EXPECT_EQ(KnownXor.Zero, ComputedXor.Zero);
213       EXPECT_EQ(KnownXor.One, ComputedXor.One);
214 
215       KnownBits ComputedUMax = KnownBits::umax(Known1, Known2);
216       EXPECT_EQ(KnownUMax.Zero, ComputedUMax.Zero);
217       EXPECT_EQ(KnownUMax.One, ComputedUMax.One);
218 
219       KnownBits ComputedUMin = KnownBits::umin(Known1, Known2);
220       EXPECT_EQ(KnownUMin.Zero, ComputedUMin.Zero);
221       EXPECT_EQ(KnownUMin.One, ComputedUMin.One);
222 
223       KnownBits ComputedSMax = KnownBits::smax(Known1, Known2);
224       EXPECT_EQ(KnownSMax.Zero, ComputedSMax.Zero);
225       EXPECT_EQ(KnownSMax.One, ComputedSMax.One);
226 
227       KnownBits ComputedSMin = KnownBits::smin(Known1, Known2);
228       EXPECT_EQ(KnownSMin.Zero, ComputedSMin.Zero);
229       EXPECT_EQ(KnownSMin.One, ComputedSMin.One);
230 
231       // The following are conservatively correct, but not guaranteed to be
232       // precise.
233       KnownBits ComputedMul = KnownBits::mul(Known1, Known2);
234       EXPECT_TRUE(ComputedMul.Zero.isSubsetOf(KnownMul.Zero));
235       EXPECT_TRUE(ComputedMul.One.isSubsetOf(KnownMul.One));
236 
237       KnownBits ComputedMulHS = KnownBits::mulhs(Known1, Known2);
238       EXPECT_TRUE(ComputedMulHS.Zero.isSubsetOf(KnownMulHS.Zero));
239       EXPECT_TRUE(ComputedMulHS.One.isSubsetOf(KnownMulHS.One));
240 
241       KnownBits ComputedMulHU = KnownBits::mulhu(Known1, Known2);
242       EXPECT_TRUE(ComputedMulHU.Zero.isSubsetOf(KnownMulHU.Zero));
243       EXPECT_TRUE(ComputedMulHU.One.isSubsetOf(KnownMulHU.One));
244 
245       KnownBits ComputedUDiv = KnownBits::udiv(Known1, Known2);
246       EXPECT_TRUE(ComputedUDiv.Zero.isSubsetOf(KnownUDiv.Zero));
247       EXPECT_TRUE(ComputedUDiv.One.isSubsetOf(KnownUDiv.One));
248 
249       KnownBits ComputedURem = KnownBits::urem(Known1, Known2);
250       EXPECT_TRUE(ComputedURem.Zero.isSubsetOf(KnownURem.Zero));
251       EXPECT_TRUE(ComputedURem.One.isSubsetOf(KnownURem.One));
252 
253       KnownBits ComputedSRem = KnownBits::srem(Known1, Known2);
254       EXPECT_TRUE(ComputedSRem.Zero.isSubsetOf(KnownSRem.Zero));
255       EXPECT_TRUE(ComputedSRem.One.isSubsetOf(KnownSRem.One));
256 
257       KnownBits ComputedShl = KnownBits::shl(Known1, Known2);
258       EXPECT_TRUE(ComputedShl.Zero.isSubsetOf(KnownShl.Zero));
259       EXPECT_TRUE(ComputedShl.One.isSubsetOf(KnownShl.One));
260 
261       KnownBits ComputedLShr = KnownBits::lshr(Known1, Known2);
262       EXPECT_TRUE(ComputedLShr.Zero.isSubsetOf(KnownLShr.Zero));
263       EXPECT_TRUE(ComputedLShr.One.isSubsetOf(KnownLShr.One));
264 
265       KnownBits ComputedAShr = KnownBits::ashr(Known1, Known2);
266       EXPECT_TRUE(ComputedAShr.Zero.isSubsetOf(KnownAShr.Zero));
267       EXPECT_TRUE(ComputedAShr.One.isSubsetOf(KnownAShr.One));
268     });
269   });
270 
271   // Also test 'unary' binary cases where the same argument is repeated.
272   ForeachKnownBits(Bits, [&](const KnownBits &Known) {
273     KnownBits KnownMul(Bits);
274     KnownMul.Zero.setAllBits();
275     KnownMul.One.setAllBits();
276 
277     ForeachNumInKnownBits(Known, [&](const APInt &N) {
278       APInt Res = N * N;
279       KnownMul.One &= Res;
280       KnownMul.Zero &= ~Res;
281     });
282 
283     KnownBits ComputedMul = KnownBits::mul(Known, Known, /*SelfMultiply*/ true);
284     EXPECT_TRUE(ComputedMul.Zero.isSubsetOf(KnownMul.Zero));
285     EXPECT_TRUE(ComputedMul.One.isSubsetOf(KnownMul.One));
286   });
287 }
288 
TEST(KnownBitsTest,UnaryExhaustive)289 TEST(KnownBitsTest, UnaryExhaustive) {
290   unsigned Bits = 4;
291   ForeachKnownBits(Bits, [&](const KnownBits &Known) {
292     KnownBits KnownAbs(Bits);
293     KnownAbs.Zero.setAllBits();
294     KnownAbs.One.setAllBits();
295     KnownBits KnownAbsPoison(KnownAbs);
296 
297     ForeachNumInKnownBits(Known, [&](const APInt &N) {
298       APInt Res = N.abs();
299       KnownAbs.One &= Res;
300       KnownAbs.Zero &= ~Res;
301 
302       if (!N.isMinSignedValue()) {
303         KnownAbsPoison.One &= Res;
304         KnownAbsPoison.Zero &= ~Res;
305       }
306     });
307 
308     // abs() is conservatively correct, but not guaranteed to be precise.
309     KnownBits ComputedAbs = Known.abs();
310     EXPECT_TRUE(ComputedAbs.Zero.isSubsetOf(KnownAbs.Zero));
311     EXPECT_TRUE(ComputedAbs.One.isSubsetOf(KnownAbs.One));
312 
313     KnownBits ComputedAbsPoison = Known.abs(true);
314     EXPECT_TRUE(ComputedAbsPoison.Zero.isSubsetOf(KnownAbsPoison.Zero));
315     EXPECT_TRUE(ComputedAbsPoison.One.isSubsetOf(KnownAbsPoison.One));
316   });
317 }
318 
TEST(KnownBitsTest,ICmpExhaustive)319 TEST(KnownBitsTest, ICmpExhaustive) {
320   unsigned Bits = 4;
321   ForeachKnownBits(Bits, [&](const KnownBits &Known1) {
322     ForeachKnownBits(Bits, [&](const KnownBits &Known2) {
323       bool AllEQ = true, NoneEQ = true;
324       bool AllNE = true, NoneNE = true;
325       bool AllUGT = true, NoneUGT = true;
326       bool AllUGE = true, NoneUGE = true;
327       bool AllULT = true, NoneULT = true;
328       bool AllULE = true, NoneULE = true;
329       bool AllSGT = true, NoneSGT = true;
330       bool AllSGE = true, NoneSGE = true;
331       bool AllSLT = true, NoneSLT = true;
332       bool AllSLE = true, NoneSLE = true;
333 
334       ForeachNumInKnownBits(Known1, [&](const APInt &N1) {
335         ForeachNumInKnownBits(Known2, [&](const APInt &N2) {
336           AllEQ &= N1.eq(N2);
337           AllNE &= N1.ne(N2);
338           AllUGT &= N1.ugt(N2);
339           AllUGE &= N1.uge(N2);
340           AllULT &= N1.ult(N2);
341           AllULE &= N1.ule(N2);
342           AllSGT &= N1.sgt(N2);
343           AllSGE &= N1.sge(N2);
344           AllSLT &= N1.slt(N2);
345           AllSLE &= N1.sle(N2);
346           NoneEQ &= !N1.eq(N2);
347           NoneNE &= !N1.ne(N2);
348           NoneUGT &= !N1.ugt(N2);
349           NoneUGE &= !N1.uge(N2);
350           NoneULT &= !N1.ult(N2);
351           NoneULE &= !N1.ule(N2);
352           NoneSGT &= !N1.sgt(N2);
353           NoneSGE &= !N1.sge(N2);
354           NoneSLT &= !N1.slt(N2);
355           NoneSLE &= !N1.sle(N2);
356         });
357       });
358 
359       Optional<bool> KnownEQ = KnownBits::eq(Known1, Known2);
360       Optional<bool> KnownNE = KnownBits::ne(Known1, Known2);
361       Optional<bool> KnownUGT = KnownBits::ugt(Known1, Known2);
362       Optional<bool> KnownUGE = KnownBits::uge(Known1, Known2);
363       Optional<bool> KnownULT = KnownBits::ult(Known1, Known2);
364       Optional<bool> KnownULE = KnownBits::ule(Known1, Known2);
365       Optional<bool> KnownSGT = KnownBits::sgt(Known1, Known2);
366       Optional<bool> KnownSGE = KnownBits::sge(Known1, Known2);
367       Optional<bool> KnownSLT = KnownBits::slt(Known1, Known2);
368       Optional<bool> KnownSLE = KnownBits::sle(Known1, Known2);
369 
370       EXPECT_EQ(AllEQ || NoneEQ, KnownEQ.hasValue());
371       EXPECT_EQ(AllNE || NoneNE, KnownNE.hasValue());
372       EXPECT_EQ(AllUGT || NoneUGT, KnownUGT.hasValue());
373       EXPECT_EQ(AllUGE || NoneUGE, KnownUGE.hasValue());
374       EXPECT_EQ(AllULT || NoneULT, KnownULT.hasValue());
375       EXPECT_EQ(AllULE || NoneULE, KnownULE.hasValue());
376       EXPECT_EQ(AllSGT || NoneSGT, KnownSGT.hasValue());
377       EXPECT_EQ(AllSGE || NoneSGE, KnownSGE.hasValue());
378       EXPECT_EQ(AllSLT || NoneSLT, KnownSLT.hasValue());
379       EXPECT_EQ(AllSLE || NoneSLE, KnownSLE.hasValue());
380 
381       EXPECT_EQ(AllEQ, KnownEQ.hasValue() && KnownEQ.getValue());
382       EXPECT_EQ(AllNE, KnownNE.hasValue() && KnownNE.getValue());
383       EXPECT_EQ(AllUGT, KnownUGT.hasValue() && KnownUGT.getValue());
384       EXPECT_EQ(AllUGE, KnownUGE.hasValue() && KnownUGE.getValue());
385       EXPECT_EQ(AllULT, KnownULT.hasValue() && KnownULT.getValue());
386       EXPECT_EQ(AllULE, KnownULE.hasValue() && KnownULE.getValue());
387       EXPECT_EQ(AllSGT, KnownSGT.hasValue() && KnownSGT.getValue());
388       EXPECT_EQ(AllSGE, KnownSGE.hasValue() && KnownSGE.getValue());
389       EXPECT_EQ(AllSLT, KnownSLT.hasValue() && KnownSLT.getValue());
390       EXPECT_EQ(AllSLE, KnownSLE.hasValue() && KnownSLE.getValue());
391 
392       EXPECT_EQ(NoneEQ, KnownEQ.hasValue() && !KnownEQ.getValue());
393       EXPECT_EQ(NoneNE, KnownNE.hasValue() && !KnownNE.getValue());
394       EXPECT_EQ(NoneUGT, KnownUGT.hasValue() && !KnownUGT.getValue());
395       EXPECT_EQ(NoneUGE, KnownUGE.hasValue() && !KnownUGE.getValue());
396       EXPECT_EQ(NoneULT, KnownULT.hasValue() && !KnownULT.getValue());
397       EXPECT_EQ(NoneULE, KnownULE.hasValue() && !KnownULE.getValue());
398       EXPECT_EQ(NoneSGT, KnownSGT.hasValue() && !KnownSGT.getValue());
399       EXPECT_EQ(NoneSGE, KnownSGE.hasValue() && !KnownSGE.getValue());
400       EXPECT_EQ(NoneSLT, KnownSLT.hasValue() && !KnownSLT.getValue());
401       EXPECT_EQ(NoneSLE, KnownSLE.hasValue() && !KnownSLE.getValue());
402     });
403   });
404 }
405 
TEST(KnownBitsTest,GetMinMaxVal)406 TEST(KnownBitsTest, GetMinMaxVal) {
407   unsigned Bits = 4;
408   ForeachKnownBits(Bits, [&](const KnownBits &Known) {
409     APInt Min = APInt::getMaxValue(Bits);
410     APInt Max = APInt::getMinValue(Bits);
411     ForeachNumInKnownBits(Known, [&](const APInt &N) {
412       Min = APIntOps::umin(Min, N);
413       Max = APIntOps::umax(Max, N);
414     });
415     EXPECT_EQ(Min, Known.getMinValue());
416     EXPECT_EQ(Max, Known.getMaxValue());
417   });
418 }
419 
TEST(KnownBitsTest,GetSignedMinMaxVal)420 TEST(KnownBitsTest, GetSignedMinMaxVal) {
421   unsigned Bits = 4;
422   ForeachKnownBits(Bits, [&](const KnownBits &Known) {
423     APInt Min = APInt::getSignedMaxValue(Bits);
424     APInt Max = APInt::getSignedMinValue(Bits);
425     ForeachNumInKnownBits(Known, [&](const APInt &N) {
426       Min = APIntOps::smin(Min, N);
427       Max = APIntOps::smax(Max, N);
428     });
429     EXPECT_EQ(Min, Known.getSignedMinValue());
430     EXPECT_EQ(Max, Known.getSignedMaxValue());
431   });
432 }
433 
TEST(KnownBitsTest,CountMaxActiveBits)434 TEST(KnownBitsTest, CountMaxActiveBits) {
435   unsigned Bits = 4;
436   ForeachKnownBits(Bits, [&](const KnownBits &Known) {
437     unsigned Expected = 0;
438     ForeachNumInKnownBits(Known, [&](const APInt &N) {
439       Expected = std::max(Expected, N.getActiveBits());
440     });
441     EXPECT_EQ(Expected, Known.countMaxActiveBits());
442   });
443 }
444 
TEST(KnownBitsTest,SExtOrTrunc)445 TEST(KnownBitsTest, SExtOrTrunc) {
446   const unsigned NarrowerSize = 4;
447   const unsigned BaseSize = 6;
448   const unsigned WiderSize = 8;
449   APInt NegativeFitsNarrower(BaseSize, -4, /*isSigned*/ true);
450   APInt NegativeDoesntFitNarrower(BaseSize, -28, /*isSigned*/ true);
451   APInt PositiveFitsNarrower(BaseSize, 14);
452   APInt PositiveDoesntFitNarrower(BaseSize, 36);
453   auto InitKnownBits = [&](KnownBits &Res, const APInt &Input) {
454     Res = KnownBits(Input.getBitWidth());
455     Res.One = Input;
456     Res.Zero = ~Input;
457   };
458 
459   for (unsigned Size : {NarrowerSize, BaseSize, WiderSize}) {
460     for (const APInt &Input :
461          {NegativeFitsNarrower, NegativeDoesntFitNarrower, PositiveFitsNarrower,
462           PositiveDoesntFitNarrower}) {
463       KnownBits Test;
464       InitKnownBits(Test, Input);
465       KnownBits Baseline;
466       InitKnownBits(Baseline, Input.sextOrTrunc(Size));
467       Test = Test.sextOrTrunc(Size);
468       EXPECT_EQ(Test.One, Baseline.One);
469       EXPECT_EQ(Test.Zero, Baseline.Zero);
470     }
471   }
472 }
473 
TEST(KnownBitsTest,SExtInReg)474 TEST(KnownBitsTest, SExtInReg) {
475   unsigned Bits = 4;
476   for (unsigned FromBits = 1; FromBits <= Bits; ++FromBits) {
477     ForeachKnownBits(Bits, [&](const KnownBits &Known) {
478       APInt CommonOne = APInt::getAllOnes(Bits);
479       APInt CommonZero = APInt::getAllOnes(Bits);
480       unsigned ExtBits = Bits - FromBits;
481       ForeachNumInKnownBits(Known, [&](const APInt &N) {
482         APInt Ext = N << ExtBits;
483         Ext.ashrInPlace(ExtBits);
484         CommonOne &= Ext;
485         CommonZero &= ~Ext;
486       });
487       KnownBits KnownSExtInReg = Known.sextInReg(FromBits);
488       EXPECT_EQ(CommonOne, KnownSExtInReg.One);
489       EXPECT_EQ(CommonZero, KnownSExtInReg.Zero);
490     });
491   }
492 }
493 
TEST(KnownBitsTest,CommonBitsSet)494 TEST(KnownBitsTest, CommonBitsSet) {
495   unsigned Bits = 4;
496   ForeachKnownBits(Bits, [&](const KnownBits &Known1) {
497     ForeachKnownBits(Bits, [&](const KnownBits &Known2) {
498       bool HasCommonBitsSet = false;
499       ForeachNumInKnownBits(Known1, [&](const APInt &N1) {
500         ForeachNumInKnownBits(Known2, [&](const APInt &N2) {
501           HasCommonBitsSet |= N1.intersects(N2);
502         });
503       });
504       EXPECT_EQ(!HasCommonBitsSet,
505                 KnownBits::haveNoCommonBitsSet(Known1, Known2));
506     });
507   });
508 }
509 
510 } // end anonymous namespace
511