1 //===--- TBAATest.cpp - Mixed TBAA unit tests -----------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
24 class TBAATest
: public testing::Test
{
26 TBAATest() : M("TBAATest", C
), MD(C
) {}
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
);
47 TEST_F(TBAATest
, checkVerifierBehaviorForOldTBAA
) {
48 auto *SI
= getFunctionWithSingleStore(&M
, "f1");
49 auto *F
= SI
->getFunction();
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
);
57 SmallVector
<char, 0> ErrorMsg
;
58 raw_svector_ostream
Outs(ErrorMsg
);
60 StringRef
ExpectedFailureMsg(
61 "Old-style TBAA is no longer allowed, use struct-path TBAA instead");
63 EXPECT_TRUE(verifyFunction(*F
, &Outs
));
64 EXPECT_TRUE(StringRef(ErrorMsg
.begin(), ErrorMsg
.size())
65 .startswith(ExpectedFailureMsg
));
68 TEST_F(TBAATest
, checkTBAAMerging
) {
69 auto *SI
= getFunctionWithSingleStore(&M
, "f2");
70 auto *F
= SI
->getFunction();
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);
78 auto *GenericMD
= MDNode::getMostGenericTBAA(StructTag1
, StructTag2
);
80 EXPECT_EQ(GenericMD
, nullptr);
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
);
86 EXPECT_TRUE(!verifyFunction(*F
));
89 } // end anonymous namspace
90 } // end llvm namespace