[llvm-commits] [llvm] r39923 - /llvm/trunk/lib/Analysis/ConstantFolding.cpp

Dan Gohman djg at cray.com
Mon Jul 16 08:26:22 PDT 2007


Author: djg
Date: Mon Jul 16 10:26:22 2007
New Revision: 39923

URL: http://llvm.org/viewvc/llvm-project?rev=39923&view=rev
Log:
Use ConstantFoldFP for folding all unary floating-point operations which may
have an error, and refector out the code for binary operators into
ConstantFoldBinaryFP and use it for all binary floating-point operations
which may have an error. These functions still rely exclusively on errno
to detect errors though.

Modified:
    llvm/trunk/lib/Analysis/ConstantFolding.cpp

Modified: llvm/trunk/lib/Analysis/ConstantFolding.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ConstantFolding.cpp?rev=39923&r1=39922&r2=39923&view=diff

==============================================================================
--- llvm/trunk/lib/Analysis/ConstantFolding.cpp (original)
+++ llvm/trunk/lib/Analysis/ConstantFolding.cpp Mon Jul 16 10:26:22 2007
@@ -363,6 +363,17 @@
   return 0;
 }
 
+static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
+                                      double V, double W,
+                                      const Type *Ty) {
+  errno = 0;
+  V = NativeFP(V, W);
+  if (errno == 0)
+    return ConstantFP::get(Ty, V);
+  errno = 0;
+  return 0;
+}
+
 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
 /// with the specified arguments, returning null if unsuccessful.
 Constant *
@@ -381,19 +392,19 @@
           else if (Name == "asin")
             return ConstantFoldFP(asin, V, Ty);
           else if (Name == "atan")
-            return ConstantFP::get(Ty, atan(V));
+            return ConstantFoldFP(atan, V, Ty);
           break;
         case 'c':
           if (Name == "ceil")
             return ConstantFoldFP(ceil, V, Ty);
           else if (Name == "cos")
-            return ConstantFP::get(Ty, cos(V));
+            return ConstantFoldFP(cos, V, Ty);
           else if (Name == "cosh")
-            return ConstantFP::get(Ty, cosh(V));
+            return ConstantFoldFP(cosh, V, Ty);
           break;
         case 'e':
           if (Name == "exp")
-            return ConstantFP::get(Ty, exp(V));
+            return ConstantFoldFP(exp, V, Ty);
           break;
         case 'f':
           if (Name == "fabs")
@@ -403,7 +414,7 @@
           break;
         case 'l':
           if (Name == "log" && V > 0)
-            return ConstantFP::get(Ty, log(V));
+            return ConstantFoldFP(log, V, Ty);
           else if (Name == "log10" && V > 0)
             return ConstantFoldFP(log10, V, Ty);
           else if (Name == "llvm.sqrt.f32" || Name == "llvm.sqrt.f64") {
@@ -415,19 +426,19 @@
           break;
         case 's':
           if (Name == "sin")
-            return ConstantFP::get(Ty, sin(V));
+            return ConstantFoldFP(sin, V, Ty);
           else if (Name == "sinh")
-            return ConstantFP::get(Ty, sinh(V));
+            return ConstantFoldFP(sinh, V, Ty);
           else if (Name == "sqrt" && V >= 0)
-            return ConstantFP::get(Ty, sqrt(V));
+            return ConstantFoldFP(sqrt, V, Ty);
           else if (Name == "sqrtf" && V >= 0)
-            return ConstantFP::get(Ty, sqrt((float)V));
+            return ConstantFoldFP(sqrt, V, Ty);
           break;
         case 't':
           if (Name == "tan")
-            return ConstantFP::get(Ty, tan(V));
+            return ConstantFoldFP(tan, V, Ty);
           else if (Name == "tanh")
-            return ConstantFP::get(Ty, tanh(V));
+            return ConstantFoldFP(tanh, V, Ty);
           break;
         default:
           break;
@@ -453,17 +464,11 @@
         double Op2V = Op2->getValue();
 
         if (Name == "pow") {
-          errno = 0;
-          double V = pow(Op1V, Op2V);
-          if (errno == 0)
-            return ConstantFP::get(Ty, V);
+          return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
         } else if (Name == "fmod") {
-          errno = 0;
-          double V = fmod(Op1V, Op2V);
-          if (errno == 0)
-            return ConstantFP::get(Ty, V);
+          return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty);
         } else if (Name == "atan2") {
-          return ConstantFP::get(Ty, atan2(Op1V,Op2V));
+          return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
         }
       } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
         if (Name == "llvm.powi.f32") {





More information about the llvm-commits mailing list