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