blob: 10226c4a3ff6454556adf25f3088280ef63c8390 [file] [log] [blame]
Teresa Johnsondf6edc52016-05-23 22:54:06 +00001//===-LTO.cpp - LLVM Link Time Optimizer ----------------------------------===//
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 implements functions and classes used to support LTO.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/LTO/LTO.h"
15#include "llvm/Bitcode/ReaderWriter.h"
16#include "llvm/Support/MemoryBuffer.h"
17#include "llvm/Support/SourceMgr.h"
18#include "llvm/Support/raw_ostream.h"
19
20namespace llvm {
21
22// Simple helper to load a module from bitcode
23std::unique_ptr<Module> loadModuleFromBuffer(const MemoryBufferRef &Buffer,
24 LLVMContext &Context, bool Lazy) {
25 SMDiagnostic Err;
26 ErrorOr<std::unique_ptr<Module>> ModuleOrErr(nullptr);
27 if (Lazy) {
28 ModuleOrErr =
29 getLazyBitcodeModule(MemoryBuffer::getMemBuffer(Buffer, false), Context,
30 /* ShouldLazyLoadMetadata */ Lazy);
31 } else {
32 ModuleOrErr = parseBitcodeFile(Buffer, Context);
33 }
34 if (std::error_code EC = ModuleOrErr.getError()) {
35 Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
36 EC.message());
37 Err.print("ThinLTO", errs());
38 report_fatal_error("Can't load module, abort.");
39 }
40 return std::move(ModuleOrErr.get());
41}
Teresa Johnson04c9a2d2016-05-25 14:03:11 +000042
43static void thinLTOResolveWeakForLinkerGUID(
44 GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
45 DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
Benjamin Kramerd3f4c052016-06-12 16:13:55 +000046 function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
Teresa Johnson04c9a2d2016-05-25 14:03:11 +000047 isPrevailing,
Benjamin Kramerd3f4c052016-06-12 16:13:55 +000048 function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
Teresa Johnson04c9a2d2016-05-25 14:03:11 +000049 recordNewLinkage) {
Teresa Johnson04c9a2d2016-05-25 14:03:11 +000050 for (auto &S : GVSummaryList) {
51 if (GlobalInvolvedWithAlias.count(S.get()))
52 continue;
53 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
54 if (!GlobalValue::isWeakForLinker(OriginalLinkage))
55 continue;
Peter Collingbourne73589f32016-07-07 18:31:51 +000056 // We need to emit only one of these. The prevailing module will keep it,
Teresa Johnson04c9a2d2016-05-25 14:03:11 +000057 // but turned into a weak, while the others will drop it when possible.
Peter Collingbourne73589f32016-07-07 18:31:51 +000058 if (isPrevailing(GUID, S.get())) {
Teresa Johnson28c03b52016-05-26 14:16:52 +000059 if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
60 S->setLinkage(GlobalValue::getWeakLinkage(
61 GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
Teresa Johnson04c9a2d2016-05-25 14:03:11 +000062 }
63 // Alias can't be turned into available_externally.
64 else if (!isa<AliasSummary>(S.get()) &&
65 (GlobalValue::isLinkOnceODRLinkage(OriginalLinkage) ||
66 GlobalValue::isWeakODRLinkage(OriginalLinkage)))
67 S->setLinkage(GlobalValue::AvailableExternallyLinkage);
68 if (S->linkage() != OriginalLinkage)
69 recordNewLinkage(S->modulePath(), GUID, S->linkage());
70 }
71}
72
73// Resolve Weak and LinkOnce values in the \p Index.
74//
75// We'd like to drop these functions if they are no longer referenced in the
76// current module. However there is a chance that another module is still
77// referencing them because of the import. We make sure we always emit at least
78// one copy.
79void thinLTOResolveWeakForLinkerInIndex(
80 ModuleSummaryIndex &Index,
Benjamin Kramerd3f4c052016-06-12 16:13:55 +000081 function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
Teresa Johnson04c9a2d2016-05-25 14:03:11 +000082 isPrevailing,
Benjamin Kramerd3f4c052016-06-12 16:13:55 +000083 function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
Teresa Johnson04c9a2d2016-05-25 14:03:11 +000084 recordNewLinkage) {
Teresa Johnson04c9a2d2016-05-25 14:03:11 +000085 // We won't optimize the globals that are referenced by an alias for now
86 // Ideally we should turn the alias into a global and duplicate the definition
87 // when needed.
88 DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
89 for (auto &I : Index)
90 for (auto &S : I.second)
91 if (auto AS = dyn_cast<AliasSummary>(S.get()))
92 GlobalInvolvedWithAlias.insert(&AS->getAliasee());
93
94 for (auto &I : Index)
95 thinLTOResolveWeakForLinkerGUID(I.second, I.first, GlobalInvolvedWithAlias,
Peter Collingbourne73589f32016-07-07 18:31:51 +000096 isPrevailing, recordNewLinkage);
Teresa Johnson04c9a2d2016-05-25 14:03:11 +000097}
98
99static void thinLTOInternalizeAndPromoteGUID(
100 GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
Benjamin Kramerd3f4c052016-06-12 16:13:55 +0000101 function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
Teresa Johnson04c9a2d2016-05-25 14:03:11 +0000102 for (auto &S : GVSummaryList) {
103 if (isExported(S->modulePath(), GUID)) {
104 if (GlobalValue::isLocalLinkage(S->linkage()))
105 S->setLinkage(GlobalValue::ExternalLinkage);
106 } else if (!GlobalValue::isLocalLinkage(S->linkage()))
107 S->setLinkage(GlobalValue::InternalLinkage);
108 }
109}
110
111// Update the linkages in the given \p Index to mark exported values
112// as external and non-exported values as internal.
113void thinLTOInternalizeAndPromoteInIndex(
114 ModuleSummaryIndex &Index,
Benjamin Kramerd3f4c052016-06-12 16:13:55 +0000115 function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
Teresa Johnson04c9a2d2016-05-25 14:03:11 +0000116 for (auto &I : Index)
117 thinLTOInternalizeAndPromoteGUID(I.second, I.first, isExported);
118}
Teresa Johnsondf6edc52016-05-23 22:54:06 +0000119}