LLVM API Documentation

InstrTypes.h

Go to the documentation of this file.
00001 //===-- llvm/InstrTypes.h - Important Instruction subclasses ----*- 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 defines various meta classes of instructions that exist in the VM
00011 // representation.  Specific concrete subclasses of these may be found in the
00012 // i*.h files...
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_INSTRUCTION_TYPES_H
00017 #define LLVM_INSTRUCTION_TYPES_H
00018 
00019 #include "llvm/Instruction.h"
00020 
00021 namespace llvm {
00022 
00023 //===----------------------------------------------------------------------===//
00024 //                            TerminatorInst Class
00025 //===----------------------------------------------------------------------===//
00026 
00027 /// TerminatorInst - Subclasses of this class are all able to terminate a basic
00028 /// block.  Thus, these are all the flow control type of operations.
00029 ///
00030 class TerminatorInst : public Instruction {
00031 protected:
00032   TerminatorInst(Instruction::TermOps iType, Use *Ops, unsigned NumOps,
00033                  Instruction *InsertBefore = 0);
00034   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
00035                   Use *Ops, unsigned NumOps,
00036                  const std::string &Name = "", Instruction *InsertBefore = 0)
00037     : Instruction(Ty, iType, Ops, NumOps, Name, InsertBefore) {}
00038 
00039   TerminatorInst(Instruction::TermOps iType, Use *Ops, unsigned NumOps,
00040                  BasicBlock *InsertAtEnd);
00041   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
00042                   Use *Ops, unsigned NumOps,
00043                  const std::string &Name, BasicBlock *InsertAtEnd)
00044     : Instruction(Ty, iType, Ops, NumOps, Name, InsertAtEnd) {}
00045 
00046   // Out of line virtual method, so the vtable, etc has a home.
00047   ~TerminatorInst();
00048 
00049   /// Virtual methods - Terminators should overload these and provide inline
00050   /// overrides of non-V methods.
00051   virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
00052   virtual unsigned getNumSuccessorsV() const = 0;
00053   virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
00054 public:
00055 
00056   virtual Instruction *clone() const = 0;
00057 
00058   /// getNumSuccessors - Return the number of successors that this terminator
00059   /// has.
00060   unsigned getNumSuccessors() const {
00061     return getNumSuccessorsV();
00062   }
00063 
00064   /// getSuccessor - Return the specified successor.
00065   ///
00066   BasicBlock *getSuccessor(unsigned idx) const {
00067     return getSuccessorV(idx);
00068   }
00069 
00070   /// setSuccessor - Update the specified successor to point at the provided
00071   /// block.
00072   void setSuccessor(unsigned idx, BasicBlock *B) {
00073     setSuccessorV(idx, B);
00074   }
00075 
00076   // Methods for support type inquiry through isa, cast, and dyn_cast:
00077   static inline bool classof(const TerminatorInst *) { return true; }
00078   static inline bool classof(const Instruction *I) {
00079     return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd;
00080   }
00081   static inline bool classof(const Value *V) {
00082     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00083   }
00084 };
00085 
00086 //===----------------------------------------------------------------------===//
00087 //                          UnaryInstruction Class
00088 //===----------------------------------------------------------------------===//
00089 
00090 class UnaryInstruction : public Instruction {
00091   Use Op;
00092 protected:
00093   UnaryInstruction(const Type *Ty, unsigned iType, Value *V,
00094                    const std::string &Name = "", Instruction *IB = 0)
00095     : Instruction(Ty, iType, &Op, 1, Name, IB), Op(V, this) {
00096   }
00097   UnaryInstruction(const Type *Ty, unsigned iType, Value *V,
00098                    const std::string &Name, BasicBlock *IAE)
00099     : Instruction(Ty, iType, &Op, 1, Name, IAE), Op(V, this) {
00100   }
00101 public:
00102   // Out of line virtual method, so the vtable, etc has a home.
00103   ~UnaryInstruction();
00104 
00105   // Transparently provide more efficient getOperand methods.
00106   Value *getOperand(unsigned i) const {
00107     assert(i == 0 && "getOperand() out of range!");
00108     return Op;
00109   }
00110   void setOperand(unsigned i, Value *Val) {
00111     assert(i == 0 && "setOperand() out of range!");
00112     Op = Val;
00113   }
00114   unsigned getNumOperands() const { return 1; }
00115 };
00116 
00117 //===----------------------------------------------------------------------===//
00118 //                           BinaryOperator Class
00119 //===----------------------------------------------------------------------===//
00120 
00121 class BinaryOperator : public Instruction {
00122   Use Ops[2];
00123 protected:
00124   void init(BinaryOps iType);
00125   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
00126                  const std::string &Name, Instruction *InsertBefore)
00127     : Instruction(Ty, iType, Ops, 2, Name, InsertBefore) {
00128       Ops[0].init(S1, this);
00129       Ops[1].init(S2, this);
00130     init(iType);
00131   }
00132   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
00133                  const std::string &Name, BasicBlock *InsertAtEnd)
00134     : Instruction(Ty, iType, Ops, 2, Name, InsertAtEnd) {
00135     Ops[0].init(S1, this);
00136     Ops[1].init(S2, this);
00137     init(iType);
00138   }
00139 
00140 public:
00141 
00142   /// Transparently provide more efficient getOperand methods.
00143   Value *getOperand(unsigned i) const {
00144     assert(i < 2 && "getOperand() out of range!");
00145     return Ops[i];
00146   }
00147   void setOperand(unsigned i, Value *Val) {
00148     assert(i < 2 && "setOperand() out of range!");
00149     Ops[i] = Val;
00150   }
00151   unsigned getNumOperands() const { return 2; }
00152 
00153   /// create() - Construct a binary instruction, given the opcode and the two
00154   /// operands.  Optionally (if InstBefore is specified) insert the instruction
00155   /// into a BasicBlock right before the specified instruction.  The specified
00156   /// Instruction is allowed to be a dereferenced end iterator.
00157   ///
00158   static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
00159                                 const std::string &Name = "",
00160                                 Instruction *InsertBefore = 0);
00161 
00162   /// create() - Construct a binary instruction, given the opcode and the two
00163   /// operands.  Also automatically insert this instruction to the end of the
00164   /// BasicBlock specified.
00165   ///
00166   static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
00167                                 const std::string &Name,
00168                                 BasicBlock *InsertAtEnd);
00169 
00170   /// create* - These methods just forward to create, and are useful when you
00171   /// statically know what type of instruction you're going to create.  These
00172   /// helpers just save some typing.
00173 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
00174   static BinaryOperator *create##OPC(Value *V1, Value *V2, \
00175                                      const std::string &Name = "") {\
00176     return create(Instruction::OPC, V1, V2, Name);\
00177   }
00178 #include "llvm/Instruction.def"
00179 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
00180   static BinaryOperator *create##OPC(Value *V1, Value *V2, \
00181                                      const std::string &Name, BasicBlock *BB) {\
00182     return create(Instruction::OPC, V1, V2, Name, BB);\
00183   }
00184 #include "llvm/Instruction.def"
00185 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
00186   static BinaryOperator *create##OPC(Value *V1, Value *V2, \
00187                                      const std::string &Name, Instruction *I) {\
00188     return create(Instruction::OPC, V1, V2, Name, I);\
00189   }
00190 #include "llvm/Instruction.def"
00191 
00192 
00193   /// Helper functions to construct and inspect unary operations (NEG and NOT)
00194   /// via binary operators SUB and XOR:
00195   ///
00196   /// createNeg, createNot - Create the NEG and NOT
00197   ///     instructions out of SUB and XOR instructions.
00198   ///
00199   static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
00200                                    Instruction *InsertBefore = 0);
00201   static BinaryOperator *createNeg(Value *Op, const std::string &Name,
00202                                    BasicBlock *InsertAtEnd);
00203   static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
00204                                    Instruction *InsertBefore = 0);
00205   static BinaryOperator *createNot(Value *Op, const std::string &Name,
00206                                    BasicBlock *InsertAtEnd);
00207 
00208   /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
00209   ///
00210   static bool            isNeg(const Value *V);
00211   static bool            isNot(const Value *V);
00212 
00213   /// getNegArgument, getNotArgument - Helper functions to extract the
00214   ///     unary argument of a NEG or NOT operation implemented via Sub or Xor.
00215   ///
00216   static const Value*    getNegArgument(const Value *BinOp);
00217   static       Value*    getNegArgument(      Value *BinOp);
00218   static const Value*    getNotArgument(const Value *BinOp);
00219   static       Value*    getNotArgument(      Value *BinOp);
00220 
00221   BinaryOps getOpcode() const {
00222     return static_cast<BinaryOps>(Instruction::getOpcode());
00223   }
00224 
00225   virtual BinaryOperator *clone() const;
00226 
00227   /// swapOperands - Exchange the two operands to this instruction.
00228   /// This instruction is safe to use on any binary instruction and
00229   /// does not modify the semantics of the instruction.  If the
00230   /// instruction is order dependent (SetLT f.e.) the opcode is
00231   /// changed.  If the instruction cannot be reversed (ie, it's a Div),
00232   /// then return true.
00233   ///
00234   bool swapOperands();
00235 
00236   // Methods for support type inquiry through isa, cast, and dyn_cast:
00237   static inline bool classof(const BinaryOperator *) { return true; }
00238   static inline bool classof(const Instruction *I) {
00239     return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd;
00240   }
00241   static inline bool classof(const Value *V) {
00242     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00243   }
00244 };
00245 
00246 } // End llvm namespace
00247 
00248 #endif