blob: 525f3f04d010ee617749cae3b06ef7b175d8a908 [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 Dunbare03eecb2009-07-25 23:55:21 +000046 SmallString<128> 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) {
Chris Lattner71a60ba2007-02-07 05:52:51 +000053 // Trim any suffix off.
54 UniqueName.resize(BaseSize);
Chris Lattner36b4e8f2008-06-27 21:26:26 +000055 UniqueName.append_uint_32(++LastUnique);
Chris Lattnerd17dbe92007-02-07 06:25:36 +000056 // Try insert the vmap entry with this suffix.
Jay Foad7d0479f2009-05-21 09:52:38 +000057 ValueName &NewName =
Daniel Dunbar5bf72e22009-07-23 18:17:34 +000058 vmap.GetOrCreateValue(StringRef(UniqueName.data(),
59 UniqueName.size()));
Chris Lattner32ab6432007-02-12 05:18:08 +000060 if (NewName.getValue() == 0) {
61 // Newly inserted name. Success!
62 NewName.setValue(V);
63 V->Name = &NewName;
Chris Lattnerb377a7c2007-02-25 20:42:59 +000064 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +000065 return;
66 }
67 }
68}
Reid Spencer25780d52006-01-10 09:51:48 +000069
Chris Lattner32ab6432007-02-12 05:18:08 +000070void ValueSymbolTable::removeValueName(ValueName *V) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +000071 //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner36b4e8f2008-06-27 21:26:26 +000072 // Remove the value from the symbol table.
Chris Lattner32ab6432007-02-12 05:18:08 +000073 vmap.remove(V);
74}
75
76/// createValueName - This method attempts to create a value name and insert
77/// it into the symbol table with the specified name. If it conflicts, it
78/// auto-renames the name and returns that instead.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000079ValueName *ValueSymbolTable::createValueName(const StringRef &Name, Value *V) {
Chris Lattner36b4e8f2008-06-27 21:26:26 +000080 // In the common case, the name is not already in the symbol table.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000081 ValueName &Entry = vmap.GetOrCreateValue(Name);
Chris Lattner32ab6432007-02-12 05:18:08 +000082 if (Entry.getValue() == 0) {
83 Entry.setValue(V);
Chris Lattnerb377a7c2007-02-25 20:42:59 +000084 //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
85 // << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +000086 return &Entry;
87 }
Chris Lattnerd17dbe92007-02-07 06:25:36 +000088
Chris Lattner32ab6432007-02-12 05:18:08 +000089 // Otherwise, there is a naming conflict. Rename this value.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000090 SmallString<128> UniqueName(Name.begin(), Name.end());
Chris Lattner36b4e8f2008-06-27 21:26:26 +000091
Chris Lattner32ab6432007-02-12 05:18:08 +000092 while (1) {
93 // Trim any suffix off.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000094 UniqueName.resize(Name.size());
Chris Lattner36b4e8f2008-06-27 21:26:26 +000095 UniqueName.append_uint_32(++LastUnique);
96
Chris Lattner32ab6432007-02-12 05:18:08 +000097 // Try insert the vmap entry with this suffix.
Jay Foad7d0479f2009-05-21 09:52:38 +000098 ValueName &NewName =
Daniel Dunbar5bf72e22009-07-23 18:17:34 +000099 vmap.GetOrCreateValue(StringRef(UniqueName.data(),
100 UniqueName.size()));
Chris Lattner32ab6432007-02-12 05:18:08 +0000101 if (NewName.getValue() == 0) {
102 // Newly inserted name. Success!
103 NewName.setValue(V);
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000104 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000105 return &NewName;
106 }
107 }
Reid Spencer25780d52006-01-10 09:51:48 +0000108}
109
Reid Spencer25780d52006-01-10 09:51:48 +0000110
111// dump - print out the symbol table
112//
113void ValueSymbolTable::dump() const {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000114 //DOUT << "ValueSymbolTable:\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000115 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000116 //DOUT << " '" << I->getKeyData() << "' = ";
Chris Lattner32ab6432007-02-12 05:18:08 +0000117 I->getValue()->dump();
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000118 //DOUT << "\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000119 }
Reid Spencer25780d52006-01-10 09:51:48 +0000120}