blob: 8579ad98c1bd51fded77e57b28e0d7576745b33f [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"
Daniel Dunbarb99eac82009-07-24 10:05:20 +000020#include "llvm/Support/raw_ostream.h"
Reid Spencer25780d52006-01-10 09:51:48 +000021using namespace llvm;
22
Reid Spencer25780d52006-01-10 09:51:48 +000023// Class destructor
24ValueSymbolTable::~ValueSymbolTable() {
25#ifndef NDEBUG // Only do this in -g mode...
Reid Spencer25780d52006-01-10 09:51:48 +000026 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
Daniel Dunbarb99eac82009-07-24 10:05:20 +000027 errs() << "Value still in symbol table! Type = '"
28 << VI->getValue()->getType()->getDescription() << "' Name = '"
29 << VI->getKeyData() << "'\n";
Chris Lattner86fc0812007-02-07 06:28:48 +000030 assert(vmap.empty() && "Values remain in symbol table!");
Reid Spencer25780d52006-01-10 09:51:48 +000031#endif
32}
33
Reid Spencer25780d52006-01-10 09:51:48 +000034// Insert a value into the symbol table with the specified name...
35//
Chris Lattner32ab6432007-02-12 05:18:08 +000036void ValueSymbolTable::reinsertValue(Value* V) {
Reid Spencer25780d52006-01-10 09:51:48 +000037 assert(V->hasName() && "Can't insert nameless Value into symbol table");
38
Chris Lattner770dadf2007-02-07 05:22:49 +000039 // Try inserting the name, assuming it won't conflict.
Chris Lattner32ab6432007-02-12 05:18:08 +000040 if (vmap.insert(V->Name)) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +000041 //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
Chris Lattner770dadf2007-02-07 05:22:49 +000042 return;
43 }
44
45 // Otherwise, there is a naming conflict. Rename this value.
Daniel Dunbar948f82b2009-08-19 19:22:52 +000046 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
Chris Lattner36b4e8f2008-06-27 21:26:26 +000047
48 // The name is too already used, just free it so we can allocate a new name.
Chris Lattner32ab6432007-02-12 05:18:08 +000049 V->Name->Destroy();
50
Chris Lattner71a60ba2007-02-07 05:52:51 +000051 unsigned BaseSize = UniqueName.size();
Chris Lattner32ab6432007-02-12 05:18:08 +000052 while (1) {
Daniel Dunbar948f82b2009-08-19 19:22:52 +000053 // Trim any suffix off and append the next number.
Chris Lattner71a60ba2007-02-07 05:52:51 +000054 UniqueName.resize(BaseSize);
Daniel Dunbar948f82b2009-08-19 19:22:52 +000055 raw_svector_ostream(UniqueName) << ++LastUnique;
56
Chris Lattnerd17dbe92007-02-07 06:25:36 +000057 // Try insert the vmap entry with this suffix.
Jay Foad7d0479f2009-05-21 09:52:38 +000058 ValueName &NewName =
Daniel Dunbar5bf72e22009-07-23 18:17:34 +000059 vmap.GetOrCreateValue(StringRef(UniqueName.data(),
60 UniqueName.size()));
Chris Lattner32ab6432007-02-12 05:18:08 +000061 if (NewName.getValue() == 0) {
62 // Newly inserted name. Success!
63 NewName.setValue(V);
64 V->Name = &NewName;
Chris Lattnerb377a7c2007-02-25 20:42:59 +000065 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +000066 return;
67 }
68 }
69}
Reid Spencer25780d52006-01-10 09:51:48 +000070
Chris Lattner32ab6432007-02-12 05:18:08 +000071void ValueSymbolTable::removeValueName(ValueName *V) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +000072 //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner36b4e8f2008-06-27 21:26:26 +000073 // Remove the value from the symbol table.
Chris Lattner32ab6432007-02-12 05:18:08 +000074 vmap.remove(V);
75}
76
77/// createValueName - This method attempts to create a value name and insert
78/// it into the symbol table with the specified name. If it conflicts, it
79/// auto-renames the name and returns that instead.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000080ValueName *ValueSymbolTable::createValueName(const StringRef &Name, Value *V) {
Chris Lattner36b4e8f2008-06-27 21:26:26 +000081 // In the common case, the name is not already in the symbol table.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000082 ValueName &Entry = vmap.GetOrCreateValue(Name);
Chris Lattner32ab6432007-02-12 05:18:08 +000083 if (Entry.getValue() == 0) {
84 Entry.setValue(V);
Chris Lattnerb377a7c2007-02-25 20:42:59 +000085 //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
86 // << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +000087 return &Entry;
88 }
Chris Lattnerd17dbe92007-02-07 06:25:36 +000089
Chris Lattner32ab6432007-02-12 05:18:08 +000090 // Otherwise, there is a naming conflict. Rename this value.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000091 SmallString<128> UniqueName(Name.begin(), Name.end());
Chris Lattner36b4e8f2008-06-27 21:26:26 +000092
Chris Lattner32ab6432007-02-12 05:18:08 +000093 while (1) {
Daniel Dunbar948f82b2009-08-19 19:22:52 +000094 // Trim any suffix off and append the next number.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000095 UniqueName.resize(Name.size());
Daniel Dunbar948f82b2009-08-19 19:22:52 +000096 raw_svector_ostream(UniqueName) << ++LastUnique;
Chris Lattner36b4e8f2008-06-27 21:26:26 +000097
Chris Lattner32ab6432007-02-12 05:18:08 +000098 // Try insert the vmap entry with this suffix.
Jay Foad7d0479f2009-05-21 09:52:38 +000099 ValueName &NewName =
Daniel Dunbar5bf72e22009-07-23 18:17:34 +0000100 vmap.GetOrCreateValue(StringRef(UniqueName.data(),
101 UniqueName.size()));
Chris Lattner32ab6432007-02-12 05:18:08 +0000102 if (NewName.getValue() == 0) {
103 // Newly inserted name. Success!
104 NewName.setValue(V);
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000105 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000106 return &NewName;
107 }
108 }
Reid Spencer25780d52006-01-10 09:51:48 +0000109}
110
Reid Spencer25780d52006-01-10 09:51:48 +0000111
112// dump - print out the symbol table
113//
114void ValueSymbolTable::dump() const {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000115 //DOUT << "ValueSymbolTable:\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000116 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000117 //DOUT << " '" << I->getKeyData() << "' = ";
Chris Lattner32ab6432007-02-12 05:18:08 +0000118 I->getValue()->dump();
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000119 //DOUT << "\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000120 }
Reid Spencer25780d52006-01-10 09:51:48 +0000121}