blob: 7d9cefa9dc34f8dc4429e65a7d5a12ff2735eb5f [file] [log] [blame]
Daniel Berlin83fc77b2016-03-01 18:46:54 +00001//===- MemorySSA.cpp - Unit tests for MemorySSA ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Daniel Berlin83fc77b2016-03-01 18:46:54 +00009#include "llvm/Transforms/Utils/MemorySSA.h"
10#include "llvm/Analysis/AliasAnalysis.h"
11#include "llvm/Analysis/BasicAliasAnalysis.h"
12#include "llvm/IR/BasicBlock.h"
Daniel Berlin14300262016-06-21 18:39:20 +000013#include "llvm/IR/DataLayout.h"
Daniel Berlin83fc77b2016-03-01 18:46:54 +000014#include "llvm/IR/Dominators.h"
15#include "llvm/IR/IRBuilder.h"
16#include "llvm/IR/Instructions.h"
17#include "llvm/IR/LLVMContext.h"
18#include "gtest/gtest.h"
19
20using namespace llvm;
21
George Burgess IV1b1fef32016-04-29 18:42:55 +000022const static char DLString[] = "e-i64:64-f80:128-n8:16:32:64-S128";
Daniel Berlin83fc77b2016-03-01 18:46:54 +000023
George Burgess IV1b1fef32016-04-29 18:42:55 +000024/// There's a lot of common setup between these tests. This fixture helps reduce
25/// that. Tests should mock up a function, store it in F, and then call
26/// setupAnalyses().
27class MemorySSATest : public testing::Test {
28protected:
29 // N.B. Many of these members depend on each other (e.g. the Module depends on
30 // the Context, etc.). So, order matters here (and in TestAnalyses).
31 LLVMContext C;
32 Module M;
33 IRBuilder<> B;
34 DataLayout DL;
35 TargetLibraryInfoImpl TLII;
36 TargetLibraryInfo TLI;
37 Function *F;
38
39 // Things that we need to build after the function is created.
40 struct TestAnalyses {
41 DominatorTree DT;
Daniel Jasperaec2fa32016-12-19 08:22:17 +000042 AssumptionCache AC;
George Burgess IV1b1fef32016-04-29 18:42:55 +000043 AAResults AA;
44 BasicAAResult BAA;
George Burgess IV80ed8632016-10-03 23:12:35 +000045 // We need to defer MSSA construction until AA is *entirely* set up, which
46 // requires calling addAAResult. Hence, we just use a pointer here.
47 std::unique_ptr<MemorySSA> MSSA;
Geoff Berryb96d3b22016-06-01 21:30:40 +000048 MemorySSAWalker *Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +000049
50 TestAnalyses(MemorySSATest &Test)
Daniel Jasperaec2fa32016-12-19 08:22:17 +000051 : DT(*Test.F), AC(*Test.F), AA(Test.TLI),
52 BAA(Test.DL, Test.TLI, AC, &DT) {
George Burgess IV1b1fef32016-04-29 18:42:55 +000053 AA.addAAResult(BAA);
George Burgess IV80ed8632016-10-03 23:12:35 +000054 MSSA = make_unique<MemorySSA>(*Test.F, &AA, &DT);
55 Walker = MSSA->getWalker();
George Burgess IV1b1fef32016-04-29 18:42:55 +000056 }
57 };
58
59 std::unique_ptr<TestAnalyses> Analyses;
60
61 void setupAnalyses() {
62 assert(F);
63 Analyses.reset(new TestAnalyses(*this));
64 }
65
66public:
67 MemorySSATest()
68 : M("MemorySSATest", C), B(C), DL(DLString), TLI(TLII), F(nullptr) {}
69};
70
Daniel Berlinf72ac492016-09-26 17:44:31 +000071TEST_F(MemorySSATest, CreateALoad) {
Daniel Berlin14300262016-06-21 18:39:20 +000072 // We create a diamond where there is a store on one side, and then after
Daniel Berlincdda3ce2016-07-31 21:08:10 +000073 // building MemorySSA, create a load after the merge point, and use it to test
Daniel Berlinf72ac492016-09-26 17:44:31 +000074 // updating by creating an access for the load.
Daniel Berlin14300262016-06-21 18:39:20 +000075 F = Function::Create(
76 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
77 GlobalValue::ExternalLinkage, "F", &M);
78 BasicBlock *Entry(BasicBlock::Create(C, "", F));
79 BasicBlock *Left(BasicBlock::Create(C, "", F));
80 BasicBlock *Right(BasicBlock::Create(C, "", F));
81 BasicBlock *Merge(BasicBlock::Create(C, "", F));
82 B.SetInsertPoint(Entry);
83 B.CreateCondBr(B.getTrue(), Left, Right);
84 B.SetInsertPoint(Left);
85 Argument *PointerArg = &*F->arg_begin();
Daniel Berlinf72ac492016-09-26 17:44:31 +000086 B.CreateStore(B.getInt8(16), PointerArg);
Daniel Berlin14300262016-06-21 18:39:20 +000087 BranchInst::Create(Merge, Left);
88 BranchInst::Create(Merge, Right);
89
90 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +000091 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlin14300262016-06-21 18:39:20 +000092 // Add the load
93 B.SetInsertPoint(Merge);
94 LoadInst *LoadInst = B.CreateLoad(PointerArg);
Daniel Berlin14300262016-06-21 18:39:20 +000095
Daniel Berlinf72ac492016-09-26 17:44:31 +000096 // MemoryPHI should already exist.
97 MemoryPhi *MP = MSSA.getMemoryAccess(Merge);
98 EXPECT_NE(MP, nullptr);
Daniel Berlin14300262016-06-21 18:39:20 +000099
100 // Create the load memory acccess
101 MemoryUse *LoadAccess = cast<MemoryUse>(
102 MSSA.createMemoryAccessInBB(LoadInst, MP, Merge, MemorySSA::Beginning));
103 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
104 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
105 MSSA.verifyMemorySSA();
106}
107
Daniel Berlin5130cc82016-07-31 21:08:20 +0000108TEST_F(MemorySSATest, MoveAStore) {
109 // We create a diamond where there is a in the entry, a store on one side, and
110 // a load at the end. After building MemorySSA, we test updating by moving
111 // the store from the side block to the entry block.
112 F = Function::Create(
113 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
114 GlobalValue::ExternalLinkage, "F", &M);
115 BasicBlock *Entry(BasicBlock::Create(C, "", F));
116 BasicBlock *Left(BasicBlock::Create(C, "", F));
117 BasicBlock *Right(BasicBlock::Create(C, "", F));
118 BasicBlock *Merge(BasicBlock::Create(C, "", F));
119 B.SetInsertPoint(Entry);
120 Argument *PointerArg = &*F->arg_begin();
121 StoreInst *EntryStore = B.CreateStore(B.getInt8(16), PointerArg);
122 B.CreateCondBr(B.getTrue(), Left, Right);
123 B.SetInsertPoint(Left);
124 StoreInst *SideStore = B.CreateStore(B.getInt8(16), PointerArg);
125 BranchInst::Create(Merge, Left);
126 BranchInst::Create(Merge, Right);
127 B.SetInsertPoint(Merge);
128 B.CreateLoad(PointerArg);
129 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000130 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlin5130cc82016-07-31 21:08:20 +0000131
132 // Move the store
133 SideStore->moveBefore(Entry->getTerminator());
134 MemoryAccess *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore);
135 MemoryAccess *SideStoreAccess = MSSA.getMemoryAccess(SideStore);
George Burgess IVf7672852016-08-03 19:59:11 +0000136 MemoryAccess *NewStoreAccess = MSSA.createMemoryAccessAfter(
137 SideStore, EntryStoreAccess, EntryStoreAccess);
Daniel Berlin5130cc82016-07-31 21:08:20 +0000138 EntryStoreAccess->replaceAllUsesWith(NewStoreAccess);
139 MSSA.removeMemoryAccess(SideStoreAccess);
140 MSSA.verifyMemorySSA();
141}
142
Daniel Berlin14300262016-06-21 18:39:20 +0000143TEST_F(MemorySSATest, RemoveAPhi) {
144 // We create a diamond where there is a store on one side, and then a load
145 // after the merge point. This enables us to test a bunch of different
146 // removal cases.
147 F = Function::Create(
148 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
149 GlobalValue::ExternalLinkage, "F", &M);
150 BasicBlock *Entry(BasicBlock::Create(C, "", F));
151 BasicBlock *Left(BasicBlock::Create(C, "", F));
152 BasicBlock *Right(BasicBlock::Create(C, "", F));
153 BasicBlock *Merge(BasicBlock::Create(C, "", F));
154 B.SetInsertPoint(Entry);
155 B.CreateCondBr(B.getTrue(), Left, Right);
156 B.SetInsertPoint(Left);
157 Argument *PointerArg = &*F->arg_begin();
158 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
159 BranchInst::Create(Merge, Left);
160 BranchInst::Create(Merge, Right);
161 B.SetInsertPoint(Merge);
162 LoadInst *LoadInst = B.CreateLoad(PointerArg);
163
164 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000165 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlin14300262016-06-21 18:39:20 +0000166 // Before, the load will be a use of a phi<store, liveonentry>.
167 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst));
168 MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
169 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
170 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
171 // Kill the store
172 MSSA.removeMemoryAccess(StoreAccess);
173 MemoryPhi *MP = cast<MemoryPhi>(DefiningAccess);
174 // Verify the phi ended up as liveonentry, liveonentry
175 for (auto &Op : MP->incoming_values())
176 EXPECT_TRUE(MSSA.isLiveOnEntryDef(cast<MemoryAccess>(Op.get())));
177 // Replace the phi uses with the live on entry def
178 MP->replaceAllUsesWith(MSSA.getLiveOnEntryDef());
179 // Verify the load is now defined by liveOnEntryDef
180 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
181 // Remove the PHI
182 MSSA.removeMemoryAccess(MP);
183 MSSA.verifyMemorySSA();
184}
185
George Burgess IV1b1fef32016-04-29 18:42:55 +0000186TEST_F(MemorySSATest, RemoveMemoryAccess) {
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000187 // We create a diamond where there is a store on one side, and then a load
188 // after the merge point. This enables us to test a bunch of different
189 // removal cases.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000190 F = Function::Create(
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000191 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
George Burgess IV1b1fef32016-04-29 18:42:55 +0000192 GlobalValue::ExternalLinkage, "F", &M);
Daniel Berlin64120022016-03-02 21:16:28 +0000193 BasicBlock *Entry(BasicBlock::Create(C, "", F));
194 BasicBlock *Left(BasicBlock::Create(C, "", F));
195 BasicBlock *Right(BasicBlock::Create(C, "", F));
196 BasicBlock *Merge(BasicBlock::Create(C, "", F));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000197 B.SetInsertPoint(Entry);
198 B.CreateCondBr(B.getTrue(), Left, Right);
199 B.SetInsertPoint(Left);
200 Argument *PointerArg = &*F->arg_begin();
201 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
202 BranchInst::Create(Merge, Left);
203 BranchInst::Create(Merge, Right);
204 B.SetInsertPoint(Merge);
205 LoadInst *LoadInst = B.CreateLoad(PointerArg);
206
George Burgess IV1b1fef32016-04-29 18:42:55 +0000207 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000208 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000209 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000210
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000211 // Before, the load will be a use of a phi<store, liveonentry>. It should be
212 // the same after.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000213 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst));
214 MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000215 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
216 EXPECT_TRUE(isa<MemoryPhi>(DefiningAccess));
217 // The load is currently clobbered by one of the phi arguments, so the walker
218 // should determine the clobbering access as the phi.
219 EXPECT_EQ(DefiningAccess, Walker->getClobberingMemoryAccess(LoadInst));
George Burgess IV1b1fef32016-04-29 18:42:55 +0000220 MSSA.removeMemoryAccess(StoreAccess);
221 MSSA.verifyMemorySSA();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000222 // After the removeaccess, let's see if we got the right accesses
223 // The load should still point to the phi ...
224 EXPECT_EQ(DefiningAccess, LoadAccess->getDefiningAccess());
225 // but we should now get live on entry for the clobbering definition of the
226 // load, since it will walk past the phi node since every argument is the
227 // same.
Daniel Berlincd2deac2016-10-20 20:13:45 +0000228 // XXX: This currently requires either removing the phi or resetting optimized
229 // on the load
230
231 EXPECT_FALSE(
232 MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst)));
233 // If we reset optimized, we get live on entry.
234 LoadAccess->resetOptimized();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000235 EXPECT_TRUE(
George Burgess IV1b1fef32016-04-29 18:42:55 +0000236 MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst)));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000237 // The phi should now be a two entry phi with two live on entry defs.
238 for (const auto &Op : DefiningAccess->operands()) {
239 MemoryAccess *Operand = cast<MemoryAccess>(&*Op);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000240 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Operand));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000241 }
242
243 // Now we try to remove the single valued phi
George Burgess IV1b1fef32016-04-29 18:42:55 +0000244 MSSA.removeMemoryAccess(DefiningAccess);
245 MSSA.verifyMemorySSA();
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000246 // Now the load should be a load of live on entry.
George Burgess IV1b1fef32016-04-29 18:42:55 +0000247 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
248}
249
250// We had a bug with caching where the walker would report MemoryDef#3's clobber
251// (below) was MemoryDef#1.
252//
253// define void @F(i8*) {
254// %A = alloca i8, i8 1
255// ; 1 = MemoryDef(liveOnEntry)
256// store i8 0, i8* %A
257// ; 2 = MemoryDef(1)
258// store i8 1, i8* %A
259// ; 3 = MemoryDef(2)
260// store i8 2, i8* %A
261// }
262TEST_F(MemorySSATest, TestTripleStore) {
Daniel Berlin14300262016-06-21 18:39:20 +0000263 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
264 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000265 B.SetInsertPoint(BasicBlock::Create(C, "", F));
266 Type *Int8 = Type::getInt8Ty(C);
267 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
268 StoreInst *S1 = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
269 StoreInst *S2 = B.CreateStore(ConstantInt::get(Int8, 1), Alloca);
270 StoreInst *S3 = B.CreateStore(ConstantInt::get(Int8, 2), Alloca);
271
272 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000273 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000274 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000275
276 unsigned I = 0;
277 for (StoreInst *V : {S1, S2, S3}) {
278 // Everything should be clobbered by its defining access
George Burgess IV66837ab2016-11-01 21:17:46 +0000279 MemoryAccess *DefiningAccess = MSSA.getMemoryAccess(V)->getDefiningAccess();
George Burgess IV1b1fef32016-04-29 18:42:55 +0000280 MemoryAccess *WalkerClobber = Walker->getClobberingMemoryAccess(V);
281 EXPECT_EQ(DefiningAccess, WalkerClobber)
282 << "Store " << I << " doesn't have the correct clobbering access";
283 // EXPECT_EQ expands such that if we increment I above, it won't get
284 // incremented except when we try to print the error message.
285 ++I;
286 }
287}
288
289// ...And fixing the above bug made it obvious that, when walking, MemorySSA's
290// walker was caching the initial node it walked. This was fine (albeit
291// mostly redundant) unless the initial node being walked is a clobber for the
292// query. In that case, we'd cache that the node clobbered itself.
293TEST_F(MemorySSATest, TestStoreAndLoad) {
Daniel Berlin14300262016-06-21 18:39:20 +0000294 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
295 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000296 B.SetInsertPoint(BasicBlock::Create(C, "", F));
297 Type *Int8 = Type::getInt8Ty(C);
298 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
299 Instruction *SI = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
300 Instruction *LI = B.CreateLoad(Alloca);
301
302 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000303 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000304 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000305
306 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LI);
307 EXPECT_EQ(LoadClobber, MSSA.getMemoryAccess(SI));
308 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(SI)));
309}
310
311// Another bug (related to the above two fixes): It was noted that, given the
312// following code:
313// ; 1 = MemoryDef(liveOnEntry)
314// store i8 0, i8* %1
315//
316// ...A query to getClobberingMemoryAccess(MemoryAccess*, MemoryLocation) would
317// hand back the store (correctly). A later call to
318// getClobberingMemoryAccess(const Instruction*) would also hand back the store
319// (incorrectly; it should return liveOnEntry).
320//
321// This test checks that repeated calls to either function returns what they're
322// meant to.
323TEST_F(MemorySSATest, TestStoreDoubleQuery) {
Daniel Berlin14300262016-06-21 18:39:20 +0000324 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
325 GlobalValue::ExternalLinkage, "F", &M);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000326 B.SetInsertPoint(BasicBlock::Create(C, "", F));
327 Type *Int8 = Type::getInt8Ty(C);
328 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
329 StoreInst *SI = B.CreateStore(ConstantInt::get(Int8, 0), Alloca);
330
331 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000332 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IVd8cdc362016-06-20 19:13:07 +0000333 MemorySSAWalker *Walker = Analyses->Walker;
George Burgess IV1b1fef32016-04-29 18:42:55 +0000334
335 MemoryAccess *StoreAccess = MSSA.getMemoryAccess(SI);
336 MemoryLocation StoreLoc = MemoryLocation::get(SI);
Daniel Berlin14300262016-06-21 18:39:20 +0000337 MemoryAccess *Clobber =
338 Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
George Burgess IV1b1fef32016-04-29 18:42:55 +0000339 MemoryAccess *LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
340
341 EXPECT_EQ(Clobber, StoreAccess);
342 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LiveOnEntry));
343
344 // Try again (with entries in the cache already) for good measure...
345 Clobber = Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
346 LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
347 EXPECT_EQ(Clobber, StoreAccess);
348 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LiveOnEntry));
Daniel Berlin83fc77b2016-03-01 18:46:54 +0000349}
George Burgess IV14633b52016-08-03 01:22:19 +0000350
351// Bug: During phi optimization, the walker wouldn't cache to the proper result
352// in the farthest-walked BB.
353//
354// Specifically, it would assume that whatever we walked to was a clobber.
355// "Whatever we walked to" isn't a clobber if we hit a cache entry.
356//
357// ...So, we need a test case that looks like:
358// A
359// / \
360// B |
361// \ /
362// C
363//
364// Where, when we try to optimize a thing in 'C', a blocker is found in 'B'.
365// The walk must determine that the blocker exists by using cache entries *while
366// walking* 'B'.
367TEST_F(MemorySSATest, PartialWalkerCacheWithPhis) {
368 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
369 GlobalValue::ExternalLinkage, "F", &M);
370 B.SetInsertPoint(BasicBlock::Create(C, "A", F));
371 Type *Int8 = Type::getInt8Ty(C);
372 Constant *One = ConstantInt::get(Int8, 1);
373 Constant *Zero = ConstantInt::get(Int8, 0);
374 Value *AllocA = B.CreateAlloca(Int8, One, "a");
375 Value *AllocB = B.CreateAlloca(Int8, One, "b");
376 BasicBlock *IfThen = BasicBlock::Create(C, "B", F);
377 BasicBlock *IfEnd = BasicBlock::Create(C, "C", F);
378
379 B.CreateCondBr(UndefValue::get(Type::getInt1Ty(C)), IfThen, IfEnd);
380
381 B.SetInsertPoint(IfThen);
382 Instruction *FirstStore = B.CreateStore(Zero, AllocA);
383 B.CreateStore(Zero, AllocB);
384 Instruction *ALoad0 = B.CreateLoad(AllocA, "");
385 Instruction *BStore = B.CreateStore(Zero, AllocB);
386 // Due to use optimization/etc. we make a store to A, which is removed after
387 // we build MSSA. This helps keep the test case simple-ish.
388 Instruction *KillStore = B.CreateStore(Zero, AllocA);
389 Instruction *ALoad = B.CreateLoad(AllocA, "");
390 B.CreateBr(IfEnd);
391
392 B.SetInsertPoint(IfEnd);
393 Instruction *BelowPhi = B.CreateStore(Zero, AllocA);
394
395 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000396 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IV14633b52016-08-03 01:22:19 +0000397 MemorySSAWalker *Walker = Analyses->Walker;
398
399 // Kill `KillStore`; it exists solely so that the load after it won't be
400 // optimized to FirstStore.
401 MSSA.removeMemoryAccess(MSSA.getMemoryAccess(KillStore));
402 KillStore->eraseFromParent();
403 auto *ALoadMA = cast<MemoryUse>(MSSA.getMemoryAccess(ALoad));
404 EXPECT_EQ(ALoadMA->getDefiningAccess(), MSSA.getMemoryAccess(BStore));
405
406 // Populate the cache for the store to AllocB directly after FirstStore. It
407 // should point to something in block B (so something in D can't be optimized
408 // to it).
409 MemoryAccess *Load0Clobber = Walker->getClobberingMemoryAccess(ALoad0);
410 EXPECT_EQ(MSSA.getMemoryAccess(FirstStore), Load0Clobber);
411
412 // If the bug exists, this will introduce a bad cache entry for %a on BStore.
413 // It will point to the store to %b after FirstStore. This only happens during
414 // phi optimization.
415 MemoryAccess *BottomClobber = Walker->getClobberingMemoryAccess(BelowPhi);
416 MemoryAccess *Phi = MSSA.getMemoryAccess(IfEnd);
417 EXPECT_EQ(BottomClobber, Phi);
418
419 // This query will first check the cache for {%a, BStore}. It should point to
420 // FirstStore, not to the store after FirstStore.
421 MemoryAccess *UseClobber = Walker->getClobberingMemoryAccess(ALoad);
422 EXPECT_EQ(UseClobber, MSSA.getMemoryAccess(FirstStore));
423}
George Burgess IV024f3d22016-08-03 19:57:02 +0000424
425// Test that our walker properly handles loads with the invariant group
426// attribute. It's a bit hacky, since we add the invariant attribute *after*
427// building MSSA. Otherwise, the use optimizer will optimize it for us, which
428// isn't what we want.
429// FIXME: It may be easier/cleaner to just add an 'optimize uses?' flag to MSSA.
430TEST_F(MemorySSATest, WalkerInvariantLoadOpt) {
431 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
432 GlobalValue::ExternalLinkage, "F", &M);
433 B.SetInsertPoint(BasicBlock::Create(C, "", F));
434 Type *Int8 = Type::getInt8Ty(C);
435 Constant *One = ConstantInt::get(Int8, 1);
436 Value *AllocA = B.CreateAlloca(Int8, One, "");
437
438 Instruction *Store = B.CreateStore(One, AllocA);
439 Instruction *Load = B.CreateLoad(AllocA);
440
441 setupAnalyses();
George Burgess IV80ed8632016-10-03 23:12:35 +0000442 MemorySSA &MSSA = *Analyses->MSSA;
George Burgess IV024f3d22016-08-03 19:57:02 +0000443 MemorySSAWalker *Walker = Analyses->Walker;
444
445 auto *LoadMA = cast<MemoryUse>(MSSA.getMemoryAccess(Load));
446 auto *StoreMA = cast<MemoryDef>(MSSA.getMemoryAccess(Store));
447 EXPECT_EQ(LoadMA->getDefiningAccess(), StoreMA);
448
449 // ...At the time of writing, no cache should exist for LoadMA. Be a bit
450 // flexible to future changes.
451 Walker->invalidateInfo(LoadMA);
452 Load->setMetadata(LLVMContext::MD_invariant_load, MDNode::get(C, {}));
453
454 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LoadMA);
455 EXPECT_EQ(LoadClobber, MSSA.getLiveOnEntryDef());
456}
George Burgess IV80ed8632016-10-03 23:12:35 +0000457
Daniel Berlincd2deac2016-10-20 20:13:45 +0000458// Test loads get reoptimized properly by the walker.
459TEST_F(MemorySSATest, WalkerReopt) {
George Burgess IV80ed8632016-10-03 23:12:35 +0000460 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
461 GlobalValue::ExternalLinkage, "F", &M);
462 B.SetInsertPoint(BasicBlock::Create(C, "", F));
George Burgess IV80ed8632016-10-03 23:12:35 +0000463 Type *Int8 = Type::getInt8Ty(C);
Daniel Berlincd2deac2016-10-20 20:13:45 +0000464 Value *AllocaA = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
465 Instruction *SIA = B.CreateStore(ConstantInt::get(Int8, 0), AllocaA);
466 Value *AllocaB = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
467 Instruction *SIB = B.CreateStore(ConstantInt::get(Int8, 0), AllocaB);
468 Instruction *LIA = B.CreateLoad(AllocaA);
George Burgess IV80ed8632016-10-03 23:12:35 +0000469
470 setupAnalyses();
471 MemorySSA &MSSA = *Analyses->MSSA;
Daniel Berlincd2deac2016-10-20 20:13:45 +0000472 MemorySSAWalker *Walker = Analyses->Walker;
473
474 MemoryAccess *LoadClobber = Walker->getClobberingMemoryAccess(LIA);
475 MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LIA));
476 EXPECT_EQ(LoadClobber, MSSA.getMemoryAccess(SIA));
477 EXPECT_TRUE(MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(SIA)));
478 MSSA.removeMemoryAccess(LoadAccess);
479
480 // Create the load memory access pointing to an unoptimized place.
481 MemoryUse *NewLoadAccess = cast<MemoryUse>(MSSA.createMemoryAccessInBB(
482 LIA, MSSA.getMemoryAccess(SIB), LIA->getParent(), MemorySSA::End));
483 // This should it cause it to be optimized
484 EXPECT_EQ(Walker->getClobberingMemoryAccess(NewLoadAccess), LoadClobber);
485 EXPECT_EQ(NewLoadAccess->getDefiningAccess(), LoadClobber);
George Burgess IV80ed8632016-10-03 23:12:35 +0000486}
Bryant Wong4213d942016-12-25 23:34:07 +0000487
Daniel Berlind602e042017-01-25 20:56:19 +0000488#if 0
Bryant Wong4213d942016-12-25 23:34:07 +0000489// Test out MemorySSA::spliceMemoryAccessAbove.
490TEST_F(MemorySSATest, SpliceAboveMemoryDef) {
491 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
492 GlobalValue::ExternalLinkage, "F", &M);
493 B.SetInsertPoint(BasicBlock::Create(C, "", F));
494
495 Type *Int8 = Type::getInt8Ty(C);
496 Value *A = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
497 Value *B_ = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "B");
498 Value *C = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "C");
499
500 StoreInst *StoreA0 = B.CreateStore(ConstantInt::get(Int8, 0), A);
501 StoreInst *StoreB = B.CreateStore(ConstantInt::get(Int8, 0), B_);
502 LoadInst *LoadB = B.CreateLoad(B_);
503 StoreInst *StoreA1 = B.CreateStore(ConstantInt::get(Int8, 4), A);
504 // splice this above StoreB
505 StoreInst *StoreC = B.CreateStore(ConstantInt::get(Int8, 4), C);
506 StoreInst *StoreA2 = B.CreateStore(ConstantInt::get(Int8, 4), A);
507 LoadInst *LoadC = B.CreateLoad(C);
508
509 setupAnalyses();
510 MemorySSA &MSSA = *Analyses->MSSA;
511 MemorySSAWalker &Walker = *Analyses->Walker;
512
513 StoreC->moveBefore(StoreB);
514 MSSA.spliceMemoryAccessAbove(cast<MemoryDef>(MSSA.getMemoryAccess(StoreB)),
515 MSSA.getMemoryAccess(StoreC));
516
517 MSSA.verifyMemorySSA();
518
519 EXPECT_EQ(MSSA.getMemoryAccess(StoreB)->getDefiningAccess(),
520 MSSA.getMemoryAccess(StoreC));
521 EXPECT_EQ(MSSA.getMemoryAccess(StoreC)->getDefiningAccess(),
522 MSSA.getMemoryAccess(StoreA0));
523 EXPECT_EQ(MSSA.getMemoryAccess(StoreA2)->getDefiningAccess(),
524 MSSA.getMemoryAccess(StoreA1));
525 EXPECT_EQ(Walker.getClobberingMemoryAccess(LoadB),
526 MSSA.getMemoryAccess(StoreB));
527 EXPECT_EQ(Walker.getClobberingMemoryAccess(LoadC),
528 MSSA.getMemoryAccess(StoreC));
529
530 // exercise block numbering
531 EXPECT_TRUE(MSSA.locallyDominates(MSSA.getMemoryAccess(StoreC),
532 MSSA.getMemoryAccess(StoreB)));
533 EXPECT_TRUE(MSSA.locallyDominates(MSSA.getMemoryAccess(StoreA1),
534 MSSA.getMemoryAccess(StoreA2)));
535}
Daniel Berlind602e042017-01-25 20:56:19 +0000536#endif