Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 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" |
Chandler Carruth | 17e0bc3 | 2015-08-06 07:33:15 +0000 | [diff] [blame^] | 11 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 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" |
| 20 | |
| 21 | namespace llvm { |
| 22 | namespace { |
| 23 | |
| 24 | class AliasAnalysisTest : public testing::Test { |
| 25 | protected: |
| 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 Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 31 | void CheckModRef(Instruction *I, ModRefInfo Result) { |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 32 | static char ID; |
| 33 | class CheckModRefTestPass : public FunctionPass { |
| 34 | public: |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 35 | CheckModRefTestPass(Instruction *I, ModRefInfo Result) |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 36 | : 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 Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 51 | EXPECT_EQ(AA.getModRefInfo(I, MemoryLocation()), ExpectResult); |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 52 | EXPECT_EQ(AA.getModRefInfo(I), ExpectResult); |
| 53 | return false; |
| 54 | } |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 55 | ModRefInfo ExpectResult; |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 56 | 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 | |
| 71 | TEST_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 Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 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); |
| 89 | auto *AtomicRMW = |
| 90 | new AtomicRMWInst(AtomicRMWInst::Xchg, Addr, ConstantInt::get(IntType, 1), |
| 91 | Monotonic, CrossThread, BB); |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 92 | |
| 93 | ReturnInst::Create(C, nullptr, BB); |
| 94 | |
| 95 | // Check basic results |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 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); |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | } // end anonymous namspace |
| 105 | } // end llvm namespace |