blob: ceee21a70cfa4ec591436de6c653ae2259e8d130 [file] [log] [blame]
Daniel Berlin83fc77b2016-03-01 18:46:54 +00001//===- MemorySSA.cpp - Unit tests for MemorySSA ---------------------------===//
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#include "llvm/IR/DataLayout.h"
10#include "llvm/Transforms/Utils/MemorySSA.h"
11#include "llvm/Analysis/AliasAnalysis.h"
12#include "llvm/Analysis/BasicAliasAnalysis.h"
13#include "llvm/IR/BasicBlock.h"
14#include "llvm/IR/Dominators.h"
15#include "llvm/IR/IRBuilder.h"
16#include "llvm/IR/Instructions.h"
17#include "llvm/IR/LLVMContext.h"
18#include "gtest/gtest.h"
19
20using namespace llvm;
21
22TEST(MemorySSA, RemoveMemoryAccess) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000023 LLVMContext C;
Daniel Berlin64120022016-03-02 21:16:28 +000024 std::unique_ptr<Module> M(new Module("Remove memory access", C));
Daniel Berlin83fc77b2016-03-01 18:46:54 +000025 IRBuilder<> B(C);
26 DataLayout DL("e-i64:64-f80:128-n8:16:32:64-S128");
27 TargetLibraryInfoImpl TLII;
28 TargetLibraryInfo TLI(TLII);
29
30 // We create a diamond where there is a store on one side, and then a load
31 // after the merge point. This enables us to test a bunch of different
32 // removal cases.
Daniel Berlin64120022016-03-02 21:16:28 +000033 Function *F = Function::Create(
Daniel Berlin83fc77b2016-03-01 18:46:54 +000034 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
Daniel Berlin64120022016-03-02 21:16:28 +000035 GlobalValue::ExternalLinkage, "F", M.get());
36 BasicBlock *Entry(BasicBlock::Create(C, "", F));
37 BasicBlock *Left(BasicBlock::Create(C, "", F));
38 BasicBlock *Right(BasicBlock::Create(C, "", F));
39 BasicBlock *Merge(BasicBlock::Create(C, "", F));
Daniel Berlin83fc77b2016-03-01 18:46:54 +000040 B.SetInsertPoint(Entry);
41 B.CreateCondBr(B.getTrue(), Left, Right);
42 B.SetInsertPoint(Left);
43 Argument *PointerArg = &*F->arg_begin();
44 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
45 BranchInst::Create(Merge, Left);
46 BranchInst::Create(Merge, Right);
47 B.SetInsertPoint(Merge);
48 LoadInst *LoadInst = B.CreateLoad(PointerArg);
49
50 std::unique_ptr<MemorySSA> MSSA(new MemorySSA(*F));
51 std::unique_ptr<DominatorTree> DT(new DominatorTree(*F));
52 std::unique_ptr<AssumptionCache> AC(new AssumptionCache(*F));
Daniel Berlin64120022016-03-02 21:16:28 +000053 AAResults AA(TLI);
54 BasicAAResult BAA(DL, TLI, *AC, &*DT);
55 AA.addAAResult(BAA);
56 std::unique_ptr<MemorySSAWalker> Walker(MSSA->buildMemorySSA(&AA, &*DT));
Daniel Berlin83fc77b2016-03-01 18:46:54 +000057 // Before, the load will be a use of a phi<store, liveonentry>. It should be
58 // the same after.
59 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA->getMemoryAccess(LoadInst));
60 MemoryDef *StoreAccess = cast<MemoryDef>(MSSA->getMemoryAccess(StoreInst));
61 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
62 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
63 // The load is currently clobbered by one of the phi arguments, so the walker
64 // should determine the clobbering access as the phi.
65 EXPECT_EQ(DefiningAccess, Walker->getClobberingMemoryAccess(LoadInst));
66 MSSA->removeMemoryAccess(StoreAccess);
67 MSSA->verifyMemorySSA();
68 // After the removeaccess, let's see if we got the right accesses
69 // The load should still point to the phi ...
70 EXPECT_EQ(DefiningAccess, LoadAccess->getDefiningAccess());
71 // but we should now get live on entry for the clobbering definition of the
72 // load, since it will walk past the phi node since every argument is the
73 // same.
74 EXPECT_TRUE(
75 MSSA->isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst)));
76
77 // The phi should now be a two entry phi with two live on entry defs.
78 for (const auto &Op : DefiningAccess->operands()) {
79 MemoryAccess *Operand = cast<MemoryAccess>(&*Op);
80 EXPECT_TRUE(MSSA->isLiveOnEntryDef(Operand));
81 }
82
83 // Now we try to remove the single valued phi
84 MSSA->removeMemoryAccess(DefiningAccess);
85 MSSA->verifyMemorySSA();
86 // Now the load should be a load of live on entry.
87 EXPECT_TRUE(MSSA->isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
88}