blob: 95267aacd02154a5bdfaefdfd0208aa44014f53b [file] [log] [blame]
Chris Lattner2f7c9632001-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 Lattner62b7fd12002-04-07 20:49:59 +00007#include "llvm/Function.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +00008#include "llvm/SymbolTable.h"
Chris Lattner113f4f42002-06-25 16:13:24 +00009#include "llvm/Type.h"
Chris Lattner184b2982002-09-08 18:59:35 +000010#include "Support/LeakDetector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000011
Chris Lattner3c787442002-09-10 15:45:53 +000012Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
13 Instruction *InsertBefore)
Chris Lattner4b361372002-02-03 07:52:58 +000014 : User(ty, Value::InstructionVal, Name) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000015 Parent = 0;
16 iType = it;
Chris Lattner184b2982002-09-08 18:59:35 +000017
18 // Make sure that we get added to a basicblock
19 LeakDetector::addGarbageObject(this);
Chris Lattner3c787442002-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 Lattner2f7c9632001-06-06 20:29:01 +000027}
28
Chris Lattner9ed7aef2002-09-06 21:33:15 +000029void Instruction::setParent(BasicBlock *P) {
Chris Lattner184b2982002-09-08 18:59:35 +000030 if (getParent())
31 LeakDetector::addGarbageObject(this);
32
Chris Lattner9ed7aef2002-09-06 21:33:15 +000033 Parent = P;
Chris Lattner184b2982002-09-08 18:59:35 +000034
35 if (getParent())
36 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000037}
38
Chris Lattner2f7c9632001-06-06 20:29:01 +000039// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000040void Instruction::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner62b7fd12002-04-07 20:49:59 +000041 BasicBlock *P = 0; Function *PP = 0;
Chris Lattnere8dd6ad2001-09-07 16:47:03 +000042 assert((ST == 0 || !getParent() || !getParent()->getParent() ||
43 ST == getParent()->getParent()->getSymbolTable()) &&
44 "Invalid symtab argument!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000045 if ((P = getParent()) && (PP = P->getParent()) && hasName())
46 PP->getSymbolTable()->remove(this);
47 Value::setName(name);
48 if (PP && hasName()) PP->getSymbolTableSure()->insert(this);
49}
Vikram S. Adve8aee7962002-07-14 23:09:40 +000050
51
52const char *Instruction::getOpcodeName(unsigned OpCode) {
53 switch (OpCode) {
54 // Terminators
Chris Lattnerb193ff82002-08-14 18:18:02 +000055 case Ret: return "ret";
56 case Br: return "br";
Vikram S. Adve8aee7962002-07-14 23:09:40 +000057 case Switch: return "switch";
58 case Invoke: return "invoke";
59
Vikram S. Adve8aee7962002-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";
94
95 default: return "<Invalid operator> ";
96 }
97
98 return 0;
99}
Chris Lattnercab6c332002-10-31 04:14:01 +0000100
101
102/// isAssociative - Return true if the instruction is associative:
103///
104/// Associative operators satisfy: x op (y op z) === (x op y) op z)
105///
106/// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
107/// applied to floating point types.
108///
109bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
110 if (Opcode == Add || Opcode == Mul ||
111 Opcode == And || Opcode == Or || Opcode == Xor) {
112 // Floating point operations do not associate!
113 return !Ty->isFloatingPoint();
114 }
115 return 0;
116}
117
118/// isCommutative - Return true if the instruction is commutative:
119///
120/// Commutative operators satistify: (x op y) === (y op x)
121///
122/// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
123/// applied to any type.
124///
125bool Instruction::isCommutative(unsigned op) {
126 switch (op) {
127 case Add:
128 case Mul:
129 case And:
130 case Or:
131 case Xor:
132 case SetEQ:
133 case SetNE:
134 return true;
135 default:
136 return false;
137 }
138}