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