mirror of
https://github.com/RPCS3/llvm.git
synced 2024-12-25 13:35:10 +00:00
Signficantly generalize our ability to constant fold floating point intrinsics, including ones on half types.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@174555 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
bfa18fdfbf
commit
42258e0ea8
@ -289,6 +289,10 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset,
|
|||||||
C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), TD);
|
C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), TD);
|
||||||
return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
|
return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
|
||||||
}
|
}
|
||||||
|
if (CFP->getType()->isHalfTy()){
|
||||||
|
C = FoldBitCast(C, Type::getInt16Ty(C->getContext()), TD);
|
||||||
|
return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -381,7 +385,9 @@ static Constant *FoldReinterpretLoadFromConstPtr(Constant *C,
|
|||||||
// that address spaces don't matter here since we're not going to result in
|
// that address spaces don't matter here since we're not going to result in
|
||||||
// an actual new load.
|
// an actual new load.
|
||||||
Type *MapTy;
|
Type *MapTy;
|
||||||
if (LoadTy->isFloatTy())
|
if (LoadTy->isHalfTy())
|
||||||
|
MapTy = Type::getInt16PtrTy(C->getContext());
|
||||||
|
else if (LoadTy->isFloatTy())
|
||||||
MapTy = Type::getInt32PtrTy(C->getContext());
|
MapTy = Type::getInt32PtrTy(C->getContext());
|
||||||
else if (LoadTy->isDoubleTy())
|
else if (LoadTy->isDoubleTy())
|
||||||
MapTy = Type::getInt64PtrTy(C->getContext());
|
MapTy = Type::getInt64PtrTy(C->getContext());
|
||||||
@ -1089,6 +1095,13 @@ Constant *llvm::ConstantFoldLoadThroughGEPIndices(Constant *C,
|
|||||||
bool
|
bool
|
||||||
llvm::canConstantFoldCallTo(const Function *F) {
|
llvm::canConstantFoldCallTo(const Function *F) {
|
||||||
switch (F->getIntrinsicID()) {
|
switch (F->getIntrinsicID()) {
|
||||||
|
case Intrinsic::fabs:
|
||||||
|
case Intrinsic::log:
|
||||||
|
case Intrinsic::log2:
|
||||||
|
case Intrinsic::log10:
|
||||||
|
case Intrinsic::exp:
|
||||||
|
case Intrinsic::exp2:
|
||||||
|
case Intrinsic::floor:
|
||||||
case Intrinsic::sqrt:
|
case Intrinsic::sqrt:
|
||||||
case Intrinsic::pow:
|
case Intrinsic::pow:
|
||||||
case Intrinsic::powi:
|
case Intrinsic::powi:
|
||||||
@ -1156,11 +1169,17 @@ static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Ty->isHalfTy()) {
|
||||||
|
APFloat APF(V);
|
||||||
|
bool unused;
|
||||||
|
APF.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &unused);
|
||||||
|
return ConstantFP::get(Ty->getContext(), APF);
|
||||||
|
}
|
||||||
if (Ty->isFloatTy())
|
if (Ty->isFloatTy())
|
||||||
return ConstantFP::get(Ty->getContext(), APFloat((float)V));
|
return ConstantFP::get(Ty->getContext(), APFloat((float)V));
|
||||||
if (Ty->isDoubleTy())
|
if (Ty->isDoubleTy())
|
||||||
return ConstantFP::get(Ty->getContext(), APFloat(V));
|
return ConstantFP::get(Ty->getContext(), APFloat(V));
|
||||||
llvm_unreachable("Can only constant fold float/double");
|
llvm_unreachable("Can only constant fold half/float/double");
|
||||||
}
|
}
|
||||||
|
|
||||||
static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
|
static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
|
||||||
@ -1172,11 +1191,17 @@ static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Ty->isHalfTy()) {
|
||||||
|
APFloat APF(V);
|
||||||
|
bool unused;
|
||||||
|
APF.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &unused);
|
||||||
|
return ConstantFP::get(Ty->getContext(), APF);
|
||||||
|
}
|
||||||
if (Ty->isFloatTy())
|
if (Ty->isFloatTy())
|
||||||
return ConstantFP::get(Ty->getContext(), APFloat((float)V));
|
return ConstantFP::get(Ty->getContext(), APFloat((float)V));
|
||||||
if (Ty->isDoubleTy())
|
if (Ty->isDoubleTy())
|
||||||
return ConstantFP::get(Ty->getContext(), APFloat(V));
|
return ConstantFP::get(Ty->getContext(), APFloat(V));
|
||||||
llvm_unreachable("Can only constant fold float/double");
|
llvm_unreachable("Can only constant fold half/float/double");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ConstantFoldConvertToInt - Attempt to an SSE floating point to integer
|
/// ConstantFoldConvertToInt - Attempt to an SSE floating point to integer
|
||||||
@ -1228,7 +1253,7 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
|
|||||||
if (!TLI)
|
if (!TLI)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (!Ty->isFloatTy() && !Ty->isDoubleTy())
|
if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/// We only fold functions with finite arguments. Folding NaN and inf is
|
/// We only fold functions with finite arguments. Folding NaN and inf is
|
||||||
@ -1241,8 +1266,36 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
|
|||||||
/// the host native double versions. Float versions are not called
|
/// the host native double versions. Float versions are not called
|
||||||
/// directly but for all these it is true (float)(f((double)arg)) ==
|
/// directly but for all these it is true (float)(f((double)arg)) ==
|
||||||
/// f(arg). Long double not supported yet.
|
/// f(arg). Long double not supported yet.
|
||||||
double V = Ty->isFloatTy() ? (double)Op->getValueAPF().convertToFloat() :
|
double V;
|
||||||
Op->getValueAPF().convertToDouble();
|
if (Ty->isFloatTy())
|
||||||
|
V = Op->getValueAPF().convertToFloat();
|
||||||
|
else if (Ty->isDoubleTy())
|
||||||
|
V = Op->getValueAPF().convertToDouble();
|
||||||
|
else {
|
||||||
|
bool unused;
|
||||||
|
APFloat APF = Op->getValueAPF();
|
||||||
|
APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &unused);
|
||||||
|
V = APF.convertToDouble();
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (F->getIntrinsicID()) {
|
||||||
|
default: break;
|
||||||
|
case Intrinsic::fabs:
|
||||||
|
return ConstantFoldFP(fabs, V, Ty);
|
||||||
|
case Intrinsic::log2:
|
||||||
|
return ConstantFoldFP(log2, V, Ty);
|
||||||
|
case Intrinsic::log:
|
||||||
|
return ConstantFoldFP(log, V, Ty);
|
||||||
|
case Intrinsic::log10:
|
||||||
|
return ConstantFoldFP(log10, V, Ty);
|
||||||
|
case Intrinsic::exp:
|
||||||
|
return ConstantFoldFP(exp, V, Ty);
|
||||||
|
case Intrinsic::exp2:
|
||||||
|
return ConstantFoldFP(exp2, V, Ty);
|
||||||
|
case Intrinsic::floor:
|
||||||
|
return ConstantFoldFP(floor, V, Ty);
|
||||||
|
}
|
||||||
|
|
||||||
switch (Name[0]) {
|
switch (Name[0]) {
|
||||||
case 'a':
|
case 'a':
|
||||||
if (Name == "acos" && TLI->has(LibFunc::acos))
|
if (Name == "acos" && TLI->has(LibFunc::acos))
|
||||||
@ -1284,7 +1337,7 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
|
|||||||
else if (Name == "log10" && V > 0 && TLI->has(LibFunc::log10))
|
else if (Name == "log10" && V > 0 && TLI->has(LibFunc::log10))
|
||||||
return ConstantFoldFP(log10, V, Ty);
|
return ConstantFoldFP(log10, V, Ty);
|
||||||
else if (F->getIntrinsicID() == Intrinsic::sqrt &&
|
else if (F->getIntrinsicID() == Intrinsic::sqrt &&
|
||||||
(Ty->isFloatTy() || Ty->isDoubleTy())) {
|
(Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy())) {
|
||||||
if (V >= -0.0)
|
if (V >= -0.0)
|
||||||
return ConstantFoldFP(sqrt, V, Ty);
|
return ConstantFoldFP(sqrt, V, Ty);
|
||||||
else // Undefined
|
else // Undefined
|
||||||
@ -1376,18 +1429,35 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
|
|||||||
|
|
||||||
if (Operands.size() == 2) {
|
if (Operands.size() == 2) {
|
||||||
if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
|
if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
|
||||||
if (!Ty->isFloatTy() && !Ty->isDoubleTy())
|
if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
|
||||||
return 0;
|
return 0;
|
||||||
double Op1V = Ty->isFloatTy() ?
|
double Op1V;
|
||||||
(double)Op1->getValueAPF().convertToFloat() :
|
if (Ty->isFloatTy())
|
||||||
Op1->getValueAPF().convertToDouble();
|
Op1V = Op1->getValueAPF().convertToFloat();
|
||||||
|
else if (Ty->isDoubleTy())
|
||||||
|
Op1V = Op1->getValueAPF().convertToDouble();
|
||||||
|
else {
|
||||||
|
bool unused;
|
||||||
|
APFloat APF = Op1->getValueAPF();
|
||||||
|
APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &unused);
|
||||||
|
Op1V = APF.convertToDouble();
|
||||||
|
}
|
||||||
|
|
||||||
if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
|
if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
|
||||||
if (Op2->getType() != Op1->getType())
|
if (Op2->getType() != Op1->getType())
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
double Op2V = Ty->isFloatTy() ?
|
double Op2V;
|
||||||
(double)Op2->getValueAPF().convertToFloat():
|
if (Ty->isFloatTy())
|
||||||
Op2->getValueAPF().convertToDouble();
|
Op2V = Op2->getValueAPF().convertToFloat();
|
||||||
|
else if (Ty->isDoubleTy())
|
||||||
|
Op2V = Op2->getValueAPF().convertToDouble();
|
||||||
|
else {
|
||||||
|
bool unused;
|
||||||
|
APFloat APF = Op2->getValueAPF();
|
||||||
|
APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &unused);
|
||||||
|
Op2V = APF.convertToDouble();
|
||||||
|
}
|
||||||
|
|
||||||
if (F->getIntrinsicID() == Intrinsic::pow) {
|
if (F->getIntrinsicID() == Intrinsic::pow) {
|
||||||
return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
|
return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
|
||||||
@ -1401,6 +1471,10 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
|
|||||||
if (Name == "atan2" && TLI->has(LibFunc::atan2))
|
if (Name == "atan2" && TLI->has(LibFunc::atan2))
|
||||||
return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
|
return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
|
||||||
} else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
|
} else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
|
||||||
|
if (F->getIntrinsicID() == Intrinsic::powi && Ty->isHalfTy())
|
||||||
|
return ConstantFP::get(F->getContext(),
|
||||||
|
APFloat((float)std::pow((float)Op1V,
|
||||||
|
(int)Op2C->getZExtValue())));
|
||||||
if (F->getIntrinsicID() == Intrinsic::powi && Ty->isFloatTy())
|
if (F->getIntrinsicID() == Intrinsic::powi && Ty->isFloatTy())
|
||||||
return ConstantFP::get(F->getContext(),
|
return ConstantFP::get(F->getContext(),
|
||||||
APFloat((float)std::pow((float)Op1V,
|
APFloat((float)std::pow((float)Op1V,
|
||||||
|
42
test/Transforms/ConstProp/half.ll
Normal file
42
test/Transforms/ConstProp/half.ll
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
; RUN: opt -constprop -S < %s | FileCheck %s
|
||||||
|
|
||||||
|
; CHECK: fabs_call
|
||||||
|
define half @fabs_call() {
|
||||||
|
; CHECK: ret half 0xH5140
|
||||||
|
%x = call half @llvm.fabs.f16(half -42.0)
|
||||||
|
ret half %x
|
||||||
|
}
|
||||||
|
declare half @llvm.fabs.f16(half %x)
|
||||||
|
|
||||||
|
; CHECK: exp_call
|
||||||
|
define half @exp_call() {
|
||||||
|
; CHECK: ret half 0xH4170
|
||||||
|
%x = call half @llvm.exp.f16(half 1.0)
|
||||||
|
ret half %x
|
||||||
|
}
|
||||||
|
declare half @llvm.exp.f16(half %x)
|
||||||
|
|
||||||
|
; CHECK: sqrt_call
|
||||||
|
define half @sqrt_call() {
|
||||||
|
; CHECK: ret half 0xH4000
|
||||||
|
%x = call half @llvm.sqrt.f16(half 4.0)
|
||||||
|
ret half %x
|
||||||
|
}
|
||||||
|
declare half @llvm.sqrt.f16(half %x)
|
||||||
|
|
||||||
|
; CHECK: floor_call
|
||||||
|
define half @floor_call() {
|
||||||
|
; CHECK: ret half 0xH4000
|
||||||
|
%x = call half @llvm.floor.f16(half 2.5)
|
||||||
|
ret half %x
|
||||||
|
}
|
||||||
|
declare half @llvm.floor.f16(half %x)
|
||||||
|
|
||||||
|
; CHECK: pow_call
|
||||||
|
define half @pow_call() {
|
||||||
|
; CHECK: ret half 0xH4400
|
||||||
|
%x = call half @llvm.pow.f16(half 2.0, half 2.0)
|
||||||
|
ret half %x
|
||||||
|
}
|
||||||
|
declare half @llvm.pow.f16(half %x, half %y)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user