[llvm-commits] CVS: llvm/lib/VMCore/ConstantFold.cpp

Reid Spencer reid at x10sys.com
Thu Mar 22 22:33:40 PDT 2007



Changes in directory llvm/lib/VMCore:

ConstantFold.cpp updated: 1.147 -> 1.148
---
Log message:

Fix constant fold of div by zero and rem by zero to match IEEE 754
requirements. We must return NaN in some cases and correctly signed 
infinity in other cases. Passes CFP2006 (not that that says much).


---
Diffs of the changes:  (+18 -8)

 ConstantFold.cpp |   26 ++++++++++++++++++--------
 1 files changed, 18 insertions(+), 8 deletions(-)


Index: llvm/lib/VMCore/ConstantFold.cpp
diff -u llvm/lib/VMCore/ConstantFold.cpp:1.147 llvm/lib/VMCore/ConstantFold.cpp:1.148
--- llvm/lib/VMCore/ConstantFold.cpp:1.147	Sat Mar  3 02:32:46 2007
+++ llvm/lib/VMCore/ConstantFold.cpp	Fri Mar 23 00:33:23 2007
@@ -651,17 +651,27 @@
       case Instruction::Mul:     
         return ConstantFP::get(CFP1->getType(), C1Val * C2Val);
       case Instruction::FDiv:
-        if (CFP2->isExactlyValue(0.0)) 
-          return ConstantFP::get(CFP1->getType(),
-                                 std::numeric_limits<double>::infinity());
-        if (CFP2->isExactlyValue(-0.0))
-          return ConstantFP::get(CFP1->getType(),
-                                 -std::numeric_limits<double>::infinity());
+        if (CFP2->isExactlyValue(0.0) || CFP2->isExactlyValue(-0.0))
+          if (CFP1->isExactlyValue(0.0) || CFP1->isExactlyValue(-0.0))
+            // IEEE 754, Section 7.1, #4
+            return ConstantFP::get(CFP1->getType(),
+                                   std::numeric_limits<double>::quiet_NaN());
+          else if (CFP2->isExactlyValue(-0.0) || C1Val < 0.0)
+            // IEEE 754, Section 7.2, negative infinity case
+            return ConstantFP::get(CFP1->getType(),
+                                   -std::numeric_limits<double>::infinity());
+          else
+            // IEEE 754, Section 7.2, positive infinity case
+            return ConstantFP::get(CFP1->getType(),
+                                   std::numeric_limits<double>::infinity());
         return ConstantFP::get(CFP1->getType(), C1Val / C2Val);
       case Instruction::FRem:
-        if (CFP2->isNullValue()) 
-          return 0;
+        if (CFP2->isExactlyValue(0.0) || CFP2->isExactlyValue(-0.0))
+          // IEEE 754, Section 7.1, #5
+          return ConstantFP::get(CFP1->getType(), 
+                                 std::numeric_limits<double>::quiet_NaN());
         return ConstantFP::get(CFP1->getType(), std::fmod(C1Val, C2Val));
+
       }
     }
   } else if (const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1)) {






More information about the llvm-commits mailing list