blob: bb4ea55c1ae5df0f8ae5cae300af567141d8044c [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
Jay Foadfc6d3a42011-07-13 10:26:04 +000019#include "llvm/ADT/ArrayRef.h"
Dan Gohman81a0c0b2008-05-31 00:58:22 +000020#include "llvm/ADT/SmallVector.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Attributes.h"
22#include "llvm/IR/CallingConv.h"
23#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/InstrTypes.h"
Eli Friedman47f35132011-07-25 23:16:38 +000025#include "llvm/Support/ErrorHandling.h"
Chandler Carruth255f89f2012-12-03 17:02:12 +000026#include "llvm/Support/IntegersSubset.h"
27#include "llvm/Support/IntegersSubsetMapping.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000028#include <iterator>
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000029
30namespace llvm {
31
Chandler Carruth4ced4ee2012-12-11 10:29:10 +000032class APInt;
Chris Lattnerd1a32602005-02-24 05:32:09 +000033class ConstantInt;
Reid Spencer3da43842007-02-28 22:00:54 +000034class ConstantRange;
Chandler Carruth4ced4ee2012-12-11 10:29:10 +000035class DataLayout;
Benjamin Kramer12ddd402009-08-11 17:45:13 +000036class LLVMContext;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000037
Eli Friedman47f35132011-07-25 23:16:38 +000038enum AtomicOrdering {
39 NotAtomic = 0,
40 Unordered = 1,
41 Monotonic = 2,
42 // Consume = 3, // Not specified yet.
43 Acquire = 4,
44 Release = 5,
45 AcquireRelease = 6,
46 SequentiallyConsistent = 7
47};
48
49enum SynchronizationScope {
50 SingleThread = 0,
51 CrossThread = 1
52};
53
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000054//===----------------------------------------------------------------------===//
Victor Hernandez7b929da2009-10-23 21:09:37 +000055// AllocaInst Class
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000056//===----------------------------------------------------------------------===//
57
Victor Hernandez7b929da2009-10-23 21:09:37 +000058/// AllocaInst - an instruction to allocate memory on the stack
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000059///
Victor Hernandez7b929da2009-10-23 21:09:37 +000060class AllocaInst : public UnaryInstruction {
Devang Patel50b6e332009-10-27 22:16:29 +000061protected:
62 virtual AllocaInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000063public:
Chris Lattnerdb125cf2011-07-18 04:54:35 +000064 explicit AllocaInst(Type *Ty, Value *ArraySize = 0,
Victor Hernandez7b929da2009-10-23 21:09:37 +000065 const Twine &Name = "", Instruction *InsertBefore = 0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000066 AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez7b929da2009-10-23 21:09:37 +000067 const Twine &Name, BasicBlock *InsertAtEnd);
68
Chris Lattnerdb125cf2011-07-18 04:54:35 +000069 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
70 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
Victor Hernandez7b929da2009-10-23 21:09:37 +000071
Chris Lattnerdb125cf2011-07-18 04:54:35 +000072 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez7b929da2009-10-23 21:09:37 +000073 const Twine &Name = "", Instruction *InsertBefore = 0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000074 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez7b929da2009-10-23 21:09:37 +000075 const Twine &Name, BasicBlock *InsertAtEnd);
76
Gabor Greif051a9502008-04-06 20:25:17 +000077 // Out of line virtual method, so the vtable, etc. has a home.
Victor Hernandez7b929da2009-10-23 21:09:37 +000078 virtual ~AllocaInst();
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000079
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000080 /// isArrayAllocation - Return true if there is an allocation size parameter
81 /// to the allocation instruction that is not 1.
82 ///
83 bool isArrayAllocation() const;
84
Dan Gohman18476ee2009-07-07 20:47:48 +000085 /// getArraySize - Get the number of elements allocated. For a simple
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000086 /// allocation of a single element, this will return a constant 1 value.
87 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000088 const Value *getArraySize() const { return getOperand(0); }
89 Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000090
91 /// getType - Overload to return most specific pointer type
92 ///
Chris Lattnerdb125cf2011-07-18 04:54:35 +000093 PointerType *getType() const {
94 return reinterpret_cast<PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000095 }
96
97 /// getAllocatedType - Return the type that is being allocated by the
98 /// instruction.
99 ///
Chris Lattner1afcace2011-07-09 17:41:24 +0000100 Type *getAllocatedType() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000101
Nate Begeman14b05292005-11-05 09:21:28 +0000102 /// getAlignment - Return the alignment of the memory that is being allocated
103 /// by the instruction.
104 ///
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000105 unsigned getAlignment() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000106 return (1u << getSubclassDataFromInstruction()) >> 1;
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000107 }
Dan Gohman52837072008-03-24 16:55:58 +0000108 void setAlignment(unsigned Align);
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000109
Chris Lattnerc5dd22a2008-11-26 02:54:17 +0000110 /// isStaticAlloca - Return true if this alloca is in the entry block of the
111 /// function and is a constant size. If so, the code generator will fold it
112 /// into the prolog/epilog code, so it is basically free.
113 bool isStaticAlloca() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000114
115 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000116 static inline bool classof(const Instruction *I) {
117 return (I->getOpcode() == Instruction::Alloca);
118 }
119 static inline bool classof(const Value *V) {
120 return isa<Instruction>(V) && classof(cast<Instruction>(V));
121 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000122private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000123 // Shadow Instruction::setInstructionSubclassData with a private forwarding
124 // method so that subclasses cannot accidentally use it.
125 void setInstructionSubclassData(unsigned short D) {
126 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000127 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000128};
129
130
131//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000132// LoadInst Class
133//===----------------------------------------------------------------------===//
134
Chris Lattner88fe29a2005-02-05 01:44:18 +0000135/// LoadInst - an instruction for reading from memory. This uses the
136/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000137///
Chris Lattner454928e2005-01-29 00:31:36 +0000138class LoadInst : public UnaryInstruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000139 void AssertOK();
Devang Patel50b6e332009-10-27 22:16:29 +0000140protected:
141 virtual LoadInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000142public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000143 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
144 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000145 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
Christopher Lamb43c7f372007-04-22 19:24:39 +0000146 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000147 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000148 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000149 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Eli Friedman21006d42011-08-09 23:02:53 +0000150 unsigned Align, Instruction *InsertBefore = 0);
151 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000152 unsigned Align, BasicBlock *InsertAtEnd);
Eli Friedman21006d42011-08-09 23:02:53 +0000153 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
154 unsigned Align, AtomicOrdering Order,
155 SynchronizationScope SynchScope = CrossThread,
156 Instruction *InsertBefore = 0);
157 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
158 unsigned Align, AtomicOrdering Order,
159 SynchronizationScope SynchScope,
160 BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000161
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000162 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
163 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
164 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
165 bool isVolatile = false, Instruction *InsertBefore = 0);
166 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
167 BasicBlock *InsertAtEnd);
168
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000169 /// isVolatile - Return true if this is a load from a volatile memory
170 /// location.
171 ///
Chris Lattnerb2406d92009-12-29 02:46:09 +0000172 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000173
174 /// setVolatile - Specify whether this is a volatile load or not.
175 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000176 void setVolatile(bool V) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000177 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
178 (V ? 1 : 0));
Christopher Lamb43c7f372007-04-22 19:24:39 +0000179 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000180
Christopher Lamb43c7f372007-04-22 19:24:39 +0000181 /// getAlignment - Return the alignment of the access that is being performed
182 ///
183 unsigned getAlignment() const {
Eli Friedman21006d42011-08-09 23:02:53 +0000184 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000185 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000186
Christopher Lamb43c7f372007-04-22 19:24:39 +0000187 void setAlignment(unsigned Align);
188
Eli Friedman21006d42011-08-09 23:02:53 +0000189 /// Returns the ordering effect of this fence.
190 AtomicOrdering getOrdering() const {
191 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
192 }
193
194 /// Set the ordering constraint on this load. May not be Release or
195 /// AcquireRelease.
196 void setOrdering(AtomicOrdering Ordering) {
197 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
198 (Ordering << 7));
199 }
200
201 SynchronizationScope getSynchScope() const {
202 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
203 }
204
205 /// Specify whether this load is ordered with respect to all
206 /// concurrently executing threads, or only with respect to signal handlers
207 /// executing in the same thread.
208 void setSynchScope(SynchronizationScope xthread) {
209 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
210 (xthread << 6));
211 }
212
213 bool isAtomic() const { return getOrdering() != NotAtomic; }
214 void setAtomic(AtomicOrdering Ordering,
215 SynchronizationScope SynchScope = CrossThread) {
216 setOrdering(Ordering);
217 setSynchScope(SynchScope);
218 }
219
220 bool isSimple() const { return !isAtomic() && !isVolatile(); }
221 bool isUnordered() const {
222 return getOrdering() <= Unordered && !isVolatile();
223 }
224
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000225 Value *getPointerOperand() { return getOperand(0); }
226 const Value *getPointerOperand() const { return getOperand(0); }
227 static unsigned getPointerOperandIndex() { return 0U; }
228
Chandler Carruthd988b752012-11-01 11:25:28 +0000229 /// \brief Returns the address space of the pointer operand.
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000230 unsigned getPointerAddressSpace() const {
Chandler Carruth8fb614c2012-11-01 09:37:49 +0000231 return getPointerOperand()->getType()->getPointerAddressSpace();
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000232 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000233
234
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000235 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000236 static inline bool classof(const Instruction *I) {
237 return I->getOpcode() == Instruction::Load;
238 }
239 static inline bool classof(const Value *V) {
240 return isa<Instruction>(V) && classof(cast<Instruction>(V));
241 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000242private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000243 // Shadow Instruction::setInstructionSubclassData with a private forwarding
244 // method so that subclasses cannot accidentally use it.
245 void setInstructionSubclassData(unsigned short D) {
246 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000247 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000248};
249
250
251//===----------------------------------------------------------------------===//
252// StoreInst Class
253//===----------------------------------------------------------------------===//
254
Misha Brukman9769ab22005-04-21 20:19:05 +0000255/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000256///
257class StoreInst : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +0000258 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Chris Lattner454928e2005-01-29 00:31:36 +0000259 void AssertOK();
Devang Patel50b6e332009-10-27 22:16:29 +0000260protected:
261 virtual StoreInst *clone_impl() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000262public:
Gabor Greif051a9502008-04-06 20:25:17 +0000263 // allocate space for exactly two operands
264 void *operator new(size_t s) {
265 return User::operator new(s, 2);
266 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000267 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
268 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
269 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
270 Instruction *InsertBefore = 0);
271 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000272 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
Eli Friedman21006d42011-08-09 23:02:53 +0000273 unsigned Align, Instruction *InsertBefore = 0);
274 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
Dan Gohman6ab2d182007-07-18 20:51:11 +0000275 unsigned Align, BasicBlock *InsertAtEnd);
Eli Friedman21006d42011-08-09 23:02:53 +0000276 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
277 unsigned Align, AtomicOrdering Order,
278 SynchronizationScope SynchScope = CrossThread,
279 Instruction *InsertBefore = 0);
280 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
281 unsigned Align, AtomicOrdering Order,
282 SynchronizationScope SynchScope,
283 BasicBlock *InsertAtEnd);
284
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000285
Eli Friedman21006d42011-08-09 23:02:53 +0000286 /// isVolatile - Return true if this is a store to a volatile memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000287 /// location.
288 ///
Chris Lattnerb2406d92009-12-29 02:46:09 +0000289 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000290
Eli Friedman21006d42011-08-09 23:02:53 +0000291 /// setVolatile - Specify whether this is a volatile store or not.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000292 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000293 void setVolatile(bool V) {
Chris Lattnerb2406d92009-12-29 02:46:09 +0000294 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
295 (V ? 1 : 0));
Christopher Lamb43c7f372007-04-22 19:24:39 +0000296 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000297
Chris Lattner454928e2005-01-29 00:31:36 +0000298 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +0000299 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner454928e2005-01-29 00:31:36 +0000300
Christopher Lamb43c7f372007-04-22 19:24:39 +0000301 /// getAlignment - Return the alignment of the access that is being performed
302 ///
303 unsigned getAlignment() const {
Eli Friedman21006d42011-08-09 23:02:53 +0000304 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000305 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000306
Christopher Lamb43c7f372007-04-22 19:24:39 +0000307 void setAlignment(unsigned Align);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000308
Eli Friedman21006d42011-08-09 23:02:53 +0000309 /// Returns the ordering effect of this store.
310 AtomicOrdering getOrdering() const {
311 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
312 }
313
314 /// Set the ordering constraint on this store. May not be Acquire or
315 /// AcquireRelease.
316 void setOrdering(AtomicOrdering Ordering) {
317 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
318 (Ordering << 7));
319 }
320
321 SynchronizationScope getSynchScope() const {
322 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
323 }
324
325 /// Specify whether this store instruction is ordered with respect to all
326 /// concurrently executing threads, or only with respect to signal handlers
327 /// executing in the same thread.
328 void setSynchScope(SynchronizationScope xthread) {
329 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
330 (xthread << 6));
331 }
332
333 bool isAtomic() const { return getOrdering() != NotAtomic; }
334 void setAtomic(AtomicOrdering Ordering,
335 SynchronizationScope SynchScope = CrossThread) {
336 setOrdering(Ordering);
337 setSynchScope(SynchScope);
338 }
339
340 bool isSimple() const { return !isAtomic() && !isVolatile(); }
341 bool isUnordered() const {
342 return getOrdering() <= Unordered && !isVolatile();
343 }
344
Chris Lattner41c9c0e2010-06-26 23:26:37 +0000345 Value *getValueOperand() { return getOperand(0); }
346 const Value *getValueOperand() const { return getOperand(0); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000347
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000348 Value *getPointerOperand() { return getOperand(1); }
349 const Value *getPointerOperand() const { return getOperand(1); }
350 static unsigned getPointerOperandIndex() { return 1U; }
351
Chandler Carruthd988b752012-11-01 11:25:28 +0000352 /// \brief Returns the address space of the pointer operand.
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000353 unsigned getPointerAddressSpace() const {
Chandler Carruth8fb614c2012-11-01 09:37:49 +0000354 return getPointerOperand()->getType()->getPointerAddressSpace();
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000355 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000356
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000357 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000358 static inline bool classof(const Instruction *I) {
359 return I->getOpcode() == Instruction::Store;
360 }
361 static inline bool classof(const Value *V) {
362 return isa<Instruction>(V) && classof(cast<Instruction>(V));
363 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000364private:
Chris Lattnerb2406d92009-12-29 02:46:09 +0000365 // Shadow Instruction::setInstructionSubclassData with a private forwarding
366 // method so that subclasses cannot accidentally use it.
367 void setInstructionSubclassData(unsigned short D) {
368 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000369 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000370};
371
Gabor Greifefe65362008-05-10 08:32:32 +0000372template <>
Jay Foad67c619b2011-01-11 15:07:38 +0000373struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
Gabor Greifefe65362008-05-10 08:32:32 +0000374};
375
376DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000377
378//===----------------------------------------------------------------------===//
Eli Friedman47f35132011-07-25 23:16:38 +0000379// FenceInst Class
380//===----------------------------------------------------------------------===//
381
382/// FenceInst - an instruction for ordering other memory operations
383///
384class FenceInst : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +0000385 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Eli Friedman47f35132011-07-25 23:16:38 +0000386 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
387protected:
388 virtual FenceInst *clone_impl() const;
389public:
390 // allocate space for exactly zero operands
391 void *operator new(size_t s) {
392 return User::operator new(s, 0);
393 }
394
395 // Ordering may only be Acquire, Release, AcquireRelease, or
396 // SequentiallyConsistent.
397 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
398 SynchronizationScope SynchScope = CrossThread,
399 Instruction *InsertBefore = 0);
400 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
401 SynchronizationScope SynchScope,
402 BasicBlock *InsertAtEnd);
403
404 /// Returns the ordering effect of this fence.
405 AtomicOrdering getOrdering() const {
406 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
407 }
408
409 /// Set the ordering constraint on this fence. May only be Acquire, Release,
410 /// AcquireRelease, or SequentiallyConsistent.
411 void setOrdering(AtomicOrdering Ordering) {
Eli Friedman21006d42011-08-09 23:02:53 +0000412 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
413 (Ordering << 1));
Eli Friedman47f35132011-07-25 23:16:38 +0000414 }
415
416 SynchronizationScope getSynchScope() const {
417 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
418 }
419
420 /// Specify whether this fence orders other operations with respect to all
421 /// concurrently executing threads, or only with respect to signal handlers
422 /// executing in the same thread.
423 void setSynchScope(SynchronizationScope xthread) {
424 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
425 xthread);
426 }
427
428 // Methods for support type inquiry through isa, cast, and dyn_cast:
Eli Friedman47f35132011-07-25 23:16:38 +0000429 static inline bool classof(const Instruction *I) {
430 return I->getOpcode() == Instruction::Fence;
431 }
432 static inline bool classof(const Value *V) {
433 return isa<Instruction>(V) && classof(cast<Instruction>(V));
434 }
435private:
436 // Shadow Instruction::setInstructionSubclassData with a private forwarding
437 // method so that subclasses cannot accidentally use it.
438 void setInstructionSubclassData(unsigned short D) {
439 Instruction::setInstructionSubclassData(D);
440 }
441};
442
443//===----------------------------------------------------------------------===//
Eli Friedmanff030482011-07-28 21:48:00 +0000444// AtomicCmpXchgInst Class
445//===----------------------------------------------------------------------===//
446
447/// AtomicCmpXchgInst - an instruction that atomically checks whether a
448/// specified value is in a memory location, and, if it is, stores a new value
449/// there. Returns the value that was loaded.
450///
451class AtomicCmpXchgInst : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +0000452 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Eli Friedmanff030482011-07-28 21:48:00 +0000453 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
454 AtomicOrdering Ordering, SynchronizationScope SynchScope);
455protected:
456 virtual AtomicCmpXchgInst *clone_impl() const;
457public:
458 // allocate space for exactly three operands
459 void *operator new(size_t s) {
460 return User::operator new(s, 3);
461 }
462 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
463 AtomicOrdering Ordering, SynchronizationScope SynchScope,
464 Instruction *InsertBefore = 0);
465 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
466 AtomicOrdering Ordering, SynchronizationScope SynchScope,
467 BasicBlock *InsertAtEnd);
468
469 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
470 /// location.
471 ///
472 bool isVolatile() const {
473 return getSubclassDataFromInstruction() & 1;
474 }
475
476 /// setVolatile - Specify whether this is a volatile cmpxchg.
477 ///
478 void setVolatile(bool V) {
479 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
480 (unsigned)V);
481 }
482
483 /// Transparently provide more efficient getOperand methods.
484 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
485
486 /// Set the ordering constraint on this cmpxchg.
487 void setOrdering(AtomicOrdering Ordering) {
488 assert(Ordering != NotAtomic &&
489 "CmpXchg instructions can only be atomic.");
490 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
491 (Ordering << 2));
492 }
493
494 /// Specify whether this cmpxchg is atomic and orders other operations with
495 /// respect to all concurrently executing threads, or only with respect to
496 /// signal handlers executing in the same thread.
497 void setSynchScope(SynchronizationScope SynchScope) {
498 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
499 (SynchScope << 1));
500 }
501
502 /// Returns the ordering constraint on this cmpxchg.
503 AtomicOrdering getOrdering() const {
504 return AtomicOrdering(getSubclassDataFromInstruction() >> 2);
505 }
506
507 /// Returns whether this cmpxchg is atomic between threads or only within a
508 /// single thread.
509 SynchronizationScope getSynchScope() const {
510 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
511 }
512
513 Value *getPointerOperand() { return getOperand(0); }
514 const Value *getPointerOperand() const { return getOperand(0); }
515 static unsigned getPointerOperandIndex() { return 0U; }
516
517 Value *getCompareOperand() { return getOperand(1); }
518 const Value *getCompareOperand() const { return getOperand(1); }
519
520 Value *getNewValOperand() { return getOperand(2); }
521 const Value *getNewValOperand() const { return getOperand(2); }
522
Chandler Carruthd988b752012-11-01 11:25:28 +0000523 /// \brief Returns the address space of the pointer operand.
Eli Friedmanff030482011-07-28 21:48:00 +0000524 unsigned getPointerAddressSpace() const {
Chandler Carruth8fb614c2012-11-01 09:37:49 +0000525 return getPointerOperand()->getType()->getPointerAddressSpace();
Eli Friedmanff030482011-07-28 21:48:00 +0000526 }
527
528 // Methods for support type inquiry through isa, cast, and dyn_cast:
Eli Friedmanff030482011-07-28 21:48:00 +0000529 static inline bool classof(const Instruction *I) {
530 return I->getOpcode() == Instruction::AtomicCmpXchg;
531 }
532 static inline bool classof(const Value *V) {
533 return isa<Instruction>(V) && classof(cast<Instruction>(V));
534 }
535private:
536 // Shadow Instruction::setInstructionSubclassData with a private forwarding
537 // method so that subclasses cannot accidentally use it.
538 void setInstructionSubclassData(unsigned short D) {
539 Instruction::setInstructionSubclassData(D);
540 }
541};
542
543template <>
544struct OperandTraits<AtomicCmpXchgInst> :
545 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
546};
547
548DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
549
550//===----------------------------------------------------------------------===//
551// AtomicRMWInst Class
552//===----------------------------------------------------------------------===//
553
554/// AtomicRMWInst - an instruction that atomically reads a memory location,
555/// combines it with another value, and then stores the result back. Returns
556/// the old value.
557///
558class AtomicRMWInst : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +0000559 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Eli Friedmanff030482011-07-28 21:48:00 +0000560protected:
561 virtual AtomicRMWInst *clone_impl() const;
562public:
563 /// This enumeration lists the possible modifications atomicrmw can make. In
564 /// the descriptions, 'p' is the pointer to the instruction's memory location,
565 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
566 /// instruction. These instructions always return 'old'.
567 enum BinOp {
568 /// *p = v
569 Xchg,
570 /// *p = old + v
571 Add,
572 /// *p = old - v
573 Sub,
574 /// *p = old & v
575 And,
576 /// *p = ~old & v
577 Nand,
578 /// *p = old | v
579 Or,
580 /// *p = old ^ v
581 Xor,
582 /// *p = old >signed v ? old : v
583 Max,
584 /// *p = old <signed v ? old : v
585 Min,
586 /// *p = old >unsigned v ? old : v
587 UMax,
588 /// *p = old <unsigned v ? old : v
589 UMin,
590
591 FIRST_BINOP = Xchg,
592 LAST_BINOP = UMin,
593 BAD_BINOP
594 };
595
596 // allocate space for exactly two operands
597 void *operator new(size_t s) {
598 return User::operator new(s, 2);
599 }
600 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
601 AtomicOrdering Ordering, SynchronizationScope SynchScope,
602 Instruction *InsertBefore = 0);
603 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
604 AtomicOrdering Ordering, SynchronizationScope SynchScope,
605 BasicBlock *InsertAtEnd);
606
607 BinOp getOperation() const {
608 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
609 }
610
611 void setOperation(BinOp Operation) {
612 unsigned short SubclassData = getSubclassDataFromInstruction();
613 setInstructionSubclassData((SubclassData & 31) |
614 (Operation << 5));
615 }
616
617 /// isVolatile - Return true if this is a RMW on a volatile memory location.
618 ///
619 bool isVolatile() const {
620 return getSubclassDataFromInstruction() & 1;
621 }
622
623 /// setVolatile - Specify whether this is a volatile RMW or not.
624 ///
625 void setVolatile(bool V) {
626 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
627 (unsigned)V);
628 }
629
630 /// Transparently provide more efficient getOperand methods.
631 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
632
633 /// Set the ordering constraint on this RMW.
634 void setOrdering(AtomicOrdering Ordering) {
635 assert(Ordering != NotAtomic &&
636 "atomicrmw instructions can only be atomic.");
Eli Friedman21006d42011-08-09 23:02:53 +0000637 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
Eli Friedmanff030482011-07-28 21:48:00 +0000638 (Ordering << 2));
639 }
640
641 /// Specify whether this RMW orders other operations with respect to all
642 /// concurrently executing threads, or only with respect to signal handlers
643 /// executing in the same thread.
644 void setSynchScope(SynchronizationScope SynchScope) {
645 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
646 (SynchScope << 1));
647 }
648
649 /// Returns the ordering constraint on this RMW.
650 AtomicOrdering getOrdering() const {
Eli Friedman21006d42011-08-09 23:02:53 +0000651 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
Eli Friedmanff030482011-07-28 21:48:00 +0000652 }
653
654 /// Returns whether this RMW is atomic between threads or only within a
655 /// single thread.
656 SynchronizationScope getSynchScope() const {
657 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
658 }
659
660 Value *getPointerOperand() { return getOperand(0); }
661 const Value *getPointerOperand() const { return getOperand(0); }
662 static unsigned getPointerOperandIndex() { return 0U; }
663
664 Value *getValOperand() { return getOperand(1); }
665 const Value *getValOperand() const { return getOperand(1); }
666
Chandler Carruthd988b752012-11-01 11:25:28 +0000667 /// \brief Returns the address space of the pointer operand.
Eli Friedmanff030482011-07-28 21:48:00 +0000668 unsigned getPointerAddressSpace() const {
Chandler Carruth8fb614c2012-11-01 09:37:49 +0000669 return getPointerOperand()->getType()->getPointerAddressSpace();
Eli Friedmanff030482011-07-28 21:48:00 +0000670 }
671
672 // Methods for support type inquiry through isa, cast, and dyn_cast:
Eli Friedmanff030482011-07-28 21:48:00 +0000673 static inline bool classof(const Instruction *I) {
674 return I->getOpcode() == Instruction::AtomicRMW;
675 }
676 static inline bool classof(const Value *V) {
677 return isa<Instruction>(V) && classof(cast<Instruction>(V));
678 }
679private:
680 void Init(BinOp Operation, Value *Ptr, Value *Val,
681 AtomicOrdering Ordering, SynchronizationScope SynchScope);
682 // Shadow Instruction::setInstructionSubclassData with a private forwarding
683 // method so that subclasses cannot accidentally use it.
684 void setInstructionSubclassData(unsigned short D) {
685 Instruction::setInstructionSubclassData(D);
686 }
687};
688
689template <>
690struct OperandTraits<AtomicRMWInst>
691 : public FixedNumOperandTraits<AtomicRMWInst,2> {
692};
693
694DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
695
696//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000697// GetElementPtrInst Class
698//===----------------------------------------------------------------------===//
699
Chris Lattner1afcace2011-07-09 17:41:24 +0000700// checkGEPType - Simple wrapper function to give a better assertion failure
David Greeneb8f74792007-09-04 15:46:09 +0000701// message on bad indexes for a gep instruction.
702//
Chandler Carruth305b5152012-06-20 08:39:33 +0000703inline Type *checkGEPType(Type *Ty) {
David Greeneb8f74792007-09-04 15:46:09 +0000704 assert(Ty && "Invalid GetElementPtrInst indices for type!");
705 return Ty;
706}
707
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000708/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
709/// access elements of arrays and structs
710///
711class GetElementPtrInst : public Instruction {
Gabor Greifefe65362008-05-10 08:32:32 +0000712 GetElementPtrInst(const GetElementPtrInst &GEPI);
Jay Foada9203102011-07-25 09:48:08 +0000713 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000714
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000715 /// Constructors - Create a getelementptr instruction with a base pointer an
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000716 /// list of indices. The first ctor can optionally insert before an existing
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000717 /// instruction, the second appends the new instruction to the specified
718 /// BasicBlock.
Jay Foada9203102011-07-25 09:48:08 +0000719 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
720 unsigned Values, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000721 Instruction *InsertBefore);
Jay Foada9203102011-07-25 09:48:08 +0000722 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
723 unsigned Values, const Twine &NameStr,
724 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +0000725protected:
726 virtual GetElementPtrInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +0000727public:
Jay Foada9203102011-07-25 09:48:08 +0000728 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000729 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000730 Instruction *InsertBefore = 0) {
Jay Foada9203102011-07-25 09:48:08 +0000731 unsigned Values = 1 + unsigned(IdxList.size());
Gabor Greifefe65362008-05-10 08:32:32 +0000732 return new(Values)
Jay Foada9203102011-07-25 09:48:08 +0000733 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000734 }
Jay Foada9203102011-07-25 09:48:08 +0000735 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000736 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000737 BasicBlock *InsertAtEnd) {
Jay Foada9203102011-07-25 09:48:08 +0000738 unsigned Values = 1 + unsigned(IdxList.size());
Gabor Greifefe65362008-05-10 08:32:32 +0000739 return new(Values)
Jay Foada9203102011-07-25 09:48:08 +0000740 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000741 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000742
Dan Gohmane2574d32009-08-11 17:57:01 +0000743 /// Create an "inbounds" getelementptr. See the documentation for the
744 /// "inbounds" flag in LangRef.html for details.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +0000745 static GetElementPtrInst *CreateInBounds(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000746 ArrayRef<Value *> IdxList,
Dan Gohmane2574d32009-08-11 17:57:01 +0000747 const Twine &NameStr = "",
748 Instruction *InsertBefore = 0) {
Jay Foada9203102011-07-25 09:48:08 +0000749 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000750 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000751 return GEP;
752 }
Dan Gohmane2574d32009-08-11 17:57:01 +0000753 static GetElementPtrInst *CreateInBounds(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000754 ArrayRef<Value *> IdxList,
Dan Gohmane2574d32009-08-11 17:57:01 +0000755 const Twine &NameStr,
756 BasicBlock *InsertAtEnd) {
Jay Foada9203102011-07-25 09:48:08 +0000757 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000758 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000759 return GEP;
760 }
761
Gabor Greifefe65362008-05-10 08:32:32 +0000762 /// Transparently provide more efficient getOperand methods.
763 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
764
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000765 // getType - Overload to return most specific pointer type...
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000766 PointerType *getType() const {
767 return reinterpret_cast<PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000768 }
769
Chandler Carruth174fbec2012-11-01 11:25:55 +0000770 /// \brief Returns the address space of this instruction's pointer type.
771 unsigned getAddressSpace() const {
772 // Note that this is always the same as the pointer operand's address space
773 // and that is cheaper to compute, so cheat here.
774 return getPointerAddressSpace();
775 }
776
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000777 /// getIndexedType - Returns the type of the element that would be loaded with
778 /// a load instruction with the specified parameters.
779 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000780 /// Null is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000781 /// pointer type.
782 ///
Jay Foada9203102011-07-25 09:48:08 +0000783 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
784 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
785 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
Misha Brukman9769ab22005-04-21 20:19:05 +0000786
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000787 inline op_iterator idx_begin() { return op_begin()+1; }
788 inline const_op_iterator idx_begin() const { return op_begin()+1; }
789 inline op_iterator idx_end() { return op_end(); }
790 inline const_op_iterator idx_end() const { return op_end(); }
791
792 Value *getPointerOperand() {
793 return getOperand(0);
794 }
795 const Value *getPointerOperand() const {
796 return getOperand(0);
797 }
798 static unsigned getPointerOperandIndex() {
Nadav Rotem16087692011-12-05 06:29:09 +0000799 return 0U; // get index for modifying correct operand.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000800 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +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
Chandler Carruthd988b752012-11-01 11:25:28 +0000808 /// \brief Returns the address space of the pointer operand.
Chandler Carruth15214062012-11-01 10:59:30 +0000809 unsigned getPointerAddressSpace() const {
810 return getPointerOperandType()->getPointerAddressSpace();
811 }
812
Nadav Rotem16087692011-12-05 06:29:09 +0000813 /// GetGEPReturnType - Returns the pointer type returned by the GEP
814 /// instruction, which may be a vector of pointers.
815 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
816 Type *PtrTy = PointerType::get(checkGEPType(
817 getIndexedType(Ptr->getType(), IdxList)),
Chandler Carruth15214062012-11-01 10:59:30 +0000818 Ptr->getType()->getPointerAddressSpace());
Nadav Rotem16087692011-12-05 06:29:09 +0000819 // Vector GEP
820 if (Ptr->getType()->isVectorTy()) {
821 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
822 return VectorType::get(PtrTy, NumElem);
823 }
824
825 // Scalar GEP
826 return PtrTy;
827 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000828
Devang Patel4d4a5e02008-02-23 01:11:02 +0000829 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000830 return getNumOperands() - 1;
831 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000832
Devang Patel4d4a5e02008-02-23 01:11:02 +0000833 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000834 return getNumOperands() > 1;
835 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000836
Chris Lattner6f771d42007-04-14 00:12:57 +0000837 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
838 /// zeros. If so, the result pointer and the first operand have the same
839 /// value, just potentially different types.
840 bool hasAllZeroIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000841
Chris Lattner6b0974c2007-04-27 20:35:56 +0000842 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
843 /// constant integers. If so, the result pointer and the first operand have
844 /// a constant offset between them.
845 bool hasAllConstantIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000846
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000847 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
848 /// See LangRef.html for the meaning of inbounds on a getelementptr.
Nick Lewyckyae05e7d2009-09-27 21:33:04 +0000849 void setIsInBounds(bool b = true);
850
851 /// isInBounds - Determine whether the GEP has the inbounds flag.
852 bool isInBounds() const;
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000853
Chandler Carruth4ced4ee2012-12-11 10:29:10 +0000854 /// \brief Accumulate the constant address offset of this GEP if possible.
855 ///
856 /// This routine accepts an APInt into which it will accumulate the constant
857 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
858 /// all-constant, it returns false and the value of the offset APInt is
859 /// undefined (it is *not* preserved!). The APInt passed into this routine
860 /// must be at least as wide as the IntPtr type for the address space of
861 /// the base GEP pointer.
862 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
863
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000864 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000865 static inline bool classof(const Instruction *I) {
866 return (I->getOpcode() == Instruction::GetElementPtr);
867 }
868 static inline bool classof(const Value *V) {
869 return isa<Instruction>(V) && classof(cast<Instruction>(V));
870 }
871};
872
Gabor Greifefe65362008-05-10 08:32:32 +0000873template <>
Jay Foad67c619b2011-01-11 15:07:38 +0000874struct OperandTraits<GetElementPtrInst> :
875 public VariadicOperandTraits<GetElementPtrInst, 1> {
Gabor Greifefe65362008-05-10 08:32:32 +0000876};
877
Gabor Greifefe65362008-05-10 08:32:32 +0000878GetElementPtrInst::GetElementPtrInst(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000879 ArrayRef<Value *> IdxList,
Gabor Greifefe65362008-05-10 08:32:32 +0000880 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000881 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000882 Instruction *InsertBefore)
Nadav Rotem16087692011-12-05 06:29:09 +0000883 : Instruction(getGEPReturnType(Ptr, IdxList),
Gabor Greifefe65362008-05-10 08:32:32 +0000884 GetElementPtr,
885 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
886 Values, InsertBefore) {
Jay Foada9203102011-07-25 09:48:08 +0000887 init(Ptr, IdxList, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +0000888}
Gabor Greifefe65362008-05-10 08:32:32 +0000889GetElementPtrInst::GetElementPtrInst(Value *Ptr,
Jay Foada9203102011-07-25 09:48:08 +0000890 ArrayRef<Value *> IdxList,
Gabor Greifefe65362008-05-10 08:32:32 +0000891 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000892 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000893 BasicBlock *InsertAtEnd)
Nadav Rotem16087692011-12-05 06:29:09 +0000894 : Instruction(getGEPReturnType(Ptr, IdxList),
Gabor Greifefe65362008-05-10 08:32:32 +0000895 GetElementPtr,
896 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
897 Values, InsertAtEnd) {
Jay Foada9203102011-07-25 09:48:08 +0000898 init(Ptr, IdxList, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +0000899}
900
901
902DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
903
904
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000905//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000906// ICmpInst Class
907//===----------------------------------------------------------------------===//
908
909/// This instruction compares its operands according to the predicate given
Nate Begemanac80ade2008-05-12 19:01:56 +0000910/// to the constructor. It only operates on integers or pointers. The operands
911/// must be identical types.
Chandler Carruthacd01d12012-11-01 10:46:54 +0000912/// \brief Represent an integer comparison operator.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000913class ICmpInst: public CmpInst {
Devang Patel50b6e332009-10-27 22:16:29 +0000914protected:
Chandler Carruthacd01d12012-11-01 10:46:54 +0000915 /// \brief Clone an identical ICmpInst
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +0000916 virtual ICmpInst *clone_impl() const;
Reid Spencer45fb3f32006-11-20 01:22:35 +0000917public:
Chandler Carruthacd01d12012-11-01 10:46:54 +0000918 /// \brief Constructor with insert-before-instruction semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000919 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000920 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +0000921 Predicate pred, ///< The predicate to use for the comparison
922 Value *LHS, ///< The left-hand-side of the expression
923 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000924 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000925 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +0000926 Instruction::ICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000927 InsertBefore) {
928 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
929 pred <= CmpInst::LAST_ICMP_PREDICATE &&
930 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000931 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000932 "Both operands to ICmp instruction are not of the same type!");
933 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000934 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Nadav Rotem16087692011-12-05 06:29:09 +0000935 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000936 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000937 }
938
Chandler Carruthacd01d12012-11-01 10:46:54 +0000939 /// \brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000940 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000941 BasicBlock &InsertAtEnd, ///< Block to insert into.
942 Predicate pred, ///< The predicate to use for the comparison
943 Value *LHS, ///< The left-hand-side of the expression
944 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000945 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000946 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000947 Instruction::ICmp, pred, LHS, RHS, NameStr,
948 &InsertAtEnd) {
949 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
950 pred <= CmpInst::LAST_ICMP_PREDICATE &&
951 "Invalid ICmp predicate value");
952 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
953 "Both operands to ICmp instruction are not of the same type!");
954 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000955 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000956 getOperand(0)->getType()->isPointerTy()) &&
Owen Anderson333c4002009-07-09 23:48:35 +0000957 "Invalid operand types for ICmp instruction");
958 }
959
Chandler Carruthacd01d12012-11-01 10:46:54 +0000960 /// \brief Constructor with no-insertion semantics
Owen Anderson333c4002009-07-09 23:48:35 +0000961 ICmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +0000962 Predicate pred, ///< The predicate to use for the comparison
963 Value *LHS, ///< The left-hand-side of the expression
964 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000965 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000966 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000967 Instruction::ICmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +0000968 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
969 pred <= CmpInst::LAST_ICMP_PREDICATE &&
970 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000971 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000972 "Both operands to ICmp instruction are not of the same type!");
973 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000974 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
Nadav Rotem16087692011-12-05 06:29:09 +0000975 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000976 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000977 }
978
Reid Spencere4d87aa2006-12-23 06:05:41 +0000979 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
980 /// @returns the predicate that would be the result if the operand were
981 /// regarded as signed.
Chandler Carruthacd01d12012-11-01 10:46:54 +0000982 /// \brief Return the signed version of the predicate
Reid Spencere4d87aa2006-12-23 06:05:41 +0000983 Predicate getSignedPredicate() const {
984 return getSignedPredicate(getPredicate());
985 }
986
987 /// This is a static version that you can use without an instruction.
Chandler Carruthacd01d12012-11-01 10:46:54 +0000988 /// \brief Return the signed version of the predicate.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000989 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000990
Nick Lewycky4189a532008-01-28 03:48:02 +0000991 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
992 /// @returns the predicate that would be the result if the operand were
993 /// regarded as unsigned.
Chandler Carruthacd01d12012-11-01 10:46:54 +0000994 /// \brief Return the unsigned version of the predicate
Nick Lewycky4189a532008-01-28 03:48:02 +0000995 Predicate getUnsignedPredicate() const {
996 return getUnsignedPredicate(getPredicate());
997 }
998
999 /// This is a static version that you can use without an instruction.
Chandler Carruthacd01d12012-11-01 10:46:54 +00001000 /// \brief Return the unsigned version of the predicate.
Nick Lewycky4189a532008-01-28 03:48:02 +00001001 static Predicate getUnsignedPredicate(Predicate pred);
1002
Chris Lattnerc2bfadb2007-11-22 23:43:29 +00001003 /// isEquality - Return true if this predicate is either EQ or NE. This also
1004 /// tests for commutativity.
1005 static bool isEquality(Predicate P) {
1006 return P == ICMP_EQ || P == ICMP_NE;
1007 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001008
Chris Lattnerc2bfadb2007-11-22 23:43:29 +00001009 /// isEquality - Return true if this predicate is either EQ or NE. This also
1010 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +00001011 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +00001012 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +00001013 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001014
1015 /// @returns true if the predicate of this ICmpInst is commutative
Chandler Carruthacd01d12012-11-01 10:46:54 +00001016 /// \brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +00001017 bool isCommutative() const { return isEquality(); }
1018
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001019 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +00001020 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +00001021 bool isRelational() const {
1022 return !isEquality();
1023 }
1024
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001025 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +00001026 ///
1027 static bool isRelational(Predicate P) {
1028 return !isEquality(P);
1029 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001030
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001031 /// Initialize a set of values that all satisfy the predicate with C.
Chandler Carruthacd01d12012-11-01 10:46:54 +00001032 /// \brief Make a ConstantRange for a relation with a constant value.
Reid Spencer3da43842007-02-28 22:00:54 +00001033 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1034
Reid Spencer45fb3f32006-11-20 01:22:35 +00001035 /// Exchange the two operands to this instruction in such a way that it does
1036 /// not modify the semantics of the instruction. The predicate value may be
1037 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001038 /// (e.g. ult).
Chandler Carruthacd01d12012-11-01 10:46:54 +00001039 /// \brief Swap operands and adjust predicate.
Reid Spencer45fb3f32006-11-20 01:22:35 +00001040 void swapOperands() {
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001041 setPredicate(getSwappedPredicate());
Gabor Greif94fb68b2008-05-13 22:51:52 +00001042 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +00001043 }
1044
1045 // Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer45fb3f32006-11-20 01:22:35 +00001046 static inline bool classof(const Instruction *I) {
1047 return I->getOpcode() == Instruction::ICmp;
1048 }
1049 static inline bool classof(const Value *V) {
1050 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1051 }
Dan Gohmanf72fb672008-09-09 01:02:47 +00001052
Reid Spencer45fb3f32006-11-20 01:22:35 +00001053};
1054
1055//===----------------------------------------------------------------------===//
1056// FCmpInst Class
1057//===----------------------------------------------------------------------===//
1058
1059/// This instruction compares its operands according to the predicate given
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001060/// to the constructor. It only operates on floating point values or packed
Reid Spencer45fb3f32006-11-20 01:22:35 +00001061/// vectors of floating point values. The operands must be identical types.
Chandler Carruthacd01d12012-11-01 10:46:54 +00001062/// \brief Represents a floating point comparison operator.
Reid Spencer45fb3f32006-11-20 01:22:35 +00001063class FCmpInst: public CmpInst {
Devang Patel50b6e332009-10-27 22:16:29 +00001064protected:
Chandler Carruthacd01d12012-11-01 10:46:54 +00001065 /// \brief Clone an identical FCmpInst
Devang Patel50b6e332009-10-27 22:16:29 +00001066 virtual FCmpInst *clone_impl() const;
Reid Spencer45fb3f32006-11-20 01:22:35 +00001067public:
Chandler Carruthacd01d12012-11-01 10:46:54 +00001068 /// \brief Constructor with insert-before-instruction semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +00001069 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +00001070 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +00001071 Predicate pred, ///< The predicate to use for the comparison
1072 Value *LHS, ///< The left-hand-side of the expression
1073 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001074 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +00001075 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +00001076 Instruction::FCmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +00001077 InsertBefore) {
1078 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1079 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +00001080 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001081 "Both operands to FCmp instruction are not of the same type!");
1082 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001083 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001084 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +00001085 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001086
Chandler Carruthacd01d12012-11-01 10:46:54 +00001087 /// \brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +00001088 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +00001089 BasicBlock &InsertAtEnd, ///< Block to insert into.
1090 Predicate pred, ///< The predicate to use for the comparison
1091 Value *LHS, ///< The left-hand-side of the expression
1092 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001093 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +00001094 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +00001095 Instruction::FCmp, pred, LHS, RHS, NameStr,
1096 &InsertAtEnd) {
1097 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1098 "Invalid FCmp predicate value");
1099 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1100 "Both operands to FCmp instruction are not of the same type!");
1101 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001102 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Owen Anderson333c4002009-07-09 23:48:35 +00001103 "Invalid operand types for FCmp instruction");
1104 }
1105
Chandler Carruthacd01d12012-11-01 10:46:54 +00001106 /// \brief Constructor with no-insertion semantics
Owen Anderson333c4002009-07-09 23:48:35 +00001107 FCmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +00001108 Predicate pred, ///< The predicate to use for the comparison
1109 Value *LHS, ///< The left-hand-side of the expression
1110 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001111 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +00001112 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +00001113 Instruction::FCmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +00001114 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1115 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +00001116 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001117 "Both operands to FCmp instruction are not of the same type!");
1118 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001119 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
Nate Begemanac80ade2008-05-12 19:01:56 +00001120 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +00001121 }
1122
Reid Spencer45fb3f32006-11-20 01:22:35 +00001123 /// @returns true if the predicate of this instruction is EQ or NE.
Chandler Carruthacd01d12012-11-01 10:46:54 +00001124 /// \brief Determine if this is an equality predicate.
Reid Spencer45fb3f32006-11-20 01:22:35 +00001125 bool isEquality() const {
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001126 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1127 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
Reid Spencer45fb3f32006-11-20 01:22:35 +00001128 }
Dan Gohman793df072008-09-16 16:44:00 +00001129
1130 /// @returns true if the predicate of this instruction is commutative.
Chandler Carruthacd01d12012-11-01 10:46:54 +00001131 /// \brief Determine if this is a commutative predicate.
Dan Gohman793df072008-09-16 16:44:00 +00001132 bool isCommutative() const {
1133 return isEquality() ||
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001134 getPredicate() == FCMP_FALSE ||
1135 getPredicate() == FCMP_TRUE ||
1136 getPredicate() == FCMP_ORD ||
1137 getPredicate() == FCMP_UNO;
Dan Gohman793df072008-09-16 16:44:00 +00001138 }
Reid Spencer45fb3f32006-11-20 01:22:35 +00001139
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001140 /// @returns true if the predicate is relational (not EQ or NE).
Chandler Carruthacd01d12012-11-01 10:46:54 +00001141 /// \brief Determine if this a relational predicate.
Reid Spencer45fb3f32006-11-20 01:22:35 +00001142 bool isRelational() const { return !isEquality(); }
1143
1144 /// Exchange the two operands to this instruction in such a way that it does
1145 /// not modify the semantics of the instruction. The predicate value may be
1146 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001147 /// (e.g. ult).
Chandler Carruthacd01d12012-11-01 10:46:54 +00001148 /// \brief Swap operands and adjust predicate.
Reid Spencer45fb3f32006-11-20 01:22:35 +00001149 void swapOperands() {
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001150 setPredicate(getSwappedPredicate());
Gabor Greif94fb68b2008-05-13 22:51:52 +00001151 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +00001152 }
1153
Chandler Carruthacd01d12012-11-01 10:46:54 +00001154 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer45fb3f32006-11-20 01:22:35 +00001155 static inline bool classof(const Instruction *I) {
1156 return I->getOpcode() == Instruction::FCmp;
1157 }
1158 static inline bool classof(const Value *V) {
1159 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1160 }
1161};
1162
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001163//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001164/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +00001165/// machine's calling convention. This class uses low bit of the SubClassData
1166/// field to indicate whether or not this is a tail call. The rest of the bits
1167/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001168///
1169class CallInst : public Instruction {
Bill Wendling99faa3b2012-12-07 23:16:57 +00001170 AttributeSet AttributeList; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001171 CallInst(const CallInst &CI);
Jay Foada3efbb12011-07-15 08:37:34 +00001172 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1173 void init(Value *Func, const Twine &NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001174
Jay Foada3efbb12011-07-15 08:37:34 +00001175 /// Construct a CallInst given a range of arguments.
Chandler Carruthacd01d12012-11-01 10:46:54 +00001176 /// \brief Construct a CallInst from a range of arguments
Jay Foada3efbb12011-07-15 08:37:34 +00001177 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1178 const Twine &NameStr, Instruction *InsertBefore);
David Greene52eec542007-08-01 03:43:44 +00001179
Jay Foada3efbb12011-07-15 08:37:34 +00001180 /// Construct a CallInst given a range of arguments.
Chandler Carruthacd01d12012-11-01 10:46:54 +00001181 /// \brief Construct a CallInst from a range of arguments
Jay Foada3efbb12011-07-15 08:37:34 +00001182 inline CallInst(Value *Func, ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001183 const Twine &NameStr, BasicBlock *InsertAtEnd);
David Greene52eec542007-08-01 03:43:44 +00001184
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001185 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001186 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001187 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001188 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001189 explicit CallInst(Value *F, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001190 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001191 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001192protected:
1193 virtual CallInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001194public:
Gabor Greifefe65362008-05-10 08:32:32 +00001195 static CallInst *Create(Value *Func,
Jay Foada3efbb12011-07-15 08:37:34 +00001196 ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001197 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001198 Instruction *InsertBefore = 0) {
Jay Foada3efbb12011-07-15 08:37:34 +00001199 return new(unsigned(Args.size() + 1))
1200 CallInst(Func, Args, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001201 }
Gabor Greifefe65362008-05-10 08:32:32 +00001202 static CallInst *Create(Value *Func,
Jay Foada3efbb12011-07-15 08:37:34 +00001203 ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001204 const Twine &NameStr, BasicBlock *InsertAtEnd) {
Jay Foada3efbb12011-07-15 08:37:34 +00001205 return new(unsigned(Args.size() + 1))
1206 CallInst(Func, Args, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001207 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001208 static CallInst *Create(Value *F, const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001209 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001210 return new(1) CallInst(F, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001211 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001212 static CallInst *Create(Value *F, const Twine &NameStr,
Evan Cheng34cd4a42008-05-05 18:30:58 +00001213 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001214 return new(1) CallInst(F, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001215 }
Evan Chengfabcb912009-09-10 04:36:43 +00001216 /// CreateMalloc - Generate the IR for a call to malloc:
1217 /// 1. Compute the malloc call's argument as the specified type's size,
1218 /// possibly multiplied by the array size if the array size is not
1219 /// constant 1.
1220 /// 2. Call malloc with that argument.
1221 /// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewycky3fc35c52009-10-17 23:52:26 +00001222 static Instruction *CreateMalloc(Instruction *InsertBefore,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001223 Type *IntPtrTy, Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +00001224 Value *AllocSize, Value *ArraySize = 0,
Chris Lattner5a30a852010-07-12 00:57:28 +00001225 Function* MallocF = 0,
Nick Lewycky3fc35c52009-10-17 23:52:26 +00001226 const Twine &Name = "");
1227 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001228 Type *IntPtrTy, Type *AllocTy,
Victor Hernandez9d0b7042009-11-07 00:16:28 +00001229 Value *AllocSize, Value *ArraySize = 0,
1230 Function* MallocF = 0,
Nick Lewycky3fc35c52009-10-17 23:52:26 +00001231 const Twine &Name = "");
Victor Hernandez66284e02009-10-24 04:23:03 +00001232 /// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner5a30a852010-07-12 00:57:28 +00001233 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
Victor Hernandez66284e02009-10-24 04:23:03 +00001234 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001235
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001236 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001237
Chris Lattnerb2406d92009-12-29 02:46:09 +00001238 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001239 void setTailCall(bool isTC = true) {
Chris Lattnerb2406d92009-12-29 02:46:09 +00001240 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
1241 unsigned(isTC));
Chris Lattner3340ffe2005-05-06 20:26:26 +00001242 }
1243
Dan Gohmanf2752502008-09-26 21:38:45 +00001244 /// Provide fast operand accessors
1245 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001246
Gabor Greif0114b992010-07-31 08:35:21 +00001247 /// getNumArgOperands - Return the number of call arguments.
1248 ///
Bill Wendling22a5b292010-06-07 19:05:06 +00001249 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
Gabor Greif0114b992010-07-31 08:35:21 +00001250
1251 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1252 ///
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001253 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1254 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
Bill Wendling22a5b292010-06-07 19:05:06 +00001255
Chris Lattner3340ffe2005-05-06 20:26:26 +00001256 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1257 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001258 CallingConv::ID getCallingConv() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +00001259 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001260 }
1261 void setCallingConv(CallingConv::ID CC) {
Chris Lattnerb2406d92009-12-29 02:46:09 +00001262 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1263 (static_cast<unsigned>(CC) << 1));
Chris Lattner3340ffe2005-05-06 20:26:26 +00001264 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00001265
Devang Patel05988662008-09-25 21:00:45 +00001266 /// getAttributes - Return the parameter attributes for this call.
Chris Lattner041221c2008-03-13 04:33:03 +00001267 ///
Bill Wendling99faa3b2012-12-07 23:16:57 +00001268 const AttributeSet &getAttributes() const { return AttributeList; }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001269
Dan Gohmanf2752502008-09-26 21:38:45 +00001270 /// setAttributes - Set the parameter attributes for this call.
1271 ///
Bill Wendling99faa3b2012-12-07 23:16:57 +00001272 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001273
Devang Patel05988662008-09-25 21:00:45 +00001274 /// addAttribute - adds the attribute to the list of attributes.
Bill Wendling034b94b2012-12-19 07:18:57 +00001275 void addAttribute(unsigned i, Attribute attr);
Duncan Sandsdc024672007-11-27 13:23:08 +00001276
Devang Patel05988662008-09-25 21:00:45 +00001277 /// removeAttribute - removes the attribute from the list of attributes.
Bill Wendling034b94b2012-12-19 07:18:57 +00001278 void removeAttribute(unsigned i, Attribute attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00001279
Chandler Carruthacd01d12012-11-01 10:46:54 +00001280 /// \brief Determine whether this call has the given attribute.
Bill Wendling629fb822012-12-22 00:37:52 +00001281 bool hasFnAttr(Attribute::AttrKind A) const;
Bill Wendling060f20a2012-10-09 00:28:54 +00001282
Chandler Carruthacd01d12012-11-01 10:46:54 +00001283 /// \brief Determine whether the call or the callee has the given attributes.
Bill Wendling629fb822012-12-22 00:37:52 +00001284 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
Bill Wendling847d1652012-10-03 17:54:26 +00001285
Chandler Carruthacd01d12012-11-01 10:46:54 +00001286 /// \brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001287 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00001288 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001289 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001290
Chandler Carruthacd01d12012-11-01 10:46:54 +00001291 /// \brief Return true if the call should not be inlined.
Bill Wendling034b94b2012-12-19 07:18:57 +00001292 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
Bill Wendling1b005072012-10-09 23:40:31 +00001293 void setIsNoInline() {
Bill Wendling99faa3b2012-12-07 23:16:57 +00001294 addAttribute(AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00001295 Attribute::get(getContext(), Attribute::NoInline));
Eric Christopherf27e6082010-03-25 04:49:10 +00001296 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001297
Chandler Carruthacd01d12012-11-01 10:46:54 +00001298 /// \brief Return true if the call can return twice
Rafael Espindola11f1a832011-10-05 20:05:13 +00001299 bool canReturnTwice() const {
Bill Wendling034b94b2012-12-19 07:18:57 +00001300 return hasFnAttr(Attribute::ReturnsTwice);
Rafael Espindola11f1a832011-10-05 20:05:13 +00001301 }
Bill Wendling1b005072012-10-09 23:40:31 +00001302 void setCanReturnTwice() {
Bill Wendling99faa3b2012-12-07 23:16:57 +00001303 addAttribute(AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00001304 Attribute::get(getContext(), Attribute::ReturnsTwice));
Rafael Espindola11f1a832011-10-05 20:05:13 +00001305 }
1306
Chandler Carruthacd01d12012-11-01 10:46:54 +00001307 /// \brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001308 bool doesNotAccessMemory() const {
Bill Wendling034b94b2012-12-19 07:18:57 +00001309 return hasFnAttr(Attribute::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001310 }
Bill Wendling1b005072012-10-09 23:40:31 +00001311 void setDoesNotAccessMemory() {
Bill Wendling99faa3b2012-12-07 23:16:57 +00001312 addAttribute(AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00001313 Attribute::get(getContext(), Attribute::ReadNone));
Duncan Sands2e033f32008-07-08 08:38:44 +00001314 }
1315
Chandler Carruthacd01d12012-11-01 10:46:54 +00001316 /// \brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001317 bool onlyReadsMemory() const {
Bill Wendling034b94b2012-12-19 07:18:57 +00001318 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001319 }
Bill Wendling1b005072012-10-09 23:40:31 +00001320 void setOnlyReadsMemory() {
Bill Wendling99faa3b2012-12-07 23:16:57 +00001321 addAttribute(AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00001322 Attribute::get(getContext(), Attribute::ReadOnly));
Duncan Sands2e033f32008-07-08 08:38:44 +00001323 }
1324
Chandler Carruthacd01d12012-11-01 10:46:54 +00001325 /// \brief Determine if the call cannot return.
Bill Wendling034b94b2012-12-19 07:18:57 +00001326 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
Bill Wendling1b005072012-10-09 23:40:31 +00001327 void setDoesNotReturn() {
Bill Wendling99faa3b2012-12-07 23:16:57 +00001328 addAttribute(AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00001329 Attribute::get(getContext(), Attribute::NoReturn));
Duncan Sands2e033f32008-07-08 08:38:44 +00001330 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001331
Chandler Carruthacd01d12012-11-01 10:46:54 +00001332 /// \brief Determine if the call cannot unwind.
Bill Wendling034b94b2012-12-19 07:18:57 +00001333 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
Bill Wendling658a8062012-10-10 18:02:57 +00001334 void setDoesNotThrow() {
Bill Wendling99faa3b2012-12-07 23:16:57 +00001335 addAttribute(AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00001336 Attribute::get(getContext(), Attribute::NoUnwind));
Duncan Sands2e033f32008-07-08 08:38:44 +00001337 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001338
James Molloy67ae1352012-12-20 16:04:27 +00001339 /// \brief Determine if the call cannot be duplicated.
1340 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1341 void setCannotDuplicate() {
1342 addAttribute(AttributeSet::FunctionIndex,
1343 Attribute::get(getContext(), Attribute::NoDuplicate));
1344 }
1345
Chandler Carruthacd01d12012-11-01 10:46:54 +00001346 /// \brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00001347 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001348 bool hasStructRetAttr() const {
1349 // Be friendly and also check the callee.
Bill Wendling034b94b2012-12-19 07:18:57 +00001350 return paramHasAttr(1, Attribute::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001351 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001352
Chandler Carruthacd01d12012-11-01 10:46:54 +00001353 /// \brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001354 bool hasByValArgument() const {
Bill Wendling9d30e722012-12-31 00:49:59 +00001355 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001356 }
Evan Chengf4a54982008-01-12 18:57:32 +00001357
Dan Gohmanf2752502008-09-26 21:38:45 +00001358 /// getCalledFunction - Return the function called, or null if this is an
1359 /// indirect function invocation.
1360 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001361 Function *getCalledFunction() const {
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001362 return dyn_cast<Function>(Op<-1>());
Chris Lattner721aef62004-11-18 17:46:57 +00001363 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001364
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001365 /// getCalledValue - Get a pointer to the function that is invoked by this
Chris Lattner14d80382009-10-18 05:08:07 +00001366 /// instruction.
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001367 const Value *getCalledValue() const { return Op<-1>(); }
1368 Value *getCalledValue() { return Op<-1>(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001369
Chris Lattner14d80382009-10-18 05:08:07 +00001370 /// setCalledFunction - Set the function called.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001371 void setCalledFunction(Value* Fn) {
Gabor Greifa6aac4c2010-07-16 09:38:02 +00001372 Op<-1>() = Fn;
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001373 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001374
Owen Anderson6f615f82010-08-07 00:19:59 +00001375 /// isInlineAsm - Check if this call is an inline asm statement.
1376 bool isInlineAsm() const {
1377 return isa<InlineAsm>(Op<-1>());
1378 }
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00001379
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001380 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001381 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001382 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001383 }
1384 static inline bool classof(const Value *V) {
1385 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1386 }
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001387private:
Chris Lattnerb2406d92009-12-29 02:46:09 +00001388 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1389 // method so that subclasses cannot accidentally use it.
1390 void setInstructionSubclassData(unsigned short D) {
1391 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00001392 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001393};
1394
Gabor Greifefe65362008-05-10 08:32:32 +00001395template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001396struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
Gabor Greifefe65362008-05-10 08:32:32 +00001397};
1398
Jay Foada3efbb12011-07-15 08:37:34 +00001399CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001400 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001401 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1402 ->getElementType())->getReturnType(),
1403 Instruction::Call,
Jay Foada3efbb12011-07-15 08:37:34 +00001404 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1405 unsigned(Args.size() + 1), InsertAtEnd) {
1406 init(Func, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00001407}
1408
Jay Foada3efbb12011-07-15 08:37:34 +00001409CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001410 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00001411 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1412 ->getElementType())->getReturnType(),
1413 Instruction::Call,
Jay Foada3efbb12011-07-15 08:37:34 +00001414 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1415 unsigned(Args.size() + 1), InsertBefore) {
1416 init(Func, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00001417}
1418
Gabor Greife9e12152010-07-06 15:44:11 +00001419
1420// Note: if you get compile errors about private methods then
1421// please update your code to use the high-level operand
1422// interfaces. See line 943 above.
Gabor Greifefe65362008-05-10 08:32:32 +00001423DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1424
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001425//===----------------------------------------------------------------------===//
1426// SelectInst Class
1427//===----------------------------------------------------------------------===//
1428
1429/// SelectInst - This class represents the LLVM 'select' instruction.
1430///
1431class SelectInst : public Instruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001432 void init(Value *C, Value *S1, Value *S2) {
Chris Lattnerb76ec322008-12-29 00:12:50 +00001433 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
Gabor Greifefe65362008-05-10 08:32:32 +00001434 Op<0>() = C;
1435 Op<1>() = S1;
1436 Op<2>() = S2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001437 }
1438
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001439 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001440 Instruction *InsertBefore)
1441 : Instruction(S1->getType(), Instruction::Select,
1442 &Op<0>(), 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001443 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001444 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001445 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001446 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001447 BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001448 : Instruction(S1->getType(), Instruction::Select,
1449 &Op<0>(), 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001450 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001451 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001452 }
Devang Patel50b6e332009-10-27 22:16:29 +00001453protected:
1454 virtual SelectInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00001455public:
Evan Chengd69bb1a2008-05-05 17:41:03 +00001456 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001457 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001458 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001459 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001460 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001461 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001462 const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001463 BasicBlock *InsertAtEnd) {
1464 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001465 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001466
Chris Lattner97159122009-09-08 03:32:53 +00001467 const Value *getCondition() const { return Op<0>(); }
1468 const Value *getTrueValue() const { return Op<1>(); }
1469 const Value *getFalseValue() const { return Op<2>(); }
1470 Value *getCondition() { return Op<0>(); }
1471 Value *getTrueValue() { return Op<1>(); }
1472 Value *getFalseValue() { return Op<2>(); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001473
Chris Lattnerb76ec322008-12-29 00:12:50 +00001474 /// areInvalidOperands - Return a string if the specified operands are invalid
1475 /// for a select operation, otherwise return null.
1476 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
Chris Lattner454928e2005-01-29 00:31:36 +00001477
1478 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001479 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001480
1481 OtherOps getOpcode() const {
1482 return static_cast<OtherOps>(Instruction::getOpcode());
1483 }
1484
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001485 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001486 static inline bool classof(const Instruction *I) {
1487 return I->getOpcode() == Instruction::Select;
1488 }
1489 static inline bool classof(const Value *V) {
1490 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1491 }
1492};
1493
Gabor Greifefe65362008-05-10 08:32:32 +00001494template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001495struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001496};
1497
1498DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1499
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001500//===----------------------------------------------------------------------===//
1501// VAArgInst Class
1502//===----------------------------------------------------------------------===//
1503
1504/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001505/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001506///
Chris Lattner454928e2005-01-29 00:31:36 +00001507class VAArgInst : public UnaryInstruction {
Devang Patel50b6e332009-10-27 22:16:29 +00001508protected:
1509 virtual VAArgInst *clone_impl() const;
1510
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001511public:
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001512 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001513 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001514 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001515 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001516 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001517 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001518 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001519 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001520 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001521 }
1522
Dan Gohmane7445412010-09-09 18:32:40 +00001523 Value *getPointerOperand() { return getOperand(0); }
1524 const Value *getPointerOperand() const { return getOperand(0); }
1525 static unsigned getPointerOperandIndex() { return 0U; }
1526
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001527 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001528 static inline bool classof(const Instruction *I) {
1529 return I->getOpcode() == VAArg;
1530 }
1531 static inline bool classof(const Value *V) {
1532 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1533 }
1534};
1535
1536//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001537// ExtractElementInst Class
1538//===----------------------------------------------------------------------===//
1539
1540/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001541/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001542///
1543class ExtractElementInst : public Instruction {
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001544 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
Chris Lattner9fc18d22006-04-08 01:15:18 +00001545 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001546 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
Chris Lattner9fc18d22006-04-08 01:15:18 +00001547 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001548protected:
1549 virtual ExtractElementInst *clone_impl() const;
1550
Eric Christophera3500da2009-07-25 02:28:41 +00001551public:
Eric Christophera3500da2009-07-25 02:28:41 +00001552 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001553 const Twine &NameStr = "",
Eric Christophera3500da2009-07-25 02:28:41 +00001554 Instruction *InsertBefore = 0) {
1555 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1556 }
1557 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001558 const Twine &NameStr,
Eric Christophera3500da2009-07-25 02:28:41 +00001559 BasicBlock *InsertAtEnd) {
1560 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1561 }
Robert Bocchino49b78a52006-01-10 19:04:13 +00001562
Chris Lattnerfa495842006-04-08 04:04:54 +00001563 /// isValidOperands - Return true if an extractelement instruction can be
1564 /// formed with the specified operands.
1565 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001566
Chris Lattner97159122009-09-08 03:32:53 +00001567 Value *getVectorOperand() { return Op<0>(); }
1568 Value *getIndexOperand() { return Op<1>(); }
1569 const Value *getVectorOperand() const { return Op<0>(); }
1570 const Value *getIndexOperand() const { return Op<1>(); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001571
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001572 VectorType *getVectorOperandType() const {
1573 return reinterpret_cast<VectorType*>(getVectorOperand()->getType());
Chris Lattner97159122009-09-08 03:32:53 +00001574 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00001575
1576
Robert Bocchino49b78a52006-01-10 19:04:13 +00001577 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001578 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001579
1580 // Methods for support type inquiry through isa, cast, and dyn_cast:
Robert Bocchino49b78a52006-01-10 19:04:13 +00001581 static inline bool classof(const Instruction *I) {
1582 return I->getOpcode() == Instruction::ExtractElement;
1583 }
1584 static inline bool classof(const Value *V) {
1585 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1586 }
1587};
1588
Gabor Greifefe65362008-05-10 08:32:32 +00001589template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001590struct OperandTraits<ExtractElementInst> :
1591 public FixedNumOperandTraits<ExtractElementInst, 2> {
Gabor Greifefe65362008-05-10 08:32:32 +00001592};
1593
1594DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1595
Robert Bocchino49b78a52006-01-10 19:04:13 +00001596//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001597// InsertElementInst Class
1598//===----------------------------------------------------------------------===//
1599
1600/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001601/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001602///
1603class InsertElementInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001604 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001605 const Twine &NameStr = "",
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001606 Instruction *InsertBefore = 0);
Gabor Greif051a9502008-04-06 20:25:17 +00001607 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001608 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001609protected:
1610 virtual InsertElementInst *clone_impl() const;
1611
Robert Bocchinof9993442006-01-17 20:05:59 +00001612public:
Gabor Greif051a9502008-04-06 20:25:17 +00001613 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001614 const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +00001615 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001616 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001617 }
Gabor Greif051a9502008-04-06 20:25:17 +00001618 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001619 const Twine &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001620 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001621 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001622 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001623
Chris Lattnerfa495842006-04-08 04:04:54 +00001624 /// isValidOperands - Return true if an insertelement instruction can be
1625 /// formed with the specified operands.
1626 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1627 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001628
Reid Spencerac9dcb92007-02-15 03:39:18 +00001629 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001630 ///
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001631 VectorType *getType() const {
1632 return reinterpret_cast<VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001633 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001634
Robert Bocchinof9993442006-01-17 20:05:59 +00001635 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001636 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchinof9993442006-01-17 20:05:59 +00001637
1638 // Methods for support type inquiry through isa, cast, and dyn_cast:
Robert Bocchinof9993442006-01-17 20:05:59 +00001639 static inline bool classof(const Instruction *I) {
1640 return I->getOpcode() == Instruction::InsertElement;
1641 }
1642 static inline bool classof(const Value *V) {
1643 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1644 }
1645};
1646
Gabor Greifefe65362008-05-10 08:32:32 +00001647template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001648struct OperandTraits<InsertElementInst> :
1649 public FixedNumOperandTraits<InsertElementInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001650};
1651
1652DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1653
Robert Bocchinof9993442006-01-17 20:05:59 +00001654//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001655// ShuffleVectorInst Class
1656//===----------------------------------------------------------------------===//
1657
1658/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1659/// input vectors.
1660///
1661class ShuffleVectorInst : public Instruction {
Devang Patel50b6e332009-10-27 22:16:29 +00001662protected:
1663 virtual ShuffleVectorInst *clone_impl() const;
1664
Chris Lattner9fc18d22006-04-08 01:15:18 +00001665public:
Gabor Greif051a9502008-04-06 20:25:17 +00001666 // allocate space for exactly three operands
1667 void *operator new(size_t s) {
1668 return User::operator new(s, 3);
1669 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001670 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001671 const Twine &NameStr = "",
Evan Cheng1bf9a182008-07-24 00:08:56 +00001672 Instruction *InsertBefor = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001673 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001674 const Twine &NameStr, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001675
Chris Lattnerfa495842006-04-08 04:04:54 +00001676 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001677 /// formed with the specified operands.
1678 static bool isValidOperands(const Value *V1, const Value *V2,
1679 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001680
Reid Spencerac9dcb92007-02-15 03:39:18 +00001681 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001682 ///
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001683 VectorType *getType() const {
1684 return reinterpret_cast<VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001685 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001686
Chris Lattner9fc18d22006-04-08 01:15:18 +00001687 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001688 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001689
Chris Lattner83694a92012-01-25 23:49:49 +00001690 Constant *getMask() const {
1691 return reinterpret_cast<Constant*>(getOperand(2));
1692 }
1693
Chris Lattner8728f192008-03-02 05:28:33 +00001694 /// getMaskValue - Return the index from the shuffle mask for the specified
1695 /// output result. This is either -1 if the element is undef or a number less
1696 /// than 2*numelements.
Chris Lattner56243b82012-01-26 02:51:13 +00001697 static int getMaskValue(Constant *Mask, unsigned i);
1698
1699 int getMaskValue(unsigned i) const {
1700 return getMaskValue(getMask(), i);
1701 }
Chris Lattner83694a92012-01-25 23:49:49 +00001702
1703 /// getShuffleMask - Return the full mask for this instruction, where each
1704 /// element is the element number and undef's are returned as -1.
Chris Lattner56243b82012-01-26 02:51:13 +00001705 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1706
1707 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1708 return getShuffleMask(getMask(), Result);
1709 }
Chris Lattner83694a92012-01-25 23:49:49 +00001710
1711 SmallVector<int, 16> getShuffleMask() const {
1712 SmallVector<int, 16> Mask;
1713 getShuffleMask(Mask);
1714 return Mask;
1715 }
1716
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001717
Chris Lattner9fc18d22006-04-08 01:15:18 +00001718 // Methods for support type inquiry through isa, cast, and dyn_cast:
Chris Lattner9fc18d22006-04-08 01:15:18 +00001719 static inline bool classof(const Instruction *I) {
1720 return I->getOpcode() == Instruction::ShuffleVector;
1721 }
1722 static inline bool classof(const Value *V) {
1723 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1724 }
1725};
1726
Gabor Greifefe65362008-05-10 08:32:32 +00001727template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001728struct OperandTraits<ShuffleVectorInst> :
1729 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001730};
1731
1732DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
Chris Lattner9fc18d22006-04-08 01:15:18 +00001733
1734//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001735// ExtractValueInst Class
1736//===----------------------------------------------------------------------===//
1737
Dan Gohmane2d896f2008-05-15 23:35:32 +00001738/// ExtractValueInst - This instruction extracts a struct member or array
1739/// element value from an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001740///
Gabor Greifd4f268b2008-06-06 20:28:12 +00001741class ExtractValueInst : public UnaryInstruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001742 SmallVector<unsigned, 4> Indices;
1743
Dan Gohman041e2eb2008-05-15 19:50:34 +00001744 ExtractValueInst(const ExtractValueInst &EVI);
Jay Foadfc6d3a42011-07-13 10:26:04 +00001745 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001746
Dan Gohmane2d896f2008-05-15 23:35:32 +00001747 /// Constructors - Create a extractvalue instruction with a base aggregate
1748 /// value and a list of indices. The first ctor can optionally insert before
1749 /// an existing instruction, the second appends the new instruction to the
1750 /// specified BasicBlock.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001751 inline ExtractValueInst(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);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001755 inline ExtractValueInst(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001756 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001757 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001758
Dan Gohman8e640412008-05-31 19:09:47 +00001759 // allocate space for exactly one operand
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001760 void *operator new(size_t s) {
1761 return User::operator new(s, 1);
1762 }
Devang Patel50b6e332009-10-27 22:16:29 +00001763protected:
1764 virtual ExtractValueInst *clone_impl() const;
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001765
Gabor Greifd4f268b2008-06-06 20:28:12 +00001766public:
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001767 static ExtractValueInst *Create(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001768 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001769 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001770 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001771 return new
Jay Foadfc6d3a42011-07-13 10:26:04 +00001772 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001773 }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001774 static ExtractValueInst *Create(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001775 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001776 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001777 BasicBlock *InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001778 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001779 }
1780
Dan Gohman041e2eb2008-05-15 19:50:34 +00001781 /// getIndexedType - Returns the type of the element that would be extracted
1782 /// with an extractvalue instruction with the specified parameters.
1783 ///
Frits van Bommelaf72c622010-12-03 14:54:33 +00001784 /// Null is returned if the indices are invalid for the specified type.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001785 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001786
Owen Anderson5678d6e2008-06-19 17:15:57 +00001787 typedef const unsigned* idx_iterator;
1788 inline idx_iterator idx_begin() const { return Indices.begin(); }
1789 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001790
1791 Value *getAggregateOperand() {
1792 return getOperand(0);
1793 }
1794 const Value *getAggregateOperand() const {
1795 return getOperand(0);
1796 }
1797 static unsigned getAggregateOperandIndex() {
1798 return 0U; // get index for modifying correct operand
1799 }
1800
Jay Foadfc6d3a42011-07-13 10:26:04 +00001801 ArrayRef<unsigned> getIndices() const {
1802 return Indices;
1803 }
1804
1805 unsigned getNumIndices() const {
Bill Wendling67944fc2008-06-05 07:35:27 +00001806 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001807 }
1808
1809 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001810 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001811 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001812
Dan Gohman041e2eb2008-05-15 19:50:34 +00001813 // Methods for support type inquiry through isa, cast, and dyn_cast:
Dan Gohman041e2eb2008-05-15 19:50:34 +00001814 static inline bool classof(const Instruction *I) {
1815 return I->getOpcode() == Instruction::ExtractValue;
1816 }
1817 static inline bool classof(const Value *V) {
1818 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1819 }
1820};
1821
Dan Gohmane4569942008-05-23 00:36:11 +00001822ExtractValueInst::ExtractValueInst(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001823 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001824 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001825 Instruction *InsertBefore)
Jay Foadfc6d3a42011-07-13 10:26:04 +00001826 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
Bill Wendling85f40542008-07-22 07:14:12 +00001827 ExtractValue, Agg, InsertBefore) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001828 init(Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001829}
Dan Gohmane4569942008-05-23 00:36:11 +00001830ExtractValueInst::ExtractValueInst(Value *Agg,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001831 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001832 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001833 BasicBlock *InsertAtEnd)
Jay Foadfc6d3a42011-07-13 10:26:04 +00001834 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
Bill Wendling85f40542008-07-22 07:14:12 +00001835 ExtractValue, Agg, InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001836 init(Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001837}
1838
Dan Gohmane4569942008-05-23 00:36:11 +00001839
Dan Gohman041e2eb2008-05-15 19:50:34 +00001840//===----------------------------------------------------------------------===//
1841// InsertValueInst Class
1842//===----------------------------------------------------------------------===//
1843
Dan Gohmane2d896f2008-05-15 23:35:32 +00001844/// InsertValueInst - This instruction inserts a struct field of array element
1845/// value into an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001846///
1847class InsertValueInst : public Instruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001848 SmallVector<unsigned, 4> Indices;
1849
Craig Topperef1623f2012-09-18 03:25:49 +00001850 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001851 InsertValueInst(const InsertValueInst &IVI);
Jay Foadfc6d3a42011-07-13 10:26:04 +00001852 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001853 const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001854
Dan Gohmane2d896f2008-05-15 23:35:32 +00001855 /// Constructors - Create a insertvalue instruction with a base aggregate
1856 /// value, a value to insert, and a list of indices. The first ctor can
1857 /// optionally insert before an existing instruction, the second appends
1858 /// the new instruction to the specified BasicBlock.
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001859 inline InsertValueInst(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001860 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001861 const Twine &NameStr,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001862 Instruction *InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001863 inline InsertValueInst(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001864 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001865 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001866
1867 /// Constructors - These two constructors are convenience methods because one
1868 /// and two index insertvalue instructions are so common.
1869 InsertValueInst(Value *Agg, Value *Val,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001870 unsigned Idx, const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001871 Instruction *InsertBefore = 0);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001872 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001873 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00001874protected:
1875 virtual InsertValueInst *clone_impl() const;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001876public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001877 // allocate space for exactly two operands
1878 void *operator new(size_t s) {
1879 return User::operator new(s, 2);
1880 }
1881
Mikhail Glushenkovd33b77b2010-10-27 07:39:54 +00001882 static InsertValueInst *Create(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001883 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001884 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001885 Instruction *InsertBefore = 0) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001886 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001887 }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001888 static InsertValueInst *Create(Value *Agg, Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001889 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001890 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001891 BasicBlock *InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001892 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001893 }
1894
Dan Gohman041e2eb2008-05-15 19:50:34 +00001895 /// Transparently provide more efficient getOperand methods.
1896 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1897
Owen Anderson5678d6e2008-06-19 17:15:57 +00001898 typedef const unsigned* idx_iterator;
1899 inline idx_iterator idx_begin() const { return Indices.begin(); }
1900 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001901
1902 Value *getAggregateOperand() {
1903 return getOperand(0);
1904 }
1905 const Value *getAggregateOperand() const {
1906 return getOperand(0);
1907 }
1908 static unsigned getAggregateOperandIndex() {
1909 return 0U; // get index for modifying correct operand
1910 }
1911
1912 Value *getInsertedValueOperand() {
1913 return getOperand(1);
1914 }
1915 const Value *getInsertedValueOperand() const {
1916 return getOperand(1);
1917 }
1918 static unsigned getInsertedValueOperandIndex() {
1919 return 1U; // get index for modifying correct operand
1920 }
1921
Jay Foadfc6d3a42011-07-13 10:26:04 +00001922 ArrayRef<unsigned> getIndices() const {
1923 return Indices;
1924 }
1925
1926 unsigned getNumIndices() const {
Bill Wendling67944fc2008-06-05 07:35:27 +00001927 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001928 }
1929
1930 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001931 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001932 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001933
Dan Gohman041e2eb2008-05-15 19:50:34 +00001934 // Methods for support type inquiry through isa, cast, and dyn_cast:
Dan Gohman041e2eb2008-05-15 19:50:34 +00001935 static inline bool classof(const Instruction *I) {
1936 return I->getOpcode() == Instruction::InsertValue;
1937 }
1938 static inline bool classof(const Value *V) {
1939 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1940 }
1941};
1942
1943template <>
Jay Foad67c619b2011-01-11 15:07:38 +00001944struct OperandTraits<InsertValueInst> :
1945 public FixedNumOperandTraits<InsertValueInst, 2> {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001946};
1947
Dan Gohmane4569942008-05-23 00:36:11 +00001948InsertValueInst::InsertValueInst(Value *Agg,
1949 Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001950 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001951 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001952 Instruction *InsertBefore)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001953 : Instruction(Agg->getType(), InsertValue,
1954 OperandTraits<InsertValueInst>::op_begin(this),
1955 2, InsertBefore) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001956 init(Agg, Val, Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001957}
Dan Gohmane4569942008-05-23 00:36:11 +00001958InsertValueInst::InsertValueInst(Value *Agg,
1959 Value *Val,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001960 ArrayRef<unsigned> Idxs,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001961 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001962 BasicBlock *InsertAtEnd)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001963 : Instruction(Agg->getType(), InsertValue,
1964 OperandTraits<InsertValueInst>::op_begin(this),
1965 2, InsertAtEnd) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001966 init(Agg, Val, Idxs, NameStr);
Dan Gohmane4569942008-05-23 00:36:11 +00001967}
1968
Dan Gohman041e2eb2008-05-15 19:50:34 +00001969DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1970
1971//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001972// PHINode Class
1973//===----------------------------------------------------------------------===//
1974
1975// PHINode - The PHINode class is used to represent the magical mystical PHI
1976// node, that can not exist in nature, but can be synthesized in a computer
1977// scientist's overactive imagination.
1978//
1979class PHINode : public Instruction {
Craig Topperef1623f2012-09-18 03:25:49 +00001980 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Chris Lattner454928e2005-01-29 00:31:36 +00001981 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1982 /// the number actually in use.
1983 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001984 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00001985 // allocate space for exactly zero operands
1986 void *operator new(size_t s) {
1987 return User::operator new(s, 0);
1988 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001989 explicit PHINode(Type *Ty, unsigned NumReservedValues,
Jay Foad3ecfc862011-03-30 11:28:46 +00001990 const Twine &NameStr = "", Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001991 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Jay Foad95c3e482011-06-23 09:09:15 +00001992 ReservedSpace(NumReservedValues) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001993 setName(NameStr);
Jay Foad3ecfc862011-03-30 11:28:46 +00001994 OperandList = allocHungoffUses(ReservedSpace);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001995 }
1996
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001997 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
Jay Foad3ecfc862011-03-30 11:28:46 +00001998 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001999 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Jay Foad95c3e482011-06-23 09:09:15 +00002000 ReservedSpace(NumReservedValues) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002001 setName(NameStr);
Jay Foad3ecfc862011-03-30 11:28:46 +00002002 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattner454928e2005-01-29 00:31:36 +00002003 }
Devang Patel50b6e332009-10-27 22:16:29 +00002004protected:
Jay Foad95c3e482011-06-23 09:09:15 +00002005 // allocHungoffUses - this is more complicated than the generic
2006 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2007 // values and pointers to the incoming blocks, all in one allocation.
2008 Use *allocHungoffUses(unsigned) const;
2009
Devang Patel50b6e332009-10-27 22:16:29 +00002010 virtual PHINode *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002011public:
Jay Foad44991762011-03-30 13:29:06 +00002012 /// Constructors - NumReservedValues is a hint for the number of incoming
2013 /// edges that this phi node will have (use 0 if you really have no idea).
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002014 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
Jay Foad3ecfc862011-03-30 11:28:46 +00002015 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00002016 Instruction *InsertBefore = 0) {
Jay Foad3ecfc862011-03-30 11:28:46 +00002017 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002018 }
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002019 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
Jay Foad3ecfc862011-03-30 11:28:46 +00002020 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2021 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002022 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002023 ~PHINode();
2024
Gabor Greifefe65362008-05-10 08:32:32 +00002025 /// Provide fast operand accessors
2026 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2027
Jay Foad95c3e482011-06-23 09:09:15 +00002028 // Block iterator interface. This provides access to the list of incoming
2029 // basic blocks, which parallels the list of incoming values.
2030
2031 typedef BasicBlock **block_iterator;
2032 typedef BasicBlock * const *const_block_iterator;
2033
2034 block_iterator block_begin() {
2035 Use::UserRef *ref =
2036 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2037 return reinterpret_cast<block_iterator>(ref + 1);
2038 }
2039
2040 const_block_iterator block_begin() const {
2041 const Use::UserRef *ref =
2042 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2043 return reinterpret_cast<const_block_iterator>(ref + 1);
2044 }
2045
2046 block_iterator block_end() {
2047 return block_begin() + getNumOperands();
2048 }
2049
2050 const_block_iterator block_end() const {
2051 return block_begin() + getNumOperands();
2052 }
2053
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002054 /// getNumIncomingValues - Return the number of incoming edges
2055 ///
Jay Foad95c3e482011-06-23 09:09:15 +00002056 unsigned getNumIncomingValues() const { return getNumOperands(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002057
Reid Spencerc773de62006-05-19 19:07:54 +00002058 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002059 ///
2060 Value *getIncomingValue(unsigned i) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002061 return getOperand(i);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002062 }
2063 void setIncomingValue(unsigned i, Value *V) {
Jay Foad95c3e482011-06-23 09:09:15 +00002064 setOperand(i, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002065 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00002066 static unsigned getOperandNumForIncomingValue(unsigned i) {
Jay Foad95c3e482011-06-23 09:09:15 +00002067 return i;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002068 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00002069 static unsigned getIncomingValueNumForOperand(unsigned i) {
Jay Foad95c3e482011-06-23 09:09:15 +00002070 return i;
Dan Gohmanb45088c2009-03-23 15:48:29 +00002071 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002072
Dan Gohmanfb76fe02010-02-22 04:10:52 +00002073 /// getIncomingBlock - Return incoming basic block number @p i.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002074 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00002075 BasicBlock *getIncomingBlock(unsigned i) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002076 return block_begin()[i];
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002077 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002078
Chris Lattnerceaa4572009-10-10 07:42:42 +00002079 /// getIncomingBlock - Return incoming basic block corresponding
2080 /// to an operand of the PHI.
2081 ///
2082 BasicBlock *getIncomingBlock(const Use &U) const {
2083 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
Jay Foad95c3e482011-06-23 09:09:15 +00002084 return getIncomingBlock(unsigned(&U - op_begin()));
Chris Lattnerceaa4572009-10-10 07:42:42 +00002085 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002086
Chris Lattnerceaa4572009-10-10 07:42:42 +00002087 /// getIncomingBlock - Return incoming basic block corresponding
2088 /// to value use iterator.
2089 ///
2090 template <typename U>
2091 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
2092 return getIncomingBlock(I.getUse());
2093 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002094
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002095 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Jay Foad95c3e482011-06-23 09:09:15 +00002096 block_begin()[i] = BB;
Dan Gohmanb45088c2009-03-23 15:48:29 +00002097 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002098
2099 /// addIncoming - Add an incoming value to the end of the PHI list
2100 ///
2101 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00002102 assert(V && "PHI node got a null value!");
2103 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002104 assert(getType() == V->getType() &&
2105 "All operands to PHI node must be the same type as the PHI node!");
Jay Foad95c3e482011-06-23 09:09:15 +00002106 if (NumOperands == ReservedSpace)
Jay Foad8891ed72011-04-01 08:00:58 +00002107 growOperands(); // Get more space!
Chris Lattner454928e2005-01-29 00:31:36 +00002108 // Initialize some new operands.
Jay Foad95c3e482011-06-23 09:09:15 +00002109 ++NumOperands;
2110 setIncomingValue(NumOperands - 1, V);
2111 setIncomingBlock(NumOperands - 1, BB);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002112 }
Misha Brukman9769ab22005-04-21 20:19:05 +00002113
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002114 /// removeIncomingValue - Remove an incoming value. This is useful if a
2115 /// predecessor basic block is deleted. The value removed is returned.
2116 ///
2117 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2118 /// is true), the PHI node is destroyed and any uses of it are replaced with
2119 /// dummy values. The only time there should be zero incoming values to a PHI
2120 /// node is when the block is dead, so this strategy is sound.
2121 ///
2122 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2123
Gabor Greifefe65362008-05-10 08:32:32 +00002124 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002125 int Idx = getBasicBlockIndex(BB);
2126 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2127 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2128 }
2129
Misha Brukman9769ab22005-04-21 20:19:05 +00002130 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002131 /// block in the value list for this PHI. Returns -1 if no instance.
2132 ///
2133 int getBasicBlockIndex(const BasicBlock *BB) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002134 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2135 if (block_begin()[i] == BB)
2136 return i;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002137 return -1;
2138 }
2139
2140 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
Jay Foad95c3e482011-06-23 09:09:15 +00002141 int Idx = getBasicBlockIndex(BB);
2142 assert(Idx >= 0 && "Invalid basic block argument!");
2143 return getIncomingValue(Idx);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002144 }
2145
Chris Lattnerf56a8db2006-10-03 17:09:12 +00002146 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00002147 /// same value, return the value, otherwise return null.
Duncan Sandsff103412010-11-17 04:30:22 +00002148 Value *hasConstantValue() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00002149
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002150 /// Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002151 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00002152 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002153 }
2154 static inline bool classof(const Value *V) {
2155 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2156 }
Chris Lattner454928e2005-01-29 00:31:36 +00002157 private:
Jay Foad8891ed72011-04-01 08:00:58 +00002158 void growOperands();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002159};
2160
Gabor Greifefe65362008-05-10 08:32:32 +00002161template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002162struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00002163};
2164
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002165DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002166
Bill Wendlinge6e88262011-08-12 20:24:12 +00002167//===----------------------------------------------------------------------===//
2168// LandingPadInst Class
2169//===----------------------------------------------------------------------===//
2170
2171//===---------------------------------------------------------------------------
2172/// LandingPadInst - The landingpad instruction holds all of the information
2173/// necessary to generate correct exception handling. The landingpad instruction
2174/// cannot be moved from the top of a landing pad block, which itself is
2175/// accessible only from the 'unwind' edge of an invoke. This uses the
2176/// SubclassData field in Value to store whether or not the landingpad is a
2177/// cleanup.
2178///
2179class LandingPadInst : public Instruction {
2180 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2181 /// the number actually in use.
2182 unsigned ReservedSpace;
2183 LandingPadInst(const LandingPadInst &LP);
2184public:
2185 enum ClauseType { Catch, Filter };
2186private:
Craig Topperef1623f2012-09-18 03:25:49 +00002187 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Bill Wendlinge6e88262011-08-12 20:24:12 +00002188 // Allocate space for exactly zero operands.
2189 void *operator new(size_t s) {
2190 return User::operator new(s, 0);
2191 }
2192 void growOperands(unsigned Size);
2193 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2194
2195 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2196 unsigned NumReservedValues, const Twine &NameStr,
2197 Instruction *InsertBefore);
2198 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2199 unsigned NumReservedValues, const Twine &NameStr,
2200 BasicBlock *InsertAtEnd);
2201protected:
2202 virtual LandingPadInst *clone_impl() const;
2203public:
2204 /// Constructors - NumReservedClauses is a hint for the number of incoming
2205 /// clauses that this landingpad will have (use 0 if you really have no idea).
2206 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2207 unsigned NumReservedClauses,
2208 const Twine &NameStr = "",
2209 Instruction *InsertBefore = 0);
2210 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2211 unsigned NumReservedClauses,
2212 const Twine &NameStr, BasicBlock *InsertAtEnd);
2213 ~LandingPadInst();
2214
2215 /// Provide fast operand accessors
2216 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2217
2218 /// getPersonalityFn - Get the personality function associated with this
2219 /// landing pad.
2220 Value *getPersonalityFn() const { return getOperand(0); }
2221
2222 /// isCleanup - Return 'true' if this landingpad instruction is a
2223 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2224 /// doesn't catch the exception.
2225 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2226
2227 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2228 void setCleanup(bool V) {
2229 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2230 (V ? 1 : 0));
2231 }
2232
2233 /// addClause - Add a catch or filter clause to the landing pad.
2234 void addClause(Value *ClauseVal);
2235
2236 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2237 /// to determine what type of clause this is.
2238 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2239
2240 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2241 bool isCatch(unsigned Idx) const {
2242 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2243 }
2244
2245 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2246 bool isFilter(unsigned Idx) const {
2247 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2248 }
2249
2250 /// getNumClauses - Get the number of clauses for this landing pad.
2251 unsigned getNumClauses() const { return getNumOperands() - 1; }
2252
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00002253 /// reserveClauses - Grow the size of the operand list to accommodate the new
Bill Wendlinge6e88262011-08-12 20:24:12 +00002254 /// number of clauses.
2255 void reserveClauses(unsigned Size) { growOperands(Size); }
2256
2257 // Methods for support type inquiry through isa, cast, and dyn_cast:
Bill Wendlinge6e88262011-08-12 20:24:12 +00002258 static inline bool classof(const Instruction *I) {
2259 return I->getOpcode() == Instruction::LandingPad;
2260 }
2261 static inline bool classof(const Value *V) {
2262 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2263 }
2264};
2265
2266template <>
2267struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2268};
2269
2270DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002271
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002272//===----------------------------------------------------------------------===//
2273// ReturnInst Class
2274//===----------------------------------------------------------------------===//
2275
2276//===---------------------------------------------------------------------------
2277/// ReturnInst - Return a value (possibly void), from a function. Execution
2278/// does not continue in this function any longer.
2279///
2280class ReturnInst : public TerminatorInst {
Chris Lattner910c80a2007-02-24 00:55:48 +00002281 ReturnInst(const ReturnInst &RI);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002282
Gabor Greif051a9502008-04-06 20:25:17 +00002283private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002284 // ReturnInst constructors:
2285 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002286 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002287 // ReturnInst(Value* X) - 'ret X' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00002288 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002289 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
Gabor Greifefe65362008-05-10 08:32:32 +00002290 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2291 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002292 //
2293 // NOTE: If the Value* passed is of type void then the constructor behaves as
2294 // if it was passed NULL.
Owen Anderson1d0be152009-08-13 21:58:54 +00002295 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2296 Instruction *InsertBefore = 0);
2297 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2298 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002299protected:
2300 virtual ReturnInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002301public:
Owen Anderson1d0be152009-08-13 21:58:54 +00002302 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2303 Instruction *InsertBefore = 0) {
2304 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002305 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002306 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2307 BasicBlock *InsertAtEnd) {
2308 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002309 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002310 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2311 return new(0) ReturnInst(C, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002312 }
Devang Patel57ef4f42008-02-23 00:35:18 +00002313 virtual ~ReturnInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002314
Gabor Greifefe65362008-05-10 08:32:32 +00002315 /// Provide fast operand accessors
2316 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Devang Patel64d4e612008-02-26 17:56:20 +00002317
Dan Gohman8faa6af2010-10-06 16:59:24 +00002318 /// Convenience accessor. Returns null if there is no return value.
2319 Value *getReturnValue() const {
2320 return getNumOperands() != 0 ? getOperand(0) : 0;
Devang Patel1eafa062008-03-11 17:35:03 +00002321 }
2322
Chris Lattner454928e2005-01-29 00:31:36 +00002323 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002324
2325 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002326 static inline bool classof(const Instruction *I) {
2327 return (I->getOpcode() == Instruction::Ret);
2328 }
2329 static inline bool classof(const Value *V) {
2330 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2331 }
Chris Lattner454928e2005-01-29 00:31:36 +00002332 private:
2333 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2334 virtual unsigned getNumSuccessorsV() const;
2335 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002336};
2337
Gabor Greifefe65362008-05-10 08:32:32 +00002338template <>
Jay Foad67c619b2011-01-11 15:07:38 +00002339struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
Gabor Greifefe65362008-05-10 08:32:32 +00002340};
2341
2342DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002343
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002344//===----------------------------------------------------------------------===//
2345// BranchInst Class
2346//===----------------------------------------------------------------------===//
2347
2348//===---------------------------------------------------------------------------
2349/// BranchInst - Conditional or Unconditional Branch instruction.
2350///
2351class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00002352 /// Ops list - Branches are strange. The operands are ordered:
Gabor Greifae5a20a2009-03-12 18:34:49 +00002353 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2354 /// they don't have to check for cond/uncond branchness. These are mostly
2355 /// accessed relative from op_end().
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002356 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00002357 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002358 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2359 // BranchInst(BB *B) - 'br B'
2360 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2361 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2362 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2363 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2364 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00002365 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002366 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002367 Instruction *InsertBefore = 0);
2368 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002369 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002370 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002371protected:
2372 virtual BranchInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002373public:
2374 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
Jay Foad8e3914d2011-01-07 20:29:02 +00002375 return new(1) BranchInst(IfTrue, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002376 }
Gabor Greifefe65362008-05-10 08:32:32 +00002377 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2378 Value *Cond, Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002379 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2380 }
2381 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
Jay Foad8e3914d2011-01-07 20:29:02 +00002382 return new(1) BranchInst(IfTrue, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002383 }
Gabor Greifefe65362008-05-10 08:32:32 +00002384 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2385 Value *Cond, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002386 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2387 }
Chris Lattner454928e2005-01-29 00:31:36 +00002388
2389 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00002390 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002391
Devang Patel4d4a5e02008-02-23 01:11:02 +00002392 bool isUnconditional() const { return getNumOperands() == 1; }
2393 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002394
Devang Patel4d4a5e02008-02-23 01:11:02 +00002395 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002396 assert(isConditional() && "Cannot get condition of an uncond branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002397 return Op<-3>();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002398 }
2399
2400 void setCondition(Value *V) {
2401 assert(isConditional() && "Cannot set condition of unconditional branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002402 Op<-3>() = V;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002403 }
2404
Chris Lattner454928e2005-01-29 00:31:36 +00002405 unsigned getNumSuccessors() const { return 1+isConditional(); }
2406
2407 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002408 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002409 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002410 }
2411
Chris Lattner454928e2005-01-29 00:31:36 +00002412 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002413 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner4b122932009-10-27 16:49:53 +00002414 *(&Op<-1>() - idx) = (Value*)NewSucc;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002415 }
2416
Chandler Carruth602650c2011-10-17 01:11:57 +00002417 /// \brief Swap the successors of this branch instruction.
2418 ///
2419 /// Swaps the successors of the branch instruction. This also swaps any
2420 /// branch weight metadata associated with the instruction so that it
2421 /// continues to map correctly to each operand.
2422 void swapSuccessors();
2423
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002424 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002425 static inline bool classof(const Instruction *I) {
2426 return (I->getOpcode() == Instruction::Br);
2427 }
2428 static inline bool classof(const Value *V) {
2429 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2430 }
Chris Lattner454928e2005-01-29 00:31:36 +00002431private:
2432 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2433 virtual unsigned getNumSuccessorsV() const;
2434 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002435};
2436
Gabor Greifefe65362008-05-10 08:32:32 +00002437template <>
Jay Foad67c619b2011-01-11 15:07:38 +00002438struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2439};
Gabor Greifefe65362008-05-10 08:32:32 +00002440
2441DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2442
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002443//===----------------------------------------------------------------------===//
2444// SwitchInst Class
2445//===----------------------------------------------------------------------===//
2446
2447//===---------------------------------------------------------------------------
2448/// SwitchInst - Multiway switch
2449///
2450class SwitchInst : public TerminatorInst {
Craig Topperef1623f2012-09-18 03:25:49 +00002451 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Chris Lattner454928e2005-01-29 00:31:36 +00002452 unsigned ReservedSpace;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002453 // Operands format:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002454 // Operand[0] = Value to switch on
2455 // Operand[1] = Default basic block destination
2456 // Operand[2n ] = Value to match
2457 // Operand[2n+1] = BasicBlock to go to on match
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002458
2459 // Store case values separately from operands list. We needn't User-Use
2460 // concept here, since it is just a case value, it will always constant,
2461 // and case value couldn't reused with another instructions/values.
2462 // Additionally:
2463 // It allows us to use custom type for case values that is not inherited
2464 // from Value. Since case value is a complex type that implements
2465 // the subset of integers, we needn't extract sub-constants within
2466 // slow getAggregateElement method.
2467 // For case values we will use std::list to by two reasons:
2468 // 1. It allows to add/remove cases without whole collection reallocation.
2469 // 2. In most of cases we needn't random access.
2470 // Currently case values are also stored in Operands List, but it will moved
2471 // out in future commits.
2472 typedef std::list<IntegersSubset> Subsets;
2473 typedef Subsets::iterator SubsetsIt;
2474 typedef Subsets::const_iterator SubsetsConstIt;
2475
2476 Subsets TheSubsets;
2477
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002478 SwitchInst(const SwitchInst &SI);
Chris Lattneraa6e3502010-11-17 05:41:46 +00002479 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
Jay Foad8891ed72011-04-01 08:00:58 +00002480 void growOperands();
Gabor Greifefe65362008-05-10 08:32:32 +00002481 // allocate space for exactly zero operands
2482 void *operator new(size_t s) {
2483 return User::operator new(s, 0);
2484 }
Chris Lattner454928e2005-01-29 00:31:36 +00002485 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2486 /// switch on and a default destination. The number of additional cases can
2487 /// be specified here to make memory allocation more efficient. This
2488 /// constructor can also autoinsert before another instruction.
2489 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002490 Instruction *InsertBefore);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002491
Chris Lattner454928e2005-01-29 00:31:36 +00002492 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2493 /// switch on and a default destination. The number of additional cases can
2494 /// be specified here to make memory allocation more efficient. This
2495 /// constructor also autoinserts at the end of the specified BasicBlock.
2496 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002497 BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002498protected:
2499 virtual SwitchInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002500public:
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002501
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002502 // FIXME: Currently there are a lot of unclean template parameters,
2503 // we need to make refactoring in future.
2504 // All these parameters are used to implement both iterator and const_iterator
2505 // without code duplication.
2506 // SwitchInstTy may be "const SwitchInst" or "SwitchInst"
2507 // ConstantIntTy may be "const ConstantInt" or "ConstantInt"
2508 // SubsetsItTy may be SubsetsConstIt or SubsetsIt
2509 // BasicBlockTy may be "const BasicBlock" or "BasicBlock"
2510 template <class SwitchInstTy, class ConstantIntTy,
2511 class SubsetsItTy, class BasicBlockTy>
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002512 class CaseIteratorT;
2513
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002514 typedef CaseIteratorT<const SwitchInst, const ConstantInt,
2515 SubsetsConstIt, const BasicBlock> ConstCaseIt;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002516 class CaseIt;
2517
Aaron Ballmana13fb622012-03-10 23:03:01 +00002518 // -2
2519 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002520
Gabor Greifefe65362008-05-10 08:32:32 +00002521 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2522 unsigned NumCases, Instruction *InsertBefore = 0) {
2523 return new SwitchInst(Value, Default, NumCases, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002524 }
Gabor Greifefe65362008-05-10 08:32:32 +00002525 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2526 unsigned NumCases, BasicBlock *InsertAtEnd) {
2527 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002528 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002529
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002530 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00002531
Gabor Greifefe65362008-05-10 08:32:32 +00002532 /// Provide fast operand accessors
2533 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2534
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002535 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00002536 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00002537 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00002538
Devang Patel4d4a5e02008-02-23 01:11:02 +00002539 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002540 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002541 }
2542
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002543 void setDefaultDest(BasicBlock *DefaultCase) {
2544 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2545 }
2546
2547 /// getNumCases - return the number of 'cases' in this switch instruction,
2548 /// except the default case
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002549 unsigned getNumCases() const {
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002550 return getNumOperands()/2 - 1;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002551 }
2552
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002553 /// Returns a read/write iterator that points to the first
2554 /// case in SwitchInst.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002555 CaseIt case_begin() {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002556 return CaseIt(this, 0, TheSubsets.begin());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002557 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002558 /// Returns a read-only iterator that points to the first
2559 /// case in the SwitchInst.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002560 ConstCaseIt case_begin() const {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002561 return ConstCaseIt(this, 0, TheSubsets.begin());
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002562 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002563
2564 /// Returns a read/write iterator that points one past the last
2565 /// in the SwitchInst.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002566 CaseIt case_end() {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002567 return CaseIt(this, getNumCases(), TheSubsets.end());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002568 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002569 /// Returns a read-only iterator that points one past the last
2570 /// in the SwitchInst.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002571 ConstCaseIt case_end() const {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002572 return ConstCaseIt(this, getNumCases(), TheSubsets.end());
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002573 }
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002574 /// Returns an iterator that points to the default case.
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002575 /// Note: this iterator allows to resolve successor only. Attempt
2576 /// to resolve case value causes an assertion.
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002577 /// Also note, that increment and decrement also causes an assertion and
2578 /// makes iterator invalid.
2579 CaseIt case_default() {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002580 return CaseIt(this, DefaultPseudoIndex, TheSubsets.end());
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002581 }
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002582 ConstCaseIt case_default() const {
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002583 return ConstCaseIt(this, DefaultPseudoIndex, TheSubsets.end());
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002584 }
2585
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002586 /// findCaseValue - Search all of the case values for the specified constant.
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002587 /// If it is explicitly handled, return the case iterator of it, otherwise
2588 /// return default case iterator to indicate
2589 /// that it is handled by the default handler.
2590 CaseIt findCaseValue(const ConstantInt *C) {
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002591 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
Stepan Dyatkovskiyf8d14c42012-06-01 10:06:14 +00002592 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002593 return i;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002594 return case_default();
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002595 }
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002596 ConstCaseIt findCaseValue(const ConstantInt *C) const {
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002597 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
Stepan Dyatkovskiyf8d14c42012-06-01 10:06:14 +00002598 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002599 return i;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002600 return case_default();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002601 }
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002602
Nick Lewycky011f1842006-09-18 19:03:59 +00002603 /// findCaseDest - Finds the unique case value for a given successor. Returns
2604 /// null if the successor is not found, not unique, or is the default case.
2605 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00002606 if (BB == getDefaultDest()) return NULL;
2607
Nick Lewycky011f1842006-09-18 19:03:59 +00002608 ConstantInt *CI = NULL;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002609 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002610 if (i.getCaseSuccessor() == BB) {
Nick Lewycky011f1842006-09-18 19:03:59 +00002611 if (CI) return NULL; // Multiple cases lead to BB.
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002612 else CI = i.getCaseValue();
Nick Lewycky011f1842006-09-18 19:03:59 +00002613 }
2614 }
2615 return CI;
2616 }
2617
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002618 /// addCase - Add an entry to the switch instruction...
Dmitri Gribenko67c89782012-09-12 16:59:47 +00002619 /// @deprecated
Stepan Dyatkovskiyc347b6f2012-03-13 12:37:10 +00002620 /// Note:
2621 /// This action invalidates case_end(). Old case_end() iterator will
2622 /// point to the added case.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002623 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002624
2625 /// addCase - Add an entry to the switch instruction.
2626 /// Note:
2627 /// This action invalidates case_end(). Old case_end() iterator will
2628 /// point to the added case.
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00002629 void addCase(IntegersSubset& OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002630
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002631 /// removeCase - This method removes the specified case and its successor
2632 /// from the switch instruction. Note that this operation may reorder the
Jay Foad0faa6092011-02-01 09:22:34 +00002633 /// remaining cases at index idx and above.
Stepan Dyatkovskiyc347b6f2012-03-13 12:37:10 +00002634 /// Note:
2635 /// This action invalidates iterators for all cases following the one removed,
2636 /// including the case_end() iterator.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002637 void removeCase(CaseIt& i);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002638
Chris Lattner454928e2005-01-29 00:31:36 +00002639 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2640 BasicBlock *getSuccessor(unsigned idx) const {
2641 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2642 return cast<BasicBlock>(getOperand(idx*2+1));
2643 }
2644 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002645 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Chris Lattner4b122932009-10-27 16:49:53 +00002646 setOperand(idx*2+1, (Value*)NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002647 }
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002648
Stepan Dyatkovskiy734dde82012-05-14 08:26:31 +00002649 uint16_t hash() const {
Stepan Dyatkovskiy1cce5bf2012-05-12 10:48:17 +00002650 uint32_t NumberOfCases = (uint32_t)getNumCases();
2651 uint16_t Hash = (0xFFFF & NumberOfCases) ^ (NumberOfCases >> 16);
2652 for (ConstCaseIt i = case_begin(), e = case_end();
2653 i != e; ++i) {
2654 uint32_t NumItems = (uint32_t)i.getCaseValueEx().getNumItems();
2655 Hash = (Hash << 1) ^ (0xFFFF & NumItems) ^ (NumItems >> 16);
2656 }
2657 return Hash;
2658 }
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002659
2660 // Case iterators definition.
2661
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002662 template <class SwitchInstTy, class ConstantIntTy,
2663 class SubsetsItTy, class BasicBlockTy>
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002664 class CaseIteratorT {
2665 protected:
2666
2667 SwitchInstTy *SI;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002668 unsigned long Index;
2669 SubsetsItTy SubsetIt;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002670
2671 /// Initializes case iterator for given SwitchInst and for given
2672 /// case number.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002673 friend class SwitchInst;
2674 CaseIteratorT(SwitchInstTy *SI, unsigned SuccessorIndex,
2675 SubsetsItTy CaseValueIt) {
Duncan Sands37eeb052012-06-22 10:35:06 +00002676 this->SI = SI;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002677 Index = SuccessorIndex;
2678 this->SubsetIt = CaseValueIt;
Duncan Sands37eeb052012-06-22 10:35:06 +00002679 }
Stepan Dyatkovskiy73512562012-06-22 07:35:13 +00002680
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002681 public:
2682 typedef typename SubsetsItTy::reference IntegersSubsetRef;
2683 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy,
2684 SubsetsItTy, BasicBlockTy> Self;
2685
2686 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2687 this->SI = SI;
2688 Index = CaseNum;
2689 SubsetIt = SI->TheSubsets.begin();
2690 std::advance(SubsetIt, CaseNum);
2691 }
2692
2693
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002694 /// Initializes case iterator for given SwitchInst and for given
2695 /// TerminatorInst's successor index.
2696 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2697 assert(SuccessorIndex < SI->getNumSuccessors() &&
2698 "Successor index # out of range!");
2699 return SuccessorIndex != 0 ?
2700 Self(SI, SuccessorIndex - 1) :
2701 Self(SI, DefaultPseudoIndex);
2702 }
2703
2704 /// Resolves case value for current case.
Dmitri Gribenko67c89782012-09-12 16:59:47 +00002705 /// @deprecated
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002706 ConstantIntTy *getCaseValue() {
2707 assert(Index < SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002708 IntegersSubsetRef CaseRanges = *SubsetIt;
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00002709
2710 // FIXME: Currently we work with ConstantInt based cases.
2711 // So return CaseValue as ConstantInt.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002712 return CaseRanges.getSingleNumber(0).toConstantInt();
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002713 }
2714
2715 /// Resolves case value for current case.
Stepan Dyatkovskiy47cbc4e2012-06-23 10:58:58 +00002716 IntegersSubsetRef getCaseValueEx() {
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002717 assert(Index < SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002718 return *SubsetIt;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002719 }
2720
2721 /// Resolves successor for current case.
2722 BasicBlockTy *getCaseSuccessor() {
2723 assert((Index < SI->getNumCases() ||
2724 Index == DefaultPseudoIndex) &&
2725 "Index out the number of cases.");
2726 return SI->getSuccessor(getSuccessorIndex());
2727 }
2728
2729 /// Returns number of current case.
2730 unsigned getCaseIndex() const { return Index; }
2731
2732 /// Returns TerminatorInst's successor index for current case successor.
2733 unsigned getSuccessorIndex() const {
2734 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2735 "Index out the number of cases.");
2736 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2737 }
2738
2739 Self operator++() {
2740 // Check index correctness after increment.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002741 // Note: Index == getNumCases() means end().
Kaelyn Uhrain35ac4b02012-06-22 17:18:15 +00002742 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002743 ++Index;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002744 if (Index == 0)
2745 SubsetIt = SI->TheSubsets.begin();
2746 else
2747 ++SubsetIt;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002748 return *this;
2749 }
2750 Self operator++(int) {
2751 Self tmp = *this;
2752 ++(*this);
2753 return tmp;
2754 }
2755 Self operator--() {
2756 // Check index correctness after decrement.
2757 // Note: Index == getNumCases() means end().
2758 // Also allow "-1" iterator here. That will became valid after ++.
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002759 unsigned NumCases = SI->getNumCases();
2760 assert((Index == 0 || Index-1 <= NumCases) &&
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002761 "Index out the number of cases.");
2762 --Index;
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002763 if (Index == NumCases) {
2764 SubsetIt = SI->TheSubsets.end();
2765 return *this;
2766 }
2767
2768 if (Index != -1UL)
2769 --SubsetIt;
2770
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002771 return *this;
2772 }
2773 Self operator--(int) {
2774 Self tmp = *this;
2775 --(*this);
2776 return tmp;
2777 }
2778 bool operator==(const Self& RHS) const {
2779 assert(RHS.SI == SI && "Incompatible operators.");
2780 return RHS.Index == Index;
2781 }
2782 bool operator!=(const Self& RHS) const {
2783 assert(RHS.SI == SI && "Incompatible operators.");
2784 return RHS.Index != Index;
2785 }
2786 };
2787
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002788 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt,
2789 SubsetsIt, BasicBlock> {
2790 typedef CaseIteratorT<SwitchInst, ConstantInt, SubsetsIt, BasicBlock>
2791 ParentTy;
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002792
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002793 protected:
2794 friend class SwitchInst;
2795 CaseIt(SwitchInst *SI, unsigned CaseNum, SubsetsIt SubsetIt) :
2796 ParentTy(SI, CaseNum, SubsetIt) {}
2797
2798 void updateCaseValueOperand(IntegersSubset& V) {
2799 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>((Constant*)V));
2800 }
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002801
2802 public:
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002803
2804 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002805
2806 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002807
2808 /// Sets the new value for current case.
Dmitri Gribenko67c89782012-09-12 16:59:47 +00002809 /// @deprecated.
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002810 void setValue(ConstantInt *V) {
2811 assert(Index < SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00002812 IntegersSubsetToBB Mapping;
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00002813 // FIXME: Currently we work with ConstantInt based cases.
2814 // So inititalize IntItem container directly from ConstantInt.
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00002815 Mapping.add(IntItem::fromConstantInt(V));
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002816 *SubsetIt = Mapping.getCase();
2817 updateCaseValueOperand(*SubsetIt);
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002818 }
2819
2820 /// Sets the new value for current case.
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00002821 void setValueEx(IntegersSubset& V) {
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002822 assert(Index < SI->getNumCases() && "Index out the number of cases.");
Stepan Dyatkovskiy43c3a4a2012-06-22 14:53:30 +00002823 *SubsetIt = V;
2824 updateCaseValueOperand(*SubsetIt);
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002825 }
2826
2827 /// Sets the new successor for current case.
2828 void setSuccessor(BasicBlock *S) {
2829 SI->setSuccessor(getSuccessorIndex(), S);
2830 }
2831 };
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002832
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002833 // Methods for support type inquiry through isa, cast, and dyn_cast:
Stepan Dyatkovskiyff208a72012-05-28 10:11:27 +00002834
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002835 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00002836 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002837 }
2838 static inline bool classof(const Value *V) {
2839 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2840 }
Chris Lattner454928e2005-01-29 00:31:36 +00002841private:
2842 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2843 virtual unsigned getNumSuccessorsV() const;
2844 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002845};
2846
Gabor Greifefe65362008-05-10 08:32:32 +00002847template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002848struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00002849};
2850
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002851DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002852
2853
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002854//===----------------------------------------------------------------------===//
Chris Lattnerab21db72009-10-28 00:19:10 +00002855// IndirectBrInst Class
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002856//===----------------------------------------------------------------------===//
2857
2858//===---------------------------------------------------------------------------
Chris Lattnerab21db72009-10-28 00:19:10 +00002859/// IndirectBrInst - Indirect Branch Instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002860///
Chris Lattnerab21db72009-10-28 00:19:10 +00002861class IndirectBrInst : public TerminatorInst {
Craig Topperef1623f2012-09-18 03:25:49 +00002862 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002863 unsigned ReservedSpace;
2864 // Operand[0] = Value to switch on
2865 // Operand[1] = Default basic block destination
2866 // Operand[2n ] = Value to match
2867 // Operand[2n+1] = BasicBlock to go to on match
Chris Lattnerab21db72009-10-28 00:19:10 +00002868 IndirectBrInst(const IndirectBrInst &IBI);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002869 void init(Value *Address, unsigned NumDests);
Jay Foad8891ed72011-04-01 08:00:58 +00002870 void growOperands();
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002871 // allocate space for exactly zero operands
2872 void *operator new(size_t s) {
2873 return User::operator new(s, 0);
2874 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002875 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2876 /// Address to jump to. The number of expected destinations can be specified
2877 /// here to make memory allocation more efficient. This constructor can also
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002878 /// autoinsert before another instruction.
Chris Lattnerab21db72009-10-28 00:19:10 +00002879 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002880
Chris Lattnerab21db72009-10-28 00:19:10 +00002881 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2882 /// Address to jump to. The number of expected destinations can be specified
2883 /// here to make memory allocation more efficient. This constructor also
2884 /// autoinserts at the end of the specified BasicBlock.
2885 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002886protected:
Chris Lattnerab21db72009-10-28 00:19:10 +00002887 virtual IndirectBrInst *clone_impl() const;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002888public:
Chris Lattnerab21db72009-10-28 00:19:10 +00002889 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2890 Instruction *InsertBefore = 0) {
2891 return new IndirectBrInst(Address, NumDests, InsertBefore);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002892 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002893 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2894 BasicBlock *InsertAtEnd) {
2895 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002896 }
Chris Lattnerab21db72009-10-28 00:19:10 +00002897 ~IndirectBrInst();
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002898
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002899 /// Provide fast operand accessors.
2900 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002901
Chris Lattnerab21db72009-10-28 00:19:10 +00002902 // Accessor Methods for IndirectBrInst instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002903 Value *getAddress() { return getOperand(0); }
2904 const Value *getAddress() const { return getOperand(0); }
2905 void setAddress(Value *V) { setOperand(0, V); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002906
2907
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002908 /// getNumDestinations - return the number of possible destinations in this
Chris Lattnerab21db72009-10-28 00:19:10 +00002909 /// indirectbr instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002910 unsigned getNumDestinations() const { return getNumOperands()-1; }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002911
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002912 /// getDestination - Return the specified destination.
2913 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2914 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002915
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002916 /// addDestination - Add a destination.
2917 ///
2918 void addDestination(BasicBlock *Dest);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002919
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002920 /// removeDestination - This method removes the specified successor from the
Chris Lattnerab21db72009-10-28 00:19:10 +00002921 /// indirectbr instruction.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002922 void removeDestination(unsigned i);
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002923
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002924 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2925 BasicBlock *getSuccessor(unsigned i) const {
2926 return cast<BasicBlock>(getOperand(i+1));
2927 }
2928 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2929 setOperand(i+1, (Value*)NewSucc);
2930 }
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002931
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002932 // Methods for support type inquiry through isa, cast, and dyn_cast:
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002933 static inline bool classof(const Instruction *I) {
Chris Lattnerab21db72009-10-28 00:19:10 +00002934 return I->getOpcode() == Instruction::IndirectBr;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002935 }
2936 static inline bool classof(const Value *V) {
2937 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2938 }
2939private:
2940 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2941 virtual unsigned getNumSuccessorsV() const;
2942 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2943};
2944
2945template <>
Chris Lattnerab21db72009-10-28 00:19:10 +00002946struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002947};
2948
Chris Lattnerab21db72009-10-28 00:19:10 +00002949DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
Mikhail Glushenkov0da14f72010-10-27 07:39:48 +00002950
2951
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002952//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002953// InvokeInst Class
2954//===----------------------------------------------------------------------===//
2955
Chris Lattner3340ffe2005-05-06 20:26:26 +00002956/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2957/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002958///
2959class InvokeInst : public TerminatorInst {
Bill Wendling99faa3b2012-12-07 23:16:57 +00002960 AttributeSet AttributeList;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002961 InvokeInst(const InvokeInst &BI);
David Greenef1355a52007-08-27 19:04:21 +00002962 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002963 ArrayRef<Value *> Args, const Twine &NameStr);
David Greenef1355a52007-08-27 19:04:21 +00002964
David Greenef1355a52007-08-27 19:04:21 +00002965 /// Construct an InvokeInst given a range of arguments.
David Greenef1355a52007-08-27 19:04:21 +00002966 ///
Chandler Carruthacd01d12012-11-01 10:46:54 +00002967 /// \brief Construct an InvokeInst from a range of arguments
Gabor Greifefe65362008-05-10 08:32:32 +00002968 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002969 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002970 const Twine &NameStr, Instruction *InsertBefore);
David Greenef1355a52007-08-27 19:04:21 +00002971
2972 /// Construct an InvokeInst given a range of arguments.
David Greenef1355a52007-08-27 19:04:21 +00002973 ///
Chandler Carruthacd01d12012-11-01 10:46:54 +00002974 /// \brief Construct an InvokeInst from a range of arguments
Gabor Greifefe65362008-05-10 08:32:32 +00002975 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002976 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002977 const Twine &NameStr, BasicBlock *InsertAtEnd);
Devang Patel50b6e332009-10-27 22:16:29 +00002978protected:
2979 virtual InvokeInst *clone_impl() const;
Gabor Greif051a9502008-04-06 20:25:17 +00002980public:
Gabor Greifefe65362008-05-10 08:32:32 +00002981 static InvokeInst *Create(Value *Func,
2982 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002983 ArrayRef<Value *> Args, const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00002984 Instruction *InsertBefore = 0) {
Jay Foada3efbb12011-07-15 08:37:34 +00002985 unsigned Values = unsigned(Args.size()) + 3;
2986 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002987 Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002988 }
Gabor Greifefe65362008-05-10 08:32:32 +00002989 static InvokeInst *Create(Value *Func,
2990 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00002991 ArrayRef<Value *> Args, const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002992 BasicBlock *InsertAtEnd) {
Jay Foada3efbb12011-07-15 08:37:34 +00002993 unsigned Values = unsigned(Args.size()) + 3;
2994 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002995 Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002996 }
David Greenef1355a52007-08-27 19:04:21 +00002997
Gabor Greifefe65362008-05-10 08:32:32 +00002998 /// Provide fast operand accessors
2999 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00003000
Gabor Greif0114b992010-07-31 08:35:21 +00003001 /// getNumArgOperands - Return the number of invoke arguments.
3002 ///
Bill Wendling22a5b292010-06-07 19:05:06 +00003003 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
Gabor Greif0114b992010-07-31 08:35:21 +00003004
3005 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3006 ///
Bill Wendling22a5b292010-06-07 19:05:06 +00003007 Value *getArgOperand(unsigned i) const { return getOperand(i); }
Gabor Greif710ac072010-06-28 12:23:36 +00003008 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
Bill Wendling22a5b292010-06-07 19:05:06 +00003009
Chris Lattner3340ffe2005-05-06 20:26:26 +00003010 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3011 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003012 CallingConv::ID getCallingConv() const {
Chris Lattnerb2406d92009-12-29 02:46:09 +00003013 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003014 }
3015 void setCallingConv(CallingConv::ID CC) {
Chris Lattnerb2406d92009-12-29 02:46:09 +00003016 setInstructionSubclassData(static_cast<unsigned>(CC));
Chris Lattner3340ffe2005-05-06 20:26:26 +00003017 }
3018
Devang Patel05988662008-09-25 21:00:45 +00003019 /// getAttributes - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00003020 ///
Bill Wendling99faa3b2012-12-07 23:16:57 +00003021 const AttributeSet &getAttributes() const { return AttributeList; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00003022
Devang Patel05988662008-09-25 21:00:45 +00003023 /// setAttributes - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00003024 ///
Bill Wendling99faa3b2012-12-07 23:16:57 +00003025 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00003026
Devang Patel05988662008-09-25 21:00:45 +00003027 /// addAttribute - adds the attribute to the list of attributes.
Bill Wendling034b94b2012-12-19 07:18:57 +00003028 void addAttribute(unsigned i, Attribute attr);
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00003029
Devang Patel05988662008-09-25 21:00:45 +00003030 /// removeAttribute - removes the attribute from the list of attributes.
Bill Wendling034b94b2012-12-19 07:18:57 +00003031 void removeAttribute(unsigned i, Attribute attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00003032
Chandler Carruthacd01d12012-11-01 10:46:54 +00003033 /// \brief Determine whether this call has the NoAlias attribute.
Bill Wendling629fb822012-12-22 00:37:52 +00003034 bool hasFnAttr(Attribute::AttrKind A) const;
Bill Wendling060f20a2012-10-09 00:28:54 +00003035
Chandler Carruthacd01d12012-11-01 10:46:54 +00003036 /// \brief Determine whether the call or the callee has the given attributes.
Bill Wendling629fb822012-12-22 00:37:52 +00003037 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
Bill Wendling847d1652012-10-03 17:54:26 +00003038
Chandler Carruthacd01d12012-11-01 10:46:54 +00003039 /// \brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003040 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00003041 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003042 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00003043
Chandler Carruthacd01d12012-11-01 10:46:54 +00003044 /// \brief Return true if the call should not be inlined.
Bill Wendling034b94b2012-12-19 07:18:57 +00003045 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
Bill Wendling1b005072012-10-09 23:40:31 +00003046 void setIsNoInline() {
Bill Wendling99faa3b2012-12-07 23:16:57 +00003047 addAttribute(AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00003048 Attribute::get(getContext(), Attribute::NoInline));
Eric Christopherf27e6082010-03-25 04:49:10 +00003049 }
Nick Lewyckyb9933b82010-07-06 03:53:22 +00003050
Chandler Carruthacd01d12012-11-01 10:46:54 +00003051 /// \brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003052 bool doesNotAccessMemory() const {
Bill Wendling034b94b2012-12-19 07:18:57 +00003053 return hasFnAttr(Attribute::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003054 }
Bill Wendling1b005072012-10-09 23:40:31 +00003055 void setDoesNotAccessMemory() {
Bill Wendling99faa3b2012-12-07 23:16:57 +00003056 addAttribute(AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00003057 Attribute::get(getContext(), Attribute::ReadNone));
Duncan Sands2e033f32008-07-08 08:38:44 +00003058 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00003059
Chandler Carruthacd01d12012-11-01 10:46:54 +00003060 /// \brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003061 bool onlyReadsMemory() const {
Bill Wendling034b94b2012-12-19 07:18:57 +00003062 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003063 }
Bill Wendling1b005072012-10-09 23:40:31 +00003064 void setOnlyReadsMemory() {
Bill Wendling99faa3b2012-12-07 23:16:57 +00003065 addAttribute(AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00003066 Attribute::get(getContext(), Attribute::ReadOnly));
Duncan Sands2e033f32008-07-08 08:38:44 +00003067 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00003068
Chandler Carruthacd01d12012-11-01 10:46:54 +00003069 /// \brief Determine if the call cannot return.
Bill Wendling034b94b2012-12-19 07:18:57 +00003070 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
Bill Wendling1b005072012-10-09 23:40:31 +00003071 void setDoesNotReturn() {
Bill Wendling99faa3b2012-12-07 23:16:57 +00003072 addAttribute(AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00003073 Attribute::get(getContext(), Attribute::NoReturn));
Duncan Sands2e033f32008-07-08 08:38:44 +00003074 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00003075
Chandler Carruthacd01d12012-11-01 10:46:54 +00003076 /// \brief Determine if the call cannot unwind.
Bill Wendling034b94b2012-12-19 07:18:57 +00003077 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
Bill Wendling1b005072012-10-09 23:40:31 +00003078 void setDoesNotThrow() {
Bill Wendling99faa3b2012-12-07 23:16:57 +00003079 addAttribute(AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00003080 Attribute::get(getContext(), Attribute::NoUnwind));
Duncan Sands2e033f32008-07-08 08:38:44 +00003081 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00003082
Chandler Carruthacd01d12012-11-01 10:46:54 +00003083 /// \brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00003084 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003085 bool hasStructRetAttr() const {
3086 // Be friendly and also check the callee.
Bill Wendling034b94b2012-12-19 07:18:57 +00003087 return paramHasAttr(1, Attribute::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00003088 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00003089
Chandler Carruthacd01d12012-11-01 10:46:54 +00003090 /// \brief Determine if any call argument is an aggregate passed by value.
Dan Gohmanf2752502008-09-26 21:38:45 +00003091 bool hasByValArgument() const {
Bill Wendling9d30e722012-12-31 00:49:59 +00003092 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
Dan Gohmanf2752502008-09-26 21:38:45 +00003093 }
3094
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003095 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00003096 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003097 ///
Chris Lattner721aef62004-11-18 17:46:57 +00003098 Function *getCalledFunction() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00003099 return dyn_cast<Function>(Op<-3>());
Chris Lattner721aef62004-11-18 17:46:57 +00003100 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003101
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00003102 /// getCalledValue - Get a pointer to the function that is invoked by this
Dan Gohmanf2752502008-09-26 21:38:45 +00003103 /// instruction
Gabor Greifc9f75002010-03-24 13:21:49 +00003104 const Value *getCalledValue() const { return Op<-3>(); }
3105 Value *getCalledValue() { return Op<-3>(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003106
Gabor Greif654c06f2010-03-20 21:00:25 +00003107 /// setCalledFunction - Set the function called.
3108 void setCalledFunction(Value* Fn) {
Gabor Greifc9f75002010-03-24 13:21:49 +00003109 Op<-3>() = Fn;
Gabor Greif654c06f2010-03-20 21:00:25 +00003110 }
3111
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003112 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00003113 BasicBlock *getNormalDest() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00003114 return cast<BasicBlock>(Op<-2>());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003115 }
Chris Lattner454928e2005-01-29 00:31:36 +00003116 BasicBlock *getUnwindDest() const {
Gabor Greifc9f75002010-03-24 13:21:49 +00003117 return cast<BasicBlock>(Op<-1>());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003118 }
Chris Lattner454928e2005-01-29 00:31:36 +00003119 void setNormalDest(BasicBlock *B) {
Gabor Greifc9f75002010-03-24 13:21:49 +00003120 Op<-2>() = reinterpret_cast<Value*>(B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003121 }
Chris Lattner454928e2005-01-29 00:31:36 +00003122 void setUnwindDest(BasicBlock *B) {
Gabor Greifc9f75002010-03-24 13:21:49 +00003123 Op<-1>() = reinterpret_cast<Value*>(B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003124 }
3125
Bill Wendlinge6e88262011-08-12 20:24:12 +00003126 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3127 /// block (the unwind destination).
3128 LandingPadInst *getLandingPadInst() const;
3129
Devang Patel4d4a5e02008-02-23 01:11:02 +00003130 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003131 assert(i < 2 && "Successor # out of range for invoke!");
3132 return i == 0 ? getNormalDest() : getUnwindDest();
3133 }
3134
Chris Lattner454928e2005-01-29 00:31:36 +00003135 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003136 assert(idx < 2 && "Successor # out of range for invoke!");
Gabor Greifc9f75002010-03-24 13:21:49 +00003137 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003138 }
3139
Chris Lattner454928e2005-01-29 00:31:36 +00003140 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003141
3142 // Methods for support type inquiry through isa, cast, and dyn_cast:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003143 static inline bool classof(const Instruction *I) {
3144 return (I->getOpcode() == Instruction::Invoke);
3145 }
3146 static inline bool classof(const Value *V) {
3147 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3148 }
Gabor Greifc9f75002010-03-24 13:21:49 +00003149
Chris Lattner454928e2005-01-29 00:31:36 +00003150private:
3151 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3152 virtual unsigned getNumSuccessorsV() const;
3153 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00003154
Chris Lattnerb2406d92009-12-29 02:46:09 +00003155 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3156 // method so that subclasses cannot accidentally use it.
3157 void setInstructionSubclassData(unsigned short D) {
3158 Instruction::setInstructionSubclassData(D);
Chris Lattnercafe9bb2009-12-29 02:14:09 +00003159 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003160};
3161
Gabor Greifefe65362008-05-10 08:32:32 +00003162template <>
Jay Foad67c619b2011-01-11 15:07:38 +00003163struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
Gabor Greifefe65362008-05-10 08:32:32 +00003164};
3165
Gabor Greifefe65362008-05-10 08:32:32 +00003166InvokeInst::InvokeInst(Value *Func,
3167 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00003168 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003169 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00003170 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3171 ->getElementType())->getReturnType(),
3172 Instruction::Invoke,
3173 OperandTraits<InvokeInst>::op_end(this) - Values,
3174 Values, InsertBefore) {
Jay Foada3efbb12011-07-15 08:37:34 +00003175 init(Func, IfNormal, IfException, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00003176}
Gabor Greifefe65362008-05-10 08:32:32 +00003177InvokeInst::InvokeInst(Value *Func,
3178 BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foada3efbb12011-07-15 08:37:34 +00003179 ArrayRef<Value *> Args, unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003180 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00003181 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3182 ->getElementType())->getReturnType(),
3183 Instruction::Invoke,
3184 OperandTraits<InvokeInst>::op_end(this) - Values,
3185 Values, InsertAtEnd) {
Jay Foada3efbb12011-07-15 08:37:34 +00003186 init(Func, IfNormal, IfException, Args, NameStr);
Gabor Greifefe65362008-05-10 08:32:32 +00003187}
3188
3189DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003190
3191//===----------------------------------------------------------------------===//
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003192// ResumeInst Class
3193//===----------------------------------------------------------------------===//
3194
3195//===---------------------------------------------------------------------------
3196/// ResumeInst - Resume the propagation of an exception.
3197///
3198class ResumeInst : public TerminatorInst {
3199 ResumeInst(const ResumeInst &RI);
3200
3201 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3202 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3203protected:
3204 virtual ResumeInst *clone_impl() const;
3205public:
3206 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3207 return new(1) ResumeInst(Exn, InsertBefore);
3208 }
3209 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3210 return new(1) ResumeInst(Exn, InsertAtEnd);
3211 }
3212
3213 /// Provide fast operand accessors
3214 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3215
3216 /// Convenience accessor.
3217 Value *getValue() const { return Op<0>(); }
3218
3219 unsigned getNumSuccessors() const { return 0; }
3220
3221 // Methods for support type inquiry through isa, cast, and dyn_cast:
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003222 static inline bool classof(const Instruction *I) {
3223 return I->getOpcode() == Instruction::Resume;
3224 }
3225 static inline bool classof(const Value *V) {
3226 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3227 }
3228private:
3229 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3230 virtual unsigned getNumSuccessorsV() const;
3231 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3232};
3233
3234template <>
3235struct OperandTraits<ResumeInst> :
3236 public FixedNumOperandTraits<ResumeInst, 1> {
3237};
3238
3239DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3240
3241//===----------------------------------------------------------------------===//
Chris Lattner076b3f12004-10-16 18:05:54 +00003242// UnreachableInst Class
3243//===----------------------------------------------------------------------===//
3244
3245//===---------------------------------------------------------------------------
3246/// UnreachableInst - This function has undefined behavior. In particular, the
3247/// presence of this instruction indicates some higher level knowledge that the
3248/// end of the block cannot be reached.
3249///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00003250class UnreachableInst : public TerminatorInst {
Craig Topperef1623f2012-09-18 03:25:49 +00003251 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Devang Patel50b6e332009-10-27 22:16:29 +00003252protected:
3253 virtual UnreachableInst *clone_impl() const;
3254
Chris Lattner1fca5ff2004-10-27 16:14:51 +00003255public:
Gabor Greif051a9502008-04-06 20:25:17 +00003256 // allocate space for exactly zero operands
3257 void *operator new(size_t s) {
3258 return User::operator new(s, 0);
3259 }
Owen Anderson1d0be152009-08-13 21:58:54 +00003260 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3261 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00003262
Chris Lattner454928e2005-01-29 00:31:36 +00003263 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00003264
3265 // Methods for support type inquiry through isa, cast, and dyn_cast:
Chris Lattner076b3f12004-10-16 18:05:54 +00003266 static inline bool classof(const Instruction *I) {
3267 return I->getOpcode() == Instruction::Unreachable;
3268 }
3269 static inline bool classof(const Value *V) {
3270 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3271 }
Chris Lattner454928e2005-01-29 00:31:36 +00003272private:
3273 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3274 virtual unsigned getNumSuccessorsV() const;
3275 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00003276};
3277
Reid Spencer3da59db2006-11-27 01:05:10 +00003278//===----------------------------------------------------------------------===//
3279// TruncInst Class
3280//===----------------------------------------------------------------------===//
3281
Chandler Carruthacd01d12012-11-01 10:46:54 +00003282/// \brief This class represents a truncation of integer types.
Reid Spencer3da59db2006-11-27 01:05:10 +00003283class TruncInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003284protected:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003285 /// \brief Clone an identical TruncInst
Devang Patel50b6e332009-10-27 22:16:29 +00003286 virtual TruncInst *clone_impl() const;
3287
Reid Spencer3da59db2006-11-27 01:05:10 +00003288public:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003289 /// \brief Constructor with insert-before-instruction semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003290 TruncInst(
3291 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003292 Type *Ty, ///< The (smaller) type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003293 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003294 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3295 );
3296
Chandler Carruthacd01d12012-11-01 10:46:54 +00003297 /// \brief Constructor with insert-at-end-of-block semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003298 TruncInst(
3299 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003300 Type *Ty, ///< The (smaller) type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003301 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003302 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3303 );
3304
Chandler Carruthacd01d12012-11-01 10:46:54 +00003305 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003306 static inline bool classof(const Instruction *I) {
3307 return I->getOpcode() == Trunc;
3308 }
3309 static inline bool classof(const Value *V) {
3310 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3311 }
3312};
3313
3314//===----------------------------------------------------------------------===//
3315// ZExtInst Class
3316//===----------------------------------------------------------------------===//
3317
Chandler Carruthacd01d12012-11-01 10:46:54 +00003318/// \brief This class represents zero extension of integer types.
Reid Spencer3da59db2006-11-27 01:05:10 +00003319class ZExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003320protected:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003321 /// \brief Clone an identical ZExtInst
Devang Patel50b6e332009-10-27 22:16:29 +00003322 virtual ZExtInst *clone_impl() const;
3323
Reid Spencer3da59db2006-11-27 01:05:10 +00003324public:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003325 /// \brief Constructor with insert-before-instruction semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003326 ZExtInst(
3327 Value *S, ///< The value to be zero extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003328 Type *Ty, ///< The type to zero extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003329 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003330 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3331 );
3332
Chandler Carruthacd01d12012-11-01 10:46:54 +00003333 /// \brief Constructor with insert-at-end semantics.
Reid Spencer3da59db2006-11-27 01:05:10 +00003334 ZExtInst(
3335 Value *S, ///< The value to be zero extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003336 Type *Ty, ///< The type to zero extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003337 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003338 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3339 );
3340
Chandler Carruthacd01d12012-11-01 10:46:54 +00003341 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003342 static inline bool classof(const Instruction *I) {
3343 return I->getOpcode() == ZExt;
3344 }
3345 static inline bool classof(const Value *V) {
3346 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3347 }
3348};
3349
3350//===----------------------------------------------------------------------===//
3351// SExtInst Class
3352//===----------------------------------------------------------------------===//
3353
Chandler Carruthacd01d12012-11-01 10:46:54 +00003354/// \brief This class represents a sign extension of integer types.
Reid Spencer3da59db2006-11-27 01:05:10 +00003355class SExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003356protected:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003357 /// \brief Clone an identical SExtInst
Devang Patel50b6e332009-10-27 22:16:29 +00003358 virtual SExtInst *clone_impl() const;
3359
Reid Spencer3da59db2006-11-27 01:05:10 +00003360public:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003361 /// \brief Constructor with insert-before-instruction semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003362 SExtInst(
3363 Value *S, ///< The value to be sign extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003364 Type *Ty, ///< The type to sign extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003365 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003366 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3367 );
3368
Chandler Carruthacd01d12012-11-01 10:46:54 +00003369 /// \brief Constructor with insert-at-end-of-block semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003370 SExtInst(
3371 Value *S, ///< The value to be sign extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003372 Type *Ty, ///< The type to sign extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003373 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003374 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3375 );
3376
Chandler Carruthacd01d12012-11-01 10:46:54 +00003377 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003378 static inline bool classof(const Instruction *I) {
3379 return I->getOpcode() == SExt;
3380 }
3381 static inline bool classof(const Value *V) {
3382 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3383 }
3384};
3385
3386//===----------------------------------------------------------------------===//
3387// FPTruncInst Class
3388//===----------------------------------------------------------------------===//
3389
Chandler Carruthacd01d12012-11-01 10:46:54 +00003390/// \brief This class represents a truncation of floating point types.
Reid Spencer3da59db2006-11-27 01:05:10 +00003391class FPTruncInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003392protected:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003393 /// \brief Clone an identical FPTruncInst
Devang Patel50b6e332009-10-27 22:16:29 +00003394 virtual FPTruncInst *clone_impl() const;
3395
Reid Spencer3da59db2006-11-27 01:05:10 +00003396public:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003397 /// \brief Constructor with insert-before-instruction semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003398 FPTruncInst(
3399 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003400 Type *Ty, ///< The type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003401 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003402 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3403 );
3404
Chandler Carruthacd01d12012-11-01 10:46:54 +00003405 /// \brief Constructor with insert-before-instruction semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003406 FPTruncInst(
3407 Value *S, ///< The value to be truncated
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003408 Type *Ty, ///< The type to truncate to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003409 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003410 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3411 );
3412
Chandler Carruthacd01d12012-11-01 10:46:54 +00003413 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003414 static inline bool classof(const Instruction *I) {
3415 return I->getOpcode() == FPTrunc;
3416 }
3417 static inline bool classof(const Value *V) {
3418 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3419 }
3420};
3421
3422//===----------------------------------------------------------------------===//
3423// FPExtInst Class
3424//===----------------------------------------------------------------------===//
3425
Chandler Carruthacd01d12012-11-01 10:46:54 +00003426/// \brief This class represents an extension of floating point types.
Reid Spencer3da59db2006-11-27 01:05:10 +00003427class FPExtInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003428protected:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003429 /// \brief Clone an identical FPExtInst
Devang Patel50b6e332009-10-27 22:16:29 +00003430 virtual FPExtInst *clone_impl() const;
3431
Reid Spencer3da59db2006-11-27 01:05:10 +00003432public:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003433 /// \brief Constructor with insert-before-instruction semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003434 FPExtInst(
3435 Value *S, ///< The value to be extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003436 Type *Ty, ///< The type to extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003437 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003438 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3439 );
3440
Chandler Carruthacd01d12012-11-01 10:46:54 +00003441 /// \brief Constructor with insert-at-end-of-block semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003442 FPExtInst(
3443 Value *S, ///< The value to be extended
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003444 Type *Ty, ///< The type to extend to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003445 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003446 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3447 );
3448
Chandler Carruthacd01d12012-11-01 10:46:54 +00003449 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003450 static inline bool classof(const Instruction *I) {
3451 return I->getOpcode() == FPExt;
3452 }
3453 static inline bool classof(const Value *V) {
3454 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3455 }
3456};
3457
3458//===----------------------------------------------------------------------===//
3459// UIToFPInst Class
3460//===----------------------------------------------------------------------===//
3461
Chandler Carruthacd01d12012-11-01 10:46:54 +00003462/// \brief This class represents a cast unsigned integer to floating point.
Reid Spencer3da59db2006-11-27 01:05:10 +00003463class UIToFPInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003464protected:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003465 /// \brief Clone an identical UIToFPInst
Devang Patel50b6e332009-10-27 22:16:29 +00003466 virtual UIToFPInst *clone_impl() const;
3467
Reid Spencer3da59db2006-11-27 01:05:10 +00003468public:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003469 /// \brief Constructor with insert-before-instruction semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003470 UIToFPInst(
3471 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003472 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003473 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003474 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3475 );
3476
Chandler Carruthacd01d12012-11-01 10:46:54 +00003477 /// \brief Constructor with insert-at-end-of-block semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003478 UIToFPInst(
3479 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003480 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003481 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003482 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3483 );
3484
Chandler Carruthacd01d12012-11-01 10:46:54 +00003485 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003486 static inline bool classof(const Instruction *I) {
3487 return I->getOpcode() == UIToFP;
3488 }
3489 static inline bool classof(const Value *V) {
3490 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3491 }
3492};
3493
3494//===----------------------------------------------------------------------===//
3495// SIToFPInst Class
3496//===----------------------------------------------------------------------===//
3497
Chandler Carruthacd01d12012-11-01 10:46:54 +00003498/// \brief This class represents a cast from signed integer to floating point.
Reid Spencer3da59db2006-11-27 01:05:10 +00003499class SIToFPInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003500protected:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003501 /// \brief Clone an identical SIToFPInst
Devang Patel50b6e332009-10-27 22:16:29 +00003502 virtual SIToFPInst *clone_impl() const;
3503
Reid Spencer3da59db2006-11-27 01:05:10 +00003504public:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003505 /// \brief Constructor with insert-before-instruction semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003506 SIToFPInst(
3507 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003508 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003509 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003510 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3511 );
3512
Chandler Carruthacd01d12012-11-01 10:46:54 +00003513 /// \brief Constructor with insert-at-end-of-block semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003514 SIToFPInst(
3515 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003516 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003517 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003518 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3519 );
3520
Chandler Carruthacd01d12012-11-01 10:46:54 +00003521 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003522 static inline bool classof(const Instruction *I) {
3523 return I->getOpcode() == SIToFP;
3524 }
3525 static inline bool classof(const Value *V) {
3526 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3527 }
3528};
3529
3530//===----------------------------------------------------------------------===//
3531// FPToUIInst Class
3532//===----------------------------------------------------------------------===//
3533
Chandler Carruthacd01d12012-11-01 10:46:54 +00003534/// \brief This class represents a cast from floating point to unsigned integer
Reid Spencer3da59db2006-11-27 01:05:10 +00003535class FPToUIInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003536protected:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003537 /// \brief Clone an identical FPToUIInst
Devang Patel50b6e332009-10-27 22:16:29 +00003538 virtual FPToUIInst *clone_impl() const;
3539
Reid Spencer3da59db2006-11-27 01:05:10 +00003540public:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003541 /// \brief Constructor with insert-before-instruction semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003542 FPToUIInst(
3543 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003544 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003545 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003546 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3547 );
3548
Chandler Carruthacd01d12012-11-01 10:46:54 +00003549 /// \brief Constructor with insert-at-end-of-block semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003550 FPToUIInst(
3551 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003552 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003553 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003554 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3555 );
3556
Chandler Carruthacd01d12012-11-01 10:46:54 +00003557 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003558 static inline bool classof(const Instruction *I) {
3559 return I->getOpcode() == FPToUI;
3560 }
3561 static inline bool classof(const Value *V) {
3562 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3563 }
3564};
3565
3566//===----------------------------------------------------------------------===//
3567// FPToSIInst Class
3568//===----------------------------------------------------------------------===//
3569
Chandler Carruthacd01d12012-11-01 10:46:54 +00003570/// \brief This class represents a cast from floating point to signed integer.
Reid Spencer3da59db2006-11-27 01:05:10 +00003571class FPToSIInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003572protected:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003573 /// \brief Clone an identical FPToSIInst
Devang Patel50b6e332009-10-27 22:16:29 +00003574 virtual FPToSIInst *clone_impl() const;
3575
Reid Spencer3da59db2006-11-27 01:05:10 +00003576public:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003577 /// \brief Constructor with insert-before-instruction semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003578 FPToSIInst(
3579 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003580 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003581 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003582 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3583 );
3584
Chandler Carruthacd01d12012-11-01 10:46:54 +00003585 /// \brief Constructor with insert-at-end-of-block semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003586 FPToSIInst(
3587 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003588 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003589 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003590 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3591 );
3592
Chandler Carruthacd01d12012-11-01 10:46:54 +00003593 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003594 static inline bool classof(const Instruction *I) {
3595 return I->getOpcode() == FPToSI;
3596 }
3597 static inline bool classof(const Value *V) {
3598 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3599 }
3600};
3601
3602//===----------------------------------------------------------------------===//
3603// IntToPtrInst Class
3604//===----------------------------------------------------------------------===//
3605
Chandler Carruthacd01d12012-11-01 10:46:54 +00003606/// \brief This class represents a cast from an integer to a pointer.
Reid Spencer3da59db2006-11-27 01:05:10 +00003607class IntToPtrInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00003608public:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003609 /// \brief Constructor with insert-before-instruction semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003610 IntToPtrInst(
3611 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003612 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003613 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003614 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3615 );
3616
Chandler Carruthacd01d12012-11-01 10:46:54 +00003617 /// \brief Constructor with insert-at-end-of-block semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003618 IntToPtrInst(
3619 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003620 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003621 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003622 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3623 );
3624
Chandler Carruthacd01d12012-11-01 10:46:54 +00003625 /// \brief Clone an identical IntToPtrInst
Devang Patel50b6e332009-10-27 22:16:29 +00003626 virtual IntToPtrInst *clone_impl() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00003627
Chandler Carruth174fbec2012-11-01 11:25:55 +00003628 /// \brief Returns the address space of this instruction's pointer type.
Micah Villmow63b8ab22012-10-09 22:27:29 +00003629 unsigned getAddressSpace() const {
Chandler Carruth8fb614c2012-11-01 09:37:49 +00003630 return getType()->getPointerAddressSpace();
Micah Villmow63b8ab22012-10-09 22:27:29 +00003631 }
3632
Reid Spencer3da59db2006-11-27 01:05:10 +00003633 // Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003634 static inline bool classof(const Instruction *I) {
3635 return I->getOpcode() == IntToPtr;
3636 }
3637 static inline bool classof(const Value *V) {
3638 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3639 }
3640};
3641
3642//===----------------------------------------------------------------------===//
3643// PtrToIntInst Class
3644//===----------------------------------------------------------------------===//
3645
Chandler Carruthacd01d12012-11-01 10:46:54 +00003646/// \brief This class represents a cast from a pointer to an integer
Reid Spencer3da59db2006-11-27 01:05:10 +00003647class PtrToIntInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003648protected:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003649 /// \brief Clone an identical PtrToIntInst
Devang Patel50b6e332009-10-27 22:16:29 +00003650 virtual PtrToIntInst *clone_impl() const;
3651
Reid Spencer3da59db2006-11-27 01:05:10 +00003652public:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003653 /// \brief Constructor with insert-before-instruction semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003654 PtrToIntInst(
3655 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003656 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003657 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003658 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3659 );
3660
Chandler Carruthacd01d12012-11-01 10:46:54 +00003661 /// \brief Constructor with insert-at-end-of-block semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003662 PtrToIntInst(
3663 Value *S, ///< The value to be converted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003664 Type *Ty, ///< The type to convert to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003665 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003666 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3667 );
3668
Chandler Carruth8a967492012-11-01 11:16:47 +00003669 /// \brief Gets the pointer operand.
3670 Value *getPointerOperand() { return getOperand(0); }
3671 /// \brief Gets the pointer operand.
3672 const Value *getPointerOperand() const { return getOperand(0); }
3673 /// \brief Gets the operand index of the pointer operand.
3674 static unsigned getPointerOperandIndex() { return 0U; }
3675
3676 /// \brief Returns the address space of the pointer operand.
Micah Villmow63b8ab22012-10-09 22:27:29 +00003677 unsigned getPointerAddressSpace() const {
Chandler Carruth8a967492012-11-01 11:16:47 +00003678 return getPointerOperand()->getType()->getPointerAddressSpace();
Micah Villmow63b8ab22012-10-09 22:27:29 +00003679 }
3680
Reid Spencer3da59db2006-11-27 01:05:10 +00003681 // Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003682 static inline bool classof(const Instruction *I) {
3683 return I->getOpcode() == PtrToInt;
3684 }
3685 static inline bool classof(const Value *V) {
3686 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3687 }
3688};
3689
3690//===----------------------------------------------------------------------===//
3691// BitCastInst Class
3692//===----------------------------------------------------------------------===//
3693
Chandler Carruthacd01d12012-11-01 10:46:54 +00003694/// \brief This class represents a no-op cast from one type to another.
Reid Spencer3da59db2006-11-27 01:05:10 +00003695class BitCastInst : public CastInst {
Devang Patel50b6e332009-10-27 22:16:29 +00003696protected:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003697 /// \brief Clone an identical BitCastInst
Devang Patel50b6e332009-10-27 22:16:29 +00003698 virtual BitCastInst *clone_impl() const;
3699
Reid Spencer3da59db2006-11-27 01:05:10 +00003700public:
Chandler Carruthacd01d12012-11-01 10:46:54 +00003701 /// \brief Constructor with insert-before-instruction semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003702 BitCastInst(
3703 Value *S, ///< The value to be casted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003704 Type *Ty, ///< The type to casted to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003705 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003706 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3707 );
3708
Chandler Carruthacd01d12012-11-01 10:46:54 +00003709 /// \brief Constructor with insert-at-end-of-block semantics
Reid Spencer3da59db2006-11-27 01:05:10 +00003710 BitCastInst(
3711 Value *S, ///< The value to be casted
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003712 Type *Ty, ///< The type to casted to
Gabor Greifc61f6b42010-07-21 08:25:55 +00003713 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003714 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3715 );
3716
Reid Spencer3da59db2006-11-27 01:05:10 +00003717 // Methods for support type inquiry through isa, cast, and dyn_cast:
Reid Spencer3da59db2006-11-27 01:05:10 +00003718 static inline bool classof(const Instruction *I) {
3719 return I->getOpcode() == BitCast;
3720 }
3721 static inline bool classof(const Value *V) {
3722 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3723 }
3724};
3725
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003726} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00003727
3728#endif