1 //===- MemorySSA.cpp - Unit tests for MemorySSA ---------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "llvm/Analysis/MemorySSA.h"
9 #include "llvm/Analysis/AliasAnalysis.h"
10 #include "llvm/Analysis/AssumptionCache.h"
11 #include "llvm/Analysis/BasicAliasAnalysis.h"
12 #include "llvm/Analysis/MemorySSAUpdater.h"
13 #include "llvm/Analysis/TargetLibraryInfo.h"
14 #include "llvm/AsmParser/Parser.h"
15 #include "llvm/IR/BasicBlock.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/Dominators.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "gtest/gtest.h"
23 
24 using namespace llvm;
25 
26 const static char DLString[] = "e-i64:64-f80:128-n8:16:32:64-S128";
27 
28 /// There's a lot of common setup between these tests. This fixture helps reduce
29 /// that. Tests should mock up a function, store it in F, and then call
30 /// setupAnalyses().
31 class MemorySSATest : public testing::Test {
32 protected:
33   // N.B. Many of these members depend on each other (e.g. the Module depends on
34   // the Context, etc.). So, order matters here (and in TestAnalyses).
35   LLVMContext C;
36   Module M;
37   IRBuilder<> B;
38   DataLayout DL;
39   TargetLibraryInfoImpl TLII;
40   TargetLibraryInfo TLI;
41   Function *F;
42 
43   // Things that we need to build after the function is created.
44   struct TestAnalyses {
45     DominatorTree DT;
46     AssumptionCache AC;
47     AAResults AA;
48     BasicAAResult BAA;
49     // We need to defer MSSA construction until AA is *entirely* set up, which
50     // requires calling addAAResult. Hence, we just use a pointer here.
51     std::unique_ptr<MemorySSA> MSSA;
52     MemorySSAWalker *Walker;
53 
54     TestAnalyses(MemorySSATest &Test)
55         : DT(*Test.F), AC(*Test.F), AA(Test.TLI),
56           BAA(Test.DL, *Test.F, Test.TLI, AC, &DT) {
57       AA.addAAResult(BAA);
58       MSSA = std::make_unique<MemorySSA>(*Test.F, &AA, &DT);
59       Walker = MSSA->getWalker();
60     }
61   };
62 
63   std::unique_ptr<TestAnalyses> Analyses;
64 
65   void setupAnalyses() {
66     assert(F);
67     Analyses.reset(new TestAnalyses(*this));
68   }
69 
70 public:
71   MemorySSATest()
72       : M("MemorySSATest", C), B(C), DL(DLString), TLI(TLII), F(nullptr) {}
73 };
74 
75 TEST_F(MemorySSATest, CreateALoad) {
76   // We create a diamond where there is a store on one side, and then after
77   // building MemorySSA, create a load after the merge point, and use it to test
78   // updating by creating an access for the load.
79   F = Function::Create(
80       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
81       GlobalValue::ExternalLinkage, "F", &M);
82   BasicBlock *Entry(BasicBlock::Create(C, "", F));
83   BasicBlock *Left(BasicBlock::Create(C, "", F));
84   BasicBlock *Right(BasicBlock::Create(C, "", F));
85   BasicBlock *Merge(BasicBlock::Create(C, "", F));
86   B.SetInsertPoint(Entry);
87   B.CreateCondBr(B.getTrue(), Left, Right);
88   B.SetInsertPoint(Left);
89   Argument *PointerArg = &*F->arg_begin();
90   B.CreateStore(B.getInt8(16), PointerArg);
91   BranchInst::Create(Merge, Left);
92   BranchInst::Create(Merge, Right);
93 
94   setupAnalyses();
95   MemorySSA &MSSA = *Analyses->MSSA;
96   MemorySSAUpdater Updater(&MSSA);
97   // Add the load
98   B.SetInsertPoint(Merge);
99   LoadInst *LoadInst = B.CreateLoad(B.getInt8Ty(), PointerArg);
100 
101   // MemoryPHI should already exist.
102   MemoryPhi *MP = MSSA.getMemoryAccess(Merge);
103   EXPECT_NE(MP, nullptr);
104 
105   // Create the load memory acccess
106   MemoryUse *LoadAccess = cast<MemoryUse>(Updater.createMemoryAccessInBB(
107       LoadInst, MP, Merge, MemorySSA::Beginning));
108   MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
109   EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
110   MSSA.verifyMemorySSA();
111 }
112 TEST_F(MemorySSATest, CreateLoadsAndStoreUpdater) {
113   // We create a diamond, then build memoryssa with no memory accesses, and
114   // incrementally update it by inserting a store in the, entry, a load in the
115   // merge point, then a store in the branch, another load in the merge point,
116   // and then a store in the entry.
117   F = Function::Create(
118       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
119       GlobalValue::ExternalLinkage, "F", &M);
120   BasicBlock *Entry(BasicBlock::Create(C, "", F));
121   BasicBlock *Left(BasicBlock::Create(C, "", F));
122   BasicBlock *Right(BasicBlock::Create(C, "", F));
123   BasicBlock *Merge(BasicBlock::Create(C, "", F));
124   B.SetInsertPoint(Entry);
125   B.CreateCondBr(B.getTrue(), Left, Right);
126   B.SetInsertPoint(Left, Left->begin());
127   Argument *PointerArg = &*F->arg_begin();
128   B.SetInsertPoint(Left);
129   B.CreateBr(Merge);
130   B.SetInsertPoint(Right);
131   B.CreateBr(Merge);
132 
133   setupAnalyses();
134   MemorySSA &MSSA = *Analyses->MSSA;
135   MemorySSAUpdater Updater(&MSSA);
136   // Add the store
137   B.SetInsertPoint(Entry, Entry->begin());
138   StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
139   MemoryAccess *EntryStoreAccess = Updater.createMemoryAccessInBB(
140       EntryStore, nullptr, Entry, MemorySSA::Beginning);
141   Updater.insertDef(cast<MemoryDef>(EntryStoreAccess));
142 
143   // Add the load
144   B.SetInsertPoint(Merge, Merge->begin());
145   LoadInst *FirstLoad = B.CreateLoad(B.getInt8Ty(), PointerArg);
146 
147   // MemoryPHI should not already exist.
148   MemoryPhi *MP = MSSA.getMemoryAccess(Merge);
149   EXPECT_EQ(MP, nullptr);
150 
151   // Create the load memory access
152   MemoryUse *FirstLoadAccess = cast<MemoryUse>(Updater.createMemoryAccessInBB(
153       FirstLoad, nullptr, Merge, MemorySSA::Beginning));
154   Updater.insertUse(FirstLoadAccess);
155   // Should just have a load using the entry access, because it should discover
156   // the phi is trivial
157   EXPECT_EQ(FirstLoadAccess->getDefiningAccess(), EntryStoreAccess);
158 
159   // Create a store on the left
160   // Add the store
161   B.SetInsertPoint(Left, Left->begin());
162   StoreInst *LeftStore = B.CreateStore(B.getInt8(16), PointerArg);
163   MemoryAccess *LeftStoreAccess = Updater.createMemoryAccessInBB(
164       LeftStore, nullptr, Left, MemorySSA::Beginning);
165   Updater.insertDef(cast<MemoryDef>(LeftStoreAccess), false);
166 
167   // MemoryPHI should exist after adding LeftStore.
168   MP = MSSA.getMemoryAccess(Merge);
169   EXPECT_NE(MP, nullptr);
170 
171   // Add the second load
172   B.SetInsertPoint(Merge, Merge->begin());
173   LoadInst *SecondLoad = B.CreateLoad(B.getInt8Ty(), PointerArg);
174 
175   // Create the load memory access
176   MemoryUse *SecondLoadAccess = cast<MemoryUse>(Updater.createMemoryAccessInBB(
177       SecondLoad, nullptr, Merge, MemorySSA::Beginning));
178   Updater.insertUse(SecondLoadAccess);
179   // Now the load should be a phi of the entry store and the left store
180   MemoryPhi *MergePhi =
181       dyn_cast<MemoryPhi>(SecondLoadAccess->getDefiningAccess());
182   EXPECT_NE(MergePhi, nullptr);
183   EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
184   EXPECT_EQ(MergePhi->getIncomingValue(1), LeftStoreAccess);
185   // Now create a store below the existing one in the entry
186   B.SetInsertPoint(Entry, --Entry->end());
187   StoreInst *SecondEntryStore = B.CreateStore(B.getInt8(16), PointerArg);
188   MemoryAccess *SecondEntryStoreAccess = Updater.createMemoryAccessInBB(
189       SecondEntryStore, nullptr, Entry, MemorySSA::End);
190   // Insert it twice just to test renaming
191   Updater.insertDef(cast<MemoryDef>(SecondEntryStoreAccess), false);
192   EXPECT_NE(FirstLoadAccess->getDefiningAccess(), MergePhi);
193   Updater.insertDef(cast<MemoryDef>(SecondEntryStoreAccess), true);
194   EXPECT_EQ(FirstLoadAccess->getDefiningAccess(), MergePhi);
195   // and make sure the phi below it got updated, despite being blocks away
196   MergePhi = dyn_cast<MemoryPhi>(SecondLoadAccess->getDefiningAccess());
197   EXPECT_NE(MergePhi, nullptr);
198   EXPECT_EQ(MergePhi->getIncomingValue(0), SecondEntryStoreAccess);
199   EXPECT_EQ(MergePhi->getIncomingValue(1), LeftStoreAccess);
200   MSSA.verifyMemorySSA();
201 }
202 
203 TEST_F(MemorySSATest, CreateALoadUpdater) {
204   // We create a diamond, then build memoryssa with no memory accesses, and
205   // incrementally update it by inserting a store in one of the branches, and a
206   // load in the merge point
207   F = Function::Create(
208       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
209       GlobalValue::ExternalLinkage, "F", &M);
210   BasicBlock *Entry(BasicBlock::Create(C, "", F));
211   BasicBlock *Left(BasicBlock::Create(C, "", F));
212   BasicBlock *Right(BasicBlock::Create(C, "", F));
213   BasicBlock *Merge(BasicBlock::Create(C, "", F));
214   B.SetInsertPoint(Entry);
215   B.CreateCondBr(B.getTrue(), Left, Right);
216   B.SetInsertPoint(Left, Left->begin());
217   Argument *PointerArg = &*F->arg_begin();
218   B.SetInsertPoint(Left);
219   B.CreateBr(Merge);
220   B.SetInsertPoint(Right);
221   B.CreateBr(Merge);
222 
223   setupAnalyses();
224   MemorySSA &MSSA = *Analyses->MSSA;
225   MemorySSAUpdater Updater(&MSSA);
226   B.SetInsertPoint(Left, Left->begin());
227   // Add the store
228   StoreInst *SI = B.CreateStore(B.getInt8(16), PointerArg);
229   MemoryAccess *StoreAccess =
230       Updater.createMemoryAccessInBB(SI, nullptr, Left, MemorySSA::Beginning);
231   Updater.insertDef(cast<MemoryDef>(StoreAccess));
232 
233   // MemoryPHI should be created when inserting the def
234   MemoryPhi *MP = MSSA.getMemoryAccess(Merge);
235   EXPECT_NE(MP, nullptr);
236 
237   // Add the load
238   B.SetInsertPoint(Merge, Merge->begin());
239   LoadInst *LoadInst = B.CreateLoad(B.getInt8Ty(), PointerArg);
240 
241   // Create the load memory acccess
242   MemoryUse *LoadAccess = cast<MemoryUse>(Updater.createMemoryAccessInBB(
243       LoadInst, nullptr, Merge, MemorySSA::Beginning));
244   Updater.insertUse(LoadAccess);
245   MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
246   EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
247   MSSA.verifyMemorySSA();
248 }
249 
250 TEST_F(MemorySSATest, SinkLoad) {
251   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   B.CreateCondBr(B.getTrue(), Left, Right);
260   B.SetInsertPoint(Left, Left->begin());
261   Argument *PointerArg = &*F->arg_begin();
262   B.SetInsertPoint(Left);
263   B.CreateBr(Merge);
264   B.SetInsertPoint(Right);
265   B.CreateBr(Merge);
266 
267   // Load in left block
268   B.SetInsertPoint(Left, Left->begin());
269   LoadInst *LoadInst1 = B.CreateLoad(B.getInt8Ty(), PointerArg);
270   // Store in merge block
271   B.SetInsertPoint(Merge, Merge->begin());
272   B.CreateStore(B.getInt8(16), PointerArg);
273 
274   setupAnalyses();
275   MemorySSA &MSSA = *Analyses->MSSA;
276   MemorySSAUpdater Updater(&MSSA);
277 
278   // Mimic sinking of a load:
279   // - clone load
280   // - insert in "exit" block
281   // - insert in mssa
282   // - remove from original block
283 
284   LoadInst *LoadInstClone = cast<LoadInst>(LoadInst1->clone());
285   Merge->getInstList().insert(Merge->begin(), LoadInstClone);
286   MemoryAccess * NewLoadAccess =
287       Updater.createMemoryAccessInBB(LoadInstClone, nullptr,
288                                      LoadInstClone->getParent(),
289                                      MemorySSA::Beginning);
290   Updater.insertUse(cast<MemoryUse>(NewLoadAccess));
291   MSSA.verifyMemorySSA();
292   Updater.removeMemoryAccess(MSSA.getMemoryAccess(LoadInst1));
293   MSSA.verifyMemorySSA();
294 }
295 
296 TEST_F(MemorySSATest, MoveAStore) {
297   // We create a diamond where there is a in the entry, a store on one side, and
298   // a load at the end.  After building MemorySSA, we test updating by moving
299   // the store from the side block to the entry block. This destroys the old
300   // access.
301   F = Function::Create(
302       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
303       GlobalValue::ExternalLinkage, "F", &M);
304   BasicBlock *Entry(BasicBlock::Create(C, "", F));
305   BasicBlock *Left(BasicBlock::Create(C, "", F));
306   BasicBlock *Right(BasicBlock::Create(C, "", F));
307   BasicBlock *Merge(BasicBlock::Create(C, "", F));
308   B.SetInsertPoint(Entry);
309   Argument *PointerArg = &*F->arg_begin();
310   StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
311   B.CreateCondBr(B.getTrue(), Left, Right);
312   B.SetInsertPoint(Left);
313   StoreInst *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
314   BranchInst::Create(Merge, Left);
315   BranchInst::Create(Merge, Right);
316   B.SetInsertPoint(Merge);
317   B.CreateLoad(B.getInt8Ty(), PointerArg);
318   setupAnalyses();
319   MemorySSA &MSSA = *Analyses->MSSA;
320   MemorySSAUpdater Updater(&MSSA);
321   // Move the store
322   SideStore->moveBefore(Entry->getTerminator());
323   MemoryAccess *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
324   MemoryAccess *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
325   MemoryAccess *NewStoreAccess = Updater.createMemoryAccessAfter(
326       SideStore, EntryStoreAccess, EntryStoreAccess);
327   EntryStoreAccess->replaceAllUsesWith(NewStoreAccess);
328   Updater.removeMemoryAccess(SideStoreAccess);
329   MSSA.verifyMemorySSA();
330 }
331 
332 TEST_F(MemorySSATest, MoveAStoreUpdater) {
333   // We create a diamond where there is a in the entry, a store on one side, and
334   // a load at the end.  After building MemorySSA, we test updating by moving
335   // the store from the side block to the entry block.  This destroys the old
336   // access.
337   F = Function::Create(
338       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
339       GlobalValue::ExternalLinkage, "F", &M);
340   BasicBlock *Entry(BasicBlock::Create(C, "", F));
341   BasicBlock *Left(BasicBlock::Create(C, "", F));
342   BasicBlock *Right(BasicBlock::Create(C, "", F));
343   BasicBlock *Merge(BasicBlock::Create(C, "", F));
344   B.SetInsertPoint(Entry);
345   Argument *PointerArg = &*F->arg_begin();
346   StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
347   B.CreateCondBr(B.getTrue(), Left, Right);
348   B.SetInsertPoint(Left);
349   auto *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
350   BranchInst::Create(Merge, Left);
351   BranchInst::Create(Merge, Right);
352   B.SetInsertPoint(Merge);
353   auto *MergeLoad = B.CreateLoad(B.getInt8Ty(), PointerArg);
354   setupAnalyses();
355   MemorySSA &MSSA = *Analyses->MSSA;
356   MemorySSAUpdater Updater(&MSSA);
357 
358   // Move the store
359   SideStore->moveBefore(Entry->getTerminator());
360   auto *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
361   auto *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
362   auto *NewStoreAccess = Updater.createMemoryAccessAfter(
363       SideStore, EntryStoreAccess, EntryStoreAccess);
364   // Before, the load will point to a phi of the EntryStore and SideStore.
365   auto *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(MergeLoad));
366   EXPECT_TRUE(isa<MemoryPhi>(LoadAccess->getDefiningAccess()));
367   MemoryPhi *MergePhi = cast<MemoryPhi>(LoadAccess->getDefiningAccess());
368   EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
369   EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
370   Updater.removeMemoryAccess(SideStoreAccess);
371   Updater.insertDef(cast<MemoryDef>(NewStoreAccess));
372   // After it's a phi of the new side store access.
373   EXPECT_EQ(MergePhi->getIncomingValue(0), NewStoreAccess);
374   EXPECT_EQ(MergePhi->getIncomingValue(1), NewStoreAccess);
375   MSSA.verifyMemorySSA();
376 }
377 
378 TEST_F(MemorySSATest, MoveAStoreUpdaterMove) {
379   // We create a diamond where there is a in the entry, a store on one side, and
380   // a load at the end.  After building MemorySSA, we test updating by moving
381   // the store from the side block to the entry block.  This does not destroy
382   // the old access.
383   F = Function::Create(
384       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
385       GlobalValue::ExternalLinkage, "F", &M);
386   BasicBlock *Entry(BasicBlock::Create(C, "", F));
387   BasicBlock *Left(BasicBlock::Create(C, "", F));
388   BasicBlock *Right(BasicBlock::Create(C, "", F));
389   BasicBlock *Merge(BasicBlock::Create(C, "", F));
390   B.SetInsertPoint(Entry);
391   Argument *PointerArg = &*F->arg_begin();
392   StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
393   B.CreateCondBr(B.getTrue(), Left, Right);
394   B.SetInsertPoint(Left);
395   auto *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
396   BranchInst::Create(Merge, Left);
397   BranchInst::Create(Merge, Right);
398   B.SetInsertPoint(Merge);
399   auto *MergeLoad = B.CreateLoad(B.getInt8Ty(), PointerArg);
400   setupAnalyses();
401   MemorySSA &MSSA = *Analyses->MSSA;
402   MemorySSAUpdater Updater(&MSSA);
403 
404   // Move the store
405   auto *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
406   auto *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
407   // Before, the load will point to a phi of the EntryStore and SideStore.
408   auto *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(MergeLoad));
409   EXPECT_TRUE(isa<MemoryPhi>(LoadAccess->getDefiningAccess()));
410   MemoryPhi *MergePhi = cast<MemoryPhi>(LoadAccess->getDefiningAccess());
411   EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
412   EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
413   SideStore->moveBefore(*EntryStore->getParent(), ++EntryStore->getIterator());
414   Updater.moveAfter(SideStoreAccess, EntryStoreAccess);
415   // After, it's a phi of the side store.
416   EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
417   EXPECT_EQ(MergePhi->getIncomingValue(1), SideStoreAccess);
418 
419   MSSA.verifyMemorySSA();
420 }
421 
422 TEST_F(MemorySSATest, MoveAStoreAllAround) {
423   // We create a diamond where there is a in the entry, a store on one side, and
424   // a load at the end.  After building MemorySSA, we test updating by moving
425   // the store from the side block to the entry block, then to the other side
426   // block, then to before the load.  This does not destroy the old access.
427   F = Function::Create(
428       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
429       GlobalValue::ExternalLinkage, "F", &M);
430   BasicBlock *Entry(BasicBlock::Create(C, "", F));
431   BasicBlock *Left(BasicBlock::Create(C, "", F));
432   BasicBlock *Right(BasicBlock::Create(C, "", F));
433   BasicBlock *Merge(BasicBlock::Create(C, "", F));
434   B.SetInsertPoint(Entry);
435   Argument *PointerArg = &*F->arg_begin();
436   StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
437   B.CreateCondBr(B.getTrue(), Left, Right);
438   B.SetInsertPoint(Left);
439   auto *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
440   BranchInst::Create(Merge, Left);
441   BranchInst::Create(Merge, Right);
442   B.SetInsertPoint(Merge);
443   auto *MergeLoad = B.CreateLoad(B.getInt8Ty(), PointerArg);
444   setupAnalyses();
445   MemorySSA &MSSA = *Analyses->MSSA;
446   MemorySSAUpdater Updater(&MSSA);
447 
448   // Move the store
449   auto *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
450   auto *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
451   // Before, the load will point to a phi of the EntryStore and SideStore.
452   auto *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(MergeLoad));
453   EXPECT_TRUE(isa<MemoryPhi>(LoadAccess->getDefiningAccess()));
454   MemoryPhi *MergePhi = cast<MemoryPhi>(LoadAccess->getDefiningAccess());
455   EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
456   EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
457   // Move the store before the entry store
458   SideStore->moveBefore(*EntryStore->getParent(), EntryStore->getIterator());
459   Updater.moveBefore(SideStoreAccess, EntryStoreAccess);
460   // After, it's a phi of the entry store.
461   EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
462   EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
463   MSSA.verifyMemorySSA();
464   // Now move the store to the right branch
465   SideStore->moveBefore(*Right, Right->begin());
466   Updater.moveToPlace(SideStoreAccess, Right, MemorySSA::Beginning);
467   MSSA.verifyMemorySSA();
468   EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
469   EXPECT_EQ(MergePhi->getIncomingValue(1), SideStoreAccess);
470   // Now move it before the load
471   SideStore->moveBefore(MergeLoad);
472   Updater.moveBefore(SideStoreAccess, LoadAccess);
473   EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
474   EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
475   MSSA.verifyMemorySSA();
476 }
477 
478 TEST_F(MemorySSATest, RemoveAPhi) {
479   // We create a diamond where there is a store on one side, and then a load
480   // after the merge point.  This enables us to test a bunch of different
481   // removal cases.
482   F = Function::Create(
483       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
484       GlobalValue::ExternalLinkage, "F", &M);
485   BasicBlock *Entry(BasicBlock::Create(C, "", F));
486   BasicBlock *Left(BasicBlock::Create(C, "", F));
487   BasicBlock *Right(BasicBlock::Create(C, "", F));
488   BasicBlock *Merge(BasicBlock::Create(C, "", F));
489   B.SetInsertPoint(Entry);
490   B.CreateCondBr(B.getTrue(), Left, Right);
491   B.SetInsertPoint(Left);
492   Argument *PointerArg = &*F->arg_begin();
493   StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
494   BranchInst::Create(Merge, Left);
495   BranchInst::Create(Merge, Right);
496   B.SetInsertPoint(Merge);
497   LoadInst *LoadInst = B.CreateLoad(B.getInt8Ty(), PointerArg);
498 
499   setupAnalyses();
500   MemorySSA &MSSA = *Analyses->MSSA;
501   MemorySSAUpdater Updater(&MSSA);
502 
503   // Before, the load will be a use of a phi<store, liveonentry>.
504   MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst));
505   MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
506   MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
507   EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
508   // Kill the store
509   Updater.removeMemoryAccess(StoreAccess);
510   MemoryPhi *MP = cast<MemoryPhi>(DefiningAccess);
511   // Verify the phi ended up as liveonentry, liveonentry
512   for (auto &Op : MP->incoming_values())
513     EXPECT_TRUE(MSSA.isLiveOnEntryDef(cast<MemoryAccess>(Op.get())));
514   // Replace the phi uses with the live on entry def
515   MP->replaceAllUsesWith(MSSA.getLiveOnEntryDef());
516   // Verify the load is now defined by liveOnEntryDef
517   EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
518   // Remove the PHI
519   Updater.removeMemoryAccess(MP);
520   MSSA.verifyMemorySSA();
521 }
522 
523 TEST_F(MemorySSATest, RemoveMemoryAccess) {
524   // We create a diamond where there is a store on one side, and then a load
525   // after the merge point.  This enables us to test a bunch of different
526   // removal cases.
527   F = Function::Create(
528       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
529       GlobalValue::ExternalLinkage, "F", &M);
530   BasicBlock *Entry(BasicBlock::Create(C, "", F));
531   BasicBlock *Left(BasicBlock::Create(C, "", F));
532   BasicBlock *Right(BasicBlock::Create(C, "", F));
533   BasicBlock *Merge(BasicBlock::Create(C, "", F));
534   B.SetInsertPoint(Entry);
535   B.CreateCondBr(B.getTrue(), Left, Right);
536   B.SetInsertPoint(Left);
537   Argument *PointerArg = &*F->arg_begin();
538   StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
539   BranchInst::Create(Merge, Left);
540   BranchInst::Create(Merge, Right);
541   B.SetInsertPoint(Merge);
542   LoadInst *LoadInst = B.CreateLoad(B.getInt8Ty(), PointerArg);
543 
544   setupAnalyses();
545   MemorySSA &MSSA = *Analyses->MSSA;
546   MemorySSAWalker *Walker = Analyses->Walker;
547   MemorySSAUpdater Updater(&MSSA);
548 
549   // Before, the load will be a use of a phi<store, liveonentry>. It should be
550   // the same after.
551   MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst));
552   MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
553   MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
554   EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
555   // The load is currently clobbered by one of the phi arguments, so the walker
556   // should determine the clobbering access as the phi.
557   EXPECT_EQ(DefiningAccess, Walker->getClobberingMemoryAccess(LoadInst));
558   Updater.removeMemoryAccess(StoreAccess);
559   MSSA.verifyMemorySSA();
560   // After the removeaccess, let's see if we got the right accesses
561   // The load should still point to the phi ...
562   EXPECT_EQ(DefiningAccess, LoadAccess->getDefiningAccess());
563   // but we should now get live on entry for the clobbering definition of the
564   // load, since it will walk past the phi node since every argument is the
565   // same.
566   // XXX: This currently requires either removing the phi or resetting optimized
567   // on the load
568 
569   EXPECT_FALSE(
570       MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst)));
571   // If we reset optimized, we get live on entry.
572   LoadAccess->resetOptimized();
573   EXPECT_TRUE(
574       MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst)));
575   // The phi should now be a two entry phi with two live on entry defs.
576   for (const auto &Op : DefiningAccess->operands()) {
577     MemoryAccess *Operand = cast<MemoryAccess>(&*Op);
578     EXPECT_TRUE(MSSA.isLiveOnEntryDef(Operand));
579   }
580 
581   // Now we try to remove the single valued phi
582   Updater.removeMemoryAccess(DefiningAccess);
583   MSSA.verifyMemorySSA();
584   // Now the load should be a load of live on entry.
585   EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
586 }
587 
588 // We had a bug with caching where the walker would report MemoryDef#3's clobber
589 // (below) was MemoryDef#1.
590 //
591 // define void @F(i8*) {
592 //   %A = alloca i8, i8 1
593 // ; 1 = MemoryDef(liveOnEntry)
594 //   store i8 0, i8* %A
595 // ; 2 = MemoryDef(1)
596 //   store i8 1, i8* %A
597 // ; 3 = MemoryDef(2)
598 //   store i8 2, i8* %A
599 // }
600 TEST_F(MemorySSATest, TestTripleStore) {
601   F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
602                        GlobalValue::ExternalLinkage, "F", &M);
603   B.SetInsertPoint(BasicBlock::Create(C, "", F));
604   Type *Int8 = Type::getInt8Ty(C);
605   Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
606   StoreInst *S1 = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
607   StoreInst *S2 = B.CreateStore(ConstantInt::get(Int8, 1), Alloca);
608   StoreInst *S3 = B.CreateStore(ConstantInt::get(Int8, 2), Alloca);
609 
610   setupAnalyses();
611   MemorySSA &MSSA = *Analyses->MSSA;
612   MemorySSAWalker *Walker = Analyses->Walker;
613 
614   unsigned I = 0;
615   for (StoreInst *V : {S1, S2, S3}) {
616     // Everything should be clobbered by its defining access
617     MemoryAccess *DefiningAccess = MSSA.getMemoryAccess(V)->getDefiningAccess();
618     MemoryAccess *WalkerClobber = Walker->getClobberingMemoryAccess(V);
619     EXPECT_EQ(DefiningAccess, WalkerClobber)
620         << "Store " << I << " doesn't have the correct clobbering access";
621     // EXPECT_EQ expands such that if we increment I above, it won't get
622     // incremented except when we try to print the error message.
623     ++I;
624   }
625 }
626 
627 // ...And fixing the above bug made it obvious that, when walking, MemorySSA's
628 // walker was caching the initial node it walked. This was fine (albeit
629 // mostly redundant) unless the initial node being walked is a clobber for the
630 // query. In that case, we'd cache that the node clobbered itself.
631 TEST_F(MemorySSATest, TestStoreAndLoad) {
632   F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
633                        GlobalValue::ExternalLinkage, "F", &M);
634   B.SetInsertPoint(BasicBlock::Create(C, "", F));
635   Type *Int8 = Type::getInt8Ty(C);
636   Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
637   Instruction *SI = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
638   Instruction *LI = B.CreateLoad(Int8, Alloca);
639 
640   setupAnalyses();
641   MemorySSA &MSSA = *Analyses->MSSA;
642   MemorySSAWalker *Walker = Analyses->Walker;
643 
644   MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LI);
645   EXPECT_EQ(LoadClobber, MSSA.getMemoryAccess(SI));
646   EXPECT_TRUE(MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(SI)));
647 }
648 
649 // Another bug (related to the above two fixes): It was noted that, given the
650 // following code:
651 // ; 1 = MemoryDef(liveOnEntry)
652 // store i8 0, i8* %1
653 //
654 // ...A query to getClobberingMemoryAccess(MemoryAccess*, MemoryLocation) would
655 // hand back the store (correctly). A later call to
656 // getClobberingMemoryAccess(const Instruction*) would also hand back the store
657 // (incorrectly; it should return liveOnEntry).
658 //
659 // This test checks that repeated calls to either function returns what they're
660 // meant to.
661 TEST_F(MemorySSATest, TestStoreDoubleQuery) {
662   F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
663                        GlobalValue::ExternalLinkage, "F", &M);
664   B.SetInsertPoint(BasicBlock::Create(C, "", F));
665   Type *Int8 = Type::getInt8Ty(C);
666   Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
667   StoreInst *SI = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
668 
669   setupAnalyses();
670   MemorySSA &MSSA = *Analyses->MSSA;
671   MemorySSAWalker *Walker = Analyses->Walker;
672 
673   MemoryAccess *StoreAccess = MSSA.getMemoryAccess(SI);
674   MemoryLocation StoreLoc = MemoryLocation::get(SI);
675   MemoryAccess *Clobber =
676       Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
677   MemoryAccess *LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
678 
679   EXPECT_EQ(Clobber, StoreAccess);
680   EXPECT_TRUE(MSSA.isLiveOnEntryDef(LiveOnEntry));
681 
682   // Try again (with entries in the cache already) for good measure...
683   Clobber = Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
684   LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
685   EXPECT_EQ(Clobber, StoreAccess);
686   EXPECT_TRUE(MSSA.isLiveOnEntryDef(LiveOnEntry));
687 }
688 
689 // Bug: During phi optimization, the walker wouldn't cache to the proper result
690 // in the farthest-walked BB.
691 //
692 // Specifically, it would assume that whatever we walked to was a clobber.
693 // "Whatever we walked to" isn't a clobber if we hit a cache entry.
694 //
695 // ...So, we need a test case that looks like:
696 //    A
697 //   / \
698 //  B   |
699 //   \ /
700 //    C
701 //
702 // Where, when we try to optimize a thing in 'C', a blocker is found in 'B'.
703 // The walk must determine that the blocker exists by using cache entries *while
704 // walking* 'B'.
705 TEST_F(MemorySSATest, PartialWalkerCacheWithPhis) {
706   F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
707                        GlobalValue::ExternalLinkage, "F", &M);
708   B.SetInsertPoint(BasicBlock::Create(C, "A", F));
709   Type *Int8 = Type::getInt8Ty(C);
710   Constant *One = ConstantInt::get(Int8, 1);
711   Constant *Zero = ConstantInt::get(Int8, 0);
712   Value *AllocA = B.CreateAlloca(Int8, One, "a");
713   Value *AllocB = B.CreateAlloca(Int8, One, "b");
714   BasicBlock *IfThen = BasicBlock::Create(C, "B", F);
715   BasicBlock *IfEnd = BasicBlock::Create(C, "C", F);
716 
717   B.CreateCondBr(UndefValue::get(Type::getInt1Ty(C)), IfThen, IfEnd);
718 
719   B.SetInsertPoint(IfThen);
720   Instruction *FirstStore = B.CreateStore(Zero, AllocA);
721   B.CreateStore(Zero, AllocB);
722   Instruction *ALoad0 = B.CreateLoad(Int8, AllocA, "");
723   Instruction *BStore = B.CreateStore(Zero, AllocB);
724   // Due to use optimization/etc. we make a store to A, which is removed after
725   // we build MSSA. This helps keep the test case simple-ish.
726   Instruction *KillStore = B.CreateStore(Zero, AllocA);
727   Instruction *ALoad = B.CreateLoad(Int8, AllocA, "");
728   B.CreateBr(IfEnd);
729 
730   B.SetInsertPoint(IfEnd);
731   Instruction *BelowPhi = B.CreateStore(Zero, AllocA);
732 
733   setupAnalyses();
734   MemorySSA &MSSA = *Analyses->MSSA;
735   MemorySSAWalker *Walker = Analyses->Walker;
736   MemorySSAUpdater Updater(&MSSA);
737 
738   // Kill `KillStore`; it exists solely so that the load after it won't be
739   // optimized to FirstStore.
740   Updater.removeMemoryAccess(MSSA.getMemoryAccess(KillStore));
741   KillStore->eraseFromParent();
742   auto *ALoadMA = cast<MemoryUse>(MSSA.getMemoryAccess(ALoad));
743   EXPECT_EQ(ALoadMA->getDefiningAccess(), MSSA.getMemoryAccess(BStore));
744 
745   // Populate the cache for the store to AllocB directly after FirstStore. It
746   // should point to something in block B (so something in D can't be optimized
747   // to it).
748   MemoryAccess *Load0Clobber = Walker->getClobberingMemoryAccess(ALoad0);
749   EXPECT_EQ(MSSA.getMemoryAccess(FirstStore), Load0Clobber);
750 
751   // If the bug exists, this will introduce a bad cache entry for %a on BStore.
752   // It will point to the store to %b after FirstStore. This only happens during
753   // phi optimization.
754   MemoryAccess *BottomClobber = Walker->getClobberingMemoryAccess(BelowPhi);
755   MemoryAccess *Phi = MSSA.getMemoryAccess(IfEnd);
756   EXPECT_EQ(BottomClobber, Phi);
757 
758   // This query will first check the cache for {%a, BStore}. It should point to
759   // FirstStore, not to the store after FirstStore.
760   MemoryAccess *UseClobber = Walker->getClobberingMemoryAccess(ALoad);
761   EXPECT_EQ(UseClobber, MSSA.getMemoryAccess(FirstStore));
762 }
763 
764 // Test that our walker properly handles loads with the invariant group
765 // attribute. It's a bit hacky, since we add the invariant attribute *after*
766 // building MSSA. Otherwise, the use optimizer will optimize it for us, which
767 // isn't what we want.
768 // FIXME: It may be easier/cleaner to just add an 'optimize uses?' flag to MSSA.
769 TEST_F(MemorySSATest, WalkerInvariantLoadOpt) {
770   F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
771                        GlobalValue::ExternalLinkage, "F", &M);
772   B.SetInsertPoint(BasicBlock::Create(C, "", F));
773   Type *Int8 = Type::getInt8Ty(C);
774   Constant *One = ConstantInt::get(Int8, 1);
775   Value *AllocA = B.CreateAlloca(Int8, One, "");
776 
777   Instruction *Store = B.CreateStore(One, AllocA);
778   Instruction *Load = B.CreateLoad(Int8, AllocA);
779 
780   setupAnalyses();
781   MemorySSA &MSSA = *Analyses->MSSA;
782   MemorySSAWalker *Walker = Analyses->Walker;
783 
784   auto *LoadMA = cast<MemoryUse>(MSSA.getMemoryAccess(Load));
785   auto *StoreMA = cast<MemoryDef>(MSSA.getMemoryAccess(Store));
786   EXPECT_EQ(LoadMA->getDefiningAccess(), StoreMA);
787 
788   // ...At the time of writing, no cache should exist for LoadMA. Be a bit
789   // flexible to future changes.
790   Walker->invalidateInfo(LoadMA);
791   Load->setMetadata(LLVMContext::MD_invariant_load, MDNode::get(C, {}));
792 
793   MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LoadMA);
794   EXPECT_EQ(LoadClobber, MSSA.getLiveOnEntryDef());
795 }
796 
797 // Test loads get reoptimized properly by the walker.
798 TEST_F(MemorySSATest, WalkerReopt) {
799   F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
800                        GlobalValue::ExternalLinkage, "F", &M);
801   B.SetInsertPoint(BasicBlock::Create(C, "", F));
802   Type *Int8 = Type::getInt8Ty(C);
803   Value *AllocaA = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
804   Instruction *SIA = B.CreateStore(ConstantInt::get(Int8, 0), AllocaA);
805   Value *AllocaB = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
806   Instruction *SIB = B.CreateStore(ConstantInt::get(Int8, 0), AllocaB);
807   Instruction *LIA = B.CreateLoad(Int8, AllocaA);
808 
809   setupAnalyses();
810   MemorySSA &MSSA = *Analyses->MSSA;
811   MemorySSAWalker *Walker = Analyses->Walker;
812   MemorySSAUpdater Updater(&MSSA);
813 
814   MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LIA);
815   MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LIA));
816   EXPECT_EQ(LoadClobber, MSSA.getMemoryAccess(SIA));
817   EXPECT_TRUE(MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(SIA)));
818   Updater.removeMemoryAccess(LoadAccess);
819 
820   // Create the load memory access pointing to an unoptimized place.
821   MemoryUse *NewLoadAccess = cast<MemoryUse>(Updater.createMemoryAccessInBB(
822       LIA, MSSA.getMemoryAccess(SIB), LIA->getParent(), MemorySSA::End));
823   // This should it cause it to be optimized
824   EXPECT_EQ(Walker->getClobberingMemoryAccess(NewLoadAccess), LoadClobber);
825   EXPECT_EQ(NewLoadAccess->getDefiningAccess(), LoadClobber);
826 }
827 
828 // Test out MemorySSAUpdater::moveBefore
829 TEST_F(MemorySSATest, MoveAboveMemoryDef) {
830   F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
831                        GlobalValue::ExternalLinkage, "F", &M);
832   B.SetInsertPoint(BasicBlock::Create(C, "", F));
833 
834   Type *Int8 = Type::getInt8Ty(C);
835   Value *A = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
836   Value *B_ = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
837   Value *C = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "C");
838 
839   StoreInst *StoreA0 = B.CreateStore(ConstantInt::get(Int8, 0), A);
840   StoreInst *StoreB = B.CreateStore(ConstantInt::get(Int8, 0), B_);
841   LoadInst *LoadB = B.CreateLoad(Int8, B_);
842   StoreInst *StoreA1 = B.CreateStore(ConstantInt::get(Int8, 4), A);
843   StoreInst *StoreC = B.CreateStore(ConstantInt::get(Int8, 4), C);
844   StoreInst *StoreA2 = B.CreateStore(ConstantInt::get(Int8, 4), A);
845   LoadInst *LoadC = B.CreateLoad(Int8, C);
846 
847   setupAnalyses();
848   MemorySSA &MSSA = *Analyses->MSSA;
849   MemorySSAWalker &Walker = *Analyses->Walker;
850 
851   MemorySSAUpdater Updater(&MSSA);
852   StoreC->moveBefore(StoreB);
853   Updater.moveBefore(cast<MemoryDef>(MSSA.getMemoryAccess(StoreC)),
854                      cast<MemoryDef>(MSSA.getMemoryAccess(StoreB)));
855 
856   MSSA.verifyMemorySSA();
857 
858   EXPECT_EQ(MSSA.getMemoryAccess(StoreB)->getDefiningAccess(),
859             MSSA.getMemoryAccess(StoreC));
860   EXPECT_EQ(MSSA.getMemoryAccess(StoreC)->getDefiningAccess(),
861             MSSA.getMemoryAccess(StoreA0));
862   EXPECT_EQ(MSSA.getMemoryAccess(StoreA2)->getDefiningAccess(),
863             MSSA.getMemoryAccess(StoreA1));
864   EXPECT_EQ(Walker.getClobberingMemoryAccess(LoadB),
865             MSSA.getMemoryAccess(StoreB));
866   EXPECT_EQ(Walker.getClobberingMemoryAccess(LoadC),
867             MSSA.getMemoryAccess(StoreC));
868 
869   // exercise block numbering
870   EXPECT_TRUE(MSSA.locallyDominates(MSSA.getMemoryAccess(StoreC),
871                                     MSSA.getMemoryAccess(StoreB)));
872   EXPECT_TRUE(MSSA.locallyDominates(MSSA.getMemoryAccess(StoreA1),
873                                     MSSA.getMemoryAccess(StoreA2)));
874 }
875 
876 TEST_F(MemorySSATest, Irreducible) {
877   // Create the equivalent of
878   // x = something
879   // if (...)
880   //    goto second_loop_entry
881   // while (...) {
882   // second_loop_entry:
883   // }
884   // use(x)
885 
886   SmallVector<PHINode *, 8> Inserted;
887   IRBuilder<> B(C);
888   F = Function::Create(
889       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
890       GlobalValue::ExternalLinkage, "F", &M);
891 
892   // Make blocks
893   BasicBlock *IfBB = BasicBlock::Create(C, "if", F);
894   BasicBlock *LoopStartBB = BasicBlock::Create(C, "loopstart", F);
895   BasicBlock *LoopMainBB = BasicBlock::Create(C, "loopmain", F);
896   BasicBlock *AfterLoopBB = BasicBlock::Create(C, "afterloop", F);
897   B.SetInsertPoint(IfBB);
898   B.CreateCondBr(B.getTrue(), LoopMainBB, LoopStartBB);
899   B.SetInsertPoint(LoopStartBB);
900   B.CreateBr(LoopMainBB);
901   B.SetInsertPoint(LoopMainBB);
902   B.CreateCondBr(B.getTrue(), LoopStartBB, AfterLoopBB);
903   B.SetInsertPoint(AfterLoopBB);
904   Argument *FirstArg = &*F->arg_begin();
905   setupAnalyses();
906   MemorySSA &MSSA = *Analyses->MSSA;
907   MemorySSAUpdater Updater(&MSSA);
908   // Create the load memory acccess
909   LoadInst *LoadInst = B.CreateLoad(B.getInt8Ty(), FirstArg);
910   MemoryUse *LoadAccess = cast<MemoryUse>(Updater.createMemoryAccessInBB(
911       LoadInst, nullptr, AfterLoopBB, MemorySSA::Beginning));
912   Updater.insertUse(LoadAccess);
913   MSSA.verifyMemorySSA();
914 }
915 
916 TEST_F(MemorySSATest, MoveToBeforeLiveOnEntryInvalidatesCache) {
917   // Create:
918   //   %1 = alloca i8
919   //   ; 1 = MemoryDef(liveOnEntry)
920   //   store i8 0, i8* %1
921   //   ; 2 = MemoryDef(1)
922   //   store i8 0, i8* %1
923   //
924   // ...And be sure that MSSA's caching doesn't give us `1` for the clobber of
925   // `2` after `1` is removed.
926   IRBuilder<> B(C);
927   F = Function::Create(
928       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
929       GlobalValue::ExternalLinkage, "F", &M);
930 
931   BasicBlock *Entry = BasicBlock::Create(C, "if", F);
932   B.SetInsertPoint(Entry);
933 
934   Value *A = B.CreateAlloca(B.getInt8Ty());
935   StoreInst *StoreA = B.CreateStore(B.getInt8(0), A);
936   StoreInst *StoreB = B.CreateStore(B.getInt8(0), A);
937 
938   setupAnalyses();
939 
940   MemorySSA &MSSA = *Analyses->MSSA;
941 
942   auto *DefA = cast<MemoryDef>(MSSA.getMemoryAccess(StoreA));
943   auto *DefB = cast<MemoryDef>(MSSA.getMemoryAccess(StoreB));
944 
945   MemoryAccess *BClobber = MSSA.getWalker()->getClobberingMemoryAccess(DefB);
946   ASSERT_EQ(DefA, BClobber);
947 
948   MemorySSAUpdater(&MSSA).removeMemoryAccess(DefA);
949   StoreA->eraseFromParent();
950 
951   EXPECT_EQ(DefB->getDefiningAccess(), MSSA.getLiveOnEntryDef());
952 
953   EXPECT_EQ(MSSA.getWalker()->getClobberingMemoryAccess(DefB),
954             MSSA.getLiveOnEntryDef())
955       << "(DefA = " << DefA << ")";
956 }
957 
958 TEST_F(MemorySSATest, RemovingDefInvalidatesCache) {
959   // Create:
960   //   %x = alloca i8
961   //   %y = alloca i8
962   //   ; 1 = MemoryDef(liveOnEntry)
963   //   store i8 0, i8* %x
964   //   ; 2 = MemoryDef(1)
965   //   store i8 0, i8* %y
966   //   ; 3 = MemoryDef(2)
967   //   store i8 0, i8* %x
968   //
969   // And be sure that MSSA's caching handles the removal of def `1`
970   // appropriately.
971   IRBuilder<> B(C);
972   F = Function::Create(
973       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
974       GlobalValue::ExternalLinkage, "F", &M);
975 
976   BasicBlock *Entry = BasicBlock::Create(C, "if", F);
977   B.SetInsertPoint(Entry);
978 
979   Value *X = B.CreateAlloca(B.getInt8Ty());
980   Value *Y = B.CreateAlloca(B.getInt8Ty());
981   StoreInst *StoreX1 = B.CreateStore(B.getInt8(0), X);
982   StoreInst *StoreY = B.CreateStore(B.getInt8(0), Y);
983   StoreInst *StoreX2 = B.CreateStore(B.getInt8(0), X);
984 
985   setupAnalyses();
986 
987   MemorySSA &MSSA = *Analyses->MSSA;
988 
989   auto *DefX1 = cast<MemoryDef>(MSSA.getMemoryAccess(StoreX1));
990   auto *DefY = cast<MemoryDef>(MSSA.getMemoryAccess(StoreY));
991   auto *DefX2 = cast<MemoryDef>(MSSA.getMemoryAccess(StoreX2));
992 
993   EXPECT_EQ(DefX2->getDefiningAccess(), DefY);
994   MemoryAccess *X2Clobber = MSSA.getWalker()->getClobberingMemoryAccess(DefX2);
995   ASSERT_EQ(DefX1, X2Clobber);
996 
997   MemorySSAUpdater(&MSSA).removeMemoryAccess(DefX1);
998   StoreX1->eraseFromParent();
999 
1000   EXPECT_EQ(DefX2->getDefiningAccess(), DefY);
1001   EXPECT_EQ(MSSA.getWalker()->getClobberingMemoryAccess(DefX2),
1002             MSSA.getLiveOnEntryDef())
1003       << "(DefX1 = " << DefX1 << ")";
1004 }
1005 
1006 // Test Must alias for optimized uses
1007 TEST_F(MemorySSATest, TestLoadMustAlias) {
1008   F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
1009                        GlobalValue::ExternalLinkage, "F", &M);
1010   B.SetInsertPoint(BasicBlock::Create(C, "", F));
1011   Type *Int8 = Type::getInt8Ty(C);
1012   Value *AllocaA = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
1013   Value *AllocaB = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
1014 
1015   B.CreateStore(ConstantInt::get(Int8, 1), AllocaB);
1016   // Check load from LOE
1017   LoadInst *LA1 = B.CreateLoad(Int8, AllocaA, "");
1018   // Check load alias cached for second load
1019   LoadInst *LA2 = B.CreateLoad(Int8, AllocaA, "");
1020 
1021   B.CreateStore(ConstantInt::get(Int8, 1), AllocaA);
1022   // Check load from store/def
1023   LoadInst *LA3 = B.CreateLoad(Int8, AllocaA, "");
1024   // Check load alias cached for second load
1025   LoadInst *LA4 = B.CreateLoad(Int8, AllocaA, "");
1026 
1027   setupAnalyses();
1028   MemorySSA &MSSA = *Analyses->MSSA;
1029   MSSA.ensureOptimizedUses();
1030 
1031   unsigned I = 0;
1032   for (LoadInst *V : {LA1, LA2}) {
1033     MemoryUse *MemUse = dyn_cast_or_null<MemoryUse>(MSSA.getMemoryAccess(V));
1034     EXPECT_EQ(MemUse->getOptimizedAccessType(), None)
1035         << "Load " << I << " doesn't have the correct alias information";
1036     // EXPECT_EQ expands such that if we increment I above, it won't get
1037     // incremented except when we try to print the error message.
1038     ++I;
1039   }
1040   for (LoadInst *V : {LA3, LA4}) {
1041     MemoryUse *MemUse = dyn_cast_or_null<MemoryUse>(MSSA.getMemoryAccess(V));
1042     EXPECT_EQ(MemUse->getOptimizedAccessType().getValue(),
1043               AliasResult::MustAlias)
1044         << "Load " << I << " doesn't have the correct alias information";
1045     // EXPECT_EQ expands such that if we increment I above, it won't get
1046     // incremented except when we try to print the error message.
1047     ++I;
1048   }
1049 }
1050 
1051 // Test Must alias for optimized defs.
1052 TEST_F(MemorySSATest, TestStoreMustAlias) {
1053   F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
1054                        GlobalValue::ExternalLinkage, "F", &M);
1055   B.SetInsertPoint(BasicBlock::Create(C, "", F));
1056   Type *Int8 = Type::getInt8Ty(C);
1057   Value *AllocaA = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
1058   Value *AllocaB = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
1059   StoreInst *SA1 = B.CreateStore(ConstantInt::get(Int8, 1), AllocaA);
1060   StoreInst *SB1 = B.CreateStore(ConstantInt::get(Int8, 1), AllocaB);
1061   StoreInst *SA2 = B.CreateStore(ConstantInt::get(Int8, 2), AllocaA);
1062   StoreInst *SB2 = B.CreateStore(ConstantInt::get(Int8, 2), AllocaB);
1063   StoreInst *SA3 = B.CreateStore(ConstantInt::get(Int8, 3), AllocaA);
1064   StoreInst *SB3 = B.CreateStore(ConstantInt::get(Int8, 3), AllocaB);
1065 
1066   setupAnalyses();
1067   MemorySSA &MSSA = *Analyses->MSSA;
1068   MemorySSAWalker *Walker = Analyses->Walker;
1069 
1070   unsigned I = 0;
1071   for (StoreInst *V : {SA1, SB1, SA2, SB2, SA3, SB3}) {
1072     MemoryDef *MemDef = dyn_cast_or_null<MemoryDef>(MSSA.getMemoryAccess(V));
1073     EXPECT_EQ(MemDef->isOptimized(), false)
1074         << "Store " << I << " is optimized from the start?";
1075     EXPECT_EQ(MemDef->getOptimizedAccessType(), None)
1076         << "Store " << I
1077         << " has correct alias information before being optimized?";
1078     if (V == SA1)
1079       Walker->getClobberingMemoryAccess(V);
1080     else {
1081       MemoryAccess *Def = MemDef->getDefiningAccess();
1082       MemoryAccess *Clob = Walker->getClobberingMemoryAccess(V);
1083       EXPECT_NE(Def, Clob) << "Store " << I
1084                            << " has Defining Access equal to Clobbering Access";
1085     }
1086     EXPECT_EQ(MemDef->isOptimized(), true)
1087         << "Store " << I << " was not optimized";
1088     if (I == 0 || I == 1)
1089       EXPECT_EQ(MemDef->getOptimizedAccessType(), None)
1090           << "Store " << I << " doesn't have the correct alias information";
1091     else
1092       EXPECT_EQ(MemDef->getOptimizedAccessType().getValue(),
1093                 AliasResult::MustAlias)
1094           << "Store " << I << " doesn't have the correct alias information";
1095     // EXPECT_EQ expands such that if we increment I above, it won't get
1096     // incremented except when we try to print the error message.
1097     ++I;
1098   }
1099 }
1100 
1101 // Test May alias for optimized uses.
1102 TEST_F(MemorySSATest, TestLoadMayAlias) {
1103   F = Function::Create(FunctionType::get(B.getVoidTy(),
1104                                          {B.getInt8PtrTy(), B.getInt8PtrTy()},
1105                                          false),
1106                        GlobalValue::ExternalLinkage, "F", &M);
1107   B.SetInsertPoint(BasicBlock::Create(C, "", F));
1108   Type *Int8 = Type::getInt8Ty(C);
1109   auto *ArgIt = F->arg_begin();
1110   Argument *PointerA = &*ArgIt;
1111   Argument *PointerB = &*(++ArgIt);
1112   B.CreateStore(ConstantInt::get(Int8, 1), PointerB);
1113   LoadInst *LA1 = B.CreateLoad(Int8, PointerA, "");
1114   B.CreateStore(ConstantInt::get(Int8, 0), PointerA);
1115   LoadInst *LB1 = B.CreateLoad(Int8, PointerB, "");
1116   B.CreateStore(ConstantInt::get(Int8, 0), PointerA);
1117   LoadInst *LA2 = B.CreateLoad(Int8, PointerA, "");
1118   B.CreateStore(ConstantInt::get(Int8, 0), PointerB);
1119   LoadInst *LB2 = B.CreateLoad(Int8, PointerB, "");
1120 
1121   setupAnalyses();
1122   MemorySSA &MSSA = *Analyses->MSSA;
1123   MSSA.ensureOptimizedUses();
1124 
1125   unsigned I = 0;
1126   for (LoadInst *V : {LA1, LB1}) {
1127     MemoryUse *MemUse = dyn_cast_or_null<MemoryUse>(MSSA.getMemoryAccess(V));
1128     EXPECT_EQ(MemUse->getOptimizedAccessType().getValue(),
1129               AliasResult::MayAlias)
1130         << "Load " << I << " doesn't have the correct alias information";
1131     // EXPECT_EQ expands such that if we increment I above, it won't get
1132     // incremented except when we try to print the error message.
1133     ++I;
1134   }
1135   for (LoadInst *V : {LA2, LB2}) {
1136     MemoryUse *MemUse = dyn_cast_or_null<MemoryUse>(MSSA.getMemoryAccess(V));
1137     EXPECT_EQ(MemUse->getOptimizedAccessType().getValue(),
1138               AliasResult::MustAlias)
1139         << "Load " << I << " doesn't have the correct alias information";
1140     // EXPECT_EQ expands such that if we increment I above, it won't get
1141     // incremented except when we try to print the error message.
1142     ++I;
1143   }
1144 }
1145 
1146 // Test May alias for optimized defs.
1147 TEST_F(MemorySSATest, TestStoreMayAlias) {
1148   F = Function::Create(FunctionType::get(B.getVoidTy(),
1149                                          {B.getInt8PtrTy(), B.getInt8PtrTy()},
1150                                          false),
1151                        GlobalValue::ExternalLinkage, "F", &M);
1152   B.SetInsertPoint(BasicBlock::Create(C, "", F));
1153   Type *Int8 = Type::getInt8Ty(C);
1154   auto *ArgIt = F->arg_begin();
1155   Argument *PointerA = &*ArgIt;
1156   Argument *PointerB = &*(++ArgIt);
1157   Value *AllocaC = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "C");
1158   // Store into arg1, must alias because it's LOE => must
1159   StoreInst *SA1 = B.CreateStore(ConstantInt::get(Int8, 0), PointerA);
1160   // Store into arg2, may alias store to arg1 => may
1161   StoreInst *SB1 = B.CreateStore(ConstantInt::get(Int8, 1), PointerB);
1162   // Store into aloca, no alias with args, so must alias LOE => must
1163   StoreInst *SC1 = B.CreateStore(ConstantInt::get(Int8, 2), AllocaC);
1164   // Store into arg1, may alias store to arg2 => may
1165   StoreInst *SA2 = B.CreateStore(ConstantInt::get(Int8, 3), PointerA);
1166   // Store into arg2, may alias store to arg1 => may
1167   StoreInst *SB2 = B.CreateStore(ConstantInt::get(Int8, 4), PointerB);
1168   // Store into aloca, no alias with args, so must alias SC1 => must
1169   StoreInst *SC2 = B.CreateStore(ConstantInt::get(Int8, 5), AllocaC);
1170   // Store into arg2, must alias store to arg2 => must
1171   StoreInst *SB3 = B.CreateStore(ConstantInt::get(Int8, 6), PointerB);
1172   std::initializer_list<StoreInst *> Sts = {SA1, SB1, SC1, SA2, SB2, SC2, SB3};
1173 
1174   setupAnalyses();
1175   MemorySSA &MSSA = *Analyses->MSSA;
1176   MemorySSAWalker *Walker = Analyses->Walker;
1177 
1178   unsigned I = 0;
1179   for (StoreInst *V : Sts) {
1180     MemoryDef *MemDef = dyn_cast_or_null<MemoryDef>(MSSA.getMemoryAccess(V));
1181     EXPECT_EQ(MemDef->isOptimized(), false)
1182         << "Store " << I << " is optimized from the start?";
1183     EXPECT_EQ(MemDef->getOptimizedAccessType(), None)
1184         << "Store " << I
1185         << " has correct alias information before being optimized?";
1186     ++I;
1187   }
1188 
1189   for (StoreInst *V : Sts)
1190     Walker->getClobberingMemoryAccess(V);
1191 
1192   I = 0;
1193   for (StoreInst *V : Sts) {
1194     MemoryDef *MemDef = dyn_cast_or_null<MemoryDef>(MSSA.getMemoryAccess(V));
1195     EXPECT_EQ(MemDef->isOptimized(), true)
1196         << "Store " << I << " was not optimized";
1197     if (I == 1 || I == 3 || I == 4)
1198       EXPECT_EQ(MemDef->getOptimizedAccessType().getValue(),
1199                 AliasResult::MayAlias)
1200           << "Store " << I << " doesn't have the correct alias information";
1201     else if (I == 0 || I == 2)
1202       EXPECT_EQ(MemDef->getOptimizedAccessType(), None)
1203           << "Store " << I << " doesn't have the correct alias information";
1204     else
1205       EXPECT_EQ(MemDef->getOptimizedAccessType().getValue(),
1206                 AliasResult::MustAlias)
1207           << "Store " << I << " doesn't have the correct alias information";
1208     // EXPECT_EQ expands such that if we increment I above, it won't get
1209     // incremented except when we try to print the error message.
1210     ++I;
1211   }
1212 }
1213 
1214 TEST_F(MemorySSATest, LifetimeMarkersAreClobbers) {
1215   // Example code:
1216   // define void @a(i8* %foo) {
1217   //   %bar = getelementptr i8, i8* %foo, i64 1
1218   //   %baz = getelementptr i8, i8* %foo, i64 2
1219   //   store i8 0, i8* %foo
1220   //   store i8 0, i8* %bar
1221   //   call void @llvm.lifetime.end.p0i8(i64 3, i8* %foo)
1222   //   call void @llvm.lifetime.start.p0i8(i64 3, i8* %foo)
1223   //   store i8 0, i8* %foo
1224   //   store i8 0, i8* %bar
1225   //   call void @llvm.memset.p0i8(i8* %baz, i8 0, i64 1)
1226   //   ret void
1227   // }
1228   //
1229   // Patterns like this are possible after inlining; the stores to %foo and %bar
1230   // should both be clobbered by the lifetime.start call if they're dominated by
1231   // it.
1232 
1233   IRBuilder<> B(C);
1234   F = Function::Create(
1235       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
1236       GlobalValue::ExternalLinkage, "F", &M);
1237 
1238   // Make blocks
1239   BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
1240 
1241   B.SetInsertPoint(Entry);
1242   Value *Foo = &*F->arg_begin();
1243 
1244   Value *Bar = B.CreateGEP(B.getInt8Ty(), Foo, B.getInt64(1), "bar");
1245   Value *Baz = B.CreateGEP(B.getInt8Ty(), Foo, B.getInt64(2), "baz");
1246 
1247   B.CreateStore(B.getInt8(0), Foo);
1248   B.CreateStore(B.getInt8(0), Bar);
1249 
1250   auto GetLifetimeIntrinsic = [&](Intrinsic::ID ID) {
1251     return Intrinsic::getDeclaration(&M, ID, {Foo->getType()});
1252   };
1253 
1254   B.CreateCall(GetLifetimeIntrinsic(Intrinsic::lifetime_end),
1255                {B.getInt64(3), Foo});
1256   Instruction *LifetimeStart = B.CreateCall(
1257       GetLifetimeIntrinsic(Intrinsic::lifetime_start), {B.getInt64(3), Foo});
1258 
1259   Instruction *FooStore = B.CreateStore(B.getInt8(0), Foo);
1260   Instruction *BarStore = B.CreateStore(B.getInt8(0), Bar);
1261   Instruction *BazMemSet = B.CreateMemSet(Baz, B.getInt8(0), 1, Align(1));
1262 
1263   setupAnalyses();
1264   MemorySSA &MSSA = *Analyses->MSSA;
1265 
1266   MemoryAccess *LifetimeStartAccess = MSSA.getMemoryAccess(LifetimeStart);
1267   ASSERT_NE(LifetimeStartAccess, nullptr);
1268 
1269   MemoryAccess *FooAccess = MSSA.getMemoryAccess(FooStore);
1270   ASSERT_NE(FooAccess, nullptr);
1271 
1272   MemoryAccess *BarAccess = MSSA.getMemoryAccess(BarStore);
1273   ASSERT_NE(BarAccess, nullptr);
1274 
1275   MemoryAccess *BazAccess = MSSA.getMemoryAccess(BazMemSet);
1276   ASSERT_NE(BazAccess, nullptr);
1277 
1278   MemoryAccess *FooClobber =
1279       MSSA.getWalker()->getClobberingMemoryAccess(FooAccess);
1280   EXPECT_EQ(FooClobber, LifetimeStartAccess);
1281 
1282   MemoryAccess *BarClobber =
1283       MSSA.getWalker()->getClobberingMemoryAccess(BarAccess);
1284   EXPECT_EQ(BarClobber, LifetimeStartAccess);
1285 
1286   MemoryAccess *BazClobber =
1287       MSSA.getWalker()->getClobberingMemoryAccess(BazAccess);
1288   EXPECT_EQ(BazClobber, LifetimeStartAccess);
1289 
1290   MemoryAccess *LifetimeStartClobber =
1291       MSSA.getWalker()->getClobberingMemoryAccess(
1292           LifetimeStartAccess, MemoryLocation::getAfter(Foo));
1293   EXPECT_EQ(LifetimeStartClobber, LifetimeStartAccess);
1294 }
1295 
1296 TEST_F(MemorySSATest, DefOptimizationsAreInvalidatedOnMoving) {
1297   IRBuilder<> B(C);
1298   F = Function::Create(FunctionType::get(B.getVoidTy(), {B.getInt1Ty()}, false),
1299                        GlobalValue::ExternalLinkage, "F", &M);
1300 
1301   // Make a CFG like
1302   //     entry
1303   //      / \
1304   //     a   b
1305   //      \ /
1306   //       c
1307   //
1308   // Put a def in A and a def in B, move the def from A -> B, observe as the
1309   // optimization is invalidated.
1310   BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
1311   BasicBlock *BlockA = BasicBlock::Create(C, "a", F);
1312   BasicBlock *BlockB = BasicBlock::Create(C, "b", F);
1313   BasicBlock *BlockC = BasicBlock::Create(C, "c", F);
1314 
1315   B.SetInsertPoint(Entry);
1316   Type *Int8 = Type::getInt8Ty(C);
1317   Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "alloc");
1318   StoreInst *StoreEntry = B.CreateStore(B.getInt8(0), Alloca);
1319   B.CreateCondBr(B.getTrue(), BlockA, BlockB);
1320 
1321   B.SetInsertPoint(BlockA);
1322   StoreInst *StoreA = B.CreateStore(B.getInt8(1), Alloca);
1323   B.CreateBr(BlockC);
1324 
1325   B.SetInsertPoint(BlockB);
1326   StoreInst *StoreB = B.CreateStore(B.getInt8(2), Alloca);
1327   B.CreateBr(BlockC);
1328 
1329   B.SetInsertPoint(BlockC);
1330   B.CreateUnreachable();
1331 
1332   setupAnalyses();
1333   MemorySSA &MSSA = *Analyses->MSSA;
1334 
1335   auto *AccessEntry = cast<MemoryDef>(MSSA.getMemoryAccess(StoreEntry));
1336   auto *StoreAEntry = cast<MemoryDef>(MSSA.getMemoryAccess(StoreA));
1337   auto *StoreBEntry = cast<MemoryDef>(MSSA.getMemoryAccess(StoreB));
1338 
1339   ASSERT_EQ(MSSA.getWalker()->getClobberingMemoryAccess(StoreAEntry),
1340             AccessEntry);
1341   ASSERT_TRUE(StoreAEntry->isOptimized());
1342 
1343   ASSERT_EQ(MSSA.getWalker()->getClobberingMemoryAccess(StoreBEntry),
1344             AccessEntry);
1345   ASSERT_TRUE(StoreBEntry->isOptimized());
1346 
1347   // Note that if we did InsertionPlace::Beginning, we don't go out of our way
1348   // to invalidate the cache for StoreBEntry. If the user wants to actually do
1349   // moves like these, it's up to them to ensure that nearby cache entries are
1350   // correctly invalidated (which, in general, requires walking all instructions
1351   // that the moved instruction dominates. So we probably shouldn't be doing
1352   // moves like this in general. Still, works as a test-case. ;) )
1353   MemorySSAUpdater(&MSSA).moveToPlace(StoreAEntry, BlockB,
1354                                       MemorySSA::InsertionPlace::End);
1355   ASSERT_FALSE(StoreAEntry->isOptimized());
1356   ASSERT_EQ(MSSA.getWalker()->getClobberingMemoryAccess(StoreAEntry),
1357             StoreBEntry);
1358 }
1359 
1360 TEST_F(MemorySSATest, TestOptimizedDefsAreProperUses) {
1361   F = Function::Create(FunctionType::get(B.getVoidTy(),
1362                                          {B.getInt8PtrTy(), B.getInt8PtrTy()},
1363                                          false),
1364                        GlobalValue::ExternalLinkage, "F", &M);
1365   B.SetInsertPoint(BasicBlock::Create(C, "", F));
1366   Type *Int8 = Type::getInt8Ty(C);
1367   Value *AllocA = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
1368   Value *AllocB = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
1369 
1370   StoreInst *StoreA = B.CreateStore(ConstantInt::get(Int8, 0), AllocA);
1371   StoreInst *StoreB = B.CreateStore(ConstantInt::get(Int8, 1), AllocB);
1372   StoreInst *StoreA2 = B.CreateStore(ConstantInt::get(Int8, 2), AllocA);
1373 
1374   setupAnalyses();
1375   MemorySSA &MSSA = *Analyses->MSSA;
1376   MemorySSAWalker *Walker = Analyses->Walker;
1377 
1378   // If these don't hold, there's no chance of the test result being useful.
1379   ASSERT_EQ(Walker->getClobberingMemoryAccess(StoreA),
1380             MSSA.getLiveOnEntryDef());
1381   ASSERT_EQ(Walker->getClobberingMemoryAccess(StoreB),
1382             MSSA.getLiveOnEntryDef());
1383   auto *StoreAAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreA));
1384   auto *StoreA2Access = cast<MemoryDef>(MSSA.getMemoryAccess(StoreA2));
1385   ASSERT_EQ(Walker->getClobberingMemoryAccess(StoreA2), StoreAAccess);
1386   ASSERT_EQ(StoreA2Access->getOptimized(), StoreAAccess);
1387 
1388   auto *StoreBAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreB));
1389   ASSERT_LT(StoreAAccess->getID(), StoreBAccess->getID());
1390   ASSERT_LT(StoreBAccess->getID(), StoreA2Access->getID());
1391 
1392   auto SortVecByID = [](std::vector<const MemoryDef *> &Defs) {
1393     llvm::sort(Defs, [](const MemoryDef *LHS, const MemoryDef *RHS) {
1394       return LHS->getID() < RHS->getID();
1395     });
1396   };
1397 
1398   auto SortedUserList = [&](const MemoryDef *MD) {
1399     std::vector<const MemoryDef *> Result;
1400     transform(MD->users(), std::back_inserter(Result),
1401               [](const User *U) { return cast<MemoryDef>(U); });
1402     SortVecByID(Result);
1403     return Result;
1404   };
1405 
1406   // Use std::vectors, since they have nice pretty-printing if the test fails.
1407   // Parens are necessary because EXPECT_EQ is a macro, and we have commas in
1408   // our init lists...
1409   EXPECT_EQ(SortedUserList(StoreAAccess),
1410             (std::vector<const MemoryDef *>{StoreBAccess, StoreA2Access}));
1411 
1412   EXPECT_EQ(SortedUserList(StoreBAccess),
1413             std::vector<const MemoryDef *>{StoreA2Access});
1414 
1415   // StoreAAccess should be present twice, since it uses liveOnEntry for both
1416   // its defining and optimized accesses. This is a bit awkward, and is not
1417   // relied upon anywhere at the moment. If this is painful, we can fix it.
1418   EXPECT_EQ(SortedUserList(cast<MemoryDef>(MSSA.getLiveOnEntryDef())),
1419             (std::vector<const MemoryDef *>{StoreAAccess, StoreAAccess,
1420                                             StoreBAccess}));
1421 }
1422 
1423 //   entry
1424 //     |
1425 //   header
1426 //    / \
1427 // body  |
1428 //    \ /
1429 //    exit
1430 // header:
1431 //  ; 1 = MemoryDef(liveOnEntry)
1432 // body:
1433 //  ; 2 = MemoryDef(1)
1434 // exit:
1435 //  ; 3 = MemoryPhi({body, 2}, {header, 1})
1436 //  ; 4 = MemoryDef(3); optimized to 3, cannot optimize thorugh phi.
1437 //  Insert edge: entry -> exit, check mssa Update is correct.
1438 TEST_F(MemorySSATest, TestAddedEdgeToBlockWithPhiNotOpt) {
1439   F = Function::Create(
1440       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
1441       GlobalValue::ExternalLinkage, "F", &M);
1442   Argument *PointerArg = &*F->arg_begin();
1443   BasicBlock *Entry(BasicBlock::Create(C, "entry", F));
1444   BasicBlock *Header(BasicBlock::Create(C, "header", F));
1445   BasicBlock *Body(BasicBlock::Create(C, "body", F));
1446   BasicBlock *Exit(BasicBlock::Create(C, "exit", F));
1447   B.SetInsertPoint(Entry);
1448   BranchInst::Create(Header, Entry);
1449   B.SetInsertPoint(Header);
1450   B.CreateStore(B.getInt8(16), PointerArg);
1451   B.CreateCondBr(B.getTrue(), Exit, Body);
1452   B.SetInsertPoint(Body);
1453   B.CreateStore(B.getInt8(16), PointerArg);
1454   BranchInst::Create(Exit, Body);
1455   B.SetInsertPoint(Exit);
1456   StoreInst *S1 = B.CreateStore(B.getInt8(16), PointerArg);
1457 
1458   setupAnalyses();
1459   MemorySSA &MSSA = *Analyses->MSSA;
1460   MemorySSAWalker *Walker = Analyses->Walker;
1461   std::unique_ptr<MemorySSAUpdater> MSSAU =
1462       std::make_unique<MemorySSAUpdater>(&MSSA);
1463 
1464   MemoryPhi *Phi = MSSA.getMemoryAccess(Exit);
1465   EXPECT_EQ(Phi, Walker->getClobberingMemoryAccess(S1));
1466 
1467   // Alter CFG, add edge: entry -> exit
1468   Entry->getTerminator()->eraseFromParent();
1469   B.SetInsertPoint(Entry);
1470   B.CreateCondBr(B.getTrue(), Header, Exit);
1471   SmallVector<CFGUpdate, 1> Updates;
1472   Updates.push_back({cfg::UpdateKind::Insert, Entry, Exit});
1473   Analyses->DT.applyUpdates(Updates);
1474   MSSAU->applyInsertUpdates(Updates, Analyses->DT);
1475   EXPECT_EQ(Phi, Walker->getClobberingMemoryAccess(S1));
1476 }
1477 
1478 //   entry
1479 //     |
1480 //   header
1481 //    / \
1482 // body  |
1483 //    \ /
1484 //    exit
1485 // header:
1486 //  ; 1 = MemoryDef(liveOnEntry)
1487 // body:
1488 //  ; 2 = MemoryDef(1)
1489 // exit:
1490 //  ; 3 = MemoryPhi({body, 2}, {header, 1})
1491 //  ; 4 = MemoryDef(3); optimize this to 1 now, added edge should invalidate
1492 //  the optimized access.
1493 //  Insert edge: entry -> exit, check mssa Update is correct.
1494 TEST_F(MemorySSATest, TestAddedEdgeToBlockWithPhiOpt) {
1495   F = Function::Create(
1496       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
1497       GlobalValue::ExternalLinkage, "F", &M);
1498   Argument *PointerArg = &*F->arg_begin();
1499   Type *Int8 = Type::getInt8Ty(C);
1500   BasicBlock *Entry(BasicBlock::Create(C, "entry", F));
1501   BasicBlock *Header(BasicBlock::Create(C, "header", F));
1502   BasicBlock *Body(BasicBlock::Create(C, "body", F));
1503   BasicBlock *Exit(BasicBlock::Create(C, "exit", F));
1504 
1505   B.SetInsertPoint(Entry);
1506   Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
1507   BranchInst::Create(Header, Entry);
1508 
1509   B.SetInsertPoint(Header);
1510   StoreInst *S1 = B.CreateStore(B.getInt8(16), PointerArg);
1511   B.CreateCondBr(B.getTrue(), Exit, Body);
1512 
1513   B.SetInsertPoint(Body);
1514   B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
1515   BranchInst::Create(Exit, Body);
1516 
1517   B.SetInsertPoint(Exit);
1518   StoreInst *S2 = B.CreateStore(B.getInt8(16), PointerArg);
1519 
1520   setupAnalyses();
1521   MemorySSA &MSSA = *Analyses->MSSA;
1522   MemorySSAWalker *Walker = Analyses->Walker;
1523   std::unique_ptr<MemorySSAUpdater> MSSAU =
1524       std::make_unique<MemorySSAUpdater>(&MSSA);
1525 
1526   MemoryDef *DefS1 = cast<MemoryDef>(MSSA.getMemoryAccess(S1));
1527   EXPECT_EQ(DefS1, Walker->getClobberingMemoryAccess(S2));
1528 
1529   // Alter CFG, add edge: entry -> exit
1530   Entry->getTerminator()->eraseFromParent();
1531   B.SetInsertPoint(Entry);
1532   B.CreateCondBr(B.getTrue(), Header, Exit);
1533   SmallVector<CFGUpdate, 1> Updates;
1534   Updates.push_back({cfg::UpdateKind::Insert, Entry, Exit});
1535   Analyses->DT.applyUpdates(Updates);
1536   MSSAU->applyInsertUpdates(Updates, Analyses->DT);
1537 
1538   MemoryPhi *Phi = MSSA.getMemoryAccess(Exit);
1539   EXPECT_EQ(Phi, Walker->getClobberingMemoryAccess(S2));
1540 }
1541 
1542 //   entry
1543 //    /  |
1544 //   a   |
1545 //  / \  |
1546 //  b c  f
1547 //  \ /  |
1548 //   d   |
1549 //    \ /
1550 //     e
1551 // f:
1552 //  ; 1 = MemoryDef(liveOnEntry)
1553 // e:
1554 //  ; 2 = MemoryPhi({d, liveOnEntry}, {f, 1})
1555 //
1556 // Insert edge: f -> c, check update is correct.
1557 // After update:
1558 // f:
1559 //  ; 1 = MemoryDef(liveOnEntry)
1560 // c:
1561 //  ; 3 = MemoryPhi({a, liveOnEntry}, {f, 1})
1562 // d:
1563 //  ; 4 = MemoryPhi({b, liveOnEntry}, {c, 3})
1564 // e:
1565 //  ; 2 = MemoryPhi({d, 4}, {f, 1})
1566 TEST_F(MemorySSATest, TestAddedEdgeToBlockWithNoPhiAddNewPhis) {
1567   F = Function::Create(
1568       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
1569       GlobalValue::ExternalLinkage, "F", &M);
1570   Argument *PointerArg = &*F->arg_begin();
1571   BasicBlock *Entry(BasicBlock::Create(C, "entry", F));
1572   BasicBlock *ABlock(BasicBlock::Create(C, "a", F));
1573   BasicBlock *BBlock(BasicBlock::Create(C, "b", F));
1574   BasicBlock *CBlock(BasicBlock::Create(C, "c", F));
1575   BasicBlock *DBlock(BasicBlock::Create(C, "d", F));
1576   BasicBlock *EBlock(BasicBlock::Create(C, "e", F));
1577   BasicBlock *FBlock(BasicBlock::Create(C, "f", F));
1578 
1579   B.SetInsertPoint(Entry);
1580   B.CreateCondBr(B.getTrue(), ABlock, FBlock);
1581   B.SetInsertPoint(ABlock);
1582   B.CreateCondBr(B.getTrue(), BBlock, CBlock);
1583   B.SetInsertPoint(BBlock);
1584   BranchInst::Create(DBlock, BBlock);
1585   B.SetInsertPoint(CBlock);
1586   BranchInst::Create(DBlock, CBlock);
1587   B.SetInsertPoint(DBlock);
1588   BranchInst::Create(EBlock, DBlock);
1589   B.SetInsertPoint(FBlock);
1590   B.CreateStore(B.getInt8(16), PointerArg);
1591   BranchInst::Create(EBlock, FBlock);
1592 
1593   setupAnalyses();
1594   MemorySSA &MSSA = *Analyses->MSSA;
1595   std::unique_ptr<MemorySSAUpdater> MSSAU =
1596       std::make_unique<MemorySSAUpdater>(&MSSA);
1597 
1598   // Alter CFG, add edge: f -> c
1599   FBlock->getTerminator()->eraseFromParent();
1600   B.SetInsertPoint(FBlock);
1601   B.CreateCondBr(B.getTrue(), CBlock, EBlock);
1602   SmallVector<CFGUpdate, 1> Updates;
1603   Updates.push_back({cfg::UpdateKind::Insert, FBlock, CBlock});
1604   Analyses->DT.applyUpdates(Updates);
1605   MSSAU->applyInsertUpdates(Updates, Analyses->DT);
1606 
1607   MemoryPhi *MPC = MSSA.getMemoryAccess(CBlock);
1608   EXPECT_NE(MPC, nullptr);
1609   MemoryPhi *MPD = MSSA.getMemoryAccess(DBlock);
1610   EXPECT_NE(MPD, nullptr);
1611   MemoryPhi *MPE = MSSA.getMemoryAccess(EBlock);
1612   EXPECT_EQ(MPD, MPE->getIncomingValueForBlock(DBlock));
1613 }
1614 
1615 TEST_F(MemorySSATest, TestCallClobber) {
1616   F = Function::Create(
1617       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
1618       GlobalValue::ExternalLinkage, "F", &M);
1619 
1620   Value *Pointer1 = &*F->arg_begin();
1621   BasicBlock *Entry(BasicBlock::Create(C, "", F));
1622   B.SetInsertPoint(Entry);
1623   Value *Pointer2 = B.CreateGEP(B.getInt8Ty(), Pointer1, B.getInt64(1));
1624   Instruction *StorePointer1 = B.CreateStore(B.getInt8(0), Pointer1);
1625   Instruction *StorePointer2 = B.CreateStore(B.getInt8(0), Pointer2);
1626   Instruction *MemSet = B.CreateMemSet(Pointer2, B.getInt8(0), 1, Align(1));
1627 
1628   setupAnalyses();
1629   MemorySSA &MSSA = *Analyses->MSSA;
1630   MemorySSAWalker *Walker = Analyses->Walker;
1631 
1632   MemoryUseOrDef *Store1Access = MSSA.getMemoryAccess(StorePointer1);
1633   MemoryUseOrDef *Store2Access = MSSA.getMemoryAccess(StorePointer2);
1634   MemoryUseOrDef *MemSetAccess = MSSA.getMemoryAccess(MemSet);
1635 
1636   MemoryAccess *Pointer1Clobber = Walker->getClobberingMemoryAccess(
1637       MemSetAccess, MemoryLocation(Pointer1, LocationSize::precise(1)));
1638   EXPECT_EQ(Pointer1Clobber, Store1Access);
1639 
1640   MemoryAccess *Pointer2Clobber = Walker->getClobberingMemoryAccess(
1641       MemSetAccess, MemoryLocation(Pointer2, LocationSize::precise(1)));
1642   EXPECT_EQ(Pointer2Clobber, MemSetAccess);
1643 
1644   MemoryAccess *MemSetClobber = Walker->getClobberingMemoryAccess(MemSetAccess);
1645   EXPECT_EQ(MemSetClobber, Store2Access);
1646 }
1647 
1648 TEST_F(MemorySSATest, TestLoadClobber) {
1649   F = Function::Create(
1650       FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
1651       GlobalValue::ExternalLinkage, "F", &M);
1652 
1653   Value *Pointer1 = &*F->arg_begin();
1654   BasicBlock *Entry(BasicBlock::Create(C, "", F));
1655   B.SetInsertPoint(Entry);
1656   Value *Pointer2 = B.CreateGEP(B.getInt8Ty(), Pointer1, B.getInt64(1));
1657   Instruction *LoadPointer1 =
1658       B.CreateLoad(B.getInt8Ty(), Pointer1, /* Volatile */ true);
1659   Instruction *LoadPointer2 =
1660       B.CreateLoad(B.getInt8Ty(), Pointer2, /* Volatile */ true);
1661 
1662   setupAnalyses();
1663   MemorySSA &MSSA = *Analyses->MSSA;
1664   MemorySSAWalker *Walker = Analyses->Walker;
1665 
1666   MemoryUseOrDef *Load1Access = MSSA.getMemoryAccess(LoadPointer1);
1667   MemoryUseOrDef *Load2Access = MSSA.getMemoryAccess(LoadPointer2);
1668 
1669   // When providing a memory location, we should never return a load as the
1670   // clobber.
1671   MemoryAccess *Pointer1Clobber = Walker->getClobberingMemoryAccess(
1672       Load2Access, MemoryLocation(Pointer1, LocationSize::precise(1)));
1673   EXPECT_TRUE(MSSA.isLiveOnEntryDef(Pointer1Clobber));
1674 
1675   MemoryAccess *Pointer2Clobber = Walker->getClobberingMemoryAccess(
1676       Load2Access, MemoryLocation(Pointer2, LocationSize::precise(1)));
1677   EXPECT_TRUE(MSSA.isLiveOnEntryDef(Pointer2Clobber));
1678 
1679   MemoryAccess *Load2Clobber = Walker->getClobberingMemoryAccess(Load2Access);
1680   EXPECT_EQ(Load2Clobber, Load1Access);
1681 }
1682 
1683 // We want to test if the location information are retained
1684 // when the IsGuaranteedLoopInvariant function handles a
1685 // memory access referring to a pointer defined in the entry
1686 // block, hence automatically guaranteed to be loop invariant.
1687 TEST_F(MemorySSATest, TestLoopInvariantEntryBlockPointer) {
1688   SMDiagnostic E;
1689   auto LocalM =
1690       parseAssemblyString("define void @test(i64 %a0, i8* %a1, i1* %a2) {\n"
1691                           "entry:\n"
1692                           "%v0 = getelementptr i8, i8* %a1, i64 %a0\n"
1693                           "%v1 = bitcast i8* %v0 to i64*\n"
1694                           "%v2 = bitcast i8* %v0 to i32*\n"
1695                           "%v3 = load i1, i1* %a2\n"
1696                           "br i1 %v3, label %body, label %exit\n"
1697                           "body:\n"
1698                           "store i32 1, i32* %v2\n"
1699                           "br label %exit\n"
1700                           "exit:\n"
1701                           "store i64 0, i64* %v1\n"
1702                           "ret void\n"
1703                           "}",
1704                           E, C);
1705   ASSERT_TRUE(LocalM);
1706   F = LocalM->getFunction("test");
1707   ASSERT_TRUE(F);
1708   // Setup the analysis
1709   setupAnalyses();
1710   MemorySSA &MSSA = *Analyses->MSSA;
1711   // Find the exit block
1712   for (auto &BB : *F) {
1713     if (BB.getName() == "exit") {
1714       // Get the store instruction
1715       auto *SI = BB.getFirstNonPHI();
1716       // Get the memory access and location
1717       MemoryAccess *MA = MSSA.getMemoryAccess(SI);
1718       MemoryLocation ML = MemoryLocation::get(SI);
1719       // Use the 'upward_defs_iterator' which internally calls
1720       // IsGuaranteedLoopInvariant
1721       auto ItA = upward_defs_begin({MA, ML}, MSSA.getDomTree());
1722       auto ItB =
1723           upward_defs_begin({ItA->first, ItA->second}, MSSA.getDomTree());
1724       // Check if the location information have been retained
1725       EXPECT_TRUE(ItB->second.Size.isPrecise());
1726       EXPECT_TRUE(ItB->second.Size.hasValue());
1727       EXPECT_TRUE(ItB->second.Size.getValue() == 8);
1728     }
1729   }
1730 }
1731 
1732 TEST_F(MemorySSATest, TestInvariantGroup) {
1733   SMDiagnostic E;
1734   auto M = parseAssemblyString("declare void @f(i8*)\n"
1735                                "define i8 @test(i8* %p) {\n"
1736                                "entry:\n"
1737                                "  store i8 42, i8* %p, !invariant.group !0\n"
1738                                "  call void @f(i8* %p)\n"
1739                                "  %v = load i8, i8* %p, !invariant.group !0\n"
1740                                "  ret i8 %v\n"
1741                                "}\n"
1742                                "!0 = !{}",
1743                                E, C);
1744   ASSERT_TRUE(M);
1745   F = M->getFunction("test");
1746   ASSERT_TRUE(F);
1747   setupAnalyses();
1748   MemorySSA &MSSA = *Analyses->MSSA;
1749   MemorySSAWalker *Walker = Analyses->Walker;
1750 
1751   auto &BB = F->getEntryBlock();
1752   auto &SI = cast<StoreInst>(*BB.begin());
1753   auto &Call = cast<CallBase>(*std::next(BB.begin()));
1754   auto &LI = cast<LoadInst>(*std::next(std::next(BB.begin())));
1755 
1756   {
1757     MemoryAccess *SAccess = MSSA.getMemoryAccess(&SI);
1758     MemoryAccess *LAccess = MSSA.getMemoryAccess(&LI);
1759     MemoryAccess *SClobber = Walker->getClobberingMemoryAccess(SAccess);
1760     EXPECT_TRUE(MSSA.isLiveOnEntryDef(SClobber));
1761     MemoryAccess *LClobber = Walker->getClobberingMemoryAccess(LAccess);
1762     EXPECT_EQ(SAccess, LClobber);
1763   }
1764 
1765   // remove store and verify that the memory accesses still make sense
1766   MemorySSAUpdater Updater(&MSSA);
1767   Updater.removeMemoryAccess(&SI);
1768   SI.eraseFromParent();
1769 
1770   {
1771     MemoryAccess *CallAccess = MSSA.getMemoryAccess(&Call);
1772     MemoryAccess *LAccess = MSSA.getMemoryAccess(&LI);
1773     MemoryAccess *LClobber = Walker->getClobberingMemoryAccess(LAccess);
1774     EXPECT_EQ(CallAccess, LClobber);
1775   }
1776 }
1777 
1778 static BasicBlock *getBasicBlockByName(Function &F, StringRef Name) {
1779   for (BasicBlock &BB : F)
1780     if (BB.getName() == Name)
1781       return &BB;
1782   llvm_unreachable("Expected to find basic block!");
1783 }
1784 
1785 static Instruction *getInstructionByName(Function &F, StringRef Name) {
1786   for (BasicBlock &BB : F)
1787     for (Instruction &I : BB)
1788       if (I.getName() == Name)
1789         return &I;
1790   llvm_unreachable("Expected to find instruction!");
1791 }
1792 
1793 TEST_F(MemorySSATest, TestVisitedBlocks) {
1794   SMDiagnostic E;
1795   auto M = parseAssemblyString(
1796       "define void @test(i64* noalias %P, i64 %N) {\n"
1797       "preheader.n:\n"
1798       "  br label %header.n\n"
1799       "header.n:\n"
1800       "  %n = phi i64 [ 0, %preheader.n ], [ %inc.n, %latch.n ]\n"
1801       "  %guard.cond.i = icmp slt i64 0, %N\n"
1802       "  br i1 %guard.cond.i, label %header.i.check, label %other.i\n"
1803       "header.i.check:\n"
1804       "  br label %preheader.i\n"
1805       "preheader.i:\n"
1806       "  br label %header.i\n"
1807       "header.i:\n"
1808       "  %i = phi i64 [ 0, %preheader.i ], [ %inc.i, %header.i ]\n"
1809       "  %v1 = load i64, i64* %P, align 8\n"
1810       "  %v2 = load i64, i64* %P, align 8\n"
1811       "  %inc.i = add nsw i64 %i, 1\n"
1812       "  %cmp.i = icmp slt i64 %inc.i, %N\n"
1813       "  br i1 %cmp.i, label %header.i, label %exit.i\n"
1814       "exit.i:\n"
1815       "  br label %commonexit\n"
1816       "other.i:\n"
1817       "  br label %commonexit\n"
1818       "commonexit:\n"
1819       "  br label %latch.n\n"
1820       "latch.n:\n"
1821       "  %inc.n = add nsw i64 %n, 1\n"
1822       "  %cmp.n = icmp slt i64 %inc.n, %N\n"
1823       "  br i1 %cmp.n, label %header.n, label %exit.n\n"
1824       "exit.n:\n"
1825       "  ret void\n"
1826       "}\n",
1827       E, C);
1828   ASSERT_TRUE(M);
1829   F = M->getFunction("test");
1830   ASSERT_TRUE(F);
1831   setupAnalyses();
1832   MemorySSA &MSSA = *Analyses->MSSA;
1833   MemorySSAUpdater Updater(&MSSA);
1834 
1835   {
1836     // Move %v1 before the terminator of %header.i.check
1837     BasicBlock *BB = getBasicBlockByName(*F, "header.i.check");
1838     Instruction *LI = getInstructionByName(*F, "v1");
1839     LI->moveBefore(BB->getTerminator());
1840     if (MemoryUseOrDef *MUD = MSSA.getMemoryAccess(LI))
1841       Updater.moveToPlace(MUD, BB, MemorySSA::BeforeTerminator);
1842 
1843     // Change the termiantor of %header.i.check to `br label true, label
1844     // %preheader.i, label %other.i`
1845     BB->getTerminator()->eraseFromParent();
1846     ConstantInt *BoolTrue = ConstantInt::getTrue(F->getContext());
1847     BranchInst::Create(getBasicBlockByName(*F, "preheader.i"),
1848                        getBasicBlockByName(*F, "other.i"), BoolTrue, BB);
1849     SmallVector<DominatorTree::UpdateType, 4> DTUpdates;
1850     DTUpdates.push_back(DominatorTree::UpdateType(
1851         DominatorTree::Insert, BB, getBasicBlockByName(*F, "other.i")));
1852     Updater.applyUpdates(DTUpdates, Analyses->DT, true);
1853   }
1854 
1855   // After the first moveToPlace(), %other.i is in VisitedBlocks, even after
1856   // there is a new edge to %other.i, which makes the second moveToPlace()
1857   // traverse incorrectly.
1858   {
1859     // Move %v2 before the terminator of %preheader.i
1860     BasicBlock *BB = getBasicBlockByName(*F, "preheader.i");
1861     Instruction *LI = getInstructionByName(*F, "v2");
1862     LI->moveBefore(BB->getTerminator());
1863     // Check that there is no assertion of "Incomplete phi during partial
1864     // rename"
1865     if (MemoryUseOrDef *MUD = MSSA.getMemoryAccess(LI))
1866       Updater.moveToPlace(MUD, BB, MemorySSA::BeforeTerminator);
1867   }
1868 }
1869