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