2016-02-02 23:46:49 +01:00
|
|
|
//===- MemorySSA.h - Build Memory SSA ---------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-08-17 00:07:40 +02:00
|
|
|
//
|
2017-03-10 21:44:39 +01:00
|
|
|
/// \file
|
|
|
|
/// \brief 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
|
2017-04-01 11:44:14 +02:00
|
|
|
/// 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.
|
2017-03-10 21:44:39 +01:00
|
|
|
///
|
|
|
|
/// 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
|
2017-04-01 11:44:14 +02:00
|
|
|
/// gotten by using the MemoryUse associated with it, and walking from use to
|
|
|
|
/// def until you hit the top of the function.
|
2017-03-10 21:44:39 +01:00
|
|
|
///
|
|
|
|
/// 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
|
2017-04-01 11:44:14 +02:00
|
|
|
/// store, all the MemoryUses on its use lists are may-aliases of that store
|
|
|
|
/// (but the MemoryDefs on its use list may not be).
|
2017-03-10 21:44:39 +01:00
|
|
|
///
|
|
|
|
/// MemoryDefs are not disambiguated because it would require multiple reaching
|
|
|
|
/// definitions, which would require multiple phis, and multiple memoryaccesses
|
|
|
|
/// per instruction.
|
2017-08-17 00:07:40 +02:00
|
|
|
//
|
2016-02-02 23:46:49 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2016-04-28 20:04:41 +02:00
|
|
|
|
2017-04-11 22:06:36 +02:00
|
|
|
#ifndef LLVM_ANALYSIS_MEMORYSSA_H
|
|
|
|
#define LLVM_ANALYSIS_MEMORYSSA_H
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-04-28 20:04:41 +02:00
|
|
|
#include "llvm/ADT/ilist.h"
|
2016-02-02 23:46:49 +01:00
|
|
|
#include "llvm/ADT/ilist_node.h"
|
|
|
|
#include "llvm/ADT/iterator.h"
|
2017-01-25 21:56:19 +01:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2017-08-17 00:07:40 +02:00
|
|
|
#include "llvm/ADT/simple_ilist.h"
|
2016-02-02 23:46:49 +01:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2016-04-28 20:04:41 +02:00
|
|
|
#include "llvm/Analysis/MemoryLocation.h"
|
2016-02-02 23:46:49 +01:00
|
|
|
#include "llvm/Analysis/PHITransAddr.h"
|
2016-04-28 20:04:41 +02:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
#include "llvm/IR/DerivedUser.h"
|
2016-02-02 23:46:49 +01:00
|
|
|
#include "llvm/IR/Dominators.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2016-04-28 20:04:41 +02:00
|
|
|
#include "llvm/IR/Use.h"
|
2016-02-02 23:46:49 +01:00
|
|
|
#include "llvm/IR/User.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
|
|
|
#include "llvm/Pass.h"
|
2016-04-28 20:04:41 +02:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <iterator>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
2016-04-28 20:04:41 +02:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
class Function;
|
2016-04-28 20:04:41 +02:00
|
|
|
class Instruction;
|
2016-02-02 23:46:49 +01:00
|
|
|
class MemoryAccess;
|
2017-08-17 00:07:40 +02:00
|
|
|
class MemorySSAWalker;
|
2016-04-28 20:04:41 +02:00
|
|
|
class LLVMContext;
|
|
|
|
class raw_ostream;
|
2017-08-17 00:07:40 +02:00
|
|
|
|
2017-01-25 21:56:19 +01:00
|
|
|
namespace MSSAHelpers {
|
2017-08-17 00:07:40 +02:00
|
|
|
|
2017-01-25 21:56:19 +01:00
|
|
|
struct AllAccessTag {};
|
|
|
|
struct DefsOnlyTag {};
|
2017-08-17 00:07:40 +02:00
|
|
|
|
|
|
|
} // end namespace MSSAHelpers
|
2017-01-14 01:32:38 +01:00
|
|
|
|
2018-02-24 00:07:18 +01:00
|
|
|
enum : unsigned {
|
2016-10-20 22:13:45 +02:00
|
|
|
// Used to signify what the default invalid ID is for MemoryAccess's
|
|
|
|
// getID()
|
2018-02-24 00:07:18 +01:00
|
|
|
INVALID_MEMORYACCESS_ID = -1U
|
2016-10-20 22:13:45 +02:00
|
|
|
};
|
2016-04-28 20:04:41 +02:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
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>;
|
|
|
|
|
|
|
|
// \brief The base for all memory accesses. All memory accesses in a block are
|
|
|
|
// linked together using an intrusive list.
|
2017-01-25 21:56:19 +01:00
|
|
|
class MemoryAccess
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
: public DerivedUser,
|
2017-01-25 21:56:19 +01:00
|
|
|
public ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::AllAccessTag>>,
|
|
|
|
public ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::DefsOnlyTag>> {
|
2016-02-02 23:46:49 +01:00
|
|
|
public:
|
2017-01-25 21:56:19 +01:00
|
|
|
using AllAccessType =
|
|
|
|
ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::AllAccessTag>>;
|
|
|
|
using DefsOnlyType =
|
|
|
|
ilist_node<MemoryAccess, ilist_tag<MSSAHelpers::DefsOnlyTag>>;
|
|
|
|
|
2017-08-17 00:07:40 +02:00
|
|
|
MemoryAccess(const MemoryAccess &) = delete;
|
|
|
|
MemoryAccess &operator=(const MemoryAccess &) = delete;
|
|
|
|
|
|
|
|
void *operator new(size_t) = delete;
|
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
// Methods for support type inquiry through isa, cast, and
|
|
|
|
// dyn_cast
|
2017-06-29 21:35:17 +02:00
|
|
|
static bool classof(const Value *V) {
|
2016-02-02 23:46:49 +01:00
|
|
|
unsigned ID = V->getValueID();
|
|
|
|
return ID == MemoryUseVal || ID == MemoryPhiVal || ID == MemoryDefVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicBlock *getBlock() const { return Block; }
|
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
void print(raw_ostream &OS) const;
|
|
|
|
void dump() const;
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
/// \brief The user iterators for a memory access
|
2017-08-17 00:07:40 +02:00
|
|
|
using iterator = user_iterator;
|
|
|
|
using const_iterator = const_user_iterator;
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
/// \brief 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;
|
|
|
|
|
2017-01-25 21:56:19 +01:00
|
|
|
/// \brief 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();
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
protected:
|
|
|
|
friend class MemoryDef;
|
|
|
|
friend class MemoryPhi;
|
2017-08-17 00:07:40 +02:00
|
|
|
friend class MemorySSA;
|
|
|
|
friend class MemoryUse;
|
|
|
|
friend class MemoryUseOrDef;
|
2016-02-02 23:46:49 +01:00
|
|
|
|
Introduce a basic MemorySSA updater, that supports insertDef,
insertUse, moveBefore and moveAfter operations.
Summary:
This creates a basic MemorySSA updater that handles arbitrary
insertion of uses and defs into MemorySSA, as well as arbitrary
movement around the CFG. It replaces the current splice API.
It can be made to handle arbitrary control flow changes.
Currently, it uses the same updater algorithm from D28934.
The main difference is because MemorySSA is single variable, we have
the complete def and use list, and don't need anyone to give it to us
as part of the API. We also have to rename stores below us in some
cases.
If we go that direction in that patch, i will merge all the updater
implementations (using an updater_traits or something to provide the
get* functions we use, called read*/write* in that patch).
Sadly, the current SSAUpdater algorithm is way too slow to use for
what we are doing here.
I have updated the tests we have to basically build memoryssa
incrementally using the updater api, and make sure it still comes out
the same.
Reviewers: george.burgess.iv
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29047
llvm-svn: 293356
2017-01-28 02:23:13 +01:00
|
|
|
/// \brief Used by MemorySSA to change the block of a MemoryAccess when it is
|
|
|
|
/// moved.
|
|
|
|
void setBlock(BasicBlock *BB) { Block = BB; }
|
|
|
|
|
2016-10-20 22:13:45 +02:00
|
|
|
/// \brief Used for debugging and tracking things about MemoryAccesses.
|
|
|
|
/// Guaranteed unique among MemoryAccesses, no guarantees otherwise.
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
inline unsigned getID() const;
|
2016-02-02 23:46:49 +01:00
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
MemoryAccess(LLVMContext &C, unsigned Vty, DeleteValueTy DeleteValue,
|
|
|
|
BasicBlock *BB, unsigned NumOperands)
|
|
|
|
: DerivedUser(Type::getVoidTy(C), Vty, nullptr, NumOperands, DeleteValue),
|
|
|
|
Block(BB) {}
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
BasicBlock *Block;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline raw_ostream &operator<<(raw_ostream &OS, const MemoryAccess &MA) {
|
|
|
|
MA.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief 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 {
|
2017-01-14 01:32:38 +01:00
|
|
|
public:
|
2016-02-02 23:46:49 +01:00
|
|
|
void *operator new(size_t) = delete;
|
|
|
|
|
|
|
|
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(MemoryAccess);
|
|
|
|
|
|
|
|
/// \brief Get the instruction that this MemoryUse represents.
|
|
|
|
Instruction *getMemoryInst() const { return MemoryInst; }
|
|
|
|
|
|
|
|
/// \brief Get the access that produces the memory state used by this Use.
|
|
|
|
MemoryAccess *getDefiningAccess() const { return getOperand(0); }
|
|
|
|
|
2017-06-29 21:35:17 +02:00
|
|
|
static bool classof(const Value *MA) {
|
2016-02-02 23:46:49 +01:00
|
|
|
return MA->getValueID() == MemoryUseVal || MA->getValueID() == MemoryDefVal;
|
|
|
|
}
|
|
|
|
|
2017-04-05 01:43:10 +02:00
|
|
|
// Sadly, these have to be public because they are needed in some of the
|
|
|
|
// iterators.
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
inline bool isOptimized() const;
|
|
|
|
inline MemoryAccess *getOptimized() const;
|
|
|
|
inline void setOptimized(MemoryAccess *);
|
2017-04-05 01:43:10 +02:00
|
|
|
|
|
|
|
/// \brief 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.
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
inline void resetOptimized();
|
2017-04-05 01:43:10 +02:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
protected:
|
|
|
|
friend class MemorySSA;
|
Introduce a basic MemorySSA updater, that supports insertDef,
insertUse, moveBefore and moveAfter operations.
Summary:
This creates a basic MemorySSA updater that handles arbitrary
insertion of uses and defs into MemorySSA, as well as arbitrary
movement around the CFG. It replaces the current splice API.
It can be made to handle arbitrary control flow changes.
Currently, it uses the same updater algorithm from D28934.
The main difference is because MemorySSA is single variable, we have
the complete def and use list, and don't need anyone to give it to us
as part of the API. We also have to rename stores below us in some
cases.
If we go that direction in that patch, i will merge all the updater
implementations (using an updater_traits or something to provide the
get* functions we use, called read*/write* in that patch).
Sadly, the current SSAUpdater algorithm is way too slow to use for
what we are doing here.
I have updated the tests we have to basically build memoryssa
incrementally using the updater api, and make sure it still comes out
the same.
Reviewers: george.burgess.iv
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29047
llvm-svn: 293356
2017-01-28 02:23:13 +01:00
|
|
|
friend class MemorySSAUpdater;
|
2017-08-17 00:07:40 +02:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
MemoryUseOrDef(LLVMContext &C, MemoryAccess *DMA, unsigned Vty,
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
DeleteValueTy DeleteValue, Instruction *MI, BasicBlock *BB)
|
|
|
|
: MemoryAccess(C, Vty, DeleteValue, BB, 1), MemoryInst(MI) {
|
2016-02-02 23:46:49 +01:00
|
|
|
setDefiningAccess(DMA);
|
|
|
|
}
|
2017-08-17 00:07:40 +02:00
|
|
|
|
2017-04-05 01:43:10 +02:00
|
|
|
void setDefiningAccess(MemoryAccess *DMA, bool Optimized = false) {
|
|
|
|
if (!Optimized) {
|
|
|
|
setOperand(0, DMA);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setOptimized(DMA);
|
|
|
|
}
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Instruction *MemoryInst;
|
|
|
|
};
|
2016-04-28 20:04:41 +02:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
template <>
|
|
|
|
struct OperandTraits<MemoryUseOrDef>
|
|
|
|
: public FixedNumOperandTraits<MemoryUseOrDef, 1> {};
|
|
|
|
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(MemoryUseOrDef, MemoryAccess)
|
|
|
|
|
|
|
|
/// \brief 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)
|
2017-08-17 00:07:40 +02:00
|
|
|
: MemoryUseOrDef(C, DMA, MemoryUseVal, deleteMe, MI, BB) {}
|
2016-02-02 23:46:49 +01:00
|
|
|
|
2017-01-14 01:32:38 +01:00
|
|
|
// allocate space for exactly one operand
|
|
|
|
void *operator new(size_t s) { return User::operator new(s, 1); }
|
|
|
|
|
2017-06-29 21:35:17 +02:00
|
|
|
static bool classof(const Value *MA) {
|
2016-02-02 23:46:49 +01:00
|
|
|
return MA->getValueID() == MemoryUseVal;
|
|
|
|
}
|
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
void print(raw_ostream &OS) const;
|
2017-01-14 01:32:38 +01:00
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
void setOptimized(MemoryAccess *DMA) {
|
2017-04-05 01:43:10 +02:00
|
|
|
OptimizedID = DMA->getID();
|
|
|
|
setOperand(0, DMA);
|
2016-10-20 22:13:45 +02:00
|
|
|
}
|
2017-01-14 01:32:38 +01:00
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
bool isOptimized() const {
|
2016-10-20 22:13:45 +02:00
|
|
|
return getDefiningAccess() && OptimizedID == getDefiningAccess()->getID();
|
|
|
|
}
|
2017-01-14 01:32:38 +01:00
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
MemoryAccess *getOptimized() const {
|
2017-04-05 01:43:10 +02:00
|
|
|
return getDefiningAccess();
|
|
|
|
}
|
2017-08-17 00:07:40 +02:00
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
void resetOptimized() {
|
2017-04-05 01:43:10 +02:00
|
|
|
OptimizedID = INVALID_MEMORYACCESS_ID;
|
|
|
|
}
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
friend class MemorySSA;
|
|
|
|
|
2016-10-20 22:13:45 +02:00
|
|
|
private:
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
static void deleteMe(DerivedUser *Self);
|
|
|
|
|
2018-02-23 19:56:42 +01:00
|
|
|
unsigned OptimizedID = INVALID_MEMORYACCESS_ID;
|
2016-02-02 23:46:49 +01:00
|
|
|
};
|
2016-04-28 20:04:41 +02:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
template <>
|
|
|
|
struct OperandTraits<MemoryUse> : public FixedNumOperandTraits<MemoryUse, 1> {};
|
|
|
|
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(MemoryUse, MemoryAccess)
|
|
|
|
|
|
|
|
/// \brief 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:
|
2017-08-17 00:07:40 +02:00
|
|
|
friend class MemorySSA;
|
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(MemoryAccess);
|
|
|
|
|
|
|
|
MemoryDef(LLVMContext &C, MemoryAccess *DMA, Instruction *MI, BasicBlock *BB,
|
|
|
|
unsigned Ver)
|
2017-08-17 00:07:40 +02:00
|
|
|
: MemoryUseOrDef(C, DMA, MemoryDefVal, deleteMe, MI, BB), ID(Ver) {}
|
2016-02-02 23:46:49 +01:00
|
|
|
|
2017-01-14 01:32:38 +01:00
|
|
|
// allocate space for exactly one operand
|
|
|
|
void *operator new(size_t s) { return User::operator new(s, 1); }
|
|
|
|
|
2017-06-29 21:35:17 +02:00
|
|
|
static bool classof(const Value *MA) {
|
2016-02-02 23:46:49 +01:00
|
|
|
return MA->getValueID() == MemoryDefVal;
|
|
|
|
}
|
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
void setOptimized(MemoryAccess *MA) {
|
2017-04-05 01:43:10 +02:00
|
|
|
Optimized = MA;
|
|
|
|
OptimizedID = getDefiningAccess()->getID();
|
|
|
|
}
|
2017-08-17 00:07:40 +02:00
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
MemoryAccess *getOptimized() const { return Optimized; }
|
2017-08-17 00:07:40 +02:00
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
bool isOptimized() const {
|
2017-04-05 01:43:10 +02:00
|
|
|
return getOptimized() && getDefiningAccess() &&
|
|
|
|
OptimizedID == getDefiningAccess()->getID();
|
|
|
|
}
|
2017-08-17 00:07:40 +02:00
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
void resetOptimized() {
|
2017-04-05 01:43:10 +02:00
|
|
|
OptimizedID = INVALID_MEMORYACCESS_ID;
|
|
|
|
}
|
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
void print(raw_ostream &OS) const;
|
2016-02-02 23:46:49 +01:00
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
unsigned getID() const { return ID; }
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
private:
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
static void deleteMe(DerivedUser *Self);
|
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
const unsigned ID;
|
2018-02-23 19:50:39 +01:00
|
|
|
unsigned OptimizedID = INVALID_MEMORYACCESS_ID;
|
2017-08-17 00:07:40 +02:00
|
|
|
MemoryAccess *Optimized = nullptr;
|
2016-02-02 23:46:49 +01:00
|
|
|
};
|
2016-04-28 20:04:41 +02:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
template <>
|
|
|
|
struct OperandTraits<MemoryDef> : public FixedNumOperandTraits<MemoryDef, 1> {};
|
|
|
|
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(MemoryDef, MemoryAccess)
|
|
|
|
|
|
|
|
/// \brief 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:
|
|
|
|
/// Provide fast operand accessors
|
|
|
|
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(MemoryAccess);
|
|
|
|
|
|
|
|
MemoryPhi(LLVMContext &C, BasicBlock *BB, unsigned Ver, unsigned NumPreds = 0)
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
: MemoryAccess(C, MemoryPhiVal, deleteMe, BB, 0), ID(Ver),
|
|
|
|
ReservedSpace(NumPreds) {
|
2016-02-02 23:46:49 +01:00
|
|
|
allocHungoffUses(ReservedSpace);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block iterator interface. This provides access to the list of incoming
|
|
|
|
// basic blocks, which parallels the list of incoming values.
|
2017-08-17 00:07:40 +02:00
|
|
|
using block_iterator = BasicBlock **;
|
|
|
|
using const_block_iterator = BasicBlock *const *;
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
block_iterator block_begin() {
|
|
|
|
auto *Ref = reinterpret_cast<Use::UserRef *>(op_begin() + ReservedSpace);
|
|
|
|
return reinterpret_cast<block_iterator>(Ref + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const_block_iterator block_begin() const {
|
|
|
|
const auto *Ref =
|
|
|
|
reinterpret_cast<const Use::UserRef *>(op_begin() + ReservedSpace);
|
|
|
|
return reinterpret_cast<const_block_iterator>(Ref + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_iterator block_end() { return block_begin() + getNumOperands(); }
|
|
|
|
|
|
|
|
const_block_iterator block_end() const {
|
|
|
|
return block_begin() + getNumOperands();
|
|
|
|
}
|
|
|
|
|
2016-12-24 22:52:10 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
op_range incoming_values() { return operands(); }
|
|
|
|
|
|
|
|
const_op_range incoming_values() const { return operands(); }
|
|
|
|
|
|
|
|
/// \brief Return the number of incoming edges
|
|
|
|
unsigned getNumIncomingValues() const { return getNumOperands(); }
|
|
|
|
|
|
|
|
/// \brief 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);
|
|
|
|
}
|
2017-08-17 00:07:40 +02:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
static unsigned getOperandNumForIncomingValue(unsigned I) { return I; }
|
|
|
|
static unsigned getIncomingValueNumForOperand(unsigned I) { return I; }
|
|
|
|
|
|
|
|
/// \brief Return incoming basic block number @p i.
|
|
|
|
BasicBlock *getIncomingBlock(unsigned I) const { return block_begin()[I]; }
|
|
|
|
|
|
|
|
/// \brief 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()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *getIncomingValueForBlock(const BasicBlock *BB) const {
|
|
|
|
int Idx = getBasicBlockIndex(BB);
|
|
|
|
assert(Idx >= 0 && "Invalid basic block argument!");
|
|
|
|
return getIncomingValue(Idx);
|
|
|
|
}
|
|
|
|
|
2017-06-29 21:35:17 +02:00
|
|
|
static bool classof(const Value *V) {
|
2016-02-02 23:46:49 +01:00
|
|
|
return V->getValueID() == MemoryPhiVal;
|
|
|
|
}
|
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
void print(raw_ostream &OS) const;
|
|
|
|
|
|
|
|
unsigned getID() const { return ID; }
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
friend class MemorySSA;
|
2017-01-14 01:32:38 +01:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
/// \brief 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;
|
|
|
|
|
|
|
|
/// \brief 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);
|
|
|
|
}
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
|
|
|
|
static void deleteMe(DerivedUser *Self);
|
2016-02-02 23:46:49 +01:00
|
|
|
};
|
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:04:41 +02:00
|
|
|
template <> struct OperandTraits<MemoryPhi> : public HungoffOperandTraits<2> {};
|
2016-02-02 23:46:49 +01:00
|
|
|
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(MemoryPhi, MemoryAccess)
|
|
|
|
|
|
|
|
/// \brief Encapsulates MemorySSA, including all data associated with memory
|
|
|
|
/// accesses.
|
|
|
|
class MemorySSA {
|
|
|
|
public:
|
2016-06-01 23:30:40 +02:00
|
|
|
MemorySSA(Function &, AliasAnalysis *, DominatorTree *);
|
2016-02-02 23:46:49 +01:00
|
|
|
~MemorySSA();
|
|
|
|
|
2016-06-01 23:30:40 +02:00
|
|
|
MemorySSAWalker *getWalker();
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
/// \brief 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.
|
2016-11-01 22:17:46 +01:00
|
|
|
MemoryUseOrDef *getMemoryAccess(const Instruction *) const;
|
2016-02-02 23:46:49 +01:00
|
|
|
MemoryPhi *getMemoryAccess(const BasicBlock *BB) const;
|
|
|
|
|
|
|
|
void dump() const;
|
|
|
|
void print(raw_ostream &) const;
|
|
|
|
|
|
|
|
/// \brief 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();
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:56:19 +01:00
|
|
|
// 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>>;
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
/// \brief Return the list of MemoryAccess's for a given basic block.
|
|
|
|
///
|
|
|
|
/// This list is not modifiable by the user.
|
2016-06-20 22:21:33 +02:00
|
|
|
const AccessList *getBlockAccesses(const BasicBlock *BB) const {
|
2016-08-02 18:24:03 +02:00
|
|
|
return getWritableBlockAccesses(BB);
|
2016-02-02 23:46:49 +01:00
|
|
|
}
|
|
|
|
|
2017-01-25 21:56:19 +01:00
|
|
|
/// \brief 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);
|
|
|
|
}
|
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
/// \brief 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;
|
|
|
|
|
[MemorySSA] Update to the new shiny walker.
This patch updates MemorySSA's use-optimizing walker to be more
accurate and, in some cases, faster.
Essentially, this changed our core walking algorithm from a
cache-as-you-go DFS to an iteratively expanded DFS, with all of the
caching happening at the end. Said expansion happens when we hit a Phi,
P; we'll try to do the smallest amount of work possible to see if
optimizing above that Phi is legal in the first place. If so, we'll
expand the search to see if we can optimize to the next phi, etc.
An iteratively expanded DFS lets us potentially quit earlier (because we
don't assume that we can optimize above all phis) than our old walker.
Additionally, because we don't cache as we go, we can now optimize above
loops.
As an added bonus, this patch adds a ton of verification (if
EXPENSIVE_CHECKS are enabled), so finding bugs is easier.
Differential Revision: https://reviews.llvm.org/D21777
llvm-svn: 275940
2016-07-19 03:29:15 +02:00
|
|
|
/// \brief 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;
|
|
|
|
|
2016-08-05 23:46:52 +02:00
|
|
|
/// \brief Given a MemoryAccess and a Use, determine whether MemoryAccess \p A
|
|
|
|
/// dominates Use \p B.
|
|
|
|
bool dominates(const MemoryAccess *A, const Use &B) const;
|
|
|
|
|
2016-02-10 18:39:43 +01:00
|
|
|
/// \brief 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() const;
|
|
|
|
|
2017-02-22 23:19:55 +01:00
|
|
|
/// Used in various insertion functions to specify whether we are talking
|
|
|
|
/// about the beginning or end of a block.
|
|
|
|
enum InsertionPlace { Beginning, End };
|
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
protected:
|
|
|
|
// Used by Memory SSA annotater, dumpers, and wrapper pass
|
|
|
|
friend class MemorySSAAnnotatedWriter;
|
2016-06-01 23:30:40 +02:00
|
|
|
friend class MemorySSAPrinterLegacyPass;
|
Introduce a basic MemorySSA updater, that supports insertDef,
insertUse, moveBefore and moveAfter operations.
Summary:
This creates a basic MemorySSA updater that handles arbitrary
insertion of uses and defs into MemorySSA, as well as arbitrary
movement around the CFG. It replaces the current splice API.
It can be made to handle arbitrary control flow changes.
Currently, it uses the same updater algorithm from D28934.
The main difference is because MemorySSA is single variable, we have
the complete def and use list, and don't need anyone to give it to us
as part of the API. We also have to rename stores below us in some
cases.
If we go that direction in that patch, i will merge all the updater
implementations (using an updater_traits or something to provide the
get* functions we use, called read*/write* in that patch).
Sadly, the current SSAUpdater algorithm is way too slow to use for
what we are doing here.
I have updated the tests we have to basically build memoryssa
incrementally using the updater api, and make sure it still comes out
the same.
Reviewers: george.burgess.iv
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29047
llvm-svn: 293356
2017-01-28 02:23:13 +01:00
|
|
|
friend class MemorySSAUpdater;
|
2017-01-14 01:32:38 +01:00
|
|
|
|
2016-02-10 18:39:43 +01:00
|
|
|
void verifyDefUses(Function &F) const;
|
|
|
|
void verifyDomination(Function &F) const;
|
2016-06-21 20:39:20 +02:00
|
|
|
void verifyOrdering(Function &F) const;
|
2016-02-02 23:46:49 +01:00
|
|
|
|
2017-01-25 21:56:19 +01:00
|
|
|
// This is used by the use optimizer and updater.
|
2016-08-02 18:24:03 +02:00
|
|
|
AccessList *getWritableBlockAccesses(const BasicBlock *BB) const {
|
|
|
|
auto It = PerBlockAccesses.find(BB);
|
|
|
|
return It == PerBlockAccesses.end() ? nullptr : It->second.get();
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:56:19 +01:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:19:55 +01:00
|
|
|
// 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.
|
|
|
|
|
Introduce a basic MemorySSA updater, that supports insertDef,
insertUse, moveBefore and moveAfter operations.
Summary:
This creates a basic MemorySSA updater that handles arbitrary
insertion of uses and defs into MemorySSA, as well as arbitrary
movement around the CFG. It replaces the current splice API.
It can be made to handle arbitrary control flow changes.
Currently, it uses the same updater algorithm from D28934.
The main difference is because MemorySSA is single variable, we have
the complete def and use list, and don't need anyone to give it to us
as part of the API. We also have to rename stores below us in some
cases.
If we go that direction in that patch, i will merge all the updater
implementations (using an updater_traits or something to provide the
get* functions we use, called read*/write* in that patch).
Sadly, the current SSAUpdater algorithm is way too slow to use for
what we are doing here.
I have updated the tests we have to basically build memoryssa
incrementally using the updater api, and make sure it still comes out
the same.
Reviewers: george.burgess.iv
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29047
llvm-svn: 293356
2017-01-28 02:23:13 +01:00
|
|
|
void moveTo(MemoryUseOrDef *What, BasicBlock *BB, AccessList::iterator Where);
|
2017-01-30 12:35:39 +01:00
|
|
|
void moveTo(MemoryUseOrDef *What, BasicBlock *BB, InsertionPlace Point);
|
2017-08-17 00:07:40 +02:00
|
|
|
|
2017-02-20 23:26:03 +01:00
|
|
|
// 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);
|
|
|
|
}
|
2017-08-17 00:07:40 +02:00
|
|
|
|
2017-02-22 23:19:55 +01:00
|
|
|
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 *);
|
Introduce a basic MemorySSA updater, that supports insertDef,
insertUse, moveBefore and moveAfter operations.
Summary:
This creates a basic MemorySSA updater that handles arbitrary
insertion of uses and defs into MemorySSA, as well as arbitrary
movement around the CFG. It replaces the current splice API.
It can be made to handle arbitrary control flow changes.
Currently, it uses the same updater algorithm from D28934.
The main difference is because MemorySSA is single variable, we have
the complete def and use list, and don't need anyone to give it to us
as part of the API. We also have to rename stores below us in some
cases.
If we go that direction in that patch, i will merge all the updater
implementations (using an updater_traits or something to provide the
get* functions we use, called read*/write* in that patch).
Sadly, the current SSAUpdater algorithm is way too slow to use for
what we are doing here.
I have updated the tests we have to basically build memoryssa
incrementally using the updater api, and make sure it still comes out
the same.
Reviewers: george.burgess.iv
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29047
llvm-svn: 293356
2017-01-28 02:23:13 +01:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
private:
|
2016-06-24 23:02:12 +02:00
|
|
|
class CachingWalker;
|
2016-08-02 18:24:03 +02:00
|
|
|
class OptimizeUses;
|
[MemorySSA] Update to the new shiny walker.
This patch updates MemorySSA's use-optimizing walker to be more
accurate and, in some cases, faster.
Essentially, this changed our core walking algorithm from a
cache-as-you-go DFS to an iteratively expanded DFS, with all of the
caching happening at the end. Said expansion happens when we hit a Phi,
P; we'll try to do the smallest amount of work possible to see if
optimizing above that Phi is legal in the first place. If so, we'll
expand the search to see if we can optimize to the next phi, etc.
An iteratively expanded DFS lets us potentially quit earlier (because we
don't assume that we can optimize above all phis) than our old walker.
Additionally, because we don't cache as we go, we can now optimize above
loops.
As an added bonus, this patch adds a ton of verification (if
EXPENSIVE_CHECKS are enabled), so finding bugs is easier.
Differential Revision: https://reviews.llvm.org/D21777
llvm-svn: 275940
2016-07-19 03:29:15 +02:00
|
|
|
|
|
|
|
CachingWalker *getWalkerImpl();
|
2016-06-27 20:22:27 +02:00
|
|
|
void buildMemorySSA();
|
2016-08-02 18:24:03 +02:00
|
|
|
void optimizeUses();
|
|
|
|
|
2016-02-10 18:39:43 +01:00
|
|
|
void verifyUseInDefs(MemoryAccess *, MemoryAccess *) const;
|
2017-08-17 00:07:40 +02:00
|
|
|
|
2016-06-20 22:21:33 +02:00
|
|
|
using AccessMap = DenseMap<const BasicBlock *, std::unique_ptr<AccessList>>;
|
2017-01-25 21:56:19 +01:00
|
|
|
using DefsMap = DenseMap<const BasicBlock *, std::unique_ptr<DefsList>>;
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
determineInsertionPoint(const SmallPtrSetImpl<BasicBlock *> &DefiningBlocks);
|
|
|
|
void markUnreachableAsLiveOnEntry(BasicBlock *BB);
|
|
|
|
bool dominatesUse(const MemoryAccess *, const MemoryAccess *) const;
|
2017-02-22 23:19:55 +01:00
|
|
|
MemoryPhi *createMemoryPhi(BasicBlock *BB);
|
2016-05-26 03:19:17 +02:00
|
|
|
MemoryUseOrDef *createNewAccess(Instruction *);
|
2016-02-02 23:46:49 +01:00
|
|
|
MemoryAccess *findDominatingDef(BasicBlock *, enum InsertionPlace);
|
2016-11-21 20:33:02 +01:00
|
|
|
void placePHINodes(const SmallPtrSetImpl<BasicBlock *> &,
|
|
|
|
const DenseMap<const BasicBlock *, unsigned int> &);
|
2017-02-20 23:26:03 +01:00
|
|
|
MemoryAccess *renameBlock(BasicBlock *, MemoryAccess *, bool);
|
|
|
|
void renameSuccessorPhis(BasicBlock *, MemoryAccess *, bool);
|
2016-02-02 23:46:49 +01:00
|
|
|
void renamePass(DomTreeNode *, MemoryAccess *IncomingVal,
|
2017-02-20 23:26:03 +01:00
|
|
|
SmallPtrSetImpl<BasicBlock *> &Visited,
|
|
|
|
bool SkipVisited = false, bool RenameAllUses = false);
|
2016-06-21 20:39:20 +02:00
|
|
|
AccessList *getOrCreateAccessList(const BasicBlock *);
|
2017-01-25 21:56:19 +01:00
|
|
|
DefsList *getOrCreateDefsList(const BasicBlock *);
|
2016-07-20 00:49:43 +02:00
|
|
|
void renumberBlock(const BasicBlock *) const;
|
2016-02-02 23:46:49 +01:00
|
|
|
AliasAnalysis *AA;
|
|
|
|
DominatorTree *DT;
|
|
|
|
Function &F;
|
|
|
|
|
|
|
|
// Memory SSA mappings
|
2016-02-10 18:41:25 +01:00
|
|
|
DenseMap<const Value *, MemoryAccess *> ValueToMemoryAccess;
|
2017-08-17 00:07:40 +02:00
|
|
|
|
2017-01-25 21:56:19 +01:00
|
|
|
// 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.
|
2016-02-02 23:46:49 +01:00
|
|
|
AccessMap PerBlockAccesses;
|
2017-01-25 21:56:19 +01:00
|
|
|
DefsMap PerBlockDefs;
|
2016-02-02 23:46:49 +01:00
|
|
|
std::unique_ptr<MemoryAccess> LiveOnEntryDef;
|
|
|
|
|
2016-07-20 00:49:43 +02:00
|
|
|
// 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;
|
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
// Memory SSA building info
|
2016-06-24 23:02:12 +02:00
|
|
|
std::unique_ptr<CachingWalker> Walker;
|
2016-02-02 23:46:49 +01:00
|
|
|
unsigned NextID;
|
|
|
|
};
|
|
|
|
|
2017-03-03 00:06:46 +01:00
|
|
|
// Internal MemorySSA utils, for use by MemorySSA classes and walkers
|
2017-04-01 11:44:14 +02:00
|
|
|
class MemorySSAUtil {
|
2017-03-03 00:06:46 +01:00
|
|
|
protected:
|
|
|
|
friend class GVNHoist;
|
2017-08-17 00:07:40 +02:00
|
|
|
friend class MemorySSAWalker;
|
|
|
|
|
2017-03-03 00:06:46 +01:00
|
|
|
// This function should not be used by new passes.
|
|
|
|
static bool defClobbersUseOrDef(MemoryDef *MD, const MemoryUseOrDef *MU,
|
|
|
|
AliasAnalysis &AA);
|
|
|
|
};
|
|
|
|
|
2016-07-06 23:20:47 +02:00
|
|
|
// 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;
|
2017-01-14 01:32:38 +01:00
|
|
|
|
|
|
|
static char ID;
|
2016-07-06 23:20:47 +02:00
|
|
|
};
|
|
|
|
|
2016-06-01 23:30:40 +02:00
|
|
|
/// An analysis that produces \c MemorySSA for a function.
|
|
|
|
///
|
|
|
|
class MemorySSAAnalysis : public AnalysisInfoMixin<MemorySSAAnalysis> {
|
|
|
|
friend AnalysisInfoMixin<MemorySSAAnalysis>;
|
2017-01-14 01:32:38 +01:00
|
|
|
|
2016-11-23 18:53:26 +01:00
|
|
|
static AnalysisKey Key;
|
2016-06-01 23:30:40 +02:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
public:
|
2016-08-08 20:27:22 +02:00
|
|
|
// 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)) {}
|
2017-08-17 00:07:40 +02:00
|
|
|
|
2016-08-08 20:27:22 +02:00
|
|
|
MemorySSA &getMSSA() { return *MSSA.get(); }
|
2016-08-08 21:33:27 +02:00
|
|
|
|
2016-08-08 20:27:22 +02:00
|
|
|
std::unique_ptr<MemorySSA> MSSA;
|
|
|
|
};
|
2016-02-02 23:46:49 +01:00
|
|
|
|
2016-08-09 02:28:15 +02:00
|
|
|
Result run(Function &F, FunctionAnalysisManager &AM);
|
2016-06-01 23:30:40 +02:00
|
|
|
};
|
2016-02-02 23:46:49 +01:00
|
|
|
|
2016-06-01 23:30:40 +02:00
|
|
|
/// \brief Printer pass for \c MemorySSA.
|
|
|
|
class MemorySSAPrinterPass : public PassInfoMixin<MemorySSAPrinterPass> {
|
|
|
|
raw_ostream &OS;
|
2016-02-02 23:46:49 +01:00
|
|
|
|
2016-06-01 23:30:40 +02:00
|
|
|
public:
|
|
|
|
explicit MemorySSAPrinterPass(raw_ostream &OS) : OS(OS) {}
|
2017-01-14 01:32:38 +01:00
|
|
|
|
2016-08-09 02:28:15 +02:00
|
|
|
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
|
2016-06-01 23:30:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Verifier pass for \c MemorySSA.
|
|
|
|
struct MemorySSAVerifierPass : PassInfoMixin<MemorySSAVerifierPass> {
|
2016-08-09 02:28:15 +02:00
|
|
|
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
|
2016-02-02 23:46:49 +01:00
|
|
|
};
|
|
|
|
|
2016-06-01 23:30:40 +02:00
|
|
|
/// \brief Legacy analysis pass which computes \c MemorySSA.
|
|
|
|
class MemorySSAWrapperPass : public FunctionPass {
|
2016-02-02 23:46:49 +01:00
|
|
|
public:
|
2016-06-01 23:30:40 +02:00
|
|
|
MemorySSAWrapperPass();
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
static char ID;
|
2017-01-14 01:32:38 +01:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
bool runOnFunction(Function &) override;
|
|
|
|
void releaseMemory() override;
|
2016-06-01 23:30:40 +02:00
|
|
|
MemorySSA &getMSSA() { return *MSSA; }
|
|
|
|
const MemorySSA &getMSSA() const { return *MSSA; }
|
2016-02-02 23:46:49 +01:00
|
|
|
|
2016-06-01 23:30:40 +02:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
|
|
|
|
void verifyAnalysis() const override;
|
|
|
|
void print(raw_ostream &OS, const Module *M = nullptr) const override;
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<MemorySSA> MSSA;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief 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 *);
|
2017-01-14 01:32:38 +01:00
|
|
|
virtual ~MemorySSAWalker() = default;
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
using MemoryAccessSet = SmallVector<MemoryAccess *, 8>;
|
|
|
|
|
|
|
|
/// \brief 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)
|
2016-07-20 21:51:34 +02:00
|
|
|
/// store %b
|
2016-02-02 23:46:49 +01:00
|
|
|
/// }
|
|
|
|
/// 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.
|
2016-07-20 21:51:34 +02:00
|
|
|
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;
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
/// \brief 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 *,
|
2016-10-28 21:22:46 +02:00
|
|
|
const MemoryLocation &) = 0;
|
2016-04-28 20:04:41 +02:00
|
|
|
|
2016-03-01 19:46:54 +01:00
|
|
|
/// \brief 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 *) {}
|
2016-02-02 23:46:49 +01:00
|
|
|
|
2016-08-08 19:52:01 +02:00
|
|
|
virtual void verify(const MemorySSA *MSSA) { assert(MSSA == this->MSSA); }
|
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
protected:
|
2016-06-01 23:30:40 +02:00
|
|
|
friend class MemorySSA; // For updating MSSA pointer in MemorySSA move
|
|
|
|
// constructor.
|
2016-02-02 23:46:49 +01:00
|
|
|
MemorySSA *MSSA;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief 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:
|
2016-07-20 21:51:34 +02:00
|
|
|
// Keep the overrides below from hiding the Instruction overload of
|
|
|
|
// getClobberingMemoryAccess.
|
|
|
|
using MemorySSAWalker::getClobberingMemoryAccess;
|
2017-01-14 01:32:38 +01:00
|
|
|
|
2016-07-20 21:51:34 +02:00
|
|
|
MemoryAccess *getClobberingMemoryAccess(MemoryAccess *) override;
|
2016-02-02 23:46:49 +01:00
|
|
|
MemoryAccess *getClobberingMemoryAccess(MemoryAccess *,
|
2016-10-28 21:22:46 +02:00
|
|
|
const MemoryLocation &) override;
|
2016-02-02 23:46:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
using MemoryAccessPair = std::pair<MemoryAccess *, MemoryLocation>;
|
|
|
|
using ConstMemoryAccessPair = std::pair<const MemoryAccess *, MemoryLocation>;
|
|
|
|
|
|
|
|
/// \brief 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:
|
2017-01-14 01:32:38 +01:00
|
|
|
memoryaccess_def_iterator_base(T *Start) : Access(Start) {}
|
|
|
|
memoryaccess_def_iterator_base() = default;
|
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
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);
|
|
|
|
}
|
2017-08-17 00:07:40 +02:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
typename BaseT::iterator::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 (MemoryPhi *MP = dyn_cast<MemoryPhi>(Access))
|
|
|
|
return MP->getIncomingValue(ArgNo);
|
|
|
|
return cast<MemoryUseOrDef>(Access)->getDefiningAccess();
|
|
|
|
}
|
2017-08-17 00:07:40 +02:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
using BaseT::operator++;
|
|
|
|
memoryaccess_def_iterator &operator++() {
|
|
|
|
assert(Access && "Hit end of iterator");
|
|
|
|
if (MemoryPhi *MP = dyn_cast<MemoryPhi>(Access)) {
|
|
|
|
if (++ArgNo >= MP->getNumIncomingValues()) {
|
|
|
|
ArgNo = 0;
|
|
|
|
Access = nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Access = nullptr;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-01-14 01:32:38 +01:00
|
|
|
T *Access = nullptr;
|
|
|
|
unsigned ArgNo = 0;
|
2016-02-02 23:46:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline memoryaccess_def_iterator MemoryAccess::defs_begin() {
|
|
|
|
return memoryaccess_def_iterator(this);
|
|
|
|
}
|
2016-04-28 20:04:41 +02:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief GraphTraits for a MemoryAccess, which walks defs in the normal case,
|
|
|
|
/// and uses in the inverse case.
|
|
|
|
template <> struct GraphTraits<MemoryAccess *> {
|
2016-08-22 23:09:30 +02:00
|
|
|
using NodeRef = MemoryAccess *;
|
2016-02-02 23:46:49 +01:00
|
|
|
using ChildIteratorType = memoryaccess_def_iterator;
|
|
|
|
|
2016-08-22 23:09:30 +02:00
|
|
|
static NodeRef getEntryNode(NodeRef N) { return N; }
|
2016-08-31 18:48:13 +02:00
|
|
|
static ChildIteratorType child_begin(NodeRef N) { return N->defs_begin(); }
|
|
|
|
static ChildIteratorType child_end(NodeRef N) { return N->defs_end(); }
|
2016-02-02 23:46:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct GraphTraits<Inverse<MemoryAccess *>> {
|
2016-08-22 23:09:30 +02:00
|
|
|
using NodeRef = MemoryAccess *;
|
2016-02-02 23:46:49 +01:00
|
|
|
using ChildIteratorType = MemoryAccess::iterator;
|
|
|
|
|
2016-08-22 23:09:30 +02:00
|
|
|
static NodeRef getEntryNode(NodeRef N) { return N; }
|
2016-08-31 18:48:13 +02:00
|
|
|
static ChildIteratorType child_begin(NodeRef N) { return N->user_begin(); }
|
|
|
|
static ChildIteratorType child_end(NodeRef N) { return N->user_end(); }
|
2016-02-02 23:46:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief 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)
|
|
|
|
: DefIterator(Info.first), Location(Info.second),
|
|
|
|
OriginalAccess(Info.first) {
|
|
|
|
CurrentPair.first = nullptr;
|
|
|
|
|
|
|
|
WalkingPhi = Info.first && isa<MemoryPhi>(Info.first);
|
|
|
|
fillInCurrentPair();
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:56:19 +01:00
|
|
|
upward_defs_iterator() { CurrentPair.first = nullptr; }
|
2016-02-02 23:46:49 +01:00
|
|
|
|
|
|
|
bool operator==(const upward_defs_iterator &Other) const {
|
|
|
|
return DefIterator == Other.DefIterator;
|
|
|
|
}
|
|
|
|
|
2016-02-03 00:15:26 +01:00
|
|
|
BaseT::iterator::reference operator*() const {
|
2016-02-02 23:46:49 +01:00
|
|
|
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:
|
|
|
|
void fillInCurrentPair() {
|
|
|
|
CurrentPair.first = *DefIterator;
|
|
|
|
if (WalkingPhi && Location.Ptr) {
|
|
|
|
PHITransAddr Translator(
|
|
|
|
const_cast<Value *>(Location.Ptr),
|
2016-12-19 09:22:17 +01:00
|
|
|
OriginalAccess->getBlock()->getModule()->getDataLayout(), nullptr);
|
2016-02-02 23:46:49 +01:00
|
|
|
if (!Translator.PHITranslateValue(OriginalAccess->getBlock(),
|
|
|
|
DefIterator.getPhiArgBlock(), nullptr,
|
|
|
|
false))
|
|
|
|
if (Translator.getAddr() != Location.Ptr) {
|
|
|
|
CurrentPair.second = Location.getWithNewPtr(Translator.getAddr());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CurrentPair.second = Location;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryAccessPair CurrentPair;
|
|
|
|
memoryaccess_def_iterator DefIterator;
|
|
|
|
MemoryLocation Location;
|
2017-01-14 01:32:38 +01:00
|
|
|
MemoryAccess *OriginalAccess = nullptr;
|
|
|
|
bool WalkingPhi = false;
|
2016-02-02 23:46:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline upward_defs_iterator upward_defs_begin(const MemoryAccessPair &Pair) {
|
|
|
|
return upward_defs_iterator(Pair);
|
|
|
|
}
|
2016-04-28 20:04:41 +02:00
|
|
|
|
2016-02-02 23:46:49 +01:00
|
|
|
inline upward_defs_iterator upward_defs_end() { return upward_defs_iterator(); }
|
|
|
|
|
2017-04-01 11:44:19 +02:00
|
|
|
inline iterator_range<upward_defs_iterator>
|
|
|
|
upward_defs(const MemoryAccessPair &Pair) {
|
|
|
|
return make_range(upward_defs_begin(Pair), upward_defs_end());
|
|
|
|
}
|
|
|
|
|
2017-04-05 01:43:10 +02:00
|
|
|
/// 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>
|
2017-04-01 11:44:19 +02:00
|
|
|
struct def_chain_iterator
|
2017-04-05 19:26:25 +02:00
|
|
|
: public iterator_facade_base<def_chain_iterator<T, UseOptimizedChain>,
|
2017-04-01 11:44:24 +02:00
|
|
|
std::forward_iterator_tag, MemoryAccess *> {
|
2017-04-01 11:44:19 +02:00
|
|
|
def_chain_iterator() : MA(nullptr) {}
|
2017-04-01 11:44:24 +02:00
|
|
|
def_chain_iterator(T MA) : MA(MA) {}
|
2017-04-01 11:44:19 +02:00
|
|
|
|
2017-04-01 11:44:24 +02:00
|
|
|
T operator*() const { return MA; }
|
2017-04-01 11:44:19 +02:00
|
|
|
|
|
|
|
def_chain_iterator &operator++() {
|
|
|
|
// N.B. liveOnEntry has a null defining access.
|
2017-04-05 01:43:10 +02:00
|
|
|
if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA)) {
|
|
|
|
if (UseOptimizedChain && MUD->isOptimized())
|
|
|
|
MA = MUD->getOptimized();
|
|
|
|
else
|
|
|
|
MA = MUD->getDefiningAccess();
|
|
|
|
} else {
|
2017-04-01 11:44:19 +02:00
|
|
|
MA = nullptr;
|
2017-04-05 01:43:10 +02:00
|
|
|
}
|
|
|
|
|
2017-04-01 11:44:19 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const def_chain_iterator &O) const { return MA == O.MA; }
|
|
|
|
|
|
|
|
private:
|
2017-04-01 11:44:24 +02:00
|
|
|
T MA;
|
2017-04-01 11:44:19 +02:00
|
|
|
};
|
|
|
|
|
2017-04-01 11:44:24 +02:00
|
|
|
template <class T>
|
|
|
|
inline iterator_range<def_chain_iterator<T>>
|
|
|
|
def_chain(T MA, MemoryAccess *UpTo = nullptr) {
|
2017-04-01 11:44:19 +02:00
|
|
|
#ifdef EXPENSIVE_CHECKS
|
2017-04-01 12:04:28 +02:00
|
|
|
assert((!UpTo || find(def_chain(MA), UpTo) != def_chain_iterator<T>()) &&
|
2017-04-01 11:44:19 +02:00
|
|
|
"UpTo isn't in the def chain!");
|
|
|
|
#endif
|
2017-04-01 11:44:24 +02:00
|
|
|
return make_range(def_chain_iterator<T>(MA), def_chain_iterator<T>(UpTo));
|
2017-04-01 11:44:19 +02:00
|
|
|
}
|
|
|
|
|
2017-04-05 01:43:10 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:04:41 +02:00
|
|
|
} // end namespace llvm
|
|
|
|
|
2017-04-11 22:06:36 +02:00
|
|
|
#endif // LLVM_ANALYSIS_MEMORYSSA_H
|