1 //===--- AliasAnalysisTest.cpp - Mixed TBAA unit tests --------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Analysis/AliasAnalysis.h"
11 #include "llvm/Analysis/Passes.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/IR/LegacyPassManager.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "gtest/gtest.h"
19
20 namespace llvm {
21 namespace {
22
23 class AliasAnalysisTest : public testing::Test {
24 protected:
AliasAnalysisTest()25 AliasAnalysisTest() : M("AliasAnalysisTBAATest", C) {}
26
27 // This is going to check that calling getModRefInfo without a location, and
28 // with a default location, first, doesn't crash, and second, gives the right
29 // answer.
CheckModRef(Instruction * I,AliasAnalysis::ModRefResult Result)30 void CheckModRef(Instruction *I, AliasAnalysis::ModRefResult Result) {
31 static char ID;
32 class CheckModRefTestPass : public FunctionPass {
33 public:
34 CheckModRefTestPass(Instruction *I, AliasAnalysis::ModRefResult Result)
35 : FunctionPass(ID), ExpectResult(Result), I(I) {}
36 static int initialize() {
37 PassInfo *PI = new PassInfo("CheckModRef testing pass", "", &ID,
38 nullptr, true, true);
39 PassRegistry::getPassRegistry()->registerPass(*PI, false);
40 initializeAliasAnalysisAnalysisGroup(*PassRegistry::getPassRegistry());
41 initializeBasicAliasAnalysisPass(*PassRegistry::getPassRegistry());
42 return 0;
43 }
44 void getAnalysisUsage(AnalysisUsage &AU) const override {
45 AU.setPreservesAll();
46 AU.addRequiredTransitive<AliasAnalysis>();
47 }
48 bool runOnFunction(Function &) override {
49 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
50 EXPECT_EQ(AA.getModRefInfo(I, AliasAnalysis::Location()), ExpectResult);
51 EXPECT_EQ(AA.getModRefInfo(I), ExpectResult);
52 return false;
53 }
54 AliasAnalysis::ModRefResult ExpectResult;
55 Instruction *I;
56 };
57 static int initialize = CheckModRefTestPass::initialize();
58 (void)initialize;
59 CheckModRefTestPass *P = new CheckModRefTestPass(I, Result);
60 legacy::PassManager PM;
61 PM.add(createBasicAliasAnalysisPass());
62 PM.add(P);
63 PM.run(M);
64 }
65
66 LLVMContext C;
67 Module M;
68 };
69
TEST_F(AliasAnalysisTest,getModRefInfo)70 TEST_F(AliasAnalysisTest, getModRefInfo) {
71 // Setup function.
72 FunctionType *FTy =
73 FunctionType::get(Type::getVoidTy(C), std::vector<Type *>(), false);
74 auto *F = cast<Function>(M.getOrInsertFunction("f", FTy));
75 auto *BB = BasicBlock::Create(C, "entry", F);
76 auto IntType = Type::getInt32Ty(C);
77 auto PtrType = Type::getInt32PtrTy(C);
78 auto *Value = ConstantInt::get(IntType, 42);
79 auto *Addr = ConstantPointerNull::get(PtrType);
80
81 auto *Store1 = new StoreInst(Value, Addr, BB);
82 auto *Load1 = new LoadInst(Addr, "load", BB);
83 auto *Add1 = BinaryOperator::CreateAdd(Value, Value, "add", BB);
84
85 ReturnInst::Create(C, nullptr, BB);
86
87 // Check basic results
88 CheckModRef(Store1, AliasAnalysis::ModRefResult::Mod);
89 CheckModRef(Load1, AliasAnalysis::ModRefResult::Ref);
90 CheckModRef(Add1, AliasAnalysis::ModRefResult::NoModRef);
91 }
92
93 } // end anonymous namspace
94 } // end llvm namespace
95