float->int conversion rounds toward 0. Duh.
[oota-llvm.git] / lib / VMCore / ConstantFold.cpp
index 5c80a377ba7b25fecfe65206309ab93b343c69b4..d8b8566193aedd95f6fe1d89844c8e443e1246fc 100644 (file)
@@ -178,30 +178,28 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
   case Instruction::FPTrunc:
   case Instruction::FPExt:
     if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
-       APFloat Val = FPC->getValueAPF();
-      Val.convert(DestTy==Type::FloatTy ? APFloat::IEEEsingle : 
-                                          APFloat::IEEEdouble, 
+      APFloat Val = FPC->getValueAPF();
+      Val.convert(DestTy == Type::FloatTy ? APFloat::IEEEsingle :
+                  DestTy == Type::DoubleTy ? APFloat::IEEEdouble :
+                  DestTy == Type::X86_FP80Ty ? APFloat::x87DoubleExtended :
+                  DestTy == Type::FP128Ty ? APFloat::IEEEquad :
+                  APFloat::Bogus,
                   APFloat::rmNearestTiesToEven);
       return ConstantFP::get(DestTy, Val);
     }
     return 0; // Can't fold.
   case Instruction::FPToUI: 
-    if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
-      APFloat V = FPC->getValueAPF();
-      bool isDouble = &V.getSemantics()==&APFloat::IEEEdouble;
-      uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
-      APInt Val(APIntOps::RoundDoubleToAPInt(isDouble ? V.convertToDouble() : 
-                                   (double)V.convertToFloat(), DestBitWidth));
-      return ConstantInt::get(Val);
-    }
-    return 0; // Can't fold.
   case Instruction::FPToSI:
     if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
       APFloat V = FPC->getValueAPF();
-      bool isDouble = &V.getSemantics()==&APFloat::IEEEdouble;
+      uint64_t x[2]; 
       uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
-      APInt Val(APIntOps::RoundDoubleToAPInt(isDouble ? V.convertToDouble() :
-                                    (double)V.convertToFloat(), DestBitWidth));
+      APFloat::opStatus status = V.convertToInteger(x, DestBitWidth, 
+                             opc==Instruction::FPToSI,
+                             APFloat::rmTowardZero);
+      if (status!=APFloat::opOK && status!=APFloat::opInexact)
+        return 0; // give up
+      APInt Val(DestBitWidth, 2, x);
       return ConstantInt::get(Val);
     }
     return 0; // Can't fold.
@@ -215,11 +213,13 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
     return 0;                   // Other pointer types cannot be casted
   case Instruction::UIToFP:
     if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
+      double d = CI->getValue().roundToDouble();
       if (DestTy==Type::FloatTy) 
-        return ConstantFP::get(DestTy, 
-                            APFloat((float)CI->getValue().roundToDouble()));
+        return ConstantFP::get(DestTy, APFloat((float)d));
+      else if (DestTy==Type::DoubleTy)
+        return ConstantFP::get(DestTy, APFloat(d));
       else
-        return ConstantFP::get(DestTy, APFloat(CI->getValue().roundToDouble()));
+        return 0;     // FIXME do this for long double
     }
     return 0;
   case Instruction::SIToFP:
@@ -227,8 +227,10 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
       double d = CI->getValue().signedRoundToDouble();
       if (DestTy==Type::FloatTy)
         return ConstantFP::get(DestTy, APFloat((float)d));
-      else
+      else if (DestTy==Type::DoubleTy)
         return ConstantFP::get(DestTy, APFloat(d));
+      else
+        return 0;     // FIXME do this for long double
     }
     return 0;
   case Instruction::ZExt:
@@ -697,23 +699,6 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
         (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
         return ConstantFP::get(CFP1->getType(), C3V);
       case Instruction::FDiv:
-        // FIXME better to look at the return code
-        if (C2V.isZero())
-          if (C1V.isZero())
-            // IEEE 754, Section 7.1, #4
-            return ConstantFP::get(CFP1->getType(), isDouble ?
-                            APFloat(std::numeric_limits<double>::quiet_NaN()) :
-                            APFloat(std::numeric_limits<float>::quiet_NaN()));
-          else if (C2V.isNegZero() || C1V.isNegative())
-            // IEEE 754, Section 7.2, negative infinity case
-            return ConstantFP::get(CFP1->getType(), isDouble ?
-                            APFloat(-std::numeric_limits<double>::infinity()) :
-                            APFloat(-std::numeric_limits<float>::infinity()));
-          else
-            // IEEE 754, Section 7.2, positive infinity case
-            return ConstantFP::get(CFP1->getType(), isDouble ?
-                            APFloat(std::numeric_limits<double>::infinity()) :
-                            APFloat(std::numeric_limits<float>::infinity()));
         (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
         return ConstantFP::get(CFP1->getType(), C3V);
       case Instruction::FRem:
@@ -1124,7 +1109,8 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
   // icmp eq/ne(null,GV) -> false/true
   if (C1->isNullValue()) {
     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
-      if (!GV->hasExternalWeakLinkage()) // External weak GV can be null
+      // Don't try to evaluate aliases.  External weak GV can be null.
+      if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage())
         if (pred == ICmpInst::ICMP_EQ)
           return ConstantInt::getFalse();
         else if (pred == ICmpInst::ICMP_NE)
@@ -1132,7 +1118,8 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
   // icmp eq/ne(GV,null) -> false/true
   } else if (C2->isNullValue()) {
     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
-      if (!GV->hasExternalWeakLinkage()) // External weak GV can be null
+      // Don't try to evaluate aliases.  External weak GV can be null.
+      if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage())
         if (pred == ICmpInst::ICMP_EQ)
           return ConstantInt::getFalse();
         else if (pred == ICmpInst::ICMP_NE)