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 
172*54565249SGeorge Rimar void filterModule(
173*54565249SGeorge Rimar     Module *M, function_ref<bool(const GlobalValue *)> ShouldKeepDefinition) {
174*54565249SGeorge Rimar   for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end();
175*54565249SGeorge Rimar        I != E;) {
176*54565249SGeorge Rimar     GlobalAlias *GA = &*I++;
177*54565249SGeorge Rimar     if (ShouldKeepDefinition(GA))
178*54565249SGeorge Rimar       continue;
179*54565249SGeorge Rimar 
180*54565249SGeorge Rimar     GlobalObject *GO;
181*54565249SGeorge Rimar     if (GA->getValueType()->isFunctionTy())
182*54565249SGeorge Rimar       GO = Function::Create(cast<FunctionType>(GA->getValueType()),
183*54565249SGeorge Rimar                             GlobalValue::ExternalLinkage, "", M);
184*54565249SGeorge Rimar     else
185*54565249SGeorge Rimar       GO = new GlobalVariable(
186*54565249SGeorge Rimar           *M, GA->getValueType(), false, GlobalValue::ExternalLinkage,
187*54565249SGeorge Rimar           nullptr, "", nullptr,
188*54565249SGeorge Rimar           GA->getThreadLocalMode(), GA->getType()->getAddressSpace());
189*54565249SGeorge Rimar     GO->takeName(GA);
190*54565249SGeorge Rimar     GA->replaceAllUsesWith(GO);
191*54565249SGeorge Rimar     GA->eraseFromParent();
192*54565249SGeorge Rimar   }
193*54565249SGeorge Rimar 
194*54565249SGeorge Rimar   for (Function &F : *M) {
195*54565249SGeorge Rimar     if (ShouldKeepDefinition(&F))
196*54565249SGeorge Rimar       continue;
197*54565249SGeorge Rimar 
198*54565249SGeorge Rimar     F.deleteBody();
199*54565249SGeorge Rimar     F.setComdat(nullptr);
200*54565249SGeorge Rimar     F.clearMetadata();
201*54565249SGeorge Rimar   }
202*54565249SGeorge Rimar 
203*54565249SGeorge Rimar   for (GlobalVariable &GV : M->globals()) {
2041398a32eSPeter Collingbourne     if (ShouldKeepDefinition(&GV))
2051398a32eSPeter Collingbourne       continue;
206*54565249SGeorge Rimar 
207*54565249SGeorge Rimar     GV.setInitializer(nullptr);
208*54565249SGeorge Rimar     GV.setLinkage(GlobalValue::ExternalLinkage);
209*54565249SGeorge Rimar     GV.setComdat(nullptr);
210*54565249SGeorge Rimar     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);
30129c6f483SPeter Collingbourne   MergedM->setModuleInlineAsm("");
3021398a32eSPeter Collingbourne 
303002c2d53SPeter Collingbourne   for (Function &F : *MergedM)
304002c2d53SPeter Collingbourne     if (!F.isDeclaration()) {
305002c2d53SPeter Collingbourne       // Reset the linkage of all functions eligible for virtual constant
306002c2d53SPeter Collingbourne       // propagation. The canonical definitions live in the thin LTO module so
307002c2d53SPeter Collingbourne       // that they can be imported.
308002c2d53SPeter Collingbourne       F.setLinkage(GlobalValue::AvailableExternallyLinkage);
309002c2d53SPeter Collingbourne       F.setComdat(nullptr);
310002c2d53SPeter Collingbourne     }
311002c2d53SPeter Collingbourne 
3124d4ee93dSEvgeniy Stepanov   SetVector<GlobalValue *> CfiFunctions;
3134d4ee93dSEvgeniy Stepanov   for (auto &F : M)
3144d4ee93dSEvgeniy Stepanov     if ((!F.hasLocalLinkage() || F.hasAddressTaken()) && HasTypeMetadata(&F))
3154d4ee93dSEvgeniy Stepanov       CfiFunctions.insert(&F);
3164d4ee93dSEvgeniy Stepanov 
3174075ccc7SBob Haarman   // Remove all globals with type metadata, globals with comdats that live in
3184075ccc7SBob Haarman   // MergedM, and aliases pointing to such globals from the thin LTO module.
319002c2d53SPeter Collingbourne   filterModule(&M, [&](const GlobalValue *GV) {
320002c2d53SPeter Collingbourne     if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject()))
3214075ccc7SBob Haarman       if (HasTypeMetadata(GVar))
3224075ccc7SBob Haarman         return false;
3234075ccc7SBob Haarman     if (const auto *C = GV->getComdat())
3244075ccc7SBob Haarman       if (MergedMComdats.count(C))
3254075ccc7SBob Haarman         return false;
326002c2d53SPeter Collingbourne     return true;
327002c2d53SPeter Collingbourne   });
3281398a32eSPeter Collingbourne 
3294d4ee93dSEvgeniy Stepanov   promoteInternals(*MergedM, M, ModuleId, CfiFunctions);
3304d4ee93dSEvgeniy Stepanov   promoteInternals(M, *MergedM, ModuleId, CfiFunctions);
3314d4ee93dSEvgeniy Stepanov 
3324d4ee93dSEvgeniy Stepanov   SmallVector<MDNode *, 8> CfiFunctionMDs;
3334d4ee93dSEvgeniy Stepanov   for (auto V : CfiFunctions) {
3344d4ee93dSEvgeniy Stepanov     Function &F = *cast<Function>(V);
3354d4ee93dSEvgeniy Stepanov     SmallVector<MDNode *, 2> Types;
3364d4ee93dSEvgeniy Stepanov     F.getMetadata(LLVMContext::MD_type, Types);
3374d4ee93dSEvgeniy Stepanov 
3384d4ee93dSEvgeniy Stepanov     auto &Ctx = MergedM->getContext();
3394d4ee93dSEvgeniy Stepanov     SmallVector<Metadata *, 4> Elts;
3404d4ee93dSEvgeniy Stepanov     Elts.push_back(MDString::get(Ctx, F.getName()));
3414d4ee93dSEvgeniy Stepanov     CfiFunctionLinkage Linkage;
3424d4ee93dSEvgeniy Stepanov     if (!F.isDeclarationForLinker())
3434d4ee93dSEvgeniy Stepanov       Linkage = CFL_Definition;
3444d4ee93dSEvgeniy Stepanov     else if (F.isWeakForLinker())
3454d4ee93dSEvgeniy Stepanov       Linkage = CFL_WeakDeclaration;
3464d4ee93dSEvgeniy Stepanov     else
3474d4ee93dSEvgeniy Stepanov       Linkage = CFL_Declaration;
3484d4ee93dSEvgeniy Stepanov     Elts.push_back(ConstantAsMetadata::get(
3494d4ee93dSEvgeniy Stepanov         llvm::ConstantInt::get(Type::getInt8Ty(Ctx), Linkage)));
3504d4ee93dSEvgeniy Stepanov     for (auto Type : Types)
3514d4ee93dSEvgeniy Stepanov       Elts.push_back(Type);
3524d4ee93dSEvgeniy Stepanov     CfiFunctionMDs.push_back(MDTuple::get(Ctx, Elts));
3534d4ee93dSEvgeniy Stepanov   }
3544d4ee93dSEvgeniy Stepanov 
3554d4ee93dSEvgeniy Stepanov   if(!CfiFunctionMDs.empty()) {
3564d4ee93dSEvgeniy Stepanov     NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("cfi.functions");
3574d4ee93dSEvgeniy Stepanov     for (auto MD : CfiFunctionMDs)
3584d4ee93dSEvgeniy Stepanov       NMD->addOperand(MD);
3594d4ee93dSEvgeniy Stepanov   }
3601398a32eSPeter Collingbourne 
361cdec22efSVlad Tsyrklevich   SmallVector<MDNode *, 8> FunctionAliases;
362cdec22efSVlad Tsyrklevich   for (auto &A : M.aliases()) {
363cdec22efSVlad Tsyrklevich     if (!isa<Function>(A.getAliasee()))
364cdec22efSVlad Tsyrklevich       continue;
365cdec22efSVlad Tsyrklevich 
366cdec22efSVlad Tsyrklevich     auto *F = cast<Function>(A.getAliasee());
367cdec22efSVlad Tsyrklevich     auto &Ctx = MergedM->getContext();
368cdec22efSVlad Tsyrklevich     SmallVector<Metadata *, 4> Elts;
369cdec22efSVlad Tsyrklevich 
370cdec22efSVlad Tsyrklevich     Elts.push_back(MDString::get(Ctx, A.getName()));
371cdec22efSVlad Tsyrklevich     Elts.push_back(MDString::get(Ctx, F->getName()));
372cdec22efSVlad Tsyrklevich     Elts.push_back(ConstantAsMetadata::get(
373cdec22efSVlad Tsyrklevich         llvm::ConstantInt::get(Type::getInt8Ty(Ctx), A.getVisibility())));
374cdec22efSVlad Tsyrklevich     Elts.push_back(ConstantAsMetadata::get(
375cdec22efSVlad Tsyrklevich         llvm::ConstantInt::get(Type::getInt8Ty(Ctx), A.isWeakForLinker())));
376cdec22efSVlad Tsyrklevich 
377cdec22efSVlad Tsyrklevich     FunctionAliases.push_back(MDTuple::get(Ctx, Elts));
378cdec22efSVlad Tsyrklevich   }
379cdec22efSVlad Tsyrklevich 
380cdec22efSVlad Tsyrklevich   if (!FunctionAliases.empty()) {
381cdec22efSVlad Tsyrklevich     NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("aliases");
382cdec22efSVlad Tsyrklevich     for (auto MD : FunctionAliases)
383cdec22efSVlad Tsyrklevich       NMD->addOperand(MD);
384cdec22efSVlad Tsyrklevich   }
385cdec22efSVlad Tsyrklevich 
3861398a32eSPeter Collingbourne   simplifyExternals(*MergedM);
3871398a32eSPeter Collingbourne 
3881398a32eSPeter Collingbourne   // FIXME: Try to re-use BSI and PFI from the original module here.
38994624acaSTeresa Johnson   ProfileSummaryInfo PSI(M);
39094624acaSTeresa Johnson   ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI);
3910c6a4ff8STeresa Johnson 
392e357fbd2SPeter Collingbourne   // Mark the merged module as requiring full LTO. We still want an index for
393e357fbd2SPeter Collingbourne   // it though, so that it can participate in summary-based dead stripping.
394e357fbd2SPeter Collingbourne   MergedM->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
395e357fbd2SPeter Collingbourne   ModuleSummaryIndex MergedMIndex =
396e357fbd2SPeter Collingbourne       buildModuleSummaryIndex(*MergedM, nullptr, &PSI);
397e357fbd2SPeter Collingbourne 
3980c6a4ff8STeresa Johnson   SmallVector<char, 0> Buffer;
3990c6a4ff8STeresa Johnson 
4000c6a4ff8STeresa Johnson   BitcodeWriter W(Buffer);
4010c6a4ff8STeresa Johnson   // Save the module hash produced for the full bitcode, which will
4020c6a4ff8STeresa Johnson   // be used in the backends, and use that in the minimized bitcode
4030c6a4ff8STeresa Johnson   // produced for the full link.
4040c6a4ff8STeresa Johnson   ModuleHash ModHash = {{0}};
4051398a32eSPeter Collingbourne   W.writeModule(&M, /*ShouldPreserveUseListOrder=*/false, &Index,
4060c6a4ff8STeresa Johnson                 /*GenerateHash=*/true, &ModHash);
407e357fbd2SPeter Collingbourne   W.writeModule(MergedM.get(), /*ShouldPreserveUseListOrder=*/false,
408e357fbd2SPeter Collingbourne                 &MergedMIndex);
40992648c25SPeter Collingbourne   W.writeSymtab();
410a0f371a1SPeter Collingbourne   W.writeStrtab();
4111398a32eSPeter Collingbourne   OS << Buffer;
4120c6a4ff8STeresa Johnson 
4131dec57d5SHaojie Wang   // If a minimized bitcode module was requested for the thin link, only
4141dec57d5SHaojie Wang   // the information that is needed by thin link will be written in the
4151dec57d5SHaojie Wang   // given OS (the merged module will be written as usual).
4160c6a4ff8STeresa Johnson   if (ThinLinkOS) {
4170c6a4ff8STeresa Johnson     Buffer.clear();
4180c6a4ff8STeresa Johnson     BitcodeWriter W2(Buffer);
4190c6a4ff8STeresa Johnson     StripDebugInfo(M);
4201dec57d5SHaojie Wang     W2.writeThinLinkBitcode(&M, Index, ModHash);
421e357fbd2SPeter Collingbourne     W2.writeModule(MergedM.get(), /*ShouldPreserveUseListOrder=*/false,
422e357fbd2SPeter Collingbourne                    &MergedMIndex);
42392648c25SPeter Collingbourne     W2.writeSymtab();
424a0f371a1SPeter Collingbourne     W2.writeStrtab();
4250c6a4ff8STeresa Johnson     *ThinLinkOS << Buffer;
4260c6a4ff8STeresa Johnson   }
4271398a32eSPeter Collingbourne }
4281398a32eSPeter Collingbourne 
4291398a32eSPeter Collingbourne // Returns whether this module needs to be split because it uses type metadata.
4301398a32eSPeter Collingbourne bool requiresSplit(Module &M) {
4311398a32eSPeter Collingbourne   SmallVector<MDNode *, 1> MDs;
4321398a32eSPeter Collingbourne   for (auto &GO : M.global_objects()) {
4331398a32eSPeter Collingbourne     GO.getMetadata(LLVMContext::MD_type, MDs);
4341398a32eSPeter Collingbourne     if (!MDs.empty())
4351398a32eSPeter Collingbourne       return true;
4361398a32eSPeter Collingbourne   }
4371398a32eSPeter Collingbourne 
4381398a32eSPeter Collingbourne   return false;
4391398a32eSPeter Collingbourne }
4401398a32eSPeter Collingbourne 
4410c6a4ff8STeresa Johnson void writeThinLTOBitcode(raw_ostream &OS, raw_ostream *ThinLinkOS,
442002c2d53SPeter Collingbourne                          function_ref<AAResults &(Function &)> AARGetter,
443002c2d53SPeter Collingbourne                          Module &M, const ModuleSummaryIndex *Index) {
4441398a32eSPeter Collingbourne   // See if this module has any type metadata. If so, we need to split it.
4451398a32eSPeter Collingbourne   if (requiresSplit(M))
4460c6a4ff8STeresa Johnson     return splitAndWriteThinLTOBitcode(OS, ThinLinkOS, AARGetter, M);
4471398a32eSPeter Collingbourne 
4481398a32eSPeter Collingbourne   // Otherwise we can just write it out as a regular module.
4490c6a4ff8STeresa Johnson 
4500c6a4ff8STeresa Johnson   // Save the module hash produced for the full bitcode, which will
4510c6a4ff8STeresa Johnson   // be used in the backends, and use that in the minimized bitcode
4520c6a4ff8STeresa Johnson   // produced for the full link.
4530c6a4ff8STeresa Johnson   ModuleHash ModHash = {{0}};
4541398a32eSPeter Collingbourne   WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false, Index,
4550c6a4ff8STeresa Johnson                      /*GenerateHash=*/true, &ModHash);
4561dec57d5SHaojie Wang   // If a minimized bitcode module was requested for the thin link, only
4571dec57d5SHaojie Wang   // the information that is needed by thin link will be written in the
4581dec57d5SHaojie Wang   // given OS.
4591dec57d5SHaojie Wang   if (ThinLinkOS && Index)
4601dec57d5SHaojie Wang     WriteThinLinkBitcodeToFile(&M, *ThinLinkOS, *Index, ModHash);
4611398a32eSPeter Collingbourne }
4621398a32eSPeter Collingbourne 
4631398a32eSPeter Collingbourne class WriteThinLTOBitcode : public ModulePass {
4641398a32eSPeter Collingbourne   raw_ostream &OS; // raw_ostream to print on
4650c6a4ff8STeresa Johnson   // The output stream on which to emit a minimized module for use
4660c6a4ff8STeresa Johnson   // just in the thin link, if requested.
4670c6a4ff8STeresa Johnson   raw_ostream *ThinLinkOS;
4681398a32eSPeter Collingbourne 
4691398a32eSPeter Collingbourne public:
4701398a32eSPeter Collingbourne   static char ID; // Pass identification, replacement for typeid
4710c6a4ff8STeresa Johnson   WriteThinLTOBitcode() : ModulePass(ID), OS(dbgs()), ThinLinkOS(nullptr) {
4721398a32eSPeter Collingbourne     initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry());
4731398a32eSPeter Collingbourne   }
4741398a32eSPeter Collingbourne 
4750c6a4ff8STeresa Johnson   explicit WriteThinLTOBitcode(raw_ostream &o, raw_ostream *ThinLinkOS)
4760c6a4ff8STeresa Johnson       : ModulePass(ID), OS(o), ThinLinkOS(ThinLinkOS) {
4771398a32eSPeter Collingbourne     initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry());
4781398a32eSPeter Collingbourne   }
4791398a32eSPeter Collingbourne 
4801398a32eSPeter Collingbourne   StringRef getPassName() const override { return "ThinLTO Bitcode Writer"; }
4811398a32eSPeter Collingbourne 
4821398a32eSPeter Collingbourne   bool runOnModule(Module &M) override {
4831398a32eSPeter Collingbourne     const ModuleSummaryIndex *Index =
4841398a32eSPeter Collingbourne         &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex());
4850c6a4ff8STeresa Johnson     writeThinLTOBitcode(OS, ThinLinkOS, LegacyAARGetter(*this), M, Index);
4861398a32eSPeter Collingbourne     return true;
4871398a32eSPeter Collingbourne   }
4881398a32eSPeter Collingbourne   void getAnalysisUsage(AnalysisUsage &AU) const override {
4891398a32eSPeter Collingbourne     AU.setPreservesAll();
490002c2d53SPeter Collingbourne     AU.addRequired<AssumptionCacheTracker>();
4911398a32eSPeter Collingbourne     AU.addRequired<ModuleSummaryIndexWrapperPass>();
492002c2d53SPeter Collingbourne     AU.addRequired<TargetLibraryInfoWrapperPass>();
4931398a32eSPeter Collingbourne   }
4941398a32eSPeter Collingbourne };
4951398a32eSPeter Collingbourne } // anonymous namespace
4961398a32eSPeter Collingbourne 
4971398a32eSPeter Collingbourne char WriteThinLTOBitcode::ID = 0;
4981398a32eSPeter Collingbourne INITIALIZE_PASS_BEGIN(WriteThinLTOBitcode, "write-thinlto-bitcode",
4991398a32eSPeter Collingbourne                       "Write ThinLTO Bitcode", false, true)
500002c2d53SPeter Collingbourne INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
5011398a32eSPeter Collingbourne INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
502002c2d53SPeter Collingbourne INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
5031398a32eSPeter Collingbourne INITIALIZE_PASS_END(WriteThinLTOBitcode, "write-thinlto-bitcode",
5041398a32eSPeter Collingbourne                     "Write ThinLTO Bitcode", false, true)
5051398a32eSPeter Collingbourne 
5060c6a4ff8STeresa Johnson ModulePass *llvm::createWriteThinLTOBitcodePass(raw_ostream &Str,
5070c6a4ff8STeresa Johnson                                                 raw_ostream *ThinLinkOS) {
5080c6a4ff8STeresa Johnson   return new WriteThinLTOBitcode(Str, ThinLinkOS);
5091398a32eSPeter Collingbourne }
5106b411418STim Shen 
5116b411418STim Shen PreservedAnalyses
5126b411418STim Shen llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
5136b411418STim Shen   FunctionAnalysisManager &FAM =
5146b411418STim Shen       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
5156b411418STim Shen   writeThinLTOBitcode(OS, ThinLinkOS,
5166b411418STim Shen                       [&FAM](Function &F) -> AAResults & {
5176b411418STim Shen                         return FAM.getResult<AAManager>(F);
5186b411418STim Shen                       },
5196b411418STim Shen                       M, &AM.getResult<ModuleSummaryIndexAnalysis>(M));
5206b411418STim Shen   return PreservedAnalyses::all();
5216b411418STim Shen }
522