10b57cec5SDimitry Andric //===- ModuleSummaryAnalysis.cpp - Module summary index builder -----------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This pass builds a ModuleSummaryIndex object for the module, to be written 100b57cec5SDimitry Andric // to bitcode or LLVM assembly. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "llvm/Analysis/ModuleSummaryAnalysis.h" 150b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 160b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h" 170b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h" 180b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 190b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h" 200b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 210b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 220b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 230b57cec5SDimitry Andric #include "llvm/Analysis/BlockFrequencyInfo.h" 240b57cec5SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h" 250b57cec5SDimitry Andric #include "llvm/Analysis/IndirectCallPromotionAnalysis.h" 260b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h" 270b57cec5SDimitry Andric #include "llvm/Analysis/ProfileSummaryInfo.h" 28*5ffd83dbSDimitry Andric #include "llvm/Analysis/StackSafetyAnalysis.h" 290b57cec5SDimitry Andric #include "llvm/Analysis/TypeMetadataUtils.h" 300b57cec5SDimitry Andric #include "llvm/IR/Attributes.h" 310b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 320b57cec5SDimitry Andric #include "llvm/IR/Constant.h" 330b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 340b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 350b57cec5SDimitry Andric #include "llvm/IR/Function.h" 360b57cec5SDimitry Andric #include "llvm/IR/GlobalAlias.h" 370b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 380b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 390b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 400b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 410b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h" 420b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 430b57cec5SDimitry Andric #include "llvm/IR/Module.h" 440b57cec5SDimitry Andric #include "llvm/IR/ModuleSummaryIndex.h" 450b57cec5SDimitry Andric #include "llvm/IR/Use.h" 460b57cec5SDimitry Andric #include "llvm/IR/User.h" 47480093f4SDimitry Andric #include "llvm/InitializePasses.h" 480b57cec5SDimitry Andric #include "llvm/Object/ModuleSymbolTable.h" 490b57cec5SDimitry Andric #include "llvm/Object/SymbolicFile.h" 500b57cec5SDimitry Andric #include "llvm/Pass.h" 510b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 520b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 530b57cec5SDimitry Andric #include <algorithm> 540b57cec5SDimitry Andric #include <cassert> 550b57cec5SDimitry Andric #include <cstdint> 560b57cec5SDimitry Andric #include <vector> 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric using namespace llvm; 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric #define DEBUG_TYPE "module-summary-analysis" 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric // Option to force edges cold which will block importing when the 630b57cec5SDimitry Andric // -import-cold-multiplier is set to 0. Useful for debugging. 640b57cec5SDimitry Andric FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold = 650b57cec5SDimitry Andric FunctionSummary::FSHT_None; 660b57cec5SDimitry Andric cl::opt<FunctionSummary::ForceSummaryHotnessType, true> FSEC( 670b57cec5SDimitry Andric "force-summary-edges-cold", cl::Hidden, cl::location(ForceSummaryEdgesCold), 680b57cec5SDimitry Andric cl::desc("Force all edges in the function summary to cold"), 690b57cec5SDimitry Andric cl::values(clEnumValN(FunctionSummary::FSHT_None, "none", "None."), 700b57cec5SDimitry Andric clEnumValN(FunctionSummary::FSHT_AllNonCritical, 710b57cec5SDimitry Andric "all-non-critical", "All non-critical edges."), 720b57cec5SDimitry Andric clEnumValN(FunctionSummary::FSHT_All, "all", "All edges."))); 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric cl::opt<std::string> ModuleSummaryDotFile( 750b57cec5SDimitry Andric "module-summary-dot-file", cl::init(""), cl::Hidden, 760b57cec5SDimitry Andric cl::value_desc("filename"), 770b57cec5SDimitry Andric cl::desc("File to emit dot graph of new summary into.")); 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric // Walk through the operands of a given User via worklist iteration and populate 800b57cec5SDimitry Andric // the set of GlobalValue references encountered. Invoked either on an 810b57cec5SDimitry Andric // Instruction or a GlobalVariable (which walks its initializer). 820b57cec5SDimitry Andric // Return true if any of the operands contains blockaddress. This is important 830b57cec5SDimitry Andric // to know when computing summary for global var, because if global variable 840b57cec5SDimitry Andric // references basic block address we can't import it separately from function 850b57cec5SDimitry Andric // containing that basic block. For simplicity we currently don't import such 860b57cec5SDimitry Andric // global vars at all. When importing function we aren't interested if any 870b57cec5SDimitry Andric // instruction in it takes an address of any basic block, because instruction 880b57cec5SDimitry Andric // can only take an address of basic block located in the same function. 890b57cec5SDimitry Andric static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser, 900b57cec5SDimitry Andric SetVector<ValueInfo> &RefEdges, 910b57cec5SDimitry Andric SmallPtrSet<const User *, 8> &Visited) { 920b57cec5SDimitry Andric bool HasBlockAddress = false; 930b57cec5SDimitry Andric SmallVector<const User *, 32> Worklist; 940b57cec5SDimitry Andric Worklist.push_back(CurUser); 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric while (!Worklist.empty()) { 970b57cec5SDimitry Andric const User *U = Worklist.pop_back_val(); 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric if (!Visited.insert(U).second) 1000b57cec5SDimitry Andric continue; 1010b57cec5SDimitry Andric 102*5ffd83dbSDimitry Andric const auto *CB = dyn_cast<CallBase>(U); 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric for (const auto &OI : U->operands()) { 1050b57cec5SDimitry Andric const User *Operand = dyn_cast<User>(OI); 1060b57cec5SDimitry Andric if (!Operand) 1070b57cec5SDimitry Andric continue; 1080b57cec5SDimitry Andric if (isa<BlockAddress>(Operand)) { 1090b57cec5SDimitry Andric HasBlockAddress = true; 1100b57cec5SDimitry Andric continue; 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric if (auto *GV = dyn_cast<GlobalValue>(Operand)) { 1130b57cec5SDimitry Andric // We have a reference to a global value. This should be added to 1140b57cec5SDimitry Andric // the reference set unless it is a callee. Callees are handled 1150b57cec5SDimitry Andric // specially by WriteFunction and are added to a separate list. 116*5ffd83dbSDimitry Andric if (!(CB && CB->isCallee(&OI))) 1170b57cec5SDimitry Andric RefEdges.insert(Index.getOrInsertValueInfo(GV)); 1180b57cec5SDimitry Andric continue; 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric Worklist.push_back(Operand); 1210b57cec5SDimitry Andric } 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric return HasBlockAddress; 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount, 1270b57cec5SDimitry Andric ProfileSummaryInfo *PSI) { 1280b57cec5SDimitry Andric if (!PSI) 1290b57cec5SDimitry Andric return CalleeInfo::HotnessType::Unknown; 1300b57cec5SDimitry Andric if (PSI->isHotCount(ProfileCount)) 1310b57cec5SDimitry Andric return CalleeInfo::HotnessType::Hot; 1320b57cec5SDimitry Andric if (PSI->isColdCount(ProfileCount)) 1330b57cec5SDimitry Andric return CalleeInfo::HotnessType::Cold; 1340b57cec5SDimitry Andric return CalleeInfo::HotnessType::None; 1350b57cec5SDimitry Andric } 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric static bool isNonRenamableLocal(const GlobalValue &GV) { 1380b57cec5SDimitry Andric return GV.hasSection() && GV.hasLocalLinkage(); 1390b57cec5SDimitry Andric } 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric /// Determine whether this call has all constant integer arguments (excluding 1420b57cec5SDimitry Andric /// "this") and summarize it to VCalls or ConstVCalls as appropriate. 1430b57cec5SDimitry Andric static void addVCallToSet(DevirtCallSite Call, GlobalValue::GUID Guid, 1440b57cec5SDimitry Andric SetVector<FunctionSummary::VFuncId> &VCalls, 1450b57cec5SDimitry Andric SetVector<FunctionSummary::ConstVCall> &ConstVCalls) { 1460b57cec5SDimitry Andric std::vector<uint64_t> Args; 1470b57cec5SDimitry Andric // Start from the second argument to skip the "this" pointer. 148*5ffd83dbSDimitry Andric for (auto &Arg : make_range(Call.CB.arg_begin() + 1, Call.CB.arg_end())) { 1490b57cec5SDimitry Andric auto *CI = dyn_cast<ConstantInt>(Arg); 1500b57cec5SDimitry Andric if (!CI || CI->getBitWidth() > 64) { 1510b57cec5SDimitry Andric VCalls.insert({Guid, Call.Offset}); 1520b57cec5SDimitry Andric return; 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric Args.push_back(CI->getZExtValue()); 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric ConstVCalls.insert({{Guid, Call.Offset}, std::move(Args)}); 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric /// If this intrinsic call requires that we add information to the function 1600b57cec5SDimitry Andric /// summary, do so via the non-constant reference arguments. 1610b57cec5SDimitry Andric static void addIntrinsicToSummary( 1620b57cec5SDimitry Andric const CallInst *CI, SetVector<GlobalValue::GUID> &TypeTests, 1630b57cec5SDimitry Andric SetVector<FunctionSummary::VFuncId> &TypeTestAssumeVCalls, 1640b57cec5SDimitry Andric SetVector<FunctionSummary::VFuncId> &TypeCheckedLoadVCalls, 1650b57cec5SDimitry Andric SetVector<FunctionSummary::ConstVCall> &TypeTestAssumeConstVCalls, 1660b57cec5SDimitry Andric SetVector<FunctionSummary::ConstVCall> &TypeCheckedLoadConstVCalls, 1670b57cec5SDimitry Andric DominatorTree &DT) { 1680b57cec5SDimitry Andric switch (CI->getCalledFunction()->getIntrinsicID()) { 1690b57cec5SDimitry Andric case Intrinsic::type_test: { 1700b57cec5SDimitry Andric auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1)); 1710b57cec5SDimitry Andric auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata()); 1720b57cec5SDimitry Andric if (!TypeId) 1730b57cec5SDimitry Andric break; 1740b57cec5SDimitry Andric GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString()); 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric // Produce a summary from type.test intrinsics. We only summarize type.test 1770b57cec5SDimitry Andric // intrinsics that are used other than by an llvm.assume intrinsic. 1780b57cec5SDimitry Andric // Intrinsics that are assumed are relevant only to the devirtualization 1790b57cec5SDimitry Andric // pass, not the type test lowering pass. 1800b57cec5SDimitry Andric bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) { 1810b57cec5SDimitry Andric auto *AssumeCI = dyn_cast<CallInst>(CIU.getUser()); 1820b57cec5SDimitry Andric if (!AssumeCI) 1830b57cec5SDimitry Andric return true; 1840b57cec5SDimitry Andric Function *F = AssumeCI->getCalledFunction(); 1850b57cec5SDimitry Andric return !F || F->getIntrinsicID() != Intrinsic::assume; 1860b57cec5SDimitry Andric }); 1870b57cec5SDimitry Andric if (HasNonAssumeUses) 1880b57cec5SDimitry Andric TypeTests.insert(Guid); 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric SmallVector<DevirtCallSite, 4> DevirtCalls; 1910b57cec5SDimitry Andric SmallVector<CallInst *, 4> Assumes; 1920b57cec5SDimitry Andric findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI, DT); 1930b57cec5SDimitry Andric for (auto &Call : DevirtCalls) 1940b57cec5SDimitry Andric addVCallToSet(Call, Guid, TypeTestAssumeVCalls, 1950b57cec5SDimitry Andric TypeTestAssumeConstVCalls); 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric break; 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric case Intrinsic::type_checked_load: { 2010b57cec5SDimitry Andric auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(2)); 2020b57cec5SDimitry Andric auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata()); 2030b57cec5SDimitry Andric if (!TypeId) 2040b57cec5SDimitry Andric break; 2050b57cec5SDimitry Andric GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString()); 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric SmallVector<DevirtCallSite, 4> DevirtCalls; 2080b57cec5SDimitry Andric SmallVector<Instruction *, 4> LoadedPtrs; 2090b57cec5SDimitry Andric SmallVector<Instruction *, 4> Preds; 2100b57cec5SDimitry Andric bool HasNonCallUses = false; 2110b57cec5SDimitry Andric findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds, 2120b57cec5SDimitry Andric HasNonCallUses, CI, DT); 2130b57cec5SDimitry Andric // Any non-call uses of the result of llvm.type.checked.load will 2140b57cec5SDimitry Andric // prevent us from optimizing away the llvm.type.test. 2150b57cec5SDimitry Andric if (HasNonCallUses) 2160b57cec5SDimitry Andric TypeTests.insert(Guid); 2170b57cec5SDimitry Andric for (auto &Call : DevirtCalls) 2180b57cec5SDimitry Andric addVCallToSet(Call, Guid, TypeCheckedLoadVCalls, 2190b57cec5SDimitry Andric TypeCheckedLoadConstVCalls); 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric break; 2220b57cec5SDimitry Andric } 2230b57cec5SDimitry Andric default: 2240b57cec5SDimitry Andric break; 2250b57cec5SDimitry Andric } 2260b57cec5SDimitry Andric } 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric static bool isNonVolatileLoad(const Instruction *I) { 2290b57cec5SDimitry Andric if (const auto *LI = dyn_cast<LoadInst>(I)) 2300b57cec5SDimitry Andric return !LI->isVolatile(); 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric return false; 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric static bool isNonVolatileStore(const Instruction *I) { 2360b57cec5SDimitry Andric if (const auto *SI = dyn_cast<StoreInst>(I)) 2370b57cec5SDimitry Andric return !SI->isVolatile(); 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric return false; 2400b57cec5SDimitry Andric } 2410b57cec5SDimitry Andric 242*5ffd83dbSDimitry Andric static void computeFunctionSummary( 243*5ffd83dbSDimitry Andric ModuleSummaryIndex &Index, const Module &M, const Function &F, 244*5ffd83dbSDimitry Andric BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, DominatorTree &DT, 245*5ffd83dbSDimitry Andric bool HasLocalsInUsedOrAsm, DenseSet<GlobalValue::GUID> &CantBePromoted, 246*5ffd83dbSDimitry Andric bool IsThinLTO, 247*5ffd83dbSDimitry Andric std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) { 2480b57cec5SDimitry Andric // Summary not currently supported for anonymous functions, they should 2490b57cec5SDimitry Andric // have been named. 2500b57cec5SDimitry Andric assert(F.hasName()); 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric unsigned NumInsts = 0; 2530b57cec5SDimitry Andric // Map from callee ValueId to profile count. Used to accumulate profile 2540b57cec5SDimitry Andric // counts for all static calls to a given callee. 2550b57cec5SDimitry Andric MapVector<ValueInfo, CalleeInfo> CallGraphEdges; 2560b57cec5SDimitry Andric SetVector<ValueInfo> RefEdges, LoadRefEdges, StoreRefEdges; 2570b57cec5SDimitry Andric SetVector<GlobalValue::GUID> TypeTests; 2580b57cec5SDimitry Andric SetVector<FunctionSummary::VFuncId> TypeTestAssumeVCalls, 2590b57cec5SDimitry Andric TypeCheckedLoadVCalls; 2600b57cec5SDimitry Andric SetVector<FunctionSummary::ConstVCall> TypeTestAssumeConstVCalls, 2610b57cec5SDimitry Andric TypeCheckedLoadConstVCalls; 2620b57cec5SDimitry Andric ICallPromotionAnalysis ICallAnalysis; 2630b57cec5SDimitry Andric SmallPtrSet<const User *, 8> Visited; 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric // Add personality function, prefix data and prologue data to function's ref 2660b57cec5SDimitry Andric // list. 2670b57cec5SDimitry Andric findRefEdges(Index, &F, RefEdges, Visited); 2680b57cec5SDimitry Andric std::vector<const Instruction *> NonVolatileLoads; 2690b57cec5SDimitry Andric std::vector<const Instruction *> NonVolatileStores; 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric bool HasInlineAsmMaybeReferencingInternal = false; 2720b57cec5SDimitry Andric for (const BasicBlock &BB : F) 2730b57cec5SDimitry Andric for (const Instruction &I : BB) { 2740b57cec5SDimitry Andric if (isa<DbgInfoIntrinsic>(I)) 2750b57cec5SDimitry Andric continue; 2760b57cec5SDimitry Andric ++NumInsts; 2770b57cec5SDimitry Andric // Regular LTO module doesn't participate in ThinLTO import, 2780b57cec5SDimitry Andric // so no reference from it can be read/writeonly, since this 2790b57cec5SDimitry Andric // would require importing variable as local copy 2800b57cec5SDimitry Andric if (IsThinLTO) { 2810b57cec5SDimitry Andric if (isNonVolatileLoad(&I)) { 2820b57cec5SDimitry Andric // Postpone processing of non-volatile load instructions 2830b57cec5SDimitry Andric // See comments below 2840b57cec5SDimitry Andric Visited.insert(&I); 2850b57cec5SDimitry Andric NonVolatileLoads.push_back(&I); 2860b57cec5SDimitry Andric continue; 2870b57cec5SDimitry Andric } else if (isNonVolatileStore(&I)) { 2880b57cec5SDimitry Andric Visited.insert(&I); 2890b57cec5SDimitry Andric NonVolatileStores.push_back(&I); 2900b57cec5SDimitry Andric // All references from second operand of store (destination address) 2910b57cec5SDimitry Andric // can be considered write-only if they're not referenced by any 2920b57cec5SDimitry Andric // non-store instruction. References from first operand of store 2930b57cec5SDimitry Andric // (stored value) can't be treated either as read- or as write-only 2940b57cec5SDimitry Andric // so we add them to RefEdges as we do with all other instructions 2950b57cec5SDimitry Andric // except non-volatile load. 2960b57cec5SDimitry Andric Value *Stored = I.getOperand(0); 2970b57cec5SDimitry Andric if (auto *GV = dyn_cast<GlobalValue>(Stored)) 2980b57cec5SDimitry Andric // findRefEdges will try to examine GV operands, so instead 2990b57cec5SDimitry Andric // of calling it we should add GV to RefEdges directly. 3000b57cec5SDimitry Andric RefEdges.insert(Index.getOrInsertValueInfo(GV)); 3010b57cec5SDimitry Andric else if (auto *U = dyn_cast<User>(Stored)) 3020b57cec5SDimitry Andric findRefEdges(Index, U, RefEdges, Visited); 3030b57cec5SDimitry Andric continue; 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric } 3060b57cec5SDimitry Andric findRefEdges(Index, &I, RefEdges, Visited); 307*5ffd83dbSDimitry Andric const auto *CB = dyn_cast<CallBase>(&I); 308*5ffd83dbSDimitry Andric if (!CB) 3090b57cec5SDimitry Andric continue; 3100b57cec5SDimitry Andric 3110b57cec5SDimitry Andric const auto *CI = dyn_cast<CallInst>(&I); 3120b57cec5SDimitry Andric // Since we don't know exactly which local values are referenced in inline 3130b57cec5SDimitry Andric // assembly, conservatively mark the function as possibly referencing 3140b57cec5SDimitry Andric // a local value from inline assembly to ensure we don't export a 3150b57cec5SDimitry Andric // reference (which would require renaming and promotion of the 3160b57cec5SDimitry Andric // referenced value). 3170b57cec5SDimitry Andric if (HasLocalsInUsedOrAsm && CI && CI->isInlineAsm()) 3180b57cec5SDimitry Andric HasInlineAsmMaybeReferencingInternal = true; 3190b57cec5SDimitry Andric 320*5ffd83dbSDimitry Andric auto *CalledValue = CB->getCalledOperand(); 321*5ffd83dbSDimitry Andric auto *CalledFunction = CB->getCalledFunction(); 3220b57cec5SDimitry Andric if (CalledValue && !CalledFunction) { 3238bcb0991SDimitry Andric CalledValue = CalledValue->stripPointerCasts(); 3240b57cec5SDimitry Andric // Stripping pointer casts can reveal a called function. 3250b57cec5SDimitry Andric CalledFunction = dyn_cast<Function>(CalledValue); 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric // Check if this is an alias to a function. If so, get the 3280b57cec5SDimitry Andric // called aliasee for the checks below. 3290b57cec5SDimitry Andric if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) { 3300b57cec5SDimitry Andric assert(!CalledFunction && "Expected null called function in callsite for alias"); 3310b57cec5SDimitry Andric CalledFunction = dyn_cast<Function>(GA->getBaseObject()); 3320b57cec5SDimitry Andric } 3330b57cec5SDimitry Andric // Check if this is a direct call to a known function or a known 3340b57cec5SDimitry Andric // intrinsic, or an indirect call with profile data. 3350b57cec5SDimitry Andric if (CalledFunction) { 3360b57cec5SDimitry Andric if (CI && CalledFunction->isIntrinsic()) { 3370b57cec5SDimitry Andric addIntrinsicToSummary( 3380b57cec5SDimitry Andric CI, TypeTests, TypeTestAssumeVCalls, TypeCheckedLoadVCalls, 3390b57cec5SDimitry Andric TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls, DT); 3400b57cec5SDimitry Andric continue; 3410b57cec5SDimitry Andric } 3420b57cec5SDimitry Andric // We should have named any anonymous globals 3430b57cec5SDimitry Andric assert(CalledFunction->hasName()); 344*5ffd83dbSDimitry Andric auto ScaledCount = PSI->getProfileCount(*CB, BFI); 3450b57cec5SDimitry Andric auto Hotness = ScaledCount ? getHotness(ScaledCount.getValue(), PSI) 3460b57cec5SDimitry Andric : CalleeInfo::HotnessType::Unknown; 3470b57cec5SDimitry Andric if (ForceSummaryEdgesCold != FunctionSummary::FSHT_None) 3480b57cec5SDimitry Andric Hotness = CalleeInfo::HotnessType::Cold; 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric // Use the original CalledValue, in case it was an alias. We want 3510b57cec5SDimitry Andric // to record the call edge to the alias in that case. Eventually 3520b57cec5SDimitry Andric // an alias summary will be created to associate the alias and 3530b57cec5SDimitry Andric // aliasee. 3540b57cec5SDimitry Andric auto &ValueInfo = CallGraphEdges[Index.getOrInsertValueInfo( 3550b57cec5SDimitry Andric cast<GlobalValue>(CalledValue))]; 3560b57cec5SDimitry Andric ValueInfo.updateHotness(Hotness); 3570b57cec5SDimitry Andric // Add the relative block frequency to CalleeInfo if there is no profile 3580b57cec5SDimitry Andric // information. 3590b57cec5SDimitry Andric if (BFI != nullptr && Hotness == CalleeInfo::HotnessType::Unknown) { 3600b57cec5SDimitry Andric uint64_t BBFreq = BFI->getBlockFreq(&BB).getFrequency(); 3610b57cec5SDimitry Andric uint64_t EntryFreq = BFI->getEntryFreq(); 3620b57cec5SDimitry Andric ValueInfo.updateRelBlockFreq(BBFreq, EntryFreq); 3630b57cec5SDimitry Andric } 3640b57cec5SDimitry Andric } else { 3650b57cec5SDimitry Andric // Skip inline assembly calls. 3660b57cec5SDimitry Andric if (CI && CI->isInlineAsm()) 3670b57cec5SDimitry Andric continue; 3680b57cec5SDimitry Andric // Skip direct calls. 3690b57cec5SDimitry Andric if (!CalledValue || isa<Constant>(CalledValue)) 3700b57cec5SDimitry Andric continue; 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric // Check if the instruction has a callees metadata. If so, add callees 3730b57cec5SDimitry Andric // to CallGraphEdges to reflect the references from the metadata, and 3740b57cec5SDimitry Andric // to enable importing for subsequent indirect call promotion and 3750b57cec5SDimitry Andric // inlining. 3760b57cec5SDimitry Andric if (auto *MD = I.getMetadata(LLVMContext::MD_callees)) { 3770b57cec5SDimitry Andric for (auto &Op : MD->operands()) { 3780b57cec5SDimitry Andric Function *Callee = mdconst::extract_or_null<Function>(Op); 3790b57cec5SDimitry Andric if (Callee) 3800b57cec5SDimitry Andric CallGraphEdges[Index.getOrInsertValueInfo(Callee)]; 3810b57cec5SDimitry Andric } 3820b57cec5SDimitry Andric } 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric uint32_t NumVals, NumCandidates; 3850b57cec5SDimitry Andric uint64_t TotalCount; 3860b57cec5SDimitry Andric auto CandidateProfileData = 3870b57cec5SDimitry Andric ICallAnalysis.getPromotionCandidatesForInstruction( 3880b57cec5SDimitry Andric &I, NumVals, TotalCount, NumCandidates); 3890b57cec5SDimitry Andric for (auto &Candidate : CandidateProfileData) 3900b57cec5SDimitry Andric CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)] 3910b57cec5SDimitry Andric .updateHotness(getHotness(Candidate.Count, PSI)); 3920b57cec5SDimitry Andric } 3930b57cec5SDimitry Andric } 394*5ffd83dbSDimitry Andric Index.addBlockCount(F.size()); 3950b57cec5SDimitry Andric 3960b57cec5SDimitry Andric std::vector<ValueInfo> Refs; 3970b57cec5SDimitry Andric if (IsThinLTO) { 3980b57cec5SDimitry Andric auto AddRefEdges = [&](const std::vector<const Instruction *> &Instrs, 3990b57cec5SDimitry Andric SetVector<ValueInfo> &Edges, 4000b57cec5SDimitry Andric SmallPtrSet<const User *, 8> &Cache) { 4010b57cec5SDimitry Andric for (const auto *I : Instrs) { 4020b57cec5SDimitry Andric Cache.erase(I); 4030b57cec5SDimitry Andric findRefEdges(Index, I, Edges, Cache); 4040b57cec5SDimitry Andric } 4050b57cec5SDimitry Andric }; 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric // By now we processed all instructions in a function, except 4080b57cec5SDimitry Andric // non-volatile loads and non-volatile value stores. Let's find 4090b57cec5SDimitry Andric // ref edges for both of instruction sets 4100b57cec5SDimitry Andric AddRefEdges(NonVolatileLoads, LoadRefEdges, Visited); 4110b57cec5SDimitry Andric // We can add some values to the Visited set when processing load 4120b57cec5SDimitry Andric // instructions which are also used by stores in NonVolatileStores. 4130b57cec5SDimitry Andric // For example this can happen if we have following code: 4140b57cec5SDimitry Andric // 4150b57cec5SDimitry Andric // store %Derived* @foo, %Derived** bitcast (%Base** @bar to %Derived**) 4160b57cec5SDimitry Andric // %42 = load %Derived*, %Derived** bitcast (%Base** @bar to %Derived**) 4170b57cec5SDimitry Andric // 4180b57cec5SDimitry Andric // After processing loads we'll add bitcast to the Visited set, and if 4190b57cec5SDimitry Andric // we use the same set while processing stores, we'll never see store 4200b57cec5SDimitry Andric // to @bar and @bar will be mistakenly treated as readonly. 4210b57cec5SDimitry Andric SmallPtrSet<const llvm::User *, 8> StoreCache; 4220b57cec5SDimitry Andric AddRefEdges(NonVolatileStores, StoreRefEdges, StoreCache); 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric // If both load and store instruction reference the same variable 4250b57cec5SDimitry Andric // we won't be able to optimize it. Add all such reference edges 4260b57cec5SDimitry Andric // to RefEdges set. 4270b57cec5SDimitry Andric for (auto &VI : StoreRefEdges) 4280b57cec5SDimitry Andric if (LoadRefEdges.remove(VI)) 4290b57cec5SDimitry Andric RefEdges.insert(VI); 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric unsigned RefCnt = RefEdges.size(); 4320b57cec5SDimitry Andric // All new reference edges inserted in two loops below are either 4330b57cec5SDimitry Andric // read or write only. They will be grouped in the end of RefEdges 4340b57cec5SDimitry Andric // vector, so we can use a single integer value to identify them. 4350b57cec5SDimitry Andric for (auto &VI : LoadRefEdges) 4360b57cec5SDimitry Andric RefEdges.insert(VI); 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric unsigned FirstWORef = RefEdges.size(); 4390b57cec5SDimitry Andric for (auto &VI : StoreRefEdges) 4400b57cec5SDimitry Andric RefEdges.insert(VI); 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andric Refs = RefEdges.takeVector(); 4430b57cec5SDimitry Andric for (; RefCnt < FirstWORef; ++RefCnt) 4440b57cec5SDimitry Andric Refs[RefCnt].setReadOnly(); 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric for (; RefCnt < Refs.size(); ++RefCnt) 4470b57cec5SDimitry Andric Refs[RefCnt].setWriteOnly(); 4480b57cec5SDimitry Andric } else { 4490b57cec5SDimitry Andric Refs = RefEdges.takeVector(); 4500b57cec5SDimitry Andric } 4510b57cec5SDimitry Andric // Explicit add hot edges to enforce importing for designated GUIDs for 4520b57cec5SDimitry Andric // sample PGO, to enable the same inlines as the profiled optimized binary. 4530b57cec5SDimitry Andric for (auto &I : F.getImportGUIDs()) 4540b57cec5SDimitry Andric CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness( 4550b57cec5SDimitry Andric ForceSummaryEdgesCold == FunctionSummary::FSHT_All 4560b57cec5SDimitry Andric ? CalleeInfo::HotnessType::Cold 4570b57cec5SDimitry Andric : CalleeInfo::HotnessType::Critical); 4580b57cec5SDimitry Andric 4590b57cec5SDimitry Andric bool NonRenamableLocal = isNonRenamableLocal(F); 4600b57cec5SDimitry Andric bool NotEligibleForImport = 4610b57cec5SDimitry Andric NonRenamableLocal || HasInlineAsmMaybeReferencingInternal; 4620b57cec5SDimitry Andric GlobalValueSummary::GVFlags Flags(F.getLinkage(), NotEligibleForImport, 4630b57cec5SDimitry Andric /* Live = */ false, F.isDSOLocal(), 4640b57cec5SDimitry Andric F.hasLinkOnceODRLinkage() && F.hasGlobalUnnamedAddr()); 4650b57cec5SDimitry Andric FunctionSummary::FFlags FunFlags{ 4660b57cec5SDimitry Andric F.hasFnAttribute(Attribute::ReadNone), 4670b57cec5SDimitry Andric F.hasFnAttribute(Attribute::ReadOnly), 4680b57cec5SDimitry Andric F.hasFnAttribute(Attribute::NoRecurse), F.returnDoesNotAlias(), 4690b57cec5SDimitry Andric // FIXME: refactor this to use the same code that inliner is using. 4700b57cec5SDimitry Andric // Don't try to import functions with noinline attribute. 471480093f4SDimitry Andric F.getAttributes().hasFnAttribute(Attribute::NoInline), 472480093f4SDimitry Andric F.hasFnAttribute(Attribute::AlwaysInline)}; 473*5ffd83dbSDimitry Andric std::vector<FunctionSummary::ParamAccess> ParamAccesses; 474*5ffd83dbSDimitry Andric if (auto *SSI = GetSSICallback(F)) 475*5ffd83dbSDimitry Andric ParamAccesses = SSI->getParamAccesses(); 4768bcb0991SDimitry Andric auto FuncSummary = std::make_unique<FunctionSummary>( 4770b57cec5SDimitry Andric Flags, NumInsts, FunFlags, /*EntryCount=*/0, std::move(Refs), 4780b57cec5SDimitry Andric CallGraphEdges.takeVector(), TypeTests.takeVector(), 4790b57cec5SDimitry Andric TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(), 4800b57cec5SDimitry Andric TypeTestAssumeConstVCalls.takeVector(), 481*5ffd83dbSDimitry Andric TypeCheckedLoadConstVCalls.takeVector(), std::move(ParamAccesses)); 4820b57cec5SDimitry Andric if (NonRenamableLocal) 4830b57cec5SDimitry Andric CantBePromoted.insert(F.getGUID()); 4840b57cec5SDimitry Andric Index.addGlobalValueSummary(F, std::move(FuncSummary)); 4850b57cec5SDimitry Andric } 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric /// Find function pointers referenced within the given vtable initializer 4880b57cec5SDimitry Andric /// (or subset of an initializer) \p I. The starting offset of \p I within 4890b57cec5SDimitry Andric /// the vtable initializer is \p StartingOffset. Any discovered function 4900b57cec5SDimitry Andric /// pointers are added to \p VTableFuncs along with their cumulative offset 4910b57cec5SDimitry Andric /// within the initializer. 4920b57cec5SDimitry Andric static void findFuncPointers(const Constant *I, uint64_t StartingOffset, 4930b57cec5SDimitry Andric const Module &M, ModuleSummaryIndex &Index, 4940b57cec5SDimitry Andric VTableFuncList &VTableFuncs) { 4950b57cec5SDimitry Andric // First check if this is a function pointer. 4960b57cec5SDimitry Andric if (I->getType()->isPointerTy()) { 4970b57cec5SDimitry Andric auto Fn = dyn_cast<Function>(I->stripPointerCasts()); 4980b57cec5SDimitry Andric // We can disregard __cxa_pure_virtual as a possible call target, as 4990b57cec5SDimitry Andric // calls to pure virtuals are UB. 5000b57cec5SDimitry Andric if (Fn && Fn->getName() != "__cxa_pure_virtual") 5010b57cec5SDimitry Andric VTableFuncs.push_back({Index.getOrInsertValueInfo(Fn), StartingOffset}); 5020b57cec5SDimitry Andric return; 5030b57cec5SDimitry Andric } 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric // Walk through the elements in the constant struct or array and recursively 5060b57cec5SDimitry Andric // look for virtual function pointers. 5070b57cec5SDimitry Andric const DataLayout &DL = M.getDataLayout(); 5080b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantStruct>(I)) { 5090b57cec5SDimitry Andric StructType *STy = dyn_cast<StructType>(C->getType()); 5100b57cec5SDimitry Andric assert(STy); 5110b57cec5SDimitry Andric const StructLayout *SL = DL.getStructLayout(C->getType()); 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric for (StructType::element_iterator EB = STy->element_begin(), EI = EB, 5140b57cec5SDimitry Andric EE = STy->element_end(); 5150b57cec5SDimitry Andric EI != EE; ++EI) { 5160b57cec5SDimitry Andric auto Offset = SL->getElementOffset(EI - EB); 5170b57cec5SDimitry Andric unsigned Op = SL->getElementContainingOffset(Offset); 5180b57cec5SDimitry Andric findFuncPointers(cast<Constant>(I->getOperand(Op)), 5190b57cec5SDimitry Andric StartingOffset + Offset, M, Index, VTableFuncs); 5200b57cec5SDimitry Andric } 5210b57cec5SDimitry Andric } else if (auto *C = dyn_cast<ConstantArray>(I)) { 5220b57cec5SDimitry Andric ArrayType *ATy = C->getType(); 5230b57cec5SDimitry Andric Type *EltTy = ATy->getElementType(); 5240b57cec5SDimitry Andric uint64_t EltSize = DL.getTypeAllocSize(EltTy); 5250b57cec5SDimitry Andric for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) { 5260b57cec5SDimitry Andric findFuncPointers(cast<Constant>(I->getOperand(i)), 5270b57cec5SDimitry Andric StartingOffset + i * EltSize, M, Index, VTableFuncs); 5280b57cec5SDimitry Andric } 5290b57cec5SDimitry Andric } 5300b57cec5SDimitry Andric } 5310b57cec5SDimitry Andric 5320b57cec5SDimitry Andric // Identify the function pointers referenced by vtable definition \p V. 5330b57cec5SDimitry Andric static void computeVTableFuncs(ModuleSummaryIndex &Index, 5340b57cec5SDimitry Andric const GlobalVariable &V, const Module &M, 5350b57cec5SDimitry Andric VTableFuncList &VTableFuncs) { 5360b57cec5SDimitry Andric if (!V.isConstant()) 5370b57cec5SDimitry Andric return; 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric findFuncPointers(V.getInitializer(), /*StartingOffset=*/0, M, Index, 5400b57cec5SDimitry Andric VTableFuncs); 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric #ifndef NDEBUG 5430b57cec5SDimitry Andric // Validate that the VTableFuncs list is ordered by offset. 5440b57cec5SDimitry Andric uint64_t PrevOffset = 0; 5450b57cec5SDimitry Andric for (auto &P : VTableFuncs) { 5460b57cec5SDimitry Andric // The findVFuncPointers traversal should have encountered the 5470b57cec5SDimitry Andric // functions in offset order. We need to use ">=" since PrevOffset 5480b57cec5SDimitry Andric // starts at 0. 5490b57cec5SDimitry Andric assert(P.VTableOffset >= PrevOffset); 5500b57cec5SDimitry Andric PrevOffset = P.VTableOffset; 5510b57cec5SDimitry Andric } 5520b57cec5SDimitry Andric #endif 5530b57cec5SDimitry Andric } 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric /// Record vtable definition \p V for each type metadata it references. 5560b57cec5SDimitry Andric static void 5570b57cec5SDimitry Andric recordTypeIdCompatibleVtableReferences(ModuleSummaryIndex &Index, 5580b57cec5SDimitry Andric const GlobalVariable &V, 5590b57cec5SDimitry Andric SmallVectorImpl<MDNode *> &Types) { 5600b57cec5SDimitry Andric for (MDNode *Type : Types) { 5610b57cec5SDimitry Andric auto TypeID = Type->getOperand(1).get(); 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric uint64_t Offset = 5640b57cec5SDimitry Andric cast<ConstantInt>( 5650b57cec5SDimitry Andric cast<ConstantAsMetadata>(Type->getOperand(0))->getValue()) 5660b57cec5SDimitry Andric ->getZExtValue(); 5670b57cec5SDimitry Andric 5680b57cec5SDimitry Andric if (auto *TypeId = dyn_cast<MDString>(TypeID)) 5690b57cec5SDimitry Andric Index.getOrInsertTypeIdCompatibleVtableSummary(TypeId->getString()) 5700b57cec5SDimitry Andric .push_back({Offset, Index.getOrInsertValueInfo(&V)}); 5710b57cec5SDimitry Andric } 5720b57cec5SDimitry Andric } 5730b57cec5SDimitry Andric 5740b57cec5SDimitry Andric static void computeVariableSummary(ModuleSummaryIndex &Index, 5750b57cec5SDimitry Andric const GlobalVariable &V, 5760b57cec5SDimitry Andric DenseSet<GlobalValue::GUID> &CantBePromoted, 5770b57cec5SDimitry Andric const Module &M, 5780b57cec5SDimitry Andric SmallVectorImpl<MDNode *> &Types) { 5790b57cec5SDimitry Andric SetVector<ValueInfo> RefEdges; 5800b57cec5SDimitry Andric SmallPtrSet<const User *, 8> Visited; 5810b57cec5SDimitry Andric bool HasBlockAddress = findRefEdges(Index, &V, RefEdges, Visited); 5820b57cec5SDimitry Andric bool NonRenamableLocal = isNonRenamableLocal(V); 5830b57cec5SDimitry Andric GlobalValueSummary::GVFlags Flags(V.getLinkage(), NonRenamableLocal, 5840b57cec5SDimitry Andric /* Live = */ false, V.isDSOLocal(), 5850b57cec5SDimitry Andric V.hasLinkOnceODRLinkage() && V.hasGlobalUnnamedAddr()); 5860b57cec5SDimitry Andric 5870b57cec5SDimitry Andric VTableFuncList VTableFuncs; 5880b57cec5SDimitry Andric // If splitting is not enabled, then we compute the summary information 5890b57cec5SDimitry Andric // necessary for index-based whole program devirtualization. 5900b57cec5SDimitry Andric if (!Index.enableSplitLTOUnit()) { 5910b57cec5SDimitry Andric Types.clear(); 5920b57cec5SDimitry Andric V.getMetadata(LLVMContext::MD_type, Types); 5930b57cec5SDimitry Andric if (!Types.empty()) { 5940b57cec5SDimitry Andric // Identify the function pointers referenced by this vtable definition. 5950b57cec5SDimitry Andric computeVTableFuncs(Index, V, M, VTableFuncs); 5960b57cec5SDimitry Andric 5970b57cec5SDimitry Andric // Record this vtable definition for each type metadata it references. 5980b57cec5SDimitry Andric recordTypeIdCompatibleVtableReferences(Index, V, Types); 5990b57cec5SDimitry Andric } 6000b57cec5SDimitry Andric } 6010b57cec5SDimitry Andric 6020b57cec5SDimitry Andric // Don't mark variables we won't be able to internalize as read/write-only. 6030b57cec5SDimitry Andric bool CanBeInternalized = 6040b57cec5SDimitry Andric !V.hasComdat() && !V.hasAppendingLinkage() && !V.isInterposable() && 6050b57cec5SDimitry Andric !V.hasAvailableExternallyLinkage() && !V.hasDLLExportStorageClass(); 606*5ffd83dbSDimitry Andric bool Constant = V.isConstant(); 607*5ffd83dbSDimitry Andric GlobalVarSummary::GVarFlags VarFlags(CanBeInternalized, 608*5ffd83dbSDimitry Andric Constant ? false : CanBeInternalized, 609*5ffd83dbSDimitry Andric Constant, V.getVCallVisibility()); 6108bcb0991SDimitry Andric auto GVarSummary = std::make_unique<GlobalVarSummary>(Flags, VarFlags, 6110b57cec5SDimitry Andric RefEdges.takeVector()); 6120b57cec5SDimitry Andric if (NonRenamableLocal) 6130b57cec5SDimitry Andric CantBePromoted.insert(V.getGUID()); 6140b57cec5SDimitry Andric if (HasBlockAddress) 6150b57cec5SDimitry Andric GVarSummary->setNotEligibleToImport(); 6160b57cec5SDimitry Andric if (!VTableFuncs.empty()) 6170b57cec5SDimitry Andric GVarSummary->setVTableFuncs(VTableFuncs); 6180b57cec5SDimitry Andric Index.addGlobalValueSummary(V, std::move(GVarSummary)); 6190b57cec5SDimitry Andric } 6200b57cec5SDimitry Andric 6210b57cec5SDimitry Andric static void 6220b57cec5SDimitry Andric computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A, 6230b57cec5SDimitry Andric DenseSet<GlobalValue::GUID> &CantBePromoted) { 6240b57cec5SDimitry Andric bool NonRenamableLocal = isNonRenamableLocal(A); 6250b57cec5SDimitry Andric GlobalValueSummary::GVFlags Flags(A.getLinkage(), NonRenamableLocal, 6260b57cec5SDimitry Andric /* Live = */ false, A.isDSOLocal(), 6270b57cec5SDimitry Andric A.hasLinkOnceODRLinkage() && A.hasGlobalUnnamedAddr()); 6288bcb0991SDimitry Andric auto AS = std::make_unique<AliasSummary>(Flags); 6290b57cec5SDimitry Andric auto *Aliasee = A.getBaseObject(); 6300b57cec5SDimitry Andric auto AliaseeVI = Index.getValueInfo(Aliasee->getGUID()); 6310b57cec5SDimitry Andric assert(AliaseeVI && "Alias expects aliasee summary to be available"); 6320b57cec5SDimitry Andric assert(AliaseeVI.getSummaryList().size() == 1 && 6330b57cec5SDimitry Andric "Expected a single entry per aliasee in per-module index"); 6340b57cec5SDimitry Andric AS->setAliasee(AliaseeVI, AliaseeVI.getSummaryList()[0].get()); 6350b57cec5SDimitry Andric if (NonRenamableLocal) 6360b57cec5SDimitry Andric CantBePromoted.insert(A.getGUID()); 6370b57cec5SDimitry Andric Index.addGlobalValueSummary(A, std::move(AS)); 6380b57cec5SDimitry Andric } 6390b57cec5SDimitry Andric 6400b57cec5SDimitry Andric // Set LiveRoot flag on entries matching the given value name. 6410b57cec5SDimitry Andric static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) { 6420b57cec5SDimitry Andric if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name))) 6430b57cec5SDimitry Andric for (auto &Summary : VI.getSummaryList()) 6440b57cec5SDimitry Andric Summary->setLive(true); 6450b57cec5SDimitry Andric } 6460b57cec5SDimitry Andric 6470b57cec5SDimitry Andric ModuleSummaryIndex llvm::buildModuleSummaryIndex( 6480b57cec5SDimitry Andric const Module &M, 6490b57cec5SDimitry Andric std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback, 650*5ffd83dbSDimitry Andric ProfileSummaryInfo *PSI, 651*5ffd83dbSDimitry Andric std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) { 6520b57cec5SDimitry Andric assert(PSI); 6530b57cec5SDimitry Andric bool EnableSplitLTOUnit = false; 6540b57cec5SDimitry Andric if (auto *MD = mdconst::extract_or_null<ConstantInt>( 6550b57cec5SDimitry Andric M.getModuleFlag("EnableSplitLTOUnit"))) 6560b57cec5SDimitry Andric EnableSplitLTOUnit = MD->getZExtValue(); 6570b57cec5SDimitry Andric ModuleSummaryIndex Index(/*HaveGVs=*/true, EnableSplitLTOUnit); 6580b57cec5SDimitry Andric 6590b57cec5SDimitry Andric // Identify the local values in the llvm.used and llvm.compiler.used sets, 6600b57cec5SDimitry Andric // which should not be exported as they would then require renaming and 6610b57cec5SDimitry Andric // promotion, but we may have opaque uses e.g. in inline asm. We collect them 6620b57cec5SDimitry Andric // here because we use this information to mark functions containing inline 6630b57cec5SDimitry Andric // assembly calls as not importable. 6640b57cec5SDimitry Andric SmallPtrSet<GlobalValue *, 8> LocalsUsed; 6650b57cec5SDimitry Andric SmallPtrSet<GlobalValue *, 8> Used; 6660b57cec5SDimitry Andric // First collect those in the llvm.used set. 6670b57cec5SDimitry Andric collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); 6680b57cec5SDimitry Andric // Next collect those in the llvm.compiler.used set. 6690b57cec5SDimitry Andric collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true); 6700b57cec5SDimitry Andric DenseSet<GlobalValue::GUID> CantBePromoted; 6710b57cec5SDimitry Andric for (auto *V : Used) { 6720b57cec5SDimitry Andric if (V->hasLocalLinkage()) { 6730b57cec5SDimitry Andric LocalsUsed.insert(V); 6740b57cec5SDimitry Andric CantBePromoted.insert(V->getGUID()); 6750b57cec5SDimitry Andric } 6760b57cec5SDimitry Andric } 6770b57cec5SDimitry Andric 6780b57cec5SDimitry Andric bool HasLocalInlineAsmSymbol = false; 6790b57cec5SDimitry Andric if (!M.getModuleInlineAsm().empty()) { 6800b57cec5SDimitry Andric // Collect the local values defined by module level asm, and set up 6810b57cec5SDimitry Andric // summaries for these symbols so that they can be marked as NoRename, 6820b57cec5SDimitry Andric // to prevent export of any use of them in regular IR that would require 6830b57cec5SDimitry Andric // renaming within the module level asm. Note we don't need to create a 6840b57cec5SDimitry Andric // summary for weak or global defs, as they don't need to be flagged as 6850b57cec5SDimitry Andric // NoRename, and defs in module level asm can't be imported anyway. 6860b57cec5SDimitry Andric // Also, any values used but not defined within module level asm should 6870b57cec5SDimitry Andric // be listed on the llvm.used or llvm.compiler.used global and marked as 6880b57cec5SDimitry Andric // referenced from there. 6890b57cec5SDimitry Andric ModuleSymbolTable::CollectAsmSymbols( 6900b57cec5SDimitry Andric M, [&](StringRef Name, object::BasicSymbolRef::Flags Flags) { 6910b57cec5SDimitry Andric // Symbols not marked as Weak or Global are local definitions. 6920b57cec5SDimitry Andric if (Flags & (object::BasicSymbolRef::SF_Weak | 6930b57cec5SDimitry Andric object::BasicSymbolRef::SF_Global)) 6940b57cec5SDimitry Andric return; 6950b57cec5SDimitry Andric HasLocalInlineAsmSymbol = true; 6960b57cec5SDimitry Andric GlobalValue *GV = M.getNamedValue(Name); 6970b57cec5SDimitry Andric if (!GV) 6980b57cec5SDimitry Andric return; 6990b57cec5SDimitry Andric assert(GV->isDeclaration() && "Def in module asm already has definition"); 7000b57cec5SDimitry Andric GlobalValueSummary::GVFlags GVFlags(GlobalValue::InternalLinkage, 7010b57cec5SDimitry Andric /* NotEligibleToImport = */ true, 7020b57cec5SDimitry Andric /* Live = */ true, 7030b57cec5SDimitry Andric /* Local */ GV->isDSOLocal(), 7040b57cec5SDimitry Andric GV->hasLinkOnceODRLinkage() && GV->hasGlobalUnnamedAddr()); 7050b57cec5SDimitry Andric CantBePromoted.insert(GV->getGUID()); 7060b57cec5SDimitry Andric // Create the appropriate summary type. 7070b57cec5SDimitry Andric if (Function *F = dyn_cast<Function>(GV)) { 7080b57cec5SDimitry Andric std::unique_ptr<FunctionSummary> Summary = 7098bcb0991SDimitry Andric std::make_unique<FunctionSummary>( 7100b57cec5SDimitry Andric GVFlags, /*InstCount=*/0, 7110b57cec5SDimitry Andric FunctionSummary::FFlags{ 7120b57cec5SDimitry Andric F->hasFnAttribute(Attribute::ReadNone), 7130b57cec5SDimitry Andric F->hasFnAttribute(Attribute::ReadOnly), 7140b57cec5SDimitry Andric F->hasFnAttribute(Attribute::NoRecurse), 7150b57cec5SDimitry Andric F->returnDoesNotAlias(), 716480093f4SDimitry Andric /* NoInline = */ false, 717480093f4SDimitry Andric F->hasFnAttribute(Attribute::AlwaysInline)}, 7180b57cec5SDimitry Andric /*EntryCount=*/0, ArrayRef<ValueInfo>{}, 7190b57cec5SDimitry Andric ArrayRef<FunctionSummary::EdgeTy>{}, 7200b57cec5SDimitry Andric ArrayRef<GlobalValue::GUID>{}, 7210b57cec5SDimitry Andric ArrayRef<FunctionSummary::VFuncId>{}, 7220b57cec5SDimitry Andric ArrayRef<FunctionSummary::VFuncId>{}, 7230b57cec5SDimitry Andric ArrayRef<FunctionSummary::ConstVCall>{}, 724*5ffd83dbSDimitry Andric ArrayRef<FunctionSummary::ConstVCall>{}, 725*5ffd83dbSDimitry Andric ArrayRef<FunctionSummary::ParamAccess>{}); 7260b57cec5SDimitry Andric Index.addGlobalValueSummary(*GV, std::move(Summary)); 7270b57cec5SDimitry Andric } else { 7280b57cec5SDimitry Andric std::unique_ptr<GlobalVarSummary> Summary = 7298bcb0991SDimitry Andric std::make_unique<GlobalVarSummary>( 730*5ffd83dbSDimitry Andric GVFlags, 731*5ffd83dbSDimitry Andric GlobalVarSummary::GVarFlags( 732*5ffd83dbSDimitry Andric false, false, cast<GlobalVariable>(GV)->isConstant(), 733*5ffd83dbSDimitry Andric GlobalObject::VCallVisibilityPublic), 7340b57cec5SDimitry Andric ArrayRef<ValueInfo>{}); 7350b57cec5SDimitry Andric Index.addGlobalValueSummary(*GV, std::move(Summary)); 7360b57cec5SDimitry Andric } 7370b57cec5SDimitry Andric }); 7380b57cec5SDimitry Andric } 7390b57cec5SDimitry Andric 7400b57cec5SDimitry Andric bool IsThinLTO = true; 7410b57cec5SDimitry Andric if (auto *MD = 7420b57cec5SDimitry Andric mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO"))) 7430b57cec5SDimitry Andric IsThinLTO = MD->getZExtValue(); 7440b57cec5SDimitry Andric 7450b57cec5SDimitry Andric // Compute summaries for all functions defined in module, and save in the 7460b57cec5SDimitry Andric // index. 7470b57cec5SDimitry Andric for (auto &F : M) { 7480b57cec5SDimitry Andric if (F.isDeclaration()) 7490b57cec5SDimitry Andric continue; 7500b57cec5SDimitry Andric 7510b57cec5SDimitry Andric DominatorTree DT(const_cast<Function &>(F)); 7520b57cec5SDimitry Andric BlockFrequencyInfo *BFI = nullptr; 7530b57cec5SDimitry Andric std::unique_ptr<BlockFrequencyInfo> BFIPtr; 7540b57cec5SDimitry Andric if (GetBFICallback) 7550b57cec5SDimitry Andric BFI = GetBFICallback(F); 7560b57cec5SDimitry Andric else if (F.hasProfileData()) { 7570b57cec5SDimitry Andric LoopInfo LI{DT}; 7580b57cec5SDimitry Andric BranchProbabilityInfo BPI{F, LI}; 7598bcb0991SDimitry Andric BFIPtr = std::make_unique<BlockFrequencyInfo>(F, BPI, LI); 7600b57cec5SDimitry Andric BFI = BFIPtr.get(); 7610b57cec5SDimitry Andric } 7620b57cec5SDimitry Andric 7630b57cec5SDimitry Andric computeFunctionSummary(Index, M, F, BFI, PSI, DT, 7640b57cec5SDimitry Andric !LocalsUsed.empty() || HasLocalInlineAsmSymbol, 765*5ffd83dbSDimitry Andric CantBePromoted, IsThinLTO, GetSSICallback); 7660b57cec5SDimitry Andric } 7670b57cec5SDimitry Andric 7680b57cec5SDimitry Andric // Compute summaries for all variables defined in module, and save in the 7690b57cec5SDimitry Andric // index. 7700b57cec5SDimitry Andric SmallVector<MDNode *, 2> Types; 7710b57cec5SDimitry Andric for (const GlobalVariable &G : M.globals()) { 7720b57cec5SDimitry Andric if (G.isDeclaration()) 7730b57cec5SDimitry Andric continue; 7740b57cec5SDimitry Andric computeVariableSummary(Index, G, CantBePromoted, M, Types); 7750b57cec5SDimitry Andric } 7760b57cec5SDimitry Andric 7770b57cec5SDimitry Andric // Compute summaries for all aliases defined in module, and save in the 7780b57cec5SDimitry Andric // index. 7790b57cec5SDimitry Andric for (const GlobalAlias &A : M.aliases()) 7800b57cec5SDimitry Andric computeAliasSummary(Index, A, CantBePromoted); 7810b57cec5SDimitry Andric 7820b57cec5SDimitry Andric for (auto *V : LocalsUsed) { 7830b57cec5SDimitry Andric auto *Summary = Index.getGlobalValueSummary(*V); 7840b57cec5SDimitry Andric assert(Summary && "Missing summary for global value"); 7850b57cec5SDimitry Andric Summary->setNotEligibleToImport(); 7860b57cec5SDimitry Andric } 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric // The linker doesn't know about these LLVM produced values, so we need 7890b57cec5SDimitry Andric // to flag them as live in the index to ensure index-based dead value 7900b57cec5SDimitry Andric // analysis treats them as live roots of the analysis. 7910b57cec5SDimitry Andric setLiveRoot(Index, "llvm.used"); 7920b57cec5SDimitry Andric setLiveRoot(Index, "llvm.compiler.used"); 7930b57cec5SDimitry Andric setLiveRoot(Index, "llvm.global_ctors"); 7940b57cec5SDimitry Andric setLiveRoot(Index, "llvm.global_dtors"); 7950b57cec5SDimitry Andric setLiveRoot(Index, "llvm.global.annotations"); 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric for (auto &GlobalList : Index) { 7980b57cec5SDimitry Andric // Ignore entries for references that are undefined in the current module. 7990b57cec5SDimitry Andric if (GlobalList.second.SummaryList.empty()) 8000b57cec5SDimitry Andric continue; 8010b57cec5SDimitry Andric 8020b57cec5SDimitry Andric assert(GlobalList.second.SummaryList.size() == 1 && 8030b57cec5SDimitry Andric "Expected module's index to have one summary per GUID"); 8040b57cec5SDimitry Andric auto &Summary = GlobalList.second.SummaryList[0]; 8050b57cec5SDimitry Andric if (!IsThinLTO) { 8060b57cec5SDimitry Andric Summary->setNotEligibleToImport(); 8070b57cec5SDimitry Andric continue; 8080b57cec5SDimitry Andric } 8090b57cec5SDimitry Andric 8100b57cec5SDimitry Andric bool AllRefsCanBeExternallyReferenced = 8110b57cec5SDimitry Andric llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) { 8120b57cec5SDimitry Andric return !CantBePromoted.count(VI.getGUID()); 8130b57cec5SDimitry Andric }); 8140b57cec5SDimitry Andric if (!AllRefsCanBeExternallyReferenced) { 8150b57cec5SDimitry Andric Summary->setNotEligibleToImport(); 8160b57cec5SDimitry Andric continue; 8170b57cec5SDimitry Andric } 8180b57cec5SDimitry Andric 8190b57cec5SDimitry Andric if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) { 8200b57cec5SDimitry Andric bool AllCallsCanBeExternallyReferenced = llvm::all_of( 8210b57cec5SDimitry Andric FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) { 8220b57cec5SDimitry Andric return !CantBePromoted.count(Edge.first.getGUID()); 8230b57cec5SDimitry Andric }); 8240b57cec5SDimitry Andric if (!AllCallsCanBeExternallyReferenced) 8250b57cec5SDimitry Andric Summary->setNotEligibleToImport(); 8260b57cec5SDimitry Andric } 8270b57cec5SDimitry Andric } 8280b57cec5SDimitry Andric 8290b57cec5SDimitry Andric if (!ModuleSummaryDotFile.empty()) { 8300b57cec5SDimitry Andric std::error_code EC; 8318bcb0991SDimitry Andric raw_fd_ostream OSDot(ModuleSummaryDotFile, EC, sys::fs::OpenFlags::OF_None); 8320b57cec5SDimitry Andric if (EC) 8330b57cec5SDimitry Andric report_fatal_error(Twine("Failed to open dot file ") + 8340b57cec5SDimitry Andric ModuleSummaryDotFile + ": " + EC.message() + "\n"); 835480093f4SDimitry Andric Index.exportToDot(OSDot, {}); 8360b57cec5SDimitry Andric } 8370b57cec5SDimitry Andric 8380b57cec5SDimitry Andric return Index; 8390b57cec5SDimitry Andric } 8400b57cec5SDimitry Andric 8410b57cec5SDimitry Andric AnalysisKey ModuleSummaryIndexAnalysis::Key; 8420b57cec5SDimitry Andric 8430b57cec5SDimitry Andric ModuleSummaryIndex 8440b57cec5SDimitry Andric ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) { 8450b57cec5SDimitry Andric ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M); 8460b57cec5SDimitry Andric auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 847*5ffd83dbSDimitry Andric bool NeedSSI = needsParamAccessSummary(M); 8480b57cec5SDimitry Andric return buildModuleSummaryIndex( 8490b57cec5SDimitry Andric M, 8500b57cec5SDimitry Andric [&FAM](const Function &F) { 8510b57cec5SDimitry Andric return &FAM.getResult<BlockFrequencyAnalysis>( 8520b57cec5SDimitry Andric *const_cast<Function *>(&F)); 8530b57cec5SDimitry Andric }, 854*5ffd83dbSDimitry Andric &PSI, 855*5ffd83dbSDimitry Andric [&FAM, NeedSSI](const Function &F) -> const StackSafetyInfo * { 856*5ffd83dbSDimitry Andric return NeedSSI ? &FAM.getResult<StackSafetyAnalysis>( 857*5ffd83dbSDimitry Andric const_cast<Function &>(F)) 858*5ffd83dbSDimitry Andric : nullptr; 859*5ffd83dbSDimitry Andric }); 8600b57cec5SDimitry Andric } 8610b57cec5SDimitry Andric 8620b57cec5SDimitry Andric char ModuleSummaryIndexWrapperPass::ID = 0; 8630b57cec5SDimitry Andric 8640b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis", 8650b57cec5SDimitry Andric "Module Summary Analysis", false, true) 8660b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass) 8670b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) 868*5ffd83dbSDimitry Andric INITIALIZE_PASS_DEPENDENCY(StackSafetyInfoWrapperPass) 8690b57cec5SDimitry Andric INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis", 8700b57cec5SDimitry Andric "Module Summary Analysis", false, true) 8710b57cec5SDimitry Andric 8720b57cec5SDimitry Andric ModulePass *llvm::createModuleSummaryIndexWrapperPass() { 8730b57cec5SDimitry Andric return new ModuleSummaryIndexWrapperPass(); 8740b57cec5SDimitry Andric } 8750b57cec5SDimitry Andric 8760b57cec5SDimitry Andric ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass() 8770b57cec5SDimitry Andric : ModulePass(ID) { 8780b57cec5SDimitry Andric initializeModuleSummaryIndexWrapperPassPass(*PassRegistry::getPassRegistry()); 8790b57cec5SDimitry Andric } 8800b57cec5SDimitry Andric 8810b57cec5SDimitry Andric bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) { 8820b57cec5SDimitry Andric auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); 883*5ffd83dbSDimitry Andric bool NeedSSI = needsParamAccessSummary(M); 8840b57cec5SDimitry Andric Index.emplace(buildModuleSummaryIndex( 8850b57cec5SDimitry Andric M, 8860b57cec5SDimitry Andric [this](const Function &F) { 8870b57cec5SDimitry Andric return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>( 8880b57cec5SDimitry Andric *const_cast<Function *>(&F)) 8890b57cec5SDimitry Andric .getBFI()); 8900b57cec5SDimitry Andric }, 891*5ffd83dbSDimitry Andric PSI, 892*5ffd83dbSDimitry Andric [&](const Function &F) -> const StackSafetyInfo * { 893*5ffd83dbSDimitry Andric return NeedSSI ? &getAnalysis<StackSafetyInfoWrapperPass>( 894*5ffd83dbSDimitry Andric const_cast<Function &>(F)) 895*5ffd83dbSDimitry Andric .getResult() 896*5ffd83dbSDimitry Andric : nullptr; 897*5ffd83dbSDimitry Andric })); 8980b57cec5SDimitry Andric return false; 8990b57cec5SDimitry Andric } 9000b57cec5SDimitry Andric 9010b57cec5SDimitry Andric bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) { 9020b57cec5SDimitry Andric Index.reset(); 9030b57cec5SDimitry Andric return false; 9040b57cec5SDimitry Andric } 9050b57cec5SDimitry Andric 9060b57cec5SDimitry Andric void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 9070b57cec5SDimitry Andric AU.setPreservesAll(); 9080b57cec5SDimitry Andric AU.addRequired<BlockFrequencyInfoWrapperPass>(); 9090b57cec5SDimitry Andric AU.addRequired<ProfileSummaryInfoWrapperPass>(); 910*5ffd83dbSDimitry Andric AU.addRequired<StackSafetyInfoWrapperPass>(); 9110b57cec5SDimitry Andric } 912*5ffd83dbSDimitry Andric 913*5ffd83dbSDimitry Andric char ImmutableModuleSummaryIndexWrapperPass::ID = 0; 914*5ffd83dbSDimitry Andric 915*5ffd83dbSDimitry Andric ImmutableModuleSummaryIndexWrapperPass::ImmutableModuleSummaryIndexWrapperPass( 916*5ffd83dbSDimitry Andric const ModuleSummaryIndex *Index) 917*5ffd83dbSDimitry Andric : ImmutablePass(ID), Index(Index) { 918*5ffd83dbSDimitry Andric initializeImmutableModuleSummaryIndexWrapperPassPass( 919*5ffd83dbSDimitry Andric *PassRegistry::getPassRegistry()); 920*5ffd83dbSDimitry Andric } 921*5ffd83dbSDimitry Andric 922*5ffd83dbSDimitry Andric void ImmutableModuleSummaryIndexWrapperPass::getAnalysisUsage( 923*5ffd83dbSDimitry Andric AnalysisUsage &AU) const { 924*5ffd83dbSDimitry Andric AU.setPreservesAll(); 925*5ffd83dbSDimitry Andric } 926*5ffd83dbSDimitry Andric 927*5ffd83dbSDimitry Andric ImmutablePass *llvm::createImmutableModuleSummaryIndexWrapperPass( 928*5ffd83dbSDimitry Andric const ModuleSummaryIndex *Index) { 929*5ffd83dbSDimitry Andric return new ImmutableModuleSummaryIndexWrapperPass(Index); 930*5ffd83dbSDimitry Andric } 931*5ffd83dbSDimitry Andric 932*5ffd83dbSDimitry Andric INITIALIZE_PASS(ImmutableModuleSummaryIndexWrapperPass, "module-summary-info", 933*5ffd83dbSDimitry Andric "Module summary info", false, true) 934