blob: b009c01eecb7cae95c16657e458b950949cad58f [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Instruction.cpp - Implement the Instruction class -----------------===//
John Criswell482202a2003-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 Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This file implements the Instruction class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner62b7fd12002-04-07 20:49:59 +000014#include "llvm/Function.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000015#include "llvm/SymbolTable.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000016#include "llvm/Type.h"
Chris Lattner184b2982002-09-08 18:59:35 +000017#include "Support/LeakDetector.h"
Chris Lattner0e03ab62003-11-20 17:45:12 +000018using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000019
Chris Lattner3c787442002-09-10 15:45:53 +000020Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
21 Instruction *InsertBefore)
Chris Lattner4b361372002-02-03 07:52:58 +000022 : User(ty, Value::InstructionVal, Name) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000023 Parent = 0;
24 iType = it;
Chris Lattner184b2982002-09-08 18:59:35 +000025
26 // Make sure that we get added to a basicblock
27 LeakDetector::addGarbageObject(this);
Chris Lattner3c787442002-09-10 15:45:53 +000028
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 Lattner2f7c9632001-06-06 20:29:01 +000035}
36
Chris Lattner9ed7aef2002-09-06 21:33:15 +000037void Instruction::setParent(BasicBlock *P) {
Chris Lattnerd8a232b2004-02-04 01:06:38 +000038 if (getParent()) {
39 if (!P) LeakDetector::addGarbageObject(this);
40 } else {
41 if (P) LeakDetector::removeGarbageObject(this);
42 }
Chris Lattner184b2982002-09-08 18:59:35 +000043
Chris Lattner9ed7aef2002-09-06 21:33:15 +000044 Parent = P;
45}
46
Chris Lattner2f7c9632001-06-06 20:29:01 +000047// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000048void Instruction::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner62b7fd12002-04-07 20:49:59 +000049 BasicBlock *P = 0; Function *PP = 0;
Chris Lattnere8dd6ad2001-09-07 16:47:03 +000050 assert((ST == 0 || !getParent() || !getParent()->getParent() ||
Chris Lattner98cf1f52002-11-20 18:36:02 +000051 ST == &getParent()->getParent()->getSymbolTable()) &&
Chris Lattnere8dd6ad2001-09-07 16:47:03 +000052 "Invalid symtab argument!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000053 if ((P = getParent()) && (PP = P->getParent()) && hasName())
Chris Lattner98cf1f52002-11-20 18:36:02 +000054 PP->getSymbolTable().remove(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000055 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +000056 if (PP && hasName()) PP->getSymbolTable().insert(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000057}
Vikram S. Adve8aee7962002-07-14 23:09:40 +000058
59
60const char *Instruction::getOpcodeName(unsigned OpCode) {
61 switch (OpCode) {
62 // Terminators
Chris Lattnerb193ff82002-08-14 18:18:02 +000063 case Ret: return "ret";
64 case Br: return "br";
Vikram S. Adve8aee7962002-07-14 23:09:40 +000065 case Switch: return "switch";
66 case Invoke: return "invoke";
Chris Lattner66d5f572003-09-08 18:54:36 +000067 case Unwind: return "unwind";
Vikram S. Adve8aee7962002-07-14 23:09:40 +000068
Vikram S. Adve8aee7962002-07-14 23:09:40 +000069 // 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 Lattnerb94550e2003-10-19 21:34:28 +000098 case PHI: return "phi";
Vikram S. Adve8aee7962002-07-14 23:09:40 +000099 case Cast: return "cast";
100 case Call: return "call";
101 case Shl: return "shl";
102 case Shr: return "shr";
Chris Lattner5b337482003-10-18 05:57:43 +0000103 case VANext: return "vanext";
104 case VAArg: return "vaarg";
Chris Lattnerf70da102003-05-08 02:44:12 +0000105
Vikram S. Adve8aee7962002-07-14 23:09:40 +0000106 default: return "<Invalid operator> ";
107 }
108
109 return 0;
110}
Chris Lattnercab6c332002-10-31 04:14:01 +0000111
112
113/// isAssociative - Return true if the instruction is associative:
114///
115/// Associative operators satisfy: x op (y op z) === (x op y) op z)
116///
117/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
118/// applied to floating point types.
119///
120bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
121 if (Opcode == Add || Opcode == Mul ||
122 Opcode == And || Opcode == Or || Opcode == Xor) {
123 // Floating point operations do not associate!
124 return !Ty->isFloatingPoint();
125 }
126 return 0;
127}
128
129/// isCommutative - Return true if the instruction is commutative:
130///
Misha Brukmanfa100532003-10-10 17:54:14 +0000131/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnercab6c332002-10-31 04:14:01 +0000132///
133/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
134/// applied to any type.
135///
136bool Instruction::isCommutative(unsigned op) {
137 switch (op) {
138 case Add:
139 case Mul:
140 case And:
141 case Or:
142 case Xor:
143 case SetEQ:
144 case SetNE:
145 return true;
146 default:
147 return false;
148 }
149}
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000150
Chris Lattnerdab8b6b2004-01-12 23:18:25 +0000151/// isRelational - Return true if the instruction is a Set* instruction:
152///
153bool Instruction::isRelational(unsigned op) {
154 switch (op) {
155 case SetEQ:
156 case SetNE:
157 case SetLT:
158 case SetGT:
159 case SetLE:
160 case SetGE:
161 return true;
162 }
163 return false;
164}
165
166
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000167
168/// isTrappingInstruction - Return true if the instruction may trap.
169///
Tanya Lattnerf8c563f2003-07-31 05:06:09 +0000170bool Instruction::isTrapping(unsigned op) {
Tanya Lattnera93c7ae2003-07-31 04:05:50 +0000171 switch(op) {
172 case Div:
173 case Rem:
174 case Load:
175 case Store:
176 case Call:
177 case Invoke:
178 return true;
179 default:
180 return false;
181 }
182}