[llvm] 7f1bb32 - [NFC] Add unittests for findAllocaForValue

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 27 01:54:29 PDT 2020


Author: Vitaly Buka
Date: 2020-08-27T01:54:19-07:00
New Revision: 7f1bb326ee112c5bec897db2be016289f15a7778

URL: https://github.com/llvm/llvm-project/commit/7f1bb326ee112c5bec897db2be016289f15a7778
DIFF: https://github.com/llvm/llvm-project/commit/7f1bb326ee112c5bec897db2be016289f15a7778.diff

LOG: [NFC] Add unittests for findAllocaForValue

Added: 
    

Modified: 
    llvm/unittests/Analysis/ValueTrackingTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/Analysis/ValueTrackingTest.cpp b/llvm/unittests/Analysis/ValueTrackingTest.cpp
index 75ebe9cbf8aa..732f71e4de90 100644
--- a/llvm/unittests/Analysis/ValueTrackingTest.cpp
+++ b/llvm/unittests/Analysis/ValueTrackingTest.cpp
@@ -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), );


        


More information about the llvm-commits mailing list