blob: f292a3173feb77385dac8bca1dde0339f36623e0 [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"
Dale Johannesen0d51e7e2008-02-19 21:38:47 +000023#include "llvm/ParameterAttributes.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000024
25namespace llvm {
26
Chris Lattner1fca5ff2004-10-27 16:14:51 +000027class BasicBlock;
Chris Lattnerd1a32602005-02-24 05:32:09 +000028class ConstantInt;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000029class PointerType;
Reid Spencer9d6565a2007-02-15 02:26:10 +000030class VectorType;
Reid Spencer3da43842007-02-28 22:00:54 +000031class ConstantRange;
32class APInt;
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 {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000042protected:
Nate Begeman14b05292005-11-05 09:21:28 +000043 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
Misha Brukman91102862005-03-16 03:46:55 +000044 const std::string &Name = "", Instruction *InsertBefore = 0);
Nate Begeman14b05292005-11-05 09:21:28 +000045 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
Misha Brukman91102862005-03-16 03:46:55 +000046 const std::string &Name, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000047public:
Gabor Greif051a9502008-04-06 20:25:17 +000048 // Out of line virtual method, so the vtable, etc. has a home.
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000049 virtual ~AllocationInst();
50
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000051 /// isArrayAllocation - Return true if there is an allocation size parameter
52 /// to the allocation instruction that is not 1.
53 ///
54 bool isArrayAllocation() const;
55
56 /// getArraySize - Get the number of element allocated, for a simple
57 /// allocation of a single element, this will return a constant 1 value.
58 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000059 const Value *getArraySize() const { return getOperand(0); }
60 Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000061
62 /// getType - Overload to return most specific pointer type
63 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000064 const PointerType *getType() const {
Misha Brukman9769ab22005-04-21 20:19:05 +000065 return reinterpret_cast<const PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000066 }
67
68 /// getAllocatedType - Return the type that is being allocated by the
69 /// instruction.
70 ///
71 const Type *getAllocatedType() const;
72
Nate Begeman14b05292005-11-05 09:21:28 +000073 /// getAlignment - Return the alignment of the memory that is being allocated
74 /// by the instruction.
75 ///
Dan Gohman52837072008-03-24 16:55:58 +000076 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
77 void setAlignment(unsigned Align);
Chris Lattnerf56a8db2006-10-03 17:09:12 +000078
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000079 virtual Instruction *clone() const = 0;
80
81 // Methods for support type inquiry through isa, cast, and dyn_cast:
82 static inline bool classof(const AllocationInst *) { return true; }
83 static inline bool classof(const Instruction *I) {
84 return I->getOpcode() == Instruction::Alloca ||
85 I->getOpcode() == Instruction::Malloc;
86 }
87 static inline bool classof(const Value *V) {
88 return isa<Instruction>(V) && classof(cast<Instruction>(V));
89 }
90};
91
92
93//===----------------------------------------------------------------------===//
94// MallocInst Class
95//===----------------------------------------------------------------------===//
96
97/// MallocInst - an instruction to allocated memory on the heap
98///
99class MallocInst : public AllocationInst {
100 MallocInst(const MallocInst &MI);
101public:
102 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
103 const std::string &Name = "",
104 Instruction *InsertBefore = 0)
Nate Begeman14b05292005-11-05 09:21:28 +0000105 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000106 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
107 BasicBlock *InsertAtEnd)
Nate Begeman14b05292005-11-05 09:21:28 +0000108 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000109
110 MallocInst(const Type *Ty, const std::string &Name,
111 Instruction *InsertBefore = 0)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000112 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
113 MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
114 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000115
116 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Nate Begeman14b05292005-11-05 09:21:28 +0000117 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000118 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
119 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Nate Begeman14b05292005-11-05 09:21:28 +0000120 const std::string &Name = "",
121 Instruction *InsertBefore = 0)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000122 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000123
Chris Lattnerf319e832004-10-15 23:52:05 +0000124 virtual MallocInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000125
126 // Methods for support type inquiry through isa, cast, and dyn_cast:
127 static inline bool classof(const MallocInst *) { return true; }
128 static inline bool classof(const Instruction *I) {
129 return (I->getOpcode() == Instruction::Malloc);
130 }
131 static inline bool classof(const Value *V) {
132 return isa<Instruction>(V) && classof(cast<Instruction>(V));
133 }
134};
135
136
137//===----------------------------------------------------------------------===//
138// AllocaInst Class
139//===----------------------------------------------------------------------===//
140
141/// AllocaInst - an instruction to allocate memory on the stack
142///
143class AllocaInst : public AllocationInst {
144 AllocaInst(const AllocaInst &);
145public:
146 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
147 const std::string &Name = "",
148 Instruction *InsertBefore = 0)
Nate Begeman14b05292005-11-05 09:21:28 +0000149 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000150 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
151 BasicBlock *InsertAtEnd)
Nate Begeman14b05292005-11-05 09:21:28 +0000152 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
Chris Lattnerb77780e2006-05-10 04:38:35 +0000153
154 AllocaInst(const Type *Ty, const std::string &Name,
155 Instruction *InsertBefore = 0)
156 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
157 AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
158 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000159
Chris Lattnerb77780e2006-05-10 04:38:35 +0000160 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
161 const std::string &Name = "", Instruction *InsertBefore = 0)
162 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
Nate Begeman14b05292005-11-05 09:21:28 +0000163 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
164 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000165 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000166
Chris Lattnerf319e832004-10-15 23:52:05 +0000167 virtual AllocaInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000168
169 // Methods for support type inquiry through isa, cast, and dyn_cast:
170 static inline bool classof(const AllocaInst *) { return true; }
171 static inline bool classof(const Instruction *I) {
172 return (I->getOpcode() == Instruction::Alloca);
173 }
174 static inline bool classof(const Value *V) {
175 return isa<Instruction>(V) && classof(cast<Instruction>(V));
176 }
177};
178
179
180//===----------------------------------------------------------------------===//
181// FreeInst Class
182//===----------------------------------------------------------------------===//
183
184/// FreeInst - an instruction to deallocate memory
185///
Chris Lattner454928e2005-01-29 00:31:36 +0000186class FreeInst : public UnaryInstruction {
187 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000188public:
189 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
190 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
191
Chris Lattnerf319e832004-10-15 23:52:05 +0000192 virtual FreeInst *clone() const;
Owen Andersonc9edf0b2007-07-06 23:13:31 +0000193
194 // Accessor methods for consistency with other memory operations
195 Value *getPointerOperand() { return getOperand(0); }
196 const Value *getPointerOperand() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000197
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000198 // Methods for support type inquiry through isa, cast, and dyn_cast:
199 static inline bool classof(const FreeInst *) { return true; }
200 static inline bool classof(const Instruction *I) {
201 return (I->getOpcode() == Instruction::Free);
202 }
203 static inline bool classof(const Value *V) {
204 return isa<Instruction>(V) && classof(cast<Instruction>(V));
205 }
206};
207
208
209//===----------------------------------------------------------------------===//
210// LoadInst Class
211//===----------------------------------------------------------------------===//
212
Chris Lattner88fe29a2005-02-05 01:44:18 +0000213/// LoadInst - an instruction for reading from memory. This uses the
214/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000215///
Chris Lattner454928e2005-01-29 00:31:36 +0000216class LoadInst : public UnaryInstruction {
Christopher Lamb43c7f372007-04-22 19:24:39 +0000217
Chris Lattner454928e2005-01-29 00:31:36 +0000218 LoadInst(const LoadInst &LI)
Chris Lattner88fe29a2005-02-05 01:44:18 +0000219 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
220 setVolatile(LI.isVolatile());
Christopher Lamb43c7f372007-04-22 19:24:39 +0000221 setAlignment(LI.getAlignment());
Misha Brukman9769ab22005-04-21 20:19:05 +0000222
Chris Lattner454928e2005-01-29 00:31:36 +0000223#ifndef NDEBUG
224 AssertOK();
225#endif
226 }
227 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000228public:
229 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
230 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000231 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile = false,
232 Instruction *InsertBefore = 0);
233 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
Chris Lattnerf00042a2007-02-13 07:54:42 +0000234 Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000235 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
236 BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000237 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
238 BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000239
Chris Lattnerf00042a2007-02-13 07:54:42 +0000240 LoadInst(Value *Ptr, const char *Name, Instruction *InsertBefore);
241 LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAtEnd);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000242 explicit LoadInst(Value *Ptr, const char *Name = 0, bool isVolatile = false,
Chris Lattnerf00042a2007-02-13 07:54:42 +0000243 Instruction *InsertBefore = 0);
244 LoadInst(Value *Ptr, const char *Name, bool isVolatile,
245 BasicBlock *InsertAtEnd);
246
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000247 /// isVolatile - Return true if this is a load from a volatile memory
248 /// location.
249 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000250 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000251
252 /// setVolatile - Specify whether this is a volatile load or not.
253 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000254 void setVolatile(bool V) {
Hartmut Kaiserefd4a512007-10-17 14:56:40 +0000255 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000256 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000257
Chris Lattnerf319e832004-10-15 23:52:05 +0000258 virtual LoadInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000259
Christopher Lamb43c7f372007-04-22 19:24:39 +0000260 /// getAlignment - Return the alignment of the access that is being performed
261 ///
262 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000263 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000264 }
265
266 void setAlignment(unsigned Align);
267
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000268 Value *getPointerOperand() { return getOperand(0); }
269 const Value *getPointerOperand() const { return getOperand(0); }
270 static unsigned getPointerOperandIndex() { return 0U; }
271
272 // Methods for support type inquiry through isa, cast, and dyn_cast:
273 static inline bool classof(const LoadInst *) { return true; }
274 static inline bool classof(const Instruction *I) {
275 return I->getOpcode() == Instruction::Load;
276 }
277 static inline bool classof(const Value *V) {
278 return isa<Instruction>(V) && classof(cast<Instruction>(V));
279 }
280};
281
282
283//===----------------------------------------------------------------------===//
284// StoreInst Class
285//===----------------------------------------------------------------------===//
286
Misha Brukman9769ab22005-04-21 20:19:05 +0000287/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000288///
289class StoreInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +0000290 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +0000291 Use Ops[2];
Christopher Lamb43c7f372007-04-22 19:24:39 +0000292
Chris Lattner88fe29a2005-02-05 01:44:18 +0000293 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
Chris Lattner454928e2005-01-29 00:31:36 +0000294 Ops[0].init(SI.Ops[0], this);
295 Ops[1].init(SI.Ops[1], this);
Chris Lattner88fe29a2005-02-05 01:44:18 +0000296 setVolatile(SI.isVolatile());
Christopher Lamb43c7f372007-04-22 19:24:39 +0000297 setAlignment(SI.getAlignment());
298
Chris Lattner454928e2005-01-29 00:31:36 +0000299#ifndef NDEBUG
300 AssertOK();
301#endif
302 }
303 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000304public:
Gabor Greif051a9502008-04-06 20:25:17 +0000305 // allocate space for exactly two operands
306 void *operator new(size_t s) {
307 return User::operator new(s, 2);
308 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000309 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
310 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
311 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
312 Instruction *InsertBefore = 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000313 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
314 unsigned Align, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000315 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000316 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
317 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000318
319
320 /// isVolatile - Return true if this is a load from a volatile memory
321 /// location.
322 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000323 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000324
325 /// setVolatile - Specify whether this is a volatile load or not.
326 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000327 void setVolatile(bool V) {
Hartmut Kaiserefd4a512007-10-17 14:56:40 +0000328 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000329 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000330
Chris Lattner454928e2005-01-29 00:31:36 +0000331 /// Transparently provide more efficient getOperand methods.
Misha Brukman9769ab22005-04-21 20:19:05 +0000332 Value *getOperand(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +0000333 assert(i < 2 && "getOperand() out of range!");
334 return Ops[i];
335 }
336 void setOperand(unsigned i, Value *Val) {
337 assert(i < 2 && "setOperand() out of range!");
338 Ops[i] = Val;
339 }
340 unsigned getNumOperands() const { return 2; }
341
Christopher Lamb43c7f372007-04-22 19:24:39 +0000342 /// getAlignment - Return the alignment of the access that is being performed
343 ///
344 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000345 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000346 }
347
348 void setAlignment(unsigned Align);
349
Chris Lattnerf319e832004-10-15 23:52:05 +0000350 virtual StoreInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000351
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000352 Value *getPointerOperand() { return getOperand(1); }
353 const Value *getPointerOperand() const { return getOperand(1); }
354 static unsigned getPointerOperandIndex() { return 1U; }
355
356 // Methods for support type inquiry through isa, cast, and dyn_cast:
357 static inline bool classof(const StoreInst *) { return true; }
358 static inline bool classof(const Instruction *I) {
359 return I->getOpcode() == Instruction::Store;
360 }
361 static inline bool classof(const Value *V) {
362 return isa<Instruction>(V) && classof(cast<Instruction>(V));
363 }
364};
365
366
367//===----------------------------------------------------------------------===//
368// GetElementPtrInst Class
369//===----------------------------------------------------------------------===//
370
David Greeneb8f74792007-09-04 15:46:09 +0000371// checkType - Simple wrapper function to give a better assertion failure
372// message on bad indexes for a gep instruction.
373//
374static inline const Type *checkType(const Type *Ty) {
375 assert(Ty && "Invalid GetElementPtrInst indices for type!");
376 return Ty;
377}
378
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000379/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
380/// access elements of arrays and structs
381///
382class GetElementPtrInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000383 GetElementPtrInst(const GetElementPtrInst &GEPI)
384 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
385 0, GEPI.getNumOperands()) {
386 Use *OL = OperandList = new Use[NumOperands];
387 Use *GEPIOL = GEPI.OperandList;
388 for (unsigned i = 0, E = NumOperands; i != E; ++i)
389 OL[i].init(GEPIOL[i], this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000390 }
Chris Lattner6ffbe172007-01-31 19:47:18 +0000391 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx);
Chris Lattner38bacf22005-05-03 05:43:30 +0000392 void init(Value *Ptr, Value *Idx);
David Greeneb8f74792007-09-04 15:46:09 +0000393
394 template<typename InputIterator>
395 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
396 const std::string &Name,
397 // This argument ensures that we have an iterator we can
398 // do arithmetic on in constant time
399 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000400 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
David Greeneb8f74792007-09-04 15:46:09 +0000401
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) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000432 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
David Greeneb8f74792007-09-04 15:46:09 +0000433
434 if (NumIdx > 0) {
435 // This requires that the iterator points to contiguous memory.
436 return(getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx,
437 AllowStructLeaf));
438 }
439 else {
440 return(getIndexedType(Ptr, (Value *const*)0, NumIdx, AllowStructLeaf));
441 }
442 }
443
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000444 /// Constructors - Create a getelementptr instruction with a base pointer an
445 /// list of indices. The first ctor can optionally insert before an existing
446 /// instruction, the second appends the new instruction to the specified
447 /// BasicBlock.
David Greeneb8f74792007-09-04 15:46:09 +0000448 template<typename InputIterator>
449 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
450 InputIterator IdxEnd,
451 const std::string &Name = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000452 Instruction *InsertBefore = 0)
David Greeneb8f74792007-09-04 15:46:09 +0000453 : Instruction(PointerType::get(
454 checkType(getIndexedType(Ptr->getType(),
Christopher Lambfe63fb92007-12-11 08:59:05 +0000455 IdxBegin, IdxEnd, true)),
456 cast<PointerType>(Ptr->getType())->getAddressSpace()),
David Greeneb8f74792007-09-04 15:46:09 +0000457 GetElementPtr, 0, 0, InsertBefore) {
458 init(Ptr, IdxBegin, IdxEnd, Name,
459 typename std::iterator_traits<InputIterator>::iterator_category());
460 }
461 template<typename InputIterator>
462 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
463 const std::string &Name, BasicBlock *InsertAtEnd)
464 : Instruction(PointerType::get(
465 checkType(getIndexedType(Ptr->getType(),
Christopher Lambfe63fb92007-12-11 08:59:05 +0000466 IdxBegin, IdxEnd, true)),
467 cast<PointerType>(Ptr->getType())->getAddressSpace()),
David Greeneb8f74792007-09-04 15:46:09 +0000468 GetElementPtr, 0, 0, InsertAtEnd) {
469 init(Ptr, IdxBegin, IdxEnd, Name,
470 typename std::iterator_traits<InputIterator>::iterator_category());
471 }
472
Chris Lattner38bacf22005-05-03 05:43:30 +0000473 /// Constructors - These two constructors are convenience methods because one
474 /// and two index getelementptr instructions are so common.
475 GetElementPtrInst(Value *Ptr, Value *Idx,
Gabor Greif051a9502008-04-06 20:25:17 +0000476 const std::string &Name = "", Instruction *InsertBefore = 0);
Chris Lattner38bacf22005-05-03 05:43:30 +0000477 GetElementPtrInst(Value *Ptr, Value *Idx,
478 const std::string &Name, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000479public:
480 template<typename InputIterator>
481 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
482 InputIterator IdxEnd,
483 const std::string &Name = "",
484 Instruction *InsertBefore = 0) {
485 return new(0/*FIXME*/) GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Name, InsertBefore);
486 }
487 template<typename InputIterator>
488 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
489 const std::string &Name, BasicBlock *InsertAtEnd) {
490 return new(0/*FIXME*/) GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Name, InsertAtEnd);
491 }
492
493 /// Constructors - These two constructors are convenience methods because one
494 /// and two index getelementptr instructions are so common.
495 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
496 const std::string &Name = "", Instruction *InsertBefore = 0) {
497 return new(2/*FIXME*/) GetElementPtrInst(Ptr, Idx, Name, InsertBefore);
498 }
499 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
500 const std::string &Name, BasicBlock *InsertAtEnd) {
501 return new(2/*FIXME*/) GetElementPtrInst(Ptr, Idx, Name, InsertAtEnd);
502 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000503 ~GetElementPtrInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000504
Chris Lattnerf319e832004-10-15 23:52:05 +0000505 virtual GetElementPtrInst *clone() const;
Misha Brukman9769ab22005-04-21 20:19:05 +0000506
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000507 // getType - Overload to return most specific pointer type...
Devang Patel4d4a5e02008-02-23 01:11:02 +0000508 const PointerType *getType() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000509 return reinterpret_cast<const PointerType*>(Instruction::getType());
510 }
511
512 /// getIndexedType - Returns the type of the element that would be loaded with
513 /// a load instruction with the specified parameters.
514 ///
Misha Brukman9769ab22005-04-21 20:19:05 +0000515 /// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000516 /// pointer type.
517 ///
David Greeneb8f74792007-09-04 15:46:09 +0000518 template<typename InputIterator>
Misha Brukman9769ab22005-04-21 20:19:05 +0000519 static const Type *getIndexedType(const Type *Ptr,
David Greeneb8f74792007-09-04 15:46:09 +0000520 InputIterator IdxBegin,
521 InputIterator IdxEnd,
522 bool AllowStructLeaf = false) {
523 return(getIndexedType(Ptr, IdxBegin, IdxEnd, AllowStructLeaf,
524 typename std::iterator_traits<InputIterator>::
525 iterator_category()));
526 }
Chris Lattner38bacf22005-05-03 05:43:30 +0000527 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
Misha Brukman9769ab22005-04-21 20:19:05 +0000528
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000529 inline op_iterator idx_begin() { return op_begin()+1; }
530 inline const_op_iterator idx_begin() const { return op_begin()+1; }
531 inline op_iterator idx_end() { return op_end(); }
532 inline const_op_iterator idx_end() const { return op_end(); }
533
534 Value *getPointerOperand() {
535 return getOperand(0);
536 }
537 const Value *getPointerOperand() const {
538 return getOperand(0);
539 }
540 static unsigned getPointerOperandIndex() {
541 return 0U; // get index for modifying correct operand
542 }
543
Devang Patel4d4a5e02008-02-23 01:11:02 +0000544 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000545 return getNumOperands() - 1;
546 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000547
Devang Patel4d4a5e02008-02-23 01:11:02 +0000548 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000549 return getNumOperands() > 1;
550 }
Chris Lattner6f771d42007-04-14 00:12:57 +0000551
552 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
553 /// zeros. If so, the result pointer and the first operand have the same
554 /// value, just potentially different types.
555 bool hasAllZeroIndices() const;
Chris Lattner6b0974c2007-04-27 20:35:56 +0000556
557 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
558 /// constant integers. If so, the result pointer and the first operand have
559 /// a constant offset between them.
560 bool hasAllConstantIndices() const;
561
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000562
563 // Methods for support type inquiry through isa, cast, and dyn_cast:
564 static inline bool classof(const GetElementPtrInst *) { return true; }
565 static inline bool classof(const Instruction *I) {
566 return (I->getOpcode() == Instruction::GetElementPtr);
567 }
568 static inline bool classof(const Value *V) {
569 return isa<Instruction>(V) && classof(cast<Instruction>(V));
570 }
571};
572
573//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000574// ICmpInst Class
575//===----------------------------------------------------------------------===//
576
577/// This instruction compares its operands according to the predicate given
578/// to the constructor. It only operates on integers, pointers, or packed
579/// vectors of integrals. The two operands must be the same type.
580/// @brief Represent an integer comparison operator.
581class ICmpInst: public CmpInst {
582public:
583 /// This enumeration lists the possible predicates for the ICmpInst. The
584 /// values in the range 0-31 are reserved for FCmpInst while values in the
585 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
586 /// predicate values are not overlapping between the classes.
587 enum Predicate {
588 ICMP_EQ = 32, ///< equal
589 ICMP_NE = 33, ///< not equal
590 ICMP_UGT = 34, ///< unsigned greater than
591 ICMP_UGE = 35, ///< unsigned greater or equal
592 ICMP_ULT = 36, ///< unsigned less than
593 ICMP_ULE = 37, ///< unsigned less or equal
594 ICMP_SGT = 38, ///< signed greater than
595 ICMP_SGE = 39, ///< signed greater or equal
596 ICMP_SLT = 40, ///< signed less than
597 ICMP_SLE = 41, ///< signed less or equal
598 FIRST_ICMP_PREDICATE = ICMP_EQ,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000599 LAST_ICMP_PREDICATE = ICMP_SLE,
600 BAD_ICMP_PREDICATE = ICMP_SLE + 1
Reid Spencer45fb3f32006-11-20 01:22:35 +0000601 };
602
603 /// @brief Constructor with insert-before-instruction semantics.
604 ICmpInst(
605 Predicate pred, ///< The predicate to use for the comparison
606 Value *LHS, ///< The left-hand-side of the expression
607 Value *RHS, ///< The right-hand-side of the expression
608 const std::string &Name = "", ///< Name of the instruction
609 Instruction *InsertBefore = 0 ///< Where to insert
610 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertBefore) {
611 }
612
613 /// @brief Constructor with insert-at-block-end semantics.
614 ICmpInst(
615 Predicate pred, ///< The predicate to use for the comparison
616 Value *LHS, ///< The left-hand-side of the expression
617 Value *RHS, ///< The right-hand-side of the expression
618 const std::string &Name, ///< Name of the instruction
619 BasicBlock *InsertAtEnd ///< Block to insert into.
620 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertAtEnd) {
621 }
622
623 /// @brief Return the predicate for this instruction.
624 Predicate getPredicate() const { return Predicate(SubclassData); }
625
Chris Lattnerb769d562007-01-14 19:41:24 +0000626 /// @brief Set the predicate for this instruction to the specified value.
627 void setPredicate(Predicate P) { SubclassData = P; }
628
Reid Spencer45fb3f32006-11-20 01:22:35 +0000629 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
630 /// @returns the inverse predicate for the instruction's current predicate.
631 /// @brief Return the inverse of the instruction's predicate.
632 Predicate getInversePredicate() const {
633 return getInversePredicate(getPredicate());
634 }
635
636 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
637 /// @returns the inverse predicate for predicate provided in \p pred.
638 /// @brief Return the inverse of a given predicate
639 static Predicate getInversePredicate(Predicate pred);
640
641 /// For example, EQ->EQ, SLE->SGE, ULT->UGT, etc.
642 /// @returns the predicate that would be the result of exchanging the two
643 /// operands of the ICmpInst instruction without changing the result
644 /// produced.
645 /// @brief Return the predicate as if the operands were swapped
646 Predicate getSwappedPredicate() const {
647 return getSwappedPredicate(getPredicate());
648 }
649
650 /// This is a static version that you can use without an instruction
651 /// available.
652 /// @brief Return the predicate as if the operands were swapped.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000653 static Predicate getSwappedPredicate(Predicate pred);
654
655 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
656 /// @returns the predicate that would be the result if the operand were
657 /// regarded as signed.
658 /// @brief Return the signed version of the predicate
659 Predicate getSignedPredicate() const {
660 return getSignedPredicate(getPredicate());
661 }
662
663 /// This is a static version that you can use without an instruction.
664 /// @brief Return the signed version of the predicate.
665 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000666
Nick Lewycky4189a532008-01-28 03:48:02 +0000667 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
668 /// @returns the predicate that would be the result if the operand were
669 /// regarded as unsigned.
670 /// @brief Return the unsigned version of the predicate
671 Predicate getUnsignedPredicate() const {
672 return getUnsignedPredicate(getPredicate());
673 }
674
675 /// This is a static version that you can use without an instruction.
676 /// @brief Return the unsigned version of the predicate.
677 static Predicate getUnsignedPredicate(Predicate pred);
678
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000679 /// isEquality - Return true if this predicate is either EQ or NE. This also
680 /// tests for commutativity.
681 static bool isEquality(Predicate P) {
682 return P == ICMP_EQ || P == ICMP_NE;
683 }
684
685 /// isEquality - Return true if this predicate is either EQ or NE. This also
686 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000687 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000688 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000689 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000690
691 /// @returns true if the predicate of this ICmpInst is commutative
692 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000693 bool isCommutative() const { return isEquality(); }
694
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000695 /// isRelational - Return true if the predicate is relational (not EQ or NE).
696 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +0000697 bool isRelational() const {
698 return !isEquality();
699 }
700
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000701 /// isRelational - Return true if the predicate is relational (not EQ or NE).
702 ///
703 static bool isRelational(Predicate P) {
704 return !isEquality(P);
705 }
706
Reid Spencere4d87aa2006-12-23 06:05:41 +0000707 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
708 /// @brief Determine if this instruction's predicate is signed.
Chris Lattner5bda9e42007-09-15 06:51:03 +0000709 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000710
711 /// @returns true if the predicate provided is signed, false otherwise
712 /// @brief Determine if the predicate is signed.
713 static bool isSignedPredicate(Predicate pred);
714
Reid Spencer3da43842007-02-28 22:00:54 +0000715 /// Initialize a set of values that all satisfy the predicate with C.
716 /// @brief Make a ConstantRange for a relation with a constant value.
717 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
718
Reid Spencer45fb3f32006-11-20 01:22:35 +0000719 /// Exchange the two operands to this instruction in such a way that it does
720 /// not modify the semantics of the instruction. The predicate value may be
721 /// changed to retain the same result if the predicate is order dependent
722 /// (e.g. ult).
723 /// @brief Swap operands and adjust predicate.
724 void swapOperands() {
725 SubclassData = getSwappedPredicate();
726 std::swap(Ops[0], Ops[1]);
727 }
728
Chris Lattnercd406fe2007-08-24 20:48:18 +0000729 virtual ICmpInst *clone() const;
730
Reid Spencer45fb3f32006-11-20 01:22:35 +0000731 // Methods for support type inquiry through isa, cast, and dyn_cast:
732 static inline bool classof(const ICmpInst *) { return true; }
733 static inline bool classof(const Instruction *I) {
734 return I->getOpcode() == Instruction::ICmp;
735 }
736 static inline bool classof(const Value *V) {
737 return isa<Instruction>(V) && classof(cast<Instruction>(V));
738 }
739};
740
741//===----------------------------------------------------------------------===//
742// FCmpInst Class
743//===----------------------------------------------------------------------===//
744
745/// This instruction compares its operands according to the predicate given
746/// to the constructor. It only operates on floating point values or packed
747/// vectors of floating point values. The operands must be identical types.
748/// @brief Represents a floating point comparison operator.
749class FCmpInst: public CmpInst {
750public:
751 /// This enumeration lists the possible predicates for the FCmpInst. Values
752 /// in the range 0-31 are reserved for FCmpInst.
753 enum Predicate {
754 // Opcode U L G E Intuitive operation
755 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
756 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
757 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
758 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
759 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
760 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
761 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
762 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
763 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
764 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
765 FCMP_UGT =10, ///< 1 0 1 0 True if unordered or greater than
766 FCMP_UGE =11, ///< 1 0 1 1 True if unordered, greater than, or equal
767 FCMP_ULT =12, ///< 1 1 0 0 True if unordered or less than
768 FCMP_ULE =13, ///< 1 1 0 1 True if unordered, less than, or equal
769 FCMP_UNE =14, ///< 1 1 1 0 True if unordered or not equal
770 FCMP_TRUE =15, ///< 1 1 1 1 Always true (always folded)
771 FIRST_FCMP_PREDICATE = FCMP_FALSE,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000772 LAST_FCMP_PREDICATE = FCMP_TRUE,
773 BAD_FCMP_PREDICATE = FCMP_TRUE + 1
Reid Spencer45fb3f32006-11-20 01:22:35 +0000774 };
775
776 /// @brief Constructor with insert-before-instruction semantics.
777 FCmpInst(
778 Predicate pred, ///< The predicate to use for the comparison
779 Value *LHS, ///< The left-hand-side of the expression
780 Value *RHS, ///< The right-hand-side of the expression
781 const std::string &Name = "", ///< Name of the instruction
782 Instruction *InsertBefore = 0 ///< Where to insert
783 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertBefore) {
784 }
785
786 /// @brief Constructor with insert-at-block-end semantics.
787 FCmpInst(
788 Predicate pred, ///< The predicate to use for the comparison
789 Value *LHS, ///< The left-hand-side of the expression
790 Value *RHS, ///< The right-hand-side of the expression
791 const std::string &Name, ///< Name of the instruction
792 BasicBlock *InsertAtEnd ///< Block to insert into.
793 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertAtEnd) {
794 }
795
796 /// @brief Return the predicate for this instruction.
797 Predicate getPredicate() const { return Predicate(SubclassData); }
798
Chris Lattnerb769d562007-01-14 19:41:24 +0000799 /// @brief Set the predicate for this instruction to the specified value.
800 void setPredicate(Predicate P) { SubclassData = P; }
801
Reid Spencer45fb3f32006-11-20 01:22:35 +0000802 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
803 /// @returns the inverse predicate for the instructions current predicate.
804 /// @brief Return the inverse of the predicate
805 Predicate getInversePredicate() const {
806 return getInversePredicate(getPredicate());
807 }
808
809 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
810 /// @returns the inverse predicate for \p pred.
811 /// @brief Return the inverse of a given predicate
812 static Predicate getInversePredicate(Predicate pred);
813
814 /// For example, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
815 /// @returns the predicate that would be the result of exchanging the two
816 /// operands of the ICmpInst instruction without changing the result
817 /// produced.
818 /// @brief Return the predicate as if the operands were swapped
819 Predicate getSwappedPredicate() const {
820 return getSwappedPredicate(getPredicate());
821 }
822
823 /// This is a static version that you can use without an instruction
824 /// available.
825 /// @brief Return the predicate as if the operands were swapped.
826 static Predicate getSwappedPredicate(Predicate Opcode);
827
828 /// This also tests for commutativity. If isEquality() returns true then
829 /// the predicate is also commutative. Only the equality predicates are
830 /// commutative.
831 /// @returns true if the predicate of this instruction is EQ or NE.
832 /// @brief Determine if this is an equality predicate.
833 bool isEquality() const {
834 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
835 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
836 }
837 bool isCommutative() const { return isEquality(); }
838
839 /// @returns true if the predicate is relational (not EQ or NE).
840 /// @brief Determine if this a relational predicate.
841 bool isRelational() const { return !isEquality(); }
842
843 /// Exchange the two operands to this instruction in such a way that it does
844 /// not modify the semantics of the instruction. The predicate value may be
845 /// changed to retain the same result if the predicate is order dependent
846 /// (e.g. ult).
847 /// @brief Swap operands and adjust predicate.
848 void swapOperands() {
849 SubclassData = getSwappedPredicate();
850 std::swap(Ops[0], Ops[1]);
851 }
852
Chris Lattnercd406fe2007-08-24 20:48:18 +0000853 virtual FCmpInst *clone() const;
854
Reid Spencer45fb3f32006-11-20 01:22:35 +0000855 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
856 static inline bool classof(const FCmpInst *) { return true; }
857 static inline bool classof(const Instruction *I) {
858 return I->getOpcode() == Instruction::FCmp;
859 }
860 static inline bool classof(const Value *V) {
861 return isa<Instruction>(V) && classof(cast<Instruction>(V));
862 }
863};
864
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000865//===----------------------------------------------------------------------===//
866// CallInst Class
867//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000868/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +0000869/// machine's calling convention. This class uses low bit of the SubClassData
870/// field to indicate whether or not this is a tail call. The rest of the bits
871/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000872///
David Greene52eec542007-08-01 03:43:44 +0000873
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000874class CallInst : public Instruction {
Chris Lattner58d74912008-03-12 17:45:29 +0000875 PAListPtr ParamAttrs; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000876 CallInst(const CallInst &CI);
Chris Lattnerd54f4322007-02-13 00:58:44 +0000877 void init(Value *Func, Value* const *Params, unsigned NumParams);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000878 void init(Value *Func, Value *Actual1, Value *Actual2);
879 void init(Value *Func, Value *Actual);
880 void init(Value *Func);
881
David Greene52eec542007-08-01 03:43:44 +0000882 template<typename InputIterator>
883 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
884 const std::string &Name,
885 // This argument ensures that we have an iterator we can
886 // do arithmetic on in constant time
887 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000888 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greene52eec542007-08-01 03:43:44 +0000889
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000890 // This requires that the iterator points to contiguous memory.
891 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
David Greene52eec542007-08-01 03:43:44 +0000892 setName(Name);
893 }
894
David Greene52eec542007-08-01 03:43:44 +0000895 /// Construct a CallInst given a range of arguments. InputIterator
896 /// must be a random-access iterator pointing to contiguous storage
897 /// (e.g. a std::vector<>::iterator). Checks are made for
898 /// random-accessness but not for contiguous storage as that would
899 /// incur runtime overhead.
900 /// @brief Construct a CallInst from a range of arguments
901 template<typename InputIterator>
902 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
903 const std::string &Name = "", Instruction *InsertBefore = 0)
904 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
905 ->getElementType())->getReturnType(),
906 Instruction::Call, 0, 0, InsertBefore) {
907 init(Func, ArgBegin, ArgEnd, Name,
908 typename std::iterator_traits<InputIterator>::iterator_category());
909 }
910
911 /// Construct a CallInst given a range of arguments. InputIterator
912 /// must be a random-access iterator pointing to contiguous storage
913 /// (e.g. a std::vector<>::iterator). Checks are made for
914 /// random-accessness but not for contiguous storage as that would
915 /// incur runtime overhead.
916 /// @brief Construct a CallInst from a range of arguments
917 template<typename InputIterator>
918 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
919 const std::string &Name, BasicBlock *InsertAtEnd)
920 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
921 ->getElementType())->getReturnType(),
922 Instruction::Call, 0, 0, InsertAtEnd) {
923 init(Func, ArgBegin, ArgEnd, Name,
924 typename std::iterator_traits<InputIterator>::iterator_category());
925 }
926
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000927 CallInst(Value *F, Value *Actual, const std::string& Name = "",
928 Instruction *InsertBefore = 0);
929 CallInst(Value *F, Value *Actual, const std::string& Name,
930 BasicBlock *InsertAtEnd);
Misha Brukman9769ab22005-04-21 20:19:05 +0000931 explicit CallInst(Value *F, const std::string &Name = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000932 Instruction *InsertBefore = 0);
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000933 CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000934public:
935 template<typename InputIterator>
Evan Chengd69bb1a2008-05-05 17:41:03 +0000936 static CallInst *Create(Value *Func, InputIterator ArgBegin,
937 InputIterator ArgEnd,
938 const std::string &Name = "",
939 Instruction *InsertBefore = 0) {
940 return new(ArgEnd - ArgBegin + 1)
941 CallInst(Func, ArgBegin, ArgEnd, Name, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000942 }
943 template<typename InputIterator>
Evan Chengd69bb1a2008-05-05 17:41:03 +0000944 static CallInst *Create(Value *Func, InputIterator ArgBegin,
945 InputIterator ArgEnd, const std::string &Name,
946 BasicBlock *InsertAtEnd) {
947 return new(ArgEnd - ArgBegin + 1)
948 CallInst(Func, ArgBegin, ArgEnd, Name, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000949 }
950 static CallInst *Create(Value *F, Value *Actual, const std::string& Name = "",
951 Instruction *InsertBefore = 0) {
952 return new(2) CallInst(F, Actual, Name, InsertBefore);
953 }
954 static CallInst *Create(Value *F, Value *Actual, const std::string& Name,
955 BasicBlock *InsertAtEnd) {
956 return new(2) CallInst(F, Actual, Name, InsertAtEnd);
957 }
958 static CallInst *Create(Value *F, const std::string &Name = "",
959 Instruction *InsertBefore = 0) {
960 return new(1) CallInst(F, Name, InsertBefore);
961 }
Evan Cheng34cd4a42008-05-05 18:30:58 +0000962 static CallInst *Create(Value *F, const std::string &Name,
963 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +0000964 return new(1) CallInst(F, Name, InsertAtEnd);
965 }
966
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000967 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000968
Chris Lattnerf319e832004-10-15 23:52:05 +0000969 virtual CallInst *clone() const;
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000970
Chris Lattner3340ffe2005-05-06 20:26:26 +0000971 bool isTailCall() const { return SubclassData & 1; }
972 void setTailCall(bool isTailCall = true) {
Jeff Cohen39cef602005-05-07 02:44:04 +0000973 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
Chris Lattner3340ffe2005-05-06 20:26:26 +0000974 }
975
976 /// getCallingConv/setCallingConv - Get or set the calling convention of this
977 /// function call.
978 unsigned getCallingConv() const { return SubclassData >> 1; }
979 void setCallingConv(unsigned CC) {
980 SubclassData = (SubclassData & 1) | (CC << 1);
981 }
Chris Lattnerddb6db42005-05-06 05:51:46 +0000982
Chris Lattner041221c2008-03-13 04:33:03 +0000983 /// getParamAttrs - Return the parameter attributes for this call.
984 ///
Chris Lattner58d74912008-03-12 17:45:29 +0000985 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
Reid Spencer4746ecf2007-04-09 15:01:12 +0000986
Chris Lattner041221c2008-03-13 04:33:03 +0000987 /// setParamAttrs - Sets the parameter attributes for this call.
Chris Lattner58d74912008-03-12 17:45:29 +0000988 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +0000989
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000990 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattnerd5d94df2008-03-13 05:00:21 +0000991 bool paramHasAttr(unsigned i, unsigned attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000992
Dale Johannesen08e78b12008-02-22 17:49:45 +0000993 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +0000994 unsigned getParamAlignment(unsigned i) const {
995 return ParamAttrs.getParamAlignment(i);
996 }
Dale Johannesen08e78b12008-02-22 17:49:45 +0000997
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000998 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +0000999 bool doesNotAccessMemory() const {
1000 return paramHasAttr(0, ParamAttr::ReadNone);
1001 }
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001002
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001003 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001004 bool onlyReadsMemory() const {
1005 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
1006 }
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001007
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001008 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001009 bool doesNotReturn() const {
1010 return paramHasAttr(0, ParamAttr::NoReturn);
1011 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001012
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001013 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001014 bool doesNotThrow() const {
1015 return paramHasAttr(0, ParamAttr::NoUnwind);
1016 }
Duncan Sandsf0c33542007-12-19 21:13:37 +00001017 void setDoesNotThrow(bool doesNotThrow = true);
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001018
Devang Patel41e23972008-03-03 21:46:28 +00001019 /// @brief Determine if the call returns a structure through first
1020 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001021 bool hasStructRetAttr() const {
1022 // Be friendly and also check the callee.
1023 return paramHasAttr(1, ParamAttr::StructRet);
1024 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001025
Evan Chengf4a54982008-01-12 18:57:32 +00001026 /// @brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001027 bool hasByValArgument() const {
1028 return ParamAttrs.hasAttrSomewhere(ParamAttr::ByVal);
1029 }
Evan Chengf4a54982008-01-12 18:57:32 +00001030
Chris Lattner721aef62004-11-18 17:46:57 +00001031 /// getCalledFunction - Return the function being called by this instruction
1032 /// if it is a direct call. If it is a call through a function pointer,
1033 /// return null.
1034 Function *getCalledFunction() const {
Dan Gohman11a7dbf2007-09-24 15:46:02 +00001035 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001036 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001037
Reid Spencerc25ec252006-12-29 04:10:59 +00001038 /// getCalledValue - Get a pointer to the function that is invoked by this
1039 /// instruction
Devang Patel4d4a5e02008-02-23 01:11:02 +00001040 const Value *getCalledValue() const { return getOperand(0); }
1041 Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001042
1043 // Methods for support type inquiry through isa, cast, and dyn_cast:
1044 static inline bool classof(const CallInst *) { return true; }
1045 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001046 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001047 }
1048 static inline bool classof(const Value *V) {
1049 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1050 }
1051};
1052
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001053//===----------------------------------------------------------------------===//
1054// SelectInst Class
1055//===----------------------------------------------------------------------===//
1056
1057/// SelectInst - This class represents the LLVM 'select' instruction.
1058///
1059class SelectInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +00001060 Use Ops[3];
1061
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001062 void init(Value *C, Value *S1, Value *S2) {
Chris Lattner454928e2005-01-29 00:31:36 +00001063 Ops[0].init(C, this);
1064 Ops[1].init(S1, this);
1065 Ops[2].init(S2, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001066 }
1067
Chris Lattner454928e2005-01-29 00:31:36 +00001068 SelectInst(const SelectInst &SI)
1069 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
1070 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
1071 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001072 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
1073 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001074 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001075 init(C, S1, S2);
Chris Lattner910c80a2007-02-24 00:55:48 +00001076 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001077 }
1078 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1079 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001080 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001081 init(C, S1, S2);
Chris Lattner910c80a2007-02-24 00:55:48 +00001082 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001083 }
Gabor Greif051a9502008-04-06 20:25:17 +00001084public:
Evan Chengd69bb1a2008-05-05 17:41:03 +00001085 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1086 const std::string &Name = "",
1087 Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00001088 return new(3) SelectInst(C, S1, S2, Name, InsertBefore);
1089 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001090 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1091 const std::string &Name, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001092 return new(3) SelectInst(C, S1, S2, Name, InsertAtEnd);
1093 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001094
Chris Lattner454928e2005-01-29 00:31:36 +00001095 Value *getCondition() const { return Ops[0]; }
1096 Value *getTrueValue() const { return Ops[1]; }
1097 Value *getFalseValue() const { return Ops[2]; }
1098
1099 /// Transparently provide more efficient getOperand methods.
1100 Value *getOperand(unsigned i) const {
1101 assert(i < 3 && "getOperand() out of range!");
1102 return Ops[i];
1103 }
1104 void setOperand(unsigned i, Value *Val) {
1105 assert(i < 3 && "setOperand() out of range!");
1106 Ops[i] = Val;
1107 }
1108 unsigned getNumOperands() const { return 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001109
1110 OtherOps getOpcode() const {
1111 return static_cast<OtherOps>(Instruction::getOpcode());
1112 }
1113
Chris Lattnerf319e832004-10-15 23:52:05 +00001114 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001115
1116 // Methods for support type inquiry through isa, cast, and dyn_cast:
1117 static inline bool classof(const SelectInst *) { return true; }
1118 static inline bool classof(const Instruction *I) {
1119 return I->getOpcode() == Instruction::Select;
1120 }
1121 static inline bool classof(const Value *V) {
1122 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1123 }
1124};
1125
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001126//===----------------------------------------------------------------------===//
1127// VAArgInst Class
1128//===----------------------------------------------------------------------===//
1129
1130/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001131/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001132///
Chris Lattner454928e2005-01-29 00:31:36 +00001133class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001134 VAArgInst(const VAArgInst &VAA)
Chris Lattner454928e2005-01-29 00:31:36 +00001135 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001136public:
1137 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
1138 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001139 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001140 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001141 }
1142 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
1143 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001144 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001145 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001146 }
1147
Chris Lattnerf319e832004-10-15 23:52:05 +00001148 virtual VAArgInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001149
1150 // Methods for support type inquiry through isa, cast, and dyn_cast:
1151 static inline bool classof(const VAArgInst *) { return true; }
1152 static inline bool classof(const Instruction *I) {
1153 return I->getOpcode() == VAArg;
1154 }
1155 static inline bool classof(const Value *V) {
1156 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1157 }
1158};
1159
1160//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001161// ExtractElementInst Class
1162//===----------------------------------------------------------------------===//
1163
1164/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001165/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001166///
1167class ExtractElementInst : public Instruction {
1168 Use Ops[2];
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001169 ExtractElementInst(const ExtractElementInst &EE) :
Robert Bocchinof9993442006-01-17 20:05:59 +00001170 Instruction(EE.getType(), ExtractElement, Ops, 2) {
1171 Ops[0].init(EE.Ops[0], this);
1172 Ops[1].init(EE.Ops[1], this);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001173 }
1174
1175public:
Gabor Greif051a9502008-04-06 20:25:17 +00001176 // allocate space for exactly two operands
1177 void *operator new(size_t s) {
1178 return User::operator new(s, 2); // FIXME: unsigned Idx forms of constructor?
1179 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001180 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
1181 Instruction *InsertBefore = 0);
Chris Lattner06a248c22006-10-05 06:24:58 +00001182 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
1183 Instruction *InsertBefore = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001184 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
1185 BasicBlock *InsertAtEnd);
Chris Lattner06a248c22006-10-05 06:24:58 +00001186 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
1187 BasicBlock *InsertAtEnd);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001188
Chris Lattnerfa495842006-04-08 04:04:54 +00001189 /// isValidOperands - Return true if an extractelement instruction can be
1190 /// formed with the specified operands.
1191 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001192
Robert Bocchino49b78a52006-01-10 19:04:13 +00001193 virtual ExtractElementInst *clone() const;
1194
Robert Bocchino49b78a52006-01-10 19:04:13 +00001195 /// Transparently provide more efficient getOperand methods.
1196 Value *getOperand(unsigned i) const {
1197 assert(i < 2 && "getOperand() out of range!");
1198 return Ops[i];
1199 }
1200 void setOperand(unsigned i, Value *Val) {
1201 assert(i < 2 && "setOperand() out of range!");
1202 Ops[i] = Val;
1203 }
1204 unsigned getNumOperands() const { return 2; }
1205
1206 // Methods for support type inquiry through isa, cast, and dyn_cast:
1207 static inline bool classof(const ExtractElementInst *) { return true; }
1208 static inline bool classof(const Instruction *I) {
1209 return I->getOpcode() == Instruction::ExtractElement;
1210 }
1211 static inline bool classof(const Value *V) {
1212 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1213 }
1214};
1215
1216//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001217// InsertElementInst Class
1218//===----------------------------------------------------------------------===//
1219
1220/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001221/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001222///
1223class InsertElementInst : public Instruction {
1224 Use Ops[3];
Chris Lattner6a56ed42006-04-14 22:20:07 +00001225 InsertElementInst(const InsertElementInst &IE);
Gabor Greif051a9502008-04-06 20:25:17 +00001226 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1227 const std::string &Name = "",Instruction *InsertBefore = 0);
1228 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1229 const std::string &Name = "",Instruction *InsertBefore = 0);
1230 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1231 const std::string &Name, BasicBlock *InsertAtEnd);
1232 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1233 const std::string &Name, BasicBlock *InsertAtEnd);
Robert Bocchinof9993442006-01-17 20:05:59 +00001234public:
Gabor Greif051a9502008-04-06 20:25:17 +00001235 static InsertElementInst *Create(const InsertElementInst &IE) {
1236 return new(IE.getNumOperands()) InsertElementInst(IE);
1237 }
1238 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1239 const std::string &Name = "",Instruction *InsertBefore = 0) {
1240 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
1241 }
1242 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001243 const std::string &Name = "",
1244 Instruction *InsertBefore = 0) {
1245 return new(3/*FIXME*/)
1246 InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001247 }
1248 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001249 const std::string &Name,
1250 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001251 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertAtEnd);
1252 }
1253 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001254 const std::string &Name,
1255 BasicBlock *InsertAtEnd) {
1256 return new(3/*FIXME*/)
1257 InsertElementInst(Vec, NewElt, Idx, Name, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001258 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001259
Chris Lattnerfa495842006-04-08 04:04:54 +00001260 /// isValidOperands - Return true if an insertelement instruction can be
1261 /// formed with the specified operands.
1262 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1263 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001264
Robert Bocchinof9993442006-01-17 20:05:59 +00001265 virtual InsertElementInst *clone() const;
1266
Reid Spencerac9dcb92007-02-15 03:39:18 +00001267 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001268 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001269 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001270 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001271 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001272
Robert Bocchinof9993442006-01-17 20:05:59 +00001273 /// Transparently provide more efficient getOperand methods.
1274 Value *getOperand(unsigned i) const {
1275 assert(i < 3 && "getOperand() out of range!");
1276 return Ops[i];
1277 }
1278 void setOperand(unsigned i, Value *Val) {
1279 assert(i < 3 && "setOperand() out of range!");
1280 Ops[i] = Val;
1281 }
1282 unsigned getNumOperands() const { return 3; }
1283
1284 // Methods for support type inquiry through isa, cast, and dyn_cast:
1285 static inline bool classof(const InsertElementInst *) { return true; }
1286 static inline bool classof(const Instruction *I) {
1287 return I->getOpcode() == Instruction::InsertElement;
1288 }
1289 static inline bool classof(const Value *V) {
1290 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1291 }
1292};
1293
1294//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001295// ShuffleVectorInst Class
1296//===----------------------------------------------------------------------===//
1297
1298/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1299/// input vectors.
1300///
1301class ShuffleVectorInst : public Instruction {
1302 Use Ops[3];
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001303 ShuffleVectorInst(const ShuffleVectorInst &IE);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001304public:
Gabor Greif051a9502008-04-06 20:25:17 +00001305 // allocate space for exactly three operands
1306 void *operator new(size_t s) {
1307 return User::operator new(s, 3);
1308 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001309 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1310 const std::string &Name = "", Instruction *InsertBefor = 0);
1311 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1312 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001313
Chris Lattnerfa495842006-04-08 04:04:54 +00001314 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001315 /// formed with the specified operands.
1316 static bool isValidOperands(const Value *V1, const Value *V2,
1317 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001318
Chris Lattner9fc18d22006-04-08 01:15:18 +00001319 virtual ShuffleVectorInst *clone() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001320
Reid Spencerac9dcb92007-02-15 03:39:18 +00001321 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001322 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001323 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001324 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001325 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001326
Chris Lattner9fc18d22006-04-08 01:15:18 +00001327 /// Transparently provide more efficient getOperand methods.
Chris Lattner4fa01442008-03-02 05:32:05 +00001328 const Value *getOperand(unsigned i) const {
1329 assert(i < 3 && "getOperand() out of range!");
1330 return Ops[i];
1331 }
1332 Value *getOperand(unsigned i) {
Chris Lattner9fc18d22006-04-08 01:15:18 +00001333 assert(i < 3 && "getOperand() out of range!");
1334 return Ops[i];
1335 }
1336 void setOperand(unsigned i, Value *Val) {
1337 assert(i < 3 && "setOperand() out of range!");
1338 Ops[i] = Val;
1339 }
1340 unsigned getNumOperands() const { return 3; }
Chris Lattner8728f192008-03-02 05:28:33 +00001341
1342 /// getMaskValue - Return the index from the shuffle mask for the specified
1343 /// output result. This is either -1 if the element is undef or a number less
1344 /// than 2*numelements.
1345 int getMaskValue(unsigned i) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001346
Chris Lattner9fc18d22006-04-08 01:15:18 +00001347 // Methods for support type inquiry through isa, cast, and dyn_cast:
1348 static inline bool classof(const ShuffleVectorInst *) { return true; }
1349 static inline bool classof(const Instruction *I) {
1350 return I->getOpcode() == Instruction::ShuffleVector;
1351 }
1352 static inline bool classof(const Value *V) {
1353 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1354 }
1355};
1356
1357
1358//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001359// PHINode Class
1360//===----------------------------------------------------------------------===//
1361
1362// PHINode - The PHINode class is used to represent the magical mystical PHI
1363// node, that can not exist in nature, but can be synthesized in a computer
1364// scientist's overactive imagination.
1365//
1366class PHINode : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001367 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00001368 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1369 /// the number actually in use.
1370 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001371 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00001372 // allocate space for exactly zero operands
1373 void *operator new(size_t s) {
1374 return User::operator new(s, 0);
1375 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001376 explicit PHINode(const Type *Ty, const std::string &Name = "",
1377 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001378 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Chris Lattner454928e2005-01-29 00:31:36 +00001379 ReservedSpace(0) {
Chris Lattner910c80a2007-02-24 00:55:48 +00001380 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001381 }
1382
1383 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001384 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Chris Lattner454928e2005-01-29 00:31:36 +00001385 ReservedSpace(0) {
Chris Lattner910c80a2007-02-24 00:55:48 +00001386 setName(Name);
Chris Lattner454928e2005-01-29 00:31:36 +00001387 }
Gabor Greif051a9502008-04-06 20:25:17 +00001388public:
1389 static PHINode *Create(const Type *Ty, const std::string &Name = "",
1390 Instruction *InsertBefore = 0) {
1391 return new PHINode(Ty, Name, InsertBefore);
1392 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001393 static PHINode *Create(const Type *Ty, const std::string &Name,
1394 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001395 return new PHINode(Ty, Name, InsertAtEnd);
1396 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001397 ~PHINode();
1398
Chris Lattner454928e2005-01-29 00:31:36 +00001399 /// reserveOperandSpace - This method can be used to avoid repeated
1400 /// reallocation of PHI operand lists by reserving space for the correct
1401 /// number of operands before adding them. Unlike normal vector reserves,
1402 /// this method can also be used to trim the operand space.
1403 void reserveOperandSpace(unsigned NumValues) {
1404 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001405 }
1406
Chris Lattnerf319e832004-10-15 23:52:05 +00001407 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001408
1409 /// getNumIncomingValues - Return the number of incoming edges
1410 ///
Chris Lattner454928e2005-01-29 00:31:36 +00001411 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001412
Reid Spencerc773de62006-05-19 19:07:54 +00001413 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001414 ///
1415 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001416 assert(i*2 < getNumOperands() && "Invalid value number!");
1417 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001418 }
1419 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +00001420 assert(i*2 < getNumOperands() && "Invalid value number!");
1421 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001422 }
Chris Lattner454928e2005-01-29 00:31:36 +00001423 unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001424 return i*2;
1425 }
1426
Reid Spencerc773de62006-05-19 19:07:54 +00001427 /// getIncomingBlock - Return incoming basic block number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001428 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00001429 BasicBlock *getIncomingBlock(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001430 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001431 }
1432 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Chris Lattner454928e2005-01-29 00:31:36 +00001433 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001434 }
1435 unsigned getOperandNumForIncomingBlock(unsigned i) {
1436 return i*2+1;
1437 }
1438
1439 /// addIncoming - Add an incoming value to the end of the PHI list
1440 ///
1441 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00001442 assert(V && "PHI node got a null value!");
1443 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001444 assert(getType() == V->getType() &&
1445 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +00001446 unsigned OpNo = NumOperands;
1447 if (OpNo+2 > ReservedSpace)
1448 resizeOperands(0); // Get more space!
1449 // Initialize some new operands.
1450 NumOperands = OpNo+2;
1451 OperandList[OpNo].init(V, this);
1452 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001453 }
Misha Brukman9769ab22005-04-21 20:19:05 +00001454
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001455 /// removeIncomingValue - Remove an incoming value. This is useful if a
1456 /// predecessor basic block is deleted. The value removed is returned.
1457 ///
1458 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1459 /// is true), the PHI node is destroyed and any uses of it are replaced with
1460 /// dummy values. The only time there should be zero incoming values to a PHI
1461 /// node is when the block is dead, so this strategy is sound.
1462 ///
1463 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1464
1465 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1466 int Idx = getBasicBlockIndex(BB);
1467 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1468 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1469 }
1470
Misha Brukman9769ab22005-04-21 20:19:05 +00001471 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001472 /// block in the value list for this PHI. Returns -1 if no instance.
1473 ///
1474 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001475 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00001476 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Chris Lattner454928e2005-01-29 00:31:36 +00001477 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001478 return -1;
1479 }
1480
1481 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1482 return getIncomingValue(getBasicBlockIndex(BB));
1483 }
1484
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001485 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00001486 /// same value, return the value, otherwise return null.
1487 ///
Chris Lattner9acbd612005-08-05 00:49:06 +00001488 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001489
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001490 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1491 static inline bool classof(const PHINode *) { return true; }
1492 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001493 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001494 }
1495 static inline bool classof(const Value *V) {
1496 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1497 }
Chris Lattner454928e2005-01-29 00:31:36 +00001498 private:
1499 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001500};
1501
1502//===----------------------------------------------------------------------===//
1503// ReturnInst Class
1504//===----------------------------------------------------------------------===//
1505
1506//===---------------------------------------------------------------------------
1507/// ReturnInst - Return a value (possibly void), from a function. Execution
1508/// does not continue in this function any longer.
1509///
1510class ReturnInst : public TerminatorInst {
Devang Patel64d4e612008-02-26 17:56:20 +00001511 Use RetVal;
Chris Lattner910c80a2007-02-24 00:55:48 +00001512 ReturnInst(const ReturnInst &RI);
Devang Patelfea98302008-02-26 19:15:26 +00001513 void init(Value * const* retVals, unsigned N);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001514
Gabor Greif051a9502008-04-06 20:25:17 +00001515private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001516 // ReturnInst constructors:
1517 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001518 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001519 // ReturnInst(Value* X) - 'ret X' instruction
1520 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
1521 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1522 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
1523 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
Devang Patele6be34a2008-02-27 01:20:54 +00001524 // ReturnInst(Value* X, N) - 'ret X,X+1...X+N-1' instruction
1525 // ReturnInst(Value* X, N, Inst *) - 'ret X,X+1...X+N-1', insert before I
1526 // ReturnInst(Value* X, N, BB *) - 'ret X,X+1...X+N-1', insert @ end of BB
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001527 //
1528 // NOTE: If the Value* passed is of type void then the constructor behaves as
1529 // if it was passed NULL.
Chris Lattner910c80a2007-02-24 00:55:48 +00001530 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1531 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
Devang Patelf4511cd2008-02-26 19:38:17 +00001532 ReturnInst(Value * const* retVals, unsigned N);
1533 ReturnInst(Value * const* retVals, unsigned N, Instruction *InsertBefore);
1534 ReturnInst(Value * const* retVals, unsigned N, BasicBlock *InsertAtEnd);
Chris Lattner910c80a2007-02-24 00:55:48 +00001535 explicit ReturnInst(BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001536public:
1537 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
1538 return new(!!retVal) ReturnInst(retVal, InsertBefore);
1539 }
1540 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
1541 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
1542 }
1543 static ReturnInst* Create(Value * const* retVals, unsigned N) {
1544 return new(N) ReturnInst(retVals, N);
1545 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001546 static ReturnInst* Create(Value * const* retVals, unsigned N,
1547 Instruction *InsertBefore) {
Gabor Greif051a9502008-04-06 20:25:17 +00001548 return new(N) ReturnInst(retVals, N, InsertBefore);
1549 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001550 static ReturnInst* Create(Value * const* retVals, unsigned N,
1551 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001552 return new(N) ReturnInst(retVals, N, InsertAtEnd);
1553 }
1554 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
1555 return new(0) ReturnInst(InsertAtEnd);
1556 }
Devang Patel57ef4f42008-02-23 00:35:18 +00001557 virtual ~ReturnInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001558
Chris Lattnerf319e832004-10-15 23:52:05 +00001559 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001560
Devang Patel1eafa062008-03-11 17:35:03 +00001561 Value *getOperand(unsigned n = 0) const {
Devang Patel22a8a732008-03-07 20:08:07 +00001562 if (getNumOperands() > 1)
Devang Patel1eafa062008-03-11 17:35:03 +00001563 return TerminatorInst::getOperand(n);
Devang Patel22a8a732008-03-07 20:08:07 +00001564 else
Devang Patel64d4e612008-02-26 17:56:20 +00001565 return RetVal;
Devang Patel64d4e612008-02-26 17:56:20 +00001566 }
1567
Devang Patel1eafa062008-03-11 17:35:03 +00001568 Value *getReturnValue(unsigned n = 0) const {
1569 return getOperand(n);
1570 }
1571
Chris Lattner454928e2005-01-29 00:31:36 +00001572 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001573
1574 // Methods for support type inquiry through isa, cast, and dyn_cast:
1575 static inline bool classof(const ReturnInst *) { return true; }
1576 static inline bool classof(const Instruction *I) {
1577 return (I->getOpcode() == Instruction::Ret);
1578 }
1579 static inline bool classof(const Value *V) {
1580 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1581 }
Chris Lattner454928e2005-01-29 00:31:36 +00001582 private:
1583 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1584 virtual unsigned getNumSuccessorsV() const;
1585 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001586};
1587
1588//===----------------------------------------------------------------------===//
1589// BranchInst Class
1590//===----------------------------------------------------------------------===//
1591
1592//===---------------------------------------------------------------------------
1593/// BranchInst - Conditional or Unconditional Branch instruction.
1594///
1595class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001596 /// Ops list - Branches are strange. The operands are ordered:
1597 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
1598 /// they don't have to check for cond/uncond branchness.
1599 Use Ops[3];
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001600 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00001601 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001602 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1603 // BranchInst(BB *B) - 'br B'
1604 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1605 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1606 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1607 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1608 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00001609 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001610 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00001611 Instruction *InsertBefore = 0);
1612 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001613 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00001614 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001615public:
1616 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
1617 return new(1) BranchInst(IfTrue, InsertBefore);
1618 }
1619 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1620 Instruction *InsertBefore = 0) {
1621 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
1622 }
1623 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
1624 return new(1) BranchInst(IfTrue, InsertAtEnd);
1625 }
1626 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1627 BasicBlock *InsertAtEnd) {
1628 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
1629 }
Chris Lattner454928e2005-01-29 00:31:36 +00001630
1631 /// Transparently provide more efficient getOperand methods.
1632 Value *getOperand(unsigned i) const {
1633 assert(i < getNumOperands() && "getOperand() out of range!");
1634 return Ops[i];
1635 }
1636 void setOperand(unsigned i, Value *Val) {
1637 assert(i < getNumOperands() && "setOperand() out of range!");
1638 Ops[i] = Val;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001639 }
1640
Chris Lattnerf319e832004-10-15 23:52:05 +00001641 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001642
Devang Patel4d4a5e02008-02-23 01:11:02 +00001643 bool isUnconditional() const { return getNumOperands() == 1; }
1644 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001645
Devang Patel4d4a5e02008-02-23 01:11:02 +00001646 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001647 assert(isConditional() && "Cannot get condition of an uncond branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001648 return getOperand(2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001649 }
1650
1651 void setCondition(Value *V) {
1652 assert(isConditional() && "Cannot set condition of unconditional branch!");
1653 setOperand(2, V);
1654 }
1655
1656 // setUnconditionalDest - Change the current branch to an unconditional branch
1657 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +00001658 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001659 void setUnconditionalDest(BasicBlock *Dest) {
Chris Lattner454928e2005-01-29 00:31:36 +00001660 if (isConditional()) { // Convert this to an uncond branch.
1661 NumOperands = 1;
1662 Ops[1].set(0);
1663 Ops[2].set(0);
1664 }
1665 setOperand(0, reinterpret_cast<Value*>(Dest));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001666 }
1667
Chris Lattner454928e2005-01-29 00:31:36 +00001668 unsigned getNumSuccessors() const { return 1+isConditional(); }
1669
1670 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001671 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Dan Gohmanb96039e2007-05-11 20:59:29 +00001672 return cast<BasicBlock>(getOperand(i));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001673 }
1674
Chris Lattner454928e2005-01-29 00:31:36 +00001675 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001676 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001677 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001678 }
1679
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001680 // Methods for support type inquiry through isa, cast, and dyn_cast:
1681 static inline bool classof(const BranchInst *) { return true; }
1682 static inline bool classof(const Instruction *I) {
1683 return (I->getOpcode() == Instruction::Br);
1684 }
1685 static inline bool classof(const Value *V) {
1686 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1687 }
Chris Lattner454928e2005-01-29 00:31:36 +00001688private:
1689 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1690 virtual unsigned getNumSuccessorsV() const;
1691 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001692};
1693
1694//===----------------------------------------------------------------------===//
1695// SwitchInst Class
1696//===----------------------------------------------------------------------===//
1697
1698//===---------------------------------------------------------------------------
1699/// SwitchInst - Multiway switch
1700///
1701class SwitchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001702 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001703 // Operand[0] = Value to switch on
1704 // Operand[1] = Default basic block destination
1705 // Operand[2n ] = Value to match
1706 // Operand[2n+1] = BasicBlock to go to on match
1707 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00001708 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1709 void resizeOperands(unsigned No);
Chris Lattner454928e2005-01-29 00:31:36 +00001710 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1711 /// switch on and a default destination. The number of additional cases can
1712 /// be specified here to make memory allocation more efficient. This
1713 /// constructor can also autoinsert before another instruction.
1714 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00001715 Instruction *InsertBefore = 0);
1716
Chris Lattner454928e2005-01-29 00:31:36 +00001717 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1718 /// switch on and a default destination. The number of additional cases can
1719 /// be specified here to make memory allocation more efficient. This
1720 /// constructor also autoinserts at the end of the specified BasicBlock.
1721 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00001722 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001723public:
1724 static SwitchInst *Create(Value *Value, BasicBlock *Default, unsigned NumCases,
1725 Instruction *InsertBefore = 0) {
Evan Chengd69bb1a2008-05-05 17:41:03 +00001726 return new(NumCases/*FIXME*/)
1727 SwitchInst(Value, Default, NumCases, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001728 }
1729 static SwitchInst *Create(Value *Value, BasicBlock *Default, unsigned NumCases,
1730 BasicBlock *InsertAtEnd) {
Evan Chengd69bb1a2008-05-05 17:41:03 +00001731 return new(NumCases/*FIXME*/)
1732 SwitchInst(Value, Default, NumCases, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001733 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001734 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00001735
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001736 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00001737 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00001738 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00001739
Devang Patel4d4a5e02008-02-23 01:11:02 +00001740 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001741 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001742 }
1743
1744 /// getNumCases - return the number of 'cases' in this switch instruction.
1745 /// Note that case #0 is always the default case.
1746 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001747 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001748 }
1749
1750 /// getCaseValue - Return the specified case value. Note that case #0, the
1751 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001752 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001753 assert(i && i < getNumCases() && "Illegal case value to get!");
1754 return getSuccessorValue(i);
1755 }
1756
1757 /// getCaseValue - Return the specified case value. Note that case #0, the
1758 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001759 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001760 assert(i && i < getNumCases() && "Illegal case value to get!");
1761 return getSuccessorValue(i);
1762 }
1763
1764 /// findCaseValue - Search all of the case values for the specified constant.
1765 /// If it is explicitly handled, return the case number of it, otherwise
1766 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001767 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001768 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1769 if (getCaseValue(i) == C)
1770 return i;
1771 return 0;
1772 }
1773
Nick Lewycky011f1842006-09-18 19:03:59 +00001774 /// findCaseDest - Finds the unique case value for a given successor. Returns
1775 /// null if the successor is not found, not unique, or is the default case.
1776 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00001777 if (BB == getDefaultDest()) return NULL;
1778
Nick Lewycky011f1842006-09-18 19:03:59 +00001779 ConstantInt *CI = NULL;
1780 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
1781 if (getSuccessor(i) == BB) {
1782 if (CI) return NULL; // Multiple cases lead to BB.
1783 else CI = getCaseValue(i);
1784 }
1785 }
1786 return CI;
1787 }
1788
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001789 /// addCase - Add an entry to the switch instruction...
1790 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00001791 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001792
1793 /// removeCase - This method removes the specified successor from the switch
1794 /// instruction. Note that this cannot be used to remove the default
1795 /// destination (successor #0).
1796 ///
1797 void removeCase(unsigned idx);
1798
Chris Lattner454928e2005-01-29 00:31:36 +00001799 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001800
Chris Lattner454928e2005-01-29 00:31:36 +00001801 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1802 BasicBlock *getSuccessor(unsigned idx) const {
1803 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1804 return cast<BasicBlock>(getOperand(idx*2+1));
1805 }
1806 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001807 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001808 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001809 }
1810
1811 // getSuccessorValue - Return the value associated with the specified
1812 // successor.
Devang Patel4d4a5e02008-02-23 01:11:02 +00001813 ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001814 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00001815 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001816 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001817
1818 // Methods for support type inquiry through isa, cast, and dyn_cast:
1819 static inline bool classof(const SwitchInst *) { return true; }
1820 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00001821 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001822 }
1823 static inline bool classof(const Value *V) {
1824 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1825 }
Chris Lattner454928e2005-01-29 00:31:36 +00001826private:
1827 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1828 virtual unsigned getNumSuccessorsV() const;
1829 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001830};
1831
1832//===----------------------------------------------------------------------===//
1833// InvokeInst Class
1834//===----------------------------------------------------------------------===//
1835
1836//===---------------------------------------------------------------------------
Chris Lattner3340ffe2005-05-06 20:26:26 +00001837
1838/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
1839/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001840///
1841class InvokeInst : public TerminatorInst {
Chris Lattner58d74912008-03-12 17:45:29 +00001842 PAListPtr ParamAttrs;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001843 InvokeInst(const InvokeInst &BI);
1844 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerd2dd1502007-02-13 01:04:01 +00001845 Value* const *Args, unsigned NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00001846
1847 template<typename InputIterator>
1848 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1849 InputIterator ArgBegin, InputIterator ArgEnd,
1850 const std::string &Name,
1851 // This argument ensures that we have an iterator we can
1852 // do arithmetic on in constant time
1853 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00001854 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greenef1355a52007-08-27 19:04:21 +00001855
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00001856 // This requires that the iterator points to contiguous memory.
1857 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00001858 setName(Name);
1859 }
1860
David Greenef1355a52007-08-27 19:04:21 +00001861 /// Construct an InvokeInst given a range of arguments.
1862 /// InputIterator must be a random-access iterator pointing to
1863 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
1864 /// made for random-accessness but not for contiguous storage as
1865 /// that would incur runtime overhead.
1866 ///
1867 /// @brief Construct an InvokeInst from a range of arguments
1868 template<typename InputIterator>
1869 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1870 InputIterator ArgBegin, InputIterator ArgEnd,
1871 const std::string &Name = "", Instruction *InsertBefore = 0)
1872 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
1873 ->getElementType())->getReturnType(),
1874 Instruction::Invoke, 0, 0, InsertBefore) {
1875 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1876 typename std::iterator_traits<InputIterator>::iterator_category());
1877 }
1878
1879 /// Construct an InvokeInst given a range of arguments.
1880 /// InputIterator must be a random-access iterator pointing to
1881 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
1882 /// made for random-accessness but not for contiguous storage as
1883 /// that would incur runtime overhead.
1884 ///
1885 /// @brief Construct an InvokeInst from a range of arguments
1886 template<typename InputIterator>
1887 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1888 InputIterator ArgBegin, InputIterator ArgEnd,
1889 const std::string &Name, BasicBlock *InsertAtEnd)
1890 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
1891 ->getElementType())->getReturnType(),
1892 Instruction::Invoke, 0, 0, InsertAtEnd) {
1893 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1894 typename std::iterator_traits<InputIterator>::iterator_category());
1895 }
Gabor Greif051a9502008-04-06 20:25:17 +00001896public:
1897 template<typename InputIterator>
Evan Chengd69bb1a2008-05-05 17:41:03 +00001898 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
1899 BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00001900 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001901 const std::string &Name = "",
1902 Instruction *InsertBefore = 0) {
1903 return new(ArgEnd - ArgBegin + 3)
1904 InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001905 }
1906 template<typename InputIterator>
Evan Chengd69bb1a2008-05-05 17:41:03 +00001907 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
1908 BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00001909 InputIterator ArgBegin, InputIterator ArgEnd,
1910 const std::string &Name, BasicBlock *InsertAtEnd) {
Evan Chengd69bb1a2008-05-05 17:41:03 +00001911 return new(ArgEnd - ArgBegin + 3)
1912 InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001913 }
David Greenef1355a52007-08-27 19:04:21 +00001914
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001915 ~InvokeInst();
1916
Chris Lattnerf319e832004-10-15 23:52:05 +00001917 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001918
Chris Lattner3340ffe2005-05-06 20:26:26 +00001919 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1920 /// function call.
1921 unsigned getCallingConv() const { return SubclassData; }
1922 void setCallingConv(unsigned CC) {
1923 SubclassData = CC;
1924 }
1925
Chris Lattner041221c2008-03-13 04:33:03 +00001926 /// getParamAttrs - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00001927 ///
1928 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00001929
Chris Lattner041221c2008-03-13 04:33:03 +00001930 /// setParamAttrs - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00001931 ///
1932 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00001933
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001934 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001935 bool paramHasAttr(unsigned i, ParameterAttributes attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001936
Dale Johannesen08e78b12008-02-22 17:49:45 +00001937 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001938 unsigned getParamAlignment(unsigned i) const {
1939 return ParamAttrs.getParamAlignment(i);
1940 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001941
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001942 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001943 bool doesNotAccessMemory() const {
1944 return paramHasAttr(0, ParamAttr::ReadNone);
1945 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001946
1947 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001948 bool onlyReadsMemory() const {
1949 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
1950 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001951
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001952 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001953 bool doesNotReturn() const {
1954 return paramHasAttr(0, ParamAttr::NoReturn);
1955 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001956
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001957 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001958 bool doesNotThrow() const {
1959 return paramHasAttr(0, ParamAttr::NoUnwind);
1960 }
Duncan Sandsf0c33542007-12-19 21:13:37 +00001961 void setDoesNotThrow(bool doesNotThrow = true);
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001962
Devang Patel41e23972008-03-03 21:46:28 +00001963 /// @brief Determine if the call returns a structure through first
1964 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001965 bool hasStructRetAttr() const {
1966 // Be friendly and also check the callee.
1967 return paramHasAttr(1, ParamAttr::StructRet);
1968 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00001969
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001970 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00001971 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001972 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001973 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001974 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001975 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001976
1977 // getCalledValue - Get a pointer to a function that is invoked by this inst.
Devang Patel4d4a5e02008-02-23 01:11:02 +00001978 Value *getCalledValue() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001979
1980 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00001981 BasicBlock *getNormalDest() const {
1982 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001983 }
Chris Lattner454928e2005-01-29 00:31:36 +00001984 BasicBlock *getUnwindDest() const {
1985 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001986 }
Chris Lattner454928e2005-01-29 00:31:36 +00001987 void setNormalDest(BasicBlock *B) {
1988 setOperand(1, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001989 }
1990
Chris Lattner454928e2005-01-29 00:31:36 +00001991 void setUnwindDest(BasicBlock *B) {
1992 setOperand(2, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001993 }
1994
Devang Patel4d4a5e02008-02-23 01:11:02 +00001995 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001996 assert(i < 2 && "Successor # out of range for invoke!");
1997 return i == 0 ? getNormalDest() : getUnwindDest();
1998 }
1999
Chris Lattner454928e2005-01-29 00:31:36 +00002000 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002001 assert(idx < 2 && "Successor # out of range for invoke!");
Chris Lattner454928e2005-01-29 00:31:36 +00002002 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002003 }
2004
Chris Lattner454928e2005-01-29 00:31:36 +00002005 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002006
2007 // Methods for support type inquiry through isa, cast, and dyn_cast:
2008 static inline bool classof(const InvokeInst *) { return true; }
2009 static inline bool classof(const Instruction *I) {
2010 return (I->getOpcode() == Instruction::Invoke);
2011 }
2012 static inline bool classof(const Value *V) {
2013 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2014 }
Chris Lattner454928e2005-01-29 00:31:36 +00002015private:
2016 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2017 virtual unsigned getNumSuccessorsV() const;
2018 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002019};
2020
2021
2022//===----------------------------------------------------------------------===//
2023// UnwindInst Class
2024//===----------------------------------------------------------------------===//
2025
2026//===---------------------------------------------------------------------------
2027/// UnwindInst - Immediately exit the current function, unwinding the stack
2028/// until an invoke instruction is found.
2029///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002030class UnwindInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002031 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002032public:
Gabor Greif051a9502008-04-06 20:25:17 +00002033 // allocate space for exactly zero operands
2034 void *operator new(size_t s) {
2035 return User::operator new(s, 0);
2036 }
Chris Lattner910c80a2007-02-24 00:55:48 +00002037 explicit UnwindInst(Instruction *InsertBefore = 0);
2038 explicit UnwindInst(BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002039
Chris Lattnerf319e832004-10-15 23:52:05 +00002040 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002041
Chris Lattner454928e2005-01-29 00:31:36 +00002042 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002043
2044 // Methods for support type inquiry through isa, cast, and dyn_cast:
2045 static inline bool classof(const UnwindInst *) { return true; }
2046 static inline bool classof(const Instruction *I) {
2047 return I->getOpcode() == Instruction::Unwind;
2048 }
2049 static inline bool classof(const Value *V) {
2050 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2051 }
Chris Lattner454928e2005-01-29 00:31:36 +00002052private:
2053 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2054 virtual unsigned getNumSuccessorsV() const;
2055 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002056};
2057
Chris Lattner076b3f12004-10-16 18:05:54 +00002058//===----------------------------------------------------------------------===//
2059// UnreachableInst Class
2060//===----------------------------------------------------------------------===//
2061
2062//===---------------------------------------------------------------------------
2063/// UnreachableInst - This function has undefined behavior. In particular, the
2064/// presence of this instruction indicates some higher level knowledge that the
2065/// end of the block cannot be reached.
2066///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002067class UnreachableInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002068 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002069public:
Gabor Greif051a9502008-04-06 20:25:17 +00002070 // allocate space for exactly zero operands
2071 void *operator new(size_t s) {
2072 return User::operator new(s, 0);
2073 }
Chris Lattner910c80a2007-02-24 00:55:48 +00002074 explicit UnreachableInst(Instruction *InsertBefore = 0);
2075 explicit UnreachableInst(BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00002076
2077 virtual UnreachableInst *clone() const;
2078
Chris Lattner454928e2005-01-29 00:31:36 +00002079 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00002080
2081 // Methods for support type inquiry through isa, cast, and dyn_cast:
2082 static inline bool classof(const UnreachableInst *) { return true; }
2083 static inline bool classof(const Instruction *I) {
2084 return I->getOpcode() == Instruction::Unreachable;
2085 }
2086 static inline bool classof(const Value *V) {
2087 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2088 }
Chris Lattner454928e2005-01-29 00:31:36 +00002089private:
2090 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2091 virtual unsigned getNumSuccessorsV() const;
2092 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00002093};
2094
Reid Spencer3da59db2006-11-27 01:05:10 +00002095//===----------------------------------------------------------------------===//
2096// TruncInst Class
2097//===----------------------------------------------------------------------===//
2098
2099/// @brief This class represents a truncation of integer types.
2100class TruncInst : public CastInst {
2101 /// Private copy constructor
2102 TruncInst(const TruncInst &CI)
2103 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2104 }
2105public:
2106 /// @brief Constructor with insert-before-instruction semantics
2107 TruncInst(
2108 Value *S, ///< The value to be truncated
2109 const Type *Ty, ///< The (smaller) type to truncate to
2110 const std::string &Name = "", ///< A name for the new instruction
2111 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2112 );
2113
2114 /// @brief Constructor with insert-at-end-of-block semantics
2115 TruncInst(
2116 Value *S, ///< The value to be truncated
2117 const Type *Ty, ///< The (smaller) type to truncate to
2118 const std::string &Name, ///< A name for the new instruction
2119 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2120 );
2121
2122 /// @brief Clone an identical TruncInst
2123 virtual CastInst *clone() const;
2124
2125 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2126 static inline bool classof(const TruncInst *) { return true; }
2127 static inline bool classof(const Instruction *I) {
2128 return I->getOpcode() == Trunc;
2129 }
2130 static inline bool classof(const Value *V) {
2131 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2132 }
2133};
2134
2135//===----------------------------------------------------------------------===//
2136// ZExtInst Class
2137//===----------------------------------------------------------------------===//
2138
2139/// @brief This class represents zero extension of integer types.
2140class ZExtInst : public CastInst {
2141 /// @brief Private copy constructor
2142 ZExtInst(const ZExtInst &CI)
2143 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2144 }
2145public:
2146 /// @brief Constructor with insert-before-instruction semantics
2147 ZExtInst(
2148 Value *S, ///< The value to be zero extended
2149 const Type *Ty, ///< The type to zero extend to
2150 const std::string &Name = "", ///< A name for the new instruction
2151 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2152 );
2153
2154 /// @brief Constructor with insert-at-end semantics.
2155 ZExtInst(
2156 Value *S, ///< The value to be zero extended
2157 const Type *Ty, ///< The type to zero extend to
2158 const std::string &Name, ///< A name for the new instruction
2159 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2160 );
2161
2162 /// @brief Clone an identical ZExtInst
2163 virtual CastInst *clone() const;
2164
2165 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2166 static inline bool classof(const ZExtInst *) { return true; }
2167 static inline bool classof(const Instruction *I) {
2168 return I->getOpcode() == ZExt;
2169 }
2170 static inline bool classof(const Value *V) {
2171 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2172 }
2173};
2174
2175//===----------------------------------------------------------------------===//
2176// SExtInst Class
2177//===----------------------------------------------------------------------===//
2178
2179/// @brief This class represents a sign extension of integer types.
2180class SExtInst : public CastInst {
2181 /// @brief Private copy constructor
2182 SExtInst(const SExtInst &CI)
2183 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2184 }
2185public:
2186 /// @brief Constructor with insert-before-instruction semantics
2187 SExtInst(
2188 Value *S, ///< The value to be sign extended
2189 const Type *Ty, ///< The type to sign extend to
2190 const std::string &Name = "", ///< A name for the new instruction
2191 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2192 );
2193
2194 /// @brief Constructor with insert-at-end-of-block semantics
2195 SExtInst(
2196 Value *S, ///< The value to be sign extended
2197 const Type *Ty, ///< The type to sign extend to
2198 const std::string &Name, ///< A name for the new instruction
2199 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2200 );
2201
2202 /// @brief Clone an identical SExtInst
2203 virtual CastInst *clone() const;
2204
2205 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2206 static inline bool classof(const SExtInst *) { return true; }
2207 static inline bool classof(const Instruction *I) {
2208 return I->getOpcode() == SExt;
2209 }
2210 static inline bool classof(const Value *V) {
2211 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2212 }
2213};
2214
2215//===----------------------------------------------------------------------===//
2216// FPTruncInst Class
2217//===----------------------------------------------------------------------===//
2218
2219/// @brief This class represents a truncation of floating point types.
2220class FPTruncInst : public CastInst {
2221 FPTruncInst(const FPTruncInst &CI)
2222 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2223 }
2224public:
2225 /// @brief Constructor with insert-before-instruction semantics
2226 FPTruncInst(
2227 Value *S, ///< The value to be truncated
2228 const Type *Ty, ///< The type to truncate to
2229 const std::string &Name = "", ///< A name for the new instruction
2230 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2231 );
2232
2233 /// @brief Constructor with insert-before-instruction semantics
2234 FPTruncInst(
2235 Value *S, ///< The value to be truncated
2236 const Type *Ty, ///< The type to truncate to
2237 const std::string &Name, ///< A name for the new instruction
2238 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2239 );
2240
2241 /// @brief Clone an identical FPTruncInst
2242 virtual CastInst *clone() const;
2243
2244 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2245 static inline bool classof(const FPTruncInst *) { return true; }
2246 static inline bool classof(const Instruction *I) {
2247 return I->getOpcode() == FPTrunc;
2248 }
2249 static inline bool classof(const Value *V) {
2250 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2251 }
2252};
2253
2254//===----------------------------------------------------------------------===//
2255// FPExtInst Class
2256//===----------------------------------------------------------------------===//
2257
2258/// @brief This class represents an extension of floating point types.
2259class FPExtInst : public CastInst {
2260 FPExtInst(const FPExtInst &CI)
2261 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2262 }
2263public:
2264 /// @brief Constructor with insert-before-instruction semantics
2265 FPExtInst(
2266 Value *S, ///< The value to be extended
2267 const Type *Ty, ///< The type to extend to
2268 const std::string &Name = "", ///< A name for the new instruction
2269 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2270 );
2271
2272 /// @brief Constructor with insert-at-end-of-block semantics
2273 FPExtInst(
2274 Value *S, ///< The value to be extended
2275 const Type *Ty, ///< The type to extend to
2276 const std::string &Name, ///< A name for the new instruction
2277 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2278 );
2279
2280 /// @brief Clone an identical FPExtInst
2281 virtual CastInst *clone() const;
2282
2283 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2284 static inline bool classof(const FPExtInst *) { return true; }
2285 static inline bool classof(const Instruction *I) {
2286 return I->getOpcode() == FPExt;
2287 }
2288 static inline bool classof(const Value *V) {
2289 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2290 }
2291};
2292
2293//===----------------------------------------------------------------------===//
2294// UIToFPInst Class
2295//===----------------------------------------------------------------------===//
2296
2297/// @brief This class represents a cast unsigned integer to floating point.
2298class UIToFPInst : public CastInst {
2299 UIToFPInst(const UIToFPInst &CI)
2300 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2301 }
2302public:
2303 /// @brief Constructor with insert-before-instruction semantics
2304 UIToFPInst(
2305 Value *S, ///< The value to be converted
2306 const Type *Ty, ///< The type to convert to
2307 const std::string &Name = "", ///< A name for the new instruction
2308 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2309 );
2310
2311 /// @brief Constructor with insert-at-end-of-block semantics
2312 UIToFPInst(
2313 Value *S, ///< The value to be converted
2314 const Type *Ty, ///< The type to convert to
2315 const std::string &Name, ///< A name for the new instruction
2316 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2317 );
2318
2319 /// @brief Clone an identical UIToFPInst
2320 virtual CastInst *clone() const;
2321
2322 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2323 static inline bool classof(const UIToFPInst *) { return true; }
2324 static inline bool classof(const Instruction *I) {
2325 return I->getOpcode() == UIToFP;
2326 }
2327 static inline bool classof(const Value *V) {
2328 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2329 }
2330};
2331
2332//===----------------------------------------------------------------------===//
2333// SIToFPInst Class
2334//===----------------------------------------------------------------------===//
2335
2336/// @brief This class represents a cast from signed integer to floating point.
2337class SIToFPInst : public CastInst {
2338 SIToFPInst(const SIToFPInst &CI)
2339 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2340 }
2341public:
2342 /// @brief Constructor with insert-before-instruction semantics
2343 SIToFPInst(
2344 Value *S, ///< The value to be converted
2345 const Type *Ty, ///< The type to convert to
2346 const std::string &Name = "", ///< A name for the new instruction
2347 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2348 );
2349
2350 /// @brief Constructor with insert-at-end-of-block semantics
2351 SIToFPInst(
2352 Value *S, ///< The value to be converted
2353 const Type *Ty, ///< The type to convert to
2354 const std::string &Name, ///< A name for the new instruction
2355 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2356 );
2357
2358 /// @brief Clone an identical SIToFPInst
2359 virtual CastInst *clone() const;
2360
2361 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2362 static inline bool classof(const SIToFPInst *) { return true; }
2363 static inline bool classof(const Instruction *I) {
2364 return I->getOpcode() == SIToFP;
2365 }
2366 static inline bool classof(const Value *V) {
2367 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2368 }
2369};
2370
2371//===----------------------------------------------------------------------===//
2372// FPToUIInst Class
2373//===----------------------------------------------------------------------===//
2374
2375/// @brief This class represents a cast from floating point to unsigned integer
2376class FPToUIInst : public CastInst {
2377 FPToUIInst(const FPToUIInst &CI)
2378 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2379 }
2380public:
2381 /// @brief Constructor with insert-before-instruction semantics
2382 FPToUIInst(
2383 Value *S, ///< The value to be converted
2384 const Type *Ty, ///< The type to convert to
2385 const std::string &Name = "", ///< A name for the new instruction
2386 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2387 );
2388
2389 /// @brief Constructor with insert-at-end-of-block semantics
2390 FPToUIInst(
2391 Value *S, ///< The value to be converted
2392 const Type *Ty, ///< The type to convert to
2393 const std::string &Name, ///< A name for the new instruction
2394 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2395 );
2396
2397 /// @brief Clone an identical FPToUIInst
2398 virtual CastInst *clone() const;
2399
2400 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2401 static inline bool classof(const FPToUIInst *) { return true; }
2402 static inline bool classof(const Instruction *I) {
2403 return I->getOpcode() == FPToUI;
2404 }
2405 static inline bool classof(const Value *V) {
2406 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2407 }
2408};
2409
2410//===----------------------------------------------------------------------===//
2411// FPToSIInst Class
2412//===----------------------------------------------------------------------===//
2413
2414/// @brief This class represents a cast from floating point to signed integer.
2415class FPToSIInst : public CastInst {
2416 FPToSIInst(const FPToSIInst &CI)
2417 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2418 }
2419public:
2420 /// @brief Constructor with insert-before-instruction semantics
2421 FPToSIInst(
2422 Value *S, ///< The value to be converted
2423 const Type *Ty, ///< The type to convert to
2424 const std::string &Name = "", ///< A name for the new instruction
2425 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2426 );
2427
2428 /// @brief Constructor with insert-at-end-of-block semantics
2429 FPToSIInst(
2430 Value *S, ///< The value to be converted
2431 const Type *Ty, ///< The type to convert to
2432 const std::string &Name, ///< A name for the new instruction
2433 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2434 );
2435
2436 /// @brief Clone an identical FPToSIInst
2437 virtual CastInst *clone() const;
2438
2439 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2440 static inline bool classof(const FPToSIInst *) { return true; }
2441 static inline bool classof(const Instruction *I) {
2442 return I->getOpcode() == FPToSI;
2443 }
2444 static inline bool classof(const Value *V) {
2445 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2446 }
2447};
2448
2449//===----------------------------------------------------------------------===//
2450// IntToPtrInst Class
2451//===----------------------------------------------------------------------===//
2452
2453/// @brief This class represents a cast from an integer to a pointer.
2454class IntToPtrInst : public CastInst {
2455 IntToPtrInst(const IntToPtrInst &CI)
2456 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2457 }
2458public:
2459 /// @brief Constructor with insert-before-instruction semantics
2460 IntToPtrInst(
2461 Value *S, ///< The value to be converted
2462 const Type *Ty, ///< The type to convert to
2463 const std::string &Name = "", ///< A name for the new instruction
2464 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2465 );
2466
2467 /// @brief Constructor with insert-at-end-of-block semantics
2468 IntToPtrInst(
2469 Value *S, ///< The value to be converted
2470 const Type *Ty, ///< The type to convert to
2471 const std::string &Name, ///< A name for the new instruction
2472 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2473 );
2474
2475 /// @brief Clone an identical IntToPtrInst
2476 virtual CastInst *clone() const;
2477
2478 // Methods for support type inquiry through isa, cast, and dyn_cast:
2479 static inline bool classof(const IntToPtrInst *) { return true; }
2480 static inline bool classof(const Instruction *I) {
2481 return I->getOpcode() == IntToPtr;
2482 }
2483 static inline bool classof(const Value *V) {
2484 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2485 }
2486};
2487
2488//===----------------------------------------------------------------------===//
2489// PtrToIntInst Class
2490//===----------------------------------------------------------------------===//
2491
2492/// @brief This class represents a cast from a pointer to an integer
2493class PtrToIntInst : public CastInst {
2494 PtrToIntInst(const PtrToIntInst &CI)
2495 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
2496 }
2497public:
2498 /// @brief Constructor with insert-before-instruction semantics
2499 PtrToIntInst(
2500 Value *S, ///< The value to be converted
2501 const Type *Ty, ///< The type to convert to
2502 const std::string &Name = "", ///< A name for the new instruction
2503 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2504 );
2505
2506 /// @brief Constructor with insert-at-end-of-block semantics
2507 PtrToIntInst(
2508 Value *S, ///< The value to be converted
2509 const Type *Ty, ///< The type to convert to
2510 const std::string &Name, ///< A name for the new instruction
2511 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2512 );
2513
2514 /// @brief Clone an identical PtrToIntInst
2515 virtual CastInst *clone() const;
2516
2517 // Methods for support type inquiry through isa, cast, and dyn_cast:
2518 static inline bool classof(const PtrToIntInst *) { return true; }
2519 static inline bool classof(const Instruction *I) {
2520 return I->getOpcode() == PtrToInt;
2521 }
2522 static inline bool classof(const Value *V) {
2523 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2524 }
2525};
2526
2527//===----------------------------------------------------------------------===//
2528// BitCastInst Class
2529//===----------------------------------------------------------------------===//
2530
2531/// @brief This class represents a no-op cast from one type to another.
2532class BitCastInst : public CastInst {
2533 BitCastInst(const BitCastInst &CI)
2534 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
2535 }
2536public:
2537 /// @brief Constructor with insert-before-instruction semantics
2538 BitCastInst(
2539 Value *S, ///< The value to be casted
2540 const Type *Ty, ///< The type to casted to
2541 const std::string &Name = "", ///< A name for the new instruction
2542 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2543 );
2544
2545 /// @brief Constructor with insert-at-end-of-block semantics
2546 BitCastInst(
2547 Value *S, ///< The value to be casted
2548 const Type *Ty, ///< The type to casted to
2549 const std::string &Name, ///< A name for the new instruction
2550 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2551 );
2552
2553 /// @brief Clone an identical BitCastInst
2554 virtual CastInst *clone() const;
2555
2556 // Methods for support type inquiry through isa, cast, and dyn_cast:
2557 static inline bool classof(const BitCastInst *) { return true; }
2558 static inline bool classof(const Instruction *I) {
2559 return I->getOpcode() == BitCast;
2560 }
2561 static inline bool classof(const Value *V) {
2562 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2563 }
2564};
2565
Devang Patel40a04212008-02-19 22:15:16 +00002566//===----------------------------------------------------------------------===//
2567// GetResultInst Class
2568//===----------------------------------------------------------------------===//
2569
2570/// GetResultInst - This instruction extracts individual result value from
2571/// aggregate value, where aggregate value is returned by CallInst.
2572///
Gabor Greif051a9502008-04-06 20:25:17 +00002573class GetResultInst : public /*FIXME: Unary*/Instruction {
2574 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Devang Patel23755d82008-02-20 19:10:47 +00002575 Use Aggr;
2576 unsigned Idx;
Devang Patel40a04212008-02-19 22:15:16 +00002577 GetResultInst(const GetResultInst &GRI) :
Devang Patel23755d82008-02-20 19:10:47 +00002578 Instruction(GRI.getType(), Instruction::GetResult, &Aggr, 1) {
2579 Aggr.init(GRI.Aggr, this);
2580 Idx = GRI.Idx;
Devang Patel40a04212008-02-19 22:15:16 +00002581 }
2582
2583public:
Gabor Greif051a9502008-04-06 20:25:17 +00002584 // allocate space for exactly one operand
2585 void *operator new(size_t s) {
2586 return User::operator new(s, 1);
2587 }
Devang Patel23755d82008-02-20 19:10:47 +00002588 explicit GetResultInst(Value *Aggr, unsigned index,
Devang Patel40a04212008-02-19 22:15:16 +00002589 const std::string &Name = "",
2590 Instruction *InsertBefore = 0);
2591
2592 /// isValidOperands - Return true if an getresult instruction can be
2593 /// formed with the specified operands.
Devang Patel23755d82008-02-20 19:10:47 +00002594 static bool isValidOperands(const Value *Aggr, unsigned index);
Devang Patel40a04212008-02-19 22:15:16 +00002595
2596 virtual GetResultInst *clone() const;
2597
Devang Patel4d4a5e02008-02-23 01:11:02 +00002598 Value *getAggregateValue() {
Devang Patel40a04212008-02-19 22:15:16 +00002599 return getOperand(0);
2600 }
Devang Patel2d2ae342008-02-20 18:36:16 +00002601
Devang Patel4d4a5e02008-02-23 01:11:02 +00002602 const Value *getAggregateValue() const {
Devang Patel2d2ae342008-02-20 18:36:16 +00002603 return getOperand(0);
2604 }
2605
Devang Patel4d4a5e02008-02-23 01:11:02 +00002606 unsigned getIndex() const {
Devang Patel23755d82008-02-20 19:10:47 +00002607 return Idx;
Devang Patel40a04212008-02-19 22:15:16 +00002608 }
2609
Devang Patel23755d82008-02-20 19:10:47 +00002610 unsigned getNumOperands() const { return 1; }
Devang Patel40a04212008-02-19 22:15:16 +00002611
2612 // Methods for support type inquiry through isa, cast, and dyn_cast:
2613 static inline bool classof(const GetResultInst *) { return true; }
2614 static inline bool classof(const Instruction *I) {
2615 return (I->getOpcode() == Instruction::GetResult);
2616 }
2617 static inline bool classof(const Value *V) {
2618 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2619 }
2620};
2621
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002622} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00002623
2624#endif