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