blob: 237c1a80981db49a303f8e52958ed525857f7563 [file] [log] [blame]
Chris Lattnera892a3a2003-01-27 22:08:52 +00001//===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
Misha Brukman9769ab22005-04-21 20:19:05 +00002//
John Criswell6fbcc262003-10-20 20:19:47 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner7ed47a12007-12-29 19:59:42 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman9769ab22005-04-21 20:19:05 +00007//
John Criswell6fbcc262003-10-20 20:19:47 +00008//===----------------------------------------------------------------------===//
Chris Lattnera892a3a2003-01-27 22:08:52 +00009//
10// This file exposes the class definitions of all of the subclasses of the
11// Instruction class. This is meant to be an easy way to get access to all
12// instruction subclasses.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_INSTRUCTIONS_H
17#define LLVM_INSTRUCTIONS_H
18
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000019#include "llvm/InstrTypes.h"
David Greene52eec542007-08-01 03:43:44 +000020#include "llvm/DerivedTypes.h"
Devang Pateleaf42ab2008-09-23 23:03:40 +000021#include "llvm/Attributes.h"
Gabor Greifefe65362008-05-10 08:32:32 +000022#include "llvm/BasicBlock.h"
Dan Gohman81a0c0b2008-05-31 00:58:22 +000023#include "llvm/ADT/SmallVector.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000024#include <iterator>
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000025
26namespace llvm {
27
Chris Lattnerd1a32602005-02-24 05:32:09 +000028class ConstantInt;
Reid Spencer3da43842007-02-28 22:00:54 +000029class ConstantRange;
30class APInt;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000031
32//===----------------------------------------------------------------------===//
33// AllocationInst Class
34//===----------------------------------------------------------------------===//
35
36/// AllocationInst - This class is the common base class of MallocInst and
37/// AllocaInst.
38///
Chris Lattner454928e2005-01-29 00:31:36 +000039class AllocationInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000040protected:
Nate Begeman14b05292005-11-05 09:21:28 +000041 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
Misha Brukman91102862005-03-16 03:46:55 +000042 const std::string &Name = "", Instruction *InsertBefore = 0);
Nate Begeman14b05292005-11-05 09:21:28 +000043 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
Misha Brukman91102862005-03-16 03:46:55 +000044 const std::string &Name, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000045public:
Gabor Greif051a9502008-04-06 20:25:17 +000046 // Out of line virtual method, so the vtable, etc. has a home.
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000047 virtual ~AllocationInst();
48
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000049 /// isArrayAllocation - Return true if there is an allocation size parameter
50 /// to the allocation instruction that is not 1.
51 ///
52 bool isArrayAllocation() const;
53
54 /// getArraySize - Get the number of element allocated, for a simple
55 /// allocation of a single element, this will return a constant 1 value.
56 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000057 const Value *getArraySize() const { return getOperand(0); }
58 Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000059
60 /// getType - Overload to return most specific pointer type
61 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000062 const PointerType *getType() const {
Misha Brukman9769ab22005-04-21 20:19:05 +000063 return reinterpret_cast<const PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000064 }
65
66 /// getAllocatedType - Return the type that is being allocated by the
67 /// instruction.
68 ///
69 const Type *getAllocatedType() const;
70
Nate Begeman14b05292005-11-05 09:21:28 +000071 /// getAlignment - Return the alignment of the memory that is being allocated
72 /// by the instruction.
73 ///
Dan Gohman52837072008-03-24 16:55:58 +000074 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
75 void setAlignment(unsigned Align);
Chris Lattnerf56a8db2006-10-03 17:09:12 +000076
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000077 virtual Instruction *clone() const = 0;
78
79 // Methods for support type inquiry through isa, cast, and dyn_cast:
80 static inline bool classof(const AllocationInst *) { return true; }
81 static inline bool classof(const Instruction *I) {
82 return I->getOpcode() == Instruction::Alloca ||
83 I->getOpcode() == Instruction::Malloc;
84 }
85 static inline bool classof(const Value *V) {
86 return isa<Instruction>(V) && classof(cast<Instruction>(V));
87 }
88};
89
90
91//===----------------------------------------------------------------------===//
92// MallocInst Class
93//===----------------------------------------------------------------------===//
94
95/// MallocInst - an instruction to allocated memory on the heap
96///
97class MallocInst : public AllocationInst {
98 MallocInst(const MallocInst &MI);
99public:
100 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000101 const std::string &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000102 Instruction *InsertBefore = 0)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000103 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertBefore) {}
104 MallocInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000105 BasicBlock *InsertAtEnd)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000106 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000107
Evan Cheng1bf9a182008-07-24 00:08:56 +0000108 MallocInst(const Type *Ty, const std::string &NameStr,
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000109 Instruction *InsertBefore = 0)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000110 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
111 MallocInst(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
112 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000113
114 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000115 const std::string &NameStr, BasicBlock *InsertAtEnd)
116 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertAtEnd) {}
Chris Lattnerb77780e2006-05-10 04:38:35 +0000117 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000118 const std::string &NameStr = "",
Nate Begeman14b05292005-11-05 09:21:28 +0000119 Instruction *InsertBefore = 0)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000120 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertBefore) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000121
Chris Lattnerf319e832004-10-15 23:52:05 +0000122 virtual MallocInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000123
124 // Methods for support type inquiry through isa, cast, and dyn_cast:
125 static inline bool classof(const MallocInst *) { return true; }
126 static inline bool classof(const Instruction *I) {
127 return (I->getOpcode() == Instruction::Malloc);
128 }
129 static inline bool classof(const Value *V) {
130 return isa<Instruction>(V) && classof(cast<Instruction>(V));
131 }
132};
133
134
135//===----------------------------------------------------------------------===//
136// AllocaInst Class
137//===----------------------------------------------------------------------===//
138
139/// AllocaInst - an instruction to allocate memory on the stack
140///
141class AllocaInst : public AllocationInst {
142 AllocaInst(const AllocaInst &);
143public:
144 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000145 const std::string &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000146 Instruction *InsertBefore = 0)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000147 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertBefore) {}
148 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000149 BasicBlock *InsertAtEnd)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000150 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
Chris Lattnerb77780e2006-05-10 04:38:35 +0000151
Evan Cheng1bf9a182008-07-24 00:08:56 +0000152 AllocaInst(const Type *Ty, const std::string &NameStr,
Chris Lattnerb77780e2006-05-10 04:38:35 +0000153 Instruction *InsertBefore = 0)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000154 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
155 AllocaInst(const Type *Ty, const std::string &NameStr,
156 BasicBlock *InsertAtEnd)
157 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000158
Chris Lattnerb77780e2006-05-10 04:38:35 +0000159 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000160 const std::string &NameStr = "", Instruction *InsertBefore = 0)
161 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertBefore) {}
Nate Begeman14b05292005-11-05 09:21:28 +0000162 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000163 const std::string &NameStr, BasicBlock *InsertAtEnd)
164 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000165
Chris Lattnerf319e832004-10-15 23:52:05 +0000166 virtual AllocaInst *clone() const;
Chris Lattnerc5dd22a2008-11-26 02:54:17 +0000167
168 /// isStaticAlloca - Return true if this alloca is in the entry block of the
169 /// function and is a constant size. If so, the code generator will fold it
170 /// into the prolog/epilog code, so it is basically free.
171 bool isStaticAlloca() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000172
173 // Methods for support type inquiry through isa, cast, and dyn_cast:
174 static inline bool classof(const AllocaInst *) { return true; }
175 static inline bool classof(const Instruction *I) {
176 return (I->getOpcode() == Instruction::Alloca);
177 }
178 static inline bool classof(const Value *V) {
179 return isa<Instruction>(V) && classof(cast<Instruction>(V));
180 }
181};
182
183
184//===----------------------------------------------------------------------===//
185// FreeInst Class
186//===----------------------------------------------------------------------===//
187
188/// FreeInst - an instruction to deallocate memory
189///
Chris Lattner454928e2005-01-29 00:31:36 +0000190class FreeInst : public UnaryInstruction {
191 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000192public:
193 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
194 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
195
Chris Lattnerf319e832004-10-15 23:52:05 +0000196 virtual FreeInst *clone() const;
Owen Andersonc9edf0b2007-07-06 23:13:31 +0000197
198 // Accessor methods for consistency with other memory operations
199 Value *getPointerOperand() { return getOperand(0); }
200 const Value *getPointerOperand() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000201
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000202 // Methods for support type inquiry through isa, cast, and dyn_cast:
203 static inline bool classof(const FreeInst *) { return true; }
204 static inline bool classof(const Instruction *I) {
205 return (I->getOpcode() == Instruction::Free);
206 }
207 static inline bool classof(const Value *V) {
208 return isa<Instruction>(V) && classof(cast<Instruction>(V));
209 }
210};
211
212
213//===----------------------------------------------------------------------===//
214// LoadInst Class
215//===----------------------------------------------------------------------===//
216
Chris Lattner88fe29a2005-02-05 01:44:18 +0000217/// LoadInst - an instruction for reading from memory. This uses the
218/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000219///
Chris Lattner454928e2005-01-29 00:31:36 +0000220class LoadInst : public UnaryInstruction {
Christopher Lamb43c7f372007-04-22 19:24:39 +0000221
Chris Lattner454928e2005-01-29 00:31:36 +0000222 LoadInst(const LoadInst &LI)
Chris Lattner88fe29a2005-02-05 01:44:18 +0000223 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
224 setVolatile(LI.isVolatile());
Christopher Lamb43c7f372007-04-22 19:24:39 +0000225 setAlignment(LI.getAlignment());
Misha Brukman9769ab22005-04-21 20:19:05 +0000226
Chris Lattner454928e2005-01-29 00:31:36 +0000227#ifndef NDEBUG
228 AssertOK();
229#endif
230 }
231 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000232public:
Evan Cheng1bf9a182008-07-24 00:08:56 +0000233 LoadInst(Value *Ptr, const std::string &NameStr, Instruction *InsertBefore);
234 LoadInst(Value *Ptr, const std::string &NameStr, BasicBlock *InsertAtEnd);
235 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile = false,
Christopher Lamb43c7f372007-04-22 19:24:39 +0000236 Instruction *InsertBefore = 0);
Evan Cheng1bf9a182008-07-24 00:08:56 +0000237 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
238 unsigned Align, Instruction *InsertBefore = 0);
239 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000240 BasicBlock *InsertAtEnd);
Evan Cheng1bf9a182008-07-24 00:08:56 +0000241 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
242 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000243
Evan Cheng1bf9a182008-07-24 00:08:56 +0000244 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
245 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
246 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
247 bool isVolatile = false, Instruction *InsertBefore = 0);
248 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
Chris Lattnerf00042a2007-02-13 07:54:42 +0000249 BasicBlock *InsertAtEnd);
250
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000251 /// isVolatile - Return true if this is a load from a volatile memory
252 /// location.
253 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000254 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000255
256 /// setVolatile - Specify whether this is a volatile load or not.
257 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000258 void setVolatile(bool V) {
Hartmut Kaiserefd4a512007-10-17 14:56:40 +0000259 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000260 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000261
Chris Lattnerf319e832004-10-15 23:52:05 +0000262 virtual LoadInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000263
Christopher Lamb43c7f372007-04-22 19:24:39 +0000264 /// getAlignment - Return the alignment of the access that is being performed
265 ///
266 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000267 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000268 }
269
270 void setAlignment(unsigned Align);
271
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000272 Value *getPointerOperand() { return getOperand(0); }
273 const Value *getPointerOperand() const { return getOperand(0); }
274 static unsigned getPointerOperandIndex() { return 0U; }
275
276 // Methods for support type inquiry through isa, cast, and dyn_cast:
277 static inline bool classof(const LoadInst *) { return true; }
278 static inline bool classof(const Instruction *I) {
279 return I->getOpcode() == Instruction::Load;
280 }
281 static inline bool classof(const Value *V) {
282 return isa<Instruction>(V) && classof(cast<Instruction>(V));
283 }
284};
285
286
287//===----------------------------------------------------------------------===//
288// StoreInst Class
289//===----------------------------------------------------------------------===//
290
Misha Brukman9769ab22005-04-21 20:19:05 +0000291/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000292///
293class StoreInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +0000294 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Christopher Lamb43c7f372007-04-22 19:24:39 +0000295
Gabor Greifefe65362008-05-10 08:32:32 +0000296 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
297 &Op<0>(), 2) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000298 Op<0>() = SI.Op<0>();
299 Op<1>() = SI.Op<1>();
Chris Lattner88fe29a2005-02-05 01:44:18 +0000300 setVolatile(SI.isVolatile());
Christopher Lamb43c7f372007-04-22 19:24:39 +0000301 setAlignment(SI.getAlignment());
302
Chris Lattner454928e2005-01-29 00:31:36 +0000303#ifndef NDEBUG
304 AssertOK();
305#endif
306 }
307 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000308public:
Gabor Greif051a9502008-04-06 20:25:17 +0000309 // allocate space for exactly two operands
310 void *operator new(size_t s) {
311 return User::operator new(s, 2);
312 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000313 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
314 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
315 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
316 Instruction *InsertBefore = 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000317 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
318 unsigned Align, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000319 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000320 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
321 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000322
323
324 /// isVolatile - Return true if this is a load from a volatile memory
325 /// location.
326 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000327 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000328
329 /// setVolatile - Specify whether this is a volatile load or not.
330 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000331 void setVolatile(bool V) {
Hartmut Kaiserefd4a512007-10-17 14:56:40 +0000332 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000333 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000334
Chris Lattner454928e2005-01-29 00:31:36 +0000335 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +0000336 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner454928e2005-01-29 00:31:36 +0000337
Christopher Lamb43c7f372007-04-22 19:24:39 +0000338 /// getAlignment - Return the alignment of the access that is being performed
339 ///
340 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000341 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000342 }
343
344 void setAlignment(unsigned Align);
345
Chris Lattnerf319e832004-10-15 23:52:05 +0000346 virtual StoreInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000347
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000348 Value *getPointerOperand() { return getOperand(1); }
349 const Value *getPointerOperand() const { return getOperand(1); }
350 static unsigned getPointerOperandIndex() { return 1U; }
351
352 // Methods for support type inquiry through isa, cast, and dyn_cast:
353 static inline bool classof(const StoreInst *) { return true; }
354 static inline bool classof(const Instruction *I) {
355 return I->getOpcode() == Instruction::Store;
356 }
357 static inline bool classof(const Value *V) {
358 return isa<Instruction>(V) && classof(cast<Instruction>(V));
359 }
360};
361
Gabor Greifefe65362008-05-10 08:32:32 +0000362template <>
363struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
364};
365
366DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000367
368//===----------------------------------------------------------------------===//
369// GetElementPtrInst Class
370//===----------------------------------------------------------------------===//
371
David Greeneb8f74792007-09-04 15:46:09 +0000372// checkType - Simple wrapper function to give a better assertion failure
373// message on bad indexes for a gep instruction.
374//
375static inline const Type *checkType(const Type *Ty) {
376 assert(Ty && "Invalid GetElementPtrInst indices for type!");
377 return Ty;
378}
379
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000380/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
381/// access elements of arrays and structs
382///
383class GetElementPtrInst : public Instruction {
Gabor Greifefe65362008-05-10 08:32:32 +0000384 GetElementPtrInst(const GetElementPtrInst &GEPI);
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +0000385 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000386 const std::string &NameStr);
387 void init(Value *Ptr, Value *Idx, const std::string &NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000388
389 template<typename InputIterator>
390 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000391 const std::string &NameStr,
David Greeneb8f74792007-09-04 15:46:09 +0000392 // This argument ensures that we have an iterator we can
393 // do arithmetic on in constant time
394 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000395 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
David Greeneb8f74792007-09-04 15:46:09 +0000396
397 if (NumIdx > 0) {
Gabor Greifefe65362008-05-10 08:32:32 +0000398 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +0000399 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Gabor Greifefe65362008-05-10 08:32:32 +0000400 // we have to build an array here
David Greeneb8f74792007-09-04 15:46:09 +0000401 }
402 else {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000403 init(Ptr, 0, NumIdx, NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000404 }
David Greeneb8f74792007-09-04 15:46:09 +0000405 }
406
407 /// getIndexedType - Returns the type of the element that would be loaded with
408 /// a load instruction with the specified parameters.
409 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000410 /// Null is returned if the indices are invalid for the specified
David Greeneb8f74792007-09-04 15:46:09 +0000411 /// pointer type.
412 ///
David Greeneb8f74792007-09-04 15:46:09 +0000413 template<typename InputIterator>
414 static const Type *getIndexedType(const Type *Ptr,
415 InputIterator IdxBegin,
416 InputIterator IdxEnd,
David Greeneb8f74792007-09-04 15:46:09 +0000417 // This argument ensures that we
418 // have an iterator we can do
419 // arithmetic on in constant time
420 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000421 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
David Greeneb8f74792007-09-04 15:46:09 +0000422
Dan Gohman041e2eb2008-05-15 19:50:34 +0000423 if (NumIdx > 0)
David Greeneb8f74792007-09-04 15:46:09 +0000424 // This requires that the iterator points to contiguous memory.
David Greene2d5a0b92008-10-29 00:30:54 +0000425 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +0000426 else
427 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
David Greeneb8f74792007-09-04 15:46:09 +0000428 }
429
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000430 /// Constructors - Create a getelementptr instruction with a base pointer an
431 /// list of indices. The first ctor can optionally insert before an existing
432 /// instruction, the second appends the new instruction to the specified
433 /// BasicBlock.
David Greeneb8f74792007-09-04 15:46:09 +0000434 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000435 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
436 InputIterator IdxEnd,
437 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000438 const std::string &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000439 Instruction *InsertBefore);
David Greeneb8f74792007-09-04 15:46:09 +0000440 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000441 inline GetElementPtrInst(Value *Ptr,
442 InputIterator IdxBegin, InputIterator IdxEnd,
443 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000444 const std::string &NameStr, BasicBlock *InsertAtEnd);
David Greeneb8f74792007-09-04 15:46:09 +0000445
Chris Lattner38bacf22005-05-03 05:43:30 +0000446 /// Constructors - These two constructors are convenience methods because one
447 /// and two index getelementptr instructions are so common.
Evan Cheng1bf9a182008-07-24 00:08:56 +0000448 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +0000449 Instruction *InsertBefore = 0);
Chris Lattner38bacf22005-05-03 05:43:30 +0000450 GetElementPtrInst(Value *Ptr, Value *Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000451 const std::string &NameStr, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000452public:
453 template<typename InputIterator>
454 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
455 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000456 const std::string &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000457 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +0000458 typename std::iterator_traits<InputIterator>::difference_type Values =
459 1 + std::distance(IdxBegin, IdxEnd);
460 return new(Values)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000461 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000462 }
463 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000464 static GetElementPtrInst *Create(Value *Ptr,
465 InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000466 const std::string &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000467 BasicBlock *InsertAtEnd) {
468 typename std::iterator_traits<InputIterator>::difference_type Values =
469 1 + std::distance(IdxBegin, IdxEnd);
470 return new(Values)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000471 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000472 }
473
Gabor Greifefe65362008-05-10 08:32:32 +0000474 /// Constructors - These two creators are convenience methods because one
475 /// index getelementptr instructions are so common.
Gabor Greif051a9502008-04-06 20:25:17 +0000476 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000477 const std::string &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +0000478 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000479 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000480 }
481 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000482 const std::string &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000483 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000484 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000485 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000486
Chris Lattnerf319e832004-10-15 23:52:05 +0000487 virtual GetElementPtrInst *clone() const;
Misha Brukman9769ab22005-04-21 20:19:05 +0000488
Gabor Greifefe65362008-05-10 08:32:32 +0000489 /// Transparently provide more efficient getOperand methods.
490 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
491
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000492 // getType - Overload to return most specific pointer type...
Devang Patel4d4a5e02008-02-23 01:11:02 +0000493 const PointerType *getType() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000494 return reinterpret_cast<const PointerType*>(Instruction::getType());
495 }
496
497 /// getIndexedType - Returns the type of the element that would be loaded with
498 /// a load instruction with the specified parameters.
499 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000500 /// Null is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000501 /// pointer type.
502 ///
David Greeneb8f74792007-09-04 15:46:09 +0000503 template<typename InputIterator>
Misha Brukman9769ab22005-04-21 20:19:05 +0000504 static const Type *getIndexedType(const Type *Ptr,
David Greeneb8f74792007-09-04 15:46:09 +0000505 InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +0000506 InputIterator IdxEnd) {
507 return getIndexedType(Ptr, IdxBegin, IdxEnd,
David Greeneb8f74792007-09-04 15:46:09 +0000508 typename std::iterator_traits<InputIterator>::
Dan Gohman041e2eb2008-05-15 19:50:34 +0000509 iterator_category());
David Greeneb8f74792007-09-04 15:46:09 +0000510 }
Matthijs Kooijmane2afded2008-07-29 08:46:11 +0000511
512 static const Type *getIndexedType(const Type *Ptr,
513 Value* const *Idx, unsigned NumIdx);
514
515 static const Type *getIndexedType(const Type *Ptr,
516 uint64_t const *Idx, unsigned NumIdx);
517
Chris Lattner38bacf22005-05-03 05:43:30 +0000518 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
Misha Brukman9769ab22005-04-21 20:19:05 +0000519
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000520 inline op_iterator idx_begin() { return op_begin()+1; }
521 inline const_op_iterator idx_begin() const { return op_begin()+1; }
522 inline op_iterator idx_end() { return op_end(); }
523 inline const_op_iterator idx_end() const { return op_end(); }
524
525 Value *getPointerOperand() {
526 return getOperand(0);
527 }
528 const Value *getPointerOperand() const {
529 return getOperand(0);
530 }
531 static unsigned getPointerOperandIndex() {
532 return 0U; // get index for modifying correct operand
533 }
534
Devang Patel4d4a5e02008-02-23 01:11:02 +0000535 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000536 return getNumOperands() - 1;
537 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000538
Devang Patel4d4a5e02008-02-23 01:11:02 +0000539 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000540 return getNumOperands() > 1;
541 }
Chris Lattner6f771d42007-04-14 00:12:57 +0000542
543 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
544 /// zeros. If so, the result pointer and the first operand have the same
545 /// value, just potentially different types.
546 bool hasAllZeroIndices() const;
Chris Lattner6b0974c2007-04-27 20:35:56 +0000547
548 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
549 /// constant integers. If so, the result pointer and the first operand have
550 /// a constant offset between them.
551 bool hasAllConstantIndices() const;
552
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000553
554 // Methods for support type inquiry through isa, cast, and dyn_cast:
555 static inline bool classof(const GetElementPtrInst *) { return true; }
556 static inline bool classof(const Instruction *I) {
557 return (I->getOpcode() == Instruction::GetElementPtr);
558 }
559 static inline bool classof(const Value *V) {
560 return isa<Instruction>(V) && classof(cast<Instruction>(V));
561 }
562};
563
Gabor Greifefe65362008-05-10 08:32:32 +0000564template <>
565struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
566};
567
568template<typename InputIterator>
569GetElementPtrInst::GetElementPtrInst(Value *Ptr,
570 InputIterator IdxBegin,
571 InputIterator IdxEnd,
572 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000573 const std::string &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000574 Instruction *InsertBefore)
575 : Instruction(PointerType::get(checkType(
576 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000577 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000578 cast<PointerType>(Ptr->getType())
579 ->getAddressSpace()),
580 GetElementPtr,
581 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
582 Values, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000583 init(Ptr, IdxBegin, IdxEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000584 typename std::iterator_traits<InputIterator>::iterator_category());
585}
586template<typename InputIterator>
587GetElementPtrInst::GetElementPtrInst(Value *Ptr,
588 InputIterator IdxBegin,
589 InputIterator IdxEnd,
590 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000591 const std::string &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000592 BasicBlock *InsertAtEnd)
593 : Instruction(PointerType::get(checkType(
594 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000595 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000596 cast<PointerType>(Ptr->getType())
597 ->getAddressSpace()),
598 GetElementPtr,
599 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
600 Values, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000601 init(Ptr, IdxBegin, IdxEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000602 typename std::iterator_traits<InputIterator>::iterator_category());
603}
604
605
606DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
607
608
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000609//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000610// ICmpInst Class
611//===----------------------------------------------------------------------===//
612
613/// This instruction compares its operands according to the predicate given
Nate Begemanac80ade2008-05-12 19:01:56 +0000614/// to the constructor. It only operates on integers or pointers. The operands
615/// must be identical types.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000616/// @brief Represent an integer comparison operator.
617class ICmpInst: public CmpInst {
618public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000619 /// @brief Constructor with insert-before-instruction semantics.
620 ICmpInst(
621 Predicate pred, ///< The predicate to use for the comparison
622 Value *LHS, ///< The left-hand-side of the expression
623 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000624 const std::string &NameStr = "", ///< Name of the instruction
Reid Spencer45fb3f32006-11-20 01:22:35 +0000625 Instruction *InsertBefore = 0 ///< Where to insert
Dan Gohmanf72fb672008-09-09 01:02:47 +0000626 ) : CmpInst(makeCmpResultType(LHS->getType()),
627 Instruction::ICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000628 InsertBefore) {
629 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
630 pred <= CmpInst::LAST_ICMP_PREDICATE &&
631 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000632 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000633 "Both operands to ICmp instruction are not of the same type!");
634 // Check that the operands are the right type
Dan Gohmanf72fb672008-09-09 01:02:47 +0000635 assert((getOperand(0)->getType()->isIntOrIntVector() ||
Nate Begeman31cd33a2008-05-14 20:28:31 +0000636 isa<PointerType>(getOperand(0)->getType())) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000637 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000638 }
639
640 /// @brief Constructor with insert-at-block-end semantics.
641 ICmpInst(
642 Predicate pred, ///< The predicate to use for the comparison
643 Value *LHS, ///< The left-hand-side of the expression
644 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000645 const std::string &NameStr, ///< Name of the instruction
Reid Spencer45fb3f32006-11-20 01:22:35 +0000646 BasicBlock *InsertAtEnd ///< Block to insert into.
Dan Gohmanf72fb672008-09-09 01:02:47 +0000647 ) : CmpInst(makeCmpResultType(LHS->getType()),
648 Instruction::ICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000649 InsertAtEnd) {
650 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
651 pred <= CmpInst::LAST_ICMP_PREDICATE &&
652 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000653 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000654 "Both operands to ICmp instruction are not of the same type!");
655 // Check that the operands are the right type
Dan Gohmanf72fb672008-09-09 01:02:47 +0000656 assert((getOperand(0)->getType()->isIntOrIntVector() ||
Nate Begeman31cd33a2008-05-14 20:28:31 +0000657 isa<PointerType>(getOperand(0)->getType())) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000658 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000659 }
660
Reid Spencere4d87aa2006-12-23 06:05:41 +0000661 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
662 /// @returns the predicate that would be the result if the operand were
663 /// regarded as signed.
664 /// @brief Return the signed version of the predicate
665 Predicate getSignedPredicate() const {
666 return getSignedPredicate(getPredicate());
667 }
668
669 /// This is a static version that you can use without an instruction.
670 /// @brief Return the signed version of the predicate.
671 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000672
Nick Lewycky4189a532008-01-28 03:48:02 +0000673 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
674 /// @returns the predicate that would be the result if the operand were
675 /// regarded as unsigned.
676 /// @brief Return the unsigned version of the predicate
677 Predicate getUnsignedPredicate() const {
678 return getUnsignedPredicate(getPredicate());
679 }
680
681 /// This is a static version that you can use without an instruction.
682 /// @brief Return the unsigned version of the predicate.
683 static Predicate getUnsignedPredicate(Predicate pred);
684
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000685 /// isEquality - Return true if this predicate is either EQ or NE. This also
686 /// tests for commutativity.
687 static bool isEquality(Predicate P) {
688 return P == ICMP_EQ || P == ICMP_NE;
689 }
690
691 /// isEquality - Return true if this predicate is either EQ or NE. This also
692 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000693 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000694 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000695 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000696
697 /// @returns true if the predicate of this ICmpInst is commutative
698 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000699 bool isCommutative() const { return isEquality(); }
700
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000701 /// isRelational - Return true if the predicate is relational (not EQ or NE).
702 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +0000703 bool isRelational() const {
704 return !isEquality();
705 }
706
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000707 /// isRelational - Return true if the predicate is relational (not EQ or NE).
708 ///
709 static bool isRelational(Predicate P) {
710 return !isEquality(P);
711 }
712
Reid Spencere4d87aa2006-12-23 06:05:41 +0000713 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
714 /// @brief Determine if this instruction's predicate is signed.
Chris Lattner5bda9e42007-09-15 06:51:03 +0000715 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000716
717 /// @returns true if the predicate provided is signed, false otherwise
718 /// @brief Determine if the predicate is signed.
719 static bool isSignedPredicate(Predicate pred);
720
Nick Lewyckyfc1efbb2008-05-17 07:33:39 +0000721 /// @returns true if the specified compare predicate is
722 /// true when both operands are equal...
723 /// @brief Determine if the icmp is true when both operands are equal
724 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
725 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
726 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
727 pred == ICmpInst::ICMP_SLE;
728 }
729
730 /// @returns true if the specified compare instruction is
731 /// true when both operands are equal...
732 /// @brief Determine if the ICmpInst returns true when both operands are equal
733 bool isTrueWhenEqual() {
734 return isTrueWhenEqual(getPredicate());
735 }
736
Reid Spencer3da43842007-02-28 22:00:54 +0000737 /// Initialize a set of values that all satisfy the predicate with C.
738 /// @brief Make a ConstantRange for a relation with a constant value.
739 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
740
Reid Spencer45fb3f32006-11-20 01:22:35 +0000741 /// Exchange the two operands to this instruction in such a way that it does
742 /// not modify the semantics of the instruction. The predicate value may be
743 /// changed to retain the same result if the predicate is order dependent
744 /// (e.g. ult).
745 /// @brief Swap operands and adjust predicate.
746 void swapOperands() {
747 SubclassData = getSwappedPredicate();
Gabor Greif94fb68b2008-05-13 22:51:52 +0000748 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000749 }
750
Chris Lattnercd406fe2007-08-24 20:48:18 +0000751 virtual ICmpInst *clone() const;
752
Reid Spencer45fb3f32006-11-20 01:22:35 +0000753 // Methods for support type inquiry through isa, cast, and dyn_cast:
754 static inline bool classof(const ICmpInst *) { return true; }
755 static inline bool classof(const Instruction *I) {
756 return I->getOpcode() == Instruction::ICmp;
757 }
758 static inline bool classof(const Value *V) {
759 return isa<Instruction>(V) && classof(cast<Instruction>(V));
760 }
Dan Gohmanf72fb672008-09-09 01:02:47 +0000761
Reid Spencer45fb3f32006-11-20 01:22:35 +0000762};
763
764//===----------------------------------------------------------------------===//
765// FCmpInst Class
766//===----------------------------------------------------------------------===//
767
768/// This instruction compares its operands according to the predicate given
769/// to the constructor. It only operates on floating point values or packed
770/// vectors of floating point values. The operands must be identical types.
771/// @brief Represents a floating point comparison operator.
772class FCmpInst: public CmpInst {
773public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000774 /// @brief Constructor with insert-before-instruction semantics.
775 FCmpInst(
776 Predicate pred, ///< The predicate to use for the comparison
777 Value *LHS, ///< The left-hand-side of the expression
778 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000779 const std::string &NameStr = "", ///< Name of the instruction
Reid Spencer45fb3f32006-11-20 01:22:35 +0000780 Instruction *InsertBefore = 0 ///< Where to insert
Dan Gohmanf72fb672008-09-09 01:02:47 +0000781 ) : CmpInst(makeCmpResultType(LHS->getType()),
782 Instruction::FCmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000783 InsertBefore) {
784 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
785 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000786 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000787 "Both operands to FCmp instruction are not of the same type!");
788 // Check that the operands are the right type
Dan Gohmanf72fb672008-09-09 01:02:47 +0000789 assert(getOperand(0)->getType()->isFPOrFPVector() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000790 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000791 }
792
793 /// @brief Constructor with insert-at-block-end semantics.
794 FCmpInst(
795 Predicate pred, ///< The predicate to use for the comparison
796 Value *LHS, ///< The left-hand-side of the expression
797 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000798 const std::string &NameStr, ///< Name of the instruction
Reid Spencer45fb3f32006-11-20 01:22:35 +0000799 BasicBlock *InsertAtEnd ///< Block to insert into.
Dan Gohmanf72fb672008-09-09 01:02:47 +0000800 ) : CmpInst(makeCmpResultType(LHS->getType()),
801 Instruction::FCmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000802 InsertAtEnd) {
803 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
804 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000805 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000806 "Both operands to FCmp instruction are not of the same type!");
807 // Check that the operands are the right type
Dan Gohmanf72fb672008-09-09 01:02:47 +0000808 assert(getOperand(0)->getType()->isFPOrFPVector() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000809 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000810 }
811
Reid Spencer45fb3f32006-11-20 01:22:35 +0000812 /// @returns true if the predicate of this instruction is EQ or NE.
813 /// @brief Determine if this is an equality predicate.
814 bool isEquality() const {
815 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
816 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
817 }
Dan Gohman793df072008-09-16 16:44:00 +0000818
819 /// @returns true if the predicate of this instruction is commutative.
820 /// @brief Determine if this is a commutative predicate.
821 bool isCommutative() const {
822 return isEquality() ||
823 SubclassData == FCMP_FALSE ||
824 SubclassData == FCMP_TRUE ||
825 SubclassData == FCMP_ORD ||
826 SubclassData == FCMP_UNO;
827 }
Reid Spencer45fb3f32006-11-20 01:22:35 +0000828
829 /// @returns true if the predicate is relational (not EQ or NE).
830 /// @brief Determine if this a relational predicate.
831 bool isRelational() const { return !isEquality(); }
832
833 /// Exchange the two operands to this instruction in such a way that it does
834 /// not modify the semantics of the instruction. The predicate value may be
835 /// changed to retain the same result if the predicate is order dependent
836 /// (e.g. ult).
837 /// @brief Swap operands and adjust predicate.
838 void swapOperands() {
839 SubclassData = getSwappedPredicate();
Gabor Greif94fb68b2008-05-13 22:51:52 +0000840 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000841 }
842
Chris Lattnercd406fe2007-08-24 20:48:18 +0000843 virtual FCmpInst *clone() const;
844
Reid Spencer45fb3f32006-11-20 01:22:35 +0000845 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
846 static inline bool classof(const FCmpInst *) { return true; }
847 static inline bool classof(const Instruction *I) {
848 return I->getOpcode() == Instruction::FCmp;
849 }
850 static inline bool classof(const Value *V) {
851 return isa<Instruction>(V) && classof(cast<Instruction>(V));
852 }
Dan Gohmanf72fb672008-09-09 01:02:47 +0000853
Reid Spencer45fb3f32006-11-20 01:22:35 +0000854};
855
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000856//===----------------------------------------------------------------------===//
Nate Begemanac80ade2008-05-12 19:01:56 +0000857// VICmpInst Class
858//===----------------------------------------------------------------------===//
859
860/// This instruction compares its operands according to the predicate given
861/// to the constructor. It only operates on vectors of integers.
862/// The operands must be identical types.
863/// @brief Represents a vector integer comparison operator.
864class VICmpInst: public CmpInst {
865public:
866 /// @brief Constructor with insert-before-instruction semantics.
867 VICmpInst(
868 Predicate pred, ///< The predicate to use for the comparison
869 Value *LHS, ///< The left-hand-side of the expression
870 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000871 const std::string &NameStr = "", ///< Name of the instruction
Nate Begemanac80ade2008-05-12 19:01:56 +0000872 Instruction *InsertBefore = 0 ///< Where to insert
Evan Cheng1bf9a182008-07-24 00:08:56 +0000873 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000874 InsertBefore) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000875 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
876 pred <= CmpInst::LAST_ICMP_PREDICATE &&
877 "Invalid VICmp predicate value");
878 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
879 "Both operands to VICmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000880 }
881
882 /// @brief Constructor with insert-at-block-end semantics.
883 VICmpInst(
884 Predicate pred, ///< The predicate to use for the comparison
885 Value *LHS, ///< The left-hand-side of the expression
886 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000887 const std::string &NameStr, ///< Name of the instruction
Nate Begemanac80ade2008-05-12 19:01:56 +0000888 BasicBlock *InsertAtEnd ///< Block to insert into.
Evan Cheng1bf9a182008-07-24 00:08:56 +0000889 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000890 InsertAtEnd) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000891 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
892 pred <= CmpInst::LAST_ICMP_PREDICATE &&
893 "Invalid VICmp predicate value");
894 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
895 "Both operands to VICmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000896 }
897
898 /// @brief Return the predicate for this instruction.
899 Predicate getPredicate() const { return Predicate(SubclassData); }
900
901 virtual VICmpInst *clone() const;
902
903 // Methods for support type inquiry through isa, cast, and dyn_cast:
904 static inline bool classof(const VICmpInst *) { return true; }
905 static inline bool classof(const Instruction *I) {
906 return I->getOpcode() == Instruction::VICmp;
907 }
908 static inline bool classof(const Value *V) {
909 return isa<Instruction>(V) && classof(cast<Instruction>(V));
910 }
911};
912
913//===----------------------------------------------------------------------===//
914// VFCmpInst Class
915//===----------------------------------------------------------------------===//
916
917/// This instruction compares its operands according to the predicate given
918/// to the constructor. It only operates on vectors of floating point values.
919/// The operands must be identical types.
920/// @brief Represents a vector floating point comparison operator.
921class VFCmpInst: public CmpInst {
922public:
923 /// @brief Constructor with insert-before-instruction semantics.
924 VFCmpInst(
925 Predicate pred, ///< The predicate to use for the comparison
926 Value *LHS, ///< The left-hand-side of the expression
927 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000928 const std::string &NameStr = "", ///< Name of the instruction
Nate Begemanac80ade2008-05-12 19:01:56 +0000929 Instruction *InsertBefore = 0 ///< Where to insert
930 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
Evan Cheng1bf9a182008-07-24 00:08:56 +0000931 Instruction::VFCmp, pred, LHS, RHS, NameStr, InsertBefore) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000932 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
933 "Invalid VFCmp predicate value");
934 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
935 "Both operands to VFCmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000936 }
937
938 /// @brief Constructor with insert-at-block-end semantics.
939 VFCmpInst(
940 Predicate pred, ///< The predicate to use for the comparison
941 Value *LHS, ///< The left-hand-side of the expression
942 Value *RHS, ///< The right-hand-side of the expression
Evan Cheng1bf9a182008-07-24 00:08:56 +0000943 const std::string &NameStr, ///< Name of the instruction
Nate Begemanac80ade2008-05-12 19:01:56 +0000944 BasicBlock *InsertAtEnd ///< Block to insert into.
945 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
Evan Cheng1bf9a182008-07-24 00:08:56 +0000946 Instruction::VFCmp, pred, LHS, RHS, NameStr, InsertAtEnd) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000947 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
948 "Invalid VFCmp predicate value");
949 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
950 "Both operands to VFCmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000951 }
952
953 /// @brief Return the predicate for this instruction.
954 Predicate getPredicate() const { return Predicate(SubclassData); }
955
956 virtual VFCmpInst *clone() const;
957
958 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
959 static inline bool classof(const VFCmpInst *) { return true; }
960 static inline bool classof(const Instruction *I) {
961 return I->getOpcode() == Instruction::VFCmp;
962 }
963 static inline bool classof(const Value *V) {
964 return isa<Instruction>(V) && classof(cast<Instruction>(V));
965 }
966};
967
968//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000969// CallInst Class
970//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000971/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +0000972/// machine's calling convention. This class uses low bit of the SubClassData
973/// field to indicate whether or not this is a tail call. The rest of the bits
974/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000975///
David Greene52eec542007-08-01 03:43:44 +0000976
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000977class CallInst : public Instruction {
Devang Patel05988662008-09-25 21:00:45 +0000978 AttrListPtr AttributeList; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000979 CallInst(const CallInst &CI);
Chris Lattnerd54f4322007-02-13 00:58:44 +0000980 void init(Value *Func, Value* const *Params, unsigned NumParams);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000981 void init(Value *Func, Value *Actual1, Value *Actual2);
982 void init(Value *Func, Value *Actual);
983 void init(Value *Func);
984
David Greene52eec542007-08-01 03:43:44 +0000985 template<typename InputIterator>
986 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000987 const std::string &NameStr,
David Greene52eec542007-08-01 03:43:44 +0000988 // This argument ensures that we have an iterator we can
989 // do arithmetic on in constant time
990 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000991 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greene52eec542007-08-01 03:43:44 +0000992
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000993 // This requires that the iterator points to contiguous memory.
994 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
Evan Cheng1bf9a182008-07-24 00:08:56 +0000995 setName(NameStr);
David Greene52eec542007-08-01 03:43:44 +0000996 }
997
David Greene52eec542007-08-01 03:43:44 +0000998 /// Construct a CallInst given a range of arguments. InputIterator
999 /// must be a random-access iterator pointing to contiguous storage
1000 /// (e.g. a std::vector<>::iterator). Checks are made for
1001 /// random-accessness but not for contiguous storage as that would
1002 /// incur runtime overhead.
1003 /// @brief Construct a CallInst from a range of arguments
1004 template<typename InputIterator>
1005 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001006 const std::string &NameStr, Instruction *InsertBefore);
David Greene52eec542007-08-01 03:43:44 +00001007
1008 /// Construct a CallInst given a range of arguments. InputIterator
1009 /// must be a random-access iterator pointing to contiguous storage
1010 /// (e.g. a std::vector<>::iterator). Checks are made for
1011 /// random-accessness but not for contiguous storage as that would
1012 /// incur runtime overhead.
1013 /// @brief Construct a CallInst from a range of arguments
1014 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00001015 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001016 const std::string &NameStr, BasicBlock *InsertAtEnd);
David Greene52eec542007-08-01 03:43:44 +00001017
Evan Cheng1bf9a182008-07-24 00:08:56 +00001018 CallInst(Value *F, Value *Actual, const std::string& NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001019 Instruction *InsertBefore);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001020 CallInst(Value *F, Value *Actual, const std::string& NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001021 BasicBlock *InsertAtEnd);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001022 explicit CallInst(Value *F, const std::string &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001023 Instruction *InsertBefore);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001024 CallInst(Value *F, const std::string &NameStr, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001025public:
1026 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00001027 static CallInst *Create(Value *Func,
1028 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001029 const std::string &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001030 Instruction *InsertBefore = 0) {
Bill Wendlingc2e73532008-05-10 19:59:59 +00001031 return new((unsigned)(ArgEnd - ArgBegin + 1))
Evan Cheng1bf9a182008-07-24 00:08:56 +00001032 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001033 }
1034 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00001035 static CallInst *Create(Value *Func,
1036 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001037 const std::string &NameStr, BasicBlock *InsertAtEnd) {
Bill Wendlingc2e73532008-05-10 19:59:59 +00001038 return new((unsigned)(ArgEnd - ArgBegin + 1))
Evan Cheng1bf9a182008-07-24 00:08:56 +00001039 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001040 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001041 static CallInst *Create(Value *F, Value *Actual,
1042 const std::string& NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001043 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001044 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001045 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001046 static CallInst *Create(Value *F, Value *Actual, const std::string& NameStr,
Gabor Greif051a9502008-04-06 20:25:17 +00001047 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001048 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001049 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001050 static CallInst *Create(Value *F, const std::string &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001051 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001052 return new(1) CallInst(F, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001053 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001054 static CallInst *Create(Value *F, const std::string &NameStr,
Evan Cheng34cd4a42008-05-05 18:30:58 +00001055 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001056 return new(1) CallInst(F, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001057 }
1058
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001059 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001060
Chris Lattner3340ffe2005-05-06 20:26:26 +00001061 bool isTailCall() const { return SubclassData & 1; }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001062 void setTailCall(bool isTC = true) {
1063 SubclassData = (SubclassData & ~1) | unsigned(isTC);
Chris Lattner3340ffe2005-05-06 20:26:26 +00001064 }
1065
Dan Gohmanf2752502008-09-26 21:38:45 +00001066 virtual CallInst *clone() const;
1067
1068 /// Provide fast operand accessors
1069 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1070
Chris Lattner3340ffe2005-05-06 20:26:26 +00001071 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1072 /// function call.
1073 unsigned getCallingConv() const { return SubclassData >> 1; }
1074 void setCallingConv(unsigned CC) {
1075 SubclassData = (SubclassData & 1) | (CC << 1);
1076 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00001077
Devang Patel05988662008-09-25 21:00:45 +00001078 /// getAttributes - Return the parameter attributes for this call.
Chris Lattner041221c2008-03-13 04:33:03 +00001079 ///
Devang Patel05988662008-09-25 21:00:45 +00001080 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001081
Dan Gohmanf2752502008-09-26 21:38:45 +00001082 /// setAttributes - Set the parameter attributes for this call.
1083 ///
Devang Patel05988662008-09-25 21:00:45 +00001084 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Eric Christopher0bf7b412008-05-16 20:39:43 +00001085
Devang Patel05988662008-09-25 21:00:45 +00001086 /// addAttribute - adds the attribute to the list of attributes.
1087 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsdc024672007-11-27 13:23:08 +00001088
Devang Patel05988662008-09-25 21:00:45 +00001089 /// removeAttribute - removes the attribute from the list of attributes.
1090 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00001091
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001092 /// @brief Determine whether the call or the callee has the given attribute.
Dan Gohmanf2752502008-09-26 21:38:45 +00001093 bool paramHasAttr(unsigned i, Attributes attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001094
Dale Johannesen08e78b12008-02-22 17:49:45 +00001095 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001096 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00001097 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001098 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001099
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001100 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001101 bool doesNotAccessMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00001102 return paramHasAttr(~0, Attribute::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001103 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001104 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001105 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1106 else removeAttribute(~0, Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00001107 }
1108
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001109 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001110 bool onlyReadsMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00001111 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001112 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001113 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001114 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1115 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00001116 }
1117
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001118 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001119 bool doesNotReturn() const {
Devang Patel19c87462008-09-26 22:53:05 +00001120 return paramHasAttr(~0, Attribute::NoReturn);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001121 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001122 void setDoesNotReturn(bool DoesNotReturn = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001123 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1124 else removeAttribute(~0, Attribute::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00001125 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001126
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001127 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001128 bool doesNotThrow() const {
Devang Patel19c87462008-09-26 22:53:05 +00001129 return paramHasAttr(~0, Attribute::NoUnwind);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001130 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001131 void setDoesNotThrow(bool DoesNotThrow = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001132 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1133 else removeAttribute(~0, Attribute::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00001134 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001135
Devang Patel41e23972008-03-03 21:46:28 +00001136 /// @brief Determine if the call returns a structure through first
1137 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001138 bool hasStructRetAttr() const {
1139 // Be friendly and also check the callee.
Devang Patel05988662008-09-25 21:00:45 +00001140 return paramHasAttr(1, Attribute::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001141 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001142
Evan Chengf4a54982008-01-12 18:57:32 +00001143 /// @brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001144 bool hasByValArgument() const {
Devang Patel05988662008-09-25 21:00:45 +00001145 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001146 }
Evan Chengf4a54982008-01-12 18:57:32 +00001147
Dan Gohmanf2752502008-09-26 21:38:45 +00001148 /// getCalledFunction - Return the function called, or null if this is an
1149 /// indirect function invocation.
1150 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001151 Function *getCalledFunction() const {
Dan Gohman11a7dbf2007-09-24 15:46:02 +00001152 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001153 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001154
Reid Spencerc25ec252006-12-29 04:10:59 +00001155 /// getCalledValue - Get a pointer to the function that is invoked by this
1156 /// instruction
Devang Patel4d4a5e02008-02-23 01:11:02 +00001157 const Value *getCalledValue() const { return getOperand(0); }
1158 Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001159
1160 // Methods for support type inquiry through isa, cast, and dyn_cast:
1161 static inline bool classof(const CallInst *) { return true; }
1162 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001163 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001164 }
1165 static inline bool classof(const Value *V) {
1166 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1167 }
1168};
1169
Gabor Greifefe65362008-05-10 08:32:32 +00001170template <>
1171struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1172};
1173
1174template<typename InputIterator>
1175CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001176 const std::string &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001177 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1178 ->getElementType())->getReturnType(),
1179 Instruction::Call,
1180 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001181 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001182 init(Func, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001183 typename std::iterator_traits<InputIterator>::iterator_category());
1184}
1185
1186template<typename InputIterator>
1187CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001188 const std::string &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00001189 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1190 ->getElementType())->getReturnType(),
1191 Instruction::Call,
1192 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001193 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001194 init(Func, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001195 typename std::iterator_traits<InputIterator>::iterator_category());
1196}
1197
1198DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1199
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001200//===----------------------------------------------------------------------===//
1201// SelectInst Class
1202//===----------------------------------------------------------------------===//
1203
1204/// SelectInst - This class represents the LLVM 'select' instruction.
1205///
1206class SelectInst : public Instruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001207 void init(Value *C, Value *S1, Value *S2) {
Chris Lattnerb76ec322008-12-29 00:12:50 +00001208 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
Gabor Greifefe65362008-05-10 08:32:32 +00001209 Op<0>() = C;
1210 Op<1>() = S1;
1211 Op<2>() = S2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001212 }
1213
Chris Lattner454928e2005-01-29 00:31:36 +00001214 SelectInst(const SelectInst &SI)
Gabor Greifefe65362008-05-10 08:32:32 +00001215 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1216 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
Chris Lattner454928e2005-01-29 00:31:36 +00001217 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001218 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001219 Instruction *InsertBefore)
1220 : Instruction(S1->getType(), Instruction::Select,
1221 &Op<0>(), 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001222 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001223 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001224 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001225 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001226 BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001227 : Instruction(S1->getType(), Instruction::Select,
1228 &Op<0>(), 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001229 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001230 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001231 }
Gabor Greif051a9502008-04-06 20:25:17 +00001232public:
Evan Chengd69bb1a2008-05-05 17:41:03 +00001233 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001234 const std::string &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001235 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001236 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001237 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001238 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001239 const std::string &NameStr,
1240 BasicBlock *InsertAtEnd) {
1241 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001242 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001243
Gabor Greifefe65362008-05-10 08:32:32 +00001244 Value *getCondition() const { return Op<0>(); }
1245 Value *getTrueValue() const { return Op<1>(); }
1246 Value *getFalseValue() const { return Op<2>(); }
Chris Lattnerb76ec322008-12-29 00:12:50 +00001247
1248 /// areInvalidOperands - Return a string if the specified operands are invalid
1249 /// for a select operation, otherwise return null.
1250 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
Chris Lattner454928e2005-01-29 00:31:36 +00001251
1252 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001253 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001254
1255 OtherOps getOpcode() const {
1256 return static_cast<OtherOps>(Instruction::getOpcode());
1257 }
1258
Chris Lattnerf319e832004-10-15 23:52:05 +00001259 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001260
1261 // Methods for support type inquiry through isa, cast, and dyn_cast:
1262 static inline bool classof(const SelectInst *) { return true; }
1263 static inline bool classof(const Instruction *I) {
1264 return I->getOpcode() == Instruction::Select;
1265 }
1266 static inline bool classof(const Value *V) {
1267 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1268 }
1269};
1270
Gabor Greifefe65362008-05-10 08:32:32 +00001271template <>
1272struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1273};
1274
1275DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1276
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001277//===----------------------------------------------------------------------===//
1278// VAArgInst Class
1279//===----------------------------------------------------------------------===//
1280
1281/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001282/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001283///
Chris Lattner454928e2005-01-29 00:31:36 +00001284class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001285 VAArgInst(const VAArgInst &VAA)
Chris Lattner454928e2005-01-29 00:31:36 +00001286 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001287public:
Evan Cheng1bf9a182008-07-24 00:08:56 +00001288 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001289 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001290 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001291 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001292 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001293 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001294 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001295 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001296 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001297 }
1298
Chris Lattnerf319e832004-10-15 23:52:05 +00001299 virtual VAArgInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001300
1301 // Methods for support type inquiry through isa, cast, and dyn_cast:
1302 static inline bool classof(const VAArgInst *) { return true; }
1303 static inline bool classof(const Instruction *I) {
1304 return I->getOpcode() == VAArg;
1305 }
1306 static inline bool classof(const Value *V) {
1307 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1308 }
1309};
1310
1311//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001312// ExtractElementInst Class
1313//===----------------------------------------------------------------------===//
1314
1315/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001316/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001317///
1318class ExtractElementInst : public Instruction {
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001319 ExtractElementInst(const ExtractElementInst &EE) :
Gabor Greifefe65362008-05-10 08:32:32 +00001320 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
Gabor Greif6c80c382008-05-26 21:33:52 +00001321 Op<0>() = EE.Op<0>();
1322 Op<1>() = EE.Op<1>();
Robert Bocchino49b78a52006-01-10 19:04:13 +00001323 }
1324
1325public:
Gabor Greif051a9502008-04-06 20:25:17 +00001326 // allocate space for exactly two operands
1327 void *operator new(size_t s) {
Gabor Greifefe65362008-05-10 08:32:32 +00001328 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
Gabor Greif051a9502008-04-06 20:25:17 +00001329 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001330 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr = "",
Chris Lattner9fc18d22006-04-08 01:15:18 +00001331 Instruction *InsertBefore = 0);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001332 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr = "",
Chris Lattner06a248c22006-10-05 06:24:58 +00001333 Instruction *InsertBefore = 0);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001334 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr,
Chris Lattner9fc18d22006-04-08 01:15:18 +00001335 BasicBlock *InsertAtEnd);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001336 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr,
Chris Lattner06a248c22006-10-05 06:24:58 +00001337 BasicBlock *InsertAtEnd);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001338
Chris Lattnerfa495842006-04-08 04:04:54 +00001339 /// isValidOperands - Return true if an extractelement instruction can be
1340 /// formed with the specified operands.
1341 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001342
Robert Bocchino49b78a52006-01-10 19:04:13 +00001343 virtual ExtractElementInst *clone() const;
1344
Robert Bocchino49b78a52006-01-10 19:04:13 +00001345 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001346 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001347
1348 // Methods for support type inquiry through isa, cast, and dyn_cast:
1349 static inline bool classof(const ExtractElementInst *) { return true; }
1350 static inline bool classof(const Instruction *I) {
1351 return I->getOpcode() == Instruction::ExtractElement;
1352 }
1353 static inline bool classof(const Value *V) {
1354 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1355 }
1356};
1357
Gabor Greifefe65362008-05-10 08:32:32 +00001358template <>
1359struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1360};
1361
1362DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1363
Robert Bocchino49b78a52006-01-10 19:04:13 +00001364//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001365// InsertElementInst Class
1366//===----------------------------------------------------------------------===//
1367
1368/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001369/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001370///
1371class InsertElementInst : public Instruction {
Chris Lattner6a56ed42006-04-14 22:20:07 +00001372 InsertElementInst(const InsertElementInst &IE);
Gabor Greif051a9502008-04-06 20:25:17 +00001373 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001374 const std::string &NameStr = "",Instruction *InsertBefore = 0);
Gabor Greif051a9502008-04-06 20:25:17 +00001375 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001376 const std::string &NameStr = "",Instruction *InsertBefore = 0);
Gabor Greif051a9502008-04-06 20:25:17 +00001377 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001378 const std::string &NameStr, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001379 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001380 const std::string &NameStr, BasicBlock *InsertAtEnd);
Robert Bocchinof9993442006-01-17 20:05:59 +00001381public:
Gabor Greif051a9502008-04-06 20:25:17 +00001382 static InsertElementInst *Create(const InsertElementInst &IE) {
1383 return new(IE.getNumOperands()) InsertElementInst(IE);
1384 }
1385 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001386 const std::string &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +00001387 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001388 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001389 }
1390 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001391 const std::string &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001392 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001393 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001394 }
1395 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001396 const std::string &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001397 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001398 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001399 }
1400 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001401 const std::string &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001402 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001403 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001404 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001405
Chris Lattnerfa495842006-04-08 04:04:54 +00001406 /// isValidOperands - Return true if an insertelement instruction can be
1407 /// formed with the specified operands.
1408 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1409 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001410
Robert Bocchinof9993442006-01-17 20:05:59 +00001411 virtual InsertElementInst *clone() const;
1412
Reid Spencerac9dcb92007-02-15 03:39:18 +00001413 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001414 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001415 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001416 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001417 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001418
Robert Bocchinof9993442006-01-17 20:05:59 +00001419 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001420 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchinof9993442006-01-17 20:05:59 +00001421
1422 // Methods for support type inquiry through isa, cast, and dyn_cast:
1423 static inline bool classof(const InsertElementInst *) { return true; }
1424 static inline bool classof(const Instruction *I) {
1425 return I->getOpcode() == Instruction::InsertElement;
1426 }
1427 static inline bool classof(const Value *V) {
1428 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1429 }
1430};
1431
Gabor Greifefe65362008-05-10 08:32:32 +00001432template <>
1433struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1434};
1435
1436DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1437
Robert Bocchinof9993442006-01-17 20:05:59 +00001438//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001439// ShuffleVectorInst Class
1440//===----------------------------------------------------------------------===//
1441
1442/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1443/// input vectors.
1444///
1445class ShuffleVectorInst : public Instruction {
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001446 ShuffleVectorInst(const ShuffleVectorInst &IE);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001447public:
Gabor Greif051a9502008-04-06 20:25:17 +00001448 // allocate space for exactly three operands
1449 void *operator new(size_t s) {
1450 return User::operator new(s, 3);
1451 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001452 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001453 const std::string &NameStr = "",
1454 Instruction *InsertBefor = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001455 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001456 const std::string &NameStr, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001457
Chris Lattnerfa495842006-04-08 04:04:54 +00001458 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001459 /// formed with the specified operands.
1460 static bool isValidOperands(const Value *V1, const Value *V2,
1461 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001462
Chris Lattner9fc18d22006-04-08 01:15:18 +00001463 virtual ShuffleVectorInst *clone() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001464
Reid Spencerac9dcb92007-02-15 03:39:18 +00001465 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001466 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001467 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001468 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001469 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001470
Chris Lattner9fc18d22006-04-08 01:15:18 +00001471 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001472 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner8728f192008-03-02 05:28:33 +00001473
1474 /// getMaskValue - Return the index from the shuffle mask for the specified
1475 /// output result. This is either -1 if the element is undef or a number less
1476 /// than 2*numelements.
1477 int getMaskValue(unsigned i) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001478
Chris Lattner9fc18d22006-04-08 01:15:18 +00001479 // Methods for support type inquiry through isa, cast, and dyn_cast:
1480 static inline bool classof(const ShuffleVectorInst *) { return true; }
1481 static inline bool classof(const Instruction *I) {
1482 return I->getOpcode() == Instruction::ShuffleVector;
1483 }
1484 static inline bool classof(const Value *V) {
1485 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1486 }
1487};
1488
Gabor Greifefe65362008-05-10 08:32:32 +00001489template <>
1490struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1491};
1492
1493DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
Chris Lattner9fc18d22006-04-08 01:15:18 +00001494
1495//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001496// ExtractValueInst Class
1497//===----------------------------------------------------------------------===//
1498
Dan Gohmane2d896f2008-05-15 23:35:32 +00001499/// ExtractValueInst - This instruction extracts a struct member or array
1500/// element value from an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001501///
Gabor Greifd4f268b2008-06-06 20:28:12 +00001502class ExtractValueInst : public UnaryInstruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001503 SmallVector<unsigned, 4> Indices;
1504
Dan Gohman041e2eb2008-05-15 19:50:34 +00001505 ExtractValueInst(const ExtractValueInst &EVI);
Gabor Greif76aca6f2008-06-06 21:06:32 +00001506 void init(const unsigned *Idx, unsigned NumIdx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001507 const std::string &NameStr);
1508 void init(unsigned Idx, const std::string &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001509
1510 template<typename InputIterator>
Gabor Greif76aca6f2008-06-06 21:06:32 +00001511 void init(InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001512 const std::string &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001513 // This argument ensures that we have an iterator we can
1514 // do arithmetic on in constant time
1515 std::random_access_iterator_tag) {
1516 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1517
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001518 // There's no fundamental reason why we require at least one index
1519 // (other than weirdness with &*IdxBegin being invalid; see
1520 // getelementptr's init routine for example). But there's no
1521 // present need to support it.
1522 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1523
1524 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +00001525 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001526 // we have to build an array here
Dan Gohman041e2eb2008-05-15 19:50:34 +00001527 }
1528
1529 /// getIndexedType - Returns the type of the element that would be extracted
1530 /// with an extractvalue instruction with the specified parameters.
1531 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +00001532 /// Null is returned if the indices are invalid for the specified
Dan Gohman041e2eb2008-05-15 19:50:34 +00001533 /// pointer type.
1534 ///
1535 static const Type *getIndexedType(const Type *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001536 const unsigned *Idx, unsigned NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001537
1538 template<typename InputIterator>
1539 static const Type *getIndexedType(const Type *Ptr,
1540 InputIterator IdxBegin,
1541 InputIterator IdxEnd,
1542 // This argument ensures that we
1543 // have an iterator we can do
1544 // arithmetic on in constant time
1545 std::random_access_iterator_tag) {
1546 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1547
1548 if (NumIdx > 0)
1549 // This requires that the iterator points to contiguous memory.
Dan Gohman19a81632008-06-23 16:38:10 +00001550 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001551 else
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001552 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001553 }
1554
Dan Gohmane2d896f2008-05-15 23:35:32 +00001555 /// Constructors - Create a extractvalue instruction with a base aggregate
1556 /// value and a list of indices. The first ctor can optionally insert before
1557 /// an existing instruction, the second appends the new instruction to the
1558 /// specified BasicBlock.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001559 template<typename InputIterator>
1560 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1561 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001562 const std::string &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001563 Instruction *InsertBefore);
1564 template<typename InputIterator>
1565 inline ExtractValueInst(Value *Agg,
1566 InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001567 const std::string &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001568
Dan Gohman8e640412008-05-31 19:09:47 +00001569 // allocate space for exactly one operand
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001570 void *operator new(size_t s) {
1571 return User::operator new(s, 1);
1572 }
1573
Gabor Greifd4f268b2008-06-06 20:28:12 +00001574public:
Dan Gohman041e2eb2008-05-15 19:50:34 +00001575 template<typename InputIterator>
1576 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1577 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001578 const std::string &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001579 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001580 return new
Evan Cheng1bf9a182008-07-24 00:08:56 +00001581 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001582 }
1583 template<typename InputIterator>
1584 static ExtractValueInst *Create(Value *Agg,
1585 InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001586 const std::string &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001587 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001588 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001589 }
1590
1591 /// Constructors - These two creators are convenience methods because one
1592 /// index extractvalue instructions are much more common than those with
1593 /// more than one.
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001594 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001595 const std::string &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001596 Instruction *InsertBefore = 0) {
Dan Gohman2f27e172008-06-23 16:48:17 +00001597 unsigned Idxs[1] = { Idx };
Evan Cheng1bf9a182008-07-24 00:08:56 +00001598 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001599 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001600 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001601 const std::string &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001602 BasicBlock *InsertAtEnd) {
Dan Gohman2f27e172008-06-23 16:48:17 +00001603 unsigned Idxs[1] = { Idx };
Evan Cheng1bf9a182008-07-24 00:08:56 +00001604 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001605 }
1606
1607 virtual ExtractValueInst *clone() const;
1608
Dan Gohman041e2eb2008-05-15 19:50:34 +00001609 // getType - Overload to return most specific pointer type...
1610 const PointerType *getType() const {
1611 return reinterpret_cast<const PointerType*>(Instruction::getType());
1612 }
1613
1614 /// getIndexedType - Returns the type of the element that would be extracted
1615 /// with an extractvalue instruction with the specified parameters.
1616 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +00001617 /// Null is returned if the indices are invalid for the specified
Dan Gohman041e2eb2008-05-15 19:50:34 +00001618 /// pointer type.
1619 ///
1620 template<typename InputIterator>
1621 static const Type *getIndexedType(const Type *Ptr,
1622 InputIterator IdxBegin,
1623 InputIterator IdxEnd) {
1624 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1625 typename std::iterator_traits<InputIterator>::
1626 iterator_category());
1627 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001628 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001629
Owen Anderson5678d6e2008-06-19 17:15:57 +00001630 typedef const unsigned* idx_iterator;
1631 inline idx_iterator idx_begin() const { return Indices.begin(); }
1632 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001633
1634 Value *getAggregateOperand() {
1635 return getOperand(0);
1636 }
1637 const Value *getAggregateOperand() const {
1638 return getOperand(0);
1639 }
1640 static unsigned getAggregateOperandIndex() {
1641 return 0U; // get index for modifying correct operand
1642 }
1643
1644 unsigned getNumIndices() const { // Note: always non-negative
Bill Wendling67944fc2008-06-05 07:35:27 +00001645 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001646 }
1647
1648 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001649 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001650 }
1651
1652 // Methods for support type inquiry through isa, cast, and dyn_cast:
1653 static inline bool classof(const ExtractValueInst *) { return true; }
1654 static inline bool classof(const Instruction *I) {
1655 return I->getOpcode() == Instruction::ExtractValue;
1656 }
1657 static inline bool classof(const Value *V) {
1658 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1659 }
1660};
1661
Dan Gohmane4569942008-05-23 00:36:11 +00001662template<typename InputIterator>
1663ExtractValueInst::ExtractValueInst(Value *Agg,
1664 InputIterator IdxBegin,
1665 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001666 const std::string &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001667 Instruction *InsertBefore)
Gabor Greifd4f268b2008-06-06 20:28:12 +00001668 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
Bill Wendling85f40542008-07-22 07:14:12 +00001669 IdxBegin, IdxEnd)),
1670 ExtractValue, Agg, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001671 init(IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001672 typename std::iterator_traits<InputIterator>::iterator_category());
1673}
1674template<typename InputIterator>
1675ExtractValueInst::ExtractValueInst(Value *Agg,
1676 InputIterator IdxBegin,
1677 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001678 const std::string &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001679 BasicBlock *InsertAtEnd)
Gabor Greifd4f268b2008-06-06 20:28:12 +00001680 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
Bill Wendling85f40542008-07-22 07:14:12 +00001681 IdxBegin, IdxEnd)),
1682 ExtractValue, Agg, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001683 init(IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001684 typename std::iterator_traits<InputIterator>::iterator_category());
1685}
1686
Dan Gohmane4569942008-05-23 00:36:11 +00001687
Dan Gohman041e2eb2008-05-15 19:50:34 +00001688//===----------------------------------------------------------------------===//
1689// InsertValueInst Class
1690//===----------------------------------------------------------------------===//
1691
Dan Gohmane2d896f2008-05-15 23:35:32 +00001692/// InsertValueInst - This instruction inserts a struct field of array element
1693/// value into an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001694///
1695class InsertValueInst : public Instruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001696 SmallVector<unsigned, 4> Indices;
1697
1698 void *operator new(size_t, unsigned); // Do not implement
Dan Gohman041e2eb2008-05-15 19:50:34 +00001699 InsertValueInst(const InsertValueInst &IVI);
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +00001700 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001701 const std::string &NameStr);
1702 void init(Value *Agg, Value *Val, unsigned Idx, const std::string &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001703
1704 template<typename InputIterator>
1705 void init(Value *Agg, Value *Val,
1706 InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001707 const std::string &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001708 // This argument ensures that we have an iterator we can
1709 // do arithmetic on in constant time
1710 std::random_access_iterator_tag) {
1711 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1712
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001713 // There's no fundamental reason why we require at least one index
1714 // (other than weirdness with &*IdxBegin being invalid; see
1715 // getelementptr's init routine for example). But there's no
1716 // present need to support it.
1717 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1718
1719 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +00001720 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001721 // we have to build an array here
Dan Gohman041e2eb2008-05-15 19:50:34 +00001722 }
1723
Dan Gohmane2d896f2008-05-15 23:35:32 +00001724 /// Constructors - Create a insertvalue instruction with a base aggregate
1725 /// value, a value to insert, and a list of indices. The first ctor can
1726 /// optionally insert before an existing instruction, the second appends
1727 /// the new instruction to the specified BasicBlock.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001728 template<typename InputIterator>
1729 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001730 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001731 const std::string &NameStr,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001732 Instruction *InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001733 template<typename InputIterator>
1734 inline InsertValueInst(Value *Agg, Value *Val,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001735 InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001736 const std::string &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001737
1738 /// Constructors - These two constructors are convenience methods because one
1739 /// and two index insertvalue instructions are so common.
1740 InsertValueInst(Value *Agg, Value *Val,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001741 unsigned Idx, const std::string &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001742 Instruction *InsertBefore = 0);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001743 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001744 const std::string &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001745public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001746 // allocate space for exactly two operands
1747 void *operator new(size_t s) {
1748 return User::operator new(s, 2);
1749 }
1750
Dan Gohman041e2eb2008-05-15 19:50:34 +00001751 template<typename InputIterator>
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +00001752 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001753 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001754 const std::string &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001755 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001756 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001757 NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001758 }
1759 template<typename InputIterator>
1760 static InsertValueInst *Create(Value *Agg, Value *Val,
1761 InputIterator IdxBegin, InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001762 const std::string &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001763 BasicBlock *InsertAtEnd) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001764 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001765 NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001766 }
1767
1768 /// Constructors - These two creators are convenience methods because one
1769 /// index insertvalue instructions are much more common than those with
1770 /// more than one.
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001771 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001772 const std::string &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001773 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001774 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001775 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001776 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001777 const std::string &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001778 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001779 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001780 }
1781
1782 virtual InsertValueInst *clone() const;
1783
1784 /// Transparently provide more efficient getOperand methods.
1785 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1786
1787 // getType - Overload to return most specific pointer type...
1788 const PointerType *getType() const {
1789 return reinterpret_cast<const PointerType*>(Instruction::getType());
1790 }
1791
Owen Anderson5678d6e2008-06-19 17:15:57 +00001792 typedef const unsigned* idx_iterator;
1793 inline idx_iterator idx_begin() const { return Indices.begin(); }
1794 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001795
1796 Value *getAggregateOperand() {
1797 return getOperand(0);
1798 }
1799 const Value *getAggregateOperand() const {
1800 return getOperand(0);
1801 }
1802 static unsigned getAggregateOperandIndex() {
1803 return 0U; // get index for modifying correct operand
1804 }
1805
1806 Value *getInsertedValueOperand() {
1807 return getOperand(1);
1808 }
1809 const Value *getInsertedValueOperand() const {
1810 return getOperand(1);
1811 }
1812 static unsigned getInsertedValueOperandIndex() {
1813 return 1U; // get index for modifying correct operand
1814 }
1815
1816 unsigned getNumIndices() const { // Note: always non-negative
Bill Wendling67944fc2008-06-05 07:35:27 +00001817 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001818 }
1819
1820 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001821 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001822 }
1823
1824 // Methods for support type inquiry through isa, cast, and dyn_cast:
1825 static inline bool classof(const InsertValueInst *) { return true; }
1826 static inline bool classof(const Instruction *I) {
1827 return I->getOpcode() == Instruction::InsertValue;
1828 }
1829 static inline bool classof(const Value *V) {
1830 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1831 }
1832};
1833
1834template <>
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001835struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001836};
1837
Dan Gohmane4569942008-05-23 00:36:11 +00001838template<typename InputIterator>
1839InsertValueInst::InsertValueInst(Value *Agg,
1840 Value *Val,
1841 InputIterator IdxBegin,
1842 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001843 const std::string &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001844 Instruction *InsertBefore)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001845 : Instruction(Agg->getType(), InsertValue,
1846 OperandTraits<InsertValueInst>::op_begin(this),
1847 2, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001848 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001849 typename std::iterator_traits<InputIterator>::iterator_category());
1850}
1851template<typename InputIterator>
1852InsertValueInst::InsertValueInst(Value *Agg,
1853 Value *Val,
1854 InputIterator IdxBegin,
1855 InputIterator IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001856 const std::string &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001857 BasicBlock *InsertAtEnd)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001858 : Instruction(Agg->getType(), InsertValue,
1859 OperandTraits<InsertValueInst>::op_begin(this),
1860 2, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001861 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001862 typename std::iterator_traits<InputIterator>::iterator_category());
1863}
1864
Dan Gohman041e2eb2008-05-15 19:50:34 +00001865DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1866
1867//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001868// PHINode Class
1869//===----------------------------------------------------------------------===//
1870
1871// PHINode - The PHINode class is used to represent the magical mystical PHI
1872// node, that can not exist in nature, but can be synthesized in a computer
1873// scientist's overactive imagination.
1874//
1875class PHINode : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001876 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00001877 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1878 /// the number actually in use.
1879 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001880 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00001881 // allocate space for exactly zero operands
1882 void *operator new(size_t s) {
1883 return User::operator new(s, 0);
1884 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001885 explicit PHINode(const Type *Ty, const std::string &NameStr = "",
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001886 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001887 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Chris Lattner454928e2005-01-29 00:31:36 +00001888 ReservedSpace(0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001889 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001890 }
1891
Evan Cheng1bf9a182008-07-24 00:08:56 +00001892 PHINode(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001893 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Chris Lattner454928e2005-01-29 00:31:36 +00001894 ReservedSpace(0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001895 setName(NameStr);
Chris Lattner454928e2005-01-29 00:31:36 +00001896 }
Gabor Greif051a9502008-04-06 20:25:17 +00001897public:
Evan Cheng1bf9a182008-07-24 00:08:56 +00001898 static PHINode *Create(const Type *Ty, const std::string &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001899 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001900 return new PHINode(Ty, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001901 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001902 static PHINode *Create(const Type *Ty, const std::string &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001903 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001904 return new PHINode(Ty, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001905 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001906 ~PHINode();
1907
Chris Lattner454928e2005-01-29 00:31:36 +00001908 /// reserveOperandSpace - This method can be used to avoid repeated
1909 /// reallocation of PHI operand lists by reserving space for the correct
1910 /// number of operands before adding them. Unlike normal vector reserves,
1911 /// this method can also be used to trim the operand space.
1912 void reserveOperandSpace(unsigned NumValues) {
1913 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001914 }
1915
Chris Lattnerf319e832004-10-15 23:52:05 +00001916 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001917
Gabor Greifefe65362008-05-10 08:32:32 +00001918 /// Provide fast operand accessors
1919 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1920
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001921 /// getNumIncomingValues - Return the number of incoming edges
1922 ///
Chris Lattner454928e2005-01-29 00:31:36 +00001923 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001924
Reid Spencerc773de62006-05-19 19:07:54 +00001925 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001926 ///
1927 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001928 assert(i*2 < getNumOperands() && "Invalid value number!");
1929 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001930 }
1931 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +00001932 assert(i*2 < getNumOperands() && "Invalid value number!");
1933 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001934 }
Chris Lattner454928e2005-01-29 00:31:36 +00001935 unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001936 return i*2;
1937 }
1938
Reid Spencerc773de62006-05-19 19:07:54 +00001939 /// getIncomingBlock - Return incoming basic block number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001940 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00001941 BasicBlock *getIncomingBlock(unsigned i) const {
Gabor Greifefe65362008-05-10 08:32:32 +00001942 return static_cast<BasicBlock*>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001943 }
1944 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Gabor Greifefe65362008-05-10 08:32:32 +00001945 setOperand(i*2+1, BB);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001946 }
1947 unsigned getOperandNumForIncomingBlock(unsigned i) {
1948 return i*2+1;
1949 }
1950
1951 /// addIncoming - Add an incoming value to the end of the PHI list
1952 ///
1953 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00001954 assert(V && "PHI node got a null value!");
1955 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001956 assert(getType() == V->getType() &&
1957 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +00001958 unsigned OpNo = NumOperands;
1959 if (OpNo+2 > ReservedSpace)
1960 resizeOperands(0); // Get more space!
1961 // Initialize some new operands.
1962 NumOperands = OpNo+2;
Gabor Greif6c80c382008-05-26 21:33:52 +00001963 OperandList[OpNo] = V;
1964 OperandList[OpNo+1] = BB;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001965 }
Misha Brukman9769ab22005-04-21 20:19:05 +00001966
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001967 /// removeIncomingValue - Remove an incoming value. This is useful if a
1968 /// predecessor basic block is deleted. The value removed is returned.
1969 ///
1970 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1971 /// is true), the PHI node is destroyed and any uses of it are replaced with
1972 /// dummy values. The only time there should be zero incoming values to a PHI
1973 /// node is when the block is dead, so this strategy is sound.
1974 ///
1975 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1976
Gabor Greifefe65362008-05-10 08:32:32 +00001977 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001978 int Idx = getBasicBlockIndex(BB);
1979 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1980 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1981 }
1982
Misha Brukman9769ab22005-04-21 20:19:05 +00001983 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001984 /// block in the value list for this PHI. Returns -1 if no instance.
1985 ///
1986 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001987 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00001988 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Gabor Greifefe65362008-05-10 08:32:32 +00001989 if (OL[i+1].get() == BB) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001990 return -1;
1991 }
1992
1993 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1994 return getIncomingValue(getBasicBlockIndex(BB));
1995 }
1996
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001997 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00001998 /// same value, return the value, otherwise return null.
1999 ///
Chris Lattner9acbd612005-08-05 00:49:06 +00002000 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00002001
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002002 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2003 static inline bool classof(const PHINode *) { return true; }
2004 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00002005 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002006 }
2007 static inline bool classof(const Value *V) {
2008 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2009 }
Chris Lattner454928e2005-01-29 00:31:36 +00002010 private:
2011 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002012};
2013
Gabor Greifefe65362008-05-10 08:32:32 +00002014template <>
2015struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
2016};
2017
2018DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2019
2020
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002021//===----------------------------------------------------------------------===//
2022// ReturnInst Class
2023//===----------------------------------------------------------------------===//
2024
2025//===---------------------------------------------------------------------------
2026/// ReturnInst - Return a value (possibly void), from a function. Execution
2027/// does not continue in this function any longer.
2028///
2029class ReturnInst : public TerminatorInst {
Chris Lattner910c80a2007-02-24 00:55:48 +00002030 ReturnInst(const ReturnInst &RI);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002031
Gabor Greif051a9502008-04-06 20:25:17 +00002032private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002033 // ReturnInst constructors:
2034 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002035 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002036 // ReturnInst(Value* X) - 'ret X' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00002037 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002038 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
Gabor Greifefe65362008-05-10 08:32:32 +00002039 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2040 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002041 //
2042 // NOTE: If the Value* passed is of type void then the constructor behaves as
2043 // if it was passed NULL.
Chris Lattner910c80a2007-02-24 00:55:48 +00002044 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
2045 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
2046 explicit ReturnInst(BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002047public:
2048 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
2049 return new(!!retVal) ReturnInst(retVal, InsertBefore);
2050 }
2051 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
2052 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
2053 }
Gabor Greif051a9502008-04-06 20:25:17 +00002054 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
2055 return new(0) ReturnInst(InsertAtEnd);
2056 }
Devang Patel57ef4f42008-02-23 00:35:18 +00002057 virtual ~ReturnInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002058
Chris Lattnerf319e832004-10-15 23:52:05 +00002059 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002060
Gabor Greifefe65362008-05-10 08:32:32 +00002061 /// Provide fast operand accessors
2062 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Devang Patel64d4e612008-02-26 17:56:20 +00002063
Gabor Greifefe65362008-05-10 08:32:32 +00002064 /// Convenience accessor
Devang Patel1eafa062008-03-11 17:35:03 +00002065 Value *getReturnValue(unsigned n = 0) const {
Gabor Greifefe65362008-05-10 08:32:32 +00002066 return n < getNumOperands()
2067 ? getOperand(n)
2068 : 0;
Devang Patel1eafa062008-03-11 17:35:03 +00002069 }
2070
Chris Lattner454928e2005-01-29 00:31:36 +00002071 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002072
2073 // Methods for support type inquiry through isa, cast, and dyn_cast:
2074 static inline bool classof(const ReturnInst *) { return true; }
2075 static inline bool classof(const Instruction *I) {
2076 return (I->getOpcode() == Instruction::Ret);
2077 }
2078 static inline bool classof(const Value *V) {
2079 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2080 }
Chris Lattner454928e2005-01-29 00:31:36 +00002081 private:
2082 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2083 virtual unsigned getNumSuccessorsV() const;
2084 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002085};
2086
Gabor Greifefe65362008-05-10 08:32:32 +00002087template <>
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002088struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
Gabor Greifefe65362008-05-10 08:32:32 +00002089};
2090
2091DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002092
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002093//===----------------------------------------------------------------------===//
2094// BranchInst Class
2095//===----------------------------------------------------------------------===//
2096
2097//===---------------------------------------------------------------------------
2098/// BranchInst - Conditional or Unconditional Branch instruction.
2099///
2100class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00002101 /// Ops list - Branches are strange. The operands are ordered:
2102 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
2103 /// they don't have to check for cond/uncond branchness.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002104 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00002105 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002106 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2107 // BranchInst(BB *B) - 'br B'
2108 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2109 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2110 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2111 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2112 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00002113 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002114 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002115 Instruction *InsertBefore = 0);
2116 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002117 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002118 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002119public:
2120 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2121 return new(1) BranchInst(IfTrue, InsertBefore);
2122 }
Gabor Greifefe65362008-05-10 08:32:32 +00002123 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2124 Value *Cond, Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002125 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2126 }
2127 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2128 return new(1) BranchInst(IfTrue, InsertAtEnd);
2129 }
Gabor Greifefe65362008-05-10 08:32:32 +00002130 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2131 Value *Cond, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002132 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2133 }
Chris Lattner454928e2005-01-29 00:31:36 +00002134
Bill Wendlingd2a5a2a2008-05-10 10:58:07 +00002135 ~BranchInst() {
Gabor Greifefe65362008-05-10 08:32:32 +00002136 if (NumOperands == 1)
Bill Wendlingc2e73532008-05-10 19:59:59 +00002137 NumOperands = (unsigned)((Use*)this - OperandList);
Gabor Greifefe65362008-05-10 08:32:32 +00002138 }
2139
Chris Lattner454928e2005-01-29 00:31:36 +00002140 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00002141 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002142
Chris Lattnerf319e832004-10-15 23:52:05 +00002143 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002144
Devang Patel4d4a5e02008-02-23 01:11:02 +00002145 bool isUnconditional() const { return getNumOperands() == 1; }
2146 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002147
Devang Patel4d4a5e02008-02-23 01:11:02 +00002148 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002149 assert(isConditional() && "Cannot get condition of an uncond branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00002150 return getOperand(2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002151 }
2152
2153 void setCondition(Value *V) {
2154 assert(isConditional() && "Cannot set condition of unconditional branch!");
2155 setOperand(2, V);
2156 }
2157
2158 // setUnconditionalDest - Change the current branch to an unconditional branch
2159 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +00002160 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002161 void setUnconditionalDest(BasicBlock *Dest) {
Gabor Greifefe65362008-05-10 08:32:32 +00002162 Op<0>() = Dest;
Chris Lattner454928e2005-01-29 00:31:36 +00002163 if (isConditional()) { // Convert this to an uncond branch.
Gabor Greifefe65362008-05-10 08:32:32 +00002164 Op<1>().set(0);
2165 Op<2>().set(0);
Chris Lattner454928e2005-01-29 00:31:36 +00002166 NumOperands = 1;
Chris Lattner454928e2005-01-29 00:31:36 +00002167 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002168 }
2169
Chris Lattner454928e2005-01-29 00:31:36 +00002170 unsigned getNumSuccessors() const { return 1+isConditional(); }
2171
2172 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002173 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Dan Gohmanb96039e2007-05-11 20:59:29 +00002174 return cast<BasicBlock>(getOperand(i));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002175 }
2176
Chris Lattner454928e2005-01-29 00:31:36 +00002177 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002178 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifefe65362008-05-10 08:32:32 +00002179 setOperand(idx, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002180 }
2181
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002182 // Methods for support type inquiry through isa, cast, and dyn_cast:
2183 static inline bool classof(const BranchInst *) { return true; }
2184 static inline bool classof(const Instruction *I) {
2185 return (I->getOpcode() == Instruction::Br);
2186 }
2187 static inline bool classof(const Value *V) {
2188 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2189 }
Chris Lattner454928e2005-01-29 00:31:36 +00002190private:
2191 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2192 virtual unsigned getNumSuccessorsV() const;
2193 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002194};
2195
Gabor Greifefe65362008-05-10 08:32:32 +00002196template <>
2197struct OperandTraits<BranchInst> : HungoffOperandTraits<> {
2198 // we need to access operands via OperandList, since
2199 // the NumOperands may change from 3 to 1
2200 static inline void *allocate(unsigned); // FIXME
2201};
2202
2203DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2204
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002205//===----------------------------------------------------------------------===//
2206// SwitchInst Class
2207//===----------------------------------------------------------------------===//
2208
2209//===---------------------------------------------------------------------------
2210/// SwitchInst - Multiway switch
2211///
2212class SwitchInst : public TerminatorInst {
Gabor Greifefe65362008-05-10 08:32:32 +00002213 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00002214 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002215 // Operand[0] = Value to switch on
2216 // Operand[1] = Default basic block destination
2217 // Operand[2n ] = Value to match
2218 // Operand[2n+1] = BasicBlock to go to on match
2219 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00002220 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2221 void resizeOperands(unsigned No);
Gabor Greifefe65362008-05-10 08:32:32 +00002222 // allocate space for exactly zero operands
2223 void *operator new(size_t s) {
2224 return User::operator new(s, 0);
2225 }
Chris Lattner454928e2005-01-29 00:31:36 +00002226 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2227 /// switch on and a default destination. The number of additional cases can
2228 /// be specified here to make memory allocation more efficient. This
2229 /// constructor can also autoinsert before another instruction.
2230 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002231 Instruction *InsertBefore = 0);
2232
Chris Lattner454928e2005-01-29 00:31:36 +00002233 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2234 /// switch on and a default destination. The number of additional cases can
2235 /// be specified here to make memory allocation more efficient. This
2236 /// constructor also autoinserts at the end of the specified BasicBlock.
2237 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002238 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002239public:
Gabor Greifefe65362008-05-10 08:32:32 +00002240 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2241 unsigned NumCases, Instruction *InsertBefore = 0) {
2242 return new SwitchInst(Value, Default, NumCases, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002243 }
Gabor Greifefe65362008-05-10 08:32:32 +00002244 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2245 unsigned NumCases, BasicBlock *InsertAtEnd) {
2246 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002247 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002248 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00002249
Gabor Greifefe65362008-05-10 08:32:32 +00002250 /// Provide fast operand accessors
2251 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2252
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002253 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00002254 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00002255 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00002256
Devang Patel4d4a5e02008-02-23 01:11:02 +00002257 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002258 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002259 }
2260
2261 /// getNumCases - return the number of 'cases' in this switch instruction.
2262 /// Note that case #0 is always the default case.
2263 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002264 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002265 }
2266
2267 /// getCaseValue - Return the specified case value. Note that case #0, the
2268 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002269 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002270 assert(i && i < getNumCases() && "Illegal case value to get!");
2271 return getSuccessorValue(i);
2272 }
2273
2274 /// getCaseValue - Return the specified case value. Note that case #0, the
2275 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002276 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002277 assert(i && i < getNumCases() && "Illegal case value to get!");
2278 return getSuccessorValue(i);
2279 }
2280
2281 /// findCaseValue - Search all of the case values for the specified constant.
2282 /// If it is explicitly handled, return the case number of it, otherwise
2283 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002284 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002285 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2286 if (getCaseValue(i) == C)
2287 return i;
2288 return 0;
2289 }
2290
Nick Lewycky011f1842006-09-18 19:03:59 +00002291 /// findCaseDest - Finds the unique case value for a given successor. Returns
2292 /// null if the successor is not found, not unique, or is the default case.
2293 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00002294 if (BB == getDefaultDest()) return NULL;
2295
Nick Lewycky011f1842006-09-18 19:03:59 +00002296 ConstantInt *CI = NULL;
2297 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2298 if (getSuccessor(i) == BB) {
2299 if (CI) return NULL; // Multiple cases lead to BB.
2300 else CI = getCaseValue(i);
2301 }
2302 }
2303 return CI;
2304 }
2305
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002306 /// addCase - Add an entry to the switch instruction...
2307 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00002308 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002309
2310 /// removeCase - This method removes the specified successor from the switch
2311 /// instruction. Note that this cannot be used to remove the default
2312 /// destination (successor #0).
2313 ///
2314 void removeCase(unsigned idx);
2315
Chris Lattner454928e2005-01-29 00:31:36 +00002316 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002317
Chris Lattner454928e2005-01-29 00:31:36 +00002318 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2319 BasicBlock *getSuccessor(unsigned idx) const {
2320 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2321 return cast<BasicBlock>(getOperand(idx*2+1));
2322 }
2323 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002324 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Gabor Greifefe65362008-05-10 08:32:32 +00002325 setOperand(idx*2+1, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002326 }
2327
2328 // getSuccessorValue - Return the value associated with the specified
2329 // successor.
Devang Patel4d4a5e02008-02-23 01:11:02 +00002330 ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002331 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00002332 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002333 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002334
2335 // Methods for support type inquiry through isa, cast, and dyn_cast:
2336 static inline bool classof(const SwitchInst *) { return true; }
2337 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00002338 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002339 }
2340 static inline bool classof(const Value *V) {
2341 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2342 }
Chris Lattner454928e2005-01-29 00:31:36 +00002343private:
2344 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2345 virtual unsigned getNumSuccessorsV() const;
2346 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002347};
2348
Gabor Greifefe65362008-05-10 08:32:32 +00002349template <>
2350struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2351};
2352
2353DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2354
2355
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002356//===----------------------------------------------------------------------===//
2357// InvokeInst Class
2358//===----------------------------------------------------------------------===//
2359
Chris Lattner3340ffe2005-05-06 20:26:26 +00002360/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2361/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002362///
2363class InvokeInst : public TerminatorInst {
Devang Patel05988662008-09-25 21:00:45 +00002364 AttrListPtr AttributeList;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002365 InvokeInst(const InvokeInst &BI);
2366 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerd2dd1502007-02-13 01:04:01 +00002367 Value* const *Args, unsigned NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00002368
2369 template<typename InputIterator>
2370 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2371 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002372 const std::string &NameStr,
David Greenef1355a52007-08-27 19:04:21 +00002373 // This argument ensures that we have an iterator we can
2374 // do arithmetic on in constant time
2375 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002376 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greenef1355a52007-08-27 19:04:21 +00002377
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002378 // This requires that the iterator points to contiguous memory.
2379 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
Evan Cheng1bf9a182008-07-24 00:08:56 +00002380 setName(NameStr);
David Greenef1355a52007-08-27 19:04:21 +00002381 }
2382
David Greenef1355a52007-08-27 19:04:21 +00002383 /// Construct an InvokeInst given a range of arguments.
2384 /// InputIterator must be a random-access iterator pointing to
2385 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2386 /// made for random-accessness but not for contiguous storage as
2387 /// that would incur runtime overhead.
2388 ///
2389 /// @brief Construct an InvokeInst from a range of arguments
2390 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002391 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2392 InputIterator ArgBegin, InputIterator ArgEnd,
2393 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002394 const std::string &NameStr, Instruction *InsertBefore);
David Greenef1355a52007-08-27 19:04:21 +00002395
2396 /// Construct an InvokeInst given a range of arguments.
2397 /// InputIterator must be a random-access iterator pointing to
2398 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2399 /// made for random-accessness but not for contiguous storage as
2400 /// that would incur runtime overhead.
2401 ///
2402 /// @brief Construct an InvokeInst from a range of arguments
2403 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002404 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2405 InputIterator ArgBegin, InputIterator ArgEnd,
2406 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002407 const std::string &NameStr, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002408public:
2409 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002410 static InvokeInst *Create(Value *Func,
2411 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002412 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002413 const std::string &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00002414 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +00002415 unsigned Values(ArgEnd - ArgBegin + 3);
2416 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002417 Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002418 }
2419 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002420 static InvokeInst *Create(Value *Func,
2421 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002422 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002423 const std::string &NameStr,
2424 BasicBlock *InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00002425 unsigned Values(ArgEnd - ArgBegin + 3);
2426 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002427 Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002428 }
David Greenef1355a52007-08-27 19:04:21 +00002429
Chris Lattnerf319e832004-10-15 23:52:05 +00002430 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002431
Gabor Greifefe65362008-05-10 08:32:32 +00002432 /// Provide fast operand accessors
2433 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2434
Chris Lattner3340ffe2005-05-06 20:26:26 +00002435 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2436 /// function call.
2437 unsigned getCallingConv() const { return SubclassData; }
2438 void setCallingConv(unsigned CC) {
2439 SubclassData = CC;
2440 }
2441
Devang Patel05988662008-09-25 21:00:45 +00002442 /// getAttributes - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002443 ///
Devang Patel05988662008-09-25 21:00:45 +00002444 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002445
Devang Patel05988662008-09-25 21:00:45 +00002446 /// setAttributes - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002447 ///
Devang Patel05988662008-09-25 21:00:45 +00002448 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00002449
Devang Patel05988662008-09-25 21:00:45 +00002450 /// addAttribute - adds the attribute to the list of attributes.
2451 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002452
Devang Patel05988662008-09-25 21:00:45 +00002453 /// removeAttribute - removes the attribute from the list of attributes.
2454 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00002455
Dan Gohmanf2752502008-09-26 21:38:45 +00002456 /// @brief Determine whether the call or the callee has the given attribute.
2457 bool paramHasAttr(unsigned i, Attributes attr) const;
2458
Dale Johannesen08e78b12008-02-22 17:49:45 +00002459 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002460 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00002461 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002462 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00002463
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002464 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002465 bool doesNotAccessMemory() const {
Devang Patel05988662008-09-25 21:00:45 +00002466 return paramHasAttr(0, Attribute::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002467 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002468 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002469 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2470 else removeAttribute(~0, Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00002471 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002472
2473 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002474 bool onlyReadsMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00002475 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002476 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002477 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002478 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2479 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00002480 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002481
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002482 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002483 bool doesNotReturn() const {
Devang Patel19c87462008-09-26 22:53:05 +00002484 return paramHasAttr(~0, Attribute::NoReturn);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002485 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002486 void setDoesNotReturn(bool DoesNotReturn = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002487 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2488 else removeAttribute(~0, Attribute::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00002489 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002490
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002491 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002492 bool doesNotThrow() const {
Devang Patel19c87462008-09-26 22:53:05 +00002493 return paramHasAttr(~0, Attribute::NoUnwind);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002494 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002495 void setDoesNotThrow(bool DoesNotThrow = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002496 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2497 else removeAttribute(~0, Attribute::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00002498 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002499
Devang Patel41e23972008-03-03 21:46:28 +00002500 /// @brief Determine if the call returns a structure through first
2501 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002502 bool hasStructRetAttr() const {
2503 // Be friendly and also check the callee.
Devang Patel05988662008-09-25 21:00:45 +00002504 return paramHasAttr(1, Attribute::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002505 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002506
Dan Gohmanf2752502008-09-26 21:38:45 +00002507 /// @brief Determine if any call argument is an aggregate passed by value.
2508 bool hasByValArgument() const {
2509 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2510 }
2511
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002512 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00002513 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002514 ///
Chris Lattner721aef62004-11-18 17:46:57 +00002515 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002516 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00002517 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002518
Dan Gohmanf2752502008-09-26 21:38:45 +00002519 /// getCalledValue - Get a pointer to the function that is invoked by this
2520 /// instruction
2521 const Value *getCalledValue() const { return getOperand(0); }
2522 Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002523
2524 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00002525 BasicBlock *getNormalDest() const {
2526 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002527 }
Chris Lattner454928e2005-01-29 00:31:36 +00002528 BasicBlock *getUnwindDest() const {
2529 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002530 }
Chris Lattner454928e2005-01-29 00:31:36 +00002531 void setNormalDest(BasicBlock *B) {
Gabor Greifefe65362008-05-10 08:32:32 +00002532 setOperand(1, B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002533 }
2534
Chris Lattner454928e2005-01-29 00:31:36 +00002535 void setUnwindDest(BasicBlock *B) {
Gabor Greifefe65362008-05-10 08:32:32 +00002536 setOperand(2, B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002537 }
2538
Devang Patel4d4a5e02008-02-23 01:11:02 +00002539 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002540 assert(i < 2 && "Successor # out of range for invoke!");
2541 return i == 0 ? getNormalDest() : getUnwindDest();
2542 }
2543
Chris Lattner454928e2005-01-29 00:31:36 +00002544 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002545 assert(idx < 2 && "Successor # out of range for invoke!");
Gabor Greifefe65362008-05-10 08:32:32 +00002546 setOperand(idx+1, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002547 }
2548
Chris Lattner454928e2005-01-29 00:31:36 +00002549 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002550
2551 // Methods for support type inquiry through isa, cast, and dyn_cast:
2552 static inline bool classof(const InvokeInst *) { return true; }
2553 static inline bool classof(const Instruction *I) {
2554 return (I->getOpcode() == Instruction::Invoke);
2555 }
2556 static inline bool classof(const Value *V) {
2557 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2558 }
Chris Lattner454928e2005-01-29 00:31:36 +00002559private:
2560 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2561 virtual unsigned getNumSuccessorsV() const;
2562 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002563};
2564
Gabor Greifefe65362008-05-10 08:32:32 +00002565template <>
2566struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2567};
2568
2569template<typename InputIterator>
2570InvokeInst::InvokeInst(Value *Func,
2571 BasicBlock *IfNormal, BasicBlock *IfException,
2572 InputIterator ArgBegin, InputIterator ArgEnd,
2573 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002574 const std::string &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00002575 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2576 ->getElementType())->getReturnType(),
2577 Instruction::Invoke,
2578 OperandTraits<InvokeInst>::op_end(this) - Values,
2579 Values, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002580 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00002581 typename std::iterator_traits<InputIterator>::iterator_category());
2582}
2583template<typename InputIterator>
2584InvokeInst::InvokeInst(Value *Func,
2585 BasicBlock *IfNormal, BasicBlock *IfException,
2586 InputIterator ArgBegin, InputIterator ArgEnd,
2587 unsigned Values,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002588 const std::string &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00002589 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2590 ->getElementType())->getReturnType(),
2591 Instruction::Invoke,
2592 OperandTraits<InvokeInst>::op_end(this) - Values,
2593 Values, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002594 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00002595 typename std::iterator_traits<InputIterator>::iterator_category());
2596}
2597
2598DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002599
2600//===----------------------------------------------------------------------===//
2601// UnwindInst Class
2602//===----------------------------------------------------------------------===//
2603
2604//===---------------------------------------------------------------------------
2605/// UnwindInst - Immediately exit the current function, unwinding the stack
2606/// until an invoke instruction is found.
2607///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002608class UnwindInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002609 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002610public:
Gabor Greif051a9502008-04-06 20:25:17 +00002611 // allocate space for exactly zero operands
2612 void *operator new(size_t s) {
2613 return User::operator new(s, 0);
2614 }
Chris Lattner910c80a2007-02-24 00:55:48 +00002615 explicit UnwindInst(Instruction *InsertBefore = 0);
2616 explicit UnwindInst(BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002617
Chris Lattnerf319e832004-10-15 23:52:05 +00002618 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002619
Chris Lattner454928e2005-01-29 00:31:36 +00002620 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002621
2622 // Methods for support type inquiry through isa, cast, and dyn_cast:
2623 static inline bool classof(const UnwindInst *) { return true; }
2624 static inline bool classof(const Instruction *I) {
2625 return I->getOpcode() == Instruction::Unwind;
2626 }
2627 static inline bool classof(const Value *V) {
2628 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2629 }
Chris Lattner454928e2005-01-29 00:31:36 +00002630private:
2631 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2632 virtual unsigned getNumSuccessorsV() const;
2633 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002634};
2635
Chris Lattner076b3f12004-10-16 18:05:54 +00002636//===----------------------------------------------------------------------===//
2637// UnreachableInst Class
2638//===----------------------------------------------------------------------===//
2639
2640//===---------------------------------------------------------------------------
2641/// UnreachableInst - This function has undefined behavior. In particular, the
2642/// presence of this instruction indicates some higher level knowledge that the
2643/// end of the block cannot be reached.
2644///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002645class UnreachableInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002646 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002647public:
Gabor Greif051a9502008-04-06 20:25:17 +00002648 // allocate space for exactly zero operands
2649 void *operator new(size_t s) {
2650 return User::operator new(s, 0);
2651 }
Chris Lattner910c80a2007-02-24 00:55:48 +00002652 explicit UnreachableInst(Instruction *InsertBefore = 0);
2653 explicit UnreachableInst(BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00002654
2655 virtual UnreachableInst *clone() const;
2656
Chris Lattner454928e2005-01-29 00:31:36 +00002657 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00002658
2659 // Methods for support type inquiry through isa, cast, and dyn_cast:
2660 static inline bool classof(const UnreachableInst *) { return true; }
2661 static inline bool classof(const Instruction *I) {
2662 return I->getOpcode() == Instruction::Unreachable;
2663 }
2664 static inline bool classof(const Value *V) {
2665 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2666 }
Chris Lattner454928e2005-01-29 00:31:36 +00002667private:
2668 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2669 virtual unsigned getNumSuccessorsV() const;
2670 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00002671};
2672
Reid Spencer3da59db2006-11-27 01:05:10 +00002673//===----------------------------------------------------------------------===//
2674// TruncInst Class
2675//===----------------------------------------------------------------------===//
2676
2677/// @brief This class represents a truncation of integer types.
2678class TruncInst : public CastInst {
2679 /// Private copy constructor
2680 TruncInst(const TruncInst &CI)
2681 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2682 }
2683public:
2684 /// @brief Constructor with insert-before-instruction semantics
2685 TruncInst(
2686 Value *S, ///< The value to be truncated
2687 const Type *Ty, ///< The (smaller) type to truncate to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002688 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002689 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2690 );
2691
2692 /// @brief Constructor with insert-at-end-of-block semantics
2693 TruncInst(
2694 Value *S, ///< The value to be truncated
2695 const Type *Ty, ///< The (smaller) type to truncate to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002696 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002697 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2698 );
2699
2700 /// @brief Clone an identical TruncInst
2701 virtual CastInst *clone() const;
2702
2703 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2704 static inline bool classof(const TruncInst *) { return true; }
2705 static inline bool classof(const Instruction *I) {
2706 return I->getOpcode() == Trunc;
2707 }
2708 static inline bool classof(const Value *V) {
2709 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2710 }
2711};
2712
2713//===----------------------------------------------------------------------===//
2714// ZExtInst Class
2715//===----------------------------------------------------------------------===//
2716
2717/// @brief This class represents zero extension of integer types.
2718class ZExtInst : public CastInst {
2719 /// @brief Private copy constructor
2720 ZExtInst(const ZExtInst &CI)
2721 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2722 }
2723public:
2724 /// @brief Constructor with insert-before-instruction semantics
2725 ZExtInst(
2726 Value *S, ///< The value to be zero extended
2727 const Type *Ty, ///< The type to zero extend to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002728 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002729 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2730 );
2731
2732 /// @brief Constructor with insert-at-end semantics.
2733 ZExtInst(
2734 Value *S, ///< The value to be zero extended
2735 const Type *Ty, ///< The type to zero extend to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002736 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002737 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2738 );
2739
2740 /// @brief Clone an identical ZExtInst
2741 virtual CastInst *clone() const;
2742
2743 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2744 static inline bool classof(const ZExtInst *) { return true; }
2745 static inline bool classof(const Instruction *I) {
2746 return I->getOpcode() == ZExt;
2747 }
2748 static inline bool classof(const Value *V) {
2749 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2750 }
2751};
2752
2753//===----------------------------------------------------------------------===//
2754// SExtInst Class
2755//===----------------------------------------------------------------------===//
2756
2757/// @brief This class represents a sign extension of integer types.
2758class SExtInst : public CastInst {
2759 /// @brief Private copy constructor
2760 SExtInst(const SExtInst &CI)
2761 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2762 }
2763public:
2764 /// @brief Constructor with insert-before-instruction semantics
2765 SExtInst(
2766 Value *S, ///< The value to be sign extended
2767 const Type *Ty, ///< The type to sign extend to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002768 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002769 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2770 );
2771
2772 /// @brief Constructor with insert-at-end-of-block semantics
2773 SExtInst(
2774 Value *S, ///< The value to be sign extended
2775 const Type *Ty, ///< The type to sign extend to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002776 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002777 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2778 );
2779
2780 /// @brief Clone an identical SExtInst
2781 virtual CastInst *clone() const;
2782
2783 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2784 static inline bool classof(const SExtInst *) { return true; }
2785 static inline bool classof(const Instruction *I) {
2786 return I->getOpcode() == SExt;
2787 }
2788 static inline bool classof(const Value *V) {
2789 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2790 }
2791};
2792
2793//===----------------------------------------------------------------------===//
2794// FPTruncInst Class
2795//===----------------------------------------------------------------------===//
2796
2797/// @brief This class represents a truncation of floating point types.
2798class FPTruncInst : public CastInst {
2799 FPTruncInst(const FPTruncInst &CI)
2800 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2801 }
2802public:
2803 /// @brief Constructor with insert-before-instruction semantics
2804 FPTruncInst(
2805 Value *S, ///< The value to be truncated
2806 const Type *Ty, ///< The type to truncate to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002807 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002808 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2809 );
2810
2811 /// @brief Constructor with insert-before-instruction semantics
2812 FPTruncInst(
2813 Value *S, ///< The value to be truncated
2814 const Type *Ty, ///< The type to truncate to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002815 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002816 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2817 );
2818
2819 /// @brief Clone an identical FPTruncInst
2820 virtual CastInst *clone() const;
2821
2822 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2823 static inline bool classof(const FPTruncInst *) { return true; }
2824 static inline bool classof(const Instruction *I) {
2825 return I->getOpcode() == FPTrunc;
2826 }
2827 static inline bool classof(const Value *V) {
2828 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2829 }
2830};
2831
2832//===----------------------------------------------------------------------===//
2833// FPExtInst Class
2834//===----------------------------------------------------------------------===//
2835
2836/// @brief This class represents an extension of floating point types.
2837class FPExtInst : public CastInst {
2838 FPExtInst(const FPExtInst &CI)
2839 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2840 }
2841public:
2842 /// @brief Constructor with insert-before-instruction semantics
2843 FPExtInst(
2844 Value *S, ///< The value to be extended
2845 const Type *Ty, ///< The type to extend to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002846 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002847 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2848 );
2849
2850 /// @brief Constructor with insert-at-end-of-block semantics
2851 FPExtInst(
2852 Value *S, ///< The value to be extended
2853 const Type *Ty, ///< The type to extend to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002854 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002855 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2856 );
2857
2858 /// @brief Clone an identical FPExtInst
2859 virtual CastInst *clone() const;
2860
2861 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2862 static inline bool classof(const FPExtInst *) { return true; }
2863 static inline bool classof(const Instruction *I) {
2864 return I->getOpcode() == FPExt;
2865 }
2866 static inline bool classof(const Value *V) {
2867 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2868 }
2869};
2870
2871//===----------------------------------------------------------------------===//
2872// UIToFPInst Class
2873//===----------------------------------------------------------------------===//
2874
2875/// @brief This class represents a cast unsigned integer to floating point.
2876class UIToFPInst : public CastInst {
2877 UIToFPInst(const UIToFPInst &CI)
2878 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2879 }
2880public:
2881 /// @brief Constructor with insert-before-instruction semantics
2882 UIToFPInst(
2883 Value *S, ///< The value to be converted
2884 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002885 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002886 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2887 );
2888
2889 /// @brief Constructor with insert-at-end-of-block semantics
2890 UIToFPInst(
2891 Value *S, ///< The value to be converted
2892 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002893 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002894 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2895 );
2896
2897 /// @brief Clone an identical UIToFPInst
2898 virtual CastInst *clone() const;
2899
2900 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2901 static inline bool classof(const UIToFPInst *) { return true; }
2902 static inline bool classof(const Instruction *I) {
2903 return I->getOpcode() == UIToFP;
2904 }
2905 static inline bool classof(const Value *V) {
2906 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2907 }
2908};
2909
2910//===----------------------------------------------------------------------===//
2911// SIToFPInst Class
2912//===----------------------------------------------------------------------===//
2913
2914/// @brief This class represents a cast from signed integer to floating point.
2915class SIToFPInst : public CastInst {
2916 SIToFPInst(const SIToFPInst &CI)
2917 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2918 }
2919public:
2920 /// @brief Constructor with insert-before-instruction semantics
2921 SIToFPInst(
2922 Value *S, ///< The value to be converted
2923 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002924 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002925 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2926 );
2927
2928 /// @brief Constructor with insert-at-end-of-block semantics
2929 SIToFPInst(
2930 Value *S, ///< The value to be converted
2931 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002932 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002933 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2934 );
2935
2936 /// @brief Clone an identical SIToFPInst
2937 virtual CastInst *clone() const;
2938
2939 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2940 static inline bool classof(const SIToFPInst *) { return true; }
2941 static inline bool classof(const Instruction *I) {
2942 return I->getOpcode() == SIToFP;
2943 }
2944 static inline bool classof(const Value *V) {
2945 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2946 }
2947};
2948
2949//===----------------------------------------------------------------------===//
2950// FPToUIInst Class
2951//===----------------------------------------------------------------------===//
2952
2953/// @brief This class represents a cast from floating point to unsigned integer
2954class FPToUIInst : public CastInst {
2955 FPToUIInst(const FPToUIInst &CI)
2956 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2957 }
2958public:
2959 /// @brief Constructor with insert-before-instruction semantics
2960 FPToUIInst(
2961 Value *S, ///< The value to be converted
2962 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002963 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002964 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2965 );
2966
2967 /// @brief Constructor with insert-at-end-of-block semantics
2968 FPToUIInst(
2969 Value *S, ///< The value to be converted
2970 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00002971 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002972 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2973 );
2974
2975 /// @brief Clone an identical FPToUIInst
2976 virtual CastInst *clone() const;
2977
2978 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2979 static inline bool classof(const FPToUIInst *) { return true; }
2980 static inline bool classof(const Instruction *I) {
2981 return I->getOpcode() == FPToUI;
2982 }
2983 static inline bool classof(const Value *V) {
2984 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2985 }
2986};
2987
2988//===----------------------------------------------------------------------===//
2989// FPToSIInst Class
2990//===----------------------------------------------------------------------===//
2991
2992/// @brief This class represents a cast from floating point to signed integer.
2993class FPToSIInst : public CastInst {
2994 FPToSIInst(const FPToSIInst &CI)
2995 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2996 }
2997public:
2998 /// @brief Constructor with insert-before-instruction semantics
2999 FPToSIInst(
3000 Value *S, ///< The value to be converted
3001 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00003002 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003003 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3004 );
3005
3006 /// @brief Constructor with insert-at-end-of-block semantics
3007 FPToSIInst(
3008 Value *S, ///< The value to be converted
3009 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00003010 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003011 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3012 );
3013
3014 /// @brief Clone an identical FPToSIInst
3015 virtual CastInst *clone() const;
3016
3017 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3018 static inline bool classof(const FPToSIInst *) { return true; }
3019 static inline bool classof(const Instruction *I) {
3020 return I->getOpcode() == FPToSI;
3021 }
3022 static inline bool classof(const Value *V) {
3023 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3024 }
3025};
3026
3027//===----------------------------------------------------------------------===//
3028// IntToPtrInst Class
3029//===----------------------------------------------------------------------===//
3030
3031/// @brief This class represents a cast from an integer to a pointer.
3032class IntToPtrInst : public CastInst {
3033 IntToPtrInst(const IntToPtrInst &CI)
3034 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
3035 }
3036public:
3037 /// @brief Constructor with insert-before-instruction semantics
3038 IntToPtrInst(
3039 Value *S, ///< The value to be converted
3040 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00003041 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003042 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3043 );
3044
3045 /// @brief Constructor with insert-at-end-of-block semantics
3046 IntToPtrInst(
3047 Value *S, ///< The value to be converted
3048 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00003049 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003050 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3051 );
3052
3053 /// @brief Clone an identical IntToPtrInst
3054 virtual CastInst *clone() const;
3055
3056 // Methods for support type inquiry through isa, cast, and dyn_cast:
3057 static inline bool classof(const IntToPtrInst *) { return true; }
3058 static inline bool classof(const Instruction *I) {
3059 return I->getOpcode() == IntToPtr;
3060 }
3061 static inline bool classof(const Value *V) {
3062 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3063 }
3064};
3065
3066//===----------------------------------------------------------------------===//
3067// PtrToIntInst Class
3068//===----------------------------------------------------------------------===//
3069
3070/// @brief This class represents a cast from a pointer to an integer
3071class PtrToIntInst : public CastInst {
3072 PtrToIntInst(const PtrToIntInst &CI)
3073 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3074 }
3075public:
3076 /// @brief Constructor with insert-before-instruction semantics
3077 PtrToIntInst(
3078 Value *S, ///< The value to be converted
3079 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00003080 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003081 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3082 );
3083
3084 /// @brief Constructor with insert-at-end-of-block semantics
3085 PtrToIntInst(
3086 Value *S, ///< The value to be converted
3087 const Type *Ty, ///< The type to convert to
Evan Cheng1bf9a182008-07-24 00:08:56 +00003088 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003089 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3090 );
3091
3092 /// @brief Clone an identical PtrToIntInst
3093 virtual CastInst *clone() const;
3094
3095 // Methods for support type inquiry through isa, cast, and dyn_cast:
3096 static inline bool classof(const PtrToIntInst *) { return true; }
3097 static inline bool classof(const Instruction *I) {
3098 return I->getOpcode() == PtrToInt;
3099 }
3100 static inline bool classof(const Value *V) {
3101 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3102 }
3103};
3104
3105//===----------------------------------------------------------------------===//
3106// BitCastInst Class
3107//===----------------------------------------------------------------------===//
3108
3109/// @brief This class represents a no-op cast from one type to another.
3110class BitCastInst : public CastInst {
3111 BitCastInst(const BitCastInst &CI)
3112 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3113 }
3114public:
3115 /// @brief Constructor with insert-before-instruction semantics
3116 BitCastInst(
3117 Value *S, ///< The value to be casted
3118 const Type *Ty, ///< The type to casted to
Evan Cheng1bf9a182008-07-24 00:08:56 +00003119 const std::string &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003120 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3121 );
3122
3123 /// @brief Constructor with insert-at-end-of-block semantics
3124 BitCastInst(
3125 Value *S, ///< The value to be casted
3126 const Type *Ty, ///< The type to casted to
Evan Cheng1bf9a182008-07-24 00:08:56 +00003127 const std::string &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003128 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3129 );
3130
3131 /// @brief Clone an identical BitCastInst
3132 virtual CastInst *clone() const;
3133
3134 // Methods for support type inquiry through isa, cast, and dyn_cast:
3135 static inline bool classof(const BitCastInst *) { return true; }
3136 static inline bool classof(const Instruction *I) {
3137 return I->getOpcode() == BitCast;
3138 }
3139 static inline bool classof(const Value *V) {
3140 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3141 }
3142};
3143
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003144} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00003145
3146#endif