llvm.org GIT mirror llvm / 11e2958
Add MemoryAccess creation and PHI creation APIs to MemorySSA Reviewers: george.burgess.iv, gberry, hfinkel Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D21463 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273295 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Berlin 4 years ago
3 changed file(s) with 238 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
397397 MemoryAccess *getIncomingValue(unsigned I) const { return getOperand(I); }
398398 void setIncomingValue(unsigned I, MemoryAccess *V) {
399399 assert(V && "PHI node got a null value!");
400 assert(getType() == V->getType() &&
401 "All operands to PHI node must be the same type as the PHI node!");
402400 setOperand(I, V);
403401 }
404402 static unsigned getOperandNumForIncomingValue(unsigned I) { return I; }
535533 return It == PerBlockAccesses.end() ? nullptr : It->second.get();
536534 }
537535
536 /// \brief Create an empty MemoryPhi in MemorySSA
537 MemoryPhi *createMemoryPhi(BasicBlock *BB);
538
539 enum InsertionPlace { Beginning, End };
540
541 /// \brief Create a MemoryAccess in MemorySSA at a specified point in a block,
542 /// with a specified clobbering definition.
543 ///
544 /// Returns the new MemoryAccess.
545 /// This should be called when a memory instruction is created that is being
546 /// used to replace an existing memory instruction. It will *not* create PHI
547 /// nodes, or verify the clobbering definition. The insertion place is used
548 /// solely to determine where in the memoryssa access lists the instruction
549 /// will be placed. The caller is expected to keep ordering the same as
550 /// instructions.
551 /// It will return the new MemoryAccess.
552 MemoryAccess *createMemoryAccessInBB(Instruction *I, MemoryAccess *Definition,
553 const BasicBlock *BB,
554 InsertionPlace Point);
555 /// \brief Create a MemoryAccess in MemorySSA before or after an existing
556 /// MemoryAccess.
557 ///
558 /// Returns the new MemoryAccess.
559 /// This should be called when a memory instruction is created that is being
560 /// used to replace an existing memory instruction. It will *not* create PHI
561 /// nodes, or verify the clobbering definition. The clobbering definition
562 /// must be non-null.
563 MemoryAccess *createMemoryAccessBefore(Instruction *I,
564 MemoryAccess *Definition,
565 MemoryAccess *InsertPt);
566 MemoryAccess *createMemoryAccessAfter(Instruction *I,
567 MemoryAccess *Definition,
568 MemoryAccess *InsertPt);
569
538570 /// \brief Remove a MemoryAccess from MemorySSA, including updating all
539571 /// definitions and uses.
540572 /// This should be called when a memory instruction that has a MemoryAccess
543575 /// on the MemoryAccess for that store/load.
544576 void removeMemoryAccess(MemoryAccess *);
545577
546 enum InsertionPlace { Beginning, End };
547
548578 /// \brief Given two memory accesses in the same basic block, determine
549579 /// whether MemoryAccess \p A dominates MemoryAccess \p B.
550580 bool locallyDominates(const MemoryAccess *A, const MemoryAccess *B) const;
559589 friend class MemorySSAPrinterLegacyPass;
560590 void verifyDefUses(Function &F) const;
561591 void verifyDomination(Function &F) const;
592 void verifyOrdering(Function &F) const;
562593
563594 private:
564595 void verifyUseInDefs(MemoryAccess *, MemoryAccess *) const;
570601 void markUnreachableAsLiveOnEntry(BasicBlock *BB);
571602 bool dominatesUse(const MemoryAccess *, const MemoryAccess *) const;
572603 MemoryUseOrDef *createNewAccess(Instruction *);
604 MemoryUseOrDef *createDefinedAccess(Instruction *, MemoryAccess *);
573605 MemoryAccess *findDominatingDef(BasicBlock *, enum InsertionPlace);
574606 void removeFromLookups(MemoryAccess *);
575607
576608 MemoryAccess *renameBlock(BasicBlock *, MemoryAccess *);
577609 void renamePass(DomTreeNode *, MemoryAccess *IncomingVal,
578610 SmallPtrSet &Visited);
579 AccessList *getOrCreateAccessList(BasicBlock *);
611 AccessList *getOrCreateAccessList(const BasicBlock *);
580612 AliasAnalysis *AA;
581613 DominatorTree *DT;
582614 Function &F;
225225 MA.dropAllReferences();
226226 }
227227
228 MemorySSA::AccessList *MemorySSA::getOrCreateAccessList(BasicBlock *BB) {
228 MemorySSA::AccessList *MemorySSA::getOrCreateAccessList(const BasicBlock *BB) {
229229 auto Res = PerBlockAccesses.insert(std::make_pair(BB, nullptr));
230230
231231 if (Res.second)
319319 for (auto &BB : IDFBlocks) {
320320 // Insert phi node
321321 AccessList *Accesses = getOrCreateAccessList(BB);
322 MemoryPhi *Phi = new MemoryPhi(F.getContext(), BB, NextID++);
322 MemoryPhi *Phi = new MemoryPhi(BB->getContext(), BB, NextID++);
323323 ValueToMemoryAccess.insert(std::make_pair(BB, Phi));
324324 // Phi's always are placed at the front of the block.
325325 Accesses->push_front(Phi);
355355 markUnreachableAsLiveOnEntry(&BB);
356356
357357 return Walker.get();
358 }
359
360 MemoryPhi *MemorySSA::createMemoryPhi(BasicBlock *BB) {
361 assert(!getMemoryAccess(BB) && "MemoryPhi already exists for this BB");
362 AccessList *Accesses = getOrCreateAccessList(BB);
363 MemoryPhi *Phi = new MemoryPhi(BB->getContext(), BB, NextID++);
364 ValueToMemoryAccess.insert(std::make_pair(BB, Phi));
365 // Phi's always are placed at the front of the block.
366 Accesses->push_front(Phi);
367 return Phi;
368 }
369
370 MemoryUseOrDef *MemorySSA::createDefinedAccess(Instruction *I,
371 MemoryAccess *Definition) {
372 assert(!isa(I) && "Cannot create a defined access for a PHI");
373 MemoryUseOrDef *NewAccess = createNewAccess(I);
374 assert(
375 NewAccess != nullptr &&
376 "Tried to create a memory access for a non-memory touching instruction");
377 NewAccess->setDefiningAccess(Definition);
378 return NewAccess;
379 }
380
381 MemoryAccess *MemorySSA::createMemoryAccessInBB(Instruction *I,
382 MemoryAccess *Definition,
383 const BasicBlock *BB,
384 InsertionPlace Point) {
385 MemoryUseOrDef *NewAccess = createDefinedAccess(I, Definition);
386 auto *Accesses = getOrCreateAccessList(BB);
387 if (Point == Beginning) {
388 // It goes after any phi nodes
389 auto AI = std::find_if(
390 Accesses->begin(), Accesses->end(),
391 [](const MemoryAccess &MA) { return !isa(MA); });
392
393 Accesses->insert(AI, NewAccess);
394 } else {
395 Accesses->push_back(NewAccess);
396 }
397
398 return NewAccess;
399 }
400 MemoryAccess *MemorySSA::createMemoryAccessBefore(Instruction *I,
401 MemoryAccess *Definition,
402 MemoryAccess *InsertPt) {
403 assert(I->getParent() == InsertPt->getBlock() &&
404 "New and old access must be in the same block");
405 MemoryUseOrDef *NewAccess = createDefinedAccess(I, Definition);
406 auto *Accesses = getOrCreateAccessList(InsertPt->getBlock());
407 Accesses->insert(AccessList::iterator(InsertPt), NewAccess);
408 return NewAccess;
409 }
410
411 MemoryAccess *MemorySSA::createMemoryAccessAfter(Instruction *I,
412 MemoryAccess *Definition,
413 MemoryAccess *InsertPt) {
414 assert(I->getParent() == InsertPt->getBlock() &&
415 "New and old access must be in the same block");
416 MemoryUseOrDef *NewAccess = createDefinedAccess(I, Definition);
417 auto *Accesses = getOrCreateAccessList(InsertPt->getBlock());
418 Accesses->insertAfter(AccessList::iterator(InsertPt), NewAccess);
419 return NewAccess;
358420 }
359421
360422 /// \brief Helper function to create new memory accesses
517579 void MemorySSA::verifyMemorySSA() const {
518580 verifyDefUses(F);
519581 verifyDomination(F);
582 verifyOrdering(F);
583 }
584
585 /// \brief Verify that the order and existence of MemoryAccesses matches the
586 /// order and existence of memory affecting instructions.
587 void MemorySSA::verifyOrdering(Function &F) const {
588 // Walk all the blocks, comparing what the lookups think and what the access
589 // lists think, as well as the order in the blocks vs the order in the access
590 // lists.
591 SmallVector ActualAccesses;
592 for (BasicBlock &B : F) {
593 const AccessList *AL = getBlockAccesses(&B);
594 MemoryAccess *Phi = getMemoryAccess(&B);
595 if (Phi)
596 ActualAccesses.push_back(Phi);
597 for (Instruction &I : B) {
598 MemoryAccess *MA = getMemoryAccess(&I);
599 assert((!MA || AL) && "We have memory affecting instructions "
600 "in this block but they are not in the "
601 "access list");
602 if (MA)
603 ActualAccesses.push_back(MA);
604 }
605 // Either we hit the assert, really have no accesses, or we have both
606 // accesses and an access list
607 if (!AL)
608 continue;
609 assert(AL->size() == ActualAccesses.size() &&
610 "We don't have the same number of accesses in the block as on the "
611 "access list");
612 auto ALI = AL->begin();
613 auto AAI = ActualAccesses.begin();
614 while (ALI != AL->end() && AAI != ActualAccesses.end()) {
615 assert(&*ALI == *AAI && "Not the same accesses in the same order");
616 ++ALI;
617 ++AAI;
618 }
619 ActualAccesses.clear();
620 }
520621 }
521622
522623 /// \brief Verify the domination properties of MemorySSA by checking that each
594695 void MemorySSA::verifyDefUses(Function &F) const {
595696 for (BasicBlock &B : F) {
596697 // Phi nodes are attached to basic blocks
597 if (MemoryPhi *Phi = getMemoryAccess(&B))
698 if (MemoryPhi *Phi = getMemoryAccess(&B)) {
699 assert(Phi->getNumOperands() ==
700 std::distance(pred_begin(&B), pred_end(&B)) &&
701 "Incomplete MemoryPhi Node");
598702 for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I)
599703 verifyUseInDefs(Phi->getIncomingValue(I), Phi);
704 }
600705
601706 for (Instruction &I : B) {
602707 if (MemoryAccess *MA = getMemoryAccess(&I)) {
55 // License. See LICENSE.TXT for details.
66 //
77 //===----------------------------------------------------------------------===//
8 #include "llvm/IR/DataLayout.h"
98 #include "llvm/Transforms/Utils/MemorySSA.h"
109 #include "llvm/Analysis/AliasAnalysis.h"
1110 #include "llvm/Analysis/BasicAliasAnalysis.h"
1211 #include "llvm/IR/BasicBlock.h"
12 #include "llvm/IR/DataLayout.h"
1313 #include "llvm/IR/Dominators.h"
1414 #include "llvm/IR/IRBuilder.h"
1515 #include "llvm/IR/Instructions.h"
6363 MemorySSATest()
6464 : M("MemorySSATest", C), B(C), DL(DLString), TLI(TLII), F(nullptr) {}
6565 };
66
67 TEST_F(MemorySSATest, CreateALoadAndPhi) {
68 // We create a diamond where there is a store on one side, and then after
69 // running memory ssa, create a load after the merge point, and use it to test
70 // updating by creating an access for the load and a memoryphi.
71 F = Function::Create(
72 FunctionType::get(B.getVoidTy(), {B.getInt8PtrTy()}, false),
73 GlobalValue::ExternalLinkage, "F", &M);
74 BasicBlock *Entry(BasicBlock::Create(C, "", F));
75 BasicBlock *Left(BasicBlock::Create(C, "", F));
76 BasicBlock *Right(BasicBlock::Create(C, "", F));
77 BasicBlock *Merge(BasicBlock::Create(C, "", F));
78 B.SetInsertPoint(Entry);
79 B.CreateCondBr(B.getTrue(), Left, Right);
80 B.SetInsertPoint(Left);
81 Argument *PointerArg = &*F->arg_begin();
82 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
83 BranchInst::Create(Merge, Left);
84 BranchInst::Create(Merge, Right);
85
86 setupAnalyses();
87 MemorySSA &MSSA = Analyses->MSSA;
88 // Add the load
89 B.SetInsertPoint(Merge);
90 LoadInst *LoadInst = B.CreateLoad(PointerArg);
91 // Should be no phi to start
92 EXPECT_EQ(MSSA.getMemoryAccess(Merge), nullptr);
93
94 // Create the phi
95 MemoryPhi *MP = MSSA.createMemoryPhi(Merge);
96 MemoryDef *StoreAccess = cast(MSSA.getMemoryAccess(StoreInst));
97 MP->addIncoming(StoreAccess, Left);
98 MP->addIncoming(MSSA.getLiveOnEntryDef(), Right);
99
100 // Create the load memory acccess
101 MemoryUse *LoadAccess = cast(
102 MSSA.createMemoryAccessInBB(LoadInst, MP, Merge, MemorySSA::Beginning));
103 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
104 EXPECT_TRUE(isa(DefiningAccess));
105 MSSA.verifyMemorySSA();
106 }
107
108 TEST_F(MemorySSATest, RemoveAPhi) {
109 // We create a diamond where there is a store on one side, and then a load
110 // after the merge point. This enables us to test a bunch of different
111 // removal cases.
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 B.CreateCondBr(B.getTrue(), Left, Right);
121 B.SetInsertPoint(Left);
122 Argument *PointerArg = &*F->arg_begin();
123 StoreInst *StoreInst = B.CreateStore(B.getInt8(16), PointerArg);
124 BranchInst::Create(Merge, Left);
125 BranchInst::Create(Merge, Right);
126 B.SetInsertPoint(Merge);
127 LoadInst *LoadInst = B.CreateLoad(PointerArg);
128
129 setupAnalyses();
130 MemorySSA &MSSA = Analyses->MSSA;
131 // Before, the load will be a use of a phi.
132 MemoryUse *LoadAccess = cast(MSSA.getMemoryAccess(LoadInst));
133 MemoryDef *StoreAccess = cast(MSSA.getMemoryAccess(StoreInst));
134 MemoryAccess *DefiningAccess = LoadAccess->getDefiningAccess();
135 EXPECT_TRUE(isa(DefiningAccess));
136 // Kill the store
137 MSSA.removeMemoryAccess(StoreAccess);
138 MemoryPhi *MP = cast(DefiningAccess);
139 // Verify the phi ended up as liveonentry, liveonentry
140 for (auto &Op : MP->incoming_values())
141 EXPECT_TRUE(MSSA.isLiveOnEntryDef(cast(Op.get())));
142 // Replace the phi uses with the live on entry def
143 MP->replaceAllUsesWith(MSSA.getLiveOnEntryDef());
144 // Verify the load is now defined by liveOnEntryDef
145 EXPECT_TRUE(MSSA.isLiveOnEntryDef(LoadAccess->getDefiningAccess()));
146 // Remove the PHI
147 MSSA.removeMemoryAccess(MP);
148 MSSA.verifyMemorySSA();
149 }
66150
67151 TEST_F(MemorySSATest, RemoveMemoryAccess) {
68152 // We create a diamond where there is a store on one side, and then a load
135219 // store i8 2, i8* %A
136220 // }
137221 TEST_F(MemorySSATest, TestTripleStore) {
138 F = Function::Create(
139 FunctionType::get(B.getVoidTy(), {}, false),
140 GlobalValue::ExternalLinkage, "F", &M);
222 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
223 GlobalValue::ExternalLinkage, "F", &M);
141224 B.SetInsertPoint(BasicBlock::Create(C, "", F));
142225 Type *Int8 = Type::getInt8Ty(C);
143226 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
168251 // mostly redundant) unless the initial node being walked is a clobber for the
169252 // query. In that case, we'd cache that the node clobbered itself.
170253 TEST_F(MemorySSATest, TestStoreAndLoad) {
171 F = Function::Create(
172 FunctionType::get(B.getVoidTy(), {}, false),
173 GlobalValue::ExternalLinkage, "F", &M);
254 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
255 GlobalValue::ExternalLinkage, "F", &M);
174256 B.SetInsertPoint(BasicBlock::Create(C, "", F));
175257 Type *Int8 = Type::getInt8Ty(C);
176258 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
199281 // This test checks that repeated calls to either function returns what they're
200282 // meant to.
201283 TEST_F(MemorySSATest, TestStoreDoubleQuery) {
202 F = Function::Create(
203 FunctionType::get(B.getVoidTy(), {}, false),
204 GlobalValue::ExternalLinkage, "F", &M);
284 F = Function::Create(FunctionType::get(B.getVoidTy(), {}, false),
285 GlobalValue::ExternalLinkage, "F", &M);
205286 B.SetInsertPoint(BasicBlock::Create(C, "", F));
206287 Type *Int8 = Type::getInt8Ty(C);
207288 Value *Alloca = B.CreateAlloca(Int8, ConstantInt::get(Int8, 1), "A");
213294
214295 MemoryAccess *StoreAccess = MSSA.getMemoryAccess(SI);
215296 MemoryLocation StoreLoc = MemoryLocation::get(SI);
216 MemoryAccess *Clobber = Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
297 MemoryAccess *Clobber =
298 Walker->getClobberingMemoryAccess(StoreAccess, StoreLoc);
217299 MemoryAccess *LiveOnEntry = Walker->getClobberingMemoryAccess(SI);
218300
219301 EXPECT_EQ(Clobber, StoreAccess);