51 #define DEBUG_TYPE "split-module" 63 assert((!isa<Constant>(U) || isa<GlobalValue>(U)) &&
"Bad user");
67 GVtoClusterMap.unionSets(GV, F);
68 }
else if (isa<GlobalIndirectSymbol>(U) || isa<Function>(U) ||
69 isa<GlobalVariable>(U)) {
70 GVtoClusterMap.unionSets(GV, cast<GlobalValue>(U));
79 for (
auto *U : V->
users()) {
82 while (!Worklist.
empty()) {
85 if (isa<Constant>(UU) && !isa<GlobalValue>(UU)) {
105 ClusterMapType GVtoClusterMap;
106 ComdatMembersType ComdatMembers;
108 auto recordGVSet = [&GVtoClusterMap, &ComdatMembers](
GlobalValue &GV) {
109 if (GV.isDeclaration())
113 GV.setName(
"__llvmsplit_unnamed");
119 if (
const Comdat *
C = GV.getComdat()) {
120 auto &Member = ComdatMembers[
C];
122 GVtoClusterMap.unionSets(Member, &GV);
129 if (
auto *GIS = dyn_cast<GlobalIndirectSymbol>(&GV)) {
131 GVtoClusterMap.unionSets(&GV,
Base);
134 if (
const Function *
F = dyn_cast<Function>(&GV)) {
143 if (GV.hasLocalLinkage())
153 auto CompareClusters = [](
const std::pair<unsigned, unsigned> &a,
154 const std::pair<unsigned, unsigned> &b) {
155 if (a.second || b.second)
156 return a.second > b.second;
158 return a.first > b.first;
161 std::priority_queue<std::pair<unsigned, unsigned>,
162 std::vector<std::pair<unsigned, unsigned>>,
163 decltype(CompareClusters)>
164 BalancinQueue(CompareClusters);
166 for (
unsigned i = 0; i <
N; ++i)
167 BalancinQueue.push(std::make_pair(i, 0));
169 using SortType = std::pair<unsigned, ClusterMapType::iterator>;
176 for (ClusterMapType::iterator
I = GVtoClusterMap.begin(),
177 E = GVtoClusterMap.end();
I !=
E; ++
I)
180 std::make_pair(std::distance(GVtoClusterMap.member_begin(
I),
181 GVtoClusterMap.member_end()),
I));
183 llvm::sort(Sets, [](
const SortType &a,
const SortType &b) {
184 if (a.first == b.first)
185 return a.second->getData()->getName() > b.second->getData()->getName();
187 return a.first > b.first;
190 for (
auto &
I : Sets) {
191 unsigned CurrentClusterID = BalancinQueue.top().first;
192 unsigned CurrentClusterSize = BalancinQueue.top().second;
195 LLVM_DEBUG(
dbgs() <<
"Root[" << CurrentClusterID <<
"] cluster_size(" 196 <<
I.first <<
") ----> " <<
I.second->getData()->getName()
199 for (ClusterMapType::member_iterator
MI =
200 GVtoClusterMap.findLeader(
I.second);
201 MI != GVtoClusterMap.member_end(); ++
MI) {
202 if (!Visited.
insert(*MI).second)
205 << ((*MI)->hasLocalLinkage() ?
" l " :
" e ") <<
"\n");
207 ClusterIDMap[*
MI] = CurrentClusterID;
208 CurrentClusterSize++;
211 BalancinQueue.push(std::make_pair(CurrentClusterID, CurrentClusterSize));
224 GV->
setName(
"__llvmsplit_unnamed");
229 if (
auto *GIS = dyn_cast<GlobalIndirectSymbol>(GV))
246 return (R[0] | (R[1] << 8)) % N ==
I;
250 std::unique_ptr<Module> M,
unsigned N,
251 function_ref<
void(std::unique_ptr<Module> MPart)> ModuleCallback,
252 bool PreserveLocals) {
253 if (!PreserveLocals) {
266 ClusterIDMapType ClusterIDMap;
271 for (
unsigned I = 0;
I <
N; ++
I) {
273 std::unique_ptr<Module> MPart(
275 if (ClusterIDMap.count(GV))
276 return (ClusterIDMap[GV] ==
I);
281 MPart->setModuleInlineAsm(
"");
282 ModuleCallback(std::move(MPart));
void setVisibility(VisibilityTypes V)
bool hasLocalLinkage() const
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
bool isConstantUsed() const
Return true if the constant has users other than constant expressions and other dangling things...
void push_back(const T &Elt)
void SplitModule(std::unique_ptr< Module > M, unsigned N, function_ref< void(std::unique_ptr< Module > MPart)> ModuleCallback, bool PreserveLocals=false)
Splits the module M into N linkable partitions.
An efficient, type-erasing, non-owning reference to a callable.
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM...
Externally visible function.
static void addAllGlobalValueUsers(ClusterMapType &GVtoClusterMap, const GlobalValue *GV, const Value *V)
static void externalize(GlobalValue *GV)
The address of a basic block.
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
void setName(const Twine &Name)
Change the name of the value.
static void findPartitions(Module *M, ClusterIDMapType &ClusterIDMap, unsigned N)
std::unique_ptr< Module > CloneModule(const Module &M)
Return an exact copy of the specified module.
iterator_range< iterator > functions()
LLVM Basic Block Representation.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void addNonConstUser(ClusterMapType &GVtoClusterMap, const GlobalValue *GV, const User *U)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
EquivalenceClasses - This represents a collection of equivalence classes and supports three efficient...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void sort(IteratorTy Start, IteratorTy End)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
LLVM_NODISCARD T pop_back_val()
void setLinkage(LinkageTypes LT)
static BlockAddress * lookup(const BasicBlock *BB)
Lookup an existing BlockAddress constant for the given BasicBlock.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
const Comdat * getComdat() const
iterator_range< user_iterator > users()
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
LLVM_NODISCARD bool empty() const
StringRef getName() const
Return a constant reference to the value's name.
static bool isInPartition(const GlobalValue *GV, unsigned I, unsigned N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
LLVM Value Representation.
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
iterator_range< global_iterator > globals()
StringRef - Represent a constant reference to a string, i.e.
UnaryPredicate for_each(R &&Range, UnaryPredicate P)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly...
iterator_range< alias_iterator > aliases()