blob: 2e22bc3641aea224cfedd4a2ccddbde73703062e [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 Lattner4b74c832003-11-20 17:45:12 +000018using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000019
Chris Lattner2aa83112002-09-10 15:45:53 +000020Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
21 Instruction *InsertBefore)
Chris Lattner71947fd2002-02-03 07:52:58 +000022 : User(ty, Value::InstructionVal, Name) {
Chris Lattner00950542001-06-06 20:29:01 +000023 Parent = 0;
24 iType = it;
Chris Lattnerd1e693f2002-09-08 18:59:35 +000025
26 // Make sure that we get added to a basicblock
27 LeakDetector::addGarbageObject(this);
Chris Lattner2aa83112002-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 Lattner00950542001-06-06 20:29:01 +000035}
36
Chris Lattnerbded1322002-09-06 21:33:15 +000037void Instruction::setParent(BasicBlock *P) {
Chris Lattnerd1e693f2002-09-08 18:59:35 +000038 if (getParent())
39 LeakDetector::addGarbageObject(this);
40
Chris Lattnerbded1322002-09-06 21:33:15 +000041 Parent = P;
Chris Lattnerd1e693f2002-09-08 18:59:35 +000042
43 if (getParent())
44 LeakDetector::removeGarbageObject(this);
Chris Lattnerbded1322002-09-06 21:33:15 +000045}
46
Chris Lattner00950542001-06-06 20:29:01 +000047// Specialize setName to take care of symbol table majik
Chris Lattner697954c2002-01-20 22:54:45 +000048void Instruction::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000049 BasicBlock *P = 0; Function *PP = 0;
Chris Lattnere05bf2f2001-09-07 16:47:03 +000050 assert((ST == 0 || !getParent() || !getParent()->getParent() ||
Chris Lattner6e6026b2002-11-20 18:36:02 +000051 ST == &getParent()->getParent()->getSymbolTable()) &&
Chris Lattnere05bf2f2001-09-07 16:47:03 +000052 "Invalid symtab argument!");
Chris Lattner00950542001-06-06 20:29:01 +000053 if ((P = getParent()) && (PP = P->getParent()) && hasName())
Chris Lattner6e6026b2002-11-20 18:36:02 +000054 PP->getSymbolTable().remove(this);
Chris Lattner00950542001-06-06 20:29:01 +000055 Value::setName(name);
Chris Lattner6e6026b2002-11-20 18:36:02 +000056 if (PP && hasName()) PP->getSymbolTable().insert(this);
Chris Lattner00950542001-06-06 20:29:01 +000057}
Vikram S. Advec1056452002-07-14 23:09:40 +000058
59
60const char *Instruction::getOpcodeName(unsigned OpCode) {
61 switch (OpCode) {
62 // Terminators
Chris Lattner0513e9f2002-08-14 18:18:02 +000063 case Ret: return "ret";
64 case Br: return "br";
Vikram S. Advec1056452002-07-14 23:09:40 +000065 case Switch: return "switch";
66 case Invoke: return "invoke";
Chris Lattnera6ce8982003-09-08 18:54:36 +000067 case Unwind: return "unwind";
Vikram S. Advec1056452002-07-14 23:09:40 +000068
Vikram S. Advec1056452002-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 Lattner3b237fc2003-10-19 21:34:28 +000098 case PHI: return "phi";
Vikram S. Advec1056452002-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 Lattner4d45bd02003-10-18 05:57:43 +0000103 case VANext: return "vanext";
104 case VAArg: return "vaarg";
Chris Lattner8f77dae2003-05-08 02:44:12 +0000105
Vikram S. Advec1056452002-07-14 23:09:40 +0000106 default: return "<Invalid operator> ";
107 }
108
109 return 0;
110}
Chris Lattnerf2da7242002-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 Brukman6b634522003-10-10 17:54:14 +0000131/// Commutative operators satisfy: (x op y) === (y op x)
Chris Lattnerf2da7242002-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 Lattner741bb002003-07-31 04:05:50 +0000150
151
152/// isTrappingInstruction - Return true if the instruction may trap.
153///
Tanya Lattnerec127bb2003-07-31 05:06:09 +0000154bool Instruction::isTrapping(unsigned op) {
Tanya Lattner741bb002003-07-31 04:05:50 +0000155 switch(op) {
156 case Div:
157 case Rem:
158 case Load:
159 case Store:
160 case Call:
161 case Invoke:
162 return true;
163 default:
164 return false;
165 }
166}