Convert to SymbolTable's new iteration interface.
[oota-llvm.git] / lib / VMCore / Constants.cpp
index 21f19b98b11191703f695a48b9189693017d4974..a393be98ae3899434f3f245bf92aa13da27e999b 100644 (file)
@@ -313,7 +313,10 @@ ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
 /// specify the full Instruction::OPCODE identifier.
 ///
 Constant *ConstantExpr::getNeg(Constant *C) {
-  return get(Instruction::Sub, getNullValue(C->getType()), C);
+  if (!C->getType()->isFloatingPoint())
+    return get(Instruction::Sub, getNullValue(C->getType()), C);
+  else
+    return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
 }
 Constant *ConstantExpr::getNot(Constant *C) {
   assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
@@ -369,7 +372,17 @@ Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
   return get(Instruction::Shr, C1, C2);
 }
 
+Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
+  if (C1->getType()->isUnsigned()) return getShr(C1, C2);
+  return getCast(getShr(getCast(C1,
+                    C1->getType()->getUnsignedVersion()), C2), C1->getType());
+}
 
+Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
+  if (C1->getType()->isSigned()) return getShr(C1, C2);
+  return getCast(getShr(getCast(C1,
+                        C1->getType()->getSignedVersion()), C2), C1->getType());
+}
 
 
 //===----------------------------------------------------------------------===//
@@ -568,6 +581,14 @@ void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
   } else if (getOpcode() == Instruction::Cast) {
     assert(getOperand(0) == From && "Cast only has one use!");
     Replacement = ConstantExpr::getCast(To, getType());
+  } else if (getOpcode() == Instruction::Select) {
+    Constant *C1 = getOperand(0);
+    Constant *C2 = getOperand(1);
+    Constant *C3 = getOperand(2);
+    if (C1 == From) C1 = To;
+    if (C2 == From) C2 = To;
+    if (C3 == From) C3 = To;
+    Replacement = ConstantExpr::getSelect(C1, C2, C3);
   } else if (getNumOperands() == 2) {
     Constant *C1 = getOperand(0);
     Constant *C2 = getOperand(1);
@@ -1055,6 +1076,8 @@ namespace llvm {
            V.first < Instruction::BinaryOpsEnd) ||
           V.first == Instruction::Shl || V.first == Instruction::Shr)
         return new ConstantExpr(V.first, V.second[0], V.second[1]);
+      if (V.first == Instruction::Select)
+        return new ConstantExpr(V.second[0], V.second[1], V.second[2]);
       
       assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
       
@@ -1118,6 +1141,22 @@ Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
   return ExprConstants.getOrCreate(Ty, Key);
 }
 
+Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
+  assert(C->getType()->isInteger() && Ty->isInteger() &&
+         C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
+         "This is an illegal sign extension!");
+  C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
+  return ConstantExpr::getCast(C, Ty);
+}
+
+Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
+  assert(C->getType()->isInteger() && Ty->isInteger() &&
+         C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
+         "This is an illegal zero extension!");
+  C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
+  return ConstantExpr::getCast(C, Ty);
+}
+
 Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
                               Constant *C1, Constant *C2) {
   if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)