blob: 93bda62f75eeacf222ff3ebbfa740bc8a83584dc [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//===----------------------------------------------------------------------===//
Daniel Berlin83fc77b2016-03-01 18:46:54 +00009#include "llvm/Transforms/Utils/MemorySSA.h"
10#include "llvm/Analysis/AliasAnalysis.h"
11#include "llvm/Analysis/BasicAliasAnalysis.h"
12#include "llvm/IR/BasicBlock.h"
Daniel Berlin14300262016-06-21 18:39:20 +000013#include "llvm/IR/DataLayout.h"
Daniel Berlin83fc77b2016-03-01 18:46:54 +000014#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
George Burgess IV1b1fef32016-04-29 18:42:55 +000022const static char DLString[] = "e-i64:64-f80:128-n8:16:32:64-S128";
Daniel Berlin83fc77b2016-03-01 18:46:54 +000023
George Burgess IV1b1fef32016-04-29 18:42:55 +000024/// There's a lot of common setup between these tests. This fixture helps reduce
25/// that. Tests should mock up a function, store it in F, and then call
26/// setupAnalyses().
27class MemorySSATest : public testing::Test {
28protected:
29 // N.B. Many of these members depend on each other (e.g. the Module depends on
30 // the Context, etc.). So, order matters here (and in TestAnalyses).
31 LLVMContext C;
32 Module M;
33 IRBuilder<> B;
34 DataLayout DL;
35 TargetLibraryInfoImpl TLII;
36 TargetLibraryInfo TLI;
37 Function *F;
38
39 // Things that we need to build after the function is created.
40 struct TestAnalyses {
41 DominatorTree DT;
42 AssumptionCache AC;
43 AAResults AA;
44 BasicAAResult BAA;
45 MemorySSA MSSA;
Geoff Berryb96d3b22016-06-01 21:30:40 +000046 MemorySSAWalker *Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +000047
48 TestAnalyses(MemorySSATest &Test)
49 : DT(*Test.F), AC(*Test.F), AA(Test.TLI),
Geoff Berryb96d3b22016-06-01 21:30:40 +000050 BAA(Test.DL, Test.TLI, AC, &DT), MSSA(*Test.F, &AA, &DT) {
George Burgess IV1b1fef32016-04-29 18:42:55 +000051 AA.addAAResult(BAA);
Geoff Berryb96d3b22016-06-01 21:30:40 +000052 Walker = MSSA.getWalker();
George Burgess IV1b1fef32016-04-29 18:42:55 +000053 }
54 };
55
56 std::unique_ptr<TestAnalyses> Analyses;
57
58 void setupAnalyses() {
59 assert(F);
60 Analyses.reset(new TestAnalyses(*this));
61 }
62
63public:
64 MemorySSATest()
65 : M("MemorySSATest", C), B(C), DL(DLString), TLI(TLII), F(nullptr) {}
66};
67
Daniel Berlin14300262016-06-21 18:39:20 +000068TEST_F(MemorySSATest, CreateALoadAndPhi) {
69 // We create a diamond where there is a store on one side, and then after
Daniel Berlincdda3ce2016-07-31 21:08:10 +000070 // building MemorySSA, create a load after the merge point, and use it to test
Daniel Berlin14300262016-06-21 18:39:20 +000071 // updating by creating an access for the load and a memoryphi.
72 F = Function::Create(
73 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
74 GlobalValue::ExternalLinkage, "F", &M);
75 BasicBlock *Entry(BasicBlock::Create(C, "", F));
76 BasicBlock *Left(BasicBlock::Create(C, "", F));
77 BasicBlock *Right(BasicBlock::Create(C, "", F));
78 BasicBlock *Merge(BasicBlock::Create(C, "", F));
79 B.SetInsertPoint(Entry);
80 B.CreateCondBr(B.getTrue(), Left, Right);
81 B.SetInsertPoint(Left);
82 Argument *PointerArg = &*F->arg_begin();
83 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
84 BranchInst::Create(Merge, Left);
85 BranchInst::Create(Merge, Right);
86
87 setupAnalyses();
88 MemorySSA &MSSA = Analyses->MSSA;
89 // Add the load
90 B.SetInsertPoint(Merge);
91 LoadInst *LoadInst = B.CreateLoad(PointerArg);
92 // Should be no phi to start
93 EXPECT_EQ(MSSA.getMemoryAccess(Merge), nullptr);
94
95 // Create the phi
96 MemoryPhi *MP = MSSA.createMemoryPhi(Merge);
97 MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
98 MP->addIncoming(StoreAccess, Left);
99 MP->addIncoming(MSSA.getLiveOnEntryDef(), Right);
100
101 // Create the load memory acccess
102 MemoryUse *LoadAccess = cast<MemoryUse>(
103 MSSA.createMemoryAccessInBB(LoadInst, MP, Merge, MemorySSA::Beginning));
104 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
105 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
106 MSSA.verifyMemorySSA();
107}
108
Daniel Berlin5130cc82016-07-31 21:08:20 +0000109TEST_F(MemorySSATest, MoveAStore) {
110 // We create a diamond where there is a in the entry, a store on one side, and
111 // a load at the end. After building MemorySSA, we test updating by moving
112 // the store from the side block to the entry block.
113 F = Function::Create(
114 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
115 GlobalValue::ExternalLinkage, "F", &M);
116 BasicBlock *Entry(BasicBlock::Create(C, "", F));
117 BasicBlock *Left(BasicBlock::Create(C, "", F));
118 BasicBlock *Right(BasicBlock::Create(C, "", F));
119 BasicBlock *Merge(BasicBlock::Create(C, "", F));
120 B.SetInsertPoint(Entry);
121 Argument *PointerArg = &*F->arg_begin();
122 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
123 B.CreateCondBr(B.getTrue(), Left, Right);
124 B.SetInsertPoint(Left);
125 StoreInst *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
126 BranchInst::Create(Merge, Left);
127 BranchInst::Create(Merge, Right);
128 B.SetInsertPoint(Merge);
129 B.CreateLoad(PointerArg);
130 setupAnalyses();
131 MemorySSA &MSSA = Analyses->MSSA;
132
133 // Move the store
134 SideStore->moveBefore(Entry->getTerminator());
135 MemoryAccess *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
136 MemoryAccess *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
137 MemoryAccess *NewStoreAccess = MSSA.createMemoryAccessAfter(SideStore,
138 EntryStoreAccess,
139 EntryStoreAccess);
140 EntryStoreAccess->replaceAllUsesWith(NewStoreAccess);
141 MSSA.removeMemoryAccess(SideStoreAccess);
142 MSSA.verifyMemorySSA();
143}
144
Daniel Berlin14300262016-06-21 18:39:20 +0000145TEST_F(MemorySSATest, RemoveAPhi) {
146 // We create a diamond where there is a store on one side, and then a load
147 // after the merge point. This enables us to test a bunch of different
148 // removal cases.
149 F = Function::Create(
150 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
151 GlobalValue::ExternalLinkage, "F", &M);
152 BasicBlock *Entry(BasicBlock::Create(C, "", F));
153 BasicBlock *Left(BasicBlock::Create(C, "", F));
154 BasicBlock *Right(BasicBlock::Create(C, "", F));
155 BasicBlock *Merge(BasicBlock::Create(C, "", F));
156 B.SetInsertPoint(Entry);
157 B.CreateCondBr(B.getTrue(), Left, Right);
158 B.SetInsertPoint(Left);
159 Argument *PointerArg = &*F->arg_begin();
160 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
161 BranchInst::Create(Merge, Left);
162 BranchInst::Create(Merge, Right);
163 B.SetInsertPoint(Merge);
164 LoadInst *LoadInst = B.CreateLoad(PointerArg);
165
166 setupAnalyses();
167 MemorySSA &MSSA = Analyses->MSSA;
168 // Before, the load will be a use of a phi<store, liveonentry>.
169 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst));
170 MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
171 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
172 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
173 // Kill the store
174 MSSA.removeMemoryAccess(StoreAccess);
175 MemoryPhi *MP = cast<MemoryPhi>(DefiningAccess);
176 // Verify the phi ended up as liveonentry, liveonentry
177 for (auto &Op : MP->incoming_values())
178 EXPECT_TRUE(MSSA.isLiveOnEntryDef(cast<MemoryAccess>(Op.get())));
179 // Replace the phi uses with the live on entry def
180 MP->replaceAllUsesWith(MSSA.getLiveOnEntryDef());
181 // Verify the load is now defined by liveOnEntryDef
182 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
183 // Remove the PHI
184 MSSA.removeMemoryAccess(MP);
185 MSSA.verifyMemorySSA();
186}
187
George Burgess IV1b1fef32016-04-29 18:42:55 +0000188TEST_F(MemorySSATest, RemoveMemoryAccess) {
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000189 // We create a diamond where there is a store on one side, and then a load
190 // after the merge point. This enables us to test a bunch of different
191 // removal cases.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000192 F = Function::Create(
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000193 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
George Burgess IV1b1fef32016-04-29 18:42:55 +0000194 GlobalValue::ExternalLinkage, "F", &M);
Daniel Berlin64120022016-03-02 21:16:28 +0000195 BasicBlock *Entry(BasicBlock::Create(C, "", F));
196 BasicBlock *Left(BasicBlock::Create(C, "", F));
197 BasicBlock *Right(BasicBlock::Create(C, "", F));
198 BasicBlock *Merge(BasicBlock::Create(C, "", F));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000199 B.SetInsertPoint(Entry);
200 B.CreateCondBr(B.getTrue(), Left, Right);
201 B.SetInsertPoint(Left);
202 Argument *PointerArg = &*F->arg_begin();
203 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
204 BranchInst::Create(Merge, Left);
205 BranchInst::Create(Merge, Right);
206 B.SetInsertPoint(Merge);
207 LoadInst *LoadInst = B.CreateLoad(PointerArg);
208
George Burgess IV1b1fef32016-04-29 18:42:55 +0000209 setupAnalyses();
210 MemorySSA &MSSA = Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000211 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000212
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000213 // Before, the load will be a use of a phi<store, liveonentry>. It should be
214 // the same after.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000215 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst));
216 MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000217 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
218 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
219 // The load is currently clobbered by one of the phi arguments, so the walker
220 // should determine the clobbering access as the phi.
221 EXPECT_EQ(DefiningAccess, Walker->getClobberingMemoryAccess(LoadInst));
George Burgess IV1b1fef32016-04-29 18:42:55 +0000222 MSSA.removeMemoryAccess(StoreAccess);
223 MSSA.verifyMemorySSA();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000224 // After the removeaccess, let's see if we got the right accesses
225 // The load should still point to the phi ...
226 EXPECT_EQ(DefiningAccess, LoadAccess->getDefiningAccess());
227 // but we should now get live on entry for the clobbering definition of the
228 // load, since it will walk past the phi node since every argument is the
229 // same.
230 EXPECT_TRUE(
George Burgess IV1b1fef32016-04-29 18:42:55 +0000231 MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst)));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000232
233 // The phi should now be a two entry phi with two live on entry defs.
234 for (const auto &Op : DefiningAccess->operands()) {
235 MemoryAccess *Operand = cast<MemoryAccess>(&*Op);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000236 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Operand));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000237 }
238
239 // Now we try to remove the single valued phi
George Burgess IV1b1fef32016-04-29 18:42:55 +0000240 MSSA.removeMemoryAccess(DefiningAccess);
241 MSSA.verifyMemorySSA();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000242 // Now the load should be a load of live on entry.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000243 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
244}
245
246// We had a bug with caching where the walker would report MemoryDef#3's clobber
247// (below) was MemoryDef#1.
248//
249// define void @F(i8*) {
250// %A = alloca i8, i8 1
251// ; 1 = MemoryDef(liveOnEntry)
252// store i8 0, i8* %A
253// ; 2 = MemoryDef(1)
254// store i8 1, i8* %A
255// ; 3 = MemoryDef(2)
256// store i8 2, i8* %A
257// }
258TEST_F(MemorySSATest, TestTripleStore) {
Daniel Berlin14300262016-06-21 18:39:20 +0000259 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
260 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000261 B.SetInsertPoint(BasicBlock::Create(C, "", F));
262 Type *Int8 = Type::getInt8Ty(C);
263 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
264 StoreInst *S1 = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
265 StoreInst *S2 = B.CreateStore(ConstantInt::get(Int8, 1), Alloca);
266 StoreInst *S3 = B.CreateStore(ConstantInt::get(Int8, 2), Alloca);
267
268 setupAnalyses();
269 MemorySSA &MSSA = Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000270 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000271
272 unsigned I = 0;
273 for (StoreInst *V : {S1, S2, S3}) {
274 // Everything should be clobbered by its defining access
275 MemoryAccess *DefiningAccess =
276 cast<MemoryUseOrDef>(MSSA.getMemoryAccess(V))->getDefiningAccess();
277 MemoryAccess *WalkerClobber = Walker->getClobberingMemoryAccess(V);
278 EXPECT_EQ(DefiningAccess, WalkerClobber)
279 << "Store " << I << " doesn't have the correct clobbering access";
280 // EXPECT_EQ expands such that if we increment I above, it won't get
281 // incremented except when we try to print the error message.
282 ++I;
283 }
284}
285
286// ...And fixing the above bug made it obvious that, when walking, MemorySSA's
287// walker was caching the initial node it walked. This was fine (albeit
288// mostly redundant) unless the initial node being walked is a clobber for the
289// query. In that case, we'd cache that the node clobbered itself.
290TEST_F(MemorySSATest, TestStoreAndLoad) {
Daniel Berlin14300262016-06-21 18:39:20 +0000291 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
292 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000293 B.SetInsertPoint(BasicBlock::Create(C, "", F));
294 Type *Int8 = Type::getInt8Ty(C);
295 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
296 Instruction *SI = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
297 Instruction *LI = B.CreateLoad(Alloca);
298
299 setupAnalyses();
300 MemorySSA &MSSA = Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000301 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000302
303 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LI);
304 EXPECT_EQ(LoadClobber, MSSA.getMemoryAccess(SI));
305 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(SI)));
306}
307
308// Another bug (related to the above two fixes): It was noted that, given the
309// following code:
310// ; 1 = MemoryDef(liveOnEntry)
311// store i8 0, i8* %1
312//
313// ...A query to getClobberingMemoryAccess(MemoryAccess*, MemoryLocation) would
314// hand back the store (correctly). A later call to
315// getClobberingMemoryAccess(const Instruction*) would also hand back the store
316// (incorrectly; it should return liveOnEntry).
317//
318// This test checks that repeated calls to either function returns what they're
319// meant to.
320TEST_F(MemorySSATest, TestStoreDoubleQuery) {
Daniel Berlin14300262016-06-21 18:39:20 +0000321 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
322 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000323 B.SetInsertPoint(BasicBlock::Create(C, "", F));
324 Type *Int8 = Type::getInt8Ty(C);
325 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
326 StoreInst *SI = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
327
328 setupAnalyses();
329 MemorySSA &MSSA = Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000330 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000331
332 MemoryAccess *StoreAccess = MSSA.getMemoryAccess(SI);
333 MemoryLocation StoreLoc = MemoryLocation::get(SI);
Daniel Berlin14300262016-06-21 18:39:20 +0000334 MemoryAccess *Clobber =
335 Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000336 MemoryAccess *LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
337
338 EXPECT_EQ(Clobber, StoreAccess);
339 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LiveOnEntry));
340
341 // Try again (with entries in the cache already) for good measure...
342 Clobber = Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
343 LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
344 EXPECT_EQ(Clobber, StoreAccess);
345 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LiveOnEntry));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000346}