1 //===- BasicAliasAnalysisTest.cpp - Unit tests for BasicAA ----------------===// 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 // 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; 47 48 TestAnalyses(BasicAATest &Test) 49 : DT(*Test.F), AC(*Test.F), BAA(Test.DL, *Test.F, Test.TLI, AC, &DT) {} 50 }; 51 52 llvm::Optional<TestAnalyses> Analyses; 53 54 BasicAAResult &setupAnalyses() { 55 assert(F); 56 Analyses.emplace(*this); 57 return Analyses->BAA; 58 } 59 60 public: 61 BasicAATest() 62 : M("BasicAATest", C), B(C), DL(DLString), TLI(TLII), F(nullptr) {} 63 }; 64 65 // Check that a function arg can't trivially alias a global when we're accessing 66 // >sizeof(global) bytes through that arg, unless the access size is just an 67 // upper-bound. 68 TEST_F(BasicAATest, AliasInstWithObjectOfImpreciseSize) { 69 F = Function::Create( 70 FunctionType::get(B.getVoidTy(), {B.getInt32Ty()->getPointerTo()}, false), 71 GlobalValue::ExternalLinkage, "F", &M); 72 73 BasicBlock *Entry(BasicBlock::Create(C, "", F)); 74 B.SetInsertPoint(Entry); 75 76 Value *IncomingI32Ptr = F->arg_begin(); 77 78 auto *GlobalPtr = 79 cast<GlobalVariable>(M.getOrInsertGlobal("some_global", B.getInt8Ty())); 80 81 // Without sufficiently restricted linkage/an init, some of the object size 82 // checking bits get more conservative. 83 GlobalPtr->setLinkage(GlobalValue::LinkageTypes::InternalLinkage); 84 GlobalPtr->setInitializer(B.getInt8(0)); 85 86 BasicAAResult &BasicAA = setupAnalyses(); 87 ASSERT_EQ( 88 BasicAA.alias(MemoryLocation(IncomingI32Ptr, LocationSize::precise(4)), 89 MemoryLocation(GlobalPtr, LocationSize::precise(1))), 90 AliasResult::NoAlias); 91 92 ASSERT_EQ( 93 BasicAA.alias(MemoryLocation(IncomingI32Ptr, LocationSize::upperBound(4)), 94 MemoryLocation(GlobalPtr, LocationSize::precise(1))), 95 AliasResult::MayAlias); 96 } 97 98 // Check that we fall back to MayAlias if we see an access of an entire object 99 // that's just an upper-bound. 100 TEST_F(BasicAATest, AliasInstWithFullObjectOfImpreciseSize) { 101 F = Function::Create( 102 FunctionType::get(B.getVoidTy(), {B.getInt64Ty()}, false), 103 GlobalValue::ExternalLinkage, "F", &M); 104 105 BasicBlock *Entry(BasicBlock::Create(C, "", F)); 106 B.SetInsertPoint(Entry); 107 108 Value *ArbitraryI32 = F->arg_begin(); 109 AllocaInst *I8 = B.CreateAlloca(B.getInt8Ty(), B.getInt32(2)); 110 auto *I8AtUncertainOffset = 111 cast<GetElementPtrInst>(B.CreateGEP(I8, ArbitraryI32)); 112 113 BasicAAResult &BasicAA = setupAnalyses(); 114 ASSERT_EQ(BasicAA.alias( 115 MemoryLocation(I8, LocationSize::precise(2)), 116 MemoryLocation(I8AtUncertainOffset, LocationSize::precise(1))), 117 AliasResult::PartialAlias); 118 119 ASSERT_EQ(BasicAA.alias( 120 MemoryLocation(I8, LocationSize::upperBound(2)), 121 MemoryLocation(I8AtUncertainOffset, LocationSize::precise(1))), 122 AliasResult::MayAlias); 123 } 124