2016-07-15 21:45:20 +08:00
|
|
|
//===- GVNHoist.cpp - Hoist scalar and load expressions -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass hoists expressions from branches to a common dominator. It uses
|
|
|
|
// GVN (global value numbering) to discover expressions computing the same
|
2016-08-13 19:56:50 +08:00
|
|
|
// values. The primary goals of code-hoisting are:
|
|
|
|
// 1. To reduce the code size.
|
|
|
|
// 2. In some cases reduce critical path (by exposing more ILP).
|
|
|
|
//
|
2017-09-13 13:28:03 +08:00
|
|
|
// The algorithm factors out the reachability of values such that multiple
|
|
|
|
// queries to find reachability of values are fast. This is based on finding the
|
|
|
|
// ANTIC points in the CFG which do not change during hoisting. The ANTIC points
|
|
|
|
// are basically the dominance-frontiers in the inverse graph. So we introduce a
|
|
|
|
// data structure (CHI nodes) to keep track of values flowing out of a basic
|
|
|
|
// block. We only do this for values with multiple occurrences in the function
|
|
|
|
// as they are the potential hoistable candidates. This approach allows us to
|
|
|
|
// hoist instructions to a basic block with more than two successors, as well as
|
|
|
|
// deal with infinite loops in a trivial way.
|
|
|
|
//
|
|
|
|
// Limitations: This pass does not hoist fully redundant expressions because
|
|
|
|
// they are already handled by GVN-PRE. It is advisable to run gvn-hoist before
|
|
|
|
// and after gvn-pre because gvn-pre creates opportunities for more instructions
|
|
|
|
// to be hoisted.
|
|
|
|
//
|
2016-07-15 21:45:20 +08:00
|
|
|
// Hoisting may affect the performance in some cases. To mitigate that, hoisting
|
|
|
|
// is disabled in the following cases.
|
|
|
|
// 1. Scalars across calls.
|
|
|
|
// 2. geps when corresponding load/store cannot be hoisted.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-07-15 21:45:20 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-07-15 21:45:20 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2017-04-18 21:25:49 +08:00
|
|
|
#include "llvm/Analysis/GlobalsModRef.h"
|
2017-09-13 13:28:03 +08:00
|
|
|
#include "llvm/Analysis/IteratedDominanceFrontier.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
|
2017-04-12 04:06:36 +08:00
|
|
|
#include "llvm/Analysis/MemorySSA.h"
|
|
|
|
#include "llvm/Analysis/MemorySSAUpdater.h"
|
2017-09-13 13:28:03 +08:00
|
|
|
#include "llvm/Analysis/PostDominators.h"
|
2018-03-22 06:34:23 +08:00
|
|
|
#include "llvm/Analysis/Utils/Local.h"
|
2016-07-15 21:45:20 +08:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include "llvm/IR/Argument.h"
|
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/CFG.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2017-09-08 07:27:44 +08:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include "llvm/IR/Intrinsics.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/PassManager.h"
|
|
|
|
#include "llvm/IR/Use.h"
|
|
|
|
#include "llvm/IR/User.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-07-15 21:45:20 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Transforms/Scalar/GVN.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <iterator>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2017-09-13 13:28:03 +08:00
|
|
|
|
2016-07-15 21:45:20 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "gvn-hoist"
|
|
|
|
|
|
|
|
STATISTIC(NumHoisted, "Number of instructions hoisted");
|
|
|
|
STATISTIC(NumRemoved, "Number of instructions removed");
|
|
|
|
STATISTIC(NumLoadsHoisted, "Number of loads hoisted");
|
|
|
|
STATISTIC(NumLoadsRemoved, "Number of loads removed");
|
|
|
|
STATISTIC(NumStoresHoisted, "Number of stores hoisted");
|
|
|
|
STATISTIC(NumStoresRemoved, "Number of stores removed");
|
|
|
|
STATISTIC(NumCallsHoisted, "Number of calls hoisted");
|
|
|
|
STATISTIC(NumCallsRemoved, "Number of calls removed");
|
|
|
|
|
|
|
|
static cl::opt<int>
|
|
|
|
MaxHoistedThreshold("gvn-max-hoisted", cl::Hidden, cl::init(-1),
|
|
|
|
cl::desc("Max number of instructions to hoist "
|
|
|
|
"(default unlimited = -1)"));
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2016-07-15 21:45:20 +08:00
|
|
|
static cl::opt<int> MaxNumberOfBBSInPath(
|
|
|
|
"gvn-hoist-max-bbs", cl::Hidden, cl::init(4),
|
|
|
|
cl::desc("Max number of basic blocks on the path between "
|
|
|
|
"hoisting locations (default = 4, unlimited = -1)"));
|
|
|
|
|
2016-07-26 08:15:08 +08:00
|
|
|
static cl::opt<int> MaxDepthInBB(
|
|
|
|
"gvn-hoist-max-depth", cl::Hidden, cl::init(100),
|
|
|
|
cl::desc("Hoist instructions from the beginning of the BB up to the "
|
|
|
|
"maximum specified depth (default = 100, unlimited = -1)"));
|
|
|
|
|
2016-10-13 09:39:10 +08:00
|
|
|
static cl::opt<int>
|
|
|
|
MaxChainLength("gvn-hoist-max-chain-length", cl::Hidden, cl::init(10),
|
|
|
|
cl::desc("Maximum length of dependent chains to hoist "
|
|
|
|
"(default = 10, unlimited = -1)"));
|
2016-08-04 04:54:38 +08:00
|
|
|
|
2017-03-03 07:06:46 +08:00
|
|
|
namespace llvm {
|
2016-07-15 21:45:20 +08:00
|
|
|
|
2017-10-14 05:17:07 +08:00
|
|
|
using BBSideEffectsSet = DenseMap<const BasicBlock *, bool>;
|
|
|
|
using SmallVecInsn = SmallVector<Instruction *, 4>;
|
|
|
|
using SmallVecImplInsn = SmallVectorImpl<Instruction *>;
|
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
// Each element of a hoisting list contains the basic block where to hoist and
|
|
|
|
// a list of instructions to be hoisted.
|
2017-10-14 05:17:07 +08:00
|
|
|
using HoistingPointInfo = std::pair<BasicBlock *, SmallVecInsn>;
|
|
|
|
|
|
|
|
using HoistingPointList = SmallVector<HoistingPointInfo, 4>;
|
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
// A map from a pair of VNs to all the instructions with those VNs.
|
2017-10-14 05:17:07 +08:00
|
|
|
using VNType = std::pair<unsigned, unsigned>;
|
|
|
|
|
|
|
|
using VNtoInsns = DenseMap<VNType, SmallVector<Instruction *, 4>>;
|
2017-09-13 13:28:03 +08:00
|
|
|
|
|
|
|
// CHI keeps information about values flowing out of a basic block. It is
|
|
|
|
// similar to PHI but in the inverse graph, and used for outgoing values on each
|
|
|
|
// edge. For conciseness, it is computed only for instructions with multiple
|
|
|
|
// occurrences in the CFG because they are the only hoistable candidates.
|
|
|
|
// A (CHI[{V, B, I1}, {V, C, I2}]
|
|
|
|
// / \
|
|
|
|
// / \
|
|
|
|
// B(I1) C (I2)
|
|
|
|
// The Value number for both I1 and I2 is V, the CHI node will save the
|
|
|
|
// instruction as well as the edge where the value is flowing to.
|
|
|
|
struct CHIArg {
|
|
|
|
VNType VN;
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
// Edge destination (shows the direction of flow), may not be where the I is.
|
|
|
|
BasicBlock *Dest;
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
// The instruction (VN) which uses the values flowing out of CHI.
|
|
|
|
Instruction *I;
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
bool operator==(const CHIArg &A) { return VN == A.VN; }
|
|
|
|
bool operator!=(const CHIArg &A) { return !(*this == A); }
|
2016-07-15 21:45:20 +08:00
|
|
|
};
|
|
|
|
|
2017-10-14 05:17:07 +08:00
|
|
|
using CHIIt = SmallVectorImpl<CHIArg>::iterator;
|
|
|
|
using CHIArgs = iterator_range<CHIIt>;
|
|
|
|
using OutValuesType = DenseMap<BasicBlock *, SmallVector<CHIArg, 2>>;
|
|
|
|
using InValuesType =
|
|
|
|
DenseMap<BasicBlock *, SmallVector<std::pair<VNType, Instruction *>, 2>>;
|
2017-09-13 13:28:03 +08:00
|
|
|
|
2016-07-18 14:11:37 +08:00
|
|
|
// An invalid value number Used when inserting a single value number into
|
|
|
|
// VNtoInsns.
|
2016-07-19 02:53:50 +08:00
|
|
|
enum : unsigned { InvalidVN = ~2U };
|
2016-07-15 21:45:20 +08:00
|
|
|
|
|
|
|
// Records all scalar instructions candidate for code hoisting.
|
|
|
|
class InsnInfo {
|
|
|
|
VNtoInsns VNtoScalars;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Inserts I and its value number in VNtoScalars.
|
|
|
|
void insert(Instruction *I, GVN::ValueTable &VN) {
|
|
|
|
// Scalar instruction.
|
|
|
|
unsigned V = VN.lookupOrAdd(I);
|
2016-07-18 14:11:37 +08:00
|
|
|
VNtoScalars[{V, InvalidVN}].push_back(I);
|
2016-07-15 21:45:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const VNtoInsns &getVNTable() const { return VNtoScalars; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Records all load instructions candidate for code hoisting.
|
|
|
|
class LoadInfo {
|
|
|
|
VNtoInsns VNtoLoads;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Insert Load and the value number of its memory address in VNtoLoads.
|
|
|
|
void insert(LoadInst *Load, GVN::ValueTable &VN) {
|
|
|
|
if (Load->isSimple()) {
|
|
|
|
unsigned V = VN.lookupOrAdd(Load->getPointerOperand());
|
2016-07-18 14:11:37 +08:00
|
|
|
VNtoLoads[{V, InvalidVN}].push_back(Load);
|
2016-07-15 21:45:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const VNtoInsns &getVNTable() const { return VNtoLoads; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Records all store instructions candidate for code hoisting.
|
|
|
|
class StoreInfo {
|
|
|
|
VNtoInsns VNtoStores;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Insert the Store and a hash number of the store address and the stored
|
|
|
|
// value in VNtoStores.
|
|
|
|
void insert(StoreInst *Store, GVN::ValueTable &VN) {
|
|
|
|
if (!Store->isSimple())
|
|
|
|
return;
|
|
|
|
// Hash the store address and the stored value.
|
|
|
|
Value *Ptr = Store->getPointerOperand();
|
|
|
|
Value *Val = Store->getValueOperand();
|
2016-07-18 14:11:37 +08:00
|
|
|
VNtoStores[{VN.lookupOrAdd(Ptr), VN.lookupOrAdd(Val)}].push_back(Store);
|
2016-07-15 21:45:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const VNtoInsns &getVNTable() const { return VNtoStores; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Records all call instructions candidate for code hoisting.
|
|
|
|
class CallInfo {
|
|
|
|
VNtoInsns VNtoCallsScalars;
|
|
|
|
VNtoInsns VNtoCallsLoads;
|
|
|
|
VNtoInsns VNtoCallsStores;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Insert Call and its value numbering in one of the VNtoCalls* containers.
|
|
|
|
void insert(CallInst *Call, GVN::ValueTable &VN) {
|
|
|
|
// A call that doesNotAccessMemory is handled as a Scalar,
|
|
|
|
// onlyReadsMemory will be handled as a Load instruction,
|
|
|
|
// all other calls will be handled as stores.
|
|
|
|
unsigned V = VN.lookupOrAdd(Call);
|
2016-07-18 14:11:37 +08:00
|
|
|
auto Entry = std::make_pair(V, InvalidVN);
|
2016-07-15 21:45:20 +08:00
|
|
|
|
|
|
|
if (Call->doesNotAccessMemory())
|
2016-07-18 14:11:37 +08:00
|
|
|
VNtoCallsScalars[Entry].push_back(Call);
|
2016-07-15 21:45:20 +08:00
|
|
|
else if (Call->onlyReadsMemory())
|
2016-07-18 14:11:37 +08:00
|
|
|
VNtoCallsLoads[Entry].push_back(Call);
|
2016-07-15 21:45:20 +08:00
|
|
|
else
|
2016-07-18 14:11:37 +08:00
|
|
|
VNtoCallsStores[Entry].push_back(Call);
|
2016-07-15 21:45:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const VNtoInsns &getScalarVNTable() const { return VNtoCallsScalars; }
|
|
|
|
const VNtoInsns &getLoadVNTable() const { return VNtoCallsLoads; }
|
|
|
|
const VNtoInsns &getStoreVNTable() const { return VNtoCallsStores; }
|
|
|
|
};
|
|
|
|
|
2016-07-25 10:21:25 +08:00
|
|
|
static void combineKnownMetadata(Instruction *ReplInst, Instruction *I) {
|
|
|
|
static const unsigned KnownIDs[] = {
|
|
|
|
LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
|
|
|
|
LLVMContext::MD_noalias, LLVMContext::MD_range,
|
|
|
|
LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load,
|
|
|
|
LLVMContext::MD_invariant_group};
|
|
|
|
combineMetadata(ReplInst, I, KnownIDs);
|
|
|
|
}
|
|
|
|
|
2016-07-15 21:45:20 +08:00
|
|
|
// This pass hoists common computations across branches sharing common
|
|
|
|
// dominator. The primary goal is to reduce the code size, and in some
|
|
|
|
// cases reduce critical path (by exposing more ILP).
|
|
|
|
class GVNHoist {
|
|
|
|
public:
|
2017-09-13 13:28:03 +08:00
|
|
|
GVNHoist(DominatorTree *DT, PostDominatorTree *PDT, AliasAnalysis *AA,
|
|
|
|
MemoryDependenceResults *MD, MemorySSA *MSSA)
|
|
|
|
: DT(DT), PDT(PDT), AA(AA), MD(MD), MSSA(MSSA),
|
2017-10-14 05:17:07 +08:00
|
|
|
MSSAUpdater(llvm::make_unique<MemorySSAUpdater>(MSSA)) {}
|
2016-11-29 22:34:01 +08:00
|
|
|
|
2016-07-26 01:24:22 +08:00
|
|
|
bool run(Function &F) {
|
2017-09-13 13:28:03 +08:00
|
|
|
NumFuncArgs = F.arg_size();
|
2016-07-26 01:24:22 +08:00
|
|
|
VN.setDomTree(DT);
|
|
|
|
VN.setAliasAnalysis(AA);
|
|
|
|
VN.setMemDep(MD);
|
|
|
|
bool Res = false;
|
2016-08-04 04:54:36 +08:00
|
|
|
// Perform DFS Numbering of instructions.
|
|
|
|
unsigned BBI = 0;
|
|
|
|
for (const BasicBlock *BB : depth_first(&F.getEntryBlock())) {
|
|
|
|
DFSNumber[BB] = ++BBI;
|
|
|
|
unsigned I = 0;
|
2016-10-13 09:39:10 +08:00
|
|
|
for (auto &Inst : *BB)
|
2016-08-04 04:54:36 +08:00
|
|
|
DFSNumber[&Inst] = ++I;
|
|
|
|
}
|
2016-07-26 01:24:22 +08:00
|
|
|
|
2016-08-04 04:54:38 +08:00
|
|
|
int ChainLength = 0;
|
|
|
|
|
2016-07-26 01:24:22 +08:00
|
|
|
// FIXME: use lazy evaluation of VN to avoid the fix-point computation.
|
2017-10-14 05:17:07 +08:00
|
|
|
while (true) {
|
2016-08-04 04:54:38 +08:00
|
|
|
if (MaxChainLength != -1 && ++ChainLength >= MaxChainLength)
|
|
|
|
return Res;
|
|
|
|
|
2016-07-26 01:24:22 +08:00
|
|
|
auto HoistStat = hoistExpressions(F);
|
2016-08-04 04:54:33 +08:00
|
|
|
if (HoistStat.first + HoistStat.second == 0)
|
2016-07-26 01:24:22 +08:00
|
|
|
return Res;
|
2016-08-04 04:54:33 +08:00
|
|
|
|
|
|
|
if (HoistStat.second > 0)
|
2016-07-26 01:24:22 +08:00
|
|
|
// To address a limitation of the current GVN, we need to rerun the
|
2016-08-04 04:54:33 +08:00
|
|
|
// hoisting after we hoisted loads or stores in order to be able to
|
|
|
|
// hoist all scalars dependent on the hoisted ld/st.
|
2016-07-26 01:24:22 +08:00
|
|
|
VN.clear();
|
2016-08-04 04:54:33 +08:00
|
|
|
|
2016-07-26 01:24:22 +08:00
|
|
|
Res = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
2016-10-13 09:39:10 +08:00
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
// Copied from NewGVN.cpp
|
|
|
|
// This function provides global ranking of operations so that we can place
|
|
|
|
// them in a canonical order. Note that rank alone is not necessarily enough
|
|
|
|
// for a complete ordering, as constants all have the same rank. However,
|
|
|
|
// generally, we will simplify an operation with all constants so that it
|
|
|
|
// doesn't matter what order they appear in.
|
|
|
|
unsigned int rank(const Value *V) const {
|
|
|
|
// Prefer constants to undef to anything else
|
|
|
|
// Undef is a constant, have to check it first.
|
|
|
|
// Prefer smaller constants to constantexprs
|
|
|
|
if (isa<ConstantExpr>(V))
|
|
|
|
return 2;
|
|
|
|
if (isa<UndefValue>(V))
|
|
|
|
return 1;
|
|
|
|
if (isa<Constant>(V))
|
|
|
|
return 0;
|
|
|
|
else if (auto *A = dyn_cast<Argument>(V))
|
|
|
|
return 3 + A->getArgNo();
|
|
|
|
|
|
|
|
// Need to shift the instruction DFS by number of arguments + 3 to account
|
|
|
|
// for the constant and argument ranking above.
|
|
|
|
auto Result = DFSNumber.lookup(V);
|
|
|
|
if (Result > 0)
|
|
|
|
return 4 + NumFuncArgs + Result;
|
|
|
|
// Unreachable or something else, just return a really large number.
|
|
|
|
return ~0;
|
|
|
|
}
|
|
|
|
|
2016-07-26 01:24:22 +08:00
|
|
|
private:
|
2016-07-15 21:45:20 +08:00
|
|
|
GVN::ValueTable VN;
|
|
|
|
DominatorTree *DT;
|
2017-09-13 13:28:03 +08:00
|
|
|
PostDominatorTree *PDT;
|
2016-07-15 21:45:20 +08:00
|
|
|
AliasAnalysis *AA;
|
|
|
|
MemoryDependenceResults *MD;
|
2016-08-23 13:42:41 +08:00
|
|
|
MemorySSA *MSSA;
|
2017-02-23 06:19:55 +08:00
|
|
|
std::unique_ptr<MemorySSAUpdater> MSSAUpdater;
|
2016-07-26 08:15:10 +08:00
|
|
|
DenseMap<const Value *, unsigned> DFSNumber;
|
2016-07-15 21:45:20 +08:00
|
|
|
BBSideEffectsSet BBSideEffects;
|
2017-09-13 13:28:03 +08:00
|
|
|
DenseSet<const BasicBlock *> HoistBarrier;
|
|
|
|
SmallVector<BasicBlock *, 32> IDFBlocks;
|
|
|
|
unsigned NumFuncArgs;
|
2017-10-14 05:17:07 +08:00
|
|
|
const bool HoistingGeps = false;
|
2016-07-18 08:35:01 +08:00
|
|
|
|
2016-07-15 21:45:20 +08:00
|
|
|
enum InsKind { Unknown, Scalar, Load, Store };
|
|
|
|
|
|
|
|
// Return true when there are exception handling in BB.
|
|
|
|
bool hasEH(const BasicBlock *BB) {
|
|
|
|
auto It = BBSideEffects.find(BB);
|
|
|
|
if (It != BBSideEffects.end())
|
|
|
|
return It->second;
|
|
|
|
|
|
|
|
if (BB->isEHPad() || BB->hasAddressTaken()) {
|
|
|
|
BBSideEffects[BB] = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BB->getTerminator()->mayThrow()) {
|
|
|
|
BBSideEffects[BB] = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BBSideEffects[BB] = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-25 19:55:47 +08:00
|
|
|
// Return true when a successor of BB dominates A.
|
|
|
|
bool successorDominate(const BasicBlock *BB, const BasicBlock *A) {
|
|
|
|
for (const BasicBlock *Succ : BB->getTerminator()->successors())
|
|
|
|
if (DT->dominates(Succ, A))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-14 05:17:07 +08:00
|
|
|
// Return true when I1 appears before I2 in the instructions of BB.
|
2016-07-26 08:15:10 +08:00
|
|
|
bool firstInBB(const Instruction *I1, const Instruction *I2) {
|
2016-10-13 09:39:10 +08:00
|
|
|
assert(I1->getParent() == I2->getParent());
|
2016-07-26 08:15:10 +08:00
|
|
|
unsigned I1DFS = DFSNumber.lookup(I1);
|
|
|
|
unsigned I2DFS = DFSNumber.lookup(I2);
|
2016-10-13 09:39:10 +08:00
|
|
|
assert(I1DFS && I2DFS);
|
2016-07-26 08:15:10 +08:00
|
|
|
return I1DFS < I2DFS;
|
2016-07-26 02:19:49 +08:00
|
|
|
}
|
2016-07-26 08:15:10 +08:00
|
|
|
|
2016-10-13 09:39:10 +08:00
|
|
|
// Return true when there are memory uses of Def in BB.
|
|
|
|
bool hasMemoryUse(const Instruction *NewPt, MemoryDef *Def,
|
|
|
|
const BasicBlock *BB) {
|
|
|
|
const MemorySSA::AccessList *Acc = MSSA->getBlockAccesses(BB);
|
|
|
|
if (!Acc)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Instruction *OldPt = Def->getMemoryInst();
|
2016-10-12 10:23:39 +08:00
|
|
|
const BasicBlock *OldBB = OldPt->getParent();
|
2016-10-13 09:39:10 +08:00
|
|
|
const BasicBlock *NewBB = NewPt->getParent();
|
|
|
|
bool ReachedNewPt = false;
|
2016-10-12 10:23:39 +08:00
|
|
|
|
2016-10-13 09:39:10 +08:00
|
|
|
for (const MemoryAccess &MA : *Acc)
|
|
|
|
if (const MemoryUse *MU = dyn_cast<MemoryUse>(&MA)) {
|
|
|
|
Instruction *Insn = MU->getMemoryInst();
|
2016-10-12 10:23:39 +08:00
|
|
|
|
2016-10-13 09:39:10 +08:00
|
|
|
// Do not check whether MU aliases Def when MU occurs after OldPt.
|
|
|
|
if (BB == OldBB && firstInBB(OldPt, Insn))
|
|
|
|
break;
|
2016-10-13 08:18:26 +08:00
|
|
|
|
2016-10-13 09:39:10 +08:00
|
|
|
// Do not check whether MU aliases Def when MU occurs before NewPt.
|
|
|
|
if (BB == NewBB) {
|
|
|
|
if (!ReachedNewPt) {
|
|
|
|
if (firstInBB(Insn, NewPt))
|
|
|
|
continue;
|
|
|
|
ReachedNewPt = true;
|
|
|
|
}
|
|
|
|
}
|
2017-03-03 07:06:46 +08:00
|
|
|
if (MemorySSAUtil::defClobbersUseOrDef(Def, MU, *AA))
|
2016-09-23 05:20:53 +08:00
|
|
|
return true;
|
2016-07-15 21:45:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-06 04:49:41 +08:00
|
|
|
bool hasEHhelper(const BasicBlock *BB, const BasicBlock *SrcBB,
|
|
|
|
int &NBBsOnAllPaths) {
|
|
|
|
// Stop walk once the limit is reached.
|
|
|
|
if (NBBsOnAllPaths == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Impossible to hoist with exceptions on the path.
|
|
|
|
if (hasEH(BB))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// No such instruction after HoistBarrier in a basic block was
|
|
|
|
// selected for hoisting so instructions selected within basic block with
|
|
|
|
// a hoist barrier can be hoisted.
|
|
|
|
if ((BB != SrcBB) && HoistBarrier.count(BB))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-15 21:45:20 +08:00
|
|
|
// Return true when there are exception handling or loads of memory Def
|
2016-10-13 09:39:10 +08:00
|
|
|
// between Def and NewPt. This function is only called for stores: Def is
|
|
|
|
// the MemoryDef of the store to be hoisted.
|
2016-07-15 21:45:20 +08:00
|
|
|
|
|
|
|
// Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
|
|
|
|
// return true when the counter NBBsOnAllPaths reaces 0, except when it is
|
|
|
|
// initialized to -1 which is unlimited.
|
2016-10-13 09:39:10 +08:00
|
|
|
bool hasEHOrLoadsOnPath(const Instruction *NewPt, MemoryDef *Def,
|
|
|
|
int &NBBsOnAllPaths) {
|
2016-07-15 21:45:20 +08:00
|
|
|
const BasicBlock *NewBB = NewPt->getParent();
|
2016-10-13 09:39:10 +08:00
|
|
|
const BasicBlock *OldBB = Def->getBlock();
|
2016-07-15 21:45:20 +08:00
|
|
|
assert(DT->dominates(NewBB, OldBB) && "invalid path");
|
2016-10-13 09:39:10 +08:00
|
|
|
assert(DT->dominates(Def->getDefiningAccess()->getBlock(), NewBB) &&
|
2016-07-15 21:45:20 +08:00
|
|
|
"def does not dominate new hoisting point");
|
|
|
|
|
|
|
|
// Walk all basic blocks reachable in depth-first iteration on the inverse
|
|
|
|
// CFG from OldBB to NewBB. These blocks are all the blocks that may be
|
|
|
|
// executed between the execution of NewBB and OldBB. Hoisting an expression
|
|
|
|
// from OldBB into NewBB has to be safe on all execution paths.
|
|
|
|
for (auto I = idf_begin(OldBB), E = idf_end(OldBB); I != E;) {
|
2017-04-11 04:45:17 +08:00
|
|
|
const BasicBlock *BB = *I;
|
|
|
|
if (BB == NewBB) {
|
2016-07-15 21:45:20 +08:00
|
|
|
// Stop traversal when reaching HoistPt.
|
|
|
|
I.skipChildren();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-06 04:49:41 +08:00
|
|
|
if (hasEHhelper(BB, OldBB, NBBsOnAllPaths))
|
2016-07-15 21:45:20 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check that we do not move a store past loads.
|
2017-04-11 04:45:17 +08:00
|
|
|
if (hasMemoryUse(NewPt, Def, BB))
|
2016-07-15 21:45:20 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// -1 is unlimited number of blocks on all paths.
|
|
|
|
if (NBBsOnAllPaths != -1)
|
|
|
|
--NBBsOnAllPaths;
|
|
|
|
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true when there are exception handling between HoistPt and BB.
|
|
|
|
// Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
|
|
|
|
// return true when the counter NBBsOnAllPaths reaches 0, except when it is
|
|
|
|
// initialized to -1 which is unlimited.
|
2017-04-11 04:45:17 +08:00
|
|
|
bool hasEHOnPath(const BasicBlock *HoistPt, const BasicBlock *SrcBB,
|
2016-07-15 21:45:20 +08:00
|
|
|
int &NBBsOnAllPaths) {
|
2017-04-11 04:45:17 +08:00
|
|
|
assert(DT->dominates(HoistPt, SrcBB) && "Invalid path");
|
2016-07-15 21:45:20 +08:00
|
|
|
|
|
|
|
// Walk all basic blocks reachable in depth-first iteration on
|
|
|
|
// the inverse CFG from BBInsn to NewHoistPt. These blocks are all the
|
|
|
|
// blocks that may be executed between the execution of NewHoistPt and
|
|
|
|
// BBInsn. Hoisting an expression from BBInsn into NewHoistPt has to be safe
|
|
|
|
// on all execution paths.
|
2017-04-11 04:45:17 +08:00
|
|
|
for (auto I = idf_begin(SrcBB), E = idf_end(SrcBB); I != E;) {
|
|
|
|
const BasicBlock *BB = *I;
|
|
|
|
if (BB == HoistPt) {
|
2016-07-15 21:45:20 +08:00
|
|
|
// Stop traversal when reaching NewHoistPt.
|
|
|
|
I.skipChildren();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-06 04:49:41 +08:00
|
|
|
if (hasEHhelper(BB, SrcBB, NBBsOnAllPaths))
|
2016-11-29 22:36:27 +08:00
|
|
|
return true;
|
|
|
|
|
2016-07-15 21:45:20 +08:00
|
|
|
// -1 is unlimited number of blocks on all paths.
|
|
|
|
if (NBBsOnAllPaths != -1)
|
|
|
|
--NBBsOnAllPaths;
|
|
|
|
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true when it is safe to hoist a memory load or store U from OldPt
|
|
|
|
// to NewPt.
|
|
|
|
bool safeToHoistLdSt(const Instruction *NewPt, const Instruction *OldPt,
|
|
|
|
MemoryUseOrDef *U, InsKind K, int &NBBsOnAllPaths) {
|
|
|
|
// In place hoisting is safe.
|
|
|
|
if (NewPt == OldPt)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const BasicBlock *NewBB = NewPt->getParent();
|
|
|
|
const BasicBlock *OldBB = OldPt->getParent();
|
|
|
|
const BasicBlock *UBB = U->getBlock();
|
|
|
|
|
|
|
|
// Check for dependences on the Memory SSA.
|
|
|
|
MemoryAccess *D = U->getDefiningAccess();
|
|
|
|
BasicBlock *DBB = D->getBlock();
|
|
|
|
if (DT->properlyDominates(NewBB, DBB))
|
|
|
|
// Cannot move the load or store to NewBB above its definition in DBB.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (NewBB == DBB && !MSSA->isLiveOnEntryDef(D))
|
2016-07-18 08:34:58 +08:00
|
|
|
if (auto *UD = dyn_cast<MemoryUseOrDef>(D))
|
2016-07-26 08:15:10 +08:00
|
|
|
if (firstInBB(NewPt, UD->getMemoryInst()))
|
2016-07-15 21:45:20 +08:00
|
|
|
// Cannot move the load or store to NewPt above its definition in D.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check for unsafe hoistings due to side effects.
|
|
|
|
if (K == InsKind::Store) {
|
2016-10-13 09:39:10 +08:00
|
|
|
if (hasEHOrLoadsOnPath(NewPt, dyn_cast<MemoryDef>(U), NBBsOnAllPaths))
|
2016-07-15 21:45:20 +08:00
|
|
|
return false;
|
|
|
|
} else if (hasEHOnPath(NewBB, OldBB, NBBsOnAllPaths))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (UBB == NewBB) {
|
|
|
|
if (DT->properlyDominates(DBB, NewBB))
|
|
|
|
return true;
|
|
|
|
assert(UBB == DBB);
|
|
|
|
assert(MSSA->locallyDominates(D, U));
|
|
|
|
}
|
|
|
|
|
|
|
|
// No side effects: it is safe to hoist.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-25 19:55:47 +08:00
|
|
|
// Return true when it is safe to hoist scalar instructions from all blocks in
|
|
|
|
// WL to HoistBB.
|
2017-09-13 13:28:03 +08:00
|
|
|
bool safeToHoistScalar(const BasicBlock *HoistBB, const BasicBlock *BB,
|
2016-08-25 19:55:47 +08:00
|
|
|
int &NBBsOnAllPaths) {
|
2017-09-13 13:28:03 +08:00
|
|
|
return !hasEHOnPath(HoistBB, BB, NBBsOnAllPaths);
|
|
|
|
}
|
2016-07-15 21:45:20 +08:00
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
// In the inverse CFG, the dominance frontier of basic block (BB) is the
|
|
|
|
// point where ANTIC needs to be computed for instructions which are going
|
|
|
|
// to be hoisted. Since this point does not change during gvn-hoist,
|
|
|
|
// we compute it only once (on demand).
|
|
|
|
// The ides is inspired from:
|
|
|
|
// "Partial Redundancy Elimination in SSA Form"
|
|
|
|
// ROBERT KENNEDY, SUN CHAN, SHIN-MING LIU, RAYMOND LO, PENG TU and FRED CHOW
|
2018-01-29 13:17:03 +08:00
|
|
|
// They use similar idea in the forward graph to find fully redundant and
|
2017-09-13 13:28:03 +08:00
|
|
|
// partially redundant expressions, here it is used in the inverse graph to
|
|
|
|
// find fully anticipable instructions at merge point (post-dominator in
|
|
|
|
// the inverse CFG).
|
|
|
|
// Returns the edge via which an instruction in BB will get the values from.
|
|
|
|
|
|
|
|
// Returns true when the values are flowing out to each edge.
|
|
|
|
bool valueAnticipable(CHIArgs C, TerminatorInst *TI) const {
|
2018-05-17 07:20:42 +08:00
|
|
|
if (TI->getNumSuccessors() > (unsigned)size(C))
|
2017-09-13 13:28:03 +08:00
|
|
|
return false; // Not enough args in this CHI.
|
|
|
|
|
|
|
|
for (auto CHI : C) {
|
|
|
|
BasicBlock *Dest = CHI.Dest;
|
|
|
|
// Find if all the edges have values flowing out of BB.
|
2017-10-14 05:17:07 +08:00
|
|
|
bool Found = llvm::any_of(TI->successors(), [Dest](const BasicBlock *BB) {
|
2017-09-13 13:28:03 +08:00
|
|
|
return BB == Dest; });
|
|
|
|
if (!Found)
|
2016-08-25 19:55:47 +08:00
|
|
|
return false;
|
2017-09-13 13:28:03 +08:00
|
|
|
}
|
2016-07-15 21:45:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
// Check if it is safe to hoist values tracked by CHI in the range
|
|
|
|
// [Begin, End) and accumulate them in Safe.
|
|
|
|
void checkSafety(CHIArgs C, BasicBlock *BB, InsKind K,
|
|
|
|
SmallVectorImpl<CHIArg> &Safe) {
|
2016-11-29 22:36:27 +08:00
|
|
|
int NumBBsOnAllPaths = MaxNumberOfBBSInPath;
|
2017-09-13 13:28:03 +08:00
|
|
|
for (auto CHI : C) {
|
|
|
|
Instruction *Insn = CHI.I;
|
|
|
|
if (!Insn) // No instruction was inserted in this CHI.
|
|
|
|
continue;
|
|
|
|
if (K == InsKind::Scalar) {
|
|
|
|
if (safeToHoistScalar(BB, Insn->getParent(), NumBBsOnAllPaths))
|
|
|
|
Safe.push_back(CHI);
|
2016-07-15 21:45:20 +08:00
|
|
|
} else {
|
2017-09-13 13:28:03 +08:00
|
|
|
MemoryUseOrDef *UD = MSSA->getMemoryAccess(Insn);
|
|
|
|
if (safeToHoistLdSt(BB->getTerminator(), Insn, UD, K, NumBBsOnAllPaths))
|
|
|
|
Safe.push_back(CHI);
|
2016-07-15 21:45:20 +08:00
|
|
|
}
|
2017-09-13 13:28:03 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-15 21:45:20 +08:00
|
|
|
|
2017-10-14 05:17:07 +08:00
|
|
|
using RenameStackType = DenseMap<VNType, SmallVector<Instruction *, 2>>;
|
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
// Push all the VNs corresponding to BB into RenameStack.
|
|
|
|
void fillRenameStack(BasicBlock *BB, InValuesType &ValueBBs,
|
|
|
|
RenameStackType &RenameStack) {
|
|
|
|
auto it1 = ValueBBs.find(BB);
|
|
|
|
if (it1 != ValueBBs.end()) {
|
|
|
|
// Iterate in reverse order to keep lower ranked values on the top.
|
|
|
|
for (std::pair<VNType, Instruction *> &VI : reverse(it1->second)) {
|
|
|
|
// Get the value of instruction I
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\nPushing on stack: " << *VI.second);
|
2017-09-13 13:28:03 +08:00
|
|
|
RenameStack[VI.first].push_back(VI.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-25 19:55:47 +08:00
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
void fillChiArgs(BasicBlock *BB, OutValuesType &CHIBBs,
|
|
|
|
RenameStackType &RenameStack) {
|
|
|
|
// For each *predecessor* (because Post-DOM) of BB check if it has a CHI
|
|
|
|
for (auto Pred : predecessors(BB)) {
|
|
|
|
auto P = CHIBBs.find(Pred);
|
|
|
|
if (P == CHIBBs.end()) {
|
|
|
|
continue;
|
2016-07-15 21:45:20 +08:00
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\nLooking at CHIs in: " << Pred->getName(););
|
2017-09-13 13:28:03 +08:00
|
|
|
// A CHI is found (BB -> Pred is an edge in the CFG)
|
|
|
|
// Pop the stack until Top(V) = Ve.
|
|
|
|
auto &VCHI = P->second;
|
|
|
|
for (auto It = VCHI.begin(), E = VCHI.end(); It != E;) {
|
|
|
|
CHIArg &C = *It;
|
|
|
|
if (!C.Dest) {
|
|
|
|
auto si = RenameStack.find(C.VN);
|
|
|
|
// The Basic Block where CHI is must dominate the value we want to
|
|
|
|
// track in a CHI. In the PDom walk, there can be values in the
|
|
|
|
// stack which are not control dependent e.g., nested loop.
|
|
|
|
if (si != RenameStack.end() && si->second.size() &&
|
2018-01-04 15:47:24 +08:00
|
|
|
DT->properlyDominates(Pred, si->second.back()->getParent())) {
|
2017-09-13 13:28:03 +08:00
|
|
|
C.Dest = BB; // Assign the edge
|
|
|
|
C.I = si->second.pop_back_val(); // Assign the argument
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs()
|
|
|
|
<< "\nCHI Inserted in BB: " << C.Dest->getName() << *C.I
|
|
|
|
<< ", VN: " << C.VN.first << ", " << C.VN.second);
|
2017-09-13 13:28:03 +08:00
|
|
|
}
|
|
|
|
// Move to next CHI of a different value
|
|
|
|
It = std::find_if(It, VCHI.end(),
|
|
|
|
[It](CHIArg &A) { return A != *It; });
|
|
|
|
} else
|
|
|
|
++It;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 21:45:20 +08:00
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
// Walk the post-dominator tree top-down and use a stack for each value to
|
|
|
|
// store the last value you see. When you hit a CHI from a given edge, the
|
|
|
|
// value to use as the argument is at the top of the stack, add the value to
|
|
|
|
// CHI and pop.
|
|
|
|
void insertCHI(InValuesType &ValueBBs, OutValuesType &CHIBBs) {
|
|
|
|
auto Root = PDT->getNode(nullptr);
|
|
|
|
if (!Root)
|
|
|
|
return;
|
|
|
|
// Depth first walk on PDom tree to fill the CHIargs at each PDF.
|
|
|
|
RenameStackType RenameStack;
|
|
|
|
for (auto Node : depth_first(Root)) {
|
|
|
|
BasicBlock *BB = Node->getBlock();
|
|
|
|
if (!BB)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Collect all values in BB and push to stack.
|
|
|
|
fillRenameStack(BB, ValueBBs, RenameStack);
|
|
|
|
|
|
|
|
// Fill outgoing values in each CHI corresponding to BB.
|
|
|
|
fillChiArgs(BB, CHIBBs, RenameStack);
|
2016-07-15 21:45:20 +08:00
|
|
|
}
|
2017-09-13 13:28:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Walk all the CHI-nodes to find ones which have a empty-entry and remove
|
|
|
|
// them Then collect all the instructions which are safe to hoist and see if
|
|
|
|
// they form a list of anticipable values. OutValues contains CHIs
|
|
|
|
// corresponding to each basic block.
|
|
|
|
void findHoistableCandidates(OutValuesType &CHIBBs, InsKind K,
|
|
|
|
HoistingPointList &HPL) {
|
|
|
|
auto cmpVN = [](const CHIArg &A, const CHIArg &B) { return A.VN < B.VN; };
|
|
|
|
|
|
|
|
// CHIArgs now have the outgoing values, so check for anticipability and
|
|
|
|
// accumulate hoistable candidates in HPL.
|
|
|
|
for (std::pair<BasicBlock *, SmallVector<CHIArg, 2>> &A : CHIBBs) {
|
|
|
|
BasicBlock *BB = A.first;
|
|
|
|
SmallVectorImpl<CHIArg> &CHIs = A.second;
|
|
|
|
// Vector of PHIs contains PHIs for different instructions.
|
|
|
|
// Sort the args according to their VNs, such that identical
|
|
|
|
// instructions are together.
|
2017-10-31 03:42:41 +08:00
|
|
|
std::stable_sort(CHIs.begin(), CHIs.end(), cmpVN);
|
2017-09-13 13:28:03 +08:00
|
|
|
auto TI = BB->getTerminator();
|
|
|
|
auto B = CHIs.begin();
|
|
|
|
// [PreIt, PHIIt) form a range of CHIs which have identical VNs.
|
|
|
|
auto PHIIt = std::find_if(CHIs.begin(), CHIs.end(),
|
|
|
|
[B](CHIArg &A) { return A != *B; });
|
|
|
|
auto PrevIt = CHIs.begin();
|
|
|
|
while (PrevIt != PHIIt) {
|
|
|
|
// Collect values which satisfy safety checks.
|
|
|
|
SmallVector<CHIArg, 2> Safe;
|
|
|
|
// We check for safety first because there might be multiple values in
|
|
|
|
// the same path, some of which are not safe to be hoisted, but overall
|
|
|
|
// each edge has at least one value which can be hoisted, making the
|
|
|
|
// value anticipable along that path.
|
|
|
|
checkSafety(make_range(PrevIt, PHIIt), BB, K, Safe);
|
|
|
|
|
|
|
|
// List of safe values should be anticipable at TI.
|
|
|
|
if (valueAnticipable(make_range(Safe.begin(), Safe.end()), TI)) {
|
|
|
|
HPL.push_back({BB, SmallVecInsn()});
|
|
|
|
SmallVecInsn &V = HPL.back().second;
|
|
|
|
for (auto B : Safe)
|
|
|
|
V.push_back(B.I);
|
|
|
|
}
|
2016-07-15 21:45:20 +08:00
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
// Check other VNs
|
|
|
|
PrevIt = PHIIt;
|
|
|
|
PHIIt = std::find_if(PrevIt, CHIs.end(),
|
|
|
|
[PrevIt](CHIArg &A) { return A != *PrevIt; });
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 21:45:20 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
// Compute insertion points for each values which can be fully anticipated at
|
|
|
|
// a dominator. HPL contains all such values.
|
2016-07-15 21:45:20 +08:00
|
|
|
void computeInsertionPoints(const VNtoInsns &Map, HoistingPointList &HPL,
|
|
|
|
InsKind K) {
|
2017-09-13 13:28:03 +08:00
|
|
|
// Sort VNs based on their rankings
|
|
|
|
std::vector<VNType> Ranks;
|
2016-07-18 08:34:58 +08:00
|
|
|
for (const auto &Entry : Map) {
|
2017-09-13 13:28:03 +08:00
|
|
|
Ranks.push_back(Entry.first);
|
|
|
|
}
|
2016-07-15 21:45:20 +08:00
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
// TODO: Remove fully-redundant expressions.
|
|
|
|
// Get instruction from the Map, assume that all the Instructions
|
|
|
|
// with same VNs have same rank (this is an approximation).
|
2018-04-14 03:47:57 +08:00
|
|
|
llvm::sort(Ranks.begin(), Ranks.end(),
|
|
|
|
[this, &Map](const VNType &r1, const VNType &r2) {
|
|
|
|
return (rank(*Map.lookup(r1).begin()) <
|
|
|
|
rank(*Map.lookup(r2).begin()));
|
|
|
|
});
|
2017-09-13 13:28:03 +08:00
|
|
|
|
|
|
|
// - Sort VNs according to their rank, and start with lowest ranked VN
|
|
|
|
// - Take a VN and for each instruction with same VN
|
|
|
|
// - Find the dominance frontier in the inverse graph (PDF)
|
|
|
|
// - Insert the chi-node at PDF
|
|
|
|
// - Remove the chi-nodes with missing entries
|
|
|
|
// - Remove values from CHI-nodes which do not truly flow out, e.g.,
|
|
|
|
// modified along the path.
|
|
|
|
// - Collect the remaining values that are still anticipable
|
|
|
|
SmallVector<BasicBlock *, 2> IDFBlocks;
|
|
|
|
ReverseIDFCalculator IDFs(*PDT);
|
|
|
|
OutValuesType OutValue;
|
|
|
|
InValuesType InValue;
|
|
|
|
for (const auto &R : Ranks) {
|
|
|
|
const SmallVecInsn &V = Map.lookup(R);
|
2016-07-15 21:45:20 +08:00
|
|
|
if (V.size() < 2)
|
|
|
|
continue;
|
2017-09-13 13:28:03 +08:00
|
|
|
const VNType &VN = R;
|
|
|
|
SmallPtrSet<BasicBlock *, 2> VNBlocks;
|
|
|
|
for (auto &I : V) {
|
|
|
|
BasicBlock *BBI = I->getParent();
|
|
|
|
if (!hasEH(BBI))
|
|
|
|
VNBlocks.insert(BBI);
|
|
|
|
}
|
|
|
|
// Compute the Post Dominance Frontiers of each basic block
|
|
|
|
// The dominance frontier of a live block X in the reverse
|
|
|
|
// control graph is the set of blocks upon which X is control
|
|
|
|
// dependent. The following sequence computes the set of blocks
|
|
|
|
// which currently have dead terminators that are control
|
|
|
|
// dependence sources of a block which is in NewLiveBlocks.
|
|
|
|
IDFs.setDefiningBlocks(VNBlocks);
|
|
|
|
IDFs.calculate(IDFBlocks);
|
|
|
|
|
|
|
|
// Make a map of BB vs instructions to be hoisted.
|
|
|
|
for (unsigned i = 0; i < V.size(); ++i) {
|
|
|
|
InValue[V[i]->getParent()].push_back(std::make_pair(VN, V[i]));
|
|
|
|
}
|
|
|
|
// Insert empty CHI node for this VN. This is used to factor out
|
|
|
|
// basic blocks where the ANTIC can potentially change.
|
|
|
|
for (auto IDFB : IDFBlocks) { // TODO: Prune out useless CHI insertions.
|
|
|
|
for (unsigned i = 0; i < V.size(); ++i) {
|
|
|
|
CHIArg C = {VN, nullptr, nullptr};
|
2017-12-14 03:40:07 +08:00
|
|
|
// Ignore spurious PDFs.
|
|
|
|
if (DT->properlyDominates(IDFB, V[i]->getParent())) {
|
2017-09-13 13:28:03 +08:00
|
|
|
OutValue[IDFB].push_back(C);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\nInsertion a CHI for BB: " << IDFB->getName()
|
|
|
|
<< ", for Insn: " << *V[i]);
|
2017-09-13 13:28:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 21:45:20 +08:00
|
|
|
}
|
2017-09-13 13:28:03 +08:00
|
|
|
|
|
|
|
// Insert CHI args at each PDF to iterate on factored graph of
|
|
|
|
// control dependence.
|
|
|
|
insertCHI(InValue, OutValue);
|
|
|
|
// Using the CHI args inserted at each PDF, find fully anticipable values.
|
|
|
|
findHoistableCandidates(OutValue, K, HPL);
|
2016-07-15 21:45:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return true when all operands of Instr are available at insertion point
|
|
|
|
// HoistPt. When limiting the number of hoisted expressions, one could hoist
|
|
|
|
// a load without hoisting its access function. So before hoisting any
|
|
|
|
// expression, make sure that all its operands are available at insert point.
|
|
|
|
bool allOperandsAvailable(const Instruction *I,
|
|
|
|
const BasicBlock *HoistPt) const {
|
2016-07-18 08:34:58 +08:00
|
|
|
for (const Use &Op : I->operands())
|
|
|
|
if (const auto *Inst = dyn_cast<Instruction>(&Op))
|
|
|
|
if (!DT->dominates(Inst->getParent(), HoistPt))
|
|
|
|
return false;
|
2016-07-15 21:45:20 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-27 13:48:12 +08:00
|
|
|
// Same as allOperandsAvailable with recursive check for GEP operands.
|
|
|
|
bool allGepOperandsAvailable(const Instruction *I,
|
|
|
|
const BasicBlock *HoistPt) const {
|
|
|
|
for (const Use &Op : I->operands())
|
|
|
|
if (const auto *Inst = dyn_cast<Instruction>(&Op))
|
|
|
|
if (!DT->dominates(Inst->getParent(), HoistPt)) {
|
2016-10-13 09:39:10 +08:00
|
|
|
if (const GetElementPtrInst *GepOp =
|
|
|
|
dyn_cast<GetElementPtrInst>(Inst)) {
|
2016-07-27 13:48:12 +08:00
|
|
|
if (!allGepOperandsAvailable(GepOp, HoistPt))
|
|
|
|
return false;
|
|
|
|
// Gep is available if all operands of GepOp are available.
|
|
|
|
} else {
|
|
|
|
// Gep is not available if it has operands other than GEPs that are
|
|
|
|
// defined in blocks not dominating HoistPt.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make all operands of the GEP available.
|
|
|
|
void makeGepsAvailable(Instruction *Repl, BasicBlock *HoistPt,
|
|
|
|
const SmallVecInsn &InstructionsToHoist,
|
|
|
|
Instruction *Gep) const {
|
2016-10-13 09:39:10 +08:00
|
|
|
assert(allGepOperandsAvailable(Gep, HoistPt) &&
|
|
|
|
"GEP operands not available");
|
2016-07-27 13:48:12 +08:00
|
|
|
|
|
|
|
Instruction *ClonedGep = Gep->clone();
|
|
|
|
for (unsigned i = 0, e = Gep->getNumOperands(); i != e; ++i)
|
|
|
|
if (Instruction *Op = dyn_cast<Instruction>(Gep->getOperand(i))) {
|
|
|
|
// Check whether the operand is already available.
|
|
|
|
if (DT->dominates(Op->getParent(), HoistPt))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// As a GEP can refer to other GEPs, recursively make all the operands
|
|
|
|
// of this GEP available at HoistPt.
|
|
|
|
if (GetElementPtrInst *GepOp = dyn_cast<GetElementPtrInst>(Op))
|
|
|
|
makeGepsAvailable(ClonedGep, HoistPt, InstructionsToHoist, GepOp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy Gep and replace its uses in Repl with ClonedGep.
|
|
|
|
ClonedGep->insertBefore(HoistPt->getTerminator());
|
|
|
|
|
|
|
|
// Conservatively discard any optimization hints, they may differ on the
|
|
|
|
// other paths.
|
|
|
|
ClonedGep->dropUnknownNonDebugMetadata();
|
|
|
|
|
|
|
|
// If we have optimization hints which agree with each other along different
|
|
|
|
// paths, preserve them.
|
|
|
|
for (const Instruction *OtherInst : InstructionsToHoist) {
|
|
|
|
const GetElementPtrInst *OtherGep;
|
|
|
|
if (auto *OtherLd = dyn_cast<LoadInst>(OtherInst))
|
|
|
|
OtherGep = cast<GetElementPtrInst>(OtherLd->getPointerOperand());
|
|
|
|
else
|
|
|
|
OtherGep = cast<GetElementPtrInst>(
|
|
|
|
cast<StoreInst>(OtherInst)->getPointerOperand());
|
2016-09-08 07:39:04 +08:00
|
|
|
ClonedGep->andIRFlags(OtherGep);
|
2016-07-27 13:48:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace uses of Gep with ClonedGep in Repl.
|
|
|
|
Repl->replaceUsesOfWith(Gep, ClonedGep);
|
|
|
|
}
|
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
void updateAlignment(Instruction *I, Instruction *Repl) {
|
|
|
|
if (auto *ReplacementLoad = dyn_cast<LoadInst>(Repl)) {
|
|
|
|
ReplacementLoad->setAlignment(
|
|
|
|
std::min(ReplacementLoad->getAlignment(),
|
|
|
|
cast<LoadInst>(I)->getAlignment()));
|
|
|
|
++NumLoadsRemoved;
|
|
|
|
} else if (auto *ReplacementStore = dyn_cast<StoreInst>(Repl)) {
|
|
|
|
ReplacementStore->setAlignment(
|
|
|
|
std::min(ReplacementStore->getAlignment(),
|
|
|
|
cast<StoreInst>(I)->getAlignment()));
|
|
|
|
++NumStoresRemoved;
|
|
|
|
} else if (auto *ReplacementAlloca = dyn_cast<AllocaInst>(Repl)) {
|
|
|
|
ReplacementAlloca->setAlignment(
|
|
|
|
std::max(ReplacementAlloca->getAlignment(),
|
|
|
|
cast<AllocaInst>(I)->getAlignment()));
|
|
|
|
} else if (isa<CallInst>(Repl)) {
|
|
|
|
++NumCallsRemoved;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all the instructions in Candidates and replace their usage with Repl.
|
|
|
|
// Returns the number of instructions removed.
|
|
|
|
unsigned rauw(const SmallVecInsn &Candidates, Instruction *Repl,
|
|
|
|
MemoryUseOrDef *NewMemAcc) {
|
|
|
|
unsigned NR = 0;
|
|
|
|
for (Instruction *I : Candidates) {
|
|
|
|
if (I != Repl) {
|
|
|
|
++NR;
|
|
|
|
updateAlignment(I, Repl);
|
|
|
|
if (NewMemAcc) {
|
|
|
|
// Update the uses of the old MSSA access with NewMemAcc.
|
|
|
|
MemoryAccess *OldMA = MSSA->getMemoryAccess(I);
|
|
|
|
OldMA->replaceAllUsesWith(NewMemAcc);
|
|
|
|
MSSAUpdater->removeMemoryAccess(OldMA);
|
|
|
|
}
|
|
|
|
|
|
|
|
Repl->andIRFlags(I);
|
|
|
|
combineKnownMetadata(Repl, I);
|
|
|
|
I->replaceAllUsesWith(Repl);
|
|
|
|
// Also invalidate the Alias Analysis cache.
|
|
|
|
MD->removeInstruction(I);
|
|
|
|
I->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace all Memory PHI usage with NewMemAcc.
|
|
|
|
void raMPHIuw(MemoryUseOrDef *NewMemAcc) {
|
|
|
|
SmallPtrSet<MemoryPhi *, 4> UsePhis;
|
|
|
|
for (User *U : NewMemAcc->users())
|
|
|
|
if (MemoryPhi *Phi = dyn_cast<MemoryPhi>(U))
|
|
|
|
UsePhis.insert(Phi);
|
|
|
|
|
|
|
|
for (MemoryPhi *Phi : UsePhis) {
|
|
|
|
auto In = Phi->incoming_values();
|
2017-10-14 05:17:07 +08:00
|
|
|
if (llvm::all_of(In, [&](Use &U) { return U == NewMemAcc; })) {
|
2017-09-13 13:28:03 +08:00
|
|
|
Phi->replaceAllUsesWith(NewMemAcc);
|
|
|
|
MSSAUpdater->removeMemoryAccess(Phi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all other instructions and replace them with Repl.
|
|
|
|
unsigned removeAndReplace(const SmallVecInsn &Candidates, Instruction *Repl,
|
|
|
|
BasicBlock *DestBB, bool MoveAccess) {
|
|
|
|
MemoryUseOrDef *NewMemAcc = MSSA->getMemoryAccess(Repl);
|
|
|
|
if (MoveAccess && NewMemAcc) {
|
|
|
|
// The definition of this ld/st will not change: ld/st hoisting is
|
|
|
|
// legal when the ld/st is not moved past its current definition.
|
|
|
|
MSSAUpdater->moveToPlace(NewMemAcc, DestBB, MemorySSA::End);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace all other instructions with Repl with memory access NewMemAcc.
|
|
|
|
unsigned NR = rauw(Candidates, Repl, NewMemAcc);
|
|
|
|
|
|
|
|
// Remove MemorySSA phi nodes with the same arguments.
|
|
|
|
if (NewMemAcc)
|
|
|
|
raMPHIuw(NewMemAcc);
|
|
|
|
return NR;
|
|
|
|
}
|
|
|
|
|
2016-07-27 13:48:12 +08:00
|
|
|
// In the case Repl is a load or a store, we make all their GEPs
|
|
|
|
// available: GEPs are not hoisted by default to avoid the address
|
|
|
|
// computations to be hoisted without the associated load or store.
|
|
|
|
bool makeGepOperandsAvailable(Instruction *Repl, BasicBlock *HoistPt,
|
|
|
|
const SmallVecInsn &InstructionsToHoist) const {
|
2016-07-15 21:45:20 +08:00
|
|
|
// Check whether the GEP of a ld/st can be synthesized at HoistPt.
|
2016-07-21 05:05:01 +08:00
|
|
|
GetElementPtrInst *Gep = nullptr;
|
2016-07-15 21:45:20 +08:00
|
|
|
Instruction *Val = nullptr;
|
2016-07-27 13:48:12 +08:00
|
|
|
if (auto *Ld = dyn_cast<LoadInst>(Repl)) {
|
2016-07-21 05:05:01 +08:00
|
|
|
Gep = dyn_cast<GetElementPtrInst>(Ld->getPointerOperand());
|
2016-07-27 13:48:12 +08:00
|
|
|
} else if (auto *St = dyn_cast<StoreInst>(Repl)) {
|
2016-07-21 05:05:01 +08:00
|
|
|
Gep = dyn_cast<GetElementPtrInst>(St->getPointerOperand());
|
2016-07-15 21:45:20 +08:00
|
|
|
Val = dyn_cast<Instruction>(St->getValueOperand());
|
2016-07-22 07:22:10 +08:00
|
|
|
// Check that the stored value is available.
|
2016-07-22 08:07:01 +08:00
|
|
|
if (Val) {
|
|
|
|
if (isa<GetElementPtrInst>(Val)) {
|
|
|
|
// Check whether we can compute the GEP at HoistPt.
|
2016-07-27 13:48:12 +08:00
|
|
|
if (!allGepOperandsAvailable(Val, HoistPt))
|
2016-07-22 08:07:01 +08:00
|
|
|
return false;
|
|
|
|
} else if (!DT->dominates(Val->getParent(), HoistPt))
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-15 21:45:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether we can compute the Gep at HoistPt.
|
2016-07-27 13:48:12 +08:00
|
|
|
if (!Gep || !allGepOperandsAvailable(Gep, HoistPt))
|
2016-07-15 21:45:20 +08:00
|
|
|
return false;
|
|
|
|
|
2016-07-27 13:48:12 +08:00
|
|
|
makeGepsAvailable(Repl, HoistPt, InstructionsToHoist, Gep);
|
2016-07-15 21:45:20 +08:00
|
|
|
|
2016-07-27 13:48:12 +08:00
|
|
|
if (Val && isa<GetElementPtrInst>(Val))
|
|
|
|
makeGepsAvailable(Repl, HoistPt, InstructionsToHoist, Val);
|
2016-07-15 21:45:20 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<unsigned, unsigned> hoist(HoistingPointList &HPL) {
|
|
|
|
unsigned NI = 0, NL = 0, NS = 0, NC = 0, NR = 0;
|
|
|
|
for (const HoistingPointInfo &HP : HPL) {
|
|
|
|
// Find out whether we already have one of the instructions in HoistPt,
|
|
|
|
// in which case we do not have to move it.
|
2017-09-13 13:28:03 +08:00
|
|
|
BasicBlock *DestBB = HP.first;
|
2016-07-15 21:45:20 +08:00
|
|
|
const SmallVecInsn &InstructionsToHoist = HP.second;
|
|
|
|
Instruction *Repl = nullptr;
|
|
|
|
for (Instruction *I : InstructionsToHoist)
|
2017-09-13 13:28:03 +08:00
|
|
|
if (I->getParent() == DestBB)
|
2016-07-15 21:45:20 +08:00
|
|
|
// If there are two instructions in HoistPt to be hoisted in place:
|
|
|
|
// update Repl to be the first one, such that we can rename the uses
|
|
|
|
// of the second based on the first.
|
2016-07-27 13:13:52 +08:00
|
|
|
if (!Repl || firstInBB(I, Repl))
|
|
|
|
Repl = I;
|
2016-07-15 21:45:20 +08:00
|
|
|
|
2016-08-12 04:32:43 +08:00
|
|
|
// Keep track of whether we moved the instruction so we know whether we
|
|
|
|
// should move the MemoryAccess.
|
|
|
|
bool MoveAccess = true;
|
2016-07-15 21:45:20 +08:00
|
|
|
if (Repl) {
|
|
|
|
// Repl is already in HoistPt: it remains in place.
|
2017-09-13 13:28:03 +08:00
|
|
|
assert(allOperandsAvailable(Repl, DestBB) &&
|
2016-07-15 21:45:20 +08:00
|
|
|
"instruction depends on operands that are not available");
|
2016-08-12 04:32:43 +08:00
|
|
|
MoveAccess = false;
|
2016-07-15 21:45:20 +08:00
|
|
|
} else {
|
|
|
|
// When we do not find Repl in HoistPt, select the first in the list
|
|
|
|
// and move it to HoistPt.
|
|
|
|
Repl = InstructionsToHoist.front();
|
|
|
|
|
|
|
|
// We can move Repl in HoistPt only when all operands are available.
|
|
|
|
// The order in which hoistings are done may influence the availability
|
|
|
|
// of operands.
|
2017-09-13 13:28:03 +08:00
|
|
|
if (!allOperandsAvailable(Repl, DestBB)) {
|
2016-08-05 07:49:05 +08:00
|
|
|
// When HoistingGeps there is nothing more we can do to make the
|
|
|
|
// operands available: just continue.
|
|
|
|
if (HoistingGeps)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// When not HoistingGeps we need to copy the GEPs.
|
2017-09-13 13:28:03 +08:00
|
|
|
if (!makeGepOperandsAvailable(Repl, DestBB, InstructionsToHoist))
|
2016-08-05 07:49:05 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-07-27 13:48:12 +08:00
|
|
|
|
2016-08-04 04:54:33 +08:00
|
|
|
// Move the instruction at the end of HoistPt.
|
2017-09-13 13:28:03 +08:00
|
|
|
Instruction *Last = DestBB->getTerminator();
|
2016-12-08 03:55:59 +08:00
|
|
|
MD->removeInstruction(Repl);
|
2016-08-04 04:54:36 +08:00
|
|
|
Repl->moveBefore(Last);
|
|
|
|
|
|
|
|
DFSNumber[Repl] = DFSNumber[Last]++;
|
2016-07-15 21:45:20 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
NR += removeAndReplace(InstructionsToHoist, Repl, DestBB, MoveAccess);
|
|
|
|
|
2016-07-15 21:45:20 +08:00
|
|
|
if (isa<LoadInst>(Repl))
|
|
|
|
++NL;
|
|
|
|
else if (isa<StoreInst>(Repl))
|
|
|
|
++NS;
|
|
|
|
else if (isa<CallInst>(Repl))
|
|
|
|
++NC;
|
|
|
|
else // Scalar
|
|
|
|
++NI;
|
|
|
|
}
|
|
|
|
|
|
|
|
NumHoisted += NL + NS + NC + NI;
|
|
|
|
NumRemoved += NR;
|
|
|
|
NumLoadsHoisted += NL;
|
|
|
|
NumStoresHoisted += NS;
|
|
|
|
NumCallsHoisted += NC;
|
|
|
|
return {NI, NL + NC + NS};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hoist all expressions. Returns Number of scalars hoisted
|
|
|
|
// and number of non-scalars hoisted.
|
|
|
|
std::pair<unsigned, unsigned> hoistExpressions(Function &F) {
|
|
|
|
InsnInfo II;
|
|
|
|
LoadInfo LI;
|
|
|
|
StoreInfo SI;
|
|
|
|
CallInfo CI;
|
|
|
|
for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
|
2016-07-26 08:15:08 +08:00
|
|
|
int InstructionNb = 0;
|
2016-07-15 21:45:20 +08:00
|
|
|
for (Instruction &I1 : *BB) {
|
2017-04-11 04:45:17 +08:00
|
|
|
// If I1 cannot guarantee progress, subsequent instructions
|
|
|
|
// in BB cannot be hoisted anyways.
|
|
|
|
if (!isGuaranteedToTransferExecutionToSuccessor(&I1)) {
|
2017-09-13 13:28:03 +08:00
|
|
|
HoistBarrier.insert(BB);
|
|
|
|
break;
|
2017-04-11 04:45:17 +08:00
|
|
|
}
|
2016-07-26 08:15:08 +08:00
|
|
|
// Only hoist the first instructions in BB up to MaxDepthInBB. Hoisting
|
|
|
|
// deeper may increase the register pressure and compilation time.
|
|
|
|
if (MaxDepthInBB != -1 && InstructionNb++ >= MaxDepthInBB)
|
|
|
|
break;
|
|
|
|
|
2016-09-22 22:45:40 +08:00
|
|
|
// Do not value number terminator instructions.
|
2016-09-22 23:08:09 +08:00
|
|
|
if (isa<TerminatorInst>(&I1))
|
2016-09-22 22:45:40 +08:00
|
|
|
break;
|
|
|
|
|
2016-07-18 08:34:58 +08:00
|
|
|
if (auto *Load = dyn_cast<LoadInst>(&I1))
|
2016-07-15 21:45:20 +08:00
|
|
|
LI.insert(Load, VN);
|
2016-07-18 08:34:58 +08:00
|
|
|
else if (auto *Store = dyn_cast<StoreInst>(&I1))
|
2016-07-15 21:45:20 +08:00
|
|
|
SI.insert(Store, VN);
|
2016-07-18 08:34:58 +08:00
|
|
|
else if (auto *Call = dyn_cast<CallInst>(&I1)) {
|
|
|
|
if (auto *Intr = dyn_cast<IntrinsicInst>(Call)) {
|
2016-07-15 21:45:20 +08:00
|
|
|
if (isa<DbgInfoIntrinsic>(Intr) ||
|
Add an @llvm.sideeffect intrinsic
This patch implements Chandler's idea [0] for supporting languages that
require support for infinite loops with side effects, such as Rust, providing
part of a solution to bug 965 [1].
Specifically, it adds an `llvm.sideeffect()` intrinsic, which has no actual
effect, but which appears to optimization passes to have obscure side effects,
such that they don't optimize away loops containing it. It also teaches
several optimization passes to ignore this intrinsic, so that it doesn't
significantly impact optimization in most cases.
As discussed on llvm-dev [2], this patch is the first of two major parts.
The second part, to change LLVM's semantics to have defined behavior
on infinite loops by default, with a function attribute for opting into
potential-undefined-behavior, will be implemented and posted for review in
a separate patch.
[0] http://lists.llvm.org/pipermail/llvm-dev/2015-July/088103.html
[1] https://bugs.llvm.org/show_bug.cgi?id=965
[2] http://lists.llvm.org/pipermail/llvm-dev/2017-October/118632.html
Differential Revision: https://reviews.llvm.org/D38336
llvm-svn: 317729
2017-11-09 05:59:51 +08:00
|
|
|
Intr->getIntrinsicID() == Intrinsic::assume ||
|
|
|
|
Intr->getIntrinsicID() == Intrinsic::sideeffect)
|
2016-07-15 21:45:20 +08:00
|
|
|
continue;
|
|
|
|
}
|
2017-03-02 01:15:08 +08:00
|
|
|
if (Call->mayHaveSideEffects())
|
|
|
|
break;
|
2016-08-05 04:52:57 +08:00
|
|
|
|
|
|
|
if (Call->isConvergent())
|
|
|
|
break;
|
|
|
|
|
2016-07-15 21:45:20 +08:00
|
|
|
CI.insert(Call, VN);
|
2016-07-27 13:48:12 +08:00
|
|
|
} else if (HoistingGeps || !isa<GetElementPtrInst>(&I1))
|
2016-07-15 21:45:20 +08:00
|
|
|
// Do not hoist scalars past calls that may write to memory because
|
|
|
|
// that could result in spills later. geps are handled separately.
|
|
|
|
// TODO: We can relax this for targets like AArch64 as they have more
|
|
|
|
// registers than X86.
|
|
|
|
II.insert(&I1, VN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HoistingPointList HPL;
|
|
|
|
computeInsertionPoints(II.getVNTable(), HPL, InsKind::Scalar);
|
|
|
|
computeInsertionPoints(LI.getVNTable(), HPL, InsKind::Load);
|
|
|
|
computeInsertionPoints(SI.getVNTable(), HPL, InsKind::Store);
|
|
|
|
computeInsertionPoints(CI.getScalarVNTable(), HPL, InsKind::Scalar);
|
|
|
|
computeInsertionPoints(CI.getLoadVNTable(), HPL, InsKind::Load);
|
|
|
|
computeInsertionPoints(CI.getStoreVNTable(), HPL, InsKind::Store);
|
|
|
|
return hoist(HPL);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class GVNHoistLegacyPass : public FunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
GVNHoistLegacyPass() : FunctionPass(ID) {
|
|
|
|
initializeGVNHoistLegacyPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F) override {
|
2016-07-20 06:57:14 +08:00
|
|
|
if (skipFunction(F))
|
|
|
|
return false;
|
2016-07-15 21:45:20 +08:00
|
|
|
auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2017-09-13 13:28:03 +08:00
|
|
|
auto &PDT = getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
|
2016-07-15 21:45:20 +08:00
|
|
|
auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
|
|
|
|
auto &MD = getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
|
2016-08-23 13:42:41 +08:00
|
|
|
auto &MSSA = getAnalysis<MemorySSAWrapperPass>().getMSSA();
|
2016-07-15 21:45:20 +08:00
|
|
|
|
2017-09-13 13:28:03 +08:00
|
|
|
GVNHoist G(&DT, &PDT, &AA, &MD, &MSSA);
|
2016-07-15 21:45:20 +08:00
|
|
|
return G.run(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
2017-09-13 13:28:03 +08:00
|
|
|
AU.addRequired<PostDominatorTreeWrapperPass>();
|
2016-07-15 21:45:20 +08:00
|
|
|
AU.addRequired<AAResultsWrapperPass>();
|
|
|
|
AU.addRequired<MemoryDependenceWrapperPass>();
|
2016-08-23 13:42:41 +08:00
|
|
|
AU.addRequired<MemorySSAWrapperPass>();
|
2016-07-15 21:45:20 +08:00
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
2016-08-23 13:42:41 +08:00
|
|
|
AU.addPreserved<MemorySSAWrapperPass>();
|
2017-04-18 21:25:49 +08:00
|
|
|
AU.addPreserved<GlobalsAAWrapperPass>();
|
2016-07-15 21:45:20 +08:00
|
|
|
}
|
|
|
|
};
|
2017-10-14 05:17:07 +08:00
|
|
|
|
|
|
|
} // end namespace llvm
|
2016-07-15 21:45:20 +08:00
|
|
|
|
2016-10-13 09:39:10 +08:00
|
|
|
PreservedAnalyses GVNHoistPass::run(Function &F, FunctionAnalysisManager &AM) {
|
2016-07-15 21:45:20 +08:00
|
|
|
DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
|
2017-09-13 13:28:03 +08:00
|
|
|
PostDominatorTree &PDT = AM.getResult<PostDominatorTreeAnalysis>(F);
|
2016-07-15 21:45:20 +08:00
|
|
|
AliasAnalysis &AA = AM.getResult<AAManager>(F);
|
|
|
|
MemoryDependenceResults &MD = AM.getResult<MemoryDependenceAnalysis>(F);
|
2016-08-23 13:42:41 +08:00
|
|
|
MemorySSA &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
|
2017-09-13 13:28:03 +08:00
|
|
|
GVNHoist G(&DT, &PDT, &AA, &MD, &MSSA);
|
2016-07-15 21:45:20 +08:00
|
|
|
if (!G.run(F))
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
|
|
|
|
PreservedAnalyses PA;
|
|
|
|
PA.preserve<DominatorTreeAnalysis>();
|
2016-08-23 13:42:41 +08:00
|
|
|
PA.preserve<MemorySSAAnalysis>();
|
2017-04-18 21:25:49 +08:00
|
|
|
PA.preserve<GlobalsAA>();
|
2016-07-15 21:45:20 +08:00
|
|
|
return PA;
|
|
|
|
}
|
|
|
|
|
|
|
|
char GVNHoistLegacyPass::ID = 0;
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2016-07-15 21:45:20 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(GVNHoistLegacyPass, "gvn-hoist",
|
|
|
|
"Early GVN Hoisting of Expressions", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
|
2016-08-23 13:42:41 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
|
2016-07-15 21:45:20 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
2018-02-08 19:45:36 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
|
2016-07-15 21:45:20 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(GVNHoistLegacyPass, "gvn-hoist",
|
|
|
|
"Early GVN Hoisting of Expressions", false, false)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createGVNHoistPass() { return new GVNHoistLegacyPass(); }
|