blob: 137c5e22310f98fe207c8b0419cfb5e5440ffeeb [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) {
400 typename std::iterator_traits<InputIterator>::difference_type NumIdx =
401 std::distance(IdxBegin, IdxEnd);
402
403 if (NumIdx > 0) {
404 // This requires that the itoerator points to contiguous memory.
405 init(Ptr, &*IdxBegin, NumIdx);
406 }
407 else {
408 init(Ptr, 0, NumIdx);
409 }
410
411 setName(Name);
412 }
413
414 /// getIndexedType - Returns the type of the element that would be loaded with
415 /// a load instruction with the specified parameters.
416 ///
417 /// A null type is returned if the indices are invalid for the specified
418 /// pointer type.
419 ///
420 static const Type *getIndexedType(const Type *Ptr,
421 Value* const *Idx, unsigned NumIdx,
422 bool AllowStructLeaf = false);
423
424 template<typename InputIterator>
425 static const Type *getIndexedType(const Type *Ptr,
426 InputIterator IdxBegin,
427 InputIterator IdxEnd,
428 bool AllowStructLeaf,
429 // This argument ensures that we
430 // have an iterator we can do
431 // arithmetic on in constant time
432 std::random_access_iterator_tag) {
433 typename std::iterator_traits<InputIterator>::difference_type NumIdx =
434 std::distance(IdxBegin, IdxEnd);
435
436 if (NumIdx > 0) {
437 // This requires that the iterator points to contiguous memory.
438 return(getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx,
439 AllowStructLeaf));
440 }
441 else {
442 return(getIndexedType(Ptr, (Value *const*)0, NumIdx, AllowStructLeaf));
443 }
444 }
445
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000446 /// Constructors - Create a getelementptr instruction with a base pointer an
447 /// list of indices. The first ctor can optionally insert before an existing
448 /// instruction, the second appends the new instruction to the specified
449 /// BasicBlock.
David Greeneb8f74792007-09-04 15:46:09 +0000450 template<typename InputIterator>
451 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
452 InputIterator IdxEnd,
453 const std::string &Name = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000454 Instruction *InsertBefore = 0)
David Greeneb8f74792007-09-04 15:46:09 +0000455 : Instruction(PointerType::get(
456 checkType(getIndexedType(Ptr->getType(),
Christopher Lambfe63fb92007-12-11 08:59:05 +0000457 IdxBegin, IdxEnd, true)),
458 cast<PointerType>(Ptr->getType())->getAddressSpace()),
David Greeneb8f74792007-09-04 15:46:09 +0000459 GetElementPtr, 0, 0, InsertBefore) {
460 init(Ptr, IdxBegin, IdxEnd, Name,
461 typename std::iterator_traits<InputIterator>::iterator_category());
462 }
463 template<typename InputIterator>
464 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
465 const std::string &Name, BasicBlock *InsertAtEnd)
466 : Instruction(PointerType::get(
467 checkType(getIndexedType(Ptr->getType(),
Christopher Lambfe63fb92007-12-11 08:59:05 +0000468 IdxBegin, IdxEnd, true)),
469 cast<PointerType>(Ptr->getType())->getAddressSpace()),
David Greeneb8f74792007-09-04 15:46:09 +0000470 GetElementPtr, 0, 0, InsertAtEnd) {
471 init(Ptr, IdxBegin, IdxEnd, Name,
472 typename std::iterator_traits<InputIterator>::iterator_category());
473 }
474
Chris Lattner38bacf22005-05-03 05:43:30 +0000475 /// Constructors - These two constructors are convenience methods because one
476 /// and two index getelementptr instructions are so common.
477 GetElementPtrInst(Value *Ptr, Value *Idx,
Gabor Greif051a9502008-04-06 20:25:17 +0000478 const std::string &Name = "", Instruction *InsertBefore = 0);
Chris Lattner38bacf22005-05-03 05:43:30 +0000479 GetElementPtrInst(Value *Ptr, Value *Idx,
480 const std::string &Name, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000481public:
482 template<typename InputIterator>
483 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
484 InputIterator IdxEnd,
485 const std::string &Name = "",
486 Instruction *InsertBefore = 0) {
487 return new(0/*FIXME*/) GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Name, InsertBefore);
488 }
489 template<typename InputIterator>
490 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
491 const std::string &Name, BasicBlock *InsertAtEnd) {
492 return new(0/*FIXME*/) GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Name, InsertAtEnd);
493 }
494
495 /// Constructors - These two constructors are convenience methods because one
496 /// and two index getelementptr instructions are so common.
497 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
498 const std::string &Name = "", Instruction *InsertBefore = 0) {
499 return new(2/*FIXME*/) GetElementPtrInst(Ptr, Idx, Name, InsertBefore);
500 }
501 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
502 const std::string &Name, BasicBlock *InsertAtEnd) {
503 return new(2/*FIXME*/) GetElementPtrInst(Ptr, Idx, Name, InsertAtEnd);
504 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000505 ~GetElementPtrInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000506
Chris Lattnerf319e832004-10-15 23:52:05 +0000507 virtual GetElementPtrInst *clone() const;
Misha Brukman9769ab22005-04-21 20:19:05 +0000508
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000509 // getType - Overload to return most specific pointer type...
Devang Patel4d4a5e02008-02-23 01:11:02 +0000510 const PointerType *getType() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000511 return reinterpret_cast<const PointerType*>(Instruction::getType());
512 }
513
514 /// getIndexedType - Returns the type of the element that would be loaded with
515 /// a load instruction with the specified parameters.
516 ///
Misha Brukman9769ab22005-04-21 20:19:05 +0000517 /// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000518 /// pointer type.
519 ///
David Greeneb8f74792007-09-04 15:46:09 +0000520 template<typename InputIterator>
Misha Brukman9769ab22005-04-21 20:19:05 +0000521 static const Type *getIndexedType(const Type *Ptr,
David Greeneb8f74792007-09-04 15:46:09 +0000522 InputIterator IdxBegin,
523 InputIterator IdxEnd,
524 bool AllowStructLeaf = false) {
525 return(getIndexedType(Ptr, IdxBegin, IdxEnd, AllowStructLeaf,
526 typename std::iterator_traits<InputIterator>::
527 iterator_category()));
528 }
Chris Lattner38bacf22005-05-03 05:43:30 +0000529 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
Misha Brukman9769ab22005-04-21 20:19:05 +0000530
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000531 inline op_iterator idx_begin() { return op_begin()+1; }
532 inline const_op_iterator idx_begin() const { return op_begin()+1; }
533 inline op_iterator idx_end() { return op_end(); }
534 inline const_op_iterator idx_end() const { return op_end(); }
535
536 Value *getPointerOperand() {
537 return getOperand(0);
538 }
539 const Value *getPointerOperand() const {
540 return getOperand(0);
541 }
542 static unsigned getPointerOperandIndex() {
543 return 0U; // get index for modifying correct operand
544 }
545
Devang Patel4d4a5e02008-02-23 01:11:02 +0000546 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000547 return getNumOperands() - 1;
548 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000549
Devang Patel4d4a5e02008-02-23 01:11:02 +0000550 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000551 return getNumOperands() > 1;
552 }
Chris Lattner6f771d42007-04-14 00:12:57 +0000553
554 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
555 /// zeros. If so, the result pointer and the first operand have the same
556 /// value, just potentially different types.
557 bool hasAllZeroIndices() const;
Chris Lattner6b0974c2007-04-27 20:35:56 +0000558
559 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
560 /// constant integers. If so, the result pointer and the first operand have
561 /// a constant offset between them.
562 bool hasAllConstantIndices() const;
563
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000564
565 // Methods for support type inquiry through isa, cast, and dyn_cast:
566 static inline bool classof(const GetElementPtrInst *) { return true; }
567 static inline bool classof(const Instruction *I) {
568 return (I->getOpcode() == Instruction::GetElementPtr);
569 }
570 static inline bool classof(const Value *V) {
571 return isa<Instruction>(V) && classof(cast<Instruction>(V));
572 }
573};
574
575//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000576// ICmpInst Class
577//===----------------------------------------------------------------------===//
578
579/// This instruction compares its operands according to the predicate given
580/// to the constructor. It only operates on integers, pointers, or packed
581/// vectors of integrals. The two operands must be the same type.
582/// @brief Represent an integer comparison operator.
583class ICmpInst: public CmpInst {
584public:
585 /// This enumeration lists the possible predicates for the ICmpInst. The
586 /// values in the range 0-31 are reserved for FCmpInst while values in the
587 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
588 /// predicate values are not overlapping between the classes.
589 enum Predicate {
590 ICMP_EQ = 32, ///< equal
591 ICMP_NE = 33, ///< not equal
592 ICMP_UGT = 34, ///< unsigned greater than
593 ICMP_UGE = 35, ///< unsigned greater or equal
594 ICMP_ULT = 36, ///< unsigned less than
595 ICMP_ULE = 37, ///< unsigned less or equal
596 ICMP_SGT = 38, ///< signed greater than
597 ICMP_SGE = 39, ///< signed greater or equal
598 ICMP_SLT = 40, ///< signed less than
599 ICMP_SLE = 41, ///< signed less or equal
600 FIRST_ICMP_PREDICATE = ICMP_EQ,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000601 LAST_ICMP_PREDICATE = ICMP_SLE,
602 BAD_ICMP_PREDICATE = ICMP_SLE + 1
Reid Spencer45fb3f32006-11-20 01:22:35 +0000603 };
604
605 /// @brief Constructor with insert-before-instruction semantics.
606 ICmpInst(
607 Predicate pred, ///< The predicate to use for the comparison
608 Value *LHS, ///< The left-hand-side of the expression
609 Value *RHS, ///< The right-hand-side of the expression
610 const std::string &Name = "", ///< Name of the instruction
611 Instruction *InsertBefore = 0 ///< Where to insert
612 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertBefore) {
613 }
614
615 /// @brief Constructor with insert-at-block-end semantics.
616 ICmpInst(
617 Predicate pred, ///< The predicate to use for the comparison
618 Value *LHS, ///< The left-hand-side of the expression
619 Value *RHS, ///< The right-hand-side of the expression
620 const std::string &Name, ///< Name of the instruction
621 BasicBlock *InsertAtEnd ///< Block to insert into.
622 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertAtEnd) {
623 }
624
625 /// @brief Return the predicate for this instruction.
626 Predicate getPredicate() const { return Predicate(SubclassData); }
627
Chris Lattnerb769d562007-01-14 19:41:24 +0000628 /// @brief Set the predicate for this instruction to the specified value.
629 void setPredicate(Predicate P) { SubclassData = P; }
630
Reid Spencer45fb3f32006-11-20 01:22:35 +0000631 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
632 /// @returns the inverse predicate for the instruction's current predicate.
633 /// @brief Return the inverse of the instruction's predicate.
634 Predicate getInversePredicate() const {
635 return getInversePredicate(getPredicate());
636 }
637
638 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
639 /// @returns the inverse predicate for predicate provided in \p pred.
640 /// @brief Return the inverse of a given predicate
641 static Predicate getInversePredicate(Predicate pred);
642
643 /// For example, EQ->EQ, SLE->SGE, ULT->UGT, etc.
644 /// @returns the predicate that would be the result of exchanging the two
645 /// operands of the ICmpInst instruction without changing the result
646 /// produced.
647 /// @brief Return the predicate as if the operands were swapped
648 Predicate getSwappedPredicate() const {
649 return getSwappedPredicate(getPredicate());
650 }
651
652 /// This is a static version that you can use without an instruction
653 /// available.
654 /// @brief Return the predicate as if the operands were swapped.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000655 static Predicate getSwappedPredicate(Predicate pred);
656
657 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
658 /// @returns the predicate that would be the result if the operand were
659 /// regarded as signed.
660 /// @brief Return the signed version of the predicate
661 Predicate getSignedPredicate() const {
662 return getSignedPredicate(getPredicate());
663 }
664
665 /// This is a static version that you can use without an instruction.
666 /// @brief Return the signed version of the predicate.
667 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000668
Nick Lewycky4189a532008-01-28 03:48:02 +0000669 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
670 /// @returns the predicate that would be the result if the operand were
671 /// regarded as unsigned.
672 /// @brief Return the unsigned version of the predicate
673 Predicate getUnsignedPredicate() const {
674 return getUnsignedPredicate(getPredicate());
675 }
676
677 /// This is a static version that you can use without an instruction.
678 /// @brief Return the unsigned version of the predicate.
679 static Predicate getUnsignedPredicate(Predicate pred);
680
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000681 /// isEquality - Return true if this predicate is either EQ or NE. This also
682 /// tests for commutativity.
683 static bool isEquality(Predicate P) {
684 return P == ICMP_EQ || P == ICMP_NE;
685 }
686
687 /// isEquality - Return true if this predicate is either EQ or NE. This also
688 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000689 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000690 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000691 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000692
693 /// @returns true if the predicate of this ICmpInst is commutative
694 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000695 bool isCommutative() const { return isEquality(); }
696
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000697 /// isRelational - Return true if the predicate is relational (not EQ or NE).
698 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +0000699 bool isRelational() const {
700 return !isEquality();
701 }
702
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000703 /// isRelational - Return true if the predicate is relational (not EQ or NE).
704 ///
705 static bool isRelational(Predicate P) {
706 return !isEquality(P);
707 }
708
Reid Spencere4d87aa2006-12-23 06:05:41 +0000709 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
710 /// @brief Determine if this instruction's predicate is signed.
Chris Lattner5bda9e42007-09-15 06:51:03 +0000711 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000712
713 /// @returns true if the predicate provided is signed, false otherwise
714 /// @brief Determine if the predicate is signed.
715 static bool isSignedPredicate(Predicate pred);
716
Reid Spencer3da43842007-02-28 22:00:54 +0000717 /// Initialize a set of values that all satisfy the predicate with C.
718 /// @brief Make a ConstantRange for a relation with a constant value.
719 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
720
Reid Spencer45fb3f32006-11-20 01:22:35 +0000721 /// Exchange the two operands to this instruction in such a way that it does
722 /// not modify the semantics of the instruction. The predicate value may be
723 /// changed to retain the same result if the predicate is order dependent
724 /// (e.g. ult).
725 /// @brief Swap operands and adjust predicate.
726 void swapOperands() {
727 SubclassData = getSwappedPredicate();
728 std::swap(Ops[0], Ops[1]);
729 }
730
Chris Lattnercd406fe2007-08-24 20:48:18 +0000731 virtual ICmpInst *clone() const;
732
Reid Spencer45fb3f32006-11-20 01:22:35 +0000733 // Methods for support type inquiry through isa, cast, and dyn_cast:
734 static inline bool classof(const ICmpInst *) { return true; }
735 static inline bool classof(const Instruction *I) {
736 return I->getOpcode() == Instruction::ICmp;
737 }
738 static inline bool classof(const Value *V) {
739 return isa<Instruction>(V) && classof(cast<Instruction>(V));
740 }
741};
742
743//===----------------------------------------------------------------------===//
744// FCmpInst Class
745//===----------------------------------------------------------------------===//
746
747/// This instruction compares its operands according to the predicate given
748/// to the constructor. It only operates on floating point values or packed
749/// vectors of floating point values. The operands must be identical types.
750/// @brief Represents a floating point comparison operator.
751class FCmpInst: public CmpInst {
752public:
753 /// This enumeration lists the possible predicates for the FCmpInst. Values
754 /// in the range 0-31 are reserved for FCmpInst.
755 enum Predicate {
756 // Opcode U L G E Intuitive operation
757 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
758 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
759 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
760 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
761 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
762 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
763 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
764 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
765 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
766 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
767 FCMP_UGT =10, ///< 1 0 1 0 True if unordered or greater than
768 FCMP_UGE =11, ///< 1 0 1 1 True if unordered, greater than, or equal
769 FCMP_ULT =12, ///< 1 1 0 0 True if unordered or less than
770 FCMP_ULE =13, ///< 1 1 0 1 True if unordered, less than, or equal
771 FCMP_UNE =14, ///< 1 1 1 0 True if unordered or not equal
772 FCMP_TRUE =15, ///< 1 1 1 1 Always true (always folded)
773 FIRST_FCMP_PREDICATE = FCMP_FALSE,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000774 LAST_FCMP_PREDICATE = FCMP_TRUE,
775 BAD_FCMP_PREDICATE = FCMP_TRUE + 1
Reid Spencer45fb3f32006-11-20 01:22:35 +0000776 };
777
778 /// @brief Constructor with insert-before-instruction semantics.
779 FCmpInst(
780 Predicate pred, ///< The predicate to use for the comparison
781 Value *LHS, ///< The left-hand-side of the expression
782 Value *RHS, ///< The right-hand-side of the expression
783 const std::string &Name = "", ///< Name of the instruction
784 Instruction *InsertBefore = 0 ///< Where to insert
785 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertBefore) {
786 }
787
788 /// @brief Constructor with insert-at-block-end semantics.
789 FCmpInst(
790 Predicate pred, ///< The predicate to use for the comparison
791 Value *LHS, ///< The left-hand-side of the expression
792 Value *RHS, ///< The right-hand-side of the expression
793 const std::string &Name, ///< Name of the instruction
794 BasicBlock *InsertAtEnd ///< Block to insert into.
795 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertAtEnd) {
796 }
797
798 /// @brief Return the predicate for this instruction.
799 Predicate getPredicate() const { return Predicate(SubclassData); }
800
Chris Lattnerb769d562007-01-14 19:41:24 +0000801 /// @brief Set the predicate for this instruction to the specified value.
802 void setPredicate(Predicate P) { SubclassData = P; }
803
Reid Spencer45fb3f32006-11-20 01:22:35 +0000804 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
805 /// @returns the inverse predicate for the instructions current predicate.
806 /// @brief Return the inverse of the predicate
807 Predicate getInversePredicate() const {
808 return getInversePredicate(getPredicate());
809 }
810
811 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
812 /// @returns the inverse predicate for \p pred.
813 /// @brief Return the inverse of a given predicate
814 static Predicate getInversePredicate(Predicate pred);
815
816 /// For example, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
817 /// @returns the predicate that would be the result of exchanging the two
818 /// operands of the ICmpInst instruction without changing the result
819 /// produced.
820 /// @brief Return the predicate as if the operands were swapped
821 Predicate getSwappedPredicate() const {
822 return getSwappedPredicate(getPredicate());
823 }
824
825 /// This is a static version that you can use without an instruction
826 /// available.
827 /// @brief Return the predicate as if the operands were swapped.
828 static Predicate getSwappedPredicate(Predicate Opcode);
829
830 /// This also tests for commutativity. If isEquality() returns true then
831 /// the predicate is also commutative. Only the equality predicates are
832 /// commutative.
833 /// @returns true if the predicate of this instruction is EQ or NE.
834 /// @brief Determine if this is an equality predicate.
835 bool isEquality() const {
836 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
837 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
838 }
839 bool isCommutative() const { return isEquality(); }
840
841 /// @returns true if the predicate is relational (not EQ or NE).
842 /// @brief Determine if this a relational predicate.
843 bool isRelational() const { return !isEquality(); }
844
845 /// Exchange the two operands to this instruction in such a way that it does
846 /// not modify the semantics of the instruction. The predicate value may be
847 /// changed to retain the same result if the predicate is order dependent
848 /// (e.g. ult).
849 /// @brief Swap operands and adjust predicate.
850 void swapOperands() {
851 SubclassData = getSwappedPredicate();
852 std::swap(Ops[0], Ops[1]);
853 }
854
Chris Lattnercd406fe2007-08-24 20:48:18 +0000855 virtual FCmpInst *clone() const;
856
Reid Spencer45fb3f32006-11-20 01:22:35 +0000857 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
858 static inline bool classof(const FCmpInst *) { return true; }
859 static inline bool classof(const Instruction *I) {
860 return I->getOpcode() == Instruction::FCmp;
861 }
862 static inline bool classof(const Value *V) {
863 return isa<Instruction>(V) && classof(cast<Instruction>(V));
864 }
865};
866
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000867//===----------------------------------------------------------------------===//
868// CallInst Class
869//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000870/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +0000871/// machine's calling convention. This class uses low bit of the SubClassData
872/// field to indicate whether or not this is a tail call. The rest of the bits
873/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000874///
David Greene52eec542007-08-01 03:43:44 +0000875
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000876class CallInst : public Instruction {
Chris Lattner58d74912008-03-12 17:45:29 +0000877 PAListPtr ParamAttrs; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000878 CallInst(const CallInst &CI);
Chris Lattnerd54f4322007-02-13 00:58:44 +0000879 void init(Value *Func, Value* const *Params, unsigned NumParams);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000880 void init(Value *Func, Value *Actual1, Value *Actual2);
881 void init(Value *Func, Value *Actual);
882 void init(Value *Func);
883
David Greene52eec542007-08-01 03:43:44 +0000884 template<typename InputIterator>
885 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
886 const std::string &Name,
887 // This argument ensures that we have an iterator we can
888 // do arithmetic on in constant time
889 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000890 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greene52eec542007-08-01 03:43:44 +0000891
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000892 // This requires that the iterator points to contiguous memory.
893 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
David Greene52eec542007-08-01 03:43:44 +0000894 setName(Name);
895 }
896
David Greene52eec542007-08-01 03:43:44 +0000897 /// Construct a CallInst given a range of arguments. InputIterator
898 /// must be a random-access iterator pointing to contiguous storage
899 /// (e.g. a std::vector<>::iterator). Checks are made for
900 /// random-accessness but not for contiguous storage as that would
901 /// incur runtime overhead.
902 /// @brief Construct a CallInst from a range of arguments
903 template<typename InputIterator>
904 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
905 const std::string &Name = "", Instruction *InsertBefore = 0)
906 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
907 ->getElementType())->getReturnType(),
908 Instruction::Call, 0, 0, InsertBefore) {
909 init(Func, ArgBegin, ArgEnd, Name,
910 typename std::iterator_traits<InputIterator>::iterator_category());
911 }
912
913 /// Construct a CallInst given a range of arguments. InputIterator
914 /// must be a random-access iterator pointing to contiguous storage
915 /// (e.g. a std::vector<>::iterator). Checks are made for
916 /// random-accessness but not for contiguous storage as that would
917 /// incur runtime overhead.
918 /// @brief Construct a CallInst from a range of arguments
919 template<typename InputIterator>
920 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
921 const std::string &Name, BasicBlock *InsertAtEnd)
922 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
923 ->getElementType())->getReturnType(),
924 Instruction::Call, 0, 0, InsertAtEnd) {
925 init(Func, ArgBegin, ArgEnd, Name,
926 typename std::iterator_traits<InputIterator>::iterator_category());
927 }
928
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000929 CallInst(Value *F, Value *Actual, const std::string& Name = "",
930 Instruction *InsertBefore = 0);
931 CallInst(Value *F, Value *Actual, const std::string& Name,
932 BasicBlock *InsertAtEnd);
Misha Brukman9769ab22005-04-21 20:19:05 +0000933 explicit CallInst(Value *F, const std::string &Name = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000934 Instruction *InsertBefore = 0);
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000935 CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000936public:
937 template<typename InputIterator>
938 static CallInst *Create(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
939 const std::string &Name = "", Instruction *InsertBefore = 0) {
940 return new(ArgEnd - ArgBegin + 1) CallInst(Func, ArgBegin, ArgEnd, Name, InsertBefore);
941 }
942 template<typename InputIterator>
943 static CallInst *Create(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
944 const std::string &Name, BasicBlock *InsertAtEnd) {
945 return new(ArgEnd - ArgBegin + 1) CallInst(Func, ArgBegin, ArgEnd, Name, InsertAtEnd);
946 }
947 static CallInst *Create(Value *F, Value *Actual, const std::string& Name = "",
948 Instruction *InsertBefore = 0) {
949 return new(2) CallInst(F, Actual, Name, InsertBefore);
950 }
951 static CallInst *Create(Value *F, Value *Actual, const std::string& Name,
952 BasicBlock *InsertAtEnd) {
953 return new(2) CallInst(F, Actual, Name, InsertAtEnd);
954 }
955 static CallInst *Create(Value *F, const std::string &Name = "",
956 Instruction *InsertBefore = 0) {
957 return new(1) CallInst(F, Name, InsertBefore);
958 }
959 static CallInst *Create(Value *F, const std::string &Name, BasicBlock *InsertAtEnd) {
960 return new(1) CallInst(F, Name, InsertAtEnd);
961 }
962
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000963 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000964
Chris Lattnerf319e832004-10-15 23:52:05 +0000965 virtual CallInst *clone() const;
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000966
Chris Lattner3340ffe2005-05-06 20:26:26 +0000967 bool isTailCall() const { return SubclassData & 1; }
968 void setTailCall(bool isTailCall = true) {
Jeff Cohen39cef602005-05-07 02:44:04 +0000969 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
Chris Lattner3340ffe2005-05-06 20:26:26 +0000970 }
971
972 /// getCallingConv/setCallingConv - Get or set the calling convention of this
973 /// function call.
974 unsigned getCallingConv() const { return SubclassData >> 1; }
975 void setCallingConv(unsigned CC) {
976 SubclassData = (SubclassData & 1) | (CC << 1);
977 }
Chris Lattnerddb6db42005-05-06 05:51:46 +0000978
Chris Lattner041221c2008-03-13 04:33:03 +0000979 /// getParamAttrs - Return the parameter attributes for this call.
980 ///
Chris Lattner58d74912008-03-12 17:45:29 +0000981 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
Reid Spencer4746ecf2007-04-09 15:01:12 +0000982
Chris Lattner041221c2008-03-13 04:33:03 +0000983 /// setParamAttrs - Sets the parameter attributes for this call.
Chris Lattner58d74912008-03-12 17:45:29 +0000984 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +0000985
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000986 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattnerd5d94df2008-03-13 05:00:21 +0000987 bool paramHasAttr(unsigned i, unsigned attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000988
Dale Johannesen08e78b12008-02-22 17:49:45 +0000989 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +0000990 unsigned getParamAlignment(unsigned i) const {
991 return ParamAttrs.getParamAlignment(i);
992 }
Dale Johannesen08e78b12008-02-22 17:49:45 +0000993
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000994 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +0000995 bool doesNotAccessMemory() const {
996 return paramHasAttr(0, ParamAttr::ReadNone);
997 }
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000998
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000999 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001000 bool onlyReadsMemory() const {
1001 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
1002 }
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001003
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001004 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001005 bool doesNotReturn() const {
1006 return paramHasAttr(0, ParamAttr::NoReturn);
1007 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001008
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001009 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001010 bool doesNotThrow() const {
1011 return paramHasAttr(0, ParamAttr::NoUnwind);
1012 }
Duncan Sandsf0c33542007-12-19 21:13:37 +00001013 void setDoesNotThrow(bool doesNotThrow = true);
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001014
Devang Patel41e23972008-03-03 21:46:28 +00001015 /// @brief Determine if the call returns a structure through first
1016 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001017 bool hasStructRetAttr() const {
1018 // Be friendly and also check the callee.
1019 return paramHasAttr(1, ParamAttr::StructRet);
1020 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001021
Evan Chengf4a54982008-01-12 18:57:32 +00001022 /// @brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001023 bool hasByValArgument() const {
1024 return ParamAttrs.hasAttrSomewhere(ParamAttr::ByVal);
1025 }
Evan Chengf4a54982008-01-12 18:57:32 +00001026
Chris Lattner721aef62004-11-18 17:46:57 +00001027 /// getCalledFunction - Return the function being called by this instruction
1028 /// if it is a direct call. If it is a call through a function pointer,
1029 /// return null.
1030 Function *getCalledFunction() const {
Dan Gohman11a7dbf2007-09-24 15:46:02 +00001031 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001032 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001033
Reid Spencerc25ec252006-12-29 04:10:59 +00001034 /// getCalledValue - Get a pointer to the function that is invoked by this
1035 /// instruction
Devang Patel4d4a5e02008-02-23 01:11:02 +00001036 const Value *getCalledValue() const { return getOperand(0); }
1037 Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001038
1039 // Methods for support type inquiry through isa, cast, and dyn_cast:
1040 static inline bool classof(const CallInst *) { return true; }
1041 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001042 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001043 }
1044 static inline bool classof(const Value *V) {
1045 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1046 }
1047};
1048
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001049//===----------------------------------------------------------------------===//
1050// SelectInst Class
1051//===----------------------------------------------------------------------===//
1052
1053/// SelectInst - This class represents the LLVM 'select' instruction.
1054///
1055class SelectInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +00001056 Use Ops[3];
1057
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001058 void init(Value *C, Value *S1, Value *S2) {
Chris Lattner454928e2005-01-29 00:31:36 +00001059 Ops[0].init(C, this);
1060 Ops[1].init(S1, this);
1061 Ops[2].init(S2, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001062 }
1063
Chris Lattner454928e2005-01-29 00:31:36 +00001064 SelectInst(const SelectInst &SI)
1065 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
1066 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
1067 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001068 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
1069 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001070 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001071 init(C, S1, S2);
Chris Lattner910c80a2007-02-24 00:55:48 +00001072 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001073 }
1074 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1075 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001076 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001077 init(C, S1, S2);
Chris Lattner910c80a2007-02-24 00:55:48 +00001078 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001079 }
Gabor Greif051a9502008-04-06 20:25:17 +00001080public:
1081 static SelectInst *Create(Value *C, Value *S1, Value *S2, const std::string &Name = "",
1082 Instruction *InsertBefore = 0) {
1083 return new(3) SelectInst(C, S1, S2, Name, InsertBefore);
1084 }
1085 static SelectInst *Create(Value *C, Value *S1, Value *S2, const std::string &Name,
1086 BasicBlock *InsertAtEnd) {
1087 return new(3) SelectInst(C, S1, S2, Name, InsertAtEnd);
1088 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001089
Chris Lattner454928e2005-01-29 00:31:36 +00001090 Value *getCondition() const { return Ops[0]; }
1091 Value *getTrueValue() const { return Ops[1]; }
1092 Value *getFalseValue() const { return Ops[2]; }
1093
1094 /// Transparently provide more efficient getOperand methods.
1095 Value *getOperand(unsigned i) const {
1096 assert(i < 3 && "getOperand() out of range!");
1097 return Ops[i];
1098 }
1099 void setOperand(unsigned i, Value *Val) {
1100 assert(i < 3 && "setOperand() out of range!");
1101 Ops[i] = Val;
1102 }
1103 unsigned getNumOperands() const { return 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001104
1105 OtherOps getOpcode() const {
1106 return static_cast<OtherOps>(Instruction::getOpcode());
1107 }
1108
Chris Lattnerf319e832004-10-15 23:52:05 +00001109 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001110
1111 // Methods for support type inquiry through isa, cast, and dyn_cast:
1112 static inline bool classof(const SelectInst *) { return true; }
1113 static inline bool classof(const Instruction *I) {
1114 return I->getOpcode() == Instruction::Select;
1115 }
1116 static inline bool classof(const Value *V) {
1117 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1118 }
1119};
1120
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001121//===----------------------------------------------------------------------===//
1122// VAArgInst Class
1123//===----------------------------------------------------------------------===//
1124
1125/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001126/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001127///
Chris Lattner454928e2005-01-29 00:31:36 +00001128class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001129 VAArgInst(const VAArgInst &VAA)
Chris Lattner454928e2005-01-29 00:31:36 +00001130 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001131public:
1132 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
1133 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001134 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001135 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001136 }
1137 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
1138 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001139 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001140 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001141 }
1142
Chris Lattnerf319e832004-10-15 23:52:05 +00001143 virtual VAArgInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001144
1145 // Methods for support type inquiry through isa, cast, and dyn_cast:
1146 static inline bool classof(const VAArgInst *) { return true; }
1147 static inline bool classof(const Instruction *I) {
1148 return I->getOpcode() == VAArg;
1149 }
1150 static inline bool classof(const Value *V) {
1151 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1152 }
1153};
1154
1155//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001156// ExtractElementInst Class
1157//===----------------------------------------------------------------------===//
1158
1159/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001160/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001161///
1162class ExtractElementInst : public Instruction {
1163 Use Ops[2];
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001164 ExtractElementInst(const ExtractElementInst &EE) :
Robert Bocchinof9993442006-01-17 20:05:59 +00001165 Instruction(EE.getType(), ExtractElement, Ops, 2) {
1166 Ops[0].init(EE.Ops[0], this);
1167 Ops[1].init(EE.Ops[1], this);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001168 }
1169
1170public:
Gabor Greif051a9502008-04-06 20:25:17 +00001171 // allocate space for exactly two operands
1172 void *operator new(size_t s) {
1173 return User::operator new(s, 2); // FIXME: unsigned Idx forms of constructor?
1174 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001175 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
1176 Instruction *InsertBefore = 0);
Chris Lattner06a248c22006-10-05 06:24:58 +00001177 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
1178 Instruction *InsertBefore = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001179 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
1180 BasicBlock *InsertAtEnd);
Chris Lattner06a248c22006-10-05 06:24:58 +00001181 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
1182 BasicBlock *InsertAtEnd);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001183
Chris Lattnerfa495842006-04-08 04:04:54 +00001184 /// isValidOperands - Return true if an extractelement instruction can be
1185 /// formed with the specified operands.
1186 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001187
Robert Bocchino49b78a52006-01-10 19:04:13 +00001188 virtual ExtractElementInst *clone() const;
1189
Robert Bocchino49b78a52006-01-10 19:04:13 +00001190 /// Transparently provide more efficient getOperand methods.
1191 Value *getOperand(unsigned i) const {
1192 assert(i < 2 && "getOperand() out of range!");
1193 return Ops[i];
1194 }
1195 void setOperand(unsigned i, Value *Val) {
1196 assert(i < 2 && "setOperand() out of range!");
1197 Ops[i] = Val;
1198 }
1199 unsigned getNumOperands() const { return 2; }
1200
1201 // Methods for support type inquiry through isa, cast, and dyn_cast:
1202 static inline bool classof(const ExtractElementInst *) { return true; }
1203 static inline bool classof(const Instruction *I) {
1204 return I->getOpcode() == Instruction::ExtractElement;
1205 }
1206 static inline bool classof(const Value *V) {
1207 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1208 }
1209};
1210
1211//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001212// InsertElementInst Class
1213//===----------------------------------------------------------------------===//
1214
1215/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001216/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001217///
1218class InsertElementInst : public Instruction {
1219 Use Ops[3];
Chris Lattner6a56ed42006-04-14 22:20:07 +00001220 InsertElementInst(const InsertElementInst &IE);
Gabor Greif051a9502008-04-06 20:25:17 +00001221 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1222 const std::string &Name = "",Instruction *InsertBefore = 0);
1223 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1224 const std::string &Name = "",Instruction *InsertBefore = 0);
1225 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1226 const std::string &Name, BasicBlock *InsertAtEnd);
1227 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1228 const std::string &Name, BasicBlock *InsertAtEnd);
Robert Bocchinof9993442006-01-17 20:05:59 +00001229public:
Gabor Greif051a9502008-04-06 20:25:17 +00001230 static InsertElementInst *Create(const InsertElementInst &IE) {
1231 return new(IE.getNumOperands()) InsertElementInst(IE);
1232 }
1233 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1234 const std::string &Name = "",Instruction *InsertBefore = 0) {
1235 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
1236 }
1237 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1238 const std::string &Name = "",Instruction *InsertBefore = 0) {
1239 return new(3/*FIXME*/) InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
1240 }
1241 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1242 const std::string &Name, BasicBlock *InsertAtEnd) {
1243 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertAtEnd);
1244 }
1245 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1246 const std::string &Name, BasicBlock *InsertAtEnd) {
1247 return new(3/*FIXME*/) InsertElementInst(Vec, NewElt, Idx, Name, InsertAtEnd);
1248 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001249
Chris Lattnerfa495842006-04-08 04:04:54 +00001250 /// isValidOperands - Return true if an insertelement instruction can be
1251 /// formed with the specified operands.
1252 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1253 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001254
Robert Bocchinof9993442006-01-17 20:05:59 +00001255 virtual InsertElementInst *clone() const;
1256
Reid Spencerac9dcb92007-02-15 03:39:18 +00001257 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001258 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001259 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001260 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001261 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001262
Robert Bocchinof9993442006-01-17 20:05:59 +00001263 /// Transparently provide more efficient getOperand methods.
1264 Value *getOperand(unsigned i) const {
1265 assert(i < 3 && "getOperand() out of range!");
1266 return Ops[i];
1267 }
1268 void setOperand(unsigned i, Value *Val) {
1269 assert(i < 3 && "setOperand() out of range!");
1270 Ops[i] = Val;
1271 }
1272 unsigned getNumOperands() const { return 3; }
1273
1274 // Methods for support type inquiry through isa, cast, and dyn_cast:
1275 static inline bool classof(const InsertElementInst *) { return true; }
1276 static inline bool classof(const Instruction *I) {
1277 return I->getOpcode() == Instruction::InsertElement;
1278 }
1279 static inline bool classof(const Value *V) {
1280 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1281 }
1282};
1283
1284//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001285// ShuffleVectorInst Class
1286//===----------------------------------------------------------------------===//
1287
1288/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1289/// input vectors.
1290///
1291class ShuffleVectorInst : public Instruction {
1292 Use Ops[3];
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001293 ShuffleVectorInst(const ShuffleVectorInst &IE);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001294public:
Gabor Greif051a9502008-04-06 20:25:17 +00001295 // allocate space for exactly three operands
1296 void *operator new(size_t s) {
1297 return User::operator new(s, 3);
1298 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001299 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1300 const std::string &Name = "", Instruction *InsertBefor = 0);
1301 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1302 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001303
Chris Lattnerfa495842006-04-08 04:04:54 +00001304 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001305 /// formed with the specified operands.
1306 static bool isValidOperands(const Value *V1, const Value *V2,
1307 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001308
Chris Lattner9fc18d22006-04-08 01:15:18 +00001309 virtual ShuffleVectorInst *clone() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001310
Reid Spencerac9dcb92007-02-15 03:39:18 +00001311 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001312 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001313 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001314 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001315 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001316
Chris Lattner9fc18d22006-04-08 01:15:18 +00001317 /// Transparently provide more efficient getOperand methods.
Chris Lattner4fa01442008-03-02 05:32:05 +00001318 const Value *getOperand(unsigned i) const {
1319 assert(i < 3 && "getOperand() out of range!");
1320 return Ops[i];
1321 }
1322 Value *getOperand(unsigned i) {
Chris Lattner9fc18d22006-04-08 01:15:18 +00001323 assert(i < 3 && "getOperand() out of range!");
1324 return Ops[i];
1325 }
1326 void setOperand(unsigned i, Value *Val) {
1327 assert(i < 3 && "setOperand() out of range!");
1328 Ops[i] = Val;
1329 }
1330 unsigned getNumOperands() const { return 3; }
Chris Lattner8728f192008-03-02 05:28:33 +00001331
1332 /// getMaskValue - Return the index from the shuffle mask for the specified
1333 /// output result. This is either -1 if the element is undef or a number less
1334 /// than 2*numelements.
1335 int getMaskValue(unsigned i) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001336
Chris Lattner9fc18d22006-04-08 01:15:18 +00001337 // Methods for support type inquiry through isa, cast, and dyn_cast:
1338 static inline bool classof(const ShuffleVectorInst *) { return true; }
1339 static inline bool classof(const Instruction *I) {
1340 return I->getOpcode() == Instruction::ShuffleVector;
1341 }
1342 static inline bool classof(const Value *V) {
1343 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1344 }
1345};
1346
1347
1348//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001349// PHINode Class
1350//===----------------------------------------------------------------------===//
1351
1352// PHINode - The PHINode class is used to represent the magical mystical PHI
1353// node, that can not exist in nature, but can be synthesized in a computer
1354// scientist's overactive imagination.
1355//
1356class PHINode : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001357 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00001358 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1359 /// the number actually in use.
1360 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001361 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00001362 // allocate space for exactly zero operands
1363 void *operator new(size_t s) {
1364 return User::operator new(s, 0);
1365 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001366 explicit PHINode(const Type *Ty, const std::string &Name = "",
1367 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001368 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Chris Lattner454928e2005-01-29 00:31:36 +00001369 ReservedSpace(0) {
Chris Lattner910c80a2007-02-24 00:55:48 +00001370 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001371 }
1372
1373 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001374 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Chris Lattner454928e2005-01-29 00:31:36 +00001375 ReservedSpace(0) {
Chris Lattner910c80a2007-02-24 00:55:48 +00001376 setName(Name);
Chris Lattner454928e2005-01-29 00:31:36 +00001377 }
Gabor Greif051a9502008-04-06 20:25:17 +00001378public:
1379 static PHINode *Create(const Type *Ty, const std::string &Name = "",
1380 Instruction *InsertBefore = 0) {
1381 return new PHINode(Ty, Name, InsertBefore);
1382 }
1383 static PHINode *Create(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd) {
1384 return new PHINode(Ty, Name, InsertAtEnd);
1385 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001386 ~PHINode();
1387
Chris Lattner454928e2005-01-29 00:31:36 +00001388 /// reserveOperandSpace - This method can be used to avoid repeated
1389 /// reallocation of PHI operand lists by reserving space for the correct
1390 /// number of operands before adding them. Unlike normal vector reserves,
1391 /// this method can also be used to trim the operand space.
1392 void reserveOperandSpace(unsigned NumValues) {
1393 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001394 }
1395
Chris Lattnerf319e832004-10-15 23:52:05 +00001396 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001397
1398 /// getNumIncomingValues - Return the number of incoming edges
1399 ///
Chris Lattner454928e2005-01-29 00:31:36 +00001400 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001401
Reid Spencerc773de62006-05-19 19:07:54 +00001402 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001403 ///
1404 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001405 assert(i*2 < getNumOperands() && "Invalid value number!");
1406 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001407 }
1408 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +00001409 assert(i*2 < getNumOperands() && "Invalid value number!");
1410 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001411 }
Chris Lattner454928e2005-01-29 00:31:36 +00001412 unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001413 return i*2;
1414 }
1415
Reid Spencerc773de62006-05-19 19:07:54 +00001416 /// getIncomingBlock - Return incoming basic block number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001417 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00001418 BasicBlock *getIncomingBlock(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001419 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001420 }
1421 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Chris Lattner454928e2005-01-29 00:31:36 +00001422 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001423 }
1424 unsigned getOperandNumForIncomingBlock(unsigned i) {
1425 return i*2+1;
1426 }
1427
1428 /// addIncoming - Add an incoming value to the end of the PHI list
1429 ///
1430 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00001431 assert(V && "PHI node got a null value!");
1432 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001433 assert(getType() == V->getType() &&
1434 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +00001435 unsigned OpNo = NumOperands;
1436 if (OpNo+2 > ReservedSpace)
1437 resizeOperands(0); // Get more space!
1438 // Initialize some new operands.
1439 NumOperands = OpNo+2;
1440 OperandList[OpNo].init(V, this);
1441 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001442 }
Misha Brukman9769ab22005-04-21 20:19:05 +00001443
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001444 /// removeIncomingValue - Remove an incoming value. This is useful if a
1445 /// predecessor basic block is deleted. The value removed is returned.
1446 ///
1447 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1448 /// is true), the PHI node is destroyed and any uses of it are replaced with
1449 /// dummy values. The only time there should be zero incoming values to a PHI
1450 /// node is when the block is dead, so this strategy is sound.
1451 ///
1452 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1453
1454 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1455 int Idx = getBasicBlockIndex(BB);
1456 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1457 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1458 }
1459
Misha Brukman9769ab22005-04-21 20:19:05 +00001460 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001461 /// block in the value list for this PHI. Returns -1 if no instance.
1462 ///
1463 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001464 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00001465 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Chris Lattner454928e2005-01-29 00:31:36 +00001466 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001467 return -1;
1468 }
1469
1470 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1471 return getIncomingValue(getBasicBlockIndex(BB));
1472 }
1473
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001474 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00001475 /// same value, return the value, otherwise return null.
1476 ///
Chris Lattner9acbd612005-08-05 00:49:06 +00001477 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001478
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001479 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1480 static inline bool classof(const PHINode *) { return true; }
1481 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001482 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001483 }
1484 static inline bool classof(const Value *V) {
1485 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1486 }
Chris Lattner454928e2005-01-29 00:31:36 +00001487 private:
1488 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001489};
1490
1491//===----------------------------------------------------------------------===//
1492// ReturnInst Class
1493//===----------------------------------------------------------------------===//
1494
1495//===---------------------------------------------------------------------------
1496/// ReturnInst - Return a value (possibly void), from a function. Execution
1497/// does not continue in this function any longer.
1498///
1499class ReturnInst : public TerminatorInst {
Devang Patel64d4e612008-02-26 17:56:20 +00001500 Use RetVal;
Chris Lattner910c80a2007-02-24 00:55:48 +00001501 ReturnInst(const ReturnInst &RI);
Devang Patelfea98302008-02-26 19:15:26 +00001502 void init(Value * const* retVals, unsigned N);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001503
Gabor Greif051a9502008-04-06 20:25:17 +00001504private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001505 // ReturnInst constructors:
1506 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001507 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001508 // ReturnInst(Value* X) - 'ret X' instruction
1509 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
1510 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1511 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
1512 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
Devang Patele6be34a2008-02-27 01:20:54 +00001513 // ReturnInst(Value* X, N) - 'ret X,X+1...X+N-1' instruction
1514 // ReturnInst(Value* X, N, Inst *) - 'ret X,X+1...X+N-1', insert before I
1515 // ReturnInst(Value* X, N, BB *) - 'ret X,X+1...X+N-1', insert @ end of BB
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001516 //
1517 // NOTE: If the Value* passed is of type void then the constructor behaves as
1518 // if it was passed NULL.
Chris Lattner910c80a2007-02-24 00:55:48 +00001519 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1520 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
Devang Patelf4511cd2008-02-26 19:38:17 +00001521 ReturnInst(Value * const* retVals, unsigned N);
1522 ReturnInst(Value * const* retVals, unsigned N, Instruction *InsertBefore);
1523 ReturnInst(Value * const* retVals, unsigned N, BasicBlock *InsertAtEnd);
Chris Lattner910c80a2007-02-24 00:55:48 +00001524 explicit ReturnInst(BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001525public:
1526 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
1527 return new(!!retVal) ReturnInst(retVal, InsertBefore);
1528 }
1529 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
1530 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
1531 }
1532 static ReturnInst* Create(Value * const* retVals, unsigned N) {
1533 return new(N) ReturnInst(retVals, N);
1534 }
1535 static ReturnInst* Create(Value * const* retVals, unsigned N, Instruction *InsertBefore) {
1536 return new(N) ReturnInst(retVals, N, InsertBefore);
1537 }
1538 static ReturnInst* Create(Value * const* retVals, unsigned N, BasicBlock *InsertAtEnd) {
1539 return new(N) ReturnInst(retVals, N, InsertAtEnd);
1540 }
1541 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
1542 return new(0) ReturnInst(InsertAtEnd);
1543 }
Devang Patel57ef4f42008-02-23 00:35:18 +00001544 virtual ~ReturnInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001545
Chris Lattnerf319e832004-10-15 23:52:05 +00001546 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001547
Devang Patel1eafa062008-03-11 17:35:03 +00001548 Value *getOperand(unsigned n = 0) const {
Devang Patel22a8a732008-03-07 20:08:07 +00001549 if (getNumOperands() > 1)
Devang Patel1eafa062008-03-11 17:35:03 +00001550 return TerminatorInst::getOperand(n);
Devang Patel22a8a732008-03-07 20:08:07 +00001551 else
Devang Patel64d4e612008-02-26 17:56:20 +00001552 return RetVal;
Devang Patel64d4e612008-02-26 17:56:20 +00001553 }
1554
Devang Patel1eafa062008-03-11 17:35:03 +00001555 Value *getReturnValue(unsigned n = 0) const {
1556 return getOperand(n);
1557 }
1558
Chris Lattner454928e2005-01-29 00:31:36 +00001559 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001560
1561 // Methods for support type inquiry through isa, cast, and dyn_cast:
1562 static inline bool classof(const ReturnInst *) { return true; }
1563 static inline bool classof(const Instruction *I) {
1564 return (I->getOpcode() == Instruction::Ret);
1565 }
1566 static inline bool classof(const Value *V) {
1567 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1568 }
Chris Lattner454928e2005-01-29 00:31:36 +00001569 private:
1570 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1571 virtual unsigned getNumSuccessorsV() const;
1572 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001573};
1574
1575//===----------------------------------------------------------------------===//
1576// BranchInst Class
1577//===----------------------------------------------------------------------===//
1578
1579//===---------------------------------------------------------------------------
1580/// BranchInst - Conditional or Unconditional Branch instruction.
1581///
1582class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001583 /// Ops list - Branches are strange. The operands are ordered:
1584 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
1585 /// they don't have to check for cond/uncond branchness.
1586 Use Ops[3];
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001587 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00001588 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001589 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1590 // BranchInst(BB *B) - 'br B'
1591 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1592 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1593 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1594 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1595 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00001596 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001597 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00001598 Instruction *InsertBefore = 0);
1599 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001600 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00001601 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001602public:
1603 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
1604 return new(1) BranchInst(IfTrue, InsertBefore);
1605 }
1606 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1607 Instruction *InsertBefore = 0) {
1608 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
1609 }
1610 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
1611 return new(1) BranchInst(IfTrue, InsertAtEnd);
1612 }
1613 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1614 BasicBlock *InsertAtEnd) {
1615 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
1616 }
Chris Lattner454928e2005-01-29 00:31:36 +00001617
1618 /// Transparently provide more efficient getOperand methods.
1619 Value *getOperand(unsigned i) const {
1620 assert(i < getNumOperands() && "getOperand() out of range!");
1621 return Ops[i];
1622 }
1623 void setOperand(unsigned i, Value *Val) {
1624 assert(i < getNumOperands() && "setOperand() out of range!");
1625 Ops[i] = Val;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001626 }
1627
Chris Lattnerf319e832004-10-15 23:52:05 +00001628 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001629
Devang Patel4d4a5e02008-02-23 01:11:02 +00001630 bool isUnconditional() const { return getNumOperands() == 1; }
1631 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001632
Devang Patel4d4a5e02008-02-23 01:11:02 +00001633 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001634 assert(isConditional() && "Cannot get condition of an uncond branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001635 return getOperand(2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001636 }
1637
1638 void setCondition(Value *V) {
1639 assert(isConditional() && "Cannot set condition of unconditional branch!");
1640 setOperand(2, V);
1641 }
1642
1643 // setUnconditionalDest - Change the current branch to an unconditional branch
1644 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +00001645 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001646 void setUnconditionalDest(BasicBlock *Dest) {
Chris Lattner454928e2005-01-29 00:31:36 +00001647 if (isConditional()) { // Convert this to an uncond branch.
1648 NumOperands = 1;
1649 Ops[1].set(0);
1650 Ops[2].set(0);
1651 }
1652 setOperand(0, reinterpret_cast<Value*>(Dest));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001653 }
1654
Chris Lattner454928e2005-01-29 00:31:36 +00001655 unsigned getNumSuccessors() const { return 1+isConditional(); }
1656
1657 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001658 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Dan Gohmanb96039e2007-05-11 20:59:29 +00001659 return cast<BasicBlock>(getOperand(i));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001660 }
1661
Chris Lattner454928e2005-01-29 00:31:36 +00001662 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001663 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001664 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001665 }
1666
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001667 // Methods for support type inquiry through isa, cast, and dyn_cast:
1668 static inline bool classof(const BranchInst *) { return true; }
1669 static inline bool classof(const Instruction *I) {
1670 return (I->getOpcode() == Instruction::Br);
1671 }
1672 static inline bool classof(const Value *V) {
1673 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1674 }
Chris Lattner454928e2005-01-29 00:31:36 +00001675private:
1676 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1677 virtual unsigned getNumSuccessorsV() const;
1678 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001679};
1680
1681//===----------------------------------------------------------------------===//
1682// SwitchInst Class
1683//===----------------------------------------------------------------------===//
1684
1685//===---------------------------------------------------------------------------
1686/// SwitchInst - Multiway switch
1687///
1688class SwitchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001689 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001690 // Operand[0] = Value to switch on
1691 // Operand[1] = Default basic block destination
1692 // Operand[2n ] = Value to match
1693 // Operand[2n+1] = BasicBlock to go to on match
1694 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00001695 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1696 void resizeOperands(unsigned No);
Chris Lattner454928e2005-01-29 00:31:36 +00001697 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1698 /// switch on and a default destination. The number of additional cases can
1699 /// be specified here to make memory allocation more efficient. This
1700 /// constructor can also autoinsert before another instruction.
1701 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00001702 Instruction *InsertBefore = 0);
1703
Chris Lattner454928e2005-01-29 00:31:36 +00001704 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1705 /// switch on and a default destination. The number of additional cases can
1706 /// be specified here to make memory allocation more efficient. This
1707 /// constructor also autoinserts at the end of the specified BasicBlock.
1708 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00001709 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001710public:
1711 static SwitchInst *Create(Value *Value, BasicBlock *Default, unsigned NumCases,
1712 Instruction *InsertBefore = 0) {
1713 return new(NumCases/*FIXME*/) SwitchInst(Value, Default, NumCases, InsertBefore);
1714 }
1715 static SwitchInst *Create(Value *Value, BasicBlock *Default, unsigned NumCases,
1716 BasicBlock *InsertAtEnd) {
1717 return new(NumCases/*FIXME*/) SwitchInst(Value, Default, NumCases, InsertAtEnd);
1718 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001719 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00001720
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001721 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00001722 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00001723 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00001724
Devang Patel4d4a5e02008-02-23 01:11:02 +00001725 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001726 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001727 }
1728
1729 /// getNumCases - return the number of 'cases' in this switch instruction.
1730 /// Note that case #0 is always the default case.
1731 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001732 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001733 }
1734
1735 /// getCaseValue - Return the specified case value. Note that case #0, the
1736 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001737 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001738 assert(i && i < getNumCases() && "Illegal case value to get!");
1739 return getSuccessorValue(i);
1740 }
1741
1742 /// getCaseValue - Return the specified case value. Note that case #0, the
1743 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001744 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001745 assert(i && i < getNumCases() && "Illegal case value to get!");
1746 return getSuccessorValue(i);
1747 }
1748
1749 /// findCaseValue - Search all of the case values for the specified constant.
1750 /// If it is explicitly handled, return the case number of it, otherwise
1751 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001752 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001753 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1754 if (getCaseValue(i) == C)
1755 return i;
1756 return 0;
1757 }
1758
Nick Lewycky011f1842006-09-18 19:03:59 +00001759 /// findCaseDest - Finds the unique case value for a given successor. Returns
1760 /// null if the successor is not found, not unique, or is the default case.
1761 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00001762 if (BB == getDefaultDest()) return NULL;
1763
Nick Lewycky011f1842006-09-18 19:03:59 +00001764 ConstantInt *CI = NULL;
1765 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
1766 if (getSuccessor(i) == BB) {
1767 if (CI) return NULL; // Multiple cases lead to BB.
1768 else CI = getCaseValue(i);
1769 }
1770 }
1771 return CI;
1772 }
1773
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001774 /// addCase - Add an entry to the switch instruction...
1775 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00001776 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001777
1778 /// removeCase - This method removes the specified successor from the switch
1779 /// instruction. Note that this cannot be used to remove the default
1780 /// destination (successor #0).
1781 ///
1782 void removeCase(unsigned idx);
1783
Chris Lattner454928e2005-01-29 00:31:36 +00001784 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001785
Chris Lattner454928e2005-01-29 00:31:36 +00001786 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1787 BasicBlock *getSuccessor(unsigned idx) const {
1788 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1789 return cast<BasicBlock>(getOperand(idx*2+1));
1790 }
1791 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001792 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001793 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001794 }
1795
1796 // getSuccessorValue - Return the value associated with the specified
1797 // successor.
Devang Patel4d4a5e02008-02-23 01:11:02 +00001798 ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001799 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00001800 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001801 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001802
1803 // Methods for support type inquiry through isa, cast, and dyn_cast:
1804 static inline bool classof(const SwitchInst *) { return true; }
1805 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00001806 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001807 }
1808 static inline bool classof(const Value *V) {
1809 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1810 }
Chris Lattner454928e2005-01-29 00:31:36 +00001811private:
1812 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1813 virtual unsigned getNumSuccessorsV() const;
1814 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001815};
1816
1817//===----------------------------------------------------------------------===//
1818// InvokeInst Class
1819//===----------------------------------------------------------------------===//
1820
1821//===---------------------------------------------------------------------------
Chris Lattner3340ffe2005-05-06 20:26:26 +00001822
1823/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
1824/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001825///
1826class InvokeInst : public TerminatorInst {
Chris Lattner58d74912008-03-12 17:45:29 +00001827 PAListPtr ParamAttrs;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001828 InvokeInst(const InvokeInst &BI);
1829 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerd2dd1502007-02-13 01:04:01 +00001830 Value* const *Args, unsigned NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00001831
1832 template<typename InputIterator>
1833 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1834 InputIterator ArgBegin, InputIterator ArgEnd,
1835 const std::string &Name,
1836 // This argument ensures that we have an iterator we can
1837 // do arithmetic on in constant time
1838 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00001839 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greenef1355a52007-08-27 19:04:21 +00001840
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00001841 // This requires that the iterator points to contiguous memory.
1842 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00001843 setName(Name);
1844 }
1845
David Greenef1355a52007-08-27 19:04:21 +00001846 /// Construct an InvokeInst given a range of arguments.
1847 /// InputIterator must be a random-access iterator pointing to
1848 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
1849 /// made for random-accessness but not for contiguous storage as
1850 /// that would incur runtime overhead.
1851 ///
1852 /// @brief Construct an InvokeInst from a range of arguments
1853 template<typename InputIterator>
1854 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1855 InputIterator ArgBegin, InputIterator ArgEnd,
1856 const std::string &Name = "", Instruction *InsertBefore = 0)
1857 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
1858 ->getElementType())->getReturnType(),
1859 Instruction::Invoke, 0, 0, InsertBefore) {
1860 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1861 typename std::iterator_traits<InputIterator>::iterator_category());
1862 }
1863
1864 /// Construct an InvokeInst given a range of arguments.
1865 /// InputIterator must be a random-access iterator pointing to
1866 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
1867 /// made for random-accessness but not for contiguous storage as
1868 /// that would incur runtime overhead.
1869 ///
1870 /// @brief Construct an InvokeInst from a range of arguments
1871 template<typename InputIterator>
1872 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1873 InputIterator ArgBegin, InputIterator ArgEnd,
1874 const std::string &Name, BasicBlock *InsertAtEnd)
1875 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
1876 ->getElementType())->getReturnType(),
1877 Instruction::Invoke, 0, 0, InsertAtEnd) {
1878 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1879 typename std::iterator_traits<InputIterator>::iterator_category());
1880 }
Gabor Greif051a9502008-04-06 20:25:17 +00001881public:
1882 template<typename InputIterator>
1883 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1884 InputIterator ArgBegin, InputIterator ArgEnd,
1885 const std::string &Name = "", Instruction *InsertBefore = 0) {
1886 return new(ArgEnd - ArgBegin + 3) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name, InsertBefore);
1887 }
1888 template<typename InputIterator>
1889 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1890 InputIterator ArgBegin, InputIterator ArgEnd,
1891 const std::string &Name, BasicBlock *InsertAtEnd) {
1892 return new(ArgEnd - ArgBegin + 3) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name, InsertAtEnd);
1893 }
David Greenef1355a52007-08-27 19:04:21 +00001894
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001895 ~InvokeInst();
1896
Chris Lattnerf319e832004-10-15 23:52:05 +00001897 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001898
Chris Lattner3340ffe2005-05-06 20:26:26 +00001899 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1900 /// function call.
1901 unsigned getCallingConv() const { return SubclassData; }
1902 void setCallingConv(unsigned CC) {
1903 SubclassData = CC;
1904 }
1905
Chris Lattner041221c2008-03-13 04:33:03 +00001906 /// getParamAttrs - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00001907 ///
1908 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00001909
Chris Lattner041221c2008-03-13 04:33:03 +00001910 /// setParamAttrs - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00001911 ///
1912 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00001913
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001914 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001915 bool paramHasAttr(unsigned i, ParameterAttributes attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001916
Dale Johannesen08e78b12008-02-22 17:49:45 +00001917 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001918 unsigned getParamAlignment(unsigned i) const {
1919 return ParamAttrs.getParamAlignment(i);
1920 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001921
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001922 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001923 bool doesNotAccessMemory() const {
1924 return paramHasAttr(0, ParamAttr::ReadNone);
1925 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001926
1927 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001928 bool onlyReadsMemory() const {
1929 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
1930 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001931
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001932 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001933 bool doesNotReturn() const {
1934 return paramHasAttr(0, ParamAttr::NoReturn);
1935 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001936
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001937 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001938 bool doesNotThrow() const {
1939 return paramHasAttr(0, ParamAttr::NoUnwind);
1940 }
Duncan Sandsf0c33542007-12-19 21:13:37 +00001941 void setDoesNotThrow(bool doesNotThrow = true);
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001942
Devang Patel41e23972008-03-03 21:46:28 +00001943 /// @brief Determine if the call returns a structure through first
1944 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001945 bool hasStructRetAttr() const {
1946 // Be friendly and also check the callee.
1947 return paramHasAttr(1, ParamAttr::StructRet);
1948 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00001949
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001950 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00001951 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001952 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001953 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001954 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001955 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001956
1957 // getCalledValue - Get a pointer to a function that is invoked by this inst.
Devang Patel4d4a5e02008-02-23 01:11:02 +00001958 Value *getCalledValue() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001959
1960 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00001961 BasicBlock *getNormalDest() const {
1962 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001963 }
Chris Lattner454928e2005-01-29 00:31:36 +00001964 BasicBlock *getUnwindDest() const {
1965 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001966 }
Chris Lattner454928e2005-01-29 00:31:36 +00001967 void setNormalDest(BasicBlock *B) {
1968 setOperand(1, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001969 }
1970
Chris Lattner454928e2005-01-29 00:31:36 +00001971 void setUnwindDest(BasicBlock *B) {
1972 setOperand(2, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001973 }
1974
Devang Patel4d4a5e02008-02-23 01:11:02 +00001975 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001976 assert(i < 2 && "Successor # out of range for invoke!");
1977 return i == 0 ? getNormalDest() : getUnwindDest();
1978 }
1979
Chris Lattner454928e2005-01-29 00:31:36 +00001980 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001981 assert(idx < 2 && "Successor # out of range for invoke!");
Chris Lattner454928e2005-01-29 00:31:36 +00001982 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001983 }
1984
Chris Lattner454928e2005-01-29 00:31:36 +00001985 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001986
1987 // Methods for support type inquiry through isa, cast, and dyn_cast:
1988 static inline bool classof(const InvokeInst *) { return true; }
1989 static inline bool classof(const Instruction *I) {
1990 return (I->getOpcode() == Instruction::Invoke);
1991 }
1992 static inline bool classof(const Value *V) {
1993 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1994 }
Chris Lattner454928e2005-01-29 00:31:36 +00001995private:
1996 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1997 virtual unsigned getNumSuccessorsV() const;
1998 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001999};
2000
2001
2002//===----------------------------------------------------------------------===//
2003// UnwindInst Class
2004//===----------------------------------------------------------------------===//
2005
2006//===---------------------------------------------------------------------------
2007/// UnwindInst - Immediately exit the current function, unwinding the stack
2008/// until an invoke instruction is found.
2009///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002010class UnwindInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002011 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002012public:
Gabor Greif051a9502008-04-06 20:25:17 +00002013 // allocate space for exactly zero operands
2014 void *operator new(size_t s) {
2015 return User::operator new(s, 0);
2016 }
Chris Lattner910c80a2007-02-24 00:55:48 +00002017 explicit UnwindInst(Instruction *InsertBefore = 0);
2018 explicit UnwindInst(BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002019
Chris Lattnerf319e832004-10-15 23:52:05 +00002020 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002021
Chris Lattner454928e2005-01-29 00:31:36 +00002022 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002023
2024 // Methods for support type inquiry through isa, cast, and dyn_cast:
2025 static inline bool classof(const UnwindInst *) { return true; }
2026 static inline bool classof(const Instruction *I) {
2027 return I->getOpcode() == Instruction::Unwind;
2028 }
2029 static inline bool classof(const Value *V) {
2030 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2031 }
Chris Lattner454928e2005-01-29 00:31:36 +00002032private:
2033 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2034 virtual unsigned getNumSuccessorsV() const;
2035 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002036};
2037
Chris Lattner076b3f12004-10-16 18:05:54 +00002038//===----------------------------------------------------------------------===//
2039// UnreachableInst Class
2040//===----------------------------------------------------------------------===//
2041
2042//===---------------------------------------------------------------------------
2043/// UnreachableInst - This function has undefined behavior. In particular, the
2044/// presence of this instruction indicates some higher level knowledge that the
2045/// end of the block cannot be reached.
2046///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002047class UnreachableInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002048 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002049public:
Gabor Greif051a9502008-04-06 20:25:17 +00002050 // allocate space for exactly zero operands
2051 void *operator new(size_t s) {
2052 return User::operator new(s, 0);
2053 }
Chris Lattner910c80a2007-02-24 00:55:48 +00002054 explicit UnreachableInst(Instruction *InsertBefore = 0);
2055 explicit UnreachableInst(BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00002056
2057 virtual UnreachableInst *clone() const;
2058
Chris Lattner454928e2005-01-29 00:31:36 +00002059 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00002060
2061 // Methods for support type inquiry through isa, cast, and dyn_cast:
2062 static inline bool classof(const UnreachableInst *) { return true; }
2063 static inline bool classof(const Instruction *I) {
2064 return I->getOpcode() == Instruction::Unreachable;
2065 }
2066 static inline bool classof(const Value *V) {
2067 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2068 }
Chris Lattner454928e2005-01-29 00:31:36 +00002069private:
2070 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2071 virtual unsigned getNumSuccessorsV() const;
2072 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00002073};
2074
Reid Spencer3da59db2006-11-27 01:05:10 +00002075//===----------------------------------------------------------------------===//
2076// TruncInst Class
2077//===----------------------------------------------------------------------===//
2078
2079/// @brief This class represents a truncation of integer types.
2080class TruncInst : public CastInst {
2081 /// Private copy constructor
2082 TruncInst(const TruncInst &CI)
2083 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2084 }
2085public:
2086 /// @brief Constructor with insert-before-instruction semantics
2087 TruncInst(
2088 Value *S, ///< The value to be truncated
2089 const Type *Ty, ///< The (smaller) type to truncate to
2090 const std::string &Name = "", ///< A name for the new instruction
2091 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2092 );
2093
2094 /// @brief Constructor with insert-at-end-of-block semantics
2095 TruncInst(
2096 Value *S, ///< The value to be truncated
2097 const Type *Ty, ///< The (smaller) type to truncate to
2098 const std::string &Name, ///< A name for the new instruction
2099 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2100 );
2101
2102 /// @brief Clone an identical TruncInst
2103 virtual CastInst *clone() const;
2104
2105 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2106 static inline bool classof(const TruncInst *) { return true; }
2107 static inline bool classof(const Instruction *I) {
2108 return I->getOpcode() == Trunc;
2109 }
2110 static inline bool classof(const Value *V) {
2111 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2112 }
2113};
2114
2115//===----------------------------------------------------------------------===//
2116// ZExtInst Class
2117//===----------------------------------------------------------------------===//
2118
2119/// @brief This class represents zero extension of integer types.
2120class ZExtInst : public CastInst {
2121 /// @brief Private copy constructor
2122 ZExtInst(const ZExtInst &CI)
2123 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2124 }
2125public:
2126 /// @brief Constructor with insert-before-instruction semantics
2127 ZExtInst(
2128 Value *S, ///< The value to be zero extended
2129 const Type *Ty, ///< The type to zero extend to
2130 const std::string &Name = "", ///< A name for the new instruction
2131 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2132 );
2133
2134 /// @brief Constructor with insert-at-end semantics.
2135 ZExtInst(
2136 Value *S, ///< The value to be zero extended
2137 const Type *Ty, ///< The type to zero extend to
2138 const std::string &Name, ///< A name for the new instruction
2139 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2140 );
2141
2142 /// @brief Clone an identical ZExtInst
2143 virtual CastInst *clone() const;
2144
2145 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2146 static inline bool classof(const ZExtInst *) { return true; }
2147 static inline bool classof(const Instruction *I) {
2148 return I->getOpcode() == ZExt;
2149 }
2150 static inline bool classof(const Value *V) {
2151 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2152 }
2153};
2154
2155//===----------------------------------------------------------------------===//
2156// SExtInst Class
2157//===----------------------------------------------------------------------===//
2158
2159/// @brief This class represents a sign extension of integer types.
2160class SExtInst : public CastInst {
2161 /// @brief Private copy constructor
2162 SExtInst(const SExtInst &CI)
2163 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2164 }
2165public:
2166 /// @brief Constructor with insert-before-instruction semantics
2167 SExtInst(
2168 Value *S, ///< The value to be sign extended
2169 const Type *Ty, ///< The type to sign extend to
2170 const std::string &Name = "", ///< A name for the new instruction
2171 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2172 );
2173
2174 /// @brief Constructor with insert-at-end-of-block semantics
2175 SExtInst(
2176 Value *S, ///< The value to be sign extended
2177 const Type *Ty, ///< The type to sign extend to
2178 const std::string &Name, ///< A name for the new instruction
2179 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2180 );
2181
2182 /// @brief Clone an identical SExtInst
2183 virtual CastInst *clone() const;
2184
2185 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2186 static inline bool classof(const SExtInst *) { return true; }
2187 static inline bool classof(const Instruction *I) {
2188 return I->getOpcode() == SExt;
2189 }
2190 static inline bool classof(const Value *V) {
2191 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2192 }
2193};
2194
2195//===----------------------------------------------------------------------===//
2196// FPTruncInst Class
2197//===----------------------------------------------------------------------===//
2198
2199/// @brief This class represents a truncation of floating point types.
2200class FPTruncInst : public CastInst {
2201 FPTruncInst(const FPTruncInst &CI)
2202 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2203 }
2204public:
2205 /// @brief Constructor with insert-before-instruction semantics
2206 FPTruncInst(
2207 Value *S, ///< The value to be truncated
2208 const Type *Ty, ///< The type to truncate to
2209 const std::string &Name = "", ///< A name for the new instruction
2210 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2211 );
2212
2213 /// @brief Constructor with insert-before-instruction semantics
2214 FPTruncInst(
2215 Value *S, ///< The value to be truncated
2216 const Type *Ty, ///< The type to truncate to
2217 const std::string &Name, ///< A name for the new instruction
2218 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2219 );
2220
2221 /// @brief Clone an identical FPTruncInst
2222 virtual CastInst *clone() const;
2223
2224 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2225 static inline bool classof(const FPTruncInst *) { return true; }
2226 static inline bool classof(const Instruction *I) {
2227 return I->getOpcode() == FPTrunc;
2228 }
2229 static inline bool classof(const Value *V) {
2230 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2231 }
2232};
2233
2234//===----------------------------------------------------------------------===//
2235// FPExtInst Class
2236//===----------------------------------------------------------------------===//
2237
2238/// @brief This class represents an extension of floating point types.
2239class FPExtInst : public CastInst {
2240 FPExtInst(const FPExtInst &CI)
2241 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2242 }
2243public:
2244 /// @brief Constructor with insert-before-instruction semantics
2245 FPExtInst(
2246 Value *S, ///< The value to be extended
2247 const Type *Ty, ///< The type to extend to
2248 const std::string &Name = "", ///< A name for the new instruction
2249 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2250 );
2251
2252 /// @brief Constructor with insert-at-end-of-block semantics
2253 FPExtInst(
2254 Value *S, ///< The value to be extended
2255 const Type *Ty, ///< The type to extend to
2256 const std::string &Name, ///< A name for the new instruction
2257 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2258 );
2259
2260 /// @brief Clone an identical FPExtInst
2261 virtual CastInst *clone() const;
2262
2263 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2264 static inline bool classof(const FPExtInst *) { return true; }
2265 static inline bool classof(const Instruction *I) {
2266 return I->getOpcode() == FPExt;
2267 }
2268 static inline bool classof(const Value *V) {
2269 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2270 }
2271};
2272
2273//===----------------------------------------------------------------------===//
2274// UIToFPInst Class
2275//===----------------------------------------------------------------------===//
2276
2277/// @brief This class represents a cast unsigned integer to floating point.
2278class UIToFPInst : public CastInst {
2279 UIToFPInst(const UIToFPInst &CI)
2280 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2281 }
2282public:
2283 /// @brief Constructor with insert-before-instruction semantics
2284 UIToFPInst(
2285 Value *S, ///< The value to be converted
2286 const Type *Ty, ///< The type to convert to
2287 const std::string &Name = "", ///< A name for the new instruction
2288 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2289 );
2290
2291 /// @brief Constructor with insert-at-end-of-block semantics
2292 UIToFPInst(
2293 Value *S, ///< The value to be converted
2294 const Type *Ty, ///< The type to convert to
2295 const std::string &Name, ///< A name for the new instruction
2296 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2297 );
2298
2299 /// @brief Clone an identical UIToFPInst
2300 virtual CastInst *clone() const;
2301
2302 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2303 static inline bool classof(const UIToFPInst *) { return true; }
2304 static inline bool classof(const Instruction *I) {
2305 return I->getOpcode() == UIToFP;
2306 }
2307 static inline bool classof(const Value *V) {
2308 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2309 }
2310};
2311
2312//===----------------------------------------------------------------------===//
2313// SIToFPInst Class
2314//===----------------------------------------------------------------------===//
2315
2316/// @brief This class represents a cast from signed integer to floating point.
2317class SIToFPInst : public CastInst {
2318 SIToFPInst(const SIToFPInst &CI)
2319 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2320 }
2321public:
2322 /// @brief Constructor with insert-before-instruction semantics
2323 SIToFPInst(
2324 Value *S, ///< The value to be converted
2325 const Type *Ty, ///< The type to convert to
2326 const std::string &Name = "", ///< A name for the new instruction
2327 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2328 );
2329
2330 /// @brief Constructor with insert-at-end-of-block semantics
2331 SIToFPInst(
2332 Value *S, ///< The value to be converted
2333 const Type *Ty, ///< The type to convert to
2334 const std::string &Name, ///< A name for the new instruction
2335 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2336 );
2337
2338 /// @brief Clone an identical SIToFPInst
2339 virtual CastInst *clone() const;
2340
2341 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2342 static inline bool classof(const SIToFPInst *) { return true; }
2343 static inline bool classof(const Instruction *I) {
2344 return I->getOpcode() == SIToFP;
2345 }
2346 static inline bool classof(const Value *V) {
2347 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2348 }
2349};
2350
2351//===----------------------------------------------------------------------===//
2352// FPToUIInst Class
2353//===----------------------------------------------------------------------===//
2354
2355/// @brief This class represents a cast from floating point to unsigned integer
2356class FPToUIInst : public CastInst {
2357 FPToUIInst(const FPToUIInst &CI)
2358 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2359 }
2360public:
2361 /// @brief Constructor with insert-before-instruction semantics
2362 FPToUIInst(
2363 Value *S, ///< The value to be converted
2364 const Type *Ty, ///< The type to convert to
2365 const std::string &Name = "", ///< A name for the new instruction
2366 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2367 );
2368
2369 /// @brief Constructor with insert-at-end-of-block semantics
2370 FPToUIInst(
2371 Value *S, ///< The value to be converted
2372 const Type *Ty, ///< The type to convert to
2373 const std::string &Name, ///< A name for the new instruction
2374 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2375 );
2376
2377 /// @brief Clone an identical FPToUIInst
2378 virtual CastInst *clone() const;
2379
2380 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2381 static inline bool classof(const FPToUIInst *) { return true; }
2382 static inline bool classof(const Instruction *I) {
2383 return I->getOpcode() == FPToUI;
2384 }
2385 static inline bool classof(const Value *V) {
2386 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2387 }
2388};
2389
2390//===----------------------------------------------------------------------===//
2391// FPToSIInst Class
2392//===----------------------------------------------------------------------===//
2393
2394/// @brief This class represents a cast from floating point to signed integer.
2395class FPToSIInst : public CastInst {
2396 FPToSIInst(const FPToSIInst &CI)
2397 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2398 }
2399public:
2400 /// @brief Constructor with insert-before-instruction semantics
2401 FPToSIInst(
2402 Value *S, ///< The value to be converted
2403 const Type *Ty, ///< The type to convert to
2404 const std::string &Name = "", ///< A name for the new instruction
2405 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2406 );
2407
2408 /// @brief Constructor with insert-at-end-of-block semantics
2409 FPToSIInst(
2410 Value *S, ///< The value to be converted
2411 const Type *Ty, ///< The type to convert to
2412 const std::string &Name, ///< A name for the new instruction
2413 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2414 );
2415
2416 /// @brief Clone an identical FPToSIInst
2417 virtual CastInst *clone() const;
2418
2419 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2420 static inline bool classof(const FPToSIInst *) { return true; }
2421 static inline bool classof(const Instruction *I) {
2422 return I->getOpcode() == FPToSI;
2423 }
2424 static inline bool classof(const Value *V) {
2425 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2426 }
2427};
2428
2429//===----------------------------------------------------------------------===//
2430// IntToPtrInst Class
2431//===----------------------------------------------------------------------===//
2432
2433/// @brief This class represents a cast from an integer to a pointer.
2434class IntToPtrInst : public CastInst {
2435 IntToPtrInst(const IntToPtrInst &CI)
2436 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2437 }
2438public:
2439 /// @brief Constructor with insert-before-instruction semantics
2440 IntToPtrInst(
2441 Value *S, ///< The value to be converted
2442 const Type *Ty, ///< The type to convert to
2443 const std::string &Name = "", ///< A name for the new instruction
2444 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2445 );
2446
2447 /// @brief Constructor with insert-at-end-of-block semantics
2448 IntToPtrInst(
2449 Value *S, ///< The value to be converted
2450 const Type *Ty, ///< The type to convert to
2451 const std::string &Name, ///< A name for the new instruction
2452 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2453 );
2454
2455 /// @brief Clone an identical IntToPtrInst
2456 virtual CastInst *clone() const;
2457
2458 // Methods for support type inquiry through isa, cast, and dyn_cast:
2459 static inline bool classof(const IntToPtrInst *) { return true; }
2460 static inline bool classof(const Instruction *I) {
2461 return I->getOpcode() == IntToPtr;
2462 }
2463 static inline bool classof(const Value *V) {
2464 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2465 }
2466};
2467
2468//===----------------------------------------------------------------------===//
2469// PtrToIntInst Class
2470//===----------------------------------------------------------------------===//
2471
2472/// @brief This class represents a cast from a pointer to an integer
2473class PtrToIntInst : public CastInst {
2474 PtrToIntInst(const PtrToIntInst &CI)
2475 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
2476 }
2477public:
2478 /// @brief Constructor with insert-before-instruction semantics
2479 PtrToIntInst(
2480 Value *S, ///< The value to be converted
2481 const Type *Ty, ///< The type to convert to
2482 const std::string &Name = "", ///< A name for the new instruction
2483 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2484 );
2485
2486 /// @brief Constructor with insert-at-end-of-block semantics
2487 PtrToIntInst(
2488 Value *S, ///< The value to be converted
2489 const Type *Ty, ///< The type to convert to
2490 const std::string &Name, ///< A name for the new instruction
2491 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2492 );
2493
2494 /// @brief Clone an identical PtrToIntInst
2495 virtual CastInst *clone() const;
2496
2497 // Methods for support type inquiry through isa, cast, and dyn_cast:
2498 static inline bool classof(const PtrToIntInst *) { return true; }
2499 static inline bool classof(const Instruction *I) {
2500 return I->getOpcode() == PtrToInt;
2501 }
2502 static inline bool classof(const Value *V) {
2503 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2504 }
2505};
2506
2507//===----------------------------------------------------------------------===//
2508// BitCastInst Class
2509//===----------------------------------------------------------------------===//
2510
2511/// @brief This class represents a no-op cast from one type to another.
2512class BitCastInst : public CastInst {
2513 BitCastInst(const BitCastInst &CI)
2514 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
2515 }
2516public:
2517 /// @brief Constructor with insert-before-instruction semantics
2518 BitCastInst(
2519 Value *S, ///< The value to be casted
2520 const Type *Ty, ///< The type to casted to
2521 const std::string &Name = "", ///< A name for the new instruction
2522 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2523 );
2524
2525 /// @brief Constructor with insert-at-end-of-block semantics
2526 BitCastInst(
2527 Value *S, ///< The value to be casted
2528 const Type *Ty, ///< The type to casted to
2529 const std::string &Name, ///< A name for the new instruction
2530 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2531 );
2532
2533 /// @brief Clone an identical BitCastInst
2534 virtual CastInst *clone() const;
2535
2536 // Methods for support type inquiry through isa, cast, and dyn_cast:
2537 static inline bool classof(const BitCastInst *) { return true; }
2538 static inline bool classof(const Instruction *I) {
2539 return I->getOpcode() == BitCast;
2540 }
2541 static inline bool classof(const Value *V) {
2542 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2543 }
2544};
2545
Devang Patel40a04212008-02-19 22:15:16 +00002546//===----------------------------------------------------------------------===//
2547// GetResultInst Class
2548//===----------------------------------------------------------------------===//
2549
2550/// GetResultInst - This instruction extracts individual result value from
2551/// aggregate value, where aggregate value is returned by CallInst.
2552///
Gabor Greif051a9502008-04-06 20:25:17 +00002553class GetResultInst : public /*FIXME: Unary*/Instruction {
2554 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Devang Patel23755d82008-02-20 19:10:47 +00002555 Use Aggr;
2556 unsigned Idx;
Devang Patel40a04212008-02-19 22:15:16 +00002557 GetResultInst(const GetResultInst &GRI) :
Devang Patel23755d82008-02-20 19:10:47 +00002558 Instruction(GRI.getType(), Instruction::GetResult, &Aggr, 1) {
2559 Aggr.init(GRI.Aggr, this);
2560 Idx = GRI.Idx;
Devang Patel40a04212008-02-19 22:15:16 +00002561 }
2562
2563public:
Gabor Greif051a9502008-04-06 20:25:17 +00002564 // allocate space for exactly one operand
2565 void *operator new(size_t s) {
2566 return User::operator new(s, 1);
2567 }
Devang Patel23755d82008-02-20 19:10:47 +00002568 explicit GetResultInst(Value *Aggr, unsigned index,
Devang Patel40a04212008-02-19 22:15:16 +00002569 const std::string &Name = "",
2570 Instruction *InsertBefore = 0);
2571
2572 /// isValidOperands - Return true if an getresult instruction can be
2573 /// formed with the specified operands.
Devang Patel23755d82008-02-20 19:10:47 +00002574 static bool isValidOperands(const Value *Aggr, unsigned index);
Devang Patel40a04212008-02-19 22:15:16 +00002575
2576 virtual GetResultInst *clone() const;
2577
Devang Patel4d4a5e02008-02-23 01:11:02 +00002578 Value *getAggregateValue() {
Devang Patel40a04212008-02-19 22:15:16 +00002579 return getOperand(0);
2580 }
Devang Patel2d2ae342008-02-20 18:36:16 +00002581
Devang Patel4d4a5e02008-02-23 01:11:02 +00002582 const Value *getAggregateValue() const {
Devang Patel2d2ae342008-02-20 18:36:16 +00002583 return getOperand(0);
2584 }
2585
Devang Patel4d4a5e02008-02-23 01:11:02 +00002586 unsigned getIndex() const {
Devang Patel23755d82008-02-20 19:10:47 +00002587 return Idx;
Devang Patel40a04212008-02-19 22:15:16 +00002588 }
2589
Devang Patel23755d82008-02-20 19:10:47 +00002590 unsigned getNumOperands() const { return 1; }
Devang Patel40a04212008-02-19 22:15:16 +00002591
2592 // Methods for support type inquiry through isa, cast, and dyn_cast:
2593 static inline bool classof(const GetResultInst *) { return true; }
2594 static inline bool classof(const Instruction *I) {
2595 return (I->getOpcode() == Instruction::GetResult);
2596 }
2597 static inline bool classof(const Value *V) {
2598 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2599 }
2600};
2601
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002602} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00002603
2604#endif