From 835f217ab824794d8bc4f51f32018ef2c16b5450 Mon Sep 17 00:00:00 2001
From: Simon Schuster <git@rationality.eu>
Date: Wed, 19 Apr 2017 13:55:05 +0200
Subject: [PATCH] Rename functions with local linkage to unique identifiers

Hacky. Renames functions with local or private linkage to
"$funcname_sanitize($modulename)". Sanitize does 's/[^a-zA-Z0-9_]/_/g'
---
 lib/CodeGen/PMLExport.cpp | 53 ++++++++++++++++++++++++++++-----------
 1 file changed, 39 insertions(+), 14 deletions(-)

diff --git a/lib/CodeGen/PMLExport.cpp b/lib/CodeGen/PMLExport.cpp
index 7069819084f..53212fb3ea8 100644
--- a/lib/CodeGen/PMLExport.cpp
+++ b/lib/CodeGen/PMLExport.cpp
@@ -42,6 +42,7 @@
 
 #include <sstream>
 #include <string>
+#include <ctype.h>
 
 using namespace llvm;
 
@@ -232,20 +233,6 @@ void PMLBitcodeExport::serialize(MachineFunction &MF)
 
   if (!Fn) return;
 
-  // Tempoorary fixup: Take a _mutable_ reference and assign unique names to unsigned basic blocks
-  // FIXME: Hacky, as it casts away const
-  Function *MFn = const_cast<Function *>(Fn);
-  unsigned int tempid = 0;
-  for (Function::iterator BI = MFn->begin(), BE = MFn->end(); BI != BE;
-      ++BI) {
-    if (BI->getName().empty()) {
-      llvm::errs() << "warning: unnamed bit-code BB in PML export, assigning unique tempname\n";
-      std::stringstream ss;
-      ss << "Unnamed" << tempid++;
-      BI->setName(ss.str());
-    }
-  }
-
   LoopInfo &LI = P.getAnalysis<LoopInfoWrapperPass>(*const_cast<Function*>(Fn)).getLoopInfo();
   ScalarEvolution &SE =
     P.getAnalysis<ScalarEvolutionWrapperPass>(*const_cast<Function*>(Fn)).getSE();
@@ -1229,6 +1216,44 @@ bool PMLModuleExportPass::runOnMachineModule(const Module &M)
   }
 }
 
+  // Fixups for pml export in bigger projects...
+  // TODO: Move to own function
+  for (MachineFunction *MF : Queue) {
+    // FIXME: Hacky, as we cast away const. Here be dragons
+    Function *Fn = const_cast<Function*>(MF->getFunction());
+
+    if (!Fn) continue;
+
+    // Tempoorary fixup: Take a _mutable_ reference and assign unique names to
+    // unsigned basic blocks
+    Function *MFn = const_cast<Function *>(Fn);
+    unsigned int tempid = 0;
+    for (Function::iterator BI = MFn->begin(), BE = MFn->end(); BI != BE;
+        ++BI) {
+      if (BI->getName().empty()) {
+        llvm::errs() << "warning: unnamed bit-code BB in PML export,"
+                     << " assigning local unique tempname\n";
+        std::stringstream ss;
+        ss << "Unnamed" << tempid++;
+        BI->setName(ss.str());
+      }
+    }
+
+    // Another fixup: Rename static (inline) functions to prohibit name
+    // collissions in pml files
+    if (Fn->hasInternalLinkage() || Fn->hasPrivateLinkage()) {
+        std::stringstream ss;
+        std::string modulename = M.getName().str();
+        // Sanitize the module name to valid ELF symbols
+        replace_if(modulename.begin(), modulename.end(), [](const char &a){
+                return !isalnum(a);
+            }, '_');
+        ss << Fn->getName().str() << "_" << modulename;
+        Fn->setName(ss.str());
+    }
+
+  }
+
   // follow roots until no new methods are found
   while (!Queue.empty()) {
     MachineFunction *MF = Queue.front();
-- 
GitLab