-
-// rename - Given a value with a non-empty name, remove its existing entry
-// from the symbol table and insert a new one for Name. This is equivalent to
-// doing "remove(V), V->Name = Name, insert(V)",
-//
-bool ValueSymbolTable::rename(Value *V, const std::string &name) {
- assert(V && "Can't rename a null Value");
- assert(V->hasName() && "Can't rename a nameless Value");
- assert(!V->getName().empty() && "Can't rename an Value with null name");
- assert(V->getName() != name && "Can't rename a Value with same name");
- assert(!name.empty() && "Can't rename a named Value with a null name");
-
- // Find the name
- iterator VI = vmap.find(V->getName());
-
- // If we didn't find it, we're done
- if (VI == vmap.end())
- return false;
-
- // Remove the old entry.
- vmap.erase(VI);
-
- // See if we can insert the new name.
- VI = vmap.lower_bound(name);
-
- // Is there a naming conflict?
- if (VI != vmap.end() && VI->first == name) {
- V->Name = getUniqueName( name);
- vmap.insert(make_pair(V->Name, V));
- } else {
- V->Name = name;
- vmap.insert(VI, make_pair(V->Name, V));
+/// createValueName - This method attempts to create a value name and insert
+/// it into the symbol table with the specified name. If it conflicts, it
+/// auto-renames the name and returns that instead.
+ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
+ // In the common case, the name is not already in the symbol table.
+ ValueName &Entry = vmap.GetOrCreateValue(Name);
+ if (Entry.getValue() == 0) {
+ Entry.setValue(V);
+ //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
+ // << *V << "\n");
+ return &Entry;
+ }
+
+ // Otherwise, there is a naming conflict. Rename this value.
+ SmallString<256> UniqueName(Name.begin(), Name.end());
+
+ while (1) {
+ // Trim any suffix off and append the next number.
+ UniqueName.resize(Name.size());
+ raw_svector_ostream(UniqueName) << ++LastUnique;
+
+ // Try insert the vmap entry with this suffix.
+ ValueName &NewName = vmap.GetOrCreateValue(UniqueName);
+ if (NewName.getValue() == 0) {
+ // Newly inserted name. Success!
+ NewName.setValue(V);
+ //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
+ return &NewName;
+ }