Start using the new function cloning header
[oota-llvm.git] / lib / VMCore / iOperators.cpp
index 956d7d5bdba38f23ceb219447d735483027abe94..5a88986135bd3082389d04dceef10b7e0b6d971d 100644 (file)
@@ -17,11 +17,36 @@ BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
                                const Type *Ty, const std::string &Name,
                                Instruction *InsertBefore)
   : Instruction(Ty, iType, Name, InsertBefore) {
+
   Operands.reserve(2);
   Operands.push_back(Use(S1, this));
   Operands.push_back(Use(S2, this));
-  assert(Operands[0] && Operands[1] && 
-         Operands[0]->getType() == Operands[1]->getType());
+  assert(S1 && S2 && S1->getType() == S2->getType());
+
+#ifndef NDEBUG
+  switch (iType) {
+  case Add: case Sub:
+  case Mul: case Div:
+  case Rem:
+    assert(Ty == S1->getType() &&
+           "Arithmetic operation should return same type as operands!");
+    assert((Ty->isInteger() || Ty->isFloatingPoint()) && 
+           "Tried to create an arithmetic operation on a non-arithmetic type!");
+    break;
+  case And: case Or:
+  case Xor:
+    assert(Ty == S1->getType() &&
+           "Logical operation should return same type as operands!");
+    assert(Ty->isIntegral() &&
+           "Tried to create an logical operation on a non-integral type!");
+    break;
+  case SetLT: case SetGT: case SetLE:
+  case SetGE: case SetEQ: case SetNE:
+    assert(Ty == Type::BoolTy && "Setcc must return bool!");
+  default:
+    break;
+  }
+#endif
 }
 
 
@@ -66,7 +91,7 @@ static inline bool isConstantAllOnes(const Value *V) {
 bool BinaryOperator::isNeg(const Value *V) {
   if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
     return Bop->getOpcode() == Instruction::Sub &&
-      isa<Constant>(Bop->getOperand(0)) && cast<Constant>(V)->isNullValue();
+      Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
   return false;
 }
 
@@ -108,21 +133,13 @@ const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
 // order dependant (SetLT f.e.) the opcode is changed.
 //
 bool BinaryOperator::swapOperands() {
-  if (SetCondInst *SCI = dyn_cast<SetCondInst>(this)) {
+  if (isCommutative())
+    ;  // If the instruction is commutative, it is safe to swap the operands
+  else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
     iType = SCI->getSwappedCondition();
-    std::swap(Operands[0], Operands[1]);
-    return false;
-  }
+  else
+    return true;   // Can't commute operands
 
-  switch (getOpcode()) {
-    // Instructions that don't need opcode modification
-  case Add: case Mul:
-  case And: case Xor:
-  case Or:
-    // Error on the side of caution
-  default:
-    return true;
-  }
   std::swap(Operands[0], Operands[1]);
   return false;
 }