blob: 0df476bc28c4ce7160b6d1cdc91d30dd0452f9a8 [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);
161 Updater.insertDef(cast<MemoryDef>(LeftStoreAccess));
162 // 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);
186 Updater.insertDef(cast<MemoryDef>(SecondEntryStoreAccess));
187 // and make sure the phi below it got updated, despite being blocks away
188 MergePhi = dyn_cast<MemoryPhi>(SecondLoadAccess->getDefiningAccess());
189 EXPECT_NE(MergePhi, nullptr);
190 EXPECT_EQ(MergePhi->getIncomingValue(0), SecondEntryStoreAccess);
191 EXPECT_EQ(MergePhi->getIncomingValue(1), LeftStoreAccess);
192 MSSA.verifyMemorySSA();
193}
194
195TEST_F(MemorySSATest, CreateALoadUpdater) {
196 // We create a diamond, then build memoryssa with no memory accesses, and
197 // incrementally update it by inserting a store in one of the branches, and a
198 // load in the merge point
199 F = Function::Create(
200 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
201 GlobalValue::ExternalLinkage, "F", &M);
202 BasicBlock *Entry(BasicBlock::Create(C, "", F));
203 BasicBlock *Left(BasicBlock::Create(C, "", F));
204 BasicBlock *Right(BasicBlock::Create(C, "", F));
205 BasicBlock *Merge(BasicBlock::Create(C, "", F));
206 B.SetInsertPoint(Entry);
207 B.CreateCondBr(B.getTrue(), Left, Right);
208 B.SetInsertPoint(Left, Left->begin());
209 Argument *PointerArg = &*F->arg_begin();
210 B.SetInsertPoint(Left);
211 B.CreateBr(Merge);
212 B.SetInsertPoint(Right);
213 B.CreateBr(Merge);
214
215 setupAnalyses();
216 MemorySSA &MSSA = *Analyses->MSSA;
217 MemorySSAUpdater Updater(&MSSA);
218 B.SetInsertPoint(Left, Left->begin());
219 // Add the store
220 StoreInst *SI = B.CreateStore(B.getInt8(16), PointerArg);
221 MemoryAccess *StoreAccess =
222 MSSA.createMemoryAccessInBB(SI, nullptr, Left, MemorySSA::Beginning);
223 Updater.insertDef(cast<MemoryDef>(StoreAccess));
224
225 // Add the load
226 B.SetInsertPoint(Merge, Merge->begin());
227 LoadInst *LoadInst = B.CreateLoad(PointerArg);
228
229 // MemoryPHI should not already exist.
230 MemoryPhi *MP = MSSA.getMemoryAccess(Merge);
231 EXPECT_EQ(MP, nullptr);
232
233 // Create the load memory acccess
234 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.createMemoryAccessInBB(
235 LoadInst, nullptr, Merge, MemorySSA::Beginning));
236 Updater.insertUse(LoadAccess);
237 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
238 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
239 MSSA.verifyMemorySSA();
240}
Daniel Berlin14300262016-06-21 18:39:20 +0000241
Daniel Berlin5130cc82016-07-31 21:08:20 +0000242TEST_F(MemorySSATest, MoveAStore) {
243 // We create a diamond where there is a in the entry, a store on one side, and
244 // a load at the end. After building MemorySSA, we test updating by moving
Daniel Berlin60ead052017-01-28 01:23:13 +0000245 // the store from the side block to the entry block. This destroys the old
246 // access.
Daniel Berlin5130cc82016-07-31 21:08:20 +0000247 F = Function::Create(
248 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
249 GlobalValue::ExternalLinkage, "F", &M);
250 BasicBlock *Entry(BasicBlock::Create(C, "", F));
251 BasicBlock *Left(BasicBlock::Create(C, "", F));
252 BasicBlock *Right(BasicBlock::Create(C, "", F));
253 BasicBlock *Merge(BasicBlock::Create(C, "", F));
254 B.SetInsertPoint(Entry);
255 Argument *PointerArg = &*F->arg_begin();
256 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
257 B.CreateCondBr(B.getTrue(), Left, Right);
258 B.SetInsertPoint(Left);
259 StoreInst *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
260 BranchInst::Create(Merge, Left);
261 BranchInst::Create(Merge, Right);
262 B.SetInsertPoint(Merge);
263 B.CreateLoad(PointerArg);
264 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000265 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlin5130cc82016-07-31 21:08:20 +0000266
267 // Move the store
268 SideStore->moveBefore(Entry->getTerminator());
269 MemoryAccess *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
270 MemoryAccess *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
George Burgess IVf7672852016-08-03 19:59:11 +0000271 MemoryAccess *NewStoreAccess = MSSA.createMemoryAccessAfter(
272 SideStore, EntryStoreAccess, EntryStoreAccess);
Daniel Berlin5130cc82016-07-31 21:08:20 +0000273 EntryStoreAccess->replaceAllUsesWith(NewStoreAccess);
274 MSSA.removeMemoryAccess(SideStoreAccess);
275 MSSA.verifyMemorySSA();
276}
277
Daniel Berlin60ead052017-01-28 01:23:13 +0000278TEST_F(MemorySSATest, MoveAStoreUpdater) {
279 // We create a diamond where there is a in the entry, a store on one side, and
280 // a load at the end. After building MemorySSA, we test updating by moving
281 // the store from the side block to the entry block. This destroys the old
282 // access.
283 F = Function::Create(
284 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
285 GlobalValue::ExternalLinkage, "F", &M);
286 BasicBlock *Entry(BasicBlock::Create(C, "", F));
287 BasicBlock *Left(BasicBlock::Create(C, "", F));
288 BasicBlock *Right(BasicBlock::Create(C, "", F));
289 BasicBlock *Merge(BasicBlock::Create(C, "", F));
290 B.SetInsertPoint(Entry);
291 Argument *PointerArg = &*F->arg_begin();
292 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
293 B.CreateCondBr(B.getTrue(), Left, Right);
294 B.SetInsertPoint(Left);
295 auto *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
296 BranchInst::Create(Merge, Left);
297 BranchInst::Create(Merge, Right);
298 B.SetInsertPoint(Merge);
299 auto *MergeLoad = B.CreateLoad(PointerArg);
300 setupAnalyses();
301 MemorySSA &MSSA = *Analyses->MSSA;
302 MemorySSAUpdater Updater(&MSSA);
303
304 // Move the store
305 SideStore->moveBefore(Entry->getTerminator());
306 auto *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
307 auto *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
308 auto *NewStoreAccess = MSSA.createMemoryAccessAfter(
309 SideStore, EntryStoreAccess, EntryStoreAccess);
310 // Before, the load will point to a phi of the EntryStore and SideStore.
311 auto *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(MergeLoad));
312 EXPECT_TRUE(isa<MemoryPhi>(LoadAccess->getDefiningAccess()));
313 MemoryPhi *MergePhi = cast<MemoryPhi>(LoadAccess->getDefiningAccess());
314 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
315 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
316 MSSA.removeMemoryAccess(SideStoreAccess);
317 Updater.insertDef(cast<MemoryDef>(NewStoreAccess));
318 // After it's a phi of the new side store access.
319 EXPECT_EQ(MergePhi->getIncomingValue(0), NewStoreAccess);
320 EXPECT_EQ(MergePhi->getIncomingValue(1), NewStoreAccess);
321 MSSA.verifyMemorySSA();
322}
323
324TEST_F(MemorySSATest, MoveAStoreUpdaterMove) {
325 // We create a diamond where there is a in the entry, a store on one side, and
326 // a load at the end. After building MemorySSA, we test updating by moving
327 // the store from the side block to the entry block. This does not destroy
328 // the old access.
329 F = Function::Create(
330 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
331 GlobalValue::ExternalLinkage, "F", &M);
332 BasicBlock *Entry(BasicBlock::Create(C, "", F));
333 BasicBlock *Left(BasicBlock::Create(C, "", F));
334 BasicBlock *Right(BasicBlock::Create(C, "", F));
335 BasicBlock *Merge(BasicBlock::Create(C, "", F));
336 B.SetInsertPoint(Entry);
337 Argument *PointerArg = &*F->arg_begin();
338 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
339 B.CreateCondBr(B.getTrue(), Left, Right);
340 B.SetInsertPoint(Left);
341 auto *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
342 BranchInst::Create(Merge, Left);
343 BranchInst::Create(Merge, Right);
344 B.SetInsertPoint(Merge);
345 auto *MergeLoad = B.CreateLoad(PointerArg);
346 setupAnalyses();
347 MemorySSA &MSSA = *Analyses->MSSA;
348 MemorySSAUpdater Updater(&MSSA);
349
350 // Move the store
351 auto *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
352 auto *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
353 // Before, the load will point to a phi of the EntryStore and SideStore.
354 auto *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(MergeLoad));
355 EXPECT_TRUE(isa<MemoryPhi>(LoadAccess->getDefiningAccess()));
356 MemoryPhi *MergePhi = cast<MemoryPhi>(LoadAccess->getDefiningAccess());
357 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
358 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
359 SideStore->moveBefore(*EntryStore->getParent(), ++EntryStore->getIterator());
360 Updater.moveAfter(SideStoreAccess, EntryStoreAccess);
361 // After, it's a phi of the side store.
362 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
363 EXPECT_EQ(MergePhi->getIncomingValue(1), SideStoreAccess);
364
365 MSSA.verifyMemorySSA();
366}
367
Daniel Berlin9d8a3352017-01-30 11:35:39 +0000368TEST_F(MemorySSATest, MoveAStoreAllAround) {
369 // We create a diamond where there is a in the entry, a store on one side, and
370 // a load at the end. After building MemorySSA, we test updating by moving
371 // the store from the side block to the entry block, then to the other side
372 // block, then to before the load. This does not destroy the old access.
373 F = Function::Create(
374 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
375 GlobalValue::ExternalLinkage, "F", &M);
376 BasicBlock *Entry(BasicBlock::Create(C, "", F));
377 BasicBlock *Left(BasicBlock::Create(C, "", F));
378 BasicBlock *Right(BasicBlock::Create(C, "", F));
379 BasicBlock *Merge(BasicBlock::Create(C, "", F));
380 B.SetInsertPoint(Entry);
381 Argument *PointerArg = &*F->arg_begin();
382 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
383 B.CreateCondBr(B.getTrue(), Left, Right);
384 B.SetInsertPoint(Left);
385 auto *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
386 BranchInst::Create(Merge, Left);
387 BranchInst::Create(Merge, Right);
388 B.SetInsertPoint(Merge);
389 auto *MergeLoad = B.CreateLoad(PointerArg);
390 setupAnalyses();
391 MemorySSA &MSSA = *Analyses->MSSA;
392 MemorySSAUpdater Updater(&MSSA);
393
394 // Move the store
395 auto *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
396 auto *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
397 // Before, the load will point to a phi of the EntryStore and SideStore.
398 auto *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(MergeLoad));
399 EXPECT_TRUE(isa<MemoryPhi>(LoadAccess->getDefiningAccess()));
400 MemoryPhi *MergePhi = cast<MemoryPhi>(LoadAccess->getDefiningAccess());
401 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
402 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
403 // Move the store before the entry store
404 SideStore->moveBefore(*EntryStore->getParent(), EntryStore->getIterator());
405 Updater.moveBefore(SideStoreAccess, EntryStoreAccess);
406 // After, it's a phi of the entry store.
407 EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
408 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
409 MSSA.verifyMemorySSA();
410 // Now move the store to the right branch
411 SideStore->moveBefore(*Right, Right->begin());
412 Updater.moveToPlace(SideStoreAccess, Right, MemorySSA::Beginning);
413 MSSA.verifyMemorySSA();
414 EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
415 EXPECT_EQ(MergePhi->getIncomingValue(1), SideStoreAccess);
416 // Now move it before the load
417 SideStore->moveBefore(MergeLoad);
418 Updater.moveBefore(SideStoreAccess, LoadAccess);
419 EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
420 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
421 MSSA.verifyMemorySSA();
422}
423
Daniel Berlin14300262016-06-21 18:39:20 +0000424TEST_F(MemorySSATest, RemoveAPhi) {
425 // We create a diamond where there is a store on one side, and then a load
426 // after the merge point. This enables us to test a bunch of different
427 // removal cases.
428 F = Function::Create(
429 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
430 GlobalValue::ExternalLinkage, "F", &M);
431 BasicBlock *Entry(BasicBlock::Create(C, "", F));
432 BasicBlock *Left(BasicBlock::Create(C, "", F));
433 BasicBlock *Right(BasicBlock::Create(C, "", F));
434 BasicBlock *Merge(BasicBlock::Create(C, "", F));
435 B.SetInsertPoint(Entry);
436 B.CreateCondBr(B.getTrue(), Left, Right);
437 B.SetInsertPoint(Left);
438 Argument *PointerArg = &*F->arg_begin();
439 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
440 BranchInst::Create(Merge, Left);
441 BranchInst::Create(Merge, Right);
442 B.SetInsertPoint(Merge);
443 LoadInst *LoadInst = B.CreateLoad(PointerArg);
444
445 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000446 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlin14300262016-06-21 18:39:20 +0000447 // Before, the load will be a use of a phi<store, liveonentry>.
448 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst));
449 MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
450 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
451 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
452 // Kill the store
453 MSSA.removeMemoryAccess(StoreAccess);
454 MemoryPhi *MP = cast<MemoryPhi>(DefiningAccess);
455 // Verify the phi ended up as liveonentry, liveonentry
456 for (auto &Op : MP->incoming_values())
457 EXPECT_TRUE(MSSA.isLiveOnEntryDef(cast<MemoryAccess>(Op.get())));
458 // Replace the phi uses with the live on entry def
459 MP->replaceAllUsesWith(MSSA.getLiveOnEntryDef());
460 // Verify the load is now defined by liveOnEntryDef
461 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
462 // Remove the PHI
463 MSSA.removeMemoryAccess(MP);
464 MSSA.verifyMemorySSA();
465}
466
George Burgess IV1b1fef32016-04-29 18:42:55 +0000467TEST_F(MemorySSATest, RemoveMemoryAccess) {
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000468 // We create a diamond where there is a store on one side, and then a load
469 // after the merge point. This enables us to test a bunch of different
470 // removal cases.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000471 F = Function::Create(
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000472 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
George Burgess IV1b1fef32016-04-29 18:42:55 +0000473 GlobalValue::ExternalLinkage, "F", &M);
Daniel Berlin64120022016-03-02 21:16:28 +0000474 BasicBlock *Entry(BasicBlock::Create(C, "", F));
475 BasicBlock *Left(BasicBlock::Create(C, "", F));
476 BasicBlock *Right(BasicBlock::Create(C, "", F));
477 BasicBlock *Merge(BasicBlock::Create(C, "", F));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000478 B.SetInsertPoint(Entry);
479 B.CreateCondBr(B.getTrue(), Left, Right);
480 B.SetInsertPoint(Left);
481 Argument *PointerArg = &*F->arg_begin();
482 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
483 BranchInst::Create(Merge, Left);
484 BranchInst::Create(Merge, Right);
485 B.SetInsertPoint(Merge);
486 LoadInst *LoadInst = B.CreateLoad(PointerArg);
487
George Burgess IV1b1fef32016-04-29 18:42:55 +0000488 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000489 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000490 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000491
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000492 // Before, the load will be a use of a phi<store, liveonentry>. It should be
493 // the same after.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000494 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst));
495 MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000496 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
497 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
498 // The load is currently clobbered by one of the phi arguments, so the walker
499 // should determine the clobbering access as the phi.
500 EXPECT_EQ(DefiningAccess, Walker->getClobberingMemoryAccess(LoadInst));
George Burgess IV1b1fef32016-04-29 18:42:55 +0000501 MSSA.removeMemoryAccess(StoreAccess);
502 MSSA.verifyMemorySSA();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000503 // After the removeaccess, let's see if we got the right accesses
504 // The load should still point to the phi ...
505 EXPECT_EQ(DefiningAccess, LoadAccess->getDefiningAccess());
506 // but we should now get live on entry for the clobbering definition of the
507 // load, since it will walk past the phi node since every argument is the
508 // same.
Daniel Berlincd2deac2016-10-20 20:13:45 +0000509 // XXX: This currently requires either removing the phi or resetting optimized
510 // on the load
511
512 EXPECT_FALSE(
513 MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst)));
514 // If we reset optimized, we get live on entry.
515 LoadAccess->resetOptimized();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000516 EXPECT_TRUE(
George Burgess IV1b1fef32016-04-29 18:42:55 +0000517 MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst)));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000518 // The phi should now be a two entry phi with two live on entry defs.
519 for (const auto &Op : DefiningAccess->operands()) {
520 MemoryAccess *Operand = cast<MemoryAccess>(&*Op);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000521 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Operand));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000522 }
523
524 // Now we try to remove the single valued phi
George Burgess IV1b1fef32016-04-29 18:42:55 +0000525 MSSA.removeMemoryAccess(DefiningAccess);
526 MSSA.verifyMemorySSA();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000527 // Now the load should be a load of live on entry.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000528 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
529}
530
531// We had a bug with caching where the walker would report MemoryDef#3's clobber
532// (below) was MemoryDef#1.
533//
534// define void @F(i8*) {
535// %A = alloca i8, i8 1
536// ; 1 = MemoryDef(liveOnEntry)
537// store i8 0, i8* %A
538// ; 2 = MemoryDef(1)
539// store i8 1, i8* %A
540// ; 3 = MemoryDef(2)
541// store i8 2, i8* %A
542// }
543TEST_F(MemorySSATest, TestTripleStore) {
Daniel Berlin14300262016-06-21 18:39:20 +0000544 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
545 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000546 B.SetInsertPoint(BasicBlock::Create(C, "", F));
547 Type *Int8 = Type::getInt8Ty(C);
548 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
549 StoreInst *S1 = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
550 StoreInst *S2 = B.CreateStore(ConstantInt::get(Int8, 1), Alloca);
551 StoreInst *S3 = B.CreateStore(ConstantInt::get(Int8, 2), Alloca);
552
553 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000554 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000555 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000556
557 unsigned I = 0;
558 for (StoreInst *V : {S1, S2, S3}) {
559 // Everything should be clobbered by its defining access
George Burgess IV66837ab2016-11-01 21:17:46 +0000560 MemoryAccess *DefiningAccess = MSSA.getMemoryAccess(V)->getDefiningAccess();
George Burgess IV1b1fef32016-04-29 18:42:55 +0000561 MemoryAccess *WalkerClobber = Walker->getClobberingMemoryAccess(V);
562 EXPECT_EQ(DefiningAccess, WalkerClobber)
563 << "Store " << I << " doesn't have the correct clobbering access";
564 // EXPECT_EQ expands such that if we increment I above, it won't get
565 // incremented except when we try to print the error message.
566 ++I;
567 }
568}
569
570// ...And fixing the above bug made it obvious that, when walking, MemorySSA's
571// walker was caching the initial node it walked. This was fine (albeit
572// mostly redundant) unless the initial node being walked is a clobber for the
573// query. In that case, we'd cache that the node clobbered itself.
574TEST_F(MemorySSATest, TestStoreAndLoad) {
Daniel Berlin14300262016-06-21 18:39:20 +0000575 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
576 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000577 B.SetInsertPoint(BasicBlock::Create(C, "", F));
578 Type *Int8 = Type::getInt8Ty(C);
579 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
580 Instruction *SI = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
581 Instruction *LI = B.CreateLoad(Alloca);
582
583 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000584 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000585 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000586
587 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LI);
588 EXPECT_EQ(LoadClobber, MSSA.getMemoryAccess(SI));
589 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(SI)));
590}
591
592// Another bug (related to the above two fixes): It was noted that, given the
593// following code:
594// ; 1 = MemoryDef(liveOnEntry)
595// store i8 0, i8* %1
596//
597// ...A query to getClobberingMemoryAccess(MemoryAccess*, MemoryLocation) would
598// hand back the store (correctly). A later call to
599// getClobberingMemoryAccess(const Instruction*) would also hand back the store
600// (incorrectly; it should return liveOnEntry).
601//
602// This test checks that repeated calls to either function returns what they're
603// meant to.
604TEST_F(MemorySSATest, TestStoreDoubleQuery) {
Daniel Berlin14300262016-06-21 18:39:20 +0000605 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
606 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000607 B.SetInsertPoint(BasicBlock::Create(C, "", F));
608 Type *Int8 = Type::getInt8Ty(C);
609 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
610 StoreInst *SI = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
611
612 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000613 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000614 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000615
616 MemoryAccess *StoreAccess = MSSA.getMemoryAccess(SI);
617 MemoryLocation StoreLoc = MemoryLocation::get(SI);
Daniel Berlin14300262016-06-21 18:39:20 +0000618 MemoryAccess *Clobber =
619 Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000620 MemoryAccess *LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
621
622 EXPECT_EQ(Clobber, StoreAccess);
623 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LiveOnEntry));
624
625 // Try again (with entries in the cache already) for good measure...
626 Clobber = Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
627 LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
628 EXPECT_EQ(Clobber, StoreAccess);
629 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LiveOnEntry));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000630}
George Burgess IV14633b52016-08-03 01:22:19 +0000631
632// Bug: During phi optimization, the walker wouldn't cache to the proper result
633// in the farthest-walked BB.
634//
635// Specifically, it would assume that whatever we walked to was a clobber.
636// "Whatever we walked to" isn't a clobber if we hit a cache entry.
637//
638// ...So, we need a test case that looks like:
639// A
640// / \
641// B |
642// \ /
643// C
644//
645// Where, when we try to optimize a thing in 'C', a blocker is found in 'B'.
646// The walk must determine that the blocker exists by using cache entries *while
647// walking* 'B'.
648TEST_F(MemorySSATest, PartialWalkerCacheWithPhis) {
649 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
650 GlobalValue::ExternalLinkage, "F", &M);
651 B.SetInsertPoint(BasicBlock::Create(C, "A", F));
652 Type *Int8 = Type::getInt8Ty(C);
653 Constant *One = ConstantInt::get(Int8, 1);
654 Constant *Zero = ConstantInt::get(Int8, 0);
655 Value *AllocA = B.CreateAlloca(Int8, One, "a");
656 Value *AllocB = B.CreateAlloca(Int8, One, "b");
657 BasicBlock *IfThen = BasicBlock::Create(C, "B", F);
658 BasicBlock *IfEnd = BasicBlock::Create(C, "C", F);
659
660 B.CreateCondBr(UndefValue::get(Type::getInt1Ty(C)), IfThen, IfEnd);
661
662 B.SetInsertPoint(IfThen);
663 Instruction *FirstStore = B.CreateStore(Zero, AllocA);
664 B.CreateStore(Zero, AllocB);
665 Instruction *ALoad0 = B.CreateLoad(AllocA, "");
666 Instruction *BStore = B.CreateStore(Zero, AllocB);
667 // Due to use optimization/etc. we make a store to A, which is removed after
668 // we build MSSA. This helps keep the test case simple-ish.
669 Instruction *KillStore = B.CreateStore(Zero, AllocA);
670 Instruction *ALoad = B.CreateLoad(AllocA, "");
671 B.CreateBr(IfEnd);
672
673 B.SetInsertPoint(IfEnd);
674 Instruction *BelowPhi = B.CreateStore(Zero, AllocA);
675
676 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000677 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IV14633b52016-08-03 01:22:19 +0000678 MemorySSAWalker *Walker = Analyses->Walker;
679
680 // Kill `KillStore`; it exists solely so that the load after it won't be
681 // optimized to FirstStore.
682 MSSA.removeMemoryAccess(MSSA.getMemoryAccess(KillStore));
683 KillStore->eraseFromParent();
684 auto *ALoadMA = cast<MemoryUse>(MSSA.getMemoryAccess(ALoad));
685 EXPECT_EQ(ALoadMA->getDefiningAccess(), MSSA.getMemoryAccess(BStore));
686
687 // Populate the cache for the store to AllocB directly after FirstStore. It
688 // should point to something in block B (so something in D can't be optimized
689 // to it).
690 MemoryAccess *Load0Clobber = Walker->getClobberingMemoryAccess(ALoad0);
691 EXPECT_EQ(MSSA.getMemoryAccess(FirstStore), Load0Clobber);
692
693 // If the bug exists, this will introduce a bad cache entry for %a on BStore.
694 // It will point to the store to %b after FirstStore. This only happens during
695 // phi optimization.
696 MemoryAccess *BottomClobber = Walker->getClobberingMemoryAccess(BelowPhi);
697 MemoryAccess *Phi = MSSA.getMemoryAccess(IfEnd);
698 EXPECT_EQ(BottomClobber, Phi);
699
700 // This query will first check the cache for {%a, BStore}. It should point to
701 // FirstStore, not to the store after FirstStore.
702 MemoryAccess *UseClobber = Walker->getClobberingMemoryAccess(ALoad);
703 EXPECT_EQ(UseClobber, MSSA.getMemoryAccess(FirstStore));
704}
George Burgess IV024f3d22016-08-03 19:57:02 +0000705
706// Test that our walker properly handles loads with the invariant group
707// attribute. It's a bit hacky, since we add the invariant attribute *after*
708// building MSSA. Otherwise, the use optimizer will optimize it for us, which
709// isn't what we want.
710// FIXME: It may be easier/cleaner to just add an 'optimize uses?' flag to MSSA.
711TEST_F(MemorySSATest, WalkerInvariantLoadOpt) {
712 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
713 GlobalValue::ExternalLinkage, "F", &M);
714 B.SetInsertPoint(BasicBlock::Create(C, "", F));
715 Type *Int8 = Type::getInt8Ty(C);
716 Constant *One = ConstantInt::get(Int8, 1);
717 Value *AllocA = B.CreateAlloca(Int8, One, "");
718
719 Instruction *Store = B.CreateStore(One, AllocA);
720 Instruction *Load = B.CreateLoad(AllocA);
721
722 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000723 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IV024f3d22016-08-03 19:57:02 +0000724 MemorySSAWalker *Walker = Analyses->Walker;
725
726 auto *LoadMA = cast<MemoryUse>(MSSA.getMemoryAccess(Load));
727 auto *StoreMA = cast<MemoryDef>(MSSA.getMemoryAccess(Store));
728 EXPECT_EQ(LoadMA->getDefiningAccess(), StoreMA);
729
730 // ...At the time of writing, no cache should exist for LoadMA. Be a bit
731 // flexible to future changes.
732 Walker->invalidateInfo(LoadMA);
733 Load->setMetadata(LLVMContext::MD_invariant_load, MDNode::get(C, {}));
734
735 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LoadMA);
736 EXPECT_EQ(LoadClobber, MSSA.getLiveOnEntryDef());
737}
George Burgess IV80ed8632016-10-03 23:12:35 +0000738
Daniel Berlincd2deac2016-10-20 20:13:45 +0000739// Test loads get reoptimized properly by the walker.
740TEST_F(MemorySSATest, WalkerReopt) {
George Burgess IV80ed8632016-10-03 23:12:35 +0000741 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
742 GlobalValue::ExternalLinkage, "F", &M);
743 B.SetInsertPoint(BasicBlock::Create(C, "", F));
George Burgess IV80ed8632016-10-03 23:12:35 +0000744 Type *Int8 = Type::getInt8Ty(C);
Daniel Berlincd2deac2016-10-20 20:13:45 +0000745 Value *AllocaA = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
746 Instruction *SIA = B.CreateStore(ConstantInt::get(Int8, 0), AllocaA);
747 Value *AllocaB = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
748 Instruction *SIB = B.CreateStore(ConstantInt::get(Int8, 0), AllocaB);
749 Instruction *LIA = B.CreateLoad(AllocaA);
George Burgess IV80ed8632016-10-03 23:12:35 +0000750
751 setupAnalyses();
752 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlincd2deac2016-10-20 20:13:45 +0000753 MemorySSAWalker *Walker = Analyses->Walker;
754
755 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LIA);
756 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LIA));
757 EXPECT_EQ(LoadClobber, MSSA.getMemoryAccess(SIA));
758 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(SIA)));
759 MSSA.removeMemoryAccess(LoadAccess);
760
761 // Create the load memory access pointing to an unoptimized place.
762 MemoryUse *NewLoadAccess = cast<MemoryUse>(MSSA.createMemoryAccessInBB(
763 LIA, MSSA.getMemoryAccess(SIB), LIA->getParent(), MemorySSA::End));
764 // This should it cause it to be optimized
765 EXPECT_EQ(Walker->getClobberingMemoryAccess(NewLoadAccess), LoadClobber);
766 EXPECT_EQ(NewLoadAccess->getDefiningAccess(), LoadClobber);
George Burgess IV80ed8632016-10-03 23:12:35 +0000767}
Bryant Wong4213d942016-12-25 23:34:07 +0000768
Daniel Berlin60ead052017-01-28 01:23:13 +0000769// Test out MemorySSAUpdater::moveBefore
770TEST_F(MemorySSATest, MoveAboveMemoryDef) {
Bryant Wong4213d942016-12-25 23:34:07 +0000771 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
772 GlobalValue::ExternalLinkage, "F", &M);
773 B.SetInsertPoint(BasicBlock::Create(C, "", F));
774
775 Type *Int8 = Type::getInt8Ty(C);
776 Value *A = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
777 Value *B_ = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
778 Value *C = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "C");
779
780 StoreInst *StoreA0 = B.CreateStore(ConstantInt::get(Int8, 0), A);
781 StoreInst *StoreB = B.CreateStore(ConstantInt::get(Int8, 0), B_);
782 LoadInst *LoadB = B.CreateLoad(B_);
783 StoreInst *StoreA1 = B.CreateStore(ConstantInt::get(Int8, 4), A);
Bryant Wong4213d942016-12-25 23:34:07 +0000784 StoreInst *StoreC = B.CreateStore(ConstantInt::get(Int8, 4), C);
785 StoreInst *StoreA2 = B.CreateStore(ConstantInt::get(Int8, 4), A);
786 LoadInst *LoadC = B.CreateLoad(C);
787
788 setupAnalyses();
789 MemorySSA &MSSA = *Analyses->MSSA;
790 MemorySSAWalker &Walker = *Analyses->Walker;
791
Daniel Berlin60ead052017-01-28 01:23:13 +0000792 MemorySSAUpdater Updater(&MSSA);
Bryant Wong4213d942016-12-25 23:34:07 +0000793 StoreC->moveBefore(StoreB);
Daniel Berlin60ead052017-01-28 01:23:13 +0000794 Updater.moveBefore(cast<MemoryDef>(MSSA.getMemoryAccess(StoreC)),
795 cast<MemoryDef>(MSSA.getMemoryAccess(StoreB)));
Bryant Wong4213d942016-12-25 23:34:07 +0000796
797 MSSA.verifyMemorySSA();
798
799 EXPECT_EQ(MSSA.getMemoryAccess(StoreB)->getDefiningAccess(),
800 MSSA.getMemoryAccess(StoreC));
801 EXPECT_EQ(MSSA.getMemoryAccess(StoreC)->getDefiningAccess(),
802 MSSA.getMemoryAccess(StoreA0));
803 EXPECT_EQ(MSSA.getMemoryAccess(StoreA2)->getDefiningAccess(),
804 MSSA.getMemoryAccess(StoreA1));
805 EXPECT_EQ(Walker.getClobberingMemoryAccess(LoadB),
806 MSSA.getMemoryAccess(StoreB));
807 EXPECT_EQ(Walker.getClobberingMemoryAccess(LoadC),
808 MSSA.getMemoryAccess(StoreC));
809
810 // exercise block numbering
811 EXPECT_TRUE(MSSA.locallyDominates(MSSA.getMemoryAccess(StoreC),
812 MSSA.getMemoryAccess(StoreB)));
813 EXPECT_TRUE(MSSA.locallyDominates(MSSA.getMemoryAccess(StoreA1),
814 MSSA.getMemoryAccess(StoreA2)));
815}
Daniel Berlin60ead052017-01-28 01:23:13 +0000816
817TEST_F(MemorySSATest, Irreducible) {
818 // Create the equivalent of
819 // x = something
820 // if (...)
821 // goto second_loop_entry
822 // while (...) {
823 // second_loop_entry:
824 // }
825 // use(x)
826
827 SmallVector<PHINode *, 8> Inserted;
828 IRBuilder<> B(C);
829 F = Function::Create(
830 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
831 GlobalValue::ExternalLinkage, "F", &M);
832
833 // Make blocks
834 BasicBlock *IfBB = BasicBlock::Create(C, "if", F);
835 BasicBlock *LoopStartBB = BasicBlock::Create(C, "loopstart", F);
836 BasicBlock *LoopMainBB = BasicBlock::Create(C, "loopmain", F);
837 BasicBlock *AfterLoopBB = BasicBlock::Create(C, "afterloop", F);
838 B.SetInsertPoint(IfBB);
839 B.CreateCondBr(B.getTrue(), LoopMainBB, LoopStartBB);
840 B.SetInsertPoint(LoopStartBB);
841 B.CreateBr(LoopMainBB);
842 B.SetInsertPoint(LoopMainBB);
843 B.CreateCondBr(B.getTrue(), LoopStartBB, AfterLoopBB);
844 B.SetInsertPoint(AfterLoopBB);
845 Argument *FirstArg = &*F->arg_begin();
846 setupAnalyses();
847 MemorySSA &MSSA = *Analyses->MSSA;
848 MemorySSAUpdater Updater(&MSSA);
849 // Create the load memory acccess
850 LoadInst *LoadInst = B.CreateLoad(FirstArg);
851 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.createMemoryAccessInBB(
852 LoadInst, nullptr, AfterLoopBB, MemorySSA::Beginning));
853 Updater.insertUse(LoadAccess);
854 MSSA.verifyMemorySSA();
855}