1 //===-- llvm/InstrTypes.h - Important Instruction subclasses ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTION_TYPES_H
17 #define LLVM_INSTRUCTION_TYPES_H
19 #include "llvm/Instruction.h"
23 //===----------------------------------------------------------------------===//
24 // TerminatorInst Class
25 //===----------------------------------------------------------------------===//
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.
30 class TerminatorInst : public Instruction {
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) {}
37 TerminatorInst(Instruction::TermOps iType, BasicBlock *InsertAtEnd);
38 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
39 const std::string &Name, BasicBlock *InsertAtEnd)
40 : Instruction(Ty, iType, Name, InsertAtEnd) {}
44 /// Terminators must implement the methods required by Instruction...
45 virtual Instruction *clone() const = 0;
47 /// Additionally, they must provide a method to get at the successors of this
48 /// terminator instruction. 'idx' may not be >= the number of successors
49 /// returned by getNumSuccessors()!
51 virtual const BasicBlock *getSuccessor(unsigned idx) const = 0;
52 virtual unsigned getNumSuccessors() const = 0;
54 /// Set a successor at a given index
55 virtual void setSuccessor(unsigned idx, BasicBlock *B) = 0;
57 inline BasicBlock *getSuccessor(unsigned idx) {
58 const TerminatorInst *TI = this;
59 return const_cast<BasicBlock*>(TI->getSuccessor(idx));
62 // Methods for support type inquiry through isa, cast, and dyn_cast:
63 static inline bool classof(const TerminatorInst *) { return true; }
64 static inline bool classof(const Instruction *I) {
65 return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd;
67 static inline bool classof(const Value *V) {
68 return isa<Instruction>(V) && classof(cast<Instruction>(V));
73 //===----------------------------------------------------------------------===//
74 // BinaryOperator Class
75 //===----------------------------------------------------------------------===//
77 class BinaryOperator : public Instruction {
79 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
80 const std::string &Name, Instruction *InsertBefore);
84 /// create() - Construct a binary instruction, given the opcode and the two
85 /// operands. Optionally (if InstBefore is specified) insert the instruction
86 /// into a BasicBlock right before the specified instruction. The specified
87 /// Instruction is allowed to be a dereferenced end iterator.
89 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
90 const std::string &Name = "",
91 Instruction *InsertBefore = 0);
94 /// Helper functions to construct and inspect unary operations (NEG and NOT)
95 /// via binary operators SUB and XOR:
97 /// createNeg, createNot - Create the NEG and NOT
98 /// instructions out of SUB and XOR instructions.
100 static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
101 Instruction *InsertBefore = 0);
102 static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
103 Instruction *InsertBefore = 0);
105 /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
107 static bool isNeg(const Value *V);
108 static bool isNot(const Value *V);
110 /// getNegArgument, getNotArgument - Helper functions to extract the
111 /// unary argument of a NEG or NOT operation implemented via Sub or Xor.
113 static const Value* getNegArgument(const BinaryOperator* Bop);
114 static Value* getNegArgument( BinaryOperator* Bop);
115 static const Value* getNotArgument(const BinaryOperator* Bop);
116 static Value* getNotArgument( BinaryOperator* Bop);
118 BinaryOps getOpcode() const {
119 return static_cast<BinaryOps>(Instruction::getOpcode());
122 virtual Instruction *clone() const {
123 return create(getOpcode(), Operands[0], Operands[1]);
126 /// swapOperands - Exchange the two operands to this instruction.
127 /// This instruction is safe to use on any binary instruction and
128 /// does not modify the semantics of the instruction. If the
129 /// instruction is order dependent (SetLT f.e.) the opcode is
130 /// changed. If the instruction cannot be reversed (ie, it's a Div),
131 /// then return true.
135 // Methods for support type inquiry through isa, cast, and dyn_cast:
136 static inline bool classof(const BinaryOperator *) { return true; }
137 static inline bool classof(const Instruction *I) {
138 return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd;
140 static inline bool classof(const Value *V) {
141 return isa<Instruction>(V) && classof(cast<Instruction>(V));
145 } // End llvm namespace