Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Instruction.cpp - Implement the Instruction class -----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the Instruction class for the VMCore library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 14 | #include "llvm/Type.h" |
Chris Lattner | 0b6ebd8d | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 15 | #include "llvm/Instructions.h" |
Andrew Lenharth | 9d4819b | 2007-02-16 02:25:55 +0000 | [diff] [blame] | 16 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 17 | #include "llvm/Function.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 18 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 0e03ab6 | 2003-11-20 17:45:12 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 5d1bc2c | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 21 | Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, |
| 22 | const std::string &Name, Instruction *InsertBefore) |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 23 | : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 24 | // Make sure that we get added to a basicblock |
| 25 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 3c78744 | 2002-09-10 15:45:53 +0000 | [diff] [blame] | 26 | |
| 27 | // If requested, insert this instruction into a basic block... |
| 28 | if (InsertBefore) { |
| 29 | assert(InsertBefore->getParent() && |
| 30 | "Instruction to insert before is not in a basic block!"); |
| 31 | InsertBefore->getParent()->getInstList().insert(InsertBefore, this); |
| 32 | } |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 33 | setName(Name); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Chris Lattner | 5d1bc2c | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 36 | Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, |
| 37 | const std::string &Name, BasicBlock *InsertAtEnd) |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 38 | : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) { |
Chris Lattner | 5d1bc2c | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 39 | // Make sure that we get added to a basicblock |
| 40 | LeakDetector::addGarbageObject(this); |
Alkis Evlogimenos | 9f0fdf7 | 2004-05-26 21:41:09 +0000 | [diff] [blame] | 41 | |
| 42 | // append this instruction into the basic block |
| 43 | assert(InsertAtEnd && "Basic block to append to may not be NULL!"); |
| 44 | InsertAtEnd->getInstList().push_back(this); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 45 | setName(Name); |
Alkis Evlogimenos | 9f0fdf7 | 2004-05-26 21:41:09 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 48 | Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, |
| 49 | const char *Name, Instruction *InsertBefore) |
| 50 | : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) { |
| 51 | // Make sure that we get added to a basicblock |
| 52 | LeakDetector::addGarbageObject(this); |
| 53 | |
| 54 | // If requested, insert this instruction into a basic block... |
| 55 | if (InsertBefore) { |
| 56 | assert(InsertBefore->getParent() && |
| 57 | "Instruction to insert before is not in a basic block!"); |
| 58 | InsertBefore->getParent()->getInstList().insert(InsertBefore, this); |
| 59 | } |
| 60 | if (Name && *Name) setName(Name); |
| 61 | } |
| 62 | |
| 63 | Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, |
| 64 | const char *Name, BasicBlock *InsertAtEnd) |
| 65 | : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) { |
| 66 | // Make sure that we get added to a basicblock |
| 67 | LeakDetector::addGarbageObject(this); |
| 68 | |
| 69 | // append this instruction into the basic block |
| 70 | assert(InsertAtEnd && "Basic block to append to may not be NULL!"); |
| 71 | InsertAtEnd->getInstList().push_back(this); |
| 72 | if (Name && *Name) setName(Name); |
| 73 | } |
| 74 | |
| 75 | |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 76 | // Out of line virtual method, so the vtable, etc has a home. |
| 77 | Instruction::~Instruction() { |
| 78 | assert(Parent == 0 && "Instruction still linked in the program!"); |
| 79 | } |
| 80 | |
| 81 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 82 | void Instruction::setParent(BasicBlock *P) { |
Chris Lattner | d8a232b | 2004-02-04 01:06:38 +0000 | [diff] [blame] | 83 | if (getParent()) { |
| 84 | if (!P) LeakDetector::addGarbageObject(this); |
| 85 | } else { |
| 86 | if (P) LeakDetector::removeGarbageObject(this); |
| 87 | } |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 89 | Parent = P; |
| 90 | } |
| 91 | |
Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 92 | void Instruction::removeFromParent() { |
| 93 | getParent()->getInstList().remove(this); |
| 94 | } |
| 95 | |
| 96 | void Instruction::eraseFromParent() { |
| 97 | getParent()->getInstList().erase(this); |
| 98 | } |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 24a0a43 | 2005-08-08 05:21:50 +0000 | [diff] [blame] | 100 | /// moveBefore - Unlink this instruction from its current basic block and |
| 101 | /// insert it into the basic block that MovePos lives in, right before |
| 102 | /// MovePos. |
| 103 | void Instruction::moveBefore(Instruction *MovePos) { |
| 104 | MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(), |
| 105 | this); |
| 106 | } |
| 107 | |
| 108 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 109 | const char *Instruction::getOpcodeName(unsigned OpCode) { |
| 110 | switch (OpCode) { |
| 111 | // Terminators |
Chris Lattner | b193ff8 | 2002-08-14 18:18:02 +0000 | [diff] [blame] | 112 | case Ret: return "ret"; |
| 113 | case Br: return "br"; |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 114 | case Switch: return "switch"; |
| 115 | case Invoke: return "invoke"; |
Chris Lattner | 66d5f57 | 2003-09-08 18:54:36 +0000 | [diff] [blame] | 116 | case Unwind: return "unwind"; |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 117 | case Unreachable: return "unreachable"; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 118 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 119 | // Standard binary operators... |
| 120 | case Add: return "add"; |
| 121 | case Sub: return "sub"; |
| 122 | case Mul: return "mul"; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 123 | case UDiv: return "udiv"; |
| 124 | case SDiv: return "sdiv"; |
| 125 | case FDiv: return "fdiv"; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 126 | case URem: return "urem"; |
| 127 | case SRem: return "srem"; |
| 128 | case FRem: return "frem"; |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 129 | |
| 130 | // Logical operators... |
| 131 | case And: return "and"; |
| 132 | case Or : return "or"; |
| 133 | case Xor: return "xor"; |
| 134 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 135 | // Memory instructions... |
| 136 | case Malloc: return "malloc"; |
| 137 | case Free: return "free"; |
| 138 | case Alloca: return "alloca"; |
| 139 | case Load: return "load"; |
| 140 | case Store: return "store"; |
| 141 | case GetElementPtr: return "getelementptr"; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 142 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 143 | // Convert instructions... |
| 144 | case Trunc: return "trunc"; |
| 145 | case ZExt: return "zext"; |
| 146 | case SExt: return "sext"; |
| 147 | case FPTrunc: return "fptrunc"; |
| 148 | case FPExt: return "fpext"; |
| 149 | case FPToUI: return "fptoui"; |
| 150 | case FPToSI: return "fptosi"; |
| 151 | case UIToFP: return "uitofp"; |
| 152 | case SIToFP: return "sitofp"; |
| 153 | case IntToPtr: return "inttoptr"; |
| 154 | case PtrToInt: return "ptrtoint"; |
| 155 | case BitCast: return "bitcast"; |
| 156 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 157 | // Other instructions... |
Reid Spencer | 45e5239 | 2006-12-03 06:27:29 +0000 | [diff] [blame] | 158 | case ICmp: return "icmp"; |
| 159 | case FCmp: return "fcmp"; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 160 | case PHI: return "phi"; |
| 161 | case Select: return "select"; |
| 162 | case Call: return "call"; |
| 163 | case Shl: return "shl"; |
| 164 | case LShr: return "lshr"; |
| 165 | case AShr: return "ashr"; |
| 166 | case VAArg: return "va_arg"; |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 167 | case ExtractElement: return "extractelement"; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 168 | case InsertElement: return "insertelement"; |
| 169 | case ShuffleVector: return "shufflevector"; |
Chris Lattner | f70da10 | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 170 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 171 | default: return "<Invalid operator> "; |
| 172 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 173 | |
Vikram S. Adve | 8aee796 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 174 | return 0; |
| 175 | } |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 0b6ebd8d | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 177 | /// isIdenticalTo - Return true if the specified instruction is exactly |
| 178 | /// identical to the current one. This means that all operands match and any |
| 179 | /// extra information (e.g. load is volatile) agree. |
| 180 | bool Instruction::isIdenticalTo(Instruction *I) const { |
| 181 | if (getOpcode() != I->getOpcode() || |
| 182 | getNumOperands() != I->getNumOperands() || |
| 183 | getType() != I->getType()) |
| 184 | return false; |
| 185 | |
| 186 | // We have two instructions of identical opcode and #operands. Check to see |
| 187 | // if all operands are the same. |
| 188 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 189 | if (getOperand(i) != I->getOperand(i)) |
| 190 | return false; |
| 191 | |
| 192 | // Check special state that is a part of some instructions. |
| 193 | if (const LoadInst *LI = dyn_cast<LoadInst>(this)) |
| 194 | return LI->isVolatile() == cast<LoadInst>(I)->isVolatile(); |
| 195 | if (const StoreInst *SI = dyn_cast<StoreInst>(this)) |
| 196 | return SI->isVolatile() == cast<StoreInst>(I)->isVolatile(); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 197 | if (const CmpInst *CI = dyn_cast<CmpInst>(this)) |
| 198 | return CI->getPredicate() == cast<CmpInst>(I)->getPredicate(); |
Chris Lattner | 0603845 | 2005-05-06 05:51:46 +0000 | [diff] [blame] | 199 | if (const CallInst *CI = dyn_cast<CallInst>(this)) |
| 200 | return CI->isTailCall() == cast<CallInst>(I)->isTailCall(); |
Chris Lattner | 0b6ebd8d | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 201 | return true; |
| 202 | } |
| 203 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 204 | // isSameOperationAs |
| 205 | bool Instruction::isSameOperationAs(Instruction *I) const { |
| 206 | if (getOpcode() != I->getOpcode() || getType() != I->getType() || |
| 207 | getNumOperands() != I->getNumOperands()) |
| 208 | return false; |
| 209 | |
| 210 | // We have two instructions of identical opcode and #operands. Check to see |
| 211 | // if all operands are the same type |
| 212 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 213 | if (getOperand(i)->getType() != I->getOperand(i)->getType()) |
| 214 | return false; |
| 215 | |
| 216 | // Check special state that is a part of some instructions. |
| 217 | if (const LoadInst *LI = dyn_cast<LoadInst>(this)) |
| 218 | return LI->isVolatile() == cast<LoadInst>(I)->isVolatile(); |
| 219 | if (const StoreInst *SI = dyn_cast<StoreInst>(this)) |
| 220 | return SI->isVolatile() == cast<StoreInst>(I)->isVolatile(); |
| 221 | if (const CmpInst *CI = dyn_cast<CmpInst>(this)) |
| 222 | return CI->getPredicate() == cast<CmpInst>(I)->getPredicate(); |
| 223 | if (const CallInst *CI = dyn_cast<CallInst>(this)) |
| 224 | return CI->isTailCall() == cast<CallInst>(I)->isTailCall(); |
| 225 | |
| 226 | return true; |
| 227 | } |
| 228 | |
Chris Lattner | c992e18 | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 229 | /// mayWriteToMemory - Return true if this instruction may modify memory. |
| 230 | /// |
| 231 | bool Instruction::mayWriteToMemory() const { |
| 232 | switch (getOpcode()) { |
| 233 | default: return false; |
| 234 | case Instruction::Free: |
| 235 | case Instruction::Store: |
| 236 | case Instruction::Invoke: |
| 237 | case Instruction::VAArg: |
| 238 | return true; |
| 239 | case Instruction::Call: |
Chris Lattner | 8635863 | 2007-02-19 19:46:17 +0000 | [diff] [blame] | 240 | //if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(this)) { |
Chris Lattner | c992e18 | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 241 | // If the intrinsic doesn't write memory, it is safe. |
Reid Spencer | 887ae70a | 2007-02-19 19:00:29 +0000 | [diff] [blame] | 242 | // FIXME: this is obviously supposed to determine which intrinsics |
| 243 | // don't write to memory, but hasn't been implemented yet. |
Chris Lattner | 8635863 | 2007-02-19 19:46:17 +0000 | [diff] [blame] | 244 | //} |
Chris Lattner | c992e18 | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 245 | return true; |
| 246 | case Instruction::Load: |
| 247 | return cast<LoadInst>(this)->isVolatile(); |
| 248 | } |
| 249 | } |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 250 | |
| 251 | /// isAssociative - Return true if the instruction is associative: |
| 252 | /// |
| 253 | /// Associative operators satisfy: x op (y op z) === (x op y) op z) |
| 254 | /// |
| 255 | /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not |
| 256 | /// applied to floating point types. |
| 257 | /// |
| 258 | bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) { |
Chris Lattner | 9138680 | 2006-10-26 18:27:26 +0000 | [diff] [blame] | 259 | if (Opcode == And || Opcode == Or || Opcode == Xor) |
| 260 | return true; |
| 261 | |
| 262 | // Add/Mul reassociate unless they are FP or FP vectors. |
| 263 | if (Opcode == Add || Opcode == Mul) |
| 264 | return !Ty->isFPOrFPVector(); |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /// isCommutative - Return true if the instruction is commutative: |
| 269 | /// |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 270 | /// Commutative operators satisfy: (x op y) === (y op x) |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 271 | /// |
| 272 | /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when |
| 273 | /// applied to any type. |
| 274 | /// |
| 275 | bool Instruction::isCommutative(unsigned op) { |
| 276 | switch (op) { |
| 277 | case Add: |
| 278 | case Mul: |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 279 | case And: |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 280 | case Or: |
| 281 | case Xor: |
Chris Lattner | cab6c33 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 282 | return true; |
| 283 | default: |
| 284 | return false; |
| 285 | } |
| 286 | } |
Tanya Lattner | a93c7ae | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 287 | |
Tanya Lattner | a93c7ae | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 288 | /// isTrappingInstruction - Return true if the instruction may trap. |
| 289 | /// |
Tanya Lattner | f8c563f | 2003-07-31 05:06:09 +0000 | [diff] [blame] | 290 | bool Instruction::isTrapping(unsigned op) { |
Tanya Lattner | a93c7ae | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 291 | switch(op) { |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 292 | case UDiv: |
| 293 | case SDiv: |
| 294 | case FDiv: |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 295 | case URem: |
| 296 | case SRem: |
| 297 | case FRem: |
Tanya Lattner | a93c7ae | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 298 | case Load: |
| 299 | case Store: |
| 300 | case Call: |
| 301 | case Invoke: |
| 302 | return true; |
| 303 | default: |
| 304 | return false; |
| 305 | } |
| 306 | } |