Implement support for inserting an instruction into a basic block right when it

is created.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@3651 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2002-09-10 15:36:11 +00:00
parent 74493a4c6e
commit 3a8b0f00e6
6 changed files with 79 additions and 69 deletions

View File

@ -20,9 +20,11 @@
/// ///
class TerminatorInst : public Instruction { class TerminatorInst : public Instruction {
protected: protected:
TerminatorInst(Instruction::TermOps iType); TerminatorInst(Instruction::TermOps iType, Instruction *InsertBefore = 0);
TerminatorInst(const Type *Ty, Instruction::TermOps iType, TerminatorInst(const Type *Ty, Instruction::TermOps iType,
const std::string &Name = ""); const std::string &Name = "", Instruction *InsertBefore = 0)
: Instruction(Ty, iType, Name, InsertBefore) {
}
public: public:
/// Terminators must implement the methods required by Instruction... /// Terminators must implement the methods required by Instruction...
@ -59,23 +61,20 @@ public:
class BinaryOperator : public Instruction { class BinaryOperator : public Instruction {
protected: protected:
BinaryOperator(BinaryOps iType, Value *S1, Value *S2, BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
const std::string &Name = "") const std::string &Name, Instruction *InsertBefore);
: 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());
}
public: public:
/// create() - Construct a binary instruction, given the opcode /// create() - Construct a binary instruction, given the opcode and the two
/// and the two operands. /// 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, 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) /// Helper functions to construct and inspect unary operations (NEG and NOT)
/// via binary operators SUB and XOR: /// via binary operators SUB and XOR:
@ -83,8 +82,10 @@ public:
/// createNeg, createNot - Create the NEG and NOT /// createNeg, createNot - Create the NEG and NOT
/// instructions out of SUB and XOR instructions. /// instructions out of SUB and XOR instructions.
/// ///
static BinaryOperator *createNeg(Value *Op, const std::string &Name = ""); static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
static BinaryOperator *createNot(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. /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
/// ///

View 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 // This file contains the declaration of the Instruction class, which is the
// base class for all of the LLVM instructions. // base class for all of the LLVM instructions.
@ -25,8 +25,10 @@ class Instruction : public User {
void setParent(BasicBlock *P); void setParent(BasicBlock *P);
protected: protected:
unsigned iType; // InstructionType: The opcode of the instruction unsigned iType; // InstructionType: The opcode of the instruction
Instruction(const Type *Ty, unsigned iType, const std::string &Name = "",
Instruction *InsertBefore = 0);
public: public:
Instruction(const Type *Ty, unsigned iType, const std::string &Name = "");
virtual ~Instruction() { virtual ~Instruction() {
assert(Parent == 0 && "Instruction still embedded in basic block!"); assert(Parent == 0 && "Instruction still embedded in basic block!");
} }

View File

@ -21,7 +21,7 @@ class PointerType;
class AllocationInst : public Instruction { class AllocationInst : public Instruction {
protected: protected:
AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
const std::string &Name = ""); const std::string &Name = "", Instruction *InsertBefore = 0);
public: public:
// isArrayAllocation - Return true if there is an allocation size parameter // isArrayAllocation - Return true if there is an allocation size parameter
@ -64,8 +64,9 @@ public:
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
struct MallocInst : public AllocationInst { struct MallocInst : public AllocationInst {
MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "") MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
: AllocationInst(Ty, ArraySize, Malloc, Name) {} Instruction *InsertBefore = 0)
: AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
virtual Instruction *clone() const { virtual Instruction *clone() const {
return new MallocInst((Type*)getType(), (Value*)Operands[0].get()); return new MallocInst((Type*)getType(), (Value*)Operands[0].get());
@ -87,8 +88,9 @@ struct MallocInst : public AllocationInst {
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
struct AllocaInst : public AllocationInst { struct AllocaInst : public AllocationInst {
AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "") AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
: AllocationInst(Ty, ArraySize, Alloca, Name) {} Instruction *InsertBefore = 0)
: AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
virtual Instruction *clone() const { virtual Instruction *clone() const {
return new AllocaInst((Type*)getType(), (Value*)Operands[0].get()); return new AllocaInst((Type*)getType(), (Value*)Operands[0].get());
@ -110,7 +112,7 @@ struct AllocaInst : public AllocationInst {
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
struct FreeInst : public Instruction { struct FreeInst : public Instruction {
FreeInst(Value *Ptr); FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
virtual Instruction *clone() const { return new FreeInst(Operands[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)); Operands.push_back(Use(LI.Operands[0], this));
} }
public: 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); } virtual Instruction *clone() const { return new LoadInst(*this); }
@ -166,7 +169,7 @@ class StoreInst : public Instruction {
Operands.push_back(Use(SI.Operands[1], this)); Operands.push_back(Use(SI.Operands[1], this));
} }
public: public:
StoreInst(Value *Val, Value *Ptr); StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore = 0);
virtual Instruction *clone() const { return new StoreInst(*this); } virtual Instruction *clone() const { return new StoreInst(*this); }
virtual bool hasSideEffects() const { return true; } virtual bool hasSideEffects() const { return true; }
@ -198,7 +201,7 @@ class GetElementPtrInst : public Instruction {
} }
public: public:
GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, 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); } virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
// getType - Overload to return most specific pointer type... // getType - Overload to return most specific pointer type...

View File

@ -1,7 +1,7 @@
//===-- llvm/iOther.h - "Other" instruction node definitions -----*- C++ -*--=// //===-- llvm/iOther.h - "Other" instruction node definitions -----*- C++ -*--=//
// //
// This file contains the declarations for instructions that fall into the // This file contains the declarations for instructions that fall into the
// grandios 'other' catagory... // grandiose 'other' catagory...
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -14,17 +14,18 @@
// CastInst Class // CastInst Class
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// CastInst - This class represents a cast from Operand[0] to the type of /// CastInst - This class represents a cast from Operand[0] to the type of
// the instruction (i->getType()). /// the instruction (i->getType()).
// ///
class CastInst : public Instruction { class CastInst : public Instruction {
CastInst(const CastInst &CI) : Instruction(CI.getType(), Cast) { CastInst(const CastInst &CI) : Instruction(CI.getType(), Cast) {
Operands.reserve(1); Operands.reserve(1);
Operands.push_back(Use(CI.Operands[0], this)); Operands.push_back(Use(CI.Operands[0], this));
} }
public: public:
CastInst(Value *S, const Type *Ty, const std::string &Name = "") CastInst(Value *S, const Type *Ty, const std::string &Name = "",
: Instruction(Ty, Cast, Name) { Instruction *InsertBefore = 0)
: Instruction(Ty, Cast, Name, InsertBefore) {
Operands.reserve(1); Operands.reserve(1);
Operands.push_back(Use(S, this)); Operands.push_back(Use(S, this));
} }
@ -43,13 +44,14 @@ public:
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// Classes to function calls and method invocations // CallInst Class
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
class CallInst : public Instruction { class CallInst : public Instruction {
CallInst(const CallInst &CI); CallInst(const CallInst &CI);
public: 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); } virtual Instruction *clone() const { return new CallInst(*this); }
bool hasSideEffects() const { return true; } bool hasSideEffects() const { return true; }
@ -89,8 +91,9 @@ class ShiftInst : public Instruction {
Operands.push_back(Use(SI.Operands[1], this)); Operands.push_back(Use(SI.Operands[1], this));
} }
public: public:
ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "") ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
: Instruction(S->getType(), Opcode, Name) { Instruction *InsertBefore = 0)
: Instruction(S->getType(), Opcode, Name, InsertBefore) {
assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!"); assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
Operands.reserve(2); Operands.reserve(2);
Operands.push_back(Use(S, this)); Operands.push_back(Use(S, this));

View File

@ -21,26 +21,30 @@ class BasicBlock;
class PHINode : public Instruction { class PHINode : public Instruction {
PHINode(const PHINode &PN); PHINode(const PHINode &PN);
public: 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); } virtual Instruction *clone() const { return new PHINode(*this); }
// getNumIncomingValues - Return the number of incoming edges the PHI node has /// getNumIncomingValues - Return the number of incoming edges the PHI node
inline unsigned getNumIncomingValues() const { return Operands.size()/2; } /// has
unsigned getNumIncomingValues() const { return Operands.size()/2; }
// getIncomingValue - Return incoming value #x /// getIncomingValue - Return incoming value #x
inline const Value *getIncomingValue(unsigned i) const { const Value *getIncomingValue(unsigned i) const {
return Operands[i*2]; return Operands[i*2];
} }
inline Value *getIncomingValue(unsigned i) { Value *getIncomingValue(unsigned i) {
return Operands[i*2]; return Operands[i*2];
} }
inline void setIncomingValue(unsigned i, Value *V) { void setIncomingValue(unsigned i, Value *V) {
Operands[i*2] = V; Operands[i*2] = V;
} }
// getIncomingBlock - Return incoming basic block #x /// getIncomingBlock - Return incoming basic block #x
inline const BasicBlock *getIncomingBlock(unsigned i) const { const BasicBlock *getIncomingBlock(unsigned i) const {
return (const BasicBlock*)Operands[i*2+1].get(); return (const BasicBlock*)Operands[i*2+1].get();
} }
inline BasicBlock *getIncomingBlock(unsigned i) { inline BasicBlock *getIncomingBlock(unsigned i) {
@ -50,23 +54,23 @@ public:
Operands[i*2+1] = (Value*)BB; 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); void addIncoming(Value *D, BasicBlock *BB);
// removeIncomingValue - Remove an incoming value. This is useful if a /// removeIncomingValue - Remove an incoming value. This is useful if a
// predecessor basic block is deleted. The value removed is returned. /// predecessor basic block is deleted. The value removed is returned.
Value *removeIncomingValue(const BasicBlock *BB); Value *removeIncomingValue(const BasicBlock *BB);
// getBasicBlockIndex - Return the first index of the specified basic /// getBasicBlockIndex - Return the first index of the specified basic
// block in the value list for this PHI. Returns -1 if no instance. /// block in the value list for this PHI. Returns -1 if no instance.
// ///
int getBasicBlockIndex(const BasicBlock *BB) const { int getBasicBlockIndex(const BasicBlock *BB) const {
for (unsigned i = 0; i < Operands.size()/2; ++i) for (unsigned i = 0; i < Operands.size()/2; ++i)
if (getIncomingBlock(i) == BB) return i; if (getIncomingBlock(i) == BB) return i;
return -1; 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 PHINode *) { return true; }
static inline bool classof(const Instruction *I) { static inline bool classof(const Instruction *I) {
return I->getOpcode() == Instruction::PHINode; return I->getOpcode() == Instruction::PHINode;

View 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 // This file contains the declarations for all the subclasses of the Instruction
// Instruction class, which is itself defined in the Instruction.h file. In // class which represent "terminator" instructions. Terminator instructions are
// between these definitions and the Instruction class are classes that expose // the only instructions allowed and required to terminate a BasicBlock.
// the SSA properties of each instruction, and that form the SSA graph.
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -12,11 +11,6 @@
#include "llvm/InstrTypes.h" #include "llvm/InstrTypes.h"
//===----------------------------------------------------------------------===//
// Classes to represent Basic Block "Terminator" instructions
//===----------------------------------------------------------------------===//
//===--------------------------------------------------------------------------- //===---------------------------------------------------------------------------
// ReturnInst - Return a value (possibly void), from a method. Execution does // ReturnInst - Return a value (possibly void), from a method. Execution does
// not continue in this method any longer. // not continue in this method any longer.
@ -30,7 +24,8 @@ class ReturnInst : public TerminatorInst {
} }
} }
public: public:
ReturnInst(Value *RetVal = 0) : TerminatorInst(Instruction::Ret) { ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
: TerminatorInst(Instruction::Ret, InsertBefore) {
if (RetVal) { if (RetVal) {
Operands.reserve(1); Operands.reserve(1);
Operands.push_back(Use(RetVal, this)); Operands.push_back(Use(RetVal, this));
@ -74,7 +69,8 @@ class BranchInst : public TerminatorInst {
BranchInst(const BranchInst &BI); BranchInst(const BranchInst &BI);
public: public:
// If cond = null, then is an unconditional br... // 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); } virtual Instruction *clone() const { return new BranchInst(*this); }
@ -84,7 +80,7 @@ public:
inline const Value *getCondition() const { inline const Value *getCondition() const {
return isUnconditional() ? 0 : Operands[2].get(); return isUnconditional() ? 0 : Operands[2].get();
} }
inline Value *getCondition() { Value *getCondition() {
return isUnconditional() ? 0 : Operands[2].get(); return isUnconditional() ? 0 : Operands[2].get();
} }
@ -133,7 +129,7 @@ class SwitchInst : public TerminatorInst {
// Operand[2n+1] = BasicBlock to go to on match // Operand[2n+1] = BasicBlock to go to on match
SwitchInst(const SwitchInst &RI); SwitchInst(const SwitchInst &RI);
public: public:
SwitchInst(Value *Value, BasicBlock *Default); SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0);
virtual Instruction *clone() const { return new SwitchInst(*this); } virtual Instruction *clone() const { return new SwitchInst(*this); }
@ -194,7 +190,8 @@ class InvokeInst : public TerminatorInst {
InvokeInst(const InvokeInst &BI); InvokeInst(const InvokeInst &BI);
public: public:
InvokeInst(Value *Meth, BasicBlock *IfNormal, BasicBlock *IfException, 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); } virtual Instruction *clone() const { return new InvokeInst(*this); }