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/BasicAliasAnalysis.h"
12 #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"
24 class AliasAnalysisTest : public testing::Test {
26 AliasAnalysisTest() : M("AliasAnalysisTBAATest", C) {}
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
31 void CheckModRef(Instruction *I, ModRefInfo Result) {
33 class CheckModRefTestPass : public FunctionPass {
35 CheckModRefTestPass(Instruction *I, ModRefInfo Result)
36 : FunctionPass(ID), ExpectResult(Result), I(I) {}
37 static int initialize() {
38 PassInfo *PI = new PassInfo("CheckModRef testing pass", "", &ID,
40 PassRegistry::getPassRegistry()->registerPass(*PI, false);
41 initializeAliasAnalysisAnalysisGroup(*PassRegistry::getPassRegistry());
42 initializeBasicAliasAnalysisPass(*PassRegistry::getPassRegistry());
45 void getAnalysisUsage(AnalysisUsage &AU) const override {
47 AU.addRequiredTransitive<AliasAnalysis>();
49 bool runOnFunction(Function &) override {
50 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
51 EXPECT_EQ(AA.getModRefInfo(I, MemoryLocation()), ExpectResult);
52 EXPECT_EQ(AA.getModRefInfo(I), ExpectResult);
55 ModRefInfo ExpectResult;
58 static int initialize = CheckModRefTestPass::initialize();
60 CheckModRefTestPass *P = new CheckModRefTestPass(I, Result);
61 legacy::PassManager PM;
62 PM.add(createBasicAliasAnalysisPass());
71 TEST_F(AliasAnalysisTest, getModRefInfo) {
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);
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);
85 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);
90 new AtomicRMWInst(AtomicRMWInst::Xchg, Addr, ConstantInt::get(IntType, 1),
91 Monotonic, CrossThread, BB);
93 ReturnInst::Create(C, nullptr, BB);
95 // Check basic results
96 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);
104 } // end anonymous namspace
105 } // end llvm namespace