blob: b2951461d7c7793c21eb63c59206776419e84dc3 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Instruction.cpp - Implement the Instruction class -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattner70aa33e2006-06-21 16:53:47 +000046// Out of line virtual method, so the vtable, etc has a home.
47Instruction::~Instruction() {
48 assert(Parent == 0 && "Instruction still linked in the program!");
49}
50
51
Chris Lattner40515db2004-06-27 18:38:48 +000052void Instruction::setOpcode(unsigned opc) {
53 setValueType(Value::InstructionVal + opc);
54}
55
Chris Lattnerbded1322002-09-06 21:33:15 +000056void Instruction::setParent(BasicBlock *P) {
Chris Lattner786993c2004-02-04 01:06:38 +000057 if (getParent()) {
58 if (!P) LeakDetector::addGarbageObject(this);
59 } else {
60 if (P) LeakDetector::removeGarbageObject(this);
61 }
Chris Lattnerd1e693f2002-09-08 18:59:35 +000062
Chris Lattnerbded1322002-09-06 21:33:15 +000063 Parent = P;
64}
65
Chris Lattner4b833802004-10-11 22:21:39 +000066void Instruction::removeFromParent() {
67 getParent()->getInstList().remove(this);
68}
69
70void Instruction::eraseFromParent() {
71 getParent()->getInstList().erase(this);
72}
Vikram S. Advec1056452002-07-14 23:09:40 +000073
Chris Lattner0fe34d82005-08-08 05:21:50 +000074/// moveBefore - Unlink this instruction from its current basic block and
75/// insert it into the basic block that MovePos lives in, right before
76/// MovePos.
77void Instruction::moveBefore(Instruction *MovePos) {
78 MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
79 this);
80}
81
82
Vikram S. Advec1056452002-07-14 23:09:40 +000083const char *Instruction::getOpcodeName(unsigned OpCode) {
84 switch (OpCode) {
85 // Terminators
Chris Lattner0513e9f2002-08-14 18:18:02 +000086 case Ret: return "ret";
87 case Br: return "br";
Vikram S. Advec1056452002-07-14 23:09:40 +000088 case Switch: return "switch";
89 case Invoke: return "invoke";
Chris Lattnera6ce8982003-09-08 18:54:36 +000090 case Unwind: return "unwind";
Chris Lattnerb976e662004-10-16 18:08:06 +000091 case Unreachable: return "unreachable";
Misha Brukmanfd939082005-04-21 23:48:37 +000092
Vikram S. Advec1056452002-07-14 23:09:40 +000093 // Standard binary operators...
94 case Add: return "add";
95 case Sub: return "sub";
96 case Mul: return "mul";
97 case Div: return "div";
98 case Rem: return "rem";
99
100 // Logical operators...
101 case And: return "and";
102 case Or : return "or";
103 case Xor: return "xor";
104
105 // SetCC operators...
106 case SetLE: return "setle";
107 case SetGE: return "setge";
108 case SetLT: return "setlt";
109 case SetGT: return "setgt";
110 case SetEQ: return "seteq";
111 case SetNE: return "setne";
Misha Brukmanfd939082005-04-21 23:48:37 +0000112
Vikram S. Advec1056452002-07-14 23:09:40 +0000113 // Memory instructions...
114 case Malloc: return "malloc";
115 case Free: return "free";
116 case Alloca: return "alloca";
117 case Load: return "load";
118 case Store: return "store";
119 case GetElementPtr: return "getelementptr";
Misha Brukmanfd939082005-04-21 23:48:37 +0000120
Vikram S. Advec1056452002-07-14 23:09:40 +0000121 // Other instructions...
Chris Lattner3b237fc2003-10-19 21:34:28 +0000122 case PHI: return "phi";
Vikram S. Advec1056452002-07-14 23:09:40 +0000123 case Cast: return "cast";
Chris Lattnerb4f48802004-03-12 05:54:20 +0000124 case Select: return "select";
Vikram S. Advec1056452002-07-14 23:09:40 +0000125 case Call: return "call";
126 case Shl: return "shl";
127 case Shr: return "shr";
Andrew Lenharth558bc882005-06-18 18:34:52 +0000128 case VAArg: return "va_arg";
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000129 case ExtractElement: return "extractelement";
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000130 case InsertElement: return "insertelement";
Chris Lattner00f10232006-04-08 01:18:18 +0000131 case ShuffleVector: return "shufflevector";
Chris Lattner8f77dae2003-05-08 02:44:12 +0000132
Vikram S. Advec1056452002-07-14 23:09:40 +0000133 default: return "<Invalid operator> ";
134 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000135
Vikram S. Advec1056452002-07-14 23:09:40 +0000136 return 0;
137}
Chris Lattnerf2da7242002-10-31 04:14:01 +0000138
Chris Lattner38f14552004-11-30 02:51:53 +0000139/// isIdenticalTo - Return true if the specified instruction is exactly
140/// identical to the current one. This means that all operands match and any
141/// extra information (e.g. load is volatile) agree.
142bool Instruction::isIdenticalTo(Instruction *I) const {
143 if (getOpcode() != I->getOpcode() ||
144 getNumOperands() != I->getNumOperands() ||
145 getType() != I->getType())
146 return false;
147
148 // We have two instructions of identical opcode and #operands. Check to see
149 // if all operands are the same.
150 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
151 if (getOperand(i) != I->getOperand(i))
152 return false;
153
154 // Check special state that is a part of some instructions.
155 if (const LoadInst *LI = dyn_cast<LoadInst>(this))
156 return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
157 if (const StoreInst *SI = dyn_cast<StoreInst>(this))
158 return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
Chris Lattnerddb6db42005-05-06 05:51:46 +0000159 if (const CallInst *CI = dyn_cast<CallInst>(this))
160 return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
Chris Lattner38f14552004-11-30 02:51:53 +0000161 return true;
162}
163
Chris Lattnerf2da7242002-10-31 04:14:01 +0000164
165/// isAssociative - Return true if the instruction is associative:
166///
167/// Associative operators satisfy: x op (y op z) === (x op y) op z)
168///
169/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
170/// applied to floating point types.
171///
172bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
173 if (Opcode == Add || Opcode == Mul ||
174 Opcode == And || Opcode == Or || Opcode == Xor) {
175 // Floating point operations do not associate!
176 return !Ty->isFloatingPoint();
177 }
178 return 0;
179}
180
181/// isCommutative - Return true if the instruction is commutative:
182///
Misha Brukman6b634522003-10-10 17:54:14 +0000183/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnerf2da7242002-10-31 04:14:01 +0000184///
185/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
186/// applied to any type.
187///
188bool Instruction::isCommutative(unsigned op) {
189 switch (op) {
190 case Add:
191 case Mul:
Misha Brukmanfd939082005-04-21 23:48:37 +0000192 case And:
Chris Lattnerf2da7242002-10-31 04:14:01 +0000193 case Or:
194 case Xor:
195 case SetEQ:
196 case SetNE:
197 return true;
198 default:
199 return false;
200 }
201}
Tanya Lattner741bb002003-07-31 04:05:50 +0000202
Chris Lattnera5b07402006-09-17 19:14:47 +0000203/// isComparison - Return true if the instruction is a Set* instruction:
Chris Lattner3a534f22004-01-12 23:18:25 +0000204///
Chris Lattnera5b07402006-09-17 19:14:47 +0000205bool Instruction::isComparison(unsigned op) {
Chris Lattner3a534f22004-01-12 23:18:25 +0000206 switch (op) {
207 case SetEQ:
208 case SetNE:
209 case SetLT:
210 case SetGT:
211 case SetLE:
212 case SetGE:
213 return true;
214 }
215 return false;
216}
217
218
Tanya Lattner741bb002003-07-31 04:05:50 +0000219
220/// isTrappingInstruction - Return true if the instruction may trap.
221///
Tanya Lattnerec127bb2003-07-31 05:06:09 +0000222bool Instruction::isTrapping(unsigned op) {
Tanya Lattner741bb002003-07-31 04:05:50 +0000223 switch(op) {
224 case Div:
225 case Rem:
226 case Load:
227 case Store:
228 case Call:
229 case Invoke:
230 return true;
231 default:
232 return false;
233 }
234}