blob: eb4188b384c3c2b6a699d149fd292877b8de4b6c [file] [log] [blame]
Chris Lattnera892a3a2003-01-27 22:08:52 +00001//===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
John Criswell6fbcc262003-10-20 20:19:47 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattnera892a3a2003-01-27 22:08:52 +00009//
10// This file exposes the class definitions of all of the subclasses of the
11// Instruction class. This is meant to be an easy way to get access to all
12// instruction subclasses.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_INSTRUCTIONS_H
17#define LLVM_INSTRUCTIONS_H
18
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000019#include "llvm/Instruction.h"
20#include "llvm/InstrTypes.h"
21
22namespace llvm {
23
Chris Lattner1fca5ff2004-10-27 16:14:51 +000024class BasicBlock;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000025class PointerType;
26
27//===----------------------------------------------------------------------===//
28// AllocationInst Class
29//===----------------------------------------------------------------------===//
30
31/// AllocationInst - This class is the common base class of MallocInst and
32/// AllocaInst.
33///
Chris Lattner454928e2005-01-29 00:31:36 +000034class AllocationInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000035protected:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000036 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
37 const std::string &Name = "", Instruction *InsertBefore = 0);
38 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
39 const std::string &Name, BasicBlock *InsertAtEnd);
40
41public:
42
43 /// isArrayAllocation - Return true if there is an allocation size parameter
44 /// to the allocation instruction that is not 1.
45 ///
46 bool isArrayAllocation() const;
47
48 /// getArraySize - Get the number of element allocated, for a simple
49 /// allocation of a single element, this will return a constant 1 value.
50 ///
Chris Lattner454928e2005-01-29 00:31:36 +000051 inline const Value *getArraySize() const { return getOperand(0); }
52 inline Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000053
54 /// getType - Overload to return most specific pointer type
55 ///
56 inline const PointerType *getType() const {
57 return reinterpret_cast<const PointerType*>(Instruction::getType());
58 }
59
60 /// getAllocatedType - Return the type that is being allocated by the
61 /// instruction.
62 ///
63 const Type *getAllocatedType() const;
64
65 virtual Instruction *clone() const = 0;
66
67 // Methods for support type inquiry through isa, cast, and dyn_cast:
68 static inline bool classof(const AllocationInst *) { return true; }
69 static inline bool classof(const Instruction *I) {
70 return I->getOpcode() == Instruction::Alloca ||
71 I->getOpcode() == Instruction::Malloc;
72 }
73 static inline bool classof(const Value *V) {
74 return isa<Instruction>(V) && classof(cast<Instruction>(V));
75 }
76};
77
78
79//===----------------------------------------------------------------------===//
80// MallocInst Class
81//===----------------------------------------------------------------------===//
82
83/// MallocInst - an instruction to allocated memory on the heap
84///
85class MallocInst : public AllocationInst {
86 MallocInst(const MallocInst &MI);
87public:
88 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
89 const std::string &Name = "",
90 Instruction *InsertBefore = 0)
91 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
92 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
93 BasicBlock *InsertAtEnd)
94 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertAtEnd) {}
95
Chris Lattnerf319e832004-10-15 23:52:05 +000096 virtual MallocInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000097
98 // Methods for support type inquiry through isa, cast, and dyn_cast:
99 static inline bool classof(const MallocInst *) { return true; }
100 static inline bool classof(const Instruction *I) {
101 return (I->getOpcode() == Instruction::Malloc);
102 }
103 static inline bool classof(const Value *V) {
104 return isa<Instruction>(V) && classof(cast<Instruction>(V));
105 }
106};
107
108
109//===----------------------------------------------------------------------===//
110// AllocaInst Class
111//===----------------------------------------------------------------------===//
112
113/// AllocaInst - an instruction to allocate memory on the stack
114///
115class AllocaInst : public AllocationInst {
116 AllocaInst(const AllocaInst &);
117public:
118 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
119 const std::string &Name = "",
120 Instruction *InsertBefore = 0)
121 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
122 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
123 BasicBlock *InsertAtEnd)
124 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertAtEnd) {}
125
Chris Lattnerf319e832004-10-15 23:52:05 +0000126 virtual AllocaInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000127
128 // Methods for support type inquiry through isa, cast, and dyn_cast:
129 static inline bool classof(const AllocaInst *) { return true; }
130 static inline bool classof(const Instruction *I) {
131 return (I->getOpcode() == Instruction::Alloca);
132 }
133 static inline bool classof(const Value *V) {
134 return isa<Instruction>(V) && classof(cast<Instruction>(V));
135 }
136};
137
138
139//===----------------------------------------------------------------------===//
140// FreeInst Class
141//===----------------------------------------------------------------------===//
142
143/// FreeInst - an instruction to deallocate memory
144///
Chris Lattner454928e2005-01-29 00:31:36 +0000145class FreeInst : public UnaryInstruction {
146 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000147public:
148 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
149 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
150
Chris Lattnerf319e832004-10-15 23:52:05 +0000151 virtual FreeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000152
153 virtual bool mayWriteToMemory() const { return true; }
154
155 // Methods for support type inquiry through isa, cast, and dyn_cast:
156 static inline bool classof(const FreeInst *) { return true; }
157 static inline bool classof(const Instruction *I) {
158 return (I->getOpcode() == Instruction::Free);
159 }
160 static inline bool classof(const Value *V) {
161 return isa<Instruction>(V) && classof(cast<Instruction>(V));
162 }
163};
164
165
166//===----------------------------------------------------------------------===//
167// LoadInst Class
168//===----------------------------------------------------------------------===//
169
Chris Lattner88fe29a2005-02-05 01:44:18 +0000170/// LoadInst - an instruction for reading from memory. This uses the
171/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000172///
Chris Lattner454928e2005-01-29 00:31:36 +0000173class LoadInst : public UnaryInstruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000174 LoadInst(const LoadInst &LI)
Chris Lattner88fe29a2005-02-05 01:44:18 +0000175 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
176 setVolatile(LI.isVolatile());
177
Chris Lattner454928e2005-01-29 00:31:36 +0000178#ifndef NDEBUG
179 AssertOK();
180#endif
181 }
182 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000183public:
184 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
185 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
186 LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
187 Instruction *InsertBefore = 0);
188 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
189 BasicBlock *InsertAtEnd);
190
191 /// isVolatile - Return true if this is a load from a volatile memory
192 /// location.
193 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000194 bool isVolatile() const { return SubclassData; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000195
196 /// setVolatile - Specify whether this is a volatile load or not.
197 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000198 void setVolatile(bool V) { SubclassData = V; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000199
Chris Lattnerf319e832004-10-15 23:52:05 +0000200 virtual LoadInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000201
202 virtual bool mayWriteToMemory() const { return isVolatile(); }
203
204 Value *getPointerOperand() { return getOperand(0); }
205 const Value *getPointerOperand() const { return getOperand(0); }
206 static unsigned getPointerOperandIndex() { return 0U; }
207
208 // Methods for support type inquiry through isa, cast, and dyn_cast:
209 static inline bool classof(const LoadInst *) { return true; }
210 static inline bool classof(const Instruction *I) {
211 return I->getOpcode() == Instruction::Load;
212 }
213 static inline bool classof(const Value *V) {
214 return isa<Instruction>(V) && classof(cast<Instruction>(V));
215 }
216};
217
218
219//===----------------------------------------------------------------------===//
220// StoreInst Class
221//===----------------------------------------------------------------------===//
222
223/// StoreInst - an instruction for storing to memory
224///
225class StoreInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000226 Use Ops[2];
Chris Lattner88fe29a2005-02-05 01:44:18 +0000227 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
Chris Lattner454928e2005-01-29 00:31:36 +0000228 Ops[0].init(SI.Ops[0], this);
229 Ops[1].init(SI.Ops[1], this);
Chris Lattner88fe29a2005-02-05 01:44:18 +0000230 setVolatile(SI.isVolatile());
Chris Lattner454928e2005-01-29 00:31:36 +0000231#ifndef NDEBUG
232 AssertOK();
233#endif
234 }
235 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000236public:
237 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
238 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
239 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
240 Instruction *InsertBefore = 0);
241 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
242
243
244 /// isVolatile - Return true if this is a load from a volatile memory
245 /// location.
246 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000247 bool isVolatile() const { return SubclassData; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000248
249 /// setVolatile - Specify whether this is a volatile load or not.
250 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000251 void setVolatile(bool V) { SubclassData = V; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000252
Chris Lattner454928e2005-01-29 00:31:36 +0000253 /// Transparently provide more efficient getOperand methods.
254 Value *getOperand(unsigned i) const {
255 assert(i < 2 && "getOperand() out of range!");
256 return Ops[i];
257 }
258 void setOperand(unsigned i, Value *Val) {
259 assert(i < 2 && "setOperand() out of range!");
260 Ops[i] = Val;
261 }
262 unsigned getNumOperands() const { return 2; }
263
264
Chris Lattnerf319e832004-10-15 23:52:05 +0000265 virtual StoreInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000266
267 virtual bool mayWriteToMemory() const { return true; }
268
269 Value *getPointerOperand() { return getOperand(1); }
270 const Value *getPointerOperand() const { return getOperand(1); }
271 static unsigned getPointerOperandIndex() { return 1U; }
272
273 // Methods for support type inquiry through isa, cast, and dyn_cast:
274 static inline bool classof(const StoreInst *) { return true; }
275 static inline bool classof(const Instruction *I) {
276 return I->getOpcode() == Instruction::Store;
277 }
278 static inline bool classof(const Value *V) {
279 return isa<Instruction>(V) && classof(cast<Instruction>(V));
280 }
281};
282
283
284//===----------------------------------------------------------------------===//
285// GetElementPtrInst Class
286//===----------------------------------------------------------------------===//
287
288/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
289/// access elements of arrays and structs
290///
291class GetElementPtrInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000292 GetElementPtrInst(const GetElementPtrInst &GEPI)
293 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
294 0, GEPI.getNumOperands()) {
295 Use *OL = OperandList = new Use[NumOperands];
296 Use *GEPIOL = GEPI.OperandList;
297 for (unsigned i = 0, E = NumOperands; i != E; ++i)
298 OL[i].init(GEPIOL[i], this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000299 }
300 void init(Value *Ptr, const std::vector<Value*> &Idx);
301 void init(Value *Ptr, Value *Idx0, Value *Idx1);
302public:
303 /// Constructors - Create a getelementptr instruction with a base pointer an
304 /// list of indices. The first ctor can optionally insert before an existing
305 /// instruction, the second appends the new instruction to the specified
306 /// BasicBlock.
307 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
308 const std::string &Name = "", Instruction *InsertBefore =0);
309 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
310 const std::string &Name, BasicBlock *InsertAtEnd);
311
312 /// Constructors - These two constructors are convenience methods because two
313 /// index getelementptr instructions are so common.
314 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
315 const std::string &Name = "", Instruction *InsertBefore =0);
316 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
317 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattner454928e2005-01-29 00:31:36 +0000318 ~GetElementPtrInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000319
Chris Lattnerf319e832004-10-15 23:52:05 +0000320 virtual GetElementPtrInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000321
322 // getType - Overload to return most specific pointer type...
323 inline const PointerType *getType() const {
324 return reinterpret_cast<const PointerType*>(Instruction::getType());
325 }
326
327 /// getIndexedType - Returns the type of the element that would be loaded with
328 /// a load instruction with the specified parameters.
329 ///
330 /// A null type is returned if the indices are invalid for the specified
331 /// pointer type.
332 ///
333 static const Type *getIndexedType(const Type *Ptr,
334 const std::vector<Value*> &Indices,
335 bool AllowStructLeaf = false);
336 static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
337 bool AllowStructLeaf = false);
338
339 inline op_iterator idx_begin() { return op_begin()+1; }
340 inline const_op_iterator idx_begin() const { return op_begin()+1; }
341 inline op_iterator idx_end() { return op_end(); }
342 inline const_op_iterator idx_end() const { return op_end(); }
343
344 Value *getPointerOperand() {
345 return getOperand(0);
346 }
347 const Value *getPointerOperand() const {
348 return getOperand(0);
349 }
350 static unsigned getPointerOperandIndex() {
351 return 0U; // get index for modifying correct operand
352 }
353
354 inline unsigned getNumIndices() const { // Note: always non-negative
355 return getNumOperands() - 1;
356 }
357
358 inline bool hasIndices() const {
359 return getNumOperands() > 1;
360 }
361
362 // Methods for support type inquiry through isa, cast, and dyn_cast:
363 static inline bool classof(const GetElementPtrInst *) { return true; }
364 static inline bool classof(const Instruction *I) {
365 return (I->getOpcode() == Instruction::GetElementPtr);
366 }
367 static inline bool classof(const Value *V) {
368 return isa<Instruction>(V) && classof(cast<Instruction>(V));
369 }
370};
371
372//===----------------------------------------------------------------------===//
373// SetCondInst Class
374//===----------------------------------------------------------------------===//
375
376/// SetCondInst class - Represent a setCC operator, where CC is eq, ne, lt, gt,
377/// le, or ge.
378///
379class SetCondInst : public BinaryOperator {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000380public:
381 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
382 const std::string &Name = "", Instruction *InsertBefore = 0);
383 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
384 const std::string &Name, BasicBlock *InsertAtEnd);
385
386 /// getInverseCondition - Return the inverse of the current condition opcode.
387 /// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
388 ///
389 BinaryOps getInverseCondition() const {
390 return getInverseCondition(getOpcode());
391 }
392
393 /// getInverseCondition - Static version that you can use without an
394 /// instruction available.
395 ///
396 static BinaryOps getInverseCondition(BinaryOps Opcode);
397
398 /// getSwappedCondition - Return the condition opcode that would be the result
399 /// of exchanging the two operands of the setcc instruction without changing
400 /// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
401 ///
402 BinaryOps getSwappedCondition() const {
403 return getSwappedCondition(getOpcode());
404 }
405
406 /// getSwappedCondition - Static version that you can use without an
407 /// instruction available.
408 ///
409 static BinaryOps getSwappedCondition(BinaryOps Opcode);
410
411
412 // Methods for support type inquiry through isa, cast, and dyn_cast:
413 static inline bool classof(const SetCondInst *) { return true; }
414 static inline bool classof(const Instruction *I) {
415 return I->getOpcode() == SetEQ || I->getOpcode() == SetNE ||
416 I->getOpcode() == SetLE || I->getOpcode() == SetGE ||
417 I->getOpcode() == SetLT || I->getOpcode() == SetGT;
418 }
419 static inline bool classof(const Value *V) {
420 return isa<Instruction>(V) && classof(cast<Instruction>(V));
421 }
422};
423
424//===----------------------------------------------------------------------===//
425// CastInst Class
426//===----------------------------------------------------------------------===//
427
428/// CastInst - This class represents a cast from Operand[0] to the type of
429/// the instruction (i->getType()).
430///
Chris Lattner454928e2005-01-29 00:31:36 +0000431class CastInst : public UnaryInstruction {
432 CastInst(const CastInst &CI)
433 : UnaryInstruction(CI.getType(), Cast, CI.getOperand(0)) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000434 }
435public:
436 CastInst(Value *S, const Type *Ty, const std::string &Name = "",
437 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000438 : UnaryInstruction(Ty, Cast, S, Name, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000439 }
440 CastInst(Value *S, const Type *Ty, const std::string &Name,
441 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000442 : UnaryInstruction(Ty, Cast, S, Name, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000443 }
444
Chris Lattnerf319e832004-10-15 23:52:05 +0000445 virtual CastInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000446
447 // Methods for support type inquiry through isa, cast, and dyn_cast:
448 static inline bool classof(const CastInst *) { return true; }
449 static inline bool classof(const Instruction *I) {
450 return I->getOpcode() == Cast;
451 }
452 static inline bool classof(const Value *V) {
453 return isa<Instruction>(V) && classof(cast<Instruction>(V));
454 }
455};
456
457
458//===----------------------------------------------------------------------===//
459// CallInst Class
460//===----------------------------------------------------------------------===//
461
462/// CallInst - This class represents a function call, abstracting a target
463/// machine's calling convention.
464///
465class CallInst : public Instruction {
466 CallInst(const CallInst &CI);
467 void init(Value *Func, const std::vector<Value*> &Params);
468 void init(Value *Func, Value *Actual1, Value *Actual2);
469 void init(Value *Func, Value *Actual);
470 void init(Value *Func);
471
472public:
473 CallInst(Value *F, const std::vector<Value*> &Par,
474 const std::string &Name = "", Instruction *InsertBefore = 0);
475 CallInst(Value *F, const std::vector<Value*> &Par,
476 const std::string &Name, BasicBlock *InsertAtEnd);
477
478 // Alternate CallInst ctors w/ two actuals, w/ one actual and no
479 // actuals, respectively.
480 CallInst(Value *F, Value *Actual1, Value *Actual2,
481 const std::string& Name = "", Instruction *InsertBefore = 0);
482 CallInst(Value *F, Value *Actual1, Value *Actual2,
483 const std::string& Name, BasicBlock *InsertAtEnd);
484 CallInst(Value *F, Value *Actual, const std::string& Name = "",
485 Instruction *InsertBefore = 0);
486 CallInst(Value *F, Value *Actual, const std::string& Name,
487 BasicBlock *InsertAtEnd);
488 explicit CallInst(Value *F, const std::string &Name = "",
489 Instruction *InsertBefore = 0);
490 explicit CallInst(Value *F, const std::string &Name,
491 BasicBlock *InsertAtEnd);
Chris Lattner454928e2005-01-29 00:31:36 +0000492 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000493
Chris Lattnerf319e832004-10-15 23:52:05 +0000494 virtual CallInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000495 bool mayWriteToMemory() const { return true; }
496
Chris Lattner721aef62004-11-18 17:46:57 +0000497 /// getCalledFunction - Return the function being called by this instruction
498 /// if it is a direct call. If it is a call through a function pointer,
499 /// return null.
500 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +0000501 return (Function*)dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +0000502 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000503
504 // getCalledValue - Get a pointer to a method that is invoked by this inst.
Chris Lattner454928e2005-01-29 00:31:36 +0000505 inline const Value *getCalledValue() const { return getOperand(0); }
506 inline Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000507
508 // Methods for support type inquiry through isa, cast, and dyn_cast:
509 static inline bool classof(const CallInst *) { return true; }
510 static inline bool classof(const Instruction *I) {
511 return I->getOpcode() == Instruction::Call;
512 }
513 static inline bool classof(const Value *V) {
514 return isa<Instruction>(V) && classof(cast<Instruction>(V));
515 }
516};
517
518
519//===----------------------------------------------------------------------===//
520// ShiftInst Class
521//===----------------------------------------------------------------------===//
522
523/// ShiftInst - This class represents left and right shift instructions.
524///
525class ShiftInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000526 Use Ops[2];
527 ShiftInst(const ShiftInst &SI)
528 : Instruction(SI.getType(), SI.getOpcode(), Ops, 2) {
529 Ops[0].init(SI.Ops[0], this);
530 Ops[1].init(SI.Ops[1], this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000531 }
532 void init(OtherOps Opcode, Value *S, Value *SA) {
533 assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
Chris Lattner454928e2005-01-29 00:31:36 +0000534 Ops[0].init(S, this);
535 Ops[1].init(SA, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000536 }
537
538public:
539 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
540 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000541 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000542 init(Opcode, S, SA);
543 }
544 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name,
545 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000546 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000547 init(Opcode, S, SA);
548 }
549
550 OtherOps getOpcode() const {
551 return static_cast<OtherOps>(Instruction::getOpcode());
552 }
553
Chris Lattner454928e2005-01-29 00:31:36 +0000554 /// Transparently provide more efficient getOperand methods.
555 Value *getOperand(unsigned i) const {
556 assert(i < 2 && "getOperand() out of range!");
557 return Ops[i];
558 }
559 void setOperand(unsigned i, Value *Val) {
560 assert(i < 2 && "setOperand() out of range!");
561 Ops[i] = Val;
562 }
563 unsigned getNumOperands() const { return 2; }
564
Chris Lattnerf319e832004-10-15 23:52:05 +0000565 virtual ShiftInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000566
567 // Methods for support type inquiry through isa, cast, and dyn_cast:
568 static inline bool classof(const ShiftInst *) { return true; }
569 static inline bool classof(const Instruction *I) {
570 return (I->getOpcode() == Instruction::Shr) |
571 (I->getOpcode() == Instruction::Shl);
572 }
573 static inline bool classof(const Value *V) {
574 return isa<Instruction>(V) && classof(cast<Instruction>(V));
575 }
576};
577
578//===----------------------------------------------------------------------===//
579// SelectInst Class
580//===----------------------------------------------------------------------===//
581
582/// SelectInst - This class represents the LLVM 'select' instruction.
583///
584class SelectInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000585 Use Ops[3];
586
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000587 void init(Value *C, Value *S1, Value *S2) {
Chris Lattner454928e2005-01-29 00:31:36 +0000588 Ops[0].init(C, this);
589 Ops[1].init(S1, this);
590 Ops[2].init(S2, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000591 }
592
Chris Lattner454928e2005-01-29 00:31:36 +0000593 SelectInst(const SelectInst &SI)
594 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
595 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
596 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000597public:
598 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
599 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000600 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
601 Name, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000602 init(C, S1, S2);
603 }
604 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
605 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000606 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
607 Name, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000608 init(C, S1, S2);
609 }
610
Chris Lattner454928e2005-01-29 00:31:36 +0000611 Value *getCondition() const { return Ops[0]; }
612 Value *getTrueValue() const { return Ops[1]; }
613 Value *getFalseValue() const { return Ops[2]; }
614
615 /// Transparently provide more efficient getOperand methods.
616 Value *getOperand(unsigned i) const {
617 assert(i < 3 && "getOperand() out of range!");
618 return Ops[i];
619 }
620 void setOperand(unsigned i, Value *Val) {
621 assert(i < 3 && "setOperand() out of range!");
622 Ops[i] = Val;
623 }
624 unsigned getNumOperands() const { return 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000625
626 OtherOps getOpcode() const {
627 return static_cast<OtherOps>(Instruction::getOpcode());
628 }
629
Chris Lattnerf319e832004-10-15 23:52:05 +0000630 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000631
632 // Methods for support type inquiry through isa, cast, and dyn_cast:
633 static inline bool classof(const SelectInst *) { return true; }
634 static inline bool classof(const Instruction *I) {
635 return I->getOpcode() == Instruction::Select;
636 }
637 static inline bool classof(const Value *V) {
638 return isa<Instruction>(V) && classof(cast<Instruction>(V));
639 }
640};
641
642
643//===----------------------------------------------------------------------===//
644// VANextInst Class
645//===----------------------------------------------------------------------===//
646
647/// VANextInst - This class represents the va_next llvm instruction, which
648/// advances a vararg list passed an argument of the specified type, returning
649/// the resultant list.
650///
Chris Lattner454928e2005-01-29 00:31:36 +0000651class VANextInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000652 PATypeHolder ArgTy;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000653 VANextInst(const VANextInst &VAN)
Chris Lattner454928e2005-01-29 00:31:36 +0000654 : UnaryInstruction(VAN.getType(), VANext, VAN.getOperand(0)),
655 ArgTy(VAN.getArgType()) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000656 }
657
658public:
659 VANextInst(Value *List, const Type *Ty, const std::string &Name = "",
660 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000661 : UnaryInstruction(List->getType(), VANext, List, Name, InsertBefore),
662 ArgTy(Ty) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000663 }
664 VANextInst(Value *List, const Type *Ty, const std::string &Name,
665 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000666 : UnaryInstruction(List->getType(), VANext, List, Name, InsertAtEnd),
667 ArgTy(Ty) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000668 }
669
670 const Type *getArgType() const { return ArgTy; }
671
Chris Lattnerf319e832004-10-15 23:52:05 +0000672 virtual VANextInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000673
674 // Methods for support type inquiry through isa, cast, and dyn_cast:
675 static inline bool classof(const VANextInst *) { return true; }
676 static inline bool classof(const Instruction *I) {
677 return I->getOpcode() == VANext;
678 }
679 static inline bool classof(const Value *V) {
680 return isa<Instruction>(V) && classof(cast<Instruction>(V));
681 }
682};
683
684
685//===----------------------------------------------------------------------===//
686// VAArgInst Class
687//===----------------------------------------------------------------------===//
688
689/// VAArgInst - This class represents the va_arg llvm instruction, which returns
690/// an argument of the specified type given a va_list.
691///
Chris Lattner454928e2005-01-29 00:31:36 +0000692class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000693 VAArgInst(const VAArgInst &VAA)
Chris Lattner454928e2005-01-29 00:31:36 +0000694 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000695public:
696 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
697 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000698 : UnaryInstruction(Ty, VAArg, List, Name, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000699 }
700 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
701 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000702 : UnaryInstruction(Ty, VAArg, List, Name, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000703 }
704
Chris Lattnerf319e832004-10-15 23:52:05 +0000705 virtual VAArgInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000706
707 // Methods for support type inquiry through isa, cast, and dyn_cast:
708 static inline bool classof(const VAArgInst *) { return true; }
709 static inline bool classof(const Instruction *I) {
710 return I->getOpcode() == VAArg;
711 }
712 static inline bool classof(const Value *V) {
713 return isa<Instruction>(V) && classof(cast<Instruction>(V));
714 }
715};
716
717//===----------------------------------------------------------------------===//
718// PHINode Class
719//===----------------------------------------------------------------------===//
720
721// PHINode - The PHINode class is used to represent the magical mystical PHI
722// node, that can not exist in nature, but can be synthesized in a computer
723// scientist's overactive imagination.
724//
725class PHINode : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000726 /// ReservedSpace - The number of operands actually allocated. NumOperands is
727 /// the number actually in use.
728 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000729 PHINode(const PHINode &PN);
730public:
731 PHINode(const Type *Ty, const std::string &Name = "",
732 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000733 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertBefore),
734 ReservedSpace(0) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000735 }
736
737 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000738 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertAtEnd),
739 ReservedSpace(0) {
740 }
741
742 ~PHINode();
743
744 /// reserveOperandSpace - This method can be used to avoid repeated
745 /// reallocation of PHI operand lists by reserving space for the correct
746 /// number of operands before adding them. Unlike normal vector reserves,
747 /// this method can also be used to trim the operand space.
748 void reserveOperandSpace(unsigned NumValues) {
749 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000750 }
751
Chris Lattnerf319e832004-10-15 23:52:05 +0000752 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000753
754 /// getNumIncomingValues - Return the number of incoming edges
755 ///
Chris Lattner454928e2005-01-29 00:31:36 +0000756 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000757
758 /// getIncomingValue - Return incoming value #x
759 ///
760 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +0000761 assert(i*2 < getNumOperands() && "Invalid value number!");
762 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000763 }
764 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +0000765 assert(i*2 < getNumOperands() && "Invalid value number!");
766 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000767 }
Chris Lattner454928e2005-01-29 00:31:36 +0000768 unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000769 return i*2;
770 }
771
772 /// getIncomingBlock - Return incoming basic block #x
773 ///
774 BasicBlock *getIncomingBlock(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +0000775 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000776 }
777 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Chris Lattner454928e2005-01-29 00:31:36 +0000778 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000779 }
780 unsigned getOperandNumForIncomingBlock(unsigned i) {
781 return i*2+1;
782 }
783
784 /// addIncoming - Add an incoming value to the end of the PHI list
785 ///
786 void addIncoming(Value *V, BasicBlock *BB) {
787 assert(getType() == V->getType() &&
788 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +0000789 unsigned OpNo = NumOperands;
790 if (OpNo+2 > ReservedSpace)
791 resizeOperands(0); // Get more space!
792 // Initialize some new operands.
793 NumOperands = OpNo+2;
794 OperandList[OpNo].init(V, this);
795 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000796 }
797
798 /// removeIncomingValue - Remove an incoming value. This is useful if a
799 /// predecessor basic block is deleted. The value removed is returned.
800 ///
801 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
802 /// is true), the PHI node is destroyed and any uses of it are replaced with
803 /// dummy values. The only time there should be zero incoming values to a PHI
804 /// node is when the block is dead, so this strategy is sound.
805 ///
806 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
807
808 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
809 int Idx = getBasicBlockIndex(BB);
810 assert(Idx >= 0 && "Invalid basic block argument to remove!");
811 return removeIncomingValue(Idx, DeletePHIIfEmpty);
812 }
813
814 /// getBasicBlockIndex - Return the first index of the specified basic
815 /// block in the value list for this PHI. Returns -1 if no instance.
816 ///
817 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +0000818 Use *OL = OperandList;
819 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
820 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000821 return -1;
822 }
823
824 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
825 return getIncomingValue(getBasicBlockIndex(BB));
826 }
827
828 /// Methods for support type inquiry through isa, cast, and dyn_cast:
829 static inline bool classof(const PHINode *) { return true; }
830 static inline bool classof(const Instruction *I) {
831 return I->getOpcode() == Instruction::PHI;
832 }
833 static inline bool classof(const Value *V) {
834 return isa<Instruction>(V) && classof(cast<Instruction>(V));
835 }
Chris Lattner454928e2005-01-29 00:31:36 +0000836 private:
837 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000838};
839
840//===----------------------------------------------------------------------===//
841// ReturnInst Class
842//===----------------------------------------------------------------------===//
843
844//===---------------------------------------------------------------------------
845/// ReturnInst - Return a value (possibly void), from a function. Execution
846/// does not continue in this function any longer.
847///
848class ReturnInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +0000849 Use RetVal; // Possibly null retval.
850 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret, &RetVal,
851 RI.getNumOperands()) {
852 if (RI.getNumOperands())
853 RetVal.init(RI.RetVal, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000854 }
855
Alkis Evlogimenos859804f2004-11-17 21:02:25 +0000856 void init(Value *RetVal);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000857
858public:
859 // ReturnInst constructors:
860 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +0000861 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000862 // ReturnInst(Value* X) - 'ret X' instruction
863 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
864 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
865 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
866 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
Alkis Evlogimenos859804f2004-11-17 21:02:25 +0000867 //
868 // NOTE: If the Value* passed is of type void then the constructor behaves as
869 // if it was passed NULL.
Chris Lattner454928e2005-01-29 00:31:36 +0000870 ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0)
871 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertBefore) {
872 init(retVal);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000873 }
Chris Lattner454928e2005-01-29 00:31:36 +0000874 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
875 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
876 init(retVal);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000877 }
878 ReturnInst(BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000879 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000880 }
881
Chris Lattnerf319e832004-10-15 23:52:05 +0000882 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000883
Chris Lattner454928e2005-01-29 00:31:36 +0000884 // Transparently provide more efficient getOperand methods.
885 Value *getOperand(unsigned i) const {
886 assert(i < getNumOperands() && "getOperand() out of range!");
887 return RetVal;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000888 }
Chris Lattner454928e2005-01-29 00:31:36 +0000889 void setOperand(unsigned i, Value *Val) {
890 assert(i < getNumOperands() && "setOperand() out of range!");
891 RetVal = Val;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000892 }
893
Chris Lattner454928e2005-01-29 00:31:36 +0000894 Value *getReturnValue() const { return RetVal; }
895
896 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000897
898 // Methods for support type inquiry through isa, cast, and dyn_cast:
899 static inline bool classof(const ReturnInst *) { return true; }
900 static inline bool classof(const Instruction *I) {
901 return (I->getOpcode() == Instruction::Ret);
902 }
903 static inline bool classof(const Value *V) {
904 return isa<Instruction>(V) && classof(cast<Instruction>(V));
905 }
Chris Lattner454928e2005-01-29 00:31:36 +0000906 private:
907 virtual BasicBlock *getSuccessorV(unsigned idx) const;
908 virtual unsigned getNumSuccessorsV() const;
909 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000910};
911
912//===----------------------------------------------------------------------===//
913// BranchInst Class
914//===----------------------------------------------------------------------===//
915
916//===---------------------------------------------------------------------------
917/// BranchInst - Conditional or Unconditional Branch instruction.
918///
919class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +0000920 /// Ops list - Branches are strange. The operands are ordered:
921 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
922 /// they don't have to check for cond/uncond branchness.
923 Use Ops[3];
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000924 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +0000925 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000926public:
927 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
928 // BranchInst(BB *B) - 'br B'
929 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
930 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
931 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
932 // BranchInst(BB* B, BB *I) - 'br B' insert at end
933 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
934 BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000935 : TerminatorInst(Instruction::Br, Ops, 1, InsertBefore) {
936 assert(IfTrue != 0 && "Branch destination may not be null!");
937 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000938 }
939 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
940 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000941 : TerminatorInst(Instruction::Br, Ops, 3, InsertBefore) {
942 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
943 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
944 Ops[2].init(Cond, this);
945#ifndef NDEBUG
946 AssertOK();
947#endif
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000948 }
949
950 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000951 : TerminatorInst(Instruction::Br, Ops, 1, InsertAtEnd) {
952 assert(IfTrue != 0 && "Branch destination may not be null!");
953 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000954 }
955
956 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
957 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000958 : TerminatorInst(Instruction::Br, Ops, 3, InsertAtEnd) {
959 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
960 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
961 Ops[2].init(Cond, this);
962#ifndef NDEBUG
963 AssertOK();
964#endif
965 }
966
967
968 /// Transparently provide more efficient getOperand methods.
969 Value *getOperand(unsigned i) const {
970 assert(i < getNumOperands() && "getOperand() out of range!");
971 return Ops[i];
972 }
973 void setOperand(unsigned i, Value *Val) {
974 assert(i < getNumOperands() && "setOperand() out of range!");
975 Ops[i] = Val;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000976 }
977
Chris Lattnerf319e832004-10-15 23:52:05 +0000978 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000979
Chris Lattner454928e2005-01-29 00:31:36 +0000980 inline bool isUnconditional() const { return getNumOperands() == 1; }
981 inline bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000982
983 inline Value *getCondition() const {
984 assert(isConditional() && "Cannot get condition of an uncond branch!");
Chris Lattner454928e2005-01-29 00:31:36 +0000985 return getOperand(2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000986 }
987
988 void setCondition(Value *V) {
989 assert(isConditional() && "Cannot set condition of unconditional branch!");
990 setOperand(2, V);
991 }
992
993 // setUnconditionalDest - Change the current branch to an unconditional branch
994 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +0000995 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000996 void setUnconditionalDest(BasicBlock *Dest) {
Chris Lattner454928e2005-01-29 00:31:36 +0000997 if (isConditional()) { // Convert this to an uncond branch.
998 NumOperands = 1;
999 Ops[1].set(0);
1000 Ops[2].set(0);
1001 }
1002 setOperand(0, reinterpret_cast<Value*>(Dest));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001003 }
1004
Chris Lattner454928e2005-01-29 00:31:36 +00001005 unsigned getNumSuccessors() const { return 1+isConditional(); }
1006
1007 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001008 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001009 return (i == 0) ? cast<BasicBlock>(getOperand(0)) :
1010 cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001011 }
1012
Chris Lattner454928e2005-01-29 00:31:36 +00001013 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001014 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001015 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001016 }
1017
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001018 // Methods for support type inquiry through isa, cast, and dyn_cast:
1019 static inline bool classof(const BranchInst *) { return true; }
1020 static inline bool classof(const Instruction *I) {
1021 return (I->getOpcode() == Instruction::Br);
1022 }
1023 static inline bool classof(const Value *V) {
1024 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1025 }
Chris Lattner454928e2005-01-29 00:31:36 +00001026private:
1027 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1028 virtual unsigned getNumSuccessorsV() const;
1029 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001030};
1031
1032//===----------------------------------------------------------------------===//
1033// SwitchInst Class
1034//===----------------------------------------------------------------------===//
1035
1036//===---------------------------------------------------------------------------
1037/// SwitchInst - Multiway switch
1038///
1039class SwitchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001040 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001041 // Operand[0] = Value to switch on
1042 // Operand[1] = Default basic block destination
1043 // Operand[2n ] = Value to match
1044 // Operand[2n+1] = BasicBlock to go to on match
1045 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00001046 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1047 void resizeOperands(unsigned No);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001048public:
Chris Lattner454928e2005-01-29 00:31:36 +00001049 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1050 /// switch on and a default destination. The number of additional cases can
1051 /// be specified here to make memory allocation more efficient. This
1052 /// constructor can also autoinsert before another instruction.
1053 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1054 Instruction *InsertBefore = 0)
1055 : TerminatorInst(Instruction::Switch, 0, 0, InsertBefore) {
1056 init(Value, Default, NumCases);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001057 }
1058
Chris Lattner454928e2005-01-29 00:31:36 +00001059 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1060 /// switch on and a default destination. The number of additional cases can
1061 /// be specified here to make memory allocation more efficient. This
1062 /// constructor also autoinserts at the end of the specified BasicBlock.
1063 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1064 BasicBlock *InsertAtEnd)
1065 : TerminatorInst(Instruction::Switch, 0, 0, InsertAtEnd) {
1066 init(Value, Default, NumCases);
1067 }
1068 ~SwitchInst();
1069
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001070
1071 // Accessor Methods for Switch stmt
Chris Lattner454928e2005-01-29 00:31:36 +00001072 inline Value *getCondition() const { return getOperand(0); }
1073 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00001074
Chris Lattner454928e2005-01-29 00:31:36 +00001075 inline BasicBlock *getDefaultDest() const {
1076 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001077 }
1078
1079 /// getNumCases - return the number of 'cases' in this switch instruction.
1080 /// Note that case #0 is always the default case.
1081 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001082 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001083 }
1084
1085 /// getCaseValue - Return the specified case value. Note that case #0, the
1086 /// default destination, does not have a case value.
1087 Constant *getCaseValue(unsigned i) {
1088 assert(i && i < getNumCases() && "Illegal case value to get!");
1089 return getSuccessorValue(i);
1090 }
1091
1092 /// getCaseValue - Return the specified case value. Note that case #0, the
1093 /// default destination, does not have a case value.
1094 const Constant *getCaseValue(unsigned i) const {
1095 assert(i && i < getNumCases() && "Illegal case value to get!");
1096 return getSuccessorValue(i);
1097 }
1098
1099 /// findCaseValue - Search all of the case values for the specified constant.
1100 /// If it is explicitly handled, return the case number of it, otherwise
1101 /// return 0 to indicate that it is handled by the default handler.
1102 unsigned findCaseValue(const Constant *C) const {
1103 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1104 if (getCaseValue(i) == C)
1105 return i;
1106 return 0;
1107 }
1108
1109 /// addCase - Add an entry to the switch instruction...
1110 ///
1111 void addCase(Constant *OnVal, BasicBlock *Dest);
1112
1113 /// removeCase - This method removes the specified successor from the switch
1114 /// instruction. Note that this cannot be used to remove the default
1115 /// destination (successor #0).
1116 ///
1117 void removeCase(unsigned idx);
1118
Chris Lattner454928e2005-01-29 00:31:36 +00001119 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001120
Chris Lattner454928e2005-01-29 00:31:36 +00001121 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1122 BasicBlock *getSuccessor(unsigned idx) const {
1123 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1124 return cast<BasicBlock>(getOperand(idx*2+1));
1125 }
1126 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001127 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001128 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001129 }
1130
1131 // getSuccessorValue - Return the value associated with the specified
1132 // successor.
Chris Lattner454928e2005-01-29 00:31:36 +00001133 inline Constant *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001134 assert(idx < getNumSuccessors() && "Successor # out of range!");
Chris Lattner454928e2005-01-29 00:31:36 +00001135 return cast<Constant>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001136 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001137
1138 // Methods for support type inquiry through isa, cast, and dyn_cast:
1139 static inline bool classof(const SwitchInst *) { return true; }
1140 static inline bool classof(const Instruction *I) {
1141 return (I->getOpcode() == Instruction::Switch);
1142 }
1143 static inline bool classof(const Value *V) {
1144 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1145 }
Chris Lattner454928e2005-01-29 00:31:36 +00001146private:
1147 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1148 virtual unsigned getNumSuccessorsV() const;
1149 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001150};
1151
1152//===----------------------------------------------------------------------===//
1153// InvokeInst Class
1154//===----------------------------------------------------------------------===//
1155
1156//===---------------------------------------------------------------------------
1157/// InvokeInst - Invoke instruction
1158///
1159class InvokeInst : public TerminatorInst {
1160 InvokeInst(const InvokeInst &BI);
1161 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1162 const std::vector<Value*> &Params);
1163public:
1164 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1165 const std::vector<Value*> &Params, const std::string &Name = "",
1166 Instruction *InsertBefore = 0);
1167 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1168 const std::vector<Value*> &Params, const std::string &Name,
1169 BasicBlock *InsertAtEnd);
Chris Lattner454928e2005-01-29 00:31:36 +00001170 ~InvokeInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001171
Chris Lattnerf319e832004-10-15 23:52:05 +00001172 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001173
1174 bool mayWriteToMemory() const { return true; }
1175
1176 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00001177 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001178 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001179 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001180 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001181 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001182
1183 // getCalledValue - Get a pointer to a function that is invoked by this inst.
Chris Lattner454928e2005-01-29 00:31:36 +00001184 inline Value *getCalledValue() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001185
1186 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00001187 BasicBlock *getNormalDest() const {
1188 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001189 }
Chris Lattner454928e2005-01-29 00:31:36 +00001190 BasicBlock *getUnwindDest() const {
1191 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001192 }
Chris Lattner454928e2005-01-29 00:31:36 +00001193 void setNormalDest(BasicBlock *B) {
1194 setOperand(1, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001195 }
1196
Chris Lattner454928e2005-01-29 00:31:36 +00001197 void setUnwindDest(BasicBlock *B) {
1198 setOperand(2, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001199 }
1200
Chris Lattner454928e2005-01-29 00:31:36 +00001201 inline BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001202 assert(i < 2 && "Successor # out of range for invoke!");
1203 return i == 0 ? getNormalDest() : getUnwindDest();
1204 }
1205
Chris Lattner454928e2005-01-29 00:31:36 +00001206 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001207 assert(idx < 2 && "Successor # out of range for invoke!");
Chris Lattner454928e2005-01-29 00:31:36 +00001208 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001209 }
1210
Chris Lattner454928e2005-01-29 00:31:36 +00001211 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001212
1213 // Methods for support type inquiry through isa, cast, and dyn_cast:
1214 static inline bool classof(const InvokeInst *) { return true; }
1215 static inline bool classof(const Instruction *I) {
1216 return (I->getOpcode() == Instruction::Invoke);
1217 }
1218 static inline bool classof(const Value *V) {
1219 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1220 }
Chris Lattner454928e2005-01-29 00:31:36 +00001221private:
1222 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1223 virtual unsigned getNumSuccessorsV() const;
1224 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001225};
1226
1227
1228//===----------------------------------------------------------------------===//
1229// UnwindInst Class
1230//===----------------------------------------------------------------------===//
1231
1232//===---------------------------------------------------------------------------
1233/// UnwindInst - Immediately exit the current function, unwinding the stack
1234/// until an invoke instruction is found.
1235///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00001236class UnwindInst : public TerminatorInst {
1237public:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001238 UnwindInst(Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001239 : TerminatorInst(Instruction::Unwind, 0, 0, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001240 }
1241 UnwindInst(BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001242 : TerminatorInst(Instruction::Unwind, 0, 0, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001243 }
1244
Chris Lattnerf319e832004-10-15 23:52:05 +00001245 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001246
Chris Lattner454928e2005-01-29 00:31:36 +00001247 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001248
1249 // Methods for support type inquiry through isa, cast, and dyn_cast:
1250 static inline bool classof(const UnwindInst *) { return true; }
1251 static inline bool classof(const Instruction *I) {
1252 return I->getOpcode() == Instruction::Unwind;
1253 }
1254 static inline bool classof(const Value *V) {
1255 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1256 }
Chris Lattner454928e2005-01-29 00:31:36 +00001257private:
1258 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1259 virtual unsigned getNumSuccessorsV() const;
1260 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001261};
1262
Chris Lattner076b3f12004-10-16 18:05:54 +00001263//===----------------------------------------------------------------------===//
1264// UnreachableInst Class
1265//===----------------------------------------------------------------------===//
1266
1267//===---------------------------------------------------------------------------
1268/// UnreachableInst - This function has undefined behavior. In particular, the
1269/// presence of this instruction indicates some higher level knowledge that the
1270/// end of the block cannot be reached.
1271///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00001272class UnreachableInst : public TerminatorInst {
1273public:
Chris Lattner076b3f12004-10-16 18:05:54 +00001274 UnreachableInst(Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001275 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertBefore) {
Chris Lattner076b3f12004-10-16 18:05:54 +00001276 }
1277 UnreachableInst(BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001278 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertAtEnd) {
Chris Lattner076b3f12004-10-16 18:05:54 +00001279 }
1280
1281 virtual UnreachableInst *clone() const;
1282
Chris Lattner454928e2005-01-29 00:31:36 +00001283 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00001284
1285 // Methods for support type inquiry through isa, cast, and dyn_cast:
1286 static inline bool classof(const UnreachableInst *) { return true; }
1287 static inline bool classof(const Instruction *I) {
1288 return I->getOpcode() == Instruction::Unreachable;
1289 }
1290 static inline bool classof(const Value *V) {
1291 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1292 }
Chris Lattner454928e2005-01-29 00:31:36 +00001293private:
1294 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1295 virtual unsigned getNumSuccessorsV() const;
1296 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00001297};
1298
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001299} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00001300
1301#endif