[BatchAA] Add test for incorrect phi cycle result (NFC)
authorNikita Popov <nikita.ppv@gmail.com>
Mon, 19 Oct 2020 18:51:37 +0000 (20:51 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 19 Oct 2020 18:53:11 +0000 (20:53 +0200)
AA computes the correct result for phi/a1 aliasing, while BatchAA
produces an incorrect result depening on which queries have been
performed beforehand.

llvm/unittests/Analysis/AliasAnalysisTest.cpp

index 6f0f6f5..fcb73d5 100644 (file)
@@ -207,6 +207,48 @@ TEST_F(AliasAnalysisTest, getModRefInfo) {
   EXPECT_EQ(AA.getModRefInfo(AtomicRMW, None), ModRefInfo::ModRef);
 }
 
+static Instruction *getInstructionByName(Function &F, StringRef Name) {
+  for (auto &I : instructions(F))
+    if (I.getName() == Name)
+      return &I;
+  llvm_unreachable("Expected to find instruction!");
+}
+
+TEST_F(AliasAnalysisTest, BatchAAPhiCycles) {
+  LLVMContext C;
+  SMDiagnostic Err;
+  std::unique_ptr<Module> M = parseAssemblyString(R"(
+    define void @f(i8* noalias %a) {
+    entry:
+      br label %loop
+
+    loop:
+      %phi = phi i8* [ null, %entry ], [ %a2, %loop ]
+      %offset1 = phi i64 [ 0, %entry ], [ %offset2, %loop]
+      %offset2 = add i64 %offset1, 1
+      %a1 = getelementptr i8, i8* %a, i64 %offset1
+      %a2 = getelementptr i8, i8* %a, i64 %offset2
+      br label %loop
+    }
+  )", Err, C);
+
+  Function *F = M->getFunction("f");
+  Instruction *Phi = getInstructionByName(*F, "phi");
+  Instruction *A1 = getInstructionByName(*F, "a1");
+  Instruction *A2 = getInstructionByName(*F, "a2");
+  MemoryLocation PhiLoc(Phi, LocationSize::precise(1));
+  MemoryLocation A1Loc(A1, LocationSize::precise(1));
+  MemoryLocation A2Loc(A2, LocationSize::precise(1));
+
+  auto &AA = getAAResults(*F);
+  EXPECT_EQ(NoAlias, AA.alias(A1Loc, A2Loc));
+  EXPECT_EQ(MayAlias, AA.alias(PhiLoc, A1Loc));
+
+  BatchAAResults BatchAA(AA);
+  EXPECT_EQ(NoAlias, BatchAA.alias(A1Loc, A2Loc));
+  EXPECT_EQ(NoAlias, BatchAA.alias(PhiLoc, A1Loc)); // TODO: This is wrong.
+}
+
 class AAPassInfraTest : public testing::Test {
 protected:
   LLVMContext C;