LLVM API Documentation

Instruction.h

Go to the documentation of this file.
00001 //===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the declaration of the Instruction class, which is the
00011 // base class for all of the LLVM instructions.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_INSTRUCTION_H
00016 #define LLVM_INSTRUCTION_H
00017 
00018 #include "llvm/User.h"
00019 #include "llvm/ADT/ilist_node.h"
00020 
00021 namespace llvm {
00022 
00023 template<typename ValueSubClass, typename ItemParentClass>
00024   class SymbolTableListTraits;
00025 
00026 class Instruction : public User, public ilist_node<Instruction> {
00027   void operator=(const Instruction &);     // Do not implement
00028   Instruction(const Instruction &);        // Do not implement
00029 
00030   BasicBlock *Parent;
00031 
00032   friend class SymbolTableListTraits<Instruction, BasicBlock>;
00033   void setParent(BasicBlock *P);
00034 protected:
00035   Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
00036               Instruction *InsertBefore = 0);
00037   Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
00038               BasicBlock *InsertAtEnd);
00039 public:
00040   // Out of line virtual method, so the vtable, etc has a home.
00041   ~Instruction();
00042   
00043   /// mayWriteToMemory - Return true if this instruction may modify memory.
00044   ///
00045   bool mayWriteToMemory() const;
00046 
00047   /// mayReadFromMemory - Return true if this instruction may read memory.
00048   ///
00049   bool mayReadFromMemory() const;
00050   
00051   /// clone() - Create a copy of 'this' instruction that is identical in all
00052   /// ways except the following:
00053   ///   * The instruction has no parent
00054   ///   * The instruction has no name
00055   ///
00056   virtual Instruction *clone() const = 0;
00057 
00058   /// isIdenticalTo - Return true if the specified instruction is exactly
00059   /// identical to the current one.  This means that all operands match and any
00060   /// extra information (e.g. load is volatile) agree.
00061   bool isIdenticalTo(const Instruction *I) const;
00062 
00063   /// This function determines if the specified instruction executes the same
00064   /// operation as the current one. This means that the opcodes, type, operand
00065   /// types and any other factors affecting the operation must be the same. This
00066   /// is similar to isIdenticalTo except the operands themselves don't have to
00067   /// be identical.
00068   /// @returns true if the specified instruction is the same operation as
00069   /// the current one.
00070   /// @brief Determine if one instruction is the same operation as another.
00071   bool isSameOperationAs(const Instruction *I) const;
00072 
00073   /// isUsedOutsideOfBlock - Return true if there are any uses of this
00074   /// instruction in blocks other than the specified block.  Note that PHI nodes
00075   /// are considered to evaluate their operands in the corresponding predecessor
00076   /// block.
00077   bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
00078   
00079   
00080   /// use_back - Specialize the methods defined in Value, as we know that an
00081   /// instruction can only be used by other instructions.
00082   Instruction       *use_back()       { return cast<Instruction>(*use_begin());}
00083   const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
00084   
00085   // Accessor methods...
00086   //
00087   inline const BasicBlock *getParent() const { return Parent; }
00088   inline       BasicBlock *getParent()       { return Parent; }
00089 
00090   /// removeFromParent - This method unlinks 'this' from the containing basic
00091   /// block, but does not delete it.
00092   ///
00093   void removeFromParent();
00094 
00095   /// eraseFromParent - This method unlinks 'this' from the containing basic
00096   /// block and deletes it.
00097   ///
00098   void eraseFromParent();
00099 
00100   /// insertBefore - Insert an unlinked instructions into a basic block
00101   /// immediately before the specified instruction.
00102   void insertBefore(Instruction *InsertPos);
00103 
00104   /// insertAfter - Insert an unlinked instructions into a basic block
00105   /// immediately after the specified instruction.
00106   void insertAfter(Instruction *InsertPos);
00107 
00108   /// moveBefore - Unlink this instruction from its current basic block and
00109   /// insert it into the basic block that MovePos lives in, right before
00110   /// MovePos.
00111   void moveBefore(Instruction *MovePos);
00112 
00113   // ---------------------------------------------------------------------------
00114   /// Subclass classification... getOpcode() returns a member of
00115   /// one of the enums that is coming soon (down below)...
00116   ///
00117   unsigned getOpcode() const { return getValueID() - InstructionVal; }
00118   const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
00119   bool isTerminator() const { return isTerminator(getOpcode()); }
00120   bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
00121   bool isShift() { return isShift(getOpcode()); }
00122   bool isCast() const { return isCast(getOpcode()); }
00123   
00124   
00125   
00126   static const char* getOpcodeName(unsigned OpCode);
00127 
00128   static inline bool isTerminator(unsigned OpCode) {
00129     return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
00130   }
00131 
00132   static inline bool isBinaryOp(unsigned Opcode) {
00133     return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
00134   }
00135 
00136   /// @brief Determine if the Opcode is one of the shift instructions.
00137   static inline bool isShift(unsigned Opcode) {
00138     return Opcode >= Shl && Opcode <= AShr;
00139   }
00140 
00141   /// isLogicalShift - Return true if this is a logical shift left or a logical
00142   /// shift right.
00143   inline bool isLogicalShift() const {
00144     return getOpcode() == Shl || getOpcode() == LShr;
00145   }
00146 
00147   /// isLogicalShift - Return true if this is a logical shift left or a logical
00148   /// shift right.
00149   inline bool isArithmeticShift() const {
00150     return getOpcode() == AShr;
00151   }
00152 
00153   /// @brief Determine if the OpCode is one of the CastInst instructions.
00154   static inline bool isCast(unsigned OpCode) {
00155     return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
00156   }
00157 
00158   /// isAssociative - Return true if the instruction is associative:
00159   ///
00160   ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
00161   ///
00162   /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
00163   /// not applied to floating point types.
00164   ///
00165   bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
00166   static bool isAssociative(unsigned op, const Type *Ty);
00167 
00168   /// isCommutative - Return true if the instruction is commutative:
00169   ///
00170   ///   Commutative operators satisfy: (x op y) === (y op x)
00171   ///
00172   /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
00173   /// applied to any type.
00174   ///
00175   bool isCommutative() const { return isCommutative(getOpcode()); }
00176   static bool isCommutative(unsigned op);
00177 
00178   /// isTrapping - Return true if the instruction may trap.
00179   ///
00180   bool isTrapping() const {
00181     return isTrapping(getOpcode());
00182   }
00183   static bool isTrapping(unsigned op);
00184 
00185   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00186   static inline bool classof(const Instruction *) { return true; }
00187   static inline bool classof(const Value *V) {
00188     return V->getValueID() >= Value::InstructionVal;
00189   }
00190 
00191   //----------------------------------------------------------------------
00192   // Exported enumerations...
00193   //
00194   enum TermOps {       // These terminate basic blocks
00195 #define  FIRST_TERM_INST(N)             TermOpsBegin = N,
00196 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
00197 #define   LAST_TERM_INST(N)             TermOpsEnd = N+1
00198 #include "llvm/Instruction.def"
00199   };
00200 
00201   enum BinaryOps {
00202 #define  FIRST_BINARY_INST(N)             BinaryOpsBegin = N,
00203 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
00204 #define   LAST_BINARY_INST(N)             BinaryOpsEnd = N+1
00205 #include "llvm/Instruction.def"
00206   };
00207 
00208   enum MemoryOps {
00209 #define  FIRST_MEMORY_INST(N)             MemoryOpsBegin = N,
00210 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
00211 #define   LAST_MEMORY_INST(N)             MemoryOpsEnd = N+1
00212 #include "llvm/Instruction.def"
00213   };
00214 
00215   enum CastOps {
00216 #define  FIRST_CAST_INST(N)             CastOpsBegin = N,
00217 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
00218 #define   LAST_CAST_INST(N)             CastOpsEnd = N+1
00219 #include "llvm/Instruction.def"
00220   };
00221 
00222   enum OtherOps {
00223 #define  FIRST_OTHER_INST(N)             OtherOpsBegin = N,
00224 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
00225 #define   LAST_OTHER_INST(N)             OtherOpsEnd = N+1
00226 #include "llvm/Instruction.def"
00227   };
00228 };
00229 
00230 } // End llvm namespace
00231 
00232 #endif