Skip to content

Commit

Permalink
[InstCombine] Allow fptrunc (fpext X)) to be reduced to a single fpex…
Browse files Browse the repository at this point in the history
…t/ftrunc

If we are only truncating bits from the extend we should be able to just use a smaller extend.

If we are truncating more than the extend we should be able to just use a fptrunc since the presense of the fpextend shouldn't affect rounding.

Differential Revision: https://reviews.llvm.org/D43970

llvm-svn: 326595
  • Loading branch information
topperc committed Mar 2, 2018
1 parent 20cf67c commit 18799f4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 25 deletions.
8 changes: 1 addition & 7 deletions llvm/lib/IR/Instructions.cpp
Expand Up @@ -2173,7 +2173,7 @@ unsigned CastInst::isEliminableCastPair(
{ 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
{ 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
{ 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
{ 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
{ 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt |
{ 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
{ 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
{ 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
Expand Down Expand Up @@ -2267,12 +2267,6 @@ unsigned CastInst::isEliminableCastPair(
case 9:
// zext, sext -> zext, because sext can't sign extend after zext
return Instruction::ZExt;
case 10:
// fpext followed by ftrunc is allowed if the bit size returned to is
// the same as the original, in which case its just a bitcast
if (SrcTy == DstTy)
return Instruction::BitCast;
return 0; // If the types are not the same we can't eliminate it.
case 11: {
// inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
if (!MidIntPtrTy)
Expand Down
21 changes: 7 additions & 14 deletions llvm/test/Transforms/InstCombine/double-float-shrink-2.ll
Expand Up @@ -448,8 +448,7 @@ define half @test_mismatched_type_intrin_fabs_fast_double_src(double %D) {
define float @test_shrink_intrin_floor_fp16_src(half %C) {
; ALL-LABEL: @test_shrink_intrin_floor_fp16_src(
; ALL-NEXT: [[E:%.*]] = call half @llvm.floor.f16(half [[C:%.*]])
; ALL-NEXT: [[TMP1:%.*]] = fpext half [[E]] to double
; ALL-NEXT: [[F:%.*]] = fptrunc double [[TMP1]] to float
; ALL-NEXT: [[F:%.*]] = fpext half [[E]] to float
; ALL-NEXT: ret float [[F]]
;
%D = fpext half %C to double
Expand All @@ -461,8 +460,7 @@ define float @test_shrink_intrin_floor_fp16_src(half %C) {
define float @test_shrink_intrin_ceil_fp16_src(half %C) {
; ALL-LABEL: @test_shrink_intrin_ceil_fp16_src(
; ALL-NEXT: [[E:%.*]] = call half @llvm.ceil.f16(half [[C:%.*]])
; ALL-NEXT: [[TMP1:%.*]] = fpext half [[E]] to double
; ALL-NEXT: [[F:%.*]] = fptrunc double [[TMP1]] to float
; ALL-NEXT: [[F:%.*]] = fpext half [[E]] to float
; ALL-NEXT: ret float [[F]]
;
%D = fpext half %C to double
Expand All @@ -474,8 +472,7 @@ define float @test_shrink_intrin_ceil_fp16_src(half %C) {
define float @test_shrink_intrin_round_fp16_src(half %C) {
; ALL-LABEL: @test_shrink_intrin_round_fp16_src(
; ALL-NEXT: [[E:%.*]] = call half @llvm.round.f16(half [[C:%.*]])
; ALL-NEXT: [[TMP1:%.*]] = fpext half [[E]] to double
; ALL-NEXT: [[F:%.*]] = fptrunc double [[TMP1]] to float
; ALL-NEXT: [[F:%.*]] = fpext half [[E]] to float
; ALL-NEXT: ret float [[F]]
;
%D = fpext half %C to double
Expand All @@ -487,8 +484,7 @@ define float @test_shrink_intrin_round_fp16_src(half %C) {
define float @test_shrink_intrin_nearbyint_fp16_src(half %C) {
; ALL-LABEL: @test_shrink_intrin_nearbyint_fp16_src(
; ALL-NEXT: [[E:%.*]] = call half @llvm.nearbyint.f16(half [[C:%.*]])
; ALL-NEXT: [[TMP1:%.*]] = fpext half [[E]] to double
; ALL-NEXT: [[F:%.*]] = fptrunc double [[TMP1]] to float
; ALL-NEXT: [[F:%.*]] = fpext half [[E]] to float
; ALL-NEXT: ret float [[F]]
;
%D = fpext half %C to double
Expand All @@ -500,8 +496,7 @@ define float @test_shrink_intrin_nearbyint_fp16_src(half %C) {
define float @test_shrink_intrin_trunc_fp16_src(half %C) {
; ALL-LABEL: @test_shrink_intrin_trunc_fp16_src(
; ALL-NEXT: [[E:%.*]] = call half @llvm.trunc.f16(half [[C:%.*]])
; ALL-NEXT: [[TMP1:%.*]] = fpext half [[E]] to double
; ALL-NEXT: [[F:%.*]] = fptrunc double [[TMP1]] to float
; ALL-NEXT: [[F:%.*]] = fpext half [[E]] to float
; ALL-NEXT: ret float [[F]]
;
%D = fpext half %C to double
Expand All @@ -513,8 +508,7 @@ define float @test_shrink_intrin_trunc_fp16_src(half %C) {
define float @test_shrink_intrin_fabs_fp16_src(half %C) {
; ALL-LABEL: @test_shrink_intrin_fabs_fp16_src(
; ALL-NEXT: [[E:%.*]] = call half @llvm.fabs.f16(half [[C:%.*]])
; ALL-NEXT: [[TMP1:%.*]] = fpext half [[E]] to double
; ALL-NEXT: [[F:%.*]] = fptrunc double [[TMP1]] to float
; ALL-NEXT: [[F:%.*]] = fpext half [[E]] to float
; ALL-NEXT: ret float [[F]]
;
%D = fpext half %C to double
Expand All @@ -527,8 +521,7 @@ define float @test_shrink_intrin_fabs_fp16_src(half %C) {
define float @test_shrink_intrin_fabs_fast_fp16_src(half %C) {
; ALL-LABEL: @test_shrink_intrin_fabs_fast_fp16_src(
; ALL-NEXT: [[E:%.*]] = call fast half @llvm.fabs.f16(half [[C:%.*]])
; ALL-NEXT: [[TMP1:%.*]] = fpext half [[E]] to double
; ALL-NEXT: [[F:%.*]] = fptrunc double [[TMP1]] to float
; ALL-NEXT: [[F:%.*]] = fpext half [[E]] to float
; ALL-NEXT: ret float [[F]]
;
%D = fpext half %C to double
Expand Down
6 changes: 2 additions & 4 deletions llvm/test/Transforms/InstCombine/fpextend.ll
Expand Up @@ -102,8 +102,7 @@ define <2 x float> @not_half_shrinkable(<2 x float> %x) {

define half @test7(float %a) nounwind {
; CHECK-LABEL: @test7(
; CHECK-NEXT: [[Y:%.*]] = fpext float [[A:%.*]] to double
; CHECK-NEXT: [[Z:%.*]] = fptrunc double [[Y]] to half
; CHECK-NEXT: [[Z:%.*]] = fptrunc float [[A:%.*]] to half
; CHECK-NEXT: ret half [[Z]]
;
%y = fpext float %a to double
Expand All @@ -113,8 +112,7 @@ define half @test7(float %a) nounwind {

define float @test8(half %a) nounwind {
; CHECK-LABEL: @test8(
; CHECK-NEXT: [[Y:%.*]] = fpext half [[A:%.*]] to double
; CHECK-NEXT: [[Z:%.*]] = fptrunc double [[Y]] to float
; CHECK-NEXT: [[Z:%.*]] = fpext half [[A:%.*]] to float
; CHECK-NEXT: ret float [[Z]]
;
%y = fpext half %a to double
Expand Down

0 comments on commit 18799f4

Please sign in to comment.