Fix warning when assertions disabled.
[oota-llvm.git] / lib / VMCore / Instructions.cpp
index f33e2dd449ade0c6ac51fed99f8a80f30eea6554..3421292930d490f4995e436b2357ce21a35cf386 100644 (file)
@@ -12,7 +12,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/BasicBlock.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Function.h"
@@ -92,6 +91,13 @@ void CallSite::setDoesNotThrow(bool doesNotThrow) {
     cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
 }
 
+bool CallSite::hasArgument(const Value *Arg) const {
+  for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
+    if (AI->get() == Arg)
+      return true;
+  return false;
+}
+
 //===----------------------------------------------------------------------===//
 //                            TerminatorInst Class
 //===----------------------------------------------------------------------===//
@@ -124,7 +130,8 @@ PHINode::PHINode(const PHINode &PN)
 }
 
 PHINode::~PHINode() {
-  dropHungoffUses(OperandList);
+  if (OperandList)
+    dropHungoffUses(OperandList);
 }
 
 // removeIncomingValue - Remove an incoming value.  This is useful if a
@@ -993,24 +1000,29 @@ static unsigned retrieveAddrSpace(const Value *Val) {
   return cast<PointerType>(Val->getType())->getAddressSpace();
 }
 
-void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
+void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
+                            const std::string &Name) {
   assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
   Use *OL = OperandList;
   OL[0] = Ptr;
 
   for (unsigned i = 0; i != NumIdx; ++i)
     OL[i+1] = Idx[i];
+
+  setName(Name);
 }
 
-void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
+void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) {
   assert(NumOperands == 2 && "NumOperands not initialized?");
   Use *OL = OperandList;
   OL[0] = Ptr;
   OL[1] = Idx;
+
+  setName(Name);
 }
 
 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
-  : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
+  : Instruction(GEPI.getType(), GetElementPtr,
                 OperandTraits<GetElementPtrInst>::op_end(this)
                 - GEPI.getNumOperands(),
                 GEPI.getNumOperands()) {
@@ -1027,8 +1039,7 @@ GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
                 GetElementPtr,
                 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
                 2, InBe) {
-  init(Ptr, Idx);
-  setName(Name);
+  init(Ptr, Idx, Name);
 }
 
 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
@@ -1038,8 +1049,7 @@ GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
                 GetElementPtr,
                 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
                 2, IAE) {
-  init(Ptr, Idx);
-  setName(Name);
+  init(Ptr, Idx, Name);
 }
 
 // getIndexedType - Returns the type of the element that would be loaded with
@@ -1059,7 +1069,22 @@ const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
   if (NumIdx == 0)
     return Agg;
 
-  return ExtractValueInst::getIndexedType(Agg, Idxs+1, Idxs+NumIdx);
+  unsigned CurIdx = 1;
+  for (; CurIdx != NumIdx; ++CurIdx) {
+    const CompositeType *CT = dyn_cast<CompositeType>(Agg);
+    if (!CT || isa<PointerType>(CT)) return 0;
+    Value *Index = Idxs[CurIdx];
+    if (!CT->indexValid(Index)) return 0;
+    Agg = CT->getTypeAtIndex(Index);
+
+    // If the new type forwards to another type, then it is in the middle
+    // of being refined to another type (and hence, may have dropped all
+    // references to what it was using before).  So, use the new forwarded
+    // type.
+    if (const Type *Ty = Agg->getForwardedType())
+      Agg = Ty;
+  }
+  return CurIdx == NumIdx ? Agg : 0;
 }
 
 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
