+// We dont want identifier names with ., space, - in them.
+// So we replace them with _
+static std::string makeNameProper(std::string x) {
+ std::string tmp;
+ for (std::string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
+ switch (*sI) {
+ case '.': tmp += "d_"; break;
+ case ' ': tmp += "s_"; break;
+ case '-': tmp += "D_"; break;
+ default: tmp += *sI;
+ }
+
+ return tmp;
+}
+
+std::string getValueName(const Value *V) {
+ if (V->hasName()) { // Print out the label if it exists...
+
+ // Name mangling occurs as follows:
+ // - If V is not a global, mangling always occurs.
+ // - Otherwise, mangling occurs when any of the following are true:
+ // 1) V has internal linkage
+ // 2) V's name would collide if it is not mangled.
+ //
+
+ if(const GlobalValue* gv = dyn_cast<GlobalValue>(V)) {
+ if(!gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
+ // No internal linkage, name will not collide -> no mangling.
+ return makeNameProper(gv->getName());
+ }
+ }
+
+ // Non-global, or global with internal linkage / colliding name -> mangle.
+ return "l" + utostr(V->getType()->getUniqueID()) + "_" +
+ makeNameProper(V->getName());
+ }
+
+ static int Count = 0;
+ Count++;
+ return "ltmp_" + itostr(Count) + "_" + utostr(V->getType()->getUniqueID());
+}
+
+