blob: c71d64ab07208d1419b05b0b3a9dd5e3bd3bb23c [file] [log] [blame]
Chris Lattnera892a3a2003-01-27 22:08:52 +00001//===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
Misha Brukman9769ab22005-04-21 20:19:05 +00002//
John Criswell6fbcc262003-10-20 20:19:47 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner7ed47a12007-12-29 19:59:42 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman9769ab22005-04-21 20:19:05 +00007//
John Criswell6fbcc262003-10-20 20:19:47 +00008//===----------------------------------------------------------------------===//
Chris Lattnera892a3a2003-01-27 22:08:52 +00009//
10// This file exposes the class definitions of all of the subclasses of the
11// Instruction class. This is meant to be an easy way to get access to all
12// instruction subclasses.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_INSTRUCTIONS_H
17#define LLVM_INSTRUCTIONS_H
18
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000019#include "llvm/InstrTypes.h"
David Greene52eec542007-08-01 03:43:44 +000020#include "llvm/DerivedTypes.h"
Devang Pateleaf42ab2008-09-23 23:03:40 +000021#include "llvm/Attributes.h"
Gabor Greifefe65362008-05-10 08:32:32 +000022#include "llvm/BasicBlock.h"
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000023#include "llvm/CallingConv.h"
Owen Anderson333c4002009-07-09 23:48:35 +000024#include "llvm/LLVMContext.h"
Dan Gohman81a0c0b2008-05-31 00:58:22 +000025#include "llvm/ADT/SmallVector.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000026#include <iterator>
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000027
28namespace llvm {
29
Chris Lattnerd1a32602005-02-24 05:32:09 +000030class ConstantInt;
Reid Spencer3da43842007-02-28 22:00:54 +000031class ConstantRange;
32class APInt;
Benjamin Kramer12ddd402009-08-11 17:45:13 +000033class LLVMContext;
Dan Gohmanbccfc242009-09-03 15:34:35 +000034class DominatorTree;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000035
36//===----------------------------------------------------------------------===//
37// AllocationInst Class
38//===----------------------------------------------------------------------===//
39
40/// AllocationInst - This class is the common base class of MallocInst and
41/// AllocaInst.
42///
Chris Lattner454928e2005-01-29 00:31:36 +000043class AllocationInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000044protected:
Owen Anderson50dead02009-07-15 23:53:25 +000045 AllocationInst(const Type *Ty, Value *ArraySize,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +000046 unsigned iTy, unsigned Align, const Twine &Name = "",
Owen Anderson9adc0ab2009-07-14 23:09:55 +000047 Instruction *InsertBefore = 0);
Owen Anderson50dead02009-07-15 23:53:25 +000048 AllocationInst(const Type *Ty, Value *ArraySize,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +000049 unsigned iTy, unsigned Align, const Twine &Name,
Owen Anderson9adc0ab2009-07-14 23:09:55 +000050 BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000051public:
Gabor Greif051a9502008-04-06 20:25:17 +000052 // Out of line virtual method, so the vtable, etc. has a home.
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000053 virtual ~AllocationInst();
54
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000055 /// isArrayAllocation - Return true if there is an allocation size parameter
56 /// to the allocation instruction that is not 1.
57 ///
58 bool isArrayAllocation() const;
59
Dan Gohman18476ee2009-07-07 20:47:48 +000060 /// getArraySize - Get the number of elements allocated. For a simple
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000061 /// allocation of a single element, this will return a constant 1 value.
62 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000063 const Value *getArraySize() const { return getOperand(0); }
64 Value *getArraySize() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000065
66 /// getType - Overload to return most specific pointer type
67 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +000068 const PointerType *getType() const {
Misha Brukman9769ab22005-04-21 20:19:05 +000069 return reinterpret_cast<const PointerType*>(Instruction::getType());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000070 }
71
72 /// getAllocatedType - Return the type that is being allocated by the
73 /// instruction.
74 ///
75 const Type *getAllocatedType() const;
76
Nate Begeman14b05292005-11-05 09:21:28 +000077 /// getAlignment - Return the alignment of the memory that is being allocated
78 /// by the instruction.
79 ///
Dan Gohman52837072008-03-24 16:55:58 +000080 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
81 void setAlignment(unsigned Align);
Chris Lattnerf56a8db2006-10-03 17:09:12 +000082
Nick Lewycky67760642009-09-27 07:38:41 +000083 virtual AllocationInst *clone() const = 0;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000084
85 // Methods for support type inquiry through isa, cast, and dyn_cast:
86 static inline bool classof(const AllocationInst *) { return true; }
87 static inline bool classof(const Instruction *I) {
88 return I->getOpcode() == Instruction::Alloca ||
89 I->getOpcode() == Instruction::Malloc;
90 }
91 static inline bool classof(const Value *V) {
92 return isa<Instruction>(V) && classof(cast<Instruction>(V));
93 }
94};
95
96
97//===----------------------------------------------------------------------===//
98// MallocInst Class
99//===----------------------------------------------------------------------===//
100
101/// MallocInst - an instruction to allocated memory on the heap
102///
103class MallocInst : public AllocationInst {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000104public:
Owen Anderson50dead02009-07-15 23:53:25 +0000105 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000106 const Twine &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000107 Instruction *InsertBefore = 0)
Owen Anderson50dead02009-07-15 23:53:25 +0000108 : AllocationInst(Ty, ArraySize, Malloc,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000109 0, NameStr, InsertBefore) {}
Owen Anderson50dead02009-07-15 23:53:25 +0000110 MallocInst(const Type *Ty, Value *ArraySize,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000111 const Twine &NameStr, BasicBlock *InsertAtEnd)
Owen Anderson50dead02009-07-15 23:53:25 +0000112 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000113
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000114 MallocInst(const Type *Ty, const Twine &NameStr,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000115 Instruction *InsertBefore = 0)
Owen Anderson50dead02009-07-15 23:53:25 +0000116 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000117 MallocInst(const Type *Ty, const Twine &NameStr,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000118 BasicBlock *InsertAtEnd)
Owen Anderson50dead02009-07-15 23:53:25 +0000119 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000120
Owen Anderson50dead02009-07-15 23:53:25 +0000121 MallocInst(const Type *Ty, Value *ArraySize,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000122 unsigned Align, const Twine &NameStr,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000123 BasicBlock *InsertAtEnd)
Owen Anderson50dead02009-07-15 23:53:25 +0000124 : AllocationInst(Ty, ArraySize, Malloc,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000125 Align, NameStr, InsertAtEnd) {}
Owen Anderson50dead02009-07-15 23:53:25 +0000126 MallocInst(const Type *Ty, Value *ArraySize,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000127 unsigned Align, const Twine &NameStr = "",
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000128 Instruction *InsertBefore = 0)
Owen Anderson50dead02009-07-15 23:53:25 +0000129 : AllocationInst(Ty, ArraySize,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000130 Malloc, Align, NameStr, InsertBefore) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000131
Nick Lewycky67760642009-09-27 07:38:41 +0000132 virtual MallocInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000133
134 // Methods for support type inquiry through isa, cast, and dyn_cast:
135 static inline bool classof(const MallocInst *) { return true; }
136 static inline bool classof(const Instruction *I) {
137 return (I->getOpcode() == Instruction::Malloc);
138 }
139 static inline bool classof(const Value *V) {
140 return isa<Instruction>(V) && classof(cast<Instruction>(V));
141 }
142};
143
144
145//===----------------------------------------------------------------------===//
146// AllocaInst Class
147//===----------------------------------------------------------------------===//
148
149/// AllocaInst - an instruction to allocate memory on the stack
150///
151class AllocaInst : public AllocationInst {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000152public:
Owen Anderson50dead02009-07-15 23:53:25 +0000153 explicit AllocaInst(const Type *Ty,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000154 Value *ArraySize = 0,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000155 const Twine &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000156 Instruction *InsertBefore = 0)
Owen Anderson50dead02009-07-15 23:53:25 +0000157 : AllocationInst(Ty, ArraySize, Alloca,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000158 0, NameStr, InsertBefore) {}
Owen Anderson50dead02009-07-15 23:53:25 +0000159 AllocaInst(const Type *Ty,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000160 Value *ArraySize, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000161 BasicBlock *InsertAtEnd)
Owen Anderson50dead02009-07-15 23:53:25 +0000162 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
Chris Lattnerb77780e2006-05-10 04:38:35 +0000163
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000164 AllocaInst(const Type *Ty, const Twine &NameStr,
Chris Lattnerb77780e2006-05-10 04:38:35 +0000165 Instruction *InsertBefore = 0)
Owen Anderson50dead02009-07-15 23:53:25 +0000166 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000167 AllocaInst(const Type *Ty, const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000168 BasicBlock *InsertAtEnd)
Owen Anderson50dead02009-07-15 23:53:25 +0000169 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000170
Owen Anderson50dead02009-07-15 23:53:25 +0000171 AllocaInst(const Type *Ty, Value *ArraySize,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000172 unsigned Align, const Twine &NameStr = "",
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000173 Instruction *InsertBefore = 0)
Owen Anderson50dead02009-07-15 23:53:25 +0000174 : AllocationInst(Ty, ArraySize, Alloca,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000175 Align, NameStr, InsertBefore) {}
Owen Anderson50dead02009-07-15 23:53:25 +0000176 AllocaInst(const Type *Ty, Value *ArraySize,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000177 unsigned Align, const Twine &NameStr,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000178 BasicBlock *InsertAtEnd)
Owen Anderson50dead02009-07-15 23:53:25 +0000179 : AllocationInst(Ty, ArraySize, Alloca,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000180 Align, NameStr, InsertAtEnd) {}
Chris Lattnerf56a8db2006-10-03 17:09:12 +0000181
Nick Lewycky67760642009-09-27 07:38:41 +0000182 virtual AllocaInst *clone() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000183
Chris Lattnerc5dd22a2008-11-26 02:54:17 +0000184 /// isStaticAlloca - Return true if this alloca is in the entry block of the
185 /// function and is a constant size. If so, the code generator will fold it
186 /// into the prolog/epilog code, so it is basically free.
187 bool isStaticAlloca() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000188
189 // Methods for support type inquiry through isa, cast, and dyn_cast:
190 static inline bool classof(const AllocaInst *) { return true; }
191 static inline bool classof(const Instruction *I) {
192 return (I->getOpcode() == Instruction::Alloca);
193 }
194 static inline bool classof(const Value *V) {
195 return isa<Instruction>(V) && classof(cast<Instruction>(V));
196 }
197};
198
199
200//===----------------------------------------------------------------------===//
201// FreeInst Class
202//===----------------------------------------------------------------------===//
203
204/// FreeInst - an instruction to deallocate memory
205///
Chris Lattner454928e2005-01-29 00:31:36 +0000206class FreeInst : public UnaryInstruction {
207 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000208public:
209 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
210 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
211
Nick Lewycky67760642009-09-27 07:38:41 +0000212 virtual FreeInst *clone() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000213
Owen Andersonc9edf0b2007-07-06 23:13:31 +0000214 // Accessor methods for consistency with other memory operations
215 Value *getPointerOperand() { return getOperand(0); }
216 const Value *getPointerOperand() const { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000217
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000218 // Methods for support type inquiry through isa, cast, and dyn_cast:
219 static inline bool classof(const FreeInst *) { return true; }
220 static inline bool classof(const Instruction *I) {
221 return (I->getOpcode() == Instruction::Free);
222 }
223 static inline bool classof(const Value *V) {
224 return isa<Instruction>(V) && classof(cast<Instruction>(V));
225 }
226};
227
228
229//===----------------------------------------------------------------------===//
230// LoadInst Class
231//===----------------------------------------------------------------------===//
232
Chris Lattner88fe29a2005-02-05 01:44:18 +0000233/// LoadInst - an instruction for reading from memory. This uses the
234/// SubclassData field in Value to store whether or not the load is volatile.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000235///
Chris Lattner454928e2005-01-29 00:31:36 +0000236class LoadInst : public UnaryInstruction {
Chris Lattner454928e2005-01-29 00:31:36 +0000237 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000238public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000239 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
240 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000241 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
Christopher Lamb43c7f372007-04-22 19:24:39 +0000242 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000243 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000244 unsigned Align, Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000245 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000246 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000247 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
Evan Cheng1bf9a182008-07-24 00:08:56 +0000248 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000249
Daniel Dunbar3603d7a2009-08-11 18:11:15 +0000250 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
251 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
252 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
253 bool isVolatile = false, Instruction *InsertBefore = 0);
254 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
255 BasicBlock *InsertAtEnd);
256
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000257 /// isVolatile - Return true if this is a load from a volatile memory
258 /// location.
259 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000260 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000261
262 /// setVolatile - Specify whether this is a volatile load or not.
263 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000264 void setVolatile(bool V) {
265 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000266 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000267
Nick Lewycky67760642009-09-27 07:38:41 +0000268 virtual LoadInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000269
Christopher Lamb43c7f372007-04-22 19:24:39 +0000270 /// getAlignment - Return the alignment of the access that is being performed
271 ///
272 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000273 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000274 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000275
Christopher Lamb43c7f372007-04-22 19:24:39 +0000276 void setAlignment(unsigned Align);
277
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000278 Value *getPointerOperand() { return getOperand(0); }
279 const Value *getPointerOperand() const { return getOperand(0); }
280 static unsigned getPointerOperandIndex() { return 0U; }
281
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000282 unsigned getPointerAddressSpace() const {
283 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
284 }
285
286
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000287 // Methods for support type inquiry through isa, cast, and dyn_cast:
288 static inline bool classof(const LoadInst *) { return true; }
289 static inline bool classof(const Instruction *I) {
290 return I->getOpcode() == Instruction::Load;
291 }
292 static inline bool classof(const Value *V) {
293 return isa<Instruction>(V) && classof(cast<Instruction>(V));
294 }
295};
296
297
298//===----------------------------------------------------------------------===//
299// StoreInst Class
300//===----------------------------------------------------------------------===//
301
Misha Brukman9769ab22005-04-21 20:19:05 +0000302/// StoreInst - an instruction for storing to memory
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000303///
304class StoreInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +0000305 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +0000306 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000307public:
Gabor Greif051a9502008-04-06 20:25:17 +0000308 // allocate space for exactly two operands
309 void *operator new(size_t s) {
310 return User::operator new(s, 2);
311 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000312 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
313 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
314 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
315 Instruction *InsertBefore = 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000316 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
317 unsigned Align, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000318 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
Dan Gohman6ab2d182007-07-18 20:51:11 +0000319 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
320 unsigned Align, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000321
322
323 /// isVolatile - Return true if this is a load from a volatile memory
324 /// location.
325 ///
Christopher Lamb43c7f372007-04-22 19:24:39 +0000326 bool isVolatile() const { return SubclassData & 1; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000327
328 /// setVolatile - Specify whether this is a volatile load or not.
329 ///
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000330 void setVolatile(bool V) {
331 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
Christopher Lamb43c7f372007-04-22 19:24:39 +0000332 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000333
Chris Lattner454928e2005-01-29 00:31:36 +0000334 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +0000335 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Chris Lattner454928e2005-01-29 00:31:36 +0000336
Christopher Lamb43c7f372007-04-22 19:24:39 +0000337 /// getAlignment - Return the alignment of the access that is being performed
338 ///
339 unsigned getAlignment() const {
Christopher Lamb032507d2007-04-22 22:22:02 +0000340 return (1 << (SubclassData>>1)) >> 1;
Christopher Lamb43c7f372007-04-22 19:24:39 +0000341 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000342
Christopher Lamb43c7f372007-04-22 19:24:39 +0000343 void setAlignment(unsigned Align);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000344
Nick Lewycky67760642009-09-27 07:38:41 +0000345 virtual StoreInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000346
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000347 Value *getPointerOperand() { return getOperand(1); }
348 const Value *getPointerOperand() const { return getOperand(1); }
349 static unsigned getPointerOperandIndex() { return 1U; }
350
Chris Lattnera07ae6b2009-08-30 19:45:21 +0000351 unsigned getPointerAddressSpace() const {
352 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
353 }
354
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000355 // Methods for support type inquiry through isa, cast, and dyn_cast:
356 static inline bool classof(const StoreInst *) { return true; }
357 static inline bool classof(const Instruction *I) {
358 return I->getOpcode() == Instruction::Store;
359 }
360 static inline bool classof(const Value *V) {
361 return isa<Instruction>(V) && classof(cast<Instruction>(V));
362 }
363};
364
Gabor Greifefe65362008-05-10 08:32:32 +0000365template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +0000366struct OperandTraits<StoreInst> : public FixedNumOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +0000367};
368
369DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000370
371//===----------------------------------------------------------------------===//
372// GetElementPtrInst Class
373//===----------------------------------------------------------------------===//
374
David Greeneb8f74792007-09-04 15:46:09 +0000375// checkType - Simple wrapper function to give a better assertion failure
376// message on bad indexes for a gep instruction.
377//
378static inline const Type *checkType(const Type *Ty) {
379 assert(Ty && "Invalid GetElementPtrInst indices for type!");
380 return Ty;
381}
382
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000383/// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
384/// access elements of arrays and structs
385///
386class GetElementPtrInst : public Instruction {
Gabor Greifefe65362008-05-10 08:32:32 +0000387 GetElementPtrInst(const GetElementPtrInst &GEPI);
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +0000388 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000389 const Twine &NameStr);
390 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000391
392 template<typename InputIterator>
393 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000394 const Twine &NameStr,
David Greeneb8f74792007-09-04 15:46:09 +0000395 // This argument ensures that we have an iterator we can
396 // do arithmetic on in constant time
397 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000398 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000399
David Greeneb8f74792007-09-04 15:46:09 +0000400 if (NumIdx > 0) {
Gabor Greifefe65362008-05-10 08:32:32 +0000401 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +0000402 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Gabor Greifefe65362008-05-10 08:32:32 +0000403 // we have to build an array here
David Greeneb8f74792007-09-04 15:46:09 +0000404 }
405 else {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000406 init(Ptr, 0, NumIdx, NameStr);
David Greeneb8f74792007-09-04 15:46:09 +0000407 }
David Greeneb8f74792007-09-04 15:46:09 +0000408 }
409
410 /// getIndexedType - Returns the type of the element that would be loaded with
411 /// a load instruction with the specified parameters.
412 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000413 /// Null is returned if the indices are invalid for the specified
David Greeneb8f74792007-09-04 15:46:09 +0000414 /// pointer type.
415 ///
David Greeneb8f74792007-09-04 15:46:09 +0000416 template<typename InputIterator>
417 static const Type *getIndexedType(const Type *Ptr,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000418 InputIterator IdxBegin,
David Greeneb8f74792007-09-04 15:46:09 +0000419 InputIterator IdxEnd,
David Greeneb8f74792007-09-04 15:46:09 +0000420 // This argument ensures that we
421 // have an iterator we can do
422 // arithmetic on in constant time
423 std::random_access_iterator_tag) {
Evan Cheng34cd4a42008-05-05 18:30:58 +0000424 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
David Greeneb8f74792007-09-04 15:46:09 +0000425
Dan Gohman041e2eb2008-05-15 19:50:34 +0000426 if (NumIdx > 0)
David Greeneb8f74792007-09-04 15:46:09 +0000427 // This requires that the iterator points to contiguous memory.
David Greene2d5a0b92008-10-29 00:30:54 +0000428 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +0000429 else
430 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
David Greeneb8f74792007-09-04 15:46:09 +0000431 }
432
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000433 /// Constructors - Create a getelementptr instruction with a base pointer an
434 /// list of indices. The first ctor can optionally insert before an existing
435 /// instruction, the second appends the new instruction to the specified
436 /// BasicBlock.
David Greeneb8f74792007-09-04 15:46:09 +0000437 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000438 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
Gabor Greifefe65362008-05-10 08:32:32 +0000439 InputIterator IdxEnd,
440 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000441 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000442 Instruction *InsertBefore);
David Greeneb8f74792007-09-04 15:46:09 +0000443 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000444 inline GetElementPtrInst(Value *Ptr,
445 InputIterator IdxBegin, InputIterator IdxEnd,
446 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000447 const Twine &NameStr, BasicBlock *InsertAtEnd);
David Greeneb8f74792007-09-04 15:46:09 +0000448
Chris Lattner38bacf22005-05-03 05:43:30 +0000449 /// Constructors - These two constructors are convenience methods because one
450 /// and two index getelementptr instructions are so common.
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000451 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +0000452 Instruction *InsertBefore = 0);
Chris Lattner38bacf22005-05-03 05:43:30 +0000453 GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000454 const Twine &NameStr, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000455public:
456 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000457 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
Gabor Greif051a9502008-04-06 20:25:17 +0000458 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000459 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +0000460 Instruction *InsertBefore = 0) {
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000461 typename std::iterator_traits<InputIterator>::difference_type Values =
Gabor Greifefe65362008-05-10 08:32:32 +0000462 1 + std::distance(IdxBegin, IdxEnd);
463 return new(Values)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000464 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000465 }
466 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000467 static GetElementPtrInst *Create(Value *Ptr,
468 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000469 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000470 BasicBlock *InsertAtEnd) {
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000471 typename std::iterator_traits<InputIterator>::difference_type Values =
Gabor Greifefe65362008-05-10 08:32:32 +0000472 1 + std::distance(IdxBegin, IdxEnd);
473 return new(Values)
Evan Cheng1bf9a182008-07-24 00:08:56 +0000474 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000475 }
476
Gabor Greifefe65362008-05-10 08:32:32 +0000477 /// Constructors - These two creators are convenience methods because one
478 /// index getelementptr instructions are so common.
Gabor Greif051a9502008-04-06 20:25:17 +0000479 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000480 const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +0000481 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000482 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +0000483 }
484 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000485 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000486 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000487 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +0000488 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000489
Dan Gohmane2574d32009-08-11 17:57:01 +0000490 /// Create an "inbounds" getelementptr. See the documentation for the
491 /// "inbounds" flag in LangRef.html for details.
492 template<typename InputIterator>
493 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
494 InputIterator IdxEnd,
495 const Twine &NameStr = "",
496 Instruction *InsertBefore = 0) {
497 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
498 NameStr, InsertBefore);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000499 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000500 return GEP;
501 }
502 template<typename InputIterator>
503 static GetElementPtrInst *CreateInBounds(Value *Ptr,
504 InputIterator IdxBegin,
505 InputIterator IdxEnd,
506 const Twine &NameStr,
507 BasicBlock *InsertAtEnd) {
508 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
509 NameStr, InsertAtEnd);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000510 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000511 return GEP;
512 }
513 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
514 const Twine &NameStr = "",
515 Instruction *InsertBefore = 0) {
516 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000517 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000518 return GEP;
519 }
520 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
521 const Twine &NameStr,
522 BasicBlock *InsertAtEnd) {
523 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000524 GEP->setIsInBounds(true);
Dan Gohmane2574d32009-08-11 17:57:01 +0000525 return GEP;
526 }
527
Nick Lewycky67760642009-09-27 07:38:41 +0000528 virtual GetElementPtrInst *clone() const;
Misha Brukman9769ab22005-04-21 20:19:05 +0000529
Gabor Greifefe65362008-05-10 08:32:32 +0000530 /// Transparently provide more efficient getOperand methods.
531 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
532
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000533 // getType - Overload to return most specific pointer type...
Devang Patel4d4a5e02008-02-23 01:11:02 +0000534 const PointerType *getType() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000535 return reinterpret_cast<const PointerType*>(Instruction::getType());
536 }
537
538 /// getIndexedType - Returns the type of the element that would be loaded with
539 /// a load instruction with the specified parameters.
540 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +0000541 /// Null is returned if the indices are invalid for the specified
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000542 /// pointer type.
543 ///
David Greeneb8f74792007-09-04 15:46:09 +0000544 template<typename InputIterator>
Misha Brukman9769ab22005-04-21 20:19:05 +0000545 static const Type *getIndexedType(const Type *Ptr,
David Greeneb8f74792007-09-04 15:46:09 +0000546 InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +0000547 InputIterator IdxEnd) {
548 return getIndexedType(Ptr, IdxBegin, IdxEnd,
David Greeneb8f74792007-09-04 15:46:09 +0000549 typename std::iterator_traits<InputIterator>::
Dan Gohman041e2eb2008-05-15 19:50:34 +0000550 iterator_category());
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000551 }
Matthijs Kooijmane2afded2008-07-29 08:46:11 +0000552
553 static const Type *getIndexedType(const Type *Ptr,
554 Value* const *Idx, unsigned NumIdx);
555
556 static const Type *getIndexedType(const Type *Ptr,
557 uint64_t const *Idx, unsigned NumIdx);
558
Chris Lattner38bacf22005-05-03 05:43:30 +0000559 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
Misha Brukman9769ab22005-04-21 20:19:05 +0000560
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000561 inline op_iterator idx_begin() { return op_begin()+1; }
562 inline const_op_iterator idx_begin() const { return op_begin()+1; }
563 inline op_iterator idx_end() { return op_end(); }
564 inline const_op_iterator idx_end() const { return op_end(); }
565
566 Value *getPointerOperand() {
567 return getOperand(0);
568 }
569 const Value *getPointerOperand() const {
570 return getOperand(0);
571 }
572 static unsigned getPointerOperandIndex() {
573 return 0U; // get index for modifying correct operand
574 }
Chris Lattner8a67ac52009-08-30 20:06:40 +0000575
576 unsigned getPointerAddressSpace() const {
577 return cast<PointerType>(getType())->getAddressSpace();
578 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000579
Chris Lattner3bc6ced2009-01-09 05:27:40 +0000580 /// getPointerOperandType - Method to return the pointer operand as a
581 /// PointerType.
582 const PointerType *getPointerOperandType() const {
583 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
584 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000585
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000586
Devang Patel4d4a5e02008-02-23 01:11:02 +0000587 unsigned getNumIndices() const { // Note: always non-negative
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000588 return getNumOperands() - 1;
589 }
Misha Brukman9769ab22005-04-21 20:19:05 +0000590
Devang Patel4d4a5e02008-02-23 01:11:02 +0000591 bool hasIndices() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000592 return getNumOperands() > 1;
593 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000594
Chris Lattner6f771d42007-04-14 00:12:57 +0000595 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
596 /// zeros. If so, the result pointer and the first operand have the same
597 /// value, just potentially different types.
598 bool hasAllZeroIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000599
Chris Lattner6b0974c2007-04-27 20:35:56 +0000600 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
601 /// constant integers. If so, the result pointer and the first operand have
602 /// a constant offset between them.
603 bool hasAllConstantIndices() const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000604
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000605 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
606 /// See LangRef.html for the meaning of inbounds on a getelementptr.
Nick Lewyckyae05e7d2009-09-27 21:33:04 +0000607 void setIsInBounds(bool b = true);
608
609 /// isInBounds - Determine whether the GEP has the inbounds flag.
610 bool isInBounds() const;
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000611
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000612 // Methods for support type inquiry through isa, cast, and dyn_cast:
613 static inline bool classof(const GetElementPtrInst *) { return true; }
614 static inline bool classof(const Instruction *I) {
615 return (I->getOpcode() == Instruction::GetElementPtr);
616 }
617 static inline bool classof(const Value *V) {
618 return isa<Instruction>(V) && classof(cast<Instruction>(V));
619 }
620};
621
Gabor Greifefe65362008-05-10 08:32:32 +0000622template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +0000623struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
Gabor Greifefe65362008-05-10 08:32:32 +0000624};
625
626template<typename InputIterator>
627GetElementPtrInst::GetElementPtrInst(Value *Ptr,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000628 InputIterator IdxBegin,
Gabor Greifefe65362008-05-10 08:32:32 +0000629 InputIterator IdxEnd,
630 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000631 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000632 Instruction *InsertBefore)
633 : Instruction(PointerType::get(checkType(
634 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000635 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000636 cast<PointerType>(Ptr->getType())
637 ->getAddressSpace()),
638 GetElementPtr,
639 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
640 Values, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000641 init(Ptr, IdxBegin, IdxEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000642 typename std::iterator_traits<InputIterator>::iterator_category());
643}
644template<typename InputIterator>
645GetElementPtrInst::GetElementPtrInst(Value *Ptr,
646 InputIterator IdxBegin,
647 InputIterator IdxEnd,
648 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000649 const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000650 BasicBlock *InsertAtEnd)
651 : Instruction(PointerType::get(checkType(
652 getIndexedType(Ptr->getType(),
Dan Gohman041e2eb2008-05-15 19:50:34 +0000653 IdxBegin, IdxEnd)),
Gabor Greifefe65362008-05-10 08:32:32 +0000654 cast<PointerType>(Ptr->getType())
655 ->getAddressSpace()),
656 GetElementPtr,
657 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
658 Values, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +0000659 init(Ptr, IdxBegin, IdxEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +0000660 typename std::iterator_traits<InputIterator>::iterator_category());
661}
662
663
664DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
665
666
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000667//===----------------------------------------------------------------------===//
Reid Spencer45fb3f32006-11-20 01:22:35 +0000668// ICmpInst Class
669//===----------------------------------------------------------------------===//
670
671/// This instruction compares its operands according to the predicate given
Nate Begemanac80ade2008-05-12 19:01:56 +0000672/// to the constructor. It only operates on integers or pointers. The operands
673/// must be identical types.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000674/// @brief Represent an integer comparison operator.
675class ICmpInst: public CmpInst {
676public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000677 /// @brief Constructor with insert-before-instruction semantics.
678 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000679 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +0000680 Predicate pred, ///< The predicate to use for the comparison
681 Value *LHS, ///< The left-hand-side of the expression
682 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000683 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000684 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +0000685 Instruction::ICmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000686 InsertBefore) {
687 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
688 pred <= CmpInst::LAST_ICMP_PREDICATE &&
689 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000690 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000691 "Both operands to ICmp instruction are not of the same type!");
692 // Check that the operands are the right type
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000693 assert((getOperand(0)->getType()->isIntOrIntVector() ||
Nate Begeman31cd33a2008-05-14 20:28:31 +0000694 isa<PointerType>(getOperand(0)->getType())) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000695 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000696 }
697
Owen Anderson333c4002009-07-09 23:48:35 +0000698 /// @brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000699 ICmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000700 BasicBlock &InsertAtEnd, ///< Block to insert into.
701 Predicate pred, ///< The predicate to use for the comparison
702 Value *LHS, ///< The left-hand-side of the expression
703 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000704 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000705 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000706 Instruction::ICmp, pred, LHS, RHS, NameStr,
707 &InsertAtEnd) {
708 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
709 pred <= CmpInst::LAST_ICMP_PREDICATE &&
710 "Invalid ICmp predicate value");
711 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
712 "Both operands to ICmp instruction are not of the same type!");
713 // Check that the operands are the right type
714 assert((getOperand(0)->getType()->isIntOrIntVector() ||
715 isa<PointerType>(getOperand(0)->getType())) &&
716 "Invalid operand types for ICmp instruction");
717 }
718
719 /// @brief Constructor with no-insertion semantics
720 ICmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +0000721 Predicate pred, ///< The predicate to use for the comparison
722 Value *LHS, ///< The left-hand-side of the expression
723 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000724 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000725 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000726 Instruction::ICmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +0000727 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
728 pred <= CmpInst::LAST_ICMP_PREDICATE &&
729 "Invalid ICmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000730 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000731 "Both operands to ICmp instruction are not of the same type!");
732 // Check that the operands are the right type
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000733 assert((getOperand(0)->getType()->isIntOrIntVector() ||
Nate Begeman31cd33a2008-05-14 20:28:31 +0000734 isa<PointerType>(getOperand(0)->getType())) &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000735 "Invalid operand types for ICmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000736 }
737
Reid Spencere4d87aa2006-12-23 06:05:41 +0000738 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
739 /// @returns the predicate that would be the result if the operand were
740 /// regarded as signed.
741 /// @brief Return the signed version of the predicate
742 Predicate getSignedPredicate() const {
743 return getSignedPredicate(getPredicate());
744 }
745
746 /// This is a static version that you can use without an instruction.
747 /// @brief Return the signed version of the predicate.
748 static Predicate getSignedPredicate(Predicate pred);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000749
Nick Lewycky4189a532008-01-28 03:48:02 +0000750 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
751 /// @returns the predicate that would be the result if the operand were
752 /// regarded as unsigned.
753 /// @brief Return the unsigned version of the predicate
754 Predicate getUnsignedPredicate() const {
755 return getUnsignedPredicate(getPredicate());
756 }
757
758 /// This is a static version that you can use without an instruction.
759 /// @brief Return the unsigned version of the predicate.
760 static Predicate getUnsignedPredicate(Predicate pred);
761
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000762 /// isEquality - Return true if this predicate is either EQ or NE. This also
763 /// tests for commutativity.
764 static bool isEquality(Predicate P) {
765 return P == ICMP_EQ || P == ICMP_NE;
766 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000767
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000768 /// isEquality - Return true if this predicate is either EQ or NE. This also
769 /// tests for commutativity.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000770 bool isEquality() const {
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000771 return isEquality(getPredicate());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000772 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000773
774 /// @returns true if the predicate of this ICmpInst is commutative
775 /// @brief Determine if this relation is commutative.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000776 bool isCommutative() const { return isEquality(); }
777
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000778 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000779 ///
Reid Spencer45fb3f32006-11-20 01:22:35 +0000780 bool isRelational() const {
781 return !isEquality();
782 }
783
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000784 /// isRelational - Return true if the predicate is relational (not EQ or NE).
Chris Lattnerc2bfadb2007-11-22 23:43:29 +0000785 ///
786 static bool isRelational(Predicate P) {
787 return !isEquality(P);
788 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000789
Reid Spencere4d87aa2006-12-23 06:05:41 +0000790 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
791 /// @brief Determine if this instruction's predicate is signed.
Chris Lattner5bda9e42007-09-15 06:51:03 +0000792 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000793
794 /// @returns true if the predicate provided is signed, false otherwise
795 /// @brief Determine if the predicate is signed.
796 static bool isSignedPredicate(Predicate pred);
797
Nick Lewyckyfc1efbb2008-05-17 07:33:39 +0000798 /// @returns true if the specified compare predicate is
799 /// true when both operands are equal...
800 /// @brief Determine if the icmp is true when both operands are equal
801 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
802 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
803 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
804 pred == ICmpInst::ICMP_SLE;
805 }
806
807 /// @returns true if the specified compare instruction is
808 /// true when both operands are equal...
809 /// @brief Determine if the ICmpInst returns true when both operands are equal
810 bool isTrueWhenEqual() {
811 return isTrueWhenEqual(getPredicate());
812 }
813
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000814 /// Initialize a set of values that all satisfy the predicate with C.
Reid Spencer3da43842007-02-28 22:00:54 +0000815 /// @brief Make a ConstantRange for a relation with a constant value.
816 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
817
Reid Spencer45fb3f32006-11-20 01:22:35 +0000818 /// Exchange the two operands to this instruction in such a way that it does
819 /// not modify the semantics of the instruction. The predicate value may be
820 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000821 /// (e.g. ult).
Reid Spencer45fb3f32006-11-20 01:22:35 +0000822 /// @brief Swap operands and adjust predicate.
823 void swapOperands() {
824 SubclassData = getSwappedPredicate();
Gabor Greif94fb68b2008-05-13 22:51:52 +0000825 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000826 }
827
Nick Lewycky67760642009-09-27 07:38:41 +0000828 virtual ICmpInst *clone() const;
Chris Lattnercd406fe2007-08-24 20:48:18 +0000829
Reid Spencer45fb3f32006-11-20 01:22:35 +0000830 // Methods for support type inquiry through isa, cast, and dyn_cast:
831 static inline bool classof(const ICmpInst *) { return true; }
832 static inline bool classof(const Instruction *I) {
833 return I->getOpcode() == Instruction::ICmp;
834 }
835 static inline bool classof(const Value *V) {
836 return isa<Instruction>(V) && classof(cast<Instruction>(V));
837 }
Dan Gohmanf72fb672008-09-09 01:02:47 +0000838
Reid Spencer45fb3f32006-11-20 01:22:35 +0000839};
840
841//===----------------------------------------------------------------------===//
842// FCmpInst Class
843//===----------------------------------------------------------------------===//
844
845/// This instruction compares its operands according to the predicate given
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000846/// to the constructor. It only operates on floating point values or packed
Reid Spencer45fb3f32006-11-20 01:22:35 +0000847/// vectors of floating point values. The operands must be identical types.
848/// @brief Represents a floating point comparison operator.
849class FCmpInst: public CmpInst {
850public:
Reid Spencer45fb3f32006-11-20 01:22:35 +0000851 /// @brief Constructor with insert-before-instruction semantics.
852 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000853 Instruction *InsertBefore, ///< Where to insert
Reid Spencer45fb3f32006-11-20 01:22:35 +0000854 Predicate pred, ///< The predicate to use for the comparison
855 Value *LHS, ///< The left-hand-side of the expression
856 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000857 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000858 ) : CmpInst(makeCmpResultType(LHS->getType()),
Dan Gohmanf72fb672008-09-09 01:02:47 +0000859 Instruction::FCmp, pred, LHS, RHS, NameStr,
Nate Begemanac80ade2008-05-12 19:01:56 +0000860 InsertBefore) {
861 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
862 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000863 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000864 "Both operands to FCmp instruction are not of the same type!");
865 // Check that the operands are the right type
Dan Gohmanf72fb672008-09-09 01:02:47 +0000866 assert(getOperand(0)->getType()->isFPOrFPVector() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000867 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000868 }
Owen Anderson333c4002009-07-09 23:48:35 +0000869
870 /// @brief Constructor with insert-at-end semantics.
Reid Spencer45fb3f32006-11-20 01:22:35 +0000871 FCmpInst(
Owen Anderson333c4002009-07-09 23:48:35 +0000872 BasicBlock &InsertAtEnd, ///< Block to insert into.
873 Predicate pred, ///< The predicate to use for the comparison
874 Value *LHS, ///< The left-hand-side of the expression
875 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000876 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000877 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000878 Instruction::FCmp, pred, LHS, RHS, NameStr,
879 &InsertAtEnd) {
880 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
881 "Invalid FCmp predicate value");
882 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
883 "Both operands to FCmp instruction are not of the same type!");
884 // Check that the operands are the right type
885 assert(getOperand(0)->getType()->isFPOrFPVector() &&
886 "Invalid operand types for FCmp instruction");
887 }
888
889 /// @brief Constructor with no-insertion semantics
890 FCmpInst(
Reid Spencer45fb3f32006-11-20 01:22:35 +0000891 Predicate pred, ///< The predicate to use for the comparison
892 Value *LHS, ///< The left-hand-side of the expression
893 Value *RHS, ///< The right-hand-side of the expression
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000894 const Twine &NameStr = "" ///< Name of the instruction
Owen Andersondebcb012009-07-29 22:17:13 +0000895 ) : CmpInst(makeCmpResultType(LHS->getType()),
Owen Anderson333c4002009-07-09 23:48:35 +0000896 Instruction::FCmp, pred, LHS, RHS, NameStr) {
Nate Begemanac80ade2008-05-12 19:01:56 +0000897 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
898 "Invalid FCmp predicate value");
Nate Begeman31cd33a2008-05-14 20:28:31 +0000899 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000900 "Both operands to FCmp instruction are not of the same type!");
901 // Check that the operands are the right type
Dan Gohmanf72fb672008-09-09 01:02:47 +0000902 assert(getOperand(0)->getType()->isFPOrFPVector() &&
Nate Begemanac80ade2008-05-12 19:01:56 +0000903 "Invalid operand types for FCmp instruction");
Reid Spencer45fb3f32006-11-20 01:22:35 +0000904 }
905
Reid Spencer45fb3f32006-11-20 01:22:35 +0000906 /// @returns true if the predicate of this instruction is EQ or NE.
907 /// @brief Determine if this is an equality predicate.
908 bool isEquality() const {
909 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
910 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
911 }
Dan Gohman793df072008-09-16 16:44:00 +0000912
913 /// @returns true if the predicate of this instruction is commutative.
914 /// @brief Determine if this is a commutative predicate.
915 bool isCommutative() const {
916 return isEquality() ||
917 SubclassData == FCMP_FALSE ||
918 SubclassData == FCMP_TRUE ||
919 SubclassData == FCMP_ORD ||
920 SubclassData == FCMP_UNO;
921 }
Reid Spencer45fb3f32006-11-20 01:22:35 +0000922
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000923 /// @returns true if the predicate is relational (not EQ or NE).
Reid Spencer45fb3f32006-11-20 01:22:35 +0000924 /// @brief Determine if this a relational predicate.
925 bool isRelational() const { return !isEquality(); }
926
927 /// Exchange the two operands to this instruction in such a way that it does
928 /// not modify the semantics of the instruction. The predicate value may be
929 /// changed to retain the same result if the predicate is order dependent
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000930 /// (e.g. ult).
Reid Spencer45fb3f32006-11-20 01:22:35 +0000931 /// @brief Swap operands and adjust predicate.
932 void swapOperands() {
933 SubclassData = getSwappedPredicate();
Gabor Greif94fb68b2008-05-13 22:51:52 +0000934 Op<0>().swap(Op<1>());
Reid Spencer45fb3f32006-11-20 01:22:35 +0000935 }
936
Nick Lewycky67760642009-09-27 07:38:41 +0000937 virtual FCmpInst *clone() const;
Chris Lattnercd406fe2007-08-24 20:48:18 +0000938
Reid Spencer45fb3f32006-11-20 01:22:35 +0000939 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
940 static inline bool classof(const FCmpInst *) { return true; }
941 static inline bool classof(const Instruction *I) {
942 return I->getOpcode() == Instruction::FCmp;
943 }
944 static inline bool classof(const Value *V) {
945 return isa<Instruction>(V) && classof(cast<Instruction>(V));
946 }
947};
948
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000949//===----------------------------------------------------------------------===//
950// CallInst Class
951//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000952/// CallInst - This class represents a function call, abstracting a target
Chris Lattner3340ffe2005-05-06 20:26:26 +0000953/// machine's calling convention. This class uses low bit of the SubClassData
954/// field to indicate whether or not this is a tail call. The rest of the bits
955/// hold the calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000956///
David Greene52eec542007-08-01 03:43:44 +0000957
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000958class CallInst : public Instruction {
Devang Patel05988662008-09-25 21:00:45 +0000959 AttrListPtr AttributeList; ///< parameter attributes for call
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000960 CallInst(const CallInst &CI);
Chris Lattnerd54f4322007-02-13 00:58:44 +0000961 void init(Value *Func, Value* const *Params, unsigned NumParams);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +0000962 void init(Value *Func, Value *Actual1, Value *Actual2);
963 void init(Value *Func, Value *Actual);
964 void init(Value *Func);
965
David Greene52eec542007-08-01 03:43:44 +0000966 template<typename InputIterator>
967 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000968 const Twine &NameStr,
David Greene52eec542007-08-01 03:43:44 +0000969 // This argument ensures that we have an iterator we can
970 // do arithmetic on in constant time
971 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000972 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +0000973
Chris Lattnera5c0d1e2007-08-29 16:32:50 +0000974 // This requires that the iterator points to contiguous memory.
975 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
Evan Cheng1bf9a182008-07-24 00:08:56 +0000976 setName(NameStr);
David Greene52eec542007-08-01 03:43:44 +0000977 }
978
David Greene52eec542007-08-01 03:43:44 +0000979 /// Construct a CallInst given a range of arguments. InputIterator
980 /// must be a random-access iterator pointing to contiguous storage
981 /// (e.g. a std::vector<>::iterator). Checks are made for
982 /// random-accessness but not for contiguous storage as that would
983 /// incur runtime overhead.
984 /// @brief Construct a CallInst from a range of arguments
985 template<typename InputIterator>
986 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000987 const Twine &NameStr, Instruction *InsertBefore);
David Greene52eec542007-08-01 03:43:44 +0000988
989 /// Construct a CallInst given a range of arguments. InputIterator
990 /// must be a random-access iterator pointing to contiguous storage
991 /// (e.g. a std::vector<>::iterator). Checks are made for
992 /// random-accessness but not for contiguous storage as that would
993 /// incur runtime overhead.
994 /// @brief Construct a CallInst from a range of arguments
995 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +0000996 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000997 const Twine &NameStr, BasicBlock *InsertAtEnd);
David Greene52eec542007-08-01 03:43:44 +0000998
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000999 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001000 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001001 CallInst(Value *F, Value *Actual, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001002 BasicBlock *InsertAtEnd);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001003 explicit CallInst(Value *F, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001004 Instruction *InsertBefore);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001005 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001006public:
1007 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00001008 static CallInst *Create(Value *Func,
1009 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001010 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001011 Instruction *InsertBefore = 0) {
Bill Wendlingc2e73532008-05-10 19:59:59 +00001012 return new((unsigned)(ArgEnd - ArgBegin + 1))
Evan Cheng1bf9a182008-07-24 00:08:56 +00001013 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001014 }
1015 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00001016 static CallInst *Create(Value *Func,
1017 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001018 const Twine &NameStr, BasicBlock *InsertAtEnd) {
Bill Wendlingc2e73532008-05-10 19:59:59 +00001019 return new((unsigned)(ArgEnd - ArgBegin + 1))
Evan Cheng1bf9a182008-07-24 00:08:56 +00001020 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001021 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001022 static CallInst *Create(Value *F, Value *Actual,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001023 const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001024 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001025 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001026 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001027 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
Gabor Greif051a9502008-04-06 20:25:17 +00001028 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001029 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001030 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001031 static CallInst *Create(Value *F, const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001032 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001033 return new(1) CallInst(F, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001034 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001035 static CallInst *Create(Value *F, const Twine &NameStr,
Evan Cheng34cd4a42008-05-05 18:30:58 +00001036 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001037 return new(1) CallInst(F, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001038 }
Evan Chengfabcb912009-09-10 04:36:43 +00001039 /// CreateMalloc - Generate the IR for a call to malloc:
1040 /// 1. Compute the malloc call's argument as the specified type's size,
1041 /// possibly multiplied by the array size if the array size is not
1042 /// constant 1.
1043 /// 2. Call malloc with that argument.
1044 /// 3. Bitcast the result of the malloc call to the specified type.
Victor Hernandez88d98392009-09-18 19:20:02 +00001045 static Value *CreateMalloc(Instruction *InsertBefore, const Type *IntPtrTy,
1046 const Type *AllocTy, Value *ArraySize = 0,
1047 const Twine &Name = "");
1048 static Value *CreateMalloc(BasicBlock *InsertAtEnd, const Type *IntPtrTy,
1049 const Type *AllocTy, Value *ArraySize = 0,
Victor Hernandez3e0c99a2009-09-25 18:11:52 +00001050 const Twine &Name = "");
Gabor Greif051a9502008-04-06 20:25:17 +00001051
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001052 ~CallInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001053
Chris Lattner3340ffe2005-05-06 20:26:26 +00001054 bool isTailCall() const { return SubclassData & 1; }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001055 void setTailCall(bool isTC = true) {
1056 SubclassData = (SubclassData & ~1) | unsigned(isTC);
Chris Lattner3340ffe2005-05-06 20:26:26 +00001057 }
1058
Nick Lewycky67760642009-09-27 07:38:41 +00001059 virtual CallInst *clone() const;
Dan Gohmanf2752502008-09-26 21:38:45 +00001060
1061 /// Provide fast operand accessors
1062 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001063
Chris Lattner3340ffe2005-05-06 20:26:26 +00001064 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1065 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001066 CallingConv::ID getCallingConv() const {
1067 return static_cast<CallingConv::ID>(SubclassData >> 1);
1068 }
1069 void setCallingConv(CallingConv::ID CC) {
1070 SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
Chris Lattner3340ffe2005-05-06 20:26:26 +00001071 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00001072
Devang Patel05988662008-09-25 21:00:45 +00001073 /// getAttributes - Return the parameter attributes for this call.
Chris Lattner041221c2008-03-13 04:33:03 +00001074 ///
Devang Patel05988662008-09-25 21:00:45 +00001075 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001076
Dan Gohmanf2752502008-09-26 21:38:45 +00001077 /// setAttributes - Set the parameter attributes for this call.
1078 ///
Devang Patel05988662008-09-25 21:00:45 +00001079 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001080
Devang Patel05988662008-09-25 21:00:45 +00001081 /// addAttribute - adds the attribute to the list of attributes.
1082 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsdc024672007-11-27 13:23:08 +00001083
Devang Patel05988662008-09-25 21:00:45 +00001084 /// removeAttribute - removes the attribute from the list of attributes.
1085 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00001086
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001087 /// @brief Determine whether the call or the callee has the given attribute.
Dan Gohmanf2752502008-09-26 21:38:45 +00001088 bool paramHasAttr(unsigned i, Attributes attr) const;
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00001089
Dale Johannesen08e78b12008-02-22 17:49:45 +00001090 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001091 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00001092 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001093 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00001094
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001095 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001096 bool doesNotAccessMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00001097 return paramHasAttr(~0, Attribute::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001098 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001099 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001100 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1101 else removeAttribute(~0, Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00001102 }
1103
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001104 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001105 bool onlyReadsMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00001106 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001107 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001108 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001109 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1110 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00001111 }
1112
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001113 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001114 bool doesNotReturn() const {
Devang Patel19c87462008-09-26 22:53:05 +00001115 return paramHasAttr(~0, Attribute::NoReturn);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001116 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001117 void setDoesNotReturn(bool DoesNotReturn = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001118 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1119 else removeAttribute(~0, Attribute::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00001120 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00001121
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001122 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001123 bool doesNotThrow() const {
Devang Patel19c87462008-09-26 22:53:05 +00001124 return paramHasAttr(~0, Attribute::NoUnwind);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001125 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00001126 void setDoesNotThrow(bool DoesNotThrow = true) {
Devang Patel19c87462008-09-26 22:53:05 +00001127 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1128 else removeAttribute(~0, Attribute::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00001129 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00001130
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001131 /// @brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00001132 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001133 bool hasStructRetAttr() const {
1134 // Be friendly and also check the callee.
Devang Patel05988662008-09-25 21:00:45 +00001135 return paramHasAttr(1, Attribute::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001136 }
Reid Spencer4746ecf2007-04-09 15:01:12 +00001137
Evan Chengf4a54982008-01-12 18:57:32 +00001138 /// @brief Determine if any call argument is an aggregate passed by value.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001139 bool hasByValArgument() const {
Devang Patel05988662008-09-25 21:00:45 +00001140 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00001141 }
Evan Chengf4a54982008-01-12 18:57:32 +00001142
Dan Gohmanf2752502008-09-26 21:38:45 +00001143 /// getCalledFunction - Return the function called, or null if this is an
1144 /// indirect function invocation.
1145 ///
Chris Lattner721aef62004-11-18 17:46:57 +00001146 Function *getCalledFunction() const {
Gabor Greif7e07c002009-03-12 23:13:03 +00001147 return dyn_cast<Function>(Op<0>());
Chris Lattner721aef62004-11-18 17:46:57 +00001148 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001149
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001150 /// getCalledValue - Get a pointer to the function that is invoked by this
Reid Spencerc25ec252006-12-29 04:10:59 +00001151 /// instruction
Gabor Greif7e07c002009-03-12 23:13:03 +00001152 const Value *getCalledValue() const { return Op<0>(); }
1153 Value *getCalledValue() { return Op<0>(); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001154
1155 // Methods for support type inquiry through isa, cast, and dyn_cast:
1156 static inline bool classof(const CallInst *) { return true; }
1157 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001158 return I->getOpcode() == Instruction::Call;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001159 }
1160 static inline bool classof(const Value *V) {
1161 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1162 }
1163};
1164
Gabor Greifefe65362008-05-10 08:32:32 +00001165template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001166struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
Gabor Greifefe65362008-05-10 08:32:32 +00001167};
1168
1169template<typename InputIterator>
1170CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001171 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001172 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1173 ->getElementType())->getReturnType(),
1174 Instruction::Call,
1175 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001176 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001177 init(Func, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001178 typename std::iterator_traits<InputIterator>::iterator_category());
1179}
1180
1181template<typename InputIterator>
1182CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001183 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00001184 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1185 ->getElementType())->getReturnType(),
1186 Instruction::Call,
1187 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
Bill Wendling1b2f7292008-05-10 11:26:52 +00001188 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001189 init(Func, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001190 typename std::iterator_traits<InputIterator>::iterator_category());
1191}
1192
1193DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1194
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001195//===----------------------------------------------------------------------===//
1196// SelectInst Class
1197//===----------------------------------------------------------------------===//
1198
1199/// SelectInst - This class represents the LLVM 'select' instruction.
1200///
1201class SelectInst : public Instruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001202 void init(Value *C, Value *S1, Value *S2) {
Chris Lattnerb76ec322008-12-29 00:12:50 +00001203 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
Gabor Greifefe65362008-05-10 08:32:32 +00001204 Op<0>() = C;
1205 Op<1>() = S1;
1206 Op<2>() = S2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001207 }
1208
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001209 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00001210 Instruction *InsertBefore)
1211 : Instruction(S1->getType(), Instruction::Select,
1212 &Op<0>(), 3, InsertBefore) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001213 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001214 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001215 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001216 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001217 BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00001218 : Instruction(S1->getType(), Instruction::Select,
1219 &Op<0>(), 3, InsertAtEnd) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001220 init(C, S1, S2);
Evan Cheng1bf9a182008-07-24 00:08:56 +00001221 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001222 }
Gabor Greif051a9502008-04-06 20:25:17 +00001223public:
Evan Chengd69bb1a2008-05-05 17:41:03 +00001224 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001225 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00001226 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001227 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001228 }
Evan Chengd69bb1a2008-05-05 17:41:03 +00001229 static SelectInst *Create(Value *C, Value *S1, Value *S2,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001230 const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001231 BasicBlock *InsertAtEnd) {
1232 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001233 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001234
Chris Lattner97159122009-09-08 03:32:53 +00001235 const Value *getCondition() const { return Op<0>(); }
1236 const Value *getTrueValue() const { return Op<1>(); }
1237 const Value *getFalseValue() const { return Op<2>(); }
1238 Value *getCondition() { return Op<0>(); }
1239 Value *getTrueValue() { return Op<1>(); }
1240 Value *getFalseValue() { return Op<2>(); }
1241
Chris Lattnerb76ec322008-12-29 00:12:50 +00001242 /// areInvalidOperands - Return a string if the specified operands are invalid
1243 /// for a select operation, otherwise return null.
1244 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
Chris Lattner454928e2005-01-29 00:31:36 +00001245
1246 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001247 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001248
1249 OtherOps getOpcode() const {
1250 return static_cast<OtherOps>(Instruction::getOpcode());
1251 }
1252
Nick Lewycky67760642009-09-27 07:38:41 +00001253 virtual SelectInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001254
1255 // Methods for support type inquiry through isa, cast, and dyn_cast:
1256 static inline bool classof(const SelectInst *) { return true; }
1257 static inline bool classof(const Instruction *I) {
1258 return I->getOpcode() == Instruction::Select;
1259 }
1260 static inline bool classof(const Value *V) {
1261 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1262 }
1263};
1264
Gabor Greifefe65362008-05-10 08:32:32 +00001265template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001266struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001267};
1268
1269DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1270
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001271//===----------------------------------------------------------------------===//
1272// VAArgInst Class
1273//===----------------------------------------------------------------------===//
1274
1275/// VAArgInst - This class represents the va_arg llvm instruction, which returns
Andrew Lenharthf5428212005-06-18 18:31:30 +00001276/// an argument of the specified type given a va_list and increments that list
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001277///
Chris Lattner454928e2005-01-29 00:31:36 +00001278class VAArgInst : public UnaryInstruction {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001279public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001280 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001281 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001282 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001283 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001284 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001285 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001286 BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001287 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001288 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001289 }
1290
Nick Lewycky67760642009-09-27 07:38:41 +00001291 virtual VAArgInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001292
1293 // Methods for support type inquiry through isa, cast, and dyn_cast:
1294 static inline bool classof(const VAArgInst *) { return true; }
1295 static inline bool classof(const Instruction *I) {
1296 return I->getOpcode() == VAArg;
1297 }
1298 static inline bool classof(const Value *V) {
1299 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1300 }
1301};
1302
1303//===----------------------------------------------------------------------===//
Robert Bocchino49b78a52006-01-10 19:04:13 +00001304// ExtractElementInst Class
1305//===----------------------------------------------------------------------===//
1306
1307/// ExtractElementInst - This instruction extracts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001308/// element from a VectorType value
Robert Bocchino49b78a52006-01-10 19:04:13 +00001309///
1310class ExtractElementInst : public Instruction {
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001311 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
Chris Lattner9fc18d22006-04-08 01:15:18 +00001312 Instruction *InsertBefore = 0);
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001313 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
Chris Lattner9fc18d22006-04-08 01:15:18 +00001314 BasicBlock *InsertAtEnd);
Eric Christophera3500da2009-07-25 02:28:41 +00001315public:
Eric Christophera3500da2009-07-25 02:28:41 +00001316 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001317 const Twine &NameStr = "",
Eric Christophera3500da2009-07-25 02:28:41 +00001318 Instruction *InsertBefore = 0) {
1319 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1320 }
1321 static ExtractElementInst *Create(Value *Vec, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001322 const Twine &NameStr,
Eric Christophera3500da2009-07-25 02:28:41 +00001323 BasicBlock *InsertAtEnd) {
1324 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1325 }
Robert Bocchino49b78a52006-01-10 19:04:13 +00001326
Chris Lattnerfa495842006-04-08 04:04:54 +00001327 /// isValidOperands - Return true if an extractelement instruction can be
1328 /// formed with the specified operands.
1329 static bool isValidOperands(const Value *Vec, const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001330
Nick Lewycky67760642009-09-27 07:38:41 +00001331 virtual ExtractElementInst *clone() const;
Robert Bocchino49b78a52006-01-10 19:04:13 +00001332
Chris Lattner97159122009-09-08 03:32:53 +00001333 Value *getVectorOperand() { return Op<0>(); }
1334 Value *getIndexOperand() { return Op<1>(); }
1335 const Value *getVectorOperand() const { return Op<0>(); }
1336 const Value *getIndexOperand() const { return Op<1>(); }
1337
1338 const VectorType *getVectorOperandType() const {
Chris Lattnerc8dee3f2009-09-08 03:39:55 +00001339 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
Chris Lattner97159122009-09-08 03:32:53 +00001340 }
1341
1342
Robert Bocchino49b78a52006-01-10 19:04:13 +00001343 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001344 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchino49b78a52006-01-10 19:04:13 +00001345
1346 // Methods for support type inquiry through isa, cast, and dyn_cast:
1347 static inline bool classof(const ExtractElementInst *) { return true; }
1348 static inline bool classof(const Instruction *I) {
1349 return I->getOpcode() == Instruction::ExtractElement;
1350 }
1351 static inline bool classof(const Value *V) {
1352 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1353 }
1354};
1355
Gabor Greifefe65362008-05-10 08:32:32 +00001356template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001357struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00001358};
1359
1360DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1361
Robert Bocchino49b78a52006-01-10 19:04:13 +00001362//===----------------------------------------------------------------------===//
Robert Bocchinof9993442006-01-17 20:05:59 +00001363// InsertElementInst Class
1364//===----------------------------------------------------------------------===//
1365
1366/// InsertElementInst - This instruction inserts a single (scalar)
Reid Spencer9d6565a2007-02-15 02:26:10 +00001367/// element into a VectorType value
Robert Bocchinof9993442006-01-17 20:05:59 +00001368///
1369class InsertElementInst : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001370 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001371 const Twine &NameStr = "",
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001372 Instruction *InsertBefore = 0);
Gabor Greif051a9502008-04-06 20:25:17 +00001373 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001374 const Twine &NameStr, BasicBlock *InsertAtEnd);
Robert Bocchinof9993442006-01-17 20:05:59 +00001375public:
Gabor Greif051a9502008-04-06 20:25:17 +00001376 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001377 const Twine &NameStr = "",
Gabor Greifefe65362008-05-10 08:32:32 +00001378 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001379 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001380 }
Gabor Greif051a9502008-04-06 20:25:17 +00001381 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001382 const Twine &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001383 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001384 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001385 }
Robert Bocchinof9993442006-01-17 20:05:59 +00001386
Chris Lattnerfa495842006-04-08 04:04:54 +00001387 /// isValidOperands - Return true if an insertelement instruction can be
1388 /// formed with the specified operands.
1389 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1390 const Value *Idx);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001391
Nick Lewycky67760642009-09-27 07:38:41 +00001392 virtual InsertElementInst *clone() const;
Robert Bocchinof9993442006-01-17 20:05:59 +00001393
Reid Spencerac9dcb92007-02-15 03:39:18 +00001394 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001395 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001396 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001397 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001398 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001399
Robert Bocchinof9993442006-01-17 20:05:59 +00001400 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001401 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Robert Bocchinof9993442006-01-17 20:05:59 +00001402
1403 // Methods for support type inquiry through isa, cast, and dyn_cast:
1404 static inline bool classof(const InsertElementInst *) { return true; }
1405 static inline bool classof(const Instruction *I) {
1406 return I->getOpcode() == Instruction::InsertElement;
1407 }
1408 static inline bool classof(const Value *V) {
1409 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1410 }
1411};
1412
Gabor Greifefe65362008-05-10 08:32:32 +00001413template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001414struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001415};
1416
1417DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1418
Robert Bocchinof9993442006-01-17 20:05:59 +00001419//===----------------------------------------------------------------------===//
Chris Lattner9fc18d22006-04-08 01:15:18 +00001420// ShuffleVectorInst Class
1421//===----------------------------------------------------------------------===//
1422
1423/// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1424/// input vectors.
1425///
1426class ShuffleVectorInst : public Instruction {
Chris Lattner9fc18d22006-04-08 01:15:18 +00001427public:
Gabor Greif051a9502008-04-06 20:25:17 +00001428 // allocate space for exactly three operands
1429 void *operator new(size_t s) {
1430 return User::operator new(s, 3);
1431 }
Chris Lattner9fc18d22006-04-08 01:15:18 +00001432 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001433 const Twine &NameStr = "",
Evan Cheng1bf9a182008-07-24 00:08:56 +00001434 Instruction *InsertBefor = 0);
Chris Lattner9fc18d22006-04-08 01:15:18 +00001435 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001436 const Twine &NameStr, BasicBlock *InsertAtEnd);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001437
Chris Lattnerfa495842006-04-08 04:04:54 +00001438 /// isValidOperands - Return true if a shufflevector instruction can be
Chris Lattner9fc18d22006-04-08 01:15:18 +00001439 /// formed with the specified operands.
1440 static bool isValidOperands(const Value *V1, const Value *V2,
1441 const Value *Mask);
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001442
Nick Lewycky67760642009-09-27 07:38:41 +00001443 virtual ShuffleVectorInst *clone() const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001444
Reid Spencerac9dcb92007-02-15 03:39:18 +00001445 /// getType - Overload to return most specific vector type.
Chris Lattner6a56ed42006-04-14 22:20:07 +00001446 ///
Devang Patel4d4a5e02008-02-23 01:11:02 +00001447 const VectorType *getType() const {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001448 return reinterpret_cast<const VectorType*>(Instruction::getType());
Chris Lattner6a56ed42006-04-14 22:20:07 +00001449 }
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001450
Chris Lattner9fc18d22006-04-08 01:15:18 +00001451 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00001452 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001453
Chris Lattner8728f192008-03-02 05:28:33 +00001454 /// getMaskValue - Return the index from the shuffle mask for the specified
1455 /// output result. This is either -1 if the element is undef or a number less
1456 /// than 2*numelements.
1457 int getMaskValue(unsigned i) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001458
Chris Lattner9fc18d22006-04-08 01:15:18 +00001459 // Methods for support type inquiry through isa, cast, and dyn_cast:
1460 static inline bool classof(const ShuffleVectorInst *) { return true; }
1461 static inline bool classof(const Instruction *I) {
1462 return I->getOpcode() == Instruction::ShuffleVector;
1463 }
1464 static inline bool classof(const Value *V) {
1465 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1466 }
1467};
1468
Gabor Greifefe65362008-05-10 08:32:32 +00001469template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001470struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
Gabor Greifefe65362008-05-10 08:32:32 +00001471};
1472
1473DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
Chris Lattner9fc18d22006-04-08 01:15:18 +00001474
1475//===----------------------------------------------------------------------===//
Dan Gohman041e2eb2008-05-15 19:50:34 +00001476// ExtractValueInst Class
1477//===----------------------------------------------------------------------===//
1478
Dan Gohmane2d896f2008-05-15 23:35:32 +00001479/// ExtractValueInst - This instruction extracts a struct member or array
1480/// element value from an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001481///
Gabor Greifd4f268b2008-06-06 20:28:12 +00001482class ExtractValueInst : public UnaryInstruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001483 SmallVector<unsigned, 4> Indices;
1484
Dan Gohman041e2eb2008-05-15 19:50:34 +00001485 ExtractValueInst(const ExtractValueInst &EVI);
Gabor Greif76aca6f2008-06-06 21:06:32 +00001486 void init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001487 const Twine &NameStr);
1488 void init(unsigned Idx, const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001489
1490 template<typename InputIterator>
Gabor Greif76aca6f2008-06-06 21:06:32 +00001491 void init(InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001492 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001493 // This argument ensures that we have an iterator we can
1494 // do arithmetic on in constant time
1495 std::random_access_iterator_tag) {
1496 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001497
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001498 // There's no fundamental reason why we require at least one index
1499 // (other than weirdness with &*IdxBegin being invalid; see
1500 // getelementptr's init routine for example). But there's no
1501 // present need to support it.
1502 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1503
1504 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +00001505 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001506 // we have to build an array here
Dan Gohman041e2eb2008-05-15 19:50:34 +00001507 }
1508
1509 /// getIndexedType - Returns the type of the element that would be extracted
1510 /// with an extractvalue instruction with the specified parameters.
1511 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +00001512 /// Null is returned if the indices are invalid for the specified
Dan Gohman041e2eb2008-05-15 19:50:34 +00001513 /// pointer type.
1514 ///
1515 static const Type *getIndexedType(const Type *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001516 const unsigned *Idx, unsigned NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001517
1518 template<typename InputIterator>
1519 static const Type *getIndexedType(const Type *Ptr,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001520 InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001521 InputIterator IdxEnd,
1522 // This argument ensures that we
1523 // have an iterator we can do
1524 // arithmetic on in constant time
1525 std::random_access_iterator_tag) {
1526 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1527
1528 if (NumIdx > 0)
1529 // This requires that the iterator points to contiguous memory.
Dan Gohman19a81632008-06-23 16:38:10 +00001530 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001531 else
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001532 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001533 }
1534
Dan Gohmane2d896f2008-05-15 23:35:32 +00001535 /// Constructors - Create a extractvalue instruction with a base aggregate
1536 /// value and a list of indices. The first ctor can optionally insert before
1537 /// an existing instruction, the second appends the new instruction to the
1538 /// specified BasicBlock.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001539 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001540 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001541 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001542 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001543 Instruction *InsertBefore);
1544 template<typename InputIterator>
1545 inline ExtractValueInst(Value *Agg,
1546 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001547 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001548
Dan Gohman8e640412008-05-31 19:09:47 +00001549 // allocate space for exactly one operand
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001550 void *operator new(size_t s) {
1551 return User::operator new(s, 1);
1552 }
1553
Gabor Greifd4f268b2008-06-06 20:28:12 +00001554public:
Dan Gohman041e2eb2008-05-15 19:50:34 +00001555 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001556 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001557 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001558 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001559 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001560 return new
Evan Cheng1bf9a182008-07-24 00:08:56 +00001561 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001562 }
1563 template<typename InputIterator>
1564 static ExtractValueInst *Create(Value *Agg,
1565 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001566 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001567 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001568 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001569 }
1570
1571 /// Constructors - These two creators are convenience methods because one
1572 /// index extractvalue instructions are much more common than those with
1573 /// more than one.
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001574 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001575 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001576 Instruction *InsertBefore = 0) {
Dan Gohman2f27e172008-06-23 16:48:17 +00001577 unsigned Idxs[1] = { Idx };
Evan Cheng1bf9a182008-07-24 00:08:56 +00001578 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001579 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001580 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001581 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001582 BasicBlock *InsertAtEnd) {
Dan Gohman2f27e172008-06-23 16:48:17 +00001583 unsigned Idxs[1] = { Idx };
Evan Cheng1bf9a182008-07-24 00:08:56 +00001584 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001585 }
1586
Nick Lewycky67760642009-09-27 07:38:41 +00001587 virtual ExtractValueInst *clone() const;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001588
Dan Gohman041e2eb2008-05-15 19:50:34 +00001589 /// getIndexedType - Returns the type of the element that would be extracted
1590 /// with an extractvalue instruction with the specified parameters.
1591 ///
Dan Gohmane2d896f2008-05-15 23:35:32 +00001592 /// Null is returned if the indices are invalid for the specified
Dan Gohman041e2eb2008-05-15 19:50:34 +00001593 /// pointer type.
1594 ///
1595 template<typename InputIterator>
1596 static const Type *getIndexedType(const Type *Ptr,
1597 InputIterator IdxBegin,
1598 InputIterator IdxEnd) {
1599 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1600 typename std::iterator_traits<InputIterator>::
1601 iterator_category());
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001602 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001603 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001604
Owen Anderson5678d6e2008-06-19 17:15:57 +00001605 typedef const unsigned* idx_iterator;
1606 inline idx_iterator idx_begin() const { return Indices.begin(); }
1607 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001608
1609 Value *getAggregateOperand() {
1610 return getOperand(0);
1611 }
1612 const Value *getAggregateOperand() const {
1613 return getOperand(0);
1614 }
1615 static unsigned getAggregateOperandIndex() {
1616 return 0U; // get index for modifying correct operand
1617 }
1618
1619 unsigned getNumIndices() const { // Note: always non-negative
Bill Wendling67944fc2008-06-05 07:35:27 +00001620 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001621 }
1622
1623 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001624 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001625 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001626
Dan Gohman041e2eb2008-05-15 19:50:34 +00001627 // Methods for support type inquiry through isa, cast, and dyn_cast:
1628 static inline bool classof(const ExtractValueInst *) { return true; }
1629 static inline bool classof(const Instruction *I) {
1630 return I->getOpcode() == Instruction::ExtractValue;
1631 }
1632 static inline bool classof(const Value *V) {
1633 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1634 }
1635};
1636
Dan Gohmane4569942008-05-23 00:36:11 +00001637template<typename InputIterator>
1638ExtractValueInst::ExtractValueInst(Value *Agg,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001639 InputIterator IdxBegin,
Dan Gohmane4569942008-05-23 00:36:11 +00001640 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001641 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001642 Instruction *InsertBefore)
Gabor Greifd4f268b2008-06-06 20:28:12 +00001643 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
Bill Wendling85f40542008-07-22 07:14:12 +00001644 IdxBegin, IdxEnd)),
1645 ExtractValue, Agg, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001646 init(IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001647 typename std::iterator_traits<InputIterator>::iterator_category());
1648}
1649template<typename InputIterator>
1650ExtractValueInst::ExtractValueInst(Value *Agg,
1651 InputIterator IdxBegin,
1652 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001653 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001654 BasicBlock *InsertAtEnd)
Gabor Greifd4f268b2008-06-06 20:28:12 +00001655 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
Bill Wendling85f40542008-07-22 07:14:12 +00001656 IdxBegin, IdxEnd)),
1657 ExtractValue, Agg, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001658 init(IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001659 typename std::iterator_traits<InputIterator>::iterator_category());
1660}
1661
Dan Gohmane4569942008-05-23 00:36:11 +00001662
Dan Gohman041e2eb2008-05-15 19:50:34 +00001663//===----------------------------------------------------------------------===//
1664// InsertValueInst Class
1665//===----------------------------------------------------------------------===//
1666
Dan Gohmane2d896f2008-05-15 23:35:32 +00001667/// InsertValueInst - This instruction inserts a struct field of array element
1668/// value into an aggregate value.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001669///
1670class InsertValueInst : public Instruction {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001671 SmallVector<unsigned, 4> Indices;
1672
1673 void *operator new(size_t, unsigned); // Do not implement
Dan Gohman041e2eb2008-05-15 19:50:34 +00001674 InsertValueInst(const InsertValueInst &IVI);
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +00001675 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001676 const Twine &NameStr);
1677 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001678
1679 template<typename InputIterator>
1680 void init(Value *Agg, Value *Val,
1681 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001682 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001683 // This argument ensures that we have an iterator we can
1684 // do arithmetic on in constant time
1685 std::random_access_iterator_tag) {
1686 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001687
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001688 // There's no fundamental reason why we require at least one index
1689 // (other than weirdness with &*IdxBegin being invalid; see
1690 // getelementptr's init routine for example). But there's no
1691 // present need to support it.
1692 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1693
1694 // This requires that the iterator points to contiguous memory.
Evan Cheng1bf9a182008-07-24 00:08:56 +00001695 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
Matthijs Kooijman444099f62008-06-04 14:40:55 +00001696 // we have to build an array here
Dan Gohman041e2eb2008-05-15 19:50:34 +00001697 }
1698
Dan Gohmane2d896f2008-05-15 23:35:32 +00001699 /// Constructors - Create a insertvalue instruction with a base aggregate
1700 /// value, a value to insert, and a list of indices. The first ctor can
1701 /// optionally insert before an existing instruction, the second appends
1702 /// the new instruction to the specified BasicBlock.
Dan Gohman041e2eb2008-05-15 19:50:34 +00001703 template<typename InputIterator>
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001704 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001705 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001706 const Twine &NameStr,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001707 Instruction *InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001708 template<typename InputIterator>
1709 inline InsertValueInst(Value *Agg, Value *Val,
Dan Gohmane2d896f2008-05-15 23:35:32 +00001710 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001711 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001712
1713 /// Constructors - These two constructors are convenience methods because one
1714 /// and two index insertvalue instructions are so common.
1715 InsertValueInst(Value *Agg, Value *Val,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001716 unsigned Idx, const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001717 Instruction *InsertBefore = 0);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001718 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001719 const Twine &NameStr, BasicBlock *InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001720public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001721 // allocate space for exactly two operands
1722 void *operator new(size_t s) {
1723 return User::operator new(s, 2);
1724 }
1725
Dan Gohman041e2eb2008-05-15 19:50:34 +00001726 template<typename InputIterator>
Matthijs Kooijmancfd5b7d2008-06-05 07:26:15 +00001727 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001728 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001729 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001730 Instruction *InsertBefore = 0) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001731 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001732 NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001733 }
1734 template<typename InputIterator>
1735 static InsertValueInst *Create(Value *Agg, Value *Val,
1736 InputIterator IdxBegin, InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001737 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001738 BasicBlock *InsertAtEnd) {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001739 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00001740 NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001741 }
1742
1743 /// Constructors - These two creators are convenience methods because one
1744 /// index insertvalue instructions are much more common than those with
1745 /// more than one.
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001746 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001747 const Twine &NameStr = "",
Dan Gohman041e2eb2008-05-15 19:50:34 +00001748 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001749 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001750 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001751 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001752 const Twine &NameStr,
Dan Gohman041e2eb2008-05-15 19:50:34 +00001753 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001754 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
Dan Gohman041e2eb2008-05-15 19:50:34 +00001755 }
1756
Nick Lewycky67760642009-09-27 07:38:41 +00001757 virtual InsertValueInst *clone() const;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001758
1759 /// Transparently provide more efficient getOperand methods.
1760 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1761
Owen Anderson5678d6e2008-06-19 17:15:57 +00001762 typedef const unsigned* idx_iterator;
1763 inline idx_iterator idx_begin() const { return Indices.begin(); }
1764 inline idx_iterator idx_end() const { return Indices.end(); }
Dan Gohman041e2eb2008-05-15 19:50:34 +00001765
1766 Value *getAggregateOperand() {
1767 return getOperand(0);
1768 }
1769 const Value *getAggregateOperand() const {
1770 return getOperand(0);
1771 }
1772 static unsigned getAggregateOperandIndex() {
1773 return 0U; // get index for modifying correct operand
1774 }
1775
1776 Value *getInsertedValueOperand() {
1777 return getOperand(1);
1778 }
1779 const Value *getInsertedValueOperand() const {
1780 return getOperand(1);
1781 }
1782 static unsigned getInsertedValueOperandIndex() {
1783 return 1U; // get index for modifying correct operand
1784 }
1785
1786 unsigned getNumIndices() const { // Note: always non-negative
Bill Wendling67944fc2008-06-05 07:35:27 +00001787 return (unsigned)Indices.size();
Dan Gohman041e2eb2008-05-15 19:50:34 +00001788 }
1789
1790 bool hasIndices() const {
Dan Gohman35651cd2008-05-31 19:09:08 +00001791 return true;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001792 }
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001793
Dan Gohman041e2eb2008-05-15 19:50:34 +00001794 // Methods for support type inquiry through isa, cast, and dyn_cast:
1795 static inline bool classof(const InsertValueInst *) { return true; }
1796 static inline bool classof(const Instruction *I) {
1797 return I->getOpcode() == Instruction::InsertValue;
1798 }
1799 static inline bool classof(const Value *V) {
1800 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1801 }
1802};
1803
1804template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00001805struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001806};
1807
Dan Gohmane4569942008-05-23 00:36:11 +00001808template<typename InputIterator>
1809InsertValueInst::InsertValueInst(Value *Agg,
1810 Value *Val,
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00001811 InputIterator IdxBegin,
Dan Gohmane4569942008-05-23 00:36:11 +00001812 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001813 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001814 Instruction *InsertBefore)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001815 : Instruction(Agg->getType(), InsertValue,
1816 OperandTraits<InsertValueInst>::op_begin(this),
1817 2, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001818 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001819 typename std::iterator_traits<InputIterator>::iterator_category());
1820}
1821template<typename InputIterator>
1822InsertValueInst::InsertValueInst(Value *Agg,
1823 Value *Val,
1824 InputIterator IdxBegin,
1825 InputIterator IdxEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001826 const Twine &NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001827 BasicBlock *InsertAtEnd)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001828 : Instruction(Agg->getType(), InsertValue,
1829 OperandTraits<InsertValueInst>::op_begin(this),
1830 2, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001831 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
Dan Gohmane4569942008-05-23 00:36:11 +00001832 typename std::iterator_traits<InputIterator>::iterator_category());
1833}
1834
Dan Gohman041e2eb2008-05-15 19:50:34 +00001835DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1836
1837//===----------------------------------------------------------------------===//
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001838// PHINode Class
1839//===----------------------------------------------------------------------===//
1840
1841// PHINode - The PHINode class is used to represent the magical mystical PHI
1842// node, that can not exist in nature, but can be synthesized in a computer
1843// scientist's overactive imagination.
1844//
1845class PHINode : public Instruction {
Gabor Greif051a9502008-04-06 20:25:17 +00001846 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00001847 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1848 /// the number actually in use.
1849 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001850 PHINode(const PHINode &PN);
Gabor Greif051a9502008-04-06 20:25:17 +00001851 // allocate space for exactly zero operands
1852 void *operator new(size_t s) {
1853 return User::operator new(s, 0);
1854 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001855 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001856 Instruction *InsertBefore = 0)
Chris Lattner910c80a2007-02-24 00:55:48 +00001857 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
Chris Lattner454928e2005-01-29 00:31:36 +00001858 ReservedSpace(0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001859 setName(NameStr);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001860 }
1861
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001862 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
Chris Lattner910c80a2007-02-24 00:55:48 +00001863 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
Chris Lattner454928e2005-01-29 00:31:36 +00001864 ReservedSpace(0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001865 setName(NameStr);
Chris Lattner454928e2005-01-29 00:31:36 +00001866 }
Gabor Greif051a9502008-04-06 20:25:17 +00001867public:
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001868 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
Gabor Greif051a9502008-04-06 20:25:17 +00001869 Instruction *InsertBefore = 0) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001870 return new PHINode(Ty, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00001871 }
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00001872 static PHINode *Create(const Type *Ty, const Twine &NameStr,
Evan Chengd69bb1a2008-05-05 17:41:03 +00001873 BasicBlock *InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00001874 return new PHINode(Ty, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00001875 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00001876 ~PHINode();
1877
Chris Lattner454928e2005-01-29 00:31:36 +00001878 /// reserveOperandSpace - This method can be used to avoid repeated
1879 /// reallocation of PHI operand lists by reserving space for the correct
1880 /// number of operands before adding them. Unlike normal vector reserves,
1881 /// this method can also be used to trim the operand space.
1882 void reserveOperandSpace(unsigned NumValues) {
1883 resizeOperands(NumValues*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001884 }
1885
Nick Lewycky67760642009-09-27 07:38:41 +00001886 virtual PHINode *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001887
Gabor Greifefe65362008-05-10 08:32:32 +00001888 /// Provide fast operand accessors
1889 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1890
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001891 /// getNumIncomingValues - Return the number of incoming edges
1892 ///
Chris Lattner454928e2005-01-29 00:31:36 +00001893 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001894
Reid Spencerc773de62006-05-19 19:07:54 +00001895 /// getIncomingValue - Return incoming value number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001896 ///
1897 Value *getIncomingValue(unsigned i) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001898 assert(i*2 < getNumOperands() && "Invalid value number!");
1899 return getOperand(i*2);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001900 }
1901 void setIncomingValue(unsigned i, Value *V) {
Chris Lattner454928e2005-01-29 00:31:36 +00001902 assert(i*2 < getNumOperands() && "Invalid value number!");
1903 setOperand(i*2, V);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001904 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001905 static unsigned getOperandNumForIncomingValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001906 return i*2;
1907 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001908 static unsigned getIncomingValueNumForOperand(unsigned i) {
1909 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1910 return i/2;
1911 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001912
Gabor Greifa36791d2009-01-23 19:40:15 +00001913 /// getIncomingBlock - Return incoming basic block corresponding
1914 /// to value use iterator
1915 ///
1916 template <typename U>
1917 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1918 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1919 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1920 }
Reid Spencerc773de62006-05-19 19:07:54 +00001921 /// getIncomingBlock - Return incoming basic block number x
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001922 ///
Misha Brukman9769ab22005-04-21 20:19:05 +00001923 BasicBlock *getIncomingBlock(unsigned i) const {
Gabor Greifefe65362008-05-10 08:32:32 +00001924 return static_cast<BasicBlock*>(getOperand(i*2+1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001925 }
1926 void setIncomingBlock(unsigned i, BasicBlock *BB) {
Gabor Greifefe65362008-05-10 08:32:32 +00001927 setOperand(i*2+1, BB);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001928 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001929 static unsigned getOperandNumForIncomingBlock(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001930 return i*2+1;
1931 }
Dan Gohmanb45088c2009-03-23 15:48:29 +00001932 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1933 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1934 return i/2;
1935 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001936
1937 /// addIncoming - Add an incoming value to the end of the PHI list
1938 ///
1939 void addIncoming(Value *V, BasicBlock *BB) {
Anton Korobeynikov351b0d42008-02-27 22:37:28 +00001940 assert(V && "PHI node got a null value!");
1941 assert(BB && "PHI node got a null basic block!");
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001942 assert(getType() == V->getType() &&
1943 "All operands to PHI node must be the same type as the PHI node!");
Chris Lattner454928e2005-01-29 00:31:36 +00001944 unsigned OpNo = NumOperands;
1945 if (OpNo+2 > ReservedSpace)
1946 resizeOperands(0); // Get more space!
1947 // Initialize some new operands.
1948 NumOperands = OpNo+2;
Gabor Greif6c80c382008-05-26 21:33:52 +00001949 OperandList[OpNo] = V;
1950 OperandList[OpNo+1] = BB;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001951 }
Misha Brukman9769ab22005-04-21 20:19:05 +00001952
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001953 /// removeIncomingValue - Remove an incoming value. This is useful if a
1954 /// predecessor basic block is deleted. The value removed is returned.
1955 ///
1956 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1957 /// is true), the PHI node is destroyed and any uses of it are replaced with
1958 /// dummy values. The only time there should be zero incoming values to a PHI
1959 /// node is when the block is dead, so this strategy is sound.
1960 ///
1961 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1962
Gabor Greifefe65362008-05-10 08:32:32 +00001963 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001964 int Idx = getBasicBlockIndex(BB);
1965 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1966 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1967 }
1968
Misha Brukman9769ab22005-04-21 20:19:05 +00001969 /// getBasicBlockIndex - Return the first index of the specified basic
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001970 /// block in the value list for this PHI. Returns -1 if no instance.
1971 ///
1972 int getBasicBlockIndex(const BasicBlock *BB) const {
Chris Lattner454928e2005-01-29 00:31:36 +00001973 Use *OL = OperandList;
Misha Brukman9769ab22005-04-21 20:19:05 +00001974 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
Gabor Greifefe65362008-05-10 08:32:32 +00001975 if (OL[i+1].get() == BB) return i/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001976 return -1;
1977 }
1978
1979 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1980 return getIncomingValue(getBasicBlockIndex(BB));
1981 }
1982
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001983 /// hasConstantValue - If the specified PHI node always merges together the
Nate Begemana83ba0f2005-08-04 23:24:19 +00001984 /// same value, return the value, otherwise return null.
1985 ///
Dan Gohmanbccfc242009-09-03 15:34:35 +00001986 /// If the PHI has undef operands, but all the rest of the operands are
1987 /// some unique value, return that value if it can be proved that the
1988 /// value dominates the PHI. If DT is null, use a conservative check,
1989 /// otherwise use DT to test for dominance.
1990 ///
1991 Value *hasConstantValue(DominatorTree *DT = 0) const;
Chris Lattnerf56a8db2006-10-03 17:09:12 +00001992
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001993 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1994 static inline bool classof(const PHINode *) { return true; }
1995 static inline bool classof(const Instruction *I) {
Misha Brukman9769ab22005-04-21 20:19:05 +00001996 return I->getOpcode() == Instruction::PHI;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00001997 }
1998 static inline bool classof(const Value *V) {
1999 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2000 }
Chris Lattner454928e2005-01-29 00:31:36 +00002001 private:
2002 void resizeOperands(unsigned NumOperands);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002003};
2004
Gabor Greifefe65362008-05-10 08:32:32 +00002005template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002006struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00002007};
2008
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002009DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002010
2011
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002012//===----------------------------------------------------------------------===//
2013// ReturnInst Class
2014//===----------------------------------------------------------------------===//
2015
2016//===---------------------------------------------------------------------------
2017/// ReturnInst - Return a value (possibly void), from a function. Execution
2018/// does not continue in this function any longer.
2019///
2020class ReturnInst : public TerminatorInst {
Chris Lattner910c80a2007-02-24 00:55:48 +00002021 ReturnInst(const ReturnInst &RI);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002022
Gabor Greif051a9502008-04-06 20:25:17 +00002023private:
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002024 // ReturnInst constructors:
2025 // ReturnInst() - 'ret void' instruction
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002026 // ReturnInst( null) - 'ret void' instruction
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002027 // ReturnInst(Value* X) - 'ret X' instruction
Gabor Greifefe65362008-05-10 08:32:32 +00002028 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002029 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
Gabor Greifefe65362008-05-10 08:32:32 +00002030 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2031 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
Alkis Evlogimenos859804f2004-11-17 21:02:25 +00002032 //
2033 // NOTE: If the Value* passed is of type void then the constructor behaves as
2034 // if it was passed NULL.
Owen Anderson1d0be152009-08-13 21:58:54 +00002035 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2036 Instruction *InsertBefore = 0);
2037 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2038 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002039public:
Owen Anderson1d0be152009-08-13 21:58:54 +00002040 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2041 Instruction *InsertBefore = 0) {
2042 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002043 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002044 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2045 BasicBlock *InsertAtEnd) {
2046 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002047 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002048 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2049 return new(0) ReturnInst(C, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002050 }
Devang Patel57ef4f42008-02-23 00:35:18 +00002051 virtual ~ReturnInst();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002052
Nick Lewycky67760642009-09-27 07:38:41 +00002053 virtual ReturnInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002054
Gabor Greifefe65362008-05-10 08:32:32 +00002055 /// Provide fast operand accessors
2056 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Devang Patel64d4e612008-02-26 17:56:20 +00002057
Gabor Greifefe65362008-05-10 08:32:32 +00002058 /// Convenience accessor
Devang Patel1eafa062008-03-11 17:35:03 +00002059 Value *getReturnValue(unsigned n = 0) const {
Gabor Greifefe65362008-05-10 08:32:32 +00002060 return n < getNumOperands()
2061 ? getOperand(n)
2062 : 0;
Devang Patel1eafa062008-03-11 17:35:03 +00002063 }
2064
Chris Lattner454928e2005-01-29 00:31:36 +00002065 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002066
2067 // Methods for support type inquiry through isa, cast, and dyn_cast:
2068 static inline bool classof(const ReturnInst *) { return true; }
2069 static inline bool classof(const Instruction *I) {
2070 return (I->getOpcode() == Instruction::Ret);
2071 }
2072 static inline bool classof(const Value *V) {
2073 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2074 }
Chris Lattner454928e2005-01-29 00:31:36 +00002075 private:
2076 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2077 virtual unsigned getNumSuccessorsV() const;
2078 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002079};
2080
Gabor Greifefe65362008-05-10 08:32:32 +00002081template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002082struct OperandTraits<ReturnInst> : public OptionalOperandTraits<> {
Gabor Greifefe65362008-05-10 08:32:32 +00002083};
2084
2085DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002086
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002087//===----------------------------------------------------------------------===//
2088// BranchInst Class
2089//===----------------------------------------------------------------------===//
2090
2091//===---------------------------------------------------------------------------
2092/// BranchInst - Conditional or Unconditional Branch instruction.
2093///
2094class BranchInst : public TerminatorInst {
Chris Lattner454928e2005-01-29 00:31:36 +00002095 /// Ops list - Branches are strange. The operands are ordered:
Gabor Greifae5a20a2009-03-12 18:34:49 +00002096 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2097 /// they don't have to check for cond/uncond branchness. These are mostly
2098 /// accessed relative from op_end().
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002099 BranchInst(const BranchInst &BI);
Chris Lattner454928e2005-01-29 00:31:36 +00002100 void AssertOK();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002101 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2102 // BranchInst(BB *B) - 'br B'
2103 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2104 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2105 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2106 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2107 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
Chris Lattner910c80a2007-02-24 00:55:48 +00002108 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002109 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002110 Instruction *InsertBefore = 0);
2111 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002112 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
Chris Lattner910c80a2007-02-24 00:55:48 +00002113 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002114public:
2115 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
Gabor Greifae5a20a2009-03-12 18:34:49 +00002116 return new(1, true) BranchInst(IfTrue, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002117 }
Gabor Greifefe65362008-05-10 08:32:32 +00002118 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2119 Value *Cond, Instruction *InsertBefore = 0) {
Gabor Greif051a9502008-04-06 20:25:17 +00002120 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2121 }
2122 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
Gabor Greifae5a20a2009-03-12 18:34:49 +00002123 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002124 }
Gabor Greifefe65362008-05-10 08:32:32 +00002125 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2126 Value *Cond, BasicBlock *InsertAtEnd) {
Gabor Greif051a9502008-04-06 20:25:17 +00002127 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2128 }
Chris Lattner454928e2005-01-29 00:31:36 +00002129
Gabor Greifae5a20a2009-03-12 18:34:49 +00002130 ~BranchInst();
Gabor Greifefe65362008-05-10 08:32:32 +00002131
Chris Lattner454928e2005-01-29 00:31:36 +00002132 /// Transparently provide more efficient getOperand methods.
Gabor Greifefe65362008-05-10 08:32:32 +00002133 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002134
Nick Lewycky67760642009-09-27 07:38:41 +00002135 virtual BranchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002136
Devang Patel4d4a5e02008-02-23 01:11:02 +00002137 bool isUnconditional() const { return getNumOperands() == 1; }
2138 bool isConditional() const { return getNumOperands() == 3; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002139
Devang Patel4d4a5e02008-02-23 01:11:02 +00002140 Value *getCondition() const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002141 assert(isConditional() && "Cannot get condition of an uncond branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002142 return Op<-3>();
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002143 }
2144
2145 void setCondition(Value *V) {
2146 assert(isConditional() && "Cannot set condition of unconditional branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002147 Op<-3>() = V;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002148 }
2149
2150 // setUnconditionalDest - Change the current branch to an unconditional branch
2151 // targeting the specified block.
Chris Lattner454928e2005-01-29 00:31:36 +00002152 // FIXME: Eliminate this ugly method.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002153 void setUnconditionalDest(BasicBlock *Dest) {
Gabor Greifae5a20a2009-03-12 18:34:49 +00002154 Op<-1>() = Dest;
Chris Lattner454928e2005-01-29 00:31:36 +00002155 if (isConditional()) { // Convert this to an uncond branch.
Gabor Greifae5a20a2009-03-12 18:34:49 +00002156 Op<-2>() = 0;
2157 Op<-3>() = 0;
Chris Lattner454928e2005-01-29 00:31:36 +00002158 NumOperands = 1;
Gabor Greifae5a20a2009-03-12 18:34:49 +00002159 OperandList = op_begin();
Chris Lattner454928e2005-01-29 00:31:36 +00002160 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002161 }
2162
Chris Lattner454928e2005-01-29 00:31:36 +00002163 unsigned getNumSuccessors() const { return 1+isConditional(); }
2164
2165 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002166 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002167 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002168 }
2169
Chris Lattner454928e2005-01-29 00:31:36 +00002170 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002171 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
Gabor Greifae5a20a2009-03-12 18:34:49 +00002172 *(&Op<-1>() - idx) = NewSucc;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002173 }
2174
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002175 // Methods for support type inquiry through isa, cast, and dyn_cast:
2176 static inline bool classof(const BranchInst *) { return true; }
2177 static inline bool classof(const Instruction *I) {
2178 return (I->getOpcode() == Instruction::Br);
2179 }
2180 static inline bool classof(const Value *V) {
2181 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2182 }
Chris Lattner454928e2005-01-29 00:31:36 +00002183private:
2184 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2185 virtual unsigned getNumSuccessorsV() const;
2186 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002187};
2188
Gabor Greifefe65362008-05-10 08:32:32 +00002189template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002190struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
Gabor Greifefe65362008-05-10 08:32:32 +00002191
2192DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2193
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002194//===----------------------------------------------------------------------===//
2195// SwitchInst Class
2196//===----------------------------------------------------------------------===//
2197
2198//===---------------------------------------------------------------------------
2199/// SwitchInst - Multiway switch
2200///
2201class SwitchInst : public TerminatorInst {
Gabor Greifefe65362008-05-10 08:32:32 +00002202 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner454928e2005-01-29 00:31:36 +00002203 unsigned ReservedSpace;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002204 // Operand[0] = Value to switch on
2205 // Operand[1] = Default basic block destination
2206 // Operand[2n ] = Value to match
2207 // Operand[2n+1] = BasicBlock to go to on match
2208 SwitchInst(const SwitchInst &RI);
Chris Lattner454928e2005-01-29 00:31:36 +00002209 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2210 void resizeOperands(unsigned No);
Gabor Greifefe65362008-05-10 08:32:32 +00002211 // allocate space for exactly zero operands
2212 void *operator new(size_t s) {
2213 return User::operator new(s, 0);
2214 }
Chris Lattner454928e2005-01-29 00:31:36 +00002215 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2216 /// switch on and a default destination. The number of additional cases can
2217 /// be specified here to make memory allocation more efficient. This
2218 /// constructor can also autoinsert before another instruction.
2219 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002220 Instruction *InsertBefore = 0);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002221
Chris Lattner454928e2005-01-29 00:31:36 +00002222 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2223 /// switch on and a default destination. The number of additional cases can
2224 /// be specified here to make memory allocation more efficient. This
2225 /// constructor also autoinserts at the end of the specified BasicBlock.
2226 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
Chris Lattner910c80a2007-02-24 00:55:48 +00002227 BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002228public:
Gabor Greifefe65362008-05-10 08:32:32 +00002229 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2230 unsigned NumCases, Instruction *InsertBefore = 0) {
2231 return new SwitchInst(Value, Default, NumCases, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002232 }
Gabor Greifefe65362008-05-10 08:32:32 +00002233 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2234 unsigned NumCases, BasicBlock *InsertAtEnd) {
2235 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002236 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +00002237 ~SwitchInst();
Chris Lattner454928e2005-01-29 00:31:36 +00002238
Gabor Greifefe65362008-05-10 08:32:32 +00002239 /// Provide fast operand accessors
2240 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2241
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002242 // Accessor Methods for Switch stmt
Devang Patel4d4a5e02008-02-23 01:11:02 +00002243 Value *getCondition() const { return getOperand(0); }
Chris Lattner454928e2005-01-29 00:31:36 +00002244 void setCondition(Value *V) { setOperand(0, V); }
Chris Lattnerbfaf88a2004-12-10 20:35:47 +00002245
Devang Patel4d4a5e02008-02-23 01:11:02 +00002246 BasicBlock *getDefaultDest() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002247 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002248 }
2249
2250 /// getNumCases - return the number of 'cases' in this switch instruction.
2251 /// Note that case #0 is always the default case.
2252 unsigned getNumCases() const {
Chris Lattner454928e2005-01-29 00:31:36 +00002253 return getNumOperands()/2;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002254 }
2255
2256 /// getCaseValue - Return the specified case value. Note that case #0, the
2257 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002258 ConstantInt *getCaseValue(unsigned i) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002259 assert(i && i < getNumCases() && "Illegal case value to get!");
2260 return getSuccessorValue(i);
2261 }
2262
2263 /// getCaseValue - Return the specified case value. Note that case #0, the
2264 /// default destination, does not have a case value.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002265 const ConstantInt *getCaseValue(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002266 assert(i && i < getNumCases() && "Illegal case value to get!");
2267 return getSuccessorValue(i);
2268 }
2269
2270 /// findCaseValue - Search all of the case values for the specified constant.
2271 /// If it is explicitly handled, return the case number of it, otherwise
2272 /// return 0 to indicate that it is handled by the default handler.
Chris Lattnerd1a32602005-02-24 05:32:09 +00002273 unsigned findCaseValue(const ConstantInt *C) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002274 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2275 if (getCaseValue(i) == C)
2276 return i;
2277 return 0;
2278 }
2279
Nick Lewycky011f1842006-09-18 19:03:59 +00002280 /// findCaseDest - Finds the unique case value for a given successor. Returns
2281 /// null if the successor is not found, not unique, or is the default case.
2282 ConstantInt *findCaseDest(BasicBlock *BB) {
Nick Lewyckyd7915442006-09-18 20:44:37 +00002283 if (BB == getDefaultDest()) return NULL;
2284
Nick Lewycky011f1842006-09-18 19:03:59 +00002285 ConstantInt *CI = NULL;
2286 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2287 if (getSuccessor(i) == BB) {
2288 if (CI) return NULL; // Multiple cases lead to BB.
2289 else CI = getCaseValue(i);
2290 }
2291 }
2292 return CI;
2293 }
2294
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002295 /// addCase - Add an entry to the switch instruction...
2296 ///
Chris Lattnerd1a32602005-02-24 05:32:09 +00002297 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002298
2299 /// removeCase - This method removes the specified successor from the switch
2300 /// instruction. Note that this cannot be used to remove the default
2301 /// destination (successor #0).
2302 ///
2303 void removeCase(unsigned idx);
2304
Nick Lewycky67760642009-09-27 07:38:41 +00002305 virtual SwitchInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002306
Chris Lattner454928e2005-01-29 00:31:36 +00002307 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2308 BasicBlock *getSuccessor(unsigned idx) const {
2309 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2310 return cast<BasicBlock>(getOperand(idx*2+1));
2311 }
2312 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002313 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
Gabor Greifefe65362008-05-10 08:32:32 +00002314 setOperand(idx*2+1, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002315 }
2316
2317 // getSuccessorValue - Return the value associated with the specified
2318 // successor.
Devang Patel4d4a5e02008-02-23 01:11:02 +00002319 ConstantInt *getSuccessorValue(unsigned idx) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002320 assert(idx < getNumSuccessors() && "Successor # out of range!");
Reid Spenceredd5d9e2005-05-15 16:13:11 +00002321 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002322 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002323
2324 // Methods for support type inquiry through isa, cast, and dyn_cast:
2325 static inline bool classof(const SwitchInst *) { return true; }
2326 static inline bool classof(const Instruction *I) {
Chris Lattnerd1a32602005-02-24 05:32:09 +00002327 return I->getOpcode() == Instruction::Switch;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002328 }
2329 static inline bool classof(const Value *V) {
2330 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2331 }
Chris Lattner454928e2005-01-29 00:31:36 +00002332private:
2333 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2334 virtual unsigned getNumSuccessorsV() const;
2335 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002336};
2337
Gabor Greifefe65362008-05-10 08:32:32 +00002338template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002339struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
Gabor Greifefe65362008-05-10 08:32:32 +00002340};
2341
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002342DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
Gabor Greifefe65362008-05-10 08:32:32 +00002343
2344
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002345//===----------------------------------------------------------------------===//
2346// InvokeInst Class
2347//===----------------------------------------------------------------------===//
2348
Chris Lattner3340ffe2005-05-06 20:26:26 +00002349/// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2350/// calling convention of the call.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002351///
2352class InvokeInst : public TerminatorInst {
Devang Patel05988662008-09-25 21:00:45 +00002353 AttrListPtr AttributeList;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002354 InvokeInst(const InvokeInst &BI);
2355 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerd2dd1502007-02-13 01:04:01 +00002356 Value* const *Args, unsigned NumArgs);
David Greenef1355a52007-08-27 19:04:21 +00002357
2358 template<typename InputIterator>
2359 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2360 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002361 const Twine &NameStr,
David Greenef1355a52007-08-27 19:04:21 +00002362 // This argument ensures that we have an iterator we can
2363 // do arithmetic on in constant time
2364 std::random_access_iterator_tag) {
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002365 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002366
Chris Lattnera5c0d1e2007-08-29 16:32:50 +00002367 // This requires that the iterator points to contiguous memory.
2368 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
Evan Cheng1bf9a182008-07-24 00:08:56 +00002369 setName(NameStr);
David Greenef1355a52007-08-27 19:04:21 +00002370 }
2371
David Greenef1355a52007-08-27 19:04:21 +00002372 /// Construct an InvokeInst given a range of arguments.
2373 /// InputIterator must be a random-access iterator pointing to
2374 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2375 /// made for random-accessness but not for contiguous storage as
2376 /// that would incur runtime overhead.
2377 ///
2378 /// @brief Construct an InvokeInst from a range of arguments
2379 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002380 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2381 InputIterator ArgBegin, InputIterator ArgEnd,
2382 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002383 const Twine &NameStr, Instruction *InsertBefore);
David Greenef1355a52007-08-27 19:04:21 +00002384
2385 /// Construct an InvokeInst given a range of arguments.
2386 /// InputIterator must be a random-access iterator pointing to
2387 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2388 /// made for random-accessness but not for contiguous storage as
2389 /// that would incur runtime overhead.
2390 ///
2391 /// @brief Construct an InvokeInst from a range of arguments
2392 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002393 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2394 InputIterator ArgBegin, InputIterator ArgEnd,
2395 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002396 const Twine &NameStr, BasicBlock *InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002397public:
2398 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002399 static InvokeInst *Create(Value *Func,
2400 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002401 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002402 const Twine &NameStr = "",
Evan Chengd69bb1a2008-05-05 17:41:03 +00002403 Instruction *InsertBefore = 0) {
Gabor Greifefe65362008-05-10 08:32:32 +00002404 unsigned Values(ArgEnd - ArgBegin + 3);
2405 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002406 Values, NameStr, InsertBefore);
Gabor Greif051a9502008-04-06 20:25:17 +00002407 }
2408 template<typename InputIterator>
Gabor Greifefe65362008-05-10 08:32:32 +00002409 static InvokeInst *Create(Value *Func,
2410 BasicBlock *IfNormal, BasicBlock *IfException,
Gabor Greif051a9502008-04-06 20:25:17 +00002411 InputIterator ArgBegin, InputIterator ArgEnd,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002412 const Twine &NameStr,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002413 BasicBlock *InsertAtEnd) {
Gabor Greifefe65362008-05-10 08:32:32 +00002414 unsigned Values(ArgEnd - ArgBegin + 3);
2415 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
Evan Cheng1bf9a182008-07-24 00:08:56 +00002416 Values, NameStr, InsertAtEnd);
Gabor Greif051a9502008-04-06 20:25:17 +00002417 }
David Greenef1355a52007-08-27 19:04:21 +00002418
Nick Lewycky67760642009-09-27 07:38:41 +00002419 virtual InvokeInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002420
Gabor Greifefe65362008-05-10 08:32:32 +00002421 /// Provide fast operand accessors
2422 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002423
Chris Lattner3340ffe2005-05-06 20:26:26 +00002424 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2425 /// function call.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002426 CallingConv::ID getCallingConv() const {
2427 return static_cast<CallingConv::ID>(SubclassData);
2428 }
2429 void setCallingConv(CallingConv::ID CC) {
2430 SubclassData = static_cast<unsigned>(CC);
Chris Lattner3340ffe2005-05-06 20:26:26 +00002431 }
2432
Devang Patel05988662008-09-25 21:00:45 +00002433 /// getAttributes - Return the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002434 ///
Devang Patel05988662008-09-25 21:00:45 +00002435 const AttrListPtr &getAttributes() const { return AttributeList; }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002436
Devang Patel05988662008-09-25 21:00:45 +00002437 /// setAttributes - Set the parameter attributes for this invoke.
Chris Lattner58d74912008-03-12 17:45:29 +00002438 ///
Devang Patel05988662008-09-25 21:00:45 +00002439 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
Duncan Sandsdc024672007-11-27 13:23:08 +00002440
Devang Patel05988662008-09-25 21:00:45 +00002441 /// addAttribute - adds the attribute to the list of attributes.
2442 void addAttribute(unsigned i, Attributes attr);
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002443
Devang Patel05988662008-09-25 21:00:45 +00002444 /// removeAttribute - removes the attribute from the list of attributes.
2445 void removeAttribute(unsigned i, Attributes attr);
Duncan Sands2e033f32008-07-08 08:38:44 +00002446
Dan Gohmanf2752502008-09-26 21:38:45 +00002447 /// @brief Determine whether the call or the callee has the given attribute.
2448 bool paramHasAttr(unsigned i, Attributes attr) const;
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002449
Dale Johannesen08e78b12008-02-22 17:49:45 +00002450 /// @brief Extract the alignment for a call or parameter (0=unknown).
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002451 unsigned getParamAlignment(unsigned i) const {
Devang Patel05988662008-09-25 21:00:45 +00002452 return AttributeList.getParamAlignment(i);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002453 }
Dale Johannesen08e78b12008-02-22 17:49:45 +00002454
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002455 /// @brief Determine if the call does not access memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002456 bool doesNotAccessMemory() const {
Dan Gohmana62b5ed2009-07-17 16:12:36 +00002457 return paramHasAttr(~0, Attribute::ReadNone);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002458 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002459 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002460 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2461 else removeAttribute(~0, Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00002462 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002463
2464 /// @brief Determine if the call does not access or only reads memory.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002465 bool onlyReadsMemory() const {
Devang Patel19c87462008-09-26 22:53:05 +00002466 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002467 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002468 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002469 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2470 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands2e033f32008-07-08 08:38:44 +00002471 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002472
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002473 /// @brief Determine if the call cannot return.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002474 bool doesNotReturn() const {
Devang Patel19c87462008-09-26 22:53:05 +00002475 return paramHasAttr(~0, Attribute::NoReturn);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002476 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002477 void setDoesNotReturn(bool DoesNotReturn = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002478 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2479 else removeAttribute(~0, Attribute::NoReturn);
Duncan Sands2e033f32008-07-08 08:38:44 +00002480 }
Duncan Sandscbb8bad2007-12-10 19:09:40 +00002481
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002482 /// @brief Determine if the call cannot unwind.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002483 bool doesNotThrow() const {
Devang Patel19c87462008-09-26 22:53:05 +00002484 return paramHasAttr(~0, Attribute::NoUnwind);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002485 }
Evan Cheng1bf9a182008-07-24 00:08:56 +00002486 void setDoesNotThrow(bool DoesNotThrow = true) {
Devang Patel19c87462008-09-26 22:53:05 +00002487 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2488 else removeAttribute(~0, Attribute::NoUnwind);
Duncan Sands2e033f32008-07-08 08:38:44 +00002489 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +00002490
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002491 /// @brief Determine if the call returns a structure through first
Devang Patel41e23972008-03-03 21:46:28 +00002492 /// pointer argument.
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002493 bool hasStructRetAttr() const {
2494 // Be friendly and also check the callee.
Devang Patel05988662008-09-25 21:00:45 +00002495 return paramHasAttr(1, Attribute::StructRet);
Chris Lattnerd5d94df2008-03-13 05:00:21 +00002496 }
Reid Spencerfa3e9122007-04-09 18:00:57 +00002497
Dan Gohmanf2752502008-09-26 21:38:45 +00002498 /// @brief Determine if any call argument is an aggregate passed by value.
2499 bool hasByValArgument() const {
2500 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2501 }
2502
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002503 /// getCalledFunction - Return the function called, or null if this is an
Chris Lattner721aef62004-11-18 17:46:57 +00002504 /// indirect function invocation.
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002505 ///
Chris Lattner721aef62004-11-18 17:46:57 +00002506 Function *getCalledFunction() const {
Gabor Greif03a5f132009-09-03 02:02:59 +00002507 return dyn_cast<Function>(getOperand(0));
Chris Lattner721aef62004-11-18 17:46:57 +00002508 }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002509
Mikhail Glushenkoved2c4532009-02-09 17:11:05 +00002510 /// getCalledValue - Get a pointer to the function that is invoked by this
Dan Gohmanf2752502008-09-26 21:38:45 +00002511 /// instruction
Gabor Greif03a5f132009-09-03 02:02:59 +00002512 const Value *getCalledValue() const { return getOperand(0); }
2513 Value *getCalledValue() { return getOperand(0); }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002514
2515 // get*Dest - Return the destination basic blocks...
Chris Lattner454928e2005-01-29 00:31:36 +00002516 BasicBlock *getNormalDest() const {
Gabor Greif03a5f132009-09-03 02:02:59 +00002517 return cast<BasicBlock>(getOperand(1));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002518 }
Chris Lattner454928e2005-01-29 00:31:36 +00002519 BasicBlock *getUnwindDest() const {
Gabor Greif03a5f132009-09-03 02:02:59 +00002520 return cast<BasicBlock>(getOperand(2));
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002521 }
Chris Lattner454928e2005-01-29 00:31:36 +00002522 void setNormalDest(BasicBlock *B) {
Gabor Greif03a5f132009-09-03 02:02:59 +00002523 setOperand(1, B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002524 }
Gabor Greif03a5f132009-09-03 02:02:59 +00002525
Chris Lattner454928e2005-01-29 00:31:36 +00002526 void setUnwindDest(BasicBlock *B) {
Gabor Greif03a5f132009-09-03 02:02:59 +00002527 setOperand(2, B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002528 }
2529
Devang Patel4d4a5e02008-02-23 01:11:02 +00002530 BasicBlock *getSuccessor(unsigned i) const {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002531 assert(i < 2 && "Successor # out of range for invoke!");
2532 return i == 0 ? getNormalDest() : getUnwindDest();
2533 }
2534
Chris Lattner454928e2005-01-29 00:31:36 +00002535 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002536 assert(idx < 2 && "Successor # out of range for invoke!");
Gabor Greif03a5f132009-09-03 02:02:59 +00002537 setOperand(idx+1, NewSucc);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002538 }
2539
Chris Lattner454928e2005-01-29 00:31:36 +00002540 unsigned getNumSuccessors() const { return 2; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002541
2542 // Methods for support type inquiry through isa, cast, and dyn_cast:
2543 static inline bool classof(const InvokeInst *) { return true; }
2544 static inline bool classof(const Instruction *I) {
2545 return (I->getOpcode() == Instruction::Invoke);
2546 }
2547 static inline bool classof(const Value *V) {
2548 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2549 }
Chris Lattner454928e2005-01-29 00:31:36 +00002550private:
2551 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2552 virtual unsigned getNumSuccessorsV() const;
2553 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002554};
2555
Gabor Greifefe65362008-05-10 08:32:32 +00002556template <>
Duncan Sands59bf4fc2009-09-06 08:55:57 +00002557struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
Gabor Greifefe65362008-05-10 08:32:32 +00002558};
2559
2560template<typename InputIterator>
2561InvokeInst::InvokeInst(Value *Func,
2562 BasicBlock *IfNormal, BasicBlock *IfException,
2563 InputIterator ArgBegin, InputIterator ArgEnd,
2564 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002565 const Twine &NameStr, Instruction *InsertBefore)
Gabor Greifefe65362008-05-10 08:32:32 +00002566 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2567 ->getElementType())->getReturnType(),
2568 Instruction::Invoke,
2569 OperandTraits<InvokeInst>::op_end(this) - Values,
2570 Values, InsertBefore) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002571 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00002572 typename std::iterator_traits<InputIterator>::iterator_category());
2573}
2574template<typename InputIterator>
2575InvokeInst::InvokeInst(Value *Func,
2576 BasicBlock *IfNormal, BasicBlock *IfException,
2577 InputIterator ArgBegin, InputIterator ArgEnd,
2578 unsigned Values,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002579 const Twine &NameStr, BasicBlock *InsertAtEnd)
Gabor Greifefe65362008-05-10 08:32:32 +00002580 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2581 ->getElementType())->getReturnType(),
2582 Instruction::Invoke,
2583 OperandTraits<InvokeInst>::op_end(this) - Values,
2584 Values, InsertAtEnd) {
Evan Cheng1bf9a182008-07-24 00:08:56 +00002585 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
Gabor Greifefe65362008-05-10 08:32:32 +00002586 typename std::iterator_traits<InputIterator>::iterator_category());
2587}
2588
2589DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002590
2591//===----------------------------------------------------------------------===//
2592// UnwindInst Class
2593//===----------------------------------------------------------------------===//
2594
2595//===---------------------------------------------------------------------------
2596/// UnwindInst - Immediately exit the current function, unwinding the stack
2597/// until an invoke instruction is found.
2598///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002599class UnwindInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002600 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002601public:
Gabor Greif051a9502008-04-06 20:25:17 +00002602 // allocate space for exactly zero operands
2603 void *operator new(size_t s) {
2604 return User::operator new(s, 0);
2605 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002606 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2607 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002608
Nick Lewycky67760642009-09-27 07:38:41 +00002609 virtual UnwindInst *clone() const;
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002610
Chris Lattner454928e2005-01-29 00:31:36 +00002611 unsigned getNumSuccessors() const { return 0; }
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002612
2613 // Methods for support type inquiry through isa, cast, and dyn_cast:
2614 static inline bool classof(const UnwindInst *) { return true; }
2615 static inline bool classof(const Instruction *I) {
2616 return I->getOpcode() == Instruction::Unwind;
2617 }
2618 static inline bool classof(const Value *V) {
2619 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2620 }
Chris Lattner454928e2005-01-29 00:31:36 +00002621private:
2622 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2623 virtual unsigned getNumSuccessorsV() const;
2624 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00002625};
2626
Chris Lattner076b3f12004-10-16 18:05:54 +00002627//===----------------------------------------------------------------------===//
2628// UnreachableInst Class
2629//===----------------------------------------------------------------------===//
2630
2631//===---------------------------------------------------------------------------
2632/// UnreachableInst - This function has undefined behavior. In particular, the
2633/// presence of this instruction indicates some higher level knowledge that the
2634/// end of the block cannot be reached.
2635///
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002636class UnreachableInst : public TerminatorInst {
Gabor Greif051a9502008-04-06 20:25:17 +00002637 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Chris Lattner1fca5ff2004-10-27 16:14:51 +00002638public:
Gabor Greif051a9502008-04-06 20:25:17 +00002639 // allocate space for exactly zero operands
2640 void *operator new(size_t s) {
2641 return User::operator new(s, 0);
2642 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002643 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2644 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
Chris Lattner076b3f12004-10-16 18:05:54 +00002645
Nick Lewycky67760642009-09-27 07:38:41 +00002646 virtual UnreachableInst *clone() const;
Chris Lattner076b3f12004-10-16 18:05:54 +00002647
Chris Lattner454928e2005-01-29 00:31:36 +00002648 unsigned getNumSuccessors() const { return 0; }
Chris Lattner076b3f12004-10-16 18:05:54 +00002649
2650 // Methods for support type inquiry through isa, cast, and dyn_cast:
2651 static inline bool classof(const UnreachableInst *) { return true; }
2652 static inline bool classof(const Instruction *I) {
2653 return I->getOpcode() == Instruction::Unreachable;
2654 }
2655 static inline bool classof(const Value *V) {
2656 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2657 }
Chris Lattner454928e2005-01-29 00:31:36 +00002658private:
2659 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2660 virtual unsigned getNumSuccessorsV() const;
2661 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
Chris Lattner076b3f12004-10-16 18:05:54 +00002662};
2663
Reid Spencer3da59db2006-11-27 01:05:10 +00002664//===----------------------------------------------------------------------===//
2665// TruncInst Class
2666//===----------------------------------------------------------------------===//
2667
2668/// @brief This class represents a truncation of integer types.
2669class TruncInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002670public:
2671 /// @brief Constructor with insert-before-instruction semantics
2672 TruncInst(
2673 Value *S, ///< The value to be truncated
2674 const Type *Ty, ///< The (smaller) type to truncate to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002675 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002676 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2677 );
2678
2679 /// @brief Constructor with insert-at-end-of-block semantics
2680 TruncInst(
2681 Value *S, ///< The value to be truncated
2682 const Type *Ty, ///< The (smaller) type to truncate to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002683 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002684 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2685 );
2686
2687 /// @brief Clone an identical TruncInst
Nick Lewycky67760642009-09-27 07:38:41 +00002688 virtual TruncInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002689
2690 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2691 static inline bool classof(const TruncInst *) { return true; }
2692 static inline bool classof(const Instruction *I) {
2693 return I->getOpcode() == Trunc;
2694 }
2695 static inline bool classof(const Value *V) {
2696 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2697 }
2698};
2699
2700//===----------------------------------------------------------------------===//
2701// ZExtInst Class
2702//===----------------------------------------------------------------------===//
2703
2704/// @brief This class represents zero extension of integer types.
2705class ZExtInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002706public:
2707 /// @brief Constructor with insert-before-instruction semantics
2708 ZExtInst(
2709 Value *S, ///< The value to be zero extended
2710 const Type *Ty, ///< The type to zero extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002711 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002712 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2713 );
2714
2715 /// @brief Constructor with insert-at-end semantics.
2716 ZExtInst(
2717 Value *S, ///< The value to be zero extended
2718 const Type *Ty, ///< The type to zero extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002719 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002720 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2721 );
2722
2723 /// @brief Clone an identical ZExtInst
Nick Lewycky67760642009-09-27 07:38:41 +00002724 virtual ZExtInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002725
2726 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2727 static inline bool classof(const ZExtInst *) { return true; }
2728 static inline bool classof(const Instruction *I) {
2729 return I->getOpcode() == ZExt;
2730 }
2731 static inline bool classof(const Value *V) {
2732 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2733 }
2734};
2735
2736//===----------------------------------------------------------------------===//
2737// SExtInst Class
2738//===----------------------------------------------------------------------===//
2739
2740/// @brief This class represents a sign extension of integer types.
2741class SExtInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002742public:
2743 /// @brief Constructor with insert-before-instruction semantics
2744 SExtInst(
2745 Value *S, ///< The value to be sign extended
2746 const Type *Ty, ///< The type to sign extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002747 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002748 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2749 );
2750
2751 /// @brief Constructor with insert-at-end-of-block semantics
2752 SExtInst(
2753 Value *S, ///< The value to be sign extended
2754 const Type *Ty, ///< The type to sign extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002755 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002756 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2757 );
2758
2759 /// @brief Clone an identical SExtInst
Nick Lewycky67760642009-09-27 07:38:41 +00002760 virtual SExtInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002761
2762 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2763 static inline bool classof(const SExtInst *) { return true; }
2764 static inline bool classof(const Instruction *I) {
2765 return I->getOpcode() == SExt;
2766 }
2767 static inline bool classof(const Value *V) {
2768 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2769 }
2770};
2771
2772//===----------------------------------------------------------------------===//
2773// FPTruncInst Class
2774//===----------------------------------------------------------------------===//
2775
2776/// @brief This class represents a truncation of floating point types.
2777class FPTruncInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002778public:
2779 /// @brief Constructor with insert-before-instruction semantics
2780 FPTruncInst(
2781 Value *S, ///< The value to be truncated
2782 const Type *Ty, ///< The type to truncate to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002783 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002784 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2785 );
2786
2787 /// @brief Constructor with insert-before-instruction semantics
2788 FPTruncInst(
2789 Value *S, ///< The value to be truncated
2790 const Type *Ty, ///< The type to truncate to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002791 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002792 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2793 );
2794
2795 /// @brief Clone an identical FPTruncInst
Nick Lewycky67760642009-09-27 07:38:41 +00002796 virtual FPTruncInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002797
2798 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2799 static inline bool classof(const FPTruncInst *) { return true; }
2800 static inline bool classof(const Instruction *I) {
2801 return I->getOpcode() == FPTrunc;
2802 }
2803 static inline bool classof(const Value *V) {
2804 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2805 }
2806};
2807
2808//===----------------------------------------------------------------------===//
2809// FPExtInst Class
2810//===----------------------------------------------------------------------===//
2811
2812/// @brief This class represents an extension of floating point types.
2813class FPExtInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002814public:
2815 /// @brief Constructor with insert-before-instruction semantics
2816 FPExtInst(
2817 Value *S, ///< The value to be extended
2818 const Type *Ty, ///< The type to extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002819 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002820 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2821 );
2822
2823 /// @brief Constructor with insert-at-end-of-block semantics
2824 FPExtInst(
2825 Value *S, ///< The value to be extended
2826 const Type *Ty, ///< The type to extend to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002827 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002828 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2829 );
2830
2831 /// @brief Clone an identical FPExtInst
Nick Lewycky67760642009-09-27 07:38:41 +00002832 virtual FPExtInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002833
2834 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2835 static inline bool classof(const FPExtInst *) { return true; }
2836 static inline bool classof(const Instruction *I) {
2837 return I->getOpcode() == FPExt;
2838 }
2839 static inline bool classof(const Value *V) {
2840 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2841 }
2842};
2843
2844//===----------------------------------------------------------------------===//
2845// UIToFPInst Class
2846//===----------------------------------------------------------------------===//
2847
2848/// @brief This class represents a cast unsigned integer to floating point.
2849class UIToFPInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002850public:
2851 /// @brief Constructor with insert-before-instruction semantics
2852 UIToFPInst(
2853 Value *S, ///< The value to be converted
2854 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002855 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002856 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2857 );
2858
2859 /// @brief Constructor with insert-at-end-of-block semantics
2860 UIToFPInst(
2861 Value *S, ///< The value to be converted
2862 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002863 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002864 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2865 );
2866
2867 /// @brief Clone an identical UIToFPInst
Nick Lewycky67760642009-09-27 07:38:41 +00002868 virtual UIToFPInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002869
2870 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2871 static inline bool classof(const UIToFPInst *) { return true; }
2872 static inline bool classof(const Instruction *I) {
2873 return I->getOpcode() == UIToFP;
2874 }
2875 static inline bool classof(const Value *V) {
2876 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2877 }
2878};
2879
2880//===----------------------------------------------------------------------===//
2881// SIToFPInst Class
2882//===----------------------------------------------------------------------===//
2883
2884/// @brief This class represents a cast from signed integer to floating point.
2885class SIToFPInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002886public:
2887 /// @brief Constructor with insert-before-instruction semantics
2888 SIToFPInst(
2889 Value *S, ///< The value to be converted
2890 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002891 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002892 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2893 );
2894
2895 /// @brief Constructor with insert-at-end-of-block semantics
2896 SIToFPInst(
2897 Value *S, ///< The value to be converted
2898 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002899 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002900 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2901 );
2902
2903 /// @brief Clone an identical SIToFPInst
Nick Lewycky67760642009-09-27 07:38:41 +00002904 virtual SIToFPInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002905
2906 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2907 static inline bool classof(const SIToFPInst *) { return true; }
2908 static inline bool classof(const Instruction *I) {
2909 return I->getOpcode() == SIToFP;
2910 }
2911 static inline bool classof(const Value *V) {
2912 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2913 }
2914};
2915
2916//===----------------------------------------------------------------------===//
2917// FPToUIInst Class
2918//===----------------------------------------------------------------------===//
2919
2920/// @brief This class represents a cast from floating point to unsigned integer
2921class FPToUIInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002922public:
2923 /// @brief Constructor with insert-before-instruction semantics
2924 FPToUIInst(
2925 Value *S, ///< The value to be converted
2926 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002927 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002928 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2929 );
2930
2931 /// @brief Constructor with insert-at-end-of-block semantics
2932 FPToUIInst(
2933 Value *S, ///< The value to be converted
2934 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002935 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002936 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2937 );
2938
2939 /// @brief Clone an identical FPToUIInst
Nick Lewycky67760642009-09-27 07:38:41 +00002940 virtual FPToUIInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002941
2942 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2943 static inline bool classof(const FPToUIInst *) { return true; }
2944 static inline bool classof(const Instruction *I) {
2945 return I->getOpcode() == FPToUI;
2946 }
2947 static inline bool classof(const Value *V) {
2948 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2949 }
2950};
2951
2952//===----------------------------------------------------------------------===//
2953// FPToSIInst Class
2954//===----------------------------------------------------------------------===//
2955
2956/// @brief This class represents a cast from floating point to signed integer.
2957class FPToSIInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002958public:
2959 /// @brief Constructor with insert-before-instruction semantics
2960 FPToSIInst(
2961 Value *S, ///< The value to be converted
2962 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002963 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002964 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2965 );
2966
2967 /// @brief Constructor with insert-at-end-of-block semantics
2968 FPToSIInst(
2969 Value *S, ///< The value to be converted
2970 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002971 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00002972 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2973 );
2974
2975 /// @brief Clone an identical FPToSIInst
Nick Lewycky67760642009-09-27 07:38:41 +00002976 virtual FPToSIInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00002977
2978 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2979 static inline bool classof(const FPToSIInst *) { return true; }
2980 static inline bool classof(const Instruction *I) {
2981 return I->getOpcode() == FPToSI;
2982 }
2983 static inline bool classof(const Value *V) {
2984 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2985 }
2986};
2987
2988//===----------------------------------------------------------------------===//
2989// IntToPtrInst Class
2990//===----------------------------------------------------------------------===//
2991
2992/// @brief This class represents a cast from an integer to a pointer.
2993class IntToPtrInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002994public:
2995 /// @brief Constructor with insert-before-instruction semantics
2996 IntToPtrInst(
2997 Value *S, ///< The value to be converted
2998 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00002999 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003000 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3001 );
3002
3003 /// @brief Constructor with insert-at-end-of-block semantics
3004 IntToPtrInst(
3005 Value *S, ///< The value to be converted
3006 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003007 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003008 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3009 );
3010
3011 /// @brief Clone an identical IntToPtrInst
Nick Lewycky67760642009-09-27 07:38:41 +00003012 virtual IntToPtrInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00003013
3014 // Methods for support type inquiry through isa, cast, and dyn_cast:
3015 static inline bool classof(const IntToPtrInst *) { return true; }
3016 static inline bool classof(const Instruction *I) {
3017 return I->getOpcode() == IntToPtr;
3018 }
3019 static inline bool classof(const Value *V) {
3020 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3021 }
3022};
3023
3024//===----------------------------------------------------------------------===//
3025// PtrToIntInst Class
3026//===----------------------------------------------------------------------===//
3027
3028/// @brief This class represents a cast from a pointer to an integer
3029class PtrToIntInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00003030public:
3031 /// @brief Constructor with insert-before-instruction semantics
3032 PtrToIntInst(
3033 Value *S, ///< The value to be converted
3034 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003035 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003036 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3037 );
3038
3039 /// @brief Constructor with insert-at-end-of-block semantics
3040 PtrToIntInst(
3041 Value *S, ///< The value to be converted
3042 const Type *Ty, ///< The type to convert to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003043 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003044 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3045 );
3046
3047 /// @brief Clone an identical PtrToIntInst
Nick Lewycky67760642009-09-27 07:38:41 +00003048 virtual PtrToIntInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00003049
3050 // Methods for support type inquiry through isa, cast, and dyn_cast:
3051 static inline bool classof(const PtrToIntInst *) { return true; }
3052 static inline bool classof(const Instruction *I) {
3053 return I->getOpcode() == PtrToInt;
3054 }
3055 static inline bool classof(const Value *V) {
3056 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3057 }
3058};
3059
3060//===----------------------------------------------------------------------===//
3061// BitCastInst Class
3062//===----------------------------------------------------------------------===//
3063
3064/// @brief This class represents a no-op cast from one type to another.
3065class BitCastInst : public CastInst {
Reid Spencer3da59db2006-11-27 01:05:10 +00003066public:
3067 /// @brief Constructor with insert-before-instruction semantics
3068 BitCastInst(
3069 Value *S, ///< The value to be casted
3070 const Type *Ty, ///< The type to casted to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003071 const Twine &NameStr = "", ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003072 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3073 );
3074
3075 /// @brief Constructor with insert-at-end-of-block semantics
3076 BitCastInst(
3077 Value *S, ///< The value to be casted
3078 const Type *Ty, ///< The type to casted to
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +00003079 const Twine &NameStr, ///< A name for the new instruction
Reid Spencer3da59db2006-11-27 01:05:10 +00003080 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3081 );
3082
3083 /// @brief Clone an identical BitCastInst
Nick Lewycky67760642009-09-27 07:38:41 +00003084 virtual BitCastInst *clone() const;
Reid Spencer3da59db2006-11-27 01:05:10 +00003085
3086 // Methods for support type inquiry through isa, cast, and dyn_cast:
3087 static inline bool classof(const BitCastInst *) { return true; }
3088 static inline bool classof(const Instruction *I) {
3089 return I->getOpcode() == BitCast;
3090 }
3091 static inline bool classof(const Value *V) {
3092 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3093 }
3094};
3095
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +00003096} // End llvm namespace
Chris Lattnera892a3a2003-01-27 22:08:52 +00003097
3098#endif