blob: 3dc64fadbacafb93b4022cb9f96b60338133ac3f [file] [log] [blame]
Daniel Berlinb2d22762015-04-13 23:05:45 +00001//===--- 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"
Chandler Carruth17e0bc32015-08-06 07:33:15 +000011#include "llvm/Analysis/BasicAliasAnalysis.h"
Daniel Berlinb2d22762015-04-13 23:05:45 +000012#include "llvm/Analysis/Passes.h"
13#include "llvm/IR/Constants.h"
14#include "llvm/IR/Instructions.h"
15#include "llvm/IR/LLVMContext.h"
16#include "llvm/IR/Module.h"
17#include "llvm/IR/LegacyPassManager.h"
18#include "llvm/Support/CommandLine.h"
19#include "gtest/gtest.h"
20
21namespace llvm {
22namespace {
23
24class AliasAnalysisTest : public testing::Test {
25protected:
26 AliasAnalysisTest() : M("AliasAnalysisTBAATest", C) {}
27
28 // This is going to check that calling getModRefInfo without a location, and
29 // with a default location, first, doesn't crash, and second, gives the right
30 // answer.
Chandler Carruth194f59c2015-07-22 23:15:57 +000031 void CheckModRef(Instruction *I, ModRefInfo Result) {
Daniel Berlinb2d22762015-04-13 23:05:45 +000032 static char ID;
33 class CheckModRefTestPass : public FunctionPass {
34 public:
Chandler Carruth194f59c2015-07-22 23:15:57 +000035 CheckModRefTestPass(Instruction *I, ModRefInfo Result)
Daniel Berlinb2d22762015-04-13 23:05:45 +000036 : FunctionPass(ID), ExpectResult(Result), I(I) {}
37 static int initialize() {
38 PassInfo *PI = new PassInfo("CheckModRef testing pass", "", &ID,
39 nullptr, true, true);
40 PassRegistry::getPassRegistry()->registerPass(*PI, false);
41 initializeAliasAnalysisAnalysisGroup(*PassRegistry::getPassRegistry());
42 initializeBasicAliasAnalysisPass(*PassRegistry::getPassRegistry());
43 return 0;
44 }
45 void getAnalysisUsage(AnalysisUsage &AU) const override {
46 AU.setPreservesAll();
47 AU.addRequiredTransitive<AliasAnalysis>();
48 }
49 bool runOnFunction(Function &) override {
50 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Chandler Carruthac80dc72015-06-17 07:18:54 +000051 EXPECT_EQ(AA.getModRefInfo(I, MemoryLocation()), ExpectResult);
Daniel Berlinb2d22762015-04-13 23:05:45 +000052 EXPECT_EQ(AA.getModRefInfo(I), ExpectResult);
53 return false;
54 }
Chandler Carruth194f59c2015-07-22 23:15:57 +000055 ModRefInfo ExpectResult;
Daniel Berlinb2d22762015-04-13 23:05:45 +000056 Instruction *I;
57 };
58 static int initialize = CheckModRefTestPass::initialize();
59 (void)initialize;
60 CheckModRefTestPass *P = new CheckModRefTestPass(I, Result);
61 legacy::PassManager PM;
62 PM.add(createBasicAliasAnalysisPass());
63 PM.add(P);
64 PM.run(M);
65 }
66
67 LLVMContext C;
68 Module M;
69};
70
71TEST_F(AliasAnalysisTest, getModRefInfo) {
72 // Setup function.
73 FunctionType *FTy =
74 FunctionType::get(Type::getVoidTy(C), std::vector<Type *>(), false);
75 auto *F = cast<Function>(M.getOrInsertFunction("f", FTy));
76 auto *BB = BasicBlock::Create(C, "entry", F);
77 auto IntType = Type::getInt32Ty(C);
78 auto PtrType = Type::getInt32PtrTy(C);
79 auto *Value = ConstantInt::get(IntType, 42);
80 auto *Addr = ConstantPointerNull::get(PtrType);
81
82 auto *Store1 = new StoreInst(Value, Addr, BB);
83 auto *Load1 = new LoadInst(Addr, "load", BB);
84 auto *Add1 = BinaryOperator::CreateAdd(Value, Value, "add", BB);
Daniel Berlinec1de3f2015-04-28 19:19:14 +000085 auto *VAArg1 = new VAArgInst(Addr, PtrType, "vaarg", BB);
86 auto *CmpXChg1 = new AtomicCmpXchgInst(Addr, ConstantInt::get(IntType, 0),
87 ConstantInt::get(IntType, 1),
88 Monotonic, Monotonic, CrossThread, BB);
89 auto *AtomicRMW =
90 new AtomicRMWInst(AtomicRMWInst::Xchg, Addr, ConstantInt::get(IntType, 1),
91 Monotonic, CrossThread, BB);
Daniel Berlinb2d22762015-04-13 23:05:45 +000092
93 ReturnInst::Create(C, nullptr, BB);
94
95 // Check basic results
Chandler Carruth194f59c2015-07-22 23:15:57 +000096 CheckModRef(Store1, MRI_Mod);
97 CheckModRef(Load1, MRI_Ref);
98 CheckModRef(Add1, MRI_NoModRef);
99 CheckModRef(VAArg1, MRI_ModRef);
100 CheckModRef(CmpXChg1, MRI_ModRef);
101 CheckModRef(AtomicRMW, MRI_ModRef);
Daniel Berlinb2d22762015-04-13 23:05:45 +0000102}
103
104} // end anonymous namspace
105} // end llvm namespace