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 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 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 
00020 namespace llvm {
00021 
00022 struct AssemblyAnnotationWriter;
00023 class BinaryOperator;
00024 
00025 template<typename SC> struct ilist_traits;
00026 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
00027          typename SubClass> class SymbolTableListTraits;
00028 
00029 class Instruction : public User {
00030   void operator=(const Instruction &);     // Do not implement
00031   Instruction(const Instruction &);        // Do not implement
00032 
00033   BasicBlock *Parent;
00034   Instruction *Prev, *Next; // Next and Prev links for our intrusive linked list
00035 
00036   void setNext(Instruction *N) { Next = N; }
00037   void setPrev(Instruction *N) { Prev = N; }
00038 
00039   friend class SymbolTableListTraits<Instruction, BasicBlock, Function,
00040                                      ilist_traits<Instruction> >;
00041   void setParent(BasicBlock *P);
00042 
00043 private:
00044   // FIXME: This is a dirty hack.  Setcc instructions shouldn't encode the CC
00045   // into the opcode field.  When they don't, this will be unneeded.
00046   void setOpcode(unsigned NewOpcode);
00047   friend class BinaryOperator;
00048 protected:
00049   Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
00050               const std::string &Name = "",
00051               Instruction *InsertBefore = 0);
00052   Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
00053               const std::string &Name, BasicBlock *InsertAtEnd);
00054 public:
00055   // Out of line virtual method, so the vtable, etc has a home.
00056   ~Instruction();
00057   
00058   /// mayWriteToMemory - Return true if this instruction may modify memory.
00059   ///
00060   virtual bool mayWriteToMemory() const { return false; }
00061 
00062   /// clone() - Create a copy of 'this' instruction that is identical in all
00063   /// ways except the following:
00064   ///   * The instruction has no parent
00065   ///   * The instruction has no name
00066   ///
00067   virtual Instruction *clone() const = 0;
00068 
00069   /// isIdenticalTo - Return true if the specified instruction is exactly
00070   /// identical to the current one.  This means that all operands match and any
00071   /// extra information (e.g. load is volatile) agree.
00072   bool isIdenticalTo(Instruction *I) const;
00073 
00074 
00075   // Accessor methods...
00076   //
00077   inline const BasicBlock *getParent() const { return Parent; }
00078   inline       BasicBlock *getParent()       { return Parent; }
00079 
00080   // getNext/Prev - Return the next or previous instruction in the list.  The
00081   // last node in the list is a terminator instruction.
00082         Instruction *getNext()       { return Next; }
00083   const Instruction *getNext() const { return Next; }
00084         Instruction *getPrev()       { return Prev; }
00085   const Instruction *getPrev() const { return Prev; }
00086 
00087   /// removeFromParent - This method unlinks 'this' from the containing basic
00088   /// block, but does not delete it.
00089   ///
00090   void removeFromParent();
00091 
00092   /// eraseFromParent - This method unlinks 'this' from the containing basic
00093   /// block and deletes it.
00094   ///
00095   void eraseFromParent();
00096 
00097   /// moveBefore - Unlink this instruction from its current basic block and
00098   /// insert it into the basic block that MovePos lives in, right before
00099   /// MovePos.
00100   void moveBefore(Instruction *MovePos);
00101 
00102   // ---------------------------------------------------------------------------
00103   /// Subclass classification... getOpcode() returns a member of
00104   /// one of the enums that is coming soon (down below)...
00105   ///
00106   unsigned getOpcode() const { return getValueType() - InstructionVal; }
00107   const char *getOpcodeName() const {
00108     return getOpcodeName(getOpcode());
00109   }
00110   static const char* getOpcodeName(unsigned OpCode);
00111 
00112   static inline bool isTerminator(unsigned OpCode) {
00113     return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
00114   }
00115 
00116   inline bool isTerminator() const {   // Instance of TerminatorInst?
00117     return isTerminator(getOpcode());
00118   }
00119 
00120   inline bool isBinaryOp() const {
00121     return getOpcode() >= BinaryOpsBegin && getOpcode() < BinaryOpsEnd;
00122   }
00123 
00124   /// isAssociative - Return true if the instruction is associative:
00125   ///
00126   ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
00127   ///
00128   /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
00129   /// not applied to floating point types.
00130   ///
00131   bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
00132   static bool isAssociative(unsigned op, const Type *Ty);
00133 
00134   /// isCommutative - Return true if the instruction is commutative:
00135   ///
00136   ///   Commutative operators satisfy: (x op y) === (y op x)
00137   ///
00138   /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
00139   /// applied to any type.
00140   ///
00141   bool isCommutative() const { return isCommutative(getOpcode()); }
00142   static bool isCommutative(unsigned op);
00143 
00144   /// isRelational - Return true if the instruction is a Set* instruction:
00145   ///
00146   bool isRelational() const { return isRelational(getOpcode()); }
00147   static bool isRelational(unsigned op);
00148 
00149 
00150   /// isTrappingInstruction - Return true if the instruction may trap.
00151   ///
00152   bool isTrapping() const {
00153     return isTrapping(getOpcode());
00154   }
00155   static bool isTrapping(unsigned op);
00156 
00157   virtual void print(std::ostream &OS) const { print(OS, 0); }
00158   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
00159 
00160   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00161   static inline bool classof(const Instruction *) { return true; }
00162   static inline bool classof(const Value *V) {
00163     return V->getValueType() >= Value::InstructionVal;
00164   }
00165 
00166   //----------------------------------------------------------------------
00167   // Exported enumerations...
00168   //
00169   enum TermOps {       // These terminate basic blocks
00170 #define  FIRST_TERM_INST(N)             TermOpsBegin = N,
00171 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
00172 #define   LAST_TERM_INST(N)             TermOpsEnd = N+1
00173 #include "llvm/Instruction.def"
00174   };
00175 
00176   enum BinaryOps {
00177 #define  FIRST_BINARY_INST(N)             BinaryOpsBegin = N,
00178 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
00179 #define   LAST_BINARY_INST(N)             BinaryOpsEnd = N+1
00180 #include "llvm/Instruction.def"
00181   };
00182 
00183   enum MemoryOps {
00184 #define  FIRST_MEMORY_INST(N)             MemoryOpsBegin = N,
00185 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
00186 #define   LAST_MEMORY_INST(N)             MemoryOpsEnd = N+1
00187 #include "llvm/Instruction.def"
00188   };
00189 
00190   enum OtherOps {
00191 #define  FIRST_OTHER_INST(N)             OtherOpsBegin = N,
00192 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
00193 #define   LAST_OTHER_INST(N)             OtherOpsEnd = N+1
00194 #include "llvm/Instruction.def"
00195   };
00196 };
00197 
00198 } // End llvm namespace
00199 
00200 #endif