blob: e3fd2161c828c5371343167470a197d5769dc56f [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:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000115 static inline bool classof(const Instruction *I) {
116 return (I->getOpcode() == Instruction::Alloca);
117 }
118 static inline bool classof(const Value *V) {
119 return isa<Instruction>(V) && classof(cast<Instruction>(V));
120 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000121private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000122 // Shadow Instruction::setInstructionSubclassData with a private forwarding
123 // method so that subclasses cannot accidentally use it.
124 void setInstructionSubclassData(unsigned short D) {
125 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000126 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000127};
128
129
130//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000131// LoadInst Class
132//===----------------------------------------------------------------------===//
133
Chris Lattner88fe29a2005-02-05 01:44:18 +0000134/// LoadInst - an instruction for reading from memory. This uses the
135/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000136///
Chris Lattner454928e2005-01-29 00:31:36 +0000137class LoadInst : public UnaryInstruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000138 void AssertOK();
Devang Patel50b6e332009-10-27 22:16:29 +0000139protected:
140 virtual LoadInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000141public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000142 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
143 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000144 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
Christopher Lamb43c7f372007-04-22 19:24:39 +0000145 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000146 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000147 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000148 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Eli Friedman21006d42011-08-09 23:02:53 +0000149 unsigned Align, Instruction *InsertBefore = 0);
150 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000151 unsigned Align, BasicBlock *InsertAtEnd);
Eli Friedman21006d42011-08-09 23:02:53 +0000152 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
153 unsigned Align, AtomicOrdering Order,
154 SynchronizationScope SynchScope = CrossThread,
155 Instruction *InsertBefore = 0);
156 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
157 unsigned Align, AtomicOrdering Order,
158 SynchronizationScope SynchScope,
159 BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000160
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000161 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
162 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
163 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
164 bool isVolatile = false, Instruction *InsertBefore = 0);
165 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
166 BasicBlock *InsertAtEnd);
167
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000168 /// isVolatile - Return true if this is a load from a volatile memory
169 /// location.
170 ///
Chris Lattnerb2406d92009-12-29 02:46:09 +0000171 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000172
173 /// setVolatile - Specify whether this is a volatile load or not.
174 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000175 void setVolatile(bool V) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000176 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
177 (V ? 1 : 0));
Christopher Lamb43c7f372007-04-22 19:24:39 +0000178 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000179
Christopher Lamb43c7f372007-04-22 19:24:39 +0000180 /// getAlignment - Return the alignment of the access that is being performed
181 ///
182 unsigned getAlignment() const {
Eli Friedman21006d42011-08-09 23:02:53 +0000183 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000184 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000185
Christopher Lamb43c7f372007-04-22 19:24:39 +0000186 void setAlignment(unsigned Align);
187
Eli Friedman21006d42011-08-09 23:02:53 +0000188 /// Returns the ordering effect of this fence.
189 AtomicOrdering getOrdering() const {
190 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
191 }
192
193 /// Set the ordering constraint on this load. May not be Release or
194 /// AcquireRelease.
195 void setOrdering(AtomicOrdering Ordering) {
196 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
197 (Ordering << 7));
198 }
199
200 SynchronizationScope getSynchScope() const {
201 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
202 }
203
204 /// Specify whether this load is ordered with respect to all
205 /// concurrently executing threads, or only with respect to signal handlers
206 /// executing in the same thread.
207 void setSynchScope(SynchronizationScope xthread) {
208 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
209 (xthread << 6));
210 }
211
212 bool isAtomic() const { return getOrdering() != NotAtomic; }
213 void setAtomic(AtomicOrdering Ordering,
214 SynchronizationScope SynchScope = CrossThread) {
215 setOrdering(Ordering);
216 setSynchScope(SynchScope);
217 }
218
219 bool isSimple() const { return !isAtomic() && !isVolatile(); }
220 bool isUnordered() const {
221 return getOrdering() <= Unordered && !isVolatile();
222 }
223
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000224 Value *getPointerOperand() { return getOperand(0); }
225 const Value *getPointerOperand() const { return getOperand(0); }
226 static unsigned getPointerOperandIndex() { return 0U; }
227
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000228 unsigned getPointerAddressSpace() const {
Chandler Carruth8fb614c2012-11-01 09:37:49 +0000229 return getPointerOperand()->getType()->getPointerAddressSpace();
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000230 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000231
232
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000233 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000234 static inline bool classof(const Instruction *I) {
235 return I->getOpcode() == Instruction::Load;
236 }
237 static inline bool classof(const Value *V) {
238 return isa<Instruction>(V) && classof(cast<Instruction>(V));
239 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000240private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000241 // Shadow Instruction::setInstructionSubclassData with a private forwarding
242 // method so that subclasses cannot accidentally use it.
243 void setInstructionSubclassData(unsigned short D) {
244 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000245 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000246};
247
248
249//===----------------------------------------------------------------------===//
250// StoreInst Class
251//===----------------------------------------------------------------------===//
252
Misha Brukman9769ab22005-04-21 20:19:05 +0000253/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000254///
255class StoreInst : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +0000256 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Chris Lattner454928e2005-01-29 00:31:36 +0000257 void AssertOK();
Devang Patel50b6e332009-10-27 22:16:29 +0000258protected:
259 virtual StoreInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000260public:
Gabor Greif051a9502008-04-06 20:25:17 +0000261 // allocate space for exactly two operands
262 void *operator new(size_t s) {
263 return User::operator new(s, 2);
264 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000265 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
266 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
267 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
268 Instruction *InsertBefore = 0);
269 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000270 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
Eli Friedman21006d42011-08-09 23:02:53 +0000271 unsigned Align, Instruction *InsertBefore = 0);
272 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
Dan Gohman6ab2d182007-07-18 20:51:11 +0000273 unsigned Align, BasicBlock *InsertAtEnd);
Eli Friedman21006d42011-08-09 23:02:53 +0000274 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
275 unsigned Align, AtomicOrdering Order,
276 SynchronizationScope SynchScope = CrossThread,
277 Instruction *InsertBefore = 0);
278 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
279 unsigned Align, AtomicOrdering Order,
280 SynchronizationScope SynchScope,
281 BasicBlock *InsertAtEnd);
282
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000283
Eli Friedman21006d42011-08-09 23:02:53 +0000284 /// isVolatile - Return true if this is a store to a volatile memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000285 /// location.
286 ///
Chris Lattnerb2406d92009-12-29 02:46:09 +0000287 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000288
Eli Friedman21006d42011-08-09 23:02:53 +0000289 /// setVolatile - Specify whether this is a volatile store or not.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000290 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000291 void setVolatile(bool V) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000292 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
293 (V ? 1 : 0));
Christopher Lamb43c7f372007-04-22 19:24:39 +0000294 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000295
Chris Lattner454928e2005-01-29 00:31:36 +0000296 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +0000297 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner454928e2005-01-29 00:31:36 +0000298
Christopher Lamb43c7f372007-04-22 19:24:39 +0000299 /// getAlignment - Return the alignment of the access that is being performed
300 ///
301 unsigned getAlignment() const {
Eli Friedman21006d42011-08-09 23:02:53 +0000302 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000303 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000304
Christopher Lamb43c7f372007-04-22 19:24:39 +0000305 void setAlignment(unsigned Align);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000306
Eli Friedman21006d42011-08-09 23:02:53 +0000307 /// Returns the ordering effect of this store.
308 AtomicOrdering getOrdering() const {
309 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
310 }
311
312 /// Set the ordering constraint on this store. May not be Acquire or
313 /// AcquireRelease.
314 void setOrdering(AtomicOrdering Ordering) {
315 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
316 (Ordering << 7));
317 }
318
319 SynchronizationScope getSynchScope() const {
320 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
321 }
322
323 /// Specify whether this store instruction is ordered with respect to all
324 /// concurrently executing threads, or only with respect to signal handlers
325 /// executing in the same thread.
326 void setSynchScope(SynchronizationScope xthread) {
327 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
328 (xthread << 6));
329 }
330
331 bool isAtomic() const { return getOrdering() != NotAtomic; }
332 void setAtomic(AtomicOrdering Ordering,
333 SynchronizationScope SynchScope = CrossThread) {
334 setOrdering(Ordering);
335 setSynchScope(SynchScope);
336 }
337
338 bool isSimple() const { return !isAtomic() && !isVolatile(); }
339 bool isUnordered() const {
340 return getOrdering() <= Unordered && !isVolatile();
341 }
342
Chris Lattner41c9c0e2010-06-26 23:26:37 +0000343 Value *getValueOperand() { return getOperand(0); }
344 const Value *getValueOperand() const { return getOperand(0); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000345
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000346 Value *getPointerOperand() { return getOperand(1); }
347 const Value *getPointerOperand() const { return getOperand(1); }
348 static unsigned getPointerOperandIndex() { return 1U; }
349
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000350 unsigned getPointerAddressSpace() const {
Chandler Carruth8fb614c2012-11-01 09:37:49 +0000351 return getPointerOperand()->getType()->getPointerAddressSpace();
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000352 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000353
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000354 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000355 static inline bool classof(const Instruction *I) {
356 return I->getOpcode() == Instruction::Store;
357 }
358 static inline bool classof(const Value *V) {
359 return isa<Instruction>(V) && classof(cast<Instruction>(V));
360 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000361private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000362 // Shadow Instruction::setInstructionSubclassData with a private forwarding
363 // method so that subclasses cannot accidentally use it.
364 void setInstructionSubclassData(unsigned short D) {
365 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000366 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000367};
368
Gabor Greifefe65362008-05-10 08:32:32 +0000369template <>
Jay Foad67c619b2011-01-11 15:07:38 +0000370struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
Gabor Greifefe65362008-05-10 08:32:32 +0000371};
372
373DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000374
375//===----------------------------------------------------------------------===//
Eli Friedman47f35132011-07-25 23:16:38 +0000376// FenceInst Class
377//===----------------------------------------------------------------------===//
378
379/// FenceInst - an instruction for ordering other memory operations
380///
381class FenceInst : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +0000382 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Eli Friedman47f35132011-07-25 23:16:38 +0000383 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
384protected:
385 virtual FenceInst *clone_impl() const;
386public:
387 // allocate space for exactly zero operands
388 void *operator new(size_t s) {
389 return User::operator new(s, 0);
390 }
391
392 // Ordering may only be Acquire, Release, AcquireRelease, or
393 // SequentiallyConsistent.
394 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
395 SynchronizationScope SynchScope = CrossThread,
396 Instruction *InsertBefore = 0);
397 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
398 SynchronizationScope SynchScope,
399 BasicBlock *InsertAtEnd);
400
401 /// Returns the ordering effect of this fence.
402 AtomicOrdering getOrdering() const {
403 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
404 }
405
406 /// Set the ordering constraint on this fence. May only be Acquire, Release,
407 /// AcquireRelease, or SequentiallyConsistent.
408 void setOrdering(AtomicOrdering Ordering) {
Eli Friedman21006d42011-08-09 23:02:53 +0000409 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
410 (Ordering << 1));
Eli Friedman47f35132011-07-25 23:16:38 +0000411 }
412
413 SynchronizationScope getSynchScope() const {
414 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
415 }
416
417 /// Specify whether this fence orders other operations with respect to all
418 /// concurrently executing threads, or only with respect to signal handlers
419 /// executing in the same thread.
420 void setSynchScope(SynchronizationScope xthread) {
421 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
422 xthread);
423 }
424
425 // Methods for support type inquiry through isa, cast, and dyn_cast:
Eli Friedman47f35132011-07-25 23:16:38 +0000426 static inline bool classof(const Instruction *I) {
427 return I->getOpcode() == Instruction::Fence;
428 }
429 static inline bool classof(const Value *V) {
430 return isa<Instruction>(V) && classof(cast<Instruction>(V));
431 }
432private:
433 // Shadow Instruction::setInstructionSubclassData with a private forwarding
434 // method so that subclasses cannot accidentally use it.
435 void setInstructionSubclassData(unsigned short D) {
436 Instruction::setInstructionSubclassData(D);
437 }
438};
439
440//===----------------------------------------------------------------------===//
Eli Friedmanff030482011-07-28 21:48:00 +0000441// AtomicCmpXchgInst Class
442//===----------------------------------------------------------------------===//
443
444/// AtomicCmpXchgInst - an instruction that atomically checks whether a
445/// specified value is in a memory location, and, if it is, stores a new value
446/// there. Returns the value that was loaded.
447///
448class AtomicCmpXchgInst : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +0000449 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Eli Friedmanff030482011-07-28 21:48:00 +0000450 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
451 AtomicOrdering Ordering, SynchronizationScope SynchScope);
452protected:
453 virtual AtomicCmpXchgInst *clone_impl() const;
454public:
455 // allocate space for exactly three operands
456 void *operator new(size_t s) {
457 return User::operator new(s, 3);
458 }
459 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
460 AtomicOrdering Ordering, SynchronizationScope SynchScope,
461 Instruction *InsertBefore = 0);
462 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
463 AtomicOrdering Ordering, SynchronizationScope SynchScope,
464 BasicBlock *InsertAtEnd);
465
466 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
467 /// location.
468 ///
469 bool isVolatile() const {
470 return getSubclassDataFromInstruction() & 1;
471 }
472
473 /// setVolatile - Specify whether this is a volatile cmpxchg.
474 ///
475 void setVolatile(bool V) {
476 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
477 (unsigned)V);
478 }
479
480 /// Transparently provide more efficient getOperand methods.
481 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
482
483 /// Set the ordering constraint on this cmpxchg.
484 void setOrdering(AtomicOrdering Ordering) {
485 assert(Ordering != NotAtomic &&
486 "CmpXchg instructions can only be atomic.");
487 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
488 (Ordering << 2));
489 }
490
491 /// Specify whether this cmpxchg is atomic and orders other operations with
492 /// respect to all concurrently executing threads, or only with respect to
493 /// signal handlers executing in the same thread.
494 void setSynchScope(SynchronizationScope SynchScope) {
495 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
496 (SynchScope << 1));
497 }
498
499 /// Returns the ordering constraint on this cmpxchg.
500 AtomicOrdering getOrdering() const {
501 return AtomicOrdering(getSubclassDataFromInstruction() >> 2);
502 }
503
504 /// Returns whether this cmpxchg is atomic between threads or only within a
505 /// single thread.
506 SynchronizationScope getSynchScope() const {
507 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
508 }
509
510 Value *getPointerOperand() { return getOperand(0); }
511 const Value *getPointerOperand() const { return getOperand(0); }
512 static unsigned getPointerOperandIndex() { return 0U; }
513
514 Value *getCompareOperand() { return getOperand(1); }
515 const Value *getCompareOperand() const { return getOperand(1); }
516
517 Value *getNewValOperand() { return getOperand(2); }
518 const Value *getNewValOperand() const { return getOperand(2); }
519
520 unsigned getPointerAddressSpace() const {
Chandler Carruth8fb614c2012-11-01 09:37:49 +0000521 return getPointerOperand()->getType()->getPointerAddressSpace();
Eli Friedmanff030482011-07-28 21:48:00 +0000522 }
523
524 // Methods for support type inquiry through isa, cast, and dyn_cast:
Eli Friedmanff030482011-07-28 21:48:00 +0000525 static inline bool classof(const Instruction *I) {
526 return I->getOpcode() == Instruction::AtomicCmpXchg;
527 }
528 static inline bool classof(const Value *V) {
529 return isa<Instruction>(V) && classof(cast<Instruction>(V));
530 }
531private:
532 // Shadow Instruction::setInstructionSubclassData with a private forwarding
533 // method so that subclasses cannot accidentally use it.
534 void setInstructionSubclassData(unsigned short D) {
535 Instruction::setInstructionSubclassData(D);
536 }
537};
538
539template <>
540struct OperandTraits<AtomicCmpXchgInst> :
541 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
542};
543
544DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
545
546//===----------------------------------------------------------------------===//
547// AtomicRMWInst Class
548//===----------------------------------------------------------------------===//
549
550/// AtomicRMWInst - an instruction that atomically reads a memory location,
551/// combines it with another value, and then stores the result back. Returns
552/// the old value.
553///
554class AtomicRMWInst : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +0000555 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Eli Friedmanff030482011-07-28 21:48:00 +0000556protected:
557 virtual AtomicRMWInst *clone_impl() const;
558public:
559 /// This enumeration lists the possible modifications atomicrmw can make. In
560 /// the descriptions, 'p' is the pointer to the instruction's memory location,
561 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
562 /// instruction. These instructions always return 'old'.
563 enum BinOp {
564 /// *p = v
565 Xchg,
566 /// *p = old + v
567 Add,
568 /// *p = old - v
569 Sub,
570 /// *p = old & v
571 And,
572 /// *p = ~old & v
573 Nand,
574 /// *p = old | v
575 Or,
576 /// *p = old ^ v
577 Xor,
578 /// *p = old >signed v ? old : v
579 Max,
580 /// *p = old <signed v ? old : v
581 Min,
582 /// *p = old >unsigned v ? old : v
583 UMax,
584 /// *p = old <unsigned v ? old : v
585 UMin,
586
587 FIRST_BINOP = Xchg,
588 LAST_BINOP = UMin,
589 BAD_BINOP
590 };
591
592 // allocate space for exactly two operands
593 void *operator new(size_t s) {
594 return User::operator new(s, 2);
595 }
596 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
597 AtomicOrdering Ordering, SynchronizationScope SynchScope,
598 Instruction *InsertBefore = 0);
599 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
600 AtomicOrdering Ordering, SynchronizationScope SynchScope,
601 BasicBlock *InsertAtEnd);
602
603 BinOp getOperation() const {
604 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
605 }
606
607 void setOperation(BinOp Operation) {
608 unsigned short SubclassData = getSubclassDataFromInstruction();
609 setInstructionSubclassData((SubclassData & 31) |
610 (Operation << 5));
611 }
612
613 /// isVolatile - Return true if this is a RMW on a volatile memory location.
614 ///
615 bool isVolatile() const {
616 return getSubclassDataFromInstruction() & 1;
617 }
618
619 /// setVolatile - Specify whether this is a volatile RMW or not.
620 ///
621 void setVolatile(bool V) {
622 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
623 (unsigned)V);
624 }
625
626 /// Transparently provide more efficient getOperand methods.
627 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
628
629 /// Set the ordering constraint on this RMW.
630 void setOrdering(AtomicOrdering Ordering) {
631 assert(Ordering != NotAtomic &&
632 "atomicrmw instructions can only be atomic.");
Eli Friedman21006d42011-08-09 23:02:53 +0000633 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
Eli Friedmanff030482011-07-28 21:48:00 +0000634 (Ordering << 2));
635 }
636
637 /// Specify whether this RMW orders other operations with respect to all
638 /// concurrently executing threads, or only with respect to signal handlers
639 /// executing in the same thread.
640 void setSynchScope(SynchronizationScope SynchScope) {
641 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
642 (SynchScope << 1));
643 }
644
645 /// Returns the ordering constraint on this RMW.
646 AtomicOrdering getOrdering() const {
Eli Friedman21006d42011-08-09 23:02:53 +0000647 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
Eli Friedmanff030482011-07-28 21:48:00 +0000648 }
649
650 /// Returns whether this RMW is atomic between threads or only within a
651 /// single thread.
652 SynchronizationScope getSynchScope() const {
653 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
654 }
655
656 Value *getPointerOperand() { return getOperand(0); }
657 const Value *getPointerOperand() const { return getOperand(0); }
658 static unsigned getPointerOperandIndex() { return 0U; }
659
660 Value *getValOperand() { return getOperand(1); }
661 const Value *getValOperand() const { return getOperand(1); }
662
663 unsigned getPointerAddressSpace() const {
Chandler Carruth8fb614c2012-11-01 09:37:49 +0000664 return getPointerOperand()->getType()->getPointerAddressSpace();
Eli Friedmanff030482011-07-28 21:48:00 +0000665 }
666
667 // Methods for support type inquiry through isa, cast, and dyn_cast:
Eli Friedmanff030482011-07-28 21:48:00 +0000668 static inline bool classof(const Instruction *I) {
669 return I->getOpcode() == Instruction::AtomicRMW;
670 }
671 static inline bool classof(const Value *V) {
672 return isa<Instruction>(V) && classof(cast<Instruction>(V));
673 }
674private:
675 void Init(BinOp Operation, Value *Ptr, Value *Val,
676 AtomicOrdering Ordering, SynchronizationScope SynchScope);
677 // Shadow Instruction::setInstructionSubclassData with a private forwarding
678 // method so that subclasses cannot accidentally use it.
679 void setInstructionSubclassData(unsigned short D) {
680 Instruction::setInstructionSubclassData(D);
681 }
682};
683
684template <>
685struct OperandTraits<AtomicRMWInst>
686 : public FixedNumOperandTraits<AtomicRMWInst,2> {
687};
688
689DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
690
691//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000692// GetElementPtrInst Class
693//===----------------------------------------------------------------------===//
694
Chris Lattner1afcace2011-07-09 17:41:24 +0000695// checkGEPType - Simple wrapper function to give a better assertion failure
David Greeneb8f74792007-09-04 15:46:09 +0000696// message on bad indexes for a gep instruction.
697//
Chandler Carruth305b5152012-06-20 08:39:33 +0000698inline Type *checkGEPType(Type *Ty) {
David Greeneb8f74792007-09-04 15:46:09 +0000699 assert(Ty && "Invalid GetElementPtrInst indices for type!");
700 return Ty;
701}
702
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000703/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
704/// access elements of arrays and structs
705///
706class GetElementPtrInst : public Instruction {
Gabor Greifefe65362008-05-10 08:32:32 +0000707 GetElementPtrInst(const GetElementPtrInst &GEPI);
Jay Foada9203102011-07-25 09:48:08 +0000708 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000709
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000710 /// Constructors - Create a getelementptr instruction with a base pointer an
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000711 /// list of indices. The first ctor can optionally insert before an existing
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000712 /// instruction, the second appends the new instruction to the specified
713 /// BasicBlock.
Jay Foada9203102011-07-25 09:48:08 +0000714 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
715 unsigned Values, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000716 Instruction *InsertBefore);
Jay Foada9203102011-07-25 09:48:08 +0000717 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
718 unsigned Values, const Twine &NameStr,
719 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +0000720protected:
721 virtual GetElementPtrInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +0000722public:
Jay Foada9203102011-07-25 09:48:08 +0000723 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000724 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000725 Instruction *InsertBefore = 0) {
Jay Foada9203102011-07-25 09:48:08 +0000726 unsigned Values = 1 + unsigned(IdxList.size());
Gabor Greifefe65362008-05-10 08:32:32 +0000727 return new(Values)
Jay Foada9203102011-07-25 09:48:08 +0000728 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000729 }
Jay Foada9203102011-07-25 09:48:08 +0000730 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000731 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000732 BasicBlock *InsertAtEnd) {
Jay Foada9203102011-07-25 09:48:08 +0000733 unsigned Values = 1 + unsigned(IdxList.size());
Gabor Greifefe65362008-05-10 08:32:32 +0000734 return new(Values)
Jay Foada9203102011-07-25 09:48:08 +0000735 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000736 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000737
Dan Gohmane2574d32009-08-11 17:57:01 +0000738 /// Create an "inbounds" getelementptr. See the documentation for the
739 /// "inbounds" flag in LangRef.html for details.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000740 static GetElementPtrInst *CreateInBounds(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000741 ArrayRef<Value *> IdxList,
Dan Gohmane2574d32009-08-11 17:57:01 +0000742 const Twine &NameStr = "",
743 Instruction *InsertBefore = 0) {
Jay Foada9203102011-07-25 09:48:08 +0000744 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000745 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000746 return GEP;
747 }
Dan Gohmane2574d32009-08-11 17:57:01 +0000748 static GetElementPtrInst *CreateInBounds(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000749 ArrayRef<Value *> IdxList,
Dan Gohmane2574d32009-08-11 17:57:01 +0000750 const Twine &NameStr,
751 BasicBlock *InsertAtEnd) {
Jay Foada9203102011-07-25 09:48:08 +0000752 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000753 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000754 return GEP;
755 }
756
Gabor Greifefe65362008-05-10 08:32:32 +0000757 /// Transparently provide more efficient getOperand methods.
758 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
759
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000760 // getType - Overload to return most specific pointer type...
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000761 PointerType *getType() const {
762 return reinterpret_cast<PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000763 }
764
765 /// getIndexedType - Returns the type of the element that would be loaded with
766 /// a load instruction with the specified parameters.
767 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000768 /// Null is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000769 /// pointer type.
770 ///
Jay Foada9203102011-07-25 09:48:08 +0000771 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
772 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
773 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
Misha Brukman9769ab22005-04-21 20:19:05 +0000774
Micah Villmow63b8ab22012-10-09 22:27:29 +0000775 /// getAddressSpace - Returns the address space used by the GEP pointer.
Nadav Rotem16087692011-12-05 06:29:09 +0000776 ///
777 static unsigned getAddressSpace(Value *Ptr);
778
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000779 inline op_iterator idx_begin() { return op_begin()+1; }
780 inline const_op_iterator idx_begin() const { return op_begin()+1; }
781 inline op_iterator idx_end() { return op_end(); }
782 inline const_op_iterator idx_end() const { return op_end(); }
783
784 Value *getPointerOperand() {
785 return getOperand(0);
786 }
787 const Value *getPointerOperand() const {
788 return getOperand(0);
789 }
790 static unsigned getPointerOperandIndex() {
Nadav Rotem16087692011-12-05 06:29:09 +0000791 return 0U; // get index for modifying correct operand.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000792 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000793
Chris Lattner8a67ac52009-08-30 20:06:40 +0000794 unsigned getPointerAddressSpace() const {
Chandler Carruth8fb614c2012-11-01 09:37:49 +0000795 return getPointerOperand()->getType()->getPointerAddressSpace();
Chris Lattner8a67ac52009-08-30 20:06:40 +0000796 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000797
Chris Lattner3bc6ced2009-01-09 05:27:40 +0000798 /// getPointerOperandType - Method to return the pointer operand as a
799 /// PointerType.
Nadav Rotem16087692011-12-05 06:29:09 +0000800 Type *getPointerOperandType() const {
801 return getPointerOperand()->getType();
Chris Lattner3bc6ced2009-01-09 05:27:40 +0000802 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000803
Nadav Rotem16087692011-12-05 06:29:09 +0000804 /// GetGEPReturnType - Returns the pointer type returned by the GEP
805 /// instruction, which may be a vector of pointers.
806 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
807 Type *PtrTy = PointerType::get(checkGEPType(
808 getIndexedType(Ptr->getType(), IdxList)),
809 getAddressSpace(Ptr));
810 // Vector GEP
811 if (Ptr->getType()->isVectorTy()) {
812 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
813 return VectorType::get(PtrTy, NumElem);
814 }
815
816 // Scalar GEP
817 return PtrTy;
818 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000819
Devang Patel4d4a5e02008-02-23 01:11:02 +0000820 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000821 return getNumOperands() - 1;
822 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000823
Devang Patel4d4a5e02008-02-23 01:11:02 +0000824 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000825 return getNumOperands() > 1;
826 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000827
Chris Lattner6f771d42007-04-14 00:12:57 +0000828 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
829 /// zeros. If so, the result pointer and the first operand have the same
830 /// value, just potentially different types.
831 bool hasAllZeroIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000832
Chris Lattner6b0974c2007-04-27 20:35:56 +0000833 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
834 /// constant integers. If so, the result pointer and the first operand have
835 /// a constant offset between them.
836 bool hasAllConstantIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000837
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000838 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
839 /// See LangRef.html for the meaning of inbounds on a getelementptr.
Nick Lewyckyae05e7d2009-09-27 21:33:04 +0000840 void setIsInBounds(bool b = true);
841
842 /// isInBounds - Determine whether the GEP has the inbounds flag.
843 bool isInBounds() const;
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000844
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000845 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000846 static inline bool classof(const Instruction *I) {
847 return (I->getOpcode() == Instruction::GetElementPtr);
848 }
849 static inline bool classof(const Value *V) {
850 return isa<Instruction>(V) && classof(cast<Instruction>(V));
851 }
852};
853
Gabor Greifefe65362008-05-10 08:32:32 +0000854template <>
Jay Foad67c619b2011-01-11 15:07:38 +0000855struct OperandTraits<GetElementPtrInst> :
856 public VariadicOperandTraits<GetElementPtrInst, 1> {
Gabor Greifefe65362008-05-10 08:32:32 +0000857};
858
Gabor Greifefe65362008-05-10 08:32:32 +0000859GetElementPtrInst::GetElementPtrInst(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000860 ArrayRef<Value *> IdxList,
Gabor Greifefe65362008-05-10 08:32:32 +0000861 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000862 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000863 Instruction *InsertBefore)
Nadav Rotem16087692011-12-05 06:29:09 +0000864 : Instruction(getGEPReturnType(Ptr, IdxList),
Gabor Greifefe65362008-05-10 08:32:32 +0000865 GetElementPtr,
866 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
867 Values, InsertBefore) {
Jay Foada9203102011-07-25 09:48:08 +0000868 init(Ptr, IdxList, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +0000869}
Gabor Greifefe65362008-05-10 08:32:32 +0000870GetElementPtrInst::GetElementPtrInst(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000871 ArrayRef<Value *> IdxList,
Gabor Greifefe65362008-05-10 08:32:32 +0000872 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000873 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000874 BasicBlock *InsertAtEnd)
Nadav Rotem16087692011-12-05 06:29:09 +0000875 : Instruction(getGEPReturnType(Ptr, IdxList),
Gabor Greifefe65362008-05-10 08:32:32 +0000876 GetElementPtr,
877 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
878 Values, InsertAtEnd) {
Jay Foada9203102011-07-25 09:48:08 +0000879 init(Ptr, IdxList, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +0000880}
881
882
883DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
884
885
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000886//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000887// ICmpInst Class
888//===----------------------------------------------------------------------===//
889
890/// This instruction compares its operands according to the predicate given
Nate Begemanac80ade2008-05-12 19:01:56 +0000891/// to the constructor. It only operates on integers or pointers. The operands
892/// must be identical types.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000893/// @brief Represent an integer comparison operator.
894class ICmpInst: public CmpInst {
Devang Patel50b6e332009-10-27 22:16:29 +0000895protected:
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000896 /// @brief Clone an identical ICmpInst
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000897 virtual ICmpInst *clone_impl() const;
Reid Spencer45fb3f32006-11-20 01:22:35 +0000898public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000899 /// @brief Constructor with insert-before-instruction semantics.
900 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000901 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +0000902 Predicate pred, ///< The predicate to use for the comparison
903 Value *LHS, ///< The left-hand-side of the expression
904 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000905 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000906 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +0000907 Instruction::ICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000908 InsertBefore) {
909 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
910 pred <= CmpInst::LAST_ICMP_PREDICATE &&
911 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000912 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000913 "Both operands to ICmp instruction are not of the same type!");
914 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000915 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Nadav Rotem16087692011-12-05 06:29:09 +0000916 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000917 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000918 }
919
Owen Anderson333c4002009-07-09 23:48:35 +0000920 /// @brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000921 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000922 BasicBlock &InsertAtEnd, ///< Block to insert into.
923 Predicate pred, ///< The predicate to use for the comparison
924 Value *LHS, ///< The left-hand-side of the expression
925 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000926 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000927 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000928 Instruction::ICmp, pred, LHS, RHS, NameStr,
929 &InsertAtEnd) {
930 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
931 pred <= CmpInst::LAST_ICMP_PREDICATE &&
932 "Invalid ICmp predicate value");
933 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
934 "Both operands to ICmp instruction are not of the same type!");
935 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000936 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000937 getOperand(0)->getType()->isPointerTy()) &&
Owen Anderson333c4002009-07-09 23:48:35 +0000938 "Invalid operand types for ICmp instruction");
939 }
940
941 /// @brief Constructor with no-insertion semantics
942 ICmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +0000943 Predicate pred, ///< The predicate to use for the comparison
944 Value *LHS, ///< The left-hand-side of the expression
945 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000946 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000947 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000948 Instruction::ICmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +0000949 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
950 pred <= CmpInst::LAST_ICMP_PREDICATE &&
951 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000952 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000953 "Both operands to ICmp instruction are not of the same type!");
954 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000955 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Nadav Rotem16087692011-12-05 06:29:09 +0000956 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000957 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000958 }
959
Reid Spencere4d87aa2006-12-23 06:05:41 +0000960 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
961 /// @returns the predicate that would be the result if the operand were
962 /// regarded as signed.
963 /// @brief Return the signed version of the predicate
964 Predicate getSignedPredicate() const {
965 return getSignedPredicate(getPredicate());
966 }
967
968 /// This is a static version that you can use without an instruction.
969 /// @brief Return the signed version of the predicate.
970 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000971
Nick Lewycky4189a532008-01-28 03:48:02 +0000972 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
973 /// @returns the predicate that would be the result if the operand were
974 /// regarded as unsigned.
975 /// @brief Return the unsigned version of the predicate
976 Predicate getUnsignedPredicate() const {
977 return getUnsignedPredicate(getPredicate());
978 }
979
980 /// This is a static version that you can use without an instruction.
981 /// @brief Return the unsigned version of the predicate.
982 static Predicate getUnsignedPredicate(Predicate pred);
983
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000984 /// isEquality - Return true if this predicate is either EQ or NE. This also
985 /// tests for commutativity.
986 static bool isEquality(Predicate P) {
987 return P == ICMP_EQ || P == ICMP_NE;
988 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000989
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000990 /// isEquality - Return true if this predicate is either EQ or NE. This also
991 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000992 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000993 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000994 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000995
996 /// @returns true if the predicate of this ICmpInst is commutative
997 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000998 bool isCommutative() const { return isEquality(); }
999
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001000 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +00001001 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +00001002 bool isRelational() const {
1003 return !isEquality();
1004 }
1005
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001006 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +00001007 ///
1008 static bool isRelational(Predicate P) {
1009 return !isEquality(P);
1010 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001011
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001012 /// Initialize a set of values that all satisfy the predicate with C.
Reid Spencer3da43842007-02-28 22:00:54 +00001013 /// @brief Make a ConstantRange for a relation with a constant value.
1014 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1015
Reid Spencer45fb3f32006-11-20 01:22:35 +00001016 /// Exchange the two operands to this instruction in such a way that it does
1017 /// not modify the semantics of the instruction. The predicate value may be
1018 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001019 /// (e.g. ult).
Reid Spencer45fb3f32006-11-20 01:22:35 +00001020 /// @brief Swap operands and adjust predicate.
1021 void swapOperands() {
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001022 setPredicate(getSwappedPredicate());
Gabor Greif94fb68b2008-05-13 22:51:52 +00001023 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +00001024 }
1025
1026 // Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer45fb3f32006-11-20 01:22:35 +00001027 static inline bool classof(const Instruction *I) {
1028 return I->getOpcode() == Instruction::ICmp;
1029 }
1030 static inline bool classof(const Value *V) {
1031 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1032 }
Dan Gohmanf72fb672008-09-09 01:02:47 +00001033
Reid Spencer45fb3f32006-11-20 01:22:35 +00001034};
1035
1036//===----------------------------------------------------------------------===//
1037// FCmpInst Class
1038//===----------------------------------------------------------------------===//
1039
1040/// This instruction compares its operands according to the predicate given
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001041/// to the constructor. It only operates on floating point values or packed
Reid Spencer45fb3f32006-11-20 01:22:35 +00001042/// vectors of floating point values. The operands must be identical types.
1043/// @brief Represents a floating point comparison operator.
1044class FCmpInst: public CmpInst {
Devang Patel50b6e332009-10-27 22:16:29 +00001045protected:
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001046 /// @brief Clone an identical FCmpInst
Devang Patel50b6e332009-10-27 22:16:29 +00001047 virtual FCmpInst *clone_impl() const;
Reid Spencer45fb3f32006-11-20 01:22:35 +00001048public:
Reid Spencer45fb3f32006-11-20 01:22:35 +00001049 /// @brief Constructor with insert-before-instruction semantics.
1050 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +00001051 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +00001052 Predicate pred, ///< The predicate to use for the comparison
1053 Value *LHS, ///< The left-hand-side of the expression
1054 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001055 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +00001056 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +00001057 Instruction::FCmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +00001058 InsertBefore) {
1059 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1060 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +00001061 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001062 "Both operands to FCmp instruction are not of the same type!");
1063 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001064 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001065 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +00001066 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001067
Owen Anderson333c4002009-07-09 23:48:35 +00001068 /// @brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +00001069 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +00001070 BasicBlock &InsertAtEnd, ///< Block to insert into.
1071 Predicate pred, ///< The predicate to use for the comparison
1072 Value *LHS, ///< The left-hand-side of the expression
1073 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001074 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +00001075 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +00001076 Instruction::FCmp, pred, LHS, RHS, NameStr,
1077 &InsertAtEnd) {
1078 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1079 "Invalid FCmp predicate value");
1080 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1081 "Both operands to FCmp instruction are not of the same type!");
1082 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001083 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Owen Anderson333c4002009-07-09 23:48:35 +00001084 "Invalid operand types for FCmp instruction");
1085 }
1086
1087 /// @brief Constructor with no-insertion semantics
1088 FCmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +00001089 Predicate pred, ///< The predicate to use for the comparison
1090 Value *LHS, ///< The left-hand-side of the expression
1091 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001092 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +00001093 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +00001094 Instruction::FCmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +00001095 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1096 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +00001097 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001098 "Both operands to FCmp instruction are not of the same type!");
1099 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001100 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001101 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +00001102 }
1103
Reid Spencer45fb3f32006-11-20 01:22:35 +00001104 /// @returns true if the predicate of this instruction is EQ or NE.
1105 /// @brief Determine if this is an equality predicate.
1106 bool isEquality() const {
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001107 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1108 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
Reid Spencer45fb3f32006-11-20 01:22:35 +00001109 }
Dan Gohman793df072008-09-16 16:44:00 +00001110
1111 /// @returns true if the predicate of this instruction is commutative.
1112 /// @brief Determine if this is a commutative predicate.
1113 bool isCommutative() const {
1114 return isEquality() ||
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001115 getPredicate() == FCMP_FALSE ||
1116 getPredicate() == FCMP_TRUE ||
1117 getPredicate() == FCMP_ORD ||
1118 getPredicate() == FCMP_UNO;
Dan Gohman793df072008-09-16 16:44:00 +00001119 }
Reid Spencer45fb3f32006-11-20 01:22:35 +00001120
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001121 /// @returns true if the predicate is relational (not EQ or NE).
Reid Spencer45fb3f32006-11-20 01:22:35 +00001122 /// @brief Determine if this a relational predicate.
1123 bool isRelational() const { return !isEquality(); }
1124
1125 /// Exchange the two operands to this instruction in such a way that it does
1126 /// not modify the semantics of the instruction. The predicate value may be
1127 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001128 /// (e.g. ult).
Reid Spencer45fb3f32006-11-20 01:22:35 +00001129 /// @brief Swap operands and adjust predicate.
1130 void swapOperands() {
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001131 setPredicate(getSwappedPredicate());
Gabor Greif94fb68b2008-05-13 22:51:52 +00001132 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +00001133 }
1134
1135 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer45fb3f32006-11-20 01:22:35 +00001136 static inline bool classof(const Instruction *I) {
1137 return I->getOpcode() == Instruction::FCmp;
1138 }
1139 static inline bool classof(const Value *V) {
1140 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1141 }
1142};
1143
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001144//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001145/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +00001146/// machine's calling convention. This class uses low bit of the SubClassData
1147/// field to indicate whether or not this is a tail call. The rest of the bits
1148/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001149///
1150class CallInst : public Instruction {
Devang Patel05988662008-09-25 21:00:45 +00001151 AttrListPtr AttributeList; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001152 CallInst(const CallInst &CI);
Jay Foada3efbb12011-07-15 08:37:34 +00001153 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1154 void init(Value *Func, const Twine &NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001155
Jay Foada3efbb12011-07-15 08:37:34 +00001156 /// Construct a CallInst given a range of arguments.
David Greene52eec542007-08-01 03:43:44 +00001157 /// @brief Construct a CallInst from a range of arguments
Jay Foada3efbb12011-07-15 08:37:34 +00001158 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1159 const Twine &NameStr, Instruction *InsertBefore);
David Greene52eec542007-08-01 03:43:44 +00001160
Jay Foada3efbb12011-07-15 08:37:34 +00001161 /// Construct a CallInst given a range of arguments.
David Greene52eec542007-08-01 03:43:44 +00001162 /// @brief Construct a CallInst from a range of arguments
Jay Foada3efbb12011-07-15 08:37:34 +00001163 inline CallInst(Value *Func, ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001164 const Twine &NameStr, BasicBlock *InsertAtEnd);
David Greene52eec542007-08-01 03:43:44 +00001165
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001166 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001167 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001168 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001169 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001170 explicit CallInst(Value *F, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001171 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001172 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001173protected:
1174 virtual CallInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001175public:
Gabor Greifefe65362008-05-10 08:32:32 +00001176 static CallInst *Create(Value *Func,
Jay Foada3efbb12011-07-15 08:37:34 +00001177 ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001178 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001179 Instruction *InsertBefore = 0) {
Jay Foada3efbb12011-07-15 08:37:34 +00001180 return new(unsigned(Args.size() + 1))
1181 CallInst(Func, Args, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001182 }
Gabor Greifefe65362008-05-10 08:32:32 +00001183 static CallInst *Create(Value *Func,
Jay Foada3efbb12011-07-15 08:37:34 +00001184 ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001185 const Twine &NameStr, BasicBlock *InsertAtEnd) {
Jay Foada3efbb12011-07-15 08:37:34 +00001186 return new(unsigned(Args.size() + 1))
1187 CallInst(Func, Args, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001188 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001189 static CallInst *Create(Value *F, const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001190 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001191 return new(1) CallInst(F, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001192 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001193 static CallInst *Create(Value *F, const Twine &NameStr,
Evan Cheng34cd4a42008-05-05 18:30:58 +00001194 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001195 return new(1) CallInst(F, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001196 }
Evan Chengfabcb912009-09-10 04:36:43 +00001197 /// CreateMalloc - Generate the IR for a call to malloc:
1198 /// 1. Compute the malloc call's argument as the specified type's size,
1199 /// possibly multiplied by the array size if the array size is not
1200 /// constant 1.
1201 /// 2. Call malloc with that argument.
1202 /// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewycky3fc35c52009-10-17 23:52:26 +00001203 static Instruction *CreateMalloc(Instruction *InsertBefore,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001204 Type *IntPtrTy, Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +00001205 Value *AllocSize, Value *ArraySize = 0,
Chris Lattner5a30a852010-07-12 00:57:28 +00001206 Function* MallocF = 0,
Nick Lewycky3fc35c52009-10-17 23:52:26 +00001207 const Twine &Name = "");
1208 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001209 Type *IntPtrTy, Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +00001210 Value *AllocSize, Value *ArraySize = 0,
1211 Function* MallocF = 0,
Nick Lewycky3fc35c52009-10-17 23:52:26 +00001212 const Twine &Name = "");
Victor Hernandez66284e02009-10-24 04:23:03 +00001213 /// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner5a30a852010-07-12 00:57:28 +00001214 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
Victor Hernandez66284e02009-10-24 04:23:03 +00001215 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001216
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001217 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001218
Chris Lattnerb2406d92009-12-29 02:46:09 +00001219 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001220 void setTailCall(bool isTC = true) {
Chris Lattnerb2406d92009-12-29 02:46:09 +00001221 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
1222 unsigned(isTC));
Chris Lattner3340ffe2005-05-06 20:26:26 +00001223 }
1224
Dan Gohmanf2752502008-09-26 21:38:45 +00001225 /// Provide fast operand accessors
1226 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001227
Gabor Greif0114b992010-07-31 08:35:21 +00001228 /// getNumArgOperands - Return the number of call arguments.
1229 ///
Bill Wendling22a5b292010-06-07 19:05:06 +00001230 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
Gabor Greif0114b992010-07-31 08:35:21 +00001231
1232 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1233 ///
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001234 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1235 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
Bill Wendling22a5b292010-06-07 19:05:06 +00001236
Chris Lattner3340ffe2005-05-06 20:26:26 +00001237 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1238 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001239 CallingConv::ID getCallingConv() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +00001240 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001241 }
1242 void setCallingConv(CallingConv::ID CC) {
Chris Lattnerb2406d92009-12-29 02:46:09 +00001243 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1244 (static_cast<unsigned>(CC) << 1));
Chris Lattner3340ffe2005-05-06 20:26:26 +00001245 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00001246
Devang Patel05988662008-09-25 21:00:45 +00001247 /// getAttributes - Return the parameter attributes for this call.
Chris Lattner041221c2008-03-13 04:33:03 +00001248 ///
Devang Patel05988662008-09-25 21:00:45 +00001249 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001250
Dan Gohmanf2752502008-09-26 21:38:45 +00001251 /// setAttributes - Set the parameter attributes for this call.
1252 ///
Devang Patel05988662008-09-25 21:00:45 +00001253 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001254
Devang Patel05988662008-09-25 21:00:45 +00001255 /// addAttribute - adds the attribute to the list of attributes.
1256 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsdc024672007-11-27 13:23:08 +00001257
Devang Patel05988662008-09-25 21:00:45 +00001258 /// removeAttribute - removes the attribute from the list of attributes.
1259 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00001260
Bill Wendling060f20a2012-10-09 00:28:54 +00001261 /// @brief Determine whether this call has the given attribute.
Bill Wendling2fa8af22012-10-09 21:49:51 +00001262 bool hasFnAttr(Attributes::AttrVal A) const;
Bill Wendling060f20a2012-10-09 00:28:54 +00001263
Bill Wendling847d1652012-10-03 17:54:26 +00001264 /// @brief Determine whether the call or the callee has the given attributes.
Bill Wendling3e2d76c2012-10-09 21:38:14 +00001265 bool paramHasAttr(unsigned i, Attributes::AttrVal A) const;
Bill Wendling847d1652012-10-03 17:54:26 +00001266
Dale Johannesen08e78b12008-02-22 17:49:45 +00001267 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001268 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00001269 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001270 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001271
Eric Christopherf27e6082010-03-25 04:49:10 +00001272 /// @brief Return true if the call should not be inlined.
Bill Wendling2fa8af22012-10-09 21:49:51 +00001273 bool isNoInline() const { return hasFnAttr(Attributes::NoInline); }
Bill Wendling1b005072012-10-09 23:40:31 +00001274 void setIsNoInline() {
Bill Wendling46d5dd92012-10-16 05:23:31 +00001275 addAttribute(AttrListPtr::FunctionIndex,
1276 Attributes::get(getContext(), Attributes::NoInline));
Eric Christopherf27e6082010-03-25 04:49:10 +00001277 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001278
Rafael Espindola11f1a832011-10-05 20:05:13 +00001279 /// @brief Return true if the call can return twice
1280 bool canReturnTwice() const {
Bill Wendling2fa8af22012-10-09 21:49:51 +00001281 return hasFnAttr(Attributes::ReturnsTwice);
Rafael Espindola11f1a832011-10-05 20:05:13 +00001282 }
Bill Wendling1b005072012-10-09 23:40:31 +00001283 void setCanReturnTwice() {
Bill Wendling46d5dd92012-10-16 05:23:31 +00001284 addAttribute(AttrListPtr::FunctionIndex,
1285 Attributes::get(getContext(), Attributes::ReturnsTwice));
Rafael Espindola11f1a832011-10-05 20:05:13 +00001286 }
1287
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001288 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001289 bool doesNotAccessMemory() const {
Bill Wendling2fa8af22012-10-09 21:49:51 +00001290 return hasFnAttr(Attributes::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001291 }
Bill Wendling1b005072012-10-09 23:40:31 +00001292 void setDoesNotAccessMemory() {
Bill Wendling46d5dd92012-10-16 05:23:31 +00001293 addAttribute(AttrListPtr::FunctionIndex,
1294 Attributes::get(getContext(), Attributes::ReadNone));
Duncan Sands2e033f32008-07-08 08:38:44 +00001295 }
1296
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001297 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001298 bool onlyReadsMemory() const {
Bill Wendling2fa8af22012-10-09 21:49:51 +00001299 return doesNotAccessMemory() || hasFnAttr(Attributes::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001300 }
Bill Wendling1b005072012-10-09 23:40:31 +00001301 void setOnlyReadsMemory() {
Bill Wendling46d5dd92012-10-16 05:23:31 +00001302 addAttribute(AttrListPtr::FunctionIndex,
1303 Attributes::get(getContext(), Attributes::ReadOnly));
Duncan Sands2e033f32008-07-08 08:38:44 +00001304 }
1305
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001306 /// @brief Determine if the call cannot return.
Bill Wendling2fa8af22012-10-09 21:49:51 +00001307 bool doesNotReturn() const { return hasFnAttr(Attributes::NoReturn); }
Bill Wendling1b005072012-10-09 23:40:31 +00001308 void setDoesNotReturn() {
Bill Wendling46d5dd92012-10-16 05:23:31 +00001309 addAttribute(AttrListPtr::FunctionIndex,
1310 Attributes::get(getContext(), Attributes::NoReturn));
Duncan Sands2e033f32008-07-08 08:38:44 +00001311 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001312
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001313 /// @brief Determine if the call cannot unwind.
Bill Wendling2fa8af22012-10-09 21:49:51 +00001314 bool doesNotThrow() const { return hasFnAttr(Attributes::NoUnwind); }
Bill Wendling658a8062012-10-10 18:02:57 +00001315 void setDoesNotThrow() {
Bill Wendling46d5dd92012-10-16 05:23:31 +00001316 addAttribute(AttrListPtr::FunctionIndex,
1317 Attributes::get(getContext(), Attributes::NoUnwind));
Duncan Sands2e033f32008-07-08 08:38:44 +00001318 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001319
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001320 /// @brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00001321 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001322 bool hasStructRetAttr() const {
1323 // Be friendly and also check the callee.
Bill Wendling3e2d76c2012-10-09 21:38:14 +00001324 return paramHasAttr(1, Attributes::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001325 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001326
Evan Chengf4a54982008-01-12 18:57:32 +00001327 /// @brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001328 bool hasByValArgument() const {
Bill Wendling2fd77652012-10-09 01:03:48 +00001329 for (unsigned I = 0, E = AttributeList.getNumAttrs(); I != E; ++I)
Bill Wendling67658342012-10-09 07:45:08 +00001330 if (AttributeList.getAttributesAtIndex(I).hasAttribute(Attributes::ByVal))
Bill Wendling2fd77652012-10-09 01:03:48 +00001331 return true;
1332 return false;
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001333 }
Evan Chengf4a54982008-01-12 18:57:32 +00001334
Dan Gohmanf2752502008-09-26 21:38:45 +00001335 /// getCalledFunction - Return the function called, or null if this is an
1336 /// indirect function invocation.
1337 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001338 Function *getCalledFunction() const {
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001339 return dyn_cast<Function>(Op<-1>());
Chris Lattner721aef62004-11-18 17:46:57 +00001340 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001341
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001342 /// getCalledValue - Get a pointer to the function that is invoked by this
Chris Lattner14d80382009-10-18 05:08:07 +00001343 /// instruction.
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001344 const Value *getCalledValue() const { return Op<-1>(); }
1345 Value *getCalledValue() { return Op<-1>(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001346
Chris Lattner14d80382009-10-18 05:08:07 +00001347 /// setCalledFunction - Set the function called.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001348 void setCalledFunction(Value* Fn) {
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001349 Op<-1>() = Fn;
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001350 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001351
Owen Anderson6f615f82010-08-07 00:19:59 +00001352 /// isInlineAsm - Check if this call is an inline asm statement.
1353 bool isInlineAsm() const {
1354 return isa<InlineAsm>(Op<-1>());
1355 }
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001356
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001357 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001358 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001359 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001360 }
1361 static inline bool classof(const Value *V) {
1362 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1363 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001364private:
Chris Lattnerb2406d92009-12-29 02:46:09 +00001365 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1366 // method so that subclasses cannot accidentally use it.
1367 void setInstructionSubclassData(unsigned short D) {
1368 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001369 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001370};
1371
Gabor Greifefe65362008-05-10 08:32:32 +00001372template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001373struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
Gabor Greifefe65362008-05-10 08:32:32 +00001374};
1375
Jay Foada3efbb12011-07-15 08:37:34 +00001376CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001377 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001378 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1379 ->getElementType())->getReturnType(),
1380 Instruction::Call,
Jay Foada3efbb12011-07-15 08:37:34 +00001381 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1382 unsigned(Args.size() + 1), InsertAtEnd) {
1383 init(Func, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00001384}
1385
Jay Foada3efbb12011-07-15 08:37:34 +00001386CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001387 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00001388 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1389 ->getElementType())->getReturnType(),
1390 Instruction::Call,
Jay Foada3efbb12011-07-15 08:37:34 +00001391 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1392 unsigned(Args.size() + 1), InsertBefore) {
1393 init(Func, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00001394}
1395
Gabor Greife9e12152010-07-06 15:44:11 +00001396
1397// Note: if you get compile errors about private methods then
1398// please update your code to use the high-level operand
1399// interfaces. See line 943 above.
Gabor Greifefe65362008-05-10 08:32:32 +00001400DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1401
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001402//===----------------------------------------------------------------------===//
1403// SelectInst Class
1404//===----------------------------------------------------------------------===//
1405
1406/// SelectInst - This class represents the LLVM 'select' instruction.
1407///
1408class SelectInst : public Instruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001409 void init(Value *C, Value *S1, Value *S2) {
Chris Lattnerb76ec322008-12-29 00:12:50 +00001410 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
Gabor Greifefe65362008-05-10 08:32:32 +00001411 Op<0>() = C;
1412 Op<1>() = S1;
1413 Op<2>() = S2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001414 }
1415
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001416 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001417 Instruction *InsertBefore)
1418 : Instruction(S1->getType(), Instruction::Select,
1419 &Op<0>(), 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001420 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001421 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001422 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001423 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001424 BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001425 : Instruction(S1->getType(), Instruction::Select,
1426 &Op<0>(), 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001427 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001428 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001429 }
Devang Patel50b6e332009-10-27 22:16:29 +00001430protected:
1431 virtual SelectInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001432public:
Evan Chengd69bb1a2008-05-05 17:41:03 +00001433 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001434 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001435 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001436 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001437 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001438 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001439 const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001440 BasicBlock *InsertAtEnd) {
1441 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001442 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001443
Chris Lattner97159122009-09-08 03:32:53 +00001444 const Value *getCondition() const { return Op<0>(); }
1445 const Value *getTrueValue() const { return Op<1>(); }
1446 const Value *getFalseValue() const { return Op<2>(); }
1447 Value *getCondition() { return Op<0>(); }
1448 Value *getTrueValue() { return Op<1>(); }
1449 Value *getFalseValue() { return Op<2>(); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001450
Chris Lattnerb76ec322008-12-29 00:12:50 +00001451 /// areInvalidOperands - Return a string if the specified operands are invalid
1452 /// for a select operation, otherwise return null.
1453 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
Chris Lattner454928e2005-01-29 00:31:36 +00001454
1455 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001456 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001457
1458 OtherOps getOpcode() const {
1459 return static_cast<OtherOps>(Instruction::getOpcode());
1460 }
1461
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001462 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001463 static inline bool classof(const Instruction *I) {
1464 return I->getOpcode() == Instruction::Select;
1465 }
1466 static inline bool classof(const Value *V) {
1467 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1468 }
1469};
1470
Gabor Greifefe65362008-05-10 08:32:32 +00001471template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001472struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001473};
1474
1475DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1476
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001477//===----------------------------------------------------------------------===//
1478// VAArgInst Class
1479//===----------------------------------------------------------------------===//
1480
1481/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001482/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001483///
Chris Lattner454928e2005-01-29 00:31:36 +00001484class VAArgInst : public UnaryInstruction {
Devang Patel50b6e332009-10-27 22:16:29 +00001485protected:
1486 virtual VAArgInst *clone_impl() const;
1487
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001488public:
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001489 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001490 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001491 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001492 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001493 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001494 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001495 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001496 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001497 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001498 }
1499
Dan Gohmane7445412010-09-09 18:32:40 +00001500 Value *getPointerOperand() { return getOperand(0); }
1501 const Value *getPointerOperand() const { return getOperand(0); }
1502 static unsigned getPointerOperandIndex() { return 0U; }
1503
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001504 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001505 static inline bool classof(const Instruction *I) {
1506 return I->getOpcode() == VAArg;
1507 }
1508 static inline bool classof(const Value *V) {
1509 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1510 }
1511};
1512
1513//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001514// ExtractElementInst Class
1515//===----------------------------------------------------------------------===//
1516
1517/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001518/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001519///
1520class ExtractElementInst : public Instruction {
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001521 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
Chris Lattner9fc18d22006-04-08 01:15:18 +00001522 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001523 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
Chris Lattner9fc18d22006-04-08 01:15:18 +00001524 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001525protected:
1526 virtual ExtractElementInst *clone_impl() const;
1527
Eric Christophera3500da2009-07-25 02:28:41 +00001528public:
Eric Christophera3500da2009-07-25 02:28:41 +00001529 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001530 const Twine &NameStr = "",
Eric Christophera3500da2009-07-25 02:28:41 +00001531 Instruction *InsertBefore = 0) {
1532 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1533 }
1534 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001535 const Twine &NameStr,
Eric Christophera3500da2009-07-25 02:28:41 +00001536 BasicBlock *InsertAtEnd) {
1537 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1538 }
Robert Bocchino49b78a52006-01-10 19:04:13 +00001539
Chris Lattnerfa495842006-04-08 04:04:54 +00001540 /// isValidOperands - Return true if an extractelement instruction can be
1541 /// formed with the specified operands.
1542 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001543
Chris Lattner97159122009-09-08 03:32:53 +00001544 Value *getVectorOperand() { return Op<0>(); }
1545 Value *getIndexOperand() { return Op<1>(); }
1546 const Value *getVectorOperand() const { return Op<0>(); }
1547 const Value *getIndexOperand() const { return Op<1>(); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001548
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001549 VectorType *getVectorOperandType() const {
1550 return reinterpret_cast<VectorType*>(getVectorOperand()->getType());
Chris Lattner97159122009-09-08 03:32:53 +00001551 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001552
1553
Robert Bocchino49b78a52006-01-10 19:04:13 +00001554 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001555 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001556
1557 // Methods for support type inquiry through isa, cast, and dyn_cast:
Robert Bocchino49b78a52006-01-10 19:04:13 +00001558 static inline bool classof(const Instruction *I) {
1559 return I->getOpcode() == Instruction::ExtractElement;
1560 }
1561 static inline bool classof(const Value *V) {
1562 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1563 }
1564};
1565
Gabor Greifefe65362008-05-10 08:32:32 +00001566template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001567struct OperandTraits<ExtractElementInst> :
1568 public FixedNumOperandTraits<ExtractElementInst, 2> {
Gabor Greifefe65362008-05-10 08:32:32 +00001569};
1570
1571DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1572
Robert Bocchino49b78a52006-01-10 19:04:13 +00001573//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001574// InsertElementInst Class
1575//===----------------------------------------------------------------------===//
1576
1577/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001578/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001579///
1580class InsertElementInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001581 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001582 const Twine &NameStr = "",
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001583 Instruction *InsertBefore = 0);
Gabor Greif051a9502008-04-06 20:25:17 +00001584 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001585 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001586protected:
1587 virtual InsertElementInst *clone_impl() const;
1588
Robert Bocchinof9993442006-01-17 20:05:59 +00001589public:
Gabor Greif051a9502008-04-06 20:25:17 +00001590 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001591 const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +00001592 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001593 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001594 }
Gabor Greif051a9502008-04-06 20:25:17 +00001595 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001596 const Twine &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001597 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001598 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001599 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001600
Chris Lattnerfa495842006-04-08 04:04:54 +00001601 /// isValidOperands - Return true if an insertelement instruction can be
1602 /// formed with the specified operands.
1603 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1604 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001605
Reid Spencerac9dcb92007-02-15 03:39:18 +00001606 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001607 ///
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001608 VectorType *getType() const {
1609 return reinterpret_cast<VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001610 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001611
Robert Bocchinof9993442006-01-17 20:05:59 +00001612 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001613 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchinof9993442006-01-17 20:05:59 +00001614
1615 // Methods for support type inquiry through isa, cast, and dyn_cast:
Robert Bocchinof9993442006-01-17 20:05:59 +00001616 static inline bool classof(const Instruction *I) {
1617 return I->getOpcode() == Instruction::InsertElement;
1618 }
1619 static inline bool classof(const Value *V) {
1620 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1621 }
1622};
1623
Gabor Greifefe65362008-05-10 08:32:32 +00001624template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001625struct OperandTraits<InsertElementInst> :
1626 public FixedNumOperandTraits<InsertElementInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001627};
1628
1629DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1630
Robert Bocchinof9993442006-01-17 20:05:59 +00001631//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001632// ShuffleVectorInst Class
1633//===----------------------------------------------------------------------===//
1634
1635/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1636/// input vectors.
1637///
1638class ShuffleVectorInst : public Instruction {
Devang Patel50b6e332009-10-27 22:16:29 +00001639protected:
1640 virtual ShuffleVectorInst *clone_impl() const;
1641
Chris Lattner9fc18d22006-04-08 01:15:18 +00001642public:
Gabor Greif051a9502008-04-06 20:25:17 +00001643 // allocate space for exactly three operands
1644 void *operator new(size_t s) {
1645 return User::operator new(s, 3);
1646 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001647 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001648 const Twine &NameStr = "",
Evan Cheng1bf9a182008-07-24 00:08:56 +00001649 Instruction *InsertBefor = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001650 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001651 const Twine &NameStr, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001652
Chris Lattnerfa495842006-04-08 04:04:54 +00001653 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001654 /// formed with the specified operands.
1655 static bool isValidOperands(const Value *V1, const Value *V2,
1656 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001657
Reid Spencerac9dcb92007-02-15 03:39:18 +00001658 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001659 ///
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001660 VectorType *getType() const {
1661 return reinterpret_cast<VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001662 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001663
Chris Lattner9fc18d22006-04-08 01:15:18 +00001664 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001665 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001666
Chris Lattner83694a92012-01-25 23:49:49 +00001667 Constant *getMask() const {
1668 return reinterpret_cast<Constant*>(getOperand(2));
1669 }
1670
Chris Lattner8728f192008-03-02 05:28:33 +00001671 /// getMaskValue - Return the index from the shuffle mask for the specified
1672 /// output result. This is either -1 if the element is undef or a number less
1673 /// than 2*numelements.
Chris Lattner56243b82012-01-26 02:51:13 +00001674 static int getMaskValue(Constant *Mask, unsigned i);
1675
1676 int getMaskValue(unsigned i) const {
1677 return getMaskValue(getMask(), i);
1678 }
Chris Lattner83694a92012-01-25 23:49:49 +00001679
1680 /// getShuffleMask - Return the full mask for this instruction, where each
1681 /// element is the element number and undef's are returned as -1.
Chris Lattner56243b82012-01-26 02:51:13 +00001682 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1683
1684 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1685 return getShuffleMask(getMask(), Result);
1686 }
Chris Lattner83694a92012-01-25 23:49:49 +00001687
1688 SmallVector<int, 16> getShuffleMask() const {
1689 SmallVector<int, 16> Mask;
1690 getShuffleMask(Mask);
1691 return Mask;
1692 }
1693
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001694
Chris Lattner9fc18d22006-04-08 01:15:18 +00001695 // Methods for support type inquiry through isa, cast, and dyn_cast:
Chris Lattner9fc18d22006-04-08 01:15:18 +00001696 static inline bool classof(const Instruction *I) {
1697 return I->getOpcode() == Instruction::ShuffleVector;
1698 }
1699 static inline bool classof(const Value *V) {
1700 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1701 }
1702};
1703
Gabor Greifefe65362008-05-10 08:32:32 +00001704template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001705struct OperandTraits<ShuffleVectorInst> :
1706 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001707};
1708
1709DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
Chris Lattner9fc18d22006-04-08 01:15:18 +00001710
1711//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001712// ExtractValueInst Class
1713//===----------------------------------------------------------------------===//
1714
Dan Gohmane2d896f2008-05-15 23:35:32 +00001715/// ExtractValueInst - This instruction extracts a struct member or array
1716/// element value from an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001717///
Gabor Greifd4f268b2008-06-06 20:28:12 +00001718class ExtractValueInst : public UnaryInstruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001719 SmallVector<unsigned, 4> Indices;
1720
Dan Gohman041e2eb2008-05-15 19:50:34 +00001721 ExtractValueInst(const ExtractValueInst &EVI);
Jay Foadfc6d3a42011-07-13 10:26:04 +00001722 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001723
Dan Gohmane2d896f2008-05-15 23:35:32 +00001724 /// Constructors - Create a extractvalue instruction with a base aggregate
1725 /// value and a list of indices. The first ctor can optionally insert before
1726 /// an existing instruction, the second appends the new instruction to the
1727 /// specified BasicBlock.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001728 inline ExtractValueInst(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001729 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001730 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001731 Instruction *InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001732 inline ExtractValueInst(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001733 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001734 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001735
Dan Gohman8e640412008-05-31 19:09:47 +00001736 // allocate space for exactly one operand
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001737 void *operator new(size_t s) {
1738 return User::operator new(s, 1);
1739 }
Devang Patel50b6e332009-10-27 22:16:29 +00001740protected:
1741 virtual ExtractValueInst *clone_impl() const;
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001742
Gabor Greifd4f268b2008-06-06 20:28:12 +00001743public:
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001744 static ExtractValueInst *Create(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001745 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001746 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001747 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001748 return new
Jay Foadfc6d3a42011-07-13 10:26:04 +00001749 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001750 }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001751 static ExtractValueInst *Create(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001752 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001753 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001754 BasicBlock *InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001755 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001756 }
1757
Dan Gohman041e2eb2008-05-15 19:50:34 +00001758 /// getIndexedType - Returns the type of the element that would be extracted
1759 /// with an extractvalue instruction with the specified parameters.
1760 ///
Frits van Bommelaf72c622010-12-03 14:54:33 +00001761 /// Null is returned if the indices are invalid for the specified type.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001762 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001763
Owen Anderson5678d6e2008-06-19 17:15:57 +00001764 typedef const unsigned* idx_iterator;
1765 inline idx_iterator idx_begin() const { return Indices.begin(); }
1766 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001767
1768 Value *getAggregateOperand() {
1769 return getOperand(0);
1770 }
1771 const Value *getAggregateOperand() const {
1772 return getOperand(0);
1773 }
1774 static unsigned getAggregateOperandIndex() {
1775 return 0U; // get index for modifying correct operand
1776 }
1777
Jay Foadfc6d3a42011-07-13 10:26:04 +00001778 ArrayRef<unsigned> getIndices() const {
1779 return Indices;
1780 }
1781
1782 unsigned getNumIndices() const {
Bill Wendling67944fc2008-06-05 07:35:27 +00001783 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001784 }
1785
1786 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001787 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001788 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001789
Dan Gohman041e2eb2008-05-15 19:50:34 +00001790 // Methods for support type inquiry through isa, cast, and dyn_cast:
Dan Gohman041e2eb2008-05-15 19:50:34 +00001791 static inline bool classof(const Instruction *I) {
1792 return I->getOpcode() == Instruction::ExtractValue;
1793 }
1794 static inline bool classof(const Value *V) {
1795 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1796 }
1797};
1798
Dan Gohmane4569942008-05-23 00:36:11 +00001799ExtractValueInst::ExtractValueInst(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001800 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001801 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001802 Instruction *InsertBefore)
Jay Foadfc6d3a42011-07-13 10:26:04 +00001803 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
Bill Wendling85f40542008-07-22 07:14:12 +00001804 ExtractValue, Agg, InsertBefore) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001805 init(Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001806}
Dan Gohmane4569942008-05-23 00:36:11 +00001807ExtractValueInst::ExtractValueInst(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001808 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001809 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001810 BasicBlock *InsertAtEnd)
Jay Foadfc6d3a42011-07-13 10:26:04 +00001811 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
Bill Wendling85f40542008-07-22 07:14:12 +00001812 ExtractValue, Agg, InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001813 init(Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001814}
1815
Dan Gohmane4569942008-05-23 00:36:11 +00001816
Dan Gohman041e2eb2008-05-15 19:50:34 +00001817//===----------------------------------------------------------------------===//
1818// InsertValueInst Class
1819//===----------------------------------------------------------------------===//
1820
Dan Gohmane2d896f2008-05-15 23:35:32 +00001821/// InsertValueInst - This instruction inserts a struct field of array element
1822/// value into an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001823///
1824class InsertValueInst : public Instruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001825 SmallVector<unsigned, 4> Indices;
1826
Craig Topperef1623f2012-09-18 03:25:49 +00001827 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001828 InsertValueInst(const InsertValueInst &IVI);
Jay Foadfc6d3a42011-07-13 10:26:04 +00001829 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001830 const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001831
Dan Gohmane2d896f2008-05-15 23:35:32 +00001832 /// Constructors - Create a insertvalue instruction with a base aggregate
1833 /// value, a value to insert, and a list of indices. The first ctor can
1834 /// optionally insert before an existing instruction, the second appends
1835 /// the new instruction to the specified BasicBlock.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001836 inline InsertValueInst(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001837 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001838 const Twine &NameStr,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001839 Instruction *InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001840 inline InsertValueInst(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001841 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001842 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001843
1844 /// Constructors - These two constructors are convenience methods because one
1845 /// and two index insertvalue instructions are so common.
1846 InsertValueInst(Value *Agg, Value *Val,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001847 unsigned Idx, const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001848 Instruction *InsertBefore = 0);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001849 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001850 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001851protected:
1852 virtual InsertValueInst *clone_impl() const;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001853public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001854 // allocate space for exactly two operands
1855 void *operator new(size_t s) {
1856 return User::operator new(s, 2);
1857 }
1858
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001859 static InsertValueInst *Create(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001860 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001861 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001862 Instruction *InsertBefore = 0) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001863 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001864 }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001865 static InsertValueInst *Create(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001866 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001867 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001868 BasicBlock *InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001869 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001870 }
1871
Dan Gohman041e2eb2008-05-15 19:50:34 +00001872 /// Transparently provide more efficient getOperand methods.
1873 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1874
Owen Anderson5678d6e2008-06-19 17:15:57 +00001875 typedef const unsigned* idx_iterator;
1876 inline idx_iterator idx_begin() const { return Indices.begin(); }
1877 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001878
1879 Value *getAggregateOperand() {
1880 return getOperand(0);
1881 }
1882 const Value *getAggregateOperand() const {
1883 return getOperand(0);
1884 }
1885 static unsigned getAggregateOperandIndex() {
1886 return 0U; // get index for modifying correct operand
1887 }
1888
1889 Value *getInsertedValueOperand() {
1890 return getOperand(1);
1891 }
1892 const Value *getInsertedValueOperand() const {
1893 return getOperand(1);
1894 }
1895 static unsigned getInsertedValueOperandIndex() {
1896 return 1U; // get index for modifying correct operand
1897 }
1898
Jay Foadfc6d3a42011-07-13 10:26:04 +00001899 ArrayRef<unsigned> getIndices() const {
1900 return Indices;
1901 }
1902
1903 unsigned getNumIndices() const {
Bill Wendling67944fc2008-06-05 07:35:27 +00001904 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001905 }
1906
1907 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001908 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001909 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001910
Dan Gohman041e2eb2008-05-15 19:50:34 +00001911 // Methods for support type inquiry through isa, cast, and dyn_cast:
Dan Gohman041e2eb2008-05-15 19:50:34 +00001912 static inline bool classof(const Instruction *I) {
1913 return I->getOpcode() == Instruction::InsertValue;
1914 }
1915 static inline bool classof(const Value *V) {
1916 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1917 }
1918};
1919
1920template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001921struct OperandTraits<InsertValueInst> :
1922 public FixedNumOperandTraits<InsertValueInst, 2> {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001923};
1924
Dan Gohmane4569942008-05-23 00:36:11 +00001925InsertValueInst::InsertValueInst(Value *Agg,
1926 Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001927 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001928 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001929 Instruction *InsertBefore)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001930 : Instruction(Agg->getType(), InsertValue,
1931 OperandTraits<InsertValueInst>::op_begin(this),
1932 2, InsertBefore) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001933 init(Agg, Val, Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001934}
Dan Gohmane4569942008-05-23 00:36:11 +00001935InsertValueInst::InsertValueInst(Value *Agg,
1936 Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001937 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001938 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001939 BasicBlock *InsertAtEnd)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001940 : Instruction(Agg->getType(), InsertValue,
1941 OperandTraits<InsertValueInst>::op_begin(this),
1942 2, InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001943 init(Agg, Val, Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001944}
1945
Dan Gohman041e2eb2008-05-15 19:50:34 +00001946DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1947
1948//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001949// PHINode Class
1950//===----------------------------------------------------------------------===//
1951
1952// PHINode - The PHINode class is used to represent the magical mystical PHI
1953// node, that can not exist in nature, but can be synthesized in a computer
1954// scientist's overactive imagination.
1955//
1956class PHINode : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +00001957 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Chris Lattner454928e2005-01-29 00:31:36 +00001958 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1959 /// the number actually in use.
1960 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001961 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00001962 // allocate space for exactly zero operands
1963 void *operator new(size_t s) {
1964 return User::operator new(s, 0);
1965 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001966 explicit PHINode(Type *Ty, unsigned NumReservedValues,
Jay Foad3ecfc862011-03-30 11:28:46 +00001967 const Twine &NameStr = "", Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001968 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Jay Foad95c3e482011-06-23 09:09:15 +00001969 ReservedSpace(NumReservedValues) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001970 setName(NameStr);
Jay Foad3ecfc862011-03-30 11:28:46 +00001971 OperandList = allocHungoffUses(ReservedSpace);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001972 }
1973
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001974 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
Jay Foad3ecfc862011-03-30 11:28:46 +00001975 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001976 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Jay Foad95c3e482011-06-23 09:09:15 +00001977 ReservedSpace(NumReservedValues) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001978 setName(NameStr);
Jay Foad3ecfc862011-03-30 11:28:46 +00001979 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattner454928e2005-01-29 00:31:36 +00001980 }
Devang Patel50b6e332009-10-27 22:16:29 +00001981protected:
Jay Foad95c3e482011-06-23 09:09:15 +00001982 // allocHungoffUses - this is more complicated than the generic
1983 // User::allocHungoffUses, because we have to allocate Uses for the incoming
1984 // values and pointers to the incoming blocks, all in one allocation.
1985 Use *allocHungoffUses(unsigned) const;
1986
Devang Patel50b6e332009-10-27 22:16:29 +00001987 virtual PHINode *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001988public:
Jay Foad44991762011-03-30 13:29:06 +00001989 /// Constructors - NumReservedValues is a hint for the number of incoming
1990 /// edges that this phi node will have (use 0 if you really have no idea).
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001991 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
Jay Foad3ecfc862011-03-30 11:28:46 +00001992 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001993 Instruction *InsertBefore = 0) {
Jay Foad3ecfc862011-03-30 11:28:46 +00001994 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001995 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001996 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
Jay Foad3ecfc862011-03-30 11:28:46 +00001997 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1998 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001999 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002000 ~PHINode();
2001
Gabor Greifefe65362008-05-10 08:32:32 +00002002 /// Provide fast operand accessors
2003 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2004
Jay Foad95c3e482011-06-23 09:09:15 +00002005 // Block iterator interface. This provides access to the list of incoming
2006 // basic blocks, which parallels the list of incoming values.
2007
2008 typedef BasicBlock **block_iterator;
2009 typedef BasicBlock * const *const_block_iterator;
2010
2011 block_iterator block_begin() {
2012 Use::UserRef *ref =
2013 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2014 return reinterpret_cast<block_iterator>(ref + 1);
2015 }
2016
2017 const_block_iterator block_begin() const {
2018 const Use::UserRef *ref =
2019 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2020 return reinterpret_cast<const_block_iterator>(ref + 1);
2021 }
2022
2023 block_iterator block_end() {
2024 return block_begin() + getNumOperands();
2025 }
2026
2027 const_block_iterator block_end() const {
2028 return block_begin() + getNumOperands();
2029 }
2030
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002031 /// getNumIncomingValues - Return the number of incoming edges
2032 ///
Jay Foad95c3e482011-06-23 09:09:15 +00002033 unsigned getNumIncomingValues() const { return getNumOperands(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002034
Reid Spencerc773de62006-05-19 19:07:54 +00002035 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002036 ///
2037 Value *getIncomingValue(unsigned i) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002038 return getOperand(i);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002039 }
2040 void setIncomingValue(unsigned i, Value *V) {
Jay Foad95c3e482011-06-23 09:09:15 +00002041 setOperand(i, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002042 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00002043 static unsigned getOperandNumForIncomingValue(unsigned i) {
Jay Foad95c3e482011-06-23 09:09:15 +00002044 return i;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002045 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00002046 static unsigned getIncomingValueNumForOperand(unsigned i) {
Jay Foad95c3e482011-06-23 09:09:15 +00002047 return i;
Dan Gohmanb45088c2009-03-23 15:48:29 +00002048 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002049
Dan Gohmanfb76fe02010-02-22 04:10:52 +00002050 /// getIncomingBlock - Return incoming basic block number @p i.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002051 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00002052 BasicBlock *getIncomingBlock(unsigned i) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002053 return block_begin()[i];
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002054 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002055
Chris Lattnerceaa4572009-10-10 07:42:42 +00002056 /// getIncomingBlock - Return incoming basic block corresponding
2057 /// to an operand of the PHI.
2058 ///
2059 BasicBlock *getIncomingBlock(const Use &U) const {
2060 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
Jay Foad95c3e482011-06-23 09:09:15 +00002061 return getIncomingBlock(unsigned(&U - op_begin()));
Chris Lattnerceaa4572009-10-10 07:42:42 +00002062 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002063
Chris Lattnerceaa4572009-10-10 07:42:42 +00002064 /// getIncomingBlock - Return incoming basic block corresponding
2065 /// to value use iterator.
2066 ///
2067 template <typename U>
2068 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
2069 return getIncomingBlock(I.getUse());
2070 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002071
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002072 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Jay Foad95c3e482011-06-23 09:09:15 +00002073 block_begin()[i] = BB;
Dan Gohmanb45088c2009-03-23 15:48:29 +00002074 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002075
2076 /// addIncoming - Add an incoming value to the end of the PHI list
2077 ///
2078 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00002079 assert(V && "PHI node got a null value!");
2080 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002081 assert(getType() == V->getType() &&
2082 "All operands to PHI node must be the same type as the PHI node!");
Jay Foad95c3e482011-06-23 09:09:15 +00002083 if (NumOperands == ReservedSpace)
Jay Foad8891ed72011-04-01 08:00:58 +00002084 growOperands(); // Get more space!
Chris Lattner454928e2005-01-29 00:31:36 +00002085 // Initialize some new operands.
Jay Foad95c3e482011-06-23 09:09:15 +00002086 ++NumOperands;
2087 setIncomingValue(NumOperands - 1, V);
2088 setIncomingBlock(NumOperands - 1, BB);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002089 }
Misha Brukman9769ab22005-04-21 20:19:05 +00002090
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002091 /// removeIncomingValue - Remove an incoming value. This is useful if a
2092 /// predecessor basic block is deleted. The value removed is returned.
2093 ///
2094 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2095 /// is true), the PHI node is destroyed and any uses of it are replaced with
2096 /// dummy values. The only time there should be zero incoming values to a PHI
2097 /// node is when the block is dead, so this strategy is sound.
2098 ///
2099 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2100
Gabor Greifefe65362008-05-10 08:32:32 +00002101 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002102 int Idx = getBasicBlockIndex(BB);
2103 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2104 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2105 }
2106
Misha Brukman9769ab22005-04-21 20:19:05 +00002107 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002108 /// block in the value list for this PHI. Returns -1 if no instance.
2109 ///
2110 int getBasicBlockIndex(const BasicBlock *BB) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002111 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2112 if (block_begin()[i] == BB)
2113 return i;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002114 return -1;
2115 }
2116
2117 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002118 int Idx = getBasicBlockIndex(BB);
2119 assert(Idx >= 0 && "Invalid basic block argument!");
2120 return getIncomingValue(Idx);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002121 }
2122
Chris Lattnerf56a8db2006-10-03 17:09:12 +00002123 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00002124 /// same value, return the value, otherwise return null.
Duncan Sandsff103412010-11-17 04:30:22 +00002125 Value *hasConstantValue() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00002126
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002127 /// Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002128 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00002129 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002130 }
2131 static inline bool classof(const Value *V) {
2132 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2133 }
Chris Lattner454928e2005-01-29 00:31:36 +00002134 private:
Jay Foad8891ed72011-04-01 08:00:58 +00002135 void growOperands();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002136};
2137
Gabor Greifefe65362008-05-10 08:32:32 +00002138template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002139struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00002140};
2141
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002142DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002143
Bill Wendlinge6e88262011-08-12 20:24:12 +00002144//===----------------------------------------------------------------------===//
2145// LandingPadInst Class
2146//===----------------------------------------------------------------------===//
2147
2148//===---------------------------------------------------------------------------
2149/// LandingPadInst - The landingpad instruction holds all of the information
2150/// necessary to generate correct exception handling. The landingpad instruction
2151/// cannot be moved from the top of a landing pad block, which itself is
2152/// accessible only from the 'unwind' edge of an invoke. This uses the
2153/// SubclassData field in Value to store whether or not the landingpad is a
2154/// cleanup.
2155///
2156class LandingPadInst : public Instruction {
2157 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2158 /// the number actually in use.
2159 unsigned ReservedSpace;
2160 LandingPadInst(const LandingPadInst &LP);
2161public:
2162 enum ClauseType { Catch, Filter };
2163private:
Craig Topperef1623f2012-09-18 03:25:49 +00002164 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Bill Wendlinge6e88262011-08-12 20:24:12 +00002165 // Allocate space for exactly zero operands.
2166 void *operator new(size_t s) {
2167 return User::operator new(s, 0);
2168 }
2169 void growOperands(unsigned Size);
2170 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2171
2172 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2173 unsigned NumReservedValues, const Twine &NameStr,
2174 Instruction *InsertBefore);
2175 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2176 unsigned NumReservedValues, const Twine &NameStr,
2177 BasicBlock *InsertAtEnd);
2178protected:
2179 virtual LandingPadInst *clone_impl() const;
2180public:
2181 /// Constructors - NumReservedClauses is a hint for the number of incoming
2182 /// clauses that this landingpad will have (use 0 if you really have no idea).
2183 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2184 unsigned NumReservedClauses,
2185 const Twine &NameStr = "",
2186 Instruction *InsertBefore = 0);
2187 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2188 unsigned NumReservedClauses,
2189 const Twine &NameStr, BasicBlock *InsertAtEnd);
2190 ~LandingPadInst();
2191
2192 /// Provide fast operand accessors
2193 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2194
2195 /// getPersonalityFn - Get the personality function associated with this
2196 /// landing pad.
2197 Value *getPersonalityFn() const { return getOperand(0); }
2198
2199 /// isCleanup - Return 'true' if this landingpad instruction is a
2200 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2201 /// doesn't catch the exception.
2202 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2203
2204 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2205 void setCleanup(bool V) {
2206 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2207 (V ? 1 : 0));
2208 }
2209
2210 /// addClause - Add a catch or filter clause to the landing pad.
2211 void addClause(Value *ClauseVal);
2212
2213 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2214 /// to determine what type of clause this is.
2215 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2216
2217 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2218 bool isCatch(unsigned Idx) const {
2219 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2220 }
2221
2222 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2223 bool isFilter(unsigned Idx) const {
2224 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2225 }
2226
2227 /// getNumClauses - Get the number of clauses for this landing pad.
2228 unsigned getNumClauses() const { return getNumOperands() - 1; }
2229
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00002230 /// reserveClauses - Grow the size of the operand list to accommodate the new
Bill Wendlinge6e88262011-08-12 20:24:12 +00002231 /// number of clauses.
2232 void reserveClauses(unsigned Size) { growOperands(Size); }
2233
2234 // Methods for support type inquiry through isa, cast, and dyn_cast:
Bill Wendlinge6e88262011-08-12 20:24:12 +00002235 static inline bool classof(const Instruction *I) {
2236 return I->getOpcode() == Instruction::LandingPad;
2237 }
2238 static inline bool classof(const Value *V) {
2239 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2240 }
2241};
2242
2243template <>
2244struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2245};
2246
2247DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002248
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002249//===----------------------------------------------------------------------===//
2250// ReturnInst Class
2251//===----------------------------------------------------------------------===//
2252
2253//===---------------------------------------------------------------------------
2254/// ReturnInst - Return a value (possibly void), from a function. Execution
2255/// does not continue in this function any longer.
2256///
2257class ReturnInst : public TerminatorInst {
Chris Lattner910c80a2007-02-24 00:55:48 +00002258 ReturnInst(const ReturnInst &RI);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002259
Gabor Greif051a9502008-04-06 20:25:17 +00002260private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002261 // ReturnInst constructors:
2262 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002263 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002264 // ReturnInst(Value* X) - 'ret X' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00002265 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002266 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
Gabor Greifefe65362008-05-10 08:32:32 +00002267 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2268 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002269 //
2270 // NOTE: If the Value* passed is of type void then the constructor behaves as
2271 // if it was passed NULL.
Owen Anderson1d0be152009-08-13 21:58:54 +00002272 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2273 Instruction *InsertBefore = 0);
2274 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2275 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002276protected:
2277 virtual ReturnInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002278public:
Owen Anderson1d0be152009-08-13 21:58:54 +00002279 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2280 Instruction *InsertBefore = 0) {
2281 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002282 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002283 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2284 BasicBlock *InsertAtEnd) {
2285 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002286 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002287 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2288 return new(0) ReturnInst(C, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002289 }
Devang Patel57ef4f42008-02-23 00:35:18 +00002290 virtual ~ReturnInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002291
Gabor Greifefe65362008-05-10 08:32:32 +00002292 /// Provide fast operand accessors
2293 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Devang Patel64d4e612008-02-26 17:56:20 +00002294
Dan Gohman8faa6af2010-10-06 16:59:24 +00002295 /// Convenience accessor. Returns null if there is no return value.
2296 Value *getReturnValue() const {
2297 return getNumOperands() != 0 ? getOperand(0) : 0;
Devang Patel1eafa062008-03-11 17:35:03 +00002298 }
2299
Chris Lattner454928e2005-01-29 00:31:36 +00002300 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002301
2302 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002303 static inline bool classof(const Instruction *I) {
2304 return (I->getOpcode() == Instruction::Ret);
2305 }
2306 static inline bool classof(const Value *V) {
2307 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2308 }
Chris Lattner454928e2005-01-29 00:31:36 +00002309 private:
2310 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2311 virtual unsigned getNumSuccessorsV() const;
2312 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002313};
2314
Gabor Greifefe65362008-05-10 08:32:32 +00002315template <>
Jay Foad67c619b2011-01-11 15:07:38 +00002316struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
Gabor Greifefe65362008-05-10 08:32:32 +00002317};
2318
2319DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002320
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002321//===----------------------------------------------------------------------===//
2322// BranchInst Class
2323//===----------------------------------------------------------------------===//
2324
2325//===---------------------------------------------------------------------------
2326/// BranchInst - Conditional or Unconditional Branch instruction.
2327///
2328class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00002329 /// Ops list - Branches are strange. The operands are ordered:
Gabor Greifae5a20a2009-03-12 18:34:49 +00002330 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2331 /// they don't have to check for cond/uncond branchness. These are mostly
2332 /// accessed relative from op_end().
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002333 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00002334 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002335 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2336 // BranchInst(BB *B) - 'br B'
2337 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2338 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2339 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2340 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2341 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00002342 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002343 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002344 Instruction *InsertBefore = 0);
2345 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002346 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002347 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002348protected:
2349 virtual BranchInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002350public:
2351 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
Jay Foad8e3914d2011-01-07 20:29:02 +00002352 return new(1) BranchInst(IfTrue, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002353 }
Gabor Greifefe65362008-05-10 08:32:32 +00002354 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2355 Value *Cond, Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002356 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2357 }
2358 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
Jay Foad8e3914d2011-01-07 20:29:02 +00002359 return new(1) BranchInst(IfTrue, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002360 }
Gabor Greifefe65362008-05-10 08:32:32 +00002361 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2362 Value *Cond, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002363 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2364 }
Chris Lattner454928e2005-01-29 00:31:36 +00002365
2366 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00002367 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002368
Devang Patel4d4a5e02008-02-23 01:11:02 +00002369 bool isUnconditional() const { return getNumOperands() == 1; }
2370 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002371
Devang Patel4d4a5e02008-02-23 01:11:02 +00002372 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002373 assert(isConditional() && "Cannot get condition of an uncond branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002374 return Op<-3>();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002375 }
2376
2377 void setCondition(Value *V) {
2378 assert(isConditional() && "Cannot set condition of unconditional branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002379 Op<-3>() = V;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002380 }
2381
Chris Lattner454928e2005-01-29 00:31:36 +00002382 unsigned getNumSuccessors() const { return 1+isConditional(); }
2383
2384 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002385 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002386 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002387 }
2388
Chris Lattner454928e2005-01-29 00:31:36 +00002389 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002390 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner4b122932009-10-27 16:49:53 +00002391 *(&Op<-1>() - idx) = (Value*)NewSucc;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002392 }
2393
Chandler Carruth602650c2011-10-17 01:11:57 +00002394 /// \brief Swap the successors of this branch instruction.
2395 ///
2396 /// Swaps the successors of the branch instruction. This also swaps any
2397 /// branch weight metadata associated with the instruction so that it
2398 /// continues to map correctly to each operand.
2399 void swapSuccessors();
2400
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002401 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002402 static inline bool classof(const Instruction *I) {
2403 return (I->getOpcode() == Instruction::Br);
2404 }
2405 static inline bool classof(const Value *V) {
2406 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2407 }
Chris Lattner454928e2005-01-29 00:31:36 +00002408private:
2409 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2410 virtual unsigned getNumSuccessorsV() const;
2411 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002412};
2413
Gabor Greifefe65362008-05-10 08:32:32 +00002414template <>
Jay Foad67c619b2011-01-11 15:07:38 +00002415struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2416};
Gabor Greifefe65362008-05-10 08:32:32 +00002417
2418DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2419
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002420//===----------------------------------------------------------------------===//
2421// SwitchInst Class
2422//===----------------------------------------------------------------------===//
2423
2424//===---------------------------------------------------------------------------
2425/// SwitchInst - Multiway switch
2426///
2427class SwitchInst : public TerminatorInst {
Craig Topperef1623f2012-09-18 03:25:49 +00002428 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Chris Lattner454928e2005-01-29 00:31:36 +00002429 unsigned ReservedSpace;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002430 // Operands format:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002431 // Operand[0] = Value to switch on
2432 // Operand[1] = Default basic block destination
2433 // Operand[2n ] = Value to match
2434 // Operand[2n+1] = BasicBlock to go to on match
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002435
2436 // Store case values separately from operands list. We needn't User-Use
2437 // concept here, since it is just a case value, it will always constant,
2438 // and case value couldn't reused with another instructions/values.
2439 // Additionally:
2440 // It allows us to use custom type for case values that is not inherited
2441 // from Value. Since case value is a complex type that implements
2442 // the subset of integers, we needn't extract sub-constants within
2443 // slow getAggregateElement method.
2444 // For case values we will use std::list to by two reasons:
2445 // 1. It allows to add/remove cases without whole collection reallocation.
2446 // 2. In most of cases we needn't random access.
2447 // Currently case values are also stored in Operands List, but it will moved
2448 // out in future commits.
2449 typedef std::list<IntegersSubset> Subsets;
2450 typedef Subsets::iterator SubsetsIt;
2451 typedef Subsets::const_iterator SubsetsConstIt;
2452
2453 Subsets TheSubsets;
2454
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002455 SwitchInst(const SwitchInst &SI);
Chris Lattneraa6e3502010-11-17 05:41:46 +00002456 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
Jay Foad8891ed72011-04-01 08:00:58 +00002457 void growOperands();
Gabor Greifefe65362008-05-10 08:32:32 +00002458 // allocate space for exactly zero operands
2459 void *operator new(size_t s) {
2460 return User::operator new(s, 0);
2461 }
Chris Lattner454928e2005-01-29 00:31:36 +00002462 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2463 /// switch on and a default destination. The number of additional cases can
2464 /// be specified here to make memory allocation more efficient. This
2465 /// constructor can also autoinsert before another instruction.
2466 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002467 Instruction *InsertBefore);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002468
Chris Lattner454928e2005-01-29 00:31:36 +00002469 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2470 /// switch on and a default destination. The number of additional cases can
2471 /// be specified here to make memory allocation more efficient. This
2472 /// constructor also autoinserts at the end of the specified BasicBlock.
2473 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002474 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002475protected:
2476 virtual SwitchInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002477public:
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002478
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002479 // FIXME: Currently there are a lot of unclean template parameters,
2480 // we need to make refactoring in future.
2481 // All these parameters are used to implement both iterator and const_iterator
2482 // without code duplication.
2483 // SwitchInstTy may be "const SwitchInst" or "SwitchInst"
2484 // ConstantIntTy may be "const ConstantInt" or "ConstantInt"
2485 // SubsetsItTy may be SubsetsConstIt or SubsetsIt
2486 // BasicBlockTy may be "const BasicBlock" or "BasicBlock"
2487 template <class SwitchInstTy, class ConstantIntTy,
2488 class SubsetsItTy, class BasicBlockTy>
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002489 class CaseIteratorT;
2490
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002491 typedef CaseIteratorT<const SwitchInst, const ConstantInt,
2492 SubsetsConstIt, const BasicBlock> ConstCaseIt;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002493 class CaseIt;
2494
Aaron Ballmana13fb622012-03-10 23:03:01 +00002495 // -2
2496 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002497
Gabor Greifefe65362008-05-10 08:32:32 +00002498 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2499 unsigned NumCases, Instruction *InsertBefore = 0) {
2500 return new SwitchInst(Value, Default, NumCases, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002501 }
Gabor Greifefe65362008-05-10 08:32:32 +00002502 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2503 unsigned NumCases, BasicBlock *InsertAtEnd) {
2504 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002505 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002506
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002507 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00002508
Gabor Greifefe65362008-05-10 08:32:32 +00002509 /// Provide fast operand accessors
2510 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2511
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002512 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00002513 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00002514 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00002515
Devang Patel4d4a5e02008-02-23 01:11:02 +00002516 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002517 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002518 }
2519
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002520 void setDefaultDest(BasicBlock *DefaultCase) {
2521 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2522 }
2523
2524 /// getNumCases - return the number of 'cases' in this switch instruction,
2525 /// except the default case
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002526 unsigned getNumCases() const {
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002527 return getNumOperands()/2 - 1;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002528 }
2529
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002530 /// Returns a read/write iterator that points to the first
2531 /// case in SwitchInst.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002532 CaseIt case_begin() {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002533 return CaseIt(this, 0, TheSubsets.begin());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002534 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002535 /// Returns a read-only iterator that points to the first
2536 /// case in the SwitchInst.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002537 ConstCaseIt case_begin() const {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002538 return ConstCaseIt(this, 0, TheSubsets.begin());
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002539 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002540
2541 /// Returns a read/write iterator that points one past the last
2542 /// in the SwitchInst.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002543 CaseIt case_end() {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002544 return CaseIt(this, getNumCases(), TheSubsets.end());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002545 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002546 /// Returns a read-only iterator that points one past the last
2547 /// in the SwitchInst.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002548 ConstCaseIt case_end() const {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002549 return ConstCaseIt(this, getNumCases(), TheSubsets.end());
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002550 }
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002551 /// Returns an iterator that points to the default case.
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002552 /// Note: this iterator allows to resolve successor only. Attempt
2553 /// to resolve case value causes an assertion.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002554 /// Also note, that increment and decrement also causes an assertion and
2555 /// makes iterator invalid.
2556 CaseIt case_default() {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002557 return CaseIt(this, DefaultPseudoIndex, TheSubsets.end());
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002558 }
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002559 ConstCaseIt case_default() const {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002560 return ConstCaseIt(this, DefaultPseudoIndex, TheSubsets.end());
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002561 }
2562
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002563 /// findCaseValue - Search all of the case values for the specified constant.
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002564 /// If it is explicitly handled, return the case iterator of it, otherwise
2565 /// return default case iterator to indicate
2566 /// that it is handled by the default handler.
2567 CaseIt findCaseValue(const ConstantInt *C) {
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002568 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
Stepan Dyatkovskiyf8d14c42012-06-01 10:06:14 +00002569 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002570 return i;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002571 return case_default();
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002572 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002573 ConstCaseIt findCaseValue(const ConstantInt *C) const {
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002574 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
Stepan Dyatkovskiyf8d14c42012-06-01 10:06:14 +00002575 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002576 return i;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002577 return case_default();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002578 }
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002579
Nick Lewycky011f1842006-09-18 19:03:59 +00002580 /// findCaseDest - Finds the unique case value for a given successor. Returns
2581 /// null if the successor is not found, not unique, or is the default case.
2582 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00002583 if (BB == getDefaultDest()) return NULL;
2584
Nick Lewycky011f1842006-09-18 19:03:59 +00002585 ConstantInt *CI = NULL;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002586 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002587 if (i.getCaseSuccessor() == BB) {
Nick Lewycky011f1842006-09-18 19:03:59 +00002588 if (CI) return NULL; // Multiple cases lead to BB.
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002589 else CI = i.getCaseValue();
Nick Lewycky011f1842006-09-18 19:03:59 +00002590 }
2591 }
2592 return CI;
2593 }
2594
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002595 /// addCase - Add an entry to the switch instruction...
Dmitri Gribenko67c89782012-09-12 16:59:47 +00002596 /// @deprecated
Stepan Dyatkovskiyc347b6f2012-03-13 12:37:10 +00002597 /// Note:
2598 /// This action invalidates case_end(). Old case_end() iterator will
2599 /// point to the added case.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002600 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002601
2602 /// addCase - Add an entry to the switch instruction.
2603 /// Note:
2604 /// This action invalidates case_end(). Old case_end() iterator will
2605 /// point to the added case.
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00002606 void addCase(IntegersSubset& OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002607
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002608 /// removeCase - This method removes the specified case and its successor
2609 /// from the switch instruction. Note that this operation may reorder the
Jay Foad0faa6092011-02-01 09:22:34 +00002610 /// remaining cases at index idx and above.
Stepan Dyatkovskiyc347b6f2012-03-13 12:37:10 +00002611 /// Note:
2612 /// This action invalidates iterators for all cases following the one removed,
2613 /// including the case_end() iterator.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002614 void removeCase(CaseIt& i);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002615
Chris Lattner454928e2005-01-29 00:31:36 +00002616 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2617 BasicBlock *getSuccessor(unsigned idx) const {
2618 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2619 return cast<BasicBlock>(getOperand(idx*2+1));
2620 }
2621 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002622 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Chris Lattner4b122932009-10-27 16:49:53 +00002623 setOperand(idx*2+1, (Value*)NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002624 }
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002625
Stepan Dyatkovskiy734dde82012-05-14 08:26:31 +00002626 uint16_t hash() const {
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002627 uint32_t NumberOfCases = (uint32_t)getNumCases();
2628 uint16_t Hash = (0xFFFF & NumberOfCases) ^ (NumberOfCases >> 16);
2629 for (ConstCaseIt i = case_begin(), e = case_end();
2630 i != e; ++i) {
2631 uint32_t NumItems = (uint32_t)i.getCaseValueEx().getNumItems();
2632 Hash = (Hash << 1) ^ (0xFFFF & NumItems) ^ (NumItems >> 16);
2633 }
2634 return Hash;
2635 }
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002636
2637 // Case iterators definition.
2638
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002639 template <class SwitchInstTy, class ConstantIntTy,
2640 class SubsetsItTy, class BasicBlockTy>
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002641 class CaseIteratorT {
2642 protected:
2643
2644 SwitchInstTy *SI;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002645 unsigned long Index;
2646 SubsetsItTy SubsetIt;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002647
2648 /// Initializes case iterator for given SwitchInst and for given
2649 /// case number.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002650 friend class SwitchInst;
2651 CaseIteratorT(SwitchInstTy *SI, unsigned SuccessorIndex,
2652 SubsetsItTy CaseValueIt) {
Duncan Sands37eeb052012-06-22 10:35:06 +00002653 this->SI = SI;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002654 Index = SuccessorIndex;
2655 this->SubsetIt = CaseValueIt;
Duncan Sands37eeb052012-06-22 10:35:06 +00002656 }
Stepan Dyatkovskiy73512562012-06-22 07:35:13 +00002657
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002658 public:
2659 typedef typename SubsetsItTy::reference IntegersSubsetRef;
2660 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy,
2661 SubsetsItTy, BasicBlockTy> Self;
2662
2663 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2664 this->SI = SI;
2665 Index = CaseNum;
2666 SubsetIt = SI->TheSubsets.begin();
2667 std::advance(SubsetIt, CaseNum);
2668 }
2669
2670
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002671 /// Initializes case iterator for given SwitchInst and for given
2672 /// TerminatorInst's successor index.
2673 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2674 assert(SuccessorIndex < SI->getNumSuccessors() &&
2675 "Successor index # out of range!");
2676 return SuccessorIndex != 0 ?
2677 Self(SI, SuccessorIndex - 1) :
2678 Self(SI, DefaultPseudoIndex);
2679 }
2680
2681 /// Resolves case value for current case.
Dmitri Gribenko67c89782012-09-12 16:59:47 +00002682 /// @deprecated
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002683 ConstantIntTy *getCaseValue() {
2684 assert(Index < SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002685 IntegersSubsetRef CaseRanges = *SubsetIt;
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00002686
2687 // FIXME: Currently we work with ConstantInt based cases.
2688 // So return CaseValue as ConstantInt.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002689 return CaseRanges.getSingleNumber(0).toConstantInt();
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002690 }
2691
2692 /// Resolves case value for current case.
Stepan Dyatkovskiy47cbc4e2012-06-23 10:58:58 +00002693 IntegersSubsetRef getCaseValueEx() {
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002694 assert(Index < SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002695 return *SubsetIt;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002696 }
2697
2698 /// Resolves successor for current case.
2699 BasicBlockTy *getCaseSuccessor() {
2700 assert((Index < SI->getNumCases() ||
2701 Index == DefaultPseudoIndex) &&
2702 "Index out the number of cases.");
2703 return SI->getSuccessor(getSuccessorIndex());
2704 }
2705
2706 /// Returns number of current case.
2707 unsigned getCaseIndex() const { return Index; }
2708
2709 /// Returns TerminatorInst's successor index for current case successor.
2710 unsigned getSuccessorIndex() const {
2711 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2712 "Index out the number of cases.");
2713 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2714 }
2715
2716 Self operator++() {
2717 // Check index correctness after increment.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002718 // Note: Index == getNumCases() means end().
Kaelyn Uhrain35ac4b02012-06-22 17:18:15 +00002719 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002720 ++Index;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002721 if (Index == 0)
2722 SubsetIt = SI->TheSubsets.begin();
2723 else
2724 ++SubsetIt;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002725 return *this;
2726 }
2727 Self operator++(int) {
2728 Self tmp = *this;
2729 ++(*this);
2730 return tmp;
2731 }
2732 Self operator--() {
2733 // Check index correctness after decrement.
2734 // Note: Index == getNumCases() means end().
2735 // Also allow "-1" iterator here. That will became valid after ++.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002736 unsigned NumCases = SI->getNumCases();
2737 assert((Index == 0 || Index-1 <= NumCases) &&
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002738 "Index out the number of cases.");
2739 --Index;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002740 if (Index == NumCases) {
2741 SubsetIt = SI->TheSubsets.end();
2742 return *this;
2743 }
2744
2745 if (Index != -1UL)
2746 --SubsetIt;
2747
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002748 return *this;
2749 }
2750 Self operator--(int) {
2751 Self tmp = *this;
2752 --(*this);
2753 return tmp;
2754 }
2755 bool operator==(const Self& RHS) const {
2756 assert(RHS.SI == SI && "Incompatible operators.");
2757 return RHS.Index == Index;
2758 }
2759 bool operator!=(const Self& RHS) const {
2760 assert(RHS.SI == SI && "Incompatible operators.");
2761 return RHS.Index != Index;
2762 }
2763 };
2764
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002765 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt,
2766 SubsetsIt, BasicBlock> {
2767 typedef CaseIteratorT<SwitchInst, ConstantInt, SubsetsIt, BasicBlock>
2768 ParentTy;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002769
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002770 protected:
2771 friend class SwitchInst;
2772 CaseIt(SwitchInst *SI, unsigned CaseNum, SubsetsIt SubsetIt) :
2773 ParentTy(SI, CaseNum, SubsetIt) {}
2774
2775 void updateCaseValueOperand(IntegersSubset& V) {
2776 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>((Constant*)V));
2777 }
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002778
2779 public:
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002780
2781 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002782
2783 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002784
2785 /// Sets the new value for current case.
Dmitri Gribenko67c89782012-09-12 16:59:47 +00002786 /// @deprecated.
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002787 void setValue(ConstantInt *V) {
2788 assert(Index < SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00002789 IntegersSubsetToBB Mapping;
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00002790 // FIXME: Currently we work with ConstantInt based cases.
2791 // So inititalize IntItem container directly from ConstantInt.
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00002792 Mapping.add(IntItem::fromConstantInt(V));
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002793 *SubsetIt = Mapping.getCase();
2794 updateCaseValueOperand(*SubsetIt);
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002795 }
2796
2797 /// Sets the new value for current case.
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00002798 void setValueEx(IntegersSubset& V) {
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002799 assert(Index < SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002800 *SubsetIt = V;
2801 updateCaseValueOperand(*SubsetIt);
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002802 }
2803
2804 /// Sets the new successor for current case.
2805 void setSuccessor(BasicBlock *S) {
2806 SI->setSuccessor(getSuccessorIndex(), S);
2807 }
2808 };
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002809
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002810 // Methods for support type inquiry through isa, cast, and dyn_cast:
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002811
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002812 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00002813 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002814 }
2815 static inline bool classof(const Value *V) {
2816 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2817 }
Chris Lattner454928e2005-01-29 00:31:36 +00002818private:
2819 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2820 virtual unsigned getNumSuccessorsV() const;
2821 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002822};
2823
Gabor Greifefe65362008-05-10 08:32:32 +00002824template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002825struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00002826};
2827
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002828DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002829
2830
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002831//===----------------------------------------------------------------------===//
Chris Lattnerab21db72009-10-28 00:19:10 +00002832// IndirectBrInst Class
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002833//===----------------------------------------------------------------------===//
2834
2835//===---------------------------------------------------------------------------
Chris Lattnerab21db72009-10-28 00:19:10 +00002836/// IndirectBrInst - Indirect Branch Instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002837///
Chris Lattnerab21db72009-10-28 00:19:10 +00002838class IndirectBrInst : public TerminatorInst {
Craig Topperef1623f2012-09-18 03:25:49 +00002839 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002840 unsigned ReservedSpace;
2841 // Operand[0] = Value to switch on
2842 // Operand[1] = Default basic block destination
2843 // Operand[2n ] = Value to match
2844 // Operand[2n+1] = BasicBlock to go to on match
Chris Lattnerab21db72009-10-28 00:19:10 +00002845 IndirectBrInst(const IndirectBrInst &IBI);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002846 void init(Value *Address, unsigned NumDests);
Jay Foad8891ed72011-04-01 08:00:58 +00002847 void growOperands();
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002848 // allocate space for exactly zero operands
2849 void *operator new(size_t s) {
2850 return User::operator new(s, 0);
2851 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002852 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2853 /// Address to jump to. The number of expected destinations can be specified
2854 /// here to make memory allocation more efficient. This constructor can also
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002855 /// autoinsert before another instruction.
Chris Lattnerab21db72009-10-28 00:19:10 +00002856 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002857
Chris Lattnerab21db72009-10-28 00:19:10 +00002858 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2859 /// Address to jump to. The number of expected destinations can be specified
2860 /// here to make memory allocation more efficient. This constructor also
2861 /// autoinserts at the end of the specified BasicBlock.
2862 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002863protected:
Chris Lattnerab21db72009-10-28 00:19:10 +00002864 virtual IndirectBrInst *clone_impl() const;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002865public:
Chris Lattnerab21db72009-10-28 00:19:10 +00002866 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2867 Instruction *InsertBefore = 0) {
2868 return new IndirectBrInst(Address, NumDests, InsertBefore);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002869 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002870 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2871 BasicBlock *InsertAtEnd) {
2872 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002873 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002874 ~IndirectBrInst();
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002875
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002876 /// Provide fast operand accessors.
2877 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002878
Chris Lattnerab21db72009-10-28 00:19:10 +00002879 // Accessor Methods for IndirectBrInst instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002880 Value *getAddress() { return getOperand(0); }
2881 const Value *getAddress() const { return getOperand(0); }
2882 void setAddress(Value *V) { setOperand(0, V); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002883
2884
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002885 /// getNumDestinations - return the number of possible destinations in this
Chris Lattnerab21db72009-10-28 00:19:10 +00002886 /// indirectbr instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002887 unsigned getNumDestinations() const { return getNumOperands()-1; }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002888
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002889 /// getDestination - Return the specified destination.
2890 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2891 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002892
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002893 /// addDestination - Add a destination.
2894 ///
2895 void addDestination(BasicBlock *Dest);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002896
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002897 /// removeDestination - This method removes the specified successor from the
Chris Lattnerab21db72009-10-28 00:19:10 +00002898 /// indirectbr instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002899 void removeDestination(unsigned i);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002900
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002901 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2902 BasicBlock *getSuccessor(unsigned i) const {
2903 return cast<BasicBlock>(getOperand(i+1));
2904 }
2905 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2906 setOperand(i+1, (Value*)NewSucc);
2907 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002908
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002909 // Methods for support type inquiry through isa, cast, and dyn_cast:
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002910 static inline bool classof(const Instruction *I) {
Chris Lattnerab21db72009-10-28 00:19:10 +00002911 return I->getOpcode() == Instruction::IndirectBr;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002912 }
2913 static inline bool classof(const Value *V) {
2914 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2915 }
2916private:
2917 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2918 virtual unsigned getNumSuccessorsV() const;
2919 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2920};
2921
2922template <>
Chris Lattnerab21db72009-10-28 00:19:10 +00002923struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002924};
2925
Chris Lattnerab21db72009-10-28 00:19:10 +00002926DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002927
2928
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002929//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002930// InvokeInst Class
2931//===----------------------------------------------------------------------===//
2932
Chris Lattner3340ffe2005-05-06 20:26:26 +00002933/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2934/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002935///
2936class InvokeInst : public TerminatorInst {
Devang Patel05988662008-09-25 21:00:45 +00002937 AttrListPtr AttributeList;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002938 InvokeInst(const InvokeInst &BI);
David Greenef1355a52007-08-27 19:04:21 +00002939 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002940 ArrayRef<Value *> Args, const Twine &NameStr);
David Greenef1355a52007-08-27 19:04:21 +00002941
David Greenef1355a52007-08-27 19:04:21 +00002942 /// Construct an InvokeInst given a range of arguments.
David Greenef1355a52007-08-27 19:04:21 +00002943 ///
2944 /// @brief Construct an InvokeInst from a range of arguments
Gabor Greifefe65362008-05-10 08:32:32 +00002945 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002946 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002947 const Twine &NameStr, Instruction *InsertBefore);
David Greenef1355a52007-08-27 19:04:21 +00002948
2949 /// Construct an InvokeInst given a range of arguments.
David Greenef1355a52007-08-27 19:04:21 +00002950 ///
2951 /// @brief Construct an InvokeInst from a range of arguments
Gabor Greifefe65362008-05-10 08:32:32 +00002952 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002953 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002954 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002955protected:
2956 virtual InvokeInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002957public:
Gabor Greifefe65362008-05-10 08:32:32 +00002958 static InvokeInst *Create(Value *Func,
2959 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002960 ArrayRef<Value *> Args, const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00002961 Instruction *InsertBefore = 0) {
Jay Foada3efbb12011-07-15 08:37:34 +00002962 unsigned Values = unsigned(Args.size()) + 3;
2963 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002964 Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002965 }
Gabor Greifefe65362008-05-10 08:32:32 +00002966 static InvokeInst *Create(Value *Func,
2967 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002968 ArrayRef<Value *> Args, const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002969 BasicBlock *InsertAtEnd) {
Jay Foada3efbb12011-07-15 08:37:34 +00002970 unsigned Values = unsigned(Args.size()) + 3;
2971 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002972 Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002973 }
David Greenef1355a52007-08-27 19:04:21 +00002974
Gabor Greifefe65362008-05-10 08:32:32 +00002975 /// Provide fast operand accessors
2976 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002977
Gabor Greif0114b992010-07-31 08:35:21 +00002978 /// getNumArgOperands - Return the number of invoke arguments.
2979 ///
Bill Wendling22a5b292010-06-07 19:05:06 +00002980 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
Gabor Greif0114b992010-07-31 08:35:21 +00002981
2982 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2983 ///
Bill Wendling22a5b292010-06-07 19:05:06 +00002984 Value *getArgOperand(unsigned i) const { return getOperand(i); }
Gabor Greif710ac072010-06-28 12:23:36 +00002985 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
Bill Wendling22a5b292010-06-07 19:05:06 +00002986
Chris Lattner3340ffe2005-05-06 20:26:26 +00002987 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2988 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002989 CallingConv::ID getCallingConv() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +00002990 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002991 }
2992 void setCallingConv(CallingConv::ID CC) {
Chris Lattnerb2406d92009-12-29 02:46:09 +00002993 setInstructionSubclassData(static_cast<unsigned>(CC));
Chris Lattner3340ffe2005-05-06 20:26:26 +00002994 }
2995
Devang Patel05988662008-09-25 21:00:45 +00002996 /// getAttributes - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002997 ///
Devang Patel05988662008-09-25 21:00:45 +00002998 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002999
Devang Patel05988662008-09-25 21:00:45 +00003000 /// setAttributes - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00003001 ///
Devang Patel05988662008-09-25 21:00:45 +00003002 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00003003
Devang Patel05988662008-09-25 21:00:45 +00003004 /// addAttribute - adds the attribute to the list of attributes.
3005 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00003006
Devang Patel05988662008-09-25 21:00:45 +00003007 /// removeAttribute - removes the attribute from the list of attributes.
3008 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00003009
Bill Wendling060f20a2012-10-09 00:28:54 +00003010 /// @brief Determine whether this call has the NoAlias attribute.
Bill Wendling2fa8af22012-10-09 21:49:51 +00003011 bool hasFnAttr(Attributes::AttrVal A) const;
Bill Wendling060f20a2012-10-09 00:28:54 +00003012
Bill Wendling847d1652012-10-03 17:54:26 +00003013 /// @brief Determine whether the call or the callee has the given attributes.
Bill Wendling3e2d76c2012-10-09 21:38:14 +00003014 bool paramHasAttr(unsigned i, Attributes::AttrVal A) const;
Bill Wendling847d1652012-10-03 17:54:26 +00003015
Dale Johannesen08e78b12008-02-22 17:49:45 +00003016 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003017 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00003018 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003019 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00003020
Eric Christopherf27e6082010-03-25 04:49:10 +00003021 /// @brief Return true if the call should not be inlined.
Bill Wendling2fa8af22012-10-09 21:49:51 +00003022 bool isNoInline() const { return hasFnAttr(Attributes::NoInline); }
Bill Wendling1b005072012-10-09 23:40:31 +00003023 void setIsNoInline() {
Bill Wendling46d5dd92012-10-16 05:23:31 +00003024 addAttribute(AttrListPtr::FunctionIndex,
3025 Attributes::get(getContext(), Attributes::NoInline));
Eric Christopherf27e6082010-03-25 04:49:10 +00003026 }
Nick Lewyckyb9933b82010-07-06 03:53:22 +00003027
Duncan Sandsa3355ff2007-12-03 20:06:50 +00003028 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003029 bool doesNotAccessMemory() const {
Bill Wendling2fa8af22012-10-09 21:49:51 +00003030 return hasFnAttr(Attributes::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003031 }
Bill Wendling1b005072012-10-09 23:40:31 +00003032 void setDoesNotAccessMemory() {
Bill Wendling46d5dd92012-10-16 05:23:31 +00003033 addAttribute(AttrListPtr::FunctionIndex,
3034 Attributes::get(getContext(), Attributes::ReadNone));
Duncan Sands2e033f32008-07-08 08:38:44 +00003035 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00003036
3037 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003038 bool onlyReadsMemory() const {
Bill Wendling2fa8af22012-10-09 21:49:51 +00003039 return doesNotAccessMemory() || hasFnAttr(Attributes::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003040 }
Bill Wendling1b005072012-10-09 23:40:31 +00003041 void setOnlyReadsMemory() {
Bill Wendling46d5dd92012-10-16 05:23:31 +00003042 addAttribute(AttrListPtr::FunctionIndex,
3043 Attributes::get(getContext(), Attributes::ReadOnly));
Duncan Sands2e033f32008-07-08 08:38:44 +00003044 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00003045
Duncan Sandscbb8bad2007-12-10 19:09:40 +00003046 /// @brief Determine if the call cannot return.
Bill Wendling2fa8af22012-10-09 21:49:51 +00003047 bool doesNotReturn() const { return hasFnAttr(Attributes::NoReturn); }
Bill Wendling1b005072012-10-09 23:40:31 +00003048 void setDoesNotReturn() {
Bill Wendling46d5dd92012-10-16 05:23:31 +00003049 addAttribute(AttrListPtr::FunctionIndex,
3050 Attributes::get(getContext(), Attributes::NoReturn));
Duncan Sands2e033f32008-07-08 08:38:44 +00003051 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00003052
Duncan Sandsa3355ff2007-12-03 20:06:50 +00003053 /// @brief Determine if the call cannot unwind.
Bill Wendling2fa8af22012-10-09 21:49:51 +00003054 bool doesNotThrow() const { return hasFnAttr(Attributes::NoUnwind); }
Bill Wendling1b005072012-10-09 23:40:31 +00003055 void setDoesNotThrow() {
Bill Wendling46d5dd92012-10-16 05:23:31 +00003056 addAttribute(AttrListPtr::FunctionIndex,
3057 Attributes::get(getContext(), Attributes::NoUnwind));
Duncan Sands2e033f32008-07-08 08:38:44 +00003058 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00003059
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00003060 /// @brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00003061 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003062 bool hasStructRetAttr() const {
3063 // Be friendly and also check the callee.
Bill Wendling3e2d76c2012-10-09 21:38:14 +00003064 return paramHasAttr(1, Attributes::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003065 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00003066
Dan Gohmanf2752502008-09-26 21:38:45 +00003067 /// @brief Determine if any call argument is an aggregate passed by value.
3068 bool hasByValArgument() const {
Bill Wendling2fd77652012-10-09 01:03:48 +00003069 for (unsigned I = 0, E = AttributeList.getNumAttrs(); I != E; ++I)
Bill Wendling67658342012-10-09 07:45:08 +00003070 if (AttributeList.getAttributesAtIndex(I).hasAttribute(Attributes::ByVal))
Bill Wendling2fd77652012-10-09 01:03:48 +00003071 return true;
3072 return false;
Dan Gohmanf2752502008-09-26 21:38:45 +00003073 }
3074
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003075 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00003076 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003077 ///
Chris Lattner721aef62004-11-18 17:46:57 +00003078 Function *getCalledFunction() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00003079 return dyn_cast<Function>(Op<-3>());
Chris Lattner721aef62004-11-18 17:46:57 +00003080 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003081
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00003082 /// getCalledValue - Get a pointer to the function that is invoked by this
Dan Gohmanf2752502008-09-26 21:38:45 +00003083 /// instruction
Gabor Greifc9f75002010-03-24 13:21:49 +00003084 const Value *getCalledValue() const { return Op<-3>(); }
3085 Value *getCalledValue() { return Op<-3>(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003086
Gabor Greif654c06f2010-03-20 21:00:25 +00003087 /// setCalledFunction - Set the function called.
3088 void setCalledFunction(Value* Fn) {
Gabor Greifc9f75002010-03-24 13:21:49 +00003089 Op<-3>() = Fn;
Gabor Greif654c06f2010-03-20 21:00:25 +00003090 }
3091
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003092 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00003093 BasicBlock *getNormalDest() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00003094 return cast<BasicBlock>(Op<-2>());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003095 }
Chris Lattner454928e2005-01-29 00:31:36 +00003096 BasicBlock *getUnwindDest() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00003097 return cast<BasicBlock>(Op<-1>());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003098 }
Chris Lattner454928e2005-01-29 00:31:36 +00003099 void setNormalDest(BasicBlock *B) {
Gabor Greifc9f75002010-03-24 13:21:49 +00003100 Op<-2>() = reinterpret_cast<Value*>(B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003101 }
Chris Lattner454928e2005-01-29 00:31:36 +00003102 void setUnwindDest(BasicBlock *B) {
Gabor Greifc9f75002010-03-24 13:21:49 +00003103 Op<-1>() = reinterpret_cast<Value*>(B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003104 }
3105
Bill Wendlinge6e88262011-08-12 20:24:12 +00003106 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3107 /// block (the unwind destination).
3108 LandingPadInst *getLandingPadInst() const;
3109
Devang Patel4d4a5e02008-02-23 01:11:02 +00003110 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003111 assert(i < 2 && "Successor # out of range for invoke!");
3112 return i == 0 ? getNormalDest() : getUnwindDest();
3113 }
3114
Chris Lattner454928e2005-01-29 00:31:36 +00003115 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003116 assert(idx < 2 && "Successor # out of range for invoke!");
Gabor Greifc9f75002010-03-24 13:21:49 +00003117 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003118 }
3119
Chris Lattner454928e2005-01-29 00:31:36 +00003120 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003121
3122 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003123 static inline bool classof(const Instruction *I) {
3124 return (I->getOpcode() == Instruction::Invoke);
3125 }
3126 static inline bool classof(const Value *V) {
3127 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3128 }
Gabor Greifc9f75002010-03-24 13:21:49 +00003129
Chris Lattner454928e2005-01-29 00:31:36 +00003130private:
3131 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3132 virtual unsigned getNumSuccessorsV() const;
3133 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00003134
Chris Lattnerb2406d92009-12-29 02:46:09 +00003135 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3136 // method so that subclasses cannot accidentally use it.
3137 void setInstructionSubclassData(unsigned short D) {
3138 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00003139 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003140};
3141
Gabor Greifefe65362008-05-10 08:32:32 +00003142template <>
Jay Foad67c619b2011-01-11 15:07:38 +00003143struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00003144};
3145
Gabor Greifefe65362008-05-10 08:32:32 +00003146InvokeInst::InvokeInst(Value *Func,
3147 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00003148 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003149 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00003150 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3151 ->getElementType())->getReturnType(),
3152 Instruction::Invoke,
3153 OperandTraits<InvokeInst>::op_end(this) - Values,
3154 Values, InsertBefore) {
Jay Foada3efbb12011-07-15 08:37:34 +00003155 init(Func, IfNormal, IfException, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00003156}
Gabor Greifefe65362008-05-10 08:32:32 +00003157InvokeInst::InvokeInst(Value *Func,
3158 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00003159 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003160 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00003161 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3162 ->getElementType())->getReturnType(),
3163 Instruction::Invoke,
3164 OperandTraits<InvokeInst>::op_end(this) - Values,
3165 Values, InsertAtEnd) {
Jay Foada3efbb12011-07-15 08:37:34 +00003166 init(Func, IfNormal, IfException, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00003167}
3168
3169DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003170
3171//===----------------------------------------------------------------------===//
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003172// ResumeInst Class
3173//===----------------------------------------------------------------------===//
3174
3175//===---------------------------------------------------------------------------
3176/// ResumeInst - Resume the propagation of an exception.
3177///
3178class ResumeInst : public TerminatorInst {
3179 ResumeInst(const ResumeInst &RI);
3180
3181 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3182 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3183protected:
3184 virtual ResumeInst *clone_impl() const;
3185public:
3186 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3187 return new(1) ResumeInst(Exn, InsertBefore);
3188 }
3189 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3190 return new(1) ResumeInst(Exn, InsertAtEnd);
3191 }
3192
3193 /// Provide fast operand accessors
3194 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3195
3196 /// Convenience accessor.
3197 Value *getValue() const { return Op<0>(); }
3198
3199 unsigned getNumSuccessors() const { return 0; }
3200
3201 // Methods for support type inquiry through isa, cast, and dyn_cast:
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003202 static inline bool classof(const Instruction *I) {
3203 return I->getOpcode() == Instruction::Resume;
3204 }
3205 static inline bool classof(const Value *V) {
3206 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3207 }
3208private:
3209 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3210 virtual unsigned getNumSuccessorsV() const;
3211 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3212};
3213
3214template <>
3215struct OperandTraits<ResumeInst> :
3216 public FixedNumOperandTraits<ResumeInst, 1> {
3217};
3218
3219DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3220
3221//===----------------------------------------------------------------------===//
Chris Lattner076b3f12004-10-16 18:05:54 +00003222// UnreachableInst Class
3223//===----------------------------------------------------------------------===//
3224
3225//===---------------------------------------------------------------------------
3226/// UnreachableInst - This function has undefined behavior. In particular, the
3227/// presence of this instruction indicates some higher level knowledge that the
3228/// end of the block cannot be reached.
3229///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00003230class UnreachableInst : public TerminatorInst {
Craig Topperef1623f2012-09-18 03:25:49 +00003231 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Devang Patel50b6e332009-10-27 22:16:29 +00003232protected:
3233 virtual UnreachableInst *clone_impl() const;
3234
Chris Lattner1fca5ff2004-10-27 16:14:51 +00003235public:
Gabor Greif051a9502008-04-06 20:25:17 +00003236 // allocate space for exactly zero operands
3237 void *operator new(size_t s) {
3238 return User::operator new(s, 0);
3239 }
Owen Anderson1d0be152009-08-13 21:58:54 +00003240 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3241 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00003242
Chris Lattner454928e2005-01-29 00:31:36 +00003243 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00003244
3245 // Methods for support type inquiry through isa, cast, and dyn_cast:
Chris Lattner076b3f12004-10-16 18:05:54 +00003246 static inline bool classof(const Instruction *I) {
3247 return I->getOpcode() == Instruction::Unreachable;
3248 }
3249 static inline bool classof(const Value *V) {
3250 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3251 }
Chris Lattner454928e2005-01-29 00:31:36 +00003252private:
3253 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3254 virtual unsigned getNumSuccessorsV() const;
3255 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00003256};
3257
Reid Spencer3da59db2006-11-27 01:05:10 +00003258//===----------------------------------------------------------------------===//
3259// TruncInst Class
3260//===----------------------------------------------------------------------===//
3261
3262/// @brief This class represents a truncation of integer types.
3263class TruncInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003264protected:
3265 /// @brief Clone an identical TruncInst
3266 virtual TruncInst *clone_impl() const;
3267
Reid Spencer3da59db2006-11-27 01:05:10 +00003268public:
3269 /// @brief Constructor with insert-before-instruction semantics
3270 TruncInst(
3271 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003272 Type *Ty, ///< The (smaller) type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003273 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003274 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3275 );
3276
3277 /// @brief Constructor with insert-at-end-of-block semantics
3278 TruncInst(
3279 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003280 Type *Ty, ///< The (smaller) type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003281 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003282 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3283 );
3284
Reid Spencer3da59db2006-11-27 01:05:10 +00003285 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003286 static inline bool classof(const Instruction *I) {
3287 return I->getOpcode() == Trunc;
3288 }
3289 static inline bool classof(const Value *V) {
3290 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3291 }
3292};
3293
3294//===----------------------------------------------------------------------===//
3295// ZExtInst Class
3296//===----------------------------------------------------------------------===//
3297
3298/// @brief This class represents zero extension of integer types.
3299class ZExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003300protected:
3301 /// @brief Clone an identical ZExtInst
3302 virtual ZExtInst *clone_impl() const;
3303
Reid Spencer3da59db2006-11-27 01:05:10 +00003304public:
3305 /// @brief Constructor with insert-before-instruction semantics
3306 ZExtInst(
3307 Value *S, ///< The value to be zero extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003308 Type *Ty, ///< The type to zero extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003309 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003310 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3311 );
3312
3313 /// @brief Constructor with insert-at-end semantics.
3314 ZExtInst(
3315 Value *S, ///< The value to be zero extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003316 Type *Ty, ///< The type to zero extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003317 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003318 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3319 );
3320
Reid Spencer3da59db2006-11-27 01:05:10 +00003321 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003322 static inline bool classof(const Instruction *I) {
3323 return I->getOpcode() == ZExt;
3324 }
3325 static inline bool classof(const Value *V) {
3326 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3327 }
3328};
3329
3330//===----------------------------------------------------------------------===//
3331// SExtInst Class
3332//===----------------------------------------------------------------------===//
3333
3334/// @brief This class represents a sign extension of integer types.
3335class SExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003336protected:
3337 /// @brief Clone an identical SExtInst
3338 virtual SExtInst *clone_impl() const;
3339
Reid Spencer3da59db2006-11-27 01:05:10 +00003340public:
3341 /// @brief Constructor with insert-before-instruction semantics
3342 SExtInst(
3343 Value *S, ///< The value to be sign extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003344 Type *Ty, ///< The type to sign extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003345 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003346 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3347 );
3348
3349 /// @brief Constructor with insert-at-end-of-block semantics
3350 SExtInst(
3351 Value *S, ///< The value to be sign extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003352 Type *Ty, ///< The type to sign extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003353 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003354 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3355 );
3356
Reid Spencer3da59db2006-11-27 01:05:10 +00003357 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003358 static inline bool classof(const Instruction *I) {
3359 return I->getOpcode() == SExt;
3360 }
3361 static inline bool classof(const Value *V) {
3362 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3363 }
3364};
3365
3366//===----------------------------------------------------------------------===//
3367// FPTruncInst Class
3368//===----------------------------------------------------------------------===//
3369
3370/// @brief This class represents a truncation of floating point types.
3371class FPTruncInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003372protected:
3373 /// @brief Clone an identical FPTruncInst
3374 virtual FPTruncInst *clone_impl() const;
3375
Reid Spencer3da59db2006-11-27 01:05:10 +00003376public:
3377 /// @brief Constructor with insert-before-instruction semantics
3378 FPTruncInst(
3379 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003380 Type *Ty, ///< The type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003381 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003382 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3383 );
3384
3385 /// @brief Constructor with insert-before-instruction semantics
3386 FPTruncInst(
3387 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003388 Type *Ty, ///< The type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003389 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003390 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3391 );
3392
Reid Spencer3da59db2006-11-27 01:05:10 +00003393 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003394 static inline bool classof(const Instruction *I) {
3395 return I->getOpcode() == FPTrunc;
3396 }
3397 static inline bool classof(const Value *V) {
3398 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3399 }
3400};
3401
3402//===----------------------------------------------------------------------===//
3403// FPExtInst Class
3404//===----------------------------------------------------------------------===//
3405
3406/// @brief This class represents an extension of floating point types.
3407class FPExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003408protected:
3409 /// @brief Clone an identical FPExtInst
3410 virtual FPExtInst *clone_impl() const;
3411
Reid Spencer3da59db2006-11-27 01:05:10 +00003412public:
3413 /// @brief Constructor with insert-before-instruction semantics
3414 FPExtInst(
3415 Value *S, ///< The value to be extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003416 Type *Ty, ///< The type to extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003417 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003418 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3419 );
3420
3421 /// @brief Constructor with insert-at-end-of-block semantics
3422 FPExtInst(
3423 Value *S, ///< The value to be extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003424 Type *Ty, ///< The type to extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003425 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003426 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3427 );
3428
Reid Spencer3da59db2006-11-27 01:05:10 +00003429 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003430 static inline bool classof(const Instruction *I) {
3431 return I->getOpcode() == FPExt;
3432 }
3433 static inline bool classof(const Value *V) {
3434 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3435 }
3436};
3437
3438//===----------------------------------------------------------------------===//
3439// UIToFPInst Class
3440//===----------------------------------------------------------------------===//
3441
3442/// @brief This class represents a cast unsigned integer to floating point.
3443class UIToFPInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003444protected:
3445 /// @brief Clone an identical UIToFPInst
3446 virtual UIToFPInst *clone_impl() const;
3447
Reid Spencer3da59db2006-11-27 01:05:10 +00003448public:
3449 /// @brief Constructor with insert-before-instruction semantics
3450 UIToFPInst(
3451 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003452 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003453 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003454 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3455 );
3456
3457 /// @brief Constructor with insert-at-end-of-block semantics
3458 UIToFPInst(
3459 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003460 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003461 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003462 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3463 );
3464
Reid Spencer3da59db2006-11-27 01:05:10 +00003465 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003466 static inline bool classof(const Instruction *I) {
3467 return I->getOpcode() == UIToFP;
3468 }
3469 static inline bool classof(const Value *V) {
3470 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3471 }
3472};
3473
3474//===----------------------------------------------------------------------===//
3475// SIToFPInst Class
3476//===----------------------------------------------------------------------===//
3477
3478/// @brief This class represents a cast from signed integer to floating point.
3479class SIToFPInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003480protected:
3481 /// @brief Clone an identical SIToFPInst
3482 virtual SIToFPInst *clone_impl() const;
3483
Reid Spencer3da59db2006-11-27 01:05:10 +00003484public:
3485 /// @brief Constructor with insert-before-instruction semantics
3486 SIToFPInst(
3487 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003488 Type *Ty, ///< The type to convert 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 SIToFPInst(
3495 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003496 Type *Ty, ///< The type to convert 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:
Reid Spencer3da59db2006-11-27 01:05:10 +00003502 static inline bool classof(const Instruction *I) {
3503 return I->getOpcode() == SIToFP;
3504 }
3505 static inline bool classof(const Value *V) {
3506 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3507 }
3508};
3509
3510//===----------------------------------------------------------------------===//
3511// FPToUIInst Class
3512//===----------------------------------------------------------------------===//
3513
3514/// @brief This class represents a cast from floating point to unsigned integer
3515class FPToUIInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003516protected:
3517 /// @brief Clone an identical FPToUIInst
3518 virtual FPToUIInst *clone_impl() const;
3519
Reid Spencer3da59db2006-11-27 01:05:10 +00003520public:
3521 /// @brief Constructor with insert-before-instruction semantics
3522 FPToUIInst(
3523 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003524 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003525 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003526 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3527 );
3528
3529 /// @brief Constructor with insert-at-end-of-block semantics
3530 FPToUIInst(
3531 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003532 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003533 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003534 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3535 );
3536
Reid Spencer3da59db2006-11-27 01:05:10 +00003537 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003538 static inline bool classof(const Instruction *I) {
3539 return I->getOpcode() == FPToUI;
3540 }
3541 static inline bool classof(const Value *V) {
3542 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3543 }
3544};
3545
3546//===----------------------------------------------------------------------===//
3547// FPToSIInst Class
3548//===----------------------------------------------------------------------===//
3549
3550/// @brief This class represents a cast from floating point to signed integer.
3551class FPToSIInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003552protected:
3553 /// @brief Clone an identical FPToSIInst
3554 virtual FPToSIInst *clone_impl() const;
3555
Reid Spencer3da59db2006-11-27 01:05:10 +00003556public:
3557 /// @brief Constructor with insert-before-instruction semantics
3558 FPToSIInst(
3559 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003560 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003561 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003562 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3563 );
3564
3565 /// @brief Constructor with insert-at-end-of-block semantics
3566 FPToSIInst(
3567 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003568 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003569 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003570 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3571 );
3572
Reid Spencer3da59db2006-11-27 01:05:10 +00003573 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003574 static inline bool classof(const Instruction *I) {
3575 return I->getOpcode() == FPToSI;
3576 }
3577 static inline bool classof(const Value *V) {
3578 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3579 }
3580};
3581
3582//===----------------------------------------------------------------------===//
3583// IntToPtrInst Class
3584//===----------------------------------------------------------------------===//
3585
3586/// @brief This class represents a cast from an integer to a pointer.
3587class IntToPtrInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00003588public:
3589 /// @brief Constructor with insert-before-instruction semantics
3590 IntToPtrInst(
3591 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003592 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003593 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003594 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3595 );
3596
3597 /// @brief Constructor with insert-at-end-of-block semantics
3598 IntToPtrInst(
3599 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003600 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003601 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003602 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3603 );
3604
3605 /// @brief Clone an identical IntToPtrInst
Devang Patel50b6e332009-10-27 22:16:29 +00003606 virtual IntToPtrInst *clone_impl() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00003607
Micah Villmow63b8ab22012-10-09 22:27:29 +00003608 /// @brief return the address space of the pointer.
3609 unsigned getAddressSpace() const {
Chandler Carruth8fb614c2012-11-01 09:37:49 +00003610 return getType()->getPointerAddressSpace();
Micah Villmow63b8ab22012-10-09 22:27:29 +00003611 }
3612
Reid Spencer3da59db2006-11-27 01:05:10 +00003613 // Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003614 static inline bool classof(const Instruction *I) {
3615 return I->getOpcode() == IntToPtr;
3616 }
3617 static inline bool classof(const Value *V) {
3618 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3619 }
3620};
3621
3622//===----------------------------------------------------------------------===//
3623// PtrToIntInst Class
3624//===----------------------------------------------------------------------===//
3625
3626/// @brief This class represents a cast from a pointer to an integer
3627class PtrToIntInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003628protected:
3629 /// @brief Clone an identical PtrToIntInst
3630 virtual PtrToIntInst *clone_impl() const;
3631
Reid Spencer3da59db2006-11-27 01:05:10 +00003632public:
3633 /// @brief Constructor with insert-before-instruction semantics
3634 PtrToIntInst(
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 PtrToIntInst(
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
Micah Villmow63b8ab22012-10-09 22:27:29 +00003649 /// @brief return the address space of the pointer.
3650 unsigned getPointerAddressSpace() const {
Chandler Carruth8fb614c2012-11-01 09:37:49 +00003651 return getOperand(0)->getType()->getPointerAddressSpace();
Micah Villmow63b8ab22012-10-09 22:27:29 +00003652 }
3653
Reid Spencer3da59db2006-11-27 01:05:10 +00003654 // Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003655 static inline bool classof(const Instruction *I) {
3656 return I->getOpcode() == PtrToInt;
3657 }
3658 static inline bool classof(const Value *V) {
3659 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3660 }
3661};
3662
3663//===----------------------------------------------------------------------===//
3664// BitCastInst Class
3665//===----------------------------------------------------------------------===//
3666
3667/// @brief This class represents a no-op cast from one type to another.
3668class BitCastInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003669protected:
3670 /// @brief Clone an identical BitCastInst
3671 virtual BitCastInst *clone_impl() const;
3672
Reid Spencer3da59db2006-11-27 01:05:10 +00003673public:
3674 /// @brief Constructor with insert-before-instruction semantics
3675 BitCastInst(
3676 Value *S, ///< The value to be casted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003677 Type *Ty, ///< The type to casted 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 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3680 );
3681
3682 /// @brief Constructor with insert-at-end-of-block semantics
3683 BitCastInst(
3684 Value *S, ///< The value to be casted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003685 Type *Ty, ///< The type to casted to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003686 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003687 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3688 );
3689
Reid Spencer3da59db2006-11-27 01:05:10 +00003690 // Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003691 static inline bool classof(const Instruction *I) {
3692 return I->getOpcode() == BitCast;
3693 }
3694 static inline bool classof(const Value *V) {
3695 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3696 }
3697};
3698
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003699} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00003700
3701#endif