1 //===--- AliasAnalysisTest.cpp - Mixed TBAA unit tests --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
23 class AliasAnalysisTest : public testing::Test {
25 AliasAnalysisTest() : M("AliasAnalysisTBAATest", C) {}
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
30 void CheckModRef(Instruction *I, AliasAnalysis::ModRefResult Result) {
32 class CheckModRefTestPass : public FunctionPass {
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,
39 PassRegistry::getPassRegistry()->registerPass(*PI, false);
40 initializeAliasAnalysisAnalysisGroup(*PassRegistry::getPassRegistry());
41 initializeBasicAliasAnalysisPass(*PassRegistry::getPassRegistry());
44 void getAnalysisUsage(AnalysisUsage &AU) const override {
46 AU.addRequiredTransitive<AliasAnalysis>();
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);
54 AliasAnalysis::ModRefResult ExpectResult;
57 static int initialize = CheckModRefTestPass::initialize();
59 CheckModRefTestPass *P = new CheckModRefTestPass(I, Result);
60 legacy::PassManager PM;
61 PM.add(createBasicAliasAnalysisPass());
70 TEST_F(AliasAnalysisTest, getModRefInfo) {
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);
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);
85 ReturnInst::Create(C, nullptr, BB);
87 // Check basic results
88 CheckModRef(Store1, AliasAnalysis::ModRefResult::Mod);
89 CheckModRef(Load1, AliasAnalysis::ModRefResult::Ref);
90 CheckModRef(Add1, AliasAnalysis::ModRefResult::NoModRef);
93 } // end anonymous namspace
94 } // end llvm namespace