blob: bed828a88551ea77b35d60e12fd89e6d121a0b3a [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"
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +000023#include "llvm/Support/IntegersSubset.h"
24#include "llvm/Support/IntegersSubsetMapping.h"
Jay Foadfc6d3a42011-07-13 10:26:04 +000025#include "llvm/ADT/ArrayRef.h"
Dan Gohman81a0c0b2008-05-31 00:58:22 +000026#include "llvm/ADT/SmallVector.h"
Eli Friedman47f35132011-07-25 23:16:38 +000027#include "llvm/Support/ErrorHandling.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000028#include <iterator>
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000029
30namespace llvm {
31
Chris Lattnerd1a32602005-02-24 05:32:09 +000032class ConstantInt;
Reid Spencer3da43842007-02-28 22:00:54 +000033class ConstantRange;
34class APInt;
Benjamin Kramer12ddd402009-08-11 17:45:13 +000035class LLVMContext;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000036
Eli Friedman47f35132011-07-25 23:16:38 +000037enum AtomicOrdering {
38 NotAtomic = 0,
39 Unordered = 1,
40 Monotonic = 2,
41 // Consume = 3, // Not specified yet.
42 Acquire = 4,
43 Release = 5,
44 AcquireRelease = 6,
45 SequentiallyConsistent = 7
46};
47
48enum SynchronizationScope {
49 SingleThread = 0,
50 CrossThread = 1
51};
52
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000053//===----------------------------------------------------------------------===//
Victor Hernandez7b929da2009-10-23 21:09:37 +000054// AllocaInst Class
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000055//===----------------------------------------------------------------------===//
56
Victor Hernandez7b929da2009-10-23 21:09:37 +000057/// AllocaInst - an instruction to allocate memory on the stack
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000058///
Victor Hernandez7b929da2009-10-23 21:09:37 +000059class AllocaInst : public UnaryInstruction {
Devang Patel50b6e332009-10-27 22:16:29 +000060protected:
61 virtual AllocaInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000062public:
Chris Lattnerdb125cf2011-07-18 04:54:35 +000063 explicit AllocaInst(Type *Ty, Value *ArraySize = 0,
Victor Hernandez7b929da2009-10-23 21:09:37 +000064 const Twine &Name = "", Instruction *InsertBefore = 0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000065 AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez7b929da2009-10-23 21:09:37 +000066 const Twine &Name, BasicBlock *InsertAtEnd);
67
Chris Lattnerdb125cf2011-07-18 04:54:35 +000068 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
69 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
Victor Hernandez7b929da2009-10-23 21:09:37 +000070
Chris Lattnerdb125cf2011-07-18 04:54:35 +000071 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez7b929da2009-10-23 21:09:37 +000072 const Twine &Name = "", Instruction *InsertBefore = 0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000073 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez7b929da2009-10-23 21:09:37 +000074 const Twine &Name, BasicBlock *InsertAtEnd);
75
Gabor Greif051a9502008-04-06 20:25:17 +000076 // Out of line virtual method, so the vtable, etc. has a home.
Victor Hernandez7b929da2009-10-23 21:09:37 +000077 virtual ~AllocaInst();
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000078
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000079 /// isArrayAllocation - Return true if there is an allocation size parameter
80 /// to the allocation instruction that is not 1.
81 ///
82 bool isArrayAllocation() const;
83
Dan Gohman18476ee2009-07-07 20:47:48 +000084 /// getArraySize - Get the number of elements allocated. For a simple
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000085 /// allocation of a single element, this will return a constant 1 value.
86 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000087 const Value *getArraySize() const { return getOperand(0); }
88 Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000089
90 /// getType - Overload to return most specific pointer type
91 ///
Chris Lattnerdb125cf2011-07-18 04:54:35 +000092 PointerType *getType() const {
93 return reinterpret_cast<PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000094 }
95
96 /// getAllocatedType - Return the type that is being allocated by the
97 /// instruction.
98 ///
Chris Lattner1afcace2011-07-09 17:41:24 +000099 Type *getAllocatedType() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000100
Nate Begeman14b05292005-11-05 09:21:28 +0000101 /// getAlignment - Return the alignment of the memory that is being allocated
102 /// by the instruction.
103 ///
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000104 unsigned getAlignment() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000105 return (1u << getSubclassDataFromInstruction()) >> 1;
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000106 }
Dan Gohman52837072008-03-24 16:55:58 +0000107 void setAlignment(unsigned Align);
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000108
Chris Lattnerc5dd22a2008-11-26 02:54:17 +0000109 /// isStaticAlloca - Return true if this alloca is in the entry block of the
110 /// function and is a constant size. If so, the code generator will fold it
111 /// into the prolog/epilog code, so it is basically free.
112 bool isStaticAlloca() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000113
114 // Methods for support type inquiry through isa, cast, and dyn_cast:
115 static inline bool classof(const AllocaInst *) { return true; }
116 static inline bool classof(const Instruction *I) {
117 return (I->getOpcode() == Instruction::Alloca);
118 }
119 static inline bool classof(const Value *V) {
120 return isa<Instruction>(V) && classof(cast<Instruction>(V));
121 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000122private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000123 // Shadow Instruction::setInstructionSubclassData with a private forwarding
124 // method so that subclasses cannot accidentally use it.
125 void setInstructionSubclassData(unsigned short D) {
126 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000127 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000128};
129
130
131//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000132// LoadInst Class
133//===----------------------------------------------------------------------===//
134
Chris Lattner88fe29a2005-02-05 01:44:18 +0000135/// LoadInst - an instruction for reading from memory. This uses the
136/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000137///
Chris Lattner454928e2005-01-29 00:31:36 +0000138class LoadInst : public UnaryInstruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000139 void AssertOK();
Devang Patel50b6e332009-10-27 22:16:29 +0000140protected:
141 virtual LoadInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000142public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000143 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
144 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000145 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
Christopher Lamb43c7f372007-04-22 19:24:39 +0000146 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000147 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000148 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000149 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Eli Friedman21006d42011-08-09 23:02:53 +0000150 unsigned Align, Instruction *InsertBefore = 0);
151 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000152 unsigned Align, BasicBlock *InsertAtEnd);
Eli Friedman21006d42011-08-09 23:02:53 +0000153 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
154 unsigned Align, AtomicOrdering Order,
155 SynchronizationScope SynchScope = CrossThread,
156 Instruction *InsertBefore = 0);
157 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
158 unsigned Align, AtomicOrdering Order,
159 SynchronizationScope SynchScope,
160 BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000161
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000162 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
163 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
164 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
165 bool isVolatile = false, Instruction *InsertBefore = 0);
166 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
167 BasicBlock *InsertAtEnd);
168
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000169 /// isVolatile - Return true if this is a load from a volatile memory
170 /// location.
171 ///
Chris Lattnerb2406d92009-12-29 02:46:09 +0000172 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000173
174 /// setVolatile - Specify whether this is a volatile load or not.
175 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000176 void setVolatile(bool V) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000177 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
178 (V ? 1 : 0));
Christopher Lamb43c7f372007-04-22 19:24:39 +0000179 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000180
Christopher Lamb43c7f372007-04-22 19:24:39 +0000181 /// getAlignment - Return the alignment of the access that is being performed
182 ///
183 unsigned getAlignment() const {
Eli Friedman21006d42011-08-09 23:02:53 +0000184 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000185 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000186
Christopher Lamb43c7f372007-04-22 19:24:39 +0000187 void setAlignment(unsigned Align);
188
Eli Friedman21006d42011-08-09 23:02:53 +0000189 /// Returns the ordering effect of this fence.
190 AtomicOrdering getOrdering() const {
191 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
192 }
193
194 /// Set the ordering constraint on this load. May not be Release or
195 /// AcquireRelease.
196 void setOrdering(AtomicOrdering Ordering) {
197 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
198 (Ordering << 7));
199 }
200
201 SynchronizationScope getSynchScope() const {
202 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
203 }
204
205 /// Specify whether this load is ordered with respect to all
206 /// concurrently executing threads, or only with respect to signal handlers
207 /// executing in the same thread.
208 void setSynchScope(SynchronizationScope xthread) {
209 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
210 (xthread << 6));
211 }
212
213 bool isAtomic() const { return getOrdering() != NotAtomic; }
214 void setAtomic(AtomicOrdering Ordering,
215 SynchronizationScope SynchScope = CrossThread) {
216 setOrdering(Ordering);
217 setSynchScope(SynchScope);
218 }
219
220 bool isSimple() const { return !isAtomic() && !isVolatile(); }
221 bool isUnordered() const {
222 return getOrdering() <= Unordered && !isVolatile();
223 }
224
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000225 Value *getPointerOperand() { return getOperand(0); }
226 const Value *getPointerOperand() const { return getOperand(0); }
227 static unsigned getPointerOperandIndex() { return 0U; }
228
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000229 unsigned getPointerAddressSpace() const {
230 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
231 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000232
233
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000234 // Methods for support type inquiry through isa, cast, and dyn_cast:
235 static inline bool classof(const LoadInst *) { return true; }
236 static inline bool classof(const Instruction *I) {
237 return I->getOpcode() == Instruction::Load;
238 }
239 static inline bool classof(const Value *V) {
240 return isa<Instruction>(V) && classof(cast<Instruction>(V));
241 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000242private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000243 // Shadow Instruction::setInstructionSubclassData with a private forwarding
244 // method so that subclasses cannot accidentally use it.
245 void setInstructionSubclassData(unsigned short D) {
246 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000247 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000248};
249
250
251//===----------------------------------------------------------------------===//
252// StoreInst Class
253//===----------------------------------------------------------------------===//
254
Misha Brukman9769ab22005-04-21 20:19:05 +0000255/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000256///
257class StoreInst : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +0000258 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Chris Lattner454928e2005-01-29 00:31:36 +0000259 void AssertOK();
Devang Patel50b6e332009-10-27 22:16:29 +0000260protected:
261 virtual StoreInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000262public:
Gabor Greif051a9502008-04-06 20:25:17 +0000263 // allocate space for exactly two operands
264 void *operator new(size_t s) {
265 return User::operator new(s, 2);
266 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000267 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
268 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
269 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
270 Instruction *InsertBefore = 0);
271 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000272 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
Eli Friedman21006d42011-08-09 23:02:53 +0000273 unsigned Align, Instruction *InsertBefore = 0);
274 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
Dan Gohman6ab2d182007-07-18 20:51:11 +0000275 unsigned Align, BasicBlock *InsertAtEnd);
Eli Friedman21006d42011-08-09 23:02:53 +0000276 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
277 unsigned Align, AtomicOrdering Order,
278 SynchronizationScope SynchScope = CrossThread,
279 Instruction *InsertBefore = 0);
280 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
281 unsigned Align, AtomicOrdering Order,
282 SynchronizationScope SynchScope,
283 BasicBlock *InsertAtEnd);
284
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000285
Eli Friedman21006d42011-08-09 23:02:53 +0000286 /// isVolatile - Return true if this is a store to a volatile memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000287 /// location.
288 ///
Chris Lattnerb2406d92009-12-29 02:46:09 +0000289 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000290
Eli Friedman21006d42011-08-09 23:02:53 +0000291 /// setVolatile - Specify whether this is a volatile store or not.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000292 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000293 void setVolatile(bool V) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000294 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
295 (V ? 1 : 0));
Christopher Lamb43c7f372007-04-22 19:24:39 +0000296 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000297
Chris Lattner454928e2005-01-29 00:31:36 +0000298 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +0000299 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner454928e2005-01-29 00:31:36 +0000300
Christopher Lamb43c7f372007-04-22 19:24:39 +0000301 /// getAlignment - Return the alignment of the access that is being performed
302 ///
303 unsigned getAlignment() const {
Eli Friedman21006d42011-08-09 23:02:53 +0000304 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000305 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000306
Christopher Lamb43c7f372007-04-22 19:24:39 +0000307 void setAlignment(unsigned Align);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000308
Eli Friedman21006d42011-08-09 23:02:53 +0000309 /// Returns the ordering effect of this store.
310 AtomicOrdering getOrdering() const {
311 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
312 }
313
314 /// Set the ordering constraint on this store. May not be Acquire or
315 /// AcquireRelease.
316 void setOrdering(AtomicOrdering Ordering) {
317 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
318 (Ordering << 7));
319 }
320
321 SynchronizationScope getSynchScope() const {
322 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
323 }
324
325 /// Specify whether this store instruction is ordered with respect to all
326 /// concurrently executing threads, or only with respect to signal handlers
327 /// executing in the same thread.
328 void setSynchScope(SynchronizationScope xthread) {
329 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
330 (xthread << 6));
331 }
332
333 bool isAtomic() const { return getOrdering() != NotAtomic; }
334 void setAtomic(AtomicOrdering Ordering,
335 SynchronizationScope SynchScope = CrossThread) {
336 setOrdering(Ordering);
337 setSynchScope(SynchScope);
338 }
339
340 bool isSimple() const { return !isAtomic() && !isVolatile(); }
341 bool isUnordered() const {
342 return getOrdering() <= Unordered && !isVolatile();
343 }
344
Chris Lattner41c9c0e2010-06-26 23:26:37 +0000345 Value *getValueOperand() { return getOperand(0); }
346 const Value *getValueOperand() const { return getOperand(0); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000347
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000348 Value *getPointerOperand() { return getOperand(1); }
349 const Value *getPointerOperand() const { return getOperand(1); }
350 static unsigned getPointerOperandIndex() { return 1U; }
351
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000352 unsigned getPointerAddressSpace() const {
353 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
354 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000355
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000356 // Methods for support type inquiry through isa, cast, and dyn_cast:
357 static inline bool classof(const StoreInst *) { return true; }
358 static inline bool classof(const Instruction *I) {
359 return I->getOpcode() == Instruction::Store;
360 }
361 static inline bool classof(const Value *V) {
362 return isa<Instruction>(V) && classof(cast<Instruction>(V));
363 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000364private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000365 // Shadow Instruction::setInstructionSubclassData with a private forwarding
366 // method so that subclasses cannot accidentally use it.
367 void setInstructionSubclassData(unsigned short D) {
368 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000369 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000370};
371
Gabor Greifefe65362008-05-10 08:32:32 +0000372template <>
Jay Foad67c619b2011-01-11 15:07:38 +0000373struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
Gabor Greifefe65362008-05-10 08:32:32 +0000374};
375
376DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000377
378//===----------------------------------------------------------------------===//
Eli Friedman47f35132011-07-25 23:16:38 +0000379// FenceInst Class
380//===----------------------------------------------------------------------===//
381
382/// FenceInst - an instruction for ordering other memory operations
383///
384class FenceInst : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +0000385 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Eli Friedman47f35132011-07-25 23:16:38 +0000386 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
387protected:
388 virtual FenceInst *clone_impl() const;
389public:
390 // allocate space for exactly zero operands
391 void *operator new(size_t s) {
392 return User::operator new(s, 0);
393 }
394
395 // Ordering may only be Acquire, Release, AcquireRelease, or
396 // SequentiallyConsistent.
397 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
398 SynchronizationScope SynchScope = CrossThread,
399 Instruction *InsertBefore = 0);
400 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
401 SynchronizationScope SynchScope,
402 BasicBlock *InsertAtEnd);
403
404 /// Returns the ordering effect of this fence.
405 AtomicOrdering getOrdering() const {
406 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
407 }
408
409 /// Set the ordering constraint on this fence. May only be Acquire, Release,
410 /// AcquireRelease, or SequentiallyConsistent.
411 void setOrdering(AtomicOrdering Ordering) {
Eli Friedman21006d42011-08-09 23:02:53 +0000412 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
413 (Ordering << 1));
Eli Friedman47f35132011-07-25 23:16:38 +0000414 }
415
416 SynchronizationScope getSynchScope() const {
417 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
418 }
419
420 /// Specify whether this fence orders other operations with respect to all
421 /// concurrently executing threads, or only with respect to signal handlers
422 /// executing in the same thread.
423 void setSynchScope(SynchronizationScope xthread) {
424 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
425 xthread);
426 }
427
428 // Methods for support type inquiry through isa, cast, and dyn_cast:
429 static inline bool classof(const FenceInst *) { return true; }
430 static inline bool classof(const Instruction *I) {
431 return I->getOpcode() == Instruction::Fence;
432 }
433 static inline bool classof(const Value *V) {
434 return isa<Instruction>(V) && classof(cast<Instruction>(V));
435 }
436private:
437 // Shadow Instruction::setInstructionSubclassData with a private forwarding
438 // method so that subclasses cannot accidentally use it.
439 void setInstructionSubclassData(unsigned short D) {
440 Instruction::setInstructionSubclassData(D);
441 }
442};
443
444//===----------------------------------------------------------------------===//
Eli Friedmanff030482011-07-28 21:48:00 +0000445// AtomicCmpXchgInst Class
446//===----------------------------------------------------------------------===//
447
448/// AtomicCmpXchgInst - an instruction that atomically checks whether a
449/// specified value is in a memory location, and, if it is, stores a new value
450/// there. Returns the value that was loaded.
451///
452class AtomicCmpXchgInst : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +0000453 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Eli Friedmanff030482011-07-28 21:48:00 +0000454 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
455 AtomicOrdering Ordering, SynchronizationScope SynchScope);
456protected:
457 virtual AtomicCmpXchgInst *clone_impl() const;
458public:
459 // allocate space for exactly three operands
460 void *operator new(size_t s) {
461 return User::operator new(s, 3);
462 }
463 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
464 AtomicOrdering Ordering, SynchronizationScope SynchScope,
465 Instruction *InsertBefore = 0);
466 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
467 AtomicOrdering Ordering, SynchronizationScope SynchScope,
468 BasicBlock *InsertAtEnd);
469
470 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
471 /// location.
472 ///
473 bool isVolatile() const {
474 return getSubclassDataFromInstruction() & 1;
475 }
476
477 /// setVolatile - Specify whether this is a volatile cmpxchg.
478 ///
479 void setVolatile(bool V) {
480 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
481 (unsigned)V);
482 }
483
484 /// Transparently provide more efficient getOperand methods.
485 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
486
487 /// Set the ordering constraint on this cmpxchg.
488 void setOrdering(AtomicOrdering Ordering) {
489 assert(Ordering != NotAtomic &&
490 "CmpXchg instructions can only be atomic.");
491 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
492 (Ordering << 2));
493 }
494
495 /// Specify whether this cmpxchg is atomic and orders other operations with
496 /// respect to all concurrently executing threads, or only with respect to
497 /// signal handlers executing in the same thread.
498 void setSynchScope(SynchronizationScope SynchScope) {
499 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
500 (SynchScope << 1));
501 }
502
503 /// Returns the ordering constraint on this cmpxchg.
504 AtomicOrdering getOrdering() const {
505 return AtomicOrdering(getSubclassDataFromInstruction() >> 2);
506 }
507
508 /// Returns whether this cmpxchg is atomic between threads or only within a
509 /// single thread.
510 SynchronizationScope getSynchScope() const {
511 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
512 }
513
514 Value *getPointerOperand() { return getOperand(0); }
515 const Value *getPointerOperand() const { return getOperand(0); }
516 static unsigned getPointerOperandIndex() { return 0U; }
517
518 Value *getCompareOperand() { return getOperand(1); }
519 const Value *getCompareOperand() const { return getOperand(1); }
520
521 Value *getNewValOperand() { return getOperand(2); }
522 const Value *getNewValOperand() const { return getOperand(2); }
523
524 unsigned getPointerAddressSpace() const {
525 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
526 }
527
528 // Methods for support type inquiry through isa, cast, and dyn_cast:
529 static inline bool classof(const AtomicCmpXchgInst *) { return true; }
530 static inline bool classof(const Instruction *I) {
531 return I->getOpcode() == Instruction::AtomicCmpXchg;
532 }
533 static inline bool classof(const Value *V) {
534 return isa<Instruction>(V) && classof(cast<Instruction>(V));
535 }
536private:
537 // Shadow Instruction::setInstructionSubclassData with a private forwarding
538 // method so that subclasses cannot accidentally use it.
539 void setInstructionSubclassData(unsigned short D) {
540 Instruction::setInstructionSubclassData(D);
541 }
542};
543
544template <>
545struct OperandTraits<AtomicCmpXchgInst> :
546 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
547};
548
549DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
550
551//===----------------------------------------------------------------------===//
552// AtomicRMWInst Class
553//===----------------------------------------------------------------------===//
554
555/// AtomicRMWInst - an instruction that atomically reads a memory location,
556/// combines it with another value, and then stores the result back. Returns
557/// the old value.
558///
559class AtomicRMWInst : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +0000560 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Eli Friedmanff030482011-07-28 21:48:00 +0000561protected:
562 virtual AtomicRMWInst *clone_impl() const;
563public:
564 /// This enumeration lists the possible modifications atomicrmw can make. In
565 /// the descriptions, 'p' is the pointer to the instruction's memory location,
566 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
567 /// instruction. These instructions always return 'old'.
568 enum BinOp {
569 /// *p = v
570 Xchg,
571 /// *p = old + v
572 Add,
573 /// *p = old - v
574 Sub,
575 /// *p = old & v
576 And,
577 /// *p = ~old & v
578 Nand,
579 /// *p = old | v
580 Or,
581 /// *p = old ^ v
582 Xor,
583 /// *p = old >signed v ? old : v
584 Max,
585 /// *p = old <signed v ? old : v
586 Min,
587 /// *p = old >unsigned v ? old : v
588 UMax,
589 /// *p = old <unsigned v ? old : v
590 UMin,
591
592 FIRST_BINOP = Xchg,
593 LAST_BINOP = UMin,
594 BAD_BINOP
595 };
596
597 // allocate space for exactly two operands
598 void *operator new(size_t s) {
599 return User::operator new(s, 2);
600 }
601 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
602 AtomicOrdering Ordering, SynchronizationScope SynchScope,
603 Instruction *InsertBefore = 0);
604 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
605 AtomicOrdering Ordering, SynchronizationScope SynchScope,
606 BasicBlock *InsertAtEnd);
607
608 BinOp getOperation() const {
609 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
610 }
611
612 void setOperation(BinOp Operation) {
613 unsigned short SubclassData = getSubclassDataFromInstruction();
614 setInstructionSubclassData((SubclassData & 31) |
615 (Operation << 5));
616 }
617
618 /// isVolatile - Return true if this is a RMW on a volatile memory location.
619 ///
620 bool isVolatile() const {
621 return getSubclassDataFromInstruction() & 1;
622 }
623
624 /// setVolatile - Specify whether this is a volatile RMW or not.
625 ///
626 void setVolatile(bool V) {
627 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
628 (unsigned)V);
629 }
630
631 /// Transparently provide more efficient getOperand methods.
632 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
633
634 /// Set the ordering constraint on this RMW.
635 void setOrdering(AtomicOrdering Ordering) {
636 assert(Ordering != NotAtomic &&
637 "atomicrmw instructions can only be atomic.");
Eli Friedman21006d42011-08-09 23:02:53 +0000638 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
Eli Friedmanff030482011-07-28 21:48:00 +0000639 (Ordering << 2));
640 }
641
642 /// Specify whether this RMW orders other operations with respect to all
643 /// concurrently executing threads, or only with respect to signal handlers
644 /// executing in the same thread.
645 void setSynchScope(SynchronizationScope SynchScope) {
646 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
647 (SynchScope << 1));
648 }
649
650 /// Returns the ordering constraint on this RMW.
651 AtomicOrdering getOrdering() const {
Eli Friedman21006d42011-08-09 23:02:53 +0000652 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
Eli Friedmanff030482011-07-28 21:48:00 +0000653 }
654
655 /// Returns whether this RMW is atomic between threads or only within a
656 /// single thread.
657 SynchronizationScope getSynchScope() const {
658 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
659 }
660
661 Value *getPointerOperand() { return getOperand(0); }
662 const Value *getPointerOperand() const { return getOperand(0); }
663 static unsigned getPointerOperandIndex() { return 0U; }
664
665 Value *getValOperand() { return getOperand(1); }
666 const Value *getValOperand() const { return getOperand(1); }
667
668 unsigned getPointerAddressSpace() const {
669 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
670 }
671
672 // Methods for support type inquiry through isa, cast, and dyn_cast:
673 static inline bool classof(const AtomicRMWInst *) { return true; }
674 static inline bool classof(const Instruction *I) {
675 return I->getOpcode() == Instruction::AtomicRMW;
676 }
677 static inline bool classof(const Value *V) {
678 return isa<Instruction>(V) && classof(cast<Instruction>(V));
679 }
680private:
681 void Init(BinOp Operation, Value *Ptr, Value *Val,
682 AtomicOrdering Ordering, SynchronizationScope SynchScope);
683 // Shadow Instruction::setInstructionSubclassData with a private forwarding
684 // method so that subclasses cannot accidentally use it.
685 void setInstructionSubclassData(unsigned short D) {
686 Instruction::setInstructionSubclassData(D);
687 }
688};
689
690template <>
691struct OperandTraits<AtomicRMWInst>
692 : public FixedNumOperandTraits<AtomicRMWInst,2> {
693};
694
695DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
696
697//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000698// GetElementPtrInst Class
699//===----------------------------------------------------------------------===//
700
Chris Lattner1afcace2011-07-09 17:41:24 +0000701// checkGEPType - Simple wrapper function to give a better assertion failure
David Greeneb8f74792007-09-04 15:46:09 +0000702// message on bad indexes for a gep instruction.
703//
Chandler Carruth305b5152012-06-20 08:39:33 +0000704inline Type *checkGEPType(Type *Ty) {
David Greeneb8f74792007-09-04 15:46:09 +0000705 assert(Ty && "Invalid GetElementPtrInst indices for type!");
706 return Ty;
707}
708
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000709/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
710/// access elements of arrays and structs
711///
712class GetElementPtrInst : public Instruction {
Gabor Greifefe65362008-05-10 08:32:32 +0000713 GetElementPtrInst(const GetElementPtrInst &GEPI);
Jay Foada9203102011-07-25 09:48:08 +0000714 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000715
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000716 /// Constructors - Create a getelementptr instruction with a base pointer an
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000717 /// list of indices. The first ctor can optionally insert before an existing
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000718 /// instruction, the second appends the new instruction to the specified
719 /// BasicBlock.
Jay Foada9203102011-07-25 09:48:08 +0000720 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
721 unsigned Values, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000722 Instruction *InsertBefore);
Jay Foada9203102011-07-25 09:48:08 +0000723 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
724 unsigned Values, const Twine &NameStr,
725 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +0000726protected:
727 virtual GetElementPtrInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +0000728public:
Jay Foada9203102011-07-25 09:48:08 +0000729 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000730 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000731 Instruction *InsertBefore = 0) {
Jay Foada9203102011-07-25 09:48:08 +0000732 unsigned Values = 1 + unsigned(IdxList.size());
Gabor Greifefe65362008-05-10 08:32:32 +0000733 return new(Values)
Jay Foada9203102011-07-25 09:48:08 +0000734 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000735 }
Jay Foada9203102011-07-25 09:48:08 +0000736 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000737 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000738 BasicBlock *InsertAtEnd) {
Jay Foada9203102011-07-25 09:48:08 +0000739 unsigned Values = 1 + unsigned(IdxList.size());
Gabor Greifefe65362008-05-10 08:32:32 +0000740 return new(Values)
Jay Foada9203102011-07-25 09:48:08 +0000741 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000742 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000743
Dan Gohmane2574d32009-08-11 17:57:01 +0000744 /// Create an "inbounds" getelementptr. See the documentation for the
745 /// "inbounds" flag in LangRef.html for details.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000746 static GetElementPtrInst *CreateInBounds(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000747 ArrayRef<Value *> IdxList,
Dan Gohmane2574d32009-08-11 17:57:01 +0000748 const Twine &NameStr = "",
749 Instruction *InsertBefore = 0) {
Jay Foada9203102011-07-25 09:48:08 +0000750 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000751 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000752 return GEP;
753 }
Dan Gohmane2574d32009-08-11 17:57:01 +0000754 static GetElementPtrInst *CreateInBounds(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000755 ArrayRef<Value *> IdxList,
Dan Gohmane2574d32009-08-11 17:57:01 +0000756 const Twine &NameStr,
757 BasicBlock *InsertAtEnd) {
Jay Foada9203102011-07-25 09:48:08 +0000758 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000759 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000760 return GEP;
761 }
762
Gabor Greifefe65362008-05-10 08:32:32 +0000763 /// Transparently provide more efficient getOperand methods.
764 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
765
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000766 // getType - Overload to return most specific pointer type...
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000767 PointerType *getType() const {
768 return reinterpret_cast<PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000769 }
770
771 /// getIndexedType - Returns the type of the element that would be loaded with
772 /// a load instruction with the specified parameters.
773 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000774 /// Null is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000775 /// pointer type.
776 ///
Jay Foada9203102011-07-25 09:48:08 +0000777 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
778 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
779 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
Misha Brukman9769ab22005-04-21 20:19:05 +0000780
Nadav Rotem16087692011-12-05 06:29:09 +0000781 /// getIndexedType - Returns the address space used by the GEP pointer.
782 ///
783 static unsigned getAddressSpace(Value *Ptr);
784
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000785 inline op_iterator idx_begin() { return op_begin()+1; }
786 inline const_op_iterator idx_begin() const { return op_begin()+1; }
787 inline op_iterator idx_end() { return op_end(); }
788 inline const_op_iterator idx_end() const { return op_end(); }
789
790 Value *getPointerOperand() {
791 return getOperand(0);
792 }
793 const Value *getPointerOperand() const {
794 return getOperand(0);
795 }
796 static unsigned getPointerOperandIndex() {
Nadav Rotem16087692011-12-05 06:29:09 +0000797 return 0U; // get index for modifying correct operand.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000798 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000799
Chris Lattner8a67ac52009-08-30 20:06:40 +0000800 unsigned getPointerAddressSpace() const {
801 return cast<PointerType>(getType())->getAddressSpace();
802 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000803
Chris Lattner3bc6ced2009-01-09 05:27:40 +0000804 /// getPointerOperandType - Method to return the pointer operand as a
805 /// PointerType.
Nadav Rotem16087692011-12-05 06:29:09 +0000806 Type *getPointerOperandType() const {
807 return getPointerOperand()->getType();
Chris Lattner3bc6ced2009-01-09 05:27:40 +0000808 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000809
Nadav Rotem16087692011-12-05 06:29:09 +0000810 /// GetGEPReturnType - Returns the pointer type returned by the GEP
811 /// instruction, which may be a vector of pointers.
812 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
813 Type *PtrTy = PointerType::get(checkGEPType(
814 getIndexedType(Ptr->getType(), IdxList)),
815 getAddressSpace(Ptr));
816 // Vector GEP
817 if (Ptr->getType()->isVectorTy()) {
818 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
819 return VectorType::get(PtrTy, NumElem);
820 }
821
822 // Scalar GEP
823 return PtrTy;
824 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000825
Devang Patel4d4a5e02008-02-23 01:11:02 +0000826 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000827 return getNumOperands() - 1;
828 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000829
Devang Patel4d4a5e02008-02-23 01:11:02 +0000830 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000831 return getNumOperands() > 1;
832 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000833
Chris Lattner6f771d42007-04-14 00:12:57 +0000834 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
835 /// zeros. If so, the result pointer and the first operand have the same
836 /// value, just potentially different types.
837 bool hasAllZeroIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000838
Chris Lattner6b0974c2007-04-27 20:35:56 +0000839 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
840 /// constant integers. If so, the result pointer and the first operand have
841 /// a constant offset between them.
842 bool hasAllConstantIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000843
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000844 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
845 /// See LangRef.html for the meaning of inbounds on a getelementptr.
Nick Lewyckyae05e7d2009-09-27 21:33:04 +0000846 void setIsInBounds(bool b = true);
847
848 /// isInBounds - Determine whether the GEP has the inbounds flag.
849 bool isInBounds() const;
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000850
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000851 // Methods for support type inquiry through isa, cast, and dyn_cast:
852 static inline bool classof(const GetElementPtrInst *) { return true; }
853 static inline bool classof(const Instruction *I) {
854 return (I->getOpcode() == Instruction::GetElementPtr);
855 }
856 static inline bool classof(const Value *V) {
857 return isa<Instruction>(V) && classof(cast<Instruction>(V));
858 }
859};
860
Gabor Greifefe65362008-05-10 08:32:32 +0000861template <>
Jay Foad67c619b2011-01-11 15:07:38 +0000862struct OperandTraits<GetElementPtrInst> :
863 public VariadicOperandTraits<GetElementPtrInst, 1> {
Gabor Greifefe65362008-05-10 08:32:32 +0000864};
865
Gabor Greifefe65362008-05-10 08:32:32 +0000866GetElementPtrInst::GetElementPtrInst(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000867 ArrayRef<Value *> IdxList,
Gabor Greifefe65362008-05-10 08:32:32 +0000868 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000869 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000870 Instruction *InsertBefore)
Nadav Rotem16087692011-12-05 06:29:09 +0000871 : Instruction(getGEPReturnType(Ptr, IdxList),
Gabor Greifefe65362008-05-10 08:32:32 +0000872 GetElementPtr,
873 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
874 Values, InsertBefore) {
Jay Foada9203102011-07-25 09:48:08 +0000875 init(Ptr, IdxList, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +0000876}
Gabor Greifefe65362008-05-10 08:32:32 +0000877GetElementPtrInst::GetElementPtrInst(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000878 ArrayRef<Value *> IdxList,
Gabor Greifefe65362008-05-10 08:32:32 +0000879 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000880 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000881 BasicBlock *InsertAtEnd)
Nadav Rotem16087692011-12-05 06:29:09 +0000882 : Instruction(getGEPReturnType(Ptr, IdxList),
Gabor Greifefe65362008-05-10 08:32:32 +0000883 GetElementPtr,
884 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
885 Values, InsertAtEnd) {
Jay Foada9203102011-07-25 09:48:08 +0000886 init(Ptr, IdxList, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +0000887}
888
889
890DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
891
892
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000893//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000894// ICmpInst Class
895//===----------------------------------------------------------------------===//
896
897/// This instruction compares its operands according to the predicate given
Nate Begemanac80ade2008-05-12 19:01:56 +0000898/// to the constructor. It only operates on integers or pointers. The operands
899/// must be identical types.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000900/// @brief Represent an integer comparison operator.
901class ICmpInst: public CmpInst {
Devang Patel50b6e332009-10-27 22:16:29 +0000902protected:
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000903 /// @brief Clone an identical ICmpInst
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000904 virtual ICmpInst *clone_impl() const;
Reid Spencer45fb3f32006-11-20 01:22:35 +0000905public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000906 /// @brief Constructor with insert-before-instruction semantics.
907 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000908 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +0000909 Predicate pred, ///< The predicate to use for the comparison
910 Value *LHS, ///< The left-hand-side of the expression
911 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000912 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000913 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +0000914 Instruction::ICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000915 InsertBefore) {
916 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
917 pred <= CmpInst::LAST_ICMP_PREDICATE &&
918 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000919 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000920 "Both operands to ICmp instruction are not of the same type!");
921 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000922 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Nadav Rotem16087692011-12-05 06:29:09 +0000923 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000924 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000925 }
926
Owen Anderson333c4002009-07-09 23:48:35 +0000927 /// @brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000928 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000929 BasicBlock &InsertAtEnd, ///< Block to insert into.
930 Predicate pred, ///< The predicate to use for the comparison
931 Value *LHS, ///< The left-hand-side of the expression
932 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000933 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000934 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000935 Instruction::ICmp, pred, LHS, RHS, NameStr,
936 &InsertAtEnd) {
937 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
938 pred <= CmpInst::LAST_ICMP_PREDICATE &&
939 "Invalid ICmp predicate value");
940 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
941 "Both operands to ICmp instruction are not of the same type!");
942 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000943 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000944 getOperand(0)->getType()->isPointerTy()) &&
Owen Anderson333c4002009-07-09 23:48:35 +0000945 "Invalid operand types for ICmp instruction");
946 }
947
948 /// @brief Constructor with no-insertion semantics
949 ICmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +0000950 Predicate pred, ///< The predicate to use for the comparison
951 Value *LHS, ///< The left-hand-side of the expression
952 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000953 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000954 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000955 Instruction::ICmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +0000956 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
957 pred <= CmpInst::LAST_ICMP_PREDICATE &&
958 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000959 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000960 "Both operands to ICmp instruction are not of the same type!");
961 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000962 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Nadav Rotem16087692011-12-05 06:29:09 +0000963 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000964 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000965 }
966
Reid Spencere4d87aa2006-12-23 06:05:41 +0000967 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
968 /// @returns the predicate that would be the result if the operand were
969 /// regarded as signed.
970 /// @brief Return the signed version of the predicate
971 Predicate getSignedPredicate() const {
972 return getSignedPredicate(getPredicate());
973 }
974
975 /// This is a static version that you can use without an instruction.
976 /// @brief Return the signed version of the predicate.
977 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000978
Nick Lewycky4189a532008-01-28 03:48:02 +0000979 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
980 /// @returns the predicate that would be the result if the operand were
981 /// regarded as unsigned.
982 /// @brief Return the unsigned version of the predicate
983 Predicate getUnsignedPredicate() const {
984 return getUnsignedPredicate(getPredicate());
985 }
986
987 /// This is a static version that you can use without an instruction.
988 /// @brief Return the unsigned version of the predicate.
989 static Predicate getUnsignedPredicate(Predicate pred);
990
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000991 /// isEquality - Return true if this predicate is either EQ or NE. This also
992 /// tests for commutativity.
993 static bool isEquality(Predicate P) {
994 return P == ICMP_EQ || P == ICMP_NE;
995 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000996
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000997 /// isEquality - Return true if this predicate is either EQ or NE. This also
998 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000999 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +00001000 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +00001001 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001002
1003 /// @returns true if the predicate of this ICmpInst is commutative
1004 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +00001005 bool isCommutative() const { return isEquality(); }
1006
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001007 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +00001008 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +00001009 bool isRelational() const {
1010 return !isEquality();
1011 }
1012
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001013 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +00001014 ///
1015 static bool isRelational(Predicate P) {
1016 return !isEquality(P);
1017 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001018
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001019 /// Initialize a set of values that all satisfy the predicate with C.
Reid Spencer3da43842007-02-28 22:00:54 +00001020 /// @brief Make a ConstantRange for a relation with a constant value.
1021 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1022
Reid Spencer45fb3f32006-11-20 01:22:35 +00001023 /// Exchange the two operands to this instruction in such a way that it does
1024 /// not modify the semantics of the instruction. The predicate value may be
1025 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001026 /// (e.g. ult).
Reid Spencer45fb3f32006-11-20 01:22:35 +00001027 /// @brief Swap operands and adjust predicate.
1028 void swapOperands() {
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001029 setPredicate(getSwappedPredicate());
Gabor Greif94fb68b2008-05-13 22:51:52 +00001030 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +00001031 }
1032
1033 // Methods for support type inquiry through isa, cast, and dyn_cast:
1034 static inline bool classof(const ICmpInst *) { return true; }
1035 static inline bool classof(const Instruction *I) {
1036 return I->getOpcode() == Instruction::ICmp;
1037 }
1038 static inline bool classof(const Value *V) {
1039 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1040 }
Dan Gohmanf72fb672008-09-09 01:02:47 +00001041
Reid Spencer45fb3f32006-11-20 01:22:35 +00001042};
1043
1044//===----------------------------------------------------------------------===//
1045// FCmpInst Class
1046//===----------------------------------------------------------------------===//
1047
1048/// This instruction compares its operands according to the predicate given
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001049/// to the constructor. It only operates on floating point values or packed
Reid Spencer45fb3f32006-11-20 01:22:35 +00001050/// vectors of floating point values. The operands must be identical types.
1051/// @brief Represents a floating point comparison operator.
1052class FCmpInst: public CmpInst {
Devang Patel50b6e332009-10-27 22:16:29 +00001053protected:
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001054 /// @brief Clone an identical FCmpInst
Devang Patel50b6e332009-10-27 22:16:29 +00001055 virtual FCmpInst *clone_impl() const;
Reid Spencer45fb3f32006-11-20 01:22:35 +00001056public:
Reid Spencer45fb3f32006-11-20 01:22:35 +00001057 /// @brief Constructor with insert-before-instruction semantics.
1058 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +00001059 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +00001060 Predicate pred, ///< The predicate to use for the comparison
1061 Value *LHS, ///< The left-hand-side of the expression
1062 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001063 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +00001064 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +00001065 Instruction::FCmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +00001066 InsertBefore) {
1067 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1068 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +00001069 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001070 "Both operands to FCmp instruction are not of the same type!");
1071 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001072 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001073 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +00001074 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001075
Owen Anderson333c4002009-07-09 23:48:35 +00001076 /// @brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +00001077 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +00001078 BasicBlock &InsertAtEnd, ///< Block to insert into.
1079 Predicate pred, ///< The predicate to use for the comparison
1080 Value *LHS, ///< The left-hand-side of the expression
1081 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001082 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +00001083 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +00001084 Instruction::FCmp, pred, LHS, RHS, NameStr,
1085 &InsertAtEnd) {
1086 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1087 "Invalid FCmp predicate value");
1088 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1089 "Both operands to FCmp instruction are not of the same type!");
1090 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001091 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Owen Anderson333c4002009-07-09 23:48:35 +00001092 "Invalid operand types for FCmp instruction");
1093 }
1094
1095 /// @brief Constructor with no-insertion semantics
1096 FCmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +00001097 Predicate pred, ///< The predicate to use for the comparison
1098 Value *LHS, ///< The left-hand-side of the expression
1099 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001100 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +00001101 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +00001102 Instruction::FCmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +00001103 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1104 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +00001105 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001106 "Both operands to FCmp instruction are not of the same type!");
1107 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001108 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001109 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +00001110 }
1111
Reid Spencer45fb3f32006-11-20 01:22:35 +00001112 /// @returns true if the predicate of this instruction is EQ or NE.
1113 /// @brief Determine if this is an equality predicate.
1114 bool isEquality() const {
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001115 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1116 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
Reid Spencer45fb3f32006-11-20 01:22:35 +00001117 }
Dan Gohman793df072008-09-16 16:44:00 +00001118
1119 /// @returns true if the predicate of this instruction is commutative.
1120 /// @brief Determine if this is a commutative predicate.
1121 bool isCommutative() const {
1122 return isEquality() ||
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001123 getPredicate() == FCMP_FALSE ||
1124 getPredicate() == FCMP_TRUE ||
1125 getPredicate() == FCMP_ORD ||
1126 getPredicate() == FCMP_UNO;
Dan Gohman793df072008-09-16 16:44:00 +00001127 }
Reid Spencer45fb3f32006-11-20 01:22:35 +00001128
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001129 /// @returns true if the predicate is relational (not EQ or NE).
Reid Spencer45fb3f32006-11-20 01:22:35 +00001130 /// @brief Determine if this a relational predicate.
1131 bool isRelational() const { return !isEquality(); }
1132
1133 /// Exchange the two operands to this instruction in such a way that it does
1134 /// not modify the semantics of the instruction. The predicate value may be
1135 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001136 /// (e.g. ult).
Reid Spencer45fb3f32006-11-20 01:22:35 +00001137 /// @brief Swap operands and adjust predicate.
1138 void swapOperands() {
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001139 setPredicate(getSwappedPredicate());
Gabor Greif94fb68b2008-05-13 22:51:52 +00001140 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +00001141 }
1142
1143 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1144 static inline bool classof(const FCmpInst *) { return true; }
1145 static inline bool classof(const Instruction *I) {
1146 return I->getOpcode() == Instruction::FCmp;
1147 }
1148 static inline bool classof(const Value *V) {
1149 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1150 }
1151};
1152
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001153//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001154/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +00001155/// machine's calling convention. This class uses low bit of the SubClassData
1156/// field to indicate whether or not this is a tail call. The rest of the bits
1157/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001158///
1159class CallInst : public Instruction {
Devang Patel05988662008-09-25 21:00:45 +00001160 AttrListPtr AttributeList; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001161 CallInst(const CallInst &CI);
Jay Foada3efbb12011-07-15 08:37:34 +00001162 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1163 void init(Value *Func, const Twine &NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001164
Jay Foada3efbb12011-07-15 08:37:34 +00001165 /// Construct a CallInst given a range of arguments.
David Greene52eec542007-08-01 03:43:44 +00001166 /// @brief Construct a CallInst from a range of arguments
Jay Foada3efbb12011-07-15 08:37:34 +00001167 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1168 const Twine &NameStr, Instruction *InsertBefore);
David Greene52eec542007-08-01 03:43:44 +00001169
Jay Foada3efbb12011-07-15 08:37:34 +00001170 /// Construct a CallInst given a range of arguments.
David Greene52eec542007-08-01 03:43:44 +00001171 /// @brief Construct a CallInst from a range of arguments
Jay Foada3efbb12011-07-15 08:37:34 +00001172 inline CallInst(Value *Func, ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001173 const Twine &NameStr, BasicBlock *InsertAtEnd);
David Greene52eec542007-08-01 03:43:44 +00001174
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001175 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001176 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001177 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001178 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001179 explicit CallInst(Value *F, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001180 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001181 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001182protected:
1183 virtual CallInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001184public:
Gabor Greifefe65362008-05-10 08:32:32 +00001185 static CallInst *Create(Value *Func,
Jay Foada3efbb12011-07-15 08:37:34 +00001186 ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001187 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001188 Instruction *InsertBefore = 0) {
Jay Foada3efbb12011-07-15 08:37:34 +00001189 return new(unsigned(Args.size() + 1))
1190 CallInst(Func, Args, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001191 }
Gabor Greifefe65362008-05-10 08:32:32 +00001192 static CallInst *Create(Value *Func,
Jay Foada3efbb12011-07-15 08:37:34 +00001193 ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001194 const Twine &NameStr, BasicBlock *InsertAtEnd) {
Jay Foada3efbb12011-07-15 08:37:34 +00001195 return new(unsigned(Args.size() + 1))
1196 CallInst(Func, Args, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001197 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001198 static CallInst *Create(Value *F, const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001199 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001200 return new(1) CallInst(F, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001201 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001202 static CallInst *Create(Value *F, const Twine &NameStr,
Evan Cheng34cd4a42008-05-05 18:30:58 +00001203 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001204 return new(1) CallInst(F, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001205 }
Evan Chengfabcb912009-09-10 04:36:43 +00001206 /// CreateMalloc - Generate the IR for a call to malloc:
1207 /// 1. Compute the malloc call's argument as the specified type's size,
1208 /// possibly multiplied by the array size if the array size is not
1209 /// constant 1.
1210 /// 2. Call malloc with that argument.
1211 /// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewycky3fc35c52009-10-17 23:52:26 +00001212 static Instruction *CreateMalloc(Instruction *InsertBefore,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001213 Type *IntPtrTy, Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +00001214 Value *AllocSize, Value *ArraySize = 0,
Chris Lattner5a30a852010-07-12 00:57:28 +00001215 Function* MallocF = 0,
Nick Lewycky3fc35c52009-10-17 23:52:26 +00001216 const Twine &Name = "");
1217 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001218 Type *IntPtrTy, Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +00001219 Value *AllocSize, Value *ArraySize = 0,
1220 Function* MallocF = 0,
Nick Lewycky3fc35c52009-10-17 23:52:26 +00001221 const Twine &Name = "");
Victor Hernandez66284e02009-10-24 04:23:03 +00001222 /// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner5a30a852010-07-12 00:57:28 +00001223 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
Victor Hernandez66284e02009-10-24 04:23:03 +00001224 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001225
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001226 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001227
Chris Lattnerb2406d92009-12-29 02:46:09 +00001228 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001229 void setTailCall(bool isTC = true) {
Chris Lattnerb2406d92009-12-29 02:46:09 +00001230 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
1231 unsigned(isTC));
Chris Lattner3340ffe2005-05-06 20:26:26 +00001232 }
1233
Dan Gohmanf2752502008-09-26 21:38:45 +00001234 /// Provide fast operand accessors
1235 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001236
Gabor Greif0114b992010-07-31 08:35:21 +00001237 /// getNumArgOperands - Return the number of call arguments.
1238 ///
Bill Wendling22a5b292010-06-07 19:05:06 +00001239 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
Gabor Greif0114b992010-07-31 08:35:21 +00001240
1241 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1242 ///
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001243 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1244 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
Bill Wendling22a5b292010-06-07 19:05:06 +00001245
Chris Lattner3340ffe2005-05-06 20:26:26 +00001246 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1247 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001248 CallingConv::ID getCallingConv() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +00001249 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001250 }
1251 void setCallingConv(CallingConv::ID CC) {
Chris Lattnerb2406d92009-12-29 02:46:09 +00001252 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1253 (static_cast<unsigned>(CC) << 1));
Chris Lattner3340ffe2005-05-06 20:26:26 +00001254 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00001255
Devang Patel05988662008-09-25 21:00:45 +00001256 /// getAttributes - Return the parameter attributes for this call.
Chris Lattner041221c2008-03-13 04:33:03 +00001257 ///
Devang Patel05988662008-09-25 21:00:45 +00001258 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001259
Dan Gohmanf2752502008-09-26 21:38:45 +00001260 /// setAttributes - Set the parameter attributes for this call.
1261 ///
Devang Patel05988662008-09-25 21:00:45 +00001262 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001263
Devang Patel05988662008-09-25 21:00:45 +00001264 /// addAttribute - adds the attribute to the list of attributes.
1265 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsdc024672007-11-27 13:23:08 +00001266
Devang Patel05988662008-09-25 21:00:45 +00001267 /// removeAttribute - removes the attribute from the list of attributes.
1268 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00001269
Bill Wendling060f20a2012-10-09 00:28:54 +00001270 /// @brief Determine whether this call has the given attribute.
1271 bool fnHasNoAliasAttr() const;
1272 bool fnHasNoInlineAttr() const;
1273 bool fnHasNoReturnAttr() const;
1274 bool fnHasNoUnwindAttr() const;
1275 bool fnHasReadNoneAttr() const;
1276 bool fnHasReadOnlyAttr() const;
1277 bool fnHasReturnsTwiceAttr() const;
1278
Nuno Lopes986da6c2012-06-25 16:16:58 +00001279 /// \brief Return true if this call has the given attribute.
1280 bool hasFnAttr(Attributes N) const {
1281 return paramHasAttr(~0, N);
1282 }
1283
Bill Wendling847d1652012-10-03 17:54:26 +00001284 /// @brief Determine whether the call or the callee has the given attributes.
Bill Wendling847d1652012-10-03 17:54:26 +00001285 bool paramHasByValAttr(unsigned i) const;
Bill Wendling060f20a2012-10-09 00:28:54 +00001286 bool paramHasInRegAttr(unsigned i) const;
1287 bool paramHasNestAttr(unsigned i) const;
Bill Wendling5df15c62012-10-04 06:52:09 +00001288 bool paramHasNoAliasAttr(unsigned i) const;
Bill Wendling365b9e02012-10-04 07:18:12 +00001289 bool paramHasNoCaptureAttr(unsigned i) const;
Bill Wendling060f20a2012-10-09 00:28:54 +00001290 bool paramHasSExtAttr(unsigned i) const;
1291 bool paramHasStructRetAttr(unsigned i) const;
1292 bool paramHasZExtAttr(unsigned i) const;
Bill Wendling847d1652012-10-03 17:54:26 +00001293
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001294 /// @brief Determine whether the call or the callee has the given attribute.
Dan Gohmanf2752502008-09-26 21:38:45 +00001295 bool paramHasAttr(unsigned i, Attributes attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001296
Dale Johannesen08e78b12008-02-22 17:49:45 +00001297 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001298 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00001299 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001300 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001301
Eric Christopherf27e6082010-03-25 04:49:10 +00001302 /// @brief Return true if the call should not be inlined.
Bill Wendling060f20a2012-10-09 00:28:54 +00001303 bool isNoInline() const { return fnHasNoInlineAttr(); }
Nick Lewyckyb9933b82010-07-06 03:53:22 +00001304 void setIsNoInline(bool Value = true) {
Eric Christopherf27e6082010-03-25 04:49:10 +00001305 if (Value) addAttribute(~0, Attribute::NoInline);
1306 else removeAttribute(~0, Attribute::NoInline);
1307 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001308
Rafael Espindola11f1a832011-10-05 20:05:13 +00001309 /// @brief Return true if the call can return twice
1310 bool canReturnTwice() const {
Bill Wendling060f20a2012-10-09 00:28:54 +00001311 return fnHasReturnsTwiceAttr();
Rafael Espindola11f1a832011-10-05 20:05:13 +00001312 }
1313 void setCanReturnTwice(bool Value = true) {
1314 if (Value) addAttribute(~0, Attribute::ReturnsTwice);
1315 else removeAttribute(~0, Attribute::ReturnsTwice);
1316 }
1317
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001318 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001319 bool doesNotAccessMemory() const {
Bill Wendling060f20a2012-10-09 00:28:54 +00001320 return fnHasReadNoneAttr();
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001321 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001322 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001323 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1324 else removeAttribute(~0, Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00001325 }
1326
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001327 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001328 bool onlyReadsMemory() const {
Bill Wendling060f20a2012-10-09 00:28:54 +00001329 return doesNotAccessMemory() || fnHasReadOnlyAttr();
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001330 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001331 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001332 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1333 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00001334 }
1335
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001336 /// @brief Determine if the call cannot return.
Bill Wendling060f20a2012-10-09 00:28:54 +00001337 bool doesNotReturn() const { return fnHasNoReturnAttr(); }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001338 void setDoesNotReturn(bool DoesNotReturn = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001339 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1340 else removeAttribute(~0, Attribute::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00001341 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001342
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001343 /// @brief Determine if the call cannot unwind.
Bill Wendling060f20a2012-10-09 00:28:54 +00001344 bool doesNotThrow() const { return fnHasNoUnwindAttr(); }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001345 void setDoesNotThrow(bool DoesNotThrow = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001346 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1347 else removeAttribute(~0, Attribute::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00001348 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001349
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001350 /// @brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00001351 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001352 bool hasStructRetAttr() const {
1353 // Be friendly and also check the callee.
Bill Wendling060f20a2012-10-09 00:28:54 +00001354 return paramHasStructRetAttr(1);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001355 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001356
Evan Chengf4a54982008-01-12 18:57:32 +00001357 /// @brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001358 bool hasByValArgument() const {
Bill Wendling2fd77652012-10-09 01:03:48 +00001359 for (unsigned I = 0, E = AttributeList.getNumAttrs(); I != E; ++I)
1360 if (AttributeList.getAttributesAtIndex(I).hasByValAttr())
1361 return true;
1362 return false;
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001363 }
Evan Chengf4a54982008-01-12 18:57:32 +00001364
Dan Gohmanf2752502008-09-26 21:38:45 +00001365 /// getCalledFunction - Return the function called, or null if this is an
1366 /// indirect function invocation.
1367 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001368 Function *getCalledFunction() const {
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001369 return dyn_cast<Function>(Op<-1>());
Chris Lattner721aef62004-11-18 17:46:57 +00001370 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001371
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001372 /// getCalledValue - Get a pointer to the function that is invoked by this
Chris Lattner14d80382009-10-18 05:08:07 +00001373 /// instruction.
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001374 const Value *getCalledValue() const { return Op<-1>(); }
1375 Value *getCalledValue() { return Op<-1>(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001376
Chris Lattner14d80382009-10-18 05:08:07 +00001377 /// setCalledFunction - Set the function called.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001378 void setCalledFunction(Value* Fn) {
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001379 Op<-1>() = Fn;
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001380 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001381
Owen Anderson6f615f82010-08-07 00:19:59 +00001382 /// isInlineAsm - Check if this call is an inline asm statement.
1383 bool isInlineAsm() const {
1384 return isa<InlineAsm>(Op<-1>());
1385 }
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001386
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001387 // Methods for support type inquiry through isa, cast, and dyn_cast:
1388 static inline bool classof(const CallInst *) { return true; }
1389 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001390 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001391 }
1392 static inline bool classof(const Value *V) {
1393 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1394 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001395private:
Chris Lattnerb2406d92009-12-29 02:46:09 +00001396 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1397 // method so that subclasses cannot accidentally use it.
1398 void setInstructionSubclassData(unsigned short D) {
1399 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001400 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001401};
1402
Gabor Greifefe65362008-05-10 08:32:32 +00001403template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001404struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
Gabor Greifefe65362008-05-10 08:32:32 +00001405};
1406
Jay Foada3efbb12011-07-15 08:37:34 +00001407CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001408 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001409 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1410 ->getElementType())->getReturnType(),
1411 Instruction::Call,
Jay Foada3efbb12011-07-15 08:37:34 +00001412 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1413 unsigned(Args.size() + 1), InsertAtEnd) {
1414 init(Func, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00001415}
1416
Jay Foada3efbb12011-07-15 08:37:34 +00001417CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001418 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00001419 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1420 ->getElementType())->getReturnType(),
1421 Instruction::Call,
Jay Foada3efbb12011-07-15 08:37:34 +00001422 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1423 unsigned(Args.size() + 1), InsertBefore) {
1424 init(Func, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00001425}
1426
Gabor Greife9e12152010-07-06 15:44:11 +00001427
1428// Note: if you get compile errors about private methods then
1429// please update your code to use the high-level operand
1430// interfaces. See line 943 above.
Gabor Greifefe65362008-05-10 08:32:32 +00001431DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1432
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001433//===----------------------------------------------------------------------===//
1434// SelectInst Class
1435//===----------------------------------------------------------------------===//
1436
1437/// SelectInst - This class represents the LLVM 'select' instruction.
1438///
1439class SelectInst : public Instruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001440 void init(Value *C, Value *S1, Value *S2) {
Chris Lattnerb76ec322008-12-29 00:12:50 +00001441 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
Gabor Greifefe65362008-05-10 08:32:32 +00001442 Op<0>() = C;
1443 Op<1>() = S1;
1444 Op<2>() = S2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001445 }
1446
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001447 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001448 Instruction *InsertBefore)
1449 : Instruction(S1->getType(), Instruction::Select,
1450 &Op<0>(), 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001451 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001452 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001453 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001454 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001455 BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001456 : Instruction(S1->getType(), Instruction::Select,
1457 &Op<0>(), 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001458 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001459 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001460 }
Devang Patel50b6e332009-10-27 22:16:29 +00001461protected:
1462 virtual SelectInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001463public:
Evan Chengd69bb1a2008-05-05 17:41:03 +00001464 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001465 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001466 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001467 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001468 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001469 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001470 const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001471 BasicBlock *InsertAtEnd) {
1472 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001473 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001474
Chris Lattner97159122009-09-08 03:32:53 +00001475 const Value *getCondition() const { return Op<0>(); }
1476 const Value *getTrueValue() const { return Op<1>(); }
1477 const Value *getFalseValue() const { return Op<2>(); }
1478 Value *getCondition() { return Op<0>(); }
1479 Value *getTrueValue() { return Op<1>(); }
1480 Value *getFalseValue() { return Op<2>(); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001481
Chris Lattnerb76ec322008-12-29 00:12:50 +00001482 /// areInvalidOperands - Return a string if the specified operands are invalid
1483 /// for a select operation, otherwise return null.
1484 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
Chris Lattner454928e2005-01-29 00:31:36 +00001485
1486 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001487 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001488
1489 OtherOps getOpcode() const {
1490 return static_cast<OtherOps>(Instruction::getOpcode());
1491 }
1492
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001493 // Methods for support type inquiry through isa, cast, and dyn_cast:
1494 static inline bool classof(const SelectInst *) { return true; }
1495 static inline bool classof(const Instruction *I) {
1496 return I->getOpcode() == Instruction::Select;
1497 }
1498 static inline bool classof(const Value *V) {
1499 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1500 }
1501};
1502
Gabor Greifefe65362008-05-10 08:32:32 +00001503template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001504struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001505};
1506
1507DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1508
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001509//===----------------------------------------------------------------------===//
1510// VAArgInst Class
1511//===----------------------------------------------------------------------===//
1512
1513/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001514/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001515///
Chris Lattner454928e2005-01-29 00:31:36 +00001516class VAArgInst : public UnaryInstruction {
Devang Patel50b6e332009-10-27 22:16:29 +00001517protected:
1518 virtual VAArgInst *clone_impl() const;
1519
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001520public:
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001521 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001522 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001523 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001524 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001525 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001526 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001527 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001528 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001529 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001530 }
1531
Dan Gohmane7445412010-09-09 18:32:40 +00001532 Value *getPointerOperand() { return getOperand(0); }
1533 const Value *getPointerOperand() const { return getOperand(0); }
1534 static unsigned getPointerOperandIndex() { return 0U; }
1535
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001536 // Methods for support type inquiry through isa, cast, and dyn_cast:
1537 static inline bool classof(const VAArgInst *) { return true; }
1538 static inline bool classof(const Instruction *I) {
1539 return I->getOpcode() == VAArg;
1540 }
1541 static inline bool classof(const Value *V) {
1542 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1543 }
1544};
1545
1546//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001547// ExtractElementInst Class
1548//===----------------------------------------------------------------------===//
1549
1550/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001551/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001552///
1553class ExtractElementInst : public Instruction {
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001554 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
Chris Lattner9fc18d22006-04-08 01:15:18 +00001555 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001556 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
Chris Lattner9fc18d22006-04-08 01:15:18 +00001557 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001558protected:
1559 virtual ExtractElementInst *clone_impl() const;
1560
Eric Christophera3500da2009-07-25 02:28:41 +00001561public:
Eric Christophera3500da2009-07-25 02:28:41 +00001562 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001563 const Twine &NameStr = "",
Eric Christophera3500da2009-07-25 02:28:41 +00001564 Instruction *InsertBefore = 0) {
1565 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1566 }
1567 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001568 const Twine &NameStr,
Eric Christophera3500da2009-07-25 02:28:41 +00001569 BasicBlock *InsertAtEnd) {
1570 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1571 }
Robert Bocchino49b78a52006-01-10 19:04:13 +00001572
Chris Lattnerfa495842006-04-08 04:04:54 +00001573 /// isValidOperands - Return true if an extractelement instruction can be
1574 /// formed with the specified operands.
1575 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001576
Chris Lattner97159122009-09-08 03:32:53 +00001577 Value *getVectorOperand() { return Op<0>(); }
1578 Value *getIndexOperand() { return Op<1>(); }
1579 const Value *getVectorOperand() const { return Op<0>(); }
1580 const Value *getIndexOperand() const { return Op<1>(); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001581
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001582 VectorType *getVectorOperandType() const {
1583 return reinterpret_cast<VectorType*>(getVectorOperand()->getType());
Chris Lattner97159122009-09-08 03:32:53 +00001584 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001585
1586
Robert Bocchino49b78a52006-01-10 19:04:13 +00001587 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001588 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001589
1590 // Methods for support type inquiry through isa, cast, and dyn_cast:
1591 static inline bool classof(const ExtractElementInst *) { return true; }
1592 static inline bool classof(const Instruction *I) {
1593 return I->getOpcode() == Instruction::ExtractElement;
1594 }
1595 static inline bool classof(const Value *V) {
1596 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1597 }
1598};
1599
Gabor Greifefe65362008-05-10 08:32:32 +00001600template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001601struct OperandTraits<ExtractElementInst> :
1602 public FixedNumOperandTraits<ExtractElementInst, 2> {
Gabor Greifefe65362008-05-10 08:32:32 +00001603};
1604
1605DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1606
Robert Bocchino49b78a52006-01-10 19:04:13 +00001607//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001608// InsertElementInst Class
1609//===----------------------------------------------------------------------===//
1610
1611/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001612/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001613///
1614class InsertElementInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001615 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001616 const Twine &NameStr = "",
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001617 Instruction *InsertBefore = 0);
Gabor Greif051a9502008-04-06 20:25:17 +00001618 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001619 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001620protected:
1621 virtual InsertElementInst *clone_impl() const;
1622
Robert Bocchinof9993442006-01-17 20:05:59 +00001623public:
Gabor Greif051a9502008-04-06 20:25:17 +00001624 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001625 const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +00001626 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001627 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001628 }
Gabor Greif051a9502008-04-06 20:25:17 +00001629 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001630 const Twine &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001631 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001632 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001633 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001634
Chris Lattnerfa495842006-04-08 04:04:54 +00001635 /// isValidOperands - Return true if an insertelement instruction can be
1636 /// formed with the specified operands.
1637 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1638 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001639
Reid Spencerac9dcb92007-02-15 03:39:18 +00001640 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001641 ///
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001642 VectorType *getType() const {
1643 return reinterpret_cast<VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001644 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001645
Robert Bocchinof9993442006-01-17 20:05:59 +00001646 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001647 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchinof9993442006-01-17 20:05:59 +00001648
1649 // Methods for support type inquiry through isa, cast, and dyn_cast:
1650 static inline bool classof(const InsertElementInst *) { return true; }
1651 static inline bool classof(const Instruction *I) {
1652 return I->getOpcode() == Instruction::InsertElement;
1653 }
1654 static inline bool classof(const Value *V) {
1655 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1656 }
1657};
1658
Gabor Greifefe65362008-05-10 08:32:32 +00001659template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001660struct OperandTraits<InsertElementInst> :
1661 public FixedNumOperandTraits<InsertElementInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001662};
1663
1664DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1665
Robert Bocchinof9993442006-01-17 20:05:59 +00001666//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001667// ShuffleVectorInst Class
1668//===----------------------------------------------------------------------===//
1669
1670/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1671/// input vectors.
1672///
1673class ShuffleVectorInst : public Instruction {
Devang Patel50b6e332009-10-27 22:16:29 +00001674protected:
1675 virtual ShuffleVectorInst *clone_impl() const;
1676
Chris Lattner9fc18d22006-04-08 01:15:18 +00001677public:
Gabor Greif051a9502008-04-06 20:25:17 +00001678 // allocate space for exactly three operands
1679 void *operator new(size_t s) {
1680 return User::operator new(s, 3);
1681 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001682 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001683 const Twine &NameStr = "",
Evan Cheng1bf9a182008-07-24 00:08:56 +00001684 Instruction *InsertBefor = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001685 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001686 const Twine &NameStr, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001687
Chris Lattnerfa495842006-04-08 04:04:54 +00001688 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001689 /// formed with the specified operands.
1690 static bool isValidOperands(const Value *V1, const Value *V2,
1691 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001692
Reid Spencerac9dcb92007-02-15 03:39:18 +00001693 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001694 ///
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001695 VectorType *getType() const {
1696 return reinterpret_cast<VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001697 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001698
Chris Lattner9fc18d22006-04-08 01:15:18 +00001699 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001700 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001701
Chris Lattner83694a92012-01-25 23:49:49 +00001702 Constant *getMask() const {
1703 return reinterpret_cast<Constant*>(getOperand(2));
1704 }
1705
Chris Lattner8728f192008-03-02 05:28:33 +00001706 /// getMaskValue - Return the index from the shuffle mask for the specified
1707 /// output result. This is either -1 if the element is undef or a number less
1708 /// than 2*numelements.
Chris Lattner56243b82012-01-26 02:51:13 +00001709 static int getMaskValue(Constant *Mask, unsigned i);
1710
1711 int getMaskValue(unsigned i) const {
1712 return getMaskValue(getMask(), i);
1713 }
Chris Lattner83694a92012-01-25 23:49:49 +00001714
1715 /// getShuffleMask - Return the full mask for this instruction, where each
1716 /// element is the element number and undef's are returned as -1.
Chris Lattner56243b82012-01-26 02:51:13 +00001717 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1718
1719 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1720 return getShuffleMask(getMask(), Result);
1721 }
Chris Lattner83694a92012-01-25 23:49:49 +00001722
1723 SmallVector<int, 16> getShuffleMask() const {
1724 SmallVector<int, 16> Mask;
1725 getShuffleMask(Mask);
1726 return Mask;
1727 }
1728
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001729
Chris Lattner9fc18d22006-04-08 01:15:18 +00001730 // Methods for support type inquiry through isa, cast, and dyn_cast:
1731 static inline bool classof(const ShuffleVectorInst *) { return true; }
1732 static inline bool classof(const Instruction *I) {
1733 return I->getOpcode() == Instruction::ShuffleVector;
1734 }
1735 static inline bool classof(const Value *V) {
1736 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1737 }
1738};
1739
Gabor Greifefe65362008-05-10 08:32:32 +00001740template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001741struct OperandTraits<ShuffleVectorInst> :
1742 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001743};
1744
1745DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
Chris Lattner9fc18d22006-04-08 01:15:18 +00001746
1747//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001748// ExtractValueInst Class
1749//===----------------------------------------------------------------------===//
1750
Dan Gohmane2d896f2008-05-15 23:35:32 +00001751/// ExtractValueInst - This instruction extracts a struct member or array
1752/// element value from an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001753///
Gabor Greifd4f268b2008-06-06 20:28:12 +00001754class ExtractValueInst : public UnaryInstruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001755 SmallVector<unsigned, 4> Indices;
1756
Dan Gohman041e2eb2008-05-15 19:50:34 +00001757 ExtractValueInst(const ExtractValueInst &EVI);
Jay Foadfc6d3a42011-07-13 10:26:04 +00001758 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001759
Dan Gohmane2d896f2008-05-15 23:35:32 +00001760 /// Constructors - Create a extractvalue instruction with a base aggregate
1761 /// value and a list of indices. The first ctor can optionally insert before
1762 /// an existing instruction, the second appends the new instruction to the
1763 /// specified BasicBlock.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001764 inline ExtractValueInst(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001765 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001766 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001767 Instruction *InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001768 inline ExtractValueInst(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001769 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001770 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001771
Dan Gohman8e640412008-05-31 19:09:47 +00001772 // allocate space for exactly one operand
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001773 void *operator new(size_t s) {
1774 return User::operator new(s, 1);
1775 }
Devang Patel50b6e332009-10-27 22:16:29 +00001776protected:
1777 virtual ExtractValueInst *clone_impl() const;
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001778
Gabor Greifd4f268b2008-06-06 20:28:12 +00001779public:
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001780 static ExtractValueInst *Create(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001781 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001782 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001783 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001784 return new
Jay Foadfc6d3a42011-07-13 10:26:04 +00001785 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001786 }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001787 static ExtractValueInst *Create(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001788 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001789 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001790 BasicBlock *InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001791 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001792 }
1793
Dan Gohman041e2eb2008-05-15 19:50:34 +00001794 /// getIndexedType - Returns the type of the element that would be extracted
1795 /// with an extractvalue instruction with the specified parameters.
1796 ///
Frits van Bommelaf72c622010-12-03 14:54:33 +00001797 /// Null is returned if the indices are invalid for the specified type.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001798 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001799
Owen Anderson5678d6e2008-06-19 17:15:57 +00001800 typedef const unsigned* idx_iterator;
1801 inline idx_iterator idx_begin() const { return Indices.begin(); }
1802 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001803
1804 Value *getAggregateOperand() {
1805 return getOperand(0);
1806 }
1807 const Value *getAggregateOperand() const {
1808 return getOperand(0);
1809 }
1810 static unsigned getAggregateOperandIndex() {
1811 return 0U; // get index for modifying correct operand
1812 }
1813
Jay Foadfc6d3a42011-07-13 10:26:04 +00001814 ArrayRef<unsigned> getIndices() const {
1815 return Indices;
1816 }
1817
1818 unsigned getNumIndices() const {
Bill Wendling67944fc2008-06-05 07:35:27 +00001819 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001820 }
1821
1822 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001823 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001824 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001825
Dan Gohman041e2eb2008-05-15 19:50:34 +00001826 // Methods for support type inquiry through isa, cast, and dyn_cast:
1827 static inline bool classof(const ExtractValueInst *) { return true; }
1828 static inline bool classof(const Instruction *I) {
1829 return I->getOpcode() == Instruction::ExtractValue;
1830 }
1831 static inline bool classof(const Value *V) {
1832 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1833 }
1834};
1835
Dan Gohmane4569942008-05-23 00:36:11 +00001836ExtractValueInst::ExtractValueInst(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001837 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001838 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001839 Instruction *InsertBefore)
Jay Foadfc6d3a42011-07-13 10:26:04 +00001840 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
Bill Wendling85f40542008-07-22 07:14:12 +00001841 ExtractValue, Agg, InsertBefore) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001842 init(Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001843}
Dan Gohmane4569942008-05-23 00:36:11 +00001844ExtractValueInst::ExtractValueInst(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001845 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001846 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001847 BasicBlock *InsertAtEnd)
Jay Foadfc6d3a42011-07-13 10:26:04 +00001848 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
Bill Wendling85f40542008-07-22 07:14:12 +00001849 ExtractValue, Agg, InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001850 init(Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001851}
1852
Dan Gohmane4569942008-05-23 00:36:11 +00001853
Dan Gohman041e2eb2008-05-15 19:50:34 +00001854//===----------------------------------------------------------------------===//
1855// InsertValueInst Class
1856//===----------------------------------------------------------------------===//
1857
Dan Gohmane2d896f2008-05-15 23:35:32 +00001858/// InsertValueInst - This instruction inserts a struct field of array element
1859/// value into an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001860///
1861class InsertValueInst : public Instruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001862 SmallVector<unsigned, 4> Indices;
1863
Craig Topperef1623f2012-09-18 03:25:49 +00001864 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001865 InsertValueInst(const InsertValueInst &IVI);
Jay Foadfc6d3a42011-07-13 10:26:04 +00001866 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001867 const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001868
Dan Gohmane2d896f2008-05-15 23:35:32 +00001869 /// Constructors - Create a insertvalue instruction with a base aggregate
1870 /// value, a value to insert, and a list of indices. The first ctor can
1871 /// optionally insert before an existing instruction, the second appends
1872 /// the new instruction to the specified BasicBlock.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001873 inline InsertValueInst(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001874 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001875 const Twine &NameStr,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001876 Instruction *InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001877 inline InsertValueInst(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001878 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001879 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001880
1881 /// Constructors - These two constructors are convenience methods because one
1882 /// and two index insertvalue instructions are so common.
1883 InsertValueInst(Value *Agg, Value *Val,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001884 unsigned Idx, const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001885 Instruction *InsertBefore = 0);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001886 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001887 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001888protected:
1889 virtual InsertValueInst *clone_impl() const;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001890public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001891 // allocate space for exactly two operands
1892 void *operator new(size_t s) {
1893 return User::operator new(s, 2);
1894 }
1895
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001896 static InsertValueInst *Create(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001897 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001898 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001899 Instruction *InsertBefore = 0) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001900 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001901 }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001902 static InsertValueInst *Create(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001903 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001904 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001905 BasicBlock *InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001906 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001907 }
1908
Dan Gohman041e2eb2008-05-15 19:50:34 +00001909 /// Transparently provide more efficient getOperand methods.
1910 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1911
Owen Anderson5678d6e2008-06-19 17:15:57 +00001912 typedef const unsigned* idx_iterator;
1913 inline idx_iterator idx_begin() const { return Indices.begin(); }
1914 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001915
1916 Value *getAggregateOperand() {
1917 return getOperand(0);
1918 }
1919 const Value *getAggregateOperand() const {
1920 return getOperand(0);
1921 }
1922 static unsigned getAggregateOperandIndex() {
1923 return 0U; // get index for modifying correct operand
1924 }
1925
1926 Value *getInsertedValueOperand() {
1927 return getOperand(1);
1928 }
1929 const Value *getInsertedValueOperand() const {
1930 return getOperand(1);
1931 }
1932 static unsigned getInsertedValueOperandIndex() {
1933 return 1U; // get index for modifying correct operand
1934 }
1935
Jay Foadfc6d3a42011-07-13 10:26:04 +00001936 ArrayRef<unsigned> getIndices() const {
1937 return Indices;
1938 }
1939
1940 unsigned getNumIndices() const {
Bill Wendling67944fc2008-06-05 07:35:27 +00001941 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001942 }
1943
1944 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001945 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001946 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001947
Dan Gohman041e2eb2008-05-15 19:50:34 +00001948 // Methods for support type inquiry through isa, cast, and dyn_cast:
1949 static inline bool classof(const InsertValueInst *) { return true; }
1950 static inline bool classof(const Instruction *I) {
1951 return I->getOpcode() == Instruction::InsertValue;
1952 }
1953 static inline bool classof(const Value *V) {
1954 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1955 }
1956};
1957
1958template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001959struct OperandTraits<InsertValueInst> :
1960 public FixedNumOperandTraits<InsertValueInst, 2> {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001961};
1962
Dan Gohmane4569942008-05-23 00:36:11 +00001963InsertValueInst::InsertValueInst(Value *Agg,
1964 Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001965 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001966 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001967 Instruction *InsertBefore)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001968 : Instruction(Agg->getType(), InsertValue,
1969 OperandTraits<InsertValueInst>::op_begin(this),
1970 2, InsertBefore) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001971 init(Agg, Val, Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001972}
Dan Gohmane4569942008-05-23 00:36:11 +00001973InsertValueInst::InsertValueInst(Value *Agg,
1974 Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001975 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001976 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001977 BasicBlock *InsertAtEnd)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001978 : Instruction(Agg->getType(), InsertValue,
1979 OperandTraits<InsertValueInst>::op_begin(this),
1980 2, InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001981 init(Agg, Val, Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001982}
1983
Dan Gohman041e2eb2008-05-15 19:50:34 +00001984DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1985
1986//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001987// PHINode Class
1988//===----------------------------------------------------------------------===//
1989
1990// PHINode - The PHINode class is used to represent the magical mystical PHI
1991// node, that can not exist in nature, but can be synthesized in a computer
1992// scientist's overactive imagination.
1993//
1994class PHINode : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +00001995 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Chris Lattner454928e2005-01-29 00:31:36 +00001996 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1997 /// the number actually in use.
1998 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001999 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00002000 // allocate space for exactly zero operands
2001 void *operator new(size_t s) {
2002 return User::operator new(s, 0);
2003 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002004 explicit PHINode(Type *Ty, unsigned NumReservedValues,
Jay Foad3ecfc862011-03-30 11:28:46 +00002005 const Twine &NameStr = "", Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00002006 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Jay Foad95c3e482011-06-23 09:09:15 +00002007 ReservedSpace(NumReservedValues) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002008 setName(NameStr);
Jay Foad3ecfc862011-03-30 11:28:46 +00002009 OperandList = allocHungoffUses(ReservedSpace);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002010 }
2011
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002012 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
Jay Foad3ecfc862011-03-30 11:28:46 +00002013 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00002014 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Jay Foad95c3e482011-06-23 09:09:15 +00002015 ReservedSpace(NumReservedValues) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002016 setName(NameStr);
Jay Foad3ecfc862011-03-30 11:28:46 +00002017 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattner454928e2005-01-29 00:31:36 +00002018 }
Devang Patel50b6e332009-10-27 22:16:29 +00002019protected:
Jay Foad95c3e482011-06-23 09:09:15 +00002020 // allocHungoffUses - this is more complicated than the generic
2021 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2022 // values and pointers to the incoming blocks, all in one allocation.
2023 Use *allocHungoffUses(unsigned) const;
2024
Devang Patel50b6e332009-10-27 22:16:29 +00002025 virtual PHINode *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002026public:
Jay Foad44991762011-03-30 13:29:06 +00002027 /// Constructors - NumReservedValues is a hint for the number of incoming
2028 /// edges that this phi node will have (use 0 if you really have no idea).
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002029 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
Jay Foad3ecfc862011-03-30 11:28:46 +00002030 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00002031 Instruction *InsertBefore = 0) {
Jay Foad3ecfc862011-03-30 11:28:46 +00002032 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002033 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002034 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
Jay Foad3ecfc862011-03-30 11:28:46 +00002035 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2036 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002037 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002038 ~PHINode();
2039
Gabor Greifefe65362008-05-10 08:32:32 +00002040 /// Provide fast operand accessors
2041 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2042
Jay Foad95c3e482011-06-23 09:09:15 +00002043 // Block iterator interface. This provides access to the list of incoming
2044 // basic blocks, which parallels the list of incoming values.
2045
2046 typedef BasicBlock **block_iterator;
2047 typedef BasicBlock * const *const_block_iterator;
2048
2049 block_iterator block_begin() {
2050 Use::UserRef *ref =
2051 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2052 return reinterpret_cast<block_iterator>(ref + 1);
2053 }
2054
2055 const_block_iterator block_begin() const {
2056 const Use::UserRef *ref =
2057 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2058 return reinterpret_cast<const_block_iterator>(ref + 1);
2059 }
2060
2061 block_iterator block_end() {
2062 return block_begin() + getNumOperands();
2063 }
2064
2065 const_block_iterator block_end() const {
2066 return block_begin() + getNumOperands();
2067 }
2068
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002069 /// getNumIncomingValues - Return the number of incoming edges
2070 ///
Jay Foad95c3e482011-06-23 09:09:15 +00002071 unsigned getNumIncomingValues() const { return getNumOperands(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002072
Reid Spencerc773de62006-05-19 19:07:54 +00002073 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002074 ///
2075 Value *getIncomingValue(unsigned i) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002076 return getOperand(i);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002077 }
2078 void setIncomingValue(unsigned i, Value *V) {
Jay Foad95c3e482011-06-23 09:09:15 +00002079 setOperand(i, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002080 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00002081 static unsigned getOperandNumForIncomingValue(unsigned i) {
Jay Foad95c3e482011-06-23 09:09:15 +00002082 return i;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002083 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00002084 static unsigned getIncomingValueNumForOperand(unsigned i) {
Jay Foad95c3e482011-06-23 09:09:15 +00002085 return i;
Dan Gohmanb45088c2009-03-23 15:48:29 +00002086 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002087
Dan Gohmanfb76fe02010-02-22 04:10:52 +00002088 /// getIncomingBlock - Return incoming basic block number @p i.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002089 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00002090 BasicBlock *getIncomingBlock(unsigned i) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002091 return block_begin()[i];
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002092 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002093
Chris Lattnerceaa4572009-10-10 07:42:42 +00002094 /// getIncomingBlock - Return incoming basic block corresponding
2095 /// to an operand of the PHI.
2096 ///
2097 BasicBlock *getIncomingBlock(const Use &U) const {
2098 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
Jay Foad95c3e482011-06-23 09:09:15 +00002099 return getIncomingBlock(unsigned(&U - op_begin()));
Chris Lattnerceaa4572009-10-10 07:42:42 +00002100 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002101
Chris Lattnerceaa4572009-10-10 07:42:42 +00002102 /// getIncomingBlock - Return incoming basic block corresponding
2103 /// to value use iterator.
2104 ///
2105 template <typename U>
2106 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
2107 return getIncomingBlock(I.getUse());
2108 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002109
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002110 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Jay Foad95c3e482011-06-23 09:09:15 +00002111 block_begin()[i] = BB;
Dan Gohmanb45088c2009-03-23 15:48:29 +00002112 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002113
2114 /// addIncoming - Add an incoming value to the end of the PHI list
2115 ///
2116 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00002117 assert(V && "PHI node got a null value!");
2118 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002119 assert(getType() == V->getType() &&
2120 "All operands to PHI node must be the same type as the PHI node!");
Jay Foad95c3e482011-06-23 09:09:15 +00002121 if (NumOperands == ReservedSpace)
Jay Foad8891ed72011-04-01 08:00:58 +00002122 growOperands(); // Get more space!
Chris Lattner454928e2005-01-29 00:31:36 +00002123 // Initialize some new operands.
Jay Foad95c3e482011-06-23 09:09:15 +00002124 ++NumOperands;
2125 setIncomingValue(NumOperands - 1, V);
2126 setIncomingBlock(NumOperands - 1, BB);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002127 }
Misha Brukman9769ab22005-04-21 20:19:05 +00002128
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002129 /// removeIncomingValue - Remove an incoming value. This is useful if a
2130 /// predecessor basic block is deleted. The value removed is returned.
2131 ///
2132 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2133 /// is true), the PHI node is destroyed and any uses of it are replaced with
2134 /// dummy values. The only time there should be zero incoming values to a PHI
2135 /// node is when the block is dead, so this strategy is sound.
2136 ///
2137 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2138
Gabor Greifefe65362008-05-10 08:32:32 +00002139 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002140 int Idx = getBasicBlockIndex(BB);
2141 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2142 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2143 }
2144
Misha Brukman9769ab22005-04-21 20:19:05 +00002145 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002146 /// block in the value list for this PHI. Returns -1 if no instance.
2147 ///
2148 int getBasicBlockIndex(const BasicBlock *BB) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002149 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2150 if (block_begin()[i] == BB)
2151 return i;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002152 return -1;
2153 }
2154
2155 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002156 int Idx = getBasicBlockIndex(BB);
2157 assert(Idx >= 0 && "Invalid basic block argument!");
2158 return getIncomingValue(Idx);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002159 }
2160
Chris Lattnerf56a8db2006-10-03 17:09:12 +00002161 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00002162 /// same value, return the value, otherwise return null.
Duncan Sandsff103412010-11-17 04:30:22 +00002163 Value *hasConstantValue() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00002164
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002165 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2166 static inline bool classof(const PHINode *) { return true; }
2167 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00002168 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002169 }
2170 static inline bool classof(const Value *V) {
2171 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2172 }
Chris Lattner454928e2005-01-29 00:31:36 +00002173 private:
Jay Foad8891ed72011-04-01 08:00:58 +00002174 void growOperands();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002175};
2176
Gabor Greifefe65362008-05-10 08:32:32 +00002177template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002178struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00002179};
2180
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002181DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002182
Bill Wendlinge6e88262011-08-12 20:24:12 +00002183//===----------------------------------------------------------------------===//
2184// LandingPadInst Class
2185//===----------------------------------------------------------------------===//
2186
2187//===---------------------------------------------------------------------------
2188/// LandingPadInst - The landingpad instruction holds all of the information
2189/// necessary to generate correct exception handling. The landingpad instruction
2190/// cannot be moved from the top of a landing pad block, which itself is
2191/// accessible only from the 'unwind' edge of an invoke. This uses the
2192/// SubclassData field in Value to store whether or not the landingpad is a
2193/// cleanup.
2194///
2195class LandingPadInst : public Instruction {
2196 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2197 /// the number actually in use.
2198 unsigned ReservedSpace;
2199 LandingPadInst(const LandingPadInst &LP);
2200public:
2201 enum ClauseType { Catch, Filter };
2202private:
Craig Topperef1623f2012-09-18 03:25:49 +00002203 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Bill Wendlinge6e88262011-08-12 20:24:12 +00002204 // Allocate space for exactly zero operands.
2205 void *operator new(size_t s) {
2206 return User::operator new(s, 0);
2207 }
2208 void growOperands(unsigned Size);
2209 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2210
2211 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2212 unsigned NumReservedValues, const Twine &NameStr,
2213 Instruction *InsertBefore);
2214 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2215 unsigned NumReservedValues, const Twine &NameStr,
2216 BasicBlock *InsertAtEnd);
2217protected:
2218 virtual LandingPadInst *clone_impl() const;
2219public:
2220 /// Constructors - NumReservedClauses is a hint for the number of incoming
2221 /// clauses that this landingpad will have (use 0 if you really have no idea).
2222 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2223 unsigned NumReservedClauses,
2224 const Twine &NameStr = "",
2225 Instruction *InsertBefore = 0);
2226 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2227 unsigned NumReservedClauses,
2228 const Twine &NameStr, BasicBlock *InsertAtEnd);
2229 ~LandingPadInst();
2230
2231 /// Provide fast operand accessors
2232 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2233
2234 /// getPersonalityFn - Get the personality function associated with this
2235 /// landing pad.
2236 Value *getPersonalityFn() const { return getOperand(0); }
2237
2238 /// isCleanup - Return 'true' if this landingpad instruction is a
2239 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2240 /// doesn't catch the exception.
2241 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2242
2243 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2244 void setCleanup(bool V) {
2245 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2246 (V ? 1 : 0));
2247 }
2248
2249 /// addClause - Add a catch or filter clause to the landing pad.
2250 void addClause(Value *ClauseVal);
2251
2252 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2253 /// to determine what type of clause this is.
2254 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2255
2256 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2257 bool isCatch(unsigned Idx) const {
2258 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2259 }
2260
2261 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2262 bool isFilter(unsigned Idx) const {
2263 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2264 }
2265
2266 /// getNumClauses - Get the number of clauses for this landing pad.
2267 unsigned getNumClauses() const { return getNumOperands() - 1; }
2268
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00002269 /// reserveClauses - Grow the size of the operand list to accommodate the new
Bill Wendlinge6e88262011-08-12 20:24:12 +00002270 /// number of clauses.
2271 void reserveClauses(unsigned Size) { growOperands(Size); }
2272
2273 // Methods for support type inquiry through isa, cast, and dyn_cast:
2274 static inline bool classof(const LandingPadInst *) { return true; }
2275 static inline bool classof(const Instruction *I) {
2276 return I->getOpcode() == Instruction::LandingPad;
2277 }
2278 static inline bool classof(const Value *V) {
2279 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2280 }
2281};
2282
2283template <>
2284struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2285};
2286
2287DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002288
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002289//===----------------------------------------------------------------------===//
2290// ReturnInst Class
2291//===----------------------------------------------------------------------===//
2292
2293//===---------------------------------------------------------------------------
2294/// ReturnInst - Return a value (possibly void), from a function. Execution
2295/// does not continue in this function any longer.
2296///
2297class ReturnInst : public TerminatorInst {
Chris Lattner910c80a2007-02-24 00:55:48 +00002298 ReturnInst(const ReturnInst &RI);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002299
Gabor Greif051a9502008-04-06 20:25:17 +00002300private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002301 // ReturnInst constructors:
2302 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002303 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002304 // ReturnInst(Value* X) - 'ret X' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00002305 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002306 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
Gabor Greifefe65362008-05-10 08:32:32 +00002307 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2308 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002309 //
2310 // NOTE: If the Value* passed is of type void then the constructor behaves as
2311 // if it was passed NULL.
Owen Anderson1d0be152009-08-13 21:58:54 +00002312 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2313 Instruction *InsertBefore = 0);
2314 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2315 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002316protected:
2317 virtual ReturnInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002318public:
Owen Anderson1d0be152009-08-13 21:58:54 +00002319 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2320 Instruction *InsertBefore = 0) {
2321 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002322 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002323 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2324 BasicBlock *InsertAtEnd) {
2325 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002326 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002327 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2328 return new(0) ReturnInst(C, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002329 }
Devang Patel57ef4f42008-02-23 00:35:18 +00002330 virtual ~ReturnInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002331
Gabor Greifefe65362008-05-10 08:32:32 +00002332 /// Provide fast operand accessors
2333 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Devang Patel64d4e612008-02-26 17:56:20 +00002334
Dan Gohman8faa6af2010-10-06 16:59:24 +00002335 /// Convenience accessor. Returns null if there is no return value.
2336 Value *getReturnValue() const {
2337 return getNumOperands() != 0 ? getOperand(0) : 0;
Devang Patel1eafa062008-03-11 17:35:03 +00002338 }
2339
Chris Lattner454928e2005-01-29 00:31:36 +00002340 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002341
2342 // Methods for support type inquiry through isa, cast, and dyn_cast:
2343 static inline bool classof(const ReturnInst *) { return true; }
2344 static inline bool classof(const Instruction *I) {
2345 return (I->getOpcode() == Instruction::Ret);
2346 }
2347 static inline bool classof(const Value *V) {
2348 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2349 }
Chris Lattner454928e2005-01-29 00:31:36 +00002350 private:
2351 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2352 virtual unsigned getNumSuccessorsV() const;
2353 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002354};
2355
Gabor Greifefe65362008-05-10 08:32:32 +00002356template <>
Jay Foad67c619b2011-01-11 15:07:38 +00002357struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
Gabor Greifefe65362008-05-10 08:32:32 +00002358};
2359
2360DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002361
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002362//===----------------------------------------------------------------------===//
2363// BranchInst Class
2364//===----------------------------------------------------------------------===//
2365
2366//===---------------------------------------------------------------------------
2367/// BranchInst - Conditional or Unconditional Branch instruction.
2368///
2369class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00002370 /// Ops list - Branches are strange. The operands are ordered:
Gabor Greifae5a20a2009-03-12 18:34:49 +00002371 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2372 /// they don't have to check for cond/uncond branchness. These are mostly
2373 /// accessed relative from op_end().
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002374 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00002375 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002376 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2377 // BranchInst(BB *B) - 'br B'
2378 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2379 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2380 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2381 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2382 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00002383 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002384 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002385 Instruction *InsertBefore = 0);
2386 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002387 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002388 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002389protected:
2390 virtual BranchInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002391public:
2392 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
Jay Foad8e3914d2011-01-07 20:29:02 +00002393 return new(1) BranchInst(IfTrue, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002394 }
Gabor Greifefe65362008-05-10 08:32:32 +00002395 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2396 Value *Cond, Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002397 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2398 }
2399 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
Jay Foad8e3914d2011-01-07 20:29:02 +00002400 return new(1) BranchInst(IfTrue, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002401 }
Gabor Greifefe65362008-05-10 08:32:32 +00002402 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2403 Value *Cond, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002404 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2405 }
Chris Lattner454928e2005-01-29 00:31:36 +00002406
2407 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00002408 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002409
Devang Patel4d4a5e02008-02-23 01:11:02 +00002410 bool isUnconditional() const { return getNumOperands() == 1; }
2411 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002412
Devang Patel4d4a5e02008-02-23 01:11:02 +00002413 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002414 assert(isConditional() && "Cannot get condition of an uncond branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002415 return Op<-3>();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002416 }
2417
2418 void setCondition(Value *V) {
2419 assert(isConditional() && "Cannot set condition of unconditional branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002420 Op<-3>() = V;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002421 }
2422
Chris Lattner454928e2005-01-29 00:31:36 +00002423 unsigned getNumSuccessors() const { return 1+isConditional(); }
2424
2425 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002426 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002427 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002428 }
2429
Chris Lattner454928e2005-01-29 00:31:36 +00002430 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002431 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner4b122932009-10-27 16:49:53 +00002432 *(&Op<-1>() - idx) = (Value*)NewSucc;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002433 }
2434
Chandler Carruth602650c2011-10-17 01:11:57 +00002435 /// \brief Swap the successors of this branch instruction.
2436 ///
2437 /// Swaps the successors of the branch instruction. This also swaps any
2438 /// branch weight metadata associated with the instruction so that it
2439 /// continues to map correctly to each operand.
2440 void swapSuccessors();
2441
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002442 // Methods for support type inquiry through isa, cast, and dyn_cast:
2443 static inline bool classof(const BranchInst *) { return true; }
2444 static inline bool classof(const Instruction *I) {
2445 return (I->getOpcode() == Instruction::Br);
2446 }
2447 static inline bool classof(const Value *V) {
2448 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2449 }
Chris Lattner454928e2005-01-29 00:31:36 +00002450private:
2451 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2452 virtual unsigned getNumSuccessorsV() const;
2453 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002454};
2455
Gabor Greifefe65362008-05-10 08:32:32 +00002456template <>
Jay Foad67c619b2011-01-11 15:07:38 +00002457struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2458};
Gabor Greifefe65362008-05-10 08:32:32 +00002459
2460DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2461
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002462//===----------------------------------------------------------------------===//
2463// SwitchInst Class
2464//===----------------------------------------------------------------------===//
2465
2466//===---------------------------------------------------------------------------
2467/// SwitchInst - Multiway switch
2468///
2469class SwitchInst : public TerminatorInst {
Craig Topperef1623f2012-09-18 03:25:49 +00002470 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Chris Lattner454928e2005-01-29 00:31:36 +00002471 unsigned ReservedSpace;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002472 // Operands format:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002473 // Operand[0] = Value to switch on
2474 // Operand[1] = Default basic block destination
2475 // Operand[2n ] = Value to match
2476 // Operand[2n+1] = BasicBlock to go to on match
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002477
2478 // Store case values separately from operands list. We needn't User-Use
2479 // concept here, since it is just a case value, it will always constant,
2480 // and case value couldn't reused with another instructions/values.
2481 // Additionally:
2482 // It allows us to use custom type for case values that is not inherited
2483 // from Value. Since case value is a complex type that implements
2484 // the subset of integers, we needn't extract sub-constants within
2485 // slow getAggregateElement method.
2486 // For case values we will use std::list to by two reasons:
2487 // 1. It allows to add/remove cases without whole collection reallocation.
2488 // 2. In most of cases we needn't random access.
2489 // Currently case values are also stored in Operands List, but it will moved
2490 // out in future commits.
2491 typedef std::list<IntegersSubset> Subsets;
2492 typedef Subsets::iterator SubsetsIt;
2493 typedef Subsets::const_iterator SubsetsConstIt;
2494
2495 Subsets TheSubsets;
2496
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002497 SwitchInst(const SwitchInst &SI);
Chris Lattneraa6e3502010-11-17 05:41:46 +00002498 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
Jay Foad8891ed72011-04-01 08:00:58 +00002499 void growOperands();
Gabor Greifefe65362008-05-10 08:32:32 +00002500 // allocate space for exactly zero operands
2501 void *operator new(size_t s) {
2502 return User::operator new(s, 0);
2503 }
Chris Lattner454928e2005-01-29 00:31:36 +00002504 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2505 /// switch on and a default destination. The number of additional cases can
2506 /// be specified here to make memory allocation more efficient. This
2507 /// constructor can also autoinsert before another instruction.
2508 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002509 Instruction *InsertBefore);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002510
Chris Lattner454928e2005-01-29 00:31:36 +00002511 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2512 /// switch on and a default destination. The number of additional cases can
2513 /// be specified here to make memory allocation more efficient. This
2514 /// constructor also autoinserts at the end of the specified BasicBlock.
2515 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002516 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002517protected:
2518 virtual SwitchInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002519public:
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002520
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002521 // FIXME: Currently there are a lot of unclean template parameters,
2522 // we need to make refactoring in future.
2523 // All these parameters are used to implement both iterator and const_iterator
2524 // without code duplication.
2525 // SwitchInstTy may be "const SwitchInst" or "SwitchInst"
2526 // ConstantIntTy may be "const ConstantInt" or "ConstantInt"
2527 // SubsetsItTy may be SubsetsConstIt or SubsetsIt
2528 // BasicBlockTy may be "const BasicBlock" or "BasicBlock"
2529 template <class SwitchInstTy, class ConstantIntTy,
2530 class SubsetsItTy, class BasicBlockTy>
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002531 class CaseIteratorT;
2532
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002533 typedef CaseIteratorT<const SwitchInst, const ConstantInt,
2534 SubsetsConstIt, const BasicBlock> ConstCaseIt;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002535 class CaseIt;
2536
Aaron Ballmana13fb622012-03-10 23:03:01 +00002537 // -2
2538 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002539
Gabor Greifefe65362008-05-10 08:32:32 +00002540 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2541 unsigned NumCases, Instruction *InsertBefore = 0) {
2542 return new SwitchInst(Value, Default, NumCases, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002543 }
Gabor Greifefe65362008-05-10 08:32:32 +00002544 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2545 unsigned NumCases, BasicBlock *InsertAtEnd) {
2546 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002547 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002548
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002549 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00002550
Gabor Greifefe65362008-05-10 08:32:32 +00002551 /// Provide fast operand accessors
2552 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2553
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002554 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00002555 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00002556 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00002557
Devang Patel4d4a5e02008-02-23 01:11:02 +00002558 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002559 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002560 }
2561
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002562 void setDefaultDest(BasicBlock *DefaultCase) {
2563 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2564 }
2565
2566 /// getNumCases - return the number of 'cases' in this switch instruction,
2567 /// except the default case
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002568 unsigned getNumCases() const {
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002569 return getNumOperands()/2 - 1;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002570 }
2571
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002572 /// Returns a read/write iterator that points to the first
2573 /// case in SwitchInst.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002574 CaseIt case_begin() {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002575 return CaseIt(this, 0, TheSubsets.begin());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002576 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002577 /// Returns a read-only iterator that points to the first
2578 /// case in the SwitchInst.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002579 ConstCaseIt case_begin() const {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002580 return ConstCaseIt(this, 0, TheSubsets.begin());
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002581 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002582
2583 /// Returns a read/write iterator that points one past the last
2584 /// in the SwitchInst.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002585 CaseIt case_end() {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002586 return CaseIt(this, getNumCases(), TheSubsets.end());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002587 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002588 /// Returns a read-only iterator that points one past the last
2589 /// in the SwitchInst.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002590 ConstCaseIt case_end() const {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002591 return ConstCaseIt(this, getNumCases(), TheSubsets.end());
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002592 }
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002593 /// Returns an iterator that points to the default case.
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002594 /// Note: this iterator allows to resolve successor only. Attempt
2595 /// to resolve case value causes an assertion.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002596 /// Also note, that increment and decrement also causes an assertion and
2597 /// makes iterator invalid.
2598 CaseIt case_default() {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002599 return CaseIt(this, DefaultPseudoIndex, TheSubsets.end());
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002600 }
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002601 ConstCaseIt case_default() const {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002602 return ConstCaseIt(this, DefaultPseudoIndex, TheSubsets.end());
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002603 }
2604
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002605 /// findCaseValue - Search all of the case values for the specified constant.
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002606 /// If it is explicitly handled, return the case iterator of it, otherwise
2607 /// return default case iterator to indicate
2608 /// that it is handled by the default handler.
2609 CaseIt findCaseValue(const ConstantInt *C) {
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002610 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
Stepan Dyatkovskiyf8d14c42012-06-01 10:06:14 +00002611 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002612 return i;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002613 return case_default();
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002614 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002615 ConstCaseIt findCaseValue(const ConstantInt *C) const {
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002616 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
Stepan Dyatkovskiyf8d14c42012-06-01 10:06:14 +00002617 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002618 return i;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002619 return case_default();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002620 }
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002621
Nick Lewycky011f1842006-09-18 19:03:59 +00002622 /// findCaseDest - Finds the unique case value for a given successor. Returns
2623 /// null if the successor is not found, not unique, or is the default case.
2624 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00002625 if (BB == getDefaultDest()) return NULL;
2626
Nick Lewycky011f1842006-09-18 19:03:59 +00002627 ConstantInt *CI = NULL;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002628 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002629 if (i.getCaseSuccessor() == BB) {
Nick Lewycky011f1842006-09-18 19:03:59 +00002630 if (CI) return NULL; // Multiple cases lead to BB.
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002631 else CI = i.getCaseValue();
Nick Lewycky011f1842006-09-18 19:03:59 +00002632 }
2633 }
2634 return CI;
2635 }
2636
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002637 /// addCase - Add an entry to the switch instruction...
Dmitri Gribenko67c89782012-09-12 16:59:47 +00002638 /// @deprecated
Stepan Dyatkovskiyc347b6f2012-03-13 12:37:10 +00002639 /// Note:
2640 /// This action invalidates case_end(). Old case_end() iterator will
2641 /// point to the added case.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002642 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002643
2644 /// addCase - Add an entry to the switch instruction.
2645 /// Note:
2646 /// This action invalidates case_end(). Old case_end() iterator will
2647 /// point to the added case.
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00002648 void addCase(IntegersSubset& OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002649
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002650 /// removeCase - This method removes the specified case and its successor
2651 /// from the switch instruction. Note that this operation may reorder the
Jay Foad0faa6092011-02-01 09:22:34 +00002652 /// remaining cases at index idx and above.
Stepan Dyatkovskiyc347b6f2012-03-13 12:37:10 +00002653 /// Note:
2654 /// This action invalidates iterators for all cases following the one removed,
2655 /// including the case_end() iterator.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002656 void removeCase(CaseIt& i);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002657
Chris Lattner454928e2005-01-29 00:31:36 +00002658 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2659 BasicBlock *getSuccessor(unsigned idx) const {
2660 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2661 return cast<BasicBlock>(getOperand(idx*2+1));
2662 }
2663 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002664 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Chris Lattner4b122932009-10-27 16:49:53 +00002665 setOperand(idx*2+1, (Value*)NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002666 }
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002667
Stepan Dyatkovskiy734dde82012-05-14 08:26:31 +00002668 uint16_t hash() const {
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002669 uint32_t NumberOfCases = (uint32_t)getNumCases();
2670 uint16_t Hash = (0xFFFF & NumberOfCases) ^ (NumberOfCases >> 16);
2671 for (ConstCaseIt i = case_begin(), e = case_end();
2672 i != e; ++i) {
2673 uint32_t NumItems = (uint32_t)i.getCaseValueEx().getNumItems();
2674 Hash = (Hash << 1) ^ (0xFFFF & NumItems) ^ (NumItems >> 16);
2675 }
2676 return Hash;
2677 }
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002678
2679 // Case iterators definition.
2680
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002681 template <class SwitchInstTy, class ConstantIntTy,
2682 class SubsetsItTy, class BasicBlockTy>
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002683 class CaseIteratorT {
2684 protected:
2685
2686 SwitchInstTy *SI;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002687 unsigned long Index;
2688 SubsetsItTy SubsetIt;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002689
2690 /// Initializes case iterator for given SwitchInst and for given
2691 /// case number.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002692 friend class SwitchInst;
2693 CaseIteratorT(SwitchInstTy *SI, unsigned SuccessorIndex,
2694 SubsetsItTy CaseValueIt) {
Duncan Sands37eeb052012-06-22 10:35:06 +00002695 this->SI = SI;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002696 Index = SuccessorIndex;
2697 this->SubsetIt = CaseValueIt;
Duncan Sands37eeb052012-06-22 10:35:06 +00002698 }
Stepan Dyatkovskiy73512562012-06-22 07:35:13 +00002699
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002700 public:
2701 typedef typename SubsetsItTy::reference IntegersSubsetRef;
2702 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy,
2703 SubsetsItTy, BasicBlockTy> Self;
2704
2705 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2706 this->SI = SI;
2707 Index = CaseNum;
2708 SubsetIt = SI->TheSubsets.begin();
2709 std::advance(SubsetIt, CaseNum);
2710 }
2711
2712
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002713 /// Initializes case iterator for given SwitchInst and for given
2714 /// TerminatorInst's successor index.
2715 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2716 assert(SuccessorIndex < SI->getNumSuccessors() &&
2717 "Successor index # out of range!");
2718 return SuccessorIndex != 0 ?
2719 Self(SI, SuccessorIndex - 1) :
2720 Self(SI, DefaultPseudoIndex);
2721 }
2722
2723 /// Resolves case value for current case.
Dmitri Gribenko67c89782012-09-12 16:59:47 +00002724 /// @deprecated
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002725 ConstantIntTy *getCaseValue() {
2726 assert(Index < SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002727 IntegersSubsetRef CaseRanges = *SubsetIt;
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00002728
2729 // FIXME: Currently we work with ConstantInt based cases.
2730 // So return CaseValue as ConstantInt.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002731 return CaseRanges.getSingleNumber(0).toConstantInt();
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002732 }
2733
2734 /// Resolves case value for current case.
Stepan Dyatkovskiy47cbc4e2012-06-23 10:58:58 +00002735 IntegersSubsetRef getCaseValueEx() {
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002736 assert(Index < SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002737 return *SubsetIt;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002738 }
2739
2740 /// Resolves successor for current case.
2741 BasicBlockTy *getCaseSuccessor() {
2742 assert((Index < SI->getNumCases() ||
2743 Index == DefaultPseudoIndex) &&
2744 "Index out the number of cases.");
2745 return SI->getSuccessor(getSuccessorIndex());
2746 }
2747
2748 /// Returns number of current case.
2749 unsigned getCaseIndex() const { return Index; }
2750
2751 /// Returns TerminatorInst's successor index for current case successor.
2752 unsigned getSuccessorIndex() const {
2753 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2754 "Index out the number of cases.");
2755 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2756 }
2757
2758 Self operator++() {
2759 // Check index correctness after increment.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002760 // Note: Index == getNumCases() means end().
Kaelyn Uhrain35ac4b02012-06-22 17:18:15 +00002761 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002762 ++Index;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002763 if (Index == 0)
2764 SubsetIt = SI->TheSubsets.begin();
2765 else
2766 ++SubsetIt;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002767 return *this;
2768 }
2769 Self operator++(int) {
2770 Self tmp = *this;
2771 ++(*this);
2772 return tmp;
2773 }
2774 Self operator--() {
2775 // Check index correctness after decrement.
2776 // Note: Index == getNumCases() means end().
2777 // Also allow "-1" iterator here. That will became valid after ++.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002778 unsigned NumCases = SI->getNumCases();
2779 assert((Index == 0 || Index-1 <= NumCases) &&
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002780 "Index out the number of cases.");
2781 --Index;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002782 if (Index == NumCases) {
2783 SubsetIt = SI->TheSubsets.end();
2784 return *this;
2785 }
2786
2787 if (Index != -1UL)
2788 --SubsetIt;
2789
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002790 return *this;
2791 }
2792 Self operator--(int) {
2793 Self tmp = *this;
2794 --(*this);
2795 return tmp;
2796 }
2797 bool operator==(const Self& RHS) const {
2798 assert(RHS.SI == SI && "Incompatible operators.");
2799 return RHS.Index == Index;
2800 }
2801 bool operator!=(const Self& RHS) const {
2802 assert(RHS.SI == SI && "Incompatible operators.");
2803 return RHS.Index != Index;
2804 }
2805 };
2806
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002807 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt,
2808 SubsetsIt, BasicBlock> {
2809 typedef CaseIteratorT<SwitchInst, ConstantInt, SubsetsIt, BasicBlock>
2810 ParentTy;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002811
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002812 protected:
2813 friend class SwitchInst;
2814 CaseIt(SwitchInst *SI, unsigned CaseNum, SubsetsIt SubsetIt) :
2815 ParentTy(SI, CaseNum, SubsetIt) {}
2816
2817 void updateCaseValueOperand(IntegersSubset& V) {
2818 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>((Constant*)V));
2819 }
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002820
2821 public:
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002822
2823 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002824
2825 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002826
2827 /// Sets the new value for current case.
Dmitri Gribenko67c89782012-09-12 16:59:47 +00002828 /// @deprecated.
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002829 void setValue(ConstantInt *V) {
2830 assert(Index < SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00002831 IntegersSubsetToBB Mapping;
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00002832 // FIXME: Currently we work with ConstantInt based cases.
2833 // So inititalize IntItem container directly from ConstantInt.
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00002834 Mapping.add(IntItem::fromConstantInt(V));
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002835 *SubsetIt = Mapping.getCase();
2836 updateCaseValueOperand(*SubsetIt);
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002837 }
2838
2839 /// Sets the new value for current case.
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00002840 void setValueEx(IntegersSubset& V) {
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002841 assert(Index < SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002842 *SubsetIt = V;
2843 updateCaseValueOperand(*SubsetIt);
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002844 }
2845
2846 /// Sets the new successor for current case.
2847 void setSuccessor(BasicBlock *S) {
2848 SI->setSuccessor(getSuccessorIndex(), S);
2849 }
2850 };
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002851
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002852 // Methods for support type inquiry through isa, cast, and dyn_cast:
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002853
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002854 static inline bool classof(const SwitchInst *) { return true; }
2855 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00002856 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002857 }
2858 static inline bool classof(const Value *V) {
2859 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2860 }
Chris Lattner454928e2005-01-29 00:31:36 +00002861private:
2862 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2863 virtual unsigned getNumSuccessorsV() const;
2864 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002865};
2866
Gabor Greifefe65362008-05-10 08:32:32 +00002867template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002868struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00002869};
2870
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002871DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002872
2873
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002874//===----------------------------------------------------------------------===//
Chris Lattnerab21db72009-10-28 00:19:10 +00002875// IndirectBrInst Class
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002876//===----------------------------------------------------------------------===//
2877
2878//===---------------------------------------------------------------------------
Chris Lattnerab21db72009-10-28 00:19:10 +00002879/// IndirectBrInst - Indirect Branch Instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002880///
Chris Lattnerab21db72009-10-28 00:19:10 +00002881class IndirectBrInst : public TerminatorInst {
Craig Topperef1623f2012-09-18 03:25:49 +00002882 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002883 unsigned ReservedSpace;
2884 // Operand[0] = Value to switch on
2885 // Operand[1] = Default basic block destination
2886 // Operand[2n ] = Value to match
2887 // Operand[2n+1] = BasicBlock to go to on match
Chris Lattnerab21db72009-10-28 00:19:10 +00002888 IndirectBrInst(const IndirectBrInst &IBI);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002889 void init(Value *Address, unsigned NumDests);
Jay Foad8891ed72011-04-01 08:00:58 +00002890 void growOperands();
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002891 // allocate space for exactly zero operands
2892 void *operator new(size_t s) {
2893 return User::operator new(s, 0);
2894 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002895 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2896 /// Address to jump to. The number of expected destinations can be specified
2897 /// here to make memory allocation more efficient. This constructor can also
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002898 /// autoinsert before another instruction.
Chris Lattnerab21db72009-10-28 00:19:10 +00002899 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002900
Chris Lattnerab21db72009-10-28 00:19:10 +00002901 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2902 /// Address to jump to. The number of expected destinations can be specified
2903 /// here to make memory allocation more efficient. This constructor also
2904 /// autoinserts at the end of the specified BasicBlock.
2905 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002906protected:
Chris Lattnerab21db72009-10-28 00:19:10 +00002907 virtual IndirectBrInst *clone_impl() const;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002908public:
Chris Lattnerab21db72009-10-28 00:19:10 +00002909 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2910 Instruction *InsertBefore = 0) {
2911 return new IndirectBrInst(Address, NumDests, InsertBefore);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002912 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002913 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2914 BasicBlock *InsertAtEnd) {
2915 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002916 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002917 ~IndirectBrInst();
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002918
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002919 /// Provide fast operand accessors.
2920 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002921
Chris Lattnerab21db72009-10-28 00:19:10 +00002922 // Accessor Methods for IndirectBrInst instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002923 Value *getAddress() { return getOperand(0); }
2924 const Value *getAddress() const { return getOperand(0); }
2925 void setAddress(Value *V) { setOperand(0, V); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002926
2927
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002928 /// getNumDestinations - return the number of possible destinations in this
Chris Lattnerab21db72009-10-28 00:19:10 +00002929 /// indirectbr instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002930 unsigned getNumDestinations() const { return getNumOperands()-1; }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002931
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002932 /// getDestination - Return the specified destination.
2933 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2934 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002935
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002936 /// addDestination - Add a destination.
2937 ///
2938 void addDestination(BasicBlock *Dest);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002939
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002940 /// removeDestination - This method removes the specified successor from the
Chris Lattnerab21db72009-10-28 00:19:10 +00002941 /// indirectbr instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002942 void removeDestination(unsigned i);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002943
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002944 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2945 BasicBlock *getSuccessor(unsigned i) const {
2946 return cast<BasicBlock>(getOperand(i+1));
2947 }
2948 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2949 setOperand(i+1, (Value*)NewSucc);
2950 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002951
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002952 // Methods for support type inquiry through isa, cast, and dyn_cast:
Chris Lattnerab21db72009-10-28 00:19:10 +00002953 static inline bool classof(const IndirectBrInst *) { return true; }
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002954 static inline bool classof(const Instruction *I) {
Chris Lattnerab21db72009-10-28 00:19:10 +00002955 return I->getOpcode() == Instruction::IndirectBr;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002956 }
2957 static inline bool classof(const Value *V) {
2958 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2959 }
2960private:
2961 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2962 virtual unsigned getNumSuccessorsV() const;
2963 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2964};
2965
2966template <>
Chris Lattnerab21db72009-10-28 00:19:10 +00002967struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002968};
2969
Chris Lattnerab21db72009-10-28 00:19:10 +00002970DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002971
2972
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002973//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002974// InvokeInst Class
2975//===----------------------------------------------------------------------===//
2976
Chris Lattner3340ffe2005-05-06 20:26:26 +00002977/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2978/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002979///
2980class InvokeInst : public TerminatorInst {
Devang Patel05988662008-09-25 21:00:45 +00002981 AttrListPtr AttributeList;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002982 InvokeInst(const InvokeInst &BI);
David Greenef1355a52007-08-27 19:04:21 +00002983 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002984 ArrayRef<Value *> Args, const Twine &NameStr);
David Greenef1355a52007-08-27 19:04:21 +00002985
David Greenef1355a52007-08-27 19:04:21 +00002986 /// Construct an InvokeInst given a range of arguments.
David Greenef1355a52007-08-27 19:04:21 +00002987 ///
2988 /// @brief Construct an InvokeInst from a range of arguments
Gabor Greifefe65362008-05-10 08:32:32 +00002989 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002990 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002991 const Twine &NameStr, Instruction *InsertBefore);
David Greenef1355a52007-08-27 19:04:21 +00002992
2993 /// Construct an InvokeInst given a range of arguments.
David Greenef1355a52007-08-27 19:04:21 +00002994 ///
2995 /// @brief Construct an InvokeInst from a range of arguments
Gabor Greifefe65362008-05-10 08:32:32 +00002996 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002997 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002998 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002999protected:
3000 virtual InvokeInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00003001public:
Gabor Greifefe65362008-05-10 08:32:32 +00003002 static InvokeInst *Create(Value *Func,
3003 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00003004 ArrayRef<Value *> Args, const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00003005 Instruction *InsertBefore = 0) {
Jay Foada3efbb12011-07-15 08:37:34 +00003006 unsigned Values = unsigned(Args.size()) + 3;
3007 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
Evan Cheng1bf9a182008-07-24 00:08:56 +00003008 Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00003009 }
Gabor Greifefe65362008-05-10 08:32:32 +00003010 static InvokeInst *Create(Value *Func,
3011 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00003012 ArrayRef<Value *> Args, const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00003013 BasicBlock *InsertAtEnd) {
Jay Foada3efbb12011-07-15 08:37:34 +00003014 unsigned Values = unsigned(Args.size()) + 3;
3015 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
Evan Cheng1bf9a182008-07-24 00:08:56 +00003016 Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00003017 }
David Greenef1355a52007-08-27 19:04:21 +00003018
Gabor Greifefe65362008-05-10 08:32:32 +00003019 /// Provide fast operand accessors
3020 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00003021
Gabor Greif0114b992010-07-31 08:35:21 +00003022 /// getNumArgOperands - Return the number of invoke arguments.
3023 ///
Bill Wendling22a5b292010-06-07 19:05:06 +00003024 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
Gabor Greif0114b992010-07-31 08:35:21 +00003025
3026 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3027 ///
Bill Wendling22a5b292010-06-07 19:05:06 +00003028 Value *getArgOperand(unsigned i) const { return getOperand(i); }
Gabor Greif710ac072010-06-28 12:23:36 +00003029 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
Bill Wendling22a5b292010-06-07 19:05:06 +00003030
Chris Lattner3340ffe2005-05-06 20:26:26 +00003031 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3032 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003033 CallingConv::ID getCallingConv() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +00003034 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003035 }
3036 void setCallingConv(CallingConv::ID CC) {
Chris Lattnerb2406d92009-12-29 02:46:09 +00003037 setInstructionSubclassData(static_cast<unsigned>(CC));
Chris Lattner3340ffe2005-05-06 20:26:26 +00003038 }
3039
Devang Patel05988662008-09-25 21:00:45 +00003040 /// getAttributes - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00003041 ///
Devang Patel05988662008-09-25 21:00:45 +00003042 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00003043
Devang Patel05988662008-09-25 21:00:45 +00003044 /// setAttributes - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00003045 ///
Devang Patel05988662008-09-25 21:00:45 +00003046 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00003047
Devang Patel05988662008-09-25 21:00:45 +00003048 /// addAttribute - adds the attribute to the list of attributes.
3049 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00003050
Devang Patel05988662008-09-25 21:00:45 +00003051 /// removeAttribute - removes the attribute from the list of attributes.
3052 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00003053
Bill Wendling060f20a2012-10-09 00:28:54 +00003054 /// @brief Determine whether this call has the NoAlias attribute.
3055 bool fnHasNoAliasAttr() const;
3056 bool fnHasNoInlineAttr() const;
3057 bool fnHasNoReturnAttr() const;
3058 bool fnHasNoUnwindAttr() const;
3059 bool fnHasReadNoneAttr() const;
3060 bool fnHasReadOnlyAttr() const;
3061 bool fnHasReturnsTwiceAttr() const;
3062
Nuno Lopes986da6c2012-06-25 16:16:58 +00003063 /// \brief Return true if this call has the given attribute.
3064 bool hasFnAttr(Attributes N) const {
3065 return paramHasAttr(~0, N);
3066 }
3067
Bill Wendling847d1652012-10-03 17:54:26 +00003068 /// @brief Determine whether the call or the callee has the given attributes.
3069 bool paramHasSExtAttr(unsigned i) const;
3070 bool paramHasZExtAttr(unsigned i) const;
3071 bool paramHasInRegAttr(unsigned i) const;
3072 bool paramHasStructRetAttr(unsigned i) const;
3073 bool paramHasNestAttr(unsigned i) const;
3074 bool paramHasByValAttr(unsigned i) const;
Bill Wendling5df15c62012-10-04 06:52:09 +00003075 bool paramHasNoAliasAttr(unsigned i) const;
Bill Wendling365b9e02012-10-04 07:18:12 +00003076 bool paramHasNoCaptureAttr(unsigned i) const;
Bill Wendling847d1652012-10-03 17:54:26 +00003077
Dan Gohmanf2752502008-09-26 21:38:45 +00003078 /// @brief Determine whether the call or the callee has the given attribute.
3079 bool paramHasAttr(unsigned i, Attributes attr) const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00003080
Dale Johannesen08e78b12008-02-22 17:49:45 +00003081 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003082 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00003083 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003084 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00003085
Eric Christopherf27e6082010-03-25 04:49:10 +00003086 /// @brief Return true if the call should not be inlined.
Bill Wendling060f20a2012-10-09 00:28:54 +00003087 bool isNoInline() const { return fnHasNoInlineAttr(); }
Nick Lewyckyb9933b82010-07-06 03:53:22 +00003088 void setIsNoInline(bool Value = true) {
Eric Christopherf27e6082010-03-25 04:49:10 +00003089 if (Value) addAttribute(~0, Attribute::NoInline);
3090 else removeAttribute(~0, Attribute::NoInline);
3091 }
Nick Lewyckyb9933b82010-07-06 03:53:22 +00003092
Duncan Sandsa3355ff2007-12-03 20:06:50 +00003093 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003094 bool doesNotAccessMemory() const {
Bill Wendling060f20a2012-10-09 00:28:54 +00003095 return fnHasReadNoneAttr();
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003096 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00003097 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00003098 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
3099 else removeAttribute(~0, Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00003100 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00003101
3102 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003103 bool onlyReadsMemory() const {
Bill Wendling060f20a2012-10-09 00:28:54 +00003104 return doesNotAccessMemory() || fnHasReadOnlyAttr();
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003105 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00003106 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00003107 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
3108 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00003109 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00003110
Duncan Sandscbb8bad2007-12-10 19:09:40 +00003111 /// @brief Determine if the call cannot return.
Bill Wendling060f20a2012-10-09 00:28:54 +00003112 bool doesNotReturn() const { return fnHasNoReturnAttr(); }
Evan Cheng1bf9a182008-07-24 00:08:56 +00003113 void setDoesNotReturn(bool DoesNotReturn = true) {
Devang Patel19c87462008-09-26 22:53:05 +00003114 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
3115 else removeAttribute(~0, Attribute::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00003116 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00003117
Duncan Sandsa3355ff2007-12-03 20:06:50 +00003118 /// @brief Determine if the call cannot unwind.
Bill Wendling060f20a2012-10-09 00:28:54 +00003119 bool doesNotThrow() const { return fnHasNoUnwindAttr(); }
Evan Cheng1bf9a182008-07-24 00:08:56 +00003120 void setDoesNotThrow(bool DoesNotThrow = true) {
Devang Patel19c87462008-09-26 22:53:05 +00003121 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
3122 else removeAttribute(~0, Attribute::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00003123 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00003124
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00003125 /// @brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00003126 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003127 bool hasStructRetAttr() const {
3128 // Be friendly and also check the callee.
Bill Wendling060f20a2012-10-09 00:28:54 +00003129 return paramHasStructRetAttr(1);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003130 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00003131
Dan Gohmanf2752502008-09-26 21:38:45 +00003132 /// @brief Determine if any call argument is an aggregate passed by value.
3133 bool hasByValArgument() const {
Bill Wendling2fd77652012-10-09 01:03:48 +00003134 for (unsigned I = 0, E = AttributeList.getNumAttrs(); I != E; ++I)
3135 if (AttributeList.getAttributesAtIndex(I).hasByValAttr())
3136 return true;
3137 return false;
Dan Gohmanf2752502008-09-26 21:38:45 +00003138 }
3139
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003140 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00003141 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003142 ///
Chris Lattner721aef62004-11-18 17:46:57 +00003143 Function *getCalledFunction() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00003144 return dyn_cast<Function>(Op<-3>());
Chris Lattner721aef62004-11-18 17:46:57 +00003145 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003146
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00003147 /// getCalledValue - Get a pointer to the function that is invoked by this
Dan Gohmanf2752502008-09-26 21:38:45 +00003148 /// instruction
Gabor Greifc9f75002010-03-24 13:21:49 +00003149 const Value *getCalledValue() const { return Op<-3>(); }
3150 Value *getCalledValue() { return Op<-3>(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003151
Gabor Greif654c06f2010-03-20 21:00:25 +00003152 /// setCalledFunction - Set the function called.
3153 void setCalledFunction(Value* Fn) {
Gabor Greifc9f75002010-03-24 13:21:49 +00003154 Op<-3>() = Fn;
Gabor Greif654c06f2010-03-20 21:00:25 +00003155 }
3156
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003157 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00003158 BasicBlock *getNormalDest() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00003159 return cast<BasicBlock>(Op<-2>());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003160 }
Chris Lattner454928e2005-01-29 00:31:36 +00003161 BasicBlock *getUnwindDest() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00003162 return cast<BasicBlock>(Op<-1>());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003163 }
Chris Lattner454928e2005-01-29 00:31:36 +00003164 void setNormalDest(BasicBlock *B) {
Gabor Greifc9f75002010-03-24 13:21:49 +00003165 Op<-2>() = reinterpret_cast<Value*>(B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003166 }
Chris Lattner454928e2005-01-29 00:31:36 +00003167 void setUnwindDest(BasicBlock *B) {
Gabor Greifc9f75002010-03-24 13:21:49 +00003168 Op<-1>() = reinterpret_cast<Value*>(B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003169 }
3170
Bill Wendlinge6e88262011-08-12 20:24:12 +00003171 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3172 /// block (the unwind destination).
3173 LandingPadInst *getLandingPadInst() const;
3174
Devang Patel4d4a5e02008-02-23 01:11:02 +00003175 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003176 assert(i < 2 && "Successor # out of range for invoke!");
3177 return i == 0 ? getNormalDest() : getUnwindDest();
3178 }
3179
Chris Lattner454928e2005-01-29 00:31:36 +00003180 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003181 assert(idx < 2 && "Successor # out of range for invoke!");
Gabor Greifc9f75002010-03-24 13:21:49 +00003182 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003183 }
3184
Chris Lattner454928e2005-01-29 00:31:36 +00003185 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003186
3187 // Methods for support type inquiry through isa, cast, and dyn_cast:
3188 static inline bool classof(const InvokeInst *) { return true; }
3189 static inline bool classof(const Instruction *I) {
3190 return (I->getOpcode() == Instruction::Invoke);
3191 }
3192 static inline bool classof(const Value *V) {
3193 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3194 }
Gabor Greifc9f75002010-03-24 13:21:49 +00003195
Chris Lattner454928e2005-01-29 00:31:36 +00003196private:
3197 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3198 virtual unsigned getNumSuccessorsV() const;
3199 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00003200
Chris Lattnerb2406d92009-12-29 02:46:09 +00003201 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3202 // method so that subclasses cannot accidentally use it.
3203 void setInstructionSubclassData(unsigned short D) {
3204 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00003205 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003206};
3207
Gabor Greifefe65362008-05-10 08:32:32 +00003208template <>
Jay Foad67c619b2011-01-11 15:07:38 +00003209struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00003210};
3211
Gabor Greifefe65362008-05-10 08:32:32 +00003212InvokeInst::InvokeInst(Value *Func,
3213 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00003214 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003215 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00003216 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3217 ->getElementType())->getReturnType(),
3218 Instruction::Invoke,
3219 OperandTraits<InvokeInst>::op_end(this) - Values,
3220 Values, InsertBefore) {
Jay Foada3efbb12011-07-15 08:37:34 +00003221 init(Func, IfNormal, IfException, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00003222}
Gabor Greifefe65362008-05-10 08:32:32 +00003223InvokeInst::InvokeInst(Value *Func,
3224 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00003225 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003226 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00003227 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3228 ->getElementType())->getReturnType(),
3229 Instruction::Invoke,
3230 OperandTraits<InvokeInst>::op_end(this) - Values,
3231 Values, InsertAtEnd) {
Jay Foada3efbb12011-07-15 08:37:34 +00003232 init(Func, IfNormal, IfException, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00003233}
3234
3235DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003236
3237//===----------------------------------------------------------------------===//
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003238// ResumeInst Class
3239//===----------------------------------------------------------------------===//
3240
3241//===---------------------------------------------------------------------------
3242/// ResumeInst - Resume the propagation of an exception.
3243///
3244class ResumeInst : public TerminatorInst {
3245 ResumeInst(const ResumeInst &RI);
3246
3247 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3248 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3249protected:
3250 virtual ResumeInst *clone_impl() const;
3251public:
3252 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3253 return new(1) ResumeInst(Exn, InsertBefore);
3254 }
3255 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3256 return new(1) ResumeInst(Exn, InsertAtEnd);
3257 }
3258
3259 /// Provide fast operand accessors
3260 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3261
3262 /// Convenience accessor.
3263 Value *getValue() const { return Op<0>(); }
3264
3265 unsigned getNumSuccessors() const { return 0; }
3266
3267 // Methods for support type inquiry through isa, cast, and dyn_cast:
3268 static inline bool classof(const ResumeInst *) { return true; }
3269 static inline bool classof(const Instruction *I) {
3270 return I->getOpcode() == Instruction::Resume;
3271 }
3272 static inline bool classof(const Value *V) {
3273 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3274 }
3275private:
3276 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3277 virtual unsigned getNumSuccessorsV() const;
3278 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3279};
3280
3281template <>
3282struct OperandTraits<ResumeInst> :
3283 public FixedNumOperandTraits<ResumeInst, 1> {
3284};
3285
3286DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3287
3288//===----------------------------------------------------------------------===//
Chris Lattner076b3f12004-10-16 18:05:54 +00003289// UnreachableInst Class
3290//===----------------------------------------------------------------------===//
3291
3292//===---------------------------------------------------------------------------
3293/// UnreachableInst - This function has undefined behavior. In particular, the
3294/// presence of this instruction indicates some higher level knowledge that the
3295/// end of the block cannot be reached.
3296///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00003297class UnreachableInst : public TerminatorInst {
Craig Topperef1623f2012-09-18 03:25:49 +00003298 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Devang Patel50b6e332009-10-27 22:16:29 +00003299protected:
3300 virtual UnreachableInst *clone_impl() const;
3301
Chris Lattner1fca5ff2004-10-27 16:14:51 +00003302public:
Gabor Greif051a9502008-04-06 20:25:17 +00003303 // allocate space for exactly zero operands
3304 void *operator new(size_t s) {
3305 return User::operator new(s, 0);
3306 }
Owen Anderson1d0be152009-08-13 21:58:54 +00003307 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3308 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00003309
Chris Lattner454928e2005-01-29 00:31:36 +00003310 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00003311
3312 // Methods for support type inquiry through isa, cast, and dyn_cast:
3313 static inline bool classof(const UnreachableInst *) { return true; }
3314 static inline bool classof(const Instruction *I) {
3315 return I->getOpcode() == Instruction::Unreachable;
3316 }
3317 static inline bool classof(const Value *V) {
3318 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3319 }
Chris Lattner454928e2005-01-29 00:31:36 +00003320private:
3321 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3322 virtual unsigned getNumSuccessorsV() const;
3323 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00003324};
3325
Reid Spencer3da59db2006-11-27 01:05:10 +00003326//===----------------------------------------------------------------------===//
3327// TruncInst Class
3328//===----------------------------------------------------------------------===//
3329
3330/// @brief This class represents a truncation of integer types.
3331class TruncInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003332protected:
3333 /// @brief Clone an identical TruncInst
3334 virtual TruncInst *clone_impl() const;
3335
Reid Spencer3da59db2006-11-27 01:05:10 +00003336public:
3337 /// @brief Constructor with insert-before-instruction semantics
3338 TruncInst(
3339 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003340 Type *Ty, ///< The (smaller) type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003341 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003342 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3343 );
3344
3345 /// @brief Constructor with insert-at-end-of-block semantics
3346 TruncInst(
3347 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003348 Type *Ty, ///< The (smaller) type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003349 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003350 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3351 );
3352
Reid Spencer3da59db2006-11-27 01:05:10 +00003353 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3354 static inline bool classof(const TruncInst *) { return true; }
3355 static inline bool classof(const Instruction *I) {
3356 return I->getOpcode() == Trunc;
3357 }
3358 static inline bool classof(const Value *V) {
3359 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3360 }
3361};
3362
3363//===----------------------------------------------------------------------===//
3364// ZExtInst Class
3365//===----------------------------------------------------------------------===//
3366
3367/// @brief This class represents zero extension of integer types.
3368class ZExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003369protected:
3370 /// @brief Clone an identical ZExtInst
3371 virtual ZExtInst *clone_impl() const;
3372
Reid Spencer3da59db2006-11-27 01:05:10 +00003373public:
3374 /// @brief Constructor with insert-before-instruction semantics
3375 ZExtInst(
3376 Value *S, ///< The value to be zero extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003377 Type *Ty, ///< The type to zero extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003378 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003379 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3380 );
3381
3382 /// @brief Constructor with insert-at-end semantics.
3383 ZExtInst(
3384 Value *S, ///< The value to be zero extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003385 Type *Ty, ///< The type to zero extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003386 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003387 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3388 );
3389
Reid Spencer3da59db2006-11-27 01:05:10 +00003390 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3391 static inline bool classof(const ZExtInst *) { return true; }
3392 static inline bool classof(const Instruction *I) {
3393 return I->getOpcode() == ZExt;
3394 }
3395 static inline bool classof(const Value *V) {
3396 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3397 }
3398};
3399
3400//===----------------------------------------------------------------------===//
3401// SExtInst Class
3402//===----------------------------------------------------------------------===//
3403
3404/// @brief This class represents a sign extension of integer types.
3405class SExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003406protected:
3407 /// @brief Clone an identical SExtInst
3408 virtual SExtInst *clone_impl() const;
3409
Reid Spencer3da59db2006-11-27 01:05:10 +00003410public:
3411 /// @brief Constructor with insert-before-instruction semantics
3412 SExtInst(
3413 Value *S, ///< The value to be sign extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003414 Type *Ty, ///< The type to sign extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003415 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003416 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3417 );
3418
3419 /// @brief Constructor with insert-at-end-of-block semantics
3420 SExtInst(
3421 Value *S, ///< The value to be sign extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003422 Type *Ty, ///< The type to sign extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003423 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003424 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3425 );
3426
Reid Spencer3da59db2006-11-27 01:05:10 +00003427 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3428 static inline bool classof(const SExtInst *) { return true; }
3429 static inline bool classof(const Instruction *I) {
3430 return I->getOpcode() == SExt;
3431 }
3432 static inline bool classof(const Value *V) {
3433 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3434 }
3435};
3436
3437//===----------------------------------------------------------------------===//
3438// FPTruncInst Class
3439//===----------------------------------------------------------------------===//
3440
3441/// @brief This class represents a truncation of floating point types.
3442class FPTruncInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003443protected:
3444 /// @brief Clone an identical FPTruncInst
3445 virtual FPTruncInst *clone_impl() const;
3446
Reid Spencer3da59db2006-11-27 01:05:10 +00003447public:
3448 /// @brief Constructor with insert-before-instruction semantics
3449 FPTruncInst(
3450 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003451 Type *Ty, ///< The type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003452 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003453 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3454 );
3455
3456 /// @brief Constructor with insert-before-instruction semantics
3457 FPTruncInst(
3458 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003459 Type *Ty, ///< The type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003460 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003461 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3462 );
3463
Reid Spencer3da59db2006-11-27 01:05:10 +00003464 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3465 static inline bool classof(const FPTruncInst *) { return true; }
3466 static inline bool classof(const Instruction *I) {
3467 return I->getOpcode() == FPTrunc;
3468 }
3469 static inline bool classof(const Value *V) {
3470 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3471 }
3472};
3473
3474//===----------------------------------------------------------------------===//
3475// FPExtInst Class
3476//===----------------------------------------------------------------------===//
3477
3478/// @brief This class represents an extension of floating point types.
3479class FPExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003480protected:
3481 /// @brief Clone an identical FPExtInst
3482 virtual FPExtInst *clone_impl() const;
3483
Reid Spencer3da59db2006-11-27 01:05:10 +00003484public:
3485 /// @brief Constructor with insert-before-instruction semantics
3486 FPExtInst(
3487 Value *S, ///< The value to be extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003488 Type *Ty, ///< The type to extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003489 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003490 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3491 );
3492
3493 /// @brief Constructor with insert-at-end-of-block semantics
3494 FPExtInst(
3495 Value *S, ///< The value to be extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003496 Type *Ty, ///< The type to extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003497 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003498 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3499 );
3500
Reid Spencer3da59db2006-11-27 01:05:10 +00003501 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3502 static inline bool classof(const FPExtInst *) { return true; }
3503 static inline bool classof(const Instruction *I) {
3504 return I->getOpcode() == FPExt;
3505 }
3506 static inline bool classof(const Value *V) {
3507 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3508 }
3509};
3510
3511//===----------------------------------------------------------------------===//
3512// UIToFPInst Class
3513//===----------------------------------------------------------------------===//
3514
3515/// @brief This class represents a cast unsigned integer to floating point.
3516class UIToFPInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003517protected:
3518 /// @brief Clone an identical UIToFPInst
3519 virtual UIToFPInst *clone_impl() const;
3520
Reid Spencer3da59db2006-11-27 01:05:10 +00003521public:
3522 /// @brief Constructor with insert-before-instruction semantics
3523 UIToFPInst(
3524 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003525 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003526 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003527 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3528 );
3529
3530 /// @brief Constructor with insert-at-end-of-block semantics
3531 UIToFPInst(
3532 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003533 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003534 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003535 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3536 );
3537
Reid Spencer3da59db2006-11-27 01:05:10 +00003538 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3539 static inline bool classof(const UIToFPInst *) { return true; }
3540 static inline bool classof(const Instruction *I) {
3541 return I->getOpcode() == UIToFP;
3542 }
3543 static inline bool classof(const Value *V) {
3544 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3545 }
3546};
3547
3548//===----------------------------------------------------------------------===//
3549// SIToFPInst Class
3550//===----------------------------------------------------------------------===//
3551
3552/// @brief This class represents a cast from signed integer to floating point.
3553class SIToFPInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003554protected:
3555 /// @brief Clone an identical SIToFPInst
3556 virtual SIToFPInst *clone_impl() const;
3557
Reid Spencer3da59db2006-11-27 01:05:10 +00003558public:
3559 /// @brief Constructor with insert-before-instruction semantics
3560 SIToFPInst(
3561 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003562 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003563 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003564 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3565 );
3566
3567 /// @brief Constructor with insert-at-end-of-block semantics
3568 SIToFPInst(
3569 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003570 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003571 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003572 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3573 );
3574
Reid Spencer3da59db2006-11-27 01:05:10 +00003575 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3576 static inline bool classof(const SIToFPInst *) { return true; }
3577 static inline bool classof(const Instruction *I) {
3578 return I->getOpcode() == SIToFP;
3579 }
3580 static inline bool classof(const Value *V) {
3581 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3582 }
3583};
3584
3585//===----------------------------------------------------------------------===//
3586// FPToUIInst Class
3587//===----------------------------------------------------------------------===//
3588
3589/// @brief This class represents a cast from floating point to unsigned integer
3590class FPToUIInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003591protected:
3592 /// @brief Clone an identical FPToUIInst
3593 virtual FPToUIInst *clone_impl() const;
3594
Reid Spencer3da59db2006-11-27 01:05:10 +00003595public:
3596 /// @brief Constructor with insert-before-instruction semantics
3597 FPToUIInst(
3598 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003599 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003600 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003601 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3602 );
3603
3604 /// @brief Constructor with insert-at-end-of-block semantics
3605 FPToUIInst(
3606 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003607 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003608 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003609 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3610 );
3611
Reid Spencer3da59db2006-11-27 01:05:10 +00003612 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3613 static inline bool classof(const FPToUIInst *) { return true; }
3614 static inline bool classof(const Instruction *I) {
3615 return I->getOpcode() == FPToUI;
3616 }
3617 static inline bool classof(const Value *V) {
3618 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3619 }
3620};
3621
3622//===----------------------------------------------------------------------===//
3623// FPToSIInst Class
3624//===----------------------------------------------------------------------===//
3625
3626/// @brief This class represents a cast from floating point to signed integer.
3627class FPToSIInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003628protected:
3629 /// @brief Clone an identical FPToSIInst
3630 virtual FPToSIInst *clone_impl() const;
3631
Reid Spencer3da59db2006-11-27 01:05:10 +00003632public:
3633 /// @brief Constructor with insert-before-instruction semantics
3634 FPToSIInst(
3635 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003636 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003637 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003638 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3639 );
3640
3641 /// @brief Constructor with insert-at-end-of-block semantics
3642 FPToSIInst(
3643 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003644 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003645 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003646 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3647 );
3648
Reid Spencer3da59db2006-11-27 01:05:10 +00003649 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3650 static inline bool classof(const FPToSIInst *) { return true; }
3651 static inline bool classof(const Instruction *I) {
3652 return I->getOpcode() == FPToSI;
3653 }
3654 static inline bool classof(const Value *V) {
3655 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3656 }
3657};
3658
3659//===----------------------------------------------------------------------===//
3660// IntToPtrInst Class
3661//===----------------------------------------------------------------------===//
3662
3663/// @brief This class represents a cast from an integer to a pointer.
3664class IntToPtrInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00003665public:
3666 /// @brief Constructor with insert-before-instruction semantics
3667 IntToPtrInst(
3668 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003669 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003670 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003671 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3672 );
3673
3674 /// @brief Constructor with insert-at-end-of-block semantics
3675 IntToPtrInst(
3676 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003677 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003678 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003679 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3680 );
3681
3682 /// @brief Clone an identical IntToPtrInst
Devang Patel50b6e332009-10-27 22:16:29 +00003683 virtual IntToPtrInst *clone_impl() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00003684
3685 // Methods for support type inquiry through isa, cast, and dyn_cast:
3686 static inline bool classof(const IntToPtrInst *) { return true; }
3687 static inline bool classof(const Instruction *I) {
3688 return I->getOpcode() == IntToPtr;
3689 }
3690 static inline bool classof(const Value *V) {
3691 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3692 }
3693};
3694
3695//===----------------------------------------------------------------------===//
3696// PtrToIntInst Class
3697//===----------------------------------------------------------------------===//
3698
3699/// @brief This class represents a cast from a pointer to an integer
3700class PtrToIntInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003701protected:
3702 /// @brief Clone an identical PtrToIntInst
3703 virtual PtrToIntInst *clone_impl() const;
3704
Reid Spencer3da59db2006-11-27 01:05:10 +00003705public:
3706 /// @brief Constructor with insert-before-instruction semantics
3707 PtrToIntInst(
3708 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003709 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003710 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003711 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3712 );
3713
3714 /// @brief Constructor with insert-at-end-of-block semantics
3715 PtrToIntInst(
3716 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003717 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003718 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003719 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3720 );
3721
Reid Spencer3da59db2006-11-27 01:05:10 +00003722 // Methods for support type inquiry through isa, cast, and dyn_cast:
3723 static inline bool classof(const PtrToIntInst *) { return true; }
3724 static inline bool classof(const Instruction *I) {
3725 return I->getOpcode() == PtrToInt;
3726 }
3727 static inline bool classof(const Value *V) {
3728 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3729 }
3730};
3731
3732//===----------------------------------------------------------------------===//
3733// BitCastInst Class
3734//===----------------------------------------------------------------------===//
3735
3736/// @brief This class represents a no-op cast from one type to another.
3737class BitCastInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003738protected:
3739 /// @brief Clone an identical BitCastInst
3740 virtual BitCastInst *clone_impl() const;
3741
Reid Spencer3da59db2006-11-27 01:05:10 +00003742public:
3743 /// @brief Constructor with insert-before-instruction semantics
3744 BitCastInst(
3745 Value *S, ///< The value to be casted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003746 Type *Ty, ///< The type to casted to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003747 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003748 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3749 );
3750
3751 /// @brief Constructor with insert-at-end-of-block semantics
3752 BitCastInst(
3753 Value *S, ///< The value to be casted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003754 Type *Ty, ///< The type to casted to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003755 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003756 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3757 );
3758
Reid Spencer3da59db2006-11-27 01:05:10 +00003759 // Methods for support type inquiry through isa, cast, and dyn_cast:
3760 static inline bool classof(const BitCastInst *) { return true; }
3761 static inline bool classof(const Instruction *I) {
3762 return I->getOpcode() == BitCast;
3763 }
3764 static inline bool classof(const Value *V) {
3765 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3766 }
3767};
3768
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003769} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00003770
3771#endif