blob: 1bd2cba21955be8776a5f46ed86e7f556cd3db0a [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 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000407 /// Null is returned if the indices are invalid for the specified
David Greeneb8f74792007-09-04 15:46:09 +0000408 /// 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 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000500 /// Null 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; }
Eric Christopher0bf7b412008-05-16 20:39:43 +00001112
1113 /// addParamAttr - adds the attribute to the list of attributes.
1114 void addParamAttr(unsigned i, ParameterAttributes attr);
Duncan Sandsdc024672007-11-27 13:23:08 +00001115
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001116 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001117 bool paramHasAttr(unsigned i, unsigned attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001118
Dale Johannesen08e78b12008-02-22 17:49:45 +00001119 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001120 unsigned getParamAlignment(unsigned i) const {
1121 return ParamAttrs.getParamAlignment(i);
1122 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001123
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001124 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001125 bool doesNotAccessMemory() const {
1126 return paramHasAttr(0, ParamAttr::ReadNone);
1127 }
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001128
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001129 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001130 bool onlyReadsMemory() const {
1131 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
1132 }
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001133
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001134 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001135 bool doesNotReturn() const {
1136 return paramHasAttr(0, ParamAttr::NoReturn);
1137 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001138
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001139 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001140 bool doesNotThrow() const {
1141 return paramHasAttr(0, ParamAttr::NoUnwind);
1142 }
Duncan Sandsf0c33542007-12-19 21:13:37 +00001143 void setDoesNotThrow(bool doesNotThrow = true);
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001144
Devang Patel41e23972008-03-03 21:46:28 +00001145 /// @brief Determine if the call returns a structure through first
1146 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001147 bool hasStructRetAttr() const {
1148 // Be friendly and also check the callee.
1149 return paramHasAttr(1, ParamAttr::StructRet);
1150 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001151
Evan Chengf4a54982008-01-12 18:57:32 +00001152 /// @brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001153 bool hasByValArgument() const {
1154 return ParamAttrs.hasAttrSomewhere(ParamAttr::ByVal);
1155 }
Evan Chengf4a54982008-01-12 18:57:32 +00001156
Chris Lattner721aef62004-11-18 17:46:57 +00001157 /// getCalledFunction - Return the function being called by this instruction
1158 /// if it is a direct call. If it is a call through a function pointer,
1159 /// return null.
1160 Function *getCalledFunction() const {
Dan Gohman11a7dbf2007-09-24 15:46:02 +00001161 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001162 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001163
Reid Spencerc25ec252006-12-29 04:10:59 +00001164 /// getCalledValue - Get a pointer to the function that is invoked by this
1165 /// instruction
Devang Patel4d4a5e02008-02-23 01:11:02 +00001166 const Value *getCalledValue() const { return getOperand(0); }
1167 Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001168
1169 // Methods for support type inquiry through isa, cast, and dyn_cast:
1170 static inline bool classof(const CallInst *) { return true; }
1171 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001172 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001173 }
1174 static inline bool classof(const Value *V) {
1175 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1176 }
1177};
1178
Gabor Greifefe65362008-05-10 08:32:32 +00001179template <>
1180struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1181};
1182
1183template<typename InputIterator>
1184CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1185 const std::string &Name, BasicBlock *InsertAtEnd)
1186 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1187 ->getElementType())->getReturnType(),
1188 Instruction::Call,
1189 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001190 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00001191 init(Func, ArgBegin, ArgEnd, Name,
1192 typename std::iterator_traits<InputIterator>::iterator_category());
1193}
1194
1195template<typename InputIterator>
1196CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1197 const std::string &Name, Instruction *InsertBefore)
1198 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1199 ->getElementType())->getReturnType(),
1200 Instruction::Call,
1201 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001202 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
Gabor Greifefe65362008-05-10 08:32:32 +00001203 init(Func, ArgBegin, ArgEnd, Name,
1204 typename std::iterator_traits<InputIterator>::iterator_category());
1205}
1206
1207DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1208
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001209//===----------------------------------------------------------------------===//
1210// SelectInst Class
1211//===----------------------------------------------------------------------===//
1212
1213/// SelectInst - This class represents the LLVM 'select' instruction.
1214///
1215class SelectInst : public Instruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001216 void init(Value *C, Value *S1, Value *S2) {
Gabor Greifefe65362008-05-10 08:32:32 +00001217 Op<0>() = C;
1218 Op<1>() = S1;
1219 Op<2>() = S2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001220 }
1221
Chris Lattner454928e2005-01-29 00:31:36 +00001222 SelectInst(const SelectInst &SI)
Gabor Greifefe65362008-05-10 08:32:32 +00001223 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1224 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
Chris Lattner454928e2005-01-29 00:31:36 +00001225 }
Gabor Greifefe65362008-05-10 08:32:32 +00001226 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1227 Instruction *InsertBefore)
1228 : Instruction(S1->getType(), Instruction::Select,
1229 &Op<0>(), 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001230 init(C, S1, S2);
Chris Lattner910c80a2007-02-24 00:55:48 +00001231 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001232 }
1233 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1234 BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001235 : Instruction(S1->getType(), Instruction::Select,
1236 &Op<0>(), 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001237 init(C, S1, S2);
Chris Lattner910c80a2007-02-24 00:55:48 +00001238 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001239 }
Gabor Greif051a9502008-04-06 20:25:17 +00001240public:
Evan Chengd69bb1a2008-05-05 17:41:03 +00001241 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1242 const std::string &Name = "",
1243 Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00001244 return new(3) SelectInst(C, S1, S2, Name, InsertBefore);
1245 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001246 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1247 const std::string &Name, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001248 return new(3) SelectInst(C, S1, S2, Name, InsertAtEnd);
1249 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001250
Gabor Greifefe65362008-05-10 08:32:32 +00001251 Value *getCondition() const { return Op<0>(); }
1252 Value *getTrueValue() const { return Op<1>(); }
1253 Value *getFalseValue() const { return Op<2>(); }
Chris Lattner454928e2005-01-29 00:31:36 +00001254
1255 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001256 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001257
1258 OtherOps getOpcode() const {
1259 return static_cast<OtherOps>(Instruction::getOpcode());
1260 }
1261
Chris Lattnerf319e832004-10-15 23:52:05 +00001262 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001263
1264 // Methods for support type inquiry through isa, cast, and dyn_cast:
1265 static inline bool classof(const SelectInst *) { return true; }
1266 static inline bool classof(const Instruction *I) {
1267 return I->getOpcode() == Instruction::Select;
1268 }
1269 static inline bool classof(const Value *V) {
1270 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1271 }
1272};
1273
Gabor Greifefe65362008-05-10 08:32:32 +00001274template <>
1275struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1276};
1277
1278DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1279
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001280//===----------------------------------------------------------------------===//
1281// VAArgInst Class
1282//===----------------------------------------------------------------------===//
1283
1284/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001285/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001286///
Chris Lattner454928e2005-01-29 00:31:36 +00001287class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001288 VAArgInst(const VAArgInst &VAA)
Chris Lattner454928e2005-01-29 00:31:36 +00001289 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001290public:
1291 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
1292 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001293 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001294 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001295 }
1296 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
1297 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001298 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001299 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001300 }
1301
Chris Lattnerf319e832004-10-15 23:52:05 +00001302 virtual VAArgInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001303
1304 // Methods for support type inquiry through isa, cast, and dyn_cast:
1305 static inline bool classof(const VAArgInst *) { return true; }
1306 static inline bool classof(const Instruction *I) {
1307 return I->getOpcode() == VAArg;
1308 }
1309 static inline bool classof(const Value *V) {
1310 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1311 }
1312};
1313
1314//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001315// ExtractElementInst Class
1316//===----------------------------------------------------------------------===//
1317
1318/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001319/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001320///
1321class ExtractElementInst : public Instruction {
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001322 ExtractElementInst(const ExtractElementInst &EE) :
Gabor Greifefe65362008-05-10 08:32:32 +00001323 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1324 Op<0>().init(EE.Op<0>(), this);
1325 Op<1>().init(EE.Op<1>(), this);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001326 }
1327
1328public:
Gabor Greif051a9502008-04-06 20:25:17 +00001329 // allocate space for exactly two operands
1330 void *operator new(size_t s) {
Gabor Greifefe65362008-05-10 08:32:32 +00001331 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
Gabor Greif051a9502008-04-06 20:25:17 +00001332 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001333 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
1334 Instruction *InsertBefore = 0);
Chris Lattner06a248c22006-10-05 06:24:58 +00001335 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
1336 Instruction *InsertBefore = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001337 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
1338 BasicBlock *InsertAtEnd);
Chris Lattner06a248c22006-10-05 06:24:58 +00001339 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
1340 BasicBlock *InsertAtEnd);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001341
Chris Lattnerfa495842006-04-08 04:04:54 +00001342 /// isValidOperands - Return true if an extractelement instruction can be
1343 /// formed with the specified operands.
1344 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001345
Robert Bocchino49b78a52006-01-10 19:04:13 +00001346 virtual ExtractElementInst *clone() const;
1347
Robert Bocchino49b78a52006-01-10 19:04:13 +00001348 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001349 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001350
1351 // Methods for support type inquiry through isa, cast, and dyn_cast:
1352 static inline bool classof(const ExtractElementInst *) { return true; }
1353 static inline bool classof(const Instruction *I) {
1354 return I->getOpcode() == Instruction::ExtractElement;
1355 }
1356 static inline bool classof(const Value *V) {
1357 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1358 }
1359};
1360
Gabor Greifefe65362008-05-10 08:32:32 +00001361template <>
1362struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1363};
1364
1365DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1366
Robert Bocchino49b78a52006-01-10 19:04:13 +00001367//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001368// InsertElementInst Class
1369//===----------------------------------------------------------------------===//
1370
1371/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001372/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001373///
1374class InsertElementInst : public Instruction {
Chris Lattner6a56ed42006-04-14 22:20:07 +00001375 InsertElementInst(const InsertElementInst &IE);
Gabor Greif051a9502008-04-06 20:25:17 +00001376 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1377 const std::string &Name = "",Instruction *InsertBefore = 0);
1378 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1379 const std::string &Name = "",Instruction *InsertBefore = 0);
1380 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1381 const std::string &Name, BasicBlock *InsertAtEnd);
1382 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1383 const std::string &Name, BasicBlock *InsertAtEnd);
Robert Bocchinof9993442006-01-17 20:05:59 +00001384public:
Gabor Greif051a9502008-04-06 20:25:17 +00001385 static InsertElementInst *Create(const InsertElementInst &IE) {
1386 return new(IE.getNumOperands()) InsertElementInst(IE);
1387 }
1388 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Gabor Greifefe65362008-05-10 08:32:32 +00001389 const std::string &Name = "",
1390 Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00001391 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
1392 }
1393 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001394 const std::string &Name = "",
1395 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +00001396 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001397 }
1398 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001399 const std::string &Name,
1400 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001401 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertAtEnd);
1402 }
1403 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001404 const std::string &Name,
1405 BasicBlock *InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00001406 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001407 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001408
Chris Lattnerfa495842006-04-08 04:04:54 +00001409 /// isValidOperands - Return true if an insertelement instruction can be
1410 /// formed with the specified operands.
1411 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1412 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001413
Robert Bocchinof9993442006-01-17 20:05:59 +00001414 virtual InsertElementInst *clone() const;
1415
Reid Spencerac9dcb92007-02-15 03:39:18 +00001416 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001417 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001418 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001419 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001420 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001421
Robert Bocchinof9993442006-01-17 20:05:59 +00001422 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001423 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchinof9993442006-01-17 20:05:59 +00001424
1425 // Methods for support type inquiry through isa, cast, and dyn_cast:
1426 static inline bool classof(const InsertElementInst *) { return true; }
1427 static inline bool classof(const Instruction *I) {
1428 return I->getOpcode() == Instruction::InsertElement;
1429 }
1430 static inline bool classof(const Value *V) {
1431 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1432 }
1433};
1434
Gabor Greifefe65362008-05-10 08:32:32 +00001435template <>
1436struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1437};
1438
1439DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1440
Robert Bocchinof9993442006-01-17 20:05:59 +00001441//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001442// ShuffleVectorInst Class
1443//===----------------------------------------------------------------------===//
1444
1445/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1446/// input vectors.
1447///
1448class ShuffleVectorInst : public Instruction {
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001449 ShuffleVectorInst(const ShuffleVectorInst &IE);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001450public:
Gabor Greif051a9502008-04-06 20:25:17 +00001451 // allocate space for exactly three operands
1452 void *operator new(size_t s) {
1453 return User::operator new(s, 3);
1454 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001455 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1456 const std::string &Name = "", Instruction *InsertBefor = 0);
1457 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1458 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001459
Chris Lattnerfa495842006-04-08 04:04:54 +00001460 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001461 /// formed with the specified operands.
1462 static bool isValidOperands(const Value *V1, const Value *V2,
1463 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001464
Chris Lattner9fc18d22006-04-08 01:15:18 +00001465 virtual ShuffleVectorInst *clone() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001466
Reid Spencerac9dcb92007-02-15 03:39:18 +00001467 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001468 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001469 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001470 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001471 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001472
Chris Lattner9fc18d22006-04-08 01:15:18 +00001473 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001474 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner8728f192008-03-02 05:28:33 +00001475
1476 /// getMaskValue - Return the index from the shuffle mask for the specified
1477 /// output result. This is either -1 if the element is undef or a number less
1478 /// than 2*numelements.
1479 int getMaskValue(unsigned i) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001480
Chris Lattner9fc18d22006-04-08 01:15:18 +00001481 // Methods for support type inquiry through isa, cast, and dyn_cast:
1482 static inline bool classof(const ShuffleVectorInst *) { return true; }
1483 static inline bool classof(const Instruction *I) {
1484 return I->getOpcode() == Instruction::ShuffleVector;
1485 }
1486 static inline bool classof(const Value *V) {
1487 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1488 }
1489};
1490
Gabor Greifefe65362008-05-10 08:32:32 +00001491template <>
1492struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1493};
1494
1495DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
Chris Lattner9fc18d22006-04-08 01:15:18 +00001496
1497//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001498// ExtractValueInst Class
1499//===----------------------------------------------------------------------===//
1500
Dan Gohmane2d896f2008-05-15 23:35:32 +00001501/// ExtractValueInst - This instruction extracts a struct member or array
1502/// element value from an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001503///
1504class ExtractValueInst : public Instruction {
1505 ExtractValueInst(const ExtractValueInst &EVI);
1506 void init(Value *Agg, Value* const *Idx, unsigned NumIdx);
1507 void init(Value *Agg, Value *Idx);
1508
1509 template<typename InputIterator>
1510 void init(Value *Agg, InputIterator IdxBegin, InputIterator IdxEnd,
1511 const std::string &Name,
1512 // This argument ensures that we have an iterator we can
1513 // do arithmetic on in constant time
1514 std::random_access_iterator_tag) {
1515 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1516
1517 if (NumIdx > 0) {
1518 // This requires that the iterator points to contiguous memory.
1519 init(Agg, &*IdxBegin, NumIdx); // FIXME: for the general case
1520 // we have to build an array here
1521 }
1522 else {
1523 init(Agg, 0, NumIdx);
1524 }
1525
1526 setName(Name);
1527 }
1528
1529 /// getIndexedType - Returns the type of the element that would be extracted
1530 /// with an extractvalue instruction with the specified parameters.
1531 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +00001532 /// Null is returned if the indices are invalid for the specified
Dan Gohman041e2eb2008-05-15 19:50:34 +00001533 /// pointer type.
1534 ///
1535 static const Type *getIndexedType(const Type *Agg,
1536 Value* const *Idx, unsigned NumIdx);
1537
1538 template<typename InputIterator>
1539 static const Type *getIndexedType(const Type *Ptr,
1540 InputIterator IdxBegin,
1541 InputIterator IdxEnd,
1542 // This argument ensures that we
1543 // have an iterator we can do
1544 // arithmetic on in constant time
1545 std::random_access_iterator_tag) {
1546 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1547
1548 if (NumIdx > 0)
1549 // This requires that the iterator points to contiguous memory.
1550 return getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx);
1551 else
1552 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
1553 }
1554
Dan Gohmane2d896f2008-05-15 23:35:32 +00001555 /// Constructors - Create a extractvalue instruction with a base aggregate
1556 /// value and a list of indices. The first ctor can optionally insert before
1557 /// an existing instruction, the second appends the new instruction to the
1558 /// specified BasicBlock.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001559 template<typename InputIterator>
1560 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1561 InputIterator IdxEnd,
1562 unsigned Values,
1563 const std::string &Name,
1564 Instruction *InsertBefore);
1565 template<typename InputIterator>
1566 inline ExtractValueInst(Value *Agg,
1567 InputIterator IdxBegin, InputIterator IdxEnd,
1568 unsigned Values,
1569 const std::string &Name, BasicBlock *InsertAtEnd);
1570
1571 /// Constructors - These two constructors are convenience methods because one
1572 /// and two index extractvalue instructions are so common.
1573 ExtractValueInst(Value *Agg, Value *Idx, const std::string &Name = "",
1574 Instruction *InsertBefore = 0);
1575 ExtractValueInst(Value *Agg, Value *Idx,
1576 const std::string &Name, BasicBlock *InsertAtEnd);
1577public:
1578 template<typename InputIterator>
1579 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1580 InputIterator IdxEnd,
1581 const std::string &Name = "",
1582 Instruction *InsertBefore = 0) {
1583 typename std::iterator_traits<InputIterator>::difference_type Values =
1584 1 + std::distance(IdxBegin, IdxEnd);
1585 return new(Values)
1586 ExtractValueInst(Agg, IdxBegin, IdxEnd, Values, Name, InsertBefore);
1587 }
1588 template<typename InputIterator>
1589 static ExtractValueInst *Create(Value *Agg,
1590 InputIterator IdxBegin, InputIterator IdxEnd,
1591 const std::string &Name,
1592 BasicBlock *InsertAtEnd) {
1593 typename std::iterator_traits<InputIterator>::difference_type Values =
1594 1 + std::distance(IdxBegin, IdxEnd);
1595 return new(Values)
1596 ExtractValueInst(Agg, IdxBegin, IdxEnd, Values, Name, InsertAtEnd);
1597 }
1598
1599 /// Constructors - These two creators are convenience methods because one
1600 /// index extractvalue instructions are much more common than those with
1601 /// more than one.
1602 static ExtractValueInst *Create(Value *Agg, Value *Idx,
1603 const std::string &Name = "",
1604 Instruction *InsertBefore = 0) {
1605 return new(2) ExtractValueInst(Agg, Idx, Name, InsertBefore);
1606 }
1607 static ExtractValueInst *Create(Value *Agg, Value *Idx,
1608 const std::string &Name,
1609 BasicBlock *InsertAtEnd) {
1610 return new(2) ExtractValueInst(Agg, Idx, Name, InsertAtEnd);
1611 }
1612
1613 virtual ExtractValueInst *clone() const;
1614
1615 /// Transparently provide more efficient getOperand methods.
1616 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1617
1618 // getType - Overload to return most specific pointer type...
1619 const PointerType *getType() const {
1620 return reinterpret_cast<const PointerType*>(Instruction::getType());
1621 }
1622
1623 /// getIndexedType - Returns the type of the element that would be extracted
1624 /// with an extractvalue instruction with the specified parameters.
1625 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +00001626 /// Null is returned if the indices are invalid for the specified
Dan Gohman041e2eb2008-05-15 19:50:34 +00001627 /// pointer type.
1628 ///
1629 template<typename InputIterator>
1630 static const Type *getIndexedType(const Type *Ptr,
1631 InputIterator IdxBegin,
1632 InputIterator IdxEnd) {
1633 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1634 typename std::iterator_traits<InputIterator>::
1635 iterator_category());
1636 }
1637 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
1638
1639 inline op_iterator idx_begin() { return op_begin()+1; }
1640 inline const_op_iterator idx_begin() const { return op_begin()+1; }
1641 inline op_iterator idx_end() { return op_end(); }
1642 inline const_op_iterator idx_end() const { return op_end(); }
1643
1644 Value *getAggregateOperand() {
1645 return getOperand(0);
1646 }
1647 const Value *getAggregateOperand() const {
1648 return getOperand(0);
1649 }
1650 static unsigned getAggregateOperandIndex() {
1651 return 0U; // get index for modifying correct operand
1652 }
1653
1654 unsigned getNumIndices() const { // Note: always non-negative
1655 return getNumOperands() - 1;
1656 }
1657
1658 bool hasIndices() const {
1659 return getNumOperands() > 1;
1660 }
1661
1662 // Methods for support type inquiry through isa, cast, and dyn_cast:
1663 static inline bool classof(const ExtractValueInst *) { return true; }
1664 static inline bool classof(const Instruction *I) {
1665 return I->getOpcode() == Instruction::ExtractValue;
1666 }
1667 static inline bool classof(const Value *V) {
1668 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1669 }
1670};
1671
1672template <>
1673struct OperandTraits<ExtractValueInst> : VariadicOperandTraits<1> {
1674};
1675
1676DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueInst, Value)
1677
1678//===----------------------------------------------------------------------===//
1679// InsertValueInst Class
1680//===----------------------------------------------------------------------===//
1681
Dan Gohmane2d896f2008-05-15 23:35:32 +00001682/// InsertValueInst - This instruction inserts a struct field of array element
1683/// value into an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001684///
1685class InsertValueInst : public Instruction {
1686 InsertValueInst(const InsertValueInst &IVI);
1687 void init(Value *Agg, Value *Val, Value* const *Idx, unsigned NumIdx);
1688 void init(Value *Agg, Value *Val, Value *Idx);
1689
1690 template<typename InputIterator>
1691 void init(Value *Agg, Value *Val,
1692 InputIterator IdxBegin, InputIterator IdxEnd,
1693 const std::string &Name,
1694 // This argument ensures that we have an iterator we can
1695 // do arithmetic on in constant time
1696 std::random_access_iterator_tag) {
1697 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1698
1699 if (NumIdx > 0) {
1700 // This requires that the iterator points to contiguous memory.
1701 init(Agg, Val, &*IdxBegin, NumIdx); // FIXME: for the general case
1702 // we have to build an array here
1703 }
1704 else {
1705 init(Agg, Val, 0, NumIdx);
1706 }
1707
1708 setName(Name);
1709 }
1710
Dan Gohmane2d896f2008-05-15 23:35:32 +00001711 /// Constructors - Create a insertvalue instruction with a base aggregate
1712 /// value, a value to insert, and a list of indices. The first ctor can
1713 /// optionally insert before an existing instruction, the second appends
1714 /// the new instruction to the specified BasicBlock.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001715 template<typename InputIterator>
1716 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001717 InputIterator IdxEnd,
1718 unsigned Values,
1719 const std::string &Name,
1720 Instruction *InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001721 template<typename InputIterator>
1722 inline InsertValueInst(Value *Agg, Value *Val,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001723 InputIterator IdxBegin, InputIterator IdxEnd,
1724 unsigned Values,
1725 const std::string &Name, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001726
1727 /// Constructors - These two constructors are convenience methods because one
1728 /// and two index insertvalue instructions are so common.
1729 InsertValueInst(Value *Agg, Value *Val,
1730 Value *Idx, const std::string &Name = "",
1731 Instruction *InsertBefore = 0);
1732 InsertValueInst(Value *Agg, Value *Val, Value *Idx,
1733 const std::string &Name, BasicBlock *InsertAtEnd);
1734public:
1735 template<typename InputIterator>
1736 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1737 InputIterator IdxEnd,
1738 const std::string &Name = "",
1739 Instruction *InsertBefore = 0) {
1740 typename std::iterator_traits<InputIterator>::difference_type Values =
1741 1 + std::distance(IdxBegin, IdxEnd);
1742 return new(Values)
1743 InsertValueInst(Agg, Val, IdxBegin, IdxEnd, Values, Name, InsertBefore);
1744 }
1745 template<typename InputIterator>
1746 static InsertValueInst *Create(Value *Agg, Value *Val,
1747 InputIterator IdxBegin, InputIterator IdxEnd,
1748 const std::string &Name,
1749 BasicBlock *InsertAtEnd) {
1750 typename std::iterator_traits<InputIterator>::difference_type Values =
1751 1 + std::distance(IdxBegin, IdxEnd);
1752 return new(Values)
1753 InsertValueInst(Agg, Val, IdxBegin, IdxEnd, Values, Name, InsertAtEnd);
1754 }
1755
1756 /// Constructors - These two creators are convenience methods because one
1757 /// index insertvalue instructions are much more common than those with
1758 /// more than one.
1759 static InsertValueInst *Create(Value *Agg, Value *Val, Value *Idx,
1760 const std::string &Name = "",
1761 Instruction *InsertBefore = 0) {
1762 return new(3) InsertValueInst(Agg, Val, Idx, Name, InsertBefore);
1763 }
1764 static InsertValueInst *Create(Value *Agg, Value *Val, Value *Idx,
1765 const std::string &Name,
1766 BasicBlock *InsertAtEnd) {
1767 return new(3) InsertValueInst(Agg, Val, Idx, Name, InsertAtEnd);
1768 }
1769
1770 virtual InsertValueInst *clone() const;
1771
1772 /// Transparently provide more efficient getOperand methods.
1773 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1774
1775 // getType - Overload to return most specific pointer type...
1776 const PointerType *getType() const {
1777 return reinterpret_cast<const PointerType*>(Instruction::getType());
1778 }
1779
1780 inline op_iterator idx_begin() { return op_begin()+1; }
1781 inline const_op_iterator idx_begin() const { return op_begin()+1; }
1782 inline op_iterator idx_end() { return op_end(); }
1783 inline const_op_iterator idx_end() const { return op_end(); }
1784
1785 Value *getAggregateOperand() {
1786 return getOperand(0);
1787 }
1788 const Value *getAggregateOperand() const {
1789 return getOperand(0);
1790 }
1791 static unsigned getAggregateOperandIndex() {
1792 return 0U; // get index for modifying correct operand
1793 }
1794
1795 Value *getInsertedValueOperand() {
1796 return getOperand(1);
1797 }
1798 const Value *getInsertedValueOperand() const {
1799 return getOperand(1);
1800 }
1801 static unsigned getInsertedValueOperandIndex() {
1802 return 1U; // get index for modifying correct operand
1803 }
1804
1805 unsigned getNumIndices() const { // Note: always non-negative
1806 return getNumOperands() - 2;
1807 }
1808
1809 bool hasIndices() const {
1810 return getNumOperands() > 2;
1811 }
1812
1813 // Methods for support type inquiry through isa, cast, and dyn_cast:
1814 static inline bool classof(const InsertValueInst *) { return true; }
1815 static inline bool classof(const Instruction *I) {
1816 return I->getOpcode() == Instruction::InsertValue;
1817 }
1818 static inline bool classof(const Value *V) {
1819 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1820 }
1821};
1822
1823template <>
1824struct OperandTraits<InsertValueInst> : VariadicOperandTraits<2> {
1825};
1826
1827DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1828
1829//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001830// PHINode Class
1831//===----------------------------------------------------------------------===//
1832
1833// PHINode - The PHINode class is used to represent the magical mystical PHI
1834// node, that can not exist in nature, but can be synthesized in a computer
1835// scientist's overactive imagination.
1836//
1837class PHINode : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001838 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00001839 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1840 /// the number actually in use.
1841 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001842 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00001843 // allocate space for exactly zero operands
1844 void *operator new(size_t s) {
1845 return User::operator new(s, 0);
1846 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001847 explicit PHINode(const Type *Ty, const std::string &Name = "",
1848 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001849 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Chris Lattner454928e2005-01-29 00:31:36 +00001850 ReservedSpace(0) {
Chris Lattner910c80a2007-02-24 00:55:48 +00001851 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001852 }
1853
1854 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001855 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Chris Lattner454928e2005-01-29 00:31:36 +00001856 ReservedSpace(0) {
Chris Lattner910c80a2007-02-24 00:55:48 +00001857 setName(Name);
Chris Lattner454928e2005-01-29 00:31:36 +00001858 }
Gabor Greif051a9502008-04-06 20:25:17 +00001859public:
1860 static PHINode *Create(const Type *Ty, const std::string &Name = "",
1861 Instruction *InsertBefore = 0) {
1862 return new PHINode(Ty, Name, InsertBefore);
1863 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001864 static PHINode *Create(const Type *Ty, const std::string &Name,
1865 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001866 return new PHINode(Ty, Name, InsertAtEnd);
1867 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001868 ~PHINode();
1869
Chris Lattner454928e2005-01-29 00:31:36 +00001870 /// reserveOperandSpace - This method can be used to avoid repeated
1871 /// reallocation of PHI operand lists by reserving space for the correct
1872 /// number of operands before adding them. Unlike normal vector reserves,
1873 /// this method can also be used to trim the operand space.
1874 void reserveOperandSpace(unsigned NumValues) {
1875 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001876 }
1877
Chris Lattnerf319e832004-10-15 23:52:05 +00001878 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001879
Gabor Greifefe65362008-05-10 08:32:32 +00001880 /// Provide fast operand accessors
1881 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1882
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001883 /// getNumIncomingValues - Return the number of incoming edges
1884 ///
Chris Lattner454928e2005-01-29 00:31:36 +00001885 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001886
Reid Spencerc773de62006-05-19 19:07:54 +00001887 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001888 ///
1889 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001890 assert(i*2 < getNumOperands() && "Invalid value number!");
1891 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001892 }
1893 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +00001894 assert(i*2 < getNumOperands() && "Invalid value number!");
1895 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001896 }
Chris Lattner454928e2005-01-29 00:31:36 +00001897 unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001898 return i*2;
1899 }
1900
Reid Spencerc773de62006-05-19 19:07:54 +00001901 /// getIncomingBlock - Return incoming basic block number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001902 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00001903 BasicBlock *getIncomingBlock(unsigned i) const {
Gabor Greifefe65362008-05-10 08:32:32 +00001904 return static_cast<BasicBlock*>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001905 }
1906 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Gabor Greifefe65362008-05-10 08:32:32 +00001907 setOperand(i*2+1, BB);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001908 }
1909 unsigned getOperandNumForIncomingBlock(unsigned i) {
1910 return i*2+1;
1911 }
1912
1913 /// addIncoming - Add an incoming value to the end of the PHI list
1914 ///
1915 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00001916 assert(V && "PHI node got a null value!");
1917 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001918 assert(getType() == V->getType() &&
1919 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +00001920 unsigned OpNo = NumOperands;
1921 if (OpNo+2 > ReservedSpace)
1922 resizeOperands(0); // Get more space!
1923 // Initialize some new operands.
1924 NumOperands = OpNo+2;
1925 OperandList[OpNo].init(V, this);
Gabor Greifefe65362008-05-10 08:32:32 +00001926 OperandList[OpNo+1].init(BB, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001927 }
Misha Brukman9769ab22005-04-21 20:19:05 +00001928
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001929 /// removeIncomingValue - Remove an incoming value. This is useful if a
1930 /// predecessor basic block is deleted. The value removed is returned.
1931 ///
1932 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1933 /// is true), the PHI node is destroyed and any uses of it are replaced with
1934 /// dummy values. The only time there should be zero incoming values to a PHI
1935 /// node is when the block is dead, so this strategy is sound.
1936 ///
1937 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1938
Gabor Greifefe65362008-05-10 08:32:32 +00001939 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001940 int Idx = getBasicBlockIndex(BB);
1941 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1942 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1943 }
1944
Misha Brukman9769ab22005-04-21 20:19:05 +00001945 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001946 /// block in the value list for this PHI. Returns -1 if no instance.
1947 ///
1948 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001949 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00001950 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Gabor Greifefe65362008-05-10 08:32:32 +00001951 if (OL[i+1].get() == BB) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001952 return -1;
1953 }
1954
1955 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1956 return getIncomingValue(getBasicBlockIndex(BB));
1957 }
1958
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001959 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00001960 /// same value, return the value, otherwise return null.
1961 ///
Chris Lattner9acbd612005-08-05 00:49:06 +00001962 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001963
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001964 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1965 static inline bool classof(const PHINode *) { return true; }
1966 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001967 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001968 }
1969 static inline bool classof(const Value *V) {
1970 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1971 }
Chris Lattner454928e2005-01-29 00:31:36 +00001972 private:
1973 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001974};
1975
Gabor Greifefe65362008-05-10 08:32:32 +00001976template <>
1977struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1978};
1979
1980DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1981
1982
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001983//===----------------------------------------------------------------------===//
1984// ReturnInst Class
1985//===----------------------------------------------------------------------===//
1986
1987//===---------------------------------------------------------------------------
1988/// ReturnInst - Return a value (possibly void), from a function. Execution
1989/// does not continue in this function any longer.
1990///
1991class ReturnInst : public TerminatorInst {
Chris Lattner910c80a2007-02-24 00:55:48 +00001992 ReturnInst(const ReturnInst &RI);
Devang Patelfea98302008-02-26 19:15:26 +00001993 void init(Value * const* retVals, unsigned N);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001994
Gabor Greif051a9502008-04-06 20:25:17 +00001995private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001996 // ReturnInst constructors:
1997 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001998 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001999 // ReturnInst(Value* X) - 'ret X' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00002000 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002001 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
Gabor Greifefe65362008-05-10 08:32:32 +00002002 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2003 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
Devang Patele6be34a2008-02-27 01:20:54 +00002004 // ReturnInst(Value* X, N) - 'ret X,X+1...X+N-1' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00002005 // ReturnInst(Value* X, N, Inst *I) - 'ret X,X+1...X+N-1', insert before I
2006 // ReturnInst(Value* X, N, BB *B) - 'ret X,X+1...X+N-1', insert @ end of B
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002007 //
2008 // NOTE: If the Value* passed is of type void then the constructor behaves as
2009 // if it was passed NULL.
Chris Lattner910c80a2007-02-24 00:55:48 +00002010 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
2011 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
Gabor Greifefe65362008-05-10 08:32:32 +00002012 ReturnInst(Value * const* retVals, unsigned N, Instruction *InsertBefore = 0);
Devang Patelf4511cd2008-02-26 19:38:17 +00002013 ReturnInst(Value * const* retVals, unsigned N, BasicBlock *InsertAtEnd);
Chris Lattner910c80a2007-02-24 00:55:48 +00002014 explicit ReturnInst(BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002015public:
2016 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
2017 return new(!!retVal) ReturnInst(retVal, InsertBefore);
2018 }
2019 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
2020 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
2021 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00002022 static ReturnInst* Create(Value * const* retVals, unsigned N,
Gabor Greifefe65362008-05-10 08:32:32 +00002023 Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002024 return new(N) ReturnInst(retVals, N, InsertBefore);
2025 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00002026 static ReturnInst* Create(Value * const* retVals, unsigned N,
2027 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002028 return new(N) ReturnInst(retVals, N, InsertAtEnd);
2029 }
2030 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
2031 return new(0) ReturnInst(InsertAtEnd);
2032 }
Devang Patel57ef4f42008-02-23 00:35:18 +00002033 virtual ~ReturnInst();
Gabor Greifefe65362008-05-10 08:32:32 +00002034 inline void operator delete(void*);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002035
Chris Lattnerf319e832004-10-15 23:52:05 +00002036 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002037
Gabor Greifefe65362008-05-10 08:32:32 +00002038 /// Provide fast operand accessors
2039 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Devang Patel64d4e612008-02-26 17:56:20 +00002040
Gabor Greifefe65362008-05-10 08:32:32 +00002041 /// Convenience accessor
Devang Patel1eafa062008-03-11 17:35:03 +00002042 Value *getReturnValue(unsigned n = 0) const {
Gabor Greifefe65362008-05-10 08:32:32 +00002043 return n < getNumOperands()
2044 ? getOperand(n)
2045 : 0;
Devang Patel1eafa062008-03-11 17:35:03 +00002046 }
2047
Chris Lattner454928e2005-01-29 00:31:36 +00002048 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002049
2050 // Methods for support type inquiry through isa, cast, and dyn_cast:
2051 static inline bool classof(const ReturnInst *) { return true; }
2052 static inline bool classof(const Instruction *I) {
2053 return (I->getOpcode() == Instruction::Ret);
2054 }
2055 static inline bool classof(const Value *V) {
2056 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2057 }
Chris Lattner454928e2005-01-29 00:31:36 +00002058 private:
2059 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2060 virtual unsigned getNumSuccessorsV() const;
2061 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002062};
2063
Gabor Greifefe65362008-05-10 08:32:32 +00002064template <>
2065struct OperandTraits<ReturnInst> : VariadicOperandTraits<> {
2066};
2067
2068DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2069void ReturnInst::operator delete(void *it) {
2070 ReturnInst* me(static_cast<ReturnInst*>(it));
2071 Use::zap(OperandTraits<ReturnInst>::op_begin(me),
2072 OperandTraits<ReturnInst>::op_end(me),
2073 true);
2074}
2075
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002076//===----------------------------------------------------------------------===//
2077// BranchInst Class
2078//===----------------------------------------------------------------------===//
2079
2080//===---------------------------------------------------------------------------
2081/// BranchInst - Conditional or Unconditional Branch instruction.
2082///
2083class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00002084 /// Ops list - Branches are strange. The operands are ordered:
2085 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
2086 /// they don't have to check for cond/uncond branchness.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002087 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00002088 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002089 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2090 // BranchInst(BB *B) - 'br B'
2091 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2092 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2093 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2094 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2095 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00002096 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002097 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002098 Instruction *InsertBefore = 0);
2099 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002100 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002101 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002102public:
2103 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2104 return new(1) BranchInst(IfTrue, InsertBefore);
2105 }
Gabor Greifefe65362008-05-10 08:32:32 +00002106 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2107 Value *Cond, Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002108 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2109 }
2110 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2111 return new(1) BranchInst(IfTrue, InsertAtEnd);
2112 }
Gabor Greifefe65362008-05-10 08:32:32 +00002113 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2114 Value *Cond, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002115 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2116 }
Chris Lattner454928e2005-01-29 00:31:36 +00002117
Bill Wendlingd2a5a2a2008-05-10 10:58:07 +00002118 ~BranchInst() {
Gabor Greifefe65362008-05-10 08:32:32 +00002119 if (NumOperands == 1)
Bill Wendlingc2e73532008-05-10 19:59:59 +00002120 NumOperands = (unsigned)((Use*)this - OperandList);
Gabor Greifefe65362008-05-10 08:32:32 +00002121 }
2122
Chris Lattner454928e2005-01-29 00:31:36 +00002123 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00002124 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002125
Chris Lattnerf319e832004-10-15 23:52:05 +00002126 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002127
Devang Patel4d4a5e02008-02-23 01:11:02 +00002128 bool isUnconditional() const { return getNumOperands() == 1; }
2129 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002130
Devang Patel4d4a5e02008-02-23 01:11:02 +00002131 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002132 assert(isConditional() && "Cannot get condition of an uncond branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00002133 return getOperand(2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002134 }
2135
2136 void setCondition(Value *V) {
2137 assert(isConditional() && "Cannot set condition of unconditional branch!");
2138 setOperand(2, V);
2139 }
2140
2141 // setUnconditionalDest - Change the current branch to an unconditional branch
2142 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +00002143 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002144 void setUnconditionalDest(BasicBlock *Dest) {
Gabor Greifefe65362008-05-10 08:32:32 +00002145 Op<0>() = Dest;
Chris Lattner454928e2005-01-29 00:31:36 +00002146 if (isConditional()) { // Convert this to an uncond branch.
Gabor Greifefe65362008-05-10 08:32:32 +00002147 Op<1>().set(0);
2148 Op<2>().set(0);
Chris Lattner454928e2005-01-29 00:31:36 +00002149 NumOperands = 1;
Chris Lattner454928e2005-01-29 00:31:36 +00002150 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002151 }
2152
Chris Lattner454928e2005-01-29 00:31:36 +00002153 unsigned getNumSuccessors() const { return 1+isConditional(); }
2154
2155 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002156 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Dan Gohmanb96039e2007-05-11 20:59:29 +00002157 return cast<BasicBlock>(getOperand(i));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002158 }
2159
Chris Lattner454928e2005-01-29 00:31:36 +00002160 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002161 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifefe65362008-05-10 08:32:32 +00002162 setOperand(idx, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002163 }
2164
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002165 // Methods for support type inquiry through isa, cast, and dyn_cast:
2166 static inline bool classof(const BranchInst *) { return true; }
2167 static inline bool classof(const Instruction *I) {
2168 return (I->getOpcode() == Instruction::Br);
2169 }
2170 static inline bool classof(const Value *V) {
2171 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2172 }
Chris Lattner454928e2005-01-29 00:31:36 +00002173private:
2174 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2175 virtual unsigned getNumSuccessorsV() const;
2176 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002177};
2178
Gabor Greifefe65362008-05-10 08:32:32 +00002179template <>
2180struct OperandTraits<BranchInst> : HungoffOperandTraits<> {
2181 // we need to access operands via OperandList, since
2182 // the NumOperands may change from 3 to 1
2183 static inline void *allocate(unsigned); // FIXME
2184};
2185
2186DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2187
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002188//===----------------------------------------------------------------------===//
2189// SwitchInst Class
2190//===----------------------------------------------------------------------===//
2191
2192//===---------------------------------------------------------------------------
2193/// SwitchInst - Multiway switch
2194///
2195class SwitchInst : public TerminatorInst {
Gabor Greifefe65362008-05-10 08:32:32 +00002196 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00002197 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002198 // Operand[0] = Value to switch on
2199 // Operand[1] = Default basic block destination
2200 // Operand[2n ] = Value to match
2201 // Operand[2n+1] = BasicBlock to go to on match
2202 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00002203 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2204 void resizeOperands(unsigned No);
Gabor Greifefe65362008-05-10 08:32:32 +00002205 // allocate space for exactly zero operands
2206 void *operator new(size_t s) {
2207 return User::operator new(s, 0);
2208 }
Chris Lattner454928e2005-01-29 00:31:36 +00002209 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2210 /// switch on and a default destination. The number of additional cases can
2211 /// be specified here to make memory allocation more efficient. This
2212 /// constructor can also autoinsert before another instruction.
2213 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002214 Instruction *InsertBefore = 0);
2215
Chris Lattner454928e2005-01-29 00:31:36 +00002216 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2217 /// switch on and a default destination. The number of additional cases can
2218 /// be specified here to make memory allocation more efficient. This
2219 /// constructor also autoinserts at the end of the specified BasicBlock.
2220 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002221 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002222public:
Gabor Greifefe65362008-05-10 08:32:32 +00002223 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2224 unsigned NumCases, Instruction *InsertBefore = 0) {
2225 return new SwitchInst(Value, Default, NumCases, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002226 }
Gabor Greifefe65362008-05-10 08:32:32 +00002227 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2228 unsigned NumCases, BasicBlock *InsertAtEnd) {
2229 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002230 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002231 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00002232
Gabor Greifefe65362008-05-10 08:32:32 +00002233 /// Provide fast operand accessors
2234 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2235
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002236 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00002237 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00002238 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00002239
Devang Patel4d4a5e02008-02-23 01:11:02 +00002240 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002241 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002242 }
2243
2244 /// getNumCases - return the number of 'cases' in this switch instruction.
2245 /// Note that case #0 is always the default case.
2246 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002247 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002248 }
2249
2250 /// getCaseValue - Return the specified case value. Note that case #0, the
2251 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002252 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002253 assert(i && i < getNumCases() && "Illegal case value to get!");
2254 return getSuccessorValue(i);
2255 }
2256
2257 /// getCaseValue - Return the specified case value. Note that case #0, the
2258 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002259 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002260 assert(i && i < getNumCases() && "Illegal case value to get!");
2261 return getSuccessorValue(i);
2262 }
2263
2264 /// findCaseValue - Search all of the case values for the specified constant.
2265 /// If it is explicitly handled, return the case number of it, otherwise
2266 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002267 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002268 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2269 if (getCaseValue(i) == C)
2270 return i;
2271 return 0;
2272 }
2273
Nick Lewycky011f1842006-09-18 19:03:59 +00002274 /// findCaseDest - Finds the unique case value for a given successor. Returns
2275 /// null if the successor is not found, not unique, or is the default case.
2276 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00002277 if (BB == getDefaultDest()) return NULL;
2278
Nick Lewycky011f1842006-09-18 19:03:59 +00002279 ConstantInt *CI = NULL;
2280 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2281 if (getSuccessor(i) == BB) {
2282 if (CI) return NULL; // Multiple cases lead to BB.
2283 else CI = getCaseValue(i);
2284 }
2285 }
2286 return CI;
2287 }
2288
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002289 /// addCase - Add an entry to the switch instruction...
2290 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00002291 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002292
2293 /// removeCase - This method removes the specified successor from the switch
2294 /// instruction. Note that this cannot be used to remove the default
2295 /// destination (successor #0).
2296 ///
2297 void removeCase(unsigned idx);
2298
Chris Lattner454928e2005-01-29 00:31:36 +00002299 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002300
Chris Lattner454928e2005-01-29 00:31:36 +00002301 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2302 BasicBlock *getSuccessor(unsigned idx) const {
2303 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2304 return cast<BasicBlock>(getOperand(idx*2+1));
2305 }
2306 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002307 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Gabor Greifefe65362008-05-10 08:32:32 +00002308 setOperand(idx*2+1, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002309 }
2310
2311 // getSuccessorValue - Return the value associated with the specified
2312 // successor.
Devang Patel4d4a5e02008-02-23 01:11:02 +00002313 ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002314 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00002315 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002316 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002317
2318 // Methods for support type inquiry through isa, cast, and dyn_cast:
2319 static inline bool classof(const SwitchInst *) { return true; }
2320 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00002321 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002322 }
2323 static inline bool classof(const Value *V) {
2324 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2325 }
Chris Lattner454928e2005-01-29 00:31:36 +00002326private:
2327 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2328 virtual unsigned getNumSuccessorsV() const;
2329 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002330};
2331
Gabor Greifefe65362008-05-10 08:32:32 +00002332template <>
2333struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2334};
2335
2336DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2337
2338
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002339//===----------------------------------------------------------------------===//
2340// InvokeInst Class
2341//===----------------------------------------------------------------------===//
2342
Chris Lattner3340ffe2005-05-06 20:26:26 +00002343/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2344/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002345///
2346class InvokeInst : public TerminatorInst {
Chris Lattner58d74912008-03-12 17:45:29 +00002347 PAListPtr ParamAttrs;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002348 InvokeInst(const InvokeInst &BI);
2349 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerd2dd1502007-02-13 01:04:01 +00002350 Value* const *Args, unsigned NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00002351
2352 template<typename InputIterator>
2353 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2354 InputIterator ArgBegin, InputIterator ArgEnd,
2355 const std::string &Name,
2356 // This argument ensures that we have an iterator we can
2357 // do arithmetic on in constant time
2358 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002359 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greenef1355a52007-08-27 19:04:21 +00002360
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002361 // This requires that the iterator points to contiguous memory.
2362 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00002363 setName(Name);
2364 }
2365
David Greenef1355a52007-08-27 19:04:21 +00002366 /// Construct an InvokeInst given a range of arguments.
2367 /// InputIterator must be a random-access iterator pointing to
2368 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2369 /// made for random-accessness but not for contiguous storage as
2370 /// that would incur runtime overhead.
2371 ///
2372 /// @brief Construct an InvokeInst from a range of arguments
2373 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002374 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2375 InputIterator ArgBegin, InputIterator ArgEnd,
2376 unsigned Values,
2377 const std::string &Name, Instruction *InsertBefore);
David Greenef1355a52007-08-27 19:04:21 +00002378
2379 /// Construct an InvokeInst given a range of arguments.
2380 /// InputIterator must be a random-access iterator pointing to
2381 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2382 /// made for random-accessness but not for contiguous storage as
2383 /// that would incur runtime overhead.
2384 ///
2385 /// @brief Construct an InvokeInst from a range of arguments
2386 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002387 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2388 InputIterator ArgBegin, InputIterator ArgEnd,
2389 unsigned Values,
2390 const std::string &Name, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002391public:
2392 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002393 static InvokeInst *Create(Value *Func,
2394 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002395 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Chengd69bb1a2008-05-05 17:41:03 +00002396 const std::string &Name = "",
2397 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +00002398 unsigned Values(ArgEnd - ArgBegin + 3);
2399 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2400 Values, Name, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002401 }
2402 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002403 static InvokeInst *Create(Value *Func,
2404 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002405 InputIterator ArgBegin, InputIterator ArgEnd,
2406 const std::string &Name, BasicBlock *InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00002407 unsigned Values(ArgEnd - ArgBegin + 3);
2408 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2409 Values, Name, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002410 }
David Greenef1355a52007-08-27 19:04:21 +00002411
Chris Lattnerf319e832004-10-15 23:52:05 +00002412 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002413
Gabor Greifefe65362008-05-10 08:32:32 +00002414 /// Provide fast operand accessors
2415 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2416
Chris Lattner3340ffe2005-05-06 20:26:26 +00002417 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2418 /// function call.
2419 unsigned getCallingConv() const { return SubclassData; }
2420 void setCallingConv(unsigned CC) {
2421 SubclassData = CC;
2422 }
2423
Chris Lattner041221c2008-03-13 04:33:03 +00002424 /// getParamAttrs - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002425 ///
2426 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002427
Chris Lattner041221c2008-03-13 04:33:03 +00002428 /// setParamAttrs - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002429 ///
2430 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00002431
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002432 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002433 bool paramHasAttr(unsigned i, ParameterAttributes attr) const;
Eric Christopher0bf7b412008-05-16 20:39:43 +00002434
2435 /// addParamAttr - adds the attribute to the list of attributes.
2436 void addParamAttr(unsigned i, ParameterAttributes attr);
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002437
Dale Johannesen08e78b12008-02-22 17:49:45 +00002438 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002439 unsigned getParamAlignment(unsigned i) const {
2440 return ParamAttrs.getParamAlignment(i);
2441 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00002442
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002443 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002444 bool doesNotAccessMemory() const {
2445 return paramHasAttr(0, ParamAttr::ReadNone);
2446 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002447
2448 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002449 bool onlyReadsMemory() const {
2450 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
2451 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002452
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002453 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002454 bool doesNotReturn() const {
2455 return paramHasAttr(0, ParamAttr::NoReturn);
2456 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002457
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002458 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002459 bool doesNotThrow() const {
2460 return paramHasAttr(0, ParamAttr::NoUnwind);
2461 }
Duncan Sandsf0c33542007-12-19 21:13:37 +00002462 void setDoesNotThrow(bool doesNotThrow = true);
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002463
Devang Patel41e23972008-03-03 21:46:28 +00002464 /// @brief Determine if the call returns a structure through first
2465 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002466 bool hasStructRetAttr() const {
2467 // Be friendly and also check the callee.
2468 return paramHasAttr(1, ParamAttr::StructRet);
2469 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002470
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002471 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00002472 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002473 ///
Chris Lattner721aef62004-11-18 17:46:57 +00002474 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002475 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00002476 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002477
2478 // getCalledValue - Get a pointer to a function that is invoked by this inst.
Devang Patel4d4a5e02008-02-23 01:11:02 +00002479 Value *getCalledValue() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002480
2481 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00002482 BasicBlock *getNormalDest() const {
2483 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002484 }
Chris Lattner454928e2005-01-29 00:31:36 +00002485 BasicBlock *getUnwindDest() const {
2486 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002487 }
Chris Lattner454928e2005-01-29 00:31:36 +00002488 void setNormalDest(BasicBlock *B) {
Gabor Greifefe65362008-05-10 08:32:32 +00002489 setOperand(1, B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002490 }
2491
Chris Lattner454928e2005-01-29 00:31:36 +00002492 void setUnwindDest(BasicBlock *B) {
Gabor Greifefe65362008-05-10 08:32:32 +00002493 setOperand(2, B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002494 }
2495
Devang Patel4d4a5e02008-02-23 01:11:02 +00002496 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002497 assert(i < 2 && "Successor # out of range for invoke!");
2498 return i == 0 ? getNormalDest() : getUnwindDest();
2499 }
2500
Chris Lattner454928e2005-01-29 00:31:36 +00002501 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002502 assert(idx < 2 && "Successor # out of range for invoke!");
Gabor Greifefe65362008-05-10 08:32:32 +00002503 setOperand(idx+1, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002504 }
2505
Chris Lattner454928e2005-01-29 00:31:36 +00002506 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002507
2508 // Methods for support type inquiry through isa, cast, and dyn_cast:
2509 static inline bool classof(const InvokeInst *) { return true; }
2510 static inline bool classof(const Instruction *I) {
2511 return (I->getOpcode() == Instruction::Invoke);
2512 }
2513 static inline bool classof(const Value *V) {
2514 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2515 }
Chris Lattner454928e2005-01-29 00:31:36 +00002516private:
2517 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2518 virtual unsigned getNumSuccessorsV() const;
2519 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002520};
2521
Gabor Greifefe65362008-05-10 08:32:32 +00002522template <>
2523struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2524};
2525
2526template<typename InputIterator>
2527InvokeInst::InvokeInst(Value *Func,
2528 BasicBlock *IfNormal, BasicBlock *IfException,
2529 InputIterator ArgBegin, InputIterator ArgEnd,
2530 unsigned Values,
2531 const std::string &Name, Instruction *InsertBefore)
2532 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2533 ->getElementType())->getReturnType(),
2534 Instruction::Invoke,
2535 OperandTraits<InvokeInst>::op_end(this) - Values,
2536 Values, InsertBefore) {
2537 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
2538 typename std::iterator_traits<InputIterator>::iterator_category());
2539}
2540template<typename InputIterator>
2541InvokeInst::InvokeInst(Value *Func,
2542 BasicBlock *IfNormal, BasicBlock *IfException,
2543 InputIterator ArgBegin, InputIterator ArgEnd,
2544 unsigned Values,
2545 const std::string &Name, BasicBlock *InsertAtEnd)
2546 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2547 ->getElementType())->getReturnType(),
2548 Instruction::Invoke,
2549 OperandTraits<InvokeInst>::op_end(this) - Values,
2550 Values, InsertAtEnd) {
2551 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
2552 typename std::iterator_traits<InputIterator>::iterator_category());
2553}
2554
2555DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002556
2557//===----------------------------------------------------------------------===//
2558// UnwindInst Class
2559//===----------------------------------------------------------------------===//
2560
2561//===---------------------------------------------------------------------------
2562/// UnwindInst - Immediately exit the current function, unwinding the stack
2563/// until an invoke instruction is found.
2564///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002565class UnwindInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002566 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002567public:
Gabor Greif051a9502008-04-06 20:25:17 +00002568 // allocate space for exactly zero operands
2569 void *operator new(size_t s) {
2570 return User::operator new(s, 0);
2571 }
Chris Lattner910c80a2007-02-24 00:55:48 +00002572 explicit UnwindInst(Instruction *InsertBefore = 0);
2573 explicit UnwindInst(BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002574
Chris Lattnerf319e832004-10-15 23:52:05 +00002575 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002576
Chris Lattner454928e2005-01-29 00:31:36 +00002577 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002578
2579 // Methods for support type inquiry through isa, cast, and dyn_cast:
2580 static inline bool classof(const UnwindInst *) { return true; }
2581 static inline bool classof(const Instruction *I) {
2582 return I->getOpcode() == Instruction::Unwind;
2583 }
2584 static inline bool classof(const Value *V) {
2585 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2586 }
Chris Lattner454928e2005-01-29 00:31:36 +00002587private:
2588 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2589 virtual unsigned getNumSuccessorsV() const;
2590 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002591};
2592
Chris Lattner076b3f12004-10-16 18:05:54 +00002593//===----------------------------------------------------------------------===//
2594// UnreachableInst Class
2595//===----------------------------------------------------------------------===//
2596
2597//===---------------------------------------------------------------------------
2598/// UnreachableInst - This function has undefined behavior. In particular, the
2599/// presence of this instruction indicates some higher level knowledge that the
2600/// end of the block cannot be reached.
2601///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002602class UnreachableInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002603 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002604public:
Gabor Greif051a9502008-04-06 20:25:17 +00002605 // allocate space for exactly zero operands
2606 void *operator new(size_t s) {
2607 return User::operator new(s, 0);
2608 }
Chris Lattner910c80a2007-02-24 00:55:48 +00002609 explicit UnreachableInst(Instruction *InsertBefore = 0);
2610 explicit UnreachableInst(BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00002611
2612 virtual UnreachableInst *clone() const;
2613
Chris Lattner454928e2005-01-29 00:31:36 +00002614 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00002615
2616 // Methods for support type inquiry through isa, cast, and dyn_cast:
2617 static inline bool classof(const UnreachableInst *) { return true; }
2618 static inline bool classof(const Instruction *I) {
2619 return I->getOpcode() == Instruction::Unreachable;
2620 }
2621 static inline bool classof(const Value *V) {
2622 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2623 }
Chris Lattner454928e2005-01-29 00:31:36 +00002624private:
2625 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2626 virtual unsigned getNumSuccessorsV() const;
2627 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00002628};
2629
Reid Spencer3da59db2006-11-27 01:05:10 +00002630//===----------------------------------------------------------------------===//
2631// TruncInst Class
2632//===----------------------------------------------------------------------===//
2633
2634/// @brief This class represents a truncation of integer types.
2635class TruncInst : public CastInst {
2636 /// Private copy constructor
2637 TruncInst(const TruncInst &CI)
2638 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2639 }
2640public:
2641 /// @brief Constructor with insert-before-instruction semantics
2642 TruncInst(
2643 Value *S, ///< The value to be truncated
2644 const Type *Ty, ///< The (smaller) type to truncate to
2645 const std::string &Name = "", ///< A name for the new instruction
2646 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2647 );
2648
2649 /// @brief Constructor with insert-at-end-of-block semantics
2650 TruncInst(
2651 Value *S, ///< The value to be truncated
2652 const Type *Ty, ///< The (smaller) type to truncate to
2653 const std::string &Name, ///< A name for the new instruction
2654 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2655 );
2656
2657 /// @brief Clone an identical TruncInst
2658 virtual CastInst *clone() const;
2659
2660 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2661 static inline bool classof(const TruncInst *) { return true; }
2662 static inline bool classof(const Instruction *I) {
2663 return I->getOpcode() == Trunc;
2664 }
2665 static inline bool classof(const Value *V) {
2666 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2667 }
2668};
2669
2670//===----------------------------------------------------------------------===//
2671// ZExtInst Class
2672//===----------------------------------------------------------------------===//
2673
2674/// @brief This class represents zero extension of integer types.
2675class ZExtInst : public CastInst {
2676 /// @brief Private copy constructor
2677 ZExtInst(const ZExtInst &CI)
2678 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2679 }
2680public:
2681 /// @brief Constructor with insert-before-instruction semantics
2682 ZExtInst(
2683 Value *S, ///< The value to be zero extended
2684 const Type *Ty, ///< The type to zero extend to
2685 const std::string &Name = "", ///< A name for the new instruction
2686 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2687 );
2688
2689 /// @brief Constructor with insert-at-end semantics.
2690 ZExtInst(
2691 Value *S, ///< The value to be zero extended
2692 const Type *Ty, ///< The type to zero extend to
2693 const std::string &Name, ///< A name for the new instruction
2694 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2695 );
2696
2697 /// @brief Clone an identical ZExtInst
2698 virtual CastInst *clone() const;
2699
2700 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2701 static inline bool classof(const ZExtInst *) { return true; }
2702 static inline bool classof(const Instruction *I) {
2703 return I->getOpcode() == ZExt;
2704 }
2705 static inline bool classof(const Value *V) {
2706 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2707 }
2708};
2709
2710//===----------------------------------------------------------------------===//
2711// SExtInst Class
2712//===----------------------------------------------------------------------===//
2713
2714/// @brief This class represents a sign extension of integer types.
2715class SExtInst : public CastInst {
2716 /// @brief Private copy constructor
2717 SExtInst(const SExtInst &CI)
2718 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2719 }
2720public:
2721 /// @brief Constructor with insert-before-instruction semantics
2722 SExtInst(
2723 Value *S, ///< The value to be sign extended
2724 const Type *Ty, ///< The type to sign extend to
2725 const std::string &Name = "", ///< A name for the new instruction
2726 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2727 );
2728
2729 /// @brief Constructor with insert-at-end-of-block semantics
2730 SExtInst(
2731 Value *S, ///< The value to be sign extended
2732 const Type *Ty, ///< The type to sign extend to
2733 const std::string &Name, ///< A name for the new instruction
2734 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2735 );
2736
2737 /// @brief Clone an identical SExtInst
2738 virtual CastInst *clone() const;
2739
2740 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2741 static inline bool classof(const SExtInst *) { return true; }
2742 static inline bool classof(const Instruction *I) {
2743 return I->getOpcode() == SExt;
2744 }
2745 static inline bool classof(const Value *V) {
2746 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2747 }
2748};
2749
2750//===----------------------------------------------------------------------===//
2751// FPTruncInst Class
2752//===----------------------------------------------------------------------===//
2753
2754/// @brief This class represents a truncation of floating point types.
2755class FPTruncInst : public CastInst {
2756 FPTruncInst(const FPTruncInst &CI)
2757 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2758 }
2759public:
2760 /// @brief Constructor with insert-before-instruction semantics
2761 FPTruncInst(
2762 Value *S, ///< The value to be truncated
2763 const Type *Ty, ///< The type to truncate to
2764 const std::string &Name = "", ///< A name for the new instruction
2765 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2766 );
2767
2768 /// @brief Constructor with insert-before-instruction semantics
2769 FPTruncInst(
2770 Value *S, ///< The value to be truncated
2771 const Type *Ty, ///< The type to truncate to
2772 const std::string &Name, ///< A name for the new instruction
2773 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2774 );
2775
2776 /// @brief Clone an identical FPTruncInst
2777 virtual CastInst *clone() const;
2778
2779 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2780 static inline bool classof(const FPTruncInst *) { return true; }
2781 static inline bool classof(const Instruction *I) {
2782 return I->getOpcode() == FPTrunc;
2783 }
2784 static inline bool classof(const Value *V) {
2785 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2786 }
2787};
2788
2789//===----------------------------------------------------------------------===//
2790// FPExtInst Class
2791//===----------------------------------------------------------------------===//
2792
2793/// @brief This class represents an extension of floating point types.
2794class FPExtInst : public CastInst {
2795 FPExtInst(const FPExtInst &CI)
2796 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2797 }
2798public:
2799 /// @brief Constructor with insert-before-instruction semantics
2800 FPExtInst(
2801 Value *S, ///< The value to be extended
2802 const Type *Ty, ///< The type to extend to
2803 const std::string &Name = "", ///< A name for the new instruction
2804 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2805 );
2806
2807 /// @brief Constructor with insert-at-end-of-block semantics
2808 FPExtInst(
2809 Value *S, ///< The value to be extended
2810 const Type *Ty, ///< The type to extend to
2811 const std::string &Name, ///< A name for the new instruction
2812 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2813 );
2814
2815 /// @brief Clone an identical FPExtInst
2816 virtual CastInst *clone() const;
2817
2818 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2819 static inline bool classof(const FPExtInst *) { return true; }
2820 static inline bool classof(const Instruction *I) {
2821 return I->getOpcode() == FPExt;
2822 }
2823 static inline bool classof(const Value *V) {
2824 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2825 }
2826};
2827
2828//===----------------------------------------------------------------------===//
2829// UIToFPInst Class
2830//===----------------------------------------------------------------------===//
2831
2832/// @brief This class represents a cast unsigned integer to floating point.
2833class UIToFPInst : public CastInst {
2834 UIToFPInst(const UIToFPInst &CI)
2835 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2836 }
2837public:
2838 /// @brief Constructor with insert-before-instruction semantics
2839 UIToFPInst(
2840 Value *S, ///< The value to be converted
2841 const Type *Ty, ///< The type to convert to
2842 const std::string &Name = "", ///< A name for the new instruction
2843 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2844 );
2845
2846 /// @brief Constructor with insert-at-end-of-block semantics
2847 UIToFPInst(
2848 Value *S, ///< The value to be converted
2849 const Type *Ty, ///< The type to convert to
2850 const std::string &Name, ///< A name for the new instruction
2851 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2852 );
2853
2854 /// @brief Clone an identical UIToFPInst
2855 virtual CastInst *clone() const;
2856
2857 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2858 static inline bool classof(const UIToFPInst *) { return true; }
2859 static inline bool classof(const Instruction *I) {
2860 return I->getOpcode() == UIToFP;
2861 }
2862 static inline bool classof(const Value *V) {
2863 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2864 }
2865};
2866
2867//===----------------------------------------------------------------------===//
2868// SIToFPInst Class
2869//===----------------------------------------------------------------------===//
2870
2871/// @brief This class represents a cast from signed integer to floating point.
2872class SIToFPInst : public CastInst {
2873 SIToFPInst(const SIToFPInst &CI)
2874 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2875 }
2876public:
2877 /// @brief Constructor with insert-before-instruction semantics
2878 SIToFPInst(
2879 Value *S, ///< The value to be converted
2880 const Type *Ty, ///< The type to convert to
2881 const std::string &Name = "", ///< A name for the new instruction
2882 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2883 );
2884
2885 /// @brief Constructor with insert-at-end-of-block semantics
2886 SIToFPInst(
2887 Value *S, ///< The value to be converted
2888 const Type *Ty, ///< The type to convert to
2889 const std::string &Name, ///< A name for the new instruction
2890 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2891 );
2892
2893 /// @brief Clone an identical SIToFPInst
2894 virtual CastInst *clone() const;
2895
2896 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2897 static inline bool classof(const SIToFPInst *) { return true; }
2898 static inline bool classof(const Instruction *I) {
2899 return I->getOpcode() == SIToFP;
2900 }
2901 static inline bool classof(const Value *V) {
2902 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2903 }
2904};
2905
2906//===----------------------------------------------------------------------===//
2907// FPToUIInst Class
2908//===----------------------------------------------------------------------===//
2909
2910/// @brief This class represents a cast from floating point to unsigned integer
2911class FPToUIInst : public CastInst {
2912 FPToUIInst(const FPToUIInst &CI)
2913 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2914 }
2915public:
2916 /// @brief Constructor with insert-before-instruction semantics
2917 FPToUIInst(
2918 Value *S, ///< The value to be converted
2919 const Type *Ty, ///< The type to convert to
2920 const std::string &Name = "", ///< A name for the new instruction
2921 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2922 );
2923
2924 /// @brief Constructor with insert-at-end-of-block semantics
2925 FPToUIInst(
2926 Value *S, ///< The value to be converted
2927 const Type *Ty, ///< The type to convert to
2928 const std::string &Name, ///< A name for the new instruction
2929 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2930 );
2931
2932 /// @brief Clone an identical FPToUIInst
2933 virtual CastInst *clone() const;
2934
2935 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2936 static inline bool classof(const FPToUIInst *) { return true; }
2937 static inline bool classof(const Instruction *I) {
2938 return I->getOpcode() == FPToUI;
2939 }
2940 static inline bool classof(const Value *V) {
2941 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2942 }
2943};
2944
2945//===----------------------------------------------------------------------===//
2946// FPToSIInst Class
2947//===----------------------------------------------------------------------===//
2948
2949/// @brief This class represents a cast from floating point to signed integer.
2950class FPToSIInst : public CastInst {
2951 FPToSIInst(const FPToSIInst &CI)
2952 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2953 }
2954public:
2955 /// @brief Constructor with insert-before-instruction semantics
2956 FPToSIInst(
2957 Value *S, ///< The value to be converted
2958 const Type *Ty, ///< The type to convert to
2959 const std::string &Name = "", ///< A name for the new instruction
2960 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2961 );
2962
2963 /// @brief Constructor with insert-at-end-of-block semantics
2964 FPToSIInst(
2965 Value *S, ///< The value to be converted
2966 const Type *Ty, ///< The type to convert to
2967 const std::string &Name, ///< A name for the new instruction
2968 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2969 );
2970
2971 /// @brief Clone an identical FPToSIInst
2972 virtual CastInst *clone() const;
2973
2974 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2975 static inline bool classof(const FPToSIInst *) { return true; }
2976 static inline bool classof(const Instruction *I) {
2977 return I->getOpcode() == FPToSI;
2978 }
2979 static inline bool classof(const Value *V) {
2980 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2981 }
2982};
2983
2984//===----------------------------------------------------------------------===//
2985// IntToPtrInst Class
2986//===----------------------------------------------------------------------===//
2987
2988/// @brief This class represents a cast from an integer to a pointer.
2989class IntToPtrInst : public CastInst {
2990 IntToPtrInst(const IntToPtrInst &CI)
2991 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2992 }
2993public:
2994 /// @brief Constructor with insert-before-instruction semantics
2995 IntToPtrInst(
2996 Value *S, ///< The value to be converted
2997 const Type *Ty, ///< The type to convert to
2998 const std::string &Name = "", ///< A name for the new instruction
2999 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3000 );
3001
3002 /// @brief Constructor with insert-at-end-of-block semantics
3003 IntToPtrInst(
3004 Value *S, ///< The value to be converted
3005 const Type *Ty, ///< The type to convert to
3006 const std::string &Name, ///< A name for the new instruction
3007 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3008 );
3009
3010 /// @brief Clone an identical IntToPtrInst
3011 virtual CastInst *clone() const;
3012
3013 // Methods for support type inquiry through isa, cast, and dyn_cast:
3014 static inline bool classof(const IntToPtrInst *) { return true; }
3015 static inline bool classof(const Instruction *I) {
3016 return I->getOpcode() == IntToPtr;
3017 }
3018 static inline bool classof(const Value *V) {
3019 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3020 }
3021};
3022
3023//===----------------------------------------------------------------------===//
3024// PtrToIntInst Class
3025//===----------------------------------------------------------------------===//
3026
3027/// @brief This class represents a cast from a pointer to an integer
3028class PtrToIntInst : public CastInst {
3029 PtrToIntInst(const PtrToIntInst &CI)
3030 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3031 }
3032public:
3033 /// @brief Constructor with insert-before-instruction semantics
3034 PtrToIntInst(
3035 Value *S, ///< The value to be converted
3036 const Type *Ty, ///< The type to convert to
3037 const std::string &Name = "", ///< A name for the new instruction
3038 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3039 );
3040
3041 /// @brief Constructor with insert-at-end-of-block semantics
3042 PtrToIntInst(
3043 Value *S, ///< The value to be converted
3044 const Type *Ty, ///< The type to convert to
3045 const std::string &Name, ///< A name for the new instruction
3046 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3047 );
3048
3049 /// @brief Clone an identical PtrToIntInst
3050 virtual CastInst *clone() const;
3051
3052 // Methods for support type inquiry through isa, cast, and dyn_cast:
3053 static inline bool classof(const PtrToIntInst *) { return true; }
3054 static inline bool classof(const Instruction *I) {
3055 return I->getOpcode() == PtrToInt;
3056 }
3057 static inline bool classof(const Value *V) {
3058 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3059 }
3060};
3061
3062//===----------------------------------------------------------------------===//
3063// BitCastInst Class
3064//===----------------------------------------------------------------------===//
3065
3066/// @brief This class represents a no-op cast from one type to another.
3067class BitCastInst : public CastInst {
3068 BitCastInst(const BitCastInst &CI)
3069 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3070 }
3071public:
3072 /// @brief Constructor with insert-before-instruction semantics
3073 BitCastInst(
3074 Value *S, ///< The value to be casted
3075 const Type *Ty, ///< The type to casted to
3076 const std::string &Name = "", ///< A name for the new instruction
3077 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3078 );
3079
3080 /// @brief Constructor with insert-at-end-of-block semantics
3081 BitCastInst(
3082 Value *S, ///< The value to be casted
3083 const Type *Ty, ///< The type to casted to
3084 const std::string &Name, ///< A name for the new instruction
3085 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3086 );
3087
3088 /// @brief Clone an identical BitCastInst
3089 virtual CastInst *clone() const;
3090
3091 // Methods for support type inquiry through isa, cast, and dyn_cast:
3092 static inline bool classof(const BitCastInst *) { return true; }
3093 static inline bool classof(const Instruction *I) {
3094 return I->getOpcode() == BitCast;
3095 }
3096 static inline bool classof(const Value *V) {
3097 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3098 }
3099};
3100
Devang Patel40a04212008-02-19 22:15:16 +00003101//===----------------------------------------------------------------------===//
3102// GetResultInst Class
3103//===----------------------------------------------------------------------===//
3104
3105/// GetResultInst - This instruction extracts individual result value from
3106/// aggregate value, where aggregate value is returned by CallInst.
3107///
Gabor Greifd6a22182008-05-13 07:09:08 +00003108class GetResultInst : public UnaryInstruction {
Devang Patel23755d82008-02-20 19:10:47 +00003109 unsigned Idx;
Devang Patel40a04212008-02-19 22:15:16 +00003110 GetResultInst(const GetResultInst &GRI) :
Gabor Greifd6a22182008-05-13 07:09:08 +00003111 UnaryInstruction(GRI.getType(), Instruction::GetResult, GRI.getOperand(0)),
3112 Idx(GRI.Idx) {
Devang Patel40a04212008-02-19 22:15:16 +00003113 }
3114
3115public:
Gabor Greifefe65362008-05-10 08:32:32 +00003116 GetResultInst(Value *Aggr, unsigned index,
3117 const std::string &Name = "",
3118 Instruction *InsertBefore = 0);
Devang Patel40a04212008-02-19 22:15:16 +00003119
3120 /// isValidOperands - Return true if an getresult instruction can be
3121 /// formed with the specified operands.
Devang Patel23755d82008-02-20 19:10:47 +00003122 static bool isValidOperands(const Value *Aggr, unsigned index);
Devang Patel40a04212008-02-19 22:15:16 +00003123
3124 virtual GetResultInst *clone() const;
3125
Devang Patel4d4a5e02008-02-23 01:11:02 +00003126 Value *getAggregateValue() {
Devang Patel40a04212008-02-19 22:15:16 +00003127 return getOperand(0);
3128 }
Devang Patel2d2ae342008-02-20 18:36:16 +00003129
Devang Patel4d4a5e02008-02-23 01:11:02 +00003130 const Value *getAggregateValue() const {
Devang Patel2d2ae342008-02-20 18:36:16 +00003131 return getOperand(0);
3132 }
3133
Devang Patel4d4a5e02008-02-23 01:11:02 +00003134 unsigned getIndex() const {
Devang Patel23755d82008-02-20 19:10:47 +00003135 return Idx;
Devang Patel40a04212008-02-19 22:15:16 +00003136 }
3137
Devang Patel40a04212008-02-19 22:15:16 +00003138 // Methods for support type inquiry through isa, cast, and dyn_cast:
3139 static inline bool classof(const GetResultInst *) { return true; }
3140 static inline bool classof(const Instruction *I) {
3141 return (I->getOpcode() == Instruction::GetResult);
3142 }
3143 static inline bool classof(const Value *V) {
3144 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3145 }
3146};
3147
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003148} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00003149
3150#endif