add direct support for making GEP instrs with one index
authorChris Lattner <sabre@nondot.org>
Tue, 3 May 2005 05:43:30 +0000 (05:43 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 3 May 2005 05:43:30 +0000 (05:43 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@21665 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 0981d6bb2906c910a74ed5478b08dca771526090..06d0490725b858f7fd83e1b6b0748b4a34504828 100644 (file)
@@ -300,6 +300,7 @@ class GetElementPtrInst : public Instruction {
   }
   void init(Value *Ptr, const std::vector<Value*> &Idx);
   void init(Value *Ptr, Value *Idx0, Value *Idx1);
+  void init(Value *Ptr, Value *Idx);
 public:
   /// Constructors - Create a getelementptr instruction with a base pointer an
   /// list of indices.  The first ctor can optionally insert before an existing
@@ -310,8 +311,12 @@ public:
   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
                     const std::string &Name, BasicBlock *InsertAtEnd);
 
-  /// Constructors - These two constructors are convenience methods because two
-  /// index getelementptr instructions are so common.
+  /// Constructors - These two constructors are convenience methods because one
+  /// and two index getelementptr instructions are so common.
+  GetElementPtrInst(Value *Ptr, Value *Idx,
+                    const std::string &Name = "", Instruction *InsertBefore =0);
+  GetElementPtrInst(Value *Ptr, Value *Idx,
+                    const std::string &Name, BasicBlock *InsertAtEnd);
   GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
                     const std::string &Name = "", Instruction *InsertBefore =0);
   GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
@@ -336,6 +341,7 @@ public:
                                     bool AllowStructLeaf = false);
   static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
                                     bool AllowStructLeaf = false);
+  static const Type *getIndexedType(const Type *Ptr, Value *Idx);
 
   inline op_iterator       idx_begin()       { return op_begin()+1; }
   inline const_op_iterator idx_begin() const { return op_begin()+1; }
index c2e7254e93a8092fc33a8a4c1362f7c3d99f1d82..8404e0bad8eafbeb4a167681e02847f3c3455751 100644 (file)
@@ -603,6 +603,13 @@ void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
   OL[2].init(Idx1, this);
 }
 
+void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
+  NumOperands = 2;
+  Use *OL = OperandList = new Use[2];
+  OL[0].init(Ptr, this);
+  OL[1].init(Idx, this);
+}
+
 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
                                      const std::string &Name, Instruction *InBe)
   : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
@@ -619,6 +626,20 @@ GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
   init(Ptr, Idx);
 }
 
+GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
+                                     const std::string &Name, Instruction *InBe)
+  : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
+                GetElementPtr, 0, 0, Name, InBe) {
+  init(Ptr, Idx);
+}
+
+GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
+                                     const std::string &Name, BasicBlock *IAE)
+  : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
+                GetElementPtr, 0, 0, Name, IAE) {
+  init(Ptr, Idx);
+}
+
 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
                                      const std::string &Name, Instruction *InBe)
   : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
@@ -700,6 +721,16 @@ const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
   return 0;
 }
 
+const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
+  const PointerType *PTy = dyn_cast<PointerType>(Ptr);
+  if (!PTy) return 0;   // Type isn't a pointer type!
+
+  // Check the pointer index.
+  if (!PTy->indexValid(Idx)) return 0;
+
+  return PTy;
+}
+
 //===----------------------------------------------------------------------===//
 //                             BinaryOperator Class
 //===----------------------------------------------------------------------===//