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