1 //===- BasicAliasAnalysisTest.cpp - Unit tests for BasicAA ----------------===//
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
7 //===----------------------------------------------------------------------===//
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/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"
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";
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
30 class BasicAATest
: public testing::Test
{
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).
38 TargetLibraryInfoImpl TLII
;
39 TargetLibraryInfo TLI
;
42 // Things that we need to build after the function is created.
48 TestAnalyses(BasicAATest
&Test
)
49 : DT(*Test
.F
), AC(*Test
.F
), BAA(Test
.DL
, *Test
.F
, Test
.TLI
, AC
, &DT
) {}
52 llvm::Optional
<TestAnalyses
> Analyses
;
54 BasicAAResult
&setupAnalyses() {
56 Analyses
.emplace(*this);
62 : M("BasicAATest", C
), B(C
), DL(DLString
), TLI(TLII
), F(nullptr) {}
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
68 TEST_F(BasicAATest
, AliasInstWithObjectOfImpreciseSize
) {
70 FunctionType::get(B
.getVoidTy(), {B
.getInt32Ty()->getPointerTo()}, false),
71 GlobalValue::ExternalLinkage
, "F", &M
);
73 BasicBlock
*Entry(BasicBlock::Create(C
, "", F
));
74 B
.SetInsertPoint(Entry
);
76 Value
*IncomingI32Ptr
= F
->arg_begin();
79 cast
<GlobalVariable
>(M
.getOrInsertGlobal("some_global", B
.getInt8Ty()));
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));
86 BasicAAResult
&BasicAA
= setupAnalyses();
88 BasicAA
.alias(MemoryLocation(IncomingI32Ptr
, LocationSize::precise(4)),
89 MemoryLocation(GlobalPtr
, LocationSize::precise(1))),
90 AliasResult::NoAlias
);
93 BasicAA
.alias(MemoryLocation(IncomingI32Ptr
, LocationSize::upperBound(4)),
94 MemoryLocation(GlobalPtr
, LocationSize::precise(1))),
95 AliasResult::MayAlias
);
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
);
105 BasicBlock
*Entry(BasicBlock::Create(C
, "", F
));
106 B
.SetInsertPoint(Entry
);
108 Value
*ArbitraryI32
= F
->arg_begin();
109 AllocaInst
*I8
= B
.CreateAlloca(B
.getInt8Ty(), B
.getInt32(2));
110 auto *I8AtUncertainOffset
=
111 cast
<GetElementPtrInst
>(B
.CreateGEP(B
.getInt8Ty(), I8
, ArbitraryI32
));
113 BasicAAResult
&BasicAA
= setupAnalyses();
114 ASSERT_EQ(BasicAA
.alias(
115 MemoryLocation(I8
, LocationSize::precise(2)),
116 MemoryLocation(I8AtUncertainOffset
, LocationSize::precise(1))),
117 AliasResult::PartialAlias
);
119 ASSERT_EQ(BasicAA
.alias(
120 MemoryLocation(I8
, LocationSize::upperBound(2)),
121 MemoryLocation(I8AtUncertainOffset
, LocationSize::precise(1))),
122 AliasResult::MayAlias
);