gollvm: improve naming for address space casts

Create more meaningful names for address-space casts of alloca
instructions (no change in functionality, just improves IR
readability).

Change-Id: Ibe64dd6c51a834c5a5dc136d6f5377d39ad508dd
Reviewed-on: https://go-review.googlesource.com/c/154197
Reviewed-by: Cherry Zhang <cherryyz@google.com>
diff --git a/bridge/CMakeLists.txt b/bridge/CMakeLists.txt
index f779f90..4db88f0 100644
--- a/bridge/CMakeLists.txt
+++ b/bridge/CMakeLists.txt
@@ -52,6 +52,7 @@
   go-llvm-typemanager.cpp
   go-llvm.cpp
   go-sha1.cpp
+  namegen.cpp
 )
 
 add_dependencies(LLVMCppGoFrontEnd intrinsics_gen)
diff --git a/bridge/go-llvm-bfunction.cpp b/bridge/go-llvm-bfunction.cpp
index e3fbe6b..216214c 100644
--- a/bridge/go-llvm-bfunction.cpp
+++ b/bridge/go-llvm-bfunction.cpp
@@ -87,6 +87,10 @@
   if (addressSpace != 0) {
     llvm::Type *pt = llvm::PointerType::get(typ, addressSpace);
     inst = new llvm::AddrSpaceCastInst(inst, pt);
+    if (! name.empty()) {
+      std::string castname(NameGen::combineTags(name, ".ascast"));
+      inst->setName(namegen(castname));
+    }
     allocas_.push_back(inst);
   }
 
diff --git a/bridge/namegen.cpp b/bridge/namegen.cpp
new file mode 100644
index 0000000..76a94db
--- /dev/null
+++ b/bridge/namegen.cpp
@@ -0,0 +1,51 @@
+//===-- namegen.cpp -------------------------------------------------------===//
+//
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+//
+//===----------------------------------------------------------------------===//
+
+// Implementation of NameGen helper class.
+
+#include "namegen.h"
+
+  // For creating useful type, inst and block names.
+std::string NameGen::namegen(const std::string &tag,
+                             unsigned expl)
+{
+  auto it = nametags_.find(tag);
+  unsigned count = 0;
+  if (it != nametags_.end())
+    count = it->second + 1;
+  if (expl != ChooseVer)
+    count = expl;
+  std::stringstream ss;
+  ss << tag << "." << count;
+  if (expl == ChooseVer)
+    nametags_[tag] = count;
+  return ss.str();
+}
+
+static std::string chopNumericSuffix(const std::string &tag)
+{
+  std::string res;
+  auto dot = tag.rfind('.');
+  if (dot == std::string::npos || dot == tag.size()-1)
+    return tag;
+  dot += 1;
+  if (!std::isdigit(tag.at(dot)))
+    return tag;
+  auto found = tag.find_first_not_of("0123456789", dot);
+  if (found != std::string::npos)
+    return tag;
+  return tag.substr(0, dot-1);
+}
+
+std::string NameGen::combineTags(const std::string &baseTag,
+                                 const std::string &suffix)
+{
+  std::string res(chopNumericSuffix(baseTag));
+  res += suffix;
+  return res;
+}
diff --git a/bridge/namegen.h b/bridge/namegen.h
index 67acbbd..1bcaf0b 100644
--- a/bridge/namegen.h
+++ b/bridge/namegen.h
@@ -13,6 +13,10 @@
 #ifndef LLVMGOFRONTEND_NAMEGEN_H
 #define LLVMGOFRONTEND_NAMEGEN_H
 
+#include <string>
+#include <sstream>
+#include <unordered_map>
+
 class NameGen {
  public:
   NameGen() { }
@@ -21,19 +25,12 @@
   static constexpr unsigned ChooseVer = 0xffffffff;
 
   // For creating useful type, inst and block names.
-  std::string namegen(const std::string &tag, unsigned expl = ChooseVer) {
-    auto it = nametags_.find(tag);
-    unsigned count = 0;
-    if (it != nametags_.end())
-      count = it->second + 1;
-    if (expl != ChooseVer)
-      count = expl;
-    std::stringstream ss;
-    ss << tag << "." << count;
-    if (expl == ChooseVer)
-      nametags_[tag] = count;
-    return ss.str();
-  }
+  std::string namegen(const std::string &tag, unsigned expl = ChooseVer);
+
+  // Form a new tag name based on an existing tag and a suffix. If
+  // the existing tag is versioned, strip off the version.
+  static std::string combineTags(const std::string &baseTag,
+                                 const std::string &suffix);
 
   NameGen *nameTags() {
     return const_cast<NameGen*>(this);
@@ -44,6 +41,4 @@
   std::unordered_map<std::string, unsigned> nametags_;
 };
 
-
-
 #endif // LLVMGOFRONTEND_TYPEMANAGER_H