Partial fix for PR1678: correct some parts of constant
[oota-llvm.git] / lib / VMCore / ValueSymbolTable.cpp
1 //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group.  It is distributed under 
6 // the University of Illinois Open Source 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/StringExtras.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 // getUniqueName - Given a base name, return a string that is either equal to
34 // it (or derived from it) that does not already occur in the symbol table for
35 // the specified type.
36 //
37 std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const {
38   std::string TryName = BaseName;
39
40   // See if the name exists
41   while (vmap.find(&TryName[0], &TryName[TryName.size()]) != vmap.end())
42     // Loop until we find a free name in the symbol table.
43     TryName = BaseName + utostr(++LastUnique);
44   return TryName;
45 }
46
47
48 // lookup a value - Returns null on failure...
49 //
50 Value *ValueSymbolTable::lookup(const std::string &Name) const {
51   const_iterator VI = vmap.find(&Name[0], &Name[Name.size()]);
52   if (VI != vmap.end())                   // We found the symbol
53     return VI->getValue();
54   return 0;
55 }
56
57 // Insert a value into the symbol table with the specified name...
58 //
59 void ValueSymbolTable::reinsertValue(Value* V) {
60   assert(V->hasName() && "Can't insert nameless Value into symbol table");
61
62   // Try inserting the name, assuming it won't conflict.
63   if (vmap.insert(V->Name)) {
64     //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
65     return;
66   }
67   
68   // FIXME: this could be much more efficient.
69   
70   // Otherwise, there is a naming conflict.  Rename this value.
71   std::string UniqueName = V->getName();
72   
73   V->Name->Destroy();
74   
75   unsigned BaseSize = UniqueName.size();
76   while (1) {
77     // Trim any suffix off.
78     UniqueName.resize(BaseSize);
79     UniqueName += utostr(++LastUnique);
80     // Try insert the vmap entry with this suffix.
81     ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
82                                                &UniqueName[UniqueName.size()]);
83     if (NewName.getValue() == 0) {
84       // Newly inserted name.  Success!
85       NewName.setValue(V);
86       V->Name = &NewName;
87       //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
88       return;
89     }
90   }
91 }
92
93 void ValueSymbolTable::removeValueName(ValueName *V) {
94   //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
95   // Remove the value from the plane.
96   vmap.remove(V);
97 }
98
99 /// createValueName - This method attempts to create a value name and insert
100 /// it into the symbol table with the specified name.  If it conflicts, it
101 /// auto-renames the name and returns that instead.
102 ValueName *ValueSymbolTable::createValueName(const char *NameStart,
103                                              unsigned NameLen, Value *V) {
104   ValueName &Entry = vmap.GetOrCreateValue(NameStart, NameStart+NameLen);
105   if (Entry.getValue() == 0) {
106     Entry.setValue(V);
107     //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
108     //           << *V << "\n");
109     return &Entry;
110   }
111   
112   // FIXME: this could be much more efficient.
113   
114   // Otherwise, there is a naming conflict.  Rename this value.
115   std::string UniqueName(NameStart, NameStart+NameLen);
116   while (1) {
117     // Trim any suffix off.
118     UniqueName.resize(NameLen);
119     UniqueName += utostr(++LastUnique);
120     // Try insert the vmap entry with this suffix.
121     ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
122                                                &UniqueName[UniqueName.size()]);
123     if (NewName.getValue() == 0) {
124       // Newly inserted name.  Success!
125       NewName.setValue(V);
126       //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
127       return &NewName;
128     }
129   }
130 }
131
132
133 // dump - print out the symbol table
134 //
135 void ValueSymbolTable::dump() const {
136   //DOUT << "ValueSymbolTable:\n";
137   for (const_iterator I = begin(), E = end(); I != E; ++I) {
138     //DOUT << "  '" << I->getKeyData() << "' = ";
139     I->getValue()->dump();
140     //DOUT << "\n";
141   }
142 }