Value.cpp revision ee976f33713016a96e3fbb394b7d0c5465be25d7
1//===-- Value.cpp - Implement the Value class -----------------------------===//
2//
3// This file implements the Value class.
4//
5//===----------------------------------------------------------------------===//
6
7#include "llvm/ValueHolderImpl.h"
8#include "llvm/InstrTypes.h"
9#include "llvm/SymbolTable.h"
10#include "llvm/SymTabValue.h"
11#include "llvm/ConstantPool.h"
12#include "llvm/ConstPoolVals.h"
13#include "llvm/Type.h"
14#ifndef NDEBUG      // Only in -g mode...
15#include "llvm/Assembly/Writer.h"
16#endif
17#include <algorithm>
18
19//===----------------------------------------------------------------------===//
20//                                Value Class
21//===----------------------------------------------------------------------===//
22
23Value::Value(const Type *ty, ValueTy vty, const string &name = "") : Name(name){
24  Ty = ty;
25  VTy = vty;
26}
27
28Value::~Value() {
29#ifndef NDEBUG      // Only in -g mode...
30  // Check to make sure that there are no uses of this value that are still
31  // around when the value is destroyed.  If there are, then we have a dangling
32  // reference and something is wrong.  This code is here to print out what is
33  // still being referenced.  The value in question should be printed as
34  // a <badref>
35  //
36  if (Uses.begin() != Uses.end()) {
37    for (use_const_iterator I = Uses.begin(); I != Uses.end(); I++)
38      cerr << "Use still stuck around after Def is destroyed:" << *I << endl;
39  }
40#endif
41  assert(Uses.begin() == Uses.end());
42}
43
44void Value::replaceAllUsesWith(Value *D) {
45  assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
46  while (!Uses.empty()) {
47    User *Use = Uses.front();
48#ifndef NDEBUG
49    unsigned NumUses = Uses.size();
50#endif
51    Use->replaceUsesOfWith(this, D);
52
53#ifndef NDEBUG      // only in -g mode...
54    if (Uses.size() == NumUses)
55      cerr << "Use: " << Use << "replace with: " << D;
56#endif
57    assert(Uses.size() != NumUses && "Didn't remove definition!");
58  }
59}
60
61void Value::killUse(User *i) {
62  if (i == 0) return;
63  use_iterator I = find(Uses.begin(), Uses.end(), i);
64
65  assert(I != Uses.end() && "Use not in uses list!!");
66  Uses.erase(I);
67}
68
69User *Value::use_remove(use_iterator &I) {
70  assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
71  User *i = *I;
72  I = Uses.erase(I);
73  return i;
74}
75
76
77//===----------------------------------------------------------------------===//
78//                                 User Class
79//===----------------------------------------------------------------------===//
80
81User::User(const Type *Ty, ValueTy vty, const string &name)
82  : Value(Ty, vty, name) {
83}
84
85// replaceUsesOfWith - Replaces all references to the "From" definition with
86// references to the "To" definition.
87//
88void User::replaceUsesOfWith(Value *From, Value *To) {
89  if (From == To) return;   // Duh what?
90
91  for (unsigned OpNum = 0; Value *D = getOperand(OpNum); OpNum++) {
92    if (D == From) {  // Okay, this operand is pointing to our fake def.
93      // The side effects of this setOperand call include linking to
94      // "To", adding "this" to the uses list of To, and
95      // most importantly, removing "this" from the use list of "From".
96      setOperand(OpNum, To); // Fix it now...
97    }
98  }
99}
100
101
102//===----------------------------------------------------------------------===//
103//                             SymTabValue Class
104//===----------------------------------------------------------------------===//
105
106// Instantiate Templates - This ugliness is the price we have to pay
107// for having a ValueHolderImpl.h file seperate from ValueHolder.h!  :(
108//
109template class ValueHolder<ConstPoolVal, SymTabValue>;
110
111SymTabValue::SymTabValue(const Type *Ty, ValueTy dty, const string &name = "")
112  : Value(Ty, dty, name), ConstPool(this) {
113  ParentSymTab = SymTab = 0;
114}
115
116
117SymTabValue::~SymTabValue() {
118  ConstPool.dropAllReferences();
119  ConstPool.delete_all();
120  ConstPool.setParent(0);
121
122  delete SymTab;
123}
124
125void SymTabValue::setParentSymTab(SymbolTable *ST) {
126  ParentSymTab = ST;
127  if (SymTab)
128    SymTab->setParentSymTab(ST);
129}
130
131SymbolTable *SymTabValue::getSymbolTableSure() {
132  if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
133  return SymTab;
134}
135
136// hasSymbolTable() - Returns true if there is a symbol table allocated to
137// this object AND if there is at least one name in it!
138//
139bool SymTabValue::hasSymbolTable() const {
140  if (!SymTab) return false;
141
142  for (SymbolTable::const_iterator I = SymTab->begin();
143       I != SymTab->end(); I++) {
144    if (I->second.begin() != I->second.end())
145      return true;                                // Found nonempty type plane!
146  }
147
148  return false;
149}
150