Skip to content

Commit b392d30

Browse files
committed
Add a new method
llvm-svn: 18531
1 parent 0ba8f4e commit b392d30

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

llvm/include/llvm/Function.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ class Function : public GlobalValue, public Annotable {
106106
unsigned getIntrinsicID() const;
107107
bool isIntrinsic() const { return getIntrinsicID() != 0; }
108108

109+
/// renameLocalSymbols - This method goes through the Function's symbol table
110+
/// and renames any symbols that conflict with symbols at global scope. This
111+
/// is required before printing out to a textual form, to ensure that there is
112+
/// no ambiguity when parsing.
113+
void renameLocalSymbols();
114+
115+
109116
/// deleteBody - This method deletes the body of the function, and converts
110117
/// the linkage to external.
111118
///

llvm/lib/VMCore/Function.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/IntrinsicInst.h"
1818
#include "llvm/Support/LeakDetector.h"
1919
#include "SymbolTableListTraitsImpl.h"
20+
#include "llvm/ADT/StringExtras.h"
2021
using namespace llvm;
2122

2223
BasicBlock *ilist_traits<BasicBlock>::createNode() {
@@ -153,6 +154,46 @@ void Function::eraseFromParent() {
153154
getParent()->getFunctionList().erase(this);
154155
}
155156

157+
158+
/// renameLocalSymbols - This method goes through the Function's symbol table
159+
/// and renames any symbols that conflict with symbols at global scope. This is
160+
/// required before printing out to a textual form, to ensure that there is no
161+
/// ambiguity when parsing.
162+
void Function::renameLocalSymbols() {
163+
SymbolTable &LST = getSymbolTable(); // Local Symtab
164+
SymbolTable &GST = getParent()->getSymbolTable(); // Global Symtab
165+
166+
for (SymbolTable::plane_iterator LPI = LST.plane_begin(), E = LST.plane_end();
167+
LPI != E; ++LPI)
168+
// All global symbols are of pointer type, ignore any non-pointer planes.
169+
if (isa<PointerType>(LPI->first)) {
170+
// Only check if the global plane has any symbols of this type.
171+
SymbolTable::plane_iterator GPI = GST.find(LPI->first);
172+
if (GPI != GST.plane_end()) {
173+
SymbolTable::ValueMap &LVM = LPI->second;
174+
const SymbolTable::ValueMap &GVM = GPI->second;
175+
176+
// Loop over all local symbols, renaming those that are in the global
177+
// symbol table already.
178+
for (SymbolTable::value_iterator VI = LVM.begin(), E = LVM.end();
179+
VI != E;) {
180+
Value *V = VI->second;
181+
const std::string &Name = VI->first;
182+
++VI;
183+
if (GVM.count(Name)) {
184+
static unsigned UniqueNum = 0;
185+
// Find a name that does not conflict!
186+
while (GVM.count(Name + "_" + utostr(++UniqueNum)) ||
187+
LVM.count(Name + "_" + utostr(UniqueNum)))
188+
/* scan for UniqueNum that works */;
189+
V->setName(Name + "_" + utostr(UniqueNum));
190+
}
191+
}
192+
}
193+
}
194+
}
195+
196+
156197
// dropAllReferences() - This function causes all the subinstructions to "let
157198
// go" of all references that they are maintaining. This allows one to
158199
// 'delete' a whole class at a time, even though there may be circular

0 commit comments

Comments
 (0)