Update test case to reflect Dale's change.
[oota-llvm.git] / lib / Analysis / ConstantFolding.cpp
index 2422c4870aa31a0a7c7cbab8683d40e4236fd096..70ce34969c9183b9f9942847b8ffb076c87c6686 100644 (file)
@@ -217,6 +217,23 @@ Constant *llvm::ConstantFoldInstOperands(const Instruction* I,
   case Instruction::FCmp:
     return ConstantExpr::getCompare(cast<CmpInst>(I)->getPredicate(), Ops[0], 
                                     Ops[1]);
+  case Instruction::PtrToInt:
+    // If the input is a inttoptr, eliminate the pair.  This requires knowing
+    // the width of a pointer, so it can't be done in ConstantExpr::getCast.
+    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
+      if (TD && CE->getOpcode() == Instruction::IntToPtr) {
+        Constant *Input = CE->getOperand(0);
+        unsigned InWidth = Input->getType()->getPrimitiveSizeInBits();
+        Constant *Mask = 
+          ConstantInt::get(APInt::getLowBitsSet(InWidth,
+                                                TD->getPointerSizeInBits()));
+        Input = ConstantExpr::getAnd(Input, Mask);
+        // Do a zext or trunc to get to the dest size.
+        return ConstantExpr::getIntegerCast(Input, I->getType(), false);
+      }
+    }
+    // FALL THROUGH.
+  case Instruction::IntToPtr:
   case Instruction::Trunc:
   case Instruction::ZExt:
   case Instruction::SExt:
@@ -226,8 +243,6 @@ Constant *llvm::ConstantFoldInstOperands(const Instruction* I,
   case Instruction::SIToFP:
   case Instruction::FPToUI:
   case Instruction::FPToSI:
-  case Instruction::PtrToInt:
-  case Instruction::IntToPtr:
   case Instruction::BitCast:
     return ConstantExpr::getCast(Opc, Ops[0], DestTy);
   case Instruction::Select:
@@ -327,9 +342,9 @@ llvm::canConstantFoldCallTo(Function *F) {
   }
 
   const ValueName *NameVal = F->getValueName();
+  if (NameVal == 0) return false;
   const char *Str = NameVal->getKeyData();
   unsigned Len = NameVal->getKeyLength();
-  if (Len == 0) return false;
   
   // In these cases, the check of the length is required.  We don't want to
   // return true for a name like "cos\0blah" which strcmp would return equal to
@@ -414,6 +429,7 @@ static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
 Constant *
 llvm::ConstantFoldCall(Function *F, Constant** Operands, unsigned NumOperands) {
   const ValueName *NameVal = F->getValueName();
+  if (NameVal == 0) return 0;
   const char *Str = NameVal->getKeyData();
   unsigned Len = NameVal->getKeyLength();