blob: 9a396aa83ef6c39c60df29d72b719da833614d2b [file] [log] [blame]
Chris Lattnera892a3a2003-01-27 22:08:52 +00001//===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
Misha Brukman9769ab22005-04-21 20:19:05 +00002//
John Criswell6fbcc262003-10-20 20:19:47 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner7ed47a12007-12-29 19:59:42 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman9769ab22005-04-21 20:19:05 +00007//
John Criswell6fbcc262003-10-20 20:19:47 +00008//===----------------------------------------------------------------------===//
Chris Lattnera892a3a2003-01-27 22:08:52 +00009//
10// This file exposes the class definitions of all of the subclasses of the
11// Instruction class. This is meant to be an easy way to get access to all
12// instruction subclasses.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_INSTRUCTIONS_H
17#define LLVM_INSTRUCTIONS_H
18
David Greene52eec542007-08-01 03:43:44 +000019#include <iterator>
20
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000021#include "llvm/InstrTypes.h"
David Greene52eec542007-08-01 03:43:44 +000022#include "llvm/DerivedTypes.h"
Dale Johannesen0d51e7e2008-02-19 21:38:47 +000023#include "llvm/ParameterAttributes.h"
Gabor Greifefe65362008-05-10 08:32:32 +000024#include "llvm/BasicBlock.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000025
26namespace llvm {
27
Chris Lattnerd1a32602005-02-24 05:32:09 +000028class ConstantInt;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000029class PointerType;
Reid Spencer9d6565a2007-02-15 02:26:10 +000030class VectorType;
Reid Spencer3da43842007-02-28 22:00:54 +000031class ConstantRange;
32class APInt;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000033
34//===----------------------------------------------------------------------===//
35// AllocationInst Class
36//===----------------------------------------------------------------------===//
37
38/// AllocationInst - This class is the common base class of MallocInst and
39/// AllocaInst.
40///
Chris Lattner454928e2005-01-29 00:31:36 +000041class AllocationInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000042protected:
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 = "", Instruction *InsertBefore = 0);
Nate Begeman14b05292005-11-05 09:21:28 +000045 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
Misha Brukman91102862005-03-16 03:46:55 +000046 const std::string &Name, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000047public:
Gabor Greif051a9502008-04-06 20:25:17 +000048 // Out of line virtual method, so the vtable, etc. has a home.
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000049 virtual ~AllocationInst();
50
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000051 /// isArrayAllocation - Return true if there is an allocation size parameter
52 /// to the allocation instruction that is not 1.
53 ///
54 bool isArrayAllocation() const;
55
56 /// getArraySize - Get the number of element allocated, for a simple
57 /// allocation of a single element, this will return a constant 1 value.
58 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000059 const Value *getArraySize() const { return getOperand(0); }
60 Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000061
62 /// getType - Overload to return most specific pointer type
63 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000064 const PointerType *getType() const {
Misha Brukman9769ab22005-04-21 20:19:05 +000065 return reinterpret_cast<const PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000066 }
67
68 /// getAllocatedType - Return the type that is being allocated by the
69 /// instruction.
70 ///
71 const Type *getAllocatedType() const;
72
Nate Begeman14b05292005-11-05 09:21:28 +000073 /// getAlignment - Return the alignment of the memory that is being allocated
74 /// by the instruction.
75 ///
Dan Gohman52837072008-03-24 16:55:58 +000076 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
77 void setAlignment(unsigned Align);
Chris Lattnerf56a8db2006-10-03 17:09:12 +000078
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000079 virtual Instruction *clone() const = 0;
80
81 // Methods for support type inquiry through isa, cast, and dyn_cast:
82 static inline bool classof(const AllocationInst *) { return true; }
83 static inline bool classof(const Instruction *I) {
84 return I->getOpcode() == Instruction::Alloca ||
85 I->getOpcode() == Instruction::Malloc;
86 }
87 static inline bool classof(const Value *V) {
88 return isa<Instruction>(V) && classof(cast<Instruction>(V));
89 }
90};
91
92
93//===----------------------------------------------------------------------===//
94// MallocInst Class
95//===----------------------------------------------------------------------===//
96
97/// MallocInst - an instruction to allocated memory on the heap
98///
99class MallocInst : public AllocationInst {
100 MallocInst(const MallocInst &MI);
101public:
102 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
103 const std::string &Name = "",
104 Instruction *InsertBefore = 0)
Nate Begeman14b05292005-11-05 09:21:28 +0000105 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000106 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
107 BasicBlock *InsertAtEnd)
Nate Begeman14b05292005-11-05 09:21:28 +0000108 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000109
110 MallocInst(const Type *Ty, const std::string &Name,
111 Instruction *InsertBefore = 0)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000112 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
113 MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
114 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000115
116 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Nate Begeman14b05292005-11-05 09:21:28 +0000117 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000118 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
119 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Nate Begeman14b05292005-11-05 09:21:28 +0000120 const std::string &Name = "",
121 Instruction *InsertBefore = 0)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000122 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000123
Chris Lattnerf319e832004-10-15 23:52:05 +0000124 virtual MallocInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000125
126 // Methods for support type inquiry through isa, cast, and dyn_cast:
127 static inline bool classof(const MallocInst *) { return true; }
128 static inline bool classof(const Instruction *I) {
129 return (I->getOpcode() == Instruction::Malloc);
130 }
131 static inline bool classof(const Value *V) {
132 return isa<Instruction>(V) && classof(cast<Instruction>(V));
133 }
134};
135
136
137//===----------------------------------------------------------------------===//
138// AllocaInst Class
139//===----------------------------------------------------------------------===//
140
141/// AllocaInst - an instruction to allocate memory on the stack
142///
143class AllocaInst : public AllocationInst {
144 AllocaInst(const AllocaInst &);
145public:
146 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
147 const std::string &Name = "",
148 Instruction *InsertBefore = 0)
Nate Begeman14b05292005-11-05 09:21:28 +0000149 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000150 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
151 BasicBlock *InsertAtEnd)
Nate Begeman14b05292005-11-05 09:21:28 +0000152 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
Chris Lattnerb77780e2006-05-10 04:38:35 +0000153
154 AllocaInst(const Type *Ty, const std::string &Name,
155 Instruction *InsertBefore = 0)
156 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
157 AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
158 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000159
Chris Lattnerb77780e2006-05-10 04:38:35 +0000160 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
161 const std::string &Name = "", Instruction *InsertBefore = 0)
162 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
Nate Begeman14b05292005-11-05 09:21:28 +0000163 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
164 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000165 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000166
Chris Lattnerf319e832004-10-15 23:52:05 +0000167 virtual AllocaInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000168
169 // Methods for support type inquiry through isa, cast, and dyn_cast:
170 static inline bool classof(const AllocaInst *) { return true; }
171 static inline bool classof(const Instruction *I) {
172 return (I->getOpcode() == Instruction::Alloca);
173 }
174 static inline bool classof(const Value *V) {
175 return isa<Instruction>(V) && classof(cast<Instruction>(V));
176 }
177};
178
179
180//===----------------------------------------------------------------------===//
181// FreeInst Class
182//===----------------------------------------------------------------------===//
183
184/// FreeInst - an instruction to deallocate memory
185///
Chris Lattner454928e2005-01-29 00:31:36 +0000186class FreeInst : public UnaryInstruction {
187 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000188public:
189 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
190 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
191
Chris Lattnerf319e832004-10-15 23:52:05 +0000192 virtual FreeInst *clone() const;
Owen Andersonc9edf0b2007-07-06 23:13:31 +0000193
194 // Accessor methods for consistency with other memory operations
195 Value *getPointerOperand() { return getOperand(0); }
196 const Value *getPointerOperand() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000197
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000198 // Methods for support type inquiry through isa, cast, and dyn_cast:
199 static inline bool classof(const FreeInst *) { return true; }
200 static inline bool classof(const Instruction *I) {
201 return (I->getOpcode() == Instruction::Free);
202 }
203 static inline bool classof(const Value *V) {
204 return isa<Instruction>(V) && classof(cast<Instruction>(V));
205 }
206};
207
208
209//===----------------------------------------------------------------------===//
210// LoadInst Class
211//===----------------------------------------------------------------------===//
212
Chris Lattner88fe29a2005-02-05 01:44:18 +0000213/// LoadInst - an instruction for reading from memory. This uses the
214/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000215///
Chris Lattner454928e2005-01-29 00:31:36 +0000216class LoadInst : public UnaryInstruction {
Christopher Lamb43c7f372007-04-22 19:24:39 +0000217
Chris Lattner454928e2005-01-29 00:31:36 +0000218 LoadInst(const LoadInst &LI)
Chris Lattner88fe29a2005-02-05 01:44:18 +0000219 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
220 setVolatile(LI.isVolatile());
Christopher Lamb43c7f372007-04-22 19:24:39 +0000221 setAlignment(LI.getAlignment());
Misha Brukman9769ab22005-04-21 20:19:05 +0000222
Chris Lattner454928e2005-01-29 00:31:36 +0000223#ifndef NDEBUG
224 AssertOK();
225#endif
226 }
227 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000228public:
229 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
230 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000231 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile = false,
232 Instruction *InsertBefore = 0);
233 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
Chris Lattnerf00042a2007-02-13 07:54:42 +0000234 Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000235 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
236 BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000237 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
238 BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000239
Chris Lattnerf00042a2007-02-13 07:54:42 +0000240 LoadInst(Value *Ptr, const char *Name, Instruction *InsertBefore);
241 LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAtEnd);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000242 explicit LoadInst(Value *Ptr, const char *Name = 0, bool isVolatile = false,
Chris Lattnerf00042a2007-02-13 07:54:42 +0000243 Instruction *InsertBefore = 0);
244 LoadInst(Value *Ptr, const char *Name, bool isVolatile,
245 BasicBlock *InsertAtEnd);
246
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000247 /// isVolatile - Return true if this is a load from a volatile memory
248 /// location.
249 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000250 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000251
252 /// setVolatile - Specify whether this is a volatile load or not.
253 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000254 void setVolatile(bool V) {
Hartmut Kaiserefd4a512007-10-17 14:56:40 +0000255 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000256 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000257
Chris Lattnerf319e832004-10-15 23:52:05 +0000258 virtual LoadInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000259
Christopher Lamb43c7f372007-04-22 19:24:39 +0000260 /// getAlignment - Return the alignment of the access that is being performed
261 ///
262 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000263 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000264 }
265
266 void setAlignment(unsigned Align);
267
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000268 Value *getPointerOperand() { return getOperand(0); }
269 const Value *getPointerOperand() const { return getOperand(0); }
270 static unsigned getPointerOperandIndex() { return 0U; }
271
272 // Methods for support type inquiry through isa, cast, and dyn_cast:
273 static inline bool classof(const LoadInst *) { return true; }
274 static inline bool classof(const Instruction *I) {
275 return I->getOpcode() == Instruction::Load;
276 }
277 static inline bool classof(const Value *V) {
278 return isa<Instruction>(V) && classof(cast<Instruction>(V));
279 }
280};
281
282
283//===----------------------------------------------------------------------===//
284// StoreInst Class
285//===----------------------------------------------------------------------===//
286
Misha Brukman9769ab22005-04-21 20:19:05 +0000287/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000288///
289class StoreInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +0000290 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Christopher Lamb43c7f372007-04-22 19:24:39 +0000291
Gabor Greifefe65362008-05-10 08:32:32 +0000292 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
293 &Op<0>(), 2) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000294 Op<0>() = SI.Op<0>();
295 Op<1>() = SI.Op<1>();
Chris Lattner88fe29a2005-02-05 01:44:18 +0000296 setVolatile(SI.isVolatile());
Christopher Lamb43c7f372007-04-22 19:24:39 +0000297 setAlignment(SI.getAlignment());
298
Chris Lattner454928e2005-01-29 00:31:36 +0000299#ifndef NDEBUG
300 AssertOK();
301#endif
302 }
303 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000304public:
Gabor Greif051a9502008-04-06 20:25:17 +0000305 // allocate space for exactly two operands
306 void *operator new(size_t s) {
307 return User::operator new(s, 2);
308 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000309 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
310 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
311 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
312 Instruction *InsertBefore = 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000313 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
314 unsigned Align, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000315 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000316 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
317 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000318
319
320 /// isVolatile - Return true if this is a load from a volatile memory
321 /// location.
322 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000323 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000324
325 /// setVolatile - Specify whether this is a volatile load or not.
326 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000327 void setVolatile(bool V) {
Hartmut Kaiserefd4a512007-10-17 14:56:40 +0000328 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000329 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000330
Chris Lattner454928e2005-01-29 00:31:36 +0000331 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +0000332 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner454928e2005-01-29 00:31:36 +0000333
Christopher Lamb43c7f372007-04-22 19:24:39 +0000334 /// getAlignment - Return the alignment of the access that is being performed
335 ///
336 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000337 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000338 }
339
340 void setAlignment(unsigned Align);
341
Chris Lattnerf319e832004-10-15 23:52:05 +0000342 virtual StoreInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000343
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000344 Value *getPointerOperand() { return getOperand(1); }
345 const Value *getPointerOperand() const { return getOperand(1); }
346 static unsigned getPointerOperandIndex() { return 1U; }
347
348 // Methods for support type inquiry through isa, cast, and dyn_cast:
349 static inline bool classof(const StoreInst *) { return true; }
350 static inline bool classof(const Instruction *I) {
351 return I->getOpcode() == Instruction::Store;
352 }
353 static inline bool classof(const Value *V) {
354 return isa<Instruction>(V) && classof(cast<Instruction>(V));
355 }
356};
357
Gabor Greifefe65362008-05-10 08:32:32 +0000358template <>
359struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
360};
361
362DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000363
364//===----------------------------------------------------------------------===//
365// GetElementPtrInst Class
366//===----------------------------------------------------------------------===//
367
David Greeneb8f74792007-09-04 15:46:09 +0000368// checkType - Simple wrapper function to give a better assertion failure
369// message on bad indexes for a gep instruction.
370//
371static inline const Type *checkType(const Type *Ty) {
372 assert(Ty && "Invalid GetElementPtrInst indices for type!");
373 return Ty;
374}
375
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000376/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
377/// access elements of arrays and structs
378///
379class GetElementPtrInst : public Instruction {
Gabor Greifefe65362008-05-10 08:32:32 +0000380 GetElementPtrInst(const GetElementPtrInst &GEPI);
Chris Lattner6ffbe172007-01-31 19:47:18 +0000381 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx);
Chris Lattner38bacf22005-05-03 05:43:30 +0000382 void init(Value *Ptr, Value *Idx);
David Greeneb8f74792007-09-04 15:46:09 +0000383
384 template<typename InputIterator>
385 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
386 const std::string &Name,
387 // This argument ensures that we have an iterator we can
388 // do arithmetic on in constant time
389 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000390 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
David Greeneb8f74792007-09-04 15:46:09 +0000391
392 if (NumIdx > 0) {
Gabor Greifefe65362008-05-10 08:32:32 +0000393 // This requires that the iterator points to contiguous memory.
394 init(Ptr, &*IdxBegin, NumIdx); // FIXME: for the general case
395 // we have to build an array here
David Greeneb8f74792007-09-04 15:46:09 +0000396 }
397 else {
398 init(Ptr, 0, NumIdx);
399 }
400
401 setName(Name);
402 }
403
404 /// getIndexedType - Returns the type of the element that would be loaded with
405 /// a load instruction with the specified parameters.
406 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000407 /// Null is returned if the indices are invalid for the specified
David Greeneb8f74792007-09-04 15:46:09 +0000408 /// pointer type.
409 ///
410 static const Type *getIndexedType(const Type *Ptr,
Dan Gohman041e2eb2008-05-15 19:50:34 +0000411 Value* const *Idx, unsigned NumIdx);
David Greeneb8f74792007-09-04 15:46:09 +0000412
413 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.
Dan Gohman041e2eb2008-05-15 19:50:34 +0000425 return getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx);
426 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,
438 const std::string &Name,
439 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,
444 const std::string &Name, 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.
Gabor Greifefe65362008-05-10 08:32:32 +0000448 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &Name = "",
449 Instruction *InsertBefore = 0);
Chris Lattner38bacf22005-05-03 05:43:30 +0000450 GetElementPtrInst(Value *Ptr, Value *Idx,
451 const std::string &Name, 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,
456 const std::string &Name = "",
457 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)
461 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, Name, 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,
466 const std::string &Name,
467 BasicBlock *InsertAtEnd) {
468 typename std::iterator_traits<InputIterator>::difference_type Values =
469 1 + std::distance(IdxBegin, IdxEnd);
470 return new(Values)
471 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, Name, 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,
Gabor Greifefe65362008-05-10 08:32:32 +0000477 const std::string &Name = "",
478 Instruction *InsertBefore = 0) {
479 return new(2) GetElementPtrInst(Ptr, Idx, Name, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000480 }
481 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Gabor Greifefe65362008-05-10 08:32:32 +0000482 const std::string &Name,
483 BasicBlock *InsertAtEnd) {
484 return new(2) GetElementPtrInst(Ptr, Idx, Name, 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 }
Chris Lattner38bacf22005-05-03 05:43:30 +0000511 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
Misha Brukman9769ab22005-04-21 20:19:05 +0000512
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000513 inline op_iterator idx_begin() { return op_begin()+1; }
514 inline const_op_iterator idx_begin() const { return op_begin()+1; }
515 inline op_iterator idx_end() { return op_end(); }
516 inline const_op_iterator idx_end() const { return op_end(); }
517
518 Value *getPointerOperand() {
519 return getOperand(0);
520 }
521 const Value *getPointerOperand() const {
522 return getOperand(0);
523 }
524 static unsigned getPointerOperandIndex() {
525 return 0U; // get index for modifying correct operand
526 }
527
Devang Patel4d4a5e02008-02-23 01:11:02 +0000528 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000529 return getNumOperands() - 1;
530 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000531
Devang Patel4d4a5e02008-02-23 01:11:02 +0000532 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000533 return getNumOperands() > 1;
534 }
Chris Lattner6f771d42007-04-14 00:12:57 +0000535
536 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
537 /// zeros. If so, the result pointer and the first operand have the same
538 /// value, just potentially different types.
539 bool hasAllZeroIndices() const;
Chris Lattner6b0974c2007-04-27 20:35:56 +0000540
541 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
542 /// constant integers. If so, the result pointer and the first operand have
543 /// a constant offset between them.
544 bool hasAllConstantIndices() const;
545
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000546
547 // Methods for support type inquiry through isa, cast, and dyn_cast:
548 static inline bool classof(const GetElementPtrInst *) { return true; }
549 static inline bool classof(const Instruction *I) {
550 return (I->getOpcode() == Instruction::GetElementPtr);
551 }
552 static inline bool classof(const Value *V) {
553 return isa<Instruction>(V) && classof(cast<Instruction>(V));
554 }
555};
556
Gabor Greifefe65362008-05-10 08:32:32 +0000557template <>
558struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
559};
560
561template<typename InputIterator>
562GetElementPtrInst::GetElementPtrInst(Value *Ptr,
563 InputIterator IdxBegin,
564 InputIterator IdxEnd,
565 unsigned Values,
566 const std::string &Name,
567 Instruction *InsertBefore)
568 : Instruction(PointerType::get(checkType(
569 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000570 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000571 cast<PointerType>(Ptr->getType())
572 ->getAddressSpace()),
573 GetElementPtr,
574 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
575 Values, InsertBefore) {
576 init(Ptr, IdxBegin, IdxEnd, Name,
577 typename std::iterator_traits<InputIterator>::iterator_category());
578}
579template<typename InputIterator>
580GetElementPtrInst::GetElementPtrInst(Value *Ptr,
581 InputIterator IdxBegin,
582 InputIterator IdxEnd,
583 unsigned Values,
584 const std::string &Name,
585 BasicBlock *InsertAtEnd)
586 : Instruction(PointerType::get(checkType(
587 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000588 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000589 cast<PointerType>(Ptr->getType())
590 ->getAddressSpace()),
591 GetElementPtr,
592 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
593 Values, InsertAtEnd) {
594 init(Ptr, IdxBegin, IdxEnd, Name,
595 typename std::iterator_traits<InputIterator>::iterator_category());
596}
597
598
599DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
600
601
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000602//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000603// ICmpInst Class
604//===----------------------------------------------------------------------===//
605
606/// This instruction compares its operands according to the predicate given
Nate Begemanac80ade2008-05-12 19:01:56 +0000607/// to the constructor. It only operates on integers or pointers. The operands
608/// must be identical types.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000609/// @brief Represent an integer comparison operator.
610class ICmpInst: public CmpInst {
611public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000612 /// @brief Constructor with insert-before-instruction semantics.
613 ICmpInst(
614 Predicate pred, ///< The predicate to use for the comparison
615 Value *LHS, ///< The left-hand-side of the expression
616 Value *RHS, ///< The right-hand-side of the expression
617 const std::string &Name = "", ///< Name of the instruction
618 Instruction *InsertBefore = 0 ///< Where to insert
Nate Begemanac80ade2008-05-12 19:01:56 +0000619 ) : CmpInst(Type::Int1Ty, Instruction::ICmp, pred, LHS, RHS, Name,
620 InsertBefore) {
621 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
622 pred <= CmpInst::LAST_ICMP_PREDICATE &&
623 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000624 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000625 "Both operands to ICmp instruction are not of the same type!");
626 // Check that the operands are the right type
Nate Begeman31cd33a2008-05-14 20:28:31 +0000627 assert((getOperand(0)->getType()->isInteger() ||
628 isa<PointerType>(getOperand(0)->getType())) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000629 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000630 }
631
632 /// @brief Constructor with insert-at-block-end semantics.
633 ICmpInst(
634 Predicate pred, ///< The predicate to use for the comparison
635 Value *LHS, ///< The left-hand-side of the expression
636 Value *RHS, ///< The right-hand-side of the expression
637 const std::string &Name, ///< Name of the instruction
638 BasicBlock *InsertAtEnd ///< Block to insert into.
Nate Begemanac80ade2008-05-12 19:01:56 +0000639 ) : CmpInst(Type::Int1Ty, Instruction::ICmp, pred, LHS, RHS, Name,
640 InsertAtEnd) {
641 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
642 pred <= CmpInst::LAST_ICMP_PREDICATE &&
643 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000644 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000645 "Both operands to ICmp instruction are not of the same type!");
646 // Check that the operands are the right type
Nate Begeman31cd33a2008-05-14 20:28:31 +0000647 assert((getOperand(0)->getType()->isInteger() ||
648 isa<PointerType>(getOperand(0)->getType())) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000649 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000650 }
651
652 /// @brief Return the predicate for this instruction.
653 Predicate getPredicate() const { return Predicate(SubclassData); }
654
Chris Lattnerb769d562007-01-14 19:41:24 +0000655 /// @brief Set the predicate for this instruction to the specified value.
656 void setPredicate(Predicate P) { SubclassData = P; }
657
Reid Spencer45fb3f32006-11-20 01:22:35 +0000658 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
659 /// @returns the inverse predicate for the instruction's current predicate.
660 /// @brief Return the inverse of the instruction's predicate.
661 Predicate getInversePredicate() const {
662 return getInversePredicate(getPredicate());
663 }
664
665 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
666 /// @returns the inverse predicate for predicate provided in \p pred.
667 /// @brief Return the inverse of a given predicate
668 static Predicate getInversePredicate(Predicate pred);
669
670 /// For example, EQ->EQ, SLE->SGE, ULT->UGT, etc.
671 /// @returns the predicate that would be the result of exchanging the two
672 /// operands of the ICmpInst instruction without changing the result
673 /// produced.
674 /// @brief Return the predicate as if the operands were swapped
675 Predicate getSwappedPredicate() const {
676 return getSwappedPredicate(getPredicate());
677 }
678
679 /// This is a static version that you can use without an instruction
680 /// available.
681 /// @brief Return the predicate as if the operands were swapped.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000682 static Predicate getSwappedPredicate(Predicate pred);
683
684 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
685 /// @returns the predicate that would be the result if the operand were
686 /// regarded as signed.
687 /// @brief Return the signed version of the predicate
688 Predicate getSignedPredicate() const {
689 return getSignedPredicate(getPredicate());
690 }
691
692 /// This is a static version that you can use without an instruction.
693 /// @brief Return the signed version of the predicate.
694 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000695
Nick Lewycky4189a532008-01-28 03:48:02 +0000696 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
697 /// @returns the predicate that would be the result if the operand were
698 /// regarded as unsigned.
699 /// @brief Return the unsigned version of the predicate
700 Predicate getUnsignedPredicate() const {
701 return getUnsignedPredicate(getPredicate());
702 }
703
704 /// This is a static version that you can use without an instruction.
705 /// @brief Return the unsigned version of the predicate.
706 static Predicate getUnsignedPredicate(Predicate pred);
707
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000708 /// isEquality - Return true if this predicate is either EQ or NE. This also
709 /// tests for commutativity.
710 static bool isEquality(Predicate P) {
711 return P == ICMP_EQ || P == ICMP_NE;
712 }
713
714 /// isEquality - Return true if this predicate is either EQ or NE. This also
715 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000716 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000717 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000718 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000719
720 /// @returns true if the predicate of this ICmpInst is commutative
721 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000722 bool isCommutative() const { return isEquality(); }
723
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000724 /// isRelational - Return true if the predicate is relational (not EQ or NE).
725 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +0000726 bool isRelational() const {
727 return !isEquality();
728 }
729
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000730 /// isRelational - Return true if the predicate is relational (not EQ or NE).
731 ///
732 static bool isRelational(Predicate P) {
733 return !isEquality(P);
734 }
735
Reid Spencere4d87aa2006-12-23 06:05:41 +0000736 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
737 /// @brief Determine if this instruction's predicate is signed.
Chris Lattner5bda9e42007-09-15 06:51:03 +0000738 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000739
740 /// @returns true if the predicate provided is signed, false otherwise
741 /// @brief Determine if the predicate is signed.
742 static bool isSignedPredicate(Predicate pred);
743
Nick Lewyckyfc1efbb2008-05-17 07:33:39 +0000744 /// @returns true if the specified compare predicate is
745 /// true when both operands are equal...
746 /// @brief Determine if the icmp is true when both operands are equal
747 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
748 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
749 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
750 pred == ICmpInst::ICMP_SLE;
751 }
752
753 /// @returns true if the specified compare instruction is
754 /// true when both operands are equal...
755 /// @brief Determine if the ICmpInst returns true when both operands are equal
756 bool isTrueWhenEqual() {
757 return isTrueWhenEqual(getPredicate());
758 }
759
Reid Spencer3da43842007-02-28 22:00:54 +0000760 /// Initialize a set of values that all satisfy the predicate with C.
761 /// @brief Make a ConstantRange for a relation with a constant value.
762 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
763
Reid Spencer45fb3f32006-11-20 01:22:35 +0000764 /// Exchange the two operands to this instruction in such a way that it does
765 /// not modify the semantics of the instruction. The predicate value may be
766 /// changed to retain the same result if the predicate is order dependent
767 /// (e.g. ult).
768 /// @brief Swap operands and adjust predicate.
769 void swapOperands() {
770 SubclassData = getSwappedPredicate();
Gabor Greif94fb68b2008-05-13 22:51:52 +0000771 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000772 }
773
Chris Lattnercd406fe2007-08-24 20:48:18 +0000774 virtual ICmpInst *clone() const;
775
Reid Spencer45fb3f32006-11-20 01:22:35 +0000776 // Methods for support type inquiry through isa, cast, and dyn_cast:
777 static inline bool classof(const ICmpInst *) { return true; }
778 static inline bool classof(const Instruction *I) {
779 return I->getOpcode() == Instruction::ICmp;
780 }
781 static inline bool classof(const Value *V) {
782 return isa<Instruction>(V) && classof(cast<Instruction>(V));
783 }
784};
785
786//===----------------------------------------------------------------------===//
787// FCmpInst Class
788//===----------------------------------------------------------------------===//
789
790/// This instruction compares its operands according to the predicate given
791/// to the constructor. It only operates on floating point values or packed
792/// vectors of floating point values. The operands must be identical types.
793/// @brief Represents a floating point comparison operator.
794class FCmpInst: public CmpInst {
795public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000796 /// @brief Constructor with insert-before-instruction semantics.
797 FCmpInst(
798 Predicate pred, ///< The predicate to use for the comparison
799 Value *LHS, ///< The left-hand-side of the expression
800 Value *RHS, ///< The right-hand-side of the expression
801 const std::string &Name = "", ///< Name of the instruction
802 Instruction *InsertBefore = 0 ///< Where to insert
Nate Begemanac80ade2008-05-12 19:01:56 +0000803 ) : CmpInst(Type::Int1Ty, Instruction::FCmp, pred, LHS, RHS, Name,
804 InsertBefore) {
805 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
806 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000807 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000808 "Both operands to FCmp instruction are not of the same type!");
809 // Check that the operands are the right type
Nate Begeman31cd33a2008-05-14 20:28:31 +0000810 assert(getOperand(0)->getType()->isFloatingPoint() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000811 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000812 }
813
814 /// @brief Constructor with insert-at-block-end semantics.
815 FCmpInst(
816 Predicate pred, ///< The predicate to use for the comparison
817 Value *LHS, ///< The left-hand-side of the expression
818 Value *RHS, ///< The right-hand-side of the expression
819 const std::string &Name, ///< Name of the instruction
820 BasicBlock *InsertAtEnd ///< Block to insert into.
Nate Begemanac80ade2008-05-12 19:01:56 +0000821 ) : CmpInst(Type::Int1Ty, Instruction::FCmp, pred, LHS, RHS, Name,
822 InsertAtEnd) {
823 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
824 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000825 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000826 "Both operands to FCmp instruction are not of the same type!");
827 // Check that the operands are the right type
Nate Begeman31cd33a2008-05-14 20:28:31 +0000828 assert(getOperand(0)->getType()->isFloatingPoint() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000829 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000830 }
831
832 /// @brief Return the predicate for this instruction.
833 Predicate getPredicate() const { return Predicate(SubclassData); }
834
Chris Lattnerb769d562007-01-14 19:41:24 +0000835 /// @brief Set the predicate for this instruction to the specified value.
836 void setPredicate(Predicate P) { SubclassData = P; }
837
Reid Spencer45fb3f32006-11-20 01:22:35 +0000838 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
839 /// @returns the inverse predicate for the instructions current predicate.
840 /// @brief Return the inverse of the predicate
841 Predicate getInversePredicate() const {
842 return getInversePredicate(getPredicate());
843 }
844
845 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
846 /// @returns the inverse predicate for \p pred.
847 /// @brief Return the inverse of a given predicate
848 static Predicate getInversePredicate(Predicate pred);
849
850 /// For example, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
851 /// @returns the predicate that would be the result of exchanging the two
852 /// operands of the ICmpInst instruction without changing the result
853 /// produced.
854 /// @brief Return the predicate as if the operands were swapped
855 Predicate getSwappedPredicate() const {
856 return getSwappedPredicate(getPredicate());
857 }
858
859 /// This is a static version that you can use without an instruction
860 /// available.
861 /// @brief Return the predicate as if the operands were swapped.
862 static Predicate getSwappedPredicate(Predicate Opcode);
863
864 /// This also tests for commutativity. If isEquality() returns true then
865 /// the predicate is also commutative. Only the equality predicates are
866 /// commutative.
867 /// @returns true if the predicate of this instruction is EQ or NE.
868 /// @brief Determine if this is an equality predicate.
869 bool isEquality() const {
870 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
871 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
872 }
873 bool isCommutative() const { return isEquality(); }
874
875 /// @returns true if the predicate is relational (not EQ or NE).
876 /// @brief Determine if this a relational predicate.
877 bool isRelational() const { return !isEquality(); }
878
879 /// Exchange the two operands to this instruction in such a way that it does
880 /// not modify the semantics of the instruction. The predicate value may be
881 /// changed to retain the same result if the predicate is order dependent
882 /// (e.g. ult).
883 /// @brief Swap operands and adjust predicate.
884 void swapOperands() {
885 SubclassData = getSwappedPredicate();
Gabor Greif94fb68b2008-05-13 22:51:52 +0000886 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000887 }
888
Chris Lattnercd406fe2007-08-24 20:48:18 +0000889 virtual FCmpInst *clone() const;
890
Reid Spencer45fb3f32006-11-20 01:22:35 +0000891 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
892 static inline bool classof(const FCmpInst *) { return true; }
893 static inline bool classof(const Instruction *I) {
894 return I->getOpcode() == Instruction::FCmp;
895 }
896 static inline bool classof(const Value *V) {
897 return isa<Instruction>(V) && classof(cast<Instruction>(V));
898 }
899};
900
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000901//===----------------------------------------------------------------------===//
Nate Begemanac80ade2008-05-12 19:01:56 +0000902// VICmpInst Class
903//===----------------------------------------------------------------------===//
904
905/// This instruction compares its operands according to the predicate given
906/// to the constructor. It only operates on vectors of integers.
907/// The operands must be identical types.
908/// @brief Represents a vector integer comparison operator.
909class VICmpInst: public CmpInst {
910public:
911 /// @brief Constructor with insert-before-instruction semantics.
912 VICmpInst(
913 Predicate pred, ///< The predicate to use for the comparison
914 Value *LHS, ///< The left-hand-side of the expression
915 Value *RHS, ///< The right-hand-side of the expression
916 const std::string &Name = "", ///< Name of the instruction
917 Instruction *InsertBefore = 0 ///< Where to insert
918 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, Name,
919 InsertBefore) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000920 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
921 pred <= CmpInst::LAST_ICMP_PREDICATE &&
922 "Invalid VICmp predicate value");
923 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
924 "Both operands to VICmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000925 }
926
927 /// @brief Constructor with insert-at-block-end semantics.
928 VICmpInst(
929 Predicate pred, ///< The predicate to use for the comparison
930 Value *LHS, ///< The left-hand-side of the expression
931 Value *RHS, ///< The right-hand-side of the expression
932 const std::string &Name, ///< Name of the instruction
933 BasicBlock *InsertAtEnd ///< Block to insert into.
934 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, Name,
935 InsertAtEnd) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000936 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
937 pred <= CmpInst::LAST_ICMP_PREDICATE &&
938 "Invalid VICmp predicate value");
939 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
940 "Both operands to VICmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000941 }
942
943 /// @brief Return the predicate for this instruction.
944 Predicate getPredicate() const { return Predicate(SubclassData); }
945
946 virtual VICmpInst *clone() const;
947
948 // Methods for support type inquiry through isa, cast, and dyn_cast:
949 static inline bool classof(const VICmpInst *) { return true; }
950 static inline bool classof(const Instruction *I) {
951 return I->getOpcode() == Instruction::VICmp;
952 }
953 static inline bool classof(const Value *V) {
954 return isa<Instruction>(V) && classof(cast<Instruction>(V));
955 }
956};
957
958//===----------------------------------------------------------------------===//
959// VFCmpInst Class
960//===----------------------------------------------------------------------===//
961
962/// This instruction compares its operands according to the predicate given
963/// to the constructor. It only operates on vectors of floating point values.
964/// The operands must be identical types.
965/// @brief Represents a vector floating point comparison operator.
966class VFCmpInst: public CmpInst {
967public:
968 /// @brief Constructor with insert-before-instruction semantics.
969 VFCmpInst(
970 Predicate pred, ///< The predicate to use for the comparison
971 Value *LHS, ///< The left-hand-side of the expression
972 Value *RHS, ///< The right-hand-side of the expression
973 const std::string &Name = "", ///< Name of the instruction
974 Instruction *InsertBefore = 0 ///< Where to insert
975 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
976 Instruction::VFCmp, pred, LHS, RHS, Name, InsertBefore) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000977 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
978 "Invalid VFCmp predicate value");
979 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
980 "Both operands to VFCmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000981 }
982
983 /// @brief Constructor with insert-at-block-end semantics.
984 VFCmpInst(
985 Predicate pred, ///< The predicate to use for the comparison
986 Value *LHS, ///< The left-hand-side of the expression
987 Value *RHS, ///< The right-hand-side of the expression
988 const std::string &Name, ///< Name of the instruction
989 BasicBlock *InsertAtEnd ///< Block to insert into.
990 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
991 Instruction::VFCmp, pred, LHS, RHS, Name, InsertAtEnd) {
Nate Begeman31cd33a2008-05-14 20:28:31 +0000992 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
993 "Invalid VFCmp predicate value");
994 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
995 "Both operands to VFCmp instruction are not of the same type!");
Nate Begemanac80ade2008-05-12 19:01:56 +0000996 }
997
998 /// @brief Return the predicate for this instruction.
999 Predicate getPredicate() const { return Predicate(SubclassData); }
1000
1001 virtual VFCmpInst *clone() const;
1002
1003 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1004 static inline bool classof(const VFCmpInst *) { return true; }
1005 static inline bool classof(const Instruction *I) {
1006 return I->getOpcode() == Instruction::VFCmp;
1007 }
1008 static inline bool classof(const Value *V) {
1009 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1010 }
1011};
1012
1013//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001014// CallInst Class
1015//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001016/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +00001017/// machine's calling convention. This class uses low bit of the SubClassData
1018/// field to indicate whether or not this is a tail call. The rest of the bits
1019/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001020///
David Greene52eec542007-08-01 03:43:44 +00001021
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001022class CallInst : public Instruction {
Chris Lattner58d74912008-03-12 17:45:29 +00001023 PAListPtr ParamAttrs; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001024 CallInst(const CallInst &CI);
Chris Lattnerd54f4322007-02-13 00:58:44 +00001025 void init(Value *Func, Value* const *Params, unsigned NumParams);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001026 void init(Value *Func, Value *Actual1, Value *Actual2);
1027 void init(Value *Func, Value *Actual);
1028 void init(Value *Func);
1029
David Greene52eec542007-08-01 03:43:44 +00001030 template<typename InputIterator>
1031 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1032 const std::string &Name,
1033 // This argument ensures that we have an iterator we can
1034 // do arithmetic on in constant time
1035 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00001036 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greene52eec542007-08-01 03:43:44 +00001037
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00001038 // This requires that the iterator points to contiguous memory.
1039 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
David Greene52eec542007-08-01 03:43:44 +00001040 setName(Name);
1041 }
1042
David Greene52eec542007-08-01 03:43:44 +00001043 /// Construct a CallInst given a range of arguments. InputIterator
1044 /// must be a random-access iterator pointing to contiguous storage
1045 /// (e.g. a std::vector<>::iterator). Checks are made for
1046 /// random-accessness but not for contiguous storage as that would
1047 /// incur runtime overhead.
1048 /// @brief Construct a CallInst from a range of arguments
1049 template<typename InputIterator>
1050 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Gabor Greifefe65362008-05-10 08:32:32 +00001051 const std::string &Name, Instruction *InsertBefore);
David Greene52eec542007-08-01 03:43:44 +00001052
1053 /// Construct a CallInst given a range of arguments. InputIterator
1054 /// must be a random-access iterator pointing to contiguous storage
1055 /// (e.g. a std::vector<>::iterator). Checks are made for
1056 /// random-accessness but not for contiguous storage as that would
1057 /// incur runtime overhead.
1058 /// @brief Construct a CallInst from a range of arguments
1059 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00001060 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1061 const std::string &Name, BasicBlock *InsertAtEnd);
David Greene52eec542007-08-01 03:43:44 +00001062
Gabor Greifefe65362008-05-10 08:32:32 +00001063 CallInst(Value *F, Value *Actual, const std::string& Name,
1064 Instruction *InsertBefore);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001065 CallInst(Value *F, Value *Actual, const std::string& Name,
1066 BasicBlock *InsertAtEnd);
Gabor Greifefe65362008-05-10 08:32:32 +00001067 explicit CallInst(Value *F, const std::string &Name,
1068 Instruction *InsertBefore);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001069 CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001070public:
1071 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00001072 static CallInst *Create(Value *Func,
1073 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001074 const std::string &Name = "",
1075 Instruction *InsertBefore = 0) {
Bill Wendlingc2e73532008-05-10 19:59:59 +00001076 return new((unsigned)(ArgEnd - ArgBegin + 1))
Evan Chengd69bb1a2008-05-05 17:41:03 +00001077 CallInst(Func, ArgBegin, ArgEnd, Name, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001078 }
1079 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00001080 static CallInst *Create(Value *Func,
1081 InputIterator ArgBegin, InputIterator ArgEnd,
1082 const std::string &Name, BasicBlock *InsertAtEnd) {
Bill Wendlingc2e73532008-05-10 19:59:59 +00001083 return new((unsigned)(ArgEnd - ArgBegin + 1))
Evan Chengd69bb1a2008-05-05 17:41:03 +00001084 CallInst(Func, ArgBegin, ArgEnd, Name, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001085 }
1086 static CallInst *Create(Value *F, Value *Actual, const std::string& Name = "",
1087 Instruction *InsertBefore = 0) {
1088 return new(2) CallInst(F, Actual, Name, InsertBefore);
1089 }
1090 static CallInst *Create(Value *F, Value *Actual, const std::string& Name,
1091 BasicBlock *InsertAtEnd) {
1092 return new(2) CallInst(F, Actual, Name, InsertAtEnd);
1093 }
1094 static CallInst *Create(Value *F, const std::string &Name = "",
1095 Instruction *InsertBefore = 0) {
1096 return new(1) CallInst(F, Name, InsertBefore);
1097 }
Evan Cheng34cd4a42008-05-05 18:30:58 +00001098 static CallInst *Create(Value *F, const std::string &Name,
1099 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001100 return new(1) CallInst(F, Name, InsertAtEnd);
1101 }
1102
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001103 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001104
Chris Lattnerf319e832004-10-15 23:52:05 +00001105 virtual CallInst *clone() const;
Gabor Greifefe65362008-05-10 08:32:32 +00001106
1107 /// Provide fast operand accessors
1108 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattnerbb5493d2007-02-15 23:15:00 +00001109
Chris Lattner3340ffe2005-05-06 20:26:26 +00001110 bool isTailCall() const { return SubclassData & 1; }
1111 void setTailCall(bool isTailCall = true) {
Jeff Cohen39cef602005-05-07 02:44:04 +00001112 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
Chris Lattner3340ffe2005-05-06 20:26:26 +00001113 }
1114
1115 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1116 /// function call.
1117 unsigned getCallingConv() const { return SubclassData >> 1; }
1118 void setCallingConv(unsigned CC) {
1119 SubclassData = (SubclassData & 1) | (CC << 1);
1120 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00001121
Chris Lattner041221c2008-03-13 04:33:03 +00001122 /// getParamAttrs - Return the parameter attributes for this call.
1123 ///
Chris Lattner58d74912008-03-12 17:45:29 +00001124 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001125
Chris Lattner041221c2008-03-13 04:33:03 +00001126 /// setParamAttrs - Sets the parameter attributes for this call.
Chris Lattner58d74912008-03-12 17:45:29 +00001127 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
Eric Christopher0bf7b412008-05-16 20:39:43 +00001128
1129 /// addParamAttr - adds the attribute to the list of attributes.
1130 void addParamAttr(unsigned i, ParameterAttributes attr);
Duncan Sandsdc024672007-11-27 13:23:08 +00001131
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001132 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001133 bool paramHasAttr(unsigned i, unsigned attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001134
Dale Johannesen08e78b12008-02-22 17:49:45 +00001135 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001136 unsigned getParamAlignment(unsigned i) const {
1137 return ParamAttrs.getParamAlignment(i);
1138 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001139
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001140 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001141 bool doesNotAccessMemory() const {
1142 return paramHasAttr(0, ParamAttr::ReadNone);
1143 }
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001144
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001145 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001146 bool onlyReadsMemory() const {
1147 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
1148 }
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001149
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001150 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001151 bool doesNotReturn() const {
1152 return paramHasAttr(0, ParamAttr::NoReturn);
1153 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001154
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001155 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001156 bool doesNotThrow() const {
1157 return paramHasAttr(0, ParamAttr::NoUnwind);
1158 }
Duncan Sandsf0c33542007-12-19 21:13:37 +00001159 void setDoesNotThrow(bool doesNotThrow = true);
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001160
Devang Patel41e23972008-03-03 21:46:28 +00001161 /// @brief Determine if the call returns a structure through first
1162 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001163 bool hasStructRetAttr() const {
1164 // Be friendly and also check the callee.
1165 return paramHasAttr(1, ParamAttr::StructRet);
1166 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001167
Evan Chengf4a54982008-01-12 18:57:32 +00001168 /// @brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001169 bool hasByValArgument() const {
1170 return ParamAttrs.hasAttrSomewhere(ParamAttr::ByVal);
1171 }
Evan Chengf4a54982008-01-12 18:57:32 +00001172
Chris Lattner721aef62004-11-18 17:46:57 +00001173 /// getCalledFunction - Return the function being called by this instruction
1174 /// if it is a direct call. If it is a call through a function pointer,
1175 /// return null.
1176 Function *getCalledFunction() const {
Dan Gohman11a7dbf2007-09-24 15:46:02 +00001177 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001178 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001179
Reid Spencerc25ec252006-12-29 04:10:59 +00001180 /// getCalledValue - Get a pointer to the function that is invoked by this
1181 /// instruction
Devang Patel4d4a5e02008-02-23 01:11:02 +00001182 const Value *getCalledValue() const { return getOperand(0); }
1183 Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001184
1185 // Methods for support type inquiry through isa, cast, and dyn_cast:
1186 static inline bool classof(const CallInst *) { return true; }
1187 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001188 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001189 }
1190 static inline bool classof(const Value *V) {
1191 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1192 }
1193};
1194
Gabor Greifefe65362008-05-10 08:32:32 +00001195template <>
1196struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1197};
1198
1199template<typename InputIterator>
1200CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1201 const std::string &Name, BasicBlock *InsertAtEnd)
1202 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1203 ->getElementType())->getReturnType(),
1204 Instruction::Call,
1205 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001206 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00001207 init(Func, ArgBegin, ArgEnd, Name,
1208 typename std::iterator_traits<InputIterator>::iterator_category());
1209}
1210
1211template<typename InputIterator>
1212CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1213 const std::string &Name, Instruction *InsertBefore)
1214 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1215 ->getElementType())->getReturnType(),
1216 Instruction::Call,
1217 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001218 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
Gabor Greifefe65362008-05-10 08:32:32 +00001219 init(Func, ArgBegin, ArgEnd, Name,
1220 typename std::iterator_traits<InputIterator>::iterator_category());
1221}
1222
1223DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1224
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001225//===----------------------------------------------------------------------===//
1226// SelectInst Class
1227//===----------------------------------------------------------------------===//
1228
1229/// SelectInst - This class represents the LLVM 'select' instruction.
1230///
1231class SelectInst : public Instruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001232 void init(Value *C, Value *S1, Value *S2) {
Gabor Greifefe65362008-05-10 08:32:32 +00001233 Op<0>() = C;
1234 Op<1>() = S1;
1235 Op<2>() = S2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001236 }
1237
Chris Lattner454928e2005-01-29 00:31:36 +00001238 SelectInst(const SelectInst &SI)
Gabor Greifefe65362008-05-10 08:32:32 +00001239 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1240 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
Chris Lattner454928e2005-01-29 00:31:36 +00001241 }
Gabor Greifefe65362008-05-10 08:32:32 +00001242 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1243 Instruction *InsertBefore)
1244 : Instruction(S1->getType(), Instruction::Select,
1245 &Op<0>(), 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001246 init(C, S1, S2);
Chris Lattner910c80a2007-02-24 00:55:48 +00001247 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001248 }
1249 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1250 BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001251 : Instruction(S1->getType(), Instruction::Select,
1252 &Op<0>(), 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001253 init(C, S1, S2);
Chris Lattner910c80a2007-02-24 00:55:48 +00001254 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001255 }
Gabor Greif051a9502008-04-06 20:25:17 +00001256public:
Evan Chengd69bb1a2008-05-05 17:41:03 +00001257 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1258 const std::string &Name = "",
1259 Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00001260 return new(3) SelectInst(C, S1, S2, Name, InsertBefore);
1261 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001262 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1263 const std::string &Name, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001264 return new(3) SelectInst(C, S1, S2, Name, InsertAtEnd);
1265 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001266
Gabor Greifefe65362008-05-10 08:32:32 +00001267 Value *getCondition() const { return Op<0>(); }
1268 Value *getTrueValue() const { return Op<1>(); }
1269 Value *getFalseValue() const { return Op<2>(); }
Chris Lattner454928e2005-01-29 00:31:36 +00001270
1271 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001272 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001273
1274 OtherOps getOpcode() const {
1275 return static_cast<OtherOps>(Instruction::getOpcode());
1276 }
1277
Chris Lattnerf319e832004-10-15 23:52:05 +00001278 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001279
1280 // Methods for support type inquiry through isa, cast, and dyn_cast:
1281 static inline bool classof(const SelectInst *) { return true; }
1282 static inline bool classof(const Instruction *I) {
1283 return I->getOpcode() == Instruction::Select;
1284 }
1285 static inline bool classof(const Value *V) {
1286 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1287 }
1288};
1289
Gabor Greifefe65362008-05-10 08:32:32 +00001290template <>
1291struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1292};
1293
1294DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1295
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001296//===----------------------------------------------------------------------===//
1297// VAArgInst Class
1298//===----------------------------------------------------------------------===//
1299
1300/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001301/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001302///
Chris Lattner454928e2005-01-29 00:31:36 +00001303class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001304 VAArgInst(const VAArgInst &VAA)
Chris Lattner454928e2005-01-29 00:31:36 +00001305 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001306public:
1307 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
1308 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001309 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001310 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001311 }
1312 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
1313 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001314 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001315 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001316 }
1317
Chris Lattnerf319e832004-10-15 23:52:05 +00001318 virtual VAArgInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001319
1320 // Methods for support type inquiry through isa, cast, and dyn_cast:
1321 static inline bool classof(const VAArgInst *) { return true; }
1322 static inline bool classof(const Instruction *I) {
1323 return I->getOpcode() == VAArg;
1324 }
1325 static inline bool classof(const Value *V) {
1326 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1327 }
1328};
1329
1330//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001331// ExtractElementInst Class
1332//===----------------------------------------------------------------------===//
1333
1334/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001335/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001336///
1337class ExtractElementInst : public Instruction {
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001338 ExtractElementInst(const ExtractElementInst &EE) :
Gabor Greifefe65362008-05-10 08:32:32 +00001339 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
Gabor Greif6c80c382008-05-26 21:33:52 +00001340 Op<0>() = EE.Op<0>();
1341 Op<1>() = EE.Op<1>();
Robert Bocchino49b78a52006-01-10 19:04:13 +00001342 }
1343
1344public:
Gabor Greif051a9502008-04-06 20:25:17 +00001345 // allocate space for exactly two operands
1346 void *operator new(size_t s) {
Gabor Greifefe65362008-05-10 08:32:32 +00001347 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
Gabor Greif051a9502008-04-06 20:25:17 +00001348 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001349 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
1350 Instruction *InsertBefore = 0);
Chris Lattner06a248c22006-10-05 06:24:58 +00001351 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
1352 Instruction *InsertBefore = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001353 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
1354 BasicBlock *InsertAtEnd);
Chris Lattner06a248c22006-10-05 06:24:58 +00001355 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
1356 BasicBlock *InsertAtEnd);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001357
Chris Lattnerfa495842006-04-08 04:04:54 +00001358 /// isValidOperands - Return true if an extractelement instruction can be
1359 /// formed with the specified operands.
1360 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001361
Robert Bocchino49b78a52006-01-10 19:04:13 +00001362 virtual ExtractElementInst *clone() const;
1363
Robert Bocchino49b78a52006-01-10 19:04:13 +00001364 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001365 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001366
1367 // Methods for support type inquiry through isa, cast, and dyn_cast:
1368 static inline bool classof(const ExtractElementInst *) { return true; }
1369 static inline bool classof(const Instruction *I) {
1370 return I->getOpcode() == Instruction::ExtractElement;
1371 }
1372 static inline bool classof(const Value *V) {
1373 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1374 }
1375};
1376
Gabor Greifefe65362008-05-10 08:32:32 +00001377template <>
1378struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1379};
1380
1381DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1382
Robert Bocchino49b78a52006-01-10 19:04:13 +00001383//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001384// InsertElementInst Class
1385//===----------------------------------------------------------------------===//
1386
1387/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001388/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001389///
1390class InsertElementInst : public Instruction {
Chris Lattner6a56ed42006-04-14 22:20:07 +00001391 InsertElementInst(const InsertElementInst &IE);
Gabor Greif051a9502008-04-06 20:25:17 +00001392 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1393 const std::string &Name = "",Instruction *InsertBefore = 0);
1394 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1395 const std::string &Name = "",Instruction *InsertBefore = 0);
1396 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1397 const std::string &Name, BasicBlock *InsertAtEnd);
1398 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1399 const std::string &Name, BasicBlock *InsertAtEnd);
Robert Bocchinof9993442006-01-17 20:05:59 +00001400public:
Gabor Greif051a9502008-04-06 20:25:17 +00001401 static InsertElementInst *Create(const InsertElementInst &IE) {
1402 return new(IE.getNumOperands()) InsertElementInst(IE);
1403 }
1404 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Gabor Greifefe65362008-05-10 08:32:32 +00001405 const std::string &Name = "",
1406 Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00001407 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
1408 }
1409 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001410 const std::string &Name = "",
1411 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +00001412 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001413 }
1414 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001415 const std::string &Name,
1416 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001417 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertAtEnd);
1418 }
1419 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001420 const std::string &Name,
1421 BasicBlock *InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00001422 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001423 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001424
Chris Lattnerfa495842006-04-08 04:04:54 +00001425 /// isValidOperands - Return true if an insertelement instruction can be
1426 /// formed with the specified operands.
1427 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1428 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001429
Robert Bocchinof9993442006-01-17 20:05:59 +00001430 virtual InsertElementInst *clone() const;
1431
Reid Spencerac9dcb92007-02-15 03:39:18 +00001432 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001433 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001434 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001435 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001436 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001437
Robert Bocchinof9993442006-01-17 20:05:59 +00001438 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001439 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchinof9993442006-01-17 20:05:59 +00001440
1441 // Methods for support type inquiry through isa, cast, and dyn_cast:
1442 static inline bool classof(const InsertElementInst *) { return true; }
1443 static inline bool classof(const Instruction *I) {
1444 return I->getOpcode() == Instruction::InsertElement;
1445 }
1446 static inline bool classof(const Value *V) {
1447 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1448 }
1449};
1450
Gabor Greifefe65362008-05-10 08:32:32 +00001451template <>
1452struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1453};
1454
1455DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1456
Robert Bocchinof9993442006-01-17 20:05:59 +00001457//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001458// ShuffleVectorInst Class
1459//===----------------------------------------------------------------------===//
1460
1461/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1462/// input vectors.
1463///
1464class ShuffleVectorInst : public Instruction {
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001465 ShuffleVectorInst(const ShuffleVectorInst &IE);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001466public:
Gabor Greif051a9502008-04-06 20:25:17 +00001467 // allocate space for exactly three operands
1468 void *operator new(size_t s) {
1469 return User::operator new(s, 3);
1470 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001471 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1472 const std::string &Name = "", Instruction *InsertBefor = 0);
1473 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1474 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001475
Chris Lattnerfa495842006-04-08 04:04:54 +00001476 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001477 /// formed with the specified operands.
1478 static bool isValidOperands(const Value *V1, const Value *V2,
1479 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001480
Chris Lattner9fc18d22006-04-08 01:15:18 +00001481 virtual ShuffleVectorInst *clone() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001482
Reid Spencerac9dcb92007-02-15 03:39:18 +00001483 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001484 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001485 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001486 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001487 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001488
Chris Lattner9fc18d22006-04-08 01:15:18 +00001489 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001490 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner8728f192008-03-02 05:28:33 +00001491
1492 /// getMaskValue - Return the index from the shuffle mask for the specified
1493 /// output result. This is either -1 if the element is undef or a number less
1494 /// than 2*numelements.
1495 int getMaskValue(unsigned i) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001496
Chris Lattner9fc18d22006-04-08 01:15:18 +00001497 // Methods for support type inquiry through isa, cast, and dyn_cast:
1498 static inline bool classof(const ShuffleVectorInst *) { return true; }
1499 static inline bool classof(const Instruction *I) {
1500 return I->getOpcode() == Instruction::ShuffleVector;
1501 }
1502 static inline bool classof(const Value *V) {
1503 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1504 }
1505};
1506
Gabor Greifefe65362008-05-10 08:32:32 +00001507template <>
1508struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1509};
1510
1511DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
Chris Lattner9fc18d22006-04-08 01:15:18 +00001512
1513//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001514// ExtractValueInst Class
1515//===----------------------------------------------------------------------===//
1516
Dan Gohmane2d896f2008-05-15 23:35:32 +00001517/// ExtractValueInst - This instruction extracts a struct member or array
1518/// element value from an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001519///
1520class ExtractValueInst : public Instruction {
1521 ExtractValueInst(const ExtractValueInst &EVI);
1522 void init(Value *Agg, Value* const *Idx, unsigned NumIdx);
1523 void init(Value *Agg, Value *Idx);
1524
1525 template<typename InputIterator>
1526 void init(Value *Agg, InputIterator IdxBegin, InputIterator IdxEnd,
1527 const std::string &Name,
1528 // This argument ensures that we have an iterator we can
1529 // do arithmetic on in constant time
1530 std::random_access_iterator_tag) {
1531 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1532
1533 if (NumIdx > 0) {
1534 // This requires that the iterator points to contiguous memory.
1535 init(Agg, &*IdxBegin, NumIdx); // FIXME: for the general case
1536 // we have to build an array here
1537 }
1538 else {
1539 init(Agg, 0, NumIdx);
1540 }
1541
1542 setName(Name);
1543 }
1544
1545 /// getIndexedType - Returns the type of the element that would be extracted
1546 /// with an extractvalue instruction with the specified parameters.
1547 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +00001548 /// Null is returned if the indices are invalid for the specified
Dan Gohman041e2eb2008-05-15 19:50:34 +00001549 /// pointer type.
1550 ///
1551 static const Type *getIndexedType(const Type *Agg,
1552 Value* const *Idx, unsigned NumIdx);
1553
1554 template<typename InputIterator>
1555 static const Type *getIndexedType(const Type *Ptr,
1556 InputIterator IdxBegin,
1557 InputIterator IdxEnd,
1558 // This argument ensures that we
1559 // have an iterator we can do
1560 // arithmetic on in constant time
1561 std::random_access_iterator_tag) {
1562 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1563
1564 if (NumIdx > 0)
1565 // This requires that the iterator points to contiguous memory.
1566 return getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx);
1567 else
1568 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
1569 }
1570
Dan Gohmane2d896f2008-05-15 23:35:32 +00001571 /// Constructors - Create a extractvalue instruction with a base aggregate
1572 /// value and a list of indices. The first ctor can optionally insert before
1573 /// an existing instruction, the second appends the new instruction to the
1574 /// specified BasicBlock.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001575 template<typename InputIterator>
1576 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1577 InputIterator IdxEnd,
1578 unsigned Values,
1579 const std::string &Name,
1580 Instruction *InsertBefore);
1581 template<typename InputIterator>
1582 inline ExtractValueInst(Value *Agg,
1583 InputIterator IdxBegin, InputIterator IdxEnd,
1584 unsigned Values,
1585 const std::string &Name, BasicBlock *InsertAtEnd);
1586
1587 /// Constructors - These two constructors are convenience methods because one
1588 /// and two index extractvalue instructions are so common.
1589 ExtractValueInst(Value *Agg, Value *Idx, const std::string &Name = "",
1590 Instruction *InsertBefore = 0);
1591 ExtractValueInst(Value *Agg, Value *Idx,
1592 const std::string &Name, BasicBlock *InsertAtEnd);
1593public:
1594 template<typename InputIterator>
1595 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1596 InputIterator IdxEnd,
1597 const std::string &Name = "",
1598 Instruction *InsertBefore = 0) {
1599 typename std::iterator_traits<InputIterator>::difference_type Values =
1600 1 + std::distance(IdxBegin, IdxEnd);
1601 return new(Values)
1602 ExtractValueInst(Agg, IdxBegin, IdxEnd, Values, Name, InsertBefore);
1603 }
1604 template<typename InputIterator>
1605 static ExtractValueInst *Create(Value *Agg,
1606 InputIterator IdxBegin, InputIterator IdxEnd,
1607 const std::string &Name,
1608 BasicBlock *InsertAtEnd) {
1609 typename std::iterator_traits<InputIterator>::difference_type Values =
1610 1 + std::distance(IdxBegin, IdxEnd);
1611 return new(Values)
1612 ExtractValueInst(Agg, IdxBegin, IdxEnd, Values, Name, InsertAtEnd);
1613 }
1614
1615 /// Constructors - These two creators are convenience methods because one
1616 /// index extractvalue instructions are much more common than those with
1617 /// more than one.
1618 static ExtractValueInst *Create(Value *Agg, Value *Idx,
1619 const std::string &Name = "",
1620 Instruction *InsertBefore = 0) {
1621 return new(2) ExtractValueInst(Agg, Idx, Name, InsertBefore);
1622 }
1623 static ExtractValueInst *Create(Value *Agg, Value *Idx,
1624 const std::string &Name,
1625 BasicBlock *InsertAtEnd) {
1626 return new(2) ExtractValueInst(Agg, Idx, Name, InsertAtEnd);
1627 }
1628
1629 virtual ExtractValueInst *clone() const;
1630
1631 /// Transparently provide more efficient getOperand methods.
1632 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1633
1634 // getType - Overload to return most specific pointer type...
1635 const PointerType *getType() const {
1636 return reinterpret_cast<const PointerType*>(Instruction::getType());
1637 }
1638
1639 /// getIndexedType - Returns the type of the element that would be extracted
1640 /// with an extractvalue instruction with the specified parameters.
1641 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +00001642 /// Null is returned if the indices are invalid for the specified
Dan Gohman041e2eb2008-05-15 19:50:34 +00001643 /// pointer type.
1644 ///
1645 template<typename InputIterator>
1646 static const Type *getIndexedType(const Type *Ptr,
1647 InputIterator IdxBegin,
1648 InputIterator IdxEnd) {
1649 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1650 typename std::iterator_traits<InputIterator>::
1651 iterator_category());
1652 }
1653 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
1654
1655 inline op_iterator idx_begin() { return op_begin()+1; }
1656 inline const_op_iterator idx_begin() const { return op_begin()+1; }
1657 inline op_iterator idx_end() { return op_end(); }
1658 inline const_op_iterator idx_end() const { return op_end(); }
1659
1660 Value *getAggregateOperand() {
1661 return getOperand(0);
1662 }
1663 const Value *getAggregateOperand() const {
1664 return getOperand(0);
1665 }
1666 static unsigned getAggregateOperandIndex() {
1667 return 0U; // get index for modifying correct operand
1668 }
1669
1670 unsigned getNumIndices() const { // Note: always non-negative
1671 return getNumOperands() - 1;
1672 }
1673
1674 bool hasIndices() const {
1675 return getNumOperands() > 1;
1676 }
1677
1678 // Methods for support type inquiry through isa, cast, and dyn_cast:
1679 static inline bool classof(const ExtractValueInst *) { return true; }
1680 static inline bool classof(const Instruction *I) {
1681 return I->getOpcode() == Instruction::ExtractValue;
1682 }
1683 static inline bool classof(const Value *V) {
1684 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1685 }
1686};
1687
1688template <>
1689struct OperandTraits<ExtractValueInst> : VariadicOperandTraits<1> {
1690};
1691
Dan Gohmane4569942008-05-23 00:36:11 +00001692template<typename InputIterator>
1693ExtractValueInst::ExtractValueInst(Value *Agg,
1694 InputIterator IdxBegin,
1695 InputIterator IdxEnd,
1696 unsigned Values,
1697 const std::string &Name,
1698 Instruction *InsertBefore)
1699 : Instruction(checkType(getIndexedType(Agg->getType(), IdxBegin, IdxEnd)),
1700 ExtractValue,
1701 OperandTraits<ExtractValueInst>::op_end(this) - Values,
1702 Values, InsertBefore) {
1703 init(Agg, IdxBegin, IdxEnd, Name,
1704 typename std::iterator_traits<InputIterator>::iterator_category());
1705}
1706template<typename InputIterator>
1707ExtractValueInst::ExtractValueInst(Value *Agg,
1708 InputIterator IdxBegin,
1709 InputIterator IdxEnd,
1710 unsigned Values,
1711 const std::string &Name,
1712 BasicBlock *InsertAtEnd)
1713 : Instruction(PointerType::get(checkType(
1714 getIndexedType(Agg->getType(),
1715 IdxBegin, IdxEnd)),
1716 cast<PointerType>(Agg->getType())
1717 ->getAddressSpace()),
1718 ExtractValue,
1719 OperandTraits<ExtractValueInst>::op_end(this) - Values,
1720 Values, InsertAtEnd) {
1721 init(Agg, IdxBegin, IdxEnd, Name,
1722 typename std::iterator_traits<InputIterator>::iterator_category());
1723}
1724
Dan Gohman041e2eb2008-05-15 19:50:34 +00001725DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueInst, Value)
1726
Dan Gohmane4569942008-05-23 00:36:11 +00001727
Dan Gohman041e2eb2008-05-15 19:50:34 +00001728//===----------------------------------------------------------------------===//
1729// InsertValueInst Class
1730//===----------------------------------------------------------------------===//
1731
Dan Gohmane2d896f2008-05-15 23:35:32 +00001732/// InsertValueInst - This instruction inserts a struct field of array element
1733/// value into an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001734///
1735class InsertValueInst : public Instruction {
1736 InsertValueInst(const InsertValueInst &IVI);
1737 void init(Value *Agg, Value *Val, Value* const *Idx, unsigned NumIdx);
1738 void init(Value *Agg, Value *Val, Value *Idx);
1739
1740 template<typename InputIterator>
1741 void init(Value *Agg, Value *Val,
1742 InputIterator IdxBegin, InputIterator IdxEnd,
1743 const std::string &Name,
1744 // This argument ensures that we have an iterator we can
1745 // do arithmetic on in constant time
1746 std::random_access_iterator_tag) {
1747 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1748
1749 if (NumIdx > 0) {
1750 // This requires that the iterator points to contiguous memory.
1751 init(Agg, Val, &*IdxBegin, NumIdx); // FIXME: for the general case
1752 // we have to build an array here
1753 }
1754 else {
1755 init(Agg, Val, 0, NumIdx);
1756 }
1757
1758 setName(Name);
1759 }
1760
Dan Gohmane2d896f2008-05-15 23:35:32 +00001761 /// Constructors - Create a insertvalue instruction with a base aggregate
1762 /// value, a value to insert, and a list of indices. The first ctor can
1763 /// optionally insert before an existing instruction, the second appends
1764 /// the new instruction to the specified BasicBlock.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001765 template<typename InputIterator>
1766 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001767 InputIterator IdxEnd,
1768 unsigned Values,
1769 const std::string &Name,
1770 Instruction *InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001771 template<typename InputIterator>
1772 inline InsertValueInst(Value *Agg, Value *Val,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001773 InputIterator IdxBegin, InputIterator IdxEnd,
1774 unsigned Values,
1775 const std::string &Name, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001776
1777 /// Constructors - These two constructors are convenience methods because one
1778 /// and two index insertvalue instructions are so common.
1779 InsertValueInst(Value *Agg, Value *Val,
1780 Value *Idx, const std::string &Name = "",
1781 Instruction *InsertBefore = 0);
1782 InsertValueInst(Value *Agg, Value *Val, Value *Idx,
1783 const std::string &Name, BasicBlock *InsertAtEnd);
1784public:
1785 template<typename InputIterator>
1786 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1787 InputIterator IdxEnd,
1788 const std::string &Name = "",
1789 Instruction *InsertBefore = 0) {
1790 typename std::iterator_traits<InputIterator>::difference_type Values =
1791 1 + std::distance(IdxBegin, IdxEnd);
1792 return new(Values)
1793 InsertValueInst(Agg, Val, IdxBegin, IdxEnd, Values, Name, InsertBefore);
1794 }
1795 template<typename InputIterator>
1796 static InsertValueInst *Create(Value *Agg, Value *Val,
1797 InputIterator IdxBegin, InputIterator IdxEnd,
1798 const std::string &Name,
1799 BasicBlock *InsertAtEnd) {
1800 typename std::iterator_traits<InputIterator>::difference_type Values =
1801 1 + std::distance(IdxBegin, IdxEnd);
1802 return new(Values)
1803 InsertValueInst(Agg, Val, IdxBegin, IdxEnd, Values, Name, InsertAtEnd);
1804 }
1805
1806 /// Constructors - These two creators are convenience methods because one
1807 /// index insertvalue instructions are much more common than those with
1808 /// more than one.
1809 static InsertValueInst *Create(Value *Agg, Value *Val, Value *Idx,
1810 const std::string &Name = "",
1811 Instruction *InsertBefore = 0) {
1812 return new(3) InsertValueInst(Agg, Val, Idx, Name, InsertBefore);
1813 }
1814 static InsertValueInst *Create(Value *Agg, Value *Val, Value *Idx,
1815 const std::string &Name,
1816 BasicBlock *InsertAtEnd) {
1817 return new(3) InsertValueInst(Agg, Val, Idx, Name, InsertAtEnd);
1818 }
1819
1820 virtual InsertValueInst *clone() const;
1821
1822 /// Transparently provide more efficient getOperand methods.
1823 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1824
1825 // getType - Overload to return most specific pointer type...
1826 const PointerType *getType() const {
1827 return reinterpret_cast<const PointerType*>(Instruction::getType());
1828 }
1829
1830 inline op_iterator idx_begin() { return op_begin()+1; }
1831 inline const_op_iterator idx_begin() const { return op_begin()+1; }
1832 inline op_iterator idx_end() { return op_end(); }
1833 inline const_op_iterator idx_end() const { return op_end(); }
1834
1835 Value *getAggregateOperand() {
1836 return getOperand(0);
1837 }
1838 const Value *getAggregateOperand() const {
1839 return getOperand(0);
1840 }
1841 static unsigned getAggregateOperandIndex() {
1842 return 0U; // get index for modifying correct operand
1843 }
1844
1845 Value *getInsertedValueOperand() {
1846 return getOperand(1);
1847 }
1848 const Value *getInsertedValueOperand() const {
1849 return getOperand(1);
1850 }
1851 static unsigned getInsertedValueOperandIndex() {
1852 return 1U; // get index for modifying correct operand
1853 }
1854
1855 unsigned getNumIndices() const { // Note: always non-negative
1856 return getNumOperands() - 2;
1857 }
1858
1859 bool hasIndices() const {
1860 return getNumOperands() > 2;
1861 }
1862
1863 // Methods for support type inquiry through isa, cast, and dyn_cast:
1864 static inline bool classof(const InsertValueInst *) { return true; }
1865 static inline bool classof(const Instruction *I) {
1866 return I->getOpcode() == Instruction::InsertValue;
1867 }
1868 static inline bool classof(const Value *V) {
1869 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1870 }
1871};
1872
1873template <>
1874struct OperandTraits<InsertValueInst> : VariadicOperandTraits<2> {
1875};
1876
Dan Gohmane4569942008-05-23 00:36:11 +00001877template<typename InputIterator>
1878InsertValueInst::InsertValueInst(Value *Agg,
1879 Value *Val,
1880 InputIterator IdxBegin,
1881 InputIterator IdxEnd,
1882 unsigned Values,
1883 const std::string &Name,
1884 Instruction *InsertBefore)
1885 : Instruction(checkType(ExtractValueInst::getIndexedType(
1886 Agg->getType(),
1887 IdxBegin, IdxEnd)),
1888 InsertValue,
1889 OperandTraits<InsertValueInst>::op_end(this) - Values,
1890 Values, InsertBefore) {
1891 init(Agg, Val, IdxBegin, IdxEnd, Name,
1892 typename std::iterator_traits<InputIterator>::iterator_category());
1893}
1894template<typename InputIterator>
1895InsertValueInst::InsertValueInst(Value *Agg,
1896 Value *Val,
1897 InputIterator IdxBegin,
1898 InputIterator IdxEnd,
1899 unsigned Values,
1900 const std::string &Name,
1901 BasicBlock *InsertAtEnd)
1902 : Instruction(PointerType::get(checkType(
1903 ExtractValueInst::getIndexedType(
1904 Val->getType(),
1905 IdxBegin, IdxEnd)),
1906 cast<PointerType>(Val->getType())
1907 ->getAddressSpace()),
1908 InsertValue,
1909 OperandTraits<InsertValueInst>::op_end(this) - Values,
1910 Values, InsertAtEnd) {
1911 init(Agg, Val, IdxBegin, IdxEnd, Name,
1912 typename std::iterator_traits<InputIterator>::iterator_category());
1913}
1914
Dan Gohman041e2eb2008-05-15 19:50:34 +00001915DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1916
1917//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001918// PHINode Class
1919//===----------------------------------------------------------------------===//
1920
1921// PHINode - The PHINode class is used to represent the magical mystical PHI
1922// node, that can not exist in nature, but can be synthesized in a computer
1923// scientist's overactive imagination.
1924//
1925class PHINode : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001926 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00001927 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1928 /// the number actually in use.
1929 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001930 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00001931 // allocate space for exactly zero operands
1932 void *operator new(size_t s) {
1933 return User::operator new(s, 0);
1934 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001935 explicit PHINode(const Type *Ty, const std::string &Name = "",
1936 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001937 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Chris Lattner454928e2005-01-29 00:31:36 +00001938 ReservedSpace(0) {
Chris Lattner910c80a2007-02-24 00:55:48 +00001939 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001940 }
1941
1942 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001943 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Chris Lattner454928e2005-01-29 00:31:36 +00001944 ReservedSpace(0) {
Chris Lattner910c80a2007-02-24 00:55:48 +00001945 setName(Name);
Chris Lattner454928e2005-01-29 00:31:36 +00001946 }
Gabor Greif051a9502008-04-06 20:25:17 +00001947public:
1948 static PHINode *Create(const Type *Ty, const std::string &Name = "",
1949 Instruction *InsertBefore = 0) {
1950 return new PHINode(Ty, Name, InsertBefore);
1951 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001952 static PHINode *Create(const Type *Ty, const std::string &Name,
1953 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00001954 return new PHINode(Ty, Name, InsertAtEnd);
1955 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001956 ~PHINode();
1957
Chris Lattner454928e2005-01-29 00:31:36 +00001958 /// reserveOperandSpace - This method can be used to avoid repeated
1959 /// reallocation of PHI operand lists by reserving space for the correct
1960 /// number of operands before adding them. Unlike normal vector reserves,
1961 /// this method can also be used to trim the operand space.
1962 void reserveOperandSpace(unsigned NumValues) {
1963 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001964 }
1965
Chris Lattnerf319e832004-10-15 23:52:05 +00001966 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001967
Gabor Greifefe65362008-05-10 08:32:32 +00001968 /// Provide fast operand accessors
1969 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1970
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001971 /// getNumIncomingValues - Return the number of incoming edges
1972 ///
Chris Lattner454928e2005-01-29 00:31:36 +00001973 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001974
Reid Spencerc773de62006-05-19 19:07:54 +00001975 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001976 ///
1977 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001978 assert(i*2 < getNumOperands() && "Invalid value number!");
1979 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001980 }
1981 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +00001982 assert(i*2 < getNumOperands() && "Invalid value number!");
1983 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001984 }
Chris Lattner454928e2005-01-29 00:31:36 +00001985 unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001986 return i*2;
1987 }
1988
Reid Spencerc773de62006-05-19 19:07:54 +00001989 /// getIncomingBlock - Return incoming basic block number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001990 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00001991 BasicBlock *getIncomingBlock(unsigned i) const {
Gabor Greifefe65362008-05-10 08:32:32 +00001992 return static_cast<BasicBlock*>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001993 }
1994 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Gabor Greifefe65362008-05-10 08:32:32 +00001995 setOperand(i*2+1, BB);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001996 }
1997 unsigned getOperandNumForIncomingBlock(unsigned i) {
1998 return i*2+1;
1999 }
2000
2001 /// addIncoming - Add an incoming value to the end of the PHI list
2002 ///
2003 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00002004 assert(V && "PHI node got a null value!");
2005 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002006 assert(getType() == V->getType() &&
2007 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +00002008 unsigned OpNo = NumOperands;
2009 if (OpNo+2 > ReservedSpace)
2010 resizeOperands(0); // Get more space!
2011 // Initialize some new operands.
2012 NumOperands = OpNo+2;
Gabor Greif6c80c382008-05-26 21:33:52 +00002013 OperandList[OpNo] = V;
2014 OperandList[OpNo+1] = BB;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002015 }
Misha Brukman9769ab22005-04-21 20:19:05 +00002016
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002017 /// removeIncomingValue - Remove an incoming value. This is useful if a
2018 /// predecessor basic block is deleted. The value removed is returned.
2019 ///
2020 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2021 /// is true), the PHI node is destroyed and any uses of it are replaced with
2022 /// dummy values. The only time there should be zero incoming values to a PHI
2023 /// node is when the block is dead, so this strategy is sound.
2024 ///
2025 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2026
Gabor Greifefe65362008-05-10 08:32:32 +00002027 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002028 int Idx = getBasicBlockIndex(BB);
2029 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2030 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2031 }
2032
Misha Brukman9769ab22005-04-21 20:19:05 +00002033 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002034 /// block in the value list for this PHI. Returns -1 if no instance.
2035 ///
2036 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00002037 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00002038 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Gabor Greifefe65362008-05-10 08:32:32 +00002039 if (OL[i+1].get() == BB) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002040 return -1;
2041 }
2042
2043 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2044 return getIncomingValue(getBasicBlockIndex(BB));
2045 }
2046
Chris Lattnerf56a8db2006-10-03 17:09:12 +00002047 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00002048 /// same value, return the value, otherwise return null.
2049 ///
Chris Lattner9acbd612005-08-05 00:49:06 +00002050 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00002051
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002052 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2053 static inline bool classof(const PHINode *) { return true; }
2054 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00002055 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002056 }
2057 static inline bool classof(const Value *V) {
2058 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2059 }
Chris Lattner454928e2005-01-29 00:31:36 +00002060 private:
2061 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002062};
2063
Gabor Greifefe65362008-05-10 08:32:32 +00002064template <>
2065struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
2066};
2067
2068DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2069
2070
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002071//===----------------------------------------------------------------------===//
2072// ReturnInst Class
2073//===----------------------------------------------------------------------===//
2074
2075//===---------------------------------------------------------------------------
2076/// ReturnInst - Return a value (possibly void), from a function. Execution
2077/// does not continue in this function any longer.
2078///
2079class ReturnInst : public TerminatorInst {
Chris Lattner910c80a2007-02-24 00:55:48 +00002080 ReturnInst(const ReturnInst &RI);
Devang Patelfea98302008-02-26 19:15:26 +00002081 void init(Value * const* retVals, unsigned N);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002082
Gabor Greif051a9502008-04-06 20:25:17 +00002083private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002084 // ReturnInst constructors:
2085 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002086 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002087 // ReturnInst(Value* X) - 'ret X' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00002088 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002089 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
Gabor Greifefe65362008-05-10 08:32:32 +00002090 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2091 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
Devang Patele6be34a2008-02-27 01:20:54 +00002092 // ReturnInst(Value* X, N) - 'ret X,X+1...X+N-1' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00002093 // ReturnInst(Value* X, N, Inst *I) - 'ret X,X+1...X+N-1', insert before I
2094 // ReturnInst(Value* X, N, BB *B) - 'ret X,X+1...X+N-1', insert @ end of B
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002095 //
2096 // NOTE: If the Value* passed is of type void then the constructor behaves as
2097 // if it was passed NULL.
Chris Lattner910c80a2007-02-24 00:55:48 +00002098 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
2099 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
Gabor Greifefe65362008-05-10 08:32:32 +00002100 ReturnInst(Value * const* retVals, unsigned N, Instruction *InsertBefore = 0);
Devang Patelf4511cd2008-02-26 19:38:17 +00002101 ReturnInst(Value * const* retVals, unsigned N, BasicBlock *InsertAtEnd);
Chris Lattner910c80a2007-02-24 00:55:48 +00002102 explicit ReturnInst(BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002103public:
2104 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
2105 return new(!!retVal) ReturnInst(retVal, InsertBefore);
2106 }
2107 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
2108 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
2109 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00002110 static ReturnInst* Create(Value * const* retVals, unsigned N,
Gabor Greifefe65362008-05-10 08:32:32 +00002111 Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002112 return new(N) ReturnInst(retVals, N, InsertBefore);
2113 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00002114 static ReturnInst* Create(Value * const* retVals, unsigned N,
2115 BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002116 return new(N) ReturnInst(retVals, N, InsertAtEnd);
2117 }
2118 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
2119 return new(0) ReturnInst(InsertAtEnd);
2120 }
Devang Patel57ef4f42008-02-23 00:35:18 +00002121 virtual ~ReturnInst();
Gabor Greifefe65362008-05-10 08:32:32 +00002122 inline void operator delete(void*);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002123
Chris Lattnerf319e832004-10-15 23:52:05 +00002124 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002125
Gabor Greifefe65362008-05-10 08:32:32 +00002126 /// Provide fast operand accessors
2127 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Devang Patel64d4e612008-02-26 17:56:20 +00002128
Gabor Greifefe65362008-05-10 08:32:32 +00002129 /// Convenience accessor
Devang Patel1eafa062008-03-11 17:35:03 +00002130 Value *getReturnValue(unsigned n = 0) const {
Gabor Greifefe65362008-05-10 08:32:32 +00002131 return n < getNumOperands()
2132 ? getOperand(n)
2133 : 0;
Devang Patel1eafa062008-03-11 17:35:03 +00002134 }
2135
Chris Lattner454928e2005-01-29 00:31:36 +00002136 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002137
2138 // Methods for support type inquiry through isa, cast, and dyn_cast:
2139 static inline bool classof(const ReturnInst *) { return true; }
2140 static inline bool classof(const Instruction *I) {
2141 return (I->getOpcode() == Instruction::Ret);
2142 }
2143 static inline bool classof(const Value *V) {
2144 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2145 }
Chris Lattner454928e2005-01-29 00:31:36 +00002146 private:
2147 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2148 virtual unsigned getNumSuccessorsV() const;
2149 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002150};
2151
Gabor Greifefe65362008-05-10 08:32:32 +00002152template <>
2153struct OperandTraits<ReturnInst> : VariadicOperandTraits<> {
2154};
2155
2156DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2157void ReturnInst::operator delete(void *it) {
2158 ReturnInst* me(static_cast<ReturnInst*>(it));
2159 Use::zap(OperandTraits<ReturnInst>::op_begin(me),
2160 OperandTraits<ReturnInst>::op_end(me),
2161 true);
2162}
2163
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002164//===----------------------------------------------------------------------===//
2165// BranchInst Class
2166//===----------------------------------------------------------------------===//
2167
2168//===---------------------------------------------------------------------------
2169/// BranchInst - Conditional or Unconditional Branch instruction.
2170///
2171class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00002172 /// Ops list - Branches are strange. The operands are ordered:
2173 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
2174 /// they don't have to check for cond/uncond branchness.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002175 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00002176 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002177 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2178 // BranchInst(BB *B) - 'br B'
2179 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2180 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2181 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2182 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2183 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00002184 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002185 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002186 Instruction *InsertBefore = 0);
2187 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002188 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002189 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002190public:
2191 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2192 return new(1) BranchInst(IfTrue, InsertBefore);
2193 }
Gabor Greifefe65362008-05-10 08:32:32 +00002194 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2195 Value *Cond, Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002196 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2197 }
2198 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2199 return new(1) BranchInst(IfTrue, InsertAtEnd);
2200 }
Gabor Greifefe65362008-05-10 08:32:32 +00002201 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2202 Value *Cond, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002203 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2204 }
Chris Lattner454928e2005-01-29 00:31:36 +00002205
Bill Wendlingd2a5a2a2008-05-10 10:58:07 +00002206 ~BranchInst() {
Gabor Greifefe65362008-05-10 08:32:32 +00002207 if (NumOperands == 1)
Bill Wendlingc2e73532008-05-10 19:59:59 +00002208 NumOperands = (unsigned)((Use*)this - OperandList);
Gabor Greifefe65362008-05-10 08:32:32 +00002209 }
2210
Chris Lattner454928e2005-01-29 00:31:36 +00002211 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00002212 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002213
Chris Lattnerf319e832004-10-15 23:52:05 +00002214 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002215
Devang Patel4d4a5e02008-02-23 01:11:02 +00002216 bool isUnconditional() const { return getNumOperands() == 1; }
2217 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002218
Devang Patel4d4a5e02008-02-23 01:11:02 +00002219 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002220 assert(isConditional() && "Cannot get condition of an uncond branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00002221 return getOperand(2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002222 }
2223
2224 void setCondition(Value *V) {
2225 assert(isConditional() && "Cannot set condition of unconditional branch!");
2226 setOperand(2, V);
2227 }
2228
2229 // setUnconditionalDest - Change the current branch to an unconditional branch
2230 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +00002231 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002232 void setUnconditionalDest(BasicBlock *Dest) {
Gabor Greifefe65362008-05-10 08:32:32 +00002233 Op<0>() = Dest;
Chris Lattner454928e2005-01-29 00:31:36 +00002234 if (isConditional()) { // Convert this to an uncond branch.
Gabor Greifefe65362008-05-10 08:32:32 +00002235 Op<1>().set(0);
2236 Op<2>().set(0);
Chris Lattner454928e2005-01-29 00:31:36 +00002237 NumOperands = 1;
Chris Lattner454928e2005-01-29 00:31:36 +00002238 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002239 }
2240
Chris Lattner454928e2005-01-29 00:31:36 +00002241 unsigned getNumSuccessors() const { return 1+isConditional(); }
2242
2243 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002244 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Dan Gohmanb96039e2007-05-11 20:59:29 +00002245 return cast<BasicBlock>(getOperand(i));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002246 }
2247
Chris Lattner454928e2005-01-29 00:31:36 +00002248 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002249 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifefe65362008-05-10 08:32:32 +00002250 setOperand(idx, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002251 }
2252
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002253 // Methods for support type inquiry through isa, cast, and dyn_cast:
2254 static inline bool classof(const BranchInst *) { return true; }
2255 static inline bool classof(const Instruction *I) {
2256 return (I->getOpcode() == Instruction::Br);
2257 }
2258 static inline bool classof(const Value *V) {
2259 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2260 }
Chris Lattner454928e2005-01-29 00:31:36 +00002261private:
2262 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2263 virtual unsigned getNumSuccessorsV() const;
2264 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002265};
2266
Gabor Greifefe65362008-05-10 08:32:32 +00002267template <>
2268struct OperandTraits<BranchInst> : HungoffOperandTraits<> {
2269 // we need to access operands via OperandList, since
2270 // the NumOperands may change from 3 to 1
2271 static inline void *allocate(unsigned); // FIXME
2272};
2273
2274DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2275
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002276//===----------------------------------------------------------------------===//
2277// SwitchInst Class
2278//===----------------------------------------------------------------------===//
2279
2280//===---------------------------------------------------------------------------
2281/// SwitchInst - Multiway switch
2282///
2283class SwitchInst : public TerminatorInst {
Gabor Greifefe65362008-05-10 08:32:32 +00002284 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00002285 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002286 // Operand[0] = Value to switch on
2287 // Operand[1] = Default basic block destination
2288 // Operand[2n ] = Value to match
2289 // Operand[2n+1] = BasicBlock to go to on match
2290 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00002291 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2292 void resizeOperands(unsigned No);
Gabor Greifefe65362008-05-10 08:32:32 +00002293 // allocate space for exactly zero operands
2294 void *operator new(size_t s) {
2295 return User::operator new(s, 0);
2296 }
Chris Lattner454928e2005-01-29 00:31:36 +00002297 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2298 /// switch on and a default destination. The number of additional cases can
2299 /// be specified here to make memory allocation more efficient. This
2300 /// constructor can also autoinsert before another instruction.
2301 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002302 Instruction *InsertBefore = 0);
2303
Chris Lattner454928e2005-01-29 00:31:36 +00002304 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2305 /// switch on and a default destination. The number of additional cases can
2306 /// be specified here to make memory allocation more efficient. This
2307 /// constructor also autoinserts at the end of the specified BasicBlock.
2308 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002309 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002310public:
Gabor Greifefe65362008-05-10 08:32:32 +00002311 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2312 unsigned NumCases, Instruction *InsertBefore = 0) {
2313 return new SwitchInst(Value, Default, NumCases, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002314 }
Gabor Greifefe65362008-05-10 08:32:32 +00002315 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2316 unsigned NumCases, BasicBlock *InsertAtEnd) {
2317 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002318 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002319 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00002320
Gabor Greifefe65362008-05-10 08:32:32 +00002321 /// Provide fast operand accessors
2322 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2323
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002324 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00002325 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00002326 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00002327
Devang Patel4d4a5e02008-02-23 01:11:02 +00002328 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002329 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002330 }
2331
2332 /// getNumCases - return the number of 'cases' in this switch instruction.
2333 /// Note that case #0 is always the default case.
2334 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002335 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002336 }
2337
2338 /// getCaseValue - Return the specified case value. Note that case #0, the
2339 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002340 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002341 assert(i && i < getNumCases() && "Illegal case value to get!");
2342 return getSuccessorValue(i);
2343 }
2344
2345 /// getCaseValue - Return the specified case value. Note that case #0, the
2346 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002347 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002348 assert(i && i < getNumCases() && "Illegal case value to get!");
2349 return getSuccessorValue(i);
2350 }
2351
2352 /// findCaseValue - Search all of the case values for the specified constant.
2353 /// If it is explicitly handled, return the case number of it, otherwise
2354 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002355 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002356 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2357 if (getCaseValue(i) == C)
2358 return i;
2359 return 0;
2360 }
2361
Nick Lewycky011f1842006-09-18 19:03:59 +00002362 /// findCaseDest - Finds the unique case value for a given successor. Returns
2363 /// null if the successor is not found, not unique, or is the default case.
2364 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00002365 if (BB == getDefaultDest()) return NULL;
2366
Nick Lewycky011f1842006-09-18 19:03:59 +00002367 ConstantInt *CI = NULL;
2368 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2369 if (getSuccessor(i) == BB) {
2370 if (CI) return NULL; // Multiple cases lead to BB.
2371 else CI = getCaseValue(i);
2372 }
2373 }
2374 return CI;
2375 }
2376
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002377 /// addCase - Add an entry to the switch instruction...
2378 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00002379 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002380
2381 /// removeCase - This method removes the specified successor from the switch
2382 /// instruction. Note that this cannot be used to remove the default
2383 /// destination (successor #0).
2384 ///
2385 void removeCase(unsigned idx);
2386
Chris Lattner454928e2005-01-29 00:31:36 +00002387 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002388
Chris Lattner454928e2005-01-29 00:31:36 +00002389 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2390 BasicBlock *getSuccessor(unsigned idx) const {
2391 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2392 return cast<BasicBlock>(getOperand(idx*2+1));
2393 }
2394 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002395 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Gabor Greifefe65362008-05-10 08:32:32 +00002396 setOperand(idx*2+1, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002397 }
2398
2399 // getSuccessorValue - Return the value associated with the specified
2400 // successor.
Devang Patel4d4a5e02008-02-23 01:11:02 +00002401 ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002402 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00002403 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002404 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002405
2406 // Methods for support type inquiry through isa, cast, and dyn_cast:
2407 static inline bool classof(const SwitchInst *) { return true; }
2408 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00002409 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002410 }
2411 static inline bool classof(const Value *V) {
2412 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2413 }
Chris Lattner454928e2005-01-29 00:31:36 +00002414private:
2415 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2416 virtual unsigned getNumSuccessorsV() const;
2417 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002418};
2419
Gabor Greifefe65362008-05-10 08:32:32 +00002420template <>
2421struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2422};
2423
2424DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2425
2426
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002427//===----------------------------------------------------------------------===//
2428// InvokeInst Class
2429//===----------------------------------------------------------------------===//
2430
Chris Lattner3340ffe2005-05-06 20:26:26 +00002431/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2432/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002433///
2434class InvokeInst : public TerminatorInst {
Chris Lattner58d74912008-03-12 17:45:29 +00002435 PAListPtr ParamAttrs;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002436 InvokeInst(const InvokeInst &BI);
2437 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerd2dd1502007-02-13 01:04:01 +00002438 Value* const *Args, unsigned NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00002439
2440 template<typename InputIterator>
2441 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2442 InputIterator ArgBegin, InputIterator ArgEnd,
2443 const std::string &Name,
2444 // This argument ensures that we have an iterator we can
2445 // do arithmetic on in constant time
2446 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002447 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greenef1355a52007-08-27 19:04:21 +00002448
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002449 // This requires that the iterator points to contiguous memory.
2450 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00002451 setName(Name);
2452 }
2453
David Greenef1355a52007-08-27 19:04:21 +00002454 /// Construct an InvokeInst given a range of arguments.
2455 /// InputIterator must be a random-access iterator pointing to
2456 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2457 /// made for random-accessness but not for contiguous storage as
2458 /// that would incur runtime overhead.
2459 ///
2460 /// @brief Construct an InvokeInst from a range of arguments
2461 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002462 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2463 InputIterator ArgBegin, InputIterator ArgEnd,
2464 unsigned Values,
2465 const std::string &Name, Instruction *InsertBefore);
David Greenef1355a52007-08-27 19:04:21 +00002466
2467 /// Construct an InvokeInst given a range of arguments.
2468 /// InputIterator must be a random-access iterator pointing to
2469 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2470 /// made for random-accessness but not for contiguous storage as
2471 /// that would incur runtime overhead.
2472 ///
2473 /// @brief Construct an InvokeInst from a range of arguments
2474 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002475 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2476 InputIterator ArgBegin, InputIterator ArgEnd,
2477 unsigned Values,
2478 const std::string &Name, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002479public:
2480 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002481 static InvokeInst *Create(Value *Func,
2482 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002483 InputIterator ArgBegin, InputIterator ArgEnd,
Evan Chengd69bb1a2008-05-05 17:41:03 +00002484 const std::string &Name = "",
2485 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +00002486 unsigned Values(ArgEnd - ArgBegin + 3);
2487 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2488 Values, Name, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002489 }
2490 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002491 static InvokeInst *Create(Value *Func,
2492 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002493 InputIterator ArgBegin, InputIterator ArgEnd,
2494 const std::string &Name, BasicBlock *InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00002495 unsigned Values(ArgEnd - ArgBegin + 3);
2496 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2497 Values, Name, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002498 }
David Greenef1355a52007-08-27 19:04:21 +00002499
Chris Lattnerf319e832004-10-15 23:52:05 +00002500 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002501
Gabor Greifefe65362008-05-10 08:32:32 +00002502 /// Provide fast operand accessors
2503 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2504
Chris Lattner3340ffe2005-05-06 20:26:26 +00002505 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2506 /// function call.
2507 unsigned getCallingConv() const { return SubclassData; }
2508 void setCallingConv(unsigned CC) {
2509 SubclassData = CC;
2510 }
2511
Chris Lattner041221c2008-03-13 04:33:03 +00002512 /// getParamAttrs - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002513 ///
2514 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002515
Chris Lattner041221c2008-03-13 04:33:03 +00002516 /// setParamAttrs - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002517 ///
2518 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00002519
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002520 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002521 bool paramHasAttr(unsigned i, ParameterAttributes attr) const;
Eric Christopher0bf7b412008-05-16 20:39:43 +00002522
2523 /// addParamAttr - adds the attribute to the list of attributes.
2524 void addParamAttr(unsigned i, ParameterAttributes attr);
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002525
Dale Johannesen08e78b12008-02-22 17:49:45 +00002526 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002527 unsigned getParamAlignment(unsigned i) const {
2528 return ParamAttrs.getParamAlignment(i);
2529 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00002530
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002531 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002532 bool doesNotAccessMemory() const {
2533 return paramHasAttr(0, ParamAttr::ReadNone);
2534 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002535
2536 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002537 bool onlyReadsMemory() const {
2538 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
2539 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002540
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002541 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002542 bool doesNotReturn() const {
2543 return paramHasAttr(0, ParamAttr::NoReturn);
2544 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002545
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002546 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002547 bool doesNotThrow() const {
2548 return paramHasAttr(0, ParamAttr::NoUnwind);
2549 }
Duncan Sandsf0c33542007-12-19 21:13:37 +00002550 void setDoesNotThrow(bool doesNotThrow = true);
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002551
Devang Patel41e23972008-03-03 21:46:28 +00002552 /// @brief Determine if the call returns a structure through first
2553 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002554 bool hasStructRetAttr() const {
2555 // Be friendly and also check the callee.
2556 return paramHasAttr(1, ParamAttr::StructRet);
2557 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002558
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002559 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00002560 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002561 ///
Chris Lattner721aef62004-11-18 17:46:57 +00002562 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002563 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00002564 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002565
2566 // getCalledValue - Get a pointer to a function that is invoked by this inst.
Devang Patel4d4a5e02008-02-23 01:11:02 +00002567 Value *getCalledValue() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002568
2569 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00002570 BasicBlock *getNormalDest() const {
2571 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002572 }
Chris Lattner454928e2005-01-29 00:31:36 +00002573 BasicBlock *getUnwindDest() const {
2574 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002575 }
Chris Lattner454928e2005-01-29 00:31:36 +00002576 void setNormalDest(BasicBlock *B) {
Gabor Greifefe65362008-05-10 08:32:32 +00002577 setOperand(1, B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002578 }
2579
Chris Lattner454928e2005-01-29 00:31:36 +00002580 void setUnwindDest(BasicBlock *B) {
Gabor Greifefe65362008-05-10 08:32:32 +00002581 setOperand(2, B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002582 }
2583
Devang Patel4d4a5e02008-02-23 01:11:02 +00002584 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002585 assert(i < 2 && "Successor # out of range for invoke!");
2586 return i == 0 ? getNormalDest() : getUnwindDest();
2587 }
2588
Chris Lattner454928e2005-01-29 00:31:36 +00002589 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002590 assert(idx < 2 && "Successor # out of range for invoke!");
Gabor Greifefe65362008-05-10 08:32:32 +00002591 setOperand(idx+1, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002592 }
2593
Chris Lattner454928e2005-01-29 00:31:36 +00002594 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002595
2596 // Methods for support type inquiry through isa, cast, and dyn_cast:
2597 static inline bool classof(const InvokeInst *) { return true; }
2598 static inline bool classof(const Instruction *I) {
2599 return (I->getOpcode() == Instruction::Invoke);
2600 }
2601 static inline bool classof(const Value *V) {
2602 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2603 }
Chris Lattner454928e2005-01-29 00:31:36 +00002604private:
2605 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2606 virtual unsigned getNumSuccessorsV() const;
2607 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002608};
2609
Gabor Greifefe65362008-05-10 08:32:32 +00002610template <>
2611struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2612};
2613
2614template<typename InputIterator>
2615InvokeInst::InvokeInst(Value *Func,
2616 BasicBlock *IfNormal, BasicBlock *IfException,
2617 InputIterator ArgBegin, InputIterator ArgEnd,
2618 unsigned Values,
2619 const std::string &Name, Instruction *InsertBefore)
2620 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2621 ->getElementType())->getReturnType(),
2622 Instruction::Invoke,
2623 OperandTraits<InvokeInst>::op_end(this) - Values,
2624 Values, InsertBefore) {
2625 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
2626 typename std::iterator_traits<InputIterator>::iterator_category());
2627}
2628template<typename InputIterator>
2629InvokeInst::InvokeInst(Value *Func,
2630 BasicBlock *IfNormal, BasicBlock *IfException,
2631 InputIterator ArgBegin, InputIterator ArgEnd,
2632 unsigned Values,
2633 const std::string &Name, BasicBlock *InsertAtEnd)
2634 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2635 ->getElementType())->getReturnType(),
2636 Instruction::Invoke,
2637 OperandTraits<InvokeInst>::op_end(this) - Values,
2638 Values, InsertAtEnd) {
2639 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
2640 typename std::iterator_traits<InputIterator>::iterator_category());
2641}
2642
2643DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002644
2645//===----------------------------------------------------------------------===//
2646// UnwindInst Class
2647//===----------------------------------------------------------------------===//
2648
2649//===---------------------------------------------------------------------------
2650/// UnwindInst - Immediately exit the current function, unwinding the stack
2651/// until an invoke instruction is found.
2652///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002653class UnwindInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002654 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002655public:
Gabor Greif051a9502008-04-06 20:25:17 +00002656 // allocate space for exactly zero operands
2657 void *operator new(size_t s) {
2658 return User::operator new(s, 0);
2659 }
Chris Lattner910c80a2007-02-24 00:55:48 +00002660 explicit UnwindInst(Instruction *InsertBefore = 0);
2661 explicit UnwindInst(BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002662
Chris Lattnerf319e832004-10-15 23:52:05 +00002663 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002664
Chris Lattner454928e2005-01-29 00:31:36 +00002665 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002666
2667 // Methods for support type inquiry through isa, cast, and dyn_cast:
2668 static inline bool classof(const UnwindInst *) { return true; }
2669 static inline bool classof(const Instruction *I) {
2670 return I->getOpcode() == Instruction::Unwind;
2671 }
2672 static inline bool classof(const Value *V) {
2673 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2674 }
Chris Lattner454928e2005-01-29 00:31:36 +00002675private:
2676 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2677 virtual unsigned getNumSuccessorsV() const;
2678 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002679};
2680
Chris Lattner076b3f12004-10-16 18:05:54 +00002681//===----------------------------------------------------------------------===//
2682// UnreachableInst Class
2683//===----------------------------------------------------------------------===//
2684
2685//===---------------------------------------------------------------------------
2686/// UnreachableInst - This function has undefined behavior. In particular, the
2687/// presence of this instruction indicates some higher level knowledge that the
2688/// end of the block cannot be reached.
2689///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002690class UnreachableInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002691 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002692public:
Gabor Greif051a9502008-04-06 20:25:17 +00002693 // allocate space for exactly zero operands
2694 void *operator new(size_t s) {
2695 return User::operator new(s, 0);
2696 }
Chris Lattner910c80a2007-02-24 00:55:48 +00002697 explicit UnreachableInst(Instruction *InsertBefore = 0);
2698 explicit UnreachableInst(BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00002699
2700 virtual UnreachableInst *clone() const;
2701
Chris Lattner454928e2005-01-29 00:31:36 +00002702 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00002703
2704 // Methods for support type inquiry through isa, cast, and dyn_cast:
2705 static inline bool classof(const UnreachableInst *) { return true; }
2706 static inline bool classof(const Instruction *I) {
2707 return I->getOpcode() == Instruction::Unreachable;
2708 }
2709 static inline bool classof(const Value *V) {
2710 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2711 }
Chris Lattner454928e2005-01-29 00:31:36 +00002712private:
2713 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2714 virtual unsigned getNumSuccessorsV() const;
2715 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00002716};
2717
Reid Spencer3da59db2006-11-27 01:05:10 +00002718//===----------------------------------------------------------------------===//
2719// TruncInst Class
2720//===----------------------------------------------------------------------===//
2721
2722/// @brief This class represents a truncation of integer types.
2723class TruncInst : public CastInst {
2724 /// Private copy constructor
2725 TruncInst(const TruncInst &CI)
2726 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2727 }
2728public:
2729 /// @brief Constructor with insert-before-instruction semantics
2730 TruncInst(
2731 Value *S, ///< The value to be truncated
2732 const Type *Ty, ///< The (smaller) type to truncate to
2733 const std::string &Name = "", ///< A name for the new instruction
2734 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2735 );
2736
2737 /// @brief Constructor with insert-at-end-of-block semantics
2738 TruncInst(
2739 Value *S, ///< The value to be truncated
2740 const Type *Ty, ///< The (smaller) type to truncate to
2741 const std::string &Name, ///< A name for the new instruction
2742 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2743 );
2744
2745 /// @brief Clone an identical TruncInst
2746 virtual CastInst *clone() const;
2747
2748 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2749 static inline bool classof(const TruncInst *) { return true; }
2750 static inline bool classof(const Instruction *I) {
2751 return I->getOpcode() == Trunc;
2752 }
2753 static inline bool classof(const Value *V) {
2754 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2755 }
2756};
2757
2758//===----------------------------------------------------------------------===//
2759// ZExtInst Class
2760//===----------------------------------------------------------------------===//
2761
2762/// @brief This class represents zero extension of integer types.
2763class ZExtInst : public CastInst {
2764 /// @brief Private copy constructor
2765 ZExtInst(const ZExtInst &CI)
2766 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2767 }
2768public:
2769 /// @brief Constructor with insert-before-instruction semantics
2770 ZExtInst(
2771 Value *S, ///< The value to be zero extended
2772 const Type *Ty, ///< The type to zero extend to
2773 const std::string &Name = "", ///< A name for the new instruction
2774 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2775 );
2776
2777 /// @brief Constructor with insert-at-end semantics.
2778 ZExtInst(
2779 Value *S, ///< The value to be zero extended
2780 const Type *Ty, ///< The type to zero extend to
2781 const std::string &Name, ///< A name for the new instruction
2782 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2783 );
2784
2785 /// @brief Clone an identical ZExtInst
2786 virtual CastInst *clone() const;
2787
2788 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2789 static inline bool classof(const ZExtInst *) { return true; }
2790 static inline bool classof(const Instruction *I) {
2791 return I->getOpcode() == ZExt;
2792 }
2793 static inline bool classof(const Value *V) {
2794 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2795 }
2796};
2797
2798//===----------------------------------------------------------------------===//
2799// SExtInst Class
2800//===----------------------------------------------------------------------===//
2801
2802/// @brief This class represents a sign extension of integer types.
2803class SExtInst : public CastInst {
2804 /// @brief Private copy constructor
2805 SExtInst(const SExtInst &CI)
2806 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2807 }
2808public:
2809 /// @brief Constructor with insert-before-instruction semantics
2810 SExtInst(
2811 Value *S, ///< The value to be sign extended
2812 const Type *Ty, ///< The type to sign extend to
2813 const std::string &Name = "", ///< A name for the new instruction
2814 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2815 );
2816
2817 /// @brief Constructor with insert-at-end-of-block semantics
2818 SExtInst(
2819 Value *S, ///< The value to be sign extended
2820 const Type *Ty, ///< The type to sign extend to
2821 const std::string &Name, ///< A name for the new instruction
2822 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2823 );
2824
2825 /// @brief Clone an identical SExtInst
2826 virtual CastInst *clone() const;
2827
2828 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2829 static inline bool classof(const SExtInst *) { return true; }
2830 static inline bool classof(const Instruction *I) {
2831 return I->getOpcode() == SExt;
2832 }
2833 static inline bool classof(const Value *V) {
2834 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2835 }
2836};
2837
2838//===----------------------------------------------------------------------===//
2839// FPTruncInst Class
2840//===----------------------------------------------------------------------===//
2841
2842/// @brief This class represents a truncation of floating point types.
2843class FPTruncInst : public CastInst {
2844 FPTruncInst(const FPTruncInst &CI)
2845 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2846 }
2847public:
2848 /// @brief Constructor with insert-before-instruction semantics
2849 FPTruncInst(
2850 Value *S, ///< The value to be truncated
2851 const Type *Ty, ///< The type to truncate to
2852 const std::string &Name = "", ///< A name for the new instruction
2853 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2854 );
2855
2856 /// @brief Constructor with insert-before-instruction semantics
2857 FPTruncInst(
2858 Value *S, ///< The value to be truncated
2859 const Type *Ty, ///< The type to truncate to
2860 const std::string &Name, ///< A name for the new instruction
2861 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2862 );
2863
2864 /// @brief Clone an identical FPTruncInst
2865 virtual CastInst *clone() const;
2866
2867 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2868 static inline bool classof(const FPTruncInst *) { return true; }
2869 static inline bool classof(const Instruction *I) {
2870 return I->getOpcode() == FPTrunc;
2871 }
2872 static inline bool classof(const Value *V) {
2873 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2874 }
2875};
2876
2877//===----------------------------------------------------------------------===//
2878// FPExtInst Class
2879//===----------------------------------------------------------------------===//
2880
2881/// @brief This class represents an extension of floating point types.
2882class FPExtInst : public CastInst {
2883 FPExtInst(const FPExtInst &CI)
2884 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2885 }
2886public:
2887 /// @brief Constructor with insert-before-instruction semantics
2888 FPExtInst(
2889 Value *S, ///< The value to be extended
2890 const Type *Ty, ///< The type to extend to
2891 const std::string &Name = "", ///< A name for the new instruction
2892 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2893 );
2894
2895 /// @brief Constructor with insert-at-end-of-block semantics
2896 FPExtInst(
2897 Value *S, ///< The value to be extended
2898 const Type *Ty, ///< The type to extend to
2899 const std::string &Name, ///< A name for the new instruction
2900 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2901 );
2902
2903 /// @brief Clone an identical FPExtInst
2904 virtual CastInst *clone() const;
2905
2906 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2907 static inline bool classof(const FPExtInst *) { return true; }
2908 static inline bool classof(const Instruction *I) {
2909 return I->getOpcode() == FPExt;
2910 }
2911 static inline bool classof(const Value *V) {
2912 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2913 }
2914};
2915
2916//===----------------------------------------------------------------------===//
2917// UIToFPInst Class
2918//===----------------------------------------------------------------------===//
2919
2920/// @brief This class represents a cast unsigned integer to floating point.
2921class UIToFPInst : public CastInst {
2922 UIToFPInst(const UIToFPInst &CI)
2923 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2924 }
2925public:
2926 /// @brief Constructor with insert-before-instruction semantics
2927 UIToFPInst(
2928 Value *S, ///< The value to be converted
2929 const Type *Ty, ///< The type to convert to
2930 const std::string &Name = "", ///< A name for the new instruction
2931 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2932 );
2933
2934 /// @brief Constructor with insert-at-end-of-block semantics
2935 UIToFPInst(
2936 Value *S, ///< The value to be converted
2937 const Type *Ty, ///< The type to convert to
2938 const std::string &Name, ///< A name for the new instruction
2939 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2940 );
2941
2942 /// @brief Clone an identical UIToFPInst
2943 virtual CastInst *clone() const;
2944
2945 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2946 static inline bool classof(const UIToFPInst *) { return true; }
2947 static inline bool classof(const Instruction *I) {
2948 return I->getOpcode() == UIToFP;
2949 }
2950 static inline bool classof(const Value *V) {
2951 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2952 }
2953};
2954
2955//===----------------------------------------------------------------------===//
2956// SIToFPInst Class
2957//===----------------------------------------------------------------------===//
2958
2959/// @brief This class represents a cast from signed integer to floating point.
2960class SIToFPInst : public CastInst {
2961 SIToFPInst(const SIToFPInst &CI)
2962 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2963 }
2964public:
2965 /// @brief Constructor with insert-before-instruction semantics
2966 SIToFPInst(
2967 Value *S, ///< The value to be converted
2968 const Type *Ty, ///< The type to convert to
2969 const std::string &Name = "", ///< A name for the new instruction
2970 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2971 );
2972
2973 /// @brief Constructor with insert-at-end-of-block semantics
2974 SIToFPInst(
2975 Value *S, ///< The value to be converted
2976 const Type *Ty, ///< The type to convert to
2977 const std::string &Name, ///< A name for the new instruction
2978 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2979 );
2980
2981 /// @brief Clone an identical SIToFPInst
2982 virtual CastInst *clone() const;
2983
2984 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2985 static inline bool classof(const SIToFPInst *) { return true; }
2986 static inline bool classof(const Instruction *I) {
2987 return I->getOpcode() == SIToFP;
2988 }
2989 static inline bool classof(const Value *V) {
2990 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2991 }
2992};
2993
2994//===----------------------------------------------------------------------===//
2995// FPToUIInst Class
2996//===----------------------------------------------------------------------===//
2997
2998/// @brief This class represents a cast from floating point to unsigned integer
2999class FPToUIInst : public CastInst {
3000 FPToUIInst(const FPToUIInst &CI)
3001 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
3002 }
3003public:
3004 /// @brief Constructor with insert-before-instruction semantics
3005 FPToUIInst(
3006 Value *S, ///< The value to be converted
3007 const Type *Ty, ///< The type to convert to
3008 const std::string &Name = "", ///< A name for the new instruction
3009 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3010 );
3011
3012 /// @brief Constructor with insert-at-end-of-block semantics
3013 FPToUIInst(
3014 Value *S, ///< The value to be converted
3015 const Type *Ty, ///< The type to convert to
3016 const std::string &Name, ///< A name for the new instruction
3017 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3018 );
3019
3020 /// @brief Clone an identical FPToUIInst
3021 virtual CastInst *clone() const;
3022
3023 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3024 static inline bool classof(const FPToUIInst *) { return true; }
3025 static inline bool classof(const Instruction *I) {
3026 return I->getOpcode() == FPToUI;
3027 }
3028 static inline bool classof(const Value *V) {
3029 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3030 }
3031};
3032
3033//===----------------------------------------------------------------------===//
3034// FPToSIInst Class
3035//===----------------------------------------------------------------------===//
3036
3037/// @brief This class represents a cast from floating point to signed integer.
3038class FPToSIInst : public CastInst {
3039 FPToSIInst(const FPToSIInst &CI)
3040 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
3041 }
3042public:
3043 /// @brief Constructor with insert-before-instruction semantics
3044 FPToSIInst(
3045 Value *S, ///< The value to be converted
3046 const Type *Ty, ///< The type to convert to
3047 const std::string &Name = "", ///< A name for the new instruction
3048 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3049 );
3050
3051 /// @brief Constructor with insert-at-end-of-block semantics
3052 FPToSIInst(
3053 Value *S, ///< The value to be converted
3054 const Type *Ty, ///< The type to convert to
3055 const std::string &Name, ///< A name for the new instruction
3056 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3057 );
3058
3059 /// @brief Clone an identical FPToSIInst
3060 virtual CastInst *clone() const;
3061
3062 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3063 static inline bool classof(const FPToSIInst *) { return true; }
3064 static inline bool classof(const Instruction *I) {
3065 return I->getOpcode() == FPToSI;
3066 }
3067 static inline bool classof(const Value *V) {
3068 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3069 }
3070};
3071
3072//===----------------------------------------------------------------------===//
3073// IntToPtrInst Class
3074//===----------------------------------------------------------------------===//
3075
3076/// @brief This class represents a cast from an integer to a pointer.
3077class IntToPtrInst : public CastInst {
3078 IntToPtrInst(const IntToPtrInst &CI)
3079 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
3080 }
3081public:
3082 /// @brief Constructor with insert-before-instruction semantics
3083 IntToPtrInst(
3084 Value *S, ///< The value to be converted
3085 const Type *Ty, ///< The type to convert to
3086 const std::string &Name = "", ///< A name for the new instruction
3087 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3088 );
3089
3090 /// @brief Constructor with insert-at-end-of-block semantics
3091 IntToPtrInst(
3092 Value *S, ///< The value to be converted
3093 const Type *Ty, ///< The type to convert to
3094 const std::string &Name, ///< A name for the new instruction
3095 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3096 );
3097
3098 /// @brief Clone an identical IntToPtrInst
3099 virtual CastInst *clone() const;
3100
3101 // Methods for support type inquiry through isa, cast, and dyn_cast:
3102 static inline bool classof(const IntToPtrInst *) { return true; }
3103 static inline bool classof(const Instruction *I) {
3104 return I->getOpcode() == IntToPtr;
3105 }
3106 static inline bool classof(const Value *V) {
3107 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3108 }
3109};
3110
3111//===----------------------------------------------------------------------===//
3112// PtrToIntInst Class
3113//===----------------------------------------------------------------------===//
3114
3115/// @brief This class represents a cast from a pointer to an integer
3116class PtrToIntInst : public CastInst {
3117 PtrToIntInst(const PtrToIntInst &CI)
3118 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3119 }
3120public:
3121 /// @brief Constructor with insert-before-instruction semantics
3122 PtrToIntInst(
3123 Value *S, ///< The value to be converted
3124 const Type *Ty, ///< The type to convert to
3125 const std::string &Name = "", ///< A name for the new instruction
3126 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3127 );
3128
3129 /// @brief Constructor with insert-at-end-of-block semantics
3130 PtrToIntInst(
3131 Value *S, ///< The value to be converted
3132 const Type *Ty, ///< The type to convert to
3133 const std::string &Name, ///< A name for the new instruction
3134 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3135 );
3136
3137 /// @brief Clone an identical PtrToIntInst
3138 virtual CastInst *clone() const;
3139
3140 // Methods for support type inquiry through isa, cast, and dyn_cast:
3141 static inline bool classof(const PtrToIntInst *) { return true; }
3142 static inline bool classof(const Instruction *I) {
3143 return I->getOpcode() == PtrToInt;
3144 }
3145 static inline bool classof(const Value *V) {
3146 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3147 }
3148};
3149
3150//===----------------------------------------------------------------------===//
3151// BitCastInst Class
3152//===----------------------------------------------------------------------===//
3153
3154/// @brief This class represents a no-op cast from one type to another.
3155class BitCastInst : public CastInst {
3156 BitCastInst(const BitCastInst &CI)
3157 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3158 }
3159public:
3160 /// @brief Constructor with insert-before-instruction semantics
3161 BitCastInst(
3162 Value *S, ///< The value to be casted
3163 const Type *Ty, ///< The type to casted to
3164 const std::string &Name = "", ///< A name for the new instruction
3165 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3166 );
3167
3168 /// @brief Constructor with insert-at-end-of-block semantics
3169 BitCastInst(
3170 Value *S, ///< The value to be casted
3171 const Type *Ty, ///< The type to casted to
3172 const std::string &Name, ///< A name for the new instruction
3173 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3174 );
3175
3176 /// @brief Clone an identical BitCastInst
3177 virtual CastInst *clone() const;
3178
3179 // Methods for support type inquiry through isa, cast, and dyn_cast:
3180 static inline bool classof(const BitCastInst *) { return true; }
3181 static inline bool classof(const Instruction *I) {
3182 return I->getOpcode() == BitCast;
3183 }
3184 static inline bool classof(const Value *V) {
3185 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3186 }
3187};
3188
Devang Patel40a04212008-02-19 22:15:16 +00003189//===----------------------------------------------------------------------===//
3190// GetResultInst Class
3191//===----------------------------------------------------------------------===//
3192
3193/// GetResultInst - This instruction extracts individual result value from
3194/// aggregate value, where aggregate value is returned by CallInst.
3195///
Gabor Greifd6a22182008-05-13 07:09:08 +00003196class GetResultInst : public UnaryInstruction {
Devang Patel23755d82008-02-20 19:10:47 +00003197 unsigned Idx;
Devang Patel40a04212008-02-19 22:15:16 +00003198 GetResultInst(const GetResultInst &GRI) :
Gabor Greifd6a22182008-05-13 07:09:08 +00003199 UnaryInstruction(GRI.getType(), Instruction::GetResult, GRI.getOperand(0)),
3200 Idx(GRI.Idx) {
Devang Patel40a04212008-02-19 22:15:16 +00003201 }
3202
3203public:
Gabor Greifefe65362008-05-10 08:32:32 +00003204 GetResultInst(Value *Aggr, unsigned index,
3205 const std::string &Name = "",
3206 Instruction *InsertBefore = 0);
Devang Patel40a04212008-02-19 22:15:16 +00003207
3208 /// isValidOperands - Return true if an getresult instruction can be
3209 /// formed with the specified operands.
Devang Patel23755d82008-02-20 19:10:47 +00003210 static bool isValidOperands(const Value *Aggr, unsigned index);
Devang Patel40a04212008-02-19 22:15:16 +00003211
3212 virtual GetResultInst *clone() const;
3213
Devang Patel4d4a5e02008-02-23 01:11:02 +00003214 Value *getAggregateValue() {
Devang Patel40a04212008-02-19 22:15:16 +00003215 return getOperand(0);
3216 }
Devang Patel2d2ae342008-02-20 18:36:16 +00003217
Devang Patel4d4a5e02008-02-23 01:11:02 +00003218 const Value *getAggregateValue() const {
Devang Patel2d2ae342008-02-20 18:36:16 +00003219 return getOperand(0);
3220 }
3221
Devang Patel4d4a5e02008-02-23 01:11:02 +00003222 unsigned getIndex() const {
Devang Patel23755d82008-02-20 19:10:47 +00003223 return Idx;
Devang Patel40a04212008-02-19 22:15:16 +00003224 }
3225
Devang Patel40a04212008-02-19 22:15:16 +00003226 // Methods for support type inquiry through isa, cast, and dyn_cast:
3227 static inline bool classof(const GetResultInst *) { return true; }
3228 static inline bool classof(const Instruction *I) {
3229 return (I->getOpcode() == Instruction::GetResult);
3230 }
3231 static inline bool classof(const Value *V) {
3232 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3233 }
3234};
3235
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003236} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00003237
3238#endif