blob: d2ce97abdd486064d6babd0d5463f75a40027962 [file] [log] [blame]
Peter Collingbourne1398a322016-12-16 00:26:30 +00001//===- ThinLTOBitcodeWriter.cpp - Bitcode writing pass for ThinLTO --------===//
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//===----------------------------------------------------------------------===//
Peter Collingbourne1398a322016-12-16 00:26:30 +00009
Tim Shen6b4114182017-06-01 01:02:12 +000010#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
Peter Collingbourne002c2d52017-02-14 03:42:38 +000011#include "llvm/Analysis/BasicAliasAnalysis.h"
Peter Collingbourne1398a322016-12-16 00:26:30 +000012#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Teresa Johnson94624ac2017-05-10 18:52:16 +000013#include "llvm/Analysis/ProfileSummaryInfo.h"
Peter Collingbourne1398a322016-12-16 00:26:30 +000014#include "llvm/Analysis/TypeMetadataUtils.h"
15#include "llvm/Bitcode/BitcodeWriter.h"
16#include "llvm/IR/Constants.h"
Peter Collingbourne28ffd322017-02-08 20:44:00 +000017#include "llvm/IR/DebugInfo.h"
Peter Collingbourne1398a322016-12-16 00:26:30 +000018#include "llvm/IR/Intrinsics.h"
19#include "llvm/IR/Module.h"
20#include "llvm/IR/PassManager.h"
21#include "llvm/Pass.h"
22#include "llvm/Support/ScopedPrinter.h"
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +000023#include "llvm/Support/raw_ostream.h"
24#include "llvm/Transforms/IPO.h"
Peter Collingbourne002c2d52017-02-14 03:42:38 +000025#include "llvm/Transforms/IPO/FunctionAttrs.h"
Peter Collingbourne1398a322016-12-16 00:26:30 +000026#include "llvm/Transforms/Utils/Cloning.h"
Evgeniy Stepanov964f4662017-04-27 20:27:27 +000027#include "llvm/Transforms/Utils/ModuleUtils.h"
Peter Collingbourne1398a322016-12-16 00:26:30 +000028using namespace llvm;
29
30namespace {
31
Peter Collingbourne1398a322016-12-16 00:26:30 +000032// Promote each local-linkage entity defined by ExportM and used by ImportM by
33// changing visibility and appending the given ModuleId.
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +000034void promoteInternals(Module &ExportM, Module &ImportM, StringRef ModuleId,
35 SetVector<GlobalValue *> &PromoteExtra) {
Bob Haarman4075ccc2017-04-12 01:43:07 +000036 DenseMap<const Comdat *, Comdat *> RenamedComdats;
Peter Collingbourne6b193962017-03-30 23:43:08 +000037 for (auto &ExportGV : ExportM.global_values()) {
Peter Collingbourne1398a322016-12-16 00:26:30 +000038 if (!ExportGV.hasLocalLinkage())
Peter Collingbourne6b193962017-03-30 23:43:08 +000039 continue;
Peter Collingbourne1398a322016-12-16 00:26:30 +000040
Bob Haarman4075ccc2017-04-12 01:43:07 +000041 auto Name = ExportGV.getName();
Peter Collingbourne1f034222017-11-30 23:05:52 +000042 GlobalValue *ImportGV = nullptr;
43 if (!PromoteExtra.count(&ExportGV)) {
44 ImportGV = ImportM.getNamedValue(Name);
45 if (!ImportGV)
46 continue;
47 ImportGV->removeDeadConstantUsers();
48 if (ImportGV->use_empty()) {
49 ImportGV->eraseFromParent();
50 continue;
51 }
52 }
Peter Collingbourne1398a322016-12-16 00:26:30 +000053
Bob Haarman4075ccc2017-04-12 01:43:07 +000054 std::string NewName = (Name + ModuleId).str();
55
56 if (const auto *C = ExportGV.getComdat())
57 if (C->getName() == Name)
58 RenamedComdats.try_emplace(C, ExportM.getOrInsertComdat(NewName));
Peter Collingbourne1398a322016-12-16 00:26:30 +000059
60 ExportGV.setName(NewName);
61 ExportGV.setLinkage(GlobalValue::ExternalLinkage);
62 ExportGV.setVisibility(GlobalValue::HiddenVisibility);
63
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +000064 if (ImportGV) {
65 ImportGV->setName(NewName);
66 ImportGV->setVisibility(GlobalValue::HiddenVisibility);
67 }
Peter Collingbourne6b193962017-03-30 23:43:08 +000068 }
Bob Haarman4075ccc2017-04-12 01:43:07 +000069
70 if (!RenamedComdats.empty())
71 for (auto &GO : ExportM.global_objects())
72 if (auto *C = GO.getComdat()) {
73 auto Replacement = RenamedComdats.find(C);
74 if (Replacement != RenamedComdats.end())
75 GO.setComdat(Replacement->second);
76 }
Peter Collingbourne1398a322016-12-16 00:26:30 +000077}
78
79// Promote all internal (i.e. distinct) type ids used by the module by replacing
80// them with external type ids formed using the module id.
81//
82// Note that this needs to be done before we clone the module because each clone
83// will receive its own set of distinct metadata nodes.
84void promoteTypeIds(Module &M, StringRef ModuleId) {
85 DenseMap<Metadata *, Metadata *> LocalToGlobal;
86 auto ExternalizeTypeId = [&](CallInst *CI, unsigned ArgNo) {
87 Metadata *MD =
88 cast<MetadataAsValue>(CI->getArgOperand(ArgNo))->getMetadata();
89
90 if (isa<MDNode>(MD) && cast<MDNode>(MD)->isDistinct()) {
91 Metadata *&GlobalMD = LocalToGlobal[MD];
92 if (!GlobalMD) {
Benjamin Kramer3a13ed62017-12-28 16:58:54 +000093 std::string NewName = (Twine(LocalToGlobal.size()) + ModuleId).str();
Peter Collingbourne1398a322016-12-16 00:26:30 +000094 GlobalMD = MDString::get(M.getContext(), NewName);
95 }
96
97 CI->setArgOperand(ArgNo,
98 MetadataAsValue::get(M.getContext(), GlobalMD));
99 }
100 };
101
102 if (Function *TypeTestFunc =
103 M.getFunction(Intrinsic::getName(Intrinsic::type_test))) {
104 for (const Use &U : TypeTestFunc->uses()) {
105 auto CI = cast<CallInst>(U.getUser());
106 ExternalizeTypeId(CI, 1);
107 }
108 }
109
110 if (Function *TypeCheckedLoadFunc =
111 M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load))) {
112 for (const Use &U : TypeCheckedLoadFunc->uses()) {
113 auto CI = cast<CallInst>(U.getUser());
114 ExternalizeTypeId(CI, 2);
115 }
116 }
117
118 for (GlobalObject &GO : M.global_objects()) {
119 SmallVector<MDNode *, 1> MDs;
120 GO.getMetadata(LLVMContext::MD_type, MDs);
121
122 GO.eraseMetadata(LLVMContext::MD_type);
123 for (auto MD : MDs) {
124 auto I = LocalToGlobal.find(MD->getOperand(1));
125 if (I == LocalToGlobal.end()) {
126 GO.addMetadata(LLVMContext::MD_type, *MD);
127 continue;
128 }
129 GO.addMetadata(
130 LLVMContext::MD_type,
131 *MDNode::get(M.getContext(),
132 ArrayRef<Metadata *>{MD->getOperand(0), I->second}));
133 }
134 }
135}
136
137// Drop unused globals, and drop type information from function declarations.
138// FIXME: If we made functions typeless then there would be no need to do this.
139void simplifyExternals(Module &M) {
140 FunctionType *EmptyFT =
141 FunctionType::get(Type::getVoidTy(M.getContext()), false);
142
143 for (auto I = M.begin(), E = M.end(); I != E;) {
144 Function &F = *I++;
145 if (F.isDeclaration() && F.use_empty()) {
146 F.eraseFromParent();
147 continue;
148 }
149
Peter Collingbourne93fdaca2017-07-19 17:54:29 +0000150 if (!F.isDeclaration() || F.getFunctionType() == EmptyFT ||
151 // Changing the type of an intrinsic may invalidate the IR.
152 F.getName().startswith("llvm."))
Peter Collingbourne1398a322016-12-16 00:26:30 +0000153 continue;
154
155 Function *NewF =
156 Function::Create(EmptyFT, GlobalValue::ExternalLinkage, "", &M);
157 NewF->setVisibility(F.getVisibility());
158 NewF->takeName(&F);
159 F.replaceAllUsesWith(ConstantExpr::getBitCast(NewF, F.getType()));
160 F.eraseFromParent();
161 }
162
163 for (auto I = M.global_begin(), E = M.global_end(); I != E;) {
164 GlobalVariable &GV = *I++;
165 if (GV.isDeclaration() && GV.use_empty()) {
166 GV.eraseFromParent();
167 continue;
168 }
169 }
170}
171
George Rimar54565242018-02-07 08:46:36 +0000172void filterModule(
173 Module *M, function_ref<bool(const GlobalValue *)> ShouldKeepDefinition) {
174 for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end();
175 I != E;) {
176 GlobalAlias *GA = &*I++;
177 if (ShouldKeepDefinition(GA))
178 continue;
179
180 GlobalObject *GO;
181 if (GA->getValueType()->isFunctionTy())
182 GO = Function::Create(cast<FunctionType>(GA->getValueType()),
183 GlobalValue::ExternalLinkage, "", M);
184 else
185 GO = new GlobalVariable(
186 *M, GA->getValueType(), false, GlobalValue::ExternalLinkage,
187 nullptr, "", nullptr,
188 GA->getThreadLocalMode(), GA->getType()->getAddressSpace());
189 GO->takeName(GA);
190 GA->replaceAllUsesWith(GO);
191 GA->eraseFromParent();
192 }
193
194 for (Function &F : *M) {
195 if (ShouldKeepDefinition(&F))
196 continue;
197
198 F.deleteBody();
199 F.setComdat(nullptr);
200 F.clearMetadata();
201 }
202
203 for (GlobalVariable &GV : M->globals()) {
Peter Collingbourne1398a322016-12-16 00:26:30 +0000204 if (ShouldKeepDefinition(&GV))
205 continue;
George Rimar54565242018-02-07 08:46:36 +0000206
207 GV.setInitializer(nullptr);
208 GV.setLinkage(GlobalValue::ExternalLinkage);
209 GV.setComdat(nullptr);
210 GV.clearMetadata();
Peter Collingbourne1398a322016-12-16 00:26:30 +0000211 }
Peter Collingbourne1398a322016-12-16 00:26:30 +0000212}
213
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000214void forEachVirtualFunction(Constant *C, function_ref<void(Function *)> Fn) {
215 if (auto *F = dyn_cast<Function>(C))
216 return Fn(F);
Peter Collingbourne3baa72a2017-03-02 23:10:17 +0000217 if (isa<GlobalValue>(C))
218 return;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000219 for (Value *Op : C->operands())
220 forEachVirtualFunction(cast<Constant>(Op), Fn);
221}
222
Peter Collingbourne1398a322016-12-16 00:26:30 +0000223// If it's possible to split M into regular and thin LTO parts, do so and write
224// a multi-module bitcode file with the two parts to OS. Otherwise, write only a
225// regular LTO bitcode file to OS.
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000226void splitAndWriteThinLTOBitcode(
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000227 raw_ostream &OS, raw_ostream *ThinLinkOS,
228 function_ref<AAResults &(Function &)> AARGetter, Module &M) {
Evgeniy Stepanov964f4662017-04-27 20:27:27 +0000229 std::string ModuleId = getUniqueModuleId(&M);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000230 if (ModuleId.empty()) {
231 // We couldn't generate a module ID for this module, just write it out as a
232 // regular LTO module.
233 WriteBitcodeToFile(&M, OS);
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000234 if (ThinLinkOS)
235 // We don't have a ThinLTO part, but still write the module to the
236 // ThinLinkOS if requested so that the expected output file is produced.
237 WriteBitcodeToFile(&M, *ThinLinkOS);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000238 return;
239 }
240
241 promoteTypeIds(M, ModuleId);
242
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000243 // Returns whether a global has attached type metadata. Such globals may
244 // participate in CFI or whole-program devirtualization, so they need to
245 // appear in the merged module instead of the thin LTO module.
246 auto HasTypeMetadata = [&](const GlobalObject *GO) {
Peter Collingbourne1398a322016-12-16 00:26:30 +0000247 SmallVector<MDNode *, 1> MDs;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000248 GO->getMetadata(LLVMContext::MD_type, MDs);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000249 return !MDs.empty();
250 };
251
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000252 // Collect the set of virtual functions that are eligible for virtual constant
253 // propagation. Each eligible function must not access memory, must return
254 // an integer of width <=64 bits, must take at least one argument, must not
255 // use its first argument (assumed to be "this") and all arguments other than
256 // the first one must be of <=64 bit integer type.
257 //
258 // Note that we test whether this copy of the function is readnone, rather
259 // than testing function attributes, which must hold for any copy of the
260 // function, even a less optimized version substituted at link time. This is
261 // sound because the virtual constant propagation optimizations effectively
262 // inline all implementations of the virtual function into each call site,
263 // rather than using function attributes to perform local optimization.
264 std::set<const Function *> EligibleVirtualFns;
Bob Haarman4075ccc2017-04-12 01:43:07 +0000265 // If any member of a comdat lives in MergedM, put all members of that
266 // comdat in MergedM to keep the comdat together.
267 DenseSet<const Comdat *> MergedMComdats;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000268 for (GlobalVariable &GV : M.globals())
Bob Haarman4075ccc2017-04-12 01:43:07 +0000269 if (HasTypeMetadata(&GV)) {
270 if (const auto *C = GV.getComdat())
271 MergedMComdats.insert(C);
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000272 forEachVirtualFunction(GV.getInitializer(), [&](Function *F) {
273 auto *RT = dyn_cast<IntegerType>(F->getReturnType());
274 if (!RT || RT->getBitWidth() > 64 || F->arg_empty() ||
275 !F->arg_begin()->use_empty())
276 return;
277 for (auto &Arg : make_range(std::next(F->arg_begin()), F->arg_end())) {
278 auto *ArgT = dyn_cast<IntegerType>(Arg.getType());
279 if (!ArgT || ArgT->getBitWidth() > 64)
280 return;
281 }
Chandler Carruth01f0c8a2017-07-11 05:39:20 +0000282 if (!F->isDeclaration() &&
283 computeFunctionBodyMemoryAccess(*F, AARGetter(*F)) == MAK_ReadNone)
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000284 EligibleVirtualFns.insert(F);
285 });
Bob Haarman4075ccc2017-04-12 01:43:07 +0000286 }
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000287
Peter Collingbourne1398a322016-12-16 00:26:30 +0000288 ValueToValueMapTy VMap;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000289 std::unique_ptr<Module> MergedM(
290 CloneModule(&M, VMap, [&](const GlobalValue *GV) -> bool {
Bob Haarman4075ccc2017-04-12 01:43:07 +0000291 if (const auto *C = GV->getComdat())
292 if (MergedMComdats.count(C))
293 return true;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000294 if (auto *F = dyn_cast<Function>(GV))
295 return EligibleVirtualFns.count(F);
296 if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject()))
297 return HasTypeMetadata(GVar);
298 return false;
299 }));
Peter Collingbourne28ffd322017-02-08 20:44:00 +0000300 StripDebugInfo(*MergedM);
Peter Collingbourne29c6f482018-02-06 03:29:18 +0000301 MergedM->setModuleInlineAsm("");
Peter Collingbourne1398a322016-12-16 00:26:30 +0000302
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000303 for (Function &F : *MergedM)
304 if (!F.isDeclaration()) {
305 // Reset the linkage of all functions eligible for virtual constant
306 // propagation. The canonical definitions live in the thin LTO module so
307 // that they can be imported.
308 F.setLinkage(GlobalValue::AvailableExternallyLinkage);
309 F.setComdat(nullptr);
310 }
311
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +0000312 SetVector<GlobalValue *> CfiFunctions;
313 for (auto &F : M)
314 if ((!F.hasLocalLinkage() || F.hasAddressTaken()) && HasTypeMetadata(&F))
315 CfiFunctions.insert(&F);
316
Bob Haarman4075ccc2017-04-12 01:43:07 +0000317 // Remove all globals with type metadata, globals with comdats that live in
318 // MergedM, and aliases pointing to such globals from the thin LTO module.
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000319 filterModule(&M, [&](const GlobalValue *GV) {
320 if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject()))
Bob Haarman4075ccc2017-04-12 01:43:07 +0000321 if (HasTypeMetadata(GVar))
322 return false;
323 if (const auto *C = GV->getComdat())
324 if (MergedMComdats.count(C))
325 return false;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000326 return true;
327 });
Peter Collingbourne1398a322016-12-16 00:26:30 +0000328
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +0000329 promoteInternals(*MergedM, M, ModuleId, CfiFunctions);
330 promoteInternals(M, *MergedM, ModuleId, CfiFunctions);
331
332 SmallVector<MDNode *, 8> CfiFunctionMDs;
333 for (auto V : CfiFunctions) {
334 Function &F = *cast<Function>(V);
335 SmallVector<MDNode *, 2> Types;
336 F.getMetadata(LLVMContext::MD_type, Types);
337
338 auto &Ctx = MergedM->getContext();
339 SmallVector<Metadata *, 4> Elts;
340 Elts.push_back(MDString::get(Ctx, F.getName()));
341 CfiFunctionLinkage Linkage;
342 if (!F.isDeclarationForLinker())
343 Linkage = CFL_Definition;
344 else if (F.isWeakForLinker())
345 Linkage = CFL_WeakDeclaration;
346 else
347 Linkage = CFL_Declaration;
348 Elts.push_back(ConstantAsMetadata::get(
349 llvm::ConstantInt::get(Type::getInt8Ty(Ctx), Linkage)));
350 for (auto Type : Types)
351 Elts.push_back(Type);
352 CfiFunctionMDs.push_back(MDTuple::get(Ctx, Elts));
353 }
354
355 if(!CfiFunctionMDs.empty()) {
356 NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("cfi.functions");
357 for (auto MD : CfiFunctionMDs)
358 NMD->addOperand(MD);
359 }
Peter Collingbourne1398a322016-12-16 00:26:30 +0000360
Vlad Tsyrklevichcdec22e2018-01-10 00:00:51 +0000361 SmallVector<MDNode *, 8> FunctionAliases;
362 for (auto &A : M.aliases()) {
363 if (!isa<Function>(A.getAliasee()))
364 continue;
365
366 auto *F = cast<Function>(A.getAliasee());
367 auto &Ctx = MergedM->getContext();
368 SmallVector<Metadata *, 4> Elts;
369
370 Elts.push_back(MDString::get(Ctx, A.getName()));
371 Elts.push_back(MDString::get(Ctx, F->getName()));
372 Elts.push_back(ConstantAsMetadata::get(
373 llvm::ConstantInt::get(Type::getInt8Ty(Ctx), A.getVisibility())));
374 Elts.push_back(ConstantAsMetadata::get(
375 llvm::ConstantInt::get(Type::getInt8Ty(Ctx), A.isWeakForLinker())));
376
377 FunctionAliases.push_back(MDTuple::get(Ctx, Elts));
378 }
379
380 if (!FunctionAliases.empty()) {
381 NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("aliases");
382 for (auto MD : FunctionAliases)
383 NMD->addOperand(MD);
384 }
385
Peter Collingbourne1398a322016-12-16 00:26:30 +0000386 simplifyExternals(*MergedM);
387
Peter Collingbourne1398a322016-12-16 00:26:30 +0000388 // FIXME: Try to re-use BSI and PFI from the original module here.
Teresa Johnson94624ac2017-05-10 18:52:16 +0000389 ProfileSummaryInfo PSI(M);
390 ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI);
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000391
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000392 // Mark the merged module as requiring full LTO. We still want an index for
393 // it though, so that it can participate in summary-based dead stripping.
394 MergedM->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
395 ModuleSummaryIndex MergedMIndex =
396 buildModuleSummaryIndex(*MergedM, nullptr, &PSI);
397
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000398 SmallVector<char, 0> Buffer;
399
400 BitcodeWriter W(Buffer);
401 // Save the module hash produced for the full bitcode, which will
402 // be used in the backends, and use that in the minimized bitcode
403 // produced for the full link.
404 ModuleHash ModHash = {{0}};
Peter Collingbourne1398a322016-12-16 00:26:30 +0000405 W.writeModule(&M, /*ShouldPreserveUseListOrder=*/false, &Index,
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000406 /*GenerateHash=*/true, &ModHash);
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000407 W.writeModule(MergedM.get(), /*ShouldPreserveUseListOrder=*/false,
408 &MergedMIndex);
Peter Collingbourne92648c22017-06-27 23:50:11 +0000409 W.writeSymtab();
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000410 W.writeStrtab();
Peter Collingbourne1398a322016-12-16 00:26:30 +0000411 OS << Buffer;
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000412
Haojie Wang1dec57d2017-07-21 17:25:20 +0000413 // If a minimized bitcode module was requested for the thin link, only
414 // the information that is needed by thin link will be written in the
415 // given OS (the merged module will be written as usual).
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000416 if (ThinLinkOS) {
417 Buffer.clear();
418 BitcodeWriter W2(Buffer);
419 StripDebugInfo(M);
Haojie Wang1dec57d2017-07-21 17:25:20 +0000420 W2.writeThinLinkBitcode(&M, Index, ModHash);
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000421 W2.writeModule(MergedM.get(), /*ShouldPreserveUseListOrder=*/false,
422 &MergedMIndex);
Peter Collingbourne92648c22017-06-27 23:50:11 +0000423 W2.writeSymtab();
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000424 W2.writeStrtab();
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000425 *ThinLinkOS << Buffer;
426 }
Peter Collingbourne1398a322016-12-16 00:26:30 +0000427}
428
429// Returns whether this module needs to be split because it uses type metadata.
430bool requiresSplit(Module &M) {
431 SmallVector<MDNode *, 1> MDs;
432 for (auto &GO : M.global_objects()) {
433 GO.getMetadata(LLVMContext::MD_type, MDs);
434 if (!MDs.empty())
435 return true;
436 }
437
438 return false;
439}
440
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000441void writeThinLTOBitcode(raw_ostream &OS, raw_ostream *ThinLinkOS,
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000442 function_ref<AAResults &(Function &)> AARGetter,
443 Module &M, const ModuleSummaryIndex *Index) {
Peter Collingbourne1398a322016-12-16 00:26:30 +0000444 // See if this module has any type metadata. If so, we need to split it.
445 if (requiresSplit(M))
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000446 return splitAndWriteThinLTOBitcode(OS, ThinLinkOS, AARGetter, M);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000447
448 // Otherwise we can just write it out as a regular module.
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000449
450 // Save the module hash produced for the full bitcode, which will
451 // be used in the backends, and use that in the minimized bitcode
452 // produced for the full link.
453 ModuleHash ModHash = {{0}};
Peter Collingbourne1398a322016-12-16 00:26:30 +0000454 WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false, Index,
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000455 /*GenerateHash=*/true, &ModHash);
Haojie Wang1dec57d2017-07-21 17:25:20 +0000456 // If a minimized bitcode module was requested for the thin link, only
457 // the information that is needed by thin link will be written in the
458 // given OS.
459 if (ThinLinkOS && Index)
460 WriteThinLinkBitcodeToFile(&M, *ThinLinkOS, *Index, ModHash);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000461}
462
463class WriteThinLTOBitcode : public ModulePass {
464 raw_ostream &OS; // raw_ostream to print on
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000465 // The output stream on which to emit a minimized module for use
466 // just in the thin link, if requested.
467 raw_ostream *ThinLinkOS;
Peter Collingbourne1398a322016-12-16 00:26:30 +0000468
469public:
470 static char ID; // Pass identification, replacement for typeid
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000471 WriteThinLTOBitcode() : ModulePass(ID), OS(dbgs()), ThinLinkOS(nullptr) {
Peter Collingbourne1398a322016-12-16 00:26:30 +0000472 initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry());
473 }
474
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000475 explicit WriteThinLTOBitcode(raw_ostream &o, raw_ostream *ThinLinkOS)
476 : ModulePass(ID), OS(o), ThinLinkOS(ThinLinkOS) {
Peter Collingbourne1398a322016-12-16 00:26:30 +0000477 initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry());
478 }
479
480 StringRef getPassName() const override { return "ThinLTO Bitcode Writer"; }
481
482 bool runOnModule(Module &M) override {
483 const ModuleSummaryIndex *Index =
484 &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex());
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000485 writeThinLTOBitcode(OS, ThinLinkOS, LegacyAARGetter(*this), M, Index);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000486 return true;
487 }
488 void getAnalysisUsage(AnalysisUsage &AU) const override {
489 AU.setPreservesAll();
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000490 AU.addRequired<AssumptionCacheTracker>();
Peter Collingbourne1398a322016-12-16 00:26:30 +0000491 AU.addRequired<ModuleSummaryIndexWrapperPass>();
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000492 AU.addRequired<TargetLibraryInfoWrapperPass>();
Peter Collingbourne1398a322016-12-16 00:26:30 +0000493 }
494};
495} // anonymous namespace
496
497char WriteThinLTOBitcode::ID = 0;
498INITIALIZE_PASS_BEGIN(WriteThinLTOBitcode, "write-thinlto-bitcode",
499 "Write ThinLTO Bitcode", false, true)
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000500INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Peter Collingbourne1398a322016-12-16 00:26:30 +0000501INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000502INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Peter Collingbourne1398a322016-12-16 00:26:30 +0000503INITIALIZE_PASS_END(WriteThinLTOBitcode, "write-thinlto-bitcode",
504 "Write ThinLTO Bitcode", false, true)
505
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000506ModulePass *llvm::createWriteThinLTOBitcodePass(raw_ostream &Str,
507 raw_ostream *ThinLinkOS) {
508 return new WriteThinLTOBitcode(Str, ThinLinkOS);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000509}
Tim Shen6b4114182017-06-01 01:02:12 +0000510
511PreservedAnalyses
512llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
513 FunctionAnalysisManager &FAM =
514 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
515 writeThinLTOBitcode(OS, ThinLinkOS,
516 [&FAM](Function &F) -> AAResults & {
517 return FAM.getResult<AAManager>(F);
518 },
519 M, &AM.getResult<ModuleSummaryIndexAnalysis>(M));
520 return PreservedAnalyses::all();
521}