blob: 272ccecfc418bf64f4571c5adcd5137a9f963f2c [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
David Greene52eec542007-08-01 03:43:44 +000019#include <iterator>
20
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000021#include "llvm/InstrTypes.h"
David Greene52eec542007-08-01 03:43:44 +000022#include "llvm/DerivedTypes.h"
Dale Johannesen0d51e7e2008-02-19 21:38:47 +000023#include "llvm/ParameterAttributes.h"
Gabor Greifefe65362008-05-10 08:32:32 +000024#include "llvm/BasicBlock.h"
Dan Gohman81a0c0b2008-05-31 00:58:22 +000025#include "llvm/ADT/SmallVector.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000026
27namespace llvm {
28
Chris Lattnerd1a32602005-02-24 05:32:09 +000029class ConstantInt;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000030class PointerType;
Reid Spencer9d6565a2007-02-15 02:26:10 +000031class VectorType;
Reid Spencer3da43842007-02-28 22:00:54 +000032class ConstantRange;
33class APInt;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000034
35//===----------------------------------------------------------------------===//
36// AllocationInst Class
37//===----------------------------------------------------------------------===//
38
39/// AllocationInst - This class is the common base class of MallocInst and
40/// AllocaInst.
41///
Chris Lattner454928e2005-01-29 00:31:36 +000042class AllocationInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000043protected:
Nate Begeman14b05292005-11-05 09:21:28 +000044 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
Misha Brukman91102862005-03-16 03:46:55 +000045 const std::string &Name = "", Instruction *InsertBefore = 0);
Nate Begeman14b05292005-11-05 09:21:28 +000046 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
Misha Brukman91102862005-03-16 03:46:55 +000047 const std::string &Name, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000048public:
Gabor Greif051a9502008-04-06 20:25:17 +000049 // Out of line virtual method, so the vtable, etc. has a home.
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000050 virtual ~AllocationInst();
51
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000052 /// isArrayAllocation - Return true if there is an allocation size parameter
53 /// to the allocation instruction that is not 1.
54 ///
55 bool isArrayAllocation() const;
56
57 /// getArraySize - Get the number of element allocated, for a simple
58 /// allocation of a single element, this will return a constant 1 value.
59 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000060 const Value *getArraySize() const { return getOperand(0); }
61 Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000062
63 /// getType - Overload to return most specific pointer type
64 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000065 const PointerType *getType() const {
Misha Brukman9769ab22005-04-21 20:19:05 +000066 return reinterpret_cast<const PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000067 }
68
69 /// getAllocatedType - Return the type that is being allocated by the
70 /// instruction.
71 ///
72 const Type *getAllocatedType() const;
73
Nate Begeman14b05292005-11-05 09:21:28 +000074 /// getAlignment - Return the alignment of the memory that is being allocated
75 /// by the instruction.
76 ///
Dan Gohman52837072008-03-24 16:55:58 +000077 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
78 void setAlignment(unsigned Align);
Chris Lattnerf56a8db2006-10-03 17:09:12 +000079
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000080 virtual Instruction *clone() const = 0;
81
82 // Methods for support type inquiry through isa, cast, and dyn_cast:
83 static inline bool classof(const AllocationInst *) { return true; }
84 static inline bool classof(const Instruction *I) {
85 return I->getOpcode() == Instruction::Alloca ||
86 I->getOpcode() == Instruction::Malloc;
87 }
88 static inline bool classof(const Value *V) {
89 return isa<Instruction>(V) && classof(cast<Instruction>(V));
90 }
91};
92
93
94//===----------------------------------------------------------------------===//
95// MallocInst Class
96//===----------------------------------------------------------------------===//
97
98/// MallocInst - an instruction to allocated memory on the heap
99///
100class MallocInst : public AllocationInst {
101 MallocInst(const MallocInst &MI);
102public:
103 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000104 const std::string &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000105 Instruction *InsertBefore = 0)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000106 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertBefore) {}
107 MallocInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000108 BasicBlock *InsertAtEnd)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000109 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000110
Evan Cheng1bf9a182008-07-24 00:08:56 +0000111 MallocInst(const Type *Ty, const std::string &NameStr,
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000112 Instruction *InsertBefore = 0)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000113 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
114 MallocInst(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
115 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000116
117 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000118 const std::string &NameStr, BasicBlock *InsertAtEnd)
119 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertAtEnd) {}
Chris Lattnerb77780e2006-05-10 04:38:35 +0000120 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000121 const std::string &NameStr = "",
Nate Begeman14b05292005-11-05 09:21:28 +0000122 Instruction *InsertBefore = 0)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000123 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertBefore) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000124
Chris Lattnerf319e832004-10-15 23:52:05 +0000125 virtual MallocInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000126
127 // Methods for support type inquiry through isa, cast, and dyn_cast:
128 static inline bool classof(const MallocInst *) { return true; }
129 static inline bool classof(const Instruction *I) {
130 return (I->getOpcode() == Instruction::Malloc);
131 }
132 static inline bool classof(const Value *V) {
133 return isa<Instruction>(V) && classof(cast<Instruction>(V));
134 }
135};
136
137
138//===----------------------------------------------------------------------===//
139// AllocaInst Class
140//===----------------------------------------------------------------------===//
141
142/// AllocaInst - an instruction to allocate memory on the stack
143///
144class AllocaInst : public AllocationInst {
145 AllocaInst(const AllocaInst &);
146public:
147 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000148 const std::string &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000149 Instruction *InsertBefore = 0)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000150 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertBefore) {}
151 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000152 BasicBlock *InsertAtEnd)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000153 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
Chris Lattnerb77780e2006-05-10 04:38:35 +0000154
Evan Cheng1bf9a182008-07-24 00:08:56 +0000155 AllocaInst(const Type *Ty, const std::string &NameStr,
Chris Lattnerb77780e2006-05-10 04:38:35 +0000156 Instruction *InsertBefore = 0)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000157 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
158 AllocaInst(const Type *Ty, const std::string &NameStr,
159 BasicBlock *InsertAtEnd)
160 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000161
Chris Lattnerb77780e2006-05-10 04:38:35 +0000162 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000163 const std::string &NameStr = "", Instruction *InsertBefore = 0)
164 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertBefore) {}
Nate Begeman14b05292005-11-05 09:21:28 +0000165 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000166 const std::string &NameStr, BasicBlock *InsertAtEnd)
167 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000168
Chris Lattnerf319e832004-10-15 23:52:05 +0000169 virtual AllocaInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000170
171 // Methods for support type inquiry through isa, cast, and dyn_cast:
172 static inline bool classof(const AllocaInst *) { return true; }
173 static inline bool classof(const Instruction *I) {
174 return (I->getOpcode() == Instruction::Alloca);
175 }
176 static inline bool classof(const Value *V) {
177 return isa<Instruction>(V) && classof(cast<Instruction>(V));
178 }
179};
180
181
182//===----------------------------------------------------------------------===//
183// FreeInst Class
184//===----------------------------------------------------------------------===//
185
186/// FreeInst - an instruction to deallocate memory
187///
Chris Lattner454928e2005-01-29 00:31:36 +0000188class FreeInst : public UnaryInstruction {
189 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000190public:
191 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
192 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
193
Chris Lattnerf319e832004-10-15 23:52:05 +0000194 virtual FreeInst *clone() const;
Owen Andersonc9edf0b2007-07-06 23:13:31 +0000195
196 // Accessor methods for consistency with other memory operations
197 Value *getPointerOperand() { return getOperand(0); }
198 const Value *getPointerOperand() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000199
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000200 // Methods for support type inquiry through isa, cast, and dyn_cast:
201 static inline bool classof(const FreeInst *) { return true; }
202 static inline bool classof(const Instruction *I) {
203 return (I->getOpcode() == Instruction::Free);
204 }
205 static inline bool classof(const Value *V) {
206 return isa<Instruction>(V) && classof(cast<Instruction>(V));
207 }
208};
209
210
211//===----------------------------------------------------------------------===//
212// LoadInst Class
213//===----------------------------------------------------------------------===//
214
Chris Lattner88fe29a2005-02-05 01:44:18 +0000215/// LoadInst - an instruction for reading from memory. This uses the
216/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000217///
Chris Lattner454928e2005-01-29 00:31:36 +0000218class LoadInst : public UnaryInstruction {
Christopher Lamb43c7f372007-04-22 19:24:39 +0000219
Chris Lattner454928e2005-01-29 00:31:36 +0000220 LoadInst(const LoadInst &LI)
Chris Lattner88fe29a2005-02-05 01:44:18 +0000221 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
222 setVolatile(LI.isVolatile());
Christopher Lamb43c7f372007-04-22 19:24:39 +0000223 setAlignment(LI.getAlignment());
Misha Brukman9769ab22005-04-21 20:19:05 +0000224
Chris Lattner454928e2005-01-29 00:31:36 +0000225#ifndef NDEBUG
226 AssertOK();
227#endif
228 }
229 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000230public:
Evan Cheng1bf9a182008-07-24 00:08:56 +0000231 LoadInst(Value *Ptr, const std::string &NameStr, Instruction *InsertBefore);
232 LoadInst(Value *Ptr, const std::string &NameStr, BasicBlock *InsertAtEnd);
233 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile = false,
Christopher Lamb43c7f372007-04-22 19:24:39 +0000234 Instruction *InsertBefore = 0);
Evan Cheng1bf9a182008-07-24 00:08:56 +0000235 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
236 unsigned Align, Instruction *InsertBefore = 0);
237 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000238 BasicBlock *InsertAtEnd);
Evan Cheng1bf9a182008-07-24 00:08:56 +0000239 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
240 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000241
Evan Cheng1bf9a182008-07-24 00:08:56 +0000242 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
243 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
244 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
245 bool isVolatile = false, Instruction *InsertBefore = 0);
246 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
Chris Lattnerf00042a2007-02-13 07:54:42 +0000247 BasicBlock *InsertAtEnd);
248
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000249 /// isVolatile - Return true if this is a load from a volatile memory
250 /// location.
251 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000252 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000253
254 /// setVolatile - Specify whether this is a volatile load or not.
255 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000256 void setVolatile(bool V) {
Hartmut Kaiserefd4a512007-10-17 14:56:40 +0000257 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000258 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000259
Chris Lattnerf319e832004-10-15 23:52:05 +0000260 virtual LoadInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000261
Christopher Lamb43c7f372007-04-22 19:24:39 +0000262 /// getAlignment - Return the alignment of the access that is being performed
263 ///
264 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000265 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000266 }
267
268 void setAlignment(unsigned Align);
269
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000270 Value *getPointerOperand() { return getOperand(0); }
271 const Value *getPointerOperand() const { return getOperand(0); }
272 static unsigned getPointerOperandIndex() { return 0U; }
273
274 // Methods for support type inquiry through isa, cast, and dyn_cast:
275 static inline bool classof(const LoadInst *) { return true; }
276 static inline bool classof(const Instruction *I) {
277 return I->getOpcode() == Instruction::Load;
278 }
279 static inline bool classof(const Value *V) {
280 return isa<Instruction>(V) && classof(cast<Instruction>(V));
281 }
282};
283
284
285//===----------------------------------------------------------------------===//
286// StoreInst Class
287//===----------------------------------------------------------------------===//
288
Misha Brukman9769ab22005-04-21 20:19:05 +0000289/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000290///
291class StoreInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +0000292 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Christopher Lamb43c7f372007-04-22 19:24:39 +0000293
Gabor Greifefe65362008-05-10 08:32:32 +0000294 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
295 &Op<0>(), 2) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000296 Op<0>() = SI.Op<0>();
297 Op<1>() = SI.Op<1>();
Chris Lattner88fe29a2005-02-05 01:44:18 +0000298 setVolatile(SI.isVolatile());
Christopher Lamb43c7f372007-04-22 19:24:39 +0000299 setAlignment(SI.getAlignment());
300
Chris Lattner454928e2005-01-29 00:31:36 +0000301#ifndef NDEBUG
302 AssertOK();
303#endif
304 }
305 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000306public:
Gabor Greif051a9502008-04-06 20:25:17 +0000307 // allocate space for exactly two operands
308 void *operator new(size_t s) {
309 return User::operator new(s, 2);
310 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000311 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
312 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
313 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
314 Instruction *InsertBefore = 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000315 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
316 unsigned Align, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000317 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000318 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
319 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000320
321
322 /// isVolatile - Return true if this is a load from a volatile memory
323 /// location.
324 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000325 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000326
327 /// setVolatile - Specify whether this is a volatile load or not.
328 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000329 void setVolatile(bool V) {
Hartmut Kaiserefd4a512007-10-17 14:56:40 +0000330 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000331 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000332
Chris Lattner454928e2005-01-29 00:31:36 +0000333 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +0000334 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner454928e2005-01-29 00:31:36 +0000335
Christopher Lamb43c7f372007-04-22 19:24:39 +0000336 /// getAlignment - Return the alignment of the access that is being performed
337 ///
338 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000339 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000340 }
341
342 void setAlignment(unsigned Align);
343
Chris Lattnerf319e832004-10-15 23:52:05 +0000344 virtual StoreInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000345
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000346 Value *getPointerOperand() { return getOperand(1); }
347 const Value *getPointerOperand() const { return getOperand(1); }
348 static unsigned getPointerOperandIndex() { return 1U; }
349
350 // Methods for support type inquiry through isa, cast, and dyn_cast:
351 static inline bool classof(const StoreInst *) { return true; }
352 static inline bool classof(const Instruction *I) {
353 return I->getOpcode() == Instruction::Store;
354 }
355 static inline bool classof(const Value *V) {
356 return isa<Instruction>(V) && classof(cast<Instruction>(V));
357 }
358};
359
Gabor Greifefe65362008-05-10 08:32:32 +0000360template <>
361struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
362};
363
364DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000365
366//===----------------------------------------------------------------------===//
367// GetElementPtrInst Class
368//===----------------------------------------------------------------------===//
369
David Greeneb8f74792007-09-04 15:46:09 +0000370// checkType - Simple wrapper function to give a better assertion failure
371// message on bad indexes for a gep instruction.
372//
373static inline const Type *checkType(const Type *Ty) {
374 assert(Ty && "Invalid GetElementPtrInst indices for type!");
375 return Ty;
376}
377
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000378/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
379/// access elements of arrays and structs
380///
381class GetElementPtrInst : public Instruction {
Gabor Greifefe65362008-05-10 08:32:32 +0000382 GetElementPtrInst(const GetElementPtrInst &GEPI);
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +0000383 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000384 const std::string &NameStr);
385 void init(Value *Ptr, Value *Idx, const std::string &NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000386
387 template<typename InputIterator>
388 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000389 const std::string &NameStr,
David Greeneb8f74792007-09-04 15:46:09 +0000390 // This argument ensures that we have an iterator we can
391 // do arithmetic on in constant time
392 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000393 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
David Greeneb8f74792007-09-04 15:46:09 +0000394
395 if (NumIdx > 0) {
Gabor Greifefe65362008-05-10 08:32:32 +0000396 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +0000397 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Gabor Greifefe65362008-05-10 08:32:32 +0000398 // we have to build an array here
David Greeneb8f74792007-09-04 15:46:09 +0000399 }
400 else {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000401 init(Ptr, 0, NumIdx, NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000402 }
David Greeneb8f74792007-09-04 15:46:09 +0000403 }
404
405 /// getIndexedType - Returns the type of the element that would be loaded with
406 /// a load instruction with the specified parameters.
407 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000408 /// Null is returned if the indices are invalid for the specified
David Greeneb8f74792007-09-04 15:46:09 +0000409 /// pointer type.
410 ///
David Greeneb8f74792007-09-04 15:46:09 +0000411 template<typename InputIterator>
412 static const Type *getIndexedType(const Type *Ptr,
413 InputIterator IdxBegin,
414 InputIterator IdxEnd,
David Greeneb8f74792007-09-04 15:46:09 +0000415 // This argument ensures that we
416 // have an iterator we can do
417 // arithmetic on in constant time
418 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000419 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
David Greeneb8f74792007-09-04 15:46:09 +0000420
Dan Gohman041e2eb2008-05-15 19:50:34 +0000421 if (NumIdx > 0)
David Greeneb8f74792007-09-04 15:46:09 +0000422 // This requires that the iterator points to contiguous memory.
Dan Gohman041e2eb2008-05-15 19:50:34 +0000423 return getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx);
424 else
425 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
David Greeneb8f74792007-09-04 15:46:09 +0000426 }
427
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000428 /// Constructors - Create a getelementptr instruction with a base pointer an
429 /// list of indices. The first ctor can optionally insert before an existing
430 /// instruction, the second appends the new instruction to the specified
431 /// BasicBlock.
David Greeneb8f74792007-09-04 15:46:09 +0000432 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000433 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
434 InputIterator IdxEnd,
435 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000436 const std::string &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000437 Instruction *InsertBefore);
David Greeneb8f74792007-09-04 15:46:09 +0000438 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000439 inline GetElementPtrInst(Value *Ptr,
440 InputIterator IdxBegin, InputIterator IdxEnd,
441 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000442 const std::string &NameStr, BasicBlock *InsertAtEnd);
David Greeneb8f74792007-09-04 15:46:09 +0000443
Chris Lattner38bacf22005-05-03 05:43:30 +0000444 /// Constructors - These two constructors are convenience methods because one
445 /// and two index getelementptr instructions are so common.
Evan Cheng1bf9a182008-07-24 00:08:56 +0000446 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +0000447 Instruction *InsertBefore = 0);
Chris Lattner38bacf22005-05-03 05:43:30 +0000448 GetElementPtrInst(Value *Ptr, Value *Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000449 const std::string &NameStr, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000450public:
451 template<typename InputIterator>
452 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
453 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000454 const std::string &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000455 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +0000456 typename std::iterator_traits<InputIterator>::difference_type Values =
457 1 + std::distance(IdxBegin, IdxEnd);
458 return new(Values)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000459 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000460 }
461 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000462 static GetElementPtrInst *Create(Value *Ptr,
463 InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000464 const std::string &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000465 BasicBlock *InsertAtEnd) {
466 typename std::iterator_traits<InputIterator>::difference_type Values =
467 1 + std::distance(IdxBegin, IdxEnd);
468 return new(Values)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000469 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000470 }
471
Gabor Greifefe65362008-05-10 08:32:32 +0000472 /// Constructors - These two creators are convenience methods because one
473 /// index getelementptr instructions are so common.
Gabor Greif051a9502008-04-06 20:25:17 +0000474 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000475 const std::string &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +0000476 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000477 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000478 }
479 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000480 const std::string &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000481 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000482 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000483 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000484
Chris Lattnerf319e832004-10-15 23:52:05 +0000485 virtual GetElementPtrInst *clone() const;
Misha Brukman9769ab22005-04-21 20:19:05 +0000486
Gabor Greifefe65362008-05-10 08:32:32 +0000487 /// Transparently provide more efficient getOperand methods.
488 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
489
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000490 // getType - Overload to return most specific pointer type...
Devang Patel4d4a5e02008-02-23 01:11:02 +0000491 const PointerType *getType() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000492 return reinterpret_cast<const PointerType*>(Instruction::getType());
493 }
494
495 /// getIndexedType - Returns the type of the element that would be loaded with
496 /// a load instruction with the specified parameters.
497 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000498 /// Null is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000499 /// pointer type.
500 ///
David Greeneb8f74792007-09-04 15:46:09 +0000501 template<typename InputIterator>
Misha Brukman9769ab22005-04-21 20:19:05 +0000502 static const Type *getIndexedType(const Type *Ptr,
David Greeneb8f74792007-09-04 15:46:09 +0000503 InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +0000504 InputIterator IdxEnd) {
505 return getIndexedType(Ptr, IdxBegin, IdxEnd,
David Greeneb8f74792007-09-04 15:46:09 +0000506 typename std::iterator_traits<InputIterator>::
Dan Gohman041e2eb2008-05-15 19:50:34 +0000507 iterator_category());
David Greeneb8f74792007-09-04 15:46:09 +0000508 }
Matthijs Kooijmane2afded2008-07-29 08:46:11 +0000509
510 static const Type *getIndexedType(const Type *Ptr,
511 Value* const *Idx, unsigned NumIdx);
512
513 static const Type *getIndexedType(const Type *Ptr,
514 uint64_t const *Idx, unsigned NumIdx);
515
Chris Lattner38bacf22005-05-03 05:43:30 +0000516 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
Misha Brukman9769ab22005-04-21 20:19:05 +0000517
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000518 inline op_iterator idx_begin() { return op_begin()+1; }
519 inline const_op_iterator idx_begin() const { return op_begin()+1; }
520 inline op_iterator idx_end() { return op_end(); }
521 inline const_op_iterator idx_end() const { return op_end(); }
522
523 Value *getPointerOperand() {
524 return getOperand(0);
525 }
526 const Value *getPointerOperand() const {
527 return getOperand(0);
528 }
529 static unsigned getPointerOperandIndex() {
530 return 0U; // get index for modifying correct operand
531 }
532
Devang Patel4d4a5e02008-02-23 01:11:02 +0000533 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000534 return getNumOperands() - 1;
535 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000536
Devang Patel4d4a5e02008-02-23 01:11:02 +0000537 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000538 return getNumOperands() > 1;
539 }
Chris Lattner6f771d42007-04-14 00:12:57 +0000540
541 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
542 /// zeros. If so, the result pointer and the first operand have the same
543 /// value, just potentially different types.
544 bool hasAllZeroIndices() const;
Chris Lattner6b0974c2007-04-27 20:35:56 +0000545
546 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
547 /// constant integers. If so, the result pointer and the first operand have
548 /// a constant offset between them.
549 bool hasAllConstantIndices() const;
550
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000551
552 // Methods for support type inquiry through isa, cast, and dyn_cast:
553 static inline bool classof(const GetElementPtrInst *) { return true; }
554 static inline bool classof(const Instruction *I) {
555 return (I->getOpcode() == Instruction::GetElementPtr);
556 }
557 static inline bool classof(const Value *V) {
558 return isa<Instruction>(V) && classof(cast<Instruction>(V));
559 }
560};
561
Gabor Greifefe65362008-05-10 08:32:32 +0000562template <>
563struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
564};
565
566template<typename InputIterator>
567GetElementPtrInst::GetElementPtrInst(Value *Ptr,
568 InputIterator IdxBegin,
569 InputIterator IdxEnd,
570 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000571 const std::string &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000572 Instruction *InsertBefore)
573 : Instruction(PointerType::get(checkType(
574 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000575 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000576 cast<PointerType>(Ptr->getType())
577 ->getAddressSpace()),
578 GetElementPtr,
579 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
580 Values, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000581 init(Ptr, IdxBegin, IdxEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000582 typename std::iterator_traits<InputIterator>::iterator_category());
583}
584template<typename InputIterator>
585GetElementPtrInst::GetElementPtrInst(Value *Ptr,
586 InputIterator IdxBegin,
587 InputIterator IdxEnd,
588 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000589 const std::string &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000590 BasicBlock *InsertAtEnd)
591 : Instruction(PointerType::get(checkType(
592 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000593 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000594 cast<PointerType>(Ptr->getType())
595 ->getAddressSpace()),
596 GetElementPtr,
597 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
598 Values, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000599 init(Ptr, IdxBegin, IdxEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000600 typename std::iterator_traits<InputIterator>::iterator_category());
601}
602
603
604DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
605
606
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000607//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000608// ICmpInst Class
609//===----------------------------------------------------------------------===//
610
611/// This instruction compares its operands according to the predicate given
Nate Begemanac80ade2008-05-12 19:01:56 +0000612/// to the constructor. It only operates on integers or pointers. The operands
613/// must be identical types.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000614/// @brief Represent an integer comparison operator.
615class ICmpInst: public CmpInst {
616public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000617 /// @brief Constructor with insert-before-instruction semantics.
618 ICmpInst(
619 Predicate pred, ///< The predicate to use for the comparison
620 Value *LHS, ///< The left-hand-side of the expression
621 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000622 const std::string &NameStr = "", ///< Name of the instruction
Reid Spencer45fb3f32006-11-20 01:22:35 +0000623 Instruction *InsertBefore = 0 ///< Where to insert
Dan Gohmanf72fb672008-09-09 01:02:47 +0000624 ) : CmpInst(makeCmpResultType(LHS->getType()),
625 Instruction::ICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000626 InsertBefore) {
627 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
628 pred <= CmpInst::LAST_ICMP_PREDICATE &&
629 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000630 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000631 "Both operands to ICmp instruction are not of the same type!");
632 // Check that the operands are the right type
Dan Gohmanf72fb672008-09-09 01:02:47 +0000633 assert((getOperand(0)->getType()->isIntOrIntVector() ||
Nate Begeman31cd33a2008-05-14 20:28:31 +0000634 isa<PointerType>(getOperand(0)->getType())) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000635 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000636 }
637
638 /// @brief Constructor with insert-at-block-end semantics.
639 ICmpInst(
640 Predicate pred, ///< The predicate to use for the comparison
641 Value *LHS, ///< The left-hand-side of the expression
642 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000643 const std::string &NameStr, ///< Name of the instruction
Reid Spencer45fb3f32006-11-20 01:22:35 +0000644 BasicBlock *InsertAtEnd ///< Block to insert into.
Dan Gohmanf72fb672008-09-09 01:02:47 +0000645 ) : CmpInst(makeCmpResultType(LHS->getType()),
646 Instruction::ICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000647 InsertAtEnd) {
648 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
649 pred <= CmpInst::LAST_ICMP_PREDICATE &&
650 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000651 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000652 "Both operands to ICmp instruction are not of the same type!");
653 // Check that the operands are the right type
Dan Gohmanf72fb672008-09-09 01:02:47 +0000654 assert((getOperand(0)->getType()->isIntOrIntVector() ||
Nate Begeman31cd33a2008-05-14 20:28:31 +0000655 isa<PointerType>(getOperand(0)->getType())) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000656 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000657 }
658
Reid Spencere4d87aa2006-12-23 06:05:41 +0000659 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
660 /// @returns the predicate that would be the result if the operand were
661 /// regarded as signed.
662 /// @brief Return the signed version of the predicate
663 Predicate getSignedPredicate() const {
664 return getSignedPredicate(getPredicate());
665 }
666
667 /// This is a static version that you can use without an instruction.
668 /// @brief Return the signed version of the predicate.
669 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000670
Nick Lewycky4189a532008-01-28 03:48:02 +0000671 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
672 /// @returns the predicate that would be the result if the operand were
673 /// regarded as unsigned.
674 /// @brief Return the unsigned version of the predicate
675 Predicate getUnsignedPredicate() const {
676 return getUnsignedPredicate(getPredicate());
677 }
678
679 /// This is a static version that you can use without an instruction.
680 /// @brief Return the unsigned version of the predicate.
681 static Predicate getUnsignedPredicate(Predicate pred);
682
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000683 /// isEquality - Return true if this predicate is either EQ or NE. This also
684 /// tests for commutativity.
685 static bool isEquality(Predicate P) {
686 return P == ICMP_EQ || P == ICMP_NE;
687 }
688
689 /// isEquality - Return true if this predicate is either EQ or NE. This also
690 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000691 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000692 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000693 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000694
695 /// @returns true if the predicate of this ICmpInst is commutative
696 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000697 bool isCommutative() const { return isEquality(); }
698
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000699 /// isRelational - Return true if the predicate is relational (not EQ or NE).
700 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +0000701 bool isRelational() const {
702 return !isEquality();
703 }
704
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000705 /// isRelational - Return true if the predicate is relational (not EQ or NE).
706 ///
707 static bool isRelational(Predicate P) {
708 return !isEquality(P);
709 }
710
Reid Spencere4d87aa2006-12-23 06:05:41 +0000711 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
712 /// @brief Determine if this instruction's predicate is signed.
Chris Lattner5bda9e42007-09-15 06:51:03 +0000713 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000714
715 /// @returns true if the predicate provided is signed, false otherwise
716 /// @brief Determine if the predicate is signed.
717 static bool isSignedPredicate(Predicate pred);
718
Nick Lewyckyfc1efbb2008-05-17 07:33:39 +0000719 /// @returns true if the specified compare predicate is
720 /// true when both operands are equal...
721 /// @brief Determine if the icmp is true when both operands are equal
722 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
723 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
724 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
725 pred == ICmpInst::ICMP_SLE;
726 }
727
728 /// @returns true if the specified compare instruction is
729 /// true when both operands are equal...
730 /// @brief Determine if the ICmpInst returns true when both operands are equal
731 bool isTrueWhenEqual() {
732 return isTrueWhenEqual(getPredicate());
733 }
734
Reid Spencer3da43842007-02-28 22:00:54 +0000735 /// Initialize a set of values that all satisfy the predicate with C.
736 /// @brief Make a ConstantRange for a relation with a constant value.
737 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
738
Reid Spencer45fb3f32006-11-20 01:22:35 +0000739 /// Exchange the two operands to this instruction in such a way that it does
740 /// not modify the semantics of the instruction. The predicate value may be
741 /// changed to retain the same result if the predicate is order dependent
742 /// (e.g. ult).
743 /// @brief Swap operands and adjust predicate.
744 void swapOperands() {
745 SubclassData = getSwappedPredicate();
Gabor Greif94fb68b2008-05-13 22:51:52 +0000746 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000747 }
748
Chris Lattnercd406fe2007-08-24 20:48:18 +0000749 virtual ICmpInst *clone() const;
750
Reid Spencer45fb3f32006-11-20 01:22:35 +0000751 // Methods for support type inquiry through isa, cast, and dyn_cast:
752 static inline bool classof(const ICmpInst *) { return true; }
753 static inline bool classof(const Instruction *I) {
754 return I->getOpcode() == Instruction::ICmp;
755 }
756 static inline bool classof(const Value *V) {
757 return isa<Instruction>(V) && classof(cast<Instruction>(V));
758 }
Dan Gohmanf72fb672008-09-09 01:02:47 +0000759
Reid Spencer45fb3f32006-11-20 01:22:35 +0000760};
761
762//===----------------------------------------------------------------------===//
763// FCmpInst Class
764//===----------------------------------------------------------------------===//
765
766/// This instruction compares its operands according to the predicate given
767/// to the constructor. It only operates on floating point values or packed
768/// vectors of floating point values. The operands must be identical types.
769/// @brief Represents a floating point comparison operator.
770class FCmpInst: public CmpInst {
771public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000772 /// @brief Constructor with insert-before-instruction semantics.
773 FCmpInst(
774 Predicate pred, ///< The predicate to use for the comparison
775 Value *LHS, ///< The left-hand-side of the expression
776 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000777 const std::string &NameStr = "", ///< Name of the instruction
Reid Spencer45fb3f32006-11-20 01:22:35 +0000778 Instruction *InsertBefore = 0 ///< Where to insert
Dan Gohmanf72fb672008-09-09 01:02:47 +0000779 ) : CmpInst(makeCmpResultType(LHS->getType()),
780 Instruction::FCmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000781 InsertBefore) {
782 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
783 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000784 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000785 "Both operands to FCmp instruction are not of the same type!");
786 // Check that the operands are the right type
Dan Gohmanf72fb672008-09-09 01:02:47 +0000787 assert(getOperand(0)->getType()->isFPOrFPVector() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000788 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000789 }
790
791 /// @brief Constructor with insert-at-block-end semantics.
792 FCmpInst(
793 Predicate pred, ///< The predicate to use for the comparison
794 Value *LHS, ///< The left-hand-side of the expression
795 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000796 const std::string &NameStr, ///< Name of the instruction
Reid Spencer45fb3f32006-11-20 01:22:35 +0000797 BasicBlock *InsertAtEnd ///< Block to insert into.
Dan Gohmanf72fb672008-09-09 01:02:47 +0000798 ) : CmpInst(makeCmpResultType(LHS->getType()),
799 Instruction::FCmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000800 InsertAtEnd) {
801 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
802 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000803 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000804 "Both operands to FCmp instruction are not of the same type!");
805 // Check that the operands are the right type
Dan Gohmanf72fb672008-09-09 01:02:47 +0000806 assert(getOperand(0)->getType()->isFPOrFPVector() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000807 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000808 }
809
Reid Spencer45fb3f32006-11-20 01:22:35 +0000810 /// @returns true if the predicate of this instruction is EQ or NE.
811 /// @brief Determine if this is an equality predicate.
812 bool isEquality() const {
813 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
814 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
815 }
Dan Gohman793df072008-09-16 16:44:00 +0000816
817 /// @returns true if the predicate of this instruction is commutative.
818 /// @brief Determine if this is a commutative predicate.
819 bool isCommutative() const {
820 return isEquality() ||
821 SubclassData == FCMP_FALSE ||
822 SubclassData == FCMP_TRUE ||
823 SubclassData == FCMP_ORD ||
824 SubclassData == FCMP_UNO;
825 }
Reid Spencer45fb3f32006-11-20 01:22:35 +0000826
827 /// @returns true if the predicate is relational (not EQ or NE).
828 /// @brief Determine if this a relational predicate.
829 bool isRelational() const { return !isEquality(); }
830
831 /// Exchange the two operands to this instruction in such a way that it does
832 /// not modify the semantics of the instruction. The predicate value may be
833 /// changed to retain the same result if the predicate is order dependent
834 /// (e.g. ult).
835 /// @brief Swap operands and adjust predicate.
836 void swapOperands() {
837 SubclassData = getSwappedPredicate();
Gabor Greif94fb68b2008-05-13 22:51:52 +0000838 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000839 }
840
Chris Lattnercd406fe2007-08-24 20:48:18 +0000841 virtual FCmpInst *clone() const;
842
Reid Spencer45fb3f32006-11-20 01:22:35 +0000843 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
844 static inline bool classof(const FCmpInst *) { return true; }
845 static inline bool classof(const Instruction *I) {
846 return I->getOpcode() == Instruction::FCmp;
847 }
848 static inline bool classof(const Value *V) {
849 return isa<Instruction>(V) && classof(cast<Instruction>(V));
850 }
Dan Gohmanf72fb672008-09-09 01:02:47 +0000851
Reid Spencer45fb3f32006-11-20 01:22:35 +0000852};
853
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000854//===----------------------------------------------------------------------===//
Nate Begemanac80ade2008-05-12 19:01:56 +0000855// VICmpInst Class
856//===----------------------------------------------------------------------===//
857
858/// This instruction compares its operands according to the predicate given
859/// to the constructor. It only operates on vectors of integers.
860/// The operands must be identical types.
861/// @brief Represents a vector integer comparison operator.
862class VICmpInst: public CmpInst {
863public:
864 /// @brief Constructor with insert-before-instruction semantics.
865 VICmpInst(
866 Predicate pred, ///< The predicate to use for the comparison
867 Value *LHS, ///< The left-hand-side of the expression
868 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000869 const std::string &NameStr = "", ///< Name of the instruction
Nate Begemanac80ade2008-05-12 19:01:56 +0000870 Instruction *InsertBefore = 0 ///< Where to insert
Evan Cheng1bf9a182008-07-24 00:08:56 +0000871 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000872 InsertBefore) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000873 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
874 pred <= CmpInst::LAST_ICMP_PREDICATE &&
875 "Invalid VICmp predicate value");
876 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
877 "Both operands to VICmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000878 }
879
880 /// @brief Constructor with insert-at-block-end semantics.
881 VICmpInst(
882 Predicate pred, ///< The predicate to use for the comparison
883 Value *LHS, ///< The left-hand-side of the expression
884 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000885 const std::string &NameStr, ///< Name of the instruction
Nate Begemanac80ade2008-05-12 19:01:56 +0000886 BasicBlock *InsertAtEnd ///< Block to insert into.
Evan Cheng1bf9a182008-07-24 00:08:56 +0000887 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000888 InsertAtEnd) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000889 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
890 pred <= CmpInst::LAST_ICMP_PREDICATE &&
891 "Invalid VICmp predicate value");
892 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
893 "Both operands to VICmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000894 }
895
896 /// @brief Return the predicate for this instruction.
897 Predicate getPredicate() const { return Predicate(SubclassData); }
898
899 virtual VICmpInst *clone() const;
900
901 // Methods for support type inquiry through isa, cast, and dyn_cast:
902 static inline bool classof(const VICmpInst *) { return true; }
903 static inline bool classof(const Instruction *I) {
904 return I->getOpcode() == Instruction::VICmp;
905 }
906 static inline bool classof(const Value *V) {
907 return isa<Instruction>(V) && classof(cast<Instruction>(V));
908 }
909};
910
911//===----------------------------------------------------------------------===//
912// VFCmpInst Class
913//===----------------------------------------------------------------------===//
914
915/// This instruction compares its operands according to the predicate given
916/// to the constructor. It only operates on vectors of floating point values.
917/// The operands must be identical types.
918/// @brief Represents a vector floating point comparison operator.
919class VFCmpInst: public CmpInst {
920public:
921 /// @brief Constructor with insert-before-instruction semantics.
922 VFCmpInst(
923 Predicate pred, ///< The predicate to use for the comparison
924 Value *LHS, ///< The left-hand-side of the expression
925 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000926 const std::string &NameStr = "", ///< Name of the instruction
Nate Begemanac80ade2008-05-12 19:01:56 +0000927 Instruction *InsertBefore = 0 ///< Where to insert
928 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
Evan Cheng1bf9a182008-07-24 00:08:56 +0000929 Instruction::VFCmp, pred, LHS, RHS, NameStr, InsertBefore) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000930 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
931 "Invalid VFCmp predicate value");
932 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
933 "Both operands to VFCmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000934 }
935
936 /// @brief Constructor with insert-at-block-end semantics.
937 VFCmpInst(
938 Predicate pred, ///< The predicate to use for the comparison
939 Value *LHS, ///< The left-hand-side of the expression
940 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000941 const std::string &NameStr, ///< Name of the instruction
Nate Begemanac80ade2008-05-12 19:01:56 +0000942 BasicBlock *InsertAtEnd ///< Block to insert into.
943 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
Evan Cheng1bf9a182008-07-24 00:08:56 +0000944 Instruction::VFCmp, pred, LHS, RHS, NameStr, InsertAtEnd) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000945 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
946 "Invalid VFCmp predicate value");
947 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
948 "Both operands to VFCmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000949 }
950
951 /// @brief Return the predicate for this instruction.
952 Predicate getPredicate() const { return Predicate(SubclassData); }
953
954 virtual VFCmpInst *clone() const;
955
956 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
957 static inline bool classof(const VFCmpInst *) { return true; }
958 static inline bool classof(const Instruction *I) {
959 return I->getOpcode() == Instruction::VFCmp;
960 }
961 static inline bool classof(const Value *V) {
962 return isa<Instruction>(V) && classof(cast<Instruction>(V));
963 }
964};
965
966//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000967// CallInst Class
968//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000969/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +0000970/// machine's calling convention. This class uses low bit of the SubClassData
971/// field to indicate whether or not this is a tail call. The rest of the bits
972/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000973///
David Greene52eec542007-08-01 03:43:44 +0000974
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000975class CallInst : public Instruction {
Chris Lattner58d74912008-03-12 17:45:29 +0000976 PAListPtr ParamAttrs; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000977 CallInst(const CallInst &CI);
Chris Lattnerd54f4322007-02-13 00:58:44 +0000978 void init(Value *Func, Value* const *Params, unsigned NumParams);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000979 void init(Value *Func, Value *Actual1, Value *Actual2);
980 void init(Value *Func, Value *Actual);
981 void init(Value *Func);
982
David Greene52eec542007-08-01 03:43:44 +0000983 template<typename InputIterator>
984 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000985 const std::string &NameStr,
David Greene52eec542007-08-01 03:43:44 +0000986 // This argument ensures that we have an iterator we can
987 // do arithmetic on in constant time
988 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000989 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greene52eec542007-08-01 03:43:44 +0000990
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000991 // This requires that the iterator points to contiguous memory.
992 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
Evan Cheng1bf9a182008-07-24 00:08:56 +0000993 setName(NameStr);
David Greene52eec542007-08-01 03:43:44 +0000994 }
995
David Greene52eec542007-08-01 03:43:44 +0000996 /// Construct a CallInst given a range of arguments. InputIterator
997 /// must be a random-access iterator pointing to contiguous storage
998 /// (e.g. a std::vector<>::iterator). Checks are made for
999 /// random-accessness but not for contiguous storage as that would
1000 /// incur runtime overhead.
1001 /// @brief Construct a CallInst from a range of arguments
1002 template<typename InputIterator>
1003 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001004 const std::string &NameStr, Instruction *InsertBefore);
David Greene52eec542007-08-01 03:43:44 +00001005
1006 /// Construct a CallInst given a range of arguments. InputIterator
1007 /// must be a random-access iterator pointing to contiguous storage
1008 /// (e.g. a std::vector<>::iterator). Checks are made for
1009 /// random-accessness but not for contiguous storage as that would
1010 /// incur runtime overhead.
1011 /// @brief Construct a CallInst from a range of arguments
1012 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00001013 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001014 const std::string &NameStr, BasicBlock *InsertAtEnd);
David Greene52eec542007-08-01 03:43:44 +00001015
Evan Cheng1bf9a182008-07-24 00:08:56 +00001016 CallInst(Value *F, Value *Actual, const std::string& NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001017 Instruction *InsertBefore);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001018 CallInst(Value *F, Value *Actual, const std::string& NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001019 BasicBlock *InsertAtEnd);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001020 explicit CallInst(Value *F, const std::string &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001021 Instruction *InsertBefore);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001022 CallInst(Value *F, const std::string &NameStr, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001023public:
1024 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00001025 static CallInst *Create(Value *Func,
1026 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001027 const std::string &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001028 Instruction *InsertBefore = 0) {
Bill Wendlingc2e73532008-05-10 19:59:59 +00001029 return new((unsigned)(ArgEnd - ArgBegin + 1))
Evan Cheng1bf9a182008-07-24 00:08:56 +00001030 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001031 }
1032 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00001033 static CallInst *Create(Value *Func,
1034 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001035 const std::string &NameStr, BasicBlock *InsertAtEnd) {
Bill Wendlingc2e73532008-05-10 19:59:59 +00001036 return new((unsigned)(ArgEnd - ArgBegin + 1))
Evan Cheng1bf9a182008-07-24 00:08:56 +00001037 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001038 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001039 static CallInst *Create(Value *F, Value *Actual,
1040 const std::string& NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001041 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001042 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001043 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001044 static CallInst *Create(Value *F, Value *Actual, const std::string& NameStr,
Gabor Greif051a9502008-04-06 20:25:17 +00001045 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001046 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001047 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001048 static CallInst *Create(Value *F, const std::string &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001049 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001050 return new(1) CallInst(F, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001051 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001052 static CallInst *Create(Value *F, const std::string &NameStr,
Evan Cheng34cd4a42008-05-05 18:30:58 +00001053 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001054 return new(1) CallInst(F, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001055 }
1056
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001057 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001058
Chris Lattnerf319e832004-10-15 23:52:05 +00001059 virtual CallInst *clone() const;
Gabor Greifefe65362008-05-10 08:32:32 +00001060
1061 /// Provide fast operand accessors
1062 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattnerbb5493d2007-02-15 23:15:00 +00001063
Chris Lattner3340ffe2005-05-06 20:26:26 +00001064 bool isTailCall() const { return SubclassData & 1; }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001065 void setTailCall(bool isTC = true) {
1066 SubclassData = (SubclassData & ~1) | unsigned(isTC);
Chris Lattner3340ffe2005-05-06 20:26:26 +00001067 }
1068
1069 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1070 /// function call.
1071 unsigned getCallingConv() const { return SubclassData >> 1; }
1072 void setCallingConv(unsigned CC) {
1073 SubclassData = (SubclassData & 1) | (CC << 1);
1074 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00001075
Chris Lattner041221c2008-03-13 04:33:03 +00001076 /// getParamAttrs - Return the parameter attributes for this call.
1077 ///
Chris Lattner58d74912008-03-12 17:45:29 +00001078 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001079
Chris Lattner041221c2008-03-13 04:33:03 +00001080 /// setParamAttrs - Sets the parameter attributes for this call.
Chris Lattner58d74912008-03-12 17:45:29 +00001081 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
Eric Christopher0bf7b412008-05-16 20:39:43 +00001082
1083 /// addParamAttr - adds the attribute to the list of attributes.
1084 void addParamAttr(unsigned i, ParameterAttributes attr);
Duncan Sandsdc024672007-11-27 13:23:08 +00001085
Duncan Sands2e033f32008-07-08 08:38:44 +00001086 /// removeParamAttr - removes the attribute from the list of attributes.
1087 void removeParamAttr(unsigned i, ParameterAttributes attr);
1088
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001089 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001090 bool paramHasAttr(unsigned i, unsigned attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001091
Dale Johannesen08e78b12008-02-22 17:49:45 +00001092 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001093 unsigned getParamAlignment(unsigned i) const {
1094 return ParamAttrs.getParamAlignment(i);
1095 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001096
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001097 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001098 bool doesNotAccessMemory() const {
1099 return paramHasAttr(0, ParamAttr::ReadNone);
1100 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001101 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1102 if (NotAccessMemory) addParamAttr(0, ParamAttr::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00001103 else removeParamAttr(0, ParamAttr::ReadNone);
1104 }
1105
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001106 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001107 bool onlyReadsMemory() const {
1108 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
1109 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001110 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1111 if (OnlyReadsMemory) addParamAttr(0, ParamAttr::ReadOnly);
Duncan Sands2e033f32008-07-08 08:38:44 +00001112 else removeParamAttr(0, ParamAttr::ReadOnly | ParamAttr::ReadNone);
1113 }
1114
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001115 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001116 bool doesNotReturn() const {
1117 return paramHasAttr(0, ParamAttr::NoReturn);
1118 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001119 void setDoesNotReturn(bool DoesNotReturn = true) {
1120 if (DoesNotReturn) addParamAttr(0, ParamAttr::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00001121 else removeParamAttr(0, ParamAttr::NoReturn);
1122 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001123
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001124 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001125 bool doesNotThrow() const {
1126 return paramHasAttr(0, ParamAttr::NoUnwind);
1127 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001128 void setDoesNotThrow(bool DoesNotThrow = true) {
1129 if (DoesNotThrow) addParamAttr(0, ParamAttr::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00001130 else removeParamAttr(0, ParamAttr::NoUnwind);
1131 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001132
Devang Patel41e23972008-03-03 21:46:28 +00001133 /// @brief Determine if the call returns a structure through first
1134 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001135 bool hasStructRetAttr() const {
1136 // Be friendly and also check the callee.
1137 return paramHasAttr(1, ParamAttr::StructRet);
1138 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001139
Evan Chengf4a54982008-01-12 18:57:32 +00001140 /// @brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001141 bool hasByValArgument() const {
1142 return ParamAttrs.hasAttrSomewhere(ParamAttr::ByVal);
1143 }
Evan Chengf4a54982008-01-12 18:57:32 +00001144
Chris Lattner721aef62004-11-18 17:46:57 +00001145 /// getCalledFunction - Return the function being called by this instruction
1146 /// if it is a direct call. If it is a call through a function pointer,
1147 /// return null.
1148 Function *getCalledFunction() const {
Dan Gohman11a7dbf2007-09-24 15:46:02 +00001149 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001150 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001151
Reid Spencerc25ec252006-12-29 04:10:59 +00001152 /// getCalledValue - Get a pointer to the function that is invoked by this
1153 /// instruction
Devang Patel4d4a5e02008-02-23 01:11:02 +00001154 const Value *getCalledValue() const { return getOperand(0); }
1155 Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001156
1157 // Methods for support type inquiry through isa, cast, and dyn_cast:
1158 static inline bool classof(const CallInst *) { return true; }
1159 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001160 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001161 }
1162 static inline bool classof(const Value *V) {
1163 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1164 }
1165};
1166
Gabor Greifefe65362008-05-10 08:32:32 +00001167template <>
1168struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1169};
1170
1171template<typename InputIterator>
1172CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001173 const std::string &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001174 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1175 ->getElementType())->getReturnType(),
1176 Instruction::Call,
1177 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001178 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001179 init(Func, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001180 typename std::iterator_traits<InputIterator>::iterator_category());
1181}
1182
1183template<typename InputIterator>
1184CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001185 const std::string &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00001186 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1187 ->getElementType())->getReturnType(),
1188 Instruction::Call,
1189 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001190 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001191 init(Func, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001192 typename std::iterator_traits<InputIterator>::iterator_category());
1193}
1194
1195DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1196
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001197//===----------------------------------------------------------------------===//
1198// SelectInst Class
1199//===----------------------------------------------------------------------===//
1200
1201/// SelectInst - This class represents the LLVM 'select' instruction.
1202///
1203class SelectInst : public Instruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001204 void init(Value *C, Value *S1, Value *S2) {
Gabor Greifefe65362008-05-10 08:32:32 +00001205 Op<0>() = C;
1206 Op<1>() = S1;
1207 Op<2>() = S2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001208 }
1209
Chris Lattner454928e2005-01-29 00:31:36 +00001210 SelectInst(const SelectInst &SI)
Gabor Greifefe65362008-05-10 08:32:32 +00001211 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1212 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
Chris Lattner454928e2005-01-29 00:31:36 +00001213 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001214 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001215 Instruction *InsertBefore)
1216 : Instruction(S1->getType(), Instruction::Select,
1217 &Op<0>(), 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001218 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001219 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001220 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001221 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001222 BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001223 : Instruction(S1->getType(), Instruction::Select,
1224 &Op<0>(), 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001225 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001226 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001227 }
Gabor Greif051a9502008-04-06 20:25:17 +00001228public:
Evan Chengd69bb1a2008-05-05 17:41:03 +00001229 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001230 const std::string &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001231 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001232 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001233 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001234 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001235 const std::string &NameStr,
1236 BasicBlock *InsertAtEnd) {
1237 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001238 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001239
Gabor Greifefe65362008-05-10 08:32:32 +00001240 Value *getCondition() const { return Op<0>(); }
1241 Value *getTrueValue() const { return Op<1>(); }
1242 Value *getFalseValue() const { return Op<2>(); }
Chris Lattner454928e2005-01-29 00:31:36 +00001243
1244 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001245 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001246
1247 OtherOps getOpcode() const {
1248 return static_cast<OtherOps>(Instruction::getOpcode());
1249 }
1250
Chris Lattnerf319e832004-10-15 23:52:05 +00001251 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001252
1253 // Methods for support type inquiry through isa, cast, and dyn_cast:
1254 static inline bool classof(const SelectInst *) { return true; }
1255 static inline bool classof(const Instruction *I) {
1256 return I->getOpcode() == Instruction::Select;
1257 }
1258 static inline bool classof(const Value *V) {
1259 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1260 }
1261};
1262
Gabor Greifefe65362008-05-10 08:32:32 +00001263template <>
1264struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1265};
1266
1267DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1268
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001269//===----------------------------------------------------------------------===//
1270// VAArgInst Class
1271//===----------------------------------------------------------------------===//
1272
1273/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001274/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001275///
Chris Lattner454928e2005-01-29 00:31:36 +00001276class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001277 VAArgInst(const VAArgInst &VAA)
Chris Lattner454928e2005-01-29 00:31:36 +00001278 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001279public:
Evan Cheng1bf9a182008-07-24 00:08:56 +00001280 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001281 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001282 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001283 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001284 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001285 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001286 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001287 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001288 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001289 }
1290
Chris Lattnerf319e832004-10-15 23:52:05 +00001291 virtual VAArgInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001292
1293 // Methods for support type inquiry through isa, cast, and dyn_cast:
1294 static inline bool classof(const VAArgInst *) { return true; }
1295 static inline bool classof(const Instruction *I) {
1296 return I->getOpcode() == VAArg;
1297 }
1298 static inline bool classof(const Value *V) {
1299 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1300 }
1301};
1302
1303//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001304// ExtractElementInst Class
1305//===----------------------------------------------------------------------===//
1306
1307/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001308/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001309///
1310class ExtractElementInst : public Instruction {
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001311 ExtractElementInst(const ExtractElementInst &EE) :
Gabor Greifefe65362008-05-10 08:32:32 +00001312 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
Gabor Greif6c80c382008-05-26 21:33:52 +00001313 Op<0>() = EE.Op<0>();
1314 Op<1>() = EE.Op<1>();
Robert Bocchino49b78a52006-01-10 19:04:13 +00001315 }
1316
1317public:
Gabor Greif051a9502008-04-06 20:25:17 +00001318 // allocate space for exactly two operands
1319 void *operator new(size_t s) {
Gabor Greifefe65362008-05-10 08:32:32 +00001320 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
Gabor Greif051a9502008-04-06 20:25:17 +00001321 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001322 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr = "",
Chris Lattner9fc18d22006-04-08 01:15:18 +00001323 Instruction *InsertBefore = 0);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001324 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr = "",
Chris Lattner06a248c22006-10-05 06:24:58 +00001325 Instruction *InsertBefore = 0);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001326 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr,
Chris Lattner9fc18d22006-04-08 01:15:18 +00001327 BasicBlock *InsertAtEnd);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001328 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr,
Chris Lattner06a248c22006-10-05 06:24:58 +00001329 BasicBlock *InsertAtEnd);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001330
Chris Lattnerfa495842006-04-08 04:04:54 +00001331 /// isValidOperands - Return true if an extractelement instruction can be
1332 /// formed with the specified operands.
1333 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001334
Robert Bocchino49b78a52006-01-10 19:04:13 +00001335 virtual ExtractElementInst *clone() const;
1336
Robert Bocchino49b78a52006-01-10 19:04:13 +00001337 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001338 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001339
1340 // Methods for support type inquiry through isa, cast, and dyn_cast:
1341 static inline bool classof(const ExtractElementInst *) { return true; }
1342 static inline bool classof(const Instruction *I) {
1343 return I->getOpcode() == Instruction::ExtractElement;
1344 }
1345 static inline bool classof(const Value *V) {
1346 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1347 }
1348};
1349
Gabor Greifefe65362008-05-10 08:32:32 +00001350template <>
1351struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1352};
1353
1354DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1355
Robert Bocchino49b78a52006-01-10 19:04:13 +00001356//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001357// InsertElementInst Class
1358//===----------------------------------------------------------------------===//
1359
1360/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001361/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001362///
1363class InsertElementInst : public Instruction {
Chris Lattner6a56ed42006-04-14 22:20:07 +00001364 InsertElementInst(const InsertElementInst &IE);
Gabor Greif051a9502008-04-06 20:25:17 +00001365 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001366 const std::string &NameStr = "",Instruction *InsertBefore = 0);
Gabor Greif051a9502008-04-06 20:25:17 +00001367 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001368 const std::string &NameStr = "",Instruction *InsertBefore = 0);
Gabor Greif051a9502008-04-06 20:25:17 +00001369 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001370 const std::string &NameStr, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001371 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001372 const std::string &NameStr, BasicBlock *InsertAtEnd);
Robert Bocchinof9993442006-01-17 20:05:59 +00001373public:
Gabor Greif051a9502008-04-06 20:25:17 +00001374 static InsertElementInst *Create(const InsertElementInst &IE) {
1375 return new(IE.getNumOperands()) InsertElementInst(IE);
1376 }
1377 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001378 const std::string &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +00001379 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001380 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001381 }
1382 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001383 const std::string &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001384 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001385 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001386 }
1387 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001388 const std::string &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001389 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001390 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001391 }
1392 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001393 const std::string &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001394 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001395 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001396 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001397
Chris Lattnerfa495842006-04-08 04:04:54 +00001398 /// isValidOperands - Return true if an insertelement instruction can be
1399 /// formed with the specified operands.
1400 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1401 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001402
Robert Bocchinof9993442006-01-17 20:05:59 +00001403 virtual InsertElementInst *clone() const;
1404
Reid Spencerac9dcb92007-02-15 03:39:18 +00001405 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001406 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001407 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001408 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001409 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001410
Robert Bocchinof9993442006-01-17 20:05:59 +00001411 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001412 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchinof9993442006-01-17 20:05:59 +00001413
1414 // Methods for support type inquiry through isa, cast, and dyn_cast:
1415 static inline bool classof(const InsertElementInst *) { return true; }
1416 static inline bool classof(const Instruction *I) {
1417 return I->getOpcode() == Instruction::InsertElement;
1418 }
1419 static inline bool classof(const Value *V) {
1420 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1421 }
1422};
1423
Gabor Greifefe65362008-05-10 08:32:32 +00001424template <>
1425struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1426};
1427
1428DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1429
Robert Bocchinof9993442006-01-17 20:05:59 +00001430//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001431// ShuffleVectorInst Class
1432//===----------------------------------------------------------------------===//
1433
1434/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1435/// input vectors.
1436///
1437class ShuffleVectorInst : public Instruction {
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001438 ShuffleVectorInst(const ShuffleVectorInst &IE);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001439public:
Gabor Greif051a9502008-04-06 20:25:17 +00001440 // allocate space for exactly three operands
1441 void *operator new(size_t s) {
1442 return User::operator new(s, 3);
1443 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001444 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001445 const std::string &NameStr = "",
1446 Instruction *InsertBefor = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001447 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001448 const std::string &NameStr, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001449
Chris Lattnerfa495842006-04-08 04:04:54 +00001450 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001451 /// formed with the specified operands.
1452 static bool isValidOperands(const Value *V1, const Value *V2,
1453 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001454
Chris Lattner9fc18d22006-04-08 01:15:18 +00001455 virtual ShuffleVectorInst *clone() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001456
Reid Spencerac9dcb92007-02-15 03:39:18 +00001457 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001458 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001459 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001460 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001461 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001462
Chris Lattner9fc18d22006-04-08 01:15:18 +00001463 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001464 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner8728f192008-03-02 05:28:33 +00001465
1466 /// getMaskValue - Return the index from the shuffle mask for the specified
1467 /// output result. This is either -1 if the element is undef or a number less
1468 /// than 2*numelements.
1469 int getMaskValue(unsigned i) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001470
Chris Lattner9fc18d22006-04-08 01:15:18 +00001471 // Methods for support type inquiry through isa, cast, and dyn_cast:
1472 static inline bool classof(const ShuffleVectorInst *) { return true; }
1473 static inline bool classof(const Instruction *I) {
1474 return I->getOpcode() == Instruction::ShuffleVector;
1475 }
1476 static inline bool classof(const Value *V) {
1477 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1478 }
1479};
1480
Gabor Greifefe65362008-05-10 08:32:32 +00001481template <>
1482struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1483};
1484
1485DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
Chris Lattner9fc18d22006-04-08 01:15:18 +00001486
1487//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001488// ExtractValueInst Class
1489//===----------------------------------------------------------------------===//
1490
Dan Gohmane2d896f2008-05-15 23:35:32 +00001491/// ExtractValueInst - This instruction extracts a struct member or array
1492/// element value from an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001493///
Gabor Greifd4f268b2008-06-06 20:28:12 +00001494class ExtractValueInst : public UnaryInstruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001495 SmallVector<unsigned, 4> Indices;
1496
Dan Gohman041e2eb2008-05-15 19:50:34 +00001497 ExtractValueInst(const ExtractValueInst &EVI);
Gabor Greif76aca6f2008-06-06 21:06:32 +00001498 void init(const unsigned *Idx, unsigned NumIdx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001499 const std::string &NameStr);
1500 void init(unsigned Idx, const std::string &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001501
1502 template<typename InputIterator>
Gabor Greif76aca6f2008-06-06 21:06:32 +00001503 void init(InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001504 const std::string &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001505 // This argument ensures that we have an iterator we can
1506 // do arithmetic on in constant time
1507 std::random_access_iterator_tag) {
1508 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1509
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001510 // There's no fundamental reason why we require at least one index
1511 // (other than weirdness with &*IdxBegin being invalid; see
1512 // getelementptr's init routine for example). But there's no
1513 // present need to support it.
1514 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1515
1516 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +00001517 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001518 // we have to build an array here
Dan Gohman041e2eb2008-05-15 19:50:34 +00001519 }
1520
1521 /// getIndexedType - Returns the type of the element that would be extracted
1522 /// with an extractvalue instruction with the specified parameters.
1523 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +00001524 /// Null is returned if the indices are invalid for the specified
Dan Gohman041e2eb2008-05-15 19:50:34 +00001525 /// pointer type.
1526 ///
1527 static const Type *getIndexedType(const Type *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001528 const unsigned *Idx, unsigned NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001529
1530 template<typename InputIterator>
1531 static const Type *getIndexedType(const Type *Ptr,
1532 InputIterator IdxBegin,
1533 InputIterator IdxEnd,
1534 // This argument ensures that we
1535 // have an iterator we can do
1536 // arithmetic on in constant time
1537 std::random_access_iterator_tag) {
1538 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1539
1540 if (NumIdx > 0)
1541 // This requires that the iterator points to contiguous memory.
Dan Gohman19a81632008-06-23 16:38:10 +00001542 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001543 else
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001544 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001545 }
1546
Dan Gohmane2d896f2008-05-15 23:35:32 +00001547 /// Constructors - Create a extractvalue instruction with a base aggregate
1548 /// value and a list of indices. The first ctor can optionally insert before
1549 /// an existing instruction, the second appends the new instruction to the
1550 /// specified BasicBlock.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001551 template<typename InputIterator>
1552 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1553 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001554 const std::string &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001555 Instruction *InsertBefore);
1556 template<typename InputIterator>
1557 inline ExtractValueInst(Value *Agg,
1558 InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001559 const std::string &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001560
Dan Gohman8e640412008-05-31 19:09:47 +00001561 // allocate space for exactly one operand
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001562 void *operator new(size_t s) {
1563 return User::operator new(s, 1);
1564 }
1565
Gabor Greifd4f268b2008-06-06 20:28:12 +00001566public:
Dan Gohman041e2eb2008-05-15 19:50:34 +00001567 template<typename InputIterator>
1568 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1569 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001570 const std::string &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001571 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001572 return new
Evan Cheng1bf9a182008-07-24 00:08:56 +00001573 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001574 }
1575 template<typename InputIterator>
1576 static ExtractValueInst *Create(Value *Agg,
1577 InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001578 const std::string &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001579 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001580 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001581 }
1582
1583 /// Constructors - These two creators are convenience methods because one
1584 /// index extractvalue instructions are much more common than those with
1585 /// more than one.
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001586 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001587 const std::string &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001588 Instruction *InsertBefore = 0) {
Dan Gohman2f27e172008-06-23 16:48:17 +00001589 unsigned Idxs[1] = { Idx };
Evan Cheng1bf9a182008-07-24 00:08:56 +00001590 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001591 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001592 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001593 const std::string &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001594 BasicBlock *InsertAtEnd) {
Dan Gohman2f27e172008-06-23 16:48:17 +00001595 unsigned Idxs[1] = { Idx };
Evan Cheng1bf9a182008-07-24 00:08:56 +00001596 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001597 }
1598
1599 virtual ExtractValueInst *clone() const;
1600
Dan Gohman041e2eb2008-05-15 19:50:34 +00001601 // getType - Overload to return most specific pointer type...
1602 const PointerType *getType() const {
1603 return reinterpret_cast<const PointerType*>(Instruction::getType());
1604 }
1605
1606 /// getIndexedType - Returns the type of the element that would be extracted
1607 /// with an extractvalue instruction with the specified parameters.
1608 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +00001609 /// Null is returned if the indices are invalid for the specified
Dan Gohman041e2eb2008-05-15 19:50:34 +00001610 /// pointer type.
1611 ///
1612 template<typename InputIterator>
1613 static const Type *getIndexedType(const Type *Ptr,
1614 InputIterator IdxBegin,
1615 InputIterator IdxEnd) {
1616 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1617 typename std::iterator_traits<InputIterator>::
1618 iterator_category());
1619 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001620 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001621
Owen Anderson5678d6e2008-06-19 17:15:57 +00001622 typedef const unsigned* idx_iterator;
1623 inline idx_iterator idx_begin() const { return Indices.begin(); }
1624 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001625
1626 Value *getAggregateOperand() {
1627 return getOperand(0);
1628 }
1629 const Value *getAggregateOperand() const {
1630 return getOperand(0);
1631 }
1632 static unsigned getAggregateOperandIndex() {
1633 return 0U; // get index for modifying correct operand
1634 }
1635
1636 unsigned getNumIndices() const { // Note: always non-negative
Bill Wendling67944fc2008-06-05 07:35:27 +00001637 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001638 }
1639
1640 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001641 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001642 }
1643
1644 // Methods for support type inquiry through isa, cast, and dyn_cast:
1645 static inline bool classof(const ExtractValueInst *) { return true; }
1646 static inline bool classof(const Instruction *I) {
1647 return I->getOpcode() == Instruction::ExtractValue;
1648 }
1649 static inline bool classof(const Value *V) {
1650 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1651 }
1652};
1653
Dan Gohmane4569942008-05-23 00:36:11 +00001654template<typename InputIterator>
1655ExtractValueInst::ExtractValueInst(Value *Agg,
1656 InputIterator IdxBegin,
1657 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001658 const std::string &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001659 Instruction *InsertBefore)
Gabor Greifd4f268b2008-06-06 20:28:12 +00001660 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
Bill Wendling85f40542008-07-22 07:14:12 +00001661 IdxBegin, IdxEnd)),
1662 ExtractValue, Agg, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001663 init(IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001664 typename std::iterator_traits<InputIterator>::iterator_category());
1665}
1666template<typename InputIterator>
1667ExtractValueInst::ExtractValueInst(Value *Agg,
1668 InputIterator IdxBegin,
1669 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001670 const std::string &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001671 BasicBlock *InsertAtEnd)
Gabor Greifd4f268b2008-06-06 20:28:12 +00001672 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
Bill Wendling85f40542008-07-22 07:14:12 +00001673 IdxBegin, IdxEnd)),
1674 ExtractValue, Agg, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001675 init(IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001676 typename std::iterator_traits<InputIterator>::iterator_category());
1677}
1678
Dan Gohmane4569942008-05-23 00:36:11 +00001679
Dan Gohman041e2eb2008-05-15 19:50:34 +00001680//===----------------------------------------------------------------------===//
1681// InsertValueInst Class
1682//===----------------------------------------------------------------------===//
1683
Dan Gohmane2d896f2008-05-15 23:35:32 +00001684/// InsertValueInst - This instruction inserts a struct field of array element
1685/// value into an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001686///
1687class InsertValueInst : public Instruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001688 SmallVector<unsigned, 4> Indices;
1689
1690 void *operator new(size_t, unsigned); // Do not implement
Dan Gohman041e2eb2008-05-15 19:50:34 +00001691 InsertValueInst(const InsertValueInst &IVI);
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +00001692 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001693 const std::string &NameStr);
1694 void init(Value *Agg, Value *Val, unsigned Idx, const std::string &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001695
1696 template<typename InputIterator>
1697 void init(Value *Agg, Value *Val,
1698 InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001699 const std::string &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001700 // This argument ensures that we have an iterator we can
1701 // do arithmetic on in constant time
1702 std::random_access_iterator_tag) {
1703 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1704
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001705 // There's no fundamental reason why we require at least one index
1706 // (other than weirdness with &*IdxBegin being invalid; see
1707 // getelementptr's init routine for example). But there's no
1708 // present need to support it.
1709 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1710
1711 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +00001712 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001713 // we have to build an array here
Dan Gohman041e2eb2008-05-15 19:50:34 +00001714 }
1715
Dan Gohmane2d896f2008-05-15 23:35:32 +00001716 /// Constructors - Create a insertvalue instruction with a base aggregate
1717 /// value, a value to insert, and a list of indices. The first ctor can
1718 /// optionally insert before an existing instruction, the second appends
1719 /// the new instruction to the specified BasicBlock.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001720 template<typename InputIterator>
1721 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001722 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001723 const std::string &NameStr,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001724 Instruction *InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001725 template<typename InputIterator>
1726 inline InsertValueInst(Value *Agg, Value *Val,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001727 InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001728 const std::string &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001729
1730 /// Constructors - These two constructors are convenience methods because one
1731 /// and two index insertvalue instructions are so common.
1732 InsertValueInst(Value *Agg, Value *Val,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001733 unsigned Idx, const std::string &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001734 Instruction *InsertBefore = 0);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001735 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001736 const std::string &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001737public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001738 // allocate space for exactly two operands
1739 void *operator new(size_t s) {
1740 return User::operator new(s, 2);
1741 }
1742
Dan Gohman041e2eb2008-05-15 19:50:34 +00001743 template<typename InputIterator>
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +00001744 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001745 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001746 const std::string &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001747 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001748 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001749 NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001750 }
1751 template<typename InputIterator>
1752 static InsertValueInst *Create(Value *Agg, Value *Val,
1753 InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001754 const std::string &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001755 BasicBlock *InsertAtEnd) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001756 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001757 NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001758 }
1759
1760 /// Constructors - These two creators are convenience methods because one
1761 /// index insertvalue instructions are much more common than those with
1762 /// more than one.
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001763 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001764 const std::string &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001765 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001766 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001767 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001768 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001769 const std::string &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001770 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001771 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001772 }
1773
1774 virtual InsertValueInst *clone() const;
1775
1776 /// Transparently provide more efficient getOperand methods.
1777 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1778
1779 // getType - Overload to return most specific pointer type...
1780 const PointerType *getType() const {
1781 return reinterpret_cast<const PointerType*>(Instruction::getType());
1782 }
1783
Owen Anderson5678d6e2008-06-19 17:15:57 +00001784 typedef const unsigned* idx_iterator;
1785 inline idx_iterator idx_begin() const { return Indices.begin(); }
1786 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001787
1788 Value *getAggregateOperand() {
1789 return getOperand(0);
1790 }
1791 const Value *getAggregateOperand() const {
1792 return getOperand(0);
1793 }
1794 static unsigned getAggregateOperandIndex() {
1795 return 0U; // get index for modifying correct operand
1796 }
1797
1798 Value *getInsertedValueOperand() {
1799 return getOperand(1);
1800 }
1801 const Value *getInsertedValueOperand() const {
1802 return getOperand(1);
1803 }
1804 static unsigned getInsertedValueOperandIndex() {
1805 return 1U; // get index for modifying correct operand
1806 }
1807
1808 unsigned getNumIndices() const { // Note: always non-negative
Bill Wendling67944fc2008-06-05 07:35:27 +00001809 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001810 }
1811
1812 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001813 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001814 }
1815
1816 // Methods for support type inquiry through isa, cast, and dyn_cast:
1817 static inline bool classof(const InsertValueInst *) { return true; }
1818 static inline bool classof(const Instruction *I) {
1819 return I->getOpcode() == Instruction::InsertValue;
1820 }
1821 static inline bool classof(const Value *V) {
1822 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1823 }
1824};
1825
1826template <>
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001827struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001828};
1829
Dan Gohmane4569942008-05-23 00:36:11 +00001830template<typename InputIterator>
1831InsertValueInst::InsertValueInst(Value *Agg,
1832 Value *Val,
1833 InputIterator IdxBegin,
1834 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001835 const std::string &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001836 Instruction *InsertBefore)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001837 : Instruction(Agg->getType(), InsertValue,
1838 OperandTraits<InsertValueInst>::op_begin(this),
1839 2, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001840 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001841 typename std::iterator_traits<InputIterator>::iterator_category());
1842}
1843template<typename InputIterator>
1844InsertValueInst::InsertValueInst(Value *Agg,
1845 Value *Val,
1846 InputIterator IdxBegin,
1847 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001848 const std::string &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001849 BasicBlock *InsertAtEnd)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001850 : Instruction(Agg->getType(), InsertValue,
1851 OperandTraits<InsertValueInst>::op_begin(this),
1852 2, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001853 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001854 typename std::iterator_traits<InputIterator>::iterator_category());
1855}
1856
Dan Gohman041e2eb2008-05-15 19:50:34 +00001857DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1858
1859//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001860// PHINode Class
1861//===----------------------------------------------------------------------===//
1862
1863// PHINode - The PHINode class is used to represent the magical mystical PHI
1864// node, that can not exist in nature, but can be synthesized in a computer
1865// scientist's overactive imagination.
1866//
1867class PHINode : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001868 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00001869 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1870 /// the number actually in use.
1871 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001872 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00001873 // allocate space for exactly zero operands
1874 void *operator new(size_t s) {
1875 return User::operator new(s, 0);
1876 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001877 explicit PHINode(const Type *Ty, const std::string &NameStr = "",
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001878 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001879 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Chris Lattner454928e2005-01-29 00:31:36 +00001880 ReservedSpace(0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001881 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001882 }
1883
Evan Cheng1bf9a182008-07-24 00:08:56 +00001884 PHINode(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001885 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Chris Lattner454928e2005-01-29 00:31:36 +00001886 ReservedSpace(0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001887 setName(NameStr);
Chris Lattner454928e2005-01-29 00:31:36 +00001888 }
Gabor Greif051a9502008-04-06 20:25:17 +00001889public:
Evan Cheng1bf9a182008-07-24 00:08:56 +00001890 static PHINode *Create(const Type *Ty, const std::string &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001891 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001892 return new PHINode(Ty, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001893 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001894 static PHINode *Create(const Type *Ty, const std::string &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001895 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001896 return new PHINode(Ty, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001897 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001898 ~PHINode();
1899
Chris Lattner454928e2005-01-29 00:31:36 +00001900 /// reserveOperandSpace - This method can be used to avoid repeated
1901 /// reallocation of PHI operand lists by reserving space for the correct
1902 /// number of operands before adding them. Unlike normal vector reserves,
1903 /// this method can also be used to trim the operand space.
1904 void reserveOperandSpace(unsigned NumValues) {
1905 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001906 }
1907
Chris Lattnerf319e832004-10-15 23:52:05 +00001908 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001909
Gabor Greifefe65362008-05-10 08:32:32 +00001910 /// Provide fast operand accessors
1911 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1912
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001913 /// getNumIncomingValues - Return the number of incoming edges
1914 ///
Chris Lattner454928e2005-01-29 00:31:36 +00001915 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001916
Reid Spencerc773de62006-05-19 19:07:54 +00001917 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001918 ///
1919 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001920 assert(i*2 < getNumOperands() && "Invalid value number!");
1921 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001922 }
1923 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +00001924 assert(i*2 < getNumOperands() && "Invalid value number!");
1925 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001926 }
Chris Lattner454928e2005-01-29 00:31:36 +00001927 unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001928 return i*2;
1929 }
1930
Reid Spencerc773de62006-05-19 19:07:54 +00001931 /// getIncomingBlock - Return incoming basic block number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001932 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00001933 BasicBlock *getIncomingBlock(unsigned i) const {
Gabor Greifefe65362008-05-10 08:32:32 +00001934 return static_cast<BasicBlock*>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001935 }
1936 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Gabor Greifefe65362008-05-10 08:32:32 +00001937 setOperand(i*2+1, BB);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001938 }
1939 unsigned getOperandNumForIncomingBlock(unsigned i) {
1940 return i*2+1;
1941 }
1942
1943 /// addIncoming - Add an incoming value to the end of the PHI list
1944 ///
1945 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00001946 assert(V && "PHI node got a null value!");
1947 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001948 assert(getType() == V->getType() &&
1949 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +00001950 unsigned OpNo = NumOperands;
1951 if (OpNo+2 > ReservedSpace)
1952 resizeOperands(0); // Get more space!
1953 // Initialize some new operands.
1954 NumOperands = OpNo+2;
Gabor Greif6c80c382008-05-26 21:33:52 +00001955 OperandList[OpNo] = V;
1956 OperandList[OpNo+1] = BB;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001957 }
Misha Brukman9769ab22005-04-21 20:19:05 +00001958
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001959 /// removeIncomingValue - Remove an incoming value. This is useful if a
1960 /// predecessor basic block is deleted. The value removed is returned.
1961 ///
1962 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1963 /// is true), the PHI node is destroyed and any uses of it are replaced with
1964 /// dummy values. The only time there should be zero incoming values to a PHI
1965 /// node is when the block is dead, so this strategy is sound.
1966 ///
1967 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1968
Gabor Greifefe65362008-05-10 08:32:32 +00001969 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001970 int Idx = getBasicBlockIndex(BB);
1971 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1972 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1973 }
1974
Misha Brukman9769ab22005-04-21 20:19:05 +00001975 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001976 /// block in the value list for this PHI. Returns -1 if no instance.
1977 ///
1978 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001979 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00001980 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Gabor Greifefe65362008-05-10 08:32:32 +00001981 if (OL[i+1].get() == BB) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001982 return -1;
1983 }
1984
1985 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1986 return getIncomingValue(getBasicBlockIndex(BB));
1987 }
1988
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001989 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00001990 /// same value, return the value, otherwise return null.
1991 ///
Chris Lattner9acbd612005-08-05 00:49:06 +00001992 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001993
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001994 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1995 static inline bool classof(const PHINode *) { return true; }
1996 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001997 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001998 }
1999 static inline bool classof(const Value *V) {
2000 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2001 }
Chris Lattner454928e2005-01-29 00:31:36 +00002002 private:
2003 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002004};
2005
Gabor Greifefe65362008-05-10 08:32:32 +00002006template <>
2007struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
2008};
2009
2010DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2011
2012
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002013//===----------------------------------------------------------------------===//
2014// ReturnInst Class
2015//===----------------------------------------------------------------------===//
2016
2017//===---------------------------------------------------------------------------
2018/// ReturnInst - Return a value (possibly void), from a function. Execution
2019/// does not continue in this function any longer.
2020///
2021class ReturnInst : public TerminatorInst {
Chris Lattner910c80a2007-02-24 00:55:48 +00002022 ReturnInst(const ReturnInst &RI);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002023
Gabor Greif051a9502008-04-06 20:25:17 +00002024private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002025 // ReturnInst constructors:
2026 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002027 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002028 // ReturnInst(Value* X) - 'ret X' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00002029 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002030 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
Gabor Greifefe65362008-05-10 08:32:32 +00002031 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2032 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002033 //
2034 // NOTE: If the Value* passed is of type void then the constructor behaves as
2035 // if it was passed NULL.
Chris Lattner910c80a2007-02-24 00:55:48 +00002036 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
2037 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
2038 explicit ReturnInst(BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002039public:
2040 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
2041 return new(!!retVal) ReturnInst(retVal, InsertBefore);
2042 }
2043 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
2044 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
2045 }
Gabor Greif051a9502008-04-06 20:25:17 +00002046 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
2047 return new(0) ReturnInst(InsertAtEnd);
2048 }
Devang Patel57ef4f42008-02-23 00:35:18 +00002049 virtual ~ReturnInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002050
Chris Lattnerf319e832004-10-15 23:52:05 +00002051 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002052
Gabor Greifefe65362008-05-10 08:32:32 +00002053 /// Provide fast operand accessors
2054 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Devang Patel64d4e612008-02-26 17:56:20 +00002055
Gabor Greifefe65362008-05-10 08:32:32 +00002056 /// Convenience accessor
Devang Patel1eafa062008-03-11 17:35:03 +00002057 Value *getReturnValue(unsigned n = 0) const {
Gabor Greifefe65362008-05-10 08:32:32 +00002058 return n < getNumOperands()
2059 ? getOperand(n)
2060 : 0;
Devang Patel1eafa062008-03-11 17:35:03 +00002061 }
2062
Chris Lattner454928e2005-01-29 00:31:36 +00002063 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002064
2065 // Methods for support type inquiry through isa, cast, and dyn_cast:
2066 static inline bool classof(const ReturnInst *) { return true; }
2067 static inline bool classof(const Instruction *I) {
2068 return (I->getOpcode() == Instruction::Ret);
2069 }
2070 static inline bool classof(const Value *V) {
2071 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2072 }
Chris Lattner454928e2005-01-29 00:31:36 +00002073 private:
2074 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2075 virtual unsigned getNumSuccessorsV() const;
2076 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002077};
2078
Gabor Greifefe65362008-05-10 08:32:32 +00002079template <>
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002080struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
Gabor Greifefe65362008-05-10 08:32:32 +00002081};
2082
2083DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002084
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002085//===----------------------------------------------------------------------===//
2086// BranchInst Class
2087//===----------------------------------------------------------------------===//
2088
2089//===---------------------------------------------------------------------------
2090/// BranchInst - Conditional or Unconditional Branch instruction.
2091///
2092class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00002093 /// Ops list - Branches are strange. The operands are ordered:
2094 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
2095 /// they don't have to check for cond/uncond branchness.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002096 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00002097 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002098 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2099 // BranchInst(BB *B) - 'br B'
2100 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2101 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2102 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2103 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2104 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00002105 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002106 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002107 Instruction *InsertBefore = 0);
2108 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002109 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002110 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002111public:
2112 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2113 return new(1) BranchInst(IfTrue, InsertBefore);
2114 }
Gabor Greifefe65362008-05-10 08:32:32 +00002115 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2116 Value *Cond, Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002117 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2118 }
2119 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2120 return new(1) BranchInst(IfTrue, InsertAtEnd);
2121 }
Gabor Greifefe65362008-05-10 08:32:32 +00002122 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2123 Value *Cond, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002124 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2125 }
Chris Lattner454928e2005-01-29 00:31:36 +00002126
Bill Wendlingd2a5a2a2008-05-10 10:58:07 +00002127 ~BranchInst() {
Gabor Greifefe65362008-05-10 08:32:32 +00002128 if (NumOperands == 1)
Bill Wendlingc2e73532008-05-10 19:59:59 +00002129 NumOperands = (unsigned)((Use*)this - OperandList);
Gabor Greifefe65362008-05-10 08:32:32 +00002130 }
2131
Chris Lattner454928e2005-01-29 00:31:36 +00002132 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00002133 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002134
Chris Lattnerf319e832004-10-15 23:52:05 +00002135 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002136
Devang Patel4d4a5e02008-02-23 01:11:02 +00002137 bool isUnconditional() const { return getNumOperands() == 1; }
2138 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002139
Devang Patel4d4a5e02008-02-23 01:11:02 +00002140 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002141 assert(isConditional() && "Cannot get condition of an uncond branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00002142 return getOperand(2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002143 }
2144
2145 void setCondition(Value *V) {
2146 assert(isConditional() && "Cannot set condition of unconditional branch!");
2147 setOperand(2, V);
2148 }
2149
2150 // setUnconditionalDest - Change the current branch to an unconditional branch
2151 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +00002152 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002153 void setUnconditionalDest(BasicBlock *Dest) {
Gabor Greifefe65362008-05-10 08:32:32 +00002154 Op<0>() = Dest;
Chris Lattner454928e2005-01-29 00:31:36 +00002155 if (isConditional()) { // Convert this to an uncond branch.
Gabor Greifefe65362008-05-10 08:32:32 +00002156 Op<1>().set(0);
2157 Op<2>().set(0);
Chris Lattner454928e2005-01-29 00:31:36 +00002158 NumOperands = 1;
Chris Lattner454928e2005-01-29 00:31:36 +00002159 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002160 }
2161
Chris Lattner454928e2005-01-29 00:31:36 +00002162 unsigned getNumSuccessors() const { return 1+isConditional(); }
2163
2164 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002165 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Dan Gohmanb96039e2007-05-11 20:59:29 +00002166 return cast<BasicBlock>(getOperand(i));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002167 }
2168
Chris Lattner454928e2005-01-29 00:31:36 +00002169 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002170 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifefe65362008-05-10 08:32:32 +00002171 setOperand(idx, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002172 }
2173
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002174 // Methods for support type inquiry through isa, cast, and dyn_cast:
2175 static inline bool classof(const BranchInst *) { return true; }
2176 static inline bool classof(const Instruction *I) {
2177 return (I->getOpcode() == Instruction::Br);
2178 }
2179 static inline bool classof(const Value *V) {
2180 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2181 }
Chris Lattner454928e2005-01-29 00:31:36 +00002182private:
2183 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2184 virtual unsigned getNumSuccessorsV() const;
2185 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002186};
2187
Gabor Greifefe65362008-05-10 08:32:32 +00002188template <>
2189struct OperandTraits<BranchInst> : HungoffOperandTraits<> {
2190 // we need to access operands via OperandList, since
2191 // the NumOperands may change from 3 to 1
2192 static inline void *allocate(unsigned); // FIXME
2193};
2194
2195DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2196
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002197//===----------------------------------------------------------------------===//
2198// SwitchInst Class
2199//===----------------------------------------------------------------------===//
2200
2201//===---------------------------------------------------------------------------
2202/// SwitchInst - Multiway switch
2203///
2204class SwitchInst : public TerminatorInst {
Gabor Greifefe65362008-05-10 08:32:32 +00002205 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00002206 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002207 // Operand[0] = Value to switch on
2208 // Operand[1] = Default basic block destination
2209 // Operand[2n ] = Value to match
2210 // Operand[2n+1] = BasicBlock to go to on match
2211 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00002212 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2213 void resizeOperands(unsigned No);
Gabor Greifefe65362008-05-10 08:32:32 +00002214 // allocate space for exactly zero operands
2215 void *operator new(size_t s) {
2216 return User::operator new(s, 0);
2217 }
Chris Lattner454928e2005-01-29 00:31:36 +00002218 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2219 /// switch on and a default destination. The number of additional cases can
2220 /// be specified here to make memory allocation more efficient. This
2221 /// constructor can also autoinsert before another instruction.
2222 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002223 Instruction *InsertBefore = 0);
2224
Chris Lattner454928e2005-01-29 00:31:36 +00002225 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2226 /// switch on and a default destination. The number of additional cases can
2227 /// be specified here to make memory allocation more efficient. This
2228 /// constructor also autoinserts at the end of the specified BasicBlock.
2229 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002230 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002231public:
Gabor Greifefe65362008-05-10 08:32:32 +00002232 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2233 unsigned NumCases, Instruction *InsertBefore = 0) {
2234 return new SwitchInst(Value, Default, NumCases, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002235 }
Gabor Greifefe65362008-05-10 08:32:32 +00002236 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2237 unsigned NumCases, BasicBlock *InsertAtEnd) {
2238 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002239 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002240 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00002241
Gabor Greifefe65362008-05-10 08:32:32 +00002242 /// Provide fast operand accessors
2243 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2244
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002245 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00002246 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00002247 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00002248
Devang Patel4d4a5e02008-02-23 01:11:02 +00002249 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002250 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002251 }
2252
2253 /// getNumCases - return the number of 'cases' in this switch instruction.
2254 /// Note that case #0 is always the default case.
2255 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002256 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002257 }
2258
2259 /// getCaseValue - Return the specified case value. Note that case #0, the
2260 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002261 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002262 assert(i && i < getNumCases() && "Illegal case value to get!");
2263 return getSuccessorValue(i);
2264 }
2265
2266 /// getCaseValue - Return the specified case value. Note that case #0, the
2267 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002268 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002269 assert(i && i < getNumCases() && "Illegal case value to get!");
2270 return getSuccessorValue(i);
2271 }
2272
2273 /// findCaseValue - Search all of the case values for the specified constant.
2274 /// If it is explicitly handled, return the case number of it, otherwise
2275 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002276 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002277 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2278 if (getCaseValue(i) == C)
2279 return i;
2280 return 0;
2281 }
2282
Nick Lewycky011f1842006-09-18 19:03:59 +00002283 /// findCaseDest - Finds the unique case value for a given successor. Returns
2284 /// null if the successor is not found, not unique, or is the default case.
2285 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00002286 if (BB == getDefaultDest()) return NULL;
2287
Nick Lewycky011f1842006-09-18 19:03:59 +00002288 ConstantInt *CI = NULL;
2289 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2290 if (getSuccessor(i) == BB) {
2291 if (CI) return NULL; // Multiple cases lead to BB.
2292 else CI = getCaseValue(i);
2293 }
2294 }
2295 return CI;
2296 }
2297
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002298 /// addCase - Add an entry to the switch instruction...
2299 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00002300 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002301
2302 /// removeCase - This method removes the specified successor from the switch
2303 /// instruction. Note that this cannot be used to remove the default
2304 /// destination (successor #0).
2305 ///
2306 void removeCase(unsigned idx);
2307
Chris Lattner454928e2005-01-29 00:31:36 +00002308 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002309
Chris Lattner454928e2005-01-29 00:31:36 +00002310 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2311 BasicBlock *getSuccessor(unsigned idx) const {
2312 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2313 return cast<BasicBlock>(getOperand(idx*2+1));
2314 }
2315 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002316 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Gabor Greifefe65362008-05-10 08:32:32 +00002317 setOperand(idx*2+1, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002318 }
2319
2320 // getSuccessorValue - Return the value associated with the specified
2321 // successor.
Devang Patel4d4a5e02008-02-23 01:11:02 +00002322 ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002323 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00002324 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002325 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002326
2327 // Methods for support type inquiry through isa, cast, and dyn_cast:
2328 static inline bool classof(const SwitchInst *) { return true; }
2329 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00002330 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002331 }
2332 static inline bool classof(const Value *V) {
2333 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2334 }
Chris Lattner454928e2005-01-29 00:31:36 +00002335private:
2336 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2337 virtual unsigned getNumSuccessorsV() const;
2338 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002339};
2340
Gabor Greifefe65362008-05-10 08:32:32 +00002341template <>
2342struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2343};
2344
2345DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2346
2347
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002348//===----------------------------------------------------------------------===//
2349// InvokeInst Class
2350//===----------------------------------------------------------------------===//
2351
Chris Lattner3340ffe2005-05-06 20:26:26 +00002352/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2353/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002354///
2355class InvokeInst : public TerminatorInst {
Chris Lattner58d74912008-03-12 17:45:29 +00002356 PAListPtr ParamAttrs;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002357 InvokeInst(const InvokeInst &BI);
2358 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerd2dd1502007-02-13 01:04:01 +00002359 Value* const *Args, unsigned NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00002360
2361 template<typename InputIterator>
2362 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2363 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002364 const std::string &NameStr,
David Greenef1355a52007-08-27 19:04:21 +00002365 // This argument ensures that we have an iterator we can
2366 // do arithmetic on in constant time
2367 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002368 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greenef1355a52007-08-27 19:04:21 +00002369
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002370 // This requires that the iterator points to contiguous memory.
2371 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
Evan Cheng1bf9a182008-07-24 00:08:56 +00002372 setName(NameStr);
David Greenef1355a52007-08-27 19:04:21 +00002373 }
2374
David Greenef1355a52007-08-27 19:04:21 +00002375 /// Construct an InvokeInst given a range of arguments.
2376 /// InputIterator must be a random-access iterator pointing to
2377 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2378 /// made for random-accessness but not for contiguous storage as
2379 /// that would incur runtime overhead.
2380 ///
2381 /// @brief Construct an InvokeInst from a range of arguments
2382 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002383 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2384 InputIterator ArgBegin, InputIterator ArgEnd,
2385 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002386 const std::string &NameStr, Instruction *InsertBefore);
David Greenef1355a52007-08-27 19:04:21 +00002387
2388 /// Construct an InvokeInst given a range of arguments.
2389 /// InputIterator must be a random-access iterator pointing to
2390 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2391 /// made for random-accessness but not for contiguous storage as
2392 /// that would incur runtime overhead.
2393 ///
2394 /// @brief Construct an InvokeInst from a range of arguments
2395 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002396 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2397 InputIterator ArgBegin, InputIterator ArgEnd,
2398 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002399 const std::string &NameStr, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002400public:
2401 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002402 static InvokeInst *Create(Value *Func,
2403 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002404 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002405 const std::string &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00002406 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +00002407 unsigned Values(ArgEnd - ArgBegin + 3);
2408 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002409 Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002410 }
2411 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002412 static InvokeInst *Create(Value *Func,
2413 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002414 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002415 const std::string &NameStr,
2416 BasicBlock *InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00002417 unsigned Values(ArgEnd - ArgBegin + 3);
2418 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002419 Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002420 }
David Greenef1355a52007-08-27 19:04:21 +00002421
Chris Lattnerf319e832004-10-15 23:52:05 +00002422 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002423
Gabor Greifefe65362008-05-10 08:32:32 +00002424 /// Provide fast operand accessors
2425 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2426
Chris Lattner3340ffe2005-05-06 20:26:26 +00002427 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2428 /// function call.
2429 unsigned getCallingConv() const { return SubclassData; }
2430 void setCallingConv(unsigned CC) {
2431 SubclassData = CC;
2432 }
2433
Chris Lattner041221c2008-03-13 04:33:03 +00002434 /// getParamAttrs - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002435 ///
2436 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002437
Chris Lattner041221c2008-03-13 04:33:03 +00002438 /// setParamAttrs - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002439 ///
2440 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00002441
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002442 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002443 bool paramHasAttr(unsigned i, ParameterAttributes attr) const;
Eric Christopher0bf7b412008-05-16 20:39:43 +00002444
2445 /// addParamAttr - adds the attribute to the list of attributes.
2446 void addParamAttr(unsigned i, ParameterAttributes attr);
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002447
Duncan Sands2e033f32008-07-08 08:38:44 +00002448 /// removeParamAttr - removes the attribute from the list of attributes.
2449 void removeParamAttr(unsigned i, ParameterAttributes attr);
2450
Dale Johannesen08e78b12008-02-22 17:49:45 +00002451 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002452 unsigned getParamAlignment(unsigned i) const {
2453 return ParamAttrs.getParamAlignment(i);
2454 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00002455
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002456 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002457 bool doesNotAccessMemory() const {
2458 return paramHasAttr(0, ParamAttr::ReadNone);
2459 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002460 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2461 if (NotAccessMemory) addParamAttr(0, ParamAttr::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00002462 else removeParamAttr(0, ParamAttr::ReadNone);
2463 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002464
2465 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002466 bool onlyReadsMemory() const {
2467 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
2468 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002469 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2470 if (OnlyReadsMemory) addParamAttr(0, ParamAttr::ReadOnly);
Duncan Sands2e033f32008-07-08 08:38:44 +00002471 else removeParamAttr(0, ParamAttr::ReadOnly | ParamAttr::ReadNone);
2472 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002473
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002474 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002475 bool doesNotReturn() const {
2476 return paramHasAttr(0, ParamAttr::NoReturn);
2477 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002478 void setDoesNotReturn(bool DoesNotReturn = true) {
2479 if (DoesNotReturn) addParamAttr(0, ParamAttr::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00002480 else removeParamAttr(0, ParamAttr::NoReturn);
2481 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002482
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002483 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002484 bool doesNotThrow() const {
2485 return paramHasAttr(0, ParamAttr::NoUnwind);
2486 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002487 void setDoesNotThrow(bool DoesNotThrow = true) {
2488 if (DoesNotThrow) addParamAttr(0, ParamAttr::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00002489 else removeParamAttr(0, ParamAttr::NoUnwind);
2490 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002491
Devang Patel41e23972008-03-03 21:46:28 +00002492 /// @brief Determine if the call returns a structure through first
2493 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002494 bool hasStructRetAttr() const {
2495 // Be friendly and also check the callee.
2496 return paramHasAttr(1, ParamAttr::StructRet);
2497 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002498
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002499 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00002500 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002501 ///
Chris Lattner721aef62004-11-18 17:46:57 +00002502 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002503 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00002504 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002505
2506 // getCalledValue - Get a pointer to a function that is invoked by this inst.
Devang Patel4d4a5e02008-02-23 01:11:02 +00002507 Value *getCalledValue() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002508
2509 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00002510 BasicBlock *getNormalDest() const {
2511 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002512 }
Chris Lattner454928e2005-01-29 00:31:36 +00002513 BasicBlock *getUnwindDest() const {
2514 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002515 }
Chris Lattner454928e2005-01-29 00:31:36 +00002516 void setNormalDest(BasicBlock *B) {
Gabor Greifefe65362008-05-10 08:32:32 +00002517 setOperand(1, B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002518 }
2519
Chris Lattner454928e2005-01-29 00:31:36 +00002520 void setUnwindDest(BasicBlock *B) {
Gabor Greifefe65362008-05-10 08:32:32 +00002521 setOperand(2, B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002522 }
2523
Devang Patel4d4a5e02008-02-23 01:11:02 +00002524 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002525 assert(i < 2 && "Successor # out of range for invoke!");
2526 return i == 0 ? getNormalDest() : getUnwindDest();
2527 }
2528
Chris Lattner454928e2005-01-29 00:31:36 +00002529 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002530 assert(idx < 2 && "Successor # out of range for invoke!");
Gabor Greifefe65362008-05-10 08:32:32 +00002531 setOperand(idx+1, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002532 }
2533
Chris Lattner454928e2005-01-29 00:31:36 +00002534 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002535
2536 // Methods for support type inquiry through isa, cast, and dyn_cast:
2537 static inline bool classof(const InvokeInst *) { return true; }
2538 static inline bool classof(const Instruction *I) {
2539 return (I->getOpcode() == Instruction::Invoke);
2540 }
2541 static inline bool classof(const Value *V) {
2542 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2543 }
Chris Lattner454928e2005-01-29 00:31:36 +00002544private:
2545 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2546 virtual unsigned getNumSuccessorsV() const;
2547 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002548};
2549
Gabor Greifefe65362008-05-10 08:32:32 +00002550template <>
2551struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2552};
2553
2554template<typename InputIterator>
2555InvokeInst::InvokeInst(Value *Func,
2556 BasicBlock *IfNormal, BasicBlock *IfException,
2557 InputIterator ArgBegin, InputIterator ArgEnd,
2558 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002559 const std::string &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00002560 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2561 ->getElementType())->getReturnType(),
2562 Instruction::Invoke,
2563 OperandTraits<InvokeInst>::op_end(this) - Values,
2564 Values, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002565 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00002566 typename std::iterator_traits<InputIterator>::iterator_category());
2567}
2568template<typename InputIterator>
2569InvokeInst::InvokeInst(Value *Func,
2570 BasicBlock *IfNormal, BasicBlock *IfException,
2571 InputIterator ArgBegin, InputIterator ArgEnd,
2572 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002573 const std::string &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00002574 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2575 ->getElementType())->getReturnType(),
2576 Instruction::Invoke,
2577 OperandTraits<InvokeInst>::op_end(this) - Values,
2578 Values, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002579 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00002580 typename std::iterator_traits<InputIterator>::iterator_category());
2581}
2582
2583DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002584
2585//===----------------------------------------------------------------------===//
2586// UnwindInst Class
2587//===----------------------------------------------------------------------===//
2588
2589//===---------------------------------------------------------------------------
2590/// UnwindInst - Immediately exit the current function, unwinding the stack
2591/// until an invoke instruction is found.
2592///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002593class UnwindInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002594 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002595public:
Gabor Greif051a9502008-04-06 20:25:17 +00002596 // allocate space for exactly zero operands
2597 void *operator new(size_t s) {
2598 return User::operator new(s, 0);
2599 }
Chris Lattner910c80a2007-02-24 00:55:48 +00002600 explicit UnwindInst(Instruction *InsertBefore = 0);
2601 explicit UnwindInst(BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002602
Chris Lattnerf319e832004-10-15 23:52:05 +00002603 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002604
Chris Lattner454928e2005-01-29 00:31:36 +00002605 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002606
2607 // Methods for support type inquiry through isa, cast, and dyn_cast:
2608 static inline bool classof(const UnwindInst *) { return true; }
2609 static inline bool classof(const Instruction *I) {
2610 return I->getOpcode() == Instruction::Unwind;
2611 }
2612 static inline bool classof(const Value *V) {
2613 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2614 }
Chris Lattner454928e2005-01-29 00:31:36 +00002615private:
2616 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2617 virtual unsigned getNumSuccessorsV() const;
2618 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002619};
2620
Chris Lattner076b3f12004-10-16 18:05:54 +00002621//===----------------------------------------------------------------------===//
2622// UnreachableInst Class
2623//===----------------------------------------------------------------------===//
2624
2625//===---------------------------------------------------------------------------
2626/// UnreachableInst - This function has undefined behavior. In particular, the
2627/// presence of this instruction indicates some higher level knowledge that the
2628/// end of the block cannot be reached.
2629///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002630class UnreachableInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002631 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002632public:
Gabor Greif051a9502008-04-06 20:25:17 +00002633 // allocate space for exactly zero operands
2634 void *operator new(size_t s) {
2635 return User::operator new(s, 0);
2636 }
Chris Lattner910c80a2007-02-24 00:55:48 +00002637 explicit UnreachableInst(Instruction *InsertBefore = 0);
2638 explicit UnreachableInst(BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00002639
2640 virtual UnreachableInst *clone() const;
2641
Chris Lattner454928e2005-01-29 00:31:36 +00002642 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00002643
2644 // Methods for support type inquiry through isa, cast, and dyn_cast:
2645 static inline bool classof(const UnreachableInst *) { return true; }
2646 static inline bool classof(const Instruction *I) {
2647 return I->getOpcode() == Instruction::Unreachable;
2648 }
2649 static inline bool classof(const Value *V) {
2650 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2651 }
Chris Lattner454928e2005-01-29 00:31:36 +00002652private:
2653 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2654 virtual unsigned getNumSuccessorsV() const;
2655 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00002656};
2657
Reid Spencer3da59db2006-11-27 01:05:10 +00002658//===----------------------------------------------------------------------===//
2659// TruncInst Class
2660//===----------------------------------------------------------------------===//
2661
2662/// @brief This class represents a truncation of integer types.
2663class TruncInst : public CastInst {
2664 /// Private copy constructor
2665 TruncInst(const TruncInst &CI)
2666 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2667 }
2668public:
2669 /// @brief Constructor with insert-before-instruction semantics
2670 TruncInst(
2671 Value *S, ///< The value to be truncated
2672 const Type *Ty, ///< The (smaller) type to truncate to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002673 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002674 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2675 );
2676
2677 /// @brief Constructor with insert-at-end-of-block semantics
2678 TruncInst(
2679 Value *S, ///< The value to be truncated
2680 const Type *Ty, ///< The (smaller) type to truncate to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002681 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002682 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2683 );
2684
2685 /// @brief Clone an identical TruncInst
2686 virtual CastInst *clone() const;
2687
2688 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2689 static inline bool classof(const TruncInst *) { return true; }
2690 static inline bool classof(const Instruction *I) {
2691 return I->getOpcode() == Trunc;
2692 }
2693 static inline bool classof(const Value *V) {
2694 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2695 }
2696};
2697
2698//===----------------------------------------------------------------------===//
2699// ZExtInst Class
2700//===----------------------------------------------------------------------===//
2701
2702/// @brief This class represents zero extension of integer types.
2703class ZExtInst : public CastInst {
2704 /// @brief Private copy constructor
2705 ZExtInst(const ZExtInst &CI)
2706 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2707 }
2708public:
2709 /// @brief Constructor with insert-before-instruction semantics
2710 ZExtInst(
2711 Value *S, ///< The value to be zero extended
2712 const Type *Ty, ///< The type to zero extend to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002713 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002714 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2715 );
2716
2717 /// @brief Constructor with insert-at-end semantics.
2718 ZExtInst(
2719 Value *S, ///< The value to be zero extended
2720 const Type *Ty, ///< The type to zero extend to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002721 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002722 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2723 );
2724
2725 /// @brief Clone an identical ZExtInst
2726 virtual CastInst *clone() const;
2727
2728 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2729 static inline bool classof(const ZExtInst *) { return true; }
2730 static inline bool classof(const Instruction *I) {
2731 return I->getOpcode() == ZExt;
2732 }
2733 static inline bool classof(const Value *V) {
2734 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2735 }
2736};
2737
2738//===----------------------------------------------------------------------===//
2739// SExtInst Class
2740//===----------------------------------------------------------------------===//
2741
2742/// @brief This class represents a sign extension of integer types.
2743class SExtInst : public CastInst {
2744 /// @brief Private copy constructor
2745 SExtInst(const SExtInst &CI)
2746 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2747 }
2748public:
2749 /// @brief Constructor with insert-before-instruction semantics
2750 SExtInst(
2751 Value *S, ///< The value to be sign extended
2752 const Type *Ty, ///< The type to sign extend to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002753 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002754 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2755 );
2756
2757 /// @brief Constructor with insert-at-end-of-block semantics
2758 SExtInst(
2759 Value *S, ///< The value to be sign extended
2760 const Type *Ty, ///< The type to sign extend to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002761 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002762 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2763 );
2764
2765 /// @brief Clone an identical SExtInst
2766 virtual CastInst *clone() const;
2767
2768 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2769 static inline bool classof(const SExtInst *) { return true; }
2770 static inline bool classof(const Instruction *I) {
2771 return I->getOpcode() == SExt;
2772 }
2773 static inline bool classof(const Value *V) {
2774 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2775 }
2776};
2777
2778//===----------------------------------------------------------------------===//
2779// FPTruncInst Class
2780//===----------------------------------------------------------------------===//
2781
2782/// @brief This class represents a truncation of floating point types.
2783class FPTruncInst : public CastInst {
2784 FPTruncInst(const FPTruncInst &CI)
2785 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2786 }
2787public:
2788 /// @brief Constructor with insert-before-instruction semantics
2789 FPTruncInst(
2790 Value *S, ///< The value to be truncated
2791 const Type *Ty, ///< The type to truncate to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002792 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002793 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2794 );
2795
2796 /// @brief Constructor with insert-before-instruction semantics
2797 FPTruncInst(
2798 Value *S, ///< The value to be truncated
2799 const Type *Ty, ///< The type to truncate to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002800 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002801 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2802 );
2803
2804 /// @brief Clone an identical FPTruncInst
2805 virtual CastInst *clone() const;
2806
2807 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2808 static inline bool classof(const FPTruncInst *) { return true; }
2809 static inline bool classof(const Instruction *I) {
2810 return I->getOpcode() == FPTrunc;
2811 }
2812 static inline bool classof(const Value *V) {
2813 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2814 }
2815};
2816
2817//===----------------------------------------------------------------------===//
2818// FPExtInst Class
2819//===----------------------------------------------------------------------===//
2820
2821/// @brief This class represents an extension of floating point types.
2822class FPExtInst : public CastInst {
2823 FPExtInst(const FPExtInst &CI)
2824 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2825 }
2826public:
2827 /// @brief Constructor with insert-before-instruction semantics
2828 FPExtInst(
2829 Value *S, ///< The value to be extended
2830 const Type *Ty, ///< The type to extend to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002831 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002832 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2833 );
2834
2835 /// @brief Constructor with insert-at-end-of-block semantics
2836 FPExtInst(
2837 Value *S, ///< The value to be extended
2838 const Type *Ty, ///< The type to extend to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002839 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002840 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2841 );
2842
2843 /// @brief Clone an identical FPExtInst
2844 virtual CastInst *clone() const;
2845
2846 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2847 static inline bool classof(const FPExtInst *) { return true; }
2848 static inline bool classof(const Instruction *I) {
2849 return I->getOpcode() == FPExt;
2850 }
2851 static inline bool classof(const Value *V) {
2852 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2853 }
2854};
2855
2856//===----------------------------------------------------------------------===//
2857// UIToFPInst Class
2858//===----------------------------------------------------------------------===//
2859
2860/// @brief This class represents a cast unsigned integer to floating point.
2861class UIToFPInst : public CastInst {
2862 UIToFPInst(const UIToFPInst &CI)
2863 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2864 }
2865public:
2866 /// @brief Constructor with insert-before-instruction semantics
2867 UIToFPInst(
2868 Value *S, ///< The value to be converted
2869 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002870 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002871 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2872 );
2873
2874 /// @brief Constructor with insert-at-end-of-block semantics
2875 UIToFPInst(
2876 Value *S, ///< The value to be converted
2877 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002878 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002879 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2880 );
2881
2882 /// @brief Clone an identical UIToFPInst
2883 virtual CastInst *clone() const;
2884
2885 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2886 static inline bool classof(const UIToFPInst *) { return true; }
2887 static inline bool classof(const Instruction *I) {
2888 return I->getOpcode() == UIToFP;
2889 }
2890 static inline bool classof(const Value *V) {
2891 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2892 }
2893};
2894
2895//===----------------------------------------------------------------------===//
2896// SIToFPInst Class
2897//===----------------------------------------------------------------------===//
2898
2899/// @brief This class represents a cast from signed integer to floating point.
2900class SIToFPInst : public CastInst {
2901 SIToFPInst(const SIToFPInst &CI)
2902 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2903 }
2904public:
2905 /// @brief Constructor with insert-before-instruction semantics
2906 SIToFPInst(
2907 Value *S, ///< The value to be converted
2908 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002909 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002910 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2911 );
2912
2913 /// @brief Constructor with insert-at-end-of-block semantics
2914 SIToFPInst(
2915 Value *S, ///< The value to be converted
2916 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002917 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002918 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2919 );
2920
2921 /// @brief Clone an identical SIToFPInst
2922 virtual CastInst *clone() const;
2923
2924 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2925 static inline bool classof(const SIToFPInst *) { return true; }
2926 static inline bool classof(const Instruction *I) {
2927 return I->getOpcode() == SIToFP;
2928 }
2929 static inline bool classof(const Value *V) {
2930 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2931 }
2932};
2933
2934//===----------------------------------------------------------------------===//
2935// FPToUIInst Class
2936//===----------------------------------------------------------------------===//
2937
2938/// @brief This class represents a cast from floating point to unsigned integer
2939class FPToUIInst : public CastInst {
2940 FPToUIInst(const FPToUIInst &CI)
2941 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2942 }
2943public:
2944 /// @brief Constructor with insert-before-instruction semantics
2945 FPToUIInst(
2946 Value *S, ///< The value to be converted
2947 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002948 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002949 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2950 );
2951
2952 /// @brief Constructor with insert-at-end-of-block semantics
2953 FPToUIInst(
2954 Value *S, ///< The value to be converted
2955 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002956 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002957 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2958 );
2959
2960 /// @brief Clone an identical FPToUIInst
2961 virtual CastInst *clone() const;
2962
2963 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2964 static inline bool classof(const FPToUIInst *) { return true; }
2965 static inline bool classof(const Instruction *I) {
2966 return I->getOpcode() == FPToUI;
2967 }
2968 static inline bool classof(const Value *V) {
2969 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2970 }
2971};
2972
2973//===----------------------------------------------------------------------===//
2974// FPToSIInst Class
2975//===----------------------------------------------------------------------===//
2976
2977/// @brief This class represents a cast from floating point to signed integer.
2978class FPToSIInst : public CastInst {
2979 FPToSIInst(const FPToSIInst &CI)
2980 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2981 }
2982public:
2983 /// @brief Constructor with insert-before-instruction semantics
2984 FPToSIInst(
2985 Value *S, ///< The value to be converted
2986 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002987 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002988 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2989 );
2990
2991 /// @brief Constructor with insert-at-end-of-block semantics
2992 FPToSIInst(
2993 Value *S, ///< The value to be converted
2994 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002995 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002996 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2997 );
2998
2999 /// @brief Clone an identical FPToSIInst
3000 virtual CastInst *clone() const;
3001
3002 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3003 static inline bool classof(const FPToSIInst *) { return true; }
3004 static inline bool classof(const Instruction *I) {
3005 return I->getOpcode() == FPToSI;
3006 }
3007 static inline bool classof(const Value *V) {
3008 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3009 }
3010};
3011
3012//===----------------------------------------------------------------------===//
3013// IntToPtrInst Class
3014//===----------------------------------------------------------------------===//
3015
3016/// @brief This class represents a cast from an integer to a pointer.
3017class IntToPtrInst : public CastInst {
3018 IntToPtrInst(const IntToPtrInst &CI)
3019 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
3020 }
3021public:
3022 /// @brief Constructor with insert-before-instruction semantics
3023 IntToPtrInst(
3024 Value *S, ///< The value to be converted
3025 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00003026 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003027 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3028 );
3029
3030 /// @brief Constructor with insert-at-end-of-block semantics
3031 IntToPtrInst(
3032 Value *S, ///< The value to be converted
3033 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00003034 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003035 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3036 );
3037
3038 /// @brief Clone an identical IntToPtrInst
3039 virtual CastInst *clone() const;
3040
3041 // Methods for support type inquiry through isa, cast, and dyn_cast:
3042 static inline bool classof(const IntToPtrInst *) { return true; }
3043 static inline bool classof(const Instruction *I) {
3044 return I->getOpcode() == IntToPtr;
3045 }
3046 static inline bool classof(const Value *V) {
3047 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3048 }
3049};
3050
3051//===----------------------------------------------------------------------===//
3052// PtrToIntInst Class
3053//===----------------------------------------------------------------------===//
3054
3055/// @brief This class represents a cast from a pointer to an integer
3056class PtrToIntInst : public CastInst {
3057 PtrToIntInst(const PtrToIntInst &CI)
3058 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3059 }
3060public:
3061 /// @brief Constructor with insert-before-instruction semantics
3062 PtrToIntInst(
3063 Value *S, ///< The value to be converted
3064 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00003065 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003066 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3067 );
3068
3069 /// @brief Constructor with insert-at-end-of-block semantics
3070 PtrToIntInst(
3071 Value *S, ///< The value to be converted
3072 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00003073 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003074 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3075 );
3076
3077 /// @brief Clone an identical PtrToIntInst
3078 virtual CastInst *clone() const;
3079
3080 // Methods for support type inquiry through isa, cast, and dyn_cast:
3081 static inline bool classof(const PtrToIntInst *) { return true; }
3082 static inline bool classof(const Instruction *I) {
3083 return I->getOpcode() == PtrToInt;
3084 }
3085 static inline bool classof(const Value *V) {
3086 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3087 }
3088};
3089
3090//===----------------------------------------------------------------------===//
3091// BitCastInst Class
3092//===----------------------------------------------------------------------===//
3093
3094/// @brief This class represents a no-op cast from one type to another.
3095class BitCastInst : public CastInst {
3096 BitCastInst(const BitCastInst &CI)
3097 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3098 }
3099public:
3100 /// @brief Constructor with insert-before-instruction semantics
3101 BitCastInst(
3102 Value *S, ///< The value to be casted
3103 const Type *Ty, ///< The type to casted to
Evan Cheng1bf9a182008-07-24 00:08:56 +00003104 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003105 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3106 );
3107
3108 /// @brief Constructor with insert-at-end-of-block semantics
3109 BitCastInst(
3110 Value *S, ///< The value to be casted
3111 const Type *Ty, ///< The type to casted to
Evan Cheng1bf9a182008-07-24 00:08:56 +00003112 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003113 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3114 );
3115
3116 /// @brief Clone an identical BitCastInst
3117 virtual CastInst *clone() const;
3118
3119 // Methods for support type inquiry through isa, cast, and dyn_cast:
3120 static inline bool classof(const BitCastInst *) { return true; }
3121 static inline bool classof(const Instruction *I) {
3122 return I->getOpcode() == BitCast;
3123 }
3124 static inline bool classof(const Value *V) {
3125 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3126 }
3127};
3128
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003129} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00003130
3131#endif