blob: 1a536427349c346b5d6c50d519245a52e66af7c9 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- Instruction.cpp - Implement the Instruction class --------*- C++ -*--=//
2//
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";
59
Vikram S. Advec1056452002-07-14 23:09:40 +000060 // Standard binary operators...
61 case Add: return "add";
62 case Sub: return "sub";
63 case Mul: return "mul";
64 case Div: return "div";
65 case Rem: return "rem";
66
67 // Logical operators...
68 case And: return "and";
69 case Or : return "or";
70 case Xor: return "xor";
71
72 // SetCC operators...
73 case SetLE: return "setle";
74 case SetGE: return "setge";
75 case SetLT: return "setlt";
76 case SetGT: return "setgt";
77 case SetEQ: return "seteq";
78 case SetNE: return "setne";
79
80 // Memory instructions...
81 case Malloc: return "malloc";
82 case Free: return "free";
83 case Alloca: return "alloca";
84 case Load: return "load";
85 case Store: return "store";
86 case GetElementPtr: return "getelementptr";
87
88 // Other instructions...
89 case PHINode: return "phi";
90 case Cast: return "cast";
91 case Call: return "call";
92 case Shl: return "shl";
93 case Shr: return "shr";
Chris Lattner8f77dae2003-05-08 02:44:12 +000094 case VarArg: return "va_arg";
95
Vikram S. Advec1056452002-07-14 23:09:40 +000096 default: return "<Invalid operator> ";
97 }
98
99 return 0;
100}
Chris Lattnerf2da7242002-10-31 04:14:01 +0000101
102
103/// isAssociative - Return true if the instruction is associative:
104///
105/// Associative operators satisfy: x op (y op z) === (x op y) op z)
106///
107/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
108/// applied to floating point types.
109///
110bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
111 if (Opcode == Add || Opcode == Mul ||
112 Opcode == And || Opcode == Or || Opcode == Xor) {
113 // Floating point operations do not associate!
114 return !Ty->isFloatingPoint();
115 }
116 return 0;
117}
118
119/// isCommutative - Return true if the instruction is commutative:
120///
121/// Commutative operators satistify: (x op y) === (y op x)
122///
123/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
124/// applied to any type.
125///
126bool Instruction::isCommutative(unsigned op) {
127 switch (op) {
128 case Add:
129 case Mul:
130 case And:
131 case Or:
132 case Xor:
133 case SetEQ:
134 case SetNE:
135 return true;
136 default:
137 return false;
138 }
139}
Tanya Lattner741bb002003-07-31 04:05:50 +0000140
141
142/// isTrappingInstruction - Return true if the instruction may trap.
143///
Tanya Lattnerec127bb2003-07-31 05:06:09 +0000144bool Instruction::isTrapping(unsigned op) {
Tanya Lattner741bb002003-07-31 04:05:50 +0000145 switch(op) {
146 case Div:
147 case Rem:
148 case Load:
149 case Store:
150 case Call:
151 case Invoke:
152 return true;
153 default:
154 return false;
155 }
156}