- Remove Value::use_remove
[oota-llvm.git] / lib / VMCore / Instruction.cpp
1 //===-- Instruction.cpp - Implement the Instruction class --------*- C++ -*--=//
2 //
3 // This file implements the Instruction class for the VMCore library.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Function.h"
8 #include "llvm/SymbolTable.h"
9 #include "llvm/Type.h"
10 #include "Support/LeakDetector.h"
11
12 Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
13                          Instruction *InsertBefore)
14   : User(ty, Value::InstructionVal, Name) {
15   Parent = 0;
16   iType = it;
17
18   // Make sure that we get added to a basicblock
19   LeakDetector::addGarbageObject(this);
20
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   }
27 }
28
29 void Instruction::setParent(BasicBlock *P) {
30   if (getParent())
31     LeakDetector::addGarbageObject(this);
32
33   Parent = P;
34
35   if (getParent())
36     LeakDetector::removeGarbageObject(this);
37 }
38
39 // Specialize setName to take care of symbol table majik
40 void Instruction::setName(const std::string &name, SymbolTable *ST) {
41   BasicBlock *P = 0; Function *PP = 0;
42   assert((ST == 0 || !getParent() || !getParent()->getParent() || 
43           ST == getParent()->getParent()->getSymbolTable()) &&
44          "Invalid symtab argument!");
45   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 }
50
51
52 const char *Instruction::getOpcodeName(unsigned OpCode) {
53   switch (OpCode) {
54   // Terminators
55   case Ret:    return "ret";
56   case Br:     return "br";
57   case Switch: return "switch";
58   case Invoke: return "invoke";
59     
60   // 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 }