xref: /llvm-project/llvm/unittests/Support/KnownBitsTest.h (revision ecb9d94a8bfb07591fdfcf3418c5cfafcad5a13d)
1 //===- llvm/unittest/Support/KnownBitsTest.h - 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 helpers for KnownBits and DemandedBits unit tests.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_UNITTESTS_SUPPORT_KNOWNBITSTEST_H
14 #define LLVM_UNITTESTS_SUPPORT_KNOWNBITSTEST_H
15 
16 #include "llvm/Support/KnownBits.h"
17 
18 namespace {
19 
20 using namespace llvm;
21 
ForeachKnownBits(unsigned Bits,FnTy Fn)22 template <typename FnTy> void ForeachKnownBits(unsigned Bits, FnTy Fn) {
23   unsigned Max = 1 << Bits;
24   KnownBits Known(Bits);
25   for (unsigned Zero = 0; Zero < Max; ++Zero) {
26     for (unsigned One = 0; One < Max; ++One) {
27       Known.Zero = Zero;
28       Known.One = One;
29       Fn(Known);
30     }
31   }
32 }
33 
34 template <typename FnTy>
ForeachNumInKnownBits(const KnownBits & Known,FnTy Fn)35 void ForeachNumInKnownBits(const KnownBits &Known, FnTy Fn) {
36   unsigned Bits = Known.getBitWidth();
37   assert(Bits < 32);
38   unsigned Max = 1u << Bits;
39   unsigned Zero = Known.Zero.getZExtValue();
40   unsigned One = Known.One.getZExtValue();
41 
42   if (Zero & One) {
43     // Known has a conflict. No values will satisfy it.
44     return;
45   }
46 
47   for (unsigned N = 0; N < Max; ++N) {
48     if ((N & Zero) == 0 && (~N & One) == 0)
49       Fn(APInt(Bits, N));
50   }
51 }
52 
53 } // end anonymous namespace
54 
55 #endif
56