From 491284f0da18d31bd982619868f96b4fe6fb8b16 Mon Sep 17 00:00:00 2001 From: Vyacheslav Klochkov Date: Tue, 22 Nov 2016 20:23:04 +0000 Subject: [PATCH] Fixed the lost FastMathFlags in Reassociate optimization. Reviewer: Hal Finkel. Differential Revision: https://reviews.llvm.org/D26957 llvm-svn: 287695 --- lib/Transforms/Scalar/Reassociate.cpp | 6 ++++++ test/Transforms/Reassociate/propagate-flags.ll | 14 ++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 test/Transforms/Reassociate/propagate-flags.ll diff --git a/lib/Transforms/Scalar/Reassociate.cpp b/lib/Transforms/Scalar/Reassociate.cpp index 40c1e3717a2..181a324861e 100644 --- a/lib/Transforms/Scalar/Reassociate.cpp +++ b/lib/Transforms/Scalar/Reassociate.cpp @@ -1778,6 +1778,12 @@ Value *ReassociatePass::OptimizeMul(BinaryOperator *I, return nullptr; // All distinct factors, so nothing left for us to do. IRBuilder<> Builder(I); + // The reassociate transformation for FP operations is performed only + // if unsafe algebra is permitted by FastMathFlags. Propagate those flags + // to the newly generated operations. + if (auto FPI = dyn_cast(I)) + Builder.setFastMathFlags(FPI->getFastMathFlags()); + Value *V = buildMinimalMultiplyDAG(Builder, Factors); if (Ops.empty()) return V; diff --git a/test/Transforms/Reassociate/propagate-flags.ll b/test/Transforms/Reassociate/propagate-flags.ll new file mode 100644 index 00000000000..b8987855f0e --- /dev/null +++ b/test/Transforms/Reassociate/propagate-flags.ll @@ -0,0 +1,14 @@ +; RUN: opt < %s -reassociate -S | FileCheck %s + +; CHECK-LABEL: func +; CHECK: fmul fast double +; CHECK-NEXT: fmul fast double +; CHECK-NEXT: ret + +define double @func(double %a, double %b) { +entry: + %mul1 = fmul fast double %a, %a + %mul2 = fmul fast double %b, %b + %mul3 = fmul fast double %mul1, %mul2 + ret double %mul3 +}