blob: 9a990e7eb96fdaefaf0364a222cc5855be11dd64 [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
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000019#include "llvm/InstrTypes.h"
David Greene52eec542007-08-01 03:43:44 +000020#include "llvm/DerivedTypes.h"
Devang Pateleaf42ab2008-09-23 23:03:40 +000021#include "llvm/Attributes.h"
Gabor Greifefe65362008-05-10 08:32:32 +000022#include "llvm/BasicBlock.h"
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000023#include "llvm/CallingConv.h"
Owen Anderson333c4002009-07-09 23:48:35 +000024#include "llvm/LLVMContext.h"
Dan Gohman81a0c0b2008-05-31 00:58:22 +000025#include "llvm/ADT/SmallVector.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000026#include <iterator>
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000027
28namespace llvm {
29
Chris Lattnerd1a32602005-02-24 05:32:09 +000030class ConstantInt;
Reid Spencer3da43842007-02-28 22:00:54 +000031class ConstantRange;
32class APInt;
Benjamin Kramer12ddd402009-08-11 17:45:13 +000033class LLVMContext;
Dan Gohmanbccfc242009-09-03 15:34:35 +000034class DominatorTree;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000035
36//===----------------------------------------------------------------------===//
Victor Hernandez7b929da2009-10-23 21:09:37 +000037// AllocaInst Class
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000038//===----------------------------------------------------------------------===//
39
Victor Hernandez7b929da2009-10-23 21:09:37 +000040/// AllocaInst - an instruction to allocate memory on the stack
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000041///
Victor Hernandez7b929da2009-10-23 21:09:37 +000042class AllocaInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000043public:
Victor Hernandez7b929da2009-10-23 21:09:37 +000044 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
45 const Twine &Name = "", Instruction *InsertBefore = 0);
46 AllocaInst(const Type *Ty, Value *ArraySize,
47 const Twine &Name, BasicBlock *InsertAtEnd);
48
49 AllocaInst(const Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
50 AllocaInst(const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
51
52 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
53 const Twine &Name = "", Instruction *InsertBefore = 0);
54 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
55 const Twine &Name, BasicBlock *InsertAtEnd);
56
Gabor Greif051a9502008-04-06 20:25:17 +000057 // Out of line virtual method, so the vtable, etc. has a home.
Victor Hernandez7b929da2009-10-23 21:09:37 +000058 virtual ~AllocaInst();
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000059
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000060 /// isArrayAllocation - Return true if there is an allocation size parameter
61 /// to the allocation instruction that is not 1.
62 ///
63 bool isArrayAllocation() const;
64
Dan Gohman18476ee2009-07-07 20:47:48 +000065 /// getArraySize - Get the number of elements allocated. For a simple
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000066 /// allocation of a single element, this will return a constant 1 value.
67 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000068 const Value *getArraySize() const { return getOperand(0); }
69 Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000070
71 /// getType - Overload to return most specific pointer type
72 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000073 const PointerType *getType() const {
Misha Brukman9769ab22005-04-21 20:19:05 +000074 return reinterpret_cast<const PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000075 }
76
77 /// getAllocatedType - Return the type that is being allocated by the
78 /// instruction.
79 ///
80 const Type *getAllocatedType() const;
81
Nate Begeman14b05292005-11-05 09:21:28 +000082 /// getAlignment - Return the alignment of the memory that is being allocated
83 /// by the instruction.
84 ///
Dan Gohman52837072008-03-24 16:55:58 +000085 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
86 void setAlignment(unsigned Align);
Chris Lattnerf56a8db2006-10-03 17:09:12 +000087
Chris Lattnerc5dd22a2008-11-26 02:54:17 +000088 /// isStaticAlloca - Return true if this alloca is in the entry block of the
89 /// function and is a constant size. If so, the code generator will fold it
90 /// into the prolog/epilog code, so it is basically free.
91 bool isStaticAlloca() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000092
Victor Hernandez7b929da2009-10-23 21:09:37 +000093 virtual AllocaInst *clone() const;
94
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000095 // Methods for support type inquiry through isa, cast, and dyn_cast:
96 static inline bool classof(const AllocaInst *) { return true; }
97 static inline bool classof(const Instruction *I) {
98 return (I->getOpcode() == Instruction::Alloca);
99 }
100 static inline bool classof(const Value *V) {
101 return isa<Instruction>(V) && classof(cast<Instruction>(V));
102 }
103};
104
105
106//===----------------------------------------------------------------------===//
107// FreeInst Class
108//===----------------------------------------------------------------------===//
109
110/// FreeInst - an instruction to deallocate memory
111///
Chris Lattner454928e2005-01-29 00:31:36 +0000112class FreeInst : public UnaryInstruction {
113 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000114public:
115 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
116 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
117
Nick Lewycky67760642009-09-27 07:38:41 +0000118 virtual FreeInst *clone() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000119
Owen Andersonc9edf0b2007-07-06 23:13:31 +0000120 // Accessor methods for consistency with other memory operations
121 Value *getPointerOperand() { return getOperand(0); }
122 const Value *getPointerOperand() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000123
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000124 // Methods for support type inquiry through isa, cast, and dyn_cast:
125 static inline bool classof(const FreeInst *) { return true; }
126 static inline bool classof(const Instruction *I) {
127 return (I->getOpcode() == Instruction::Free);
128 }
129 static inline bool classof(const Value *V) {
130 return isa<Instruction>(V) && classof(cast<Instruction>(V));
131 }
132};
133
134
135//===----------------------------------------------------------------------===//
136// LoadInst Class
137//===----------------------------------------------------------------------===//
138
Chris Lattner88fe29a2005-02-05 01:44:18 +0000139/// LoadInst - an instruction for reading from memory. This uses the
140/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000141///
Chris Lattner454928e2005-01-29 00:31:36 +0000142class LoadInst : public UnaryInstruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000143 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000144public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000145 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
146 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000147 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
Christopher Lamb43c7f372007-04-22 19:24:39 +0000148 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000149 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000150 unsigned Align, Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000151 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000152 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000153 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000154 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000155
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000156 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
157 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
158 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
159 bool isVolatile = false, Instruction *InsertBefore = 0);
160 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
161 BasicBlock *InsertAtEnd);
162
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000163 /// isVolatile - Return true if this is a load from a volatile memory
164 /// location.
165 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000166 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000167
168 /// setVolatile - Specify whether this is a volatile load or not.
169 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000170 void setVolatile(bool V) {
171 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000172 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000173
Nick Lewycky67760642009-09-27 07:38:41 +0000174 virtual LoadInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000175
Christopher Lamb43c7f372007-04-22 19:24:39 +0000176 /// getAlignment - Return the alignment of the access that is being performed
177 ///
178 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000179 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000180 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000181
Christopher Lamb43c7f372007-04-22 19:24:39 +0000182 void setAlignment(unsigned Align);
183
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000184 Value *getPointerOperand() { return getOperand(0); }
185 const Value *getPointerOperand() const { return getOperand(0); }
186 static unsigned getPointerOperandIndex() { return 0U; }
187
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000188 unsigned getPointerAddressSpace() const {
189 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
190 }
191
192
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000193 // Methods for support type inquiry through isa, cast, and dyn_cast:
194 static inline bool classof(const LoadInst *) { return true; }
195 static inline bool classof(const Instruction *I) {
196 return I->getOpcode() == Instruction::Load;
197 }
198 static inline bool classof(const Value *V) {
199 return isa<Instruction>(V) && classof(cast<Instruction>(V));
200 }
201};
202
203
204//===----------------------------------------------------------------------===//
205// StoreInst Class
206//===----------------------------------------------------------------------===//
207
Misha Brukman9769ab22005-04-21 20:19:05 +0000208/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000209///
210class StoreInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +0000211 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +0000212 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000213public:
Gabor Greif051a9502008-04-06 20:25:17 +0000214 // allocate space for exactly two operands
215 void *operator new(size_t s) {
216 return User::operator new(s, 2);
217 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000218 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
219 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
220 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
221 Instruction *InsertBefore = 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000222 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
223 unsigned Align, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000224 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000225 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
226 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000227
228
229 /// isVolatile - Return true if this is a load from a volatile memory
230 /// location.
231 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000232 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000233
234 /// setVolatile - Specify whether this is a volatile load or not.
235 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000236 void setVolatile(bool V) {
237 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000238 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000239
Chris Lattner454928e2005-01-29 00:31:36 +0000240 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +0000241 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner454928e2005-01-29 00:31:36 +0000242
Christopher Lamb43c7f372007-04-22 19:24:39 +0000243 /// getAlignment - Return the alignment of the access that is being performed
244 ///
245 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000246 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000247 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000248
Christopher Lamb43c7f372007-04-22 19:24:39 +0000249 void setAlignment(unsigned Align);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000250
Nick Lewycky67760642009-09-27 07:38:41 +0000251 virtual StoreInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000252
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000253 Value *getPointerOperand() { return getOperand(1); }
254 const Value *getPointerOperand() const { return getOperand(1); }
255 static unsigned getPointerOperandIndex() { return 1U; }
256
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000257 unsigned getPointerAddressSpace() const {
258 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
259 }
260
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000261 // Methods for support type inquiry through isa, cast, and dyn_cast:
262 static inline bool classof(const StoreInst *) { return true; }
263 static inline bool classof(const Instruction *I) {
264 return I->getOpcode() == Instruction::Store;
265 }
266 static inline bool classof(const Value *V) {
267 return isa<Instruction>(V) && classof(cast<Instruction>(V));
268 }
269};
270
Gabor Greifefe65362008-05-10 08:32:32 +0000271template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +0000272struct OperandTraits<StoreInst> : public FixedNumOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +0000273};
274
275DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000276
277//===----------------------------------------------------------------------===//
278// GetElementPtrInst Class
279//===----------------------------------------------------------------------===//
280
David Greeneb8f74792007-09-04 15:46:09 +0000281// checkType - Simple wrapper function to give a better assertion failure
282// message on bad indexes for a gep instruction.
283//
284static inline const Type *checkType(const Type *Ty) {
285 assert(Ty && "Invalid GetElementPtrInst indices for type!");
286 return Ty;
287}
288
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000289/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
290/// access elements of arrays and structs
291///
292class GetElementPtrInst : public Instruction {
Gabor Greifefe65362008-05-10 08:32:32 +0000293 GetElementPtrInst(const GetElementPtrInst &GEPI);
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +0000294 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000295 const Twine &NameStr);
296 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000297
298 template<typename InputIterator>
299 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000300 const Twine &NameStr,
David Greeneb8f74792007-09-04 15:46:09 +0000301 // This argument ensures that we have an iterator we can
302 // do arithmetic on in constant time
303 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000304 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000305
David Greeneb8f74792007-09-04 15:46:09 +0000306 if (NumIdx > 0) {
Gabor Greifefe65362008-05-10 08:32:32 +0000307 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +0000308 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Gabor Greifefe65362008-05-10 08:32:32 +0000309 // we have to build an array here
David Greeneb8f74792007-09-04 15:46:09 +0000310 }
311 else {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000312 init(Ptr, 0, NumIdx, NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000313 }
David Greeneb8f74792007-09-04 15:46:09 +0000314 }
315
316 /// getIndexedType - Returns the type of the element that would be loaded with
317 /// a load instruction with the specified parameters.
318 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000319 /// Null is returned if the indices are invalid for the specified
David Greeneb8f74792007-09-04 15:46:09 +0000320 /// pointer type.
321 ///
David Greeneb8f74792007-09-04 15:46:09 +0000322 template<typename InputIterator>
323 static const Type *getIndexedType(const Type *Ptr,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000324 InputIterator IdxBegin,
David Greeneb8f74792007-09-04 15:46:09 +0000325 InputIterator IdxEnd,
David Greeneb8f74792007-09-04 15:46:09 +0000326 // This argument ensures that we
327 // have an iterator we can do
328 // arithmetic on in constant time
329 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000330 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
David Greeneb8f74792007-09-04 15:46:09 +0000331
Dan Gohman041e2eb2008-05-15 19:50:34 +0000332 if (NumIdx > 0)
David Greeneb8f74792007-09-04 15:46:09 +0000333 // This requires that the iterator points to contiguous memory.
David Greene2d5a0b92008-10-29 00:30:54 +0000334 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +0000335 else
336 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
David Greeneb8f74792007-09-04 15:46:09 +0000337 }
338
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000339 /// Constructors - Create a getelementptr instruction with a base pointer an
340 /// list of indices. The first ctor can optionally insert before an existing
341 /// instruction, the second appends the new instruction to the specified
342 /// BasicBlock.
David Greeneb8f74792007-09-04 15:46:09 +0000343 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000344 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
Gabor Greifefe65362008-05-10 08:32:32 +0000345 InputIterator IdxEnd,
346 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000347 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000348 Instruction *InsertBefore);
David Greeneb8f74792007-09-04 15:46:09 +0000349 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000350 inline GetElementPtrInst(Value *Ptr,
351 InputIterator IdxBegin, InputIterator IdxEnd,
352 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000353 const Twine &NameStr, BasicBlock *InsertAtEnd);
David Greeneb8f74792007-09-04 15:46:09 +0000354
Chris Lattner38bacf22005-05-03 05:43:30 +0000355 /// Constructors - These two constructors are convenience methods because one
356 /// and two index getelementptr instructions are so common.
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000357 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +0000358 Instruction *InsertBefore = 0);
Chris Lattner38bacf22005-05-03 05:43:30 +0000359 GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000360 const Twine &NameStr, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000361public:
362 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000363 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
Gabor Greif051a9502008-04-06 20:25:17 +0000364 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000365 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000366 Instruction *InsertBefore = 0) {
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000367 typename std::iterator_traits<InputIterator>::difference_type Values =
Gabor Greifefe65362008-05-10 08:32:32 +0000368 1 + std::distance(IdxBegin, IdxEnd);
369 return new(Values)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000370 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000371 }
372 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000373 static GetElementPtrInst *Create(Value *Ptr,
374 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000375 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000376 BasicBlock *InsertAtEnd) {
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000377 typename std::iterator_traits<InputIterator>::difference_type Values =
Gabor Greifefe65362008-05-10 08:32:32 +0000378 1 + std::distance(IdxBegin, IdxEnd);
379 return new(Values)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000380 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000381 }
382
Gabor Greifefe65362008-05-10 08:32:32 +0000383 /// Constructors - These two creators are convenience methods because one
384 /// index getelementptr instructions are so common.
Gabor Greif051a9502008-04-06 20:25:17 +0000385 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000386 const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +0000387 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000388 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000389 }
390 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000391 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000392 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000393 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000394 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000395
Dan Gohmane2574d32009-08-11 17:57:01 +0000396 /// Create an "inbounds" getelementptr. See the documentation for the
397 /// "inbounds" flag in LangRef.html for details.
398 template<typename InputIterator>
399 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
400 InputIterator IdxEnd,
401 const Twine &NameStr = "",
402 Instruction *InsertBefore = 0) {
403 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
404 NameStr, InsertBefore);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000405 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000406 return GEP;
407 }
408 template<typename InputIterator>
409 static GetElementPtrInst *CreateInBounds(Value *Ptr,
410 InputIterator IdxBegin,
411 InputIterator IdxEnd,
412 const Twine &NameStr,
413 BasicBlock *InsertAtEnd) {
414 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
415 NameStr, InsertAtEnd);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000416 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000417 return GEP;
418 }
419 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
420 const Twine &NameStr = "",
421 Instruction *InsertBefore = 0) {
422 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000423 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000424 return GEP;
425 }
426 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
427 const Twine &NameStr,
428 BasicBlock *InsertAtEnd) {
429 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000430 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000431 return GEP;
432 }
433
Nick Lewycky67760642009-09-27 07:38:41 +0000434 virtual GetElementPtrInst *clone() const;
Misha Brukman9769ab22005-04-21 20:19:05 +0000435
Gabor Greifefe65362008-05-10 08:32:32 +0000436 /// Transparently provide more efficient getOperand methods.
437 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
438
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000439 // getType - Overload to return most specific pointer type...
Devang Patel4d4a5e02008-02-23 01:11:02 +0000440 const PointerType *getType() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000441 return reinterpret_cast<const PointerType*>(Instruction::getType());
442 }
443
444 /// getIndexedType - Returns the type of the element that would be loaded with
445 /// a load instruction with the specified parameters.
446 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000447 /// Null is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000448 /// pointer type.
449 ///
David Greeneb8f74792007-09-04 15:46:09 +0000450 template<typename InputIterator>
Misha Brukman9769ab22005-04-21 20:19:05 +0000451 static const Type *getIndexedType(const Type *Ptr,
David Greeneb8f74792007-09-04 15:46:09 +0000452 InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +0000453 InputIterator IdxEnd) {
454 return getIndexedType(Ptr, IdxBegin, IdxEnd,
David Greeneb8f74792007-09-04 15:46:09 +0000455 typename std::iterator_traits<InputIterator>::
Dan Gohman041e2eb2008-05-15 19:50:34 +0000456 iterator_category());
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000457 }
Matthijs Kooijmane2afded2008-07-29 08:46:11 +0000458
459 static const Type *getIndexedType(const Type *Ptr,
460 Value* const *Idx, unsigned NumIdx);
461
462 static const Type *getIndexedType(const Type *Ptr,
463 uint64_t const *Idx, unsigned NumIdx);
464
Chris Lattner38bacf22005-05-03 05:43:30 +0000465 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
Misha Brukman9769ab22005-04-21 20:19:05 +0000466
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000467 inline op_iterator idx_begin() { return op_begin()+1; }
468 inline const_op_iterator idx_begin() const { return op_begin()+1; }
469 inline op_iterator idx_end() { return op_end(); }
470 inline const_op_iterator idx_end() const { return op_end(); }
471
472 Value *getPointerOperand() {
473 return getOperand(0);
474 }
475 const Value *getPointerOperand() const {
476 return getOperand(0);
477 }
478 static unsigned getPointerOperandIndex() {
479 return 0U; // get index for modifying correct operand
480 }
Chris Lattner8a67ac52009-08-30 20:06:40 +0000481
482 unsigned getPointerAddressSpace() const {
483 return cast<PointerType>(getType())->getAddressSpace();
484 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000485
Chris Lattner3bc6ced2009-01-09 05:27:40 +0000486 /// getPointerOperandType - Method to return the pointer operand as a
487 /// PointerType.
488 const PointerType *getPointerOperandType() const {
489 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
490 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000491
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000492
Devang Patel4d4a5e02008-02-23 01:11:02 +0000493 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000494 return getNumOperands() - 1;
495 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000496
Devang Patel4d4a5e02008-02-23 01:11:02 +0000497 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000498 return getNumOperands() > 1;
499 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000500
Chris Lattner6f771d42007-04-14 00:12:57 +0000501 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
502 /// zeros. If so, the result pointer and the first operand have the same
503 /// value, just potentially different types.
504 bool hasAllZeroIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000505
Chris Lattner6b0974c2007-04-27 20:35:56 +0000506 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
507 /// constant integers. If so, the result pointer and the first operand have
508 /// a constant offset between them.
509 bool hasAllConstantIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000510
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000511 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
512 /// See LangRef.html for the meaning of inbounds on a getelementptr.
Nick Lewyckyae05e7d2009-09-27 21:33:04 +0000513 void setIsInBounds(bool b = true);
514
515 /// isInBounds - Determine whether the GEP has the inbounds flag.
516 bool isInBounds() const;
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000517
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000518 // Methods for support type inquiry through isa, cast, and dyn_cast:
519 static inline bool classof(const GetElementPtrInst *) { return true; }
520 static inline bool classof(const Instruction *I) {
521 return (I->getOpcode() == Instruction::GetElementPtr);
522 }
523 static inline bool classof(const Value *V) {
524 return isa<Instruction>(V) && classof(cast<Instruction>(V));
525 }
526};
527
Gabor Greifefe65362008-05-10 08:32:32 +0000528template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +0000529struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
Gabor Greifefe65362008-05-10 08:32:32 +0000530};
531
532template<typename InputIterator>
533GetElementPtrInst::GetElementPtrInst(Value *Ptr,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000534 InputIterator IdxBegin,
Gabor Greifefe65362008-05-10 08:32:32 +0000535 InputIterator IdxEnd,
536 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000537 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000538 Instruction *InsertBefore)
539 : Instruction(PointerType::get(checkType(
540 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000541 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000542 cast<PointerType>(Ptr->getType())
543 ->getAddressSpace()),
544 GetElementPtr,
545 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
546 Values, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000547 init(Ptr, IdxBegin, IdxEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000548 typename std::iterator_traits<InputIterator>::iterator_category());
549}
550template<typename InputIterator>
551GetElementPtrInst::GetElementPtrInst(Value *Ptr,
552 InputIterator IdxBegin,
553 InputIterator IdxEnd,
554 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000555 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000556 BasicBlock *InsertAtEnd)
557 : Instruction(PointerType::get(checkType(
558 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000559 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000560 cast<PointerType>(Ptr->getType())
561 ->getAddressSpace()),
562 GetElementPtr,
563 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
564 Values, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000565 init(Ptr, IdxBegin, IdxEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000566 typename std::iterator_traits<InputIterator>::iterator_category());
567}
568
569
570DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
571
572
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000573//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000574// ICmpInst Class
575//===----------------------------------------------------------------------===//
576
577/// This instruction compares its operands according to the predicate given
Nate Begemanac80ade2008-05-12 19:01:56 +0000578/// to the constructor. It only operates on integers or pointers. The operands
579/// must be identical types.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000580/// @brief Represent an integer comparison operator.
581class ICmpInst: public CmpInst {
582public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000583 /// @brief Constructor with insert-before-instruction semantics.
584 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000585 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +0000586 Predicate pred, ///< The predicate to use for the comparison
587 Value *LHS, ///< The left-hand-side of the expression
588 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000589 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000590 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +0000591 Instruction::ICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000592 InsertBefore) {
593 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
594 pred <= CmpInst::LAST_ICMP_PREDICATE &&
595 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000596 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000597 "Both operands to ICmp instruction are not of the same type!");
598 // Check that the operands are the right type
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000599 assert((getOperand(0)->getType()->isIntOrIntVector() ||
Nate Begeman31cd33a2008-05-14 20:28:31 +0000600 isa<PointerType>(getOperand(0)->getType())) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000601 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000602 }
603
Owen Anderson333c4002009-07-09 23:48:35 +0000604 /// @brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000605 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000606 BasicBlock &InsertAtEnd, ///< Block to insert into.
607 Predicate pred, ///< The predicate to use for the comparison
608 Value *LHS, ///< The left-hand-side of the expression
609 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000610 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000611 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000612 Instruction::ICmp, pred, LHS, RHS, NameStr,
613 &InsertAtEnd) {
614 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
615 pred <= CmpInst::LAST_ICMP_PREDICATE &&
616 "Invalid ICmp predicate value");
617 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
618 "Both operands to ICmp instruction are not of the same type!");
619 // Check that the operands are the right type
620 assert((getOperand(0)->getType()->isIntOrIntVector() ||
621 isa<PointerType>(getOperand(0)->getType())) &&
622 "Invalid operand types for ICmp instruction");
623 }
624
625 /// @brief Constructor with no-insertion semantics
626 ICmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +0000627 Predicate pred, ///< The predicate to use for the comparison
628 Value *LHS, ///< The left-hand-side of the expression
629 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000630 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000631 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000632 Instruction::ICmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +0000633 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
634 pred <= CmpInst::LAST_ICMP_PREDICATE &&
635 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000636 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000637 "Both operands to ICmp instruction are not of the same type!");
638 // Check that the operands are the right type
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000639 assert((getOperand(0)->getType()->isIntOrIntVector() ||
Nate Begeman31cd33a2008-05-14 20:28:31 +0000640 isa<PointerType>(getOperand(0)->getType())) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000641 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000642 }
643
Reid Spencere4d87aa2006-12-23 06:05:41 +0000644 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
645 /// @returns the predicate that would be the result if the operand were
646 /// regarded as signed.
647 /// @brief Return the signed version of the predicate
648 Predicate getSignedPredicate() const {
649 return getSignedPredicate(getPredicate());
650 }
651
652 /// This is a static version that you can use without an instruction.
653 /// @brief Return the signed version of the predicate.
654 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000655
Nick Lewycky4189a532008-01-28 03:48:02 +0000656 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
657 /// @returns the predicate that would be the result if the operand were
658 /// regarded as unsigned.
659 /// @brief Return the unsigned version of the predicate
660 Predicate getUnsignedPredicate() const {
661 return getUnsignedPredicate(getPredicate());
662 }
663
664 /// This is a static version that you can use without an instruction.
665 /// @brief Return the unsigned version of the predicate.
666 static Predicate getUnsignedPredicate(Predicate pred);
667
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000668 /// isEquality - Return true if this predicate is either EQ or NE. This also
669 /// tests for commutativity.
670 static bool isEquality(Predicate P) {
671 return P == ICMP_EQ || P == ICMP_NE;
672 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000673
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000674 /// isEquality - Return true if this predicate is either EQ or NE. This also
675 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000676 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000677 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000678 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000679
680 /// @returns true if the predicate of this ICmpInst is commutative
681 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000682 bool isCommutative() const { return isEquality(); }
683
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000684 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000685 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +0000686 bool isRelational() const {
687 return !isEquality();
688 }
689
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000690 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000691 ///
692 static bool isRelational(Predicate P) {
693 return !isEquality(P);
694 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000695
Reid Spencere4d87aa2006-12-23 06:05:41 +0000696 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
697 /// @brief Determine if this instruction's predicate is signed.
Chris Lattner5bda9e42007-09-15 06:51:03 +0000698 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000699
700 /// @returns true if the predicate provided is signed, false otherwise
701 /// @brief Determine if the predicate is signed.
702 static bool isSignedPredicate(Predicate pred);
703
Nick Lewyckyfc1efbb2008-05-17 07:33:39 +0000704 /// @returns true if the specified compare predicate is
705 /// true when both operands are equal...
706 /// @brief Determine if the icmp is true when both operands are equal
707 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
708 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
709 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
710 pred == ICmpInst::ICMP_SLE;
711 }
712
713 /// @returns true if the specified compare instruction is
714 /// true when both operands are equal...
715 /// @brief Determine if the ICmpInst returns true when both operands are equal
716 bool isTrueWhenEqual() {
717 return isTrueWhenEqual(getPredicate());
718 }
719
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000720 /// Initialize a set of values that all satisfy the predicate with C.
Reid Spencer3da43842007-02-28 22:00:54 +0000721 /// @brief Make a ConstantRange for a relation with a constant value.
722 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
723
Reid Spencer45fb3f32006-11-20 01:22:35 +0000724 /// Exchange the two operands to this instruction in such a way that it does
725 /// not modify the semantics of the instruction. The predicate value may be
726 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000727 /// (e.g. ult).
Reid Spencer45fb3f32006-11-20 01:22:35 +0000728 /// @brief Swap operands and adjust predicate.
729 void swapOperands() {
730 SubclassData = getSwappedPredicate();
Gabor Greif94fb68b2008-05-13 22:51:52 +0000731 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000732 }
733
Nick Lewycky67760642009-09-27 07:38:41 +0000734 virtual ICmpInst *clone() const;
Chris Lattnercd406fe2007-08-24 20:48:18 +0000735
Reid Spencer45fb3f32006-11-20 01:22:35 +0000736 // Methods for support type inquiry through isa, cast, and dyn_cast:
737 static inline bool classof(const ICmpInst *) { return true; }
738 static inline bool classof(const Instruction *I) {
739 return I->getOpcode() == Instruction::ICmp;
740 }
741 static inline bool classof(const Value *V) {
742 return isa<Instruction>(V) && classof(cast<Instruction>(V));
743 }
Dan Gohmanf72fb672008-09-09 01:02:47 +0000744
Reid Spencer45fb3f32006-11-20 01:22:35 +0000745};
746
747//===----------------------------------------------------------------------===//
748// FCmpInst Class
749//===----------------------------------------------------------------------===//
750
751/// This instruction compares its operands according to the predicate given
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000752/// to the constructor. It only operates on floating point values or packed
Reid Spencer45fb3f32006-11-20 01:22:35 +0000753/// vectors of floating point values. The operands must be identical types.
754/// @brief Represents a floating point comparison operator.
755class FCmpInst: public CmpInst {
756public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000757 /// @brief Constructor with insert-before-instruction semantics.
758 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000759 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +0000760 Predicate pred, ///< The predicate to use for the comparison
761 Value *LHS, ///< The left-hand-side of the expression
762 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000763 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000764 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +0000765 Instruction::FCmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000766 InsertBefore) {
767 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
768 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000769 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000770 "Both operands to FCmp instruction are not of the same type!");
771 // Check that the operands are the right type
Dan Gohmanf72fb672008-09-09 01:02:47 +0000772 assert(getOperand(0)->getType()->isFPOrFPVector() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000773 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000774 }
Owen Anderson333c4002009-07-09 23:48:35 +0000775
776 /// @brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000777 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000778 BasicBlock &InsertAtEnd, ///< Block to insert into.
779 Predicate pred, ///< The predicate to use for the comparison
780 Value *LHS, ///< The left-hand-side of the expression
781 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000782 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000783 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000784 Instruction::FCmp, pred, LHS, RHS, NameStr,
785 &InsertAtEnd) {
786 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
787 "Invalid FCmp predicate value");
788 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
789 "Both operands to FCmp instruction are not of the same type!");
790 // Check that the operands are the right type
791 assert(getOperand(0)->getType()->isFPOrFPVector() &&
792 "Invalid operand types for FCmp instruction");
793 }
794
795 /// @brief Constructor with no-insertion semantics
796 FCmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +0000797 Predicate pred, ///< The predicate to use for the comparison
798 Value *LHS, ///< The left-hand-side of the expression
799 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000800 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000801 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000802 Instruction::FCmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +0000803 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
804 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000805 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000806 "Both operands to FCmp instruction are not of the same type!");
807 // Check that the operands are the right type
Dan Gohmanf72fb672008-09-09 01:02:47 +0000808 assert(getOperand(0)->getType()->isFPOrFPVector() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000809 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000810 }
811
Reid Spencer45fb3f32006-11-20 01:22:35 +0000812 /// @returns true if the predicate of this instruction is EQ or NE.
813 /// @brief Determine if this is an equality predicate.
814 bool isEquality() const {
815 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
816 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
817 }
Dan Gohman793df072008-09-16 16:44:00 +0000818
819 /// @returns true if the predicate of this instruction is commutative.
820 /// @brief Determine if this is a commutative predicate.
821 bool isCommutative() const {
822 return isEquality() ||
823 SubclassData == FCMP_FALSE ||
824 SubclassData == FCMP_TRUE ||
825 SubclassData == FCMP_ORD ||
826 SubclassData == FCMP_UNO;
827 }
Reid Spencer45fb3f32006-11-20 01:22:35 +0000828
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000829 /// @returns true if the predicate is relational (not EQ or NE).
Reid Spencer45fb3f32006-11-20 01:22:35 +0000830 /// @brief Determine if this a relational predicate.
831 bool isRelational() const { return !isEquality(); }
832
833 /// Exchange the two operands to this instruction in such a way that it does
834 /// not modify the semantics of the instruction. The predicate value may be
835 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000836 /// (e.g. ult).
Reid Spencer45fb3f32006-11-20 01:22:35 +0000837 /// @brief Swap operands and adjust predicate.
838 void swapOperands() {
839 SubclassData = getSwappedPredicate();
Gabor Greif94fb68b2008-05-13 22:51:52 +0000840 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000841 }
842
Nick Lewycky67760642009-09-27 07:38:41 +0000843 virtual FCmpInst *clone() const;
Chris Lattnercd406fe2007-08-24 20:48:18 +0000844
Reid Spencer45fb3f32006-11-20 01:22:35 +0000845 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
846 static inline bool classof(const FCmpInst *) { return true; }
847 static inline bool classof(const Instruction *I) {
848 return I->getOpcode() == Instruction::FCmp;
849 }
850 static inline bool classof(const Value *V) {
851 return isa<Instruction>(V) && classof(cast<Instruction>(V));
852 }
853};
854
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000855//===----------------------------------------------------------------------===//
856// CallInst Class
857//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000858/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +0000859/// machine's calling convention. This class uses low bit of the SubClassData
860/// field to indicate whether or not this is a tail call. The rest of the bits
861/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000862///
David Greene52eec542007-08-01 03:43:44 +0000863
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000864class CallInst : public Instruction {
Devang Patel05988662008-09-25 21:00:45 +0000865 AttrListPtr AttributeList; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000866 CallInst(const CallInst &CI);
Chris Lattnerd54f4322007-02-13 00:58:44 +0000867 void init(Value *Func, Value* const *Params, unsigned NumParams);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000868 void init(Value *Func, Value *Actual1, Value *Actual2);
869 void init(Value *Func, Value *Actual);
870 void init(Value *Func);
871
David Greene52eec542007-08-01 03:43:44 +0000872 template<typename InputIterator>
873 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000874 const Twine &NameStr,
David Greene52eec542007-08-01 03:43:44 +0000875 // This argument ensures that we have an iterator we can
876 // do arithmetic on in constant time
877 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000878 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000879
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000880 // This requires that the iterator points to contiguous memory.
881 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
Evan Cheng1bf9a182008-07-24 00:08:56 +0000882 setName(NameStr);
David Greene52eec542007-08-01 03:43:44 +0000883 }
884
David Greene52eec542007-08-01 03:43:44 +0000885 /// Construct a CallInst given a range of arguments. InputIterator
886 /// must be a random-access iterator pointing to contiguous storage
887 /// (e.g. a std::vector<>::iterator). Checks are made for
888 /// random-accessness but not for contiguous storage as that would
889 /// incur runtime overhead.
890 /// @brief Construct a CallInst from a range of arguments
891 template<typename InputIterator>
892 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000893 const Twine &NameStr, Instruction *InsertBefore);
David Greene52eec542007-08-01 03:43:44 +0000894
895 /// Construct a CallInst given a range of arguments. InputIterator
896 /// must be a random-access iterator pointing to contiguous storage
897 /// (e.g. a std::vector<>::iterator). Checks are made for
898 /// random-accessness but not for contiguous storage as that would
899 /// incur runtime overhead.
900 /// @brief Construct a CallInst from a range of arguments
901 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000902 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000903 const Twine &NameStr, BasicBlock *InsertAtEnd);
David Greene52eec542007-08-01 03:43:44 +0000904
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000905 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000906 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000907 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000908 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000909 explicit CallInst(Value *F, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000910 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000911 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000912public:
913 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000914 static CallInst *Create(Value *Func,
915 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000916 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +0000917 Instruction *InsertBefore = 0) {
Bill Wendlingc2e73532008-05-10 19:59:59 +0000918 return new((unsigned)(ArgEnd - ArgBegin + 1))
Evan Cheng1bf9a182008-07-24 00:08:56 +0000919 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000920 }
921 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000922 static CallInst *Create(Value *Func,
923 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000924 const Twine &NameStr, BasicBlock *InsertAtEnd) {
Bill Wendlingc2e73532008-05-10 19:59:59 +0000925 return new((unsigned)(ArgEnd - ArgBegin + 1))
Evan Cheng1bf9a182008-07-24 00:08:56 +0000926 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000927 }
Evan Cheng1bf9a182008-07-24 00:08:56 +0000928 static CallInst *Create(Value *F, Value *Actual,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000929 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000930 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000931 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000932 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000933 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
Gabor Greif051a9502008-04-06 20:25:17 +0000934 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000935 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000936 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000937 static CallInst *Create(Value *F, const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000938 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000939 return new(1) CallInst(F, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000940 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000941 static CallInst *Create(Value *F, const Twine &NameStr,
Evan Cheng34cd4a42008-05-05 18:30:58 +0000942 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000943 return new(1) CallInst(F, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000944 }
Evan Chengfabcb912009-09-10 04:36:43 +0000945 /// CreateMalloc - Generate the IR for a call to malloc:
946 /// 1. Compute the malloc call's argument as the specified type's size,
947 /// possibly multiplied by the array size if the array size is not
948 /// constant 1.
949 /// 2. Call malloc with that argument.
950 /// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewycky3fc35c52009-10-17 23:52:26 +0000951 static Instruction *CreateMalloc(Instruction *InsertBefore,
952 const Type *IntPtrTy, const Type *AllocTy,
953 Value *ArraySize = 0,
954 const Twine &Name = "");
955 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
956 const Type *IntPtrTy, const Type *AllocTy,
957 Value *ArraySize = 0, Function* MallocF = 0,
958 const Twine &Name = "");
Gabor Greif051a9502008-04-06 20:25:17 +0000959
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000960 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000961
Chris Lattner3340ffe2005-05-06 20:26:26 +0000962 bool isTailCall() const { return SubclassData & 1; }
Evan Cheng1bf9a182008-07-24 00:08:56 +0000963 void setTailCall(bool isTC = true) {
964 SubclassData = (SubclassData & ~1) | unsigned(isTC);
Chris Lattner3340ffe2005-05-06 20:26:26 +0000965 }
966
Nick Lewycky67760642009-09-27 07:38:41 +0000967 virtual CallInst *clone() const;
Dan Gohmanf2752502008-09-26 21:38:45 +0000968
969 /// Provide fast operand accessors
970 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000971
Chris Lattner3340ffe2005-05-06 20:26:26 +0000972 /// getCallingConv/setCallingConv - Get or set the calling convention of this
973 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000974 CallingConv::ID getCallingConv() const {
975 return static_cast<CallingConv::ID>(SubclassData >> 1);
976 }
977 void setCallingConv(CallingConv::ID CC) {
978 SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
Chris Lattner3340ffe2005-05-06 20:26:26 +0000979 }
Chris Lattnerddb6db42005-05-06 05:51:46 +0000980
Devang Patel05988662008-09-25 21:00:45 +0000981 /// getAttributes - Return the parameter attributes for this call.
Chris Lattner041221c2008-03-13 04:33:03 +0000982 ///
Devang Patel05988662008-09-25 21:00:45 +0000983 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencer4746ecf2007-04-09 15:01:12 +0000984
Dan Gohmanf2752502008-09-26 21:38:45 +0000985 /// setAttributes - Set the parameter attributes for this call.
986 ///
Devang Patel05988662008-09-25 21:00:45 +0000987 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000988
Devang Patel05988662008-09-25 21:00:45 +0000989 /// addAttribute - adds the attribute to the list of attributes.
990 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsdc024672007-11-27 13:23:08 +0000991
Devang Patel05988662008-09-25 21:00:45 +0000992 /// removeAttribute - removes the attribute from the list of attributes.
993 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +0000994
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000995 /// @brief Determine whether the call or the callee has the given attribute.
Dan Gohmanf2752502008-09-26 21:38:45 +0000996 bool paramHasAttr(unsigned i, Attributes attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000997
Dale Johannesen08e78b12008-02-22 17:49:45 +0000998 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +0000999 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00001000 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001001 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001002
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001003 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001004 bool doesNotAccessMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00001005 return paramHasAttr(~0, Attribute::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001006 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001007 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001008 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1009 else removeAttribute(~0, Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00001010 }
1011
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001012 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001013 bool onlyReadsMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00001014 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001015 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001016 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001017 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1018 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00001019 }
1020
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001021 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001022 bool doesNotReturn() const {
Devang Patel19c87462008-09-26 22:53:05 +00001023 return paramHasAttr(~0, Attribute::NoReturn);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001024 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001025 void setDoesNotReturn(bool DoesNotReturn = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001026 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1027 else removeAttribute(~0, Attribute::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00001028 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001029
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001030 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001031 bool doesNotThrow() const {
Devang Patel19c87462008-09-26 22:53:05 +00001032 return paramHasAttr(~0, Attribute::NoUnwind);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001033 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001034 void setDoesNotThrow(bool DoesNotThrow = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001035 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1036 else removeAttribute(~0, Attribute::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00001037 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001038
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001039 /// @brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00001040 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001041 bool hasStructRetAttr() const {
1042 // Be friendly and also check the callee.
Devang Patel05988662008-09-25 21:00:45 +00001043 return paramHasAttr(1, Attribute::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001044 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001045
Evan Chengf4a54982008-01-12 18:57:32 +00001046 /// @brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001047 bool hasByValArgument() const {
Devang Patel05988662008-09-25 21:00:45 +00001048 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001049 }
Evan Chengf4a54982008-01-12 18:57:32 +00001050
Dan Gohmanf2752502008-09-26 21:38:45 +00001051 /// getCalledFunction - Return the function called, or null if this is an
1052 /// indirect function invocation.
1053 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001054 Function *getCalledFunction() const {
Gabor Greif7e07c002009-03-12 23:13:03 +00001055 return dyn_cast<Function>(Op<0>());
Chris Lattner721aef62004-11-18 17:46:57 +00001056 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001057
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001058 /// getCalledValue - Get a pointer to the function that is invoked by this
Chris Lattner14d80382009-10-18 05:08:07 +00001059 /// instruction.
Gabor Greif7e07c002009-03-12 23:13:03 +00001060 const Value *getCalledValue() const { return Op<0>(); }
1061 Value *getCalledValue() { return Op<0>(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001062
Chris Lattner14d80382009-10-18 05:08:07 +00001063 /// setCalledFunction - Set the function called.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001064 void setCalledFunction(Value* Fn) {
1065 Op<0>() = Fn;
1066 }
1067
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001068 // Methods for support type inquiry through isa, cast, and dyn_cast:
1069 static inline bool classof(const CallInst *) { return true; }
1070 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001071 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001072 }
1073 static inline bool classof(const Value *V) {
1074 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1075 }
1076};
1077
Gabor Greifefe65362008-05-10 08:32:32 +00001078template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001079struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
Gabor Greifefe65362008-05-10 08:32:32 +00001080};
1081
1082template<typename InputIterator>
1083CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001084 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001085 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1086 ->getElementType())->getReturnType(),
1087 Instruction::Call,
1088 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001089 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001090 init(Func, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001091 typename std::iterator_traits<InputIterator>::iterator_category());
1092}
1093
1094template<typename InputIterator>
1095CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001096 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00001097 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1098 ->getElementType())->getReturnType(),
1099 Instruction::Call,
1100 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001101 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001102 init(Func, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001103 typename std::iterator_traits<InputIterator>::iterator_category());
1104}
1105
1106DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1107
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001108//===----------------------------------------------------------------------===//
1109// SelectInst Class
1110//===----------------------------------------------------------------------===//
1111
1112/// SelectInst - This class represents the LLVM 'select' instruction.
1113///
1114class SelectInst : public Instruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001115 void init(Value *C, Value *S1, Value *S2) {
Chris Lattnerb76ec322008-12-29 00:12:50 +00001116 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
Gabor Greifefe65362008-05-10 08:32:32 +00001117 Op<0>() = C;
1118 Op<1>() = S1;
1119 Op<2>() = S2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001120 }
1121
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001122 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001123 Instruction *InsertBefore)
1124 : Instruction(S1->getType(), Instruction::Select,
1125 &Op<0>(), 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001126 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001127 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001128 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001129 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001130 BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001131 : Instruction(S1->getType(), Instruction::Select,
1132 &Op<0>(), 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001133 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001134 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001135 }
Gabor Greif051a9502008-04-06 20:25:17 +00001136public:
Evan Chengd69bb1a2008-05-05 17:41:03 +00001137 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001138 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001139 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001140 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001141 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001142 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001143 const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001144 BasicBlock *InsertAtEnd) {
1145 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001146 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001147
Chris Lattner97159122009-09-08 03:32:53 +00001148 const Value *getCondition() const { return Op<0>(); }
1149 const Value *getTrueValue() const { return Op<1>(); }
1150 const Value *getFalseValue() const { return Op<2>(); }
1151 Value *getCondition() { return Op<0>(); }
1152 Value *getTrueValue() { return Op<1>(); }
1153 Value *getFalseValue() { return Op<2>(); }
1154
Chris Lattnerb76ec322008-12-29 00:12:50 +00001155 /// areInvalidOperands - Return a string if the specified operands are invalid
1156 /// for a select operation, otherwise return null.
1157 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
Chris Lattner454928e2005-01-29 00:31:36 +00001158
1159 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001160 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001161
1162 OtherOps getOpcode() const {
1163 return static_cast<OtherOps>(Instruction::getOpcode());
1164 }
1165
Nick Lewycky67760642009-09-27 07:38:41 +00001166 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001167
1168 // Methods for support type inquiry through isa, cast, and dyn_cast:
1169 static inline bool classof(const SelectInst *) { return true; }
1170 static inline bool classof(const Instruction *I) {
1171 return I->getOpcode() == Instruction::Select;
1172 }
1173 static inline bool classof(const Value *V) {
1174 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1175 }
1176};
1177
Gabor Greifefe65362008-05-10 08:32:32 +00001178template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001179struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001180};
1181
1182DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1183
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001184//===----------------------------------------------------------------------===//
1185// VAArgInst Class
1186//===----------------------------------------------------------------------===//
1187
1188/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001189/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001190///
Chris Lattner454928e2005-01-29 00:31:36 +00001191class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001192public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001193 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001194 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001195 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001196 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001197 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001198 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001199 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001200 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001201 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001202 }
1203
Nick Lewycky67760642009-09-27 07:38:41 +00001204 virtual VAArgInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001205
1206 // Methods for support type inquiry through isa, cast, and dyn_cast:
1207 static inline bool classof(const VAArgInst *) { return true; }
1208 static inline bool classof(const Instruction *I) {
1209 return I->getOpcode() == VAArg;
1210 }
1211 static inline bool classof(const Value *V) {
1212 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1213 }
1214};
1215
1216//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001217// ExtractElementInst Class
1218//===----------------------------------------------------------------------===//
1219
1220/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001221/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001222///
1223class ExtractElementInst : public Instruction {
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001224 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
Chris Lattner9fc18d22006-04-08 01:15:18 +00001225 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001226 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
Chris Lattner9fc18d22006-04-08 01:15:18 +00001227 BasicBlock *InsertAtEnd);
Eric Christophera3500da2009-07-25 02:28:41 +00001228public:
Eric Christophera3500da2009-07-25 02:28:41 +00001229 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001230 const Twine &NameStr = "",
Eric Christophera3500da2009-07-25 02:28:41 +00001231 Instruction *InsertBefore = 0) {
1232 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1233 }
1234 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001235 const Twine &NameStr,
Eric Christophera3500da2009-07-25 02:28:41 +00001236 BasicBlock *InsertAtEnd) {
1237 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1238 }
Robert Bocchino49b78a52006-01-10 19:04:13 +00001239
Chris Lattnerfa495842006-04-08 04:04:54 +00001240 /// isValidOperands - Return true if an extractelement instruction can be
1241 /// formed with the specified operands.
1242 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001243
Nick Lewycky67760642009-09-27 07:38:41 +00001244 virtual ExtractElementInst *clone() const;
Robert Bocchino49b78a52006-01-10 19:04:13 +00001245
Chris Lattner97159122009-09-08 03:32:53 +00001246 Value *getVectorOperand() { return Op<0>(); }
1247 Value *getIndexOperand() { return Op<1>(); }
1248 const Value *getVectorOperand() const { return Op<0>(); }
1249 const Value *getIndexOperand() const { return Op<1>(); }
1250
1251 const VectorType *getVectorOperandType() const {
Chris Lattnerc8dee3f2009-09-08 03:39:55 +00001252 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
Chris Lattner97159122009-09-08 03:32:53 +00001253 }
1254
1255
Robert Bocchino49b78a52006-01-10 19:04:13 +00001256 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001257 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001258
1259 // Methods for support type inquiry through isa, cast, and dyn_cast:
1260 static inline bool classof(const ExtractElementInst *) { return true; }
1261 static inline bool classof(const Instruction *I) {
1262 return I->getOpcode() == Instruction::ExtractElement;
1263 }
1264 static inline bool classof(const Value *V) {
1265 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1266 }
1267};
1268
Gabor Greifefe65362008-05-10 08:32:32 +00001269template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001270struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00001271};
1272
1273DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1274
Robert Bocchino49b78a52006-01-10 19:04:13 +00001275//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001276// InsertElementInst Class
1277//===----------------------------------------------------------------------===//
1278
1279/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001280/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001281///
1282class InsertElementInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001283 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001284 const Twine &NameStr = "",
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001285 Instruction *InsertBefore = 0);
Gabor Greif051a9502008-04-06 20:25:17 +00001286 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001287 const Twine &NameStr, BasicBlock *InsertAtEnd);
Robert Bocchinof9993442006-01-17 20:05:59 +00001288public:
Gabor Greif051a9502008-04-06 20:25:17 +00001289 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001290 const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +00001291 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001292 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001293 }
Gabor Greif051a9502008-04-06 20:25:17 +00001294 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001295 const Twine &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001296 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001297 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001298 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001299
Chris Lattnerfa495842006-04-08 04:04:54 +00001300 /// isValidOperands - Return true if an insertelement instruction can be
1301 /// formed with the specified operands.
1302 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1303 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001304
Nick Lewycky67760642009-09-27 07:38:41 +00001305 virtual InsertElementInst *clone() const;
Robert Bocchinof9993442006-01-17 20:05:59 +00001306
Reid Spencerac9dcb92007-02-15 03:39:18 +00001307 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001308 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001309 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001310 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001311 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001312
Robert Bocchinof9993442006-01-17 20:05:59 +00001313 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001314 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchinof9993442006-01-17 20:05:59 +00001315
1316 // Methods for support type inquiry through isa, cast, and dyn_cast:
1317 static inline bool classof(const InsertElementInst *) { return true; }
1318 static inline bool classof(const Instruction *I) {
1319 return I->getOpcode() == Instruction::InsertElement;
1320 }
1321 static inline bool classof(const Value *V) {
1322 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1323 }
1324};
1325
Gabor Greifefe65362008-05-10 08:32:32 +00001326template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001327struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001328};
1329
1330DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1331
Robert Bocchinof9993442006-01-17 20:05:59 +00001332//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001333// ShuffleVectorInst Class
1334//===----------------------------------------------------------------------===//
1335
1336/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1337/// input vectors.
1338///
1339class ShuffleVectorInst : public Instruction {
Chris Lattner9fc18d22006-04-08 01:15:18 +00001340public:
Gabor Greif051a9502008-04-06 20:25:17 +00001341 // allocate space for exactly three operands
1342 void *operator new(size_t s) {
1343 return User::operator new(s, 3);
1344 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001345 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001346 const Twine &NameStr = "",
Evan Cheng1bf9a182008-07-24 00:08:56 +00001347 Instruction *InsertBefor = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001348 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001349 const Twine &NameStr, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001350
Chris Lattnerfa495842006-04-08 04:04:54 +00001351 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001352 /// formed with the specified operands.
1353 static bool isValidOperands(const Value *V1, const Value *V2,
1354 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001355
Nick Lewycky67760642009-09-27 07:38:41 +00001356 virtual ShuffleVectorInst *clone() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001357
Reid Spencerac9dcb92007-02-15 03:39:18 +00001358 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001359 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001360 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001361 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001362 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001363
Chris Lattner9fc18d22006-04-08 01:15:18 +00001364 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001365 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001366
Chris Lattner8728f192008-03-02 05:28:33 +00001367 /// getMaskValue - Return the index from the shuffle mask for the specified
1368 /// output result. This is either -1 if the element is undef or a number less
1369 /// than 2*numelements.
1370 int getMaskValue(unsigned i) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001371
Chris Lattner9fc18d22006-04-08 01:15:18 +00001372 // Methods for support type inquiry through isa, cast, and dyn_cast:
1373 static inline bool classof(const ShuffleVectorInst *) { return true; }
1374 static inline bool classof(const Instruction *I) {
1375 return I->getOpcode() == Instruction::ShuffleVector;
1376 }
1377 static inline bool classof(const Value *V) {
1378 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1379 }
1380};
1381
Gabor Greifefe65362008-05-10 08:32:32 +00001382template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001383struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001384};
1385
1386DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
Chris Lattner9fc18d22006-04-08 01:15:18 +00001387
1388//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001389// ExtractValueInst Class
1390//===----------------------------------------------------------------------===//
1391
Dan Gohmane2d896f2008-05-15 23:35:32 +00001392/// ExtractValueInst - This instruction extracts a struct member or array
1393/// element value from an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001394///
Gabor Greifd4f268b2008-06-06 20:28:12 +00001395class ExtractValueInst : public UnaryInstruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001396 SmallVector<unsigned, 4> Indices;
1397
Dan Gohman041e2eb2008-05-15 19:50:34 +00001398 ExtractValueInst(const ExtractValueInst &EVI);
Gabor Greif76aca6f2008-06-06 21:06:32 +00001399 void init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001400 const Twine &NameStr);
1401 void init(unsigned Idx, const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001402
1403 template<typename InputIterator>
Gabor Greif76aca6f2008-06-06 21:06:32 +00001404 void init(InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001405 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001406 // This argument ensures that we have an iterator we can
1407 // do arithmetic on in constant time
1408 std::random_access_iterator_tag) {
1409 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001410
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001411 // There's no fundamental reason why we require at least one index
1412 // (other than weirdness with &*IdxBegin being invalid; see
1413 // getelementptr's init routine for example). But there's no
1414 // present need to support it.
1415 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1416
1417 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +00001418 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001419 // we have to build an array here
Dan Gohman041e2eb2008-05-15 19:50:34 +00001420 }
1421
1422 /// getIndexedType - Returns the type of the element that would be extracted
1423 /// with an extractvalue instruction with the specified parameters.
1424 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +00001425 /// Null is returned if the indices are invalid for the specified
Dan Gohman041e2eb2008-05-15 19:50:34 +00001426 /// pointer type.
1427 ///
1428 static const Type *getIndexedType(const Type *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001429 const unsigned *Idx, unsigned NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001430
1431 template<typename InputIterator>
1432 static const Type *getIndexedType(const Type *Ptr,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001433 InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001434 InputIterator IdxEnd,
1435 // This argument ensures that we
1436 // have an iterator we can do
1437 // arithmetic on in constant time
1438 std::random_access_iterator_tag) {
1439 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1440
1441 if (NumIdx > 0)
1442 // This requires that the iterator points to contiguous memory.
Dan Gohman19a81632008-06-23 16:38:10 +00001443 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001444 else
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001445 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001446 }
1447
Dan Gohmane2d896f2008-05-15 23:35:32 +00001448 /// Constructors - Create a extractvalue instruction with a base aggregate
1449 /// value and a list of indices. The first ctor can optionally insert before
1450 /// an existing instruction, the second appends the new instruction to the
1451 /// specified BasicBlock.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001452 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001453 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001454 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001455 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001456 Instruction *InsertBefore);
1457 template<typename InputIterator>
1458 inline ExtractValueInst(Value *Agg,
1459 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001460 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001461
Dan Gohman8e640412008-05-31 19:09:47 +00001462 // allocate space for exactly one operand
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001463 void *operator new(size_t s) {
1464 return User::operator new(s, 1);
1465 }
1466
Gabor Greifd4f268b2008-06-06 20:28:12 +00001467public:
Dan Gohman041e2eb2008-05-15 19:50:34 +00001468 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001469 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001470 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001471 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001472 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001473 return new
Evan Cheng1bf9a182008-07-24 00:08:56 +00001474 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001475 }
1476 template<typename InputIterator>
1477 static ExtractValueInst *Create(Value *Agg,
1478 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001479 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001480 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001481 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001482 }
1483
1484 /// Constructors - These two creators are convenience methods because one
1485 /// index extractvalue instructions are much more common than those with
1486 /// more than one.
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001487 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001488 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001489 Instruction *InsertBefore = 0) {
Dan Gohman2f27e172008-06-23 16:48:17 +00001490 unsigned Idxs[1] = { Idx };
Evan Cheng1bf9a182008-07-24 00:08:56 +00001491 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001492 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001493 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001494 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001495 BasicBlock *InsertAtEnd) {
Dan Gohman2f27e172008-06-23 16:48:17 +00001496 unsigned Idxs[1] = { Idx };
Evan Cheng1bf9a182008-07-24 00:08:56 +00001497 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001498 }
1499
Nick Lewycky67760642009-09-27 07:38:41 +00001500 virtual ExtractValueInst *clone() const;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001501
Dan Gohman041e2eb2008-05-15 19:50:34 +00001502 /// getIndexedType - Returns the type of the element that would be extracted
1503 /// with an extractvalue instruction with the specified parameters.
1504 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +00001505 /// Null is returned if the indices are invalid for the specified
Dan Gohman041e2eb2008-05-15 19:50:34 +00001506 /// pointer type.
1507 ///
1508 template<typename InputIterator>
1509 static const Type *getIndexedType(const Type *Ptr,
1510 InputIterator IdxBegin,
1511 InputIterator IdxEnd) {
1512 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1513 typename std::iterator_traits<InputIterator>::
1514 iterator_category());
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001515 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001516 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001517
Owen Anderson5678d6e2008-06-19 17:15:57 +00001518 typedef const unsigned* idx_iterator;
1519 inline idx_iterator idx_begin() const { return Indices.begin(); }
1520 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001521
1522 Value *getAggregateOperand() {
1523 return getOperand(0);
1524 }
1525 const Value *getAggregateOperand() const {
1526 return getOperand(0);
1527 }
1528 static unsigned getAggregateOperandIndex() {
1529 return 0U; // get index for modifying correct operand
1530 }
1531
1532 unsigned getNumIndices() const { // Note: always non-negative
Bill Wendling67944fc2008-06-05 07:35:27 +00001533 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001534 }
1535
1536 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001537 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001538 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001539
Dan Gohman041e2eb2008-05-15 19:50:34 +00001540 // Methods for support type inquiry through isa, cast, and dyn_cast:
1541 static inline bool classof(const ExtractValueInst *) { return true; }
1542 static inline bool classof(const Instruction *I) {
1543 return I->getOpcode() == Instruction::ExtractValue;
1544 }
1545 static inline bool classof(const Value *V) {
1546 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1547 }
1548};
1549
Dan Gohmane4569942008-05-23 00:36:11 +00001550template<typename InputIterator>
1551ExtractValueInst::ExtractValueInst(Value *Agg,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001552 InputIterator IdxBegin,
Dan Gohmane4569942008-05-23 00:36:11 +00001553 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001554 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001555 Instruction *InsertBefore)
Gabor Greifd4f268b2008-06-06 20:28:12 +00001556 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
Bill Wendling85f40542008-07-22 07:14:12 +00001557 IdxBegin, IdxEnd)),
1558 ExtractValue, Agg, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001559 init(IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001560 typename std::iterator_traits<InputIterator>::iterator_category());
1561}
1562template<typename InputIterator>
1563ExtractValueInst::ExtractValueInst(Value *Agg,
1564 InputIterator IdxBegin,
1565 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001566 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001567 BasicBlock *InsertAtEnd)
Gabor Greifd4f268b2008-06-06 20:28:12 +00001568 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
Bill Wendling85f40542008-07-22 07:14:12 +00001569 IdxBegin, IdxEnd)),
1570 ExtractValue, Agg, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001571 init(IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001572 typename std::iterator_traits<InputIterator>::iterator_category());
1573}
1574
Dan Gohmane4569942008-05-23 00:36:11 +00001575
Dan Gohman041e2eb2008-05-15 19:50:34 +00001576//===----------------------------------------------------------------------===//
1577// InsertValueInst Class
1578//===----------------------------------------------------------------------===//
1579
Dan Gohmane2d896f2008-05-15 23:35:32 +00001580/// InsertValueInst - This instruction inserts a struct field of array element
1581/// value into an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001582///
1583class InsertValueInst : public Instruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001584 SmallVector<unsigned, 4> Indices;
1585
1586 void *operator new(size_t, unsigned); // Do not implement
Dan Gohman041e2eb2008-05-15 19:50:34 +00001587 InsertValueInst(const InsertValueInst &IVI);
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +00001588 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001589 const Twine &NameStr);
1590 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001591
1592 template<typename InputIterator>
1593 void init(Value *Agg, Value *Val,
1594 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001595 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001596 // This argument ensures that we have an iterator we can
1597 // do arithmetic on in constant time
1598 std::random_access_iterator_tag) {
1599 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001600
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001601 // There's no fundamental reason why we require at least one index
1602 // (other than weirdness with &*IdxBegin being invalid; see
1603 // getelementptr's init routine for example). But there's no
1604 // present need to support it.
1605 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1606
1607 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +00001608 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001609 // we have to build an array here
Dan Gohman041e2eb2008-05-15 19:50:34 +00001610 }
1611
Dan Gohmane2d896f2008-05-15 23:35:32 +00001612 /// Constructors - Create a insertvalue instruction with a base aggregate
1613 /// value, a value to insert, and a list of indices. The first ctor can
1614 /// optionally insert before an existing instruction, the second appends
1615 /// the new instruction to the specified BasicBlock.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001616 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001617 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001618 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001619 const Twine &NameStr,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001620 Instruction *InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001621 template<typename InputIterator>
1622 inline InsertValueInst(Value *Agg, Value *Val,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001623 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001624 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001625
1626 /// Constructors - These two constructors are convenience methods because one
1627 /// and two index insertvalue instructions are so common.
1628 InsertValueInst(Value *Agg, Value *Val,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001629 unsigned Idx, const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001630 Instruction *InsertBefore = 0);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001631 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001632 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001633public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001634 // allocate space for exactly two operands
1635 void *operator new(size_t s) {
1636 return User::operator new(s, 2);
1637 }
1638
Dan Gohman041e2eb2008-05-15 19:50:34 +00001639 template<typename InputIterator>
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +00001640 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001641 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001642 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001643 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001644 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001645 NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001646 }
1647 template<typename InputIterator>
1648 static InsertValueInst *Create(Value *Agg, Value *Val,
1649 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001650 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001651 BasicBlock *InsertAtEnd) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001652 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001653 NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001654 }
1655
1656 /// Constructors - These two creators are convenience methods because one
1657 /// index insertvalue instructions are much more common than those with
1658 /// more than one.
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001659 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001660 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001661 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001662 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001663 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001664 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001665 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001666 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001667 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001668 }
1669
Nick Lewycky67760642009-09-27 07:38:41 +00001670 virtual InsertValueInst *clone() const;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001671
1672 /// Transparently provide more efficient getOperand methods.
1673 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1674
Owen Anderson5678d6e2008-06-19 17:15:57 +00001675 typedef const unsigned* idx_iterator;
1676 inline idx_iterator idx_begin() const { return Indices.begin(); }
1677 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001678
1679 Value *getAggregateOperand() {
1680 return getOperand(0);
1681 }
1682 const Value *getAggregateOperand() const {
1683 return getOperand(0);
1684 }
1685 static unsigned getAggregateOperandIndex() {
1686 return 0U; // get index for modifying correct operand
1687 }
1688
1689 Value *getInsertedValueOperand() {
1690 return getOperand(1);
1691 }
1692 const Value *getInsertedValueOperand() const {
1693 return getOperand(1);
1694 }
1695 static unsigned getInsertedValueOperandIndex() {
1696 return 1U; // get index for modifying correct operand
1697 }
1698
1699 unsigned getNumIndices() const { // Note: always non-negative
Bill Wendling67944fc2008-06-05 07:35:27 +00001700 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001701 }
1702
1703 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001704 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001705 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001706
Dan Gohman041e2eb2008-05-15 19:50:34 +00001707 // Methods for support type inquiry through isa, cast, and dyn_cast:
1708 static inline bool classof(const InsertValueInst *) { return true; }
1709 static inline bool classof(const Instruction *I) {
1710 return I->getOpcode() == Instruction::InsertValue;
1711 }
1712 static inline bool classof(const Value *V) {
1713 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1714 }
1715};
1716
1717template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001718struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001719};
1720
Dan Gohmane4569942008-05-23 00:36:11 +00001721template<typename InputIterator>
1722InsertValueInst::InsertValueInst(Value *Agg,
1723 Value *Val,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001724 InputIterator IdxBegin,
Dan Gohmane4569942008-05-23 00:36:11 +00001725 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001726 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001727 Instruction *InsertBefore)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001728 : Instruction(Agg->getType(), InsertValue,
1729 OperandTraits<InsertValueInst>::op_begin(this),
1730 2, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001731 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001732 typename std::iterator_traits<InputIterator>::iterator_category());
1733}
1734template<typename InputIterator>
1735InsertValueInst::InsertValueInst(Value *Agg,
1736 Value *Val,
1737 InputIterator IdxBegin,
1738 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001739 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001740 BasicBlock *InsertAtEnd)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001741 : Instruction(Agg->getType(), InsertValue,
1742 OperandTraits<InsertValueInst>::op_begin(this),
1743 2, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001744 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001745 typename std::iterator_traits<InputIterator>::iterator_category());
1746}
1747
Dan Gohman041e2eb2008-05-15 19:50:34 +00001748DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1749
1750//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001751// PHINode Class
1752//===----------------------------------------------------------------------===//
1753
1754// PHINode - The PHINode class is used to represent the magical mystical PHI
1755// node, that can not exist in nature, but can be synthesized in a computer
1756// scientist's overactive imagination.
1757//
1758class PHINode : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001759 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00001760 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1761 /// the number actually in use.
1762 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001763 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00001764 // allocate space for exactly zero operands
1765 void *operator new(size_t s) {
1766 return User::operator new(s, 0);
1767 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001768 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001769 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001770 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Chris Lattner454928e2005-01-29 00:31:36 +00001771 ReservedSpace(0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001772 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001773 }
1774
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001775 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001776 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Chris Lattner454928e2005-01-29 00:31:36 +00001777 ReservedSpace(0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001778 setName(NameStr);
Chris Lattner454928e2005-01-29 00:31:36 +00001779 }
Gabor Greif051a9502008-04-06 20:25:17 +00001780public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001781 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001782 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001783 return new PHINode(Ty, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001784 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001785 static PHINode *Create(const Type *Ty, const Twine &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001786 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001787 return new PHINode(Ty, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001788 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001789 ~PHINode();
1790
Chris Lattner454928e2005-01-29 00:31:36 +00001791 /// reserveOperandSpace - This method can be used to avoid repeated
1792 /// reallocation of PHI operand lists by reserving space for the correct
1793 /// number of operands before adding them. Unlike normal vector reserves,
1794 /// this method can also be used to trim the operand space.
1795 void reserveOperandSpace(unsigned NumValues) {
1796 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001797 }
1798
Nick Lewycky67760642009-09-27 07:38:41 +00001799 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001800
Gabor Greifefe65362008-05-10 08:32:32 +00001801 /// Provide fast operand accessors
1802 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1803
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001804 /// getNumIncomingValues - Return the number of incoming edges
1805 ///
Chris Lattner454928e2005-01-29 00:31:36 +00001806 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001807
Reid Spencerc773de62006-05-19 19:07:54 +00001808 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001809 ///
1810 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001811 assert(i*2 < getNumOperands() && "Invalid value number!");
1812 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001813 }
1814 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +00001815 assert(i*2 < getNumOperands() && "Invalid value number!");
1816 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001817 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001818 static unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001819 return i*2;
1820 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001821 static unsigned getIncomingValueNumForOperand(unsigned i) {
1822 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1823 return i/2;
1824 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001825
Chris Lattnerceaa4572009-10-10 07:42:42 +00001826 /// getIncomingBlock - Return incoming basic block #i.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001827 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00001828 BasicBlock *getIncomingBlock(unsigned i) const {
Chris Lattnerfc61aef2009-10-10 08:01:27 +00001829 return cast<BasicBlock>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001830 }
Chris Lattnerceaa4572009-10-10 07:42:42 +00001831
1832 /// getIncomingBlock - Return incoming basic block corresponding
1833 /// to an operand of the PHI.
1834 ///
1835 BasicBlock *getIncomingBlock(const Use &U) const {
1836 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
Chris Lattnerfc61aef2009-10-10 08:01:27 +00001837 return cast<BasicBlock>((&U + 1)->get());
Chris Lattnerceaa4572009-10-10 07:42:42 +00001838 }
1839
1840 /// getIncomingBlock - Return incoming basic block corresponding
1841 /// to value use iterator.
1842 ///
1843 template <typename U>
1844 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1845 return getIncomingBlock(I.getUse());
1846 }
1847
1848
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001849 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Gabor Greifefe65362008-05-10 08:32:32 +00001850 setOperand(i*2+1, BB);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001851 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001852 static unsigned getOperandNumForIncomingBlock(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001853 return i*2+1;
1854 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001855 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1856 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1857 return i/2;
1858 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001859
1860 /// addIncoming - Add an incoming value to the end of the PHI list
1861 ///
1862 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00001863 assert(V && "PHI node got a null value!");
1864 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001865 assert(getType() == V->getType() &&
1866 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +00001867 unsigned OpNo = NumOperands;
1868 if (OpNo+2 > ReservedSpace)
1869 resizeOperands(0); // Get more space!
1870 // Initialize some new operands.
1871 NumOperands = OpNo+2;
Gabor Greif6c80c382008-05-26 21:33:52 +00001872 OperandList[OpNo] = V;
1873 OperandList[OpNo+1] = BB;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001874 }
Misha Brukman9769ab22005-04-21 20:19:05 +00001875
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001876 /// removeIncomingValue - Remove an incoming value. This is useful if a
1877 /// predecessor basic block is deleted. The value removed is returned.
1878 ///
1879 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1880 /// is true), the PHI node is destroyed and any uses of it are replaced with
1881 /// dummy values. The only time there should be zero incoming values to a PHI
1882 /// node is when the block is dead, so this strategy is sound.
1883 ///
1884 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1885
Gabor Greifefe65362008-05-10 08:32:32 +00001886 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001887 int Idx = getBasicBlockIndex(BB);
1888 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1889 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1890 }
1891
Misha Brukman9769ab22005-04-21 20:19:05 +00001892 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001893 /// block in the value list for this PHI. Returns -1 if no instance.
1894 ///
1895 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001896 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00001897 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Gabor Greifefe65362008-05-10 08:32:32 +00001898 if (OL[i+1].get() == BB) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001899 return -1;
1900 }
1901
1902 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1903 return getIncomingValue(getBasicBlockIndex(BB));
1904 }
1905
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001906 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00001907 /// same value, return the value, otherwise return null.
1908 ///
Dan Gohmanbccfc242009-09-03 15:34:35 +00001909 /// If the PHI has undef operands, but all the rest of the operands are
1910 /// some unique value, return that value if it can be proved that the
1911 /// value dominates the PHI. If DT is null, use a conservative check,
1912 /// otherwise use DT to test for dominance.
1913 ///
1914 Value *hasConstantValue(DominatorTree *DT = 0) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001915
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001916 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1917 static inline bool classof(const PHINode *) { return true; }
1918 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001919 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001920 }
1921 static inline bool classof(const Value *V) {
1922 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1923 }
Chris Lattner454928e2005-01-29 00:31:36 +00001924 private:
1925 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001926};
1927
Gabor Greifefe65362008-05-10 08:32:32 +00001928template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001929struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00001930};
1931
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001932DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00001933
1934
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001935//===----------------------------------------------------------------------===//
1936// ReturnInst Class
1937//===----------------------------------------------------------------------===//
1938
1939//===---------------------------------------------------------------------------
1940/// ReturnInst - Return a value (possibly void), from a function. Execution
1941/// does not continue in this function any longer.
1942///
1943class ReturnInst : public TerminatorInst {
Chris Lattner910c80a2007-02-24 00:55:48 +00001944 ReturnInst(const ReturnInst &RI);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001945
Gabor Greif051a9502008-04-06 20:25:17 +00001946private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001947 // ReturnInst constructors:
1948 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001949 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001950 // ReturnInst(Value* X) - 'ret X' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00001951 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001952 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
Gabor Greifefe65362008-05-10 08:32:32 +00001953 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1954 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001955 //
1956 // NOTE: If the Value* passed is of type void then the constructor behaves as
1957 // if it was passed NULL.
Owen Anderson1d0be152009-08-13 21:58:54 +00001958 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1959 Instruction *InsertBefore = 0);
1960 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1961 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001962public:
Owen Anderson1d0be152009-08-13 21:58:54 +00001963 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1964 Instruction *InsertBefore = 0) {
1965 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001966 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001967 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1968 BasicBlock *InsertAtEnd) {
1969 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001970 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001971 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1972 return new(0) ReturnInst(C, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001973 }
Devang Patel57ef4f42008-02-23 00:35:18 +00001974 virtual ~ReturnInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001975
Nick Lewycky67760642009-09-27 07:38:41 +00001976 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001977
Gabor Greifefe65362008-05-10 08:32:32 +00001978 /// Provide fast operand accessors
1979 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Devang Patel64d4e612008-02-26 17:56:20 +00001980
Gabor Greifefe65362008-05-10 08:32:32 +00001981 /// Convenience accessor
Devang Patel1eafa062008-03-11 17:35:03 +00001982 Value *getReturnValue(unsigned n = 0) const {
Gabor Greifefe65362008-05-10 08:32:32 +00001983 return n < getNumOperands()
1984 ? getOperand(n)
1985 : 0;
Devang Patel1eafa062008-03-11 17:35:03 +00001986 }
1987
Chris Lattner454928e2005-01-29 00:31:36 +00001988 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001989
1990 // Methods for support type inquiry through isa, cast, and dyn_cast:
1991 static inline bool classof(const ReturnInst *) { return true; }
1992 static inline bool classof(const Instruction *I) {
1993 return (I->getOpcode() == Instruction::Ret);
1994 }
1995 static inline bool classof(const Value *V) {
1996 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1997 }
Chris Lattner454928e2005-01-29 00:31:36 +00001998 private:
1999 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2000 virtual unsigned getNumSuccessorsV() const;
2001 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002002};
2003
Gabor Greifefe65362008-05-10 08:32:32 +00002004template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002005struct OperandTraits<ReturnInst> : public OptionalOperandTraits<> {
Gabor Greifefe65362008-05-10 08:32:32 +00002006};
2007
2008DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002009
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002010//===----------------------------------------------------------------------===//
2011// BranchInst Class
2012//===----------------------------------------------------------------------===//
2013
2014//===---------------------------------------------------------------------------
2015/// BranchInst - Conditional or Unconditional Branch instruction.
2016///
2017class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00002018 /// Ops list - Branches are strange. The operands are ordered:
Gabor Greifae5a20a2009-03-12 18:34:49 +00002019 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2020 /// they don't have to check for cond/uncond branchness. These are mostly
2021 /// accessed relative from op_end().
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002022 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00002023 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002024 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2025 // BranchInst(BB *B) - 'br B'
2026 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2027 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2028 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2029 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2030 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00002031 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002032 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002033 Instruction *InsertBefore = 0);
2034 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002035 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002036 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002037public:
2038 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
Gabor Greifae5a20a2009-03-12 18:34:49 +00002039 return new(1, true) BranchInst(IfTrue, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002040 }
Gabor Greifefe65362008-05-10 08:32:32 +00002041 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2042 Value *Cond, Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002043 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2044 }
2045 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
Gabor Greifae5a20a2009-03-12 18:34:49 +00002046 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002047 }
Gabor Greifefe65362008-05-10 08:32:32 +00002048 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2049 Value *Cond, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002050 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2051 }
Chris Lattner454928e2005-01-29 00:31:36 +00002052
Gabor Greifae5a20a2009-03-12 18:34:49 +00002053 ~BranchInst();
Gabor Greifefe65362008-05-10 08:32:32 +00002054
Chris Lattner454928e2005-01-29 00:31:36 +00002055 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00002056 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002057
Nick Lewycky67760642009-09-27 07:38:41 +00002058 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002059
Devang Patel4d4a5e02008-02-23 01:11:02 +00002060 bool isUnconditional() const { return getNumOperands() == 1; }
2061 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002062
Devang Patel4d4a5e02008-02-23 01:11:02 +00002063 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002064 assert(isConditional() && "Cannot get condition of an uncond branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002065 return Op<-3>();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002066 }
2067
2068 void setCondition(Value *V) {
2069 assert(isConditional() && "Cannot set condition of unconditional branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002070 Op<-3>() = V;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002071 }
2072
2073 // setUnconditionalDest - Change the current branch to an unconditional branch
2074 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +00002075 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002076 void setUnconditionalDest(BasicBlock *Dest) {
Gabor Greifae5a20a2009-03-12 18:34:49 +00002077 Op<-1>() = Dest;
Chris Lattner454928e2005-01-29 00:31:36 +00002078 if (isConditional()) { // Convert this to an uncond branch.
Gabor Greifae5a20a2009-03-12 18:34:49 +00002079 Op<-2>() = 0;
2080 Op<-3>() = 0;
Chris Lattner454928e2005-01-29 00:31:36 +00002081 NumOperands = 1;
Gabor Greifae5a20a2009-03-12 18:34:49 +00002082 OperandList = op_begin();
Chris Lattner454928e2005-01-29 00:31:36 +00002083 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002084 }
2085
Chris Lattner454928e2005-01-29 00:31:36 +00002086 unsigned getNumSuccessors() const { return 1+isConditional(); }
2087
2088 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002089 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002090 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002091 }
2092
Chris Lattner454928e2005-01-29 00:31:36 +00002093 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002094 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002095 *(&Op<-1>() - idx) = NewSucc;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002096 }
2097
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002098 // Methods for support type inquiry through isa, cast, and dyn_cast:
2099 static inline bool classof(const BranchInst *) { return true; }
2100 static inline bool classof(const Instruction *I) {
2101 return (I->getOpcode() == Instruction::Br);
2102 }
2103 static inline bool classof(const Value *V) {
2104 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2105 }
Chris Lattner454928e2005-01-29 00:31:36 +00002106private:
2107 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2108 virtual unsigned getNumSuccessorsV() const;
2109 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002110};
2111
Gabor Greifefe65362008-05-10 08:32:32 +00002112template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002113struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
Gabor Greifefe65362008-05-10 08:32:32 +00002114
2115DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2116
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002117//===----------------------------------------------------------------------===//
2118// SwitchInst Class
2119//===----------------------------------------------------------------------===//
2120
2121//===---------------------------------------------------------------------------
2122/// SwitchInst - Multiway switch
2123///
2124class SwitchInst : public TerminatorInst {
Gabor Greifefe65362008-05-10 08:32:32 +00002125 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00002126 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002127 // Operand[0] = Value to switch on
2128 // Operand[1] = Default basic block destination
2129 // Operand[2n ] = Value to match
2130 // Operand[2n+1] = BasicBlock to go to on match
2131 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00002132 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2133 void resizeOperands(unsigned No);
Gabor Greifefe65362008-05-10 08:32:32 +00002134 // allocate space for exactly zero operands
2135 void *operator new(size_t s) {
2136 return User::operator new(s, 0);
2137 }
Chris Lattner454928e2005-01-29 00:31:36 +00002138 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2139 /// switch on and a default destination. The number of additional cases can
2140 /// be specified here to make memory allocation more efficient. This
2141 /// constructor can also autoinsert before another instruction.
2142 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002143 Instruction *InsertBefore = 0);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002144
Chris Lattner454928e2005-01-29 00:31:36 +00002145 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2146 /// switch on and a default destination. The number of additional cases can
2147 /// be specified here to make memory allocation more efficient. This
2148 /// constructor also autoinserts at the end of the specified BasicBlock.
2149 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002150 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002151public:
Gabor Greifefe65362008-05-10 08:32:32 +00002152 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2153 unsigned NumCases, Instruction *InsertBefore = 0) {
2154 return new SwitchInst(Value, Default, NumCases, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002155 }
Gabor Greifefe65362008-05-10 08:32:32 +00002156 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2157 unsigned NumCases, BasicBlock *InsertAtEnd) {
2158 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002159 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002160 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00002161
Gabor Greifefe65362008-05-10 08:32:32 +00002162 /// Provide fast operand accessors
2163 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2164
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002165 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00002166 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00002167 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00002168
Devang Patel4d4a5e02008-02-23 01:11:02 +00002169 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002170 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002171 }
2172
2173 /// getNumCases - return the number of 'cases' in this switch instruction.
2174 /// Note that case #0 is always the default case.
2175 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002176 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002177 }
2178
2179 /// getCaseValue - Return the specified case value. Note that case #0, the
2180 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002181 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002182 assert(i && i < getNumCases() && "Illegal case value to get!");
2183 return getSuccessorValue(i);
2184 }
2185
2186 /// getCaseValue - Return the specified case value. Note that case #0, the
2187 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002188 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002189 assert(i && i < getNumCases() && "Illegal case value to get!");
2190 return getSuccessorValue(i);
2191 }
2192
2193 /// findCaseValue - Search all of the case values for the specified constant.
2194 /// If it is explicitly handled, return the case number of it, otherwise
2195 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002196 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002197 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2198 if (getCaseValue(i) == C)
2199 return i;
2200 return 0;
2201 }
2202
Nick Lewycky011f1842006-09-18 19:03:59 +00002203 /// findCaseDest - Finds the unique case value for a given successor. Returns
2204 /// null if the successor is not found, not unique, or is the default case.
2205 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00002206 if (BB == getDefaultDest()) return NULL;
2207
Nick Lewycky011f1842006-09-18 19:03:59 +00002208 ConstantInt *CI = NULL;
2209 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2210 if (getSuccessor(i) == BB) {
2211 if (CI) return NULL; // Multiple cases lead to BB.
2212 else CI = getCaseValue(i);
2213 }
2214 }
2215 return CI;
2216 }
2217
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002218 /// addCase - Add an entry to the switch instruction...
2219 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00002220 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002221
2222 /// removeCase - This method removes the specified successor from the switch
2223 /// instruction. Note that this cannot be used to remove the default
2224 /// destination (successor #0).
2225 ///
2226 void removeCase(unsigned idx);
2227
Nick Lewycky67760642009-09-27 07:38:41 +00002228 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002229
Chris Lattner454928e2005-01-29 00:31:36 +00002230 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2231 BasicBlock *getSuccessor(unsigned idx) const {
2232 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2233 return cast<BasicBlock>(getOperand(idx*2+1));
2234 }
2235 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002236 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Gabor Greifefe65362008-05-10 08:32:32 +00002237 setOperand(idx*2+1, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002238 }
2239
2240 // getSuccessorValue - Return the value associated with the specified
2241 // successor.
Devang Patel4d4a5e02008-02-23 01:11:02 +00002242 ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002243 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00002244 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002245 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002246
2247 // Methods for support type inquiry through isa, cast, and dyn_cast:
2248 static inline bool classof(const SwitchInst *) { return true; }
2249 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00002250 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002251 }
2252 static inline bool classof(const Value *V) {
2253 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2254 }
Chris Lattner454928e2005-01-29 00:31:36 +00002255private:
2256 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2257 virtual unsigned getNumSuccessorsV() const;
2258 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002259};
2260
Gabor Greifefe65362008-05-10 08:32:32 +00002261template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002262struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00002263};
2264
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002265DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002266
2267
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002268//===----------------------------------------------------------------------===//
2269// InvokeInst Class
2270//===----------------------------------------------------------------------===//
2271
Chris Lattner3340ffe2005-05-06 20:26:26 +00002272/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2273/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002274///
2275class InvokeInst : public TerminatorInst {
Devang Patel05988662008-09-25 21:00:45 +00002276 AttrListPtr AttributeList;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002277 InvokeInst(const InvokeInst &BI);
2278 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerd2dd1502007-02-13 01:04:01 +00002279 Value* const *Args, unsigned NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00002280
2281 template<typename InputIterator>
2282 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2283 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002284 const Twine &NameStr,
David Greenef1355a52007-08-27 19:04:21 +00002285 // This argument ensures that we have an iterator we can
2286 // do arithmetic on in constant time
2287 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002288 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002289
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002290 // This requires that the iterator points to contiguous memory.
2291 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
Evan Cheng1bf9a182008-07-24 00:08:56 +00002292 setName(NameStr);
David Greenef1355a52007-08-27 19:04:21 +00002293 }
2294
David Greenef1355a52007-08-27 19:04:21 +00002295 /// Construct an InvokeInst given a range of arguments.
2296 /// InputIterator must be a random-access iterator pointing to
2297 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2298 /// made for random-accessness but not for contiguous storage as
2299 /// that would incur runtime overhead.
2300 ///
2301 /// @brief Construct an InvokeInst from a range of arguments
2302 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002303 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2304 InputIterator ArgBegin, InputIterator ArgEnd,
2305 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002306 const Twine &NameStr, Instruction *InsertBefore);
David Greenef1355a52007-08-27 19:04:21 +00002307
2308 /// Construct an InvokeInst given a range of arguments.
2309 /// InputIterator must be a random-access iterator pointing to
2310 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2311 /// made for random-accessness but not for contiguous storage as
2312 /// that would incur runtime overhead.
2313 ///
2314 /// @brief Construct an InvokeInst from a range of arguments
2315 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002316 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2317 InputIterator ArgBegin, InputIterator ArgEnd,
2318 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002319 const Twine &NameStr, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002320public:
2321 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002322 static InvokeInst *Create(Value *Func,
2323 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002324 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002325 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00002326 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +00002327 unsigned Values(ArgEnd - ArgBegin + 3);
2328 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002329 Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002330 }
2331 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002332 static InvokeInst *Create(Value *Func,
2333 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002334 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002335 const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002336 BasicBlock *InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00002337 unsigned Values(ArgEnd - ArgBegin + 3);
2338 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002339 Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002340 }
David Greenef1355a52007-08-27 19:04:21 +00002341
Nick Lewycky67760642009-09-27 07:38:41 +00002342 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002343
Gabor Greifefe65362008-05-10 08:32:32 +00002344 /// Provide fast operand accessors
2345 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002346
Chris Lattner3340ffe2005-05-06 20:26:26 +00002347 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2348 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002349 CallingConv::ID getCallingConv() const {
2350 return static_cast<CallingConv::ID>(SubclassData);
2351 }
2352 void setCallingConv(CallingConv::ID CC) {
2353 SubclassData = static_cast<unsigned>(CC);
Chris Lattner3340ffe2005-05-06 20:26:26 +00002354 }
2355
Devang Patel05988662008-09-25 21:00:45 +00002356 /// getAttributes - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002357 ///
Devang Patel05988662008-09-25 21:00:45 +00002358 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002359
Devang Patel05988662008-09-25 21:00:45 +00002360 /// setAttributes - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002361 ///
Devang Patel05988662008-09-25 21:00:45 +00002362 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00002363
Devang Patel05988662008-09-25 21:00:45 +00002364 /// addAttribute - adds the attribute to the list of attributes.
2365 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002366
Devang Patel05988662008-09-25 21:00:45 +00002367 /// removeAttribute - removes the attribute from the list of attributes.
2368 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00002369
Dan Gohmanf2752502008-09-26 21:38:45 +00002370 /// @brief Determine whether the call or the callee has the given attribute.
2371 bool paramHasAttr(unsigned i, Attributes attr) const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002372
Dale Johannesen08e78b12008-02-22 17:49:45 +00002373 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002374 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00002375 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002376 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00002377
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002378 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002379 bool doesNotAccessMemory() const {
Dan Gohmana62b5ed2009-07-17 16:12:36 +00002380 return paramHasAttr(~0, Attribute::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002381 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002382 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002383 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2384 else removeAttribute(~0, Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00002385 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002386
2387 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002388 bool onlyReadsMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00002389 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002390 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002391 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002392 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2393 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00002394 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002395
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002396 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002397 bool doesNotReturn() const {
Devang Patel19c87462008-09-26 22:53:05 +00002398 return paramHasAttr(~0, Attribute::NoReturn);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002399 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002400 void setDoesNotReturn(bool DoesNotReturn = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002401 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2402 else removeAttribute(~0, Attribute::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00002403 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002404
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002405 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002406 bool doesNotThrow() const {
Devang Patel19c87462008-09-26 22:53:05 +00002407 return paramHasAttr(~0, Attribute::NoUnwind);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002408 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002409 void setDoesNotThrow(bool DoesNotThrow = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002410 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2411 else removeAttribute(~0, Attribute::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00002412 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002413
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002414 /// @brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00002415 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002416 bool hasStructRetAttr() const {
2417 // Be friendly and also check the callee.
Devang Patel05988662008-09-25 21:00:45 +00002418 return paramHasAttr(1, Attribute::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002419 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002420
Dan Gohmanf2752502008-09-26 21:38:45 +00002421 /// @brief Determine if any call argument is an aggregate passed by value.
2422 bool hasByValArgument() const {
2423 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2424 }
2425
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002426 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00002427 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002428 ///
Chris Lattner721aef62004-11-18 17:46:57 +00002429 Function *getCalledFunction() const {
Gabor Greif03a5f132009-09-03 02:02:59 +00002430 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00002431 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002432
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002433 /// getCalledValue - Get a pointer to the function that is invoked by this
Dan Gohmanf2752502008-09-26 21:38:45 +00002434 /// instruction
Gabor Greif03a5f132009-09-03 02:02:59 +00002435 const Value *getCalledValue() const { return getOperand(0); }
2436 Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002437
2438 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00002439 BasicBlock *getNormalDest() const {
Gabor Greif03a5f132009-09-03 02:02:59 +00002440 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002441 }
Chris Lattner454928e2005-01-29 00:31:36 +00002442 BasicBlock *getUnwindDest() const {
Gabor Greif03a5f132009-09-03 02:02:59 +00002443 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002444 }
Chris Lattner454928e2005-01-29 00:31:36 +00002445 void setNormalDest(BasicBlock *B) {
Gabor Greif03a5f132009-09-03 02:02:59 +00002446 setOperand(1, B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002447 }
Gabor Greif03a5f132009-09-03 02:02:59 +00002448
Chris Lattner454928e2005-01-29 00:31:36 +00002449 void setUnwindDest(BasicBlock *B) {
Gabor Greif03a5f132009-09-03 02:02:59 +00002450 setOperand(2, B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002451 }
2452
Devang Patel4d4a5e02008-02-23 01:11:02 +00002453 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002454 assert(i < 2 && "Successor # out of range for invoke!");
2455 return i == 0 ? getNormalDest() : getUnwindDest();
2456 }
2457
Chris Lattner454928e2005-01-29 00:31:36 +00002458 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002459 assert(idx < 2 && "Successor # out of range for invoke!");
Gabor Greif03a5f132009-09-03 02:02:59 +00002460 setOperand(idx+1, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002461 }
2462
Chris Lattner454928e2005-01-29 00:31:36 +00002463 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002464
2465 // Methods for support type inquiry through isa, cast, and dyn_cast:
2466 static inline bool classof(const InvokeInst *) { return true; }
2467 static inline bool classof(const Instruction *I) {
2468 return (I->getOpcode() == Instruction::Invoke);
2469 }
2470 static inline bool classof(const Value *V) {
2471 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2472 }
Chris Lattner454928e2005-01-29 00:31:36 +00002473private:
2474 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2475 virtual unsigned getNumSuccessorsV() const;
2476 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002477};
2478
Gabor Greifefe65362008-05-10 08:32:32 +00002479template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002480struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
Gabor Greifefe65362008-05-10 08:32:32 +00002481};
2482
2483template<typename InputIterator>
2484InvokeInst::InvokeInst(Value *Func,
2485 BasicBlock *IfNormal, BasicBlock *IfException,
2486 InputIterator ArgBegin, InputIterator ArgEnd,
2487 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002488 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00002489 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2490 ->getElementType())->getReturnType(),
2491 Instruction::Invoke,
2492 OperandTraits<InvokeInst>::op_end(this) - Values,
2493 Values, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002494 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00002495 typename std::iterator_traits<InputIterator>::iterator_category());
2496}
2497template<typename InputIterator>
2498InvokeInst::InvokeInst(Value *Func,
2499 BasicBlock *IfNormal, BasicBlock *IfException,
2500 InputIterator ArgBegin, InputIterator ArgEnd,
2501 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002502 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00002503 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2504 ->getElementType())->getReturnType(),
2505 Instruction::Invoke,
2506 OperandTraits<InvokeInst>::op_end(this) - Values,
2507 Values, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002508 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00002509 typename std::iterator_traits<InputIterator>::iterator_category());
2510}
2511
2512DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002513
2514//===----------------------------------------------------------------------===//
2515// UnwindInst Class
2516//===----------------------------------------------------------------------===//
2517
2518//===---------------------------------------------------------------------------
2519/// UnwindInst - Immediately exit the current function, unwinding the stack
2520/// until an invoke instruction is found.
2521///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002522class UnwindInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002523 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002524public:
Gabor Greif051a9502008-04-06 20:25:17 +00002525 // allocate space for exactly zero operands
2526 void *operator new(size_t s) {
2527 return User::operator new(s, 0);
2528 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002529 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2530 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002531
Nick Lewycky67760642009-09-27 07:38:41 +00002532 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002533
Chris Lattner454928e2005-01-29 00:31:36 +00002534 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002535
2536 // Methods for support type inquiry through isa, cast, and dyn_cast:
2537 static inline bool classof(const UnwindInst *) { return true; }
2538 static inline bool classof(const Instruction *I) {
2539 return I->getOpcode() == Instruction::Unwind;
2540 }
2541 static inline bool classof(const Value *V) {
2542 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2543 }
Chris Lattner454928e2005-01-29 00:31:36 +00002544private:
2545 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2546 virtual unsigned getNumSuccessorsV() const;
2547 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002548};
2549
Chris Lattner076b3f12004-10-16 18:05:54 +00002550//===----------------------------------------------------------------------===//
2551// UnreachableInst Class
2552//===----------------------------------------------------------------------===//
2553
2554//===---------------------------------------------------------------------------
2555/// UnreachableInst - This function has undefined behavior. In particular, the
2556/// presence of this instruction indicates some higher level knowledge that the
2557/// end of the block cannot be reached.
2558///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002559class UnreachableInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002560 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002561public:
Gabor Greif051a9502008-04-06 20:25:17 +00002562 // allocate space for exactly zero operands
2563 void *operator new(size_t s) {
2564 return User::operator new(s, 0);
2565 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002566 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2567 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00002568
Nick Lewycky67760642009-09-27 07:38:41 +00002569 virtual UnreachableInst *clone() const;
Chris Lattner076b3f12004-10-16 18:05:54 +00002570
Chris Lattner454928e2005-01-29 00:31:36 +00002571 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00002572
2573 // Methods for support type inquiry through isa, cast, and dyn_cast:
2574 static inline bool classof(const UnreachableInst *) { return true; }
2575 static inline bool classof(const Instruction *I) {
2576 return I->getOpcode() == Instruction::Unreachable;
2577 }
2578 static inline bool classof(const Value *V) {
2579 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2580 }
Chris Lattner454928e2005-01-29 00:31:36 +00002581private:
2582 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2583 virtual unsigned getNumSuccessorsV() const;
2584 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00002585};
2586
Reid Spencer3da59db2006-11-27 01:05:10 +00002587//===----------------------------------------------------------------------===//
2588// TruncInst Class
2589//===----------------------------------------------------------------------===//
2590
2591/// @brief This class represents a truncation of integer types.
2592class TruncInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002593public:
2594 /// @brief Constructor with insert-before-instruction semantics
2595 TruncInst(
2596 Value *S, ///< The value to be truncated
2597 const Type *Ty, ///< The (smaller) type to truncate to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002598 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002599 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2600 );
2601
2602 /// @brief Constructor with insert-at-end-of-block semantics
2603 TruncInst(
2604 Value *S, ///< The value to be truncated
2605 const Type *Ty, ///< The (smaller) type to truncate to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002606 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002607 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2608 );
2609
2610 /// @brief Clone an identical TruncInst
Nick Lewycky67760642009-09-27 07:38:41 +00002611 virtual TruncInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002612
2613 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2614 static inline bool classof(const TruncInst *) { return true; }
2615 static inline bool classof(const Instruction *I) {
2616 return I->getOpcode() == Trunc;
2617 }
2618 static inline bool classof(const Value *V) {
2619 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2620 }
2621};
2622
2623//===----------------------------------------------------------------------===//
2624// ZExtInst Class
2625//===----------------------------------------------------------------------===//
2626
2627/// @brief This class represents zero extension of integer types.
2628class ZExtInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002629public:
2630 /// @brief Constructor with insert-before-instruction semantics
2631 ZExtInst(
2632 Value *S, ///< The value to be zero extended
2633 const Type *Ty, ///< The type to zero extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002634 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002635 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2636 );
2637
2638 /// @brief Constructor with insert-at-end semantics.
2639 ZExtInst(
2640 Value *S, ///< The value to be zero extended
2641 const Type *Ty, ///< The type to zero extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002642 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002643 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2644 );
2645
2646 /// @brief Clone an identical ZExtInst
Nick Lewycky67760642009-09-27 07:38:41 +00002647 virtual ZExtInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002648
2649 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2650 static inline bool classof(const ZExtInst *) { return true; }
2651 static inline bool classof(const Instruction *I) {
2652 return I->getOpcode() == ZExt;
2653 }
2654 static inline bool classof(const Value *V) {
2655 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2656 }
2657};
2658
2659//===----------------------------------------------------------------------===//
2660// SExtInst Class
2661//===----------------------------------------------------------------------===//
2662
2663/// @brief This class represents a sign extension of integer types.
2664class SExtInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002665public:
2666 /// @brief Constructor with insert-before-instruction semantics
2667 SExtInst(
2668 Value *S, ///< The value to be sign extended
2669 const Type *Ty, ///< The type to sign extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002670 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002671 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2672 );
2673
2674 /// @brief Constructor with insert-at-end-of-block semantics
2675 SExtInst(
2676 Value *S, ///< The value to be sign extended
2677 const Type *Ty, ///< The type to sign extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002678 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002679 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2680 );
2681
2682 /// @brief Clone an identical SExtInst
Nick Lewycky67760642009-09-27 07:38:41 +00002683 virtual SExtInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002684
2685 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2686 static inline bool classof(const SExtInst *) { return true; }
2687 static inline bool classof(const Instruction *I) {
2688 return I->getOpcode() == SExt;
2689 }
2690 static inline bool classof(const Value *V) {
2691 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2692 }
2693};
2694
2695//===----------------------------------------------------------------------===//
2696// FPTruncInst Class
2697//===----------------------------------------------------------------------===//
2698
2699/// @brief This class represents a truncation of floating point types.
2700class FPTruncInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002701public:
2702 /// @brief Constructor with insert-before-instruction semantics
2703 FPTruncInst(
2704 Value *S, ///< The value to be truncated
2705 const Type *Ty, ///< The type to truncate to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002706 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002707 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2708 );
2709
2710 /// @brief Constructor with insert-before-instruction semantics
2711 FPTruncInst(
2712 Value *S, ///< The value to be truncated
2713 const Type *Ty, ///< The type to truncate to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002714 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002715 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2716 );
2717
2718 /// @brief Clone an identical FPTruncInst
Nick Lewycky67760642009-09-27 07:38:41 +00002719 virtual FPTruncInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002720
2721 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2722 static inline bool classof(const FPTruncInst *) { return true; }
2723 static inline bool classof(const Instruction *I) {
2724 return I->getOpcode() == FPTrunc;
2725 }
2726 static inline bool classof(const Value *V) {
2727 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2728 }
2729};
2730
2731//===----------------------------------------------------------------------===//
2732// FPExtInst Class
2733//===----------------------------------------------------------------------===//
2734
2735/// @brief This class represents an extension of floating point types.
2736class FPExtInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002737public:
2738 /// @brief Constructor with insert-before-instruction semantics
2739 FPExtInst(
2740 Value *S, ///< The value to be extended
2741 const Type *Ty, ///< The type to extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002742 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002743 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2744 );
2745
2746 /// @brief Constructor with insert-at-end-of-block semantics
2747 FPExtInst(
2748 Value *S, ///< The value to be extended
2749 const Type *Ty, ///< The type to extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002750 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002751 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2752 );
2753
2754 /// @brief Clone an identical FPExtInst
Nick Lewycky67760642009-09-27 07:38:41 +00002755 virtual FPExtInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002756
2757 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2758 static inline bool classof(const FPExtInst *) { return true; }
2759 static inline bool classof(const Instruction *I) {
2760 return I->getOpcode() == FPExt;
2761 }
2762 static inline bool classof(const Value *V) {
2763 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2764 }
2765};
2766
2767//===----------------------------------------------------------------------===//
2768// UIToFPInst Class
2769//===----------------------------------------------------------------------===//
2770
2771/// @brief This class represents a cast unsigned integer to floating point.
2772class UIToFPInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002773public:
2774 /// @brief Constructor with insert-before-instruction semantics
2775 UIToFPInst(
2776 Value *S, ///< The value to be converted
2777 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002778 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002779 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2780 );
2781
2782 /// @brief Constructor with insert-at-end-of-block semantics
2783 UIToFPInst(
2784 Value *S, ///< The value to be converted
2785 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002786 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002787 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2788 );
2789
2790 /// @brief Clone an identical UIToFPInst
Nick Lewycky67760642009-09-27 07:38:41 +00002791 virtual UIToFPInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002792
2793 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2794 static inline bool classof(const UIToFPInst *) { return true; }
2795 static inline bool classof(const Instruction *I) {
2796 return I->getOpcode() == UIToFP;
2797 }
2798 static inline bool classof(const Value *V) {
2799 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2800 }
2801};
2802
2803//===----------------------------------------------------------------------===//
2804// SIToFPInst Class
2805//===----------------------------------------------------------------------===//
2806
2807/// @brief This class represents a cast from signed integer to floating point.
2808class SIToFPInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002809public:
2810 /// @brief Constructor with insert-before-instruction semantics
2811 SIToFPInst(
2812 Value *S, ///< The value to be converted
2813 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002814 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002815 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2816 );
2817
2818 /// @brief Constructor with insert-at-end-of-block semantics
2819 SIToFPInst(
2820 Value *S, ///< The value to be converted
2821 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002822 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002823 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2824 );
2825
2826 /// @brief Clone an identical SIToFPInst
Nick Lewycky67760642009-09-27 07:38:41 +00002827 virtual SIToFPInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002828
2829 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2830 static inline bool classof(const SIToFPInst *) { return true; }
2831 static inline bool classof(const Instruction *I) {
2832 return I->getOpcode() == SIToFP;
2833 }
2834 static inline bool classof(const Value *V) {
2835 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2836 }
2837};
2838
2839//===----------------------------------------------------------------------===//
2840// FPToUIInst Class
2841//===----------------------------------------------------------------------===//
2842
2843/// @brief This class represents a cast from floating point to unsigned integer
2844class FPToUIInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002845public:
2846 /// @brief Constructor with insert-before-instruction semantics
2847 FPToUIInst(
2848 Value *S, ///< The value to be converted
2849 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002850 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002851 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2852 );
2853
2854 /// @brief Constructor with insert-at-end-of-block semantics
2855 FPToUIInst(
2856 Value *S, ///< The value to be converted
2857 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002858 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002859 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2860 );
2861
2862 /// @brief Clone an identical FPToUIInst
Nick Lewycky67760642009-09-27 07:38:41 +00002863 virtual FPToUIInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002864
2865 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2866 static inline bool classof(const FPToUIInst *) { return true; }
2867 static inline bool classof(const Instruction *I) {
2868 return I->getOpcode() == FPToUI;
2869 }
2870 static inline bool classof(const Value *V) {
2871 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2872 }
2873};
2874
2875//===----------------------------------------------------------------------===//
2876// FPToSIInst Class
2877//===----------------------------------------------------------------------===//
2878
2879/// @brief This class represents a cast from floating point to signed integer.
2880class FPToSIInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002881public:
2882 /// @brief Constructor with insert-before-instruction semantics
2883 FPToSIInst(
2884 Value *S, ///< The value to be converted
2885 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002886 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002887 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2888 );
2889
2890 /// @brief Constructor with insert-at-end-of-block semantics
2891 FPToSIInst(
2892 Value *S, ///< The value to be converted
2893 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002894 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002895 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2896 );
2897
2898 /// @brief Clone an identical FPToSIInst
Nick Lewycky67760642009-09-27 07:38:41 +00002899 virtual FPToSIInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002900
2901 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2902 static inline bool classof(const FPToSIInst *) { return true; }
2903 static inline bool classof(const Instruction *I) {
2904 return I->getOpcode() == FPToSI;
2905 }
2906 static inline bool classof(const Value *V) {
2907 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2908 }
2909};
2910
2911//===----------------------------------------------------------------------===//
2912// IntToPtrInst Class
2913//===----------------------------------------------------------------------===//
2914
2915/// @brief This class represents a cast from an integer to a pointer.
2916class IntToPtrInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002917public:
2918 /// @brief Constructor with insert-before-instruction semantics
2919 IntToPtrInst(
2920 Value *S, ///< The value to be converted
2921 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002922 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002923 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2924 );
2925
2926 /// @brief Constructor with insert-at-end-of-block semantics
2927 IntToPtrInst(
2928 Value *S, ///< The value to be converted
2929 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002930 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002931 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2932 );
2933
2934 /// @brief Clone an identical IntToPtrInst
Nick Lewycky67760642009-09-27 07:38:41 +00002935 virtual IntToPtrInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002936
2937 // Methods for support type inquiry through isa, cast, and dyn_cast:
2938 static inline bool classof(const IntToPtrInst *) { return true; }
2939 static inline bool classof(const Instruction *I) {
2940 return I->getOpcode() == IntToPtr;
2941 }
2942 static inline bool classof(const Value *V) {
2943 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2944 }
2945};
2946
2947//===----------------------------------------------------------------------===//
2948// PtrToIntInst Class
2949//===----------------------------------------------------------------------===//
2950
2951/// @brief This class represents a cast from a pointer to an integer
2952class PtrToIntInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002953public:
2954 /// @brief Constructor with insert-before-instruction semantics
2955 PtrToIntInst(
2956 Value *S, ///< The value to be converted
2957 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002958 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002959 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2960 );
2961
2962 /// @brief Constructor with insert-at-end-of-block semantics
2963 PtrToIntInst(
2964 Value *S, ///< The value to be converted
2965 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002966 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002967 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2968 );
2969
2970 /// @brief Clone an identical PtrToIntInst
Nick Lewycky67760642009-09-27 07:38:41 +00002971 virtual PtrToIntInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002972
2973 // Methods for support type inquiry through isa, cast, and dyn_cast:
2974 static inline bool classof(const PtrToIntInst *) { return true; }
2975 static inline bool classof(const Instruction *I) {
2976 return I->getOpcode() == PtrToInt;
2977 }
2978 static inline bool classof(const Value *V) {
2979 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2980 }
2981};
2982
2983//===----------------------------------------------------------------------===//
2984// BitCastInst Class
2985//===----------------------------------------------------------------------===//
2986
2987/// @brief This class represents a no-op cast from one type to another.
2988class BitCastInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002989public:
2990 /// @brief Constructor with insert-before-instruction semantics
2991 BitCastInst(
2992 Value *S, ///< The value to be casted
2993 const Type *Ty, ///< The type to casted to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002994 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002995 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2996 );
2997
2998 /// @brief Constructor with insert-at-end-of-block semantics
2999 BitCastInst(
3000 Value *S, ///< The value to be casted
3001 const Type *Ty, ///< The type to casted to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003002 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003003 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3004 );
3005
3006 /// @brief Clone an identical BitCastInst
Nick Lewycky67760642009-09-27 07:38:41 +00003007 virtual BitCastInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00003008
3009 // Methods for support type inquiry through isa, cast, and dyn_cast:
3010 static inline bool classof(const BitCastInst *) { return true; }
3011 static inline bool classof(const Instruction *I) {
3012 return I->getOpcode() == BitCast;
3013 }
3014 static inline bool classof(const Value *V) {
3015 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3016 }
3017};
3018
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003019} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00003020
3021#endif