blob: 74f8d247f83a7e675643621a730a7fbbab6c680f [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;
Daniel Jasperaec2fa32016-12-19 08:22:17 +000042 AssumptionCache AC;
George Burgess IV1b1fef32016-04-29 18:42:55 +000043 AAResults AA;
44 BasicAAResult BAA;
George Burgess IV80ed8632016-10-03 23:12:35 +000045 // We need to defer MSSA construction until AA is *entirely* set up, which
46 // requires calling addAAResult. Hence, we just use a pointer here.
47 std::unique_ptr<MemorySSA> MSSA;
Geoff Berryb96d3b22016-06-01 21:30:40 +000048 MemorySSAWalker *Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +000049
50 TestAnalyses(MemorySSATest &Test)
Daniel Jasperaec2fa32016-12-19 08:22:17 +000051 : DT(*Test.F), AC(*Test.F), AA(Test.TLI),
52 BAA(Test.DL, Test.TLI, AC, &DT) {
George Burgess IV1b1fef32016-04-29 18:42:55 +000053 AA.addAAResult(BAA);
George Burgess IV80ed8632016-10-03 23:12:35 +000054 MSSA = make_unique<MemorySSA>(*Test.F, &AA, &DT);
55 Walker = MSSA->getWalker();
George Burgess IV1b1fef32016-04-29 18:42:55 +000056 }
57 };
58
59 std::unique_ptr<TestAnalyses> Analyses;
60
61 void setupAnalyses() {
62 assert(F);
63 Analyses.reset(new TestAnalyses(*this));
64 }
65
66public:
67 MemorySSATest()
68 : M("MemorySSATest", C), B(C), DL(DLString), TLI(TLII), F(nullptr) {}
69};
70
Daniel Berlinf72ac492016-09-26 17:44:31 +000071TEST_F(MemorySSATest, CreateALoad) {
Daniel Berlin14300262016-06-21 18:39:20 +000072 // We create a diamond where there is a store on one side, and then after
Daniel Berlincdda3ce2016-07-31 21:08:10 +000073 // building MemorySSA, create a load after the merge point, and use it to test
Daniel Berlinf72ac492016-09-26 17:44:31 +000074 // updating by creating an access for the load.
Daniel Berlin14300262016-06-21 18:39:20 +000075 F = Function::Create(
76 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
77 GlobalValue::ExternalLinkage, "F", &M);
78 BasicBlock *Entry(BasicBlock::Create(C, "", F));
79 BasicBlock *Left(BasicBlock::Create(C, "", F));
80 BasicBlock *Right(BasicBlock::Create(C, "", F));
81 BasicBlock *Merge(BasicBlock::Create(C, "", F));
82 B.SetInsertPoint(Entry);
83 B.CreateCondBr(B.getTrue(), Left, Right);
84 B.SetInsertPoint(Left);
85 Argument *PointerArg = &*F->arg_begin();
Daniel Berlinf72ac492016-09-26 17:44:31 +000086 B.CreateStore(B.getInt8(16), PointerArg);
Daniel Berlin14300262016-06-21 18:39:20 +000087 BranchInst::Create(Merge, Left);
88 BranchInst::Create(Merge, Right);
89
90 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +000091 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlin14300262016-06-21 18:39:20 +000092 // Add the load
93 B.SetInsertPoint(Merge);
94 LoadInst *LoadInst = B.CreateLoad(PointerArg);
Daniel Berlin14300262016-06-21 18:39:20 +000095
Daniel Berlinf72ac492016-09-26 17:44:31 +000096 // MemoryPHI should already exist.
97 MemoryPhi *MP = MSSA.getMemoryAccess(Merge);
98 EXPECT_NE(MP, nullptr);
Daniel Berlin14300262016-06-21 18:39:20 +000099
100 // Create the load memory acccess
101 MemoryUse *LoadAccess = cast<MemoryUse>(
102 MSSA.createMemoryAccessInBB(LoadInst, MP, Merge, MemorySSA::Beginning));
103 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
104 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
105 MSSA.verifyMemorySSA();
106}
Daniel Berlin60ead052017-01-28 01:23:13 +0000107TEST_F(MemorySSATest, CreateLoadsAndStoreUpdater) {
108 // We create a diamond, then build memoryssa with no memory accesses, and
109 // incrementally update it by inserting a store in the, entry, a load in the
110 // merge point, then a store in the branch, another load in the merge point,
111 // and then a store in the entry.
112 F = Function::Create(
113 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
114 GlobalValue::ExternalLinkage, "F", &M);
115 BasicBlock *Entry(BasicBlock::Create(C, "", F));
116 BasicBlock *Left(BasicBlock::Create(C, "", F));
117 BasicBlock *Right(BasicBlock::Create(C, "", F));
118 BasicBlock *Merge(BasicBlock::Create(C, "", F));
119 B.SetInsertPoint(Entry);
120 B.CreateCondBr(B.getTrue(), Left, Right);
121 B.SetInsertPoint(Left, Left->begin());
122 Argument *PointerArg = &*F->arg_begin();
123 B.SetInsertPoint(Left);
124 B.CreateBr(Merge);
125 B.SetInsertPoint(Right);
126 B.CreateBr(Merge);
127
128 setupAnalyses();
129 MemorySSA &MSSA = *Analyses->MSSA;
130 MemorySSAUpdater Updater(&MSSA);
131 // Add the store
132 B.SetInsertPoint(Entry, Entry->begin());
133 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
134 MemoryAccess *EntryStoreAccess = MSSA.createMemoryAccessInBB(
135 EntryStore, nullptr, Entry, MemorySSA::Beginning);
136 Updater.insertDef(cast<MemoryDef>(EntryStoreAccess));
137
138 // Add the load
139 B.SetInsertPoint(Merge, Merge->begin());
140 LoadInst *FirstLoad = B.CreateLoad(PointerArg);
141
142 // MemoryPHI should not already exist.
143 MemoryPhi *MP = MSSA.getMemoryAccess(Merge);
144 EXPECT_EQ(MP, nullptr);
145
146 // Create the load memory access
147 MemoryUse *FirstLoadAccess = cast<MemoryUse>(MSSA.createMemoryAccessInBB(
148 FirstLoad, nullptr, Merge, MemorySSA::Beginning));
149 Updater.insertUse(FirstLoadAccess);
150 // Should just have a load using the entry access, because it should discover
151 // the phi is trivial
152 EXPECT_EQ(FirstLoadAccess->getDefiningAccess(), EntryStoreAccess);
153
154 // Create a store on the left
155 // Add the store
156 B.SetInsertPoint(Left, Left->begin());
157 StoreInst *LeftStore = B.CreateStore(B.getInt8(16), PointerArg);
158 MemoryAccess *LeftStoreAccess = MSSA.createMemoryAccessInBB(
159 LeftStore, nullptr, Left, MemorySSA::Beginning);
160 Updater.insertDef(cast<MemoryDef>(LeftStoreAccess));
161 // We don't touch existing loads, so we need to create a new one to get a phi
162 // Add the second load
163 B.SetInsertPoint(Merge, Merge->begin());
164 LoadInst *SecondLoad = B.CreateLoad(PointerArg);
165
166 // MemoryPHI should not already exist.
167 MP = MSSA.getMemoryAccess(Merge);
168 EXPECT_EQ(MP, nullptr);
169
170 // Create the load memory access
171 MemoryUse *SecondLoadAccess = cast<MemoryUse>(MSSA.createMemoryAccessInBB(
172 SecondLoad, nullptr, Merge, MemorySSA::Beginning));
173 Updater.insertUse(SecondLoadAccess);
174 // Now the load should be a phi of the entry store and the left store
175 MemoryPhi *MergePhi =
176 dyn_cast<MemoryPhi>(SecondLoadAccess->getDefiningAccess());
177 EXPECT_NE(MergePhi, nullptr);
178 EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
179 EXPECT_EQ(MergePhi->getIncomingValue(1), LeftStoreAccess);
180 // Now create a store below the existing one in the entry
181 B.SetInsertPoint(Entry, --Entry->end());
182 StoreInst *SecondEntryStore = B.CreateStore(B.getInt8(16), PointerArg);
183 MemoryAccess *SecondEntryStoreAccess = MSSA.createMemoryAccessInBB(
184 SecondEntryStore, nullptr, Entry, MemorySSA::End);
185 Updater.insertDef(cast<MemoryDef>(SecondEntryStoreAccess));
186 // and make sure the phi below it got updated, despite being blocks away
187 MergePhi = dyn_cast<MemoryPhi>(SecondLoadAccess->getDefiningAccess());
188 EXPECT_NE(MergePhi, nullptr);
189 EXPECT_EQ(MergePhi->getIncomingValue(0), SecondEntryStoreAccess);
190 EXPECT_EQ(MergePhi->getIncomingValue(1), LeftStoreAccess);
191 MSSA.verifyMemorySSA();
192}
193
194TEST_F(MemorySSATest, CreateALoadUpdater) {
195 // We create a diamond, then build memoryssa with no memory accesses, and
196 // incrementally update it by inserting a store in one of the branches, and a
197 // load in the merge point
198 F = Function::Create(
199 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
200 GlobalValue::ExternalLinkage, "F", &M);
201 BasicBlock *Entry(BasicBlock::Create(C, "", F));
202 BasicBlock *Left(BasicBlock::Create(C, "", F));
203 BasicBlock *Right(BasicBlock::Create(C, "", F));
204 BasicBlock *Merge(BasicBlock::Create(C, "", F));
205 B.SetInsertPoint(Entry);
206 B.CreateCondBr(B.getTrue(), Left, Right);
207 B.SetInsertPoint(Left, Left->begin());
208 Argument *PointerArg = &*F->arg_begin();
209 B.SetInsertPoint(Left);
210 B.CreateBr(Merge);
211 B.SetInsertPoint(Right);
212 B.CreateBr(Merge);
213
214 setupAnalyses();
215 MemorySSA &MSSA = *Analyses->MSSA;
216 MemorySSAUpdater Updater(&MSSA);
217 B.SetInsertPoint(Left, Left->begin());
218 // Add the store
219 StoreInst *SI = B.CreateStore(B.getInt8(16), PointerArg);
220 MemoryAccess *StoreAccess =
221 MSSA.createMemoryAccessInBB(SI, nullptr, Left, MemorySSA::Beginning);
222 Updater.insertDef(cast<MemoryDef>(StoreAccess));
223
224 // Add the load
225 B.SetInsertPoint(Merge, Merge->begin());
226 LoadInst *LoadInst = B.CreateLoad(PointerArg);
227
228 // MemoryPHI should not already exist.
229 MemoryPhi *MP = MSSA.getMemoryAccess(Merge);
230 EXPECT_EQ(MP, nullptr);
231
232 // Create the load memory acccess
233 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.createMemoryAccessInBB(
234 LoadInst, nullptr, Merge, MemorySSA::Beginning));
235 Updater.insertUse(LoadAccess);
236 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
237 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
238 MSSA.verifyMemorySSA();
239}
Daniel Berlin14300262016-06-21 18:39:20 +0000240
Daniel Berlin5130cc82016-07-31 21:08:20 +0000241TEST_F(MemorySSATest, MoveAStore) {
242 // We create a diamond where there is a in the entry, a store on one side, and
243 // a load at the end. After building MemorySSA, we test updating by moving
Daniel Berlin60ead052017-01-28 01:23:13 +0000244 // the store from the side block to the entry block. This destroys the old
245 // access.
Daniel Berlin5130cc82016-07-31 21:08:20 +0000246 F = Function::Create(
247 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
248 GlobalValue::ExternalLinkage, "F", &M);
249 BasicBlock *Entry(BasicBlock::Create(C, "", F));
250 BasicBlock *Left(BasicBlock::Create(C, "", F));
251 BasicBlock *Right(BasicBlock::Create(C, "", F));
252 BasicBlock *Merge(BasicBlock::Create(C, "", F));
253 B.SetInsertPoint(Entry);
254 Argument *PointerArg = &*F->arg_begin();
255 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
256 B.CreateCondBr(B.getTrue(), Left, Right);
257 B.SetInsertPoint(Left);
258 StoreInst *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
259 BranchInst::Create(Merge, Left);
260 BranchInst::Create(Merge, Right);
261 B.SetInsertPoint(Merge);
262 B.CreateLoad(PointerArg);
263 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000264 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlin5130cc82016-07-31 21:08:20 +0000265
266 // Move the store
267 SideStore->moveBefore(Entry->getTerminator());
268 MemoryAccess *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
269 MemoryAccess *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
George Burgess IVf7672852016-08-03 19:59:11 +0000270 MemoryAccess *NewStoreAccess = MSSA.createMemoryAccessAfter(
271 SideStore, EntryStoreAccess, EntryStoreAccess);
Daniel Berlin5130cc82016-07-31 21:08:20 +0000272 EntryStoreAccess->replaceAllUsesWith(NewStoreAccess);
273 MSSA.removeMemoryAccess(SideStoreAccess);
274 MSSA.verifyMemorySSA();
275}
276
Daniel Berlin60ead052017-01-28 01:23:13 +0000277TEST_F(MemorySSATest, MoveAStoreUpdater) {
278 // We create a diamond where there is a in the entry, a store on one side, and
279 // a load at the end. After building MemorySSA, we test updating by moving
280 // the store from the side block to the entry block. This destroys the old
281 // access.
282 F = Function::Create(
283 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
284 GlobalValue::ExternalLinkage, "F", &M);
285 BasicBlock *Entry(BasicBlock::Create(C, "", F));
286 BasicBlock *Left(BasicBlock::Create(C, "", F));
287 BasicBlock *Right(BasicBlock::Create(C, "", F));
288 BasicBlock *Merge(BasicBlock::Create(C, "", F));
289 B.SetInsertPoint(Entry);
290 Argument *PointerArg = &*F->arg_begin();
291 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
292 B.CreateCondBr(B.getTrue(), Left, Right);
293 B.SetInsertPoint(Left);
294 auto *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
295 BranchInst::Create(Merge, Left);
296 BranchInst::Create(Merge, Right);
297 B.SetInsertPoint(Merge);
298 auto *MergeLoad = B.CreateLoad(PointerArg);
299 setupAnalyses();
300 MemorySSA &MSSA = *Analyses->MSSA;
301 MemorySSAUpdater Updater(&MSSA);
302
303 // Move the store
304 SideStore->moveBefore(Entry->getTerminator());
305 auto *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
306 auto *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
307 auto *NewStoreAccess = MSSA.createMemoryAccessAfter(
308 SideStore, EntryStoreAccess, EntryStoreAccess);
309 // Before, the load will point to a phi of the EntryStore and SideStore.
310 auto *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(MergeLoad));
311 EXPECT_TRUE(isa<MemoryPhi>(LoadAccess->getDefiningAccess()));
312 MemoryPhi *MergePhi = cast<MemoryPhi>(LoadAccess->getDefiningAccess());
313 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
314 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
315 MSSA.removeMemoryAccess(SideStoreAccess);
316 Updater.insertDef(cast<MemoryDef>(NewStoreAccess));
317 // After it's a phi of the new side store access.
318 EXPECT_EQ(MergePhi->getIncomingValue(0), NewStoreAccess);
319 EXPECT_EQ(MergePhi->getIncomingValue(1), NewStoreAccess);
320 MSSA.verifyMemorySSA();
321}
322
323TEST_F(MemorySSATest, MoveAStoreUpdaterMove) {
324 // We create a diamond where there is a in the entry, a store on one side, and
325 // a load at the end. After building MemorySSA, we test updating by moving
326 // the store from the side block to the entry block. This does not destroy
327 // the old access.
328 F = Function::Create(
329 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
330 GlobalValue::ExternalLinkage, "F", &M);
331 BasicBlock *Entry(BasicBlock::Create(C, "", F));
332 BasicBlock *Left(BasicBlock::Create(C, "", F));
333 BasicBlock *Right(BasicBlock::Create(C, "", F));
334 BasicBlock *Merge(BasicBlock::Create(C, "", F));
335 B.SetInsertPoint(Entry);
336 Argument *PointerArg = &*F->arg_begin();
337 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
338 B.CreateCondBr(B.getTrue(), Left, Right);
339 B.SetInsertPoint(Left);
340 auto *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
341 BranchInst::Create(Merge, Left);
342 BranchInst::Create(Merge, Right);
343 B.SetInsertPoint(Merge);
344 auto *MergeLoad = B.CreateLoad(PointerArg);
345 setupAnalyses();
346 MemorySSA &MSSA = *Analyses->MSSA;
347 MemorySSAUpdater Updater(&MSSA);
348
349 // Move the store
350 auto *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
351 auto *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
352 // Before, the load will point to a phi of the EntryStore and SideStore.
353 auto *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(MergeLoad));
354 EXPECT_TRUE(isa<MemoryPhi>(LoadAccess->getDefiningAccess()));
355 MemoryPhi *MergePhi = cast<MemoryPhi>(LoadAccess->getDefiningAccess());
356 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
357 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
358 SideStore->moveBefore(*EntryStore->getParent(), ++EntryStore->getIterator());
359 Updater.moveAfter(SideStoreAccess, EntryStoreAccess);
360 // After, it's a phi of the side store.
361 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
362 EXPECT_EQ(MergePhi->getIncomingValue(1), SideStoreAccess);
363
364 MSSA.verifyMemorySSA();
365}
366
Daniel Berlin14300262016-06-21 18:39:20 +0000367TEST_F(MemorySSATest, RemoveAPhi) {
368 // We create a diamond where there is a store on one side, and then a load
369 // after the merge point. This enables us to test a bunch of different
370 // removal cases.
371 F = Function::Create(
372 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
373 GlobalValue::ExternalLinkage, "F", &M);
374 BasicBlock *Entry(BasicBlock::Create(C, "", F));
375 BasicBlock *Left(BasicBlock::Create(C, "", F));
376 BasicBlock *Right(BasicBlock::Create(C, "", F));
377 BasicBlock *Merge(BasicBlock::Create(C, "", F));
378 B.SetInsertPoint(Entry);
379 B.CreateCondBr(B.getTrue(), Left, Right);
380 B.SetInsertPoint(Left);
381 Argument *PointerArg = &*F->arg_begin();
382 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
383 BranchInst::Create(Merge, Left);
384 BranchInst::Create(Merge, Right);
385 B.SetInsertPoint(Merge);
386 LoadInst *LoadInst = B.CreateLoad(PointerArg);
387
388 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000389 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlin14300262016-06-21 18:39:20 +0000390 // Before, the load will be a use of a phi<store, liveonentry>.
391 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst));
392 MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
393 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
394 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
395 // Kill the store
396 MSSA.removeMemoryAccess(StoreAccess);
397 MemoryPhi *MP = cast<MemoryPhi>(DefiningAccess);
398 // Verify the phi ended up as liveonentry, liveonentry
399 for (auto &Op : MP->incoming_values())
400 EXPECT_TRUE(MSSA.isLiveOnEntryDef(cast<MemoryAccess>(Op.get())));
401 // Replace the phi uses with the live on entry def
402 MP->replaceAllUsesWith(MSSA.getLiveOnEntryDef());
403 // Verify the load is now defined by liveOnEntryDef
404 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
405 // Remove the PHI
406 MSSA.removeMemoryAccess(MP);
407 MSSA.verifyMemorySSA();
408}
409
George Burgess IV1b1fef32016-04-29 18:42:55 +0000410TEST_F(MemorySSATest, RemoveMemoryAccess) {
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000411 // We create a diamond where there is a store on one side, and then a load
412 // after the merge point. This enables us to test a bunch of different
413 // removal cases.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000414 F = Function::Create(
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000415 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
George Burgess IV1b1fef32016-04-29 18:42:55 +0000416 GlobalValue::ExternalLinkage, "F", &M);
Daniel Berlin64120022016-03-02 21:16:28 +0000417 BasicBlock *Entry(BasicBlock::Create(C, "", F));
418 BasicBlock *Left(BasicBlock::Create(C, "", F));
419 BasicBlock *Right(BasicBlock::Create(C, "", F));
420 BasicBlock *Merge(BasicBlock::Create(C, "", F));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000421 B.SetInsertPoint(Entry);
422 B.CreateCondBr(B.getTrue(), Left, Right);
423 B.SetInsertPoint(Left);
424 Argument *PointerArg = &*F->arg_begin();
425 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
426 BranchInst::Create(Merge, Left);
427 BranchInst::Create(Merge, Right);
428 B.SetInsertPoint(Merge);
429 LoadInst *LoadInst = B.CreateLoad(PointerArg);
430
George Burgess IV1b1fef32016-04-29 18:42:55 +0000431 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000432 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000433 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000434
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000435 // Before, the load will be a use of a phi<store, liveonentry>. It should be
436 // the same after.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000437 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst));
438 MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000439 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
440 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
441 // The load is currently clobbered by one of the phi arguments, so the walker
442 // should determine the clobbering access as the phi.
443 EXPECT_EQ(DefiningAccess, Walker->getClobberingMemoryAccess(LoadInst));
George Burgess IV1b1fef32016-04-29 18:42:55 +0000444 MSSA.removeMemoryAccess(StoreAccess);
445 MSSA.verifyMemorySSA();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000446 // After the removeaccess, let's see if we got the right accesses
447 // The load should still point to the phi ...
448 EXPECT_EQ(DefiningAccess, LoadAccess->getDefiningAccess());
449 // but we should now get live on entry for the clobbering definition of the
450 // load, since it will walk past the phi node since every argument is the
451 // same.
Daniel Berlincd2deac2016-10-20 20:13:45 +0000452 // XXX: This currently requires either removing the phi or resetting optimized
453 // on the load
454
455 EXPECT_FALSE(
456 MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst)));
457 // If we reset optimized, we get live on entry.
458 LoadAccess->resetOptimized();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000459 EXPECT_TRUE(
George Burgess IV1b1fef32016-04-29 18:42:55 +0000460 MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst)));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000461 // The phi should now be a two entry phi with two live on entry defs.
462 for (const auto &Op : DefiningAccess->operands()) {
463 MemoryAccess *Operand = cast<MemoryAccess>(&*Op);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000464 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Operand));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000465 }
466
467 // Now we try to remove the single valued phi
George Burgess IV1b1fef32016-04-29 18:42:55 +0000468 MSSA.removeMemoryAccess(DefiningAccess);
469 MSSA.verifyMemorySSA();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000470 // Now the load should be a load of live on entry.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000471 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
472}
473
474// We had a bug with caching where the walker would report MemoryDef#3's clobber
475// (below) was MemoryDef#1.
476//
477// define void @F(i8*) {
478// %A = alloca i8, i8 1
479// ; 1 = MemoryDef(liveOnEntry)
480// store i8 0, i8* %A
481// ; 2 = MemoryDef(1)
482// store i8 1, i8* %A
483// ; 3 = MemoryDef(2)
484// store i8 2, i8* %A
485// }
486TEST_F(MemorySSATest, TestTripleStore) {
Daniel Berlin14300262016-06-21 18:39:20 +0000487 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
488 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000489 B.SetInsertPoint(BasicBlock::Create(C, "", F));
490 Type *Int8 = Type::getInt8Ty(C);
491 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
492 StoreInst *S1 = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
493 StoreInst *S2 = B.CreateStore(ConstantInt::get(Int8, 1), Alloca);
494 StoreInst *S3 = B.CreateStore(ConstantInt::get(Int8, 2), Alloca);
495
496 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000497 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000498 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000499
500 unsigned I = 0;
501 for (StoreInst *V : {S1, S2, S3}) {
502 // Everything should be clobbered by its defining access
George Burgess IV66837ab2016-11-01 21:17:46 +0000503 MemoryAccess *DefiningAccess = MSSA.getMemoryAccess(V)->getDefiningAccess();
George Burgess IV1b1fef32016-04-29 18:42:55 +0000504 MemoryAccess *WalkerClobber = Walker->getClobberingMemoryAccess(V);
505 EXPECT_EQ(DefiningAccess, WalkerClobber)
506 << "Store " << I << " doesn't have the correct clobbering access";
507 // EXPECT_EQ expands such that if we increment I above, it won't get
508 // incremented except when we try to print the error message.
509 ++I;
510 }
511}
512
513// ...And fixing the above bug made it obvious that, when walking, MemorySSA's
514// walker was caching the initial node it walked. This was fine (albeit
515// mostly redundant) unless the initial node being walked is a clobber for the
516// query. In that case, we'd cache that the node clobbered itself.
517TEST_F(MemorySSATest, TestStoreAndLoad) {
Daniel Berlin14300262016-06-21 18:39:20 +0000518 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
519 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000520 B.SetInsertPoint(BasicBlock::Create(C, "", F));
521 Type *Int8 = Type::getInt8Ty(C);
522 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
523 Instruction *SI = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
524 Instruction *LI = B.CreateLoad(Alloca);
525
526 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000527 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000528 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000529
530 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LI);
531 EXPECT_EQ(LoadClobber, MSSA.getMemoryAccess(SI));
532 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(SI)));
533}
534
535// Another bug (related to the above two fixes): It was noted that, given the
536// following code:
537// ; 1 = MemoryDef(liveOnEntry)
538// store i8 0, i8* %1
539//
540// ...A query to getClobberingMemoryAccess(MemoryAccess*, MemoryLocation) would
541// hand back the store (correctly). A later call to
542// getClobberingMemoryAccess(const Instruction*) would also hand back the store
543// (incorrectly; it should return liveOnEntry).
544//
545// This test checks that repeated calls to either function returns what they're
546// meant to.
547TEST_F(MemorySSATest, TestStoreDoubleQuery) {
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 *SI = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
554
555 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000556 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000557 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000558
559 MemoryAccess *StoreAccess = MSSA.getMemoryAccess(SI);
560 MemoryLocation StoreLoc = MemoryLocation::get(SI);
Daniel Berlin14300262016-06-21 18:39:20 +0000561 MemoryAccess *Clobber =
562 Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000563 MemoryAccess *LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
564
565 EXPECT_EQ(Clobber, StoreAccess);
566 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LiveOnEntry));
567
568 // Try again (with entries in the cache already) for good measure...
569 Clobber = Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
570 LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
571 EXPECT_EQ(Clobber, StoreAccess);
572 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LiveOnEntry));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000573}
George Burgess IV14633b52016-08-03 01:22:19 +0000574
575// Bug: During phi optimization, the walker wouldn't cache to the proper result
576// in the farthest-walked BB.
577//
578// Specifically, it would assume that whatever we walked to was a clobber.
579// "Whatever we walked to" isn't a clobber if we hit a cache entry.
580//
581// ...So, we need a test case that looks like:
582// A
583// / \
584// B |
585// \ /
586// C
587//
588// Where, when we try to optimize a thing in 'C', a blocker is found in 'B'.
589// The walk must determine that the blocker exists by using cache entries *while
590// walking* 'B'.
591TEST_F(MemorySSATest, PartialWalkerCacheWithPhis) {
592 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
593 GlobalValue::ExternalLinkage, "F", &M);
594 B.SetInsertPoint(BasicBlock::Create(C, "A", F));
595 Type *Int8 = Type::getInt8Ty(C);
596 Constant *One = ConstantInt::get(Int8, 1);
597 Constant *Zero = ConstantInt::get(Int8, 0);
598 Value *AllocA = B.CreateAlloca(Int8, One, "a");
599 Value *AllocB = B.CreateAlloca(Int8, One, "b");
600 BasicBlock *IfThen = BasicBlock::Create(C, "B", F);
601 BasicBlock *IfEnd = BasicBlock::Create(C, "C", F);
602
603 B.CreateCondBr(UndefValue::get(Type::getInt1Ty(C)), IfThen, IfEnd);
604
605 B.SetInsertPoint(IfThen);
606 Instruction *FirstStore = B.CreateStore(Zero, AllocA);
607 B.CreateStore(Zero, AllocB);
608 Instruction *ALoad0 = B.CreateLoad(AllocA, "");
609 Instruction *BStore = B.CreateStore(Zero, AllocB);
610 // Due to use optimization/etc. we make a store to A, which is removed after
611 // we build MSSA. This helps keep the test case simple-ish.
612 Instruction *KillStore = B.CreateStore(Zero, AllocA);
613 Instruction *ALoad = B.CreateLoad(AllocA, "");
614 B.CreateBr(IfEnd);
615
616 B.SetInsertPoint(IfEnd);
617 Instruction *BelowPhi = B.CreateStore(Zero, AllocA);
618
619 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000620 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IV14633b52016-08-03 01:22:19 +0000621 MemorySSAWalker *Walker = Analyses->Walker;
622
623 // Kill `KillStore`; it exists solely so that the load after it won't be
624 // optimized to FirstStore.
625 MSSA.removeMemoryAccess(MSSA.getMemoryAccess(KillStore));
626 KillStore->eraseFromParent();
627 auto *ALoadMA = cast<MemoryUse>(MSSA.getMemoryAccess(ALoad));
628 EXPECT_EQ(ALoadMA->getDefiningAccess(), MSSA.getMemoryAccess(BStore));
629
630 // Populate the cache for the store to AllocB directly after FirstStore. It
631 // should point to something in block B (so something in D can't be optimized
632 // to it).
633 MemoryAccess *Load0Clobber = Walker->getClobberingMemoryAccess(ALoad0);
634 EXPECT_EQ(MSSA.getMemoryAccess(FirstStore), Load0Clobber);
635
636 // If the bug exists, this will introduce a bad cache entry for %a on BStore.
637 // It will point to the store to %b after FirstStore. This only happens during
638 // phi optimization.
639 MemoryAccess *BottomClobber = Walker->getClobberingMemoryAccess(BelowPhi);
640 MemoryAccess *Phi = MSSA.getMemoryAccess(IfEnd);
641 EXPECT_EQ(BottomClobber, Phi);
642
643 // This query will first check the cache for {%a, BStore}. It should point to
644 // FirstStore, not to the store after FirstStore.
645 MemoryAccess *UseClobber = Walker->getClobberingMemoryAccess(ALoad);
646 EXPECT_EQ(UseClobber, MSSA.getMemoryAccess(FirstStore));
647}
George Burgess IV024f3d22016-08-03 19:57:02 +0000648
649// Test that our walker properly handles loads with the invariant group
650// attribute. It's a bit hacky, since we add the invariant attribute *after*
651// building MSSA. Otherwise, the use optimizer will optimize it for us, which
652// isn't what we want.
653// FIXME: It may be easier/cleaner to just add an 'optimize uses?' flag to MSSA.
654TEST_F(MemorySSATest, WalkerInvariantLoadOpt) {
655 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
656 GlobalValue::ExternalLinkage, "F", &M);
657 B.SetInsertPoint(BasicBlock::Create(C, "", F));
658 Type *Int8 = Type::getInt8Ty(C);
659 Constant *One = ConstantInt::get(Int8, 1);
660 Value *AllocA = B.CreateAlloca(Int8, One, "");
661
662 Instruction *Store = B.CreateStore(One, AllocA);
663 Instruction *Load = B.CreateLoad(AllocA);
664
665 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000666 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IV024f3d22016-08-03 19:57:02 +0000667 MemorySSAWalker *Walker = Analyses->Walker;
668
669 auto *LoadMA = cast<MemoryUse>(MSSA.getMemoryAccess(Load));
670 auto *StoreMA = cast<MemoryDef>(MSSA.getMemoryAccess(Store));
671 EXPECT_EQ(LoadMA->getDefiningAccess(), StoreMA);
672
673 // ...At the time of writing, no cache should exist for LoadMA. Be a bit
674 // flexible to future changes.
675 Walker->invalidateInfo(LoadMA);
676 Load->setMetadata(LLVMContext::MD_invariant_load, MDNode::get(C, {}));
677
678 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LoadMA);
679 EXPECT_EQ(LoadClobber, MSSA.getLiveOnEntryDef());
680}
George Burgess IV80ed8632016-10-03 23:12:35 +0000681
Daniel Berlincd2deac2016-10-20 20:13:45 +0000682// Test loads get reoptimized properly by the walker.
683TEST_F(MemorySSATest, WalkerReopt) {
George Burgess IV80ed8632016-10-03 23:12:35 +0000684 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
685 GlobalValue::ExternalLinkage, "F", &M);
686 B.SetInsertPoint(BasicBlock::Create(C, "", F));
George Burgess IV80ed8632016-10-03 23:12:35 +0000687 Type *Int8 = Type::getInt8Ty(C);
Daniel Berlincd2deac2016-10-20 20:13:45 +0000688 Value *AllocaA = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
689 Instruction *SIA = B.CreateStore(ConstantInt::get(Int8, 0), AllocaA);
690 Value *AllocaB = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
691 Instruction *SIB = B.CreateStore(ConstantInt::get(Int8, 0), AllocaB);
692 Instruction *LIA = B.CreateLoad(AllocaA);
George Burgess IV80ed8632016-10-03 23:12:35 +0000693
694 setupAnalyses();
695 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlincd2deac2016-10-20 20:13:45 +0000696 MemorySSAWalker *Walker = Analyses->Walker;
697
698 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LIA);
699 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LIA));
700 EXPECT_EQ(LoadClobber, MSSA.getMemoryAccess(SIA));
701 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(SIA)));
702 MSSA.removeMemoryAccess(LoadAccess);
703
704 // Create the load memory access pointing to an unoptimized place.
705 MemoryUse *NewLoadAccess = cast<MemoryUse>(MSSA.createMemoryAccessInBB(
706 LIA, MSSA.getMemoryAccess(SIB), LIA->getParent(), MemorySSA::End));
707 // This should it cause it to be optimized
708 EXPECT_EQ(Walker->getClobberingMemoryAccess(NewLoadAccess), LoadClobber);
709 EXPECT_EQ(NewLoadAccess->getDefiningAccess(), LoadClobber);
George Burgess IV80ed8632016-10-03 23:12:35 +0000710}
Bryant Wong4213d942016-12-25 23:34:07 +0000711
Daniel Berlin60ead052017-01-28 01:23:13 +0000712// Test out MemorySSAUpdater::moveBefore
713TEST_F(MemorySSATest, MoveAboveMemoryDef) {
Bryant Wong4213d942016-12-25 23:34:07 +0000714 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
715 GlobalValue::ExternalLinkage, "F", &M);
716 B.SetInsertPoint(BasicBlock::Create(C, "", F));
717
718 Type *Int8 = Type::getInt8Ty(C);
719 Value *A = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
720 Value *B_ = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
721 Value *C = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "C");
722
723 StoreInst *StoreA0 = B.CreateStore(ConstantInt::get(Int8, 0), A);
724 StoreInst *StoreB = B.CreateStore(ConstantInt::get(Int8, 0), B_);
725 LoadInst *LoadB = B.CreateLoad(B_);
726 StoreInst *StoreA1 = B.CreateStore(ConstantInt::get(Int8, 4), A);
Bryant Wong4213d942016-12-25 23:34:07 +0000727 StoreInst *StoreC = B.CreateStore(ConstantInt::get(Int8, 4), C);
728 StoreInst *StoreA2 = B.CreateStore(ConstantInt::get(Int8, 4), A);
729 LoadInst *LoadC = B.CreateLoad(C);
730
731 setupAnalyses();
732 MemorySSA &MSSA = *Analyses->MSSA;
733 MemorySSAWalker &Walker = *Analyses->Walker;
734
Daniel Berlin60ead052017-01-28 01:23:13 +0000735 MemorySSAUpdater Updater(&MSSA);
Bryant Wong4213d942016-12-25 23:34:07 +0000736 StoreC->moveBefore(StoreB);
Daniel Berlin60ead052017-01-28 01:23:13 +0000737 Updater.moveBefore(cast<MemoryDef>(MSSA.getMemoryAccess(StoreC)),
738 cast<MemoryDef>(MSSA.getMemoryAccess(StoreB)));
Bryant Wong4213d942016-12-25 23:34:07 +0000739
740 MSSA.verifyMemorySSA();
741
742 EXPECT_EQ(MSSA.getMemoryAccess(StoreB)->getDefiningAccess(),
743 MSSA.getMemoryAccess(StoreC));
744 EXPECT_EQ(MSSA.getMemoryAccess(StoreC)->getDefiningAccess(),
745 MSSA.getMemoryAccess(StoreA0));
746 EXPECT_EQ(MSSA.getMemoryAccess(StoreA2)->getDefiningAccess(),
747 MSSA.getMemoryAccess(StoreA1));
748 EXPECT_EQ(Walker.getClobberingMemoryAccess(LoadB),
749 MSSA.getMemoryAccess(StoreB));
750 EXPECT_EQ(Walker.getClobberingMemoryAccess(LoadC),
751 MSSA.getMemoryAccess(StoreC));
752
753 // exercise block numbering
754 EXPECT_TRUE(MSSA.locallyDominates(MSSA.getMemoryAccess(StoreC),
755 MSSA.getMemoryAccess(StoreB)));
756 EXPECT_TRUE(MSSA.locallyDominates(MSSA.getMemoryAccess(StoreA1),
757 MSSA.getMemoryAccess(StoreA2)));
758}
Daniel Berlin60ead052017-01-28 01:23:13 +0000759
760TEST_F(MemorySSATest, Irreducible) {
761 // Create the equivalent of
762 // x = something
763 // if (...)
764 // goto second_loop_entry
765 // while (...) {
766 // second_loop_entry:
767 // }
768 // use(x)
769
770 SmallVector<PHINode *, 8> Inserted;
771 IRBuilder<> B(C);
772 F = Function::Create(
773 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
774 GlobalValue::ExternalLinkage, "F", &M);
775
776 // Make blocks
777 BasicBlock *IfBB = BasicBlock::Create(C, "if", F);
778 BasicBlock *LoopStartBB = BasicBlock::Create(C, "loopstart", F);
779 BasicBlock *LoopMainBB = BasicBlock::Create(C, "loopmain", F);
780 BasicBlock *AfterLoopBB = BasicBlock::Create(C, "afterloop", F);
781 B.SetInsertPoint(IfBB);
782 B.CreateCondBr(B.getTrue(), LoopMainBB, LoopStartBB);
783 B.SetInsertPoint(LoopStartBB);
784 B.CreateBr(LoopMainBB);
785 B.SetInsertPoint(LoopMainBB);
786 B.CreateCondBr(B.getTrue(), LoopStartBB, AfterLoopBB);
787 B.SetInsertPoint(AfterLoopBB);
788 Argument *FirstArg = &*F->arg_begin();
789 setupAnalyses();
790 MemorySSA &MSSA = *Analyses->MSSA;
791 MemorySSAUpdater Updater(&MSSA);
792 // Create the load memory acccess
793 LoadInst *LoadInst = B.CreateLoad(FirstArg);
794 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.createMemoryAccessInBB(
795 LoadInst, nullptr, AfterLoopBB, MemorySSA::Beginning));
796 Updater.insertUse(LoadAccess);
797 MSSA.verifyMemorySSA();
798}