LLVM  12.0.0git
UniqueInternalLinkageNames.cpp
Go to the documentation of this file.
1 //===- UniqueInternalLinkageNames.cpp - Unique Internal Linkage Sym Names -===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements unique naming of internal linkage symbols with option
10 // -funique-internal-linkage-symbols.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/InitializePasses.h"
18 #include "llvm/Support/MD5.h"
20 
21 using namespace llvm;
22 
24  llvm::MD5 Md5;
25  Md5.update(M.getSourceFileName());
27  Md5.final(R);
28  SmallString<32> Str;
30  std::string ModuleNameHash = (Twine(".") + Twine(Str)).str();
31  bool Changed = false;
32 
33  // Append the module hash to all internal linkage functions.
34  for (auto &F : M) {
35  if (F.hasInternalLinkage()) {
36  F.setName(F.getName() + ModuleNameHash);
37  Changed = true;
38  }
39  }
40 
41  // Append the module hash to all internal linkage globals.
42  for (auto &GV : M.globals()) {
43  if (GV.hasInternalLinkage()) {
44  GV.setName(GV.getName() + ModuleNameHash);
45  Changed = true;
46  }
47  }
48  return Changed;
49 }
50 
51 namespace {
52 
53 // Legacy pass that provides a name to every anon globals.
54 class UniqueInternalLinkageNamesLegacyPass : public ModulePass {
55 
56 public:
57  /// Pass identification, replacement for typeid
58  static char ID;
59 
60  /// Specify pass name for debug output
61  StringRef getPassName() const override {
62  return "Unique Internal Linkage Names";
63  }
64 
65  explicit UniqueInternalLinkageNamesLegacyPass() : ModulePass(ID) {
68  }
69 
70  bool runOnModule(Module &M) override {
72  }
73 };
74 
76 } // anonymous namespace
77 
81  return PreservedAnalyses::all();
82 
83  return PreservedAnalyses::none();
84 }
85 
86 INITIALIZE_PASS_BEGIN(UniqueInternalLinkageNamesLegacyPass,
87  "unique-internal-linkage-names",
88  "Uniqueify internal linkage names", false, false)
89 INITIALIZE_PASS_END(UniqueInternalLinkageNamesLegacyPass,
90  "unique-internal-linkage-names",
91  "Uniqueify Internal linkage names", false, false)
92 
93 namespace llvm {
95  return new UniqueInternalLinkageNamesLegacyPass();
96 }
97 } // namespace llvm
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:67
static void stringifyResult(MD5Result &Result, SmallString< 32 > &Str)
Translates the bytes in Res to a hex string that is deposited into Str.
Definition: MD5.cpp:273
F(f)
void initializeUniqueInternalLinkageNamesLegacyPassPass(PassRegistry &)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Definition: MD5.cpp:190
const std::string & getSourceFileName() const
Get the module&#39;s original source file name.
Definition: Module.h:230
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:157
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
INITIALIZE_PASS_BEGIN(UniqueInternalLinkageNamesLegacyPass, "unique-internal-linkage-names", "Uniqueify internal linkage names", false, false) INITIALIZE_PASS_END(UniqueInternalLinkageNamesLegacyPass
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
static bool uniqueifyInternalLinkageNames(Module &M)
ModulePass * createUniqueInternalLinkageNamesPass()
Module.h This file contains the declarations for the Module class.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:224
Definition: MD5.h:41
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
Definition: MD5.cpp:235
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
A container for analyses that lazily runs them and caches their results.