forked from OSchip/llvm-project
Revert "[Constant Hoisting] Extend coverage of the constant hoisting pass."
I will break this up into smaller pieces for review and recommit. llvm-svn: 204393
This commit is contained in:
parent
59442b4f3a
commit
46357931ab
|
@ -297,10 +297,10 @@ public:
|
||||||
/// \brief Return the expected cost of materialization for the given integer
|
/// \brief Return the expected cost of materialization for the given integer
|
||||||
/// immediate of the specified type for a given instruction. The cost can be
|
/// immediate of the specified type for a given instruction. The cost can be
|
||||||
/// zero if the immediate can be folded into the specified instruction.
|
/// zero if the immediate can be folded into the specified instruction.
|
||||||
virtual unsigned getIntImmCost(unsigned Opc, unsigned Idx, const APInt &Imm,
|
virtual unsigned getIntImmCost(unsigned Opcode, const APInt &Imm,
|
||||||
|
Type *Ty) const;
|
||||||
|
virtual unsigned getIntImmCost(Intrinsic::ID IID, const APInt &Imm,
|
||||||
Type *Ty) const;
|
Type *Ty) const;
|
||||||
virtual unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx,
|
|
||||||
const APInt &Imm, Type *Ty) const;
|
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
/// \name Vector Target Information
|
/// \name Vector Target Information
|
||||||
|
|
|
@ -148,14 +148,14 @@ unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
|
||||||
return PrevTTI->getIntImmCost(Imm, Ty);
|
return PrevTTI->getIntImmCost(Imm, Ty);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned TargetTransformInfo::getIntImmCost(unsigned Opc, unsigned Idx,
|
unsigned TargetTransformInfo::getIntImmCost(unsigned Opcode, const APInt &Imm,
|
||||||
const APInt &Imm, Type *Ty) const {
|
Type *Ty) const {
|
||||||
return PrevTTI->getIntImmCost(Opc, Idx, Imm, Ty);
|
return PrevTTI->getIntImmCost(Opcode, Imm, Ty);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
|
unsigned TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, const APInt &Imm,
|
||||||
const APInt &Imm, Type *Ty) const {
|
Type *Ty) const {
|
||||||
return PrevTTI->getIntImmCost(IID, Idx, Imm, Ty);
|
return PrevTTI->getIntImmCost(IID, Imm, Ty);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
|
unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
|
||||||
|
@ -539,12 +539,12 @@ struct NoTTI final : ImmutablePass, TargetTransformInfo {
|
||||||
return TCC_Basic;
|
return TCC_Basic;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
|
unsigned getIntImmCost(unsigned Opcode, const APInt &Imm,
|
||||||
Type *Ty) const override {
|
Type *Ty) const override {
|
||||||
return TCC_Free;
|
return TCC_Free;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
|
unsigned getIntImmCost(Intrinsic::ID IID, const APInt &Imm,
|
||||||
Type *Ty) const override {
|
Type *Ty) const override {
|
||||||
return TCC_Free;
|
return TCC_Free;
|
||||||
}
|
}
|
||||||
|
|
|
@ -103,9 +103,9 @@ public:
|
||||||
|
|
||||||
unsigned getIntImmCost(const APInt &Imm, Type *Ty) const override;
|
unsigned getIntImmCost(const APInt &Imm, Type *Ty) const override;
|
||||||
|
|
||||||
unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
|
unsigned getIntImmCost(unsigned Opcode, const APInt &Imm,
|
||||||
Type *Ty) const override;
|
Type *Ty) const override;
|
||||||
unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
|
unsigned getIntImmCost(Intrinsic::ID IID, const APInt &Imm,
|
||||||
Type *Ty) const override;
|
Type *Ty) const override;
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
|
@ -776,9 +776,6 @@ unsigned X86TTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
|
||||||
if (BitSize == 0)
|
if (BitSize == 0)
|
||||||
return ~0U;
|
return ~0U;
|
||||||
|
|
||||||
if (Imm == 0)
|
|
||||||
return TCC_Free;
|
|
||||||
|
|
||||||
if (Imm.getBitWidth() <= 64 &&
|
if (Imm.getBitWidth() <= 64 &&
|
||||||
(isInt<32>(Imm.getSExtValue()) || isUInt<32>(Imm.getZExtValue())))
|
(isInt<32>(Imm.getSExtValue()) || isUInt<32>(Imm.getZExtValue())))
|
||||||
return TCC_Basic;
|
return TCC_Basic;
|
||||||
|
@ -786,7 +783,7 @@ unsigned X86TTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
|
||||||
return 2 * TCC_Basic;
|
return 2 * TCC_Basic;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned X86TTI::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
|
unsigned X86TTI::getIntImmCost(unsigned Opcode, const APInt &Imm,
|
||||||
Type *Ty) const {
|
Type *Ty) const {
|
||||||
assert(Ty->isIntegerTy());
|
assert(Ty->isIntegerTy());
|
||||||
|
|
||||||
|
@ -794,15 +791,7 @@ unsigned X86TTI::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
|
||||||
if (BitSize == 0)
|
if (BitSize == 0)
|
||||||
return ~0U;
|
return ~0U;
|
||||||
|
|
||||||
unsigned ImmIdx = ~0U;
|
|
||||||
switch (Opcode) {
|
switch (Opcode) {
|
||||||
default: return TCC_Free;
|
|
||||||
case Instruction::GetElementPtr:
|
|
||||||
if (Idx != 0)
|
|
||||||
return TCC_Free;
|
|
||||||
case Instruction::Store:
|
|
||||||
ImmIdx = 0;
|
|
||||||
break;
|
|
||||||
case Instruction::Add:
|
case Instruction::Add:
|
||||||
case Instruction::Sub:
|
case Instruction::Sub:
|
||||||
case Instruction::Mul:
|
case Instruction::Mul:
|
||||||
|
@ -817,31 +806,28 @@ unsigned X86TTI::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
|
||||||
case Instruction::Or:
|
case Instruction::Or:
|
||||||
case Instruction::Xor:
|
case Instruction::Xor:
|
||||||
case Instruction::ICmp:
|
case Instruction::ICmp:
|
||||||
ImmIdx = 1;
|
if (Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
|
||||||
break;
|
return TCC_Free;
|
||||||
|
else
|
||||||
|
return X86TTI::getIntImmCost(Imm, Ty);
|
||||||
case Instruction::Trunc:
|
case Instruction::Trunc:
|
||||||
case Instruction::ZExt:
|
case Instruction::ZExt:
|
||||||
case Instruction::SExt:
|
case Instruction::SExt:
|
||||||
case Instruction::IntToPtr:
|
case Instruction::IntToPtr:
|
||||||
case Instruction::PtrToInt:
|
case Instruction::PtrToInt:
|
||||||
case Instruction::BitCast:
|
case Instruction::BitCast:
|
||||||
case Instruction::PHI:
|
|
||||||
case Instruction::Call:
|
case Instruction::Call:
|
||||||
case Instruction::Select:
|
case Instruction::Select:
|
||||||
case Instruction::Ret:
|
case Instruction::Ret:
|
||||||
case Instruction::Load:
|
case Instruction::Load:
|
||||||
break;
|
case Instruction::Store:
|
||||||
|
return X86TTI::getIntImmCost(Imm, Ty);
|
||||||
}
|
}
|
||||||
|
return TargetTransformInfo::getIntImmCost(Opcode, Imm, Ty);
|
||||||
if ((Idx == ImmIdx) &&
|
|
||||||
Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
|
|
||||||
return TCC_Free;
|
|
||||||
|
|
||||||
return X86TTI::getIntImmCost(Imm, Ty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned X86TTI::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
|
unsigned X86TTI::getIntImmCost(Intrinsic::ID IID, const APInt &Imm,
|
||||||
const APInt &Imm, Type *Ty) const {
|
Type *Ty) const {
|
||||||
assert(Ty->isIntegerTy());
|
assert(Ty->isIntegerTy());
|
||||||
|
|
||||||
unsigned BitSize = Ty->getPrimitiveSizeInBits();
|
unsigned BitSize = Ty->getPrimitiveSizeInBits();
|
||||||
|
@ -849,24 +835,21 @@ unsigned X86TTI::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
|
||||||
return ~0U;
|
return ~0U;
|
||||||
|
|
||||||
switch (IID) {
|
switch (IID) {
|
||||||
default: return TCC_Free;
|
default: return TargetTransformInfo::getIntImmCost(IID, Imm, Ty);
|
||||||
case Intrinsic::sadd_with_overflow:
|
case Intrinsic::sadd_with_overflow:
|
||||||
case Intrinsic::uadd_with_overflow:
|
case Intrinsic::uadd_with_overflow:
|
||||||
case Intrinsic::ssub_with_overflow:
|
case Intrinsic::ssub_with_overflow:
|
||||||
case Intrinsic::usub_with_overflow:
|
case Intrinsic::usub_with_overflow:
|
||||||
case Intrinsic::smul_with_overflow:
|
case Intrinsic::smul_with_overflow:
|
||||||
case Intrinsic::umul_with_overflow:
|
case Intrinsic::umul_with_overflow:
|
||||||
if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
|
if (Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
|
||||||
return TCC_Free;
|
return TCC_Free;
|
||||||
else
|
else
|
||||||
return X86TTI::getIntImmCost(Imm, Ty);
|
return X86TTI::getIntImmCost(Imm, Ty);
|
||||||
case Intrinsic::experimental_stackmap:
|
case Intrinsic::experimental_stackmap:
|
||||||
if (Idx < 2)
|
|
||||||
return TCC_Free;
|
|
||||||
case Intrinsic::experimental_patchpoint_void:
|
case Intrinsic::experimental_patchpoint_void:
|
||||||
case Intrinsic::experimental_patchpoint_i64:
|
case Intrinsic::experimental_patchpoint_i64:
|
||||||
if ((Idx < 4 ) ||
|
if (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))
|
||||||
(Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
|
|
||||||
return TCC_Free;
|
return TCC_Free;
|
||||||
else
|
else
|
||||||
return X86TTI::getIntImmCost(Imm, Ty);
|
return X86TTI::getIntImmCost(Imm, Ty);
|
||||||
|
|
|
@ -35,14 +35,15 @@
|
||||||
|
|
||||||
#define DEBUG_TYPE "consthoist"
|
#define DEBUG_TYPE "consthoist"
|
||||||
#include "llvm/Transforms/Scalar.h"
|
#include "llvm/Transforms/Scalar.h"
|
||||||
|
#include "llvm/ADT/MapVector.h"
|
||||||
#include "llvm/ADT/SmallSet.h"
|
#include "llvm/ADT/SmallSet.h"
|
||||||
#include "llvm/ADT/SmallVector.h"
|
|
||||||
#include "llvm/ADT/Statistic.h"
|
#include "llvm/ADT/Statistic.h"
|
||||||
#include "llvm/Analysis/TargetTransformInfo.h"
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
||||||
#include "llvm/IR/Constants.h"
|
#include "llvm/IR/Constants.h"
|
||||||
#include "llvm/IR/Dominators.h"
|
#include "llvm/IR/Dominators.h"
|
||||||
#include "llvm/IR/IntrinsicInst.h"
|
#include "llvm/IR/IntrinsicInst.h"
|
||||||
#include "llvm/Pass.h"
|
#include "llvm/Pass.h"
|
||||||
|
#include "llvm/Support/CommandLine.h"
|
||||||
#include "llvm/Support/Debug.h"
|
#include "llvm/Support/Debug.h"
|
||||||
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
@ -50,80 +51,42 @@ using namespace llvm;
|
||||||
STATISTIC(NumConstantsHoisted, "Number of constants hoisted");
|
STATISTIC(NumConstantsHoisted, "Number of constants hoisted");
|
||||||
STATISTIC(NumConstantsRebased, "Number of constants rebased");
|
STATISTIC(NumConstantsRebased, "Number of constants rebased");
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
struct ConstantUser;
|
typedef SmallVector<User *, 4> ConstantUseListType;
|
||||||
struct RebasedConstantInfo;
|
|
||||||
|
|
||||||
typedef SmallVector<ConstantUser, 8> ConstantUseListType;
|
|
||||||
typedef SmallVector<RebasedConstantInfo, 4> RebasedConstantListType;
|
|
||||||
|
|
||||||
/// \brief Keeps track of the user of a constant and the operand index where the
|
|
||||||
/// constant is used.
|
|
||||||
struct ConstantUser {
|
|
||||||
Instruction *Inst;
|
|
||||||
unsigned OpndIdx;
|
|
||||||
|
|
||||||
ConstantUser(Instruction *Inst, unsigned Idx) : Inst(Inst), OpndIdx(Idx) { }
|
|
||||||
};
|
|
||||||
|
|
||||||
/// \brief Keeps track of a constant candidate and its usees.
|
|
||||||
struct ConstantCandidate {
|
struct ConstantCandidate {
|
||||||
ConstantUseListType Uses;
|
|
||||||
ConstantInt *ConstInt;
|
|
||||||
unsigned CumulativeCost;
|
unsigned CumulativeCost;
|
||||||
|
|
||||||
ConstantCandidate(ConstantInt *ConstInt)
|
|
||||||
: ConstInt(ConstInt), CumulativeCost(0) { }
|
|
||||||
|
|
||||||
/// \brief Add the user to the use list and update the cost.
|
|
||||||
void addUser(Instruction *Inst, unsigned Idx, unsigned Cost) {
|
|
||||||
CumulativeCost += Cost;
|
|
||||||
Uses.push_back(ConstantUser(Inst, Idx));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/// \brief This represents a constant that has been rebased with respect to a
|
|
||||||
/// base constant. The difference to the base constant is recorded in Offset.
|
|
||||||
struct RebasedConstantInfo {
|
|
||||||
ConstantUseListType Uses;
|
ConstantUseListType Uses;
|
||||||
Constant *Offset;
|
|
||||||
mutable BasicBlock *IDom;
|
|
||||||
|
|
||||||
RebasedConstantInfo(ConstantUseListType &&Uses, Constant *Offset)
|
|
||||||
: Uses(Uses), Offset(Offset), IDom(nullptr) { }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// \brief A base constant and all its rebased constants.
|
|
||||||
struct ConstantInfo {
|
struct ConstantInfo {
|
||||||
ConstantInt *BaseConstant;
|
ConstantInt *BaseConstant;
|
||||||
|
struct RebasedConstantInfo {
|
||||||
|
ConstantInt *OriginalConstant;
|
||||||
|
Constant *Offset;
|
||||||
|
ConstantUseListType Uses;
|
||||||
|
};
|
||||||
|
typedef SmallVector<RebasedConstantInfo, 4> RebasedConstantListType;
|
||||||
RebasedConstantListType RebasedConstants;
|
RebasedConstantListType RebasedConstants;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// \brief The constant hoisting pass.
|
|
||||||
class ConstantHoisting : public FunctionPass {
|
class ConstantHoisting : public FunctionPass {
|
||||||
typedef DenseMap<ConstantInt *, unsigned> ConstCandMapType;
|
|
||||||
typedef std::vector<ConstantCandidate> ConstCandVecType;
|
|
||||||
|
|
||||||
const TargetTransformInfo *TTI;
|
const TargetTransformInfo *TTI;
|
||||||
DominatorTree *DT;
|
DominatorTree *DT;
|
||||||
BasicBlock *Entry;
|
|
||||||
|
|
||||||
/// Keeps track of constant candidates found in the function.
|
/// Keeps track of expensive constants found in the function.
|
||||||
ConstCandMapType ConstCandMap;
|
typedef MapVector<ConstantInt *, ConstantCandidate> ConstantMapType;
|
||||||
ConstCandVecType ConstCandVec;
|
ConstantMapType ConstantMap;
|
||||||
|
|
||||||
/// Keep track of cast instructions we already cloned.
|
|
||||||
SmallDenseMap<Instruction *, Instruction *> ClonedCastMap;
|
|
||||||
|
|
||||||
/// These are the final constants we decided to hoist.
|
/// These are the final constants we decided to hoist.
|
||||||
SmallVector<ConstantInfo, 8> ConstantVec;
|
SmallVector<ConstantInfo, 4> Constants;
|
||||||
public:
|
public:
|
||||||
static char ID; // Pass identification, replacement for typeid
|
static char ID; // Pass identification, replacement for typeid
|
||||||
ConstantHoisting() : FunctionPass(ID), TTI(0), DT(0), Entry(0) {
|
ConstantHoisting() : FunctionPass(ID), TTI(0) {
|
||||||
initializeConstantHoistingPass(*PassRegistry::getPassRegistry());
|
initializeConstantHoistingPass(*PassRegistry::getPassRegistry());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool runOnFunction(Function &Fn) override;
|
bool runOnFunction(Function &F) override;
|
||||||
|
|
||||||
const char *getPassName() const override { return "Constant Hoisting"; }
|
const char *getPassName() const override { return "Constant Hoisting"; }
|
||||||
|
|
||||||
|
@ -134,49 +97,19 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// \brief Initialize the pass.
|
void CollectConstant(User *U, unsigned Opcode, Intrinsic::ID IID,
|
||||||
void setup(Function &Fn) {
|
ConstantInt *C);
|
||||||
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
void CollectConstants(Instruction *I);
|
||||||
TTI = &getAnalysis<TargetTransformInfo>();
|
void CollectConstants(Function &F);
|
||||||
Entry = &Fn.getEntryBlock();
|
void FindAndMakeBaseConstant(ConstantMapType::iterator S,
|
||||||
}
|
ConstantMapType::iterator E);
|
||||||
|
void FindBaseConstants();
|
||||||
/// \brief Cleanup.
|
Instruction *FindConstantInsertionPoint(Function &F,
|
||||||
void cleanup() {
|
const ConstantInfo &CI) const;
|
||||||
ConstantVec.clear();
|
void EmitBaseConstants(Function &F, User *U, Instruction *Base,
|
||||||
ClonedCastMap.clear();
|
Constant *Offset, ConstantInt *OriginalConstant);
|
||||||
ConstCandVec.clear();
|
bool EmitBaseConstants(Function &F);
|
||||||
ConstCandMap.clear();
|
bool OptimizeConstants(Function &F);
|
||||||
|
|
||||||
TTI = nullptr;
|
|
||||||
DT = nullptr;
|
|
||||||
Entry = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief Find the common dominator of all uses and cache the result for
|
|
||||||
/// future lookup.
|
|
||||||
BasicBlock *getIDom(const RebasedConstantInfo &RCI) const {
|
|
||||||
if (RCI.IDom)
|
|
||||||
return RCI.IDom;
|
|
||||||
RCI.IDom = findIDomOfAllUses(RCI.Uses);
|
|
||||||
assert(RCI.IDom && "Invalid IDom.");
|
|
||||||
return RCI.IDom;
|
|
||||||
}
|
|
||||||
|
|
||||||
BasicBlock *findIDomOfAllUses(const ConstantUseListType &Uses) const;
|
|
||||||
Instruction *findMatInsertPt(Instruction *I, unsigned Idx = ~0U) const;
|
|
||||||
Instruction *findConstantInsertionPoint(const ConstantInfo &CI) const;
|
|
||||||
void collectConstantCandidates(Instruction *I, unsigned Idx, ConstantInt *C);
|
|
||||||
void collectConstantCandidates(Instruction *I);
|
|
||||||
void collectConstantCandidates(Function &Fn);
|
|
||||||
void findAndMakeBaseConstant(ConstCandVecType::iterator S,
|
|
||||||
ConstCandVecType::iterator E);
|
|
||||||
void findBaseConstants();
|
|
||||||
void emitBaseConstants(Instruction *Base, Constant *Offset,
|
|
||||||
const ConstantUser &CU);
|
|
||||||
bool emitBaseConstants();
|
|
||||||
void deleteDeadCastInst() const;
|
|
||||||
bool optimizeConstants(Function &F);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,352 +126,297 @@ FunctionPass *llvm::createConstantHoistingPass() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Perform the constant hoisting optimization for the given function.
|
/// \brief Perform the constant hoisting optimization for the given function.
|
||||||
bool ConstantHoisting::runOnFunction(Function &Fn) {
|
bool ConstantHoisting::runOnFunction(Function &F) {
|
||||||
DEBUG(dbgs() << "********** Begin Constant Hoisting **********\n");
|
DEBUG(dbgs() << "********** Constant Hoisting **********\n");
|
||||||
DEBUG(dbgs() << "********** Function: " << Fn.getName() << '\n');
|
DEBUG(dbgs() << "********** Function: " << F.getName() << '\n');
|
||||||
|
|
||||||
setup(Fn);
|
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
||||||
|
TTI = &getAnalysis<TargetTransformInfo>();
|
||||||
|
|
||||||
bool MadeChange = optimizeConstants(Fn);
|
return OptimizeConstants(F);
|
||||||
|
|
||||||
if (MadeChange) {
|
|
||||||
DEBUG(dbgs() << "********** Function after Constant Hoisting: "
|
|
||||||
<< Fn.getName() << '\n');
|
|
||||||
DEBUG(dbgs() << Fn);
|
|
||||||
}
|
|
||||||
DEBUG(dbgs() << "********** End Constant Hoisting **********\n");
|
|
||||||
|
|
||||||
cleanup();
|
|
||||||
|
|
||||||
return MadeChange;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Find nearest common dominator of all uses.
|
void ConstantHoisting::CollectConstant(User * U, unsigned Opcode,
|
||||||
/// FIXME: Replace this with NearestCommonDominator once it is in common code.
|
Intrinsic::ID IID, ConstantInt *C) {
|
||||||
BasicBlock *
|
|
||||||
ConstantHoisting::findIDomOfAllUses(const ConstantUseListType &Uses) const {
|
|
||||||
// Collect all basic blocks.
|
|
||||||
SmallPtrSet<BasicBlock *, 8> BBs;
|
|
||||||
for (auto const &U : Uses)
|
|
||||||
BBs.insert(findMatInsertPt(U.Inst, U.OpndIdx)->getParent());
|
|
||||||
|
|
||||||
if (BBs.count(Entry))
|
|
||||||
return Entry;
|
|
||||||
|
|
||||||
while (BBs.size() >= 2) {
|
|
||||||
BasicBlock *BB, *BB1, *BB2;
|
|
||||||
BB1 = *BBs.begin();
|
|
||||||
BB2 = *std::next(BBs.begin());
|
|
||||||
BB = DT->findNearestCommonDominator(BB1, BB2);
|
|
||||||
if (BB == Entry)
|
|
||||||
return Entry;
|
|
||||||
BBs.erase(BB1);
|
|
||||||
BBs.erase(BB2);
|
|
||||||
BBs.insert(BB);
|
|
||||||
}
|
|
||||||
assert((BBs.size() == 1) && "Expected only one element.");
|
|
||||||
return *BBs.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief Find the constant materialization insertion point.
|
|
||||||
Instruction *ConstantHoisting::findMatInsertPt(Instruction *Inst,
|
|
||||||
unsigned Idx) const {
|
|
||||||
// The simple and common case.
|
|
||||||
if (!isa<PHINode>(Inst) && !isa<LandingPadInst>(Inst))
|
|
||||||
return Inst;
|
|
||||||
|
|
||||||
// We can't insert directly before a phi node or landing pad. Insert before
|
|
||||||
// the terminator of the incoming or dominating block.
|
|
||||||
assert(Entry != Inst->getParent() && "PHI or landing pad in entry block!");
|
|
||||||
if (Idx != ~0U && isa<PHINode>(Inst))
|
|
||||||
return cast<PHINode>(Inst)->getIncomingBlock(Idx)->getTerminator();
|
|
||||||
|
|
||||||
BasicBlock *IDom = DT->getNode(Inst->getParent())->getIDom()->getBlock();
|
|
||||||
return IDom->getTerminator();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief Find an insertion point that dominates all uses.
|
|
||||||
Instruction *ConstantHoisting::
|
|
||||||
findConstantInsertionPoint(const ConstantInfo &ConstInfo) const {
|
|
||||||
assert(!ConstInfo.RebasedConstants.empty() && "Invalid constant info entry.");
|
|
||||||
// Collect all IDoms.
|
|
||||||
SmallPtrSet<BasicBlock *, 8> BBs;
|
|
||||||
for (auto const &RCI : ConstInfo.RebasedConstants)
|
|
||||||
BBs.insert(getIDom(RCI));
|
|
||||||
|
|
||||||
assert(!BBs.empty() && "No dominators!?");
|
|
||||||
|
|
||||||
if (BBs.count(Entry))
|
|
||||||
return &Entry->front();
|
|
||||||
|
|
||||||
while (BBs.size() >= 2) {
|
|
||||||
BasicBlock *BB, *BB1, *BB2;
|
|
||||||
BB1 = *BBs.begin();
|
|
||||||
BB2 = *std::next(BBs.begin());
|
|
||||||
BB = DT->findNearestCommonDominator(BB1, BB2);
|
|
||||||
if (BB == Entry)
|
|
||||||
return &Entry->front();
|
|
||||||
BBs.erase(BB1);
|
|
||||||
BBs.erase(BB2);
|
|
||||||
BBs.insert(BB);
|
|
||||||
}
|
|
||||||
assert((BBs.size() == 1) && "Expected only one element.");
|
|
||||||
Instruction &FirstInst = (*BBs.begin())->front();
|
|
||||||
return findMatInsertPt(&FirstInst);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// \brief Record constant integer ConstInt for instruction Inst at operand
|
|
||||||
/// index Idx.
|
|
||||||
///
|
|
||||||
/// The operand at index Idx is not necessarily the constant inetger itself. It
|
|
||||||
/// could also be a cast instruction or a constant expression that uses the
|
|
||||||
// constant integer.
|
|
||||||
void ConstantHoisting::collectConstantCandidates(Instruction *Inst,
|
|
||||||
unsigned Idx,
|
|
||||||
ConstantInt *ConstInt) {
|
|
||||||
unsigned Cost;
|
unsigned Cost;
|
||||||
// Ask the target about the cost of materializing the constant for the given
|
if (Opcode)
|
||||||
// instruction and operand index.
|
Cost = TTI->getIntImmCost(Opcode, C->getValue(), C->getType());
|
||||||
if (auto IntrInst = dyn_cast<IntrinsicInst>(Inst))
|
|
||||||
Cost = TTI->getIntImmCost(IntrInst->getIntrinsicID(), Idx,
|
|
||||||
ConstInt->getValue(), ConstInt->getType());
|
|
||||||
else
|
else
|
||||||
Cost = TTI->getIntImmCost(Inst->getOpcode(), Idx, ConstInt->getValue(),
|
Cost = TTI->getIntImmCost(IID, C->getValue(), C->getType());
|
||||||
ConstInt->getType());
|
|
||||||
|
|
||||||
// Ignore cheap integer constants.
|
|
||||||
if (Cost > TargetTransformInfo::TCC_Basic) {
|
if (Cost > TargetTransformInfo::TCC_Basic) {
|
||||||
ConstCandMapType::iterator Itr;
|
ConstantCandidate &CC = ConstantMap[C];
|
||||||
bool Inserted;
|
CC.CumulativeCost += Cost;
|
||||||
std::tie(Itr, Inserted) = ConstCandMap.insert(std::make_pair(ConstInt, 0));
|
CC.Uses.push_back(U);
|
||||||
if (Inserted) {
|
DEBUG(dbgs() << "Collect constant " << *C << " with cost " << Cost
|
||||||
ConstCandVec.push_back(ConstantCandidate(ConstInt));
|
<< " from " << *U << '\n');
|
||||||
Itr->second = ConstCandVec.size() - 1;
|
|
||||||
}
|
|
||||||
ConstCandVec[Itr->second].addUser(Inst, Idx, Cost);
|
|
||||||
DEBUG(if (auto ConstInt = dyn_cast<ConstantInt>(Inst->getOperand(Idx)))
|
|
||||||
dbgs() << "Collect constant " << *ConstInt << " from " << *Inst
|
|
||||||
<< " with cost " << Cost << '\n';
|
|
||||||
else
|
|
||||||
dbgs() << "Collect constant " << *ConstInt << " indirectly from "
|
|
||||||
<< *Inst << " via " << *Inst->getOperand(Idx) << " with cost "
|
|
||||||
<< Cost << '\n';
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Scan the instruction for expensive integer constants and record them
|
/// \brief Scan the instruction or constant expression for expensive integer
|
||||||
/// in the constant candidate vector.
|
/// constants and record them in the constant map.
|
||||||
void ConstantHoisting::collectConstantCandidates(Instruction *Inst) {
|
void ConstantHoisting::CollectConstants(Instruction *I) {
|
||||||
// Skip all cast instructions. They are visited indirectly later on.
|
unsigned Opcode = 0;
|
||||||
if (Inst->isCast())
|
Intrinsic::ID IID = Intrinsic::not_intrinsic;
|
||||||
return;
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
|
||||||
|
IID = II->getIntrinsicID();
|
||||||
// Can't handle inline asm. Skip it.
|
else
|
||||||
if (auto Call = dyn_cast<CallInst>(Inst))
|
Opcode = I->getOpcode();
|
||||||
if (isa<InlineAsm>(Call->getCalledValue()))
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Scan all operands.
|
// Scan all operands.
|
||||||
for (unsigned Idx = 0, E = Inst->getNumOperands(); Idx != E; ++Idx) {
|
for (User::op_iterator O = I->op_begin(), E = I->op_end(); O != E; ++O) {
|
||||||
Value *Opnd = Inst->getOperand(Idx);
|
if (ConstantInt *C = dyn_cast<ConstantInt>(O)) {
|
||||||
|
CollectConstant(I, Opcode, IID, C);
|
||||||
// Vist constant integers.
|
|
||||||
if (auto ConstInt = dyn_cast<ConstantInt>(Opnd)) {
|
|
||||||
collectConstantCandidates(Inst, Idx, ConstInt);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(O)) {
|
||||||
// Visit cast instructions that have constant integers.
|
// We only handle constant cast expressions.
|
||||||
if (auto CastInst = dyn_cast<Instruction>(Opnd)) {
|
if (!CE->isCast())
|
||||||
// Only visit cast instructions, which have been skipped. All other
|
|
||||||
// instructions should have already been visited.
|
|
||||||
if (!CastInst->isCast())
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (auto *ConstInt = dyn_cast<ConstantInt>(CastInst->getOperand(0))) {
|
if (ConstantInt *C = dyn_cast<ConstantInt>(CE->getOperand(0))) {
|
||||||
// Pretend the constant is directly used by the instruction and ignore
|
// Ignore the cast expression and use the opcode of the instruction.
|
||||||
// the cast instruction.
|
CollectConstant(CE, Opcode, IID, C);
|
||||||
collectConstantCandidates(Inst, Idx, ConstInt);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Visit constant expressions that have constant integers.
|
|
||||||
if (auto ConstExpr = dyn_cast<ConstantExpr>(Opnd)) {
|
|
||||||
// Only visit constant cast expressions.
|
|
||||||
if (!ConstExpr->isCast())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (auto ConstInt = dyn_cast<ConstantInt>(ConstExpr->getOperand(0))) {
|
|
||||||
// Pretend the constant is directly used by the instruction and ignore
|
|
||||||
// the constant expression.
|
|
||||||
collectConstantCandidates(Inst, Idx, ConstInt);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} // end of for all operands
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Collect all integer constants in the function that cannot be folded
|
/// \brief Collect all integer constants in the function that cannot be folded
|
||||||
/// into an instruction itself.
|
/// into an instruction itself.
|
||||||
void ConstantHoisting::collectConstantCandidates(Function &Fn) {
|
void ConstantHoisting::CollectConstants(Function &F) {
|
||||||
for (Function::iterator BB : Fn)
|
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
||||||
for (BasicBlock::iterator I : *BB)
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
|
||||||
collectConstantCandidates(I);
|
CollectConstants(I);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Find the base constant within the given range and rebase all other
|
/// \brief Find the base constant within the given range and rebase all other
|
||||||
/// constants with respect to the base constant.
|
/// constants with respect to the base constant.
|
||||||
void ConstantHoisting::findAndMakeBaseConstant(ConstCandVecType::iterator S,
|
void ConstantHoisting::FindAndMakeBaseConstant(ConstantMapType::iterator S,
|
||||||
ConstCandVecType::iterator E) {
|
ConstantMapType::iterator E) {
|
||||||
auto MaxCostItr = S;
|
ConstantMapType::iterator MaxCostItr = S;
|
||||||
unsigned NumUses = 0;
|
unsigned NumUses = 0;
|
||||||
// Use the constant that has the maximum cost as base constant.
|
// Use the constant that has the maximum cost as base constant.
|
||||||
for (auto ConstCand = S; ConstCand != E; ++ConstCand) {
|
for (ConstantMapType::iterator I = S; I != E; ++I) {
|
||||||
NumUses += ConstCand->Uses.size();
|
NumUses += I->second.Uses.size();
|
||||||
if (ConstCand->CumulativeCost > MaxCostItr->CumulativeCost)
|
if (I->second.CumulativeCost > MaxCostItr->second.CumulativeCost)
|
||||||
MaxCostItr = ConstCand;
|
MaxCostItr = I;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't hoist constants that have only one use.
|
// Don't hoist constants that have only one use.
|
||||||
if (NumUses <= 1)
|
if (NumUses <= 1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ConstantInfo ConstInfo;
|
ConstantInfo CI;
|
||||||
ConstInfo.BaseConstant = MaxCostItr->ConstInt;
|
CI.BaseConstant = MaxCostItr->first;
|
||||||
Type *Ty = ConstInfo.BaseConstant->getType();
|
Type *Ty = CI.BaseConstant->getType();
|
||||||
|
|
||||||
// Rebase the constants with respect to the base constant.
|
// Rebase the constants with respect to the base constant.
|
||||||
for (auto ConstCand = S; ConstCand != E; ++ConstCand) {
|
for (ConstantMapType::iterator I = S; I != E; ++I) {
|
||||||
APInt Diff = ConstCand->ConstInt->getValue() -
|
APInt Diff = I->first->getValue() - CI.BaseConstant->getValue();
|
||||||
ConstInfo.BaseConstant->getValue();
|
ConstantInfo::RebasedConstantInfo RCI;
|
||||||
Constant *Offset = Diff == 0 ? nullptr : ConstantInt::get(Ty, Diff);
|
RCI.OriginalConstant = I->first;
|
||||||
ConstInfo.RebasedConstants.push_back(
|
RCI.Offset = ConstantInt::get(Ty, Diff);
|
||||||
RebasedConstantInfo(std::move(ConstCand->Uses), Offset));
|
RCI.Uses = std::move(I->second.Uses);
|
||||||
|
CI.RebasedConstants.push_back(RCI);
|
||||||
}
|
}
|
||||||
ConstantVec.push_back(ConstInfo);
|
Constants.push_back(CI);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Finds and combines constant candidates that can be easily
|
/// \brief Finds and combines constants that can be easily rematerialized with
|
||||||
/// rematerialized with an add from a common base constant.
|
/// an add from a common base constant.
|
||||||
void ConstantHoisting::findBaseConstants() {
|
void ConstantHoisting::FindBaseConstants() {
|
||||||
// Sort the constants by value and type. This invalidates the mapping!
|
// Sort the constants by value and type. This invalidates the mapping.
|
||||||
std::sort(ConstCandVec.begin(), ConstCandVec.end(),
|
std::sort(ConstantMap.begin(), ConstantMap.end(),
|
||||||
[](const ConstantCandidate &LHS, const ConstantCandidate &RHS) {
|
[](const std::pair<ConstantInt *, ConstantCandidate> &LHS,
|
||||||
if (LHS.ConstInt->getType() != RHS.ConstInt->getType())
|
const std::pair<ConstantInt *, ConstantCandidate> &RHS) {
|
||||||
return LHS.ConstInt->getType()->getBitWidth() <
|
if (LHS.first->getType() != RHS.first->getType())
|
||||||
RHS.ConstInt->getType()->getBitWidth();
|
return LHS.first->getType()->getBitWidth() <
|
||||||
return LHS.ConstInt->getValue().ult(RHS.ConstInt->getValue());
|
RHS.first->getType()->getBitWidth();
|
||||||
});
|
return LHS.first->getValue().ult(RHS.first->getValue());
|
||||||
|
});
|
||||||
|
|
||||||
// Simple linear scan through the sorted constant candidate vector for viable
|
// Simple linear scan through the sorted constant map for viable merge
|
||||||
// merge candidates.
|
// candidates.
|
||||||
auto MinValItr = ConstCandVec.begin();
|
ConstantMapType::iterator MinValItr = ConstantMap.begin();
|
||||||
for (auto CC = std::next(ConstCandVec.begin()), E = ConstCandVec.end();
|
for (ConstantMapType::iterator I = std::next(ConstantMap.begin()),
|
||||||
CC != E; ++CC) {
|
E = ConstantMap.end(); I != E; ++I) {
|
||||||
if (MinValItr->ConstInt->getType() == CC->ConstInt->getType()) {
|
if (MinValItr->first->getType() == I->first->getType()) {
|
||||||
// Check if the constant is in range of an add with immediate.
|
// Check if the constant is in range of an add with immediate.
|
||||||
APInt Diff = CC->ConstInt->getValue() - MinValItr->ConstInt->getValue();
|
APInt Diff = I->first->getValue() - MinValItr->first->getValue();
|
||||||
if ((Diff.getBitWidth() <= 64) &&
|
if ((Diff.getBitWidth() <= 64) &&
|
||||||
TTI->isLegalAddImmediate(Diff.getSExtValue()))
|
TTI->isLegalAddImmediate(Diff.getSExtValue()))
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// We either have now a different constant type or the constant is not in
|
// We either have now a different constant type or the constant is not in
|
||||||
// range of an add with immediate anymore.
|
// range of an add with immediate anymore.
|
||||||
findAndMakeBaseConstant(MinValItr, CC);
|
FindAndMakeBaseConstant(MinValItr, I);
|
||||||
// Start a new base constant search.
|
// Start a new base constant search.
|
||||||
MinValItr = CC;
|
MinValItr = I;
|
||||||
}
|
}
|
||||||
// Finalize the last base constant search.
|
// Finalize the last base constant search.
|
||||||
findAndMakeBaseConstant(MinValItr, ConstCandVec.end());
|
FindAndMakeBaseConstant(MinValItr, ConstantMap.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \brief Records the basic block of the instruction or all basic blocks of the
|
||||||
|
/// users of the constant expression.
|
||||||
|
static void CollectBasicBlocks(SmallPtrSet<BasicBlock *, 4> &BBs, Function &F,
|
||||||
|
User *U) {
|
||||||
|
if (Instruction *I = dyn_cast<Instruction>(U))
|
||||||
|
BBs.insert(I->getParent());
|
||||||
|
else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U))
|
||||||
|
// Find all users of this constant expression.
|
||||||
|
for (User *UU : CE->users())
|
||||||
|
// Only record users that are instructions. We don't want to go down a
|
||||||
|
// nested constant expression chain. Also check if the instruction is even
|
||||||
|
// in the current function.
|
||||||
|
if (Instruction *I = dyn_cast<Instruction>(UU))
|
||||||
|
if(I->getParent()->getParent() == &F)
|
||||||
|
BBs.insert(I->getParent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \brief Find the instruction we should insert the constant materialization
|
||||||
|
/// before.
|
||||||
|
static Instruction *getMatInsertPt(Instruction *I, const DominatorTree *DT) {
|
||||||
|
if (!isa<PHINode>(I) && !isa<LandingPadInst>(I)) // Simple case.
|
||||||
|
return I;
|
||||||
|
|
||||||
|
// We can't insert directly before a phi node or landing pad. Insert before
|
||||||
|
// the terminator of the dominating block.
|
||||||
|
assert(&I->getParent()->getParent()->getEntryBlock() != I->getParent() &&
|
||||||
|
"PHI or landing pad in entry block!");
|
||||||
|
BasicBlock *IDom = DT->getNode(I->getParent())->getIDom()->getBlock();
|
||||||
|
return IDom->getTerminator();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \brief Find an insertion point that dominates all uses.
|
||||||
|
Instruction *ConstantHoisting::
|
||||||
|
FindConstantInsertionPoint(Function &F, const ConstantInfo &CI) const {
|
||||||
|
BasicBlock *Entry = &F.getEntryBlock();
|
||||||
|
|
||||||
|
// Collect all basic blocks.
|
||||||
|
SmallPtrSet<BasicBlock *, 4> BBs;
|
||||||
|
ConstantInfo::RebasedConstantListType::const_iterator RCI, RCE;
|
||||||
|
for (RCI = CI.RebasedConstants.begin(), RCE = CI.RebasedConstants.end();
|
||||||
|
RCI != RCE; ++RCI)
|
||||||
|
for (SmallVectorImpl<User *>::const_iterator U = RCI->Uses.begin(),
|
||||||
|
E = RCI->Uses.end(); U != E; ++U)
|
||||||
|
CollectBasicBlocks(BBs, F, *U);
|
||||||
|
|
||||||
|
if (BBs.count(Entry))
|
||||||
|
return getMatInsertPt(&Entry->front(), DT);
|
||||||
|
|
||||||
|
while (BBs.size() >= 2) {
|
||||||
|
BasicBlock *BB, *BB1, *BB2;
|
||||||
|
BB1 = *BBs.begin();
|
||||||
|
BB2 = *std::next(BBs.begin());
|
||||||
|
BB = DT->findNearestCommonDominator(BB1, BB2);
|
||||||
|
if (BB == Entry)
|
||||||
|
return getMatInsertPt(&Entry->front(), DT);
|
||||||
|
BBs.erase(BB1);
|
||||||
|
BBs.erase(BB2);
|
||||||
|
BBs.insert(BB);
|
||||||
|
}
|
||||||
|
assert((BBs.size() == 1) && "Expected only one element.");
|
||||||
|
Instruction &FirstInst = (*BBs.begin())->front();
|
||||||
|
return getMatInsertPt(&FirstInst, DT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Emit materialization code for all rebased constants and update their
|
/// \brief Emit materialization code for all rebased constants and update their
|
||||||
/// users.
|
/// users.
|
||||||
void ConstantHoisting::emitBaseConstants(Instruction *Base, Constant *Offset,
|
void ConstantHoisting::EmitBaseConstants(Function &F, User *U,
|
||||||
const ConstantUser &CU) {
|
Instruction *Base, Constant *Offset,
|
||||||
Instruction *Mat = Base;
|
ConstantInt *OriginalConstant) {
|
||||||
if (Offset) {
|
if (Instruction *I = dyn_cast<Instruction>(U)) {
|
||||||
Instruction *InsertionPt = findMatInsertPt(CU.Inst, CU.OpndIdx);
|
Instruction *Mat = Base;
|
||||||
Mat = BinaryOperator::Create(Instruction::Add, Base, Offset,
|
if (!Offset->isNullValue()) {
|
||||||
"const_mat", InsertionPt);
|
Mat = BinaryOperator::Create(Instruction::Add, Base, Offset,
|
||||||
|
"const_mat", getMatInsertPt(I, DT));
|
||||||
|
|
||||||
DEBUG(dbgs() << "Materialize constant (" << *Base->getOperand(0)
|
// Use the same debug location as the instruction we are about to update.
|
||||||
<< " + " << *Offset << ") in BB "
|
Mat->setDebugLoc(I->getDebugLoc());
|
||||||
<< Mat->getParent()->getName() << '\n' << *Mat << '\n');
|
|
||||||
Mat->setDebugLoc(CU.Inst->getDebugLoc());
|
|
||||||
}
|
|
||||||
Value *Opnd = CU.Inst->getOperand(CU.OpndIdx);
|
|
||||||
|
|
||||||
// Visit constant integer.
|
DEBUG(dbgs() << "Materialize constant (" << *Base->getOperand(0)
|
||||||
if (isa<ConstantInt>(Opnd)) {
|
<< " + " << *Offset << ") in BB "
|
||||||
DEBUG(dbgs() << "Update: " << *CU.Inst << '\n');
|
<< I->getParent()->getName() << '\n' << *Mat << '\n');
|
||||||
CU.Inst->setOperand(CU.OpndIdx, Mat);
|
|
||||||
DEBUG(dbgs() << "To : " << *CU.Inst << '\n');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Visit cast instruction.
|
|
||||||
if (auto CastInst = dyn_cast<Instruction>(Opnd)) {
|
|
||||||
assert(CastInst->isCast() && "Expected an cast instruction!");
|
|
||||||
// Check if we already have visited this cast instruction before to avoid
|
|
||||||
// unnecessary cloning.
|
|
||||||
Instruction *&ClonedCastInst = ClonedCastMap[CastInst];
|
|
||||||
if (!ClonedCastInst) {
|
|
||||||
ClonedCastInst = CastInst->clone();
|
|
||||||
ClonedCastInst->setOperand(0, Mat);
|
|
||||||
ClonedCastInst->insertAfter(CastInst);
|
|
||||||
// Use the same debug location as the original cast instruction.
|
|
||||||
ClonedCastInst->setDebugLoc(CastInst->getDebugLoc());
|
|
||||||
DEBUG(dbgs() << "Clone instruction: " << *ClonedCastInst << '\n'
|
|
||||||
<< "To : " << *CastInst << '\n');
|
|
||||||
}
|
}
|
||||||
|
DEBUG(dbgs() << "Update: " << *I << '\n');
|
||||||
DEBUG(dbgs() << "Update: " << *CU.Inst << '\n');
|
I->replaceUsesOfWith(OriginalConstant, Mat);
|
||||||
CU.Inst->setOperand(CU.OpndIdx, ClonedCastInst);
|
DEBUG(dbgs() << "To: " << *I << '\n');
|
||||||
DEBUG(dbgs() << "To : " << *CU.Inst << '\n');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
assert(isa<ConstantExpr>(U) && "Expected a ConstantExpr.");
|
||||||
|
ConstantExpr *CE = cast<ConstantExpr>(U);
|
||||||
|
SmallVector<std::pair<Instruction *, Instruction *>, 8> WorkList;
|
||||||
|
DEBUG(dbgs() << "Visit ConstantExpr " << *CE << '\n');
|
||||||
|
for (User *UU : CE->users()) {
|
||||||
|
DEBUG(dbgs() << "Check user "; UU->print(dbgs()); dbgs() << '\n');
|
||||||
|
// We only handel instructions here and won't walk down a ConstantExpr chain
|
||||||
|
// to replace all ConstExpr with instructions.
|
||||||
|
if (Instruction *I = dyn_cast<Instruction>(UU)) {
|
||||||
|
// Only update constant expressions in the current function.
|
||||||
|
if (I->getParent()->getParent() != &F) {
|
||||||
|
DEBUG(dbgs() << "Not in the same function - skip.\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Visit constant expression.
|
Instruction *Mat = Base;
|
||||||
if (auto ConstExpr = dyn_cast<ConstantExpr>(Opnd)) {
|
Instruction *InsertBefore = getMatInsertPt(I, DT);
|
||||||
Instruction *ConstExprInst = ConstExpr->getAsInstruction();
|
if (!Offset->isNullValue()) {
|
||||||
ConstExprInst->setOperand(0, Mat);
|
Mat = BinaryOperator::Create(Instruction::Add, Base, Offset,
|
||||||
ConstExprInst->insertBefore(findMatInsertPt(CU.Inst, CU.OpndIdx));
|
"const_mat", InsertBefore);
|
||||||
|
|
||||||
// Use the same debug location as the instruction we are about to update.
|
// Use the same debug location as the instruction we are about to
|
||||||
ConstExprInst->setDebugLoc(CU.Inst->getDebugLoc());
|
// update.
|
||||||
|
Mat->setDebugLoc(I->getDebugLoc());
|
||||||
|
|
||||||
DEBUG(dbgs() << "Create instruction: " << *ConstExprInst << '\n'
|
DEBUG(dbgs() << "Materialize constant (" << *Base->getOperand(0)
|
||||||
<< "From : " << *ConstExpr << '\n');
|
<< " + " << *Offset << ") in BB "
|
||||||
DEBUG(dbgs() << "Update: " << *CU.Inst << '\n');
|
<< I->getParent()->getName() << '\n' << *Mat << '\n');
|
||||||
CU.Inst->setOperand(CU.OpndIdx, ConstExprInst);
|
}
|
||||||
DEBUG(dbgs() << "To : " << *CU.Inst << '\n');
|
Instruction *ICE = CE->getAsInstruction();
|
||||||
return;
|
ICE->replaceUsesOfWith(OriginalConstant, Mat);
|
||||||
|
ICE->insertBefore(InsertBefore);
|
||||||
|
|
||||||
|
// Use the same debug location as the instruction we are about to update.
|
||||||
|
ICE->setDebugLoc(I->getDebugLoc());
|
||||||
|
|
||||||
|
WorkList.push_back(std::make_pair(I, ICE));
|
||||||
|
} else {
|
||||||
|
DEBUG(dbgs() << "Not an instruction - skip.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SmallVectorImpl<std::pair<Instruction *, Instruction *> >::iterator I, E;
|
||||||
|
for (I = WorkList.begin(), E = WorkList.end(); I != E; ++I) {
|
||||||
|
DEBUG(dbgs() << "Create instruction: " << *I->second << '\n');
|
||||||
|
DEBUG(dbgs() << "Update: " << *I->first << '\n');
|
||||||
|
I->first->replaceUsesOfWith(CE, I->second);
|
||||||
|
DEBUG(dbgs() << "To: " << *I->first << '\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Hoist and hide the base constant behind a bitcast and emit
|
/// \brief Hoist and hide the base constant behind a bitcast and emit
|
||||||
/// materialization code for derived constants.
|
/// materialization code for derived constants.
|
||||||
bool ConstantHoisting::emitBaseConstants() {
|
bool ConstantHoisting::EmitBaseConstants(Function &F) {
|
||||||
bool MadeChange = false;
|
bool MadeChange = false;
|
||||||
for (auto const &ConstInfo : ConstantVec) {
|
SmallVectorImpl<ConstantInfo>::iterator CI, CE;
|
||||||
|
for (CI = Constants.begin(), CE = Constants.end(); CI != CE; ++CI) {
|
||||||
// Hoist and hide the base constant behind a bitcast.
|
// Hoist and hide the base constant behind a bitcast.
|
||||||
Instruction *IP = findConstantInsertionPoint(ConstInfo);
|
Instruction *IP = FindConstantInsertionPoint(F, *CI);
|
||||||
IntegerType *Ty = ConstInfo.BaseConstant->getType();
|
IntegerType *Ty = CI->BaseConstant->getType();
|
||||||
Instruction *Base =
|
Instruction *Base = new BitCastInst(CI->BaseConstant, Ty, "const", IP);
|
||||||
new BitCastInst(ConstInfo.BaseConstant, Ty, "const", IP);
|
DEBUG(dbgs() << "Hoist constant (" << *CI->BaseConstant << ") to BB "
|
||||||
DEBUG(dbgs() << "Hoist constant (" << *ConstInfo.BaseConstant << ") to BB "
|
<< IP->getParent()->getName() << '\n');
|
||||||
<< IP->getParent()->getName() << '\n' << *Base << '\n');
|
|
||||||
NumConstantsHoisted++;
|
NumConstantsHoisted++;
|
||||||
|
|
||||||
// Emit materialization code for all rebased constants.
|
// Emit materialization code for all rebased constants.
|
||||||
for (auto const &RCI : ConstInfo.RebasedConstants) {
|
ConstantInfo::RebasedConstantListType::iterator RCI, RCE;
|
||||||
|
for (RCI = CI->RebasedConstants.begin(), RCE = CI->RebasedConstants.end();
|
||||||
|
RCI != RCE; ++RCI) {
|
||||||
NumConstantsRebased++;
|
NumConstantsRebased++;
|
||||||
for (auto const &U : RCI.Uses)
|
for (SmallVectorImpl<User *>::iterator U = RCI->Uses.begin(),
|
||||||
emitBaseConstants(Base, RCI.Offset, U);
|
E = RCI->Uses.end(); U != E; ++U)
|
||||||
|
EmitBaseConstants(F, *U, Base, RCI->Offset, RCI->OriginalConstant);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the same debug location as the last user of the constant.
|
// Use the same debug location as the last user of the constant.
|
||||||
|
@ -554,37 +432,27 @@ bool ConstantHoisting::emitBaseConstants() {
|
||||||
return MadeChange;
|
return MadeChange;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Check all cast instructions we made a copy of and remove them if they
|
|
||||||
/// have no more users.
|
|
||||||
void ConstantHoisting::deleteDeadCastInst() const {
|
|
||||||
for (auto const &I : ClonedCastMap)
|
|
||||||
if (I.first->use_empty())
|
|
||||||
I.first->removeFromParent();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief Optimize expensive integer constants in the given function.
|
/// \brief Optimize expensive integer constants in the given function.
|
||||||
bool ConstantHoisting::optimizeConstants(Function &Fn) {
|
bool ConstantHoisting::OptimizeConstants(Function &F) {
|
||||||
// Collect all constant candidates.
|
bool MadeChange = false;
|
||||||
collectConstantCandidates(Fn);
|
|
||||||
|
|
||||||
// There are no constant candidates to worry about.
|
// Collect all constant candidates.
|
||||||
if (ConstCandVec.empty())
|
CollectConstants(F);
|
||||||
return false;
|
|
||||||
|
// There are no constants to worry about.
|
||||||
|
if (ConstantMap.empty())
|
||||||
|
return MadeChange;
|
||||||
|
|
||||||
// Combine constants that can be easily materialized with an add from a common
|
// Combine constants that can be easily materialized with an add from a common
|
||||||
// base constant.
|
// base constant.
|
||||||
findBaseConstants();
|
FindBaseConstants();
|
||||||
|
|
||||||
// There are no constants to emit.
|
|
||||||
if (ConstantVec.empty())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Finally hoist the base constant and emit materializating code for dependent
|
// Finally hoist the base constant and emit materializating code for dependent
|
||||||
// constants.
|
// constants.
|
||||||
bool MadeChange = emitBaseConstants();
|
MadeChange |= EmitBaseConstants(F);
|
||||||
|
|
||||||
// Cleanup dead instructions.
|
ConstantMap.clear();
|
||||||
deleteDeadCastInst();
|
Constants.clear();
|
||||||
|
|
||||||
return MadeChange;
|
return MadeChange;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,24 +3,26 @@
|
||||||
; The inner loop should require only one add (and no leas either).
|
; The inner loop should require only one add (and no leas either).
|
||||||
; rdar://8100380
|
; rdar://8100380
|
||||||
|
|
||||||
; CHECK: BB0_2:
|
; CHECK: BB0_3:
|
||||||
; CHECK-NEXT: movb $0, flags(%rcx)
|
; CHECK-NEXT: movb $0, flags(%rdx)
|
||||||
; CHECK-NEXT: addq %rax, %rcx
|
; CHECK-NEXT: addq %rax, %rdx
|
||||||
; CHECK-NEXT: cmpq $8192, %rcx
|
; CHECK-NEXT: cmpq $8192, %rdx
|
||||||
; CHECK-NEXT: jl
|
; CHECK-NEXT: jl
|
||||||
|
|
||||||
@flags = external global [8192 x i8], align 16 ; <[8192 x i8]*> [#uses=1]
|
@flags = external global [8192 x i8], align 16 ; <[8192 x i8]*> [#uses=1]
|
||||||
|
|
||||||
define void @foo() nounwind {
|
define void @foo() nounwind {
|
||||||
entry:
|
entry:
|
||||||
br label %bb
|
%tmp = icmp slt i64 2, 8192 ; <i1> [#uses=1]
|
||||||
|
br i1 %tmp, label %bb, label %bb21
|
||||||
|
|
||||||
bb: ; preds = %entry
|
bb: ; preds = %entry
|
||||||
br label %bb7
|
br label %bb7
|
||||||
|
|
||||||
bb7: ; preds = %bb, %bb17
|
bb7: ; preds = %bb, %bb17
|
||||||
%tmp8 = phi i64 [ %tmp18, %bb17 ], [ 2, %bb ] ; <i64> [#uses=2]
|
%tmp8 = phi i64 [ %tmp18, %bb17 ], [ 2, %bb ] ; <i64> [#uses=2]
|
||||||
br label %bb10
|
%tmp9 = icmp slt i64 2, 8192 ; <i1> [#uses=1]
|
||||||
|
br i1 %tmp9, label %bb10, label %bb17
|
||||||
|
|
||||||
bb10: ; preds = %bb7
|
bb10: ; preds = %bb7
|
||||||
br label %bb11
|
br label %bb11
|
||||||
|
|
|
@ -827,7 +827,9 @@ declare void @_ZN11MatrixTools9transposeI11FixedMatrixIdLi6ELi6ELi0ELi0EEEENT_13
|
||||||
declare void @_ZN21HNodeTranslateRotate311toCartesianEv(%struct.HNodeTranslateRotate3*)
|
declare void @_ZN21HNodeTranslateRotate311toCartesianEv(%struct.HNodeTranslateRotate3*)
|
||||||
|
|
||||||
define linkonce void @_ZN21HNodeTranslateRotate36setVelERK9CDSVectorIdLi1EN3CDS12DefaultAllocEE(%struct.HNodeTranslateRotate3* %this, %"struct.CDSVector<double,0,CDS::DefaultAlloc>"* %velv) {
|
define linkonce void @_ZN21HNodeTranslateRotate36setVelERK9CDSVectorIdLi1EN3CDS12DefaultAllocEE(%struct.HNodeTranslateRotate3* %this, %"struct.CDSVector<double,0,CDS::DefaultAlloc>"* %velv) {
|
||||||
%1 = getelementptr double* null, i32 -1 ; <double*> [#uses=1]
|
entry:
|
||||||
|
%0 = add i32 0, -1 ; <i32> [#uses=1]
|
||||||
|
%1 = getelementptr double* null, i32 %0 ; <double*> [#uses=1]
|
||||||
%2 = load double* %1, align 8 ; <double> [#uses=1]
|
%2 = load double* %1, align 8 ; <double> [#uses=1]
|
||||||
%3 = load double* null, align 8 ; <double> [#uses=2]
|
%3 = load double* null, align 8 ; <double> [#uses=2]
|
||||||
%4 = load double* null, align 8 ; <double> [#uses=2]
|
%4 = load double* null, align 8 ; <double> [#uses=2]
|
||||||
|
@ -888,12 +890,13 @@ define linkonce void @_ZN21HNodeTranslateRotate36setVelERK9CDSVectorIdLi1EN3CDS1
|
||||||
store double %52, double* %55, align 8
|
store double %52, double* %55, align 8
|
||||||
%56 = getelementptr %struct.HNodeTranslateRotate3* %this, i32 0, i32 0, i32 10, i32 0, i32 0, i32 2 ; <double*> [#uses=1]
|
%56 = getelementptr %struct.HNodeTranslateRotate3* %this, i32 0, i32 0, i32 10, i32 0, i32 0, i32 2 ; <double*> [#uses=1]
|
||||||
store double %53, double* %56, align 8
|
store double %53, double* %56, align 8
|
||||||
%57 = getelementptr %"struct.SubVector<CDSVector<double, 1, CDS::DefaultAlloc> >"* null, i32 0, i32 0 ; <%"struct.CDSVector<double,0,CDS::DefaultAlloc>"**> [#uses=1]
|
%57 = add i32 0, 4 ; <i32> [#uses=1]
|
||||||
store %"struct.CDSVector<double,0,CDS::DefaultAlloc>"* %velv, %"struct.CDSVector<double,0,CDS::DefaultAlloc>"** %57, align 8
|
%58 = getelementptr %"struct.SubVector<CDSVector<double, 1, CDS::DefaultAlloc> >"* null, i32 0, i32 0 ; <%"struct.CDSVector<double,0,CDS::DefaultAlloc>"**> [#uses=1]
|
||||||
%58 = getelementptr %"struct.SubVector<CDSVector<double, 1, CDS::DefaultAlloc> >"* null, i32 0, i32 1 ; <i32*> [#uses=1]
|
store %"struct.CDSVector<double,0,CDS::DefaultAlloc>"* %velv, %"struct.CDSVector<double,0,CDS::DefaultAlloc>"** %58, align 8
|
||||||
store i32 4, i32* %58, align 4
|
%59 = getelementptr %"struct.SubVector<CDSVector<double, 1, CDS::DefaultAlloc> >"* null, i32 0, i32 1 ; <i32*> [#uses=1]
|
||||||
%59 = getelementptr %"struct.SubVector<CDSVector<double, 1, CDS::DefaultAlloc> >"* null, i32 0, i32 2 ; <i32*> [#uses=1]
|
store i32 %57, i32* %59, align 4
|
||||||
store i32 3, i32* %59, align 8
|
%60 = getelementptr %"struct.SubVector<CDSVector<double, 1, CDS::DefaultAlloc> >"* null, i32 0, i32 2 ; <i32*> [#uses=1]
|
||||||
|
store i32 3, i32* %60, align 8
|
||||||
unreachable
|
unreachable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,11 +19,11 @@ return:
|
||||||
ret i8* %retval.0
|
ret i8* %retval.0
|
||||||
|
|
||||||
; CHECK-LABEL: @test1
|
; CHECK-LABEL: @test1
|
||||||
; CHECK: if.end:
|
; CHECK: entry:
|
||||||
; CHECK: %2 = inttoptr i64 %const to i8*
|
; CHECK: %const_mat = add i64 %const, 1
|
||||||
; CHECK-NEXT: br
|
; CHECK-NEXT: %1 = inttoptr i64 %const_mat to i8*
|
||||||
; CHECK: return:
|
; CHECK-NEXT: br i1 %cmp
|
||||||
; CHECK-NEXT: %retval.0 = phi i8* [ null, %entry ], [ %2, %if.end ]
|
; CHECK: %retval.0 = phi i8* [ null, %entry ], [ %1, %if.end ]
|
||||||
}
|
}
|
||||||
|
|
||||||
define void @test2(i1 %cmp, i64** %tmp) {
|
define void @test2(i1 %cmp, i64** %tmp) {
|
||||||
|
|
Loading…
Reference in New Issue