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