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