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