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