LLVM  14.0.0git
NameAnonGlobals.cpp
Go to the documentation of this file.
1 //===- NameAnonGlobals.cpp - ThinLTO Support: Name Unnamed Globals --------===//
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 naming anonymous globals to make sure they can be
10 // referred to by ThinLTO.
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 
23 namespace {
24 // Compute a "unique" hash for the module based on the name of the public
25 // globals.
26 class ModuleHasher {
27  Module &TheModule;
28  std::string TheHash;
29 
30 public:
31  ModuleHasher(Module &M) : TheModule(M) {}
32 
33  /// Return the lazily computed hash.
34  std::string &get() {
35  if (!TheHash.empty())
36  // Cache hit :)
37  return TheHash;
38 
39  MD5 Hasher;
40  for (auto &F : TheModule) {
41  if (F.isDeclaration() || F.hasLocalLinkage() || !F.hasName())
42  continue;
43  auto Name = F.getName();
44  Hasher.update(Name);
45  }
46  for (auto &GV : TheModule.globals()) {
47  if (GV.isDeclaration() || GV.hasLocalLinkage() || !GV.hasName())
48  continue;
49  auto Name = GV.getName();
50  Hasher.update(Name);
51  }
52 
53  // Now return the result.
54  MD5::MD5Result Hash;
55  Hasher.final(Hash);
57  MD5::stringifyResult(Hash, Result);
58  TheHash = std::string(Result.str());
59  return TheHash;
60  }
61 };
62 } // end anonymous namespace
63 
64 // Rename all the anon globals in the module
66  bool Changed = false;
67  ModuleHasher ModuleHash(M);
68  int count = 0;
69  auto RenameIfNeed = [&](GlobalValue &GV) {
70  if (GV.hasName())
71  return;
72  GV.setName(Twine("anon.") + ModuleHash.get() + "." + Twine(count++));
73  Changed = true;
74  };
75  for (auto &GO : M.global_objects())
76  RenameIfNeed(GO);
77  for (auto &GA : M.aliases())
78  RenameIfNeed(GA);
79 
80  return Changed;
81 }
82 
83 namespace {
84 
85 // Legacy pass that provides a name to every anon globals.
86 class NameAnonGlobalLegacyPass : public ModulePass {
87 
88 public:
89  /// Pass identification, replacement for typeid
90  static char ID;
91 
92  /// Specify pass name for debug output
93  StringRef getPassName() const override { return "Name Anon Globals"; }
94 
95  explicit NameAnonGlobalLegacyPass() : ModulePass(ID) {}
96 
97  bool runOnModule(Module &M) override { return nameUnamedGlobals(M); }
98 };
100 
101 } // anonymous namespace
102 
104  ModuleAnalysisManager &AM) {
105  if (!nameUnamedGlobals(M))
106  return PreservedAnalyses::all();
107 
108  return PreservedAnalyses::none();
109 }
110 
111 INITIALIZE_PASS_BEGIN(NameAnonGlobalLegacyPass, "name-anon-globals",
112  "Provide a name to nameless globals", false, false)
113 INITIALIZE_PASS_END(NameAnonGlobalLegacyPass, "name-anon-globals",
114  "Provide a name to nameless globals", false, false)
115 
116 namespace llvm {
118  return new NameAnonGlobalLegacyPass();
119 }
120 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::MD5::update
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Definition: MD5.cpp:190
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
llvm::MD5::stringifyResult
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:290
to
Should compile to
Definition: README.txt:449
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:158
Module.h
llvm::createNameAnonGlobalPass
ModulePass * createNameAnonGlobalPass()
===------------------------------------------------------------------—===//
Definition: NameAnonGlobals.cpp:117
F
#define F(x, y, z)
Definition: MD5.cpp:56
a
=0.0 ? 0.0 :(a > 0.0 ? 1.0 :-1.0) a
Definition: README.txt:489
llvm::MD5::final
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
Definition: MD5.cpp:235
llvm::nameUnamedGlobals
bool nameUnamedGlobals(Module &M)
Rename all the anon globals in the module using a hash computed from the list of public globals in th...
Definition: NameAnonGlobals.cpp:65
MD5.h
SmallString.h
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
false
Definition: StackSlotColoring.cpp:142
llvm::SmallString< 32 >
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::count
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1634
llvm::MD5
Definition: MD5.h:41
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
NameAnonGlobals.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::MD5::MD5Result
Definition: MD5.h:43
name
static const char * name
Definition: SVEIntrinsicOpts.cpp:78
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
get
Should compile to something r4 addze r3 instead we get
Definition: README.txt:24
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(NameAnonGlobalLegacyPass, "name-anon-globals", "Provide a name to nameless globals", false, false) INITIALIZE_PASS_END(NameAnonGlobalLegacyPass
globals
name anon globals
Definition: NameAnonGlobals.cpp:113
llvm::NameAnonGlobalPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: NameAnonGlobals.cpp:103
ModuleUtils.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::ModuleHash
std::array< uint32_t, 5 > ModuleHash
160 bits SHA1
Definition: ModuleSummaryIndex.h:983
InitializePasses.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37