From: Alex Rosenberg Date: Wed, 26 Aug 2015 06:11:38 +0000 (+0000) Subject: Reduce code duplication. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=26a2a58413568dbcac8758dca719cff5c85df725;p=oota-llvm.git Reduce code duplication. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@246017 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Utils/MetaRenamer.cpp b/lib/Transforms/Utils/MetaRenamer.cpp index 54f3874507b..c5ccd7d0c8e 100644 --- a/lib/Transforms/Utils/MetaRenamer.cpp +++ b/lib/Transforms/Utils/MetaRenamer.cpp @@ -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;