From 6575a1d134ca7b2fe55dabf43519f9bfa66564d6 Mon Sep 17 00:00:00 2001 From: "Vikram S. Adve" Date: Fri, 20 Jul 2001 21:07:06 +0000 Subject: [PATCH] Provide uniform access to the pointer operand and to the index 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 | 42 +++++++++++++++++++++++------- lib/VMCore/iMemory.cpp | 59 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 84 insertions(+), 17 deletions(-) diff --git a/include/llvm/iMemory.h b/include/llvm/iMemory.h index 92d6a6c80d4..f57a019af42 100644 --- a/include/llvm/iMemory.h +++ b/include/llvm/iMemory.h @@ -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 &Idx, + const string &Nam = "") + : Instruction(Ty, Opcode, Nam), + indexVec(Idx) + {} + +protected: + vector 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 &Indices, bool AllowStructLeaf = false); + + static unsigned int getIndexedOfsetForTarget(const Type *Ptr, + const vector &Indices, + const TargetMachine& targetMachine); + + const vector& 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 &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; }; diff --git a/lib/VMCore/iMemory.cpp b/lib/VMCore/iMemory.cpp index 2f2c6c115ff..9c52da3abde 100644 --- a/lib/VMCore/iMemory.cpp +++ b/lib/VMCore/iMemory.cpp @@ -17,14 +17,15 @@ // A null type is returned if the indices are invalid for the specified // pointer type. // -const Type *MemAccessInst::getIndexedType(const Type *Ptr, - const vector &Idx, - bool AllowStructLeaf = false) { +/* static */ +const Type* MemAccessInst::getIndexedType(const Type *Ptr, + const vector &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 &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 &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 &Idx, StoreInst::StoreInst(Value *Val, Value *Ptr, const vector &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)); -- 2.34.1