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