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" |
| 18 | #include "gtest/gtest.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 22 | 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] | 23 | |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 24 | /// 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(). |
| 27 | class MemorySSATest : public testing::Test { |
| 28 | protected: |
| 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 Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 42 | AssumptionCache AC; |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 43 | AAResults AA; |
| 44 | BasicAAResult BAA; |
George Burgess IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 45 | // 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 Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 48 | MemorySSAWalker *Walker; |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 49 | |
| 50 | TestAnalyses(MemorySSATest &Test) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 51 | : DT(*Test.F), AC(*Test.F), AA(Test.TLI), |
| 52 | BAA(Test.DL, Test.TLI, AC, &DT) { |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 53 | AA.addAAResult(BAA); |
George Burgess IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 54 | MSSA = make_unique<MemorySSA>(*Test.F, &AA, &DT); |
| 55 | Walker = MSSA->getWalker(); |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 56 | } |
| 57 | }; |
| 58 | |
| 59 | std::unique_ptr<TestAnalyses> Analyses; |
| 60 | |
| 61 | void setupAnalyses() { |
| 62 | assert(F); |
| 63 | Analyses.reset(new TestAnalyses(*this)); |
| 64 | } |
| 65 | |
| 66 | public: |
| 67 | MemorySSATest() |
| 68 | : M("MemorySSATest", C), B(C), DL(DLString), TLI(TLII), F(nullptr) {} |
| 69 | }; |
| 70 | |
Daniel Berlin | f72ac49 | 2016-09-26 17:44:31 +0000 | [diff] [blame] | 71 | TEST_F(MemorySSATest, CreateALoad) { |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 72 | // 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] | 73 | // 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] | 74 | // updating by creating an access for the load. |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 75 | 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 Berlin | f72ac49 | 2016-09-26 17:44:31 +0000 | [diff] [blame] | 86 | B.CreateStore(B.getInt8(16), PointerArg); |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 87 | BranchInst::Create(Merge, Left); |
| 88 | BranchInst::Create(Merge, Right); |
| 89 | |
| 90 | setupAnalyses(); |
George Burgess IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 91 | MemorySSA &MSSA = *Analyses->MSSA; |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 92 | // Add the load |
| 93 | B.SetInsertPoint(Merge); |
| 94 | LoadInst *LoadInst = B.CreateLoad(PointerArg); |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 95 | |
Daniel Berlin | f72ac49 | 2016-09-26 17:44:31 +0000 | [diff] [blame] | 96 | // MemoryPHI should already exist. |
| 97 | MemoryPhi *MP = MSSA.getMemoryAccess(Merge); |
| 98 | EXPECT_NE(MP, nullptr); |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 99 | |
| 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 Berlin | 5130cc8 | 2016-07-31 21:08:20 +0000 | [diff] [blame] | 108 | TEST_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 IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 130 | MemorySSA &MSSA = *Analyses->MSSA; |
Daniel Berlin | 5130cc8 | 2016-07-31 21:08:20 +0000 | [diff] [blame] | 131 | |
| 132 | // Move the store |
| 133 | SideStore->moveBefore(Entry->getTerminator()); |
| 134 | MemoryAccess *EntryStoreAccess = MSSA.getMemoryAccess(EntryStore); |
| 135 | MemoryAccess *SideStoreAccess = MSSA.getMemoryAccess(SideStore); |
George Burgess IV | f767285 | 2016-08-03 19:59:11 +0000 | [diff] [blame] | 136 | MemoryAccess *NewStoreAccess = MSSA.createMemoryAccessAfter( |
| 137 | SideStore, EntryStoreAccess, EntryStoreAccess); |
Daniel Berlin | 5130cc8 | 2016-07-31 21:08:20 +0000 | [diff] [blame] | 138 | EntryStoreAccess->replaceAllUsesWith(NewStoreAccess); |
| 139 | MSSA.removeMemoryAccess(SideStoreAccess); |
| 140 | MSSA.verifyMemorySSA(); |
| 141 | } |
| 142 | |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 143 | TEST_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 IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 165 | MemorySSA &MSSA = *Analyses->MSSA; |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 166 | // 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 IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 186 | TEST_F(MemorySSATest, RemoveMemoryAccess) { |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 187 | // 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 IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 190 | F = Function::Create( |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 191 | FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false), |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 192 | GlobalValue::ExternalLinkage, "F", &M); |
Daniel Berlin | 6412002 | 2016-03-02 21:16:28 +0000 | [diff] [blame] | 193 | 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 Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 197 | 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 IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 207 | setupAnalyses(); |
George Burgess IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 208 | MemorySSA &MSSA = *Analyses->MSSA; |
George Burgess IV | d8cdc36 | 2016-06-20 19:13:07 +0000 | [diff] [blame] | 209 | MemorySSAWalker *Walker = Analyses->Walker; |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 210 | |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 211 | // Before, the load will be a use of a phi<store, liveonentry>. It should be |
| 212 | // the same after. |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 213 | MemoryUse *LoadAccess = cast<MemoryUse>(MSSA.getMemoryAccess(LoadInst)); |
| 214 | MemoryDef *StoreAccess = cast<MemoryDef>(MSSA.getMemoryAccess(StoreInst)); |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 215 | 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 IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 220 | MSSA.removeMemoryAccess(StoreAccess); |
| 221 | MSSA.verifyMemorySSA(); |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 222 | // 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 Berlin | cd2deac | 2016-10-20 20:13:45 +0000 | [diff] [blame] | 228 | // 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 Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 235 | EXPECT_TRUE( |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 236 | MSSA.isLiveOnEntryDef(Walker->getClobberingMemoryAccess(LoadInst))); |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 237 | // 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 IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 240 | EXPECT_TRUE(MSSA.isLiveOnEntryDef(Operand)); |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // Now we try to remove the single valued phi |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 244 | MSSA.removeMemoryAccess(DefiningAccess); |
| 245 | MSSA.verifyMemorySSA(); |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 246 | // Now the load should be a load of live on entry. |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 247 | 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 | // } |
| 262 | TEST_F(MemorySSATest, TestTripleStore) { |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 263 | F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false), |
| 264 | GlobalValue::ExternalLinkage, "F", &M); |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 265 | 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 IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 273 | MemorySSA &MSSA = *Analyses->MSSA; |
George Burgess IV | d8cdc36 | 2016-06-20 19:13:07 +0000 | [diff] [blame] | 274 | MemorySSAWalker *Walker = Analyses->Walker; |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 275 | |
| 276 | unsigned I = 0; |
| 277 | for (StoreInst *V : {S1, S2, S3}) { |
| 278 | // Everything should be clobbered by its defining access |
George Burgess IV | 66837ab | 2016-11-01 21:17:46 +0000 | [diff] [blame] | 279 | MemoryAccess *DefiningAccess = MSSA.getMemoryAccess(V)->getDefiningAccess(); |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 280 | 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. |
| 293 | TEST_F(MemorySSATest, TestStoreAndLoad) { |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 294 | F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false), |
| 295 | GlobalValue::ExternalLinkage, "F", &M); |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 296 | 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 IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 303 | MemorySSA &MSSA = *Analyses->MSSA; |
George Burgess IV | d8cdc36 | 2016-06-20 19:13:07 +0000 | [diff] [blame] | 304 | MemorySSAWalker *Walker = Analyses->Walker; |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 305 | |
| 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. |
| 323 | TEST_F(MemorySSATest, TestStoreDoubleQuery) { |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 324 | F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false), |
| 325 | GlobalValue::ExternalLinkage, "F", &M); |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 326 | 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 IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 332 | MemorySSA &MSSA = *Analyses->MSSA; |
George Burgess IV | d8cdc36 | 2016-06-20 19:13:07 +0000 | [diff] [blame] | 333 | MemorySSAWalker *Walker = Analyses->Walker; |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 334 | |
| 335 | MemoryAccess *StoreAccess = MSSA.getMemoryAccess(SI); |
| 336 | MemoryLocation StoreLoc = MemoryLocation::get(SI); |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 337 | MemoryAccess *Clobber = |
| 338 | Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc); |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 339 | 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 Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 349 | } |
George Burgess IV | 14633b5 | 2016-08-03 01:22:19 +0000 | [diff] [blame] | 350 | |
| 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'. |
| 367 | TEST_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 IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 396 | MemorySSA &MSSA = *Analyses->MSSA; |
George Burgess IV | 14633b5 | 2016-08-03 01:22:19 +0000 | [diff] [blame] | 397 | 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 IV | 024f3d2 | 2016-08-03 19:57:02 +0000 | [diff] [blame] | 424 | |
| 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. |
| 430 | TEST_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 IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 442 | MemorySSA &MSSA = *Analyses->MSSA; |
George Burgess IV | 024f3d2 | 2016-08-03 19:57:02 +0000 | [diff] [blame] | 443 | 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 IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 457 | |
Daniel Berlin | cd2deac | 2016-10-20 20:13:45 +0000 | [diff] [blame] | 458 | // Test loads get reoptimized properly by the walker. |
| 459 | TEST_F(MemorySSATest, WalkerReopt) { |
George Burgess IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 460 | F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false), |
| 461 | GlobalValue::ExternalLinkage, "F", &M); |
| 462 | B.SetInsertPoint(BasicBlock::Create(C, "", F)); |
George Burgess IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 463 | Type *Int8 = Type::getInt8Ty(C); |
Daniel Berlin | cd2deac | 2016-10-20 20:13:45 +0000 | [diff] [blame] | 464 | 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 IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 469 | |
| 470 | setupAnalyses(); |
| 471 | MemorySSA &MSSA = *Analyses->MSSA; |
Daniel Berlin | cd2deac | 2016-10-20 20:13:45 +0000 | [diff] [blame] | 472 | 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 IV | 80ed863 | 2016-10-03 23:12:35 +0000 | [diff] [blame] | 486 | } |
Bryant Wong | 4213d94 | 2016-12-25 23:34:07 +0000 | [diff] [blame] | 487 | |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 488 | #if 0 |
Bryant Wong | 4213d94 | 2016-12-25 23:34:07 +0000 | [diff] [blame] | 489 | // Test out MemorySSA::spliceMemoryAccessAbove. |
| 490 | TEST_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 Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 536 | #endif |