diff options
| author | Juergen Ributzka <juergen@apple.com> | 2014-03-21 06:04:45 +0000 |
|---|---|---|
| committer | Juergen Ributzka <juergen@apple.com> | 2014-03-21 06:04:45 +0000 |
| commit | f0dff49ad0869b3d8e7df9a40539132b7c88c721 (patch) | |
| tree | da9b9da726d4a35e95730426fc05028728c9c7a9 /llvm/lib | |
| parent | 500abd48d17772ad8c1505995b6619bedebc5403 (diff) | |
| download | bcm5719-llvm-f0dff49ad0869b3d8e7df9a40539132b7c88c721.tar.gz bcm5719-llvm-f0dff49ad0869b3d8e7df9a40539132b7c88c721.zip | |
[Constant Hoisting] Make the constant materialization cost operand dependent
Extend the target hook to take also the operand index into account when
calculating the cost of the constant materialization.
Related to <rdar://problem/16381500>
llvm-svn: 204435
Diffstat (limited to 'llvm/lib')
| -rw-r--r-- | llvm/lib/Analysis/TargetTransformInfo.cpp | 16 | ||||
| -rw-r--r-- | llvm/lib/Target/X86/X86TargetTransformInfo.cpp | 47 | ||||
| -rw-r--r-- | llvm/lib/Transforms/Scalar/ConstantHoisting.cpp | 14 |
3 files changed, 47 insertions, 30 deletions
diff --git a/llvm/lib/Analysis/TargetTransformInfo.cpp b/llvm/lib/Analysis/TargetTransformInfo.cpp index 0dcdd12a409..75d053c6891 100644 --- a/llvm/lib/Analysis/TargetTransformInfo.cpp +++ b/llvm/lib/Analysis/TargetTransformInfo.cpp @@ -148,14 +148,14 @@ unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const { return PrevTTI->getIntImmCost(Imm, Ty); } -unsigned TargetTransformInfo::getIntImmCost(unsigned Opcode, const APInt &Imm, - Type *Ty) const { - return PrevTTI->getIntImmCost(Opcode, Imm, Ty); +unsigned TargetTransformInfo::getIntImmCost(unsigned Opc, unsigned Idx, + const APInt &Imm, Type *Ty) const { + return PrevTTI->getIntImmCost(Opc, Idx, Imm, Ty); } -unsigned TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, const APInt &Imm, - Type *Ty) const { - return PrevTTI->getIntImmCost(IID, Imm, Ty); +unsigned TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx, + const APInt &Imm, Type *Ty) const { + return PrevTTI->getIntImmCost(IID, Idx, Imm, Ty); } unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const { @@ -539,12 +539,12 @@ struct NoTTI final : ImmutablePass, TargetTransformInfo { return TCC_Basic; } - unsigned getIntImmCost(unsigned Opcode, const APInt &Imm, + unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, Type *Ty) const override { return TCC_Free; } - unsigned getIntImmCost(Intrinsic::ID IID, const APInt &Imm, + unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, Type *Ty) const override { return TCC_Free; } diff --git a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp index 1a0208c1a52..87a5dd6536b 100644 --- a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp +++ b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp @@ -103,9 +103,9 @@ public: unsigned getIntImmCost(const APInt &Imm, Type *Ty) const override; - unsigned getIntImmCost(unsigned Opcode, const APInt &Imm, + unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, Type *Ty) const override; - unsigned getIntImmCost(Intrinsic::ID IID, const APInt &Imm, + unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, Type *Ty) const override; /// @} @@ -776,6 +776,9 @@ unsigned X86TTI::getIntImmCost(const APInt &Imm, Type *Ty) const { if (BitSize == 0) return ~0U; + if (Imm == 0) + return TCC_Free; + if (Imm.getBitWidth() <= 64 && (isInt<32>(Imm.getSExtValue()) || isUInt<32>(Imm.getZExtValue()))) return TCC_Basic; @@ -783,7 +786,7 @@ unsigned X86TTI::getIntImmCost(const APInt &Imm, Type *Ty) const { return 2 * TCC_Basic; } -unsigned X86TTI::getIntImmCost(unsigned Opcode, const APInt &Imm, +unsigned X86TTI::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, Type *Ty) const { assert(Ty->isIntegerTy()); @@ -791,7 +794,15 @@ unsigned X86TTI::getIntImmCost(unsigned Opcode, const APInt &Imm, if (BitSize == 0) return ~0U; + unsigned ImmIdx = ~0U; 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::Sub: case Instruction::Mul: @@ -806,28 +817,31 @@ unsigned X86TTI::getIntImmCost(unsigned Opcode, const APInt &Imm, case Instruction::Or: case Instruction::Xor: case Instruction::ICmp: - if (Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue())) - return TCC_Free; - else - return X86TTI::getIntImmCost(Imm, Ty); + ImmIdx = 1; + break; case Instruction::Trunc: case Instruction::ZExt: case Instruction::SExt: case Instruction::IntToPtr: case Instruction::PtrToInt: case Instruction::BitCast: + case Instruction::PHI: case Instruction::Call: case Instruction::Select: case Instruction::Ret: case Instruction::Load: - case Instruction::Store: - return X86TTI::getIntImmCost(Imm, Ty); + break; } - 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, const APInt &Imm, - Type *Ty) const { +unsigned X86TTI::getIntImmCost(Intrinsic::ID IID, unsigned Idx, + const APInt &Imm, Type *Ty) const { assert(Ty->isIntegerTy()); unsigned BitSize = Ty->getPrimitiveSizeInBits(); @@ -835,21 +849,24 @@ unsigned X86TTI::getIntImmCost(Intrinsic::ID IID, const APInt &Imm, return ~0U; switch (IID) { - default: return TargetTransformInfo::getIntImmCost(IID, Imm, Ty); + default: return TCC_Free; case Intrinsic::sadd_with_overflow: case Intrinsic::uadd_with_overflow: case Intrinsic::ssub_with_overflow: case Intrinsic::usub_with_overflow: case Intrinsic::smul_with_overflow: case Intrinsic::umul_with_overflow: - if (Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue())) + if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue())) return TCC_Free; else return X86TTI::getIntImmCost(Imm, Ty); case Intrinsic::experimental_stackmap: + if (Idx < 2) + return TCC_Free; case Intrinsic::experimental_patchpoint_void: case Intrinsic::experimental_patchpoint_i64: - if (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())) + if ((Idx < 4 ) || + (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) return TCC_Free; else return X86TTI::getIntImmCost(Imm, Ty); diff --git a/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp b/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp index 89df2b496f3..fc5917b8f37 100644 --- a/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp +++ b/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp @@ -29,7 +29,7 @@ // 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: +// simple (this means not nested) constant cast expressions. For example: // %0 = load i64* inttoptr (i64 big_constant to i64*) //===----------------------------------------------------------------------===// @@ -66,7 +66,7 @@ struct ConstantUser { ConstantUser(Instruction *Inst, unsigned Idx) : Inst(Inst), OpndIdx(Idx) { } }; -/// \brief Keeps track of a constant candidate and its usees. +/// \brief Keeps track of a constant candidate and its uses. struct ConstantCandidate { ConstantUseListType Uses; ConstantInt *ConstInt; @@ -292,7 +292,7 @@ findConstantInsertionPoint(const ConstantInfo &ConstInfo) const { /// \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 +/// The operand at index Idx is not necessarily the constant integer itself. It /// could also be a cast instruction or a constant expression that uses the // constant integer. void ConstantHoisting::collectConstantCandidates(Instruction *Inst, @@ -300,12 +300,12 @@ void ConstantHoisting::collectConstantCandidates(Instruction *Inst, ConstantInt *ConstInt) { unsigned Cost; // Ask the target about the cost of materializing the constant for the given - // instruction. + // instruction and operand index. if (auto IntrInst = dyn_cast<IntrinsicInst>(Inst)) - Cost = TTI->getIntImmCost(IntrInst->getIntrinsicID(), + Cost = TTI->getIntImmCost(IntrInst->getIntrinsicID(), Idx, ConstInt->getValue(), ConstInt->getType()); else - Cost = TTI->getIntImmCost(Inst->getOpcode(), ConstInt->getValue(), + Cost = TTI->getIntImmCost(Inst->getOpcode(), Idx, ConstInt->getValue(), ConstInt->getType()); // Ignore cheap integer constants. @@ -582,7 +582,7 @@ bool ConstantHoisting::optimizeConstants(Function &Fn) { if (ConstantVec.empty()) return false; - // Finally hoist the base constant and emit materializating code for dependent + // Finally hoist the base constant and emit materialization code for dependent // constants. bool MadeChange = emitBaseConstants(); |

