| 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 |
|
| 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 |
if (Known.hasConflict()) |
| 30 |
continue; |
| 31 |
|
| 32 |
Fn(Known); |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
template <typename FnTy> |
| 38 |
void ForeachNumInKnownBits(const KnownBits &Known, FnTy Fn) { |
| 39 |
unsigned Bits = Known.getBitWidth(); |
| 40 |
unsigned Max = 1 << Bits; |
| 41 |
for (unsigned N = 0; N < Max; ++N) { |
| 42 |
APInt Num(Bits, N); |
| 43 |
if ((Num & Known.Zero) != 0 || (~Num & Known.One) != 0) |
| 44 |
continue; |
| 45 |
|
| 46 |
Fn(Num); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
} // end anonymous namespace |
| 51 |
|
| 52 |
#endif |