blob: a7bcc7cc55325ae3bb34f083a7e234f51a54e083 [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"
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +000022#include "llvm/Support/FileSystem.h"
Peter Collingbourne1398a322016-12-16 00:26:30 +000023#include "llvm/Support/ScopedPrinter.h"
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +000024#include "llvm/Support/raw_ostream.h"
25#include "llvm/Transforms/IPO.h"
Peter Collingbourne002c2d52017-02-14 03:42:38 +000026#include "llvm/Transforms/IPO/FunctionAttrs.h"
Peter Collingbourne1398a322016-12-16 00:26:30 +000027#include "llvm/Transforms/Utils/Cloning.h"
Evgeniy Stepanov964f4662017-04-27 20:27:27 +000028#include "llvm/Transforms/Utils/ModuleUtils.h"
Peter Collingbourne1398a322016-12-16 00:26:30 +000029using namespace llvm;
30
31namespace {
32
Peter Collingbourne1398a322016-12-16 00:26:30 +000033// Promote each local-linkage entity defined by ExportM and used by ImportM by
34// changing visibility and appending the given ModuleId.
35void promoteInternals(Module &ExportM, Module &ImportM, StringRef ModuleId) {
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();
42 GlobalValue *ImportGV = ImportM.getNamedValue(Name);
Peter Collingbourne1398a322016-12-16 00:26:30 +000043 if (!ImportGV || ImportGV->use_empty())
Peter Collingbourne6b193962017-03-30 23:43:08 +000044 continue;
Peter Collingbourne1398a322016-12-16 00:26:30 +000045
Bob Haarman4075ccc2017-04-12 01:43:07 +000046 std::string NewName = (Name + ModuleId).str();
47
48 if (const auto *C = ExportGV.getComdat())
49 if (C->getName() == Name)
50 RenamedComdats.try_emplace(C, ExportM.getOrInsertComdat(NewName));
Peter Collingbourne1398a322016-12-16 00:26:30 +000051
52 ExportGV.setName(NewName);
53 ExportGV.setLinkage(GlobalValue::ExternalLinkage);
54 ExportGV.setVisibility(GlobalValue::HiddenVisibility);
55
56 ImportGV->setName(NewName);
57 ImportGV->setVisibility(GlobalValue::HiddenVisibility);
Peter Collingbourne6b193962017-03-30 23:43:08 +000058 }
Bob Haarman4075ccc2017-04-12 01:43:07 +000059
60 if (!RenamedComdats.empty())
61 for (auto &GO : ExportM.global_objects())
62 if (auto *C = GO.getComdat()) {
63 auto Replacement = RenamedComdats.find(C);
64 if (Replacement != RenamedComdats.end())
65 GO.setComdat(Replacement->second);
66 }
Peter Collingbourne1398a322016-12-16 00:26:30 +000067}
68
69// Promote all internal (i.e. distinct) type ids used by the module by replacing
70// them with external type ids formed using the module id.
71//
72// Note that this needs to be done before we clone the module because each clone
73// will receive its own set of distinct metadata nodes.
74void promoteTypeIds(Module &M, StringRef ModuleId) {
75 DenseMap<Metadata *, Metadata *> LocalToGlobal;
76 auto ExternalizeTypeId = [&](CallInst *CI, unsigned ArgNo) {
77 Metadata *MD =
78 cast<MetadataAsValue>(CI->getArgOperand(ArgNo))->getMetadata();
79
80 if (isa<MDNode>(MD) && cast<MDNode>(MD)->isDistinct()) {
81 Metadata *&GlobalMD = LocalToGlobal[MD];
82 if (!GlobalMD) {
83 std::string NewName =
84 (to_string(LocalToGlobal.size()) + ModuleId).str();
85 GlobalMD = MDString::get(M.getContext(), NewName);
86 }
87
88 CI->setArgOperand(ArgNo,
89 MetadataAsValue::get(M.getContext(), GlobalMD));
90 }
91 };
92
93 if (Function *TypeTestFunc =
94 M.getFunction(Intrinsic::getName(Intrinsic::type_test))) {
95 for (const Use &U : TypeTestFunc->uses()) {
96 auto CI = cast<CallInst>(U.getUser());
97 ExternalizeTypeId(CI, 1);
98 }
99 }
100
101 if (Function *TypeCheckedLoadFunc =
102 M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load))) {
103 for (const Use &U : TypeCheckedLoadFunc->uses()) {
104 auto CI = cast<CallInst>(U.getUser());
105 ExternalizeTypeId(CI, 2);
106 }
107 }
108
109 for (GlobalObject &GO : M.global_objects()) {
110 SmallVector<MDNode *, 1> MDs;
111 GO.getMetadata(LLVMContext::MD_type, MDs);
112
113 GO.eraseMetadata(LLVMContext::MD_type);
114 for (auto MD : MDs) {
115 auto I = LocalToGlobal.find(MD->getOperand(1));
116 if (I == LocalToGlobal.end()) {
117 GO.addMetadata(LLVMContext::MD_type, *MD);
118 continue;
119 }
120 GO.addMetadata(
121 LLVMContext::MD_type,
122 *MDNode::get(M.getContext(),
123 ArrayRef<Metadata *>{MD->getOperand(0), I->second}));
124 }
125 }
126}
127
128// Drop unused globals, and drop type information from function declarations.
129// FIXME: If we made functions typeless then there would be no need to do this.
130void simplifyExternals(Module &M) {
131 FunctionType *EmptyFT =
132 FunctionType::get(Type::getVoidTy(M.getContext()), false);
133
134 for (auto I = M.begin(), E = M.end(); I != E;) {
135 Function &F = *I++;
136 if (F.isDeclaration() && F.use_empty()) {
137 F.eraseFromParent();
138 continue;
139 }
140
141 if (!F.isDeclaration() || F.getFunctionType() == EmptyFT)
142 continue;
143
144 Function *NewF =
145 Function::Create(EmptyFT, GlobalValue::ExternalLinkage, "", &M);
146 NewF->setVisibility(F.getVisibility());
147 NewF->takeName(&F);
148 F.replaceAllUsesWith(ConstantExpr::getBitCast(NewF, F.getType()));
149 F.eraseFromParent();
150 }
151
152 for (auto I = M.global_begin(), E = M.global_end(); I != E;) {
153 GlobalVariable &GV = *I++;
154 if (GV.isDeclaration() && GV.use_empty()) {
155 GV.eraseFromParent();
156 continue;
157 }
158 }
159}
160
161void filterModule(
Benjamin Kramer061f4a52017-01-13 14:39:03 +0000162 Module *M, function_ref<bool(const GlobalValue *)> ShouldKeepDefinition) {
Bob Haarman6de81342017-04-05 00:42:07 +0000163 for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end();
164 I != E;) {
165 GlobalAlias *GA = &*I++;
166 if (ShouldKeepDefinition(GA))
167 continue;
168
169 GlobalObject *GO;
170 if (GA->getValueType()->isFunctionTy())
171 GO = Function::Create(cast<FunctionType>(GA->getValueType()),
172 GlobalValue::ExternalLinkage, "", M);
173 else
174 GO = new GlobalVariable(
175 *M, GA->getValueType(), false, GlobalValue::ExternalLinkage,
Serge Gueltonf4dc59b2017-05-11 08:53:00 +0000176 nullptr, "", nullptr,
Bob Haarman6de81342017-04-05 00:42:07 +0000177 GA->getThreadLocalMode(), GA->getType()->getAddressSpace());
178 GO->takeName(GA);
179 GA->replaceAllUsesWith(GO);
180 GA->eraseFromParent();
181 }
182
Peter Collingbourne1398a322016-12-16 00:26:30 +0000183 for (Function &F : *M) {
184 if (ShouldKeepDefinition(&F))
185 continue;
186
187 F.deleteBody();
Peter Collingbourne20a00932017-01-18 20:03:02 +0000188 F.setComdat(nullptr);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000189 F.clearMetadata();
190 }
191
192 for (GlobalVariable &GV : M->globals()) {
193 if (ShouldKeepDefinition(&GV))
194 continue;
195
196 GV.setInitializer(nullptr);
197 GV.setLinkage(GlobalValue::ExternalLinkage);
Peter Collingbourne20a00932017-01-18 20:03:02 +0000198 GV.setComdat(nullptr);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000199 GV.clearMetadata();
200 }
Peter Collingbourne1398a322016-12-16 00:26:30 +0000201}
202
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000203void forEachVirtualFunction(Constant *C, function_ref<void(Function *)> Fn) {
204 if (auto *F = dyn_cast<Function>(C))
205 return Fn(F);
Peter Collingbourne3baa72a2017-03-02 23:10:17 +0000206 if (isa<GlobalValue>(C))
207 return;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000208 for (Value *Op : C->operands())
209 forEachVirtualFunction(cast<Constant>(Op), Fn);
210}
211
Peter Collingbourne1398a322016-12-16 00:26:30 +0000212// If it's possible to split M into regular and thin LTO parts, do so and write
213// a multi-module bitcode file with the two parts to OS. Otherwise, write only a
214// regular LTO bitcode file to OS.
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000215void splitAndWriteThinLTOBitcode(
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000216 raw_ostream &OS, raw_ostream *ThinLinkOS,
217 function_ref<AAResults &(Function &)> AARGetter, Module &M) {
Evgeniy Stepanov964f4662017-04-27 20:27:27 +0000218 std::string ModuleId = getUniqueModuleId(&M);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000219 if (ModuleId.empty()) {
220 // We couldn't generate a module ID for this module, just write it out as a
221 // regular LTO module.
222 WriteBitcodeToFile(&M, OS);
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000223 if (ThinLinkOS)
224 // We don't have a ThinLTO part, but still write the module to the
225 // ThinLinkOS if requested so that the expected output file is produced.
226 WriteBitcodeToFile(&M, *ThinLinkOS);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000227 return;
228 }
229
230 promoteTypeIds(M, ModuleId);
231
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000232 // Returns whether a global has attached type metadata. Such globals may
233 // participate in CFI or whole-program devirtualization, so they need to
234 // appear in the merged module instead of the thin LTO module.
235 auto HasTypeMetadata = [&](const GlobalObject *GO) {
Peter Collingbourne1398a322016-12-16 00:26:30 +0000236 SmallVector<MDNode *, 1> MDs;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000237 GO->getMetadata(LLVMContext::MD_type, MDs);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000238 return !MDs.empty();
239 };
240
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000241 // Collect the set of virtual functions that are eligible for virtual constant
242 // propagation. Each eligible function must not access memory, must return
243 // an integer of width <=64 bits, must take at least one argument, must not
244 // use its first argument (assumed to be "this") and all arguments other than
245 // the first one must be of <=64 bit integer type.
246 //
247 // Note that we test whether this copy of the function is readnone, rather
248 // than testing function attributes, which must hold for any copy of the
249 // function, even a less optimized version substituted at link time. This is
250 // sound because the virtual constant propagation optimizations effectively
251 // inline all implementations of the virtual function into each call site,
252 // rather than using function attributes to perform local optimization.
253 std::set<const Function *> EligibleVirtualFns;
Bob Haarman4075ccc2017-04-12 01:43:07 +0000254 // If any member of a comdat lives in MergedM, put all members of that
255 // comdat in MergedM to keep the comdat together.
256 DenseSet<const Comdat *> MergedMComdats;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000257 for (GlobalVariable &GV : M.globals())
Bob Haarman4075ccc2017-04-12 01:43:07 +0000258 if (HasTypeMetadata(&GV)) {
259 if (const auto *C = GV.getComdat())
260 MergedMComdats.insert(C);
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000261 forEachVirtualFunction(GV.getInitializer(), [&](Function *F) {
262 auto *RT = dyn_cast<IntegerType>(F->getReturnType());
263 if (!RT || RT->getBitWidth() > 64 || F->arg_empty() ||
264 !F->arg_begin()->use_empty())
265 return;
266 for (auto &Arg : make_range(std::next(F->arg_begin()), F->arg_end())) {
267 auto *ArgT = dyn_cast<IntegerType>(Arg.getType());
268 if (!ArgT || ArgT->getBitWidth() > 64)
269 return;
270 }
271 if (computeFunctionBodyMemoryAccess(*F, AARGetter(*F)) == MAK_ReadNone)
272 EligibleVirtualFns.insert(F);
273 });
Bob Haarman4075ccc2017-04-12 01:43:07 +0000274 }
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000275
Peter Collingbourne1398a322016-12-16 00:26:30 +0000276 ValueToValueMapTy VMap;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000277 std::unique_ptr<Module> MergedM(
278 CloneModule(&M, VMap, [&](const GlobalValue *GV) -> bool {
Bob Haarman4075ccc2017-04-12 01:43:07 +0000279 if (const auto *C = GV->getComdat())
280 if (MergedMComdats.count(C))
281 return true;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000282 if (auto *F = dyn_cast<Function>(GV))
283 return EligibleVirtualFns.count(F);
284 if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject()))
285 return HasTypeMetadata(GVar);
286 return false;
287 }));
Peter Collingbourne28ffd322017-02-08 20:44:00 +0000288 StripDebugInfo(*MergedM);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000289
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000290 for (Function &F : *MergedM)
291 if (!F.isDeclaration()) {
292 // Reset the linkage of all functions eligible for virtual constant
293 // propagation. The canonical definitions live in the thin LTO module so
294 // that they can be imported.
295 F.setLinkage(GlobalValue::AvailableExternallyLinkage);
296 F.setComdat(nullptr);
297 }
298
Bob Haarman4075ccc2017-04-12 01:43:07 +0000299 // Remove all globals with type metadata, globals with comdats that live in
300 // MergedM, and aliases pointing to such globals from the thin LTO module.
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000301 filterModule(&M, [&](const GlobalValue *GV) {
302 if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject()))
Bob Haarman4075ccc2017-04-12 01:43:07 +0000303 if (HasTypeMetadata(GVar))
304 return false;
305 if (const auto *C = GV->getComdat())
306 if (MergedMComdats.count(C))
307 return false;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000308 return true;
309 });
Peter Collingbourne1398a322016-12-16 00:26:30 +0000310
311 promoteInternals(*MergedM, M, ModuleId);
312 promoteInternals(M, *MergedM, ModuleId);
313
314 simplifyExternals(*MergedM);
315
Peter Collingbourne1398a322016-12-16 00:26:30 +0000316
317 // FIXME: Try to re-use BSI and PFI from the original module here.
Teresa Johnson94624ac2017-05-10 18:52:16 +0000318 ProfileSummaryInfo PSI(M);
319 ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI);
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000320
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000321 // Mark the merged module as requiring full LTO. We still want an index for
322 // it though, so that it can participate in summary-based dead stripping.
323 MergedM->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
324 ModuleSummaryIndex MergedMIndex =
325 buildModuleSummaryIndex(*MergedM, nullptr, &PSI);
326
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000327 SmallVector<char, 0> Buffer;
328
329 BitcodeWriter W(Buffer);
330 // Save the module hash produced for the full bitcode, which will
331 // be used in the backends, and use that in the minimized bitcode
332 // produced for the full link.
333 ModuleHash ModHash = {{0}};
Peter Collingbourne1398a322016-12-16 00:26:30 +0000334 W.writeModule(&M, /*ShouldPreserveUseListOrder=*/false, &Index,
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000335 /*GenerateHash=*/true, &ModHash);
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000336 W.writeModule(MergedM.get(), /*ShouldPreserveUseListOrder=*/false,
337 &MergedMIndex);
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000338 W.writeStrtab();
Peter Collingbourne1398a322016-12-16 00:26:30 +0000339 OS << Buffer;
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000340
341 // If a minimized bitcode module was requested for the thin link,
342 // strip the debug info (the merged module was already stripped above)
343 // and write it to the given OS.
344 if (ThinLinkOS) {
345 Buffer.clear();
346 BitcodeWriter W2(Buffer);
347 StripDebugInfo(M);
348 W2.writeModule(&M, /*ShouldPreserveUseListOrder=*/false, &Index,
349 /*GenerateHash=*/false, &ModHash);
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000350 W2.writeModule(MergedM.get(), /*ShouldPreserveUseListOrder=*/false,
351 &MergedMIndex);
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000352 W2.writeStrtab();
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000353 *ThinLinkOS << Buffer;
354 }
Peter Collingbourne1398a322016-12-16 00:26:30 +0000355}
356
357// Returns whether this module needs to be split because it uses type metadata.
358bool requiresSplit(Module &M) {
359 SmallVector<MDNode *, 1> MDs;
360 for (auto &GO : M.global_objects()) {
361 GO.getMetadata(LLVMContext::MD_type, MDs);
362 if (!MDs.empty())
363 return true;
364 }
365
366 return false;
367}
368
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000369void writeThinLTOBitcode(raw_ostream &OS, raw_ostream *ThinLinkOS,
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000370 function_ref<AAResults &(Function &)> AARGetter,
371 Module &M, const ModuleSummaryIndex *Index) {
Peter Collingbourne1398a322016-12-16 00:26:30 +0000372 // See if this module has any type metadata. If so, we need to split it.
373 if (requiresSplit(M))
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000374 return splitAndWriteThinLTOBitcode(OS, ThinLinkOS, AARGetter, M);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000375
376 // Otherwise we can just write it out as a regular module.
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000377
378 // Save the module hash produced for the full bitcode, which will
379 // be used in the backends, and use that in the minimized bitcode
380 // produced for the full link.
381 ModuleHash ModHash = {{0}};
Peter Collingbourne1398a322016-12-16 00:26:30 +0000382 WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false, Index,
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000383 /*GenerateHash=*/true, &ModHash);
384 // If a minimized bitcode module was requested for the thin link,
385 // strip the debug info and write it to the given OS.
386 if (ThinLinkOS) {
387 StripDebugInfo(M);
388 WriteBitcodeToFile(&M, *ThinLinkOS, /*ShouldPreserveUseListOrder=*/false,
389 Index,
390 /*GenerateHash=*/false, &ModHash);
391 }
Peter Collingbourne1398a322016-12-16 00:26:30 +0000392}
393
394class WriteThinLTOBitcode : public ModulePass {
395 raw_ostream &OS; // raw_ostream to print on
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000396 // The output stream on which to emit a minimized module for use
397 // just in the thin link, if requested.
398 raw_ostream *ThinLinkOS;
Peter Collingbourne1398a322016-12-16 00:26:30 +0000399
400public:
401 static char ID; // Pass identification, replacement for typeid
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000402 WriteThinLTOBitcode() : ModulePass(ID), OS(dbgs()), ThinLinkOS(nullptr) {
Peter Collingbourne1398a322016-12-16 00:26:30 +0000403 initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry());
404 }
405
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000406 explicit WriteThinLTOBitcode(raw_ostream &o, raw_ostream *ThinLinkOS)
407 : ModulePass(ID), OS(o), ThinLinkOS(ThinLinkOS) {
Peter Collingbourne1398a322016-12-16 00:26:30 +0000408 initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry());
409 }
410
411 StringRef getPassName() const override { return "ThinLTO Bitcode Writer"; }
412
413 bool runOnModule(Module &M) override {
414 const ModuleSummaryIndex *Index =
415 &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex());
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000416 writeThinLTOBitcode(OS, ThinLinkOS, LegacyAARGetter(*this), M, Index);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000417 return true;
418 }
419 void getAnalysisUsage(AnalysisUsage &AU) const override {
420 AU.setPreservesAll();
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000421 AU.addRequired<AssumptionCacheTracker>();
Peter Collingbourne1398a322016-12-16 00:26:30 +0000422 AU.addRequired<ModuleSummaryIndexWrapperPass>();
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000423 AU.addRequired<TargetLibraryInfoWrapperPass>();
Peter Collingbourne1398a322016-12-16 00:26:30 +0000424 }
425};
426} // anonymous namespace
427
428char WriteThinLTOBitcode::ID = 0;
429INITIALIZE_PASS_BEGIN(WriteThinLTOBitcode, "write-thinlto-bitcode",
430 "Write ThinLTO Bitcode", false, true)
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000431INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Peter Collingbourne1398a322016-12-16 00:26:30 +0000432INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000433INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Peter Collingbourne1398a322016-12-16 00:26:30 +0000434INITIALIZE_PASS_END(WriteThinLTOBitcode, "write-thinlto-bitcode",
435 "Write ThinLTO Bitcode", false, true)
436
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000437ModulePass *llvm::createWriteThinLTOBitcodePass(raw_ostream &Str,
438 raw_ostream *ThinLinkOS) {
439 return new WriteThinLTOBitcode(Str, ThinLinkOS);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000440}
Tim Shen6b4114182017-06-01 01:02:12 +0000441
442PreservedAnalyses
443llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
444 FunctionAnalysisManager &FAM =
445 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
446 writeThinLTOBitcode(OS, ThinLinkOS,
447 [&FAM](Function &F) -> AAResults & {
448 return FAM.getResult<AAManager>(F);
449 },
450 M, &AM.getResult<ModuleSummaryIndexAnalysis>(M));
451 return PreservedAnalyses::all();
452}