LLVM 20.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
16#include "llvm/IR/Module.h"
17#include "llvm/Support/MD5.h"
19
20using namespace llvm;
21
22namespace {
23// Compute a "unique" hash for the module based on the name of the public
24// globals.
25class ModuleHasher {
26 Module &TheModule;
27 std::string TheHash;
28
29public:
30 ModuleHasher(Module &M) : TheModule(M) {}
31
32 /// Return the lazily computed hash.
33 std::string &get() {
34 if (!TheHash.empty())
35 // Cache hit :)
36 return TheHash;
37
38 MD5 Hasher;
39 for (auto &F : TheModule) {
40 if (F.isDeclaration() || F.hasLocalLinkage() || !F.hasName())
41 continue;
42 auto Name = F.getName();
43 Hasher.update(Name);
44 }
45 for (auto &GV : TheModule.globals()) {
46 if (GV.isDeclaration() || GV.hasLocalLinkage() || !GV.hasName())
47 continue;
48 auto Name = GV.getName();
49 Hasher.update(Name);
50 }
51
52 // Now return the result.
53 MD5::MD5Result Hash;
54 Hasher.final(Hash);
56 MD5::stringifyResult(Hash, Result);
57 TheHash = std::string(Result);
58 return TheHash;
59 }
60};
61} // end anonymous namespace
62
63// Rename all the anon globals in the module
65 bool Changed = false;
66 ModuleHasher ModuleHash(M);
67 int count = 0;
68 auto RenameIfNeed = [&](GlobalValue &GV) {
69 if (GV.hasName())
70 return;
71 GV.setName(Twine("anon.") + ModuleHash.get() + "." + Twine(count++));
72 Changed = true;
73 };
74 for (auto &GO : M.global_objects())
75 RenameIfNeed(GO);
76 for (auto &GA : M.aliases())
77 RenameIfNeed(GA);
78
79 return Changed;
80}
81
84 if (!nameUnamedGlobals(M))
86
88}
std::string Name
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
This file defines the SmallString class.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Definition: MD5.h:41
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Definition: MD5.cpp:189
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
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
Definition: MD5.cpp:234
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:114
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
bool nameUnamedGlobals(Module &M)
Rename all the anon globals in the module using a hash computed from the list of public globals in th...
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:1938
std::array< uint32_t, 5 > ModuleHash
160 bits SHA1