Implement constant folding of fmod, which is used a lot in povray
authorChris Lattner <sabre@nondot.org>
Thu, 27 May 2004 07:25:00 +0000 (07:25 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 27 May 2004 07:25:00 +0000 (07:25 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13823 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Utils/Local.cpp

index 1675f71610921065e7f998ac677ce793280fd651..256ddfdbccb39603f53404b8453379ad88faf7c1 100644 (file)
@@ -236,7 +236,7 @@ bool llvm::canConstantFoldCallTo(Function *F) {
   const std::string &Name = F->getName();
   return Name == "sin" || Name == "cos" || Name == "tan" || Name == "sqrt" ||
          Name == "log" || Name == "log10" || Name == "exp" || Name == "pow" ||
-         Name == "acos" || Name == "asin";
+         Name == "acos" || Name == "asin" || Name == "atan" || Name == "fmod";
 }
 
 static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
@@ -282,9 +282,16 @@ Constant *llvm::ConstantFoldCall(Function *F,
   } else if (Operands.size() == 2) {
     if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0]))
       if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
+        double Op1V = Op1->getValue(), Op2V = Op2->getValue();
+
         if (Name == "pow") {
           errno = 0;
-          double V = pow(Op1->getValue(), Op2->getValue());
+          double V = pow(Op1V, Op2V);
+          if (errno == 0)
+            return ConstantFP::get(Ty, V);
+        } else if (Name == "fmod") {
+          errno = 0;
+          double V = fmod(Op1V, Op2V);
           if (errno == 0)
             return ConstantFP::get(Ty, V);
         }