From 5bf620748f7ae33f76f4643d45442209eb9b1793 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 10 Sep 2002 15:36:11 +0000 Subject: [PATCH] Implement support for inserting an instruction into a basic block right when it is created. llvm-svn: 3651 --- llvm/include/llvm/InstrTypes.h | 33 +++++++++++++-------------- llvm/include/llvm/Instruction.h | 6 +++-- llvm/include/llvm/iMemory.h | 21 ++++++++++-------- llvm/include/llvm/iOther.h | 23 ++++++++++--------- llvm/include/llvm/iPHINode.h | 38 ++++++++++++++++++-------------- llvm/include/llvm/iTerminators.h | 27 ++++++++++------------- 6 files changed, 79 insertions(+), 69 deletions(-) diff --git a/llvm/include/llvm/InstrTypes.h b/llvm/include/llvm/InstrTypes.h index 22039a9a011c..fc1917b10be2 100644 --- a/llvm/include/llvm/InstrTypes.h +++ b/llvm/include/llvm/InstrTypes.h @@ -20,9 +20,11 @@ /// 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. /// diff --git a/llvm/include/llvm/Instruction.h b/llvm/include/llvm/Instruction.h index 8dd9f63b6939..0674f0ed1ab3 100644 --- a/llvm/include/llvm/Instruction.h +++ b/llvm/include/llvm/Instruction.h @@ -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!"); } diff --git a/llvm/include/llvm/iMemory.h b/llvm/include/llvm/iMemory.h index ab1b593387ba..e2a1e1bd64e4 100644 --- a/llvm/include/llvm/iMemory.h +++ b/llvm/include/llvm/iMemory.h @@ -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 &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... diff --git a/llvm/include/llvm/iOther.h b/llvm/include/llvm/iOther.h index 31c50fe32ee2..b8a2ee433a04 100644 --- a/llvm/include/llvm/iOther.h +++ b/llvm/include/llvm/iOther.h @@ -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... // //===----------------------------------------------------------------------===// @@ -14,17 +14,18 @@ // 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 &Par, const std::string & = ""); + CallInst(Value *F, const std::vector &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)); diff --git a/llvm/include/llvm/iPHINode.h b/llvm/include/llvm/iPHINode.h index 081c57a7d446..287ba90bfeff 100644 --- a/llvm/include/llvm/iPHINode.h +++ b/llvm/include/llvm/iPHINode.h @@ -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; diff --git a/llvm/include/llvm/iTerminators.h b/llvm/include/llvm/iTerminators.h index 9a92ba0303bd..adf67abe45de 100644 --- a/llvm/include/llvm/iTerminators.h +++ b/llvm/include/llvm/iTerminators.h @@ -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. // //===----------------------------------------------------------------------===// @@ -12,11 +11,6 @@ #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 &Params, const std::string &Name = ""); + const std::vector &Params, const std::string &Name = "", + Instruction *InsertBefore = 0); virtual Instruction *clone() const { return new InvokeInst(*this); }