LLVM 23.0.0git
MemorySSA.cpp
Go to the documentation of this file.
1//===- MemorySSA.cpp - Memory SSA Builder ---------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the MemorySSA class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/DenseSet.h"
18#include "llvm/ADT/Hashing.h"
19#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/iterator.h"
30#include "llvm/Config/llvm-config.h"
32#include "llvm/IR/BasicBlock.h"
33#include "llvm/IR/Dominators.h"
34#include "llvm/IR/Function.h"
35#include "llvm/IR/Instruction.h"
38#include "llvm/IR/LLVMContext.h"
39#include "llvm/IR/Operator.h"
40#include "llvm/IR/PassManager.h"
41#include "llvm/IR/Use.h"
43#include "llvm/Pass.h"
48#include "llvm/Support/Debug.h"
53#include <algorithm>
54#include <cassert>
55#include <iterator>
56#include <memory>
57#include <utility>
58
59using namespace llvm;
60
61#define DEBUG_TYPE "memoryssa"
62
64 DotCFGMSSA("dot-cfg-mssa",
65 cl::value_desc("file name for generated dot file"),
66 cl::desc("file name for generated dot file"), cl::init(""));
67
68INITIALIZE_PASS_BEGIN(MemorySSAWrapperPass, "memoryssa", "Memory SSA", false,
69 true)
73 true)
74
75static cl::opt<unsigned> MaxCheckLimit(
76 "memssa-check-limit", cl::Hidden, cl::init(100),
77 cl::desc("The maximum number of stores/phis MemorySSA"
78 "will consider trying to walk past (default = 100)"));
79
80// Always verify MemorySSA if expensive checking is enabled.
81#ifdef EXPENSIVE_CHECKS
82bool llvm::VerifyMemorySSA = true;
83#else
85#endif
86
89 cl::Hidden, cl::desc("Enable verification of MemorySSA."));
90
91const static char LiveOnEntryStr[] = "liveOnEntry";
92
93namespace {
94
95/// An assembly annotator class to print Memory SSA information in
96/// comments.
97class MemorySSAAnnotatedWriter : public AssemblyAnnotationWriter {
98 const MemorySSA *MSSA;
99
100public:
101 MemorySSAAnnotatedWriter(const MemorySSA *M) : MSSA(M) {}
102
103 void emitBasicBlockStartAnnot(const BasicBlock *BB,
104 formatted_raw_ostream &OS) override {
105 if (MemoryAccess *MA = MSSA->getMemoryAccess(BB))
106 OS << "; " << *MA << "\n";
107 }
108
109 void emitInstructionAnnot(const Instruction *I,
110 formatted_raw_ostream &OS) override {
111 if (MemoryAccess *MA = MSSA->getMemoryAccess(I))
112 OS << "; " << *MA << "\n";
113 }
114};
115
116/// An assembly annotator class to print Memory SSA information in
117/// comments.
118class MemorySSAWalkerAnnotatedWriter : public AssemblyAnnotationWriter {
119 MemorySSA *MSSA;
120 MemorySSAWalker *Walker;
121 BatchAAResults BAA;
122
123public:
124 MemorySSAWalkerAnnotatedWriter(MemorySSA *M)
125 : MSSA(M), Walker(M->getWalker()), BAA(M->getAA()) {}
126
127 void emitBasicBlockStartAnnot(const BasicBlock *BB,
128 formatted_raw_ostream &OS) override {
129 if (MemoryAccess *MA = MSSA->getMemoryAccess(BB))
130 OS << "; " << *MA << "\n";
131 }
132
133 void emitInstructionAnnot(const Instruction *I,
134 formatted_raw_ostream &OS) override {
135 if (MemoryAccess *MA = MSSA->getMemoryAccess(I)) {
136 MemoryAccess *Clobber = Walker->getClobberingMemoryAccess(MA, BAA);
137 OS << "; " << *MA;
138 if (Clobber) {
139 OS << " - clobbered by ";
140 if (MSSA->isLiveOnEntryDef(Clobber))
141 OS << LiveOnEntryStr;
142 else
143 OS << *Clobber;
144 }
145 OS << "\n";
146 }
147 }
148};
149
150} // namespace
151
152namespace {
153
154/// Our current alias analysis API differentiates heavily between calls and
155/// non-calls, and functions called on one usually assert on the other.
156/// This class encapsulates the distinction to simplify other code that wants
157/// "Memory affecting instructions and related data" to use as a key.
158/// For example, this class is used as a densemap key in the use optimizer.
159class MemoryLocOrCall {
160public:
161 bool IsCall = false;
162
163 MemoryLocOrCall(MemoryUseOrDef *MUD)
164 : MemoryLocOrCall(MUD->getMemoryInst()) {}
165 MemoryLocOrCall(const MemoryUseOrDef *MUD)
166 : MemoryLocOrCall(MUD->getMemoryInst()) {}
167
168 MemoryLocOrCall(Instruction *Inst) {
169 if (auto *C = dyn_cast<CallBase>(Inst)) {
170 IsCall = true;
171 Call = C;
172 } else {
173 IsCall = false;
174 // There is no such thing as a memorylocation for a fence inst, and it is
175 // unique in that regard.
176 if (!isa<FenceInst>(Inst))
177 Loc = MemoryLocation::get(Inst);
178 }
179 }
180
181 explicit MemoryLocOrCall(const MemoryLocation &Loc) : Loc(Loc) {}
182
183 const CallBase *getCall() const {
184 assert(IsCall);
185 return Call;
186 }
187
188 MemoryLocation getLoc() const {
189 assert(!IsCall);
190 return Loc;
191 }
192
193 bool operator==(const MemoryLocOrCall &Other) const {
194 if (IsCall != Other.IsCall)
195 return false;
196
197 if (!IsCall)
198 return Loc == Other.Loc;
199
200 if (Call->getCalledOperand() != Other.Call->getCalledOperand())
201 return false;
202
203 return Call->arg_size() == Other.Call->arg_size() &&
204 std::equal(Call->arg_begin(), Call->arg_end(),
205 Other.Call->arg_begin());
206 }
207
208private:
209 union {
210 const CallBase *Call;
211 MemoryLocation Loc;
212 };
213};
214
215} // end anonymous namespace
216
217namespace llvm {
218
219template <> struct DenseMapInfo<MemoryLocOrCall> {
220 static inline MemoryLocOrCall getEmptyKey() {
221 return MemoryLocOrCall(DenseMapInfo<MemoryLocation>::getEmptyKey());
222 }
223
224 static unsigned getHashValue(const MemoryLocOrCall &MLOC) {
225 if (!MLOC.IsCall)
226 return hash_combine(
227 MLOC.IsCall,
229
230 hash_code hash =
232 MLOC.getCall()->getCalledOperand()));
233
234 for (const Value *Arg : MLOC.getCall()->args())
236 return hash;
237 }
238
239 static bool isEqual(const MemoryLocOrCall &LHS, const MemoryLocOrCall &RHS) {
240 return LHS == RHS;
241 }
242};
243
244} // end namespace llvm
245
246/// This does one-way checks to see if Use could theoretically be hoisted above
247/// MayClobber. This will not check the other way around.
248///
249/// This assumes that, for the purposes of MemorySSA, Use comes directly after
250/// MayClobber, with no potentially clobbering operations in between them.
251/// (Where potentially clobbering ops are memory barriers, aliased stores, etc.)
253 const LoadInst *MayClobber) {
254 bool VolatileUse = Use->isVolatile();
255 bool VolatileClobber = MayClobber->isVolatile();
256 // Volatile operations may never be reordered with other volatile operations.
257 if (VolatileUse && VolatileClobber)
258 return false;
259 // Otherwise, volatile doesn't matter here. From the language reference:
260 // 'optimizers may change the order of volatile operations relative to
261 // non-volatile operations.'"
262
263 // If a load is seq_cst, it cannot be moved above other loads. If its ordering
264 // is weaker, it can be moved above other loads. We just need to be sure that
265 // MayClobber isn't an acquire load, because loads can't be moved above
266 // acquire loads.
267 //
268 // Note that this explicitly *does* allow the free reordering of monotonic (or
269 // weaker) loads of the same address.
270 bool SeqCstUse = Use->getOrdering() == AtomicOrdering::SequentiallyConsistent;
271 bool MayClobberIsAcquire = isAtLeastOrStrongerThan(MayClobber->getOrdering(),
273 return !(SeqCstUse || MayClobberIsAcquire);
274}
275
276template <typename AliasAnalysisType>
277static bool
279 const Instruction *UseInst, AliasAnalysisType &AA) {
280 Instruction *DefInst = MD->getMemoryInst();
281 assert(DefInst && "Defining instruction not actually an instruction");
282
283 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(DefInst)) {
284 // These intrinsics will show up as affecting memory, but they are just
285 // markers, mostly.
286 //
287 // FIXME: We probably don't actually want MemorySSA to model these at all
288 // (including creating MemoryAccesses for them): we just end up inventing
289 // clobbers where they don't really exist at all. Please see D43269 for
290 // context.
291 switch (II->getIntrinsicID()) {
292 case Intrinsic::allow_runtime_check:
293 case Intrinsic::allow_ubsan_check:
294 case Intrinsic::invariant_start:
295 case Intrinsic::invariant_end:
296 case Intrinsic::assume:
297 case Intrinsic::experimental_noalias_scope_decl:
298 case Intrinsic::pseudoprobe:
299 return false;
300 case Intrinsic::dbg_declare:
301 case Intrinsic::dbg_label:
302 case Intrinsic::dbg_value:
303 llvm_unreachable("debuginfo shouldn't have associated defs!");
304 default:
305 break;
306 }
307 }
308
309 if (auto *CB = dyn_cast_or_null<CallBase>(UseInst)) {
310 ModRefInfo I = AA.getModRefInfo(DefInst, CB);
311 return isModSet(I);
312 }
313
314 if (auto *DefLoad = dyn_cast<LoadInst>(DefInst))
315 if (auto *UseLoad = dyn_cast_or_null<LoadInst>(UseInst))
316 return !areLoadsReorderable(UseLoad, DefLoad);
317
318 ModRefInfo I = AA.getModRefInfo(DefInst, UseLoc);
319 return isModSet(I);
320}
321
322template <typename AliasAnalysisType>
324 const MemoryLocOrCall &UseMLOC,
325 AliasAnalysisType &AA) {
326 // FIXME: This is a temporary hack to allow a single instructionClobbersQuery
327 // to exist while MemoryLocOrCall is pushed through places.
328 if (UseMLOC.IsCall)
330 AA);
331 return instructionClobbersQuery(MD, UseMLOC.getLoc(), MU->getMemoryInst(),
332 AA);
333}
334
335// Return true when MD may alias MU, return false otherwise.
337 AliasAnalysis &AA) {
338 return instructionClobbersQuery(MD, MU, MemoryLocOrCall(MU), AA);
339}
340
341namespace {
342
343struct UpwardsMemoryQuery {
344 // True if our original query started off as a call
345 bool IsCall = false;
346 // The pointer location we started the query with. This will be empty if
347 // IsCall is true.
348 MemoryLocation StartingLoc;
349 // This is the instruction we were querying about.
350 const Instruction *Inst = nullptr;
351 // The MemoryAccess we actually got called with, used to test local domination
352 const MemoryAccess *OriginalAccess = nullptr;
353 bool SkipSelfAccess = false;
354
355 UpwardsMemoryQuery() = default;
356
357 UpwardsMemoryQuery(const Instruction *Inst, const MemoryAccess *Access)
358 : IsCall(isa<CallBase>(Inst)), Inst(Inst), OriginalAccess(Access) {
359 if (!IsCall)
360 StartingLoc = MemoryLocation::get(Inst);
361 }
362};
363
364} // end anonymous namespace
365
366template <typename AliasAnalysisType>
367static bool isUseTriviallyOptimizableToLiveOnEntry(AliasAnalysisType &AA,
368 const Instruction *I) {
369 // If the memory can't be changed, then loads of the memory can't be
370 // clobbered.
371 if (auto *LI = dyn_cast<LoadInst>(I)) {
372 return I->hasMetadata(LLVMContext::MD_invariant_load) ||
373 !isModSet(AA.getModRefInfoMask(MemoryLocation::get(LI)));
374 }
375 return false;
376}
377
378/// Verifies that `Start` is clobbered by `ClobberAt`, and that nothing
379/// inbetween `Start` and `ClobberAt` can clobbers `Start`.
380///
381/// This is meant to be as simple and self-contained as possible. Because it
382/// uses no cache, etc., it can be relatively expensive.
383///
384/// \param Start The MemoryAccess that we want to walk from.
385/// \param ClobberAt A clobber for Start.
386/// \param StartLoc The MemoryLocation for Start.
387/// \param MSSA The MemorySSA instance that Start and ClobberAt belong to.
388/// \param Query The UpwardsMemoryQuery we used for our search.
389/// \param AA The AliasAnalysis we used for our search.
390/// \param AllowImpreciseClobber Always false, unless we do relaxed verify.
391
392[[maybe_unused]] static void
394 const MemoryLocation &StartLoc, const MemorySSA &MSSA,
395 const UpwardsMemoryQuery &Query, BatchAAResults &AA,
396 bool AllowImpreciseClobber = false) {
397 assert(MSSA.dominates(ClobberAt, Start) && "Clobber doesn't dominate start?");
398
399 if (MSSA.isLiveOnEntryDef(Start)) {
400 assert(MSSA.isLiveOnEntryDef(ClobberAt) &&
401 "liveOnEntry must clobber itself");
402 return;
403 }
404
405 bool FoundClobber = false;
406 DenseSet<UpwardDefsElem> VisitedPhis;
408 Worklist.push_back({Start, StartLoc, /*MayBeCrossIteration=*/false});
409 // Walk all paths from Start to ClobberAt, while looking for clobbers. If one
410 // is found, complain.
411 while (!Worklist.empty()) {
412 auto MAP = Worklist.pop_back_val();
413 // All we care about is that nothing from Start to ClobberAt clobbers Start.
414 // We learn nothing from revisiting nodes.
415 if (!VisitedPhis.insert(MAP).second)
416 continue;
417
418 for (auto *MA : def_chain(MAP.MA)) {
419 if (MA == ClobberAt) {
420 if (const auto *MD = dyn_cast<MemoryDef>(MA)) {
421 // instructionClobbersQuery isn't essentially free, so don't use `|=`,
422 // since it won't let us short-circuit.
423 //
424 // Also, note that this can't be hoisted out of the `Worklist` loop,
425 // since MD may only act as a clobber for 1 of N MemoryLocations.
426 FoundClobber = FoundClobber || MSSA.isLiveOnEntryDef(MD);
427 if (!FoundClobber) {
428 BatchAACrossIterationScope _(AA, MAP.MayBeCrossIteration);
429 if (instructionClobbersQuery(MD, MAP.Loc, Query.Inst, AA))
430 FoundClobber = true;
431 }
432 }
433 break;
434 }
435
436 // We should never hit liveOnEntry, unless it's the clobber.
437 assert(!MSSA.isLiveOnEntryDef(MA) && "Hit liveOnEntry before clobber?");
438
439 if (const auto *MD = dyn_cast<MemoryDef>(MA)) {
440 // If Start is a Def, skip self.
441 if (MD == Start)
442 continue;
443
444 BatchAACrossIterationScope _(AA, MAP.MayBeCrossIteration);
445 assert(!instructionClobbersQuery(MD, MAP.Loc, Query.Inst, AA) &&
446 "Found clobber before reaching ClobberAt!");
447 continue;
448 }
449
450 if (const auto *MU = dyn_cast<MemoryUse>(MA)) {
451 (void)MU;
452 assert (MU == Start &&
453 "Can only find use in def chain if Start is a use");
454 continue;
455 }
456
458
459 // Add reachable phi predecessors
460 for (auto ItB = upward_defs_begin({MA, MAP.Loc, MAP.MayBeCrossIteration},
461 MSSA.getDomTree()),
462 ItE = upward_defs_end();
463 ItB != ItE; ++ItB)
464 if (MSSA.getDomTree().isReachableFromEntry(ItB.getPhiArgBlock()))
465 Worklist.emplace_back(*ItB);
466 }
467 }
468
469 // If the verify is done following an optimization, it's possible that
470 // ClobberAt was a conservative clobbering, that we can now infer is not a
471 // true clobbering access. Don't fail the verify if that's the case.
472 // We do have accesses that claim they're optimized, but could be optimized
473 // further. Updating all these can be expensive, so allow it for now (FIXME).
474 if (AllowImpreciseClobber)
475 return;
476
477 // If ClobberAt is a MemoryPhi, we can assume something above it acted as a
478 // clobber. Otherwise, `ClobberAt` should've acted as a clobber at some point.
479 assert((isa<MemoryPhi>(ClobberAt) || FoundClobber) &&
480 "ClobberAt never acted as a clobber");
481}
482
483namespace {
484
485/// Our algorithm for walking (and trying to optimize) clobbers, all wrapped up
486/// in one class.
487class ClobberWalker {
488 /// Save a few bytes by using unsigned instead of size_t.
489 using ListIndex = unsigned;
490
491 /// Represents a span of contiguous MemoryDefs, potentially ending in a
492 /// MemoryPhi.
493 struct DefPath {
494 MemoryLocation Loc;
495 // Note that, because we always walk in reverse, Last will always dominate
496 // First. Also note that First and Last are inclusive.
497 MemoryAccess *First;
498 MemoryAccess *Last;
499 std::optional<ListIndex> Previous;
500 bool MayBeCrossIteration;
501
502 DefPath(const MemoryLocation &Loc, MemoryAccess *First, MemoryAccess *Last,
503 bool MayBeCrossIteration, std::optional<ListIndex> Previous)
504 : Loc(Loc), First(First), Last(Last), Previous(Previous),
505 MayBeCrossIteration(MayBeCrossIteration) {}
506
507 DefPath(const MemoryLocation &Loc, MemoryAccess *Init,
508 bool MayBeCrossIteration, std::optional<ListIndex> Previous)
509 : DefPath(Loc, Init, Init, MayBeCrossIteration, Previous) {}
510 };
511
512 const MemorySSA &MSSA;
513 DominatorTree &DT;
514 BatchAAResults *AA;
515 UpwardsMemoryQuery *Query;
516 unsigned *UpwardWalkLimit;
517
518 // Phi optimization bookkeeping:
519 // List of DefPath to process during the current phi optimization walk.
521 // List of visited <Access, Location> pairs; we can skip paths already
522 // visited with the same memory location.
523 DenseSet<UpwardDefsElem> VisitedPhis;
524
525 /// Find the nearest def or phi that `From` can legally be optimized to.
526 const MemoryAccess *getWalkTarget(const MemoryPhi *From) const {
527 assert(From->getNumOperands() && "Phi with no operands?");
528
529 BasicBlock *BB = From->getBlock();
530 MemoryAccess *Result = MSSA.getLiveOnEntryDef();
531 DomTreeNode *Node = DT.getNode(BB);
532 while ((Node = Node->getIDom())) {
533 auto *Defs = MSSA.getBlockDefs(Node->getBlock());
534 if (Defs)
535 return &*Defs->rbegin();
536 }
537 return Result;
538 }
539
540 /// Result of calling walkToPhiOrClobber.
541 struct UpwardsWalkResult {
542 /// The "Result" of the walk. Either a clobber, the last thing we walked, or
543 /// both. Include alias info when clobber found.
544 MemoryAccess *Result;
545 bool IsKnownClobber;
546 };
547
548 /// Walk to the next Phi or Clobber in the def chain starting at Desc.Last.
549 /// This will update Desc.Last as it walks. It will (optionally) also stop at
550 /// StopAt.
551 ///
552 /// This does not test for whether StopAt is a clobber
553 UpwardsWalkResult
554 walkToPhiOrClobber(DefPath &Desc, const MemoryAccess *StopAt = nullptr,
555 const MemoryAccess *SkipStopAt = nullptr) const {
556 assert(!isa<MemoryUse>(Desc.Last) && "Uses don't exist in my world");
557 assert(UpwardWalkLimit && "Need a valid walk limit");
558 bool LimitAlreadyReached = false;
559 // (*UpwardWalkLimit) may be 0 here, due to the loop in tryOptimizePhi. Set
560 // it to 1. This will not do any alias() calls. It either returns in the
561 // first iteration in the loop below, or is set back to 0 if all def chains
562 // are free of MemoryDefs.
563 if (!*UpwardWalkLimit) {
564 *UpwardWalkLimit = 1;
565 LimitAlreadyReached = true;
566 }
567
568 for (MemoryAccess *Current : def_chain(Desc.Last)) {
569 Desc.Last = Current;
570 if (Current == StopAt || Current == SkipStopAt)
571 return {Current, false};
572
573 if (auto *MD = dyn_cast<MemoryDef>(Current)) {
574 if (MSSA.isLiveOnEntryDef(MD))
575 return {MD, true};
576
577 if (!--*UpwardWalkLimit)
578 return {Current, true};
579
580 BatchAACrossIterationScope _(*AA, Desc.MayBeCrossIteration);
581 if (instructionClobbersQuery(MD, Desc.Loc, Query->Inst, *AA))
582 return {MD, true};
583 }
584 }
585
586 if (LimitAlreadyReached)
587 *UpwardWalkLimit = 0;
588
589 assert(isa<MemoryPhi>(Desc.Last) &&
590 "Ended at a non-clobber that's not a phi?");
591 return {Desc.Last, false};
592 }
593
594 void addSearches(MemoryPhi *Phi, SmallVectorImpl<ListIndex> &PausedSearches,
595 ListIndex PriorNode, bool MayBeCrossIteration) {
596 auto UpwardDefsBegin =
597 upward_defs_begin({Phi, Paths[PriorNode].Loc, MayBeCrossIteration}, DT);
598 auto UpwardDefs = make_range(UpwardDefsBegin, upward_defs_end());
599 for (const UpwardDefsElem &E : UpwardDefs) {
600 PausedSearches.push_back(Paths.size());
601 Paths.emplace_back(E.Loc, E.MA, E.MayBeCrossIteration, PriorNode);
602 }
603 }
604
605 /// Represents a search that terminated after finding a clobber. This clobber
606 /// may or may not be present in the path of defs from LastNode..SearchStart,
607 /// since it may have been retrieved from cache.
608 struct TerminatedPath {
609 MemoryAccess *Clobber;
610 ListIndex LastNode;
611 };
612
613 /// Get an access that keeps us from optimizing to the given phi.
614 ///
615 /// PausedSearches is an array of indices into the Paths array. Its incoming
616 /// value is the indices of searches that stopped at the last phi optimization
617 /// target. It's left in an unspecified state.
618 ///
619 /// If this returns std::nullopt, NewPaused is a vector of searches that
620 /// terminated at StopWhere. Otherwise, NewPaused is left in an unspecified
621 /// state.
622 std::optional<TerminatedPath>
623 getBlockingAccess(const MemoryAccess *StopWhere,
624 SmallVectorImpl<ListIndex> &PausedSearches,
625 SmallVectorImpl<ListIndex> &NewPaused,
626 SmallVectorImpl<TerminatedPath> &Terminated) {
627 assert(!PausedSearches.empty() && "No searches to continue?");
628
629 // BFS vs DFS really doesn't make a difference here, so just do a DFS with
630 // PausedSearches as our stack.
631 while (!PausedSearches.empty()) {
632 ListIndex PathIndex = PausedSearches.pop_back_val();
633 DefPath &Node = Paths[PathIndex];
634
635 // If we've already visited this path with this MemoryLocation, we don't
636 // need to do so again.
637 //
638 // NOTE: That we just drop these paths on the ground makes caching
639 // behavior sporadic. e.g. given a diamond:
640 // A
641 // B C
642 // D
643 //
644 // ...If we walk D, B, A, C, we'll only cache the result of phi
645 // optimization for A, B, and D; C will be skipped because it dies here.
646 // This arguably isn't the worst thing ever, since:
647 // - We generally query things in a top-down order, so if we got below D
648 // without needing cache entries for {C, MemLoc}, then chances are
649 // that those cache entries would end up ultimately unused.
650 // - We still cache things for A, so C only needs to walk up a bit.
651 // If this behavior becomes problematic, we can fix without a ton of extra
652 // work.
653 if (!VisitedPhis.insert({Node.Last, Node.Loc, Node.MayBeCrossIteration})
654 .second)
655 continue;
656
657 const MemoryAccess *SkipStopWhere = nullptr;
658 if (Query->SkipSelfAccess && Node.Loc == Query->StartingLoc) {
659 assert(isa<MemoryDef>(Query->OriginalAccess));
660 SkipStopWhere = Query->OriginalAccess;
661 }
662
663 UpwardsWalkResult Res = walkToPhiOrClobber(Node,
664 /*StopAt=*/StopWhere,
665 /*SkipStopAt=*/SkipStopWhere);
666 if (Res.IsKnownClobber) {
667 assert(Res.Result != StopWhere && Res.Result != SkipStopWhere);
668
669 // If this wasn't a cache hit, we hit a clobber when walking. That's a
670 // failure.
671 TerminatedPath Term{Res.Result, PathIndex};
672 if (!MSSA.dominates(Res.Result, StopWhere))
673 return Term;
674
675 // Otherwise, it's a valid thing to potentially optimize to.
676 Terminated.push_back(Term);
677 continue;
678 }
679
680 if (Res.Result == StopWhere || Res.Result == SkipStopWhere) {
681 // We've hit our target. Save this path off for if we want to continue
682 // walking. If we are in the mode of skipping the OriginalAccess, and
683 // we've reached back to the OriginalAccess, do not save path, we've
684 // just looped back to self.
685 if (Res.Result != SkipStopWhere)
686 NewPaused.push_back(PathIndex);
687 continue;
688 }
689
690 assert(!MSSA.isLiveOnEntryDef(Res.Result) && "liveOnEntry is a clobber");
691 addSearches(cast<MemoryPhi>(Res.Result), PausedSearches, PathIndex,
692 Node.MayBeCrossIteration);
693 }
694
695 return std::nullopt;
696 }
697
698 template <typename T, typename Walker>
699 struct generic_def_path_iterator
700 : public iterator_facade_base<generic_def_path_iterator<T, Walker>,
701 std::forward_iterator_tag, T *> {
702 generic_def_path_iterator() = default;
703 generic_def_path_iterator(Walker *W, ListIndex N) : W(W), N(N) {}
704
705 T &operator*() const { return curNode(); }
706
707 generic_def_path_iterator &operator++() {
708 N = curNode().Previous;
709 return *this;
710 }
711
712 bool operator==(const generic_def_path_iterator &O) const {
713 if (N.has_value() != O.N.has_value())
714 return false;
715 return !N || *N == *O.N;
716 }
717
718 private:
719 T &curNode() const { return W->Paths[*N]; }
720
721 Walker *W = nullptr;
722 std::optional<ListIndex> N;
723 };
724
725 using def_path_iterator = generic_def_path_iterator<DefPath, ClobberWalker>;
726 using const_def_path_iterator =
727 generic_def_path_iterator<const DefPath, const ClobberWalker>;
728
729 iterator_range<def_path_iterator> def_path(ListIndex From) {
730 return make_range(def_path_iterator(this, From), def_path_iterator());
731 }
732
733 iterator_range<const_def_path_iterator> const_def_path(ListIndex From) const {
734 return make_range(const_def_path_iterator(this, From),
735 const_def_path_iterator());
736 }
737
738 struct OptznResult {
739 /// The path that contains our result.
740 TerminatedPath PrimaryClobber;
741 /// The paths that we can legally cache back from, but that aren't
742 /// necessarily the result of the Phi optimization.
743 SmallVector<TerminatedPath, 4> OtherClobbers;
744 };
745
746 ListIndex defPathIndex(const DefPath &N) const {
747 // The assert looks nicer if we don't need to do &N
748 const DefPath *NP = &N;
749 assert(!Paths.empty() && NP >= &Paths.front() && NP <= &Paths.back() &&
750 "Out of bounds DefPath!");
751 return NP - &Paths.front();
752 }
753
754 /// Try to optimize a phi as best as we can. Returns a SmallVector of Paths
755 /// that act as legal clobbers. Note that this won't return *all* clobbers.
756 ///
757 /// Phi optimization algorithm tl;dr:
758 /// - Find the earliest def/phi, A, we can optimize to
759 /// - Find if all paths from the starting memory access ultimately reach A
760 /// - If not, optimization isn't possible.
761 /// - Otherwise, walk from A to another clobber or phi, A'.
762 /// - If A' is a def, we're done.
763 /// - If A' is a phi, try to optimize it.
764 ///
765 /// A path is a series of {MemoryAccess, MemoryLocation} pairs. A path
766 /// terminates when a MemoryAccess that clobbers said MemoryLocation is found.
767 OptznResult tryOptimizePhi(MemoryPhi *Phi, MemoryAccess *Start,
768 const MemoryLocation &Loc) {
769 assert(Paths.empty() && VisitedPhis.empty() &&
770 "Reset the optimization state.");
771
772 Paths.emplace_back(Loc, Start, Phi, /*MayBeCrossIteration=*/false,
773 std::nullopt);
774 // Stores how many "valid" optimization nodes we had prior to calling
775 // addSearches/getBlockingAccess. Necessary for caching if we had a blocker.
776 auto PriorPathsSize = Paths.size();
777
778 SmallVector<ListIndex, 16> PausedSearches;
780 SmallVector<TerminatedPath, 4> TerminatedPaths;
781
782 addSearches(Phi, PausedSearches, 0, /*MayBeCrossIteration=*/false);
783
784 // Moves the TerminatedPath with the "most dominated" Clobber to the end of
785 // Paths.
786 auto MoveDominatedPathToEnd = [&](SmallVectorImpl<TerminatedPath> &Paths) {
787 assert(!Paths.empty() && "Need a path to move");
788 auto Dom = Paths.begin();
789 for (auto I = std::next(Dom), E = Paths.end(); I != E; ++I)
790 if (!MSSA.dominates(I->Clobber, Dom->Clobber))
791 Dom = I;
792 auto Last = Paths.end() - 1;
793 if (Last != Dom)
794 std::iter_swap(Last, Dom);
795 };
796
797 MemoryPhi *Current = Phi;
798 while (true) {
799 assert(!MSSA.isLiveOnEntryDef(Current) &&
800 "liveOnEntry wasn't treated as a clobber?");
801
802 const auto *Target = getWalkTarget(Current);
803 // If a TerminatedPath doesn't dominate Target, then it wasn't a legal
804 // optimization for the prior phi.
805 assert(all_of(TerminatedPaths, [&](const TerminatedPath &P) {
806 return MSSA.dominates(P.Clobber, Target);
807 }));
808
809 // FIXME: This is broken, because the Blocker may be reported to be
810 // liveOnEntry, and we'll happily wait for that to disappear (read: never)
811 // For the moment, this is fine, since we do nothing with blocker info.
812 if (std::optional<TerminatedPath> Blocker = getBlockingAccess(
813 Target, PausedSearches, NewPaused, TerminatedPaths)) {
814
815 // Find the node we started at. We can't search based on N->Last, since
816 // we may have gone around a loop with a different MemoryLocation.
817 auto Iter = find_if(def_path(Blocker->LastNode), [&](const DefPath &N) {
818 return defPathIndex(N) < PriorPathsSize;
819 });
820 assert(Iter != def_path_iterator());
821
822 DefPath &CurNode = *Iter;
823 assert(CurNode.Last == Current);
824
825 // Two things:
826 // A. We can't reliably cache all of NewPaused back. Consider a case
827 // where we have two paths in NewPaused; one of which can't optimize
828 // above this phi, whereas the other can. If we cache the second path
829 // back, we'll end up with suboptimal cache entries. We can handle
830 // cases like this a bit better when we either try to find all
831 // clobbers that block phi optimization, or when our cache starts
832 // supporting unfinished searches.
833 // B. We can't reliably cache TerminatedPaths back here without doing
834 // extra checks; consider a case like:
835 // T
836 // / \
837 // D C
838 // \ /
839 // S
840 // Where T is our target, C is a node with a clobber on it, D is a
841 // diamond (with a clobber *only* on the left or right node, N), and
842 // S is our start. Say we walk to D, through the node opposite N
843 // (read: ignoring the clobber), and see a cache entry in the top
844 // node of D. That cache entry gets put into TerminatedPaths. We then
845 // walk up to C (N is later in our worklist), find the clobber, and
846 // quit. If we append TerminatedPaths to OtherClobbers, we'll cache
847 // the bottom part of D to the cached clobber, ignoring the clobber
848 // in N. Again, this problem goes away if we start tracking all
849 // blockers for a given phi optimization.
850 TerminatedPath Result{CurNode.Last, defPathIndex(CurNode)};
851 return {Result, {}};
852 }
853
854 // If there's nothing left to search, then all paths led to valid clobbers
855 // that we got from our cache; pick the nearest to the start, and allow
856 // the rest to be cached back.
857 if (NewPaused.empty()) {
858 MoveDominatedPathToEnd(TerminatedPaths);
859 TerminatedPath Result = TerminatedPaths.pop_back_val();
860 return {Result, std::move(TerminatedPaths)};
861 }
862
863 MemoryAccess *DefChainEnd = nullptr;
865 for (ListIndex Paused : NewPaused) {
866 UpwardsWalkResult WR = walkToPhiOrClobber(Paths[Paused]);
867 if (WR.IsKnownClobber)
868 Clobbers.push_back({WR.Result, Paused});
869 else
870 // Micro-opt: If we hit the end of the chain, save it.
871 DefChainEnd = WR.Result;
872 }
873
874 if (!TerminatedPaths.empty()) {
875 // If we couldn't find the dominating phi/liveOnEntry in the above loop,
876 // do it now.
877 if (!DefChainEnd)
878 for (auto *MA : def_chain(const_cast<MemoryAccess *>(Target)))
879 DefChainEnd = MA;
880 assert(DefChainEnd && "Failed to find dominating phi/liveOnEntry");
881
882 // If any of the terminated paths don't dominate the phi we'll try to
883 // optimize, we need to figure out what they are and quit.
884 const BasicBlock *ChainBB = DefChainEnd->getBlock();
885 for (const TerminatedPath &TP : TerminatedPaths) {
886 // Because we know that DefChainEnd is as "high" as we can go, we
887 // don't need local dominance checks; BB dominance is sufficient.
888 if (DT.dominates(ChainBB, TP.Clobber->getBlock()))
889 Clobbers.push_back(TP);
890 }
891 }
892
893 // If we have clobbers in the def chain, find the one closest to Current
894 // and quit.
895 if (!Clobbers.empty()) {
896 MoveDominatedPathToEnd(Clobbers);
897 TerminatedPath Result = Clobbers.pop_back_val();
898 return {Result, std::move(Clobbers)};
899 }
900
901 assert(all_of(NewPaused,
902 [&](ListIndex I) { return Paths[I].Last == DefChainEnd; }));
903
904 // Because liveOnEntry is a clobber, this must be a phi.
905 auto *DefChainPhi = cast<MemoryPhi>(DefChainEnd);
906
907 PriorPathsSize = Paths.size();
908 PausedSearches.clear();
909 for (ListIndex I : NewPaused)
910 addSearches(DefChainPhi, PausedSearches, I,
911 Paths[I].MayBeCrossIteration);
912 NewPaused.clear();
913
914 Current = DefChainPhi;
915 }
916 }
917
918 void verifyOptResult(const OptznResult &R) const {
919 assert(all_of(R.OtherClobbers, [&](const TerminatedPath &P) {
920 return MSSA.dominates(P.Clobber, R.PrimaryClobber.Clobber);
921 }));
922 }
923
924 void resetPhiOptznState() {
925 Paths.clear();
926 VisitedPhis.clear();
927 }
928
929public:
930 ClobberWalker(const MemorySSA &MSSA, DominatorTree &DT)
931 : MSSA(MSSA), DT(DT) {}
932
933 /// Finds the nearest clobber for the given query, optimizing phis if
934 /// possible.
935 MemoryAccess *findClobber(BatchAAResults &BAA, MemoryAccess *Start,
936 UpwardsMemoryQuery &Q, unsigned &UpWalkLimit) {
937 AA = &BAA;
938 Query = &Q;
939 UpwardWalkLimit = &UpWalkLimit;
940 // Starting limit must be > 0.
941 if (!UpWalkLimit)
942 UpWalkLimit++;
943
944 MemoryAccess *Current = Start;
945 // This walker pretends uses don't exist. If we're handed one, silently grab
946 // its def. (This has the nice side-effect of ensuring we never cache uses)
947 if (auto *MU = dyn_cast<MemoryUse>(Start))
948 Current = MU->getDefiningAccess();
949
950 DefPath FirstDesc(Q.StartingLoc, Current, Current,
951 /*MayBeCrossIteration=*/false, std::nullopt);
952 // Fast path for the overly-common case (no crazy phi optimization
953 // necessary)
954 UpwardsWalkResult WalkResult = walkToPhiOrClobber(FirstDesc);
955 MemoryAccess *Result;
956 if (WalkResult.IsKnownClobber) {
957 Result = WalkResult.Result;
958 } else {
959 OptznResult OptRes = tryOptimizePhi(cast<MemoryPhi>(FirstDesc.Last),
960 Current, Q.StartingLoc);
961 verifyOptResult(OptRes);
962 resetPhiOptznState();
963 Result = OptRes.PrimaryClobber.Clobber;
964 }
965
966#ifdef EXPENSIVE_CHECKS
967 if (!Q.SkipSelfAccess && *UpwardWalkLimit > 0)
968 checkClobberSanity(Current, Result, Q.StartingLoc, MSSA, Q, BAA);
969#endif
970 return Result;
971 }
972};
973
974struct RenamePassData {
975 DomTreeNode *DTN;
976 DomTreeNode::const_iterator ChildIt;
977 MemoryAccess *IncomingVal;
978
979 RenamePassData(DomTreeNode *D, DomTreeNode::const_iterator It,
980 MemoryAccess *M)
981 : DTN(D), ChildIt(It), IncomingVal(M) {}
982
983 void swap(RenamePassData &RHS) {
984 std::swap(DTN, RHS.DTN);
985 std::swap(ChildIt, RHS.ChildIt);
986 std::swap(IncomingVal, RHS.IncomingVal);
987 }
988};
989
990} // end anonymous namespace
991
992namespace llvm {
993
995 ClobberWalker Walker;
996 MemorySSA *MSSA;
997
998public:
999 ClobberWalkerBase(MemorySSA *M, DominatorTree *D) : Walker(*M, *D), MSSA(M) {}
1000
1002 const MemoryLocation &,
1003 BatchAAResults &, unsigned &);
1004 // Third argument (bool), defines whether the clobber search should skip the
1005 // original queried access. If true, there will be a follow-up query searching
1006 // for a clobber access past "self". Note that the Optimized access is not
1007 // updated if a new clobber is found by this SkipSelf search. If this
1008 // additional query becomes heavily used we may decide to cache the result.
1009 // Walker instantiations will decide how to set the SkipSelf bool.
1011 unsigned &, bool,
1012 bool UseInvariantGroup = true);
1013};
1014
1015/// A MemorySSAWalker that does AA walks to disambiguate accesses. It no
1016/// longer does caching on its own, but the name has been retained for the
1017/// moment.
1019 ClobberWalkerBase *Walker;
1020
1021public:
1024 ~CachingWalker() override = default;
1025
1027
1029 unsigned &UWL) {
1030 return Walker->getClobberingMemoryAccessBase(MA, BAA, UWL, false);
1031 }
1033 const MemoryLocation &Loc,
1034 BatchAAResults &BAA, unsigned &UWL) {
1035 return Walker->getClobberingMemoryAccessBase(MA, Loc, BAA, UWL);
1036 }
1037 // This method is not accessible outside of this file.
1039 MemoryAccess *MA, BatchAAResults &BAA, unsigned &UWL) {
1040 return Walker->getClobberingMemoryAccessBase(MA, BAA, UWL, false, false);
1041 }
1042
1044 BatchAAResults &BAA) override {
1045 unsigned UpwardWalkLimit = MaxCheckLimit;
1046 return getClobberingMemoryAccess(MA, BAA, UpwardWalkLimit);
1047 }
1049 const MemoryLocation &Loc,
1050 BatchAAResults &BAA) override {
1051 unsigned UpwardWalkLimit = MaxCheckLimit;
1052 return getClobberingMemoryAccess(MA, Loc, BAA, UpwardWalkLimit);
1053 }
1054
1055 void invalidateInfo(MemoryAccess *MA) override {
1056 if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
1057 MUD->resetOptimized();
1058 }
1059};
1060
1062 ClobberWalkerBase *Walker;
1063
1064public:
1067 ~SkipSelfWalker() override = default;
1068
1070
1072 unsigned &UWL) {
1073 return Walker->getClobberingMemoryAccessBase(MA, BAA, UWL, true);
1074 }
1076 const MemoryLocation &Loc,
1077 BatchAAResults &BAA, unsigned &UWL) {
1078 return Walker->getClobberingMemoryAccessBase(MA, Loc, BAA, UWL);
1079 }
1080
1082 BatchAAResults &BAA) override {
1083 unsigned UpwardWalkLimit = MaxCheckLimit;
1084 return getClobberingMemoryAccess(MA, BAA, UpwardWalkLimit);
1085 }
1087 const MemoryLocation &Loc,
1088 BatchAAResults &BAA) override {
1089 unsigned UpwardWalkLimit = MaxCheckLimit;
1090 return getClobberingMemoryAccess(MA, Loc, BAA, UpwardWalkLimit);
1091 }
1092
1093 void invalidateInfo(MemoryAccess *MA) override {
1094 if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
1095 MUD->resetOptimized();
1096 }
1097};
1098
1099} // end namespace llvm
1100
1101void MemorySSA::renameSuccessorPhis(BasicBlock *BB, MemoryAccess *IncomingVal,
1102 bool RenameAllUses) {
1103 // Pass through values to our successors
1104 for (const BasicBlock *S : successors(BB)) {
1105 auto It = PerBlockAccesses.find(S);
1106 // Rename the phi nodes in our successor block
1107 if (It == PerBlockAccesses.end() || !isa<MemoryPhi>(It->second->front()))
1108 continue;
1109 AccessList *Accesses = It->second.get();
1110 auto *Phi = cast<MemoryPhi>(&Accesses->front());
1111 if (RenameAllUses) {
1112 bool ReplacementDone = false;
1113 for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I)
1114 if (Phi->getIncomingBlock(I) == BB) {
1115 Phi->setIncomingValue(I, IncomingVal);
1116 ReplacementDone = true;
1117 }
1118 (void) ReplacementDone;
1119 assert(ReplacementDone && "Incomplete phi during partial rename");
1120 } else
1121 Phi->addIncoming(IncomingVal, BB);
1122 }
1123}
1124
1125/// Rename a single basic block into MemorySSA form.
1126/// Uses the standard SSA renaming algorithm.
1127/// \returns The new incoming value.
1128MemoryAccess *MemorySSA::renameBlock(BasicBlock *BB, MemoryAccess *IncomingVal,
1129 bool RenameAllUses) {
1130 auto It = PerBlockAccesses.find(BB);
1131 // Skip most processing if the list is empty.
1132 if (It != PerBlockAccesses.end()) {
1133 AccessList *Accesses = It->second.get();
1134 for (MemoryAccess &L : *Accesses) {
1135 if (MemoryUseOrDef *MUD = dyn_cast<MemoryUseOrDef>(&L)) {
1136 if (MUD->getDefiningAccess() == nullptr || RenameAllUses)
1137 MUD->setDefiningAccess(IncomingVal);
1138 if (isa<MemoryDef>(&L))
1139 IncomingVal = &L;
1140 } else {
1141 IncomingVal = &L;
1142 }
1143 }
1144 }
1145 return IncomingVal;
1146}
1147
1148/// This is the standard SSA renaming algorithm.
1149///
1150/// We walk the dominator tree in preorder, renaming accesses, and then filling
1151/// in phi nodes in our successors.
1152void MemorySSA::renamePass(DomTreeNode *Root, MemoryAccess *IncomingVal,
1154 bool SkipVisited, bool RenameAllUses) {
1155 assert(Root && "Trying to rename accesses in an unreachable block");
1156
1158 // Skip everything if we already renamed this block and we are skipping.
1159 // Note: You can't sink this into the if, because we need it to occur
1160 // regardless of whether we skip blocks or not.
1161 bool AlreadyVisited = !Visited.insert(Root->getBlock()).second;
1162 if (SkipVisited && AlreadyVisited)
1163 return;
1164
1165 IncomingVal = renameBlock(Root->getBlock(), IncomingVal, RenameAllUses);
1166 renameSuccessorPhis(Root->getBlock(), IncomingVal, RenameAllUses);
1167 WorkStack.push_back({Root, Root->begin(), IncomingVal});
1168
1169 while (!WorkStack.empty()) {
1170 DomTreeNode *Node = WorkStack.back().DTN;
1171 DomTreeNode::const_iterator ChildIt = WorkStack.back().ChildIt;
1172 IncomingVal = WorkStack.back().IncomingVal;
1173
1174 if (ChildIt == Node->end()) {
1175 WorkStack.pop_back();
1176 } else {
1177 DomTreeNode *Child = *ChildIt;
1178 ++WorkStack.back().ChildIt;
1179 BasicBlock *BB = Child->getBlock();
1180 // Note: You can't sink this into the if, because we need it to occur
1181 // regardless of whether we skip blocks or not.
1182 AlreadyVisited = !Visited.insert(BB).second;
1183 if (SkipVisited && AlreadyVisited) {
1184 // We already visited this during our renaming, which can happen when
1185 // being asked to rename multiple blocks. Figure out the incoming val,
1186 // which is the last def.
1187 // Incoming value can only change if there is a block def, and in that
1188 // case, it's the last block def in the list.
1189 if (auto *BlockDefs = getBlockDefs(BB))
1190 IncomingVal = &*BlockDefs->rbegin();
1191 } else
1192 IncomingVal = renameBlock(BB, IncomingVal, RenameAllUses);
1193 renameSuccessorPhis(BB, IncomingVal, RenameAllUses);
1194 WorkStack.push_back({Child, Child->begin(), IncomingVal});
1195 }
1196 }
1197}
1198
1199/// This handles unreachable block accesses by deleting phi nodes in
1200/// unreachable blocks, and marking all other unreachable MemoryAccess's as
1201/// being uses of the live on entry definition.
1202void MemorySSA::markUnreachableAsLiveOnEntry(BasicBlock *BB) {
1203 assert(!DT->isReachableFromEntry(BB) &&
1204 "Reachable block found while handling unreachable blocks");
1205
1206 // Make sure phi nodes in our reachable successors end up with a
1207 // LiveOnEntryDef for our incoming edge, even though our block is forward
1208 // unreachable. We could just disconnect these blocks from the CFG fully,
1209 // but we do not right now.
1210 for (const BasicBlock *S : successors(BB)) {
1211 if (!DT->isReachableFromEntry(S))
1212 continue;
1213 auto It = PerBlockAccesses.find(S);
1214 // Rename the phi nodes in our successor block
1215 if (It == PerBlockAccesses.end() || !isa<MemoryPhi>(It->second->front()))
1216 continue;
1217 AccessList *Accesses = It->second.get();
1218 auto *Phi = cast<MemoryPhi>(&Accesses->front());
1219 Phi->addIncoming(LiveOnEntryDef.get(), BB);
1220 }
1221
1222 auto It = PerBlockAccesses.find(BB);
1223 if (It == PerBlockAccesses.end())
1224 return;
1225
1226 auto &Accesses = It->second;
1227 for (auto AI = Accesses->begin(), AE = Accesses->end(); AI != AE;) {
1228 auto Next = std::next(AI);
1229 // If we have a phi, just remove it. We are going to replace all
1230 // users with live on entry.
1231 if (auto *UseOrDef = dyn_cast<MemoryUseOrDef>(AI))
1232 UseOrDef->setDefiningAccess(LiveOnEntryDef.get());
1233 else
1234 Accesses->erase(AI);
1235 AI = Next;
1236 }
1237}
1238
1240 : DT(DT), F(&Func), LiveOnEntryDef(nullptr), Walker(nullptr),
1241 SkipWalker(nullptr) {
1242 // Build MemorySSA using a batch alias analysis. This reuses the internal
1243 // state that AA collects during an alias()/getModRefInfo() call. This is
1244 // safe because there are no CFG changes while building MemorySSA and can
1245 // significantly reduce the time spent by the compiler in AA, because we will
1246 // make queries about all the instructions in the Function.
1247 assert(AA && "No alias analysis?");
1248 BatchAAResults BatchAA(*AA);
1249 buildMemorySSA(BatchAA, iterator_range(F->begin(), F->end()));
1250 // Intentionally leave AA to nullptr while building so we don't accidentally
1251 // use non-batch AliasAnalysis.
1252 this->AA = AA;
1253 // Also create the walker here.
1254 getWalker();
1255}
1256
1258 : DT(DT), L(&L), LiveOnEntryDef(nullptr), Walker(nullptr),
1259 SkipWalker(nullptr) {
1260 // Build MemorySSA using a batch alias analysis. This reuses the internal
1261 // state that AA collects during an alias()/getModRefInfo() call. This is
1262 // safe because there are no CFG changes while building MemorySSA and can
1263 // significantly reduce the time spent by the compiler in AA, because we will
1264 // make queries about all the instructions in the Function.
1265 assert(AA && "No alias analysis?");
1266 BatchAAResults BatchAA(*AA);
1267 buildMemorySSA(
1268 BatchAA, map_range(L.blocks(), [](const BasicBlock *BB) -> BasicBlock & {
1269 return *const_cast<BasicBlock *>(BB);
1270 }));
1271 // Intentionally leave AA to nullptr while building so we don't accidentally
1272 // use non-batch AliasAnalysis.
1273 this->AA = AA;
1274 // Also create the walker here.
1275 getWalker();
1276}
1277
1279 // Drop all our references
1280 for (const auto &Pair : PerBlockAccesses)
1281 for (MemoryAccess &MA : *Pair.second)
1282 MA.dropAllReferences();
1283}
1284
1285MemorySSA::AccessList *MemorySSA::getOrCreateAccessList(const BasicBlock *BB) {
1286 auto Res = PerBlockAccesses.try_emplace(BB);
1287
1288 if (Res.second)
1289 Res.first->second = std::make_unique<AccessList>();
1290 return Res.first->second.get();
1291}
1292
1293MemorySSA::DefsList *MemorySSA::getOrCreateDefsList(const BasicBlock *BB) {
1294 auto Res = PerBlockDefs.try_emplace(BB);
1295
1296 if (Res.second)
1297 Res.first->second = std::make_unique<DefsList>();
1298 return Res.first->second.get();
1299}
1300
1301namespace llvm {
1302
1303/// This class is a batch walker of all MemoryUse's in the program, and points
1304/// their defining access at the thing that actually clobbers them. Because it
1305/// is a batch walker that touches everything, it does not operate like the
1306/// other walkers. This walker is basically performing a top-down SSA renaming
1307/// pass, where the version stack is used as the cache. This enables it to be
1308/// significantly more time and memory efficient than using the regular walker,
1309/// which is walking bottom-up.
1311public:
1313 DominatorTree *DT)
1314 : MSSA(MSSA), Walker(Walker), AA(BAA), DT(DT) {}
1315
1316 void optimizeUses();
1317
1318private:
1319 /// This represents where a given memorylocation is in the stack.
1320 struct MemlocStackInfo {
1321 // This essentially is keeping track of versions of the stack. Whenever
1322 // the stack changes due to pushes or pops, these versions increase.
1323 unsigned long StackEpoch;
1324 unsigned long PopEpoch;
1325 // This is the lower bound of places on the stack to check. It is equal to
1326 // the place the last stack walk ended.
1327 // Note: Correctness depends on this being initialized to 0, which densemap
1328 // does
1329 unsigned long LowerBound;
1330 const BasicBlock *LowerBoundBlock;
1331 // This is where the last walk for this memory location ended.
1332 unsigned long LastKill;
1333 bool LastKillValid;
1334 };
1335
1336 void optimizeUsesInBlock(const BasicBlock *, unsigned long &, unsigned long &,
1339
1340 MemorySSA *MSSA;
1341 CachingWalker *Walker;
1343 DominatorTree *DT;
1344};
1345
1346} // end namespace llvm
1347
1348/// Optimize the uses in a given block This is basically the SSA renaming
1349/// algorithm, with one caveat: We are able to use a single stack for all
1350/// MemoryUses. This is because the set of *possible* reaching MemoryDefs is
1351/// the same for every MemoryUse. The *actual* clobbering MemoryDef is just
1352/// going to be some position in that stack of possible ones.
1353///
1354/// We track the stack positions that each MemoryLocation needs
1355/// to check, and last ended at. This is because we only want to check the
1356/// things that changed since last time. The same MemoryLocation should
1357/// get clobbered by the same store (getModRefInfo does not use invariantness or
1358/// things like this, and if they start, we can modify MemoryLocOrCall to
1359/// include relevant data)
1360void MemorySSA::OptimizeUses::optimizeUsesInBlock(
1361 const BasicBlock *BB, unsigned long &StackEpoch, unsigned long &PopEpoch,
1362 SmallVectorImpl<MemoryAccess *> &VersionStack,
1364
1365 /// If no accesses, nothing to do.
1366 MemorySSA::AccessList *Accesses = MSSA->getBlockAccesses(BB);
1367 if (Accesses == nullptr)
1368 return;
1369
1370 // Pop everything that doesn't dominate the current block off the stack,
1371 // increment the PopEpoch to account for this.
1372 while (true) {
1373 assert(
1374 !VersionStack.empty() &&
1375 "Version stack should have liveOnEntry sentinel dominating everything");
1376 BasicBlock *BackBlock = VersionStack.back()->getBlock();
1377 if (DT->dominates(BackBlock, BB))
1378 break;
1379 while (VersionStack.back()->getBlock() == BackBlock)
1380 VersionStack.pop_back();
1381 ++PopEpoch;
1382 }
1383
1384 for (MemoryAccess &MA : *Accesses) {
1385 auto *MU = dyn_cast<MemoryUse>(&MA);
1386 if (!MU) {
1387 VersionStack.push_back(&MA);
1388 ++StackEpoch;
1389 continue;
1390 }
1391
1392 if (MU->isOptimized())
1393 continue;
1394
1395 MemoryLocOrCall UseMLOC(MU);
1396 auto &LocInfo = LocStackInfo[UseMLOC];
1397 // If the pop epoch changed, it means we've removed stuff from top of
1398 // stack due to changing blocks. We may have to reset the lower bound or
1399 // last kill info.
1400 if (LocInfo.PopEpoch != PopEpoch) {
1401 LocInfo.PopEpoch = PopEpoch;
1402 LocInfo.StackEpoch = StackEpoch;
1403 // If the lower bound was in something that no longer dominates us, we
1404 // have to reset it.
1405 // We can't simply track stack size, because the stack may have had
1406 // pushes/pops in the meantime.
1407 // XXX: This is non-optimal, but only is slower cases with heavily
1408 // branching dominator trees. To get the optimal number of queries would
1409 // be to make lowerbound and lastkill a per-loc stack, and pop it until
1410 // the top of that stack dominates us. This does not seem worth it ATM.
1411 // A much cheaper optimization would be to always explore the deepest
1412 // branch of the dominator tree first. This will guarantee this resets on
1413 // the smallest set of blocks.
1414 if (LocInfo.LowerBoundBlock && LocInfo.LowerBoundBlock != BB &&
1415 !DT->dominates(LocInfo.LowerBoundBlock, BB)) {
1416 // Reset the lower bound of things to check.
1417 // TODO: Some day we should be able to reset to last kill, rather than
1418 // 0.
1419 LocInfo.LowerBound = 0;
1420 LocInfo.LowerBoundBlock = VersionStack[0]->getBlock();
1421 LocInfo.LastKillValid = false;
1422 }
1423 } else if (LocInfo.StackEpoch != StackEpoch) {
1424 // If all that has changed is the StackEpoch, we only have to check the
1425 // new things on the stack, because we've checked everything before. In
1426 // this case, the lower bound of things to check remains the same.
1427 LocInfo.PopEpoch = PopEpoch;
1428 LocInfo.StackEpoch = StackEpoch;
1429 }
1430 if (!LocInfo.LastKillValid) {
1431 LocInfo.LastKill = VersionStack.size() - 1;
1432 LocInfo.LastKillValid = true;
1433 }
1434
1435 // At this point, we should have corrected last kill and LowerBound to be
1436 // in bounds.
1437 assert(LocInfo.LowerBound < VersionStack.size() &&
1438 "Lower bound out of range");
1439 assert(LocInfo.LastKill < VersionStack.size() &&
1440 "Last kill info out of range");
1441 // In any case, the new upper bound is the top of the stack.
1442 unsigned long UpperBound = VersionStack.size() - 1;
1443
1444 if (UpperBound - LocInfo.LowerBound > MaxCheckLimit) {
1445 LLVM_DEBUG(dbgs() << "MemorySSA skipping optimization of " << *MU << " ("
1446 << *(MU->getMemoryInst()) << ")"
1447 << " because there are "
1448 << UpperBound - LocInfo.LowerBound
1449 << " stores to disambiguate\n");
1450 // Because we did not walk, LastKill is no longer valid, as this may
1451 // have been a kill.
1452 LocInfo.LastKillValid = false;
1453 continue;
1454 }
1455 bool FoundClobberResult = false;
1456 unsigned UpwardWalkLimit = MaxCheckLimit;
1457 while (UpperBound > LocInfo.LowerBound) {
1458 if (isa<MemoryPhi>(VersionStack[UpperBound])) {
1459 // For phis, use the walker, see where we ended up, go there.
1460 // The invariant.group handling in MemorySSA is ad-hoc and doesn't
1461 // support updates, so don't use it to optimize uses.
1462 MemoryAccess *Result =
1463 Walker->getClobberingMemoryAccessWithoutInvariantGroup(
1464 MU, *AA, UpwardWalkLimit);
1465 // We are guaranteed to find it or something is wrong.
1466 while (VersionStack[UpperBound] != Result) {
1467 assert(UpperBound != 0);
1468 --UpperBound;
1469 }
1470 FoundClobberResult = true;
1471 break;
1472 }
1473
1474 MemoryDef *MD = cast<MemoryDef>(VersionStack[UpperBound]);
1475 if (instructionClobbersQuery(MD, MU, UseMLOC, *AA)) {
1476 FoundClobberResult = true;
1477 break;
1478 }
1479 --UpperBound;
1480 }
1481
1482 // At the end of this loop, UpperBound is either a clobber, or lower bound
1483 // PHI walking may cause it to be < LowerBound, and in fact, < LastKill.
1484 if (FoundClobberResult || UpperBound < LocInfo.LastKill) {
1485 MU->setDefiningAccess(VersionStack[UpperBound], true);
1486 LocInfo.LastKill = UpperBound;
1487 } else {
1488 // Otherwise, we checked all the new ones, and now we know we can get to
1489 // LastKill.
1490 MU->setDefiningAccess(VersionStack[LocInfo.LastKill], true);
1491 }
1492 LocInfo.LowerBound = VersionStack.size() - 1;
1493 LocInfo.LowerBoundBlock = BB;
1494 }
1495}
1496
1497/// Optimize uses to point to their actual clobbering definitions.
1501 VersionStack.push_back(MSSA->getLiveOnEntryDef());
1502
1503 unsigned long StackEpoch = 1;
1504 unsigned long PopEpoch = 1;
1505 // We perform a non-recursive top-down dominator tree walk.
1506 for (const auto *DomNode : depth_first(DT->getRootNode()))
1507 optimizeUsesInBlock(DomNode->getBlock(), StackEpoch, PopEpoch, VersionStack,
1508 LocStackInfo);
1509}
1510
1511void MemorySSA::placePHINodes(
1512 const SmallPtrSetImpl<BasicBlock *> &DefiningBlocks) {
1513 // Determine where our MemoryPhi's should go
1514 ForwardIDFCalculator IDFs(*DT);
1515 IDFs.setDefiningBlocks(DefiningBlocks);
1517 IDFs.calculate(IDFBlocks);
1518
1519 // Now place MemoryPhi nodes.
1520 for (auto &BB : IDFBlocks)
1521 createMemoryPhi(BB);
1522}
1523
1524template <typename IterT>
1525void MemorySSA::buildMemorySSA(BatchAAResults &BAA, IterT Blocks) {
1526 // We create an access to represent "live on entry", for things like
1527 // arguments or users of globals, where the memory they use is defined before
1528 // the beginning of the function. We do not actually insert it into the IR.
1529 // We do not define a live on exit for the immediate uses, and thus our
1530 // semantics do *not* imply that something with no immediate uses can simply
1531 // be removed.
1532 BasicBlock &StartingPoint = *Blocks.begin();
1533 LiveOnEntryDef.reset(new MemoryDef(StartingPoint.getContext(), nullptr,
1534 nullptr, &StartingPoint, NextID++));
1535
1536 // We maintain lists of memory accesses per-block, trading memory for time. We
1537 // could just look up the memory access for every possible instruction in the
1538 // stream.
1539 SmallPtrSet<BasicBlock *, 32> DefiningBlocks;
1540 // Go through each block, figure out where defs occur, and chain together all
1541 // the accesses.
1542 for (BasicBlock &B : Blocks) {
1543 bool InsertIntoDef = false;
1544 AccessList *Accesses = nullptr;
1545 DefsList *Defs = nullptr;
1546 for (Instruction &I : B) {
1547 MemoryUseOrDef *MUD = createNewAccess(&I, &BAA);
1548 if (!MUD)
1549 continue;
1550
1551 if (!Accesses)
1552 Accesses = getOrCreateAccessList(&B);
1553 Accesses->push_back(MUD);
1554 if (isa<MemoryDef>(MUD)) {
1555 InsertIntoDef = true;
1556 if (!Defs)
1557 Defs = getOrCreateDefsList(&B);
1558 Defs->push_back(*MUD);
1559 }
1560 }
1561 if (InsertIntoDef)
1562 DefiningBlocks.insert(&B);
1563 }
1564 placePHINodes(DefiningBlocks);
1565
1566 // Now do regular SSA renaming on the MemoryDef/MemoryUse. Visited will get
1567 // filled in with all blocks.
1568 SmallPtrSet<BasicBlock *, 16> Visited;
1569 if (L) {
1570 // Only building MemorySSA for a single loop. placePHINodes may have
1571 // inserted a MemoryPhi in the loop's preheader. As this is outside the
1572 // scope of the loop, set them to LiveOnEntry.
1573 if (auto *P = getMemoryAccess(L->getLoopPreheader())) {
1574 for (Use &U : make_early_inc_range(P->uses()))
1575 U.set(LiveOnEntryDef.get());
1577 }
1578 // Now rename accesses in the loop. Populate Visited with the exit blocks of
1579 // the loop, to limit the scope of the renaming.
1580 SmallVector<BasicBlock *> ExitBlocks;
1581 L->getExitBlocks(ExitBlocks);
1582 Visited.insert_range(ExitBlocks);
1583 renamePass(DT->getNode(L->getLoopPreheader()), LiveOnEntryDef.get(),
1584 Visited);
1585 } else {
1586 renamePass(DT->getRootNode(), LiveOnEntryDef.get(), Visited);
1587 }
1588
1589 // Mark the uses in unreachable blocks as live on entry, so that they go
1590 // somewhere.
1591 for (auto &BB : Blocks)
1592 if (!Visited.count(&BB))
1593 markUnreachableAsLiveOnEntry(&BB);
1594}
1595
1596MemorySSAWalker *MemorySSA::getWalker() { return getWalkerImpl(); }
1597
1598MemorySSA::CachingWalker *MemorySSA::getWalkerImpl() {
1599 if (Walker)
1600 return Walker.get();
1601
1602 if (!WalkerBase)
1603 WalkerBase = std::make_unique<ClobberWalkerBase>(this, DT);
1604
1605 Walker = std::make_unique<CachingWalker>(this, WalkerBase.get());
1606 return Walker.get();
1607}
1608
1610 if (SkipWalker)
1611 return SkipWalker.get();
1612
1613 if (!WalkerBase)
1614 WalkerBase = std::make_unique<ClobberWalkerBase>(this, DT);
1615
1616 SkipWalker = std::make_unique<SkipSelfWalker>(this, WalkerBase.get());
1617 return SkipWalker.get();
1618 }
1619
1620
1621// This is a helper function used by the creation routines. It places NewAccess
1622// into the access and defs lists for a given basic block, at the given
1623// insertion point.
1625 const BasicBlock *BB,
1626 InsertionPlace Point) {
1627 auto *Accesses = getOrCreateAccessList(BB);
1628 if (Point == Beginning) {
1629 // If it's a phi node, it goes first, otherwise, it goes after any phi
1630 // nodes.
1631 if (isa<MemoryPhi>(NewAccess)) {
1632 Accesses->push_front(NewAccess);
1633 auto *Defs = getOrCreateDefsList(BB);
1634 Defs->push_front(*NewAccess);
1635 } else {
1636 auto AI = find_if_not(
1637 *Accesses, [](const MemoryAccess &MA) { return isa<MemoryPhi>(MA); });
1638 Accesses->insert(AI, NewAccess);
1639 if (!isa<MemoryUse>(NewAccess)) {
1640 auto *Defs = getOrCreateDefsList(BB);
1641 auto DI = find_if_not(
1642 *Defs, [](const MemoryAccess &MA) { return isa<MemoryPhi>(MA); });
1643 Defs->insert(DI, *NewAccess);
1644 }
1645 }
1646 } else {
1647 Accesses->push_back(NewAccess);
1648 if (!isa<MemoryUse>(NewAccess)) {
1649 auto *Defs = getOrCreateDefsList(BB);
1650 Defs->push_back(*NewAccess);
1651 }
1652 }
1653 BlockNumberingValid.erase(BB);
1654}
1655
1657 AccessList::iterator InsertPt) {
1658 auto *Accesses = getBlockAccesses(BB);
1659 bool WasEnd = InsertPt == Accesses->end();
1660 Accesses->insert(AccessList::iterator(InsertPt), What);
1661 if (!isa<MemoryUse>(What)) {
1662 auto *Defs = getOrCreateDefsList(BB);
1663 // If we got asked to insert at the end, we have an easy job, just shove it
1664 // at the end. If we got asked to insert before an existing def, we also get
1665 // an iterator. If we got asked to insert before a use, we have to hunt for
1666 // the next def.
1667 if (WasEnd) {
1668 Defs->push_back(*What);
1669 } else if (isa<MemoryDef>(InsertPt)) {
1670 Defs->insert(InsertPt->getDefsIterator(), *What);
1671 } else {
1672 while (InsertPt != Accesses->end() && !isa<MemoryDef>(InsertPt))
1673 ++InsertPt;
1674 // Either we found a def, or we are inserting at the end
1675 if (InsertPt == Accesses->end())
1676 Defs->push_back(*What);
1677 else
1678 Defs->insert(InsertPt->getDefsIterator(), *What);
1679 }
1680 }
1681 BlockNumberingValid.erase(BB);
1682}
1683
1684void MemorySSA::prepareForMoveTo(MemoryAccess *What, BasicBlock *BB) {
1685 // Keep it in the lookup tables, remove from the lists
1686 removeFromLists(What, false);
1687
1688 // Note that moving should implicitly invalidate the optimized state of a
1689 // MemoryUse (and Phis can't be optimized). However, it doesn't do so for a
1690 // MemoryDef.
1691 if (auto *MD = dyn_cast<MemoryDef>(What))
1692 MD->resetOptimized();
1693 What->setBlock(BB);
1694}
1695
1696// Move What before Where in the IR. The end result is that What will belong to
1697// the right lists and have the right Block set, but will not otherwise be
1698// correct. It will not have the right defining access, and if it is a def,
1699// things below it will not properly be updated.
1701 AccessList::iterator Where) {
1702 prepareForMoveTo(What, BB);
1703 insertIntoListsBefore(What, BB, Where);
1704}
1705
1707 InsertionPlace Point) {
1708 if (isa<MemoryPhi>(What)) {
1709 assert(Point == Beginning &&
1710 "Can only move a Phi at the beginning of the block");
1711 // Update lookup table entry
1712 ValueToMemoryAccess.erase(What->getBlock());
1713 bool Inserted = ValueToMemoryAccess.insert({BB, What}).second;
1714 (void)Inserted;
1715 assert(Inserted && "Cannot move a Phi to a block that already has one");
1716 }
1717
1718 prepareForMoveTo(What, BB);
1719 insertIntoListsForBlock(What, BB, Point);
1720}
1721
1722MemoryPhi *MemorySSA::createMemoryPhi(BasicBlock *BB) {
1723 assert(!getMemoryAccess(BB) && "MemoryPhi already exists for this BB");
1724 MemoryPhi *Phi = new MemoryPhi(BB->getContext(), BB, NextID++);
1725 // Phi's always are placed at the front of the block.
1727 ValueToMemoryAccess[BB] = Phi;
1728 return Phi;
1729}
1730
1732 MemoryAccess *Definition,
1733 const MemoryUseOrDef *Template,
1734 bool CreationMustSucceed) {
1735 assert(!isa<PHINode>(I) && "Cannot create a defined access for a PHI");
1736 MemoryUseOrDef *NewAccess = createNewAccess(I, AA, Template);
1737 if (CreationMustSucceed)
1738 assert(NewAccess != nullptr && "Tried to create a memory access for a "
1739 "non-memory touching instruction");
1740 if (NewAccess) {
1741 assert((!Definition || !isa<MemoryUse>(Definition)) &&
1742 "A use cannot be a defining access");
1743 NewAccess->setDefiningAccess(Definition);
1744 }
1745 return NewAccess;
1746}
1747
1748// Return true if the instruction has ordering constraints.
1749// Note specifically that this only considers stores and loads
1750// because others are still considered ModRef by getModRefInfo.
1751static inline bool isOrdered(const Instruction *I) {
1752 if (auto *SI = dyn_cast<StoreInst>(I)) {
1753 if (!SI->isUnordered())
1754 return true;
1755 } else if (auto *LI = dyn_cast<LoadInst>(I)) {
1756 if (!LI->isUnordered())
1757 return true;
1758 }
1759 return false;
1760}
1761
1762/// Helper function to create new memory accesses
1763template <typename AliasAnalysisType>
1764MemoryUseOrDef *MemorySSA::createNewAccess(Instruction *I,
1765 AliasAnalysisType *AAP,
1766 const MemoryUseOrDef *Template) {
1767 // The assume intrinsic has a control dependency which we model by claiming
1768 // that it writes arbitrarily. Debuginfo intrinsics may be considered
1769 // clobbers when we have a nonstandard AA pipeline. Ignore these fake memory
1770 // dependencies here.
1771 // FIXME: Replace this special casing with a more accurate modelling of
1772 // assume's control dependency.
1773 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1774 switch (II->getIntrinsicID()) {
1775 default:
1776 break;
1777 case Intrinsic::allow_runtime_check:
1778 case Intrinsic::allow_ubsan_check:
1779 case Intrinsic::assume:
1780 case Intrinsic::experimental_noalias_scope_decl:
1781 case Intrinsic::pseudoprobe:
1782 return nullptr;
1783 }
1784 }
1785
1786 // Using a nonstandard AA pipelines might leave us with unexpected modref
1787 // results for I, so add a check to not model instructions that may not read
1788 // from or write to memory. This is necessary for correctness.
1789 if (!I->mayReadFromMemory() && !I->mayWriteToMemory())
1790 return nullptr;
1791
1792 bool Def, Use;
1793 if (Template) {
1796#if !defined(NDEBUG)
1797 ModRefInfo ModRef = AAP->getModRefInfo(I, std::nullopt);
1798 bool DefCheck, UseCheck;
1799 DefCheck = isModSet(ModRef) || isOrdered(I);
1800 UseCheck = isRefSet(ModRef);
1801 // Memory accesses should only be reduced and can not be increased since AA
1802 // just might return better results as a result of some transformations.
1803 assert((Def == DefCheck || !DefCheck) &&
1804 "Memory accesses should only be reduced");
1805 if (!Def && Use != UseCheck) {
1806 // New Access should not have more power than template access
1807 assert(!UseCheck && "Invalid template");
1808 }
1809#endif
1810 } else {
1811 // Find out what affect this instruction has on memory.
1812 ModRefInfo ModRef = AAP->getModRefInfo(I, std::nullopt);
1813 // The isOrdered check is used to ensure that volatiles end up as defs
1814 // (atomics end up as ModRef right now anyway). Until we separate the
1815 // ordering chain from the memory chain, this enables people to see at least
1816 // some relative ordering to volatiles. Note that getClobberingMemoryAccess
1817 // will still give an answer that bypasses other volatile loads. TODO:
1818 // Separate memory aliasing and ordering into two different chains so that
1819 // we can precisely represent both "what memory will this read/write/is
1820 // clobbered by" and "what instructions can I move this past".
1821 Def = isModSet(ModRef) || isOrdered(I);
1822 Use = isRefSet(ModRef);
1823 }
1824
1825 // It's possible for an instruction to not modify memory at all. During
1826 // construction, we ignore them.
1827 if (!Def && !Use)
1828 return nullptr;
1829
1830 MemoryUseOrDef *MUD;
1831 if (Def) {
1832 MUD = new MemoryDef(I->getContext(), nullptr, I, I->getParent(), NextID++);
1833 } else {
1834 MUD = new MemoryUse(I->getContext(), nullptr, I, I->getParent());
1836 MemoryAccess *LiveOnEntry = getLiveOnEntryDef();
1837 MUD->setOptimized(LiveOnEntry);
1838 }
1839 }
1840 ValueToMemoryAccess[I] = MUD;
1841 return MUD;
1842}
1843
1844/// Properly remove \p MA from all of MemorySSA's lookup tables.
1846 assert(MA->use_empty() &&
1847 "Trying to remove memory access that still has uses");
1848 BlockNumbering.erase(MA);
1849 if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
1850 MUD->setDefiningAccess(nullptr);
1851 // Invalidate our walker's cache if necessary
1852 if (!isa<MemoryUse>(MA))
1854
1855 Value *MemoryInst;
1856 if (const auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
1857 MemoryInst = MUD->getMemoryInst();
1858 else
1859 MemoryInst = MA->getBlock();
1860
1861 auto VMA = ValueToMemoryAccess.find(MemoryInst);
1862 if (VMA->second == MA)
1863 ValueToMemoryAccess.erase(VMA);
1864}
1865
1866/// Properly remove \p MA from all of MemorySSA's lists.
1867///
1868/// Because of the way the intrusive list and use lists work, it is important to
1869/// do removal in the right order.
1870/// ShouldDelete defaults to true, and will cause the memory access to also be
1871/// deleted, not just removed.
1872void MemorySSA::removeFromLists(MemoryAccess *MA, bool ShouldDelete) {
1873 BasicBlock *BB = MA->getBlock();
1874 // The access list owns the reference, so we erase it from the non-owning list
1875 // first.
1876 if (!isa<MemoryUse>(MA)) {
1877 auto DefsIt = PerBlockDefs.find(BB);
1878 std::unique_ptr<DefsList> &Defs = DefsIt->second;
1879 Defs->remove(*MA);
1880 if (Defs->empty())
1881 PerBlockDefs.erase(DefsIt);
1882 }
1883
1884 // The erase call here will delete it. If we don't want it deleted, we call
1885 // remove instead.
1886 auto AccessIt = PerBlockAccesses.find(BB);
1887 std::unique_ptr<AccessList> &Accesses = AccessIt->second;
1888 if (ShouldDelete)
1889 Accesses->erase(MA);
1890 else
1891 Accesses->remove(MA);
1892
1893 if (Accesses->empty()) {
1894 PerBlockAccesses.erase(AccessIt);
1895 BlockNumberingValid.erase(BB);
1896 }
1897}
1898
1900 MemorySSAAnnotatedWriter Writer(this);
1901 Function *F = this->F;
1902 if (L)
1903 F = L->getHeader()->getParent();
1904 F->print(OS, &Writer);
1905}
1906
1907#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1909#endif
1910
1912#if !defined(NDEBUG) && defined(EXPENSIVE_CHECKS)
1914#endif
1915
1916#ifndef NDEBUG
1917 if (F) {
1918 auto Blocks = iterator_range(F->begin(), F->end());
1921 if (VL == VerificationLevel::Full)
1922 verifyPrevDefInPhis(Blocks);
1923 } else {
1924 assert(L && "must either have loop or function");
1925 auto Blocks =
1926 map_range(L->blocks(), [](const BasicBlock *BB) -> BasicBlock & {
1927 return *const_cast<BasicBlock *>(BB);
1928 });
1931 if (VL == VerificationLevel::Full)
1932 verifyPrevDefInPhis(Blocks);
1933 }
1934#endif
1935 // Previously, the verification used to also verify that the clobberingAccess
1936 // cached by MemorySSA is the same as the clobberingAccess found at a later
1937 // query to AA. This does not hold true in general due to the current fragility
1938 // of BasicAA which has arbitrary caps on the things it analyzes before giving
1939 // up. As a result, transformations that are correct, will lead to BasicAA
1940 // returning different Alias answers before and after that transformation.
1941 // Invalidating MemorySSA is not an option, as the results in BasicAA can be so
1942 // random, in the worst case we'd need to rebuild MemorySSA from scratch after
1943 // every transformation, which defeats the purpose of using it. For such an
1944 // example, see test4 added in D51960.
1945}
1946
1947template <typename IterT>
1948void MemorySSA::verifyPrevDefInPhis(IterT Blocks) const {
1949 for (const BasicBlock &BB : Blocks) {
1950 if (MemoryPhi *Phi = getMemoryAccess(&BB)) {
1951 for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) {
1952 auto *Pred = Phi->getIncomingBlock(I);
1953 auto *IncAcc = Phi->getIncomingValue(I);
1954 // If Pred has no unreachable predecessors, get last def looking at
1955 // IDoms. If, while walkings IDoms, any of these has an unreachable
1956 // predecessor, then the incoming def can be any access.
1957 if (auto *DTNode = DT->getNode(Pred)) {
1958 while (DTNode) {
1959 if (auto *DefList = getBlockDefs(DTNode->getBlock())) {
1960 auto *LastAcc = &*(--DefList->end());
1961 assert(LastAcc == IncAcc &&
1962 "Incorrect incoming access into phi.");
1963 (void)IncAcc;
1964 (void)LastAcc;
1965 break;
1966 }
1967 DTNode = DTNode->getIDom();
1968 }
1969 } else {
1970 // If Pred has unreachable predecessors, but has at least a Def, the
1971 // incoming access can be the last Def in Pred, or it could have been
1972 // optimized to LoE. After an update, though, the LoE may have been
1973 // replaced by another access, so IncAcc may be any access.
1974 // If Pred has unreachable predecessors and no Defs, incoming access
1975 // should be LoE; However, after an update, it may be any access.
1976 }
1977 }
1978 }
1979 }
1980}
1981
1982/// Verify that all of the blocks we believe to have valid domination numbers
1983/// actually have valid domination numbers.
1984template <typename IterT>
1985void MemorySSA::verifyDominationNumbers(IterT Blocks) const {
1986 if (BlockNumberingValid.empty())
1987 return;
1988
1989 SmallPtrSet<const BasicBlock *, 16> ValidBlocks = BlockNumberingValid;
1990 for (const BasicBlock &BB : Blocks) {
1991 if (!ValidBlocks.count(&BB))
1992 continue;
1993
1994 ValidBlocks.erase(&BB);
1995
1996 const AccessList *Accesses = getBlockAccesses(&BB);
1997 // It's correct to say an empty block has valid numbering.
1998 if (!Accesses)
1999 continue;
2000
2001 // Block numbering starts at 1.
2002 unsigned long LastNumber = 0;
2003 for (const MemoryAccess &MA : *Accesses) {
2004 auto ThisNumberIter = BlockNumbering.find(&MA);
2005 assert(ThisNumberIter != BlockNumbering.end() &&
2006 "MemoryAccess has no domination number in a valid block!");
2007
2008 unsigned long ThisNumber = ThisNumberIter->second;
2009 assert(ThisNumber > LastNumber &&
2010 "Domination numbers should be strictly increasing!");
2011 (void)LastNumber;
2012 LastNumber = ThisNumber;
2013 }
2014 }
2015
2016 assert(ValidBlocks.empty() &&
2017 "All valid BasicBlocks should exist in F -- dangling pointers?");
2018}
2019
2020/// Verify ordering: the order and existence of MemoryAccesses matches the
2021/// order and existence of memory affecting instructions.
2022/// Verify domination: each definition dominates all of its uses.
2023/// Verify def-uses: the immediate use information - walk all the memory
2024/// accesses and verifying that, for each use, it appears in the appropriate
2025/// def's use list
2026template <typename IterT>
2028 VerificationLevel VL) const {
2029 // Walk all the blocks, comparing what the lookups think and what the access
2030 // lists think, as well as the order in the blocks vs the order in the access
2031 // lists.
2032 SmallVector<MemoryAccess *, 32> ActualAccesses;
2034 for (BasicBlock &B : Blocks) {
2035 const AccessList *AL = getBlockAccesses(&B);
2036 const auto *DL = getBlockDefs(&B);
2037 MemoryPhi *Phi = getMemoryAccess(&B);
2038 if (Phi) {
2039 // Verify ordering.
2040 ActualAccesses.push_back(Phi);
2041 ActualDefs.push_back(Phi);
2042 // Verify domination
2043 for (const Use &U : Phi->uses()) {
2044 assert(dominates(Phi, U) && "Memory PHI does not dominate it's uses");
2045 (void)U;
2046 }
2047 // Verify def-uses for full verify.
2048 if (VL == VerificationLevel::Full) {
2049 assert(Phi->getNumOperands() == pred_size(&B) &&
2050 "Incomplete MemoryPhi Node");
2051 for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) {
2052 verifyUseInDefs(Phi->getIncomingValue(I), Phi);
2053 assert(is_contained(predecessors(&B), Phi->getIncomingBlock(I)) &&
2054 "Incoming phi block not a block predecessor");
2055 }
2056 }
2057 }
2058
2059 for (Instruction &I : B) {
2061 assert((!MA || (AL && (isa<MemoryUse>(MA) || DL))) &&
2062 "We have memory affecting instructions "
2063 "in this block but they are not in the "
2064 "access list or defs list");
2065 if (MA) {
2066 // Verify ordering.
2067 ActualAccesses.push_back(MA);
2068 if (MemoryAccess *MD = dyn_cast<MemoryDef>(MA)) {
2069 // Verify ordering.
2070 ActualDefs.push_back(MA);
2071 // Verify domination.
2072 for (const Use &U : MD->uses()) {
2073 assert(dominates(MD, U) &&
2074 "Memory Def does not dominate it's uses");
2075 (void)U;
2076 }
2077 }
2078 // Verify def-uses for full verify.
2079 if (VL == VerificationLevel::Full)
2080 verifyUseInDefs(MA->getDefiningAccess(), MA);
2081 }
2082 }
2083 // Either we hit the assert, really have no accesses, or we have both
2084 // accesses and an access list. Same with defs.
2085 if (!AL && !DL)
2086 continue;
2087 // Verify ordering.
2088 assert(AL->size() == ActualAccesses.size() &&
2089 "We don't have the same number of accesses in the block as on the "
2090 "access list");
2091 assert((DL || ActualDefs.size() == 0) &&
2092 "Either we should have a defs list, or we should have no defs");
2093 assert((!DL || DL->size() == ActualDefs.size()) &&
2094 "We don't have the same number of defs in the block as on the "
2095 "def list");
2096 auto ALI = AL->begin();
2097 auto AAI = ActualAccesses.begin();
2098 while (ALI != AL->end() && AAI != ActualAccesses.end()) {
2099 assert(&*ALI == *AAI && "Not the same accesses in the same order");
2100 ++ALI;
2101 ++AAI;
2102 }
2103 ActualAccesses.clear();
2104 if (DL) {
2105 auto DLI = DL->begin();
2106 auto ADI = ActualDefs.begin();
2107 while (DLI != DL->end() && ADI != ActualDefs.end()) {
2108 assert(&*DLI == *ADI && "Not the same defs in the same order");
2109 ++DLI;
2110 ++ADI;
2111 }
2112 }
2113 ActualDefs.clear();
2114 }
2115}
2116
2117/// Verify the def-use lists in MemorySSA, by verifying that \p Use
2118/// appears in the use list of \p Def.
2119void MemorySSA::verifyUseInDefs(MemoryAccess *Def, MemoryAccess *Use) const {
2120 // The live on entry use may cause us to get a NULL def here
2121 if (!Def)
2123 "Null def but use not point to live on entry def");
2124 else
2125 assert(is_contained(Def->users(), Use) &&
2126 "Did not find use in def's use list");
2127}
2128
2129/// Perform a local numbering on blocks so that instruction ordering can be
2130/// determined in constant time.
2131/// TODO: We currently just number in order. If we numbered by N, we could
2132/// allow at least N-1 sequences of insertBefore or insertAfter (and at least
2133/// log2(N) sequences of mixed before and after) without needing to invalidate
2134/// the numbering.
2135void MemorySSA::renumberBlock(const BasicBlock *B) const {
2136 // The pre-increment ensures the numbers really start at 1.
2137 unsigned long CurrentNumber = 0;
2138 const AccessList *AL = getBlockAccesses(B);
2139 assert(AL != nullptr && "Asking to renumber an empty block");
2140 for (const auto &I : *AL)
2141 BlockNumbering[&I] = ++CurrentNumber;
2142 BlockNumberingValid.insert(B);
2143}
2144
2145/// Determine, for two memory accesses in the same block,
2146/// whether \p Dominator dominates \p Dominatee.
2147/// \returns True if \p Dominator dominates \p Dominatee.
2149 const MemoryAccess *Dominatee) const {
2150 const BasicBlock *DominatorBlock = Dominator->getBlock();
2151
2152 assert((DominatorBlock == Dominatee->getBlock()) &&
2153 "Asking for local domination when accesses are in different blocks!");
2154 // A node dominates itself.
2155 if (Dominatee == Dominator)
2156 return true;
2157
2158 // When Dominatee is defined on function entry, it is not dominated by another
2159 // memory access.
2160 if (isLiveOnEntryDef(Dominatee))
2161 return false;
2162
2163 // When Dominator is defined on function entry, it dominates the other memory
2164 // access.
2165 if (isLiveOnEntryDef(Dominator))
2166 return true;
2167
2168 if (!BlockNumberingValid.count(DominatorBlock))
2169 renumberBlock(DominatorBlock);
2170
2171 unsigned long DominatorNum = BlockNumbering.lookup(Dominator);
2172 // All numbers start with 1
2173 assert(DominatorNum != 0 && "Block was not numbered properly");
2174 unsigned long DominateeNum = BlockNumbering.lookup(Dominatee);
2175 assert(DominateeNum != 0 && "Block was not numbered properly");
2176 return DominatorNum < DominateeNum;
2177}
2178
2180 const MemoryAccess *Dominatee) const {
2181 if (Dominator == Dominatee)
2182 return true;
2183
2184 if (isLiveOnEntryDef(Dominatee))
2185 return false;
2186
2187 if (Dominator->getBlock() != Dominatee->getBlock())
2188 return DT->dominates(Dominator->getBlock(), Dominatee->getBlock());
2189 return locallyDominates(Dominator, Dominatee);
2190}
2191
2193 const Use &Dominatee) const {
2194 if (MemoryPhi *MP = dyn_cast<MemoryPhi>(Dominatee.getUser())) {
2195 BasicBlock *UseBB = MP->getIncomingBlock(Dominatee);
2196 // The def must dominate the incoming block of the phi.
2197 if (UseBB != Dominator->getBlock())
2198 return DT->dominates(Dominator->getBlock(), UseBB);
2199 // If the UseBB and the DefBB are the same, compare locally.
2200 return locallyDominates(Dominator, cast<MemoryAccess>(Dominatee));
2201 }
2202 // If it's not a PHI node use, the normal dominates can already handle it.
2203 return dominates(Dominator, cast<MemoryAccess>(Dominatee.getUser()));
2204}
2205
2207 if (IsOptimized)
2208 return;
2209
2210 BatchAAResults BatchAA(*AA);
2211 ClobberWalkerBase WalkerBase(this, DT);
2212 CachingWalker WalkerLocal(this, &WalkerBase);
2213 OptimizeUses(this, &WalkerLocal, &BatchAA, DT).optimizeUses();
2214 IsOptimized = true;
2215}
2216
2218 switch (getValueID()) {
2219 case MemoryPhiVal: return static_cast<const MemoryPhi *>(this)->print(OS);
2220 case MemoryDefVal: return static_cast<const MemoryDef *>(this)->print(OS);
2221 case MemoryUseVal: return static_cast<const MemoryUse *>(this)->print(OS);
2222 }
2223 llvm_unreachable("invalid value id");
2224}
2225
2228
2229 auto printID = [&OS](MemoryAccess *A) {
2230 if (A && A->getID())
2231 OS << A->getID();
2232 else
2233 OS << LiveOnEntryStr;
2234 };
2235
2236 OS << getID() << " = MemoryDef(";
2237 printID(UO);
2238 OS << ")";
2239
2240 if (isOptimized()) {
2241 OS << "->";
2242 printID(getOptimized());
2243 }
2244}
2245
2247 ListSeparator LS(",");
2248 OS << getID() << " = MemoryPhi(";
2249 for (const auto &Op : operands()) {
2252
2253 OS << LS << '{';
2254 if (BB->hasName())
2255 OS << BB->getName();
2256 else
2257 BB->printAsOperand(OS, false);
2258 OS << ',';
2259 if (unsigned ID = MA->getID())
2260 OS << ID;
2261 else
2262 OS << LiveOnEntryStr;
2263 OS << '}';
2264 }
2265 OS << ')';
2266}
2267
2270 OS << "MemoryUse(";
2271 if (UO && UO->getID())
2272 OS << UO->getID();
2273 else
2274 OS << LiveOnEntryStr;
2275 OS << ')';
2276}
2277
2279// Cannot completely remove virtual function even in release mode.
2280#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2281 print(dbgs());
2282 dbgs() << "\n";
2283#endif
2284}
2285
2287private:
2288 const Function &F;
2289 MemorySSAAnnotatedWriter MSSAWriter;
2290
2291public:
2293 : F(F), MSSAWriter(&MSSA) {}
2294
2295 const Function *getFunction() { return &F; }
2296 MemorySSAAnnotatedWriter &getWriter() { return MSSAWriter; }
2297};
2298
2299namespace llvm {
2300
2301template <>
2304 return &(CFGInfo->getFunction()->getEntryBlock());
2305 }
2306
2307 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
2309
2311 return nodes_iterator(CFGInfo->getFunction()->begin());
2312 }
2313
2315 return nodes_iterator(CFGInfo->getFunction()->end());
2316 }
2317
2318 static size_t size(DOTFuncMSSAInfo *CFGInfo) {
2319 return CFGInfo->getFunction()->size();
2320 }
2321};
2322
2323template <>
2325
2326 DOTGraphTraits(bool IsSimple = false) : DefaultDOTGraphTraits(IsSimple) {}
2327
2328 static std::string getGraphName(DOTFuncMSSAInfo *CFGInfo) {
2329 return "MSSA CFG for '" + CFGInfo->getFunction()->getName().str() +
2330 "' function";
2331 }
2332
2333 std::string getNodeLabel(const BasicBlock *Node, DOTFuncMSSAInfo *CFGInfo) {
2335 Node, nullptr,
2336 [CFGInfo](raw_string_ostream &OS, const BasicBlock &BB) -> void {
2337 BB.print(OS, &CFGInfo->getWriter(), true, true);
2338 },
2339 [](std::string &S, unsigned &I, unsigned Idx) -> void {
2340 std::string Str = S.substr(I, Idx - I);
2341 StringRef SR = Str;
2342 if (SR.count(" = MemoryDef(") || SR.count(" = MemoryPhi(") ||
2343 SR.count("MemoryUse("))
2344 return;
2346 });
2347 }
2348
2353
2354 /// Display the raw branch weights from PGO.
2356 DOTFuncMSSAInfo *CFGInfo) {
2357 return "";
2358 }
2359
2361 DOTFuncMSSAInfo *CFGInfo) {
2362 return getNodeLabel(Node, CFGInfo).find(';') != std::string::npos
2363 ? "style=filled, fillcolor=lightpink"
2364 : "";
2365 }
2366};
2367
2368} // namespace llvm
2369
2370AnalysisKey MemorySSAAnalysis::Key;
2371
2374 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
2375 auto &AA = AM.getResult<AAManager>(F);
2376 return MemorySSAAnalysis::Result(std::make_unique<MemorySSA>(F, &AA, &DT));
2377}
2378
2380 Function &F, const PreservedAnalyses &PA,
2381 FunctionAnalysisManager::Invalidator &Inv) {
2382 auto PAC = PA.getChecker<MemorySSAAnalysis>();
2383 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
2384 Inv.invalidate<AAManager>(F, PA) ||
2385 Inv.invalidate<DominatorTreeAnalysis>(F, PA);
2386}
2387
2390 auto &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
2391 if (EnsureOptimizedUses)
2392 MSSA.ensureOptimizedUses();
2393 if (DotCFGMSSA != "") {
2394 DOTFuncMSSAInfo CFGInfo(F, MSSA);
2395 WriteGraph(&CFGInfo, "", false, "MSSA", DotCFGMSSA);
2396 } else {
2397 OS << "MemorySSA for function: " << F.getName() << "\n";
2398 MSSA.print(OS);
2399 }
2400
2401 return PreservedAnalyses::all();
2402}
2403
2406 auto &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
2407 OS << "MemorySSA (walker) for function: " << F.getName() << "\n";
2408 MemorySSAWalkerAnnotatedWriter Writer(&MSSA);
2409 F.print(OS, &Writer);
2410
2411 return PreservedAnalyses::all();
2412}
2413
2416 AM.getResult<MemorySSAAnalysis>(F).getMSSA().verifyMemorySSA();
2417
2418 return PreservedAnalyses::all();
2419}
2420
2422
2424
2426
2432
2434 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2435 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
2436 MSSA.reset(new MemorySSA(F, &AA, &DT));
2437 return false;
2438}
2439
2441 if (VerifyMemorySSA)
2442 MSSA->verifyMemorySSA();
2443}
2444
2446 MSSA->print(OS);
2447}
2448
2450
2451/// Walk the use-def chains starting at \p StartingAccess and find
2452/// the MemoryAccess that actually clobbers Loc.
2453///
2454/// \returns our clobbering memory access
2456 MemoryAccess *StartingAccess, const MemoryLocation &Loc,
2457 BatchAAResults &BAA, unsigned &UpwardWalkLimit) {
2458 assert(!isa<MemoryUse>(StartingAccess) && "Use cannot be defining access");
2459
2460 // If location is undefined, conservatively return starting access.
2461 if (Loc.Ptr == nullptr)
2462 return StartingAccess;
2463
2464 Instruction *I = nullptr;
2465 if (auto *StartingUseOrDef = dyn_cast<MemoryUseOrDef>(StartingAccess)) {
2466 if (MSSA->isLiveOnEntryDef(StartingUseOrDef))
2467 return StartingUseOrDef;
2468
2469 I = StartingUseOrDef->getMemoryInst();
2470
2471 // Conservatively, fences are always clobbers, so don't perform the walk if
2472 // we hit a fence.
2473 if (!isa<CallBase>(I) && I->isFenceLike())
2474 return StartingUseOrDef;
2475 }
2476
2477 UpwardsMemoryQuery Q;
2478 Q.OriginalAccess = StartingAccess;
2479 Q.StartingLoc = Loc;
2480 Q.Inst = nullptr;
2481 Q.IsCall = false;
2482
2483 // Unlike the other function, do not walk to the def of a def, because we are
2484 // handed something we already believe is the clobbering access.
2485 // We never set SkipSelf to true in Q in this method.
2486 MemoryAccess *Clobber =
2487 Walker.findClobber(BAA, StartingAccess, Q, UpwardWalkLimit);
2488 LLVM_DEBUG({
2489 dbgs() << "Clobber starting at access " << *StartingAccess << "\n";
2490 if (I)
2491 dbgs() << " for instruction " << *I << "\n";
2492 dbgs() << " is " << *Clobber << "\n";
2493 });
2494 return Clobber;
2495}
2496
2497static const Instruction *
2499 if (!I.hasMetadata(LLVMContext::MD_invariant_group) || I.isVolatile())
2500 return nullptr;
2501
2502 // We consider bitcasts and zero GEPs to be the same pointer value. Start by
2503 // stripping bitcasts and zero GEPs, then we will recursively look at loads
2504 // and stores through bitcasts and zero GEPs.
2505 Value *PointerOperand = getLoadStorePointerOperand(&I)->stripPointerCasts();
2506
2507 // It's not safe to walk the use list of a global value because function
2508 // passes aren't allowed to look outside their functions.
2509 // FIXME: this could be fixed by filtering instructions from outside of
2510 // current function.
2511 if (isa<Constant>(PointerOperand))
2512 return nullptr;
2513
2514 const Instruction *MostDominatingInstruction = &I;
2515
2516 for (const User *Us : PointerOperand->users()) {
2517 auto *U = dyn_cast<Instruction>(Us);
2518 if (!U || U == &I || !DT.dominates(U, MostDominatingInstruction))
2519 continue;
2520
2521 // If we hit a load/store with an invariant.group metadata and the same
2522 // pointer operand, we can assume that value pointed to by the pointer
2523 // operand didn't change.
2524 if (U->hasMetadata(LLVMContext::MD_invariant_group) &&
2525 getLoadStorePointerOperand(U) == PointerOperand && !U->isVolatile()) {
2526 MostDominatingInstruction = U;
2527 }
2528 }
2529
2530 return MostDominatingInstruction == &I ? nullptr : MostDominatingInstruction;
2531}
2532
2534 MemoryAccess *MA, BatchAAResults &BAA, unsigned &UpwardWalkLimit,
2535 bool SkipSelf, bool UseInvariantGroup) {
2536 auto *StartingAccess = dyn_cast<MemoryUseOrDef>(MA);
2537 // If this is a MemoryPhi, we can't do anything.
2538 if (!StartingAccess)
2539 return MA;
2540
2541 if (UseInvariantGroup) {
2543 *StartingAccess->getMemoryInst(), MSSA->getDomTree())) {
2545
2546 auto *ClobberMA = MSSA->getMemoryAccess(I);
2547 assert(ClobberMA);
2548 if (isa<MemoryUse>(ClobberMA))
2549 return ClobberMA->getDefiningAccess();
2550 return ClobberMA;
2551 }
2552 }
2553
2554 bool IsOptimized = false;
2555
2556 // If this is an already optimized use or def, return the optimized result.
2557 // Note: Currently, we store the optimized def result in a separate field,
2558 // since we can't use the defining access.
2559 if (StartingAccess->isOptimized()) {
2560 if (!SkipSelf || !isa<MemoryDef>(StartingAccess))
2561 return StartingAccess->getOptimized();
2562 IsOptimized = true;
2563 }
2564
2565 const Instruction *I = StartingAccess->getMemoryInst();
2566 // We can't sanely do anything with a fence, since they conservatively clobber
2567 // all memory, and have no locations to get pointers from to try to
2568 // disambiguate.
2569 if (!isa<CallBase>(I) && I->isFenceLike())
2570 return StartingAccess;
2571
2572 UpwardsMemoryQuery Q(I, StartingAccess);
2573
2575 MemoryAccess *LiveOnEntry = MSSA->getLiveOnEntryDef();
2576 StartingAccess->setOptimized(LiveOnEntry);
2577 return LiveOnEntry;
2578 }
2579
2580 MemoryAccess *OptimizedAccess;
2581 if (!IsOptimized) {
2582 // Start with the thing we already think clobbers this location
2583 MemoryAccess *DefiningAccess = StartingAccess->getDefiningAccess();
2584
2585 // At this point, DefiningAccess may be the live on entry def.
2586 // If it is, we will not get a better result.
2587 if (MSSA->isLiveOnEntryDef(DefiningAccess)) {
2588 StartingAccess->setOptimized(DefiningAccess);
2589 return DefiningAccess;
2590 }
2591
2592 OptimizedAccess =
2593 Walker.findClobber(BAA, DefiningAccess, Q, UpwardWalkLimit);
2594 StartingAccess->setOptimized(OptimizedAccess);
2595 } else
2596 OptimizedAccess = StartingAccess->getOptimized();
2597
2598 LLVM_DEBUG(dbgs() << "Starting Memory SSA clobber for " << *I << " is ");
2599 LLVM_DEBUG(dbgs() << *StartingAccess << "\n");
2600 LLVM_DEBUG(dbgs() << "Optimized Memory SSA clobber for " << *I << " is ");
2601 LLVM_DEBUG(dbgs() << *OptimizedAccess << "\n");
2602
2603 MemoryAccess *Result;
2604 if (SkipSelf && isa<MemoryPhi>(OptimizedAccess) &&
2605 isa<MemoryDef>(StartingAccess) && UpwardWalkLimit) {
2606 assert(isa<MemoryDef>(Q.OriginalAccess));
2607 Q.SkipSelfAccess = true;
2608 Result = Walker.findClobber(BAA, OptimizedAccess, Q, UpwardWalkLimit);
2609 } else
2610 Result = OptimizedAccess;
2611
2612 LLVM_DEBUG(dbgs() << "Result Memory SSA clobber [SkipSelf = " << SkipSelf);
2613 LLVM_DEBUG(dbgs() << "] for " << *I << " is " << *Result << "\n");
2614
2615 return Result;
2616}
2617
2618MemoryAccess *
2620 BatchAAResults &) {
2621 if (auto *Use = dyn_cast<MemoryUseOrDef>(MA))
2622 return Use->getDefiningAccess();
2623 return MA;
2624}
2625
2627 MemoryAccess *StartingAccess, const MemoryLocation &, BatchAAResults &) {
2628 if (auto *Use = dyn_cast<MemoryUseOrDef>(StartingAccess))
2629 return Use->getDefiningAccess();
2630 return StartingAccess;
2631}
2632
2633void MemoryPhi::deleteMe(DerivedUser *Self) {
2634 delete static_cast<MemoryPhi *>(Self);
2635}
2636
2637void MemoryDef::deleteMe(DerivedUser *Self) {
2638 delete static_cast<MemoryDef *>(Self);
2639}
2640
2641void MemoryUse::deleteMe(DerivedUser *Self) {
2642 delete static_cast<MemoryUse *>(Self);
2643}
2644
2645bool upward_defs_iterator::IsGuaranteedLoopInvariant(const Value *Ptr) const {
2646 auto IsGuaranteedLoopInvariantBase = [](const Value *Ptr) {
2647 Ptr = Ptr->stripPointerCasts();
2648 if (!isa<Instruction>(Ptr))
2649 return true;
2650 return isa<AllocaInst>(Ptr);
2651 };
2652
2653 Ptr = Ptr->stripPointerCasts();
2654 if (auto *I = dyn_cast<Instruction>(Ptr)) {
2655 if (I->getParent()->isEntryBlock())
2656 return true;
2657 }
2658 if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
2659 return IsGuaranteedLoopInvariantBase(GEP->getPointerOperand()) &&
2660 GEP->hasAllConstantIndices();
2661 }
2662 return IsGuaranteedLoopInvariantBase(Ptr);
2663}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Atomic ordering constants.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:661
DXIL Forward Handle Accesses
DXIL Resource Access
This file defines DenseMapInfo traits for DenseMap.
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
early cse Early CSE w MemorySSA
Hexagon Common GEP
#define _
This header defines various interfaces for pass management in LLVM.
This defines the Use class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file provides utility analysis objects describing memory locations.
static bool instructionClobbersQuery(const MemoryDef *MD, const MemoryLocation &UseLoc, const Instruction *UseInst, AliasAnalysisType &AA)
Memory static true cl::opt< unsigned > MaxCheckLimit("memssa-check-limit", cl::Hidden, cl::init(100), cl::desc("The maximum number of stores/phis MemorySSA" "will consider trying to walk past (default = 100)"))
static void checkClobberSanity(MemoryAccess *Start, MemoryAccess *ClobberAt, const MemoryLocation &StartLoc, const MemorySSA &MSSA, const UpwardsMemoryQuery &Query, BatchAAResults &AA, bool AllowImpreciseClobber=false)
Verifies that Start is clobbered by ClobberAt, and that nothing inbetween Start and ClobberAt can clo...
static cl::opt< bool, true > VerifyMemorySSAX("verify-memoryssa", cl::location(VerifyMemorySSA), cl::Hidden, cl::desc("Enable verification of MemorySSA."))
static const char LiveOnEntryStr[]
Definition MemorySSA.cpp:91
static bool isUseTriviallyOptimizableToLiveOnEntry(AliasAnalysisType &AA, const Instruction *I)
static bool areLoadsReorderable(const LoadInst *Use, const LoadInst *MayClobber)
This does one-way checks to see if Use could theoretically be hoisted above MayClobber.
static const Instruction * getInvariantGroupClobberingInstruction(Instruction &I, DominatorTree &DT)
static cl::opt< std::string > DotCFGMSSA("dot-cfg-mssa", cl::value_desc("file name for generated dot file"), cl::desc("file name for generated dot file"), cl::init(""))
static bool isOrdered(const Instruction *I)
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
#define T
static std::string getNodeLabel(const ValueInfo &VI, GlobalValueSummary *GVS)
uint64_t IntrinsicInst * II
#define P(N)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:119
Value * RHS
DOTFuncMSSAInfo(const Function &F, MemorySSA &MSSA)
const Function * getFunction()
MemorySSAAnnotatedWriter & getWriter()
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
AnalysisUsage & addRequiredTransitive()
LLVM Basic Block Representation.
Definition BasicBlock.h:62
LLVM_ABI BasicBlock::iterator erase(BasicBlock::iterator FromIt, BasicBlock::iterator ToIt)
Erases a range of instructions from FromIt to (not including) ToIt.
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:461
LLVM_ABI void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW=nullptr, bool ShouldPreserveUseListOrder=false, bool IsForDebug=false) const
Print the basic block to an output stream with an optional AssemblyAnnotationWriter.
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
Temporarily set the cross iteration mode on a BatchAA instance.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
Value * getCalledOperand() const
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
unsigned arg_size() const
Implements a dense probed hash-table based set.
Definition DenseSet.h:289
Extension point for the Value hierarchy.
Definition DerivedUser.h:27
MemoryAccess * getClobberingMemoryAccess(MemoryAccess *, BatchAAResults &) override
Does the same thing as getClobberingMemoryAccess(const Instruction *I), but takes a MemoryAccess inst...
iterator begin() const
NodeT * getBlock() const
Analysis pass which computes a DominatorTree.
Definition Dominators.h:274
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:310
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:155
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
FunctionPass(char &pid)
Definition Pass.h:316
iterator begin()
Definition Function.h:853
size_t size() const
Definition Function.h:858
iterator end()
Definition Function.h:855
Module * getParent()
Get the module that this global value is contained inside of...
A wrapper class for inspecting calls to intrinsic functions.
A helper class to return the specified delimiter string after the first invocation of operator String...
An instruction for reading from memory.
bool isVolatile() const
Return true if this is a load from a volatile memory location.
AtomicOrdering getOrdering() const
Returns the ordering constraint of this load instruction.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
LLVM_ABI void dump() const
MemoryAccess(const MemoryAccess &)=delete
friend class MemoryUse
Definition MemorySSA.h:210
friend class MemoryPhi
Definition MemorySSA.h:208
friend class MemoryDef
Definition MemorySSA.h:207
BasicBlock * getBlock() const
Definition MemorySSA.h:162
LLVM_ABI void print(raw_ostream &OS) const
unsigned getID() const
Used for debugging and tracking things about MemoryAccesses.
Definition MemorySSA.h:663
void setBlock(BasicBlock *BB)
Used by MemorySSA to change the block of a MemoryAccess when it is moved.
Definition MemorySSA.h:215
Represents a read-write access to memory, whether it is a must-alias, or a may-alias.
Definition MemorySSA.h:371
LLVM_ABI void print(raw_ostream &OS) const
void resetOptimized()
Definition MemorySSA.h:405
MemoryAccess * getOptimized() const
Definition MemorySSA.h:397
unsigned getID() const
Definition MemorySSA.h:412
bool isOptimized() const
Definition MemorySSA.h:401
Representation for a specific memory location.
static LLVM_ABI MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
Represents phi nodes for memory accesses.
Definition MemorySSA.h:479
BasicBlock * getIncomingBlock(unsigned I) const
Return incoming basic block number i.
Definition MemorySSA.h:542
LLVM_ABI void print(raw_ostream &OS) const
unsigned getID() const
Definition MemorySSA.h:634
An analysis that produces MemorySSA for a function.
Definition MemorySSA.h:922
LLVM_ABI Result run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static LLVM_ABI bool defClobbersUseOrDef(MemoryDef *MD, const MemoryUseOrDef *MU, AliasAnalysis &AA)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
This is the generic walker interface for walkers of MemorySSA.
Definition MemorySSA.h:1006
friend class MemorySSA
Definition MemorySSA.h:1086
MemoryAccess * getClobberingMemoryAccess(const Instruction *I, BatchAAResults &AA)
Given a memory Mod/Ref/ModRef'ing instruction, calling this will give you the nearest dominating Memo...
Definition MemorySSA.h:1035
LLVM_ABI MemorySSAWalker(MemorySSA *)
virtual void invalidateInfo(MemoryAccess *)
Given a memory access, invalidate anything this walker knows about that access.
Definition MemorySSA.h:1083
Legacy analysis pass which computes MemorySSA.
Definition MemorySSA.h:975
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
bool runOnFunction(Function &) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
A MemorySSAWalker that does AA walks to disambiguate accesses.
MemoryAccess * getClobberingMemoryAccess(MemoryAccess *MA, BatchAAResults &BAA) override
Does the same thing as getClobberingMemoryAccess(const Instruction *I), but takes a MemoryAccess inst...
MemoryAccess * getClobberingMemoryAccessWithoutInvariantGroup(MemoryAccess *MA, BatchAAResults &BAA, unsigned &UWL)
MemoryAccess * getClobberingMemoryAccess(MemoryAccess *MA, const MemoryLocation &Loc, BatchAAResults &BAA, unsigned &UWL)
void invalidateInfo(MemoryAccess *MA) override
Given a memory access, invalidate anything this walker knows about that access.
~CachingWalker() override=default
MemoryAccess * getClobberingMemoryAccess(MemoryAccess *MA, BatchAAResults &BAA, unsigned &UWL)
CachingWalker(MemorySSA *M, ClobberWalkerBase *W)
MemoryAccess * getClobberingMemoryAccess(MemoryAccess *MA, const MemoryLocation &Loc, BatchAAResults &BAA) override
Given a potentially clobbering memory access and a new location, calling this will give you the neare...
ClobberWalkerBase(MemorySSA *M, DominatorTree *D)
MemoryAccess * getClobberingMemoryAccessBase(MemoryAccess *, BatchAAResults &, unsigned &, bool, bool UseInvariantGroup=true)
MemoryAccess * getClobberingMemoryAccessBase(MemoryAccess *, const MemoryLocation &, BatchAAResults &, unsigned &)
This class is a batch walker of all MemoryUse's in the program, and points their defining access at t...
void optimizeUses()
Optimize uses to point to their actual clobbering definitions.
OptimizeUses(MemorySSA *MSSA, CachingWalker *Walker, BatchAAResults *BAA, DominatorTree *DT)
MemoryAccess * getClobberingMemoryAccess(MemoryAccess *MA, const MemoryLocation &Loc, BatchAAResults &BAA) override
Given a potentially clobbering memory access and a new location, calling this will give you the neare...
MemoryAccess * getClobberingMemoryAccess(MemoryAccess *MA, const MemoryLocation &Loc, BatchAAResults &BAA, unsigned &UWL)
~SkipSelfWalker() override=default
MemoryAccess * getClobberingMemoryAccess(MemoryAccess *MA, BatchAAResults &BAA, unsigned &UWL)
SkipSelfWalker(MemorySSA *M, ClobberWalkerBase *W)
MemoryAccess * getClobberingMemoryAccess(MemoryAccess *MA, BatchAAResults &BAA) override
Does the same thing as getClobberingMemoryAccess(const Instruction *I), but takes a MemoryAccess inst...
void invalidateInfo(MemoryAccess *MA) override
Given a memory access, invalidate anything this walker knows about that access.
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition MemorySSA.h:702
LLVM_ABI void dump() const
simple_ilist< MemoryAccess, ilist_tag< MSSAHelpers::DefsOnlyTag > > DefsList
Definition MemorySSA.h:754
LLVM_ABI void moveTo(MemoryUseOrDef *What, BasicBlock *BB, AccessList::iterator Where)
void renamePass(BasicBlock *BB, MemoryAccess *IncomingVal, SmallPtrSetImpl< BasicBlock * > &Visited)
Definition MemorySSA.h:822
DefsList * getBlockDefs(const BasicBlock *BB) const
Return the list of MemoryDef's and MemoryPhi's for a given basic block.
Definition MemorySSA.h:765
LLVM_ABI MemorySSAWalker * getSkipSelfWalker()
LLVM_ABI MemorySSA(Function &, AliasAnalysis *, DominatorTree *)
iplist< MemoryAccess, ilist_tag< MSSAHelpers::AllAccessTag > > AccessList
Definition MemorySSA.h:753
AccessList * getBlockAccesses(const BasicBlock *BB) const
Return the list of MemoryAccess's for a given basic block.
Definition MemorySSA.h:758
LLVM_ABI bool dominates(const MemoryAccess *A, const MemoryAccess *B) const
Given two memory accesses in potentially different blocks, determine whether MemoryAccess A dominates...
void verifyOrderingDominationAndDefUses(IterT Blocks, VerificationLevel=VerificationLevel::Fast) const
Verify ordering: the order and existence of MemoryAccesses matches the order and existence of memory ...
LLVM_ABI void verifyMemorySSA(VerificationLevel=VerificationLevel::Fast) const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
LLVM_ABI void insertIntoListsForBlock(MemoryAccess *, const BasicBlock *, InsertionPlace)
LLVM_ABI MemorySSAWalker * getWalker()
InsertionPlace
Used in various insertion functions to specify whether we are talking about the beginning or end of a...
Definition MemorySSA.h:791
LLVM_ABI void insertIntoListsBefore(MemoryAccess *, const BasicBlock *, AccessList::iterator)
LLVM_ABI MemoryUseOrDef * createDefinedAccess(Instruction *, MemoryAccess *, const MemoryUseOrDef *Template=nullptr, bool CreationMustSucceed=true)
DominatorTree & getDomTree() const
Definition MemorySSA.h:728
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
Definition MemorySSA.h:720
MemoryAccess * getLiveOnEntryDef() const
Definition MemorySSA.h:744
LLVM_ABI void removeFromLookups(MemoryAccess *)
Properly remove MA from all of MemorySSA's lookup tables.
LLVM_ABI void ensureOptimizedUses()
By default, uses are not optimized during MemorySSA construction.
void verifyDominationNumbers(IterT Blocks) const
Verify that all of the blocks we believe to have valid domination numbers actually have valid dominat...
void verifyPrevDefInPhis(IterT Blocks) const
LLVM_ABI bool locallyDominates(const MemoryAccess *A, const MemoryAccess *B) const
Given two memory accesses in the same basic block, determine whether MemoryAccess A dominates MemoryA...
LLVM_ABI void removeFromLists(MemoryAccess *, bool ShouldDelete=true)
Properly remove MA from all of MemorySSA's lists.
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.
Definition MemorySSA.h:740
LLVM_ABI ~MemorySSA()
LLVM_ABI void print(raw_ostream &) const
Class that has the common methods + fields of memory uses/defs.
Definition MemorySSA.h:250
MemoryAccess * getDefiningAccess() const
Get the access that produces the memory state used by this Use.
Definition MemorySSA.h:260
void setDefiningAccess(MemoryAccess *DMA, bool Optimized=false)
Definition MemorySSA.h:293
void setOptimized(MemoryAccess *)
Sets the optimized use for a MemoryDef.
Definition MemorySSA.h:683
Instruction * getMemoryInst() const
Get the instruction that this MemoryUse represents.
Definition MemorySSA.h:257
LLVM_ABI void print(raw_ostream &OS) const
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW, bool ShouldPreserveUseListOrder=false, bool IsForDebug=false) const
Print the module to an output stream with an optional AssemblyAnnotationWriter.
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition Analysis.h:275
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
void insert_range(Range &&R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition StringRef.h:471
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
op_range operands()
Definition User.h:267
void dropAllReferences()
Drop all references to operands.
Definition User.h:324
unsigned getNumOperands() const
Definition User.h:229
LLVM Value Representation.
Definition Value.h:75
iterator_range< user_iterator > users()
Definition Value.h:426
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition Value.h:543
LLVM_ABI void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:712
bool use_empty() const
Definition Value.h:346
iterator_range< use_iterator > uses()
Definition Value.h:380
bool hasName() const
Definition Value.h:261
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:318
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:212
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
An opaque object representing a hash code.
Definition Hashing.h:78
typename base_list_type::iterator iterator
Definition ilist.h:121
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
reverse_iterator rbegin()
CallInst * Call
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
Definition Attributor.h:165
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
This namespace contains all of the command line option processing machinery.
Definition MCSchedule.h:35
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1738
static cl::opt< unsigned long > StopAt("sbvec-stop-at", cl::init(StopAtDisabled), cl::Hidden, cl::desc("Vectorize if the invocation count is < than this. 0 " "disables vectorization."))
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
APInt operator*(APInt a, uint64_t RHS)
Definition APInt.h:2264
auto successors(const MachineBasicBlock *BB)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
auto pred_size(const MachineBasicBlock *BB)
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
Op::Description Desc
auto map_range(ContainerTy &&C, FuncTy F)
Return a range that applies F to the elements of C.
Definition STLExtras.h:365
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition Dominators.h:94
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
iterator_range< def_chain_iterator< T > > def_chain(T MA, MemoryAccess *UpTo=nullptr)
Definition MemorySSA.h:1378
bool isModSet(const ModRefInfo MRI)
Definition ModRef.h:49
auto find_if_not(R &&Range, UnaryPredicate P)
Definition STLExtras.h:1776
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool isAtLeastOrStrongerThan(AtomicOrdering AO, AtomicOrdering Other)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
IDFCalculator< false > ForwardIDFCalculator
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition ModRef.h:28
@ ModRef
The access may reference and may modify the value stored in memory.
Definition ModRef.h:36
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
LLVM_ABI bool VerifyMemorySSA
Enables verification of MemorySSA.
Definition MemorySSA.cpp:84
upward_defs_iterator upward_defs_end()
Definition MemorySSA.h:1327
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1771
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1946
Instruction::const_succ_iterator const_succ_iterator
Definition CFG.h:127
iterator_range< df_iterator< T > > depth_first(const T &G)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:325
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
upward_defs_iterator upward_defs_begin(const UpwardDefsElem &Pair, DominatorTree &DT)
Definition MemorySSA.h:1322
bool isRefSet(const ModRefInfo MRI)
Definition ModRef.h:52
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:863
#define MAP(n)
#define N
std::string getEdgeAttributes(const BasicBlock *Node, const_succ_iterator I, DOTFuncMSSAInfo *CFGInfo)
Display the raw branch weights from PGO.
std::string getNodeAttributes(const BasicBlock *Node, DOTFuncMSSAInfo *CFGInfo)
static std::string getEdgeSourceLabel(const BasicBlock *Node, const_succ_iterator I)
std::string getNodeLabel(const BasicBlock *Node, DOTFuncMSSAInfo *CFGInfo)
static std::string getGraphName(DOTFuncMSSAInfo *CFGInfo)
static std::string getEdgeSourceLabel(const void *, EdgeIter)
getEdgeSourceLabel - If you want to label the edge source itself, implement this method.
static bool isEqual(const MemoryLocOrCall &LHS, const MemoryLocOrCall &RHS)
static unsigned getHashValue(const MemoryLocOrCall &MLOC)
static MemoryLocOrCall getEmptyKey()
An information struct used to provide DenseMap with the various necessary components for a given valu...
pointer_iterator< Function::const_iterator > nodes_iterator
static size_t size(DOTFuncMSSAInfo *CFGInfo)
static NodeRef getEntryNode(DOTFuncMSSAInfo *CFGInfo)
static nodes_iterator nodes_begin(DOTFuncMSSAInfo *CFGInfo)
static nodes_iterator nodes_end(DOTFuncMSSAInfo *CFGInfo)
typename DOTFuncMSSAInfo *::UnknownGraphTypeError NodeRef
Definition GraphTraits.h:95
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)