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/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"
21 class TBAATest
: public testing::Test
{
23 TBAATest() : M("TBAATest", C
), MD(C
) {}
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
);
44 TEST_F(TBAATest
, checkVerifierBehaviorForOldTBAA
) {
45 auto *SI
= getFunctionWithSingleStore(&M
, "f1");
46 auto *F
= SI
->getFunction();
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
);
54 SmallVector
<char, 0> ErrorMsg
;
55 raw_svector_ostream
Outs(ErrorMsg
);
57 StringRef
ExpectedFailureMsg(
58 "Old-style TBAA is no longer allowed, use struct-path TBAA instead");
60 EXPECT_TRUE(verifyFunction(*F
, &Outs
));
61 EXPECT_TRUE(StringRef(ErrorMsg
.begin(), ErrorMsg
.size())
62 .starts_with(ExpectedFailureMsg
));
65 TEST_F(TBAATest
, checkTBAAMerging
) {
66 auto *SI
= getFunctionWithSingleStore(&M
, "f2");
67 auto *F
= SI
->getFunction();
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);
75 auto *GenericMD
= MDNode::getMostGenericTBAA(StructTag1
, StructTag2
);
77 EXPECT_EQ(GenericMD
, nullptr);
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
);
83 EXPECT_TRUE(!verifyFunction(*F
));
86 } // end anonymous namspace
87 } // end llvm namespace