blob: f87bb631c8df549447ca03b62213c6c10ed8fe20 [file] [log] [blame]
Reid Spencer25780d52006-01-10 09:51:48 +00001//===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer25780d52006-01-10 09:51:48 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ValueSymbolTable class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000014#define DEBUG_TYPE "valuesymtab"
Reid Spencer25780d52006-01-10 09:51:48 +000015#include "llvm/GlobalValue.h"
16#include "llvm/Type.h"
17#include "llvm/ValueSymbolTable.h"
Chris Lattner36b4e8f2008-06-27 21:26:26 +000018#include "llvm/ADT/SmallString.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000019#include "llvm/Support/Debug.h"
Reid Spencer25780d52006-01-10 09:51:48 +000020using namespace llvm;
21
Reid Spencer25780d52006-01-10 09:51:48 +000022// Class destructor
23ValueSymbolTable::~ValueSymbolTable() {
24#ifndef NDEBUG // Only do this in -g mode...
Reid Spencer25780d52006-01-10 09:51:48 +000025 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
Chris Lattnerb377a7c2007-02-25 20:42:59 +000026 cerr << "Value still in symbol table! Type = '"
27 << VI->getValue()->getType()->getDescription() << "' Name = '"
28 << VI->getKeyData() << "'\n";
Chris Lattner86fc0812007-02-07 06:28:48 +000029 assert(vmap.empty() && "Values remain in symbol table!");
Reid Spencer25780d52006-01-10 09:51:48 +000030#endif
31}
32
Reid Spencer25780d52006-01-10 09:51:48 +000033// Insert a value into the symbol table with the specified name...
34//
Chris Lattner32ab6432007-02-12 05:18:08 +000035void ValueSymbolTable::reinsertValue(Value* V) {
Reid Spencer25780d52006-01-10 09:51:48 +000036 assert(V->hasName() && "Can't insert nameless Value into symbol table");
37
Chris Lattner770dadf2007-02-07 05:22:49 +000038 // Try inserting the name, assuming it won't conflict.
Chris Lattner32ab6432007-02-12 05:18:08 +000039 if (vmap.insert(V->Name)) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +000040 //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
Chris Lattner770dadf2007-02-07 05:22:49 +000041 return;
42 }
43
44 // Otherwise, there is a naming conflict. Rename this value.
Chris Lattner36b4e8f2008-06-27 21:26:26 +000045 SmallString<128> UniqueName(V->getNameStart(), V->getNameEnd());
46
47 // The name is too already used, just free it so we can allocate a new name.
Chris Lattner32ab6432007-02-12 05:18:08 +000048 V->Name->Destroy();
49
Chris Lattner71a60ba2007-02-07 05:52:51 +000050 unsigned BaseSize = UniqueName.size();
Chris Lattner32ab6432007-02-12 05:18:08 +000051 while (1) {
Chris Lattner71a60ba2007-02-07 05:52:51 +000052 // Trim any suffix off.
53 UniqueName.resize(BaseSize);
Chris Lattner36b4e8f2008-06-27 21:26:26 +000054 UniqueName.append_uint_32(++LastUnique);
Chris Lattnerd17dbe92007-02-07 06:25:36 +000055 // Try insert the vmap entry with this suffix.
Jay Foad7d0479f2009-05-21 09:52:38 +000056 ValueName &NewName =
Daniel Dunbar5bf72e22009-07-23 18:17:34 +000057 vmap.GetOrCreateValue(StringRef(UniqueName.data(),
58 UniqueName.size()));
Chris Lattner32ab6432007-02-12 05:18:08 +000059 if (NewName.getValue() == 0) {
60 // Newly inserted name. Success!
61 NewName.setValue(V);
62 V->Name = &NewName;
Chris Lattnerb377a7c2007-02-25 20:42:59 +000063 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +000064 return;
65 }
66 }
67}
Reid Spencer25780d52006-01-10 09:51:48 +000068
Chris Lattner32ab6432007-02-12 05:18:08 +000069void ValueSymbolTable::removeValueName(ValueName *V) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +000070 //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner36b4e8f2008-06-27 21:26:26 +000071 // Remove the value from the symbol table.
Chris Lattner32ab6432007-02-12 05:18:08 +000072 vmap.remove(V);
73}
74
75/// createValueName - This method attempts to create a value name and insert
76/// it into the symbol table with the specified name. If it conflicts, it
77/// auto-renames the name and returns that instead.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000078ValueName *ValueSymbolTable::createValueName(const StringRef &Name, Value *V) {
Chris Lattner36b4e8f2008-06-27 21:26:26 +000079 // In the common case, the name is not already in the symbol table.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000080 ValueName &Entry = vmap.GetOrCreateValue(Name);
Chris Lattner32ab6432007-02-12 05:18:08 +000081 if (Entry.getValue() == 0) {
82 Entry.setValue(V);
Chris Lattnerb377a7c2007-02-25 20:42:59 +000083 //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
84 // << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +000085 return &Entry;
86 }
Chris Lattnerd17dbe92007-02-07 06:25:36 +000087
Chris Lattner32ab6432007-02-12 05:18:08 +000088 // Otherwise, there is a naming conflict. Rename this value.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000089 SmallString<128> UniqueName(Name.begin(), Name.end());
Chris Lattner36b4e8f2008-06-27 21:26:26 +000090
Chris Lattner32ab6432007-02-12 05:18:08 +000091 while (1) {
92 // Trim any suffix off.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000093 UniqueName.resize(Name.size());
Chris Lattner36b4e8f2008-06-27 21:26:26 +000094 UniqueName.append_uint_32(++LastUnique);
95
Chris Lattner32ab6432007-02-12 05:18:08 +000096 // Try insert the vmap entry with this suffix.
Jay Foad7d0479f2009-05-21 09:52:38 +000097 ValueName &NewName =
Daniel Dunbar5bf72e22009-07-23 18:17:34 +000098 vmap.GetOrCreateValue(StringRef(UniqueName.data(),
99 UniqueName.size()));
Chris Lattner32ab6432007-02-12 05:18:08 +0000100 if (NewName.getValue() == 0) {
101 // Newly inserted name. Success!
102 NewName.setValue(V);
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000103 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000104 return &NewName;
105 }
106 }
Reid Spencer25780d52006-01-10 09:51:48 +0000107}
108
Reid Spencer25780d52006-01-10 09:51:48 +0000109
110// dump - print out the symbol table
111//
112void ValueSymbolTable::dump() const {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000113 //DOUT << "ValueSymbolTable:\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000114 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000115 //DOUT << " '" << I->getKeyData() << "' = ";
Chris Lattner32ab6432007-02-12 05:18:08 +0000116 I->getValue()->dump();
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000117 //DOUT << "\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000118 }
Reid Spencer25780d52006-01-10 09:51:48 +0000119}