blob: 0eaba874d6c881fdaa54795af73c5b4e3b71c980 [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;
Chris Lattnerd1a32602005-02-24 05:32:09 +000025class ConstantInt;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000026class PointerType;
27
28//===----------------------------------------------------------------------===//
29// AllocationInst Class
30//===----------------------------------------------------------------------===//
31
32/// AllocationInst - This class is the common base class of MallocInst and
33/// AllocaInst.
34///
Chris Lattner454928e2005-01-29 00:31:36 +000035class AllocationInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000036protected:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000037 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
38 const std::string &Name = "", Instruction *InsertBefore = 0);
39 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
40 const std::string &Name, BasicBlock *InsertAtEnd);
41
42public:
43
44 /// isArrayAllocation - Return true if there is an allocation size parameter
45 /// to the allocation instruction that is not 1.
46 ///
47 bool isArrayAllocation() const;
48
49 /// getArraySize - Get the number of element allocated, for a simple
50 /// allocation of a single element, this will return a constant 1 value.
51 ///
Chris Lattner454928e2005-01-29 00:31:36 +000052 inline const Value *getArraySize() const { return getOperand(0); }
53 inline Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000054
55 /// getType - Overload to return most specific pointer type
56 ///
57 inline const PointerType *getType() const {
58 return reinterpret_cast<const PointerType*>(Instruction::getType());
59 }
60
61 /// getAllocatedType - Return the type that is being allocated by the
62 /// instruction.
63 ///
64 const Type *getAllocatedType() const;
65
66 virtual Instruction *clone() const = 0;
67
68 // Methods for support type inquiry through isa, cast, and dyn_cast:
69 static inline bool classof(const AllocationInst *) { return true; }
70 static inline bool classof(const Instruction *I) {
71 return I->getOpcode() == Instruction::Alloca ||
72 I->getOpcode() == Instruction::Malloc;
73 }
74 static inline bool classof(const Value *V) {
75 return isa<Instruction>(V) && classof(cast<Instruction>(V));
76 }
77};
78
79
80//===----------------------------------------------------------------------===//
81// MallocInst Class
82//===----------------------------------------------------------------------===//
83
84/// MallocInst - an instruction to allocated memory on the heap
85///
86class MallocInst : public AllocationInst {
87 MallocInst(const MallocInst &MI);
88public:
89 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
90 const std::string &Name = "",
91 Instruction *InsertBefore = 0)
92 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
93 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
94 BasicBlock *InsertAtEnd)
95 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertAtEnd) {}
96
Chris Lattnerf319e832004-10-15 23:52:05 +000097 virtual MallocInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000098
99 // Methods for support type inquiry through isa, cast, and dyn_cast:
100 static inline bool classof(const MallocInst *) { return true; }
101 static inline bool classof(const Instruction *I) {
102 return (I->getOpcode() == Instruction::Malloc);
103 }
104 static inline bool classof(const Value *V) {
105 return isa<Instruction>(V) && classof(cast<Instruction>(V));
106 }
107};
108
109
110//===----------------------------------------------------------------------===//
111// AllocaInst Class
112//===----------------------------------------------------------------------===//
113
114/// AllocaInst - an instruction to allocate memory on the stack
115///
116class AllocaInst : public AllocationInst {
117 AllocaInst(const AllocaInst &);
118public:
119 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
120 const std::string &Name = "",
121 Instruction *InsertBefore = 0)
122 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
123 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
124 BasicBlock *InsertAtEnd)
125 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertAtEnd) {}
126
Chris Lattnerf319e832004-10-15 23:52:05 +0000127 virtual AllocaInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000128
129 // Methods for support type inquiry through isa, cast, and dyn_cast:
130 static inline bool classof(const AllocaInst *) { return true; }
131 static inline bool classof(const Instruction *I) {
132 return (I->getOpcode() == Instruction::Alloca);
133 }
134 static inline bool classof(const Value *V) {
135 return isa<Instruction>(V) && classof(cast<Instruction>(V));
136 }
137};
138
139
140//===----------------------------------------------------------------------===//
141// FreeInst Class
142//===----------------------------------------------------------------------===//
143
144/// FreeInst - an instruction to deallocate memory
145///
Chris Lattner454928e2005-01-29 00:31:36 +0000146class FreeInst : public UnaryInstruction {
147 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000148public:
149 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
150 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
151
Chris Lattnerf319e832004-10-15 23:52:05 +0000152 virtual FreeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000153
154 virtual bool mayWriteToMemory() const { return true; }
155
156 // Methods for support type inquiry through isa, cast, and dyn_cast:
157 static inline bool classof(const FreeInst *) { return true; }
158 static inline bool classof(const Instruction *I) {
159 return (I->getOpcode() == Instruction::Free);
160 }
161 static inline bool classof(const Value *V) {
162 return isa<Instruction>(V) && classof(cast<Instruction>(V));
163 }
164};
165
166
167//===----------------------------------------------------------------------===//
168// LoadInst Class
169//===----------------------------------------------------------------------===//
170
Chris Lattner88fe29a2005-02-05 01:44:18 +0000171/// LoadInst - an instruction for reading from memory. This uses the
172/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000173///
Chris Lattner454928e2005-01-29 00:31:36 +0000174class LoadInst : public UnaryInstruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000175 LoadInst(const LoadInst &LI)
Chris Lattner88fe29a2005-02-05 01:44:18 +0000176 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
177 setVolatile(LI.isVolatile());
178
Chris Lattner454928e2005-01-29 00:31:36 +0000179#ifndef NDEBUG
180 AssertOK();
181#endif
182 }
183 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000184public:
185 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
186 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
187 LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
188 Instruction *InsertBefore = 0);
189 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
190 BasicBlock *InsertAtEnd);
191
192 /// isVolatile - Return true if this is a load from a volatile memory
193 /// location.
194 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000195 bool isVolatile() const { return SubclassData; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000196
197 /// setVolatile - Specify whether this is a volatile load or not.
198 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000199 void setVolatile(bool V) { SubclassData = V; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000200
Chris Lattnerf319e832004-10-15 23:52:05 +0000201 virtual LoadInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000202
203 virtual bool mayWriteToMemory() const { return isVolatile(); }
204
205 Value *getPointerOperand() { return getOperand(0); }
206 const Value *getPointerOperand() const { return getOperand(0); }
207 static unsigned getPointerOperandIndex() { return 0U; }
208
209 // Methods for support type inquiry through isa, cast, and dyn_cast:
210 static inline bool classof(const LoadInst *) { return true; }
211 static inline bool classof(const Instruction *I) {
212 return I->getOpcode() == Instruction::Load;
213 }
214 static inline bool classof(const Value *V) {
215 return isa<Instruction>(V) && classof(cast<Instruction>(V));
216 }
217};
218
219
220//===----------------------------------------------------------------------===//
221// StoreInst Class
222//===----------------------------------------------------------------------===//
223
224/// StoreInst - an instruction for storing to memory
225///
226class StoreInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000227 Use Ops[2];
Chris Lattner88fe29a2005-02-05 01:44:18 +0000228 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
Chris Lattner454928e2005-01-29 00:31:36 +0000229 Ops[0].init(SI.Ops[0], this);
230 Ops[1].init(SI.Ops[1], this);
Chris Lattner88fe29a2005-02-05 01:44:18 +0000231 setVolatile(SI.isVolatile());
Chris Lattner454928e2005-01-29 00:31:36 +0000232#ifndef NDEBUG
233 AssertOK();
234#endif
235 }
236 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000237public:
238 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
239 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
240 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
241 Instruction *InsertBefore = 0);
242 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
243
244
245 /// isVolatile - Return true if this is a load from a volatile memory
246 /// location.
247 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000248 bool isVolatile() const { return SubclassData; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000249
250 /// setVolatile - Specify whether this is a volatile load or not.
251 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000252 void setVolatile(bool V) { SubclassData = V; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000253
Chris Lattner454928e2005-01-29 00:31:36 +0000254 /// Transparently provide more efficient getOperand methods.
255 Value *getOperand(unsigned i) const {
256 assert(i < 2 && "getOperand() out of range!");
257 return Ops[i];
258 }
259 void setOperand(unsigned i, Value *Val) {
260 assert(i < 2 && "setOperand() out of range!");
261 Ops[i] = Val;
262 }
263 unsigned getNumOperands() const { return 2; }
264
265
Chris Lattnerf319e832004-10-15 23:52:05 +0000266 virtual StoreInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000267
268 virtual bool mayWriteToMemory() const { return true; }
269
270 Value *getPointerOperand() { return getOperand(1); }
271 const Value *getPointerOperand() const { return getOperand(1); }
272 static unsigned getPointerOperandIndex() { return 1U; }
273
274 // Methods for support type inquiry through isa, cast, and dyn_cast:
275 static inline bool classof(const StoreInst *) { return true; }
276 static inline bool classof(const Instruction *I) {
277 return I->getOpcode() == Instruction::Store;
278 }
279 static inline bool classof(const Value *V) {
280 return isa<Instruction>(V) && classof(cast<Instruction>(V));
281 }
282};
283
284
285//===----------------------------------------------------------------------===//
286// GetElementPtrInst Class
287//===----------------------------------------------------------------------===//
288
289/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
290/// access elements of arrays and structs
291///
292class GetElementPtrInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000293 GetElementPtrInst(const GetElementPtrInst &GEPI)
294 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
295 0, GEPI.getNumOperands()) {
296 Use *OL = OperandList = new Use[NumOperands];
297 Use *GEPIOL = GEPI.OperandList;
298 for (unsigned i = 0, E = NumOperands; i != E; ++i)
299 OL[i].init(GEPIOL[i], this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000300 }
301 void init(Value *Ptr, const std::vector<Value*> &Idx);
302 void init(Value *Ptr, Value *Idx0, Value *Idx1);
303public:
304 /// Constructors - Create a getelementptr instruction with a base pointer an
305 /// list of indices. The first ctor can optionally insert before an existing
306 /// instruction, the second appends the new instruction to the specified
307 /// BasicBlock.
308 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
309 const std::string &Name = "", Instruction *InsertBefore =0);
310 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
311 const std::string &Name, BasicBlock *InsertAtEnd);
312
313 /// Constructors - These two constructors are convenience methods because two
314 /// index getelementptr instructions are so common.
315 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
316 const std::string &Name = "", Instruction *InsertBefore =0);
317 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
318 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattner454928e2005-01-29 00:31:36 +0000319 ~GetElementPtrInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000320
Chris Lattnerf319e832004-10-15 23:52:05 +0000321 virtual GetElementPtrInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000322
323 // getType - Overload to return most specific pointer type...
324 inline const PointerType *getType() const {
325 return reinterpret_cast<const PointerType*>(Instruction::getType());
326 }
327
328 /// getIndexedType - Returns the type of the element that would be loaded with
329 /// a load instruction with the specified parameters.
330 ///
331 /// A null type is returned if the indices are invalid for the specified
332 /// pointer type.
333 ///
334 static const Type *getIndexedType(const Type *Ptr,
335 const std::vector<Value*> &Indices,
336 bool AllowStructLeaf = false);
337 static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
338 bool AllowStructLeaf = false);
339
340 inline op_iterator idx_begin() { return op_begin()+1; }
341 inline const_op_iterator idx_begin() const { return op_begin()+1; }
342 inline op_iterator idx_end() { return op_end(); }
343 inline const_op_iterator idx_end() const { return op_end(); }
344
345 Value *getPointerOperand() {
346 return getOperand(0);
347 }
348 const Value *getPointerOperand() const {
349 return getOperand(0);
350 }
351 static unsigned getPointerOperandIndex() {
352 return 0U; // get index for modifying correct operand
353 }
354
355 inline unsigned getNumIndices() const { // Note: always non-negative
356 return getNumOperands() - 1;
357 }
358
359 inline bool hasIndices() const {
360 return getNumOperands() > 1;
361 }
362
363 // Methods for support type inquiry through isa, cast, and dyn_cast:
364 static inline bool classof(const GetElementPtrInst *) { return true; }
365 static inline bool classof(const Instruction *I) {
366 return (I->getOpcode() == Instruction::GetElementPtr);
367 }
368 static inline bool classof(const Value *V) {
369 return isa<Instruction>(V) && classof(cast<Instruction>(V));
370 }
371};
372
373//===----------------------------------------------------------------------===//
374// SetCondInst Class
375//===----------------------------------------------------------------------===//
376
377/// SetCondInst class - Represent a setCC operator, where CC is eq, ne, lt, gt,
378/// le, or ge.
379///
380class SetCondInst : public BinaryOperator {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000381public:
382 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
383 const std::string &Name = "", Instruction *InsertBefore = 0);
384 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
385 const std::string &Name, BasicBlock *InsertAtEnd);
386
387 /// getInverseCondition - Return the inverse of the current condition opcode.
388 /// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
389 ///
390 BinaryOps getInverseCondition() const {
391 return getInverseCondition(getOpcode());
392 }
393
394 /// getInverseCondition - Static version that you can use without an
395 /// instruction available.
396 ///
397 static BinaryOps getInverseCondition(BinaryOps Opcode);
398
399 /// getSwappedCondition - Return the condition opcode that would be the result
400 /// of exchanging the two operands of the setcc instruction without changing
401 /// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
402 ///
403 BinaryOps getSwappedCondition() const {
404 return getSwappedCondition(getOpcode());
405 }
406
407 /// getSwappedCondition - Static version that you can use without an
408 /// instruction available.
409 ///
410 static BinaryOps getSwappedCondition(BinaryOps Opcode);
411
412
413 // Methods for support type inquiry through isa, cast, and dyn_cast:
414 static inline bool classof(const SetCondInst *) { return true; }
415 static inline bool classof(const Instruction *I) {
416 return I->getOpcode() == SetEQ || I->getOpcode() == SetNE ||
417 I->getOpcode() == SetLE || I->getOpcode() == SetGE ||
418 I->getOpcode() == SetLT || I->getOpcode() == SetGT;
419 }
420 static inline bool classof(const Value *V) {
421 return isa<Instruction>(V) && classof(cast<Instruction>(V));
422 }
423};
424
425//===----------------------------------------------------------------------===//
426// CastInst Class
427//===----------------------------------------------------------------------===//
428
429/// CastInst - This class represents a cast from Operand[0] to the type of
430/// the instruction (i->getType()).
431///
Chris Lattner454928e2005-01-29 00:31:36 +0000432class CastInst : public UnaryInstruction {
433 CastInst(const CastInst &CI)
434 : UnaryInstruction(CI.getType(), Cast, CI.getOperand(0)) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000435 }
436public:
437 CastInst(Value *S, const Type *Ty, const std::string &Name = "",
438 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000439 : UnaryInstruction(Ty, Cast, S, Name, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000440 }
441 CastInst(Value *S, const Type *Ty, const std::string &Name,
442 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000443 : UnaryInstruction(Ty, Cast, S, Name, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000444 }
445
Chris Lattnerf319e832004-10-15 23:52:05 +0000446 virtual CastInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000447
448 // Methods for support type inquiry through isa, cast, and dyn_cast:
449 static inline bool classof(const CastInst *) { return true; }
450 static inline bool classof(const Instruction *I) {
451 return I->getOpcode() == Cast;
452 }
453 static inline bool classof(const Value *V) {
454 return isa<Instruction>(V) && classof(cast<Instruction>(V));
455 }
456};
457
458
459//===----------------------------------------------------------------------===//
460// CallInst Class
461//===----------------------------------------------------------------------===//
462
463/// CallInst - This class represents a function call, abstracting a target
464/// machine's calling convention.
465///
466class CallInst : public Instruction {
467 CallInst(const CallInst &CI);
468 void init(Value *Func, const std::vector<Value*> &Params);
469 void init(Value *Func, Value *Actual1, Value *Actual2);
470 void init(Value *Func, Value *Actual);
471 void init(Value *Func);
472
473public:
474 CallInst(Value *F, const std::vector<Value*> &Par,
475 const std::string &Name = "", Instruction *InsertBefore = 0);
476 CallInst(Value *F, const std::vector<Value*> &Par,
477 const std::string &Name, BasicBlock *InsertAtEnd);
478
479 // Alternate CallInst ctors w/ two actuals, w/ one actual and no
480 // actuals, respectively.
481 CallInst(Value *F, Value *Actual1, Value *Actual2,
482 const std::string& Name = "", Instruction *InsertBefore = 0);
483 CallInst(Value *F, Value *Actual1, Value *Actual2,
484 const std::string& Name, BasicBlock *InsertAtEnd);
485 CallInst(Value *F, Value *Actual, const std::string& Name = "",
486 Instruction *InsertBefore = 0);
487 CallInst(Value *F, Value *Actual, const std::string& Name,
488 BasicBlock *InsertAtEnd);
489 explicit CallInst(Value *F, const std::string &Name = "",
490 Instruction *InsertBefore = 0);
491 explicit CallInst(Value *F, const std::string &Name,
492 BasicBlock *InsertAtEnd);
Chris Lattner454928e2005-01-29 00:31:36 +0000493 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000494
Chris Lattnerf319e832004-10-15 23:52:05 +0000495 virtual CallInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000496 bool mayWriteToMemory() const { return true; }
497
Chris Lattner721aef62004-11-18 17:46:57 +0000498 /// getCalledFunction - Return the function being called by this instruction
499 /// if it is a direct call. If it is a call through a function pointer,
500 /// return null.
501 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +0000502 return (Function*)dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +0000503 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000504
505 // getCalledValue - Get a pointer to a method that is invoked by this inst.
Chris Lattner454928e2005-01-29 00:31:36 +0000506 inline const Value *getCalledValue() const { return getOperand(0); }
507 inline Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000508
509 // Methods for support type inquiry through isa, cast, and dyn_cast:
510 static inline bool classof(const CallInst *) { return true; }
511 static inline bool classof(const Instruction *I) {
512 return I->getOpcode() == Instruction::Call;
513 }
514 static inline bool classof(const Value *V) {
515 return isa<Instruction>(V) && classof(cast<Instruction>(V));
516 }
517};
518
519
520//===----------------------------------------------------------------------===//
521// ShiftInst Class
522//===----------------------------------------------------------------------===//
523
524/// ShiftInst - This class represents left and right shift instructions.
525///
526class ShiftInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000527 Use Ops[2];
528 ShiftInst(const ShiftInst &SI)
529 : Instruction(SI.getType(), SI.getOpcode(), Ops, 2) {
530 Ops[0].init(SI.Ops[0], this);
531 Ops[1].init(SI.Ops[1], this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000532 }
533 void init(OtherOps Opcode, Value *S, Value *SA) {
534 assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
Chris Lattner454928e2005-01-29 00:31:36 +0000535 Ops[0].init(S, this);
536 Ops[1].init(SA, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000537 }
538
539public:
540 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
541 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000542 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000543 init(Opcode, S, SA);
544 }
545 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name,
546 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000547 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000548 init(Opcode, S, SA);
549 }
550
551 OtherOps getOpcode() const {
552 return static_cast<OtherOps>(Instruction::getOpcode());
553 }
554
Chris Lattner454928e2005-01-29 00:31:36 +0000555 /// Transparently provide more efficient getOperand methods.
556 Value *getOperand(unsigned i) const {
557 assert(i < 2 && "getOperand() out of range!");
558 return Ops[i];
559 }
560 void setOperand(unsigned i, Value *Val) {
561 assert(i < 2 && "setOperand() out of range!");
562 Ops[i] = Val;
563 }
564 unsigned getNumOperands() const { return 2; }
565
Chris Lattnerf319e832004-10-15 23:52:05 +0000566 virtual ShiftInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000567
568 // Methods for support type inquiry through isa, cast, and dyn_cast:
569 static inline bool classof(const ShiftInst *) { return true; }
570 static inline bool classof(const Instruction *I) {
571 return (I->getOpcode() == Instruction::Shr) |
572 (I->getOpcode() == Instruction::Shl);
573 }
574 static inline bool classof(const Value *V) {
575 return isa<Instruction>(V) && classof(cast<Instruction>(V));
576 }
577};
578
579//===----------------------------------------------------------------------===//
580// SelectInst Class
581//===----------------------------------------------------------------------===//
582
583/// SelectInst - This class represents the LLVM 'select' instruction.
584///
585class SelectInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000586 Use Ops[3];
587
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000588 void init(Value *C, Value *S1, Value *S2) {
Chris Lattner454928e2005-01-29 00:31:36 +0000589 Ops[0].init(C, this);
590 Ops[1].init(S1, this);
591 Ops[2].init(S2, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000592 }
593
Chris Lattner454928e2005-01-29 00:31:36 +0000594 SelectInst(const SelectInst &SI)
595 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
596 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
597 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000598public:
599 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
600 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000601 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
602 Name, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000603 init(C, S1, S2);
604 }
605 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
606 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000607 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
608 Name, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000609 init(C, S1, S2);
610 }
611
Chris Lattner454928e2005-01-29 00:31:36 +0000612 Value *getCondition() const { return Ops[0]; }
613 Value *getTrueValue() const { return Ops[1]; }
614 Value *getFalseValue() const { return Ops[2]; }
615
616 /// Transparently provide more efficient getOperand methods.
617 Value *getOperand(unsigned i) const {
618 assert(i < 3 && "getOperand() out of range!");
619 return Ops[i];
620 }
621 void setOperand(unsigned i, Value *Val) {
622 assert(i < 3 && "setOperand() out of range!");
623 Ops[i] = Val;
624 }
625 unsigned getNumOperands() const { return 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000626
627 OtherOps getOpcode() const {
628 return static_cast<OtherOps>(Instruction::getOpcode());
629 }
630
Chris Lattnerf319e832004-10-15 23:52:05 +0000631 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000632
633 // Methods for support type inquiry through isa, cast, and dyn_cast:
634 static inline bool classof(const SelectInst *) { return true; }
635 static inline bool classof(const Instruction *I) {
636 return I->getOpcode() == Instruction::Select;
637 }
638 static inline bool classof(const Value *V) {
639 return isa<Instruction>(V) && classof(cast<Instruction>(V));
640 }
641};
642
643
644//===----------------------------------------------------------------------===//
645// VANextInst Class
646//===----------------------------------------------------------------------===//
647
648/// VANextInst - This class represents the va_next llvm instruction, which
649/// advances a vararg list passed an argument of the specified type, returning
650/// the resultant list.
651///
Chris Lattner454928e2005-01-29 00:31:36 +0000652class VANextInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000653 PATypeHolder ArgTy;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000654 VANextInst(const VANextInst &VAN)
Chris Lattner454928e2005-01-29 00:31:36 +0000655 : UnaryInstruction(VAN.getType(), VANext, VAN.getOperand(0)),
656 ArgTy(VAN.getArgType()) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000657 }
658
659public:
660 VANextInst(Value *List, const Type *Ty, const std::string &Name = "",
661 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000662 : UnaryInstruction(List->getType(), VANext, List, Name, InsertBefore),
663 ArgTy(Ty) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000664 }
665 VANextInst(Value *List, const Type *Ty, const std::string &Name,
666 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000667 : UnaryInstruction(List->getType(), VANext, List, Name, InsertAtEnd),
668 ArgTy(Ty) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000669 }
670
671 const Type *getArgType() const { return ArgTy; }
672
Chris Lattnerf319e832004-10-15 23:52:05 +0000673 virtual VANextInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000674
675 // Methods for support type inquiry through isa, cast, and dyn_cast:
676 static inline bool classof(const VANextInst *) { return true; }
677 static inline bool classof(const Instruction *I) {
678 return I->getOpcode() == VANext;
679 }
680 static inline bool classof(const Value *V) {
681 return isa<Instruction>(V) && classof(cast<Instruction>(V));
682 }
683};
684
685
686//===----------------------------------------------------------------------===//
687// VAArgInst Class
688//===----------------------------------------------------------------------===//
689
690/// VAArgInst - This class represents the va_arg llvm instruction, which returns
691/// an argument of the specified type given a va_list.
692///
Chris Lattner454928e2005-01-29 00:31:36 +0000693class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000694 VAArgInst(const VAArgInst &VAA)
Chris Lattner454928e2005-01-29 00:31:36 +0000695 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000696public:
697 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
698 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000699 : UnaryInstruction(Ty, VAArg, List, Name, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000700 }
701 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
702 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000703 : UnaryInstruction(Ty, VAArg, List, Name, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000704 }
705
Chris Lattnerf319e832004-10-15 23:52:05 +0000706 virtual VAArgInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000707
708 // Methods for support type inquiry through isa, cast, and dyn_cast:
709 static inline bool classof(const VAArgInst *) { return true; }
710 static inline bool classof(const Instruction *I) {
711 return I->getOpcode() == VAArg;
712 }
713 static inline bool classof(const Value *V) {
714 return isa<Instruction>(V) && classof(cast<Instruction>(V));
715 }
716};
717
718//===----------------------------------------------------------------------===//
719// PHINode Class
720//===----------------------------------------------------------------------===//
721
722// PHINode - The PHINode class is used to represent the magical mystical PHI
723// node, that can not exist in nature, but can be synthesized in a computer
724// scientist's overactive imagination.
725//
726class PHINode : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000727 /// ReservedSpace - The number of operands actually allocated. NumOperands is
728 /// the number actually in use.
729 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000730 PHINode(const PHINode &PN);
731public:
732 PHINode(const Type *Ty, const std::string &Name = "",
733 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000734 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertBefore),
735 ReservedSpace(0) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000736 }
737
738 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000739 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertAtEnd),
740 ReservedSpace(0) {
741 }
742
743 ~PHINode();
744
745 /// reserveOperandSpace - This method can be used to avoid repeated
746 /// reallocation of PHI operand lists by reserving space for the correct
747 /// number of operands before adding them. Unlike normal vector reserves,
748 /// this method can also be used to trim the operand space.
749 void reserveOperandSpace(unsigned NumValues) {
750 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000751 }
752
Chris Lattnerf319e832004-10-15 23:52:05 +0000753 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000754
755 /// getNumIncomingValues - Return the number of incoming edges
756 ///
Chris Lattner454928e2005-01-29 00:31:36 +0000757 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000758
759 /// getIncomingValue - Return incoming value #x
760 ///
761 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +0000762 assert(i*2 < getNumOperands() && "Invalid value number!");
763 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000764 }
765 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +0000766 assert(i*2 < getNumOperands() && "Invalid value number!");
767 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000768 }
Chris Lattner454928e2005-01-29 00:31:36 +0000769 unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000770 return i*2;
771 }
772
773 /// getIncomingBlock - Return incoming basic block #x
774 ///
775 BasicBlock *getIncomingBlock(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +0000776 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000777 }
778 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Chris Lattner454928e2005-01-29 00:31:36 +0000779 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000780 }
781 unsigned getOperandNumForIncomingBlock(unsigned i) {
782 return i*2+1;
783 }
784
785 /// addIncoming - Add an incoming value to the end of the PHI list
786 ///
787 void addIncoming(Value *V, BasicBlock *BB) {
788 assert(getType() == V->getType() &&
789 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +0000790 unsigned OpNo = NumOperands;
791 if (OpNo+2 > ReservedSpace)
792 resizeOperands(0); // Get more space!
793 // Initialize some new operands.
794 NumOperands = OpNo+2;
795 OperandList[OpNo].init(V, this);
796 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000797 }
798
799 /// removeIncomingValue - Remove an incoming value. This is useful if a
800 /// predecessor basic block is deleted. The value removed is returned.
801 ///
802 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
803 /// is true), the PHI node is destroyed and any uses of it are replaced with
804 /// dummy values. The only time there should be zero incoming values to a PHI
805 /// node is when the block is dead, so this strategy is sound.
806 ///
807 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
808
809 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
810 int Idx = getBasicBlockIndex(BB);
811 assert(Idx >= 0 && "Invalid basic block argument to remove!");
812 return removeIncomingValue(Idx, DeletePHIIfEmpty);
813 }
814
815 /// getBasicBlockIndex - Return the first index of the specified basic
816 /// block in the value list for this PHI. Returns -1 if no instance.
817 ///
818 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +0000819 Use *OL = OperandList;
820 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
821 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000822 return -1;
823 }
824
825 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
826 return getIncomingValue(getBasicBlockIndex(BB));
827 }
828
829 /// Methods for support type inquiry through isa, cast, and dyn_cast:
830 static inline bool classof(const PHINode *) { return true; }
831 static inline bool classof(const Instruction *I) {
832 return I->getOpcode() == Instruction::PHI;
833 }
834 static inline bool classof(const Value *V) {
835 return isa<Instruction>(V) && classof(cast<Instruction>(V));
836 }
Chris Lattner454928e2005-01-29 00:31:36 +0000837 private:
838 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000839};
840
841//===----------------------------------------------------------------------===//
842// ReturnInst Class
843//===----------------------------------------------------------------------===//
844
845//===---------------------------------------------------------------------------
846/// ReturnInst - Return a value (possibly void), from a function. Execution
847/// does not continue in this function any longer.
848///
849class ReturnInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +0000850 Use RetVal; // Possibly null retval.
851 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret, &RetVal,
852 RI.getNumOperands()) {
853 if (RI.getNumOperands())
854 RetVal.init(RI.RetVal, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000855 }
856
Alkis Evlogimenos859804f2004-11-17 21:02:25 +0000857 void init(Value *RetVal);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000858
859public:
860 // ReturnInst constructors:
861 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +0000862 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000863 // ReturnInst(Value* X) - 'ret X' instruction
864 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
865 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
866 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
867 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
Alkis Evlogimenos859804f2004-11-17 21:02:25 +0000868 //
869 // NOTE: If the Value* passed is of type void then the constructor behaves as
870 // if it was passed NULL.
Chris Lattner454928e2005-01-29 00:31:36 +0000871 ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0)
872 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertBefore) {
873 init(retVal);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000874 }
Chris Lattner454928e2005-01-29 00:31:36 +0000875 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
876 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
877 init(retVal);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000878 }
879 ReturnInst(BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000880 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000881 }
882
Chris Lattnerf319e832004-10-15 23:52:05 +0000883 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000884
Chris Lattner454928e2005-01-29 00:31:36 +0000885 // Transparently provide more efficient getOperand methods.
886 Value *getOperand(unsigned i) const {
887 assert(i < getNumOperands() && "getOperand() out of range!");
888 return RetVal;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000889 }
Chris Lattner454928e2005-01-29 00:31:36 +0000890 void setOperand(unsigned i, Value *Val) {
891 assert(i < getNumOperands() && "setOperand() out of range!");
892 RetVal = Val;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000893 }
894
Chris Lattner454928e2005-01-29 00:31:36 +0000895 Value *getReturnValue() const { return RetVal; }
896
897 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000898
899 // Methods for support type inquiry through isa, cast, and dyn_cast:
900 static inline bool classof(const ReturnInst *) { return true; }
901 static inline bool classof(const Instruction *I) {
902 return (I->getOpcode() == Instruction::Ret);
903 }
904 static inline bool classof(const Value *V) {
905 return isa<Instruction>(V) && classof(cast<Instruction>(V));
906 }
Chris Lattner454928e2005-01-29 00:31:36 +0000907 private:
908 virtual BasicBlock *getSuccessorV(unsigned idx) const;
909 virtual unsigned getNumSuccessorsV() const;
910 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000911};
912
913//===----------------------------------------------------------------------===//
914// BranchInst Class
915//===----------------------------------------------------------------------===//
916
917//===---------------------------------------------------------------------------
918/// BranchInst - Conditional or Unconditional Branch instruction.
919///
920class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +0000921 /// Ops list - Branches are strange. The operands are ordered:
922 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
923 /// they don't have to check for cond/uncond branchness.
924 Use Ops[3];
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000925 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +0000926 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000927public:
928 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
929 // BranchInst(BB *B) - 'br B'
930 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
931 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
932 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
933 // BranchInst(BB* B, BB *I) - 'br B' insert at end
934 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
935 BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000936 : TerminatorInst(Instruction::Br, Ops, 1, InsertBefore) {
937 assert(IfTrue != 0 && "Branch destination may not be null!");
938 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000939 }
940 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
941 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000942 : TerminatorInst(Instruction::Br, Ops, 3, InsertBefore) {
943 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
944 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
945 Ops[2].init(Cond, this);
946#ifndef NDEBUG
947 AssertOK();
948#endif
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000949 }
950
951 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000952 : TerminatorInst(Instruction::Br, Ops, 1, InsertAtEnd) {
953 assert(IfTrue != 0 && "Branch destination may not be null!");
954 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000955 }
956
957 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
958 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000959 : TerminatorInst(Instruction::Br, Ops, 3, InsertAtEnd) {
960 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
961 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
962 Ops[2].init(Cond, this);
963#ifndef NDEBUG
964 AssertOK();
965#endif
966 }
967
968
969 /// Transparently provide more efficient getOperand methods.
970 Value *getOperand(unsigned i) const {
971 assert(i < getNumOperands() && "getOperand() out of range!");
972 return Ops[i];
973 }
974 void setOperand(unsigned i, Value *Val) {
975 assert(i < getNumOperands() && "setOperand() out of range!");
976 Ops[i] = Val;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000977 }
978
Chris Lattnerf319e832004-10-15 23:52:05 +0000979 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000980
Chris Lattner454928e2005-01-29 00:31:36 +0000981 inline bool isUnconditional() const { return getNumOperands() == 1; }
982 inline bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000983
984 inline Value *getCondition() const {
985 assert(isConditional() && "Cannot get condition of an uncond branch!");
Chris Lattner454928e2005-01-29 00:31:36 +0000986 return getOperand(2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000987 }
988
989 void setCondition(Value *V) {
990 assert(isConditional() && "Cannot set condition of unconditional branch!");
991 setOperand(2, V);
992 }
993
994 // setUnconditionalDest - Change the current branch to an unconditional branch
995 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +0000996 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000997 void setUnconditionalDest(BasicBlock *Dest) {
Chris Lattner454928e2005-01-29 00:31:36 +0000998 if (isConditional()) { // Convert this to an uncond branch.
999 NumOperands = 1;
1000 Ops[1].set(0);
1001 Ops[2].set(0);
1002 }
1003 setOperand(0, reinterpret_cast<Value*>(Dest));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001004 }
1005
Chris Lattner454928e2005-01-29 00:31:36 +00001006 unsigned getNumSuccessors() const { return 1+isConditional(); }
1007
1008 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001009 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001010 return (i == 0) ? cast<BasicBlock>(getOperand(0)) :
1011 cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001012 }
1013
Chris Lattner454928e2005-01-29 00:31:36 +00001014 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001015 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001016 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001017 }
1018
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001019 // Methods for support type inquiry through isa, cast, and dyn_cast:
1020 static inline bool classof(const BranchInst *) { return true; }
1021 static inline bool classof(const Instruction *I) {
1022 return (I->getOpcode() == Instruction::Br);
1023 }
1024 static inline bool classof(const Value *V) {
1025 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1026 }
Chris Lattner454928e2005-01-29 00:31:36 +00001027private:
1028 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1029 virtual unsigned getNumSuccessorsV() const;
1030 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001031};
1032
1033//===----------------------------------------------------------------------===//
1034// SwitchInst Class
1035//===----------------------------------------------------------------------===//
1036
1037//===---------------------------------------------------------------------------
1038/// SwitchInst - Multiway switch
1039///
1040class SwitchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001041 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001042 // Operand[0] = Value to switch on
1043 // Operand[1] = Default basic block destination
1044 // Operand[2n ] = Value to match
1045 // Operand[2n+1] = BasicBlock to go to on match
1046 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00001047 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1048 void resizeOperands(unsigned No);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001049public:
Chris Lattner454928e2005-01-29 00:31:36 +00001050 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1051 /// switch on and a default destination. The number of additional cases can
1052 /// be specified here to make memory allocation more efficient. This
1053 /// constructor can also autoinsert before another instruction.
1054 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1055 Instruction *InsertBefore = 0)
1056 : TerminatorInst(Instruction::Switch, 0, 0, InsertBefore) {
1057 init(Value, Default, NumCases);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001058 }
1059
Chris Lattner454928e2005-01-29 00:31:36 +00001060 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1061 /// switch on and a default destination. The number of additional cases can
1062 /// be specified here to make memory allocation more efficient. This
1063 /// constructor also autoinserts at the end of the specified BasicBlock.
1064 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1065 BasicBlock *InsertAtEnd)
1066 : TerminatorInst(Instruction::Switch, 0, 0, InsertAtEnd) {
1067 init(Value, Default, NumCases);
1068 }
1069 ~SwitchInst();
1070
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001071
1072 // Accessor Methods for Switch stmt
Chris Lattner454928e2005-01-29 00:31:36 +00001073 inline Value *getCondition() const { return getOperand(0); }
1074 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00001075
Chris Lattner454928e2005-01-29 00:31:36 +00001076 inline BasicBlock *getDefaultDest() const {
1077 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001078 }
1079
1080 /// getNumCases - return the number of 'cases' in this switch instruction.
1081 /// Note that case #0 is always the default case.
1082 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001083 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001084 }
1085
1086 /// getCaseValue - Return the specified case value. Note that case #0, the
1087 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001088 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001089 assert(i && i < getNumCases() && "Illegal case value to get!");
1090 return getSuccessorValue(i);
1091 }
1092
1093 /// getCaseValue - Return the specified case value. Note that case #0, the
1094 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001095 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001096 assert(i && i < getNumCases() && "Illegal case value to get!");
1097 return getSuccessorValue(i);
1098 }
1099
1100 /// findCaseValue - Search all of the case values for the specified constant.
1101 /// If it is explicitly handled, return the case number of it, otherwise
1102 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001103 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001104 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1105 if (getCaseValue(i) == C)
1106 return i;
1107 return 0;
1108 }
1109
1110 /// addCase - Add an entry to the switch instruction...
1111 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00001112 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001113
1114 /// removeCase - This method removes the specified successor from the switch
1115 /// instruction. Note that this cannot be used to remove the default
1116 /// destination (successor #0).
1117 ///
1118 void removeCase(unsigned idx);
1119
Chris Lattner454928e2005-01-29 00:31:36 +00001120 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001121
Chris Lattner454928e2005-01-29 00:31:36 +00001122 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1123 BasicBlock *getSuccessor(unsigned idx) const {
1124 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1125 return cast<BasicBlock>(getOperand(idx*2+1));
1126 }
1127 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001128 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001129 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001130 }
1131
1132 // getSuccessorValue - Return the value associated with the specified
1133 // successor.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001134 inline ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001135 assert(idx < getNumSuccessors() && "Successor # out of range!");
Chris Lattnerd1a32602005-02-24 05:32:09 +00001136 return (ConstantInt*)getOperand(idx*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001137 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001138
1139 // Methods for support type inquiry through isa, cast, and dyn_cast:
1140 static inline bool classof(const SwitchInst *) { return true; }
1141 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00001142 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001143 }
1144 static inline bool classof(const Value *V) {
1145 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1146 }
Chris Lattner454928e2005-01-29 00:31:36 +00001147private:
1148 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1149 virtual unsigned getNumSuccessorsV() const;
1150 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001151};
1152
1153//===----------------------------------------------------------------------===//
1154// InvokeInst Class
1155//===----------------------------------------------------------------------===//
1156
1157//===---------------------------------------------------------------------------
1158/// InvokeInst - Invoke instruction
1159///
1160class InvokeInst : public TerminatorInst {
1161 InvokeInst(const InvokeInst &BI);
1162 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1163 const std::vector<Value*> &Params);
1164public:
1165 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1166 const std::vector<Value*> &Params, const std::string &Name = "",
1167 Instruction *InsertBefore = 0);
1168 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1169 const std::vector<Value*> &Params, const std::string &Name,
1170 BasicBlock *InsertAtEnd);
Chris Lattner454928e2005-01-29 00:31:36 +00001171 ~InvokeInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001172
Chris Lattnerf319e832004-10-15 23:52:05 +00001173 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001174
1175 bool mayWriteToMemory() const { return true; }
1176
1177 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00001178 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001179 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001180 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001181 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001182 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001183
1184 // getCalledValue - Get a pointer to a function that is invoked by this inst.
Chris Lattner454928e2005-01-29 00:31:36 +00001185 inline Value *getCalledValue() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001186
1187 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00001188 BasicBlock *getNormalDest() const {
1189 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001190 }
Chris Lattner454928e2005-01-29 00:31:36 +00001191 BasicBlock *getUnwindDest() const {
1192 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001193 }
Chris Lattner454928e2005-01-29 00:31:36 +00001194 void setNormalDest(BasicBlock *B) {
1195 setOperand(1, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001196 }
1197
Chris Lattner454928e2005-01-29 00:31:36 +00001198 void setUnwindDest(BasicBlock *B) {
1199 setOperand(2, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001200 }
1201
Chris Lattner454928e2005-01-29 00:31:36 +00001202 inline BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001203 assert(i < 2 && "Successor # out of range for invoke!");
1204 return i == 0 ? getNormalDest() : getUnwindDest();
1205 }
1206
Chris Lattner454928e2005-01-29 00:31:36 +00001207 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001208 assert(idx < 2 && "Successor # out of range for invoke!");
Chris Lattner454928e2005-01-29 00:31:36 +00001209 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001210 }
1211
Chris Lattner454928e2005-01-29 00:31:36 +00001212 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001213
1214 // Methods for support type inquiry through isa, cast, and dyn_cast:
1215 static inline bool classof(const InvokeInst *) { return true; }
1216 static inline bool classof(const Instruction *I) {
1217 return (I->getOpcode() == Instruction::Invoke);
1218 }
1219 static inline bool classof(const Value *V) {
1220 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1221 }
Chris Lattner454928e2005-01-29 00:31:36 +00001222private:
1223 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1224 virtual unsigned getNumSuccessorsV() const;
1225 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001226};
1227
1228
1229//===----------------------------------------------------------------------===//
1230// UnwindInst Class
1231//===----------------------------------------------------------------------===//
1232
1233//===---------------------------------------------------------------------------
1234/// UnwindInst - Immediately exit the current function, unwinding the stack
1235/// until an invoke instruction is found.
1236///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00001237class UnwindInst : public TerminatorInst {
1238public:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001239 UnwindInst(Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001240 : TerminatorInst(Instruction::Unwind, 0, 0, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001241 }
1242 UnwindInst(BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001243 : TerminatorInst(Instruction::Unwind, 0, 0, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001244 }
1245
Chris Lattnerf319e832004-10-15 23:52:05 +00001246 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001247
Chris Lattner454928e2005-01-29 00:31:36 +00001248 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001249
1250 // Methods for support type inquiry through isa, cast, and dyn_cast:
1251 static inline bool classof(const UnwindInst *) { return true; }
1252 static inline bool classof(const Instruction *I) {
1253 return I->getOpcode() == Instruction::Unwind;
1254 }
1255 static inline bool classof(const Value *V) {
1256 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1257 }
Chris Lattner454928e2005-01-29 00:31:36 +00001258private:
1259 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1260 virtual unsigned getNumSuccessorsV() const;
1261 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001262};
1263
Chris Lattner076b3f12004-10-16 18:05:54 +00001264//===----------------------------------------------------------------------===//
1265// UnreachableInst Class
1266//===----------------------------------------------------------------------===//
1267
1268//===---------------------------------------------------------------------------
1269/// UnreachableInst - This function has undefined behavior. In particular, the
1270/// presence of this instruction indicates some higher level knowledge that the
1271/// end of the block cannot be reached.
1272///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00001273class UnreachableInst : public TerminatorInst {
1274public:
Chris Lattner076b3f12004-10-16 18:05:54 +00001275 UnreachableInst(Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001276 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertBefore) {
Chris Lattner076b3f12004-10-16 18:05:54 +00001277 }
1278 UnreachableInst(BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001279 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertAtEnd) {
Chris Lattner076b3f12004-10-16 18:05:54 +00001280 }
1281
1282 virtual UnreachableInst *clone() const;
1283
Chris Lattner454928e2005-01-29 00:31:36 +00001284 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00001285
1286 // Methods for support type inquiry through isa, cast, and dyn_cast:
1287 static inline bool classof(const UnreachableInst *) { return true; }
1288 static inline bool classof(const Instruction *I) {
1289 return I->getOpcode() == Instruction::Unreachable;
1290 }
1291 static inline bool classof(const Value *V) {
1292 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1293 }
Chris Lattner454928e2005-01-29 00:31:36 +00001294private:
1295 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1296 virtual unsigned getNumSuccessorsV() const;
1297 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00001298};
1299
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001300} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00001301
1302#endif