[yaml2obj/obj2yaml] - Do not trigger llvm_unreachable when dumping/parsing relocation...
[llvm-complete.git] / unittests / Analysis / BasicAliasAnalysisTest.cpp
blobf69d2d14a6c0bb35c53de69aa5aef0a5b1be95dd
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/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"
22 using namespace llvm;
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
29 /// setupAnalyses().
30 class BasicAATest : public testing::Test {
31 protected:
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;
42 // Things that we need to build after the function is created.
43 struct TestAnalyses {
44 DominatorTree DT;
45 AssumptionCache AC;
46 BasicAAResult BAA;
47 AAQueryInfo AAQI;
49 TestAnalyses(BasicAATest &Test)
50 : DT(*Test.F), AC(*Test.F), BAA(Test.DL, *Test.F, Test.TLI, AC, &DT),
51 AAQI() {}
54 llvm::Optional<TestAnalyses> Analyses;
56 TestAnalyses &setupAnalyses() {
57 assert(F);
58 Analyses.emplace(*this);
59 return Analyses.getValue();
62 public:
63 BasicAATest()
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
69 // upper-bound.
70 TEST_F(BasicAATest, AliasInstWithObjectOfImpreciseSize) {
71 F = Function::Create(
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();
80 auto *GlobalPtr =
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;
91 ASSERT_EQ(
92 BasicAA.alias(MemoryLocation(IncomingI32Ptr, LocationSize::precise(4)),
93 MemoryLocation(GlobalPtr, LocationSize::precise(1)), AAQI),
94 AliasResult::NoAlias);
96 ASSERT_EQ(
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)),
123 AAQI),
124 AliasResult::PartialAlias);
126 ASSERT_EQ(BasicAA.alias(
127 MemoryLocation(I8, LocationSize::upperBound(2)),
128 MemoryLocation(I8AtUncertainOffset, LocationSize::precise(1)),
129 AAQI),
130 AliasResult::MayAlias);