Chris Lattner | a892a3a | 2003-01-27 22:08:52 +0000 | [diff] [blame] | 1 | //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===// |
John Criswell | 6fbcc26 | 2003-10-20 20:19:47 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | a892a3a | 2003-01-27 22:08:52 +0000 | [diff] [blame] | 9 | // |
| 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 Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 19 | #include "llvm/Instruction.h" |
| 20 | #include "llvm/InstrTypes.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
Chris Lattner | 1fca5ff | 2004-10-27 16:14:51 +0000 | [diff] [blame] | 24 | class BasicBlock; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 25 | class PointerType; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // AllocationInst Class |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | /// AllocationInst - This class is the common base class of MallocInst and |
| 32 | /// AllocaInst. |
| 33 | /// |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 34 | class AllocationInst : public UnaryInstruction { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 35 | protected: |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 36 | AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
| 37 | const std::string &Name = "", Instruction *InsertBefore = 0); |
| 38 | AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
| 39 | const std::string &Name, BasicBlock *InsertAtEnd); |
| 40 | |
| 41 | public: |
| 42 | |
| 43 | /// isArrayAllocation - Return true if there is an allocation size parameter |
| 44 | /// to the allocation instruction that is not 1. |
| 45 | /// |
| 46 | bool isArrayAllocation() const; |
| 47 | |
| 48 | /// getArraySize - Get the number of element allocated, for a simple |
| 49 | /// allocation of a single element, this will return a constant 1 value. |
| 50 | /// |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 51 | inline const Value *getArraySize() const { return getOperand(0); } |
| 52 | inline Value *getArraySize() { return getOperand(0); } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 53 | |
| 54 | /// getType - Overload to return most specific pointer type |
| 55 | /// |
| 56 | inline const PointerType *getType() const { |
| 57 | return reinterpret_cast<const PointerType*>(Instruction::getType()); |
| 58 | } |
| 59 | |
| 60 | /// getAllocatedType - Return the type that is being allocated by the |
| 61 | /// instruction. |
| 62 | /// |
| 63 | const Type *getAllocatedType() const; |
| 64 | |
| 65 | virtual Instruction *clone() const = 0; |
| 66 | |
| 67 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 68 | static inline bool classof(const AllocationInst *) { return true; } |
| 69 | static inline bool classof(const Instruction *I) { |
| 70 | return I->getOpcode() == Instruction::Alloca || |
| 71 | I->getOpcode() == Instruction::Malloc; |
| 72 | } |
| 73 | static inline bool classof(const Value *V) { |
| 74 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | // MallocInst Class |
| 81 | //===----------------------------------------------------------------------===// |
| 82 | |
| 83 | /// MallocInst - an instruction to allocated memory on the heap |
| 84 | /// |
| 85 | class MallocInst : public AllocationInst { |
| 86 | MallocInst(const MallocInst &MI); |
| 87 | public: |
| 88 | explicit MallocInst(const Type *Ty, Value *ArraySize = 0, |
| 89 | const std::string &Name = "", |
| 90 | Instruction *InsertBefore = 0) |
| 91 | : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {} |
| 92 | MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name, |
| 93 | BasicBlock *InsertAtEnd) |
| 94 | : AllocationInst(Ty, ArraySize, Malloc, Name, InsertAtEnd) {} |
| 95 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 96 | virtual MallocInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 97 | |
| 98 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 99 | static inline bool classof(const MallocInst *) { return true; } |
| 100 | static inline bool classof(const Instruction *I) { |
| 101 | return (I->getOpcode() == Instruction::Malloc); |
| 102 | } |
| 103 | static inline bool classof(const Value *V) { |
| 104 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | |
| 109 | //===----------------------------------------------------------------------===// |
| 110 | // AllocaInst Class |
| 111 | //===----------------------------------------------------------------------===// |
| 112 | |
| 113 | /// AllocaInst - an instruction to allocate memory on the stack |
| 114 | /// |
| 115 | class AllocaInst : public AllocationInst { |
| 116 | AllocaInst(const AllocaInst &); |
| 117 | public: |
| 118 | explicit AllocaInst(const Type *Ty, Value *ArraySize = 0, |
| 119 | const std::string &Name = "", |
| 120 | Instruction *InsertBefore = 0) |
| 121 | : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {} |
| 122 | AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name, |
| 123 | BasicBlock *InsertAtEnd) |
| 124 | : AllocationInst(Ty, ArraySize, Alloca, Name, InsertAtEnd) {} |
| 125 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 126 | virtual AllocaInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 127 | |
| 128 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 129 | static inline bool classof(const AllocaInst *) { return true; } |
| 130 | static inline bool classof(const Instruction *I) { |
| 131 | return (I->getOpcode() == Instruction::Alloca); |
| 132 | } |
| 133 | static inline bool classof(const Value *V) { |
| 134 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | //===----------------------------------------------------------------------===// |
| 140 | // FreeInst Class |
| 141 | //===----------------------------------------------------------------------===// |
| 142 | |
| 143 | /// FreeInst - an instruction to deallocate memory |
| 144 | /// |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 145 | class FreeInst : public UnaryInstruction { |
| 146 | void AssertOK(); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 147 | public: |
| 148 | explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0); |
| 149 | FreeInst(Value *Ptr, BasicBlock *InsertAfter); |
| 150 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 151 | virtual FreeInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 152 | |
| 153 | virtual bool mayWriteToMemory() const { return true; } |
| 154 | |
| 155 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 156 | static inline bool classof(const FreeInst *) { return true; } |
| 157 | static inline bool classof(const Instruction *I) { |
| 158 | return (I->getOpcode() == Instruction::Free); |
| 159 | } |
| 160 | static inline bool classof(const Value *V) { |
| 161 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | |
| 166 | //===----------------------------------------------------------------------===// |
| 167 | // LoadInst Class |
| 168 | //===----------------------------------------------------------------------===// |
| 169 | |
Chris Lattner | 88fe29a | 2005-02-05 01:44:18 +0000 | [diff] [blame^] | 170 | /// LoadInst - an instruction for reading from memory. This uses the |
| 171 | /// SubclassData field in Value to store whether or not the load is volatile. |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 172 | /// |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 173 | class LoadInst : public UnaryInstruction { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 174 | LoadInst(const LoadInst &LI) |
Chris Lattner | 88fe29a | 2005-02-05 01:44:18 +0000 | [diff] [blame^] | 175 | : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) { |
| 176 | setVolatile(LI.isVolatile()); |
| 177 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 178 | #ifndef NDEBUG |
| 179 | AssertOK(); |
| 180 | #endif |
| 181 | } |
| 182 | void AssertOK(); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 183 | public: |
| 184 | LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore); |
| 185 | LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd); |
| 186 | LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false, |
| 187 | Instruction *InsertBefore = 0); |
| 188 | LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 189 | BasicBlock *InsertAtEnd); |
| 190 | |
| 191 | /// isVolatile - Return true if this is a load from a volatile memory |
| 192 | /// location. |
| 193 | /// |
Chris Lattner | 88fe29a | 2005-02-05 01:44:18 +0000 | [diff] [blame^] | 194 | bool isVolatile() const { return SubclassData; } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 195 | |
| 196 | /// setVolatile - Specify whether this is a volatile load or not. |
| 197 | /// |
Chris Lattner | 88fe29a | 2005-02-05 01:44:18 +0000 | [diff] [blame^] | 198 | void setVolatile(bool V) { SubclassData = V; } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 199 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 200 | virtual LoadInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 201 | |
| 202 | virtual bool mayWriteToMemory() const { return isVolatile(); } |
| 203 | |
| 204 | Value *getPointerOperand() { return getOperand(0); } |
| 205 | const Value *getPointerOperand() const { return getOperand(0); } |
| 206 | static unsigned getPointerOperandIndex() { return 0U; } |
| 207 | |
| 208 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 209 | static inline bool classof(const LoadInst *) { return true; } |
| 210 | static inline bool classof(const Instruction *I) { |
| 211 | return I->getOpcode() == Instruction::Load; |
| 212 | } |
| 213 | static inline bool classof(const Value *V) { |
| 214 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | |
| 219 | //===----------------------------------------------------------------------===// |
| 220 | // StoreInst Class |
| 221 | //===----------------------------------------------------------------------===// |
| 222 | |
| 223 | /// StoreInst - an instruction for storing to memory |
| 224 | /// |
| 225 | class StoreInst : public Instruction { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 226 | Use Ops[2]; |
Chris Lattner | 88fe29a | 2005-02-05 01:44:18 +0000 | [diff] [blame^] | 227 | StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 228 | Ops[0].init(SI.Ops[0], this); |
| 229 | Ops[1].init(SI.Ops[1], this); |
Chris Lattner | 88fe29a | 2005-02-05 01:44:18 +0000 | [diff] [blame^] | 230 | setVolatile(SI.isVolatile()); |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 231 | #ifndef NDEBUG |
| 232 | AssertOK(); |
| 233 | #endif |
| 234 | } |
| 235 | void AssertOK(); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 236 | public: |
| 237 | StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore); |
| 238 | StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd); |
| 239 | StoreInst(Value *Val, Value *Ptr, bool isVolatile = false, |
| 240 | Instruction *InsertBefore = 0); |
| 241 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd); |
| 242 | |
| 243 | |
| 244 | /// isVolatile - Return true if this is a load from a volatile memory |
| 245 | /// location. |
| 246 | /// |
Chris Lattner | 88fe29a | 2005-02-05 01:44:18 +0000 | [diff] [blame^] | 247 | bool isVolatile() const { return SubclassData; } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 248 | |
| 249 | /// setVolatile - Specify whether this is a volatile load or not. |
| 250 | /// |
Chris Lattner | 88fe29a | 2005-02-05 01:44:18 +0000 | [diff] [blame^] | 251 | void setVolatile(bool V) { SubclassData = V; } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 253 | /// Transparently provide more efficient getOperand methods. |
| 254 | Value *getOperand(unsigned i) const { |
| 255 | assert(i < 2 && "getOperand() out of range!"); |
| 256 | return Ops[i]; |
| 257 | } |
| 258 | void setOperand(unsigned i, Value *Val) { |
| 259 | assert(i < 2 && "setOperand() out of range!"); |
| 260 | Ops[i] = Val; |
| 261 | } |
| 262 | unsigned getNumOperands() const { return 2; } |
| 263 | |
| 264 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 265 | virtual StoreInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 266 | |
| 267 | virtual bool mayWriteToMemory() const { return true; } |
| 268 | |
| 269 | Value *getPointerOperand() { return getOperand(1); } |
| 270 | const Value *getPointerOperand() const { return getOperand(1); } |
| 271 | static unsigned getPointerOperandIndex() { return 1U; } |
| 272 | |
| 273 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 274 | static inline bool classof(const StoreInst *) { return true; } |
| 275 | static inline bool classof(const Instruction *I) { |
| 276 | return I->getOpcode() == Instruction::Store; |
| 277 | } |
| 278 | static inline bool classof(const Value *V) { |
| 279 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 280 | } |
| 281 | }; |
| 282 | |
| 283 | |
| 284 | //===----------------------------------------------------------------------===// |
| 285 | // GetElementPtrInst Class |
| 286 | //===----------------------------------------------------------------------===// |
| 287 | |
| 288 | /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to |
| 289 | /// access elements of arrays and structs |
| 290 | /// |
| 291 | class GetElementPtrInst : public Instruction { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 292 | GetElementPtrInst(const GetElementPtrInst &GEPI) |
| 293 | : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr, |
| 294 | 0, GEPI.getNumOperands()) { |
| 295 | Use *OL = OperandList = new Use[NumOperands]; |
| 296 | Use *GEPIOL = GEPI.OperandList; |
| 297 | for (unsigned i = 0, E = NumOperands; i != E; ++i) |
| 298 | OL[i].init(GEPIOL[i], this); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 299 | } |
| 300 | void init(Value *Ptr, const std::vector<Value*> &Idx); |
| 301 | void init(Value *Ptr, Value *Idx0, Value *Idx1); |
| 302 | public: |
| 303 | /// Constructors - Create a getelementptr instruction with a base pointer an |
| 304 | /// list of indices. The first ctor can optionally insert before an existing |
| 305 | /// instruction, the second appends the new instruction to the specified |
| 306 | /// BasicBlock. |
| 307 | GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, |
| 308 | const std::string &Name = "", Instruction *InsertBefore =0); |
| 309 | GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, |
| 310 | const std::string &Name, BasicBlock *InsertAtEnd); |
| 311 | |
| 312 | /// Constructors - These two constructors are convenience methods because two |
| 313 | /// index getelementptr instructions are so common. |
| 314 | GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, |
| 315 | const std::string &Name = "", Instruction *InsertBefore =0); |
| 316 | GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, |
| 317 | const std::string &Name, BasicBlock *InsertAtEnd); |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 318 | ~GetElementPtrInst(); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 319 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 320 | virtual GetElementPtrInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 321 | |
| 322 | // getType - Overload to return most specific pointer type... |
| 323 | inline const PointerType *getType() const { |
| 324 | return reinterpret_cast<const PointerType*>(Instruction::getType()); |
| 325 | } |
| 326 | |
| 327 | /// getIndexedType - Returns the type of the element that would be loaded with |
| 328 | /// a load instruction with the specified parameters. |
| 329 | /// |
| 330 | /// A null type is returned if the indices are invalid for the specified |
| 331 | /// pointer type. |
| 332 | /// |
| 333 | static const Type *getIndexedType(const Type *Ptr, |
| 334 | const std::vector<Value*> &Indices, |
| 335 | bool AllowStructLeaf = false); |
| 336 | static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1, |
| 337 | bool AllowStructLeaf = false); |
| 338 | |
| 339 | inline op_iterator idx_begin() { return op_begin()+1; } |
| 340 | inline const_op_iterator idx_begin() const { return op_begin()+1; } |
| 341 | inline op_iterator idx_end() { return op_end(); } |
| 342 | inline const_op_iterator idx_end() const { return op_end(); } |
| 343 | |
| 344 | Value *getPointerOperand() { |
| 345 | return getOperand(0); |
| 346 | } |
| 347 | const Value *getPointerOperand() const { |
| 348 | return getOperand(0); |
| 349 | } |
| 350 | static unsigned getPointerOperandIndex() { |
| 351 | return 0U; // get index for modifying correct operand |
| 352 | } |
| 353 | |
| 354 | inline unsigned getNumIndices() const { // Note: always non-negative |
| 355 | return getNumOperands() - 1; |
| 356 | } |
| 357 | |
| 358 | inline bool hasIndices() const { |
| 359 | return getNumOperands() > 1; |
| 360 | } |
| 361 | |
| 362 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 363 | static inline bool classof(const GetElementPtrInst *) { return true; } |
| 364 | static inline bool classof(const Instruction *I) { |
| 365 | return (I->getOpcode() == Instruction::GetElementPtr); |
| 366 | } |
| 367 | static inline bool classof(const Value *V) { |
| 368 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 369 | } |
| 370 | }; |
| 371 | |
| 372 | //===----------------------------------------------------------------------===// |
| 373 | // SetCondInst Class |
| 374 | //===----------------------------------------------------------------------===// |
| 375 | |
| 376 | /// SetCondInst class - Represent a setCC operator, where CC is eq, ne, lt, gt, |
| 377 | /// le, or ge. |
| 378 | /// |
| 379 | class SetCondInst : public BinaryOperator { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 380 | public: |
| 381 | SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS, |
| 382 | const std::string &Name = "", Instruction *InsertBefore = 0); |
| 383 | SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS, |
| 384 | const std::string &Name, BasicBlock *InsertAtEnd); |
| 385 | |
| 386 | /// getInverseCondition - Return the inverse of the current condition opcode. |
| 387 | /// For example seteq -> setne, setgt -> setle, setlt -> setge, etc... |
| 388 | /// |
| 389 | BinaryOps getInverseCondition() const { |
| 390 | return getInverseCondition(getOpcode()); |
| 391 | } |
| 392 | |
| 393 | /// getInverseCondition - Static version that you can use without an |
| 394 | /// instruction available. |
| 395 | /// |
| 396 | static BinaryOps getInverseCondition(BinaryOps Opcode); |
| 397 | |
| 398 | /// getSwappedCondition - Return the condition opcode that would be the result |
| 399 | /// of exchanging the two operands of the setcc instruction without changing |
| 400 | /// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc. |
| 401 | /// |
| 402 | BinaryOps getSwappedCondition() const { |
| 403 | return getSwappedCondition(getOpcode()); |
| 404 | } |
| 405 | |
| 406 | /// getSwappedCondition - Static version that you can use without an |
| 407 | /// instruction available. |
| 408 | /// |
| 409 | static BinaryOps getSwappedCondition(BinaryOps Opcode); |
| 410 | |
| 411 | |
| 412 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 413 | static inline bool classof(const SetCondInst *) { return true; } |
| 414 | static inline bool classof(const Instruction *I) { |
| 415 | return I->getOpcode() == SetEQ || I->getOpcode() == SetNE || |
| 416 | I->getOpcode() == SetLE || I->getOpcode() == SetGE || |
| 417 | I->getOpcode() == SetLT || I->getOpcode() == SetGT; |
| 418 | } |
| 419 | static inline bool classof(const Value *V) { |
| 420 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 421 | } |
| 422 | }; |
| 423 | |
| 424 | //===----------------------------------------------------------------------===// |
| 425 | // CastInst Class |
| 426 | //===----------------------------------------------------------------------===// |
| 427 | |
| 428 | /// CastInst - This class represents a cast from Operand[0] to the type of |
| 429 | /// the instruction (i->getType()). |
| 430 | /// |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 431 | class CastInst : public UnaryInstruction { |
| 432 | CastInst(const CastInst &CI) |
| 433 | : UnaryInstruction(CI.getType(), Cast, CI.getOperand(0)) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 434 | } |
| 435 | public: |
| 436 | CastInst(Value *S, const Type *Ty, const std::string &Name = "", |
| 437 | Instruction *InsertBefore = 0) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 438 | : UnaryInstruction(Ty, Cast, S, Name, InsertBefore) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 439 | } |
| 440 | CastInst(Value *S, const Type *Ty, const std::string &Name, |
| 441 | BasicBlock *InsertAtEnd) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 442 | : UnaryInstruction(Ty, Cast, S, Name, InsertAtEnd) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 445 | virtual CastInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 446 | |
| 447 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 448 | static inline bool classof(const CastInst *) { return true; } |
| 449 | static inline bool classof(const Instruction *I) { |
| 450 | return I->getOpcode() == Cast; |
| 451 | } |
| 452 | static inline bool classof(const Value *V) { |
| 453 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 454 | } |
| 455 | }; |
| 456 | |
| 457 | |
| 458 | //===----------------------------------------------------------------------===// |
| 459 | // CallInst Class |
| 460 | //===----------------------------------------------------------------------===// |
| 461 | |
| 462 | /// CallInst - This class represents a function call, abstracting a target |
| 463 | /// machine's calling convention. |
| 464 | /// |
| 465 | class CallInst : public Instruction { |
| 466 | CallInst(const CallInst &CI); |
| 467 | void init(Value *Func, const std::vector<Value*> &Params); |
| 468 | void init(Value *Func, Value *Actual1, Value *Actual2); |
| 469 | void init(Value *Func, Value *Actual); |
| 470 | void init(Value *Func); |
| 471 | |
| 472 | public: |
| 473 | CallInst(Value *F, const std::vector<Value*> &Par, |
| 474 | const std::string &Name = "", Instruction *InsertBefore = 0); |
| 475 | CallInst(Value *F, const std::vector<Value*> &Par, |
| 476 | const std::string &Name, BasicBlock *InsertAtEnd); |
| 477 | |
| 478 | // Alternate CallInst ctors w/ two actuals, w/ one actual and no |
| 479 | // actuals, respectively. |
| 480 | CallInst(Value *F, Value *Actual1, Value *Actual2, |
| 481 | const std::string& Name = "", Instruction *InsertBefore = 0); |
| 482 | CallInst(Value *F, Value *Actual1, Value *Actual2, |
| 483 | const std::string& Name, BasicBlock *InsertAtEnd); |
| 484 | CallInst(Value *F, Value *Actual, const std::string& Name = "", |
| 485 | Instruction *InsertBefore = 0); |
| 486 | CallInst(Value *F, Value *Actual, const std::string& Name, |
| 487 | BasicBlock *InsertAtEnd); |
| 488 | explicit CallInst(Value *F, const std::string &Name = "", |
| 489 | Instruction *InsertBefore = 0); |
| 490 | explicit CallInst(Value *F, const std::string &Name, |
| 491 | BasicBlock *InsertAtEnd); |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 492 | ~CallInst(); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 493 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 494 | virtual CallInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 495 | bool mayWriteToMemory() const { return true; } |
| 496 | |
Chris Lattner | 721aef6 | 2004-11-18 17:46:57 +0000 | [diff] [blame] | 497 | /// getCalledFunction - Return the function being called by this instruction |
| 498 | /// if it is a direct call. If it is a call through a function pointer, |
| 499 | /// return null. |
| 500 | Function *getCalledFunction() const { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 501 | return (Function*)dyn_cast<Function>(getOperand(0)); |
Chris Lattner | 721aef6 | 2004-11-18 17:46:57 +0000 | [diff] [blame] | 502 | } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 503 | |
| 504 | // getCalledValue - Get a pointer to a method that is invoked by this inst. |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 505 | inline const Value *getCalledValue() const { return getOperand(0); } |
| 506 | inline Value *getCalledValue() { return getOperand(0); } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 507 | |
| 508 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 509 | static inline bool classof(const CallInst *) { return true; } |
| 510 | static inline bool classof(const Instruction *I) { |
| 511 | return I->getOpcode() == Instruction::Call; |
| 512 | } |
| 513 | static inline bool classof(const Value *V) { |
| 514 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 515 | } |
| 516 | }; |
| 517 | |
| 518 | |
| 519 | //===----------------------------------------------------------------------===// |
| 520 | // ShiftInst Class |
| 521 | //===----------------------------------------------------------------------===// |
| 522 | |
| 523 | /// ShiftInst - This class represents left and right shift instructions. |
| 524 | /// |
| 525 | class ShiftInst : public Instruction { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 526 | Use Ops[2]; |
| 527 | ShiftInst(const ShiftInst &SI) |
| 528 | : Instruction(SI.getType(), SI.getOpcode(), Ops, 2) { |
| 529 | Ops[0].init(SI.Ops[0], this); |
| 530 | Ops[1].init(SI.Ops[1], this); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 531 | } |
| 532 | void init(OtherOps Opcode, Value *S, Value *SA) { |
| 533 | assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!"); |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 534 | Ops[0].init(S, this); |
| 535 | Ops[1].init(SA, this); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | public: |
| 539 | ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "", |
| 540 | Instruction *InsertBefore = 0) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 541 | : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertBefore) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 542 | init(Opcode, S, SA); |
| 543 | } |
| 544 | ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name, |
| 545 | BasicBlock *InsertAtEnd) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 546 | : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertAtEnd) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 547 | init(Opcode, S, SA); |
| 548 | } |
| 549 | |
| 550 | OtherOps getOpcode() const { |
| 551 | return static_cast<OtherOps>(Instruction::getOpcode()); |
| 552 | } |
| 553 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 554 | /// Transparently provide more efficient getOperand methods. |
| 555 | Value *getOperand(unsigned i) const { |
| 556 | assert(i < 2 && "getOperand() out of range!"); |
| 557 | return Ops[i]; |
| 558 | } |
| 559 | void setOperand(unsigned i, Value *Val) { |
| 560 | assert(i < 2 && "setOperand() out of range!"); |
| 561 | Ops[i] = Val; |
| 562 | } |
| 563 | unsigned getNumOperands() const { return 2; } |
| 564 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 565 | virtual ShiftInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 566 | |
| 567 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 568 | static inline bool classof(const ShiftInst *) { return true; } |
| 569 | static inline bool classof(const Instruction *I) { |
| 570 | return (I->getOpcode() == Instruction::Shr) | |
| 571 | (I->getOpcode() == Instruction::Shl); |
| 572 | } |
| 573 | static inline bool classof(const Value *V) { |
| 574 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 575 | } |
| 576 | }; |
| 577 | |
| 578 | //===----------------------------------------------------------------------===// |
| 579 | // SelectInst Class |
| 580 | //===----------------------------------------------------------------------===// |
| 581 | |
| 582 | /// SelectInst - This class represents the LLVM 'select' instruction. |
| 583 | /// |
| 584 | class SelectInst : public Instruction { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 585 | Use Ops[3]; |
| 586 | |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 587 | void init(Value *C, Value *S1, Value *S2) { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 588 | Ops[0].init(C, this); |
| 589 | Ops[1].init(S1, this); |
| 590 | Ops[2].init(S2, this); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 593 | SelectInst(const SelectInst &SI) |
| 594 | : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) { |
| 595 | init(SI.Ops[0], SI.Ops[1], SI.Ops[2]); |
| 596 | } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 597 | public: |
| 598 | SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "", |
| 599 | Instruction *InsertBefore = 0) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 600 | : Instruction(S1->getType(), Instruction::Select, Ops, 3, |
| 601 | Name, InsertBefore) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 602 | init(C, S1, S2); |
| 603 | } |
| 604 | SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name, |
| 605 | BasicBlock *InsertAtEnd) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 606 | : Instruction(S1->getType(), Instruction::Select, Ops, 3, |
| 607 | Name, InsertAtEnd) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 608 | init(C, S1, S2); |
| 609 | } |
| 610 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 611 | Value *getCondition() const { return Ops[0]; } |
| 612 | Value *getTrueValue() const { return Ops[1]; } |
| 613 | Value *getFalseValue() const { return Ops[2]; } |
| 614 | |
| 615 | /// Transparently provide more efficient getOperand methods. |
| 616 | Value *getOperand(unsigned i) const { |
| 617 | assert(i < 3 && "getOperand() out of range!"); |
| 618 | return Ops[i]; |
| 619 | } |
| 620 | void setOperand(unsigned i, Value *Val) { |
| 621 | assert(i < 3 && "setOperand() out of range!"); |
| 622 | Ops[i] = Val; |
| 623 | } |
| 624 | unsigned getNumOperands() const { return 3; } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 625 | |
| 626 | OtherOps getOpcode() const { |
| 627 | return static_cast<OtherOps>(Instruction::getOpcode()); |
| 628 | } |
| 629 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 630 | virtual SelectInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 631 | |
| 632 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 633 | static inline bool classof(const SelectInst *) { return true; } |
| 634 | static inline bool classof(const Instruction *I) { |
| 635 | return I->getOpcode() == Instruction::Select; |
| 636 | } |
| 637 | static inline bool classof(const Value *V) { |
| 638 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 639 | } |
| 640 | }; |
| 641 | |
| 642 | |
| 643 | //===----------------------------------------------------------------------===// |
| 644 | // VANextInst Class |
| 645 | //===----------------------------------------------------------------------===// |
| 646 | |
| 647 | /// VANextInst - This class represents the va_next llvm instruction, which |
| 648 | /// advances a vararg list passed an argument of the specified type, returning |
| 649 | /// the resultant list. |
| 650 | /// |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 651 | class VANextInst : public UnaryInstruction { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 652 | PATypeHolder ArgTy; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 653 | VANextInst(const VANextInst &VAN) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 654 | : UnaryInstruction(VAN.getType(), VANext, VAN.getOperand(0)), |
| 655 | ArgTy(VAN.getArgType()) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | public: |
| 659 | VANextInst(Value *List, const Type *Ty, const std::string &Name = "", |
| 660 | Instruction *InsertBefore = 0) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 661 | : UnaryInstruction(List->getType(), VANext, List, Name, InsertBefore), |
| 662 | ArgTy(Ty) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 663 | } |
| 664 | VANextInst(Value *List, const Type *Ty, const std::string &Name, |
| 665 | BasicBlock *InsertAtEnd) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 666 | : UnaryInstruction(List->getType(), VANext, List, Name, InsertAtEnd), |
| 667 | ArgTy(Ty) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | const Type *getArgType() const { return ArgTy; } |
| 671 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 672 | virtual VANextInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 673 | |
| 674 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 675 | static inline bool classof(const VANextInst *) { return true; } |
| 676 | static inline bool classof(const Instruction *I) { |
| 677 | return I->getOpcode() == VANext; |
| 678 | } |
| 679 | static inline bool classof(const Value *V) { |
| 680 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 681 | } |
| 682 | }; |
| 683 | |
| 684 | |
| 685 | //===----------------------------------------------------------------------===// |
| 686 | // VAArgInst Class |
| 687 | //===----------------------------------------------------------------------===// |
| 688 | |
| 689 | /// VAArgInst - This class represents the va_arg llvm instruction, which returns |
| 690 | /// an argument of the specified type given a va_list. |
| 691 | /// |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 692 | class VAArgInst : public UnaryInstruction { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 693 | VAArgInst(const VAArgInst &VAA) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 694 | : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {} |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 695 | public: |
| 696 | VAArgInst(Value *List, const Type *Ty, const std::string &Name = "", |
| 697 | Instruction *InsertBefore = 0) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 698 | : UnaryInstruction(Ty, VAArg, List, Name, InsertBefore) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 699 | } |
| 700 | VAArgInst(Value *List, const Type *Ty, const std::string &Name, |
| 701 | BasicBlock *InsertAtEnd) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 702 | : UnaryInstruction(Ty, VAArg, List, Name, InsertAtEnd) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 705 | virtual VAArgInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 706 | |
| 707 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 708 | static inline bool classof(const VAArgInst *) { return true; } |
| 709 | static inline bool classof(const Instruction *I) { |
| 710 | return I->getOpcode() == VAArg; |
| 711 | } |
| 712 | static inline bool classof(const Value *V) { |
| 713 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 714 | } |
| 715 | }; |
| 716 | |
| 717 | //===----------------------------------------------------------------------===// |
| 718 | // PHINode Class |
| 719 | //===----------------------------------------------------------------------===// |
| 720 | |
| 721 | // PHINode - The PHINode class is used to represent the magical mystical PHI |
| 722 | // node, that can not exist in nature, but can be synthesized in a computer |
| 723 | // scientist's overactive imagination. |
| 724 | // |
| 725 | class PHINode : public Instruction { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 726 | /// ReservedSpace - The number of operands actually allocated. NumOperands is |
| 727 | /// the number actually in use. |
| 728 | unsigned ReservedSpace; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 729 | PHINode(const PHINode &PN); |
| 730 | public: |
| 731 | PHINode(const Type *Ty, const std::string &Name = "", |
| 732 | Instruction *InsertBefore = 0) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 733 | : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertBefore), |
| 734 | ReservedSpace(0) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 738 | : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertAtEnd), |
| 739 | ReservedSpace(0) { |
| 740 | } |
| 741 | |
| 742 | ~PHINode(); |
| 743 | |
| 744 | /// reserveOperandSpace - This method can be used to avoid repeated |
| 745 | /// reallocation of PHI operand lists by reserving space for the correct |
| 746 | /// number of operands before adding them. Unlike normal vector reserves, |
| 747 | /// this method can also be used to trim the operand space. |
| 748 | void reserveOperandSpace(unsigned NumValues) { |
| 749 | resizeOperands(NumValues*2); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 752 | virtual PHINode *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 753 | |
| 754 | /// getNumIncomingValues - Return the number of incoming edges |
| 755 | /// |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 756 | unsigned getNumIncomingValues() const { return getNumOperands()/2; } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 757 | |
| 758 | /// getIncomingValue - Return incoming value #x |
| 759 | /// |
| 760 | Value *getIncomingValue(unsigned i) const { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 761 | assert(i*2 < getNumOperands() && "Invalid value number!"); |
| 762 | return getOperand(i*2); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 763 | } |
| 764 | void setIncomingValue(unsigned i, Value *V) { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 765 | assert(i*2 < getNumOperands() && "Invalid value number!"); |
| 766 | setOperand(i*2, V); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 767 | } |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 768 | unsigned getOperandNumForIncomingValue(unsigned i) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 769 | return i*2; |
| 770 | } |
| 771 | |
| 772 | /// getIncomingBlock - Return incoming basic block #x |
| 773 | /// |
| 774 | BasicBlock *getIncomingBlock(unsigned i) const { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 775 | return reinterpret_cast<BasicBlock*>(getOperand(i*2+1)); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 776 | } |
| 777 | void setIncomingBlock(unsigned i, BasicBlock *BB) { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 778 | setOperand(i*2+1, reinterpret_cast<Value*>(BB)); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 779 | } |
| 780 | unsigned getOperandNumForIncomingBlock(unsigned i) { |
| 781 | return i*2+1; |
| 782 | } |
| 783 | |
| 784 | /// addIncoming - Add an incoming value to the end of the PHI list |
| 785 | /// |
| 786 | void addIncoming(Value *V, BasicBlock *BB) { |
| 787 | assert(getType() == V->getType() && |
| 788 | "All operands to PHI node must be the same type as the PHI node!"); |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 789 | unsigned OpNo = NumOperands; |
| 790 | if (OpNo+2 > ReservedSpace) |
| 791 | resizeOperands(0); // Get more space! |
| 792 | // Initialize some new operands. |
| 793 | NumOperands = OpNo+2; |
| 794 | OperandList[OpNo].init(V, this); |
| 795 | OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /// removeIncomingValue - Remove an incoming value. This is useful if a |
| 799 | /// predecessor basic block is deleted. The value removed is returned. |
| 800 | /// |
| 801 | /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty |
| 802 | /// is true), the PHI node is destroyed and any uses of it are replaced with |
| 803 | /// dummy values. The only time there should be zero incoming values to a PHI |
| 804 | /// node is when the block is dead, so this strategy is sound. |
| 805 | /// |
| 806 | Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true); |
| 807 | |
| 808 | Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){ |
| 809 | int Idx = getBasicBlockIndex(BB); |
| 810 | assert(Idx >= 0 && "Invalid basic block argument to remove!"); |
| 811 | return removeIncomingValue(Idx, DeletePHIIfEmpty); |
| 812 | } |
| 813 | |
| 814 | /// getBasicBlockIndex - Return the first index of the specified basic |
| 815 | /// block in the value list for this PHI. Returns -1 if no instance. |
| 816 | /// |
| 817 | int getBasicBlockIndex(const BasicBlock *BB) const { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 818 | Use *OL = OperandList; |
| 819 | for (unsigned i = 0, e = getNumOperands(); i != e; i += 2) |
| 820 | if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 821 | return -1; |
| 822 | } |
| 823 | |
| 824 | Value *getIncomingValueForBlock(const BasicBlock *BB) const { |
| 825 | return getIncomingValue(getBasicBlockIndex(BB)); |
| 826 | } |
| 827 | |
| 828 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 829 | static inline bool classof(const PHINode *) { return true; } |
| 830 | static inline bool classof(const Instruction *I) { |
| 831 | return I->getOpcode() == Instruction::PHI; |
| 832 | } |
| 833 | static inline bool classof(const Value *V) { |
| 834 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 835 | } |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 836 | private: |
| 837 | void resizeOperands(unsigned NumOperands); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 838 | }; |
| 839 | |
| 840 | //===----------------------------------------------------------------------===// |
| 841 | // ReturnInst Class |
| 842 | //===----------------------------------------------------------------------===// |
| 843 | |
| 844 | //===--------------------------------------------------------------------------- |
| 845 | /// ReturnInst - Return a value (possibly void), from a function. Execution |
| 846 | /// does not continue in this function any longer. |
| 847 | /// |
| 848 | class ReturnInst : public TerminatorInst { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 849 | Use RetVal; // Possibly null retval. |
| 850 | ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret, &RetVal, |
| 851 | RI.getNumOperands()) { |
| 852 | if (RI.getNumOperands()) |
| 853 | RetVal.init(RI.RetVal, this); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Alkis Evlogimenos | 859804f | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 856 | void init(Value *RetVal); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 857 | |
| 858 | public: |
| 859 | // ReturnInst constructors: |
| 860 | // ReturnInst() - 'ret void' instruction |
Alkis Evlogimenos | 859804f | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 861 | // ReturnInst( null) - 'ret void' instruction |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 862 | // ReturnInst(Value* X) - 'ret X' instruction |
| 863 | // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I |
| 864 | // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I |
| 865 | // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB |
| 866 | // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB |
Alkis Evlogimenos | 859804f | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 867 | // |
| 868 | // NOTE: If the Value* passed is of type void then the constructor behaves as |
| 869 | // if it was passed NULL. |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 870 | ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0) |
| 871 | : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertBefore) { |
| 872 | init(retVal); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 873 | } |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 874 | ReturnInst(Value *retVal, BasicBlock *InsertAtEnd) |
| 875 | : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) { |
| 876 | init(retVal); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 877 | } |
| 878 | ReturnInst(BasicBlock *InsertAtEnd) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 879 | : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 882 | virtual ReturnInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 883 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 884 | // Transparently provide more efficient getOperand methods. |
| 885 | Value *getOperand(unsigned i) const { |
| 886 | assert(i < getNumOperands() && "getOperand() out of range!"); |
| 887 | return RetVal; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 888 | } |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 889 | void setOperand(unsigned i, Value *Val) { |
| 890 | assert(i < getNumOperands() && "setOperand() out of range!"); |
| 891 | RetVal = Val; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 894 | Value *getReturnValue() const { return RetVal; } |
| 895 | |
| 896 | unsigned getNumSuccessors() const { return 0; } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 897 | |
| 898 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 899 | static inline bool classof(const ReturnInst *) { return true; } |
| 900 | static inline bool classof(const Instruction *I) { |
| 901 | return (I->getOpcode() == Instruction::Ret); |
| 902 | } |
| 903 | static inline bool classof(const Value *V) { |
| 904 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 905 | } |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 906 | private: |
| 907 | virtual BasicBlock *getSuccessorV(unsigned idx) const; |
| 908 | virtual unsigned getNumSuccessorsV() const; |
| 909 | virtual void setSuccessorV(unsigned idx, BasicBlock *B); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 910 | }; |
| 911 | |
| 912 | //===----------------------------------------------------------------------===// |
| 913 | // BranchInst Class |
| 914 | //===----------------------------------------------------------------------===// |
| 915 | |
| 916 | //===--------------------------------------------------------------------------- |
| 917 | /// BranchInst - Conditional or Unconditional Branch instruction. |
| 918 | /// |
| 919 | class BranchInst : public TerminatorInst { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 920 | /// Ops list - Branches are strange. The operands are ordered: |
| 921 | /// TrueDest, FalseDest, Cond. This makes some accessors faster because |
| 922 | /// they don't have to check for cond/uncond branchness. |
| 923 | Use Ops[3]; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 924 | BranchInst(const BranchInst &BI); |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 925 | void AssertOK(); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 926 | public: |
| 927 | // BranchInst constructors (where {B, T, F} are blocks, and C is a condition): |
| 928 | // BranchInst(BB *B) - 'br B' |
| 929 | // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F' |
| 930 | // BranchInst(BB* B, Inst *I) - 'br B' insert before I |
| 931 | // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I |
| 932 | // BranchInst(BB* B, BB *I) - 'br B' insert at end |
| 933 | // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end |
| 934 | BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 935 | : TerminatorInst(Instruction::Br, Ops, 1, InsertBefore) { |
| 936 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
| 937 | Ops[0].init(reinterpret_cast<Value*>(IfTrue), this); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 938 | } |
| 939 | BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 940 | Instruction *InsertBefore = 0) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 941 | : TerminatorInst(Instruction::Br, Ops, 3, InsertBefore) { |
| 942 | Ops[0].init(reinterpret_cast<Value*>(IfTrue), this); |
| 943 | Ops[1].init(reinterpret_cast<Value*>(IfFalse), this); |
| 944 | Ops[2].init(Cond, this); |
| 945 | #ifndef NDEBUG |
| 946 | AssertOK(); |
| 947 | #endif |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 951 | : TerminatorInst(Instruction::Br, Ops, 1, InsertAtEnd) { |
| 952 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
| 953 | Ops[0].init(reinterpret_cast<Value*>(IfTrue), this); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 957 | BasicBlock *InsertAtEnd) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 958 | : TerminatorInst(Instruction::Br, Ops, 3, InsertAtEnd) { |
| 959 | Ops[0].init(reinterpret_cast<Value*>(IfTrue), this); |
| 960 | Ops[1].init(reinterpret_cast<Value*>(IfFalse), this); |
| 961 | Ops[2].init(Cond, this); |
| 962 | #ifndef NDEBUG |
| 963 | AssertOK(); |
| 964 | #endif |
| 965 | } |
| 966 | |
| 967 | |
| 968 | /// Transparently provide more efficient getOperand methods. |
| 969 | Value *getOperand(unsigned i) const { |
| 970 | assert(i < getNumOperands() && "getOperand() out of range!"); |
| 971 | return Ops[i]; |
| 972 | } |
| 973 | void setOperand(unsigned i, Value *Val) { |
| 974 | assert(i < getNumOperands() && "setOperand() out of range!"); |
| 975 | Ops[i] = Val; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 978 | virtual BranchInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 979 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 980 | inline bool isUnconditional() const { return getNumOperands() == 1; } |
| 981 | inline bool isConditional() const { return getNumOperands() == 3; } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 982 | |
| 983 | inline Value *getCondition() const { |
| 984 | assert(isConditional() && "Cannot get condition of an uncond branch!"); |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 985 | return getOperand(2); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | void setCondition(Value *V) { |
| 989 | assert(isConditional() && "Cannot set condition of unconditional branch!"); |
| 990 | setOperand(2, V); |
| 991 | } |
| 992 | |
| 993 | // setUnconditionalDest - Change the current branch to an unconditional branch |
| 994 | // targeting the specified block. |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 995 | // FIXME: Eliminate this ugly method. |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 996 | void setUnconditionalDest(BasicBlock *Dest) { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 997 | if (isConditional()) { // Convert this to an uncond branch. |
| 998 | NumOperands = 1; |
| 999 | Ops[1].set(0); |
| 1000 | Ops[2].set(0); |
| 1001 | } |
| 1002 | setOperand(0, reinterpret_cast<Value*>(Dest)); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1005 | unsigned getNumSuccessors() const { return 1+isConditional(); } |
| 1006 | |
| 1007 | BasicBlock *getSuccessor(unsigned i) const { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1008 | assert(i < getNumSuccessors() && "Successor # out of range for Branch!"); |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1009 | return (i == 0) ? cast<BasicBlock>(getOperand(0)) : |
| 1010 | cast<BasicBlock>(getOperand(1)); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1013 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1014 | assert(idx < getNumSuccessors() && "Successor # out of range for Branch!"); |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1015 | setOperand(idx, reinterpret_cast<Value*>(NewSucc)); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1018 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 1019 | static inline bool classof(const BranchInst *) { return true; } |
| 1020 | static inline bool classof(const Instruction *I) { |
| 1021 | return (I->getOpcode() == Instruction::Br); |
| 1022 | } |
| 1023 | static inline bool classof(const Value *V) { |
| 1024 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 1025 | } |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1026 | private: |
| 1027 | virtual BasicBlock *getSuccessorV(unsigned idx) const; |
| 1028 | virtual unsigned getNumSuccessorsV() const; |
| 1029 | virtual void setSuccessorV(unsigned idx, BasicBlock *B); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1030 | }; |
| 1031 | |
| 1032 | //===----------------------------------------------------------------------===// |
| 1033 | // SwitchInst Class |
| 1034 | //===----------------------------------------------------------------------===// |
| 1035 | |
| 1036 | //===--------------------------------------------------------------------------- |
| 1037 | /// SwitchInst - Multiway switch |
| 1038 | /// |
| 1039 | class SwitchInst : public TerminatorInst { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1040 | unsigned ReservedSpace; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1041 | // Operand[0] = Value to switch on |
| 1042 | // Operand[1] = Default basic block destination |
| 1043 | // Operand[2n ] = Value to match |
| 1044 | // Operand[2n+1] = BasicBlock to go to on match |
| 1045 | SwitchInst(const SwitchInst &RI); |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1046 | void init(Value *Value, BasicBlock *Default, unsigned NumCases); |
| 1047 | void resizeOperands(unsigned No); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1048 | public: |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1049 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 1050 | /// switch on and a default destination. The number of additional cases can |
| 1051 | /// be specified here to make memory allocation more efficient. This |
| 1052 | /// constructor can also autoinsert before another instruction. |
| 1053 | SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 1054 | Instruction *InsertBefore = 0) |
| 1055 | : TerminatorInst(Instruction::Switch, 0, 0, InsertBefore) { |
| 1056 | init(Value, Default, NumCases); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1059 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 1060 | /// switch on and a default destination. The number of additional cases can |
| 1061 | /// be specified here to make memory allocation more efficient. This |
| 1062 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 1063 | SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 1064 | BasicBlock *InsertAtEnd) |
| 1065 | : TerminatorInst(Instruction::Switch, 0, 0, InsertAtEnd) { |
| 1066 | init(Value, Default, NumCases); |
| 1067 | } |
| 1068 | ~SwitchInst(); |
| 1069 | |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1070 | |
| 1071 | // Accessor Methods for Switch stmt |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1072 | inline Value *getCondition() const { return getOperand(0); } |
| 1073 | void setCondition(Value *V) { setOperand(0, V); } |
Chris Lattner | bfaf88a | 2004-12-10 20:35:47 +0000 | [diff] [blame] | 1074 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1075 | inline BasicBlock *getDefaultDest() const { |
| 1076 | return cast<BasicBlock>(getOperand(1)); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /// getNumCases - return the number of 'cases' in this switch instruction. |
| 1080 | /// Note that case #0 is always the default case. |
| 1081 | unsigned getNumCases() const { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1082 | return getNumOperands()/2; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | /// getCaseValue - Return the specified case value. Note that case #0, the |
| 1086 | /// default destination, does not have a case value. |
| 1087 | Constant *getCaseValue(unsigned i) { |
| 1088 | assert(i && i < getNumCases() && "Illegal case value to get!"); |
| 1089 | return getSuccessorValue(i); |
| 1090 | } |
| 1091 | |
| 1092 | /// getCaseValue - Return the specified case value. Note that case #0, the |
| 1093 | /// default destination, does not have a case value. |
| 1094 | const Constant *getCaseValue(unsigned i) const { |
| 1095 | assert(i && i < getNumCases() && "Illegal case value to get!"); |
| 1096 | return getSuccessorValue(i); |
| 1097 | } |
| 1098 | |
| 1099 | /// findCaseValue - Search all of the case values for the specified constant. |
| 1100 | /// If it is explicitly handled, return the case number of it, otherwise |
| 1101 | /// return 0 to indicate that it is handled by the default handler. |
| 1102 | unsigned findCaseValue(const Constant *C) const { |
| 1103 | for (unsigned i = 1, e = getNumCases(); i != e; ++i) |
| 1104 | if (getCaseValue(i) == C) |
| 1105 | return i; |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
| 1109 | /// addCase - Add an entry to the switch instruction... |
| 1110 | /// |
| 1111 | void addCase(Constant *OnVal, BasicBlock *Dest); |
| 1112 | |
| 1113 | /// removeCase - This method removes the specified successor from the switch |
| 1114 | /// instruction. Note that this cannot be used to remove the default |
| 1115 | /// destination (successor #0). |
| 1116 | /// |
| 1117 | void removeCase(unsigned idx); |
| 1118 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1119 | virtual SwitchInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1120 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1121 | unsigned getNumSuccessors() const { return getNumOperands()/2; } |
| 1122 | BasicBlock *getSuccessor(unsigned idx) const { |
| 1123 | assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!"); |
| 1124 | return cast<BasicBlock>(getOperand(idx*2+1)); |
| 1125 | } |
| 1126 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1127 | assert(idx < getNumSuccessors() && "Successor # out of range for switch!"); |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1128 | setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc)); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | // getSuccessorValue - Return the value associated with the specified |
| 1132 | // successor. |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1133 | inline Constant *getSuccessorValue(unsigned idx) const { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1134 | assert(idx < getNumSuccessors() && "Successor # out of range!"); |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1135 | return cast<Constant>(getOperand(idx*2)); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1136 | } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1137 | |
| 1138 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 1139 | static inline bool classof(const SwitchInst *) { return true; } |
| 1140 | static inline bool classof(const Instruction *I) { |
| 1141 | return (I->getOpcode() == Instruction::Switch); |
| 1142 | } |
| 1143 | static inline bool classof(const Value *V) { |
| 1144 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 1145 | } |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1146 | private: |
| 1147 | virtual BasicBlock *getSuccessorV(unsigned idx) const; |
| 1148 | virtual unsigned getNumSuccessorsV() const; |
| 1149 | virtual void setSuccessorV(unsigned idx, BasicBlock *B); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1150 | }; |
| 1151 | |
| 1152 | //===----------------------------------------------------------------------===// |
| 1153 | // InvokeInst Class |
| 1154 | //===----------------------------------------------------------------------===// |
| 1155 | |
| 1156 | //===--------------------------------------------------------------------------- |
| 1157 | /// InvokeInst - Invoke instruction |
| 1158 | /// |
| 1159 | class InvokeInst : public TerminatorInst { |
| 1160 | InvokeInst(const InvokeInst &BI); |
| 1161 | void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
| 1162 | const std::vector<Value*> &Params); |
| 1163 | public: |
| 1164 | InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
| 1165 | const std::vector<Value*> &Params, const std::string &Name = "", |
| 1166 | Instruction *InsertBefore = 0); |
| 1167 | InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
| 1168 | const std::vector<Value*> &Params, const std::string &Name, |
| 1169 | BasicBlock *InsertAtEnd); |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1170 | ~InvokeInst(); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1171 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 1172 | virtual InvokeInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1173 | |
| 1174 | bool mayWriteToMemory() const { return true; } |
| 1175 | |
| 1176 | /// getCalledFunction - Return the function called, or null if this is an |
Chris Lattner | 721aef6 | 2004-11-18 17:46:57 +0000 | [diff] [blame] | 1177 | /// indirect function invocation. |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1178 | /// |
Chris Lattner | 721aef6 | 2004-11-18 17:46:57 +0000 | [diff] [blame] | 1179 | Function *getCalledFunction() const { |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1180 | return dyn_cast<Function>(getOperand(0)); |
Chris Lattner | 721aef6 | 2004-11-18 17:46:57 +0000 | [diff] [blame] | 1181 | } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1182 | |
| 1183 | // getCalledValue - Get a pointer to a function that is invoked by this inst. |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1184 | inline Value *getCalledValue() const { return getOperand(0); } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1185 | |
| 1186 | // get*Dest - Return the destination basic blocks... |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1187 | BasicBlock *getNormalDest() const { |
| 1188 | return cast<BasicBlock>(getOperand(1)); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1189 | } |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1190 | BasicBlock *getUnwindDest() const { |
| 1191 | return cast<BasicBlock>(getOperand(2)); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1192 | } |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1193 | void setNormalDest(BasicBlock *B) { |
| 1194 | setOperand(1, reinterpret_cast<Value*>(B)); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1197 | void setUnwindDest(BasicBlock *B) { |
| 1198 | setOperand(2, reinterpret_cast<Value*>(B)); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1201 | inline BasicBlock *getSuccessor(unsigned i) const { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1202 | assert(i < 2 && "Successor # out of range for invoke!"); |
| 1203 | return i == 0 ? getNormalDest() : getUnwindDest(); |
| 1204 | } |
| 1205 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1206 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1207 | assert(idx < 2 && "Successor # out of range for invoke!"); |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1208 | setOperand(idx+1, reinterpret_cast<Value*>(NewSucc)); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1211 | unsigned getNumSuccessors() const { return 2; } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1212 | |
| 1213 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 1214 | static inline bool classof(const InvokeInst *) { return true; } |
| 1215 | static inline bool classof(const Instruction *I) { |
| 1216 | return (I->getOpcode() == Instruction::Invoke); |
| 1217 | } |
| 1218 | static inline bool classof(const Value *V) { |
| 1219 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 1220 | } |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1221 | private: |
| 1222 | virtual BasicBlock *getSuccessorV(unsigned idx) const; |
| 1223 | virtual unsigned getNumSuccessorsV() const; |
| 1224 | virtual void setSuccessorV(unsigned idx, BasicBlock *B); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1225 | }; |
| 1226 | |
| 1227 | |
| 1228 | //===----------------------------------------------------------------------===// |
| 1229 | // UnwindInst Class |
| 1230 | //===----------------------------------------------------------------------===// |
| 1231 | |
| 1232 | //===--------------------------------------------------------------------------- |
| 1233 | /// UnwindInst - Immediately exit the current function, unwinding the stack |
| 1234 | /// until an invoke instruction is found. |
| 1235 | /// |
Chris Lattner | 1fca5ff | 2004-10-27 16:14:51 +0000 | [diff] [blame] | 1236 | class UnwindInst : public TerminatorInst { |
| 1237 | public: |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1238 | UnwindInst(Instruction *InsertBefore = 0) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1239 | : TerminatorInst(Instruction::Unwind, 0, 0, InsertBefore) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1240 | } |
| 1241 | UnwindInst(BasicBlock *InsertAtEnd) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1242 | : TerminatorInst(Instruction::Unwind, 0, 0, InsertAtEnd) { |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Chris Lattner | f319e83 | 2004-10-15 23:52:05 +0000 | [diff] [blame] | 1245 | virtual UnwindInst *clone() const; |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1246 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1247 | unsigned getNumSuccessors() const { return 0; } |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1248 | |
| 1249 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 1250 | static inline bool classof(const UnwindInst *) { return true; } |
| 1251 | static inline bool classof(const Instruction *I) { |
| 1252 | return I->getOpcode() == Instruction::Unwind; |
| 1253 | } |
| 1254 | static inline bool classof(const Value *V) { |
| 1255 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 1256 | } |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1257 | private: |
| 1258 | virtual BasicBlock *getSuccessorV(unsigned idx) const; |
| 1259 | virtual unsigned getNumSuccessorsV() const; |
| 1260 | virtual void setSuccessorV(unsigned idx, BasicBlock *B); |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1261 | }; |
| 1262 | |
Chris Lattner | 076b3f1 | 2004-10-16 18:05:54 +0000 | [diff] [blame] | 1263 | //===----------------------------------------------------------------------===// |
| 1264 | // UnreachableInst Class |
| 1265 | //===----------------------------------------------------------------------===// |
| 1266 | |
| 1267 | //===--------------------------------------------------------------------------- |
| 1268 | /// UnreachableInst - This function has undefined behavior. In particular, the |
| 1269 | /// presence of this instruction indicates some higher level knowledge that the |
| 1270 | /// end of the block cannot be reached. |
| 1271 | /// |
Chris Lattner | 1fca5ff | 2004-10-27 16:14:51 +0000 | [diff] [blame] | 1272 | class UnreachableInst : public TerminatorInst { |
| 1273 | public: |
Chris Lattner | 076b3f1 | 2004-10-16 18:05:54 +0000 | [diff] [blame] | 1274 | UnreachableInst(Instruction *InsertBefore = 0) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1275 | : TerminatorInst(Instruction::Unreachable, 0, 0, InsertBefore) { |
Chris Lattner | 076b3f1 | 2004-10-16 18:05:54 +0000 | [diff] [blame] | 1276 | } |
| 1277 | UnreachableInst(BasicBlock *InsertAtEnd) |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1278 | : TerminatorInst(Instruction::Unreachable, 0, 0, InsertAtEnd) { |
Chris Lattner | 076b3f1 | 2004-10-16 18:05:54 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | virtual UnreachableInst *clone() const; |
| 1282 | |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1283 | unsigned getNumSuccessors() const { return 0; } |
Chris Lattner | 076b3f1 | 2004-10-16 18:05:54 +0000 | [diff] [blame] | 1284 | |
| 1285 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 1286 | static inline bool classof(const UnreachableInst *) { return true; } |
| 1287 | static inline bool classof(const Instruction *I) { |
| 1288 | return I->getOpcode() == Instruction::Unreachable; |
| 1289 | } |
| 1290 | static inline bool classof(const Value *V) { |
| 1291 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 1292 | } |
Chris Lattner | 454928e | 2005-01-29 00:31:36 +0000 | [diff] [blame] | 1293 | private: |
| 1294 | virtual BasicBlock *getSuccessorV(unsigned idx) const; |
| 1295 | virtual unsigned getNumSuccessorsV() const; |
| 1296 | virtual void setSuccessorV(unsigned idx, BasicBlock *B); |
Chris Lattner | 076b3f1 | 2004-10-16 18:05:54 +0000 | [diff] [blame] | 1297 | }; |
| 1298 | |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 1299 | } // End llvm namespace |
Chris Lattner | a892a3a | 2003-01-27 22:08:52 +0000 | [diff] [blame] | 1300 | |
| 1301 | #endif |