Don't waste as much horizontal space on #uses flag when printing
[oota-llvm.git] / lib / VMCore / iMemory.cpp
index c793783b2f850eef5b6bb49324271d5e798f9e88..c61961b58e3574028ee4aacc9f6d11e2333f867b 100644 (file)
@@ -5,7 +5,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/iMemory.h"
-#include "llvm/ConstPoolVals.h"
 
 //===----------------------------------------------------------------------===//
 //                        MemAccessInst Implementation
 // pointer type.
 //
 const Type* MemAccessInst::getIndexedType(const Type *Ptr, 
-                                         const vector<ConstPoolVal*> &Idx,
-                                         bool AllowStructLeaf = false) {
+                                         const vector<Value*> &Idx,
+                                         bool AllowCompositeLeaf = false) {
   if (!Ptr->isPointerType()) return 0;   // Type isn't a pointer type!
  
   // Get the type pointed to...
-  Ptr = ((const PointerType*)Ptr)->getValueType();
+  Ptr = cast<PointerType>(Ptr)->getElementType();
   
-  if (Ptr->isStructType()) {
-    unsigned CurIDX = 0;
-    while (Ptr->isStructType()) {
-      if (Idx.size() == CurIDX) 
-       return AllowStructLeaf ? Ptr : 0;   // Can't load a whole structure!?!?
-      if (Idx[CurIDX]->getType() != Type::UByteTy) return 0; // Illegal idx
-      unsigned NextIdx = ((ConstPoolUInt*)Idx[CurIDX++])->getValue();
-      
-      const StructType *ST = (const StructType *)Ptr;
-      Ptr = ST->getElementTypes()[NextIdx];
-    }
-    return Ptr;
-  } else if (Ptr->isArrayType()) {
-    assert(0 && "Loading from arrays not implemented yet!");
-  } else {
-    return (Idx.size() == 0) ? Ptr : 0;  // Load directly through ptr
+  unsigned CurIDX = 0;
+  while (const CompositeType *ST = dyn_cast<CompositeType>(Ptr)) {
+    if (Idx.size() == CurIDX)
+      return AllowCompositeLeaf ? Ptr : 0;   // Can't load a whole structure!?!?
+
+    Value *Index = Idx[CurIDX++];
+    if (!ST->indexValid(Index)) return 0;
+    Ptr = ST->getTypeAtIndex(Index);
   }
+  return CurIDX == Idx.size() ? Ptr : 0;
 }
 
-unsigned int
-MemAccessInst::getIndexedOfsetForTarget(const Type *Ptr, 
-                                       const vector<ConstPoolVal*> &Idx,
-                                       const TargetMachine& targetMachine)
-{
-  if (!Ptr->isPointerType())
-    return 0;                          // Type isn't a pointer type!
-  unsigned int curOffset = 0;
-  
-  // Get the type pointed to...
-  Ptr = ((const PointerType*) Ptr)->getValueType();
-  
-  if (Ptr->isStructType()) {
-    unsigned CurIDX = 0;               // which element of Idx vector
-    while (Ptr->isStructType()) {
-      const StructType * SPtr = (StructType *) Ptr;
-      
-      if (Idx.size() == CurIDX) 
-       break;
-      
-      assert (Idx[CurIDX]->getType() == Type::UByteTy && "Illegal struct idx");
-      unsigned NextIdx = ((ConstPoolUInt*)Idx[CurIDX++])->getValue();
-      
-      // add the offset for the current element
-      curOffset += SPtr->getElementOffset(NextIdx, targetMachine);
-      
-      // and update Ptr to refer to current element
-      Ptr = SPtr->getElementTypes()[NextIdx];
-    }
-    return curOffset;
-  } else if (Ptr->isArrayType()) {
-    assert(0 && "Loading from arrays not implemented yet!");
-  } else {
-    assert (Idx.size() == 0 && "Indexing type that is not struct or array?");
-    return 0;                          // Load directly through ptr
-  }
+
+#if 1
+#include "llvm/ConstantVals.h"
+const vector<Constant*> MemAccessInst::getIndicesBROKEN() const {
+  cerr << "MemAccessInst::getIndices() does not do what you want it to.  Talk"
+       << " to Chris about this.  We can phase it out after the paper.\n";
+
+  vector<Constant*> RetVal;
+
+  // THIS CODE WILL FAIL IF A NON CONSTANT INDEX IS USED AS AN ARRAY INDEX
+  // THIS IS WHY YOU SHOULD NOT USE THIS FUNCTION ANY MORE!!!
+  for (unsigned i = getFirstIndexOperandNumber(); i < getNumOperands(); ++i)
+    RetVal.push_back(cast<Constant>(getOperand(i)));
+
+  return RetVal;
 }
-  
+#endif
 
 //===----------------------------------------------------------------------===//
 //                           LoadInst Implementation
 //===----------------------------------------------------------------------===//
 
-LoadInst::LoadInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
+LoadInst::LoadInst(Value *Ptr, const vector<Value*> &Idx,
                   const string &Name = "")
-  : MemAccessInst(getIndexedType(Ptr->getType(), Idx), Load, Idx, Name) {
+  : MemAccessInst(getIndexedType(Ptr->getType(), Idx), Load, Name) {
   assert(getIndexedType(Ptr->getType(), Idx) && "Load operands invalid!");
   Operands.reserve(1+Idx.size());
   Operands.push_back(Use(Ptr, this));
@@ -100,14 +70,21 @@ LoadInst::LoadInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
   
 }
 
+LoadInst::LoadInst(Value *Ptr, const string &Name = "")
+  : MemAccessInst(cast<PointerType>(Ptr->getType())->getElementType(),
+                  Load, Name) {
+  Operands.reserve(1);
+  Operands.push_back(Use(Ptr, this));
+}
+
 
 //===----------------------------------------------------------------------===//
 //                           StoreInst Implementation
 //===----------------------------------------------------------------------===//
 
-StoreInst::StoreInst(Value *Val, Value *Ptr, const vector<ConstPoolVal*> &Idx,
+StoreInst::StoreInst(Value *Val, Value *Ptr, const vector<Value*> &Idx,
                     const string &Name = "")
-  : MemAccessInst(Type::VoidTy, Store, Idx, Name) {
+  : MemAccessInst(Type::VoidTy, Store, Name) {
   assert(getIndexedType(Ptr->getType(), Idx) && "Store operands invalid!");
   
   Operands.reserve(2+Idx.size());
@@ -118,17 +95,23 @@ StoreInst::StoreInst(Value *Val, Value *Ptr, const vector<ConstPoolVal*> &Idx,
     Operands.push_back(Use(Idx[i], this));
 }
 
+StoreInst::StoreInst(Value *Val, Value *Ptr, const string &Name = "")
+  : MemAccessInst(Type::VoidTy, Store, Name) {
+  
+  Operands.reserve(2);
+  Operands.push_back(Use(Val, this));
+  Operands.push_back(Use(Ptr, this));
+}
+
 
 //===----------------------------------------------------------------------===//
 //                       GetElementPtrInst Implementation
 //===----------------------------------------------------------------------===//
 
-GetElementPtrInst::GetElementPtrInst(Value *Ptr, 
-                                    const vector<ConstPoolVal*> &Idx,
+GetElementPtrInst::GetElementPtrInst(Value *Ptr, const vector<Value*> &Idx,
                                     const string &Name = "")
-  : MemAccessInst(PointerType::getPointerType(getIndexedType(Ptr->getType(),
-                                                            Idx, true)),
-                 GetElementPtr, Idx, Name) {
+  : MemAccessInst(PointerType::get(getIndexedType(Ptr->getType(), Idx, true)),
+                 GetElementPtr, Name) {
   assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!");
   Operands.reserve(1+Idx.size());
   Operands.push_back(Use(Ptr, this));
@@ -138,5 +121,5 @@ GetElementPtrInst::GetElementPtrInst(Value *Ptr,
 }
 
 bool GetElementPtrInst::isStructSelector() const {
-  return ((PointerType*)Operands[0]->getType())->getValueType()->isStructType();
+  return ((PointerType*)Operands[0]->getType())->getElementType()->isStructType();
 }