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