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