blob: 13ed8c1f6ed393663b4e2722f7c781228dce0660 [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"
Jay Foadfc6d3a42011-07-13 10:26:04 +000023#include "llvm/ADT/ArrayRef.h"
Dan Gohman81a0c0b2008-05-31 00:58:22 +000024#include "llvm/ADT/SmallVector.h"
Eli Friedman47f35132011-07-25 23:16:38 +000025#include "llvm/Support/ErrorHandling.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000026#include <iterator>
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000027
28namespace llvm {
29
Chris Lattnerd1a32602005-02-24 05:32:09 +000030class ConstantInt;
Reid Spencer3da43842007-02-28 22:00:54 +000031class ConstantRange;
32class APInt;
Benjamin Kramer12ddd402009-08-11 17:45:13 +000033class LLVMContext;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000034
Eli Friedman47f35132011-07-25 23:16:38 +000035enum AtomicOrdering {
36 NotAtomic = 0,
37 Unordered = 1,
38 Monotonic = 2,
39 // Consume = 3, // Not specified yet.
40 Acquire = 4,
41 Release = 5,
42 AcquireRelease = 6,
43 SequentiallyConsistent = 7
44};
45
46enum SynchronizationScope {
47 SingleThread = 0,
48 CrossThread = 1
49};
50
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000051//===----------------------------------------------------------------------===//
Victor Hernandez7b929da2009-10-23 21:09:37 +000052// AllocaInst Class
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000053//===----------------------------------------------------------------------===//
54
Victor Hernandez7b929da2009-10-23 21:09:37 +000055/// AllocaInst - an instruction to allocate memory on the stack
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000056///
Victor Hernandez7b929da2009-10-23 21:09:37 +000057class AllocaInst : public UnaryInstruction {
Devang Patel50b6e332009-10-27 22:16:29 +000058protected:
59 virtual AllocaInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000060public:
Chris Lattnerdb125cf2011-07-18 04:54:35 +000061 explicit AllocaInst(Type *Ty, Value *ArraySize = 0,
Victor Hernandez7b929da2009-10-23 21:09:37 +000062 const Twine &Name = "", Instruction *InsertBefore = 0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000063 AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez7b929da2009-10-23 21:09:37 +000064 const Twine &Name, BasicBlock *InsertAtEnd);
65
Chris Lattnerdb125cf2011-07-18 04:54:35 +000066 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
67 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
Victor Hernandez7b929da2009-10-23 21:09:37 +000068
Chris Lattnerdb125cf2011-07-18 04:54:35 +000069 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez7b929da2009-10-23 21:09:37 +000070 const Twine &Name = "", Instruction *InsertBefore = 0);
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, BasicBlock *InsertAtEnd);
73
Gabor Greif051a9502008-04-06 20:25:17 +000074 // Out of line virtual method, so the vtable, etc. has a home.
Victor Hernandez7b929da2009-10-23 21:09:37 +000075 virtual ~AllocaInst();
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000076
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000077 /// isArrayAllocation - Return true if there is an allocation size parameter
78 /// to the allocation instruction that is not 1.
79 ///
80 bool isArrayAllocation() const;
81
Dan Gohman18476ee2009-07-07 20:47:48 +000082 /// getArraySize - Get the number of elements allocated. For a simple
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000083 /// allocation of a single element, this will return a constant 1 value.
84 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000085 const Value *getArraySize() const { return getOperand(0); }
86 Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000087
88 /// getType - Overload to return most specific pointer type
89 ///
Chris Lattnerdb125cf2011-07-18 04:54:35 +000090 PointerType *getType() const {
91 return reinterpret_cast<PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000092 }
93
94 /// getAllocatedType - Return the type that is being allocated by the
95 /// instruction.
96 ///
Chris Lattner1afcace2011-07-09 17:41:24 +000097 Type *getAllocatedType() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000098
Nate Begeman14b05292005-11-05 09:21:28 +000099 /// getAlignment - Return the alignment of the memory that is being allocated
100 /// by the instruction.
101 ///
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000102 unsigned getAlignment() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000103 return (1u << getSubclassDataFromInstruction()) >> 1;
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000104 }
Dan Gohman52837072008-03-24 16:55:58 +0000105 void setAlignment(unsigned Align);
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000106
Chris Lattnerc5dd22a2008-11-26 02:54:17 +0000107 /// isStaticAlloca - Return true if this alloca is in the entry block of the
108 /// function and is a constant size. If so, the code generator will fold it
109 /// into the prolog/epilog code, so it is basically free.
110 bool isStaticAlloca() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000111
112 // Methods for support type inquiry through isa, cast, and dyn_cast:
113 static inline bool classof(const AllocaInst *) { return true; }
114 static inline bool classof(const Instruction *I) {
115 return (I->getOpcode() == Instruction::Alloca);
116 }
117 static inline bool classof(const Value *V) {
118 return isa<Instruction>(V) && classof(cast<Instruction>(V));
119 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000120private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000121 // Shadow Instruction::setInstructionSubclassData with a private forwarding
122 // method so that subclasses cannot accidentally use it.
123 void setInstructionSubclassData(unsigned short D) {
124 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000125 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000126};
127
128
129//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000130// LoadInst Class
131//===----------------------------------------------------------------------===//
132
Chris Lattner88fe29a2005-02-05 01:44:18 +0000133/// LoadInst - an instruction for reading from memory. This uses the
134/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000135///
Chris Lattner454928e2005-01-29 00:31:36 +0000136class LoadInst : public UnaryInstruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000137 void AssertOK();
Devang Patel50b6e332009-10-27 22:16:29 +0000138protected:
139 virtual LoadInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000140public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000141 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
142 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000143 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
Christopher Lamb43c7f372007-04-22 19:24:39 +0000144 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000145 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000146 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000147 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Eli Friedman21006d42011-08-09 23:02:53 +0000148 unsigned Align, Instruction *InsertBefore = 0);
149 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000150 unsigned Align, BasicBlock *InsertAtEnd);
Eli Friedman21006d42011-08-09 23:02:53 +0000151 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
152 unsigned Align, AtomicOrdering Order,
153 SynchronizationScope SynchScope = CrossThread,
154 Instruction *InsertBefore = 0);
155 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
156 unsigned Align, AtomicOrdering Order,
157 SynchronizationScope SynchScope,
158 BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000159
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000160 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
161 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
162 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
163 bool isVolatile = false, Instruction *InsertBefore = 0);
164 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
165 BasicBlock *InsertAtEnd);
166
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000167 /// isVolatile - Return true if this is a load from a volatile memory
168 /// location.
169 ///
Chris Lattnerb2406d92009-12-29 02:46:09 +0000170 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000171
172 /// setVolatile - Specify whether this is a volatile load or not.
173 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000174 void setVolatile(bool V) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000175 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
176 (V ? 1 : 0));
Christopher Lamb43c7f372007-04-22 19:24:39 +0000177 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000178
Christopher Lamb43c7f372007-04-22 19:24:39 +0000179 /// getAlignment - Return the alignment of the access that is being performed
180 ///
181 unsigned getAlignment() const {
Eli Friedman21006d42011-08-09 23:02:53 +0000182 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000183 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000184
Christopher Lamb43c7f372007-04-22 19:24:39 +0000185 void setAlignment(unsigned Align);
186
Eli Friedman21006d42011-08-09 23:02:53 +0000187 /// Returns the ordering effect of this fence.
188 AtomicOrdering getOrdering() const {
189 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
190 }
191
192 /// Set the ordering constraint on this load. May not be Release or
193 /// AcquireRelease.
194 void setOrdering(AtomicOrdering Ordering) {
195 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
196 (Ordering << 7));
197 }
198
199 SynchronizationScope getSynchScope() const {
200 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
201 }
202
203 /// Specify whether this load is ordered with respect to all
204 /// concurrently executing threads, or only with respect to signal handlers
205 /// executing in the same thread.
206 void setSynchScope(SynchronizationScope xthread) {
207 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
208 (xthread << 6));
209 }
210
211 bool isAtomic() const { return getOrdering() != NotAtomic; }
212 void setAtomic(AtomicOrdering Ordering,
213 SynchronizationScope SynchScope = CrossThread) {
214 setOrdering(Ordering);
215 setSynchScope(SynchScope);
216 }
217
218 bool isSimple() const { return !isAtomic() && !isVolatile(); }
219 bool isUnordered() const {
220 return getOrdering() <= Unordered && !isVolatile();
221 }
222
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000223 Value *getPointerOperand() { return getOperand(0); }
224 const Value *getPointerOperand() const { return getOperand(0); }
225 static unsigned getPointerOperandIndex() { return 0U; }
226
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000227 unsigned getPointerAddressSpace() const {
228 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
229 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000230
231
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000232 // Methods for support type inquiry through isa, cast, and dyn_cast:
233 static inline bool classof(const LoadInst *) { return true; }
234 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 {
Gabor Greif051a9502008-04-06 20:25:17 +0000256 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
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 {
351 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
352 }
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:
355 static inline bool classof(const StoreInst *) { return true; }
356 static inline bool classof(const Instruction *I) {
357 return I->getOpcode() == Instruction::Store;
358 }
359 static inline bool classof(const Value *V) {
360 return isa<Instruction>(V) && classof(cast<Instruction>(V));
361 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000362private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000363 // Shadow Instruction::setInstructionSubclassData with a private forwarding
364 // method so that subclasses cannot accidentally use it.
365 void setInstructionSubclassData(unsigned short D) {
366 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000367 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000368};
369
Gabor Greifefe65362008-05-10 08:32:32 +0000370template <>
Jay Foad67c619b2011-01-11 15:07:38 +0000371struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
Gabor Greifefe65362008-05-10 08:32:32 +0000372};
373
374DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000375
376//===----------------------------------------------------------------------===//
Eli Friedman47f35132011-07-25 23:16:38 +0000377// FenceInst Class
378//===----------------------------------------------------------------------===//
379
380/// FenceInst - an instruction for ordering other memory operations
381///
382class FenceInst : public Instruction {
383 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
384 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
385protected:
386 virtual FenceInst *clone_impl() const;
387public:
388 // allocate space for exactly zero operands
389 void *operator new(size_t s) {
390 return User::operator new(s, 0);
391 }
392
393 // Ordering may only be Acquire, Release, AcquireRelease, or
394 // SequentiallyConsistent.
395 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
396 SynchronizationScope SynchScope = CrossThread,
397 Instruction *InsertBefore = 0);
398 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
399 SynchronizationScope SynchScope,
400 BasicBlock *InsertAtEnd);
401
402 /// Returns the ordering effect of this fence.
403 AtomicOrdering getOrdering() const {
404 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
405 }
406
407 /// Set the ordering constraint on this fence. May only be Acquire, Release,
408 /// AcquireRelease, or SequentiallyConsistent.
409 void setOrdering(AtomicOrdering Ordering) {
Eli Friedman21006d42011-08-09 23:02:53 +0000410 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
411 (Ordering << 1));
Eli Friedman47f35132011-07-25 23:16:38 +0000412 }
413
414 SynchronizationScope getSynchScope() const {
415 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
416 }
417
418 /// Specify whether this fence orders other operations with respect to all
419 /// concurrently executing threads, or only with respect to signal handlers
420 /// executing in the same thread.
421 void setSynchScope(SynchronizationScope xthread) {
422 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
423 xthread);
424 }
425
426 // Methods for support type inquiry through isa, cast, and dyn_cast:
427 static inline bool classof(const FenceInst *) { return true; }
428 static inline bool classof(const Instruction *I) {
429 return I->getOpcode() == Instruction::Fence;
430 }
431 static inline bool classof(const Value *V) {
432 return isa<Instruction>(V) && classof(cast<Instruction>(V));
433 }
434private:
435 // Shadow Instruction::setInstructionSubclassData with a private forwarding
436 // method so that subclasses cannot accidentally use it.
437 void setInstructionSubclassData(unsigned short D) {
438 Instruction::setInstructionSubclassData(D);
439 }
440};
441
442//===----------------------------------------------------------------------===//
Eli Friedmanff030482011-07-28 21:48:00 +0000443// AtomicCmpXchgInst Class
444//===----------------------------------------------------------------------===//
445
446/// AtomicCmpXchgInst - an instruction that atomically checks whether a
447/// specified value is in a memory location, and, if it is, stores a new value
448/// there. Returns the value that was loaded.
449///
450class AtomicCmpXchgInst : public Instruction {
451 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
452 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
453 AtomicOrdering Ordering, SynchronizationScope SynchScope);
454protected:
455 virtual AtomicCmpXchgInst *clone_impl() const;
456public:
457 // allocate space for exactly three operands
458 void *operator new(size_t s) {
459 return User::operator new(s, 3);
460 }
461 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
462 AtomicOrdering Ordering, SynchronizationScope SynchScope,
463 Instruction *InsertBefore = 0);
464 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
465 AtomicOrdering Ordering, SynchronizationScope SynchScope,
466 BasicBlock *InsertAtEnd);
467
468 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
469 /// location.
470 ///
471 bool isVolatile() const {
472 return getSubclassDataFromInstruction() & 1;
473 }
474
475 /// setVolatile - Specify whether this is a volatile cmpxchg.
476 ///
477 void setVolatile(bool V) {
478 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
479 (unsigned)V);
480 }
481
482 /// Transparently provide more efficient getOperand methods.
483 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
484
485 /// Set the ordering constraint on this cmpxchg.
486 void setOrdering(AtomicOrdering Ordering) {
487 assert(Ordering != NotAtomic &&
488 "CmpXchg instructions can only be atomic.");
489 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
490 (Ordering << 2));
491 }
492
493 /// Specify whether this cmpxchg is atomic and orders other operations with
494 /// respect to all concurrently executing threads, or only with respect to
495 /// signal handlers executing in the same thread.
496 void setSynchScope(SynchronizationScope SynchScope) {
497 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
498 (SynchScope << 1));
499 }
500
501 /// Returns the ordering constraint on this cmpxchg.
502 AtomicOrdering getOrdering() const {
503 return AtomicOrdering(getSubclassDataFromInstruction() >> 2);
504 }
505
506 /// Returns whether this cmpxchg is atomic between threads or only within a
507 /// single thread.
508 SynchronizationScope getSynchScope() const {
509 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
510 }
511
512 Value *getPointerOperand() { return getOperand(0); }
513 const Value *getPointerOperand() const { return getOperand(0); }
514 static unsigned getPointerOperandIndex() { return 0U; }
515
516 Value *getCompareOperand() { return getOperand(1); }
517 const Value *getCompareOperand() const { return getOperand(1); }
518
519 Value *getNewValOperand() { return getOperand(2); }
520 const Value *getNewValOperand() const { return getOperand(2); }
521
522 unsigned getPointerAddressSpace() const {
523 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
524 }
525
526 // Methods for support type inquiry through isa, cast, and dyn_cast:
527 static inline bool classof(const AtomicCmpXchgInst *) { return true; }
528 static inline bool classof(const Instruction *I) {
529 return I->getOpcode() == Instruction::AtomicCmpXchg;
530 }
531 static inline bool classof(const Value *V) {
532 return isa<Instruction>(V) && classof(cast<Instruction>(V));
533 }
534private:
535 // Shadow Instruction::setInstructionSubclassData with a private forwarding
536 // method so that subclasses cannot accidentally use it.
537 void setInstructionSubclassData(unsigned short D) {
538 Instruction::setInstructionSubclassData(D);
539 }
540};
541
542template <>
543struct OperandTraits<AtomicCmpXchgInst> :
544 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
545};
546
547DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
548
549//===----------------------------------------------------------------------===//
550// AtomicRMWInst Class
551//===----------------------------------------------------------------------===//
552
553/// AtomicRMWInst - an instruction that atomically reads a memory location,
554/// combines it with another value, and then stores the result back. Returns
555/// the old value.
556///
557class AtomicRMWInst : public Instruction {
558 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
559protected:
560 virtual AtomicRMWInst *clone_impl() const;
561public:
562 /// This enumeration lists the possible modifications atomicrmw can make. In
563 /// the descriptions, 'p' is the pointer to the instruction's memory location,
564 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
565 /// instruction. These instructions always return 'old'.
566 enum BinOp {
567 /// *p = v
568 Xchg,
569 /// *p = old + v
570 Add,
571 /// *p = old - v
572 Sub,
573 /// *p = old & v
574 And,
575 /// *p = ~old & v
576 Nand,
577 /// *p = old | v
578 Or,
579 /// *p = old ^ v
580 Xor,
581 /// *p = old >signed v ? old : v
582 Max,
583 /// *p = old <signed v ? old : v
584 Min,
585 /// *p = old >unsigned v ? old : v
586 UMax,
587 /// *p = old <unsigned v ? old : v
588 UMin,
589
590 FIRST_BINOP = Xchg,
591 LAST_BINOP = UMin,
592 BAD_BINOP
593 };
594
595 // allocate space for exactly two operands
596 void *operator new(size_t s) {
597 return User::operator new(s, 2);
598 }
599 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
600 AtomicOrdering Ordering, SynchronizationScope SynchScope,
601 Instruction *InsertBefore = 0);
602 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
603 AtomicOrdering Ordering, SynchronizationScope SynchScope,
604 BasicBlock *InsertAtEnd);
605
606 BinOp getOperation() const {
607 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
608 }
609
610 void setOperation(BinOp Operation) {
611 unsigned short SubclassData = getSubclassDataFromInstruction();
612 setInstructionSubclassData((SubclassData & 31) |
613 (Operation << 5));
614 }
615
616 /// isVolatile - Return true if this is a RMW on a volatile memory location.
617 ///
618 bool isVolatile() const {
619 return getSubclassDataFromInstruction() & 1;
620 }
621
622 /// setVolatile - Specify whether this is a volatile RMW or not.
623 ///
624 void setVolatile(bool V) {
625 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
626 (unsigned)V);
627 }
628
629 /// Transparently provide more efficient getOperand methods.
630 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
631
632 /// Set the ordering constraint on this RMW.
633 void setOrdering(AtomicOrdering Ordering) {
634 assert(Ordering != NotAtomic &&
635 "atomicrmw instructions can only be atomic.");
Eli Friedman21006d42011-08-09 23:02:53 +0000636 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
Eli Friedmanff030482011-07-28 21:48:00 +0000637 (Ordering << 2));
638 }
639
640 /// Specify whether this RMW orders other operations with respect to all
641 /// concurrently executing threads, or only with respect to signal handlers
642 /// executing in the same thread.
643 void setSynchScope(SynchronizationScope SynchScope) {
644 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
645 (SynchScope << 1));
646 }
647
648 /// Returns the ordering constraint on this RMW.
649 AtomicOrdering getOrdering() const {
Eli Friedman21006d42011-08-09 23:02:53 +0000650 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
Eli Friedmanff030482011-07-28 21:48:00 +0000651 }
652
653 /// Returns whether this RMW is atomic between threads or only within a
654 /// single thread.
655 SynchronizationScope getSynchScope() const {
656 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
657 }
658
659 Value *getPointerOperand() { return getOperand(0); }
660 const Value *getPointerOperand() const { return getOperand(0); }
661 static unsigned getPointerOperandIndex() { return 0U; }
662
663 Value *getValOperand() { return getOperand(1); }
664 const Value *getValOperand() const { return getOperand(1); }
665
666 unsigned getPointerAddressSpace() const {
667 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
668 }
669
670 // Methods for support type inquiry through isa, cast, and dyn_cast:
671 static inline bool classof(const AtomicRMWInst *) { return true; }
672 static inline bool classof(const Instruction *I) {
673 return I->getOpcode() == Instruction::AtomicRMW;
674 }
675 static inline bool classof(const Value *V) {
676 return isa<Instruction>(V) && classof(cast<Instruction>(V));
677 }
678private:
679 void Init(BinOp Operation, Value *Ptr, Value *Val,
680 AtomicOrdering Ordering, SynchronizationScope SynchScope);
681 // Shadow Instruction::setInstructionSubclassData with a private forwarding
682 // method so that subclasses cannot accidentally use it.
683 void setInstructionSubclassData(unsigned short D) {
684 Instruction::setInstructionSubclassData(D);
685 }
686};
687
688template <>
689struct OperandTraits<AtomicRMWInst>
690 : public FixedNumOperandTraits<AtomicRMWInst,2> {
691};
692
693DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
694
695//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000696// GetElementPtrInst Class
697//===----------------------------------------------------------------------===//
698
Chris Lattner1afcace2011-07-09 17:41:24 +0000699// checkGEPType - Simple wrapper function to give a better assertion failure
David Greeneb8f74792007-09-04 15:46:09 +0000700// message on bad indexes for a gep instruction.
701//
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000702static inline Type *checkGEPType(Type *Ty) {
David Greeneb8f74792007-09-04 15:46:09 +0000703 assert(Ty && "Invalid GetElementPtrInst indices for type!");
704 return Ty;
705}
706
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000707/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
708/// access elements of arrays and structs
709///
710class GetElementPtrInst : public Instruction {
Gabor Greifefe65362008-05-10 08:32:32 +0000711 GetElementPtrInst(const GetElementPtrInst &GEPI);
Jay Foada9203102011-07-25 09:48:08 +0000712 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000713
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000714 /// Constructors - Create a getelementptr instruction with a base pointer an
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000715 /// list of indices. The first ctor can optionally insert before an existing
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000716 /// instruction, the second appends the new instruction to the specified
717 /// BasicBlock.
Jay Foada9203102011-07-25 09:48:08 +0000718 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
719 unsigned Values, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000720 Instruction *InsertBefore);
Jay Foada9203102011-07-25 09:48:08 +0000721 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
722 unsigned Values, const Twine &NameStr,
723 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +0000724protected:
725 virtual GetElementPtrInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +0000726public:
Jay Foada9203102011-07-25 09:48:08 +0000727 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000728 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000729 Instruction *InsertBefore = 0) {
Jay Foada9203102011-07-25 09:48:08 +0000730 unsigned Values = 1 + unsigned(IdxList.size());
Gabor Greifefe65362008-05-10 08:32:32 +0000731 return new(Values)
Jay Foada9203102011-07-25 09:48:08 +0000732 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000733 }
Jay Foada9203102011-07-25 09:48:08 +0000734 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000735 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000736 BasicBlock *InsertAtEnd) {
Jay Foada9203102011-07-25 09:48:08 +0000737 unsigned Values = 1 + unsigned(IdxList.size());
Gabor Greifefe65362008-05-10 08:32:32 +0000738 return new(Values)
Jay Foada9203102011-07-25 09:48:08 +0000739 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000740 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000741
Dan Gohmane2574d32009-08-11 17:57:01 +0000742 /// Create an "inbounds" getelementptr. See the documentation for the
743 /// "inbounds" flag in LangRef.html for details.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000744 static GetElementPtrInst *CreateInBounds(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000745 ArrayRef<Value *> IdxList,
Dan Gohmane2574d32009-08-11 17:57:01 +0000746 const Twine &NameStr = "",
747 Instruction *InsertBefore = 0) {
Jay Foada9203102011-07-25 09:48:08 +0000748 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000749 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000750 return GEP;
751 }
Dan Gohmane2574d32009-08-11 17:57:01 +0000752 static GetElementPtrInst *CreateInBounds(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000753 ArrayRef<Value *> IdxList,
Dan Gohmane2574d32009-08-11 17:57:01 +0000754 const Twine &NameStr,
755 BasicBlock *InsertAtEnd) {
Jay Foada9203102011-07-25 09:48:08 +0000756 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000757 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000758 return GEP;
759 }
760
Gabor Greifefe65362008-05-10 08:32:32 +0000761 /// Transparently provide more efficient getOperand methods.
762 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
763
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000764 // getType - Overload to return most specific pointer type...
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000765 PointerType *getType() const {
766 return reinterpret_cast<PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000767 }
768
769 /// getIndexedType - Returns the type of the element that would be loaded with
770 /// a load instruction with the specified parameters.
771 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000772 /// Null is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000773 /// pointer type.
774 ///
Jay Foada9203102011-07-25 09:48:08 +0000775 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
776 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
777 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
Misha Brukman9769ab22005-04-21 20:19:05 +0000778
Nadav Rotem16087692011-12-05 06:29:09 +0000779 /// getIndexedType - Returns the address space used by the GEP pointer.
780 ///
781 static unsigned getAddressSpace(Value *Ptr);
782
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000783 inline op_iterator idx_begin() { return op_begin()+1; }
784 inline const_op_iterator idx_begin() const { return op_begin()+1; }
785 inline op_iterator idx_end() { return op_end(); }
786 inline const_op_iterator idx_end() const { return op_end(); }
787
788 Value *getPointerOperand() {
789 return getOperand(0);
790 }
791 const Value *getPointerOperand() const {
792 return getOperand(0);
793 }
794 static unsigned getPointerOperandIndex() {
Nadav Rotem16087692011-12-05 06:29:09 +0000795 return 0U; // get index for modifying correct operand.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000796 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000797
Chris Lattner8a67ac52009-08-30 20:06:40 +0000798 unsigned getPointerAddressSpace() const {
799 return cast<PointerType>(getType())->getAddressSpace();
800 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000801
Chris Lattner3bc6ced2009-01-09 05:27:40 +0000802 /// getPointerOperandType - Method to return the pointer operand as a
803 /// PointerType.
Nadav Rotem16087692011-12-05 06:29:09 +0000804 Type *getPointerOperandType() const {
805 return getPointerOperand()->getType();
Chris Lattner3bc6ced2009-01-09 05:27:40 +0000806 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000807
Nadav Rotem16087692011-12-05 06:29:09 +0000808 /// GetGEPReturnType - Returns the pointer type returned by the GEP
809 /// instruction, which may be a vector of pointers.
810 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
811 Type *PtrTy = PointerType::get(checkGEPType(
812 getIndexedType(Ptr->getType(), IdxList)),
813 getAddressSpace(Ptr));
814 // Vector GEP
815 if (Ptr->getType()->isVectorTy()) {
816 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
817 return VectorType::get(PtrTy, NumElem);
818 }
819
820 // Scalar GEP
821 return PtrTy;
822 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000823
Devang Patel4d4a5e02008-02-23 01:11:02 +0000824 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000825 return getNumOperands() - 1;
826 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000827
Devang Patel4d4a5e02008-02-23 01:11:02 +0000828 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000829 return getNumOperands() > 1;
830 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000831
Chris Lattner6f771d42007-04-14 00:12:57 +0000832 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
833 /// zeros. If so, the result pointer and the first operand have the same
834 /// value, just potentially different types.
835 bool hasAllZeroIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000836
Chris Lattner6b0974c2007-04-27 20:35:56 +0000837 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
838 /// constant integers. If so, the result pointer and the first operand have
839 /// a constant offset between them.
840 bool hasAllConstantIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000841
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000842 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
843 /// See LangRef.html for the meaning of inbounds on a getelementptr.
Nick Lewyckyae05e7d2009-09-27 21:33:04 +0000844 void setIsInBounds(bool b = true);
845
846 /// isInBounds - Determine whether the GEP has the inbounds flag.
847 bool isInBounds() const;
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000848
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000849 // Methods for support type inquiry through isa, cast, and dyn_cast:
850 static inline bool classof(const GetElementPtrInst *) { return true; }
851 static inline bool classof(const Instruction *I) {
852 return (I->getOpcode() == Instruction::GetElementPtr);
853 }
854 static inline bool classof(const Value *V) {
855 return isa<Instruction>(V) && classof(cast<Instruction>(V));
856 }
857};
858
Gabor Greifefe65362008-05-10 08:32:32 +0000859template <>
Jay Foad67c619b2011-01-11 15:07:38 +0000860struct OperandTraits<GetElementPtrInst> :
861 public VariadicOperandTraits<GetElementPtrInst, 1> {
Gabor Greifefe65362008-05-10 08:32:32 +0000862};
863
Gabor Greifefe65362008-05-10 08:32:32 +0000864GetElementPtrInst::GetElementPtrInst(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000865 ArrayRef<Value *> IdxList,
Gabor Greifefe65362008-05-10 08:32:32 +0000866 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000867 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000868 Instruction *InsertBefore)
Nadav Rotem16087692011-12-05 06:29:09 +0000869 : Instruction(getGEPReturnType(Ptr, IdxList),
Gabor Greifefe65362008-05-10 08:32:32 +0000870 GetElementPtr,
871 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
872 Values, InsertBefore) {
Jay Foada9203102011-07-25 09:48:08 +0000873 init(Ptr, IdxList, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +0000874}
Gabor Greifefe65362008-05-10 08:32:32 +0000875GetElementPtrInst::GetElementPtrInst(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000876 ArrayRef<Value *> IdxList,
Gabor Greifefe65362008-05-10 08:32:32 +0000877 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000878 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000879 BasicBlock *InsertAtEnd)
Nadav Rotem16087692011-12-05 06:29:09 +0000880 : Instruction(getGEPReturnType(Ptr, IdxList),
Gabor Greifefe65362008-05-10 08:32:32 +0000881 GetElementPtr,
882 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
883 Values, InsertAtEnd) {
Jay Foada9203102011-07-25 09:48:08 +0000884 init(Ptr, IdxList, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +0000885}
886
887
888DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
889
890
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000891//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000892// ICmpInst Class
893//===----------------------------------------------------------------------===//
894
895/// This instruction compares its operands according to the predicate given
Nate Begemanac80ade2008-05-12 19:01:56 +0000896/// to the constructor. It only operates on integers or pointers. The operands
897/// must be identical types.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000898/// @brief Represent an integer comparison operator.
899class ICmpInst: public CmpInst {
Devang Patel50b6e332009-10-27 22:16:29 +0000900protected:
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000901 /// @brief Clone an identical ICmpInst
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000902 virtual ICmpInst *clone_impl() const;
Reid Spencer45fb3f32006-11-20 01:22:35 +0000903public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000904 /// @brief Constructor with insert-before-instruction semantics.
905 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000906 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +0000907 Predicate pred, ///< The predicate to use for the comparison
908 Value *LHS, ///< The left-hand-side of the expression
909 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000910 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000911 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +0000912 Instruction::ICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000913 InsertBefore) {
914 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
915 pred <= CmpInst::LAST_ICMP_PREDICATE &&
916 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000917 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000918 "Both operands to ICmp instruction are not of the same type!");
919 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000920 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Nadav Rotem16087692011-12-05 06:29:09 +0000921 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000922 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000923 }
924
Owen Anderson333c4002009-07-09 23:48:35 +0000925 /// @brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000926 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000927 BasicBlock &InsertAtEnd, ///< Block to insert into.
928 Predicate pred, ///< The predicate to use for the comparison
929 Value *LHS, ///< The left-hand-side of the expression
930 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000931 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000932 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000933 Instruction::ICmp, pred, LHS, RHS, NameStr,
934 &InsertAtEnd) {
935 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
936 pred <= CmpInst::LAST_ICMP_PREDICATE &&
937 "Invalid ICmp predicate value");
938 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
939 "Both operands to ICmp instruction are not of the same type!");
940 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000941 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000942 getOperand(0)->getType()->isPointerTy()) &&
Owen Anderson333c4002009-07-09 23:48:35 +0000943 "Invalid operand types for ICmp instruction");
944 }
945
946 /// @brief Constructor with no-insertion semantics
947 ICmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +0000948 Predicate pred, ///< The predicate to use for the comparison
949 Value *LHS, ///< The left-hand-side of the expression
950 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000951 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000952 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000953 Instruction::ICmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +0000954 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
955 pred <= CmpInst::LAST_ICMP_PREDICATE &&
956 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000957 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000958 "Both operands to ICmp instruction are not of the same type!");
959 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000960 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Nadav Rotem16087692011-12-05 06:29:09 +0000961 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000962 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000963 }
964
Reid Spencere4d87aa2006-12-23 06:05:41 +0000965 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
966 /// @returns the predicate that would be the result if the operand were
967 /// regarded as signed.
968 /// @brief Return the signed version of the predicate
969 Predicate getSignedPredicate() const {
970 return getSignedPredicate(getPredicate());
971 }
972
973 /// This is a static version that you can use without an instruction.
974 /// @brief Return the signed version of the predicate.
975 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000976
Nick Lewycky4189a532008-01-28 03:48:02 +0000977 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
978 /// @returns the predicate that would be the result if the operand were
979 /// regarded as unsigned.
980 /// @brief Return the unsigned version of the predicate
981 Predicate getUnsignedPredicate() const {
982 return getUnsignedPredicate(getPredicate());
983 }
984
985 /// This is a static version that you can use without an instruction.
986 /// @brief Return the unsigned version of the predicate.
987 static Predicate getUnsignedPredicate(Predicate pred);
988
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000989 /// isEquality - Return true if this predicate is either EQ or NE. This also
990 /// tests for commutativity.
991 static bool isEquality(Predicate P) {
992 return P == ICMP_EQ || P == ICMP_NE;
993 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000994
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000995 /// isEquality - Return true if this predicate is either EQ or NE. This also
996 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000997 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000998 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000999 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001000
1001 /// @returns true if the predicate of this ICmpInst is commutative
1002 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +00001003 bool isCommutative() const { return isEquality(); }
1004
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001005 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +00001006 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +00001007 bool isRelational() const {
1008 return !isEquality();
1009 }
1010
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001011 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +00001012 ///
1013 static bool isRelational(Predicate P) {
1014 return !isEquality(P);
1015 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001016
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001017 /// Initialize a set of values that all satisfy the predicate with C.
Reid Spencer3da43842007-02-28 22:00:54 +00001018 /// @brief Make a ConstantRange for a relation with a constant value.
1019 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1020
Reid Spencer45fb3f32006-11-20 01:22:35 +00001021 /// Exchange the two operands to this instruction in such a way that it does
1022 /// not modify the semantics of the instruction. The predicate value may be
1023 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001024 /// (e.g. ult).
Reid Spencer45fb3f32006-11-20 01:22:35 +00001025 /// @brief Swap operands and adjust predicate.
1026 void swapOperands() {
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001027 setPredicate(getSwappedPredicate());
Gabor Greif94fb68b2008-05-13 22:51:52 +00001028 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +00001029 }
1030
1031 // Methods for support type inquiry through isa, cast, and dyn_cast:
1032 static inline bool classof(const ICmpInst *) { return true; }
1033 static inline bool classof(const Instruction *I) {
1034 return I->getOpcode() == Instruction::ICmp;
1035 }
1036 static inline bool classof(const Value *V) {
1037 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1038 }
Dan Gohmanf72fb672008-09-09 01:02:47 +00001039
Reid Spencer45fb3f32006-11-20 01:22:35 +00001040};
1041
1042//===----------------------------------------------------------------------===//
1043// FCmpInst Class
1044//===----------------------------------------------------------------------===//
1045
1046/// This instruction compares its operands according to the predicate given
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001047/// to the constructor. It only operates on floating point values or packed
Reid Spencer45fb3f32006-11-20 01:22:35 +00001048/// vectors of floating point values. The operands must be identical types.
1049/// @brief Represents a floating point comparison operator.
1050class FCmpInst: public CmpInst {
Devang Patel50b6e332009-10-27 22:16:29 +00001051protected:
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001052 /// @brief Clone an identical FCmpInst
Devang Patel50b6e332009-10-27 22:16:29 +00001053 virtual FCmpInst *clone_impl() const;
Reid Spencer45fb3f32006-11-20 01:22:35 +00001054public:
Reid Spencer45fb3f32006-11-20 01:22:35 +00001055 /// @brief Constructor with insert-before-instruction semantics.
1056 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +00001057 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +00001058 Predicate pred, ///< The predicate to use for the comparison
1059 Value *LHS, ///< The left-hand-side of the expression
1060 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001061 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +00001062 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +00001063 Instruction::FCmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +00001064 InsertBefore) {
1065 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1066 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +00001067 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001068 "Both operands to FCmp instruction are not of the same type!");
1069 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001070 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001071 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +00001072 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001073
Owen Anderson333c4002009-07-09 23:48:35 +00001074 /// @brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +00001075 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +00001076 BasicBlock &InsertAtEnd, ///< Block to insert into.
1077 Predicate pred, ///< The predicate to use for the comparison
1078 Value *LHS, ///< The left-hand-side of the expression
1079 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001080 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +00001081 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +00001082 Instruction::FCmp, pred, LHS, RHS, NameStr,
1083 &InsertAtEnd) {
1084 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1085 "Invalid FCmp predicate value");
1086 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1087 "Both operands to FCmp instruction are not of the same type!");
1088 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001089 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Owen Anderson333c4002009-07-09 23:48:35 +00001090 "Invalid operand types for FCmp instruction");
1091 }
1092
1093 /// @brief Constructor with no-insertion semantics
1094 FCmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +00001095 Predicate pred, ///< The predicate to use for the comparison
1096 Value *LHS, ///< The left-hand-side of the expression
1097 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001098 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +00001099 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +00001100 Instruction::FCmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +00001101 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1102 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +00001103 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001104 "Both operands to FCmp instruction are not of the same type!");
1105 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001106 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001107 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +00001108 }
1109
Reid Spencer45fb3f32006-11-20 01:22:35 +00001110 /// @returns true if the predicate of this instruction is EQ or NE.
1111 /// @brief Determine if this is an equality predicate.
1112 bool isEquality() const {
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001113 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1114 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
Reid Spencer45fb3f32006-11-20 01:22:35 +00001115 }
Dan Gohman793df072008-09-16 16:44:00 +00001116
1117 /// @returns true if the predicate of this instruction is commutative.
1118 /// @brief Determine if this is a commutative predicate.
1119 bool isCommutative() const {
1120 return isEquality() ||
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001121 getPredicate() == FCMP_FALSE ||
1122 getPredicate() == FCMP_TRUE ||
1123 getPredicate() == FCMP_ORD ||
1124 getPredicate() == FCMP_UNO;
Dan Gohman793df072008-09-16 16:44:00 +00001125 }
Reid Spencer45fb3f32006-11-20 01:22:35 +00001126
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001127 /// @returns true if the predicate is relational (not EQ or NE).
Reid Spencer45fb3f32006-11-20 01:22:35 +00001128 /// @brief Determine if this a relational predicate.
1129 bool isRelational() const { return !isEquality(); }
1130
1131 /// Exchange the two operands to this instruction in such a way that it does
1132 /// not modify the semantics of the instruction. The predicate value may be
1133 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001134 /// (e.g. ult).
Reid Spencer45fb3f32006-11-20 01:22:35 +00001135 /// @brief Swap operands and adjust predicate.
1136 void swapOperands() {
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001137 setPredicate(getSwappedPredicate());
Gabor Greif94fb68b2008-05-13 22:51:52 +00001138 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +00001139 }
1140
1141 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1142 static inline bool classof(const FCmpInst *) { return true; }
1143 static inline bool classof(const Instruction *I) {
1144 return I->getOpcode() == Instruction::FCmp;
1145 }
1146 static inline bool classof(const Value *V) {
1147 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1148 }
1149};
1150
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001151//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001152/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +00001153/// machine's calling convention. This class uses low bit of the SubClassData
1154/// field to indicate whether or not this is a tail call. The rest of the bits
1155/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001156///
1157class CallInst : public Instruction {
Devang Patel05988662008-09-25 21:00:45 +00001158 AttrListPtr AttributeList; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001159 CallInst(const CallInst &CI);
Jay Foada3efbb12011-07-15 08:37:34 +00001160 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1161 void init(Value *Func, const Twine &NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001162
Jay Foada3efbb12011-07-15 08:37:34 +00001163 /// Construct a CallInst given a range of arguments.
David Greene52eec542007-08-01 03:43:44 +00001164 /// @brief Construct a CallInst from a range of arguments
Jay Foada3efbb12011-07-15 08:37:34 +00001165 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1166 const Twine &NameStr, Instruction *InsertBefore);
David Greene52eec542007-08-01 03:43:44 +00001167
Jay Foada3efbb12011-07-15 08:37:34 +00001168 /// Construct a CallInst given a range of arguments.
David Greene52eec542007-08-01 03:43:44 +00001169 /// @brief Construct a CallInst from a range of arguments
Jay Foada3efbb12011-07-15 08:37:34 +00001170 inline CallInst(Value *Func, ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001171 const Twine &NameStr, BasicBlock *InsertAtEnd);
David Greene52eec542007-08-01 03:43:44 +00001172
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001173 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001174 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001175 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001176 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001177 explicit CallInst(Value *F, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001178 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001179 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001180protected:
1181 virtual CallInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001182public:
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 = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001186 Instruction *InsertBefore = 0) {
Jay Foada3efbb12011-07-15 08:37:34 +00001187 return new(unsigned(Args.size() + 1))
1188 CallInst(Func, Args, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001189 }
Gabor Greifefe65362008-05-10 08:32:32 +00001190 static CallInst *Create(Value *Func,
Jay Foada3efbb12011-07-15 08:37:34 +00001191 ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001192 const Twine &NameStr, BasicBlock *InsertAtEnd) {
Jay Foada3efbb12011-07-15 08:37:34 +00001193 return new(unsigned(Args.size() + 1))
1194 CallInst(Func, Args, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001195 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001196 static CallInst *Create(Value *F, const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001197 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001198 return new(1) CallInst(F, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001199 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001200 static CallInst *Create(Value *F, const Twine &NameStr,
Evan Cheng34cd4a42008-05-05 18:30:58 +00001201 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001202 return new(1) CallInst(F, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001203 }
Evan Chengfabcb912009-09-10 04:36:43 +00001204 /// CreateMalloc - Generate the IR for a call to malloc:
1205 /// 1. Compute the malloc call's argument as the specified type's size,
1206 /// possibly multiplied by the array size if the array size is not
1207 /// constant 1.
1208 /// 2. Call malloc with that argument.
1209 /// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewycky3fc35c52009-10-17 23:52:26 +00001210 static Instruction *CreateMalloc(Instruction *InsertBefore,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001211 Type *IntPtrTy, Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +00001212 Value *AllocSize, Value *ArraySize = 0,
Chris Lattner5a30a852010-07-12 00:57:28 +00001213 Function* MallocF = 0,
Nick Lewycky3fc35c52009-10-17 23:52:26 +00001214 const Twine &Name = "");
1215 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001216 Type *IntPtrTy, Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +00001217 Value *AllocSize, Value *ArraySize = 0,
1218 Function* MallocF = 0,
Nick Lewycky3fc35c52009-10-17 23:52:26 +00001219 const Twine &Name = "");
Victor Hernandez66284e02009-10-24 04:23:03 +00001220 /// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner5a30a852010-07-12 00:57:28 +00001221 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
Victor Hernandez66284e02009-10-24 04:23:03 +00001222 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001223
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001224 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001225
Chris Lattnerb2406d92009-12-29 02:46:09 +00001226 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001227 void setTailCall(bool isTC = true) {
Chris Lattnerb2406d92009-12-29 02:46:09 +00001228 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
1229 unsigned(isTC));
Chris Lattner3340ffe2005-05-06 20:26:26 +00001230 }
1231
Dan Gohmanf2752502008-09-26 21:38:45 +00001232 /// Provide fast operand accessors
1233 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001234
Gabor Greif0114b992010-07-31 08:35:21 +00001235 /// getNumArgOperands - Return the number of call arguments.
1236 ///
Bill Wendling22a5b292010-06-07 19:05:06 +00001237 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
Gabor Greif0114b992010-07-31 08:35:21 +00001238
1239 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1240 ///
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001241 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1242 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
Bill Wendling22a5b292010-06-07 19:05:06 +00001243
Chris Lattner3340ffe2005-05-06 20:26:26 +00001244 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1245 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001246 CallingConv::ID getCallingConv() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +00001247 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001248 }
1249 void setCallingConv(CallingConv::ID CC) {
Chris Lattnerb2406d92009-12-29 02:46:09 +00001250 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1251 (static_cast<unsigned>(CC) << 1));
Chris Lattner3340ffe2005-05-06 20:26:26 +00001252 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00001253
Devang Patel05988662008-09-25 21:00:45 +00001254 /// getAttributes - Return the parameter attributes for this call.
Chris Lattner041221c2008-03-13 04:33:03 +00001255 ///
Devang Patel05988662008-09-25 21:00:45 +00001256 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001257
Dan Gohmanf2752502008-09-26 21:38:45 +00001258 /// setAttributes - Set the parameter attributes for this call.
1259 ///
Devang Patel05988662008-09-25 21:00:45 +00001260 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001261
Devang Patel05988662008-09-25 21:00:45 +00001262 /// addAttribute - adds the attribute to the list of attributes.
1263 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsdc024672007-11-27 13:23:08 +00001264
Devang Patel05988662008-09-25 21:00:45 +00001265 /// removeAttribute - removes the attribute from the list of attributes.
1266 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00001267
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001268 /// @brief Determine whether the call or the callee has the given attribute.
Dan Gohmanf2752502008-09-26 21:38:45 +00001269 bool paramHasAttr(unsigned i, Attributes attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001270
Dale Johannesen08e78b12008-02-22 17:49:45 +00001271 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001272 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00001273 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001274 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001275
Eric Christopherf27e6082010-03-25 04:49:10 +00001276 /// @brief Return true if the call should not be inlined.
1277 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
Nick Lewyckyb9933b82010-07-06 03:53:22 +00001278 void setIsNoInline(bool Value = true) {
Eric Christopherf27e6082010-03-25 04:49:10 +00001279 if (Value) addAttribute(~0, Attribute::NoInline);
1280 else removeAttribute(~0, Attribute::NoInline);
1281 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001282
Rafael Espindola11f1a832011-10-05 20:05:13 +00001283 /// @brief Return true if the call can return twice
1284 bool canReturnTwice() const {
1285 return paramHasAttr(~0, Attribute::ReturnsTwice);
1286 }
1287 void setCanReturnTwice(bool Value = true) {
1288 if (Value) addAttribute(~0, Attribute::ReturnsTwice);
1289 else removeAttribute(~0, Attribute::ReturnsTwice);
1290 }
1291
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001292 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001293 bool doesNotAccessMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00001294 return paramHasAttr(~0, Attribute::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001295 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001296 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001297 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1298 else removeAttribute(~0, Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00001299 }
1300
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001301 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001302 bool onlyReadsMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00001303 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001304 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001305 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001306 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1307 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00001308 }
1309
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001310 /// @brief Determine if the call cannot return.
Nick Lewyckyb9933b82010-07-06 03:53:22 +00001311 bool doesNotReturn() const { return paramHasAttr(~0, Attribute::NoReturn); }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001312 void setDoesNotReturn(bool DoesNotReturn = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001313 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1314 else removeAttribute(~0, Attribute::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00001315 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001316
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001317 /// @brief Determine if the call cannot unwind.
Nick Lewyckyb9933b82010-07-06 03:53:22 +00001318 bool doesNotThrow() const { return paramHasAttr(~0, Attribute::NoUnwind); }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001319 void setDoesNotThrow(bool DoesNotThrow = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001320 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1321 else removeAttribute(~0, Attribute::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00001322 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001323
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001324 /// @brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00001325 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001326 bool hasStructRetAttr() const {
1327 // Be friendly and also check the callee.
Devang Patel05988662008-09-25 21:00:45 +00001328 return paramHasAttr(1, Attribute::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001329 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001330
Evan Chengf4a54982008-01-12 18:57:32 +00001331 /// @brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001332 bool hasByValArgument() const {
Devang Patel05988662008-09-25 21:00:45 +00001333 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001334 }
Evan Chengf4a54982008-01-12 18:57:32 +00001335
Dan Gohmanf2752502008-09-26 21:38:45 +00001336 /// getCalledFunction - Return the function called, or null if this is an
1337 /// indirect function invocation.
1338 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001339 Function *getCalledFunction() const {
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001340 return dyn_cast<Function>(Op<-1>());
Chris Lattner721aef62004-11-18 17:46:57 +00001341 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001342
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001343 /// getCalledValue - Get a pointer to the function that is invoked by this
Chris Lattner14d80382009-10-18 05:08:07 +00001344 /// instruction.
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001345 const Value *getCalledValue() const { return Op<-1>(); }
1346 Value *getCalledValue() { return Op<-1>(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001347
Chris Lattner14d80382009-10-18 05:08:07 +00001348 /// setCalledFunction - Set the function called.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001349 void setCalledFunction(Value* Fn) {
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001350 Op<-1>() = Fn;
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001351 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001352
Owen Anderson6f615f82010-08-07 00:19:59 +00001353 /// isInlineAsm - Check if this call is an inline asm statement.
1354 bool isInlineAsm() const {
1355 return isa<InlineAsm>(Op<-1>());
1356 }
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001357
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001358 // Methods for support type inquiry through isa, cast, and dyn_cast:
1359 static inline bool classof(const CallInst *) { return true; }
1360 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001361 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001362 }
1363 static inline bool classof(const Value *V) {
1364 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1365 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001366private:
Chris Lattnerb2406d92009-12-29 02:46:09 +00001367 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1368 // method so that subclasses cannot accidentally use it.
1369 void setInstructionSubclassData(unsigned short D) {
1370 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001371 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001372};
1373
Gabor Greifefe65362008-05-10 08:32:32 +00001374template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001375struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
Gabor Greifefe65362008-05-10 08:32:32 +00001376};
1377
Jay Foada3efbb12011-07-15 08:37:34 +00001378CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001379 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001380 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1381 ->getElementType())->getReturnType(),
1382 Instruction::Call,
Jay Foada3efbb12011-07-15 08:37:34 +00001383 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1384 unsigned(Args.size() + 1), InsertAtEnd) {
1385 init(Func, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00001386}
1387
Jay Foada3efbb12011-07-15 08:37:34 +00001388CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001389 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00001390 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1391 ->getElementType())->getReturnType(),
1392 Instruction::Call,
Jay Foada3efbb12011-07-15 08:37:34 +00001393 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1394 unsigned(Args.size() + 1), InsertBefore) {
1395 init(Func, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00001396}
1397
Gabor Greife9e12152010-07-06 15:44:11 +00001398
1399// Note: if you get compile errors about private methods then
1400// please update your code to use the high-level operand
1401// interfaces. See line 943 above.
Gabor Greifefe65362008-05-10 08:32:32 +00001402DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1403
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001404//===----------------------------------------------------------------------===//
1405// SelectInst Class
1406//===----------------------------------------------------------------------===//
1407
1408/// SelectInst - This class represents the LLVM 'select' instruction.
1409///
1410class SelectInst : public Instruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001411 void init(Value *C, Value *S1, Value *S2) {
Chris Lattnerb76ec322008-12-29 00:12:50 +00001412 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
Gabor Greifefe65362008-05-10 08:32:32 +00001413 Op<0>() = C;
1414 Op<1>() = S1;
1415 Op<2>() = S2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001416 }
1417
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001418 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001419 Instruction *InsertBefore)
1420 : Instruction(S1->getType(), Instruction::Select,
1421 &Op<0>(), 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001422 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001423 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001424 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001425 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001426 BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001427 : Instruction(S1->getType(), Instruction::Select,
1428 &Op<0>(), 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001429 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001430 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001431 }
Devang Patel50b6e332009-10-27 22:16:29 +00001432protected:
1433 virtual SelectInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001434public:
Evan Chengd69bb1a2008-05-05 17:41:03 +00001435 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001436 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001437 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001438 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001439 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001440 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001441 const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001442 BasicBlock *InsertAtEnd) {
1443 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001444 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001445
Chris Lattner97159122009-09-08 03:32:53 +00001446 const Value *getCondition() const { return Op<0>(); }
1447 const Value *getTrueValue() const { return Op<1>(); }
1448 const Value *getFalseValue() const { return Op<2>(); }
1449 Value *getCondition() { return Op<0>(); }
1450 Value *getTrueValue() { return Op<1>(); }
1451 Value *getFalseValue() { return Op<2>(); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001452
Chris Lattnerb76ec322008-12-29 00:12:50 +00001453 /// areInvalidOperands - Return a string if the specified operands are invalid
1454 /// for a select operation, otherwise return null.
1455 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
Chris Lattner454928e2005-01-29 00:31:36 +00001456
1457 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001458 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001459
1460 OtherOps getOpcode() const {
1461 return static_cast<OtherOps>(Instruction::getOpcode());
1462 }
1463
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001464 // Methods for support type inquiry through isa, cast, and dyn_cast:
1465 static inline bool classof(const SelectInst *) { return true; }
1466 static inline bool classof(const Instruction *I) {
1467 return I->getOpcode() == Instruction::Select;
1468 }
1469 static inline bool classof(const Value *V) {
1470 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1471 }
1472};
1473
Gabor Greifefe65362008-05-10 08:32:32 +00001474template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001475struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001476};
1477
1478DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1479
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001480//===----------------------------------------------------------------------===//
1481// VAArgInst Class
1482//===----------------------------------------------------------------------===//
1483
1484/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001485/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001486///
Chris Lattner454928e2005-01-29 00:31:36 +00001487class VAArgInst : public UnaryInstruction {
Devang Patel50b6e332009-10-27 22:16:29 +00001488protected:
1489 virtual VAArgInst *clone_impl() const;
1490
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001491public:
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001492 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001493 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001494 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001495 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001496 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001497 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001498 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001499 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001500 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001501 }
1502
Dan Gohmane7445412010-09-09 18:32:40 +00001503 Value *getPointerOperand() { return getOperand(0); }
1504 const Value *getPointerOperand() const { return getOperand(0); }
1505 static unsigned getPointerOperandIndex() { return 0U; }
1506
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001507 // Methods for support type inquiry through isa, cast, and dyn_cast:
1508 static inline bool classof(const VAArgInst *) { return true; }
1509 static inline bool classof(const Instruction *I) {
1510 return I->getOpcode() == VAArg;
1511 }
1512 static inline bool classof(const Value *V) {
1513 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1514 }
1515};
1516
1517//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001518// ExtractElementInst Class
1519//===----------------------------------------------------------------------===//
1520
1521/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001522/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001523///
1524class ExtractElementInst : public Instruction {
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001525 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
Chris Lattner9fc18d22006-04-08 01:15:18 +00001526 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001527 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
Chris Lattner9fc18d22006-04-08 01:15:18 +00001528 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001529protected:
1530 virtual ExtractElementInst *clone_impl() const;
1531
Eric Christophera3500da2009-07-25 02:28:41 +00001532public:
Eric Christophera3500da2009-07-25 02:28:41 +00001533 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001534 const Twine &NameStr = "",
Eric Christophera3500da2009-07-25 02:28:41 +00001535 Instruction *InsertBefore = 0) {
1536 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1537 }
1538 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001539 const Twine &NameStr,
Eric Christophera3500da2009-07-25 02:28:41 +00001540 BasicBlock *InsertAtEnd) {
1541 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1542 }
Robert Bocchino49b78a52006-01-10 19:04:13 +00001543
Chris Lattnerfa495842006-04-08 04:04:54 +00001544 /// isValidOperands - Return true if an extractelement instruction can be
1545 /// formed with the specified operands.
1546 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001547
Chris Lattner97159122009-09-08 03:32:53 +00001548 Value *getVectorOperand() { return Op<0>(); }
1549 Value *getIndexOperand() { return Op<1>(); }
1550 const Value *getVectorOperand() const { return Op<0>(); }
1551 const Value *getIndexOperand() const { return Op<1>(); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001552
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001553 VectorType *getVectorOperandType() const {
1554 return reinterpret_cast<VectorType*>(getVectorOperand()->getType());
Chris Lattner97159122009-09-08 03:32:53 +00001555 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001556
1557
Robert Bocchino49b78a52006-01-10 19:04:13 +00001558 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001559 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001560
1561 // Methods for support type inquiry through isa, cast, and dyn_cast:
1562 static inline bool classof(const ExtractElementInst *) { return true; }
1563 static inline bool classof(const Instruction *I) {
1564 return I->getOpcode() == Instruction::ExtractElement;
1565 }
1566 static inline bool classof(const Value *V) {
1567 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1568 }
1569};
1570
Gabor Greifefe65362008-05-10 08:32:32 +00001571template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001572struct OperandTraits<ExtractElementInst> :
1573 public FixedNumOperandTraits<ExtractElementInst, 2> {
Gabor Greifefe65362008-05-10 08:32:32 +00001574};
1575
1576DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1577
Robert Bocchino49b78a52006-01-10 19:04:13 +00001578//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001579// InsertElementInst Class
1580//===----------------------------------------------------------------------===//
1581
1582/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001583/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001584///
1585class InsertElementInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001586 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001587 const Twine &NameStr = "",
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001588 Instruction *InsertBefore = 0);
Gabor Greif051a9502008-04-06 20:25:17 +00001589 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001590 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001591protected:
1592 virtual InsertElementInst *clone_impl() const;
1593
Robert Bocchinof9993442006-01-17 20:05:59 +00001594public:
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 = "",
Gabor Greifefe65362008-05-10 08:32:32 +00001597 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001598 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001599 }
Gabor Greif051a9502008-04-06 20:25:17 +00001600 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001601 const Twine &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001602 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001603 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001604 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001605
Chris Lattnerfa495842006-04-08 04:04:54 +00001606 /// isValidOperands - Return true if an insertelement instruction can be
1607 /// formed with the specified operands.
1608 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1609 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001610
Reid Spencerac9dcb92007-02-15 03:39:18 +00001611 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001612 ///
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001613 VectorType *getType() const {
1614 return reinterpret_cast<VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001615 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001616
Robert Bocchinof9993442006-01-17 20:05:59 +00001617 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001618 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchinof9993442006-01-17 20:05:59 +00001619
1620 // Methods for support type inquiry through isa, cast, and dyn_cast:
1621 static inline bool classof(const InsertElementInst *) { return true; }
1622 static inline bool classof(const Instruction *I) {
1623 return I->getOpcode() == Instruction::InsertElement;
1624 }
1625 static inline bool classof(const Value *V) {
1626 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1627 }
1628};
1629
Gabor Greifefe65362008-05-10 08:32:32 +00001630template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001631struct OperandTraits<InsertElementInst> :
1632 public FixedNumOperandTraits<InsertElementInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001633};
1634
1635DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1636
Robert Bocchinof9993442006-01-17 20:05:59 +00001637//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001638// ShuffleVectorInst Class
1639//===----------------------------------------------------------------------===//
1640
1641/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1642/// input vectors.
1643///
1644class ShuffleVectorInst : public Instruction {
Devang Patel50b6e332009-10-27 22:16:29 +00001645protected:
1646 virtual ShuffleVectorInst *clone_impl() const;
1647
Chris Lattner9fc18d22006-04-08 01:15:18 +00001648public:
Gabor Greif051a9502008-04-06 20:25:17 +00001649 // allocate space for exactly three operands
1650 void *operator new(size_t s) {
1651 return User::operator new(s, 3);
1652 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001653 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001654 const Twine &NameStr = "",
Evan Cheng1bf9a182008-07-24 00:08:56 +00001655 Instruction *InsertBefor = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001656 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001657 const Twine &NameStr, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001658
Chris Lattnerfa495842006-04-08 04:04:54 +00001659 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001660 /// formed with the specified operands.
1661 static bool isValidOperands(const Value *V1, const Value *V2,
1662 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001663
Reid Spencerac9dcb92007-02-15 03:39:18 +00001664 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001665 ///
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001666 VectorType *getType() const {
1667 return reinterpret_cast<VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001668 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001669
Chris Lattner9fc18d22006-04-08 01:15:18 +00001670 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001671 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001672
Chris Lattner83694a92012-01-25 23:49:49 +00001673 Constant *getMask() const {
1674 return reinterpret_cast<Constant*>(getOperand(2));
1675 }
1676
Chris Lattner8728f192008-03-02 05:28:33 +00001677 /// getMaskValue - Return the index from the shuffle mask for the specified
1678 /// output result. This is either -1 if the element is undef or a number less
1679 /// than 2*numelements.
Chris Lattner56243b82012-01-26 02:51:13 +00001680 static int getMaskValue(Constant *Mask, unsigned i);
1681
1682 int getMaskValue(unsigned i) const {
1683 return getMaskValue(getMask(), i);
1684 }
Chris Lattner83694a92012-01-25 23:49:49 +00001685
1686 /// getShuffleMask - Return the full mask for this instruction, where each
1687 /// element is the element number and undef's are returned as -1.
Chris Lattner56243b82012-01-26 02:51:13 +00001688 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1689
1690 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1691 return getShuffleMask(getMask(), Result);
1692 }
Chris Lattner83694a92012-01-25 23:49:49 +00001693
1694 SmallVector<int, 16> getShuffleMask() const {
1695 SmallVector<int, 16> Mask;
1696 getShuffleMask(Mask);
1697 return Mask;
1698 }
1699
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001700
Chris Lattner9fc18d22006-04-08 01:15:18 +00001701 // Methods for support type inquiry through isa, cast, and dyn_cast:
1702 static inline bool classof(const ShuffleVectorInst *) { return true; }
1703 static inline bool classof(const Instruction *I) {
1704 return I->getOpcode() == Instruction::ShuffleVector;
1705 }
1706 static inline bool classof(const Value *V) {
1707 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1708 }
1709};
1710
Gabor Greifefe65362008-05-10 08:32:32 +00001711template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001712struct OperandTraits<ShuffleVectorInst> :
1713 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001714};
1715
1716DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
Chris Lattner9fc18d22006-04-08 01:15:18 +00001717
1718//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001719// ExtractValueInst Class
1720//===----------------------------------------------------------------------===//
1721
Dan Gohmane2d896f2008-05-15 23:35:32 +00001722/// ExtractValueInst - This instruction extracts a struct member or array
1723/// element value from an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001724///
Gabor Greifd4f268b2008-06-06 20:28:12 +00001725class ExtractValueInst : public UnaryInstruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001726 SmallVector<unsigned, 4> Indices;
1727
Dan Gohman041e2eb2008-05-15 19:50:34 +00001728 ExtractValueInst(const ExtractValueInst &EVI);
Jay Foadfc6d3a42011-07-13 10:26:04 +00001729 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001730
Dan Gohmane2d896f2008-05-15 23:35:32 +00001731 /// Constructors - Create a extractvalue instruction with a base aggregate
1732 /// value and a list of indices. The first ctor can optionally insert before
1733 /// an existing instruction, the second appends the new instruction to the
1734 /// specified BasicBlock.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001735 inline ExtractValueInst(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001736 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001737 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001738 Instruction *InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001739 inline ExtractValueInst(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001740 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001741 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001742
Dan Gohman8e640412008-05-31 19:09:47 +00001743 // allocate space for exactly one operand
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001744 void *operator new(size_t s) {
1745 return User::operator new(s, 1);
1746 }
Devang Patel50b6e332009-10-27 22:16:29 +00001747protected:
1748 virtual ExtractValueInst *clone_impl() const;
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001749
Gabor Greifd4f268b2008-06-06 20:28:12 +00001750public:
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +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 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001755 return new
Jay Foadfc6d3a42011-07-13 10:26:04 +00001756 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001757 }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001758 static ExtractValueInst *Create(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001759 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001760 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001761 BasicBlock *InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001762 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001763 }
1764
Dan Gohman041e2eb2008-05-15 19:50:34 +00001765 /// getIndexedType - Returns the type of the element that would be extracted
1766 /// with an extractvalue instruction with the specified parameters.
1767 ///
Frits van Bommelaf72c622010-12-03 14:54:33 +00001768 /// Null is returned if the indices are invalid for the specified type.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001769 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001770
Owen Anderson5678d6e2008-06-19 17:15:57 +00001771 typedef const unsigned* idx_iterator;
1772 inline idx_iterator idx_begin() const { return Indices.begin(); }
1773 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001774
1775 Value *getAggregateOperand() {
1776 return getOperand(0);
1777 }
1778 const Value *getAggregateOperand() const {
1779 return getOperand(0);
1780 }
1781 static unsigned getAggregateOperandIndex() {
1782 return 0U; // get index for modifying correct operand
1783 }
1784
Jay Foadfc6d3a42011-07-13 10:26:04 +00001785 ArrayRef<unsigned> getIndices() const {
1786 return Indices;
1787 }
1788
1789 unsigned getNumIndices() const {
Bill Wendling67944fc2008-06-05 07:35:27 +00001790 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001791 }
1792
1793 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001794 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001795 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001796
Dan Gohman041e2eb2008-05-15 19:50:34 +00001797 // Methods for support type inquiry through isa, cast, and dyn_cast:
1798 static inline bool classof(const ExtractValueInst *) { return true; }
1799 static inline bool classof(const Instruction *I) {
1800 return I->getOpcode() == Instruction::ExtractValue;
1801 }
1802 static inline bool classof(const Value *V) {
1803 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1804 }
1805};
1806
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 Instruction *InsertBefore)
Jay Foadfc6d3a42011-07-13 10:26:04 +00001811 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
Bill Wendling85f40542008-07-22 07:14:12 +00001812 ExtractValue, Agg, InsertBefore) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001813 init(Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001814}
Dan Gohmane4569942008-05-23 00:36:11 +00001815ExtractValueInst::ExtractValueInst(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001816 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001817 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001818 BasicBlock *InsertAtEnd)
Jay Foadfc6d3a42011-07-13 10:26:04 +00001819 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
Bill Wendling85f40542008-07-22 07:14:12 +00001820 ExtractValue, Agg, InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001821 init(Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001822}
1823
Dan Gohmane4569942008-05-23 00:36:11 +00001824
Dan Gohman041e2eb2008-05-15 19:50:34 +00001825//===----------------------------------------------------------------------===//
1826// InsertValueInst Class
1827//===----------------------------------------------------------------------===//
1828
Dan Gohmane2d896f2008-05-15 23:35:32 +00001829/// InsertValueInst - This instruction inserts a struct field of array element
1830/// value into an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001831///
1832class InsertValueInst : public Instruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001833 SmallVector<unsigned, 4> Indices;
1834
1835 void *operator new(size_t, unsigned); // Do not implement
Dan Gohman041e2eb2008-05-15 19:50:34 +00001836 InsertValueInst(const InsertValueInst &IVI);
Jay Foadfc6d3a42011-07-13 10:26:04 +00001837 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001838 const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001839
Dan Gohmane2d896f2008-05-15 23:35:32 +00001840 /// Constructors - Create a insertvalue instruction with a base aggregate
1841 /// value, a value to insert, and a list of indices. The first ctor can
1842 /// optionally insert before an existing instruction, the second appends
1843 /// the new instruction to the specified BasicBlock.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001844 inline InsertValueInst(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001845 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001846 const Twine &NameStr,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001847 Instruction *InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001848 inline InsertValueInst(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001849 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001850 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001851
1852 /// Constructors - These two constructors are convenience methods because one
1853 /// and two index insertvalue instructions are so common.
1854 InsertValueInst(Value *Agg, Value *Val,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001855 unsigned Idx, const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001856 Instruction *InsertBefore = 0);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001857 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001858 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001859protected:
1860 virtual InsertValueInst *clone_impl() const;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001861public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001862 // allocate space for exactly two operands
1863 void *operator new(size_t s) {
1864 return User::operator new(s, 2);
1865 }
1866
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001867 static InsertValueInst *Create(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001868 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001869 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001870 Instruction *InsertBefore = 0) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001871 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001872 }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001873 static InsertValueInst *Create(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001874 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001875 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001876 BasicBlock *InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001877 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001878 }
1879
Dan Gohman041e2eb2008-05-15 19:50:34 +00001880 /// Transparently provide more efficient getOperand methods.
1881 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1882
Owen Anderson5678d6e2008-06-19 17:15:57 +00001883 typedef const unsigned* idx_iterator;
1884 inline idx_iterator idx_begin() const { return Indices.begin(); }
1885 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001886
1887 Value *getAggregateOperand() {
1888 return getOperand(0);
1889 }
1890 const Value *getAggregateOperand() const {
1891 return getOperand(0);
1892 }
1893 static unsigned getAggregateOperandIndex() {
1894 return 0U; // get index for modifying correct operand
1895 }
1896
1897 Value *getInsertedValueOperand() {
1898 return getOperand(1);
1899 }
1900 const Value *getInsertedValueOperand() const {
1901 return getOperand(1);
1902 }
1903 static unsigned getInsertedValueOperandIndex() {
1904 return 1U; // get index for modifying correct operand
1905 }
1906
Jay Foadfc6d3a42011-07-13 10:26:04 +00001907 ArrayRef<unsigned> getIndices() const {
1908 return Indices;
1909 }
1910
1911 unsigned getNumIndices() const {
Bill Wendling67944fc2008-06-05 07:35:27 +00001912 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001913 }
1914
1915 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001916 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001917 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001918
Dan Gohman041e2eb2008-05-15 19:50:34 +00001919 // Methods for support type inquiry through isa, cast, and dyn_cast:
1920 static inline bool classof(const InsertValueInst *) { return true; }
1921 static inline bool classof(const Instruction *I) {
1922 return I->getOpcode() == Instruction::InsertValue;
1923 }
1924 static inline bool classof(const Value *V) {
1925 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1926 }
1927};
1928
1929template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001930struct OperandTraits<InsertValueInst> :
1931 public FixedNumOperandTraits<InsertValueInst, 2> {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001932};
1933
Dan Gohmane4569942008-05-23 00:36:11 +00001934InsertValueInst::InsertValueInst(Value *Agg,
1935 Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001936 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001937 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001938 Instruction *InsertBefore)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001939 : Instruction(Agg->getType(), InsertValue,
1940 OperandTraits<InsertValueInst>::op_begin(this),
1941 2, InsertBefore) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001942 init(Agg, Val, Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001943}
Dan Gohmane4569942008-05-23 00:36:11 +00001944InsertValueInst::InsertValueInst(Value *Agg,
1945 Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001946 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001947 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001948 BasicBlock *InsertAtEnd)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001949 : Instruction(Agg->getType(), InsertValue,
1950 OperandTraits<InsertValueInst>::op_begin(this),
1951 2, InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001952 init(Agg, Val, Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001953}
1954
Dan Gohman041e2eb2008-05-15 19:50:34 +00001955DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1956
1957//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001958// PHINode Class
1959//===----------------------------------------------------------------------===//
1960
1961// PHINode - The PHINode class is used to represent the magical mystical PHI
1962// node, that can not exist in nature, but can be synthesized in a computer
1963// scientist's overactive imagination.
1964//
1965class PHINode : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001966 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00001967 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1968 /// the number actually in use.
1969 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001970 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00001971 // allocate space for exactly zero operands
1972 void *operator new(size_t s) {
1973 return User::operator new(s, 0);
1974 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001975 explicit PHINode(Type *Ty, unsigned NumReservedValues,
Jay Foad3ecfc862011-03-30 11:28:46 +00001976 const Twine &NameStr = "", Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001977 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Jay Foad95c3e482011-06-23 09:09:15 +00001978 ReservedSpace(NumReservedValues) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001979 setName(NameStr);
Jay Foad3ecfc862011-03-30 11:28:46 +00001980 OperandList = allocHungoffUses(ReservedSpace);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001981 }
1982
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001983 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
Jay Foad3ecfc862011-03-30 11:28:46 +00001984 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001985 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Jay Foad95c3e482011-06-23 09:09:15 +00001986 ReservedSpace(NumReservedValues) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001987 setName(NameStr);
Jay Foad3ecfc862011-03-30 11:28:46 +00001988 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattner454928e2005-01-29 00:31:36 +00001989 }
Devang Patel50b6e332009-10-27 22:16:29 +00001990protected:
Jay Foad95c3e482011-06-23 09:09:15 +00001991 // allocHungoffUses - this is more complicated than the generic
1992 // User::allocHungoffUses, because we have to allocate Uses for the incoming
1993 // values and pointers to the incoming blocks, all in one allocation.
1994 Use *allocHungoffUses(unsigned) const;
1995
Devang Patel50b6e332009-10-27 22:16:29 +00001996 virtual PHINode *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001997public:
Jay Foad44991762011-03-30 13:29:06 +00001998 /// Constructors - NumReservedValues is a hint for the number of incoming
1999 /// edges that this phi node will have (use 0 if you really have no idea).
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002000 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
Jay Foad3ecfc862011-03-30 11:28:46 +00002001 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00002002 Instruction *InsertBefore = 0) {
Jay Foad3ecfc862011-03-30 11:28:46 +00002003 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002004 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002005 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
Jay Foad3ecfc862011-03-30 11:28:46 +00002006 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2007 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002008 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002009 ~PHINode();
2010
Gabor Greifefe65362008-05-10 08:32:32 +00002011 /// Provide fast operand accessors
2012 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2013
Jay Foad95c3e482011-06-23 09:09:15 +00002014 // Block iterator interface. This provides access to the list of incoming
2015 // basic blocks, which parallels the list of incoming values.
2016
2017 typedef BasicBlock **block_iterator;
2018 typedef BasicBlock * const *const_block_iterator;
2019
2020 block_iterator block_begin() {
2021 Use::UserRef *ref =
2022 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2023 return reinterpret_cast<block_iterator>(ref + 1);
2024 }
2025
2026 const_block_iterator block_begin() const {
2027 const Use::UserRef *ref =
2028 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2029 return reinterpret_cast<const_block_iterator>(ref + 1);
2030 }
2031
2032 block_iterator block_end() {
2033 return block_begin() + getNumOperands();
2034 }
2035
2036 const_block_iterator block_end() const {
2037 return block_begin() + getNumOperands();
2038 }
2039
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002040 /// getNumIncomingValues - Return the number of incoming edges
2041 ///
Jay Foad95c3e482011-06-23 09:09:15 +00002042 unsigned getNumIncomingValues() const { return getNumOperands(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002043
Reid Spencerc773de62006-05-19 19:07:54 +00002044 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002045 ///
2046 Value *getIncomingValue(unsigned i) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002047 return getOperand(i);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002048 }
2049 void setIncomingValue(unsigned i, Value *V) {
Jay Foad95c3e482011-06-23 09:09:15 +00002050 setOperand(i, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002051 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00002052 static unsigned getOperandNumForIncomingValue(unsigned i) {
Jay Foad95c3e482011-06-23 09:09:15 +00002053 return i;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002054 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00002055 static unsigned getIncomingValueNumForOperand(unsigned i) {
Jay Foad95c3e482011-06-23 09:09:15 +00002056 return i;
Dan Gohmanb45088c2009-03-23 15:48:29 +00002057 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002058
Dan Gohmanfb76fe02010-02-22 04:10:52 +00002059 /// getIncomingBlock - Return incoming basic block number @p i.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002060 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00002061 BasicBlock *getIncomingBlock(unsigned i) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002062 return block_begin()[i];
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002063 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002064
Chris Lattnerceaa4572009-10-10 07:42:42 +00002065 /// getIncomingBlock - Return incoming basic block corresponding
2066 /// to an operand of the PHI.
2067 ///
2068 BasicBlock *getIncomingBlock(const Use &U) const {
2069 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
Jay Foad95c3e482011-06-23 09:09:15 +00002070 return getIncomingBlock(unsigned(&U - op_begin()));
Chris Lattnerceaa4572009-10-10 07:42:42 +00002071 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002072
Chris Lattnerceaa4572009-10-10 07:42:42 +00002073 /// getIncomingBlock - Return incoming basic block corresponding
2074 /// to value use iterator.
2075 ///
2076 template <typename U>
2077 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
2078 return getIncomingBlock(I.getUse());
2079 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002080
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002081 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Jay Foad95c3e482011-06-23 09:09:15 +00002082 block_begin()[i] = BB;
Dan Gohmanb45088c2009-03-23 15:48:29 +00002083 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002084
2085 /// addIncoming - Add an incoming value to the end of the PHI list
2086 ///
2087 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00002088 assert(V && "PHI node got a null value!");
2089 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002090 assert(getType() == V->getType() &&
2091 "All operands to PHI node must be the same type as the PHI node!");
Jay Foad95c3e482011-06-23 09:09:15 +00002092 if (NumOperands == ReservedSpace)
Jay Foad8891ed72011-04-01 08:00:58 +00002093 growOperands(); // Get more space!
Chris Lattner454928e2005-01-29 00:31:36 +00002094 // Initialize some new operands.
Jay Foad95c3e482011-06-23 09:09:15 +00002095 ++NumOperands;
2096 setIncomingValue(NumOperands - 1, V);
2097 setIncomingBlock(NumOperands - 1, BB);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002098 }
Misha Brukman9769ab22005-04-21 20:19:05 +00002099
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002100 /// removeIncomingValue - Remove an incoming value. This is useful if a
2101 /// predecessor basic block is deleted. The value removed is returned.
2102 ///
2103 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2104 /// is true), the PHI node is destroyed and any uses of it are replaced with
2105 /// dummy values. The only time there should be zero incoming values to a PHI
2106 /// node is when the block is dead, so this strategy is sound.
2107 ///
2108 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2109
Gabor Greifefe65362008-05-10 08:32:32 +00002110 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002111 int Idx = getBasicBlockIndex(BB);
2112 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2113 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2114 }
2115
Misha Brukman9769ab22005-04-21 20:19:05 +00002116 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002117 /// block in the value list for this PHI. Returns -1 if no instance.
2118 ///
2119 int getBasicBlockIndex(const BasicBlock *BB) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002120 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2121 if (block_begin()[i] == BB)
2122 return i;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002123 return -1;
2124 }
2125
2126 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002127 int Idx = getBasicBlockIndex(BB);
2128 assert(Idx >= 0 && "Invalid basic block argument!");
2129 return getIncomingValue(Idx);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002130 }
2131
Chris Lattnerf56a8db2006-10-03 17:09:12 +00002132 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00002133 /// same value, return the value, otherwise return null.
Duncan Sandsff103412010-11-17 04:30:22 +00002134 Value *hasConstantValue() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00002135
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002136 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2137 static inline bool classof(const PHINode *) { return true; }
2138 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00002139 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002140 }
2141 static inline bool classof(const Value *V) {
2142 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2143 }
Chris Lattner454928e2005-01-29 00:31:36 +00002144 private:
Jay Foad8891ed72011-04-01 08:00:58 +00002145 void growOperands();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002146};
2147
Gabor Greifefe65362008-05-10 08:32:32 +00002148template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002149struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00002150};
2151
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002152DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002153
Bill Wendlinge6e88262011-08-12 20:24:12 +00002154//===----------------------------------------------------------------------===//
2155// LandingPadInst Class
2156//===----------------------------------------------------------------------===//
2157
2158//===---------------------------------------------------------------------------
2159/// LandingPadInst - The landingpad instruction holds all of the information
2160/// necessary to generate correct exception handling. The landingpad instruction
2161/// cannot be moved from the top of a landing pad block, which itself is
2162/// accessible only from the 'unwind' edge of an invoke. This uses the
2163/// SubclassData field in Value to store whether or not the landingpad is a
2164/// cleanup.
2165///
2166class LandingPadInst : public Instruction {
2167 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2168 /// the number actually in use.
2169 unsigned ReservedSpace;
2170 LandingPadInst(const LandingPadInst &LP);
2171public:
2172 enum ClauseType { Catch, Filter };
2173private:
2174 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2175 // Allocate space for exactly zero operands.
2176 void *operator new(size_t s) {
2177 return User::operator new(s, 0);
2178 }
2179 void growOperands(unsigned Size);
2180 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2181
2182 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2183 unsigned NumReservedValues, const Twine &NameStr,
2184 Instruction *InsertBefore);
2185 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2186 unsigned NumReservedValues, const Twine &NameStr,
2187 BasicBlock *InsertAtEnd);
2188protected:
2189 virtual LandingPadInst *clone_impl() const;
2190public:
2191 /// Constructors - NumReservedClauses is a hint for the number of incoming
2192 /// clauses that this landingpad will have (use 0 if you really have no idea).
2193 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2194 unsigned NumReservedClauses,
2195 const Twine &NameStr = "",
2196 Instruction *InsertBefore = 0);
2197 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2198 unsigned NumReservedClauses,
2199 const Twine &NameStr, BasicBlock *InsertAtEnd);
2200 ~LandingPadInst();
2201
2202 /// Provide fast operand accessors
2203 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2204
2205 /// getPersonalityFn - Get the personality function associated with this
2206 /// landing pad.
2207 Value *getPersonalityFn() const { return getOperand(0); }
2208
2209 /// isCleanup - Return 'true' if this landingpad instruction is a
2210 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2211 /// doesn't catch the exception.
2212 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2213
2214 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2215 void setCleanup(bool V) {
2216 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2217 (V ? 1 : 0));
2218 }
2219
2220 /// addClause - Add a catch or filter clause to the landing pad.
2221 void addClause(Value *ClauseVal);
2222
2223 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2224 /// to determine what type of clause this is.
2225 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2226
2227 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2228 bool isCatch(unsigned Idx) const {
2229 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2230 }
2231
2232 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2233 bool isFilter(unsigned Idx) const {
2234 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2235 }
2236
2237 /// getNumClauses - Get the number of clauses for this landing pad.
2238 unsigned getNumClauses() const { return getNumOperands() - 1; }
2239
2240 /// reserveClauses - Grow the size of the operand list to accomodate the new
2241 /// number of clauses.
2242 void reserveClauses(unsigned Size) { growOperands(Size); }
2243
2244 // Methods for support type inquiry through isa, cast, and dyn_cast:
2245 static inline bool classof(const LandingPadInst *) { return true; }
2246 static inline bool classof(const Instruction *I) {
2247 return I->getOpcode() == Instruction::LandingPad;
2248 }
2249 static inline bool classof(const Value *V) {
2250 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2251 }
2252};
2253
2254template <>
2255struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2256};
2257
2258DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002259
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002260//===----------------------------------------------------------------------===//
2261// ReturnInst Class
2262//===----------------------------------------------------------------------===//
2263
2264//===---------------------------------------------------------------------------
2265/// ReturnInst - Return a value (possibly void), from a function. Execution
2266/// does not continue in this function any longer.
2267///
2268class ReturnInst : public TerminatorInst {
Chris Lattner910c80a2007-02-24 00:55:48 +00002269 ReturnInst(const ReturnInst &RI);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002270
Gabor Greif051a9502008-04-06 20:25:17 +00002271private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002272 // ReturnInst constructors:
2273 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002274 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002275 // ReturnInst(Value* X) - 'ret X' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00002276 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002277 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
Gabor Greifefe65362008-05-10 08:32:32 +00002278 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2279 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002280 //
2281 // NOTE: If the Value* passed is of type void then the constructor behaves as
2282 // if it was passed NULL.
Owen Anderson1d0be152009-08-13 21:58:54 +00002283 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2284 Instruction *InsertBefore = 0);
2285 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2286 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002287protected:
2288 virtual ReturnInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002289public:
Owen Anderson1d0be152009-08-13 21:58:54 +00002290 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2291 Instruction *InsertBefore = 0) {
2292 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002293 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002294 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2295 BasicBlock *InsertAtEnd) {
2296 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002297 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002298 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2299 return new(0) ReturnInst(C, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002300 }
Devang Patel57ef4f42008-02-23 00:35:18 +00002301 virtual ~ReturnInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002302
Gabor Greifefe65362008-05-10 08:32:32 +00002303 /// Provide fast operand accessors
2304 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Devang Patel64d4e612008-02-26 17:56:20 +00002305
Dan Gohman8faa6af2010-10-06 16:59:24 +00002306 /// Convenience accessor. Returns null if there is no return value.
2307 Value *getReturnValue() const {
2308 return getNumOperands() != 0 ? getOperand(0) : 0;
Devang Patel1eafa062008-03-11 17:35:03 +00002309 }
2310
Chris Lattner454928e2005-01-29 00:31:36 +00002311 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002312
2313 // Methods for support type inquiry through isa, cast, and dyn_cast:
2314 static inline bool classof(const ReturnInst *) { return true; }
2315 static inline bool classof(const Instruction *I) {
2316 return (I->getOpcode() == Instruction::Ret);
2317 }
2318 static inline bool classof(const Value *V) {
2319 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2320 }
Chris Lattner454928e2005-01-29 00:31:36 +00002321 private:
2322 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2323 virtual unsigned getNumSuccessorsV() const;
2324 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002325};
2326
Gabor Greifefe65362008-05-10 08:32:32 +00002327template <>
Jay Foad67c619b2011-01-11 15:07:38 +00002328struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
Gabor Greifefe65362008-05-10 08:32:32 +00002329};
2330
2331DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002332
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002333//===----------------------------------------------------------------------===//
2334// BranchInst Class
2335//===----------------------------------------------------------------------===//
2336
2337//===---------------------------------------------------------------------------
2338/// BranchInst - Conditional or Unconditional Branch instruction.
2339///
2340class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00002341 /// Ops list - Branches are strange. The operands are ordered:
Gabor Greifae5a20a2009-03-12 18:34:49 +00002342 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2343 /// they don't have to check for cond/uncond branchness. These are mostly
2344 /// accessed relative from op_end().
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002345 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00002346 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002347 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2348 // BranchInst(BB *B) - 'br B'
2349 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2350 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2351 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2352 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2353 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00002354 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002355 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002356 Instruction *InsertBefore = 0);
2357 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002358 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002359 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002360protected:
2361 virtual BranchInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002362public:
2363 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
Jay Foad8e3914d2011-01-07 20:29:02 +00002364 return new(1) BranchInst(IfTrue, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002365 }
Gabor Greifefe65362008-05-10 08:32:32 +00002366 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2367 Value *Cond, Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002368 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2369 }
2370 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
Jay Foad8e3914d2011-01-07 20:29:02 +00002371 return new(1) BranchInst(IfTrue, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002372 }
Gabor Greifefe65362008-05-10 08:32:32 +00002373 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2374 Value *Cond, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002375 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2376 }
Chris Lattner454928e2005-01-29 00:31:36 +00002377
2378 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00002379 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002380
Devang Patel4d4a5e02008-02-23 01:11:02 +00002381 bool isUnconditional() const { return getNumOperands() == 1; }
2382 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002383
Devang Patel4d4a5e02008-02-23 01:11:02 +00002384 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002385 assert(isConditional() && "Cannot get condition of an uncond branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002386 return Op<-3>();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002387 }
2388
2389 void setCondition(Value *V) {
2390 assert(isConditional() && "Cannot set condition of unconditional branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002391 Op<-3>() = V;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002392 }
2393
Chris Lattner454928e2005-01-29 00:31:36 +00002394 unsigned getNumSuccessors() const { return 1+isConditional(); }
2395
2396 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002397 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002398 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002399 }
2400
Chris Lattner454928e2005-01-29 00:31:36 +00002401 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002402 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner4b122932009-10-27 16:49:53 +00002403 *(&Op<-1>() - idx) = (Value*)NewSucc;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002404 }
2405
Chandler Carruth602650c2011-10-17 01:11:57 +00002406 /// \brief Swap the successors of this branch instruction.
2407 ///
2408 /// Swaps the successors of the branch instruction. This also swaps any
2409 /// branch weight metadata associated with the instruction so that it
2410 /// continues to map correctly to each operand.
2411 void swapSuccessors();
2412
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002413 // Methods for support type inquiry through isa, cast, and dyn_cast:
2414 static inline bool classof(const BranchInst *) { return true; }
2415 static inline bool classof(const Instruction *I) {
2416 return (I->getOpcode() == Instruction::Br);
2417 }
2418 static inline bool classof(const Value *V) {
2419 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2420 }
Chris Lattner454928e2005-01-29 00:31:36 +00002421private:
2422 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2423 virtual unsigned getNumSuccessorsV() const;
2424 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002425};
2426
Gabor Greifefe65362008-05-10 08:32:32 +00002427template <>
Jay Foad67c619b2011-01-11 15:07:38 +00002428struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2429};
Gabor Greifefe65362008-05-10 08:32:32 +00002430
2431DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2432
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002433//===----------------------------------------------------------------------===//
2434// SwitchInst Class
2435//===----------------------------------------------------------------------===//
2436
2437//===---------------------------------------------------------------------------
2438/// SwitchInst - Multiway switch
2439///
2440class SwitchInst : public TerminatorInst {
Gabor Greifefe65362008-05-10 08:32:32 +00002441 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00002442 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002443 // Operand[0] = Value to switch on
2444 // Operand[1] = Default basic block destination
2445 // Operand[2n ] = Value to match
2446 // Operand[2n+1] = BasicBlock to go to on match
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002447 SwitchInst(const SwitchInst &SI);
Chris Lattneraa6e3502010-11-17 05:41:46 +00002448 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
Jay Foad8891ed72011-04-01 08:00:58 +00002449 void growOperands();
Gabor Greifefe65362008-05-10 08:32:32 +00002450 // allocate space for exactly zero operands
2451 void *operator new(size_t s) {
2452 return User::operator new(s, 0);
2453 }
Chris Lattner454928e2005-01-29 00:31:36 +00002454 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2455 /// switch on and a default destination. The number of additional cases can
2456 /// be specified here to make memory allocation more efficient. This
2457 /// constructor can also autoinsert before another instruction.
2458 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002459 Instruction *InsertBefore);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002460
Chris Lattner454928e2005-01-29 00:31:36 +00002461 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2462 /// switch on and a default destination. The number of additional cases can
2463 /// be specified here to make memory allocation more efficient. This
2464 /// constructor also autoinserts at the end of the specified BasicBlock.
2465 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002466 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002467protected:
2468 virtual SwitchInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002469public:
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002470
2471 static const unsigned DefaultPseudoIndex = ~0L-1; // -2
2472
2473 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2474 class CaseIteratorT {
2475 protected:
2476
2477 SwitchInstTy *SI;
2478 unsigned Index;
2479
2480 public:
2481
2482 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2483
2484 /// Initializes case iterator for given SwitchInst and for given
2485 /// case number.
2486 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2487 this->SI = SI;
2488 Index = CaseNum;
2489 }
2490
2491 /// Initializes case iterator for given SwitchInst and for given
2492 /// TerminatorInst's successor index.
2493 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2494 assert(SuccessorIndex < SI->getNumSuccessors() &&
2495 "Successor index # out of range!");
2496 return SuccessorIndex != 0 ?
2497 Self(SI, SuccessorIndex - 1) :
2498 Self(SI, DefaultPseudoIndex);
2499 }
2500
2501 /// Resolves case value for current case.
2502 ConstantIntTy *getCaseValue() {
2503 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2504 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2505 }
2506
2507 /// Resolves successor for current case.
2508 BasicBlockTy *getCaseSuccessor() {
2509 assert((Index < SI->getNumCases() || DefaultPseudoIndex) &&
2510 "Index out the number of cases.");
2511 return SI->getSuccessor(getSuccessorIndex());
2512 }
2513
2514 /// Returns number of current case.
2515 unsigned getCaseIndex() const { return Index; }
2516
2517 /// Returns TerminatorInst's successor index for current case successor.
2518 unsigned getSuccessorIndex() const {
2519 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2520 "Index out the number of cases.");
2521 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2522 }
2523
2524 Self operator++() {
2525 // Check index correctness after increment.
2526 // Note: Index == getNumCases() means end().
2527 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2528 ++Index;
2529 return *this;
2530 }
2531 Self operator++(int) {
2532 Self tmp = *this;
2533 ++(*this);
2534 return tmp;
2535 }
2536 Self operator--() {
2537 // Check index correctness after decrement.
2538 // Note: Index == getNumCases() means end().
2539 // Also allow "-1" iterator here. That will became valid after ++.
2540 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2541 "Index out the number of cases.");
2542 --Index;
2543 return *this;
2544 }
2545 Self operator--(int) {
2546 Self tmp = *this;
2547 --(*this);
2548 return tmp;
2549 }
2550 bool operator==(const Self& RHS) const {
2551 assert(RHS.SI == SI && "Incompatible operators.");
2552 return RHS.Index == Index;
2553 }
2554 bool operator!=(const Self& RHS) const {
2555 assert(RHS.SI == SI && "Incompatible operators.");
2556 return RHS.Index != Index;
2557 }
2558 };
2559
2560 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2561 ConstCaseIt;
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002562
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002563 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2564
2565 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2566
2567 public:
2568
2569 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2570 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2571
2572 /// Sets the new value for current case.
2573 void setValue(ConstantInt *V) {
2574 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2575 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2576 }
2577
2578 /// Sets the new successor for current case.
2579 void setSuccessor(BasicBlock *S) {
2580 SI->setSuccessor(getSuccessorIndex(), S);
2581 }
2582 };
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002583
Gabor Greifefe65362008-05-10 08:32:32 +00002584 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2585 unsigned NumCases, Instruction *InsertBefore = 0) {
2586 return new SwitchInst(Value, Default, NumCases, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002587 }
Gabor Greifefe65362008-05-10 08:32:32 +00002588 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2589 unsigned NumCases, BasicBlock *InsertAtEnd) {
2590 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002591 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002592
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002593 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00002594
Gabor Greifefe65362008-05-10 08:32:32 +00002595 /// Provide fast operand accessors
2596 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2597
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002598 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00002599 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00002600 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00002601
Devang Patel4d4a5e02008-02-23 01:11:02 +00002602 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002603 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002604 }
2605
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002606 void setDefaultDest(BasicBlock *DefaultCase) {
2607 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2608 }
2609
2610 /// getNumCases - return the number of 'cases' in this switch instruction,
2611 /// except the default case
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002612 unsigned getNumCases() const {
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002613 return getNumOperands()/2 - 1;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002614 }
2615
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002616 /// Returns a read/write iterator that points to the first
2617 /// case in SwitchInst.
2618 CaseIt caseBegin() {
2619 return CaseIt(this, 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002620 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002621 /// Returns a read-only iterator that points to the first
2622 /// case in the SwitchInst.
2623 ConstCaseIt caseBegin() const {
2624 return ConstCaseIt(this, 0);
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002625 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002626
2627 /// Returns a read/write iterator that points one past the last
2628 /// in the SwitchInst.
2629 CaseIt caseEnd() {
2630 return CaseIt(this, getNumCases());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002631 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002632 /// Returns a read-only iterator that points one past the last
2633 /// in the SwitchInst.
2634 ConstCaseIt caseEnd() const {
2635 return ConstCaseIt(this, getNumCases());
2636 }
2637 /// Returns an iterator that points to default case.
2638 /// Note: this iterator allows to resolve successor only. Attempt
2639 /// to resolve case value causes an assertion.
2640 CaseIt caseDefault() {
2641 return CaseIt(this, DefaultPseudoIndex);
2642 }
2643 ConstCaseIt caseDefault() const {
2644 return ConstCaseIt(this, DefaultPseudoIndex);
2645 }
2646
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002647 /// findCaseValue - Search all of the case values for the specified constant.
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002648 /// If it is explicitly handled, return the case iterator of it, otherwise
2649 /// return default case iterator to indicate
2650 /// that it is handled by the default handler.
2651 CaseIt findCaseValue(const ConstantInt *C) {
2652 for (CaseIt i = caseBegin(), e = caseEnd(); i != e; ++i)
2653 if (i.getCaseValue() == C)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002654 return i;
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002655 return caseDefault();
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002656 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002657 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2658 for (ConstCaseIt i = caseBegin(), e = caseEnd(); i != e; ++i)
2659 if (i.getCaseValue() == C)
2660 return i;
2661 return caseDefault();
2662 }
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002663
Nick Lewycky011f1842006-09-18 19:03:59 +00002664 /// findCaseDest - Finds the unique case value for a given successor. Returns
2665 /// null if the successor is not found, not unique, or is the default case.
2666 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00002667 if (BB == getDefaultDest()) return NULL;
2668
Nick Lewycky011f1842006-09-18 19:03:59 +00002669 ConstantInt *CI = NULL;
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002670 for (CaseIt i = caseBegin(), e = caseEnd(); i != e; ++i) {
2671 if (i.getCaseSuccessor() == BB) {
Nick Lewycky011f1842006-09-18 19:03:59 +00002672 if (CI) return NULL; // Multiple cases lead to BB.
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002673 else CI = i.getCaseValue();
Nick Lewycky011f1842006-09-18 19:03:59 +00002674 }
2675 }
2676 return CI;
2677 }
2678
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002679 /// addCase - Add an entry to the switch instruction...
2680 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00002681 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002682
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002683 /// removeCase - This method removes the specified case and its successor
2684 /// from the switch instruction. Note that this operation may reorder the
Jay Foad0faa6092011-02-01 09:22:34 +00002685 /// remaining cases at index idx and above.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002686 ///
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002687 void removeCase(CaseIt i);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002688
Chris Lattner454928e2005-01-29 00:31:36 +00002689 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2690 BasicBlock *getSuccessor(unsigned idx) const {
2691 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2692 return cast<BasicBlock>(getOperand(idx*2+1));
2693 }
2694 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002695 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Chris Lattner4b122932009-10-27 16:49:53 +00002696 setOperand(idx*2+1, (Value*)NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002697 }
2698
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002699 // Methods for support type inquiry through isa, cast, and dyn_cast:
2700 static inline bool classof(const SwitchInst *) { return true; }
2701 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00002702 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002703 }
2704 static inline bool classof(const Value *V) {
2705 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2706 }
Chris Lattner454928e2005-01-29 00:31:36 +00002707private:
2708 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2709 virtual unsigned getNumSuccessorsV() const;
2710 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002711};
2712
Gabor Greifefe65362008-05-10 08:32:32 +00002713template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002714struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00002715};
2716
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002717DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002718
2719
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002720//===----------------------------------------------------------------------===//
Chris Lattnerab21db72009-10-28 00:19:10 +00002721// IndirectBrInst Class
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002722//===----------------------------------------------------------------------===//
2723
2724//===---------------------------------------------------------------------------
Chris Lattnerab21db72009-10-28 00:19:10 +00002725/// IndirectBrInst - Indirect Branch Instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002726///
Chris Lattnerab21db72009-10-28 00:19:10 +00002727class IndirectBrInst : public TerminatorInst {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002728 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2729 unsigned ReservedSpace;
2730 // Operand[0] = Value to switch on
2731 // Operand[1] = Default basic block destination
2732 // Operand[2n ] = Value to match
2733 // Operand[2n+1] = BasicBlock to go to on match
Chris Lattnerab21db72009-10-28 00:19:10 +00002734 IndirectBrInst(const IndirectBrInst &IBI);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002735 void init(Value *Address, unsigned NumDests);
Jay Foad8891ed72011-04-01 08:00:58 +00002736 void growOperands();
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002737 // allocate space for exactly zero operands
2738 void *operator new(size_t s) {
2739 return User::operator new(s, 0);
2740 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002741 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2742 /// Address to jump to. The number of expected destinations can be specified
2743 /// here to make memory allocation more efficient. This constructor can also
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002744 /// autoinsert before another instruction.
Chris Lattnerab21db72009-10-28 00:19:10 +00002745 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002746
Chris Lattnerab21db72009-10-28 00:19:10 +00002747 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2748 /// Address to jump to. The number of expected destinations can be specified
2749 /// here to make memory allocation more efficient. This constructor also
2750 /// autoinserts at the end of the specified BasicBlock.
2751 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002752protected:
Chris Lattnerab21db72009-10-28 00:19:10 +00002753 virtual IndirectBrInst *clone_impl() const;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002754public:
Chris Lattnerab21db72009-10-28 00:19:10 +00002755 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2756 Instruction *InsertBefore = 0) {
2757 return new IndirectBrInst(Address, NumDests, InsertBefore);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002758 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002759 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2760 BasicBlock *InsertAtEnd) {
2761 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002762 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002763 ~IndirectBrInst();
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002764
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002765 /// Provide fast operand accessors.
2766 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002767
Chris Lattnerab21db72009-10-28 00:19:10 +00002768 // Accessor Methods for IndirectBrInst instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002769 Value *getAddress() { return getOperand(0); }
2770 const Value *getAddress() const { return getOperand(0); }
2771 void setAddress(Value *V) { setOperand(0, V); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002772
2773
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002774 /// getNumDestinations - return the number of possible destinations in this
Chris Lattnerab21db72009-10-28 00:19:10 +00002775 /// indirectbr instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002776 unsigned getNumDestinations() const { return getNumOperands()-1; }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002777
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002778 /// getDestination - Return the specified destination.
2779 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2780 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002781
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002782 /// addDestination - Add a destination.
2783 ///
2784 void addDestination(BasicBlock *Dest);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002785
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002786 /// removeDestination - This method removes the specified successor from the
Chris Lattnerab21db72009-10-28 00:19:10 +00002787 /// indirectbr instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002788 void removeDestination(unsigned i);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002789
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002790 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2791 BasicBlock *getSuccessor(unsigned i) const {
2792 return cast<BasicBlock>(getOperand(i+1));
2793 }
2794 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2795 setOperand(i+1, (Value*)NewSucc);
2796 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002797
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002798 // Methods for support type inquiry through isa, cast, and dyn_cast:
Chris Lattnerab21db72009-10-28 00:19:10 +00002799 static inline bool classof(const IndirectBrInst *) { return true; }
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002800 static inline bool classof(const Instruction *I) {
Chris Lattnerab21db72009-10-28 00:19:10 +00002801 return I->getOpcode() == Instruction::IndirectBr;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002802 }
2803 static inline bool classof(const Value *V) {
2804 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2805 }
2806private:
2807 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2808 virtual unsigned getNumSuccessorsV() const;
2809 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2810};
2811
2812template <>
Chris Lattnerab21db72009-10-28 00:19:10 +00002813struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002814};
2815
Chris Lattnerab21db72009-10-28 00:19:10 +00002816DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002817
2818
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002819//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002820// InvokeInst Class
2821//===----------------------------------------------------------------------===//
2822
Chris Lattner3340ffe2005-05-06 20:26:26 +00002823/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2824/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002825///
2826class InvokeInst : public TerminatorInst {
Devang Patel05988662008-09-25 21:00:45 +00002827 AttrListPtr AttributeList;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002828 InvokeInst(const InvokeInst &BI);
David Greenef1355a52007-08-27 19:04:21 +00002829 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002830 ArrayRef<Value *> Args, const Twine &NameStr);
David Greenef1355a52007-08-27 19:04:21 +00002831
David Greenef1355a52007-08-27 19:04:21 +00002832 /// Construct an InvokeInst given a range of arguments.
David Greenef1355a52007-08-27 19:04:21 +00002833 ///
2834 /// @brief Construct an InvokeInst from a range of arguments
Gabor Greifefe65362008-05-10 08:32:32 +00002835 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002836 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002837 const Twine &NameStr, Instruction *InsertBefore);
David Greenef1355a52007-08-27 19:04:21 +00002838
2839 /// Construct an InvokeInst given a range of arguments.
David Greenef1355a52007-08-27 19:04:21 +00002840 ///
2841 /// @brief Construct an InvokeInst from a range of arguments
Gabor Greifefe65362008-05-10 08:32:32 +00002842 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002843 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002844 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002845protected:
2846 virtual InvokeInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002847public:
Gabor Greifefe65362008-05-10 08:32:32 +00002848 static InvokeInst *Create(Value *Func,
2849 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002850 ArrayRef<Value *> Args, const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00002851 Instruction *InsertBefore = 0) {
Jay Foada3efbb12011-07-15 08:37:34 +00002852 unsigned Values = unsigned(Args.size()) + 3;
2853 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002854 Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002855 }
Gabor Greifefe65362008-05-10 08:32:32 +00002856 static InvokeInst *Create(Value *Func,
2857 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002858 ArrayRef<Value *> Args, const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002859 BasicBlock *InsertAtEnd) {
Jay Foada3efbb12011-07-15 08:37:34 +00002860 unsigned Values = unsigned(Args.size()) + 3;
2861 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002862 Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002863 }
David Greenef1355a52007-08-27 19:04:21 +00002864
Gabor Greifefe65362008-05-10 08:32:32 +00002865 /// Provide fast operand accessors
2866 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002867
Gabor Greif0114b992010-07-31 08:35:21 +00002868 /// getNumArgOperands - Return the number of invoke arguments.
2869 ///
Bill Wendling22a5b292010-06-07 19:05:06 +00002870 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
Gabor Greif0114b992010-07-31 08:35:21 +00002871
2872 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2873 ///
Bill Wendling22a5b292010-06-07 19:05:06 +00002874 Value *getArgOperand(unsigned i) const { return getOperand(i); }
Gabor Greif710ac072010-06-28 12:23:36 +00002875 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
Bill Wendling22a5b292010-06-07 19:05:06 +00002876
Chris Lattner3340ffe2005-05-06 20:26:26 +00002877 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2878 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002879 CallingConv::ID getCallingConv() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +00002880 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002881 }
2882 void setCallingConv(CallingConv::ID CC) {
Chris Lattnerb2406d92009-12-29 02:46:09 +00002883 setInstructionSubclassData(static_cast<unsigned>(CC));
Chris Lattner3340ffe2005-05-06 20:26:26 +00002884 }
2885
Devang Patel05988662008-09-25 21:00:45 +00002886 /// getAttributes - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002887 ///
Devang Patel05988662008-09-25 21:00:45 +00002888 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002889
Devang Patel05988662008-09-25 21:00:45 +00002890 /// setAttributes - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002891 ///
Devang Patel05988662008-09-25 21:00:45 +00002892 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00002893
Devang Patel05988662008-09-25 21:00:45 +00002894 /// addAttribute - adds the attribute to the list of attributes.
2895 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002896
Devang Patel05988662008-09-25 21:00:45 +00002897 /// removeAttribute - removes the attribute from the list of attributes.
2898 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00002899
Dan Gohmanf2752502008-09-26 21:38:45 +00002900 /// @brief Determine whether the call or the callee has the given attribute.
2901 bool paramHasAttr(unsigned i, Attributes attr) const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002902
Dale Johannesen08e78b12008-02-22 17:49:45 +00002903 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002904 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00002905 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002906 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00002907
Eric Christopherf27e6082010-03-25 04:49:10 +00002908 /// @brief Return true if the call should not be inlined.
2909 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
Nick Lewyckyb9933b82010-07-06 03:53:22 +00002910 void setIsNoInline(bool Value = true) {
Eric Christopherf27e6082010-03-25 04:49:10 +00002911 if (Value) addAttribute(~0, Attribute::NoInline);
2912 else removeAttribute(~0, Attribute::NoInline);
2913 }
Nick Lewyckyb9933b82010-07-06 03:53:22 +00002914
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002915 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002916 bool doesNotAccessMemory() const {
Dan Gohmana62b5ed2009-07-17 16:12:36 +00002917 return paramHasAttr(~0, Attribute::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002918 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002919 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002920 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2921 else removeAttribute(~0, Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00002922 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002923
2924 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002925 bool onlyReadsMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00002926 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002927 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002928 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002929 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2930 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00002931 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002932
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002933 /// @brief Determine if the call cannot return.
Nick Lewyckyb9933b82010-07-06 03:53:22 +00002934 bool doesNotReturn() const { return paramHasAttr(~0, Attribute::NoReturn); }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002935 void setDoesNotReturn(bool DoesNotReturn = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002936 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2937 else removeAttribute(~0, Attribute::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00002938 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002939
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002940 /// @brief Determine if the call cannot unwind.
Nick Lewyckyb9933b82010-07-06 03:53:22 +00002941 bool doesNotThrow() const { return paramHasAttr(~0, Attribute::NoUnwind); }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002942 void setDoesNotThrow(bool DoesNotThrow = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002943 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2944 else removeAttribute(~0, Attribute::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00002945 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002946
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002947 /// @brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00002948 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002949 bool hasStructRetAttr() const {
2950 // Be friendly and also check the callee.
Devang Patel05988662008-09-25 21:00:45 +00002951 return paramHasAttr(1, Attribute::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002952 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002953
Dan Gohmanf2752502008-09-26 21:38:45 +00002954 /// @brief Determine if any call argument is an aggregate passed by value.
2955 bool hasByValArgument() const {
2956 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2957 }
2958
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002959 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00002960 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002961 ///
Chris Lattner721aef62004-11-18 17:46:57 +00002962 Function *getCalledFunction() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00002963 return dyn_cast<Function>(Op<-3>());
Chris Lattner721aef62004-11-18 17:46:57 +00002964 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002965
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002966 /// getCalledValue - Get a pointer to the function that is invoked by this
Dan Gohmanf2752502008-09-26 21:38:45 +00002967 /// instruction
Gabor Greifc9f75002010-03-24 13:21:49 +00002968 const Value *getCalledValue() const { return Op<-3>(); }
2969 Value *getCalledValue() { return Op<-3>(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002970
Gabor Greif654c06f2010-03-20 21:00:25 +00002971 /// setCalledFunction - Set the function called.
2972 void setCalledFunction(Value* Fn) {
Gabor Greifc9f75002010-03-24 13:21:49 +00002973 Op<-3>() = Fn;
Gabor Greif654c06f2010-03-20 21:00:25 +00002974 }
2975
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002976 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00002977 BasicBlock *getNormalDest() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00002978 return cast<BasicBlock>(Op<-2>());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002979 }
Chris Lattner454928e2005-01-29 00:31:36 +00002980 BasicBlock *getUnwindDest() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00002981 return cast<BasicBlock>(Op<-1>());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002982 }
Chris Lattner454928e2005-01-29 00:31:36 +00002983 void setNormalDest(BasicBlock *B) {
Gabor Greifc9f75002010-03-24 13:21:49 +00002984 Op<-2>() = reinterpret_cast<Value*>(B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002985 }
Chris Lattner454928e2005-01-29 00:31:36 +00002986 void setUnwindDest(BasicBlock *B) {
Gabor Greifc9f75002010-03-24 13:21:49 +00002987 Op<-1>() = reinterpret_cast<Value*>(B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002988 }
2989
Bill Wendlinge6e88262011-08-12 20:24:12 +00002990 /// getLandingPadInst - Get the landingpad instruction from the landing pad
2991 /// block (the unwind destination).
2992 LandingPadInst *getLandingPadInst() const;
2993
Devang Patel4d4a5e02008-02-23 01:11:02 +00002994 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002995 assert(i < 2 && "Successor # out of range for invoke!");
2996 return i == 0 ? getNormalDest() : getUnwindDest();
2997 }
2998
Chris Lattner454928e2005-01-29 00:31:36 +00002999 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003000 assert(idx < 2 && "Successor # out of range for invoke!");
Gabor Greifc9f75002010-03-24 13:21:49 +00003001 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003002 }
3003
Chris Lattner454928e2005-01-29 00:31:36 +00003004 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003005
3006 // Methods for support type inquiry through isa, cast, and dyn_cast:
3007 static inline bool classof(const InvokeInst *) { return true; }
3008 static inline bool classof(const Instruction *I) {
3009 return (I->getOpcode() == Instruction::Invoke);
3010 }
3011 static inline bool classof(const Value *V) {
3012 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3013 }
Gabor Greifc9f75002010-03-24 13:21:49 +00003014
Chris Lattner454928e2005-01-29 00:31:36 +00003015private:
3016 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3017 virtual unsigned getNumSuccessorsV() const;
3018 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00003019
Chris Lattnerb2406d92009-12-29 02:46:09 +00003020 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3021 // method so that subclasses cannot accidentally use it.
3022 void setInstructionSubclassData(unsigned short D) {
3023 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00003024 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003025};
3026
Gabor Greifefe65362008-05-10 08:32:32 +00003027template <>
Jay Foad67c619b2011-01-11 15:07:38 +00003028struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00003029};
3030
Gabor Greifefe65362008-05-10 08:32:32 +00003031InvokeInst::InvokeInst(Value *Func,
3032 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00003033 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003034 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00003035 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3036 ->getElementType())->getReturnType(),
3037 Instruction::Invoke,
3038 OperandTraits<InvokeInst>::op_end(this) - Values,
3039 Values, InsertBefore) {
Jay Foada3efbb12011-07-15 08:37:34 +00003040 init(Func, IfNormal, IfException, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00003041}
Gabor Greifefe65362008-05-10 08:32:32 +00003042InvokeInst::InvokeInst(Value *Func,
3043 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00003044 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003045 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00003046 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3047 ->getElementType())->getReturnType(),
3048 Instruction::Invoke,
3049 OperandTraits<InvokeInst>::op_end(this) - Values,
3050 Values, InsertAtEnd) {
Jay Foada3efbb12011-07-15 08:37:34 +00003051 init(Func, IfNormal, IfException, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00003052}
3053
3054DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003055
3056//===----------------------------------------------------------------------===//
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003057// ResumeInst Class
3058//===----------------------------------------------------------------------===//
3059
3060//===---------------------------------------------------------------------------
3061/// ResumeInst - Resume the propagation of an exception.
3062///
3063class ResumeInst : public TerminatorInst {
3064 ResumeInst(const ResumeInst &RI);
3065
3066 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3067 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3068protected:
3069 virtual ResumeInst *clone_impl() const;
3070public:
3071 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3072 return new(1) ResumeInst(Exn, InsertBefore);
3073 }
3074 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3075 return new(1) ResumeInst(Exn, InsertAtEnd);
3076 }
3077
3078 /// Provide fast operand accessors
3079 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3080
3081 /// Convenience accessor.
3082 Value *getValue() const { return Op<0>(); }
3083
3084 unsigned getNumSuccessors() const { return 0; }
3085
3086 // Methods for support type inquiry through isa, cast, and dyn_cast:
3087 static inline bool classof(const ResumeInst *) { return true; }
3088 static inline bool classof(const Instruction *I) {
3089 return I->getOpcode() == Instruction::Resume;
3090 }
3091 static inline bool classof(const Value *V) {
3092 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3093 }
3094private:
3095 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3096 virtual unsigned getNumSuccessorsV() const;
3097 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3098};
3099
3100template <>
3101struct OperandTraits<ResumeInst> :
3102 public FixedNumOperandTraits<ResumeInst, 1> {
3103};
3104
3105DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3106
3107//===----------------------------------------------------------------------===//
Chris Lattner076b3f12004-10-16 18:05:54 +00003108// UnreachableInst Class
3109//===----------------------------------------------------------------------===//
3110
3111//===---------------------------------------------------------------------------
3112/// UnreachableInst - This function has undefined behavior. In particular, the
3113/// presence of this instruction indicates some higher level knowledge that the
3114/// end of the block cannot be reached.
3115///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00003116class UnreachableInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00003117 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Devang Patel50b6e332009-10-27 22:16:29 +00003118protected:
3119 virtual UnreachableInst *clone_impl() const;
3120
Chris Lattner1fca5ff2004-10-27 16:14:51 +00003121public:
Gabor Greif051a9502008-04-06 20:25:17 +00003122 // allocate space for exactly zero operands
3123 void *operator new(size_t s) {
3124 return User::operator new(s, 0);
3125 }
Owen Anderson1d0be152009-08-13 21:58:54 +00003126 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3127 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00003128
Chris Lattner454928e2005-01-29 00:31:36 +00003129 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00003130
3131 // Methods for support type inquiry through isa, cast, and dyn_cast:
3132 static inline bool classof(const UnreachableInst *) { return true; }
3133 static inline bool classof(const Instruction *I) {
3134 return I->getOpcode() == Instruction::Unreachable;
3135 }
3136 static inline bool classof(const Value *V) {
3137 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3138 }
Chris Lattner454928e2005-01-29 00:31:36 +00003139private:
3140 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3141 virtual unsigned getNumSuccessorsV() const;
3142 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00003143};
3144
Reid Spencer3da59db2006-11-27 01:05:10 +00003145//===----------------------------------------------------------------------===//
3146// TruncInst Class
3147//===----------------------------------------------------------------------===//
3148
3149/// @brief This class represents a truncation of integer types.
3150class TruncInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003151protected:
3152 /// @brief Clone an identical TruncInst
3153 virtual TruncInst *clone_impl() const;
3154
Reid Spencer3da59db2006-11-27 01:05:10 +00003155public:
3156 /// @brief Constructor with insert-before-instruction semantics
3157 TruncInst(
3158 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003159 Type *Ty, ///< The (smaller) type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003160 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003161 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3162 );
3163
3164 /// @brief Constructor with insert-at-end-of-block semantics
3165 TruncInst(
3166 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003167 Type *Ty, ///< The (smaller) type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003168 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003169 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3170 );
3171
Reid Spencer3da59db2006-11-27 01:05:10 +00003172 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3173 static inline bool classof(const TruncInst *) { return true; }
3174 static inline bool classof(const Instruction *I) {
3175 return I->getOpcode() == Trunc;
3176 }
3177 static inline bool classof(const Value *V) {
3178 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3179 }
3180};
3181
3182//===----------------------------------------------------------------------===//
3183// ZExtInst Class
3184//===----------------------------------------------------------------------===//
3185
3186/// @brief This class represents zero extension of integer types.
3187class ZExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003188protected:
3189 /// @brief Clone an identical ZExtInst
3190 virtual ZExtInst *clone_impl() const;
3191
Reid Spencer3da59db2006-11-27 01:05:10 +00003192public:
3193 /// @brief Constructor with insert-before-instruction semantics
3194 ZExtInst(
3195 Value *S, ///< The value to be zero extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003196 Type *Ty, ///< The type to zero extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003197 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003198 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3199 );
3200
3201 /// @brief Constructor with insert-at-end semantics.
3202 ZExtInst(
3203 Value *S, ///< The value to be zero extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003204 Type *Ty, ///< The type to zero extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003205 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003206 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3207 );
3208
Reid Spencer3da59db2006-11-27 01:05:10 +00003209 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3210 static inline bool classof(const ZExtInst *) { return true; }
3211 static inline bool classof(const Instruction *I) {
3212 return I->getOpcode() == ZExt;
3213 }
3214 static inline bool classof(const Value *V) {
3215 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3216 }
3217};
3218
3219//===----------------------------------------------------------------------===//
3220// SExtInst Class
3221//===----------------------------------------------------------------------===//
3222
3223/// @brief This class represents a sign extension of integer types.
3224class SExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003225protected:
3226 /// @brief Clone an identical SExtInst
3227 virtual SExtInst *clone_impl() const;
3228
Reid Spencer3da59db2006-11-27 01:05:10 +00003229public:
3230 /// @brief Constructor with insert-before-instruction semantics
3231 SExtInst(
3232 Value *S, ///< The value to be sign extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003233 Type *Ty, ///< The type to sign extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003234 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003235 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3236 );
3237
3238 /// @brief Constructor with insert-at-end-of-block semantics
3239 SExtInst(
3240 Value *S, ///< The value to be sign extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003241 Type *Ty, ///< The type to sign extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003242 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003243 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3244 );
3245
Reid Spencer3da59db2006-11-27 01:05:10 +00003246 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3247 static inline bool classof(const SExtInst *) { return true; }
3248 static inline bool classof(const Instruction *I) {
3249 return I->getOpcode() == SExt;
3250 }
3251 static inline bool classof(const Value *V) {
3252 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3253 }
3254};
3255
3256//===----------------------------------------------------------------------===//
3257// FPTruncInst Class
3258//===----------------------------------------------------------------------===//
3259
3260/// @brief This class represents a truncation of floating point types.
3261class FPTruncInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003262protected:
3263 /// @brief Clone an identical FPTruncInst
3264 virtual FPTruncInst *clone_impl() const;
3265
Reid Spencer3da59db2006-11-27 01:05:10 +00003266public:
3267 /// @brief Constructor with insert-before-instruction semantics
3268 FPTruncInst(
3269 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003270 Type *Ty, ///< The type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003271 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003272 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3273 );
3274
3275 /// @brief Constructor with insert-before-instruction semantics
3276 FPTruncInst(
3277 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003278 Type *Ty, ///< The type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003279 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003280 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3281 );
3282
Reid Spencer3da59db2006-11-27 01:05:10 +00003283 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3284 static inline bool classof(const FPTruncInst *) { return true; }
3285 static inline bool classof(const Instruction *I) {
3286 return I->getOpcode() == FPTrunc;
3287 }
3288 static inline bool classof(const Value *V) {
3289 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3290 }
3291};
3292
3293//===----------------------------------------------------------------------===//
3294// FPExtInst Class
3295//===----------------------------------------------------------------------===//
3296
3297/// @brief This class represents an extension of floating point types.
3298class FPExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003299protected:
3300 /// @brief Clone an identical FPExtInst
3301 virtual FPExtInst *clone_impl() const;
3302
Reid Spencer3da59db2006-11-27 01:05:10 +00003303public:
3304 /// @brief Constructor with insert-before-instruction semantics
3305 FPExtInst(
3306 Value *S, ///< The value to be extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003307 Type *Ty, ///< The type to extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003308 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003309 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3310 );
3311
3312 /// @brief Constructor with insert-at-end-of-block semantics
3313 FPExtInst(
3314 Value *S, ///< The value to be extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003315 Type *Ty, ///< The type to extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003316 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003317 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3318 );
3319
Reid Spencer3da59db2006-11-27 01:05:10 +00003320 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3321 static inline bool classof(const FPExtInst *) { return true; }
3322 static inline bool classof(const Instruction *I) {
3323 return I->getOpcode() == FPExt;
3324 }
3325 static inline bool classof(const Value *V) {
3326 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3327 }
3328};
3329
3330//===----------------------------------------------------------------------===//
3331// UIToFPInst Class
3332//===----------------------------------------------------------------------===//
3333
3334/// @brief This class represents a cast unsigned integer to floating point.
3335class UIToFPInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003336protected:
3337 /// @brief Clone an identical UIToFPInst
3338 virtual UIToFPInst *clone_impl() const;
3339
Reid Spencer3da59db2006-11-27 01:05:10 +00003340public:
3341 /// @brief Constructor with insert-before-instruction semantics
3342 UIToFPInst(
3343 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003344 Type *Ty, ///< The type to convert 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 UIToFPInst(
3351 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003352 Type *Ty, ///< The type to convert 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:
3358 static inline bool classof(const UIToFPInst *) { return true; }
3359 static inline bool classof(const Instruction *I) {
3360 return I->getOpcode() == UIToFP;
3361 }
3362 static inline bool classof(const Value *V) {
3363 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3364 }
3365};
3366
3367//===----------------------------------------------------------------------===//
3368// SIToFPInst Class
3369//===----------------------------------------------------------------------===//
3370
3371/// @brief This class represents a cast from signed integer to floating point.
3372class SIToFPInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003373protected:
3374 /// @brief Clone an identical SIToFPInst
3375 virtual SIToFPInst *clone_impl() const;
3376
Reid Spencer3da59db2006-11-27 01:05:10 +00003377public:
3378 /// @brief Constructor with insert-before-instruction semantics
3379 SIToFPInst(
3380 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003381 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003382 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003383 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3384 );
3385
3386 /// @brief Constructor with insert-at-end-of-block semantics
3387 SIToFPInst(
3388 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003389 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003390 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003391 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3392 );
3393
Reid Spencer3da59db2006-11-27 01:05:10 +00003394 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3395 static inline bool classof(const SIToFPInst *) { return true; }
3396 static inline bool classof(const Instruction *I) {
3397 return I->getOpcode() == SIToFP;
3398 }
3399 static inline bool classof(const Value *V) {
3400 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3401 }
3402};
3403
3404//===----------------------------------------------------------------------===//
3405// FPToUIInst Class
3406//===----------------------------------------------------------------------===//
3407
3408/// @brief This class represents a cast from floating point to unsigned integer
3409class FPToUIInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003410protected:
3411 /// @brief Clone an identical FPToUIInst
3412 virtual FPToUIInst *clone_impl() const;
3413
Reid Spencer3da59db2006-11-27 01:05:10 +00003414public:
3415 /// @brief Constructor with insert-before-instruction semantics
3416 FPToUIInst(
3417 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003418 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003419 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003420 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3421 );
3422
3423 /// @brief Constructor with insert-at-end-of-block semantics
3424 FPToUIInst(
3425 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003426 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003427 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003428 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3429 );
3430
Reid Spencer3da59db2006-11-27 01:05:10 +00003431 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3432 static inline bool classof(const FPToUIInst *) { return true; }
3433 static inline bool classof(const Instruction *I) {
3434 return I->getOpcode() == FPToUI;
3435 }
3436 static inline bool classof(const Value *V) {
3437 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3438 }
3439};
3440
3441//===----------------------------------------------------------------------===//
3442// FPToSIInst Class
3443//===----------------------------------------------------------------------===//
3444
3445/// @brief This class represents a cast from floating point to signed integer.
3446class FPToSIInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003447protected:
3448 /// @brief Clone an identical FPToSIInst
3449 virtual FPToSIInst *clone_impl() const;
3450
Reid Spencer3da59db2006-11-27 01:05:10 +00003451public:
3452 /// @brief Constructor with insert-before-instruction semantics
3453 FPToSIInst(
3454 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003455 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003456 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003457 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3458 );
3459
3460 /// @brief Constructor with insert-at-end-of-block semantics
3461 FPToSIInst(
3462 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003463 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003464 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003465 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3466 );
3467
Reid Spencer3da59db2006-11-27 01:05:10 +00003468 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3469 static inline bool classof(const FPToSIInst *) { return true; }
3470 static inline bool classof(const Instruction *I) {
3471 return I->getOpcode() == FPToSI;
3472 }
3473 static inline bool classof(const Value *V) {
3474 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3475 }
3476};
3477
3478//===----------------------------------------------------------------------===//
3479// IntToPtrInst Class
3480//===----------------------------------------------------------------------===//
3481
3482/// @brief This class represents a cast from an integer to a pointer.
3483class IntToPtrInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00003484public:
3485 /// @brief Constructor with insert-before-instruction semantics
3486 IntToPtrInst(
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 IntToPtrInst(
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
3501 /// @brief Clone an identical IntToPtrInst
Devang Patel50b6e332009-10-27 22:16:29 +00003502 virtual IntToPtrInst *clone_impl() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00003503
3504 // Methods for support type inquiry through isa, cast, and dyn_cast:
3505 static inline bool classof(const IntToPtrInst *) { return true; }
3506 static inline bool classof(const Instruction *I) {
3507 return I->getOpcode() == IntToPtr;
3508 }
3509 static inline bool classof(const Value *V) {
3510 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3511 }
3512};
3513
3514//===----------------------------------------------------------------------===//
3515// PtrToIntInst Class
3516//===----------------------------------------------------------------------===//
3517
3518/// @brief This class represents a cast from a pointer to an integer
3519class PtrToIntInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003520protected:
3521 /// @brief Clone an identical PtrToIntInst
3522 virtual PtrToIntInst *clone_impl() const;
3523
Reid Spencer3da59db2006-11-27 01:05:10 +00003524public:
3525 /// @brief Constructor with insert-before-instruction semantics
3526 PtrToIntInst(
3527 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003528 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003529 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003530 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3531 );
3532
3533 /// @brief Constructor with insert-at-end-of-block semantics
3534 PtrToIntInst(
3535 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003536 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003537 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003538 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3539 );
3540
Reid Spencer3da59db2006-11-27 01:05:10 +00003541 // Methods for support type inquiry through isa, cast, and dyn_cast:
3542 static inline bool classof(const PtrToIntInst *) { return true; }
3543 static inline bool classof(const Instruction *I) {
3544 return I->getOpcode() == PtrToInt;
3545 }
3546 static inline bool classof(const Value *V) {
3547 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3548 }
3549};
3550
3551//===----------------------------------------------------------------------===//
3552// BitCastInst Class
3553//===----------------------------------------------------------------------===//
3554
3555/// @brief This class represents a no-op cast from one type to another.
3556class BitCastInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003557protected:
3558 /// @brief Clone an identical BitCastInst
3559 virtual BitCastInst *clone_impl() const;
3560
Reid Spencer3da59db2006-11-27 01:05:10 +00003561public:
3562 /// @brief Constructor with insert-before-instruction semantics
3563 BitCastInst(
3564 Value *S, ///< The value to be casted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003565 Type *Ty, ///< The type to casted to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003566 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003567 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3568 );
3569
3570 /// @brief Constructor with insert-at-end-of-block semantics
3571 BitCastInst(
3572 Value *S, ///< The value to be casted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003573 Type *Ty, ///< The type to casted to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003574 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003575 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3576 );
3577
Reid Spencer3da59db2006-11-27 01:05:10 +00003578 // Methods for support type inquiry through isa, cast, and dyn_cast:
3579 static inline bool classof(const BitCastInst *) { return true; }
3580 static inline bool classof(const Instruction *I) {
3581 return I->getOpcode() == BitCast;
3582 }
3583 static inline bool classof(const Value *V) {
3584 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3585 }
3586};
3587
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003588} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00003589
3590#endif