Skip to content

Commit 8452fac

Browse files
committedMar 5, 2018
[InstCombine] Add constant vector support to getMinimumFPType for visitFPTrunc.
This patch teaches getMinimumFPType to support shrinking a vector of ConstantFPs. This should improve our ability to combine vector fptrunc with fp binops. Differential Revision: https://reviews.llvm.org/D43774 llvm-svn: 326729
1 parent 75bc70f commit 8452fac

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed
 

‎llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,36 @@ static Type *shrinkFPConstant(ConstantFP *CFP) {
14351435
return nullptr;
14361436
}
14371437

1438+
// Determine if this is a vector of ConstantFPs and if so, return the minimal
1439+
// type we can safely truncate all elements to.
1440+
// TODO: Make these support undef elements.
1441+
static Type *shrinkFPConstantVector(Value *V) {
1442+
auto *CV = dyn_cast<Constant>(V);
1443+
if (!CV || !CV->getType()->isVectorTy())
1444+
return nullptr;
1445+
1446+
Type *MinType = nullptr;
1447+
1448+
unsigned NumElts = CV->getType()->getVectorNumElements();
1449+
for (unsigned i = 0; i != NumElts; ++i) {
1450+
auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
1451+
if (!CFP)
1452+
return nullptr;
1453+
1454+
Type *T = shrinkFPConstant(CFP);
1455+
if (!T)
1456+
return nullptr;
1457+
1458+
// If we haven't found a type yet or this type has a larger mantissa than
1459+
// our previous type, this is our new minimal type.
1460+
if (!MinType || T->getFPMantissaWidth() > MinType->getFPMantissaWidth())
1461+
MinType = T;
1462+
}
1463+
1464+
// Make a vector type from the minimal type.
1465+
return VectorType::get(MinType, NumElts);
1466+
}
1467+
14381468
/// Find the minimum FP type we can safely truncate to.
14391469
static Type *getMinimumFPType(Value *V) {
14401470
if (auto *FPExt = dyn_cast<FPExtInst>(V))
@@ -1447,6 +1477,10 @@ static Type *getMinimumFPType(Value *V) {
14471477
if (Type *T = shrinkFPConstant(CFP))
14481478
return T;
14491479

1480+
// Try to shrink a vector of FP constants.
1481+
if (Type *T = shrinkFPConstantVector(V))
1482+
return T;
1483+
14501484
return V->getType();
14511485
}
14521486

‎llvm/test/Transforms/InstCombine/fpextend.ll

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ entry:
5959
define <2 x float> @test5(<2 x float> %x) nounwind {
6060
; CHECK-LABEL: @test5(
6161
; CHECK-NEXT: entry:
62-
; CHECK-NEXT: [[TMP1:%.*]] = fpext <2 x float> [[X:%.*]] to <2 x double>
63-
; CHECK-NEXT: [[TMP3:%.*]] = fadd <2 x double> [[TMP1]], zeroinitializer
64-
; CHECK-NEXT: [[TMP34:%.*]] = fptrunc <2 x double> [[TMP3]] to <2 x float>
62+
; CHECK-NEXT: [[TMP34:%.*]] = fadd <2 x float> [[X:%.*]], zeroinitializer
6563
; CHECK-NEXT: ret <2 x float> [[TMP34]]
6664
;
6765
entry:
@@ -75,23 +73,36 @@ entry:
7573
define <2 x float> @test6(<2 x float> %x) nounwind {
7674
; CHECK-LABEL: @test6(
7775
; CHECK-NEXT: entry:
76+
; CHECK-NEXT: [[TMP34:%.*]] = fadd <2 x float> [[X:%.*]], <float 0.000000e+00, float -0.000000e+00>
77+
; CHECK-NEXT: ret <2 x float> [[TMP34]]
78+
;
79+
entry:
80+
%tmp1 = fpext <2 x float> %x to <2 x double>
81+
%tmp3 = fadd <2 x double> %tmp1, <double 0.000000e+00, double -0.000000e+00>
82+
%tmp34 = fptrunc <2 x double> %tmp3 to <2 x float>
83+
ret <2 x float> %tmp34
84+
}
85+
86+
; Test with an undef element
87+
; TODO: Support undef elements.
88+
define <2 x float> @test6_undef(<2 x float> %x) nounwind {
89+
; CHECK-LABEL: @test6_undef(
90+
; CHECK-NEXT: entry:
7891
; CHECK-NEXT: [[TMP1:%.*]] = fpext <2 x float> [[X:%.*]] to <2 x double>
79-
; CHECK-NEXT: [[TMP3:%.*]] = fadd <2 x double> [[TMP1]], <double 0.000000e+00, double -0.000000e+00>
92+
; CHECK-NEXT: [[TMP3:%.*]] = fadd <2 x double> [[TMP1]], <double 0.000000e+00, double undef>
8093
; CHECK-NEXT: [[TMP34:%.*]] = fptrunc <2 x double> [[TMP3]] to <2 x float>
8194
; CHECK-NEXT: ret <2 x float> [[TMP34]]
8295
;
8396
entry:
8497
%tmp1 = fpext <2 x float> %x to <2 x double>
85-
%tmp3 = fadd <2 x double> %tmp1, <double 0.000000e+00, double -0.000000e+00>
98+
%tmp3 = fadd <2 x double> %tmp1, <double 0.000000e+00, double undef>
8699
%tmp34 = fptrunc <2 x double> %tmp3 to <2 x float>
87100
ret <2 x float> %tmp34
88101
}
89102

90103
define <2 x float> @not_half_shrinkable(<2 x float> %x) {
91104
; CHECK-LABEL: @not_half_shrinkable(
92-
; CHECK-NEXT: [[EXT:%.*]] = fpext <2 x float> [[X:%.*]] to <2 x double>
93-
; CHECK-NEXT: [[ADD:%.*]] = fadd <2 x double> [[EXT]], <double 0.000000e+00, double 2.049000e+03>
94-
; CHECK-NEXT: [[R:%.*]] = fptrunc <2 x double> [[ADD]] to <2 x float>
105+
; CHECK-NEXT: [[R:%.*]] = fadd <2 x float> [[X:%.*]], <float 0.000000e+00, float 2.049000e+03>
95106
; CHECK-NEXT: ret <2 x float> [[R]]
96107
;
97108
%ext = fpext <2 x float> %x to <2 x double>

0 commit comments

Comments
 (0)
Please sign in to comment.