diff options
author | Juergen Ributzka <juergen@apple.com> | 2014-01-24 18:23:08 +0000 |
---|---|---|
committer | Juergen Ributzka <juergen@apple.com> | 2014-01-24 18:23:08 +0000 |
commit | fb282c68b7a31e8ee2810c81f5a6ed06e77cf7d1 (patch) | |
tree | 6b63d9b1a0642aff1c8bc52a62876348e27588bb /include | |
parent | 8346f147ab6f06be4dac4af5c0e451a22bccf475 (diff) | |
download | llvm-fb282c68b7a31e8ee2810c81f5a6ed06e77cf7d1.tar.gz llvm-fb282c68b7a31e8ee2810c81f5a6ed06e77cf7d1.tar.bz2 llvm-fb282c68b7a31e8ee2810c81f5a6ed06e77cf7d1.tar.xz |
Add Constant Hoisting Pass
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*)
Reviewed by Eric
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@200022 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-rw-r--r-- | include/llvm/Analysis/TargetTransformInfo.h | 8 | ||||
-rw-r--r-- | include/llvm/CodeGen/SelectionDAG.h | 22 | ||||
-rw-r--r-- | include/llvm/CodeGen/SelectionDAGNodes.h | 5 | ||||
-rw-r--r-- | include/llvm/InitializePasses.h | 1 | ||||
-rw-r--r-- | include/llvm/LinkAllPasses.h | 1 | ||||
-rw-r--r-- | include/llvm/Transforms/Scalar.h | 6 |
6 files changed, 33 insertions, 10 deletions
diff --git a/include/llvm/Analysis/TargetTransformInfo.h b/include/llvm/Analysis/TargetTransformInfo.h index a8975b7a4f..f2613e35c5 100644 --- a/include/llvm/Analysis/TargetTransformInfo.h +++ b/include/llvm/Analysis/TargetTransformInfo.h @@ -92,6 +92,7 @@ public: enum TargetCostConstants { TCC_Free = 0, ///< Expected to fold away in lowering. TCC_Basic = 1, ///< The cost of a typical 'add' instruction. + TCC_Load = 3, TCC_Expensive = 4 ///< The cost of a 'div' instruction on x86. }; @@ -299,6 +300,13 @@ public: /// immediate of the specified type. virtual unsigned getIntImmCost(const APInt &Imm, Type *Ty) const; + /// \brief Return the expected cost of materialization for the given integer + /// immediate of the specified type for a given instruction. The cost can be + /// zero if the immediate can be folded into the specified instruction. + virtual unsigned getIntImmCost(unsigned Opcode, const APInt &Imm, + Type *Ty) const; + virtual unsigned getIntImmCost(Intrinsic::ID IID, const APInt &Imm, + Type *Ty) const; /// @} /// \name Vector Target Information diff --git a/include/llvm/CodeGen/SelectionDAG.h b/include/llvm/CodeGen/SelectionDAG.h index 82becca315..523c81337c 100644 --- a/include/llvm/CodeGen/SelectionDAG.h +++ b/include/llvm/CodeGen/SelectionDAG.h @@ -401,18 +401,22 @@ public: //===--------------------------------------------------------------------===// // Node creation methods. // - SDValue getConstant(uint64_t Val, EVT VT, bool isTarget = false); - SDValue getConstant(const APInt &Val, EVT VT, bool isTarget = false); - SDValue getConstant(const ConstantInt &Val, EVT VT, bool isTarget = false); + SDValue getConstant(uint64_t Val, EVT VT, bool isTarget = false, + bool isOpaque = false); + SDValue getConstant(const APInt &Val, EVT VT, bool isTarget = false, + bool isOpaque = false); + SDValue getConstant(const ConstantInt &Val, EVT VT, bool isTarget = false, + bool isOpaque = false); SDValue getIntPtrConstant(uint64_t Val, bool isTarget = false); - SDValue getTargetConstant(uint64_t Val, EVT VT) { - return getConstant(Val, VT, true); + SDValue getTargetConstant(uint64_t Val, EVT VT, bool isOpaque = false) { + return getConstant(Val, VT, true, isOpaque); } - SDValue getTargetConstant(const APInt &Val, EVT VT) { - return getConstant(Val, VT, true); + SDValue getTargetConstant(const APInt &Val, EVT VT, bool isOpaque = false) { + return getConstant(Val, VT, true, isOpaque); } - SDValue getTargetConstant(const ConstantInt &Val, EVT VT) { - return getConstant(Val, VT, true); + SDValue getTargetConstant(const ConstantInt &Val, EVT VT, + bool isOpaque = false) { + return getConstant(Val, VT, true, isOpaque); } // The forms below that take a double should only be used for simple // constants that can be exactly represented in VT. No checks are made. diff --git a/include/llvm/CodeGen/SelectionDAGNodes.h b/include/llvm/CodeGen/SelectionDAGNodes.h index 00773b3e66..976d212ce3 100644 --- a/include/llvm/CodeGen/SelectionDAGNodes.h +++ b/include/llvm/CodeGen/SelectionDAGNodes.h @@ -1250,9 +1250,10 @@ public: class ConstantSDNode : public SDNode { const ConstantInt *Value; friend class SelectionDAG; - ConstantSDNode(bool isTarget, const ConstantInt *val, EVT VT) + ConstantSDNode(bool isTarget, bool isOpaque, const ConstantInt *val, EVT VT) : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, 0, DebugLoc(), getSDVTList(VT)), Value(val) { + SubclassData |= isOpaque; } public: @@ -1265,6 +1266,8 @@ public: bool isNullValue() const { return Value->isNullValue(); } bool isAllOnesValue() const { return Value->isAllOnesValue(); } + bool isOpaque() const { return SubclassData & 1; } + static bool classof(const SDNode *N) { return N->getOpcode() == ISD::Constant || N->getOpcode() == ISD::TargetConstant; diff --git a/include/llvm/InitializePasses.h b/include/llvm/InitializePasses.h index 36efee570b..923571ea4b 100644 --- a/include/llvm/InitializePasses.h +++ b/include/llvm/InitializePasses.h @@ -90,6 +90,7 @@ void initializeCFGSimplifyPassPass(PassRegistry&); void initializeFlattenCFGPassPass(PassRegistry&); void initializeStructurizeCFGPass(PassRegistry&); void initializeCFGViewerPass(PassRegistry&); +void initializeConstantHoistingPass(PassRegistry&); void initializeCodeGenPreparePass(PassRegistry&); void initializeConstantMergePass(PassRegistry&); void initializeConstantPropagationPass(PassRegistry&); diff --git a/include/llvm/LinkAllPasses.h b/include/llvm/LinkAllPasses.h index e1d788e6e0..16032504cb 100644 --- a/include/llvm/LinkAllPasses.h +++ b/include/llvm/LinkAllPasses.h @@ -129,6 +129,7 @@ namespace { (void) llvm::createJumpThreadingPass(); (void) llvm::createUnifyFunctionExitNodesPass(); (void) llvm::createInstCountPass(); + (void) llvm::createConstantHoistingPass(); (void) llvm::createCodeGenPreparePass(); (void) llvm::createEarlyCSEPass(); (void) llvm::createGVNPass(); diff --git a/include/llvm/Transforms/Scalar.h b/include/llvm/Transforms/Scalar.h index 6f9e1245e5..e1890f6803 100644 --- a/include/llvm/Transforms/Scalar.h +++ b/include/llvm/Transforms/Scalar.h @@ -312,6 +312,12 @@ Pass *createLoopDeletionPass(); //===----------------------------------------------------------------------===// // +// ConstantHoisting - This pass prepares a function for expensive constants. +// +FunctionPass *createConstantHoistingPass(); + +//===----------------------------------------------------------------------===// +// // CodeGenPrepare - This pass prepares a function for instruction selection. // FunctionPass *createCodeGenPreparePass(const TargetMachine *TM = 0); |