more space; NFC
[oota-llvm.git] / lib / Transforms / Utils / SplitModule.cpp
1 //===- SplitModule.cpp - Split a module into partitions -------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the function llvm::SplitModule, which splits a module
11 // into multiple linkable partitions. It can be used to implement parallel code
12 // generation for link-time optimization.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Transforms/Utils/SplitModule.h"
17 #include "llvm/ADT/Hashing.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/GlobalAlias.h"
20 #include "llvm/IR/GlobalObject.h"
21 #include "llvm/IR/GlobalValue.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/MD5.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Transforms/Utils/Cloning.h"
26
27 using namespace llvm;
28
29 static void externalize(GlobalValue *GV) {
30   if (GV->hasLocalLinkage()) {
31     GV->setLinkage(GlobalValue::ExternalLinkage);
32     GV->setVisibility(GlobalValue::HiddenVisibility);
33   }
34
35   // Unnamed entities must be named consistently between modules. setName will
36   // give a distinct name to each such entity.
37   if (!GV->hasName())
38     GV->setName("__llvmsplit_unnamed");
39 }
40
41 // Returns whether GV should be in partition (0-based) I of N.
42 static bool isInPartition(const GlobalValue *GV, unsigned I, unsigned N) {
43   if (auto GA = dyn_cast<GlobalAlias>(GV))
44     if (const GlobalObject *Base = GA->getBaseObject())
45       GV = Base;
46
47   StringRef Name;
48   if (const Comdat *C = GV->getComdat())
49     Name = C->getName();
50   else
51     Name = GV->getName();
52
53   // Partition by MD5 hash. We only need a few bits for evenness as the number
54   // of partitions will generally be in the 1-2 figure range; the low 16 bits
55   // are enough.
56   MD5 H;
57   MD5::MD5Result R;
58   H.update(Name);
59   H.final(R);
60   return (R[0] | (R[1] << 8)) % N == I;
61 }
62
63 void llvm::SplitModule(
64     std::unique_ptr<Module> M, unsigned N,
65     std::function<void(std::unique_ptr<Module> MPart)> ModuleCallback) {
66   for (Function &F : *M)
67     externalize(&F);
68   for (GlobalVariable &GV : M->globals())
69     externalize(&GV);
70   for (GlobalAlias &GA : M->aliases())
71     externalize(&GA);
72
73   // FIXME: We should be able to reuse M as the last partition instead of
74   // cloning it.
75   for (unsigned I = 0; I != N; ++I) {
76     ValueToValueMapTy VMap;
77     std::unique_ptr<Module> MPart(
78         CloneModule(M.get(), VMap, [=](const GlobalValue *GV) {
79           return isInPartition(GV, I, N);
80         }));
81     if (I != 0)
82       MPart->setModuleInlineAsm("");
83     ModuleCallback(std::move(MPart));
84   }
85 }