|
17 | 17 | #include "llvm/IntrinsicInst.h"
|
18 | 18 | #include "llvm/Support/LeakDetector.h"
|
19 | 19 | #include "SymbolTableListTraitsImpl.h"
|
| 20 | +#include "llvm/ADT/StringExtras.h" |
20 | 21 | using namespace llvm;
|
21 | 22 |
|
22 | 23 | BasicBlock *ilist_traits<BasicBlock>::createNode() {
|
@@ -153,6 +154,46 @@ void Function::eraseFromParent() {
|
153 | 154 | getParent()->getFunctionList().erase(this);
|
154 | 155 | }
|
155 | 156 |
|
| 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 | + |
156 | 197 | // dropAllReferences() - This function causes all the subinstructions to "let
|
157 | 198 | // go" of all references that they are maintaining. This allows one to
|
158 | 199 | // 'delete' a whole class at a time, even though there may be circular
|
|
0 commit comments