[llvm] r246017 - Reduce code duplication.

Alex Rosenberg via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 25 23:11:39 PDT 2015


Author: alexr
Date: Wed Aug 26 01:11:38 2015
New Revision: 246017

URL: http://llvm.org/viewvc/llvm-project?rev=246017&view=rev
Log:
Reduce code duplication.

Modified:
    llvm/trunk/lib/Transforms/Utils/MetaRenamer.cpp

Modified: llvm/trunk/lib/Transforms/Utils/MetaRenamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/MetaRenamer.cpp?rev=246017&r1=246016&r2=246017&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/MetaRenamer.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/MetaRenamer.cpp Wed Aug 26 01:11:38 2015
@@ -42,6 +42,24 @@ namespace {
     }
   };
 
+  static const char *const metaNames[] = {
+    // See http://en.wikipedia.org/wiki/Metasyntactic_variable
+    "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
+    "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
+  };
+
+  struct Renamer {
+    Renamer(unsigned int seed) {
+      prng.srand(seed);
+    }
+
+    const char *newName() {
+      return metaNames[prng.rand() % array_lengthof(metaNames)];
+    }
+
+    PRNG prng;
+  };
+  
   struct MetaRenamer : public ModulePass {
     static char ID; // Pass identification, replacement for typeid
     MetaRenamer() : ModulePass(ID) {
@@ -53,12 +71,6 @@ namespace {
     }
 
     bool runOnModule(Module &M) override {
-      static const char *const metaNames[] = {
-        // See http://en.wikipedia.org/wiki/Metasyntactic_variable
-        "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
-        "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
-      };
-
       // Seed our PRNG with simple additive sum of ModuleID. We're looking to
       // simply avoid always having the same function names, and we need to
       // remain deterministic.
@@ -67,9 +79,8 @@ namespace {
            E = M.getModuleIdentifier().end(); I != E; ++I)
         randSeed += *I;
 
-      PRNG prng;
-      prng.srand(randSeed);
-
+      Renamer renamer(randSeed);
+           
       // Rename all aliases
       for (Module::alias_iterator AI = M.alias_begin(), AE = M.alias_end();
            AI != AE; ++AI) {
@@ -98,8 +109,8 @@ namespace {
         if (STy->isLiteral() || STy->getName().empty()) continue;
 
         SmallString<128> NameStorage;
-        STy->setName((Twine("struct.") + metaNames[prng.rand() %
-                     array_lengthof(metaNames)]).toStringRef(NameStorage));
+        STy->setName((Twine("struct.") +
+          renamer.newName()).toStringRef(NameStorage));
       }
 
       // Rename all functions
@@ -109,7 +120,7 @@ namespace {
         if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
           continue;
 
-        FI->setName(metaNames[prng.rand() % array_lengthof(metaNames)]);
+        FI->setName(renamer.newName());
         runOnFunction(*FI);
       }
       return true;




More information about the llvm-commits mailing list