[llvm] r207923 - [TBAA] Fix handling of mixed TBAA (path-aware and non-path-aware TBAA).

Juergen Ributzka juergen at apple.com
Sat May 3 15:32:52 PDT 2014


Author: ributzka
Date: Sat May  3 17:32:52 2014
New Revision: 207923

URL: http://llvm.org/viewvc/llvm-project?rev=207923&view=rev
Log:
[TBAA] Fix handling of mixed TBAA (path-aware and non-path-aware TBAA).

This fix simply ensures that both metadata nodes are path-aware before
performing path-aware alias analysis.

This issue isn't normally triggered in LLVM, because we perform an autoupgrade
of the TBAA metadata to the new format when reading in LL or BC files. This
issue only appears when a client creates the IR manually and mixes old and new
TBAA metadata format.

This fixes <rdar://problem/16760860>.

Added:
    llvm/trunk/unittests/Analysis/MixedTBAATest.cpp
Modified:
    llvm/trunk/lib/Analysis/TypeBasedAliasAnalysis.cpp
    llvm/trunk/unittests/Analysis/CMakeLists.txt

Modified: llvm/trunk/lib/Analysis/TypeBasedAliasAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/TypeBasedAliasAnalysis.cpp?rev=207923&r1=207922&r2=207923&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/TypeBasedAliasAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/TypeBasedAliasAnalysis.cpp Sat May  3 17:32:52 2014
@@ -339,7 +339,8 @@ static bool isStructPathTBAA(const MDNod
 bool
 TypeBasedAliasAnalysis::Aliases(const MDNode *A,
                                 const MDNode *B) const {
-  if (isStructPathTBAA(A))
+  // Make sure that both MDNodes are struct-path aware.
+  if (isStructPathTBAA(A) && isStructPathTBAA(B))
     return PathAliases(A, B);
 
   // Keep track of the root node for A and B.
@@ -385,6 +386,10 @@ TypeBasedAliasAnalysis::Aliases(const MD
 bool
 TypeBasedAliasAnalysis::PathAliases(const MDNode *A,
                                     const MDNode *B) const {
+  // Verify that both input nodes are struct-path aware.
+  assert(isStructPathTBAA(A) && "MDNode A is not struct-path aware.");
+  assert(isStructPathTBAA(B) && "MDNode B is not struct-path aware.");
+
   // Keep track of the root node for A and B.
   TBAAStructTypeNode RootA, RootB;
   TBAAStructTagNode TagA(A), TagB(B);
@@ -560,7 +565,7 @@ MDNode *MDNode::getMostGenericTBAA(MDNod
     return A;
 
   // For struct-path aware TBAA, we use the access type of the tag.
-  bool StructPath = isStructPathTBAA(A);
+  bool StructPath = isStructPathTBAA(A) && isStructPathTBAA(B);
   if (StructPath) {
     A = cast_or_null<MDNode>(A->getOperand(1));
     if (!A) return nullptr;

Modified: llvm/trunk/unittests/Analysis/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Analysis/CMakeLists.txt?rev=207923&r1=207922&r2=207923&view=diff
==============================================================================
--- llvm/trunk/unittests/Analysis/CMakeLists.txt (original)
+++ llvm/trunk/unittests/Analysis/CMakeLists.txt Sat May  3 17:32:52 2014
@@ -9,4 +9,5 @@ add_llvm_unittest(AnalysisTests
   CFGTest.cpp
   LazyCallGraphTest.cpp
   ScalarEvolutionTest.cpp
+  MixedTBAATest.cpp
   )

Added: llvm/trunk/unittests/Analysis/MixedTBAATest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Analysis/MixedTBAATest.cpp?rev=207923&view=auto
==============================================================================
--- llvm/trunk/unittests/Analysis/MixedTBAATest.cpp (added)
+++ llvm/trunk/unittests/Analysis/MixedTBAATest.cpp Sat May  3 17:32:52 2014
@@ -0,0 +1,77 @@
+//===--- MixedTBAATest.cpp - Mixed TBAA unit tests ------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Analysis/Passes.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/MDBuilder.h"
+#include "llvm/IR/Module.h"
+#include "llvm/PassManager.h"
+#include "llvm/Support/CommandLine.h"
+#include "gtest/gtest.h"
+
+namespace llvm {
+namespace {
+
+class MixedTBAATest : public testing::Test {
+protected:
+  MixedTBAATest() : M("MixedTBAATest", C), MD(C) {}
+
+  LLVMContext C;
+  Module M;
+  MDBuilder MD;
+  PassManager PM;
+};
+
+TEST_F(MixedTBAATest, MixedTBAA) {
+  // Setup function.
+  FunctionType *FTy = FunctionType::get(Type::getVoidTy(C),
+                                        std::vector<Type *>(), false);
+  auto *F = cast<Function>(M.getOrInsertFunction("f", FTy));
+  auto *BB = BasicBlock::Create(C, "entry", F);
+  auto IntType = Type::getInt32Ty(C);
+  auto PtrType = Type::getInt32PtrTy(C);
+  auto *Value  = ConstantInt::get(IntType, 42);
+  auto *Addr = ConstantPointerNull::get(PtrType);
+
+  auto *Store1 = new StoreInst(Value, Addr, BB);
+  auto *Store2 = new StoreInst(Value, Addr, BB);
+  ReturnInst::Create(C, 0, BB);
+
+  // New TBAA metadata
+  {
+    auto RootMD = MD.createTBAARoot("Simple C/C++ TBAA");
+    auto MD1 = MD.createTBAAScalarTypeNode("omnipotent char", RootMD);
+    auto MD2 = MD.createTBAAScalarTypeNode("int", MD1);
+    auto MD3 = MD.createTBAAStructTagNode(MD2, MD2, 0);
+    Store2->setMetadata(LLVMContext::MD_tbaa, MD3);
+  }
+
+  // Old TBAA metadata
+  {
+    auto RootMD = MD.createTBAARoot("Simple C/C++ TBAA");
+    auto MD1 = MD.createTBAANode("omnipotent char", RootMD);
+    auto MD2 = MD.createTBAANode("int", MD1);
+    Store1->setMetadata(LLVMContext::MD_tbaa, MD2);
+  }
+
+  // Run the TBAA eval pass on a mixture of path-aware and non-path-aware TBAA.
+  // The order of the metadata (path-aware vs non-path-aware) is important,
+  // because the AA eval pass only runs one test per store-pair.
+  const char* args[] = { "MixedTBAATest", "-evaluate-tbaa" };
+  cl::ParseCommandLineOptions(sizeof(args) / sizeof(const char*), args);
+  PM.add(createTypeBasedAliasAnalysisPass());
+  PM.add(createAAEvalPass());
+  PM.run(M);
+}
+
+} // end anonymous namspace
+} // end llvm namespace
+





More information about the llvm-commits mailing list