1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
//===-- llvm/iTerminators.h - Termintator instruction nodes -----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains the declarations for all the subclasses of the Instruction
// class which represent "terminator" instructions. Terminator instructions are
// the only instructions allowed and required to terminate a BasicBlock.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_ITERMINATORS_H
#define LLVM_ITERMINATORS_H
#include "llvm/InstrTypes.h"
namespace llvm {
//===---------------------------------------------------------------------------
/// ReturnInst - Return a value (possibly void), from a function. Execution
/// does not continue in this function any longer.
///
class ReturnInst : public TerminatorInst {
ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret) {
if (RI.Operands.size()) {
assert(RI.Operands.size() == 1 && "Return insn can only have 1 operand!");
Operands.reserve(1);
Operands.push_back(Use(RI.Operands[0], this));
}
}
public:
// ReturnInst constructors:
// ReturnInst() - 'ret void' instruction
// ReturnInst(Value* X) - 'ret X' instruction
// ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
// ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
// ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
// ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
: TerminatorInst(Instruction::Ret, InsertBefore) {
if (RetVal) {
Operands.reserve(1);
Operands.push_back(Use(RetVal, this));
}
}
ReturnInst(Value *RetVal, BasicBlock *InsertAtEnd)
: TerminatorInst(Instruction::Ret, InsertAtEnd) {
if (RetVal) {
Operands.reserve(1);
Operands.push_back(Use(RetVal, this));
}
}
virtual Instruction *clone() const { return new ReturnInst(*this); }
inline const Value *getReturnValue() const {
return Operands.size() ? Operands[0].get() : 0;
}
inline Value *getReturnValue() {
return Operands.size() ? Operands[0].get() : 0;
}
virtual const BasicBlock *getSuccessor(unsigned idx) const {
assert(0 && "ReturnInst has no successors!");
abort();
return 0;
}
virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
virtual unsigned getNumSuccessors() const { return 0; }
// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const ReturnInst *) { return true; }
static inline bool classof(const Instruction *I) {
return (I->getOpcode() == Instruction::Ret);
}
static inline bool classof(const Value *V) {
return isa<Instruction>(V) && classof(cast<Instruction>(V));
}
};
//===---------------------------------------------------------------------------
/// BranchInst - Conditional or Unconditional Branch instruction.
///
class BranchInst : public TerminatorInst {
BranchInst(const BranchInst &BI);
public:
// BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
// BranchInst(BB *B) - 'br B'
// BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
// BranchInst(BB* B, Inst *I) - 'br B' insert before I
// BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
// BranchInst(BB* B, BB *I) - 'br B' insert at end
// BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *cond,
Instruction *InsertBefore = 0);
BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *cond,
BasicBlock *InsertAtEnd);
virtual Instruction *clone() const { return new BranchInst(*this); }
inline bool isUnconditional() const { return Operands.size() == 1; }
inline bool isConditional() const { return Operands.size() == 3; }
inline Value *getCondition() const {
return isUnconditional() ? 0 : reinterpret_cast<Value*>(Operands[2].get());
}
void setCondition(Value *V) {
assert(isConditional() && "Cannot set condition of unconditional branch!");
setOperand(2, V);
}
// setUnconditionalDest - Change the current branch to an unconditional branch
// targeting the specified block.
//
void setUnconditionalDest(BasicBlock *Dest) {
if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
Operands[0] = reinterpret_cast<Value*>(Dest);
}
virtual const BasicBlock *getSuccessor(unsigned i) const {
assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
return (i == 0) ? cast<BasicBlock>(Operands[0].get()) :
cast<BasicBlock>(Operands[1].get());
}
inline BasicBlock *getSuccessor(unsigned idx) {
const BranchInst *BI = this;
return const_cast<BasicBlock*>(BI->getSuccessor(idx));
}
virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Operands[idx] = reinterpret_cast<Value*>(NewSucc);
}
virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const BranchInst *) { return true; }
static inline bool classof(const Instruction *I) {
return (I->getOpcode() == Instruction::Br);
}
static inline bool classof(const Value *V) {
return isa<Instruction>(V) && classof(cast<Instruction>(V));
}
};
//===---------------------------------------------------------------------------
/// SwitchInst - Multiway switch
///
class SwitchInst : public TerminatorInst {
// Operand[0] = Value to switch on
// Operand[1] = Default basic block destination
// Operand[2n ] = Value to match
// Operand[2n+1] = BasicBlock to go to on match
SwitchInst(const SwitchInst &RI);
public:
SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0);
SwitchInst(Value *Value, BasicBlock *Default, BasicBlock *InsertAtEnd);
virtual Instruction *clone() const { return new SwitchInst(*this); }
// Accessor Methods for Switch stmt
//
inline const Value *getCondition() const { return Operands[0]; }
inline Value *getCondition() { return Operands[0]; }
inline const BasicBlock *getDefaultDest() const {
return cast<BasicBlock>(Operands[1].get());
}
inline BasicBlock *getDefaultDest() {
return cast<BasicBlock>(Operands[1].get());
}
/// getNumCases - return the number of 'cases' in this switch instruction.
/// Note that case #0 is always the default case.
unsigned getNumCases() const {
return Operands.size()/2;
}
/// getCaseValue - Return the specified case value. Note that case #0, the
/// default destination, does not have a case value.
Constant *getCaseValue(unsigned i) {
assert(i && i < getNumCases() && "Illegal case value to get!");
return getSuccessorValue(i);
}
/// getCaseValue - Return the specified case value. Note that case #0, the
/// default destination, does not have a case value.
const Constant *getCaseValue(unsigned i) const {
assert(i && i < getNumCases() && "Illegal case value to get!");
return getSuccessorValue(i);
}
/// findCaseValue - Search all of the case values for the specified constant.
/// If it is explicitly handled, return the case number of it, otherwise
/// return 0 to indicate that it is handled by the default handler.
unsigned findCaseValue(const Constant *C) const {
for (unsigned i = 1, e = getNumCases(); i != e; ++i)
if (getCaseValue(i) == C)
return i;
return 0;
}
/// addCase - Add an entry to the switch instruction...
///
void addCase(Constant *OnVal, BasicBlock *Dest);
/// removeCase - This method removes the specified successor from the switch
/// instruction. Note that this cannot be used to remove the default
/// destination (successor #0).
///
void removeCase(unsigned idx);
virtual const BasicBlock *getSuccessor(unsigned idx) const {
assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
return cast<BasicBlock>(Operands[idx*2+1].get());
}
inline BasicBlock *getSuccessor(unsigned idx) {
assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
return cast<BasicBlock>(Operands[idx*2+1].get());
}
virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Operands[idx*2+1] = reinterpret_cast<Value*>(NewSucc);
}
// getSuccessorValue - Return the value associated with the specified
// successor.
inline const Constant *getSuccessorValue(unsigned idx) const {
assert(idx < getNumSuccessors() && "Successor # out of range!");
return cast<Constant>(Operands[idx*2].get());
}
inline Constant *getSuccessorValue(unsigned idx) {
assert(idx < getNumSuccessors() && "Successor # out of range!");
return cast<Constant>(Operands[idx*2].get());
}
virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const SwitchInst *) { return true; }
static inline bool classof(const Instruction *I) {
return (I->getOpcode() == Instruction::Switch);
}
static inline bool classof(const Value *V) {
return isa<Instruction>(V) && classof(cast<Instruction>(V));
}
};
//===---------------------------------------------------------------------------
/// InvokeInst - Invoke instruction
///
class InvokeInst : public TerminatorInst {
InvokeInst(const InvokeInst &BI);
public:
InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
const std::vector<Value*> &Params, const std::string &Name = "",
Instruction *InsertBefore = 0);
InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
const std::vector<Value*> &Params, const std::string &Name,
BasicBlock *InsertAtEnd);
virtual Instruction *clone() const { return new InvokeInst(*this); }
bool mayWriteToMemory() const { return true; }
/// getCalledFunction - Return the function called, or null if this is an
/// indirect function invocation...
///
/// FIXME: These should be inlined once we get rid of ConstantPointerRefs!
///
const Function *getCalledFunction() const;
Function *getCalledFunction();
// getCalledValue - Get a pointer to a function that is invoked by this inst.
inline const Value *getCalledValue() const { return Operands[0]; }
inline Value *getCalledValue() { return Operands[0]; }
// get*Dest - Return the destination basic blocks...
inline const BasicBlock *getNormalDest() const {
return cast<BasicBlock>(Operands[1].get());
}
inline BasicBlock *getNormalDest() {
return cast<BasicBlock>(Operands[1].get());
}
inline const BasicBlock *getUnwindDest() const {
return cast<BasicBlock>(Operands[2].get());
}
inline BasicBlock *getUnwindDest() {
return cast<BasicBlock>(Operands[2].get());
}
inline void setNormalDest(BasicBlock *B){
Operands[1] = reinterpret_cast<Value*>(B);
}
inline void setUnwindDest(BasicBlock *B){
Operands[2] = reinterpret_cast<Value*>(B);
}
virtual const BasicBlock *getSuccessor(unsigned i) const {
assert(i < 2 && "Successor # out of range for invoke!");
return i == 0 ? getNormalDest() : getUnwindDest();
}
inline BasicBlock *getSuccessor(unsigned i) {
assert(i < 2 && "Successor # out of range for invoke!");
return i == 0 ? getNormalDest() : getUnwindDest();
}
virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
assert(idx < 2 && "Successor # out of range for invoke!");
Operands[idx+1] = reinterpret_cast<Value*>(NewSucc);
}
virtual unsigned getNumSuccessors() const { return 2; }
// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const InvokeInst *) { return true; }
static inline bool classof(const Instruction *I) {
return (I->getOpcode() == Instruction::Invoke);
}
static inline bool classof(const Value *V) {
return isa<Instruction>(V) && classof(cast<Instruction>(V));
}
};
//===---------------------------------------------------------------------------
/// UnwindInst - Immediately exit the current function, unwinding the stack
/// until an invoke instruction is found.
///
struct UnwindInst : public TerminatorInst {
UnwindInst(Instruction *InsertBefore = 0)
: TerminatorInst(Instruction::Unwind, InsertBefore) {
}
UnwindInst(BasicBlock *InsertAtEnd)
: TerminatorInst(Instruction::Unwind, InsertAtEnd) {
}
virtual Instruction *clone() const { return new UnwindInst(); }
virtual const BasicBlock *getSuccessor(unsigned idx) const {
assert(0 && "UnwindInst has no successors!");
abort();
return 0;
}
virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
virtual unsigned getNumSuccessors() const { return 0; }
// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const UnwindInst *) { return true; }
static inline bool classof(const Instruction *I) {
return I->getOpcode() == Instruction::Unwind;
}
static inline bool classof(const Value *V) {
return isa<Instruction>(V) && classof(cast<Instruction>(V));
}
};
} // End llvm namespace
#endif
|