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