Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Instruction.cpp - Implement the Instruction class -----------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 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 | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 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 | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 14 | #include "llvm/Type.h" |
Chris Lattner | 38f1455 | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 15 | #include "llvm/Instructions.h" |
Duncan Sands | 5d84afd | 2007-12-05 21:03:28 +0000 | [diff] [blame^] | 16 | #include "llvm/IntrinsicInst.h" // FIXME: remove |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 17 | #include "llvm/Function.h" |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 19 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 4b74c83 | 2003-11-20 17:45:12 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 96d83f6 | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 22 | Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 23 | Instruction *InsertBefore) |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 24 | : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) { |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 25 | // Make sure that we get added to a basicblock |
| 26 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 2aa8311 | 2002-09-10 15:45:53 +0000 | [diff] [blame] | 27 | |
| 28 | // If requested, insert this instruction into a basic block... |
| 29 | if (InsertBefore) { |
| 30 | assert(InsertBefore->getParent() && |
| 31 | "Instruction to insert before is not in a basic block!"); |
| 32 | InsertBefore->getParent()->getInstList().insert(InsertBefore, this); |
| 33 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Chris Lattner | 96d83f6 | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 36 | Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 37 | BasicBlock *InsertAtEnd) |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 38 | : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) { |
Chris Lattner | 96d83f6 | 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 | e5828f1 | 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 | f00042a | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | |
Chris Lattner | 70aa33e | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 48 | // Out of line virtual method, so the vtable, etc has a home. |
| 49 | Instruction::~Instruction() { |
| 50 | assert(Parent == 0 && "Instruction still linked in the program!"); |
| 51 | } |
| 52 | |
| 53 | |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 54 | void Instruction::setParent(BasicBlock *P) { |
Chris Lattner | 786993c | 2004-02-04 01:06:38 +0000 | [diff] [blame] | 55 | if (getParent()) { |
| 56 | if (!P) LeakDetector::addGarbageObject(this); |
| 57 | } else { |
| 58 | if (P) LeakDetector::removeGarbageObject(this); |
| 59 | } |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 60 | |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 61 | Parent = P; |
| 62 | } |
| 63 | |
Chris Lattner | 4b83380 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 64 | void Instruction::removeFromParent() { |
| 65 | getParent()->getInstList().remove(this); |
| 66 | } |
| 67 | |
| 68 | void Instruction::eraseFromParent() { |
| 69 | getParent()->getInstList().erase(this); |
| 70 | } |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 0fe34d8 | 2005-08-08 05:21:50 +0000 | [diff] [blame] | 72 | /// moveBefore - Unlink this instruction from its current basic block and |
| 73 | /// insert it into the basic block that MovePos lives in, right before |
| 74 | /// MovePos. |
| 75 | void Instruction::moveBefore(Instruction *MovePos) { |
| 76 | MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(), |
| 77 | this); |
| 78 | } |
| 79 | |
| 80 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 81 | const char *Instruction::getOpcodeName(unsigned OpCode) { |
| 82 | switch (OpCode) { |
| 83 | // Terminators |
Chris Lattner | 0513e9f | 2002-08-14 18:18:02 +0000 | [diff] [blame] | 84 | case Ret: return "ret"; |
| 85 | case Br: return "br"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 86 | case Switch: return "switch"; |
| 87 | case Invoke: return "invoke"; |
Chris Lattner | a6ce898 | 2003-09-08 18:54:36 +0000 | [diff] [blame] | 88 | case Unwind: return "unwind"; |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 89 | case Unreachable: return "unreachable"; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 90 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 91 | // Standard binary operators... |
| 92 | case Add: return "add"; |
| 93 | case Sub: return "sub"; |
| 94 | case Mul: return "mul"; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 95 | case UDiv: return "udiv"; |
| 96 | case SDiv: return "sdiv"; |
| 97 | case FDiv: return "fdiv"; |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 98 | case URem: return "urem"; |
| 99 | case SRem: return "srem"; |
| 100 | case FRem: return "frem"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 101 | |
| 102 | // Logical operators... |
| 103 | case And: return "and"; |
| 104 | case Or : return "or"; |
| 105 | case Xor: return "xor"; |
| 106 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 107 | // Memory instructions... |
| 108 | case Malloc: return "malloc"; |
| 109 | case Free: return "free"; |
| 110 | case Alloca: return "alloca"; |
| 111 | case Load: return "load"; |
| 112 | case Store: return "store"; |
| 113 | case GetElementPtr: return "getelementptr"; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 114 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 115 | // Convert instructions... |
| 116 | case Trunc: return "trunc"; |
| 117 | case ZExt: return "zext"; |
| 118 | case SExt: return "sext"; |
| 119 | case FPTrunc: return "fptrunc"; |
| 120 | case FPExt: return "fpext"; |
| 121 | case FPToUI: return "fptoui"; |
| 122 | case FPToSI: return "fptosi"; |
| 123 | case UIToFP: return "uitofp"; |
| 124 | case SIToFP: return "sitofp"; |
| 125 | case IntToPtr: return "inttoptr"; |
| 126 | case PtrToInt: return "ptrtoint"; |
| 127 | case BitCast: return "bitcast"; |
| 128 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 129 | // Other instructions... |
Reid Spencer | 74f1642 | 2006-12-03 06:27:29 +0000 | [diff] [blame] | 130 | case ICmp: return "icmp"; |
| 131 | case FCmp: return "fcmp"; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 132 | case PHI: return "phi"; |
| 133 | case Select: return "select"; |
| 134 | case Call: return "call"; |
| 135 | case Shl: return "shl"; |
| 136 | case LShr: return "lshr"; |
| 137 | case AShr: return "ashr"; |
| 138 | case VAArg: return "va_arg"; |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 139 | case ExtractElement: return "extractelement"; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 140 | case InsertElement: return "insertelement"; |
| 141 | case ShuffleVector: return "shufflevector"; |
Chris Lattner | 8f77dae | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 142 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 143 | default: return "<Invalid operator> "; |
| 144 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 145 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 146 | return 0; |
| 147 | } |
Chris Lattner | f2da724 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 38f1455 | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 149 | /// isIdenticalTo - Return true if the specified instruction is exactly |
| 150 | /// identical to the current one. This means that all operands match and any |
| 151 | /// extra information (e.g. load is volatile) agree. |
| 152 | bool Instruction::isIdenticalTo(Instruction *I) const { |
| 153 | if (getOpcode() != I->getOpcode() || |
| 154 | getNumOperands() != I->getNumOperands() || |
| 155 | getType() != I->getType()) |
| 156 | return false; |
| 157 | |
| 158 | // We have two instructions of identical opcode and #operands. Check to see |
| 159 | // if all operands are the same. |
| 160 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 161 | if (getOperand(i) != I->getOperand(i)) |
| 162 | return false; |
| 163 | |
| 164 | // Check special state that is a part of some instructions. |
| 165 | if (const LoadInst *LI = dyn_cast<LoadInst>(this)) |
| 166 | return LI->isVolatile() == cast<LoadInst>(I)->isVolatile(); |
| 167 | if (const StoreInst *SI = dyn_cast<StoreInst>(this)) |
| 168 | return SI->isVolatile() == cast<StoreInst>(I)->isVolatile(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 169 | if (const CmpInst *CI = dyn_cast<CmpInst>(this)) |
| 170 | return CI->getPredicate() == cast<CmpInst>(I)->getPredicate(); |
Chris Lattner | ddb6db4 | 2005-05-06 05:51:46 +0000 | [diff] [blame] | 171 | if (const CallInst *CI = dyn_cast<CallInst>(this)) |
| 172 | return CI->isTailCall() == cast<CallInst>(I)->isTailCall(); |
Chris Lattner | 38f1455 | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 176 | // isSameOperationAs |
| 177 | bool Instruction::isSameOperationAs(Instruction *I) const { |
| 178 | if (getOpcode() != I->getOpcode() || getType() != I->getType() || |
| 179 | getNumOperands() != I->getNumOperands()) |
| 180 | return false; |
| 181 | |
| 182 | // We have two instructions of identical opcode and #operands. Check to see |
| 183 | // if all operands are the same type |
| 184 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 185 | if (getOperand(i)->getType() != I->getOperand(i)->getType()) |
| 186 | return false; |
| 187 | |
| 188 | // Check special state that is a part of some instructions. |
| 189 | if (const LoadInst *LI = dyn_cast<LoadInst>(this)) |
| 190 | return LI->isVolatile() == cast<LoadInst>(I)->isVolatile(); |
| 191 | if (const StoreInst *SI = dyn_cast<StoreInst>(this)) |
| 192 | return SI->isVolatile() == cast<StoreInst>(I)->isVolatile(); |
| 193 | if (const CmpInst *CI = dyn_cast<CmpInst>(this)) |
| 194 | return CI->getPredicate() == cast<CmpInst>(I)->getPredicate(); |
| 195 | if (const CallInst *CI = dyn_cast<CallInst>(this)) |
| 196 | return CI->isTailCall() == cast<CallInst>(I)->isTailCall(); |
| 197 | |
| 198 | return true; |
| 199 | } |
| 200 | |
Chris Lattner | bb5493d | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 201 | /// mayWriteToMemory - Return true if this instruction may modify memory. |
| 202 | /// |
| 203 | bool Instruction::mayWriteToMemory() const { |
| 204 | switch (getOpcode()) { |
| 205 | default: return false; |
| 206 | case Instruction::Free: |
Chris Lattner | bb5493d | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 207 | case Instruction::Invoke: |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 208 | case Instruction::Store: |
Chris Lattner | bb5493d | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 209 | case Instruction::VAArg: |
| 210 | return true; |
| 211 | case Instruction::Call: |
Duncan Sands | 5d84afd | 2007-12-05 21:03:28 +0000 | [diff] [blame^] | 212 | if (!isa<IntrinsicInst>(this)) |
| 213 | return true; // FIXME: workaround gcc bootstrap breakage |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 214 | return !cast<CallInst>(this)->onlyReadsMemory(); |
Chris Lattner | bb5493d | 2007-02-15 23:15:00 +0000 | [diff] [blame] | 215 | case Instruction::Load: |
| 216 | return cast<LoadInst>(this)->isVolatile(); |
| 217 | } |
| 218 | } |
Chris Lattner | f2da724 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 219 | |
| 220 | /// isAssociative - Return true if the instruction is associative: |
| 221 | /// |
| 222 | /// Associative operators satisfy: x op (y op z) === (x op y) op z) |
| 223 | /// |
| 224 | /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not |
| 225 | /// applied to floating point types. |
| 226 | /// |
| 227 | bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) { |
Chris Lattner | 4d3839d | 2006-10-26 18:27:26 +0000 | [diff] [blame] | 228 | if (Opcode == And || Opcode == Or || Opcode == Xor) |
| 229 | return true; |
| 230 | |
| 231 | // Add/Mul reassociate unless they are FP or FP vectors. |
| 232 | if (Opcode == Add || Opcode == Mul) |
| 233 | return !Ty->isFPOrFPVector(); |
Chris Lattner | f2da724 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /// isCommutative - Return true if the instruction is commutative: |
| 238 | /// |
Misha Brukman | 6b63452 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 239 | /// Commutative operators satisfy: (x op y) === (y op x) |
Chris Lattner | f2da724 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 240 | /// |
| 241 | /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when |
| 242 | /// applied to any type. |
| 243 | /// |
| 244 | bool Instruction::isCommutative(unsigned op) { |
| 245 | switch (op) { |
| 246 | case Add: |
| 247 | case Mul: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 248 | case And: |
Chris Lattner | f2da724 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 249 | case Or: |
| 250 | case Xor: |
Chris Lattner | f2da724 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 251 | return true; |
| 252 | default: |
| 253 | return false; |
| 254 | } |
| 255 | } |
Tanya Lattner | 741bb00 | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 256 | |
Tanya Lattner | 741bb00 | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 257 | /// isTrappingInstruction - Return true if the instruction may trap. |
| 258 | /// |
Tanya Lattner | ec127bb | 2003-07-31 05:06:09 +0000 | [diff] [blame] | 259 | bool Instruction::isTrapping(unsigned op) { |
Tanya Lattner | 741bb00 | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 260 | switch(op) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 261 | case UDiv: |
| 262 | case SDiv: |
| 263 | case FDiv: |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 264 | case URem: |
| 265 | case SRem: |
| 266 | case FRem: |
Tanya Lattner | 741bb00 | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 267 | case Load: |
| 268 | case Store: |
| 269 | case Call: |
| 270 | case Invoke: |
| 271 | return true; |
| 272 | default: |
| 273 | return false; |
| 274 | } |
| 275 | } |