Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Instruction.cpp - Implement the Instruction class -----------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 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 | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 14 | #include "llvm/Function.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | #include "llvm/SymbolTable.h" |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 16 | #include "llvm/Type.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 17 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 4b74c83 | 2003-11-20 17:45:12 +0000 | [diff] [blame] | 18 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 19 | |
Alkis Evlogimenos | e5828f1 | 2004-05-26 21:41:09 +0000 | [diff] [blame] | 20 | void Instruction::init() |
| 21 | { |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 22 | // Make sure that we get added to a basicblock |
| 23 | LeakDetector::addGarbageObject(this); |
Alkis Evlogimenos | e5828f1 | 2004-05-26 21:41:09 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name, |
| 27 | Instruction *InsertBefore) |
Chris Lattner | 40515db | 2004-06-27 18:38:48 +0000 | [diff] [blame] | 28 | : User(ty, Value::InstructionVal + it, Name), Parent(0) { |
Alkis Evlogimenos | e5828f1 | 2004-05-26 21:41:09 +0000 | [diff] [blame] | 29 | init(); |
Chris Lattner | 2aa8311 | 2002-09-10 15:45:53 +0000 | [diff] [blame] | 30 | |
| 31 | // If requested, insert this instruction into a basic block... |
| 32 | if (InsertBefore) { |
| 33 | assert(InsertBefore->getParent() && |
| 34 | "Instruction to insert before is not in a basic block!"); |
| 35 | InsertBefore->getParent()->getInstList().insert(InsertBefore, this); |
| 36 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Alkis Evlogimenos | e5828f1 | 2004-05-26 21:41:09 +0000 | [diff] [blame] | 39 | Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name, |
| 40 | BasicBlock *InsertAtEnd) |
Chris Lattner | 40515db | 2004-06-27 18:38:48 +0000 | [diff] [blame] | 41 | : User(ty, Value::InstructionVal + it, Name), Parent(0) { |
Alkis Evlogimenos | e5828f1 | 2004-05-26 21:41:09 +0000 | [diff] [blame] | 42 | init(); |
| 43 | |
| 44 | // append this instruction into the basic block |
| 45 | assert(InsertAtEnd && "Basic block to append to may not be NULL!"); |
| 46 | InsertAtEnd->getInstList().push_back(this); |
| 47 | } |
| 48 | |
Chris Lattner | 40515db | 2004-06-27 18:38:48 +0000 | [diff] [blame] | 49 | void Instruction::setOpcode(unsigned opc) { |
| 50 | setValueType(Value::InstructionVal + opc); |
| 51 | } |
| 52 | |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 53 | void Instruction::setParent(BasicBlock *P) { |
Chris Lattner | 786993c | 2004-02-04 01:06:38 +0000 | [diff] [blame] | 54 | if (getParent()) { |
| 55 | if (!P) LeakDetector::addGarbageObject(this); |
| 56 | } else { |
| 57 | if (P) LeakDetector::removeGarbageObject(this); |
| 58 | } |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 59 | |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 60 | Parent = P; |
| 61 | } |
| 62 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 63 | // Specialize setName to take care of symbol table majik |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 64 | void Instruction::setName(const std::string &name, SymbolTable *ST) { |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 65 | BasicBlock *P = 0; Function *PP = 0; |
Chris Lattner | e05bf2f | 2001-09-07 16:47:03 +0000 | [diff] [blame] | 66 | assert((ST == 0 || !getParent() || !getParent()->getParent() || |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 67 | ST == &getParent()->getParent()->getSymbolTable()) && |
Chris Lattner | e05bf2f | 2001-09-07 16:47:03 +0000 | [diff] [blame] | 68 | "Invalid symtab argument!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 69 | if ((P = getParent()) && (PP = P->getParent()) && hasName()) |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 70 | PP->getSymbolTable().remove(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 71 | Value::setName(name); |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 72 | if (PP && hasName()) PP->getSymbolTable().insert(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 73 | } |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 4b83380 | 2004-10-11 22:21:39 +0000 | [diff] [blame^] | 75 | void Instruction::removeFromParent() { |
| 76 | getParent()->getInstList().remove(this); |
| 77 | } |
| 78 | |
| 79 | void Instruction::eraseFromParent() { |
| 80 | getParent()->getInstList().erase(this); |
| 81 | } |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 82 | |
| 83 | const char *Instruction::getOpcodeName(unsigned OpCode) { |
| 84 | switch (OpCode) { |
| 85 | // Terminators |
Chris Lattner | 0513e9f | 2002-08-14 18:18:02 +0000 | [diff] [blame] | 86 | case Ret: return "ret"; |
| 87 | case Br: return "br"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 88 | case Switch: return "switch"; |
| 89 | case Invoke: return "invoke"; |
Chris Lattner | a6ce898 | 2003-09-08 18:54:36 +0000 | [diff] [blame] | 90 | case Unwind: return "unwind"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 91 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 92 | // Standard binary operators... |
| 93 | case Add: return "add"; |
| 94 | case Sub: return "sub"; |
| 95 | case Mul: return "mul"; |
| 96 | case Div: return "div"; |
| 97 | case Rem: return "rem"; |
| 98 | |
| 99 | // Logical operators... |
| 100 | case And: return "and"; |
| 101 | case Or : return "or"; |
| 102 | case Xor: return "xor"; |
| 103 | |
| 104 | // SetCC operators... |
| 105 | case SetLE: return "setle"; |
| 106 | case SetGE: return "setge"; |
| 107 | case SetLT: return "setlt"; |
| 108 | case SetGT: return "setgt"; |
| 109 | case SetEQ: return "seteq"; |
| 110 | case SetNE: return "setne"; |
| 111 | |
| 112 | // Memory instructions... |
| 113 | case Malloc: return "malloc"; |
| 114 | case Free: return "free"; |
| 115 | case Alloca: return "alloca"; |
| 116 | case Load: return "load"; |
| 117 | case Store: return "store"; |
| 118 | case GetElementPtr: return "getelementptr"; |
| 119 | |
| 120 | // Other instructions... |
Chris Lattner | 3b237fc | 2003-10-19 21:34:28 +0000 | [diff] [blame] | 121 | case PHI: return "phi"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 122 | case Cast: return "cast"; |
Chris Lattner | b4f4880 | 2004-03-12 05:54:20 +0000 | [diff] [blame] | 123 | case Select: return "select"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 124 | case Call: return "call"; |
| 125 | case Shl: return "shl"; |
| 126 | case Shr: return "shr"; |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 127 | case VANext: return "vanext"; |
| 128 | case VAArg: return "vaarg"; |
Chris Lattner | 8f77dae | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 129 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 130 | default: return "<Invalid operator> "; |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
Chris Lattner | f2da724 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 135 | |
| 136 | |
| 137 | /// isAssociative - Return true if the instruction is associative: |
| 138 | /// |
| 139 | /// Associative operators satisfy: x op (y op z) === (x op y) op z) |
| 140 | /// |
| 141 | /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not |
| 142 | /// applied to floating point types. |
| 143 | /// |
| 144 | bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) { |
| 145 | if (Opcode == Add || Opcode == Mul || |
| 146 | Opcode == And || Opcode == Or || Opcode == Xor) { |
| 147 | // Floating point operations do not associate! |
| 148 | return !Ty->isFloatingPoint(); |
| 149 | } |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /// isCommutative - Return true if the instruction is commutative: |
| 154 | /// |
Misha Brukman | 6b63452 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 155 | /// Commutative operators satisfy: (x op y) === (y op x) |
Chris Lattner | f2da724 | 2002-10-31 04:14:01 +0000 | [diff] [blame] | 156 | /// |
| 157 | /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when |
| 158 | /// applied to any type. |
| 159 | /// |
| 160 | bool Instruction::isCommutative(unsigned op) { |
| 161 | switch (op) { |
| 162 | case Add: |
| 163 | case Mul: |
| 164 | case And: |
| 165 | case Or: |
| 166 | case Xor: |
| 167 | case SetEQ: |
| 168 | case SetNE: |
| 169 | return true; |
| 170 | default: |
| 171 | return false; |
| 172 | } |
| 173 | } |
Tanya Lattner | 741bb00 | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 3a534f2 | 2004-01-12 23:18:25 +0000 | [diff] [blame] | 175 | /// isRelational - Return true if the instruction is a Set* instruction: |
| 176 | /// |
| 177 | bool Instruction::isRelational(unsigned op) { |
| 178 | switch (op) { |
| 179 | case SetEQ: |
| 180 | case SetNE: |
| 181 | case SetLT: |
| 182 | case SetGT: |
| 183 | case SetLE: |
| 184 | case SetGE: |
| 185 | return true; |
| 186 | } |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | |
Tanya Lattner | 741bb00 | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 191 | |
| 192 | /// isTrappingInstruction - Return true if the instruction may trap. |
| 193 | /// |
Tanya Lattner | ec127bb | 2003-07-31 05:06:09 +0000 | [diff] [blame] | 194 | bool Instruction::isTrapping(unsigned op) { |
Tanya Lattner | 741bb00 | 2003-07-31 04:05:50 +0000 | [diff] [blame] | 195 | switch(op) { |
| 196 | case Div: |
| 197 | case Rem: |
| 198 | case Load: |
| 199 | case Store: |
| 200 | case Call: |
| 201 | case Invoke: |
| 202 | return true; |
| 203 | default: |
| 204 | return false; |
| 205 | } |
| 206 | } |