123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371 |
- #pragma once
- #ifdef __GNUC__
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wunused-parameter"
- #endif
- //===- MemorySSA.h - Build Memory SSA ---------------------------*- C++ -*-===//
- //
- // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
- // See https://llvm.org/LICENSE.txt for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- //
- //===----------------------------------------------------------------------===//
- //
- /// \file
- /// This file exposes an interface to building/using memory SSA to
- /// walk memory instructions using a use/def graph.
- ///
- /// Memory SSA class builds an SSA form that links together memory access
- /// instructions such as loads, stores, atomics, and calls. Additionally, it
- /// does a trivial form of "heap versioning" Every time the memory state changes
- /// in the program, we generate a new heap version. It generates
- /// MemoryDef/Uses/Phis that are overlayed on top of the existing instructions.
- ///
- /// As a trivial example,
- /// define i32 @main() #0 {
- /// entry:
- /// %call = call noalias i8* @_Znwm(i64 4) #2
- /// %0 = bitcast i8* %call to i32*
- /// %call1 = call noalias i8* @_Znwm(i64 4) #2
- /// %1 = bitcast i8* %call1 to i32*
- /// store i32 5, i32* %0, align 4
- /// store i32 7, i32* %1, align 4
- /// %2 = load i32* %0, align 4
- /// %3 = load i32* %1, align 4
- /// %add = add nsw i32 %2, %3
- /// ret i32 %add
- /// }
- ///
- /// Will become
- /// define i32 @main() #0 {
- /// entry:
- /// ; 1 = MemoryDef(0)
- /// %call = call noalias i8* @_Znwm(i64 4) #3
- /// %2 = bitcast i8* %call to i32*
- /// ; 2 = MemoryDef(1)
- /// %call1 = call noalias i8* @_Znwm(i64 4) #3
- /// %4 = bitcast i8* %call1 to i32*
- /// ; 3 = MemoryDef(2)
- /// store i32 5, i32* %2, align 4
- /// ; 4 = MemoryDef(3)
- /// store i32 7, i32* %4, align 4
- /// ; MemoryUse(3)
- /// %7 = load i32* %2, align 4
- /// ; MemoryUse(4)
- /// %8 = load i32* %4, align 4
- /// %add = add nsw i32 %7, %8
- /// ret i32 %add
- /// }
- ///
- /// Given this form, all the stores that could ever effect the load at %8 can be
- /// gotten by using the MemoryUse associated with it, and walking from use to
- /// def until you hit the top of the function.
- ///
- /// Each def also has a list of users associated with it, so you can walk from
- /// both def to users, and users to defs. Note that we disambiguate MemoryUses,
- /// but not the RHS of MemoryDefs. You can see this above at %7, which would
- /// otherwise be a MemoryUse(4). Being disambiguated means that for a given
- /// store, all the MemoryUses on its use lists are may-aliases of that store
- /// (but the MemoryDefs on its use list may not be).
- ///
- /// MemoryDefs are not disambiguated because it would require multiple reaching
- /// definitions, which would require multiple phis, and multiple memoryaccesses
- /// per instruction.
- //
- //===----------------------------------------------------------------------===//
- #ifndef LLVM_ANALYSIS_MEMORYSSA_H
- #define LLVM_ANALYSIS_MEMORYSSA_H
- #include "llvm/ADT/DenseMap.h"
- #include "llvm/ADT/GraphTraits.h"
- #include "llvm/ADT/SmallPtrSet.h"
- #include "llvm/ADT/SmallVector.h"
- #include "llvm/ADT/ilist.h"
- #include "llvm/ADT/ilist_node.h"
- #include "llvm/ADT/iterator.h"
- #include "llvm/ADT/iterator_range.h"
- #include "llvm/ADT/simple_ilist.h"
- #include "llvm/Analysis/AliasAnalysis.h"
- #include "llvm/Analysis/MemoryLocation.h"
- #include "llvm/Analysis/PHITransAddr.h"
- #include "llvm/IR/BasicBlock.h"
- #include "llvm/IR/DerivedUser.h"
- #include "llvm/IR/Dominators.h"
- #include "llvm/IR/Module.h"
- #include "llvm/IR/Operator.h"
- #include "llvm/IR/Type.h"
- #include "llvm/IR/Use.h"
- #include "llvm/IR/User.h"
- #include "llvm/IR/Value.h"
- #include "llvm/IR/ValueHandle.h"
- #include "llvm/Pass.h"
- #include "llvm/Support/Casting.h"
- #include "llvm/Support/CommandLine.h"
- #include <algorithm>
- #include <cassert>
- #include <cstddef>
- #include <iterator>
- #include <memory>
- #include <utility>
- namespace llvm {
- class Function;
- class Instruction;
- class MemoryAccess;
- class MemorySSAWalker;
- class LLVMContext;
- class raw_ostream;
- namespace MSSAHelpers {
- struct AllAccessTag {};
- struct DefsOnlyTag {};
- } // end namespace MSSAHelpers
- enum : unsigned {
- // Used to signify what the default invalid ID is for MemoryAccess's
- // getID()
- INVALID_MEMORYACCESS_ID = -1U
- };
- template <class T> class memoryaccess_def_iterator_base;
- using memoryaccess_def_iterator = memoryaccess_def_iterator_base<MemoryAccess>;
- using const_memoryaccess_def_iterator =
- memoryaccess_def_iterator_base<const MemoryAccess>;
- // The base for all memory accesses. All memory accesses in a block are
- // linked together using an intrusive list.
- class MemoryAccess
- : public DerivedUser,
- public ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::AllAccessTag>>,
- public ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::DefsOnlyTag>> {
- public:
- using AllAccessType =
- ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::AllAccessTag>>;
- using DefsOnlyType =
- ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::DefsOnlyTag>>;
- MemoryAccess(const MemoryAccess &) = delete;
- MemoryAccess &operator=(const MemoryAccess &) = delete;
- void *operator new(size_t) = delete;
- // Methods for support type inquiry through isa, cast, and
- // dyn_cast
- static bool classof(const Value *V) {
- unsigned ID = V->getValueID();
- return ID == MemoryUseVal || ID == MemoryPhiVal || ID == MemoryDefVal;
- }
- BasicBlock *getBlock() const { return Block; }
- void print(raw_ostream &OS) const;
- void dump() const;
- /// The user iterators for a memory access
- using iterator = user_iterator;
- using const_iterator = const_user_iterator;
- /// This iterator walks over all of the defs in a given
- /// MemoryAccess. For MemoryPhi nodes, this walks arguments. For
- /// MemoryUse/MemoryDef, this walks the defining access.
- memoryaccess_def_iterator defs_begin();
- const_memoryaccess_def_iterator defs_begin() const;
- memoryaccess_def_iterator defs_end();
- const_memoryaccess_def_iterator defs_end() const;
- /// Get the iterators for the all access list and the defs only list
- /// We default to the all access list.
- AllAccessType::self_iterator getIterator() {
- return this->AllAccessType::getIterator();
- }
- AllAccessType::const_self_iterator getIterator() const {
- return this->AllAccessType::getIterator();
- }
- AllAccessType::reverse_self_iterator getReverseIterator() {
- return this->AllAccessType::getReverseIterator();
- }
- AllAccessType::const_reverse_self_iterator getReverseIterator() const {
- return this->AllAccessType::getReverseIterator();
- }
- DefsOnlyType::self_iterator getDefsIterator() {
- return this->DefsOnlyType::getIterator();
- }
- DefsOnlyType::const_self_iterator getDefsIterator() const {
- return this->DefsOnlyType::getIterator();
- }
- DefsOnlyType::reverse_self_iterator getReverseDefsIterator() {
- return this->DefsOnlyType::getReverseIterator();
- }
- DefsOnlyType::const_reverse_self_iterator getReverseDefsIterator() const {
- return this->DefsOnlyType::getReverseIterator();
- }
- protected:
- friend class MemoryDef;
- friend class MemoryPhi;
- friend class MemorySSA;
- friend class MemoryUse;
- friend class MemoryUseOrDef;
- /// Used by MemorySSA to change the block of a MemoryAccess when it is
- /// moved.
- void setBlock(BasicBlock *BB) { Block = BB; }
- /// Used for debugging and tracking things about MemoryAccesses.
- /// Guaranteed unique among MemoryAccesses, no guarantees otherwise.
- inline unsigned getID() const;
- MemoryAccess(LLVMContext &C, unsigned Vty, DeleteValueTy DeleteValue,
- BasicBlock *BB, unsigned NumOperands)
- : DerivedUser(Type::getVoidTy(C), Vty, nullptr, NumOperands, DeleteValue),
- Block(BB) {}
- // Use deleteValue() to delete a generic MemoryAccess.
- ~MemoryAccess() = default;
- private:
- BasicBlock *Block;
- };
- template <>
- struct ilist_alloc_traits<MemoryAccess> {
- static void deleteNode(MemoryAccess *MA) { MA->deleteValue(); }
- };
- inline raw_ostream &operator<<(raw_ostream &OS, const MemoryAccess &MA) {
- MA.print(OS);
- return OS;
- }
- /// Class that has the common methods + fields of memory uses/defs. It's
- /// a little awkward to have, but there are many cases where we want either a
- /// use or def, and there are many cases where uses are needed (defs aren't
- /// acceptable), and vice-versa.
- ///
- /// This class should never be instantiated directly; make a MemoryUse or
- /// MemoryDef instead.
- class MemoryUseOrDef : public MemoryAccess {
- public:
- void *operator new(size_t) = delete;
- DECLARE_TRANSPARENT_OPERAND_ACCESSORS(MemoryAccess);
- /// Get the instruction that this MemoryUse represents.
- Instruction *getMemoryInst() const { return MemoryInstruction; }
- /// Get the access that produces the memory state used by this Use.
- MemoryAccess *getDefiningAccess() const { return getOperand(0); }
- static bool classof(const Value *MA) {
- return MA->getValueID() == MemoryUseVal || MA->getValueID() == MemoryDefVal;
- }
- // Sadly, these have to be public because they are needed in some of the
- // iterators.
- inline bool isOptimized() const;
- inline MemoryAccess *getOptimized() const;
- inline void setOptimized(MemoryAccess *);
- // Retrieve AliasResult type of the optimized access. Ideally this would be
- // returned by the caching walker and may go away in the future.
- Optional<AliasResult> getOptimizedAccessType() const {
- return isOptimized() ? OptimizedAccessAlias : None;
- }
- /// Reset the ID of what this MemoryUse was optimized to, causing it to
- /// be rewalked by the walker if necessary.
- /// This really should only be called by tests.
- inline void resetOptimized();
- protected:
- friend class MemorySSA;
- friend class MemorySSAUpdater;
- MemoryUseOrDef(LLVMContext &C, MemoryAccess *DMA, unsigned Vty,
- DeleteValueTy DeleteValue, Instruction *MI, BasicBlock *BB,
- unsigned NumOperands)
- : MemoryAccess(C, Vty, DeleteValue, BB, NumOperands),
- MemoryInstruction(MI), OptimizedAccessAlias(AliasResult::MayAlias) {
- setDefiningAccess(DMA);
- }
- // Use deleteValue() to delete a generic MemoryUseOrDef.
- ~MemoryUseOrDef() = default;
- void setOptimizedAccessType(Optional<AliasResult> AR) {
- OptimizedAccessAlias = AR;
- }
- void setDefiningAccess(
- MemoryAccess *DMA, bool Optimized = false,
- Optional<AliasResult> AR = AliasResult(AliasResult::MayAlias)) {
- if (!Optimized) {
- setOperand(0, DMA);
- return;
- }
- setOptimized(DMA);
- setOptimizedAccessType(AR);
- }
- private:
- Instruction *MemoryInstruction;
- Optional<AliasResult> OptimizedAccessAlias;
- };
- /// Represents read-only accesses to memory
- ///
- /// In particular, the set of Instructions that will be represented by
- /// MemoryUse's is exactly the set of Instructions for which
- /// AliasAnalysis::getModRefInfo returns "Ref".
- class MemoryUse final : public MemoryUseOrDef {
- public:
- DECLARE_TRANSPARENT_OPERAND_ACCESSORS(MemoryAccess);
- MemoryUse(LLVMContext &C, MemoryAccess *DMA, Instruction *MI, BasicBlock *BB)
- : MemoryUseOrDef(C, DMA, MemoryUseVal, deleteMe, MI, BB,
- /*NumOperands=*/1) {}
- // allocate space for exactly one operand
- void *operator new(size_t S) { return User::operator new(S, 1); }
- void operator delete(void *Ptr) { User::operator delete(Ptr); }
- static bool classof(const Value *MA) {
- return MA->getValueID() == MemoryUseVal;
- }
- void print(raw_ostream &OS) const;
- void setOptimized(MemoryAccess *DMA) {
- OptimizedID = DMA->getID();
- setOperand(0, DMA);
- }
- bool isOptimized() const {
- return getDefiningAccess() && OptimizedID == getDefiningAccess()->getID();
- }
- MemoryAccess *getOptimized() const {
- return getDefiningAccess();
- }
- void resetOptimized() {
- OptimizedID = INVALID_MEMORYACCESS_ID;
- }
- protected:
- friend class MemorySSA;
- private:
- static void deleteMe(DerivedUser *Self);
- unsigned OptimizedID = INVALID_MEMORYACCESS_ID;
- };
- template <>
- struct OperandTraits<MemoryUse> : public FixedNumOperandTraits<MemoryUse, 1> {};
- DEFINE_TRANSPARENT_OPERAND_ACCESSORS(MemoryUse, MemoryAccess)
- /// Represents a read-write access to memory, whether it is a must-alias,
- /// or a may-alias.
- ///
- /// In particular, the set of Instructions that will be represented by
- /// MemoryDef's is exactly the set of Instructions for which
- /// AliasAnalysis::getModRefInfo returns "Mod" or "ModRef".
- /// Note that, in order to provide def-def chains, all defs also have a use
- /// associated with them. This use points to the nearest reaching
- /// MemoryDef/MemoryPhi.
- class MemoryDef final : public MemoryUseOrDef {
- public:
- friend class MemorySSA;
- DECLARE_TRANSPARENT_OPERAND_ACCESSORS(MemoryAccess);
- MemoryDef(LLVMContext &C, MemoryAccess *DMA, Instruction *MI, BasicBlock *BB,
- unsigned Ver)
- : MemoryUseOrDef(C, DMA, MemoryDefVal, deleteMe, MI, BB,
- /*NumOperands=*/2),
- ID(Ver) {}
- // allocate space for exactly two operands
- void *operator new(size_t S) { return User::operator new(S, 2); }
- void operator delete(void *Ptr) { User::operator delete(Ptr); }
- static bool classof(const Value *MA) {
- return MA->getValueID() == MemoryDefVal;
- }
- void setOptimized(MemoryAccess *MA) {
- setOperand(1, MA);
- OptimizedID = MA->getID();
- }
- MemoryAccess *getOptimized() const {
- return cast_or_null<MemoryAccess>(getOperand(1));
- }
- bool isOptimized() const {
- return getOptimized() && OptimizedID == getOptimized()->getID();
- }
- void resetOptimized() {
- OptimizedID = INVALID_MEMORYACCESS_ID;
- setOperand(1, nullptr);
- }
- void print(raw_ostream &OS) const;
- unsigned getID() const { return ID; }
- private:
- static void deleteMe(DerivedUser *Self);
- const unsigned ID;
- unsigned OptimizedID = INVALID_MEMORYACCESS_ID;
- };
- template <>
- struct OperandTraits<MemoryDef> : public FixedNumOperandTraits<MemoryDef, 2> {};
- DEFINE_TRANSPARENT_OPERAND_ACCESSORS(MemoryDef, MemoryAccess)
- template <>
- struct OperandTraits<MemoryUseOrDef> {
- static Use *op_begin(MemoryUseOrDef *MUD) {
- if (auto *MU = dyn_cast<MemoryUse>(MUD))
- return OperandTraits<MemoryUse>::op_begin(MU);
- return OperandTraits<MemoryDef>::op_begin(cast<MemoryDef>(MUD));
- }
- static Use *op_end(MemoryUseOrDef *MUD) {
- if (auto *MU = dyn_cast<MemoryUse>(MUD))
- return OperandTraits<MemoryUse>::op_end(MU);
- return OperandTraits<MemoryDef>::op_end(cast<MemoryDef>(MUD));
- }
- static unsigned operands(const MemoryUseOrDef *MUD) {
- if (const auto *MU = dyn_cast<MemoryUse>(MUD))
- return OperandTraits<MemoryUse>::operands(MU);
- return OperandTraits<MemoryDef>::operands(cast<MemoryDef>(MUD));
- }
- };
- DEFINE_TRANSPARENT_OPERAND_ACCESSORS(MemoryUseOrDef, MemoryAccess)
- /// Represents phi nodes for memory accesses.
- ///
- /// These have the same semantic as regular phi nodes, with the exception that
- /// only one phi will ever exist in a given basic block.
- /// Guaranteeing one phi per block means guaranteeing there is only ever one
- /// valid reaching MemoryDef/MemoryPHI along each path to the phi node.
- /// This is ensured by not allowing disambiguation of the RHS of a MemoryDef or
- /// a MemoryPhi's operands.
- /// That is, given
- /// if (a) {
- /// store %a
- /// store %b
- /// }
- /// it *must* be transformed into
- /// if (a) {
- /// 1 = MemoryDef(liveOnEntry)
- /// store %a
- /// 2 = MemoryDef(1)
- /// store %b
- /// }
- /// and *not*
- /// if (a) {
- /// 1 = MemoryDef(liveOnEntry)
- /// store %a
- /// 2 = MemoryDef(liveOnEntry)
- /// store %b
- /// }
- /// even if the two stores do not conflict. Otherwise, both 1 and 2 reach the
- /// end of the branch, and if there are not two phi nodes, one will be
- /// disconnected completely from the SSA graph below that point.
- /// Because MemoryUse's do not generate new definitions, they do not have this
- /// issue.
- class MemoryPhi final : public MemoryAccess {
- // allocate space for exactly zero operands
- void *operator new(size_t S) { return User::operator new(S); }
- public:
- void operator delete(void *Ptr) { User::operator delete(Ptr); }
- /// Provide fast operand accessors
- DECLARE_TRANSPARENT_OPERAND_ACCESSORS(MemoryAccess);
- MemoryPhi(LLVMContext &C, BasicBlock *BB, unsigned Ver, unsigned NumPreds = 0)
- : MemoryAccess(C, MemoryPhiVal, deleteMe, BB, 0), ID(Ver),
- ReservedSpace(NumPreds) {
- allocHungoffUses(ReservedSpace);
- }
- // Block iterator interface. This provides access to the list of incoming
- // basic blocks, which parallels the list of incoming values.
- using block_iterator = BasicBlock **;
- using const_block_iterator = BasicBlock *const *;
- block_iterator block_begin() {
- return reinterpret_cast<block_iterator>(op_begin() + ReservedSpace);
- }
- const_block_iterator block_begin() const {
- return reinterpret_cast<const_block_iterator>(op_begin() + ReservedSpace);
- }
- block_iterator block_end() { return block_begin() + getNumOperands(); }
- const_block_iterator block_end() const {
- return block_begin() + getNumOperands();
- }
- iterator_range<block_iterator> blocks() {
- return make_range(block_begin(), block_end());
- }
- iterator_range<const_block_iterator> blocks() const {
- return make_range(block_begin(), block_end());
- }
- op_range incoming_values() { return operands(); }
- const_op_range incoming_values() const { return operands(); }
- /// Return the number of incoming edges
- unsigned getNumIncomingValues() const { return getNumOperands(); }
- /// Return incoming value number x
- MemoryAccess *getIncomingValue(unsigned I) const { return getOperand(I); }
- void setIncomingValue(unsigned I, MemoryAccess *V) {
- assert(V && "PHI node got a null value!");
- setOperand(I, V);
- }
- static unsigned getOperandNumForIncomingValue(unsigned I) { return I; }
- static unsigned getIncomingValueNumForOperand(unsigned I) { return I; }
- /// Return incoming basic block number @p i.
- BasicBlock *getIncomingBlock(unsigned I) const { return block_begin()[I]; }
- /// Return incoming basic block corresponding
- /// to an operand of the PHI.
- BasicBlock *getIncomingBlock(const Use &U) const {
- assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
- return getIncomingBlock(unsigned(&U - op_begin()));
- }
- /// Return incoming basic block corresponding
- /// to value use iterator.
- BasicBlock *getIncomingBlock(MemoryAccess::const_user_iterator I) const {
- return getIncomingBlock(I.getUse());
- }
- void setIncomingBlock(unsigned I, BasicBlock *BB) {
- assert(BB && "PHI node got a null basic block!");
- block_begin()[I] = BB;
- }
- /// Add an incoming value to the end of the PHI list
- void addIncoming(MemoryAccess *V, BasicBlock *BB) {
- if (getNumOperands() == ReservedSpace)
- growOperands(); // Get more space!
- // Initialize some new operands.
- setNumHungOffUseOperands(getNumOperands() + 1);
- setIncomingValue(getNumOperands() - 1, V);
- setIncomingBlock(getNumOperands() - 1, BB);
- }
- /// Return the first index of the specified basic
- /// block in the value list for this PHI. Returns -1 if no instance.
- int getBasicBlockIndex(const BasicBlock *BB) const {
- for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
- if (block_begin()[I] == BB)
- return I;
- return -1;
- }
- MemoryAccess *getIncomingValueForBlock(const BasicBlock *BB) const {
- int Idx = getBasicBlockIndex(BB);
- assert(Idx >= 0 && "Invalid basic block argument!");
- return getIncomingValue(Idx);
- }
- // After deleting incoming position I, the order of incoming may be changed.
- void unorderedDeleteIncoming(unsigned I) {
- unsigned E = getNumOperands();
- assert(I < E && "Cannot remove out of bounds Phi entry.");
- // MemoryPhi must have at least two incoming values, otherwise the MemoryPhi
- // itself should be deleted.
- assert(E >= 2 && "Cannot only remove incoming values in MemoryPhis with "
- "at least 2 values.");
- setIncomingValue(I, getIncomingValue(E - 1));
- setIncomingBlock(I, block_begin()[E - 1]);
- setOperand(E - 1, nullptr);
- block_begin()[E - 1] = nullptr;
- setNumHungOffUseOperands(getNumOperands() - 1);
- }
- // After deleting entries that satisfy Pred, remaining entries may have
- // changed order.
- template <typename Fn> void unorderedDeleteIncomingIf(Fn &&Pred) {
- for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
- if (Pred(getIncomingValue(I), getIncomingBlock(I))) {
- unorderedDeleteIncoming(I);
- E = getNumOperands();
- --I;
- }
- assert(getNumOperands() >= 1 &&
- "Cannot remove all incoming blocks in a MemoryPhi.");
- }
- // After deleting incoming block BB, the incoming blocks order may be changed.
- void unorderedDeleteIncomingBlock(const BasicBlock *BB) {
- unorderedDeleteIncomingIf(
- [&](const MemoryAccess *, const BasicBlock *B) { return BB == B; });
- }
- // After deleting incoming memory access MA, the incoming accesses order may
- // be changed.
- void unorderedDeleteIncomingValue(const MemoryAccess *MA) {
- unorderedDeleteIncomingIf(
- [&](const MemoryAccess *M, const BasicBlock *) { return MA == M; });
- }
- static bool classof(const Value *V) {
- return V->getValueID() == MemoryPhiVal;
- }
- void print(raw_ostream &OS) const;
- unsigned getID() const { return ID; }
- protected:
- friend class MemorySSA;
- /// this is more complicated than the generic
- /// User::allocHungoffUses, because we have to allocate Uses for the incoming
- /// values and pointers to the incoming blocks, all in one allocation.
- void allocHungoffUses(unsigned N) {
- User::allocHungoffUses(N, /* IsPhi */ true);
- }
- private:
- // For debugging only
- const unsigned ID;
- unsigned ReservedSpace;
- /// This grows the operand list in response to a push_back style of
- /// operation. This grows the number of ops by 1.5 times.
- void growOperands() {
- unsigned E = getNumOperands();
- // 2 op PHI nodes are VERY common, so reserve at least enough for that.
- ReservedSpace = std::max(E + E / 2, 2u);
- growHungoffUses(ReservedSpace, /* IsPhi */ true);
- }
- static void deleteMe(DerivedUser *Self);
- };
- inline unsigned MemoryAccess::getID() const {
- assert((isa<MemoryDef>(this) || isa<MemoryPhi>(this)) &&
- "only memory defs and phis have ids");
- if (const auto *MD = dyn_cast<MemoryDef>(this))
- return MD->getID();
- return cast<MemoryPhi>(this)->getID();
- }
- inline bool MemoryUseOrDef::isOptimized() const {
- if (const auto *MD = dyn_cast<MemoryDef>(this))
- return MD->isOptimized();
- return cast<MemoryUse>(this)->isOptimized();
- }
- inline MemoryAccess *MemoryUseOrDef::getOptimized() const {
- if (const auto *MD = dyn_cast<MemoryDef>(this))
- return MD->getOptimized();
- return cast<MemoryUse>(this)->getOptimized();
- }
- inline void MemoryUseOrDef::setOptimized(MemoryAccess *MA) {
- if (auto *MD = dyn_cast<MemoryDef>(this))
- MD->setOptimized(MA);
- else
- cast<MemoryUse>(this)->setOptimized(MA);
- }
- inline void MemoryUseOrDef::resetOptimized() {
- if (auto *MD = dyn_cast<MemoryDef>(this))
- MD->resetOptimized();
- else
- cast<MemoryUse>(this)->resetOptimized();
- }
- template <> struct OperandTraits<MemoryPhi> : public HungoffOperandTraits<2> {};
- DEFINE_TRANSPARENT_OPERAND_ACCESSORS(MemoryPhi, MemoryAccess)
- /// Encapsulates MemorySSA, including all data associated with memory
- /// accesses.
- class MemorySSA {
- public:
- MemorySSA(Function &, AliasAnalysis *, DominatorTree *);
- // MemorySSA must remain where it's constructed; Walkers it creates store
- // pointers to it.
- MemorySSA(MemorySSA &&) = delete;
- ~MemorySSA();
- MemorySSAWalker *getWalker();
- MemorySSAWalker *getSkipSelfWalker();
- /// Given a memory Mod/Ref'ing instruction, get the MemorySSA
- /// access associated with it. If passed a basic block gets the memory phi
- /// node that exists for that block, if there is one. Otherwise, this will get
- /// a MemoryUseOrDef.
- MemoryUseOrDef *getMemoryAccess(const Instruction *I) const {
- return cast_or_null<MemoryUseOrDef>(ValueToMemoryAccess.lookup(I));
- }
- MemoryPhi *getMemoryAccess(const BasicBlock *BB) const {
- return cast_or_null<MemoryPhi>(ValueToMemoryAccess.lookup(cast<Value>(BB)));
- }
- DominatorTree &getDomTree() const { return *DT; }
- void dump() const;
- void print(raw_ostream &) const;
- /// Return true if \p MA represents the live on entry value
- ///
- /// Loads and stores from pointer arguments and other global values may be
- /// defined by memory operations that do not occur in the current function, so
- /// they may be live on entry to the function. MemorySSA represents such
- /// memory state by the live on entry definition, which is guaranteed to occur
- /// before any other memory access in the function.
- inline bool isLiveOnEntryDef(const MemoryAccess *MA) const {
- return MA == LiveOnEntryDef.get();
- }
- inline MemoryAccess *getLiveOnEntryDef() const {
- return LiveOnEntryDef.get();
- }
- // Sadly, iplists, by default, owns and deletes pointers added to the
- // list. It's not currently possible to have two iplists for the same type,
- // where one owns the pointers, and one does not. This is because the traits
- // are per-type, not per-tag. If this ever changes, we should make the
- // DefList an iplist.
- using AccessList = iplist<MemoryAccess, ilist_tag<MSSAHelpers::AllAccessTag>>;
- using DefsList =
- simple_ilist<MemoryAccess, ilist_tag<MSSAHelpers::DefsOnlyTag>>;
- /// Return the list of MemoryAccess's for a given basic block.
- ///
- /// This list is not modifiable by the user.
- const AccessList *getBlockAccesses(const BasicBlock *BB) const {
- return getWritableBlockAccesses(BB);
- }
- /// Return the list of MemoryDef's and MemoryPhi's for a given basic
- /// block.
- ///
- /// This list is not modifiable by the user.
- const DefsList *getBlockDefs(const BasicBlock *BB) const {
- return getWritableBlockDefs(BB);
- }
- /// Given two memory accesses in the same basic block, determine
- /// whether MemoryAccess \p A dominates MemoryAccess \p B.
- bool locallyDominates(const MemoryAccess *A, const MemoryAccess *B) const;
- /// Given two memory accesses in potentially different blocks,
- /// determine whether MemoryAccess \p A dominates MemoryAccess \p B.
- bool dominates(const MemoryAccess *A, const MemoryAccess *B) const;
- /// Given a MemoryAccess and a Use, determine whether MemoryAccess \p A
- /// dominates Use \p B.
- bool dominates(const MemoryAccess *A, const Use &B) const;
- enum class VerificationLevel { Fast, Full };
- /// Verify that MemorySSA is self consistent (IE definitions dominate
- /// all uses, uses appear in the right places). This is used by unit tests.
- void verifyMemorySSA(VerificationLevel = VerificationLevel::Fast) const;
- /// Used in various insertion functions to specify whether we are talking
- /// about the beginning or end of a block.
- enum InsertionPlace { Beginning, End, BeforeTerminator };
- protected:
- // Used by Memory SSA dumpers and wrapper pass
- friend class MemorySSAPrinterLegacyPass;
- friend class MemorySSAUpdater;
- void verifyOrderingDominationAndDefUses(
- Function &F, VerificationLevel = VerificationLevel::Fast) const;
- void verifyDominationNumbers(const Function &F) const;
- void verifyPrevDefInPhis(Function &F) const;
- // This is used by the use optimizer and updater.
- AccessList *getWritableBlockAccesses(const BasicBlock *BB) const {
- auto It = PerBlockAccesses.find(BB);
- return It == PerBlockAccesses.end() ? nullptr : It->second.get();
- }
- // This is used by the use optimizer and updater.
- DefsList *getWritableBlockDefs(const BasicBlock *BB) const {
- auto It = PerBlockDefs.find(BB);
- return It == PerBlockDefs.end() ? nullptr : It->second.get();
- }
- // These is used by the updater to perform various internal MemorySSA
- // machinsations. They do not always leave the IR in a correct state, and
- // relies on the updater to fixup what it breaks, so it is not public.
- void moveTo(MemoryUseOrDef *What, BasicBlock *BB, AccessList::iterator Where);
- void moveTo(MemoryAccess *What, BasicBlock *BB, InsertionPlace Point);
- // Rename the dominator tree branch rooted at BB.
- void renamePass(BasicBlock *BB, MemoryAccess *IncomingVal,
- SmallPtrSetImpl<BasicBlock *> &Visited) {
- renamePass(DT->getNode(BB), IncomingVal, Visited, true, true);
- }
- void removeFromLookups(MemoryAccess *);
- void removeFromLists(MemoryAccess *, bool ShouldDelete = true);
- void insertIntoListsForBlock(MemoryAccess *, const BasicBlock *,
- InsertionPlace);
- void insertIntoListsBefore(MemoryAccess *, const BasicBlock *,
- AccessList::iterator);
- MemoryUseOrDef *createDefinedAccess(Instruction *, MemoryAccess *,
- const MemoryUseOrDef *Template = nullptr,
- bool CreationMustSucceed = true);
- private:
- template <class AliasAnalysisType> class ClobberWalkerBase;
- template <class AliasAnalysisType> class CachingWalker;
- template <class AliasAnalysisType> class SkipSelfWalker;
- class OptimizeUses;
- CachingWalker<AliasAnalysis> *getWalkerImpl();
- void buildMemorySSA(BatchAAResults &BAA);
- void prepareForMoveTo(MemoryAccess *, BasicBlock *);
- void verifyUseInDefs(MemoryAccess *, MemoryAccess *) const;
- using AccessMap = DenseMap<const BasicBlock *, std::unique_ptr<AccessList>>;
- using DefsMap = DenseMap<const BasicBlock *, std::unique_ptr<DefsList>>;
- void markUnreachableAsLiveOnEntry(BasicBlock *BB);
- MemoryPhi *createMemoryPhi(BasicBlock *BB);
- template <typename AliasAnalysisType>
- MemoryUseOrDef *createNewAccess(Instruction *, AliasAnalysisType *,
- const MemoryUseOrDef *Template = nullptr);
- void placePHINodes(const SmallPtrSetImpl<BasicBlock *> &);
- MemoryAccess *renameBlock(BasicBlock *, MemoryAccess *, bool);
- void renameSuccessorPhis(BasicBlock *, MemoryAccess *, bool);
- void renamePass(DomTreeNode *, MemoryAccess *IncomingVal,
- SmallPtrSetImpl<BasicBlock *> &Visited,
- bool SkipVisited = false, bool RenameAllUses = false);
- AccessList *getOrCreateAccessList(const BasicBlock *);
- DefsList *getOrCreateDefsList(const BasicBlock *);
- void renumberBlock(const BasicBlock *) const;
- AliasAnalysis *AA = nullptr;
- DominatorTree *DT;
- Function &F;
- // Memory SSA mappings
- DenseMap<const Value *, MemoryAccess *> ValueToMemoryAccess;
- // These two mappings contain the main block to access/def mappings for
- // MemorySSA. The list contained in PerBlockAccesses really owns all the
- // MemoryAccesses.
- // Both maps maintain the invariant that if a block is found in them, the
- // corresponding list is not empty, and if a block is not found in them, the
- // corresponding list is empty.
- AccessMap PerBlockAccesses;
- DefsMap PerBlockDefs;
- std::unique_ptr<MemoryAccess, ValueDeleter> LiveOnEntryDef;
- // Domination mappings
- // Note that the numbering is local to a block, even though the map is
- // global.
- mutable SmallPtrSet<const BasicBlock *, 16> BlockNumberingValid;
- mutable DenseMap<const MemoryAccess *, unsigned long> BlockNumbering;
- // Memory SSA building info
- std::unique_ptr<ClobberWalkerBase<AliasAnalysis>> WalkerBase;
- std::unique_ptr<CachingWalker<AliasAnalysis>> Walker;
- std::unique_ptr<SkipSelfWalker<AliasAnalysis>> SkipWalker;
- unsigned NextID = 0;
- };
- /// Enables verification of MemorySSA.
- ///
- /// The checks which this flag enables is exensive and disabled by default
- /// unless `EXPENSIVE_CHECKS` is defined. The flag `-verify-memoryssa` can be
- /// used to selectively enable the verification without re-compilation.
- extern bool VerifyMemorySSA;
- // Internal MemorySSA utils, for use by MemorySSA classes and walkers
- class MemorySSAUtil {
- protected:
- friend class GVNHoist;
- friend class MemorySSAWalker;
- // This function should not be used by new passes.
- static bool defClobbersUseOrDef(MemoryDef *MD, const MemoryUseOrDef *MU,
- AliasAnalysis &AA);
- };
- // This pass does eager building and then printing of MemorySSA. It is used by
- // the tests to be able to build, dump, and verify Memory SSA.
- class MemorySSAPrinterLegacyPass : public FunctionPass {
- public:
- MemorySSAPrinterLegacyPass();
- bool runOnFunction(Function &) override;
- void getAnalysisUsage(AnalysisUsage &AU) const override;
- static char ID;
- };
- /// An analysis that produces \c MemorySSA for a function.
- ///
- class MemorySSAAnalysis : public AnalysisInfoMixin<MemorySSAAnalysis> {
- friend AnalysisInfoMixin<MemorySSAAnalysis>;
- static AnalysisKey Key;
- public:
- // Wrap MemorySSA result to ensure address stability of internal MemorySSA
- // pointers after construction. Use a wrapper class instead of plain
- // unique_ptr<MemorySSA> to avoid build breakage on MSVC.
- struct Result {
- Result(std::unique_ptr<MemorySSA> &&MSSA) : MSSA(std::move(MSSA)) {}
- MemorySSA &getMSSA() { return *MSSA.get(); }
- std::unique_ptr<MemorySSA> MSSA;
- bool invalidate(Function &F, const PreservedAnalyses &PA,
- FunctionAnalysisManager::Invalidator &Inv);
- };
- Result run(Function &F, FunctionAnalysisManager &AM);
- };
- /// Printer pass for \c MemorySSA.
- class MemorySSAPrinterPass : public PassInfoMixin<MemorySSAPrinterPass> {
- raw_ostream &OS;
- public:
- explicit MemorySSAPrinterPass(raw_ostream &OS) : OS(OS) {}
- PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
- };
- /// Printer pass for \c MemorySSA via the walker.
- class MemorySSAWalkerPrinterPass
- : public PassInfoMixin<MemorySSAWalkerPrinterPass> {
- raw_ostream &OS;
- public:
- explicit MemorySSAWalkerPrinterPass(raw_ostream &OS) : OS(OS) {}
- PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
- };
- /// Verifier pass for \c MemorySSA.
- struct MemorySSAVerifierPass : PassInfoMixin<MemorySSAVerifierPass> {
- PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
- };
- /// Legacy analysis pass which computes \c MemorySSA.
- class MemorySSAWrapperPass : public FunctionPass {
- public:
- MemorySSAWrapperPass();
- static char ID;
- bool runOnFunction(Function &) override;
- void releaseMemory() override;
- MemorySSA &getMSSA() { return *MSSA; }
- const MemorySSA &getMSSA() const { return *MSSA; }
- void getAnalysisUsage(AnalysisUsage &AU) const override;
- void verifyAnalysis() const override;
- void print(raw_ostream &OS, const Module *M = nullptr) const override;
- private:
- std::unique_ptr<MemorySSA> MSSA;
- };
- /// This is the generic walker interface for walkers of MemorySSA.
- /// Walkers are used to be able to further disambiguate the def-use chains
- /// MemorySSA gives you, or otherwise produce better info than MemorySSA gives
- /// you.
- /// In particular, while the def-use chains provide basic information, and are
- /// guaranteed to give, for example, the nearest may-aliasing MemoryDef for a
- /// MemoryUse as AliasAnalysis considers it, a user mant want better or other
- /// information. In particular, they may want to use SCEV info to further
- /// disambiguate memory accesses, or they may want the nearest dominating
- /// may-aliasing MemoryDef for a call or a store. This API enables a
- /// standardized interface to getting and using that info.
- class MemorySSAWalker {
- public:
- MemorySSAWalker(MemorySSA *);
- virtual ~MemorySSAWalker() = default;
- using MemoryAccessSet = SmallVector<MemoryAccess *, 8>;
- /// Given a memory Mod/Ref/ModRef'ing instruction, calling this
- /// will give you the nearest dominating MemoryAccess that Mod's the location
- /// the instruction accesses (by skipping any def which AA can prove does not
- /// alias the location(s) accessed by the instruction given).
- ///
- /// Note that this will return a single access, and it must dominate the
- /// Instruction, so if an operand of a MemoryPhi node Mod's the instruction,
- /// this will return the MemoryPhi, not the operand. This means that
- /// given:
- /// if (a) {
- /// 1 = MemoryDef(liveOnEntry)
- /// store %a
- /// } else {
- /// 2 = MemoryDef(liveOnEntry)
- /// store %b
- /// }
- /// 3 = MemoryPhi(2, 1)
- /// MemoryUse(3)
- /// load %a
- ///
- /// calling this API on load(%a) will return the MemoryPhi, not the MemoryDef
- /// in the if (a) branch.
- MemoryAccess *getClobberingMemoryAccess(const Instruction *I) {
- MemoryAccess *MA = MSSA->getMemoryAccess(I);
- assert(MA && "Handed an instruction that MemorySSA doesn't recognize?");
- return getClobberingMemoryAccess(MA);
- }
- /// Does the same thing as getClobberingMemoryAccess(const Instruction *I),
- /// but takes a MemoryAccess instead of an Instruction.
- virtual MemoryAccess *getClobberingMemoryAccess(MemoryAccess *) = 0;
- /// Given a potentially clobbering memory access and a new location,
- /// calling this will give you the nearest dominating clobbering MemoryAccess
- /// (by skipping non-aliasing def links).
- ///
- /// This version of the function is mainly used to disambiguate phi translated
- /// pointers, where the value of a pointer may have changed from the initial
- /// memory access. Note that this expects to be handed either a MemoryUse,
- /// or an already potentially clobbering access. Unlike the above API, if
- /// given a MemoryDef that clobbers the pointer as the starting access, it
- /// will return that MemoryDef, whereas the above would return the clobber
- /// starting from the use side of the memory def.
- virtual MemoryAccess *getClobberingMemoryAccess(MemoryAccess *,
- const MemoryLocation &) = 0;
- /// Given a memory access, invalidate anything this walker knows about
- /// that access.
- /// This API is used by walkers that store information to perform basic cache
- /// invalidation. This will be called by MemorySSA at appropriate times for
- /// the walker it uses or returns.
- virtual void invalidateInfo(MemoryAccess *) {}
- protected:
- friend class MemorySSA; // For updating MSSA pointer in MemorySSA move
- // constructor.
- MemorySSA *MSSA;
- };
- /// A MemorySSAWalker that does no alias queries, or anything else. It
- /// simply returns the links as they were constructed by the builder.
- class DoNothingMemorySSAWalker final : public MemorySSAWalker {
- public:
- // Keep the overrides below from hiding the Instruction overload of
- // getClobberingMemoryAccess.
- using MemorySSAWalker::getClobberingMemoryAccess;
- MemoryAccess *getClobberingMemoryAccess(MemoryAccess *) override;
- MemoryAccess *getClobberingMemoryAccess(MemoryAccess *,
- const MemoryLocation &) override;
- };
- using MemoryAccessPair = std::pair<MemoryAccess *, MemoryLocation>;
- using ConstMemoryAccessPair = std::pair<const MemoryAccess *, MemoryLocation>;
- /// Iterator base class used to implement const and non-const iterators
- /// over the defining accesses of a MemoryAccess.
- template <class T>
- class memoryaccess_def_iterator_base
- : public iterator_facade_base<memoryaccess_def_iterator_base<T>,
- std::forward_iterator_tag, T, ptrdiff_t, T *,
- T *> {
- using BaseT = typename memoryaccess_def_iterator_base::iterator_facade_base;
- public:
- memoryaccess_def_iterator_base(T *Start) : Access(Start) {}
- memoryaccess_def_iterator_base() = default;
- bool operator==(const memoryaccess_def_iterator_base &Other) const {
- return Access == Other.Access && (!Access || ArgNo == Other.ArgNo);
- }
- // This is a bit ugly, but for MemoryPHI's, unlike PHINodes, you can't get the
- // block from the operand in constant time (In a PHINode, the uselist has
- // both, so it's just subtraction). We provide it as part of the
- // iterator to avoid callers having to linear walk to get the block.
- // If the operation becomes constant time on MemoryPHI's, this bit of
- // abstraction breaking should be removed.
- BasicBlock *getPhiArgBlock() const {
- MemoryPhi *MP = dyn_cast<MemoryPhi>(Access);
- assert(MP && "Tried to get phi arg block when not iterating over a PHI");
- return MP->getIncomingBlock(ArgNo);
- }
- typename std::iterator_traits<BaseT>::pointer operator*() const {
- assert(Access && "Tried to access past the end of our iterator");
- // Go to the first argument for phis, and the defining access for everything
- // else.
- if (const MemoryPhi *MP = dyn_cast<MemoryPhi>(Access))
- return MP->getIncomingValue(ArgNo);
- return cast<MemoryUseOrDef>(Access)->getDefiningAccess();
- }
- using BaseT::operator++;
- memoryaccess_def_iterator_base &operator++() {
- assert(Access && "Hit end of iterator");
- if (const MemoryPhi *MP = dyn_cast<MemoryPhi>(Access)) {
- if (++ArgNo >= MP->getNumIncomingValues()) {
- ArgNo = 0;
- Access = nullptr;
- }
- } else {
- Access = nullptr;
- }
- return *this;
- }
- private:
- T *Access = nullptr;
- unsigned ArgNo = 0;
- };
- inline memoryaccess_def_iterator MemoryAccess::defs_begin() {
- return memoryaccess_def_iterator(this);
- }
- inline const_memoryaccess_def_iterator MemoryAccess::defs_begin() const {
- return const_memoryaccess_def_iterator(this);
- }
- inline memoryaccess_def_iterator MemoryAccess::defs_end() {
- return memoryaccess_def_iterator();
- }
- inline const_memoryaccess_def_iterator MemoryAccess::defs_end() const {
- return const_memoryaccess_def_iterator();
- }
- /// GraphTraits for a MemoryAccess, which walks defs in the normal case,
- /// and uses in the inverse case.
- template <> struct GraphTraits<MemoryAccess *> {
- using NodeRef = MemoryAccess *;
- using ChildIteratorType = memoryaccess_def_iterator;
- static NodeRef getEntryNode(NodeRef N) { return N; }
- static ChildIteratorType child_begin(NodeRef N) { return N->defs_begin(); }
- static ChildIteratorType child_end(NodeRef N) { return N->defs_end(); }
- };
- template <> struct GraphTraits<Inverse<MemoryAccess *>> {
- using NodeRef = MemoryAccess *;
- using ChildIteratorType = MemoryAccess::iterator;
- static NodeRef getEntryNode(NodeRef N) { return N; }
- static ChildIteratorType child_begin(NodeRef N) { return N->user_begin(); }
- static ChildIteratorType child_end(NodeRef N) { return N->user_end(); }
- };
- /// Provide an iterator that walks defs, giving both the memory access,
- /// and the current pointer location, updating the pointer location as it
- /// changes due to phi node translation.
- ///
- /// This iterator, while somewhat specialized, is what most clients actually
- /// want when walking upwards through MemorySSA def chains. It takes a pair of
- /// <MemoryAccess,MemoryLocation>, and walks defs, properly translating the
- /// memory location through phi nodes for the user.
- class upward_defs_iterator
- : public iterator_facade_base<upward_defs_iterator,
- std::forward_iterator_tag,
- const MemoryAccessPair> {
- using BaseT = upward_defs_iterator::iterator_facade_base;
- public:
- upward_defs_iterator(const MemoryAccessPair &Info, DominatorTree *DT,
- bool *PerformedPhiTranslation = nullptr)
- : DefIterator(Info.first), Location(Info.second),
- OriginalAccess(Info.first), DT(DT),
- PerformedPhiTranslation(PerformedPhiTranslation) {
- CurrentPair.first = nullptr;
- WalkingPhi = Info.first && isa<MemoryPhi>(Info.first);
- fillInCurrentPair();
- }
- upward_defs_iterator() { CurrentPair.first = nullptr; }
- bool operator==(const upward_defs_iterator &Other) const {
- return DefIterator == Other.DefIterator;
- }
- typename std::iterator_traits<BaseT>::reference operator*() const {
- assert(DefIterator != OriginalAccess->defs_end() &&
- "Tried to access past the end of our iterator");
- return CurrentPair;
- }
- using BaseT::operator++;
- upward_defs_iterator &operator++() {
- assert(DefIterator != OriginalAccess->defs_end() &&
- "Tried to access past the end of the iterator");
- ++DefIterator;
- if (DefIterator != OriginalAccess->defs_end())
- fillInCurrentPair();
- return *this;
- }
- BasicBlock *getPhiArgBlock() const { return DefIterator.getPhiArgBlock(); }
- private:
- /// Returns true if \p Ptr is guaranteed to be loop invariant for any possible
- /// loop. In particular, this guarantees that it only references a single
- /// MemoryLocation during execution of the containing function.
- bool IsGuaranteedLoopInvariant(Value *Ptr) const;
- void fillInCurrentPair() {
- CurrentPair.first = *DefIterator;
- CurrentPair.second = Location;
- if (WalkingPhi && Location.Ptr) {
- // Mark size as unknown, if the location is not guaranteed to be
- // loop-invariant for any possible loop in the function. Setting the size
- // to unknown guarantees that any memory accesses that access locations
- // after the pointer are considered as clobbers, which is important to
- // catch loop carried dependences.
- if (Location.Ptr &&
- !IsGuaranteedLoopInvariant(const_cast<Value *>(Location.Ptr)))
- CurrentPair.second =
- Location.getWithNewSize(LocationSize::beforeOrAfterPointer());
- PHITransAddr Translator(
- const_cast<Value *>(Location.Ptr),
- OriginalAccess->getBlock()->getModule()->getDataLayout(), nullptr);
- if (!Translator.PHITranslateValue(OriginalAccess->getBlock(),
- DefIterator.getPhiArgBlock(), DT,
- true)) {
- Value *TransAddr = Translator.getAddr();
- if (TransAddr != Location.Ptr) {
- CurrentPair.second = CurrentPair.second.getWithNewPtr(TransAddr);
- if (TransAddr &&
- !IsGuaranteedLoopInvariant(const_cast<Value *>(TransAddr)))
- CurrentPair.second = CurrentPair.second.getWithNewSize(
- LocationSize::beforeOrAfterPointer());
- if (PerformedPhiTranslation)
- *PerformedPhiTranslation = true;
- }
- }
- }
- }
- MemoryAccessPair CurrentPair;
- memoryaccess_def_iterator DefIterator;
- MemoryLocation Location;
- MemoryAccess *OriginalAccess = nullptr;
- DominatorTree *DT = nullptr;
- bool WalkingPhi = false;
- bool *PerformedPhiTranslation = nullptr;
- };
- inline upward_defs_iterator
- upward_defs_begin(const MemoryAccessPair &Pair, DominatorTree &DT,
- bool *PerformedPhiTranslation = nullptr) {
- return upward_defs_iterator(Pair, &DT, PerformedPhiTranslation);
- }
- inline upward_defs_iterator upward_defs_end() { return upward_defs_iterator(); }
- inline iterator_range<upward_defs_iterator>
- upward_defs(const MemoryAccessPair &Pair, DominatorTree &DT) {
- return make_range(upward_defs_begin(Pair, DT), upward_defs_end());
- }
- /// Walks the defining accesses of MemoryDefs. Stops after we hit something that
- /// has no defining use (e.g. a MemoryPhi or liveOnEntry). Note that, when
- /// comparing against a null def_chain_iterator, this will compare equal only
- /// after walking said Phi/liveOnEntry.
- ///
- /// The UseOptimizedChain flag specifies whether to walk the clobbering
- /// access chain, or all the accesses.
- ///
- /// Normally, MemoryDef are all just def/use linked together, so a def_chain on
- /// a MemoryDef will walk all MemoryDefs above it in the program until it hits
- /// a phi node. The optimized chain walks the clobbering access of a store.
- /// So if you are just trying to find, given a store, what the next
- /// thing that would clobber the same memory is, you want the optimized chain.
- template <class T, bool UseOptimizedChain = false>
- struct def_chain_iterator
- : public iterator_facade_base<def_chain_iterator<T, UseOptimizedChain>,
- std::forward_iterator_tag, MemoryAccess *> {
- def_chain_iterator() : MA(nullptr) {}
- def_chain_iterator(T MA) : MA(MA) {}
- T operator*() const { return MA; }
- def_chain_iterator &operator++() {
- // N.B. liveOnEntry has a null defining access.
- if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA)) {
- if (UseOptimizedChain && MUD->isOptimized())
- MA = MUD->getOptimized();
- else
- MA = MUD->getDefiningAccess();
- } else {
- MA = nullptr;
- }
- return *this;
- }
- bool operator==(const def_chain_iterator &O) const { return MA == O.MA; }
- private:
- T MA;
- };
- template <class T>
- inline iterator_range<def_chain_iterator<T>>
- def_chain(T MA, MemoryAccess *UpTo = nullptr) {
- #ifdef EXPENSIVE_CHECKS
- assert((!UpTo || find(def_chain(MA), UpTo) != def_chain_iterator<T>()) &&
- "UpTo isn't in the def chain!");
- #endif
- return make_range(def_chain_iterator<T>(MA), def_chain_iterator<T>(UpTo));
- }
- template <class T>
- inline iterator_range<def_chain_iterator<T, true>> optimized_def_chain(T MA) {
- return make_range(def_chain_iterator<T, true>(MA),
- def_chain_iterator<T, true>(nullptr));
- }
- } // end namespace llvm
- #endif // LLVM_ANALYSIS_MEMORYSSA_H
- #ifdef __GNUC__
- #pragma GCC diagnostic pop
- #endif
|