2014-01-25 02:02:55 +00:00
|
|
|
//===- ConstantHoisting.cpp - Prepare code for expensive constants --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass identifies expensive constants to hoist and coalesces them to
|
|
|
|
// better prepare it for SelectionDAG-based code generation. This works around
|
|
|
|
// the limitations of the basic-block-at-a-time approach.
|
|
|
|
//
|
|
|
|
// First it scans all instructions for integer constants and calculates its
|
|
|
|
// cost. If the constant can be folded into the instruction (the cost is
|
|
|
|
// TCC_Free) or the cost is just a simple operation (TCC_BASIC), then we don't
|
|
|
|
// consider it expensive and leave it alone. This is the default behavior and
|
|
|
|
// the default implementation of getIntImmCost will always return TCC_Free.
|
|
|
|
//
|
|
|
|
// If the cost is more than TCC_BASIC, then the integer constant can't be folded
|
|
|
|
// into the instruction and it might be beneficial to hoist the constant.
|
|
|
|
// Similar constants are coalesced to reduce register pressure and
|
|
|
|
// materialization code.
|
|
|
|
//
|
|
|
|
// When a constant is hoisted, it is also hidden behind a bitcast to force it to
|
|
|
|
// be live-out of the basic block. Otherwise the constant would be just
|
|
|
|
// duplicated and each basic block would have its own copy in the SelectionDAG.
|
|
|
|
// The SelectionDAG recognizes such constants as opaque and doesn't perform
|
|
|
|
// certain transformations on them, which would create a new expensive constant.
|
|
|
|
//
|
|
|
|
// This optimization is only applied to integer constants in instructions and
|
|
|
|
// simple (this means not nested) constant cast experessions. For example:
|
|
|
|
// %0 = load i64* inttoptr (i64 big_constant to i64*)
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "consthoist"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2014-03-21 06:04:30 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2014-01-25 02:02:55 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
STATISTIC(NumConstantsHoisted, "Number of constants hoisted");
|
|
|
|
STATISTIC(NumConstantsRebased, "Number of constants rebased");
|
|
|
|
|
2014-03-20 20:17:13 +00:00
|
|
|
namespace {
|
|
|
|
typedef SmallVector<User *, 4> ConstantUseListType;
|
2014-01-25 02:02:55 +00:00
|
|
|
struct ConstantCandidate {
|
|
|
|
ConstantUseListType Uses;
|
2014-03-21 06:04:30 +00:00
|
|
|
ConstantInt *ConstInt;
|
|
|
|
unsigned CumulativeCost;
|
|
|
|
|
|
|
|
ConstantCandidate(ConstantInt *ConstInt)
|
|
|
|
: ConstInt(ConstInt), CumulativeCost(0) { }
|
2014-01-25 02:02:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ConstantInfo {
|
|
|
|
ConstantInt *BaseConstant;
|
2014-03-20 20:17:13 +00:00
|
|
|
struct RebasedConstantInfo {
|
|
|
|
ConstantInt *OriginalConstant;
|
|
|
|
Constant *Offset;
|
|
|
|
ConstantUseListType Uses;
|
|
|
|
};
|
|
|
|
typedef SmallVector<RebasedConstantInfo, 4> RebasedConstantListType;
|
2014-01-25 02:02:55 +00:00
|
|
|
RebasedConstantListType RebasedConstants;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ConstantHoisting : public FunctionPass {
|
2014-03-21 06:04:30 +00:00
|
|
|
typedef DenseMap<ConstantInt *, unsigned> ConstCandMapType;
|
|
|
|
typedef std::vector<ConstantCandidate> ConstCandVecType;
|
|
|
|
|
2014-01-25 02:02:55 +00:00
|
|
|
const TargetTransformInfo *TTI;
|
|
|
|
DominatorTree *DT;
|
|
|
|
|
2014-03-21 06:04:30 +00:00
|
|
|
/// Keeps track of constant candidates found in the function.
|
|
|
|
ConstCandMapType ConstCandMap;
|
|
|
|
ConstCandVecType ConstCandVec;
|
2014-01-25 02:02:55 +00:00
|
|
|
|
|
|
|
/// These are the final constants we decided to hoist.
|
2014-03-20 20:17:13 +00:00
|
|
|
SmallVector<ConstantInfo, 4> Constants;
|
2014-01-25 02:02:55 +00:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2014-03-20 20:17:13 +00:00
|
|
|
ConstantHoisting() : FunctionPass(ID), TTI(0) {
|
2014-01-25 02:02:55 +00:00
|
|
|
initializeConstantHoistingPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2014-03-20 20:17:13 +00:00
|
|
|
bool runOnFunction(Function &F) override;
|
2014-01-25 02:02:55 +00:00
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
const char *getPassName() const override { return "Constant Hoisting"; }
|
2014-01-25 02:02:55 +00:00
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-01-25 02:02:55 +00:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
|
|
|
AU.addRequired<TargetTransformInfo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-03-21 06:04:33 +00:00
|
|
|
void collectConstantCandidates(User *U, unsigned Opcode, Intrinsic::ID IID,
|
|
|
|
ConstantInt *C);
|
|
|
|
void collectConstantCandidates(Instruction *I);
|
|
|
|
void collectConstantCandidates(Function &F);
|
|
|
|
void findAndMakeBaseConstant(ConstCandVecType::iterator S,
|
2014-03-21 06:04:30 +00:00
|
|
|
ConstCandVecType::iterator E);
|
2014-03-21 06:04:33 +00:00
|
|
|
void findBaseConstants();
|
|
|
|
Instruction *findConstantInsertionPoint(Function &F,
|
2014-03-20 20:17:13 +00:00
|
|
|
const ConstantInfo &CI) const;
|
2014-03-21 06:04:33 +00:00
|
|
|
void emitBaseConstants(Function &F, User *U, Instruction *Base,
|
2014-03-20 20:17:13 +00:00
|
|
|
Constant *Offset, ConstantInt *OriginalConstant);
|
2014-03-21 06:04:33 +00:00
|
|
|
bool emitBaseConstants(Function &F);
|
|
|
|
bool optimizeConstants(Function &F);
|
2014-01-25 02:02:55 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char ConstantHoisting::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(ConstantHoisting, "consthoist", "Constant Hoisting",
|
|
|
|
false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
|
|
|
INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
|
|
|
|
INITIALIZE_PASS_END(ConstantHoisting, "consthoist", "Constant Hoisting",
|
|
|
|
false, false)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createConstantHoistingPass() {
|
|
|
|
return new ConstantHoisting();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Perform the constant hoisting optimization for the given function.
|
2014-03-20 20:17:13 +00:00
|
|
|
bool ConstantHoisting::runOnFunction(Function &F) {
|
|
|
|
DEBUG(dbgs() << "********** Constant Hoisting **********\n");
|
|
|
|
DEBUG(dbgs() << "********** Function: " << F.getName() << '\n');
|
2014-01-25 02:02:55 +00:00
|
|
|
|
2014-03-20 20:17:13 +00:00
|
|
|
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
|
|
|
TTI = &getAnalysis<TargetTransformInfo>();
|
2014-01-25 02:02:55 +00:00
|
|
|
|
2014-03-21 06:04:33 +00:00
|
|
|
return optimizeConstants(F);
|
2014-01-25 02:02:55 +00:00
|
|
|
}
|
|
|
|
|
2014-03-21 06:04:33 +00:00
|
|
|
void ConstantHoisting::collectConstantCandidates(User * U, unsigned Opcode,
|
|
|
|
Intrinsic::ID IID,
|
|
|
|
ConstantInt *C) {
|
2014-01-25 02:02:55 +00:00
|
|
|
unsigned Cost;
|
2014-03-20 20:17:13 +00:00
|
|
|
if (Opcode)
|
|
|
|
Cost = TTI->getIntImmCost(Opcode, C->getValue(), C->getType());
|
2014-01-25 02:02:55 +00:00
|
|
|
else
|
2014-03-20 20:17:13 +00:00
|
|
|
Cost = TTI->getIntImmCost(IID, C->getValue(), C->getType());
|
2014-01-25 02:02:55 +00:00
|
|
|
|
2014-03-21 06:04:30 +00:00
|
|
|
// Ignore cheap integer constants.
|
2014-01-25 02:02:55 +00:00
|
|
|
if (Cost > TargetTransformInfo::TCC_Basic) {
|
2014-03-21 06:04:30 +00:00
|
|
|
ConstCandMapType::iterator Itr;
|
|
|
|
bool Inserted;
|
|
|
|
std::tie(Itr, Inserted) = ConstCandMap.insert(std::make_pair(C, 0));
|
|
|
|
if (Inserted) {
|
|
|
|
ConstCandVec.push_back(ConstantCandidate(C));
|
|
|
|
Itr->second = ConstCandVec.size() - 1;
|
|
|
|
}
|
|
|
|
ConstantCandidate &CC = ConstCandVec[Itr->second];
|
2014-03-20 20:17:13 +00:00
|
|
|
CC.CumulativeCost += Cost;
|
|
|
|
CC.Uses.push_back(U);
|
|
|
|
DEBUG(dbgs() << "Collect constant " << *C << " with cost " << Cost
|
|
|
|
<< " from " << *U << '\n');
|
2014-01-25 02:02:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-20 20:17:13 +00:00
|
|
|
/// \brief Scan the instruction or constant expression for expensive integer
|
|
|
|
/// constants and record them in the constant map.
|
2014-03-21 06:04:33 +00:00
|
|
|
void ConstantHoisting::collectConstantCandidates(Instruction *I) {
|
2014-03-20 20:17:13 +00:00
|
|
|
unsigned Opcode = 0;
|
|
|
|
Intrinsic::ID IID = Intrinsic::not_intrinsic;
|
|
|
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
|
|
|
|
IID = II->getIntrinsicID();
|
|
|
|
else
|
|
|
|
Opcode = I->getOpcode();
|
2014-01-25 02:02:55 +00:00
|
|
|
|
|
|
|
// Scan all operands.
|
2014-03-20 20:17:13 +00:00
|
|
|
for (User::op_iterator O = I->op_begin(), E = I->op_end(); O != E; ++O) {
|
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(O)) {
|
2014-03-21 06:04:33 +00:00
|
|
|
collectConstantCandidates(I, Opcode, IID, C);
|
2014-01-25 02:02:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-03-20 20:17:13 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(O)) {
|
|
|
|
// We only handle constant cast expressions.
|
|
|
|
if (!CE->isCast())
|
2014-01-25 02:02:55 +00:00
|
|
|
continue;
|
|
|
|
|
2014-03-20 20:17:13 +00:00
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(CE->getOperand(0))) {
|
|
|
|
// Ignore the cast expression and use the opcode of the instruction.
|
2014-03-21 06:04:33 +00:00
|
|
|
collectConstantCandidates(CE, Opcode, IID, C);
|
2014-01-25 02:02:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-03-20 20:17:13 +00:00
|
|
|
}
|
2014-01-25 02:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Collect all integer constants in the function that cannot be folded
|
|
|
|
/// into an instruction itself.
|
2014-03-21 06:04:33 +00:00
|
|
|
void ConstantHoisting::collectConstantCandidates(Function &F) {
|
2014-03-20 20:17:13 +00:00
|
|
|
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
|
2014-03-21 06:04:33 +00:00
|
|
|
collectConstantCandidates(I);
|
2014-01-25 02:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Find the base constant within the given range and rebase all other
|
|
|
|
/// constants with respect to the base constant.
|
2014-03-21 06:04:33 +00:00
|
|
|
void ConstantHoisting::findAndMakeBaseConstant(ConstCandVecType::iterator S,
|
2014-03-21 06:04:30 +00:00
|
|
|
ConstCandVecType::iterator E) {
|
|
|
|
ConstCandVecType::iterator MaxCostItr = S;
|
2014-01-25 02:02:55 +00:00
|
|
|
unsigned NumUses = 0;
|
|
|
|
// Use the constant that has the maximum cost as base constant.
|
2014-03-21 06:04:30 +00:00
|
|
|
for (ConstCandVecType::iterator I = S; I != E; ++I) {
|
|
|
|
NumUses += I->Uses.size();
|
|
|
|
if (I->CumulativeCost > MaxCostItr->CumulativeCost)
|
2014-03-20 20:17:13 +00:00
|
|
|
MaxCostItr = I;
|
2014-01-25 02:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't hoist constants that have only one use.
|
|
|
|
if (NumUses <= 1)
|
|
|
|
return;
|
|
|
|
|
2014-03-20 20:17:13 +00:00
|
|
|
ConstantInfo CI;
|
2014-03-21 06:04:30 +00:00
|
|
|
CI.BaseConstant = MaxCostItr->ConstInt;
|
2014-03-20 20:17:13 +00:00
|
|
|
Type *Ty = CI.BaseConstant->getType();
|
2014-01-25 02:02:55 +00:00
|
|
|
// Rebase the constants with respect to the base constant.
|
2014-03-21 06:04:30 +00:00
|
|
|
for (ConstCandVecType::iterator I = S; I != E; ++I) {
|
|
|
|
APInt Diff = I->ConstInt->getValue() - CI.BaseConstant->getValue();
|
2014-03-20 20:17:13 +00:00
|
|
|
ConstantInfo::RebasedConstantInfo RCI;
|
2014-03-21 06:04:30 +00:00
|
|
|
RCI.OriginalConstant = I->ConstInt;
|
2014-03-20 20:17:13 +00:00
|
|
|
RCI.Offset = ConstantInt::get(Ty, Diff);
|
2014-03-21 06:04:30 +00:00
|
|
|
RCI.Uses = std::move(I->Uses);
|
2014-03-20 20:17:13 +00:00
|
|
|
CI.RebasedConstants.push_back(RCI);
|
2014-01-25 02:02:55 +00:00
|
|
|
}
|
2014-03-20 20:17:13 +00:00
|
|
|
Constants.push_back(CI);
|
2014-01-25 02:02:55 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 20:17:13 +00:00
|
|
|
/// \brief Finds and combines constants that can be easily rematerialized with
|
|
|
|
/// an add from a common base constant.
|
2014-03-21 06:04:33 +00:00
|
|
|
void ConstantHoisting::findBaseConstants() {
|
2014-03-20 20:17:13 +00:00
|
|
|
// Sort the constants by value and type. This invalidates the mapping.
|
2014-03-21 06:04:30 +00:00
|
|
|
std::sort(ConstCandVec.begin(), ConstCandVec.end(),
|
|
|
|
[](const ConstantCandidate &LHS, const ConstantCandidate &RHS) {
|
|
|
|
if (LHS.ConstInt->getType() != RHS.ConstInt->getType())
|
|
|
|
return LHS.ConstInt->getType()->getBitWidth() <
|
|
|
|
RHS.ConstInt->getType()->getBitWidth();
|
|
|
|
return LHS.ConstInt->getValue().ult(RHS.ConstInt->getValue());
|
2014-03-20 20:17:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Simple linear scan through the sorted constant map for viable merge
|
|
|
|
// candidates.
|
2014-03-21 06:04:30 +00:00
|
|
|
ConstCandVecType::iterator MinValItr = ConstCandVec.begin();
|
|
|
|
for (ConstCandVecType::iterator I = std::next(ConstCandVec.begin()),
|
|
|
|
E = ConstCandVec.end(); I != E; ++I) {
|
|
|
|
if (MinValItr->ConstInt->getType() == I->ConstInt->getType()) {
|
2014-01-25 02:02:55 +00:00
|
|
|
// Check if the constant is in range of an add with immediate.
|
2014-03-21 06:04:30 +00:00
|
|
|
APInt Diff = I->ConstInt->getValue() - MinValItr->ConstInt->getValue();
|
2014-01-25 02:02:55 +00:00
|
|
|
if ((Diff.getBitWidth() <= 64) &&
|
|
|
|
TTI->isLegalAddImmediate(Diff.getSExtValue()))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// We either have now a different constant type or the constant is not in
|
|
|
|
// range of an add with immediate anymore.
|
2014-03-21 06:04:33 +00:00
|
|
|
findAndMakeBaseConstant(MinValItr, I);
|
2014-01-25 02:02:55 +00:00
|
|
|
// Start a new base constant search.
|
2014-03-20 20:17:13 +00:00
|
|
|
MinValItr = I;
|
2014-01-25 02:02:55 +00:00
|
|
|
}
|
|
|
|
// Finalize the last base constant search.
|
2014-03-21 06:04:33 +00:00
|
|
|
findAndMakeBaseConstant(MinValItr, ConstCandVec.end());
|
2014-01-27 13:11:43 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 20:17:13 +00:00
|
|
|
/// \brief Records the basic block of the instruction or all basic blocks of the
|
|
|
|
/// users of the constant expression.
|
2014-03-21 06:04:33 +00:00
|
|
|
static void collectBasicBlocks(SmallPtrSet<BasicBlock *, 4> &BBs, Function &F,
|
2014-03-20 20:17:13 +00:00
|
|
|
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());
|
|
|
|
}
|
2014-03-20 19:55:52 +00:00
|
|
|
|
2014-03-20 20:17:13 +00:00
|
|
|
/// \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::
|
2014-03-21 06:04:33 +00:00
|
|
|
findConstantInsertionPoint(Function &F, const ConstantInfo &CI) const {
|
2014-03-20 20:17:13 +00:00
|
|
|
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)
|
2014-03-21 06:04:33 +00:00
|
|
|
collectBasicBlocks(BBs, F, *U);
|
2014-03-20 20:17:13 +00:00
|
|
|
|
|
|
|
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);
|
2014-01-25 02:02:55 +00:00
|
|
|
}
|
2014-03-20 20:17:13 +00:00
|
|
|
assert((BBs.size() == 1) && "Expected only one element.");
|
|
|
|
Instruction &FirstInst = (*BBs.begin())->front();
|
|
|
|
return getMatInsertPt(&FirstInst, DT);
|
|
|
|
}
|
2014-01-25 02:02:55 +00:00
|
|
|
|
2014-03-20 20:17:13 +00:00
|
|
|
/// \brief Emit materialization code for all rebased constants and update their
|
|
|
|
/// users.
|
2014-03-21 06:04:33 +00:00
|
|
|
void ConstantHoisting::emitBaseConstants(Function &F, User *U,
|
2014-03-20 20:17:13 +00:00
|
|
|
Instruction *Base, Constant *Offset,
|
|
|
|
ConstantInt *OriginalConstant) {
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(U)) {
|
|
|
|
Instruction *Mat = Base;
|
|
|
|
if (!Offset->isNullValue()) {
|
|
|
|
Mat = BinaryOperator::Create(Instruction::Add, Base, Offset,
|
|
|
|
"const_mat", getMatInsertPt(I, DT));
|
|
|
|
|
|
|
|
// Use the same debug location as the instruction we are about to update.
|
|
|
|
Mat->setDebugLoc(I->getDebugLoc());
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Materialize constant (" << *Base->getOperand(0)
|
|
|
|
<< " + " << *Offset << ") in BB "
|
|
|
|
<< I->getParent()->getName() << '\n' << *Mat << '\n');
|
2014-03-20 19:55:52 +00:00
|
|
|
}
|
2014-03-20 20:17:13 +00:00
|
|
|
DEBUG(dbgs() << "Update: " << *I << '\n');
|
|
|
|
I->replaceUsesOfWith(OriginalConstant, Mat);
|
|
|
|
DEBUG(dbgs() << "To: " << *I << '\n');
|
2014-03-20 19:55:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-03-20 20:17:13 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-01-25 02:02:55 +00:00
|
|
|
|
2014-03-20 20:17:13 +00:00
|
|
|
Instruction *Mat = Base;
|
|
|
|
Instruction *InsertBefore = getMatInsertPt(I, DT);
|
|
|
|
if (!Offset->isNullValue()) {
|
|
|
|
Mat = BinaryOperator::Create(Instruction::Add, Base, Offset,
|
|
|
|
"const_mat", InsertBefore);
|
2014-01-25 02:02:55 +00:00
|
|
|
|
2014-03-20 20:17:13 +00:00
|
|
|
// Use the same debug location as the instruction we are about to
|
|
|
|
// update.
|
|
|
|
Mat->setDebugLoc(I->getDebugLoc());
|
2014-01-25 02:02:55 +00:00
|
|
|
|
2014-03-20 20:17:13 +00:00
|
|
|
DEBUG(dbgs() << "Materialize constant (" << *Base->getOperand(0)
|
|
|
|
<< " + " << *Offset << ") in BB "
|
|
|
|
<< I->getParent()->getName() << '\n' << *Mat << '\n');
|
|
|
|
}
|
|
|
|
Instruction *ICE = CE->getAsInstruction();
|
|
|
|
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');
|
2014-02-08 00:20:45 +00:00
|
|
|
}
|
2014-01-25 02:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Hoist and hide the base constant behind a bitcast and emit
|
|
|
|
/// materialization code for derived constants.
|
2014-03-21 06:04:33 +00:00
|
|
|
bool ConstantHoisting::emitBaseConstants(Function &F) {
|
2014-01-25 02:02:55 +00:00
|
|
|
bool MadeChange = false;
|
2014-03-20 20:17:13 +00:00
|
|
|
SmallVectorImpl<ConstantInfo>::iterator CI, CE;
|
|
|
|
for (CI = Constants.begin(), CE = Constants.end(); CI != CE; ++CI) {
|
2014-01-25 02:02:55 +00:00
|
|
|
// Hoist and hide the base constant behind a bitcast.
|
2014-03-21 06:04:33 +00:00
|
|
|
Instruction *IP = findConstantInsertionPoint(F, *CI);
|
2014-03-20 20:17:13 +00:00
|
|
|
IntegerType *Ty = CI->BaseConstant->getType();
|
|
|
|
Instruction *Base = new BitCastInst(CI->BaseConstant, Ty, "const", IP);
|
|
|
|
DEBUG(dbgs() << "Hoist constant (" << *CI->BaseConstant << ") to BB "
|
|
|
|
<< IP->getParent()->getName() << '\n');
|
2014-01-25 02:02:55 +00:00
|
|
|
NumConstantsHoisted++;
|
|
|
|
|
|
|
|
// Emit materialization code for all rebased constants.
|
2014-03-20 20:17:13 +00:00
|
|
|
ConstantInfo::RebasedConstantListType::iterator RCI, RCE;
|
|
|
|
for (RCI = CI->RebasedConstants.begin(), RCE = CI->RebasedConstants.end();
|
|
|
|
RCI != RCE; ++RCI) {
|
2014-01-25 02:02:55 +00:00
|
|
|
NumConstantsRebased++;
|
2014-03-20 20:17:13 +00:00
|
|
|
for (SmallVectorImpl<User *>::iterator U = RCI->Uses.begin(),
|
|
|
|
E = RCI->Uses.end(); U != E; ++U)
|
2014-03-21 06:04:33 +00:00
|
|
|
emitBaseConstants(F, *U, Base, RCI->Offset, RCI->OriginalConstant);
|
2014-01-25 02:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use the same debug location as the last user of the constant.
|
|
|
|
assert(!Base->use_empty() && "The use list is empty!?");
|
2014-03-09 03:16:01 +00:00
|
|
|
assert(isa<Instruction>(Base->user_back()) &&
|
2014-01-25 02:02:55 +00:00
|
|
|
"All uses should be instructions.");
|
2014-03-09 03:16:01 +00:00
|
|
|
Base->setDebugLoc(cast<Instruction>(Base->user_back())->getDebugLoc());
|
2014-01-25 02:02:55 +00:00
|
|
|
|
|
|
|
// Correct for base constant, which we counted above too.
|
|
|
|
NumConstantsRebased--;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2014-03-20 19:55:52 +00:00
|
|
|
/// \brief Optimize expensive integer constants in the given function.
|
2014-03-21 06:04:33 +00:00
|
|
|
bool ConstantHoisting::optimizeConstants(Function &F) {
|
2014-03-20 20:17:13 +00:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
2014-01-25 02:02:55 +00:00
|
|
|
// Collect all constant candidates.
|
2014-03-21 06:04:33 +00:00
|
|
|
collectConstantCandidates(F);
|
2014-01-25 02:02:55 +00:00
|
|
|
|
2014-03-21 06:04:30 +00:00
|
|
|
// There are no constant candidates to worry about.
|
|
|
|
if (ConstCandVec.empty())
|
|
|
|
return false;
|
2014-01-25 02:02:55 +00:00
|
|
|
|
|
|
|
// Combine constants that can be easily materialized with an add from a common
|
|
|
|
// base constant.
|
2014-03-21 06:04:33 +00:00
|
|
|
findBaseConstants();
|
2014-01-25 02:02:55 +00:00
|
|
|
|
2014-02-25 04:21:15 +00:00
|
|
|
// Finally hoist the base constant and emit materializating code for dependent
|
2014-01-25 02:02:55 +00:00
|
|
|
// constants.
|
2014-03-21 06:04:33 +00:00
|
|
|
MadeChange |= emitBaseConstants(F);
|
2014-01-25 02:02:55 +00:00
|
|
|
|
2014-03-21 06:04:30 +00:00
|
|
|
ConstCandMap.clear();
|
|
|
|
ConstCandVec.clear();
|
2014-03-20 20:17:13 +00:00
|
|
|
Constants.clear();
|
2014-01-25 02:02:55 +00:00
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|