Sanjoy Das | b73e155 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 1 | //===--- TBAATest.cpp - Mixed TBAA unit tests -----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/Analysis/AliasAnalysisEvaluator.h" |
| 11 | #include "llvm/Analysis/Passes.h" |
| 12 | #include "llvm/IR/Constants.h" |
| 13 | #include "llvm/IR/Instructions.h" |
| 14 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 3c0d607 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 15 | #include "llvm/IR/LegacyPassManager.h" |
Sanjoy Das | b73e155 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 16 | #include "llvm/IR/MDBuilder.h" |
| 17 | #include "llvm/IR/Module.h" |
Sanjoy Das | b73e155 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Verifier.h" |
| 19 | #include "llvm/Support/CommandLine.h" |
| 20 | #include "gtest/gtest.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | namespace { |
| 24 | |
Sanjoy Das | 965b15e | 2016-12-11 20:07:25 +0000 | [diff] [blame] | 25 | class TBAATest : public testing::Test { |
Sanjoy Das | b73e155 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 26 | protected: |
Sanjoy Das | 965b15e | 2016-12-11 20:07:25 +0000 | [diff] [blame] | 27 | TBAATest() : M("TBAATest", C), MD(C) {} |
Sanjoy Das | b73e155 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 28 | |
| 29 | LLVMContext C; |
| 30 | Module M; |
| 31 | MDBuilder MD; |
| 32 | }; |
| 33 | |
Sanjoy Das | 965b15e | 2016-12-11 20:07:25 +0000 | [diff] [blame] | 34 | static StoreInst *getFunctionWithSingleStore(Module *M, StringRef Name) { |
| 35 | auto &C = M->getContext(); |
Sanjoy Das | b73e155 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 36 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), {}); |
Sanjoy Das | 965b15e | 2016-12-11 20:07:25 +0000 | [diff] [blame] | 37 | auto *F = cast<Function>(M->getOrInsertFunction(Name, FTy)); |
Sanjoy Das | b73e155 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 38 | auto *BB = BasicBlock::Create(C, "entry", F); |
| 39 | auto *IntType = Type::getInt32Ty(C); |
| 40 | auto *PtrType = Type::getInt32PtrTy(C); |
| 41 | auto *SI = new StoreInst(ConstantInt::get(IntType, 42), |
| 42 | ConstantPointerNull::get(PtrType), BB); |
| 43 | ReturnInst::Create(C, nullptr, BB); |
| 44 | |
Sanjoy Das | 965b15e | 2016-12-11 20:07:25 +0000 | [diff] [blame] | 45 | return SI; |
| 46 | } |
| 47 | |
| 48 | TEST_F(TBAATest, checkVerifierBehaviorForOldTBAA) { |
| 49 | auto *SI = getFunctionWithSingleStore(&M, "f1"); |
| 50 | auto *F = SI->getFunction(); |
| 51 | |
| 52 | // C++ unit test case to avoid going through the auto upgrade logic. |
Sanjoy Das | b73e155 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 53 | auto *RootMD = MD.createTBAARoot("Simple C/C++ TBAA"); |
| 54 | auto *MD1 = MD.createTBAANode("omnipotent char", RootMD); |
| 55 | auto *MD2 = MD.createTBAANode("int", MD1); |
| 56 | SI->setMetadata(LLVMContext::MD_tbaa, MD2); |
| 57 | |
| 58 | SmallVector<char, 0> ErrorMsg; |
| 59 | raw_svector_ostream Outs(ErrorMsg); |
| 60 | |
| 61 | StringRef ExpectedFailureMsg( |
| 62 | "Old-style TBAA is no longer allowed, use struct-path TBAA instead"); |
| 63 | |
| 64 | EXPECT_TRUE(verifyFunction(*F, &Outs)); |
| 65 | EXPECT_TRUE(StringRef(ErrorMsg.begin(), ErrorMsg.size()) |
| 66 | .startswith(ExpectedFailureMsg)); |
| 67 | } |
| 68 | |
Sanjoy Das | 965b15e | 2016-12-11 20:07:25 +0000 | [diff] [blame] | 69 | TEST_F(TBAATest, checkTBAAMerging) { |
| 70 | auto *SI = getFunctionWithSingleStore(&M, "f2"); |
| 71 | auto *F = SI->getFunction(); |
| 72 | |
| 73 | auto *RootMD = MD.createTBAARoot("tbaa-root"); |
| 74 | auto *MD1 = MD.createTBAANode("scalar-a", RootMD); |
| 75 | auto *StructTag1 = MD.createTBAAStructTagNode(MD1, MD1, 0); |
| 76 | auto *MD2 = MD.createTBAANode("scalar-b", RootMD); |
| 77 | auto *StructTag2 = MD.createTBAAStructTagNode(MD2, MD2, 0); |
| 78 | |
| 79 | auto *GenericMD = MDNode::getMostGenericTBAA(StructTag1, StructTag2); |
| 80 | |
| 81 | EXPECT_EQ(GenericMD, nullptr); |
| 82 | |
| 83 | // Despite GenericMD being nullptr, we expect the setMetadata call to be well |
| 84 | // defined and produce a well-formed function. |
| 85 | SI->setMetadata(LLVMContext::MD_tbaa, GenericMD); |
| 86 | |
| 87 | EXPECT_TRUE(!verifyFunction(*F)); |
| 88 | } |
| 89 | |
Sanjoy Das | b73e155 | 2016-11-08 20:46:01 +0000 | [diff] [blame] | 90 | } // end anonymous namspace |
| 91 | } // end llvm namespace |