Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / unittests / Analysis / BasicAliasAnalysisTest.cpp
blob8f0feb6e4797e26eee2c6791dbe6034c5ce23295
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`.
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"
24 using namespace llvm;
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";
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;
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 AAResults AAR;
50 SimpleAAQueryInfo AAQI;
52 TestAnalyses(BasicAATest &Test)
53 : DT(*Test.F), AC(*Test.F), BAA(Test.DL, *Test.F, Test.TLI, AC, &DT),
54 AAR(Test.TLI), AAQI(AAR) {
55 AAR.addAAResult(BAA);
59 std::optional<TestAnalyses> Analyses;
61 TestAnalyses &setupAnalyses() {
62 assert(F);
63 Analyses.emplace(*this);
64 return *Analyses;
67 public:
68 BasicAATest()
69 : M("BasicAATest", C), B(C), DL(DLString), TLI(TLII), F(nullptr) {}
72 // Check that a function arg can't trivially alias a global when we're accessing
73 // >sizeof(global) bytes through that arg, unless the access size is just an
74 // upper-bound.
75 TEST_F(BasicAATest, AliasInstWithObjectOfImpreciseSize) {
76 F = Function::Create(FunctionType::get(B.getVoidTy(), {B.getPtrTy()}, false),
77 GlobalValue::ExternalLinkage, "F", &M);
79 BasicBlock *Entry(BasicBlock::Create(C, "", F));
80 B.SetInsertPoint(Entry);
82 Value *IncomingI32Ptr = F->arg_begin();
84 auto *GlobalPtr =
85 cast<GlobalVariable>(M.getOrInsertGlobal("some_global", B.getInt8Ty()));
87 // Without sufficiently restricted linkage/an init, some of the object size
88 // checking bits get more conservative.
89 GlobalPtr->setLinkage(GlobalValue::LinkageTypes::InternalLinkage);
90 GlobalPtr->setInitializer(B.getInt8(0));
92 auto &AllAnalyses = setupAnalyses();
93 BasicAAResult &BasicAA = AllAnalyses.BAA;
94 AAQueryInfo &AAQI = AllAnalyses.AAQI;
95 ASSERT_EQ(
96 BasicAA.alias(MemoryLocation(IncomingI32Ptr, LocationSize::precise(4)),
97 MemoryLocation(GlobalPtr, LocationSize::precise(1)), AAQI,
98 nullptr),
99 AliasResult::NoAlias);
101 ASSERT_EQ(
102 BasicAA.alias(MemoryLocation(IncomingI32Ptr, LocationSize::upperBound(4)),
103 MemoryLocation(GlobalPtr, LocationSize::precise(1)), AAQI,
104 nullptr),
105 AliasResult::MayAlias);
108 // Check that we fall back to MayAlias if we see an access of an entire object
109 // that's just an upper-bound.
110 TEST_F(BasicAATest, AliasInstWithFullObjectOfImpreciseSize) {
111 F = Function::Create(
112 FunctionType::get(B.getVoidTy(), {B.getInt64Ty()}, false),
113 GlobalValue::ExternalLinkage, "F", &M);
115 BasicBlock *Entry(BasicBlock::Create(C, "", F));
116 B.SetInsertPoint(Entry);
118 Value *ArbitraryI32 = F->arg_begin();
119 AllocaInst *I8 = B.CreateAlloca(B.getInt8Ty(), B.getInt32(2));
120 auto *I8AtUncertainOffset =
121 cast<GetElementPtrInst>(B.CreateGEP(B.getInt8Ty(), I8, ArbitraryI32));
123 auto &AllAnalyses = setupAnalyses();
124 BasicAAResult &BasicAA = AllAnalyses.BAA;
125 AAQueryInfo &AAQI = AllAnalyses.AAQI;
126 ASSERT_EQ(BasicAA.alias(
127 MemoryLocation(I8, LocationSize::precise(2)),
128 MemoryLocation(I8AtUncertainOffset, LocationSize::precise(1)),
129 AAQI, nullptr),
130 AliasResult::PartialAlias);
132 ASSERT_EQ(BasicAA.alias(
133 MemoryLocation(I8, LocationSize::upperBound(2)),
134 MemoryLocation(I8AtUncertainOffset, LocationSize::precise(1)),
135 AAQI, nullptr),
136 AliasResult::MayAlias);
139 TEST_F(BasicAATest, PartialAliasOffsetPhi) {
140 F = Function::Create(
141 FunctionType::get(B.getVoidTy(), {B.getPtrTy(), B.getInt1Ty()}, false),
142 GlobalValue::ExternalLinkage, "F", &M);
144 Value *Ptr = F->arg_begin();
145 Value *I = F->arg_begin() + 1;
147 BasicBlock *Entry(BasicBlock::Create(C, "", F));
148 BasicBlock *B1(BasicBlock::Create(C, "", F));
149 BasicBlock *B2(BasicBlock::Create(C, "", F));
150 BasicBlock *End(BasicBlock::Create(C, "", F));
152 B.SetInsertPoint(Entry);
153 B.CreateCondBr(I, B1, B2);
155 B.SetInsertPoint(B1);
156 auto *Ptr1 =
157 cast<GetElementPtrInst>(B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1)));
158 B.CreateBr(End);
160 B.SetInsertPoint(B2);
161 auto *Ptr2 =
162 cast<GetElementPtrInst>(B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1)));
163 B.CreateBr(End);
165 B.SetInsertPoint(End);
166 auto *Phi = B.CreatePHI(B.getPtrTy(), 2);
167 Phi->addIncoming(Ptr1, B1);
168 Phi->addIncoming(Ptr2, B2);
169 B.CreateRetVoid();
171 auto &AllAnalyses = setupAnalyses();
172 BasicAAResult &BasicAA = AllAnalyses.BAA;
173 AAQueryInfo &AAQI = AllAnalyses.AAQI;
174 AliasResult AR = BasicAA.alias(MemoryLocation(Ptr, LocationSize::precise(2)),
175 MemoryLocation(Phi, LocationSize::precise(1)),
176 AAQI, nullptr);
177 ASSERT_EQ(AR.getOffset(), 1);
180 TEST_F(BasicAATest, PartialAliasOffsetSelect) {
181 F = Function::Create(
182 FunctionType::get(B.getVoidTy(), {B.getPtrTy(), B.getInt1Ty()}, false),
183 GlobalValue::ExternalLinkage, "F", &M);
185 Value *Ptr = F->arg_begin();
186 Value *I = F->arg_begin() + 1;
188 BasicBlock *Entry(BasicBlock::Create(C, "", F));
189 B.SetInsertPoint(Entry);
191 auto *Ptr1 =
192 cast<GetElementPtrInst>(B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1)));
193 auto *Ptr2 =
194 cast<GetElementPtrInst>(B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1)));
195 auto *Select = B.CreateSelect(I, Ptr1, Ptr2);
196 B.CreateRetVoid();
198 auto &AllAnalyses = setupAnalyses();
199 BasicAAResult &BasicAA = AllAnalyses.BAA;
200 AAQueryInfo &AAQI = AllAnalyses.AAQI;
201 AliasResult AR = BasicAA.alias(
202 MemoryLocation(Ptr, LocationSize::precise(2)),
203 MemoryLocation(Select, LocationSize::precise(1)), AAQI, nullptr);
204 ASSERT_EQ(AR.getOffset(), 1);