From 838d13e7ee6168ec31822b8278949cfe6266ac1e Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Mon, 10 Apr 2017 07:00:10 +0000 Subject: [PATCH] [InstCombine] Make sure we preserve fast math flags when folding fp instructions into phi nodes Summary: I noticed in the select folding code that we copied fast math flags, but did not do the same for the similar handling in phi nodes. This patch fixes that to do the same thing as select Reviewers: spatel, davide, majnemer, hfinkel Reviewed By: davide Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D31690 llvm-svn: 299838 --- .../InstCombine/InstructionCombining.cpp | 8 ++++++-- llvm/test/Transforms/InstCombine/fast-math.ll | 23 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index bc168dd..398b4974 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -937,11 +937,15 @@ Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { Constant *C = cast(I.getOperand(1)); for (unsigned i = 0; i != NumPHIValues; ++i) { Value *InV = nullptr; - if (Constant *InC = dyn_cast(PN->getIncomingValue(i))) + if (Constant *InC = dyn_cast(PN->getIncomingValue(i))) { InV = ConstantExpr::get(I.getOpcode(), InC, C); - else + } else { InV = Builder->CreateBinOp(cast(I).getOpcode(), PN->getIncomingValue(i), C, "phitmp"); + auto *FPInst = dyn_cast(InV); + if (FPInst && isa(FPInst)) + FPInst->copyFastMathFlags(&I); + } NewPN->addIncoming(InV, PN->getIncomingBlock(i)); } } else { diff --git a/llvm/test/Transforms/InstCombine/fast-math.ll b/llvm/test/Transforms/InstCombine/fast-math.ll index ad8a924..6ddf3a5 100644 --- a/llvm/test/Transforms/InstCombine/fast-math.ll +++ b/llvm/test/Transforms/InstCombine/fast-math.ll @@ -831,3 +831,26 @@ define fp128 @min4(fp128 %a, fp128 %b) { ; CHECK-NEXT: select {{.*}} fp128 %a, fp128 %b ; CHECK-NEXT: ret } + +define float @test55(i1 %which, float %a) { +; CHECK-LABEL: @test55( +; CHECK-NEXT: entry: +; CHECK-NEXT: br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]] +; CHECK: delay: +; CHECK-NEXT: [[PHITMP:%.*]] = fadd fast float [[A:%.*]], 1.000000e+00 +; CHECK-NEXT: br label [[FINAL]] +; CHECK: final: +; CHECK-NEXT: [[A:%.*]] = phi float [ 3.000000e+00, [[ENTRY:%.*]] ], [ [[PHITMP]], [[DELAY]] ] +; CHECK-NEXT: ret float [[A]] +; +entry: + br i1 %which, label %final, label %delay + +delay: + br label %final + +final: + %A = phi float [ 2.0, %entry ], [ %a, %delay ] + %value = fadd fast float %A, 1.0 + ret float %value +} -- 2.7.4