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.
49 TestAnalyses(BasicAATest
&Test
)
50 : DT(*Test
.F
), AC(*Test
.F
), BAA(Test
.DL
, *Test
.F
, Test
.TLI
, AC
, &DT
),
54 llvm::Optional
<TestAnalyses
> Analyses
;
56 TestAnalyses
&setupAnalyses() {
58 Analyses
.emplace(*this);
59 return Analyses
.getValue();
64 : M("BasicAATest", C
), B(C
), DL(DLString
), TLI(TLII
), F(nullptr) {}
67 // Check that a function arg can't trivially alias a global when we're accessing
68 // >sizeof(global) bytes through that arg, unless the access size is just an
70 TEST_F(BasicAATest
, AliasInstWithObjectOfImpreciseSize
) {
72 FunctionType::get(B
.getVoidTy(), {B
.getInt32Ty()->getPointerTo()}, false),
73 GlobalValue::ExternalLinkage
, "F", &M
);
75 BasicBlock
*Entry(BasicBlock::Create(C
, "", F
));
76 B
.SetInsertPoint(Entry
);
78 Value
*IncomingI32Ptr
= F
->arg_begin();
81 cast
<GlobalVariable
>(M
.getOrInsertGlobal("some_global", B
.getInt8Ty()));
83 // Without sufficiently restricted linkage/an init, some of the object size
84 // checking bits get more conservative.
85 GlobalPtr
->setLinkage(GlobalValue::LinkageTypes::InternalLinkage
);
86 GlobalPtr
->setInitializer(B
.getInt8(0));
88 auto &AllAnalyses
= setupAnalyses();
89 BasicAAResult
&BasicAA
= AllAnalyses
.BAA
;
90 AAQueryInfo
&AAQI
= AllAnalyses
.AAQI
;
92 BasicAA
.alias(MemoryLocation(IncomingI32Ptr
, LocationSize::precise(4)),
93 MemoryLocation(GlobalPtr
, LocationSize::precise(1)), AAQI
),
94 AliasResult::NoAlias
);
97 BasicAA
.alias(MemoryLocation(IncomingI32Ptr
, LocationSize::upperBound(4)),
98 MemoryLocation(GlobalPtr
, LocationSize::precise(1)), AAQI
),
99 AliasResult::MayAlias
);
102 // Check that we fall back to MayAlias if we see an access of an entire object
103 // that's just an upper-bound.
104 TEST_F(BasicAATest
, AliasInstWithFullObjectOfImpreciseSize
) {
105 F
= Function::Create(
106 FunctionType::get(B
.getVoidTy(), {B
.getInt64Ty()}, false),
107 GlobalValue::ExternalLinkage
, "F", &M
);
109 BasicBlock
*Entry(BasicBlock::Create(C
, "", F
));
110 B
.SetInsertPoint(Entry
);
112 Value
*ArbitraryI32
= F
->arg_begin();
113 AllocaInst
*I8
= B
.CreateAlloca(B
.getInt8Ty(), B
.getInt32(2));
114 auto *I8AtUncertainOffset
=
115 cast
<GetElementPtrInst
>(B
.CreateGEP(B
.getInt8Ty(), I8
, ArbitraryI32
));
117 auto &AllAnalyses
= setupAnalyses();
118 BasicAAResult
&BasicAA
= AllAnalyses
.BAA
;
119 AAQueryInfo
&AAQI
= AllAnalyses
.AAQI
;
120 ASSERT_EQ(BasicAA
.alias(
121 MemoryLocation(I8
, LocationSize::precise(2)),
122 MemoryLocation(I8AtUncertainOffset
, LocationSize::precise(1)),
124 AliasResult::PartialAlias
);
126 ASSERT_EQ(BasicAA
.alias(
127 MemoryLocation(I8
, LocationSize::upperBound(2)),
128 MemoryLocation(I8AtUncertainOffset
, LocationSize::precise(1)),
130 AliasResult::MayAlias
);