From 5797bfd4a3392738b27796162d526b0e37dd1e9f Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Thu, 3 Oct 2013 21:08:05 +0000 Subject: [PATCH] Pull fptrunc's upwards through selects when one of the select's selectands was a constant. This has a number of benefits, including producing small immediates (easier to materialize, smaller constant pools) as well as being more likely to allow the fptrunc to fuse with a preceding instruction (truncating selects are unusual). llvm-svn: 191929 --- .../lib/Transforms/InstCombine/InstCombineCasts.cpp | 13 +++++++++++++ llvm/test/Transforms/InstCombine/fpcast.ll | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp index a35631fc15c3..01894cbfdc15 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -1229,6 +1229,19 @@ Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { } } + // (fptrunc (select cond, R1, Cst)) --> + // (select cond, (fptrunc R1), (fptrunc Cst)) + SelectInst *SI = dyn_cast(CI.getOperand(0)); + if (SI && + (isa(SI->getOperand(1)) || + isa(SI->getOperand(2)))) { + Value *LHSTrunc = Builder->CreateFPTrunc(SI->getOperand(1), + CI.getType()); + Value *RHSTrunc = Builder->CreateFPTrunc(SI->getOperand(2), + CI.getType()); + return SelectInst::Create(SI->getOperand(0), LHSTrunc, RHSTrunc); + } + IntrinsicInst *II = dyn_cast(CI.getOperand(0)); if (II) { switch (II->getIntrinsicID()) { diff --git a/llvm/test/Transforms/InstCombine/fpcast.ll b/llvm/test/Transforms/InstCombine/fpcast.ll index 09f053289dc1..05d1b48d5996 100644 --- a/llvm/test/Transforms/InstCombine/fpcast.ll +++ b/llvm/test/Transforms/InstCombine/fpcast.ll @@ -31,4 +31,16 @@ define half @test4(float %a) { ret half %c } +; CHECK: test5 +define half @test5(float %a, float %b, float %c) { +; CHECK: fcmp ogt +; CHECK: fptrunc +; CHECK: select +; CHECK: half 0xH3C00 + %d = fcmp ogt float %a, %b + %e = select i1 %d, float %c, float 1.0 + %f = fptrunc float %e to half + ret half %f +} + declare float @llvm.fabs.f32(float) nounwind readonly