LLVM API Documentation

Instructions.h

Go to the documentation of this file.
00001 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file exposes the class definitions of all of the subclasses of the
00011 // Instruction class.  This is meant to be an easy way to get access to all
00012 // instruction subclasses.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_INSTRUCTIONS_H
00017 #define LLVM_INSTRUCTIONS_H
00018 
00019 #include "llvm/Instruction.h"
00020 #include "llvm/InstrTypes.h"
00021 
00022 namespace llvm {
00023 
00024 class BasicBlock;
00025 class ConstantInt;
00026 class PointerType;
00027 class PackedType;
00028 
00029 //===----------------------------------------------------------------------===//
00030 //                             AllocationInst Class
00031 //===----------------------------------------------------------------------===//
00032 
00033 /// AllocationInst - This class is the common base class of MallocInst and
00034 /// AllocaInst.
00035 ///
00036 class AllocationInst : public UnaryInstruction {
00037   unsigned Alignment;
00038 protected:
00039   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
00040                  const std::string &Name = "", Instruction *InsertBefore = 0);
00041   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
00042                  const std::string &Name, BasicBlock *InsertAtEnd);
00043 public:
00044   // Out of line virtual method, so the vtable, etc has a home.
00045   virtual ~AllocationInst();
00046   
00047   /// isArrayAllocation - Return true if there is an allocation size parameter
00048   /// to the allocation instruction that is not 1.
00049   ///
00050   bool isArrayAllocation() const;
00051 
00052   /// getArraySize - Get the number of element allocated, for a simple
00053   /// allocation of a single element, this will return a constant 1 value.
00054   ///
00055   inline const Value *getArraySize() const { return getOperand(0); }
00056   inline Value *getArraySize() { return getOperand(0); }
00057 
00058   /// getType - Overload to return most specific pointer type
00059   ///
00060   inline const PointerType *getType() const {
00061     return reinterpret_cast<const PointerType*>(Instruction::getType());
00062   }
00063 
00064   /// getAllocatedType - Return the type that is being allocated by the
00065   /// instruction.
00066   ///
00067   const Type *getAllocatedType() const;
00068 
00069   /// getAlignment - Return the alignment of the memory that is being allocated
00070   /// by the instruction.
00071   ///
00072   unsigned getAlignment() const { return Alignment; }
00073   void setAlignment(unsigned Align) {
00074     assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
00075     Alignment = Align;
00076   }
00077   
00078   virtual Instruction *clone() const = 0;
00079 
00080   // Methods for support type inquiry through isa, cast, and dyn_cast:
00081   static inline bool classof(const AllocationInst *) { return true; }
00082   static inline bool classof(const Instruction *I) {
00083     return I->getOpcode() == Instruction::Alloca ||
00084            I->getOpcode() == Instruction::Malloc;
00085   }
00086   static inline bool classof(const Value *V) {
00087     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00088   }
00089 };
00090 
00091 
00092 //===----------------------------------------------------------------------===//
00093 //                                MallocInst Class
00094 //===----------------------------------------------------------------------===//
00095 
00096 /// MallocInst - an instruction to allocated memory on the heap
00097 ///
00098 class MallocInst : public AllocationInst {
00099   MallocInst(const MallocInst &MI);
00100 public:
00101   explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
00102                       const std::string &Name = "",
00103                       Instruction *InsertBefore = 0)
00104     : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
00105   MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
00106              BasicBlock *InsertAtEnd)
00107     : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
00108   
00109   explicit MallocInst(const Type *Ty, const std::string &Name,
00110                       Instruction *InsertBefore = 0)
00111     : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
00112   MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
00113     : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
00114   
00115   MallocInst(const Type *Ty, Value *ArraySize, unsigned Align, 
00116              const std::string &Name, BasicBlock *InsertAtEnd)
00117     : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
00118   MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
00119                       const std::string &Name = "",
00120                       Instruction *InsertBefore = 0)
00121     : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
00122   
00123   virtual MallocInst *clone() const;
00124 
00125   // Methods for support type inquiry through isa, cast, and dyn_cast:
00126   static inline bool classof(const MallocInst *) { return true; }
00127   static inline bool classof(const Instruction *I) {
00128     return (I->getOpcode() == Instruction::Malloc);
00129   }
00130   static inline bool classof(const Value *V) {
00131     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00132   }
00133 };
00134 
00135 
00136 //===----------------------------------------------------------------------===//
00137 //                                AllocaInst Class
00138 //===----------------------------------------------------------------------===//
00139 
00140 /// AllocaInst - an instruction to allocate memory on the stack
00141 ///
00142 class AllocaInst : public AllocationInst {
00143   AllocaInst(const AllocaInst &);
00144 public:
00145   explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
00146                       const std::string &Name = "",
00147                       Instruction *InsertBefore = 0)
00148     : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
00149   AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
00150              BasicBlock *InsertAtEnd)
00151     : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
00152 
00153   AllocaInst(const Type *Ty, const std::string &Name,
00154              Instruction *InsertBefore = 0)
00155     : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
00156   AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
00157     : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
00158   
00159   AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
00160              const std::string &Name = "", Instruction *InsertBefore = 0)
00161     : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
00162   AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
00163              const std::string &Name, BasicBlock *InsertAtEnd)
00164     : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
00165   
00166   virtual AllocaInst *clone() const;
00167 
00168   // Methods for support type inquiry through isa, cast, and dyn_cast:
00169   static inline bool classof(const AllocaInst *) { return true; }
00170   static inline bool classof(const Instruction *I) {
00171     return (I->getOpcode() == Instruction::Alloca);
00172   }
00173   static inline bool classof(const Value *V) {
00174     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00175   }
00176 };
00177 
00178 
00179 //===----------------------------------------------------------------------===//
00180 //                                 FreeInst Class
00181 //===----------------------------------------------------------------------===//
00182 
00183 /// FreeInst - an instruction to deallocate memory
00184 ///
00185 class FreeInst : public UnaryInstruction {
00186   void AssertOK();
00187 public:
00188   explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
00189   FreeInst(Value *Ptr, BasicBlock *InsertAfter);
00190 
00191   virtual FreeInst *clone() const;
00192 
00193   virtual bool mayWriteToMemory() const { return true; }
00194 
00195   // Methods for support type inquiry through isa, cast, and dyn_cast:
00196   static inline bool classof(const FreeInst *) { return true; }
00197   static inline bool classof(const Instruction *I) {
00198     return (I->getOpcode() == Instruction::Free);
00199   }
00200   static inline bool classof(const Value *V) {
00201     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00202   }
00203 };
00204 
00205 
00206 //===----------------------------------------------------------------------===//
00207 //                                LoadInst Class
00208 //===----------------------------------------------------------------------===//
00209 
00210 /// LoadInst - an instruction for reading from memory.  This uses the
00211 /// SubclassData field in Value to store whether or not the load is volatile.
00212 ///
00213 class LoadInst : public UnaryInstruction {
00214   LoadInst(const LoadInst &LI)
00215     : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
00216     setVolatile(LI.isVolatile());
00217 
00218 #ifndef NDEBUG
00219     AssertOK();
00220 #endif
00221   }
00222   void AssertOK();
00223 public:
00224   LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
00225   LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
00226   LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
00227            Instruction *InsertBefore = 0);
00228   LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
00229            BasicBlock *InsertAtEnd);
00230 
00231   /// isVolatile - Return true if this is a load from a volatile memory
00232   /// location.
00233   ///
00234   bool isVolatile() const { return SubclassData; }
00235 
00236   /// setVolatile - Specify whether this is a volatile load or not.
00237   ///
00238   void setVolatile(bool V) { SubclassData = V; }
00239 
00240   virtual LoadInst *clone() const;
00241 
00242   virtual bool mayWriteToMemory() const { return isVolatile(); }
00243 
00244   Value *getPointerOperand() { return getOperand(0); }
00245   const Value *getPointerOperand() const { return getOperand(0); }
00246   static unsigned getPointerOperandIndex() { return 0U; }
00247 
00248   // Methods for support type inquiry through isa, cast, and dyn_cast:
00249   static inline bool classof(const LoadInst *) { return true; }
00250   static inline bool classof(const Instruction *I) {
00251     return I->getOpcode() == Instruction::Load;
00252   }
00253   static inline bool classof(const Value *V) {
00254     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00255   }
00256 };
00257 
00258 
00259 //===----------------------------------------------------------------------===//
00260 //                                StoreInst Class
00261 //===----------------------------------------------------------------------===//
00262 
00263 /// StoreInst - an instruction for storing to memory
00264 ///
00265 class StoreInst : public Instruction {
00266   Use Ops[2];
00267   StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
00268     Ops[0].init(SI.Ops[0], this);
00269     Ops[1].init(SI.Ops[1], this);
00270     setVolatile(SI.isVolatile());
00271 #ifndef NDEBUG
00272     AssertOK();
00273 #endif
00274   }
00275   void AssertOK();
00276 public:
00277   StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
00278   StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
00279   StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
00280             Instruction *InsertBefore = 0);
00281   StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
00282 
00283 
00284   /// isVolatile - Return true if this is a load from a volatile memory
00285   /// location.
00286   ///
00287   bool isVolatile() const { return SubclassData; }
00288 
00289   /// setVolatile - Specify whether this is a volatile load or not.
00290   ///
00291   void setVolatile(bool V) { SubclassData = V; }
00292 
00293   /// Transparently provide more efficient getOperand methods.
00294   Value *getOperand(unsigned i) const {
00295     assert(i < 2 && "getOperand() out of range!");
00296     return Ops[i];
00297   }
00298   void setOperand(unsigned i, Value *Val) {
00299     assert(i < 2 && "setOperand() out of range!");
00300     Ops[i] = Val;
00301   }
00302   unsigned getNumOperands() const { return 2; }
00303 
00304 
00305   virtual StoreInst *clone() const;
00306 
00307   virtual bool mayWriteToMemory() const { return true; }
00308 
00309   Value *getPointerOperand() { return getOperand(1); }
00310   const Value *getPointerOperand() const { return getOperand(1); }
00311   static unsigned getPointerOperandIndex() { return 1U; }
00312 
00313   // Methods for support type inquiry through isa, cast, and dyn_cast:
00314   static inline bool classof(const StoreInst *) { return true; }
00315   static inline bool classof(const Instruction *I) {
00316     return I->getOpcode() == Instruction::Store;
00317   }
00318   static inline bool classof(const Value *V) {
00319     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00320   }
00321 };
00322 
00323 
00324 //===----------------------------------------------------------------------===//
00325 //                             GetElementPtrInst Class
00326 //===----------------------------------------------------------------------===//
00327 
00328 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
00329 /// access elements of arrays and structs
00330 ///
00331 class GetElementPtrInst : public Instruction {
00332   GetElementPtrInst(const GetElementPtrInst &GEPI)
00333     : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
00334                   0, GEPI.getNumOperands()) {
00335     Use *OL = OperandList = new Use[NumOperands];
00336     Use *GEPIOL = GEPI.OperandList;
00337     for (unsigned i = 0, E = NumOperands; i != E; ++i)
00338       OL[i].init(GEPIOL[i], this);
00339   }
00340   void init(Value *Ptr, const std::vector<Value*> &Idx);
00341   void init(Value *Ptr, Value *Idx0, Value *Idx1);
00342   void init(Value *Ptr, Value *Idx);
00343 public:
00344   /// Constructors - Create a getelementptr instruction with a base pointer an
00345   /// list of indices.  The first ctor can optionally insert before an existing
00346   /// instruction, the second appends the new instruction to the specified
00347   /// BasicBlock.
00348   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
00349                     const std::string &Name = "", Instruction *InsertBefore =0);
00350   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
00351                     const std::string &Name, BasicBlock *InsertAtEnd);
00352 
00353   /// Constructors - These two constructors are convenience methods because one
00354   /// and two index getelementptr instructions are so common.
00355   GetElementPtrInst(Value *Ptr, Value *Idx,
00356                     const std::string &Name = "", Instruction *InsertBefore =0);
00357   GetElementPtrInst(Value *Ptr, Value *Idx,
00358                     const std::string &Name, BasicBlock *InsertAtEnd);
00359   GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
00360                     const std::string &Name = "", Instruction *InsertBefore =0);
00361   GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
00362                     const std::string &Name, BasicBlock *InsertAtEnd);
00363   ~GetElementPtrInst();
00364 
00365   virtual GetElementPtrInst *clone() const;
00366 
00367   // getType - Overload to return most specific pointer type...
00368   inline const PointerType *getType() const {
00369     return reinterpret_cast<const PointerType*>(Instruction::getType());
00370   }
00371 
00372   /// getIndexedType - Returns the type of the element that would be loaded with
00373   /// a load instruction with the specified parameters.
00374   ///
00375   /// A null type is returned if the indices are invalid for the specified
00376   /// pointer type.
00377   ///
00378   static const Type *getIndexedType(const Type *Ptr,
00379                                     const std::vector<Value*> &Indices,
00380                                     bool AllowStructLeaf = false);
00381   static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
00382                                     bool AllowStructLeaf = false);
00383   static const Type *getIndexedType(const Type *Ptr, Value *Idx);
00384 
00385   inline op_iterator       idx_begin()       { return op_begin()+1; }
00386   inline const_op_iterator idx_begin() const { return op_begin()+1; }
00387   inline op_iterator       idx_end()         { return op_end(); }
00388   inline const_op_iterator idx_end()   const { return op_end(); }
00389 
00390   Value *getPointerOperand() {
00391     return getOperand(0);
00392   }
00393   const Value *getPointerOperand() const {
00394     return getOperand(0);
00395   }
00396   static unsigned getPointerOperandIndex() {
00397     return 0U;                      // get index for modifying correct operand
00398   }
00399 
00400   inline unsigned getNumIndices() const {  // Note: always non-negative
00401     return getNumOperands() - 1;
00402   }
00403 
00404   inline bool hasIndices() const {
00405     return getNumOperands() > 1;
00406   }
00407 
00408   // Methods for support type inquiry through isa, cast, and dyn_cast:
00409   static inline bool classof(const GetElementPtrInst *) { return true; }
00410   static inline bool classof(const Instruction *I) {
00411     return (I->getOpcode() == Instruction::GetElementPtr);
00412   }
00413   static inline bool classof(const Value *V) {
00414     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00415   }
00416 };
00417 
00418 //===----------------------------------------------------------------------===//
00419 //                            SetCondInst Class
00420 //===----------------------------------------------------------------------===//
00421 
00422 /// SetCondInst class - Represent a setCC operator, where CC is eq, ne, lt, gt,
00423 /// le, or ge.
00424 ///
00425 class SetCondInst : public BinaryOperator {
00426 public:
00427   SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
00428               const std::string &Name = "", Instruction *InsertBefore = 0);
00429   SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
00430               const std::string &Name, BasicBlock *InsertAtEnd);
00431 
00432   /// getInverseCondition - Return the inverse of the current condition opcode.
00433   /// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
00434   ///
00435   BinaryOps getInverseCondition() const {
00436     return getInverseCondition(getOpcode());
00437   }
00438 
00439   /// getInverseCondition - Static version that you can use without an
00440   /// instruction available.
00441   ///
00442   static BinaryOps getInverseCondition(BinaryOps Opcode);
00443 
00444   /// getSwappedCondition - Return the condition opcode that would be the result
00445   /// of exchanging the two operands of the setcc instruction without changing
00446   /// the result produced.  Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
00447   ///
00448   BinaryOps getSwappedCondition() const {
00449     return getSwappedCondition(getOpcode());
00450   }
00451 
00452   /// getSwappedCondition - Static version that you can use without an
00453   /// instruction available.
00454   ///
00455   static BinaryOps getSwappedCondition(BinaryOps Opcode);
00456 
00457 
00458   // Methods for support type inquiry through isa, cast, and dyn_cast:
00459   static inline bool classof(const SetCondInst *) { return true; }
00460   static inline bool classof(const Instruction *I) {
00461     return I->getOpcode() == SetEQ || I->getOpcode() == SetNE ||
00462            I->getOpcode() == SetLE || I->getOpcode() == SetGE ||
00463            I->getOpcode() == SetLT || I->getOpcode() == SetGT;
00464   }
00465   static inline bool classof(const Value *V) {
00466     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00467   }
00468 };
00469 
00470 //===----------------------------------------------------------------------===//
00471 //                                 CastInst Class
00472 //===----------------------------------------------------------------------===//
00473 
00474 /// CastInst - This class represents a cast from Operand[0] to the type of
00475 /// the instruction (i->getType()).
00476 ///
00477 class CastInst : public UnaryInstruction {
00478   CastInst(const CastInst &CI)
00479     : UnaryInstruction(CI.getType(), Cast, CI.getOperand(0)) {
00480   }
00481 public:
00482   CastInst(Value *S, const Type *Ty, const std::string &Name = "",
00483            Instruction *InsertBefore = 0)
00484     : UnaryInstruction(Ty, Cast, S, Name, InsertBefore) {
00485   }
00486   CastInst(Value *S, const Type *Ty, const std::string &Name,
00487            BasicBlock *InsertAtEnd)
00488     : UnaryInstruction(Ty, Cast, S, Name, InsertAtEnd) {
00489   }
00490 
00491   virtual CastInst *clone() const;
00492 
00493   // Methods for support type inquiry through isa, cast, and dyn_cast:
00494   static inline bool classof(const CastInst *) { return true; }
00495   static inline bool classof(const Instruction *I) {
00496     return I->getOpcode() == Cast;
00497   }
00498   static inline bool classof(const Value *V) {
00499     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00500   }
00501 };
00502 
00503 
00504 //===----------------------------------------------------------------------===//
00505 //                                 CallInst Class
00506 //===----------------------------------------------------------------------===//
00507 
00508 /// CallInst - This class represents a function call, abstracting a target
00509 /// machine's calling convention.  This class uses low bit of the SubClassData
00510 /// field to indicate whether or not this is a tail call.  The rest of the bits
00511 /// hold the calling convention of the call.
00512 ///
00513 class CallInst : public Instruction {
00514   CallInst(const CallInst &CI);
00515   void init(Value *Func, const std::vector<Value*> &Params);
00516   void init(Value *Func, Value *Actual1, Value *Actual2);
00517   void init(Value *Func, Value *Actual);
00518   void init(Value *Func);
00519 
00520 public:
00521   CallInst(Value *F, const std::vector<Value*> &Par,
00522            const std::string &Name = "", Instruction *InsertBefore = 0);
00523   CallInst(Value *F, const std::vector<Value*> &Par,
00524            const std::string &Name, BasicBlock *InsertAtEnd);
00525 
00526   // Alternate CallInst ctors w/ two actuals, w/ one actual and no
00527   // actuals, respectively.
00528   CallInst(Value *F, Value *Actual1, Value *Actual2,
00529            const std::string& Name = "", Instruction *InsertBefore = 0);
00530   CallInst(Value *F, Value *Actual1, Value *Actual2,
00531            const std::string& Name, BasicBlock *InsertAtEnd);
00532   CallInst(Value *F, Value *Actual, const std::string& Name = "",
00533            Instruction *InsertBefore = 0);
00534   CallInst(Value *F, Value *Actual, const std::string& Name,
00535            BasicBlock *InsertAtEnd);
00536   explicit CallInst(Value *F, const std::string &Name = "",
00537                     Instruction *InsertBefore = 0);
00538   explicit CallInst(Value *F, const std::string &Name,
00539                     BasicBlock *InsertAtEnd);
00540   ~CallInst();
00541 
00542   virtual CallInst *clone() const;
00543   bool mayWriteToMemory() const { return true; }
00544 
00545   bool isTailCall() const           { return SubclassData & 1; }
00546   void setTailCall(bool isTailCall = true) {
00547     SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
00548   }
00549 
00550   /// getCallingConv/setCallingConv - Get or set the calling convention of this
00551   /// function call.
00552   unsigned getCallingConv() const { return SubclassData >> 1; }
00553   void setCallingConv(unsigned CC) {
00554     SubclassData = (SubclassData & 1) | (CC << 1);
00555   }
00556 
00557   /// getCalledFunction - Return the function being called by this instruction
00558   /// if it is a direct call.  If it is a call through a function pointer,
00559   /// return null.
00560   Function *getCalledFunction() const {
00561     return static_cast<Function*>(dyn_cast<Function>(getOperand(0)));
00562   }
00563 
00564   // getCalledValue - Get a pointer to a method that is invoked by this inst.
00565   inline const Value *getCalledValue() const { return getOperand(0); }
00566   inline       Value *getCalledValue()       { return getOperand(0); }
00567 
00568   // Methods for support type inquiry through isa, cast, and dyn_cast:
00569   static inline bool classof(const CallInst *) { return true; }
00570   static inline bool classof(const Instruction *I) {
00571     return I->getOpcode() == Instruction::Call;
00572   }
00573   static inline bool classof(const Value *V) {
00574     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00575   }
00576 };
00577 
00578 
00579 //===----------------------------------------------------------------------===//
00580 //                                 ShiftInst Class
00581 //===----------------------------------------------------------------------===//
00582 
00583 /// ShiftInst - This class represents left and right shift instructions.
00584 ///
00585 class ShiftInst : public Instruction {
00586   Use Ops[2];
00587   ShiftInst(const ShiftInst &SI)
00588     : Instruction(SI.getType(), SI.getOpcode(), Ops, 2) {
00589     Ops[0].init(SI.Ops[0], this);
00590     Ops[1].init(SI.Ops[1], this);
00591   }
00592   void init(OtherOps Opcode, Value *S, Value *SA) {
00593     assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
00594     Ops[0].init(S, this);
00595     Ops[1].init(SA, this);
00596   }
00597 
00598 public:
00599   ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
00600             Instruction *InsertBefore = 0)
00601     : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertBefore) {
00602     init(Opcode, S, SA);
00603   }
00604   ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name,
00605             BasicBlock *InsertAtEnd)
00606     : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertAtEnd) {
00607     init(Opcode, S, SA);
00608   }
00609 
00610   OtherOps getOpcode() const {
00611     return static_cast<OtherOps>(Instruction::getOpcode());
00612   }
00613 
00614   /// Transparently provide more efficient getOperand methods.
00615   Value *getOperand(unsigned i) const {
00616     assert(i < 2 && "getOperand() out of range!");
00617     return Ops[i];
00618   }
00619   void setOperand(unsigned i, Value *Val) {
00620     assert(i < 2 && "setOperand() out of range!");
00621     Ops[i] = Val;
00622   }
00623   unsigned getNumOperands() const { return 2; }
00624 
00625   virtual ShiftInst *clone() const;
00626 
00627   // Methods for support type inquiry through isa, cast, and dyn_cast:
00628   static inline bool classof(const ShiftInst *) { return true; }
00629   static inline bool classof(const Instruction *I) {
00630     return (I->getOpcode() == Instruction::Shr) |
00631            (I->getOpcode() == Instruction::Shl);
00632   }
00633   static inline bool classof(const Value *V) {
00634     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00635   }
00636 };
00637 
00638 //===----------------------------------------------------------------------===//
00639 //                               SelectInst Class
00640 //===----------------------------------------------------------------------===//
00641 
00642 /// SelectInst - This class represents the LLVM 'select' instruction.
00643 ///
00644 class SelectInst : public Instruction {
00645   Use Ops[3];
00646 
00647   void init(Value *C, Value *S1, Value *S2) {
00648     Ops[0].init(C, this);
00649     Ops[1].init(S1, this);
00650     Ops[2].init(S2, this);
00651   }
00652 
00653   SelectInst(const SelectInst &SI)
00654     : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
00655     init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
00656   }
00657 public:
00658   SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
00659              Instruction *InsertBefore = 0)
00660     : Instruction(S1->getType(), Instruction::Select, Ops, 3,
00661                   Name, InsertBefore) {
00662     init(C, S1, S2);
00663   }
00664   SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
00665              BasicBlock *InsertAtEnd)
00666     : Instruction(S1->getType(), Instruction::Select, Ops, 3,
00667                   Name, InsertAtEnd) {
00668     init(C, S1, S2);
00669   }
00670 
00671   Value *getCondition() const { return Ops[0]; }
00672   Value *getTrueValue() const { return Ops[1]; }
00673   Value *getFalseValue() const { return Ops[2]; }
00674 
00675   /// Transparently provide more efficient getOperand methods.
00676   Value *getOperand(unsigned i) const {
00677     assert(i < 3 && "getOperand() out of range!");
00678     return Ops[i];
00679   }
00680   void setOperand(unsigned i, Value *Val) {
00681     assert(i < 3 && "setOperand() out of range!");
00682     Ops[i] = Val;
00683   }
00684   unsigned getNumOperands() const { return 3; }
00685 
00686   OtherOps getOpcode() const {
00687     return static_cast<OtherOps>(Instruction::getOpcode());
00688   }
00689 
00690   virtual SelectInst *clone() const;
00691 
00692   // Methods for support type inquiry through isa, cast, and dyn_cast:
00693   static inline bool classof(const SelectInst *) { return true; }
00694   static inline bool classof(const Instruction *I) {
00695     return I->getOpcode() == Instruction::Select;
00696   }
00697   static inline bool classof(const Value *V) {
00698     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00699   }
00700 };
00701 
00702 //===----------------------------------------------------------------------===//
00703 //                                VAArgInst Class
00704 //===----------------------------------------------------------------------===//
00705 
00706 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
00707 /// an argument of the specified type given a va_list and increments that list
00708 ///
00709 class VAArgInst : public UnaryInstruction {
00710   VAArgInst(const VAArgInst &VAA)
00711     : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
00712 public:
00713   VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
00714              Instruction *InsertBefore = 0)
00715     : UnaryInstruction(Ty, VAArg, List, Name, InsertBefore) {
00716   }
00717   VAArgInst(Value *List, const Type *Ty, const std::string &Name,
00718             BasicBlock *InsertAtEnd)
00719     : UnaryInstruction(Ty, VAArg, List, Name, InsertAtEnd) {
00720   }
00721 
00722   virtual VAArgInst *clone() const;
00723   bool mayWriteToMemory() const { return true; }
00724 
00725   // Methods for support type inquiry through isa, cast, and dyn_cast:
00726   static inline bool classof(const VAArgInst *) { return true; }
00727   static inline bool classof(const Instruction *I) {
00728     return I->getOpcode() == VAArg;
00729   }
00730   static inline bool classof(const Value *V) {
00731     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00732   }
00733 };
00734 
00735 //===----------------------------------------------------------------------===//
00736 //                                ExtractElementInst Class
00737 //===----------------------------------------------------------------------===//
00738 
00739 /// ExtractElementInst - This instruction extracts a single (scalar)
00740 /// element from a PackedType value
00741 ///
00742 class ExtractElementInst : public Instruction {
00743   Use Ops[2];
00744   ExtractElementInst(const ExtractElementInst &EE) : 
00745     Instruction(EE.getType(), ExtractElement, Ops, 2) {
00746     Ops[0].init(EE.Ops[0], this);
00747     Ops[1].init(EE.Ops[1], this);
00748   }
00749 
00750 public:
00751   ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
00752                      Instruction *InsertBefore = 0);
00753   ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
00754                      BasicBlock *InsertAtEnd);
00755 
00756   /// isValidOperands - Return true if an extractelement instruction can be
00757   /// formed with the specified operands.
00758   static bool isValidOperands(const Value *Vec, const Value *Idx);
00759   
00760   virtual ExtractElementInst *clone() const;
00761 
00762   virtual bool mayWriteToMemory() const { return false; }
00763 
00764   /// Transparently provide more efficient getOperand methods.
00765   Value *getOperand(unsigned i) const {
00766     assert(i < 2 && "getOperand() out of range!");
00767     return Ops[i];
00768   }
00769   void setOperand(unsigned i, Value *Val) {
00770     assert(i < 2 && "setOperand() out of range!");
00771     Ops[i] = Val;
00772   }
00773   unsigned getNumOperands() const { return 2; }
00774 
00775   // Methods for support type inquiry through isa, cast, and dyn_cast:
00776   static inline bool classof(const ExtractElementInst *) { return true; }
00777   static inline bool classof(const Instruction *I) {
00778     return I->getOpcode() == Instruction::ExtractElement;
00779   }
00780   static inline bool classof(const Value *V) {
00781     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00782   }
00783 };
00784 
00785 //===----------------------------------------------------------------------===//
00786 //                                InsertElementInst Class
00787 //===----------------------------------------------------------------------===//
00788 
00789 /// InsertElementInst - This instruction inserts a single (scalar)
00790 /// element into a PackedType value
00791 ///
00792 class InsertElementInst : public Instruction {
00793   Use Ops[3];
00794   InsertElementInst(const InsertElementInst &IE);
00795 public:
00796   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
00797                     const std::string &Name = "",Instruction *InsertBefore = 0);
00798   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
00799                     const std::string &Name, BasicBlock *InsertAtEnd);
00800 
00801   /// isValidOperands - Return true if an insertelement instruction can be
00802   /// formed with the specified operands.
00803   static bool isValidOperands(const Value *Vec, const Value *NewElt,
00804                               const Value *Idx);
00805   
00806   virtual InsertElementInst *clone() const;
00807 
00808   virtual bool mayWriteToMemory() const { return false; }
00809 
00810   /// getType - Overload to return most specific packed type.
00811   ///
00812   inline const PackedType *getType() const {
00813     return reinterpret_cast<const PackedType*>(Instruction::getType());
00814   }
00815   
00816   /// Transparently provide more efficient getOperand methods.
00817   Value *getOperand(unsigned i) const {
00818     assert(i < 3 && "getOperand() out of range!");
00819     return Ops[i];
00820   }
00821   void setOperand(unsigned i, Value *Val) {
00822     assert(i < 3 && "setOperand() out of range!");
00823     Ops[i] = Val;
00824   }
00825   unsigned getNumOperands() const { return 3; }
00826 
00827   // Methods for support type inquiry through isa, cast, and dyn_cast:
00828   static inline bool classof(const InsertElementInst *) { return true; }
00829   static inline bool classof(const Instruction *I) {
00830     return I->getOpcode() == Instruction::InsertElement;
00831   }
00832   static inline bool classof(const Value *V) {
00833     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00834   }
00835 };
00836 
00837 //===----------------------------------------------------------------------===//
00838 //                           ShuffleVectorInst Class
00839 //===----------------------------------------------------------------------===//
00840 
00841 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
00842 /// input vectors.
00843 ///
00844 class ShuffleVectorInst : public Instruction {
00845   Use Ops[3];
00846   ShuffleVectorInst(const ShuffleVectorInst &IE);  
00847 public:
00848   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
00849                     const std::string &Name = "", Instruction *InsertBefor = 0);
00850   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
00851                     const std::string &Name, BasicBlock *InsertAtEnd);
00852   
00853   /// isValidOperands - Return true if a shufflevector instruction can be
00854   /// formed with the specified operands.
00855   static bool isValidOperands(const Value *V1, const Value *V2,
00856                               const Value *Mask);
00857   
00858   virtual ShuffleVectorInst *clone() const;
00859   
00860   virtual bool mayWriteToMemory() const { return false; }
00861   
00862   /// getType - Overload to return most specific packed type.
00863   ///
00864   inline const PackedType *getType() const {
00865     return reinterpret_cast<const PackedType*>(Instruction::getType());
00866   }
00867   
00868   /// Transparently provide more efficient getOperand methods.
00869   Value *getOperand(unsigned i) const {
00870     assert(i < 3 && "getOperand() out of range!");
00871     return Ops[i];
00872   }
00873   void setOperand(unsigned i, Value *Val) {
00874     assert(i < 3 && "setOperand() out of range!");
00875     Ops[i] = Val;
00876   }
00877   unsigned getNumOperands() const { return 3; }
00878   
00879   // Methods for support type inquiry through isa, cast, and dyn_cast:
00880   static inline bool classof(const ShuffleVectorInst *) { return true; }
00881   static inline bool classof(const Instruction *I) {
00882     return I->getOpcode() == Instruction::ShuffleVector;
00883   }
00884   static inline bool classof(const Value *V) {
00885     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00886   }
00887 };
00888 
00889 
00890 //===----------------------------------------------------------------------===//
00891 //                               PHINode Class
00892 //===----------------------------------------------------------------------===//
00893 
00894 // PHINode - The PHINode class is used to represent the magical mystical PHI
00895 // node, that can not exist in nature, but can be synthesized in a computer
00896 // scientist's overactive imagination.
00897 //
00898 class PHINode : public Instruction {
00899   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
00900   /// the number actually in use.
00901   unsigned ReservedSpace;
00902   PHINode(const PHINode &PN);
00903 public:
00904   PHINode(const Type *Ty, const std::string &Name = "",
00905           Instruction *InsertBefore = 0)
00906     : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertBefore),
00907       ReservedSpace(0) {
00908   }
00909 
00910   PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
00911     : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertAtEnd),
00912       ReservedSpace(0) {
00913   }
00914 
00915   ~PHINode();
00916 
00917   /// reserveOperandSpace - This method can be used to avoid repeated
00918   /// reallocation of PHI operand lists by reserving space for the correct
00919   /// number of operands before adding them.  Unlike normal vector reserves,
00920   /// this method can also be used to trim the operand space.
00921   void reserveOperandSpace(unsigned NumValues) {
00922     resizeOperands(NumValues*2);
00923   }
00924 
00925   virtual PHINode *clone() const;
00926 
00927   /// getNumIncomingValues - Return the number of incoming edges
00928   ///
00929   unsigned getNumIncomingValues() const { return getNumOperands()/2; }
00930 
00931   /// getIncomingValue - Return incoming value number x
00932   ///
00933   Value *getIncomingValue(unsigned i) const {
00934     assert(i*2 < getNumOperands() && "Invalid value number!");
00935     return getOperand(i*2);
00936   }
00937   void setIncomingValue(unsigned i, Value *V) {
00938     assert(i*2 < getNumOperands() && "Invalid value number!");
00939     setOperand(i*2, V);
00940   }
00941   unsigned getOperandNumForIncomingValue(unsigned i) {
00942     return i*2;
00943   }
00944 
00945   /// getIncomingBlock - Return incoming basic block number x
00946   ///
00947   BasicBlock *getIncomingBlock(unsigned i) const {
00948     return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
00949   }
00950   void setIncomingBlock(unsigned i, BasicBlock *BB) {
00951     setOperand(i*2+1, reinterpret_cast<Value*>(BB));
00952   }
00953   unsigned getOperandNumForIncomingBlock(unsigned i) {
00954     return i*2+1;
00955   }
00956 
00957   /// addIncoming - Add an incoming value to the end of the PHI list
00958   ///
00959   void addIncoming(Value *V, BasicBlock *BB) {
00960     assert(getType() == V->getType() &&
00961            "All operands to PHI node must be the same type as the PHI node!");
00962     unsigned OpNo = NumOperands;
00963     if (OpNo+2 > ReservedSpace)
00964       resizeOperands(0);  // Get more space!
00965     // Initialize some new operands.
00966     NumOperands = OpNo+2;
00967     OperandList[OpNo].init(V, this);
00968     OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
00969   }
00970 
00971   /// removeIncomingValue - Remove an incoming value.  This is useful if a
00972   /// predecessor basic block is deleted.  The value removed is returned.
00973   ///
00974   /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
00975   /// is true), the PHI node is destroyed and any uses of it are replaced with
00976   /// dummy values.  The only time there should be zero incoming values to a PHI
00977   /// node is when the block is dead, so this strategy is sound.
00978   ///
00979   Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
00980 
00981   Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
00982     int Idx = getBasicBlockIndex(BB);
00983     assert(Idx >= 0 && "Invalid basic block argument to remove!");
00984     return removeIncomingValue(Idx, DeletePHIIfEmpty);
00985   }
00986 
00987   /// getBasicBlockIndex - Return the first index of the specified basic
00988   /// block in the value list for this PHI.  Returns -1 if no instance.
00989   ///
00990   int getBasicBlockIndex(const BasicBlock *BB) const {
00991     Use *OL = OperandList;
00992     for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
00993       if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
00994     return -1;
00995   }
00996 
00997   Value *getIncomingValueForBlock(const BasicBlock *BB) const {
00998     return getIncomingValue(getBasicBlockIndex(BB));
00999   }
01000 
01001   /// hasConstantValue - If the specified PHI node always merges together the 
01002   /// same value, return the value, otherwise return null.
01003   ///
01004   Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
01005   
01006   /// Methods for support type inquiry through isa, cast, and dyn_cast:
01007   static inline bool classof(const PHINode *) { return true; }
01008   static inline bool classof(const Instruction *I) {
01009     return I->getOpcode() == Instruction::PHI;
01010   }
01011   static inline bool classof(const Value *V) {
01012     return isa<Instruction>(V) && classof(cast<Instruction>(V));
01013   }
01014  private:
01015   void resizeOperands(unsigned NumOperands);
01016 };
01017 
01018 //===----------------------------------------------------------------------===//
01019 //                               ReturnInst Class
01020 //===----------------------------------------------------------------------===//
01021 
01022 //===---------------------------------------------------------------------------
01023 /// ReturnInst - Return a value (possibly void), from a function.  Execution
01024 /// does not continue in this function any longer.
01025 ///
01026 class ReturnInst : public TerminatorInst {
01027   Use RetVal;  // Possibly null retval.
01028   ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret, &RetVal,
01029                                                     RI.getNumOperands()) {
01030     if (RI.getNumOperands())
01031       RetVal.init(RI.RetVal, this);
01032   }
01033 
01034   void init(Value *RetVal);
01035 
01036 public:
01037   // ReturnInst constructors:
01038   // ReturnInst()                  - 'ret void' instruction
01039   // ReturnInst(    null)          - 'ret void' instruction
01040   // ReturnInst(Value* X)          - 'ret X'    instruction
01041   // ReturnInst(    null, Inst *)  - 'ret void' instruction, insert before I
01042   // ReturnInst(Value* X, Inst *I) - 'ret X'    instruction, insert before I
01043   // ReturnInst(    null, BB *B)   - 'ret void' instruction, insert @ end of BB
01044   // ReturnInst(Value* X, BB *B)   - 'ret X'    instruction, insert @ end of BB
01045   //
01046   // NOTE: If the Value* passed is of type void then the constructor behaves as
01047   // if it was passed NULL.
01048   ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0)
01049     : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertBefore) {
01050     init(retVal);
01051   }
01052   ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
01053     : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
01054     init(retVal);
01055   }
01056   ReturnInst(BasicBlock *InsertAtEnd)
01057     : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
01058   }
01059 
01060   virtual ReturnInst *clone() const;
01061 
01062   // Transparently provide more efficient getOperand methods.
01063   Value *getOperand(unsigned i) const {
01064     assert(i < getNumOperands() && "getOperand() out of range!");
01065     return RetVal;
01066   }
01067   void setOperand(unsigned i, Value *Val) {
01068     assert(i < getNumOperands() && "setOperand() out of range!");
01069     RetVal = Val;
01070   }
01071 
01072   Value *getReturnValue() const { return RetVal; }
01073 
01074   unsigned getNumSuccessors() const { return 0; }
01075 
01076   // Methods for support type inquiry through isa, cast, and dyn_cast:
01077   static inline bool classof(const ReturnInst *) { return true; }
01078   static inline bool classof(const Instruction *I) {
01079     return (I->getOpcode() == Instruction::Ret);
01080   }
01081   static inline bool classof(const Value *V) {
01082     return isa<Instruction>(V) && classof(cast<Instruction>(V));
01083   }
01084  private:
01085   virtual BasicBlock *getSuccessorV(unsigned idx) const;
01086   virtual unsigned getNumSuccessorsV() const;
01087   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
01088 };
01089 
01090 //===----------------------------------------------------------------------===//
01091 //                               BranchInst Class
01092 //===----------------------------------------------------------------------===//
01093 
01094 //===---------------------------------------------------------------------------
01095 /// BranchInst - Conditional or Unconditional Branch instruction.
01096 ///
01097 class BranchInst : public TerminatorInst {
01098   /// Ops list - Branches are strange.  The operands are ordered:
01099   ///  TrueDest, FalseDest, Cond.  This makes some accessors faster because
01100   /// they don't have to check for cond/uncond branchness.
01101   Use Ops[3];
01102   BranchInst(const BranchInst &BI);
01103   void AssertOK();
01104 public:
01105   // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
01106   // BranchInst(BB *B)                           - 'br B'
01107   // BranchInst(BB* T, BB *F, Value *C)          - 'br C, T, F'
01108   // BranchInst(BB* B, Inst *I)                  - 'br B'        insert before I
01109   // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
01110   // BranchInst(BB* B, BB *I)                    - 'br B'        insert at end
01111   // BranchInst(BB* T, BB *F, Value *C, BB *I)   - 'br C, T, F', insert at end
01112   BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
01113     : TerminatorInst(Instruction::Br, Ops, 1, InsertBefore) {
01114     assert(IfTrue != 0 && "Branch destination may not be null!");
01115     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
01116   }
01117   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
01118              Instruction *InsertBefore = 0)
01119     : TerminatorInst(Instruction::Br, Ops, 3, InsertBefore) {
01120     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
01121     Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
01122     Ops[2].init(Cond, this);
01123 #ifndef NDEBUG
01124     AssertOK();
01125 #endif
01126   }
01127 
01128   BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
01129     : TerminatorInst(Instruction::Br, Ops, 1, InsertAtEnd) {
01130     assert(IfTrue != 0 && "Branch destination may not be null!");
01131     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
01132   }
01133 
01134   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
01135              BasicBlock *InsertAtEnd)
01136     : TerminatorInst(Instruction::Br, Ops, 3, InsertAtEnd) {
01137     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
01138     Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
01139     Ops[2].init(Cond, this);
01140 #ifndef NDEBUG
01141     AssertOK();
01142 #endif
01143   }
01144 
01145 
01146   /// Transparently provide more efficient getOperand methods.
01147   Value *getOperand(unsigned i) const {
01148     assert(i < getNumOperands() && "getOperand() out of range!");
01149     return Ops[i];
01150   }
01151   void setOperand(unsigned i, Value *Val) {
01152     assert(i < getNumOperands() && "setOperand() out of range!");
01153     Ops[i] = Val;
01154   }
01155 
01156   virtual BranchInst *clone() const;
01157 
01158   inline bool isUnconditional() const { return getNumOperands() == 1; }
01159   inline bool isConditional()   const { return getNumOperands() == 3; }
01160 
01161   inline Value *getCondition() const {
01162     assert(isConditional() && "Cannot get condition of an uncond branch!");
01163     return getOperand(2);
01164   }
01165 
01166   void setCondition(Value *V) {
01167     assert(isConditional() && "Cannot set condition of unconditional branch!");
01168     setOperand(2, V);
01169   }
01170 
01171   // setUnconditionalDest - Change the current branch to an unconditional branch
01172   // targeting the specified block.
01173   // FIXME: Eliminate this ugly method.
01174   void setUnconditionalDest(BasicBlock *Dest) {
01175     if (isConditional()) {  // Convert this to an uncond branch.
01176       NumOperands = 1;
01177       Ops[1].set(0);
01178       Ops[2].set(0);
01179     }
01180     setOperand(0, reinterpret_cast<Value*>(Dest));
01181   }
01182 
01183   unsigned getNumSuccessors() const { return 1+isConditional(); }
01184 
01185   BasicBlock *getSuccessor(unsigned i) const {
01186     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
01187     return (i == 0) ? cast<BasicBlock>(getOperand(0)) :
01188                       cast<BasicBlock>(getOperand(1));
01189   }
01190 
01191   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
01192     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
01193     setOperand(idx, reinterpret_cast<Value*>(NewSucc));
01194   }
01195 
01196   // Methods for support type inquiry through isa, cast, and dyn_cast:
01197   static inline bool classof(const BranchInst *) { return true; }
01198   static inline bool classof(const Instruction *I) {
01199     return (I->getOpcode() == Instruction::Br);
01200   }
01201   static inline bool classof(const Value *V) {
01202     return isa<Instruction>(V) && classof(cast<Instruction>(V));
01203   }
01204 private:
01205   virtual BasicBlock *getSuccessorV(unsigned idx) const;
01206   virtual unsigned getNumSuccessorsV() const;
01207   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
01208 };
01209 
01210 //===----------------------------------------------------------------------===//
01211 //                               SwitchInst Class
01212 //===----------------------------------------------------------------------===//
01213 
01214 //===---------------------------------------------------------------------------
01215 /// SwitchInst - Multiway switch
01216 ///
01217 class SwitchInst : public TerminatorInst {
01218   unsigned ReservedSpace;
01219   // Operand[0]    = Value to switch on
01220   // Operand[1]    = Default basic block destination
01221   // Operand[2n  ] = Value to match
01222   // Operand[2n+1] = BasicBlock to go to on match
01223   SwitchInst(const SwitchInst &RI);
01224   void init(Value *Value, BasicBlock *Default, unsigned NumCases);
01225   void resizeOperands(unsigned No);
01226 public:
01227   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
01228   /// switch on and a default destination.  The number of additional cases can
01229   /// be specified here to make memory allocation more efficient.  This
01230   /// constructor can also autoinsert before another instruction.
01231   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
01232              Instruction *InsertBefore = 0)
01233     : TerminatorInst(Instruction::Switch, 0, 0, InsertBefore) {
01234     init(Value, Default, NumCases);
01235   }
01236 
01237   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
01238   /// switch on and a default destination.  The number of additional cases can
01239   /// be specified here to make memory allocation more efficient.  This
01240   /// constructor also autoinserts at the end of the specified BasicBlock.
01241   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
01242              BasicBlock *InsertAtEnd)
01243     : TerminatorInst(Instruction::Switch, 0, 0, InsertAtEnd) {
01244     init(Value, Default, NumCases);
01245   }
01246   ~SwitchInst();
01247 
01248 
01249   // Accessor Methods for Switch stmt
01250   inline Value *getCondition() const { return getOperand(0); }
01251   void setCondition(Value *V) { setOperand(0, V); }
01252 
01253   inline BasicBlock *getDefaultDest() const {
01254     return cast<BasicBlock>(getOperand(1));
01255   }
01256 
01257   /// getNumCases - return the number of 'cases' in this switch instruction.
01258   /// Note that case #0 is always the default case.
01259   unsigned getNumCases() const {
01260     return getNumOperands()/2;
01261   }
01262 
01263   /// getCaseValue - Return the specified case value.  Note that case #0, the
01264   /// default destination, does not have a case value.
01265   ConstantInt *getCaseValue(unsigned i) {
01266     assert(i && i < getNumCases() && "Illegal case value to get!");
01267     return getSuccessorValue(i);
01268   }
01269 
01270   /// getCaseValue - Return the specified case value.  Note that case #0, the
01271   /// default destination, does not have a case value.
01272   const ConstantInt *getCaseValue(unsigned i) const {
01273     assert(i && i < getNumCases() && "Illegal case value to get!");
01274     return getSuccessorValue(i);
01275   }
01276 
01277   /// findCaseValue - Search all of the case values for the specified constant.
01278   /// If it is explicitly handled, return the case number of it, otherwise
01279   /// return 0 to indicate that it is handled by the default handler.
01280   unsigned findCaseValue(const ConstantInt *C) const {
01281     for (unsigned i = 1, e = getNumCases(); i != e; ++i)
01282       if (getCaseValue(i) == C)
01283         return i;
01284     return 0;
01285   }
01286 
01287   /// addCase - Add an entry to the switch instruction...
01288   ///
01289   void addCase(ConstantInt *OnVal, BasicBlock *Dest);
01290 
01291   /// removeCase - This method removes the specified successor from the switch
01292   /// instruction.  Note that this cannot be used to remove the default
01293   /// destination (successor #0).
01294   ///
01295   void removeCase(unsigned idx);
01296 
01297   virtual SwitchInst *clone() const;
01298 
01299   unsigned getNumSuccessors() const { return getNumOperands()/2; }
01300   BasicBlock *getSuccessor(unsigned idx) const {
01301     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
01302     return cast<BasicBlock>(getOperand(idx*2+1));
01303   }
01304   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
01305     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
01306     setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
01307   }
01308 
01309   // getSuccessorValue - Return the value associated with the specified
01310   // successor.
01311   inline ConstantInt *getSuccessorValue(unsigned idx) const {
01312     assert(idx < getNumSuccessors() && "Successor # out of range!");
01313     return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
01314   }
01315 
01316   // Methods for support type inquiry through isa, cast, and dyn_cast:
01317   static inline bool classof(const SwitchInst *) { return true; }
01318   static inline bool classof(const Instruction *I) {
01319     return I->getOpcode() == Instruction::Switch;
01320   }
01321   static inline bool classof(const Value *V) {
01322     return isa<Instruction>(V) && classof(cast<Instruction>(V));
01323   }
01324 private:
01325   virtual BasicBlock *getSuccessorV(unsigned idx) const;
01326   virtual unsigned getNumSuccessorsV() const;
01327   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
01328 };
01329 
01330 //===----------------------------------------------------------------------===//
01331 //                               InvokeInst Class
01332 //===----------------------------------------------------------------------===//
01333 
01334 //===---------------------------------------------------------------------------
01335 
01336 /// InvokeInst - Invoke instruction.  The SubclassData field is used to hold the
01337 /// calling convention of the call.
01338 ///
01339 class InvokeInst : public TerminatorInst {
01340   InvokeInst(const InvokeInst &BI);
01341   void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
01342             const std::vector<Value*> &Params);
01343 public:
01344   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
01345              const std::vector<Value*> &Params, const std::string &Name = "",
01346              Instruction *InsertBefore = 0);
01347   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
01348              const std::vector<Value*> &Params, const std::string &Name,
01349              BasicBlock *InsertAtEnd);
01350   ~InvokeInst();
01351 
01352   virtual InvokeInst *clone() const;
01353 
01354   bool mayWriteToMemory() const { return true; }
01355 
01356   /// getCallingConv/setCallingConv - Get or set the calling convention of this
01357   /// function call.
01358   unsigned getCallingConv() const { return SubclassData; }
01359   void setCallingConv(unsigned CC) {
01360     SubclassData = CC;
01361   }
01362 
01363   /// getCalledFunction - Return the function called, or null if this is an
01364   /// indirect function invocation.
01365   ///
01366   Function *getCalledFunction() const {
01367     return dyn_cast<Function>(getOperand(0));
01368   }
01369 
01370   // getCalledValue - Get a pointer to a function that is invoked by this inst.
01371   inline Value *getCalledValue() const { return getOperand(0); }
01372 
01373   // get*Dest - Return the destination basic blocks...
01374   BasicBlock *getNormalDest() const {
01375     return cast<BasicBlock>(getOperand(1));
01376   }
01377   BasicBlock *getUnwindDest() const {
01378     return cast<BasicBlock>(getOperand(2));
01379   }
01380   void setNormalDest(BasicBlock *B) {
01381     setOperand(1, reinterpret_cast<Value*>(B));
01382   }
01383 
01384   void setUnwindDest(BasicBlock *B) {
01385     setOperand(2, reinterpret_cast<Value*>(B));
01386   }
01387 
01388   inline BasicBlock *getSuccessor(unsigned i) const {
01389     assert(i < 2 && "Successor # out of range for invoke!");
01390     return i == 0 ? getNormalDest() : getUnwindDest();
01391   }
01392 
01393   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
01394     assert(idx < 2 && "Successor # out of range for invoke!");
01395     setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
01396   }
01397 
01398   unsigned getNumSuccessors() const { return 2; }
01399 
01400   // Methods for support type inquiry through isa, cast, and dyn_cast:
01401   static inline bool classof(const InvokeInst *) { return true; }
01402   static inline bool classof(const Instruction *I) {
01403     return (I->getOpcode() == Instruction::Invoke);
01404   }
01405   static inline bool classof(const Value *V) {
01406     return isa<Instruction>(V) && classof(cast<Instruction>(V));
01407   }
01408 private:
01409   virtual BasicBlock *getSuccessorV(unsigned idx) const;
01410   virtual unsigned getNumSuccessorsV() const;
01411   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
01412 };
01413 
01414 
01415 //===----------------------------------------------------------------------===//
01416 //                              UnwindInst Class
01417 //===----------------------------------------------------------------------===//
01418 
01419 //===---------------------------------------------------------------------------
01420 /// UnwindInst - Immediately exit the current function, unwinding the stack
01421 /// until an invoke instruction is found.
01422 ///
01423 class UnwindInst : public TerminatorInst {
01424 public:
01425   UnwindInst(Instruction *InsertBefore = 0)
01426     : TerminatorInst(Instruction::Unwind, 0, 0, InsertBefore) {
01427   }
01428   UnwindInst(BasicBlock *InsertAtEnd)
01429     : TerminatorInst(Instruction::Unwind, 0, 0, InsertAtEnd) {
01430   }
01431 
01432   virtual UnwindInst *clone() const;
01433 
01434   unsigned getNumSuccessors() const { return 0; }
01435 
01436   // Methods for support type inquiry through isa, cast, and dyn_cast:
01437   static inline bool classof(const UnwindInst *) { return true; }
01438   static inline bool classof(const Instruction *I) {
01439     return I->getOpcode() == Instruction::Unwind;
01440   }
01441   static inline bool classof(const Value *V) {
01442     return isa<Instruction>(V) && classof(cast<Instruction>(V));
01443   }
01444 private:
01445   virtual BasicBlock *getSuccessorV(unsigned idx) const;
01446   virtual unsigned getNumSuccessorsV() const;
01447   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
01448 };
01449 
01450 //===----------------------------------------------------------------------===//
01451 //                           UnreachableInst Class
01452 //===----------------------------------------------------------------------===//
01453 
01454 //===---------------------------------------------------------------------------
01455 /// UnreachableInst - This function has undefined behavior.  In particular, the
01456 /// presence of this instruction indicates some higher level knowledge that the
01457 /// end of the block cannot be reached.
01458 ///
01459 class UnreachableInst : public TerminatorInst {
01460 public:
01461   UnreachableInst(Instruction *InsertBefore = 0)
01462     : TerminatorInst(Instruction::Unreachable, 0, 0, InsertBefore) {
01463   }
01464   UnreachableInst(BasicBlock *InsertAtEnd)
01465     : TerminatorInst(Instruction::Unreachable, 0, 0, InsertAtEnd) {
01466   }
01467 
01468   virtual UnreachableInst *clone() const;
01469 
01470   unsigned getNumSuccessors() const { return 0; }
01471 
01472   // Methods for support type inquiry through isa, cast, and dyn_cast:
01473   static inline bool classof(const UnreachableInst *) { return true; }
01474   static inline bool classof(const Instruction *I) {
01475     return I->getOpcode() == Instruction::Unreachable;
01476   }
01477   static inline bool classof(const Value *V) {
01478     return isa<Instruction>(V) && classof(cast<Instruction>(V));
01479   }
01480 private:
01481   virtual BasicBlock *getSuccessorV(unsigned idx) const;
01482   virtual unsigned getNumSuccessorsV() const;
01483   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
01484 };
01485 
01486 } // End llvm namespace
01487 
01488 #endif