blob: b449c9962fa2c85750fc10c00d7480336188cabc [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Instruction.cpp - Implement the Instruction class -----------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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 Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the Instruction class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner38f14552004-11-30 02:51:53 +000014#include "llvm/Instructions.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000015#include "llvm/Function.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
Chris Lattner7e708292002-06-25 16:13:24 +000017#include "llvm/Type.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/Support/LeakDetector.h"
Chris Lattner4b74c832003-11-20 17:45:12 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner96d83f62005-01-29 00:35:33 +000021Instruction::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 Lattnerd1e693f2002-09-08 18:59:35 +000024 // Make sure that we get added to a basicblock
25 LeakDetector::addGarbageObject(this);
Chris Lattner2aa83112002-09-10 15:45:53 +000026
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 Lattner00950542001-06-06 20:29:01 +000033}
34
Chris Lattner96d83f62005-01-29 00:35:33 +000035Instruction::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 Evlogimenose5828f12004-05-26 21:41:09 +000040
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 Lattner40515db2004-06-27 18:38:48 +000046void Instruction::setOpcode(unsigned opc) {
47 setValueType(Value::InstructionVal + opc);
48}
49
Chris Lattnerbded1322002-09-06 21:33:15 +000050void Instruction::setParent(BasicBlock *P) {
Chris Lattner786993c2004-02-04 01:06:38 +000051 if (getParent()) {
52 if (!P) LeakDetector::addGarbageObject(this);
53 } else {
54 if (P) LeakDetector::removeGarbageObject(this);
55 }
Chris Lattnerd1e693f2002-09-08 18:59:35 +000056
Chris Lattnerbded1322002-09-06 21:33:15 +000057 Parent = P;
58}
59
Chris Lattner00950542001-06-06 20:29:01 +000060// Specialize setName to take care of symbol table majik
Chris Lattner697954c2002-01-20 22:54:45 +000061void Instruction::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000062 BasicBlock *P = 0; Function *PP = 0;
Chris Lattnere05bf2f2001-09-07 16:47:03 +000063 assert((ST == 0 || !getParent() || !getParent()->getParent() ||
Chris Lattner6e6026b2002-11-20 18:36:02 +000064 ST == &getParent()->getParent()->getSymbolTable()) &&
Chris Lattnere05bf2f2001-09-07 16:47:03 +000065 "Invalid symtab argument!");
Chris Lattner00950542001-06-06 20:29:01 +000066 if ((P = getParent()) && (PP = P->getParent()) && hasName())
Chris Lattner6e6026b2002-11-20 18:36:02 +000067 PP->getSymbolTable().remove(this);
Chris Lattner00950542001-06-06 20:29:01 +000068 Value::setName(name);
Chris Lattner6e6026b2002-11-20 18:36:02 +000069 if (PP && hasName()) PP->getSymbolTable().insert(this);
Chris Lattner00950542001-06-06 20:29:01 +000070}
Vikram S. Advec1056452002-07-14 23:09:40 +000071
Chris Lattner4b833802004-10-11 22:21:39 +000072void Instruction::removeFromParent() {
73 getParent()->getInstList().remove(this);
74}
75
76void Instruction::eraseFromParent() {
77 getParent()->getInstList().erase(this);
78}
Vikram S. Advec1056452002-07-14 23:09:40 +000079
80const char *Instruction::getOpcodeName(unsigned OpCode) {
81 switch (OpCode) {
82 // Terminators
Chris Lattner0513e9f2002-08-14 18:18:02 +000083 case Ret: return "ret";
84 case Br: return "br";
Vikram S. Advec1056452002-07-14 23:09:40 +000085 case Switch: return "switch";
86 case Invoke: return "invoke";
Chris Lattnera6ce8982003-09-08 18:54:36 +000087 case Unwind: return "unwind";
Chris Lattnerb976e662004-10-16 18:08:06 +000088 case Unreachable: return "unreachable";
Vikram S. Advec1056452002-07-14 23:09:40 +000089
Vikram S. Advec1056452002-07-14 23:09:40 +000090 // Standard binary operators...
91 case Add: return "add";
92 case Sub: return "sub";
93 case Mul: return "mul";
94 case Div: return "div";
95 case Rem: return "rem";
96
97 // Logical operators...
98 case And: return "and";
99 case Or : return "or";
100 case Xor: return "xor";
101
102 // SetCC operators...
103 case SetLE: return "setle";
104 case SetGE: return "setge";
105 case SetLT: return "setlt";
106 case SetGT: return "setgt";
107 case SetEQ: return "seteq";
108 case SetNE: return "setne";
109
110 // Memory instructions...
111 case Malloc: return "malloc";
112 case Free: return "free";
113 case Alloca: return "alloca";
114 case Load: return "load";
115 case Store: return "store";
116 case GetElementPtr: return "getelementptr";
117
118 // Other instructions...
Chris Lattner3b237fc2003-10-19 21:34:28 +0000119 case PHI: return "phi";
Vikram S. Advec1056452002-07-14 23:09:40 +0000120 case Cast: return "cast";
Chris Lattnerb4f48802004-03-12 05:54:20 +0000121 case Select: return "select";
Vikram S. Advec1056452002-07-14 23:09:40 +0000122 case Call: return "call";
123 case Shl: return "shl";
124 case Shr: return "shr";
Chris Lattner4d45bd02003-10-18 05:57:43 +0000125 case VANext: return "vanext";
126 case VAArg: return "vaarg";
Chris Lattner8f77dae2003-05-08 02:44:12 +0000127
Vikram S. Advec1056452002-07-14 23:09:40 +0000128 default: return "<Invalid operator> ";
129 }
130
131 return 0;
132}
Chris Lattnerf2da7242002-10-31 04:14:01 +0000133
Chris Lattner38f14552004-11-30 02:51:53 +0000134/// isIdenticalTo - Return true if the specified instruction is exactly
135/// identical to the current one. This means that all operands match and any
136/// extra information (e.g. load is volatile) agree.
137bool Instruction::isIdenticalTo(Instruction *I) const {
138 if (getOpcode() != I->getOpcode() ||
139 getNumOperands() != I->getNumOperands() ||
140 getType() != I->getType())
141 return false;
142
143 // We have two instructions of identical opcode and #operands. Check to see
144 // if all operands are the same.
145 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
146 if (getOperand(i) != I->getOperand(i))
147 return false;
148
149 // Check special state that is a part of some instructions.
150 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
151 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
152 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
153 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
154 if (const VANextInst *VAN = dyn_cast<VANextInst>(this))
155 return VAN->getArgType() == cast<VANextInst>(I)->getArgType();
156 return true;
157}
158
Chris Lattnerf2da7242002-10-31 04:14:01 +0000159
160/// isAssociative - Return true if the instruction is associative:
161///
162/// Associative operators satisfy: x op (y op z) === (x op y) op z)
163///
164/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
165/// applied to floating point types.
166///
167bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
168 if (Opcode == Add || Opcode == Mul ||
169 Opcode == And || Opcode == Or || Opcode == Xor) {
170 // Floating point operations do not associate!
171 return !Ty->isFloatingPoint();
172 }
173 return 0;
174}
175
176/// isCommutative - Return true if the instruction is commutative:
177///
Misha Brukman6b634522003-10-10 17:54:14 +0000178/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnerf2da7242002-10-31 04:14:01 +0000179///
180/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
181/// applied to any type.
182///
183bool Instruction::isCommutative(unsigned op) {
184 switch (op) {
185 case Add:
186 case Mul:
187 case And:
188 case Or:
189 case Xor:
190 case SetEQ:
191 case SetNE:
192 return true;
193 default:
194 return false;
195 }
196}
Tanya Lattner741bb002003-07-31 04:05:50 +0000197
Chris Lattner3a534f22004-01-12 23:18:25 +0000198/// isRelational - Return true if the instruction is a Set* instruction:
199///
200bool Instruction::isRelational(unsigned op) {
201 switch (op) {
202 case SetEQ:
203 case SetNE:
204 case SetLT:
205 case SetGT:
206 case SetLE:
207 case SetGE:
208 return true;
209 }
210 return false;
211}
212
213
Tanya Lattner741bb002003-07-31 04:05:50 +0000214
215/// isTrappingInstruction - Return true if the instruction may trap.
216///
Tanya Lattnerec127bb2003-07-31 05:06:09 +0000217bool Instruction::isTrapping(unsigned op) {
Tanya Lattner741bb002003-07-31 04:05:50 +0000218 switch(op) {
219 case Div:
220 case Rem:
221 case Load:
222 case Store:
223 case Call:
224 case Invoke:
225 return true;
226 default:
227 return false;
228 }
229}