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