KnownBitsTest.cpp
6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//===- llvm/unittest/Support/KnownBitsTest.cpp - KnownBits tests ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements unit tests for KnownBits functions.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/KnownBits.h"
#include "KnownBitsTest.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
TEST(KnownBitsTest, AddCarryExhaustive) {
unsigned Bits = 4;
ForeachKnownBits(Bits, [&](const KnownBits &Known1) {
ForeachKnownBits(Bits, [&](const KnownBits &Known2) {
ForeachKnownBits(1, [&](const KnownBits &KnownCarry) {
// Explicitly compute known bits of the addition by trying all
// possibilities.
KnownBits Known(Bits);
Known.Zero.setAllBits();
Known.One.setAllBits();
ForeachNumInKnownBits(Known1, [&](const APInt &N1) {
ForeachNumInKnownBits(Known2, [&](const APInt &N2) {
ForeachNumInKnownBits(KnownCarry, [&](const APInt &Carry) {
APInt Add = N1 + N2;
if (Carry.getBoolValue())
++Add;
Known.One &= Add;
Known.Zero &= ~Add;
});
});
});
KnownBits KnownComputed = KnownBits::computeForAddCarry(
Known1, Known2, KnownCarry);
EXPECT_EQ(Known.Zero, KnownComputed.Zero);
EXPECT_EQ(Known.One, KnownComputed.One);
});
});
});
}
static void TestAddSubExhaustive(bool IsAdd) {
unsigned Bits = 4;
ForeachKnownBits(Bits, [&](const KnownBits &Known1) {
ForeachKnownBits(Bits, [&](const KnownBits &Known2) {
KnownBits Known(Bits), KnownNSW(Bits);
Known.Zero.setAllBits();
Known.One.setAllBits();
KnownNSW.Zero.setAllBits();
KnownNSW.One.setAllBits();
ForeachNumInKnownBits(Known1, [&](const APInt &N1) {
ForeachNumInKnownBits(Known2, [&](const APInt &N2) {
bool Overflow;
APInt Res;
if (IsAdd)
Res = N1.sadd_ov(N2, Overflow);
else
Res = N1.ssub_ov(N2, Overflow);
Known.One &= Res;
Known.Zero &= ~Res;
if (!Overflow) {
KnownNSW.One &= Res;
KnownNSW.Zero &= ~Res;
}
});
});
KnownBits KnownComputed = KnownBits::computeForAddSub(
IsAdd, /*NSW*/false, Known1, Known2);
EXPECT_EQ(Known.Zero, KnownComputed.Zero);
EXPECT_EQ(Known.One, KnownComputed.One);
// The NSW calculation is not precise, only check that it's
// conservatively correct.
KnownBits KnownNSWComputed = KnownBits::computeForAddSub(
IsAdd, /*NSW*/true, Known1, Known2);
EXPECT_TRUE(KnownNSWComputed.Zero.isSubsetOf(KnownNSW.Zero));
EXPECT_TRUE(KnownNSWComputed.One.isSubsetOf(KnownNSW.One));
});
});
}
TEST(KnownBitsTest, AddSubExhaustive) {
TestAddSubExhaustive(true);
TestAddSubExhaustive(false);
}
TEST(KnownBitsTest, BinaryExhaustive) {
unsigned Bits = 4;
ForeachKnownBits(Bits, [&](const KnownBits &Known1) {
ForeachKnownBits(Bits, [&](const KnownBits &Known2) {
KnownBits KnownAnd(Bits);
KnownAnd.Zero.setAllBits();
KnownAnd.One.setAllBits();
KnownBits KnownOr(KnownAnd);
KnownBits KnownXor(KnownAnd);
KnownBits KnownUMax(KnownAnd);
KnownBits KnownUMin(KnownAnd);
KnownBits KnownSMax(KnownAnd);
KnownBits KnownSMin(KnownAnd);
ForeachNumInKnownBits(Known1, [&](const APInt &N1) {
ForeachNumInKnownBits(Known2, [&](const APInt &N2) {
APInt Res;
Res = N1 & N2;
KnownAnd.One &= Res;
KnownAnd.Zero &= ~Res;
Res = N1 | N2;
KnownOr.One &= Res;
KnownOr.Zero &= ~Res;
Res = N1 ^ N2;
KnownXor.One &= Res;
KnownXor.Zero &= ~Res;
Res = APIntOps::umax(N1, N2);
KnownUMax.One &= Res;
KnownUMax.Zero &= ~Res;
Res = APIntOps::umin(N1, N2);
KnownUMin.One &= Res;
KnownUMin.Zero &= ~Res;
Res = APIntOps::smax(N1, N2);
KnownSMax.One &= Res;
KnownSMax.Zero &= ~Res;
Res = APIntOps::smin(N1, N2);
KnownSMin.One &= Res;
KnownSMin.Zero &= ~Res;
});
});
KnownBits ComputedAnd = Known1 & Known2;
EXPECT_EQ(KnownAnd.Zero, ComputedAnd.Zero);
EXPECT_EQ(KnownAnd.One, ComputedAnd.One);
KnownBits ComputedOr = Known1 | Known2;
EXPECT_EQ(KnownOr.Zero, ComputedOr.Zero);
EXPECT_EQ(KnownOr.One, ComputedOr.One);
KnownBits ComputedXor = Known1 ^ Known2;
EXPECT_EQ(KnownXor.Zero, ComputedXor.Zero);
EXPECT_EQ(KnownXor.One, ComputedXor.One);
KnownBits ComputedUMax = KnownBits::umax(Known1, Known2);
EXPECT_EQ(KnownUMax.Zero, ComputedUMax.Zero);
EXPECT_EQ(KnownUMax.One, ComputedUMax.One);
KnownBits ComputedUMin = KnownBits::umin(Known1, Known2);
EXPECT_EQ(KnownUMin.Zero, ComputedUMin.Zero);
EXPECT_EQ(KnownUMin.One, ComputedUMin.One);
KnownBits ComputedSMax = KnownBits::smax(Known1, Known2);
EXPECT_EQ(KnownSMax.Zero, ComputedSMax.Zero);
EXPECT_EQ(KnownSMax.One, ComputedSMax.One);
KnownBits ComputedSMin = KnownBits::smin(Known1, Known2);
EXPECT_EQ(KnownSMin.Zero, ComputedSMin.Zero);
EXPECT_EQ(KnownSMin.One, ComputedSMin.One);
});
});
}
TEST(KnownBitsTest, GetMinMaxVal) {
unsigned Bits = 4;
ForeachKnownBits(Bits, [&](const KnownBits &Known) {
APInt Min = APInt::getMaxValue(Bits);
APInt Max = APInt::getMinValue(Bits);
ForeachNumInKnownBits(Known, [&](const APInt &N) {
Min = APIntOps::umin(Min, N);
Max = APIntOps::umax(Max, N);
});
EXPECT_EQ(Min, Known.getMinValue());
EXPECT_EQ(Max, Known.getMaxValue());
});
}
} // end anonymous namespace