Implement support for inserting an instruction into a basic block right when it
authorChris Lattner <sabre@nondot.org>
Tue, 10 Sep 2002 15:36:11 +0000 (15:36 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 10 Sep 2002 15:36:11 +0000 (15:36 +0000)
is created.

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

include/llvm/InstrTypes.h
include/llvm/Instruction.h
include/llvm/iMemory.h
include/llvm/iOther.h
include/llvm/iPHINode.h
include/llvm/iTerminators.h

index 22039a9a011c793876b4c3b19de9564910e768b9..fc1917b10be2b14f8a20c887af88b5b7687797ae 100644 (file)
 ///
 class TerminatorInst : public Instruction {
 protected:
-  TerminatorInst(Instruction::TermOps iType);
+  TerminatorInst(Instruction::TermOps iType, Instruction *InsertBefore = 0);
   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
-                 const std::string &Name = "");
+                 const std::string &Name = "", Instruction *InsertBefore = 0)
+    : Instruction(Ty, iType, Name, InsertBefore) {
+  }
 public:
 
   /// Terminators must implement the methods required by Instruction...
@@ -59,23 +61,20 @@ public:
 
 class BinaryOperator : public Instruction {
 protected:
-  BinaryOperator(BinaryOps iType, Value *S1, Value *S2, 
-                 const std::string &Name = "") 
-    : Instruction(S1->getType(), iType, Name) {
-    Operands.reserve(2);
-    Operands.push_back(Use(S1, this));
-    Operands.push_back(Use(S2, this));
-    assert(Operands[0] && Operands[1] && 
-          Operands[0]->getType() == Operands[1]->getType());
-  }
+  BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
+                 const std::string &Name, Instruction *InsertBefore);
 
 public:
 
-  /// create() - Construct a binary instruction, given the opcode
-  /// and the two operands.
+  /// create() - Construct a binary instruction, given the opcode and the two
+  /// operands.  Optionally (if InstBefore is specified) insert the instruction
+  /// into a BasicBlock right before the specified instruction.  The specified
+  /// Instruction is allowed to be a dereferenced end iterator.
   ///
   static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
-                               const std::string &Name = "");
+                               const std::string &Name = "",
+                                Instruction *InsertBefore = 0);
+                               
 
   /// Helper functions to construct and inspect unary operations (NEG and NOT)
   /// via binary operators SUB and XOR:
@@ -83,8 +82,10 @@ public:
   /// createNeg, createNot - Create the NEG and NOT
   ///     instructions out of SUB and XOR instructions.
   ///
-  static BinaryOperator *createNeg(Value *Op, const std::string &Name = "");
-  static BinaryOperator *createNot(Value *Op, const std::string &Name = "");
+  static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
+                                   Instruction *InsertBefore = 0);
+  static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
+                                   Instruction *InsertBefore = 0);
 
   /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
   ///
index 8dd9f63b6939b75333597b2edb3dadecf32a5501..0674f0ed1ab34193f6170df3c3b7939ef486e081 100644 (file)
@@ -1,4 +1,4 @@
-//===-- llvm/Instruction.h - Instruction class definition --------*- C++ -*--=//
+//===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===//
 //
 // This file contains the declaration of the Instruction class, which is the
 // base class for all of the LLVM instructions.
@@ -25,8 +25,10 @@ class Instruction : public User {
   void setParent(BasicBlock *P);
 protected:
   unsigned iType;      // InstructionType: The opcode of the instruction
+
+  Instruction(const Type *Ty, unsigned iType, const std::string &Name = "",
+              Instruction *InsertBefore = 0);
 public:
-  Instruction(const Type *Ty, unsigned iType, const std::string &Name = "");
   virtual ~Instruction() {
     assert(Parent == 0 && "Instruction still embedded in basic block!");
   }
index ab1b593387ba66ce6513614697042e8b0186c702..e2a1e1bd64e46fd061f0316c8b4ee0700b6bf046 100644 (file)
@@ -21,7 +21,7 @@ class PointerType;
 class AllocationInst : public Instruction {
 protected:
   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, 
-                const std::string &Name = "");
+                const std::string &Name = "", Instruction *InsertBefore = 0);
 public:
 
   // isArrayAllocation - Return true if there is an allocation size parameter
