- Rename Instruction::First*Op to *OpsBegin, and Num*Ops to *OpsEnd to
[oota-llvm.git] / include / llvm / InstrTypes.h
1 //===-- llvm/InstrTypes.h - Important Instruction subclasses -----*- C++ -*--=//
2 //
3 // This file defines various meta classes of instructions that exist in the VM
4 // representation.  Specific concrete subclasses of these may be found in the 
5 // i*.h files...
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_INSTRUCTION_TYPES_H
10 #define LLVM_INSTRUCTION_TYPES_H
11
12 #include "llvm/Instruction.h"
13
14 //===----------------------------------------------------------------------===//
15 //                            TerminatorInst Class
16 //===----------------------------------------------------------------------===//
17
18 /// TerminatorInst - Subclasses of this class are all able to terminate a basic 
19 /// block.  Thus, these are all the flow control type of operations.
20 ///
21 class TerminatorInst : public Instruction {
22 protected:
23   TerminatorInst(Instruction::TermOps iType, Instruction *InsertBefore = 0);
24   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
25                  const std::string &Name = "", Instruction *InsertBefore = 0)
26     : Instruction(Ty, iType, Name, InsertBefore) {
27   }
28 public:
29
30   /// Terminators must implement the methods required by Instruction...
31   virtual Instruction *clone() const = 0;
32
33   /// Additionally, they must provide a method to get at the successors of this
34   /// terminator instruction.  'idx' may not be >= the number of successors
35   /// returned by getNumSuccessors()!
36   ///
37   virtual const BasicBlock *getSuccessor(unsigned idx) const = 0;
38   virtual unsigned getNumSuccessors() const = 0;
39   
40   /// Set a successor at a given index
41   virtual void setSuccessor(unsigned idx, BasicBlock *B) = 0;
42
43   inline BasicBlock *getSuccessor(unsigned idx) {
44     return (BasicBlock*)((const TerminatorInst *)this)->getSuccessor(idx);
45   }
46
47   // Methods for support type inquiry through isa, cast, and dyn_cast:
48   static inline bool classof(const TerminatorInst *) { return true; }
49   static inline bool classof(const Instruction *I) {
50     return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd;
51   }
52   static inline bool classof(const Value *V) {
53     return isa<Instruction>(V) && classof(cast<Instruction>(V));
54   }
55 };
56
57
58 //===----------------------------------------------------------------------===//
59 //                           BinaryOperator Class
60 //===----------------------------------------------------------------------===//
61
62 class BinaryOperator : public Instruction {
63 protected:
64   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
65                  const std::string &Name, Instruction *InsertBefore);
66
67 public:
68
69   /// create() - Construct a binary instruction, given the opcode and the two
70   /// operands.  Optionally (if InstBefore is specified) insert the instruction
71   /// into a BasicBlock right before the specified instruction.  The specified
72   /// Instruction is allowed to be a dereferenced end iterator.
73   ///
74   static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
75                                 const std::string &Name = "",
76                                 Instruction *InsertBefore = 0);
77                                
78
79   /// Helper functions to construct and inspect unary operations (NEG and NOT)
80   /// via binary operators SUB and XOR:
81   /// 
82   /// createNeg, createNot - Create the NEG and NOT
83   ///     instructions out of SUB and XOR instructions.
84   ///
85   static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
86                                    Instruction *InsertBefore = 0);
87   static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
88                                    Instruction *InsertBefore = 0);
89
90   /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
91   ///
92   static bool            isNeg(const Value *V);
93   static bool            isNot(const Value *V);
94
95   /// getNegArgument, getNotArgument - Helper functions to extract the
96   ///     unary argument of a NEG or NOT operation implemented via Sub or Xor.
97   /// 
98   static const Value*    getNegArgument(const BinaryOperator* Bop);
99   static       Value*    getNegArgument(      BinaryOperator* Bop);
100   static const Value*    getNotArgument(const BinaryOperator* Bop);
101   static       Value*    getNotArgument(      BinaryOperator* Bop);
102
103   BinaryOps getOpcode() const { 
104     return (BinaryOps)Instruction::getOpcode();
105   }
106
107   virtual Instruction *clone() const {
108     return create(getOpcode(), Operands[0], Operands[1]);
109   }
110
111   /// swapOperands - Exchange the two operands to this instruction.
112   /// This instruction is safe to use on any binary instruction and
113   /// does not modify the semantics of the instruction.  If the
114   /// instruction is order dependant (SetLT f.e.) the opcode is
115   /// changed.  If the instruction cannot be reversed (ie, it's a Div),
116   /// then return true.
117   ///
118   bool swapOperands();
119
120   // Methods for support type inquiry through isa, cast, and dyn_cast:
121   static inline bool classof(const BinaryOperator *) { return true; }
122   static inline bool classof(const Instruction *I) {
123     return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd; 
124   }
125   static inline bool classof(const Value *V) {
126     return isa<Instruction>(V) && classof(cast<Instruction>(V));
127   }
128 };
129
130 #endif