blob: f69d2d14a6c0bb35c53de69aa5aef0a5b1be95dd [file] [log] [blame]
George Burgess IV40dc63e2018-10-10 06:39:40 +00001//===- BasicAliasAnalysisTest.cpp - Unit tests for BasicAA ----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
George Burgess IV40dc63e2018-10-10 06:39:40 +00006//
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/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"
21
22using namespace llvm;
23
24// FIXME: This is duplicated between this file and MemorySSATest. Refactor.
25const static char DLString[] = "e-i64:64-f80:128-n8:16:32:64-S128";
26
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
29/// setupAnalyses().
30class BasicAATest : public testing::Test {
31protected:
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).
34 LLVMContext C;
35 Module M;
36 IRBuilder<> B;
37 DataLayout DL;
38 TargetLibraryInfoImpl TLII;
39 TargetLibraryInfo TLI;
40 Function *F;
41
42 // Things that we need to build after the function is created.
43 struct TestAnalyses {
44 DominatorTree DT;
45 AssumptionCache AC;
46 BasicAAResult BAA;
Alina Sbirleabfc779e2019-03-22 17:22:19 +000047 AAQueryInfo AAQI;
George Burgess IV40dc63e2018-10-10 06:39:40 +000048
49 TestAnalyses(BasicAATest &Test)
Alina Sbirleabfc779e2019-03-22 17:22:19 +000050 : DT(*Test.F), AC(*Test.F), BAA(Test.DL, *Test.F, Test.TLI, AC, &DT),
51 AAQI() {}
George Burgess IV40dc63e2018-10-10 06:39:40 +000052 };
53
54 llvm::Optional<TestAnalyses> Analyses;
55
Alina Sbirleabfc779e2019-03-22 17:22:19 +000056 TestAnalyses &setupAnalyses() {
George Burgess IV40dc63e2018-10-10 06:39:40 +000057 assert(F);
58 Analyses.emplace(*this);
Alina Sbirleabfc779e2019-03-22 17:22:19 +000059 return Analyses.getValue();
George Burgess IV40dc63e2018-10-10 06:39:40 +000060 }
61
62public:
63 BasicAATest()
64 : M("BasicAATest", C), B(C), DL(DLString), TLI(TLII), F(nullptr) {}
65};
66
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
69// upper-bound.
70TEST_F(BasicAATest, AliasInstWithObjectOfImpreciseSize) {
71 F = Function::Create(
72 FunctionType::get(B.getVoidTy(), {B.getInt32Ty()->getPointerTo()}, false),
73 GlobalValue::ExternalLinkage, "F", &M);
74
75 BasicBlock *Entry(BasicBlock::Create(C, "", F));
76 B.SetInsertPoint(Entry);
77
78 Value *IncomingI32Ptr = F->arg_begin();
79
80 auto *GlobalPtr =
81 cast<GlobalVariable>(M.getOrInsertGlobal("some_global", B.getInt8Ty()));
82
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));
87
Alina Sbirleabfc779e2019-03-22 17:22:19 +000088 auto &AllAnalyses = setupAnalyses();
89 BasicAAResult &BasicAA = AllAnalyses.BAA;
90 AAQueryInfo &AAQI = AllAnalyses.AAQI;
George Burgess IV40dc63e2018-10-10 06:39:40 +000091 ASSERT_EQ(
92 BasicAA.alias(MemoryLocation(IncomingI32Ptr, LocationSize::precise(4)),
Alina Sbirleabfc779e2019-03-22 17:22:19 +000093 MemoryLocation(GlobalPtr, LocationSize::precise(1)), AAQI),
George Burgess IV40dc63e2018-10-10 06:39:40 +000094 AliasResult::NoAlias);
95
96 ASSERT_EQ(
97 BasicAA.alias(MemoryLocation(IncomingI32Ptr, LocationSize::upperBound(4)),
Alina Sbirleabfc779e2019-03-22 17:22:19 +000098 MemoryLocation(GlobalPtr, LocationSize::precise(1)), AAQI),
George Burgess IV40dc63e2018-10-10 06:39:40 +000099 AliasResult::MayAlias);
100}
101
102// Check that we fall back to MayAlias if we see an access of an entire object
103// that's just an upper-bound.
104TEST_F(BasicAATest, AliasInstWithFullObjectOfImpreciseSize) {
105 F = Function::Create(
106 FunctionType::get(B.getVoidTy(), {B.getInt64Ty()}, false),
107 GlobalValue::ExternalLinkage, "F", &M);
108
109 BasicBlock *Entry(BasicBlock::Create(C, "", F));
110 B.SetInsertPoint(Entry);
111
112 Value *ArbitraryI32 = F->arg_begin();
113 AllocaInst *I8 = B.CreateAlloca(B.getInt8Ty(), B.getInt32(2));
114 auto *I8AtUncertainOffset =
James Y Knight77160752019-02-01 20:44:47 +0000115 cast<GetElementPtrInst>(B.CreateGEP(B.getInt8Ty(), I8, ArbitraryI32));
George Burgess IV40dc63e2018-10-10 06:39:40 +0000116
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000117 auto &AllAnalyses = setupAnalyses();
118 BasicAAResult &BasicAA = AllAnalyses.BAA;
119 AAQueryInfo &AAQI = AllAnalyses.AAQI;
George Burgess IV40dc63e2018-10-10 06:39:40 +0000120 ASSERT_EQ(BasicAA.alias(
121 MemoryLocation(I8, LocationSize::precise(2)),
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000122 MemoryLocation(I8AtUncertainOffset, LocationSize::precise(1)),
123 AAQI),
George Burgess IV40dc63e2018-10-10 06:39:40 +0000124 AliasResult::PartialAlias);
125
126 ASSERT_EQ(BasicAA.alias(
127 MemoryLocation(I8, LocationSize::upperBound(2)),
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000128 MemoryLocation(I8AtUncertainOffset, LocationSize::precise(1)),
129 AAQI),
George Burgess IV40dc63e2018-10-10 06:39:40 +0000130 AliasResult::MayAlias);
131}