blob: e5e6501bd3d74f76d7d170cc1003247fddf0c279 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Instruction.cpp - Implement the Instruction class -----------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This file implements the Instruction class for the VMCore library.
4//
5//===----------------------------------------------------------------------===//
6
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00007#include "llvm/Function.h"
Chris Lattner00950542001-06-06 20:29:01 +00008#include "llvm/SymbolTable.h"
Chris Lattner7e708292002-06-25 16:13:24 +00009#include "llvm/Type.h"
Chris Lattnerd1e693f2002-09-08 18:59:35 +000010#include "Support/LeakDetector.h"
Chris Lattner00950542001-06-06 20:29:01 +000011
Chris Lattner2aa83112002-09-10 15:45:53 +000012Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
13 Instruction *InsertBefore)
Chris Lattner71947fd2002-02-03 07:52:58 +000014 : User(ty, Value::InstructionVal, Name) {
Chris Lattner00950542001-06-06 20:29:01 +000015 Parent = 0;
16 iType = it;
Chris Lattnerd1e693f2002-09-08 18:59:35 +000017
18 // Make sure that we get added to a basicblock
19 LeakDetector::addGarbageObject(this);
Chris Lattner2aa83112002-09-10 15:45:53 +000020
21 // If requested, insert this instruction into a basic block...
22 if (InsertBefore) {
23 assert(InsertBefore->getParent() &&
24 "Instruction to insert before is not in a basic block!");
25 InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
26 }
Chris Lattner00950542001-06-06 20:29:01 +000027}
28
Chris Lattnerbded1322002-09-06 21:33:15 +000029void Instruction::setParent(BasicBlock *P) {
Chris Lattnerd1e693f2002-09-08 18:59:35 +000030 if (getParent())
31 LeakDetector::addGarbageObject(this);
32
Chris Lattnerbded1322002-09-06 21:33:15 +000033 Parent = P;
Chris Lattnerd1e693f2002-09-08 18:59:35 +000034
35 if (getParent())
36 LeakDetector::removeGarbageObject(this);
Chris Lattnerbded1322002-09-06 21:33:15 +000037}
38
Chris Lattner00950542001-06-06 20:29:01 +000039// Specialize setName to take care of symbol table majik
Chris Lattner697954c2002-01-20 22:54:45 +000040void Instruction::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000041 BasicBlock *P = 0; Function *PP = 0;
Chris Lattnere05bf2f2001-09-07 16:47:03 +000042 assert((ST == 0 || !getParent() || !getParent()->getParent() ||
Chris Lattner6e6026b2002-11-20 18:36:02 +000043 ST == &getParent()->getParent()->getSymbolTable()) &&
Chris Lattnere05bf2f2001-09-07 16:47:03 +000044 "Invalid symtab argument!");
Chris Lattner00950542001-06-06 20:29:01 +000045 if ((P = getParent()) && (PP = P->getParent()) && hasName())
Chris Lattner6e6026b2002-11-20 18:36:02 +000046 PP->getSymbolTable().remove(this);
Chris Lattner00950542001-06-06 20:29:01 +000047 Value::setName(name);
Chris Lattner6e6026b2002-11-20 18:36:02 +000048 if (PP && hasName()) PP->getSymbolTable().insert(this);
Chris Lattner00950542001-06-06 20:29:01 +000049}
Vikram S. Advec1056452002-07-14 23:09:40 +000050
51
52const char *Instruction::getOpcodeName(unsigned OpCode) {
53 switch (OpCode) {
54 // Terminators
Chris Lattner0513e9f2002-08-14 18:18:02 +000055 case Ret: return "ret";
56 case Br: return "br";
Vikram S. Advec1056452002-07-14 23:09:40 +000057 case Switch: return "switch";
58 case Invoke: return "invoke";
Chris Lattnera6ce8982003-09-08 18:54:36 +000059 case Unwind: return "unwind";
Vikram S. Advec1056452002-07-14 23:09:40 +000060
Vikram S. Advec1056452002-07-14 23:09:40 +000061 // Standard binary operators...
62 case Add: return "add";
63 case Sub: return "sub";
64 case Mul: return "mul";
65 case Div: return "div";
66 case Rem: return "rem";
67
68 // Logical operators...
69 case And: return "and";
70 case Or : return "or";
71 case Xor: return "xor";
72
73 // SetCC operators...
74 case SetLE: return "setle";
75 case SetGE: return "setge";
76 case SetLT: return "setlt";
77 case SetGT: return "setgt";
78 case SetEQ: return "seteq";
79 case SetNE: return "setne";
80
81 // Memory instructions...
82 case Malloc: return "malloc";
83 case Free: return "free";
84 case Alloca: return "alloca";
85 case Load: return "load";
86 case Store: return "store";
87 case GetElementPtr: return "getelementptr";
88
89 // Other instructions...
90 case PHINode: return "phi";
91 case Cast: return "cast";
92 case Call: return "call";
93 case Shl: return "shl";
94 case Shr: return "shr";
Chris Lattner8f77dae2003-05-08 02:44:12 +000095 case VarArg: return "va_arg";
96
Vikram S. Advec1056452002-07-14 23:09:40 +000097 default: return "<Invalid operator> ";
98 }
99
100 return 0;
101}
Chris Lattnerf2da7242002-10-31 04:14:01 +0000102
103
104/// isAssociative - Return true if the instruction is associative:
105///
106/// Associative operators satisfy: x op (y op z) === (x op y) op z)
107///
108/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
109/// applied to floating point types.
110///
111bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
112 if (Opcode == Add || Opcode == Mul ||
113 Opcode == And || Opcode == Or || Opcode == Xor) {
114 // Floating point operations do not associate!
115 return !Ty->isFloatingPoint();
116 }
117 return 0;
118}
119
120/// isCommutative - Return true if the instruction is commutative:
121///
Misha Brukman6b634522003-10-10 17:54:14 +0000122/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnerf2da7242002-10-31 04:14:01 +0000123///
124/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
125/// applied to any type.
126///
127bool Instruction::isCommutative(unsigned op) {
128 switch (op) {
129 case Add:
130 case Mul:
131 case And:
132 case Or:
133 case Xor:
134 case SetEQ:
135 case SetNE:
136 return true;
137 default:
138 return false;
139 }
140}
Tanya Lattner741bb002003-07-31 04:05:50 +0000141
142
143/// isTrappingInstruction - Return true if the instruction may trap.
144///
Tanya Lattnerec127bb2003-07-31 05:06:09 +0000145bool Instruction::isTrapping(unsigned op) {
Tanya Lattner741bb002003-07-31 04:05:50 +0000146 switch(op) {
147 case Div:
148 case Rem:
149 case Load:
150 case Store:
151 case Call:
152 case Invoke:
153 return true;
154 default:
155 return false;
156 }
157}