blob: 1c3ca0f614507a018dd2603701ff48d187e5184f [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"
Chris Lattner36f78c82007-02-12 05:00:35 +000020#include <vector> // fixme remove.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000021
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;
Chris Lattner6a56ed42006-04-14 22:20:07 +000027class PackedType;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000028
29//===----------------------------------------------------------------------===//
30// AllocationInst Class
31//===----------------------------------------------------------------------===//
32
33/// AllocationInst - This class is the common base class of MallocInst and
34/// AllocaInst.
35///
Chris Lattner454928e2005-01-29 00:31:36 +000036class AllocationInst : public UnaryInstruction {
Nate Begeman14b05292005-11-05 09:21:28 +000037 unsigned Alignment;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000038protected:
Nate Begeman14b05292005-11-05 09:21:28 +000039 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
Misha Brukman91102862005-03-16 03:46:55 +000040 const std::string &Name = "", Instruction *InsertBefore = 0);
Nate Begeman14b05292005-11-05 09:21:28 +000041 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
Misha Brukman91102862005-03-16 03:46:55 +000042 const std::string &Name, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000043public:
Chris Lattner70aa33e2006-06-21 16:53:47 +000044 // Out of line virtual method, so the vtable, etc has a home.
45 virtual ~AllocationInst();
Chris Lattnerf56a8db2006-10-03 17:09:12 +000046
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000047 /// isArrayAllocation - Return true if there is an allocation size parameter
48 /// to the allocation instruction that is not 1.
49 ///
50 bool isArrayAllocation() const;
51
52 /// getArraySize - Get the number of element allocated, for a simple
53 /// allocation of a single element, this will return a constant 1 value.
54 ///
Chris Lattner454928e2005-01-29 00:31:36 +000055 inline const Value *getArraySize() const { return getOperand(0); }
56 inline Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000057
58 /// getType - Overload to return most specific pointer type
59 ///
60 inline const PointerType *getType() const {
Misha Brukman9769ab22005-04-21 20:19:05 +000061 return reinterpret_cast<const PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000062 }
63
64 /// getAllocatedType - Return the type that is being allocated by the
65 /// instruction.
66 ///
67 const Type *getAllocatedType() const;
68
Nate Begeman14b05292005-11-05 09:21:28 +000069 /// getAlignment - Return the alignment of the memory that is being allocated
70 /// by the instruction.
71 ///
72 unsigned getAlignment() const { return Alignment; }
Chris Lattner8ae779d2005-11-05 21:58:30 +000073 void setAlignment(unsigned Align) {
74 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
75 Alignment = Align;
76 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +000077
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000078 virtual Instruction *clone() const = 0;
79
80 // Methods for support type inquiry through isa, cast, and dyn_cast:
81 static inline bool classof(const AllocationInst *) { return true; }
82 static inline bool classof(const Instruction *I) {
83 return I->getOpcode() == Instruction::Alloca ||
84 I->getOpcode() == Instruction::Malloc;
85 }
86 static inline bool classof(const Value *V) {
87 return isa<Instruction>(V) && classof(cast<Instruction>(V));
88 }
89};
90
91
92//===----------------------------------------------------------------------===//
93// MallocInst Class
94//===----------------------------------------------------------------------===//
95
96/// MallocInst - an instruction to allocated memory on the heap
97///
98class MallocInst : public AllocationInst {
99 MallocInst(const MallocInst &MI);
100public:
101 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
102 const std::string &Name = "",
103 Instruction *InsertBefore = 0)
Nate Begeman14b05292005-11-05 09:21:28 +0000104 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000105 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
106 BasicBlock *InsertAtEnd)
Nate Begeman14b05292005-11-05 09:21:28 +0000107 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000108
109 MallocInst(const Type *Ty, const std::string &Name,
110 Instruction *InsertBefore = 0)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000111 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
112 MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
113 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000114
115 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Nate Begeman14b05292005-11-05 09:21:28 +0000116 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000117 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
118 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Nate Begeman14b05292005-11-05 09:21:28 +0000119 const std::string &Name = "",
120 Instruction *InsertBefore = 0)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000121 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000122
Chris Lattnerf319e832004-10-15 23:52:05 +0000123 virtual MallocInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000124
125 // Methods for support type inquiry through isa, cast, and dyn_cast:
126 static inline bool classof(const MallocInst *) { return true; }
127 static inline bool classof(const Instruction *I) {
128 return (I->getOpcode() == Instruction::Malloc);
129 }
130 static inline bool classof(const Value *V) {
131 return isa<Instruction>(V) && classof(cast<Instruction>(V));
132 }
133};
134
135
136//===----------------------------------------------------------------------===//
137// AllocaInst Class
138//===----------------------------------------------------------------------===//
139
140/// AllocaInst - an instruction to allocate memory on the stack
141///
142class AllocaInst : public AllocationInst {
143 AllocaInst(const AllocaInst &);
144public:
145 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
146 const std::string &Name = "",
147 Instruction *InsertBefore = 0)
Nate Begeman14b05292005-11-05 09:21:28 +0000148 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000149 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
150 BasicBlock *InsertAtEnd)
Nate Begeman14b05292005-11-05 09:21:28 +0000151 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
Chris Lattnerb77780e2006-05-10 04:38:35 +0000152
153 AllocaInst(const Type *Ty, const std::string &Name,
154 Instruction *InsertBefore = 0)
155 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
156 AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
157 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000158
Chris Lattnerb77780e2006-05-10 04:38:35 +0000159 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
160 const std::string &Name = "", Instruction *InsertBefore = 0)
161 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
Nate Begeman14b05292005-11-05 09:21:28 +0000162 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
163 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000164 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000165
Chris Lattnerf319e832004-10-15 23:52:05 +0000166 virtual AllocaInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000167
168 // Methods for support type inquiry through isa, cast, and dyn_cast:
169 static inline bool classof(const AllocaInst *) { return true; }
170 static inline bool classof(const Instruction *I) {
171 return (I->getOpcode() == Instruction::Alloca);
172 }
173 static inline bool classof(const Value *V) {
174 return isa<Instruction>(V) && classof(cast<Instruction>(V));
175 }
176};
177
178
179//===----------------------------------------------------------------------===//
180// FreeInst Class
181//===----------------------------------------------------------------------===//
182
183/// FreeInst - an instruction to deallocate memory
184///
Chris Lattner454928e2005-01-29 00:31:36 +0000185class FreeInst : public UnaryInstruction {
186 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000187public:
188 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
189 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
190
Chris Lattnerf319e832004-10-15 23:52:05 +0000191 virtual FreeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000192
193 virtual bool mayWriteToMemory() const { return true; }
194
195 // Methods for support type inquiry through isa, cast, and dyn_cast:
196 static inline bool classof(const FreeInst *) { return true; }
197 static inline bool classof(const Instruction *I) {
198 return (I->getOpcode() == Instruction::Free);
199 }
200 static inline bool classof(const Value *V) {
201 return isa<Instruction>(V) && classof(cast<Instruction>(V));
202 }
203};
204
205
206//===----------------------------------------------------------------------===//
207// LoadInst Class
208//===----------------------------------------------------------------------===//
209
Chris Lattner88fe29a2005-02-05 01:44:18 +0000210/// LoadInst - an instruction for reading from memory. This uses the
211/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000212///
Chris Lattner454928e2005-01-29 00:31:36 +0000213class LoadInst : public UnaryInstruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000214 LoadInst(const LoadInst &LI)
Chris Lattner88fe29a2005-02-05 01:44:18 +0000215 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
216 setVolatile(LI.isVolatile());
Misha Brukman9769ab22005-04-21 20:19:05 +0000217
Chris Lattner454928e2005-01-29 00:31:36 +0000218#ifndef NDEBUG
219 AssertOK();
220#endif
221 }
222 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000223public:
224 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
225 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000226 explicit LoadInst(Value *Ptr, const std::string &Name = "",
227 bool isVolatile = false, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000228 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
229 BasicBlock *InsertAtEnd);
230
231 /// isVolatile - Return true if this is a load from a volatile memory
232 /// location.
233 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000234 bool isVolatile() const { return SubclassData; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000235
236 /// setVolatile - Specify whether this is a volatile load or not.
237 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000238 void setVolatile(bool V) { SubclassData = V; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000239
Chris Lattnerf319e832004-10-15 23:52:05 +0000240 virtual LoadInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000241
242 virtual bool mayWriteToMemory() const { return isVolatile(); }
243
244 Value *getPointerOperand() { return getOperand(0); }
245 const Value *getPointerOperand() const { return getOperand(0); }
246 static unsigned getPointerOperandIndex() { return 0U; }
247
248 // Methods for support type inquiry through isa, cast, and dyn_cast:
249 static inline bool classof(const LoadInst *) { return true; }
250 static inline bool classof(const Instruction *I) {
251 return I->getOpcode() == Instruction::Load;
252 }
253 static inline bool classof(const Value *V) {
254 return isa<Instruction>(V) && classof(cast<Instruction>(V));
255 }
256};
257
258
259//===----------------------------------------------------------------------===//
260// StoreInst Class
261//===----------------------------------------------------------------------===//
262
Misha Brukman9769ab22005-04-21 20:19:05 +0000263/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000264///
265class StoreInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000266 Use Ops[2];
Chris Lattner88fe29a2005-02-05 01:44:18 +0000267 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
Chris Lattner454928e2005-01-29 00:31:36 +0000268 Ops[0].init(SI.Ops[0], this);
269 Ops[1].init(SI.Ops[1], this);
Chris Lattner88fe29a2005-02-05 01:44:18 +0000270 setVolatile(SI.isVolatile());
Chris Lattner454928e2005-01-29 00:31:36 +0000271#ifndef NDEBUG
272 AssertOK();
273#endif
274 }
275 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000276public:
277 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
278 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
279 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
280 Instruction *InsertBefore = 0);
281 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
282
283
284 /// isVolatile - Return true if this is a load from a volatile memory
285 /// location.
286 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000287 bool isVolatile() const { return SubclassData; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000288
289 /// setVolatile - Specify whether this is a volatile load or not.
290 ///
Chris Lattner88fe29a2005-02-05 01:44:18 +0000291 void setVolatile(bool V) { SubclassData = V; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000292
Chris Lattner454928e2005-01-29 00:31:36 +0000293 /// Transparently provide more efficient getOperand methods.
Misha Brukman9769ab22005-04-21 20:19:05 +0000294 Value *getOperand(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +0000295 assert(i < 2 && "getOperand() out of range!");
296 return Ops[i];
297 }
298 void setOperand(unsigned i, Value *Val) {
299 assert(i < 2 && "setOperand() out of range!");
300 Ops[i] = Val;
301 }
302 unsigned getNumOperands() const { return 2; }
303
304
Chris Lattnerf319e832004-10-15 23:52:05 +0000305 virtual StoreInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000306
307 virtual bool mayWriteToMemory() const { return true; }
308
309 Value *getPointerOperand() { return getOperand(1); }
310 const Value *getPointerOperand() const { return getOperand(1); }
311 static unsigned getPointerOperandIndex() { return 1U; }
312
313 // Methods for support type inquiry through isa, cast, and dyn_cast:
314 static inline bool classof(const StoreInst *) { return true; }
315 static inline bool classof(const Instruction *I) {
316 return I->getOpcode() == Instruction::Store;
317 }
318 static inline bool classof(const Value *V) {
319 return isa<Instruction>(V) && classof(cast<Instruction>(V));
320 }
321};
322
323
324//===----------------------------------------------------------------------===//
325// GetElementPtrInst Class
326//===----------------------------------------------------------------------===//
327
328/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
329/// access elements of arrays and structs
330///
331class GetElementPtrInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000332 GetElementPtrInst(const GetElementPtrInst &GEPI)
333 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
334 0, GEPI.getNumOperands()) {
335 Use *OL = OperandList = new Use[NumOperands];
336 Use *GEPIOL = GEPI.OperandList;
337 for (unsigned i = 0, E = NumOperands; i != E; ++i)
338 OL[i].init(GEPIOL[i], this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000339 }
Chris Lattner6ffbe172007-01-31 19:47:18 +0000340 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000341 void init(Value *Ptr, Value *Idx0, Value *Idx1);
Chris Lattner38bacf22005-05-03 05:43:30 +0000342 void init(Value *Ptr, Value *Idx);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000343public:
344 /// Constructors - Create a getelementptr instruction with a base pointer an
345 /// list of indices. The first ctor can optionally insert before an existing
346 /// instruction, the second appends the new instruction to the specified
347 /// BasicBlock.
Chris Lattnerfb110532007-01-31 04:39:29 +0000348 GetElementPtrInst(Value *Ptr, Value* const *Idx, unsigned NumIdx,
349 const std::string &Name = "", Instruction *InsertBefore =0);
350 GetElementPtrInst(Value *Ptr, Value* const *Idx, unsigned NumIdx,
351 const std::string &Name, BasicBlock *InsertAtEnd);
352
Chris Lattner38bacf22005-05-03 05:43:30 +0000353 /// Constructors - These two constructors are convenience methods because one
354 /// and two index getelementptr instructions are so common.
355 GetElementPtrInst(Value *Ptr, Value *Idx,
356 const std::string &Name = "", Instruction *InsertBefore =0);
357 GetElementPtrInst(Value *Ptr, Value *Idx,
358 const std::string &Name, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000359 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman91102862005-03-16 03:46:55 +0000360 const std::string &Name = "", Instruction *InsertBefore =0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000361 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman91102862005-03-16 03:46:55 +0000362 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattner454928e2005-01-29 00:31:36 +0000363 ~GetElementPtrInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000364
Chris Lattnerf319e832004-10-15 23:52:05 +0000365 virtual GetElementPtrInst *clone() const;
Misha Brukman9769ab22005-04-21 20:19:05 +0000366
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000367 // getType - Overload to return most specific pointer type...
368 inline const PointerType *getType() const {
369 return reinterpret_cast<const PointerType*>(Instruction::getType());
370 }
371
372 /// getIndexedType - Returns the type of the element that would be loaded with
373 /// a load instruction with the specified parameters.
374 ///
Misha Brukman9769ab22005-04-21 20:19:05 +0000375 /// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000376 /// pointer type.
377 ///
Misha Brukman9769ab22005-04-21 20:19:05 +0000378 static const Type *getIndexedType(const Type *Ptr,
Chris Lattnerfb110532007-01-31 04:39:29 +0000379 Value* const *Idx, unsigned NumIdx,
Misha Brukman91102862005-03-16 03:46:55 +0000380 bool AllowStructLeaf = false);
Chris Lattnerfb110532007-01-31 04:39:29 +0000381
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000382 static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman91102862005-03-16 03:46:55 +0000383 bool AllowStructLeaf = false);
Chris Lattner38bacf22005-05-03 05:43:30 +0000384 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
Misha Brukman9769ab22005-04-21 20:19:05 +0000385
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000386 inline op_iterator idx_begin() { return op_begin()+1; }
387 inline const_op_iterator idx_begin() const { return op_begin()+1; }
388 inline op_iterator idx_end() { return op_end(); }
389 inline const_op_iterator idx_end() const { return op_end(); }
390
391 Value *getPointerOperand() {
392 return getOperand(0);
393 }
394 const Value *getPointerOperand() const {
395 return getOperand(0);
396 }
397 static unsigned getPointerOperandIndex() {
398 return 0U; // get index for modifying correct operand
399 }
400
401 inline unsigned getNumIndices() const { // Note: always non-negative
402 return getNumOperands() - 1;
403 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000404
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000405 inline bool hasIndices() const {
406 return getNumOperands() > 1;
407 }
408
409 // Methods for support type inquiry through isa, cast, and dyn_cast:
410 static inline bool classof(const GetElementPtrInst *) { return true; }
411 static inline bool classof(const Instruction *I) {
412 return (I->getOpcode() == Instruction::GetElementPtr);
413 }
414 static inline bool classof(const Value *V) {
415 return isa<Instruction>(V) && classof(cast<Instruction>(V));
416 }
417};
418
419//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000420// ICmpInst Class
421//===----------------------------------------------------------------------===//
422
423/// This instruction compares its operands according to the predicate given
424/// to the constructor. It only operates on integers, pointers, or packed
425/// vectors of integrals. The two operands must be the same type.
426/// @brief Represent an integer comparison operator.
427class ICmpInst: public CmpInst {
428public:
429 /// This enumeration lists the possible predicates for the ICmpInst. The
430 /// values in the range 0-31 are reserved for FCmpInst while values in the
431 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
432 /// predicate values are not overlapping between the classes.
433 enum Predicate {
434 ICMP_EQ = 32, ///< equal
435 ICMP_NE = 33, ///< not equal
436 ICMP_UGT = 34, ///< unsigned greater than
437 ICMP_UGE = 35, ///< unsigned greater or equal
438 ICMP_ULT = 36, ///< unsigned less than
439 ICMP_ULE = 37, ///< unsigned less or equal
440 ICMP_SGT = 38, ///< signed greater than
441 ICMP_SGE = 39, ///< signed greater or equal
442 ICMP_SLT = 40, ///< signed less than
443 ICMP_SLE = 41, ///< signed less or equal
444 FIRST_ICMP_PREDICATE = ICMP_EQ,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000445 LAST_ICMP_PREDICATE = ICMP_SLE,
446 BAD_ICMP_PREDICATE = ICMP_SLE + 1
Reid Spencer45fb3f32006-11-20 01:22:35 +0000447 };
448
449 /// @brief Constructor with insert-before-instruction semantics.
450 ICmpInst(
451 Predicate pred, ///< The predicate to use for the comparison
452 Value *LHS, ///< The left-hand-side of the expression
453 Value *RHS, ///< The right-hand-side of the expression
454 const std::string &Name = "", ///< Name of the instruction
455 Instruction *InsertBefore = 0 ///< Where to insert
456 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertBefore) {
457 }
458
459 /// @brief Constructor with insert-at-block-end semantics.
460 ICmpInst(
461 Predicate pred, ///< The predicate to use for the comparison
462 Value *LHS, ///< The left-hand-side of the expression
463 Value *RHS, ///< The right-hand-side of the expression
464 const std::string &Name, ///< Name of the instruction
465 BasicBlock *InsertAtEnd ///< Block to insert into.
466 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertAtEnd) {
467 }
468
469 /// @brief Return the predicate for this instruction.
470 Predicate getPredicate() const { return Predicate(SubclassData); }
471
Chris Lattnerb769d562007-01-14 19:41:24 +0000472 /// @brief Set the predicate for this instruction to the specified value.
473 void setPredicate(Predicate P) { SubclassData = P; }
474
Reid Spencer45fb3f32006-11-20 01:22:35 +0000475 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
476 /// @returns the inverse predicate for the instruction's current predicate.
477 /// @brief Return the inverse of the instruction's predicate.
478 Predicate getInversePredicate() const {
479 return getInversePredicate(getPredicate());
480 }
481
482 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
483 /// @returns the inverse predicate for predicate provided in \p pred.
484 /// @brief Return the inverse of a given predicate
485 static Predicate getInversePredicate(Predicate pred);
486
487 /// For example, EQ->EQ, SLE->SGE, ULT->UGT, etc.
488 /// @returns the predicate that would be the result of exchanging the two
489 /// operands of the ICmpInst instruction without changing the result
490 /// produced.
491 /// @brief Return the predicate as if the operands were swapped
492 Predicate getSwappedPredicate() const {
493 return getSwappedPredicate(getPredicate());
494 }
495
496 /// This is a static version that you can use without an instruction
497 /// available.
498 /// @brief Return the predicate as if the operands were swapped.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000499 static Predicate getSwappedPredicate(Predicate pred);
500
501 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
502 /// @returns the predicate that would be the result if the operand were
503 /// regarded as signed.
504 /// @brief Return the signed version of the predicate
505 Predicate getSignedPredicate() const {
506 return getSignedPredicate(getPredicate());
507 }
508
509 /// This is a static version that you can use without an instruction.
510 /// @brief Return the signed version of the predicate.
511 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000512
513 /// This also tests for commutativity. If isEquality() returns true then
Reid Spencere4d87aa2006-12-23 06:05:41 +0000514 /// the predicate is also commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000515 /// @returns true if the predicate of this instruction is EQ or NE.
516 /// @brief Determine if this is an equality predicate.
517 bool isEquality() const {
518 return SubclassData == ICMP_EQ || SubclassData == ICMP_NE;
519 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000520
521 /// @returns true if the predicate of this ICmpInst is commutative
522 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000523 bool isCommutative() const { return isEquality(); }
524
525 /// @returns true if the predicate is relational (not EQ or NE).
526 /// @brief Determine if this a relational predicate.
527 bool isRelational() const {
528 return !isEquality();
529 }
530
Reid Spencere4d87aa2006-12-23 06:05:41 +0000531 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
532 /// @brief Determine if this instruction's predicate is signed.
533 bool isSignedPredicate() { return isSignedPredicate(getPredicate()); }
534
535 /// @returns true if the predicate provided is signed, false otherwise
536 /// @brief Determine if the predicate is signed.
537 static bool isSignedPredicate(Predicate pred);
538
Reid Spencer45fb3f32006-11-20 01:22:35 +0000539 /// Exchange the two operands to this instruction in such a way that it does
540 /// not modify the semantics of the instruction. The predicate value may be
541 /// changed to retain the same result if the predicate is order dependent
542 /// (e.g. ult).
543 /// @brief Swap operands and adjust predicate.
544 void swapOperands() {
545 SubclassData = getSwappedPredicate();
546 std::swap(Ops[0], Ops[1]);
547 }
548
549 // Methods for support type inquiry through isa, cast, and dyn_cast:
550 static inline bool classof(const ICmpInst *) { return true; }
551 static inline bool classof(const Instruction *I) {
552 return I->getOpcode() == Instruction::ICmp;
553 }
554 static inline bool classof(const Value *V) {
555 return isa<Instruction>(V) && classof(cast<Instruction>(V));
556 }
557};
558
559//===----------------------------------------------------------------------===//
560// FCmpInst Class
561//===----------------------------------------------------------------------===//
562
563/// This instruction compares its operands according to the predicate given
564/// to the constructor. It only operates on floating point values or packed
565/// vectors of floating point values. The operands must be identical types.
566/// @brief Represents a floating point comparison operator.
567class FCmpInst: public CmpInst {
568public:
569 /// This enumeration lists the possible predicates for the FCmpInst. Values
570 /// in the range 0-31 are reserved for FCmpInst.
571 enum Predicate {
572 // Opcode U L G E Intuitive operation
573 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
574 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
575 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
576 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
577 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
578 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
579 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
580 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
581 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
582 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
583 FCMP_UGT =10, ///< 1 0 1 0 True if unordered or greater than
584 FCMP_UGE =11, ///< 1 0 1 1 True if unordered, greater than, or equal
585 FCMP_ULT =12, ///< 1 1 0 0 True if unordered or less than
586 FCMP_ULE =13, ///< 1 1 0 1 True if unordered, less than, or equal
587 FCMP_UNE =14, ///< 1 1 1 0 True if unordered or not equal
588 FCMP_TRUE =15, ///< 1 1 1 1 Always true (always folded)
589 FIRST_FCMP_PREDICATE = FCMP_FALSE,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000590 LAST_FCMP_PREDICATE = FCMP_TRUE,
591 BAD_FCMP_PREDICATE = FCMP_TRUE + 1
Reid Spencer45fb3f32006-11-20 01:22:35 +0000592 };
593
594 /// @brief Constructor with insert-before-instruction semantics.
595 FCmpInst(
596 Predicate pred, ///< The predicate to use for the comparison
597 Value *LHS, ///< The left-hand-side of the expression
598 Value *RHS, ///< The right-hand-side of the expression
599 const std::string &Name = "", ///< Name of the instruction
600 Instruction *InsertBefore = 0 ///< Where to insert
601 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertBefore) {
602 }
603
604 /// @brief Constructor with insert-at-block-end semantics.
605 FCmpInst(
606 Predicate pred, ///< The predicate to use for the comparison
607 Value *LHS, ///< The left-hand-side of the expression
608 Value *RHS, ///< The right-hand-side of the expression
609 const std::string &Name, ///< Name of the instruction
610 BasicBlock *InsertAtEnd ///< Block to insert into.
611 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertAtEnd) {
612 }
613
614 /// @brief Return the predicate for this instruction.
615 Predicate getPredicate() const { return Predicate(SubclassData); }
616
Chris Lattnerb769d562007-01-14 19:41:24 +0000617 /// @brief Set the predicate for this instruction to the specified value.
618 void setPredicate(Predicate P) { SubclassData = P; }
619
Reid Spencer45fb3f32006-11-20 01:22:35 +0000620 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
621 /// @returns the inverse predicate for the instructions current predicate.
622 /// @brief Return the inverse of the predicate
623 Predicate getInversePredicate() const {
624 return getInversePredicate(getPredicate());
625 }
626
627 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
628 /// @returns the inverse predicate for \p pred.
629 /// @brief Return the inverse of a given predicate
630 static Predicate getInversePredicate(Predicate pred);
631
632 /// For example, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
633 /// @returns the predicate that would be the result of exchanging the two
634 /// operands of the ICmpInst instruction without changing the result
635 /// produced.
636 /// @brief Return the predicate as if the operands were swapped
637 Predicate getSwappedPredicate() const {
638 return getSwappedPredicate(getPredicate());
639 }
640
641 /// This is a static version that you can use without an instruction
642 /// available.
643 /// @brief Return the predicate as if the operands were swapped.
644 static Predicate getSwappedPredicate(Predicate Opcode);
645
646 /// This also tests for commutativity. If isEquality() returns true then
647 /// the predicate is also commutative. Only the equality predicates are
648 /// commutative.
649 /// @returns true if the predicate of this instruction is EQ or NE.
650 /// @brief Determine if this is an equality predicate.
651 bool isEquality() const {
652 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
653 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
654 }
655 bool isCommutative() const { return isEquality(); }
656
657 /// @returns true if the predicate is relational (not EQ or NE).
658 /// @brief Determine if this a relational predicate.
659 bool isRelational() const { return !isEquality(); }
660
661 /// Exchange the two operands to this instruction in such a way that it does
662 /// not modify the semantics of the instruction. The predicate value may be
663 /// changed to retain the same result if the predicate is order dependent
664 /// (e.g. ult).
665 /// @brief Swap operands and adjust predicate.
666 void swapOperands() {
667 SubclassData = getSwappedPredicate();
668 std::swap(Ops[0], Ops[1]);
669 }
670
671 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
672 static inline bool classof(const FCmpInst *) { return true; }
673 static inline bool classof(const Instruction *I) {
674 return I->getOpcode() == Instruction::FCmp;
675 }
676 static inline bool classof(const Value *V) {
677 return isa<Instruction>(V) && classof(cast<Instruction>(V));
678 }
679};
680
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000681//===----------------------------------------------------------------------===//
682// CallInst Class
683//===----------------------------------------------------------------------===//
684
685/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +0000686/// machine's calling convention. This class uses low bit of the SubClassData
687/// field to indicate whether or not this is a tail call. The rest of the bits
688/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000689///
690class CallInst : public Instruction {
691 CallInst(const CallInst &CI);
Chris Lattnerd54f4322007-02-13 00:58:44 +0000692 void init(Value *Func, Value* const *Params, unsigned NumParams);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000693 void init(Value *Func, Value *Actual1, Value *Actual2);
694 void init(Value *Func, Value *Actual);
695 void init(Value *Func);
696
697public:
Chris Lattnerd2dd1502007-02-13 01:04:01 +0000698 CallInst(Value *F, Value* const *Args, unsigned NumArgs,
699 const std::string &Name = "", Instruction *InsertBefore = 0);
700 CallInst(Value *F, Value *const *Args, unsigned NumArgs,
701 const std::string &Name, BasicBlock *InsertAtEnd);
702
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000703 CallInst(Value *F, const std::vector<Value*> &Par,
704 const std::string &Name = "", Instruction *InsertBefore = 0);
705 CallInst(Value *F, const std::vector<Value*> &Par,
706 const std::string &Name, BasicBlock *InsertAtEnd);
707
708 // Alternate CallInst ctors w/ two actuals, w/ one actual and no
709 // actuals, respectively.
710 CallInst(Value *F, Value *Actual1, Value *Actual2,
711 const std::string& Name = "", Instruction *InsertBefore = 0);
712 CallInst(Value *F, Value *Actual1, Value *Actual2,
713 const std::string& Name, BasicBlock *InsertAtEnd);
714 CallInst(Value *F, Value *Actual, const std::string& Name = "",
715 Instruction *InsertBefore = 0);
716 CallInst(Value *F, Value *Actual, const std::string& Name,
717 BasicBlock *InsertAtEnd);
Misha Brukman9769ab22005-04-21 20:19:05 +0000718 explicit CallInst(Value *F, const std::string &Name = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000719 Instruction *InsertBefore = 0);
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000720 CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattner454928e2005-01-29 00:31:36 +0000721 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000722
Chris Lattnerf319e832004-10-15 23:52:05 +0000723 virtual CallInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000724 bool mayWriteToMemory() const { return true; }
725
Chris Lattner3340ffe2005-05-06 20:26:26 +0000726 bool isTailCall() const { return SubclassData & 1; }
727 void setTailCall(bool isTailCall = true) {
Jeff Cohen39cef602005-05-07 02:44:04 +0000728 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
Chris Lattner3340ffe2005-05-06 20:26:26 +0000729 }
730
731 /// getCallingConv/setCallingConv - Get or set the calling convention of this
732 /// function call.
733 unsigned getCallingConv() const { return SubclassData >> 1; }
734 void setCallingConv(unsigned CC) {
735 SubclassData = (SubclassData & 1) | (CC << 1);
736 }
Chris Lattnerddb6db42005-05-06 05:51:46 +0000737
Chris Lattner721aef62004-11-18 17:46:57 +0000738 /// getCalledFunction - Return the function being called by this instruction
739 /// if it is a direct call. If it is a call through a function pointer,
740 /// return null.
741 Function *getCalledFunction() const {
Reid Spenceredd5d9e2005-05-15 16:13:11 +0000742 return static_cast<Function*>(dyn_cast<Function>(getOperand(0)));
Chris Lattner721aef62004-11-18 17:46:57 +0000743 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000744
Reid Spencerc25ec252006-12-29 04:10:59 +0000745 /// getCalledValue - Get a pointer to the function that is invoked by this
746 /// instruction
Chris Lattner454928e2005-01-29 00:31:36 +0000747 inline const Value *getCalledValue() const { return getOperand(0); }
748 inline Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000749
750 // Methods for support type inquiry through isa, cast, and dyn_cast:
751 static inline bool classof(const CallInst *) { return true; }
752 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +0000753 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000754 }
755 static inline bool classof(const Value *V) {
756 return isa<Instruction>(V) && classof(cast<Instruction>(V));
757 }
758};
759
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000760//===----------------------------------------------------------------------===//
761// SelectInst Class
762//===----------------------------------------------------------------------===//
763
764/// SelectInst - This class represents the LLVM 'select' instruction.
765///
766class SelectInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000767 Use Ops[3];
768
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000769 void init(Value *C, Value *S1, Value *S2) {
Chris Lattner454928e2005-01-29 00:31:36 +0000770 Ops[0].init(C, this);
771 Ops[1].init(S1, this);
772 Ops[2].init(S2, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000773 }
774
Chris Lattner454928e2005-01-29 00:31:36 +0000775 SelectInst(const SelectInst &SI)
776 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
777 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
778 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000779public:
780 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
781 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000782 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
783 Name, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000784 init(C, S1, S2);
785 }
786 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
787 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000788 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
789 Name, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000790 init(C, S1, S2);
791 }
792
Chris Lattner454928e2005-01-29 00:31:36 +0000793 Value *getCondition() const { return Ops[0]; }
794 Value *getTrueValue() const { return Ops[1]; }
795 Value *getFalseValue() const { return Ops[2]; }
796
797 /// Transparently provide more efficient getOperand methods.
798 Value *getOperand(unsigned i) const {
799 assert(i < 3 && "getOperand() out of range!");
800 return Ops[i];
801 }
802 void setOperand(unsigned i, Value *Val) {
803 assert(i < 3 && "setOperand() out of range!");
804 Ops[i] = Val;
805 }
806 unsigned getNumOperands() const { return 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000807
808 OtherOps getOpcode() const {
809 return static_cast<OtherOps>(Instruction::getOpcode());
810 }
811
Chris Lattnerf319e832004-10-15 23:52:05 +0000812 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000813
814 // Methods for support type inquiry through isa, cast, and dyn_cast:
815 static inline bool classof(const SelectInst *) { return true; }
816 static inline bool classof(const Instruction *I) {
817 return I->getOpcode() == Instruction::Select;
818 }
819 static inline bool classof(const Value *V) {
820 return isa<Instruction>(V) && classof(cast<Instruction>(V));
821 }
822};
823
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000824//===----------------------------------------------------------------------===//
825// VAArgInst Class
826//===----------------------------------------------------------------------===//
827
828/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +0000829/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000830///
Chris Lattner454928e2005-01-29 00:31:36 +0000831class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000832 VAArgInst(const VAArgInst &VAA)
Chris Lattner454928e2005-01-29 00:31:36 +0000833 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000834public:
835 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
836 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +0000837 : UnaryInstruction(Ty, VAArg, List, Name, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000838 }
839 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
840 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +0000841 : UnaryInstruction(Ty, VAArg, List, Name, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000842 }
843
Chris Lattnerf319e832004-10-15 23:52:05 +0000844 virtual VAArgInst *clone() const;
Andrew Lenharthc64b64a2005-06-19 14:46:20 +0000845 bool mayWriteToMemory() const { return true; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000846
847 // Methods for support type inquiry through isa, cast, and dyn_cast:
848 static inline bool classof(const VAArgInst *) { return true; }
849 static inline bool classof(const Instruction *I) {
850 return I->getOpcode() == VAArg;
851 }
852 static inline bool classof(const Value *V) {
853 return isa<Instruction>(V) && classof(cast<Instruction>(V));
854 }
855};
856
857//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +0000858// ExtractElementInst Class
859//===----------------------------------------------------------------------===//
860
861/// ExtractElementInst - This instruction extracts a single (scalar)
862/// element from a PackedType value
863///
864class ExtractElementInst : public Instruction {
865 Use Ops[2];
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000866 ExtractElementInst(const ExtractElementInst &EE) :
Robert Bocchinof9993442006-01-17 20:05:59 +0000867 Instruction(EE.getType(), ExtractElement, Ops, 2) {
868 Ops[0].init(EE.Ops[0], this);
869 Ops[1].init(EE.Ops[1], this);
Robert Bocchino49b78a52006-01-10 19:04:13 +0000870 }
871
872public:
Chris Lattner9fc18d22006-04-08 01:15:18 +0000873 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
874 Instruction *InsertBefore = 0);
Chris Lattner06a248c22006-10-05 06:24:58 +0000875 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
876 Instruction *InsertBefore = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +0000877 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
878 BasicBlock *InsertAtEnd);
Chris Lattner06a248c22006-10-05 06:24:58 +0000879 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
880 BasicBlock *InsertAtEnd);
Robert Bocchino49b78a52006-01-10 19:04:13 +0000881
Chris Lattnerfa495842006-04-08 04:04:54 +0000882 /// isValidOperands - Return true if an extractelement instruction can be
883 /// formed with the specified operands.
884 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000885
Robert Bocchino49b78a52006-01-10 19:04:13 +0000886 virtual ExtractElementInst *clone() const;
887
888 virtual bool mayWriteToMemory() const { return false; }
889
890 /// Transparently provide more efficient getOperand methods.
891 Value *getOperand(unsigned i) const {
892 assert(i < 2 && "getOperand() out of range!");
893 return Ops[i];
894 }
895 void setOperand(unsigned i, Value *Val) {
896 assert(i < 2 && "setOperand() out of range!");
897 Ops[i] = Val;
898 }
899 unsigned getNumOperands() const { return 2; }
900
901 // Methods for support type inquiry through isa, cast, and dyn_cast:
902 static inline bool classof(const ExtractElementInst *) { return true; }
903 static inline bool classof(const Instruction *I) {
904 return I->getOpcode() == Instruction::ExtractElement;
905 }
906 static inline bool classof(const Value *V) {
907 return isa<Instruction>(V) && classof(cast<Instruction>(V));
908 }
909};
910
911//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +0000912// InsertElementInst Class
913//===----------------------------------------------------------------------===//
914
915/// InsertElementInst - This instruction inserts a single (scalar)
916/// element into a PackedType value
917///
918class InsertElementInst : public Instruction {
919 Use Ops[3];
Chris Lattner6a56ed42006-04-14 22:20:07 +0000920 InsertElementInst(const InsertElementInst &IE);
Robert Bocchinof9993442006-01-17 20:05:59 +0000921public:
Chris Lattner9fc18d22006-04-08 01:15:18 +0000922 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
923 const std::string &Name = "",Instruction *InsertBefore = 0);
Chris Lattner06a248c22006-10-05 06:24:58 +0000924 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
925 const std::string &Name = "",Instruction *InsertBefore = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +0000926 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Robert Bocchinof9993442006-01-17 20:05:59 +0000927 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattner06a248c22006-10-05 06:24:58 +0000928 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
929 const std::string &Name, BasicBlock *InsertAtEnd);
Robert Bocchinof9993442006-01-17 20:05:59 +0000930
Chris Lattnerfa495842006-04-08 04:04:54 +0000931 /// isValidOperands - Return true if an insertelement instruction can be
932 /// formed with the specified operands.
933 static bool isValidOperands(const Value *Vec, const Value *NewElt,
934 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000935
Robert Bocchinof9993442006-01-17 20:05:59 +0000936 virtual InsertElementInst *clone() const;
937
938 virtual bool mayWriteToMemory() const { return false; }
939
Chris Lattner6a56ed42006-04-14 22:20:07 +0000940 /// getType - Overload to return most specific packed type.
941 ///
942 inline const PackedType *getType() const {
943 return reinterpret_cast<const PackedType*>(Instruction::getType());
944 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000945
Robert Bocchinof9993442006-01-17 20:05:59 +0000946 /// Transparently provide more efficient getOperand methods.
947 Value *getOperand(unsigned i) const {
948 assert(i < 3 && "getOperand() out of range!");
949 return Ops[i];
950 }
951 void setOperand(unsigned i, Value *Val) {
952 assert(i < 3 && "setOperand() out of range!");
953 Ops[i] = Val;
954 }
955 unsigned getNumOperands() const { return 3; }
956
957 // Methods for support type inquiry through isa, cast, and dyn_cast:
958 static inline bool classof(const InsertElementInst *) { return true; }
959 static inline bool classof(const Instruction *I) {
960 return I->getOpcode() == Instruction::InsertElement;
961 }
962 static inline bool classof(const Value *V) {
963 return isa<Instruction>(V) && classof(cast<Instruction>(V));
964 }
965};
966
967//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +0000968// ShuffleVectorInst Class
969//===----------------------------------------------------------------------===//
970
971/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
972/// input vectors.
973///
974class ShuffleVectorInst : public Instruction {
975 Use Ops[3];
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000976 ShuffleVectorInst(const ShuffleVectorInst &IE);
Chris Lattner9fc18d22006-04-08 01:15:18 +0000977public:
978 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
979 const std::string &Name = "", Instruction *InsertBefor = 0);
980 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
981 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000982
Chris Lattnerfa495842006-04-08 04:04:54 +0000983 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +0000984 /// formed with the specified operands.
985 static bool isValidOperands(const Value *V1, const Value *V2,
986 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000987
Chris Lattner9fc18d22006-04-08 01:15:18 +0000988 virtual ShuffleVectorInst *clone() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000989
Chris Lattner9fc18d22006-04-08 01:15:18 +0000990 virtual bool mayWriteToMemory() const { return false; }
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000991
Chris Lattner6a56ed42006-04-14 22:20:07 +0000992 /// getType - Overload to return most specific packed type.
993 ///
994 inline const PackedType *getType() const {
995 return reinterpret_cast<const PackedType*>(Instruction::getType());
996 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000997
Chris Lattner9fc18d22006-04-08 01:15:18 +0000998 /// Transparently provide more efficient getOperand methods.
999 Value *getOperand(unsigned i) const {
1000 assert(i < 3 && "getOperand() out of range!");
1001 return Ops[i];
1002 }
1003 void setOperand(unsigned i, Value *Val) {
1004 assert(i < 3 && "setOperand() out of range!");
1005 Ops[i] = Val;
1006 }
1007 unsigned getNumOperands() const { return 3; }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001008
Chris Lattner9fc18d22006-04-08 01:15:18 +00001009 // Methods for support type inquiry through isa, cast, and dyn_cast:
1010 static inline bool classof(const ShuffleVectorInst *) { return true; }
1011 static inline bool classof(const Instruction *I) {
1012 return I->getOpcode() == Instruction::ShuffleVector;
1013 }
1014 static inline bool classof(const Value *V) {
1015 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1016 }
1017};
1018
1019
1020//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001021// PHINode Class
1022//===----------------------------------------------------------------------===//
1023
1024// PHINode - The PHINode class is used to represent the magical mystical PHI
1025// node, that can not exist in nature, but can be synthesized in a computer
1026// scientist's overactive imagination.
1027//
1028class PHINode : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +00001029 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1030 /// the number actually in use.
1031 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001032 PHINode(const PHINode &PN);
1033public:
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001034 explicit PHINode(const Type *Ty, const std::string &Name = "",
1035 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001036 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertBefore),
1037 ReservedSpace(0) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001038 }
1039
1040 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001041 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertAtEnd),
1042 ReservedSpace(0) {
1043 }
1044
1045 ~PHINode();
1046
1047 /// reserveOperandSpace - This method can be used to avoid repeated
1048 /// reallocation of PHI operand lists by reserving space for the correct
1049 /// number of operands before adding them. Unlike normal vector reserves,
1050 /// this method can also be used to trim the operand space.
1051 void reserveOperandSpace(unsigned NumValues) {
1052 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001053 }
1054
Chris Lattnerf319e832004-10-15 23:52:05 +00001055 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001056
1057 /// getNumIncomingValues - Return the number of incoming edges
1058 ///
Chris Lattner454928e2005-01-29 00:31:36 +00001059 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001060
Reid Spencerc773de62006-05-19 19:07:54 +00001061 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001062 ///
1063 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001064 assert(i*2 < getNumOperands() && "Invalid value number!");
1065 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001066 }
1067 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +00001068 assert(i*2 < getNumOperands() && "Invalid value number!");
1069 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001070 }
Chris Lattner454928e2005-01-29 00:31:36 +00001071 unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001072 return i*2;
1073 }
1074
Reid Spencerc773de62006-05-19 19:07:54 +00001075 /// getIncomingBlock - Return incoming basic block number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001076 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00001077 BasicBlock *getIncomingBlock(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001078 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001079 }
1080 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Chris Lattner454928e2005-01-29 00:31:36 +00001081 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001082 }
1083 unsigned getOperandNumForIncomingBlock(unsigned i) {
1084 return i*2+1;
1085 }
1086
1087 /// addIncoming - Add an incoming value to the end of the PHI list
1088 ///
1089 void addIncoming(Value *V, BasicBlock *BB) {
1090 assert(getType() == V->getType() &&
1091 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +00001092 unsigned OpNo = NumOperands;
1093 if (OpNo+2 > ReservedSpace)
1094 resizeOperands(0); // Get more space!
1095 // Initialize some new operands.
1096 NumOperands = OpNo+2;
1097 OperandList[OpNo].init(V, this);
1098 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001099 }
Misha Brukman9769ab22005-04-21 20:19:05 +00001100
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001101 /// removeIncomingValue - Remove an incoming value. This is useful if a
1102 /// predecessor basic block is deleted. The value removed is returned.
1103 ///
1104 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1105 /// is true), the PHI node is destroyed and any uses of it are replaced with
1106 /// dummy values. The only time there should be zero incoming values to a PHI
1107 /// node is when the block is dead, so this strategy is sound.
1108 ///
1109 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1110
1111 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1112 int Idx = getBasicBlockIndex(BB);
1113 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1114 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1115 }
1116
Misha Brukman9769ab22005-04-21 20:19:05 +00001117 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001118 /// block in the value list for this PHI. Returns -1 if no instance.
1119 ///
1120 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001121 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00001122 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Chris Lattner454928e2005-01-29 00:31:36 +00001123 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001124 return -1;
1125 }
1126
1127 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1128 return getIncomingValue(getBasicBlockIndex(BB));
1129 }
1130
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001131 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00001132 /// same value, return the value, otherwise return null.
1133 ///
Chris Lattner9acbd612005-08-05 00:49:06 +00001134 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001135
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001136 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1137 static inline bool classof(const PHINode *) { return true; }
1138 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001139 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001140 }
1141 static inline bool classof(const Value *V) {
1142 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1143 }
Chris Lattner454928e2005-01-29 00:31:36 +00001144 private:
1145 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001146};
1147
1148//===----------------------------------------------------------------------===//
1149// ReturnInst Class
1150//===----------------------------------------------------------------------===//
1151
1152//===---------------------------------------------------------------------------
1153/// ReturnInst - Return a value (possibly void), from a function. Execution
1154/// does not continue in this function any longer.
1155///
1156class ReturnInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001157 Use RetVal; // Possibly null retval.
1158 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret, &RetVal,
1159 RI.getNumOperands()) {
1160 if (RI.getNumOperands())
1161 RetVal.init(RI.RetVal, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001162 }
1163
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001164 void init(Value *RetVal);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001165
1166public:
1167 // ReturnInst constructors:
1168 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001169 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001170 // ReturnInst(Value* X) - 'ret X' instruction
1171 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
1172 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1173 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
1174 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001175 //
1176 // NOTE: If the Value* passed is of type void then the constructor behaves as
1177 // if it was passed NULL.
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001178 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001179 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertBefore) {
1180 init(retVal);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001181 }
Chris Lattner454928e2005-01-29 00:31:36 +00001182 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
1183 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
1184 init(retVal);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001185 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001186 explicit ReturnInst(BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001187 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001188 }
1189
Chris Lattnerf319e832004-10-15 23:52:05 +00001190 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001191
Chris Lattner454928e2005-01-29 00:31:36 +00001192 // Transparently provide more efficient getOperand methods.
1193 Value *getOperand(unsigned i) const {
1194 assert(i < getNumOperands() && "getOperand() out of range!");
1195 return RetVal;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001196 }
Chris Lattner454928e2005-01-29 00:31:36 +00001197 void setOperand(unsigned i, Value *Val) {
1198 assert(i < getNumOperands() && "setOperand() out of range!");
1199 RetVal = Val;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001200 }
1201
Chris Lattner454928e2005-01-29 00:31:36 +00001202 Value *getReturnValue() const { return RetVal; }
1203
1204 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001205
1206 // Methods for support type inquiry through isa, cast, and dyn_cast:
1207 static inline bool classof(const ReturnInst *) { return true; }
1208 static inline bool classof(const Instruction *I) {
1209 return (I->getOpcode() == Instruction::Ret);
1210 }
1211 static inline bool classof(const Value *V) {
1212 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1213 }
Chris Lattner454928e2005-01-29 00:31:36 +00001214 private:
1215 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1216 virtual unsigned getNumSuccessorsV() const;
1217 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001218};
1219
1220//===----------------------------------------------------------------------===//
1221// BranchInst Class
1222//===----------------------------------------------------------------------===//
1223
1224//===---------------------------------------------------------------------------
1225/// BranchInst - Conditional or Unconditional Branch instruction.
1226///
1227class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001228 /// Ops list - Branches are strange. The operands are ordered:
1229 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
1230 /// they don't have to check for cond/uncond branchness.
1231 Use Ops[3];
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001232 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00001233 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001234public:
1235 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1236 // BranchInst(BB *B) - 'br B'
1237 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1238 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1239 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1240 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1241 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001242 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001243 : TerminatorInst(Instruction::Br, Ops, 1, InsertBefore) {
1244 assert(IfTrue != 0 && "Branch destination may not be null!");
1245 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001246 }
1247 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1248 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001249 : TerminatorInst(Instruction::Br, Ops, 3, InsertBefore) {
1250 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1251 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
1252 Ops[2].init(Cond, this);
1253#ifndef NDEBUG
1254 AssertOK();
1255#endif
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001256 }
1257
1258 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001259 : TerminatorInst(Instruction::Br, Ops, 1, InsertAtEnd) {
1260 assert(IfTrue != 0 && "Branch destination may not be null!");
1261 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001262 }
1263
1264 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1265 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001266 : TerminatorInst(Instruction::Br, Ops, 3, InsertAtEnd) {
1267 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1268 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
1269 Ops[2].init(Cond, this);
1270#ifndef NDEBUG
1271 AssertOK();
1272#endif
1273 }
1274
1275
1276 /// Transparently provide more efficient getOperand methods.
1277 Value *getOperand(unsigned i) const {
1278 assert(i < getNumOperands() && "getOperand() out of range!");
1279 return Ops[i];
1280 }
1281 void setOperand(unsigned i, Value *Val) {
1282 assert(i < getNumOperands() && "setOperand() out of range!");
1283 Ops[i] = Val;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001284 }
1285
Chris Lattnerf319e832004-10-15 23:52:05 +00001286 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001287
Chris Lattner454928e2005-01-29 00:31:36 +00001288 inline bool isUnconditional() const { return getNumOperands() == 1; }
1289 inline bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001290
1291 inline Value *getCondition() const {
1292 assert(isConditional() && "Cannot get condition of an uncond branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001293 return getOperand(2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001294 }
1295
1296 void setCondition(Value *V) {
1297 assert(isConditional() && "Cannot set condition of unconditional branch!");
1298 setOperand(2, V);
1299 }
1300
1301 // setUnconditionalDest - Change the current branch to an unconditional branch
1302 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +00001303 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001304 void setUnconditionalDest(BasicBlock *Dest) {
Chris Lattner454928e2005-01-29 00:31:36 +00001305 if (isConditional()) { // Convert this to an uncond branch.
1306 NumOperands = 1;
1307 Ops[1].set(0);
1308 Ops[2].set(0);
1309 }
1310 setOperand(0, reinterpret_cast<Value*>(Dest));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001311 }
1312
Chris Lattner454928e2005-01-29 00:31:36 +00001313 unsigned getNumSuccessors() const { return 1+isConditional(); }
1314
1315 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001316 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Misha Brukman9769ab22005-04-21 20:19:05 +00001317 return (i == 0) ? cast<BasicBlock>(getOperand(0)) :
Chris Lattner454928e2005-01-29 00:31:36 +00001318 cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001319 }
1320
Chris Lattner454928e2005-01-29 00:31:36 +00001321 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001322 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001323 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001324 }
1325
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001326 // Methods for support type inquiry through isa, cast, and dyn_cast:
1327 static inline bool classof(const BranchInst *) { return true; }
1328 static inline bool classof(const Instruction *I) {
1329 return (I->getOpcode() == Instruction::Br);
1330 }
1331 static inline bool classof(const Value *V) {
1332 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1333 }
Chris Lattner454928e2005-01-29 00:31:36 +00001334private:
1335 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1336 virtual unsigned getNumSuccessorsV() const;
1337 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001338};
1339
1340//===----------------------------------------------------------------------===//
1341// SwitchInst Class
1342//===----------------------------------------------------------------------===//
1343
1344//===---------------------------------------------------------------------------
1345/// SwitchInst - Multiway switch
1346///
1347class SwitchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001348 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001349 // Operand[0] = Value to switch on
1350 // Operand[1] = Default basic block destination
1351 // Operand[2n ] = Value to match
1352 // Operand[2n+1] = BasicBlock to go to on match
1353 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00001354 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1355 void resizeOperands(unsigned No);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001356public:
Chris Lattner454928e2005-01-29 00:31:36 +00001357 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1358 /// switch on and a default destination. The number of additional cases can
1359 /// be specified here to make memory allocation more efficient. This
1360 /// constructor can also autoinsert before another instruction.
1361 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Misha Brukman9769ab22005-04-21 20:19:05 +00001362 Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001363 : TerminatorInst(Instruction::Switch, 0, 0, InsertBefore) {
1364 init(Value, Default, NumCases);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001365 }
1366
Chris Lattner454928e2005-01-29 00:31:36 +00001367 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1368 /// switch on and a default destination. The number of additional cases can
1369 /// be specified here to make memory allocation more efficient. This
1370 /// constructor also autoinserts at the end of the specified BasicBlock.
1371 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Misha Brukman9769ab22005-04-21 20:19:05 +00001372 BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001373 : TerminatorInst(Instruction::Switch, 0, 0, InsertAtEnd) {
1374 init(Value, Default, NumCases);
1375 }
1376 ~SwitchInst();
1377
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001378
1379 // Accessor Methods for Switch stmt
Chris Lattner454928e2005-01-29 00:31:36 +00001380 inline Value *getCondition() const { return getOperand(0); }
1381 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00001382
Chris Lattner454928e2005-01-29 00:31:36 +00001383 inline BasicBlock *getDefaultDest() const {
1384 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001385 }
1386
1387 /// getNumCases - return the number of 'cases' in this switch instruction.
1388 /// Note that case #0 is always the default case.
1389 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001390 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001391 }
1392
1393 /// getCaseValue - Return the specified case value. Note that case #0, the
1394 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001395 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001396 assert(i && i < getNumCases() && "Illegal case value to get!");
1397 return getSuccessorValue(i);
1398 }
1399
1400 /// getCaseValue - Return the specified case value. Note that case #0, the
1401 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001402 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001403 assert(i && i < getNumCases() && "Illegal case value to get!");
1404 return getSuccessorValue(i);
1405 }
1406
1407 /// findCaseValue - Search all of the case values for the specified constant.
1408 /// If it is explicitly handled, return the case number of it, otherwise
1409 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001410 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001411 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1412 if (getCaseValue(i) == C)
1413 return i;
1414 return 0;
1415 }
1416
Nick Lewycky011f1842006-09-18 19:03:59 +00001417 /// findCaseDest - Finds the unique case value for a given successor. Returns
1418 /// null if the successor is not found, not unique, or is the default case.
1419 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00001420 if (BB == getDefaultDest()) return NULL;
1421
Nick Lewycky011f1842006-09-18 19:03:59 +00001422 ConstantInt *CI = NULL;
1423 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
1424 if (getSuccessor(i) == BB) {
1425 if (CI) return NULL; // Multiple cases lead to BB.
1426 else CI = getCaseValue(i);
1427 }
1428 }
1429 return CI;
1430 }
1431
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001432 /// addCase - Add an entry to the switch instruction...
1433 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00001434 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001435
1436 /// removeCase - This method removes the specified successor from the switch
1437 /// instruction. Note that this cannot be used to remove the default
1438 /// destination (successor #0).
1439 ///
1440 void removeCase(unsigned idx);
1441
Chris Lattner454928e2005-01-29 00:31:36 +00001442 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001443
Chris Lattner454928e2005-01-29 00:31:36 +00001444 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1445 BasicBlock *getSuccessor(unsigned idx) const {
1446 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1447 return cast<BasicBlock>(getOperand(idx*2+1));
1448 }
1449 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001450 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001451 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001452 }
1453
1454 // getSuccessorValue - Return the value associated with the specified
1455 // successor.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001456 inline ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001457 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00001458 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001459 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001460
1461 // Methods for support type inquiry through isa, cast, and dyn_cast:
1462 static inline bool classof(const SwitchInst *) { return true; }
1463 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00001464 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001465 }
1466 static inline bool classof(const Value *V) {
1467 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1468 }
Chris Lattner454928e2005-01-29 00:31:36 +00001469private:
1470 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1471 virtual unsigned getNumSuccessorsV() const;
1472 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001473};
1474
1475//===----------------------------------------------------------------------===//
1476// InvokeInst Class
1477//===----------------------------------------------------------------------===//
1478
1479//===---------------------------------------------------------------------------
Chris Lattner3340ffe2005-05-06 20:26:26 +00001480
1481/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
1482/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001483///
1484class InvokeInst : public TerminatorInst {
1485 InvokeInst(const InvokeInst &BI);
1486 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerd2dd1502007-02-13 01:04:01 +00001487 Value* const *Args, unsigned NumArgs);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001488public:
1489 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Misha Brukman91102862005-03-16 03:46:55 +00001490 const std::vector<Value*> &Params, const std::string &Name = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001491 Instruction *InsertBefore = 0);
1492 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Misha Brukman91102862005-03-16 03:46:55 +00001493 const std::vector<Value*> &Params, const std::string &Name,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001494 BasicBlock *InsertAtEnd);
Chris Lattnerd2dd1502007-02-13 01:04:01 +00001495 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1496 Value* const* Args, unsigned NumArgs, const std::string &Name = "",
1497 Instruction *InsertBefore = 0);
1498 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1499 Value* const* Args, unsigned NumArgs, const std::string &Name,
1500 BasicBlock *InsertAtEnd);
Chris Lattner454928e2005-01-29 00:31:36 +00001501 ~InvokeInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001502
Chris Lattnerf319e832004-10-15 23:52:05 +00001503 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001504
1505 bool mayWriteToMemory() const { return true; }
1506
Chris Lattner3340ffe2005-05-06 20:26:26 +00001507 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1508 /// function call.
1509 unsigned getCallingConv() const { return SubclassData; }
1510 void setCallingConv(unsigned CC) {
1511 SubclassData = CC;
1512 }
1513
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001514 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00001515 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001516 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001517 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001518 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001519 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001520
1521 // getCalledValue - Get a pointer to a function that is invoked by this inst.
Chris Lattner454928e2005-01-29 00:31:36 +00001522 inline Value *getCalledValue() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001523
1524 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00001525 BasicBlock *getNormalDest() const {
1526 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001527 }
Chris Lattner454928e2005-01-29 00:31:36 +00001528 BasicBlock *getUnwindDest() const {
1529 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001530 }
Chris Lattner454928e2005-01-29 00:31:36 +00001531 void setNormalDest(BasicBlock *B) {
1532 setOperand(1, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001533 }
1534
Chris Lattner454928e2005-01-29 00:31:36 +00001535 void setUnwindDest(BasicBlock *B) {
1536 setOperand(2, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001537 }
1538
Chris Lattner454928e2005-01-29 00:31:36 +00001539 inline BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001540 assert(i < 2 && "Successor # out of range for invoke!");
1541 return i == 0 ? getNormalDest() : getUnwindDest();
1542 }
1543
Chris Lattner454928e2005-01-29 00:31:36 +00001544 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001545 assert(idx < 2 && "Successor # out of range for invoke!");
Chris Lattner454928e2005-01-29 00:31:36 +00001546 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001547 }
1548
Chris Lattner454928e2005-01-29 00:31:36 +00001549 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001550
1551 // Methods for support type inquiry through isa, cast, and dyn_cast:
1552 static inline bool classof(const InvokeInst *) { return true; }
1553 static inline bool classof(const Instruction *I) {
1554 return (I->getOpcode() == Instruction::Invoke);
1555 }
1556 static inline bool classof(const Value *V) {
1557 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1558 }
Chris Lattner454928e2005-01-29 00:31:36 +00001559private:
1560 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1561 virtual unsigned getNumSuccessorsV() const;
1562 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001563};
1564
1565
1566//===----------------------------------------------------------------------===//
1567// UnwindInst Class
1568//===----------------------------------------------------------------------===//
1569
1570//===---------------------------------------------------------------------------
1571/// UnwindInst - Immediately exit the current function, unwinding the stack
1572/// until an invoke instruction is found.
1573///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00001574class UnwindInst : public TerminatorInst {
1575public:
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001576 explicit UnwindInst(Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001577 : TerminatorInst(Instruction::Unwind, 0, 0, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001578 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001579 explicit UnwindInst(BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001580 : TerminatorInst(Instruction::Unwind, 0, 0, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001581 }
1582
Chris Lattnerf319e832004-10-15 23:52:05 +00001583 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001584
Chris Lattner454928e2005-01-29 00:31:36 +00001585 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001586
1587 // Methods for support type inquiry through isa, cast, and dyn_cast:
1588 static inline bool classof(const UnwindInst *) { return true; }
1589 static inline bool classof(const Instruction *I) {
1590 return I->getOpcode() == Instruction::Unwind;
1591 }
1592 static inline bool classof(const Value *V) {
1593 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1594 }
Chris Lattner454928e2005-01-29 00:31:36 +00001595private:
1596 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1597 virtual unsigned getNumSuccessorsV() const;
1598 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001599};
1600
Chris Lattner076b3f12004-10-16 18:05:54 +00001601//===----------------------------------------------------------------------===//
1602// UnreachableInst Class
1603//===----------------------------------------------------------------------===//
1604
1605//===---------------------------------------------------------------------------
1606/// UnreachableInst - This function has undefined behavior. In particular, the
1607/// presence of this instruction indicates some higher level knowledge that the
1608/// end of the block cannot be reached.
1609///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00001610class UnreachableInst : public TerminatorInst {
1611public:
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001612 explicit UnreachableInst(Instruction *InsertBefore = 0)
Chris Lattner454928e2005-01-29 00:31:36 +00001613 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertBefore) {
Chris Lattner076b3f12004-10-16 18:05:54 +00001614 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001615 explicit UnreachableInst(BasicBlock *InsertAtEnd)
Chris Lattner454928e2005-01-29 00:31:36 +00001616 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertAtEnd) {
Chris Lattner076b3f12004-10-16 18:05:54 +00001617 }
1618
1619 virtual UnreachableInst *clone() const;
1620
Chris Lattner454928e2005-01-29 00:31:36 +00001621 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00001622
1623 // Methods for support type inquiry through isa, cast, and dyn_cast:
1624 static inline bool classof(const UnreachableInst *) { return true; }
1625 static inline bool classof(const Instruction *I) {
1626 return I->getOpcode() == Instruction::Unreachable;
1627 }
1628 static inline bool classof(const Value *V) {
1629 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1630 }
Chris Lattner454928e2005-01-29 00:31:36 +00001631private:
1632 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1633 virtual unsigned getNumSuccessorsV() const;
1634 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00001635};
1636
Reid Spencer3da59db2006-11-27 01:05:10 +00001637//===----------------------------------------------------------------------===//
1638// TruncInst Class
1639//===----------------------------------------------------------------------===//
1640
1641/// @brief This class represents a truncation of integer types.
1642class TruncInst : public CastInst {
1643 /// Private copy constructor
1644 TruncInst(const TruncInst &CI)
1645 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
1646 }
1647public:
1648 /// @brief Constructor with insert-before-instruction semantics
1649 TruncInst(
1650 Value *S, ///< The value to be truncated
1651 const Type *Ty, ///< The (smaller) type to truncate to
1652 const std::string &Name = "", ///< A name for the new instruction
1653 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1654 );
1655
1656 /// @brief Constructor with insert-at-end-of-block semantics
1657 TruncInst(
1658 Value *S, ///< The value to be truncated
1659 const Type *Ty, ///< The (smaller) type to truncate to
1660 const std::string &Name, ///< A name for the new instruction
1661 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1662 );
1663
1664 /// @brief Clone an identical TruncInst
1665 virtual CastInst *clone() const;
1666
1667 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1668 static inline bool classof(const TruncInst *) { return true; }
1669 static inline bool classof(const Instruction *I) {
1670 return I->getOpcode() == Trunc;
1671 }
1672 static inline bool classof(const Value *V) {
1673 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1674 }
1675};
1676
1677//===----------------------------------------------------------------------===//
1678// ZExtInst Class
1679//===----------------------------------------------------------------------===//
1680
1681/// @brief This class represents zero extension of integer types.
1682class ZExtInst : public CastInst {
1683 /// @brief Private copy constructor
1684 ZExtInst(const ZExtInst &CI)
1685 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
1686 }
1687public:
1688 /// @brief Constructor with insert-before-instruction semantics
1689 ZExtInst(
1690 Value *S, ///< The value to be zero extended
1691 const Type *Ty, ///< The type to zero extend to
1692 const std::string &Name = "", ///< A name for the new instruction
1693 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1694 );
1695
1696 /// @brief Constructor with insert-at-end semantics.
1697 ZExtInst(
1698 Value *S, ///< The value to be zero extended
1699 const Type *Ty, ///< The type to zero extend to
1700 const std::string &Name, ///< A name for the new instruction
1701 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1702 );
1703
1704 /// @brief Clone an identical ZExtInst
1705 virtual CastInst *clone() const;
1706
1707 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1708 static inline bool classof(const ZExtInst *) { return true; }
1709 static inline bool classof(const Instruction *I) {
1710 return I->getOpcode() == ZExt;
1711 }
1712 static inline bool classof(const Value *V) {
1713 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1714 }
1715};
1716
1717//===----------------------------------------------------------------------===//
1718// SExtInst Class
1719//===----------------------------------------------------------------------===//
1720
1721/// @brief This class represents a sign extension of integer types.
1722class SExtInst : public CastInst {
1723 /// @brief Private copy constructor
1724 SExtInst(const SExtInst &CI)
1725 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
1726 }
1727public:
1728 /// @brief Constructor with insert-before-instruction semantics
1729 SExtInst(
1730 Value *S, ///< The value to be sign extended
1731 const Type *Ty, ///< The type to sign extend to
1732 const std::string &Name = "", ///< A name for the new instruction
1733 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1734 );
1735
1736 /// @brief Constructor with insert-at-end-of-block semantics
1737 SExtInst(
1738 Value *S, ///< The value to be sign extended
1739 const Type *Ty, ///< The type to sign extend to
1740 const std::string &Name, ///< A name for the new instruction
1741 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1742 );
1743
1744 /// @brief Clone an identical SExtInst
1745 virtual CastInst *clone() const;
1746
1747 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1748 static inline bool classof(const SExtInst *) { return true; }
1749 static inline bool classof(const Instruction *I) {
1750 return I->getOpcode() == SExt;
1751 }
1752 static inline bool classof(const Value *V) {
1753 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1754 }
1755};
1756
1757//===----------------------------------------------------------------------===//
1758// FPTruncInst Class
1759//===----------------------------------------------------------------------===//
1760
1761/// @brief This class represents a truncation of floating point types.
1762class FPTruncInst : public CastInst {
1763 FPTruncInst(const FPTruncInst &CI)
1764 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
1765 }
1766public:
1767 /// @brief Constructor with insert-before-instruction semantics
1768 FPTruncInst(
1769 Value *S, ///< The value to be truncated
1770 const Type *Ty, ///< The type to truncate to
1771 const std::string &Name = "", ///< A name for the new instruction
1772 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1773 );
1774
1775 /// @brief Constructor with insert-before-instruction semantics
1776 FPTruncInst(
1777 Value *S, ///< The value to be truncated
1778 const Type *Ty, ///< The type to truncate to
1779 const std::string &Name, ///< A name for the new instruction
1780 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1781 );
1782
1783 /// @brief Clone an identical FPTruncInst
1784 virtual CastInst *clone() const;
1785
1786 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1787 static inline bool classof(const FPTruncInst *) { return true; }
1788 static inline bool classof(const Instruction *I) {
1789 return I->getOpcode() == FPTrunc;
1790 }
1791 static inline bool classof(const Value *V) {
1792 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1793 }
1794};
1795
1796//===----------------------------------------------------------------------===//
1797// FPExtInst Class
1798//===----------------------------------------------------------------------===//
1799
1800/// @brief This class represents an extension of floating point types.
1801class FPExtInst : public CastInst {
1802 FPExtInst(const FPExtInst &CI)
1803 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
1804 }
1805public:
1806 /// @brief Constructor with insert-before-instruction semantics
1807 FPExtInst(
1808 Value *S, ///< The value to be extended
1809 const Type *Ty, ///< The type to extend to
1810 const std::string &Name = "", ///< A name for the new instruction
1811 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1812 );
1813
1814 /// @brief Constructor with insert-at-end-of-block semantics
1815 FPExtInst(
1816 Value *S, ///< The value to be extended
1817 const Type *Ty, ///< The type to extend to
1818 const std::string &Name, ///< A name for the new instruction
1819 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1820 );
1821
1822 /// @brief Clone an identical FPExtInst
1823 virtual CastInst *clone() const;
1824
1825 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1826 static inline bool classof(const FPExtInst *) { return true; }
1827 static inline bool classof(const Instruction *I) {
1828 return I->getOpcode() == FPExt;
1829 }
1830 static inline bool classof(const Value *V) {
1831 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1832 }
1833};
1834
1835//===----------------------------------------------------------------------===//
1836// UIToFPInst Class
1837//===----------------------------------------------------------------------===//
1838
1839/// @brief This class represents a cast unsigned integer to floating point.
1840class UIToFPInst : public CastInst {
1841 UIToFPInst(const UIToFPInst &CI)
1842 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
1843 }
1844public:
1845 /// @brief Constructor with insert-before-instruction semantics
1846 UIToFPInst(
1847 Value *S, ///< The value to be converted
1848 const Type *Ty, ///< The type to convert to
1849 const std::string &Name = "", ///< A name for the new instruction
1850 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1851 );
1852
1853 /// @brief Constructor with insert-at-end-of-block semantics
1854 UIToFPInst(
1855 Value *S, ///< The value to be converted
1856 const Type *Ty, ///< The type to convert to
1857 const std::string &Name, ///< A name for the new instruction
1858 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1859 );
1860
1861 /// @brief Clone an identical UIToFPInst
1862 virtual CastInst *clone() const;
1863
1864 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1865 static inline bool classof(const UIToFPInst *) { return true; }
1866 static inline bool classof(const Instruction *I) {
1867 return I->getOpcode() == UIToFP;
1868 }
1869 static inline bool classof(const Value *V) {
1870 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1871 }
1872};
1873
1874//===----------------------------------------------------------------------===//
1875// SIToFPInst Class
1876//===----------------------------------------------------------------------===//
1877
1878/// @brief This class represents a cast from signed integer to floating point.
1879class SIToFPInst : public CastInst {
1880 SIToFPInst(const SIToFPInst &CI)
1881 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
1882 }
1883public:
1884 /// @brief Constructor with insert-before-instruction semantics
1885 SIToFPInst(
1886 Value *S, ///< The value to be converted
1887 const Type *Ty, ///< The type to convert to
1888 const std::string &Name = "", ///< A name for the new instruction
1889 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1890 );
1891
1892 /// @brief Constructor with insert-at-end-of-block semantics
1893 SIToFPInst(
1894 Value *S, ///< The value to be converted
1895 const Type *Ty, ///< The type to convert to
1896 const std::string &Name, ///< A name for the new instruction
1897 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1898 );
1899
1900 /// @brief Clone an identical SIToFPInst
1901 virtual CastInst *clone() const;
1902
1903 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1904 static inline bool classof(const SIToFPInst *) { return true; }
1905 static inline bool classof(const Instruction *I) {
1906 return I->getOpcode() == SIToFP;
1907 }
1908 static inline bool classof(const Value *V) {
1909 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1910 }
1911};
1912
1913//===----------------------------------------------------------------------===//
1914// FPToUIInst Class
1915//===----------------------------------------------------------------------===//
1916
1917/// @brief This class represents a cast from floating point to unsigned integer
1918class FPToUIInst : public CastInst {
1919 FPToUIInst(const FPToUIInst &CI)
1920 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
1921 }
1922public:
1923 /// @brief Constructor with insert-before-instruction semantics
1924 FPToUIInst(
1925 Value *S, ///< The value to be converted
1926 const Type *Ty, ///< The type to convert to
1927 const std::string &Name = "", ///< A name for the new instruction
1928 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1929 );
1930
1931 /// @brief Constructor with insert-at-end-of-block semantics
1932 FPToUIInst(
1933 Value *S, ///< The value to be converted
1934 const Type *Ty, ///< The type to convert to
1935 const std::string &Name, ///< A name for the new instruction
1936 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
1937 );
1938
1939 /// @brief Clone an identical FPToUIInst
1940 virtual CastInst *clone() const;
1941
1942 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1943 static inline bool classof(const FPToUIInst *) { return true; }
1944 static inline bool classof(const Instruction *I) {
1945 return I->getOpcode() == FPToUI;
1946 }
1947 static inline bool classof(const Value *V) {
1948 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1949 }
1950};
1951
1952//===----------------------------------------------------------------------===//
1953// FPToSIInst Class
1954//===----------------------------------------------------------------------===//
1955
1956/// @brief This class represents a cast from floating point to signed integer.
1957class FPToSIInst : public CastInst {
1958 FPToSIInst(const FPToSIInst &CI)
1959 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
1960 }
1961public:
1962 /// @brief Constructor with insert-before-instruction semantics
1963 FPToSIInst(
1964 Value *S, ///< The value to be converted
1965 const Type *Ty, ///< The type to convert to
1966 const std::string &Name = "", ///< A name for the new instruction
1967 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1968 );
1969
1970 /// @brief Constructor with insert-at-end-of-block semantics
1971 FPToSIInst(
1972 Value *S, ///< The value to be converted
1973 const Type *Ty, ///< The type to convert to
1974 const std::string &Name, ///< A name for the new instruction
1975 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1976 );
1977
1978 /// @brief Clone an identical FPToSIInst
1979 virtual CastInst *clone() const;
1980
1981 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1982 static inline bool classof(const FPToSIInst *) { return true; }
1983 static inline bool classof(const Instruction *I) {
1984 return I->getOpcode() == FPToSI;
1985 }
1986 static inline bool classof(const Value *V) {
1987 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1988 }
1989};
1990
1991//===----------------------------------------------------------------------===//
1992// IntToPtrInst Class
1993//===----------------------------------------------------------------------===//
1994
1995/// @brief This class represents a cast from an integer to a pointer.
1996class IntToPtrInst : public CastInst {
1997 IntToPtrInst(const IntToPtrInst &CI)
1998 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
1999 }
2000public:
2001 /// @brief Constructor with insert-before-instruction semantics
2002 IntToPtrInst(
2003 Value *S, ///< The value to be converted
2004 const Type *Ty, ///< The type to convert to
2005 const std::string &Name = "", ///< A name for the new instruction
2006 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2007 );
2008
2009 /// @brief Constructor with insert-at-end-of-block semantics
2010 IntToPtrInst(
2011 Value *S, ///< The value to be converted
2012 const Type *Ty, ///< The type to convert to
2013 const std::string &Name, ///< A name for the new instruction
2014 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2015 );
2016
2017 /// @brief Clone an identical IntToPtrInst
2018 virtual CastInst *clone() const;
2019
2020 // Methods for support type inquiry through isa, cast, and dyn_cast:
2021 static inline bool classof(const IntToPtrInst *) { return true; }
2022 static inline bool classof(const Instruction *I) {
2023 return I->getOpcode() == IntToPtr;
2024 }
2025 static inline bool classof(const Value *V) {
2026 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2027 }
2028};
2029
2030//===----------------------------------------------------------------------===//
2031// PtrToIntInst Class
2032//===----------------------------------------------------------------------===//
2033
2034/// @brief This class represents a cast from a pointer to an integer
2035class PtrToIntInst : public CastInst {
2036 PtrToIntInst(const PtrToIntInst &CI)
2037 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
2038 }
2039public:
2040 /// @brief Constructor with insert-before-instruction semantics
2041 PtrToIntInst(
2042 Value *S, ///< The value to be converted
2043 const Type *Ty, ///< The type to convert to
2044 const std::string &Name = "", ///< A name for the new instruction
2045 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2046 );
2047
2048 /// @brief Constructor with insert-at-end-of-block semantics
2049 PtrToIntInst(
2050 Value *S, ///< The value to be converted
2051 const Type *Ty, ///< The type to convert to
2052 const std::string &Name, ///< A name for the new instruction
2053 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2054 );
2055
2056 /// @brief Clone an identical PtrToIntInst
2057 virtual CastInst *clone() const;
2058
2059 // Methods for support type inquiry through isa, cast, and dyn_cast:
2060 static inline bool classof(const PtrToIntInst *) { return true; }
2061 static inline bool classof(const Instruction *I) {
2062 return I->getOpcode() == PtrToInt;
2063 }
2064 static inline bool classof(const Value *V) {
2065 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2066 }
2067};
2068
2069//===----------------------------------------------------------------------===//
2070// BitCastInst Class
2071//===----------------------------------------------------------------------===//
2072
2073/// @brief This class represents a no-op cast from one type to another.
2074class BitCastInst : public CastInst {
2075 BitCastInst(const BitCastInst &CI)
2076 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
2077 }
2078public:
2079 /// @brief Constructor with insert-before-instruction semantics
2080 BitCastInst(
2081 Value *S, ///< The value to be casted
2082 const Type *Ty, ///< The type to casted to
2083 const std::string &Name = "", ///< A name for the new instruction
2084 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2085 );
2086
2087 /// @brief Constructor with insert-at-end-of-block semantics
2088 BitCastInst(
2089 Value *S, ///< The value to be casted
2090 const Type *Ty, ///< The type to casted to
2091 const std::string &Name, ///< A name for the new instruction
2092 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2093 );
2094
2095 /// @brief Clone an identical BitCastInst
2096 virtual CastInst *clone() const;
2097
2098 // Methods for support type inquiry through isa, cast, and dyn_cast:
2099 static inline bool classof(const BitCastInst *) { return true; }
2100 static inline bool classof(const Instruction *I) {
2101 return I->getOpcode() == BitCast;
2102 }
2103 static inline bool classof(const Value *V) {
2104 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2105 }
2106};
2107
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002108} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00002109
2110#endif