xref: /src/contrib/llvm-project/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1b915e9e0SDimitry Andric //===- ThinLTOBitcodeWriter.cpp - Bitcode writing pass for ThinLTO --------===//
2b915e9e0SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b915e9e0SDimitry Andric //
7b915e9e0SDimitry Andric //===----------------------------------------------------------------------===//
8b915e9e0SDimitry Andric 
9f382538dSDimitry Andric #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
1071d5a254SDimitry Andric #include "llvm/Analysis/BasicAliasAnalysis.h"
11b915e9e0SDimitry Andric #include "llvm/Analysis/ModuleSummaryAnalysis.h"
126b3f41edSDimitry Andric #include "llvm/Analysis/ProfileSummaryInfo.h"
13b915e9e0SDimitry Andric #include "llvm/Analysis/TypeMetadataUtils.h"
14b915e9e0SDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h"
15b915e9e0SDimitry Andric #include "llvm/IR/Constants.h"
1671d5a254SDimitry Andric #include "llvm/IR/DebugInfo.h"
17b60736ecSDimitry Andric #include "llvm/IR/Instructions.h"
18b915e9e0SDimitry Andric #include "llvm/IR/Intrinsics.h"
19b915e9e0SDimitry Andric #include "llvm/IR/Module.h"
20b915e9e0SDimitry Andric #include "llvm/IR/PassManager.h"
21eb11fae6SDimitry Andric #include "llvm/Object/ModuleSymbolTable.h"
2271d5a254SDimitry Andric #include "llvm/Support/raw_ostream.h"
2371d5a254SDimitry Andric #include "llvm/Transforms/IPO.h"
2471d5a254SDimitry Andric #include "llvm/Transforms/IPO/FunctionAttrs.h"
25eb11fae6SDimitry Andric #include "llvm/Transforms/IPO/FunctionImport.h"
261d5ae102SDimitry Andric #include "llvm/Transforms/IPO/LowerTypeTests.h"
27b915e9e0SDimitry Andric #include "llvm/Transforms/Utils/Cloning.h"
28a303c417SDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
29b915e9e0SDimitry Andric using namespace llvm;
30b915e9e0SDimitry Andric 
31b915e9e0SDimitry Andric namespace {
32b915e9e0SDimitry Andric 
33c0981da4SDimitry Andric // Determine if a promotion alias should be created for a symbol name.
allowPromotionAlias(const std::string & Name)34c0981da4SDimitry Andric static bool allowPromotionAlias(const std::string &Name) {
35c0981da4SDimitry Andric   // Promotion aliases are used only in inline assembly. It's safe to
36c0981da4SDimitry Andric   // simply skip unusual names. Subset of MCAsmInfo::isAcceptableChar()
37c0981da4SDimitry Andric   // and MCAsmInfoXCOFF::isAcceptableChar().
38c0981da4SDimitry Andric   for (const char &C : Name) {
39c0981da4SDimitry Andric     if (isAlnum(C) || C == '_' || C == '.')
40c0981da4SDimitry Andric       continue;
41c0981da4SDimitry Andric     return false;
42c0981da4SDimitry Andric   }
43c0981da4SDimitry Andric   return true;
44c0981da4SDimitry Andric }
45c0981da4SDimitry Andric 
46b915e9e0SDimitry Andric // Promote each local-linkage entity defined by ExportM and used by ImportM by
47b915e9e0SDimitry Andric // changing visibility and appending the given ModuleId.
promoteInternals(Module & ExportM,Module & ImportM,StringRef ModuleId,SetVector<GlobalValue * > & PromoteExtra)487c7aba6eSDimitry Andric void promoteInternals(Module &ExportM, Module &ImportM, StringRef ModuleId,
497c7aba6eSDimitry Andric                       SetVector<GlobalValue *> &PromoteExtra) {
5071d5a254SDimitry Andric   DenseMap<const Comdat *, Comdat *> RenamedComdats;
5171d5a254SDimitry Andric   for (auto &ExportGV : ExportM.global_values()) {
52b915e9e0SDimitry Andric     if (!ExportGV.hasLocalLinkage())
5371d5a254SDimitry Andric       continue;
54b915e9e0SDimitry Andric 
5571d5a254SDimitry Andric     auto Name = ExportGV.getName();
56044eb2f6SDimitry Andric     GlobalValue *ImportGV = nullptr;
57044eb2f6SDimitry Andric     if (!PromoteExtra.count(&ExportGV)) {
58044eb2f6SDimitry Andric       ImportGV = ImportM.getNamedValue(Name);
59044eb2f6SDimitry Andric       if (!ImportGV)
6071d5a254SDimitry Andric         continue;
61044eb2f6SDimitry Andric       ImportGV->removeDeadConstantUsers();
62044eb2f6SDimitry Andric       if (ImportGV->use_empty()) {
63044eb2f6SDimitry Andric         ImportGV->eraseFromParent();
64044eb2f6SDimitry Andric         continue;
65044eb2f6SDimitry Andric       }
66044eb2f6SDimitry Andric     }
67b915e9e0SDimitry Andric 
68c0981da4SDimitry Andric     std::string OldName = Name.str();
6971d5a254SDimitry Andric     std::string NewName = (Name + ModuleId).str();
7071d5a254SDimitry Andric 
7171d5a254SDimitry Andric     if (const auto *C = ExportGV.getComdat())
7271d5a254SDimitry Andric       if (C->getName() == Name)
7371d5a254SDimitry Andric         RenamedComdats.try_emplace(C, ExportM.getOrInsertComdat(NewName));
74b915e9e0SDimitry Andric 
75b915e9e0SDimitry Andric     ExportGV.setName(NewName);
76b915e9e0SDimitry Andric     ExportGV.setLinkage(GlobalValue::ExternalLinkage);
77b915e9e0SDimitry Andric     ExportGV.setVisibility(GlobalValue::HiddenVisibility);
78b915e9e0SDimitry Andric 
797c7aba6eSDimitry Andric     if (ImportGV) {
80b915e9e0SDimitry Andric       ImportGV->setName(NewName);
81b915e9e0SDimitry Andric       ImportGV->setVisibility(GlobalValue::HiddenVisibility);
8271d5a254SDimitry Andric     }
83c0981da4SDimitry Andric 
84c0981da4SDimitry Andric     if (isa<Function>(&ExportGV) && allowPromotionAlias(OldName)) {
85c0981da4SDimitry Andric       // Create a local alias with the original name to avoid breaking
86c0981da4SDimitry Andric       // references from inline assembly.
8777fc4c14SDimitry Andric       std::string Alias =
8877fc4c14SDimitry Andric           ".lto_set_conditional " + OldName + "," + NewName + "\n";
89c0981da4SDimitry Andric       ExportM.appendModuleInlineAsm(Alias);
90c0981da4SDimitry Andric     }
917c7aba6eSDimitry Andric   }
92b915e9e0SDimitry Andric 
9371d5a254SDimitry Andric   if (!RenamedComdats.empty())
9471d5a254SDimitry Andric     for (auto &GO : ExportM.global_objects())
9571d5a254SDimitry Andric       if (auto *C = GO.getComdat()) {
9671d5a254SDimitry Andric         auto Replacement = RenamedComdats.find(C);
9771d5a254SDimitry Andric         if (Replacement != RenamedComdats.end())
9871d5a254SDimitry Andric           GO.setComdat(Replacement->second);
9971d5a254SDimitry Andric       }
100b915e9e0SDimitry Andric }
101b915e9e0SDimitry Andric 
102b915e9e0SDimitry Andric // Promote all internal (i.e. distinct) type ids used by the module by replacing
103b915e9e0SDimitry Andric // them with external type ids formed using the module id.
104b915e9e0SDimitry Andric //
105b915e9e0SDimitry Andric // Note that this needs to be done before we clone the module because each clone
106b915e9e0SDimitry Andric // will receive its own set of distinct metadata nodes.
promoteTypeIds(Module & M,StringRef ModuleId)107b915e9e0SDimitry Andric void promoteTypeIds(Module &M, StringRef ModuleId) {
108b915e9e0SDimitry Andric   DenseMap<Metadata *, Metadata *> LocalToGlobal;
109b915e9e0SDimitry Andric   auto ExternalizeTypeId = [&](CallInst *CI, unsigned ArgNo) {
110b915e9e0SDimitry Andric     Metadata *MD =
111b915e9e0SDimitry Andric         cast<MetadataAsValue>(CI->getArgOperand(ArgNo))->getMetadata();
112b915e9e0SDimitry Andric 
113b915e9e0SDimitry Andric     if (isa<MDNode>(MD) && cast<MDNode>(MD)->isDistinct()) {
114b915e9e0SDimitry Andric       Metadata *&GlobalMD = LocalToGlobal[MD];
115b915e9e0SDimitry Andric       if (!GlobalMD) {
116b2b7c066SDimitry Andric         std::string NewName = (Twine(LocalToGlobal.size()) + ModuleId).str();
117b915e9e0SDimitry Andric         GlobalMD = MDString::get(M.getContext(), NewName);
118b915e9e0SDimitry Andric       }
119b915e9e0SDimitry Andric 
120b915e9e0SDimitry Andric       CI->setArgOperand(ArgNo,
121b915e9e0SDimitry Andric                         MetadataAsValue::get(M.getContext(), GlobalMD));
122b915e9e0SDimitry Andric     }
123b915e9e0SDimitry Andric   };
124b915e9e0SDimitry Andric 
125b915e9e0SDimitry Andric   if (Function *TypeTestFunc =
126b915e9e0SDimitry Andric           M.getFunction(Intrinsic::getName(Intrinsic::type_test))) {
127b915e9e0SDimitry Andric     for (const Use &U : TypeTestFunc->uses()) {
128b915e9e0SDimitry Andric       auto CI = cast<CallInst>(U.getUser());
129b915e9e0SDimitry Andric       ExternalizeTypeId(CI, 1);
130b915e9e0SDimitry Andric     }
131b915e9e0SDimitry Andric   }
132b915e9e0SDimitry Andric 
13308e8dd7bSDimitry Andric   if (Function *PublicTypeTestFunc =
13408e8dd7bSDimitry Andric           M.getFunction(Intrinsic::getName(Intrinsic::public_type_test))) {
13508e8dd7bSDimitry Andric     for (const Use &U : PublicTypeTestFunc->uses()) {
13608e8dd7bSDimitry Andric       auto CI = cast<CallInst>(U.getUser());
13708e8dd7bSDimitry Andric       ExternalizeTypeId(CI, 1);
13808e8dd7bSDimitry Andric     }
13908e8dd7bSDimitry Andric   }
14008e8dd7bSDimitry Andric 
141b915e9e0SDimitry Andric   if (Function *TypeCheckedLoadFunc =
142b915e9e0SDimitry Andric           M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load))) {
143b915e9e0SDimitry Andric     for (const Use &U : TypeCheckedLoadFunc->uses()) {
144b915e9e0SDimitry Andric       auto CI = cast<CallInst>(U.getUser());
145b915e9e0SDimitry Andric       ExternalizeTypeId(CI, 2);
146b915e9e0SDimitry Andric     }
147b915e9e0SDimitry Andric   }
148b915e9e0SDimitry Andric 
1497fa27ce4SDimitry Andric   if (Function *TypeCheckedLoadRelativeFunc = M.getFunction(
1507fa27ce4SDimitry Andric           Intrinsic::getName(Intrinsic::type_checked_load_relative))) {
1517fa27ce4SDimitry Andric     for (const Use &U : TypeCheckedLoadRelativeFunc->uses()) {
1527fa27ce4SDimitry Andric       auto CI = cast<CallInst>(U.getUser());
1537fa27ce4SDimitry Andric       ExternalizeTypeId(CI, 2);
1547fa27ce4SDimitry Andric     }
1557fa27ce4SDimitry Andric   }
1567fa27ce4SDimitry Andric 
157b915e9e0SDimitry Andric   for (GlobalObject &GO : M.global_objects()) {
158b915e9e0SDimitry Andric     SmallVector<MDNode *, 1> MDs;
159b915e9e0SDimitry Andric     GO.getMetadata(LLVMContext::MD_type, MDs);
160b915e9e0SDimitry Andric 
161b915e9e0SDimitry Andric     GO.eraseMetadata(LLVMContext::MD_type);
162e3b55780SDimitry Andric     for (auto *MD : MDs) {
163b915e9e0SDimitry Andric       auto I = LocalToGlobal.find(MD->getOperand(1));
164b915e9e0SDimitry Andric       if (I == LocalToGlobal.end()) {
165b915e9e0SDimitry Andric         GO.addMetadata(LLVMContext::MD_type, *MD);
166b915e9e0SDimitry Andric         continue;
167b915e9e0SDimitry Andric       }
168b915e9e0SDimitry Andric       GO.addMetadata(
169b915e9e0SDimitry Andric           LLVMContext::MD_type,
170eb11fae6SDimitry Andric           *MDNode::get(M.getContext(), {MD->getOperand(0), I->second}));
171b915e9e0SDimitry Andric     }
172b915e9e0SDimitry Andric   }
173b915e9e0SDimitry Andric }
174b915e9e0SDimitry Andric 
175b915e9e0SDimitry Andric // Drop unused globals, and drop type information from function declarations.
176b915e9e0SDimitry Andric // FIXME: If we made functions typeless then there would be no need to do this.
simplifyExternals(Module & M)177b915e9e0SDimitry Andric void simplifyExternals(Module &M) {
178b915e9e0SDimitry Andric   FunctionType *EmptyFT =
179b915e9e0SDimitry Andric       FunctionType::get(Type::getVoidTy(M.getContext()), false);
180b915e9e0SDimitry Andric 
181c0981da4SDimitry Andric   for (Function &F : llvm::make_early_inc_range(M)) {
182b915e9e0SDimitry Andric     if (F.isDeclaration() && F.use_empty()) {
183b915e9e0SDimitry Andric       F.eraseFromParent();
184b915e9e0SDimitry Andric       continue;
185b915e9e0SDimitry Andric     }
186b915e9e0SDimitry Andric 
187044eb2f6SDimitry Andric     if (!F.isDeclaration() || F.getFunctionType() == EmptyFT ||
188044eb2f6SDimitry Andric         // Changing the type of an intrinsic may invalidate the IR.
189b1c73532SDimitry Andric         F.getName().starts_with("llvm."))
190b915e9e0SDimitry Andric       continue;
191b915e9e0SDimitry Andric 
192b915e9e0SDimitry Andric     Function *NewF =
193d8e91e46SDimitry Andric         Function::Create(EmptyFT, GlobalValue::ExternalLinkage,
194d8e91e46SDimitry Andric                          F.getAddressSpace(), "", &M);
195344a3780SDimitry Andric     NewF->copyAttributesFrom(&F);
196344a3780SDimitry Andric     // Only copy function attribtues.
197c0981da4SDimitry Andric     NewF->setAttributes(AttributeList::get(M.getContext(),
198c0981da4SDimitry Andric                                            AttributeList::FunctionIndex,
199c0981da4SDimitry Andric                                            F.getAttributes().getFnAttrs()));
200b915e9e0SDimitry Andric     NewF->takeName(&F);
201b1c73532SDimitry Andric     F.replaceAllUsesWith(NewF);
202b915e9e0SDimitry Andric     F.eraseFromParent();
203b915e9e0SDimitry Andric   }
204b915e9e0SDimitry Andric 
2057fa27ce4SDimitry Andric   for (GlobalIFunc &I : llvm::make_early_inc_range(M.ifuncs())) {
2067fa27ce4SDimitry Andric     if (I.use_empty())
2077fa27ce4SDimitry Andric       I.eraseFromParent();
2087fa27ce4SDimitry Andric     else
2097fa27ce4SDimitry Andric       assert(I.getResolverFunction() && "ifunc misses its resolver function");
2107fa27ce4SDimitry Andric   }
2117fa27ce4SDimitry Andric 
212c0981da4SDimitry Andric   for (GlobalVariable &GV : llvm::make_early_inc_range(M.globals())) {
213b915e9e0SDimitry Andric     if (GV.isDeclaration() && GV.use_empty()) {
214b915e9e0SDimitry Andric       GV.eraseFromParent();
215b915e9e0SDimitry Andric       continue;
216b915e9e0SDimitry Andric     }
217b915e9e0SDimitry Andric   }
218b915e9e0SDimitry Andric }
219b915e9e0SDimitry Andric 
220eb11fae6SDimitry Andric static void
filterModule(Module * M,function_ref<bool (const GlobalValue *)> ShouldKeepDefinition)221eb11fae6SDimitry Andric filterModule(Module *M,
222eb11fae6SDimitry Andric              function_ref<bool(const GlobalValue *)> ShouldKeepDefinition) {
223eb11fae6SDimitry Andric   std::vector<GlobalValue *> V;
224eb11fae6SDimitry Andric   for (GlobalValue &GV : M->global_values())
225eb11fae6SDimitry Andric     if (!ShouldKeepDefinition(&GV))
226eb11fae6SDimitry Andric       V.push_back(&GV);
227b915e9e0SDimitry Andric 
228eb11fae6SDimitry Andric   for (GlobalValue *GV : V)
229eb11fae6SDimitry Andric     if (!convertToDeclaration(*GV))
230eb11fae6SDimitry Andric       GV->eraseFromParent();
23171d5a254SDimitry Andric }
23271d5a254SDimitry Andric 
forEachVirtualFunction(Constant * C,function_ref<void (Function *)> Fn)23371d5a254SDimitry Andric void forEachVirtualFunction(Constant *C, function_ref<void(Function *)> Fn) {
23471d5a254SDimitry Andric   if (auto *F = dyn_cast<Function>(C))
23571d5a254SDimitry Andric     return Fn(F);
23671d5a254SDimitry Andric   if (isa<GlobalValue>(C))
23771d5a254SDimitry Andric     return;
23871d5a254SDimitry Andric   for (Value *Op : C->operands())
23971d5a254SDimitry Andric     forEachVirtualFunction(cast<Constant>(Op), Fn);
240b915e9e0SDimitry Andric }
241b915e9e0SDimitry Andric 
242344a3780SDimitry Andric // Clone any @llvm[.compiler].used over to the new module and append
243344a3780SDimitry Andric // values whose defs were cloned into that module.
cloneUsedGlobalVariables(const Module & SrcM,Module & DestM,bool CompilerUsed)244344a3780SDimitry Andric static void cloneUsedGlobalVariables(const Module &SrcM, Module &DestM,
245344a3780SDimitry Andric                                      bool CompilerUsed) {
246344a3780SDimitry Andric   SmallVector<GlobalValue *, 4> Used, NewUsed;
247344a3780SDimitry Andric   // First collect those in the llvm[.compiler].used set.
248344a3780SDimitry Andric   collectUsedGlobalVariables(SrcM, Used, CompilerUsed);
249344a3780SDimitry Andric   // Next build a set of the equivalent values defined in DestM.
250344a3780SDimitry Andric   for (auto *V : Used) {
251344a3780SDimitry Andric     auto *GV = DestM.getNamedValue(V->getName());
252344a3780SDimitry Andric     if (GV && !GV->isDeclaration())
253344a3780SDimitry Andric       NewUsed.push_back(GV);
254344a3780SDimitry Andric   }
255344a3780SDimitry Andric   // Finally, add them to a llvm[.compiler].used variable in DestM.
256344a3780SDimitry Andric   if (CompilerUsed)
257344a3780SDimitry Andric     appendToCompilerUsed(DestM, NewUsed);
258344a3780SDimitry Andric   else
259344a3780SDimitry Andric     appendToUsed(DestM, NewUsed);
260344a3780SDimitry Andric }
261344a3780SDimitry Andric 
2627fa27ce4SDimitry Andric #ifndef NDEBUG
enableUnifiedLTO(Module & M)2637fa27ce4SDimitry Andric static bool enableUnifiedLTO(Module &M) {
2647fa27ce4SDimitry Andric   bool UnifiedLTO = false;
2657fa27ce4SDimitry Andric   if (auto *MD =
2667fa27ce4SDimitry Andric           mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("UnifiedLTO")))
2677fa27ce4SDimitry Andric     UnifiedLTO = MD->getZExtValue();
2687fa27ce4SDimitry Andric   return UnifiedLTO;
2697fa27ce4SDimitry Andric }
2707fa27ce4SDimitry Andric #endif
2717fa27ce4SDimitry Andric 
272b915e9e0SDimitry Andric // If it's possible to split M into regular and thin LTO parts, do so and write
273b915e9e0SDimitry Andric // a multi-module bitcode file with the two parts to OS. Otherwise, write only a
274b915e9e0SDimitry Andric // regular LTO bitcode file to OS.
splitAndWriteThinLTOBitcode(raw_ostream & OS,raw_ostream * ThinLinkOS,function_ref<AAResults & (Function &)> AARGetter,Module & M)27571d5a254SDimitry Andric void splitAndWriteThinLTOBitcode(
27671d5a254SDimitry Andric     raw_ostream &OS, raw_ostream *ThinLinkOS,
27771d5a254SDimitry Andric     function_ref<AAResults &(Function &)> AARGetter, Module &M) {
278a303c417SDimitry Andric   std::string ModuleId = getUniqueModuleId(&M);
279b915e9e0SDimitry Andric   if (ModuleId.empty()) {
2807fa27ce4SDimitry Andric     assert(!enableUnifiedLTO(M));
281eb11fae6SDimitry Andric     // We couldn't generate a module ID for this module, write it out as a
282eb11fae6SDimitry Andric     // regular LTO module with an index for summary-based dead stripping.
283eb11fae6SDimitry Andric     ProfileSummaryInfo PSI(M);
284eb11fae6SDimitry Andric     M.addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
285eb11fae6SDimitry Andric     ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI);
2867fa27ce4SDimitry Andric     WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false, &Index,
2877fa27ce4SDimitry Andric                        /*UnifiedLTO=*/false);
288eb11fae6SDimitry Andric 
28971d5a254SDimitry Andric     if (ThinLinkOS)
29071d5a254SDimitry Andric       // We don't have a ThinLTO part, but still write the module to the
29171d5a254SDimitry Andric       // ThinLinkOS if requested so that the expected output file is produced.
292eb11fae6SDimitry Andric       WriteBitcodeToFile(M, *ThinLinkOS, /*ShouldPreserveUseListOrder=*/false,
2937fa27ce4SDimitry Andric                          &Index, /*UnifiedLTO=*/false);
294eb11fae6SDimitry Andric 
295b915e9e0SDimitry Andric     return;
296b915e9e0SDimitry Andric   }
297b915e9e0SDimitry Andric 
298b915e9e0SDimitry Andric   promoteTypeIds(M, ModuleId);
299b915e9e0SDimitry Andric 
3001d5ae102SDimitry Andric   // Returns whether a global or its associated global has attached type
3011d5ae102SDimitry Andric   // metadata. The former may participate in CFI or whole-program
3021d5ae102SDimitry Andric   // devirtualization, so they need to appear in the merged module instead of
3031d5ae102SDimitry Andric   // the thin LTO module. Similarly, globals that are associated with globals
3041d5ae102SDimitry Andric   // with type metadata need to appear in the merged module because they will
3051d5ae102SDimitry Andric   // reference the global's section directly.
306eb11fae6SDimitry Andric   auto HasTypeMetadata = [](const GlobalObject *GO) {
3071d5ae102SDimitry Andric     if (MDNode *MD = GO->getMetadata(LLVMContext::MD_associated))
3081d5ae102SDimitry Andric       if (auto *AssocVM = dyn_cast_or_null<ValueAsMetadata>(MD->getOperand(0)))
3091d5ae102SDimitry Andric         if (auto *AssocGO = dyn_cast<GlobalObject>(AssocVM->getValue()))
3101d5ae102SDimitry Andric           if (AssocGO->hasMetadata(LLVMContext::MD_type))
3111d5ae102SDimitry Andric             return true;
312eb11fae6SDimitry Andric     return GO->hasMetadata(LLVMContext::MD_type);
313b915e9e0SDimitry Andric   };
314b915e9e0SDimitry Andric 
31571d5a254SDimitry Andric   // Collect the set of virtual functions that are eligible for virtual constant
31671d5a254SDimitry Andric   // propagation. Each eligible function must not access memory, must return
31771d5a254SDimitry Andric   // an integer of width <=64 bits, must take at least one argument, must not
31871d5a254SDimitry Andric   // use its first argument (assumed to be "this") and all arguments other than
31971d5a254SDimitry Andric   // the first one must be of <=64 bit integer type.
32071d5a254SDimitry Andric   //
32171d5a254SDimitry Andric   // Note that we test whether this copy of the function is readnone, rather
32271d5a254SDimitry Andric   // than testing function attributes, which must hold for any copy of the
32371d5a254SDimitry Andric   // function, even a less optimized version substituted at link time. This is
32471d5a254SDimitry Andric   // sound because the virtual constant propagation optimizations effectively
32571d5a254SDimitry Andric   // inline all implementations of the virtual function into each call site,
32671d5a254SDimitry Andric   // rather than using function attributes to perform local optimization.
327d8e91e46SDimitry Andric   DenseSet<const Function *> EligibleVirtualFns;
32871d5a254SDimitry Andric   // If any member of a comdat lives in MergedM, put all members of that
32971d5a254SDimitry Andric   // comdat in MergedM to keep the comdat together.
33071d5a254SDimitry Andric   DenseSet<const Comdat *> MergedMComdats;
33171d5a254SDimitry Andric   for (GlobalVariable &GV : M.globals())
332b1c73532SDimitry Andric     if (!GV.isDeclaration() && HasTypeMetadata(&GV)) {
33371d5a254SDimitry Andric       if (const auto *C = GV.getComdat())
33471d5a254SDimitry Andric         MergedMComdats.insert(C);
33571d5a254SDimitry Andric       forEachVirtualFunction(GV.getInitializer(), [&](Function *F) {
33671d5a254SDimitry Andric         auto *RT = dyn_cast<IntegerType>(F->getReturnType());
33771d5a254SDimitry Andric         if (!RT || RT->getBitWidth() > 64 || F->arg_empty() ||
33871d5a254SDimitry Andric             !F->arg_begin()->use_empty())
33971d5a254SDimitry Andric           return;
340b60736ecSDimitry Andric         for (auto &Arg : drop_begin(F->args())) {
34171d5a254SDimitry Andric           auto *ArgT = dyn_cast<IntegerType>(Arg.getType());
34271d5a254SDimitry Andric           if (!ArgT || ArgT->getBitWidth() > 64)
34371d5a254SDimitry Andric             return;
34471d5a254SDimitry Andric         }
345ca089b24SDimitry Andric         if (!F->isDeclaration() &&
346e3b55780SDimitry Andric             computeFunctionBodyMemoryAccess(*F, AARGetter(*F))
347e3b55780SDimitry Andric                 .doesNotAccessMemory())
34871d5a254SDimitry Andric           EligibleVirtualFns.insert(F);
34971d5a254SDimitry Andric       });
35071d5a254SDimitry Andric     }
351b915e9e0SDimitry Andric 
35271d5a254SDimitry Andric   ValueToValueMapTy VMap;
35371d5a254SDimitry Andric   std::unique_ptr<Module> MergedM(
354eb11fae6SDimitry Andric       CloneModule(M, VMap, [&](const GlobalValue *GV) -> bool {
35571d5a254SDimitry Andric         if (const auto *C = GV->getComdat())
35671d5a254SDimitry Andric           if (MergedMComdats.count(C))
35771d5a254SDimitry Andric             return true;
35871d5a254SDimitry Andric         if (auto *F = dyn_cast<Function>(GV))
35971d5a254SDimitry Andric           return EligibleVirtualFns.count(F);
360c0981da4SDimitry Andric         if (auto *GVar =
361c0981da4SDimitry Andric                 dyn_cast_or_null<GlobalVariable>(GV->getAliaseeObject()))
36271d5a254SDimitry Andric           return HasTypeMetadata(GVar);
36371d5a254SDimitry Andric         return false;
36471d5a254SDimitry Andric       }));
36571d5a254SDimitry Andric   StripDebugInfo(*MergedM);
366eb11fae6SDimitry Andric   MergedM->setModuleInlineAsm("");
36771d5a254SDimitry Andric 
368344a3780SDimitry Andric   // Clone any llvm.*used globals to ensure the included values are
369344a3780SDimitry Andric   // not deleted.
370344a3780SDimitry Andric   cloneUsedGlobalVariables(M, *MergedM, /*CompilerUsed*/ false);
371344a3780SDimitry Andric   cloneUsedGlobalVariables(M, *MergedM, /*CompilerUsed*/ true);
372344a3780SDimitry Andric 
37371d5a254SDimitry Andric   for (Function &F : *MergedM)
37471d5a254SDimitry Andric     if (!F.isDeclaration()) {
37571d5a254SDimitry Andric       // Reset the linkage of all functions eligible for virtual constant
37671d5a254SDimitry Andric       // propagation. The canonical definitions live in the thin LTO module so
37771d5a254SDimitry Andric       // that they can be imported.
37871d5a254SDimitry Andric       F.setLinkage(GlobalValue::AvailableExternallyLinkage);
37971d5a254SDimitry Andric       F.setComdat(nullptr);
38071d5a254SDimitry Andric     }
38171d5a254SDimitry Andric 
3827c7aba6eSDimitry Andric   SetVector<GlobalValue *> CfiFunctions;
3837c7aba6eSDimitry Andric   for (auto &F : M)
3847c7aba6eSDimitry Andric     if ((!F.hasLocalLinkage() || F.hasAddressTaken()) && HasTypeMetadata(&F))
3857c7aba6eSDimitry Andric       CfiFunctions.insert(&F);
3867c7aba6eSDimitry Andric 
38771d5a254SDimitry Andric   // Remove all globals with type metadata, globals with comdats that live in
38871d5a254SDimitry Andric   // MergedM, and aliases pointing to such globals from the thin LTO module.
38971d5a254SDimitry Andric   filterModule(&M, [&](const GlobalValue *GV) {
390c0981da4SDimitry Andric     if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getAliaseeObject()))
39171d5a254SDimitry Andric       if (HasTypeMetadata(GVar))
39271d5a254SDimitry Andric         return false;
39371d5a254SDimitry Andric     if (const auto *C = GV->getComdat())
39471d5a254SDimitry Andric       if (MergedMComdats.count(C))
39571d5a254SDimitry Andric         return false;
39671d5a254SDimitry Andric     return true;
39771d5a254SDimitry Andric   });
398b915e9e0SDimitry Andric 
3997c7aba6eSDimitry Andric   promoteInternals(*MergedM, M, ModuleId, CfiFunctions);
4007c7aba6eSDimitry Andric   promoteInternals(M, *MergedM, ModuleId, CfiFunctions);
4017c7aba6eSDimitry Andric 
402eb11fae6SDimitry Andric   auto &Ctx = MergedM->getContext();
4037c7aba6eSDimitry Andric   SmallVector<MDNode *, 8> CfiFunctionMDs;
404e3b55780SDimitry Andric   for (auto *V : CfiFunctions) {
4057c7aba6eSDimitry Andric     Function &F = *cast<Function>(V);
4067c7aba6eSDimitry Andric     SmallVector<MDNode *, 2> Types;
4077c7aba6eSDimitry Andric     F.getMetadata(LLVMContext::MD_type, Types);
4087c7aba6eSDimitry Andric 
4097c7aba6eSDimitry Andric     SmallVector<Metadata *, 4> Elts;
4107c7aba6eSDimitry Andric     Elts.push_back(MDString::get(Ctx, F.getName()));
4117c7aba6eSDimitry Andric     CfiFunctionLinkage Linkage;
4121d5ae102SDimitry Andric     if (lowertypetests::isJumpTableCanonical(&F))
4137c7aba6eSDimitry Andric       Linkage = CFL_Definition;
4141d5ae102SDimitry Andric     else if (F.hasExternalWeakLinkage())
4157c7aba6eSDimitry Andric       Linkage = CFL_WeakDeclaration;
4167c7aba6eSDimitry Andric     else
4177c7aba6eSDimitry Andric       Linkage = CFL_Declaration;
4187c7aba6eSDimitry Andric     Elts.push_back(ConstantAsMetadata::get(
4197c7aba6eSDimitry Andric         llvm::ConstantInt::get(Type::getInt8Ty(Ctx), Linkage)));
420b60736ecSDimitry Andric     append_range(Elts, Types);
4217c7aba6eSDimitry Andric     CfiFunctionMDs.push_back(MDTuple::get(Ctx, Elts));
4227c7aba6eSDimitry Andric   }
4237c7aba6eSDimitry Andric 
4247c7aba6eSDimitry Andric   if(!CfiFunctionMDs.empty()) {
4257c7aba6eSDimitry Andric     NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("cfi.functions");
426e3b55780SDimitry Andric     for (auto *MD : CfiFunctionMDs)
4277c7aba6eSDimitry Andric       NMD->addOperand(MD);
4287c7aba6eSDimitry Andric   }
429b915e9e0SDimitry Andric 
430eb11fae6SDimitry Andric   SmallVector<MDNode *, 8> FunctionAliases;
431eb11fae6SDimitry Andric   for (auto &A : M.aliases()) {
432eb11fae6SDimitry Andric     if (!isa<Function>(A.getAliasee()))
433eb11fae6SDimitry Andric       continue;
434eb11fae6SDimitry Andric 
435eb11fae6SDimitry Andric     auto *F = cast<Function>(A.getAliasee());
436eb11fae6SDimitry Andric 
437eb11fae6SDimitry Andric     Metadata *Elts[] = {
438eb11fae6SDimitry Andric         MDString::get(Ctx, A.getName()),
439eb11fae6SDimitry Andric         MDString::get(Ctx, F->getName()),
440eb11fae6SDimitry Andric         ConstantAsMetadata::get(
441eb11fae6SDimitry Andric             ConstantInt::get(Type::getInt8Ty(Ctx), A.getVisibility())),
442eb11fae6SDimitry Andric         ConstantAsMetadata::get(
443eb11fae6SDimitry Andric             ConstantInt::get(Type::getInt8Ty(Ctx), A.isWeakForLinker())),
444eb11fae6SDimitry Andric     };
445eb11fae6SDimitry Andric 
446eb11fae6SDimitry Andric     FunctionAliases.push_back(MDTuple::get(Ctx, Elts));
447eb11fae6SDimitry Andric   }
448eb11fae6SDimitry Andric 
449eb11fae6SDimitry Andric   if (!FunctionAliases.empty()) {
450eb11fae6SDimitry Andric     NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("aliases");
451e3b55780SDimitry Andric     for (auto *MD : FunctionAliases)
452eb11fae6SDimitry Andric       NMD->addOperand(MD);
453eb11fae6SDimitry Andric   }
454eb11fae6SDimitry Andric 
455eb11fae6SDimitry Andric   SmallVector<MDNode *, 8> Symvers;
456eb11fae6SDimitry Andric   ModuleSymbolTable::CollectAsmSymvers(M, [&](StringRef Name, StringRef Alias) {
457eb11fae6SDimitry Andric     Function *F = M.getFunction(Name);
458eb11fae6SDimitry Andric     if (!F || F->use_empty())
459eb11fae6SDimitry Andric       return;
460eb11fae6SDimitry Andric 
461eb11fae6SDimitry Andric     Symvers.push_back(MDTuple::get(
462eb11fae6SDimitry Andric         Ctx, {MDString::get(Ctx, Name), MDString::get(Ctx, Alias)}));
463eb11fae6SDimitry Andric   });
464eb11fae6SDimitry Andric 
465eb11fae6SDimitry Andric   if (!Symvers.empty()) {
466eb11fae6SDimitry Andric     NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("symvers");
467e3b55780SDimitry Andric     for (auto *MD : Symvers)
468eb11fae6SDimitry Andric       NMD->addOperand(MD);
469eb11fae6SDimitry Andric   }
470eb11fae6SDimitry Andric 
471b915e9e0SDimitry Andric   simplifyExternals(*MergedM);
472b915e9e0SDimitry Andric 
473b915e9e0SDimitry Andric   // FIXME: Try to re-use BSI and PFI from the original module here.
4746b3f41edSDimitry Andric   ProfileSummaryInfo PSI(M);
4756b3f41edSDimitry Andric   ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI);
47671d5a254SDimitry Andric 
4777ab83427SDimitry Andric   // Mark the merged module as requiring full LTO. We still want an index for
4787ab83427SDimitry Andric   // it though, so that it can participate in summary-based dead stripping.
4797ab83427SDimitry Andric   MergedM->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
4807ab83427SDimitry Andric   ModuleSummaryIndex MergedMIndex =
4817ab83427SDimitry Andric       buildModuleSummaryIndex(*MergedM, nullptr, &PSI);
4827ab83427SDimitry Andric 
48371d5a254SDimitry Andric   SmallVector<char, 0> Buffer;
48471d5a254SDimitry Andric 
48571d5a254SDimitry Andric   BitcodeWriter W(Buffer);
48671d5a254SDimitry Andric   // Save the module hash produced for the full bitcode, which will
48771d5a254SDimitry Andric   // be used in the backends, and use that in the minimized bitcode
48871d5a254SDimitry Andric   // produced for the full link.
48971d5a254SDimitry Andric   ModuleHash ModHash = {{0}};
490eb11fae6SDimitry Andric   W.writeModule(M, /*ShouldPreserveUseListOrder=*/false, &Index,
49171d5a254SDimitry Andric                 /*GenerateHash=*/true, &ModHash);
492eb11fae6SDimitry Andric   W.writeModule(*MergedM, /*ShouldPreserveUseListOrder=*/false, &MergedMIndex);
4939df3605dSDimitry Andric   W.writeSymtab();
494d99dafe2SDimitry Andric   W.writeStrtab();
495b915e9e0SDimitry Andric   OS << Buffer;
49671d5a254SDimitry Andric 
497044eb2f6SDimitry Andric   // If a minimized bitcode module was requested for the thin link, only
498044eb2f6SDimitry Andric   // the information that is needed by thin link will be written in the
499044eb2f6SDimitry Andric   // given OS (the merged module will be written as usual).
50071d5a254SDimitry Andric   if (ThinLinkOS) {
50171d5a254SDimitry Andric     Buffer.clear();
50271d5a254SDimitry Andric     BitcodeWriter W2(Buffer);
50371d5a254SDimitry Andric     StripDebugInfo(M);
504eb11fae6SDimitry Andric     W2.writeThinLinkBitcode(M, Index, ModHash);
505eb11fae6SDimitry Andric     W2.writeModule(*MergedM, /*ShouldPreserveUseListOrder=*/false,
5067ab83427SDimitry Andric                    &MergedMIndex);
5079df3605dSDimitry Andric     W2.writeSymtab();
508d99dafe2SDimitry Andric     W2.writeStrtab();
50971d5a254SDimitry Andric     *ThinLinkOS << Buffer;
51071d5a254SDimitry Andric   }
511b915e9e0SDimitry Andric }
512b915e9e0SDimitry Andric 
513e6d15924SDimitry Andric // Check if the LTO Unit splitting has been enabled.
enableSplitLTOUnit(Module & M)514e6d15924SDimitry Andric bool enableSplitLTOUnit(Module &M) {
515d8e91e46SDimitry Andric   bool EnableSplitLTOUnit = false;
516d8e91e46SDimitry Andric   if (auto *MD = mdconst::extract_or_null<ConstantInt>(
517d8e91e46SDimitry Andric           M.getModuleFlag("EnableSplitLTOUnit")))
518d8e91e46SDimitry Andric     EnableSplitLTOUnit = MD->getZExtValue();
519e6d15924SDimitry Andric   return EnableSplitLTOUnit;
520e6d15924SDimitry Andric }
521d8e91e46SDimitry Andric 
522e6d15924SDimitry Andric // Returns whether this module needs to be split because it uses type metadata.
hasTypeMetadata(Module & M)523e6d15924SDimitry Andric bool hasTypeMetadata(Module &M) {
524b915e9e0SDimitry Andric   for (auto &GO : M.global_objects()) {
525eb11fae6SDimitry Andric     if (GO.hasMetadata(LLVMContext::MD_type))
526b915e9e0SDimitry Andric       return true;
527b915e9e0SDimitry Andric   }
528b915e9e0SDimitry Andric   return false;
529b915e9e0SDimitry Andric }
530b915e9e0SDimitry Andric 
writeThinLTOBitcode(raw_ostream & OS,raw_ostream * ThinLinkOS,function_ref<AAResults & (Function &)> AARGetter,Module & M,const ModuleSummaryIndex * Index)5317fa27ce4SDimitry Andric bool writeThinLTOBitcode(raw_ostream &OS, raw_ostream *ThinLinkOS,
53271d5a254SDimitry Andric                          function_ref<AAResults &(Function &)> AARGetter,
53371d5a254SDimitry Andric                          Module &M, const ModuleSummaryIndex *Index) {
534e6d15924SDimitry Andric   std::unique_ptr<ModuleSummaryIndex> NewIndex = nullptr;
535e6d15924SDimitry Andric   // See if this module has any type metadata. If so, we try to split it
536e6d15924SDimitry Andric   // or at least promote type ids to enable WPD.
537e6d15924SDimitry Andric   if (hasTypeMetadata(M)) {
5387fa27ce4SDimitry Andric     if (enableSplitLTOUnit(M)) {
5397fa27ce4SDimitry Andric       splitAndWriteThinLTOBitcode(OS, ThinLinkOS, AARGetter, M);
5407fa27ce4SDimitry Andric       return true;
5417fa27ce4SDimitry Andric     }
542e6d15924SDimitry Andric     // Promote type ids as needed for index-based WPD.
543e6d15924SDimitry Andric     std::string ModuleId = getUniqueModuleId(&M);
544e6d15924SDimitry Andric     if (!ModuleId.empty()) {
545e6d15924SDimitry Andric       promoteTypeIds(M, ModuleId);
546e6d15924SDimitry Andric       // Need to rebuild the index so that it contains type metadata
547e6d15924SDimitry Andric       // for the newly promoted type ids.
548e6d15924SDimitry Andric       // FIXME: Probably should not bother building the index at all
549e6d15924SDimitry Andric       // in the caller of writeThinLTOBitcode (which does so via the
550e6d15924SDimitry Andric       // ModuleSummaryIndexAnalysis pass), since we have to rebuild it
551e6d15924SDimitry Andric       // anyway whenever there is type metadata (here or in
552e6d15924SDimitry Andric       // splitAndWriteThinLTOBitcode). Just always build it once via the
553e6d15924SDimitry Andric       // buildModuleSummaryIndex when Module(s) are ready.
554e6d15924SDimitry Andric       ProfileSummaryInfo PSI(M);
5551d5ae102SDimitry Andric       NewIndex = std::make_unique<ModuleSummaryIndex>(
556e6d15924SDimitry Andric           buildModuleSummaryIndex(M, nullptr, &PSI));
557e6d15924SDimitry Andric       Index = NewIndex.get();
558e6d15924SDimitry Andric     }
559e6d15924SDimitry Andric   }
560b915e9e0SDimitry Andric 
561e6d15924SDimitry Andric   // Write it out as an unsplit ThinLTO module.
56271d5a254SDimitry Andric 
56371d5a254SDimitry Andric   // Save the module hash produced for the full bitcode, which will
56471d5a254SDimitry Andric   // be used in the backends, and use that in the minimized bitcode
56571d5a254SDimitry Andric   // produced for the full link.
56671d5a254SDimitry Andric   ModuleHash ModHash = {{0}};
567eb11fae6SDimitry Andric   WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false, Index,
56871d5a254SDimitry Andric                      /*GenerateHash=*/true, &ModHash);
569044eb2f6SDimitry Andric   // If a minimized bitcode module was requested for the thin link, only
570044eb2f6SDimitry Andric   // the information that is needed by thin link will be written in the
571044eb2f6SDimitry Andric   // given OS.
572044eb2f6SDimitry Andric   if (ThinLinkOS && Index)
573ecbca9f5SDimitry Andric     writeThinLinkBitcodeToFile(M, *ThinLinkOS, *Index, ModHash);
5747fa27ce4SDimitry Andric   return false;
575b915e9e0SDimitry Andric }
576b915e9e0SDimitry Andric 
577b915e9e0SDimitry Andric } // anonymous namespace
578ac9a064cSDimitry Andric extern bool WriteNewDbgInfoFormatToBitcode;
579f382538dSDimitry Andric PreservedAnalyses
run(Module & M,ModuleAnalysisManager & AM)580f382538dSDimitry Andric llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
581f382538dSDimitry Andric   FunctionAnalysisManager &FAM =
582f382538dSDimitry Andric       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
583ac9a064cSDimitry Andric 
584ac9a064cSDimitry Andric   ScopedDbgInfoFormatSetter FormatSetter(M, M.IsNewDbgInfoFormat &&
585ac9a064cSDimitry Andric                                                 WriteNewDbgInfoFormatToBitcode);
586ac9a064cSDimitry Andric   if (M.IsNewDbgInfoFormat)
587ac9a064cSDimitry Andric     M.removeDebugIntrinsicDeclarations();
588ac9a064cSDimitry Andric 
5897fa27ce4SDimitry Andric   bool Changed = writeThinLTOBitcode(
5907fa27ce4SDimitry Andric       OS, ThinLinkOS,
591f382538dSDimitry Andric       [&FAM](Function &F) -> AAResults & {
592f382538dSDimitry Andric         return FAM.getResult<AAManager>(F);
593f382538dSDimitry Andric       },
594f382538dSDimitry Andric       M, &AM.getResult<ModuleSummaryIndexAnalysis>(M));
595ac9a064cSDimitry Andric 
5967fa27ce4SDimitry Andric   return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
597f382538dSDimitry Andric }
598