blob: 17ff763c52bf4df68926214966237691494ea3bc [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"
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000022#include "llvm/CallingConv.h"
Dan Gohman81a0c0b2008-05-31 00:58:22 +000023#include "llvm/ADT/SmallVector.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000024#include <iterator>
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000025
26namespace llvm {
27
Chris Lattnerd1a32602005-02-24 05:32:09 +000028class ConstantInt;
Reid Spencer3da43842007-02-28 22:00:54 +000029class ConstantRange;
30class APInt;
Benjamin Kramer12ddd402009-08-11 17:45:13 +000031class LLVMContext;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000032
33//===----------------------------------------------------------------------===//
Victor Hernandez7b929da2009-10-23 21:09:37 +000034// AllocaInst Class
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000035//===----------------------------------------------------------------------===//
36
Victor Hernandez7b929da2009-10-23 21:09:37 +000037/// AllocaInst - an instruction to allocate memory on the stack
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000038///
Victor Hernandez7b929da2009-10-23 21:09:37 +000039class AllocaInst : public UnaryInstruction {
Devang Patel50b6e332009-10-27 22:16:29 +000040protected:
41 virtual AllocaInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000042public:
Victor Hernandez7b929da2009-10-23 21:09:37 +000043 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
44 const Twine &Name = "", Instruction *InsertBefore = 0);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +000045 AllocaInst(const Type *Ty, Value *ArraySize,
Victor Hernandez7b929da2009-10-23 21:09:37 +000046 const Twine &Name, BasicBlock *InsertAtEnd);
47
48 AllocaInst(const Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
49 AllocaInst(const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
50
51 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
52 const Twine &Name = "", Instruction *InsertBefore = 0);
53 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
54 const Twine &Name, BasicBlock *InsertAtEnd);
55
Gabor Greif051a9502008-04-06 20:25:17 +000056 // Out of line virtual method, so the vtable, etc. has a home.
Victor Hernandez7b929da2009-10-23 21:09:37 +000057 virtual ~AllocaInst();
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000058
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000059 /// isArrayAllocation - Return true if there is an allocation size parameter
60 /// to the allocation instruction that is not 1.
61 ///
62 bool isArrayAllocation() const;
63
Dan Gohman18476ee2009-07-07 20:47:48 +000064 /// getArraySize - Get the number of elements allocated. For a simple
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000065 /// allocation of a single element, this will return a constant 1 value.
66 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000067 const Value *getArraySize() const { return getOperand(0); }
68 Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000069
70 /// getType - Overload to return most specific pointer type
71 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000072 const PointerType *getType() const {
Misha Brukman9769ab22005-04-21 20:19:05 +000073 return reinterpret_cast<const PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000074 }
75
76 /// getAllocatedType - Return the type that is being allocated by the
77 /// instruction.
78 ///
79 const Type *getAllocatedType() const;
80
Nate Begeman14b05292005-11-05 09:21:28 +000081 /// getAlignment - Return the alignment of the memory that is being allocated
82 /// by the instruction.
83 ///
Chris Lattnercafe9bb2009-12-29 02:14:09 +000084 unsigned getAlignment() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +000085 return (1u << getSubclassDataFromInstruction()) >> 1;
Chris Lattnercafe9bb2009-12-29 02:14:09 +000086 }
Dan Gohman52837072008-03-24 16:55:58 +000087 void setAlignment(unsigned Align);
Chris Lattnerf56a8db2006-10-03 17:09:12 +000088
Chris Lattnerc5dd22a2008-11-26 02:54:17 +000089 /// isStaticAlloca - Return true if this alloca is in the entry block of the
90 /// function and is a constant size. If so, the code generator will fold it
91 /// into the prolog/epilog code, so it is basically free.
92 bool isStaticAlloca() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000093
94 // Methods for support type inquiry through isa, cast, and dyn_cast:
95 static inline bool classof(const AllocaInst *) { return true; }
96 static inline bool classof(const Instruction *I) {
97 return (I->getOpcode() == Instruction::Alloca);
98 }
99 static inline bool classof(const Value *V) {
100 return isa<Instruction>(V) && classof(cast<Instruction>(V));
101 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000102private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000103 // Shadow Instruction::setInstructionSubclassData with a private forwarding
104 // method so that subclasses cannot accidentally use it.
105 void setInstructionSubclassData(unsigned short D) {
106 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000107 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000108};
109
110
111//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000112// LoadInst Class
113//===----------------------------------------------------------------------===//
114
Chris Lattner88fe29a2005-02-05 01:44:18 +0000115/// LoadInst - an instruction for reading from memory. This uses the
116/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000117///
Chris Lattner454928e2005-01-29 00:31:36 +0000118class LoadInst : public UnaryInstruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000119 void AssertOK();
Devang Patel50b6e332009-10-27 22:16:29 +0000120protected:
121 virtual LoadInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000122public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000123 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
124 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000125 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
Christopher Lamb43c7f372007-04-22 19:24:39 +0000126 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000127 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000128 unsigned Align, Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000129 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000130 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000131 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000132 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000133
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000134 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
135 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
136 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
137 bool isVolatile = false, Instruction *InsertBefore = 0);
138 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
139 BasicBlock *InsertAtEnd);
140
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000141 /// isVolatile - Return true if this is a load from a volatile memory
142 /// location.
143 ///
Chris Lattnerb2406d92009-12-29 02:46:09 +0000144 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000145
146 /// setVolatile - Specify whether this is a volatile load or not.
147 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000148 void setVolatile(bool V) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000149 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
150 (V ? 1 : 0));
Christopher Lamb43c7f372007-04-22 19:24:39 +0000151 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000152
Christopher Lamb43c7f372007-04-22 19:24:39 +0000153 /// getAlignment - Return the alignment of the access that is being performed
154 ///
155 unsigned getAlignment() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000156 return (1 << (getSubclassDataFromInstruction() >> 1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000157 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000158
Christopher Lamb43c7f372007-04-22 19:24:39 +0000159 void setAlignment(unsigned Align);
160
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000161 Value *getPointerOperand() { return getOperand(0); }
162 const Value *getPointerOperand() const { return getOperand(0); }
163 static unsigned getPointerOperandIndex() { return 0U; }
164
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000165 unsigned getPointerAddressSpace() const {
166 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
167 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000168
169
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000170 // Methods for support type inquiry through isa, cast, and dyn_cast:
171 static inline bool classof(const LoadInst *) { return true; }
172 static inline bool classof(const Instruction *I) {
173 return I->getOpcode() == Instruction::Load;
174 }
175 static inline bool classof(const Value *V) {
176 return isa<Instruction>(V) && classof(cast<Instruction>(V));
177 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000178private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000179 // Shadow Instruction::setInstructionSubclassData with a private forwarding
180 // method so that subclasses cannot accidentally use it.
181 void setInstructionSubclassData(unsigned short D) {
182 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000183 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000184};
185
186
187//===----------------------------------------------------------------------===//
188// StoreInst Class
189//===----------------------------------------------------------------------===//
190
Misha Brukman9769ab22005-04-21 20:19:05 +0000191/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000192///
193class StoreInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +0000194 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +0000195 void AssertOK();
Devang Patel50b6e332009-10-27 22:16:29 +0000196protected:
197 virtual StoreInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000198public:
Gabor Greif051a9502008-04-06 20:25:17 +0000199 // allocate space for exactly two operands
200 void *operator new(size_t s) {
201 return User::operator new(s, 2);
202 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000203 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
204 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
205 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
206 Instruction *InsertBefore = 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000207 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
208 unsigned Align, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000209 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000210 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
211 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000212
213
214 /// isVolatile - Return true if this is a load from a volatile memory
215 /// location.
216 ///
Chris Lattnerb2406d92009-12-29 02:46:09 +0000217 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000218
219 /// setVolatile - Specify whether this is a volatile load or not.
220 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000221 void setVolatile(bool V) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000222 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
223 (V ? 1 : 0));
Christopher Lamb43c7f372007-04-22 19:24:39 +0000224 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000225
Chris Lattner454928e2005-01-29 00:31:36 +0000226 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +0000227 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner454928e2005-01-29 00:31:36 +0000228
Christopher Lamb43c7f372007-04-22 19:24:39 +0000229 /// getAlignment - Return the alignment of the access that is being performed
230 ///
231 unsigned getAlignment() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000232 return (1 << (getSubclassDataFromInstruction() >> 1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000233 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000234
Christopher Lamb43c7f372007-04-22 19:24:39 +0000235 void setAlignment(unsigned Align);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000236
Chris Lattner41c9c0e2010-06-26 23:26:37 +0000237 Value *getValueOperand() { return getOperand(0); }
238 const Value *getValueOperand() const { return getOperand(0); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000239
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000240 Value *getPointerOperand() { return getOperand(1); }
241 const Value *getPointerOperand() const { return getOperand(1); }
242 static unsigned getPointerOperandIndex() { return 1U; }
243
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000244 unsigned getPointerAddressSpace() const {
245 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
246 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000247
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000248 // Methods for support type inquiry through isa, cast, and dyn_cast:
249 static inline bool classof(const StoreInst *) { return true; }
250 static inline bool classof(const Instruction *I) {
251 return I->getOpcode() == Instruction::Store;
252 }
253 static inline bool classof(const Value *V) {
254 return isa<Instruction>(V) && classof(cast<Instruction>(V));
255 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000256private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000257 // Shadow Instruction::setInstructionSubclassData with a private forwarding
258 // method so that subclasses cannot accidentally use it.
259 void setInstructionSubclassData(unsigned short D) {
260 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000261 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000262};
263
Gabor Greifefe65362008-05-10 08:32:32 +0000264template <>
Jay Foad67c619b2011-01-11 15:07:38 +0000265struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
Gabor Greifefe65362008-05-10 08:32:32 +0000266};
267
268DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000269
270//===----------------------------------------------------------------------===//
271// GetElementPtrInst Class
272//===----------------------------------------------------------------------===//
273
David Greeneb8f74792007-09-04 15:46:09 +0000274// checkType - Simple wrapper function to give a better assertion failure
275// message on bad indexes for a gep instruction.
276//
277static inline const Type *checkType(const Type *Ty) {
278 assert(Ty && "Invalid GetElementPtrInst indices for type!");
279 return Ty;
280}
281
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000282/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
283/// access elements of arrays and structs
284///
285class GetElementPtrInst : public Instruction {
Gabor Greifefe65362008-05-10 08:32:32 +0000286 GetElementPtrInst(const GetElementPtrInst &GEPI);
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +0000287 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000288 const Twine &NameStr);
289 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000290
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000291 template<typename RandomAccessIterator>
292 void init(Value *Ptr,
293 RandomAccessIterator IdxBegin,
294 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000295 const Twine &NameStr,
David Greeneb8f74792007-09-04 15:46:09 +0000296 // This argument ensures that we have an iterator we can
297 // do arithmetic on in constant time
298 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000299 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000300
David Greeneb8f74792007-09-04 15:46:09 +0000301 if (NumIdx > 0) {
Gabor Greifefe65362008-05-10 08:32:32 +0000302 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +0000303 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Gabor Greifefe65362008-05-10 08:32:32 +0000304 // we have to build an array here
David Greeneb8f74792007-09-04 15:46:09 +0000305 }
306 else {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000307 init(Ptr, 0, NumIdx, NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000308 }
David Greeneb8f74792007-09-04 15:46:09 +0000309 }
310
311 /// getIndexedType - Returns the type of the element that would be loaded with
312 /// a load instruction with the specified parameters.
313 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000314 /// Null is returned if the indices are invalid for the specified
David Greeneb8f74792007-09-04 15:46:09 +0000315 /// pointer type.
316 ///
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000317 template<typename RandomAccessIterator>
David Greeneb8f74792007-09-04 15:46:09 +0000318 static const Type *getIndexedType(const Type *Ptr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000319 RandomAccessIterator IdxBegin,
320 RandomAccessIterator IdxEnd,
David Greeneb8f74792007-09-04 15:46:09 +0000321 // This argument ensures that we
322 // have an iterator we can do
323 // arithmetic on in constant time
324 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000325 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
David Greeneb8f74792007-09-04 15:46:09 +0000326
Dan Gohman041e2eb2008-05-15 19:50:34 +0000327 if (NumIdx > 0)
David Greeneb8f74792007-09-04 15:46:09 +0000328 // This requires that the iterator points to contiguous memory.
David Greene2d5a0b92008-10-29 00:30:54 +0000329 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +0000330 else
331 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
David Greeneb8f74792007-09-04 15:46:09 +0000332 }
333
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000334 /// Constructors - Create a getelementptr instruction with a base pointer an
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000335 /// list of indices. The first ctor can optionally insert before an existing
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000336 /// instruction, the second appends the new instruction to the specified
337 /// BasicBlock.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000338 template<typename RandomAccessIterator>
339 inline GetElementPtrInst(Value *Ptr, RandomAccessIterator IdxBegin,
340 RandomAccessIterator IdxEnd,
Gabor Greifefe65362008-05-10 08:32:32 +0000341 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000342 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000343 Instruction *InsertBefore);
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000344 template<typename RandomAccessIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000345 inline GetElementPtrInst(Value *Ptr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000346 RandomAccessIterator IdxBegin,
347 RandomAccessIterator IdxEnd,
Gabor Greifefe65362008-05-10 08:32:32 +0000348 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000349 const Twine &NameStr, BasicBlock *InsertAtEnd);
David Greeneb8f74792007-09-04 15:46:09 +0000350
Chris Lattner38bacf22005-05-03 05:43:30 +0000351 /// Constructors - These two constructors are convenience methods because one
352 /// and two index getelementptr instructions are so common.
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000353 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +0000354 Instruction *InsertBefore = 0);
Chris Lattner38bacf22005-05-03 05:43:30 +0000355 GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000356 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +0000357protected:
358 virtual GetElementPtrInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +0000359public:
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000360 template<typename RandomAccessIterator>
361 static GetElementPtrInst *Create(Value *Ptr, RandomAccessIterator IdxBegin,
362 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000363 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000364 Instruction *InsertBefore = 0) {
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000365 typename std::iterator_traits<RandomAccessIterator>::difference_type
366 Values = 1 + std::distance(IdxBegin, IdxEnd);
Gabor Greifefe65362008-05-10 08:32:32 +0000367 return new(Values)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000368 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000369 }
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000370 template<typename RandomAccessIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000371 static GetElementPtrInst *Create(Value *Ptr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000372 RandomAccessIterator IdxBegin,
373 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000374 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000375 BasicBlock *InsertAtEnd) {
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000376 typename std::iterator_traits<RandomAccessIterator>::difference_type
377 Values = 1 + std::distance(IdxBegin, IdxEnd);
Gabor Greifefe65362008-05-10 08:32:32 +0000378 return new(Values)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000379 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000380 }
381
Gabor Greifefe65362008-05-10 08:32:32 +0000382 /// Constructors - These two creators are convenience methods because one
383 /// index getelementptr instructions are so common.
Gabor Greif051a9502008-04-06 20:25:17 +0000384 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000385 const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +0000386 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000387 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000388 }
389 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000390 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000391 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000392 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000393 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000394
Dan Gohmane2574d32009-08-11 17:57:01 +0000395 /// Create an "inbounds" getelementptr. See the documentation for the
396 /// "inbounds" flag in LangRef.html for details.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000397 template<typename RandomAccessIterator>
398 static GetElementPtrInst *CreateInBounds(Value *Ptr,
399 RandomAccessIterator IdxBegin,
400 RandomAccessIterator IdxEnd,
Dan Gohmane2574d32009-08-11 17:57:01 +0000401 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 }
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000408 template<typename RandomAccessIterator>
Dan Gohmane2574d32009-08-11 17:57:01 +0000409 static GetElementPtrInst *CreateInBounds(Value *Ptr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000410 RandomAccessIterator IdxBegin,
411 RandomAccessIterator IdxEnd,
Dan Gohmane2574d32009-08-11 17:57:01 +0000412 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
Gabor Greifefe65362008-05-10 08:32:32 +0000434 /// Transparently provide more efficient getOperand methods.
435 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
436
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000437 // getType - Overload to return most specific pointer type...
Devang Patel4d4a5e02008-02-23 01:11:02 +0000438 const PointerType *getType() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000439 return reinterpret_cast<const PointerType*>(Instruction::getType());
440 }
441
442 /// getIndexedType - Returns the type of the element that would be loaded with
443 /// a load instruction with the specified parameters.
444 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000445 /// Null is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000446 /// pointer type.
447 ///
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000448 template<typename RandomAccessIterator>
Misha Brukman9769ab22005-04-21 20:19:05 +0000449 static const Type *getIndexedType(const Type *Ptr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000450 RandomAccessIterator IdxBegin,
451 RandomAccessIterator IdxEnd) {
Dan Gohman041e2eb2008-05-15 19:50:34 +0000452 return getIndexedType(Ptr, IdxBegin, IdxEnd,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000453 typename std::iterator_traits<RandomAccessIterator>::
Dan Gohman041e2eb2008-05-15 19:50:34 +0000454 iterator_category());
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000455 }
Matthijs Kooijmane2afded2008-07-29 08:46:11 +0000456
457 static const Type *getIndexedType(const Type *Ptr,
458 Value* const *Idx, unsigned NumIdx);
459
460 static const Type *getIndexedType(const Type *Ptr,
Jay Foad25052d82011-01-14 08:07:43 +0000461 Constant* const *Idx, unsigned NumIdx);
462
463 static const Type *getIndexedType(const Type *Ptr,
Matthijs Kooijmane2afded2008-07-29 08:46:11 +0000464 uint64_t const *Idx, unsigned NumIdx);
465
Chris Lattner38bacf22005-05-03 05:43:30 +0000466 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
Misha Brukman9769ab22005-04-21 20:19:05 +0000467
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000468 inline op_iterator idx_begin() { return op_begin()+1; }
469 inline const_op_iterator idx_begin() const { return op_begin()+1; }
470 inline op_iterator idx_end() { return op_end(); }
471 inline const_op_iterator idx_end() const { return op_end(); }
472
473 Value *getPointerOperand() {
474 return getOperand(0);
475 }
476 const Value *getPointerOperand() const {
477 return getOperand(0);
478 }
479 static unsigned getPointerOperandIndex() {
480 return 0U; // get index for modifying correct operand
481 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000482
Chris Lattner8a67ac52009-08-30 20:06:40 +0000483 unsigned getPointerAddressSpace() const {
484 return cast<PointerType>(getType())->getAddressSpace();
485 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000486
Chris Lattner3bc6ced2009-01-09 05:27:40 +0000487 /// getPointerOperandType - Method to return the pointer operand as a
488 /// PointerType.
489 const PointerType *getPointerOperandType() const {
490 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
491 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000492
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000493
Devang Patel4d4a5e02008-02-23 01:11:02 +0000494 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000495 return getNumOperands() - 1;
496 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000497
Devang Patel4d4a5e02008-02-23 01:11:02 +0000498 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000499 return getNumOperands() > 1;
500 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000501
Chris Lattner6f771d42007-04-14 00:12:57 +0000502 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
503 /// zeros. If so, the result pointer and the first operand have the same
504 /// value, just potentially different types.
505 bool hasAllZeroIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000506
Chris Lattner6b0974c2007-04-27 20:35:56 +0000507 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
508 /// constant integers. If so, the result pointer and the first operand have
509 /// a constant offset between them.
510 bool hasAllConstantIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000511
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000512 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
513 /// See LangRef.html for the meaning of inbounds on a getelementptr.
Nick Lewyckyae05e7d2009-09-27 21:33:04 +0000514 void setIsInBounds(bool b = true);
515
516 /// isInBounds - Determine whether the GEP has the inbounds flag.
517 bool isInBounds() const;
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000518
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000519 // Methods for support type inquiry through isa, cast, and dyn_cast:
520 static inline bool classof(const GetElementPtrInst *) { return true; }
521 static inline bool classof(const Instruction *I) {
522 return (I->getOpcode() == Instruction::GetElementPtr);
523 }
524 static inline bool classof(const Value *V) {
525 return isa<Instruction>(V) && classof(cast<Instruction>(V));
526 }
527};
528
Gabor Greifefe65362008-05-10 08:32:32 +0000529template <>
Jay Foad67c619b2011-01-11 15:07:38 +0000530struct OperandTraits<GetElementPtrInst> :
531 public VariadicOperandTraits<GetElementPtrInst, 1> {
Gabor Greifefe65362008-05-10 08:32:32 +0000532};
533
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000534template<typename RandomAccessIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000535GetElementPtrInst::GetElementPtrInst(Value *Ptr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000536 RandomAccessIterator IdxBegin,
537 RandomAccessIterator IdxEnd,
Gabor Greifefe65362008-05-10 08:32:32 +0000538 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000539 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000540 Instruction *InsertBefore)
541 : Instruction(PointerType::get(checkType(
542 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000543 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000544 cast<PointerType>(Ptr->getType())
545 ->getAddressSpace()),
546 GetElementPtr,
547 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
548 Values, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000549 init(Ptr, IdxBegin, IdxEnd, NameStr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000550 typename std::iterator_traits<RandomAccessIterator>
551 ::iterator_category());
Gabor Greifefe65362008-05-10 08:32:32 +0000552}
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000553template<typename RandomAccessIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000554GetElementPtrInst::GetElementPtrInst(Value *Ptr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000555 RandomAccessIterator IdxBegin,
556 RandomAccessIterator IdxEnd,
Gabor Greifefe65362008-05-10 08:32:32 +0000557 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000558 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000559 BasicBlock *InsertAtEnd)
560 : Instruction(PointerType::get(checkType(
561 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000562 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000563 cast<PointerType>(Ptr->getType())
564 ->getAddressSpace()),
565 GetElementPtr,
566 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
567 Values, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000568 init(Ptr, IdxBegin, IdxEnd, NameStr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000569 typename std::iterator_traits<RandomAccessIterator>
570 ::iterator_category());
Gabor Greifefe65362008-05-10 08:32:32 +0000571}
572
573
574DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
575
576
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000577//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000578// ICmpInst Class
579//===----------------------------------------------------------------------===//
580
581/// This instruction compares its operands according to the predicate given
Nate Begemanac80ade2008-05-12 19:01:56 +0000582/// to the constructor. It only operates on integers or pointers. The operands
583/// must be identical types.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000584/// @brief Represent an integer comparison operator.
585class ICmpInst: public CmpInst {
Devang Patel50b6e332009-10-27 22:16:29 +0000586protected:
587 /// @brief Clone an indentical ICmpInst
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000588 virtual ICmpInst *clone_impl() const;
Reid Spencer45fb3f32006-11-20 01:22:35 +0000589public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000590 /// @brief Constructor with insert-before-instruction semantics.
591 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000592 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +0000593 Predicate pred, ///< The predicate to use for the comparison
594 Value *LHS, ///< The left-hand-side of the expression
595 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000596 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000597 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +0000598 Instruction::ICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000599 InsertBefore) {
600 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
601 pred <= CmpInst::LAST_ICMP_PREDICATE &&
602 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000603 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000604 "Both operands to ICmp instruction are not of the same type!");
605 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000606 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000607 getOperand(0)->getType()->isPointerTy()) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000608 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000609 }
610
Owen Anderson333c4002009-07-09 23:48:35 +0000611 /// @brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000612 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000613 BasicBlock &InsertAtEnd, ///< Block to insert into.
614 Predicate pred, ///< The predicate to use for the comparison
615 Value *LHS, ///< The left-hand-side of the expression
616 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000617 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000618 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000619 Instruction::ICmp, pred, LHS, RHS, NameStr,
620 &InsertAtEnd) {
621 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
622 pred <= CmpInst::LAST_ICMP_PREDICATE &&
623 "Invalid ICmp predicate value");
624 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
625 "Both operands to ICmp instruction are not of the same type!");
626 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000627 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000628 getOperand(0)->getType()->isPointerTy()) &&
Owen Anderson333c4002009-07-09 23:48:35 +0000629 "Invalid operand types for ICmp instruction");
630 }
631
632 /// @brief Constructor with no-insertion semantics
633 ICmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +0000634 Predicate pred, ///< The predicate to use for the comparison
635 Value *LHS, ///< The left-hand-side of the expression
636 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000637 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000638 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000639 Instruction::ICmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +0000640 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
641 pred <= CmpInst::LAST_ICMP_PREDICATE &&
642 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000643 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000644 "Both operands to ICmp instruction are not of the same type!");
645 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000646 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000647 getOperand(0)->getType()->isPointerTy()) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000648 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000649 }
650
Reid Spencere4d87aa2006-12-23 06:05:41 +0000651 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
652 /// @returns the predicate that would be the result if the operand were
653 /// regarded as signed.
654 /// @brief Return the signed version of the predicate
655 Predicate getSignedPredicate() const {
656 return getSignedPredicate(getPredicate());
657 }
658
659 /// This is a static version that you can use without an instruction.
660 /// @brief Return the signed version of the predicate.
661 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000662
Nick Lewycky4189a532008-01-28 03:48:02 +0000663 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
664 /// @returns the predicate that would be the result if the operand were
665 /// regarded as unsigned.
666 /// @brief Return the unsigned version of the predicate
667 Predicate getUnsignedPredicate() const {
668 return getUnsignedPredicate(getPredicate());
669 }
670
671 /// This is a static version that you can use without an instruction.
672 /// @brief Return the unsigned version of the predicate.
673 static Predicate getUnsignedPredicate(Predicate pred);
674
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000675 /// isEquality - Return true if this predicate is either EQ or NE. This also
676 /// tests for commutativity.
677 static bool isEquality(Predicate P) {
678 return P == ICMP_EQ || P == ICMP_NE;
679 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000680
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000681 /// isEquality - Return true if this predicate is either EQ or NE. This also
682 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000683 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000684 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000685 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000686
687 /// @returns true if the predicate of this ICmpInst is commutative
688 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000689 bool isCommutative() const { return isEquality(); }
690
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000691 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000692 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +0000693 bool isRelational() const {
694 return !isEquality();
695 }
696
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000697 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000698 ///
699 static bool isRelational(Predicate P) {
700 return !isEquality(P);
701 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000702
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000703 /// Initialize a set of values that all satisfy the predicate with C.
Reid Spencer3da43842007-02-28 22:00:54 +0000704 /// @brief Make a ConstantRange for a relation with a constant value.
705 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
706
Reid Spencer45fb3f32006-11-20 01:22:35 +0000707 /// Exchange the two operands to this instruction in such a way that it does
708 /// not modify the semantics of the instruction. The predicate value may be
709 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000710 /// (e.g. ult).
Reid Spencer45fb3f32006-11-20 01:22:35 +0000711 /// @brief Swap operands and adjust predicate.
712 void swapOperands() {
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000713 setPredicate(getSwappedPredicate());
Gabor Greif94fb68b2008-05-13 22:51:52 +0000714 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000715 }
716
717 // Methods for support type inquiry through isa, cast, and dyn_cast:
718 static inline bool classof(const ICmpInst *) { return true; }
719 static inline bool classof(const Instruction *I) {
720 return I->getOpcode() == Instruction::ICmp;
721 }
722 static inline bool classof(const Value *V) {
723 return isa<Instruction>(V) && classof(cast<Instruction>(V));
724 }
Dan Gohmanf72fb672008-09-09 01:02:47 +0000725
Reid Spencer45fb3f32006-11-20 01:22:35 +0000726};
727
728//===----------------------------------------------------------------------===//
729// FCmpInst Class
730//===----------------------------------------------------------------------===//
731
732/// This instruction compares its operands according to the predicate given
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000733/// to the constructor. It only operates on floating point values or packed
Reid Spencer45fb3f32006-11-20 01:22:35 +0000734/// vectors of floating point values. The operands must be identical types.
735/// @brief Represents a floating point comparison operator.
736class FCmpInst: public CmpInst {
Devang Patel50b6e332009-10-27 22:16:29 +0000737protected:
738 /// @brief Clone an indentical FCmpInst
739 virtual FCmpInst *clone_impl() const;
Reid Spencer45fb3f32006-11-20 01:22:35 +0000740public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000741 /// @brief Constructor with insert-before-instruction semantics.
742 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000743 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +0000744 Predicate pred, ///< The predicate to use for the comparison
745 Value *LHS, ///< The left-hand-side of the expression
746 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000747 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000748 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +0000749 Instruction::FCmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000750 InsertBefore) {
751 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
752 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000753 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000754 "Both operands to FCmp instruction are not of the same type!");
755 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000756 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000757 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000758 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000759
Owen Anderson333c4002009-07-09 23:48:35 +0000760 /// @brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000761 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000762 BasicBlock &InsertAtEnd, ///< Block to insert into.
763 Predicate pred, ///< The predicate to use for the comparison
764 Value *LHS, ///< The left-hand-side of the expression
765 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000766 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000767 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000768 Instruction::FCmp, pred, LHS, RHS, NameStr,
769 &InsertAtEnd) {
770 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
771 "Invalid FCmp predicate value");
772 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
773 "Both operands to FCmp instruction are not of the same type!");
774 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000775 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Owen Anderson333c4002009-07-09 23:48:35 +0000776 "Invalid operand types for FCmp instruction");
777 }
778
779 /// @brief Constructor with no-insertion semantics
780 FCmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +0000781 Predicate pred, ///< The predicate to use for the comparison
782 Value *LHS, ///< The left-hand-side of the expression
783 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000784 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000785 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000786 Instruction::FCmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +0000787 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
788 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000789 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000790 "Both operands to FCmp instruction are not of the same type!");
791 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000792 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000793 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000794 }
795
Reid Spencer45fb3f32006-11-20 01:22:35 +0000796 /// @returns true if the predicate of this instruction is EQ or NE.
797 /// @brief Determine if this is an equality predicate.
798 bool isEquality() const {
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000799 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
800 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
Reid Spencer45fb3f32006-11-20 01:22:35 +0000801 }
Dan Gohman793df072008-09-16 16:44:00 +0000802
803 /// @returns true if the predicate of this instruction is commutative.
804 /// @brief Determine if this is a commutative predicate.
805 bool isCommutative() const {
806 return isEquality() ||
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000807 getPredicate() == FCMP_FALSE ||
808 getPredicate() == FCMP_TRUE ||
809 getPredicate() == FCMP_ORD ||
810 getPredicate() == FCMP_UNO;
Dan Gohman793df072008-09-16 16:44:00 +0000811 }
Reid Spencer45fb3f32006-11-20 01:22:35 +0000812
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000813 /// @returns true if the predicate is relational (not EQ or NE).
Reid Spencer45fb3f32006-11-20 01:22:35 +0000814 /// @brief Determine if this a relational predicate.
815 bool isRelational() const { return !isEquality(); }
816
817 /// Exchange the two operands to this instruction in such a way that it does
818 /// not modify the semantics of the instruction. The predicate value may be
819 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000820 /// (e.g. ult).
Reid Spencer45fb3f32006-11-20 01:22:35 +0000821 /// @brief Swap operands and adjust predicate.
822 void swapOperands() {
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000823 setPredicate(getSwappedPredicate());
Gabor Greif94fb68b2008-05-13 22:51:52 +0000824 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000825 }
826
827 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
828 static inline bool classof(const FCmpInst *) { return true; }
829 static inline bool classof(const Instruction *I) {
830 return I->getOpcode() == Instruction::FCmp;
831 }
832 static inline bool classof(const Value *V) {
833 return isa<Instruction>(V) && classof(cast<Instruction>(V));
834 }
835};
836
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000837//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000838/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +0000839/// machine's calling convention. This class uses low bit of the SubClassData
840/// field to indicate whether or not this is a tail call. The rest of the bits
841/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000842///
843class CallInst : public Instruction {
Devang Patel05988662008-09-25 21:00:45 +0000844 AttrListPtr AttributeList; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000845 CallInst(const CallInst &CI);
Chris Lattnerd54f4322007-02-13 00:58:44 +0000846 void init(Value *Func, Value* const *Params, unsigned NumParams);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000847 void init(Value *Func, Value *Actual1, Value *Actual2);
848 void init(Value *Func, Value *Actual);
849 void init(Value *Func);
850
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000851 template<typename RandomAccessIterator>
852 void init(Value *Func,
853 RandomAccessIterator ArgBegin,
854 RandomAccessIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000855 const Twine &NameStr,
David Greene52eec542007-08-01 03:43:44 +0000856 // This argument ensures that we have an iterator we can
857 // do arithmetic on in constant time
858 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000859 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000860
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000861 // This requires that the iterator points to contiguous memory.
862 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
Evan Cheng1bf9a182008-07-24 00:08:56 +0000863 setName(NameStr);
David Greene52eec542007-08-01 03:43:44 +0000864 }
865
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000866 /// Construct a CallInst given a range of arguments. RandomAccessIterator
David Greene52eec542007-08-01 03:43:44 +0000867 /// must be a random-access iterator pointing to contiguous storage
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000868 /// (e.g. a std::vector<>::iterator). Checks are made for
David Greene52eec542007-08-01 03:43:44 +0000869 /// random-accessness but not for contiguous storage as that would
870 /// incur runtime overhead.
871 /// @brief Construct a CallInst from a range of arguments
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000872 template<typename RandomAccessIterator>
873 CallInst(Value *Func,
874 RandomAccessIterator ArgBegin, RandomAccessIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000875 const Twine &NameStr, Instruction *InsertBefore);
David Greene52eec542007-08-01 03:43:44 +0000876
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000877 /// Construct a CallInst given a range of arguments. RandomAccessIterator
David Greene52eec542007-08-01 03:43:44 +0000878 /// must be a random-access iterator pointing to contiguous storage
879 /// (e.g. a std::vector<>::iterator). Checks are made for
880 /// random-accessness but not for contiguous storage as that would
881 /// incur runtime overhead.
882 /// @brief Construct a CallInst from a range of arguments
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000883 template<typename RandomAccessIterator>
884 inline CallInst(Value *Func,
885 RandomAccessIterator ArgBegin, RandomAccessIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000886 const Twine &NameStr, BasicBlock *InsertAtEnd);
David Greene52eec542007-08-01 03:43:44 +0000887
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000888 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000889 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000890 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000891 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000892 explicit CallInst(Value *F, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000893 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000894 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +0000895protected:
896 virtual CallInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +0000897public:
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000898 template<typename RandomAccessIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000899 static CallInst *Create(Value *Func,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000900 RandomAccessIterator ArgBegin,
901 RandomAccessIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000902 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +0000903 Instruction *InsertBefore = 0) {
Gabor Greifdc4f3912010-07-07 09:29:07 +0000904 return new(unsigned(ArgEnd - ArgBegin + 1))
Evan Cheng1bf9a182008-07-24 00:08:56 +0000905 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000906 }
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000907 template<typename RandomAccessIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000908 static CallInst *Create(Value *Func,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000909 RandomAccessIterator ArgBegin,
910 RandomAccessIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000911 const Twine &NameStr, BasicBlock *InsertAtEnd) {
Gabor Greifdc4f3912010-07-07 09:29:07 +0000912 return new(unsigned(ArgEnd - ArgBegin + 1))
Evan Cheng1bf9a182008-07-24 00:08:56 +0000913 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000914 }
Evan Cheng1bf9a182008-07-24 00:08:56 +0000915 static CallInst *Create(Value *F, Value *Actual,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000916 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000917 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000918 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000919 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000920 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
Gabor Greif051a9502008-04-06 20:25:17 +0000921 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000922 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000923 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000924 static CallInst *Create(Value *F, const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000925 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000926 return new(1) CallInst(F, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000927 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000928 static CallInst *Create(Value *F, const Twine &NameStr,
Evan Cheng34cd4a42008-05-05 18:30:58 +0000929 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000930 return new(1) CallInst(F, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000931 }
Evan Chengfabcb912009-09-10 04:36:43 +0000932 /// CreateMalloc - Generate the IR for a call to malloc:
933 /// 1. Compute the malloc call's argument as the specified type's size,
934 /// possibly multiplied by the array size if the array size is not
935 /// constant 1.
936 /// 2. Call malloc with that argument.
937 /// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewycky3fc35c52009-10-17 23:52:26 +0000938 static Instruction *CreateMalloc(Instruction *InsertBefore,
939 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000940 Value *AllocSize, Value *ArraySize = 0,
Chris Lattner5a30a852010-07-12 00:57:28 +0000941 Function* MallocF = 0,
Nick Lewycky3fc35c52009-10-17 23:52:26 +0000942 const Twine &Name = "");
943 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
944 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000945 Value *AllocSize, Value *ArraySize = 0,
946 Function* MallocF = 0,
Nick Lewycky3fc35c52009-10-17 23:52:26 +0000947 const Twine &Name = "");
Victor Hernandez66284e02009-10-24 04:23:03 +0000948 /// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner5a30a852010-07-12 00:57:28 +0000949 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
Victor Hernandez66284e02009-10-24 04:23:03 +0000950 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000951
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000952 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000953
Chris Lattnerb2406d92009-12-29 02:46:09 +0000954 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
Evan Cheng1bf9a182008-07-24 00:08:56 +0000955 void setTailCall(bool isTC = true) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000956 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
957 unsigned(isTC));
Chris Lattner3340ffe2005-05-06 20:26:26 +0000958 }
959
Dan Gohmanf2752502008-09-26 21:38:45 +0000960 /// Provide fast operand accessors
961 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000962
Gabor Greif0114b992010-07-31 08:35:21 +0000963 /// getNumArgOperands - Return the number of call arguments.
964 ///
Bill Wendling22a5b292010-06-07 19:05:06 +0000965 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
Gabor Greif0114b992010-07-31 08:35:21 +0000966
967 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
968 ///
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000969 Value *getArgOperand(unsigned i) const { return getOperand(i); }
970 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
Bill Wendling22a5b292010-06-07 19:05:06 +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 {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000975 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000976 }
977 void setCallingConv(CallingConv::ID CC) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000978 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
979 (static_cast<unsigned>(CC) << 1));
Chris Lattner3340ffe2005-05-06 20:26:26 +0000980 }
Chris Lattnerddb6db42005-05-06 05:51:46 +0000981
Devang Patel05988662008-09-25 21:00:45 +0000982 /// getAttributes - Return the parameter attributes for this call.
Chris Lattner041221c2008-03-13 04:33:03 +0000983 ///
Devang Patel05988662008-09-25 21:00:45 +0000984 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencer4746ecf2007-04-09 15:01:12 +0000985
Dan Gohmanf2752502008-09-26 21:38:45 +0000986 /// setAttributes - Set the parameter attributes for this call.
987 ///
Devang Patel05988662008-09-25 21:00:45 +0000988 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000989
Devang Patel05988662008-09-25 21:00:45 +0000990 /// addAttribute - adds the attribute to the list of attributes.
991 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsdc024672007-11-27 13:23:08 +0000992
Devang Patel05988662008-09-25 21:00:45 +0000993 /// removeAttribute - removes the attribute from the list of attributes.
994 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +0000995
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000996 /// @brief Determine whether the call or the callee has the given attribute.
Dan Gohmanf2752502008-09-26 21:38:45 +0000997 bool paramHasAttr(unsigned i, Attributes attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000998
Dale Johannesen08e78b12008-02-22 17:49:45 +0000999 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001000 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00001001 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001002 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001003
Eric Christopherf27e6082010-03-25 04:49:10 +00001004 /// @brief Return true if the call should not be inlined.
1005 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
Nick Lewyckyb9933b82010-07-06 03:53:22 +00001006 void setIsNoInline(bool Value = true) {
Eric Christopherf27e6082010-03-25 04:49:10 +00001007 if (Value) addAttribute(~0, Attribute::NoInline);
1008 else removeAttribute(~0, Attribute::NoInline);
1009 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001010
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001011 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001012 bool doesNotAccessMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00001013 return paramHasAttr(~0, Attribute::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001014 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001015 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001016 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1017 else removeAttribute(~0, Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00001018 }
1019
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001020 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001021 bool onlyReadsMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00001022 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001023 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001024 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001025 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1026 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00001027 }
1028
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001029 /// @brief Determine if the call cannot return.
Nick Lewyckyb9933b82010-07-06 03:53:22 +00001030 bool doesNotReturn() const { return paramHasAttr(~0, Attribute::NoReturn); }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001031 void setDoesNotReturn(bool DoesNotReturn = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001032 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1033 else removeAttribute(~0, Attribute::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00001034 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001035
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001036 /// @brief Determine if the call cannot unwind.
Nick Lewyckyb9933b82010-07-06 03:53:22 +00001037 bool doesNotThrow() const { return paramHasAttr(~0, Attribute::NoUnwind); }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001038 void setDoesNotThrow(bool DoesNotThrow = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001039 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1040 else removeAttribute(~0, Attribute::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00001041 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001042
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001043 /// @brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00001044 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001045 bool hasStructRetAttr() const {
1046 // Be friendly and also check the callee.
Devang Patel05988662008-09-25 21:00:45 +00001047 return paramHasAttr(1, Attribute::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001048 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001049
Evan Chengf4a54982008-01-12 18:57:32 +00001050 /// @brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001051 bool hasByValArgument() const {
Devang Patel05988662008-09-25 21:00:45 +00001052 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001053 }
Evan Chengf4a54982008-01-12 18:57:32 +00001054
Dan Gohmanf2752502008-09-26 21:38:45 +00001055 /// getCalledFunction - Return the function called, or null if this is an
1056 /// indirect function invocation.
1057 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001058 Function *getCalledFunction() const {
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001059 return dyn_cast<Function>(Op<-1>());
Chris Lattner721aef62004-11-18 17:46:57 +00001060 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001061
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001062 /// getCalledValue - Get a pointer to the function that is invoked by this
Chris Lattner14d80382009-10-18 05:08:07 +00001063 /// instruction.
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001064 const Value *getCalledValue() const { return Op<-1>(); }
1065 Value *getCalledValue() { return Op<-1>(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001066
Chris Lattner14d80382009-10-18 05:08:07 +00001067 /// setCalledFunction - Set the function called.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001068 void setCalledFunction(Value* Fn) {
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001069 Op<-1>() = Fn;
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001070 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001071
Owen Anderson6f615f82010-08-07 00:19:59 +00001072 /// isInlineAsm - Check if this call is an inline asm statement.
1073 bool isInlineAsm() const {
1074 return isa<InlineAsm>(Op<-1>());
1075 }
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001076
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001077 // Methods for support type inquiry through isa, cast, and dyn_cast:
1078 static inline bool classof(const CallInst *) { return true; }
1079 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001080 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001081 }
1082 static inline bool classof(const Value *V) {
1083 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1084 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001085private:
Chris Lattnerb2406d92009-12-29 02:46:09 +00001086 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1087 // method so that subclasses cannot accidentally use it.
1088 void setInstructionSubclassData(unsigned short D) {
1089 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001090 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001091};
1092
Gabor Greifefe65362008-05-10 08:32:32 +00001093template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001094struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
Gabor Greifefe65362008-05-10 08:32:32 +00001095};
1096
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001097template<typename RandomAccessIterator>
1098CallInst::CallInst(Value *Func,
1099 RandomAccessIterator ArgBegin, RandomAccessIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001100 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001101 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1102 ->getElementType())->getReturnType(),
1103 Instruction::Call,
1104 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Gabor Greif16575de2010-06-29 16:27:38 +00001105 unsigned(ArgEnd - ArgBegin + 1), InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001106 init(Func, ArgBegin, ArgEnd, NameStr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001107 typename std::iterator_traits<RandomAccessIterator>
1108 ::iterator_category());
Gabor Greifefe65362008-05-10 08:32:32 +00001109}
1110
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001111template<typename RandomAccessIterator>
1112CallInst::CallInst(Value *Func,
1113 RandomAccessIterator ArgBegin, RandomAccessIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001114 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00001115 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1116 ->getElementType())->getReturnType(),
1117 Instruction::Call,
1118 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Gabor Greifdc4f3912010-07-07 09:29:07 +00001119 unsigned(ArgEnd - ArgBegin + 1), InsertBefore) {
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001120 init(Func, ArgBegin, ArgEnd, NameStr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001121 typename std::iterator_traits<RandomAccessIterator>
1122 ::iterator_category());
Gabor Greifefe65362008-05-10 08:32:32 +00001123}
1124
Gabor Greife9e12152010-07-06 15:44:11 +00001125
1126// Note: if you get compile errors about private methods then
1127// please update your code to use the high-level operand
1128// interfaces. See line 943 above.
Gabor Greifefe65362008-05-10 08:32:32 +00001129DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1130
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001131//===----------------------------------------------------------------------===//
1132// SelectInst Class
1133//===----------------------------------------------------------------------===//
1134
1135/// SelectInst - This class represents the LLVM 'select' instruction.
1136///
1137class SelectInst : public Instruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001138 void init(Value *C, Value *S1, Value *S2) {
Chris Lattnerb76ec322008-12-29 00:12:50 +00001139 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
Gabor Greifefe65362008-05-10 08:32:32 +00001140 Op<0>() = C;
1141 Op<1>() = S1;
1142 Op<2>() = S2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001143 }
1144
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001145 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001146 Instruction *InsertBefore)
1147 : Instruction(S1->getType(), Instruction::Select,
1148 &Op<0>(), 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001149 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001150 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001151 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001152 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001153 BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001154 : Instruction(S1->getType(), Instruction::Select,
1155 &Op<0>(), 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001156 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001157 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001158 }
Devang Patel50b6e332009-10-27 22:16:29 +00001159protected:
1160 virtual SelectInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001161public:
Evan Chengd69bb1a2008-05-05 17:41:03 +00001162 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001163 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001164 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001165 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001166 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001167 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001168 const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001169 BasicBlock *InsertAtEnd) {
1170 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001171 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001172
Chris Lattner97159122009-09-08 03:32:53 +00001173 const Value *getCondition() const { return Op<0>(); }
1174 const Value *getTrueValue() const { return Op<1>(); }
1175 const Value *getFalseValue() const { return Op<2>(); }
1176 Value *getCondition() { return Op<0>(); }
1177 Value *getTrueValue() { return Op<1>(); }
1178 Value *getFalseValue() { return Op<2>(); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001179
Chris Lattnerb76ec322008-12-29 00:12:50 +00001180 /// areInvalidOperands - Return a string if the specified operands are invalid
1181 /// for a select operation, otherwise return null.
1182 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
Chris Lattner454928e2005-01-29 00:31:36 +00001183
1184 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001185 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001186
1187 OtherOps getOpcode() const {
1188 return static_cast<OtherOps>(Instruction::getOpcode());
1189 }
1190
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001191 // Methods for support type inquiry through isa, cast, and dyn_cast:
1192 static inline bool classof(const SelectInst *) { return true; }
1193 static inline bool classof(const Instruction *I) {
1194 return I->getOpcode() == Instruction::Select;
1195 }
1196 static inline bool classof(const Value *V) {
1197 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1198 }
1199};
1200
Gabor Greifefe65362008-05-10 08:32:32 +00001201template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001202struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001203};
1204
1205DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1206
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001207//===----------------------------------------------------------------------===//
1208// VAArgInst Class
1209//===----------------------------------------------------------------------===//
1210
1211/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001212/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001213///
Chris Lattner454928e2005-01-29 00:31:36 +00001214class VAArgInst : public UnaryInstruction {
Devang Patel50b6e332009-10-27 22:16:29 +00001215protected:
1216 virtual VAArgInst *clone_impl() const;
1217
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001218public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001219 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001220 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001221 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001222 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001223 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001224 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001225 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001226 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001227 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001228 }
1229
Dan Gohmane7445412010-09-09 18:32:40 +00001230 Value *getPointerOperand() { return getOperand(0); }
1231 const Value *getPointerOperand() const { return getOperand(0); }
1232 static unsigned getPointerOperandIndex() { return 0U; }
1233
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001234 // Methods for support type inquiry through isa, cast, and dyn_cast:
1235 static inline bool classof(const VAArgInst *) { return true; }
1236 static inline bool classof(const Instruction *I) {
1237 return I->getOpcode() == VAArg;
1238 }
1239 static inline bool classof(const Value *V) {
1240 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1241 }
1242};
1243
1244//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001245// ExtractElementInst Class
1246//===----------------------------------------------------------------------===//
1247
1248/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001249/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001250///
1251class ExtractElementInst : public Instruction {
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001252 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
Chris Lattner9fc18d22006-04-08 01:15:18 +00001253 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001254 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
Chris Lattner9fc18d22006-04-08 01:15:18 +00001255 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001256protected:
1257 virtual ExtractElementInst *clone_impl() const;
1258
Eric Christophera3500da2009-07-25 02:28:41 +00001259public:
Eric Christophera3500da2009-07-25 02:28:41 +00001260 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001261 const Twine &NameStr = "",
Eric Christophera3500da2009-07-25 02:28:41 +00001262 Instruction *InsertBefore = 0) {
1263 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1264 }
1265 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001266 const Twine &NameStr,
Eric Christophera3500da2009-07-25 02:28:41 +00001267 BasicBlock *InsertAtEnd) {
1268 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1269 }
Robert Bocchino49b78a52006-01-10 19:04:13 +00001270
Chris Lattnerfa495842006-04-08 04:04:54 +00001271 /// isValidOperands - Return true if an extractelement instruction can be
1272 /// formed with the specified operands.
1273 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001274
Chris Lattner97159122009-09-08 03:32:53 +00001275 Value *getVectorOperand() { return Op<0>(); }
1276 Value *getIndexOperand() { return Op<1>(); }
1277 const Value *getVectorOperand() const { return Op<0>(); }
1278 const Value *getIndexOperand() const { return Op<1>(); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001279
Chris Lattner97159122009-09-08 03:32:53 +00001280 const VectorType *getVectorOperandType() const {
Chris Lattnerc8dee3f2009-09-08 03:39:55 +00001281 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
Chris Lattner97159122009-09-08 03:32:53 +00001282 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001283
1284
Robert Bocchino49b78a52006-01-10 19:04:13 +00001285 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001286 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001287
1288 // Methods for support type inquiry through isa, cast, and dyn_cast:
1289 static inline bool classof(const ExtractElementInst *) { return true; }
1290 static inline bool classof(const Instruction *I) {
1291 return I->getOpcode() == Instruction::ExtractElement;
1292 }
1293 static inline bool classof(const Value *V) {
1294 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1295 }
1296};
1297
Gabor Greifefe65362008-05-10 08:32:32 +00001298template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001299struct OperandTraits<ExtractElementInst> :
1300 public FixedNumOperandTraits<ExtractElementInst, 2> {
Gabor Greifefe65362008-05-10 08:32:32 +00001301};
1302
1303DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1304
Robert Bocchino49b78a52006-01-10 19:04:13 +00001305//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001306// InsertElementInst Class
1307//===----------------------------------------------------------------------===//
1308
1309/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001310/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001311///
1312class InsertElementInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001313 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001314 const Twine &NameStr = "",
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001315 Instruction *InsertBefore = 0);
Gabor Greif051a9502008-04-06 20:25:17 +00001316 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001317 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001318protected:
1319 virtual InsertElementInst *clone_impl() const;
1320
Robert Bocchinof9993442006-01-17 20:05:59 +00001321public:
Gabor Greif051a9502008-04-06 20:25:17 +00001322 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001323 const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +00001324 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001325 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001326 }
Gabor Greif051a9502008-04-06 20:25:17 +00001327 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001328 const Twine &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001329 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001330 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001331 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001332
Chris Lattnerfa495842006-04-08 04:04:54 +00001333 /// isValidOperands - Return true if an insertelement instruction can be
1334 /// formed with the specified operands.
1335 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1336 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001337
Reid Spencerac9dcb92007-02-15 03:39:18 +00001338 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001339 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001340 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001341 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001342 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001343
Robert Bocchinof9993442006-01-17 20:05:59 +00001344 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001345 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchinof9993442006-01-17 20:05:59 +00001346
1347 // Methods for support type inquiry through isa, cast, and dyn_cast:
1348 static inline bool classof(const InsertElementInst *) { return true; }
1349 static inline bool classof(const Instruction *I) {
1350 return I->getOpcode() == Instruction::InsertElement;
1351 }
1352 static inline bool classof(const Value *V) {
1353 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1354 }
1355};
1356
Gabor Greifefe65362008-05-10 08:32:32 +00001357template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001358struct OperandTraits<InsertElementInst> :
1359 public FixedNumOperandTraits<InsertElementInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001360};
1361
1362DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1363
Robert Bocchinof9993442006-01-17 20:05:59 +00001364//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001365// ShuffleVectorInst Class
1366//===----------------------------------------------------------------------===//
1367
1368/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1369/// input vectors.
1370///
1371class ShuffleVectorInst : public Instruction {
Devang Patel50b6e332009-10-27 22:16:29 +00001372protected:
1373 virtual ShuffleVectorInst *clone_impl() const;
1374
Chris Lattner9fc18d22006-04-08 01:15:18 +00001375public:
Gabor Greif051a9502008-04-06 20:25:17 +00001376 // allocate space for exactly three operands
1377 void *operator new(size_t s) {
1378 return User::operator new(s, 3);
1379 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001380 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001381 const Twine &NameStr = "",
Evan Cheng1bf9a182008-07-24 00:08:56 +00001382 Instruction *InsertBefor = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001383 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001384 const Twine &NameStr, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001385
Chris Lattnerfa495842006-04-08 04:04:54 +00001386 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001387 /// formed with the specified operands.
1388 static bool isValidOperands(const Value *V1, const Value *V2,
1389 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001390
Reid Spencerac9dcb92007-02-15 03:39:18 +00001391 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001392 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001393 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001394 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001395 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001396
Chris Lattner9fc18d22006-04-08 01:15:18 +00001397 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001398 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001399
Chris Lattner8728f192008-03-02 05:28:33 +00001400 /// getMaskValue - Return the index from the shuffle mask for the specified
1401 /// output result. This is either -1 if the element is undef or a number less
1402 /// than 2*numelements.
1403 int getMaskValue(unsigned i) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001404
Chris Lattner9fc18d22006-04-08 01:15:18 +00001405 // Methods for support type inquiry through isa, cast, and dyn_cast:
1406 static inline bool classof(const ShuffleVectorInst *) { return true; }
1407 static inline bool classof(const Instruction *I) {
1408 return I->getOpcode() == Instruction::ShuffleVector;
1409 }
1410 static inline bool classof(const Value *V) {
1411 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1412 }
1413};
1414
Gabor Greifefe65362008-05-10 08:32:32 +00001415template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001416struct OperandTraits<ShuffleVectorInst> :
1417 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001418};
1419
1420DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
Chris Lattner9fc18d22006-04-08 01:15:18 +00001421
1422//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001423// ExtractValueInst Class
1424//===----------------------------------------------------------------------===//
1425
Dan Gohmane2d896f2008-05-15 23:35:32 +00001426/// ExtractValueInst - This instruction extracts a struct member or array
1427/// element value from an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001428///
Gabor Greifd4f268b2008-06-06 20:28:12 +00001429class ExtractValueInst : public UnaryInstruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001430 SmallVector<unsigned, 4> Indices;
1431
Dan Gohman041e2eb2008-05-15 19:50:34 +00001432 ExtractValueInst(const ExtractValueInst &EVI);
Gabor Greif76aca6f2008-06-06 21:06:32 +00001433 void init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001434 const Twine &NameStr);
1435 void init(unsigned Idx, const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001436
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001437 template<typename RandomAccessIterator>
1438 void init(RandomAccessIterator IdxBegin,
1439 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001440 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001441 // This argument ensures that we have an iterator we can
1442 // do arithmetic on in constant time
1443 std::random_access_iterator_tag) {
1444 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001445
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001446 // There's no fundamental reason why we require at least one index
1447 // (other than weirdness with &*IdxBegin being invalid; see
1448 // getelementptr's init routine for example). But there's no
1449 // present need to support it.
1450 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1451
1452 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +00001453 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001454 // we have to build an array here
Dan Gohman041e2eb2008-05-15 19:50:34 +00001455 }
1456
1457 /// getIndexedType - Returns the type of the element that would be extracted
1458 /// with an extractvalue instruction with the specified parameters.
1459 ///
Frits van Bommelaf72c622010-12-03 14:54:33 +00001460 /// Null is returned if the indices are invalid for the specified type.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001461 ///
1462 static const Type *getIndexedType(const Type *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001463 const unsigned *Idx, unsigned NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001464
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001465 template<typename RandomAccessIterator>
Dan Gohman041e2eb2008-05-15 19:50:34 +00001466 static const Type *getIndexedType(const Type *Ptr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001467 RandomAccessIterator IdxBegin,
1468 RandomAccessIterator IdxEnd,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001469 // This argument ensures that we
1470 // have an iterator we can do
1471 // arithmetic on in constant time
1472 std::random_access_iterator_tag) {
1473 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1474
1475 if (NumIdx > 0)
1476 // This requires that the iterator points to contiguous memory.
Dan Gohman19a81632008-06-23 16:38:10 +00001477 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001478 else
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001479 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001480 }
1481
Dan Gohmane2d896f2008-05-15 23:35:32 +00001482 /// Constructors - Create a extractvalue instruction with a base aggregate
1483 /// value and a list of indices. The first ctor can optionally insert before
1484 /// an existing instruction, the second appends the new instruction to the
1485 /// specified BasicBlock.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001486 template<typename RandomAccessIterator>
1487 inline ExtractValueInst(Value *Agg,
1488 RandomAccessIterator IdxBegin,
1489 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001490 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001491 Instruction *InsertBefore);
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001492 template<typename RandomAccessIterator>
Dan Gohman041e2eb2008-05-15 19:50:34 +00001493 inline ExtractValueInst(Value *Agg,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001494 RandomAccessIterator IdxBegin,
1495 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001496 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001497
Dan Gohman8e640412008-05-31 19:09:47 +00001498 // allocate space for exactly one operand
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001499 void *operator new(size_t s) {
1500 return User::operator new(s, 1);
1501 }
Devang Patel50b6e332009-10-27 22:16:29 +00001502protected:
1503 virtual ExtractValueInst *clone_impl() const;
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001504
Gabor Greifd4f268b2008-06-06 20:28:12 +00001505public:
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001506 template<typename RandomAccessIterator>
1507 static ExtractValueInst *Create(Value *Agg,
1508 RandomAccessIterator IdxBegin,
1509 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001510 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001511 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001512 return new
Evan Cheng1bf9a182008-07-24 00:08:56 +00001513 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001514 }
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001515 template<typename RandomAccessIterator>
Dan Gohman041e2eb2008-05-15 19:50:34 +00001516 static ExtractValueInst *Create(Value *Agg,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001517 RandomAccessIterator IdxBegin,
1518 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001519 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001520 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001521 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001522 }
1523
1524 /// Constructors - These two creators are convenience methods because one
1525 /// index extractvalue instructions are much more common than those with
1526 /// more than one.
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001527 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001528 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001529 Instruction *InsertBefore = 0) {
Dan Gohman2f27e172008-06-23 16:48:17 +00001530 unsigned Idxs[1] = { Idx };
Evan Cheng1bf9a182008-07-24 00:08:56 +00001531 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001532 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001533 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001534 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001535 BasicBlock *InsertAtEnd) {
Dan Gohman2f27e172008-06-23 16:48:17 +00001536 unsigned Idxs[1] = { Idx };
Evan Cheng1bf9a182008-07-24 00:08:56 +00001537 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001538 }
1539
Dan Gohman041e2eb2008-05-15 19:50:34 +00001540 /// getIndexedType - Returns the type of the element that would be extracted
1541 /// with an extractvalue instruction with the specified parameters.
1542 ///
Frits van Bommelaf72c622010-12-03 14:54:33 +00001543 /// Null is returned if the indices are invalid for the specified type.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001544 ///
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001545 template<typename RandomAccessIterator>
Dan Gohman041e2eb2008-05-15 19:50:34 +00001546 static const Type *getIndexedType(const Type *Ptr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001547 RandomAccessIterator IdxBegin,
1548 RandomAccessIterator IdxEnd) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001549 return getIndexedType(Ptr, IdxBegin, IdxEnd,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001550 typename std::iterator_traits<RandomAccessIterator>::
Dan Gohman041e2eb2008-05-15 19:50:34 +00001551 iterator_category());
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001552 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001553 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001554
Owen Anderson5678d6e2008-06-19 17:15:57 +00001555 typedef const unsigned* idx_iterator;
1556 inline idx_iterator idx_begin() const { return Indices.begin(); }
1557 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001558
1559 Value *getAggregateOperand() {
1560 return getOperand(0);
1561 }
1562 const Value *getAggregateOperand() const {
1563 return getOperand(0);
1564 }
1565 static unsigned getAggregateOperandIndex() {
1566 return 0U; // get index for modifying correct operand
1567 }
1568
1569 unsigned getNumIndices() const { // Note: always non-negative
Bill Wendling67944fc2008-06-05 07:35:27 +00001570 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001571 }
1572
1573 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001574 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001575 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001576
Dan Gohman041e2eb2008-05-15 19:50:34 +00001577 // Methods for support type inquiry through isa, cast, and dyn_cast:
1578 static inline bool classof(const ExtractValueInst *) { return true; }
1579 static inline bool classof(const Instruction *I) {
1580 return I->getOpcode() == Instruction::ExtractValue;
1581 }
1582 static inline bool classof(const Value *V) {
1583 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1584 }
1585};
1586
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001587template<typename RandomAccessIterator>
Dan Gohmane4569942008-05-23 00:36:11 +00001588ExtractValueInst::ExtractValueInst(Value *Agg,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001589 RandomAccessIterator IdxBegin,
1590 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001591 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001592 Instruction *InsertBefore)
Gabor Greifd4f268b2008-06-06 20:28:12 +00001593 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
Bill Wendling85f40542008-07-22 07:14:12 +00001594 IdxBegin, IdxEnd)),
1595 ExtractValue, Agg, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001596 init(IdxBegin, IdxEnd, NameStr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001597 typename std::iterator_traits<RandomAccessIterator>
1598 ::iterator_category());
Dan Gohmane4569942008-05-23 00:36:11 +00001599}
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001600template<typename RandomAccessIterator>
Dan Gohmane4569942008-05-23 00:36:11 +00001601ExtractValueInst::ExtractValueInst(Value *Agg,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001602 RandomAccessIterator IdxBegin,
1603 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001604 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001605 BasicBlock *InsertAtEnd)
Gabor Greifd4f268b2008-06-06 20:28:12 +00001606 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
Bill Wendling85f40542008-07-22 07:14:12 +00001607 IdxBegin, IdxEnd)),
1608 ExtractValue, Agg, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001609 init(IdxBegin, IdxEnd, NameStr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001610 typename std::iterator_traits<RandomAccessIterator>
1611 ::iterator_category());
Dan Gohmane4569942008-05-23 00:36:11 +00001612}
1613
Dan Gohmane4569942008-05-23 00:36:11 +00001614
Dan Gohman041e2eb2008-05-15 19:50:34 +00001615//===----------------------------------------------------------------------===//
1616// InsertValueInst Class
1617//===----------------------------------------------------------------------===//
1618
Dan Gohmane2d896f2008-05-15 23:35:32 +00001619/// InsertValueInst - This instruction inserts a struct field of array element
1620/// value into an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001621///
1622class InsertValueInst : public Instruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001623 SmallVector<unsigned, 4> Indices;
1624
1625 void *operator new(size_t, unsigned); // Do not implement
Dan Gohman041e2eb2008-05-15 19:50:34 +00001626 InsertValueInst(const InsertValueInst &IVI);
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +00001627 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001628 const Twine &NameStr);
1629 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001630
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001631 template<typename RandomAccessIterator>
Dan Gohman041e2eb2008-05-15 19:50:34 +00001632 void init(Value *Agg, Value *Val,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001633 RandomAccessIterator IdxBegin, RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001634 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001635 // This argument ensures that we have an iterator we can
1636 // do arithmetic on in constant time
1637 std::random_access_iterator_tag) {
1638 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001639
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001640 // There's no fundamental reason why we require at least one index
1641 // (other than weirdness with &*IdxBegin being invalid; see
1642 // getelementptr's init routine for example). But there's no
1643 // present need to support it.
1644 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1645
1646 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +00001647 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001648 // we have to build an array here
Dan Gohman041e2eb2008-05-15 19:50:34 +00001649 }
1650
Dan Gohmane2d896f2008-05-15 23:35:32 +00001651 /// Constructors - Create a insertvalue instruction with a base aggregate
1652 /// value, a value to insert, and a list of indices. The first ctor can
1653 /// optionally insert before an existing instruction, the second appends
1654 /// the new instruction to the specified BasicBlock.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001655 template<typename RandomAccessIterator>
1656 inline InsertValueInst(Value *Agg, Value *Val,
1657 RandomAccessIterator IdxBegin,
1658 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001659 const Twine &NameStr,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001660 Instruction *InsertBefore);
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001661 template<typename RandomAccessIterator>
Dan Gohman041e2eb2008-05-15 19:50:34 +00001662 inline InsertValueInst(Value *Agg, Value *Val,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001663 RandomAccessIterator IdxBegin,
1664 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001665 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001666
1667 /// Constructors - These two constructors are convenience methods because one
1668 /// and two index insertvalue instructions are so common.
1669 InsertValueInst(Value *Agg, Value *Val,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001670 unsigned Idx, const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001671 Instruction *InsertBefore = 0);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001672 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001673 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001674protected:
1675 virtual InsertValueInst *clone_impl() const;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001676public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001677 // allocate space for exactly two operands
1678 void *operator new(size_t s) {
1679 return User::operator new(s, 2);
1680 }
1681
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001682 template<typename RandomAccessIterator>
1683 static InsertValueInst *Create(Value *Agg, Value *Val,
1684 RandomAccessIterator IdxBegin,
1685 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001686 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001687 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001688 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001689 NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001690 }
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001691 template<typename RandomAccessIterator>
Dan Gohman041e2eb2008-05-15 19:50:34 +00001692 static InsertValueInst *Create(Value *Agg, Value *Val,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001693 RandomAccessIterator IdxBegin,
1694 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001695 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001696 BasicBlock *InsertAtEnd) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001697 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001698 NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001699 }
1700
1701 /// Constructors - These two creators are convenience methods because one
1702 /// index insertvalue instructions are much more common than those with
1703 /// more than one.
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001704 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001705 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001706 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001707 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001708 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001709 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001710 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001711 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001712 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001713 }
1714
Dan Gohman041e2eb2008-05-15 19:50:34 +00001715 /// Transparently provide more efficient getOperand methods.
1716 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1717
Owen Anderson5678d6e2008-06-19 17:15:57 +00001718 typedef const unsigned* idx_iterator;
1719 inline idx_iterator idx_begin() const { return Indices.begin(); }
1720 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001721
1722 Value *getAggregateOperand() {
1723 return getOperand(0);
1724 }
1725 const Value *getAggregateOperand() const {
1726 return getOperand(0);
1727 }
1728 static unsigned getAggregateOperandIndex() {
1729 return 0U; // get index for modifying correct operand
1730 }
1731
1732 Value *getInsertedValueOperand() {
1733 return getOperand(1);
1734 }
1735 const Value *getInsertedValueOperand() const {
1736 return getOperand(1);
1737 }
1738 static unsigned getInsertedValueOperandIndex() {
1739 return 1U; // get index for modifying correct operand
1740 }
1741
1742 unsigned getNumIndices() const { // Note: always non-negative
Bill Wendling67944fc2008-06-05 07:35:27 +00001743 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001744 }
1745
1746 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001747 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001748 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001749
Dan Gohman041e2eb2008-05-15 19:50:34 +00001750 // Methods for support type inquiry through isa, cast, and dyn_cast:
1751 static inline bool classof(const InsertValueInst *) { return true; }
1752 static inline bool classof(const Instruction *I) {
1753 return I->getOpcode() == Instruction::InsertValue;
1754 }
1755 static inline bool classof(const Value *V) {
1756 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1757 }
1758};
1759
1760template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001761struct OperandTraits<InsertValueInst> :
1762 public FixedNumOperandTraits<InsertValueInst, 2> {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001763};
1764
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001765template<typename RandomAccessIterator>
Dan Gohmane4569942008-05-23 00:36:11 +00001766InsertValueInst::InsertValueInst(Value *Agg,
1767 Value *Val,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001768 RandomAccessIterator IdxBegin,
1769 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001770 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001771 Instruction *InsertBefore)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001772 : Instruction(Agg->getType(), InsertValue,
1773 OperandTraits<InsertValueInst>::op_begin(this),
1774 2, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001775 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001776 typename std::iterator_traits<RandomAccessIterator>
1777 ::iterator_category());
Dan Gohmane4569942008-05-23 00:36:11 +00001778}
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001779template<typename RandomAccessIterator>
Dan Gohmane4569942008-05-23 00:36:11 +00001780InsertValueInst::InsertValueInst(Value *Agg,
1781 Value *Val,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001782 RandomAccessIterator IdxBegin,
1783 RandomAccessIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001784 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001785 BasicBlock *InsertAtEnd)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001786 : Instruction(Agg->getType(), InsertValue,
1787 OperandTraits<InsertValueInst>::op_begin(this),
1788 2, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001789 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001790 typename std::iterator_traits<RandomAccessIterator>
1791 ::iterator_category());
Dan Gohmane4569942008-05-23 00:36:11 +00001792}
1793
Dan Gohman041e2eb2008-05-15 19:50:34 +00001794DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1795
1796//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001797// PHINode Class
1798//===----------------------------------------------------------------------===//
1799
1800// PHINode - The PHINode class is used to represent the magical mystical PHI
1801// node, that can not exist in nature, but can be synthesized in a computer
1802// scientist's overactive imagination.
1803//
1804class PHINode : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001805 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00001806 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1807 /// the number actually in use.
1808 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001809 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00001810 // allocate space for exactly zero operands
1811 void *operator new(size_t s) {
1812 return User::operator new(s, 0);
1813 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001814 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001815 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001816 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Chris Lattner454928e2005-01-29 00:31:36 +00001817 ReservedSpace(0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001818 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001819 }
1820
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001821 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001822 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Chris Lattner454928e2005-01-29 00:31:36 +00001823 ReservedSpace(0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001824 setName(NameStr);
Chris Lattner454928e2005-01-29 00:31:36 +00001825 }
Devang Patel50b6e332009-10-27 22:16:29 +00001826protected:
1827 virtual PHINode *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001828public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001829 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001830 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001831 return new PHINode(Ty, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001832 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001833 static PHINode *Create(const Type *Ty, const Twine &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001834 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001835 return new PHINode(Ty, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001836 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001837 ~PHINode();
1838
Chris Lattner454928e2005-01-29 00:31:36 +00001839 /// reserveOperandSpace - This method can be used to avoid repeated
1840 /// reallocation of PHI operand lists by reserving space for the correct
1841 /// number of operands before adding them. Unlike normal vector reserves,
1842 /// this method can also be used to trim the operand space.
1843 void reserveOperandSpace(unsigned NumValues) {
1844 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001845 }
1846
Gabor Greifefe65362008-05-10 08:32:32 +00001847 /// Provide fast operand accessors
1848 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1849
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001850 /// getNumIncomingValues - Return the number of incoming edges
1851 ///
Chris Lattner454928e2005-01-29 00:31:36 +00001852 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001853
Reid Spencerc773de62006-05-19 19:07:54 +00001854 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001855 ///
1856 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001857 assert(i*2 < getNumOperands() && "Invalid value number!");
1858 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001859 }
1860 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +00001861 assert(i*2 < getNumOperands() && "Invalid value number!");
1862 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001863 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001864 static unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001865 return i*2;
1866 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001867 static unsigned getIncomingValueNumForOperand(unsigned i) {
1868 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1869 return i/2;
1870 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001871
Dan Gohmanfb76fe02010-02-22 04:10:52 +00001872 /// getIncomingBlock - Return incoming basic block number @p i.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001873 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00001874 BasicBlock *getIncomingBlock(unsigned i) const {
Chris Lattnerfc61aef2009-10-10 08:01:27 +00001875 return cast<BasicBlock>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001876 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001877
Chris Lattnerceaa4572009-10-10 07:42:42 +00001878 /// getIncomingBlock - Return incoming basic block corresponding
1879 /// to an operand of the PHI.
1880 ///
1881 BasicBlock *getIncomingBlock(const Use &U) const {
1882 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
Chris Lattnerfc61aef2009-10-10 08:01:27 +00001883 return cast<BasicBlock>((&U + 1)->get());
Chris Lattnerceaa4572009-10-10 07:42:42 +00001884 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001885
Chris Lattnerceaa4572009-10-10 07:42:42 +00001886 /// getIncomingBlock - Return incoming basic block corresponding
1887 /// to value use iterator.
1888 ///
1889 template <typename U>
1890 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1891 return getIncomingBlock(I.getUse());
1892 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001893
1894
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001895 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Chris Lattner4b122932009-10-27 16:49:53 +00001896 setOperand(i*2+1, (Value*)BB);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001897 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001898 static unsigned getOperandNumForIncomingBlock(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001899 return i*2+1;
1900 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001901 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1902 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1903 return i/2;
1904 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001905
1906 /// addIncoming - Add an incoming value to the end of the PHI list
1907 ///
1908 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00001909 assert(V && "PHI node got a null value!");
1910 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001911 assert(getType() == V->getType() &&
1912 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +00001913 unsigned OpNo = NumOperands;
1914 if (OpNo+2 > ReservedSpace)
1915 resizeOperands(0); // Get more space!
1916 // Initialize some new operands.
1917 NumOperands = OpNo+2;
Gabor Greif6c80c382008-05-26 21:33:52 +00001918 OperandList[OpNo] = V;
Chris Lattner4b122932009-10-27 16:49:53 +00001919 OperandList[OpNo+1] = (Value*)BB;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001920 }
Misha Brukman9769ab22005-04-21 20:19:05 +00001921
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001922 /// removeIncomingValue - Remove an incoming value. This is useful if a
1923 /// predecessor basic block is deleted. The value removed is returned.
1924 ///
1925 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1926 /// is true), the PHI node is destroyed and any uses of it are replaced with
1927 /// dummy values. The only time there should be zero incoming values to a PHI
1928 /// node is when the block is dead, so this strategy is sound.
1929 ///
1930 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1931
Gabor Greifefe65362008-05-10 08:32:32 +00001932 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001933 int Idx = getBasicBlockIndex(BB);
1934 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1935 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1936 }
1937
Misha Brukman9769ab22005-04-21 20:19:05 +00001938 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001939 /// block in the value list for this PHI. Returns -1 if no instance.
1940 ///
1941 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001942 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00001943 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Chris Lattner4b122932009-10-27 16:49:53 +00001944 if (OL[i+1].get() == (const Value*)BB) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001945 return -1;
1946 }
1947
1948 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1949 return getIncomingValue(getBasicBlockIndex(BB));
1950 }
1951
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001952 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00001953 /// same value, return the value, otherwise return null.
Duncan Sandsff103412010-11-17 04:30:22 +00001954 Value *hasConstantValue() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001955
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001956 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1957 static inline bool classof(const PHINode *) { return true; }
1958 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001959 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001960 }
1961 static inline bool classof(const Value *V) {
1962 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1963 }
Chris Lattner454928e2005-01-29 00:31:36 +00001964 private:
1965 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001966};
1967
Gabor Greifefe65362008-05-10 08:32:32 +00001968template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001969struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00001970};
1971
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001972DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00001973
1974
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001975//===----------------------------------------------------------------------===//
1976// ReturnInst Class
1977//===----------------------------------------------------------------------===//
1978
1979//===---------------------------------------------------------------------------
1980/// ReturnInst - Return a value (possibly void), from a function. Execution
1981/// does not continue in this function any longer.
1982///
1983class ReturnInst : public TerminatorInst {
Chris Lattner910c80a2007-02-24 00:55:48 +00001984 ReturnInst(const ReturnInst &RI);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001985
Gabor Greif051a9502008-04-06 20:25:17 +00001986private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001987 // ReturnInst constructors:
1988 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001989 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001990 // ReturnInst(Value* X) - 'ret X' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00001991 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001992 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
Gabor Greifefe65362008-05-10 08:32:32 +00001993 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1994 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001995 //
1996 // NOTE: If the Value* passed is of type void then the constructor behaves as
1997 // if it was passed NULL.
Owen Anderson1d0be152009-08-13 21:58:54 +00001998 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1999 Instruction *InsertBefore = 0);
2000 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2001 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002002protected:
2003 virtual ReturnInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002004public:
Owen Anderson1d0be152009-08-13 21:58:54 +00002005 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2006 Instruction *InsertBefore = 0) {
2007 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002008 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002009 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2010 BasicBlock *InsertAtEnd) {
2011 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002012 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002013 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2014 return new(0) ReturnInst(C, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002015 }
Devang Patel57ef4f42008-02-23 00:35:18 +00002016 virtual ~ReturnInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002017
Gabor Greifefe65362008-05-10 08:32:32 +00002018 /// Provide fast operand accessors
2019 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Devang Patel64d4e612008-02-26 17:56:20 +00002020
Dan Gohman8faa6af2010-10-06 16:59:24 +00002021 /// Convenience accessor. Returns null if there is no return value.
2022 Value *getReturnValue() const {
2023 return getNumOperands() != 0 ? getOperand(0) : 0;
Devang Patel1eafa062008-03-11 17:35:03 +00002024 }
2025
Chris Lattner454928e2005-01-29 00:31:36 +00002026 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002027
2028 // Methods for support type inquiry through isa, cast, and dyn_cast:
2029 static inline bool classof(const ReturnInst *) { return true; }
2030 static inline bool classof(const Instruction *I) {
2031 return (I->getOpcode() == Instruction::Ret);
2032 }
2033 static inline bool classof(const Value *V) {
2034 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2035 }
Chris Lattner454928e2005-01-29 00:31:36 +00002036 private:
2037 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2038 virtual unsigned getNumSuccessorsV() const;
2039 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002040};
2041
Gabor Greifefe65362008-05-10 08:32:32 +00002042template <>
Jay Foad67c619b2011-01-11 15:07:38 +00002043struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
Gabor Greifefe65362008-05-10 08:32:32 +00002044};
2045
2046DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002047
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002048//===----------------------------------------------------------------------===//
2049// BranchInst Class
2050//===----------------------------------------------------------------------===//
2051
2052//===---------------------------------------------------------------------------
2053/// BranchInst - Conditional or Unconditional Branch instruction.
2054///
2055class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00002056 /// Ops list - Branches are strange. The operands are ordered:
Gabor Greifae5a20a2009-03-12 18:34:49 +00002057 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2058 /// they don't have to check for cond/uncond branchness. These are mostly
2059 /// accessed relative from op_end().
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002060 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00002061 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002062 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2063 // BranchInst(BB *B) - 'br B'
2064 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2065 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2066 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2067 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2068 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00002069 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002070 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002071 Instruction *InsertBefore = 0);
2072 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002073 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002074 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002075protected:
2076 virtual BranchInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002077public:
2078 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
Jay Foad8e3914d2011-01-07 20:29:02 +00002079 return new(1) BranchInst(IfTrue, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002080 }
Gabor Greifefe65362008-05-10 08:32:32 +00002081 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2082 Value *Cond, Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002083 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2084 }
2085 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
Jay Foad8e3914d2011-01-07 20:29:02 +00002086 return new(1) BranchInst(IfTrue, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002087 }
Gabor Greifefe65362008-05-10 08:32:32 +00002088 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2089 Value *Cond, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002090 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2091 }
Chris Lattner454928e2005-01-29 00:31:36 +00002092
2093 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00002094 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002095
Devang Patel4d4a5e02008-02-23 01:11:02 +00002096 bool isUnconditional() const { return getNumOperands() == 1; }
2097 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002098
Devang Patel4d4a5e02008-02-23 01:11:02 +00002099 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002100 assert(isConditional() && "Cannot get condition of an uncond branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002101 return Op<-3>();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002102 }
2103
2104 void setCondition(Value *V) {
2105 assert(isConditional() && "Cannot set condition of unconditional branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002106 Op<-3>() = V;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002107 }
2108
Chris Lattner454928e2005-01-29 00:31:36 +00002109 unsigned getNumSuccessors() const { return 1+isConditional(); }
2110
2111 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002112 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002113 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002114 }
2115
Chris Lattner454928e2005-01-29 00:31:36 +00002116 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002117 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner4b122932009-10-27 16:49:53 +00002118 *(&Op<-1>() - idx) = (Value*)NewSucc;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002119 }
2120
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002121 // Methods for support type inquiry through isa, cast, and dyn_cast:
2122 static inline bool classof(const BranchInst *) { return true; }
2123 static inline bool classof(const Instruction *I) {
2124 return (I->getOpcode() == Instruction::Br);
2125 }
2126 static inline bool classof(const Value *V) {
2127 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2128 }
Chris Lattner454928e2005-01-29 00:31:36 +00002129private:
2130 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2131 virtual unsigned getNumSuccessorsV() const;
2132 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002133};
2134
Gabor Greifefe65362008-05-10 08:32:32 +00002135template <>
Jay Foad67c619b2011-01-11 15:07:38 +00002136struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2137};
Gabor Greifefe65362008-05-10 08:32:32 +00002138
2139DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2140
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002141//===----------------------------------------------------------------------===//
2142// SwitchInst Class
2143//===----------------------------------------------------------------------===//
2144
2145//===---------------------------------------------------------------------------
2146/// SwitchInst - Multiway switch
2147///
2148class SwitchInst : public TerminatorInst {
Gabor Greifefe65362008-05-10 08:32:32 +00002149 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00002150 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002151 // Operand[0] = Value to switch on
2152 // Operand[1] = Default basic block destination
2153 // Operand[2n ] = Value to match
2154 // Operand[2n+1] = BasicBlock to go to on match
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002155 SwitchInst(const SwitchInst &SI);
Chris Lattneraa6e3502010-11-17 05:41:46 +00002156 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
Chris Lattner454928e2005-01-29 00:31:36 +00002157 void resizeOperands(unsigned No);
Gabor Greifefe65362008-05-10 08:32:32 +00002158 // allocate space for exactly zero operands
2159 void *operator new(size_t s) {
2160 return User::operator new(s, 0);
2161 }
Chris Lattner454928e2005-01-29 00:31:36 +00002162 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2163 /// switch on and a default destination. The number of additional cases can
2164 /// be specified here to make memory allocation more efficient. This
2165 /// constructor can also autoinsert before another instruction.
2166 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002167 Instruction *InsertBefore);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002168
Chris Lattner454928e2005-01-29 00:31:36 +00002169 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2170 /// switch on and a default destination. The number of additional cases can
2171 /// be specified here to make memory allocation more efficient. This
2172 /// constructor also autoinserts at the end of the specified BasicBlock.
2173 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002174 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002175protected:
2176 virtual SwitchInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002177public:
Gabor Greifefe65362008-05-10 08:32:32 +00002178 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2179 unsigned NumCases, Instruction *InsertBefore = 0) {
2180 return new SwitchInst(Value, Default, NumCases, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002181 }
Gabor Greifefe65362008-05-10 08:32:32 +00002182 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2183 unsigned NumCases, BasicBlock *InsertAtEnd) {
2184 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002185 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002186 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00002187
Gabor Greifefe65362008-05-10 08:32:32 +00002188 /// Provide fast operand accessors
2189 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2190
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002191 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00002192 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00002193 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00002194
Devang Patel4d4a5e02008-02-23 01:11:02 +00002195 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002196 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002197 }
2198
2199 /// getNumCases - return the number of 'cases' in this switch instruction.
2200 /// Note that case #0 is always the default case.
2201 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002202 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002203 }
2204
2205 /// getCaseValue - Return the specified case value. Note that case #0, the
2206 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002207 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002208 assert(i && i < getNumCases() && "Illegal case value to get!");
2209 return getSuccessorValue(i);
2210 }
2211
2212 /// getCaseValue - Return the specified case value. Note that case #0, the
2213 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002214 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002215 assert(i && i < getNumCases() && "Illegal case value to get!");
2216 return getSuccessorValue(i);
2217 }
2218
2219 /// findCaseValue - Search all of the case values for the specified constant.
2220 /// If it is explicitly handled, return the case number of it, otherwise
2221 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002222 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002223 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2224 if (getCaseValue(i) == C)
2225 return i;
2226 return 0;
2227 }
2228
Nick Lewycky011f1842006-09-18 19:03:59 +00002229 /// findCaseDest - Finds the unique case value for a given successor. Returns
2230 /// null if the successor is not found, not unique, or is the default case.
2231 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00002232 if (BB == getDefaultDest()) return NULL;
2233
Nick Lewycky011f1842006-09-18 19:03:59 +00002234 ConstantInt *CI = NULL;
2235 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2236 if (getSuccessor(i) == BB) {
2237 if (CI) return NULL; // Multiple cases lead to BB.
2238 else CI = getCaseValue(i);
2239 }
2240 }
2241 return CI;
2242 }
2243
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002244 /// addCase - Add an entry to the switch instruction...
2245 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00002246 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002247
2248 /// removeCase - This method removes the specified successor from the switch
2249 /// instruction. Note that this cannot be used to remove the default
Jay Foad0faa6092011-02-01 09:22:34 +00002250 /// destination (successor #0). Also note that this operation may reorder the
2251 /// remaining cases at index idx and above.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002252 ///
2253 void removeCase(unsigned idx);
2254
Chris Lattner454928e2005-01-29 00:31:36 +00002255 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2256 BasicBlock *getSuccessor(unsigned idx) const {
2257 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2258 return cast<BasicBlock>(getOperand(idx*2+1));
2259 }
2260 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002261 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Chris Lattner4b122932009-10-27 16:49:53 +00002262 setOperand(idx*2+1, (Value*)NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002263 }
2264
2265 // getSuccessorValue - Return the value associated with the specified
2266 // successor.
Devang Patel4d4a5e02008-02-23 01:11:02 +00002267 ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002268 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00002269 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002270 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002271
2272 // Methods for support type inquiry through isa, cast, and dyn_cast:
2273 static inline bool classof(const SwitchInst *) { return true; }
2274 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00002275 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002276 }
2277 static inline bool classof(const Value *V) {
2278 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2279 }
Chris Lattner454928e2005-01-29 00:31:36 +00002280private:
2281 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2282 virtual unsigned getNumSuccessorsV() const;
2283 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002284};
2285
Gabor Greifefe65362008-05-10 08:32:32 +00002286template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002287struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00002288};
2289
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002290DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002291
2292
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002293//===----------------------------------------------------------------------===//
Chris Lattnerab21db72009-10-28 00:19:10 +00002294// IndirectBrInst Class
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002295//===----------------------------------------------------------------------===//
2296
2297//===---------------------------------------------------------------------------
Chris Lattnerab21db72009-10-28 00:19:10 +00002298/// IndirectBrInst - Indirect Branch Instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002299///
Chris Lattnerab21db72009-10-28 00:19:10 +00002300class IndirectBrInst : public TerminatorInst {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002301 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2302 unsigned ReservedSpace;
2303 // Operand[0] = Value to switch on
2304 // Operand[1] = Default basic block destination
2305 // Operand[2n ] = Value to match
2306 // Operand[2n+1] = BasicBlock to go to on match
Chris Lattnerab21db72009-10-28 00:19:10 +00002307 IndirectBrInst(const IndirectBrInst &IBI);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002308 void init(Value *Address, unsigned NumDests);
2309 void resizeOperands(unsigned No);
2310 // allocate space for exactly zero operands
2311 void *operator new(size_t s) {
2312 return User::operator new(s, 0);
2313 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002314 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2315 /// Address to jump to. The number of expected destinations can be specified
2316 /// here to make memory allocation more efficient. This constructor can also
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002317 /// autoinsert before another instruction.
Chris Lattnerab21db72009-10-28 00:19:10 +00002318 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002319
Chris Lattnerab21db72009-10-28 00:19:10 +00002320 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2321 /// Address to jump to. The number of expected destinations can be specified
2322 /// here to make memory allocation more efficient. This constructor also
2323 /// autoinserts at the end of the specified BasicBlock.
2324 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002325protected:
Chris Lattnerab21db72009-10-28 00:19:10 +00002326 virtual IndirectBrInst *clone_impl() const;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002327public:
Chris Lattnerab21db72009-10-28 00:19:10 +00002328 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2329 Instruction *InsertBefore = 0) {
2330 return new IndirectBrInst(Address, NumDests, InsertBefore);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002331 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002332 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2333 BasicBlock *InsertAtEnd) {
2334 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002335 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002336 ~IndirectBrInst();
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002337
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002338 /// Provide fast operand accessors.
2339 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002340
Chris Lattnerab21db72009-10-28 00:19:10 +00002341 // Accessor Methods for IndirectBrInst instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002342 Value *getAddress() { return getOperand(0); }
2343 const Value *getAddress() const { return getOperand(0); }
2344 void setAddress(Value *V) { setOperand(0, V); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002345
2346
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002347 /// getNumDestinations - return the number of possible destinations in this
Chris Lattnerab21db72009-10-28 00:19:10 +00002348 /// indirectbr instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002349 unsigned getNumDestinations() const { return getNumOperands()-1; }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002350
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002351 /// getDestination - Return the specified destination.
2352 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2353 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002354
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002355 /// addDestination - Add a destination.
2356 ///
2357 void addDestination(BasicBlock *Dest);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002358
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002359 /// removeDestination - This method removes the specified successor from the
Chris Lattnerab21db72009-10-28 00:19:10 +00002360 /// indirectbr instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002361 void removeDestination(unsigned i);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002362
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002363 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2364 BasicBlock *getSuccessor(unsigned i) const {
2365 return cast<BasicBlock>(getOperand(i+1));
2366 }
2367 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2368 setOperand(i+1, (Value*)NewSucc);
2369 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002370
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002371 // Methods for support type inquiry through isa, cast, and dyn_cast:
Chris Lattnerab21db72009-10-28 00:19:10 +00002372 static inline bool classof(const IndirectBrInst *) { return true; }
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002373 static inline bool classof(const Instruction *I) {
Chris Lattnerab21db72009-10-28 00:19:10 +00002374 return I->getOpcode() == Instruction::IndirectBr;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002375 }
2376 static inline bool classof(const Value *V) {
2377 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2378 }
2379private:
2380 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2381 virtual unsigned getNumSuccessorsV() const;
2382 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2383};
2384
2385template <>
Chris Lattnerab21db72009-10-28 00:19:10 +00002386struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002387};
2388
Chris Lattnerab21db72009-10-28 00:19:10 +00002389DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002390
2391
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002392//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002393// InvokeInst Class
2394//===----------------------------------------------------------------------===//
2395
Chris Lattner3340ffe2005-05-06 20:26:26 +00002396/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2397/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002398///
2399class InvokeInst : public TerminatorInst {
Devang Patel05988662008-09-25 21:00:45 +00002400 AttrListPtr AttributeList;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002401 InvokeInst(const InvokeInst &BI);
2402 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerd2dd1502007-02-13 01:04:01 +00002403 Value* const *Args, unsigned NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00002404
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002405 template<typename RandomAccessIterator>
David Greenef1355a52007-08-27 19:04:21 +00002406 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002407 RandomAccessIterator ArgBegin, RandomAccessIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002408 const Twine &NameStr,
David Greenef1355a52007-08-27 19:04:21 +00002409 // This argument ensures that we have an iterator we can
2410 // do arithmetic on in constant time
2411 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002412 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002413
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002414 // This requires that the iterator points to contiguous memory.
2415 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
Evan Cheng1bf9a182008-07-24 00:08:56 +00002416 setName(NameStr);
David Greenef1355a52007-08-27 19:04:21 +00002417 }
2418
David Greenef1355a52007-08-27 19:04:21 +00002419 /// Construct an InvokeInst given a range of arguments.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002420 /// RandomAccessIterator must be a random-access iterator pointing to
David Greenef1355a52007-08-27 19:04:21 +00002421 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2422 /// made for random-accessness but not for contiguous storage as
2423 /// that would incur runtime overhead.
2424 ///
2425 /// @brief Construct an InvokeInst from a range of arguments
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002426 template<typename RandomAccessIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002427 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002428 RandomAccessIterator ArgBegin, RandomAccessIterator ArgEnd,
Gabor Greifefe65362008-05-10 08:32:32 +00002429 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002430 const Twine &NameStr, Instruction *InsertBefore);
David Greenef1355a52007-08-27 19:04:21 +00002431
2432 /// Construct an InvokeInst given a range of arguments.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002433 /// RandomAccessIterator must be a random-access iterator pointing to
David Greenef1355a52007-08-27 19:04:21 +00002434 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2435 /// made for random-accessness but not for contiguous storage as
2436 /// that would incur runtime overhead.
2437 ///
2438 /// @brief Construct an InvokeInst from a range of arguments
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002439 template<typename RandomAccessIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002440 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002441 RandomAccessIterator ArgBegin, RandomAccessIterator ArgEnd,
Gabor Greifefe65362008-05-10 08:32:32 +00002442 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002443 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002444protected:
2445 virtual InvokeInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002446public:
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002447 template<typename RandomAccessIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002448 static InvokeInst *Create(Value *Func,
2449 BasicBlock *IfNormal, BasicBlock *IfException,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002450 RandomAccessIterator ArgBegin,
2451 RandomAccessIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002452 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00002453 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +00002454 unsigned Values(ArgEnd - ArgBegin + 3);
2455 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002456 Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002457 }
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002458 template<typename RandomAccessIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002459 static InvokeInst *Create(Value *Func,
2460 BasicBlock *IfNormal, BasicBlock *IfException,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002461 RandomAccessIterator ArgBegin,
2462 RandomAccessIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002463 const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002464 BasicBlock *InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00002465 unsigned Values(ArgEnd - ArgBegin + 3);
2466 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002467 Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002468 }
David Greenef1355a52007-08-27 19:04:21 +00002469
Gabor Greifefe65362008-05-10 08:32:32 +00002470 /// Provide fast operand accessors
2471 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002472
Gabor Greif0114b992010-07-31 08:35:21 +00002473 /// getNumArgOperands - Return the number of invoke arguments.
2474 ///
Bill Wendling22a5b292010-06-07 19:05:06 +00002475 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
Gabor Greif0114b992010-07-31 08:35:21 +00002476
2477 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2478 ///
Bill Wendling22a5b292010-06-07 19:05:06 +00002479 Value *getArgOperand(unsigned i) const { return getOperand(i); }
Gabor Greif710ac072010-06-28 12:23:36 +00002480 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
Bill Wendling22a5b292010-06-07 19:05:06 +00002481
Chris Lattner3340ffe2005-05-06 20:26:26 +00002482 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2483 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002484 CallingConv::ID getCallingConv() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +00002485 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002486 }
2487 void setCallingConv(CallingConv::ID CC) {
Chris Lattnerb2406d92009-12-29 02:46:09 +00002488 setInstructionSubclassData(static_cast<unsigned>(CC));
Chris Lattner3340ffe2005-05-06 20:26:26 +00002489 }
2490
Devang Patel05988662008-09-25 21:00:45 +00002491 /// getAttributes - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002492 ///
Devang Patel05988662008-09-25 21:00:45 +00002493 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002494
Devang Patel05988662008-09-25 21:00:45 +00002495 /// setAttributes - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002496 ///
Devang Patel05988662008-09-25 21:00:45 +00002497 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00002498
Devang Patel05988662008-09-25 21:00:45 +00002499 /// addAttribute - adds the attribute to the list of attributes.
2500 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002501
Devang Patel05988662008-09-25 21:00:45 +00002502 /// removeAttribute - removes the attribute from the list of attributes.
2503 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00002504
Dan Gohmanf2752502008-09-26 21:38:45 +00002505 /// @brief Determine whether the call or the callee has the given attribute.
2506 bool paramHasAttr(unsigned i, Attributes attr) const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002507
Dale Johannesen08e78b12008-02-22 17:49:45 +00002508 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002509 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00002510 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002511 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00002512
Eric Christopherf27e6082010-03-25 04:49:10 +00002513 /// @brief Return true if the call should not be inlined.
2514 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
Nick Lewyckyb9933b82010-07-06 03:53:22 +00002515 void setIsNoInline(bool Value = true) {
Eric Christopherf27e6082010-03-25 04:49:10 +00002516 if (Value) addAttribute(~0, Attribute::NoInline);
2517 else removeAttribute(~0, Attribute::NoInline);
2518 }
Nick Lewyckyb9933b82010-07-06 03:53:22 +00002519
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002520 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002521 bool doesNotAccessMemory() const {
Dan Gohmana62b5ed2009-07-17 16:12:36 +00002522 return paramHasAttr(~0, Attribute::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002523 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002524 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002525 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2526 else removeAttribute(~0, Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00002527 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002528
2529 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002530 bool onlyReadsMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00002531 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002532 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002533 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002534 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2535 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00002536 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002537
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002538 /// @brief Determine if the call cannot return.
Nick Lewyckyb9933b82010-07-06 03:53:22 +00002539 bool doesNotReturn() const { return paramHasAttr(~0, Attribute::NoReturn); }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002540 void setDoesNotReturn(bool DoesNotReturn = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002541 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2542 else removeAttribute(~0, Attribute::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00002543 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002544
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002545 /// @brief Determine if the call cannot unwind.
Nick Lewyckyb9933b82010-07-06 03:53:22 +00002546 bool doesNotThrow() const { return paramHasAttr(~0, Attribute::NoUnwind); }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002547 void setDoesNotThrow(bool DoesNotThrow = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002548 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2549 else removeAttribute(~0, Attribute::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00002550 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002551
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002552 /// @brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00002553 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002554 bool hasStructRetAttr() const {
2555 // Be friendly and also check the callee.
Devang Patel05988662008-09-25 21:00:45 +00002556 return paramHasAttr(1, Attribute::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002557 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002558
Dan Gohmanf2752502008-09-26 21:38:45 +00002559 /// @brief Determine if any call argument is an aggregate passed by value.
2560 bool hasByValArgument() const {
2561 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2562 }
2563
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002564 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00002565 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002566 ///
Chris Lattner721aef62004-11-18 17:46:57 +00002567 Function *getCalledFunction() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00002568 return dyn_cast<Function>(Op<-3>());
Chris Lattner721aef62004-11-18 17:46:57 +00002569 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002570
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002571 /// getCalledValue - Get a pointer to the function that is invoked by this
Dan Gohmanf2752502008-09-26 21:38:45 +00002572 /// instruction
Gabor Greifc9f75002010-03-24 13:21:49 +00002573 const Value *getCalledValue() const { return Op<-3>(); }
2574 Value *getCalledValue() { return Op<-3>(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002575
Gabor Greif654c06f2010-03-20 21:00:25 +00002576 /// setCalledFunction - Set the function called.
2577 void setCalledFunction(Value* Fn) {
Gabor Greifc9f75002010-03-24 13:21:49 +00002578 Op<-3>() = Fn;
Gabor Greif654c06f2010-03-20 21:00:25 +00002579 }
2580
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002581 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00002582 BasicBlock *getNormalDest() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00002583 return cast<BasicBlock>(Op<-2>());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002584 }
Chris Lattner454928e2005-01-29 00:31:36 +00002585 BasicBlock *getUnwindDest() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00002586 return cast<BasicBlock>(Op<-1>());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002587 }
Chris Lattner454928e2005-01-29 00:31:36 +00002588 void setNormalDest(BasicBlock *B) {
Gabor Greifc9f75002010-03-24 13:21:49 +00002589 Op<-2>() = reinterpret_cast<Value*>(B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002590 }
Chris Lattner454928e2005-01-29 00:31:36 +00002591 void setUnwindDest(BasicBlock *B) {
Gabor Greifc9f75002010-03-24 13:21:49 +00002592 Op<-1>() = reinterpret_cast<Value*>(B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002593 }
2594
Devang Patel4d4a5e02008-02-23 01:11:02 +00002595 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002596 assert(i < 2 && "Successor # out of range for invoke!");
2597 return i == 0 ? getNormalDest() : getUnwindDest();
2598 }
2599
Chris Lattner454928e2005-01-29 00:31:36 +00002600 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002601 assert(idx < 2 && "Successor # out of range for invoke!");
Gabor Greifc9f75002010-03-24 13:21:49 +00002602 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002603 }
2604
Chris Lattner454928e2005-01-29 00:31:36 +00002605 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002606
2607 // Methods for support type inquiry through isa, cast, and dyn_cast:
2608 static inline bool classof(const InvokeInst *) { return true; }
2609 static inline bool classof(const Instruction *I) {
2610 return (I->getOpcode() == Instruction::Invoke);
2611 }
2612 static inline bool classof(const Value *V) {
2613 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2614 }
Gabor Greifc9f75002010-03-24 13:21:49 +00002615
Chris Lattner454928e2005-01-29 00:31:36 +00002616private:
2617 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2618 virtual unsigned getNumSuccessorsV() const;
2619 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00002620
Chris Lattnerb2406d92009-12-29 02:46:09 +00002621 // Shadow Instruction::setInstructionSubclassData with a private forwarding
2622 // method so that subclasses cannot accidentally use it.
2623 void setInstructionSubclassData(unsigned short D) {
2624 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00002625 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002626};
2627
Gabor Greifefe65362008-05-10 08:32:32 +00002628template <>
Jay Foad67c619b2011-01-11 15:07:38 +00002629struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00002630};
2631
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002632template<typename RandomAccessIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002633InvokeInst::InvokeInst(Value *Func,
2634 BasicBlock *IfNormal, BasicBlock *IfException,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002635 RandomAccessIterator ArgBegin,
2636 RandomAccessIterator ArgEnd,
Gabor Greifefe65362008-05-10 08:32:32 +00002637 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002638 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00002639 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2640 ->getElementType())->getReturnType(),
2641 Instruction::Invoke,
2642 OperandTraits<InvokeInst>::op_end(this) - Values,
2643 Values, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002644 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002645 typename std::iterator_traits<RandomAccessIterator>
2646 ::iterator_category());
Gabor Greifefe65362008-05-10 08:32:32 +00002647}
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002648template<typename RandomAccessIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002649InvokeInst::InvokeInst(Value *Func,
2650 BasicBlock *IfNormal, BasicBlock *IfException,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002651 RandomAccessIterator ArgBegin,
2652 RandomAccessIterator ArgEnd,
Gabor Greifefe65362008-05-10 08:32:32 +00002653 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002654 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00002655 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2656 ->getElementType())->getReturnType(),
2657 Instruction::Invoke,
2658 OperandTraits<InvokeInst>::op_end(this) - Values,
2659 Values, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002660 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00002661 typename std::iterator_traits<RandomAccessIterator>
2662 ::iterator_category());
Gabor Greifefe65362008-05-10 08:32:32 +00002663}
2664
2665DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002666
2667//===----------------------------------------------------------------------===//
2668// UnwindInst Class
2669//===----------------------------------------------------------------------===//
2670
2671//===---------------------------------------------------------------------------
2672/// UnwindInst - Immediately exit the current function, unwinding the stack
2673/// until an invoke instruction is found.
2674///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002675class UnwindInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002676 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Devang Patel50b6e332009-10-27 22:16:29 +00002677protected:
2678 virtual UnwindInst *clone_impl() const;
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002679public:
Gabor Greif051a9502008-04-06 20:25:17 +00002680 // allocate space for exactly zero operands
2681 void *operator new(size_t s) {
2682 return User::operator new(s, 0);
2683 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002684 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2685 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002686
Chris Lattner454928e2005-01-29 00:31:36 +00002687 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002688
2689 // Methods for support type inquiry through isa, cast, and dyn_cast:
2690 static inline bool classof(const UnwindInst *) { return true; }
2691 static inline bool classof(const Instruction *I) {
2692 return I->getOpcode() == Instruction::Unwind;
2693 }
2694 static inline bool classof(const Value *V) {
2695 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2696 }
Chris Lattner454928e2005-01-29 00:31:36 +00002697private:
2698 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2699 virtual unsigned getNumSuccessorsV() const;
2700 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002701};
2702
Chris Lattner076b3f12004-10-16 18:05:54 +00002703//===----------------------------------------------------------------------===//
2704// UnreachableInst Class
2705//===----------------------------------------------------------------------===//
2706
2707//===---------------------------------------------------------------------------
2708/// UnreachableInst - This function has undefined behavior. In particular, the
2709/// presence of this instruction indicates some higher level knowledge that the
2710/// end of the block cannot be reached.
2711///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002712class UnreachableInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002713 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Devang Patel50b6e332009-10-27 22:16:29 +00002714protected:
2715 virtual UnreachableInst *clone_impl() const;
2716
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002717public:
Gabor Greif051a9502008-04-06 20:25:17 +00002718 // allocate space for exactly zero operands
2719 void *operator new(size_t s) {
2720 return User::operator new(s, 0);
2721 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002722 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2723 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00002724
Chris Lattner454928e2005-01-29 00:31:36 +00002725 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00002726
2727 // Methods for support type inquiry through isa, cast, and dyn_cast:
2728 static inline bool classof(const UnreachableInst *) { return true; }
2729 static inline bool classof(const Instruction *I) {
2730 return I->getOpcode() == Instruction::Unreachable;
2731 }
2732 static inline bool classof(const Value *V) {
2733 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2734 }
Chris Lattner454928e2005-01-29 00:31:36 +00002735private:
2736 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2737 virtual unsigned getNumSuccessorsV() const;
2738 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00002739};
2740
Reid Spencer3da59db2006-11-27 01:05:10 +00002741//===----------------------------------------------------------------------===//
2742// TruncInst Class
2743//===----------------------------------------------------------------------===//
2744
2745/// @brief This class represents a truncation of integer types.
2746class TruncInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002747protected:
2748 /// @brief Clone an identical TruncInst
2749 virtual TruncInst *clone_impl() const;
2750
Reid Spencer3da59db2006-11-27 01:05:10 +00002751public:
2752 /// @brief Constructor with insert-before-instruction semantics
2753 TruncInst(
2754 Value *S, ///< The value to be truncated
2755 const Type *Ty, ///< The (smaller) type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00002756 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002757 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2758 );
2759
2760 /// @brief Constructor with insert-at-end-of-block semantics
2761 TruncInst(
2762 Value *S, ///< The value to be truncated
2763 const Type *Ty, ///< The (smaller) type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00002764 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002765 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2766 );
2767
Reid Spencer3da59db2006-11-27 01:05:10 +00002768 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2769 static inline bool classof(const TruncInst *) { return true; }
2770 static inline bool classof(const Instruction *I) {
2771 return I->getOpcode() == Trunc;
2772 }
2773 static inline bool classof(const Value *V) {
2774 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2775 }
2776};
2777
2778//===----------------------------------------------------------------------===//
2779// ZExtInst Class
2780//===----------------------------------------------------------------------===//
2781
2782/// @brief This class represents zero extension of integer types.
2783class ZExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002784protected:
2785 /// @brief Clone an identical ZExtInst
2786 virtual ZExtInst *clone_impl() const;
2787
Reid Spencer3da59db2006-11-27 01:05:10 +00002788public:
2789 /// @brief Constructor with insert-before-instruction semantics
2790 ZExtInst(
2791 Value *S, ///< The value to be zero extended
2792 const Type *Ty, ///< The type to zero extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00002793 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002794 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2795 );
2796
2797 /// @brief Constructor with insert-at-end semantics.
2798 ZExtInst(
2799 Value *S, ///< The value to be zero extended
2800 const Type *Ty, ///< The type to zero extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00002801 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002802 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2803 );
2804
Reid Spencer3da59db2006-11-27 01:05:10 +00002805 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2806 static inline bool classof(const ZExtInst *) { return true; }
2807 static inline bool classof(const Instruction *I) {
2808 return I->getOpcode() == ZExt;
2809 }
2810 static inline bool classof(const Value *V) {
2811 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2812 }
2813};
2814
2815//===----------------------------------------------------------------------===//
2816// SExtInst Class
2817//===----------------------------------------------------------------------===//
2818
2819/// @brief This class represents a sign extension of integer types.
2820class SExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002821protected:
2822 /// @brief Clone an identical SExtInst
2823 virtual SExtInst *clone_impl() const;
2824
Reid Spencer3da59db2006-11-27 01:05:10 +00002825public:
2826 /// @brief Constructor with insert-before-instruction semantics
2827 SExtInst(
2828 Value *S, ///< The value to be sign extended
2829 const Type *Ty, ///< The type to sign extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00002830 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002831 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2832 );
2833
2834 /// @brief Constructor with insert-at-end-of-block semantics
2835 SExtInst(
2836 Value *S, ///< The value to be sign extended
2837 const Type *Ty, ///< The type to sign extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00002838 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002839 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2840 );
2841
Reid Spencer3da59db2006-11-27 01:05:10 +00002842 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2843 static inline bool classof(const SExtInst *) { return true; }
2844 static inline bool classof(const Instruction *I) {
2845 return I->getOpcode() == SExt;
2846 }
2847 static inline bool classof(const Value *V) {
2848 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2849 }
2850};
2851
2852//===----------------------------------------------------------------------===//
2853// FPTruncInst Class
2854//===----------------------------------------------------------------------===//
2855
2856/// @brief This class represents a truncation of floating point types.
2857class FPTruncInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002858protected:
2859 /// @brief Clone an identical FPTruncInst
2860 virtual FPTruncInst *clone_impl() const;
2861
Reid Spencer3da59db2006-11-27 01:05:10 +00002862public:
2863 /// @brief Constructor with insert-before-instruction semantics
2864 FPTruncInst(
2865 Value *S, ///< The value to be truncated
2866 const Type *Ty, ///< The type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00002867 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002868 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2869 );
2870
2871 /// @brief Constructor with insert-before-instruction semantics
2872 FPTruncInst(
2873 Value *S, ///< The value to be truncated
2874 const Type *Ty, ///< The type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00002875 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002876 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2877 );
2878
Reid Spencer3da59db2006-11-27 01:05:10 +00002879 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2880 static inline bool classof(const FPTruncInst *) { return true; }
2881 static inline bool classof(const Instruction *I) {
2882 return I->getOpcode() == FPTrunc;
2883 }
2884 static inline bool classof(const Value *V) {
2885 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2886 }
2887};
2888
2889//===----------------------------------------------------------------------===//
2890// FPExtInst Class
2891//===----------------------------------------------------------------------===//
2892
2893/// @brief This class represents an extension of floating point types.
2894class FPExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002895protected:
2896 /// @brief Clone an identical FPExtInst
2897 virtual FPExtInst *clone_impl() const;
2898
Reid Spencer3da59db2006-11-27 01:05:10 +00002899public:
2900 /// @brief Constructor with insert-before-instruction semantics
2901 FPExtInst(
2902 Value *S, ///< The value to be extended
2903 const Type *Ty, ///< The type to extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00002904 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002905 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2906 );
2907
2908 /// @brief Constructor with insert-at-end-of-block semantics
2909 FPExtInst(
2910 Value *S, ///< The value to be extended
2911 const Type *Ty, ///< The type to extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00002912 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002913 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2914 );
2915
Reid Spencer3da59db2006-11-27 01:05:10 +00002916 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2917 static inline bool classof(const FPExtInst *) { return true; }
2918 static inline bool classof(const Instruction *I) {
2919 return I->getOpcode() == FPExt;
2920 }
2921 static inline bool classof(const Value *V) {
2922 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2923 }
2924};
2925
2926//===----------------------------------------------------------------------===//
2927// UIToFPInst Class
2928//===----------------------------------------------------------------------===//
2929
2930/// @brief This class represents a cast unsigned integer to floating point.
2931class UIToFPInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002932protected:
2933 /// @brief Clone an identical UIToFPInst
2934 virtual UIToFPInst *clone_impl() const;
2935
Reid Spencer3da59db2006-11-27 01:05:10 +00002936public:
2937 /// @brief Constructor with insert-before-instruction semantics
2938 UIToFPInst(
2939 Value *S, ///< The value to be converted
2940 const Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00002941 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002942 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2943 );
2944
2945 /// @brief Constructor with insert-at-end-of-block semantics
2946 UIToFPInst(
2947 Value *S, ///< The value to be converted
2948 const Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00002949 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002950 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2951 );
2952
Reid Spencer3da59db2006-11-27 01:05:10 +00002953 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2954 static inline bool classof(const UIToFPInst *) { return true; }
2955 static inline bool classof(const Instruction *I) {
2956 return I->getOpcode() == UIToFP;
2957 }
2958 static inline bool classof(const Value *V) {
2959 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2960 }
2961};
2962
2963//===----------------------------------------------------------------------===//
2964// SIToFPInst Class
2965//===----------------------------------------------------------------------===//
2966
2967/// @brief This class represents a cast from signed integer to floating point.
2968class SIToFPInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002969protected:
2970 /// @brief Clone an identical SIToFPInst
2971 virtual SIToFPInst *clone_impl() const;
2972
Reid Spencer3da59db2006-11-27 01:05:10 +00002973public:
2974 /// @brief Constructor with insert-before-instruction semantics
2975 SIToFPInst(
2976 Value *S, ///< The value to be converted
2977 const Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00002978 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002979 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2980 );
2981
2982 /// @brief Constructor with insert-at-end-of-block semantics
2983 SIToFPInst(
2984 Value *S, ///< The value to be converted
2985 const Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00002986 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002987 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2988 );
2989
Reid Spencer3da59db2006-11-27 01:05:10 +00002990 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2991 static inline bool classof(const SIToFPInst *) { return true; }
2992 static inline bool classof(const Instruction *I) {
2993 return I->getOpcode() == SIToFP;
2994 }
2995 static inline bool classof(const Value *V) {
2996 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2997 }
2998};
2999
3000//===----------------------------------------------------------------------===//
3001// FPToUIInst Class
3002//===----------------------------------------------------------------------===//
3003
3004/// @brief This class represents a cast from floating point to unsigned integer
3005class FPToUIInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003006protected:
3007 /// @brief Clone an identical FPToUIInst
3008 virtual FPToUIInst *clone_impl() const;
3009
Reid Spencer3da59db2006-11-27 01:05:10 +00003010public:
3011 /// @brief Constructor with insert-before-instruction semantics
3012 FPToUIInst(
3013 Value *S, ///< The value to be converted
3014 const Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003015 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003016 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3017 );
3018
3019 /// @brief Constructor with insert-at-end-of-block semantics
3020 FPToUIInst(
3021 Value *S, ///< The value to be converted
3022 const Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003023 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003024 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3025 );
3026
Reid Spencer3da59db2006-11-27 01:05:10 +00003027 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3028 static inline bool classof(const FPToUIInst *) { return true; }
3029 static inline bool classof(const Instruction *I) {
3030 return I->getOpcode() == FPToUI;
3031 }
3032 static inline bool classof(const Value *V) {
3033 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3034 }
3035};
3036
3037//===----------------------------------------------------------------------===//
3038// FPToSIInst Class
3039//===----------------------------------------------------------------------===//
3040
3041/// @brief This class represents a cast from floating point to signed integer.
3042class FPToSIInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003043protected:
3044 /// @brief Clone an identical FPToSIInst
3045 virtual FPToSIInst *clone_impl() const;
3046
Reid Spencer3da59db2006-11-27 01:05:10 +00003047public:
3048 /// @brief Constructor with insert-before-instruction semantics
3049 FPToSIInst(
3050 Value *S, ///< The value to be converted
3051 const Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003052 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003053 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3054 );
3055
3056 /// @brief Constructor with insert-at-end-of-block semantics
3057 FPToSIInst(
3058 Value *S, ///< The value to be converted
3059 const Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003060 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003061 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3062 );
3063
Reid Spencer3da59db2006-11-27 01:05:10 +00003064 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3065 static inline bool classof(const FPToSIInst *) { return true; }
3066 static inline bool classof(const Instruction *I) {
3067 return I->getOpcode() == FPToSI;
3068 }
3069 static inline bool classof(const Value *V) {
3070 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3071 }
3072};
3073
3074//===----------------------------------------------------------------------===//
3075// IntToPtrInst Class
3076//===----------------------------------------------------------------------===//
3077
3078/// @brief This class represents a cast from an integer to a pointer.
3079class IntToPtrInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00003080public:
3081 /// @brief Constructor with insert-before-instruction semantics
3082 IntToPtrInst(
3083 Value *S, ///< The value to be converted
3084 const Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003085 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003086 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3087 );
3088
3089 /// @brief Constructor with insert-at-end-of-block semantics
3090 IntToPtrInst(
3091 Value *S, ///< The value to be converted
3092 const Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003093 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003094 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3095 );
3096
3097 /// @brief Clone an identical IntToPtrInst
Devang Patel50b6e332009-10-27 22:16:29 +00003098 virtual IntToPtrInst *clone_impl() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00003099
3100 // Methods for support type inquiry through isa, cast, and dyn_cast:
3101 static inline bool classof(const IntToPtrInst *) { return true; }
3102 static inline bool classof(const Instruction *I) {
3103 return I->getOpcode() == IntToPtr;
3104 }
3105 static inline bool classof(const Value *V) {
3106 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3107 }
3108};
3109
3110//===----------------------------------------------------------------------===//
3111// PtrToIntInst Class
3112//===----------------------------------------------------------------------===//
3113
3114/// @brief This class represents a cast from a pointer to an integer
3115class PtrToIntInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003116protected:
3117 /// @brief Clone an identical PtrToIntInst
3118 virtual PtrToIntInst *clone_impl() const;
3119
Reid Spencer3da59db2006-11-27 01:05:10 +00003120public:
3121 /// @brief Constructor with insert-before-instruction semantics
3122 PtrToIntInst(
3123 Value *S, ///< The value to be converted
3124 const Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003125 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003126 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3127 );
3128
3129 /// @brief Constructor with insert-at-end-of-block semantics
3130 PtrToIntInst(
3131 Value *S, ///< The value to be converted
3132 const Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003133 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003134 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3135 );
3136
Reid Spencer3da59db2006-11-27 01:05:10 +00003137 // Methods for support type inquiry through isa, cast, and dyn_cast:
3138 static inline bool classof(const PtrToIntInst *) { return true; }
3139 static inline bool classof(const Instruction *I) {
3140 return I->getOpcode() == PtrToInt;
3141 }
3142 static inline bool classof(const Value *V) {
3143 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3144 }
3145};
3146
3147//===----------------------------------------------------------------------===//
3148// BitCastInst Class
3149//===----------------------------------------------------------------------===//
3150
3151/// @brief This class represents a no-op cast from one type to another.
3152class BitCastInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003153protected:
3154 /// @brief Clone an identical BitCastInst
3155 virtual BitCastInst *clone_impl() const;
3156
Reid Spencer3da59db2006-11-27 01:05:10 +00003157public:
3158 /// @brief Constructor with insert-before-instruction semantics
3159 BitCastInst(
3160 Value *S, ///< The value to be casted
3161 const Type *Ty, ///< The type to casted to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003162 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003163 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3164 );
3165
3166 /// @brief Constructor with insert-at-end-of-block semantics
3167 BitCastInst(
3168 Value *S, ///< The value to be casted
3169 const Type *Ty, ///< The type to casted to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003170 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003171 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3172 );
3173
Reid Spencer3da59db2006-11-27 01:05:10 +00003174 // Methods for support type inquiry through isa, cast, and dyn_cast:
3175 static inline bool classof(const BitCastInst *) { return true; }
3176 static inline bool classof(const Instruction *I) {
3177 return I->getOpcode() == BitCast;
3178 }
3179 static inline bool classof(const Value *V) {
3180 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3181 }
3182};
3183
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003184} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00003185
3186#endif