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: |
23 | TBAATest() : M("TBAATest" , C), MD(C) {} |
24 | |
25 | LLVMContext C; |
26 | Module M; |
27 | MDBuilder MD; |
28 | }; |
29 | |
30 | static StoreInst *getFunctionWithSingleStore(Module *M, StringRef Name) { |
31 | auto &C = M->getContext(); |
32 | FunctionType *FTy = FunctionType::get(Result: Type::getVoidTy(C), isVarArg: {}); |
33 | auto *F = Function::Create(Ty: FTy, Linkage: Function::ExternalLinkage, N: Name, M); |
34 | auto *BB = BasicBlock::Create(Context&: C, Name: "entry" , Parent: F); |
35 | auto *IntType = Type::getInt32Ty(C); |
36 | auto *PtrType = PointerType::get(C, AddressSpace: 0); |
37 | auto *SI = new StoreInst(ConstantInt::get(Ty: IntType, V: 42), |
38 | ConstantPointerNull::get(T: PtrType), BB); |
39 | ReturnInst::Create(C, retVal: nullptr, InsertAtEnd: BB); |
40 | |
41 | return SI; |
42 | } |
43 | |
44 | TEST_F(TBAATest, checkVerifierBehaviorForOldTBAA) { |
45 | auto *SI = getFunctionWithSingleStore(M: &M, Name: "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(Name: "Simple C/C++ TBAA" ); |
50 | auto *MD1 = MD.createTBAANode(Name: "omnipotent char" , Parent: RootMD); |
51 | auto *MD2 = MD.createTBAANode(Name: "int" , Parent: MD1); |
52 | SI->setMetadata(KindID: LLVMContext::MD_tbaa, Node: 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 | |
65 | TEST_F(TBAATest, checkTBAAMerging) { |
66 | auto *SI = getFunctionWithSingleStore(M: &M, Name: "f2" ); |
67 | auto *F = SI->getFunction(); |
68 | |
69 | auto *RootMD = MD.createTBAARoot(Name: "tbaa-root" ); |
70 | auto *MD1 = MD.createTBAANode(Name: "scalar-a" , Parent: RootMD); |
71 | auto *StructTag1 = MD.createTBAAStructTagNode(BaseType: MD1, AccessType: MD1, Offset: 0); |
72 | auto *MD2 = MD.createTBAANode(Name: "scalar-b" , Parent: RootMD); |
73 | auto *StructTag2 = MD.createTBAAStructTagNode(BaseType: MD2, AccessType: MD2, Offset: 0); |
74 | |
75 | auto *GenericMD = MDNode::getMostGenericTBAA(A: StructTag1, B: 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(KindID: LLVMContext::MD_tbaa, Node: GenericMD); |
82 | |
83 | EXPECT_TRUE(!verifyFunction(*F)); |
84 | } |
85 | |
86 | } // end anonymous namspace |
87 | } // end llvm namespace |
88 | |