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 class LLVMContext;
00024 
00025 template<typename ValueSubClass, typename ItemParentClass>
00026   class SymbolTableListTraits;
00027 
00028 class Instruction : public User, public ilist_node<Instruction> {
00029   void operator=(const Instruction &);     // Do not implement
00030   Instruction(const Instruction &);        // Do not implement
00031 
00032   BasicBlock *Parent;
00033 
00034   friend class SymbolTableListTraits<Instruction, BasicBlock>;
00035   void setParent(BasicBlock *P);
00036 protected:
00037   Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
00038               Instruction *InsertBefore = 0);
00039   Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
00040               BasicBlock *InsertAtEnd);
00041 public:
00042   // Out of line virtual method, so the vtable, etc has a home.
00043   ~Instruction();
00044   
00045   /// clone() - Create a copy of 'this' instruction that is identical in all
00046   /// ways except the following:
00047   ///   * The instruction has no parent
00048   ///   * The instruction has no name
00049   ///
00050   virtual Instruction *clone(LLVMContext &Context) const = 0;
00051 
00052   /// isIdenticalTo - Return true if the specified instruction is exactly
00053   /// identical to the current one.  This means that all operands match and any
00054   /// extra information (e.g. load is volatile) agree.
00055   bool isIdenticalTo(const Instruction *I) const;
00056 
00057   /// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
00058   /// ignores the SubclassOptionalData flags, which specify conditions
00059   /// under which the instruction's result is undefined.
00060   bool isIdenticalToWhenDefined(const Instruction *I) const;
00061 
00062   /// This function determines if the specified instruction executes the same
00063   /// operation as the current one. This means that the opcodes, type, operand
00064   /// types and any other factors affecting the operation must be the same. This
00065   /// is similar to isIdenticalTo except the operands themselves don't have to
00066   /// be identical.
00067   /// @returns true if the specified instruction is the same operation as
00068   /// the current one.
00069   /// @brief Determine if one instruction is the same operation as another.
00070   bool isSameOperationAs(const Instruction *I) const;
00071 
00072   /// isUsedOutsideOfBlock - Return true if there are any uses of this
00073   /// instruction in blocks other than the specified block.  Note that PHI nodes
00074   /// are considered to evaluate their operands in the corresponding predecessor
00075   /// block.
00076   bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
00077   
00078   
00079   /// use_back - Specialize the methods defined in Value, as we know that an
00080   /// instruction can only be used by other instructions.
00081   Instruction       *use_back()       { return cast<Instruction>(*use_begin());}
00082   const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
00083   
00084   // Accessor methods...
00085   //
00086   inline const BasicBlock *getParent() const { return Parent; }
00087   inline       BasicBlock *getParent()       { return Parent; }
00088 
00089   /// removeFromParent - This method unlinks 'this' from the containing basic
00090   /// block, but does not delete it.
00091   ///
00092   void removeFromParent();
00093 
00094   /// eraseFromParent - This method unlinks 'this' from the containing basic
00095   /// block and deletes it.
00096   ///
00097   void eraseFromParent();
00098 
00099   /// insertBefore - Insert an unlinked instructions into a basic block
00100   /// immediately before the specified instruction.
00101   void insertBefore(Instruction *InsertPos);
00102 
00103   /// insertAfter - Insert an unlinked instructions into a basic block
00104   /// immediately after the specified instruction.
00105   void insertAfter(Instruction *InsertPos);
00106 
00107   /// moveBefore - Unlink this instruction from its current basic block and
00108   /// insert it into the basic block that MovePos lives in, right before
00109   /// MovePos.
00110   void moveBefore(Instruction *MovePos);
00111 
00112   // ---------------------------------------------------------------------------
00113   /// Subclass classification... getOpcode() returns a member of
00114   /// one of the enums that is coming soon (down below)...
00115   ///
00116   unsigned getOpcode() const { return getValueID() - InstructionVal; }
00117   const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
00118   bool isTerminator() const { return isTerminator(getOpcode()); }
00119   bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
00120   bool isShift() { return isShift(getOpcode()); }
00121   bool isCast() const { return isCast(getOpcode()); }
00122   
00123   
00124   
00125   static const char* getOpcodeName(unsigned OpCode);
00126 
00127   static inline bool isTerminator(unsigned OpCode) {
00128     return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
00129   }
00130 
00131   static inline bool isBinaryOp(unsigned Opcode) {
00132     return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
00133   }
00134 
00135   /// @brief Determine if the Opcode is one of the shift instructions.
00136   static inline bool isShift(unsigned Opcode) {
00137     return Opcode >= Shl && Opcode <= AShr;
00138   }
00139 
00140   /// isLogicalShift - Return true if this is a logical shift left or a logical
00141   /// shift right.
00142   inline bool isLogicalShift() const {
00143     return getOpcode() == Shl || getOpcode() == LShr;
00144   }
00145 
00146   /// isArithmeticShift - Return true if this is an arithmetic shift right.
00147   inline bool isArithmeticShift() const {
00148     return getOpcode() == AShr;
00149   }
00150 
00151   /// @brief Determine if the OpCode is one of the CastInst instructions.
00152   static inline bool isCast(unsigned OpCode) {
00153     return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
00154   }
00155 
00156   /// isAssociative - Return true if the instruction is associative:
00157   ///
00158   ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
00159   ///
00160   /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
00161   /// not applied to floating point types.
00162   ///
00163   bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
00164   static bool isAssociative(unsigned op, const Type *Ty);
00165 
00166   /// isCommutative - Return true if the instruction is commutative:
00167   ///
00168   ///   Commutative operators satisfy: (x op y) === (y op x)
00169   ///
00170   /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
00171   /// applied to any type.
00172   ///
00173   bool isCommutative() const { return isCommutative(getOpcode()); }
00174   static bool isCommutative(unsigned op);
00175 
00176   /// mayWriteToMemory - Return true if this instruction may modify memory.
00177   ///
00178   bool mayWriteToMemory() const;
00179 
00180   /// mayReadFromMemory - Return true if this instruction may read memory.
00181   ///
00182   bool mayReadFromMemory() const;
00183 
00184   /// mayThrow - Return true if this instruction may throw an exception.
00185   ///
00186   bool mayThrow() const;
00187 
00188   /// mayHaveSideEffects - Return true if the instruction may have side effects.
00189   ///
00190   /// Note that this does not consider malloc and alloca to have side
00191   /// effects because the newly allocated memory is completely invisible to
00192   /// instructions which don't used the returned value.  For cases where this
00193   /// matters, isSafeToSpeculativelyExecute may be more appropriate.
00194   bool mayHaveSideEffects() const {
00195     return mayWriteToMemory() || mayThrow();
00196   }
00197 
00198   /// isSafeToSpeculativelyExecute - Return true if the instruction does not
00199   /// have any effects besides calculating the result and does not have
00200   /// undefined behavior.
00201   ///
00202   /// This method never returns true for an instruction that returns true for
00203   /// mayHaveSideEffects; however, this method also does some other checks in
00204   /// addition. It checks for undefined behavior, like dividing by zero or
00205   /// loading from an invalid pointer (but not for undefined results, like a
00206   /// shift with a shift amount larger than the width of the result). It checks
00207   /// for malloc and alloca because speculatively executing them might cause a
00208   /// memory leak. It also returns false for instructions related to control
00209   /// flow, specifically terminators and PHI nodes.
00210   ///
00211   /// This method only looks at the instruction itself and its operands, so if
00212   /// this method returns true, it is safe to move the instruction as long as
00213   /// the correct dominance relationships for the operands and users hold.
00214   /// However, this method can return true for instructions that read memory;
00215   /// for such instructions, moving them may change the resulting value.
00216   bool isSafeToSpeculativelyExecute() const;
00217 
00218   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00219   static inline bool classof(const Instruction *) { return true; }
00220   static inline bool classof(const Value *V) {
00221     return V->getValueID() >= Value::InstructionVal;
00222   }
00223 
00224   //----------------------------------------------------------------------
00225   // Exported enumerations...
00226   //
00227   enum TermOps {       // These terminate basic blocks
00228 #define  FIRST_TERM_INST(N)             TermOpsBegin = N,
00229 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
00230 #define   LAST_TERM_INST(N)             TermOpsEnd = N+1
00231 #include "llvm/Instruction.def"
00232   };
00233 
00234   enum BinaryOps {
00235 #define  FIRST_BINARY_INST(N)             BinaryOpsBegin = N,
00236 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
00237 #define   LAST_BINARY_INST(N)             BinaryOpsEnd = N+1
00238 #include "llvm/Instruction.def"
00239   };
00240 
00241   enum MemoryOps {
00242 #define  FIRST_MEMORY_INST(N)             MemoryOpsBegin = N,
00243 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
00244 #define   LAST_MEMORY_INST(N)             MemoryOpsEnd = N+1
00245 #include "llvm/Instruction.def"
00246   };
00247 
00248   enum CastOps {
00249 #define  FIRST_CAST_INST(N)             CastOpsBegin = N,
00250 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
00251 #define   LAST_CAST_INST(N)             CastOpsEnd = N+1
00252 #include "llvm/Instruction.def"
00253   };
00254 
00255   enum OtherOps {
00256 #define  FIRST_OTHER_INST(N)             OtherOpsBegin = N,
00257 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
00258 #define   LAST_OTHER_INST(N)             OtherOpsEnd = N+1
00259 #include "llvm/Instruction.def"
00260   };
00261 };
00262 
00263 // Instruction* is only 4-byte aligned.
00264 template<>
00265 class PointerLikeTypeTraits<Instruction*> {
00266   typedef Instruction* PT;
00267 public:
00268   static inline void *getAsVoidPointer(PT P) { return P; }
00269   static inline PT getFromVoidPointer(void *P) {
00270     return static_cast<PT>(P);
00271   }
00272   enum { NumLowBitsAvailable = 2 };
00273 };
00274   
00275 } // End llvm namespace
00276 
00277 #endif