[PATCH] D27587: Add "X / 1.0" to SimplifyFDivInst

Zia Ansari via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 8 11:56:23 PST 2016


zansari created this revision.
zansari added reviewers: sanjoy, spatel, majnemer.
zansari added subscribers: DavidKreitzer, llvm-commits.

We support most other simplifications, but not "X / 1.0", for some reason.

This should be legal with and without NaNs.

I also added a case to call SimplifyFDivInst in SimplifyFPBinOp. Again, I'm not sure why this was missing, but it's useful to have for cases where the fast math flags are important for simplification (otherwise, we end up calling SimplifyBinOp which will drop the FMFs by the time it gets back to SimplifyFDivInst).


https://reviews.llvm.org/D27587

Files:
  lib/Analysis/InstructionSimplify.cpp
  test/Transforms/EarlyCSE/instsimplify-fdiv.ll


Index: lib/Analysis/InstructionSimplify.cpp
===================================================================
--- lib/Analysis/InstructionSimplify.cpp
+++ lib/Analysis/InstructionSimplify.cpp
@@ -1127,6 +1127,10 @@
   if (match(Op1, m_Undef()))
     return Op1;
 
+  // X / 1.0 -> X
+  if (match(Op1, m_FPOne()))
+    return Op0;
+
   // 0 / X -> 0
   // Requires that NaNs are off (X could be zero) and signed zeroes are
   // ignored (X could be positive or negative, so the output sign is unknown).
@@ -4093,6 +4097,8 @@
     return SimplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse);
   case Instruction::FMul:
     return SimplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse);
+  case Instruction::FDiv:
+    return SimplifyFDivInst(LHS, RHS, FMF, Q, MaxRecurse);
   default:
     return SimplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse);
   }
Index: test/Transforms/EarlyCSE/instsimplify-fdiv.ll
===================================================================
--- test/Transforms/EarlyCSE/instsimplify-fdiv.ll
+++ test/Transforms/EarlyCSE/instsimplify-fdiv.ll
@@ -0,0 +1,19 @@
+; RUN: opt -O3 -S -mtriple=i686-pc-linux -print-after=early-cse < %s 2>&1 | FileCheck %s
+;
+; Check to make sure divide by 1.0 is simplified in early-cse.
+;
+
+define double @foo(double %x, i32 %n, double %e) #0 {
+entry:
+  %x.addr = alloca double, align 8
+  %n.addr = alloca i32, align 4
+  %e.addr = alloca double, align 8
+  store double %x, double* %x.addr, align 8
+  store i32 %n, i32* %n.addr, align 4
+  store double %e, double* %e.addr, align 8
+  %0 = load double, double* %x.addr, align 8
+  %div = fdiv double %0, 1.000000e+00
+  ret double %div
+}
+
+; CHECK-NOT: fdiv


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27587.80797.patch
Type: text/x-patch
Size: 1662 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161208/a3f4298b/attachment.bin>


More information about the llvm-commits mailing list