blob: 413a595ab38af64d902a9a0fd91b55ad012ca1f6 [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;
Dan Gohmanbccfc242009-09-03 15:34:35 +000032class DominatorTree;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000033
34//===----------------------------------------------------------------------===//
Victor Hernandez7b929da2009-10-23 21:09:37 +000035// AllocaInst Class
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000036//===----------------------------------------------------------------------===//
37
Victor Hernandez7b929da2009-10-23 21:09:37 +000038/// AllocaInst - an instruction to allocate memory on the stack
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000039///
Victor Hernandez7b929da2009-10-23 21:09:37 +000040class AllocaInst : public UnaryInstruction {
Devang Patel50b6e332009-10-27 22:16:29 +000041protected:
42 virtual AllocaInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000043public:
Victor Hernandez7b929da2009-10-23 21:09:37 +000044 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
45 const Twine &Name = "", Instruction *InsertBefore = 0);
46 AllocaInst(const Type *Ty, Value *ArraySize,
47 const Twine &Name, BasicBlock *InsertAtEnd);
48
49 AllocaInst(const Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
50 AllocaInst(const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
51
52 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
53 const Twine &Name = "", Instruction *InsertBefore = 0);
54 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
55 const Twine &Name, BasicBlock *InsertAtEnd);
56
Gabor Greif051a9502008-04-06 20:25:17 +000057 // Out of line virtual method, so the vtable, etc. has a home.
Victor Hernandez7b929da2009-10-23 21:09:37 +000058 virtual ~AllocaInst();
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000059
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000060 /// isArrayAllocation - Return true if there is an allocation size parameter
61 /// to the allocation instruction that is not 1.
62 ///
63 bool isArrayAllocation() const;
64
Dan Gohman18476ee2009-07-07 20:47:48 +000065 /// getArraySize - Get the number of elements allocated. For a simple
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000066 /// allocation of a single element, this will return a constant 1 value.
67 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000068 const Value *getArraySize() const { return getOperand(0); }
69 Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000070
71 /// getType - Overload to return most specific pointer type
72 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000073 const PointerType *getType() const {
Misha Brukman9769ab22005-04-21 20:19:05 +000074 return reinterpret_cast<const PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000075 }
76
77 /// getAllocatedType - Return the type that is being allocated by the
78 /// instruction.
79 ///
80 const Type *getAllocatedType() const;
81
Nate Begeman14b05292005-11-05 09:21:28 +000082 /// getAlignment - Return the alignment of the memory that is being allocated
83 /// by the instruction.
84 ///
Chris Lattnercafe9bb2009-12-29 02:14:09 +000085 unsigned getAlignment() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +000086 return (1u << getSubclassDataFromInstruction()) >> 1;
Chris Lattnercafe9bb2009-12-29 02:14:09 +000087 }
Dan Gohman52837072008-03-24 16:55:58 +000088 void setAlignment(unsigned Align);
Chris Lattnerf56a8db2006-10-03 17:09:12 +000089
Chris Lattnerc5dd22a2008-11-26 02:54:17 +000090 /// isStaticAlloca - Return true if this alloca is in the entry block of the
91 /// function and is a constant size. If so, the code generator will fold it
92 /// into the prolog/epilog code, so it is basically free.
93 bool isStaticAlloca() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000094
95 // Methods for support type inquiry through isa, cast, and dyn_cast:
96 static inline bool classof(const AllocaInst *) { return true; }
97 static inline bool classof(const Instruction *I) {
98 return (I->getOpcode() == Instruction::Alloca);
99 }
100 static inline bool classof(const Value *V) {
101 return isa<Instruction>(V) && classof(cast<Instruction>(V));
102 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000103private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000104 // Shadow Instruction::setInstructionSubclassData with a private forwarding
105 // method so that subclasses cannot accidentally use it.
106 void setInstructionSubclassData(unsigned short D) {
107 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000108 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000109};
110
111
112//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000113// LoadInst Class
114//===----------------------------------------------------------------------===//
115
Chris Lattner88fe29a2005-02-05 01:44:18 +0000116/// LoadInst - an instruction for reading from memory. This uses the
117/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000118///
Chris Lattner454928e2005-01-29 00:31:36 +0000119class LoadInst : public UnaryInstruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000120 void AssertOK();
Devang Patel50b6e332009-10-27 22:16:29 +0000121protected:
122 virtual LoadInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000123public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000124 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
125 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000126 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
Christopher Lamb43c7f372007-04-22 19:24:39 +0000127 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000128 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000129 unsigned Align, Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000130 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000131 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000132 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000133 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000134
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000135 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
136 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
137 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
138 bool isVolatile = false, Instruction *InsertBefore = 0);
139 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
140 BasicBlock *InsertAtEnd);
141
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000142 /// isVolatile - Return true if this is a load from a volatile memory
143 /// location.
144 ///
Chris Lattnerb2406d92009-12-29 02:46:09 +0000145 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000146
147 /// setVolatile - Specify whether this is a volatile load or not.
148 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000149 void setVolatile(bool V) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000150 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
151 (V ? 1 : 0));
Christopher Lamb43c7f372007-04-22 19:24:39 +0000152 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000153
Christopher Lamb43c7f372007-04-22 19:24:39 +0000154 /// getAlignment - Return the alignment of the access that is being performed
155 ///
156 unsigned getAlignment() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000157 return (1 << (getSubclassDataFromInstruction() >> 1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000158 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000159
Christopher Lamb43c7f372007-04-22 19:24:39 +0000160 void setAlignment(unsigned Align);
161
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000162 Value *getPointerOperand() { return getOperand(0); }
163 const Value *getPointerOperand() const { return getOperand(0); }
164 static unsigned getPointerOperandIndex() { return 0U; }
165
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000166 unsigned getPointerAddressSpace() const {
167 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
168 }
169
170
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000171 // Methods for support type inquiry through isa, cast, and dyn_cast:
172 static inline bool classof(const LoadInst *) { return true; }
173 static inline bool classof(const Instruction *I) {
174 return I->getOpcode() == Instruction::Load;
175 }
176 static inline bool classof(const Value *V) {
177 return isa<Instruction>(V) && classof(cast<Instruction>(V));
178 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000179private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000180 // Shadow Instruction::setInstructionSubclassData with a private forwarding
181 // method so that subclasses cannot accidentally use it.
182 void setInstructionSubclassData(unsigned short D) {
183 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000184 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000185};
186
187
188//===----------------------------------------------------------------------===//
189// StoreInst Class
190//===----------------------------------------------------------------------===//
191
Misha Brukman9769ab22005-04-21 20:19:05 +0000192/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000193///
194class StoreInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +0000195 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +0000196 void AssertOK();
Devang Patel50b6e332009-10-27 22:16:29 +0000197protected:
198 virtual StoreInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000199public:
Gabor Greif051a9502008-04-06 20:25:17 +0000200 // allocate space for exactly two operands
201 void *operator new(size_t s) {
202 return User::operator new(s, 2);
203 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000204 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
205 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
206 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
207 Instruction *InsertBefore = 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000208 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
209 unsigned Align, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000210 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000211 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
212 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000213
214
215 /// isVolatile - Return true if this is a load from a volatile memory
216 /// location.
217 ///
Chris Lattnerb2406d92009-12-29 02:46:09 +0000218 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000219
220 /// setVolatile - Specify whether this is a volatile load or not.
221 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000222 void setVolatile(bool V) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000223 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
224 (V ? 1 : 0));
Christopher Lamb43c7f372007-04-22 19:24:39 +0000225 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000226
Chris Lattner454928e2005-01-29 00:31:36 +0000227 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +0000228 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner454928e2005-01-29 00:31:36 +0000229
Christopher Lamb43c7f372007-04-22 19:24:39 +0000230 /// getAlignment - Return the alignment of the access that is being performed
231 ///
232 unsigned getAlignment() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000233 return (1 << (getSubclassDataFromInstruction() >> 1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000234 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000235
Christopher Lamb43c7f372007-04-22 19:24:39 +0000236 void setAlignment(unsigned Align);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000237
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000238 Value *getPointerOperand() { return getOperand(1); }
239 const Value *getPointerOperand() const { return getOperand(1); }
240 static unsigned getPointerOperandIndex() { return 1U; }
241
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000242 unsigned getPointerAddressSpace() const {
243 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
244 }
245
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000246 // Methods for support type inquiry through isa, cast, and dyn_cast:
247 static inline bool classof(const StoreInst *) { return true; }
248 static inline bool classof(const Instruction *I) {
249 return I->getOpcode() == Instruction::Store;
250 }
251 static inline bool classof(const Value *V) {
252 return isa<Instruction>(V) && classof(cast<Instruction>(V));
253 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000254private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000255 // Shadow Instruction::setInstructionSubclassData with a private forwarding
256 // method so that subclasses cannot accidentally use it.
257 void setInstructionSubclassData(unsigned short D) {
258 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000259 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000260};
261
Gabor Greifefe65362008-05-10 08:32:32 +0000262template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +0000263struct OperandTraits<StoreInst> : public FixedNumOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +0000264};
265
266DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000267
268//===----------------------------------------------------------------------===//
269// GetElementPtrInst Class
270//===----------------------------------------------------------------------===//
271
David Greeneb8f74792007-09-04 15:46:09 +0000272// checkType - Simple wrapper function to give a better assertion failure
273// message on bad indexes for a gep instruction.
274//
275static inline const Type *checkType(const Type *Ty) {
276 assert(Ty && "Invalid GetElementPtrInst indices for type!");
277 return Ty;
278}
279
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000280/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
281/// access elements of arrays and structs
282///
283class GetElementPtrInst : public Instruction {
Gabor Greifefe65362008-05-10 08:32:32 +0000284 GetElementPtrInst(const GetElementPtrInst &GEPI);
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +0000285 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000286 const Twine &NameStr);
287 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000288
289 template<typename InputIterator>
290 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000291 const Twine &NameStr,
David Greeneb8f74792007-09-04 15:46:09 +0000292 // This argument ensures that we have an iterator we can
293 // do arithmetic on in constant time
294 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000295 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000296
David Greeneb8f74792007-09-04 15:46:09 +0000297 if (NumIdx > 0) {
Gabor Greifefe65362008-05-10 08:32:32 +0000298 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +0000299 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Gabor Greifefe65362008-05-10 08:32:32 +0000300 // we have to build an array here
David Greeneb8f74792007-09-04 15:46:09 +0000301 }
302 else {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000303 init(Ptr, 0, NumIdx, NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000304 }
David Greeneb8f74792007-09-04 15:46:09 +0000305 }
306
307 /// getIndexedType - Returns the type of the element that would be loaded with
308 /// a load instruction with the specified parameters.
309 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000310 /// Null is returned if the indices are invalid for the specified
David Greeneb8f74792007-09-04 15:46:09 +0000311 /// pointer type.
312 ///
David Greeneb8f74792007-09-04 15:46:09 +0000313 template<typename InputIterator>
314 static const Type *getIndexedType(const Type *Ptr,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000315 InputIterator IdxBegin,
David Greeneb8f74792007-09-04 15:46:09 +0000316 InputIterator IdxEnd,
David Greeneb8f74792007-09-04 15:46:09 +0000317 // This argument ensures that we
318 // have an iterator we can do
319 // arithmetic on in constant time
320 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000321 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
David Greeneb8f74792007-09-04 15:46:09 +0000322
Dan Gohman041e2eb2008-05-15 19:50:34 +0000323 if (NumIdx > 0)
David Greeneb8f74792007-09-04 15:46:09 +0000324 // This requires that the iterator points to contiguous memory.
David Greene2d5a0b92008-10-29 00:30:54 +0000325 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +0000326 else
327 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
David Greeneb8f74792007-09-04 15:46:09 +0000328 }
329
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000330 /// Constructors - Create a getelementptr instruction with a base pointer an
331 /// list of indices. The first ctor can optionally insert before an existing
332 /// instruction, the second appends the new instruction to the specified
333 /// BasicBlock.
David Greeneb8f74792007-09-04 15:46:09 +0000334 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000335 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
Gabor Greifefe65362008-05-10 08:32:32 +0000336 InputIterator IdxEnd,
337 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000338 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000339 Instruction *InsertBefore);
David Greeneb8f74792007-09-04 15:46:09 +0000340 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000341 inline GetElementPtrInst(Value *Ptr,
342 InputIterator IdxBegin, InputIterator IdxEnd,
343 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000344 const Twine &NameStr, BasicBlock *InsertAtEnd);
David Greeneb8f74792007-09-04 15:46:09 +0000345
Chris Lattner38bacf22005-05-03 05:43:30 +0000346 /// Constructors - These two constructors are convenience methods because one
347 /// and two index getelementptr instructions are so common.
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000348 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +0000349 Instruction *InsertBefore = 0);
Chris Lattner38bacf22005-05-03 05:43:30 +0000350 GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000351 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +0000352protected:
353 virtual GetElementPtrInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +0000354public:
355 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000356 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
Gabor Greif051a9502008-04-06 20:25:17 +0000357 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000358 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000359 Instruction *InsertBefore = 0) {
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000360 typename std::iterator_traits<InputIterator>::difference_type Values =
Gabor Greifefe65362008-05-10 08:32:32 +0000361 1 + std::distance(IdxBegin, IdxEnd);
362 return new(Values)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000363 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000364 }
365 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000366 static GetElementPtrInst *Create(Value *Ptr,
367 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000368 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000369 BasicBlock *InsertAtEnd) {
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000370 typename std::iterator_traits<InputIterator>::difference_type Values =
Gabor Greifefe65362008-05-10 08:32:32 +0000371 1 + std::distance(IdxBegin, IdxEnd);
372 return new(Values)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000373 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000374 }
375
Gabor Greifefe65362008-05-10 08:32:32 +0000376 /// Constructors - These two creators are convenience methods because one
377 /// index getelementptr instructions are so common.
Gabor Greif051a9502008-04-06 20:25:17 +0000378 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000379 const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +0000380 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000381 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000382 }
383 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000384 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000385 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000386 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000387 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000388
Dan Gohmane2574d32009-08-11 17:57:01 +0000389 /// Create an "inbounds" getelementptr. See the documentation for the
390 /// "inbounds" flag in LangRef.html for details.
391 template<typename InputIterator>
392 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
393 InputIterator IdxEnd,
394 const Twine &NameStr = "",
395 Instruction *InsertBefore = 0) {
396 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
397 NameStr, InsertBefore);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000398 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000399 return GEP;
400 }
401 template<typename InputIterator>
402 static GetElementPtrInst *CreateInBounds(Value *Ptr,
403 InputIterator IdxBegin,
404 InputIterator IdxEnd,
405 const Twine &NameStr,
406 BasicBlock *InsertAtEnd) {
407 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
408 NameStr, InsertAtEnd);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000409 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000410 return GEP;
411 }
412 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
413 const Twine &NameStr = "",
414 Instruction *InsertBefore = 0) {
415 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
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 BasicBlock *InsertAtEnd) {
422 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000423 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000424 return GEP;
425 }
426
Gabor Greifefe65362008-05-10 08:32:32 +0000427 /// Transparently provide more efficient getOperand methods.
428 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
429
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000430 // getType - Overload to return most specific pointer type...
Devang Patel4d4a5e02008-02-23 01:11:02 +0000431 const PointerType *getType() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000432 return reinterpret_cast<const PointerType*>(Instruction::getType());
433 }
434
435 /// getIndexedType - Returns the type of the element that would be loaded with
436 /// a load instruction with the specified parameters.
437 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000438 /// Null is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000439 /// pointer type.
440 ///
David Greeneb8f74792007-09-04 15:46:09 +0000441 template<typename InputIterator>
Misha Brukman9769ab22005-04-21 20:19:05 +0000442 static const Type *getIndexedType(const Type *Ptr,
David Greeneb8f74792007-09-04 15:46:09 +0000443 InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +0000444 InputIterator IdxEnd) {
445 return getIndexedType(Ptr, IdxBegin, IdxEnd,
David Greeneb8f74792007-09-04 15:46:09 +0000446 typename std::iterator_traits<InputIterator>::
Dan Gohman041e2eb2008-05-15 19:50:34 +0000447 iterator_category());
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000448 }
Matthijs Kooijmane2afded2008-07-29 08:46:11 +0000449
450 static const Type *getIndexedType(const Type *Ptr,
451 Value* const *Idx, unsigned NumIdx);
452
453 static const Type *getIndexedType(const Type *Ptr,
454 uint64_t const *Idx, unsigned NumIdx);
455
Chris Lattner38bacf22005-05-03 05:43:30 +0000456 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
Misha Brukman9769ab22005-04-21 20:19:05 +0000457
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000458 inline op_iterator idx_begin() { return op_begin()+1; }
459 inline const_op_iterator idx_begin() const { return op_begin()+1; }
460 inline op_iterator idx_end() { return op_end(); }
461 inline const_op_iterator idx_end() const { return op_end(); }
462
463 Value *getPointerOperand() {
464 return getOperand(0);
465 }
466 const Value *getPointerOperand() const {
467 return getOperand(0);
468 }
469 static unsigned getPointerOperandIndex() {
470 return 0U; // get index for modifying correct operand
471 }
Chris Lattner8a67ac52009-08-30 20:06:40 +0000472
473 unsigned getPointerAddressSpace() const {
474 return cast<PointerType>(getType())->getAddressSpace();
475 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000476
Chris Lattner3bc6ced2009-01-09 05:27:40 +0000477 /// getPointerOperandType - Method to return the pointer operand as a
478 /// PointerType.
479 const PointerType *getPointerOperandType() const {
480 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
481 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000482
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000483
Devang Patel4d4a5e02008-02-23 01:11:02 +0000484 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000485 return getNumOperands() - 1;
486 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000487
Devang Patel4d4a5e02008-02-23 01:11:02 +0000488 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000489 return getNumOperands() > 1;
490 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000491
Chris Lattner6f771d42007-04-14 00:12:57 +0000492 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
493 /// zeros. If so, the result pointer and the first operand have the same
494 /// value, just potentially different types.
495 bool hasAllZeroIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000496
Chris Lattner6b0974c2007-04-27 20:35:56 +0000497 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
498 /// constant integers. If so, the result pointer and the first operand have
499 /// a constant offset between them.
500 bool hasAllConstantIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000501
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000502 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
503 /// See LangRef.html for the meaning of inbounds on a getelementptr.
Nick Lewyckyae05e7d2009-09-27 21:33:04 +0000504 void setIsInBounds(bool b = true);
505
506 /// isInBounds - Determine whether the GEP has the inbounds flag.
507 bool isInBounds() const;
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000508
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000509 // Methods for support type inquiry through isa, cast, and dyn_cast:
510 static inline bool classof(const GetElementPtrInst *) { return true; }
511 static inline bool classof(const Instruction *I) {
512 return (I->getOpcode() == Instruction::GetElementPtr);
513 }
514 static inline bool classof(const Value *V) {
515 return isa<Instruction>(V) && classof(cast<Instruction>(V));
516 }
517};
518
Gabor Greifefe65362008-05-10 08:32:32 +0000519template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +0000520struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
Gabor Greifefe65362008-05-10 08:32:32 +0000521};
522
523template<typename InputIterator>
524GetElementPtrInst::GetElementPtrInst(Value *Ptr,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000525 InputIterator IdxBegin,
Gabor Greifefe65362008-05-10 08:32:32 +0000526 InputIterator IdxEnd,
527 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000528 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000529 Instruction *InsertBefore)
530 : Instruction(PointerType::get(checkType(
531 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000532 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000533 cast<PointerType>(Ptr->getType())
534 ->getAddressSpace()),
535 GetElementPtr,
536 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
537 Values, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000538 init(Ptr, IdxBegin, IdxEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000539 typename std::iterator_traits<InputIterator>::iterator_category());
540}
541template<typename InputIterator>
542GetElementPtrInst::GetElementPtrInst(Value *Ptr,
543 InputIterator IdxBegin,
544 InputIterator IdxEnd,
545 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000546 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000547 BasicBlock *InsertAtEnd)
548 : Instruction(PointerType::get(checkType(
549 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000550 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000551 cast<PointerType>(Ptr->getType())
552 ->getAddressSpace()),
553 GetElementPtr,
554 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
555 Values, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000556 init(Ptr, IdxBegin, IdxEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000557 typename std::iterator_traits<InputIterator>::iterator_category());
558}
559
560
561DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
562
563
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000564//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000565// ICmpInst Class
566//===----------------------------------------------------------------------===//
567
568/// This instruction compares its operands according to the predicate given
Nate Begemanac80ade2008-05-12 19:01:56 +0000569/// to the constructor. It only operates on integers or pointers. The operands
570/// must be identical types.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000571/// @brief Represent an integer comparison operator.
572class ICmpInst: public CmpInst {
Devang Patel50b6e332009-10-27 22:16:29 +0000573protected:
574 /// @brief Clone an indentical ICmpInst
575 virtual ICmpInst *clone_impl() const;
Reid Spencer45fb3f32006-11-20 01:22:35 +0000576public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000577 /// @brief Constructor with insert-before-instruction semantics.
578 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000579 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +0000580 Predicate pred, ///< The predicate to use for the comparison
581 Value *LHS, ///< The left-hand-side of the expression
582 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000583 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000584 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +0000585 Instruction::ICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000586 InsertBefore) {
587 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
588 pred <= CmpInst::LAST_ICMP_PREDICATE &&
589 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000590 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000591 "Both operands to ICmp instruction are not of the same type!");
592 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000593 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000594 getOperand(0)->getType()->isPointerTy()) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000595 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000596 }
597
Owen Anderson333c4002009-07-09 23:48:35 +0000598 /// @brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000599 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000600 BasicBlock &InsertAtEnd, ///< Block to insert into.
601 Predicate pred, ///< The predicate to use for the comparison
602 Value *LHS, ///< The left-hand-side of the expression
603 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000604 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000605 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000606 Instruction::ICmp, pred, LHS, RHS, NameStr,
607 &InsertAtEnd) {
608 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
609 pred <= CmpInst::LAST_ICMP_PREDICATE &&
610 "Invalid ICmp predicate value");
611 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
612 "Both operands to ICmp instruction are not of the same type!");
613 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000614 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000615 getOperand(0)->getType()->isPointerTy()) &&
Owen Anderson333c4002009-07-09 23:48:35 +0000616 "Invalid operand types for ICmp instruction");
617 }
618
619 /// @brief Constructor with no-insertion semantics
620 ICmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +0000621 Predicate pred, ///< The predicate to use for the comparison
622 Value *LHS, ///< The left-hand-side of the expression
623 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000624 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000625 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000626 Instruction::ICmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +0000627 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
628 pred <= CmpInst::LAST_ICMP_PREDICATE &&
629 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000630 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000631 "Both operands to ICmp instruction are not of the same type!");
632 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000633 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000634 getOperand(0)->getType()->isPointerTy()) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000635 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000636 }
637
Reid Spencere4d87aa2006-12-23 06:05:41 +0000638 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
639 /// @returns the predicate that would be the result if the operand were
640 /// regarded as signed.
641 /// @brief Return the signed version of the predicate
642 Predicate getSignedPredicate() const {
643 return getSignedPredicate(getPredicate());
644 }
645
646 /// This is a static version that you can use without an instruction.
647 /// @brief Return the signed version of the predicate.
648 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000649
Nick Lewycky4189a532008-01-28 03:48:02 +0000650 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
651 /// @returns the predicate that would be the result if the operand were
652 /// regarded as unsigned.
653 /// @brief Return the unsigned version of the predicate
654 Predicate getUnsignedPredicate() const {
655 return getUnsignedPredicate(getPredicate());
656 }
657
658 /// This is a static version that you can use without an instruction.
659 /// @brief Return the unsigned version of the predicate.
660 static Predicate getUnsignedPredicate(Predicate pred);
661
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000662 /// isEquality - Return true if this predicate is either EQ or NE. This also
663 /// tests for commutativity.
664 static bool isEquality(Predicate P) {
665 return P == ICMP_EQ || P == ICMP_NE;
666 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000667
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000668 /// isEquality - Return true if this predicate is either EQ or NE. This also
669 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000670 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000671 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000672 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000673
674 /// @returns true if the predicate of this ICmpInst is commutative
675 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000676 bool isCommutative() const { return isEquality(); }
677
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000678 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000679 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +0000680 bool isRelational() const {
681 return !isEquality();
682 }
683
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000684 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000685 ///
686 static bool isRelational(Predicate P) {
687 return !isEquality(P);
688 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000689
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000690 /// Initialize a set of values that all satisfy the predicate with C.
Reid Spencer3da43842007-02-28 22:00:54 +0000691 /// @brief Make a ConstantRange for a relation with a constant value.
692 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
693
Reid Spencer45fb3f32006-11-20 01:22:35 +0000694 /// Exchange the two operands to this instruction in such a way that it does
695 /// not modify the semantics of the instruction. The predicate value may be
696 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000697 /// (e.g. ult).
Reid Spencer45fb3f32006-11-20 01:22:35 +0000698 /// @brief Swap operands and adjust predicate.
699 void swapOperands() {
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000700 setPredicate(getSwappedPredicate());
Gabor Greif94fb68b2008-05-13 22:51:52 +0000701 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000702 }
703
704 // Methods for support type inquiry through isa, cast, and dyn_cast:
705 static inline bool classof(const ICmpInst *) { return true; }
706 static inline bool classof(const Instruction *I) {
707 return I->getOpcode() == Instruction::ICmp;
708 }
709 static inline bool classof(const Value *V) {
710 return isa<Instruction>(V) && classof(cast<Instruction>(V));
711 }
Dan Gohmanf72fb672008-09-09 01:02:47 +0000712
Reid Spencer45fb3f32006-11-20 01:22:35 +0000713};
714
715//===----------------------------------------------------------------------===//
716// FCmpInst Class
717//===----------------------------------------------------------------------===//
718
719/// This instruction compares its operands according to the predicate given
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000720/// to the constructor. It only operates on floating point values or packed
Reid Spencer45fb3f32006-11-20 01:22:35 +0000721/// vectors of floating point values. The operands must be identical types.
722/// @brief Represents a floating point comparison operator.
723class FCmpInst: public CmpInst {
Devang Patel50b6e332009-10-27 22:16:29 +0000724protected:
725 /// @brief Clone an indentical FCmpInst
726 virtual FCmpInst *clone_impl() const;
Reid Spencer45fb3f32006-11-20 01:22:35 +0000727public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000728 /// @brief Constructor with insert-before-instruction semantics.
729 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000730 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +0000731 Predicate pred, ///< The predicate to use for the comparison
732 Value *LHS, ///< The left-hand-side of the expression
733 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000734 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000735 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +0000736 Instruction::FCmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000737 InsertBefore) {
738 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
739 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000740 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000741 "Both operands to FCmp instruction are not of the same type!");
742 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000743 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000744 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000745 }
Owen Anderson333c4002009-07-09 23:48:35 +0000746
747 /// @brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000748 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000749 BasicBlock &InsertAtEnd, ///< Block to insert into.
750 Predicate pred, ///< The predicate to use for the comparison
751 Value *LHS, ///< The left-hand-side of the expression
752 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000753 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000754 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000755 Instruction::FCmp, pred, LHS, RHS, NameStr,
756 &InsertAtEnd) {
757 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
758 "Invalid FCmp predicate value");
759 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
760 "Both operands to FCmp instruction are not of the same type!");
761 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000762 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Owen Anderson333c4002009-07-09 23:48:35 +0000763 "Invalid operand types for FCmp instruction");
764 }
765
766 /// @brief Constructor with no-insertion semantics
767 FCmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +0000768 Predicate pred, ///< The predicate to use for the comparison
769 Value *LHS, ///< The left-hand-side of the expression
770 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000771 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000772 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000773 Instruction::FCmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +0000774 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
775 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000776 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000777 "Both operands to FCmp instruction are not of the same type!");
778 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000779 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000780 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000781 }
782
Reid Spencer45fb3f32006-11-20 01:22:35 +0000783 /// @returns true if the predicate of this instruction is EQ or NE.
784 /// @brief Determine if this is an equality predicate.
785 bool isEquality() const {
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000786 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
787 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
Reid Spencer45fb3f32006-11-20 01:22:35 +0000788 }
Dan Gohman793df072008-09-16 16:44:00 +0000789
790 /// @returns true if the predicate of this instruction is commutative.
791 /// @brief Determine if this is a commutative predicate.
792 bool isCommutative() const {
793 return isEquality() ||
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000794 getPredicate() == FCMP_FALSE ||
795 getPredicate() == FCMP_TRUE ||
796 getPredicate() == FCMP_ORD ||
797 getPredicate() == FCMP_UNO;
Dan Gohman793df072008-09-16 16:44:00 +0000798 }
Reid Spencer45fb3f32006-11-20 01:22:35 +0000799
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000800 /// @returns true if the predicate is relational (not EQ or NE).
Reid Spencer45fb3f32006-11-20 01:22:35 +0000801 /// @brief Determine if this a relational predicate.
802 bool isRelational() const { return !isEquality(); }
803
804 /// Exchange the two operands to this instruction in such a way that it does
805 /// not modify the semantics of the instruction. The predicate value may be
806 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000807 /// (e.g. ult).
Reid Spencer45fb3f32006-11-20 01:22:35 +0000808 /// @brief Swap operands and adjust predicate.
809 void swapOperands() {
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000810 setPredicate(getSwappedPredicate());
Gabor Greif94fb68b2008-05-13 22:51:52 +0000811 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000812 }
813
814 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
815 static inline bool classof(const FCmpInst *) { return true; }
816 static inline bool classof(const Instruction *I) {
817 return I->getOpcode() == Instruction::FCmp;
818 }
819 static inline bool classof(const Value *V) {
820 return isa<Instruction>(V) && classof(cast<Instruction>(V));
821 }
822};
823
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000824//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000825/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +0000826/// machine's calling convention. This class uses low bit of the SubClassData
827/// field to indicate whether or not this is a tail call. The rest of the bits
828/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000829///
830class CallInst : public Instruction {
Devang Patel05988662008-09-25 21:00:45 +0000831 AttrListPtr AttributeList; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000832 CallInst(const CallInst &CI);
Chris Lattnerd54f4322007-02-13 00:58:44 +0000833 void init(Value *Func, Value* const *Params, unsigned NumParams);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000834 void init(Value *Func, Value *Actual1, Value *Actual2);
835 void init(Value *Func, Value *Actual);
836 void init(Value *Func);
837
David Greene52eec542007-08-01 03:43:44 +0000838 template<typename InputIterator>
839 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000840 const Twine &NameStr,
David Greene52eec542007-08-01 03:43:44 +0000841 // This argument ensures that we have an iterator we can
842 // do arithmetic on in constant time
843 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000844 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000845
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000846 // This requires that the iterator points to contiguous memory.
847 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
Evan Cheng1bf9a182008-07-24 00:08:56 +0000848 setName(NameStr);
David Greene52eec542007-08-01 03:43:44 +0000849 }
850
David Greene52eec542007-08-01 03:43:44 +0000851 /// Construct a CallInst given a range of arguments. InputIterator
852 /// must be a random-access iterator pointing to contiguous storage
853 /// (e.g. a std::vector<>::iterator). Checks are made for
854 /// random-accessness but not for contiguous storage as that would
855 /// incur runtime overhead.
856 /// @brief Construct a CallInst from a range of arguments
857 template<typename InputIterator>
858 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000859 const Twine &NameStr, Instruction *InsertBefore);
David Greene52eec542007-08-01 03:43:44 +0000860
861 /// Construct a CallInst given a range of arguments. InputIterator
862 /// must be a random-access iterator pointing to contiguous storage
863 /// (e.g. a std::vector<>::iterator). Checks are made for
864 /// random-accessness but not for contiguous storage as that would
865 /// incur runtime overhead.
866 /// @brief Construct a CallInst from a range of arguments
867 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000868 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000869 const Twine &NameStr, BasicBlock *InsertAtEnd);
David Greene52eec542007-08-01 03:43:44 +0000870
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000871 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000872 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000873 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000874 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000875 explicit CallInst(Value *F, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000876 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000877 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +0000878protected:
879 virtual CallInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +0000880public:
881 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000882 static CallInst *Create(Value *Func,
883 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000884 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +0000885 Instruction *InsertBefore = 0) {
Bill Wendlingc2e73532008-05-10 19:59:59 +0000886 return new((unsigned)(ArgEnd - ArgBegin + 1))
Evan Cheng1bf9a182008-07-24 00:08:56 +0000887 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000888 }
889 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000890 static CallInst *Create(Value *Func,
891 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000892 const Twine &NameStr, BasicBlock *InsertAtEnd) {
Bill Wendlingc2e73532008-05-10 19:59:59 +0000893 return new((unsigned)(ArgEnd - ArgBegin + 1))
Evan Cheng1bf9a182008-07-24 00:08:56 +0000894 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000895 }
Evan Cheng1bf9a182008-07-24 00:08:56 +0000896 static CallInst *Create(Value *F, Value *Actual,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000897 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000898 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000899 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000900 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000901 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
Gabor Greif051a9502008-04-06 20:25:17 +0000902 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000903 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000904 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000905 static CallInst *Create(Value *F, const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000906 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000907 return new(1) CallInst(F, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000908 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000909 static CallInst *Create(Value *F, const Twine &NameStr,
Evan Cheng34cd4a42008-05-05 18:30:58 +0000910 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000911 return new(1) CallInst(F, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000912 }
Evan Chengfabcb912009-09-10 04:36:43 +0000913 /// CreateMalloc - Generate the IR for a call to malloc:
914 /// 1. Compute the malloc call's argument as the specified type's size,
915 /// possibly multiplied by the array size if the array size is not
916 /// constant 1.
917 /// 2. Call malloc with that argument.
918 /// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewycky3fc35c52009-10-17 23:52:26 +0000919 static Instruction *CreateMalloc(Instruction *InsertBefore,
920 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000921 Value *AllocSize, Value *ArraySize = 0,
Nick Lewycky3fc35c52009-10-17 23:52:26 +0000922 const Twine &Name = "");
923 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
924 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000925 Value *AllocSize, Value *ArraySize = 0,
926 Function* MallocF = 0,
Nick Lewycky3fc35c52009-10-17 23:52:26 +0000927 const Twine &Name = "");
Victor Hernandez66284e02009-10-24 04:23:03 +0000928 /// CreateFree - Generate the IR for a call to the builtin free function.
929 static void CreateFree(Value* Source, Instruction *InsertBefore);
930 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000931
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000932 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000933
Chris Lattnerb2406d92009-12-29 02:46:09 +0000934 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
Evan Cheng1bf9a182008-07-24 00:08:56 +0000935 void setTailCall(bool isTC = true) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000936 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
937 unsigned(isTC));
Chris Lattner3340ffe2005-05-06 20:26:26 +0000938 }
939
Dan Gohmanf2752502008-09-26 21:38:45 +0000940 /// Provide fast operand accessors
941 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000942
Chris Lattner3340ffe2005-05-06 20:26:26 +0000943 /// getCallingConv/setCallingConv - Get or set the calling convention of this
944 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000945 CallingConv::ID getCallingConv() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000946 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000947 }
948 void setCallingConv(CallingConv::ID CC) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000949 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
950 (static_cast<unsigned>(CC) << 1));
Chris Lattner3340ffe2005-05-06 20:26:26 +0000951 }
Chris Lattnerddb6db42005-05-06 05:51:46 +0000952
Devang Patel05988662008-09-25 21:00:45 +0000953 /// getAttributes - Return the parameter attributes for this call.
Chris Lattner041221c2008-03-13 04:33:03 +0000954 ///
Devang Patel05988662008-09-25 21:00:45 +0000955 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencer4746ecf2007-04-09 15:01:12 +0000956
Dan Gohmanf2752502008-09-26 21:38:45 +0000957 /// setAttributes - Set the parameter attributes for this call.
958 ///
Devang Patel05988662008-09-25 21:00:45 +0000959 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000960
Devang Patel05988662008-09-25 21:00:45 +0000961 /// addAttribute - adds the attribute to the list of attributes.
962 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsdc024672007-11-27 13:23:08 +0000963
Devang Patel05988662008-09-25 21:00:45 +0000964 /// removeAttribute - removes the attribute from the list of attributes.
965 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +0000966
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000967 /// @brief Determine whether the call or the callee has the given attribute.
Dan Gohmanf2752502008-09-26 21:38:45 +0000968 bool paramHasAttr(unsigned i, Attributes attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000969
Dale Johannesen08e78b12008-02-22 17:49:45 +0000970 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +0000971 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +0000972 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +0000973 }
Eric Christopherf27e6082010-03-25 04:49:10 +0000974
975 /// @brief Return true if the call should not be inlined.
976 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
977 void setIsNoInline(bool Value) {
978 if (Value) addAttribute(~0, Attribute::NoInline);
979 else removeAttribute(~0, Attribute::NoInline);
980 }
Dale Johannesen08e78b12008-02-22 17:49:45 +0000981
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000982 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +0000983 bool doesNotAccessMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +0000984 return paramHasAttr(~0, Attribute::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +0000985 }
Evan Cheng1bf9a182008-07-24 00:08:56 +0000986 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +0000987 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
988 else removeAttribute(~0, Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +0000989 }
990
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000991 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +0000992 bool onlyReadsMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +0000993 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +0000994 }
Evan Cheng1bf9a182008-07-24 00:08:56 +0000995 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +0000996 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
997 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +0000998 }
999
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001000 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001001 bool doesNotReturn() const {
Devang Patel19c87462008-09-26 22:53:05 +00001002 return paramHasAttr(~0, Attribute::NoReturn);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001003 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001004 void setDoesNotReturn(bool DoesNotReturn = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001005 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1006 else removeAttribute(~0, Attribute::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00001007 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001008
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001009 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001010 bool doesNotThrow() const {
Devang Patel19c87462008-09-26 22:53:05 +00001011 return paramHasAttr(~0, Attribute::NoUnwind);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001012 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001013 void setDoesNotThrow(bool DoesNotThrow = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001014 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1015 else removeAttribute(~0, Attribute::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00001016 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001017
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001018 /// @brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00001019 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001020 bool hasStructRetAttr() const {
1021 // Be friendly and also check the callee.
Devang Patel05988662008-09-25 21:00:45 +00001022 return paramHasAttr(1, Attribute::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001023 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001024
Evan Chengf4a54982008-01-12 18:57:32 +00001025 /// @brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001026 bool hasByValArgument() const {
Devang Patel05988662008-09-25 21:00:45 +00001027 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001028 }
Evan Chengf4a54982008-01-12 18:57:32 +00001029
Dan Gohmanf2752502008-09-26 21:38:45 +00001030 /// getCalledFunction - Return the function called, or null if this is an
1031 /// indirect function invocation.
1032 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001033 Function *getCalledFunction() const {
Gabor Greif7e07c002009-03-12 23:13:03 +00001034 return dyn_cast<Function>(Op<0>());
Chris Lattner721aef62004-11-18 17:46:57 +00001035 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001036
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001037 /// getCalledValue - Get a pointer to the function that is invoked by this
Chris Lattner14d80382009-10-18 05:08:07 +00001038 /// instruction.
Gabor Greif7e07c002009-03-12 23:13:03 +00001039 const Value *getCalledValue() const { return Op<0>(); }
1040 Value *getCalledValue() { return Op<0>(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001041
Chris Lattner14d80382009-10-18 05:08:07 +00001042 /// setCalledFunction - Set the function called.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001043 void setCalledFunction(Value* Fn) {
1044 Op<0>() = Fn;
1045 }
1046
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001047 // Methods for support type inquiry through isa, cast, and dyn_cast:
1048 static inline bool classof(const CallInst *) { return true; }
1049 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001050 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001051 }
1052 static inline bool classof(const Value *V) {
1053 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1054 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001055private:
Chris Lattnerb2406d92009-12-29 02:46:09 +00001056 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1057 // method so that subclasses cannot accidentally use it.
1058 void setInstructionSubclassData(unsigned short D) {
1059 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001060 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001061};
1062
Gabor Greifefe65362008-05-10 08:32:32 +00001063template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001064struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
Gabor Greifefe65362008-05-10 08:32:32 +00001065};
1066
1067template<typename InputIterator>
1068CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001069 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001070 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1071 ->getElementType())->getReturnType(),
1072 Instruction::Call,
1073 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001074 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001075 init(Func, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001076 typename std::iterator_traits<InputIterator>::iterator_category());
1077}
1078
1079template<typename InputIterator>
1080CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001081 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00001082 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1083 ->getElementType())->getReturnType(),
1084 Instruction::Call,
1085 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001086 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001087 init(Func, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001088 typename std::iterator_traits<InputIterator>::iterator_category());
1089}
1090
1091DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1092
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001093//===----------------------------------------------------------------------===//
1094// SelectInst Class
1095//===----------------------------------------------------------------------===//
1096
1097/// SelectInst - This class represents the LLVM 'select' instruction.
1098///
1099class SelectInst : public Instruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001100 void init(Value *C, Value *S1, Value *S2) {
Chris Lattnerb76ec322008-12-29 00:12:50 +00001101 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
Gabor Greifefe65362008-05-10 08:32:32 +00001102 Op<0>() = C;
1103 Op<1>() = S1;
1104 Op<2>() = S2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001105 }
1106
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001107 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001108 Instruction *InsertBefore)
1109 : Instruction(S1->getType(), Instruction::Select,
1110 &Op<0>(), 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001111 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001112 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001113 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001114 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001115 BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001116 : Instruction(S1->getType(), Instruction::Select,
1117 &Op<0>(), 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001118 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001119 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001120 }
Devang Patel50b6e332009-10-27 22:16:29 +00001121protected:
1122 virtual SelectInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001123public:
Evan Chengd69bb1a2008-05-05 17:41:03 +00001124 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001125 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001126 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001127 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001128 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001129 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001130 const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001131 BasicBlock *InsertAtEnd) {
1132 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001133 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001134
Chris Lattner97159122009-09-08 03:32:53 +00001135 const Value *getCondition() const { return Op<0>(); }
1136 const Value *getTrueValue() const { return Op<1>(); }
1137 const Value *getFalseValue() const { return Op<2>(); }
1138 Value *getCondition() { return Op<0>(); }
1139 Value *getTrueValue() { return Op<1>(); }
1140 Value *getFalseValue() { return Op<2>(); }
1141
Chris Lattnerb76ec322008-12-29 00:12:50 +00001142 /// areInvalidOperands - Return a string if the specified operands are invalid
1143 /// for a select operation, otherwise return null.
1144 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
Chris Lattner454928e2005-01-29 00:31:36 +00001145
1146 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001147 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001148
1149 OtherOps getOpcode() const {
1150 return static_cast<OtherOps>(Instruction::getOpcode());
1151 }
1152
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001153 // Methods for support type inquiry through isa, cast, and dyn_cast:
1154 static inline bool classof(const SelectInst *) { return true; }
1155 static inline bool classof(const Instruction *I) {
1156 return I->getOpcode() == Instruction::Select;
1157 }
1158 static inline bool classof(const Value *V) {
1159 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1160 }
1161};
1162
Gabor Greifefe65362008-05-10 08:32:32 +00001163template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001164struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001165};
1166
1167DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1168
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001169//===----------------------------------------------------------------------===//
1170// VAArgInst Class
1171//===----------------------------------------------------------------------===//
1172
1173/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001174/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001175///
Chris Lattner454928e2005-01-29 00:31:36 +00001176class VAArgInst : public UnaryInstruction {
Devang Patel50b6e332009-10-27 22:16:29 +00001177protected:
1178 virtual VAArgInst *clone_impl() const;
1179
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001180public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001181 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001182 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001183 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001184 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001185 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001186 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001187 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001188 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001189 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001190 }
1191
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001192 // Methods for support type inquiry through isa, cast, and dyn_cast:
1193 static inline bool classof(const VAArgInst *) { return true; }
1194 static inline bool classof(const Instruction *I) {
1195 return I->getOpcode() == VAArg;
1196 }
1197 static inline bool classof(const Value *V) {
1198 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1199 }
1200};
1201
1202//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001203// ExtractElementInst Class
1204//===----------------------------------------------------------------------===//
1205
1206/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001207/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001208///
1209class ExtractElementInst : public Instruction {
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001210 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
Chris Lattner9fc18d22006-04-08 01:15:18 +00001211 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001212 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
Chris Lattner9fc18d22006-04-08 01:15:18 +00001213 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001214protected:
1215 virtual ExtractElementInst *clone_impl() const;
1216
Eric Christophera3500da2009-07-25 02:28:41 +00001217public:
Eric Christophera3500da2009-07-25 02:28:41 +00001218 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001219 const Twine &NameStr = "",
Eric Christophera3500da2009-07-25 02:28:41 +00001220 Instruction *InsertBefore = 0) {
1221 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1222 }
1223 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001224 const Twine &NameStr,
Eric Christophera3500da2009-07-25 02:28:41 +00001225 BasicBlock *InsertAtEnd) {
1226 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1227 }
Robert Bocchino49b78a52006-01-10 19:04:13 +00001228
Chris Lattnerfa495842006-04-08 04:04:54 +00001229 /// isValidOperands - Return true if an extractelement instruction can be
1230 /// formed with the specified operands.
1231 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001232
Chris Lattner97159122009-09-08 03:32:53 +00001233 Value *getVectorOperand() { return Op<0>(); }
1234 Value *getIndexOperand() { return Op<1>(); }
1235 const Value *getVectorOperand() const { return Op<0>(); }
1236 const Value *getIndexOperand() const { return Op<1>(); }
1237
1238 const VectorType *getVectorOperandType() const {
Chris Lattnerc8dee3f2009-09-08 03:39:55 +00001239 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
Chris Lattner97159122009-09-08 03:32:53 +00001240 }
1241
1242
Robert Bocchino49b78a52006-01-10 19:04:13 +00001243 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001244 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001245
1246 // Methods for support type inquiry through isa, cast, and dyn_cast:
1247 static inline bool classof(const ExtractElementInst *) { return true; }
1248 static inline bool classof(const Instruction *I) {
1249 return I->getOpcode() == Instruction::ExtractElement;
1250 }
1251 static inline bool classof(const Value *V) {
1252 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1253 }
1254};
1255
Gabor Greifefe65362008-05-10 08:32:32 +00001256template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001257struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00001258};
1259
1260DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1261
Robert Bocchino49b78a52006-01-10 19:04:13 +00001262//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001263// InsertElementInst Class
1264//===----------------------------------------------------------------------===//
1265
1266/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001267/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001268///
1269class InsertElementInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001270 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001271 const Twine &NameStr = "",
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001272 Instruction *InsertBefore = 0);
Gabor Greif051a9502008-04-06 20:25:17 +00001273 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001274 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001275protected:
1276 virtual InsertElementInst *clone_impl() const;
1277
Robert Bocchinof9993442006-01-17 20:05:59 +00001278public:
Gabor Greif051a9502008-04-06 20:25:17 +00001279 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001280 const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +00001281 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001282 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001283 }
Gabor Greif051a9502008-04-06 20:25:17 +00001284 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001285 const Twine &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001286 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001287 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001288 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001289
Chris Lattnerfa495842006-04-08 04:04:54 +00001290 /// isValidOperands - Return true if an insertelement instruction can be
1291 /// formed with the specified operands.
1292 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1293 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001294
Reid Spencerac9dcb92007-02-15 03:39:18 +00001295 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001296 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001297 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001298 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001299 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001300
Robert Bocchinof9993442006-01-17 20:05:59 +00001301 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001302 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchinof9993442006-01-17 20:05:59 +00001303
1304 // Methods for support type inquiry through isa, cast, and dyn_cast:
1305 static inline bool classof(const InsertElementInst *) { return true; }
1306 static inline bool classof(const Instruction *I) {
1307 return I->getOpcode() == Instruction::InsertElement;
1308 }
1309 static inline bool classof(const Value *V) {
1310 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1311 }
1312};
1313
Gabor Greifefe65362008-05-10 08:32:32 +00001314template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001315struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001316};
1317
1318DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1319
Robert Bocchinof9993442006-01-17 20:05:59 +00001320//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001321// ShuffleVectorInst Class
1322//===----------------------------------------------------------------------===//
1323
1324/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1325/// input vectors.
1326///
1327class ShuffleVectorInst : public Instruction {
Devang Patel50b6e332009-10-27 22:16:29 +00001328protected:
1329 virtual ShuffleVectorInst *clone_impl() const;
1330
Chris Lattner9fc18d22006-04-08 01:15:18 +00001331public:
Gabor Greif051a9502008-04-06 20:25:17 +00001332 // allocate space for exactly three operands
1333 void *operator new(size_t s) {
1334 return User::operator new(s, 3);
1335 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001336 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001337 const Twine &NameStr = "",
Evan Cheng1bf9a182008-07-24 00:08:56 +00001338 Instruction *InsertBefor = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001339 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001340 const Twine &NameStr, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001341
Chris Lattnerfa495842006-04-08 04:04:54 +00001342 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001343 /// formed with the specified operands.
1344 static bool isValidOperands(const Value *V1, const Value *V2,
1345 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001346
Reid Spencerac9dcb92007-02-15 03:39:18 +00001347 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001348 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001349 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001350 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001351 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001352
Chris Lattner9fc18d22006-04-08 01:15:18 +00001353 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001354 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001355
Chris Lattner8728f192008-03-02 05:28:33 +00001356 /// getMaskValue - Return the index from the shuffle mask for the specified
1357 /// output result. This is either -1 if the element is undef or a number less
1358 /// than 2*numelements.
1359 int getMaskValue(unsigned i) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001360
Chris Lattner9fc18d22006-04-08 01:15:18 +00001361 // Methods for support type inquiry through isa, cast, and dyn_cast:
1362 static inline bool classof(const ShuffleVectorInst *) { return true; }
1363 static inline bool classof(const Instruction *I) {
1364 return I->getOpcode() == Instruction::ShuffleVector;
1365 }
1366 static inline bool classof(const Value *V) {
1367 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1368 }
1369};
1370
Gabor Greifefe65362008-05-10 08:32:32 +00001371template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001372struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001373};
1374
1375DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
Chris Lattner9fc18d22006-04-08 01:15:18 +00001376
1377//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001378// ExtractValueInst Class
1379//===----------------------------------------------------------------------===//
1380
Dan Gohmane2d896f2008-05-15 23:35:32 +00001381/// ExtractValueInst - This instruction extracts a struct member or array
1382/// element value from an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001383///
Gabor Greifd4f268b2008-06-06 20:28:12 +00001384class ExtractValueInst : public UnaryInstruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001385 SmallVector<unsigned, 4> Indices;
1386
Dan Gohman041e2eb2008-05-15 19:50:34 +00001387 ExtractValueInst(const ExtractValueInst &EVI);
Gabor Greif76aca6f2008-06-06 21:06:32 +00001388 void init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001389 const Twine &NameStr);
1390 void init(unsigned Idx, const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001391
1392 template<typename InputIterator>
Gabor Greif76aca6f2008-06-06 21:06:32 +00001393 void init(InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001394 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001395 // This argument ensures that we have an iterator we can
1396 // do arithmetic on in constant time
1397 std::random_access_iterator_tag) {
1398 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001399
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001400 // There's no fundamental reason why we require at least one index
1401 // (other than weirdness with &*IdxBegin being invalid; see
1402 // getelementptr's init routine for example). But there's no
1403 // present need to support it.
1404 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1405
1406 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +00001407 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001408 // we have to build an array here
Dan Gohman041e2eb2008-05-15 19:50:34 +00001409 }
1410
1411 /// getIndexedType - Returns the type of the element that would be extracted
1412 /// with an extractvalue instruction with the specified parameters.
1413 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +00001414 /// Null is returned if the indices are invalid for the specified
Dan Gohman041e2eb2008-05-15 19:50:34 +00001415 /// pointer type.
1416 ///
1417 static const Type *getIndexedType(const Type *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001418 const unsigned *Idx, unsigned NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001419
1420 template<typename InputIterator>
1421 static const Type *getIndexedType(const Type *Ptr,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001422 InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001423 InputIterator IdxEnd,
1424 // This argument ensures that we
1425 // have an iterator we can do
1426 // arithmetic on in constant time
1427 std::random_access_iterator_tag) {
1428 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1429
1430 if (NumIdx > 0)
1431 // This requires that the iterator points to contiguous memory.
Dan Gohman19a81632008-06-23 16:38:10 +00001432 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001433 else
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001434 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001435 }
1436
Dan Gohmane2d896f2008-05-15 23:35:32 +00001437 /// Constructors - Create a extractvalue instruction with a base aggregate
1438 /// value and a list of indices. The first ctor can optionally insert before
1439 /// an existing instruction, the second appends the new instruction to the
1440 /// specified BasicBlock.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001441 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001442 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001443 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001444 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001445 Instruction *InsertBefore);
1446 template<typename InputIterator>
1447 inline ExtractValueInst(Value *Agg,
1448 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001449 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001450
Dan Gohman8e640412008-05-31 19:09:47 +00001451 // allocate space for exactly one operand
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001452 void *operator new(size_t s) {
1453 return User::operator new(s, 1);
1454 }
Devang Patel50b6e332009-10-27 22:16:29 +00001455protected:
1456 virtual ExtractValueInst *clone_impl() const;
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001457
Gabor Greifd4f268b2008-06-06 20:28:12 +00001458public:
Dan Gohman041e2eb2008-05-15 19:50:34 +00001459 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001460 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001461 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001462 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001463 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001464 return new
Evan Cheng1bf9a182008-07-24 00:08:56 +00001465 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001466 }
1467 template<typename InputIterator>
1468 static ExtractValueInst *Create(Value *Agg,
1469 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001470 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001471 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001472 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001473 }
1474
1475 /// Constructors - These two creators are convenience methods because one
1476 /// index extractvalue instructions are much more common than those with
1477 /// more than one.
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001478 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001479 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001480 Instruction *InsertBefore = 0) {
Dan Gohman2f27e172008-06-23 16:48:17 +00001481 unsigned Idxs[1] = { Idx };
Evan Cheng1bf9a182008-07-24 00:08:56 +00001482 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001483 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001484 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001485 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001486 BasicBlock *InsertAtEnd) {
Dan Gohman2f27e172008-06-23 16:48:17 +00001487 unsigned Idxs[1] = { Idx };
Evan Cheng1bf9a182008-07-24 00:08:56 +00001488 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001489 }
1490
Dan Gohman041e2eb2008-05-15 19:50:34 +00001491 /// getIndexedType - Returns the type of the element that would be extracted
1492 /// with an extractvalue instruction with the specified parameters.
1493 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +00001494 /// Null is returned if the indices are invalid for the specified
Dan Gohman041e2eb2008-05-15 19:50:34 +00001495 /// pointer type.
1496 ///
1497 template<typename InputIterator>
1498 static const Type *getIndexedType(const Type *Ptr,
1499 InputIterator IdxBegin,
1500 InputIterator IdxEnd) {
1501 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1502 typename std::iterator_traits<InputIterator>::
1503 iterator_category());
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001504 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001505 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001506
Owen Anderson5678d6e2008-06-19 17:15:57 +00001507 typedef const unsigned* idx_iterator;
1508 inline idx_iterator idx_begin() const { return Indices.begin(); }
1509 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001510
1511 Value *getAggregateOperand() {
1512 return getOperand(0);
1513 }
1514 const Value *getAggregateOperand() const {
1515 return getOperand(0);
1516 }
1517 static unsigned getAggregateOperandIndex() {
1518 return 0U; // get index for modifying correct operand
1519 }
1520
1521 unsigned getNumIndices() const { // Note: always non-negative
Bill Wendling67944fc2008-06-05 07:35:27 +00001522 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001523 }
1524
1525 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001526 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001527 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001528
Dan Gohman041e2eb2008-05-15 19:50:34 +00001529 // Methods for support type inquiry through isa, cast, and dyn_cast:
1530 static inline bool classof(const ExtractValueInst *) { return true; }
1531 static inline bool classof(const Instruction *I) {
1532 return I->getOpcode() == Instruction::ExtractValue;
1533 }
1534 static inline bool classof(const Value *V) {
1535 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1536 }
1537};
1538
Dan Gohmane4569942008-05-23 00:36:11 +00001539template<typename InputIterator>
1540ExtractValueInst::ExtractValueInst(Value *Agg,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001541 InputIterator IdxBegin,
Dan Gohmane4569942008-05-23 00:36:11 +00001542 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001543 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001544 Instruction *InsertBefore)
Gabor Greifd4f268b2008-06-06 20:28:12 +00001545 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
Bill Wendling85f40542008-07-22 07:14:12 +00001546 IdxBegin, IdxEnd)),
1547 ExtractValue, Agg, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001548 init(IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001549 typename std::iterator_traits<InputIterator>::iterator_category());
1550}
1551template<typename InputIterator>
1552ExtractValueInst::ExtractValueInst(Value *Agg,
1553 InputIterator IdxBegin,
1554 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001555 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001556 BasicBlock *InsertAtEnd)
Gabor Greifd4f268b2008-06-06 20:28:12 +00001557 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
Bill Wendling85f40542008-07-22 07:14:12 +00001558 IdxBegin, IdxEnd)),
1559 ExtractValue, Agg, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001560 init(IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001561 typename std::iterator_traits<InputIterator>::iterator_category());
1562}
1563
Dan Gohmane4569942008-05-23 00:36:11 +00001564
Dan Gohman041e2eb2008-05-15 19:50:34 +00001565//===----------------------------------------------------------------------===//
1566// InsertValueInst Class
1567//===----------------------------------------------------------------------===//
1568
Dan Gohmane2d896f2008-05-15 23:35:32 +00001569/// InsertValueInst - This instruction inserts a struct field of array element
1570/// value into an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001571///
1572class InsertValueInst : public Instruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001573 SmallVector<unsigned, 4> Indices;
1574
1575 void *operator new(size_t, unsigned); // Do not implement
Dan Gohman041e2eb2008-05-15 19:50:34 +00001576 InsertValueInst(const InsertValueInst &IVI);
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +00001577 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001578 const Twine &NameStr);
1579 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001580
1581 template<typename InputIterator>
1582 void init(Value *Agg, Value *Val,
1583 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001584 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001585 // This argument ensures that we have an iterator we can
1586 // do arithmetic on in constant time
1587 std::random_access_iterator_tag) {
1588 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001589
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001590 // There's no fundamental reason why we require at least one index
1591 // (other than weirdness with &*IdxBegin being invalid; see
1592 // getelementptr's init routine for example). But there's no
1593 // present need to support it.
1594 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1595
1596 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +00001597 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001598 // we have to build an array here
Dan Gohman041e2eb2008-05-15 19:50:34 +00001599 }
1600
Dan Gohmane2d896f2008-05-15 23:35:32 +00001601 /// Constructors - Create a insertvalue instruction with a base aggregate
1602 /// value, a value to insert, and a list of indices. The first ctor can
1603 /// optionally insert before an existing instruction, the second appends
1604 /// the new instruction to the specified BasicBlock.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001605 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001606 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001607 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001608 const Twine &NameStr,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001609 Instruction *InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001610 template<typename InputIterator>
1611 inline InsertValueInst(Value *Agg, Value *Val,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001612 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001613 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001614
1615 /// Constructors - These two constructors are convenience methods because one
1616 /// and two index insertvalue instructions are so common.
1617 InsertValueInst(Value *Agg, Value *Val,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001618 unsigned Idx, const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001619 Instruction *InsertBefore = 0);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001620 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001621 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001622protected:
1623 virtual InsertValueInst *clone_impl() const;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001624public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001625 // allocate space for exactly two operands
1626 void *operator new(size_t s) {
1627 return User::operator new(s, 2);
1628 }
1629
Dan Gohman041e2eb2008-05-15 19:50:34 +00001630 template<typename InputIterator>
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +00001631 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001632 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001633 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001634 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001635 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001636 NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001637 }
1638 template<typename InputIterator>
1639 static InsertValueInst *Create(Value *Agg, Value *Val,
1640 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001641 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001642 BasicBlock *InsertAtEnd) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001643 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001644 NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001645 }
1646
1647 /// Constructors - These two creators are convenience methods because one
1648 /// index insertvalue instructions are much more common than those with
1649 /// more than one.
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001650 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001651 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001652 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001653 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001654 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001655 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001656 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001657 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001658 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001659 }
1660
Dan Gohman041e2eb2008-05-15 19:50:34 +00001661 /// Transparently provide more efficient getOperand methods.
1662 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1663
Owen Anderson5678d6e2008-06-19 17:15:57 +00001664 typedef const unsigned* idx_iterator;
1665 inline idx_iterator idx_begin() const { return Indices.begin(); }
1666 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001667
1668 Value *getAggregateOperand() {
1669 return getOperand(0);
1670 }
1671 const Value *getAggregateOperand() const {
1672 return getOperand(0);
1673 }
1674 static unsigned getAggregateOperandIndex() {
1675 return 0U; // get index for modifying correct operand
1676 }
1677
1678 Value *getInsertedValueOperand() {
1679 return getOperand(1);
1680 }
1681 const Value *getInsertedValueOperand() const {
1682 return getOperand(1);
1683 }
1684 static unsigned getInsertedValueOperandIndex() {
1685 return 1U; // get index for modifying correct operand
1686 }
1687
1688 unsigned getNumIndices() const { // Note: always non-negative
Bill Wendling67944fc2008-06-05 07:35:27 +00001689 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001690 }
1691
1692 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001693 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001694 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001695
Dan Gohman041e2eb2008-05-15 19:50:34 +00001696 // Methods for support type inquiry through isa, cast, and dyn_cast:
1697 static inline bool classof(const InsertValueInst *) { return true; }
1698 static inline bool classof(const Instruction *I) {
1699 return I->getOpcode() == Instruction::InsertValue;
1700 }
1701 static inline bool classof(const Value *V) {
1702 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1703 }
1704};
1705
1706template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001707struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001708};
1709
Dan Gohmane4569942008-05-23 00:36:11 +00001710template<typename InputIterator>
1711InsertValueInst::InsertValueInst(Value *Agg,
1712 Value *Val,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001713 InputIterator IdxBegin,
Dan Gohmane4569942008-05-23 00:36:11 +00001714 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001715 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001716 Instruction *InsertBefore)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001717 : Instruction(Agg->getType(), InsertValue,
1718 OperandTraits<InsertValueInst>::op_begin(this),
1719 2, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001720 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001721 typename std::iterator_traits<InputIterator>::iterator_category());
1722}
1723template<typename InputIterator>
1724InsertValueInst::InsertValueInst(Value *Agg,
1725 Value *Val,
1726 InputIterator IdxBegin,
1727 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001728 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001729 BasicBlock *InsertAtEnd)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001730 : Instruction(Agg->getType(), InsertValue,
1731 OperandTraits<InsertValueInst>::op_begin(this),
1732 2, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001733 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001734 typename std::iterator_traits<InputIterator>::iterator_category());
1735}
1736
Dan Gohman041e2eb2008-05-15 19:50:34 +00001737DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1738
1739//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001740// PHINode Class
1741//===----------------------------------------------------------------------===//
1742
1743// PHINode - The PHINode class is used to represent the magical mystical PHI
1744// node, that can not exist in nature, but can be synthesized in a computer
1745// scientist's overactive imagination.
1746//
1747class PHINode : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001748 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00001749 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1750 /// the number actually in use.
1751 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001752 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00001753 // allocate space for exactly zero operands
1754 void *operator new(size_t s) {
1755 return User::operator new(s, 0);
1756 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001757 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001758 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001759 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Chris Lattner454928e2005-01-29 00:31:36 +00001760 ReservedSpace(0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001761 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001762 }
1763
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001764 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001765 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Chris Lattner454928e2005-01-29 00:31:36 +00001766 ReservedSpace(0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001767 setName(NameStr);
Chris Lattner454928e2005-01-29 00:31:36 +00001768 }
Devang Patel50b6e332009-10-27 22:16:29 +00001769protected:
1770 virtual PHINode *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001771public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001772 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001773 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001774 return new PHINode(Ty, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001775 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001776 static PHINode *Create(const Type *Ty, const Twine &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001777 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001778 return new PHINode(Ty, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001779 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001780 ~PHINode();
1781
Chris Lattner454928e2005-01-29 00:31:36 +00001782 /// reserveOperandSpace - This method can be used to avoid repeated
1783 /// reallocation of PHI operand lists by reserving space for the correct
1784 /// number of operands before adding them. Unlike normal vector reserves,
1785 /// this method can also be used to trim the operand space.
1786 void reserveOperandSpace(unsigned NumValues) {
1787 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001788 }
1789
Gabor Greifefe65362008-05-10 08:32:32 +00001790 /// Provide fast operand accessors
1791 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1792
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001793 /// getNumIncomingValues - Return the number of incoming edges
1794 ///
Chris Lattner454928e2005-01-29 00:31:36 +00001795 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001796
Reid Spencerc773de62006-05-19 19:07:54 +00001797 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001798 ///
1799 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001800 assert(i*2 < getNumOperands() && "Invalid value number!");
1801 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001802 }
1803 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +00001804 assert(i*2 < getNumOperands() && "Invalid value number!");
1805 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001806 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001807 static unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001808 return i*2;
1809 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001810 static unsigned getIncomingValueNumForOperand(unsigned i) {
1811 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1812 return i/2;
1813 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001814
Dan Gohmanfb76fe02010-02-22 04:10:52 +00001815 /// getIncomingBlock - Return incoming basic block number @p i.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001816 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00001817 BasicBlock *getIncomingBlock(unsigned i) const {
Chris Lattnerfc61aef2009-10-10 08:01:27 +00001818 return cast<BasicBlock>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001819 }
Chris Lattnerceaa4572009-10-10 07:42:42 +00001820
1821 /// getIncomingBlock - Return incoming basic block corresponding
1822 /// to an operand of the PHI.
1823 ///
1824 BasicBlock *getIncomingBlock(const Use &U) const {
1825 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
Chris Lattnerfc61aef2009-10-10 08:01:27 +00001826 return cast<BasicBlock>((&U + 1)->get());
Chris Lattnerceaa4572009-10-10 07:42:42 +00001827 }
1828
1829 /// getIncomingBlock - Return incoming basic block corresponding
1830 /// to value use iterator.
1831 ///
1832 template <typename U>
1833 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1834 return getIncomingBlock(I.getUse());
1835 }
1836
1837
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001838 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Chris Lattner4b122932009-10-27 16:49:53 +00001839 setOperand(i*2+1, (Value*)BB);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001840 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001841 static unsigned getOperandNumForIncomingBlock(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001842 return i*2+1;
1843 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001844 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1845 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1846 return i/2;
1847 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001848
1849 /// addIncoming - Add an incoming value to the end of the PHI list
1850 ///
1851 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00001852 assert(V && "PHI node got a null value!");
1853 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001854 assert(getType() == V->getType() &&
1855 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +00001856 unsigned OpNo = NumOperands;
1857 if (OpNo+2 > ReservedSpace)
1858 resizeOperands(0); // Get more space!
1859 // Initialize some new operands.
1860 NumOperands = OpNo+2;
Gabor Greif6c80c382008-05-26 21:33:52 +00001861 OperandList[OpNo] = V;
Chris Lattner4b122932009-10-27 16:49:53 +00001862 OperandList[OpNo+1] = (Value*)BB;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001863 }
Misha Brukman9769ab22005-04-21 20:19:05 +00001864
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001865 /// removeIncomingValue - Remove an incoming value. This is useful if a
1866 /// predecessor basic block is deleted. The value removed is returned.
1867 ///
1868 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1869 /// is true), the PHI node is destroyed and any uses of it are replaced with
1870 /// dummy values. The only time there should be zero incoming values to a PHI
1871 /// node is when the block is dead, so this strategy is sound.
1872 ///
1873 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1874
Gabor Greifefe65362008-05-10 08:32:32 +00001875 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001876 int Idx = getBasicBlockIndex(BB);
1877 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1878 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1879 }
1880
Misha Brukman9769ab22005-04-21 20:19:05 +00001881 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001882 /// block in the value list for this PHI. Returns -1 if no instance.
1883 ///
1884 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001885 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00001886 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Chris Lattner4b122932009-10-27 16:49:53 +00001887 if (OL[i+1].get() == (const Value*)BB) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001888 return -1;
1889 }
1890
1891 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1892 return getIncomingValue(getBasicBlockIndex(BB));
1893 }
1894
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001895 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00001896 /// same value, return the value, otherwise return null.
1897 ///
Dan Gohmanbccfc242009-09-03 15:34:35 +00001898 /// If the PHI has undef operands, but all the rest of the operands are
1899 /// some unique value, return that value if it can be proved that the
1900 /// value dominates the PHI. If DT is null, use a conservative check,
1901 /// otherwise use DT to test for dominance.
1902 ///
1903 Value *hasConstantValue(DominatorTree *DT = 0) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001904
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001905 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1906 static inline bool classof(const PHINode *) { return true; }
1907 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001908 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001909 }
1910 static inline bool classof(const Value *V) {
1911 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1912 }
Chris Lattner454928e2005-01-29 00:31:36 +00001913 private:
1914 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001915};
1916
Gabor Greifefe65362008-05-10 08:32:32 +00001917template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001918struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00001919};
1920
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001921DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00001922
1923
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001924//===----------------------------------------------------------------------===//
1925// ReturnInst Class
1926//===----------------------------------------------------------------------===//
1927
1928//===---------------------------------------------------------------------------
1929/// ReturnInst - Return a value (possibly void), from a function. Execution
1930/// does not continue in this function any longer.
1931///
1932class ReturnInst : public TerminatorInst {
Chris Lattner910c80a2007-02-24 00:55:48 +00001933 ReturnInst(const ReturnInst &RI);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001934
Gabor Greif051a9502008-04-06 20:25:17 +00001935private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001936 // ReturnInst constructors:
1937 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001938 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001939 // ReturnInst(Value* X) - 'ret X' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00001940 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001941 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
Gabor Greifefe65362008-05-10 08:32:32 +00001942 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1943 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001944 //
1945 // NOTE: If the Value* passed is of type void then the constructor behaves as
1946 // if it was passed NULL.
Owen Anderson1d0be152009-08-13 21:58:54 +00001947 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1948 Instruction *InsertBefore = 0);
1949 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1950 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001951protected:
1952 virtual ReturnInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001953public:
Owen Anderson1d0be152009-08-13 21:58:54 +00001954 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1955 Instruction *InsertBefore = 0) {
1956 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001957 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001958 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1959 BasicBlock *InsertAtEnd) {
1960 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001961 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001962 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1963 return new(0) ReturnInst(C, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001964 }
Devang Patel57ef4f42008-02-23 00:35:18 +00001965 virtual ~ReturnInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001966
Gabor Greifefe65362008-05-10 08:32:32 +00001967 /// Provide fast operand accessors
1968 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Devang Patel64d4e612008-02-26 17:56:20 +00001969
Gabor Greifefe65362008-05-10 08:32:32 +00001970 /// Convenience accessor
Devang Patel1eafa062008-03-11 17:35:03 +00001971 Value *getReturnValue(unsigned n = 0) const {
Gabor Greifefe65362008-05-10 08:32:32 +00001972 return n < getNumOperands()
1973 ? getOperand(n)
1974 : 0;
Devang Patel1eafa062008-03-11 17:35:03 +00001975 }
1976
Chris Lattner454928e2005-01-29 00:31:36 +00001977 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001978
1979 // Methods for support type inquiry through isa, cast, and dyn_cast:
1980 static inline bool classof(const ReturnInst *) { return true; }
1981 static inline bool classof(const Instruction *I) {
1982 return (I->getOpcode() == Instruction::Ret);
1983 }
1984 static inline bool classof(const Value *V) {
1985 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1986 }
Chris Lattner454928e2005-01-29 00:31:36 +00001987 private:
1988 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1989 virtual unsigned getNumSuccessorsV() const;
1990 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001991};
1992
Gabor Greifefe65362008-05-10 08:32:32 +00001993template <>
Gabor Greif138acfe2010-03-16 10:59:48 +00001994struct OperandTraits<ReturnInst> : public VariadicOperandTraits<> {
Gabor Greifefe65362008-05-10 08:32:32 +00001995};
1996
1997DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00001998
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001999//===----------------------------------------------------------------------===//
2000// BranchInst Class
2001//===----------------------------------------------------------------------===//
2002
2003//===---------------------------------------------------------------------------
2004/// BranchInst - Conditional or Unconditional Branch instruction.
2005///
2006class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00002007 /// Ops list - Branches are strange. The operands are ordered:
Gabor Greifae5a20a2009-03-12 18:34:49 +00002008 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2009 /// they don't have to check for cond/uncond branchness. These are mostly
2010 /// accessed relative from op_end().
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002011 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00002012 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002013 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2014 // BranchInst(BB *B) - 'br B'
2015 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2016 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2017 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2018 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2019 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00002020 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002021 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002022 Instruction *InsertBefore = 0);
2023 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002024 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002025 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002026protected:
2027 virtual BranchInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002028public:
2029 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
Gabor Greifae5a20a2009-03-12 18:34:49 +00002030 return new(1, true) BranchInst(IfTrue, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002031 }
Gabor Greifefe65362008-05-10 08:32:32 +00002032 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2033 Value *Cond, Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002034 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2035 }
2036 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
Gabor Greifae5a20a2009-03-12 18:34:49 +00002037 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002038 }
Gabor Greifefe65362008-05-10 08:32:32 +00002039 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2040 Value *Cond, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002041 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2042 }
Chris Lattner454928e2005-01-29 00:31:36 +00002043
Gabor Greifae5a20a2009-03-12 18:34:49 +00002044 ~BranchInst();
Gabor Greifefe65362008-05-10 08:32:32 +00002045
Chris Lattner454928e2005-01-29 00:31:36 +00002046 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00002047 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002048
Devang Patel4d4a5e02008-02-23 01:11:02 +00002049 bool isUnconditional() const { return getNumOperands() == 1; }
2050 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002051
Devang Patel4d4a5e02008-02-23 01:11:02 +00002052 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002053 assert(isConditional() && "Cannot get condition of an uncond branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002054 return Op<-3>();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002055 }
2056
2057 void setCondition(Value *V) {
2058 assert(isConditional() && "Cannot set condition of unconditional branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002059 Op<-3>() = V;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002060 }
2061
2062 // setUnconditionalDest - Change the current branch to an unconditional branch
2063 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +00002064 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002065 void setUnconditionalDest(BasicBlock *Dest) {
Chris Lattner4b122932009-10-27 16:49:53 +00002066 Op<-1>() = (Value*)Dest;
Chris Lattner454928e2005-01-29 00:31:36 +00002067 if (isConditional()) { // Convert this to an uncond branch.
Gabor Greifae5a20a2009-03-12 18:34:49 +00002068 Op<-2>() = 0;
2069 Op<-3>() = 0;
Chris Lattner454928e2005-01-29 00:31:36 +00002070 NumOperands = 1;
Gabor Greifae5a20a2009-03-12 18:34:49 +00002071 OperandList = op_begin();
Chris Lattner454928e2005-01-29 00:31:36 +00002072 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002073 }
2074
Chris Lattner454928e2005-01-29 00:31:36 +00002075 unsigned getNumSuccessors() const { return 1+isConditional(); }
2076
2077 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002078 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002079 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002080 }
2081
Chris Lattner454928e2005-01-29 00:31:36 +00002082 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002083 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner4b122932009-10-27 16:49:53 +00002084 *(&Op<-1>() - idx) = (Value*)NewSucc;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002085 }
2086
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002087 // Methods for support type inquiry through isa, cast, and dyn_cast:
2088 static inline bool classof(const BranchInst *) { return true; }
2089 static inline bool classof(const Instruction *I) {
2090 return (I->getOpcode() == Instruction::Br);
2091 }
2092 static inline bool classof(const Value *V) {
2093 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2094 }
Chris Lattner454928e2005-01-29 00:31:36 +00002095private:
2096 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2097 virtual unsigned getNumSuccessorsV() const;
2098 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002099};
2100
Gabor Greifefe65362008-05-10 08:32:32 +00002101template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002102struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
Gabor Greifefe65362008-05-10 08:32:32 +00002103
2104DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2105
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002106//===----------------------------------------------------------------------===//
2107// SwitchInst Class
2108//===----------------------------------------------------------------------===//
2109
2110//===---------------------------------------------------------------------------
2111/// SwitchInst - Multiway switch
2112///
2113class SwitchInst : public TerminatorInst {
Gabor Greifefe65362008-05-10 08:32:32 +00002114 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00002115 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002116 // Operand[0] = Value to switch on
2117 // Operand[1] = Default basic block destination
2118 // Operand[2n ] = Value to match
2119 // Operand[2n+1] = BasicBlock to go to on match
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002120 SwitchInst(const SwitchInst &SI);
Chris Lattner454928e2005-01-29 00:31:36 +00002121 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2122 void resizeOperands(unsigned No);
Gabor Greifefe65362008-05-10 08:32:32 +00002123 // allocate space for exactly zero operands
2124 void *operator new(size_t s) {
2125 return User::operator new(s, 0);
2126 }
Chris Lattner454928e2005-01-29 00:31:36 +00002127 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2128 /// switch on and a default destination. The number of additional cases can
2129 /// be specified here to make memory allocation more efficient. This
2130 /// constructor can also autoinsert before another instruction.
2131 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002132 Instruction *InsertBefore);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002133
Chris Lattner454928e2005-01-29 00:31:36 +00002134 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2135 /// switch on and a default destination. The number of additional cases can
2136 /// be specified here to make memory allocation more efficient. This
2137 /// constructor also autoinserts at the end of the specified BasicBlock.
2138 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002139 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002140protected:
2141 virtual SwitchInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002142public:
Gabor Greifefe65362008-05-10 08:32:32 +00002143 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2144 unsigned NumCases, Instruction *InsertBefore = 0) {
2145 return new SwitchInst(Value, Default, NumCases, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002146 }
Gabor Greifefe65362008-05-10 08:32:32 +00002147 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2148 unsigned NumCases, BasicBlock *InsertAtEnd) {
2149 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002150 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002151 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00002152
Gabor Greifefe65362008-05-10 08:32:32 +00002153 /// Provide fast operand accessors
2154 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2155
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002156 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00002157 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00002158 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00002159
Devang Patel4d4a5e02008-02-23 01:11:02 +00002160 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002161 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002162 }
2163
2164 /// getNumCases - return the number of 'cases' in this switch instruction.
2165 /// Note that case #0 is always the default case.
2166 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002167 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002168 }
2169
2170 /// getCaseValue - Return the specified case value. Note that case #0, the
2171 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002172 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002173 assert(i && i < getNumCases() && "Illegal case value to get!");
2174 return getSuccessorValue(i);
2175 }
2176
2177 /// getCaseValue - Return the specified case value. Note that case #0, the
2178 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002179 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002180 assert(i && i < getNumCases() && "Illegal case value to get!");
2181 return getSuccessorValue(i);
2182 }
2183
2184 /// findCaseValue - Search all of the case values for the specified constant.
2185 /// If it is explicitly handled, return the case number of it, otherwise
2186 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002187 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002188 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2189 if (getCaseValue(i) == C)
2190 return i;
2191 return 0;
2192 }
2193
Nick Lewycky011f1842006-09-18 19:03:59 +00002194 /// findCaseDest - Finds the unique case value for a given successor. Returns
2195 /// null if the successor is not found, not unique, or is the default case.
2196 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00002197 if (BB == getDefaultDest()) return NULL;
2198
Nick Lewycky011f1842006-09-18 19:03:59 +00002199 ConstantInt *CI = NULL;
2200 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2201 if (getSuccessor(i) == BB) {
2202 if (CI) return NULL; // Multiple cases lead to BB.
2203 else CI = getCaseValue(i);
2204 }
2205 }
2206 return CI;
2207 }
2208
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002209 /// addCase - Add an entry to the switch instruction...
2210 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00002211 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002212
2213 /// removeCase - This method removes the specified successor from the switch
2214 /// instruction. Note that this cannot be used to remove the default
2215 /// destination (successor #0).
2216 ///
2217 void removeCase(unsigned idx);
2218
Chris Lattner454928e2005-01-29 00:31:36 +00002219 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2220 BasicBlock *getSuccessor(unsigned idx) const {
2221 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2222 return cast<BasicBlock>(getOperand(idx*2+1));
2223 }
2224 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002225 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Chris Lattner4b122932009-10-27 16:49:53 +00002226 setOperand(idx*2+1, (Value*)NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002227 }
2228
2229 // getSuccessorValue - Return the value associated with the specified
2230 // successor.
Devang Patel4d4a5e02008-02-23 01:11:02 +00002231 ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002232 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00002233 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002234 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002235
2236 // Methods for support type inquiry through isa, cast, and dyn_cast:
2237 static inline bool classof(const SwitchInst *) { return true; }
2238 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00002239 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002240 }
2241 static inline bool classof(const Value *V) {
2242 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2243 }
Chris Lattner454928e2005-01-29 00:31:36 +00002244private:
2245 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2246 virtual unsigned getNumSuccessorsV() const;
2247 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002248};
2249
Gabor Greifefe65362008-05-10 08:32:32 +00002250template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002251struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00002252};
2253
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002254DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002255
2256
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002257//===----------------------------------------------------------------------===//
Chris Lattnerab21db72009-10-28 00:19:10 +00002258// IndirectBrInst Class
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002259//===----------------------------------------------------------------------===//
2260
2261//===---------------------------------------------------------------------------
Chris Lattnerab21db72009-10-28 00:19:10 +00002262/// IndirectBrInst - Indirect Branch Instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002263///
Chris Lattnerab21db72009-10-28 00:19:10 +00002264class IndirectBrInst : public TerminatorInst {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002265 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2266 unsigned ReservedSpace;
2267 // Operand[0] = Value to switch on
2268 // Operand[1] = Default basic block destination
2269 // Operand[2n ] = Value to match
2270 // Operand[2n+1] = BasicBlock to go to on match
Chris Lattnerab21db72009-10-28 00:19:10 +00002271 IndirectBrInst(const IndirectBrInst &IBI);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002272 void init(Value *Address, unsigned NumDests);
2273 void resizeOperands(unsigned No);
2274 // allocate space for exactly zero operands
2275 void *operator new(size_t s) {
2276 return User::operator new(s, 0);
2277 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002278 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2279 /// Address to jump to. The number of expected destinations can be specified
2280 /// here to make memory allocation more efficient. This constructor can also
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002281 /// autoinsert before another instruction.
Chris Lattnerab21db72009-10-28 00:19:10 +00002282 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002283
Chris Lattnerab21db72009-10-28 00:19:10 +00002284 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2285 /// Address to jump to. The number of expected destinations can be specified
2286 /// here to make memory allocation more efficient. This constructor also
2287 /// autoinserts at the end of the specified BasicBlock.
2288 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002289protected:
Chris Lattnerab21db72009-10-28 00:19:10 +00002290 virtual IndirectBrInst *clone_impl() const;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002291public:
Chris Lattnerab21db72009-10-28 00:19:10 +00002292 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2293 Instruction *InsertBefore = 0) {
2294 return new IndirectBrInst(Address, NumDests, InsertBefore);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002295 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002296 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2297 BasicBlock *InsertAtEnd) {
2298 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002299 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002300 ~IndirectBrInst();
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002301
2302 /// Provide fast operand accessors.
2303 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2304
Chris Lattnerab21db72009-10-28 00:19:10 +00002305 // Accessor Methods for IndirectBrInst instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002306 Value *getAddress() { return getOperand(0); }
2307 const Value *getAddress() const { return getOperand(0); }
2308 void setAddress(Value *V) { setOperand(0, V); }
2309
2310
2311 /// getNumDestinations - return the number of possible destinations in this
Chris Lattnerab21db72009-10-28 00:19:10 +00002312 /// indirectbr instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002313 unsigned getNumDestinations() const { return getNumOperands()-1; }
2314
2315 /// getDestination - Return the specified destination.
2316 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2317 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2318
2319 /// addDestination - Add a destination.
2320 ///
2321 void addDestination(BasicBlock *Dest);
2322
2323 /// removeDestination - This method removes the specified successor from the
Chris Lattnerab21db72009-10-28 00:19:10 +00002324 /// indirectbr instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002325 void removeDestination(unsigned i);
2326
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002327 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2328 BasicBlock *getSuccessor(unsigned i) const {
2329 return cast<BasicBlock>(getOperand(i+1));
2330 }
2331 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2332 setOperand(i+1, (Value*)NewSucc);
2333 }
2334
2335 // Methods for support type inquiry through isa, cast, and dyn_cast:
Chris Lattnerab21db72009-10-28 00:19:10 +00002336 static inline bool classof(const IndirectBrInst *) { return true; }
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002337 static inline bool classof(const Instruction *I) {
Chris Lattnerab21db72009-10-28 00:19:10 +00002338 return I->getOpcode() == Instruction::IndirectBr;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002339 }
2340 static inline bool classof(const Value *V) {
2341 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2342 }
2343private:
2344 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2345 virtual unsigned getNumSuccessorsV() const;
2346 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2347};
2348
2349template <>
Chris Lattnerab21db72009-10-28 00:19:10 +00002350struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002351};
2352
Chris Lattnerab21db72009-10-28 00:19:10 +00002353DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002354
2355
2356//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002357// InvokeInst Class
2358//===----------------------------------------------------------------------===//
2359
Chris Lattner3340ffe2005-05-06 20:26:26 +00002360/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2361/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002362///
2363class InvokeInst : public TerminatorInst {
Devang Patel05988662008-09-25 21:00:45 +00002364 AttrListPtr AttributeList;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002365 InvokeInst(const InvokeInst &BI);
2366 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerd2dd1502007-02-13 01:04:01 +00002367 Value* const *Args, unsigned NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00002368
2369 template<typename InputIterator>
2370 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2371 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002372 const Twine &NameStr,
David Greenef1355a52007-08-27 19:04:21 +00002373 // This argument ensures that we have an iterator we can
2374 // do arithmetic on in constant time
2375 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002376 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002377
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002378 // This requires that the iterator points to contiguous memory.
2379 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
Evan Cheng1bf9a182008-07-24 00:08:56 +00002380 setName(NameStr);
David Greenef1355a52007-08-27 19:04:21 +00002381 }
2382
David Greenef1355a52007-08-27 19:04:21 +00002383 /// Construct an InvokeInst given a range of arguments.
2384 /// InputIterator must be a random-access iterator pointing to
2385 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2386 /// made for random-accessness but not for contiguous storage as
2387 /// that would incur runtime overhead.
2388 ///
2389 /// @brief Construct an InvokeInst from a range of arguments
2390 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002391 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2392 InputIterator ArgBegin, InputIterator ArgEnd,
2393 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002394 const Twine &NameStr, Instruction *InsertBefore);
David Greenef1355a52007-08-27 19:04:21 +00002395
2396 /// Construct an InvokeInst given a range of arguments.
2397 /// InputIterator must be a random-access iterator pointing to
2398 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2399 /// made for random-accessness but not for contiguous storage as
2400 /// that would incur runtime overhead.
2401 ///
2402 /// @brief Construct an InvokeInst from a range of arguments
2403 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002404 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2405 InputIterator ArgBegin, InputIterator ArgEnd,
2406 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002407 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002408protected:
2409 virtual InvokeInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002410public:
2411 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002412 static InvokeInst *Create(Value *Func,
2413 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002414 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002415 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00002416 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +00002417 unsigned Values(ArgEnd - ArgBegin + 3);
2418 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002419 Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002420 }
2421 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002422 static InvokeInst *Create(Value *Func,
2423 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002424 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002425 const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002426 BasicBlock *InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00002427 unsigned Values(ArgEnd - ArgBegin + 3);
2428 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002429 Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002430 }
David Greenef1355a52007-08-27 19:04:21 +00002431
Gabor Greifefe65362008-05-10 08:32:32 +00002432 /// Provide fast operand accessors
2433 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002434
Chris Lattner3340ffe2005-05-06 20:26:26 +00002435 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2436 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002437 CallingConv::ID getCallingConv() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +00002438 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002439 }
2440 void setCallingConv(CallingConv::ID CC) {
Chris Lattnerb2406d92009-12-29 02:46:09 +00002441 setInstructionSubclassData(static_cast<unsigned>(CC));
Chris Lattner3340ffe2005-05-06 20:26:26 +00002442 }
2443
Devang Patel05988662008-09-25 21:00:45 +00002444 /// getAttributes - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002445 ///
Devang Patel05988662008-09-25 21:00:45 +00002446 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002447
Devang Patel05988662008-09-25 21:00:45 +00002448 /// setAttributes - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002449 ///
Devang Patel05988662008-09-25 21:00:45 +00002450 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00002451
Devang Patel05988662008-09-25 21:00:45 +00002452 /// addAttribute - adds the attribute to the list of attributes.
2453 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002454
Devang Patel05988662008-09-25 21:00:45 +00002455 /// removeAttribute - removes the attribute from the list of attributes.
2456 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00002457
Dan Gohmanf2752502008-09-26 21:38:45 +00002458 /// @brief Determine whether the call or the callee has the given attribute.
2459 bool paramHasAttr(unsigned i, Attributes attr) const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002460
Dale Johannesen08e78b12008-02-22 17:49:45 +00002461 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002462 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00002463 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002464 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00002465
Eric Christopherf27e6082010-03-25 04:49:10 +00002466 /// @brief Return true if the call should not be inlined.
2467 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
2468 void setIsNoInline(bool Value) {
2469 if (Value) addAttribute(~0, Attribute::NoInline);
2470 else removeAttribute(~0, Attribute::NoInline);
2471 }
2472
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002473 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002474 bool doesNotAccessMemory() const {
Dan Gohmana62b5ed2009-07-17 16:12:36 +00002475 return paramHasAttr(~0, Attribute::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002476 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002477 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002478 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2479 else removeAttribute(~0, Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00002480 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002481
2482 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002483 bool onlyReadsMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00002484 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002485 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002486 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002487 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2488 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00002489 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002490
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002491 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002492 bool doesNotReturn() const {
Devang Patel19c87462008-09-26 22:53:05 +00002493 return paramHasAttr(~0, Attribute::NoReturn);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002494 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002495 void setDoesNotReturn(bool DoesNotReturn = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002496 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2497 else removeAttribute(~0, Attribute::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00002498 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002499
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002500 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002501 bool doesNotThrow() const {
Devang Patel19c87462008-09-26 22:53:05 +00002502 return paramHasAttr(~0, Attribute::NoUnwind);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002503 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002504 void setDoesNotThrow(bool DoesNotThrow = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002505 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2506 else removeAttribute(~0, Attribute::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00002507 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002508
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002509 /// @brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00002510 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002511 bool hasStructRetAttr() const {
2512 // Be friendly and also check the callee.
Devang Patel05988662008-09-25 21:00:45 +00002513 return paramHasAttr(1, Attribute::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002514 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002515
Dan Gohmanf2752502008-09-26 21:38:45 +00002516 /// @brief Determine if any call argument is an aggregate passed by value.
2517 bool hasByValArgument() const {
2518 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2519 }
2520
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002521 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00002522 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002523 ///
Chris Lattner721aef62004-11-18 17:46:57 +00002524 Function *getCalledFunction() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00002525 return dyn_cast<Function>(Op<-3>());
Chris Lattner721aef62004-11-18 17:46:57 +00002526 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002527
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002528 /// getCalledValue - Get a pointer to the function that is invoked by this
Dan Gohmanf2752502008-09-26 21:38:45 +00002529 /// instruction
Gabor Greifc9f75002010-03-24 13:21:49 +00002530 const Value *getCalledValue() const { return Op<-3>(); }
2531 Value *getCalledValue() { return Op<-3>(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002532
Gabor Greif654c06f2010-03-20 21:00:25 +00002533 /// setCalledFunction - Set the function called.
2534 void setCalledFunction(Value* Fn) {
Gabor Greifc9f75002010-03-24 13:21:49 +00002535 Op<-3>() = Fn;
Gabor Greif654c06f2010-03-20 21:00:25 +00002536 }
2537
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002538 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00002539 BasicBlock *getNormalDest() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00002540 return cast<BasicBlock>(Op<-2>());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002541 }
Chris Lattner454928e2005-01-29 00:31:36 +00002542 BasicBlock *getUnwindDest() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00002543 return cast<BasicBlock>(Op<-1>());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002544 }
Chris Lattner454928e2005-01-29 00:31:36 +00002545 void setNormalDest(BasicBlock *B) {
Gabor Greifc9f75002010-03-24 13:21:49 +00002546 Op<-2>() = reinterpret_cast<Value*>(B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002547 }
Chris Lattner454928e2005-01-29 00:31:36 +00002548 void setUnwindDest(BasicBlock *B) {
Gabor Greifc9f75002010-03-24 13:21:49 +00002549 Op<-1>() = reinterpret_cast<Value*>(B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002550 }
2551
Devang Patel4d4a5e02008-02-23 01:11:02 +00002552 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002553 assert(i < 2 && "Successor # out of range for invoke!");
2554 return i == 0 ? getNormalDest() : getUnwindDest();
2555 }
2556
Chris Lattner454928e2005-01-29 00:31:36 +00002557 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002558 assert(idx < 2 && "Successor # out of range for invoke!");
Gabor Greifc9f75002010-03-24 13:21:49 +00002559 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002560 }
2561
Chris Lattner454928e2005-01-29 00:31:36 +00002562 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002563
2564 // Methods for support type inquiry through isa, cast, and dyn_cast:
2565 static inline bool classof(const InvokeInst *) { return true; }
2566 static inline bool classof(const Instruction *I) {
2567 return (I->getOpcode() == Instruction::Invoke);
2568 }
2569 static inline bool classof(const Value *V) {
2570 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2571 }
Gabor Greifc9f75002010-03-24 13:21:49 +00002572
Chris Lattner454928e2005-01-29 00:31:36 +00002573private:
2574 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2575 virtual unsigned getNumSuccessorsV() const;
2576 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00002577
Chris Lattnerb2406d92009-12-29 02:46:09 +00002578 // Shadow Instruction::setInstructionSubclassData with a private forwarding
2579 // method so that subclasses cannot accidentally use it.
2580 void setInstructionSubclassData(unsigned short D) {
2581 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00002582 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002583};
2584
Gabor Greifefe65362008-05-10 08:32:32 +00002585template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002586struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
Gabor Greifefe65362008-05-10 08:32:32 +00002587};
2588
2589template<typename InputIterator>
2590InvokeInst::InvokeInst(Value *Func,
2591 BasicBlock *IfNormal, BasicBlock *IfException,
2592 InputIterator ArgBegin, InputIterator ArgEnd,
2593 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002594 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00002595 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2596 ->getElementType())->getReturnType(),
2597 Instruction::Invoke,
2598 OperandTraits<InvokeInst>::op_end(this) - Values,
2599 Values, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002600 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00002601 typename std::iterator_traits<InputIterator>::iterator_category());
2602}
2603template<typename InputIterator>
2604InvokeInst::InvokeInst(Value *Func,
2605 BasicBlock *IfNormal, BasicBlock *IfException,
2606 InputIterator ArgBegin, InputIterator ArgEnd,
2607 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002608 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00002609 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2610 ->getElementType())->getReturnType(),
2611 Instruction::Invoke,
2612 OperandTraits<InvokeInst>::op_end(this) - Values,
2613 Values, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002614 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00002615 typename std::iterator_traits<InputIterator>::iterator_category());
2616}
2617
2618DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002619
2620//===----------------------------------------------------------------------===//
2621// UnwindInst Class
2622//===----------------------------------------------------------------------===//
2623
2624//===---------------------------------------------------------------------------
2625/// UnwindInst - Immediately exit the current function, unwinding the stack
2626/// until an invoke instruction is found.
2627///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002628class UnwindInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002629 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Devang Patel50b6e332009-10-27 22:16:29 +00002630protected:
2631 virtual UnwindInst *clone_impl() const;
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002632public:
Gabor Greif051a9502008-04-06 20:25:17 +00002633 // allocate space for exactly zero operands
2634 void *operator new(size_t s) {
2635 return User::operator new(s, 0);
2636 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002637 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2638 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002639
Chris Lattner454928e2005-01-29 00:31:36 +00002640 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002641
2642 // Methods for support type inquiry through isa, cast, and dyn_cast:
2643 static inline bool classof(const UnwindInst *) { return true; }
2644 static inline bool classof(const Instruction *I) {
2645 return I->getOpcode() == Instruction::Unwind;
2646 }
2647 static inline bool classof(const Value *V) {
2648 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2649 }
Chris Lattner454928e2005-01-29 00:31:36 +00002650private:
2651 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2652 virtual unsigned getNumSuccessorsV() const;
2653 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002654};
2655
Chris Lattner076b3f12004-10-16 18:05:54 +00002656//===----------------------------------------------------------------------===//
2657// UnreachableInst Class
2658//===----------------------------------------------------------------------===//
2659
2660//===---------------------------------------------------------------------------
2661/// UnreachableInst - This function has undefined behavior. In particular, the
2662/// presence of this instruction indicates some higher level knowledge that the
2663/// end of the block cannot be reached.
2664///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002665class UnreachableInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002666 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Devang Patel50b6e332009-10-27 22:16:29 +00002667protected:
2668 virtual UnreachableInst *clone_impl() const;
2669
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002670public:
Gabor Greif051a9502008-04-06 20:25:17 +00002671 // allocate space for exactly zero operands
2672 void *operator new(size_t s) {
2673 return User::operator new(s, 0);
2674 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002675 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2676 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00002677
Chris Lattner454928e2005-01-29 00:31:36 +00002678 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00002679
2680 // Methods for support type inquiry through isa, cast, and dyn_cast:
2681 static inline bool classof(const UnreachableInst *) { return true; }
2682 static inline bool classof(const Instruction *I) {
2683 return I->getOpcode() == Instruction::Unreachable;
2684 }
2685 static inline bool classof(const Value *V) {
2686 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2687 }
Chris Lattner454928e2005-01-29 00:31:36 +00002688private:
2689 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2690 virtual unsigned getNumSuccessorsV() const;
2691 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00002692};
2693
Reid Spencer3da59db2006-11-27 01:05:10 +00002694//===----------------------------------------------------------------------===//
2695// TruncInst Class
2696//===----------------------------------------------------------------------===//
2697
2698/// @brief This class represents a truncation of integer types.
2699class TruncInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002700protected:
2701 /// @brief Clone an identical TruncInst
2702 virtual TruncInst *clone_impl() const;
2703
Reid Spencer3da59db2006-11-27 01:05:10 +00002704public:
2705 /// @brief Constructor with insert-before-instruction semantics
2706 TruncInst(
2707 Value *S, ///< The value to be truncated
2708 const Type *Ty, ///< The (smaller) type to truncate to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002709 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002710 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2711 );
2712
2713 /// @brief Constructor with insert-at-end-of-block semantics
2714 TruncInst(
2715 Value *S, ///< The value to be truncated
2716 const Type *Ty, ///< The (smaller) type to truncate to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002717 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002718 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2719 );
2720
Reid Spencer3da59db2006-11-27 01:05:10 +00002721 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2722 static inline bool classof(const TruncInst *) { return true; }
2723 static inline bool classof(const Instruction *I) {
2724 return I->getOpcode() == Trunc;
2725 }
2726 static inline bool classof(const Value *V) {
2727 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2728 }
2729};
2730
2731//===----------------------------------------------------------------------===//
2732// ZExtInst Class
2733//===----------------------------------------------------------------------===//
2734
2735/// @brief This class represents zero extension of integer types.
2736class ZExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002737protected:
2738 /// @brief Clone an identical ZExtInst
2739 virtual ZExtInst *clone_impl() const;
2740
Reid Spencer3da59db2006-11-27 01:05:10 +00002741public:
2742 /// @brief Constructor with insert-before-instruction semantics
2743 ZExtInst(
2744 Value *S, ///< The value to be zero extended
2745 const Type *Ty, ///< The type to zero extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002746 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002747 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2748 );
2749
2750 /// @brief Constructor with insert-at-end semantics.
2751 ZExtInst(
2752 Value *S, ///< The value to be zero extended
2753 const Type *Ty, ///< The type to zero extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002754 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002755 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2756 );
2757
Reid Spencer3da59db2006-11-27 01:05:10 +00002758 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2759 static inline bool classof(const ZExtInst *) { return true; }
2760 static inline bool classof(const Instruction *I) {
2761 return I->getOpcode() == ZExt;
2762 }
2763 static inline bool classof(const Value *V) {
2764 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2765 }
2766};
2767
2768//===----------------------------------------------------------------------===//
2769// SExtInst Class
2770//===----------------------------------------------------------------------===//
2771
2772/// @brief This class represents a sign extension of integer types.
2773class SExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002774protected:
2775 /// @brief Clone an identical SExtInst
2776 virtual SExtInst *clone_impl() const;
2777
Reid Spencer3da59db2006-11-27 01:05:10 +00002778public:
2779 /// @brief Constructor with insert-before-instruction semantics
2780 SExtInst(
2781 Value *S, ///< The value to be sign extended
2782 const Type *Ty, ///< The type to sign extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002783 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002784 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2785 );
2786
2787 /// @brief Constructor with insert-at-end-of-block semantics
2788 SExtInst(
2789 Value *S, ///< The value to be sign extended
2790 const Type *Ty, ///< The type to sign extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002791 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002792 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2793 );
2794
Reid Spencer3da59db2006-11-27 01:05:10 +00002795 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2796 static inline bool classof(const SExtInst *) { return true; }
2797 static inline bool classof(const Instruction *I) {
2798 return I->getOpcode() == SExt;
2799 }
2800 static inline bool classof(const Value *V) {
2801 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2802 }
2803};
2804
2805//===----------------------------------------------------------------------===//
2806// FPTruncInst Class
2807//===----------------------------------------------------------------------===//
2808
2809/// @brief This class represents a truncation of floating point types.
2810class FPTruncInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002811protected:
2812 /// @brief Clone an identical FPTruncInst
2813 virtual FPTruncInst *clone_impl() const;
2814
Reid Spencer3da59db2006-11-27 01:05:10 +00002815public:
2816 /// @brief Constructor with insert-before-instruction semantics
2817 FPTruncInst(
2818 Value *S, ///< The value to be truncated
2819 const Type *Ty, ///< The type to truncate to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002820 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002821 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2822 );
2823
2824 /// @brief Constructor with insert-before-instruction semantics
2825 FPTruncInst(
2826 Value *S, ///< The value to be truncated
2827 const Type *Ty, ///< The type to truncate to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002828 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002829 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2830 );
2831
Reid Spencer3da59db2006-11-27 01:05:10 +00002832 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2833 static inline bool classof(const FPTruncInst *) { return true; }
2834 static inline bool classof(const Instruction *I) {
2835 return I->getOpcode() == FPTrunc;
2836 }
2837 static inline bool classof(const Value *V) {
2838 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2839 }
2840};
2841
2842//===----------------------------------------------------------------------===//
2843// FPExtInst Class
2844//===----------------------------------------------------------------------===//
2845
2846/// @brief This class represents an extension of floating point types.
2847class FPExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002848protected:
2849 /// @brief Clone an identical FPExtInst
2850 virtual FPExtInst *clone_impl() const;
2851
Reid Spencer3da59db2006-11-27 01:05:10 +00002852public:
2853 /// @brief Constructor with insert-before-instruction semantics
2854 FPExtInst(
2855 Value *S, ///< The value to be extended
2856 const Type *Ty, ///< The type to extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002857 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002858 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2859 );
2860
2861 /// @brief Constructor with insert-at-end-of-block semantics
2862 FPExtInst(
2863 Value *S, ///< The value to be extended
2864 const Type *Ty, ///< The type to extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002865 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002866 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2867 );
2868
Reid Spencer3da59db2006-11-27 01:05:10 +00002869 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2870 static inline bool classof(const FPExtInst *) { return true; }
2871 static inline bool classof(const Instruction *I) {
2872 return I->getOpcode() == FPExt;
2873 }
2874 static inline bool classof(const Value *V) {
2875 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2876 }
2877};
2878
2879//===----------------------------------------------------------------------===//
2880// UIToFPInst Class
2881//===----------------------------------------------------------------------===//
2882
2883/// @brief This class represents a cast unsigned integer to floating point.
2884class UIToFPInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002885protected:
2886 /// @brief Clone an identical UIToFPInst
2887 virtual UIToFPInst *clone_impl() const;
2888
Reid Spencer3da59db2006-11-27 01:05:10 +00002889public:
2890 /// @brief Constructor with insert-before-instruction semantics
2891 UIToFPInst(
2892 Value *S, ///< The value to be converted
2893 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002894 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002895 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2896 );
2897
2898 /// @brief Constructor with insert-at-end-of-block semantics
2899 UIToFPInst(
2900 Value *S, ///< The value to be converted
2901 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002902 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002903 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2904 );
2905
Reid Spencer3da59db2006-11-27 01:05:10 +00002906 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2907 static inline bool classof(const UIToFPInst *) { return true; }
2908 static inline bool classof(const Instruction *I) {
2909 return I->getOpcode() == UIToFP;
2910 }
2911 static inline bool classof(const Value *V) {
2912 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2913 }
2914};
2915
2916//===----------------------------------------------------------------------===//
2917// SIToFPInst Class
2918//===----------------------------------------------------------------------===//
2919
2920/// @brief This class represents a cast from signed integer to floating point.
2921class SIToFPInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002922protected:
2923 /// @brief Clone an identical SIToFPInst
2924 virtual SIToFPInst *clone_impl() const;
2925
Reid Spencer3da59db2006-11-27 01:05:10 +00002926public:
2927 /// @brief Constructor with insert-before-instruction semantics
2928 SIToFPInst(
2929 Value *S, ///< The value to be converted
2930 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002931 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002932 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2933 );
2934
2935 /// @brief Constructor with insert-at-end-of-block semantics
2936 SIToFPInst(
2937 Value *S, ///< The value to be converted
2938 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002939 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002940 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2941 );
2942
Reid Spencer3da59db2006-11-27 01:05:10 +00002943 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2944 static inline bool classof(const SIToFPInst *) { return true; }
2945 static inline bool classof(const Instruction *I) {
2946 return I->getOpcode() == SIToFP;
2947 }
2948 static inline bool classof(const Value *V) {
2949 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2950 }
2951};
2952
2953//===----------------------------------------------------------------------===//
2954// FPToUIInst Class
2955//===----------------------------------------------------------------------===//
2956
2957/// @brief This class represents a cast from floating point to unsigned integer
2958class FPToUIInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002959protected:
2960 /// @brief Clone an identical FPToUIInst
2961 virtual FPToUIInst *clone_impl() const;
2962
Reid Spencer3da59db2006-11-27 01:05:10 +00002963public:
2964 /// @brief Constructor with insert-before-instruction semantics
2965 FPToUIInst(
2966 Value *S, ///< The value to be converted
2967 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002968 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002969 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2970 );
2971
2972 /// @brief Constructor with insert-at-end-of-block semantics
2973 FPToUIInst(
2974 Value *S, ///< The value to be converted
2975 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002976 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002977 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2978 );
2979
Reid Spencer3da59db2006-11-27 01:05:10 +00002980 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2981 static inline bool classof(const FPToUIInst *) { return true; }
2982 static inline bool classof(const Instruction *I) {
2983 return I->getOpcode() == FPToUI;
2984 }
2985 static inline bool classof(const Value *V) {
2986 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2987 }
2988};
2989
2990//===----------------------------------------------------------------------===//
2991// FPToSIInst Class
2992//===----------------------------------------------------------------------===//
2993
2994/// @brief This class represents a cast from floating point to signed integer.
2995class FPToSIInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00002996protected:
2997 /// @brief Clone an identical FPToSIInst
2998 virtual FPToSIInst *clone_impl() const;
2999
Reid Spencer3da59db2006-11-27 01:05:10 +00003000public:
3001 /// @brief Constructor with insert-before-instruction semantics
3002 FPToSIInst(
3003 Value *S, ///< The value to be converted
3004 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003005 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003006 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3007 );
3008
3009 /// @brief Constructor with insert-at-end-of-block semantics
3010 FPToSIInst(
3011 Value *S, ///< The value to be converted
3012 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003013 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003014 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3015 );
3016
Reid Spencer3da59db2006-11-27 01:05:10 +00003017 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3018 static inline bool classof(const FPToSIInst *) { return true; }
3019 static inline bool classof(const Instruction *I) {
3020 return I->getOpcode() == FPToSI;
3021 }
3022 static inline bool classof(const Value *V) {
3023 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3024 }
3025};
3026
3027//===----------------------------------------------------------------------===//
3028// IntToPtrInst Class
3029//===----------------------------------------------------------------------===//
3030
3031/// @brief This class represents a cast from an integer to a pointer.
3032class IntToPtrInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00003033public:
3034 /// @brief Constructor with insert-before-instruction semantics
3035 IntToPtrInst(
3036 Value *S, ///< The value to be converted
3037 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003038 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003039 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3040 );
3041
3042 /// @brief Constructor with insert-at-end-of-block semantics
3043 IntToPtrInst(
3044 Value *S, ///< The value to be converted
3045 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003046 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003047 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3048 );
3049
3050 /// @brief Clone an identical IntToPtrInst
Devang Patel50b6e332009-10-27 22:16:29 +00003051 virtual IntToPtrInst *clone_impl() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00003052
3053 // Methods for support type inquiry through isa, cast, and dyn_cast:
3054 static inline bool classof(const IntToPtrInst *) { return true; }
3055 static inline bool classof(const Instruction *I) {
3056 return I->getOpcode() == IntToPtr;
3057 }
3058 static inline bool classof(const Value *V) {
3059 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3060 }
3061};
3062
3063//===----------------------------------------------------------------------===//
3064// PtrToIntInst Class
3065//===----------------------------------------------------------------------===//
3066
3067/// @brief This class represents a cast from a pointer to an integer
3068class PtrToIntInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003069protected:
3070 /// @brief Clone an identical PtrToIntInst
3071 virtual PtrToIntInst *clone_impl() const;
3072
Reid Spencer3da59db2006-11-27 01:05:10 +00003073public:
3074 /// @brief Constructor with insert-before-instruction semantics
3075 PtrToIntInst(
3076 Value *S, ///< The value to be converted
3077 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003078 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003079 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3080 );
3081
3082 /// @brief Constructor with insert-at-end-of-block semantics
3083 PtrToIntInst(
3084 Value *S, ///< The value to be converted
3085 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003086 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003087 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3088 );
3089
Reid Spencer3da59db2006-11-27 01:05:10 +00003090 // Methods for support type inquiry through isa, cast, and dyn_cast:
3091 static inline bool classof(const PtrToIntInst *) { return true; }
3092 static inline bool classof(const Instruction *I) {
3093 return I->getOpcode() == PtrToInt;
3094 }
3095 static inline bool classof(const Value *V) {
3096 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3097 }
3098};
3099
3100//===----------------------------------------------------------------------===//
3101// BitCastInst Class
3102//===----------------------------------------------------------------------===//
3103
3104/// @brief This class represents a no-op cast from one type to another.
3105class BitCastInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003106protected:
3107 /// @brief Clone an identical BitCastInst
3108 virtual BitCastInst *clone_impl() const;
3109
Reid Spencer3da59db2006-11-27 01:05:10 +00003110public:
3111 /// @brief Constructor with insert-before-instruction semantics
3112 BitCastInst(
3113 Value *S, ///< The value to be casted
3114 const Type *Ty, ///< The type to casted to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003115 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003116 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3117 );
3118
3119 /// @brief Constructor with insert-at-end-of-block semantics
3120 BitCastInst(
3121 Value *S, ///< The value to be casted
3122 const Type *Ty, ///< The type to casted to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003123 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003124 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3125 );
3126
Reid Spencer3da59db2006-11-27 01:05:10 +00003127 // Methods for support type inquiry through isa, cast, and dyn_cast:
3128 static inline bool classof(const BitCastInst *) { return true; }
3129 static inline bool classof(const Instruction *I) {
3130 return I->getOpcode() == BitCast;
3131 }
3132 static inline bool classof(const Value *V) {
3133 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3134 }
3135};
3136
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003137} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00003138
3139#endif