blob: d65657bd333e71286a5332abce92fde792c4c87c [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"
Gabor Greifefe65362008-05-10 08:32:32 +000024#include "llvm/BasicBlock.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000025
26namespace llvm {
27
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
Christopher Lamb43c7f372007-04-22 19:24:39 +0000291
Gabor Greifefe65362008-05-10 08:32:32 +0000292 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
293 &Op<0>(), 2) {
294 Op<0>().init(SI.Op<0>(), this);
295 Op<1>().init(SI.Op<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.
Gabor Greifefe65362008-05-10 08:32:32 +0000332 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner454928e2005-01-29 00:31:36 +0000333
Christopher Lamb43c7f372007-04-22 19:24:39 +0000334 /// getAlignment - Return the alignment of the access that is being performed
335 ///
336 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000337 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000338 }
339
340 void setAlignment(unsigned Align);
341
Chris Lattnerf319e832004-10-15 23:52:05 +0000342 virtual StoreInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000343
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000344 Value *getPointerOperand() { return getOperand(1); }
345 const Value *getPointerOperand() const { return getOperand(1); }
346 static unsigned getPointerOperandIndex() { return 1U; }
347
348 // Methods for support type inquiry through isa, cast, and dyn_cast:
349 static inline bool classof(const StoreInst *) { return true; }
350 static inline bool classof(const Instruction *I) {
351 return I->getOpcode() == Instruction::Store;
352 }
353 static inline bool classof(const Value *V) {
354 return isa<Instruction>(V) && classof(cast<Instruction>(V));
355 }
356};
357
Gabor Greifefe65362008-05-10 08:32:32 +0000358template <>
359struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
360};
361
362DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000363
364//===----------------------------------------------------------------------===//
365// GetElementPtrInst Class
366//===----------------------------------------------------------------------===//
367
David Greeneb8f74792007-09-04 15:46:09 +0000368// checkType - Simple wrapper function to give a better assertion failure
369// message on bad indexes for a gep instruction.
370//
371static inline const Type *checkType(const Type *Ty) {
372 assert(Ty && "Invalid GetElementPtrInst indices for type!");
373 return Ty;
374}
375
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000376/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
377/// access elements of arrays and structs
378///
379class GetElementPtrInst : public Instruction {
Gabor Greifefe65362008-05-10 08:32:32 +0000380 GetElementPtrInst(const GetElementPtrInst &GEPI);
Chris Lattner6ffbe172007-01-31 19:47:18 +0000381 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx);
Chris Lattner38bacf22005-05-03 05:43:30 +0000382 void init(Value *Ptr, Value *Idx);
David Greeneb8f74792007-09-04 15:46:09 +0000383
384 template<typename InputIterator>
385 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
386 const std::string &Name,
387 // This argument ensures that we have an iterator we can
388 // do arithmetic on in constant time
389 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000390 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
David Greeneb8f74792007-09-04 15:46:09 +0000391
392 if (NumIdx > 0) {
Gabor Greifefe65362008-05-10 08:32:32 +0000393 // This requires that the iterator points to contiguous memory.
394 init(Ptr, &*IdxBegin, NumIdx); // FIXME: for the general case
395 // we have to build an array here
David Greeneb8f74792007-09-04 15:46:09 +0000396 }
397 else {
398 init(Ptr, 0, NumIdx);
399 }
400
401 setName(Name);
402 }
403
404 /// getIndexedType - Returns the type of the element that would be loaded with
405 /// a load instruction with the specified parameters.
406 ///
407 /// A null type is returned if the indices are invalid for the specified
408 /// pointer type.
409 ///
410 static const Type *getIndexedType(const Type *Ptr,
Dan Gohman041e2eb2008-05-15 19:50:34 +0000411 Value* const *Idx, unsigned NumIdx);
David Greeneb8f74792007-09-04 15:46:09 +0000412
413 template<typename InputIterator>
414 static const Type *getIndexedType(const Type *Ptr,
415 InputIterator IdxBegin,
416 InputIterator IdxEnd,
David Greeneb8f74792007-09-04 15:46:09 +0000417 // This argument ensures that we
418 // have an iterator we can do
419 // arithmetic on in constant time
420 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000421 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
David Greeneb8f74792007-09-04 15:46:09 +0000422
Dan Gohman041e2eb2008-05-15 19:50:34 +0000423 if (NumIdx > 0)
David Greeneb8f74792007-09-04 15:46:09 +0000424 // This requires that the iterator points to contiguous memory.
Dan Gohman041e2eb2008-05-15 19:50:34 +0000425 return getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx);
426 else
427 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
David Greeneb8f74792007-09-04 15:46:09 +0000428 }
429
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000430 /// Constructors - Create a getelementptr instruction with a base pointer an
431 /// list of indices. The first ctor can optionally insert before an existing
432 /// instruction, the second appends the new instruction to the specified
433 /// BasicBlock.
David Greeneb8f74792007-09-04 15:46:09 +0000434 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000435 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
436 InputIterator IdxEnd,
437 unsigned Values,
438 const std::string &Name,
439 Instruction *InsertBefore);
David Greeneb8f74792007-09-04 15:46:09 +0000440 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000441 inline GetElementPtrInst(Value *Ptr,
442 InputIterator IdxBegin, InputIterator IdxEnd,
443 unsigned Values,
444 const std::string &Name, BasicBlock *InsertAtEnd);
David Greeneb8f74792007-09-04 15:46:09 +0000445
Chris Lattner38bacf22005-05-03 05:43:30 +0000446 /// Constructors - These two constructors are convenience methods because one
447 /// and two index getelementptr instructions are so common.
Gabor Greifefe65362008-05-10 08:32:32 +0000448 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &Name = "",
449 Instruction *InsertBefore = 0);
Chris Lattner38bacf22005-05-03 05:43:30 +0000450 GetElementPtrInst(Value *Ptr, Value *Idx,
451 const std::string &Name, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000452public:
453 template<typename InputIterator>
454 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
455 InputIterator IdxEnd,
456 const std::string &Name = "",
457 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +0000458 typename std::iterator_traits<InputIterator>::difference_type Values =
459 1 + std::distance(IdxBegin, IdxEnd);
460 return new(Values)
461 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, Name, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000462 }
463 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000464 static GetElementPtrInst *Create(Value *Ptr,
465 InputIterator IdxBegin, InputIterator IdxEnd,
466 const std::string &Name,
467 BasicBlock *InsertAtEnd) {
468 typename std::iterator_traits<InputIterator>::difference_type Values =
469 1 + std::distance(IdxBegin, IdxEnd);
470 return new(Values)
471 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, Name, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000472 }
473
Gabor Greifefe65362008-05-10 08:32:32 +0000474 /// Constructors - These two creators are convenience methods because one
475 /// index getelementptr instructions are so common.
Gabor Greif051a9502008-04-06 20:25:17 +0000476 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Gabor Greifefe65362008-05-10 08:32:32 +0000477 const std::string &Name = "",
478 Instruction *InsertBefore = 0) {
479 return new(2) GetElementPtrInst(Ptr, Idx, Name, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000480 }
481 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Gabor Greifefe65362008-05-10 08:32:32 +0000482 const std::string &Name,
483 BasicBlock *InsertAtEnd) {
484 return new(2) GetElementPtrInst(Ptr, Idx, Name, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000485 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000486
Chris Lattnerf319e832004-10-15 23:52:05 +0000487 virtual GetElementPtrInst *clone() const;
Misha Brukman9769ab22005-04-21 20:19:05 +0000488
Gabor Greifefe65362008-05-10 08:32:32 +0000489 /// Transparently provide more efficient getOperand methods.
490 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
491
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000492 // getType - Overload to return most specific pointer type...
Devang Patel4d4a5e02008-02-23 01:11:02 +0000493 const PointerType *getType() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000494 return reinterpret_cast<const PointerType*>(Instruction::getType());
495 }
496
497 /// getIndexedType - Returns the type of the element that would be loaded with
498 /// a load instruction with the specified parameters.
499 ///
Misha Brukman9769ab22005-04-21 20:19:05 +0000500 /// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000501 /// pointer type.
502 ///
David Greeneb8f74792007-09-04 15:46:09 +0000503 template<typename InputIterator>
Misha Brukman9769ab22005-04-21 20:19:05 +0000504 static const Type *getIndexedType(const Type *Ptr,
David Greeneb8f74792007-09-04 15:46:09 +0000505 InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +0000506 InputIterator IdxEnd) {
507 return getIndexedType(Ptr, IdxBegin, IdxEnd,
David Greeneb8f74792007-09-04 15:46:09 +0000508 typename std::iterator_traits<InputIterator>::
Dan Gohman041e2eb2008-05-15 19:50:34 +0000509 iterator_category());
David Greeneb8f74792007-09-04 15:46:09 +0000510 }
Chris Lattner38bacf22005-05-03 05:43:30 +0000511 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
Misha Brukman9769ab22005-04-21 20:19:05 +0000512
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000513 inline op_iterator idx_begin() { return op_begin()+1; }
514 inline const_op_iterator idx_begin() const { return op_begin()+1; }
515 inline op_iterator idx_end() { return op_end(); }
516 inline const_op_iterator idx_end() const { return op_end(); }
517
518 Value *getPointerOperand() {
519 return getOperand(0);
520 }
521 const Value *getPointerOperand() const {
522 return getOperand(0);
523 }
524 static unsigned getPointerOperandIndex() {
525 return 0U; // get index for modifying correct operand
526 }
527
Devang Patel4d4a5e02008-02-23 01:11:02 +0000528 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000529 return getNumOperands() - 1;
530 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000531
Devang Patel4d4a5e02008-02-23 01:11:02 +0000532 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000533 return getNumOperands() > 1;
534 }
Chris Lattner6f771d42007-04-14 00:12:57 +0000535
536 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
537 /// zeros. If so, the result pointer and the first operand have the same
538 /// value, just potentially different types.
539 bool hasAllZeroIndices() const;
Chris Lattner6b0974c2007-04-27 20:35:56 +0000540
541 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
542 /// constant integers. If so, the result pointer and the first operand have
543 /// a constant offset between them.
544 bool hasAllConstantIndices() const;
545
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000546
547 // Methods for support type inquiry through isa, cast, and dyn_cast:
548 static inline bool classof(const GetElementPtrInst *) { return true; }
549 static inline bool classof(const Instruction *I) {
550 return (I->getOpcode() == Instruction::GetElementPtr);
551 }
552 static inline bool classof(const Value *V) {
553 return isa<Instruction>(V) && classof(cast<Instruction>(V));
554 }
555};
556
Gabor Greifefe65362008-05-10 08:32:32 +0000557template <>
558struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
559};
560
561template<typename InputIterator>
562GetElementPtrInst::GetElementPtrInst(Value *Ptr,
563 InputIterator IdxBegin,
564 InputIterator IdxEnd,
565 unsigned Values,
566 const std::string &Name,
567 Instruction *InsertBefore)
568 : Instruction(PointerType::get(checkType(
569 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000570 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000571 cast<PointerType>(Ptr->getType())
572 ->getAddressSpace()),
573 GetElementPtr,
574 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
575 Values, InsertBefore) {
576 init(Ptr, IdxBegin, IdxEnd, Name,
577 typename std::iterator_traits<InputIterator>::iterator_category());
578}
579template<typename InputIterator>
580GetElementPtrInst::GetElementPtrInst(Value *Ptr,
581 InputIterator IdxBegin,
582 InputIterator IdxEnd,
583 unsigned Values,
584 const std::string &Name,
585 BasicBlock *InsertAtEnd)
586 : Instruction(PointerType::get(checkType(
587 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000588 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000589 cast<PointerType>(Ptr->getType())
590 ->getAddressSpace()),
591 GetElementPtr,
592 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
593 Values, InsertAtEnd) {
594 init(Ptr, IdxBegin, IdxEnd, Name,
595 typename std::iterator_traits<InputIterator>::iterator_category());
596}
597
598
599DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
600
601
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000602//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000603// ICmpInst Class
604//===----------------------------------------------------------------------===//
605
606/// This instruction compares its operands according to the predicate given
Nate Begemanac80ade2008-05-12 19:01:56 +0000607/// to the constructor. It only operates on integers or pointers. The operands
608/// must be identical types.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000609/// @brief Represent an integer comparison operator.
610class ICmpInst: public CmpInst {
611public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000612 /// @brief Constructor with insert-before-instruction semantics.
613 ICmpInst(
614 Predicate pred, ///< The predicate to use for the comparison
615 Value *LHS, ///< The left-hand-side of the expression
616 Value *RHS, ///< The right-hand-side of the expression
617 const std::string &Name = "", ///< Name of the instruction
618 Instruction *InsertBefore = 0 ///< Where to insert
Nate Begemanac80ade2008-05-12 19:01:56 +0000619 ) : CmpInst(Type::Int1Ty, Instruction::ICmp, pred, LHS, RHS, Name,
620 InsertBefore) {
621 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
622 pred <= CmpInst::LAST_ICMP_PREDICATE &&
623 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000624 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000625 "Both operands to ICmp instruction are not of the same type!");
626 // Check that the operands are the right type
Nate Begeman31cd33a2008-05-14 20:28:31 +0000627 assert((getOperand(0)->getType()->isInteger() ||
628 isa<PointerType>(getOperand(0)->getType())) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000629 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000630 }
631
632 /// @brief Constructor with insert-at-block-end semantics.
633 ICmpInst(
634 Predicate pred, ///< The predicate to use for the comparison
635 Value *LHS, ///< The left-hand-side of the expression
636 Value *RHS, ///< The right-hand-side of the expression
637 const std::string &Name, ///< Name of the instruction
638 BasicBlock *InsertAtEnd ///< Block to insert into.
Nate Begemanac80ade2008-05-12 19:01:56 +0000639 ) : CmpInst(Type::Int1Ty, Instruction::ICmp, pred, LHS, RHS, Name,
640 InsertAtEnd) {
641 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
642 pred <= CmpInst::LAST_ICMP_PREDICATE &&
643 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000644 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000645 "Both operands to ICmp instruction are not of the same type!");
646 // Check that the operands are the right type
Nate Begeman31cd33a2008-05-14 20:28:31 +0000647 assert((getOperand(0)->getType()->isInteger() ||
648 isa<PointerType>(getOperand(0)->getType())) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000649 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000650 }
651
652 /// @brief Return the predicate for this instruction.
653 Predicate getPredicate() const { return Predicate(SubclassData); }
654
Chris Lattnerb769d562007-01-14 19:41:24 +0000655 /// @brief Set the predicate for this instruction to the specified value.
656 void setPredicate(Predicate P) { SubclassData = P; }
657
Reid Spencer45fb3f32006-11-20 01:22:35 +0000658 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
659 /// @returns the inverse predicate for the instruction's current predicate.
660 /// @brief Return the inverse of the instruction's predicate.
661 Predicate getInversePredicate() const {
662 return getInversePredicate(getPredicate());
663 }
664
665 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
666 /// @returns the inverse predicate for predicate provided in \p pred.
667 /// @brief Return the inverse of a given predicate
668 static Predicate getInversePredicate(Predicate pred);
669
670 /// For example, EQ->EQ, SLE->SGE, ULT->UGT, etc.
671 /// @returns the predicate that would be the result of exchanging the two
672 /// operands of the ICmpInst instruction without changing the result
673 /// produced.
674 /// @brief Return the predicate as if the operands were swapped
675 Predicate getSwappedPredicate() const {
676 return getSwappedPredicate(getPredicate());
677 }
678
679 /// This is a static version that you can use without an instruction
680 /// available.
681 /// @brief Return the predicate as if the operands were swapped.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000682 static Predicate getSwappedPredicate(Predicate pred);
683
684 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
685 /// @returns the predicate that would be the result if the operand were
686 /// regarded as signed.
687 /// @brief Return the signed version of the predicate
688 Predicate getSignedPredicate() const {
689 return getSignedPredicate(getPredicate());
690 }
691
692 /// This is a static version that you can use without an instruction.
693 /// @brief Return the signed version of the predicate.
694 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000695
Nick Lewycky4189a532008-01-28 03:48:02 +0000696 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
697 /// @returns the predicate that would be the result if the operand were
698 /// regarded as unsigned.
699 /// @brief Return the unsigned version of the predicate
700 Predicate getUnsignedPredicate() const {
701 return getUnsignedPredicate(getPredicate());
702 }
703
704 /// This is a static version that you can use without an instruction.
705 /// @brief Return the unsigned version of the predicate.
706 static Predicate getUnsignedPredicate(Predicate pred);
707
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000708 /// isEquality - Return true if this predicate is either EQ or NE. This also
709 /// tests for commutativity.
710 static bool isEquality(Predicate P) {
711 return P == ICMP_EQ || P == ICMP_NE;
712 }
713
714 /// isEquality - Return true if this predicate is either EQ or NE. This also
715 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000716 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000717 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000718 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000719
720 /// @returns true if the predicate of this ICmpInst is commutative
721 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000722 bool isCommutative() const { return isEquality(); }
723
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000724 /// isRelational - Return true if the predicate is relational (not EQ or NE).
725 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +0000726 bool isRelational() const {
727 return !isEquality();
728 }
729
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000730 /// isRelational - Return true if the predicate is relational (not EQ or NE).
731 ///
732 static bool isRelational(Predicate P) {
733 return !isEquality(P);
734 }
735
Reid Spencere4d87aa2006-12-23 06:05:41 +0000736 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
737 /// @brief Determine if this instruction's predicate is signed.
Chris Lattner5bda9e42007-09-15 06:51:03 +0000738 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000739
740 /// @returns true if the predicate provided is signed, false otherwise
741 /// @brief Determine if the predicate is signed.
742 static bool isSignedPredicate(Predicate pred);
743
Reid Spencer3da43842007-02-28 22:00:54 +0000744 /// Initialize a set of values that all satisfy the predicate with C.
745 /// @brief Make a ConstantRange for a relation with a constant value.
746 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
747
Reid Spencer45fb3f32006-11-20 01:22:35 +0000748 /// Exchange the two operands to this instruction in such a way that it does
749 /// not modify the semantics of the instruction. The predicate value may be
750 /// changed to retain the same result if the predicate is order dependent
751 /// (e.g. ult).
752 /// @brief Swap operands and adjust predicate.
753 void swapOperands() {
754 SubclassData = getSwappedPredicate();
Gabor Greif94fb68b2008-05-13 22:51:52 +0000755 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000756 }
757
Chris Lattnercd406fe2007-08-24 20:48:18 +0000758 virtual ICmpInst *clone() const;
759
Reid Spencer45fb3f32006-11-20 01:22:35 +0000760 // Methods for support type inquiry through isa, cast, and dyn_cast:
761 static inline bool classof(const ICmpInst *) { return true; }
762 static inline bool classof(const Instruction *I) {
763 return I->getOpcode() == Instruction::ICmp;
764 }
765 static inline bool classof(const Value *V) {
766 return isa<Instruction>(V) && classof(cast<Instruction>(V));
767 }
768};
769
770//===----------------------------------------------------------------------===//
771// FCmpInst Class
772//===----------------------------------------------------------------------===//
773
774/// This instruction compares its operands according to the predicate given
775/// to the constructor. It only operates on floating point values or packed
776/// vectors of floating point values. The operands must be identical types.
777/// @brief Represents a floating point comparison operator.
778class FCmpInst: public CmpInst {
779public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000780 /// @brief Constructor with insert-before-instruction semantics.
781 FCmpInst(
782 Predicate pred, ///< The predicate to use for the comparison
783 Value *LHS, ///< The left-hand-side of the expression
784 Value *RHS, ///< The right-hand-side of the expression
785 const std::string &Name = "", ///< Name of the instruction
786 Instruction *InsertBefore = 0 ///< Where to insert
Nate Begemanac80ade2008-05-12 19:01:56 +0000787 ) : CmpInst(Type::Int1Ty, Instruction::FCmp, pred, LHS, RHS, Name,
788 InsertBefore) {
789 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
790 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000791 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000792 "Both operands to FCmp instruction are not of the same type!");
793 // Check that the operands are the right type
Nate Begeman31cd33a2008-05-14 20:28:31 +0000794 assert(getOperand(0)->getType()->isFloatingPoint() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000795 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000796 }
797
798 /// @brief Constructor with insert-at-block-end semantics.
799 FCmpInst(
800 Predicate pred, ///< The predicate to use for the comparison
801 Value *LHS, ///< The left-hand-side of the expression
802 Value *RHS, ///< The right-hand-side of the expression
803 const std::string &Name, ///< Name of the instruction
804 BasicBlock *InsertAtEnd ///< Block to insert into.
Nate Begemanac80ade2008-05-12 19:01:56 +0000805 ) : CmpInst(Type::Int1Ty, Instruction::FCmp, pred, LHS, RHS, Name,
806 InsertAtEnd) {
807 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
808 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000809 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000810 "Both operands to FCmp instruction are not of the same type!");
811 // Check that the operands are the right type
Nate Begeman31cd33a2008-05-14 20:28:31 +0000812 assert(getOperand(0)->getType()->isFloatingPoint() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000813 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000814 }
815
816 /// @brief Return the predicate for this instruction.
817 Predicate getPredicate() const { return Predicate(SubclassData); }
818
Chris Lattnerb769d562007-01-14 19:41:24 +0000819 /// @brief Set the predicate for this instruction to the specified value.
820 void setPredicate(Predicate P) { SubclassData = P; }
821
Reid Spencer45fb3f32006-11-20 01:22:35 +0000822 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
823 /// @returns the inverse predicate for the instructions current predicate.
824 /// @brief Return the inverse of the predicate
825 Predicate getInversePredicate() const {
826 return getInversePredicate(getPredicate());
827 }
828
829 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
830 /// @returns the inverse predicate for \p pred.
831 /// @brief Return the inverse of a given predicate
832 static Predicate getInversePredicate(Predicate pred);
833
834 /// For example, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
835 /// @returns the predicate that would be the result of exchanging the two
836 /// operands of the ICmpInst instruction without changing the result
837 /// produced.
838 /// @brief Return the predicate as if the operands were swapped
839 Predicate getSwappedPredicate() const {
840 return getSwappedPredicate(getPredicate());
841 }
842
843 /// This is a static version that you can use without an instruction
844 /// available.
845 /// @brief Return the predicate as if the operands were swapped.
846 static Predicate getSwappedPredicate(Predicate Opcode);
847
848 /// This also tests for commutativity. If isEquality() returns true then
849 /// the predicate is also commutative. Only the equality predicates are
850 /// commutative.
851 /// @returns true if the predicate of this instruction is EQ or NE.
852 /// @brief Determine if this is an equality predicate.
853 bool isEquality() const {
854 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
855 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
856 }
857 bool isCommutative() const { return isEquality(); }
858
859 /// @returns true if the predicate is relational (not EQ or NE).
860 /// @brief Determine if this a relational predicate.
861 bool isRelational() const { return !isEquality(); }
862
863 /// Exchange the two operands to this instruction in such a way that it does
864 /// not modify the semantics of the instruction. The predicate value may be
865 /// changed to retain the same result if the predicate is order dependent
866 /// (e.g. ult).
867 /// @brief Swap operands and adjust predicate.
868 void swapOperands() {
869 SubclassData = getSwappedPredicate();
Gabor Greif94fb68b2008-05-13 22:51:52 +0000870 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000871 }
872
Chris Lattnercd406fe2007-08-24 20:48:18 +0000873 virtual FCmpInst *clone() const;
874
Reid Spencer45fb3f32006-11-20 01:22:35 +0000875 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
876 static inline bool classof(const FCmpInst *) { return true; }
877 static inline bool classof(const Instruction *I) {
878 return I->getOpcode() == Instruction::FCmp;
879 }
880 static inline bool classof(const Value *V) {
881 return isa<Instruction>(V) && classof(cast<Instruction>(V));
882 }
883};
884
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000885//===----------------------------------------------------------------------===//
Nate Begemanac80ade2008-05-12 19:01:56 +0000886// VICmpInst Class
887//===----------------------------------------------------------------------===//
888
889/// This instruction compares its operands according to the predicate given
890/// to the constructor. It only operates on vectors of integers.
891/// The operands must be identical types.
892/// @brief Represents a vector integer comparison operator.
893class VICmpInst: public CmpInst {
894public:
895 /// @brief Constructor with insert-before-instruction semantics.
896 VICmpInst(
897 Predicate pred, ///< The predicate to use for the comparison
898 Value *LHS, ///< The left-hand-side of the expression
899 Value *RHS, ///< The right-hand-side of the expression
900 const std::string &Name = "", ///< Name of the instruction
901 Instruction *InsertBefore = 0 ///< Where to insert
902 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, Name,
903 InsertBefore) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000904 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
905 pred <= CmpInst::LAST_ICMP_PREDICATE &&
906 "Invalid VICmp predicate value");
907 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
908 "Both operands to VICmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000909 }
910
911 /// @brief Constructor with insert-at-block-end semantics.
912 VICmpInst(
913 Predicate pred, ///< The predicate to use for the comparison
914 Value *LHS, ///< The left-hand-side of the expression
915 Value *RHS, ///< The right-hand-side of the expression
916 const std::string &Name, ///< Name of the instruction
917 BasicBlock *InsertAtEnd ///< Block to insert into.
918 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, Name,
919 InsertAtEnd) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000920 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
921 pred <= CmpInst::LAST_ICMP_PREDICATE &&
922 "Invalid VICmp predicate value");
923 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
924 "Both operands to VICmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000925 }
926
927 /// @brief Return the predicate for this instruction.
928 Predicate getPredicate() const { return Predicate(SubclassData); }
929
930 virtual VICmpInst *clone() const;
931
932 // Methods for support type inquiry through isa, cast, and dyn_cast:
933 static inline bool classof(const VICmpInst *) { return true; }
934 static inline bool classof(const Instruction *I) {
935 return I->getOpcode() == Instruction::VICmp;
936 }
937 static inline bool classof(const Value *V) {
938 return isa<Instruction>(V) && classof(cast<Instruction>(V));
939 }
940};
941
942//===----------------------------------------------------------------------===//
943// VFCmpInst Class
944//===----------------------------------------------------------------------===//
945
946/// This instruction compares its operands according to the predicate given
947/// to the constructor. It only operates on vectors of floating point values.
948/// The operands must be identical types.
949/// @brief Represents a vector floating point comparison operator.
950class VFCmpInst: public CmpInst {
951public:
952 /// @brief Constructor with insert-before-instruction semantics.
953 VFCmpInst(
954 Predicate pred, ///< The predicate to use for the comparison
955 Value *LHS, ///< The left-hand-side of the expression
956 Value *RHS, ///< The right-hand-side of the expression
957 const std::string &Name = "", ///< Name of the instruction
958 Instruction *InsertBefore = 0 ///< Where to insert
959 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
960 Instruction::VFCmp, pred, LHS, RHS, Name, InsertBefore) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000961 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
962 "Invalid VFCmp predicate value");
963 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
964 "Both operands to VFCmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000965 }
966
967 /// @brief Constructor with insert-at-block-end semantics.
968 VFCmpInst(
969 Predicate pred, ///< The predicate to use for the comparison
970 Value *LHS, ///< The left-hand-side of the expression
971 Value *RHS, ///< The right-hand-side of the expression
972 const std::string &Name, ///< Name of the instruction
973 BasicBlock *InsertAtEnd ///< Block to insert into.
974 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
975 Instruction::VFCmp, pred, LHS, RHS, Name, InsertAtEnd) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000976 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
977 "Invalid VFCmp predicate value");
978 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
979 "Both operands to VFCmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000980 }
981
982 /// @brief Return the predicate for this instruction.
983 Predicate getPredicate() const { return Predicate(SubclassData); }
984
985 virtual VFCmpInst *clone() const;
986
987 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
988 static inline bool classof(const VFCmpInst *) { return true; }
989 static inline bool classof(const Instruction *I) {
990 return I->getOpcode() == Instruction::VFCmp;
991 }
992 static inline bool classof(const Value *V) {
993 return isa<Instruction>(V) && classof(cast<Instruction>(V));
994 }
995};
996
997//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000998// CallInst Class
999//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001000/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +00001001/// machine's calling convention. This class uses low bit of the SubClassData
1002/// field to indicate whether or not this is a tail call. The rest of the bits
1003/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001004///
David Greene52eec542007-08-01 03:43:44 +00001005
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001006class CallInst : public Instruction {
Chris Lattner58d74912008-03-12 17:45:29 +00001007 PAListPtr ParamAttrs; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001008 CallInst(const CallInst &CI);
Chris Lattnerd54f4322007-02-13 00:58:44 +00001009 void init(Value *Func, Value* const *Params, unsigned NumParams);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001010 void init(Value *Func, Value *Actual1, Value *Actual2);
1011 void init(Value *Func, Value *Actual);
1012 void init(Value *Func);
1013
David Greene52eec542007-08-01 03:43:44 +00001014 template<typename InputIterator>
1015 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1016 const std::string &Name,
1017 // This argument ensures that we have an iterator we can
1018 // do arithmetic on in constant time
1019 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00001020 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greene52eec542007-08-01 03:43:44 +00001021
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00001022 // This requires that the iterator points to contiguous memory.
1023 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
David Greene52eec542007-08-01 03:43:44 +00001024 setName(Name);
1025 }
1026
David Greene52eec542007-08-01 03:43:44 +00001027 /// Construct a CallInst given a range of arguments. InputIterator
1028 /// must be a random-access iterator pointing to contiguous storage
1029 /// (e.g. a std::vector<>::iterator). Checks are made for
1030 /// random-accessness but not for contiguous storage as that would
1031 /// incur runtime overhead.
1032 /// @brief Construct a CallInst from a range of arguments
1033 template<typename InputIterator>
1034 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Gabor Greifefe65362008-05-10 08:32:32 +00001035 const std::string &Name, Instruction *InsertBefore);
David Greene52eec542007-08-01 03:43:44 +00001036
1037 /// Construct a CallInst given a range of arguments. InputIterator
1038 /// must be a random-access iterator pointing to contiguous storage
1039 /// (e.g. a std::vector<>::iterator). Checks are made for
1040 /// random-accessness but not for contiguous storage as that would
1041 /// incur runtime overhead.
1042 /// @brief Construct a CallInst from a range of arguments
1043 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00001044 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1045 const std::string &Name, BasicBlock *InsertAtEnd);
David Greene52eec542007-08-01 03:43:44 +00001046
Gabor Greifefe65362008-05-10 08:32:32 +00001047 CallInst(Value *F, Value *Actual, const std::string& Name,
1048 Instruction *InsertBefore);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001049 CallInst(Value *F, Value *Actual, const std::string& Name,
1050 BasicBlock *InsertAtEnd);
Gabor Greifefe65362008-05-10 08:32:32 +00001051 explicit CallInst(Value *F, const std::string &Name,
1052 Instruction *InsertBefore);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001053 CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001054public:
1055 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00001056 static CallInst *Create(Value *Func,
1057 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001058 const std::string &Name = "",
1059 Instruction *InsertBefore = 0) {
Bill Wendlingc2e73532008-05-10 19:59:59 +00001060 return new((unsigned)(ArgEnd - ArgBegin + 1))
Evan Chengd69bb1a2008-05-05 17:41:03 +00001061 CallInst(Func, ArgBegin, ArgEnd, Name, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001062 }
1063 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00001064 static CallInst *Create(Value *Func,
1065 InputIterator ArgBegin, InputIterator ArgEnd,
1066 const std::string &Name, BasicBlock *InsertAtEnd) {
Bill Wendlingc2e73532008-05-10 19:59:59 +00001067 return new((unsigned)(ArgEnd - ArgBegin + 1))
Evan Chengd69bb1a2008-05-05 17:41:03 +00001068 CallInst(Func, ArgBegin, ArgEnd, Name, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001069 }
1070 static CallInst *Create(Value *F, Value *Actual, const std::string& Name = "",
1071 Instruction *InsertBefore = 0) {
1072 return new(2) CallInst(F, Actual, Name, InsertBefore);
1073 }
1074 static CallInst *Create(Value *F, Value *Actual, const std::string& Name,
1075 BasicBlock *InsertAtEnd) {
1076 return new(2) CallInst(F, Actual, Name, InsertAtEnd);
1077 }
1078 static CallInst *Create(Value *F, const std::string &Name = "",
1079 Instruction *InsertBefore = 0) {
1080 return new(1) CallInst(F, Name, InsertBefore);
1081 }
Evan Cheng34cd4a42008-05-05 18:30:58 +00001082 static CallInst *Create(Value *F, const std::string &Name,
1083 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001084 return new(1) CallInst(F, Name, InsertAtEnd);
1085 }
1086
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001087 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001088
Chris Lattnerf319e832004-10-15 23:52:05 +00001089 virtual CallInst *clone() const;
Gabor Greifefe65362008-05-10 08:32:32 +00001090
1091 /// Provide fast operand accessors
1092 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattnerbb5493d2007-02-15 23:15:00 +00001093
Chris Lattner3340ffe2005-05-06 20:26:26 +00001094 bool isTailCall() const { return SubclassData & 1; }
1095 void setTailCall(bool isTailCall = true) {
Jeff Cohen39cef602005-05-07 02:44:04 +00001096 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
Chris Lattner3340ffe2005-05-06 20:26:26 +00001097 }
1098
1099 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1100 /// function call.
1101 unsigned getCallingConv() const { return SubclassData >> 1; }
1102 void setCallingConv(unsigned CC) {
1103 SubclassData = (SubclassData & 1) | (CC << 1);
1104 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00001105
Chris Lattner041221c2008-03-13 04:33:03 +00001106 /// getParamAttrs - Return the parameter attributes for this call.
1107 ///
Chris Lattner58d74912008-03-12 17:45:29 +00001108 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001109
Chris Lattner041221c2008-03-13 04:33:03 +00001110 /// setParamAttrs - Sets the parameter attributes for this call.
Chris Lattner58d74912008-03-12 17:45:29 +00001111 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00001112
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001113 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001114 bool paramHasAttr(unsigned i, unsigned attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001115
Dale Johannesen08e78b12008-02-22 17:49:45 +00001116 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001117 unsigned getParamAlignment(unsigned i) const {
1118 return ParamAttrs.getParamAlignment(i);
1119 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001120
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001121 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001122 bool doesNotAccessMemory() const {
1123 return paramHasAttr(0, ParamAttr::ReadNone);
1124 }
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001125
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001126 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001127 bool onlyReadsMemory() const {
1128 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
1129 }
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001130
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001131 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001132 bool doesNotReturn() const {
1133 return paramHasAttr(0, ParamAttr::NoReturn);
1134 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001135
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001136 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001137 bool doesNotThrow() const {
1138 return paramHasAttr(0, ParamAttr::NoUnwind);
1139 }
Duncan Sandsf0c33542007-12-19 21:13:37 +00001140 void setDoesNotThrow(bool doesNotThrow = true);
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001141
Devang Patel41e23972008-03-03 21:46:28 +00001142 /// @brief Determine if the call returns a structure through first
1143 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001144 bool hasStructRetAttr() const {
1145 // Be friendly and also check the callee.
1146 return paramHasAttr(1, ParamAttr::StructRet);
1147 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001148
Evan Chengf4a54982008-01-12 18:57:32 +00001149 /// @brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001150 bool hasByValArgument() const {
1151 return ParamAttrs.hasAttrSomewhere(ParamAttr::ByVal);
1152 }
Evan Chengf4a54982008-01-12 18:57:32 +00001153
Chris Lattner721aef62004-11-18 17:46:57 +00001154 /// getCalledFunction - Return the function being called by this instruction
1155 /// if it is a direct call. If it is a call through a function pointer,
1156 /// return null.
1157 Function *getCalledFunction() const {
Dan Gohman11a7dbf2007-09-24 15:46:02 +00001158 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001159 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001160
Reid Spencerc25ec252006-12-29 04:10:59 +00001161 /// getCalledValue - Get a pointer to the function that is invoked by this
1162 /// instruction
Devang Patel4d4a5e02008-02-23 01:11:02 +00001163 const Value *getCalledValue() const { return getOperand(0); }
1164 Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001165
1166 // Methods for support type inquiry through isa, cast, and dyn_cast:
1167 static inline bool classof(const CallInst *) { return true; }
1168 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001169 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001170 }
1171 static inline bool classof(const Value *V) {
1172 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1173 }
1174};
1175
Gabor Greifefe65362008-05-10 08:32:32 +00001176template <>
1177struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1178};
1179
1180template<typename InputIterator>
1181CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1182 const std::string &Name, BasicBlock *InsertAtEnd)
1183 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1184 ->getElementType())->getReturnType(),
1185 Instruction::Call,
1186 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001187 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00001188 init(Func, ArgBegin, ArgEnd, Name,
1189 typename std::iterator_traits<InputIterator>::iterator_category());
1190}
1191
1192template<typename InputIterator>
1193CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1194 const std::string &Name, Instruction *InsertBefore)
1195 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1196 ->getElementType())->getReturnType(),
1197 Instruction::Call,
1198 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001199 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
Gabor Greifefe65362008-05-10 08:32:32 +00001200 init(Func, ArgBegin, ArgEnd, Name,
1201 typename std::iterator_traits<InputIterator>::iterator_category());
1202}
1203
1204DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1205
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001206//===----------------------------------------------------------------------===//
1207// SelectInst Class
1208//===----------------------------------------------------------------------===//
1209
1210/// SelectInst - This class represents the LLVM 'select' instruction.
1211///
1212class SelectInst : public Instruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001213 void init(Value *C, Value *S1, Value *S2) {
Gabor Greifefe65362008-05-10 08:32:32 +00001214 Op<0>() = C;
1215 Op<1>() = S1;
1216 Op<2>() = S2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001217 }
1218
Chris Lattner454928e2005-01-29 00:31:36 +00001219 SelectInst(const SelectInst &SI)
Gabor Greifefe65362008-05-10 08:32:32 +00001220 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1221 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
Chris Lattner454928e2005-01-29 00:31:36 +00001222 }
Gabor Greifefe65362008-05-10 08:32:32 +00001223 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1224 Instruction *InsertBefore)
1225 : Instruction(S1->getType(), Instruction::Select,
1226 &Op<0>(), 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001227 init(C, S1, S2);
Chris Lattner910c80a2007-02-24 00:55:48 +00001228 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001229 }
1230 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1231 BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001232 : Instruction(S1->getType(), Instruction::Select,
1233 &Op<0>(), 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001234 init(C, S1, S2);
Chris Lattner910c80a2007-02-24 00:55:48 +00001235 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001236 }
Gabor Greif051a9502008-04-06 20:25:17 +00001237public:
Evan Chengd69bb1a2008-05-05 17:41:03 +00001238 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1239 const std::string &Name = "",
1240 Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00001241 return new(3) SelectInst(C, S1, S2, Name, InsertBefore);
1242 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001243 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1244 const std::string &Name, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001245 return new(3) SelectInst(C, S1, S2, Name, InsertAtEnd);
1246 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001247
Gabor Greifefe65362008-05-10 08:32:32 +00001248 Value *getCondition() const { return Op<0>(); }
1249 Value *getTrueValue() const { return Op<1>(); }
1250 Value *getFalseValue() const { return Op<2>(); }
Chris Lattner454928e2005-01-29 00:31:36 +00001251
1252 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001253 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001254
1255 OtherOps getOpcode() const {
1256 return static_cast<OtherOps>(Instruction::getOpcode());
1257 }
1258
Chris Lattnerf319e832004-10-15 23:52:05 +00001259 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001260
1261 // Methods for support type inquiry through isa, cast, and dyn_cast:
1262 static inline bool classof(const SelectInst *) { return true; }
1263 static inline bool classof(const Instruction *I) {
1264 return I->getOpcode() == Instruction::Select;
1265 }
1266 static inline bool classof(const Value *V) {
1267 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1268 }
1269};
1270
Gabor Greifefe65362008-05-10 08:32:32 +00001271template <>
1272struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1273};
1274
1275DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1276
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001277//===----------------------------------------------------------------------===//
1278// VAArgInst Class
1279//===----------------------------------------------------------------------===//
1280
1281/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001282/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001283///
Chris Lattner454928e2005-01-29 00:31:36 +00001284class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001285 VAArgInst(const VAArgInst &VAA)
Chris Lattner454928e2005-01-29 00:31:36 +00001286 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001287public:
1288 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
1289 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001290 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001291 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001292 }
1293 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
1294 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001295 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001296 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001297 }
1298
Chris Lattnerf319e832004-10-15 23:52:05 +00001299 virtual VAArgInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001300
1301 // Methods for support type inquiry through isa, cast, and dyn_cast:
1302 static inline bool classof(const VAArgInst *) { return true; }
1303 static inline bool classof(const Instruction *I) {
1304 return I->getOpcode() == VAArg;
1305 }
1306 static inline bool classof(const Value *V) {
1307 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1308 }
1309};
1310
1311//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001312// ExtractElementInst Class
1313//===----------------------------------------------------------------------===//
1314
1315/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001316/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001317///
1318class ExtractElementInst : public Instruction {
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001319 ExtractElementInst(const ExtractElementInst &EE) :
Gabor Greifefe65362008-05-10 08:32:32 +00001320 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1321 Op<0>().init(EE.Op<0>(), this);
1322 Op<1>().init(EE.Op<1>(), this);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001323 }
1324
1325public:
Gabor Greif051a9502008-04-06 20:25:17 +00001326 // allocate space for exactly two operands
1327 void *operator new(size_t s) {
Gabor Greifefe65362008-05-10 08:32:32 +00001328 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
Gabor Greif051a9502008-04-06 20:25:17 +00001329 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001330 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
1331 Instruction *InsertBefore = 0);
Chris Lattner06a248c22006-10-05 06:24:58 +00001332 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
1333 Instruction *InsertBefore = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001334 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
1335 BasicBlock *InsertAtEnd);
Chris Lattner06a248c22006-10-05 06:24:58 +00001336 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
1337 BasicBlock *InsertAtEnd);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001338
Chris Lattnerfa495842006-04-08 04:04:54 +00001339 /// isValidOperands - Return true if an extractelement instruction can be
1340 /// formed with the specified operands.
1341 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001342
Robert Bocchino49b78a52006-01-10 19:04:13 +00001343 virtual ExtractElementInst *clone() const;
1344
Robert Bocchino49b78a52006-01-10 19:04:13 +00001345 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001346 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001347
1348 // Methods for support type inquiry through isa, cast, and dyn_cast:
1349 static inline bool classof(const ExtractElementInst *) { return true; }
1350 static inline bool classof(const Instruction *I) {
1351 return I->getOpcode() == Instruction::ExtractElement;
1352 }
1353 static inline bool classof(const Value *V) {
1354 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1355 }
1356};
1357
Gabor Greifefe65362008-05-10 08:32:32 +00001358template <>
1359struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1360};
1361
1362DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1363
Robert Bocchino49b78a52006-01-10 19:04:13 +00001364//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001365// InsertElementInst Class
1366//===----------------------------------------------------------------------===//
1367
1368/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001369/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001370///
1371class InsertElementInst : public Instruction {
Chris Lattner6a56ed42006-04-14 22:20:07 +00001372 InsertElementInst(const InsertElementInst &IE);
Gabor Greif051a9502008-04-06 20:25:17 +00001373 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1374 const std::string &Name = "",Instruction *InsertBefore = 0);
1375 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1376 const std::string &Name = "",Instruction *InsertBefore = 0);
1377 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1378 const std::string &Name, BasicBlock *InsertAtEnd);
1379 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1380 const std::string &Name, BasicBlock *InsertAtEnd);
Robert Bocchinof9993442006-01-17 20:05:59 +00001381public:
Gabor Greif051a9502008-04-06 20:25:17 +00001382 static InsertElementInst *Create(const InsertElementInst &IE) {
1383 return new(IE.getNumOperands()) InsertElementInst(IE);
1384 }
1385 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Gabor Greifefe65362008-05-10 08:32:32 +00001386 const std::string &Name = "",
1387 Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00001388 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
1389 }
1390 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001391 const std::string &Name = "",
1392 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +00001393 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001394 }
1395 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001396 const std::string &Name,
1397 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001398 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertAtEnd);
1399 }
1400 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001401 const std::string &Name,
1402 BasicBlock *InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00001403 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001404 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001405
Chris Lattnerfa495842006-04-08 04:04:54 +00001406 /// isValidOperands - Return true if an insertelement instruction can be
1407 /// formed with the specified operands.
1408 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1409 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001410
Robert Bocchinof9993442006-01-17 20:05:59 +00001411 virtual InsertElementInst *clone() const;
1412
Reid Spencerac9dcb92007-02-15 03:39:18 +00001413 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001414 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001415 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001416 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001417 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001418
Robert Bocchinof9993442006-01-17 20:05:59 +00001419 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001420 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchinof9993442006-01-17 20:05:59 +00001421
1422 // Methods for support type inquiry through isa, cast, and dyn_cast:
1423 static inline bool classof(const InsertElementInst *) { return true; }
1424 static inline bool classof(const Instruction *I) {
1425 return I->getOpcode() == Instruction::InsertElement;
1426 }
1427 static inline bool classof(const Value *V) {
1428 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1429 }
1430};
1431
Gabor Greifefe65362008-05-10 08:32:32 +00001432template <>
1433struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1434};
1435
1436DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1437
Robert Bocchinof9993442006-01-17 20:05:59 +00001438//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001439// ShuffleVectorInst Class
1440//===----------------------------------------------------------------------===//
1441
1442/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1443/// input vectors.
1444///
1445class ShuffleVectorInst : public Instruction {
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001446 ShuffleVectorInst(const ShuffleVectorInst &IE);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001447public:
Gabor Greif051a9502008-04-06 20:25:17 +00001448 // allocate space for exactly three operands
1449 void *operator new(size_t s) {
1450 return User::operator new(s, 3);
1451 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001452 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1453 const std::string &Name = "", Instruction *InsertBefor = 0);
1454 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1455 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001456
Chris Lattnerfa495842006-04-08 04:04:54 +00001457 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001458 /// formed with the specified operands.
1459 static bool isValidOperands(const Value *V1, const Value *V2,
1460 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001461
Chris Lattner9fc18d22006-04-08 01:15:18 +00001462 virtual ShuffleVectorInst *clone() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001463
Reid Spencerac9dcb92007-02-15 03:39:18 +00001464 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001465 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001466 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001467 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001468 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001469
Chris Lattner9fc18d22006-04-08 01:15:18 +00001470 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001471 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner8728f192008-03-02 05:28:33 +00001472
1473 /// getMaskValue - Return the index from the shuffle mask for the specified
1474 /// output result. This is either -1 if the element is undef or a number less
1475 /// than 2*numelements.
1476 int getMaskValue(unsigned i) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001477
Chris Lattner9fc18d22006-04-08 01:15:18 +00001478 // Methods for support type inquiry through isa, cast, and dyn_cast:
1479 static inline bool classof(const ShuffleVectorInst *) { return true; }
1480 static inline bool classof(const Instruction *I) {
1481 return I->getOpcode() == Instruction::ShuffleVector;
1482 }
1483 static inline bool classof(const Value *V) {
1484 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1485 }
1486};
1487
Gabor Greifefe65362008-05-10 08:32:32 +00001488template <>
1489struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1490};
1491
1492DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
Chris Lattner9fc18d22006-04-08 01:15:18 +00001493
1494//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001495// ExtractValueInst Class
1496//===----------------------------------------------------------------------===//
1497
1498/// ExtractValueInst - This instruction extracts a value
1499/// from an aggregate value
1500///
1501class ExtractValueInst : public Instruction {
1502 ExtractValueInst(const ExtractValueInst &EVI);
1503 void init(Value *Agg, Value* const *Idx, unsigned NumIdx);
1504 void init(Value *Agg, Value *Idx);
1505
1506 template<typename InputIterator>
1507 void init(Value *Agg, InputIterator IdxBegin, InputIterator IdxEnd,
1508 const std::string &Name,
1509 // This argument ensures that we have an iterator we can
1510 // do arithmetic on in constant time
1511 std::random_access_iterator_tag) {
1512 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1513
1514 if (NumIdx > 0) {
1515 // This requires that the iterator points to contiguous memory.
1516 init(Agg, &*IdxBegin, NumIdx); // FIXME: for the general case
1517 // we have to build an array here
1518 }
1519 else {
1520 init(Agg, 0, NumIdx);
1521 }
1522
1523 setName(Name);
1524 }
1525
1526 /// getIndexedType - Returns the type of the element that would be extracted
1527 /// with an extractvalue instruction with the specified parameters.
1528 ///
1529 /// A null type is returned if the indices are invalid for the specified
1530 /// pointer type.
1531 ///
1532 static const Type *getIndexedType(const Type *Agg,
1533 Value* const *Idx, unsigned NumIdx);
1534
1535 template<typename InputIterator>
1536 static const Type *getIndexedType(const Type *Ptr,
1537 InputIterator IdxBegin,
1538 InputIterator IdxEnd,
1539 // This argument ensures that we
1540 // have an iterator we can do
1541 // arithmetic on in constant time
1542 std::random_access_iterator_tag) {
1543 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1544
1545 if (NumIdx > 0)
1546 // This requires that the iterator points to contiguous memory.
1547 return getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx);
1548 else
1549 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
1550 }
1551
1552 /// Constructors - Create a extractvalue instruction with a base pointer an
1553 /// list of indices. The first ctor can optionally insert before an existing
1554 /// instruction, the second appends the new instruction to the specified
1555 /// BasicBlock.
1556 template<typename InputIterator>
1557 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1558 InputIterator IdxEnd,
1559 unsigned Values,
1560 const std::string &Name,
1561 Instruction *InsertBefore);
1562 template<typename InputIterator>
1563 inline ExtractValueInst(Value *Agg,
1564 InputIterator IdxBegin, InputIterator IdxEnd,
1565 unsigned Values,
1566 const std::string &Name, BasicBlock *InsertAtEnd);
1567
1568 /// Constructors - These two constructors are convenience methods because one
1569 /// and two index extractvalue instructions are so common.
1570 ExtractValueInst(Value *Agg, Value *Idx, const std::string &Name = "",
1571 Instruction *InsertBefore = 0);
1572 ExtractValueInst(Value *Agg, Value *Idx,
1573 const std::string &Name, BasicBlock *InsertAtEnd);
1574public:
1575 template<typename InputIterator>
1576 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1577 InputIterator IdxEnd,
1578 const std::string &Name = "",
1579 Instruction *InsertBefore = 0) {
1580 typename std::iterator_traits<InputIterator>::difference_type Values =
1581 1 + std::distance(IdxBegin, IdxEnd);
1582 return new(Values)
1583 ExtractValueInst(Agg, IdxBegin, IdxEnd, Values, Name, InsertBefore);
1584 }
1585 template<typename InputIterator>
1586 static ExtractValueInst *Create(Value *Agg,
1587 InputIterator IdxBegin, InputIterator IdxEnd,
1588 const std::string &Name,
1589 BasicBlock *InsertAtEnd) {
1590 typename std::iterator_traits<InputIterator>::difference_type Values =
1591 1 + std::distance(IdxBegin, IdxEnd);
1592 return new(Values)
1593 ExtractValueInst(Agg, IdxBegin, IdxEnd, Values, Name, InsertAtEnd);
1594 }
1595
1596 /// Constructors - These two creators are convenience methods because one
1597 /// index extractvalue instructions are much more common than those with
1598 /// more than one.
1599 static ExtractValueInst *Create(Value *Agg, Value *Idx,
1600 const std::string &Name = "",
1601 Instruction *InsertBefore = 0) {
1602 return new(2) ExtractValueInst(Agg, Idx, Name, InsertBefore);
1603 }
1604 static ExtractValueInst *Create(Value *Agg, Value *Idx,
1605 const std::string &Name,
1606 BasicBlock *InsertAtEnd) {
1607 return new(2) ExtractValueInst(Agg, Idx, Name, InsertAtEnd);
1608 }
1609
1610 virtual ExtractValueInst *clone() const;
1611
1612 /// Transparently provide more efficient getOperand methods.
1613 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1614
1615 // getType - Overload to return most specific pointer type...
1616 const PointerType *getType() const {
1617 return reinterpret_cast<const PointerType*>(Instruction::getType());
1618 }
1619
1620 /// getIndexedType - Returns the type of the element that would be extracted
1621 /// with an extractvalue instruction with the specified parameters.
1622 ///
1623 /// A null type is returned if the indices are invalid for the specified
1624 /// pointer type.
1625 ///
1626 template<typename InputIterator>
1627 static const Type *getIndexedType(const Type *Ptr,
1628 InputIterator IdxBegin,
1629 InputIterator IdxEnd) {
1630 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1631 typename std::iterator_traits<InputIterator>::
1632 iterator_category());
1633 }
1634 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
1635
1636 inline op_iterator idx_begin() { return op_begin()+1; }
1637 inline const_op_iterator idx_begin() const { return op_begin()+1; }
1638 inline op_iterator idx_end() { return op_end(); }
1639 inline const_op_iterator idx_end() const { return op_end(); }
1640
1641 Value *getAggregateOperand() {
1642 return getOperand(0);
1643 }
1644 const Value *getAggregateOperand() const {
1645 return getOperand(0);
1646 }
1647 static unsigned getAggregateOperandIndex() {
1648 return 0U; // get index for modifying correct operand
1649 }
1650
1651 unsigned getNumIndices() const { // Note: always non-negative
1652 return getNumOperands() - 1;
1653 }
1654
1655 bool hasIndices() const {
1656 return getNumOperands() > 1;
1657 }
1658
1659 // Methods for support type inquiry through isa, cast, and dyn_cast:
1660 static inline bool classof(const ExtractValueInst *) { return true; }
1661 static inline bool classof(const Instruction *I) {
1662 return I->getOpcode() == Instruction::ExtractValue;
1663 }
1664 static inline bool classof(const Value *V) {
1665 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1666 }
1667};
1668
1669template <>
1670struct OperandTraits<ExtractValueInst> : VariadicOperandTraits<1> {
1671};
1672
1673DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueInst, Value)
1674
1675//===----------------------------------------------------------------------===//
1676// InsertValueInst Class
1677//===----------------------------------------------------------------------===//
1678
1679/// InsertValueInst - This instruction extracts a value
1680/// from an aggregate value
1681///
1682class InsertValueInst : public Instruction {
1683 InsertValueInst(const InsertValueInst &IVI);
1684 void init(Value *Agg, Value *Val, Value* const *Idx, unsigned NumIdx);
1685 void init(Value *Agg, Value *Val, Value *Idx);
1686
1687 template<typename InputIterator>
1688 void init(Value *Agg, Value *Val,
1689 InputIterator IdxBegin, InputIterator IdxEnd,
1690 const std::string &Name,
1691 // This argument ensures that we have an iterator we can
1692 // do arithmetic on in constant time
1693 std::random_access_iterator_tag) {
1694 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1695
1696 if (NumIdx > 0) {
1697 // This requires that the iterator points to contiguous memory.
1698 init(Agg, Val, &*IdxBegin, NumIdx); // FIXME: for the general case
1699 // we have to build an array here
1700 }
1701 else {
1702 init(Agg, Val, 0, NumIdx);
1703 }
1704
1705 setName(Name);
1706 }
1707
1708 /// Constructors - Create a insertvalue instruction with a base pointer an
1709 /// list of indices. The first ctor can optionally insert before an existing
1710 /// instruction, the second appends the new instruction to the specified
1711 /// BasicBlock.
1712 template<typename InputIterator>
1713 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1714 InputIterator IdxEnd,
1715 unsigned Values,
1716 const std::string &Name,
1717 Instruction *InsertBefore);
1718 template<typename InputIterator>
1719 inline InsertValueInst(Value *Agg, Value *Val,
1720 InputIterator IdxBegin, InputIterator IdxEnd,
1721 unsigned Values,
1722 const std::string &Name, BasicBlock *InsertAtEnd);
1723
1724 /// Constructors - These two constructors are convenience methods because one
1725 /// and two index insertvalue instructions are so common.
1726 InsertValueInst(Value *Agg, Value *Val,
1727 Value *Idx, const std::string &Name = "",
1728 Instruction *InsertBefore = 0);
1729 InsertValueInst(Value *Agg, Value *Val, Value *Idx,
1730 const std::string &Name, BasicBlock *InsertAtEnd);
1731public:
1732 template<typename InputIterator>
1733 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1734 InputIterator IdxEnd,
1735 const std::string &Name = "",
1736 Instruction *InsertBefore = 0) {
1737 typename std::iterator_traits<InputIterator>::difference_type Values =
1738 1 + std::distance(IdxBegin, IdxEnd);
1739 return new(Values)
1740 InsertValueInst(Agg, Val, IdxBegin, IdxEnd, Values, Name, InsertBefore);
1741 }
1742 template<typename InputIterator>
1743 static InsertValueInst *Create(Value *Agg, Value *Val,
1744 InputIterator IdxBegin, InputIterator IdxEnd,
1745 const std::string &Name,
1746 BasicBlock *InsertAtEnd) {
1747 typename std::iterator_traits<InputIterator>::difference_type Values =
1748 1 + std::distance(IdxBegin, IdxEnd);
1749 return new(Values)
1750 InsertValueInst(Agg, Val, IdxBegin, IdxEnd, Values, Name, InsertAtEnd);
1751 }
1752
1753 /// Constructors - These two creators are convenience methods because one
1754 /// index insertvalue instructions are much more common than those with
1755 /// more than one.
1756 static InsertValueInst *Create(Value *Agg, Value *Val, Value *Idx,
1757 const std::string &Name = "",
1758 Instruction *InsertBefore = 0) {
1759 return new(3) InsertValueInst(Agg, Val, Idx, Name, InsertBefore);
1760 }
1761 static InsertValueInst *Create(Value *Agg, Value *Val, Value *Idx,
1762 const std::string &Name,
1763 BasicBlock *InsertAtEnd) {
1764 return new(3) InsertValueInst(Agg, Val, Idx, Name, InsertAtEnd);
1765 }
1766
1767 virtual InsertValueInst *clone() const;
1768
1769 /// Transparently provide more efficient getOperand methods.
1770 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1771
1772 // getType - Overload to return most specific pointer type...
1773 const PointerType *getType() const {
1774 return reinterpret_cast<const PointerType*>(Instruction::getType());
1775 }
1776
1777 inline op_iterator idx_begin() { return op_begin()+1; }
1778 inline const_op_iterator idx_begin() const { return op_begin()+1; }
1779 inline op_iterator idx_end() { return op_end(); }
1780 inline const_op_iterator idx_end() const { return op_end(); }
1781
1782 Value *getAggregateOperand() {
1783 return getOperand(0);
1784 }
1785 const Value *getAggregateOperand() const {
1786 return getOperand(0);
1787 }
1788 static unsigned getAggregateOperandIndex() {
1789 return 0U; // get index for modifying correct operand
1790 }
1791
1792 Value *getInsertedValueOperand() {
1793 return getOperand(1);
1794 }
1795 const Value *getInsertedValueOperand() const {
1796 return getOperand(1);
1797 }
1798 static unsigned getInsertedValueOperandIndex() {
1799 return 1U; // get index for modifying correct operand
1800 }
1801
1802 unsigned getNumIndices() const { // Note: always non-negative
1803 return getNumOperands() - 2;
1804 }
1805
1806 bool hasIndices() const {
1807 return getNumOperands() > 2;
1808 }
1809
1810 // Methods for support type inquiry through isa, cast, and dyn_cast:
1811 static inline bool classof(const InsertValueInst *) { return true; }
1812 static inline bool classof(const Instruction *I) {
1813 return I->getOpcode() == Instruction::InsertValue;
1814 }
1815 static inline bool classof(const Value *V) {
1816 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1817 }
1818};
1819
1820template <>
1821struct OperandTraits<InsertValueInst> : VariadicOperandTraits<2> {
1822};
1823
1824DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1825
1826//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001827// PHINode Class
1828//===----------------------------------------------------------------------===//
1829
1830// PHINode - The PHINode class is used to represent the magical mystical PHI
1831// node, that can not exist in nature, but can be synthesized in a computer
1832// scientist's overactive imagination.
1833//
1834class PHINode : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001835 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00001836 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1837 /// the number actually in use.
1838 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001839 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00001840 // allocate space for exactly zero operands
1841 void *operator new(size_t s) {
1842 return User::operator new(s, 0);
1843 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001844 explicit PHINode(const Type *Ty, const std::string &Name = "",
1845 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001846 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Chris Lattner454928e2005-01-29 00:31:36 +00001847 ReservedSpace(0) {
Chris Lattner910c80a2007-02-24 00:55:48 +00001848 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001849 }
1850
1851 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001852 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Chris Lattner454928e2005-01-29 00:31:36 +00001853 ReservedSpace(0) {
Chris Lattner910c80a2007-02-24 00:55:48 +00001854 setName(Name);
Chris Lattner454928e2005-01-29 00:31:36 +00001855 }
Gabor Greif051a9502008-04-06 20:25:17 +00001856public:
1857 static PHINode *Create(const Type *Ty, const std::string &Name = "",
1858 Instruction *InsertBefore = 0) {
1859 return new PHINode(Ty, Name, InsertBefore);
1860 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001861 static PHINode *Create(const Type *Ty, const std::string &Name,
1862 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001863 return new PHINode(Ty, Name, InsertAtEnd);
1864 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001865 ~PHINode();
1866
Chris Lattner454928e2005-01-29 00:31:36 +00001867 /// reserveOperandSpace - This method can be used to avoid repeated
1868 /// reallocation of PHI operand lists by reserving space for the correct
1869 /// number of operands before adding them. Unlike normal vector reserves,
1870 /// this method can also be used to trim the operand space.
1871 void reserveOperandSpace(unsigned NumValues) {
1872 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001873 }
1874
Chris Lattnerf319e832004-10-15 23:52:05 +00001875 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001876
Gabor Greifefe65362008-05-10 08:32:32 +00001877 /// Provide fast operand accessors
1878 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1879
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001880 /// getNumIncomingValues - Return the number of incoming edges
1881 ///
Chris Lattner454928e2005-01-29 00:31:36 +00001882 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001883
Reid Spencerc773de62006-05-19 19:07:54 +00001884 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001885 ///
1886 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001887 assert(i*2 < getNumOperands() && "Invalid value number!");
1888 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001889 }
1890 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +00001891 assert(i*2 < getNumOperands() && "Invalid value number!");
1892 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001893 }
Chris Lattner454928e2005-01-29 00:31:36 +00001894 unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001895 return i*2;
1896 }
1897
Reid Spencerc773de62006-05-19 19:07:54 +00001898 /// getIncomingBlock - Return incoming basic block number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001899 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00001900 BasicBlock *getIncomingBlock(unsigned i) const {
Gabor Greifefe65362008-05-10 08:32:32 +00001901 return static_cast<BasicBlock*>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001902 }
1903 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Gabor Greifefe65362008-05-10 08:32:32 +00001904 setOperand(i*2+1, BB);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001905 }
1906 unsigned getOperandNumForIncomingBlock(unsigned i) {
1907 return i*2+1;
1908 }
1909
1910 /// addIncoming - Add an incoming value to the end of the PHI list
1911 ///
1912 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00001913 assert(V && "PHI node got a null value!");
1914 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001915 assert(getType() == V->getType() &&
1916 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +00001917 unsigned OpNo = NumOperands;
1918 if (OpNo+2 > ReservedSpace)
1919 resizeOperands(0); // Get more space!
1920 // Initialize some new operands.
1921 NumOperands = OpNo+2;
1922 OperandList[OpNo].init(V, this);
Gabor Greifefe65362008-05-10 08:32:32 +00001923 OperandList[OpNo+1].init(BB, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001924 }
Misha Brukman9769ab22005-04-21 20:19:05 +00001925
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001926 /// removeIncomingValue - Remove an incoming value. This is useful if a
1927 /// predecessor basic block is deleted. The value removed is returned.
1928 ///
1929 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1930 /// is true), the PHI node is destroyed and any uses of it are replaced with
1931 /// dummy values. The only time there should be zero incoming values to a PHI
1932 /// node is when the block is dead, so this strategy is sound.
1933 ///
1934 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1935
Gabor Greifefe65362008-05-10 08:32:32 +00001936 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001937 int Idx = getBasicBlockIndex(BB);
1938 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1939 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1940 }
1941
Misha Brukman9769ab22005-04-21 20:19:05 +00001942 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001943 /// block in the value list for this PHI. Returns -1 if no instance.
1944 ///
1945 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001946 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00001947 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Gabor Greifefe65362008-05-10 08:32:32 +00001948 if (OL[i+1].get() == BB) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001949 return -1;
1950 }
1951
1952 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1953 return getIncomingValue(getBasicBlockIndex(BB));
1954 }
1955
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001956 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00001957 /// same value, return the value, otherwise return null.
1958 ///
Chris Lattner9acbd612005-08-05 00:49:06 +00001959 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001960
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001961 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1962 static inline bool classof(const PHINode *) { return true; }
1963 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001964 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001965 }
1966 static inline bool classof(const Value *V) {
1967 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1968 }
Chris Lattner454928e2005-01-29 00:31:36 +00001969 private:
1970 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001971};
1972
Gabor Greifefe65362008-05-10 08:32:32 +00001973template <>
1974struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1975};
1976
1977DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1978
1979
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001980//===----------------------------------------------------------------------===//
1981// ReturnInst Class
1982//===----------------------------------------------------------------------===//
1983
1984//===---------------------------------------------------------------------------
1985/// ReturnInst - Return a value (possibly void), from a function. Execution
1986/// does not continue in this function any longer.
1987///
1988class ReturnInst : public TerminatorInst {
Chris Lattner910c80a2007-02-24 00:55:48 +00001989 ReturnInst(const ReturnInst &RI);
Devang Patelfea98302008-02-26 19:15:26 +00001990 void init(Value * const* retVals, unsigned N);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001991
Gabor Greif051a9502008-04-06 20:25:17 +00001992private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001993 // ReturnInst constructors:
1994 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001995 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001996 // ReturnInst(Value* X) - 'ret X' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00001997 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001998 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
Gabor Greifefe65362008-05-10 08:32:32 +00001999 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2000 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
Devang Patele6be34a2008-02-27 01:20:54 +00002001 // ReturnInst(Value* X, N) - 'ret X,X+1...X+N-1' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00002002 // ReturnInst(Value* X, N, Inst *I) - 'ret X,X+1...X+N-1', insert before I
2003 // ReturnInst(Value* X, N, BB *B) - 'ret X,X+1...X+N-1', insert @ end of B
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002004 //
2005 // NOTE: If the Value* passed is of type void then the constructor behaves as
2006 // if it was passed NULL.
Chris Lattner910c80a2007-02-24 00:55:48 +00002007 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
2008 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
Gabor Greifefe65362008-05-10 08:32:32 +00002009 ReturnInst(Value * const* retVals, unsigned N, Instruction *InsertBefore = 0);
Devang Patelf4511cd2008-02-26 19:38:17 +00002010 ReturnInst(Value * const* retVals, unsigned N, BasicBlock *InsertAtEnd);
Chris Lattner910c80a2007-02-24 00:55:48 +00002011 explicit ReturnInst(BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002012public:
2013 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
2014 return new(!!retVal) ReturnInst(retVal, InsertBefore);
2015 }
2016 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
2017 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
2018 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00002019 static ReturnInst* Create(Value * const* retVals, unsigned N,
Gabor Greifefe65362008-05-10 08:32:32 +00002020 Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002021 return new(N) ReturnInst(retVals, N, InsertBefore);
2022 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00002023 static ReturnInst* Create(Value * const* retVals, unsigned N,
2024 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002025 return new(N) ReturnInst(retVals, N, InsertAtEnd);
2026 }
2027 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
2028 return new(0) ReturnInst(InsertAtEnd);
2029 }
Devang Patel57ef4f42008-02-23 00:35:18 +00002030 virtual ~ReturnInst();
Gabor Greifefe65362008-05-10 08:32:32 +00002031 inline void operator delete(void*);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002032
Chris Lattnerf319e832004-10-15 23:52:05 +00002033 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002034
Gabor Greifefe65362008-05-10 08:32:32 +00002035 /// Provide fast operand accessors
2036 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Devang Patel64d4e612008-02-26 17:56:20 +00002037
Gabor Greifefe65362008-05-10 08:32:32 +00002038 /// Convenience accessor
Devang Patel1eafa062008-03-11 17:35:03 +00002039 Value *getReturnValue(unsigned n = 0) const {
Gabor Greifefe65362008-05-10 08:32:32 +00002040 return n < getNumOperands()
2041 ? getOperand(n)
2042 : 0;
Devang Patel1eafa062008-03-11 17:35:03 +00002043 }
2044
Chris Lattner454928e2005-01-29 00:31:36 +00002045 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002046
2047 // Methods for support type inquiry through isa, cast, and dyn_cast:
2048 static inline bool classof(const ReturnInst *) { return true; }
2049 static inline bool classof(const Instruction *I) {
2050 return (I->getOpcode() == Instruction::Ret);
2051 }
2052 static inline bool classof(const Value *V) {
2053 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2054 }
Chris Lattner454928e2005-01-29 00:31:36 +00002055 private:
2056 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2057 virtual unsigned getNumSuccessorsV() const;
2058 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002059};
2060
Gabor Greifefe65362008-05-10 08:32:32 +00002061template <>
2062struct OperandTraits<ReturnInst> : VariadicOperandTraits<> {
2063};
2064
2065DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2066void ReturnInst::operator delete(void *it) {
2067 ReturnInst* me(static_cast<ReturnInst*>(it));
2068 Use::zap(OperandTraits<ReturnInst>::op_begin(me),
2069 OperandTraits<ReturnInst>::op_end(me),
2070 true);
2071}
2072
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002073//===----------------------------------------------------------------------===//
2074// BranchInst Class
2075//===----------------------------------------------------------------------===//
2076
2077//===---------------------------------------------------------------------------
2078/// BranchInst - Conditional or Unconditional Branch instruction.
2079///
2080class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00002081 /// Ops list - Branches are strange. The operands are ordered:
2082 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
2083 /// they don't have to check for cond/uncond branchness.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002084 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00002085 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002086 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2087 // BranchInst(BB *B) - 'br B'
2088 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2089 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2090 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2091 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2092 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00002093 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002094 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002095 Instruction *InsertBefore = 0);
2096 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002097 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002098 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002099public:
2100 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2101 return new(1) BranchInst(IfTrue, InsertBefore);
2102 }
Gabor Greifefe65362008-05-10 08:32:32 +00002103 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2104 Value *Cond, Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002105 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2106 }
2107 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2108 return new(1) BranchInst(IfTrue, InsertAtEnd);
2109 }
Gabor Greifefe65362008-05-10 08:32:32 +00002110 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2111 Value *Cond, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002112 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2113 }
Chris Lattner454928e2005-01-29 00:31:36 +00002114
Bill Wendlingd2a5a2a2008-05-10 10:58:07 +00002115 ~BranchInst() {
Gabor Greifefe65362008-05-10 08:32:32 +00002116 if (NumOperands == 1)
Bill Wendlingc2e73532008-05-10 19:59:59 +00002117 NumOperands = (unsigned)((Use*)this - OperandList);
Gabor Greifefe65362008-05-10 08:32:32 +00002118 }
2119
Chris Lattner454928e2005-01-29 00:31:36 +00002120 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00002121 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002122
Chris Lattnerf319e832004-10-15 23:52:05 +00002123 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002124
Devang Patel4d4a5e02008-02-23 01:11:02 +00002125 bool isUnconditional() const { return getNumOperands() == 1; }
2126 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002127
Devang Patel4d4a5e02008-02-23 01:11:02 +00002128 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002129 assert(isConditional() && "Cannot get condition of an uncond branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00002130 return getOperand(2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002131 }
2132
2133 void setCondition(Value *V) {
2134 assert(isConditional() && "Cannot set condition of unconditional branch!");
2135 setOperand(2, V);
2136 }
2137
2138 // setUnconditionalDest - Change the current branch to an unconditional branch
2139 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +00002140 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002141 void setUnconditionalDest(BasicBlock *Dest) {
Gabor Greifefe65362008-05-10 08:32:32 +00002142 Op<0>() = Dest;
Chris Lattner454928e2005-01-29 00:31:36 +00002143 if (isConditional()) { // Convert this to an uncond branch.
Gabor Greifefe65362008-05-10 08:32:32 +00002144 Op<1>().set(0);
2145 Op<2>().set(0);
Chris Lattner454928e2005-01-29 00:31:36 +00002146 NumOperands = 1;
Chris Lattner454928e2005-01-29 00:31:36 +00002147 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002148 }
2149
Chris Lattner454928e2005-01-29 00:31:36 +00002150 unsigned getNumSuccessors() const { return 1+isConditional(); }
2151
2152 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002153 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Dan Gohmanb96039e2007-05-11 20:59:29 +00002154 return cast<BasicBlock>(getOperand(i));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002155 }
2156
Chris Lattner454928e2005-01-29 00:31:36 +00002157 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002158 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifefe65362008-05-10 08:32:32 +00002159 setOperand(idx, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002160 }
2161
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002162 // Methods for support type inquiry through isa, cast, and dyn_cast:
2163 static inline bool classof(const BranchInst *) { return true; }
2164 static inline bool classof(const Instruction *I) {
2165 return (I->getOpcode() == Instruction::Br);
2166 }
2167 static inline bool classof(const Value *V) {
2168 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2169 }
Chris Lattner454928e2005-01-29 00:31:36 +00002170private:
2171 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2172 virtual unsigned getNumSuccessorsV() const;
2173 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002174};
2175
Gabor Greifefe65362008-05-10 08:32:32 +00002176template <>
2177struct OperandTraits<BranchInst> : HungoffOperandTraits<> {
2178 // we need to access operands via OperandList, since
2179 // the NumOperands may change from 3 to 1
2180 static inline void *allocate(unsigned); // FIXME
2181};
2182
2183DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2184
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002185//===----------------------------------------------------------------------===//
2186// SwitchInst Class
2187//===----------------------------------------------------------------------===//
2188
2189//===---------------------------------------------------------------------------
2190/// SwitchInst - Multiway switch
2191///
2192class SwitchInst : public TerminatorInst {
Gabor Greifefe65362008-05-10 08:32:32 +00002193 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00002194 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002195 // Operand[0] = Value to switch on
2196 // Operand[1] = Default basic block destination
2197 // Operand[2n ] = Value to match
2198 // Operand[2n+1] = BasicBlock to go to on match
2199 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00002200 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2201 void resizeOperands(unsigned No);
Gabor Greifefe65362008-05-10 08:32:32 +00002202 // allocate space for exactly zero operands
2203 void *operator new(size_t s) {
2204 return User::operator new(s, 0);
2205 }
Chris Lattner454928e2005-01-29 00:31:36 +00002206 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2207 /// switch on and a default destination. The number of additional cases can
2208 /// be specified here to make memory allocation more efficient. This
2209 /// constructor can also autoinsert before another instruction.
2210 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002211 Instruction *InsertBefore = 0);
2212
Chris Lattner454928e2005-01-29 00:31:36 +00002213 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2214 /// switch on and a default destination. The number of additional cases can
2215 /// be specified here to make memory allocation more efficient. This
2216 /// constructor also autoinserts at the end of the specified BasicBlock.
2217 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002218 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002219public:
Gabor Greifefe65362008-05-10 08:32:32 +00002220 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2221 unsigned NumCases, Instruction *InsertBefore = 0) {
2222 return new SwitchInst(Value, Default, NumCases, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002223 }
Gabor Greifefe65362008-05-10 08:32:32 +00002224 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2225 unsigned NumCases, BasicBlock *InsertAtEnd) {
2226 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002227 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002228 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00002229
Gabor Greifefe65362008-05-10 08:32:32 +00002230 /// Provide fast operand accessors
2231 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2232
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002233 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00002234 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00002235 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00002236
Devang Patel4d4a5e02008-02-23 01:11:02 +00002237 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002238 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002239 }
2240
2241 /// getNumCases - return the number of 'cases' in this switch instruction.
2242 /// Note that case #0 is always the default case.
2243 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002244 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002245 }
2246
2247 /// getCaseValue - Return the specified case value. Note that case #0, the
2248 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002249 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002250 assert(i && i < getNumCases() && "Illegal case value to get!");
2251 return getSuccessorValue(i);
2252 }
2253
2254 /// getCaseValue - Return the specified case value. Note that case #0, the
2255 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002256 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002257 assert(i && i < getNumCases() && "Illegal case value to get!");
2258 return getSuccessorValue(i);
2259 }
2260
2261 /// findCaseValue - Search all of the case values for the specified constant.
2262 /// If it is explicitly handled, return the case number of it, otherwise
2263 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002264 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002265 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2266 if (getCaseValue(i) == C)
2267 return i;
2268 return 0;
2269 }
2270
Nick Lewycky011f1842006-09-18 19:03:59 +00002271 /// findCaseDest - Finds the unique case value for a given successor. Returns
2272 /// null if the successor is not found, not unique, or is the default case.
2273 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00002274 if (BB == getDefaultDest()) return NULL;
2275
Nick Lewycky011f1842006-09-18 19:03:59 +00002276 ConstantInt *CI = NULL;
2277 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2278 if (getSuccessor(i) == BB) {
2279 if (CI) return NULL; // Multiple cases lead to BB.
2280 else CI = getCaseValue(i);
2281 }
2282 }
2283 return CI;
2284 }
2285
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002286 /// addCase - Add an entry to the switch instruction...
2287 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00002288 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002289
2290 /// removeCase - This method removes the specified successor from the switch
2291 /// instruction. Note that this cannot be used to remove the default
2292 /// destination (successor #0).
2293 ///
2294 void removeCase(unsigned idx);
2295
Chris Lattner454928e2005-01-29 00:31:36 +00002296 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002297
Chris Lattner454928e2005-01-29 00:31:36 +00002298 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2299 BasicBlock *getSuccessor(unsigned idx) const {
2300 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2301 return cast<BasicBlock>(getOperand(idx*2+1));
2302 }
2303 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002304 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Gabor Greifefe65362008-05-10 08:32:32 +00002305 setOperand(idx*2+1, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002306 }
2307
2308 // getSuccessorValue - Return the value associated with the specified
2309 // successor.
Devang Patel4d4a5e02008-02-23 01:11:02 +00002310 ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002311 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00002312 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002313 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002314
2315 // Methods for support type inquiry through isa, cast, and dyn_cast:
2316 static inline bool classof(const SwitchInst *) { return true; }
2317 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00002318 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002319 }
2320 static inline bool classof(const Value *V) {
2321 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2322 }
Chris Lattner454928e2005-01-29 00:31:36 +00002323private:
2324 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2325 virtual unsigned getNumSuccessorsV() const;
2326 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002327};
2328
Gabor Greifefe65362008-05-10 08:32:32 +00002329template <>
2330struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2331};
2332
2333DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2334
2335
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002336//===----------------------------------------------------------------------===//
2337// InvokeInst Class
2338//===----------------------------------------------------------------------===//
2339
Chris Lattner3340ffe2005-05-06 20:26:26 +00002340/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2341/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002342///
2343class InvokeInst : public TerminatorInst {
Chris Lattner58d74912008-03-12 17:45:29 +00002344 PAListPtr ParamAttrs;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002345 InvokeInst(const InvokeInst &BI);
2346 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerd2dd1502007-02-13 01:04:01 +00002347 Value* const *Args, unsigned NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00002348
2349 template<typename InputIterator>
2350 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2351 InputIterator ArgBegin, InputIterator ArgEnd,
2352 const std::string &Name,
2353 // This argument ensures that we have an iterator we can
2354 // do arithmetic on in constant time
2355 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002356 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greenef1355a52007-08-27 19:04:21 +00002357
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002358 // This requires that the iterator points to contiguous memory.
2359 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00002360 setName(Name);
2361 }
2362
David Greenef1355a52007-08-27 19:04:21 +00002363 /// Construct an InvokeInst given a range of arguments.
2364 /// InputIterator must be a random-access iterator pointing to
2365 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2366 /// made for random-accessness but not for contiguous storage as
2367 /// that would incur runtime overhead.
2368 ///
2369 /// @brief Construct an InvokeInst from a range of arguments
2370 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002371 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2372 InputIterator ArgBegin, InputIterator ArgEnd,
2373 unsigned Values,
2374 const std::string &Name, Instruction *InsertBefore);
David Greenef1355a52007-08-27 19:04:21 +00002375
2376 /// Construct an InvokeInst given a range of arguments.
2377 /// InputIterator must be a random-access iterator pointing to
2378 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2379 /// made for random-accessness but not for contiguous storage as
2380 /// that would incur runtime overhead.
2381 ///
2382 /// @brief Construct an InvokeInst from a range of arguments
2383 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002384 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2385 InputIterator ArgBegin, InputIterator ArgEnd,
2386 unsigned Values,
2387 const std::string &Name, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002388public:
2389 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002390 static InvokeInst *Create(Value *Func,
2391 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002392 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Chengd69bb1a2008-05-05 17:41:03 +00002393 const std::string &Name = "",
2394 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +00002395 unsigned Values(ArgEnd - ArgBegin + 3);
2396 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2397 Values, Name, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002398 }
2399 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002400 static InvokeInst *Create(Value *Func,
2401 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002402 InputIterator ArgBegin, InputIterator ArgEnd,
2403 const std::string &Name, BasicBlock *InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00002404 unsigned Values(ArgEnd - ArgBegin + 3);
2405 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2406 Values, Name, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002407 }
David Greenef1355a52007-08-27 19:04:21 +00002408
Chris Lattnerf319e832004-10-15 23:52:05 +00002409 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002410
Gabor Greifefe65362008-05-10 08:32:32 +00002411 /// Provide fast operand accessors
2412 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2413
Chris Lattner3340ffe2005-05-06 20:26:26 +00002414 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2415 /// function call.
2416 unsigned getCallingConv() const { return SubclassData; }
2417 void setCallingConv(unsigned CC) {
2418 SubclassData = CC;
2419 }
2420
Chris Lattner041221c2008-03-13 04:33:03 +00002421 /// getParamAttrs - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002422 ///
2423 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002424
Chris Lattner041221c2008-03-13 04:33:03 +00002425 /// setParamAttrs - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002426 ///
2427 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00002428
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002429 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002430 bool paramHasAttr(unsigned i, ParameterAttributes attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002431
Dale Johannesen08e78b12008-02-22 17:49:45 +00002432 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002433 unsigned getParamAlignment(unsigned i) const {
2434 return ParamAttrs.getParamAlignment(i);
2435 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00002436
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002437 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002438 bool doesNotAccessMemory() const {
2439 return paramHasAttr(0, ParamAttr::ReadNone);
2440 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002441
2442 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002443 bool onlyReadsMemory() const {
2444 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
2445 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002446
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002447 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002448 bool doesNotReturn() const {
2449 return paramHasAttr(0, ParamAttr::NoReturn);
2450 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002451
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002452 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002453 bool doesNotThrow() const {
2454 return paramHasAttr(0, ParamAttr::NoUnwind);
2455 }
Duncan Sandsf0c33542007-12-19 21:13:37 +00002456 void setDoesNotThrow(bool doesNotThrow = true);
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002457
Devang Patel41e23972008-03-03 21:46:28 +00002458 /// @brief Determine if the call returns a structure through first
2459 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002460 bool hasStructRetAttr() const {
2461 // Be friendly and also check the callee.
2462 return paramHasAttr(1, ParamAttr::StructRet);
2463 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002464
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002465 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00002466 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002467 ///
Chris Lattner721aef62004-11-18 17:46:57 +00002468 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002469 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00002470 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002471
2472 // getCalledValue - Get a pointer to a function that is invoked by this inst.
Devang Patel4d4a5e02008-02-23 01:11:02 +00002473 Value *getCalledValue() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002474
2475 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00002476 BasicBlock *getNormalDest() const {
2477 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002478 }
Chris Lattner454928e2005-01-29 00:31:36 +00002479 BasicBlock *getUnwindDest() const {
2480 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002481 }
Chris Lattner454928e2005-01-29 00:31:36 +00002482 void setNormalDest(BasicBlock *B) {
Gabor Greifefe65362008-05-10 08:32:32 +00002483 setOperand(1, B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002484 }
2485
Chris Lattner454928e2005-01-29 00:31:36 +00002486 void setUnwindDest(BasicBlock *B) {
Gabor Greifefe65362008-05-10 08:32:32 +00002487 setOperand(2, B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002488 }
2489
Devang Patel4d4a5e02008-02-23 01:11:02 +00002490 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002491 assert(i < 2 && "Successor # out of range for invoke!");
2492 return i == 0 ? getNormalDest() : getUnwindDest();
2493 }
2494
Chris Lattner454928e2005-01-29 00:31:36 +00002495 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002496 assert(idx < 2 && "Successor # out of range for invoke!");
Gabor Greifefe65362008-05-10 08:32:32 +00002497 setOperand(idx+1, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002498 }
2499
Chris Lattner454928e2005-01-29 00:31:36 +00002500 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002501
2502 // Methods for support type inquiry through isa, cast, and dyn_cast:
2503 static inline bool classof(const InvokeInst *) { return true; }
2504 static inline bool classof(const Instruction *I) {
2505 return (I->getOpcode() == Instruction::Invoke);
2506 }
2507 static inline bool classof(const Value *V) {
2508 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2509 }
Chris Lattner454928e2005-01-29 00:31:36 +00002510private:
2511 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2512 virtual unsigned getNumSuccessorsV() const;
2513 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002514};
2515
Gabor Greifefe65362008-05-10 08:32:32 +00002516template <>
2517struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2518};
2519
2520template<typename InputIterator>
2521InvokeInst::InvokeInst(Value *Func,
2522 BasicBlock *IfNormal, BasicBlock *IfException,
2523 InputIterator ArgBegin, InputIterator ArgEnd,
2524 unsigned Values,
2525 const std::string &Name, Instruction *InsertBefore)
2526 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2527 ->getElementType())->getReturnType(),
2528 Instruction::Invoke,
2529 OperandTraits<InvokeInst>::op_end(this) - Values,
2530 Values, InsertBefore) {
2531 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
2532 typename std::iterator_traits<InputIterator>::iterator_category());
2533}
2534template<typename InputIterator>
2535InvokeInst::InvokeInst(Value *Func,
2536 BasicBlock *IfNormal, BasicBlock *IfException,
2537 InputIterator ArgBegin, InputIterator ArgEnd,
2538 unsigned Values,
2539 const std::string &Name, BasicBlock *InsertAtEnd)
2540 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2541 ->getElementType())->getReturnType(),
2542 Instruction::Invoke,
2543 OperandTraits<InvokeInst>::op_end(this) - Values,
2544 Values, InsertAtEnd) {
2545 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
2546 typename std::iterator_traits<InputIterator>::iterator_category());
2547}
2548
2549DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002550
2551//===----------------------------------------------------------------------===//
2552// UnwindInst Class
2553//===----------------------------------------------------------------------===//
2554
2555//===---------------------------------------------------------------------------
2556/// UnwindInst - Immediately exit the current function, unwinding the stack
2557/// until an invoke instruction is found.
2558///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002559class UnwindInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002560 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002561public:
Gabor Greif051a9502008-04-06 20:25:17 +00002562 // allocate space for exactly zero operands
2563 void *operator new(size_t s) {
2564 return User::operator new(s, 0);
2565 }
Chris Lattner910c80a2007-02-24 00:55:48 +00002566 explicit UnwindInst(Instruction *InsertBefore = 0);
2567 explicit UnwindInst(BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002568
Chris Lattnerf319e832004-10-15 23:52:05 +00002569 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002570
Chris Lattner454928e2005-01-29 00:31:36 +00002571 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002572
2573 // Methods for support type inquiry through isa, cast, and dyn_cast:
2574 static inline bool classof(const UnwindInst *) { return true; }
2575 static inline bool classof(const Instruction *I) {
2576 return I->getOpcode() == Instruction::Unwind;
2577 }
2578 static inline bool classof(const Value *V) {
2579 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2580 }
Chris Lattner454928e2005-01-29 00:31:36 +00002581private:
2582 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2583 virtual unsigned getNumSuccessorsV() const;
2584 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002585};
2586
Chris Lattner076b3f12004-10-16 18:05:54 +00002587//===----------------------------------------------------------------------===//
2588// UnreachableInst Class
2589//===----------------------------------------------------------------------===//
2590
2591//===---------------------------------------------------------------------------
2592/// UnreachableInst - This function has undefined behavior. In particular, the
2593/// presence of this instruction indicates some higher level knowledge that the
2594/// end of the block cannot be reached.
2595///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002596class UnreachableInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002597 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002598public:
Gabor Greif051a9502008-04-06 20:25:17 +00002599 // allocate space for exactly zero operands
2600 void *operator new(size_t s) {
2601 return User::operator new(s, 0);
2602 }
Chris Lattner910c80a2007-02-24 00:55:48 +00002603 explicit UnreachableInst(Instruction *InsertBefore = 0);
2604 explicit UnreachableInst(BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00002605
2606 virtual UnreachableInst *clone() const;
2607
Chris Lattner454928e2005-01-29 00:31:36 +00002608 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00002609
2610 // Methods for support type inquiry through isa, cast, and dyn_cast:
2611 static inline bool classof(const UnreachableInst *) { return true; }
2612 static inline bool classof(const Instruction *I) {
2613 return I->getOpcode() == Instruction::Unreachable;
2614 }
2615 static inline bool classof(const Value *V) {
2616 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2617 }
Chris Lattner454928e2005-01-29 00:31:36 +00002618private:
2619 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2620 virtual unsigned getNumSuccessorsV() const;
2621 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00002622};
2623
Reid Spencer3da59db2006-11-27 01:05:10 +00002624//===----------------------------------------------------------------------===//
2625// TruncInst Class
2626//===----------------------------------------------------------------------===//
2627
2628/// @brief This class represents a truncation of integer types.
2629class TruncInst : public CastInst {
2630 /// Private copy constructor
2631 TruncInst(const TruncInst &CI)
2632 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2633 }
2634public:
2635 /// @brief Constructor with insert-before-instruction semantics
2636 TruncInst(
2637 Value *S, ///< The value to be truncated
2638 const Type *Ty, ///< The (smaller) type to truncate to
2639 const std::string &Name = "", ///< A name for the new instruction
2640 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2641 );
2642
2643 /// @brief Constructor with insert-at-end-of-block semantics
2644 TruncInst(
2645 Value *S, ///< The value to be truncated
2646 const Type *Ty, ///< The (smaller) type to truncate to
2647 const std::string &Name, ///< A name for the new instruction
2648 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2649 );
2650
2651 /// @brief Clone an identical TruncInst
2652 virtual CastInst *clone() const;
2653
2654 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2655 static inline bool classof(const TruncInst *) { return true; }
2656 static inline bool classof(const Instruction *I) {
2657 return I->getOpcode() == Trunc;
2658 }
2659 static inline bool classof(const Value *V) {
2660 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2661 }
2662};
2663
2664//===----------------------------------------------------------------------===//
2665// ZExtInst Class
2666//===----------------------------------------------------------------------===//
2667
2668/// @brief This class represents zero extension of integer types.
2669class ZExtInst : public CastInst {
2670 /// @brief Private copy constructor
2671 ZExtInst(const ZExtInst &CI)
2672 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2673 }
2674public:
2675 /// @brief Constructor with insert-before-instruction semantics
2676 ZExtInst(
2677 Value *S, ///< The value to be zero extended
2678 const Type *Ty, ///< The type to zero extend to
2679 const std::string &Name = "", ///< A name for the new instruction
2680 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2681 );
2682
2683 /// @brief Constructor with insert-at-end semantics.
2684 ZExtInst(
2685 Value *S, ///< The value to be zero extended
2686 const Type *Ty, ///< The type to zero extend to
2687 const std::string &Name, ///< A name for the new instruction
2688 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2689 );
2690
2691 /// @brief Clone an identical ZExtInst
2692 virtual CastInst *clone() const;
2693
2694 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2695 static inline bool classof(const ZExtInst *) { return true; }
2696 static inline bool classof(const Instruction *I) {
2697 return I->getOpcode() == ZExt;
2698 }
2699 static inline bool classof(const Value *V) {
2700 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2701 }
2702};
2703
2704//===----------------------------------------------------------------------===//
2705// SExtInst Class
2706//===----------------------------------------------------------------------===//
2707
2708/// @brief This class represents a sign extension of integer types.
2709class SExtInst : public CastInst {
2710 /// @brief Private copy constructor
2711 SExtInst(const SExtInst &CI)
2712 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2713 }
2714public:
2715 /// @brief Constructor with insert-before-instruction semantics
2716 SExtInst(
2717 Value *S, ///< The value to be sign extended
2718 const Type *Ty, ///< The type to sign extend to
2719 const std::string &Name = "", ///< A name for the new instruction
2720 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2721 );
2722
2723 /// @brief Constructor with insert-at-end-of-block semantics
2724 SExtInst(
2725 Value *S, ///< The value to be sign extended
2726 const Type *Ty, ///< The type to sign extend to
2727 const std::string &Name, ///< A name for the new instruction
2728 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2729 );
2730
2731 /// @brief Clone an identical SExtInst
2732 virtual CastInst *clone() const;
2733
2734 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2735 static inline bool classof(const SExtInst *) { return true; }
2736 static inline bool classof(const Instruction *I) {
2737 return I->getOpcode() == SExt;
2738 }
2739 static inline bool classof(const Value *V) {
2740 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2741 }
2742};
2743
2744//===----------------------------------------------------------------------===//
2745// FPTruncInst Class
2746//===----------------------------------------------------------------------===//
2747
2748/// @brief This class represents a truncation of floating point types.
2749class FPTruncInst : public CastInst {
2750 FPTruncInst(const FPTruncInst &CI)
2751 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2752 }
2753public:
2754 /// @brief Constructor with insert-before-instruction semantics
2755 FPTruncInst(
2756 Value *S, ///< The value to be truncated
2757 const Type *Ty, ///< The type to truncate to
2758 const std::string &Name = "", ///< A name for the new instruction
2759 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2760 );
2761
2762 /// @brief Constructor with insert-before-instruction semantics
2763 FPTruncInst(
2764 Value *S, ///< The value to be truncated
2765 const Type *Ty, ///< The type to truncate to
2766 const std::string &Name, ///< A name for the new instruction
2767 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2768 );
2769
2770 /// @brief Clone an identical FPTruncInst
2771 virtual CastInst *clone() const;
2772
2773 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2774 static inline bool classof(const FPTruncInst *) { return true; }
2775 static inline bool classof(const Instruction *I) {
2776 return I->getOpcode() == FPTrunc;
2777 }
2778 static inline bool classof(const Value *V) {
2779 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2780 }
2781};
2782
2783//===----------------------------------------------------------------------===//
2784// FPExtInst Class
2785//===----------------------------------------------------------------------===//
2786
2787/// @brief This class represents an extension of floating point types.
2788class FPExtInst : public CastInst {
2789 FPExtInst(const FPExtInst &CI)
2790 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2791 }
2792public:
2793 /// @brief Constructor with insert-before-instruction semantics
2794 FPExtInst(
2795 Value *S, ///< The value to be extended
2796 const Type *Ty, ///< The type to extend to
2797 const std::string &Name = "", ///< A name for the new instruction
2798 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2799 );
2800
2801 /// @brief Constructor with insert-at-end-of-block semantics
2802 FPExtInst(
2803 Value *S, ///< The value to be extended
2804 const Type *Ty, ///< The type to extend to
2805 const std::string &Name, ///< A name for the new instruction
2806 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2807 );
2808
2809 /// @brief Clone an identical FPExtInst
2810 virtual CastInst *clone() const;
2811
2812 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2813 static inline bool classof(const FPExtInst *) { return true; }
2814 static inline bool classof(const Instruction *I) {
2815 return I->getOpcode() == FPExt;
2816 }
2817 static inline bool classof(const Value *V) {
2818 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2819 }
2820};
2821
2822//===----------------------------------------------------------------------===//
2823// UIToFPInst Class
2824//===----------------------------------------------------------------------===//
2825
2826/// @brief This class represents a cast unsigned integer to floating point.
2827class UIToFPInst : public CastInst {
2828 UIToFPInst(const UIToFPInst &CI)
2829 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2830 }
2831public:
2832 /// @brief Constructor with insert-before-instruction semantics
2833 UIToFPInst(
2834 Value *S, ///< The value to be converted
2835 const Type *Ty, ///< The type to convert to
2836 const std::string &Name = "", ///< A name for the new instruction
2837 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2838 );
2839
2840 /// @brief Constructor with insert-at-end-of-block semantics
2841 UIToFPInst(
2842 Value *S, ///< The value to be converted
2843 const Type *Ty, ///< The type to convert to
2844 const std::string &Name, ///< A name for the new instruction
2845 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2846 );
2847
2848 /// @brief Clone an identical UIToFPInst
2849 virtual CastInst *clone() const;
2850
2851 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2852 static inline bool classof(const UIToFPInst *) { return true; }
2853 static inline bool classof(const Instruction *I) {
2854 return I->getOpcode() == UIToFP;
2855 }
2856 static inline bool classof(const Value *V) {
2857 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2858 }
2859};
2860
2861//===----------------------------------------------------------------------===//
2862// SIToFPInst Class
2863//===----------------------------------------------------------------------===//
2864
2865/// @brief This class represents a cast from signed integer to floating point.
2866class SIToFPInst : public CastInst {
2867 SIToFPInst(const SIToFPInst &CI)
2868 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2869 }
2870public:
2871 /// @brief Constructor with insert-before-instruction semantics
2872 SIToFPInst(
2873 Value *S, ///< The value to be converted
2874 const Type *Ty, ///< The type to convert to
2875 const std::string &Name = "", ///< A name for the new instruction
2876 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2877 );
2878
2879 /// @brief Constructor with insert-at-end-of-block semantics
2880 SIToFPInst(
2881 Value *S, ///< The value to be converted
2882 const Type *Ty, ///< The type to convert to
2883 const std::string &Name, ///< A name for the new instruction
2884 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2885 );
2886
2887 /// @brief Clone an identical SIToFPInst
2888 virtual CastInst *clone() const;
2889
2890 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2891 static inline bool classof(const SIToFPInst *) { return true; }
2892 static inline bool classof(const Instruction *I) {
2893 return I->getOpcode() == SIToFP;
2894 }
2895 static inline bool classof(const Value *V) {
2896 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2897 }
2898};
2899
2900//===----------------------------------------------------------------------===//
2901// FPToUIInst Class
2902//===----------------------------------------------------------------------===//
2903
2904/// @brief This class represents a cast from floating point to unsigned integer
2905class FPToUIInst : public CastInst {
2906 FPToUIInst(const FPToUIInst &CI)
2907 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2908 }
2909public:
2910 /// @brief Constructor with insert-before-instruction semantics
2911 FPToUIInst(
2912 Value *S, ///< The value to be converted
2913 const Type *Ty, ///< The type to convert to
2914 const std::string &Name = "", ///< A name for the new instruction
2915 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2916 );
2917
2918 /// @brief Constructor with insert-at-end-of-block semantics
2919 FPToUIInst(
2920 Value *S, ///< The value to be converted
2921 const Type *Ty, ///< The type to convert to
2922 const std::string &Name, ///< A name for the new instruction
2923 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2924 );
2925
2926 /// @brief Clone an identical FPToUIInst
2927 virtual CastInst *clone() const;
2928
2929 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2930 static inline bool classof(const FPToUIInst *) { return true; }
2931 static inline bool classof(const Instruction *I) {
2932 return I->getOpcode() == FPToUI;
2933 }
2934 static inline bool classof(const Value *V) {
2935 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2936 }
2937};
2938
2939//===----------------------------------------------------------------------===//
2940// FPToSIInst Class
2941//===----------------------------------------------------------------------===//
2942
2943/// @brief This class represents a cast from floating point to signed integer.
2944class FPToSIInst : public CastInst {
2945 FPToSIInst(const FPToSIInst &CI)
2946 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2947 }
2948public:
2949 /// @brief Constructor with insert-before-instruction semantics
2950 FPToSIInst(
2951 Value *S, ///< The value to be converted
2952 const Type *Ty, ///< The type to convert to
2953 const std::string &Name = "", ///< A name for the new instruction
2954 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2955 );
2956
2957 /// @brief Constructor with insert-at-end-of-block semantics
2958 FPToSIInst(
2959 Value *S, ///< The value to be converted
2960 const Type *Ty, ///< The type to convert to
2961 const std::string &Name, ///< A name for the new instruction
2962 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2963 );
2964
2965 /// @brief Clone an identical FPToSIInst
2966 virtual CastInst *clone() const;
2967
2968 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2969 static inline bool classof(const FPToSIInst *) { return true; }
2970 static inline bool classof(const Instruction *I) {
2971 return I->getOpcode() == FPToSI;
2972 }
2973 static inline bool classof(const Value *V) {
2974 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2975 }
2976};
2977
2978//===----------------------------------------------------------------------===//
2979// IntToPtrInst Class
2980//===----------------------------------------------------------------------===//
2981
2982/// @brief This class represents a cast from an integer to a pointer.
2983class IntToPtrInst : public CastInst {
2984 IntToPtrInst(const IntToPtrInst &CI)
2985 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2986 }
2987public:
2988 /// @brief Constructor with insert-before-instruction semantics
2989 IntToPtrInst(
2990 Value *S, ///< The value to be converted
2991 const Type *Ty, ///< The type to convert to
2992 const std::string &Name = "", ///< A name for the new instruction
2993 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2994 );
2995
2996 /// @brief Constructor with insert-at-end-of-block semantics
2997 IntToPtrInst(
2998 Value *S, ///< The value to be converted
2999 const Type *Ty, ///< The type to convert to
3000 const std::string &Name, ///< A name for the new instruction
3001 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3002 );
3003
3004 /// @brief Clone an identical IntToPtrInst
3005 virtual CastInst *clone() const;
3006
3007 // Methods for support type inquiry through isa, cast, and dyn_cast:
3008 static inline bool classof(const IntToPtrInst *) { return true; }
3009 static inline bool classof(const Instruction *I) {
3010 return I->getOpcode() == IntToPtr;
3011 }
3012 static inline bool classof(const Value *V) {
3013 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3014 }
3015};
3016
3017//===----------------------------------------------------------------------===//
3018// PtrToIntInst Class
3019//===----------------------------------------------------------------------===//
3020
3021/// @brief This class represents a cast from a pointer to an integer
3022class PtrToIntInst : public CastInst {
3023 PtrToIntInst(const PtrToIntInst &CI)
3024 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3025 }
3026public:
3027 /// @brief Constructor with insert-before-instruction semantics
3028 PtrToIntInst(
3029 Value *S, ///< The value to be converted
3030 const Type *Ty, ///< The type to convert to
3031 const std::string &Name = "", ///< A name for the new instruction
3032 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3033 );
3034
3035 /// @brief Constructor with insert-at-end-of-block semantics
3036 PtrToIntInst(
3037 Value *S, ///< The value to be converted
3038 const Type *Ty, ///< The type to convert to
3039 const std::string &Name, ///< A name for the new instruction
3040 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3041 );
3042
3043 /// @brief Clone an identical PtrToIntInst
3044 virtual CastInst *clone() const;
3045
3046 // Methods for support type inquiry through isa, cast, and dyn_cast:
3047 static inline bool classof(const PtrToIntInst *) { return true; }
3048 static inline bool classof(const Instruction *I) {
3049 return I->getOpcode() == PtrToInt;
3050 }
3051 static inline bool classof(const Value *V) {
3052 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3053 }
3054};
3055
3056//===----------------------------------------------------------------------===//
3057// BitCastInst Class
3058//===----------------------------------------------------------------------===//
3059
3060/// @brief This class represents a no-op cast from one type to another.
3061class BitCastInst : public CastInst {
3062 BitCastInst(const BitCastInst &CI)
3063 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3064 }
3065public:
3066 /// @brief Constructor with insert-before-instruction semantics
3067 BitCastInst(
3068 Value *S, ///< The value to be casted
3069 const Type *Ty, ///< The type to casted to
3070 const std::string &Name = "", ///< A name for the new instruction
3071 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3072 );
3073
3074 /// @brief Constructor with insert-at-end-of-block semantics
3075 BitCastInst(
3076 Value *S, ///< The value to be casted
3077 const Type *Ty, ///< The type to casted to
3078 const std::string &Name, ///< A name for the new instruction
3079 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3080 );
3081
3082 /// @brief Clone an identical BitCastInst
3083 virtual CastInst *clone() const;
3084
3085 // Methods for support type inquiry through isa, cast, and dyn_cast:
3086 static inline bool classof(const BitCastInst *) { return true; }
3087 static inline bool classof(const Instruction *I) {
3088 return I->getOpcode() == BitCast;
3089 }
3090 static inline bool classof(const Value *V) {
3091 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3092 }
3093};
3094
Devang Patel40a04212008-02-19 22:15:16 +00003095//===----------------------------------------------------------------------===//
3096// GetResultInst Class
3097//===----------------------------------------------------------------------===//
3098
3099/// GetResultInst - This instruction extracts individual result value from
3100/// aggregate value, where aggregate value is returned by CallInst.
3101///
Gabor Greifd6a22182008-05-13 07:09:08 +00003102class GetResultInst : public UnaryInstruction {
Devang Patel23755d82008-02-20 19:10:47 +00003103 unsigned Idx;
Devang Patel40a04212008-02-19 22:15:16 +00003104 GetResultInst(const GetResultInst &GRI) :
Gabor Greifd6a22182008-05-13 07:09:08 +00003105 UnaryInstruction(GRI.getType(), Instruction::GetResult, GRI.getOperand(0)),
3106 Idx(GRI.Idx) {
Devang Patel40a04212008-02-19 22:15:16 +00003107 }
3108
3109public:
Gabor Greifefe65362008-05-10 08:32:32 +00003110 GetResultInst(Value *Aggr, unsigned index,
3111 const std::string &Name = "",
3112 Instruction *InsertBefore = 0);
Devang Patel40a04212008-02-19 22:15:16 +00003113
3114 /// isValidOperands - Return true if an getresult instruction can be
3115 /// formed with the specified operands.
Devang Patel23755d82008-02-20 19:10:47 +00003116 static bool isValidOperands(const Value *Aggr, unsigned index);
Devang Patel40a04212008-02-19 22:15:16 +00003117
3118 virtual GetResultInst *clone() const;
3119
Devang Patel4d4a5e02008-02-23 01:11:02 +00003120 Value *getAggregateValue() {
Devang Patel40a04212008-02-19 22:15:16 +00003121 return getOperand(0);
3122 }
Devang Patel2d2ae342008-02-20 18:36:16 +00003123
Devang Patel4d4a5e02008-02-23 01:11:02 +00003124 const Value *getAggregateValue() const {
Devang Patel2d2ae342008-02-20 18:36:16 +00003125 return getOperand(0);
3126 }
3127
Devang Patel4d4a5e02008-02-23 01:11:02 +00003128 unsigned getIndex() const {
Devang Patel23755d82008-02-20 19:10:47 +00003129 return Idx;
Devang Patel40a04212008-02-19 22:15:16 +00003130 }
3131
Devang Patel40a04212008-02-19 22:15:16 +00003132 // Methods for support type inquiry through isa, cast, and dyn_cast:
3133 static inline bool classof(const GetResultInst *) { return true; }
3134 static inline bool classof(const Instruction *I) {
3135 return (I->getOpcode() == Instruction::GetResult);
3136 }
3137 static inline bool classof(const Value *V) {
3138 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3139 }
3140};
3141
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003142} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00003143
3144#endif