[NFC] Add unittests for findAllocaForValue

This commit is contained in:
Vitaly Buka 2020-08-27 01:49:15 -07:00
parent 38fc834d2c
commit f918804878

View File

@ -1430,3 +1430,111 @@ TEST_F(ValueTrackingTest, ComputeConstantRange) {
EXPECT_TRUE(CR2.isFullSet());
}
}
class FindAllocaForValueTest
: public ValueTrackingTest,
public ::testing::WithParamInterface<std::pair<const char *, bool>> {
protected:
};
const std::pair<const char *, bool> FindAllocaForValueTests[] = {
{R"(
define void @test() {
%a = alloca i64
%r = bitcast i64* %a to i32*
ret void
})",
true},
{R"(
define void @test() {
%a = alloca i32
%r = getelementptr i32, i32* %a, i32 1
ret void
})",
true},
{R"(
define void @test(i1 %cond) {
entry:
%a = alloca i32
br label %bb1
bb1:
%r = phi i32* [ %a, %entry ], [ %r, %bb1 ]
br i1 %cond, label %bb1, label %exit
exit:
ret void
})",
true},
{R"(
define void @test(i1 %cond) {
entry:
%a = alloca i64
%a32 = bitcast i64* %a to i32*
br label %bb1
bb1:
%x = phi i32* [ %a32, %entry ], [ %x, %bb1 ]
%r = getelementptr i32, i32* %x, i32 1
br i1 %cond, label %bb1, label %exit
exit:
ret void
})",
true},
{R"(
define void @test(i1 %cond) {
entry:
%a = alloca i64
%a32 = bitcast i64* %a to i32*
br label %bb1
bb1:
%x = phi i32* [ %a32, %entry ], [ %r, %bb1 ]
%r = getelementptr i32, i32* %x, i32 1
br i1 %cond, label %bb1, label %exit
exit:
ret void
})",
false},
{R"(
define void @test(i1 %cond, i64* %a) {
entry:
%r = bitcast i64* %a to i32*
ret void
})",
false},
{R"(
define void @test(i1 %cond) {
entry:
%a = alloca i32
%b = alloca i32
br label %bb1
bb1:
%x = phi i32* [ %a, %entry ], [ %b, %bb1 ]
br i1 %cond, label %bb1, label %exit
exit:
ret void
})",
false},
};
TEST_P(FindAllocaForValueTest, findAllocaForValue) {
auto M = parseModule(GetParam().first);
Function *F = M->getFunction("test");
Instruction *I = &findInstructionByName(F, "r");
const AllocaInst *AI = findAllocaForValue(I);
EXPECT_EQ(!!AI, GetParam().second);
}
INSTANTIATE_TEST_CASE_P(FindAllocaForValueTest, FindAllocaForValueTest,
::testing::ValuesIn(FindAllocaForValueTests), );