1 //===- BasicAliasAnalysisTest.cpp - Unit tests for BasicAA ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Targeted tests that are hard/convoluted to make happen with just `opt`.
13 #include "llvm/Analysis/BasicAliasAnalysis.h"
14 #include "llvm/Analysis/AliasAnalysis.h"
15 #include "llvm/AsmParser/Parser.h"
16 #include "llvm/IR/Dominators.h"
17 #include "llvm/IR/IRBuilder.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "gtest/gtest.h"
25 // FIXME: This is duplicated between this file and MemorySSATest. Refactor.
26 const static char DLString
[] = "e-i64:64-f80:128-n8:16:32:64-S128";
28 /// There's a lot of common setup between these tests. This fixture helps reduce
29 /// that. Tests should mock up a function, store it in F, and then call
31 class BasicAATest
: public testing::Test
{
33 // N.B. Many of these members depend on each other (e.g. the Module depends on
34 // the Context, etc.). So, order matters here (and in TestAnalyses).
39 TargetLibraryInfoImpl TLII
;
40 TargetLibraryInfo TLI
;
43 // Things that we need to build after the function is created.
49 TestAnalyses(BasicAATest
&Test
)
50 : DT(*Test
.F
), AC(*Test
.F
), BAA(Test
.DL
, *Test
.F
, Test
.TLI
, AC
, &DT
) {}
53 llvm::Optional
<TestAnalyses
> Analyses
;
55 BasicAAResult
&setupAnalyses() {
57 Analyses
.emplace(*this);
63 : M("BasicAATest", C
), B(C
), DL(DLString
), TLI(TLII
), F(nullptr) {}
66 // Check that a function arg can't trivially alias a global when we're accessing
67 // >sizeof(global) bytes through that arg, unless the access size is just an
69 TEST_F(BasicAATest
, AliasInstWithObjectOfImpreciseSize
) {
71 FunctionType::get(B
.getVoidTy(), {B
.getInt32Ty()->getPointerTo()}, false),
72 GlobalValue::ExternalLinkage
, "F", &M
);
74 BasicBlock
*Entry(BasicBlock::Create(C
, "", F
));
75 B
.SetInsertPoint(Entry
);
77 Value
*IncomingI32Ptr
= F
->arg_begin();
80 cast
<GlobalVariable
>(M
.getOrInsertGlobal("some_global", B
.getInt8Ty()));
82 // Without sufficiently restricted linkage/an init, some of the object size
83 // checking bits get more conservative.
84 GlobalPtr
->setLinkage(GlobalValue::LinkageTypes::InternalLinkage
);
85 GlobalPtr
->setInitializer(B
.getInt8(0));
87 BasicAAResult
&BasicAA
= setupAnalyses();
89 BasicAA
.alias(MemoryLocation(IncomingI32Ptr
, LocationSize::precise(4)),
90 MemoryLocation(GlobalPtr
, LocationSize::precise(1))),
91 AliasResult::NoAlias
);
94 BasicAA
.alias(MemoryLocation(IncomingI32Ptr
, LocationSize::upperBound(4)),
95 MemoryLocation(GlobalPtr
, LocationSize::precise(1))),
96 AliasResult::MayAlias
);
99 // Check that we fall back to MayAlias if we see an access of an entire object
100 // that's just an upper-bound.
101 TEST_F(BasicAATest
, AliasInstWithFullObjectOfImpreciseSize
) {
102 F
= Function::Create(
103 FunctionType::get(B
.getVoidTy(), {B
.getInt64Ty()}, false),
104 GlobalValue::ExternalLinkage
, "F", &M
);
106 BasicBlock
*Entry(BasicBlock::Create(C
, "", F
));
107 B
.SetInsertPoint(Entry
);
109 Value
*ArbitraryI32
= F
->arg_begin();
110 AllocaInst
*I8
= B
.CreateAlloca(B
.getInt8Ty(), B
.getInt32(2));
111 auto *I8AtUncertainOffset
=
112 cast
<GetElementPtrInst
>(B
.CreateGEP(I8
, ArbitraryI32
));
114 BasicAAResult
&BasicAA
= setupAnalyses();
115 ASSERT_EQ(BasicAA
.alias(
116 MemoryLocation(I8
, LocationSize::precise(2)),
117 MemoryLocation(I8AtUncertainOffset
, LocationSize::precise(1))),
118 AliasResult::PartialAlias
);
120 ASSERT_EQ(BasicAA
.alias(
121 MemoryLocation(I8
, LocationSize::upperBound(2)),
122 MemoryLocation(I8AtUncertainOffset
, LocationSize::precise(1))),
123 AliasResult::MayAlias
);