Provide uniform access to the pointer operand and to the index
authorVikram S. Adve <vadve@cs.uiuc.edu>
Fri, 20 Jul 2001 21:07:06 +0000 (21:07 +0000)
committerVikram S. Adve <vadve@cs.uiuc.edu>
Fri, 20 Jul 2001 21:07:06 +0000 (21:07 +0000)
operands (if any) for different types of MemAccessInst's.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/iMemory.h
lib/VMCore/iMemory.cpp

index 92d6a6c80d4a99f5ec8497ce0ee1dfada80c4523..f57a019af42e4ee6c087585270779a130a95e9e7 100644 (file)
@@ -109,8 +109,16 @@ public:
 //
 class MemAccessInst : public Instruction {
 protected:
-  inline MemAccessInst(const Type *Ty, unsigned Opcode, const string &Nam = "") 
-    : Instruction(Ty, Opcode, Nam) {}
+  inline MemAccessInst(const Type *Ty, unsigned Opcode,
+                      const vector<ConstPoolVal*> &Idx,
+                      const string &Nam = "")
+    : Instruction(Ty, Opcode, Nam),
+      indexVec(Idx)
+  {}
+  
+protected:
+  vector<ConstPoolVal*> indexVec;
+  
 public:
   // getIndexedType - Returns the type of the element that would be loaded with
   // a load instruction with the specified parameters.
@@ -121,6 +129,16 @@ public:
   static const Type *getIndexedType(const Type *Ptr, 
                                    const vector<ConstPoolVal*> &Indices,
                                    bool AllowStructLeaf = false);
+  
+  static unsigned int getIndexedOfsetForTarget(const Type *Ptr, 
+                                         const vector<ConstPoolVal*> &Indices,
+                                         const TargetMachine& targetMachine);
+  
+  const vector<ConstPoolVal*>& getIndexVec() const { return indexVec; }
+  
+  virtual Value* getPtrOperand() = 0;
+  
+  virtual int  getFirstOffsetIdx() const = 0;
 };
 
 
