LLVM  17.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/Pass.h"
19 #include "llvm/Support/MD5.h"
21 
22 using namespace llvm;
23 
24 namespace {
25 // Compute a "unique" hash for the module based on the name of the public
26 // globals.
27 class ModuleHasher {
28  Module &TheModule;
29  std::string TheHash;
30 
31 public:
32  ModuleHasher(Module &M) : TheModule(M) {}
33 
34  /// Return the lazily computed hash.
35  std::string &get() {
36  if (!TheHash.empty())
37  // Cache hit :)
38  return TheHash;
39 
40  MD5 Hasher;
41  for (auto &F : TheModule) {
42  if (F.isDeclaration() || F.hasLocalLinkage() || !F.hasName())
43  continue;
44  auto Name = F.getName();
45  Hasher.update(Name);
46  }
47  for (auto &GV : TheModule.globals()) {
48  if (GV.isDeclaration() || GV.hasLocalLinkage() || !GV.hasName())
49  continue;
50  auto Name = GV.getName();
51  Hasher.update(Name);
52  }
53 
54  // Now return the result.
55  MD5::MD5Result Hash;
56  Hasher.final(Hash);
58  MD5::stringifyResult(Hash, Result);
59  TheHash = std::string(Result.str());
60  return TheHash;
61  }
62 };
63 } // end anonymous namespace
64 
65 // Rename all the anon globals in the module
67  bool Changed = false;
68  ModuleHasher ModuleHash(M);
69  int count = 0;
70  auto RenameIfNeed = [&](GlobalValue &GV) {
71  if (GV.hasName())
72  return;
73  GV.setName(Twine("anon.") + ModuleHash.get() + "." + Twine(count++));
74  Changed = true;
75  };
76  for (auto &GO : M.global_objects())
77  RenameIfNeed(GO);
78  for (auto &GA : M.aliases())
79  RenameIfNeed(GA);
80 
81  return Changed;
82 }
83 
86  if (!nameUnamedGlobals(M))
87  return PreservedAnalyses::all();
88 
89  return PreservedAnalyses::none();
90 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:189
llvm::MD5::stringifyResult
static void stringifyResult(MD5Result &Result, SmallVectorImpl< char > &Str)
Translates the bytes in Res to a hex string that is deposited into Str.
Definition: MD5.cpp:287
Pass.h
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
Module.h
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::get
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
Definition: PointerIntPair.h:234
llvm::MD5::final
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
Definition: MD5.cpp:234
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:66
MD5.h
SmallString.h
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
llvm::SmallString< 32 >
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:1896
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:65
NameAnonGlobals.h
llvm::MD5::MD5Result
Definition: MD5.h:43
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
llvm::NameAnonGlobalPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: NameAnonGlobals.cpp:84
ModuleUtils.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::ModuleHash
std::array< uint32_t, 5 > ModuleHash
160 bits SHA1
Definition: ModuleSummaryIndex.h:1154
InitializePasses.h