blob: 581fdf50d8b4783c63a9add699ff7c2b0e5034e9 [file] [log] [blame]
Daniel Berlinae6b8b62017-01-28 01:35:02 +00001//===-- MemorySSAUpdater.cpp - Memory SSA Updater--------------------===//
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//===----------------------------------------------------------------===//
9//
10// This file implements the MemorySSAUpdater class.
11//
12//===----------------------------------------------------------------===//
Daniel Berlin554dcd82017-04-11 20:06:36 +000013#include "llvm/Analysis/MemorySSAUpdater.h"
Daniel Berlinae6b8b62017-01-28 01:35:02 +000014#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/Analysis/MemorySSA.h"
Daniel Berlinae6b8b62017-01-28 01:35:02 +000017#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/Dominators.h"
19#include "llvm/IR/GlobalVariable.h"
20#include "llvm/IR/IRBuilder.h"
Daniel Berlinae6b8b62017-01-28 01:35:02 +000021#include "llvm/IR/LLVMContext.h"
22#include "llvm/IR/Metadata.h"
23#include "llvm/IR/Module.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/FormattedStream.h"
Daniel Berlinae6b8b62017-01-28 01:35:02 +000026#include <algorithm>
27
28#define DEBUG_TYPE "memoryssa"
29using namespace llvm;
George Burgess IV56169ed2017-04-21 04:54:52 +000030
Daniel Berlinae6b8b62017-01-28 01:35:02 +000031// This is the marker algorithm from "Simple and Efficient Construction of
32// Static Single Assignment Form"
33// The simple, non-marker algorithm places phi nodes at any join
34// Here, we place markers, and only place phi nodes if they end up necessary.
35// They are only necessary if they break a cycle (IE we recursively visit
36// ourselves again), or we discover, while getting the value of the operands,
37// that there are two or more definitions needing to be merged.
38// This still will leave non-minimal form in the case of irreducible control
39// flow, where phi nodes may be in cycles with themselves, but unnecessary.
Eli Friedman88e2bac2018-03-26 19:52:54 +000040MemoryAccess *MemorySSAUpdater::getPreviousDefRecursive(
41 BasicBlock *BB,
42 DenseMap<BasicBlock *, TrackingVH<MemoryAccess>> &CachedPreviousDef) {
43 // First, do a cache lookup. Without this cache, certain CFG structures
44 // (like a series of if statements) take exponential time to visit.
45 auto Cached = CachedPreviousDef.find(BB);
46 if (Cached != CachedPreviousDef.end()) {
47 return Cached->second;
George Burgess IV45f263d2018-05-26 02:28:55 +000048 }
49
50 if (BasicBlock *Pred = BB->getSinglePredecessor()) {
Eli Friedman88e2bac2018-03-26 19:52:54 +000051 // Single predecessor case, just recurse, we can only have one definition.
52 MemoryAccess *Result = getPreviousDefFromEnd(Pred, CachedPreviousDef);
53 CachedPreviousDef.insert({BB, Result});
54 return Result;
George Burgess IV45f263d2018-05-26 02:28:55 +000055 }
56
57 if (VisitedBlocks.count(BB)) {
Daniel Berlinae6b8b62017-01-28 01:35:02 +000058 // We hit our node again, meaning we had a cycle, we must insert a phi
59 // node to break it so we have an operand. The only case this will
60 // insert useless phis is if we have irreducible control flow.
Eli Friedman88e2bac2018-03-26 19:52:54 +000061 MemoryAccess *Result = MSSA->createMemoryPhi(BB);
62 CachedPreviousDef.insert({BB, Result});
63 return Result;
George Burgess IV45f263d2018-05-26 02:28:55 +000064 }
65
66 if (VisitedBlocks.insert(BB).second) {
Daniel Berlinae6b8b62017-01-28 01:35:02 +000067 // Mark us visited so we can detect a cycle
68 SmallVector<MemoryAccess *, 8> PhiOps;
69
70 // Recurse to get the values in our predecessors for placement of a
71 // potential phi node. This will insert phi nodes if we cycle in order to
72 // break the cycle and have an operand.
73 for (auto *Pred : predecessors(BB))
Eli Friedman88e2bac2018-03-26 19:52:54 +000074 PhiOps.push_back(getPreviousDefFromEnd(Pred, CachedPreviousDef));
Daniel Berlinae6b8b62017-01-28 01:35:02 +000075
76 // Now try to simplify the ops to avoid placing a phi.
77 // This may return null if we never created a phi yet, that's okay
78 MemoryPhi *Phi = dyn_cast_or_null<MemoryPhi>(MSSA->getMemoryAccess(BB));
79 bool PHIExistsButNeedsUpdate = false;
80 // See if the existing phi operands match what we need.
81 // Unlike normal SSA, we only allow one phi node per block, so we can't just
82 // create a new one.
83 if (Phi && Phi->getNumOperands() != 0)
84 if (!std::equal(Phi->op_begin(), Phi->op_end(), PhiOps.begin())) {
85 PHIExistsButNeedsUpdate = true;
86 }
87
88 // See if we can avoid the phi by simplifying it.
89 auto *Result = tryRemoveTrivialPhi(Phi, PhiOps);
90 // If we couldn't simplify, we may have to create a phi
91 if (Result == Phi) {
92 if (!Phi)
93 Phi = MSSA->createMemoryPhi(BB);
94
95 // These will have been filled in by the recursive read we did above.
96 if (PHIExistsButNeedsUpdate) {
97 std::copy(PhiOps.begin(), PhiOps.end(), Phi->op_begin());
98 std::copy(pred_begin(BB), pred_end(BB), Phi->block_begin());
99 } else {
100 unsigned i = 0;
101 for (auto *Pred : predecessors(BB))
102 Phi->addIncoming(PhiOps[i++], Pred);
Daniel Berlin97f34e82017-09-27 05:35:19 +0000103 InsertedPHIs.push_back(Phi);
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000104 }
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000105 Result = Phi;
106 }
Daniel Berlin97f34e82017-09-27 05:35:19 +0000107
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000108 // Set ourselves up for the next variable by resetting visited state.
109 VisitedBlocks.erase(BB);
Eli Friedman88e2bac2018-03-26 19:52:54 +0000110 CachedPreviousDef.insert({BB, Result});
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000111 return Result;
112 }
113 llvm_unreachable("Should have hit one of the three cases above");
114}
115
116// This starts at the memory access, and goes backwards in the block to find the
117// previous definition. If a definition is not found the block of the access,
118// it continues globally, creating phi nodes to ensure we have a single
119// definition.
120MemoryAccess *MemorySSAUpdater::getPreviousDef(MemoryAccess *MA) {
Eli Friedman88e2bac2018-03-26 19:52:54 +0000121 if (auto *LocalResult = getPreviousDefInBlock(MA))
122 return LocalResult;
123 DenseMap<BasicBlock *, TrackingVH<MemoryAccess>> CachedPreviousDef;
124 return getPreviousDefRecursive(MA->getBlock(), CachedPreviousDef);
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000125}
126
127// This starts at the memory access, and goes backwards in the block to the find
128// the previous definition. If the definition is not found in the block of the
129// access, it returns nullptr.
130MemoryAccess *MemorySSAUpdater::getPreviousDefInBlock(MemoryAccess *MA) {
131 auto *Defs = MSSA->getWritableBlockDefs(MA->getBlock());
132
133 // It's possible there are no defs, or we got handed the first def to start.
134 if (Defs) {
135 // If this is a def, we can just use the def iterators.
136 if (!isa<MemoryUse>(MA)) {
137 auto Iter = MA->getReverseDefsIterator();
138 ++Iter;
139 if (Iter != Defs->rend())
140 return &*Iter;
141 } else {
142 // Otherwise, have to walk the all access iterator.
Alina Sbirlea33e58722017-06-07 16:46:53 +0000143 auto End = MSSA->getWritableBlockAccesses(MA->getBlock())->rend();
144 for (auto &U : make_range(++MA->getReverseIterator(), End))
145 if (!isa<MemoryUse>(U))
146 return cast<MemoryAccess>(&U);
147 // Note that if MA comes before Defs->begin(), we won't hit a def.
148 return nullptr;
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000149 }
150 }
151 return nullptr;
152}
153
154// This starts at the end of block
Eli Friedman88e2bac2018-03-26 19:52:54 +0000155MemoryAccess *MemorySSAUpdater::getPreviousDefFromEnd(
156 BasicBlock *BB,
157 DenseMap<BasicBlock *, TrackingVH<MemoryAccess>> &CachedPreviousDef) {
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000158 auto *Defs = MSSA->getWritableBlockDefs(BB);
159
160 if (Defs)
161 return &*Defs->rbegin();
162
Eli Friedman88e2bac2018-03-26 19:52:54 +0000163 return getPreviousDefRecursive(BB, CachedPreviousDef);
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000164}
165// Recurse over a set of phi uses to eliminate the trivial ones
166MemoryAccess *MemorySSAUpdater::recursePhi(MemoryAccess *Phi) {
167 if (!Phi)
168 return nullptr;
169 TrackingVH<MemoryAccess> Res(Phi);
170 SmallVector<TrackingVH<Value>, 8> Uses;
171 std::copy(Phi->user_begin(), Phi->user_end(), std::back_inserter(Uses));
172 for (auto &U : Uses) {
173 if (MemoryPhi *UsePhi = dyn_cast<MemoryPhi>(&*U)) {
174 auto OperRange = UsePhi->operands();
175 tryRemoveTrivialPhi(UsePhi, OperRange);
176 }
177 }
178 return Res;
179}
180
181// Eliminate trivial phis
182// Phis are trivial if they are defined either by themselves, or all the same
183// argument.
184// IE phi(a, a) or b = phi(a, b) or c = phi(a, a, c)
185// We recursively try to remove them.
186template <class RangeType>
187MemoryAccess *MemorySSAUpdater::tryRemoveTrivialPhi(MemoryPhi *Phi,
188 RangeType &Operands) {
Zhaoshi Zheng43af17b2018-04-09 20:55:37 +0000189 // Bail out on non-opt Phis.
190 if (NonOptPhis.count(Phi))
191 return Phi;
192
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000193 // Detect equal or self arguments
194 MemoryAccess *Same = nullptr;
195 for (auto &Op : Operands) {
196 // If the same or self, good so far
197 if (Op == Phi || Op == Same)
198 continue;
199 // not the same, return the phi since it's not eliminatable by us
200 if (Same)
201 return Phi;
202 Same = cast<MemoryAccess>(Op);
203 }
204 // Never found a non-self reference, the phi is undef
205 if (Same == nullptr)
206 return MSSA->getLiveOnEntryDef();
207 if (Phi) {
208 Phi->replaceAllUsesWith(Same);
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000209 removeMemoryAccess(Phi);
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000210 }
211
212 // We should only end up recursing in case we replaced something, in which
213 // case, we may have made other Phis trivial.
214 return recursePhi(Same);
215}
216
217void MemorySSAUpdater::insertUse(MemoryUse *MU) {
218 InsertedPHIs.clear();
219 MU->setDefiningAccess(getPreviousDef(MU));
220 // Unlike for defs, there is no extra work to do. Because uses do not create
221 // new may-defs, there are only two cases:
222 //
223 // 1. There was a def already below us, and therefore, we should not have
224 // created a phi node because it was already needed for the def.
225 //
226 // 2. There is no def below us, and therefore, there is no extra renaming work
227 // to do.
228}
229
Daniel Berlin9d8a3352017-01-30 11:35:39 +0000230// Set every incoming edge {BB, MP->getBlock()} of MemoryPhi MP to NewDef.
George Burgess IV56169ed2017-04-21 04:54:52 +0000231static void setMemoryPhiValueForBlock(MemoryPhi *MP, const BasicBlock *BB,
232 MemoryAccess *NewDef) {
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000233 // Replace any operand with us an incoming block with the new defining
234 // access.
235 int i = MP->getBasicBlockIndex(BB);
236 assert(i != -1 && "Should have found the basic block in the phi");
Daniel Berlin9d8a3352017-01-30 11:35:39 +0000237 // We can't just compare i against getNumOperands since one is signed and the
238 // other not. So use it to index into the block iterator.
239 for (auto BBIter = MP->block_begin() + i; BBIter != MP->block_end();
240 ++BBIter) {
241 if (*BBIter != BB)
242 break;
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000243 MP->setIncomingValue(i, NewDef);
244 ++i;
245 }
246}
247
248// A brief description of the algorithm:
249// First, we compute what should define the new def, using the SSA
250// construction algorithm.
251// Then, we update the defs below us (and any new phi nodes) in the graph to
252// point to the correct new defs, to ensure we only have one variable, and no
253// disconnected stores.
Daniel Berlin78cbd282017-02-20 22:26:03 +0000254void MemorySSAUpdater::insertDef(MemoryDef *MD, bool RenameUses) {
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000255 InsertedPHIs.clear();
256
257 // See if we had a local def, and if not, go hunting.
Eli Friedman88e2bac2018-03-26 19:52:54 +0000258 MemoryAccess *DefBefore = getPreviousDef(MD);
259 bool DefBeforeSameBlock = DefBefore->getBlock() == MD->getBlock();
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000260
261 // There is a def before us, which means we can replace any store/phi uses
262 // of that thing with us, since we are in the way of whatever was there
263 // before.
264 // We now define that def's memorydefs and memoryphis
Daniel Berlin9d8a3352017-01-30 11:35:39 +0000265 if (DefBeforeSameBlock) {
266 for (auto UI = DefBefore->use_begin(), UE = DefBefore->use_end();
267 UI != UE;) {
268 Use &U = *UI++;
269 // Leave the uses alone
270 if (isa<MemoryUse>(U.getUser()))
271 continue;
272 U.set(MD);
273 }
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000274 }
Daniel Berlin9d8a3352017-01-30 11:35:39 +0000275
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000276 // and that def is now our defining access.
277 // We change them in this order otherwise we will appear in the use list
278 // above and reset ourselves.
279 MD->setDefiningAccess(DefBefore);
280
281 SmallVector<MemoryAccess *, 8> FixupList(InsertedPHIs.begin(),
282 InsertedPHIs.end());
283 if (!DefBeforeSameBlock) {
284 // If there was a local def before us, we must have the same effect it
285 // did. Because every may-def is the same, any phis/etc we would create, it
286 // would also have created. If there was no local def before us, we
287 // performed a global update, and have to search all successors and make
288 // sure we update the first def in each of them (following all paths until
289 // we hit the first def along each path). This may also insert phi nodes.
290 // TODO: There are other cases we can skip this work, such as when we have a
291 // single successor, and only used a straight line of single pred blocks
292 // backwards to find the def. To make that work, we'd have to track whether
293 // getDefRecursive only ever used the single predecessor case. These types
294 // of paths also only exist in between CFG simplifications.
295 FixupList.push_back(MD);
296 }
297
298 while (!FixupList.empty()) {
299 unsigned StartingPHISize = InsertedPHIs.size();
300 fixupDefs(FixupList);
301 FixupList.clear();
302 // Put any new phis on the fixup list, and process them
303 FixupList.append(InsertedPHIs.end() - StartingPHISize, InsertedPHIs.end());
304 }
Daniel Berlin78cbd282017-02-20 22:26:03 +0000305 // Now that all fixups are done, rename all uses if we are asked.
306 if (RenameUses) {
307 SmallPtrSet<BasicBlock *, 16> Visited;
308 BasicBlock *StartBlock = MD->getBlock();
309 // We are guaranteed there is a def in the block, because we just got it
310 // handed to us in this function.
311 MemoryAccess *FirstDef = &*MSSA->getWritableBlockDefs(StartBlock)->begin();
312 // Convert to incoming value if it's a memorydef. A phi *is* already an
313 // incoming value.
314 if (auto *MD = dyn_cast<MemoryDef>(FirstDef))
315 FirstDef = MD->getDefiningAccess();
316
317 MSSA->renamePass(MD->getBlock(), FirstDef, Visited);
318 // We just inserted a phi into this block, so the incoming value will become
319 // the phi anyway, so it does not matter what we pass.
320 for (auto *MP : InsertedPHIs)
321 MSSA->renamePass(MP->getBlock(), nullptr, Visited);
322 }
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000323}
324
325void MemorySSAUpdater::fixupDefs(const SmallVectorImpl<MemoryAccess *> &Vars) {
326 SmallPtrSet<const BasicBlock *, 8> Seen;
327 SmallVector<const BasicBlock *, 16> Worklist;
328 for (auto *NewDef : Vars) {
329 // First, see if there is a local def after the operand.
330 auto *Defs = MSSA->getWritableBlockDefs(NewDef->getBlock());
331 auto DefIter = NewDef->getDefsIterator();
332
Zhaoshi Zheng43af17b2018-04-09 20:55:37 +0000333 // The temporary Phi is being fixed, unmark it for not to optimize.
George Burgess IVe7cdb7e2018-07-12 21:56:31 +0000334 if (MemoryPhi *Phi = dyn_cast<MemoryPhi>(NewDef))
Zhaoshi Zheng43af17b2018-04-09 20:55:37 +0000335 NonOptPhis.erase(Phi);
336
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000337 // If there is a local def after us, we only have to rename that.
338 if (++DefIter != Defs->end()) {
339 cast<MemoryDef>(DefIter)->setDefiningAccess(NewDef);
340 continue;
341 }
342
343 // Otherwise, we need to search down through the CFG.
344 // For each of our successors, handle it directly if their is a phi, or
345 // place on the fixup worklist.
346 for (const auto *S : successors(NewDef->getBlock())) {
347 if (auto *MP = MSSA->getMemoryAccess(S))
348 setMemoryPhiValueForBlock(MP, NewDef->getBlock(), NewDef);
349 else
350 Worklist.push_back(S);
351 }
352
353 while (!Worklist.empty()) {
354 const BasicBlock *FixupBlock = Worklist.back();
355 Worklist.pop_back();
356
357 // Get the first def in the block that isn't a phi node.
358 if (auto *Defs = MSSA->getWritableBlockDefs(FixupBlock)) {
359 auto *FirstDef = &*Defs->begin();
360 // The loop above and below should have taken care of phi nodes
361 assert(!isa<MemoryPhi>(FirstDef) &&
362 "Should have already handled phi nodes!");
363 // We are now this def's defining access, make sure we actually dominate
364 // it
365 assert(MSSA->dominates(NewDef, FirstDef) &&
366 "Should have dominated the new access");
367
368 // This may insert new phi nodes, because we are not guaranteed the
369 // block we are processing has a single pred, and depending where the
370 // store was inserted, it may require phi nodes below it.
371 cast<MemoryDef>(FirstDef)->setDefiningAccess(getPreviousDef(FirstDef));
372 return;
373 }
374 // We didn't find a def, so we must continue.
375 for (const auto *S : successors(FixupBlock)) {
376 // If there is a phi node, handle it.
377 // Otherwise, put the block on the worklist
378 if (auto *MP = MSSA->getMemoryAccess(S))
379 setMemoryPhiValueForBlock(MP, FixupBlock, NewDef);
380 else {
381 // If we cycle, we should have ended up at a phi node that we already
382 // processed. FIXME: Double check this
383 if (!Seen.insert(S).second)
384 continue;
385 Worklist.push_back(S);
386 }
387 }
388 }
389 }
390}
391
392// Move What before Where in the MemorySSA IR.
Daniel Berlin9d8a3352017-01-30 11:35:39 +0000393template <class WhereType>
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000394void MemorySSAUpdater::moveTo(MemoryUseOrDef *What, BasicBlock *BB,
Daniel Berlin9d8a3352017-01-30 11:35:39 +0000395 WhereType Where) {
Zhaoshi Zheng43af17b2018-04-09 20:55:37 +0000396 // Mark MemoryPhi users of What not to be optimized.
397 for (auto *U : What->users())
George Burgess IVe7cdb7e2018-07-12 21:56:31 +0000398 if (MemoryPhi *PhiUser = dyn_cast<MemoryPhi>(U))
Zhaoshi Zheng43af17b2018-04-09 20:55:37 +0000399 NonOptPhis.insert(PhiUser);
400
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000401 // Replace all our users with our defining access.
402 What->replaceAllUsesWith(What->getDefiningAccess());
403
404 // Let MemorySSA take care of moving it around in the lists.
405 MSSA->moveTo(What, BB, Where);
406
407 // Now reinsert it into the IR and do whatever fixups needed.
408 if (auto *MD = dyn_cast<MemoryDef>(What))
409 insertDef(MD);
410 else
411 insertUse(cast<MemoryUse>(What));
Zhaoshi Zheng43af17b2018-04-09 20:55:37 +0000412
413 // Clear dangling pointers. We added all MemoryPhi users, but not all
414 // of them are removed by fixupDefs().
415 NonOptPhis.clear();
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000416}
Daniel Berlin9d8a3352017-01-30 11:35:39 +0000417
Daniel Berlinae6b8b62017-01-28 01:35:02 +0000418// Move What before Where in the MemorySSA IR.
419void MemorySSAUpdater::moveBefore(MemoryUseOrDef *What, MemoryUseOrDef *Where) {
420 moveTo(What, Where->getBlock(), Where->getIterator());
421}
422
423// Move What after Where in the MemorySSA IR.
424void MemorySSAUpdater::moveAfter(MemoryUseOrDef *What, MemoryUseOrDef *Where) {
425 moveTo(What, Where->getBlock(), ++Where->getIterator());
426}
427
Daniel Berlin9d8a3352017-01-30 11:35:39 +0000428void MemorySSAUpdater::moveToPlace(MemoryUseOrDef *What, BasicBlock *BB,
429 MemorySSA::InsertionPlace Where) {
430 return moveTo(What, BB, Where);
431}
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000432
Alina Sbirlea0f533552018-07-11 22:11:46 +0000433// All accesses in To used to be in From. Move to end and update access lists.
434void MemorySSAUpdater::moveAllAccesses(BasicBlock *From, BasicBlock *To,
435 Instruction *Start) {
436
437 MemorySSA::AccessList *Accs = MSSA->getWritableBlockAccesses(From);
438 if (!Accs)
439 return;
440
441 MemoryAccess *FirstInNew = nullptr;
442 for (Instruction &I : make_range(Start->getIterator(), To->end()))
443 if ((FirstInNew = MSSA->getMemoryAccess(&I)))
444 break;
445 if (!FirstInNew)
446 return;
447
448 auto *MUD = cast<MemoryUseOrDef>(FirstInNew);
449 do {
450 auto NextIt = ++MUD->getIterator();
451 MemoryUseOrDef *NextMUD = (!Accs || NextIt == Accs->end())
452 ? nullptr
453 : cast<MemoryUseOrDef>(&*NextIt);
454 MSSA->moveTo(MUD, To, MemorySSA::End);
455 // Moving MUD from Accs in the moveTo above, may delete Accs, so we need to
456 // retrieve it again.
457 Accs = MSSA->getWritableBlockAccesses(From);
458 MUD = NextMUD;
459 } while (MUD);
460}
461
462void MemorySSAUpdater::moveAllAfterSpliceBlocks(BasicBlock *From,
463 BasicBlock *To,
464 Instruction *Start) {
465 assert(MSSA->getBlockAccesses(To) == nullptr &&
466 "To block is expected to be free of MemoryAccesses.");
467 moveAllAccesses(From, To, Start);
468 for (BasicBlock *Succ : successors(To))
469 if (MemoryPhi *MPhi = MSSA->getMemoryAccess(Succ))
470 MPhi->setIncomingBlock(MPhi->getBasicBlockIndex(From), To);
471}
472
473void MemorySSAUpdater::moveAllAfterMergeBlocks(BasicBlock *From, BasicBlock *To,
474 Instruction *Start) {
475 assert(From->getSinglePredecessor() == To &&
476 "From block is expected to have a single predecessor (To).");
477 moveAllAccesses(From, To, Start);
478 for (BasicBlock *Succ : successors(From))
479 if (MemoryPhi *MPhi = MSSA->getMemoryAccess(Succ))
480 MPhi->setIncomingBlock(MPhi->getBasicBlockIndex(From), To);
481}
482
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000483/// If all arguments of a MemoryPHI are defined by the same incoming
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000484/// argument, return that argument.
485static MemoryAccess *onlySingleValue(MemoryPhi *MP) {
486 MemoryAccess *MA = nullptr;
487
488 for (auto &Arg : MP->operands()) {
489 if (!MA)
490 MA = cast<MemoryAccess>(Arg);
491 else if (MA != Arg)
492 return nullptr;
493 }
494 return MA;
495}
George Burgess IV56169ed2017-04-21 04:54:52 +0000496
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000497void MemorySSAUpdater::removeMemoryAccess(MemoryAccess *MA) {
498 assert(!MSSA->isLiveOnEntryDef(MA) &&
499 "Trying to remove the live on entry def");
500 // We can only delete phi nodes if they have no uses, or we can replace all
501 // uses with a single definition.
502 MemoryAccess *NewDefTarget = nullptr;
503 if (MemoryPhi *MP = dyn_cast<MemoryPhi>(MA)) {
504 // Note that it is sufficient to know that all edges of the phi node have
505 // the same argument. If they do, by the definition of dominance frontiers
506 // (which we used to place this phi), that argument must dominate this phi,
507 // and thus, must dominate the phi's uses, and so we will not hit the assert
508 // below.
509 NewDefTarget = onlySingleValue(MP);
510 assert((NewDefTarget || MP->use_empty()) &&
511 "We can't delete this memory phi");
512 } else {
513 NewDefTarget = cast<MemoryUseOrDef>(MA)->getDefiningAccess();
514 }
515
516 // Re-point the uses at our defining access
517 if (!isa<MemoryUse>(MA) && !MA->use_empty()) {
518 // Reset optimized on users of this store, and reset the uses.
519 // A few notes:
520 // 1. This is a slightly modified version of RAUW to avoid walking the
521 // uses twice here.
522 // 2. If we wanted to be complete, we would have to reset the optimized
523 // flags on users of phi nodes if doing the below makes a phi node have all
524 // the same arguments. Instead, we prefer users to removeMemoryAccess those
525 // phi nodes, because doing it here would be N^3.
526 if (MA->hasValueHandle())
527 ValueHandleBase::ValueIsRAUWd(MA, NewDefTarget);
528 // Note: We assume MemorySSA is not used in metadata since it's not really
529 // part of the IR.
530
531 while (!MA->use_empty()) {
532 Use &U = *MA->use_begin();
Daniel Berline33bc312017-04-04 23:43:10 +0000533 if (auto *MUD = dyn_cast<MemoryUseOrDef>(U.getUser()))
534 MUD->resetOptimized();
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000535 U.set(NewDefTarget);
536 }
537 }
538
539 // The call below to erase will destroy MA, so we can't change the order we
540 // are doing things here
541 MSSA->removeFromLookups(MA);
542 MSSA->removeFromLists(MA);
543}
544
Alina Sbirleada1e80f2018-06-29 20:46:16 +0000545void MemorySSAUpdater::removeBlocks(
546 const SmallPtrSetImpl<BasicBlock *> &DeadBlocks) {
547 // First delete all uses of BB in MemoryPhis.
548 for (BasicBlock *BB : DeadBlocks) {
549 TerminatorInst *TI = BB->getTerminator();
550 assert(TI && "Basic block expected to have a terminator instruction");
551 for (BasicBlock *Succ : TI->successors())
552 if (!DeadBlocks.count(Succ))
553 if (MemoryPhi *MP = MSSA->getMemoryAccess(Succ)) {
554 MP->unorderedDeleteIncomingBlock(BB);
555 if (MP->getNumIncomingValues() == 1)
556 removeMemoryAccess(MP);
557 }
558 // Drop all references of all accesses in BB
559 if (MemorySSA::AccessList *Acc = MSSA->getWritableBlockAccesses(BB))
560 for (MemoryAccess &MA : *Acc)
561 MA.dropAllReferences();
562 }
563
564 // Next, delete all memory accesses in each block
565 for (BasicBlock *BB : DeadBlocks) {
566 MemorySSA::AccessList *Acc = MSSA->getWritableBlockAccesses(BB);
567 if (!Acc)
568 continue;
569 for (auto AB = Acc->begin(), AE = Acc->end(); AB != AE;) {
570 MemoryAccess *MA = &*AB;
571 ++AB;
572 MSSA->removeFromLookups(MA);
573 MSSA->removeFromLists(MA);
574 }
575 }
576}
577
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000578MemoryAccess *MemorySSAUpdater::createMemoryAccessInBB(
579 Instruction *I, MemoryAccess *Definition, const BasicBlock *BB,
580 MemorySSA::InsertionPlace Point) {
581 MemoryUseOrDef *NewAccess = MSSA->createDefinedAccess(I, Definition);
582 MSSA->insertIntoListsForBlock(NewAccess, BB, Point);
583 return NewAccess;
584}
585
586MemoryUseOrDef *MemorySSAUpdater::createMemoryAccessBefore(
587 Instruction *I, MemoryAccess *Definition, MemoryUseOrDef *InsertPt) {
588 assert(I->getParent() == InsertPt->getBlock() &&
589 "New and old access must be in the same block");
590 MemoryUseOrDef *NewAccess = MSSA->createDefinedAccess(I, Definition);
591 MSSA->insertIntoListsBefore(NewAccess, InsertPt->getBlock(),
592 InsertPt->getIterator());
593 return NewAccess;
594}
595
596MemoryUseOrDef *MemorySSAUpdater::createMemoryAccessAfter(
597 Instruction *I, MemoryAccess *Definition, MemoryAccess *InsertPt) {
598 assert(I->getParent() == InsertPt->getBlock() &&
599 "New and old access must be in the same block");
600 MemoryUseOrDef *NewAccess = MSSA->createDefinedAccess(I, Definition);
601 MSSA->insertIntoListsBefore(NewAccess, InsertPt->getBlock(),
602 ++InsertPt->getIterator());
603 return NewAccess;
604}