blob: 2fb1a7a1427438a92a71655986269fa2cc6d2bfb [file] [log] [blame]
Chris Lattnera892a3a2003-01-27 22:08:52 +00001//===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
Misha Brukman9769ab22005-04-21 20:19:05 +00002//
John Criswell6fbcc262003-10-20 20:19:47 +00003// 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.
Misha Brukman9769ab22005-04-21 20:19:05 +00007//
John Criswell6fbcc262003-10-20 20:19:47 +00008//===----------------------------------------------------------------------===//
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/InstrTypes.h"
20
21namespace llvm {
22
Chris Lattner1fca5ff2004-10-27 16:14:51 +000023class BasicBlock;
Chris Lattnerd1a32602005-02-24 05:32:09 +000024class ConstantInt;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000025class PointerType;
Chris Lattner6a56ed42006-04-14 22:20:07 +000026class PackedType;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000027
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 {
Nate Begeman14b05292005-11-05 09:21:28 +000036 unsigned Alignment;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000037protected:
Nate Begeman14b05292005-11-05 09:21:28 +000038 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
Misha Brukman91102862005-03-16 03:46:55 +000039 const std::string &Name = "", Instruction *InsertBefore = 0);
Nate Begeman14b05292005-11-05 09:21:28 +000040 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
Misha Brukman91102862005-03-16 03:46:55 +000041 const std::string &Name, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000042public:
Chris Lattner70aa33e2006-06-21 16:53:47 +000043 // Out of line virtual method, so the vtable, etc has a home.
44 virtual ~AllocationInst();
45
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000046 /// isArrayAllocation - Return true if there is an allocation size parameter
47 /// to the allocation instruction that is not 1.
48 ///
49 bool isArrayAllocation() const;
50
51 /// getArraySize - Get the number of element allocated, for a simple
52 /// allocation of a single element, this will return a constant 1 value.
53 ///
Chris Lattner454928e2005-01-29 00:31:36 +000054 inline const Value *getArraySize() const { return getOperand(0); }
55 inline Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000056
57 /// getType - Overload to return most specific pointer type
58 ///
59 inline const PointerType *getType() const {
Misha Brukman9769ab22005-04-21 20:19:05 +000060 return reinterpret_cast<const PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000061 }
62
63 /// getAllocatedType - Return the type that is being allocated by the
64 /// instruction.
65 ///
66 const Type *getAllocatedType() const;
67
Nate Begeman14b05292005-11-05 09:21:28 +000068 /// getAlignment - Return the alignment of the memory that is being allocated
69 /// by the instruction.
70 ///
71 unsigned getAlignment() const { return Alignment; }
Chris Lattner8ae779d2005-11-05 21:58:30 +000072 void setAlignment(unsigned Align) {
73 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
74 Alignment = Align;
75 }
Nate Begeman14b05292005-11-05 09:21:28 +000076
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000077 virtual Instruction *clone() const = 0;
78
79 // Methods for support type inquiry through isa, cast, and dyn_cast:
80 static inline bool classof(const AllocationInst *) { return true; }
81 static inline bool classof(const Instruction *I) {
82 return I->getOpcode() == Instruction::Alloca ||
83 I->getOpcode() == Instruction::Malloc;
84 }
85 static inline bool classof(const Value *V) {
86 return isa<Instruction>(V) && classof(cast<Instruction>(V));
87 }
88};
89
90
91//===----------------------------------------------------------------------===//
92// MallocInst Class
93//===----------------------------------------------------------------------===//
94
95/// MallocInst - an instruction to allocated memory on the heap
96///
97class MallocInst : public AllocationInst {
98 MallocInst(const MallocInst &MI);
99public:
100 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
101 const std::string &Name = "",
102 Instruction *InsertBefore = 0)
Nate Begeman14b05292005-11-05 09:21:28 +0000103 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000104 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
105 BasicBlock *InsertAtEnd)
Nate Begeman14b05292005-11-05 09:21:28 +0000106 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
Chris Lattnerb77780e2006-05-10 04:38:35 +0000107
108 explicit MallocInst(const Type *Ty, const std::string &Name,
109 Instruction *InsertBefore = 0)
110 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
111 MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
112 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
113
Nate Begeman14b05292005-11-05 09:21:28 +0000114 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
115 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000116 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
117 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Nate Begeman14b05292005-11-05 09:21:28 +0000118 const std::string &Name = "",
119 Instruction *InsertBefore = 0)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000120 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
Nate Begeman14b05292005-11-05 09:21:28 +0000121
Chris Lattnerf319e832004-10-15 23:52:05 +0000122 virtual MallocInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000123
124 // Methods for support type inquiry through isa, cast, and dyn_cast:
125 static inline bool classof(const MallocInst *) { return true; }
126 static inline bool classof(const Instruction *I) {
127 return (I->getOpcode() == Instruction::Malloc);
128 }
129 static inline bool classof(const Value *V) {
130 return isa<Instruction>(V) && classof(cast<Instruction>(V));
131 }
132};
133
134
135//===----------------------------------------------------------------------===//
136// AllocaInst Class
137//===----------------------------------------------------------------------===//
138
139/// AllocaInst - an instruction to allocate memory on the stack
140///
141class AllocaInst : public AllocationInst {
142 AllocaInst(const AllocaInst &);
143public:
144 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
145 const std::string &Name = "",
146 Instruction *InsertBefore = 0)
Nate Begeman14b05292005-11-05 09:21:28 +0000147 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000148 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
149 BasicBlock *InsertAtEnd)
Nate Begeman14b05292005-11-05 09:21:28 +0000150 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
Chris Lattnerb77780e2006-05-10 04:38:35 +0000151
152 AllocaInst(const Type *Ty, const std::string &Name,
153 Instruction *InsertBefore = 0)
154 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
155 AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
156 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
157
158 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
159 const std::string &Name = "", Instruction *InsertBefore = 0)
160 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
Nate Begeman14b05292005-11-05 09:21:28 +0000161 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
162 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000163 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
Nate Begeman14b05292005-11-05 09:21:28 +0000164
Chris Lattnerf319e832004-10-15 23:52:05 +0000165 virtual AllocaInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000166
167 // Methods for support type inquiry through isa, cast, and dyn_cast:
168 static inline bool classof(const AllocaInst *) { return true; }
169 static inline bool classof(const Instruction *I) {
170 return (I->getOpcode() == Instruction::Alloca);
171 }
172 static inline bool classof(const Value *V) {
173 return isa<Instruction>(V) && classof(cast<Instruction>(V));
174 }
175};
176
177
178//===----------------------------------------------------------------------===//
179// FreeInst Class
180//===----------------------------------------------------------------------===//
181
182/// FreeInst - an instruction to deallocate memory
183///
Chris Lattner454928e2005-01-29 00:31:36 +0000184class FreeInst : public UnaryInstruction {
185 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000186public:
187 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
188 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
189
Chris Lattnerf319e832004-10-15 23:52:05 +0000190 virtual FreeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000191
192 virtual bool mayWriteToMemory() const { return true; }
193
194 // Methods for support type inquiry through isa, cast, and dyn_cast:
195 static inline bool classof(const FreeInst *) { return true; }
196 static inline bool classof(const Instruction *I) {
197 return (I->getOpcode() == Instruction::Free);
198 }
199 static inline bool classof(const Value *V) {
200 return isa<Instruction>(V) && classof(cast<Instruction>(V));
201 }
202};
203
204
205//===----------------------------------------------------------------------===//
206// LoadInst Class
207//===----------------------------------------------------------------------===//
208
Chris Lattner88fe29a2005-02-05 01:44:18 +0000209/// LoadInst - an instruction for reading from memory. This uses the
210/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000211///
Chris Lattner454928e2005-01-29 00:31:36 +0000212class LoadInst : public UnaryInstruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000213 LoadInst(const LoadInst &LI)
Chris Lattner88fe29a2005-02-05 01:44:18 +0000214 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
215 setVolatile(LI.isVolatile());
Misha Brukman9769ab22005-04-21 20:19:05 +0000216
Chris Lattner454928e2005-01-29 00:31:36 +0000217#ifndef NDEBUG
218 AssertOK();
219#endif
220 }
221 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000222public:
223 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
224 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
225 LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
226 Instruction *InsertBefore = 0);
227 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
228 BasicBlock *InsertAtEnd);
229
230 /// isVolatile - Return true if this is a load from a volatile memory
231 /// location.
232 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000233 bool isVolatile() const { return SubclassData; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000234
235 /// setVolatile - Specify whether this is a volatile load or not.
236 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000237 void setVolatile(bool V) { SubclassData = V; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000238
Chris Lattnerf319e832004-10-15 23:52:05 +0000239 virtual LoadInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000240
241 virtual bool mayWriteToMemory() const { return isVolatile(); }
242
243 Value *getPointerOperand() { return getOperand(0); }
244 const Value *getPointerOperand() const { return getOperand(0); }
245 static unsigned getPointerOperandIndex() { return 0U; }
246
247 // Methods for support type inquiry through isa, cast, and dyn_cast:
248 static inline bool classof(const LoadInst *) { return true; }
249 static inline bool classof(const Instruction *I) {
250 return I->getOpcode() == Instruction::Load;
251 }
252 static inline bool classof(const Value *V) {
253 return isa<Instruction>(V) && classof(cast<Instruction>(V));
254 }
255};
256
257
258//===----------------------------------------------------------------------===//
259// StoreInst Class
260//===----------------------------------------------------------------------===//
261
Misha Brukman9769ab22005-04-21 20:19:05 +0000262/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000263///
264class StoreInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000265 Use Ops[2];
Chris Lattner88fe29a2005-02-05 01:44:18 +0000266 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
Chris Lattner454928e2005-01-29 00:31:36 +0000267 Ops[0].init(SI.Ops[0], this);
268 Ops[1].init(SI.Ops[1], this);
Chris Lattner88fe29a2005-02-05 01:44:18 +0000269 setVolatile(SI.isVolatile());
Chris Lattner454928e2005-01-29 00:31:36 +0000270#ifndef NDEBUG
271 AssertOK();
272#endif
273 }
274 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000275public:
276 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
277 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
278 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
279 Instruction *InsertBefore = 0);
280 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
281
282
283 /// isVolatile - Return true if this is a load from a volatile memory
284 /// location.
285 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000286 bool isVolatile() const { return SubclassData; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000287
288 /// setVolatile - Specify whether this is a volatile load or not.
289 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000290 void setVolatile(bool V) { SubclassData = V; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000291
Chris Lattner454928e2005-01-29 00:31:36 +0000292 /// Transparently provide more efficient getOperand methods.
Misha Brukman9769ab22005-04-21 20:19:05 +0000293 Value *getOperand(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +0000294 assert(i < 2 && "getOperand() out of range!");
295 return Ops[i];
296 }
297 void setOperand(unsigned i, Value *Val) {
298 assert(i < 2 && "setOperand() out of range!");
299 Ops[i] = Val;
300 }
301 unsigned getNumOperands() const { return 2; }
302
303
Chris Lattnerf319e832004-10-15 23:52:05 +0000304 virtual StoreInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000305
306 virtual bool mayWriteToMemory() const { return true; }
307
308 Value *getPointerOperand() { return getOperand(1); }
309 const Value *getPointerOperand() const { return getOperand(1); }
310 static unsigned getPointerOperandIndex() { return 1U; }
311
312 // Methods for support type inquiry through isa, cast, and dyn_cast:
313 static inline bool classof(const StoreInst *) { return true; }
314 static inline bool classof(const Instruction *I) {
315 return I->getOpcode() == Instruction::Store;
316 }
317 static inline bool classof(const Value *V) {
318 return isa<Instruction>(V) && classof(cast<Instruction>(V));
319 }
320};
321
322
323//===----------------------------------------------------------------------===//
324// GetElementPtrInst Class
325//===----------------------------------------------------------------------===//
326
327/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
328/// access elements of arrays and structs
329///
330class GetElementPtrInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000331 GetElementPtrInst(const GetElementPtrInst &GEPI)
332 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
333 0, GEPI.getNumOperands()) {
334 Use *OL = OperandList = new Use[NumOperands];
335 Use *GEPIOL = GEPI.OperandList;
336 for (unsigned i = 0, E = NumOperands; i != E; ++i)
337 OL[i].init(GEPIOL[i], this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000338 }
339 void init(Value *Ptr, const std::vector<Value*> &Idx);
340 void init(Value *Ptr, Value *Idx0, Value *Idx1);
Chris Lattner38bacf22005-05-03 05:43:30 +0000341 void init(Value *Ptr, Value *Idx);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000342public:
343 /// Constructors - Create a getelementptr instruction with a base pointer an
344 /// list of indices. The first ctor can optionally insert before an existing
345 /// instruction, the second appends the new instruction to the specified
346 /// BasicBlock.
347 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
Misha Brukman91102862005-03-16 03:46:55 +0000348 const std::string &Name = "", Instruction *InsertBefore =0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000349 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
Misha Brukman91102862005-03-16 03:46:55 +0000350 const std::string &Name, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000351
Chris Lattner38bacf22005-05-03 05:43:30 +0000352 /// Constructors - These two constructors are convenience methods because one
353 /// and two index getelementptr instructions are so common.
354 GetElementPtrInst(Value *Ptr, Value *Idx,
355 const std::string &Name = "", Instruction *InsertBefore =0);
356 GetElementPtrInst(Value *Ptr, Value *Idx,
357 const std::string &Name, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000358 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman91102862005-03-16 03:46:55 +0000359 const std::string &Name = "", Instruction *InsertBefore =0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000360 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman91102862005-03-16 03:46:55 +0000361 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattner454928e2005-01-29 00:31:36 +0000362 ~GetElementPtrInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000363
Chris Lattnerf319e832004-10-15 23:52:05 +0000364 virtual GetElementPtrInst *clone() const;
Misha Brukman9769ab22005-04-21 20:19:05 +0000365
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000366 // getType - Overload to return most specific pointer type...
367 inline const PointerType *getType() const {
368 return reinterpret_cast<const PointerType*>(Instruction::getType());
369 }
370
371 /// getIndexedType - Returns the type of the element that would be loaded with
372 /// a load instruction with the specified parameters.
373 ///
Misha Brukman9769ab22005-04-21 20:19:05 +0000374 /// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000375 /// pointer type.
376 ///
Misha Brukman9769ab22005-04-21 20:19:05 +0000377 static const Type *getIndexedType(const Type *Ptr,
Misha Brukman91102862005-03-16 03:46:55 +0000378 const std::vector<Value*> &Indices,
379 bool AllowStructLeaf = false);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000380 static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman91102862005-03-16 03:46:55 +0000381 bool AllowStructLeaf = false);
Chris Lattner38bacf22005-05-03 05:43:30 +0000382 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
Misha Brukman9769ab22005-04-21 20:19:05 +0000383
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000384 inline op_iterator idx_begin() { return op_begin()+1; }
385 inline const_op_iterator idx_begin() const { return op_begin()+1; }
386 inline op_iterator idx_end() { return op_end(); }
387 inline const_op_iterator idx_end() const { return op_end(); }
388
389 Value *getPointerOperand() {
390 return getOperand(0);
391 }
392 const Value *getPointerOperand() const {
393 return getOperand(0);
394 }
395 static unsigned getPointerOperandIndex() {
396 return 0U; // get index for modifying correct operand
397 }
398
399 inline unsigned getNumIndices() const { // Note: always non-negative
400 return getNumOperands() - 1;
401 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000402
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000403 inline bool hasIndices() const {
404 return getNumOperands() > 1;
405 }
406
407 // Methods for support type inquiry through isa, cast, and dyn_cast:
408 static inline bool classof(const GetElementPtrInst *) { return true; }
409 static inline bool classof(const Instruction *I) {
410 return (I->getOpcode() == Instruction::GetElementPtr);
411 }
412 static inline bool classof(const Value *V) {
413 return isa<Instruction>(V) && classof(cast<Instruction>(V));
414 }
415};
416
417//===----------------------------------------------------------------------===//
418// SetCondInst Class
419//===----------------------------------------------------------------------===//
420
421/// SetCondInst class - Represent a setCC operator, where CC is eq, ne, lt, gt,
422/// le, or ge.
423///
424class SetCondInst : public BinaryOperator {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000425public:
426 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
Misha Brukman91102862005-03-16 03:46:55 +0000427 const std::string &Name = "", Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000428 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
Misha Brukman91102862005-03-16 03:46:55 +0000429 const std::string &Name, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000430
431 /// getInverseCondition - Return the inverse of the current condition opcode.
432 /// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
433 ///
434 BinaryOps getInverseCondition() const {
435 return getInverseCondition(getOpcode());
436 }
437
438 /// getInverseCondition - Static version that you can use without an
439 /// instruction available.
440 ///
441 static BinaryOps getInverseCondition(BinaryOps Opcode);
442
443 /// getSwappedCondition - Return the condition opcode that would be the result
444 /// of exchanging the two operands of the setcc instruction without changing
445 /// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
446 ///
447 BinaryOps getSwappedCondition() const {
448 return getSwappedCondition(getOpcode());
449 }
450
451 /// getSwappedCondition - Static version that you can use without an
452 /// instruction available.
453 ///
454 static BinaryOps getSwappedCondition(BinaryOps Opcode);
455
Chris Lattnera5b07402006-09-17 19:14:47 +0000456 /// isEquality - Return true if this comparison is an ==/!= comparison.
457 ///
458 bool isEquality() const {
459 return getOpcode() == SetEQ || getOpcode() == SetNE;
460 }
461
462 /// isRelational - Return true if this comparison is a </>/<=/>= comparison.
463 ///
464 bool isRelational() const {
465 return !isEquality();
466 }
467
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000468 // Methods for support type inquiry through isa, cast, and dyn_cast:
469 static inline bool classof(const SetCondInst *) { return true; }
470 static inline bool classof(const Instruction *I) {
471 return I->getOpcode() == SetEQ || I->getOpcode() == SetNE ||
472 I->getOpcode() == SetLE || I->getOpcode() == SetGE ||
473 I->getOpcode() == SetLT || I->getOpcode() == SetGT;
474 }
475 static inline bool classof(const Value *V) {
476 return isa<Instruction>(V) && classof(cast<Instruction>(V));
477 }
478};
479
480//===----------------------------------------------------------------------===//
481// CastInst Class
482//===----------------------------------------------------------------------===//
483
484/// CastInst - This class represents a cast from Operand[0] to the type of
485/// the instruction (i->getType()).
486///
Chris Lattner454928e2005-01-29 00:31:36 +0000487class CastInst : public UnaryInstruction {
Misha Brukman9769ab22005-04-21 20:19:05 +0000488 CastInst(const CastInst &CI)
Chris Lattner454928e2005-01-29 00:31:36 +0000489 : UnaryInstruction(CI.getType(), Cast, CI.getOperand(0)) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000490 }
491public:
492 CastInst(Value *S, const Type *Ty, const std::string &Name = "",
493 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000494 : UnaryInstruction(Ty, Cast, S, Name, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000495 }
496 CastInst(Value *S, const Type *Ty, const std::string &Name,
497 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000498 : UnaryInstruction(Ty, Cast, S, Name, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000499 }
500
Chris Lattner79bc3322006-09-18 04:54:57 +0000501 /// isTruncIntCast - Return true if this is a truncating integer cast
502 /// instruction, e.g. a cast from long to uint.
503 bool isTruncIntCast() const;
504
505
Chris Lattnerf319e832004-10-15 23:52:05 +0000506 virtual CastInst *clone() const;
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 CastInst *) { return true; }
510 static inline bool classof(const Instruction *I) {
511 return I->getOpcode() == Cast;
512 }
513 static inline bool classof(const Value *V) {
514 return isa<Instruction>(V) && classof(cast<Instruction>(V));
515 }
516};
517
518
519//===----------------------------------------------------------------------===//
520// CallInst Class
521//===----------------------------------------------------------------------===//
522
523/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +0000524/// machine's calling convention. This class uses low bit of the SubClassData
525/// field to indicate whether or not this is a tail call. The rest of the bits
526/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000527///
528class CallInst : public Instruction {
529 CallInst(const CallInst &CI);
530 void init(Value *Func, const std::vector<Value*> &Params);
531 void init(Value *Func, Value *Actual1, Value *Actual2);
532 void init(Value *Func, Value *Actual);
533 void init(Value *Func);
534
535public:
536 CallInst(Value *F, const std::vector<Value*> &Par,
537 const std::string &Name = "", Instruction *InsertBefore = 0);
538 CallInst(Value *F, const std::vector<Value*> &Par,
539 const std::string &Name, BasicBlock *InsertAtEnd);
540
541 // Alternate CallInst ctors w/ two actuals, w/ one actual and no
542 // actuals, respectively.
543 CallInst(Value *F, Value *Actual1, Value *Actual2,
544 const std::string& Name = "", Instruction *InsertBefore = 0);
545 CallInst(Value *F, Value *Actual1, Value *Actual2,
546 const std::string& Name, BasicBlock *InsertAtEnd);
547 CallInst(Value *F, Value *Actual, const std::string& Name = "",
548 Instruction *InsertBefore = 0);
549 CallInst(Value *F, Value *Actual, const std::string& Name,
550 BasicBlock *InsertAtEnd);
Misha Brukman9769ab22005-04-21 20:19:05 +0000551 explicit CallInst(Value *F, const std::string &Name = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000552 Instruction *InsertBefore = 0);
Misha Brukman9769ab22005-04-21 20:19:05 +0000553 explicit CallInst(Value *F, const std::string &Name,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000554 BasicBlock *InsertAtEnd);
Chris Lattner454928e2005-01-29 00:31:36 +0000555 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000556
Chris Lattnerf319e832004-10-15 23:52:05 +0000557 virtual CallInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000558 bool mayWriteToMemory() const { return true; }
559
Chris Lattner3340ffe2005-05-06 20:26:26 +0000560 bool isTailCall() const { return SubclassData & 1; }
561 void setTailCall(bool isTailCall = true) {
Jeff Cohen39cef602005-05-07 02:44:04 +0000562 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
Chris Lattner3340ffe2005-05-06 20:26:26 +0000563 }
564
565 /// getCallingConv/setCallingConv - Get or set the calling convention of this
566 /// function call.
567 unsigned getCallingConv() const { return SubclassData >> 1; }
568 void setCallingConv(unsigned CC) {
569 SubclassData = (SubclassData & 1) | (CC << 1);
570 }
Chris Lattnerddb6db42005-05-06 05:51:46 +0000571
Chris Lattner721aef62004-11-18 17:46:57 +0000572 /// getCalledFunction - Return the function being called by this instruction
573 /// if it is a direct call. If it is a call through a function pointer,
574 /// return null.
575 Function *getCalledFunction() const {
Reid Spenceredd5d9e2005-05-15 16:13:11 +0000576 return static_cast<Function*>(dyn_cast<Function>(getOperand(0)));
Chris Lattner721aef62004-11-18 17:46:57 +0000577 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000578
579 // getCalledValue - Get a pointer to a method that is invoked by this inst.
Chris Lattner454928e2005-01-29 00:31:36 +0000580 inline const Value *getCalledValue() const { return getOperand(0); }
581 inline Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000582
583 // Methods for support type inquiry through isa, cast, and dyn_cast:
584 static inline bool classof(const CallInst *) { return true; }
585 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +0000586 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000587 }
588 static inline bool classof(const Value *V) {
589 return isa<Instruction>(V) && classof(cast<Instruction>(V));
590 }
591};
592
593
594//===----------------------------------------------------------------------===//
595// ShiftInst Class
596//===----------------------------------------------------------------------===//
597
598/// ShiftInst - This class represents left and right shift instructions.
599///
600class ShiftInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000601 Use Ops[2];
602 ShiftInst(const ShiftInst &SI)
603 : Instruction(SI.getType(), SI.getOpcode(), Ops, 2) {
604 Ops[0].init(SI.Ops[0], this);
605 Ops[1].init(SI.Ops[1], this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000606 }
607 void init(OtherOps Opcode, Value *S, Value *SA) {
608 assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
Chris Lattner454928e2005-01-29 00:31:36 +0000609 Ops[0].init(S, this);
610 Ops[1].init(SA, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000611 }
612
613public:
614 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
615 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000616 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000617 init(Opcode, S, SA);
618 }
619 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name,
620 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000621 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000622 init(Opcode, S, SA);
623 }
624
625 OtherOps getOpcode() const {
626 return static_cast<OtherOps>(Instruction::getOpcode());
627 }
628
Chris Lattner454928e2005-01-29 00:31:36 +0000629 /// Transparently provide more efficient getOperand methods.
630 Value *getOperand(unsigned i) const {
631 assert(i < 2 && "getOperand() out of range!");
632 return Ops[i];
633 }
634 void setOperand(unsigned i, Value *Val) {
635 assert(i < 2 && "setOperand() out of range!");
636 Ops[i] = Val;
637 }
638 unsigned getNumOperands() const { return 2; }
639
Chris Lattner2f463862006-09-17 19:29:56 +0000640 /// isLogicalShift - Return true if this is a logical shift left or a logical
641 /// shift right.
642 bool isLogicalShift() const;
643
644 /// isArithmeticShift - Return true if this is a sign-extending shift right
645 /// operation.
646 bool isArithmeticShift() const {
647 return !isLogicalShift();
648 }
649
650
Chris Lattnerf319e832004-10-15 23:52:05 +0000651 virtual ShiftInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000652
653 // Methods for support type inquiry through isa, cast, and dyn_cast:
654 static inline bool classof(const ShiftInst *) { return true; }
655 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +0000656 return (I->getOpcode() == Instruction::Shr) |
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000657 (I->getOpcode() == Instruction::Shl);
658 }
659 static inline bool classof(const Value *V) {
660 return isa<Instruction>(V) && classof(cast<Instruction>(V));
661 }
662};
663
664//===----------------------------------------------------------------------===//
665// SelectInst Class
666//===----------------------------------------------------------------------===//
667
668/// SelectInst - This class represents the LLVM 'select' instruction.
669///
670class SelectInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000671 Use Ops[3];
672
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000673 void init(Value *C, Value *S1, Value *S2) {
Chris Lattner454928e2005-01-29 00:31:36 +0000674 Ops[0].init(C, this);
675 Ops[1].init(S1, this);
676 Ops[2].init(S2, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000677 }
678
Chris Lattner454928e2005-01-29 00:31:36 +0000679 SelectInst(const SelectInst &SI)
680 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
681 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
682 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000683public:
684 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
685 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000686 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
687 Name, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000688 init(C, S1, S2);
689 }
690 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
691 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000692 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
693 Name, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000694 init(C, S1, S2);
695 }
696
Chris Lattner454928e2005-01-29 00:31:36 +0000697 Value *getCondition() const { return Ops[0]; }
698 Value *getTrueValue() const { return Ops[1]; }
699 Value *getFalseValue() const { return Ops[2]; }
700
701 /// Transparently provide more efficient getOperand methods.
702 Value *getOperand(unsigned i) const {
703 assert(i < 3 && "getOperand() out of range!");
704 return Ops[i];
705 }
706 void setOperand(unsigned i, Value *Val) {
707 assert(i < 3 && "setOperand() out of range!");
708 Ops[i] = Val;
709 }
710 unsigned getNumOperands() const { return 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000711
712 OtherOps getOpcode() const {
713 return static_cast<OtherOps>(Instruction::getOpcode());
714 }
715
Chris Lattnerf319e832004-10-15 23:52:05 +0000716 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000717
718 // Methods for support type inquiry through isa, cast, and dyn_cast:
719 static inline bool classof(const SelectInst *) { return true; }
720 static inline bool classof(const Instruction *I) {
721 return I->getOpcode() == Instruction::Select;
722 }
723 static inline bool classof(const Value *V) {
724 return isa<Instruction>(V) && classof(cast<Instruction>(V));
725 }
726};
727
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000728//===----------------------------------------------------------------------===//
729// VAArgInst Class
730//===----------------------------------------------------------------------===//
731
732/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +0000733/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000734///
Chris Lattner454928e2005-01-29 00:31:36 +0000735class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000736 VAArgInst(const VAArgInst &VAA)
Chris Lattner454928e2005-01-29 00:31:36 +0000737 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000738public:
739 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
740 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000741 : UnaryInstruction(Ty, VAArg, List, Name, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000742 }
743 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
744 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000745 : UnaryInstruction(Ty, VAArg, List, Name, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000746 }
747
Chris Lattnerf319e832004-10-15 23:52:05 +0000748 virtual VAArgInst *clone() const;
Andrew Lenharthc64b64a2005-06-19 14:46:20 +0000749 bool mayWriteToMemory() const { return true; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000750
751 // Methods for support type inquiry through isa, cast, and dyn_cast:
752 static inline bool classof(const VAArgInst *) { return true; }
753 static inline bool classof(const Instruction *I) {
754 return I->getOpcode() == VAArg;
755 }
756 static inline bool classof(const Value *V) {
757 return isa<Instruction>(V) && classof(cast<Instruction>(V));
758 }
759};
760
761//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +0000762// ExtractElementInst Class
763//===----------------------------------------------------------------------===//
764
765/// ExtractElementInst - This instruction extracts a single (scalar)
766/// element from a PackedType value
767///
768class ExtractElementInst : public Instruction {
769 Use Ops[2];
Robert Bocchinof9993442006-01-17 20:05:59 +0000770 ExtractElementInst(const ExtractElementInst &EE) :
771 Instruction(EE.getType(), ExtractElement, Ops, 2) {
772 Ops[0].init(EE.Ops[0], this);
773 Ops[1].init(EE.Ops[1], this);
Robert Bocchino49b78a52006-01-10 19:04:13 +0000774 }
775
776public:
Chris Lattner9fc18d22006-04-08 01:15:18 +0000777 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
778 Instruction *InsertBefore = 0);
779 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
780 BasicBlock *InsertAtEnd);
Robert Bocchino49b78a52006-01-10 19:04:13 +0000781
Chris Lattnerfa495842006-04-08 04:04:54 +0000782 /// isValidOperands - Return true if an extractelement instruction can be
783 /// formed with the specified operands.
784 static bool isValidOperands(const Value *Vec, const Value *Idx);
785
Robert Bocchino49b78a52006-01-10 19:04:13 +0000786 virtual ExtractElementInst *clone() const;
787
788 virtual bool mayWriteToMemory() const { return false; }
789
790 /// Transparently provide more efficient getOperand methods.
791 Value *getOperand(unsigned i) const {
792 assert(i < 2 && "getOperand() out of range!");
793 return Ops[i];
794 }
795 void setOperand(unsigned i, Value *Val) {
796 assert(i < 2 && "setOperand() out of range!");
797 Ops[i] = Val;
798 }
799 unsigned getNumOperands() const { return 2; }
800
801 // Methods for support type inquiry through isa, cast, and dyn_cast:
802 static inline bool classof(const ExtractElementInst *) { return true; }
803 static inline bool classof(const Instruction *I) {
804 return I->getOpcode() == Instruction::ExtractElement;
805 }
806 static inline bool classof(const Value *V) {
807 return isa<Instruction>(V) && classof(cast<Instruction>(V));
808 }
809};
810
811//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +0000812// InsertElementInst Class
813//===----------------------------------------------------------------------===//
814
815/// InsertElementInst - This instruction inserts a single (scalar)
816/// element into a PackedType value
817///
818class InsertElementInst : public Instruction {
819 Use Ops[3];
Chris Lattner6a56ed42006-04-14 22:20:07 +0000820 InsertElementInst(const InsertElementInst &IE);
Robert Bocchinof9993442006-01-17 20:05:59 +0000821public:
Chris Lattner9fc18d22006-04-08 01:15:18 +0000822 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
823 const std::string &Name = "",Instruction *InsertBefore = 0);
824 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Robert Bocchinof9993442006-01-17 20:05:59 +0000825 const std::string &Name, BasicBlock *InsertAtEnd);
826
Chris Lattnerfa495842006-04-08 04:04:54 +0000827 /// isValidOperands - Return true if an insertelement instruction can be
828 /// formed with the specified operands.
829 static bool isValidOperands(const Value *Vec, const Value *NewElt,
830 const Value *Idx);
831
Robert Bocchinof9993442006-01-17 20:05:59 +0000832 virtual InsertElementInst *clone() const;
833
834 virtual bool mayWriteToMemory() const { return false; }
835
Chris Lattner6a56ed42006-04-14 22:20:07 +0000836 /// getType - Overload to return most specific packed type.
837 ///
838 inline const PackedType *getType() const {
839 return reinterpret_cast<const PackedType*>(Instruction::getType());
840 }
841
Robert Bocchinof9993442006-01-17 20:05:59 +0000842 /// Transparently provide more efficient getOperand methods.
843 Value *getOperand(unsigned i) const {
844 assert(i < 3 && "getOperand() out of range!");
845 return Ops[i];
846 }
847 void setOperand(unsigned i, Value *Val) {
848 assert(i < 3 && "setOperand() out of range!");
849 Ops[i] = Val;
850 }
851 unsigned getNumOperands() const { return 3; }
852
853 // Methods for support type inquiry through isa, cast, and dyn_cast:
854 static inline bool classof(const InsertElementInst *) { return true; }
855 static inline bool classof(const Instruction *I) {
856 return I->getOpcode() == Instruction::InsertElement;
857 }
858 static inline bool classof(const Value *V) {
859 return isa<Instruction>(V) && classof(cast<Instruction>(V));
860 }
861};
862
863//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +0000864// ShuffleVectorInst Class
865//===----------------------------------------------------------------------===//
866
867/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
868/// input vectors.
869///
870class ShuffleVectorInst : public Instruction {
871 Use Ops[3];
Chris Lattner6a56ed42006-04-14 22:20:07 +0000872 ShuffleVectorInst(const ShuffleVectorInst &IE);
Chris Lattner9fc18d22006-04-08 01:15:18 +0000873public:
874 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
875 const std::string &Name = "", Instruction *InsertBefor = 0);
876 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
877 const std::string &Name, BasicBlock *InsertAtEnd);
878
Chris Lattnerfa495842006-04-08 04:04:54 +0000879 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +0000880 /// formed with the specified operands.
881 static bool isValidOperands(const Value *V1, const Value *V2,
882 const Value *Mask);
883
884 virtual ShuffleVectorInst *clone() const;
885
886 virtual bool mayWriteToMemory() const { return false; }
887
Chris Lattner6a56ed42006-04-14 22:20:07 +0000888 /// getType - Overload to return most specific packed type.
889 ///
890 inline const PackedType *getType() const {
891 return reinterpret_cast<const PackedType*>(Instruction::getType());
892 }
893
Chris Lattner9fc18d22006-04-08 01:15:18 +0000894 /// Transparently provide more efficient getOperand methods.
895 Value *getOperand(unsigned i) const {
896 assert(i < 3 && "getOperand() out of range!");
897 return Ops[i];
898 }
899 void setOperand(unsigned i, Value *Val) {
900 assert(i < 3 && "setOperand() out of range!");
901 Ops[i] = Val;
902 }
903 unsigned getNumOperands() const { return 3; }
904
905 // Methods for support type inquiry through isa, cast, and dyn_cast:
906 static inline bool classof(const ShuffleVectorInst *) { return true; }
907 static inline bool classof(const Instruction *I) {
908 return I->getOpcode() == Instruction::ShuffleVector;
909 }
910 static inline bool classof(const Value *V) {
911 return isa<Instruction>(V) && classof(cast<Instruction>(V));
912 }
913};
914
915
916//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000917// PHINode Class
918//===----------------------------------------------------------------------===//
919
920// PHINode - The PHINode class is used to represent the magical mystical PHI
921// node, that can not exist in nature, but can be synthesized in a computer
922// scientist's overactive imagination.
923//
924class PHINode : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000925 /// ReservedSpace - The number of operands actually allocated. NumOperands is
926 /// the number actually in use.
927 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000928 PHINode(const PHINode &PN);
929public:
930 PHINode(const Type *Ty, const std::string &Name = "",
931 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000932 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertBefore),
933 ReservedSpace(0) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000934 }
935
936 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000937 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertAtEnd),
938 ReservedSpace(0) {
939 }
940
941 ~PHINode();
942
943 /// reserveOperandSpace - This method can be used to avoid repeated
944 /// reallocation of PHI operand lists by reserving space for the correct
945 /// number of operands before adding them. Unlike normal vector reserves,
946 /// this method can also be used to trim the operand space.
947 void reserveOperandSpace(unsigned NumValues) {
948 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000949 }
950
Chris Lattnerf319e832004-10-15 23:52:05 +0000951 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000952
953 /// getNumIncomingValues - Return the number of incoming edges
954 ///
Chris Lattner454928e2005-01-29 00:31:36 +0000955 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000956
Reid Spencerc773de62006-05-19 19:07:54 +0000957 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000958 ///
959 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +0000960 assert(i*2 < getNumOperands() && "Invalid value number!");
961 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000962 }
963 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +0000964 assert(i*2 < getNumOperands() && "Invalid value number!");
965 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000966 }
Chris Lattner454928e2005-01-29 00:31:36 +0000967 unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000968 return i*2;
969 }
970
Reid Spencerc773de62006-05-19 19:07:54 +0000971 /// getIncomingBlock - Return incoming basic block number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000972 ///
Misha Brukman9769ab22005-04-21 20:19:05 +0000973 BasicBlock *getIncomingBlock(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +0000974 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000975 }
976 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Chris Lattner454928e2005-01-29 00:31:36 +0000977 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000978 }
979 unsigned getOperandNumForIncomingBlock(unsigned i) {
980 return i*2+1;
981 }
982
983 /// addIncoming - Add an incoming value to the end of the PHI list
984 ///
985 void addIncoming(Value *V, BasicBlock *BB) {
986 assert(getType() == V->getType() &&
987 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +0000988 unsigned OpNo = NumOperands;
989 if (OpNo+2 > ReservedSpace)
990 resizeOperands(0); // Get more space!
991 // Initialize some new operands.
992 NumOperands = OpNo+2;
993 OperandList[OpNo].init(V, this);
994 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000995 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000996
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000997 /// removeIncomingValue - Remove an incoming value. This is useful if a
998 /// predecessor basic block is deleted. The value removed is returned.
999 ///
1000 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1001 /// is true), the PHI node is destroyed and any uses of it are replaced with
1002 /// dummy values. The only time there should be zero incoming values to a PHI
1003 /// node is when the block is dead, so this strategy is sound.
1004 ///
1005 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1006
1007 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1008 int Idx = getBasicBlockIndex(BB);
1009 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1010 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1011 }
1012
Misha Brukman9769ab22005-04-21 20:19:05 +00001013 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001014 /// block in the value list for this PHI. Returns -1 if no instance.
1015 ///
1016 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001017 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00001018 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Chris Lattner454928e2005-01-29 00:31:36 +00001019 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001020 return -1;
1021 }
1022
1023 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1024 return getIncomingValue(getBasicBlockIndex(BB));
1025 }
1026
Nate Begemana83ba0f2005-08-04 23:24:19 +00001027 /// hasConstantValue - If the specified PHI node always merges together the
1028 /// same value, return the value, otherwise return null.
1029 ///
Chris Lattner9acbd612005-08-05 00:49:06 +00001030 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
Nate Begemana83ba0f2005-08-04 23:24:19 +00001031
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001032 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1033 static inline bool classof(const PHINode *) { return true; }
1034 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001035 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001036 }
1037 static inline bool classof(const Value *V) {
1038 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1039 }
Chris Lattner454928e2005-01-29 00:31:36 +00001040 private:
1041 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001042};
1043
1044//===----------------------------------------------------------------------===//
1045// ReturnInst Class
1046//===----------------------------------------------------------------------===//
1047
1048//===---------------------------------------------------------------------------
1049/// ReturnInst - Return a value (possibly void), from a function. Execution
1050/// does not continue in this function any longer.
1051///
1052class ReturnInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001053 Use RetVal; // Possibly null retval.
1054 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret, &RetVal,
1055 RI.getNumOperands()) {
1056 if (RI.getNumOperands())
1057 RetVal.init(RI.RetVal, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001058 }
1059
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001060 void init(Value *RetVal);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001061
1062public:
1063 // ReturnInst constructors:
1064 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001065 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001066 // ReturnInst(Value* X) - 'ret X' instruction
1067 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
1068 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1069 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
1070 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001071 //
1072 // NOTE: If the Value* passed is of type void then the constructor behaves as
1073 // if it was passed NULL.
Chris Lattner454928e2005-01-29 00:31:36 +00001074 ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0)
1075 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertBefore) {
1076 init(retVal);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001077 }
Chris Lattner454928e2005-01-29 00:31:36 +00001078 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
1079 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
1080 init(retVal);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001081 }
1082 ReturnInst(BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001083 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001084 }
1085
Chris Lattnerf319e832004-10-15 23:52:05 +00001086 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001087
Chris Lattner454928e2005-01-29 00:31:36 +00001088 // Transparently provide more efficient getOperand methods.
1089 Value *getOperand(unsigned i) const {
1090 assert(i < getNumOperands() && "getOperand() out of range!");
1091 return RetVal;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001092 }
Chris Lattner454928e2005-01-29 00:31:36 +00001093 void setOperand(unsigned i, Value *Val) {
1094 assert(i < getNumOperands() && "setOperand() out of range!");
1095 RetVal = Val;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001096 }
1097
Chris Lattner454928e2005-01-29 00:31:36 +00001098 Value *getReturnValue() const { return RetVal; }
1099
1100 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001101
1102 // Methods for support type inquiry through isa, cast, and dyn_cast:
1103 static inline bool classof(const ReturnInst *) { return true; }
1104 static inline bool classof(const Instruction *I) {
1105 return (I->getOpcode() == Instruction::Ret);
1106 }
1107 static inline bool classof(const Value *V) {
1108 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1109 }
Chris Lattner454928e2005-01-29 00:31:36 +00001110 private:
1111 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1112 virtual unsigned getNumSuccessorsV() const;
1113 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001114};
1115
1116//===----------------------------------------------------------------------===//
1117// BranchInst Class
1118//===----------------------------------------------------------------------===//
1119
1120//===---------------------------------------------------------------------------
1121/// BranchInst - Conditional or Unconditional Branch instruction.
1122///
1123class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001124 /// Ops list - Branches are strange. The operands are ordered:
1125 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
1126 /// they don't have to check for cond/uncond branchness.
1127 Use Ops[3];
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001128 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00001129 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001130public:
1131 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1132 // BranchInst(BB *B) - 'br B'
1133 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1134 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1135 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1136 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1137 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
1138 BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001139 : TerminatorInst(Instruction::Br, Ops, 1, InsertBefore) {
1140 assert(IfTrue != 0 && "Branch destination may not be null!");
1141 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001142 }
1143 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1144 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001145 : TerminatorInst(Instruction::Br, Ops, 3, InsertBefore) {
1146 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1147 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
1148 Ops[2].init(Cond, this);
1149#ifndef NDEBUG
1150 AssertOK();
1151#endif
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001152 }
1153
1154 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001155 : TerminatorInst(Instruction::Br, Ops, 1, InsertAtEnd) {
1156 assert(IfTrue != 0 && "Branch destination may not be null!");
1157 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001158 }
1159
1160 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1161 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001162 : TerminatorInst(Instruction::Br, Ops, 3, InsertAtEnd) {
1163 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1164 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
1165 Ops[2].init(Cond, this);
1166#ifndef NDEBUG
1167 AssertOK();
1168#endif
1169 }
1170
1171
1172 /// Transparently provide more efficient getOperand methods.
1173 Value *getOperand(unsigned i) const {
1174 assert(i < getNumOperands() && "getOperand() out of range!");
1175 return Ops[i];
1176 }
1177 void setOperand(unsigned i, Value *Val) {
1178 assert(i < getNumOperands() && "setOperand() out of range!");
1179 Ops[i] = Val;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001180 }
1181
Chris Lattnerf319e832004-10-15 23:52:05 +00001182 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001183
Chris Lattner454928e2005-01-29 00:31:36 +00001184 inline bool isUnconditional() const { return getNumOperands() == 1; }
1185 inline bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001186
1187 inline Value *getCondition() const {
1188 assert(isConditional() && "Cannot get condition of an uncond branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001189 return getOperand(2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001190 }
1191
1192 void setCondition(Value *V) {
1193 assert(isConditional() && "Cannot set condition of unconditional branch!");
1194 setOperand(2, V);
1195 }
1196
1197 // setUnconditionalDest - Change the current branch to an unconditional branch
1198 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +00001199 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001200 void setUnconditionalDest(BasicBlock *Dest) {
Chris Lattner454928e2005-01-29 00:31:36 +00001201 if (isConditional()) { // Convert this to an uncond branch.
1202 NumOperands = 1;
1203 Ops[1].set(0);
1204 Ops[2].set(0);
1205 }
1206 setOperand(0, reinterpret_cast<Value*>(Dest));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001207 }
1208
Chris Lattner454928e2005-01-29 00:31:36 +00001209 unsigned getNumSuccessors() const { return 1+isConditional(); }
1210
1211 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001212 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Misha Brukman9769ab22005-04-21 20:19:05 +00001213 return (i == 0) ? cast<BasicBlock>(getOperand(0)) :
Chris Lattner454928e2005-01-29 00:31:36 +00001214 cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001215 }
1216
Chris Lattner454928e2005-01-29 00:31:36 +00001217 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001218 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001219 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001220 }
1221
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001222 // Methods for support type inquiry through isa, cast, and dyn_cast:
1223 static inline bool classof(const BranchInst *) { return true; }
1224 static inline bool classof(const Instruction *I) {
1225 return (I->getOpcode() == Instruction::Br);
1226 }
1227 static inline bool classof(const Value *V) {
1228 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1229 }
Chris Lattner454928e2005-01-29 00:31:36 +00001230private:
1231 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1232 virtual unsigned getNumSuccessorsV() const;
1233 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001234};
1235
1236//===----------------------------------------------------------------------===//
1237// SwitchInst Class
1238//===----------------------------------------------------------------------===//
1239
1240//===---------------------------------------------------------------------------
1241/// SwitchInst - Multiway switch
1242///
1243class SwitchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001244 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001245 // Operand[0] = Value to switch on
1246 // Operand[1] = Default basic block destination
1247 // Operand[2n ] = Value to match
1248 // Operand[2n+1] = BasicBlock to go to on match
1249 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00001250 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1251 void resizeOperands(unsigned No);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001252public:
Chris Lattner454928e2005-01-29 00:31:36 +00001253 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1254 /// switch on and a default destination. The number of additional cases can
1255 /// be specified here to make memory allocation more efficient. This
1256 /// constructor can also autoinsert before another instruction.
1257 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Misha Brukman9769ab22005-04-21 20:19:05 +00001258 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001259 : TerminatorInst(Instruction::Switch, 0, 0, InsertBefore) {
1260 init(Value, Default, NumCases);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001261 }
1262
Chris Lattner454928e2005-01-29 00:31:36 +00001263 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1264 /// switch on and a default destination. The number of additional cases can
1265 /// be specified here to make memory allocation more efficient. This
1266 /// constructor also autoinserts at the end of the specified BasicBlock.
1267 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Misha Brukman9769ab22005-04-21 20:19:05 +00001268 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001269 : TerminatorInst(Instruction::Switch, 0, 0, InsertAtEnd) {
1270 init(Value, Default, NumCases);
1271 }
1272 ~SwitchInst();
1273
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001274
1275 // Accessor Methods for Switch stmt
Chris Lattner454928e2005-01-29 00:31:36 +00001276 inline Value *getCondition() const { return getOperand(0); }
1277 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00001278
Chris Lattner454928e2005-01-29 00:31:36 +00001279 inline BasicBlock *getDefaultDest() const {
1280 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001281 }
1282
1283 /// getNumCases - return the number of 'cases' in this switch instruction.
1284 /// Note that case #0 is always the default case.
1285 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001286 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001287 }
1288
1289 /// getCaseValue - Return the specified case value. Note that case #0, the
1290 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001291 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001292 assert(i && i < getNumCases() && "Illegal case value to get!");
1293 return getSuccessorValue(i);
1294 }
1295
1296 /// getCaseValue - Return the specified case value. Note that case #0, the
1297 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001298 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001299 assert(i && i < getNumCases() && "Illegal case value to get!");
1300 return getSuccessorValue(i);
1301 }
1302
1303 /// findCaseValue - Search all of the case values for the specified constant.
1304 /// If it is explicitly handled, return the case number of it, otherwise
1305 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001306 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001307 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1308 if (getCaseValue(i) == C)
1309 return i;
1310 return 0;
1311 }
1312
Nick Lewycky011f1842006-09-18 19:03:59 +00001313 /// findCaseDest - Finds the unique case value for a given successor. Returns
1314 /// null if the successor is not found, not unique, or is the default case.
1315 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00001316 if (BB == getDefaultDest()) return NULL;
1317
Nick Lewycky011f1842006-09-18 19:03:59 +00001318 ConstantInt *CI = NULL;
1319 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
1320 if (getSuccessor(i) == BB) {
1321 if (CI) return NULL; // Multiple cases lead to BB.
1322 else CI = getCaseValue(i);
1323 }
1324 }
1325 return CI;
1326 }
1327
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001328 /// addCase - Add an entry to the switch instruction...
1329 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00001330 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001331
1332 /// removeCase - This method removes the specified successor from the switch
1333 /// instruction. Note that this cannot be used to remove the default
1334 /// destination (successor #0).
1335 ///
1336 void removeCase(unsigned idx);
1337
Chris Lattner454928e2005-01-29 00:31:36 +00001338 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001339
Chris Lattner454928e2005-01-29 00:31:36 +00001340 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1341 BasicBlock *getSuccessor(unsigned idx) const {
1342 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1343 return cast<BasicBlock>(getOperand(idx*2+1));
1344 }
1345 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001346 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001347 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001348 }
1349
1350 // getSuccessorValue - Return the value associated with the specified
1351 // successor.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001352 inline ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001353 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00001354 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001355 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001356
1357 // Methods for support type inquiry through isa, cast, and dyn_cast:
1358 static inline bool classof(const SwitchInst *) { return true; }
1359 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00001360 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001361 }
1362 static inline bool classof(const Value *V) {
1363 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1364 }
Chris Lattner454928e2005-01-29 00:31:36 +00001365private:
1366 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1367 virtual unsigned getNumSuccessorsV() const;
1368 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001369};
1370
1371//===----------------------------------------------------------------------===//
1372// InvokeInst Class
1373//===----------------------------------------------------------------------===//
1374
1375//===---------------------------------------------------------------------------
Chris Lattner3340ffe2005-05-06 20:26:26 +00001376
1377/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
1378/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001379///
1380class InvokeInst : public TerminatorInst {
1381 InvokeInst(const InvokeInst &BI);
1382 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1383 const std::vector<Value*> &Params);
1384public:
1385 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Misha Brukman91102862005-03-16 03:46:55 +00001386 const std::vector<Value*> &Params, const std::string &Name = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001387 Instruction *InsertBefore = 0);
1388 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Misha Brukman91102862005-03-16 03:46:55 +00001389 const std::vector<Value*> &Params, const std::string &Name,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001390 BasicBlock *InsertAtEnd);
Chris Lattner454928e2005-01-29 00:31:36 +00001391 ~InvokeInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001392
Chris Lattnerf319e832004-10-15 23:52:05 +00001393 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001394
1395 bool mayWriteToMemory() const { return true; }
1396
Chris Lattner3340ffe2005-05-06 20:26:26 +00001397 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1398 /// function call.
1399 unsigned getCallingConv() const { return SubclassData; }
1400 void setCallingConv(unsigned CC) {
1401 SubclassData = CC;
1402 }
1403
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001404 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00001405 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001406 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001407 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001408 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001409 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001410
1411 // getCalledValue - Get a pointer to a function that is invoked by this inst.
Chris Lattner454928e2005-01-29 00:31:36 +00001412 inline Value *getCalledValue() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001413
1414 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00001415 BasicBlock *getNormalDest() const {
1416 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001417 }
Chris Lattner454928e2005-01-29 00:31:36 +00001418 BasicBlock *getUnwindDest() const {
1419 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001420 }
Chris Lattner454928e2005-01-29 00:31:36 +00001421 void setNormalDest(BasicBlock *B) {
1422 setOperand(1, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001423 }
1424
Chris Lattner454928e2005-01-29 00:31:36 +00001425 void setUnwindDest(BasicBlock *B) {
1426 setOperand(2, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001427 }
1428
Chris Lattner454928e2005-01-29 00:31:36 +00001429 inline BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001430 assert(i < 2 && "Successor # out of range for invoke!");
1431 return i == 0 ? getNormalDest() : getUnwindDest();
1432 }
1433
Chris Lattner454928e2005-01-29 00:31:36 +00001434 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001435 assert(idx < 2 && "Successor # out of range for invoke!");
Chris Lattner454928e2005-01-29 00:31:36 +00001436 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001437 }
1438
Chris Lattner454928e2005-01-29 00:31:36 +00001439 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001440
1441 // Methods for support type inquiry through isa, cast, and dyn_cast:
1442 static inline bool classof(const InvokeInst *) { return true; }
1443 static inline bool classof(const Instruction *I) {
1444 return (I->getOpcode() == Instruction::Invoke);
1445 }
1446 static inline bool classof(const Value *V) {
1447 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1448 }
Chris Lattner454928e2005-01-29 00:31:36 +00001449private:
1450 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1451 virtual unsigned getNumSuccessorsV() const;
1452 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001453};
1454
1455
1456//===----------------------------------------------------------------------===//
1457// UnwindInst Class
1458//===----------------------------------------------------------------------===//
1459
1460//===---------------------------------------------------------------------------
1461/// UnwindInst - Immediately exit the current function, unwinding the stack
1462/// until an invoke instruction is found.
1463///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00001464class UnwindInst : public TerminatorInst {
1465public:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001466 UnwindInst(Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001467 : TerminatorInst(Instruction::Unwind, 0, 0, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001468 }
1469 UnwindInst(BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001470 : TerminatorInst(Instruction::Unwind, 0, 0, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001471 }
1472
Chris Lattnerf319e832004-10-15 23:52:05 +00001473 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001474
Chris Lattner454928e2005-01-29 00:31:36 +00001475 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001476
1477 // Methods for support type inquiry through isa, cast, and dyn_cast:
1478 static inline bool classof(const UnwindInst *) { return true; }
1479 static inline bool classof(const Instruction *I) {
1480 return I->getOpcode() == Instruction::Unwind;
1481 }
1482 static inline bool classof(const Value *V) {
1483 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1484 }
Chris Lattner454928e2005-01-29 00:31:36 +00001485private:
1486 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1487 virtual unsigned getNumSuccessorsV() const;
1488 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001489};
1490
Chris Lattner076b3f12004-10-16 18:05:54 +00001491//===----------------------------------------------------------------------===//
1492// UnreachableInst Class
1493//===----------------------------------------------------------------------===//
1494
1495//===---------------------------------------------------------------------------
1496/// UnreachableInst - This function has undefined behavior. In particular, the
1497/// presence of this instruction indicates some higher level knowledge that the
1498/// end of the block cannot be reached.
1499///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00001500class UnreachableInst : public TerminatorInst {
1501public:
Chris Lattner076b3f12004-10-16 18:05:54 +00001502 UnreachableInst(Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001503 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertBefore) {
Chris Lattner076b3f12004-10-16 18:05:54 +00001504 }
1505 UnreachableInst(BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001506 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertAtEnd) {
Chris Lattner076b3f12004-10-16 18:05:54 +00001507 }
1508
1509 virtual UnreachableInst *clone() const;
1510
Chris Lattner454928e2005-01-29 00:31:36 +00001511 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00001512
1513 // Methods for support type inquiry through isa, cast, and dyn_cast:
1514 static inline bool classof(const UnreachableInst *) { return true; }
1515 static inline bool classof(const Instruction *I) {
1516 return I->getOpcode() == Instruction::Unreachable;
1517 }
1518 static inline bool classof(const Value *V) {
1519 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1520 }
Chris Lattner454928e2005-01-29 00:31:36 +00001521private:
1522 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1523 virtual unsigned getNumSuccessorsV() const;
1524 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00001525};
1526
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001527} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00001528
1529#endif