blob: f54acf5f03edb23e2b9e2ec1af4b2fd99e81882c [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 Lattner2fbfdcf2002-04-07 20:49:59 +000014#include "llvm/Function.h"
Chris Lattner00950542001-06-06 20:29:01 +000015#include "llvm/SymbolTable.h"
Chris Lattner7e708292002-06-25 16:13:24 +000016#include "llvm/Type.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000017#include "llvm/Support/LeakDetector.h"
Chris Lattner4b74c832003-11-20 17:45:12 +000018using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000019
Alkis Evlogimenose5828f12004-05-26 21:41:09 +000020void Instruction::init()
21{
Chris Lattnerd1e693f2002-09-08 18:59:35 +000022 // Make sure that we get added to a basicblock
23 LeakDetector::addGarbageObject(this);
Alkis Evlogimenose5828f12004-05-26 21:41:09 +000024}
25
26Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
27 Instruction *InsertBefore)
Chris Lattner40515db2004-06-27 18:38:48 +000028 : User(ty, Value::InstructionVal + it, Name), Parent(0) {
Alkis Evlogimenose5828f12004-05-26 21:41:09 +000029 init();
Chris Lattner2aa83112002-09-10 15:45:53 +000030
31 // If requested, insert this instruction into a basic block...
32 if (InsertBefore) {
33 assert(InsertBefore->getParent() &&
34 "Instruction to insert before is not in a basic block!");
35 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
36 }
Chris Lattner00950542001-06-06 20:29:01 +000037}
38
Alkis Evlogimenose5828f12004-05-26 21:41:09 +000039Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
40 BasicBlock *InsertAtEnd)
Chris Lattner40515db2004-06-27 18:38:48 +000041 : User(ty, Value::InstructionVal + it, Name), Parent(0) {
Alkis Evlogimenose5828f12004-05-26 21:41:09 +000042 init();
43
44 // append this instruction into the basic block
45 assert(InsertAtEnd && "Basic block to append to may not be NULL!");
46 InsertAtEnd->getInstList().push_back(this);
47}
48
Chris Lattner40515db2004-06-27 18:38:48 +000049void Instruction::setOpcode(unsigned opc) {
50 setValueType(Value::InstructionVal + opc);
51}
52
Chris Lattnerbded1322002-09-06 21:33:15 +000053void Instruction::setParent(BasicBlock *P) {
Chris Lattner786993c2004-02-04 01:06:38 +000054 if (getParent()) {
55 if (!P) LeakDetector::addGarbageObject(this);
56 } else {
57 if (P) LeakDetector::removeGarbageObject(this);
58 }
Chris Lattnerd1e693f2002-09-08 18:59:35 +000059
Chris Lattnerbded1322002-09-06 21:33:15 +000060 Parent = P;
61}
62
Chris Lattner00950542001-06-06 20:29:01 +000063// Specialize setName to take care of symbol table majik
Chris Lattner697954c2002-01-20 22:54:45 +000064void Instruction::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000065 BasicBlock *P = 0; Function *PP = 0;
Chris Lattnere05bf2f2001-09-07 16:47:03 +000066 assert((ST == 0 || !getParent() || !getParent()->getParent() ||
Chris Lattner6e6026b2002-11-20 18:36:02 +000067 ST == &getParent()->getParent()->getSymbolTable()) &&
Chris Lattnere05bf2f2001-09-07 16:47:03 +000068 "Invalid symtab argument!");
Chris Lattner00950542001-06-06 20:29:01 +000069 if ((P = getParent()) && (PP = P->getParent()) && hasName())
Chris Lattner6e6026b2002-11-20 18:36:02 +000070 PP->getSymbolTable().remove(this);
Chris Lattner00950542001-06-06 20:29:01 +000071 Value::setName(name);
Chris Lattner6e6026b2002-11-20 18:36:02 +000072 if (PP && hasName()) PP->getSymbolTable().insert(this);
Chris Lattner00950542001-06-06 20:29:01 +000073}
Vikram S. Advec1056452002-07-14 23:09:40 +000074
75
76const char *Instruction::getOpcodeName(unsigned OpCode) {
77 switch (OpCode) {
78 // Terminators
Chris Lattner0513e9f2002-08-14 18:18:02 +000079 case Ret: return "ret";
80 case Br: return "br";
Vikram S. Advec1056452002-07-14 23:09:40 +000081 case Switch: return "switch";
82 case Invoke: return "invoke";
Chris Lattnera6ce8982003-09-08 18:54:36 +000083 case Unwind: return "unwind";
Vikram S. Advec1056452002-07-14 23:09:40 +000084
Vikram S. Advec1056452002-07-14 23:09:40 +000085 // Standard binary operators...
86 case Add: return "add";
87 case Sub: return "sub";
88 case Mul: return "mul";
89 case Div: return "div";
90 case Rem: return "rem";
91
92 // Logical operators...
93 case And: return "and";
94 case Or : return "or";
95 case Xor: return "xor";
96
97 // SetCC operators...
98 case SetLE: return "setle";
99 case SetGE: return "setge";
100 case SetLT: return "setlt";
101 case SetGT: return "setgt";
102 case SetEQ: return "seteq";
103 case SetNE: return "setne";
104
105 // Memory instructions...
106 case Malloc: return "malloc";
107 case Free: return "free";
108 case Alloca: return "alloca";
109 case Load: return "load";
110 case Store: return "store";
111 case GetElementPtr: return "getelementptr";
112
113 // Other instructions...
Chris Lattner3b237fc2003-10-19 21:34:28 +0000114 case PHI: return "phi";
Vikram S. Advec1056452002-07-14 23:09:40 +0000115 case Cast: return "cast";
Chris Lattnerb4f48802004-03-12 05:54:20 +0000116 case Select: return "select";
Vikram S. Advec1056452002-07-14 23:09:40 +0000117 case Call: return "call";
118 case Shl: return "shl";
119 case Shr: return "shr";
Chris Lattner4d45bd02003-10-18 05:57:43 +0000120 case VANext: return "vanext";
121 case VAArg: return "vaarg";
Chris Lattner8f77dae2003-05-08 02:44:12 +0000122
Vikram S. Advec1056452002-07-14 23:09:40 +0000123 default: return "<Invalid operator> ";
124 }
125
126 return 0;
127}
Chris Lattnerf2da7242002-10-31 04:14:01 +0000128
129
130/// isAssociative - Return true if the instruction is associative:
131///
132/// Associative operators satisfy: x op (y op z) === (x op y) op z)
133///
134/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
135/// applied to floating point types.
136///
137bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
138 if (Opcode == Add || Opcode == Mul ||
139 Opcode == And || Opcode == Or || Opcode == Xor) {
140 // Floating point operations do not associate!
141 return !Ty->isFloatingPoint();
142 }
143 return 0;
144}
145
146/// isCommutative - Return true if the instruction is commutative:
147///
Misha Brukman6b634522003-10-10 17:54:14 +0000148/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnerf2da7242002-10-31 04:14:01 +0000149///
150/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
151/// applied to any type.
152///
153bool Instruction::isCommutative(unsigned op) {
154 switch (op) {
155 case Add:
156 case Mul:
157 case And:
158 case Or:
159 case Xor:
160 case SetEQ:
161 case SetNE:
162 return true;
163 default:
164 return false;
165 }
166}
Tanya Lattner741bb002003-07-31 04:05:50 +0000167
Chris Lattner3a534f22004-01-12 23:18:25 +0000168/// isRelational - Return true if the instruction is a Set* instruction:
169///
170bool Instruction::isRelational(unsigned op) {
171 switch (op) {
172 case SetEQ:
173 case SetNE:
174 case SetLT:
175 case SetGT:
176 case SetLE:
177 case SetGE:
178 return true;
179 }
180 return false;
181}
182
183
Tanya Lattner741bb002003-07-31 04:05:50 +0000184
185/// isTrappingInstruction - Return true if the instruction may trap.
186///
Tanya Lattnerec127bb2003-07-31 05:06:09 +0000187bool Instruction::isTrapping(unsigned op) {
Tanya Lattner741bb002003-07-31 04:05:50 +0000188 switch(op) {
189 case Div:
190 case Rem:
191 case Load:
192 case Store:
193 case Call:
194 case Invoke:
195 return true;
196 default:
197 return false;
198 }
199}