blob: fb57acd2ec45b038ff150cd6e838c42076c61aaa [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"
Daniel Berlinae6b8b62017-01-28 01:35:02 +000018#include "llvm/Transforms/Utils/MemorySSAUpdater.h"
Daniel Berlin83fc77b2016-03-01 18:46:54 +000019#include "gtest/gtest.h"
20
21using namespace llvm;
22
George Burgess IV1b1fef32016-04-29 18:42:55 +000023const static char DLString[] = "e-i64:64-f80:128-n8:16:32:64-S128";
Daniel Berlin83fc77b2016-03-01 18:46:54 +000024
George Burgess IV1b1fef32016-04-29 18:42:55 +000025/// There's a lot of common setup between these tests. This fixture helps reduce
26/// that. Tests should mock up a function, store it in F, and then call
27/// setupAnalyses().
28class MemorySSATest : public testing::Test {
29protected:
30 // N.B. Many of these members depend on each other (e.g. the Module depends on
31 // the Context, etc.). So, order matters here (and in TestAnalyses).
32 LLVMContext C;
33 Module M;
34 IRBuilder<> B;
35 DataLayout DL;
36 TargetLibraryInfoImpl TLII;
37 TargetLibraryInfo TLI;
38 Function *F;
39
40 // Things that we need to build after the function is created.
41 struct TestAnalyses {
42 DominatorTree DT;
Daniel Jasperaec2fa32016-12-19 08:22:17 +000043 AssumptionCache AC;
George Burgess IV1b1fef32016-04-29 18:42:55 +000044 AAResults AA;
45 BasicAAResult BAA;
George Burgess IV80ed8632016-10-03 23:12:35 +000046 // We need to defer MSSA construction until AA is *entirely* set up, which
47 // requires calling addAAResult. Hence, we just use a pointer here.
48 std::unique_ptr<MemorySSA> MSSA;
Geoff Berryb96d3b22016-06-01 21:30:40 +000049 MemorySSAWalker *Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +000050
51 TestAnalyses(MemorySSATest &Test)
Daniel Jasperaec2fa32016-12-19 08:22:17 +000052 : DT(*Test.F), AC(*Test.F), AA(Test.TLI),
53 BAA(Test.DL, Test.TLI, AC, &DT) {
George Burgess IV1b1fef32016-04-29 18:42:55 +000054 AA.addAAResult(BAA);
George Burgess IV80ed8632016-10-03 23:12:35 +000055 MSSA = make_unique<MemorySSA>(*Test.F, &AA, &DT);
56 Walker = MSSA->getWalker();
George Burgess IV1b1fef32016-04-29 18:42:55 +000057 }
58 };
59
60 std::unique_ptr<TestAnalyses> Analyses;
61
62 void setupAnalyses() {
63 assert(F);
64 Analyses.reset(new TestAnalyses(*this));
65 }
66
67public:
68 MemorySSATest()
69 : M("MemorySSATest", C), B(C), DL(DLString), TLI(TLII), F(nullptr) {}
70};
71
Daniel Berlinf72ac492016-09-26 17:44:31 +000072TEST_F(MemorySSATest, CreateALoad) {
Daniel Berlin14300262016-06-21 18:39:20 +000073 // We create a diamond where there is a store on one side, and then after
Daniel Berlincdda3ce2016-07-31 21:08:10 +000074 // building MemorySSA, create a load after the merge point, and use it to test
Daniel Berlinf72ac492016-09-26 17:44:31 +000075 // updating by creating an access for the load.
Daniel Berlin14300262016-06-21 18:39:20 +000076 F = Function::Create(
77 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
78 GlobalValue::ExternalLinkage, "F", &M);
79 BasicBlock *Entry(BasicBlock::Create(C, "", F));
80 BasicBlock *Left(BasicBlock::Create(C, "", F));
81 BasicBlock *Right(BasicBlock::Create(C, "", F));
82 BasicBlock *Merge(BasicBlock::Create(C, "", F));
83 B.SetInsertPoint(Entry);
84 B.CreateCondBr(B.getTrue(), Left, Right);
85 B.SetInsertPoint(Left);
86 Argument *PointerArg = &*F->arg_begin();
Daniel Berlinf72ac492016-09-26 17:44:31 +000087 B.CreateStore(B.getInt8(16), PointerArg);
Daniel Berlin14300262016-06-21 18:39:20 +000088 BranchInst::Create(Merge, Left);
89 BranchInst::Create(Merge, Right);
90
91 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +000092 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlin14300262016-06-21 18:39:20 +000093 // Add the load
94 B.SetInsertPoint(Merge);
95 LoadInst *LoadInst = B.CreateLoad(PointerArg);
Daniel Berlin14300262016-06-21 18:39:20 +000096
Daniel Berlinf72ac492016-09-26 17:44:31 +000097 // MemoryPHI should already exist.
98 MemoryPhi *MP = MSSA.getMemoryAccess(Merge);
99 EXPECT_NE(MP, nullptr);
Daniel Berlin14300262016-06-21 18:39:20 +0000100
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}
Daniel Berlin60ead052017-01-28 01:23:13 +0000108TEST_F(MemorySSATest, CreateLoadsAndStoreUpdater) {
109 // We create a diamond, then build memoryssa with no memory accesses, and
110 // incrementally update it by inserting a store in the, entry, a load in the
111 // merge point, then a store in the branch, another load in the merge point,
112 // and then a store in the entry.
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 B.CreateCondBr(B.getTrue(), Left, Right);
122 B.SetInsertPoint(Left, Left->begin());
123 Argument *PointerArg = &*F->arg_begin();
124 B.SetInsertPoint(Left);
125 B.CreateBr(Merge);
126 B.SetInsertPoint(Right);
127 B.CreateBr(Merge);
128
129 setupAnalyses();
130 MemorySSA &MSSA = *Analyses->MSSA;
131 MemorySSAUpdater Updater(&MSSA);
132 // Add the store
133 B.SetInsertPoint(Entry, Entry->begin());
134 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
135 MemoryAccess *EntryStoreAccess = MSSA.createMemoryAccessInBB(
136 EntryStore, nullptr, Entry, MemorySSA::Beginning);
137 Updater.insertDef(cast<MemoryDef>(EntryStoreAccess));
138
139 // Add the load
140 B.SetInsertPoint(Merge, Merge->begin());
141 LoadInst *FirstLoad = B.CreateLoad(PointerArg);
142
143 // MemoryPHI should not already exist.
144 MemoryPhi *MP = MSSA.getMemoryAccess(Merge);
145 EXPECT_EQ(MP, nullptr);
146
147 // Create the load memory access
148 MemoryUse *FirstLoadAccess = cast<MemoryUse>(MSSA.createMemoryAccessInBB(
149 FirstLoad, nullptr, Merge, MemorySSA::Beginning));
150 Updater.insertUse(FirstLoadAccess);
151 // Should just have a load using the entry access, because it should discover
152 // the phi is trivial
153 EXPECT_EQ(FirstLoadAccess->getDefiningAccess(), EntryStoreAccess);
154
155 // Create a store on the left
156 // Add the store
157 B.SetInsertPoint(Left, Left->begin());
158 StoreInst *LeftStore = B.CreateStore(B.getInt8(16), PointerArg);
159 MemoryAccess *LeftStoreAccess = MSSA.createMemoryAccessInBB(
160 LeftStore, nullptr, Left, MemorySSA::Beginning);
Daniel Berlin78cbd282017-02-20 22:26:03 +0000161 Updater.insertDef(cast<MemoryDef>(LeftStoreAccess), false);
Daniel Berlin60ead052017-01-28 01:23:13 +0000162 // We don't touch existing loads, so we need to create a new one to get a phi
163 // Add the second load
164 B.SetInsertPoint(Merge, Merge->begin());
165 LoadInst *SecondLoad = B.CreateLoad(PointerArg);
166
167 // MemoryPHI should not already exist.
168 MP = MSSA.getMemoryAccess(Merge);
169 EXPECT_EQ(MP, nullptr);
170
171 // Create the load memory access
172 MemoryUse *SecondLoadAccess = cast<MemoryUse>(MSSA.createMemoryAccessInBB(
173 SecondLoad, nullptr, Merge, MemorySSA::Beginning));
174 Updater.insertUse(SecondLoadAccess);
175 // Now the load should be a phi of the entry store and the left store
176 MemoryPhi *MergePhi =
177 dyn_cast<MemoryPhi>(SecondLoadAccess->getDefiningAccess());
178 EXPECT_NE(MergePhi, nullptr);
179 EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
180 EXPECT_EQ(MergePhi->getIncomingValue(1), LeftStoreAccess);
181 // Now create a store below the existing one in the entry
182 B.SetInsertPoint(Entry, --Entry->end());
183 StoreInst *SecondEntryStore = B.CreateStore(B.getInt8(16), PointerArg);
184 MemoryAccess *SecondEntryStoreAccess = MSSA.createMemoryAccessInBB(
185 SecondEntryStore, nullptr, Entry, MemorySSA::End);
Daniel Berlin78cbd282017-02-20 22:26:03 +0000186 // Insert it twice just to test renaming
187 Updater.insertDef(cast<MemoryDef>(SecondEntryStoreAccess), false);
188 EXPECT_NE(FirstLoadAccess->getDefiningAccess(), MergePhi);
189 Updater.insertDef(cast<MemoryDef>(SecondEntryStoreAccess), true);
190 EXPECT_EQ(FirstLoadAccess->getDefiningAccess(), MergePhi);
Daniel Berlin60ead052017-01-28 01:23:13 +0000191 // and make sure the phi below it got updated, despite being blocks away
192 MergePhi = dyn_cast<MemoryPhi>(SecondLoadAccess->getDefiningAccess());
193 EXPECT_NE(MergePhi, nullptr);
194 EXPECT_EQ(MergePhi->getIncomingValue(0), SecondEntryStoreAccess);
195 EXPECT_EQ(MergePhi->getIncomingValue(1), LeftStoreAccess);
196 MSSA.verifyMemorySSA();
197}
198
199TEST_F(MemorySSATest, CreateALoadUpdater) {
200 // We create a diamond, then build memoryssa with no memory accesses, and
201 // incrementally update it by inserting a store in one of the branches, and a
202 // load in the merge point
203 F = Function::Create(
204 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
205 GlobalValue::ExternalLinkage, "F", &M);
206 BasicBlock *Entry(BasicBlock::Create(C, "", F));
207 BasicBlock *Left(BasicBlock::Create(C, "", F));
208 BasicBlock *Right(BasicBlock::Create(C, "", F));
209 BasicBlock *Merge(BasicBlock::Create(C, "", F));
210 B.SetInsertPoint(Entry);
211 B.CreateCondBr(B.getTrue(), Left, Right);
212 B.SetInsertPoint(Left, Left->begin());
213 Argument *PointerArg = &*F->arg_begin();
214 B.SetInsertPoint(Left);
215 B.CreateBr(Merge);
216 B.SetInsertPoint(Right);
217 B.CreateBr(Merge);
218
219 setupAnalyses();
220 MemorySSA &MSSA = *Analyses->MSSA;
221 MemorySSAUpdater Updater(&MSSA);
222 B.SetInsertPoint(Left, Left->begin());
223 // Add the store
224 StoreInst *SI = B.CreateStore(B.getInt8(16), PointerArg);
225 MemoryAccess *StoreAccess =
226 MSSA.createMemoryAccessInBB(SI, nullptr, Left, MemorySSA::Beginning);
227 Updater.insertDef(cast<MemoryDef>(StoreAccess));
228
229 // Add the load
230 B.SetInsertPoint(Merge, Merge->begin());
231 LoadInst *LoadInst = B.CreateLoad(PointerArg);
232
233 // MemoryPHI should not already exist.
234 MemoryPhi *MP = MSSA.getMemoryAccess(Merge);
235 EXPECT_EQ(MP, nullptr);
236
237 // Create the load memory acccess
238 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.createMemoryAccessInBB(
239 LoadInst, nullptr, Merge, MemorySSA::Beginning));
240 Updater.insertUse(LoadAccess);
241 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
242 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
243 MSSA.verifyMemorySSA();
244}
Daniel Berlin14300262016-06-21 18:39:20 +0000245
Daniel Berlin5130cc82016-07-31 21:08:20 +0000246TEST_F(MemorySSATest, MoveAStore) {
247 // We create a diamond where there is a in the entry, a store on one side, and
248 // a load at the end. After building MemorySSA, we test updating by moving
Daniel Berlin60ead052017-01-28 01:23:13 +0000249 // the store from the side block to the entry block. This destroys the old
250 // access.
Daniel Berlin5130cc82016-07-31 21:08:20 +0000251 F = Function::Create(
252 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
253 GlobalValue::ExternalLinkage, "F", &M);
254 BasicBlock *Entry(BasicBlock::Create(C, "", F));
255 BasicBlock *Left(BasicBlock::Create(C, "", F));
256 BasicBlock *Right(BasicBlock::Create(C, "", F));
257 BasicBlock *Merge(BasicBlock::Create(C, "", F));
258 B.SetInsertPoint(Entry);
259 Argument *PointerArg = &*F->arg_begin();
260 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
261 B.CreateCondBr(B.getTrue(), Left, Right);
262 B.SetInsertPoint(Left);
263 StoreInst *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
264 BranchInst::Create(Merge, Left);
265 BranchInst::Create(Merge, Right);
266 B.SetInsertPoint(Merge);
267 B.CreateLoad(PointerArg);
268 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000269 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlin5130cc82016-07-31 21:08:20 +0000270
271 // Move the store
272 SideStore->moveBefore(Entry->getTerminator());
273 MemoryAccess *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
274 MemoryAccess *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
George Burgess IVf7672852016-08-03 19:59:11 +0000275 MemoryAccess *NewStoreAccess = MSSA.createMemoryAccessAfter(
276 SideStore, EntryStoreAccess, EntryStoreAccess);
Daniel Berlin5130cc82016-07-31 21:08:20 +0000277 EntryStoreAccess->replaceAllUsesWith(NewStoreAccess);
278 MSSA.removeMemoryAccess(SideStoreAccess);
279 MSSA.verifyMemorySSA();
280}
281
Daniel Berlin60ead052017-01-28 01:23:13 +0000282TEST_F(MemorySSATest, MoveAStoreUpdater) {
283 // We create a diamond where there is a in the entry, a store on one side, and
284 // a load at the end. After building MemorySSA, we test updating by moving
285 // the store from the side block to the entry block. This destroys the old
286 // access.
287 F = Function::Create(
288 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
289 GlobalValue::ExternalLinkage, "F", &M);
290 BasicBlock *Entry(BasicBlock::Create(C, "", F));
291 BasicBlock *Left(BasicBlock::Create(C, "", F));
292 BasicBlock *Right(BasicBlock::Create(C, "", F));
293 BasicBlock *Merge(BasicBlock::Create(C, "", F));
294 B.SetInsertPoint(Entry);
295 Argument *PointerArg = &*F->arg_begin();
296 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
297 B.CreateCondBr(B.getTrue(), Left, Right);
298 B.SetInsertPoint(Left);
299 auto *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
300 BranchInst::Create(Merge, Left);
301 BranchInst::Create(Merge, Right);
302 B.SetInsertPoint(Merge);
303 auto *MergeLoad = B.CreateLoad(PointerArg);
304 setupAnalyses();
305 MemorySSA &MSSA = *Analyses->MSSA;
306 MemorySSAUpdater Updater(&MSSA);
307
308 // Move the store
309 SideStore->moveBefore(Entry->getTerminator());
310 auto *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
311 auto *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
312 auto *NewStoreAccess = MSSA.createMemoryAccessAfter(
313 SideStore, EntryStoreAccess, EntryStoreAccess);
314 // Before, the load will point to a phi of the EntryStore and SideStore.
315 auto *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(MergeLoad));
316 EXPECT_TRUE(isa<MemoryPhi>(LoadAccess->getDefiningAccess()));
317 MemoryPhi *MergePhi = cast<MemoryPhi>(LoadAccess->getDefiningAccess());
318 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
319 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
320 MSSA.removeMemoryAccess(SideStoreAccess);
321 Updater.insertDef(cast<MemoryDef>(NewStoreAccess));
322 // After it's a phi of the new side store access.
323 EXPECT_EQ(MergePhi->getIncomingValue(0), NewStoreAccess);
324 EXPECT_EQ(MergePhi->getIncomingValue(1), NewStoreAccess);
325 MSSA.verifyMemorySSA();
326}
327
328TEST_F(MemorySSATest, MoveAStoreUpdaterMove) {
329 // We create a diamond where there is a in the entry, a store on one side, and
330 // a load at the end. After building MemorySSA, we test updating by moving
331 // the store from the side block to the entry block. This does not destroy
332 // the old access.
333 F = Function::Create(
334 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
335 GlobalValue::ExternalLinkage, "F", &M);
336 BasicBlock *Entry(BasicBlock::Create(C, "", F));
337 BasicBlock *Left(BasicBlock::Create(C, "", F));
338 BasicBlock *Right(BasicBlock::Create(C, "", F));
339 BasicBlock *Merge(BasicBlock::Create(C, "", F));
340 B.SetInsertPoint(Entry);
341 Argument *PointerArg = &*F->arg_begin();
342 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
343 B.CreateCondBr(B.getTrue(), Left, Right);
344 B.SetInsertPoint(Left);
345 auto *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
346 BranchInst::Create(Merge, Left);
347 BranchInst::Create(Merge, Right);
348 B.SetInsertPoint(Merge);
349 auto *MergeLoad = B.CreateLoad(PointerArg);
350 setupAnalyses();
351 MemorySSA &MSSA = *Analyses->MSSA;
352 MemorySSAUpdater Updater(&MSSA);
353
354 // Move the store
355 auto *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
356 auto *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
357 // Before, the load will point to a phi of the EntryStore and SideStore.
358 auto *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(MergeLoad));
359 EXPECT_TRUE(isa<MemoryPhi>(LoadAccess->getDefiningAccess()));
360 MemoryPhi *MergePhi = cast<MemoryPhi>(LoadAccess->getDefiningAccess());
361 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
362 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
363 SideStore->moveBefore(*EntryStore->getParent(), ++EntryStore->getIterator());
364 Updater.moveAfter(SideStoreAccess, EntryStoreAccess);
365 // After, it's a phi of the side store.
366 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
367 EXPECT_EQ(MergePhi->getIncomingValue(1), SideStoreAccess);
368
369 MSSA.verifyMemorySSA();
370}
371
Daniel Berlin9d8a3352017-01-30 11:35:39 +0000372TEST_F(MemorySSATest, MoveAStoreAllAround) {
373 // We create a diamond where there is a in the entry, a store on one side, and
374 // a load at the end. After building MemorySSA, we test updating by moving
375 // the store from the side block to the entry block, then to the other side
376 // block, then to before the load. This does not destroy the old access.
377 F = Function::Create(
378 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
379 GlobalValue::ExternalLinkage, "F", &M);
380 BasicBlock *Entry(BasicBlock::Create(C, "", F));
381 BasicBlock *Left(BasicBlock::Create(C, "", F));
382 BasicBlock *Right(BasicBlock::Create(C, "", F));
383 BasicBlock *Merge(BasicBlock::Create(C, "", F));
384 B.SetInsertPoint(Entry);
385 Argument *PointerArg = &*F->arg_begin();
386 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
387 B.CreateCondBr(B.getTrue(), Left, Right);
388 B.SetInsertPoint(Left);
389 auto *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
390 BranchInst::Create(Merge, Left);
391 BranchInst::Create(Merge, Right);
392 B.SetInsertPoint(Merge);
393 auto *MergeLoad = B.CreateLoad(PointerArg);
394 setupAnalyses();
395 MemorySSA &MSSA = *Analyses->MSSA;
396 MemorySSAUpdater Updater(&MSSA);
397
398 // Move the store
399 auto *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
400 auto *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
401 // Before, the load will point to a phi of the EntryStore and SideStore.
402 auto *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(MergeLoad));
403 EXPECT_TRUE(isa<MemoryPhi>(LoadAccess->getDefiningAccess()));
404 MemoryPhi *MergePhi = cast<MemoryPhi>(LoadAccess->getDefiningAccess());
405 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
406 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
407 // Move the store before the entry store
408 SideStore->moveBefore(*EntryStore->getParent(), EntryStore->getIterator());
409 Updater.moveBefore(SideStoreAccess, EntryStoreAccess);
410 // After, it's a phi of the entry store.
411 EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
412 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
413 MSSA.verifyMemorySSA();
414 // Now move the store to the right branch
415 SideStore->moveBefore(*Right, Right->begin());
416 Updater.moveToPlace(SideStoreAccess, Right, MemorySSA::Beginning);
417 MSSA.verifyMemorySSA();
418 EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
419 EXPECT_EQ(MergePhi->getIncomingValue(1), SideStoreAccess);
420 // Now move it before the load
421 SideStore->moveBefore(MergeLoad);
422 Updater.moveBefore(SideStoreAccess, LoadAccess);
423 EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
424 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
425 MSSA.verifyMemorySSA();
426}
427
Daniel Berlin14300262016-06-21 18:39:20 +0000428TEST_F(MemorySSATest, RemoveAPhi) {
429 // We create a diamond where there is a store on one side, and then a load
430 // after the merge point. This enables us to test a bunch of different
431 // removal cases.
432 F = Function::Create(
433 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
434 GlobalValue::ExternalLinkage, "F", &M);
435 BasicBlock *Entry(BasicBlock::Create(C, "", F));
436 BasicBlock *Left(BasicBlock::Create(C, "", F));
437 BasicBlock *Right(BasicBlock::Create(C, "", F));
438 BasicBlock *Merge(BasicBlock::Create(C, "", F));
439 B.SetInsertPoint(Entry);
440 B.CreateCondBr(B.getTrue(), Left, Right);
441 B.SetInsertPoint(Left);
442 Argument *PointerArg = &*F->arg_begin();
443 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
444 BranchInst::Create(Merge, Left);
445 BranchInst::Create(Merge, Right);
446 B.SetInsertPoint(Merge);
447 LoadInst *LoadInst = B.CreateLoad(PointerArg);
448
449 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000450 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlin14300262016-06-21 18:39:20 +0000451 // Before, the load will be a use of a phi<store, liveonentry>.
452 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst));
453 MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
454 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
455 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
456 // Kill the store
457 MSSA.removeMemoryAccess(StoreAccess);
458 MemoryPhi *MP = cast<MemoryPhi>(DefiningAccess);
459 // Verify the phi ended up as liveonentry, liveonentry
460 for (auto &Op : MP->incoming_values())
461 EXPECT_TRUE(MSSA.isLiveOnEntryDef(cast<MemoryAccess>(Op.get())));
462 // Replace the phi uses with the live on entry def
463 MP->replaceAllUsesWith(MSSA.getLiveOnEntryDef());
464 // Verify the load is now defined by liveOnEntryDef
465 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
466 // Remove the PHI
467 MSSA.removeMemoryAccess(MP);
468 MSSA.verifyMemorySSA();
469}
470
George Burgess IV1b1fef32016-04-29 18:42:55 +0000471TEST_F(MemorySSATest, RemoveMemoryAccess) {
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000472 // We create a diamond where there is a store on one side, and then a load
473 // after the merge point. This enables us to test a bunch of different
474 // removal cases.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000475 F = Function::Create(
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000476 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
George Burgess IV1b1fef32016-04-29 18:42:55 +0000477 GlobalValue::ExternalLinkage, "F", &M);
Daniel Berlin64120022016-03-02 21:16:28 +0000478 BasicBlock *Entry(BasicBlock::Create(C, "", F));
479 BasicBlock *Left(BasicBlock::Create(C, "", F));
480 BasicBlock *Right(BasicBlock::Create(C, "", F));
481 BasicBlock *Merge(BasicBlock::Create(C, "", F));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000482 B.SetInsertPoint(Entry);
483 B.CreateCondBr(B.getTrue(), Left, Right);
484 B.SetInsertPoint(Left);
485 Argument *PointerArg = &*F->arg_begin();
486 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
487 BranchInst::Create(Merge, Left);
488 BranchInst::Create(Merge, Right);
489 B.SetInsertPoint(Merge);
490 LoadInst *LoadInst = B.CreateLoad(PointerArg);
491
George Burgess IV1b1fef32016-04-29 18:42:55 +0000492 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000493 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000494 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000495
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000496 // Before, the load will be a use of a phi<store, liveonentry>. It should be
497 // the same after.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000498 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst));
499 MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000500 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
501 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
502 // The load is currently clobbered by one of the phi arguments, so the walker
503 // should determine the clobbering access as the phi.
504 EXPECT_EQ(DefiningAccess, Walker->getClobberingMemoryAccess(LoadInst));
George Burgess IV1b1fef32016-04-29 18:42:55 +0000505 MSSA.removeMemoryAccess(StoreAccess);
506 MSSA.verifyMemorySSA();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000507 // After the removeaccess, let's see if we got the right accesses
508 // The load should still point to the phi ...
509 EXPECT_EQ(DefiningAccess, LoadAccess->getDefiningAccess());
510 // but we should now get live on entry for the clobbering definition of the
511 // load, since it will walk past the phi node since every argument is the
512 // same.
Daniel Berlincd2deac2016-10-20 20:13:45 +0000513 // XXX: This currently requires either removing the phi or resetting optimized
514 // on the load
515
516 EXPECT_FALSE(
517 MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst)));
518 // If we reset optimized, we get live on entry.
519 LoadAccess->resetOptimized();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000520 EXPECT_TRUE(
George Burgess IV1b1fef32016-04-29 18:42:55 +0000521 MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst)));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000522 // The phi should now be a two entry phi with two live on entry defs.
523 for (const auto &Op : DefiningAccess->operands()) {
524 MemoryAccess *Operand = cast<MemoryAccess>(&*Op);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000525 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Operand));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000526 }
527
528 // Now we try to remove the single valued phi
George Burgess IV1b1fef32016-04-29 18:42:55 +0000529 MSSA.removeMemoryAccess(DefiningAccess);
530 MSSA.verifyMemorySSA();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000531 // Now the load should be a load of live on entry.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000532 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
533}
534
535// We had a bug with caching where the walker would report MemoryDef#3's clobber
536// (below) was MemoryDef#1.
537//
538// define void @F(i8*) {
539// %A = alloca i8, i8 1
540// ; 1 = MemoryDef(liveOnEntry)
541// store i8 0, i8* %A
542// ; 2 = MemoryDef(1)
543// store i8 1, i8* %A
544// ; 3 = MemoryDef(2)
545// store i8 2, i8* %A
546// }
547TEST_F(MemorySSATest, TestTripleStore) {
Daniel Berlin14300262016-06-21 18:39:20 +0000548 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
549 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000550 B.SetInsertPoint(BasicBlock::Create(C, "", F));
551 Type *Int8 = Type::getInt8Ty(C);
552 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
553 StoreInst *S1 = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
554 StoreInst *S2 = B.CreateStore(ConstantInt::get(Int8, 1), Alloca);
555 StoreInst *S3 = B.CreateStore(ConstantInt::get(Int8, 2), Alloca);
556
557 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000558 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000559 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000560
561 unsigned I = 0;
562 for (StoreInst *V : {S1, S2, S3}) {
563 // Everything should be clobbered by its defining access
George Burgess IV66837ab2016-11-01 21:17:46 +0000564 MemoryAccess *DefiningAccess = MSSA.getMemoryAccess(V)->getDefiningAccess();
George Burgess IV1b1fef32016-04-29 18:42:55 +0000565 MemoryAccess *WalkerClobber = Walker->getClobberingMemoryAccess(V);
566 EXPECT_EQ(DefiningAccess, WalkerClobber)
567 << "Store " << I << " doesn't have the correct clobbering access";
568 // EXPECT_EQ expands such that if we increment I above, it won't get
569 // incremented except when we try to print the error message.
570 ++I;
571 }
572}
573
574// ...And fixing the above bug made it obvious that, when walking, MemorySSA's
575// walker was caching the initial node it walked. This was fine (albeit
576// mostly redundant) unless the initial node being walked is a clobber for the
577// query. In that case, we'd cache that the node clobbered itself.
578TEST_F(MemorySSATest, TestStoreAndLoad) {
Daniel Berlin14300262016-06-21 18:39:20 +0000579 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
580 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000581 B.SetInsertPoint(BasicBlock::Create(C, "", F));
582 Type *Int8 = Type::getInt8Ty(C);
583 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
584 Instruction *SI = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
585 Instruction *LI = B.CreateLoad(Alloca);
586
587 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000588 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000589 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000590
591 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LI);
592 EXPECT_EQ(LoadClobber, MSSA.getMemoryAccess(SI));
593 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(SI)));
594}
595
596// Another bug (related to the above two fixes): It was noted that, given the
597// following code:
598// ; 1 = MemoryDef(liveOnEntry)
599// store i8 0, i8* %1
600//
601// ...A query to getClobberingMemoryAccess(MemoryAccess*, MemoryLocation) would
602// hand back the store (correctly). A later call to
603// getClobberingMemoryAccess(const Instruction*) would also hand back the store
604// (incorrectly; it should return liveOnEntry).
605//
606// This test checks that repeated calls to either function returns what they're
607// meant to.
608TEST_F(MemorySSATest, TestStoreDoubleQuery) {
Daniel Berlin14300262016-06-21 18:39:20 +0000609 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
610 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000611 B.SetInsertPoint(BasicBlock::Create(C, "", F));
612 Type *Int8 = Type::getInt8Ty(C);
613 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
614 StoreInst *SI = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
615
616 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000617 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000618 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000619
620 MemoryAccess *StoreAccess = MSSA.getMemoryAccess(SI);
621 MemoryLocation StoreLoc = MemoryLocation::get(SI);
Daniel Berlin14300262016-06-21 18:39:20 +0000622 MemoryAccess *Clobber =
623 Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000624 MemoryAccess *LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
625
626 EXPECT_EQ(Clobber, StoreAccess);
627 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LiveOnEntry));
628
629 // Try again (with entries in the cache already) for good measure...
630 Clobber = Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
631 LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
632 EXPECT_EQ(Clobber, StoreAccess);
633 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LiveOnEntry));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000634}
George Burgess IV14633b52016-08-03 01:22:19 +0000635
636// Bug: During phi optimization, the walker wouldn't cache to the proper result
637// in the farthest-walked BB.
638//
639// Specifically, it would assume that whatever we walked to was a clobber.
640// "Whatever we walked to" isn't a clobber if we hit a cache entry.
641//
642// ...So, we need a test case that looks like:
643// A
644// / \
645// B |
646// \ /
647// C
648//
649// Where, when we try to optimize a thing in 'C', a blocker is found in 'B'.
650// The walk must determine that the blocker exists by using cache entries *while
651// walking* 'B'.
652TEST_F(MemorySSATest, PartialWalkerCacheWithPhis) {
653 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
654 GlobalValue::ExternalLinkage, "F", &M);
655 B.SetInsertPoint(BasicBlock::Create(C, "A", F));
656 Type *Int8 = Type::getInt8Ty(C);
657 Constant *One = ConstantInt::get(Int8, 1);
658 Constant *Zero = ConstantInt::get(Int8, 0);
659 Value *AllocA = B.CreateAlloca(Int8, One, "a");
660 Value *AllocB = B.CreateAlloca(Int8, One, "b");
661 BasicBlock *IfThen = BasicBlock::Create(C, "B", F);
662 BasicBlock *IfEnd = BasicBlock::Create(C, "C", F);
663
664 B.CreateCondBr(UndefValue::get(Type::getInt1Ty(C)), IfThen, IfEnd);
665
666 B.SetInsertPoint(IfThen);
667 Instruction *FirstStore = B.CreateStore(Zero, AllocA);
668 B.CreateStore(Zero, AllocB);
669 Instruction *ALoad0 = B.CreateLoad(AllocA, "");
670 Instruction *BStore = B.CreateStore(Zero, AllocB);
671 // Due to use optimization/etc. we make a store to A, which is removed after
672 // we build MSSA. This helps keep the test case simple-ish.
673 Instruction *KillStore = B.CreateStore(Zero, AllocA);
674 Instruction *ALoad = B.CreateLoad(AllocA, "");
675 B.CreateBr(IfEnd);
676
677 B.SetInsertPoint(IfEnd);
678 Instruction *BelowPhi = B.CreateStore(Zero, AllocA);
679
680 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000681 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IV14633b52016-08-03 01:22:19 +0000682 MemorySSAWalker *Walker = Analyses->Walker;
683
684 // Kill `KillStore`; it exists solely so that the load after it won't be
685 // optimized to FirstStore.
686 MSSA.removeMemoryAccess(MSSA.getMemoryAccess(KillStore));
687 KillStore->eraseFromParent();
688 auto *ALoadMA = cast<MemoryUse>(MSSA.getMemoryAccess(ALoad));
689 EXPECT_EQ(ALoadMA->getDefiningAccess(), MSSA.getMemoryAccess(BStore));
690
691 // Populate the cache for the store to AllocB directly after FirstStore. It
692 // should point to something in block B (so something in D can't be optimized
693 // to it).
694 MemoryAccess *Load0Clobber = Walker->getClobberingMemoryAccess(ALoad0);
695 EXPECT_EQ(MSSA.getMemoryAccess(FirstStore), Load0Clobber);
696
697 // If the bug exists, this will introduce a bad cache entry for %a on BStore.
698 // It will point to the store to %b after FirstStore. This only happens during
699 // phi optimization.
700 MemoryAccess *BottomClobber = Walker->getClobberingMemoryAccess(BelowPhi);
701 MemoryAccess *Phi = MSSA.getMemoryAccess(IfEnd);
702 EXPECT_EQ(BottomClobber, Phi);
703
704 // This query will first check the cache for {%a, BStore}. It should point to
705 // FirstStore, not to the store after FirstStore.
706 MemoryAccess *UseClobber = Walker->getClobberingMemoryAccess(ALoad);
707 EXPECT_EQ(UseClobber, MSSA.getMemoryAccess(FirstStore));
708}
George Burgess IV024f3d22016-08-03 19:57:02 +0000709
710// Test that our walker properly handles loads with the invariant group
711// attribute. It's a bit hacky, since we add the invariant attribute *after*
712// building MSSA. Otherwise, the use optimizer will optimize it for us, which
713// isn't what we want.
714// FIXME: It may be easier/cleaner to just add an 'optimize uses?' flag to MSSA.
715TEST_F(MemorySSATest, WalkerInvariantLoadOpt) {
716 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
717 GlobalValue::ExternalLinkage, "F", &M);
718 B.SetInsertPoint(BasicBlock::Create(C, "", F));
719 Type *Int8 = Type::getInt8Ty(C);
720 Constant *One = ConstantInt::get(Int8, 1);
721 Value *AllocA = B.CreateAlloca(Int8, One, "");
722
723 Instruction *Store = B.CreateStore(One, AllocA);
724 Instruction *Load = B.CreateLoad(AllocA);
725
726 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000727 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IV024f3d22016-08-03 19:57:02 +0000728 MemorySSAWalker *Walker = Analyses->Walker;
729
730 auto *LoadMA = cast<MemoryUse>(MSSA.getMemoryAccess(Load));
731 auto *StoreMA = cast<MemoryDef>(MSSA.getMemoryAccess(Store));
732 EXPECT_EQ(LoadMA->getDefiningAccess(), StoreMA);
733
734 // ...At the time of writing, no cache should exist for LoadMA. Be a bit
735 // flexible to future changes.
736 Walker->invalidateInfo(LoadMA);
737 Load->setMetadata(LLVMContext::MD_invariant_load, MDNode::get(C, {}));
738
739 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LoadMA);
740 EXPECT_EQ(LoadClobber, MSSA.getLiveOnEntryDef());
741}
George Burgess IV80ed8632016-10-03 23:12:35 +0000742
Daniel Berlincd2deac2016-10-20 20:13:45 +0000743// Test loads get reoptimized properly by the walker.
744TEST_F(MemorySSATest, WalkerReopt) {
George Burgess IV80ed8632016-10-03 23:12:35 +0000745 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
746 GlobalValue::ExternalLinkage, "F", &M);
747 B.SetInsertPoint(BasicBlock::Create(C, "", F));
George Burgess IV80ed8632016-10-03 23:12:35 +0000748 Type *Int8 = Type::getInt8Ty(C);
Daniel Berlincd2deac2016-10-20 20:13:45 +0000749 Value *AllocaA = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
750 Instruction *SIA = B.CreateStore(ConstantInt::get(Int8, 0), AllocaA);
751 Value *AllocaB = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
752 Instruction *SIB = B.CreateStore(ConstantInt::get(Int8, 0), AllocaB);
753 Instruction *LIA = B.CreateLoad(AllocaA);
George Burgess IV80ed8632016-10-03 23:12:35 +0000754
755 setupAnalyses();
756 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlincd2deac2016-10-20 20:13:45 +0000757 MemorySSAWalker *Walker = Analyses->Walker;
758
759 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LIA);
760 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LIA));
761 EXPECT_EQ(LoadClobber, MSSA.getMemoryAccess(SIA));
762 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(SIA)));
763 MSSA.removeMemoryAccess(LoadAccess);
764
765 // Create the load memory access pointing to an unoptimized place.
766 MemoryUse *NewLoadAccess = cast<MemoryUse>(MSSA.createMemoryAccessInBB(
767 LIA, MSSA.getMemoryAccess(SIB), LIA->getParent(), MemorySSA::End));
768 // This should it cause it to be optimized
769 EXPECT_EQ(Walker->getClobberingMemoryAccess(NewLoadAccess), LoadClobber);
770 EXPECT_EQ(NewLoadAccess->getDefiningAccess(), LoadClobber);
George Burgess IV80ed8632016-10-03 23:12:35 +0000771}
Bryant Wong4213d942016-12-25 23:34:07 +0000772
Daniel Berlin60ead052017-01-28 01:23:13 +0000773// Test out MemorySSAUpdater::moveBefore
774TEST_F(MemorySSATest, MoveAboveMemoryDef) {
Bryant Wong4213d942016-12-25 23:34:07 +0000775 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
776 GlobalValue::ExternalLinkage, "F", &M);
777 B.SetInsertPoint(BasicBlock::Create(C, "", F));
778
779 Type *Int8 = Type::getInt8Ty(C);
780 Value *A = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
781 Value *B_ = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
782 Value *C = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "C");
783
784 StoreInst *StoreA0 = B.CreateStore(ConstantInt::get(Int8, 0), A);
785 StoreInst *StoreB = B.CreateStore(ConstantInt::get(Int8, 0), B_);
786 LoadInst *LoadB = B.CreateLoad(B_);
787 StoreInst *StoreA1 = B.CreateStore(ConstantInt::get(Int8, 4), A);
Bryant Wong4213d942016-12-25 23:34:07 +0000788 StoreInst *StoreC = B.CreateStore(ConstantInt::get(Int8, 4), C);
789 StoreInst *StoreA2 = B.CreateStore(ConstantInt::get(Int8, 4), A);
790 LoadInst *LoadC = B.CreateLoad(C);
791
792 setupAnalyses();
793 MemorySSA &MSSA = *Analyses->MSSA;
794 MemorySSAWalker &Walker = *Analyses->Walker;
795
Daniel Berlin60ead052017-01-28 01:23:13 +0000796 MemorySSAUpdater Updater(&MSSA);
Bryant Wong4213d942016-12-25 23:34:07 +0000797 StoreC->moveBefore(StoreB);
Daniel Berlin60ead052017-01-28 01:23:13 +0000798 Updater.moveBefore(cast<MemoryDef>(MSSA.getMemoryAccess(StoreC)),
799 cast<MemoryDef>(MSSA.getMemoryAccess(StoreB)));
Bryant Wong4213d942016-12-25 23:34:07 +0000800
801 MSSA.verifyMemorySSA();
802
803 EXPECT_EQ(MSSA.getMemoryAccess(StoreB)->getDefiningAccess(),
804 MSSA.getMemoryAccess(StoreC));
805 EXPECT_EQ(MSSA.getMemoryAccess(StoreC)->getDefiningAccess(),
806 MSSA.getMemoryAccess(StoreA0));
807 EXPECT_EQ(MSSA.getMemoryAccess(StoreA2)->getDefiningAccess(),
808 MSSA.getMemoryAccess(StoreA1));
809 EXPECT_EQ(Walker.getClobberingMemoryAccess(LoadB),
810 MSSA.getMemoryAccess(StoreB));
811 EXPECT_EQ(Walker.getClobberingMemoryAccess(LoadC),
812 MSSA.getMemoryAccess(StoreC));
813
814 // exercise block numbering
815 EXPECT_TRUE(MSSA.locallyDominates(MSSA.getMemoryAccess(StoreC),
816 MSSA.getMemoryAccess(StoreB)));
817 EXPECT_TRUE(MSSA.locallyDominates(MSSA.getMemoryAccess(StoreA1),
818 MSSA.getMemoryAccess(StoreA2)));
819}
Daniel Berlin60ead052017-01-28 01:23:13 +0000820
821TEST_F(MemorySSATest, Irreducible) {
822 // Create the equivalent of
823 // x = something
824 // if (...)
825 // goto second_loop_entry
826 // while (...) {
827 // second_loop_entry:
828 // }
829 // use(x)
830
831 SmallVector<PHINode *, 8> Inserted;
832 IRBuilder<> B(C);
833 F = Function::Create(
834 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
835 GlobalValue::ExternalLinkage, "F", &M);
836
837 // Make blocks
838 BasicBlock *IfBB = BasicBlock::Create(C, "if", F);
839 BasicBlock *LoopStartBB = BasicBlock::Create(C, "loopstart", F);
840 BasicBlock *LoopMainBB = BasicBlock::Create(C, "loopmain", F);
841 BasicBlock *AfterLoopBB = BasicBlock::Create(C, "afterloop", F);
842 B.SetInsertPoint(IfBB);
843 B.CreateCondBr(B.getTrue(), LoopMainBB, LoopStartBB);
844 B.SetInsertPoint(LoopStartBB);
845 B.CreateBr(LoopMainBB);
846 B.SetInsertPoint(LoopMainBB);
847 B.CreateCondBr(B.getTrue(), LoopStartBB, AfterLoopBB);
848 B.SetInsertPoint(AfterLoopBB);
849 Argument *FirstArg = &*F->arg_begin();
850 setupAnalyses();
851 MemorySSA &MSSA = *Analyses->MSSA;
852 MemorySSAUpdater Updater(&MSSA);
853 // Create the load memory acccess
854 LoadInst *LoadInst = B.CreateLoad(FirstArg);
855 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.createMemoryAccessInBB(
856 LoadInst, nullptr, AfterLoopBB, MemorySSA::Beginning));
857 Updater.insertUse(LoadAccess);
858 MSSA.verifyMemorySSA();
859}