@@ -64,8 +64,9 @@ public:
 //===----------------------------------------------------------------------===//
 
 struct MallocInst : public AllocationInst {
-  MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "")
-    : AllocationInst(Ty, ArraySize, Malloc, Name) {}
+  MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
+             Instruction *InsertBefore = 0)
+    : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
 
   virtual Instruction *clone() const { 
     return new MallocInst((Type*)getType(), (Value*)Operands[0].get());
@@ -87,8 +88,9 @@ struct MallocInst : public AllocationInst {
 //===----------------------------------------------------------------------===//
 
 struct AllocaInst : public AllocationInst {
-  AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "")
-    : AllocationInst(Ty, ArraySize, Alloca, Name) {}
+  AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
+             Instruction *InsertBefore = 0)
+    : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
 
   virtual Instruction *clone() const { 
     return new AllocaInst((Type*)getType(), (Value*)Operands[0].get());
@@ -110,7 +112,7 @@ struct AllocaInst : public AllocationInst {
 //===----------------------------------------------------------------------===//
 
 struct FreeInst : public Instruction {
-  FreeInst(Value *Ptr);
+  FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
 
   virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
 
@@ -137,7 +139,8 @@ class LoadInst : public Instruction {
     Operands.push_back(Use(LI.Operands[0], this));
   }
 public:
-  LoadInst(Value *Ptr, const std::string &Name = "");
+  LoadInst(Value *Ptr, const std::string &Name = "",
+           Instruction *InsertBefore = 0);
 
   virtual Instruction *clone() const { return new LoadInst(*this); }
 
@@ -166,7 +169,7 @@ class StoreInst : public Instruction {
     Operands.push_back(Use(SI.Operands[1], this));
   }
 public:
-  StoreInst(Value *Val, Value *Ptr);
+  StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore = 0);
   virtual Instruction *clone() const { return new StoreInst(*this); }
 
   virtual bool hasSideEffects() const { return true; }
@@ -198,7 +201,7 @@ class GetElementPtrInst : public Instruction {
   }
 public:
   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
-                   const std::string &Name = "");
+                   const std::string &Name = "", Instruction *InsertBefore =0);
   virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
   
   // getType - Overload to return most specific pointer type...
index 31c50fe32ee286da9371e2c3c8ff747989816086..b8a2ee433a04df6cda3098a741d5b6fb23504bad 100644 (file)
@@ -1,7 +1,7 @@
 //===-- llvm/iOther.h - "Other" instruction node definitions -----*- C++ -*--=//
 //
 // This file contains the declarations for instructions that fall into the 
-// grandios 'other' catagory...
+// grandiose 'other' catagory...
 //
 //===----------------------------------------------------------------------===//
 
 //                                 CastInst Class
 //===----------------------------------------------------------------------===//
 
-// CastInst - This class represents a cast from Operand[0] to the type of
-// the instruction (i->getType()).
-//
+/// CastInst - This class represents a cast from Operand[0] to the type of
+/// the instruction (i->getType()).
+///
 class CastInst : public Instruction {
   CastInst(const CastInst &CI) : Instruction(CI.getType(), Cast) {
     Operands.reserve(1);
     Operands.push_back(Use(CI.Operands[0], this));
   }
 public:
-  CastInst(Value *S, const Type *Ty, const std::string &Name = "")
-    : Instruction(Ty, Cast, Name) {
+  CastInst(Value *S, const Type *Ty, const std::string &Name = "",
+           Instruction *InsertBefore = 0)
+    : Instruction(Ty, Cast, Name, InsertBefore) {
     Operands.reserve(1);
     Operands.push_back(Use(S, this));
   }
@@ -43,13 +44,14 @@ public:
 
 
 //===----------------------------------------------------------------------===//
-//             Classes to function calls and method invocations
+//                                 CallInst Class
 //===----------------------------------------------------------------------===//
 
 class CallInst : public Instruction {
   CallInst(const CallInst &CI);
 public:
-  CallInst(Value *M, const std::vector<Value*> &Par, const std::string & = "");
+  CallInst(Value *F, const std::vector<Value*> &Par,
+           const std::string &Name = "", Instruction *InsertBefore = 0);
 
   virtual Instruction *clone() const { return new CallInst(*this); }
   bool hasSideEffects() const { return true; }
@@ -89,8 +91,9 @@ class ShiftInst : public Instruction {
     Operands.push_back(Use(SI.Operands[1], this));
   }
 public:
-  ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "")
-    : Instruction(S->getType(), Opcode, Name) {
+  ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
+            Instruction *InsertBefore = 0)
+    : Instruction(S->getType(), Opcode, Name, InsertBefore) {
     assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
     Operands.reserve(2);
     Operands.push_back(Use(S, this));
index 081c57a7d446573722a346d3b11f289adc885461..287ba90bfeff9401c1d62c5070d5d94cb6e42fa6 100644 (file)
@@ -21,26 +21,30 @@ class BasicBlock;
 class PHINode : public Instruction {
   PHINode(const PHINode &PN);
 public:
-  PHINode(const Type *Ty, const std::string &Name = "");
+  PHINode(const Type *Ty, const std::string &Name = "",
+          Instruction *InsertBefore = 0)
+    : Instruction(Ty, Instruction::PHINode, Name, InsertBefore) {
+  }
 
   virtual Instruction *clone() const { return new PHINode(*this); }
 
-  // getNumIncomingValues - Return the number of incoming edges the PHI node has
-  inline unsigned getNumIncomingValues() const { return Operands.size()/2; }
+  /// getNumIncomingValues - Return the number of incoming edges the PHI node
+  /// has
+  unsigned getNumIncomingValues() const { return Operands.size()/2; }
 
-  // getIncomingValue - Return incoming value #x
-  inline const Value *getIncomingValue(unsigned i) const {
+  /// getIncomingValue - Return incoming value #x
+  const Value *getIncomingValue(unsigned i) const {
     return Operands[i*2];
   }
-  inline Value *getIncomingValue(unsigned i) {
+  Value *getIncomingValue(unsigned i) {
     return Operands[i*2];
   }
-  inline void setIncomingValue(unsigned i, Value *V) {
+  void setIncomingValue(unsigned i, Value *V) {
     Operands[i*2] = V;
   }
 
-  // getIncomingBlock - Return incoming basic block #x
-  inline const BasicBlock *getIncomingBlock(unsigned i) const { 
+  /// getIncomingBlock - Return incoming basic block #x
+  const BasicBlock *getIncomingBlock(unsigned i) const { 
     return (const BasicBlock*)Operands[i*2+1].get();
   }
   inline BasicBlock *getIncomingBlock(unsigned i) { 
@@ -50,23 +54,23 @@ public:
     Operands[i*2+1] = (Value*)BB;
   }
 
-  // addIncoming - Add an incoming value to the end of the PHI list
+  /// addIncoming - Add an incoming value to the end of the PHI list
   void addIncoming(Value *D, BasicBlock *BB);
-
-  // removeIncomingValue - Remove an incoming value.  This is useful if a
-  // predecessor basic block is deleted.  The value removed is returned.
+  
+  /// removeIncomingValue - Remove an incoming value.  This is useful if a
+  /// predecessor basic block is deleted.  The value removed is returned.
   Value *removeIncomingValue(const BasicBlock *BB);
 
-  // getBasicBlockIndex - Return the first index of the specified basic 
-  // block in the value list for this PHI.  Returns -1 if no instance.
-  //
+  /// getBasicBlockIndex - Return the first index of the specified basic 
+  /// block in the value list for this PHI.  Returns -1 if no instance.
+  ///
   int getBasicBlockIndex(const BasicBlock *BB) const {
     for (unsigned i = 0; i < Operands.size()/2; ++i) 
       if (getIncomingBlock(i) == BB) return i;
     return -1;
   }
 
-  // Methods for support type inquiry through isa, cast, and dyn_cast:
+  /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const PHINode *) { return true; }
   static inline bool classof(const Instruction *I) {
     return I->getOpcode() == Instruction::PHINode; 
index 9a92ba0303bd5ab2910923992e370e014179247e..adf67abe45de5972df318f29d2512c9336cf5bdf 100644 (file)
@@ -1,9 +1,8 @@
-//===-- llvm/iTerminators.h - Termintator instruction nodes ------*- C++ -*--=//
+//===-- llvm/iTerminators.h - Termintator instruction nodes -----*- C++ -*-===//
 //
-// This file contains the declarations for all the subclasses of the
-// Instruction class, which is itself defined in the Instruction.h file.  In 
-// between these definitions and the Instruction class are classes that expose
-// the SSA properties of each instruction, and that form the SSA graph.
+// This file contains the declarations for all the subclasses of the Instruction
+// class which represent "terminator" instructions.  Terminator instructions are
+// the only instructions allowed and required to terminate a BasicBlock.
 //
 //===----------------------------------------------------------------------===//
 
 
 #include "llvm/InstrTypes.h"
 
-//===----------------------------------------------------------------------===//
-//         Classes to represent Basic Block "Terminator" instructions
-//===----------------------------------------------------------------------===//
-
-
 //===---------------------------------------------------------------------------
 // ReturnInst - Return a value (possibly void), from a method.  Execution does
 //              not continue in this method any longer.
@@ -30,7 +24,8 @@ class ReturnInst : public TerminatorInst {
     }
   }
 public:
-  ReturnInst(Value *RetVal = 0) : TerminatorInst(Instruction::Ret) {
+  ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
+    : TerminatorInst(Instruction::Ret, InsertBefore) {
     if (RetVal) {
       Operands.reserve(1);
       Operands.push_back(Use(RetVal, this));
@@ -74,7 +69,8 @@ class BranchInst : public TerminatorInst {
   BranchInst(const BranchInst &BI);
 public:
   // If cond = null, then is an unconditional br...
-  BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse = 0, Value *cond = 0);
+  BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse = 0, Value *cond = 0,
+             Instruction *InsertBefore = 0);
 
   virtual Instruction *clone() const { return new BranchInst(*this); }
 
@@ -84,7 +80,7 @@ public:
   inline const Value *getCondition() const {
     return isUnconditional() ? 0 : Operands[2].get();
   }
-  inline       Value *getCondition()       {
+  Value *getCondition() {
     return isUnconditional() ? 0 : Operands[2].get();
   }
 
@@ -133,7 +129,7 @@ class SwitchInst : public TerminatorInst {
   // Operand[2n+1] = BasicBlock to go to on match
   SwitchInst(const SwitchInst &RI);
 public:
-  SwitchInst(Value *Value, BasicBlock *Default);
+  SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0);
 
   virtual Instruction *clone() const { return new SwitchInst(*this); }
 
@@ -194,7 +190,8 @@ class InvokeInst : public TerminatorInst {
   InvokeInst(const InvokeInst &BI);
 public:
   InvokeInst(Value *Meth, BasicBlock *IfNormal, BasicBlock *IfException,
-            const std::vector<Value*> &Params, const std::string &Name = "");
+            const std::vector<Value*> &Params, const std::string &Name = "",
+             Instruction *InsertBefore = 0);
 
   virtual Instruction *clone() const { return new InvokeInst(*this); }