blob: b72656bdc232bee0490fc076ca063ae68b39f4c5 [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"
Chris Lattnerd1e693f2002-09-08 18:59:35 +000017#include "Support/LeakDetector.h"
Chris Lattner00950542001-06-06 20:29:01 +000018
Chris Lattner2aa83112002-09-10 15:45:53 +000019Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
20 Instruction *InsertBefore)
Chris Lattner71947fd2002-02-03 07:52:58 +000021 : User(ty, Value::InstructionVal, Name) {
Chris Lattner00950542001-06-06 20:29:01 +000022 Parent = 0;
23 iType = it;
Chris Lattnerd1e693f2002-09-08 18:59:35 +000024
25 // Make sure that we get added to a basicblock
26 LeakDetector::addGarbageObject(this);
Chris Lattner2aa83112002-09-10 15:45:53 +000027
28 // If requested, insert this instruction into a basic block...
29 if (InsertBefore) {
30 assert(InsertBefore->getParent() &&
31 "Instruction to insert before is not in a basic block!");
32 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
33 }
Chris Lattner00950542001-06-06 20:29:01 +000034}
35
Chris Lattnerbded1322002-09-06 21:33:15 +000036void Instruction::setParent(BasicBlock *P) {
Chris Lattnerd1e693f2002-09-08 18:59:35 +000037 if (getParent())
38 LeakDetector::addGarbageObject(this);
39
Chris Lattnerbded1322002-09-06 21:33:15 +000040 Parent = P;
Chris Lattnerd1e693f2002-09-08 18:59:35 +000041
42 if (getParent())
43 LeakDetector::removeGarbageObject(this);
Chris Lattnerbded1322002-09-06 21:33:15 +000044}
45
Chris Lattner00950542001-06-06 20:29:01 +000046// Specialize setName to take care of symbol table majik
Chris Lattner697954c2002-01-20 22:54:45 +000047void Instruction::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000048 BasicBlock *P = 0; Function *PP = 0;
Chris Lattnere05bf2f2001-09-07 16:47:03 +000049 assert((ST == 0 || !getParent() || !getParent()->getParent() ||
Chris Lattner6e6026b2002-11-20 18:36:02 +000050 ST == &getParent()->getParent()->getSymbolTable()) &&
Chris Lattnere05bf2f2001-09-07 16:47:03 +000051 "Invalid symtab argument!");
Chris Lattner00950542001-06-06 20:29:01 +000052 if ((P = getParent()) && (PP = P->getParent()) && hasName())
Chris Lattner6e6026b2002-11-20 18:36:02 +000053 PP->getSymbolTable().remove(this);
Chris Lattner00950542001-06-06 20:29:01 +000054 Value::setName(name);
Chris Lattner6e6026b2002-11-20 18:36:02 +000055 if (PP && hasName()) PP->getSymbolTable().insert(this);
Chris Lattner00950542001-06-06 20:29:01 +000056}
Vikram S. Advec1056452002-07-14 23:09:40 +000057
58
59const char *Instruction::getOpcodeName(unsigned OpCode) {
60 switch (OpCode) {
61 // Terminators
Chris Lattner0513e9f2002-08-14 18:18:02 +000062 case Ret: return "ret";
63 case Br: return "br";
Vikram S. Advec1056452002-07-14 23:09:40 +000064 case Switch: return "switch";
65 case Invoke: return "invoke";
Chris Lattnera6ce8982003-09-08 18:54:36 +000066 case Unwind: return "unwind";
Vikram S. Advec1056452002-07-14 23:09:40 +000067
Vikram S. Advec1056452002-07-14 23:09:40 +000068 // Standard binary operators...
69 case Add: return "add";
70 case Sub: return "sub";
71 case Mul: return "mul";
72 case Div: return "div";
73 case Rem: return "rem";
74
75 // Logical operators...
76 case And: return "and";
77 case Or : return "or";
78 case Xor: return "xor";
79
80 // SetCC operators...
81 case SetLE: return "setle";
82 case SetGE: return "setge";
83 case SetLT: return "setlt";
84 case SetGT: return "setgt";
85 case SetEQ: return "seteq";
86 case SetNE: return "setne";
87
88 // Memory instructions...
89 case Malloc: return "malloc";
90 case Free: return "free";
91 case Alloca: return "alloca";
92 case Load: return "load";
93 case Store: return "store";
94 case GetElementPtr: return "getelementptr";
95
96 // Other instructions...
Chris Lattner3b237fc2003-10-19 21:34:28 +000097 case PHI: return "phi";
Vikram S. Advec1056452002-07-14 23:09:40 +000098 case Cast: return "cast";
99 case Call: return "call";
100 case Shl: return "shl";
101 case Shr: return "shr";
Chris Lattner4d45bd02003-10-18 05:57:43 +0000102 case VANext: return "vanext";
103 case VAArg: return "vaarg";
Chris Lattner8f77dae2003-05-08 02:44:12 +0000104
Vikram S. Advec1056452002-07-14 23:09:40 +0000105 default: return "<Invalid operator> ";
106 }
107
108 return 0;
109}
Chris Lattnerf2da7242002-10-31 04:14:01 +0000110
111
112/// isAssociative - Return true if the instruction is associative:
113///
114/// Associative operators satisfy: x op (y op z) === (x op y) op z)
115///
116/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
117/// applied to floating point types.
118///
119bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
120 if (Opcode == Add || Opcode == Mul ||
121 Opcode == And || Opcode == Or || Opcode == Xor) {
122 // Floating point operations do not associate!
123 return !Ty->isFloatingPoint();
124 }
125 return 0;
126}
127
128/// isCommutative - Return true if the instruction is commutative:
129///
Misha Brukman6b634522003-10-10 17:54:14 +0000130/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnerf2da7242002-10-31 04:14:01 +0000131///
132/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
133/// applied to any type.
134///
135bool Instruction::isCommutative(unsigned op) {
136 switch (op) {
137 case Add:
138 case Mul:
139 case And:
140 case Or:
141 case Xor:
142 case SetEQ:
143 case SetNE:
144 return true;
145 default:
146 return false;
147 }
148}
Tanya Lattner741bb002003-07-31 04:05:50 +0000149
150
151/// isTrappingInstruction - Return true if the instruction may trap.
152///
Tanya Lattnerec127bb2003-07-31 05:06:09 +0000153bool Instruction::isTrapping(unsigned op) {
Tanya Lattner741bb002003-07-31 04:05:50 +0000154 switch(op) {
155 case Div:
156 case Rem:
157 case Load:
158 case Store:
159 case Call:
160 case Invoke:
161 return true;
162 default:
163 return false;
164 }
165}