@@ -1336,64 +1361,78 @@ int ShuffleVectorInst::getMaskValue(unsigned i) const {
 //                             InsertValueInst Class
 //===----------------------------------------------------------------------===//
 
-void InsertValueInst::init(Value *Agg, Value *Val, Value* const *Idx, unsigned NumIdx) {
-  assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
-  Use *OL = OperandList;
-  OL[0] = Agg;
-  OL[1] = Val;
+void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx, 
+                           unsigned NumIdx, const std::string &Name) {
+  assert(NumOperands == 2 && "NumOperands not initialized?");
+  Op<0>() = Agg;
+  Op<1>() = Val;
 
-  for (unsigned i = 0; i != NumIdx; ++i)
-    OL[i+2] = Idx[i];
+  Indices.insert(Indices.end(), Idx, Idx + NumIdx);
+  setName(Name);
 }
 
-void InsertValueInst::init(Value *Agg, Value *Val, Value *Idx) {
-  assert(NumOperands == 3 && "NumOperands not initialized?");
-  Use *OL = OperandList;
-  OL[0] = Agg;
-  OL[1] = Val;
-  OL[2] = Idx;
+void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx, 
+                           const std::string &Name) {
+  assert(NumOperands == 2 && "NumOperands not initialized?");
+  Op<0>() = Agg;
+  Op<1>() = Val;
+
+  Indices.push_back(Idx);
+  setName(Name);
 }
 
 InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
-  : Instruction(reinterpret_cast<const Type*>(IVI.getType()), InsertValue,
-                OperandTraits<InsertValueInst>::op_end(this)
-                - IVI.getNumOperands(),
-                IVI.getNumOperands()) {
-  Use *OL = OperandList;
-  Use *IVIOL = IVI.OperandList;
-  for (unsigned i = 0, E = NumOperands; i != E; ++i)
-    OL[i] = IVIOL[i];
+  : Instruction(IVI.getType(), InsertValue,
+                OperandTraits<InsertValueInst>::op_begin(this), 2),
+    Indices(IVI.Indices) {
+  Op<0>() = IVI.getOperand(0);
+  Op<1>() = IVI.getOperand(1);
+}
+
+InsertValueInst::InsertValueInst(Value *Agg,
+                                 Value *Val,
+                                 unsigned Idx, 
+                                 const std::string &Name,
+                                 Instruction *InsertBefore)
+  : Instruction(Agg->getType(), InsertValue,
+                OperandTraits<InsertValueInst>::op_begin(this),
+                2, InsertBefore) {
+  init(Agg, Val, Idx, Name);
+}
+
+InsertValueInst::InsertValueInst(Value *Agg,
+                                 Value *Val,
+                                 unsigned Idx, 
+                                 const std::string &Name,
+                                 BasicBlock *InsertAtEnd)
+  : Instruction(Agg->getType(), InsertValue,
+                OperandTraits<InsertValueInst>::op_begin(this),
+                2, InsertAtEnd) {
+  init(Agg, Val, Idx, Name);
 }
 
 //===----------------------------------------------------------------------===//
 //                             ExtractValueInst Class
 //===----------------------------------------------------------------------===//
 
-void ExtractValueInst::init(Value *Agg, Value* const *Idx, unsigned NumIdx) {
-  assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
-  Use *OL = OperandList;
-  OL[0] = Agg;
+void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
+                           const std::string &Name) {
+  assert(NumOperands == 1 && "NumOperands not initialized?");
 
-  for (unsigned i = 0; i != NumIdx; ++i)
-    OL[i+1] = Idx[i];
+  Indices.insert(Indices.end(), Idx, Idx + NumIdx);
+  setName(Name);
 }
 
-void ExtractValueInst::init(Value *Agg, Value *Idx) {
-  assert(NumOperands == 2 && "NumOperands not initialized?");
-  Use *OL = OperandList;
-  OL[0] = Agg;
-  OL[1] = Idx;
+void ExtractValueInst::init(unsigned Idx, const std::string &Name) {
+  assert(NumOperands == 1 && "NumOperands not initialized?");
+
+  Indices.push_back(Idx);
+  setName(Name);
 }
 
 ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
-  : Instruction(reinterpret_cast<const Type*>(EVI.getType()), ExtractValue,
-                OperandTraits<ExtractValueInst>::op_end(this)
-                - EVI.getNumOperands(),
-                EVI.getNumOperands()) {
-  Use *OL = OperandList;
-  Use *EVIOL = EVI.OperandList;
-  for (unsigned i = 0, E = NumOperands; i != E; ++i)
-    OL[i] = EVIOL[i];
+  : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
+    Indices(EVI.Indices) {
 }
 
 // getIndexedType - Returns the type of the element that would be extracted
@@ -1403,13 +1442,13 @@ ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
 // pointer type.
 //
 const Type* ExtractValueInst::getIndexedType(const Type *Agg,
-                                             Value* const *Idxs,
+                                             const unsigned *Idxs,
                                              unsigned NumIdx) {
   unsigned CurIdx = 0;
   for (; CurIdx != NumIdx; ++CurIdx) {
     const CompositeType *CT = dyn_cast<CompositeType>(Agg);
-    if (!CT || isa<PointerType>(CT)) return 0;
-    Value *Index = Idxs[CurIdx];
+    if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
+    unsigned Index = Idxs[CurIdx];
     if (!CT->indexValid(Index)) return 0;
     Agg = CT->getTypeAtIndex(Index);
 
@@ -1423,6 +1462,29 @@ const Type* ExtractValueInst::getIndexedType(const Type *Agg,
   return CurIdx == NumIdx ? Agg : 0;
 }
 
+const Type* ExtractValueInst::getIndexedType(const Type *Agg,
+                                             unsigned Idx) {
+  return getIndexedType(Agg, &Idx, 1);
+}
+
+ExtractValueInst::ExtractValueInst(Value *Agg,
+                                   unsigned Idx,
+                                   const std::string &Name,
+                                   BasicBlock *InsertAtEnd)
+  : UnaryInstruction(checkType(getIndexedType(Agg->getType(), &Idx, 1)),
+                    ExtractValue, Agg, InsertAtEnd) {
+  init(Idx, Name);
+}
+
+ExtractValueInst::ExtractValueInst(Value *Agg,
+                                   unsigned Idx,
+                                   const std::string &Name,
+                                   Instruction *InsertBefore)
+  : UnaryInstruction(checkType(getIndexedType(Agg->getType(), &Idx, 1)),
+                    ExtractValue, Agg, InsertBefore) {
+  init(Idx, Name);
+}
+
 //===----------------------------------------------------------------------===//
 //                             BinaryOperator Class
 //===----------------------------------------------------------------------===//
@@ -2496,10 +2558,9 @@ bool CmpInst::isEquality() {
 }
 
 
-ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
+CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
   switch (pred) {
-    default:
-      assert(!"Unknown icmp predicate!");
+    default: assert(!"Unknown cmp predicate!");
     case ICMP_EQ: return ICMP_NE;
     case ICMP_NE: return ICMP_EQ;
     case ICMP_UGT: return ICMP_ULE;
@@ -2510,22 +2571,23 @@ ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
     case ICMP_SLT: return ICMP_SGE;
     case ICMP_SGE: return ICMP_SLT;
     case ICMP_SLE: return ICMP_SGT;
-  }
-}
 
-ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
-  switch (pred) {
-    default: assert(! "Unknown icmp predicate!");
-    case ICMP_EQ: case ICMP_NE:
-      return pred;
-    case ICMP_SGT: return ICMP_SLT;
-    case ICMP_SLT: return ICMP_SGT;
-    case ICMP_SGE: return ICMP_SLE;
-    case ICMP_SLE: return ICMP_SGE;
-    case ICMP_UGT: return ICMP_ULT;
-    case ICMP_ULT: return ICMP_UGT;
-    case ICMP_UGE: return ICMP_ULE;
-    case ICMP_ULE: return ICMP_UGE;
+    case FCMP_OEQ: return FCMP_UNE;
+    case FCMP_ONE: return FCMP_UEQ;
+    case FCMP_OGT: return FCMP_ULE;
+    case FCMP_OLT: return FCMP_UGE;
+    case FCMP_OGE: return FCMP_ULT;
+    case FCMP_OLE: return FCMP_UGT;
+    case FCMP_UEQ: return FCMP_ONE;
+    case FCMP_UNE: return FCMP_OEQ;
+    case FCMP_UGT: return FCMP_OLE;
+    case FCMP_ULT: return FCMP_OGE;
+    case FCMP_UGE: return FCMP_OLT;
+    case FCMP_ULE: return FCMP_OGT;
+    case FCMP_ORD: return FCMP_UNO;
+    case FCMP_UNO: return FCMP_ORD;
+    case FCMP_TRUE: return FCMP_FALSE;
+    case FCMP_FALSE: return FCMP_TRUE;
   }
 }
 
@@ -2601,32 +2663,20 @@ ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
   return ConstantRange(Lower, Upper);
 }
 
-FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
-  switch (pred) {
-    default:
-      assert(!"Unknown icmp predicate!");
-    case FCMP_OEQ: return FCMP_UNE;
-    case FCMP_ONE: return FCMP_UEQ;
-    case FCMP_OGT: return FCMP_ULE;
-    case FCMP_OLT: return FCMP_UGE;
-    case FCMP_OGE: return FCMP_ULT;
-    case FCMP_OLE: return FCMP_UGT;
-    case FCMP_UEQ: return FCMP_ONE;
-    case FCMP_UNE: return FCMP_OEQ;
-    case FCMP_UGT: return FCMP_OLE;
-    case FCMP_ULT: return FCMP_OGE;
-    case FCMP_UGE: return FCMP_OLT;
-    case FCMP_ULE: return FCMP_OGT;
-    case FCMP_ORD: return FCMP_UNO;
-    case FCMP_UNO: return FCMP_ORD;
-    case FCMP_TRUE: return FCMP_FALSE;
-    case FCMP_FALSE: return FCMP_TRUE;
-  }
-}
-
-FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
+CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
   switch (pred) {
-    default: assert(!"Unknown fcmp predicate!");
+    default: assert(!"Unknown cmp predicate!");
+    case ICMP_EQ: case ICMP_NE:
+      return pred;
+    case ICMP_SGT: return ICMP_SLT;
+    case ICMP_SLT: return ICMP_SGT;
+    case ICMP_SGE: return ICMP_SLE;
+    case ICMP_SLE: return ICMP_SGE;
+    case ICMP_UGT: return ICMP_ULT;
+    case ICMP_ULT: return ICMP_UGT;
+    case ICMP_UGE: return ICMP_ULE;
+    case ICMP_ULE: return ICMP_UGE;
+  
     case FCMP_FALSE: case FCMP_TRUE:
     case FCMP_OEQ: case FCMP_ONE:
     case FCMP_UEQ: case FCMP_UNE:
@@ -2870,10 +2920,10 @@ VICmpInst* VICmpInst::clone() const {
 }
 
 ExtractValueInst *ExtractValueInst::clone() const {
-  return new(getNumOperands()) ExtractValueInst(*this);
+  return new ExtractValueInst(*this);
 }
 InsertValueInst *InsertValueInst::clone() const {
-  return new(getNumOperands()) InsertValueInst(*this);
+  return new InsertValueInst(*this);
 }