blob: 3474429f7628079e045d6935fa1762dc877d7270 [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"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000024
25namespace llvm {
26
Chris Lattner1fca5ff2004-10-27 16:14:51 +000027class BasicBlock;
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;
Reid Spencer4746ecf2007-04-09 15:01:12 +000033class ParamAttrsList;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000034
35//===----------------------------------------------------------------------===//
36// AllocationInst Class
37//===----------------------------------------------------------------------===//
38
39/// AllocationInst - This class is the common base class of MallocInst and
40/// AllocaInst.
41///
Chris Lattner454928e2005-01-29 00:31:36 +000042class AllocationInst : public UnaryInstruction {
Nate Begeman14b05292005-11-05 09:21:28 +000043 unsigned Alignment;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000044protected:
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 = "", Instruction *InsertBefore = 0);
Nate Begeman14b05292005-11-05 09:21:28 +000047 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
Misha Brukman91102862005-03-16 03:46:55 +000048 const std::string &Name, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000049public:
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000050 // Out of line virtual method, so the vtable, etc has a home.
51 virtual ~AllocationInst();
52
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000053 /// isArrayAllocation - Return true if there is an allocation size parameter
54 /// to the allocation instruction that is not 1.
55 ///
56 bool isArrayAllocation() const;
57
58 /// getArraySize - Get the number of element allocated, for a simple
59 /// allocation of a single element, this will return a constant 1 value.
60 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000061 const Value *getArraySize() const { return getOperand(0); }
62 Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000063
64 /// getType - Overload to return most specific pointer type
65 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000066 const PointerType *getType() const {
Misha Brukman9769ab22005-04-21 20:19:05 +000067 return reinterpret_cast<const PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000068 }
69
70 /// getAllocatedType - Return the type that is being allocated by the
71 /// instruction.
72 ///
73 const Type *getAllocatedType() const;
74
Nate Begeman14b05292005-11-05 09:21:28 +000075 /// getAlignment - Return the alignment of the memory that is being allocated
76 /// by the instruction.
77 ///
78 unsigned getAlignment() const { return Alignment; }
Chris Lattner8ae779d2005-11-05 21:58:30 +000079 void setAlignment(unsigned Align) {
80 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
81 Alignment = Align;
82 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +000083
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000084 virtual Instruction *clone() const = 0;
85
86 // Methods for support type inquiry through isa, cast, and dyn_cast:
87 static inline bool classof(const AllocationInst *) { return true; }
88 static inline bool classof(const Instruction *I) {
89 return I->getOpcode() == Instruction::Alloca ||
90 I->getOpcode() == Instruction::Malloc;
91 }
92 static inline bool classof(const Value *V) {
93 return isa<Instruction>(V) && classof(cast<Instruction>(V));
94 }
95};
96
97
98//===----------------------------------------------------------------------===//
99// MallocInst Class
100//===----------------------------------------------------------------------===//
101
102/// MallocInst - an instruction to allocated memory on the heap
103///
104class MallocInst : public AllocationInst {
105 MallocInst(const MallocInst &MI);
106public:
107 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
108 const std::string &Name = "",
109 Instruction *InsertBefore = 0)
Nate Begeman14b05292005-11-05 09:21:28 +0000110 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000111 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
112 BasicBlock *InsertAtEnd)
Nate Begeman14b05292005-11-05 09:21:28 +0000113 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000114
115 MallocInst(const Type *Ty, const std::string &Name,
116 Instruction *InsertBefore = 0)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000117 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
118 MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
119 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000120
121 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Nate Begeman14b05292005-11-05 09:21:28 +0000122 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000123 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
124 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
Nate Begeman14b05292005-11-05 09:21:28 +0000125 const std::string &Name = "",
126 Instruction *InsertBefore = 0)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000127 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000128
Chris Lattnerf319e832004-10-15 23:52:05 +0000129 virtual MallocInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000130
131 // Methods for support type inquiry through isa, cast, and dyn_cast:
132 static inline bool classof(const MallocInst *) { return true; }
133 static inline bool classof(const Instruction *I) {
134 return (I->getOpcode() == Instruction::Malloc);
135 }
136 static inline bool classof(const Value *V) {
137 return isa<Instruction>(V) && classof(cast<Instruction>(V));
138 }
139};
140
141
142//===----------------------------------------------------------------------===//
143// AllocaInst Class
144//===----------------------------------------------------------------------===//
145
146/// AllocaInst - an instruction to allocate memory on the stack
147///
148class AllocaInst : public AllocationInst {
149 AllocaInst(const AllocaInst &);
150public:
151 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
152 const std::string &Name = "",
153 Instruction *InsertBefore = 0)
Nate Begeman14b05292005-11-05 09:21:28 +0000154 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000155 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
156 BasicBlock *InsertAtEnd)
Nate Begeman14b05292005-11-05 09:21:28 +0000157 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
Chris Lattnerb77780e2006-05-10 04:38:35 +0000158
159 AllocaInst(const Type *Ty, const std::string &Name,
160 Instruction *InsertBefore = 0)
161 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
162 AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
163 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000164
Chris Lattnerb77780e2006-05-10 04:38:35 +0000165 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
166 const std::string &Name = "", Instruction *InsertBefore = 0)
167 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
Nate Begeman14b05292005-11-05 09:21:28 +0000168 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
169 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattnerb77780e2006-05-10 04:38:35 +0000170 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000171
Chris Lattnerf319e832004-10-15 23:52:05 +0000172 virtual AllocaInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000173
174 // Methods for support type inquiry through isa, cast, and dyn_cast:
175 static inline bool classof(const AllocaInst *) { return true; }
176 static inline bool classof(const Instruction *I) {
177 return (I->getOpcode() == Instruction::Alloca);
178 }
179 static inline bool classof(const Value *V) {
180 return isa<Instruction>(V) && classof(cast<Instruction>(V));
181 }
182};
183
184
185//===----------------------------------------------------------------------===//
186// FreeInst Class
187//===----------------------------------------------------------------------===//
188
189/// FreeInst - an instruction to deallocate memory
190///
Chris Lattner454928e2005-01-29 00:31:36 +0000191class FreeInst : public UnaryInstruction {
192 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000193public:
194 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
195 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
196
Chris Lattnerf319e832004-10-15 23:52:05 +0000197 virtual FreeInst *clone() const;
Owen Andersonc9edf0b2007-07-06 23:13:31 +0000198
199 // Accessor methods for consistency with other memory operations
200 Value *getPointerOperand() { return getOperand(0); }
201 const Value *getPointerOperand() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000202
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000203 // Methods for support type inquiry through isa, cast, and dyn_cast:
204 static inline bool classof(const FreeInst *) { return true; }
205 static inline bool classof(const Instruction *I) {
206 return (I->getOpcode() == Instruction::Free);
207 }
208 static inline bool classof(const Value *V) {
209 return isa<Instruction>(V) && classof(cast<Instruction>(V));
210 }
211};
212
213
214//===----------------------------------------------------------------------===//
215// LoadInst Class
216//===----------------------------------------------------------------------===//
217
Chris Lattner88fe29a2005-02-05 01:44:18 +0000218/// LoadInst - an instruction for reading from memory. This uses the
219/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000220///
Chris Lattner454928e2005-01-29 00:31:36 +0000221class LoadInst : public UnaryInstruction {
Christopher Lamb43c7f372007-04-22 19:24:39 +0000222
Chris Lattner454928e2005-01-29 00:31:36 +0000223 LoadInst(const LoadInst &LI)
Chris Lattner88fe29a2005-02-05 01:44:18 +0000224 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
225 setVolatile(LI.isVolatile());
Christopher Lamb43c7f372007-04-22 19:24:39 +0000226 setAlignment(LI.getAlignment());
Misha Brukman9769ab22005-04-21 20:19:05 +0000227
Chris Lattner454928e2005-01-29 00:31:36 +0000228#ifndef NDEBUG
229 AssertOK();
230#endif
231 }
232 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000233public:
234 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
235 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000236 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile = false,
237 Instruction *InsertBefore = 0);
238 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
Chris Lattnerf00042a2007-02-13 07:54:42 +0000239 Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000240 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
241 BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000242 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
243 BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000244
Chris Lattnerf00042a2007-02-13 07:54:42 +0000245 LoadInst(Value *Ptr, const char *Name, Instruction *InsertBefore);
246 LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAtEnd);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000247 explicit LoadInst(Value *Ptr, const char *Name = 0, bool isVolatile = false,
Chris Lattnerf00042a2007-02-13 07:54:42 +0000248 Instruction *InsertBefore = 0);
249 LoadInst(Value *Ptr, const char *Name, bool isVolatile,
250 BasicBlock *InsertAtEnd);
251
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000252 /// isVolatile - Return true if this is a load from a volatile memory
253 /// location.
254 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000255 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000256
257 /// setVolatile - Specify whether this is a volatile load or not.
258 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000259 void setVolatile(bool V) {
Hartmut Kaiserefd4a512007-10-17 14:56:40 +0000260 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000261 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000262
Chris Lattnerf319e832004-10-15 23:52:05 +0000263 virtual LoadInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000264
Christopher Lamb43c7f372007-04-22 19:24:39 +0000265 /// getAlignment - Return the alignment of the access that is being performed
266 ///
267 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000268 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000269 }
270
271 void setAlignment(unsigned Align);
272
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000273 Value *getPointerOperand() { return getOperand(0); }
274 const Value *getPointerOperand() const { return getOperand(0); }
275 static unsigned getPointerOperandIndex() { return 0U; }
276
277 // Methods for support type inquiry through isa, cast, and dyn_cast:
278 static inline bool classof(const LoadInst *) { return true; }
279 static inline bool classof(const Instruction *I) {
280 return I->getOpcode() == Instruction::Load;
281 }
282 static inline bool classof(const Value *V) {
283 return isa<Instruction>(V) && classof(cast<Instruction>(V));
284 }
285};
286
287
288//===----------------------------------------------------------------------===//
289// StoreInst Class
290//===----------------------------------------------------------------------===//
291
Misha Brukman9769ab22005-04-21 20:19:05 +0000292/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000293///
294class StoreInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000295 Use Ops[2];
Christopher Lamb43c7f372007-04-22 19:24:39 +0000296
Chris Lattner88fe29a2005-02-05 01:44:18 +0000297 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
Chris Lattner454928e2005-01-29 00:31:36 +0000298 Ops[0].init(SI.Ops[0], this);
299 Ops[1].init(SI.Ops[1], this);
Chris Lattner88fe29a2005-02-05 01:44:18 +0000300 setVolatile(SI.isVolatile());
Christopher Lamb43c7f372007-04-22 19:24:39 +0000301 setAlignment(SI.getAlignment());
302
Chris Lattner454928e2005-01-29 00:31:36 +0000303#ifndef NDEBUG
304 AssertOK();
305#endif
306 }
307 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000308public:
309 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.
Misha Brukman9769ab22005-04-21 20:19:05 +0000332 Value *getOperand(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +0000333 assert(i < 2 && "getOperand() out of range!");
334 return Ops[i];
335 }
336 void setOperand(unsigned i, Value *Val) {
337 assert(i < 2 && "setOperand() out of range!");
338 Ops[i] = Val;
339 }
340 unsigned getNumOperands() const { return 2; }
341
Christopher Lamb43c7f372007-04-22 19:24:39 +0000342 /// getAlignment - Return the alignment of the access that is being performed
343 ///
344 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000345 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000346 }
347
348 void setAlignment(unsigned Align);
349
Chris Lattnerf319e832004-10-15 23:52:05 +0000350 virtual StoreInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000351
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000352 Value *getPointerOperand() { return getOperand(1); }
353 const Value *getPointerOperand() const { return getOperand(1); }
354 static unsigned getPointerOperandIndex() { return 1U; }
355
356 // Methods for support type inquiry through isa, cast, and dyn_cast:
357 static inline bool classof(const StoreInst *) { return true; }
358 static inline bool classof(const Instruction *I) {
359 return I->getOpcode() == Instruction::Store;
360 }
361 static inline bool classof(const Value *V) {
362 return isa<Instruction>(V) && classof(cast<Instruction>(V));
363 }
364};
365
366
367//===----------------------------------------------------------------------===//
368// GetElementPtrInst Class
369//===----------------------------------------------------------------------===//
370
David Greeneb8f74792007-09-04 15:46:09 +0000371// checkType - Simple wrapper function to give a better assertion failure
372// message on bad indexes for a gep instruction.
373//
374static inline const Type *checkType(const Type *Ty) {
375 assert(Ty && "Invalid GetElementPtrInst indices for type!");
376 return Ty;
377}
378
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000379/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
380/// access elements of arrays and structs
381///
382class GetElementPtrInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000383 GetElementPtrInst(const GetElementPtrInst &GEPI)
384 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
385 0, GEPI.getNumOperands()) {
386 Use *OL = OperandList = new Use[NumOperands];
387 Use *GEPIOL = GEPI.OperandList;
388 for (unsigned i = 0, E = NumOperands; i != E; ++i)
389 OL[i].init(GEPIOL[i], this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000390 }
Chris Lattner6ffbe172007-01-31 19:47:18 +0000391 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx);
Chris Lattner38bacf22005-05-03 05:43:30 +0000392 void init(Value *Ptr, Value *Idx);
David Greeneb8f74792007-09-04 15:46:09 +0000393
394 template<typename InputIterator>
395 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
396 const std::string &Name,
397 // This argument ensures that we have an iterator we can
398 // do arithmetic on in constant time
399 std::random_access_iterator_tag) {
400 typename std::iterator_traits<InputIterator>::difference_type NumIdx =
401 std::distance(IdxBegin, IdxEnd);
402
403 if (NumIdx > 0) {
404 // This requires that the itoerator points to contiguous memory.
405 init(Ptr, &*IdxBegin, NumIdx);
406 }
407 else {
408 init(Ptr, 0, NumIdx);
409 }
410
411 setName(Name);
412 }
413
414 /// getIndexedType - Returns the type of the element that would be loaded with
415 /// a load instruction with the specified parameters.
416 ///
417 /// A null type is returned if the indices are invalid for the specified
418 /// pointer type.
419 ///
420 static const Type *getIndexedType(const Type *Ptr,
421 Value* const *Idx, unsigned NumIdx,
422 bool AllowStructLeaf = false);
423
424 template<typename InputIterator>
425 static const Type *getIndexedType(const Type *Ptr,
426 InputIterator IdxBegin,
427 InputIterator IdxEnd,
428 bool AllowStructLeaf,
429 // This argument ensures that we
430 // have an iterator we can do
431 // arithmetic on in constant time
432 std::random_access_iterator_tag) {
433 typename std::iterator_traits<InputIterator>::difference_type NumIdx =
434 std::distance(IdxBegin, IdxEnd);
435
436 if (NumIdx > 0) {
437 // This requires that the iterator points to contiguous memory.
438 return(getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx,
439 AllowStructLeaf));
440 }
441 else {
442 return(getIndexedType(Ptr, (Value *const*)0, NumIdx, AllowStructLeaf));
443 }
444 }
445
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000446public:
447 /// Constructors - Create a getelementptr instruction with a base pointer an
448 /// list of indices. The first ctor can optionally insert before an existing
449 /// instruction, the second appends the new instruction to the specified
450 /// BasicBlock.
David Greeneb8f74792007-09-04 15:46:09 +0000451 template<typename InputIterator>
452 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
453 InputIterator IdxEnd,
454 const std::string &Name = "",
455 Instruction *InsertBefore =0)
456 : Instruction(PointerType::get(
457 checkType(getIndexedType(Ptr->getType(),
Christopher Lambfe63fb92007-12-11 08:59:05 +0000458 IdxBegin, IdxEnd, true)),
459 cast<PointerType>(Ptr->getType())->getAddressSpace()),
David Greeneb8f74792007-09-04 15:46:09 +0000460 GetElementPtr, 0, 0, InsertBefore) {
461 init(Ptr, IdxBegin, IdxEnd, Name,
462 typename std::iterator_traits<InputIterator>::iterator_category());
463 }
464 template<typename InputIterator>
465 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
466 const std::string &Name, BasicBlock *InsertAtEnd)
467 : Instruction(PointerType::get(
468 checkType(getIndexedType(Ptr->getType(),
Christopher Lambfe63fb92007-12-11 08:59:05 +0000469 IdxBegin, IdxEnd, true)),
470 cast<PointerType>(Ptr->getType())->getAddressSpace()),
David Greeneb8f74792007-09-04 15:46:09 +0000471 GetElementPtr, 0, 0, InsertAtEnd) {
472 init(Ptr, IdxBegin, IdxEnd, Name,
473 typename std::iterator_traits<InputIterator>::iterator_category());
474 }
475
Chris Lattner38bacf22005-05-03 05:43:30 +0000476 /// Constructors - These two constructors are convenience methods because one
477 /// and two index getelementptr instructions are so common.
478 GetElementPtrInst(Value *Ptr, Value *Idx,
479 const std::string &Name = "", Instruction *InsertBefore =0);
480 GetElementPtrInst(Value *Ptr, Value *Idx,
481 const std::string &Name, BasicBlock *InsertAtEnd);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000482 ~GetElementPtrInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000483
Chris Lattnerf319e832004-10-15 23:52:05 +0000484 virtual GetElementPtrInst *clone() const;
Misha Brukman9769ab22005-04-21 20:19:05 +0000485
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000486 // getType - Overload to return most specific pointer type...
Devang Patel4d4a5e02008-02-23 01:11:02 +0000487 const PointerType *getType() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000488 return reinterpret_cast<const PointerType*>(Instruction::getType());
489 }
490
491 /// getIndexedType - Returns the type of the element that would be loaded with
492 /// a load instruction with the specified parameters.
493 ///
Misha Brukman9769ab22005-04-21 20:19:05 +0000494 /// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000495 /// pointer type.
496 ///
David Greeneb8f74792007-09-04 15:46:09 +0000497 template<typename InputIterator>
Misha Brukman9769ab22005-04-21 20:19:05 +0000498 static const Type *getIndexedType(const Type *Ptr,
David Greeneb8f74792007-09-04 15:46:09 +0000499 InputIterator IdxBegin,
500 InputIterator IdxEnd,
501 bool AllowStructLeaf = false) {
502 return(getIndexedType(Ptr, IdxBegin, IdxEnd, AllowStructLeaf,
503 typename std::iterator_traits<InputIterator>::
504 iterator_category()));
505 }
Chris Lattner38bacf22005-05-03 05:43:30 +0000506 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
Misha Brukman9769ab22005-04-21 20:19:05 +0000507
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000508 inline op_iterator idx_begin() { return op_begin()+1; }
509 inline const_op_iterator idx_begin() const { return op_begin()+1; }
510 inline op_iterator idx_end() { return op_end(); }
511 inline const_op_iterator idx_end() const { return op_end(); }
512
513 Value *getPointerOperand() {
514 return getOperand(0);
515 }
516 const Value *getPointerOperand() const {
517 return getOperand(0);
518 }
519 static unsigned getPointerOperandIndex() {
520 return 0U; // get index for modifying correct operand
521 }
522
Devang Patel4d4a5e02008-02-23 01:11:02 +0000523 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000524 return getNumOperands() - 1;
525 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000526
Devang Patel4d4a5e02008-02-23 01:11:02 +0000527 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000528 return getNumOperands() > 1;
529 }
Chris Lattner6f771d42007-04-14 00:12:57 +0000530
531 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
532 /// zeros. If so, the result pointer and the first operand have the same
533 /// value, just potentially different types.
534 bool hasAllZeroIndices() const;
Chris Lattner6b0974c2007-04-27 20:35:56 +0000535
536 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
537 /// constant integers. If so, the result pointer and the first operand have
538 /// a constant offset between them.
539 bool hasAllConstantIndices() const;
540
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000541
542 // Methods for support type inquiry through isa, cast, and dyn_cast:
543 static inline bool classof(const GetElementPtrInst *) { return true; }
544 static inline bool classof(const Instruction *I) {
545 return (I->getOpcode() == Instruction::GetElementPtr);
546 }
547 static inline bool classof(const Value *V) {
548 return isa<Instruction>(V) && classof(cast<Instruction>(V));
549 }
550};
551
552//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000553// ICmpInst Class
554//===----------------------------------------------------------------------===//
555
556/// This instruction compares its operands according to the predicate given
557/// to the constructor. It only operates on integers, pointers, or packed
558/// vectors of integrals. The two operands must be the same type.
559/// @brief Represent an integer comparison operator.
560class ICmpInst: public CmpInst {
561public:
562 /// This enumeration lists the possible predicates for the ICmpInst. The
563 /// values in the range 0-31 are reserved for FCmpInst while values in the
564 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
565 /// predicate values are not overlapping between the classes.
566 enum Predicate {
567 ICMP_EQ = 32, ///< equal
568 ICMP_NE = 33, ///< not equal
569 ICMP_UGT = 34, ///< unsigned greater than
570 ICMP_UGE = 35, ///< unsigned greater or equal
571 ICMP_ULT = 36, ///< unsigned less than
572 ICMP_ULE = 37, ///< unsigned less or equal
573 ICMP_SGT = 38, ///< signed greater than
574 ICMP_SGE = 39, ///< signed greater or equal
575 ICMP_SLT = 40, ///< signed less than
576 ICMP_SLE = 41, ///< signed less or equal
577 FIRST_ICMP_PREDICATE = ICMP_EQ,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000578 LAST_ICMP_PREDICATE = ICMP_SLE,
579 BAD_ICMP_PREDICATE = ICMP_SLE + 1
Reid Spencer45fb3f32006-11-20 01:22:35 +0000580 };
581
582 /// @brief Constructor with insert-before-instruction semantics.
583 ICmpInst(
584 Predicate pred, ///< The predicate to use for the comparison
585 Value *LHS, ///< The left-hand-side of the expression
586 Value *RHS, ///< The right-hand-side of the expression
587 const std::string &Name = "", ///< Name of the instruction
588 Instruction *InsertBefore = 0 ///< Where to insert
589 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertBefore) {
590 }
591
592 /// @brief Constructor with insert-at-block-end semantics.
593 ICmpInst(
594 Predicate pred, ///< The predicate to use for the comparison
595 Value *LHS, ///< The left-hand-side of the expression
596 Value *RHS, ///< The right-hand-side of the expression
597 const std::string &Name, ///< Name of the instruction
598 BasicBlock *InsertAtEnd ///< Block to insert into.
599 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertAtEnd) {
600 }
601
602 /// @brief Return the predicate for this instruction.
603 Predicate getPredicate() const { return Predicate(SubclassData); }
604
Chris Lattnerb769d562007-01-14 19:41:24 +0000605 /// @brief Set the predicate for this instruction to the specified value.
606 void setPredicate(Predicate P) { SubclassData = P; }
607
Reid Spencer45fb3f32006-11-20 01:22:35 +0000608 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
609 /// @returns the inverse predicate for the instruction's current predicate.
610 /// @brief Return the inverse of the instruction's predicate.
611 Predicate getInversePredicate() const {
612 return getInversePredicate(getPredicate());
613 }
614
615 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
616 /// @returns the inverse predicate for predicate provided in \p pred.
617 /// @brief Return the inverse of a given predicate
618 static Predicate getInversePredicate(Predicate pred);
619
620 /// For example, EQ->EQ, SLE->SGE, ULT->UGT, etc.
621 /// @returns the predicate that would be the result of exchanging the two
622 /// operands of the ICmpInst instruction without changing the result
623 /// produced.
624 /// @brief Return the predicate as if the operands were swapped
625 Predicate getSwappedPredicate() const {
626 return getSwappedPredicate(getPredicate());
627 }
628
629 /// This is a static version that you can use without an instruction
630 /// available.
631 /// @brief Return the predicate as if the operands were swapped.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000632 static Predicate getSwappedPredicate(Predicate pred);
633
634 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
635 /// @returns the predicate that would be the result if the operand were
636 /// regarded as signed.
637 /// @brief Return the signed version of the predicate
638 Predicate getSignedPredicate() const {
639 return getSignedPredicate(getPredicate());
640 }
641
642 /// This is a static version that you can use without an instruction.
643 /// @brief Return the signed version of the predicate.
644 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000645
Nick Lewycky4189a532008-01-28 03:48:02 +0000646 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
647 /// @returns the predicate that would be the result if the operand were
648 /// regarded as unsigned.
649 /// @brief Return the unsigned version of the predicate
650 Predicate getUnsignedPredicate() const {
651 return getUnsignedPredicate(getPredicate());
652 }
653
654 /// This is a static version that you can use without an instruction.
655 /// @brief Return the unsigned version of the predicate.
656 static Predicate getUnsignedPredicate(Predicate pred);
657
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000658 /// isEquality - Return true if this predicate is either EQ or NE. This also
659 /// tests for commutativity.
660 static bool isEquality(Predicate P) {
661 return P == ICMP_EQ || P == ICMP_NE;
662 }
663
664 /// isEquality - Return true if this predicate is either EQ or NE. This also
665 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000666 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000667 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000668 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000669
670 /// @returns true if the predicate of this ICmpInst is commutative
671 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000672 bool isCommutative() const { return isEquality(); }
673
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000674 /// isRelational - Return true if the predicate is relational (not EQ or NE).
675 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +0000676 bool isRelational() const {
677 return !isEquality();
678 }
679
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000680 /// isRelational - Return true if the predicate is relational (not EQ or NE).
681 ///
682 static bool isRelational(Predicate P) {
683 return !isEquality(P);
684 }
685
Reid Spencere4d87aa2006-12-23 06:05:41 +0000686 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
687 /// @brief Determine if this instruction's predicate is signed.
Chris Lattner5bda9e42007-09-15 06:51:03 +0000688 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000689
690 /// @returns true if the predicate provided is signed, false otherwise
691 /// @brief Determine if the predicate is signed.
692 static bool isSignedPredicate(Predicate pred);
693
Reid Spencer3da43842007-02-28 22:00:54 +0000694 /// Initialize a set of values that all satisfy the predicate with C.
695 /// @brief Make a ConstantRange for a relation with a constant value.
696 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
697
Reid Spencer45fb3f32006-11-20 01:22:35 +0000698 /// Exchange the two operands to this instruction in such a way that it does
699 /// not modify the semantics of the instruction. The predicate value may be
700 /// changed to retain the same result if the predicate is order dependent
701 /// (e.g. ult).
702 /// @brief Swap operands and adjust predicate.
703 void swapOperands() {
704 SubclassData = getSwappedPredicate();
705 std::swap(Ops[0], Ops[1]);
706 }
707
Chris Lattnercd406fe2007-08-24 20:48:18 +0000708 virtual ICmpInst *clone() const;
709
Reid Spencer45fb3f32006-11-20 01:22:35 +0000710 // Methods for support type inquiry through isa, cast, and dyn_cast:
711 static inline bool classof(const ICmpInst *) { return true; }
712 static inline bool classof(const Instruction *I) {
713 return I->getOpcode() == Instruction::ICmp;
714 }
715 static inline bool classof(const Value *V) {
716 return isa<Instruction>(V) && classof(cast<Instruction>(V));
717 }
718};
719
720//===----------------------------------------------------------------------===//
721// FCmpInst Class
722//===----------------------------------------------------------------------===//
723
724/// This instruction compares its operands according to the predicate given
725/// to the constructor. It only operates on floating point values or packed
726/// vectors of floating point values. The operands must be identical types.
727/// @brief Represents a floating point comparison operator.
728class FCmpInst: public CmpInst {
729public:
730 /// This enumeration lists the possible predicates for the FCmpInst. Values
731 /// in the range 0-31 are reserved for FCmpInst.
732 enum Predicate {
733 // Opcode U L G E Intuitive operation
734 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
735 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
736 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
737 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
738 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
739 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
740 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
741 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
742 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
743 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
744 FCMP_UGT =10, ///< 1 0 1 0 True if unordered or greater than
745 FCMP_UGE =11, ///< 1 0 1 1 True if unordered, greater than, or equal
746 FCMP_ULT =12, ///< 1 1 0 0 True if unordered or less than
747 FCMP_ULE =13, ///< 1 1 0 1 True if unordered, less than, or equal
748 FCMP_UNE =14, ///< 1 1 1 0 True if unordered or not equal
749 FCMP_TRUE =15, ///< 1 1 1 1 Always true (always folded)
750 FIRST_FCMP_PREDICATE = FCMP_FALSE,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000751 LAST_FCMP_PREDICATE = FCMP_TRUE,
752 BAD_FCMP_PREDICATE = FCMP_TRUE + 1
Reid Spencer45fb3f32006-11-20 01:22:35 +0000753 };
754
755 /// @brief Constructor with insert-before-instruction semantics.
756 FCmpInst(
757 Predicate pred, ///< The predicate to use for the comparison
758 Value *LHS, ///< The left-hand-side of the expression
759 Value *RHS, ///< The right-hand-side of the expression
760 const std::string &Name = "", ///< Name of the instruction
761 Instruction *InsertBefore = 0 ///< Where to insert
762 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertBefore) {
763 }
764
765 /// @brief Constructor with insert-at-block-end semantics.
766 FCmpInst(
767 Predicate pred, ///< The predicate to use for the comparison
768 Value *LHS, ///< The left-hand-side of the expression
769 Value *RHS, ///< The right-hand-side of the expression
770 const std::string &Name, ///< Name of the instruction
771 BasicBlock *InsertAtEnd ///< Block to insert into.
772 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertAtEnd) {
773 }
774
775 /// @brief Return the predicate for this instruction.
776 Predicate getPredicate() const { return Predicate(SubclassData); }
777
Chris Lattnerb769d562007-01-14 19:41:24 +0000778 /// @brief Set the predicate for this instruction to the specified value.
779 void setPredicate(Predicate P) { SubclassData = P; }
780
Reid Spencer45fb3f32006-11-20 01:22:35 +0000781 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
782 /// @returns the inverse predicate for the instructions current predicate.
783 /// @brief Return the inverse of the predicate
784 Predicate getInversePredicate() const {
785 return getInversePredicate(getPredicate());
786 }
787
788 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
789 /// @returns the inverse predicate for \p pred.
790 /// @brief Return the inverse of a given predicate
791 static Predicate getInversePredicate(Predicate pred);
792
793 /// For example, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
794 /// @returns the predicate that would be the result of exchanging the two
795 /// operands of the ICmpInst instruction without changing the result
796 /// produced.
797 /// @brief Return the predicate as if the operands were swapped
798 Predicate getSwappedPredicate() const {
799 return getSwappedPredicate(getPredicate());
800 }
801
802 /// This is a static version that you can use without an instruction
803 /// available.
804 /// @brief Return the predicate as if the operands were swapped.
805 static Predicate getSwappedPredicate(Predicate Opcode);
806
807 /// This also tests for commutativity. If isEquality() returns true then
808 /// the predicate is also commutative. Only the equality predicates are
809 /// commutative.
810 /// @returns true if the predicate of this instruction is EQ or NE.
811 /// @brief Determine if this is an equality predicate.
812 bool isEquality() const {
813 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
814 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
815 }
816 bool isCommutative() const { return isEquality(); }
817
818 /// @returns true if the predicate is relational (not EQ or NE).
819 /// @brief Determine if this a relational predicate.
820 bool isRelational() const { return !isEquality(); }
821
822 /// Exchange the two operands to this instruction in such a way that it does
823 /// not modify the semantics of the instruction. The predicate value may be
824 /// changed to retain the same result if the predicate is order dependent
825 /// (e.g. ult).
826 /// @brief Swap operands and adjust predicate.
827 void swapOperands() {
828 SubclassData = getSwappedPredicate();
829 std::swap(Ops[0], Ops[1]);
830 }
831
Chris Lattnercd406fe2007-08-24 20:48:18 +0000832 virtual FCmpInst *clone() const;
833
Reid Spencer45fb3f32006-11-20 01:22:35 +0000834 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
835 static inline bool classof(const FCmpInst *) { return true; }
836 static inline bool classof(const Instruction *I) {
837 return I->getOpcode() == Instruction::FCmp;
838 }
839 static inline bool classof(const Value *V) {
840 return isa<Instruction>(V) && classof(cast<Instruction>(V));
841 }
842};
843
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000844//===----------------------------------------------------------------------===//
845// CallInst Class
846//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000847/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +0000848/// machine's calling convention. This class uses low bit of the SubClassData
849/// field to indicate whether or not this is a tail call. The rest of the bits
850/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000851///
David Greene52eec542007-08-01 03:43:44 +0000852
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000853class CallInst : public Instruction {
Duncan Sandsdc024672007-11-27 13:23:08 +0000854 const ParamAttrsList *ParamAttrs; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000855 CallInst(const CallInst &CI);
Chris Lattnerd54f4322007-02-13 00:58:44 +0000856 void init(Value *Func, Value* const *Params, unsigned NumParams);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000857 void init(Value *Func, Value *Actual1, Value *Actual2);
858 void init(Value *Func, Value *Actual);
859 void init(Value *Func);
860
David Greene52eec542007-08-01 03:43:44 +0000861 template<typename InputIterator>
862 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
863 const std::string &Name,
864 // This argument ensures that we have an iterator we can
865 // do arithmetic on in constant time
866 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000867 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greene52eec542007-08-01 03:43:44 +0000868
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000869 // This requires that the iterator points to contiguous memory.
870 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
David Greene52eec542007-08-01 03:43:44 +0000871 setName(Name);
872 }
873
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000874public:
David Greene52eec542007-08-01 03:43:44 +0000875 /// Construct a CallInst given a range of arguments. InputIterator
876 /// must be a random-access iterator pointing to contiguous storage
877 /// (e.g. a std::vector<>::iterator). Checks are made for
878 /// random-accessness but not for contiguous storage as that would
879 /// incur runtime overhead.
880 /// @brief Construct a CallInst from a range of arguments
881 template<typename InputIterator>
882 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
883 const std::string &Name = "", Instruction *InsertBefore = 0)
884 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
885 ->getElementType())->getReturnType(),
886 Instruction::Call, 0, 0, InsertBefore) {
887 init(Func, ArgBegin, ArgEnd, Name,
888 typename std::iterator_traits<InputIterator>::iterator_category());
889 }
890
891 /// Construct a CallInst given a range of arguments. InputIterator
892 /// must be a random-access iterator pointing to contiguous storage
893 /// (e.g. a std::vector<>::iterator). Checks are made for
894 /// random-accessness but not for contiguous storage as that would
895 /// incur runtime overhead.
896 /// @brief Construct a CallInst from a range of arguments
897 template<typename InputIterator>
898 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
899 const std::string &Name, BasicBlock *InsertAtEnd)
900 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
901 ->getElementType())->getReturnType(),
902 Instruction::Call, 0, 0, InsertAtEnd) {
903 init(Func, ArgBegin, ArgEnd, Name,
904 typename std::iterator_traits<InputIterator>::iterator_category());
905 }
906
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000907 CallInst(Value *F, Value *Actual, const std::string& Name = "",
908 Instruction *InsertBefore = 0);
909 CallInst(Value *F, Value *Actual, const std::string& Name,
910 BasicBlock *InsertAtEnd);
Misha Brukman9769ab22005-04-21 20:19:05 +0000911 explicit CallInst(Value *F, const std::string &Name = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000912 Instruction *InsertBefore = 0);
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000913 CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000914 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000915
Chris Lattnerf319e832004-10-15 23:52:05 +0000916 virtual CallInst *clone() const;
Chris Lattnerbb5493d2007-02-15 23:15:00 +0000917
Chris Lattner3340ffe2005-05-06 20:26:26 +0000918 bool isTailCall() const { return SubclassData & 1; }
919 void setTailCall(bool isTailCall = true) {
Jeff Cohen39cef602005-05-07 02:44:04 +0000920 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
Chris Lattner3340ffe2005-05-06 20:26:26 +0000921 }
922
923 /// getCallingConv/setCallingConv - Get or set the calling convention of this
924 /// function call.
925 unsigned getCallingConv() const { return SubclassData >> 1; }
926 void setCallingConv(unsigned CC) {
927 SubclassData = (SubclassData & 1) | (CC << 1);
928 }
Chris Lattnerddb6db42005-05-06 05:51:46 +0000929
Reid Spencerfa3e9122007-04-09 18:00:57 +0000930 /// Obtains a pointer to the ParamAttrsList object which holds the
931 /// parameter attributes information, if any.
932 /// @returns 0 if no attributes have been set.
Reid Spencer4746ecf2007-04-09 15:01:12 +0000933 /// @brief Get the parameter attributes.
Duncan Sandsdc024672007-11-27 13:23:08 +0000934 const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
Reid Spencer4746ecf2007-04-09 15:01:12 +0000935
936 /// Sets the parameter attributes for this CallInst. To construct a
937 /// ParamAttrsList, see ParameterAttributes.h
938 /// @brief Set the parameter attributes.
Duncan Sandsdc024672007-11-27 13:23:08 +0000939 void setParamAttrs(const ParamAttrsList *attrs);
940
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000941 /// @brief Determine whether the call or the callee has the given attribute.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000942 bool paramHasAttr(uint16_t i, unsigned attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000943
Dale Johannesen08e78b12008-02-22 17:49:45 +0000944 /// @brief Extract the alignment for a call or parameter (0=unknown).
945 uint16_t getParamAlignment(uint16_t i) const;
946
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000947 /// @brief Determine if the call does not access memory.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000948 bool doesNotAccessMemory() const;
949
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000950 /// @brief Determine if the call does not access or only reads memory.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000951 bool onlyReadsMemory() const;
952
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000953 /// @brief Determine if the call cannot return.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000954 bool doesNotReturn() const;
Duncan Sandscbb8bad2007-12-10 19:09:40 +0000955
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000956 /// @brief Determine if the call cannot unwind.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000957 bool doesNotThrow() const;
Duncan Sandsf0c33542007-12-19 21:13:37 +0000958 void setDoesNotThrow(bool doesNotThrow = true);
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000959
Duncan Sandsdc024672007-11-27 13:23:08 +0000960 /// @brief Determine if the call returns a structure.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000961 bool isStructReturn() const;
Reid Spencer4746ecf2007-04-09 15:01:12 +0000962
Evan Chengf4a54982008-01-12 18:57:32 +0000963 /// @brief Determine if any call argument is an aggregate passed by value.
964 bool hasByValArgument() const;
965
Chris Lattner721aef62004-11-18 17:46:57 +0000966 /// getCalledFunction - Return the function being called by this instruction
967 /// if it is a direct call. If it is a call through a function pointer,
968 /// return null.
969 Function *getCalledFunction() const {
Dan Gohman11a7dbf2007-09-24 15:46:02 +0000970 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +0000971 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000972
Reid Spencerc25ec252006-12-29 04:10:59 +0000973 /// getCalledValue - Get a pointer to the function that is invoked by this
974 /// instruction
Devang Patel4d4a5e02008-02-23 01:11:02 +0000975 const Value *getCalledValue() const { return getOperand(0); }
976 Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000977
978 // Methods for support type inquiry through isa, cast, and dyn_cast:
979 static inline bool classof(const CallInst *) { return true; }
980 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +0000981 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000982 }
983 static inline bool classof(const Value *V) {
984 return isa<Instruction>(V) && classof(cast<Instruction>(V));
985 }
986};
987
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000988//===----------------------------------------------------------------------===//
989// SelectInst Class
990//===----------------------------------------------------------------------===//
991
992/// SelectInst - This class represents the LLVM 'select' instruction.
993///
994class SelectInst : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000995 Use Ops[3];
996
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000997 void init(Value *C, Value *S1, Value *S2) {
Chris Lattner454928e2005-01-29 00:31:36 +0000998 Ops[0].init(C, this);
999 Ops[1].init(S1, this);
1000 Ops[2].init(S2, this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001001 }
1002
Chris Lattner454928e2005-01-29 00:31:36 +00001003 SelectInst(const SelectInst &SI)
1004 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
1005 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
1006 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001007public:
1008 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
1009 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001010 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001011 init(C, S1, S2);
Chris Lattner910c80a2007-02-24 00:55:48 +00001012 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001013 }
1014 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1015 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001016 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001017 init(C, S1, S2);
Chris Lattner910c80a2007-02-24 00:55:48 +00001018 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001019 }
1020
Chris Lattner454928e2005-01-29 00:31:36 +00001021 Value *getCondition() const { return Ops[0]; }
1022 Value *getTrueValue() const { return Ops[1]; }
1023 Value *getFalseValue() const { return Ops[2]; }
1024
1025 /// Transparently provide more efficient getOperand methods.
1026 Value *getOperand(unsigned i) const {
1027 assert(i < 3 && "getOperand() out of range!");
1028 return Ops[i];
1029 }
1030 void setOperand(unsigned i, Value *Val) {
1031 assert(i < 3 && "setOperand() out of range!");
1032 Ops[i] = Val;
1033 }
1034 unsigned getNumOperands() const { return 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001035
1036 OtherOps getOpcode() const {
1037 return static_cast<OtherOps>(Instruction::getOpcode());
1038 }
1039
Chris Lattnerf319e832004-10-15 23:52:05 +00001040 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001041
1042 // Methods for support type inquiry through isa, cast, and dyn_cast:
1043 static inline bool classof(const SelectInst *) { return true; }
1044 static inline bool classof(const Instruction *I) {
1045 return I->getOpcode() == Instruction::Select;
1046 }
1047 static inline bool classof(const Value *V) {
1048 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1049 }
1050};
1051
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001052//===----------------------------------------------------------------------===//
1053// VAArgInst Class
1054//===----------------------------------------------------------------------===//
1055
1056/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001057/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001058///
Chris Lattner454928e2005-01-29 00:31:36 +00001059class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001060 VAArgInst(const VAArgInst &VAA)
Chris Lattner454928e2005-01-29 00:31:36 +00001061 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001062public:
1063 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
1064 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001065 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001066 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001067 }
1068 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
1069 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001070 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Chris Lattnerf00042a2007-02-13 07:54:42 +00001071 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001072 }
1073
Chris Lattnerf319e832004-10-15 23:52:05 +00001074 virtual VAArgInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001075
1076 // Methods for support type inquiry through isa, cast, and dyn_cast:
1077 static inline bool classof(const VAArgInst *) { return true; }
1078 static inline bool classof(const Instruction *I) {
1079 return I->getOpcode() == VAArg;
1080 }
1081 static inline bool classof(const Value *V) {
1082 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1083 }
1084};
1085
1086//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001087// ExtractElementInst Class
1088//===----------------------------------------------------------------------===//
1089
1090/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001091/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001092///
1093class ExtractElementInst : public Instruction {
1094 Use Ops[2];
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001095 ExtractElementInst(const ExtractElementInst &EE) :
Robert Bocchinof9993442006-01-17 20:05:59 +00001096 Instruction(EE.getType(), ExtractElement, Ops, 2) {
1097 Ops[0].init(EE.Ops[0], this);
1098 Ops[1].init(EE.Ops[1], this);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001099 }
1100
1101public:
Chris Lattner9fc18d22006-04-08 01:15:18 +00001102 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
1103 Instruction *InsertBefore = 0);
Chris Lattner06a248c22006-10-05 06:24:58 +00001104 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
1105 Instruction *InsertBefore = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001106 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
1107 BasicBlock *InsertAtEnd);
Chris Lattner06a248c22006-10-05 06:24:58 +00001108 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
1109 BasicBlock *InsertAtEnd);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001110
Chris Lattnerfa495842006-04-08 04:04:54 +00001111 /// isValidOperands - Return true if an extractelement instruction can be
1112 /// formed with the specified operands.
1113 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001114
Robert Bocchino49b78a52006-01-10 19:04:13 +00001115 virtual ExtractElementInst *clone() const;
1116
Robert Bocchino49b78a52006-01-10 19:04:13 +00001117 /// Transparently provide more efficient getOperand methods.
1118 Value *getOperand(unsigned i) const {
1119 assert(i < 2 && "getOperand() out of range!");
1120 return Ops[i];
1121 }
1122 void setOperand(unsigned i, Value *Val) {
1123 assert(i < 2 && "setOperand() out of range!");
1124 Ops[i] = Val;
1125 }
1126 unsigned getNumOperands() const { return 2; }
1127
1128 // Methods for support type inquiry through isa, cast, and dyn_cast:
1129 static inline bool classof(const ExtractElementInst *) { return true; }
1130 static inline bool classof(const Instruction *I) {
1131 return I->getOpcode() == Instruction::ExtractElement;
1132 }
1133 static inline bool classof(const Value *V) {
1134 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1135 }
1136};
1137
1138//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001139// InsertElementInst Class
1140//===----------------------------------------------------------------------===//
1141
1142/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001143/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001144///
1145class InsertElementInst : public Instruction {
1146 Use Ops[3];
Chris Lattner6a56ed42006-04-14 22:20:07 +00001147 InsertElementInst(const InsertElementInst &IE);
Robert Bocchinof9993442006-01-17 20:05:59 +00001148public:
Chris Lattner9fc18d22006-04-08 01:15:18 +00001149 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1150 const std::string &Name = "",Instruction *InsertBefore = 0);
Chris Lattner06a248c22006-10-05 06:24:58 +00001151 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1152 const std::string &Name = "",Instruction *InsertBefore = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001153 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Robert Bocchinof9993442006-01-17 20:05:59 +00001154 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattner06a248c22006-10-05 06:24:58 +00001155 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1156 const std::string &Name, BasicBlock *InsertAtEnd);
Robert Bocchinof9993442006-01-17 20:05:59 +00001157
Chris Lattnerfa495842006-04-08 04:04:54 +00001158 /// isValidOperands - Return true if an insertelement instruction can be
1159 /// formed with the specified operands.
1160 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1161 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001162
Robert Bocchinof9993442006-01-17 20:05:59 +00001163 virtual InsertElementInst *clone() const;
1164
Reid Spencerac9dcb92007-02-15 03:39:18 +00001165 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001166 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001167 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001168 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001169 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001170
Robert Bocchinof9993442006-01-17 20:05:59 +00001171 /// Transparently provide more efficient getOperand methods.
1172 Value *getOperand(unsigned i) const {
1173 assert(i < 3 && "getOperand() out of range!");
1174 return Ops[i];
1175 }
1176 void setOperand(unsigned i, Value *Val) {
1177 assert(i < 3 && "setOperand() out of range!");
1178 Ops[i] = Val;
1179 }
1180 unsigned getNumOperands() const { return 3; }
1181
1182 // Methods for support type inquiry through isa, cast, and dyn_cast:
1183 static inline bool classof(const InsertElementInst *) { return true; }
1184 static inline bool classof(const Instruction *I) {
1185 return I->getOpcode() == Instruction::InsertElement;
1186 }
1187 static inline bool classof(const Value *V) {
1188 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1189 }
1190};
1191
1192//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001193// ShuffleVectorInst Class
1194//===----------------------------------------------------------------------===//
1195
1196/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1197/// input vectors.
1198///
1199class ShuffleVectorInst : public Instruction {
1200 Use Ops[3];
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001201 ShuffleVectorInst(const ShuffleVectorInst &IE);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001202public:
1203 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1204 const std::string &Name = "", Instruction *InsertBefor = 0);
1205 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1206 const std::string &Name, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001207
Chris Lattnerfa495842006-04-08 04:04:54 +00001208 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001209 /// formed with the specified operands.
1210 static bool isValidOperands(const Value *V1, const Value *V2,
1211 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001212
Chris Lattner9fc18d22006-04-08 01:15:18 +00001213 virtual ShuffleVectorInst *clone() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001214
Reid Spencerac9dcb92007-02-15 03:39:18 +00001215 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001216 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001217 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001218 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001219 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001220
Chris Lattner9fc18d22006-04-08 01:15:18 +00001221 /// Transparently provide more efficient getOperand methods.
1222 Value *getOperand(unsigned i) const {
1223 assert(i < 3 && "getOperand() out of range!");
1224 return Ops[i];
1225 }
1226 void setOperand(unsigned i, Value *Val) {
1227 assert(i < 3 && "setOperand() out of range!");
1228 Ops[i] = Val;
1229 }
1230 unsigned getNumOperands() const { return 3; }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001231
Chris Lattner9fc18d22006-04-08 01:15:18 +00001232 // Methods for support type inquiry through isa, cast, and dyn_cast:
1233 static inline bool classof(const ShuffleVectorInst *) { return true; }
1234 static inline bool classof(const Instruction *I) {
1235 return I->getOpcode() == Instruction::ShuffleVector;
1236 }
1237 static inline bool classof(const Value *V) {
1238 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1239 }
1240};
1241
1242
1243//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001244// PHINode Class
1245//===----------------------------------------------------------------------===//
1246
1247// PHINode - The PHINode class is used to represent the magical mystical PHI
1248// node, that can not exist in nature, but can be synthesized in a computer
1249// scientist's overactive imagination.
1250//
1251class PHINode : public Instruction {
Chris Lattner454928e2005-01-29 00:31:36 +00001252 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1253 /// the number actually in use.
1254 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001255 PHINode(const PHINode &PN);
1256public:
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001257 explicit PHINode(const Type *Ty, const std::string &Name = "",
1258 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001259 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Chris Lattner454928e2005-01-29 00:31:36 +00001260 ReservedSpace(0) {
Chris Lattner910c80a2007-02-24 00:55:48 +00001261 setName(Name);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001262 }
1263
1264 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001265 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Chris Lattner454928e2005-01-29 00:31:36 +00001266 ReservedSpace(0) {
Chris Lattner910c80a2007-02-24 00:55:48 +00001267 setName(Name);
Chris Lattner454928e2005-01-29 00:31:36 +00001268 }
1269
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001270 ~PHINode();
1271
Chris Lattner454928e2005-01-29 00:31:36 +00001272 /// reserveOperandSpace - This method can be used to avoid repeated
1273 /// reallocation of PHI operand lists by reserving space for the correct
1274 /// number of operands before adding them. Unlike normal vector reserves,
1275 /// this method can also be used to trim the operand space.
1276 void reserveOperandSpace(unsigned NumValues) {
1277 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001278 }
1279
Chris Lattnerf319e832004-10-15 23:52:05 +00001280 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001281
1282 /// getNumIncomingValues - Return the number of incoming edges
1283 ///
Chris Lattner454928e2005-01-29 00:31:36 +00001284 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001285
Reid Spencerc773de62006-05-19 19:07:54 +00001286 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001287 ///
1288 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001289 assert(i*2 < getNumOperands() && "Invalid value number!");
1290 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001291 }
1292 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +00001293 assert(i*2 < getNumOperands() && "Invalid value number!");
1294 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001295 }
Chris Lattner454928e2005-01-29 00:31:36 +00001296 unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001297 return i*2;
1298 }
1299
Reid Spencerc773de62006-05-19 19:07:54 +00001300 /// getIncomingBlock - Return incoming basic block number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001301 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00001302 BasicBlock *getIncomingBlock(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001303 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001304 }
1305 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Chris Lattner454928e2005-01-29 00:31:36 +00001306 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001307 }
1308 unsigned getOperandNumForIncomingBlock(unsigned i) {
1309 return i*2+1;
1310 }
1311
1312 /// addIncoming - Add an incoming value to the end of the PHI list
1313 ///
1314 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00001315 assert(V && "PHI node got a null value!");
1316 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001317 assert(getType() == V->getType() &&
1318 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +00001319 unsigned OpNo = NumOperands;
1320 if (OpNo+2 > ReservedSpace)
1321 resizeOperands(0); // Get more space!
1322 // Initialize some new operands.
1323 NumOperands = OpNo+2;
1324 OperandList[OpNo].init(V, this);
1325 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001326 }
Misha Brukman9769ab22005-04-21 20:19:05 +00001327
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001328 /// removeIncomingValue - Remove an incoming value. This is useful if a
1329 /// predecessor basic block is deleted. The value removed is returned.
1330 ///
1331 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1332 /// is true), the PHI node is destroyed and any uses of it are replaced with
1333 /// dummy values. The only time there should be zero incoming values to a PHI
1334 /// node is when the block is dead, so this strategy is sound.
1335 ///
1336 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1337
1338 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1339 int Idx = getBasicBlockIndex(BB);
1340 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1341 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1342 }
1343
Misha Brukman9769ab22005-04-21 20:19:05 +00001344 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001345 /// block in the value list for this PHI. Returns -1 if no instance.
1346 ///
1347 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001348 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00001349 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Chris Lattner454928e2005-01-29 00:31:36 +00001350 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001351 return -1;
1352 }
1353
1354 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1355 return getIncomingValue(getBasicBlockIndex(BB));
1356 }
1357
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001358 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00001359 /// same value, return the value, otherwise return null.
1360 ///
Chris Lattner9acbd612005-08-05 00:49:06 +00001361 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001362
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001363 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1364 static inline bool classof(const PHINode *) { return true; }
1365 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001366 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001367 }
1368 static inline bool classof(const Value *V) {
1369 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1370 }
Chris Lattner454928e2005-01-29 00:31:36 +00001371 private:
1372 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001373};
1374
1375//===----------------------------------------------------------------------===//
1376// ReturnInst Class
1377//===----------------------------------------------------------------------===//
1378
1379//===---------------------------------------------------------------------------
1380/// ReturnInst - Return a value (possibly void), from a function. Execution
1381/// does not continue in this function any longer.
1382///
1383class ReturnInst : public TerminatorInst {
Devang Patel64d4e612008-02-26 17:56:20 +00001384 Use RetVal;
Chris Lattner910c80a2007-02-24 00:55:48 +00001385 ReturnInst(const ReturnInst &RI);
Devang Patelfea98302008-02-26 19:15:26 +00001386 void init(Value * const* retVals, unsigned N);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001387
1388public:
1389 // ReturnInst constructors:
1390 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001391 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001392 // ReturnInst(Value* X) - 'ret X' instruction
1393 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
1394 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1395 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
1396 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
Devang Patele6be34a2008-02-27 01:20:54 +00001397 // ReturnInst(Value* X, N) - 'ret X,X+1...X+N-1' instruction
1398 // ReturnInst(Value* X, N, Inst *) - 'ret X,X+1...X+N-1', insert before I
1399 // ReturnInst(Value* X, N, BB *) - 'ret X,X+1...X+N-1', insert @ end of BB
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00001400 //
1401 // NOTE: If the Value* passed is of type void then the constructor behaves as
1402 // if it was passed NULL.
Chris Lattner910c80a2007-02-24 00:55:48 +00001403 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1404 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
Devang Patelf4511cd2008-02-26 19:38:17 +00001405 ReturnInst(Value * const* retVals, unsigned N);
1406 ReturnInst(Value * const* retVals, unsigned N, Instruction *InsertBefore);
1407 ReturnInst(Value * const* retVals, unsigned N, BasicBlock *InsertAtEnd);
Chris Lattner910c80a2007-02-24 00:55:48 +00001408 explicit ReturnInst(BasicBlock *InsertAtEnd);
Devang Patel57ef4f42008-02-23 00:35:18 +00001409 virtual ~ReturnInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001410
Chris Lattnerf319e832004-10-15 23:52:05 +00001411 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001412
Devang Patelf8989652008-02-26 19:08:13 +00001413 Value *getReturnValue(unsigned n = 0) const {
1414 if (n == 0)
Devang Patel64d4e612008-02-26 17:56:20 +00001415 return RetVal;
Devang Patelf8989652008-02-26 19:08:13 +00001416 return getOperand(n);
Devang Patel64d4e612008-02-26 17:56:20 +00001417 }
1418
Chris Lattner454928e2005-01-29 00:31:36 +00001419 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001420
1421 // Methods for support type inquiry through isa, cast, and dyn_cast:
1422 static inline bool classof(const ReturnInst *) { return true; }
1423 static inline bool classof(const Instruction *I) {
1424 return (I->getOpcode() == Instruction::Ret);
1425 }
1426 static inline bool classof(const Value *V) {
1427 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1428 }
Chris Lattner454928e2005-01-29 00:31:36 +00001429 private:
1430 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1431 virtual unsigned getNumSuccessorsV() const;
1432 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001433};
1434
1435//===----------------------------------------------------------------------===//
1436// BranchInst Class
1437//===----------------------------------------------------------------------===//
1438
1439//===---------------------------------------------------------------------------
1440/// BranchInst - Conditional or Unconditional Branch instruction.
1441///
1442class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001443 /// Ops list - Branches are strange. The operands are ordered:
1444 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
1445 /// they don't have to check for cond/uncond branchness.
1446 Use Ops[3];
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001447 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00001448 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001449public:
1450 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1451 // BranchInst(BB *B) - 'br B'
1452 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1453 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1454 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1455 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1456 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00001457 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001458 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00001459 Instruction *InsertBefore = 0);
1460 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001461 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00001462 BasicBlock *InsertAtEnd);
Chris Lattner454928e2005-01-29 00:31:36 +00001463
1464 /// Transparently provide more efficient getOperand methods.
1465 Value *getOperand(unsigned i) const {
1466 assert(i < getNumOperands() && "getOperand() out of range!");
1467 return Ops[i];
1468 }
1469 void setOperand(unsigned i, Value *Val) {
1470 assert(i < getNumOperands() && "setOperand() out of range!");
1471 Ops[i] = Val;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001472 }
1473
Chris Lattnerf319e832004-10-15 23:52:05 +00001474 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001475
Devang Patel4d4a5e02008-02-23 01:11:02 +00001476 bool isUnconditional() const { return getNumOperands() == 1; }
1477 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001478
Devang Patel4d4a5e02008-02-23 01:11:02 +00001479 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001480 assert(isConditional() && "Cannot get condition of an uncond branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001481 return getOperand(2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001482 }
1483
1484 void setCondition(Value *V) {
1485 assert(isConditional() && "Cannot set condition of unconditional branch!");
1486 setOperand(2, V);
1487 }
1488
1489 // setUnconditionalDest - Change the current branch to an unconditional branch
1490 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +00001491 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001492 void setUnconditionalDest(BasicBlock *Dest) {
Chris Lattner454928e2005-01-29 00:31:36 +00001493 if (isConditional()) { // Convert this to an uncond branch.
1494 NumOperands = 1;
1495 Ops[1].set(0);
1496 Ops[2].set(0);
1497 }
1498 setOperand(0, reinterpret_cast<Value*>(Dest));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001499 }
1500
Chris Lattner454928e2005-01-29 00:31:36 +00001501 unsigned getNumSuccessors() const { return 1+isConditional(); }
1502
1503 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001504 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Dan Gohmanb96039e2007-05-11 20:59:29 +00001505 return cast<BasicBlock>(getOperand(i));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001506 }
1507
Chris Lattner454928e2005-01-29 00:31:36 +00001508 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001509 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001510 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001511 }
1512
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001513 // Methods for support type inquiry through isa, cast, and dyn_cast:
1514 static inline bool classof(const BranchInst *) { return true; }
1515 static inline bool classof(const Instruction *I) {
1516 return (I->getOpcode() == Instruction::Br);
1517 }
1518 static inline bool classof(const Value *V) {
1519 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1520 }
Chris Lattner454928e2005-01-29 00:31:36 +00001521private:
1522 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1523 virtual unsigned getNumSuccessorsV() const;
1524 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001525};
1526
1527//===----------------------------------------------------------------------===//
1528// SwitchInst Class
1529//===----------------------------------------------------------------------===//
1530
1531//===---------------------------------------------------------------------------
1532/// SwitchInst - Multiway switch
1533///
1534class SwitchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00001535 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001536 // Operand[0] = Value to switch on
1537 // Operand[1] = Default basic block destination
1538 // Operand[2n ] = Value to match
1539 // Operand[2n+1] = BasicBlock to go to on match
1540 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00001541 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1542 void resizeOperands(unsigned No);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001543public:
Chris Lattner454928e2005-01-29 00:31:36 +00001544 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1545 /// switch on and a default destination. The number of additional cases can
1546 /// be specified here to make memory allocation more efficient. This
1547 /// constructor can also autoinsert before another instruction.
1548 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00001549 Instruction *InsertBefore = 0);
1550
Chris Lattner454928e2005-01-29 00:31:36 +00001551 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1552 /// switch on and a default destination. The number of additional cases can
1553 /// be specified here to make memory allocation more efficient. This
1554 /// constructor also autoinserts at the end of the specified BasicBlock.
1555 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00001556 BasicBlock *InsertAtEnd);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001557 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00001558
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001559
1560 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00001561 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00001562 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00001563
Devang Patel4d4a5e02008-02-23 01:11:02 +00001564 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001565 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001566 }
1567
1568 /// getNumCases - return the number of 'cases' in this switch instruction.
1569 /// Note that case #0 is always the default case.
1570 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001571 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001572 }
1573
1574 /// getCaseValue - Return the specified case value. Note that case #0, the
1575 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001576 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001577 assert(i && i < getNumCases() && "Illegal case value to get!");
1578 return getSuccessorValue(i);
1579 }
1580
1581 /// getCaseValue - Return the specified case value. Note that case #0, the
1582 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001583 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001584 assert(i && i < getNumCases() && "Illegal case value to get!");
1585 return getSuccessorValue(i);
1586 }
1587
1588 /// findCaseValue - Search all of the case values for the specified constant.
1589 /// If it is explicitly handled, return the case number of it, otherwise
1590 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00001591 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001592 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1593 if (getCaseValue(i) == C)
1594 return i;
1595 return 0;
1596 }
1597
Nick Lewycky011f1842006-09-18 19:03:59 +00001598 /// findCaseDest - Finds the unique case value for a given successor. Returns
1599 /// null if the successor is not found, not unique, or is the default case.
1600 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00001601 if (BB == getDefaultDest()) return NULL;
1602
Nick Lewycky011f1842006-09-18 19:03:59 +00001603 ConstantInt *CI = NULL;
1604 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
1605 if (getSuccessor(i) == BB) {
1606 if (CI) return NULL; // Multiple cases lead to BB.
1607 else CI = getCaseValue(i);
1608 }
1609 }
1610 return CI;
1611 }
1612
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001613 /// addCase - Add an entry to the switch instruction...
1614 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00001615 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001616
1617 /// removeCase - This method removes the specified successor from the switch
1618 /// instruction. Note that this cannot be used to remove the default
1619 /// destination (successor #0).
1620 ///
1621 void removeCase(unsigned idx);
1622
Chris Lattner454928e2005-01-29 00:31:36 +00001623 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001624
Chris Lattner454928e2005-01-29 00:31:36 +00001625 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1626 BasicBlock *getSuccessor(unsigned idx) const {
1627 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1628 return cast<BasicBlock>(getOperand(idx*2+1));
1629 }
1630 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001631 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Chris Lattner454928e2005-01-29 00:31:36 +00001632 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001633 }
1634
1635 // getSuccessorValue - Return the value associated with the specified
1636 // successor.
Devang Patel4d4a5e02008-02-23 01:11:02 +00001637 ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001638 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00001639 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001640 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001641
1642 // Methods for support type inquiry through isa, cast, and dyn_cast:
1643 static inline bool classof(const SwitchInst *) { return true; }
1644 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00001645 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001646 }
1647 static inline bool classof(const Value *V) {
1648 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1649 }
Chris Lattner454928e2005-01-29 00:31:36 +00001650private:
1651 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1652 virtual unsigned getNumSuccessorsV() const;
1653 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001654};
1655
1656//===----------------------------------------------------------------------===//
1657// InvokeInst Class
1658//===----------------------------------------------------------------------===//
1659
1660//===---------------------------------------------------------------------------
Chris Lattner3340ffe2005-05-06 20:26:26 +00001661
1662/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
1663/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001664///
1665class InvokeInst : public TerminatorInst {
Duncan Sandsdc024672007-11-27 13:23:08 +00001666 const ParamAttrsList *ParamAttrs;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001667 InvokeInst(const InvokeInst &BI);
1668 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerd2dd1502007-02-13 01:04:01 +00001669 Value* const *Args, unsigned NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00001670
1671 template<typename InputIterator>
1672 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1673 InputIterator ArgBegin, InputIterator ArgEnd,
1674 const std::string &Name,
1675 // This argument ensures that we have an iterator we can
1676 // do arithmetic on in constant time
1677 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00001678 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
David Greenef1355a52007-08-27 19:04:21 +00001679
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00001680 // This requires that the iterator points to contiguous memory.
1681 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00001682 setName(Name);
1683 }
1684
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001685public:
David Greenef1355a52007-08-27 19:04:21 +00001686 /// Construct an InvokeInst given a range of arguments.
1687 /// InputIterator must be a random-access iterator pointing to
1688 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
1689 /// made for random-accessness but not for contiguous storage as
1690 /// that would incur runtime overhead.
1691 ///
1692 /// @brief Construct an InvokeInst from a range of arguments
1693 template<typename InputIterator>
1694 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1695 InputIterator ArgBegin, InputIterator ArgEnd,
1696 const std::string &Name = "", Instruction *InsertBefore = 0)
1697 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
1698 ->getElementType())->getReturnType(),
1699 Instruction::Invoke, 0, 0, InsertBefore) {
1700 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1701 typename std::iterator_traits<InputIterator>::iterator_category());
1702 }
1703
1704 /// Construct an InvokeInst given a range of arguments.
1705 /// InputIterator must be a random-access iterator pointing to
1706 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
1707 /// made for random-accessness but not for contiguous storage as
1708 /// that would incur runtime overhead.
1709 ///
1710 /// @brief Construct an InvokeInst from a range of arguments
1711 template<typename InputIterator>
1712 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1713 InputIterator ArgBegin, InputIterator ArgEnd,
1714 const std::string &Name, BasicBlock *InsertAtEnd)
1715 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
1716 ->getElementType())->getReturnType(),
1717 Instruction::Invoke, 0, 0, InsertAtEnd) {
1718 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1719 typename std::iterator_traits<InputIterator>::iterator_category());
1720 }
1721
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001722 ~InvokeInst();
1723
Chris Lattnerf319e832004-10-15 23:52:05 +00001724 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001725
Chris Lattner3340ffe2005-05-06 20:26:26 +00001726 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1727 /// function call.
1728 unsigned getCallingConv() const { return SubclassData; }
1729 void setCallingConv(unsigned CC) {
1730 SubclassData = CC;
1731 }
1732
Reid Spencerfa3e9122007-04-09 18:00:57 +00001733 /// Obtains a pointer to the ParamAttrsList object which holds the
1734 /// parameter attributes information, if any.
1735 /// @returns 0 if no attributes have been set.
1736 /// @brief Get the parameter attributes.
Duncan Sandsdc024672007-11-27 13:23:08 +00001737 const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00001738
1739 /// Sets the parameter attributes for this InvokeInst. To construct a
1740 /// ParamAttrsList, see ParameterAttributes.h
1741 /// @brief Set the parameter attributes.
Duncan Sandsdc024672007-11-27 13:23:08 +00001742 void setParamAttrs(const ParamAttrsList *attrs);
1743
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001744 /// @brief Determine whether the call or the callee has the given attribute.
Dale Johannesen0d51e7e2008-02-19 21:38:47 +00001745 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001746
Dale Johannesen08e78b12008-02-22 17:49:45 +00001747 /// @brief Extract the alignment for a call or parameter (0=unknown).
1748 uint16_t getParamAlignment(uint16_t i) const;
1749
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001750 /// @brief Determine if the call does not access memory.
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001751 bool doesNotAccessMemory() const;
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001752
1753 /// @brief Determine if the call does not access or only reads memory.
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001754 bool onlyReadsMemory() const;
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001755
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001756 /// @brief Determine if the call cannot return.
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001757 bool doesNotReturn() const;
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001758
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001759 /// @brief Determine if the call cannot unwind.
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001760 bool doesNotThrow() const;
Duncan Sandsf0c33542007-12-19 21:13:37 +00001761 void setDoesNotThrow(bool doesNotThrow = true);
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001762
Duncan Sandsdc024672007-11-27 13:23:08 +00001763 /// @brief Determine if the call returns a structure.
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001764 bool isStructReturn() const;
Reid Spencerfa3e9122007-04-09 18:00:57 +00001765
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001766 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00001767 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001768 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001769 Function *getCalledFunction() const {
Chris Lattner454928e2005-01-29 00:31:36 +00001770 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00001771 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001772
1773 // getCalledValue - Get a pointer to a function that is invoked by this inst.
Devang Patel4d4a5e02008-02-23 01:11:02 +00001774 Value *getCalledValue() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001775
1776 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00001777 BasicBlock *getNormalDest() const {
1778 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001779 }
Chris Lattner454928e2005-01-29 00:31:36 +00001780 BasicBlock *getUnwindDest() const {
1781 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001782 }
Chris Lattner454928e2005-01-29 00:31:36 +00001783 void setNormalDest(BasicBlock *B) {
1784 setOperand(1, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001785 }
1786
Chris Lattner454928e2005-01-29 00:31:36 +00001787 void setUnwindDest(BasicBlock *B) {
1788 setOperand(2, reinterpret_cast<Value*>(B));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001789 }
1790
Devang Patel4d4a5e02008-02-23 01:11:02 +00001791 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001792 assert(i < 2 && "Successor # out of range for invoke!");
1793 return i == 0 ? getNormalDest() : getUnwindDest();
1794 }
1795
Chris Lattner454928e2005-01-29 00:31:36 +00001796 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001797 assert(idx < 2 && "Successor # out of range for invoke!");
Chris Lattner454928e2005-01-29 00:31:36 +00001798 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001799 }
1800
Chris Lattner454928e2005-01-29 00:31:36 +00001801 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001802
1803 // Methods for support type inquiry through isa, cast, and dyn_cast:
1804 static inline bool classof(const InvokeInst *) { return true; }
1805 static inline bool classof(const Instruction *I) {
1806 return (I->getOpcode() == Instruction::Invoke);
1807 }
1808 static inline bool classof(const Value *V) {
1809 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1810 }
Chris Lattner454928e2005-01-29 00:31:36 +00001811private:
1812 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1813 virtual unsigned getNumSuccessorsV() const;
1814 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001815};
1816
1817
1818//===----------------------------------------------------------------------===//
1819// UnwindInst Class
1820//===----------------------------------------------------------------------===//
1821
1822//===---------------------------------------------------------------------------
1823/// UnwindInst - Immediately exit the current function, unwinding the stack
1824/// until an invoke instruction is found.
1825///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00001826class UnwindInst : public TerminatorInst {
1827public:
Chris Lattner910c80a2007-02-24 00:55:48 +00001828 explicit UnwindInst(Instruction *InsertBefore = 0);
1829 explicit UnwindInst(BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001830
Chris Lattnerf319e832004-10-15 23:52:05 +00001831 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001832
Chris Lattner454928e2005-01-29 00:31:36 +00001833 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001834
1835 // Methods for support type inquiry through isa, cast, and dyn_cast:
1836 static inline bool classof(const UnwindInst *) { return true; }
1837 static inline bool classof(const Instruction *I) {
1838 return I->getOpcode() == Instruction::Unwind;
1839 }
1840 static inline bool classof(const Value *V) {
1841 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1842 }
Chris Lattner454928e2005-01-29 00:31:36 +00001843private:
1844 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1845 virtual unsigned getNumSuccessorsV() const;
1846 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001847};
1848
Chris Lattner076b3f12004-10-16 18:05:54 +00001849//===----------------------------------------------------------------------===//
1850// UnreachableInst Class
1851//===----------------------------------------------------------------------===//
1852
1853//===---------------------------------------------------------------------------
1854/// UnreachableInst - This function has undefined behavior. In particular, the
1855/// presence of this instruction indicates some higher level knowledge that the
1856/// end of the block cannot be reached.
1857///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00001858class UnreachableInst : public TerminatorInst {
1859public:
Chris Lattner910c80a2007-02-24 00:55:48 +00001860 explicit UnreachableInst(Instruction *InsertBefore = 0);
1861 explicit UnreachableInst(BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00001862
1863 virtual UnreachableInst *clone() const;
1864
Chris Lattner454928e2005-01-29 00:31:36 +00001865 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00001866
1867 // Methods for support type inquiry through isa, cast, and dyn_cast:
1868 static inline bool classof(const UnreachableInst *) { return true; }
1869 static inline bool classof(const Instruction *I) {
1870 return I->getOpcode() == Instruction::Unreachable;
1871 }
1872 static inline bool classof(const Value *V) {
1873 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1874 }
Chris Lattner454928e2005-01-29 00:31:36 +00001875private:
1876 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1877 virtual unsigned getNumSuccessorsV() const;
1878 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00001879};
1880
Reid Spencer3da59db2006-11-27 01:05:10 +00001881//===----------------------------------------------------------------------===//
1882// TruncInst Class
1883//===----------------------------------------------------------------------===//
1884
1885/// @brief This class represents a truncation of integer types.
1886class TruncInst : public CastInst {
1887 /// Private copy constructor
1888 TruncInst(const TruncInst &CI)
1889 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
1890 }
1891public:
1892 /// @brief Constructor with insert-before-instruction semantics
1893 TruncInst(
1894 Value *S, ///< The value to be truncated
1895 const Type *Ty, ///< The (smaller) type to truncate to
1896 const std::string &Name = "", ///< A name for the new instruction
1897 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1898 );
1899
1900 /// @brief Constructor with insert-at-end-of-block semantics
1901 TruncInst(
1902 Value *S, ///< The value to be truncated
1903 const Type *Ty, ///< The (smaller) type to truncate to
1904 const std::string &Name, ///< A name for the new instruction
1905 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1906 );
1907
1908 /// @brief Clone an identical TruncInst
1909 virtual CastInst *clone() const;
1910
1911 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1912 static inline bool classof(const TruncInst *) { return true; }
1913 static inline bool classof(const Instruction *I) {
1914 return I->getOpcode() == Trunc;
1915 }
1916 static inline bool classof(const Value *V) {
1917 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1918 }
1919};
1920
1921//===----------------------------------------------------------------------===//
1922// ZExtInst Class
1923//===----------------------------------------------------------------------===//
1924
1925/// @brief This class represents zero extension of integer types.
1926class ZExtInst : public CastInst {
1927 /// @brief Private copy constructor
1928 ZExtInst(const ZExtInst &CI)
1929 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
1930 }
1931public:
1932 /// @brief Constructor with insert-before-instruction semantics
1933 ZExtInst(
1934 Value *S, ///< The value to be zero extended
1935 const Type *Ty, ///< The type to zero extend to
1936 const std::string &Name = "", ///< A name for the new instruction
1937 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1938 );
1939
1940 /// @brief Constructor with insert-at-end semantics.
1941 ZExtInst(
1942 Value *S, ///< The value to be zero extended
1943 const Type *Ty, ///< The type to zero extend to
1944 const std::string &Name, ///< A name for the new instruction
1945 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1946 );
1947
1948 /// @brief Clone an identical ZExtInst
1949 virtual CastInst *clone() const;
1950
1951 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1952 static inline bool classof(const ZExtInst *) { return true; }
1953 static inline bool classof(const Instruction *I) {
1954 return I->getOpcode() == ZExt;
1955 }
1956 static inline bool classof(const Value *V) {
1957 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1958 }
1959};
1960
1961//===----------------------------------------------------------------------===//
1962// SExtInst Class
1963//===----------------------------------------------------------------------===//
1964
1965/// @brief This class represents a sign extension of integer types.
1966class SExtInst : public CastInst {
1967 /// @brief Private copy constructor
1968 SExtInst(const SExtInst &CI)
1969 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
1970 }
1971public:
1972 /// @brief Constructor with insert-before-instruction semantics
1973 SExtInst(
1974 Value *S, ///< The value to be sign extended
1975 const Type *Ty, ///< The type to sign extend to
1976 const std::string &Name = "", ///< A name for the new instruction
1977 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1978 );
1979
1980 /// @brief Constructor with insert-at-end-of-block semantics
1981 SExtInst(
1982 Value *S, ///< The value to be sign extended
1983 const Type *Ty, ///< The type to sign extend to
1984 const std::string &Name, ///< A name for the new instruction
1985 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1986 );
1987
1988 /// @brief Clone an identical SExtInst
1989 virtual CastInst *clone() const;
1990
1991 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1992 static inline bool classof(const SExtInst *) { return true; }
1993 static inline bool classof(const Instruction *I) {
1994 return I->getOpcode() == SExt;
1995 }
1996 static inline bool classof(const Value *V) {
1997 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1998 }
1999};
2000
2001//===----------------------------------------------------------------------===//
2002// FPTruncInst Class
2003//===----------------------------------------------------------------------===//
2004
2005/// @brief This class represents a truncation of floating point types.
2006class FPTruncInst : public CastInst {
2007 FPTruncInst(const FPTruncInst &CI)
2008 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2009 }
2010public:
2011 /// @brief Constructor with insert-before-instruction semantics
2012 FPTruncInst(
2013 Value *S, ///< The value to be truncated
2014 const Type *Ty, ///< The type to truncate to
2015 const std::string &Name = "", ///< A name for the new instruction
2016 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2017 );
2018
2019 /// @brief Constructor with insert-before-instruction semantics
2020 FPTruncInst(
2021 Value *S, ///< The value to be truncated
2022 const Type *Ty, ///< The type to truncate to
2023 const std::string &Name, ///< A name for the new instruction
2024 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2025 );
2026
2027 /// @brief Clone an identical FPTruncInst
2028 virtual CastInst *clone() const;
2029
2030 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2031 static inline bool classof(const FPTruncInst *) { return true; }
2032 static inline bool classof(const Instruction *I) {
2033 return I->getOpcode() == FPTrunc;
2034 }
2035 static inline bool classof(const Value *V) {
2036 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2037 }
2038};
2039
2040//===----------------------------------------------------------------------===//
2041// FPExtInst Class
2042//===----------------------------------------------------------------------===//
2043
2044/// @brief This class represents an extension of floating point types.
2045class FPExtInst : public CastInst {
2046 FPExtInst(const FPExtInst &CI)
2047 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2048 }
2049public:
2050 /// @brief Constructor with insert-before-instruction semantics
2051 FPExtInst(
2052 Value *S, ///< The value to be extended
2053 const Type *Ty, ///< The type to extend to
2054 const std::string &Name = "", ///< A name for the new instruction
2055 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2056 );
2057
2058 /// @brief Constructor with insert-at-end-of-block semantics
2059 FPExtInst(
2060 Value *S, ///< The value to be extended
2061 const Type *Ty, ///< The type to extend to
2062 const std::string &Name, ///< A name for the new instruction
2063 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2064 );
2065
2066 /// @brief Clone an identical FPExtInst
2067 virtual CastInst *clone() const;
2068
2069 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2070 static inline bool classof(const FPExtInst *) { return true; }
2071 static inline bool classof(const Instruction *I) {
2072 return I->getOpcode() == FPExt;
2073 }
2074 static inline bool classof(const Value *V) {
2075 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2076 }
2077};
2078
2079//===----------------------------------------------------------------------===//
2080// UIToFPInst Class
2081//===----------------------------------------------------------------------===//
2082
2083/// @brief This class represents a cast unsigned integer to floating point.
2084class UIToFPInst : public CastInst {
2085 UIToFPInst(const UIToFPInst &CI)
2086 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2087 }
2088public:
2089 /// @brief Constructor with insert-before-instruction semantics
2090 UIToFPInst(
2091 Value *S, ///< The value to be converted
2092 const Type *Ty, ///< The type to convert to
2093 const std::string &Name = "", ///< A name for the new instruction
2094 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2095 );
2096
2097 /// @brief Constructor with insert-at-end-of-block semantics
2098 UIToFPInst(
2099 Value *S, ///< The value to be converted
2100 const Type *Ty, ///< The type to convert to
2101 const std::string &Name, ///< A name for the new instruction
2102 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2103 );
2104
2105 /// @brief Clone an identical UIToFPInst
2106 virtual CastInst *clone() const;
2107
2108 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2109 static inline bool classof(const UIToFPInst *) { return true; }
2110 static inline bool classof(const Instruction *I) {
2111 return I->getOpcode() == UIToFP;
2112 }
2113 static inline bool classof(const Value *V) {
2114 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2115 }
2116};
2117
2118//===----------------------------------------------------------------------===//
2119// SIToFPInst Class
2120//===----------------------------------------------------------------------===//
2121
2122/// @brief This class represents a cast from signed integer to floating point.
2123class SIToFPInst : public CastInst {
2124 SIToFPInst(const SIToFPInst &CI)
2125 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2126 }
2127public:
2128 /// @brief Constructor with insert-before-instruction semantics
2129 SIToFPInst(
2130 Value *S, ///< The value to be converted
2131 const Type *Ty, ///< The type to convert to
2132 const std::string &Name = "", ///< A name for the new instruction
2133 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2134 );
2135
2136 /// @brief Constructor with insert-at-end-of-block semantics
2137 SIToFPInst(
2138 Value *S, ///< The value to be converted
2139 const Type *Ty, ///< The type to convert to
2140 const std::string &Name, ///< A name for the new instruction
2141 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2142 );
2143
2144 /// @brief Clone an identical SIToFPInst
2145 virtual CastInst *clone() const;
2146
2147 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2148 static inline bool classof(const SIToFPInst *) { return true; }
2149 static inline bool classof(const Instruction *I) {
2150 return I->getOpcode() == SIToFP;
2151 }
2152 static inline bool classof(const Value *V) {
2153 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2154 }
2155};
2156
2157//===----------------------------------------------------------------------===//
2158// FPToUIInst Class
2159//===----------------------------------------------------------------------===//
2160
2161/// @brief This class represents a cast from floating point to unsigned integer
2162class FPToUIInst : public CastInst {
2163 FPToUIInst(const FPToUIInst &CI)
2164 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2165 }
2166public:
2167 /// @brief Constructor with insert-before-instruction semantics
2168 FPToUIInst(
2169 Value *S, ///< The value to be converted
2170 const Type *Ty, ///< The type to convert to
2171 const std::string &Name = "", ///< A name for the new instruction
2172 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2173 );
2174
2175 /// @brief Constructor with insert-at-end-of-block semantics
2176 FPToUIInst(
2177 Value *S, ///< The value to be converted
2178 const Type *Ty, ///< The type to convert to
2179 const std::string &Name, ///< A name for the new instruction
2180 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2181 );
2182
2183 /// @brief Clone an identical FPToUIInst
2184 virtual CastInst *clone() const;
2185
2186 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2187 static inline bool classof(const FPToUIInst *) { return true; }
2188 static inline bool classof(const Instruction *I) {
2189 return I->getOpcode() == FPToUI;
2190 }
2191 static inline bool classof(const Value *V) {
2192 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2193 }
2194};
2195
2196//===----------------------------------------------------------------------===//
2197// FPToSIInst Class
2198//===----------------------------------------------------------------------===//
2199
2200/// @brief This class represents a cast from floating point to signed integer.
2201class FPToSIInst : public CastInst {
2202 FPToSIInst(const FPToSIInst &CI)
2203 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2204 }
2205public:
2206 /// @brief Constructor with insert-before-instruction semantics
2207 FPToSIInst(
2208 Value *S, ///< The value to be converted
2209 const Type *Ty, ///< The type to convert to
2210 const std::string &Name = "", ///< A name for the new instruction
2211 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2212 );
2213
2214 /// @brief Constructor with insert-at-end-of-block semantics
2215 FPToSIInst(
2216 Value *S, ///< The value to be converted
2217 const Type *Ty, ///< The type to convert to
2218 const std::string &Name, ///< A name for the new instruction
2219 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2220 );
2221
2222 /// @brief Clone an identical FPToSIInst
2223 virtual CastInst *clone() const;
2224
2225 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2226 static inline bool classof(const FPToSIInst *) { return true; }
2227 static inline bool classof(const Instruction *I) {
2228 return I->getOpcode() == FPToSI;
2229 }
2230 static inline bool classof(const Value *V) {
2231 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2232 }
2233};
2234
2235//===----------------------------------------------------------------------===//
2236// IntToPtrInst Class
2237//===----------------------------------------------------------------------===//
2238
2239/// @brief This class represents a cast from an integer to a pointer.
2240class IntToPtrInst : public CastInst {
2241 IntToPtrInst(const IntToPtrInst &CI)
2242 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2243 }
2244public:
2245 /// @brief Constructor with insert-before-instruction semantics
2246 IntToPtrInst(
2247 Value *S, ///< The value to be converted
2248 const Type *Ty, ///< The type to convert to
2249 const std::string &Name = "", ///< A name for the new instruction
2250 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2251 );
2252
2253 /// @brief Constructor with insert-at-end-of-block semantics
2254 IntToPtrInst(
2255 Value *S, ///< The value to be converted
2256 const Type *Ty, ///< The type to convert to
2257 const std::string &Name, ///< A name for the new instruction
2258 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2259 );
2260
2261 /// @brief Clone an identical IntToPtrInst
2262 virtual CastInst *clone() const;
2263
2264 // Methods for support type inquiry through isa, cast, and dyn_cast:
2265 static inline bool classof(const IntToPtrInst *) { return true; }
2266 static inline bool classof(const Instruction *I) {
2267 return I->getOpcode() == IntToPtr;
2268 }
2269 static inline bool classof(const Value *V) {
2270 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2271 }
2272};
2273
2274//===----------------------------------------------------------------------===//
2275// PtrToIntInst Class
2276//===----------------------------------------------------------------------===//
2277
2278/// @brief This class represents a cast from a pointer to an integer
2279class PtrToIntInst : public CastInst {
2280 PtrToIntInst(const PtrToIntInst &CI)
2281 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
2282 }
2283public:
2284 /// @brief Constructor with insert-before-instruction semantics
2285 PtrToIntInst(
2286 Value *S, ///< The value to be converted
2287 const Type *Ty, ///< The type to convert to
2288 const std::string &Name = "", ///< A name for the new instruction
2289 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2290 );
2291
2292 /// @brief Constructor with insert-at-end-of-block semantics
2293 PtrToIntInst(
2294 Value *S, ///< The value to be converted
2295 const Type *Ty, ///< The type to convert to
2296 const std::string &Name, ///< A name for the new instruction
2297 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2298 );
2299
2300 /// @brief Clone an identical PtrToIntInst
2301 virtual CastInst *clone() const;
2302
2303 // Methods for support type inquiry through isa, cast, and dyn_cast:
2304 static inline bool classof(const PtrToIntInst *) { return true; }
2305 static inline bool classof(const Instruction *I) {
2306 return I->getOpcode() == PtrToInt;
2307 }
2308 static inline bool classof(const Value *V) {
2309 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2310 }
2311};
2312
2313//===----------------------------------------------------------------------===//
2314// BitCastInst Class
2315//===----------------------------------------------------------------------===//
2316
2317/// @brief This class represents a no-op cast from one type to another.
2318class BitCastInst : public CastInst {
2319 BitCastInst(const BitCastInst &CI)
2320 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
2321 }
2322public:
2323 /// @brief Constructor with insert-before-instruction semantics
2324 BitCastInst(
2325 Value *S, ///< The value to be casted
2326 const Type *Ty, ///< The type to casted to
2327 const std::string &Name = "", ///< A name for the new instruction
2328 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2329 );
2330
2331 /// @brief Constructor with insert-at-end-of-block semantics
2332 BitCastInst(
2333 Value *S, ///< The value to be casted
2334 const Type *Ty, ///< The type to casted to
2335 const std::string &Name, ///< A name for the new instruction
2336 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2337 );
2338
2339 /// @brief Clone an identical BitCastInst
2340 virtual CastInst *clone() const;
2341
2342 // Methods for support type inquiry through isa, cast, and dyn_cast:
2343 static inline bool classof(const BitCastInst *) { return true; }
2344 static inline bool classof(const Instruction *I) {
2345 return I->getOpcode() == BitCast;
2346 }
2347 static inline bool classof(const Value *V) {
2348 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2349 }
2350};
2351
Devang Patel40a04212008-02-19 22:15:16 +00002352//===----------------------------------------------------------------------===//
2353// GetResultInst Class
2354//===----------------------------------------------------------------------===//
2355
2356/// GetResultInst - This instruction extracts individual result value from
2357/// aggregate value, where aggregate value is returned by CallInst.
2358///
2359class GetResultInst : public Instruction {
Devang Patel23755d82008-02-20 19:10:47 +00002360 Use Aggr;
2361 unsigned Idx;
Devang Patel40a04212008-02-19 22:15:16 +00002362 GetResultInst(const GetResultInst &GRI) :
Devang Patel23755d82008-02-20 19:10:47 +00002363 Instruction(GRI.getType(), Instruction::GetResult, &Aggr, 1) {
2364 Aggr.init(GRI.Aggr, this);
2365 Idx = GRI.Idx;
Devang Patel40a04212008-02-19 22:15:16 +00002366 }
2367
2368public:
Devang Patel23755d82008-02-20 19:10:47 +00002369 explicit GetResultInst(Value *Aggr, unsigned index,
Devang Patel40a04212008-02-19 22:15:16 +00002370 const std::string &Name = "",
2371 Instruction *InsertBefore = 0);
2372
2373 /// isValidOperands - Return true if an getresult instruction can be
2374 /// formed with the specified operands.
Devang Patel23755d82008-02-20 19:10:47 +00002375 static bool isValidOperands(const Value *Aggr, unsigned index);
Devang Patel40a04212008-02-19 22:15:16 +00002376
2377 virtual GetResultInst *clone() const;
2378
Devang Patel4d4a5e02008-02-23 01:11:02 +00002379 Value *getAggregateValue() {
Devang Patel40a04212008-02-19 22:15:16 +00002380 return getOperand(0);
2381 }
Devang Patel2d2ae342008-02-20 18:36:16 +00002382
Devang Patel4d4a5e02008-02-23 01:11:02 +00002383 const Value *getAggregateValue() const {
Devang Patel2d2ae342008-02-20 18:36:16 +00002384 return getOperand(0);
2385 }
2386
Devang Patel4d4a5e02008-02-23 01:11:02 +00002387 unsigned getIndex() const {
Devang Patel23755d82008-02-20 19:10:47 +00002388 return Idx;
Devang Patel40a04212008-02-19 22:15:16 +00002389 }
2390
Devang Patel23755d82008-02-20 19:10:47 +00002391 unsigned getNumOperands() const { return 1; }
Devang Patel40a04212008-02-19 22:15:16 +00002392
2393 // Methods for support type inquiry through isa, cast, and dyn_cast:
2394 static inline bool classof(const GetResultInst *) { return true; }
2395 static inline bool classof(const Instruction *I) {
2396 return (I->getOpcode() == Instruction::GetResult);
2397 }
2398 static inline bool classof(const Value *V) {
2399 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2400 }
2401};
2402
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002403} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00002404
2405#endif