[PM/AA] Remove the Location typedef from the AliasAnalysis class now
[oota-llvm.git] / unittests / Analysis / AliasAnalysisTest.cpp
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:
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.
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, MemoryLocation()), 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
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   auto *VAArg1 = new VAArgInst(Addr, PtrType, "vaarg", BB);
85   auto *CmpXChg1 = new AtomicCmpXchgInst(Addr, ConstantInt::get(IntType, 0),
86                                          ConstantInt::get(IntType, 1),
87                                          Monotonic, Monotonic, CrossThread, BB);
88   auto *AtomicRMW =
89       new AtomicRMWInst(AtomicRMWInst::Xchg, Addr, ConstantInt::get(IntType, 1),
90                         Monotonic, CrossThread, BB);
91
92   ReturnInst::Create(C, nullptr, BB);
93
94   // Check basic results
95   CheckModRef(Store1, AliasAnalysis::ModRefResult::Mod);
96   CheckModRef(Load1, AliasAnalysis::ModRefResult::Ref);
97   CheckModRef(Add1, AliasAnalysis::ModRefResult::NoModRef);
98   CheckModRef(VAArg1, AliasAnalysis::ModRefResult::ModRef);
99   CheckModRef(CmpXChg1, AliasAnalysis::ModRefResult::ModRef);
100   CheckModRef(AtomicRMW, AliasAnalysis::ModRefResult::ModRef);
101 }
102
103 } // end anonymous namspace
104 } // end llvm namespace