@@ -129,7 +147,7 @@ public:
 //===----------------------------------------------------------------------===//
 
 class LoadInst : public MemAccessInst {
-  LoadInst(const LoadInst &LI) : MemAccessInst(LI.getType(), Load) {
+  LoadInst(const LoadInst &LI) : MemAccessInst(LI.getType(), Load, LI.getIndexVec()) {
     Operands.reserve(LI.Operands.size());
     for (unsigned i = 0, E = LI.Operands.size(); i != E; ++i)
       Operands.push_back(Use(LI.Operands[i], this));
@@ -137,8 +155,10 @@ class LoadInst : public MemAccessInst {
 public:
   LoadInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
           const string &Name = "");
-  virtual Instruction *clone() const { return new LoadInst(*this); }
-  virtual const char *getOpcodeName() const { return "load"; }  
+  virtual Instruction* clone() const { return new LoadInst(*this); }
+  virtual const char*  getOpcodeName() const { return "load"; }  
+  virtual Value*       getPtrOperand() { return this->getOperand(0); }
+  virtual int getFirstOffsetIdx() const { return (this->getNumOperands() > 1)? 1 : -1;}
 };
 
 
@@ -147,7 +167,7 @@ public:
 //===----------------------------------------------------------------------===//
 
 class StoreInst : public MemAccessInst {
-  StoreInst(const StoreInst &SI) : MemAccessInst(SI.getType(), Store) {
+  StoreInst(const StoreInst &SI) : MemAccessInst(SI.getType(), Store, SI.getIndexVec()) {
     Operands.reserve(SI.Operands.size());
     for (unsigned i = 0, E = SI.Operands.size(); i != E; ++i)
       Operands.push_back(Use(SI.Operands[i], this));
@@ -157,8 +177,10 @@ public:
            const string &Name = "");
   virtual Instruction *clone() const { return new StoreInst(*this); }
   virtual const char *getOpcodeName() const { return "store"; }  
-
+  
   virtual bool hasSideEffects() const { return true; }
+  virtual Value*       getPtrOperand() { return this->getOperand(1); }
+  virtual int getFirstOffsetIdx() const { return (this->getNumOperands() > 2)? 2 : -1;}
 };
 
 
@@ -168,7 +190,7 @@ public:
 
 class GetElementPtrInst : public MemAccessInst {
   GetElementPtrInst(const GetElementPtrInst &EPI)
-    : MemAccessInst(EPI.getType(), GetElementPtr) {
+    : MemAccessInst(EPI.getType(), GetElementPtr, EPI.getIndexVec()) {
     Operands.reserve(EPI.Operands.size());
     for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
       Operands.push_back(Use(EPI.Operands[i], this));
@@ -178,7 +200,9 @@ public:
                    const string &Name = "");
   virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
   virtual const char *getOpcodeName() const { return "getelementptr"; }  
-
+  virtual Value*       getPtrOperand() { return this->getOperand(0); }
+  virtual int getFirstOffsetIdx() const { return (this->getNumOperands() > 1)? 1 : -1;}
+  
   inline bool isArraySelector() const { return !isStructSelector(); }
   bool isStructSelector() const;
 };
index 2f2c6c115ffd82315c7105be5ee94c5faecd4ef3..9c52da3abdedc00bca3fa5c7f5ae976bfbeb492a 100644 (file)
 // A null type is returned if the indices are invalid for the specified 
 // pointer type.
 //
-const Type *MemAccessInst::getIndexedType(const Type *Ptr, 
-                                         const vector<ConstPoolVal*> &Idx,
-                                         bool AllowStructLeaf = false) {
+/* static */
+const Type* MemAccessInst::getIndexedType(const Type *Ptr, 
+                                       const vector<ConstPoolVal*> &Idx,
+                                       bool AllowStructLeaf = false) {
   if (!Ptr->isPointerType()) return 0;   // Type isn't a pointer type!
  
   // Get the type pointed to...
   Ptr = ((const PointerType*)Ptr)->getValueType();
-
+  
   if (Ptr->isStructType()) {
     unsigned CurIDX = 0;
     while (Ptr->isStructType()) {
@@ -44,6 +45,46 @@ const Type *MemAccessInst::getIndexedType(const Type *Ptr,
   }
 }
 
+/* static */
+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
+  }
+}
+  
 
 //===----------------------------------------------------------------------===//
 //                           LoadInst Implementation
@@ -51,13 +92,15 @@ const Type *MemAccessInst::getIndexedType(const Type *Ptr,
 
 LoadInst::LoadInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
                   const string &Name = "")
-  : MemAccessInst(getIndexedType(Ptr->getType(), Idx), Load, Name) {
+  : MemAccessInst(getIndexedType(Ptr->getType(), Idx), Load, Idx, Name)
+{
   assert(getIndexedType(Ptr->getType(), Idx) && "Load operands invalid!");
   Operands.reserve(1+Idx.size());
   Operands.push_back(Use(Ptr, this));
-
+  
   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
     Operands.push_back(Use(Idx[i], this));
+  
 }
 
 
@@ -67,7 +110,7 @@ LoadInst::LoadInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
 
 StoreInst::StoreInst(Value *Val, Value *Ptr, const vector<ConstPoolVal*> &Idx,
                     const string &Name = "")
-  : MemAccessInst(Type::VoidTy, Store, Name) {
+  : MemAccessInst(Type::VoidTy, Store, Idx, Name) {
   assert(getIndexedType(Ptr->getType(), Idx) && "Store operands invalid!");
   
   Operands.reserve(2+Idx.size());
@@ -88,7 +131,7 @@ GetElementPtrInst::GetElementPtrInst(Value *Ptr,
                                     const string &Name = "")
   : MemAccessInst(PointerType::getPointerType(getIndexedType(Ptr->getType(),
                                                             Idx, true)),
-                 GetElementPtr, Name) {
+                 GetElementPtr, Idx, Name) {
   assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!");
   Operands.reserve(1+Idx.size());
   Operands.push_back(Use(Ptr, this));