blob: 49ec4f54fd5cc20227d648b7944b17901e2dca37 [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//
Chris Lattner7ed47a12007-12-29 19:59:42 +00005// This file is distributed under the University of Illinois Open Source
6// 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
David Greene52eec542007-08-01 03:43:44 +000019#include <iterator>
20
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000021#include "llvm/InstrTypes.h"
David Greene52eec542007-08-01 03:43:44 +000022#include "llvm/DerivedTypes.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000023
24namespace llvm {
25
Chris Lattner1fca5ff2004-10-27 16:14:51 +000026class BasicBlock;
Chris Lattnerd1a32602005-02-24 05:32:09 +000027class ConstantInt;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000028class PointerType;
Reid Spencer9d6565a2007-02-15 02:26:10 +000029class VectorType;
Reid Spencer3da43842007-02-28 22:00:54 +000030class ConstantRange;
31class APInt;
Reid Spencer4746ecf2007-04-09 15:01:12 +000032class ParamAttrsList;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000033
34//===----------------------------------------------------------------------===//
35// AllocationInst Class
36//===----------------------------------------------------------------------===//
37
38/// AllocationInst - This class is the common base class of MallocInst and
39/// AllocaInst.
40///
Chris Lattner454928e2005-01-29 00:31:36 +000041class AllocationInst : public UnaryInstruction {
Nate Begeman14b05292005-11-05 09:21:28 +000042 unsigned Alignment;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000043protected:
Nate Begeman14b05292005-11-05 09:21:28 +000044 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
Misha Brukman91102862005-03-16 03:46:55 +000045 const std::string &Name = "", Instruction *InsertBefore = 0);
Nate Begeman14b05292005-11-05 09:21:28 +000046 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
Misha Brukman91102862005-03-16 03:46:55 +000047 const std::string &Name, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000048public:
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000049 // Out of line virtual method, so the vtable, etc has a home.
50 virtual ~AllocationInst();
51
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000052 /// isArrayAllocation - Return true if there is an allocation size parameter
53 /// to the allocation instruction that is not 1.
54 ///
55 bool isArrayAllocation() const;
56
57 /// getArraySize - Get the number of element allocated, for a simple
58 /// allocation of a single element, this will return a constant 1 value.
59 ///
Chris Lattner454928e2005-01-29 00:31:36 +000060 inline const Value *getArraySize() const { return getOperand(0); }
61 inline Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000062
63 /// getType - Overload to return most specific pointer type
64 ///
65 inline const PointerType *getType() const {
Misha Brukman9769ab22005-04-21 20:19:05 +000066 return reinterpret_cast<const PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000067 }
68
69 /// getAllocatedType - Return the type that is being allocated by the
70 /// instruction.
71 ///
72 const Type *getAllocatedType() const;
73
Nate Begeman14b05292005-11-05 09:21:28 +000074 /// getAlignment - Return the alignment of the memory that is being allocated
75 /// by the instruction.
76 ///
77 unsigned getAlignment() const { return Alignment; }
Chris Lattner8ae779d2005-11-05 21:58:30 +000078 void setAlignment(unsigned Align) {
79 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
80 Alignment = Align;
81 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +000082
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000083 virtual Instruction *clone() const = 0;
84
85 // Methods for support type inquiry through isa, cast, and dyn_cast:
86 static inline bool classof(const AllocationInst *) { return true; }
87 static inline bool classof(const Instruction *I) {
88 return I->getOpcode() == Instruction::Alloca ||
89 I->getOpcode() == Instruction::Malloc;
90 }
91 static inline bool classof(const Value *V) {
92 return isa<Instruction>(V) && classof(cast<Instruction>(V));
93 }
94};
95
96
97//===----------------------------------------------------------------------===//
98// MallocInst Class
99//===----------------------------------------------------------------------===//
100
101/// MallocInst - an instruction to allocated memory on the heap
102///
103class MallocInst : public AllocationInst {
104 MallocInst(const MallocInst &MI);
105public:
106 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
107 const std::string &Name = "",
108 Instruction *InsertBefore = 0)
Nate Begeman14b05292005-11-05 09:21:28 +0000109 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000110 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
111 BasicBlock *InsertAtEnd)
Nate Begeman14b05292005-11-05 09:21:28 +0000112 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000113
114 MallocInst(const Type *Ty, const std::string &Name,
115 Instruction *InsertBefore = 0)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000116 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
117 MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
118 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000119
120 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Nate Begeman14b05292005-11-05 09:21:28 +0000121 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000122 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
123 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Nate Begeman14b05292005-11-05 09:21:28 +0000124 const std::string &Name = "",
125 Instruction *InsertBefore = 0)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000126 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000127
Chris Lattnerf319e832004-10-15 23:52:05 +0000128 virtual MallocInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000129
130 // Methods for support type inquiry through isa, cast, and dyn_cast:
131 static inline bool classof(const MallocInst *) { return true; }
132 static inline bool classof(const Instruction *I) {
133 return (I->getOpcode() == Instruction::Malloc);
134 }
135 static inline bool classof(const Value *V) {
136 return isa<Instruction>(V) && classof(cast<Instruction>(V));
137 }
138};
139
140
141//===----------------------------------------------------------------------===//
142// AllocaInst Class
143//===----------------------------------------------------------------------===//
144
145/// AllocaInst - an instruction to allocate memory on the stack
146///
147class AllocaInst : public AllocationInst {
148 AllocaInst(const AllocaInst &);
149public:
150 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
151 const std::string &Name = "",
152 Instruction *InsertBefore = 0)
Nate Begeman14b05292005-11-05 09:21:28 +0000153 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000154 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
155 BasicBlock *InsertAtEnd)
Nate Begeman14b05292005-11-05 09:21:28 +0000156 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
Chris Lattnerb77780e2006-05-10 04:38:35 +0000157
158 AllocaInst(const Type *Ty, const std::string &Name,
159 Instruction *InsertBefore = 0)
160 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
161 AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
162 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000163
Chris Lattnerb77780e2006-05-10 04:38:35 +0000164 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
165 const std::string &Name = "", Instruction *InsertBefore = 0)
166 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
Nate Begeman14b05292005-11-05 09:21:28 +0000167 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
168 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000169 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000170
Chris Lattnerf319e832004-10-15 23:52:05 +0000171 virtual AllocaInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000172
173 // Methods for support type inquiry through isa, cast, and dyn_cast:
174 static inline bool classof(const AllocaInst *) { return true; }
175 static inline bool classof(const Instruction *I) {
176 return (I->getOpcode() == Instruction::Alloca);
177 }
178 static inline bool classof(const Value *V) {
179 return isa<Instruction>(V) && classof(cast<Instruction>(V));
180 }
181};
182
183
184//===----------------------------------------------------------------------===//
185// FreeInst Class
186//===----------------------------------------------------------------------===//
187
188/// FreeInst - an instruction to deallocate memory
189///
Chris Lattner454928e2005-01-29 00:31:36 +0000190class FreeInst : public UnaryInstruction {
191 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000192public:
193 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
194 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
195
Chris Lattnerf319e832004-10-15 23:52:05 +0000196 virtual FreeInst *clone() const;
Owen Andersonc9edf0b2007-07-06 23:13:31 +0000197
198 // Accessor methods for consistency with other memory operations
199 Value *getPointerOperand() { return getOperand(0); }
200 const Value *getPointerOperand() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000201
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000202 // Methods for support type inquiry through isa, cast, and dyn_cast:
203 static inline bool classof(const FreeInst *) { return true; }
204 static inline bool classof(const Instruction *I) {
205 return (I->getOpcode() == Instruction::Free);
206 }
207 static inline bool classof(const Value *V) {
208 return isa<Instruction>(V) && classof(cast<Instruction>(V));
209 }
210};
211
212
213//===----------------------------------------------------------------------===//
214// LoadInst Class
215//===----------------------------------------------------------------------===//
216
Chris Lattner88fe29a2005-02-05 01:44:18 +0000217/// LoadInst - an instruction for reading from memory. This uses the
218/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000219///
Chris Lattner454928e2005-01-29 00:31:36 +0000220class LoadInst : public UnaryInstruction {
Christopher Lamb43c7f372007-04-22 19:24:39 +0000221
Chris Lattner454928e2005-01-29 00:31:36 +0000222 LoadInst(const LoadInst &LI)
Chris Lattner88fe29a2005-02-05 01:44:18 +0000223 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
224 setVolatile(LI.isVolatile());
Christopher Lamb43c7f372007-04-22 19:24:39 +0000225 setAlignment(LI.getAlignment());
Misha Brukman9769ab22005-04-21 20:19:05 +0000226
Chris Lattner454928e2005-01-29 00:31:36 +0000227#ifndef NDEBUG
228 AssertOK();
229#endif
230 }
231 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000232public:
233 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
234 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000235 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile = false,
236 Instruction *InsertBefore = 0);
237 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
Chris Lattnerf00042a2007-02-13 07:54:42 +0000238 Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000239 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
240 BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000241 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
242 BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000243
Chris Lattnerf00042a2007-02-13 07:54:42 +0000244 LoadInst(Value *Ptr, const char *Name, Instruction *InsertBefore);
245 LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAtEnd);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000246 explicit LoadInst(Value *Ptr, const char *Name = 0, bool isVolatile = false,
Chris Lattnerf00042a2007-02-13 07:54:42 +0000247 Instruction *InsertBefore = 0);
248 LoadInst(Value *Ptr, const char *Name, bool isVolatile,
249 BasicBlock *InsertAtEnd);
250
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000251 /// isVolatile - Return true if this is a load from a volatile memory
252 /// location.
253 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000254 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000255
256 /// setVolatile - Specify whether this is a volatile load or not.
257 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000258 void setVolatile(bool V) {
Hartmut Kaiserefd4a512007-10-17 14:56:40 +0000259 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000260 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000261
Chris Lattnerf319e832004-10-15 23:52:05 +0000262 virtual LoadInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000263
Christopher Lamb43c7f372007-04-22 19:24:39 +0000264 /// getAlignment - Return the alignment of the access that is being performed
265 ///
266 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000267 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000268 }
269
270 void setAlignment(unsigned Align);
271
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000272 Value *getPointerOperand() { return getOperand(0); }
273 const Value *getPointerOperand() const { return getOperand(0); }
274 static unsigned getPointerOperandIndex() { return 0U; }
275
276 // Methods for support type inquiry through isa, cast, and dyn_cast:
277 static inline bool classof(const LoadInst *) { return true; }
278 static inline bool classof(const Instruction *I) {
279 return I->getOpcode() == Instruction::Load;
280 }
281 static inline bool classof(const Value *V) {
282 return isa<Instruction>(V) && classof(cast<Instruction>(V));
283 }
284};
285
286
287//===----------------------------------------------------------------------===//
288// StoreInst Class
289//===----------------------------------------------------------------------===//
290
Misha Brukman9769ab22005-04-21 20:19:05 +0000291/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000292///
293class StoreInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000294 Use Ops[2];
Christopher Lamb43c7f372007-04-22 19:24:39 +0000295
Chris Lattner88fe29a2005-02-05 01:44:18 +0000296 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
Chris Lattner454928e2005-01-29 00:31:36 +0000297 Ops[0].init(SI.Ops[0], this);
298 Ops[1].init(SI.Ops[1], this);
Chris Lattner88fe29a2005-02-05 01:44:18 +0000299 setVolatile(SI.isVolatile());
Christopher Lamb43c7f372007-04-22 19:24:39 +0000300 setAlignment(SI.getAlignment());
301
Chris Lattner454928e2005-01-29 00:31:36 +0000302#ifndef NDEBUG
303 AssertOK();
304#endif
305 }
306 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000307public:
308 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
309 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
310 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
311 Instruction *InsertBefore = 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000312 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
313 unsigned Align, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000314 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000315 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
316 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000317
318
319 /// isVolatile - Return true if this is a load from a volatile memory
320 /// location.
321 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000322 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000323
324 /// setVolatile - Specify whether this is a volatile load or not.
325 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000326 void setVolatile(bool V) {
Hartmut Kaiserefd4a512007-10-17 14:56:40 +0000327 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000328 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000329
Chris Lattner454928e2005-01-29 00:31:36 +0000330 /// Transparently provide more efficient getOperand methods.
Misha Brukman9769ab22005-04-21 20:19:05 +0000331 Value *getOperand(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +0000332 assert(i < 2 && "getOperand() out of range!");
333 return Ops[i];
334 }
335 void setOperand(unsigned i, Value *Val) {
336 assert(i < 2 && "setOperand() out of range!");
337 Ops[i] = Val;
338 }
339 unsigned getNumOperands() const { return 2; }
340
Christopher Lamb43c7f372007-04-22 19:24:39 +0000341 /// getAlignment - Return the alignment of the access that is being performed
342 ///
343 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000344 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000345 }
346
347 void setAlignment(unsigned Align);
348
Chris Lattnerf319e832004-10-15 23:52:05 +0000349 virtual StoreInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000350
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000351 Value *getPointerOperand() { return getOperand(1); }
352 const Value *getPointerOperand() const { return getOperand(1); }
353 static unsigned getPointerOperandIndex() { return 1U; }
354
355 // Methods for support type inquiry through isa, cast, and dyn_cast:
356 static inline bool classof(const StoreInst *) { return true; }
357 static inline bool classof(const Instruction *I) {
358 return I->getOpcode() == Instruction::Store;
359 }
360 static inline bool classof(const Value *V) {
361 return isa<Instruction>(V) && classof(cast<Instruction>(V));
362 }
363};
364
365
366//===----------------------------------------------------------------------===//
367// GetElementPtrInst Class
368//===----------------------------------------------------------------------===//
369
David Greeneb8f74792007-09-04 15:46:09 +0000370// checkType - Simple wrapper function to give a better assertion failure
371// message on bad indexes for a gep instruction.
372//
373static inline const Type *checkType(const Type *Ty) {
374 assert(Ty && "Invalid GetElementPtrInst indices for type!");
375 return Ty;
376}
377
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000378/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
379/// access elements of arrays and structs
380///
381class GetElementPtrInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000382 GetElementPtrInst(const GetElementPtrInst &GEPI)
383 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
384 0, GEPI.getNumOperands()) {
385 Use *OL = OperandList = new Use[NumOperands];
386 Use *GEPIOL = GEPI.OperandList;
387 for (unsigned i = 0, E = NumOperands; i != E; ++i)
388 OL[i].init(GEPIOL[i], this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000389 }
Chris Lattner6ffbe172007-01-31 19:47:18 +0000390 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx);
Chris Lattner38bacf22005-05-03 05:43:30 +0000391 void init(Value *Ptr, Value *Idx);
David Greeneb8f74792007-09-04 15:46:09 +0000392
393 template<typename InputIterator>
394 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
395 const std::string &Name,
396 // This argument ensures that we have an iterator we can
397 // do arithmetic on in constant time
398 std::random_access_iterator_tag) {
399 typename std::iterator_traits<InputIterator>::difference_type NumIdx =
400 std::distance(IdxBegin, IdxEnd);
401
402 if (NumIdx > 0) {
403 // This requires that the itoerator points to contiguous memory.
404 init(Ptr, &*IdxBegin, NumIdx);
405 }
406 else {
407 init(Ptr, 0, NumIdx);
408 }
409
410 setName(Name);
411 }
412
413 /// getIndexedType - Returns the type of the element that would be loaded with
414 /// a load instruction with the specified parameters.
415 ///
416 /// A null type is returned if the indices are invalid for the specified
417 /// pointer type.
418 ///
419 static const Type *getIndexedType(const Type *Ptr,
420 Value* const *Idx, unsigned NumIdx,
421 bool AllowStructLeaf = false);
422
423 template<typename InputIterator>
424 static const Type *getIndexedType(const Type *Ptr,
425 InputIterator IdxBegin,
426 InputIterator IdxEnd,
427 bool AllowStructLeaf,
428 // This argument ensures that we
429 // have an iterator we can do
430 // arithmetic on in constant time
431 std::random_access_iterator_tag) {
432 typename std::iterator_traits<InputIterator>::difference_type NumIdx =
433 std::distance(IdxBegin, IdxEnd);
434
435 if (NumIdx > 0) {
436 // This requires that the iterator points to contiguous memory.
437 return(getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx,
438 AllowStructLeaf));
439 }
440 else {
441 return(getIndexedType(Ptr, (Value *const*)0, NumIdx, AllowStructLeaf));
442 }
443 }
444
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000445public:
446 /// Constructors - Create a getelementptr instruction with a base pointer an
447 /// list of indices. The first ctor can optionally insert before an existing
448 /// instruction, the second appends the new instruction to the specified
449 /// BasicBlock.
David Greeneb8f74792007-09-04 15:46:09 +0000450 template<typename InputIterator>
451 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
452 InputIterator IdxEnd,
453 const std::string &Name = "",
454 Instruction *InsertBefore =0)
455 : Instruction(PointerType::get(
456 checkType(getIndexedType(Ptr->getType(),
Christopher Lambfe63fb92007-12-11 08:59:05 +0000457 IdxBegin, IdxEnd, true)),
458 cast<PointerType>(Ptr->getType())->getAddressSpace()),
David Greeneb8f74792007-09-04 15:46:09 +0000459 GetElementPtr, 0, 0, InsertBefore) {
460 init(Ptr, IdxBegin, IdxEnd, Name,
461 typename std::iterator_traits<InputIterator>::iterator_category());
462 }
463 template<typename InputIterator>
464 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
465 const std::string &Name, BasicBlock *InsertAtEnd)
466 : Instruction(PointerType::get(
467 checkType(getIndexedType(Ptr->getType(),
Christopher Lambfe63fb92007-12-11 08:59:05 +0000468 IdxBegin, IdxEnd, true)),
469 cast<PointerType>(Ptr->getType())->getAddressSpace()),
David Greeneb8f74792007-09-04 15:46:09 +0000470 GetElementPtr, 0, 0, InsertAtEnd) {
471 init(Ptr, IdxBegin, IdxEnd, Name,
472 typename std::iterator_traits<InputIterator>::iterator_category());
473 }
474
Chris Lattner38bacf22005-05-03 05:43:30 +0000475 /// Constructors - These two constructors are convenience methods because one
476 /// and two index getelementptr instructions are so common.
477 GetElementPtrInst(Value *Ptr, Value *Idx,
478 const std::string &Name = "", Instruction *InsertBefore =0);
479 GetElementPtrInst(Value *Ptr, Value *Idx,
480 const std::string &Name, BasicBlock *InsertAtEnd);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000481 ~GetElementPtrInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000482
Chris Lattnerf319e832004-10-15 23:52:05 +0000483 virtual GetElementPtrInst *clone() const;
Misha Brukman9769ab22005-04-21 20:19:05 +0000484
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000485 // getType - Overload to return most specific pointer type...
486 inline const PointerType *getType() const {
487 return reinterpret_cast<const PointerType*>(Instruction::getType());
488 }
489
490 /// getIndexedType - Returns the type of the element that would be loaded with
491 /// a load instruction with the specified parameters.
492 ///
Misha Brukman9769ab22005-04-21 20:19:05 +0000493 /// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000494 /// pointer type.
495 ///
David Greeneb8f74792007-09-04 15:46:09 +0000496 template<typename InputIterator>
Misha Brukman9769ab22005-04-21 20:19:05 +0000497 static const Type *getIndexedType(const Type *Ptr,
David Greeneb8f74792007-09-04 15:46:09 +0000498 InputIterator IdxBegin,
499 InputIterator IdxEnd,
500 bool AllowStructLeaf = false) {
501 return(getIndexedType(Ptr, IdxBegin, IdxEnd, AllowStructLeaf,
502 typename std::iterator_traits<InputIterator>::
503 iterator_category()));
504 }
Chris Lattner38bacf22005-05-03 05:43:30 +0000505 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
Misha Brukman9769ab22005-04-21 20:19:05 +0000506
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000507 inline op_iterator idx_begin() { return op_begin()+1; }
508 inline const_op_iterator idx_begin() const { return op_begin()+1; }
509 inline op_iterator idx_end() { return op_end(); }
510 inline const_op_iterator idx_end() const { return op_end(); }
511
512 Value *getPointerOperand() {
513 return getOperand(0);
514 }
515 const Value *getPointerOperand() const {
516 return getOperand(0);
517 }
518 static unsigned getPointerOperandIndex() {
519 return 0U; // get index for modifying correct operand
520 }
521
522 inline unsigned getNumIndices() const { // Note: always non-negative
523 return getNumOperands() - 1;
524 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000525
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000526 inline bool hasIndices() const {
527 return getNumOperands() > 1;
528 }
Chris Lattner6f771d42007-04-14 00:12:57 +0000529
530 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
531 /// zeros. If so, the result pointer and the first operand have the same
532 /// value, just potentially different types.
533 bool hasAllZeroIndices() const;
Chris Lattner6b0974c2007-04-27 20:35:56 +0000534
535 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
536 /// constant integers. If so, the result pointer and the first operand have
537 /// a constant offset between them.
538 bool hasAllConstantIndices() const;
539
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000540
541 // Methods for support type inquiry through isa, cast, and dyn_cast:
542 static inline bool classof(const GetElementPtrInst *) { return true; }
543 static inline bool classof(const Instruction *I) {
544 return (I->getOpcode() == Instruction::GetElementPtr);
545 }
546 static inline bool classof(const Value *V) {
547 return isa<Instruction>(V) && classof(cast<Instruction>(V));
548 }
549};
550
551//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000552// ICmpInst Class
553//===----------------------------------------------------------------------===//
554
555/// This instruction compares its operands according to the predicate given
556/// to the constructor. It only operates on integers, pointers, or packed
557/// vectors of integrals. The two operands must be the same type.
558/// @brief Represent an integer comparison operator.
559class ICmpInst: public CmpInst {
560public:
561 /// This enumeration lists the possible predicates for the ICmpInst. The
562 /// values in the range 0-31 are reserved for FCmpInst while values in the
563 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
564 /// predicate values are not overlapping between the classes.
565 enum Predicate {
566 ICMP_EQ = 32, ///< equal
567 ICMP_NE = 33, ///< not equal
568 ICMP_UGT = 34, ///< unsigned greater than
569 ICMP_UGE = 35, ///< unsigned greater or equal
570 ICMP_ULT = 36, ///< unsigned less than
571 ICMP_ULE = 37, ///< unsigned less or equal
572 ICMP_SGT = 38, ///< signed greater than
573 ICMP_SGE = 39, ///< signed greater or equal
574 ICMP_SLT = 40, ///< signed less than
575 ICMP_SLE = 41, ///< signed less or equal
576 FIRST_ICMP_PREDICATE = ICMP_EQ,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000577 LAST_ICMP_PREDICATE = ICMP_SLE,
578 BAD_ICMP_PREDICATE = ICMP_SLE + 1
Reid Spencer45fb3f32006-11-20 01:22:35 +0000579 };
580
581 /// @brief Constructor with insert-before-instruction semantics.
582 ICmpInst(
583 Predicate pred, ///< The predicate to use for the comparison
584 Value *LHS, ///< The left-hand-side of the expression
585 Value *RHS, ///< The right-hand-side of the expression
586 const std::string &Name = "", ///< Name of the instruction
587 Instruction *InsertBefore = 0 ///< Where to insert
588 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertBefore) {
589 }
590
591 /// @brief Constructor with insert-at-block-end semantics.
592 ICmpInst(
593 Predicate pred, ///< The predicate to use for the comparison
594 Value *LHS, ///< The left-hand-side of the expression
595 Value *RHS, ///< The right-hand-side of the expression
596 const std::string &Name, ///< Name of the instruction
597 BasicBlock *InsertAtEnd ///< Block to insert into.
598 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertAtEnd) {
599 }
600
601 /// @brief Return the predicate for this instruction.
602 Predicate getPredicate() const { return Predicate(SubclassData); }
603
Chris Lattnerb769d562007-01-14 19:41:24 +0000604 /// @brief Set the predicate for this instruction to the specified value.
605 void setPredicate(Predicate P) { SubclassData = P; }
606
Reid Spencer45fb3f32006-11-20 01:22:35 +0000607 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
608 /// @returns the inverse predicate for the instruction's current predicate.
609 /// @brief Return the inverse of the instruction's predicate.
610 Predicate getInversePredicate() const {
611 return getInversePredicate(getPredicate());
612 }
613
614 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
615 /// @returns the inverse predicate for predicate provided in \p pred.
616 /// @brief Return the inverse of a given predicate
617 static Predicate getInversePredicate(Predicate pred);
618
619 /// For example, EQ->EQ, SLE->SGE, ULT->UGT, etc.
620 /// @returns the predicate that would be the result of exchanging the two
621 /// operands of the ICmpInst instruction without changing the result
622 /// produced.
623 /// @brief Return the predicate as if the operands were swapped
624 Predicate getSwappedPredicate() const {
625 return getSwappedPredicate(getPredicate());
626 }
627
628 /// This is a static version that you can use without an instruction
629 /// available.
630 /// @brief Return the predicate as if the operands were swapped.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000631 static Predicate getSwappedPredicate(Predicate pred);
632
633 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
634 /// @returns the predicate that would be the result if the operand were
635 /// regarded as signed.
636 /// @brief Return the signed version of the predicate
637 Predicate getSignedPredicate() const {
638 return getSignedPredicate(getPredicate());
639 }
640
641 /// This is a static version that you can use without an instruction.
642 /// @brief Return the signed version of the predicate.
643 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000644
Nick Lewycky4189a532008-01-28 03:48:02 +0000645 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
646 /// @returns the predicate that would be the result if the operand were
647 /// regarded as unsigned.
648 /// @brief Return the unsigned version of the predicate
649 Predicate getUnsignedPredicate() const {
650 return getUnsignedPredicate(getPredicate());
651 }
652
653 /// This is a static version that you can use without an instruction.
654 /// @brief Return the unsigned version of the predicate.
655 static Predicate getUnsignedPredicate(Predicate pred);
656
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000657 /// isEquality - Return true if this predicate is either EQ or NE. This also
658 /// tests for commutativity.
659 static bool isEquality(Predicate P) {
660 return P == ICMP_EQ || P == ICMP_NE;
661 }
662
663 /// isEquality - Return true if this predicate is either EQ or NE. This also
664 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000665 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000666 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000667 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000668
669 /// @returns true if the predicate of this ICmpInst is commutative
670 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000671 bool isCommutative() const { return isEquality(); }
672
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000673 /// isRelational - Return true if the predicate is relational (not EQ or NE).
674 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +0000675 bool isRelational() const {
676 return !isEquality();
677 }
678
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000679 /// isRelational - Return true if the predicate is relational (not EQ or NE).
680 ///
681 static bool isRelational(Predicate P) {
682 return !isEquality(P);
683 }
684
Reid Spencere4d87aa2006-12-23 06:05:41 +0000685 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
686 /// @brief Determine if this instruction's predicate is signed.
Chris Lattner5bda9e42007-09-15 06:51:03 +0000687 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000688
689 /// @returns true if the predicate provided is signed, false otherwise
690 /// @brief Determine if the predicate is signed.
691 static bool isSignedPredicate(Predicate pred);
692
Reid Spencer3da43842007-02-28 22:00:54 +0000693 /// Initialize a set of values that all satisfy the predicate with C.
694 /// @brief Make a ConstantRange for a relation with a constant value.
695 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
696
Reid Spencer45fb3f32006-11-20 01:22:35 +0000697 /// Exchange the two operands to this instruction in such a way that it does
698 /// not modify the semantics of the instruction. The predicate value may be
699 /// changed to retain the same result if the predicate is order dependent
700 /// (e.g. ult).
701 /// @brief Swap operands and adjust predicate.
702 void swapOperands() {
703 SubclassData = getSwappedPredicate();
704 std::swap(Ops[0], Ops[1]);
705 }
706
Chris Lattnercd406fe2007-08-24 20:48:18 +0000707 virtual ICmpInst *clone() const;
708
Reid Spencer45fb3f32006-11-20 01:22:35 +0000709 // Methods for support type inquiry through isa, cast, and dyn_cast:
710 static inline bool classof(const ICmpInst *) { return true; }
711 static inline bool classof(const Instruction *I) {
712 return I->getOpcode() == Instruction::ICmp;
713 }
714 static inline bool classof(const Value *V) {
715 return isa<Instruction>(V) && classof(cast<Instruction>(V));
716 }
717};
718
719//===----------------------------------------------------------------------===//
720// FCmpInst Class
721//===----------------------------------------------------------------------===//
722
723/// This instruction compares its operands according to the predicate given
724/// to the constructor. It only operates on floating point values or packed
725/// vectors of floating point values. The operands must be identical types.
726/// @brief Represents a floating point comparison operator.
727class FCmpInst: public CmpInst {
728public:
729 /// This enumeration lists the possible predicates for the FCmpInst. Values
730 /// in the range 0-31 are reserved for FCmpInst.
731 enum Predicate {
732 // Opcode U L G E Intuitive operation
733 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
734 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
735 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
736 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
737 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
738 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
739 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
740 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
741 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
742 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
743 FCMP_UGT =10, ///< 1 0 1 0 True if unordered or greater than
744 FCMP_UGE =11, ///< 1 0 1 1 True if unordered, greater than, or equal
745 FCMP_ULT =12, ///< 1 1 0 0 True if unordered or less than
746 FCMP_ULE =13, ///< 1 1 0 1 True if unordered, less than, or equal
747 FCMP_UNE =14, ///< 1 1 1 0 True if unordered or not equal
748 FCMP_TRUE =15, ///< 1 1 1 1 Always true (always folded)
749 FIRST_FCMP_PREDICATE = FCMP_FALSE,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000750 LAST_FCMP_PREDICATE = FCMP_TRUE,
751 BAD_FCMP_PREDICATE = FCMP_TRUE + 1
Reid Spencer45fb3f32006-11-20 01:22:35 +0000752 };
753
754 /// @brief Constructor with insert-before-instruction semantics.
755 FCmpInst(
756 Predicate pred, ///< The predicate to use for the comparison
757 Value *LHS, ///< The left-hand-side of the expression
758 Value *RHS, ///< The right-hand-side of the expression
759 const std::string &Name = "", ///< Name of the instruction
760 Instruction *InsertBefore = 0 ///< Where to insert
761 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertBefore) {
762 }
763
764 /// @brief Constructor with insert-at-block-end semantics.
765 FCmpInst(
766 Predicate pred, ///< The predicate to use for the comparison
767 Value *LHS, ///< The left-hand-side of the expression
768 Value *RHS, ///< The right-hand-side of the expression
769 const std::string &Name, ///< Name of the instruction
770 BasicBlock *InsertAtEnd ///< Block to insert into.
771 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertAtEnd) {
772 }
773
774 /// @brief Return the predicate for this instruction.
775 Predicate getPredicate() const { return Predicate(SubclassData); }
776
Chris Lattnerb769d562007-01-14 19:41:24 +0000777 /// @brief Set the predicate for this instruction to the specified value.
778 void setPredicate(Predicate P) { SubclassData = P; }
779
Reid Spencer45fb3f32006-11-20 01:22:35 +0000780 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
781 /// @returns the inverse predicate for the instructions current predicate.
782 /// @brief Return the inverse of the predicate
783 Predicate getInversePredicate() const {
784 return getInversePredicate(getPredicate());
785 }
786
787 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
788 /// @returns the inverse predicate for \p pred.
789 /// @brief Return the inverse of a given predicate
790 static Predicate getInversePredicate(Predicate pred);
791
792 /// For example, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
793 /// @returns the predicate that would be the result of exchanging the two
794 /// operands of the ICmpInst instruction without changing the result
795 /// produced.
796 /// @brief Return the predicate as if the operands were swapped
797 Predicate getSwappedPredicate() const {
798 return getSwappedPredicate(getPredicate());
799 }
800
801 /// This is a static version that you can use without an instruction
802 /// available.
803 /// @brief Return the predicate as if the operands were swapped.
804 static Predicate getSwappedPredicate(Predicate Opcode);
805
806 /// This also tests for commutativity. If isEquality() returns true then
807 /// the predicate is also commutative. Only the equality predicates are
808 /// commutative.
809 /// @returns true if the predicate of this instruction is EQ or NE.
810 /// @brief Determine if this is an equality predicate.
811 bool isEquality() const {
812 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
813 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
814 }
815 bool isCommutative() const { return isEquality(); }
816
817 /// @returns true if the predicate is relational (not EQ or NE).
818 /// @brief Determine if this a relational predicate.
819 bool isRelational() const { return !isEquality(); }
820
821 /// Exchange the two operands to this instruction in such a way that it does
822 /// not modify the semantics of the instruction. The predicate value may be
823 /// changed to retain the same result if the predicate is order dependent
824 /// (e.g. ult).
825 /// @brief Swap operands and adjust predicate.
826 void swapOperands() {
827 SubclassData = getSwappedPredicate();
828 std::swap(Ops[0], Ops[1]);
829 }
830
Chris Lattnercd406fe2007-08-24 20:48:18 +0000831 virtual FCmpInst *clone() const;
832
Reid Spencer45fb3f32006-11-20 01:22:35 +0000833 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
834 static inline bool classof(const FCmpInst *) { return true; }
835 static inline bool classof(const Instruction *I) {
836 return I->getOpcode() == Instruction::FCmp;
837 }
838 static inline bool classof(const Value *V) {
839 return isa<Instruction>(V) && classof(cast<Instruction>(V));
840 }
841};
842
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000843//===----------------------------------------------------------------------===//
844// CallInst Class
845//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000846/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +0000847/// machine's calling convention. This class uses low bit of the SubClassData
848/// field to indicate whether or not this is a tail call. The rest of the bits
849/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000850///
David Greene52eec542007-08-01 03:43:44 +0000851
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000852class CallInst : public Instruction {
Duncan Sandsdc024672007-11-27 13:23:08 +0000853 const ParamAttrsList *ParamAttrs; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000854 CallInst(const CallInst &CI);
Chris Lattnerd54f4322007-02-13 00:58:44 +0000855 void init(Value *Func, Value* const *Params, unsigned NumParams);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000856 void init(Value *Func, Value *Actual1, Value *Actual2);
857 void init(Value *Func, Value *Actual);
858 void init(Value *Func);
859
David Greene52eec542007-08-01 03:43:44 +0000860 template<typename InputIterator>
861 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
862 const std::string &Name,
863 // This argument ensures that we have an iterator we can
864 // do arithmetic on in constant time
865 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000866 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greene52eec542007-08-01 03:43:44 +0000867
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000868 // This requires that the iterator points to contiguous memory.
869 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
David Greene52eec542007-08-01 03:43:44 +0000870 setName(Name);
871 }
872
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000873public:
David Greene52eec542007-08-01 03:43:44 +0000874 /// Construct a CallInst given a range of arguments. InputIterator
875 /// must be a random-access iterator pointing to contiguous storage
876 /// (e.g. a std::vector<>::iterator). Checks are made for
877 /// random-accessness but not for contiguous storage as that would
878 /// incur runtime overhead.
879 /// @brief Construct a CallInst from a range of arguments
880 template<typename InputIterator>
881 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
882 const std::string &Name = "", Instruction *InsertBefore = 0)
883 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
884 ->getElementType())->getReturnType(),
885 Instruction::Call, 0, 0, InsertBefore) {
886 init(Func, ArgBegin, ArgEnd, Name,
887 typename std::iterator_traits<InputIterator>::iterator_category());
888 }
889
890 /// Construct a CallInst given a range of arguments. InputIterator
891 /// must be a random-access iterator pointing to contiguous storage
892 /// (e.g. a std::vector<>::iterator). Checks are made for
893 /// random-accessness but not for contiguous storage as that would
894 /// incur runtime overhead.
895 /// @brief Construct a CallInst from a range of arguments
896 template<typename InputIterator>
897 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
898 const std::string &Name, BasicBlock *InsertAtEnd)
899 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
900 ->getElementType())->getReturnType(),
901 Instruction::Call, 0, 0, InsertAtEnd) {
902 init(Func, ArgBegin, ArgEnd, Name,
903 typename std::iterator_traits<InputIterator>::iterator_category());
904 }
905
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000906 CallInst(Value *F, Value *Actual, const std::string& Name = "",
907 Instruction *InsertBefore = 0);
908 CallInst(Value *F, Value *Actual, const std::string& Name,
909 BasicBlock *InsertAtEnd);
Misha Brukman9769ab22005-04-21 20:19:05 +0000910 explicit CallInst(Value *F, const std::string &Name = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000911 Instruction *InsertBefore = 0);
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000912 CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000913 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000914
Chris Lattnerf319e832004-10-15 23:52:05 +0000915 virtual CallInst *clone() const;
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000916
Chris Lattner3340ffe2005-05-06 20:26:26 +0000917 bool isTailCall() const { return SubclassData & 1; }
918 void setTailCall(bool isTailCall = true) {
Jeff Cohen39cef602005-05-07 02:44:04 +0000919 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
Chris Lattner3340ffe2005-05-06 20:26:26 +0000920 }
921
922 /// getCallingConv/setCallingConv - Get or set the calling convention of this
923 /// function call.
924 unsigned getCallingConv() const { return SubclassData >> 1; }
925 void setCallingConv(unsigned CC) {
926 SubclassData = (SubclassData & 1) | (CC << 1);
927 }
Chris Lattnerddb6db42005-05-06 05:51:46 +0000928
Reid Spencerfa3e9122007-04-09 18:00:57 +0000929 /// Obtains a pointer to the ParamAttrsList object which holds the
930 /// parameter attributes information, if any.
931 /// @returns 0 if no attributes have been set.
Reid Spencer4746ecf2007-04-09 15:01:12 +0000932 /// @brief Get the parameter attributes.
Duncan Sandsdc024672007-11-27 13:23:08 +0000933 const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
Reid Spencer4746ecf2007-04-09 15:01:12 +0000934
935 /// Sets the parameter attributes for this CallInst. To construct a
936 /// ParamAttrsList, see ParameterAttributes.h
937 /// @brief Set the parameter attributes.
Duncan Sandsdc024672007-11-27 13:23:08 +0000938 void setParamAttrs(const ParamAttrsList *attrs);
939
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000940 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000941 bool paramHasAttr(uint16_t i, unsigned attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000942
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000943 /// @brief Determine if the call does not access memory.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000944 bool doesNotAccessMemory() const;
945
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000946 /// @brief Determine if the call does not access or only reads memory.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000947 bool onlyReadsMemory() const;
948
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000949 /// @brief Determine if the call cannot return.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000950 bool doesNotReturn() const;
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000951
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000952 /// @brief Determine if the call cannot unwind.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000953 bool doesNotThrow() const;
Duncan Sandsf0c33542007-12-19 21:13:37 +0000954 void setDoesNotThrow(bool doesNotThrow = true);
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000955
Duncan Sandsdc024672007-11-27 13:23:08 +0000956 /// @brief Determine if the call returns a structure.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000957 bool isStructReturn() const;
Reid Spencer4746ecf2007-04-09 15:01:12 +0000958
Evan Chengf4a54982008-01-12 18:57:32 +0000959 /// @brief Determine if any call argument is an aggregate passed by value.
960 bool hasByValArgument() const;
961
Chris Lattner721aef62004-11-18 17:46:57 +0000962 /// getCalledFunction - Return the function being called by this instruction
963 /// if it is a direct call. If it is a call through a function pointer,
964 /// return null.
965 Function *getCalledFunction() const {
Dan Gohman11a7dbf2007-09-24 15:46:02 +0000966 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +0000967 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000968
Reid Spencerc25ec252006-12-29 04:10:59 +0000969 /// getCalledValue - Get a pointer to the function that is invoked by this
970 /// instruction
Chris Lattner454928e2005-01-29 00:31:36 +0000971 inline const Value *getCalledValue() const { return getOperand(0); }
972 inline Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000973
974 // Methods for support type inquiry through isa, cast, and dyn_cast:
975 static inline bool classof(const CallInst *) { return true; }
976 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +0000977 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000978 }
979 static inline bool classof(const Value *V) {
980 return isa<Instruction>(V) && classof(cast<Instruction>(V));
981 }
982};
983
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000984//===----------------------------------------------------------------------===//
985// SelectInst Class
986//===----------------------------------------------------------------------===//
987
988/// SelectInst - This class represents the LLVM 'select' instruction.
989///
990class SelectInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000991 Use Ops[3];
992
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000993 void init(Value *C, Value *S1, Value *S2) {
Chris Lattner454928e2005-01-29 00:31:36 +0000994 Ops[0].init(C, this);
995 Ops[1].init(S1, this);
996 Ops[2].init(S2, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000997 }
998
Chris Lattner454928e2005-01-29 00:31:36 +0000999 SelectInst(const SelectInst &SI)
1000 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
1001 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
1002 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001003public:
1004 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
1005 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001006 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001007 init(C, S1, S2);
Chris Lattner910c80a2007-02-24 00:55:48 +00001008 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001009 }
1010 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1011 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001012 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001013 init(C, S1, S2);
Chris Lattner910c80a2007-02-24 00:55:48 +00001014 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001015 }
1016
Chris Lattner454928e2005-01-29 00:31:36 +00001017 Value *getCondition() const { return Ops[0]; }
1018 Value *getTrueValue() const { return Ops[1]; }
1019 Value *getFalseValue() const { return Ops[2]; }
1020
1021 /// Transparently provide more efficient getOperand methods.
1022 Value *getOperand(unsigned i) const {
1023 assert(i < 3 && "getOperand() out of range!");
1024 return Ops[i];
1025 }
1026 void setOperand(unsigned i, Value *Val) {
1027 assert(i < 3 && "setOperand() out of range!");
1028 Ops[i] = Val;
1029 }
1030 unsigned getNumOperands() const { return 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001031
1032 OtherOps getOpcode() const {
1033 return static_cast<OtherOps>(Instruction::getOpcode());
1034 }
1035
Chris Lattnerf319e832004-10-15 23:52:05 +00001036 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001037
1038 // Methods for support type inquiry through isa, cast, and dyn_cast:
1039 static inline bool classof(const SelectInst *) { return true; }
1040 static inline bool classof(const Instruction *I) {
1041 return I->getOpcode() == Instruction::Select;
1042 }
1043 static inline bool classof(const Value *V) {
1044 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1045 }
1046};
1047
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001048//===----------------------------------------------------------------------===//
1049// VAArgInst Class
1050//===----------------------------------------------------------------------===//
1051
1052/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001053/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001054///
Chris Lattner454928e2005-01-29 00:31:36 +00001055class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001056 VAArgInst(const VAArgInst &VAA)
Chris Lattner454928e2005-01-29 00:31:36 +00001057 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001058public:
1059 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
1060 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001061 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001062 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001063 }
1064 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
1065 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001066 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001067 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001068 }
1069
Chris Lattnerf319e832004-10-15 23:52:05 +00001070 virtual VAArgInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001071
1072 // Methods for support type inquiry through isa, cast, and dyn_cast:
1073 static inline bool classof(const VAArgInst *) { return true; }
1074 static inline bool classof(const Instruction *I) {
1075 return I->getOpcode() == VAArg;
1076 }
1077 static inline bool classof(const Value *V) {
1078 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1079 }
1080};
1081
1082//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001083// ExtractElementInst Class
1084//===----------------------------------------------------------------------===//
1085
1086/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001087/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001088///
1089class ExtractElementInst : public Instruction {
1090 Use Ops[2];
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001091 ExtractElementInst(const ExtractElementInst &EE) :
Robert Bocchinof9993442006-01-17 20:05:59 +00001092 Instruction(EE.getType(), ExtractElement, Ops, 2) {
1093 Ops[0].init(EE.Ops[0], this);
1094 Ops[1].init(EE.Ops[1], this);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001095 }
1096
1097public:
Chris Lattner9fc18d22006-04-08 01:15:18 +00001098 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
1099 Instruction *InsertBefore = 0);
Chris Lattner06a248c22006-10-05 06:24:58 +00001100 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
1101 Instruction *InsertBefore = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001102 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
1103 BasicBlock *InsertAtEnd);
Chris Lattner06a248c22006-10-05 06:24:58 +00001104 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
1105 BasicBlock *InsertAtEnd);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001106
Chris Lattnerfa495842006-04-08 04:04:54 +00001107 /// isValidOperands - Return true if an extractelement instruction can be
1108 /// formed with the specified operands.
1109 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001110
Robert Bocchino49b78a52006-01-10 19:04:13 +00001111 virtual ExtractElementInst *clone() const;
1112
Robert Bocchino49b78a52006-01-10 19:04:13 +00001113 /// Transparently provide more efficient getOperand methods.
1114 Value *getOperand(unsigned i) const {
1115 assert(i < 2 && "getOperand() out of range!");
1116 return Ops[i];
1117 }
1118 void setOperand(unsigned i, Value *Val) {
1119 assert(i < 2 && "setOperand() out of range!");
1120 Ops[i] = Val;
1121 }
1122 unsigned getNumOperands() const { return 2; }
1123
1124 // Methods for support type inquiry through isa, cast, and dyn_cast:
1125 static inline bool classof(const ExtractElementInst *) { return true; }
1126 static inline bool classof(const Instruction *I) {
1127 return I->getOpcode() == Instruction::ExtractElement;
1128 }
1129 static inline bool classof(const Value *V) {
1130 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1131 }
1132};
1133
1134//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001135// InsertElementInst Class
1136//===----------------------------------------------------------------------===//
1137
1138/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001139/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001140///
1141class InsertElementInst : public Instruction {
1142 Use Ops[3];
Chris Lattner6a56ed42006-04-14 22:20:07 +00001143 InsertElementInst(const InsertElementInst &IE);
Robert Bocchinof9993442006-01-17 20:05:59 +00001144public:
Chris Lattner9fc18d22006-04-08 01:15:18 +00001145 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1146 const std::string &Name = "",Instruction *InsertBefore = 0);
Chris Lattner06a248c22006-10-05 06:24:58 +00001147 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1148 const std::string &Name = "",Instruction *InsertBefore = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001149 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Robert Bocchinof9993442006-01-17 20:05:59 +00001150 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattner06a248c22006-10-05 06:24:58 +00001151 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1152 const std::string &Name, BasicBlock *InsertAtEnd);
Robert Bocchinof9993442006-01-17 20:05:59 +00001153
Chris Lattnerfa495842006-04-08 04:04:54 +00001154 /// isValidOperands - Return true if an insertelement instruction can be
1155 /// formed with the specified operands.
1156 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1157 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001158
Robert Bocchinof9993442006-01-17 20:05:59 +00001159 virtual InsertElementInst *clone() const;
1160
Reid Spencerac9dcb92007-02-15 03:39:18 +00001161 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001162 ///
Reid Spencer9d6565a2007-02-15 02:26:10 +00001163 inline const VectorType *getType() const {
1164 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001165 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001166
Robert Bocchinof9993442006-01-17 20:05:59 +00001167 /// Transparently provide more efficient getOperand methods.
1168 Value *getOperand(unsigned i) const {
1169 assert(i < 3 && "getOperand() out of range!");
1170 return Ops[i];
1171 }
1172 void setOperand(unsigned i, Value *Val) {
1173 assert(i < 3 && "setOperand() out of range!");
1174 Ops[i] = Val;
1175 }
1176 unsigned getNumOperands() const { return 3; }
1177
1178 // Methods for support type inquiry through isa, cast, and dyn_cast:
1179 static inline bool classof(const InsertElementInst *) { return true; }
1180 static inline bool classof(const Instruction *I) {
1181 return I->getOpcode() == Instruction::InsertElement;
1182 }
1183 static inline bool classof(const Value *V) {
1184 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1185 }
1186};
1187
1188//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001189// ShuffleVectorInst Class
1190//===----------------------------------------------------------------------===//
1191
1192/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1193/// input vectors.
1194///
1195class ShuffleVectorInst : public Instruction {
1196 Use Ops[3];
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001197 ShuffleVectorInst(const ShuffleVectorInst &IE);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001198public:
1199 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1200 const std::string &Name = "", Instruction *InsertBefor = 0);
1201 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1202 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001203
Chris Lattnerfa495842006-04-08 04:04:54 +00001204 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001205 /// formed with the specified operands.
1206 static bool isValidOperands(const Value *V1, const Value *V2,
1207 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001208
Chris Lattner9fc18d22006-04-08 01:15:18 +00001209 virtual ShuffleVectorInst *clone() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001210
Reid Spencerac9dcb92007-02-15 03:39:18 +00001211 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001212 ///
Reid Spencer9d6565a2007-02-15 02:26:10 +00001213 inline const VectorType *getType() const {
1214 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001215 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001216
Chris Lattner9fc18d22006-04-08 01:15:18 +00001217 /// Transparently provide more efficient getOperand methods.
1218 Value *getOperand(unsigned i) const {
1219 assert(i < 3 && "getOperand() out of range!");
1220 return Ops[i];
1221 }
1222 void setOperand(unsigned i, Value *Val) {
1223 assert(i < 3 && "setOperand() out of range!");
1224 Ops[i] = Val;
1225 }
1226 unsigned getNumOperands() const { return 3; }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001227
Chris Lattner9fc18d22006-04-08 01:15:18 +00001228 // Methods for support type inquiry through isa, cast, and dyn_cast:
1229 static inline bool classof(const ShuffleVectorInst *) { return true; }
1230 static inline bool classof(const Instruction *I) {
1231 return I->getOpcode() == Instruction::ShuffleVector;
1232 }
1233 static inline bool classof(const Value *V) {
1234 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1235 }
1236};
1237
1238
1239//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001240// PHINode Class
1241//===----------------------------------------------------------------------===//
1242
1243// PHINode - The PHINode class is used to represent the magical mystical PHI
1244// node, that can not exist in nature, but can be synthesized in a computer
1245// scientist's overactive imagination.
1246//
1247class PHINode : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +00001248 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1249 /// the number actually in use.
1250 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001251 PHINode(const PHINode &PN);
1252public:
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001253 explicit PHINode(const Type *Ty, const std::string &Name = "",
1254 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001255 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Chris Lattner454928e2005-01-29 00:31:36 +00001256 ReservedSpace(0) {
Chris Lattner910c80a2007-02-24 00:55:48 +00001257 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001258 }
1259
1260 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001261 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Chris Lattner454928e2005-01-29 00:31:36 +00001262 ReservedSpace(0) {
Chris Lattner910c80a2007-02-24 00:55:48 +00001263 setName(Name);
Chris Lattner454928e2005-01-29 00:31:36 +00001264 }
1265
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001266 ~PHINode();
1267
Chris Lattner454928e2005-01-29 00:31:36 +00001268 /// reserveOperandSpace - This method can be used to avoid repeated
1269 /// reallocation of PHI operand lists by reserving space for the correct
1270 /// number of operands before adding them. Unlike normal vector reserves,
1271 /// this method can also be used to trim the operand space.
1272 void reserveOperandSpace(unsigned NumValues) {
1273 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001274 }
1275
Chris Lattnerf319e832004-10-15 23:52:05 +00001276 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001277
1278 /// getNumIncomingValues - Return the number of incoming edges
1279 ///
Chris Lattner454928e2005-01-29 00:31:36 +00001280 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001281
Reid Spencerc773de62006-05-19 19:07:54 +00001282 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001283 ///
1284 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001285 assert(i*2 < getNumOperands() && "Invalid value number!");
1286 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001287 }
1288 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +00001289 assert(i*2 < getNumOperands() && "Invalid value number!");
1290 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001291 }
Chris Lattner454928e2005-01-29 00:31:36 +00001292 unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001293 return i*2;
1294 }
1295
Reid Spencerc773de62006-05-19 19:07:54 +00001296 /// getIncomingBlock - Return incoming basic block number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001297 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00001298 BasicBlock *getIncomingBlock(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001299 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001300 }
1301 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Chris Lattner454928e2005-01-29 00:31:36 +00001302 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001303 }
1304 unsigned getOperandNumForIncomingBlock(unsigned i) {
1305 return i*2+1;
1306 }
1307
1308 /// addIncoming - Add an incoming value to the end of the PHI list
1309 ///
1310 void addIncoming(Value *V, BasicBlock *BB) {
1311 assert(getType() == V->getType() &&
1312 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +00001313 unsigned OpNo = NumOperands;
1314 if (OpNo+2 > ReservedSpace)
1315 resizeOperands(0); // Get more space!
1316 // Initialize some new operands.
1317 NumOperands = OpNo+2;
1318 OperandList[OpNo].init(V, this);
1319 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001320 }
Misha Brukman9769ab22005-04-21 20:19:05 +00001321
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001322 /// removeIncomingValue - Remove an incoming value. This is useful if a
1323 /// predecessor basic block is deleted. The value removed is returned.
1324 ///
1325 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1326 /// is true), the PHI node is destroyed and any uses of it are replaced with
1327 /// dummy values. The only time there should be zero incoming values to a PHI
1328 /// node is when the block is dead, so this strategy is sound.
1329 ///
1330 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1331
1332 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1333 int Idx = getBasicBlockIndex(BB);
1334 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1335 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1336 }
1337
Misha Brukman9769ab22005-04-21 20:19:05 +00001338 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001339 /// block in the value list for this PHI. Returns -1 if no instance.
1340 ///
1341 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001342 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00001343 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Chris Lattner454928e2005-01-29 00:31:36 +00001344 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001345 return -1;
1346 }
1347
1348 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1349 return getIncomingValue(getBasicBlockIndex(BB));
1350 }
1351
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001352 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00001353 /// same value, return the value, otherwise return null.
1354 ///
Chris Lattner9acbd612005-08-05 00:49:06 +00001355 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001356
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001357 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1358 static inline bool classof(const PHINode *) { return true; }
1359 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001360 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001361 }
1362 static inline bool classof(const Value *V) {
1363 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1364 }
Chris Lattner454928e2005-01-29 00:31:36 +00001365 private:
1366 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001367};
1368
1369//===----------------------------------------------------------------------===//
1370// ReturnInst Class
1371//===----------------------------------------------------------------------===//
1372
1373//===---------------------------------------------------------------------------
1374/// ReturnInst - Return a value (possibly void), from a function. Execution
1375/// does not continue in this function any longer.
1376///
1377class ReturnInst : public TerminatorInst {
Chris Lattner910c80a2007-02-24 00:55:48 +00001378 Use RetVal; // Return Value: null if 'void'.
1379 ReturnInst(const ReturnInst &RI);
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001380 void init(Value *RetVal);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001381
1382public:
1383 // ReturnInst constructors:
1384 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001385 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001386 // ReturnInst(Value* X) - 'ret X' instruction
1387 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
1388 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1389 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
1390 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001391 //
1392 // NOTE: If the Value* passed is of type void then the constructor behaves as
1393 // if it was passed NULL.
Chris Lattner910c80a2007-02-24 00:55:48 +00001394 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1395 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
1396 explicit ReturnInst(BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001397
Chris Lattnerf319e832004-10-15 23:52:05 +00001398 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001399
Chris Lattner454928e2005-01-29 00:31:36 +00001400 // Transparently provide more efficient getOperand methods.
1401 Value *getOperand(unsigned i) const {
1402 assert(i < getNumOperands() && "getOperand() out of range!");
1403 return RetVal;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001404 }
Chris Lattner454928e2005-01-29 00:31:36 +00001405 void setOperand(unsigned i, Value *Val) {
1406 assert(i < getNumOperands() && "setOperand() out of range!");
1407 RetVal = Val;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001408 }
1409
Chris Lattner454928e2005-01-29 00:31:36 +00001410 Value *getReturnValue() const { return RetVal; }
1411
1412 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001413
1414 // Methods for support type inquiry through isa, cast, and dyn_cast:
1415 static inline bool classof(const ReturnInst *) { return true; }
1416 static inline bool classof(const Instruction *I) {
1417 return (I->getOpcode() == Instruction::Ret);
1418 }
1419 static inline bool classof(const Value *V) {
1420 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1421 }
Chris Lattner454928e2005-01-29 00:31:36 +00001422 private:
1423 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1424 virtual unsigned getNumSuccessorsV() const;
1425 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001426};
1427
1428//===----------------------------------------------------------------------===//
1429// BranchInst Class
1430//===----------------------------------------------------------------------===//
1431
1432//===---------------------------------------------------------------------------
1433/// BranchInst - Conditional or Unconditional Branch instruction.
1434///
1435class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001436 /// Ops list - Branches are strange. The operands are ordered:
1437 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
1438 /// they don't have to check for cond/uncond branchness.
1439 Use Ops[3];
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001440 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00001441 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001442public:
1443 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1444 // BranchInst(BB *B) - 'br B'
1445 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1446 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1447 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1448 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1449 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00001450 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001451 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00001452 Instruction *InsertBefore = 0);
1453 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001454 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00001455 BasicBlock *InsertAtEnd);
Chris Lattner454928e2005-01-29 00:31:36 +00001456
1457 /// Transparently provide more efficient getOperand methods.
1458 Value *getOperand(unsigned i) const {
1459 assert(i < getNumOperands() && "getOperand() out of range!");
1460 return Ops[i];
1461 }
1462 void setOperand(unsigned i, Value *Val) {
1463 assert(i < getNumOperands() && "setOperand() out of range!");
1464 Ops[i] = Val;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001465 }
1466
Chris Lattnerf319e832004-10-15 23:52:05 +00001467 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001468
Chris Lattner454928e2005-01-29 00:31:36 +00001469 inline bool isUnconditional() const { return getNumOperands() == 1; }
1470 inline bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001471
1472 inline Value *getCondition() const {
1473 assert(isConditional() && "Cannot get condition of an uncond branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001474 return getOperand(2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001475 }
1476
1477 void setCondition(Value *V) {
1478 assert(isConditional() && "Cannot set condition of unconditional branch!");
1479 setOperand(2, V);
1480 }
1481
1482 // setUnconditionalDest - Change the current branch to an unconditional branch
1483 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +00001484 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001485 void setUnconditionalDest(BasicBlock *Dest) {
Chris Lattner454928e2005-01-29 00:31:36 +00001486 if (isConditional()) { // Convert this to an uncond branch.
1487 NumOperands = 1;
1488 Ops[1].set(0);
1489 Ops[2].set(0);
1490 }
1491 setOperand(0, reinterpret_cast<Value*>(Dest));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001492 }
1493
Chris Lattner454928e2005-01-29 00:31:36 +00001494 unsigned getNumSuccessors() const { return 1+isConditional(); }
1495
1496 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001497 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Dan Gohmanb96039e2007-05-11 20:59:29 +00001498 return cast<BasicBlock>(getOperand(i));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001499 }
1500
Chris Lattner454928e2005-01-29 00:31:36 +00001501 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001502 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001503 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001504 }
1505
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001506 // Methods for support type inquiry through isa, cast, and dyn_cast:
1507 static inline bool classof(const BranchInst *) { return true; }
1508 static inline bool classof(const Instruction *I) {
1509 return (I->getOpcode() == Instruction::Br);
1510 }
1511 static inline bool classof(const Value *V) {
1512 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1513 }
Chris Lattner454928e2005-01-29 00:31:36 +00001514private:
1515 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1516 virtual unsigned getNumSuccessorsV() const;
1517 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001518};
1519
1520//===----------------------------------------------------------------------===//
1521// SwitchInst Class
1522//===----------------------------------------------------------------------===//
1523
1524//===---------------------------------------------------------------------------
1525/// SwitchInst - Multiway switch
1526///
1527class SwitchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001528 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001529 // Operand[0] = Value to switch on
1530 // Operand[1] = Default basic block destination
1531 // Operand[2n ] = Value to match
1532 // Operand[2n+1] = BasicBlock to go to on match
1533 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00001534 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1535 void resizeOperands(unsigned No);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001536public:
Chris Lattner454928e2005-01-29 00:31:36 +00001537 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1538 /// switch on and a default destination. The number of additional cases can
1539 /// be specified here to make memory allocation more efficient. This
1540 /// constructor can also autoinsert before another instruction.
1541 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00001542 Instruction *InsertBefore = 0);
1543
Chris Lattner454928e2005-01-29 00:31:36 +00001544 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1545 /// switch on and a default destination. The number of additional cases can
1546 /// be specified here to make memory allocation more efficient. This
1547 /// constructor also autoinserts at the end of the specified BasicBlock.
1548 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00001549 BasicBlock *InsertAtEnd);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001550 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00001551
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001552
1553 // Accessor Methods for Switch stmt
Chris Lattner454928e2005-01-29 00:31:36 +00001554 inline Value *getCondition() const { return getOperand(0); }
1555 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00001556
Chris Lattner454928e2005-01-29 00:31:36 +00001557 inline BasicBlock *getDefaultDest() const {
1558 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001559 }
1560
1561 /// getNumCases - return the number of 'cases' in this switch instruction.
1562 /// Note that case #0 is always the default case.
1563 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001564 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001565 }
1566
1567 /// getCaseValue - Return the specified case value. Note that case #0, the
1568 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001569 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001570 assert(i && i < getNumCases() && "Illegal case value to get!");
1571 return getSuccessorValue(i);
1572 }
1573
1574 /// getCaseValue - Return the specified case value. Note that case #0, the
1575 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001576 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001577 assert(i && i < getNumCases() && "Illegal case value to get!");
1578 return getSuccessorValue(i);
1579 }
1580
1581 /// findCaseValue - Search all of the case values for the specified constant.
1582 /// If it is explicitly handled, return the case number of it, otherwise
1583 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001584 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001585 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1586 if (getCaseValue(i) == C)
1587 return i;
1588 return 0;
1589 }
1590
Nick Lewycky011f1842006-09-18 19:03:59 +00001591 /// findCaseDest - Finds the unique case value for a given successor. Returns
1592 /// null if the successor is not found, not unique, or is the default case.
1593 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00001594 if (BB == getDefaultDest()) return NULL;
1595
Nick Lewycky011f1842006-09-18 19:03:59 +00001596 ConstantInt *CI = NULL;
1597 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
1598 if (getSuccessor(i) == BB) {
1599 if (CI) return NULL; // Multiple cases lead to BB.
1600 else CI = getCaseValue(i);
1601 }
1602 }
1603 return CI;
1604 }
1605
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001606 /// addCase - Add an entry to the switch instruction...
1607 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00001608 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001609
1610 /// removeCase - This method removes the specified successor from the switch
1611 /// instruction. Note that this cannot be used to remove the default
1612 /// destination (successor #0).
1613 ///
1614 void removeCase(unsigned idx);
1615
Chris Lattner454928e2005-01-29 00:31:36 +00001616 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001617
Chris Lattner454928e2005-01-29 00:31:36 +00001618 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1619 BasicBlock *getSuccessor(unsigned idx) const {
1620 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1621 return cast<BasicBlock>(getOperand(idx*2+1));
1622 }
1623 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001624 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001625 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001626 }
1627
1628 // getSuccessorValue - Return the value associated with the specified
1629 // successor.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001630 inline ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001631 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00001632 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001633 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001634
1635 // Methods for support type inquiry through isa, cast, and dyn_cast:
1636 static inline bool classof(const SwitchInst *) { return true; }
1637 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00001638 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001639 }
1640 static inline bool classof(const Value *V) {
1641 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1642 }
Chris Lattner454928e2005-01-29 00:31:36 +00001643private:
1644 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1645 virtual unsigned getNumSuccessorsV() const;
1646 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001647};
1648
1649//===----------------------------------------------------------------------===//
1650// InvokeInst Class
1651//===----------------------------------------------------------------------===//
1652
1653//===---------------------------------------------------------------------------
Chris Lattner3340ffe2005-05-06 20:26:26 +00001654
1655/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
1656/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001657///
1658class InvokeInst : public TerminatorInst {
Duncan Sandsdc024672007-11-27 13:23:08 +00001659 const ParamAttrsList *ParamAttrs;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001660 InvokeInst(const InvokeInst &BI);
1661 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerd2dd1502007-02-13 01:04:01 +00001662 Value* const *Args, unsigned NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00001663
1664 template<typename InputIterator>
1665 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1666 InputIterator ArgBegin, InputIterator ArgEnd,
1667 const std::string &Name,
1668 // This argument ensures that we have an iterator we can
1669 // do arithmetic on in constant time
1670 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00001671 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greenef1355a52007-08-27 19:04:21 +00001672
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00001673 // This requires that the iterator points to contiguous memory.
1674 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00001675 setName(Name);
1676 }
1677
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001678public:
David Greenef1355a52007-08-27 19:04:21 +00001679 /// Construct an InvokeInst given a range of arguments.
1680 /// InputIterator must be a random-access iterator pointing to
1681 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
1682 /// made for random-accessness but not for contiguous storage as
1683 /// that would incur runtime overhead.
1684 ///
1685 /// @brief Construct an InvokeInst from a range of arguments
1686 template<typename InputIterator>
1687 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1688 InputIterator ArgBegin, InputIterator ArgEnd,
1689 const std::string &Name = "", Instruction *InsertBefore = 0)
1690 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
1691 ->getElementType())->getReturnType(),
1692 Instruction::Invoke, 0, 0, InsertBefore) {
1693 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1694 typename std::iterator_traits<InputIterator>::iterator_category());
1695 }
1696
1697 /// Construct an InvokeInst given a range of arguments.
1698 /// InputIterator must be a random-access iterator pointing to
1699 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
1700 /// made for random-accessness but not for contiguous storage as
1701 /// that would incur runtime overhead.
1702 ///
1703 /// @brief Construct an InvokeInst from a range of arguments
1704 template<typename InputIterator>
1705 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1706 InputIterator ArgBegin, InputIterator ArgEnd,
1707 const std::string &Name, BasicBlock *InsertAtEnd)
1708 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
1709 ->getElementType())->getReturnType(),
1710 Instruction::Invoke, 0, 0, InsertAtEnd) {
1711 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1712 typename std::iterator_traits<InputIterator>::iterator_category());
1713 }
1714
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001715 ~InvokeInst();
1716
Chris Lattnerf319e832004-10-15 23:52:05 +00001717 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001718
Chris Lattner3340ffe2005-05-06 20:26:26 +00001719 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1720 /// function call.
1721 unsigned getCallingConv() const { return SubclassData; }
1722 void setCallingConv(unsigned CC) {
1723 SubclassData = CC;
1724 }
1725
Reid Spencerfa3e9122007-04-09 18:00:57 +00001726 /// Obtains a pointer to the ParamAttrsList object which holds the
1727 /// parameter attributes information, if any.
1728 /// @returns 0 if no attributes have been set.
1729 /// @brief Get the parameter attributes.
Duncan Sandsdc024672007-11-27 13:23:08 +00001730 const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00001731
1732 /// Sets the parameter attributes for this InvokeInst. To construct a
1733 /// ParamAttrsList, see ParameterAttributes.h
1734 /// @brief Set the parameter attributes.
Duncan Sandsdc024672007-11-27 13:23:08 +00001735 void setParamAttrs(const ParamAttrsList *attrs);
1736
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001737 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001738 bool paramHasAttr(uint16_t i, unsigned attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001739
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001740 /// @brief Determine if the call does not access memory.
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001741 bool doesNotAccessMemory() const;
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001742
1743 /// @brief Determine if the call does not access or only reads memory.
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001744 bool onlyReadsMemory() const;
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001745
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001746 /// @brief Determine if the call cannot return.
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001747 bool doesNotReturn() const;
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001748
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001749 /// @brief Determine if the call cannot unwind.
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001750 bool doesNotThrow() const;
Duncan Sandsf0c33542007-12-19 21:13:37 +00001751 void setDoesNotThrow(bool doesNotThrow = true);
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001752
Duncan Sandsdc024672007-11-27 13:23:08 +00001753 /// @brief Determine if the call returns a structure.
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001754 bool isStructReturn() const;
Reid Spencerfa3e9122007-04-09 18:00:57 +00001755
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001756 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00001757 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001758 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001759 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001760 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001761 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001762
1763 // getCalledValue - Get a pointer to a function that is invoked by this inst.
Chris Lattner454928e2005-01-29 00:31:36 +00001764 inline Value *getCalledValue() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001765
1766 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00001767 BasicBlock *getNormalDest() const {
1768 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001769 }
Chris Lattner454928e2005-01-29 00:31:36 +00001770 BasicBlock *getUnwindDest() const {
1771 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001772 }
Chris Lattner454928e2005-01-29 00:31:36 +00001773 void setNormalDest(BasicBlock *B) {
1774 setOperand(1, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001775 }
1776
Chris Lattner454928e2005-01-29 00:31:36 +00001777 void setUnwindDest(BasicBlock *B) {
1778 setOperand(2, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001779 }
1780
Chris Lattner454928e2005-01-29 00:31:36 +00001781 inline BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001782 assert(i < 2 && "Successor # out of range for invoke!");
1783 return i == 0 ? getNormalDest() : getUnwindDest();
1784 }
1785
Chris Lattner454928e2005-01-29 00:31:36 +00001786 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001787 assert(idx < 2 && "Successor # out of range for invoke!");
Chris Lattner454928e2005-01-29 00:31:36 +00001788 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001789 }
1790
Chris Lattner454928e2005-01-29 00:31:36 +00001791 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001792
1793 // Methods for support type inquiry through isa, cast, and dyn_cast:
1794 static inline bool classof(const InvokeInst *) { return true; }
1795 static inline bool classof(const Instruction *I) {
1796 return (I->getOpcode() == Instruction::Invoke);
1797 }
1798 static inline bool classof(const Value *V) {
1799 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1800 }
Chris Lattner454928e2005-01-29 00:31:36 +00001801private:
1802 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1803 virtual unsigned getNumSuccessorsV() const;
1804 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001805};
1806
1807
1808//===----------------------------------------------------------------------===//
1809// UnwindInst Class
1810//===----------------------------------------------------------------------===//
1811
1812//===---------------------------------------------------------------------------
1813/// UnwindInst - Immediately exit the current function, unwinding the stack
1814/// until an invoke instruction is found.
1815///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00001816class UnwindInst : public TerminatorInst {
1817public:
Chris Lattner910c80a2007-02-24 00:55:48 +00001818 explicit UnwindInst(Instruction *InsertBefore = 0);
1819 explicit UnwindInst(BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001820
Chris Lattnerf319e832004-10-15 23:52:05 +00001821 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001822
Chris Lattner454928e2005-01-29 00:31:36 +00001823 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001824
1825 // Methods for support type inquiry through isa, cast, and dyn_cast:
1826 static inline bool classof(const UnwindInst *) { return true; }
1827 static inline bool classof(const Instruction *I) {
1828 return I->getOpcode() == Instruction::Unwind;
1829 }
1830 static inline bool classof(const Value *V) {
1831 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1832 }
Chris Lattner454928e2005-01-29 00:31:36 +00001833private:
1834 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1835 virtual unsigned getNumSuccessorsV() const;
1836 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001837};
1838
Chris Lattner076b3f12004-10-16 18:05:54 +00001839//===----------------------------------------------------------------------===//
1840// UnreachableInst Class
1841//===----------------------------------------------------------------------===//
1842
1843//===---------------------------------------------------------------------------
1844/// UnreachableInst - This function has undefined behavior. In particular, the
1845/// presence of this instruction indicates some higher level knowledge that the
1846/// end of the block cannot be reached.
1847///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00001848class UnreachableInst : public TerminatorInst {
1849public:
Chris Lattner910c80a2007-02-24 00:55:48 +00001850 explicit UnreachableInst(Instruction *InsertBefore = 0);
1851 explicit UnreachableInst(BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00001852
1853 virtual UnreachableInst *clone() const;
1854
Chris Lattner454928e2005-01-29 00:31:36 +00001855 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00001856
1857 // Methods for support type inquiry through isa, cast, and dyn_cast:
1858 static inline bool classof(const UnreachableInst *) { return true; }
1859 static inline bool classof(const Instruction *I) {
1860 return I->getOpcode() == Instruction::Unreachable;
1861 }
1862 static inline bool classof(const Value *V) {
1863 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1864 }
Chris Lattner454928e2005-01-29 00:31:36 +00001865private:
1866 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1867 virtual unsigned getNumSuccessorsV() const;
1868 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00001869};
1870
Reid Spencer3da59db2006-11-27 01:05:10 +00001871//===----------------------------------------------------------------------===//
1872// TruncInst Class
1873//===----------------------------------------------------------------------===//
1874
1875/// @brief This class represents a truncation of integer types.
1876class TruncInst : public CastInst {
1877 /// Private copy constructor
1878 TruncInst(const TruncInst &CI)
1879 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
1880 }
1881public:
1882 /// @brief Constructor with insert-before-instruction semantics
1883 TruncInst(
1884 Value *S, ///< The value to be truncated
1885 const Type *Ty, ///< The (smaller) type to truncate to
1886 const std::string &Name = "", ///< A name for the new instruction
1887 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1888 );
1889
1890 /// @brief Constructor with insert-at-end-of-block semantics
1891 TruncInst(
1892 Value *S, ///< The value to be truncated
1893 const Type *Ty, ///< The (smaller) type to truncate to
1894 const std::string &Name, ///< A name for the new instruction
1895 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1896 );
1897
1898 /// @brief Clone an identical TruncInst
1899 virtual CastInst *clone() const;
1900
1901 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1902 static inline bool classof(const TruncInst *) { return true; }
1903 static inline bool classof(const Instruction *I) {
1904 return I->getOpcode() == Trunc;
1905 }
1906 static inline bool classof(const Value *V) {
1907 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1908 }
1909};
1910
1911//===----------------------------------------------------------------------===//
1912// ZExtInst Class
1913//===----------------------------------------------------------------------===//
1914
1915/// @brief This class represents zero extension of integer types.
1916class ZExtInst : public CastInst {
1917 /// @brief Private copy constructor
1918 ZExtInst(const ZExtInst &CI)
1919 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
1920 }
1921public:
1922 /// @brief Constructor with insert-before-instruction semantics
1923 ZExtInst(
1924 Value *S, ///< The value to be zero extended
1925 const Type *Ty, ///< The type to zero extend to
1926 const std::string &Name = "", ///< A name for the new instruction
1927 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1928 );
1929
1930 /// @brief Constructor with insert-at-end semantics.
1931 ZExtInst(
1932 Value *S, ///< The value to be zero extended
1933 const Type *Ty, ///< The type to zero extend to
1934 const std::string &Name, ///< A name for the new instruction
1935 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1936 );
1937
1938 /// @brief Clone an identical ZExtInst
1939 virtual CastInst *clone() const;
1940
1941 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1942 static inline bool classof(const ZExtInst *) { return true; }
1943 static inline bool classof(const Instruction *I) {
1944 return I->getOpcode() == ZExt;
1945 }
1946 static inline bool classof(const Value *V) {
1947 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1948 }
1949};
1950
1951//===----------------------------------------------------------------------===//
1952// SExtInst Class
1953//===----------------------------------------------------------------------===//
1954
1955/// @brief This class represents a sign extension of integer types.
1956class SExtInst : public CastInst {
1957 /// @brief Private copy constructor
1958 SExtInst(const SExtInst &CI)
1959 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
1960 }
1961public:
1962 /// @brief Constructor with insert-before-instruction semantics
1963 SExtInst(
1964 Value *S, ///< The value to be sign extended
1965 const Type *Ty, ///< The type to sign extend 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 SExtInst(
1972 Value *S, ///< The value to be sign extended
1973 const Type *Ty, ///< The type to sign extend 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 SExtInst
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 SExtInst *) { return true; }
1983 static inline bool classof(const Instruction *I) {
1984 return I->getOpcode() == SExt;
1985 }
1986 static inline bool classof(const Value *V) {
1987 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1988 }
1989};
1990
1991//===----------------------------------------------------------------------===//
1992// FPTruncInst Class
1993//===----------------------------------------------------------------------===//
1994
1995/// @brief This class represents a truncation of floating point types.
1996class FPTruncInst : public CastInst {
1997 FPTruncInst(const FPTruncInst &CI)
1998 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
1999 }
2000public:
2001 /// @brief Constructor with insert-before-instruction semantics
2002 FPTruncInst(
2003 Value *S, ///< The value to be truncated
2004 const Type *Ty, ///< The type to truncate 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-before-instruction semantics
2010 FPTruncInst(
2011 Value *S, ///< The value to be truncated
2012 const Type *Ty, ///< The type to truncate 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 FPTruncInst
2018 virtual CastInst *clone() const;
2019
2020 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2021 static inline bool classof(const FPTruncInst *) { return true; }
2022 static inline bool classof(const Instruction *I) {
2023 return I->getOpcode() == FPTrunc;
2024 }
2025 static inline bool classof(const Value *V) {
2026 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2027 }
2028};
2029
2030//===----------------------------------------------------------------------===//
2031// FPExtInst Class
2032//===----------------------------------------------------------------------===//
2033
2034/// @brief This class represents an extension of floating point types.
2035class FPExtInst : public CastInst {
2036 FPExtInst(const FPExtInst &CI)
2037 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2038 }
2039public:
2040 /// @brief Constructor with insert-before-instruction semantics
2041 FPExtInst(
2042 Value *S, ///< The value to be extended
2043 const Type *Ty, ///< The type to extend 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 FPExtInst(
2050 Value *S, ///< The value to be extended
2051 const Type *Ty, ///< The type to extend 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 FPExtInst
2057 virtual CastInst *clone() const;
2058
2059 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2060 static inline bool classof(const FPExtInst *) { return true; }
2061 static inline bool classof(const Instruction *I) {
2062 return I->getOpcode() == FPExt;
2063 }
2064 static inline bool classof(const Value *V) {
2065 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2066 }
2067};
2068
2069//===----------------------------------------------------------------------===//
2070// UIToFPInst Class
2071//===----------------------------------------------------------------------===//
2072
2073/// @brief This class represents a cast unsigned integer to floating point.
2074class UIToFPInst : public CastInst {
2075 UIToFPInst(const UIToFPInst &CI)
2076 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2077 }
2078public:
2079 /// @brief Constructor with insert-before-instruction semantics
2080 UIToFPInst(
2081 Value *S, ///< The value to be converted
2082 const Type *Ty, ///< The type to convert 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 UIToFPInst(
2089 Value *S, ///< The value to be converted
2090 const Type *Ty, ///< The type to convert 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 UIToFPInst
2096 virtual CastInst *clone() const;
2097
2098 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2099 static inline bool classof(const UIToFPInst *) { return true; }
2100 static inline bool classof(const Instruction *I) {
2101 return I->getOpcode() == UIToFP;
2102 }
2103 static inline bool classof(const Value *V) {
2104 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2105 }
2106};
2107
2108//===----------------------------------------------------------------------===//
2109// SIToFPInst Class
2110//===----------------------------------------------------------------------===//
2111
2112/// @brief This class represents a cast from signed integer to floating point.
2113class SIToFPInst : public CastInst {
2114 SIToFPInst(const SIToFPInst &CI)
2115 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2116 }
2117public:
2118 /// @brief Constructor with insert-before-instruction semantics
2119 SIToFPInst(
2120 Value *S, ///< The value to be converted
2121 const Type *Ty, ///< The type to convert to
2122 const std::string &Name = "", ///< A name for the new instruction
2123 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2124 );
2125
2126 /// @brief Constructor with insert-at-end-of-block semantics
2127 SIToFPInst(
2128 Value *S, ///< The value to be converted
2129 const Type *Ty, ///< The type to convert to
2130 const std::string &Name, ///< A name for the new instruction
2131 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2132 );
2133
2134 /// @brief Clone an identical SIToFPInst
2135 virtual CastInst *clone() const;
2136
2137 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2138 static inline bool classof(const SIToFPInst *) { return true; }
2139 static inline bool classof(const Instruction *I) {
2140 return I->getOpcode() == SIToFP;
2141 }
2142 static inline bool classof(const Value *V) {
2143 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2144 }
2145};
2146
2147//===----------------------------------------------------------------------===//
2148// FPToUIInst Class
2149//===----------------------------------------------------------------------===//
2150
2151/// @brief This class represents a cast from floating point to unsigned integer
2152class FPToUIInst : public CastInst {
2153 FPToUIInst(const FPToUIInst &CI)
2154 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2155 }
2156public:
2157 /// @brief Constructor with insert-before-instruction semantics
2158 FPToUIInst(
2159 Value *S, ///< The value to be converted
2160 const Type *Ty, ///< The type to convert to
2161 const std::string &Name = "", ///< A name for the new instruction
2162 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2163 );
2164
2165 /// @brief Constructor with insert-at-end-of-block semantics
2166 FPToUIInst(
2167 Value *S, ///< The value to be converted
2168 const Type *Ty, ///< The type to convert to
2169 const std::string &Name, ///< A name for the new instruction
2170 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2171 );
2172
2173 /// @brief Clone an identical FPToUIInst
2174 virtual CastInst *clone() const;
2175
2176 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2177 static inline bool classof(const FPToUIInst *) { return true; }
2178 static inline bool classof(const Instruction *I) {
2179 return I->getOpcode() == FPToUI;
2180 }
2181 static inline bool classof(const Value *V) {
2182 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2183 }
2184};
2185
2186//===----------------------------------------------------------------------===//
2187// FPToSIInst Class
2188//===----------------------------------------------------------------------===//
2189
2190/// @brief This class represents a cast from floating point to signed integer.
2191class FPToSIInst : public CastInst {
2192 FPToSIInst(const FPToSIInst &CI)
2193 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2194 }
2195public:
2196 /// @brief Constructor with insert-before-instruction semantics
2197 FPToSIInst(
2198 Value *S, ///< The value to be converted
2199 const Type *Ty, ///< The type to convert to
2200 const std::string &Name = "", ///< A name for the new instruction
2201 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2202 );
2203
2204 /// @brief Constructor with insert-at-end-of-block semantics
2205 FPToSIInst(
2206 Value *S, ///< The value to be converted
2207 const Type *Ty, ///< The type to convert to
2208 const std::string &Name, ///< A name for the new instruction
2209 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2210 );
2211
2212 /// @brief Clone an identical FPToSIInst
2213 virtual CastInst *clone() const;
2214
2215 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2216 static inline bool classof(const FPToSIInst *) { return true; }
2217 static inline bool classof(const Instruction *I) {
2218 return I->getOpcode() == FPToSI;
2219 }
2220 static inline bool classof(const Value *V) {
2221 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2222 }
2223};
2224
2225//===----------------------------------------------------------------------===//
2226// IntToPtrInst Class
2227//===----------------------------------------------------------------------===//
2228
2229/// @brief This class represents a cast from an integer to a pointer.
2230class IntToPtrInst : public CastInst {
2231 IntToPtrInst(const IntToPtrInst &CI)
2232 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2233 }
2234public:
2235 /// @brief Constructor with insert-before-instruction semantics
2236 IntToPtrInst(
2237 Value *S, ///< The value to be converted
2238 const Type *Ty, ///< The type to convert to
2239 const std::string &Name = "", ///< A name for the new instruction
2240 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2241 );
2242
2243 /// @brief Constructor with insert-at-end-of-block semantics
2244 IntToPtrInst(
2245 Value *S, ///< The value to be converted
2246 const Type *Ty, ///< The type to convert to
2247 const std::string &Name, ///< A name for the new instruction
2248 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2249 );
2250
2251 /// @brief Clone an identical IntToPtrInst
2252 virtual CastInst *clone() const;
2253
2254 // Methods for support type inquiry through isa, cast, and dyn_cast:
2255 static inline bool classof(const IntToPtrInst *) { return true; }
2256 static inline bool classof(const Instruction *I) {
2257 return I->getOpcode() == IntToPtr;
2258 }
2259 static inline bool classof(const Value *V) {
2260 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2261 }
2262};
2263
2264//===----------------------------------------------------------------------===//
2265// PtrToIntInst Class
2266//===----------------------------------------------------------------------===//
2267
2268/// @brief This class represents a cast from a pointer to an integer
2269class PtrToIntInst : public CastInst {
2270 PtrToIntInst(const PtrToIntInst &CI)
2271 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
2272 }
2273public:
2274 /// @brief Constructor with insert-before-instruction semantics
2275 PtrToIntInst(
2276 Value *S, ///< The value to be converted
2277 const Type *Ty, ///< The type to convert to
2278 const std::string &Name = "", ///< A name for the new instruction
2279 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2280 );
2281
2282 /// @brief Constructor with insert-at-end-of-block semantics
2283 PtrToIntInst(
2284 Value *S, ///< The value to be converted
2285 const Type *Ty, ///< The type to convert to
2286 const std::string &Name, ///< A name for the new instruction
2287 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2288 );
2289
2290 /// @brief Clone an identical PtrToIntInst
2291 virtual CastInst *clone() const;
2292
2293 // Methods for support type inquiry through isa, cast, and dyn_cast:
2294 static inline bool classof(const PtrToIntInst *) { return true; }
2295 static inline bool classof(const Instruction *I) {
2296 return I->getOpcode() == PtrToInt;
2297 }
2298 static inline bool classof(const Value *V) {
2299 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2300 }
2301};
2302
2303//===----------------------------------------------------------------------===//
2304// BitCastInst Class
2305//===----------------------------------------------------------------------===//
2306
2307/// @brief This class represents a no-op cast from one type to another.
2308class BitCastInst : public CastInst {
2309 BitCastInst(const BitCastInst &CI)
2310 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
2311 }
2312public:
2313 /// @brief Constructor with insert-before-instruction semantics
2314 BitCastInst(
2315 Value *S, ///< The value to be casted
2316 const Type *Ty, ///< The type to casted to
2317 const std::string &Name = "", ///< A name for the new instruction
2318 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2319 );
2320
2321 /// @brief Constructor with insert-at-end-of-block semantics
2322 BitCastInst(
2323 Value *S, ///< The value to be casted
2324 const Type *Ty, ///< The type to casted to
2325 const std::string &Name, ///< A name for the new instruction
2326 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2327 );
2328
2329 /// @brief Clone an identical BitCastInst
2330 virtual CastInst *clone() const;
2331
2332 // Methods for support type inquiry through isa, cast, and dyn_cast:
2333 static inline bool classof(const BitCastInst *) { return true; }
2334 static inline bool classof(const Instruction *I) {
2335 return I->getOpcode() == BitCast;
2336 }
2337 static inline bool classof(const Value *V) {
2338 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2339 }
2340};
2341
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002342} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00002343
2344#endif