10
10
//
11
11
// ===----------------------------------------------------------------------===//
12
12
13
- #include " llvm/ADT/ArrayRef.h"
14
13
#include " llvm/Support/KnownBits.h"
15
14
#include " KnownBitsTest.h"
15
+ #include " llvm/ADT/ArrayRef.h"
16
+ #include " llvm/ADT/StringRef.h"
17
+ #include " llvm/ADT/Twine.h"
16
18
#include " gtest/gtest.h"
17
19
18
20
using namespace llvm ;
@@ -25,7 +27,7 @@ using BinaryBitsFn =
25
27
using BinaryIntFn =
26
28
llvm::function_ref<std::optional<APInt>(const APInt &, const APInt &)>;
27
29
28
- static testing::AssertionResult checkResult (const KnownBits &Exact,
30
+ static testing::AssertionResult checkResult (Twine Name, const KnownBits &Exact,
29
31
const KnownBits &Computed,
30
32
ArrayRef<KnownBits> Inputs,
31
33
bool CheckOptimality) {
@@ -41,14 +43,16 @@ static testing::AssertionResult checkResult(const KnownBits &Exact,
41
43
}
42
44
43
45
testing::AssertionResult Result = testing::AssertionFailure ();
46
+ Result << Name << " : " ;
44
47
Result << " Inputs = " ;
45
48
for (const KnownBits &Input : Inputs)
46
49
Result << Input << " , " ;
47
50
Result << " Computed = " << Computed << " , Exact = " << Exact;
48
51
return Result;
49
52
}
50
53
51
- static void testUnaryOpExhaustive (UnaryBitsFn BitsFn, UnaryIntFn IntFn,
54
+ static void testUnaryOpExhaustive (StringRef Name, UnaryBitsFn BitsFn,
55
+ UnaryIntFn IntFn,
52
56
bool CheckOptimality = true ) {
53
57
for (unsigned Bits : {1 , 4 }) {
54
58
ForeachKnownBits (Bits, [&](const KnownBits &Known) {
@@ -65,12 +69,13 @@ static void testUnaryOpExhaustive(UnaryBitsFn BitsFn, UnaryIntFn IntFn,
65
69
});
66
70
67
71
EXPECT_TRUE (!Computed.hasConflict ());
68
- EXPECT_TRUE (checkResult (Exact, Computed, Known, CheckOptimality));
72
+ EXPECT_TRUE (checkResult (Name, Exact, Computed, Known, CheckOptimality));
69
73
});
70
74
}
71
75
}
72
76
73
- static void testBinaryOpExhaustive (BinaryBitsFn BitsFn, BinaryIntFn IntFn,
77
+ static void testBinaryOpExhaustive (StringRef Name, BinaryBitsFn BitsFn,
78
+ BinaryIntFn IntFn,
74
79
bool CheckOptimality = true ,
75
80
bool RefinePoisonToZero = false ) {
76
81
for (unsigned Bits : {1 , 4 }) {
@@ -91,8 +96,8 @@ static void testBinaryOpExhaustive(BinaryBitsFn BitsFn, BinaryIntFn IntFn,
91
96
});
92
97
93
98
EXPECT_TRUE (!Computed.hasConflict ());
94
- EXPECT_TRUE (
95
- checkResult (Exact, Computed, {Known1, Known2}, CheckOptimality));
99
+ EXPECT_TRUE (checkResult (Name, Exact, Computed, {Known1, Known2},
100
+ CheckOptimality));
96
101
// In some cases we choose to return zero if the result is always
97
102
// poison.
98
103
if (RefinePoisonToZero && Exact.hasConflict ()) {
@@ -137,6 +142,7 @@ TEST(KnownBitsTest, AddCarryExhaustive) {
137
142
}
138
143
139
144
static void TestAddSubExhaustive (bool IsAdd) {
145
+ Twine Name = IsAdd ? " add" : " sub" ;
140
146
unsigned Bits = 4 ;
141
147
ForeachKnownBits (Bits, [&](const KnownBits &Known1) {
142
148
ForeachKnownBits (Bits, [&](const KnownBits &Known2) {
@@ -186,23 +192,26 @@ static void TestAddSubExhaustive(bool IsAdd) {
186
192
187
193
KnownBits KnownComputed = KnownBits::computeForAddSub (
188
194
IsAdd, /* NSW=*/ false , /* NUW=*/ false , Known1, Known2);
189
- EXPECT_TRUE (checkResult (Known, KnownComputed, {Known1, Known2},
195
+ EXPECT_TRUE (checkResult (Name, Known, KnownComputed, {Known1, Known2},
190
196
/* CheckOptimality=*/ true ));
191
197
192
198
KnownBits KnownNSWComputed = KnownBits::computeForAddSub (
193
199
IsAdd, /* NSW=*/ true , /* NUW=*/ false , Known1, Known2);
194
- EXPECT_TRUE (checkResult (KnownNSW, KnownNSWComputed, {Known1, Known2},
200
+ EXPECT_TRUE (checkResult (Name + " nsw" , KnownNSW, KnownNSWComputed,
201
+ {Known1, Known2},
195
202
/* CheckOptimality=*/ true ));
196
203
197
204
KnownBits KnownNUWComputed = KnownBits::computeForAddSub (
198
205
IsAdd, /* NSW=*/ false , /* NUW=*/ true , Known1, Known2);
199
- EXPECT_TRUE (checkResult (KnownNUW, KnownNUWComputed, {Known1, Known2},
206
+ EXPECT_TRUE (checkResult (Name + " nuw" , KnownNUW, KnownNUWComputed,
207
+ {Known1, Known2},
200
208
/* CheckOptimality=*/ true ));
201
209
202
210
KnownBits KnownNSWAndNUWComputed = KnownBits::computeForAddSub (
203
211
IsAdd, /* NSW=*/ true , /* NUW=*/ true , Known1, Known2);
204
- EXPECT_TRUE (checkResult (KnownNSWAndNUW, KnownNSWAndNUWComputed,
205
- {Known1, Known2}, /* CheckOptimality=*/ true ));
212
+ EXPECT_TRUE (checkResult (Name + " nsw nuw" , KnownNSWAndNUW,
213
+ KnownNSWAndNUWComputed, {Known1, Known2},
214
+ /* CheckOptimality=*/ true ));
206
215
});
207
216
});
208
217
}
@@ -267,27 +276,31 @@ TEST(KnownBitsTest, SignBitUnknown) {
267
276
268
277
TEST (KnownBitsTest, BinaryExhaustive) {
269
278
testBinaryOpExhaustive (
279
+ " and" ,
270
280
[](const KnownBits &Known1, const KnownBits &Known2) {
271
281
return Known1 & Known2;
272
282
},
273
283
[](const APInt &N1, const APInt &N2) { return N1 & N2; });
274
284
testBinaryOpExhaustive (
285
+ " or" ,
275
286
[](const KnownBits &Known1, const KnownBits &Known2) {
276
287
return Known1 | Known2;
277
288
},
278
289
[](const APInt &N1, const APInt &N2) { return N1 | N2; });
279
290
testBinaryOpExhaustive (
291
+ " xor" ,
280
292
[](const KnownBits &Known1, const KnownBits &Known2) {
281
293
return Known1 ^ Known2;
282
294
},
283
295
[](const APInt &N1, const APInt &N2) { return N1 ^ N2; });
284
- testBinaryOpExhaustive (KnownBits::umax, APIntOps::umax);
285
- testBinaryOpExhaustive (KnownBits::umin, APIntOps::umin);
286
- testBinaryOpExhaustive (KnownBits::smax, APIntOps::smax);
287
- testBinaryOpExhaustive (KnownBits::smin, APIntOps::smin);
288
- testBinaryOpExhaustive (KnownBits::abdu, APIntOps::abdu);
289
- testBinaryOpExhaustive (KnownBits::abds, APIntOps::abds);
296
+ testBinaryOpExhaustive (" umax " , KnownBits::umax, APIntOps::umax);
297
+ testBinaryOpExhaustive (" umin " , KnownBits::umin, APIntOps::umin);
298
+ testBinaryOpExhaustive (" smax " , KnownBits::smax, APIntOps::smax);
299
+ testBinaryOpExhaustive (" smin " , KnownBits::smin, APIntOps::smin);
300
+ testBinaryOpExhaustive (" abdu " , KnownBits::abdu, APIntOps::abdu);
301
+ testBinaryOpExhaustive (" abds " , KnownBits::abds, APIntOps::abds);
290
302
testBinaryOpExhaustive (
303
+ " udiv" ,
291
304
[](const KnownBits &Known1, const KnownBits &Known2) {
292
305
return KnownBits::udiv (Known1, Known2);
293
306
},
@@ -298,6 +311,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
298
311
},
299
312
/* CheckOptimality=*/ false );
300
313
testBinaryOpExhaustive (
314
+ " udiv exact" ,
301
315
[](const KnownBits &Known1, const KnownBits &Known2) {
302
316
return KnownBits::udiv (Known1, Known2, /* Exact*/ true );
303
317
},
@@ -308,6 +322,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
308
322
},
309
323
/* CheckOptimality=*/ false );
310
324
testBinaryOpExhaustive (
325
+ " sdiv" ,
311
326
[](const KnownBits &Known1, const KnownBits &Known2) {
312
327
return KnownBits::sdiv (Known1, Known2);
313
328
},
@@ -318,6 +333,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
318
333
},
319
334
/* CheckOptimality=*/ false );
320
335
testBinaryOpExhaustive (
336
+ " sdiv exact" ,
321
337
[](const KnownBits &Known1, const KnownBits &Known2) {
322
338
return KnownBits::sdiv (Known1, Known2, /* Exact*/ true );
323
339
},
@@ -329,46 +345,47 @@ TEST(KnownBitsTest, BinaryExhaustive) {
329
345
},
330
346
/* CheckOptimality=*/ false );
331
347
testBinaryOpExhaustive (
332
- KnownBits::urem,
348
+ " urem " , KnownBits::urem,
333
349
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
334
350
if (N2.isZero ())
335
351
return std::nullopt;
336
352
return N1.urem (N2);
337
353
},
338
354
/* CheckOptimality=*/ false );
339
355
testBinaryOpExhaustive (
340
- KnownBits::srem,
356
+ " srem " , KnownBits::srem,
341
357
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
342
358
if (N2.isZero ())
343
359
return std::nullopt;
344
360
return N1.srem (N2);
345
361
},
346
362
/* CheckOptimality=*/ false );
347
363
testBinaryOpExhaustive (
348
- KnownBits::sadd_sat,
364
+ " sadd_sat " , KnownBits::sadd_sat,
349
365
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
350
366
return N1.sadd_sat (N2);
351
367
},
352
368
/* CheckOptimality=*/ false );
353
369
testBinaryOpExhaustive (
354
- KnownBits::uadd_sat,
370
+ " uadd_sat " , KnownBits::uadd_sat,
355
371
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
356
372
return N1.uadd_sat (N2);
357
373
},
358
374
/* CheckOptimality=*/ false );
359
375
testBinaryOpExhaustive (
360
- KnownBits::ssub_sat,
376
+ " ssub_sat " , KnownBits::ssub_sat,
361
377
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
362
378
return N1.ssub_sat (N2);
363
379
},
364
380
/* CheckOptimality=*/ false );
365
381
testBinaryOpExhaustive (
366
- KnownBits::usub_sat,
382
+ " usub_sat " , KnownBits::usub_sat,
367
383
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
368
384
return N1.usub_sat (N2);
369
385
},
370
386
/* CheckOptimality=*/ false );
371
387
testBinaryOpExhaustive (
388
+ " shl" ,
372
389
[](const KnownBits &Known1, const KnownBits &Known2) {
373
390
return KnownBits::shl (Known1, Known2);
374
391
},
@@ -379,6 +396,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
379
396
},
380
397
/* CheckOptimality=*/ true , /* RefinePoisonToZero */ true );
381
398
testBinaryOpExhaustive (
399
+ " ushl_ov" ,
382
400
[](const KnownBits &Known1, const KnownBits &Known2) {
383
401
return KnownBits::shl (Known1, Known2, /* NUW */ true );
384
402
},
@@ -391,6 +409,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
391
409
},
392
410
/* CheckOptimality=*/ true , /* RefinePoisonToZero */ true );
393
411
testBinaryOpExhaustive (
412
+ " shl nsw" ,
394
413
[](const KnownBits &Known1, const KnownBits &Known2) {
395
414
return KnownBits::shl (Known1, Known2, /* NUW */ false , /* NSW */ true );
396
415
},
@@ -403,6 +422,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
403
422
},
404
423
/* CheckOptimality=*/ true , /* RefinePoisonToZero */ true );
405
424
testBinaryOpExhaustive (
425
+ " shl nuw" ,
406
426
[](const KnownBits &Known1, const KnownBits &Known2) {
407
427
return KnownBits::shl (Known1, Known2, /* NUW */ true , /* NSW */ true );
408
428
},
@@ -417,6 +437,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
417
437
/* CheckOptimality=*/ true , /* RefinePoisonToZero */ true );
418
438
419
439
testBinaryOpExhaustive (
440
+ " lshr" ,
420
441
[](const KnownBits &Known1, const KnownBits &Known2) {
421
442
return KnownBits::lshr (Known1, Known2);
422
443
},
@@ -427,6 +448,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
427
448
},
428
449
/* CheckOptimality=*/ true , /* RefinePoisonToZero */ true );
429
450
testBinaryOpExhaustive (
451
+ " lshr exact" ,
430
452
[](const KnownBits &Known1, const KnownBits &Known2) {
431
453
return KnownBits::lshr (Known1, Known2, /* ShAmtNonZero=*/ false ,
432
454
/* Exact=*/ true );
@@ -440,6 +462,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
440
462
},
441
463
/* CheckOptimality=*/ true , /* RefinePoisonToZero */ true );
442
464
testBinaryOpExhaustive (
465
+ " ashr" ,
443
466
[](const KnownBits &Known1, const KnownBits &Known2) {
444
467
return KnownBits::ashr (Known1, Known2);
445
468
},
@@ -450,6 +473,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
450
473
},
451
474
/* CheckOptimality=*/ true , /* RefinePoisonToZero */ true );
452
475
testBinaryOpExhaustive (
476
+ " ashr exact" ,
453
477
[](const KnownBits &Known1, const KnownBits &Known2) {
454
478
return KnownBits::ashr (Known1, Known2, /* ShAmtNonZero=*/ false ,
455
479
/* Exact=*/ true );
@@ -463,38 +487,44 @@ TEST(KnownBitsTest, BinaryExhaustive) {
463
487
},
464
488
/* CheckOptimality=*/ true , /* RefinePoisonToZero */ true );
465
489
testBinaryOpExhaustive (
490
+ " mul" ,
466
491
[](const KnownBits &Known1, const KnownBits &Known2) {
467
492
return KnownBits::mul (Known1, Known2);
468
493
},
469
494
[](const APInt &N1, const APInt &N2) { return N1 * N2; },
470
495
/* CheckOptimality=*/ false );
471
496
testBinaryOpExhaustive (
472
- KnownBits::mulhs,
497
+ " mulhs " , KnownBits::mulhs,
473
498
[](const APInt &N1, const APInt &N2) { return APIntOps::mulhs (N1, N2); },
474
499
/* CheckOptimality=*/ false );
475
500
testBinaryOpExhaustive (
476
- KnownBits::mulhu,
501
+ " mulhu " , KnownBits::mulhu,
477
502
[](const APInt &N1, const APInt &N2) { return APIntOps::mulhu (N1, N2); },
478
503
/* CheckOptimality=*/ false );
479
504
}
480
505
481
506
TEST (KnownBitsTest, UnaryExhaustive) {
482
- testUnaryOpExhaustive ([](const KnownBits &Known) { return Known.abs (); },
483
- [](const APInt &N) { return N.abs (); });
507
+ testUnaryOpExhaustive (
508
+ " abs" , [](const KnownBits &Known) { return Known.abs (); },
509
+ [](const APInt &N) { return N.abs (); });
484
510
485
- testUnaryOpExhaustive ([](const KnownBits &Known) { return Known.abs (true ); },
486
- [](const APInt &N) -> std::optional<APInt> {
487
- if (N.isMinSignedValue ())
488
- return std::nullopt;
489
- return N.abs ();
490
- });
511
+ testUnaryOpExhaustive (
512
+ " abs(true)" , [](const KnownBits &Known) { return Known.abs (true ); },
513
+ [](const APInt &N) -> std::optional<APInt> {
514
+ if (N.isMinSignedValue ())
515
+ return std::nullopt;
516
+ return N.abs ();
517
+ });
491
518
492
- testUnaryOpExhaustive ([](const KnownBits &Known) { return Known.blsi (); },
493
- [](const APInt &N) { return N & -N; });
494
- testUnaryOpExhaustive ([](const KnownBits &Known) { return Known.blsmsk (); },
495
- [](const APInt &N) { return N ^ (N - 1 ); });
519
+ testUnaryOpExhaustive (
520
+ " blsi" , [](const KnownBits &Known) { return Known.blsi (); },
521
+ [](const APInt &N) { return N & -N; });
522
+ testUnaryOpExhaustive (
523
+ " blsmsk" , [](const KnownBits &Known) { return Known.blsmsk (); },
524
+ [](const APInt &N) { return N ^ (N - 1 ); });
496
525
497
526
testUnaryOpExhaustive (
527
+ " mul self" ,
498
528
[](const KnownBits &Known) {
499
529
return KnownBits::mul (Known, Known, /* SelfMultiply*/ true );
500
530
},
0 commit comments