blob: fffacb377770006024f4ee0b160dd96d66b21f85 [file] [log] [blame]
Reid Spencereb7116b2006-01-10 09:51:48 +00001//===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Spencereb7116b2006-01-10 09:51:48 +00007//
8//===----------------------------------------------------------------------===//
9//
Chandler Carruthc2c50cd2013-01-02 09:10:48 +000010// This file implements the ValueSymbolTable class for the IR library.
Reid Spencereb7116b2006-01-10 09:51:48 +000011//
12//===----------------------------------------------------------------------===//
13
Reid Spenceref9b9a72007-02-05 20:47:22 +000014#define DEBUG_TYPE "valuesymtab"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000015#include "llvm/IR/ValueSymbolTable.h"
Chris Lattner97388462008-06-27 21:26:26 +000016#include "llvm/ADT/SmallString.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/GlobalValue.h"
18#include "llvm/IR/Type.h"
Bill Wendling2e3def12006-11-17 08:03:48 +000019#include "llvm/Support/Debug.h"
Daniel Dunbarb95c2fd2009-07-24 10:05:20 +000020#include "llvm/Support/raw_ostream.h"
Reid Spencereb7116b2006-01-10 09:51:48 +000021using namespace llvm;
22
Reid Spencereb7116b2006-01-10 09:51:48 +000023// Class destructor
24ValueSymbolTable::~ValueSymbolTable() {
25#ifndef NDEBUG // Only do this in -g mode...
Reid Spencereb7116b2006-01-10 09:51:48 +000026 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
David Greeneefc25812010-01-05 01:30:06 +000027 dbgs() << "Value still in symbol table! Type = '"
Chris Lattner0cd0d882011-06-18 21:18:23 +000028 << *VI->getValue()->getType() << "' Name = '"
Daniel Dunbarb95c2fd2009-07-24 10:05:20 +000029 << VI->getKeyData() << "'\n";
Chris Lattner92f5b6a2007-02-07 06:28:48 +000030 assert(vmap.empty() && "Values remain in symbol table!");
Reid Spencereb7116b2006-01-10 09:51:48 +000031#endif
32}
33
Reid Spencereb7116b2006-01-10 09:51:48 +000034// Insert a value into the symbol table with the specified name...
35//
Chris Lattnerdec628e2007-02-12 05:18:08 +000036void ValueSymbolTable::reinsertValue(Value* V) {
Reid Spencereb7116b2006-01-10 09:51:48 +000037 assert(V->hasName() && "Can't insert nameless Value into symbol table");
38
Chris Lattnerf5111552007-02-07 05:22:49 +000039 // Try inserting the name, assuming it won't conflict.
Chris Lattnerdec628e2007-02-12 05:18:08 +000040 if (vmap.insert(V->Name)) {
David Greeneefc25812010-01-05 01:30:06 +000041 //DEBUG(dbgs() << " Inserted value: " << V->Name << ": " << *V << "\n");
Chris Lattnerf5111552007-02-07 05:22:49 +000042 return;
43 }
44
45 // Otherwise, there is a naming conflict. Rename this value.
Daniel Dunbar29c14202009-08-19 19:22:52 +000046 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
Chris Lattner97388462008-06-27 21:26:26 +000047
48 // The name is too already used, just free it so we can allocate a new name.
Chris Lattnerdec628e2007-02-12 05:18:08 +000049 V->Name->Destroy();
50
Chris Lattner421d3da2007-02-07 05:52:51 +000051 unsigned BaseSize = UniqueName.size();
Chris Lattnerdec628e2007-02-12 05:18:08 +000052 while (1) {
Daniel Dunbar29c14202009-08-19 19:22:52 +000053 // Trim any suffix off and append the next number.
Chris Lattner421d3da2007-02-07 05:52:51 +000054 UniqueName.resize(BaseSize);
Daniel Dunbar29c14202009-08-19 19:22:52 +000055 raw_svector_ostream(UniqueName) << ++LastUnique;
56
Chris Lattnerb4d8d622007-02-07 06:25:36 +000057 // Try insert the vmap entry with this suffix.
Benjamin Kramer4ef68222010-03-31 16:04:26 +000058 ValueName &NewName = vmap.GetOrCreateValue(UniqueName);
Chris Lattnerdec628e2007-02-12 05:18:08 +000059 if (NewName.getValue() == 0) {
60 // Newly inserted name. Success!
61 NewName.setValue(V);
62 V->Name = &NewName;
David Greeneefc25812010-01-05 01:30:06 +000063 //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattnerdec628e2007-02-12 05:18:08 +000064 return;
65 }
66 }
67}
Reid Spencereb7116b2006-01-10 09:51:48 +000068
Chris Lattnerdec628e2007-02-12 05:18:08 +000069void ValueSymbolTable::removeValueName(ValueName *V) {
David Greeneefc25812010-01-05 01:30:06 +000070 //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner97388462008-06-27 21:26:26 +000071 // Remove the value from the symbol table.
Chris Lattnerdec628e2007-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 Dunbar2928c832009-11-06 10:58:06 +000078ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
Chris Lattner97388462008-06-27 21:26:26 +000079 // In the common case, the name is not already in the symbol table.
Daniel Dunbarbc8d8132009-07-23 18:52:12 +000080 ValueName &Entry = vmap.GetOrCreateValue(Name);
Chris Lattnerdec628e2007-02-12 05:18:08 +000081 if (Entry.getValue() == 0) {
82 Entry.setValue(V);
David Greeneefc25812010-01-05 01:30:06 +000083 //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
Chris Lattner3f3098c2007-02-25 20:42:59 +000084 // << *V << "\n");
Chris Lattnerdec628e2007-02-12 05:18:08 +000085 return &Entry;
86 }
Chris Lattnerb4d8d622007-02-07 06:25:36 +000087
Chris Lattnerdec628e2007-02-12 05:18:08 +000088 // Otherwise, there is a naming conflict. Rename this value.
Benjamin Kramer4ef68222010-03-31 16:04:26 +000089 SmallString<256> UniqueName(Name.begin(), Name.end());
Chris Lattner97388462008-06-27 21:26:26 +000090
Chris Lattnerdec628e2007-02-12 05:18:08 +000091 while (1) {
Daniel Dunbar29c14202009-08-19 19:22:52 +000092 // Trim any suffix off and append the next number.
Daniel Dunbarbc8d8132009-07-23 18:52:12 +000093 UniqueName.resize(Name.size());
Daniel Dunbar29c14202009-08-19 19:22:52 +000094 raw_svector_ostream(UniqueName) << ++LastUnique;
Chris Lattner97388462008-06-27 21:26:26 +000095
Chris Lattnerdec628e2007-02-12 05:18:08 +000096 // Try insert the vmap entry with this suffix.
Benjamin Kramer4ef68222010-03-31 16:04:26 +000097 ValueName &NewName = vmap.GetOrCreateValue(UniqueName);
Chris Lattnerdec628e2007-02-12 05:18:08 +000098 if (NewName.getValue() == 0) {
99 // Newly inserted name. Success!
100 NewName.setValue(V);
David Greeneefc25812010-01-05 01:30:06 +0000101 //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattnerdec628e2007-02-12 05:18:08 +0000102 return &NewName;
103 }
104 }
Reid Spencereb7116b2006-01-10 09:51:48 +0000105}
106
Reid Spencereb7116b2006-01-10 09:51:48 +0000107
108// dump - print out the symbol table
109//
110void ValueSymbolTable::dump() const {
David Greeneefc25812010-01-05 01:30:06 +0000111 //DEBUG(dbgs() << "ValueSymbolTable:\n");
Chris Lattnerdec628e2007-02-12 05:18:08 +0000112 for (const_iterator I = begin(), E = end(); I != E; ++I) {
David Greeneefc25812010-01-05 01:30:06 +0000113 //DEBUG(dbgs() << " '" << I->getKeyData() << "' = ");
Chris Lattnerdec628e2007-02-12 05:18:08 +0000114 I->getValue()->dump();
David Greeneefc25812010-01-05 01:30:06 +0000115 //DEBUG(dbgs() << "\n");
Chris Lattnerdec628e2007-02-12 05:18:08 +0000116 }
Reid Spencereb7116b2006-01-10 09:51:48 +0000117}