[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
[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/AssumptionCache.h"
12 #include "llvm/Analysis/BasicAliasAnalysis.h"
13 #include "llvm/Analysis/TargetLibraryInfo.h"
14 #include "llvm/IR/Constants.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "gtest/gtest.h"
20
21 namespace llvm {
22 namespace {
23
24 class AliasAnalysisTest : public testing::Test {
25 protected:
26   LLVMContext C;
27   Module M;
28   TargetLibraryInfoImpl TLII;
29   TargetLibraryInfo TLI;
30   std::unique_ptr<AssumptionCache> AC;
31   std::unique_ptr<BasicAAResult> BAR;
32   std::unique_ptr<AAResults> AAR;
33
34   AliasAnalysisTest() : M("AliasAnalysisTest", C), TLI(TLII) {}
35
36   AAResults &getAAResults(Function &F) {
37     // Reset the Function AA results first to clear out any references.
38     AAR.reset(new AAResults());
39
40     // Build the various AA results and register them.
41     AC.reset(new AssumptionCache(F));
42     BAR.reset(new BasicAAResult(M.getDataLayout(), TLI, *AC));
43     AAR->addAAResult(*BAR);
44
45     return *AAR;
46   }
47 };
48
49 TEST_F(AliasAnalysisTest, getModRefInfo) {
50   // Setup function.
51   FunctionType *FTy =
52       FunctionType::get(Type::getVoidTy(C), std::vector<Type *>(), false);
53   auto *F = cast<Function>(M.getOrInsertFunction("f", FTy));
54   auto *BB = BasicBlock::Create(C, "entry", F);
55   auto IntType = Type::getInt32Ty(C);
56   auto PtrType = Type::getInt32PtrTy(C);
57   auto *Value = ConstantInt::get(IntType, 42);
58   auto *Addr = ConstantPointerNull::get(PtrType);
59
60   auto *Store1 = new StoreInst(Value, Addr, BB);
61   auto *Load1 = new LoadInst(Addr, "load", BB);
62   auto *Add1 = BinaryOperator::CreateAdd(Value, Value, "add", BB);
63   auto *VAArg1 = new VAArgInst(Addr, PtrType, "vaarg", BB);
64   auto *CmpXChg1 = new AtomicCmpXchgInst(Addr, ConstantInt::get(IntType, 0),
65                                          ConstantInt::get(IntType, 1),
66                                          Monotonic, Monotonic, CrossThread, BB);
67   auto *AtomicRMW =
68       new AtomicRMWInst(AtomicRMWInst::Xchg, Addr, ConstantInt::get(IntType, 1),
69                         Monotonic, CrossThread, BB);
70
71   ReturnInst::Create(C, nullptr, BB);
72
73   auto &AA = getAAResults(*F);
74
75   // Check basic results
76   EXPECT_EQ(AA.getModRefInfo(Store1, MemoryLocation()), MRI_Mod);
77   EXPECT_EQ(AA.getModRefInfo(Store1), MRI_Mod);
78   EXPECT_EQ(AA.getModRefInfo(Load1, MemoryLocation()), MRI_Ref);
79   EXPECT_EQ(AA.getModRefInfo(Load1), MRI_Ref);
80   EXPECT_EQ(AA.getModRefInfo(Add1, MemoryLocation()), MRI_NoModRef);
81   EXPECT_EQ(AA.getModRefInfo(Add1), MRI_NoModRef);
82   EXPECT_EQ(AA.getModRefInfo(VAArg1, MemoryLocation()), MRI_ModRef);
83   EXPECT_EQ(AA.getModRefInfo(VAArg1), MRI_ModRef);
84   EXPECT_EQ(AA.getModRefInfo(CmpXChg1, MemoryLocation()), MRI_ModRef);
85   EXPECT_EQ(AA.getModRefInfo(CmpXChg1), MRI_ModRef);
86   EXPECT_EQ(AA.getModRefInfo(AtomicRMW, MemoryLocation()), MRI_ModRef);
87   EXPECT_EQ(AA.getModRefInfo(AtomicRMW), MRI_ModRef);
88 }
89
90 } // end anonymous namspace
91 } // end llvm namespace