-
-// 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(const char *NameStart,
+ unsigned NameLen, Value *V) {
+ ValueName &Entry = vmap.GetOrCreateValue(NameStart, NameStart+NameLen);
+ if (Entry.getValue() == 0) {
+ Entry.setValue(V);
+ DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
+ << *V << "\n");
+ return &Entry;
+ }
+
+ // FIXME: this could be much more efficient.
+
+ // Otherwise, there is a naming conflict. Rename this value.
+ std::string UniqueName(NameStart, NameStart+NameLen);
+ while (1) {
+ // Trim any suffix off.
+ UniqueName.resize(NameLen);
+ UniqueName += utostr(++LastUnique);
+ // Try insert the vmap entry with this suffix.
+ ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
+ &UniqueName[UniqueName.size()]);
+ if (NewName.getValue() == 0) {
+ // Newly inserted name. Success!
+ NewName.setValue(V);
+ DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
+ return &NewName;
+ }