[llvm-commits] [PATCH][FAST-MATH] recognize fp negation

Shuxin Yang shuxin.llvm at gmail.com
Thu Jan 3 14:46:33 PST 2013


Hi,

     Currently the compiler only considers "-0.0 - x" as a negation of 
quantity x regardless the
  fast-math flags associated with the expr or the fast-math flags 
derived from the supper-expr.
  This patch is to fix this problem.

    With this change, "0.0 - x" will be considered as negation of x as 
well so long as we don't care
"signed-zero".

    It is about two weeks old. Unfortunately, as of I write this mail, 
I'm not able to update the latest revision
and re-test it.

Thanks
Shuxin

-------------- next part --------------
Index: include/llvm/InstrTypes.h
===================================================================
--- include/llvm/InstrTypes.h	(revision 170838)
+++ include/llvm/InstrTypes.h	(working copy)
@@ -309,7 +309,7 @@
   /// NEG, FNeg, or NOT instruction.
   ///
   static bool isNeg(const Value *V);
-  static bool isFNeg(const Value *V);
+  static bool isFNeg(const Value *V, bool IgnoreZeroSign=false);
   static bool isNot(const Value *V);
 
   /// getNegArgument, getNotArgument - Helper functions to extract the
Index: include/llvm/Constant.h
===================================================================
--- include/llvm/Constant.h	(revision 170838)
+++ include/llvm/Constant.h	(working copy)
@@ -61,6 +61,9 @@
   /// by getZeroValueForNegation.
   bool isNegativeZeroValue() const;
 
+  /// Return true if the value is negative zero or null value.
+  bool isZeroValue() const;
+
   /// canTrap - Return true if evaluation of this constant could trap.  This is
   /// true for things like constant expressions that could divide by zero.
   bool canTrap() const;
Index: lib/VMCore/Instructions.cpp
===================================================================
--- lib/VMCore/Instructions.cpp	(revision 170838)
+++ lib/VMCore/Instructions.cpp	(working copy)
@@ -1928,11 +1928,11 @@
   return false;
 }
 
-bool BinaryOperator::isFNeg(const Value *V) {
+bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
   if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
     if (Bop->getOpcode() == Instruction::FSub)
       if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
-        return C->isNegativeZeroValue();
+        return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
   return false;
 }
 
Index: lib/VMCore/Constants.cpp
===================================================================
--- lib/VMCore/Constants.cpp	(revision 170838)
+++ lib/VMCore/Constants.cpp	(working copy)
@@ -51,6 +51,15 @@
   return isNullValue();
 }
 
+bool Constant::isZeroValue() const {
+  // Floating point values have an explicit -0.0 value.
+  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
+    return CFP->isZero();
+
+  // Otherwise, just use +0.0.
+  return isNullValue();
+}
+
 bool Constant::isNullValue() const {
   // 0 is null.
   if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
Index: lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- lib/Transforms/InstCombine/InstructionCombining.cpp	(revision 170838)
+++ lib/Transforms/InstCombine/InstructionCombining.cpp	(working copy)
@@ -516,8 +516,8 @@
 // instruction if the LHS is a constant negative zero (which is the 'negate'
 // form).
 //
-Value *InstCombiner::dyn_castFNegVal(Value *V) const {
-  if (BinaryOperator::isFNeg(V))
+Value *InstCombiner::dyn_castFNegVal(Value *V, bool IgnoreZeroSign) const {
+  if (BinaryOperator::isFNeg(V, IgnoreZeroSign))
     return BinaryOperator::getFNegArgument(V);
 
   // Constants can be considered to be negated values if they can be folded.
Index: lib/Transforms/InstCombine/InstCombine.h
===================================================================
--- lib/Transforms/InstCombine/InstCombine.h	(revision 170838)
+++ lib/Transforms/InstCombine/InstCombine.h	(working copy)
@@ -207,7 +207,7 @@
 private:
   bool ShouldChangeType(Type *From, Type *To) const;
   Value *dyn_castNegVal(Value *V) const;
-  Value *dyn_castFNegVal(Value *V) const;
+  Value *dyn_castFNegVal(Value *V, bool NoSignedZero=false) const;
   Type *FindElementAtOffset(Type *Ty, int64_t Offset, 
                                   SmallVectorImpl<Value*> &NewIndices);
   Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);


More information about the llvm-commits mailing list