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