11398a32eSPeter Collingbourne //===- ThinLTOBitcodeWriter.cpp - Bitcode writing pass for ThinLTO --------===//
21398a32eSPeter Collingbourne //
31398a32eSPeter Collingbourne //                     The LLVM Compiler Infrastructure
41398a32eSPeter Collingbourne //
51398a32eSPeter Collingbourne // This file is distributed under the University of Illinois Open Source
61398a32eSPeter Collingbourne // License. See LICENSE.TXT for details.
71398a32eSPeter Collingbourne //
81398a32eSPeter Collingbourne //===----------------------------------------------------------------------===//
91398a32eSPeter Collingbourne 
106b411418STim Shen #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
11002c2d53SPeter Collingbourne #include "llvm/Analysis/BasicAliasAnalysis.h"
121398a32eSPeter Collingbourne #include "llvm/Analysis/ModuleSummaryAnalysis.h"
1394624acaSTeresa Johnson #include "llvm/Analysis/ProfileSummaryInfo.h"
141398a32eSPeter Collingbourne #include "llvm/Analysis/TypeMetadataUtils.h"
151398a32eSPeter Collingbourne #include "llvm/Bitcode/BitcodeWriter.h"
161398a32eSPeter Collingbourne #include "llvm/IR/Constants.h"
1728ffd326SPeter Collingbourne #include "llvm/IR/DebugInfo.h"
181398a32eSPeter Collingbourne #include "llvm/IR/Intrinsics.h"
191398a32eSPeter Collingbourne #include "llvm/IR/Module.h"
201398a32eSPeter Collingbourne #include "llvm/IR/PassManager.h"
211398a32eSPeter Collingbourne #include "llvm/Pass.h"
221398a32eSPeter Collingbourne #include "llvm/Support/ScopedPrinter.h"
230c6a4ff8STeresa Johnson #include "llvm/Support/raw_ostream.h"
240c6a4ff8STeresa Johnson #include "llvm/Transforms/IPO.h"
25002c2d53SPeter Collingbourne #include "llvm/Transforms/IPO/FunctionAttrs.h"
261398a32eSPeter Collingbourne #include "llvm/Transforms/Utils/Cloning.h"
27964f4663SEvgeniy Stepanov #include "llvm/Transforms/Utils/ModuleUtils.h"
281398a32eSPeter Collingbourne using namespace llvm;
291398a32eSPeter Collingbourne 
301398a32eSPeter Collingbourne namespace {
311398a32eSPeter Collingbourne 
321398a32eSPeter Collingbourne // Promote each local-linkage entity defined by ExportM and used by ImportM by
331398a32eSPeter Collingbourne // changing visibility and appending the given ModuleId.
344d4ee93dSEvgeniy Stepanov void promoteInternals(Module &ExportM, Module &ImportM, StringRef ModuleId,
354d4ee93dSEvgeniy Stepanov                       SetVector<GlobalValue *> &PromoteExtra) {
364075ccc7SBob Haarman   DenseMap<const Comdat *, Comdat *> RenamedComdats;
376b193966SPeter Collingbourne   for (auto &ExportGV : ExportM.global_values()) {
381398a32eSPeter Collingbourne     if (!ExportGV.hasLocalLinkage())
396b193966SPeter Collingbourne       continue;
401398a32eSPeter Collingbourne 
414075ccc7SBob Haarman     auto Name = ExportGV.getName();
421f034226SPeter Collingbourne     GlobalValue *ImportGV = nullptr;
431f034226SPeter Collingbourne     if (!PromoteExtra.count(&ExportGV)) {
441f034226SPeter Collingbourne       ImportGV = ImportM.getNamedValue(Name);
451f034226SPeter Collingbourne       if (!ImportGV)
466b193966SPeter Collingbourne         continue;
471f034226SPeter Collingbourne       ImportGV->removeDeadConstantUsers();
481f034226SPeter Collingbourne       if (ImportGV->use_empty()) {
491f034226SPeter Collingbourne         ImportGV->eraseFromParent();
501f034226SPeter Collingbourne         continue;
511f034226SPeter Collingbourne       }
521f034226SPeter Collingbourne     }
531398a32eSPeter Collingbourne 
544075ccc7SBob Haarman     std::string NewName = (Name + ModuleId).str();
554075ccc7SBob Haarman 
564075ccc7SBob Haarman     if (const auto *C = ExportGV.getComdat())
574075ccc7SBob Haarman       if (C->getName() == Name)
584075ccc7SBob Haarman         RenamedComdats.try_emplace(C, ExportM.getOrInsertComdat(NewName));
591398a32eSPeter Collingbourne 
601398a32eSPeter Collingbourne     ExportGV.setName(NewName);
611398a32eSPeter Collingbourne     ExportGV.setLinkage(GlobalValue::ExternalLinkage);
621398a32eSPeter Collingbourne     ExportGV.setVisibility(GlobalValue::HiddenVisibility);
631398a32eSPeter Collingbourne 
644d4ee93dSEvgeniy Stepanov     if (ImportGV) {
651398a32eSPeter Collingbourne       ImportGV->setName(NewName);
661398a32eSPeter Collingbourne       ImportGV->setVisibility(GlobalValue::HiddenVisibility);
676b193966SPeter Collingbourne     }
684d4ee93dSEvgeniy Stepanov   }
694075ccc7SBob Haarman 
704075ccc7SBob Haarman   if (!RenamedComdats.empty())
714075ccc7SBob Haarman     for (auto &GO : ExportM.global_objects())
724075ccc7SBob Haarman       if (auto *C = GO.getComdat()) {
734075ccc7SBob Haarman         auto Replacement = RenamedComdats.find(C);
744075ccc7SBob Haarman         if (Replacement != RenamedComdats.end())
754075ccc7SBob Haarman           GO.setComdat(Replacement->second);
764075ccc7SBob Haarman       }
771398a32eSPeter Collingbourne }
781398a32eSPeter Collingbourne 
791398a32eSPeter Collingbourne // Promote all internal (i.e. distinct) type ids used by the module by replacing
801398a32eSPeter Collingbourne // them with external type ids formed using the module id.
811398a32eSPeter Collingbourne //
821398a32eSPeter Collingbourne // Note that this needs to be done before we clone the module because each clone
831398a32eSPeter Collingbourne // will receive its own set of distinct metadata nodes.
841398a32eSPeter Collingbourne void promoteTypeIds(Module &M, StringRef ModuleId) {
851398a32eSPeter Collingbourne   DenseMap<Metadata *, Metadata *> LocalToGlobal;
861398a32eSPeter Collingbourne   auto ExternalizeTypeId = [&](CallInst *CI, unsigned ArgNo) {
871398a32eSPeter Collingbourne     Metadata *MD =
881398a32eSPeter Collingbourne         cast<MetadataAsValue>(CI->getArgOperand(ArgNo))->getMetadata();
891398a32eSPeter Collingbourne 
901398a32eSPeter Collingbourne     if (isa<MDNode>(MD) && cast<MDNode>(MD)->isDistinct()) {
911398a32eSPeter Collingbourne       Metadata *&GlobalMD = LocalToGlobal[MD];
921398a32eSPeter Collingbourne       if (!GlobalMD) {
933a13ed60SBenjamin Kramer         std::string NewName = (Twine(LocalToGlobal.size()) + ModuleId).str();
941398a32eSPeter Collingbourne         GlobalMD = MDString::get(M.getContext(), NewName);
951398a32eSPeter Collingbourne       }
961398a32eSPeter Collingbourne 
971398a32eSPeter Collingbourne       CI->setArgOperand(ArgNo,
981398a32eSPeter Collingbourne                         MetadataAsValue::get(M.getContext(), GlobalMD));
991398a32eSPeter Collingbourne     }
1001398a32eSPeter Collingbourne   };
1011398a32eSPeter Collingbourne 
1021398a32eSPeter Collingbourne   if (Function *TypeTestFunc =
1031398a32eSPeter Collingbourne           M.getFunction(Intrinsic::getName(Intrinsic::type_test))) {
1041398a32eSPeter Collingbourne     for (const Use &U : TypeTestFunc->uses()) {
1051398a32eSPeter Collingbourne       auto CI = cast<CallInst>(U.getUser());
1061398a32eSPeter Collingbourne       ExternalizeTypeId(CI, 1);
1071398a32eSPeter Collingbourne     }
1081398a32eSPeter Collingbourne   }
1091398a32eSPeter Collingbourne 
1101398a32eSPeter Collingbourne   if (Function *TypeCheckedLoadFunc =
1111398a32eSPeter Collingbourne           M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load))) {
1121398a32eSPeter Collingbourne     for (const Use &U : TypeCheckedLoadFunc->uses()) {
1131398a32eSPeter Collingbourne       auto CI = cast<CallInst>(U.getUser());
1141398a32eSPeter Collingbourne       ExternalizeTypeId(CI, 2);
1151398a32eSPeter Collingbourne     }
1161398a32eSPeter Collingbourne   }
1171398a32eSPeter Collingbourne 
1181398a32eSPeter Collingbourne   for (GlobalObject &GO : M.global_objects()) {
1191398a32eSPeter Collingbourne     SmallVector<MDNode *, 1> MDs;
1201398a32eSPeter Collingbourne     GO.getMetadata(LLVMContext::MD_type, MDs);
1211398a32eSPeter Collingbourne 
1221398a32eSPeter Collingbourne     GO.eraseMetadata(LLVMContext::MD_type);
1231398a32eSPeter Collingbourne     for (auto MD : MDs) {
1241398a32eSPeter Collingbourne       auto I = LocalToGlobal.find(MD->getOperand(1));
1251398a32eSPeter Collingbourne       if (I == LocalToGlobal.end()) {
1261398a32eSPeter Collingbourne         GO.addMetadata(LLVMContext::MD_type, *MD);
1271398a32eSPeter Collingbourne         continue;
1281398a32eSPeter Collingbourne       }
1291398a32eSPeter Collingbourne       GO.addMetadata(
1301398a32eSPeter Collingbourne           LLVMContext::MD_type,
1311398a32eSPeter Collingbourne           *MDNode::get(M.getContext(),
1321398a32eSPeter Collingbourne                        ArrayRef<Metadata *>{MD->getOperand(0), I->second}));
1331398a32eSPeter Collingbourne     }
1341398a32eSPeter Collingbourne   }
1351398a32eSPeter Collingbourne }
1361398a32eSPeter Collingbourne 
1371398a32eSPeter Collingbourne // Drop unused globals, and drop type information from function declarations.
1381398a32eSPeter Collingbourne // FIXME: If we made functions typeless then there would be no need to do this.
1391398a32eSPeter Collingbourne void simplifyExternals(Module &M) {
1401398a32eSPeter Collingbourne   FunctionType *EmptyFT =
1411398a32eSPeter Collingbourne       FunctionType::get(Type::getVoidTy(M.getContext()), false);
1421398a32eSPeter Collingbourne 
1431398a32eSPeter Collingbourne   for (auto I = M.begin(), E = M.end(); I != E;) {
1441398a32eSPeter Collingbourne     Function &F = *I++;
1451398a32eSPeter Collingbourne     if (F.isDeclaration() && F.use_empty()) {
1461398a32eSPeter Collingbourne       F.eraseFromParent();
1471398a32eSPeter Collingbourne       continue;
1481398a32eSPeter Collingbourne     }
1491398a32eSPeter Collingbourne 
15093fdaca5SPeter Collingbourne     if (!F.isDeclaration() || F.getFunctionType() == EmptyFT ||
15193fdaca5SPeter Collingbourne         // Changing the type of an intrinsic may invalidate the IR.
15293fdaca5SPeter Collingbourne         F.getName().startswith("llvm."))
1531398a32eSPeter Collingbourne       continue;
1541398a32eSPeter Collingbourne 
1551398a32eSPeter Collingbourne     Function *NewF =
1561398a32eSPeter Collingbourne         Function::Create(EmptyFT, GlobalValue::ExternalLinkage, "", &M);
1571398a32eSPeter Collingbourne     NewF->setVisibility(F.getVisibility());
1581398a32eSPeter Collingbourne     NewF->takeName(&F);
1591398a32eSPeter Collingbourne     F.replaceAllUsesWith(ConstantExpr::getBitCast(NewF, F.getType()));
1601398a32eSPeter Collingbourne     F.eraseFromParent();
1611398a32eSPeter Collingbourne   }
1621398a32eSPeter Collingbourne 
1631398a32eSPeter Collingbourne   for (auto I = M.global_begin(), E = M.global_end(); I != E;) {
1641398a32eSPeter Collingbourne     GlobalVariable &GV = *I++;
1651398a32eSPeter Collingbourne     if (GV.isDeclaration() && GV.use_empty()) {
1661398a32eSPeter Collingbourne       GV.eraseFromParent();
1671398a32eSPeter Collingbourne       continue;
1681398a32eSPeter Collingbourne     }
1691398a32eSPeter Collingbourne   }
1701398a32eSPeter Collingbourne }
1711398a32eSPeter Collingbourne 
1721398a32eSPeter Collingbourne void filterModule(
173061f4a5fSBenjamin Kramer     Module *M, function_ref<bool(const GlobalValue *)> ShouldKeepDefinition) {
1746de81347SBob Haarman   for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end();
1756de81347SBob Haarman        I != E;) {
1766de81347SBob Haarman     GlobalAlias *GA = &*I++;
1776de81347SBob Haarman     if (ShouldKeepDefinition(GA))
1786de81347SBob Haarman       continue;
1796de81347SBob Haarman 
1806de81347SBob Haarman     GlobalObject *GO;
1816de81347SBob Haarman     if (GA->getValueType()->isFunctionTy())
1826de81347SBob Haarman       GO = Function::Create(cast<FunctionType>(GA->getValueType()),
1836de81347SBob Haarman                             GlobalValue::ExternalLinkage, "", M);
1846de81347SBob Haarman     else
1856de81347SBob Haarman       GO = new GlobalVariable(
1866de81347SBob Haarman           *M, GA->getValueType(), false, GlobalValue::ExternalLinkage,
187f4dc59baSSerge Guelton           nullptr, "", nullptr,
1886de81347SBob Haarman           GA->getThreadLocalMode(), GA->getType()->getAddressSpace());
1896de81347SBob Haarman     GO->takeName(GA);
1906de81347SBob Haarman     GA->replaceAllUsesWith(GO);
1916de81347SBob Haarman     GA->eraseFromParent();
1926de81347SBob Haarman   }
1936de81347SBob Haarman 
1941398a32eSPeter Collingbourne   for (Function &F : *M) {
1951398a32eSPeter Collingbourne     if (ShouldKeepDefinition(&F))
1961398a32eSPeter Collingbourne       continue;
1971398a32eSPeter Collingbourne 
1981398a32eSPeter Collingbourne     F.deleteBody();
19920a00933SPeter Collingbourne     F.setComdat(nullptr);
2001398a32eSPeter Collingbourne     F.clearMetadata();
2011398a32eSPeter Collingbourne   }
2021398a32eSPeter Collingbourne 
2031398a32eSPeter Collingbourne   for (GlobalVariable &GV : M->globals()) {
2041398a32eSPeter Collingbourne     if (ShouldKeepDefinition(&GV))
2051398a32eSPeter Collingbourne       continue;
2061398a32eSPeter Collingbourne 
2071398a32eSPeter Collingbourne     GV.setInitializer(nullptr);
2081398a32eSPeter Collingbourne     GV.setLinkage(GlobalValue::ExternalLinkage);
20920a00933SPeter Collingbourne     GV.setComdat(nullptr);
2101398a32eSPeter Collingbourne     GV.clearMetadata();
2111398a32eSPeter Collingbourne   }
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 
2231398a32eSPeter Collingbourne // If it's possible to split M into regular and thin LTO parts, do so and write
2241398a32eSPeter Collingbourne // a multi-module bitcode file with the two parts to OS. Otherwise, write only a
2251398a32eSPeter Collingbourne // regular LTO bitcode file to OS.
226002c2d53SPeter Collingbourne void splitAndWriteThinLTOBitcode(
2270c6a4ff8STeresa Johnson     raw_ostream &OS, raw_ostream *ThinLinkOS,
2280c6a4ff8STeresa Johnson     function_ref<AAResults &(Function &)> AARGetter, Module &M) {
229964f4663SEvgeniy Stepanov   std::string ModuleId = getUniqueModuleId(&M);
2301398a32eSPeter Collingbourne   if (ModuleId.empty()) {
2311398a32eSPeter Collingbourne     // We couldn't generate a module ID for this module, just write it out as a
2321398a32eSPeter Collingbourne     // regular LTO module.
2331398a32eSPeter Collingbourne     WriteBitcodeToFile(&M, OS);
2340c6a4ff8STeresa Johnson     if (ThinLinkOS)
2350c6a4ff8STeresa Johnson       // We don't have a ThinLTO part, but still write the module to the
2360c6a4ff8STeresa Johnson       // ThinLinkOS if requested so that the expected output file is produced.
2370c6a4ff8STeresa Johnson       WriteBitcodeToFile(&M, *ThinLinkOS);
2381398a32eSPeter Collingbourne     return;
2391398a32eSPeter Collingbourne   }
2401398a32eSPeter Collingbourne 
2411398a32eSPeter Collingbourne   promoteTypeIds(M, ModuleId);
2421398a32eSPeter Collingbourne 
243002c2d53SPeter Collingbourne   // Returns whether a global has attached type metadata. Such globals may
244002c2d53SPeter Collingbourne   // participate in CFI or whole-program devirtualization, so they need to
245002c2d53SPeter Collingbourne   // appear in the merged module instead of the thin LTO module.
246002c2d53SPeter Collingbourne   auto HasTypeMetadata = [&](const GlobalObject *GO) {
2471398a32eSPeter Collingbourne     SmallVector<MDNode *, 1> MDs;
248002c2d53SPeter Collingbourne     GO->getMetadata(LLVMContext::MD_type, MDs);
2491398a32eSPeter Collingbourne     return !MDs.empty();
2501398a32eSPeter Collingbourne   };
2511398a32eSPeter Collingbourne 
252002c2d53SPeter Collingbourne   // Collect the set of virtual functions that are eligible for virtual constant
253002c2d53SPeter Collingbourne   // propagation. Each eligible function must not access memory, must return
254002c2d53SPeter Collingbourne   // an integer of width <=64 bits, must take at least one argument, must not
255002c2d53SPeter Collingbourne   // use its first argument (assumed to be "this") and all arguments other than
256002c2d53SPeter Collingbourne   // the first one must be of <=64 bit integer type.
257002c2d53SPeter Collingbourne   //
258002c2d53SPeter Collingbourne   // Note that we test whether this copy of the function is readnone, rather
259002c2d53SPeter Collingbourne   // than testing function attributes, which must hold for any copy of the
260002c2d53SPeter Collingbourne   // function, even a less optimized version substituted at link time. This is
261002c2d53SPeter Collingbourne   // sound because the virtual constant propagation optimizations effectively
262002c2d53SPeter Collingbourne   // inline all implementations of the virtual function into each call site,
263002c2d53SPeter Collingbourne   // rather than using function attributes to perform local optimization.
264002c2d53SPeter Collingbourne   std::set<const Function *> EligibleVirtualFns;
2654075ccc7SBob Haarman   // If any member of a comdat lives in MergedM, put all members of that
2664075ccc7SBob Haarman   // comdat in MergedM to keep the comdat together.
2674075ccc7SBob Haarman   DenseSet<const Comdat *> MergedMComdats;
268002c2d53SPeter Collingbourne   for (GlobalVariable &GV : M.globals())
2694075ccc7SBob Haarman     if (HasTypeMetadata(&GV)) {
2704075ccc7SBob Haarman       if (const auto *C = GV.getComdat())
2714075ccc7SBob Haarman         MergedMComdats.insert(C);
272002c2d53SPeter Collingbourne       forEachVirtualFunction(GV.getInitializer(), [&](Function *F) {
273002c2d53SPeter Collingbourne         auto *RT = dyn_cast<IntegerType>(F->getReturnType());
274002c2d53SPeter Collingbourne         if (!RT || RT->getBitWidth() > 64 || F->arg_empty() ||
275002c2d53SPeter Collingbourne             !F->arg_begin()->use_empty())
276002c2d53SPeter Collingbourne           return;
277002c2d53SPeter Collingbourne         for (auto &Arg : make_range(std::next(F->arg_begin()), F->arg_end())) {
278002c2d53SPeter Collingbourne           auto *ArgT = dyn_cast<IntegerType>(Arg.getType());
279002c2d53SPeter Collingbourne           if (!ArgT || ArgT->getBitWidth() > 64)
280002c2d53SPeter Collingbourne             return;
281002c2d53SPeter Collingbourne         }
28201f0c8a8SChandler Carruth         if (!F->isDeclaration() &&
28301f0c8a8SChandler Carruth             computeFunctionBodyMemoryAccess(*F, AARGetter(*F)) == MAK_ReadNone)
284002c2d53SPeter Collingbourne           EligibleVirtualFns.insert(F);
285002c2d53SPeter Collingbourne       });
2864075ccc7SBob Haarman     }
287002c2d53SPeter Collingbourne 
2881398a32eSPeter Collingbourne   ValueToValueMapTy VMap;
289002c2d53SPeter Collingbourne   std::unique_ptr<Module> MergedM(
290002c2d53SPeter Collingbourne       CloneModule(&M, VMap, [&](const GlobalValue *GV) -> bool {
2914075ccc7SBob Haarman         if (const auto *C = GV->getComdat())
2924075ccc7SBob Haarman           if (MergedMComdats.count(C))
2934075ccc7SBob Haarman             return true;
294002c2d53SPeter Collingbourne         if (auto *F = dyn_cast<Function>(GV))
295002c2d53SPeter Collingbourne           return EligibleVirtualFns.count(F);
296002c2d53SPeter Collingbourne         if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject()))
297002c2d53SPeter Collingbourne           return HasTypeMetadata(GVar);
298002c2d53SPeter Collingbourne         return false;
299002c2d53SPeter Collingbourne       }));
30028ffd326SPeter Collingbourne   StripDebugInfo(*MergedM);
3011398a32eSPeter Collingbourne 
302002c2d53SPeter Collingbourne   for (Function &F : *MergedM)
303002c2d53SPeter Collingbourne     if (!F.isDeclaration()) {
304002c2d53SPeter Collingbourne       // Reset the linkage of all functions eligible for virtual constant
305002c2d53SPeter Collingbourne       // propagation. The canonical definitions live in the thin LTO module so
306002c2d53SPeter Collingbourne       // that they can be imported.
307002c2d53SPeter Collingbourne       F.setLinkage(GlobalValue::AvailableExternallyLinkage);
308002c2d53SPeter Collingbourne       F.setComdat(nullptr);
309002c2d53SPeter Collingbourne     }
310002c2d53SPeter Collingbourne 
3114d4ee93dSEvgeniy Stepanov   SetVector<GlobalValue *> CfiFunctions;
3124d4ee93dSEvgeniy Stepanov   for (auto &F : M)
3134d4ee93dSEvgeniy Stepanov     if ((!F.hasLocalLinkage() || F.hasAddressTaken()) && HasTypeMetadata(&F))
3144d4ee93dSEvgeniy Stepanov       CfiFunctions.insert(&F);
3154d4ee93dSEvgeniy Stepanov 
3164075ccc7SBob Haarman   // Remove all globals with type metadata, globals with comdats that live in
3174075ccc7SBob Haarman   // MergedM, and aliases pointing to such globals from the thin LTO module.
318002c2d53SPeter Collingbourne   filterModule(&M, [&](const GlobalValue *GV) {
319002c2d53SPeter Collingbourne     if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject()))
3204075ccc7SBob Haarman       if (HasTypeMetadata(GVar))
3214075ccc7SBob Haarman         return false;
3224075ccc7SBob Haarman     if (const auto *C = GV->getComdat())
3234075ccc7SBob Haarman       if (MergedMComdats.count(C))
3244075ccc7SBob Haarman         return false;
325002c2d53SPeter Collingbourne     return true;
326002c2d53SPeter Collingbourne   });
3271398a32eSPeter Collingbourne 
3284d4ee93dSEvgeniy Stepanov   promoteInternals(*MergedM, M, ModuleId, CfiFunctions);
3294d4ee93dSEvgeniy Stepanov   promoteInternals(M, *MergedM, ModuleId, CfiFunctions);
3304d4ee93dSEvgeniy Stepanov 
3314d4ee93dSEvgeniy Stepanov   SmallVector<MDNode *, 8> CfiFunctionMDs;
3324d4ee93dSEvgeniy Stepanov   for (auto V : CfiFunctions) {
3334d4ee93dSEvgeniy Stepanov     Function &F = *cast<Function>(V);
3344d4ee93dSEvgeniy Stepanov     SmallVector<MDNode *, 2> Types;
3354d4ee93dSEvgeniy Stepanov     F.getMetadata(LLVMContext::MD_type, Types);
3364d4ee93dSEvgeniy Stepanov 
3374d4ee93dSEvgeniy Stepanov     auto &Ctx = MergedM->getContext();
3384d4ee93dSEvgeniy Stepanov     SmallVector<Metadata *, 4> Elts;
3394d4ee93dSEvgeniy Stepanov     Elts.push_back(MDString::get(Ctx, F.getName()));
3404d4ee93dSEvgeniy Stepanov     CfiFunctionLinkage Linkage;
3414d4ee93dSEvgeniy Stepanov     if (!F.isDeclarationForLinker())
3424d4ee93dSEvgeniy Stepanov       Linkage = CFL_Definition;
3434d4ee93dSEvgeniy Stepanov     else if (F.isWeakForLinker())
3444d4ee93dSEvgeniy Stepanov       Linkage = CFL_WeakDeclaration;
3454d4ee93dSEvgeniy Stepanov     else
3464d4ee93dSEvgeniy Stepanov       Linkage = CFL_Declaration;
3474d4ee93dSEvgeniy Stepanov     Elts.push_back(ConstantAsMetadata::get(
3484d4ee93dSEvgeniy Stepanov         llvm::ConstantInt::get(Type::getInt8Ty(Ctx), Linkage)));
3494d4ee93dSEvgeniy Stepanov     for (auto Type : Types)
3504d4ee93dSEvgeniy Stepanov       Elts.push_back(Type);
3514d4ee93dSEvgeniy Stepanov     CfiFunctionMDs.push_back(MDTuple::get(Ctx, Elts));
3524d4ee93dSEvgeniy Stepanov   }
3534d4ee93dSEvgeniy Stepanov 
3544d4ee93dSEvgeniy Stepanov   if(!CfiFunctionMDs.empty()) {
3554d4ee93dSEvgeniy Stepanov     NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("cfi.functions");
3564d4ee93dSEvgeniy Stepanov     for (auto MD : CfiFunctionMDs)
3574d4ee93dSEvgeniy Stepanov       NMD->addOperand(MD);
3584d4ee93dSEvgeniy Stepanov   }
3591398a32eSPeter Collingbourne 
360*cdec22efSVlad Tsyrklevich   SmallVector<MDNode *, 8> FunctionAliases;
361*cdec22efSVlad Tsyrklevich   for (auto &A : M.aliases()) {
362*cdec22efSVlad Tsyrklevich     if (!isa<Function>(A.getAliasee()))
363*cdec22efSVlad Tsyrklevich       continue;
364*cdec22efSVlad Tsyrklevich 
365*cdec22efSVlad Tsyrklevich     auto *F = cast<Function>(A.getAliasee());
366*cdec22efSVlad Tsyrklevich     auto &Ctx = MergedM->getContext();
367*cdec22efSVlad Tsyrklevich     SmallVector<Metadata *, 4> Elts;
368*cdec22efSVlad Tsyrklevich 
369*cdec22efSVlad Tsyrklevich     Elts.push_back(MDString::get(Ctx, A.getName()));
370*cdec22efSVlad Tsyrklevich     Elts.push_back(MDString::get(Ctx, F->getName()));
371*cdec22efSVlad Tsyrklevich     Elts.push_back(ConstantAsMetadata::get(
372*cdec22efSVlad Tsyrklevich         llvm::ConstantInt::get(Type::getInt8Ty(Ctx), A.getVisibility())));
373*cdec22efSVlad Tsyrklevich     Elts.push_back(ConstantAsMetadata::get(
374*cdec22efSVlad Tsyrklevich         llvm::ConstantInt::get(Type::getInt8Ty(Ctx), A.isWeakForLinker())));
375*cdec22efSVlad Tsyrklevich 
376*cdec22efSVlad Tsyrklevich     FunctionAliases.push_back(MDTuple::get(Ctx, Elts));
377*cdec22efSVlad Tsyrklevich   }
378*cdec22efSVlad Tsyrklevich 
379*cdec22efSVlad Tsyrklevich   if (!FunctionAliases.empty()) {
380*cdec22efSVlad Tsyrklevich     NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("aliases");
381*cdec22efSVlad Tsyrklevich     for (auto MD : FunctionAliases)
382*cdec22efSVlad Tsyrklevich       NMD->addOperand(MD);
383*cdec22efSVlad Tsyrklevich   }
384*cdec22efSVlad Tsyrklevich 
3851398a32eSPeter Collingbourne   simplifyExternals(*MergedM);
3861398a32eSPeter Collingbourne 
3871398a32eSPeter Collingbourne   // FIXME: Try to re-use BSI and PFI from the original module here.
38894624acaSTeresa Johnson   ProfileSummaryInfo PSI(M);
38994624acaSTeresa Johnson   ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI);
3900c6a4ff8STeresa Johnson 
391e357fbd2SPeter Collingbourne   // Mark the merged module as requiring full LTO. We still want an index for
392e357fbd2SPeter Collingbourne   // it though, so that it can participate in summary-based dead stripping.
393e357fbd2SPeter Collingbourne   MergedM->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
394e357fbd2SPeter Collingbourne   ModuleSummaryIndex MergedMIndex =
395e357fbd2SPeter Collingbourne       buildModuleSummaryIndex(*MergedM, nullptr, &PSI);
396e357fbd2SPeter Collingbourne 
3970c6a4ff8STeresa Johnson   SmallVector<char, 0> Buffer;
3980c6a4ff8STeresa Johnson 
3990c6a4ff8STeresa Johnson   BitcodeWriter W(Buffer);
4000c6a4ff8STeresa Johnson   // Save the module hash produced for the full bitcode, which will
4010c6a4ff8STeresa Johnson   // be used in the backends, and use that in the minimized bitcode
4020c6a4ff8STeresa Johnson   // produced for the full link.
4030c6a4ff8STeresa Johnson   ModuleHash ModHash = {{0}};
4041398a32eSPeter Collingbourne   W.writeModule(&M, /*ShouldPreserveUseListOrder=*/false, &Index,
4050c6a4ff8STeresa Johnson                 /*GenerateHash=*/true, &ModHash);
406e357fbd2SPeter Collingbourne   W.writeModule(MergedM.get(), /*ShouldPreserveUseListOrder=*/false,
407e357fbd2SPeter Collingbourne                 &MergedMIndex);
40892648c25SPeter Collingbourne   W.writeSymtab();
409a0f371a1SPeter Collingbourne   W.writeStrtab();
4101398a32eSPeter Collingbourne   OS << Buffer;
4110c6a4ff8STeresa Johnson 
4121dec57d5SHaojie Wang   // If a minimized bitcode module was requested for the thin link, only
4131dec57d5SHaojie Wang   // the information that is needed by thin link will be written in the
4141dec57d5SHaojie Wang   // given OS (the merged module will be written as usual).
4150c6a4ff8STeresa Johnson   if (ThinLinkOS) {
4160c6a4ff8STeresa Johnson     Buffer.clear();
4170c6a4ff8STeresa Johnson     BitcodeWriter W2(Buffer);
4180c6a4ff8STeresa Johnson     StripDebugInfo(M);
4191dec57d5SHaojie Wang     W2.writeThinLinkBitcode(&M, Index, ModHash);
420e357fbd2SPeter Collingbourne     W2.writeModule(MergedM.get(), /*ShouldPreserveUseListOrder=*/false,
421e357fbd2SPeter Collingbourne                    &MergedMIndex);
42292648c25SPeter Collingbourne     W2.writeSymtab();
423a0f371a1SPeter Collingbourne     W2.writeStrtab();
4240c6a4ff8STeresa Johnson     *ThinLinkOS << Buffer;
4250c6a4ff8STeresa Johnson   }
4261398a32eSPeter Collingbourne }
4271398a32eSPeter Collingbourne 
4281398a32eSPeter Collingbourne // Returns whether this module needs to be split because it uses type metadata.
4291398a32eSPeter Collingbourne bool requiresSplit(Module &M) {
4301398a32eSPeter Collingbourne   SmallVector<MDNode *, 1> MDs;
4311398a32eSPeter Collingbourne   for (auto &GO : M.global_objects()) {
4321398a32eSPeter Collingbourne     GO.getMetadata(LLVMContext::MD_type, MDs);
4331398a32eSPeter Collingbourne     if (!MDs.empty())
4341398a32eSPeter Collingbourne       return true;
4351398a32eSPeter Collingbourne   }
4361398a32eSPeter Collingbourne 
4371398a32eSPeter Collingbourne   return false;
4381398a32eSPeter Collingbourne }
4391398a32eSPeter Collingbourne 
4400c6a4ff8STeresa Johnson void writeThinLTOBitcode(raw_ostream &OS, raw_ostream *ThinLinkOS,
441002c2d53SPeter Collingbourne                          function_ref<AAResults &(Function &)> AARGetter,
442002c2d53SPeter Collingbourne                          Module &M, const ModuleSummaryIndex *Index) {
4431398a32eSPeter Collingbourne   // See if this module has any type metadata. If so, we need to split it.
4441398a32eSPeter Collingbourne   if (requiresSplit(M))
4450c6a4ff8STeresa Johnson     return splitAndWriteThinLTOBitcode(OS, ThinLinkOS, AARGetter, M);
4461398a32eSPeter Collingbourne 
4471398a32eSPeter Collingbourne   // Otherwise we can just write it out as a regular module.
4480c6a4ff8STeresa Johnson 
4490c6a4ff8STeresa Johnson   // Save the module hash produced for the full bitcode, which will
4500c6a4ff8STeresa Johnson   // be used in the backends, and use that in the minimized bitcode
4510c6a4ff8STeresa Johnson   // produced for the full link.
4520c6a4ff8STeresa Johnson   ModuleHash ModHash = {{0}};
4531398a32eSPeter Collingbourne   WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false, Index,
4540c6a4ff8STeresa Johnson                      /*GenerateHash=*/true, &ModHash);
4551dec57d5SHaojie Wang   // If a minimized bitcode module was requested for the thin link, only
4561dec57d5SHaojie Wang   // the information that is needed by thin link will be written in the
4571dec57d5SHaojie Wang   // given OS.
4581dec57d5SHaojie Wang   if (ThinLinkOS && Index)
4591dec57d5SHaojie Wang     WriteThinLinkBitcodeToFile(&M, *ThinLinkOS, *Index, ModHash);
4601398a32eSPeter Collingbourne }
4611398a32eSPeter Collingbourne 
4621398a32eSPeter Collingbourne class WriteThinLTOBitcode : public ModulePass {
4631398a32eSPeter Collingbourne   raw_ostream &OS; // raw_ostream to print on
4640c6a4ff8STeresa Johnson   // The output stream on which to emit a minimized module for use
4650c6a4ff8STeresa Johnson   // just in the thin link, if requested.
4660c6a4ff8STeresa Johnson   raw_ostream *ThinLinkOS;
4671398a32eSPeter Collingbourne 
4681398a32eSPeter Collingbourne public:
4691398a32eSPeter Collingbourne   static char ID; // Pass identification, replacement for typeid
4700c6a4ff8STeresa Johnson   WriteThinLTOBitcode() : ModulePass(ID), OS(dbgs()), ThinLinkOS(nullptr) {
4711398a32eSPeter Collingbourne     initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry());
4721398a32eSPeter Collingbourne   }
4731398a32eSPeter Collingbourne 
4740c6a4ff8STeresa Johnson   explicit WriteThinLTOBitcode(raw_ostream &o, raw_ostream *ThinLinkOS)
4750c6a4ff8STeresa Johnson       : ModulePass(ID), OS(o), ThinLinkOS(ThinLinkOS) {
4761398a32eSPeter Collingbourne     initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry());
4771398a32eSPeter Collingbourne   }
4781398a32eSPeter Collingbourne 
4791398a32eSPeter Collingbourne   StringRef getPassName() const override { return "ThinLTO Bitcode Writer"; }
4801398a32eSPeter Collingbourne 
4811398a32eSPeter Collingbourne   bool runOnModule(Module &M) override {
4821398a32eSPeter Collingbourne     const ModuleSummaryIndex *Index =
4831398a32eSPeter Collingbourne         &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex());
4840c6a4ff8STeresa Johnson     writeThinLTOBitcode(OS, ThinLinkOS, LegacyAARGetter(*this), M, Index);
4851398a32eSPeter Collingbourne     return true;
4861398a32eSPeter Collingbourne   }
4871398a32eSPeter Collingbourne   void getAnalysisUsage(AnalysisUsage &AU) const override {
4881398a32eSPeter Collingbourne     AU.setPreservesAll();
489002c2d53SPeter Collingbourne     AU.addRequired<AssumptionCacheTracker>();
4901398a32eSPeter Collingbourne     AU.addRequired<ModuleSummaryIndexWrapperPass>();
491002c2d53SPeter Collingbourne     AU.addRequired<TargetLibraryInfoWrapperPass>();
4921398a32eSPeter Collingbourne   }
4931398a32eSPeter Collingbourne };
4941398a32eSPeter Collingbourne } // anonymous namespace
4951398a32eSPeter Collingbourne 
4961398a32eSPeter Collingbourne char WriteThinLTOBitcode::ID = 0;
4971398a32eSPeter Collingbourne INITIALIZE_PASS_BEGIN(WriteThinLTOBitcode, "write-thinlto-bitcode",
4981398a32eSPeter Collingbourne                       "Write ThinLTO Bitcode", false, true)
499002c2d53SPeter Collingbourne INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
5001398a32eSPeter Collingbourne INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
501002c2d53SPeter Collingbourne INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
5021398a32eSPeter Collingbourne INITIALIZE_PASS_END(WriteThinLTOBitcode, "write-thinlto-bitcode",
5031398a32eSPeter Collingbourne                     "Write ThinLTO Bitcode", false, true)
5041398a32eSPeter Collingbourne 
5050c6a4ff8STeresa Johnson ModulePass *llvm::createWriteThinLTOBitcodePass(raw_ostream &Str,
5060c6a4ff8STeresa Johnson                                                 raw_ostream *ThinLinkOS) {
5070c6a4ff8STeresa Johnson   return new WriteThinLTOBitcode(Str, ThinLinkOS);
5081398a32eSPeter Collingbourne }
5096b411418STim Shen 
5106b411418STim Shen PreservedAnalyses
5116b411418STim Shen llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
5126b411418STim Shen   FunctionAnalysisManager &FAM =
5136b411418STim Shen       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
5146b411418STim Shen   writeThinLTOBitcode(OS, ThinLinkOS,
5156b411418STim Shen                       [&FAM](Function &F) -> AAResults & {
5166b411418STim Shen                         return FAM.getResult<AAManager>(F);
5176b411418STim Shen                       },
5186b411418STim Shen                       M, &AM.getResult<ModuleSummaryIndexAnalysis>(M));
5196b411418STim Shen   return PreservedAnalyses::all();
5206b411418STim Shen }
521