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