llvm.org GIT mirror llvm / c16c6e6
[ValueTracking] Make unit tests easier to write; NFC Generalize the existing MatchSelectPatternTest class to also work with other types of tests. This reduces the amount of boilerplate necessary to write ValueTracking tests in general, and computeKnownBits tests in particular. The inherited convention is that the function must be @test and the tested instruction %A. Differential Revision: https://reviews.llvm.org/D55141 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@348043 91177308-0d34-0410-b5e6-96231b3b80d8 Nikita Popov 9 months ago
1 changed file(s) with 69 addition(s) and 112 deletion(s). Raw diff Collapse all Expand all
2121
2222 namespace {
2323
24 class MatchSelectPatternTest : public testing::Test {
24 class ValueTrackingTest : public testing::Test {
2525 protected:
2626 void parseAssembly(const char *Assembly) {
2727 SMDiagnostic Error;
5050 report_fatal_error("@test must have an instruction %A");
5151 }
5252
53 LLVMContext Context;
54 std::unique_ptr M;
55 Instruction *A;
56 };
57
58 class MatchSelectPatternTest : public ValueTrackingTest {
59 protected:
5360 void expectPattern(const SelectPatternResult &P) {
5461 Value *LHS, *RHS;
5562 Instruction::CastOps CastOp;
5865 EXPECT_EQ(P.NaNBehavior, R.NaNBehavior);
5966 EXPECT_EQ(P.Ordered, R.Ordered);
6067 }
61
62 LLVMContext Context;
63 std::unique_ptr M;
64 Instruction *A, *B;
68 };
69
70 class ComputeKnownBitsTest : public ValueTrackingTest {
71 protected:
72 void expectKnownBits(uint64_t Zero, uint64_t One) {
73 auto Known = computeKnownBits(A, M->getDataLayout());
74 ASSERT_FALSE(Known.hasConflict());
75 EXPECT_EQ(Known.One.getZExtValue(), One);
76 EXPECT_EQ(Known.Zero.getZExtValue(), Zero);
77 }
6578 };
6679
6780 }
496509 }
497510 }
498511
499 TEST(ValueTracking, ComputeNumSignBits_PR32045) {
500 StringRef Assembly = "define i32 @f(i32 %a) { "
501 " %val = ashr i32 %a, -1 "
502 " ret i32 %val "
503 "} ";
504
505 LLVMContext Context;
506 SMDiagnostic Error;
507 auto M = parseAssemblyString(Assembly, Error, Context);
508 assert(M && "Bad assembly?");
509
510 auto *F = M->getFunction("f");
511 assert(F && "Bad assembly?");
512
513 auto *RVal =
514 cast(F->getEntryBlock().getTerminator())->getOperand(0);
515 EXPECT_EQ(ComputeNumSignBits(RVal, M->getDataLayout()), 1u);
512 TEST_F(ValueTrackingTest, ComputeNumSignBits_PR32045) {
513 parseAssembly(
514 "define i32 @test(i32 %a) {\n"
515 " %A = ashr i32 %a, -1\n"
516 " ret i32 %A\n"
517 "}\n");
518 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u);
516519 }
517520
518521 // No guarantees for canonical IR in this analysis, so this just bails out.
519 TEST(ValueTracking, ComputeNumSignBits_Shuffle) {
520 StringRef Assembly = "define <2 x i32> @f() { "
521 " %val = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> "
522 " ret <2 x i32> %val "
523 "} ";
524
525 LLVMContext Context;
526 SMDiagnostic Error;
527 auto M = parseAssemblyString(Assembly, Error, Context);
528 assert(M && "Bad assembly?");
529
530 auto *F = M->getFunction("f");
531 assert(F && "Bad assembly?");
532
533 auto *RVal =
534 cast(F->getEntryBlock().getTerminator())->getOperand(0);
535 EXPECT_EQ(ComputeNumSignBits(RVal, M->getDataLayout()), 1u);
522 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle) {
523 parseAssembly(
524 "define <2 x i32> @test() {\n"
525 " %A = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> \n"
526 " ret <2 x i32> %A\n"
527 "}\n");
528 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u);
536529 }
537530
538531 // No guarantees for canonical IR in this analysis, so a shuffle element that
539532 // references an undef value means this can't return any extra information.
540 TEST(ValueTracking, ComputeNumSignBits_Shuffle2) {
541 StringRef Assembly = "define <2 x i32> @f(<2 x i1> %x) { "
542 " %sext = sext <2 x i1> %x to <2 x i32> "
543 " %val = shufflevector <2 x i32> %sext, <2 x i32> undef, <2 x i32> "
544 " ret <2 x i32> %val "
545 "} ";
546
547 LLVMContext Context;
548 SMDiagnostic Error;
549 auto M = parseAssemblyString(Assembly, Error, Context);
550 assert(M && "Bad assembly?");
551
552 auto *F = M->getFunction("f");
553 assert(F && "Bad assembly?");
554
555 auto *RVal =
556 cast(F->getEntryBlock().getTerminator())->getOperand(0);
557 EXPECT_EQ(ComputeNumSignBits(RVal, M->getDataLayout()), 1u);
558 }
559
560 TEST(ValueTracking, ComputeKnownBits) {
561 StringRef Assembly = "define i32 @f(i32 %a, i32 %b) { "
562 " %ash = mul i32 %a, 8 "
563 " %aad = add i32 %ash, 7 "
564 " %aan = and i32 %aad, 4095 "
565 " %bsh = shl i32 %b, 4 "
566 " %bad = or i32 %bsh, 6 "
567 " %ban = and i32 %bad, 4095 "
568 " %mul = mul i32 %aan, %ban "
569 " ret i32 %mul "
570 "} ";
571
572 LLVMContext Context;
573 SMDiagnostic Error;
574 auto M = parseAssemblyString(Assembly, Error, Context);
575 assert(M && "Bad assembly?");
576
577 auto *F = M->getFunction("f");
578 assert(F && "Bad assembly?");
579
580 auto *RVal =
581 cast(F->getEntryBlock().getTerminator())->getOperand(0);
582 auto Known = computeKnownBits(RVal, M->getDataLayout());
583 ASSERT_FALSE(Known.hasConflict());
584 EXPECT_EQ(Known.One.getZExtValue(), 10u);
585 EXPECT_EQ(Known.Zero.getZExtValue(), 4278190085u);
586 }
587
588 TEST(ValueTracking, ComputeKnownMulBits) {
589 StringRef Assembly = "define i32 @f(i32 %a, i32 %b) { "
590 " %aa = shl i32 %a, 5 "
591 " %bb = shl i32 %b, 5 "
592 " %aaa = or i32 %aa, 24 "
593 " %bbb = or i32 %bb, 28 "
594 " %mul = mul i32 %aaa, %bbb "
595 " ret i32 %mul "
596 "} ";
597
598 LLVMContext Context;
599 SMDiagnostic Error;
600 auto M = parseAssemblyString(Assembly, Error, Context);
601 assert(M && "Bad assembly?");
602
603 auto *F = M->getFunction("f");
604 assert(F && "Bad assembly?");
605
606 auto *RVal =
607 cast(F->getEntryBlock().getTerminator())->getOperand(0);
608 auto Known = computeKnownBits(RVal, M->getDataLayout());
609 ASSERT_FALSE(Known.hasConflict());
610 EXPECT_EQ(Known.One.getZExtValue(), 32u);
611 EXPECT_EQ(Known.Zero.getZExtValue(), 95u);
612 }
533 TEST_F(ValueTrackingTest, ComputeNumSignBits_Shuffle2) {
534 parseAssembly(
535 "define <2 x i32> @test(<2 x i1> %x) {\n"
536 " %sext = sext <2 x i1> %x to <2 x i32>\n"
537 " %A = shufflevector <2 x i32> %sext, <2 x i32> undef, <2 x i32> \n"
538 " ret <2 x i32> %A\n"
539 "}\n");
540 EXPECT_EQ(ComputeNumSignBits(A, M->getDataLayout()), 1u);
541 }
542
543 TEST_F(ComputeKnownBitsTest, ComputeKnownBits) {
544 parseAssembly(
545 "define i32 @test(i32 %a, i32 %b) {\n"
546 " %ash = mul i32 %a, 8\n"
547 " %aad = add i32 %ash, 7\n"
548 " %aan = and i32 %aad, 4095\n"
549 " %bsh = shl i32 %b, 4\n"
550 " %bad = or i32 %bsh, 6\n"
551 " %ban = and i32 %bad, 4095\n"
552 " %A = mul i32 %aan, %ban\n"
553 " ret i32 %A\n"
554 "}\n");
555 expectKnownBits(/*zero*/ 4278190085u, /*one*/ 10u);
556 }
557
558 TEST_F(ComputeKnownBitsTest, ComputeKnownMulBits) {
559 parseAssembly(
560 "define i32 @test(i32 %a, i32 %b) {\n"
561 " %aa = shl i32 %a, 5\n"
562 " %bb = shl i32 %b, 5\n"
563 " %aaa = or i32 %aa, 24\n"
564 " %bbb = or i32 %bb, 28\n"
565 " %A = mul i32 %aaa, %bbb\n"
566 " ret i32 %A\n"
567 "}\n");
568 expectKnownBits(/*zero*/ 95u, /*one*/ 32u);
569 }