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 | |
Chris Lattner | 38f1455 | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 14 | #include "llvm/Instructions.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 15 | #include "llvm/Function.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | #include "llvm/SymbolTable.h" |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 17 | #include "llvm/Type.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 18 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 4b74c83 | 2003-11-20 17:45:12 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 96d83f6 | 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) |
| 23 | : User(ty, Value::InstructionVal + it, Ops, NumOps, Name), Parent(0) { |
Chris Lattner | d1e693f | 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 | 2aa8311 | 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 | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Chris Lattner | 96d83f6 | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 35 | Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, |
| 36 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 37 | : User(ty, Value::InstructionVal + it, Ops, NumOps, Name), Parent(0) { |
| 38 | // Make sure that we get added to a basicblock |
| 39 | LeakDetector::addGarbageObject(this); |
Alkis Evlogimenos | e5828f1 | 2004-05-26 21:41:09 +0000 | [diff] [blame] | 40 | |
| 41 | // append this instruction into the basic block |
| 42 | assert(InsertAtEnd && "Basic block to append to may not be NULL!"); |
| 43 | InsertAtEnd->getInstList().push_back(this); |
| 44 | } |
| 45 | |
Chris Lattner | 40515db | 2004-06-27 18:38:48 +0000 | [diff] [blame] | 46 | void Instruction::setOpcode(unsigned opc) { |
| 47 | setValueType(Value::InstructionVal + opc); |
| 48 | } |
| 49 | |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 50 | void Instruction::setParent(BasicBlock *P) { |
Chris Lattner | 786993c | 2004-02-04 01:06:38 +0000 | [diff] [blame] | 51 | if (getParent()) { |
| 52 | if (!P) LeakDetector::addGarbageObject(this); |
| 53 | } else { |
| 54 | if (P) LeakDetector::removeGarbageObject(this); |
| 55 | } |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 56 | |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 57 | Parent = P; |
| 58 | } |
| 59 | |
Chris Lattner | 4b83380 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 60 | void Instruction::removeFromParent() { |
| 61 | getParent()->getInstList().remove(this); |
| 62 | } |
| 63 | |
| 64 | void Instruction::eraseFromParent() { |
| 65 | getParent()->getInstList().erase(this); |
| 66 | } |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 0fe34d8 | 2005-08-08 05:21:50 +0000 | [diff] [blame] | 68 | /// moveBefore - Unlink this instruction from its current basic block and |
| 69 | /// insert it into the basic block that MovePos lives in, right before |
| 70 | /// MovePos. |
| 71 | void Instruction::moveBefore(Instruction *MovePos) { |
| 72 | MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(), |
| 73 | this); |
| 74 | } |
| 75 | |
| 76 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 77 | const char *Instruction::getOpcodeName(unsigned OpCode) { |
| 78 | switch (OpCode) { |
| 79 | // Terminators |
Chris Lattner | 0513e9f | 2002-08-14 18:18:02 +0000 | [diff] [blame] | 80 | case Ret: return "ret"; |
| 81 | case Br: return "br"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 82 | case Switch: return "switch"; |
| 83 | case Invoke: return "invoke"; |
Chris Lattner | a6ce898 | 2003-09-08 18:54:36 +0000 | [diff] [blame] | 84 | case Unwind: return "unwind"; |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 85 | case Unreachable: return "unreachable"; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 86 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 87 | // Standard binary operators... |
| 88 | case Add: return "add"; |
| 89 | case Sub: return "sub"; |
| 90 | case Mul: return "mul"; |
| 91 | case Div: return "div"; |
| 92 | case Rem: return "rem"; |
| 93 | |
| 94 | // Logical operators... |
| 95 | case And: return "and"; |
| 96 | case Or : return "or"; |
| 97 | case Xor: return "xor"; |
| 98 | |
| 99 | // SetCC operators... |
| 100 | case SetLE: return "setle"; |
| 101 | case SetGE: return "setge"; |
| 102 | case SetLT: return "setlt"; |
| 103 | case SetGT: return "setgt"; |
| 104 | case SetEQ: return "seteq"; |
| 105 | case SetNE: return "setne"; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 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 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 115 | // Other instructions... |
Chris Lattner | 3b237fc | 2003-10-19 21:34:28 +0000 | [diff] [blame] | 116 | case PHI: return "phi"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 117 | case Cast: return "cast"; |
Chris Lattner | b4f4880 | 2004-03-12 05:54:20 +0000 | [diff] [blame] | 118 | case Select: return "select"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 119 | case Call: return "call"; |
| 120 | case Shl: return "shl"; |
| 121 | case Shr: return "shr"; |
Andrew Lenharth | 558bc88 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 122 | case VAArg: return "va_arg"; |
Chris Lattner | 8f77dae | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 123 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 124 | default: return "<Invalid operator> "; |
| 125 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 126 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 127 | return 0; |
| 128 | } |
Chris Lattner | f2da724 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 38f1455 | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 130 | /// isIdenticalTo - Return true if the specified instruction is exactly |
| 131 | /// identical to the current one. This means that all operands match and any |
| 132 | /// extra information (e.g. load is volatile) agree. |
| 133 | bool Instruction::isIdenticalTo(Instruction *I) const { |
| 134 | if (getOpcode() != I->getOpcode() || |
| 135 | getNumOperands() != I->getNumOperands() || |
| 136 | getType() != I->getType()) |
| 137 | return false; |
| 138 | |
| 139 | // We have two instructions of identical opcode and #operands. Check to see |
| 140 | // if all operands are the same. |
| 141 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 142 | if (getOperand(i) != I->getOperand(i)) |
| 143 | return false; |
| 144 | |
| 145 | // Check special state that is a part of some instructions. |
| 146 | if (const LoadInst *LI = dyn_cast<LoadInst>(this)) |
| 147 | return LI->isVolatile() == cast<LoadInst>(I)->isVolatile(); |
| 148 | if (const StoreInst *SI = dyn_cast<StoreInst>(this)) |
| 149 | return SI->isVolatile() == cast<StoreInst>(I)->isVolatile(); |
Chris Lattner | ddb6db4 | 2005-05-06 05:51:46 +0000 | [diff] [blame] | 150 | if (const CallInst *CI = dyn_cast<CallInst>(this)) |
| 151 | return CI->isTailCall() == cast<CallInst>(I)->isTailCall(); |
Chris Lattner | 38f1455 | 2004-11-30 02:51:53 +0000 | [diff] [blame] | 152 | return true; |
| 153 | } |
| 154 | |
Chris Lattner | f2da724 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 155 | |
| 156 | /// isAssociative - Return true if the instruction is associative: |
| 157 | /// |
| 158 | /// Associative operators satisfy: x op (y op z) === (x op y) op z) |
| 159 | /// |
| 160 | /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not |
| 161 | /// applied to floating point types. |
| 162 | /// |
| 163 | bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) { |
| 164 | if (Opcode == Add || Opcode == Mul || |
| 165 | Opcode == And || Opcode == Or || Opcode == Xor) { |
| 166 | // Floating point operations do not associate! |
| 167 | return !Ty->isFloatingPoint(); |
| 168 | } |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /// isCommutative - Return true if the instruction is commutative: |
| 173 | /// |
Misha Brukman | 6b63452 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 174 | /// Commutative operators satisfy: (x op y) === (y op x) |
Chris Lattner | f2da724 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 175 | /// |
| 176 | /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when |
| 177 | /// applied to any type. |
| 178 | /// |
| 179 | bool Instruction::isCommutative(unsigned op) { |
| 180 | switch (op) { |
| 181 | case Add: |
| 182 | case Mul: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 183 | case And: |
Chris Lattner | f2da724 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 184 | case Or: |
| 185 | case Xor: |
| 186 | case SetEQ: |
| 187 | case SetNE: |
| 188 | return true; |
| 189 | default: |
| 190 | return false; |
| 191 | } |
| 192 | } |
Tanya Lattner | 741bb00 | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 3a534f2 | 2004-01-12 23:18:25 +0000 | [diff] [blame] | 194 | /// isRelational - Return true if the instruction is a Set* instruction: |
| 195 | /// |
| 196 | bool Instruction::isRelational(unsigned op) { |
| 197 | switch (op) { |
| 198 | case SetEQ: |
| 199 | case SetNE: |
| 200 | case SetLT: |
| 201 | case SetGT: |
| 202 | case SetLE: |
| 203 | case SetGE: |
| 204 | return true; |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | |
Tanya Lattner | 741bb00 | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 210 | |
| 211 | /// isTrappingInstruction - Return true if the instruction may trap. |
| 212 | /// |
Tanya Lattner | ec127bb | 2003-07-31 05:06:09 +0000 | [diff] [blame] | 213 | bool Instruction::isTrapping(unsigned op) { |
Tanya Lattner | 741bb00 | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 214 | switch(op) { |
| 215 | case Div: |
| 216 | case Rem: |
| 217 | case Load: |
| 218 | case Store: |
| 219 | case Call: |
| 220 | case Invoke: |
| 221 | return true; |
| 222 | default: |
| 223 | return false; |
| 224 | } |
| 225 | } |