11398a32eSPeter Collingbourne //===- ThinLTOBitcodeWriter.cpp - Bitcode writing pass for ThinLTO --------===//
21398a32eSPeter Collingbourne //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61398a32eSPeter Collingbourne //
71398a32eSPeter Collingbourne //===----------------------------------------------------------------------===//
81398a32eSPeter Collingbourne 
96b411418STim Shen #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
10002c2d53SPeter Collingbourne #include "llvm/Analysis/BasicAliasAnalysis.h"
11384ca190SMehdi Amini #include "llvm/Analysis/ModuleSummaryAnalysis.h"
1294624acaSTeresa Johnson #include "llvm/Analysis/ProfileSummaryInfo.h"
131398a32eSPeter Collingbourne #include "llvm/Analysis/TypeMetadataUtils.h"
141398a32eSPeter Collingbourne #include "llvm/Bitcode/BitcodeWriter.h"
151398a32eSPeter Collingbourne #include "llvm/IR/Constants.h"
1628ffd326SPeter Collingbourne #include "llvm/IR/DebugInfo.h"
179ed8e0caSdfukalov #include "llvm/IR/Instructions.h"
181398a32eSPeter Collingbourne #include "llvm/IR/Intrinsics.h"
191398a32eSPeter Collingbourne #include "llvm/IR/Module.h"
201398a32eSPeter Collingbourne #include "llvm/IR/PassManager.h"
2105da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
22230b2567SVlad Tsyrklevich #include "llvm/Object/ModuleSymbolTable.h"
231398a32eSPeter Collingbourne #include "llvm/Pass.h"
241398a32eSPeter Collingbourne #include "llvm/Support/ScopedPrinter.h"
250c6a4ff8STeresa Johnson #include "llvm/Support/raw_ostream.h"
260c6a4ff8STeresa Johnson #include "llvm/Transforms/IPO.h"
27002c2d53SPeter Collingbourne #include "llvm/Transforms/IPO/FunctionAttrs.h"
28d3704f67SGeorge Rimar #include "llvm/Transforms/IPO/FunctionImport.h"
290e497d15SPeter Collingbourne #include "llvm/Transforms/IPO/LowerTypeTests.h"
301398a32eSPeter Collingbourne #include "llvm/Transforms/Utils/Cloning.h"
31964f4663SEvgeniy Stepanov #include "llvm/Transforms/Utils/ModuleUtils.h"
321398a32eSPeter Collingbourne using namespace llvm;
331398a32eSPeter Collingbourne 
341398a32eSPeter Collingbourne namespace {
351398a32eSPeter Collingbourne 
367ce1c4daSSami Tolvanen // Determine if a promotion alias should be created for a symbol name.
377ce1c4daSSami Tolvanen static bool allowPromotionAlias(const std::string &Name) {
387ce1c4daSSami Tolvanen   // Promotion aliases are used only in inline assembly. It's safe to
397ce1c4daSSami Tolvanen   // simply skip unusual names. Subset of MCAsmInfo::isAcceptableChar()
407ce1c4daSSami Tolvanen   // and MCAsmInfoXCOFF::isAcceptableChar().
417ce1c4daSSami Tolvanen   for (const char &C : Name) {
427ce1c4daSSami Tolvanen     if (isAlnum(C) || C == '_' || C == '.')
437ce1c4daSSami Tolvanen       continue;
447ce1c4daSSami Tolvanen     return false;
457ce1c4daSSami Tolvanen   }
467ce1c4daSSami Tolvanen   return true;
477ce1c4daSSami Tolvanen }
487ce1c4daSSami Tolvanen 
491398a32eSPeter Collingbourne // Promote each local-linkage entity defined by ExportM and used by ImportM by
501398a32eSPeter Collingbourne // changing visibility and appending the given ModuleId.
514d4ee93dSEvgeniy Stepanov void promoteInternals(Module &ExportM, Module &ImportM, StringRef ModuleId,
524d4ee93dSEvgeniy Stepanov                       SetVector<GlobalValue *> &PromoteExtra) {
534075ccc7SBob Haarman   DenseMap<const Comdat *, Comdat *> RenamedComdats;
546b193966SPeter Collingbourne   for (auto &ExportGV : ExportM.global_values()) {
551398a32eSPeter Collingbourne     if (!ExportGV.hasLocalLinkage())
566b193966SPeter Collingbourne       continue;
571398a32eSPeter Collingbourne 
584075ccc7SBob Haarman     auto Name = ExportGV.getName();
591f034226SPeter Collingbourne     GlobalValue *ImportGV = nullptr;
601f034226SPeter Collingbourne     if (!PromoteExtra.count(&ExportGV)) {
611f034226SPeter Collingbourne       ImportGV = ImportM.getNamedValue(Name);
621f034226SPeter Collingbourne       if (!ImportGV)
636b193966SPeter Collingbourne         continue;
641f034226SPeter Collingbourne       ImportGV->removeDeadConstantUsers();
651f034226SPeter Collingbourne       if (ImportGV->use_empty()) {
661f034226SPeter Collingbourne         ImportGV->eraseFromParent();
671f034226SPeter Collingbourne         continue;
681f034226SPeter Collingbourne       }
691f034226SPeter Collingbourne     }
701398a32eSPeter Collingbourne 
717ce1c4daSSami Tolvanen     std::string OldName = Name.str();
724075ccc7SBob Haarman     std::string NewName = (Name + ModuleId).str();
734075ccc7SBob Haarman 
744075ccc7SBob Haarman     if (const auto *C = ExportGV.getComdat())
754075ccc7SBob Haarman       if (C->getName() == Name)
764075ccc7SBob Haarman         RenamedComdats.try_emplace(C, ExportM.getOrInsertComdat(NewName));
771398a32eSPeter Collingbourne 
781398a32eSPeter Collingbourne     ExportGV.setName(NewName);
791398a32eSPeter Collingbourne     ExportGV.setLinkage(GlobalValue::ExternalLinkage);
801398a32eSPeter Collingbourne     ExportGV.setVisibility(GlobalValue::HiddenVisibility);
811398a32eSPeter Collingbourne 
824d4ee93dSEvgeniy Stepanov     if (ImportGV) {
831398a32eSPeter Collingbourne       ImportGV->setName(NewName);
841398a32eSPeter Collingbourne       ImportGV->setVisibility(GlobalValue::HiddenVisibility);
856b193966SPeter Collingbourne     }
867ce1c4daSSami Tolvanen 
877ce1c4daSSami Tolvanen     if (isa<Function>(&ExportGV) && allowPromotionAlias(OldName)) {
887ce1c4daSSami Tolvanen       // Create a local alias with the original name to avoid breaking
897ce1c4daSSami Tolvanen       // references from inline assembly.
907ce1c4daSSami Tolvanen       std::string Alias = ".set " + OldName + "," + NewName + "\n";
917ce1c4daSSami Tolvanen       ExportM.appendModuleInlineAsm(Alias);
927ce1c4daSSami Tolvanen     }
934d4ee93dSEvgeniy Stepanov   }
944075ccc7SBob Haarman 
954075ccc7SBob Haarman   if (!RenamedComdats.empty())
964075ccc7SBob Haarman     for (auto &GO : ExportM.global_objects())
974075ccc7SBob Haarman       if (auto *C = GO.getComdat()) {
984075ccc7SBob Haarman         auto Replacement = RenamedComdats.find(C);
994075ccc7SBob Haarman         if (Replacement != RenamedComdats.end())
1004075ccc7SBob Haarman           GO.setComdat(Replacement->second);
1014075ccc7SBob Haarman       }
1021398a32eSPeter Collingbourne }
1031398a32eSPeter Collingbourne 
1041398a32eSPeter Collingbourne // Promote all internal (i.e. distinct) type ids used by the module by replacing
1051398a32eSPeter Collingbourne // them with external type ids formed using the module id.
1061398a32eSPeter Collingbourne //
1071398a32eSPeter Collingbourne // Note that this needs to be done before we clone the module because each clone
1081398a32eSPeter Collingbourne // will receive its own set of distinct metadata nodes.
1091398a32eSPeter Collingbourne void promoteTypeIds(Module &M, StringRef ModuleId) {
1101398a32eSPeter Collingbourne   DenseMap<Metadata *, Metadata *> LocalToGlobal;
1111398a32eSPeter Collingbourne   auto ExternalizeTypeId = [&](CallInst *CI, unsigned ArgNo) {
1121398a32eSPeter Collingbourne     Metadata *MD =
1131398a32eSPeter Collingbourne         cast<MetadataAsValue>(CI->getArgOperand(ArgNo))->getMetadata();
1141398a32eSPeter Collingbourne 
1151398a32eSPeter Collingbourne     if (isa<MDNode>(MD) && cast<MDNode>(MD)->isDistinct()) {
1161398a32eSPeter Collingbourne       Metadata *&GlobalMD = LocalToGlobal[MD];
1171398a32eSPeter Collingbourne       if (!GlobalMD) {
1183a13ed60SBenjamin Kramer         std::string NewName = (Twine(LocalToGlobal.size()) + ModuleId).str();
1191398a32eSPeter Collingbourne         GlobalMD = MDString::get(M.getContext(), NewName);
1201398a32eSPeter Collingbourne       }
1211398a32eSPeter Collingbourne 
1221398a32eSPeter Collingbourne       CI->setArgOperand(ArgNo,
1231398a32eSPeter Collingbourne                         MetadataAsValue::get(M.getContext(), GlobalMD));
1241398a32eSPeter Collingbourne     }
1251398a32eSPeter Collingbourne   };
1261398a32eSPeter Collingbourne 
1271398a32eSPeter Collingbourne   if (Function *TypeTestFunc =
1281398a32eSPeter Collingbourne           M.getFunction(Intrinsic::getName(Intrinsic::type_test))) {
1291398a32eSPeter Collingbourne     for (const Use &U : TypeTestFunc->uses()) {
1301398a32eSPeter Collingbourne       auto CI = cast<CallInst>(U.getUser());
1311398a32eSPeter Collingbourne       ExternalizeTypeId(CI, 1);
1321398a32eSPeter Collingbourne     }
1331398a32eSPeter Collingbourne   }
1341398a32eSPeter Collingbourne 
1351398a32eSPeter Collingbourne   if (Function *TypeCheckedLoadFunc =
1361398a32eSPeter Collingbourne           M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load))) {
1371398a32eSPeter Collingbourne     for (const Use &U : TypeCheckedLoadFunc->uses()) {
1381398a32eSPeter Collingbourne       auto CI = cast<CallInst>(U.getUser());
1391398a32eSPeter Collingbourne       ExternalizeTypeId(CI, 2);
1401398a32eSPeter Collingbourne     }
1411398a32eSPeter Collingbourne   }
1421398a32eSPeter Collingbourne 
1431398a32eSPeter Collingbourne   for (GlobalObject &GO : M.global_objects()) {
1441398a32eSPeter Collingbourne     SmallVector<MDNode *, 1> MDs;
1451398a32eSPeter Collingbourne     GO.getMetadata(LLVMContext::MD_type, MDs);
1461398a32eSPeter Collingbourne 
1471398a32eSPeter Collingbourne     GO.eraseMetadata(LLVMContext::MD_type);
1481398a32eSPeter Collingbourne     for (auto MD : MDs) {
1491398a32eSPeter Collingbourne       auto I = LocalToGlobal.find(MD->getOperand(1));
1501398a32eSPeter Collingbourne       if (I == LocalToGlobal.end()) {
1511398a32eSPeter Collingbourne         GO.addMetadata(LLVMContext::MD_type, *MD);
1521398a32eSPeter Collingbourne         continue;
1531398a32eSPeter Collingbourne       }
1541398a32eSPeter Collingbourne       GO.addMetadata(
1551398a32eSPeter Collingbourne           LLVMContext::MD_type,
1560deb9a9aSBenjamin Kramer           *MDNode::get(M.getContext(), {MD->getOperand(0), I->second}));
1571398a32eSPeter Collingbourne     }
1581398a32eSPeter Collingbourne   }
1591398a32eSPeter Collingbourne }
1601398a32eSPeter Collingbourne 
1611398a32eSPeter Collingbourne // Drop unused globals, and drop type information from function declarations.
1621398a32eSPeter Collingbourne // FIXME: If we made functions typeless then there would be no need to do this.
1631398a32eSPeter Collingbourne void simplifyExternals(Module &M) {
1641398a32eSPeter Collingbourne   FunctionType *EmptyFT =
1651398a32eSPeter Collingbourne       FunctionType::get(Type::getVoidTy(M.getContext()), false);
1661398a32eSPeter Collingbourne 
1671398a32eSPeter Collingbourne   for (auto I = M.begin(), E = M.end(); I != E;) {
1681398a32eSPeter Collingbourne     Function &F = *I++;
1691398a32eSPeter Collingbourne     if (F.isDeclaration() && F.use_empty()) {
1701398a32eSPeter Collingbourne       F.eraseFromParent();
1711398a32eSPeter Collingbourne       continue;
1721398a32eSPeter Collingbourne     }
1731398a32eSPeter Collingbourne 
17493fdaca5SPeter Collingbourne     if (!F.isDeclaration() || F.getFunctionType() == EmptyFT ||
17593fdaca5SPeter Collingbourne         // Changing the type of an intrinsic may invalidate the IR.
17693fdaca5SPeter Collingbourne         F.getName().startswith("llvm."))
1771398a32eSPeter Collingbourne       continue;
1781398a32eSPeter Collingbourne 
1791398a32eSPeter Collingbourne     Function *NewF =
180f920da00SDylan McKay         Function::Create(EmptyFT, GlobalValue::ExternalLinkage,
181f920da00SDylan McKay                          F.getAddressSpace(), "", &M);
182e28435caSZequan Wu     NewF->copyAttributesFrom(&F);
183e28435caSZequan Wu     // Only copy function attribtues.
184*80ea2bb5SArthur Eubanks     NewF->setAttributes(AttributeList::get(M.getContext(),
185*80ea2bb5SArthur Eubanks                                            AttributeList::FunctionIndex,
186*80ea2bb5SArthur Eubanks                                            F.getAttributes().getFnAttrs()));
1871398a32eSPeter Collingbourne     NewF->takeName(&F);
1881398a32eSPeter Collingbourne     F.replaceAllUsesWith(ConstantExpr::getBitCast(NewF, F.getType()));
1891398a32eSPeter Collingbourne     F.eraseFromParent();
1901398a32eSPeter Collingbourne   }
1911398a32eSPeter Collingbourne 
1921398a32eSPeter Collingbourne   for (auto I = M.global_begin(), E = M.global_end(); I != E;) {
1931398a32eSPeter Collingbourne     GlobalVariable &GV = *I++;
1941398a32eSPeter Collingbourne     if (GV.isDeclaration() && GV.use_empty()) {
1951398a32eSPeter Collingbourne       GV.eraseFromParent();
1961398a32eSPeter Collingbourne       continue;
1971398a32eSPeter Collingbourne     }
1981398a32eSPeter Collingbourne   }
1991398a32eSPeter Collingbourne }
2001398a32eSPeter Collingbourne 
201d3704f67SGeorge Rimar static void
202d3704f67SGeorge Rimar filterModule(Module *M,
203d3704f67SGeorge Rimar              function_ref<bool(const GlobalValue *)> ShouldKeepDefinition) {
204d3704f67SGeorge Rimar   std::vector<GlobalValue *> V;
205d3704f67SGeorge Rimar   for (GlobalValue &GV : M->global_values())
206d3704f67SGeorge Rimar     if (!ShouldKeepDefinition(&GV))
207d3704f67SGeorge Rimar       V.push_back(&GV);
20854565249SGeorge Rimar 
209d3704f67SGeorge Rimar   for (GlobalValue *GV : V)
210d3704f67SGeorge Rimar     if (!convertToDeclaration(*GV))
211d3704f67SGeorge Rimar       GV->eraseFromParent();
2121398a32eSPeter Collingbourne }
2131398a32eSPeter Collingbourne 
214002c2d53SPeter Collingbourne void forEachVirtualFunction(Constant *C, function_ref<void(Function *)> Fn) {
215002c2d53SPeter Collingbourne   if (auto *F = dyn_cast<Function>(C))
216002c2d53SPeter Collingbourne     return Fn(F);
2173baa72afSPeter Collingbourne   if (isa<GlobalValue>(C))
2183baa72afSPeter Collingbourne     return;
219002c2d53SPeter Collingbourne   for (Value *Op : C->operands())
220002c2d53SPeter Collingbourne     forEachVirtualFunction(cast<Constant>(Op), Fn);
221002c2d53SPeter Collingbourne }
222002c2d53SPeter Collingbourne 
223fde55a9cSTeresa Johnson // Clone any @llvm[.compiler].used over to the new module and append
224fde55a9cSTeresa Johnson // values whose defs were cloned into that module.
225fde55a9cSTeresa Johnson static void cloneUsedGlobalVariables(const Module &SrcM, Module &DestM,
226fde55a9cSTeresa Johnson                                      bool CompilerUsed) {
2273adb89bbSFangrui Song   SmallVector<GlobalValue *, 4> Used, NewUsed;
228fde55a9cSTeresa Johnson   // First collect those in the llvm[.compiler].used set.
229fde55a9cSTeresa Johnson   collectUsedGlobalVariables(SrcM, Used, CompilerUsed);
230fde55a9cSTeresa Johnson   // Next build a set of the equivalent values defined in DestM.
231fde55a9cSTeresa Johnson   for (auto *V : Used) {
232fde55a9cSTeresa Johnson     auto *GV = DestM.getNamedValue(V->getName());
233fde55a9cSTeresa Johnson     if (GV && !GV->isDeclaration())
2343adb89bbSFangrui Song       NewUsed.push_back(GV);
235fde55a9cSTeresa Johnson   }
236fde55a9cSTeresa Johnson   // Finally, add them to a llvm[.compiler].used variable in DestM.
237fde55a9cSTeresa Johnson   if (CompilerUsed)
2383adb89bbSFangrui Song     appendToCompilerUsed(DestM, NewUsed);
239fde55a9cSTeresa Johnson   else
2403adb89bbSFangrui Song     appendToUsed(DestM, NewUsed);
241fde55a9cSTeresa Johnson }
242fde55a9cSTeresa Johnson 
2431398a32eSPeter Collingbourne // If it's possible to split M into regular and thin LTO parts, do so and write
2441398a32eSPeter Collingbourne // a multi-module bitcode file with the two parts to OS. Otherwise, write only a
2451398a32eSPeter Collingbourne // regular LTO bitcode file to OS.
246002c2d53SPeter Collingbourne void splitAndWriteThinLTOBitcode(
2470c6a4ff8STeresa Johnson     raw_ostream &OS, raw_ostream *ThinLinkOS,
2480c6a4ff8STeresa Johnson     function_ref<AAResults &(Function &)> AARGetter, Module &M) {
249964f4663SEvgeniy Stepanov   std::string ModuleId = getUniqueModuleId(&M);
2501398a32eSPeter Collingbourne   if (ModuleId.empty()) {
2516867ab7cSVlad Tsyrklevich     // We couldn't generate a module ID for this module, write it out as a
2526867ab7cSVlad Tsyrklevich     // regular LTO module with an index for summary-based dead stripping.
2536867ab7cSVlad Tsyrklevich     ProfileSummaryInfo PSI(M);
2546867ab7cSVlad Tsyrklevich     M.addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
2556867ab7cSVlad Tsyrklevich     ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI);
2566867ab7cSVlad Tsyrklevich     WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false, &Index);
2576867ab7cSVlad Tsyrklevich 
2580c6a4ff8STeresa Johnson     if (ThinLinkOS)
2590c6a4ff8STeresa Johnson       // We don't have a ThinLTO part, but still write the module to the
2600c6a4ff8STeresa Johnson       // ThinLinkOS if requested so that the expected output file is produced.
2616867ab7cSVlad Tsyrklevich       WriteBitcodeToFile(M, *ThinLinkOS, /*ShouldPreserveUseListOrder=*/false,
2626867ab7cSVlad Tsyrklevich                          &Index);
2636867ab7cSVlad Tsyrklevich 
2641398a32eSPeter Collingbourne     return;
2651398a32eSPeter Collingbourne   }
2661398a32eSPeter Collingbourne 
2671398a32eSPeter Collingbourne   promoteTypeIds(M, ModuleId);
2681398a32eSPeter Collingbourne 
269dd968219SPeter Collingbourne   // Returns whether a global or its associated global has attached type
270dd968219SPeter Collingbourne   // metadata. The former may participate in CFI or whole-program
271dd968219SPeter Collingbourne   // devirtualization, so they need to appear in the merged module instead of
272dd968219SPeter Collingbourne   // the thin LTO module. Similarly, globals that are associated with globals
273dd968219SPeter Collingbourne   // with type metadata need to appear in the merged module because they will
274dd968219SPeter Collingbourne   // reference the global's section directly.
2750deb9a9aSBenjamin Kramer   auto HasTypeMetadata = [](const GlobalObject *GO) {
276dd968219SPeter Collingbourne     if (MDNode *MD = GO->getMetadata(LLVMContext::MD_associated))
277dd968219SPeter Collingbourne       if (auto *AssocVM = dyn_cast_or_null<ValueAsMetadata>(MD->getOperand(0)))
278dd968219SPeter Collingbourne         if (auto *AssocGO = dyn_cast<GlobalObject>(AssocVM->getValue()))
279dd968219SPeter Collingbourne           if (AssocGO->hasMetadata(LLVMContext::MD_type))
280dd968219SPeter Collingbourne             return true;
2810deb9a9aSBenjamin Kramer     return GO->hasMetadata(LLVMContext::MD_type);
2821398a32eSPeter Collingbourne   };
2831398a32eSPeter Collingbourne 
284002c2d53SPeter Collingbourne   // Collect the set of virtual functions that are eligible for virtual constant
285002c2d53SPeter Collingbourne   // propagation. Each eligible function must not access memory, must return
286002c2d53SPeter Collingbourne   // an integer of width <=64 bits, must take at least one argument, must not
287002c2d53SPeter Collingbourne   // use its first argument (assumed to be "this") and all arguments other than
288002c2d53SPeter Collingbourne   // the first one must be of <=64 bit integer type.
289002c2d53SPeter Collingbourne   //
290002c2d53SPeter Collingbourne   // Note that we test whether this copy of the function is readnone, rather
291002c2d53SPeter Collingbourne   // than testing function attributes, which must hold for any copy of the
292002c2d53SPeter Collingbourne   // function, even a less optimized version substituted at link time. This is
293002c2d53SPeter Collingbourne   // sound because the virtual constant propagation optimizations effectively
294002c2d53SPeter Collingbourne   // inline all implementations of the virtual function into each call site,
295002c2d53SPeter Collingbourne   // rather than using function attributes to perform local optimization.
296aa09a82bSGeorge Burgess IV   DenseSet<const Function *> EligibleVirtualFns;
2974075ccc7SBob Haarman   // If any member of a comdat lives in MergedM, put all members of that
2984075ccc7SBob Haarman   // comdat in MergedM to keep the comdat together.
2994075ccc7SBob Haarman   DenseSet<const Comdat *> MergedMComdats;
300002c2d53SPeter Collingbourne   for (GlobalVariable &GV : M.globals())
3014075ccc7SBob Haarman     if (HasTypeMetadata(&GV)) {
3024075ccc7SBob Haarman       if (const auto *C = GV.getComdat())
3034075ccc7SBob Haarman         MergedMComdats.insert(C);
304002c2d53SPeter Collingbourne       forEachVirtualFunction(GV.getInitializer(), [&](Function *F) {
305002c2d53SPeter Collingbourne         auto *RT = dyn_cast<IntegerType>(F->getReturnType());
306002c2d53SPeter Collingbourne         if (!RT || RT->getBitWidth() > 64 || F->arg_empty() ||
307002c2d53SPeter Collingbourne             !F->arg_begin()->use_empty())
308002c2d53SPeter Collingbourne           return;
30923b0ab2aSKazu Hirata         for (auto &Arg : drop_begin(F->args())) {
310002c2d53SPeter Collingbourne           auto *ArgT = dyn_cast<IntegerType>(Arg.getType());
311002c2d53SPeter Collingbourne           if (!ArgT || ArgT->getBitWidth() > 64)
312002c2d53SPeter Collingbourne             return;
313002c2d53SPeter Collingbourne         }
31401f0c8a8SChandler Carruth         if (!F->isDeclaration() &&
31501f0c8a8SChandler Carruth             computeFunctionBodyMemoryAccess(*F, AARGetter(*F)) == MAK_ReadNone)
316002c2d53SPeter Collingbourne           EligibleVirtualFns.insert(F);
317002c2d53SPeter Collingbourne       });
3184075ccc7SBob Haarman     }
319002c2d53SPeter Collingbourne 
3201398a32eSPeter Collingbourne   ValueToValueMapTy VMap;
321002c2d53SPeter Collingbourne   std::unique_ptr<Module> MergedM(
32271867532SRafael Espindola       CloneModule(M, VMap, [&](const GlobalValue *GV) -> bool {
3234075ccc7SBob Haarman         if (const auto *C = GV->getComdat())
3244075ccc7SBob Haarman           if (MergedMComdats.count(C))
3254075ccc7SBob Haarman             return true;
326002c2d53SPeter Collingbourne         if (auto *F = dyn_cast<Function>(GV))
327002c2d53SPeter Collingbourne           return EligibleVirtualFns.count(F);
328002c2d53SPeter Collingbourne         if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject()))
329002c2d53SPeter Collingbourne           return HasTypeMetadata(GVar);
330002c2d53SPeter Collingbourne         return false;
331002c2d53SPeter Collingbourne       }));
33228ffd326SPeter Collingbourne   StripDebugInfo(*MergedM);
33329c6f483SPeter Collingbourne   MergedM->setModuleInlineAsm("");
3341398a32eSPeter Collingbourne 
335fde55a9cSTeresa Johnson   // Clone any llvm.*used globals to ensure the included values are
336fde55a9cSTeresa Johnson   // not deleted.
337fde55a9cSTeresa Johnson   cloneUsedGlobalVariables(M, *MergedM, /*CompilerUsed*/ false);
338fde55a9cSTeresa Johnson   cloneUsedGlobalVariables(M, *MergedM, /*CompilerUsed*/ true);
339fde55a9cSTeresa Johnson 
340002c2d53SPeter Collingbourne   for (Function &F : *MergedM)
341002c2d53SPeter Collingbourne     if (!F.isDeclaration()) {
342002c2d53SPeter Collingbourne       // Reset the linkage of all functions eligible for virtual constant
343002c2d53SPeter Collingbourne       // propagation. The canonical definitions live in the thin LTO module so
344002c2d53SPeter Collingbourne       // that they can be imported.
345002c2d53SPeter Collingbourne       F.setLinkage(GlobalValue::AvailableExternallyLinkage);
346002c2d53SPeter Collingbourne       F.setComdat(nullptr);
347002c2d53SPeter Collingbourne     }
348002c2d53SPeter Collingbourne 
3494d4ee93dSEvgeniy Stepanov   SetVector<GlobalValue *> CfiFunctions;
3504d4ee93dSEvgeniy Stepanov   for (auto &F : M)
3514d4ee93dSEvgeniy Stepanov     if ((!F.hasLocalLinkage() || F.hasAddressTaken()) && HasTypeMetadata(&F))
3524d4ee93dSEvgeniy Stepanov       CfiFunctions.insert(&F);
3534d4ee93dSEvgeniy Stepanov 
3544075ccc7SBob Haarman   // Remove all globals with type metadata, globals with comdats that live in
3554075ccc7SBob Haarman   // MergedM, and aliases pointing to such globals from the thin LTO module.
356002c2d53SPeter Collingbourne   filterModule(&M, [&](const GlobalValue *GV) {
357002c2d53SPeter Collingbourne     if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject()))
3584075ccc7SBob Haarman       if (HasTypeMetadata(GVar))
3594075ccc7SBob Haarman         return false;
3604075ccc7SBob Haarman     if (const auto *C = GV->getComdat())
3614075ccc7SBob Haarman       if (MergedMComdats.count(C))
3624075ccc7SBob Haarman         return false;
363002c2d53SPeter Collingbourne     return true;
364002c2d53SPeter Collingbourne   });
3651398a32eSPeter Collingbourne 
3664d4ee93dSEvgeniy Stepanov   promoteInternals(*MergedM, M, ModuleId, CfiFunctions);
3674d4ee93dSEvgeniy Stepanov   promoteInternals(M, *MergedM, ModuleId, CfiFunctions);
3684d4ee93dSEvgeniy Stepanov 
369230b2567SVlad Tsyrklevich   auto &Ctx = MergedM->getContext();
3704d4ee93dSEvgeniy Stepanov   SmallVector<MDNode *, 8> CfiFunctionMDs;
3714d4ee93dSEvgeniy Stepanov   for (auto V : CfiFunctions) {
3724d4ee93dSEvgeniy Stepanov     Function &F = *cast<Function>(V);
3734d4ee93dSEvgeniy Stepanov     SmallVector<MDNode *, 2> Types;
3744d4ee93dSEvgeniy Stepanov     F.getMetadata(LLVMContext::MD_type, Types);
3754d4ee93dSEvgeniy Stepanov 
3764d4ee93dSEvgeniy Stepanov     SmallVector<Metadata *, 4> Elts;
3774d4ee93dSEvgeniy Stepanov     Elts.push_back(MDString::get(Ctx, F.getName()));
3784d4ee93dSEvgeniy Stepanov     CfiFunctionLinkage Linkage;
3790e497d15SPeter Collingbourne     if (lowertypetests::isJumpTableCanonical(&F))
3804d4ee93dSEvgeniy Stepanov       Linkage = CFL_Definition;
3810e497d15SPeter Collingbourne     else if (F.hasExternalWeakLinkage())
3824d4ee93dSEvgeniy Stepanov       Linkage = CFL_WeakDeclaration;
3834d4ee93dSEvgeniy Stepanov     else
3844d4ee93dSEvgeniy Stepanov       Linkage = CFL_Declaration;
3854d4ee93dSEvgeniy Stepanov     Elts.push_back(ConstantAsMetadata::get(
3864d4ee93dSEvgeniy Stepanov         llvm::ConstantInt::get(Type::getInt8Ty(Ctx), Linkage)));
387e53472deSKazu Hirata     append_range(Elts, Types);
3884d4ee93dSEvgeniy Stepanov     CfiFunctionMDs.push_back(MDTuple::get(Ctx, Elts));
3894d4ee93dSEvgeniy Stepanov   }
3904d4ee93dSEvgeniy Stepanov 
3914d4ee93dSEvgeniy Stepanov   if(!CfiFunctionMDs.empty()) {
3924d4ee93dSEvgeniy Stepanov     NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("cfi.functions");
3934d4ee93dSEvgeniy Stepanov     for (auto MD : CfiFunctionMDs)
3944d4ee93dSEvgeniy Stepanov       NMD->addOperand(MD);
3954d4ee93dSEvgeniy Stepanov   }
3961398a32eSPeter Collingbourne 
397cdec22efSVlad Tsyrklevich   SmallVector<MDNode *, 8> FunctionAliases;
398cdec22efSVlad Tsyrklevich   for (auto &A : M.aliases()) {
399cdec22efSVlad Tsyrklevich     if (!isa<Function>(A.getAliasee()))
400cdec22efSVlad Tsyrklevich       continue;
401cdec22efSVlad Tsyrklevich 
402cdec22efSVlad Tsyrklevich     auto *F = cast<Function>(A.getAliasee());
403cdec22efSVlad Tsyrklevich 
4040deb9a9aSBenjamin Kramer     Metadata *Elts[] = {
4050deb9a9aSBenjamin Kramer         MDString::get(Ctx, A.getName()),
4060deb9a9aSBenjamin Kramer         MDString::get(Ctx, F->getName()),
4070deb9a9aSBenjamin Kramer         ConstantAsMetadata::get(
4080deb9a9aSBenjamin Kramer             ConstantInt::get(Type::getInt8Ty(Ctx), A.getVisibility())),
4090deb9a9aSBenjamin Kramer         ConstantAsMetadata::get(
4100deb9a9aSBenjamin Kramer             ConstantInt::get(Type::getInt8Ty(Ctx), A.isWeakForLinker())),
4110deb9a9aSBenjamin Kramer     };
412cdec22efSVlad Tsyrklevich 
413cdec22efSVlad Tsyrklevich     FunctionAliases.push_back(MDTuple::get(Ctx, Elts));
414cdec22efSVlad Tsyrklevich   }
415cdec22efSVlad Tsyrklevich 
416cdec22efSVlad Tsyrklevich   if (!FunctionAliases.empty()) {
417cdec22efSVlad Tsyrklevich     NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("aliases");
418cdec22efSVlad Tsyrklevich     for (auto MD : FunctionAliases)
419cdec22efSVlad Tsyrklevich       NMD->addOperand(MD);
420cdec22efSVlad Tsyrklevich   }
421cdec22efSVlad Tsyrklevich 
422230b2567SVlad Tsyrklevich   SmallVector<MDNode *, 8> Symvers;
423230b2567SVlad Tsyrklevich   ModuleSymbolTable::CollectAsmSymvers(M, [&](StringRef Name, StringRef Alias) {
424230b2567SVlad Tsyrklevich     Function *F = M.getFunction(Name);
425230b2567SVlad Tsyrklevich     if (!F || F->use_empty())
426230b2567SVlad Tsyrklevich       return;
427230b2567SVlad Tsyrklevich 
4280deb9a9aSBenjamin Kramer     Symvers.push_back(MDTuple::get(
4290deb9a9aSBenjamin Kramer         Ctx, {MDString::get(Ctx, Name), MDString::get(Ctx, Alias)}));
430230b2567SVlad Tsyrklevich   });
431230b2567SVlad Tsyrklevich 
432230b2567SVlad Tsyrklevich   if (!Symvers.empty()) {
433230b2567SVlad Tsyrklevich     NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("symvers");
434230b2567SVlad Tsyrklevich     for (auto MD : Symvers)
435230b2567SVlad Tsyrklevich       NMD->addOperand(MD);
436230b2567SVlad Tsyrklevich   }
437230b2567SVlad Tsyrklevich 
4381398a32eSPeter Collingbourne   simplifyExternals(*MergedM);
4391398a32eSPeter Collingbourne 
4401398a32eSPeter Collingbourne   // FIXME: Try to re-use BSI and PFI from the original module here.
44194624acaSTeresa Johnson   ProfileSummaryInfo PSI(M);
44294624acaSTeresa Johnson   ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI);
4430c6a4ff8STeresa Johnson 
444e357fbd2SPeter Collingbourne   // Mark the merged module as requiring full LTO. We still want an index for
445e357fbd2SPeter Collingbourne   // it though, so that it can participate in summary-based dead stripping.
446e357fbd2SPeter Collingbourne   MergedM->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
447e357fbd2SPeter Collingbourne   ModuleSummaryIndex MergedMIndex =
448e357fbd2SPeter Collingbourne       buildModuleSummaryIndex(*MergedM, nullptr, &PSI);
449e357fbd2SPeter Collingbourne 
4500c6a4ff8STeresa Johnson   SmallVector<char, 0> Buffer;
4510c6a4ff8STeresa Johnson 
4520c6a4ff8STeresa Johnson   BitcodeWriter W(Buffer);
4530c6a4ff8STeresa Johnson   // Save the module hash produced for the full bitcode, which will
4540c6a4ff8STeresa Johnson   // be used in the backends, and use that in the minimized bitcode
4550c6a4ff8STeresa Johnson   // produced for the full link.
4560c6a4ff8STeresa Johnson   ModuleHash ModHash = {{0}};
4576a86e25dSRafael Espindola   W.writeModule(M, /*ShouldPreserveUseListOrder=*/false, &Index,
4580c6a4ff8STeresa Johnson                 /*GenerateHash=*/true, &ModHash);
4596a86e25dSRafael Espindola   W.writeModule(*MergedM, /*ShouldPreserveUseListOrder=*/false, &MergedMIndex);
46092648c25SPeter Collingbourne   W.writeSymtab();
461a0f371a1SPeter Collingbourne   W.writeStrtab();
4621398a32eSPeter Collingbourne   OS << Buffer;
4630c6a4ff8STeresa Johnson 
4641dec57d5SHaojie Wang   // If a minimized bitcode module was requested for the thin link, only
4651dec57d5SHaojie Wang   // the information that is needed by thin link will be written in the
4661dec57d5SHaojie Wang   // given OS (the merged module will be written as usual).
4670c6a4ff8STeresa Johnson   if (ThinLinkOS) {
4680c6a4ff8STeresa Johnson     Buffer.clear();
4690c6a4ff8STeresa Johnson     BitcodeWriter W2(Buffer);
4700c6a4ff8STeresa Johnson     StripDebugInfo(M);
4716a86e25dSRafael Espindola     W2.writeThinLinkBitcode(M, Index, ModHash);
4726a86e25dSRafael Espindola     W2.writeModule(*MergedM, /*ShouldPreserveUseListOrder=*/false,
473e357fbd2SPeter Collingbourne                    &MergedMIndex);
47492648c25SPeter Collingbourne     W2.writeSymtab();
475a0f371a1SPeter Collingbourne     W2.writeStrtab();
4760c6a4ff8STeresa Johnson     *ThinLinkOS << Buffer;
4770c6a4ff8STeresa Johnson   }
4781398a32eSPeter Collingbourne }
4791398a32eSPeter Collingbourne 
480a7004363STeresa Johnson // Check if the LTO Unit splitting has been enabled.
481a7004363STeresa Johnson bool enableSplitLTOUnit(Module &M) {
482290a8398STeresa Johnson   bool EnableSplitLTOUnit = false;
483290a8398STeresa Johnson   if (auto *MD = mdconst::extract_or_null<ConstantInt>(
484290a8398STeresa Johnson           M.getModuleFlag("EnableSplitLTOUnit")))
485290a8398STeresa Johnson     EnableSplitLTOUnit = MD->getZExtValue();
486a7004363STeresa Johnson   return EnableSplitLTOUnit;
487a7004363STeresa Johnson }
488290a8398STeresa Johnson 
489a7004363STeresa Johnson // Returns whether this module needs to be split because it uses type metadata.
490a7004363STeresa Johnson bool hasTypeMetadata(Module &M) {
4911398a32eSPeter Collingbourne   for (auto &GO : M.global_objects()) {
4920deb9a9aSBenjamin Kramer     if (GO.hasMetadata(LLVMContext::MD_type))
4931398a32eSPeter Collingbourne       return true;
4941398a32eSPeter Collingbourne   }
4951398a32eSPeter Collingbourne   return false;
4961398a32eSPeter Collingbourne }
4971398a32eSPeter Collingbourne 
4980c6a4ff8STeresa Johnson void writeThinLTOBitcode(raw_ostream &OS, raw_ostream *ThinLinkOS,
499002c2d53SPeter Collingbourne                          function_ref<AAResults &(Function &)> AARGetter,
500002c2d53SPeter Collingbourne                          Module &M, const ModuleSummaryIndex *Index) {
501a7004363STeresa Johnson   std::unique_ptr<ModuleSummaryIndex> NewIndex = nullptr;
502a7004363STeresa Johnson   // See if this module has any type metadata. If so, we try to split it
503a7004363STeresa Johnson   // or at least promote type ids to enable WPD.
504a7004363STeresa Johnson   if (hasTypeMetadata(M)) {
505a7004363STeresa Johnson     if (enableSplitLTOUnit(M))
5060c6a4ff8STeresa Johnson       return splitAndWriteThinLTOBitcode(OS, ThinLinkOS, AARGetter, M);
507a7004363STeresa Johnson     // Promote type ids as needed for index-based WPD.
508a7004363STeresa Johnson     std::string ModuleId = getUniqueModuleId(&M);
509a7004363STeresa Johnson     if (!ModuleId.empty()) {
510a7004363STeresa Johnson       promoteTypeIds(M, ModuleId);
511a7004363STeresa Johnson       // Need to rebuild the index so that it contains type metadata
512a7004363STeresa Johnson       // for the newly promoted type ids.
513a7004363STeresa Johnson       // FIXME: Probably should not bother building the index at all
514a7004363STeresa Johnson       // in the caller of writeThinLTOBitcode (which does so via the
515a7004363STeresa Johnson       // ModuleSummaryIndexAnalysis pass), since we have to rebuild it
516a7004363STeresa Johnson       // anyway whenever there is type metadata (here or in
517a7004363STeresa Johnson       // splitAndWriteThinLTOBitcode). Just always build it once via the
518a7004363STeresa Johnson       // buildModuleSummaryIndex when Module(s) are ready.
519a7004363STeresa Johnson       ProfileSummaryInfo PSI(M);
5200eaee545SJonas Devlieghere       NewIndex = std::make_unique<ModuleSummaryIndex>(
521a7004363STeresa Johnson           buildModuleSummaryIndex(M, nullptr, &PSI));
522a7004363STeresa Johnson       Index = NewIndex.get();
523a7004363STeresa Johnson     }
524a7004363STeresa Johnson   }
5251398a32eSPeter Collingbourne 
526a7004363STeresa Johnson   // Write it out as an unsplit ThinLTO module.
5270c6a4ff8STeresa Johnson 
5280c6a4ff8STeresa Johnson   // Save the module hash produced for the full bitcode, which will
5290c6a4ff8STeresa Johnson   // be used in the backends, and use that in the minimized bitcode
5300c6a4ff8STeresa Johnson   // produced for the full link.
5310c6a4ff8STeresa Johnson   ModuleHash ModHash = {{0}};
5326a86e25dSRafael Espindola   WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false, Index,
5330c6a4ff8STeresa Johnson                      /*GenerateHash=*/true, &ModHash);
5341dec57d5SHaojie Wang   // If a minimized bitcode module was requested for the thin link, only
5351dec57d5SHaojie Wang   // the information that is needed by thin link will be written in the
5361dec57d5SHaojie Wang   // given OS.
5371dec57d5SHaojie Wang   if (ThinLinkOS && Index)
5386a86e25dSRafael Espindola     WriteThinLinkBitcodeToFile(M, *ThinLinkOS, *Index, ModHash);
5391398a32eSPeter Collingbourne }
5401398a32eSPeter Collingbourne 
5411398a32eSPeter Collingbourne class WriteThinLTOBitcode : public ModulePass {
5421398a32eSPeter Collingbourne   raw_ostream &OS; // raw_ostream to print on
5430c6a4ff8STeresa Johnson   // The output stream on which to emit a minimized module for use
5440c6a4ff8STeresa Johnson   // just in the thin link, if requested.
5450c6a4ff8STeresa Johnson   raw_ostream *ThinLinkOS;
5461398a32eSPeter Collingbourne 
5471398a32eSPeter Collingbourne public:
5481398a32eSPeter Collingbourne   static char ID; // Pass identification, replacement for typeid
5490c6a4ff8STeresa Johnson   WriteThinLTOBitcode() : ModulePass(ID), OS(dbgs()), ThinLinkOS(nullptr) {
5501398a32eSPeter Collingbourne     initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry());
5511398a32eSPeter Collingbourne   }
5521398a32eSPeter Collingbourne 
5530c6a4ff8STeresa Johnson   explicit WriteThinLTOBitcode(raw_ostream &o, raw_ostream *ThinLinkOS)
5540c6a4ff8STeresa Johnson       : ModulePass(ID), OS(o), ThinLinkOS(ThinLinkOS) {
5551398a32eSPeter Collingbourne     initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry());
5561398a32eSPeter Collingbourne   }
5571398a32eSPeter Collingbourne 
5581398a32eSPeter Collingbourne   StringRef getPassName() const override { return "ThinLTO Bitcode Writer"; }
5591398a32eSPeter Collingbourne 
5601398a32eSPeter Collingbourne   bool runOnModule(Module &M) override {
5611398a32eSPeter Collingbourne     const ModuleSummaryIndex *Index =
5621398a32eSPeter Collingbourne         &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex());
5630c6a4ff8STeresa Johnson     writeThinLTOBitcode(OS, ThinLinkOS, LegacyAARGetter(*this), M, Index);
5641398a32eSPeter Collingbourne     return true;
5651398a32eSPeter Collingbourne   }
5661398a32eSPeter Collingbourne   void getAnalysisUsage(AnalysisUsage &AU) const override {
5671398a32eSPeter Collingbourne     AU.setPreservesAll();
568002c2d53SPeter Collingbourne     AU.addRequired<AssumptionCacheTracker>();
5691398a32eSPeter Collingbourne     AU.addRequired<ModuleSummaryIndexWrapperPass>();
570002c2d53SPeter Collingbourne     AU.addRequired<TargetLibraryInfoWrapperPass>();
5711398a32eSPeter Collingbourne   }
5721398a32eSPeter Collingbourne };
5731398a32eSPeter Collingbourne } // anonymous namespace
5741398a32eSPeter Collingbourne 
5751398a32eSPeter Collingbourne char WriteThinLTOBitcode::ID = 0;
5761398a32eSPeter Collingbourne INITIALIZE_PASS_BEGIN(WriteThinLTOBitcode, "write-thinlto-bitcode",
5771398a32eSPeter Collingbourne                       "Write ThinLTO Bitcode", false, true)
578002c2d53SPeter Collingbourne INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
5791398a32eSPeter Collingbourne INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
580002c2d53SPeter Collingbourne INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
5811398a32eSPeter Collingbourne INITIALIZE_PASS_END(WriteThinLTOBitcode, "write-thinlto-bitcode",
5821398a32eSPeter Collingbourne                     "Write ThinLTO Bitcode", false, true)
5831398a32eSPeter Collingbourne 
5840c6a4ff8STeresa Johnson ModulePass *llvm::createWriteThinLTOBitcodePass(raw_ostream &Str,
5850c6a4ff8STeresa Johnson                                                 raw_ostream *ThinLinkOS) {
5860c6a4ff8STeresa Johnson   return new WriteThinLTOBitcode(Str, ThinLinkOS);
5871398a32eSPeter Collingbourne }
5886b411418STim Shen 
5896b411418STim Shen PreservedAnalyses
5906b411418STim Shen llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
5916b411418STim Shen   FunctionAnalysisManager &FAM =
5926b411418STim Shen       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
5936b411418STim Shen   writeThinLTOBitcode(OS, ThinLinkOS,
5946b411418STim Shen                       [&FAM](Function &F) -> AAResults & {
5956b411418STim Shen                         return FAM.getResult<AAManager>(F);
5966b411418STim Shen                       },
5976b411418STim Shen                       M, &AM.getResult<ModuleSummaryIndexAnalysis>(M));
5986b411418STim Shen   return PreservedAnalyses::all();
5996b411418STim Shen }
600