• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- TBAATest.cpp - Mixed TBAA unit tests -----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/Analysis/AliasAnalysisEvaluator.h"
11 #include "llvm/Analysis/Passes.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/LegacyPassManager.h"
16 #include "llvm/IR/MDBuilder.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/Verifier.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "gtest/gtest.h"
21 
22 namespace llvm {
23 namespace {
24 
25 class TBAATest : public testing::Test {
26 protected:
TBAATest()27   TBAATest() : M("TBAATest", C), MD(C) {}
28 
29   LLVMContext C;
30   Module M;
31   MDBuilder MD;
32 };
33 
getFunctionWithSingleStore(Module * M,StringRef Name)34 static StoreInst *getFunctionWithSingleStore(Module *M, StringRef Name) {
35   auto &C = M->getContext();
36   FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), {});
37   auto *F = cast<Function>(M->getOrInsertFunction(Name, FTy));
38   auto *BB = BasicBlock::Create(C, "entry", F);
39   auto *IntType = Type::getInt32Ty(C);
40   auto *PtrType = Type::getInt32PtrTy(C);
41   auto *SI = new StoreInst(ConstantInt::get(IntType, 42),
42                            ConstantPointerNull::get(PtrType), BB);
43   ReturnInst::Create(C, nullptr, BB);
44 
45   return SI;
46 }
47 
TEST_F(TBAATest,checkVerifierBehaviorForOldTBAA)48 TEST_F(TBAATest, checkVerifierBehaviorForOldTBAA) {
49   auto *SI = getFunctionWithSingleStore(&M, "f1");
50   auto *F = SI->getFunction();
51 
52   // C++ unit test case to avoid going through the auto upgrade logic.
53   auto *RootMD = MD.createTBAARoot("Simple C/C++ TBAA");
54   auto *MD1 = MD.createTBAANode("omnipotent char", RootMD);
55   auto *MD2 = MD.createTBAANode("int", MD1);
56   SI->setMetadata(LLVMContext::MD_tbaa, MD2);
57 
58   SmallVector<char, 0> ErrorMsg;
59   raw_svector_ostream Outs(ErrorMsg);
60 
61   StringRef ExpectedFailureMsg(
62       "Old-style TBAA is no longer allowed, use struct-path TBAA instead");
63 
64   EXPECT_TRUE(verifyFunction(*F, &Outs));
65   EXPECT_TRUE(StringRef(ErrorMsg.begin(), ErrorMsg.size())
66                   .startswith(ExpectedFailureMsg));
67 }
68 
TEST_F(TBAATest,checkTBAAMerging)69 TEST_F(TBAATest, checkTBAAMerging) {
70   auto *SI = getFunctionWithSingleStore(&M, "f2");
71   auto *F = SI->getFunction();
72 
73   auto *RootMD = MD.createTBAARoot("tbaa-root");
74   auto *MD1 = MD.createTBAANode("scalar-a", RootMD);
75   auto *StructTag1 = MD.createTBAAStructTagNode(MD1, MD1, 0);
76   auto *MD2 = MD.createTBAANode("scalar-b", RootMD);
77   auto *StructTag2 = MD.createTBAAStructTagNode(MD2, MD2, 0);
78 
79   auto *GenericMD = MDNode::getMostGenericTBAA(StructTag1, StructTag2);
80 
81   EXPECT_EQ(GenericMD, nullptr);
82 
83   // Despite GenericMD being nullptr, we expect the setMetadata call to be well
84   // defined and produce a well-formed function.
85   SI->setMetadata(LLVMContext::MD_tbaa, GenericMD);
86 
87   EXPECT_TRUE(!verifyFunction(*F));
88 }
89 
90 } // end anonymous namspace
91 } // end llvm namespace
92