Propagate debug loc info through prologue/epilogue.
[oota-llvm.git] / lib / Analysis / ConstantFolding.cpp
index 1a0e4b410c3a201dfbf0633e2b0de0b7272c70fc..d4457b30318fcd631bb45f8226cd327aad724253 100644 (file)
@@ -76,7 +76,7 @@ static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
         Offset += TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue());
       } else {
         const SequentialType *SQT = cast<SequentialType>(*GTI);
-        Offset += TD.getABITypeSize(SQT->getElementType())*CI->getSExtValue();
+        Offset += TD.getTypePaddedSize(SQT->getElementType())*CI->getSExtValue();
       }
     }
     return true;
@@ -87,7 +87,7 @@ static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
 
 
 /// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression.
-/// Attempt to symbolically evaluate the result of  a binary operator merging
+/// Attempt to symbolically evaluate the result of a binary operator merging
 /// these together.  If target data info is available, it is provided as TD, 
 /// otherwise TD is null.
 static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0,
@@ -113,7 +113,6 @@ static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0,
       }
   }
     
-  // TODO: Fold icmp setne/seteq as well.
   return 0;
 }
 
@@ -281,7 +280,7 @@ static Constant *FoldBitCast(Constant *C, const Type *DestTy,
 Constant *llvm::ConstantFoldInstruction(Instruction *I, const TargetData *TD) {
   if (PHINode *PN = dyn_cast<PHINode>(I)) {
     if (PN->getNumIncomingValues() == 0)
-      return Constant::getNullValue(PN->getType());
+      return UndefValue::get(PN->getType());
 
     Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0));
     if (Result == 0) return 0;
@@ -368,16 +367,31 @@ Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
       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);
+        if (TD->getPointerSizeInBits() < InWidth) {
+          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, DestTy, false);
       }
     }
     return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
   case Instruction::IntToPtr:
+    // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
+    // the int size is >= the ptr size.  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::PtrToInt &&
+          TD->getPointerSizeInBits() <=
+          CE->getType()->getPrimitiveSizeInBits()) {
+        Constant *Input = CE->getOperand(0);
+        Constant *C = FoldBitCast(Input, DestTy, *TD);
+        return C ? C : ConstantExpr::getBitCast(Input, DestTy);
+      }
+    }
+    return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
   case Instruction::Trunc:
   case Instruction::ZExt:
   case Instruction::SExt: