Sanjoy Das | 2582e69 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 1 | //===--- TBAATest.cpp - Mixed TBAA unit tests -----------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Sanjoy Das | 2582e69 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 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" |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 14 | #include "llvm/IR/LegacyPassManager.h" |
Sanjoy Das | 2582e69 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 15 | #include "llvm/IR/MDBuilder.h" |
| 16 | #include "llvm/IR/Module.h" |
Sanjoy Das | 2582e69 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Verifier.h" |
| 18 | #include "llvm/Support/CommandLine.h" |
| 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | namespace llvm { |
| 22 | namespace { |
| 23 | |
Sanjoy Das | 6de6788 | 2016-12-11 20:07:25 +0000 | [diff] [blame] | 24 | class TBAATest : public testing::Test { |
Sanjoy Das | 2582e69 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 25 | protected: |
Sanjoy Das | 6de6788 | 2016-12-11 20:07:25 +0000 | [diff] [blame] | 26 | TBAATest() : M("TBAATest", C), MD(C) {} |
Sanjoy Das | 2582e69 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 27 | |
| 28 | LLVMContext C; |
| 29 | Module M; |
| 30 | MDBuilder MD; |
| 31 | }; |
| 32 | |
Sanjoy Das | 6de6788 | 2016-12-11 20:07:25 +0000 | [diff] [blame] | 33 | static StoreInst *getFunctionWithSingleStore(Module *M, StringRef Name) { |
| 34 | auto &C = M->getContext(); |
Sanjoy Das | 2582e69 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 35 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), {}); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 36 | auto *F = Function::Create(FTy, Function::ExternalLinkage, Name, M); |
Sanjoy Das | 2582e69 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 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 | |
Sanjoy Das | 6de6788 | 2016-12-11 20:07:25 +0000 | [diff] [blame] | 44 | return SI; |
| 45 | } |
| 46 | |
| 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. |
Sanjoy Das | 2582e69 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 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 | |
Sanjoy Das | 6de6788 | 2016-12-11 20:07:25 +0000 | [diff] [blame] | 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 | |
Sanjoy Das | 2582e69 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 89 | } // end anonymous namspace |
| 90 | } // end llvm namespace |