blob: 2bce2dcd22c4781e4db9fa23f95bda46d5344651 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Instruction.cpp - Implement the Instruction class -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This file implements the Instruction class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +000014#include "llvm/Instructions.h"
Chris Lattner62b7fd12002-04-07 20:49:59 +000015#include "llvm/Function.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000017#include "llvm/Type.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000018#include "llvm/Support/LeakDetector.h"
Chris Lattner0e03ab62003-11-20 17:45:12 +000019using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000020
Chris Lattner5d1bc2c2005-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 Lattner184b2982002-09-08 18:59:35 +000024 // Make sure that we get added to a basicblock
25 LeakDetector::addGarbageObject(this);
Chris Lattner3c787442002-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 Lattner2f7c9632001-06-06 20:29:01 +000033}
34
Chris Lattner5d1bc2c2005-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 Evlogimenos9f0fdf72004-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 Lattnerdd7a7072004-06-27 18:38:48 +000046void Instruction::setOpcode(unsigned opc) {
47 setValueType(Value::InstructionVal + opc);
48}
49
Chris Lattner9ed7aef2002-09-06 21:33:15 +000050void Instruction::setParent(BasicBlock *P) {
Chris Lattnerd8a232b2004-02-04 01:06:38 +000051 if (getParent()) {
52 if (!P) LeakDetector::addGarbageObject(this);
53 } else {
54 if (P) LeakDetector::removeGarbageObject(this);
55 }
Chris Lattner184b2982002-09-08 18:59:35 +000056
Chris Lattner9ed7aef2002-09-06 21:33:15 +000057 Parent = P;
58}
59
Chris Lattner02a71e72004-10-11 22:21:39 +000060void Instruction::removeFromParent() {
61 getParent()->getInstList().remove(this);
62}
63
64void Instruction::eraseFromParent() {
65 getParent()->getInstList().erase(this);
66}
Vikram S. Adve8aee7962002-07-14 23:09:40 +000067
68const char *Instruction::getOpcodeName(unsigned OpCode) {
69 switch (OpCode) {
70 // Terminators
Chris Lattnerb193ff82002-08-14 18:18:02 +000071 case Ret: return "ret";
72 case Br: return "br";
Vikram S. Adve8aee7962002-07-14 23:09:40 +000073 case Switch: return "switch";
74 case Invoke: return "invoke";
Chris Lattner66d5f572003-09-08 18:54:36 +000075 case Unwind: return "unwind";
Chris Lattner5e0b9f22004-10-16 18:08:06 +000076 case Unreachable: return "unreachable";
Misha Brukmanb1c93172005-04-21 23:48:37 +000077
Vikram S. Adve8aee7962002-07-14 23:09:40 +000078 // Standard binary operators...
79 case Add: return "add";
80 case Sub: return "sub";
81 case Mul: return "mul";
82 case Div: return "div";
83 case Rem: return "rem";
84
85 // Logical operators...
86 case And: return "and";
87 case Or : return "or";
88 case Xor: return "xor";
89
90 // SetCC operators...
91 case SetLE: return "setle";
92 case SetGE: return "setge";
93 case SetLT: return "setlt";
94 case SetGT: return "setgt";
95 case SetEQ: return "seteq";
96 case SetNE: return "setne";
Misha Brukmanb1c93172005-04-21 23:48:37 +000097
Vikram S. Adve8aee7962002-07-14 23:09:40 +000098 // Memory instructions...
99 case Malloc: return "malloc";
100 case Free: return "free";
101 case Alloca: return "alloca";
102 case Load: return "load";
103 case Store: return "store";
104 case GetElementPtr: return "getelementptr";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000105
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000106 // Other instructions...
Chris Lattnerb94550e2003-10-19 21:34:28 +0000107 case PHI: return "phi";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000108 case Cast: return "cast";
Chris Lattner8337d442004-03-12 05:54:20 +0000109 case Select: return "select";
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000110 case Call: return "call";
111 case Shl: return "shl";
112 case Shr: return "shr";
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000113 case VAArg: return "va_arg";
Chris Lattnerf70da102003-05-08 02:44:12 +0000114
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000115 default: return "<Invalid operator> ";
116 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000117
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000118 return 0;
119}
Chris Lattnercab6c332002-10-31 04:14:01 +0000120
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000121/// isIdenticalTo - Return true if the specified instruction is exactly
122/// identical to the current one. This means that all operands match and any
123/// extra information (e.g. load is volatile) agree.
124bool Instruction::isIdenticalTo(Instruction *I) const {
125 if (getOpcode() != I->getOpcode() ||
126 getNumOperands() != I->getNumOperands() ||
127 getType() != I->getType())
128 return false;
129
130 // We have two instructions of identical opcode and #operands. Check to see
131 // if all operands are the same.
132 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
133 if (getOperand(i) != I->getOperand(i))
134 return false;
135
136 // Check special state that is a part of some instructions.
137 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
138 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
139 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
140 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
Chris Lattner06038452005-05-06 05:51:46 +0000141 if (const CallInst *CI = dyn_cast<CallInst>(this))
142 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
Chris Lattner0b6ebd8d2004-11-30 02:51:53 +0000143 return true;
144}
145
Chris Lattnercab6c332002-10-31 04:14:01 +0000146
147/// isAssociative - Return true if the instruction is associative:
148///
149/// Associative operators satisfy: x op (y op z) === (x op y) op z)
150///
151/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
152/// applied to floating point types.
153///
154bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
155 if (Opcode == Add || Opcode == Mul ||
156 Opcode == And || Opcode == Or || Opcode == Xor) {
157 // Floating point operations do not associate!
158 return !Ty->isFloatingPoint();
159 }
160 return 0;
161}
162
163/// isCommutative - Return true if the instruction is commutative:
164///
Misha Brukmanfa100532003-10-10 17:54:14 +0000165/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnercab6c332002-10-31 04:14:01 +0000166///
167/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
168/// applied to any type.
169///
170bool Instruction::isCommutative(unsigned op) {
171 switch (op) {
172 case Add:
173 case Mul:
Misha Brukmanb1c93172005-04-21 23:48:37 +0000174 case And:
Chris Lattnercab6c332002-10-31 04:14:01 +0000175 case Or:
176 case Xor:
177 case SetEQ:
178 case SetNE:
179 return true;
180 default:
181 return false;
182 }
183}
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000184
Chris Lattnerdab8b6b2004-01-12 23:18:25 +0000185/// isRelational - Return true if the instruction is a Set* instruction:
186///
187bool Instruction::isRelational(unsigned op) {
188 switch (op) {
189 case SetEQ:
190 case SetNE:
191 case SetLT:
192 case SetGT:
193 case SetLE:
194 case SetGE:
195 return true;
196 }
197 return false;
198}
199
200
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000201
202/// isTrappingInstruction - Return true if the instruction may trap.
203///
Tanya Lattnerf8c563f2003-07-31 05:06:09 +0000204bool Instruction::isTrapping(unsigned op) {
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000205 switch(op) {
206 case Div:
207 case Rem:
208 case Load:
209 case Store:
210 case Call:
211 case Invoke:
212 return true;
213 default:
214 return false;
215 }
216}