- Record that the debug info is actually used so that the label folder doesn't
[oota-llvm.git] / lib / VMCore / ValueSymbolTable.cpp
1 //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ValueSymbolTable class for the VMCore library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "valuesymtab"
15 #include "llvm/GlobalValue.h"
16 #include "llvm/Type.h"
17 #include "llvm/ValueSymbolTable.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/Support/Debug.h"
20 using namespace llvm;
21
22 // Class destructor
23 ValueSymbolTable::~ValueSymbolTable() {
24 #ifndef NDEBUG   // Only do this in -g mode...
25   for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
26     cerr << "Value still in symbol table! Type = '"
27          << VI->getValue()->getType()->getDescription() << "' Name = '"
28          << VI->getKeyData() << "'\n";
29   assert(vmap.empty() && "Values remain in symbol table!");
30 #endif
31 }
32
33 // lookup a value - Returns null on failure...
34 //
35 Value *ValueSymbolTable::lookup(const std::string &Name) const {
36   const_iterator VI = vmap.find(&Name[0], &Name[Name.size()]);
37   if (VI != vmap.end())                   // We found the symbol
38     return VI->getValue();
39   return 0;
40 }
41
42 Value *ValueSymbolTable::lookup(const char *NameBegin,
43                                 const char *NameEnd) const {
44   const_iterator VI = vmap.find(NameBegin, NameEnd);
45   if (VI != vmap.end())                   // We found the symbol
46     return VI->getValue();
47   return 0;
48 }
49
50 // Insert a value into the symbol table with the specified name...
51 //
52 void ValueSymbolTable::reinsertValue(Value* V) {
53   assert(V->hasName() && "Can't insert nameless Value into symbol table");
54
55   // Try inserting the name, assuming it won't conflict.
56   if (vmap.insert(V->Name)) {
57     //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
58     return;
59   }
60   
61   // Otherwise, there is a naming conflict.  Rename this value.
62   SmallString<128> UniqueName(V->getNameStart(), V->getNameEnd());
63
64   // The name is too already used, just free it so we can allocate a new name.
65   V->Name->Destroy();
66   
67   unsigned BaseSize = UniqueName.size();
68   while (1) {
69     // Trim any suffix off.
70     UniqueName.resize(BaseSize);
71     UniqueName.append_uint_32(++LastUnique);
72     // Try insert the vmap entry with this suffix.
73     ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
74                                                &UniqueName[UniqueName.size()]);
75     if (NewName.getValue() == 0) {
76       // Newly inserted name.  Success!
77       NewName.setValue(V);
78       V->Name = &NewName;
79       //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
80       return;
81     }
82   }
83 }
84
85 void ValueSymbolTable::removeValueName(ValueName *V) {
86   //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
87   // Remove the value from the symbol table.
88   vmap.remove(V);
89 }
90
91 /// createValueName - This method attempts to create a value name and insert
92 /// it into the symbol table with the specified name.  If it conflicts, it
93 /// auto-renames the name and returns that instead.
94 ValueName *ValueSymbolTable::createValueName(const char *NameStart,
95                                              unsigned NameLen, Value *V) {
96   // In the common case, the name is not already in the symbol table.
97   ValueName &Entry = vmap.GetOrCreateValue(NameStart, NameStart+NameLen);
98   if (Entry.getValue() == 0) {
99     Entry.setValue(V);
100     //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
101     //           << *V << "\n");
102     return &Entry;
103   }
104   
105   // Otherwise, there is a naming conflict.  Rename this value.
106   SmallString<128> UniqueName(NameStart, NameStart+NameLen);
107   
108   while (1) {
109     // Trim any suffix off.
110     UniqueName.resize(NameLen);
111     UniqueName.append_uint_32(++LastUnique);
112     
113     // Try insert the vmap entry with this suffix.
114     ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
115                                                &UniqueName[UniqueName.size()]);
116     if (NewName.getValue() == 0) {
117       // Newly inserted name.  Success!
118       NewName.setValue(V);
119       //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
120       return &NewName;
121     }
122   }
123 }
124
125
126 // dump - print out the symbol table
127 //
128 void ValueSymbolTable::dump() const {
129   //DOUT << "ValueSymbolTable:\n";
130   for (const_iterator I = begin(), E = end(); I != E; ++I) {
131     //DOUT << "  '" << I->getKeyData() << "' = ";
132     I->getValue()->dump();
133     //DOUT << "\n";
134   }
135 }