Convert StringMap to using StringRef for its APIs.
[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);
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   // FIXME: ValueSymbolTable should move to a StringRef based API.
45   const_iterator VI = vmap.find(StringRef(NameBegin, NameEnd - NameBegin));
46   if (VI != vmap.end())                   // We found the symbol
47     return VI->getValue();
48   return 0;
49 }
50
51 // Insert a value into the symbol table with the specified name...
52 //
53 void ValueSymbolTable::reinsertValue(Value* V) {
54   assert(V->hasName() && "Can't insert nameless Value into symbol table");
55
56   // Try inserting the name, assuming it won't conflict.
57   if (vmap.insert(V->Name)) {
58     //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
59     return;
60   }
61   
62   // Otherwise, there is a naming conflict.  Rename this value.
63   SmallString<128> UniqueName(V->getNameStart(), V->getNameEnd());
64
65   // The name is too already used, just free it so we can allocate a new name.
66   V->Name->Destroy();
67   
68   unsigned BaseSize = UniqueName.size();
69   while (1) {
70     // Trim any suffix off.
71     UniqueName.resize(BaseSize);
72     UniqueName.append_uint_32(++LastUnique);
73     // Try insert the vmap entry with this suffix.
74     ValueName &NewName =
75       vmap.GetOrCreateValue(StringRef(UniqueName.data(),
76                                       UniqueName.size()));
77     if (NewName.getValue() == 0) {
78       // Newly inserted name.  Success!
79       NewName.setValue(V);
80       V->Name = &NewName;
81       //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
82       return;
83     }
84   }
85 }
86
87 void ValueSymbolTable::removeValueName(ValueName *V) {
88   //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
89   // Remove the value from the symbol table.
90   vmap.remove(V);
91 }
92
93 /// createValueName - This method attempts to create a value name and insert
94 /// it into the symbol table with the specified name.  If it conflicts, it
95 /// auto-renames the name and returns that instead.
96 ValueName *ValueSymbolTable::createValueName(const char *NameStart,
97                                              unsigned NameLen, Value *V) {
98   // In the common case, the name is not already in the symbol table.
99   ValueName &Entry = vmap.GetOrCreateValue(StringRef(NameStart, NameLen));
100   if (Entry.getValue() == 0) {
101     Entry.setValue(V);
102     //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
103     //           << *V << "\n");
104     return &Entry;
105   }
106   
107   // Otherwise, there is a naming conflict.  Rename this value.
108   SmallString<128> UniqueName(NameStart, NameStart+NameLen);
109   
110   while (1) {
111     // Trim any suffix off.
112     UniqueName.resize(NameLen);
113     UniqueName.append_uint_32(++LastUnique);
114     
115     // Try insert the vmap entry with this suffix.
116     ValueName &NewName =
117       vmap.GetOrCreateValue(StringRef(UniqueName.data(),
118                                       UniqueName.size()));
119     if (NewName.getValue() == 0) {
120       // Newly inserted name.  Success!
121       NewName.setValue(V);
122       //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
123       return &NewName;
124     }
125   }
126 }
127
128
129 // dump - print out the symbol table
130 //
131 void ValueSymbolTable::dump() const {
132   //DOUT << "ValueSymbolTable:\n";
133   for (const_iterator I = begin(), E = end(); I != E; ++I) {
134     //DOUT << "  '" << I->getKeyData() << "' = ";
135     I->getValue()->dump();
136     //DOUT << "\n";
137   }
138 }