blob: 7295591f7b6de6232201fa34b6f8ad527a9b8cfc [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//===----------------------------------------------------------------------===//
Chandler Carruth9a67b072017-06-06 11:06:56 +00009#include "llvm/Analysis/MemorySSA.h"
Daniel Berlin83fc77b2016-03-01 18:46:54 +000010#include "llvm/Analysis/AliasAnalysis.h"
11#include "llvm/Analysis/BasicAliasAnalysis.h"
Daniel Berlin554dcd82017-04-11 20:06:36 +000012#include "llvm/Analysis/MemorySSAUpdater.h"
Daniel Berlin83fc77b2016-03-01 18:46:54 +000013#include "llvm/IR/BasicBlock.h"
Daniel Berlin14300262016-06-21 18:39:20 +000014#include "llvm/IR/DataLayout.h"
Daniel Berlin83fc77b2016-03-01 18:46:54 +000015#include "llvm/IR/Dominators.h"
16#include "llvm/IR/IRBuilder.h"
17#include "llvm/IR/Instructions.h"
18#include "llvm/IR/LLVMContext.h"
19#include "gtest/gtest.h"
20
21using namespace llvm;
22
George Burgess IV1b1fef32016-04-29 18:42:55 +000023const static char DLString[] = "e-i64:64-f80:128-n8:16:32:64-S128";
Daniel Berlin83fc77b2016-03-01 18:46:54 +000024
George Burgess IV1b1fef32016-04-29 18:42:55 +000025/// There's a lot of common setup between these tests. This fixture helps reduce
26/// that. Tests should mock up a function, store it in F, and then call
27/// setupAnalyses().
28class MemorySSATest : public testing::Test {
29protected:
30 // N.B. Many of these members depend on each other (e.g. the Module depends on
31 // the Context, etc.). So, order matters here (and in TestAnalyses).
32 LLVMContext C;
33 Module M;
34 IRBuilder<> B;
35 DataLayout DL;
36 TargetLibraryInfoImpl TLII;
37 TargetLibraryInfo TLI;
38 Function *F;
39
40 // Things that we need to build after the function is created.
41 struct TestAnalyses {
42 DominatorTree DT;
Daniel Jasperaec2fa32016-12-19 08:22:17 +000043 AssumptionCache AC;
George Burgess IV1b1fef32016-04-29 18:42:55 +000044 AAResults AA;
45 BasicAAResult BAA;
George Burgess IV80ed8632016-10-03 23:12:35 +000046 // We need to defer MSSA construction until AA is *entirely* set up, which
47 // requires calling addAAResult. Hence, we just use a pointer here.
48 std::unique_ptr<MemorySSA> MSSA;
Geoff Berryb96d3b22016-06-01 21:30:40 +000049 MemorySSAWalker *Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +000050
51 TestAnalyses(MemorySSATest &Test)
Daniel Jasperaec2fa32016-12-19 08:22:17 +000052 : DT(*Test.F), AC(*Test.F), AA(Test.TLI),
53 BAA(Test.DL, Test.TLI, AC, &DT) {
George Burgess IV1b1fef32016-04-29 18:42:55 +000054 AA.addAAResult(BAA);
George Burgess IV80ed8632016-10-03 23:12:35 +000055 MSSA = make_unique<MemorySSA>(*Test.F, &AA, &DT);
56 Walker = MSSA->getWalker();
George Burgess IV1b1fef32016-04-29 18:42:55 +000057 }
58 };
59
60 std::unique_ptr<TestAnalyses> Analyses;
61
62 void setupAnalyses() {
63 assert(F);
64 Analyses.reset(new TestAnalyses(*this));
65 }
66
67public:
68 MemorySSATest()
69 : M("MemorySSATest", C), B(C), DL(DLString), TLI(TLII), F(nullptr) {}
70};
71
Daniel Berlinf72ac492016-09-26 17:44:31 +000072TEST_F(MemorySSATest, CreateALoad) {
Daniel Berlin14300262016-06-21 18:39:20 +000073 // We create a diamond where there is a store on one side, and then after
Daniel Berlincdda3ce2016-07-31 21:08:10 +000074 // building MemorySSA, create a load after the merge point, and use it to test
Daniel Berlinf72ac492016-09-26 17:44:31 +000075 // updating by creating an access for the load.
Daniel Berlin14300262016-06-21 18:39:20 +000076 F = Function::Create(
77 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
78 GlobalValue::ExternalLinkage, "F", &M);
79 BasicBlock *Entry(BasicBlock::Create(C, "", F));
80 BasicBlock *Left(BasicBlock::Create(C, "", F));
81 BasicBlock *Right(BasicBlock::Create(C, "", F));
82 BasicBlock *Merge(BasicBlock::Create(C, "", F));
83 B.SetInsertPoint(Entry);
84 B.CreateCondBr(B.getTrue(), Left, Right);
85 B.SetInsertPoint(Left);
86 Argument *PointerArg = &*F->arg_begin();
Daniel Berlinf72ac492016-09-26 17:44:31 +000087 B.CreateStore(B.getInt8(16), PointerArg);
Daniel Berlin14300262016-06-21 18:39:20 +000088 BranchInst::Create(Merge, Left);
89 BranchInst::Create(Merge, Right);
90
91 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +000092 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlin17e8d0e2017-02-22 22:19:55 +000093 MemorySSAUpdater Updater(&MSSA);
Daniel Berlin14300262016-06-21 18:39:20 +000094 // Add the load
95 B.SetInsertPoint(Merge);
96 LoadInst *LoadInst = B.CreateLoad(PointerArg);
Daniel Berlin14300262016-06-21 18:39:20 +000097
Daniel Berlinf72ac492016-09-26 17:44:31 +000098 // MemoryPHI should already exist.
99 MemoryPhi *MP = MSSA.getMemoryAccess(Merge);
100 EXPECT_NE(MP, nullptr);
Daniel Berlin14300262016-06-21 18:39:20 +0000101
102 // Create the load memory acccess
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000103 MemoryUse *LoadAccess = cast<MemoryUse>(Updater.createMemoryAccessInBB(
104 LoadInst, MP, Merge, MemorySSA::Beginning));
Daniel Berlin14300262016-06-21 18:39:20 +0000105 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
106 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
107 MSSA.verifyMemorySSA();
108}
Daniel Berlin60ead052017-01-28 01:23:13 +0000109TEST_F(MemorySSATest, CreateLoadsAndStoreUpdater) {
110 // We create a diamond, then build memoryssa with no memory accesses, and
111 // incrementally update it by inserting a store in the, entry, a load in the
112 // merge point, then a store in the branch, another load in the merge point,
113 // and then a store in the entry.
114 F = Function::Create(
115 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
116 GlobalValue::ExternalLinkage, "F", &M);
117 BasicBlock *Entry(BasicBlock::Create(C, "", F));
118 BasicBlock *Left(BasicBlock::Create(C, "", F));
119 BasicBlock *Right(BasicBlock::Create(C, "", F));
120 BasicBlock *Merge(BasicBlock::Create(C, "", F));
121 B.SetInsertPoint(Entry);
122 B.CreateCondBr(B.getTrue(), Left, Right);
123 B.SetInsertPoint(Left, Left->begin());
124 Argument *PointerArg = &*F->arg_begin();
125 B.SetInsertPoint(Left);
126 B.CreateBr(Merge);
127 B.SetInsertPoint(Right);
128 B.CreateBr(Merge);
129
130 setupAnalyses();
131 MemorySSA &MSSA = *Analyses->MSSA;
132 MemorySSAUpdater Updater(&MSSA);
133 // Add the store
134 B.SetInsertPoint(Entry, Entry->begin());
135 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000136 MemoryAccess *EntryStoreAccess = Updater.createMemoryAccessInBB(
Daniel Berlin60ead052017-01-28 01:23:13 +0000137 EntryStore, nullptr, Entry, MemorySSA::Beginning);
138 Updater.insertDef(cast<MemoryDef>(EntryStoreAccess));
139
140 // Add the load
141 B.SetInsertPoint(Merge, Merge->begin());
142 LoadInst *FirstLoad = B.CreateLoad(PointerArg);
143
144 // MemoryPHI should not already exist.
145 MemoryPhi *MP = MSSA.getMemoryAccess(Merge);
146 EXPECT_EQ(MP, nullptr);
147
148 // Create the load memory access
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000149 MemoryUse *FirstLoadAccess = cast<MemoryUse>(Updater.createMemoryAccessInBB(
Daniel Berlin60ead052017-01-28 01:23:13 +0000150 FirstLoad, nullptr, Merge, MemorySSA::Beginning));
151 Updater.insertUse(FirstLoadAccess);
152 // Should just have a load using the entry access, because it should discover
153 // the phi is trivial
154 EXPECT_EQ(FirstLoadAccess->getDefiningAccess(), EntryStoreAccess);
155
156 // Create a store on the left
157 // Add the store
158 B.SetInsertPoint(Left, Left->begin());
159 StoreInst *LeftStore = B.CreateStore(B.getInt8(16), PointerArg);
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000160 MemoryAccess *LeftStoreAccess = Updater.createMemoryAccessInBB(
Daniel Berlin60ead052017-01-28 01:23:13 +0000161 LeftStore, nullptr, Left, MemorySSA::Beginning);
Daniel Berlin78cbd282017-02-20 22:26:03 +0000162 Updater.insertDef(cast<MemoryDef>(LeftStoreAccess), false);
Daniel Berlin60ead052017-01-28 01:23:13 +0000163 // We don't touch existing loads, so we need to create a new one to get a phi
164 // Add the second load
165 B.SetInsertPoint(Merge, Merge->begin());
166 LoadInst *SecondLoad = B.CreateLoad(PointerArg);
167
168 // MemoryPHI should not already exist.
169 MP = MSSA.getMemoryAccess(Merge);
170 EXPECT_EQ(MP, nullptr);
171
172 // Create the load memory access
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000173 MemoryUse *SecondLoadAccess = cast<MemoryUse>(Updater.createMemoryAccessInBB(
Daniel Berlin60ead052017-01-28 01:23:13 +0000174 SecondLoad, nullptr, Merge, MemorySSA::Beginning));
175 Updater.insertUse(SecondLoadAccess);
176 // Now the load should be a phi of the entry store and the left store
177 MemoryPhi *MergePhi =
178 dyn_cast<MemoryPhi>(SecondLoadAccess->getDefiningAccess());
179 EXPECT_NE(MergePhi, nullptr);
180 EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
181 EXPECT_EQ(MergePhi->getIncomingValue(1), LeftStoreAccess);
182 // Now create a store below the existing one in the entry
183 B.SetInsertPoint(Entry, --Entry->end());
184 StoreInst *SecondEntryStore = B.CreateStore(B.getInt8(16), PointerArg);
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000185 MemoryAccess *SecondEntryStoreAccess = Updater.createMemoryAccessInBB(
Daniel Berlin60ead052017-01-28 01:23:13 +0000186 SecondEntryStore, nullptr, Entry, MemorySSA::End);
Daniel Berlin78cbd282017-02-20 22:26:03 +0000187 // Insert it twice just to test renaming
188 Updater.insertDef(cast<MemoryDef>(SecondEntryStoreAccess), false);
189 EXPECT_NE(FirstLoadAccess->getDefiningAccess(), MergePhi);
190 Updater.insertDef(cast<MemoryDef>(SecondEntryStoreAccess), true);
191 EXPECT_EQ(FirstLoadAccess->getDefiningAccess(), MergePhi);
Daniel Berlin60ead052017-01-28 01:23:13 +0000192 // and make sure the phi below it got updated, despite being blocks away
193 MergePhi = dyn_cast<MemoryPhi>(SecondLoadAccess->getDefiningAccess());
194 EXPECT_NE(MergePhi, nullptr);
195 EXPECT_EQ(MergePhi->getIncomingValue(0), SecondEntryStoreAccess);
196 EXPECT_EQ(MergePhi->getIncomingValue(1), LeftStoreAccess);
197 MSSA.verifyMemorySSA();
198}
199
200TEST_F(MemorySSATest, CreateALoadUpdater) {
201 // We create a diamond, then build memoryssa with no memory accesses, and
202 // incrementally update it by inserting a store in one of the branches, and a
203 // load in the merge point
204 F = Function::Create(
205 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
206 GlobalValue::ExternalLinkage, "F", &M);
207 BasicBlock *Entry(BasicBlock::Create(C, "", F));
208 BasicBlock *Left(BasicBlock::Create(C, "", F));
209 BasicBlock *Right(BasicBlock::Create(C, "", F));
210 BasicBlock *Merge(BasicBlock::Create(C, "", F));
211 B.SetInsertPoint(Entry);
212 B.CreateCondBr(B.getTrue(), Left, Right);
213 B.SetInsertPoint(Left, Left->begin());
214 Argument *PointerArg = &*F->arg_begin();
215 B.SetInsertPoint(Left);
216 B.CreateBr(Merge);
217 B.SetInsertPoint(Right);
218 B.CreateBr(Merge);
219
220 setupAnalyses();
221 MemorySSA &MSSA = *Analyses->MSSA;
222 MemorySSAUpdater Updater(&MSSA);
223 B.SetInsertPoint(Left, Left->begin());
224 // Add the store
225 StoreInst *SI = B.CreateStore(B.getInt8(16), PointerArg);
226 MemoryAccess *StoreAccess =
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000227 Updater.createMemoryAccessInBB(SI, nullptr, Left, MemorySSA::Beginning);
Daniel Berlin60ead052017-01-28 01:23:13 +0000228 Updater.insertDef(cast<MemoryDef>(StoreAccess));
229
230 // Add the load
231 B.SetInsertPoint(Merge, Merge->begin());
232 LoadInst *LoadInst = B.CreateLoad(PointerArg);
233
234 // MemoryPHI should not already exist.
235 MemoryPhi *MP = MSSA.getMemoryAccess(Merge);
236 EXPECT_EQ(MP, nullptr);
237
238 // Create the load memory acccess
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000239 MemoryUse *LoadAccess = cast<MemoryUse>(Updater.createMemoryAccessInBB(
Daniel Berlin60ead052017-01-28 01:23:13 +0000240 LoadInst, nullptr, Merge, MemorySSA::Beginning));
241 Updater.insertUse(LoadAccess);
242 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
243 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
244 MSSA.verifyMemorySSA();
245}
Daniel Berlin14300262016-06-21 18:39:20 +0000246
Daniel Berlin5130cc82016-07-31 21:08:20 +0000247TEST_F(MemorySSATest, MoveAStore) {
248 // We create a diamond where there is a in the entry, a store on one side, and
249 // a load at the end. After building MemorySSA, we test updating by moving
Daniel Berlin60ead052017-01-28 01:23:13 +0000250 // the store from the side block to the entry block. This destroys the old
251 // access.
Daniel Berlin5130cc82016-07-31 21:08:20 +0000252 F = Function::Create(
253 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
254 GlobalValue::ExternalLinkage, "F", &M);
255 BasicBlock *Entry(BasicBlock::Create(C, "", F));
256 BasicBlock *Left(BasicBlock::Create(C, "", F));
257 BasicBlock *Right(BasicBlock::Create(C, "", F));
258 BasicBlock *Merge(BasicBlock::Create(C, "", F));
259 B.SetInsertPoint(Entry);
260 Argument *PointerArg = &*F->arg_begin();
261 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
262 B.CreateCondBr(B.getTrue(), Left, Right);
263 B.SetInsertPoint(Left);
264 StoreInst *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
265 BranchInst::Create(Merge, Left);
266 BranchInst::Create(Merge, Right);
267 B.SetInsertPoint(Merge);
268 B.CreateLoad(PointerArg);
269 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000270 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000271 MemorySSAUpdater Updater(&MSSA);
Daniel Berlin5130cc82016-07-31 21:08:20 +0000272 // Move the store
273 SideStore->moveBefore(Entry->getTerminator());
274 MemoryAccess *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
275 MemoryAccess *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000276 MemoryAccess *NewStoreAccess = Updater.createMemoryAccessAfter(
George Burgess IVf7672852016-08-03 19:59:11 +0000277 SideStore, EntryStoreAccess, EntryStoreAccess);
Daniel Berlin5130cc82016-07-31 21:08:20 +0000278 EntryStoreAccess->replaceAllUsesWith(NewStoreAccess);
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000279 Updater.removeMemoryAccess(SideStoreAccess);
Daniel Berlin5130cc82016-07-31 21:08:20 +0000280 MSSA.verifyMemorySSA();
281}
282
Daniel Berlin60ead052017-01-28 01:23:13 +0000283TEST_F(MemorySSATest, MoveAStoreUpdater) {
284 // We create a diamond where there is a in the entry, a store on one side, and
285 // a load at the end. After building MemorySSA, we test updating by moving
286 // the store from the side block to the entry block. This destroys the old
287 // access.
288 F = Function::Create(
289 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
290 GlobalValue::ExternalLinkage, "F", &M);
291 BasicBlock *Entry(BasicBlock::Create(C, "", F));
292 BasicBlock *Left(BasicBlock::Create(C, "", F));
293 BasicBlock *Right(BasicBlock::Create(C, "", F));
294 BasicBlock *Merge(BasicBlock::Create(C, "", F));
295 B.SetInsertPoint(Entry);
296 Argument *PointerArg = &*F->arg_begin();
297 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
298 B.CreateCondBr(B.getTrue(), Left, Right);
299 B.SetInsertPoint(Left);
300 auto *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
301 BranchInst::Create(Merge, Left);
302 BranchInst::Create(Merge, Right);
303 B.SetInsertPoint(Merge);
304 auto *MergeLoad = B.CreateLoad(PointerArg);
305 setupAnalyses();
306 MemorySSA &MSSA = *Analyses->MSSA;
307 MemorySSAUpdater Updater(&MSSA);
308
309 // Move the store
310 SideStore->moveBefore(Entry->getTerminator());
311 auto *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
312 auto *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000313 auto *NewStoreAccess = Updater.createMemoryAccessAfter(
Daniel Berlin60ead052017-01-28 01:23:13 +0000314 SideStore, EntryStoreAccess, EntryStoreAccess);
315 // Before, the load will point to a phi of the EntryStore and SideStore.
316 auto *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(MergeLoad));
317 EXPECT_TRUE(isa<MemoryPhi>(LoadAccess->getDefiningAccess()));
318 MemoryPhi *MergePhi = cast<MemoryPhi>(LoadAccess->getDefiningAccess());
319 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
320 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000321 Updater.removeMemoryAccess(SideStoreAccess);
Daniel Berlin60ead052017-01-28 01:23:13 +0000322 Updater.insertDef(cast<MemoryDef>(NewStoreAccess));
323 // After it's a phi of the new side store access.
324 EXPECT_EQ(MergePhi->getIncomingValue(0), NewStoreAccess);
325 EXPECT_EQ(MergePhi->getIncomingValue(1), NewStoreAccess);
326 MSSA.verifyMemorySSA();
327}
328
329TEST_F(MemorySSATest, MoveAStoreUpdaterMove) {
330 // We create a diamond where there is a in the entry, a store on one side, and
331 // a load at the end. After building MemorySSA, we test updating by moving
332 // the store from the side block to the entry block. This does not destroy
333 // the old access.
334 F = Function::Create(
335 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
336 GlobalValue::ExternalLinkage, "F", &M);
337 BasicBlock *Entry(BasicBlock::Create(C, "", F));
338 BasicBlock *Left(BasicBlock::Create(C, "", F));
339 BasicBlock *Right(BasicBlock::Create(C, "", F));
340 BasicBlock *Merge(BasicBlock::Create(C, "", F));
341 B.SetInsertPoint(Entry);
342 Argument *PointerArg = &*F->arg_begin();
343 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
344 B.CreateCondBr(B.getTrue(), Left, Right);
345 B.SetInsertPoint(Left);
346 auto *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
347 BranchInst::Create(Merge, Left);
348 BranchInst::Create(Merge, Right);
349 B.SetInsertPoint(Merge);
350 auto *MergeLoad = B.CreateLoad(PointerArg);
351 setupAnalyses();
352 MemorySSA &MSSA = *Analyses->MSSA;
353 MemorySSAUpdater Updater(&MSSA);
354
355 // Move the store
356 auto *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
357 auto *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
358 // Before, the load will point to a phi of the EntryStore and SideStore.
359 auto *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(MergeLoad));
360 EXPECT_TRUE(isa<MemoryPhi>(LoadAccess->getDefiningAccess()));
361 MemoryPhi *MergePhi = cast<MemoryPhi>(LoadAccess->getDefiningAccess());
362 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
363 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
364 SideStore->moveBefore(*EntryStore->getParent(), ++EntryStore->getIterator());
365 Updater.moveAfter(SideStoreAccess, EntryStoreAccess);
366 // After, it's a phi of the side store.
367 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
368 EXPECT_EQ(MergePhi->getIncomingValue(1), SideStoreAccess);
369
370 MSSA.verifyMemorySSA();
371}
372
Daniel Berlin9d8a3352017-01-30 11:35:39 +0000373TEST_F(MemorySSATest, MoveAStoreAllAround) {
374 // We create a diamond where there is a in the entry, a store on one side, and
375 // a load at the end. After building MemorySSA, we test updating by moving
376 // the store from the side block to the entry block, then to the other side
377 // block, then to before the load. This does not destroy the old access.
378 F = Function::Create(
379 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
380 GlobalValue::ExternalLinkage, "F", &M);
381 BasicBlock *Entry(BasicBlock::Create(C, "", F));
382 BasicBlock *Left(BasicBlock::Create(C, "", F));
383 BasicBlock *Right(BasicBlock::Create(C, "", F));
384 BasicBlock *Merge(BasicBlock::Create(C, "", F));
385 B.SetInsertPoint(Entry);
386 Argument *PointerArg = &*F->arg_begin();
387 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
388 B.CreateCondBr(B.getTrue(), Left, Right);
389 B.SetInsertPoint(Left);
390 auto *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
391 BranchInst::Create(Merge, Left);
392 BranchInst::Create(Merge, Right);
393 B.SetInsertPoint(Merge);
394 auto *MergeLoad = B.CreateLoad(PointerArg);
395 setupAnalyses();
396 MemorySSA &MSSA = *Analyses->MSSA;
397 MemorySSAUpdater Updater(&MSSA);
398
399 // Move the store
400 auto *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
401 auto *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
402 // Before, the load will point to a phi of the EntryStore and SideStore.
403 auto *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(MergeLoad));
404 EXPECT_TRUE(isa<MemoryPhi>(LoadAccess->getDefiningAccess()));
405 MemoryPhi *MergePhi = cast<MemoryPhi>(LoadAccess->getDefiningAccess());
406 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
407 EXPECT_EQ(MergePhi->getIncomingValue(0), SideStoreAccess);
408 // Move the store before the entry store
409 SideStore->moveBefore(*EntryStore->getParent(), EntryStore->getIterator());
410 Updater.moveBefore(SideStoreAccess, EntryStoreAccess);
411 // After, it's a phi of the entry store.
412 EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
413 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
414 MSSA.verifyMemorySSA();
415 // Now move the store to the right branch
416 SideStore->moveBefore(*Right, Right->begin());
417 Updater.moveToPlace(SideStoreAccess, Right, MemorySSA::Beginning);
418 MSSA.verifyMemorySSA();
419 EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
420 EXPECT_EQ(MergePhi->getIncomingValue(1), SideStoreAccess);
421 // Now move it before the load
422 SideStore->moveBefore(MergeLoad);
423 Updater.moveBefore(SideStoreAccess, LoadAccess);
424 EXPECT_EQ(MergePhi->getIncomingValue(0), EntryStoreAccess);
425 EXPECT_EQ(MergePhi->getIncomingValue(1), EntryStoreAccess);
426 MSSA.verifyMemorySSA();
427}
428
Daniel Berlin14300262016-06-21 18:39:20 +0000429TEST_F(MemorySSATest, RemoveAPhi) {
430 // We create a diamond where there is a store on one side, and then a load
431 // after the merge point. This enables us to test a bunch of different
432 // removal cases.
433 F = Function::Create(
434 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
435 GlobalValue::ExternalLinkage, "F", &M);
436 BasicBlock *Entry(BasicBlock::Create(C, "", F));
437 BasicBlock *Left(BasicBlock::Create(C, "", F));
438 BasicBlock *Right(BasicBlock::Create(C, "", F));
439 BasicBlock *Merge(BasicBlock::Create(C, "", F));
440 B.SetInsertPoint(Entry);
441 B.CreateCondBr(B.getTrue(), Left, Right);
442 B.SetInsertPoint(Left);
443 Argument *PointerArg = &*F->arg_begin();
444 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
445 BranchInst::Create(Merge, Left);
446 BranchInst::Create(Merge, Right);
447 B.SetInsertPoint(Merge);
448 LoadInst *LoadInst = B.CreateLoad(PointerArg);
449
450 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000451 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000452 MemorySSAUpdater Updater(&MSSA);
453
Daniel Berlin14300262016-06-21 18:39:20 +0000454 // Before, the load will be a use of a phi<store, liveonentry>.
455 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst));
456 MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
457 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
458 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
459 // Kill the store
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000460 Updater.removeMemoryAccess(StoreAccess);
Daniel Berlin14300262016-06-21 18:39:20 +0000461 MemoryPhi *MP = cast<MemoryPhi>(DefiningAccess);
462 // Verify the phi ended up as liveonentry, liveonentry
463 for (auto &Op : MP->incoming_values())
464 EXPECT_TRUE(MSSA.isLiveOnEntryDef(cast<MemoryAccess>(Op.get())));
465 // Replace the phi uses with the live on entry def
466 MP->replaceAllUsesWith(MSSA.getLiveOnEntryDef());
467 // Verify the load is now defined by liveOnEntryDef
468 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
469 // Remove the PHI
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000470 Updater.removeMemoryAccess(MP);
Daniel Berlin14300262016-06-21 18:39:20 +0000471 MSSA.verifyMemorySSA();
472}
473
George Burgess IV1b1fef32016-04-29 18:42:55 +0000474TEST_F(MemorySSATest, RemoveMemoryAccess) {
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000475 // We create a diamond where there is a store on one side, and then a load
476 // after the merge point. This enables us to test a bunch of different
477 // removal cases.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000478 F = Function::Create(
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000479 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
George Burgess IV1b1fef32016-04-29 18:42:55 +0000480 GlobalValue::ExternalLinkage, "F", &M);
Daniel Berlin64120022016-03-02 21:16:28 +0000481 BasicBlock *Entry(BasicBlock::Create(C, "", F));
482 BasicBlock *Left(BasicBlock::Create(C, "", F));
483 BasicBlock *Right(BasicBlock::Create(C, "", F));
484 BasicBlock *Merge(BasicBlock::Create(C, "", F));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000485 B.SetInsertPoint(Entry);
486 B.CreateCondBr(B.getTrue(), Left, Right);
487 B.SetInsertPoint(Left);
488 Argument *PointerArg = &*F->arg_begin();
489 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
490 BranchInst::Create(Merge, Left);
491 BranchInst::Create(Merge, Right);
492 B.SetInsertPoint(Merge);
493 LoadInst *LoadInst = B.CreateLoad(PointerArg);
494
George Burgess IV1b1fef32016-04-29 18:42:55 +0000495 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000496 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000497 MemorySSAWalker *Walker = Analyses->Walker;
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000498 MemorySSAUpdater Updater(&MSSA);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000499
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000500 // Before, the load will be a use of a phi<store, liveonentry>. It should be
501 // the same after.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000502 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst));
503 MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000504 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
505 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
506 // The load is currently clobbered by one of the phi arguments, so the walker
507 // should determine the clobbering access as the phi.
508 EXPECT_EQ(DefiningAccess, Walker->getClobberingMemoryAccess(LoadInst));
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000509 Updater.removeMemoryAccess(StoreAccess);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000510 MSSA.verifyMemorySSA();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000511 // After the removeaccess, let's see if we got the right accesses
512 // The load should still point to the phi ...
513 EXPECT_EQ(DefiningAccess, LoadAccess->getDefiningAccess());
514 // but we should now get live on entry for the clobbering definition of the
515 // load, since it will walk past the phi node since every argument is the
516 // same.
Daniel Berlincd2deac2016-10-20 20:13:45 +0000517 // XXX: This currently requires either removing the phi or resetting optimized
518 // on the load
519
520 EXPECT_FALSE(
521 MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst)));
522 // If we reset optimized, we get live on entry.
523 LoadAccess->resetOptimized();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000524 EXPECT_TRUE(
George Burgess IV1b1fef32016-04-29 18:42:55 +0000525 MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst)));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000526 // The phi should now be a two entry phi with two live on entry defs.
527 for (const auto &Op : DefiningAccess->operands()) {
528 MemoryAccess *Operand = cast<MemoryAccess>(&*Op);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000529 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Operand));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000530 }
531
532 // Now we try to remove the single valued phi
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000533 Updater.removeMemoryAccess(DefiningAccess);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000534 MSSA.verifyMemorySSA();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000535 // Now the load should be a load of live on entry.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000536 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
537}
538
539// We had a bug with caching where the walker would report MemoryDef#3's clobber
540// (below) was MemoryDef#1.
541//
542// define void @F(i8*) {
543// %A = alloca i8, i8 1
544// ; 1 = MemoryDef(liveOnEntry)
545// store i8 0, i8* %A
546// ; 2 = MemoryDef(1)
547// store i8 1, i8* %A
548// ; 3 = MemoryDef(2)
549// store i8 2, i8* %A
550// }
551TEST_F(MemorySSATest, TestTripleStore) {
Daniel Berlin14300262016-06-21 18:39:20 +0000552 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
553 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000554 B.SetInsertPoint(BasicBlock::Create(C, "", F));
555 Type *Int8 = Type::getInt8Ty(C);
556 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
557 StoreInst *S1 = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
558 StoreInst *S2 = B.CreateStore(ConstantInt::get(Int8, 1), Alloca);
559 StoreInst *S3 = B.CreateStore(ConstantInt::get(Int8, 2), Alloca);
560
561 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000562 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000563 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000564
565 unsigned I = 0;
566 for (StoreInst *V : {S1, S2, S3}) {
567 // Everything should be clobbered by its defining access
George Burgess IV66837ab2016-11-01 21:17:46 +0000568 MemoryAccess *DefiningAccess = MSSA.getMemoryAccess(V)->getDefiningAccess();
George Burgess IV1b1fef32016-04-29 18:42:55 +0000569 MemoryAccess *WalkerClobber = Walker->getClobberingMemoryAccess(V);
570 EXPECT_EQ(DefiningAccess, WalkerClobber)
571 << "Store " << I << " doesn't have the correct clobbering access";
572 // EXPECT_EQ expands such that if we increment I above, it won't get
573 // incremented except when we try to print the error message.
574 ++I;
575 }
576}
577
578// ...And fixing the above bug made it obvious that, when walking, MemorySSA's
579// walker was caching the initial node it walked. This was fine (albeit
580// mostly redundant) unless the initial node being walked is a clobber for the
581// query. In that case, we'd cache that the node clobbered itself.
582TEST_F(MemorySSATest, TestStoreAndLoad) {
Daniel Berlin14300262016-06-21 18:39:20 +0000583 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
584 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000585 B.SetInsertPoint(BasicBlock::Create(C, "", F));
586 Type *Int8 = Type::getInt8Ty(C);
587 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
588 Instruction *SI = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
589 Instruction *LI = B.CreateLoad(Alloca);
590
591 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000592 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000593 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000594
595 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LI);
596 EXPECT_EQ(LoadClobber, MSSA.getMemoryAccess(SI));
597 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(SI)));
598}
599
600// Another bug (related to the above two fixes): It was noted that, given the
601// following code:
602// ; 1 = MemoryDef(liveOnEntry)
603// store i8 0, i8* %1
604//
605// ...A query to getClobberingMemoryAccess(MemoryAccess*, MemoryLocation) would
606// hand back the store (correctly). A later call to
607// getClobberingMemoryAccess(const Instruction*) would also hand back the store
608// (incorrectly; it should return liveOnEntry).
609//
610// This test checks that repeated calls to either function returns what they're
611// meant to.
612TEST_F(MemorySSATest, TestStoreDoubleQuery) {
Daniel Berlin14300262016-06-21 18:39:20 +0000613 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
614 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000615 B.SetInsertPoint(BasicBlock::Create(C, "", F));
616 Type *Int8 = Type::getInt8Ty(C);
617 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
618 StoreInst *SI = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
619
620 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000621 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000622 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000623
624 MemoryAccess *StoreAccess = MSSA.getMemoryAccess(SI);
625 MemoryLocation StoreLoc = MemoryLocation::get(SI);
Daniel Berlin14300262016-06-21 18:39:20 +0000626 MemoryAccess *Clobber =
627 Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000628 MemoryAccess *LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
629
630 EXPECT_EQ(Clobber, StoreAccess);
631 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LiveOnEntry));
632
633 // Try again (with entries in the cache already) for good measure...
634 Clobber = Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
635 LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
636 EXPECT_EQ(Clobber, StoreAccess);
637 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LiveOnEntry));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000638}
George Burgess IV14633b52016-08-03 01:22:19 +0000639
640// Bug: During phi optimization, the walker wouldn't cache to the proper result
641// in the farthest-walked BB.
642//
643// Specifically, it would assume that whatever we walked to was a clobber.
644// "Whatever we walked to" isn't a clobber if we hit a cache entry.
645//
646// ...So, we need a test case that looks like:
647// A
648// / \
649// B |
650// \ /
651// C
652//
653// Where, when we try to optimize a thing in 'C', a blocker is found in 'B'.
654// The walk must determine that the blocker exists by using cache entries *while
655// walking* 'B'.
656TEST_F(MemorySSATest, PartialWalkerCacheWithPhis) {
657 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
658 GlobalValue::ExternalLinkage, "F", &M);
659 B.SetInsertPoint(BasicBlock::Create(C, "A", F));
660 Type *Int8 = Type::getInt8Ty(C);
661 Constant *One = ConstantInt::get(Int8, 1);
662 Constant *Zero = ConstantInt::get(Int8, 0);
663 Value *AllocA = B.CreateAlloca(Int8, One, "a");
664 Value *AllocB = B.CreateAlloca(Int8, One, "b");
665 BasicBlock *IfThen = BasicBlock::Create(C, "B", F);
666 BasicBlock *IfEnd = BasicBlock::Create(C, "C", F);
667
668 B.CreateCondBr(UndefValue::get(Type::getInt1Ty(C)), IfThen, IfEnd);
669
670 B.SetInsertPoint(IfThen);
671 Instruction *FirstStore = B.CreateStore(Zero, AllocA);
672 B.CreateStore(Zero, AllocB);
673 Instruction *ALoad0 = B.CreateLoad(AllocA, "");
674 Instruction *BStore = B.CreateStore(Zero, AllocB);
675 // Due to use optimization/etc. we make a store to A, which is removed after
676 // we build MSSA. This helps keep the test case simple-ish.
677 Instruction *KillStore = B.CreateStore(Zero, AllocA);
678 Instruction *ALoad = B.CreateLoad(AllocA, "");
679 B.CreateBr(IfEnd);
680
681 B.SetInsertPoint(IfEnd);
682 Instruction *BelowPhi = B.CreateStore(Zero, AllocA);
683
684 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000685 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IV14633b52016-08-03 01:22:19 +0000686 MemorySSAWalker *Walker = Analyses->Walker;
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000687 MemorySSAUpdater Updater(&MSSA);
George Burgess IV14633b52016-08-03 01:22:19 +0000688
689 // Kill `KillStore`; it exists solely so that the load after it won't be
690 // optimized to FirstStore.
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000691 Updater.removeMemoryAccess(MSSA.getMemoryAccess(KillStore));
George Burgess IV14633b52016-08-03 01:22:19 +0000692 KillStore->eraseFromParent();
693 auto *ALoadMA = cast<MemoryUse>(MSSA.getMemoryAccess(ALoad));
694 EXPECT_EQ(ALoadMA->getDefiningAccess(), MSSA.getMemoryAccess(BStore));
695
696 // Populate the cache for the store to AllocB directly after FirstStore. It
697 // should point to something in block B (so something in D can't be optimized
698 // to it).
699 MemoryAccess *Load0Clobber = Walker->getClobberingMemoryAccess(ALoad0);
700 EXPECT_EQ(MSSA.getMemoryAccess(FirstStore), Load0Clobber);
701
702 // If the bug exists, this will introduce a bad cache entry for %a on BStore.
703 // It will point to the store to %b after FirstStore. This only happens during
704 // phi optimization.
705 MemoryAccess *BottomClobber = Walker->getClobberingMemoryAccess(BelowPhi);
706 MemoryAccess *Phi = MSSA.getMemoryAccess(IfEnd);
707 EXPECT_EQ(BottomClobber, Phi);
708
709 // This query will first check the cache for {%a, BStore}. It should point to
710 // FirstStore, not to the store after FirstStore.
711 MemoryAccess *UseClobber = Walker->getClobberingMemoryAccess(ALoad);
712 EXPECT_EQ(UseClobber, MSSA.getMemoryAccess(FirstStore));
713}
George Burgess IV024f3d22016-08-03 19:57:02 +0000714
715// Test that our walker properly handles loads with the invariant group
716// attribute. It's a bit hacky, since we add the invariant attribute *after*
717// building MSSA. Otherwise, the use optimizer will optimize it for us, which
718// isn't what we want.
719// FIXME: It may be easier/cleaner to just add an 'optimize uses?' flag to MSSA.
720TEST_F(MemorySSATest, WalkerInvariantLoadOpt) {
721 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
722 GlobalValue::ExternalLinkage, "F", &M);
723 B.SetInsertPoint(BasicBlock::Create(C, "", F));
724 Type *Int8 = Type::getInt8Ty(C);
725 Constant *One = ConstantInt::get(Int8, 1);
726 Value *AllocA = B.CreateAlloca(Int8, One, "");
727
728 Instruction *Store = B.CreateStore(One, AllocA);
729 Instruction *Load = B.CreateLoad(AllocA);
730
731 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000732 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IV024f3d22016-08-03 19:57:02 +0000733 MemorySSAWalker *Walker = Analyses->Walker;
734
735 auto *LoadMA = cast<MemoryUse>(MSSA.getMemoryAccess(Load));
736 auto *StoreMA = cast<MemoryDef>(MSSA.getMemoryAccess(Store));
737 EXPECT_EQ(LoadMA->getDefiningAccess(), StoreMA);
738
739 // ...At the time of writing, no cache should exist for LoadMA. Be a bit
740 // flexible to future changes.
741 Walker->invalidateInfo(LoadMA);
742 Load->setMetadata(LLVMContext::MD_invariant_load, MDNode::get(C, {}));
743
744 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LoadMA);
745 EXPECT_EQ(LoadClobber, MSSA.getLiveOnEntryDef());
746}
George Burgess IV80ed8632016-10-03 23:12:35 +0000747
Daniel Berlincd2deac2016-10-20 20:13:45 +0000748// Test loads get reoptimized properly by the walker.
749TEST_F(MemorySSATest, WalkerReopt) {
George Burgess IV80ed8632016-10-03 23:12:35 +0000750 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
751 GlobalValue::ExternalLinkage, "F", &M);
752 B.SetInsertPoint(BasicBlock::Create(C, "", F));
George Burgess IV80ed8632016-10-03 23:12:35 +0000753 Type *Int8 = Type::getInt8Ty(C);
Daniel Berlincd2deac2016-10-20 20:13:45 +0000754 Value *AllocaA = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
755 Instruction *SIA = B.CreateStore(ConstantInt::get(Int8, 0), AllocaA);
756 Value *AllocaB = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
757 Instruction *SIB = B.CreateStore(ConstantInt::get(Int8, 0), AllocaB);
758 Instruction *LIA = B.CreateLoad(AllocaA);
George Burgess IV80ed8632016-10-03 23:12:35 +0000759
760 setupAnalyses();
761 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlincd2deac2016-10-20 20:13:45 +0000762 MemorySSAWalker *Walker = Analyses->Walker;
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000763 MemorySSAUpdater Updater(&MSSA);
Daniel Berlincd2deac2016-10-20 20:13:45 +0000764
765 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LIA);
766 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LIA));
767 EXPECT_EQ(LoadClobber, MSSA.getMemoryAccess(SIA));
768 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(SIA)));
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000769 Updater.removeMemoryAccess(LoadAccess);
Daniel Berlincd2deac2016-10-20 20:13:45 +0000770
771 // Create the load memory access pointing to an unoptimized place.
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000772 MemoryUse *NewLoadAccess = cast<MemoryUse>(Updater.createMemoryAccessInBB(
Daniel Berlincd2deac2016-10-20 20:13:45 +0000773 LIA, MSSA.getMemoryAccess(SIB), LIA->getParent(), MemorySSA::End));
774 // This should it cause it to be optimized
775 EXPECT_EQ(Walker->getClobberingMemoryAccess(NewLoadAccess), LoadClobber);
776 EXPECT_EQ(NewLoadAccess->getDefiningAccess(), LoadClobber);
George Burgess IV80ed8632016-10-03 23:12:35 +0000777}
Bryant Wong4213d942016-12-25 23:34:07 +0000778
Daniel Berlin60ead052017-01-28 01:23:13 +0000779// Test out MemorySSAUpdater::moveBefore
780TEST_F(MemorySSATest, MoveAboveMemoryDef) {
Bryant Wong4213d942016-12-25 23:34:07 +0000781 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
782 GlobalValue::ExternalLinkage, "F", &M);
783 B.SetInsertPoint(BasicBlock::Create(C, "", F));
784
785 Type *Int8 = Type::getInt8Ty(C);
786 Value *A = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
787 Value *B_ = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
788 Value *C = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "C");
789
790 StoreInst *StoreA0 = B.CreateStore(ConstantInt::get(Int8, 0), A);
791 StoreInst *StoreB = B.CreateStore(ConstantInt::get(Int8, 0), B_);
792 LoadInst *LoadB = B.CreateLoad(B_);
793 StoreInst *StoreA1 = B.CreateStore(ConstantInt::get(Int8, 4), A);
Bryant Wong4213d942016-12-25 23:34:07 +0000794 StoreInst *StoreC = B.CreateStore(ConstantInt::get(Int8, 4), C);
795 StoreInst *StoreA2 = B.CreateStore(ConstantInt::get(Int8, 4), A);
796 LoadInst *LoadC = B.CreateLoad(C);
797
798 setupAnalyses();
799 MemorySSA &MSSA = *Analyses->MSSA;
800 MemorySSAWalker &Walker = *Analyses->Walker;
801
Daniel Berlin60ead052017-01-28 01:23:13 +0000802 MemorySSAUpdater Updater(&MSSA);
Bryant Wong4213d942016-12-25 23:34:07 +0000803 StoreC->moveBefore(StoreB);
Daniel Berlin60ead052017-01-28 01:23:13 +0000804 Updater.moveBefore(cast<MemoryDef>(MSSA.getMemoryAccess(StoreC)),
805 cast<MemoryDef>(MSSA.getMemoryAccess(StoreB)));
Bryant Wong4213d942016-12-25 23:34:07 +0000806
807 MSSA.verifyMemorySSA();
808
809 EXPECT_EQ(MSSA.getMemoryAccess(StoreB)->getDefiningAccess(),
810 MSSA.getMemoryAccess(StoreC));
811 EXPECT_EQ(MSSA.getMemoryAccess(StoreC)->getDefiningAccess(),
812 MSSA.getMemoryAccess(StoreA0));
813 EXPECT_EQ(MSSA.getMemoryAccess(StoreA2)->getDefiningAccess(),
814 MSSA.getMemoryAccess(StoreA1));
815 EXPECT_EQ(Walker.getClobberingMemoryAccess(LoadB),
816 MSSA.getMemoryAccess(StoreB));
817 EXPECT_EQ(Walker.getClobberingMemoryAccess(LoadC),
818 MSSA.getMemoryAccess(StoreC));
819
820 // exercise block numbering
821 EXPECT_TRUE(MSSA.locallyDominates(MSSA.getMemoryAccess(StoreC),
822 MSSA.getMemoryAccess(StoreB)));
823 EXPECT_TRUE(MSSA.locallyDominates(MSSA.getMemoryAccess(StoreA1),
824 MSSA.getMemoryAccess(StoreA2)));
825}
Daniel Berlin60ead052017-01-28 01:23:13 +0000826
827TEST_F(MemorySSATest, Irreducible) {
828 // Create the equivalent of
829 // x = something
830 // if (...)
831 // goto second_loop_entry
832 // while (...) {
833 // second_loop_entry:
834 // }
835 // use(x)
836
837 SmallVector<PHINode *, 8> Inserted;
838 IRBuilder<> B(C);
839 F = Function::Create(
840 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
841 GlobalValue::ExternalLinkage, "F", &M);
842
843 // Make blocks
844 BasicBlock *IfBB = BasicBlock::Create(C, "if", F);
845 BasicBlock *LoopStartBB = BasicBlock::Create(C, "loopstart", F);
846 BasicBlock *LoopMainBB = BasicBlock::Create(C, "loopmain", F);
847 BasicBlock *AfterLoopBB = BasicBlock::Create(C, "afterloop", F);
848 B.SetInsertPoint(IfBB);
849 B.CreateCondBr(B.getTrue(), LoopMainBB, LoopStartBB);
850 B.SetInsertPoint(LoopStartBB);
851 B.CreateBr(LoopMainBB);
852 B.SetInsertPoint(LoopMainBB);
853 B.CreateCondBr(B.getTrue(), LoopStartBB, AfterLoopBB);
854 B.SetInsertPoint(AfterLoopBB);
855 Argument *FirstArg = &*F->arg_begin();
856 setupAnalyses();
857 MemorySSA &MSSA = *Analyses->MSSA;
858 MemorySSAUpdater Updater(&MSSA);
859 // Create the load memory acccess
860 LoadInst *LoadInst = B.CreateLoad(FirstArg);
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000861 MemoryUse *LoadAccess = cast<MemoryUse>(Updater.createMemoryAccessInBB(
Daniel Berlin60ead052017-01-28 01:23:13 +0000862 LoadInst, nullptr, AfterLoopBB, MemorySSA::Beginning));
863 Updater.insertUse(LoadAccess);
864 MSSA.verifyMemorySSA();
865}