George Burgess IV | 91f4bc63 | 2018-10-10 06:39:40 +0000 | [diff] [blame] | 1 | //===- BasicAliasAnalysisTest.cpp - Unit tests for BasicAA ----------------===// |
| 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 | // Targeted tests that are hard/convoluted to make happen with just `opt`. |
| 11 | // |
| 12 | |
| 13 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
| 14 | #include "llvm/Analysis/AliasAnalysis.h" |
| 15 | #include "llvm/AsmParser/Parser.h" |
| 16 | #include "llvm/IR/Dominators.h" |
| 17 | #include "llvm/IR/IRBuilder.h" |
| 18 | #include "llvm/IR/LLVMContext.h" |
| 19 | #include "llvm/IR/Module.h" |
| 20 | #include "llvm/Support/SourceMgr.h" |
| 21 | #include "gtest/gtest.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | // FIXME: This is duplicated between this file and MemorySSATest. Refactor. |
| 26 | const static char DLString[] = "e-i64:64-f80:128-n8:16:32:64-S128"; |
| 27 | |
| 28 | /// There's a lot of common setup between these tests. This fixture helps reduce |
| 29 | /// that. Tests should mock up a function, store it in F, and then call |
| 30 | /// setupAnalyses(). |
| 31 | class BasicAATest : public testing::Test { |
| 32 | protected: |
| 33 | // N.B. Many of these members depend on each other (e.g. the Module depends on |
| 34 | // the Context, etc.). So, order matters here (and in TestAnalyses). |
| 35 | LLVMContext C; |
| 36 | Module M; |
| 37 | IRBuilder<> B; |
| 38 | DataLayout DL; |
| 39 | TargetLibraryInfoImpl TLII; |
| 40 | TargetLibraryInfo TLI; |
| 41 | Function *F; |
| 42 | |
| 43 | // Things that we need to build after the function is created. |
| 44 | struct TestAnalyses { |
| 45 | DominatorTree DT; |
| 46 | AssumptionCache AC; |
| 47 | BasicAAResult BAA; |
| 48 | |
| 49 | TestAnalyses(BasicAATest &Test) |
| 50 | : DT(*Test.F), AC(*Test.F), BAA(Test.DL, *Test.F, Test.TLI, AC, &DT) {} |
| 51 | }; |
| 52 | |
| 53 | llvm::Optional<TestAnalyses> Analyses; |
| 54 | |
| 55 | BasicAAResult &setupAnalyses() { |
| 56 | assert(F); |
| 57 | Analyses.emplace(*this); |
| 58 | return Analyses->BAA; |
| 59 | } |
| 60 | |
| 61 | public: |
| 62 | BasicAATest() |
| 63 | : M("BasicAATest", C), B(C), DL(DLString), TLI(TLII), F(nullptr) {} |
| 64 | }; |
| 65 | |
| 66 | // Check that a function arg can't trivially alias a global when we're accessing |
| 67 | // >sizeof(global) bytes through that arg, unless the access size is just an |
| 68 | // upper-bound. |
| 69 | TEST_F(BasicAATest, AliasInstWithObjectOfImpreciseSize) { |
| 70 | F = Function::Create( |
| 71 | FunctionType::get(B.getVoidTy(), {B.getInt32Ty()->getPointerTo()}, false), |
| 72 | GlobalValue::ExternalLinkage, "F", &M); |
| 73 | |
| 74 | BasicBlock *Entry(BasicBlock::Create(C, "", F)); |
| 75 | B.SetInsertPoint(Entry); |
| 76 | |
| 77 | Value *IncomingI32Ptr = F->arg_begin(); |
| 78 | |
| 79 | auto *GlobalPtr = |
| 80 | cast<GlobalVariable>(M.getOrInsertGlobal("some_global", B.getInt8Ty())); |
| 81 | |
| 82 | // Without sufficiently restricted linkage/an init, some of the object size |
| 83 | // checking bits get more conservative. |
| 84 | GlobalPtr->setLinkage(GlobalValue::LinkageTypes::InternalLinkage); |
| 85 | GlobalPtr->setInitializer(B.getInt8(0)); |
| 86 | |
| 87 | BasicAAResult &BasicAA = setupAnalyses(); |
| 88 | ASSERT_EQ( |
| 89 | BasicAA.alias(MemoryLocation(IncomingI32Ptr, LocationSize::precise(4)), |
| 90 | MemoryLocation(GlobalPtr, LocationSize::precise(1))), |
| 91 | AliasResult::NoAlias); |
| 92 | |
| 93 | ASSERT_EQ( |
| 94 | BasicAA.alias(MemoryLocation(IncomingI32Ptr, LocationSize::upperBound(4)), |
| 95 | MemoryLocation(GlobalPtr, LocationSize::precise(1))), |
| 96 | AliasResult::MayAlias); |
| 97 | } |
| 98 | |
| 99 | // Check that we fall back to MayAlias if we see an access of an entire object |
| 100 | // that's just an upper-bound. |
| 101 | TEST_F(BasicAATest, AliasInstWithFullObjectOfImpreciseSize) { |
| 102 | F = Function::Create( |
| 103 | FunctionType::get(B.getVoidTy(), {B.getInt64Ty()}, false), |
| 104 | GlobalValue::ExternalLinkage, "F", &M); |
| 105 | |
| 106 | BasicBlock *Entry(BasicBlock::Create(C, "", F)); |
| 107 | B.SetInsertPoint(Entry); |
| 108 | |
| 109 | Value *ArbitraryI32 = F->arg_begin(); |
| 110 | AllocaInst *I8 = B.CreateAlloca(B.getInt8Ty(), B.getInt32(2)); |
| 111 | auto *I8AtUncertainOffset = |
| 112 | cast<GetElementPtrInst>(B.CreateGEP(I8, ArbitraryI32)); |
| 113 | |
| 114 | BasicAAResult &BasicAA = setupAnalyses(); |
| 115 | ASSERT_EQ(BasicAA.alias( |
| 116 | MemoryLocation(I8, LocationSize::precise(2)), |
| 117 | MemoryLocation(I8AtUncertainOffset, LocationSize::precise(1))), |
| 118 | AliasResult::PartialAlias); |
| 119 | |
| 120 | ASSERT_EQ(BasicAA.alias( |
| 121 | MemoryLocation(I8, LocationSize::upperBound(2)), |
| 122 | MemoryLocation(I8AtUncertainOffset, LocationSize::precise(1))), |
| 123 | AliasResult::MayAlias); |
| 124 | } |