summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2014-03-05 19:41:48 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2014-03-05 19:41:48 +0000
commit4d36f91c0852c2c054056fa9bab7f6c443a82bb6 (patch)
treee84e6be0f4b6d5b2a6cdb977e95535e1d3456203 /lib
parentf3ff7c32f78f9fafe72a182837f42e14a12cff70 (diff)
downloadllvm-4d36f91c0852c2c054056fa9bab7f6c443a82bb6.tar.gz
llvm-4d36f91c0852c2c054056fa9bab7f6c443a82bb6.tar.bz2
llvm-4d36f91c0852c2c054056fa9bab7f6c443a82bb6.tar.xz
ConstantFolding: Also fold the vector overloads of our math intrinsics.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202997 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/Analysis/ConstantFolding.cpp107
1 files changed, 73 insertions, 34 deletions
diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp
index 254f2d9f50..08b60671a6 100644
--- a/lib/Analysis/ConstantFolding.cpp
+++ b/lib/Analysis/ConstantFolding.cpp
@@ -1327,28 +1327,19 @@ static double getValueAsDouble(ConstantFP *Op) {
return APF.convertToDouble();
}
-/// ConstantFoldCall - Attempt to constant fold a call to the specified function
-/// with the specified arguments, returning null if unsuccessful.
-Constant *
-llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
- const TargetLibraryInfo *TLI) {
- if (!F->hasName())
- return 0;
- StringRef Name = F->getName();
-
- Type *Ty = F->getReturnType();
+static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID,
+ Type *Ty, ArrayRef<Constant *> Operands,
+ const TargetLibraryInfo *TLI) {
if (Operands.size() == 1) {
if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
- if (F->getIntrinsicID() == Intrinsic::convert_to_fp16) {
+ if (IntrinsicID == Intrinsic::convert_to_fp16) {
APFloat Val(Op->getValueAPF());
bool lost = false;
Val.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &lost);
- return ConstantInt::get(F->getContext(), Val.bitcastToAPInt());
+ return ConstantInt::get(Ty->getContext(), Val.bitcastToAPInt());
}
- if (!TLI)
- return 0;
if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
return 0;
@@ -1365,7 +1356,7 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
/// f(arg). Long double not supported yet.
double V = getValueAsDouble(Op);
- switch (F->getIntrinsicID()) {
+ switch (IntrinsicID) {
default: break;
case Intrinsic::fabs:
return ConstantFoldFP(fabs, V, Ty);
@@ -1393,6 +1384,9 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
return ConstantFoldFP(floor, V, Ty);
}
+ if (!TLI)
+ return 0;
+
switch (Name[0]) {
case 'a':
if (Name == "acos" && TLI->has(LibFunc::acos))
@@ -1433,7 +1427,7 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
return ConstantFoldFP(log, V, Ty);
else if (Name == "log10" && V > 0 && TLI->has(LibFunc::log10))
return ConstantFoldFP(log10, V, Ty);
- else if (F->getIntrinsicID() == Intrinsic::sqrt &&
+ else if (IntrinsicID == Intrinsic::sqrt &&
(Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy())) {
if (V >= -0.0)
return ConstantFoldFP(sqrt, V, Ty);
@@ -1466,9 +1460,9 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
}
if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) {
- switch (F->getIntrinsicID()) {
+ switch (IntrinsicID) {
case Intrinsic::bswap:
- return ConstantInt::get(F->getContext(), Op->getValue().byteSwap());
+ return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap());
case Intrinsic::ctpop:
return ConstantInt::get(Ty, Op->getValue().countPopulation());
case Intrinsic::convert_from_fp16: {
@@ -1483,7 +1477,7 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
assert(status == APFloat::opOK && !lost &&
"Precision lost during fp16 constfolding");
- return ConstantFP::get(F->getContext(), Val);
+ return ConstantFP::get(Ty->getContext(), Val);
}
default:
return 0;
@@ -1494,7 +1488,7 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
if (isa<ConstantVector>(Operands[0]) ||
isa<ConstantDataVector>(Operands[0])) {
Constant *Op = cast<Constant>(Operands[0]);
- switch (F->getIntrinsicID()) {
+ switch (IntrinsicID) {
default: break;
case Intrinsic::x86_sse_cvtss2si:
case Intrinsic::x86_sse_cvtss2si64:
@@ -1516,7 +1510,7 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
}
if (isa<UndefValue>(Operands[0])) {
- if (F->getIntrinsicID() == Intrinsic::bswap)
+ if (IntrinsicID == Intrinsic::bswap)
return Operands[0];
return 0;
}
@@ -1535,7 +1529,7 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
return 0;
double Op2V = getValueAsDouble(Op2);
- if (F->getIntrinsicID() == Intrinsic::pow) {
+ if (IntrinsicID == Intrinsic::pow) {
return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
}
if (!TLI)
@@ -1547,16 +1541,16 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
if (Name == "atan2" && TLI->has(LibFunc::atan2))
return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
} else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
- if (F->getIntrinsicID() == Intrinsic::powi && Ty->isHalfTy())
- return ConstantFP::get(F->getContext(),
+ if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy())
+ return ConstantFP::get(Ty->getContext(),
APFloat((float)std::pow((float)Op1V,
(int)Op2C->getZExtValue())));
- if (F->getIntrinsicID() == Intrinsic::powi && Ty->isFloatTy())
- return ConstantFP::get(F->getContext(),
+ if (IntrinsicID == Intrinsic::powi && Ty->isFloatTy())
+ return ConstantFP::get(Ty->getContext(),
APFloat((float)std::pow((float)Op1V,
(int)Op2C->getZExtValue())));
- if (F->getIntrinsicID() == Intrinsic::powi && Ty->isDoubleTy())
- return ConstantFP::get(F->getContext(),
+ if (IntrinsicID == Intrinsic::powi && Ty->isDoubleTy())
+ return ConstantFP::get(Ty->getContext(),
APFloat((double)std::pow((double)Op1V,
(int)Op2C->getZExtValue())));
}
@@ -1565,7 +1559,7 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
if (ConstantInt *Op1 = dyn_cast<ConstantInt>(Operands[0])) {
if (ConstantInt *Op2 = dyn_cast<ConstantInt>(Operands[1])) {
- switch (F->getIntrinsicID()) {
+ switch (IntrinsicID) {
default: break;
case Intrinsic::sadd_with_overflow:
case Intrinsic::uadd_with_overflow:
@@ -1575,7 +1569,7 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
case Intrinsic::umul_with_overflow: {
APInt Res;
bool Overflow;
- switch (F->getIntrinsicID()) {
+ switch (IntrinsicID) {
default: llvm_unreachable("Invalid case");
case Intrinsic::sadd_with_overflow:
Res = Op1->getValue().sadd_ov(Op2->getValue(), Overflow);
@@ -1597,10 +1591,10 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
break;
}
Constant *Ops[] = {
- ConstantInt::get(F->getContext(), Res),
- ConstantInt::get(Type::getInt1Ty(F->getContext()), Overflow)
+ ConstantInt::get(Ty->getContext(), Res),
+ ConstantInt::get(Type::getInt1Ty(Ty->getContext()), Overflow)
};
- return ConstantStruct::get(cast<StructType>(F->getReturnType()), Ops);
+ return ConstantStruct::get(cast<StructType>(Ty), Ops);
}
case Intrinsic::cttz:
if (Op2->isOne() && Op1->isZero()) // cttz(0, 1) is undef.
@@ -1624,7 +1618,7 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
if (const ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
if (const ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
if (const ConstantFP *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
- switch (F->getIntrinsicID()) {
+ switch (IntrinsicID) {
default: break;
case Intrinsic::fma:
case Intrinsic::fmuladd: {
@@ -1644,3 +1638,48 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
return 0;
}
+
+static Constant *ConstantFoldVectorCall(StringRef Name, unsigned IntrinsicID,
+ VectorType *VTy,
+ ArrayRef<Constant *> Operands,
+ const TargetLibraryInfo *TLI) {
+ SmallVector<Constant *, 4> Result(VTy->getNumElements());
+ SmallVector<Constant *, 4> Lane(Operands.size());
+ Type *Ty = VTy->getElementType();
+
+ for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
+ // Gather a column of constants.
+ for (unsigned J = 0, JE = Operands.size(); J != JE; ++J) {
+ Constant *Agg = Operands[J]->getAggregateElement(I);
+ if (!Agg)
+ return nullptr;
+
+ Lane[J] = Agg;
+ }
+
+ // Use the regular scalar folding to simplify this column.
+ Constant *Folded = ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI);
+ if (!Folded)
+ return nullptr;
+ Result[I] = Folded;
+ }
+
+ return ConstantVector::get(Result);
+}
+
+/// ConstantFoldCall - Attempt to constant fold a call to the specified function
+/// with the specified arguments, returning null if unsuccessful.
+Constant *
+llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
+ const TargetLibraryInfo *TLI) {
+ if (!F->hasName())
+ return 0;
+ StringRef Name = F->getName();
+
+ Type *Ty = F->getReturnType();
+
+ if (VectorType *VTy = dyn_cast<VectorType>(Ty))
+ return ConstantFoldVectorCall(Name, F->getIntrinsicID(), VTy, Operands, TLI);
+
+ return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI);
+}